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