Implement unboxed sum primitive type
[ghc.git] / compiler / hsSyn / HsTypes.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5
6 HsTypes: Abstract syntax: user-defined types
7 -}
8
9 {-# LANGUAGE DeriveDataTypeable #-}
10 {-# LANGUAGE FlexibleContexts #-}
11 {-# LANGUAGE FlexibleInstances #-}
12 {-# LANGUAGE StandaloneDeriving #-}
13 {-# LANGUAGE TypeSynonymInstances #-}
14 {-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
15 -- in module PlaceHolder
16 {-# LANGUAGE ConstraintKinds #-}
17 {-# LANGUAGE CPP #-}
18
19 module HsTypes (
20 HsType(..), LHsType, HsKind, LHsKind,
21 HsTyVarBndr(..), LHsTyVarBndr,
22 LHsQTyVars(..),
23 HsImplicitBndrs(..),
24 HsWildCardBndrs(..),
25 LHsSigType, LHsSigWcType, LHsWcType,
26 HsTupleSort(..),
27 HsContext, LHsContext,
28 HsTyLit(..),
29 HsIPName(..), hsIPNameFS,
30 HsAppType(..),LHsAppType,
31
32 LBangType, BangType,
33 HsSrcBang(..), HsImplBang(..),
34 SrcStrictness(..), SrcUnpackedness(..),
35 getBangType, getBangStrictness,
36
37 ConDeclField(..), LConDeclField, pprConDeclFields, updateGadtResult,
38
39 HsConDetails(..),
40
41 FieldOcc(..), LFieldOcc, mkFieldOcc,
42 AmbiguousFieldOcc(..), mkAmbiguousFieldOcc,
43 rdrNameAmbiguousFieldOcc, selectorAmbiguousFieldOcc,
44 unambiguousFieldOcc, ambiguousFieldOcc,
45
46 HsWildCardInfo(..), mkAnonWildCardTy,
47 wildCardName, sameWildCard,
48
49 mkHsImplicitBndrs, mkHsWildCardBndrs, hsImplicitBody,
50 mkEmptyImplicitBndrs, mkEmptyWildCardBndrs,
51 mkHsQTvs, hsQTvExplicit, emptyLHsQTvs, isEmptyLHsQTvs,
52 isHsKindedTyVar, hsTvbAllKinded,
53 hsScopedTvs, hsWcScopedTvs, dropWildCards,
54 hsTyVarName, hsAllLTyVarNames, hsLTyVarLocNames,
55 hsLTyVarName, hsLTyVarLocName, hsExplicitLTyVarNames,
56 splitLHsInstDeclTy, getLHsInstDeclHead, getLHsInstDeclClass_maybe,
57 splitLHsPatSynTy,
58 splitLHsForAllTy, splitLHsQualTy, splitLHsSigmaTy,
59 splitHsFunType, splitHsAppsTy,
60 splitHsAppTys, getAppsTyHead_maybe, hsTyGetAppHead_maybe,
61 mkHsOpTy, mkHsAppTy, mkHsAppTys,
62 ignoreParens, hsSigType, hsSigWcType,
63 hsLTyVarBndrToType, hsLTyVarBndrsToTypes,
64
65 -- Printing
66 pprParendHsType, pprHsForAll, pprHsForAllTvs, pprHsForAllExtra,
67 pprHsContext, pprHsContextNoArrow, pprHsContextMaybe
68 ) where
69
70 import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
71
72 import PlaceHolder ( PostTc,PostRn,DataId,PlaceHolder(..),
73 OutputableBndrId )
74
75 import Id ( Id )
76 import Name( Name )
77 import RdrName ( RdrName )
78 import NameSet ( NameSet, emptyNameSet )
79 import DataCon( HsSrcBang(..), HsImplBang(..),
80 SrcStrictness(..), SrcUnpackedness(..) )
81 import TysPrim( funTyConName )
82 import Type
83 import HsDoc
84 import BasicTypes
85 import SrcLoc
86 import StaticFlags
87 import Outputable
88 import FastString
89 import Maybes( isJust )
90
91 import Data.Data hiding ( Fixity )
92 import Data.Maybe ( fromMaybe )
93 import Control.Monad ( unless )
94 #if __GLASGOW_HASKELL > 710
95 import Data.Semigroup ( Semigroup )
96 import qualified Data.Semigroup as Semigroup
97 #endif
98
99 {-
100 ************************************************************************
101 * *
102 \subsection{Bang annotations}
103 * *
104 ************************************************************************
105 -}
106
107 type LBangType name = Located (BangType name)
108 type BangType name = HsType name -- Bangs are in the HsType data type
109
110 getBangType :: LHsType a -> LHsType a
111 getBangType (L _ (HsBangTy _ ty)) = ty
112 getBangType ty = ty
113
114 getBangStrictness :: LHsType a -> HsSrcBang
115 getBangStrictness (L _ (HsBangTy s _)) = s
116 getBangStrictness _ = (HsSrcBang Nothing NoSrcUnpack NoSrcStrict)
117
118 {-
119 ************************************************************************
120 * *
121 \subsection{Data types}
122 * *
123 ************************************************************************
124
125 This is the syntax for types as seen in type signatures.
126
127 Note [HsBSig binder lists]
128 ~~~~~~~~~~~~~~~~~~~~~~~~~~
129 Consider a binder (or pattern) decorated with a type or kind,
130 \ (x :: a -> a). blah
131 forall (a :: k -> *) (b :: k). blah
132 Then we use a LHsBndrSig on the binder, so that the
133 renamer can decorate it with the variables bound
134 by the pattern ('a' in the first example, 'k' in the second),
135 assuming that neither of them is in scope already
136 See also Note [Kind and type-variable binders] in RnTypes
137
138 Note [HsType binders]
139 ~~~~~~~~~~~~~~~~~~~~~
140 The system for recording type and kind-variable binders in HsTypes
141 is a bit complicated. Here's how it works.
142
143 * In a HsType,
144 HsForAllTy represents an /explicit, user-written/ 'forall'
145 e.g. forall a b. ...
146 HsQualTy represents an /explicit, user-written/ context
147 e.g. (Eq a, Show a) => ...
148 The context can be empty if that's what the user wrote
149 These constructors represent what the user wrote, no more
150 and no less.
151
152 * HsTyVarBndr describes a quantified type variable written by the
153 user. For example
154 f :: forall a (b :: *). blah
155 here 'a' and '(b::*)' are each a HsTyVarBndr. A HsForAllTy has
156 a list of LHsTyVarBndrs.
157
158 * HsImplicitBndrs is a wrapper that gives the implicitly-quantified
159 kind and type variables of the wrapped thing. It is filled in by
160 the renamer. For example, if the user writes
161 f :: a -> a
162 the HsImplicitBinders binds the 'a' (not a HsForAllTy!).
163 NB: this implicit quantification is purely lexical: we bind any
164 type or kind variables that are not in scope. The type checker
165 may subsequently quantify over further kind variables.
166
167 * HsWildCardBndrs is a wrapper that binds the wildcard variables
168 of the wrapped thing. It is filled in by the renamer
169 f :: _a -> _
170 The enclosing HsWildCardBndrs binds the wildcards _a and _.
171
172 * The explicit presence of these wrappers specifies, in the HsSyn,
173 exactly where implicit quantification is allowed, and where
174 wildcards are allowed.
175
176 * LHsQTyVars is used in data/class declarations, where the user gives
177 explicit *type* variable bindings, but we need to implicitly bind
178 *kind* variables. For example
179 class C (a :: k -> *) where ...
180 The 'k' is implicitly bound in the hsq_tvs field of LHsQTyVars
181
182 Note [The wildcard story for types]
183 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184 Types can have wildcards in them, to support partial type signatures,
185 like f :: Int -> (_ , _a) -> _a
186
187 A wildcard in a type can be
188
189 * An anonymous wildcard,
190 written '_'
191 In HsType this is represented by HsWildCardTy.
192 After the renamer, this contains a Name which uniquely
193 identifies this particular occurrence.
194
195 * A named wildcard,
196 written '_a', '_foo', etc
197 In HsType this is represented by (HsTyVar "_a")
198 i.e. a perfectly ordinary type variable that happens
199 to start with an underscore
200
201 Note carefully:
202
203 * When NamedWildCards is off, type variables that start with an
204 underscore really /are/ ordinary type variables. And indeed, even
205 when NamedWildCards is on you can bind _a explicitly as an ordinary
206 type variable:
207 data T _a _b = MkT _b _a
208 Or even:
209 f :: forall _a. _a -> _b
210 Here _a is an ordinary forall'd binder, but (With NamedWildCards)
211 _b is a named wildcard. (See the comments in Trac #10982)
212
213 * All wildcards, whether named or anonymous, are bound by the
214 HsWildCardBndrs construct, which wraps types that are allowed
215 to have wildcards.
216
217 * After type checking is done, we report what types the wildcards
218 got unified with.
219
220 -}
221
222 type LHsContext name = Located (HsContext name)
223 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnUnit'
224
225 -- For details on above see note [Api annotations] in ApiAnnotation
226
227 type HsContext name = [LHsType name]
228
229 type LHsType name = Located (HsType name)
230 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
231 -- in a list
232
233 -- For details on above see note [Api annotations] in ApiAnnotation
234 type HsKind name = HsType name
235 type LHsKind name = Located (HsKind name)
236 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
237
238 -- For details on above see note [Api annotations] in ApiAnnotation
239
240 --------------------------------------------------
241 -- LHsQTyVars
242 -- The explicitly-quantified binders in a data/type declaration
243
244 type LHsTyVarBndr name = Located (HsTyVarBndr name)
245 -- See Note [HsType binders]
246
247 data LHsQTyVars name -- See Note [HsType binders]
248 = HsQTvs { hsq_implicit :: PostRn name [Name] -- implicit (dependent) variables
249 , hsq_explicit :: [LHsTyVarBndr name] -- explicit variables
250 -- See Note [HsForAllTy tyvar binders]
251 , hsq_dependent :: PostRn name NameSet
252 -- which explicit vars are dependent
253 -- See Note [Dependent LHsQTyVars] in TcHsType
254 }
255
256 deriving instance (DataId name) => Data (LHsQTyVars name)
257
258 mkHsQTvs :: [LHsTyVarBndr RdrName] -> LHsQTyVars RdrName
259 mkHsQTvs tvs = HsQTvs { hsq_implicit = PlaceHolder, hsq_explicit = tvs
260 , hsq_dependent = PlaceHolder }
261
262 hsQTvExplicit :: LHsQTyVars name -> [LHsTyVarBndr name]
263 hsQTvExplicit = hsq_explicit
264
265 emptyLHsQTvs :: LHsQTyVars Name
266 emptyLHsQTvs = HsQTvs [] [] emptyNameSet
267
268 isEmptyLHsQTvs :: LHsQTyVars Name -> Bool
269 isEmptyLHsQTvs (HsQTvs [] [] _) = True
270 isEmptyLHsQTvs _ = False
271
272 ------------------------------------------------
273 -- HsImplicitBndrs
274 -- Used to quantify the binders of a type in cases
275 -- when a HsForAll isn't appropriate:
276 -- * Patterns in a type/data family instance (HsTyPats)
277 -- * Type of a rule binder (RuleBndr)
278 -- * Pattern type signatures (SigPatIn)
279 -- In the last of these, wildcards can happen, so we must accommodate them
280
281 data HsImplicitBndrs name thing -- See Note [HsType binders]
282 = HsIB { hsib_vars :: PostRn name [Name] -- Implicitly-bound kind & type vars
283 , hsib_body :: thing -- Main payload (type or list of types)
284 }
285
286 data HsWildCardBndrs name thing
287 -- See Note [HsType binders]
288 -- See Note [The wildcard story for types]
289 = HsWC { hswc_wcs :: PostRn name [Name]
290 -- Wild cards, both named and anonymous
291 -- after the renamer
292
293 , hswc_body :: thing
294 -- Main payload (type or list of types)
295 -- If there is an extra-constraints wildcard,
296 -- it's still there in the hsc_body.
297 }
298
299 deriving instance (Data name, Data thing, Data (PostRn name [Name]))
300 => Data (HsImplicitBndrs name thing)
301
302 deriving instance (Data name, Data thing, Data (PostRn name [Name]))
303 => Data (HsWildCardBndrs name thing)
304
305 type LHsSigType name = HsImplicitBndrs name (LHsType name) -- Implicit only
306 type LHsWcType name = HsWildCardBndrs name (LHsType name) -- Wildcard only
307 type LHsSigWcType name = HsWildCardBndrs name (LHsSigType name) -- Both
308
309 -- See Note [Representing type signatures]
310
311 hsImplicitBody :: HsImplicitBndrs name thing -> thing
312 hsImplicitBody (HsIB { hsib_body = body }) = body
313
314 hsSigType :: LHsSigType name -> LHsType name
315 hsSigType = hsImplicitBody
316
317 hsSigWcType :: LHsSigWcType name -> LHsType name
318 hsSigWcType sig_ty = hsib_body (hswc_body sig_ty)
319
320 dropWildCards :: LHsSigWcType name -> LHsSigType name
321 -- Drop the wildcard part of a LHsSigWcType
322 dropWildCards sig_ty = hswc_body sig_ty
323
324 {- Note [Representing type signatures]
325 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
326 HsSigType is used to represent an explicit user type signature
327 such as f :: a -> a
328 or g (x :: a -> a) = x
329
330 A HsSigType is just a HsImplicitBndrs wrapping a LHsType.
331 * The HsImplicitBndrs binds the /implicitly/ quantified tyvars
332 * The LHsType binds the /explictly/ quantified tyvars
333
334 E.g. For a signature like
335 f :: forall (a::k). blah
336 we get
337 HsIB { hsib_vars = [k]
338 , hsib_body = HsForAllTy { hst_bndrs = [(a::*)]
339 , hst_body = blah }
340 The implicit kind variable 'k' is bound by the HsIB;
341 the explictly forall'd tyvar 'a' is bounnd by the HsForAllTy
342 -}
343
344 mkHsImplicitBndrs :: thing -> HsImplicitBndrs RdrName thing
345 mkHsImplicitBndrs x = HsIB { hsib_body = x
346 , hsib_vars = PlaceHolder }
347
348 mkHsWildCardBndrs :: thing -> HsWildCardBndrs RdrName thing
349 mkHsWildCardBndrs x = HsWC { hswc_body = x
350 , hswc_wcs = PlaceHolder }
351
352 -- Add empty binders. This is a bit suspicious; what if
353 -- the wrapped thing had free type variables?
354 mkEmptyImplicitBndrs :: thing -> HsImplicitBndrs Name thing
355 mkEmptyImplicitBndrs x = HsIB { hsib_body = x
356 , hsib_vars = [] }
357
358 mkEmptyWildCardBndrs :: thing -> HsWildCardBndrs Name thing
359 mkEmptyWildCardBndrs x = HsWC { hswc_body = x
360 , hswc_wcs = [] }
361
362
363 --------------------------------------------------
364 -- | These names are used early on to store the names of implicit
365 -- parameters. They completely disappear after type-checking.
366 newtype HsIPName = HsIPName FastString
367 deriving( Eq, Data )
368
369 hsIPNameFS :: HsIPName -> FastString
370 hsIPNameFS (HsIPName n) = n
371
372 instance Outputable HsIPName where
373 ppr (HsIPName n) = char '?' <> ftext n -- Ordinary implicit parameters
374
375 instance OutputableBndr HsIPName where
376 pprBndr _ n = ppr n -- Simple for now
377 pprInfixOcc n = ppr n
378 pprPrefixOcc n = ppr n
379
380 --------------------------------------------------
381 data HsTyVarBndr name
382 = UserTyVar -- no explicit kinding
383 (Located name)
384 -- See Note [Located RdrNames] in HsExpr
385 | KindedTyVar
386 (Located name)
387 (LHsKind name) -- The user-supplied kind signature
388 -- ^
389 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
390 -- 'ApiAnnotation.AnnDcolon', 'ApiAnnotation.AnnClose'
391
392 -- For details on above see note [Api annotations] in ApiAnnotation
393 deriving instance (DataId name) => Data (HsTyVarBndr name)
394
395 -- | Does this 'HsTyVarBndr' come with an explicit kind annotation?
396 isHsKindedTyVar :: HsTyVarBndr name -> Bool
397 isHsKindedTyVar (UserTyVar {}) = False
398 isHsKindedTyVar (KindedTyVar {}) = True
399
400 -- | Do all type variables in this 'LHsQTyVars' come with kind annotations?
401 hsTvbAllKinded :: LHsQTyVars name -> Bool
402 hsTvbAllKinded = all (isHsKindedTyVar . unLoc) . hsQTvExplicit
403
404 data HsType name
405 = HsForAllTy -- See Note [HsType binders]
406 { hst_bndrs :: [LHsTyVarBndr name] -- Explicit, user-supplied 'forall a b c'
407 , hst_body :: LHsType name -- body type
408 }
409 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnForall',
410 -- 'ApiAnnotation.AnnDot','ApiAnnotation.AnnDarrow'
411 -- For details on above see note [Api annotations] in ApiAnnotation
412
413 | HsQualTy -- See Note [HsType binders]
414 { hst_ctxt :: LHsContext name -- Context C => blah
415 , hst_body :: LHsType name }
416
417 | HsTyVar (Located name)
418 -- Type variable, type constructor, or data constructor
419 -- see Note [Promotions (HsTyVar)]
420 -- See Note [Located RdrNames] in HsExpr
421 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
422
423 -- For details on above see note [Api annotations] in ApiAnnotation
424
425 | HsAppsTy [LHsAppType name] -- Used only before renaming,
426 -- Note [HsAppsTy]
427 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
428
429 | HsAppTy (LHsType name)
430 (LHsType name)
431 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
432
433 -- For details on above see note [Api annotations] in ApiAnnotation
434
435 | HsFunTy (LHsType name) -- function type
436 (LHsType name)
437 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow',
438
439 -- For details on above see note [Api annotations] in ApiAnnotation
440
441 | HsListTy (LHsType name) -- Element type
442 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
443 -- 'ApiAnnotation.AnnClose' @']'@
444
445 -- For details on above see note [Api annotations] in ApiAnnotation
446
447 | HsPArrTy (LHsType name) -- Elem. type of parallel array: [:t:]
448 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
449 -- 'ApiAnnotation.AnnClose' @':]'@
450
451 -- For details on above see note [Api annotations] in ApiAnnotation
452
453 | HsTupleTy HsTupleSort
454 [LHsType name] -- Element types (length gives arity)
455 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(' or '(#'@,
456 -- 'ApiAnnotation.AnnClose' @')' or '#)'@
457
458 -- For details on above see note [Api annotations] in ApiAnnotation
459
460 | HsSumTy [LHsType name] -- Element types (length gives arity)
461 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(#'@,
462 -- 'ApiAnnotation.AnnClose' '#)'@
463
464 -- For details on above see note [Api annotations] in ApiAnnotation
465
466 | HsOpTy (LHsType name) (Located name) (LHsType name)
467 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
468
469 -- For details on above see note [Api annotations] in ApiAnnotation
470
471 | HsParTy (LHsType name) -- See Note [Parens in HsSyn] in HsExpr
472 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
473 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
474 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
475 -- 'ApiAnnotation.AnnClose' @')'@
476
477 -- For details on above see note [Api annotations] in ApiAnnotation
478
479 | HsIParamTy HsIPName -- (?x :: ty)
480 (LHsType name) -- Implicit parameters as they occur in contexts
481 -- ^
482 -- > (?x :: ty)
483 --
484 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
485
486 -- For details on above see note [Api annotations] in ApiAnnotation
487
488 | HsEqTy (LHsType name) -- ty1 ~ ty2
489 (LHsType name) -- Always allowed even without TypeOperators, and has special kinding rule
490 -- ^
491 -- > ty1 ~ ty2
492 --
493 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'
494
495 -- For details on above see note [Api annotations] in ApiAnnotation
496
497 | HsKindSig (LHsType name) -- (ty :: kind)
498 (LHsKind name) -- A type with a kind signature
499 -- ^
500 -- > (ty :: kind)
501 --
502 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
503 -- 'ApiAnnotation.AnnDcolon','ApiAnnotation.AnnClose' @')'@
504
505 -- For details on above see note [Api annotations] in ApiAnnotation
506
507 | HsSpliceTy (HsSplice name) -- Includes quasi-quotes
508 (PostTc name Kind)
509 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'$('@,
510 -- 'ApiAnnotation.AnnClose' @')'@
511
512 -- For details on above see note [Api annotations] in ApiAnnotation
513
514 | HsDocTy (LHsType name) LHsDocString -- A documented type
515 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
516
517 -- For details on above see note [Api annotations] in ApiAnnotation
518
519 | HsBangTy HsSrcBang (LHsType name) -- Bang-style type annotations
520 -- ^ - 'ApiAnnotation.AnnKeywordId' :
521 -- 'ApiAnnotation.AnnOpen' @'{-\# UNPACK' or '{-\# NOUNPACK'@,
522 -- 'ApiAnnotation.AnnClose' @'#-}'@
523 -- 'ApiAnnotation.AnnBang' @\'!\'@
524
525 -- For details on above see note [Api annotations] in ApiAnnotation
526
527 | HsRecTy [LConDeclField name] -- Only in data type declarations
528 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
529 -- 'ApiAnnotation.AnnClose' @'}'@
530
531 -- For details on above see note [Api annotations] in ApiAnnotation
532
533 | HsCoreTy Type -- An escape hatch for tunnelling a *closed*
534 -- Core Type through HsSyn.
535 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
536
537 -- For details on above see note [Api annotations] in ApiAnnotation
538
539 | HsExplicitListTy -- A promoted explicit list
540 (PostTc name Kind) -- See Note [Promoted lists and tuples]
541 [LHsType name]
542 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'["@,
543 -- 'ApiAnnotation.AnnClose' @']'@
544
545 -- For details on above see note [Api annotations] in ApiAnnotation
546
547 | HsExplicitTupleTy -- A promoted explicit tuple
548 [PostTc name Kind] -- See Note [Promoted lists and tuples]
549 [LHsType name]
550 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'("@,
551 -- 'ApiAnnotation.AnnClose' @')'@
552
553 -- For details on above see note [Api annotations] in ApiAnnotation
554
555 | HsTyLit HsTyLit -- A promoted numeric literal.
556 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
557
558 -- For details on above see note [Api annotations] in ApiAnnotation
559
560 | HsWildCardTy (HsWildCardInfo name) -- A type wildcard
561 -- See Note [The wildcard story for types]
562 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
563
564 -- For details on above see note [Api annotations] in ApiAnnotation
565 deriving instance (DataId name) => Data (HsType name)
566
567 -- Note [Literal source text] in BasicTypes for SourceText fields in
568 -- the following
569 data HsTyLit
570 = HsNumTy SourceText Integer
571 | HsStrTy SourceText FastString
572 deriving Data
573
574 newtype HsWildCardInfo name -- See Note [The wildcard story for types]
575 = AnonWildCard (PostRn name (Located Name))
576 -- A anonymous wild card ('_'). A fresh Name is generated for
577 -- each individual anonymous wildcard during renaming
578 deriving instance (DataId name) => Data (HsWildCardInfo name)
579
580 type LHsAppType name = Located (HsAppType name)
581 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnSimpleQuote'
582
583 data HsAppType name
584 = HsAppInfix (Located name) -- either a symbol or an id in backticks
585 | HsAppPrefix (LHsType name) -- anything else, including things like (+)
586 deriving instance (DataId name) => Data (HsAppType name)
587
588 instance (OutputableBndrId name) => Outputable (HsAppType name) where
589 ppr = ppr_app_ty TopPrec
590
591 {-
592 Note [HsForAllTy tyvar binders]
593 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
594 After parsing:
595 * Implicit => empty
596 Explicit => the variables the user wrote
597
598 After renaming
599 * Implicit => the *type* variables free in the type
600 Explicit => the variables the user wrote (renamed)
601
602 Qualified currently behaves exactly as Implicit,
603 but it is deprecated to use it for implicit quantification.
604 In this case, GHC 7.10 gives a warning; see
605 Note [Context quantification] in RnTypes, and Trac #4426.
606 In GHC 8.0, Qualified will no longer bind variables
607 and this will become an error.
608
609 The kind variables bound in the hsq_implicit field come both
610 a) from the kind signatures on the kind vars (eg k1)
611 b) from the scope of the forall (eg k2)
612 Example: f :: forall (a::k1) b. T a (b::k2)
613
614
615 Note [Unit tuples]
616 ~~~~~~~~~~~~~~~~~~
617 Consider the type
618 type instance F Int = ()
619 We want to parse that "()"
620 as HsTupleTy HsBoxedOrConstraintTuple [],
621 NOT as HsTyVar unitTyCon
622
623 Why? Because F might have kind (* -> Constraint), so we when parsing we
624 don't know if that tuple is going to be a constraint tuple or an ordinary
625 unit tuple. The HsTupleSort flag is specifically designed to deal with
626 that, but it has to work for unit tuples too.
627
628 Note [Promotions (HsTyVar)]
629 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
630 HsTyVar: A name in a type or kind.
631 Here are the allowed namespaces for the name.
632 In a type:
633 Var: not allowed
634 Data: promoted data constructor
635 Tv: type variable
636 TcCls before renamer: type constructor, class constructor, or promoted data constructor
637 TcCls after renamer: type constructor or class constructor
638 In a kind:
639 Var, Data: not allowed
640 Tv: kind variable
641 TcCls: kind constructor or promoted type constructor
642
643 Note [HsAppsTy]
644 ~~~~~~~~~~~~~~~
645 How to parse
646
647 Foo * Int
648
649 ? Is it `(*) Foo Int` or `Foo GHC.Types.* Int`? There's no way to know until renaming.
650 So we just take type expressions like this and put each component in a list, so be
651 sorted out in the renamer. The sorting out is done by RnTypes.mkHsOpTyRn. This means
652 that the parser should never produce HsAppTy or HsOpTy.
653
654 Note [Promoted lists and tuples]
655 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
656 Notice the difference between
657 HsListTy HsExplicitListTy
658 HsTupleTy HsExplicitListTupleTy
659
660 E.g. f :: [Int] HsListTy
661
662 g3 :: T '[] All these use
663 g2 :: T '[True] HsExplicitListTy
664 g1 :: T '[True,False]
665 g1a :: T [True,False] (can omit ' where unambiguous)
666
667 kind of T :: [Bool] -> * This kind uses HsListTy!
668
669 E.g. h :: (Int,Bool) HsTupleTy; f is a pair
670 k :: S '(True,False) HsExplicitTypleTy; S is indexed by
671 a type-level pair of booleans
672 kind of S :: (Bool,Bool) -> * This kind uses HsExplicitTupleTy
673
674 Note [Distinguishing tuple kinds]
675 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
676
677 Apart from promotion, tuples can have one of three different kinds:
678
679 x :: (Int, Bool) -- Regular boxed tuples
680 f :: Int# -> (# Int#, Int# #) -- Unboxed tuples
681 g :: (Eq a, Ord a) => a -- Constraint tuples
682
683 For convenience, internally we use a single constructor for all of these,
684 namely HsTupleTy, but keep track of the tuple kind (in the first argument to
685 HsTupleTy, a HsTupleSort). We can tell if a tuple is unboxed while parsing,
686 because of the #. However, with -XConstraintKinds we can only distinguish
687 between constraint and boxed tuples during type checking, in general. Hence the
688 four constructors of HsTupleSort:
689
690 HsUnboxedTuple -> Produced by the parser
691 HsBoxedTuple -> Certainly a boxed tuple
692 HsConstraintTuple -> Certainly a constraint tuple
693 HsBoxedOrConstraintTuple -> Could be a boxed or a constraint
694 tuple. Produced by the parser only,
695 disappears after type checking
696 -}
697
698 data HsTupleSort = HsUnboxedTuple
699 | HsBoxedTuple
700 | HsConstraintTuple
701 | HsBoxedOrConstraintTuple
702 deriving Data
703
704 type LConDeclField name = Located (ConDeclField name)
705 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
706 -- in a list
707
708 -- For details on above see note [Api annotations] in ApiAnnotation
709 data ConDeclField name -- Record fields have Haddoc docs on them
710 = ConDeclField { cd_fld_names :: [LFieldOcc name],
711 -- ^ See Note [ConDeclField names]
712 cd_fld_type :: LBangType name,
713 cd_fld_doc :: Maybe LHsDocString }
714 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
715
716 -- For details on above see note [Api annotations] in ApiAnnotation
717 deriving instance (DataId name) => Data (ConDeclField name)
718
719 instance (OutputableBndrId name) => Outputable (ConDeclField name) where
720 ppr (ConDeclField fld_n fld_ty _) = ppr fld_n <+> dcolon <+> ppr fld_ty
721
722 -- HsConDetails is used for patterns/expressions *and* for data type
723 -- declarations
724 data HsConDetails arg rec
725 = PrefixCon [arg] -- C p1 p2 p3
726 | RecCon rec -- C { x = p1, y = p2 }
727 | InfixCon arg arg -- p1 `C` p2
728 deriving Data
729
730 instance (Outputable arg, Outputable rec)
731 => Outputable (HsConDetails arg rec) where
732 ppr (PrefixCon args) = text "PrefixCon" <+> ppr args
733 ppr (RecCon rec) = text "RecCon:" <+> ppr rec
734 ppr (InfixCon l r) = text "InfixCon:" <+> ppr [l, r]
735
736 -- Takes details and result type of a GADT data constructor as created by the
737 -- parser and rejigs them using information about fixities from the renamer.
738 -- See Note [Sorting out the result type] in RdrHsSyn
739 updateGadtResult
740 :: (Monad m)
741 => (SDoc -> m ())
742 -> SDoc
743 -> HsConDetails (LHsType Name) (Located [LConDeclField Name])
744 -- ^ Original details
745 -> LHsType Name -- ^ Original result type
746 -> m (HsConDetails (LHsType Name) (Located [LConDeclField Name]),
747 LHsType Name)
748 updateGadtResult failWith doc details ty
749 = do { let (arg_tys, res_ty) = splitHsFunType ty
750 badConSig = text "Malformed constructor signature"
751 ; case details of
752 InfixCon {} -> pprPanic "updateGadtResult" (ppr ty)
753
754 RecCon {} -> do { unless (null arg_tys)
755 (failWith (doc <+> badConSig))
756 ; return (details, res_ty) }
757
758 PrefixCon {} -> return (PrefixCon arg_tys, res_ty)}
759
760 {-
761 Note [ConDeclField names]
762 ~~~~~~~~~~~~~~~~~~~~~~~~~
763
764 A ConDeclField contains a list of field occurrences: these always
765 include the field label as the user wrote it. After the renamer, it
766 will additionally contain the identity of the selector function in the
767 second component.
768
769 Due to DuplicateRecordFields, the OccName of the selector function
770 may have been mangled, which is why we keep the original field label
771 separately. For example, when DuplicateRecordFields is enabled
772
773 data T = MkT { x :: Int }
774
775 gives
776
777 ConDeclField { cd_fld_names = [L _ (FieldOcc "x" $sel:x:MkT)], ... }.
778 -}
779
780 -----------------------
781 -- A valid type must have a for-all at the top of the type, or of the fn arg
782 -- types
783
784 ---------------------
785 hsWcScopedTvs :: LHsSigWcType Name -> [Name]
786 -- Get the lexically-scoped type variables of a HsSigType
787 -- - the explicitly-given forall'd type variables
788 -- - the implicitly-bound kind variables
789 -- - the named wildcars; see Note [Scoping of named wildcards]
790 -- because they scope in the same way
791 hsWcScopedTvs sig_ty
792 | HsWC { hswc_wcs = nwcs, hswc_body = sig_ty1 } <- sig_ty
793 , HsIB { hsib_vars = vars, hsib_body = sig_ty2 } <- sig_ty1
794 = case sig_ty2 of
795 L _ (HsForAllTy { hst_bndrs = tvs }) -> vars ++ nwcs ++
796 map hsLTyVarName tvs
797 -- include kind variables only if the type is headed by forall
798 -- (this is consistent with GHC 7 behaviour)
799 _ -> nwcs
800
801 hsScopedTvs :: LHsSigType Name -> [Name]
802 -- Same as hsWcScopedTvs, but for a LHsSigType
803 hsScopedTvs sig_ty
804 | HsIB { hsib_vars = vars, hsib_body = sig_ty2 } <- sig_ty
805 , L _ (HsForAllTy { hst_bndrs = tvs }) <- sig_ty2
806 = vars ++ map hsLTyVarName tvs
807 | otherwise
808 = []
809
810 {- Note [Scoping of named wildcards]
811 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
812 Consider
813 f :: _a -> _a
814 f x = let g :: _a -> _a
815 g = ...
816 in ...
817
818 Currently, for better or worse, the "_a" variables are all the same. So
819 although there is no explicit forall, the "_a" scopes over the definition.
820 I don't know if this is a good idea, but there it is.
821 -}
822
823 ---------------------
824 hsTyVarName :: HsTyVarBndr name -> name
825 hsTyVarName (UserTyVar (L _ n)) = n
826 hsTyVarName (KindedTyVar (L _ n) _) = n
827
828 hsLTyVarName :: LHsTyVarBndr name -> name
829 hsLTyVarName = hsTyVarName . unLoc
830
831 hsExplicitLTyVarNames :: LHsQTyVars name -> [name]
832 -- Explicit variables only
833 hsExplicitLTyVarNames qtvs = map hsLTyVarName (hsQTvExplicit qtvs)
834
835 hsAllLTyVarNames :: LHsQTyVars Name -> [Name]
836 -- All variables
837 hsAllLTyVarNames (HsQTvs { hsq_implicit = kvs, hsq_explicit = tvs })
838 = kvs ++ map hsLTyVarName tvs
839
840 hsLTyVarLocName :: LHsTyVarBndr name -> Located name
841 hsLTyVarLocName = fmap hsTyVarName
842
843 hsLTyVarLocNames :: LHsQTyVars name -> [Located name]
844 hsLTyVarLocNames qtvs = map hsLTyVarLocName (hsQTvExplicit qtvs)
845
846 -- | Convert a LHsTyVarBndr to an equivalent LHsType.
847 hsLTyVarBndrToType :: LHsTyVarBndr name -> LHsType name
848 hsLTyVarBndrToType = fmap cvt
849 where cvt (UserTyVar n) = HsTyVar n
850 cvt (KindedTyVar (L name_loc n) kind)
851 = HsKindSig (L name_loc (HsTyVar (L name_loc n))) kind
852
853 -- | Convert a LHsTyVarBndrs to a list of types.
854 -- Works on *type* variable only, no kind vars.
855 hsLTyVarBndrsToTypes :: LHsQTyVars name -> [LHsType name]
856 hsLTyVarBndrsToTypes (HsQTvs { hsq_explicit = tvbs }) = map hsLTyVarBndrToType tvbs
857
858 ---------------------
859 wildCardName :: HsWildCardInfo Name -> Name
860 wildCardName (AnonWildCard (L _ n)) = n
861
862 -- Two wild cards are the same when they have the same location
863 sameWildCard :: Located (HsWildCardInfo name)
864 -> Located (HsWildCardInfo name) -> Bool
865 sameWildCard (L l1 (AnonWildCard _)) (L l2 (AnonWildCard _)) = l1 == l2
866
867 ignoreParens :: LHsType name -> LHsType name
868 ignoreParens (L _ (HsParTy ty)) = ignoreParens ty
869 ignoreParens (L _ (HsAppsTy [L _ (HsAppPrefix ty)])) = ignoreParens ty
870 ignoreParens ty = ty
871
872 {-
873 ************************************************************************
874 * *
875 Building types
876 * *
877 ************************************************************************
878 -}
879
880 mkAnonWildCardTy :: HsType RdrName
881 mkAnonWildCardTy = HsWildCardTy (AnonWildCard PlaceHolder)
882
883 mkHsOpTy :: LHsType name -> Located name -> LHsType name -> HsType name
884 mkHsOpTy ty1 op ty2 = HsOpTy ty1 op ty2
885
886 mkHsAppTy :: LHsType name -> LHsType name -> LHsType name
887 mkHsAppTy t1 t2 = addCLoc t1 t2 (HsAppTy t1 t2)
888
889 mkHsAppTys :: LHsType name -> [LHsType name] -> LHsType name
890 mkHsAppTys = foldl mkHsAppTy
891
892
893 {-
894 ************************************************************************
895 * *
896 Decomposing HsTypes
897 * *
898 ************************************************************************
899 -}
900
901 ---------------------------------
902 -- splitHsFunType decomposes a type (t1 -> t2 ... -> tn)
903 -- Breaks up any parens in the result type:
904 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
905 -- Also deals with (->) t1 t2; that is why it only works on LHsType Name
906 -- (see Trac #9096)
907 splitHsFunType :: LHsType Name -> ([LHsType Name], LHsType Name)
908 splitHsFunType (L _ (HsParTy ty))
909 = splitHsFunType ty
910
911 splitHsFunType (L _ (HsFunTy x y))
912 | (args, res) <- splitHsFunType y
913 = (x:args, res)
914
915 splitHsFunType orig_ty@(L _ (HsAppTy t1 t2))
916 = go t1 [t2]
917 where -- Look for (->) t1 t2, possibly with parenthesisation
918 go (L _ (HsTyVar (L _ fn))) tys | fn == funTyConName
919 , [t1,t2] <- tys
920 , (args, res) <- splitHsFunType t2
921 = (t1:args, res)
922 go (L _ (HsAppTy t1 t2)) tys = go t1 (t2:tys)
923 go (L _ (HsParTy ty)) tys = go ty tys
924 go _ _ = ([], orig_ty) -- Failure to match
925
926 splitHsFunType other = ([], other)
927
928 --------------------------------
929 -- | Retrieves the head of an HsAppsTy, if this can be done unambiguously,
930 -- without consulting fixities.
931 getAppsTyHead_maybe :: [LHsAppType name] -> Maybe (LHsType name, [LHsType name])
932 getAppsTyHead_maybe tys = case splitHsAppsTy tys of
933 ([app1:apps], []) -> -- no symbols, some normal types
934 Just (mkHsAppTys app1 apps, [])
935 ([app1l:appsl, app1r:appsr], [L loc op]) -> -- one operator
936 Just (L loc (HsTyVar (L loc op)), [mkHsAppTys app1l appsl, mkHsAppTys app1r appsr])
937 _ -> -- can't figure it out
938 Nothing
939
940 -- | Splits a [HsAppType name] (the payload of an HsAppsTy) into regions of prefix
941 -- types (normal types) and infix operators.
942 -- If @splitHsAppsTy tys = (non_syms, syms)@, then @tys@ starts with the first
943 -- element of @non_syms@ followed by the first element of @syms@ followed by
944 -- the next element of @non_syms@, etc. It is guaranteed that the non_syms list
945 -- has one more element than the syms list.
946 splitHsAppsTy :: [LHsAppType name] -> ([[LHsType name]], [Located name])
947 splitHsAppsTy = go [] [] []
948 where
949 go acc acc_non acc_sym [] = (reverse (reverse acc : acc_non), reverse acc_sym)
950 go acc acc_non acc_sym (L _ (HsAppPrefix ty) : rest)
951 = go (ty : acc) acc_non acc_sym rest
952 go acc acc_non acc_sym (L _ (HsAppInfix op) : rest)
953 = go [] (reverse acc : acc_non) (op : acc_sym) rest
954
955 -- Retrieve the name of the "head" of a nested type application
956 -- somewhat like splitHsAppTys, but a little more thorough
957 -- used to examine the result of a GADT-like datacon, so it doesn't handle
958 -- *all* cases (like lists, tuples, (~), etc.)
959 hsTyGetAppHead_maybe :: LHsType name -> Maybe (Located name, [LHsType name])
960 hsTyGetAppHead_maybe = go []
961 where
962 go tys (L _ (HsTyVar ln)) = Just (ln, tys)
963 go tys (L _ (HsAppsTy apps))
964 | Just (head, args) <- getAppsTyHead_maybe apps
965 = go (args ++ tys) head
966 go tys (L _ (HsAppTy l r)) = go (r : tys) l
967 go tys (L _ (HsOpTy l (L loc n) r)) = Just (L loc n, l : r : tys)
968 go tys (L _ (HsParTy t)) = go tys t
969 go tys (L _ (HsKindSig t _)) = go tys t
970 go _ _ = Nothing
971
972 splitHsAppTys :: LHsType Name -> [LHsType Name] -> (LHsType Name, [LHsType Name])
973 -- no need to worry about HsAppsTy here
974 splitHsAppTys (L _ (HsAppTy f a)) as = splitHsAppTys f (a:as)
975 splitHsAppTys (L _ (HsParTy f)) as = splitHsAppTys f as
976 splitHsAppTys f as = (f,as)
977
978 --------------------------------
979 splitLHsPatSynTy :: LHsType name
980 -> ( [LHsTyVarBndr name] -- universals
981 , LHsContext name -- required constraints
982 , [LHsTyVarBndr name] -- existentials
983 , LHsContext name -- provided constraints
984 , LHsType name) -- body type
985 splitLHsPatSynTy ty = (univs, reqs, exis, provs, ty4)
986 where
987 (univs, ty1) = splitLHsForAllTy ty
988 (reqs, ty2) = splitLHsQualTy ty1
989 (exis, ty3) = splitLHsForAllTy ty2
990 (provs, ty4) = splitLHsQualTy ty3
991
992 splitLHsSigmaTy :: LHsType name -> ([LHsTyVarBndr name], LHsContext name, LHsType name)
993 splitLHsSigmaTy ty
994 | (tvs, ty1) <- splitLHsForAllTy ty
995 , (ctxt, ty2) <- splitLHsQualTy ty1
996 = (tvs, ctxt, ty2)
997
998 splitLHsForAllTy :: LHsType name -> ([LHsTyVarBndr name], LHsType name)
999 splitLHsForAllTy (L _ (HsForAllTy { hst_bndrs = tvs, hst_body = body })) = (tvs, body)
1000 splitLHsForAllTy body = ([], body)
1001
1002 splitLHsQualTy :: LHsType name -> (LHsContext name, LHsType name)
1003 splitLHsQualTy (L _ (HsQualTy { hst_ctxt = ctxt, hst_body = body })) = (ctxt, body)
1004 splitLHsQualTy body = (noLoc [], body)
1005
1006 splitLHsInstDeclTy :: LHsSigType Name
1007 -> ([Name], LHsContext Name, LHsType Name)
1008 -- Split up an instance decl type, returning the pieces
1009 splitLHsInstDeclTy (HsIB { hsib_vars = itkvs
1010 , hsib_body = inst_ty })
1011 | (tvs, cxt, body_ty) <- splitLHsSigmaTy inst_ty
1012 = (itkvs ++ map hsLTyVarName tvs, cxt, body_ty)
1013 -- Return implicitly bound type and kind vars
1014 -- For an instance decl, all of them are in scope
1015 where
1016
1017 getLHsInstDeclHead :: LHsSigType name -> LHsType name
1018 getLHsInstDeclHead inst_ty
1019 | (_tvs, _cxt, body_ty) <- splitLHsSigmaTy (hsSigType inst_ty)
1020 = body_ty
1021
1022 getLHsInstDeclClass_maybe :: LHsSigType name -> Maybe (Located name)
1023 -- Works on (HsSigType RdrName)
1024 getLHsInstDeclClass_maybe inst_ty
1025 = do { let head_ty = getLHsInstDeclHead inst_ty
1026 ; (cls, _) <- hsTyGetAppHead_maybe head_ty
1027 ; return cls }
1028
1029 {-
1030 ************************************************************************
1031 * *
1032 FieldOcc
1033 * *
1034 ************************************************************************
1035 -}
1036
1037 type LFieldOcc name = Located (FieldOcc name)
1038
1039 -- | Represents an *occurrence* of an unambiguous field. We store
1040 -- both the 'RdrName' the user originally wrote, and after the
1041 -- renamer, the selector function.
1042 data FieldOcc name = FieldOcc { rdrNameFieldOcc :: Located RdrName
1043 -- ^ See Note [Located RdrNames] in HsExpr
1044 , selectorFieldOcc :: PostRn name name
1045 }
1046 deriving instance Eq (PostRn name name) => Eq (FieldOcc name)
1047 deriving instance Ord (PostRn name name) => Ord (FieldOcc name)
1048 deriving instance (Data name, Data (PostRn name name)) => Data (FieldOcc name)
1049
1050 instance Outputable (FieldOcc name) where
1051 ppr = ppr . rdrNameFieldOcc
1052
1053 mkFieldOcc :: Located RdrName -> FieldOcc RdrName
1054 mkFieldOcc rdr = FieldOcc rdr PlaceHolder
1055
1056
1057 -- | Represents an *occurrence* of a field that is potentially
1058 -- ambiguous after the renamer, with the ambiguity resolved by the
1059 -- typechecker. We always store the 'RdrName' that the user
1060 -- originally wrote, and store the selector function after the renamer
1061 -- (for unambiguous occurrences) or the typechecker (for ambiguous
1062 -- occurrences).
1063 --
1064 -- See Note [HsRecField and HsRecUpdField] in HsPat and
1065 -- Note [Disambiguating record fields] in TcExpr.
1066 -- See Note [Located RdrNames] in HsExpr
1067 data AmbiguousFieldOcc name
1068 = Unambiguous (Located RdrName) (PostRn name name)
1069 | Ambiguous (Located RdrName) (PostTc name name)
1070 deriving instance ( Data name
1071 , Data (PostRn name name)
1072 , Data (PostTc name name))
1073 => Data (AmbiguousFieldOcc name)
1074
1075 instance Outputable (AmbiguousFieldOcc name) where
1076 ppr = ppr . rdrNameAmbiguousFieldOcc
1077
1078 instance OutputableBndr (AmbiguousFieldOcc name) where
1079 pprInfixOcc = pprInfixOcc . rdrNameAmbiguousFieldOcc
1080 pprPrefixOcc = pprPrefixOcc . rdrNameAmbiguousFieldOcc
1081
1082 mkAmbiguousFieldOcc :: Located RdrName -> AmbiguousFieldOcc RdrName
1083 mkAmbiguousFieldOcc rdr = Unambiguous rdr PlaceHolder
1084
1085 rdrNameAmbiguousFieldOcc :: AmbiguousFieldOcc name -> RdrName
1086 rdrNameAmbiguousFieldOcc (Unambiguous (L _ rdr) _) = rdr
1087 rdrNameAmbiguousFieldOcc (Ambiguous (L _ rdr) _) = rdr
1088
1089 selectorAmbiguousFieldOcc :: AmbiguousFieldOcc Id -> Id
1090 selectorAmbiguousFieldOcc (Unambiguous _ sel) = sel
1091 selectorAmbiguousFieldOcc (Ambiguous _ sel) = sel
1092
1093 unambiguousFieldOcc :: AmbiguousFieldOcc Id -> FieldOcc Id
1094 unambiguousFieldOcc (Unambiguous rdr sel) = FieldOcc rdr sel
1095 unambiguousFieldOcc (Ambiguous rdr sel) = FieldOcc rdr sel
1096
1097 ambiguousFieldOcc :: FieldOcc name -> AmbiguousFieldOcc name
1098 ambiguousFieldOcc (FieldOcc rdr sel) = Unambiguous rdr sel
1099
1100 {-
1101 ************************************************************************
1102 * *
1103 \subsection{Pretty printing}
1104 * *
1105 ************************************************************************
1106 -}
1107
1108 instance (OutputableBndrId name) => Outputable (HsType name) where
1109 ppr ty = pprHsType ty
1110
1111 instance Outputable HsTyLit where
1112 ppr = ppr_tylit
1113
1114 instance (OutputableBndrId name) => Outputable (LHsQTyVars name) where
1115 ppr (HsQTvs { hsq_explicit = tvs }) = interppSP tvs
1116
1117 instance (OutputableBndrId name) => Outputable (HsTyVarBndr name) where
1118 ppr (UserTyVar n) = ppr n
1119 ppr (KindedTyVar n k) = parens $ hsep [ppr n, dcolon, ppr k]
1120
1121 instance (Outputable thing) => Outputable (HsImplicitBndrs name thing) where
1122 ppr (HsIB { hsib_body = ty }) = ppr ty
1123
1124 instance (Outputable thing) => Outputable (HsWildCardBndrs name thing) where
1125 ppr (HsWC { hswc_body = ty }) = ppr ty
1126
1127 instance Outputable (HsWildCardInfo name) where
1128 ppr (AnonWildCard _) = char '_'
1129
1130 pprHsForAll :: (OutputableBndrId name)
1131 => [LHsTyVarBndr name] -> LHsContext name -> SDoc
1132 pprHsForAll = pprHsForAllExtra Nothing
1133
1134 -- | Version of 'pprHsForAll' that can also print an extra-constraints
1135 -- wildcard, e.g. @_ => a -> Bool@ or @(Show a, _) => a -> String@. This
1136 -- underscore will be printed when the 'Maybe SrcSpan' argument is a 'Just'
1137 -- containing the location of the extra-constraints wildcard. A special
1138 -- function for this is needed, as the extra-constraints wildcard is removed
1139 -- from the actual context and type, and stored in a separate field, thus just
1140 -- printing the type will not print the extra-constraints wildcard.
1141 pprHsForAllExtra :: (OutputableBndrId name)
1142 => Maybe SrcSpan -> [LHsTyVarBndr name] -> LHsContext name
1143 -> SDoc
1144 pprHsForAllExtra extra qtvs cxt
1145 = pprHsForAllTvs qtvs <+> pprHsContextExtra show_extra (unLoc cxt)
1146 where
1147 show_extra = isJust extra
1148
1149 pprHsForAllTvs :: (OutputableBndrId name) => [LHsTyVarBndr name] -> SDoc
1150 pprHsForAllTvs qtvs
1151 | show_forall = forAllLit <+> interppSP qtvs <> dot
1152 | otherwise = empty
1153 where
1154 show_forall = opt_PprStyle_Debug || not (null qtvs)
1155
1156 pprHsContext :: (OutputableBndrId name) => HsContext name -> SDoc
1157 pprHsContext = maybe empty (<+> darrow) . pprHsContextMaybe
1158
1159 pprHsContextNoArrow :: (OutputableBndrId name) => HsContext name -> SDoc
1160 pprHsContextNoArrow = fromMaybe empty . pprHsContextMaybe
1161
1162 pprHsContextMaybe :: (OutputableBndrId name) => HsContext name -> Maybe SDoc
1163 pprHsContextMaybe [] = Nothing
1164 pprHsContextMaybe [L _ pred] = Just $ ppr_mono_ty FunPrec pred
1165 pprHsContextMaybe cxt = Just $ parens (interpp'SP cxt)
1166
1167 -- True <=> print an extra-constraints wildcard, e.g. @(Show a, _) =>@
1168 pprHsContextExtra :: (OutputableBndrId name) => Bool -> HsContext name -> SDoc
1169 pprHsContextExtra show_extra ctxt
1170 | not show_extra
1171 = pprHsContext ctxt
1172 | null ctxt
1173 = char '_' <+> darrow
1174 | otherwise
1175 = parens (sep (punctuate comma ctxt')) <+> darrow
1176 where
1177 ctxt' = map ppr ctxt ++ [char '_']
1178
1179 pprConDeclFields :: (OutputableBndrId name) => [LConDeclField name] -> SDoc
1180 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
1181 where
1182 ppr_fld (L _ (ConDeclField { cd_fld_names = ns, cd_fld_type = ty,
1183 cd_fld_doc = doc }))
1184 = ppr_names ns <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
1185 ppr_names [n] = ppr n
1186 ppr_names ns = sep (punctuate comma (map ppr ns))
1187
1188 {-
1189 Note [Printing KindedTyVars]
1190 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1191 Trac #3830 reminded me that we should really only print the kind
1192 signature on a KindedTyVar if the kind signature was put there by the
1193 programmer. During kind inference GHC now adds a PostTcKind to UserTyVars,
1194 rather than converting to KindedTyVars as before.
1195
1196 (As it happens, the message in #3830 comes out a different way now,
1197 and the problem doesn't show up; but having the flag on a KindedTyVar
1198 seems like the Right Thing anyway.)
1199 -}
1200
1201 -- Printing works more-or-less as for Types
1202
1203 pprHsType, pprParendHsType :: (OutputableBndrId name) => HsType name -> SDoc
1204
1205 pprHsType ty = ppr_mono_ty TopPrec (prepare ty)
1206 pprParendHsType ty = ppr_mono_ty TyConPrec ty
1207
1208 -- Before printing a type, remove outermost HsParTy parens
1209 prepare :: HsType name -> HsType name
1210 prepare (HsParTy ty) = prepare (unLoc ty)
1211 prepare (HsAppsTy [L _ (HsAppPrefix (L _ ty))]) = prepare ty
1212 prepare ty = ty
1213
1214 ppr_mono_lty :: (OutputableBndrId name) => TyPrec -> LHsType name -> SDoc
1215 ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty)
1216
1217 ppr_mono_ty :: (OutputableBndrId name) => TyPrec -> HsType name -> SDoc
1218 ppr_mono_ty ctxt_prec (HsForAllTy { hst_bndrs = tvs, hst_body = ty })
1219 = maybeParen ctxt_prec FunPrec $
1220 sep [pprHsForAllTvs tvs, ppr_mono_lty TopPrec ty]
1221
1222 ppr_mono_ty ctxt_prec (HsQualTy { hst_ctxt = L _ ctxt, hst_body = ty })
1223 = maybeParen ctxt_prec FunPrec $
1224 sep [pprHsContext ctxt, ppr_mono_lty TopPrec ty]
1225
1226 ppr_mono_ty _ (HsBangTy b ty) = ppr b <> ppr_mono_lty TyConPrec ty
1227 ppr_mono_ty _ (HsRecTy flds) = pprConDeclFields flds
1228 ppr_mono_ty _ (HsTyVar (L _ name))= pprPrefixOcc name
1229 ppr_mono_ty prec (HsFunTy ty1 ty2) = ppr_fun_ty prec ty1 ty2
1230 ppr_mono_ty _ (HsTupleTy con tys) = tupleParens std_con (pprWithCommas ppr tys)
1231 where std_con = case con of
1232 HsUnboxedTuple -> UnboxedTuple
1233 _ -> BoxedTuple
1234 ppr_mono_ty _ (HsSumTy tys) = tupleParens UnboxedTuple (pprWithBars ppr tys)
1235 ppr_mono_ty _ (HsKindSig ty kind) = parens (ppr_mono_lty TopPrec ty <+> dcolon <+> ppr kind)
1236 ppr_mono_ty _ (HsListTy ty) = brackets (ppr_mono_lty TopPrec ty)
1237 ppr_mono_ty _ (HsPArrTy ty) = paBrackets (ppr_mono_lty TopPrec ty)
1238 ppr_mono_ty prec (HsIParamTy n ty) = maybeParen prec FunPrec (ppr n <+> dcolon <+> ppr_mono_lty TopPrec ty)
1239 ppr_mono_ty _ (HsSpliceTy s _) = pprSplice s
1240 ppr_mono_ty _ (HsCoreTy ty) = ppr ty
1241 ppr_mono_ty _ (HsExplicitListTy _ tys) = quote $ brackets (interpp'SP tys)
1242 ppr_mono_ty _ (HsExplicitTupleTy _ tys) = quote $ parens (interpp'SP tys)
1243 ppr_mono_ty _ (HsTyLit t) = ppr_tylit t
1244 ppr_mono_ty _ (HsWildCardTy {}) = char '_'
1245
1246 ppr_mono_ty ctxt_prec (HsEqTy ty1 ty2)
1247 = maybeParen ctxt_prec TyOpPrec $
1248 ppr_mono_lty TyOpPrec ty1 <+> char '~' <+> ppr_mono_lty TyOpPrec ty2
1249
1250 ppr_mono_ty ctxt_prec (HsAppsTy tys)
1251 = maybeParen ctxt_prec TyConPrec $
1252 hsep (map (ppr_app_ty TopPrec . unLoc) tys)
1253
1254 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
1255 = maybeParen ctxt_prec TyConPrec $
1256 hsep [ppr_mono_lty FunPrec fun_ty, ppr_mono_lty TyConPrec arg_ty]
1257
1258 ppr_mono_ty ctxt_prec (HsOpTy ty1 (L _ op) ty2)
1259 = maybeParen ctxt_prec TyOpPrec $
1260 sep [ ppr_mono_lty TyOpPrec ty1
1261 , sep [pprInfixOcc op, ppr_mono_lty TyOpPrec ty2 ] ]
1262
1263 ppr_mono_ty _ (HsParTy ty)
1264 = parens (ppr_mono_lty TopPrec ty)
1265 -- Put the parens in where the user did
1266 -- But we still use the precedence stuff to add parens because
1267 -- toHsType doesn't put in any HsParTys, so we may still need them
1268
1269 ppr_mono_ty ctxt_prec (HsDocTy ty doc)
1270 = maybeParen ctxt_prec TyOpPrec $
1271 ppr_mono_lty TyOpPrec ty <+> ppr (unLoc doc)
1272 -- we pretty print Haddock comments on types as if they were
1273 -- postfix operators
1274
1275 --------------------------
1276 ppr_fun_ty :: (OutputableBndrId name)
1277 => TyPrec -> LHsType name -> LHsType name -> SDoc
1278 ppr_fun_ty ctxt_prec ty1 ty2
1279 = let p1 = ppr_mono_lty FunPrec ty1
1280 p2 = ppr_mono_lty TopPrec ty2
1281 in
1282 maybeParen ctxt_prec FunPrec $
1283 sep [p1, text "->" <+> p2]
1284
1285 --------------------------
1286 ppr_app_ty :: (OutputableBndrId name) => TyPrec -> HsAppType name -> SDoc
1287 ppr_app_ty _ (HsAppInfix (L _ n)) = pprInfixOcc n
1288 ppr_app_ty _ (HsAppPrefix (L _ (HsTyVar (L _ n)))) = pprPrefixOcc n
1289 ppr_app_ty ctxt (HsAppPrefix ty) = ppr_mono_lty ctxt ty
1290
1291 --------------------------
1292 ppr_tylit :: HsTyLit -> SDoc
1293 ppr_tylit (HsNumTy _ i) = integer i
1294 ppr_tylit (HsStrTy _ s) = text (show s)