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