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