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