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