ApiAnnotations: Make all RdrName occurences 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 HsTyOp,LHsTyOp,
22 HsTyVarBndr(..), LHsTyVarBndr,
23 LHsTyVarBndrs(..),
24 HsWithBndrs(..),
25 HsTupleSort(..), HsExplicitFlag(..),
26 HsContext, LHsContext,
27 HsTyWrapper(..),
28 HsTyLit(..),
29 HsIPName(..), hsIPNameFS,
30
31 LBangType, BangType,
32 HsSrcBang(..), HsImplBang(..),
33 SrcStrictness(..), SrcUnpackedness(..),
34 getBangType, getBangStrictness,
35
36 ConDeclField(..), LConDeclField, pprConDeclFields,
37
38 FieldOcc(..), LFieldOcc, mkFieldOcc,
39 AmbiguousFieldOcc(..), mkAmbiguousFieldOcc,
40 rdrNameAmbiguousFieldOcc, selectorAmbiguousFieldOcc,
41 unambiguousFieldOcc, ambiguousFieldOcc,
42
43 HsWildCardInfo(..), mkAnonWildCardTy, mkNamedWildCardTy,
44 wildCardName, sameWildCard, sameNamedWildCard,
45 isAnonWildCard, isNamedWildCard,
46
47 mkHsQTvs, hsQTvBndrs, isHsKindedTyVar, hsTvbAllKinded,
48 mkExplicitHsForAllTy, mkImplicitHsForAllTy, mkQualifiedHsForAllTy,
49 mkHsForAllTy,
50 flattenTopLevelLHsForAllTy,flattenTopLevelHsForAllTy,
51 flattenHsForAllTyKeepAnns,
52 hsExplicitTvs,
53 hsTyVarName, mkHsWithBndrs, hsLKiTyVarNames,
54 hsLTyVarName, hsLTyVarNames, hsLTyVarLocName, hsLTyVarLocNames,
55 hsLTyVarBndrsToTypes,
56 splitLHsInstDeclTy_maybe,
57 splitHsClassTy_maybe, splitLHsClassTy_maybe,
58 splitHsFunType,
59 splitHsAppTys, hsTyGetAppHead_maybe, mkHsAppTys, mkHsOpTy,
60 ignoreParens,
61
62 -- Printing
63 pprParendHsType, pprHsForAll, pprHsForAllExtra,
64 pprHsContext, pprHsContextNoArrow, pprHsContextMaybe
65 ) where
66
67 import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
68
69 import PlaceHolder ( PostTc,PostRn,DataId,PlaceHolder(..) )
70
71 import Id( Id )
72 import Name( Name )
73 import RdrName( RdrName )
74 import DataCon( HsSrcBang(..), HsImplBang(..),
75 SrcStrictness(..), SrcUnpackedness(..) )
76 import TysPrim( funTyConName )
77 import Type
78 import HsDoc
79 import BasicTypes
80 import SrcLoc
81 import StaticFlags
82 import Outputable
83 import FastString
84 import Lexer ( AddAnn, mkParensApiAnn )
85 import Maybes( isJust )
86
87 import Data.Data hiding ( Fixity )
88 import Data.Maybe ( fromMaybe )
89 #if __GLASGOW_HASKELL__ < 709
90 import Data.Monoid hiding ((<>))
91 #endif
92
93 {-
94 ************************************************************************
95 * *
96 \subsection{Bang annotations}
97 * *
98 ************************************************************************
99 -}
100
101 type LBangType name = Located (BangType name)
102 type BangType name = HsType name -- Bangs are in the HsType data type
103
104 getBangType :: LHsType a -> LHsType a
105 getBangType (L _ (HsBangTy _ ty)) = ty
106 getBangType ty = ty
107
108 getBangStrictness :: LHsType a -> HsSrcBang
109 getBangStrictness (L _ (HsBangTy s _)) = s
110 getBangStrictness _ = (HsSrcBang Nothing NoSrcUnpack NoSrcStrict)
111
112 {-
113 ************************************************************************
114 * *
115 \subsection{Data types}
116 * *
117 ************************************************************************
118
119 This is the syntax for types as seen in type signatures.
120
121 Note [HsBSig binder lists]
122 ~~~~~~~~~~~~~~~~~~~~~~~~~~
123 Consider a binder (or pattern) decoarated with a type or kind,
124 \ (x :: a -> a). blah
125 forall (a :: k -> *) (b :: k). blah
126 Then we use a LHsBndrSig on the binder, so that the
127 renamer can decorate it with the variables bound
128 by the pattern ('a' in the first example, 'k' in the second),
129 assuming that neither of them is in scope already
130 See also Note [Kind and type-variable binders] in RnTypes
131 -}
132
133 type LHsContext name = Located (HsContext name)
134 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnUnit'
135
136 -- For details on above see note [Api annotations] in ApiAnnotation
137
138 type HsContext name = [LHsType name]
139
140 type LHsType name = Located (HsType name)
141 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
142 -- in a list
143
144 -- For details on above see note [Api annotations] in ApiAnnotation
145 type HsKind name = HsType name
146 type LHsKind name = Located (HsKind name)
147 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
148
149 -- For details on above see note [Api annotations] in ApiAnnotation
150
151 --------------------------------------------------
152 -- LHsTyVarBndrs
153 -- The quantified binders in a HsForallTy
154
155 type LHsTyVarBndr name = Located (HsTyVarBndr name)
156
157 data LHsTyVarBndrs name
158 = HsQTvs { hsq_kvs :: [Name] -- Kind variables
159 , hsq_tvs :: [LHsTyVarBndr name] -- Type variables
160 -- See Note [HsForAllTy tyvar binders]
161 }
162 deriving( Typeable )
163 deriving instance (DataId name) => Data (LHsTyVarBndrs name)
164
165 mkHsQTvs :: [LHsTyVarBndr RdrName] -> LHsTyVarBndrs RdrName
166 -- Just at RdrName because in the Name variant we should know just
167 -- what the kind-variable binders are; and we don't
168 -- We put an empty list (rather than a panic) for the kind vars so
169 -- that the pretty printer works ok on them.
170 mkHsQTvs tvs = HsQTvs { hsq_kvs = [], hsq_tvs = tvs }
171
172 emptyHsQTvs :: LHsTyVarBndrs name -- Use only when you know there are no kind binders
173 emptyHsQTvs = HsQTvs { hsq_kvs = [], hsq_tvs = [] }
174
175 hsQTvBndrs :: LHsTyVarBndrs name -> [LHsTyVarBndr name]
176 hsQTvBndrs = hsq_tvs
177
178 instance Monoid (LHsTyVarBndrs name) where
179 mempty = emptyHsQTvs
180 mappend (HsQTvs kvs1 tvs1) (HsQTvs kvs2 tvs2)
181 = HsQTvs (kvs1 ++ kvs2) (tvs1 ++ tvs2)
182
183 ------------------------------------------------
184 -- HsWithBndrs
185 -- Used to quantify the binders of a type in cases
186 -- when a HsForAll isn't appropriate:
187 -- * Patterns in a type/data family instance (HsTyPats)
188 -- * Type of a rule binder (RuleBndr)
189 -- * Pattern type signatures (SigPatIn)
190 -- In the last of these, wildcards can happen, so we must accommodate them
191
192 data HsWithBndrs name thing
193 = HsWB { hswb_cts :: thing -- Main payload (type or list of types)
194 , hswb_kvs :: PostRn name [Name] -- Kind vars
195 , hswb_tvs :: PostRn name [Name] -- Type vars
196 , hswb_wcs :: PostRn name [Name] -- Wild cards
197 }
198 deriving (Typeable)
199 deriving instance (Data name, Data thing, Data (PostRn name [Name]))
200 => Data (HsWithBndrs name thing)
201
202 mkHsWithBndrs :: thing -> HsWithBndrs RdrName thing
203 mkHsWithBndrs x = HsWB { hswb_cts = x, hswb_kvs = PlaceHolder
204 , hswb_tvs = PlaceHolder
205 , hswb_wcs = PlaceHolder }
206
207
208 --------------------------------------------------
209 -- | These names are used early on to store the names of implicit
210 -- parameters. They completely disappear after type-checking.
211 newtype HsIPName = HsIPName FastString
212 deriving( Eq, Data, Typeable )
213
214 hsIPNameFS :: HsIPName -> FastString
215 hsIPNameFS (HsIPName n) = n
216
217 instance Outputable HsIPName where
218 ppr (HsIPName n) = char '?' <> ftext n -- Ordinary implicit parameters
219
220 instance OutputableBndr HsIPName where
221 pprBndr _ n = ppr n -- Simple for now
222 pprInfixOcc n = ppr n
223 pprPrefixOcc n = ppr n
224
225 --------------------------------------------------
226 data HsTyVarBndr name
227 = UserTyVar -- no explicit kinding
228 (Located name)
229
230 | KindedTyVar
231 (Located name)
232 (LHsKind name) -- The user-supplied kind signature
233 -- ^
234 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
235 -- 'ApiAnnotation.AnnDcolon', 'ApiAnnotation.AnnClose'
236
237 -- For details on above see note [Api annotations] in ApiAnnotation
238 deriving (Typeable)
239 deriving instance (DataId name) => Data (HsTyVarBndr name)
240
241 -- | Does this 'HsTyVarBndr' come with an explicit kind annotation?
242 isHsKindedTyVar :: HsTyVarBndr name -> Bool
243 isHsKindedTyVar (UserTyVar {}) = False
244 isHsKindedTyVar (KindedTyVar {}) = True
245
246 -- | Do all type variables in this 'LHsTyVarBndr' come with kind annotations?
247 hsTvbAllKinded :: LHsTyVarBndrs name -> Bool
248 hsTvbAllKinded = all (isHsKindedTyVar . unLoc) . hsQTvBndrs
249
250 data HsType name
251 = HsForAllTy HsExplicitFlag -- Renamer leaves this flag unchanged, to record the way
252 -- the user wrote it originally, so that the printer can
253 -- print it as the user wrote it
254 (Maybe SrcSpan) -- Indicates whether extra constraints may be inferred.
255 -- When Nothing, no, otherwise the location of the extra-
256 -- constraints wildcard is stored. For instance, for the
257 -- signature (Eq a, _) => a -> a -> Bool, this field would
258 -- be something like (Just 1:8), with 1:8 being line 1,
259 -- column 8.
260 (LHsTyVarBndrs name)
261 (LHsContext name)
262 (LHsType name)
263 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnForall',
264 -- 'ApiAnnotation.AnnDot','ApiAnnotation.AnnDarrow'
265
266 -- For details on above see note [Api annotations] in ApiAnnotation
267
268 | HsTyVar (Located name)
269 -- Type variable, type constructor, or data constructor
270 -- see Note [Promotions (HsTyVar)]
271 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
272
273 -- For details on above see note [Api annotations] in ApiAnnotation
274
275 | HsAppTy (LHsType name)
276 (LHsType name)
277 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
278
279 -- For details on above see note [Api annotations] in ApiAnnotation
280
281 | HsFunTy (LHsType name) -- function type
282 (LHsType name)
283 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow',
284
285 -- For details on above see note [Api annotations] in ApiAnnotation
286
287 | HsListTy (LHsType name) -- Element type
288 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
289 -- 'ApiAnnotation.AnnClose' @']'@
290
291 -- For details on above see note [Api annotations] in ApiAnnotation
292
293 | HsPArrTy (LHsType name) -- Elem. type of parallel array: [:t:]
294 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
295 -- 'ApiAnnotation.AnnClose' @':]'@
296
297 -- For details on above see note [Api annotations] in ApiAnnotation
298
299 | HsTupleTy HsTupleSort
300 [LHsType name] -- Element types (length gives arity)
301 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(' or '(#'@,
302 -- 'ApiAnnotation.AnnClose' @')' or '#)'@
303
304 -- For details on above see note [Api annotations] in ApiAnnotation
305
306 | HsOpTy (LHsType name) (LHsTyOp name) (LHsType name)
307 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
308
309 -- For details on above see note [Api annotations] in ApiAnnotation
310
311 | HsParTy (LHsType name) -- See Note [Parens in HsSyn] in HsExpr
312 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
313 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
314 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
315 -- 'ApiAnnotation.AnnClose' @')'@
316
317 -- For details on above see note [Api annotations] in ApiAnnotation
318
319 | HsIParamTy HsIPName -- (?x :: ty)
320 (LHsType name) -- Implicit parameters as they occur in contexts
321 -- ^
322 -- > (?x :: ty)
323 --
324 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
325
326 -- For details on above see note [Api annotations] in ApiAnnotation
327
328 | HsEqTy (LHsType name) -- ty1 ~ ty2
329 (LHsType name) -- Always allowed even without TypeOperators, and has special kinding rule
330 -- ^
331 -- > ty1 ~ ty2
332 --
333 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'
334
335 -- For details on above see note [Api annotations] in ApiAnnotation
336
337 | HsKindSig (LHsType name) -- (ty :: kind)
338 (LHsKind name) -- A type with a kind signature
339 -- ^
340 -- > (ty :: kind)
341 --
342 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
343 -- 'ApiAnnotation.AnnDcolon','ApiAnnotation.AnnClose' @')'@
344
345 -- For details on above see note [Api annotations] in ApiAnnotation
346
347 | HsSpliceTy (HsSplice name) -- Includes quasi-quotes
348 (PostTc name Kind)
349 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'$('@,
350 -- 'ApiAnnotation.AnnClose' @')'@
351
352 -- For details on above see note [Api annotations] in ApiAnnotation
353
354 | HsDocTy (LHsType name) LHsDocString -- A documented type
355 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
356
357 -- For details on above see note [Api annotations] in ApiAnnotation
358
359 | HsBangTy HsSrcBang (LHsType name) -- Bang-style type annotations
360 -- ^ - 'ApiAnnotation.AnnKeywordId' :
361 -- 'ApiAnnotation.AnnOpen' @'{-\# UNPACK' or '{-\# NOUNPACK'@,
362 -- 'ApiAnnotation.AnnClose' @'#-}'@
363 -- 'ApiAnnotation.AnnBang' @\'!\'@
364
365 -- For details on above see note [Api annotations] in ApiAnnotation
366
367 | HsRecTy [LConDeclField name] -- Only in data type declarations
368 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
369 -- 'ApiAnnotation.AnnClose' @'}'@
370
371 -- For details on above see note [Api annotations] in ApiAnnotation
372
373 | HsCoreTy Type -- An escape hatch for tunnelling a *closed*
374 -- Core Type through HsSyn.
375 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
376
377 -- For details on above see note [Api annotations] in ApiAnnotation
378
379 | HsExplicitListTy -- A promoted explicit list
380 (PostTc name Kind) -- See Note [Promoted lists and tuples]
381 [LHsType name]
382 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'["@,
383 -- 'ApiAnnotation.AnnClose' @']'@
384
385 -- For details on above see note [Api annotations] in ApiAnnotation
386
387 | HsExplicitTupleTy -- A promoted explicit tuple
388 [PostTc name Kind] -- See Note [Promoted lists and tuples]
389 [LHsType name]
390 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'("@,
391 -- 'ApiAnnotation.AnnClose' @')'@
392
393 -- For details on above see note [Api annotations] in ApiAnnotation
394
395 | HsTyLit HsTyLit -- A promoted numeric literal.
396 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
397
398 -- For details on above see note [Api annotations] in ApiAnnotation
399
400 | HsWrapTy HsTyWrapper (HsType name) -- only in typechecker output
401 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
402
403 -- For details on above see note [Api annotations] in ApiAnnotation
404
405 | HsWildCardTy (HsWildCardInfo name) -- A type wildcard
406 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
407
408 -- For details on above see note [Api annotations] in ApiAnnotation
409 deriving (Typeable)
410 deriving instance (DataId name) => Data (HsType name)
411
412 -- Note [Literal source text] in BasicTypes for SourceText fields in
413 -- the following
414 data HsTyLit
415 = HsNumTy SourceText Integer
416 | HsStrTy SourceText FastString
417 deriving (Data, Typeable)
418
419 data HsTyWrapper
420 = WpKiApps [Kind] -- kind instantiation: [] k1 k2 .. kn
421 deriving (Data, Typeable)
422
423 type LHsTyOp name = HsTyOp (Located name)
424 type HsTyOp name = (HsTyWrapper, name)
425
426 mkHsOpTy :: LHsType name -> Located name -> LHsType name -> HsType name
427 mkHsOpTy ty1 op ty2 = HsOpTy ty1 (WpKiApps [], op) ty2
428
429 data HsWildCardInfo name
430 = AnonWildCard (PostRn name (Located Name))
431 -- A anonymous wild card ('_'). A name is generated during renaming.
432 | NamedWildCard (Located name)
433 -- A named wild card ('_a').
434 deriving (Typeable)
435 deriving instance (DataId name) => Data (HsWildCardInfo name)
436
437 {-
438 Note [HsForAllTy tyvar binders]
439 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
440 After parsing:
441 * Implicit => empty
442 Explicit => the variables the user wrote
443
444 After renaming
445 * Implicit => the *type* variables free in the type
446 Explicit => the variables the user wrote (renamed)
447
448 Qualified currently behaves exactly as Implicit,
449 but it is deprecated to use it for implicit quantification.
450 In this case, GHC 7.10 gives a warning; see
451 Note [Context quantification] in RnTypes, and Trac #4426.
452 In GHC 7.12, Qualified will no longer bind variables
453 and this will become an error.
454
455 The kind variables bound in the hsq_kvs field come both
456 a) from the kind signatures on the kind vars (eg k1)
457 b) from the scope of the forall (eg k2)
458 Example: f :: forall (a::k1) b. T a (b::k2)
459
460
461 Note [Unit tuples]
462 ~~~~~~~~~~~~~~~~~~
463 Consider the type
464 type instance F Int = ()
465 We want to parse that "()"
466 as HsTupleTy HsBoxedOrConstraintTuple [],
467 NOT as HsTyVar unitTyCon
468
469 Why? Because F might have kind (* -> Constraint), so we when parsing we
470 don't know if that tuple is going to be a constraint tuple or an ordinary
471 unit tuple. The HsTupleSort flag is specifically designed to deal with
472 that, but it has to work for unit tuples too.
473
474 Note [Promotions (HsTyVar)]
475 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
476 HsTyVar: A name in a type or kind.
477 Here are the allowed namespaces for the name.
478 In a type:
479 Var: not allowed
480 Data: promoted data constructor
481 Tv: type variable
482 TcCls before renamer: type constructor, class constructor, or promoted data constructor
483 TcCls after renamer: type constructor or class constructor
484 In a kind:
485 Var, Data: not allowed
486 Tv: kind variable
487 TcCls: kind constructor or promoted type constructor
488
489
490 Note [Promoted lists and tuples]
491 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
492 Notice the difference between
493 HsListTy HsExplicitListTy
494 HsTupleTy HsExplicitListTupleTy
495
496 E.g. f :: [Int] HsListTy
497
498 g3 :: T '[] All these use
499 g2 :: T '[True] HsExplicitListTy
500 g1 :: T '[True,False]
501 g1a :: T [True,False] (can omit ' where unambiguous)
502
503 kind of T :: [Bool] -> * This kind uses HsListTy!
504
505 E.g. h :: (Int,Bool) HsTupleTy; f is a pair
506 k :: S '(True,False) HsExplicitTypleTy; S is indexed by
507 a type-level pair of booleans
508 kind of S :: (Bool,Bool) -> * This kind uses HsExplicitTupleTy
509
510 Note [Distinguishing tuple kinds]
511 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
512
513 Apart from promotion, tuples can have one of three different kinds:
514
515 x :: (Int, Bool) -- Regular boxed tuples
516 f :: Int# -> (# Int#, Int# #) -- Unboxed tuples
517 g :: (Eq a, Ord a) => a -- Constraint tuples
518
519 For convenience, internally we use a single constructor for all of these,
520 namely HsTupleTy, but keep track of the tuple kind (in the first argument to
521 HsTupleTy, a HsTupleSort). We can tell if a tuple is unboxed while parsing,
522 because of the #. However, with -XConstraintKinds we can only distinguish
523 between constraint and boxed tuples during type checking, in general. Hence the
524 four constructors of HsTupleSort:
525
526 HsUnboxedTuple -> Produced by the parser
527 HsBoxedTuple -> Certainly a boxed tuple
528 HsConstraintTuple -> Certainly a constraint tuple
529 HsBoxedOrConstraintTuple -> Could be a boxed or a constraint
530 tuple. Produced by the parser only,
531 disappears after type checking
532 -}
533
534 data HsTupleSort = HsUnboxedTuple
535 | HsBoxedTuple
536 | HsConstraintTuple
537 | HsBoxedOrConstraintTuple
538 deriving (Data, Typeable)
539
540 data HsExplicitFlag
541 = Explicit -- An explicit forall, eg f :: forall a. a-> a
542 | Implicit -- No explicit forall, eg f :: a -> a, or f :: Eq a => a -> a
543 | Qualified -- A *nested* occurrences of (ctxt => ty), with no explicit forall
544 -- e.g. f :: (Eq a => a -> a) -> Int
545 deriving (Data, Typeable)
546
547 type LConDeclField name = Located (ConDeclField name)
548 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
549 -- in a list
550
551 -- For details on above see note [Api annotations] in ApiAnnotation
552 data ConDeclField name -- Record fields have Haddoc docs on them
553 = ConDeclField { cd_fld_names :: [LFieldOcc name],
554 -- ^ See Note [ConDeclField names]
555 cd_fld_type :: LBangType name,
556 cd_fld_doc :: Maybe LHsDocString }
557 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
558
559 -- For details on above see note [Api annotations] in ApiAnnotation
560 deriving (Typeable)
561 deriving instance (DataId name) => Data (ConDeclField name)
562
563
564 type LFieldOcc name = Located (FieldOcc name)
565
566 -- | Represents an *occurrence* of an unambiguous field. We store
567 -- both the 'RdrName' the user originally wrote, and after the
568 -- renamer, the selector function.
569 data FieldOcc name = FieldOcc { rdrNameFieldOcc :: RdrName
570 , selectorFieldOcc :: PostRn name name
571 }
572 deriving Typeable
573 deriving instance Eq (PostRn name name) => Eq (FieldOcc name)
574 deriving instance Ord (PostRn name name) => Ord (FieldOcc name)
575 deriving instance (Data name, Data (PostRn name name)) => Data (FieldOcc name)
576
577 instance Outputable (FieldOcc name) where
578 ppr = ppr . rdrNameFieldOcc
579
580 mkFieldOcc :: RdrName -> FieldOcc RdrName
581 mkFieldOcc rdr = FieldOcc rdr PlaceHolder
582
583
584 -- | Represents an *occurrence* of a field that is potentially
585 -- ambiguous after the renamer, with the ambiguity resolved by the
586 -- typechecker. We always store the 'RdrName' that the user
587 -- originally wrote, and store the selector function after the renamer
588 -- (for unambiguous occurrences) or the typechecker (for ambiguous
589 -- occurrences).
590 --
591 -- See Note [HsRecField and HsRecUpdField] in HsPat and
592 -- Note [Disambiguating record fields] in TcExpr.
593 data AmbiguousFieldOcc name
594 = Unambiguous RdrName (PostRn name name)
595 | Ambiguous RdrName (PostTc name name)
596 deriving (Typeable)
597 deriving instance ( Data name
598 , Data (PostRn name name)
599 , Data (PostTc name name))
600 => Data (AmbiguousFieldOcc name)
601
602 instance Outputable (AmbiguousFieldOcc name) where
603 ppr = ppr . rdrNameAmbiguousFieldOcc
604
605 mkAmbiguousFieldOcc :: RdrName -> AmbiguousFieldOcc RdrName
606 mkAmbiguousFieldOcc rdr = Unambiguous rdr PlaceHolder
607
608 rdrNameAmbiguousFieldOcc :: AmbiguousFieldOcc name -> RdrName
609 rdrNameAmbiguousFieldOcc (Unambiguous rdr _) = rdr
610 rdrNameAmbiguousFieldOcc (Ambiguous rdr _) = rdr
611
612 selectorAmbiguousFieldOcc :: AmbiguousFieldOcc Id -> Id
613 selectorAmbiguousFieldOcc (Unambiguous _ sel) = sel
614 selectorAmbiguousFieldOcc (Ambiguous _ sel) = sel
615
616 unambiguousFieldOcc :: AmbiguousFieldOcc Id -> FieldOcc Id
617 unambiguousFieldOcc (Unambiguous rdr sel) = FieldOcc rdr sel
618 unambiguousFieldOcc (Ambiguous rdr sel) = FieldOcc rdr sel
619
620 ambiguousFieldOcc :: FieldOcc name -> AmbiguousFieldOcc name
621 ambiguousFieldOcc (FieldOcc rdr sel) = Unambiguous rdr sel
622
623 {-
624 Note [ConDeclField names]
625 ~~~~~~~~~~~~~~~~~~~~~~~~~
626
627 A ConDeclField contains a list of field occurrences: these always
628 include the field label as the user wrote it. After the renamer, it
629 will additionally contain the identity of the selector function in the
630 second component.
631
632 Due to DuplicateRecordFields, the OccName of the selector function
633 may have been mangled, which is why we keep the original field label
634 separately. For example, when DuplicateRecordFields is enabled
635
636 data T = MkT { x :: Int }
637
638 gives
639
640 ConDeclField { cd_fld_names = [L _ (FieldOcc "x" $sel:x:MkT)], ... }.
641 -}
642
643 -----------------------
644 -- A valid type must have a for-all at the top of the type, or of the fn arg
645 -- types
646
647 mkImplicitHsForAllTy :: LHsType RdrName -> HsType RdrName
648 mkExplicitHsForAllTy :: [LHsTyVarBndr RdrName] -> LHsContext RdrName -> LHsType RdrName -> HsType RdrName
649 mkQualifiedHsForAllTy :: LHsContext RdrName -> LHsType RdrName -> HsType RdrName
650
651 -- | mkImplicitHsForAllTy is called when we encounter
652 -- f :: type
653 -- Wrap around a HsForallTy if one is not there already.
654 mkImplicitHsForAllTy (L _ (HsForAllTy exp extra tvs cxt ty))
655 = HsForAllTy exp' extra tvs cxt ty
656 where
657 exp' = case exp of
658 Qualified -> Implicit
659 -- Qualified is used only for a nested forall,
660 -- this is now top level
661 _ -> exp
662 mkImplicitHsForAllTy ty = mkHsForAllTy Implicit [] (noLoc []) ty
663
664 mkExplicitHsForAllTy tvs ctxt ty = mkHsForAllTy Explicit tvs ctxt ty
665 mkQualifiedHsForAllTy ctxt ty = mkHsForAllTy Qualified [] ctxt ty
666
667 -- |Smart constructor for HsForAllTy, which populates the extra-constraints
668 -- field if a wildcard is present in the context.
669 mkHsForAllTy :: HsExplicitFlag -> [LHsTyVarBndr RdrName] -> LHsContext RdrName -> LHsType RdrName -> HsType RdrName
670 mkHsForAllTy exp tvs ctxt ty
671 = HsForAllTy exp Nothing (mkHsQTvs tvs) ctxt ty
672
673 -- |When a sigtype is parsed, the type found is wrapped in an Implicit
674 -- HsForAllTy via mkImplicitHsForAllTy, to ensure that a signature always has a
675 -- forall at the outer level. For Api Annotations this nested structure is
676 -- important to ensure that all `forall` and `.` locations are retained. From
677 -- the renamer onwards this structure is flattened, to ease the renaming and
678 -- type checking process.
679 flattenTopLevelLHsForAllTy :: LHsType name -> LHsType name
680 flattenTopLevelLHsForAllTy (L l ty) = L l (flattenTopLevelHsForAllTy ty)
681
682 flattenTopLevelHsForAllTy :: HsType name -> HsType name
683 flattenTopLevelHsForAllTy (HsForAllTy exp extra tvs (L l []) ty)
684 = snd $ mk_forall_ty [] l exp extra tvs ty
685 flattenTopLevelHsForAllTy ty = ty
686
687 flattenHsForAllTyKeepAnns :: HsType name -> ([AddAnn],HsType name)
688 flattenHsForAllTyKeepAnns (HsForAllTy exp extra tvs (L l []) ty)
689 = mk_forall_ty [] l exp extra tvs ty
690 flattenHsForAllTyKeepAnns ty = ([],ty)
691
692 -- mk_forall_ty makes a pure for-all type (no context)
693 mk_forall_ty :: [AddAnn] -> SrcSpan -> HsExplicitFlag -> Maybe SrcSpan
694 -> LHsTyVarBndrs name
695 -> LHsType name -> ([AddAnn],HsType name)
696 mk_forall_ty ann _ exp1 extra1 tvs1 (L _ (HsForAllTy exp2 extra qtvs2 ctxt ty))
697 = (ann,HsForAllTy (exp1 `plus` exp2) (mergeExtra extra1 extra)
698 (tvs1 `mappend` qtvs2) ctxt ty)
699 where
700 -- Bias the merging of extra's to the top level, so that a single
701 -- wildcard context will prevail
702 mergeExtra (Just s) _ = Just s
703 mergeExtra _ e = e
704 mk_forall_ty ann l exp extra tvs (L lp (HsParTy ty))
705 = mk_forall_ty (ann ++ mkParensApiAnn lp) l exp extra tvs ty
706 mk_forall_ty ann l exp extra tvs ty
707 = (ann,HsForAllTy exp extra tvs (L l []) ty)
708 -- Even if tvs is empty, we still make a HsForAll!
709 -- In the Implicit case, this signals the place to do implicit quantification
710 -- In the Explicit case, it prevents implicit quantification
711 -- (see the sigtype production in Parser.y)
712 -- so that (forall. ty) isn't implicitly quantified
713
714 plus :: HsExplicitFlag -> HsExplicitFlag -> HsExplicitFlag
715 Qualified `plus` Qualified = Qualified
716 Explicit `plus` _ = Explicit
717 _ `plus` Explicit = Explicit
718 _ `plus` _ = Implicit
719 -- NB: Implicit `plus` Qualified = Implicit
720 -- so that f :: Eq a => a -> a ends up Implicit
721
722 ---------------------
723 hsExplicitTvs :: LHsType Name -> [Name]
724 -- The explicitly-given forall'd type variables of a HsType
725 hsExplicitTvs (L _ (HsForAllTy Explicit _ tvs _ _)) = hsLKiTyVarNames tvs
726 hsExplicitTvs _ = []
727
728 ---------------------
729 hsTyVarName :: HsTyVarBndr name -> name
730 hsTyVarName (UserTyVar (L _ n)) = n
731 hsTyVarName (KindedTyVar (L _ n) _) = n
732
733 hsLTyVarName :: LHsTyVarBndr name -> name
734 hsLTyVarName = hsTyVarName . unLoc
735
736 hsLTyVarNames :: LHsTyVarBndrs name -> [name]
737 -- Type variables only
738 hsLTyVarNames qtvs = map hsLTyVarName (hsQTvBndrs qtvs)
739
740 hsLKiTyVarNames :: LHsTyVarBndrs Name -> [Name]
741 -- Kind and type variables
742 hsLKiTyVarNames (HsQTvs { hsq_kvs = kvs, hsq_tvs = tvs })
743 = kvs ++ map hsLTyVarName tvs
744
745 hsLTyVarLocName :: LHsTyVarBndr name -> Located name
746 hsLTyVarLocName = fmap hsTyVarName
747
748 hsLTyVarLocNames :: LHsTyVarBndrs name -> [Located name]
749 hsLTyVarLocNames qtvs = map hsLTyVarLocName (hsQTvBndrs qtvs)
750
751 -- | Convert a LHsTyVarBndr to an equivalent LHsType. Used in Template Haskell
752 -- quoting for type family equations.
753 hsLTyVarBndrToType :: LHsTyVarBndr name -> LHsType name
754 hsLTyVarBndrToType = fmap cvt
755 where cvt (UserTyVar n) = HsTyVar n
756 cvt (KindedTyVar (L name_loc n) kind)
757 = HsKindSig (L name_loc (HsTyVar (L name_loc n))) kind
758
759 -- | Convert a LHsTyVarBndrs to a list of types. Used in Template Haskell
760 -- quoting for type family equations. Works on *type* variable only, no kind
761 -- vars.
762 hsLTyVarBndrsToTypes :: LHsTyVarBndrs name -> [LHsType name]
763 hsLTyVarBndrsToTypes (HsQTvs { hsq_tvs = tvbs }) = map hsLTyVarBndrToType tvbs
764
765 ---------------------
766 mkAnonWildCardTy :: HsType RdrName
767 mkAnonWildCardTy = HsWildCardTy (AnonWildCard PlaceHolder)
768
769 mkNamedWildCardTy :: Located n -> HsType n
770 mkNamedWildCardTy = HsWildCardTy . NamedWildCard
771
772 isAnonWildCard :: HsWildCardInfo name -> Bool
773 isAnonWildCard (AnonWildCard _) = True
774 isAnonWildCard _ = False
775
776 isNamedWildCard :: HsWildCardInfo name -> Bool
777 isNamedWildCard = not . isAnonWildCard
778
779 wildCardName :: HsWildCardInfo Name -> Name
780 wildCardName (NamedWildCard (L _ n)) = n
781 wildCardName (AnonWildCard (L _ n)) = n
782
783 -- Two wild cards are the same when: they're both named and have the same
784 -- name, or they're both anonymous and have the same location.
785 sameWildCard :: Eq name
786 => Located (HsWildCardInfo name)
787 -> Located (HsWildCardInfo name) -> Bool
788 sameWildCard (L l1 (AnonWildCard _)) (L l2 (AnonWildCard _)) = l1 == l2
789 sameWildCard (L _ (NamedWildCard (L _ n1)))
790 (L _ (NamedWildCard (L _ n2))) = n1 == n2
791 sameWildCard _ _ = False
792
793 sameNamedWildCard :: Eq name
794 => Located (HsWildCardInfo name)
795 -> Located (HsWildCardInfo name) -> Bool
796 sameNamedWildCard (L _ (NamedWildCard (L _ n1)))
797 (L _ (NamedWildCard (L _ n2))) = n1 == n2
798 sameNamedWildCard _ _ = False
799
800 splitHsAppTys :: LHsType n -> [LHsType n] -> (LHsType n, [LHsType n])
801 splitHsAppTys (L _ (HsAppTy f a)) as = splitHsAppTys f (a:as)
802 splitHsAppTys (L _ (HsParTy f)) as = splitHsAppTys f as
803 splitHsAppTys f as = (f,as)
804
805 -- retrieve the name of the "head" of a nested type application
806 -- somewhat like splitHsAppTys, but a little more thorough
807 -- used to examine the result of a GADT-like datacon, so it doesn't handle
808 -- *all* cases (like lists, tuples, (~), etc.)
809 hsTyGetAppHead_maybe :: LHsType n -> Maybe (n, [LHsType n])
810 hsTyGetAppHead_maybe = go []
811 where
812 go tys (L _ (HsTyVar (L _ n))) = Just (n, tys)
813 go tys (L _ (HsAppTy l r)) = go (r : tys) l
814 go tys (L _ (HsOpTy l (_, L _ n) r)) = Just (n, l : r : tys)
815 go tys (L _ (HsParTy t)) = go tys t
816 go tys (L _ (HsKindSig t _)) = go tys t
817 go _ _ = Nothing
818
819 mkHsAppTys :: OutputableBndr n => LHsType n -> [LHsType n] -> HsType n
820 mkHsAppTys fun_ty [] = pprPanic "mkHsAppTys" (ppr fun_ty)
821 mkHsAppTys fun_ty (arg_ty:arg_tys)
822 = foldl mk_app (HsAppTy fun_ty arg_ty) arg_tys
823 where
824 mk_app fun arg = HsAppTy (noLoc fun) arg
825 -- Add noLocs for inner nodes of the application;
826 -- they are never used
827
828 splitLHsInstDeclTy_maybe
829 :: LHsType name
830 -> Maybe (LHsTyVarBndrs name, HsContext name, Located name, [LHsType name])
831 -- Split up an instance decl type, returning the pieces
832 splitLHsInstDeclTy_maybe inst_ty = do
833 let (tvs, cxt, ty) = splitLHsForAllTy inst_ty
834 (cls, tys) <- splitLHsClassTy_maybe ty
835 return (tvs, cxt, cls, tys)
836
837 splitLHsForAllTy
838 :: LHsType name
839 -> (LHsTyVarBndrs name, HsContext name, LHsType name)
840 splitLHsForAllTy poly_ty
841 = case unLoc poly_ty of
842 HsParTy ty -> splitLHsForAllTy ty
843 HsForAllTy _ _ tvs cxt ty -> (tvs, unLoc cxt, ty)
844 _ -> (emptyHsQTvs, [], poly_ty)
845 -- The type vars should have been computed by now, even if they were implicit
846
847 splitHsClassTy_maybe :: HsType name -> Maybe (name, [LHsType name])
848 splitHsClassTy_maybe ty = fmap (\(L _ n, tys) -> (n, tys)) $ splitLHsClassTy_maybe (noLoc ty)
849
850 splitLHsClassTy_maybe :: LHsType name -> Maybe (Located name, [LHsType name])
851 --- Watch out.. in ...deriving( Show )... we use this on
852 --- the list of partially applied predicates in the deriving,
853 --- so there can be zero args.
854
855 -- In TcDeriv we also use this to figure out what data type is being
856 -- mentioned in a deriving (Generic (Foo bar baz)) declaration (i.e. "Foo").
857 splitLHsClassTy_maybe ty
858 = checkl ty []
859 where
860 checkl (L _ ty) args = case ty of
861 HsTyVar (L lt t) -> Just (L lt t, args)
862 HsAppTy l r -> checkl l (r:args)
863 HsOpTy l (_,L lt tc) r -> checkl (L lt (HsTyVar (L lt tc))) (l:r:args)
864 HsParTy t -> checkl t args
865 HsKindSig ty _ -> checkl ty args
866 _ -> Nothing
867
868 -- splitHsFunType decomposes a type (t1 -> t2 ... -> tn)
869 -- Breaks up any parens in the result type:
870 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
871 -- Also deals with (->) t1 t2; that is why it only works on LHsType Name
872 -- (see Trac #9096)
873 splitHsFunType :: LHsType Name -> ([LHsType Name], LHsType Name)
874 splitHsFunType (L _ (HsParTy ty))
875 = splitHsFunType ty
876
877 splitHsFunType (L _ (HsFunTy x y))
878 | (args, res) <- splitHsFunType y
879 = (x:args, res)
880
881 splitHsFunType orig_ty@(L _ (HsAppTy t1 t2))
882 = go t1 [t2]
883 where -- Look for (->) t1 t2, possibly with parenthesisation
884 go (L _ (HsTyVar (L _ fn))) tys | fn == funTyConName
885 , [t1,t2] <- tys
886 , (args, res) <- splitHsFunType t2
887 = (t1:args, res)
888 go (L _ (HsAppTy t1 t2)) tys = go t1 (t2:tys)
889 go (L _ (HsParTy ty)) tys = go ty tys
890 go _ _ = ([], orig_ty) -- Failure to match
891
892 splitHsFunType other = ([], other)
893
894
895 ignoreParens :: LHsType name -> LHsType name
896 ignoreParens (L _ (HsParTy ty)) = ignoreParens ty
897 ignoreParens ty = ty
898
899 {-
900 ************************************************************************
901 * *
902 \subsection{Pretty printing}
903 * *
904 ************************************************************************
905 -}
906
907 instance (OutputableBndr name) => Outputable (HsType name) where
908 ppr ty = pprHsType ty
909
910 instance Outputable HsTyLit where
911 ppr = ppr_tylit
912
913 instance (OutputableBndr name) => Outputable (LHsTyVarBndrs name) where
914 ppr (HsQTvs { hsq_kvs = kvs, hsq_tvs = tvs })
915 = sep [ ifPprDebug $ braces (interppSP kvs), interppSP tvs ]
916
917 instance (OutputableBndr name) => Outputable (HsTyVarBndr name) where
918 ppr (UserTyVar n) = ppr n
919 ppr (KindedTyVar n k) = parens $ hsep [ppr n, dcolon, ppr k]
920
921 instance (Outputable thing) => Outputable (HsWithBndrs name thing) where
922 ppr (HsWB { hswb_cts = ty }) = ppr ty
923
924 instance (Outputable name) => Outputable (HsWildCardInfo name) where
925 ppr (AnonWildCard _) = char '_'
926 ppr (NamedWildCard n) = ppr n
927
928 pprHsForAll :: OutputableBndr name => HsExplicitFlag -> LHsTyVarBndrs name -> LHsContext name -> SDoc
929 pprHsForAll exp = pprHsForAllExtra exp Nothing
930
931 -- | Version of 'pprHsForAll' that can also print an extra-constraints
932 -- wildcard, e.g. @_ => a -> Bool@ or @(Show a, _) => a -> String@. This
933 -- underscore will be printed when the 'Maybe SrcSpan' argument is a 'Just'
934 -- containing the location of the extra-constraints wildcard. A special
935 -- function for this is needed, as the extra-constraints wildcard is removed
936 -- from the actual context and type, and stored in a separate field, thus just
937 -- printing the type will not print the extra-constraints wildcard.
938 pprHsForAllExtra :: OutputableBndr name => HsExplicitFlag -> Maybe SrcSpan -> LHsTyVarBndrs name -> LHsContext name -> SDoc
939 pprHsForAllExtra exp extra qtvs cxt
940 | show_forall = forall_part <+> pprHsContextExtra show_extra (unLoc cxt)
941 | otherwise = pprHsContextExtra show_extra (unLoc cxt)
942 where
943 show_extra = isJust extra
944 show_forall = opt_PprStyle_Debug
945 || (not (null (hsQTvBndrs qtvs)) && is_explicit)
946 is_explicit = case exp of {Explicit -> True; Implicit -> False; Qualified -> False}
947 forall_part = forAllLit <+> ppr qtvs <> dot
948
949 pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc
950 pprHsContext = maybe empty (<+> darrow) . pprHsContextMaybe
951
952 pprHsContextNoArrow :: (OutputableBndr name) => HsContext name -> SDoc
953 pprHsContextNoArrow = fromMaybe empty . pprHsContextMaybe
954
955 pprHsContextMaybe :: (OutputableBndr name) => HsContext name -> Maybe SDoc
956 pprHsContextMaybe [] = Nothing
957 pprHsContextMaybe [L _ pred] = Just $ ppr_mono_ty FunPrec pred
958 pprHsContextMaybe cxt = Just $ parens (interpp'SP cxt)
959
960 -- True <=> print an extra-constraints wildcard, e.g. @(Show a, _) =>@
961 pprHsContextExtra :: (OutputableBndr name) => Bool -> HsContext name -> SDoc
962 pprHsContextExtra False = pprHsContext
963 pprHsContextExtra True
964 = \ctxt -> case ctxt of
965 [] -> char '_' <+> darrow
966 _ -> parens (sep (punctuate comma ctxt')) <+> darrow
967 where ctxt' = map ppr ctxt ++ [char '_']
968
969 pprConDeclFields :: OutputableBndr name => [LConDeclField name] -> SDoc
970 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
971 where
972 ppr_fld (L _ (ConDeclField { cd_fld_names = ns, cd_fld_type = ty,
973 cd_fld_doc = doc }))
974 = ppr_names ns <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
975 ppr_names [n] = ppr n
976 ppr_names ns = sep (punctuate comma (map ppr ns))
977
978 {-
979 Note [Printing KindedTyVars]
980 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
981 Trac #3830 reminded me that we should really only print the kind
982 signature on a KindedTyVar if the kind signature was put there by the
983 programmer. During kind inference GHC now adds a PostTcKind to UserTyVars,
984 rather than converting to KindedTyVars as before.
985
986 (As it happens, the message in #3830 comes out a different way now,
987 and the problem doesn't show up; but having the flag on a KindedTyVar
988 seems like the Right Thing anyway.)
989 -}
990
991 -- Printing works more-or-less as for Types
992
993 pprHsType, pprParendHsType :: (OutputableBndr name) => HsType name -> SDoc
994
995 pprHsType ty = getPprStyle $ \sty -> ppr_mono_ty TopPrec (prepare sty ty)
996 pprParendHsType ty = ppr_mono_ty TyConPrec ty
997
998 -- Before printing a type
999 -- (a) Remove outermost HsParTy parens
1000 -- (b) Drop top-level for-all type variables in user style
1001 -- since they are implicit in Haskell
1002 prepare :: PprStyle -> HsType name -> HsType name
1003 prepare sty (HsParTy ty) = prepare sty (unLoc ty)
1004 prepare _ ty = ty
1005
1006 ppr_mono_lty :: (OutputableBndr name) => TyPrec -> LHsType name -> SDoc
1007 ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty)
1008
1009 ppr_mono_ty :: (OutputableBndr name) => TyPrec -> HsType name -> SDoc
1010 ppr_mono_ty ctxt_prec (HsForAllTy exp extra tvs ctxt ty)
1011 = maybeParen ctxt_prec FunPrec $
1012 sep [pprHsForAllExtra exp extra tvs ctxt, ppr_mono_lty TopPrec ty]
1013
1014 ppr_mono_ty _ (HsBangTy b ty) = ppr b <> ppr_mono_lty TyConPrec ty
1015 ppr_mono_ty _ (HsRecTy flds) = pprConDeclFields flds
1016 ppr_mono_ty _ (HsTyVar (L _ name))= pprPrefixOcc name
1017 ppr_mono_ty prec (HsFunTy ty1 ty2) = ppr_fun_ty prec ty1 ty2
1018 ppr_mono_ty _ (HsTupleTy con tys) = tupleParens std_con (pprWithCommas ppr tys)
1019 where std_con = case con of
1020 HsUnboxedTuple -> UnboxedTuple
1021 _ -> BoxedTuple
1022 ppr_mono_ty _ (HsKindSig ty kind) = parens (ppr_mono_lty TopPrec ty <+> dcolon <+> ppr kind)
1023 ppr_mono_ty _ (HsListTy ty) = brackets (ppr_mono_lty TopPrec ty)
1024 ppr_mono_ty _ (HsPArrTy ty) = paBrackets (ppr_mono_lty TopPrec ty)
1025 ppr_mono_ty prec (HsIParamTy n ty) = maybeParen prec FunPrec (ppr n <+> dcolon <+> ppr_mono_lty TopPrec ty)
1026 ppr_mono_ty _ (HsSpliceTy s _) = pprSplice s
1027 ppr_mono_ty _ (HsCoreTy ty) = ppr ty
1028 ppr_mono_ty _ (HsExplicitListTy _ tys) = quote $ brackets (interpp'SP tys)
1029 ppr_mono_ty _ (HsExplicitTupleTy _ tys) = quote $ parens (interpp'SP tys)
1030 ppr_mono_ty _ (HsTyLit t) = ppr_tylit t
1031 ppr_mono_ty _ (HsWildCardTy (AnonWildCard _)) = char '_'
1032 ppr_mono_ty _ (HsWildCardTy (NamedWildCard name)) = ppr name
1033
1034 ppr_mono_ty ctxt_prec (HsWrapTy (WpKiApps _kis) ty)
1035 = ppr_mono_ty ctxt_prec ty
1036 -- We are not printing kind applications. If we wanted to do so, we should do
1037 -- something like this:
1038 {-
1039 = go ctxt_prec kis ty
1040 where
1041 go ctxt_prec [] ty = ppr_mono_ty ctxt_prec ty
1042 go ctxt_prec (ki:kis) ty
1043 = maybeParen ctxt_prec TyConPrec $
1044 hsep [ go FunPrec kis ty
1045 , ptext (sLit "@") <> pprParendKind ki ]
1046 -}
1047
1048 ppr_mono_ty ctxt_prec (HsEqTy ty1 ty2)
1049 = maybeParen ctxt_prec TyOpPrec $
1050 ppr_mono_lty TyOpPrec ty1 <+> char '~' <+> ppr_mono_lty TyOpPrec ty2
1051
1052 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
1053 = maybeParen ctxt_prec TyConPrec $
1054 hsep [ppr_mono_lty FunPrec fun_ty, ppr_mono_lty TyConPrec arg_ty]
1055
1056 ppr_mono_ty ctxt_prec (HsOpTy ty1 (_wrapper, L _ op) ty2)
1057 = maybeParen ctxt_prec TyOpPrec $
1058 sep [ ppr_mono_lty TyOpPrec ty1
1059 , sep [pprInfixOcc op, ppr_mono_lty TyOpPrec ty2 ] ]
1060 -- Don't print the wrapper (= kind applications)
1061 -- c.f. HsWrapTy
1062
1063 ppr_mono_ty _ (HsParTy ty)
1064 = parens (ppr_mono_lty TopPrec ty)
1065 -- Put the parens in where the user did
1066 -- But we still use the precedence stuff to add parens because
1067 -- toHsType doesn't put in any HsParTys, so we may still need them
1068
1069 ppr_mono_ty ctxt_prec (HsDocTy ty doc)
1070 = maybeParen ctxt_prec TyOpPrec $
1071 ppr_mono_lty TyOpPrec ty <+> ppr (unLoc doc)
1072 -- we pretty print Haddock comments on types as if they were
1073 -- postfix operators
1074
1075 --------------------------
1076 ppr_fun_ty :: (OutputableBndr name) => TyPrec -> LHsType name -> LHsType name -> SDoc
1077 ppr_fun_ty ctxt_prec ty1 ty2
1078 = let p1 = ppr_mono_lty FunPrec ty1
1079 p2 = ppr_mono_lty TopPrec ty2
1080 in
1081 maybeParen ctxt_prec FunPrec $
1082 sep [p1, ptext (sLit "->") <+> p2]
1083
1084 --------------------------
1085 ppr_tylit :: HsTyLit -> SDoc
1086 ppr_tylit (HsNumTy _ i) = integer i
1087 ppr_tylit (HsStrTy _ s) = text (show s)