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