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