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