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