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