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