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