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