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