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