9bb73c361bf16f045cb4e50ad3b896681b02813f
[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 {-# LANGUAGE TypeFamilies #-}
19
20 module HsTypes (
21 HsType(..), NewHsTypeX(..), LHsType, HsKind, LHsKind,
22 HsTyVarBndr(..), LHsTyVarBndr, ForallVisFlag(..),
23 LHsQTyVars(..),
24 HsImplicitBndrs(..),
25 HsWildCardBndrs(..),
26 LHsSigType, LHsSigWcType, LHsWcType,
27 HsTupleSort(..),
28 HsContext, LHsContext, noLHsContext,
29 HsTyLit(..),
30 HsIPName(..), hsIPNameFS,
31 HsArg(..), numVisibleArgs,
32 LHsTypeArg,
33
34 LBangType, BangType,
35 HsSrcBang(..), HsImplBang(..),
36 SrcStrictness(..), SrcUnpackedness(..),
37 getBangType, getBangStrictness,
38
39 ConDeclField(..), LConDeclField, pprConDeclFields,
40
41 HsConDetails(..),
42
43 FieldOcc(..), LFieldOcc, mkFieldOcc,
44 AmbiguousFieldOcc(..), mkAmbiguousFieldOcc,
45 rdrNameAmbiguousFieldOcc, selectorAmbiguousFieldOcc,
46 unambiguousFieldOcc, ambiguousFieldOcc,
47
48 mkAnonWildCardTy, pprAnonWildCard,
49
50 mkHsImplicitBndrs, mkHsWildCardBndrs, hsImplicitBody,
51 mkEmptyImplicitBndrs, mkEmptyWildCardBndrs,
52 mkHsQTvs, hsQTvExplicit, emptyLHsQTvs, isEmptyLHsQTvs,
53 isHsKindedTyVar, hsTvbAllKinded, isLHsForAllTy,
54 hsScopedTvs, hsWcScopedTvs, dropWildCards,
55 hsTyVarName, hsAllLTyVarNames, hsLTyVarLocNames,
56 hsLTyVarName, hsLTyVarNames, hsLTyVarLocName, hsExplicitLTyVarNames,
57 splitLHsInstDeclTy, getLHsInstDeclHead, getLHsInstDeclClass_maybe,
58 splitLHsPatSynTy,
59 splitLHsForAllTy, splitLHsForAllTyInvis,
60 splitLHsQualTy, splitLHsSigmaTy, splitLHsSigmaTyInvis,
61 splitHsFunType, hsTyGetAppHead_maybe,
62 mkHsOpTy, mkHsAppTy, mkHsAppTys, mkHsAppKindTy,
63 ignoreParens, hsSigType, hsSigWcType,
64 hsLTyVarBndrToType, hsLTyVarBndrsToTypes,
65
66 -- Printing
67 pprHsType, pprHsForAll, pprHsForAllExtra, pprHsExplicitForAll,
68 pprLHsContext,
69 hsTypeNeedsParens, parenthesizeHsType, parenthesizeHsContext
70 ) where
71
72 import GhcPrelude
73
74 import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
75
76 import HsExtension
77 import HsLit () -- for instances
78
79 import Id ( Id )
80 import Name( Name )
81 import RdrName ( RdrName )
82 import DataCon( HsSrcBang(..), HsImplBang(..),
83 SrcStrictness(..), SrcUnpackedness(..) )
84 import TysPrim( funTyConName )
85 import Type
86 import HsDoc
87 import BasicTypes
88 import SrcLoc
89 import Outputable
90 import FastString
91 import Maybes( isJust )
92 import Util ( count )
93
94 import Data.Data hiding ( Fixity, Prefix, Infix )
95
96 {-
97 ************************************************************************
98 * *
99 \subsection{Bang annotations}
100 * *
101 ************************************************************************
102 -}
103
104 -- | Located Bang Type
105 type LBangType pass = Located (BangType pass)
106
107 -- | Bang Type
108 type BangType pass = HsType pass -- Bangs are in the HsType data type
109
110 getBangType :: LHsType a -> LHsType a
111 getBangType (L _ (HsBangTy _ _ ty)) = ty
112 getBangType ty = ty
113
114 getBangStrictness :: LHsType a -> HsSrcBang
115 getBangStrictness (L _ (HsBangTy _ s _)) = s
116 getBangStrictness _ = (HsSrcBang NoSourceText NoSrcUnpack NoSrcStrict)
117
118 {-
119 ************************************************************************
120 * *
121 \subsection{Data types}
122 * *
123 ************************************************************************
124
125 This is the syntax for types as seen in type signatures.
126
127 Note [HsBSig binder lists]
128 ~~~~~~~~~~~~~~~~~~~~~~~~~~
129 Consider a binder (or pattern) decorated with a type or kind,
130 \ (x :: a -> a). blah
131 forall (a :: k -> *) (b :: k). blah
132 Then we use a LHsBndrSig on the binder, so that the
133 renamer can decorate it with the variables bound
134 by the pattern ('a' in the first example, 'k' in the second),
135 assuming that neither of them is in scope already
136 See also Note [Kind and type-variable binders] in RnTypes
137
138 Note [HsType binders]
139 ~~~~~~~~~~~~~~~~~~~~~
140 The system for recording type and kind-variable binders in HsTypes
141 is a bit complicated. Here's how it works.
142
143 * In a HsType,
144 HsForAllTy represents an /explicit, user-written/ 'forall'
145 e.g. forall a b. {...} or
146 forall a b -> {...}
147 HsQualTy represents an /explicit, user-written/ context
148 e.g. (Eq a, Show a) => ...
149 The context can be empty if that's what the user wrote
150 These constructors represent what the user wrote, no more
151 and no less.
152
153 * The ForallVisFlag field of HsForAllTy represents whether a forall is
154 invisible (e.g., forall a b. {...}, with a dot) or visible
155 (e.g., forall a b -> {...}, with an arrow).
156
157 * HsTyVarBndr describes a quantified type variable written by the
158 user. For example
159 f :: forall a (b :: *). blah
160 here 'a' and '(b::*)' are each a HsTyVarBndr. A HsForAllTy has
161 a list of LHsTyVarBndrs.
162
163 * HsImplicitBndrs is a wrapper that gives the implicitly-quantified
164 kind and type variables of the wrapped thing. It is filled in by
165 the renamer. For example, if the user writes
166 f :: a -> a
167 the HsImplicitBinders binds the 'a' (not a HsForAllTy!).
168 NB: this implicit quantification is purely lexical: we bind any
169 type or kind variables that are not in scope. The type checker
170 may subsequently quantify over further kind variables.
171
172 * HsWildCardBndrs is a wrapper that binds the wildcard variables
173 of the wrapped thing. It is filled in by the renamer
174 f :: _a -> _
175 The enclosing HsWildCardBndrs binds the wildcards _a and _.
176
177 * The explicit presence of these wrappers specifies, in the HsSyn,
178 exactly where implicit quantification is allowed, and where
179 wildcards are allowed.
180
181 * LHsQTyVars is used in data/class declarations, where the user gives
182 explicit *type* variable bindings, but we need to implicitly bind
183 *kind* variables. For example
184 class C (a :: k -> *) where ...
185 The 'k' is implicitly bound in the hsq_tvs field of LHsQTyVars
186
187 Note [The wildcard story for types]
188 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
189 Types can have wildcards in them, to support partial type signatures,
190 like f :: Int -> (_ , _a) -> _a
191
192 A wildcard in a type can be
193
194 * An anonymous wildcard,
195 written '_'
196 In HsType this is represented by HsWildCardTy.
197 The renamer leaves it untouched, and it is later given fresh meta tyvars in
198 the typechecker.
199
200 * A named wildcard,
201 written '_a', '_foo', etc
202 In HsType this is represented by (HsTyVar "_a")
203 i.e. a perfectly ordinary type variable that happens
204 to start with an underscore
205
206 Note carefully:
207
208 * When NamedWildCards is off, type variables that start with an
209 underscore really /are/ ordinary type variables. And indeed, even
210 when NamedWildCards is on you can bind _a explicitly as an ordinary
211 type variable:
212 data T _a _b = MkT _b _a
213 Or even:
214 f :: forall _a. _a -> _b
215 Here _a is an ordinary forall'd binder, but (With NamedWildCards)
216 _b is a named wildcard. (See the comments in #10982)
217
218 * Named wildcards are bound by the HsWildCardBndrs construct, which wraps
219 types that are allowed to have wildcards. Unnamed wildcards however are left
220 unchanged until typechecking, where we give them fresh wild tyavrs and
221 determine whether or not to emit hole constraints on each wildcard
222 (we don't if it's a visible type/kind argument or a type family pattern).
223 See related notes Note [Wildcards in visible kind application]
224 and Note [Wildcards in visible type application] in TcHsType.hs
225
226 * After type checking is done, we report what types the wildcards
227 got unified with.
228
229 Note [Ordering of implicit variables]
230 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231 Since the advent of -XTypeApplications, GHC makes promises about the ordering
232 of implicit variable quantification. Specifically, we offer that implicitly
233 quantified variables (such as those in const :: a -> b -> a, without a `forall`)
234 will occur in left-to-right order of first occurrence. Here are a few examples:
235
236 const :: a -> b -> a -- forall a b. ...
237 f :: Eq a => b -> a -> a -- forall a b. ... contexts are included
238
239 type a <-< b = b -> a
240 g :: a <-< b -- forall a b. ... type synonyms matter
241
242 class Functor f where
243 fmap :: (a -> b) -> f a -> f b -- forall f a b. ...
244 -- The f is quantified by the class, so only a and b are considered in fmap
245
246 This simple story is complicated by the possibility of dependency: all variables
247 must come after any variables mentioned in their kinds.
248
249 typeRep :: Typeable a => TypeRep (a :: k) -- forall k a. ...
250
251 The k comes first because a depends on k, even though the k appears later than
252 the a in the code. Thus, GHC does a *stable topological sort* on the variables.
253 By "stable", we mean that any two variables who do not depend on each other
254 preserve their existing left-to-right ordering.
255
256 Implicitly bound variables are collected by the extract- family of functions
257 (extractHsTysRdrTyVars, extractHsTyVarBndrsKVs, etc.) in RnTypes.
258 These functions thus promise to keep left-to-right ordering.
259 Look for pointers to this note to see the places where the action happens.
260
261 Note that we also maintain this ordering in kind signatures. Even though
262 there's no visible kind application (yet), having implicit variables be
263 quantified in left-to-right order in kind signatures is nice since:
264
265 * It's consistent with the treatment for type signatures.
266 * It can affect how types are displayed with -fprint-explicit-kinds (see
267 #15568 for an example), which is a situation where knowing the order in
268 which implicit variables are quantified can be useful.
269 * In the event that visible kind application is implemented, the order in
270 which we would expect implicit variables to be ordered in kinds will have
271 already been established.
272 -}
273
274 -- | Located Haskell Context
275 type LHsContext pass = Located (HsContext pass)
276 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnUnit'
277 -- For details on above see note [Api annotations] in ApiAnnotation
278
279 noLHsContext :: LHsContext pass
280 -- Use this when there is no context in the original program
281 -- It would really be more kosher to use a Maybe, to distinguish
282 -- class () => C a where ...
283 -- from
284 -- class C a where ...
285 noLHsContext = noLoc []
286
287 -- | Haskell Context
288 type HsContext pass = [LHsType pass]
289
290 -- | Located Haskell Type
291 type LHsType pass = Located (HsType pass)
292 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
293 -- in a list
294
295 -- For details on above see note [Api annotations] in ApiAnnotation
296
297 -- | Haskell Kind
298 type HsKind pass = HsType pass
299
300 -- | Located Haskell Kind
301 type LHsKind pass = Located (HsKind pass)
302 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
303
304 -- For details on above see note [Api annotations] in ApiAnnotation
305
306 --------------------------------------------------
307 -- LHsQTyVars
308 -- The explicitly-quantified binders in a data/type declaration
309
310 -- | Located Haskell Type Variable Binder
311 type LHsTyVarBndr pass = Located (HsTyVarBndr pass)
312 -- See Note [HsType binders]
313
314 -- | Located Haskell Quantified Type Variables
315 data LHsQTyVars pass -- See Note [HsType binders]
316 = HsQTvs { hsq_ext :: XHsQTvs pass
317
318 , hsq_explicit :: [LHsTyVarBndr pass]
319 -- Explicit variables, written by the user
320 -- See Note [HsForAllTy tyvar binders]
321 }
322 | XLHsQTyVars (XXLHsQTyVars pass)
323
324 type HsQTvsRn = [Name] -- Implicit variables
325 -- For example, in data T (a :: k1 -> k2) = ...
326 -- the 'a' is explicit while 'k1', 'k2' are implicit
327
328 type instance XHsQTvs GhcPs = NoExt
329 type instance XHsQTvs GhcRn = HsQTvsRn
330 type instance XHsQTvs GhcTc = HsQTvsRn
331
332 type instance XXLHsQTyVars (GhcPass _) = NoExt
333
334 mkHsQTvs :: [LHsTyVarBndr GhcPs] -> LHsQTyVars GhcPs
335 mkHsQTvs tvs = HsQTvs { hsq_ext = noExt, hsq_explicit = tvs }
336
337 hsQTvExplicit :: LHsQTyVars pass -> [LHsTyVarBndr pass]
338 hsQTvExplicit = hsq_explicit
339
340 emptyLHsQTvs :: LHsQTyVars GhcRn
341 emptyLHsQTvs = HsQTvs { hsq_ext = [], hsq_explicit = [] }
342
343 isEmptyLHsQTvs :: LHsQTyVars GhcRn -> Bool
344 isEmptyLHsQTvs (HsQTvs { hsq_ext = imp, hsq_explicit = exp })
345 = null imp && null exp
346 isEmptyLHsQTvs _ = False
347
348 ------------------------------------------------
349 -- HsImplicitBndrs
350 -- Used to quantify the implicit binders of a type
351 -- * Implicit binders of a type signature (LHsSigType/LHsSigWcType)
352 -- * Patterns in a type/data family instance (HsTyPats)
353
354 -- | Haskell Implicit Binders
355 data HsImplicitBndrs pass thing -- See Note [HsType binders]
356 = HsIB { hsib_ext :: XHsIB pass thing -- after renamer: [Name]
357 -- Implicitly-bound kind & type vars
358 -- Order is important; see
359 -- Note [Ordering of implicit variables]
360 -- in RnTypes
361
362 , hsib_body :: thing -- Main payload (type or list of types)
363 }
364 | XHsImplicitBndrs (XXHsImplicitBndrs pass thing)
365
366 type instance XHsIB GhcPs _ = NoExt
367 type instance XHsIB GhcRn _ = [Name]
368 type instance XHsIB GhcTc _ = [Name]
369
370 type instance XXHsImplicitBndrs (GhcPass _) _ = NoExt
371
372 -- | Haskell Wildcard Binders
373 data HsWildCardBndrs pass thing
374 -- See Note [HsType binders]
375 -- See Note [The wildcard story for types]
376 = HsWC { hswc_ext :: XHsWC pass thing
377 -- after the renamer
378 -- Wild cards, only named
379 -- See Note [Wildcards in visible kind application]
380
381 , hswc_body :: thing
382 -- Main payload (type or list of types)
383 -- If there is an extra-constraints wildcard,
384 -- it's still there in the hsc_body.
385 }
386 | XHsWildCardBndrs (XXHsWildCardBndrs pass thing)
387
388 type instance XHsWC GhcPs b = NoExt
389 type instance XHsWC GhcRn b = [Name]
390 type instance XHsWC GhcTc b = [Name]
391
392 type instance XXHsWildCardBndrs (GhcPass _) b = NoExt
393
394 -- | Located Haskell Signature Type
395 type LHsSigType pass = HsImplicitBndrs pass (LHsType pass) -- Implicit only
396
397 -- | Located Haskell Wildcard Type
398 type LHsWcType pass = HsWildCardBndrs pass (LHsType pass) -- Wildcard only
399
400 -- | Located Haskell Signature Wildcard Type
401 type LHsSigWcType pass = HsWildCardBndrs pass (LHsSigType pass) -- Both
402
403 -- See Note [Representing type signatures]
404
405 hsImplicitBody :: HsImplicitBndrs pass thing -> thing
406 hsImplicitBody (HsIB { hsib_body = body }) = body
407 hsImplicitBody (XHsImplicitBndrs _) = panic "hsImplicitBody"
408
409 hsSigType :: LHsSigType pass -> LHsType pass
410 hsSigType = hsImplicitBody
411
412 hsSigWcType :: LHsSigWcType pass -> LHsType pass
413 hsSigWcType sig_ty = hsib_body (hswc_body sig_ty)
414
415 dropWildCards :: LHsSigWcType pass -> LHsSigType pass
416 -- Drop the wildcard part of a LHsSigWcType
417 dropWildCards sig_ty = hswc_body sig_ty
418
419 {- Note [Representing type signatures]
420 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
421 HsSigType is used to represent an explicit user type signature
422 such as f :: a -> a
423 or g (x :: a -> a) = x
424
425 A HsSigType is just a HsImplicitBndrs wrapping a LHsType.
426 * The HsImplicitBndrs binds the /implicitly/ quantified tyvars
427 * The LHsType binds the /explicitly/ quantified tyvars
428
429 E.g. For a signature like
430 f :: forall (a::k). blah
431 we get
432 HsIB { hsib_vars = [k]
433 , hsib_body = HsForAllTy { hst_bndrs = [(a::*)]
434 , hst_body = blah }
435 The implicit kind variable 'k' is bound by the HsIB;
436 the explicitly forall'd tyvar 'a' is bound by the HsForAllTy
437 -}
438
439 mkHsImplicitBndrs :: thing -> HsImplicitBndrs GhcPs thing
440 mkHsImplicitBndrs x = HsIB { hsib_ext = noExt
441 , hsib_body = x }
442
443 mkHsWildCardBndrs :: thing -> HsWildCardBndrs GhcPs thing
444 mkHsWildCardBndrs x = HsWC { hswc_body = x
445 , hswc_ext = noExt }
446
447 -- Add empty binders. This is a bit suspicious; what if
448 -- the wrapped thing had free type variables?
449 mkEmptyImplicitBndrs :: thing -> HsImplicitBndrs GhcRn thing
450 mkEmptyImplicitBndrs x = HsIB { hsib_ext = []
451 , hsib_body = x }
452
453 mkEmptyWildCardBndrs :: thing -> HsWildCardBndrs GhcRn thing
454 mkEmptyWildCardBndrs x = HsWC { hswc_body = x
455 , hswc_ext = [] }
456
457
458 --------------------------------------------------
459 -- | These names are used early on to store the names of implicit
460 -- parameters. They completely disappear after type-checking.
461 newtype HsIPName = HsIPName FastString
462 deriving( Eq, Data )
463
464 hsIPNameFS :: HsIPName -> FastString
465 hsIPNameFS (HsIPName n) = n
466
467 instance Outputable HsIPName where
468 ppr (HsIPName n) = char '?' <> ftext n -- Ordinary implicit parameters
469
470 instance OutputableBndr HsIPName where
471 pprBndr _ n = ppr n -- Simple for now
472 pprInfixOcc n = ppr n
473 pprPrefixOcc n = ppr n
474
475 --------------------------------------------------
476
477 -- | Haskell Type Variable Binder
478 data HsTyVarBndr pass
479 = UserTyVar -- no explicit kinding
480 (XUserTyVar pass)
481 (Located (IdP pass))
482 -- See Note [Located RdrNames] in HsExpr
483 | KindedTyVar
484 (XKindedTyVar pass)
485 (Located (IdP pass))
486 (LHsKind pass) -- The user-supplied kind signature
487 -- ^
488 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
489 -- 'ApiAnnotation.AnnDcolon', 'ApiAnnotation.AnnClose'
490
491 -- For details on above see note [Api annotations] in ApiAnnotation
492
493 | XTyVarBndr
494 (XXTyVarBndr pass)
495
496 type instance XUserTyVar (GhcPass _) = NoExt
497 type instance XKindedTyVar (GhcPass _) = NoExt
498 type instance XXTyVarBndr (GhcPass _) = NoExt
499
500 -- | Does this 'HsTyVarBndr' come with an explicit kind annotation?
501 isHsKindedTyVar :: HsTyVarBndr pass -> Bool
502 isHsKindedTyVar (UserTyVar {}) = False
503 isHsKindedTyVar (KindedTyVar {}) = True
504 isHsKindedTyVar (XTyVarBndr{}) = panic "isHsKindedTyVar"
505
506 -- | Do all type variables in this 'LHsQTyVars' come with kind annotations?
507 hsTvbAllKinded :: LHsQTyVars pass -> Bool
508 hsTvbAllKinded = all (isHsKindedTyVar . unLoc) . hsQTvExplicit
509
510 -- | Haskell Type
511 data HsType pass
512 = HsForAllTy -- See Note [HsType binders]
513 { hst_xforall :: XForAllTy pass
514 , hst_fvf :: ForallVisFlag -- Is this `forall a -> {...}` or
515 -- `forall a. {...}`?
516 , hst_bndrs :: [LHsTyVarBndr pass]
517 -- Explicit, user-supplied 'forall a b c'
518 , hst_body :: LHsType pass -- body type
519 }
520 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnForall',
521 -- 'ApiAnnotation.AnnDot','ApiAnnotation.AnnDarrow'
522 -- For details on above see note [Api annotations] in ApiAnnotation
523
524 | HsQualTy -- See Note [HsType binders]
525 { hst_xqual :: XQualTy pass
526 , hst_ctxt :: LHsContext pass -- Context C => blah
527 , hst_body :: LHsType pass }
528
529 | HsTyVar (XTyVar pass)
530 PromotionFlag -- Whether explicitly promoted,
531 -- for the pretty printer
532 (Located (IdP pass))
533 -- Type variable, type constructor, or data constructor
534 -- see Note [Promotions (HsTyVar)]
535 -- See Note [Located RdrNames] in HsExpr
536 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
537
538 -- For details on above see note [Api annotations] in ApiAnnotation
539
540 | HsAppTy (XAppTy pass)
541 (LHsType pass)
542 (LHsType pass)
543 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
544
545 -- For details on above see note [Api annotations] in ApiAnnotation
546
547 | HsAppKindTy (XAppKindTy pass) -- type level type app
548 (LHsType pass)
549 (LHsKind pass)
550
551 | HsFunTy (XFunTy pass)
552 (LHsType pass) -- function type
553 (LHsType pass)
554 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow',
555
556 -- For details on above see note [Api annotations] in ApiAnnotation
557
558 | HsListTy (XListTy pass)
559 (LHsType pass) -- Element type
560 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
561 -- 'ApiAnnotation.AnnClose' @']'@
562
563 -- For details on above see note [Api annotations] in ApiAnnotation
564
565 | HsTupleTy (XTupleTy pass)
566 HsTupleSort
567 [LHsType pass] -- Element types (length gives arity)
568 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(' or '(#'@,
569 -- 'ApiAnnotation.AnnClose' @')' or '#)'@
570
571 -- For details on above see note [Api annotations] in ApiAnnotation
572
573 | HsSumTy (XSumTy pass)
574 [LHsType pass] -- Element types (length gives arity)
575 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(#'@,
576 -- 'ApiAnnotation.AnnClose' '#)'@
577
578 -- For details on above see note [Api annotations] in ApiAnnotation
579
580 | HsOpTy (XOpTy pass)
581 (LHsType pass) (Located (IdP pass)) (LHsType pass)
582 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
583
584 -- For details on above see note [Api annotations] in ApiAnnotation
585
586 | HsParTy (XParTy pass)
587 (LHsType pass) -- See Note [Parens in HsSyn] in HsExpr
588 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
589 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
590 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
591 -- 'ApiAnnotation.AnnClose' @')'@
592
593 -- For details on above see note [Api annotations] in ApiAnnotation
594
595 | HsIParamTy (XIParamTy pass)
596 (Located HsIPName) -- (?x :: ty)
597 (LHsType pass) -- Implicit parameters as they occur in
598 -- contexts
599 -- ^
600 -- > (?x :: ty)
601 --
602 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
603
604 -- For details on above see note [Api annotations] in ApiAnnotation
605
606 | HsStarTy (XStarTy pass)
607 Bool -- Is this the Unicode variant?
608 -- Note [HsStarTy]
609 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
610
611 | HsKindSig (XKindSig pass)
612 (LHsType pass) -- (ty :: kind)
613 (LHsKind pass) -- A type with a kind signature
614 -- ^
615 -- > (ty :: kind)
616 --
617 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
618 -- 'ApiAnnotation.AnnDcolon','ApiAnnotation.AnnClose' @')'@
619
620 -- For details on above see note [Api annotations] in ApiAnnotation
621
622 | HsSpliceTy (XSpliceTy pass)
623 (HsSplice pass) -- Includes quasi-quotes
624 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'$('@,
625 -- 'ApiAnnotation.AnnClose' @')'@
626
627 -- For details on above see note [Api annotations] in ApiAnnotation
628
629 | HsDocTy (XDocTy pass)
630 (LHsType pass) LHsDocString -- A documented type
631 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
632
633 -- For details on above see note [Api annotations] in ApiAnnotation
634
635 | HsBangTy (XBangTy pass)
636 HsSrcBang (LHsType pass) -- Bang-style type annotations
637 -- ^ - 'ApiAnnotation.AnnKeywordId' :
638 -- 'ApiAnnotation.AnnOpen' @'{-\# UNPACK' or '{-\# NOUNPACK'@,
639 -- 'ApiAnnotation.AnnClose' @'#-}'@
640 -- 'ApiAnnotation.AnnBang' @\'!\'@
641
642 -- For details on above see note [Api annotations] in ApiAnnotation
643
644 | HsRecTy (XRecTy pass)
645 [LConDeclField pass] -- Only in data type declarations
646 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
647 -- 'ApiAnnotation.AnnClose' @'}'@
648
649 -- For details on above see note [Api annotations] in ApiAnnotation
650
651 -- | HsCoreTy (XCoreTy pass) Type -- An escape hatch for tunnelling a *closed*
652 -- -- Core Type through HsSyn.
653 -- -- ^ - 'ApiAnnotation.AnnKeywordId' : None
654
655 -- For details on above see note [Api annotations] in ApiAnnotation
656
657 | HsExplicitListTy -- A promoted explicit list
658 (XExplicitListTy pass)
659 PromotionFlag -- whether explcitly promoted, for pretty printer
660 [LHsType pass]
661 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'["@,
662 -- 'ApiAnnotation.AnnClose' @']'@
663
664 -- For details on above see note [Api annotations] in ApiAnnotation
665
666 | HsExplicitTupleTy -- A promoted explicit tuple
667 (XExplicitTupleTy pass)
668 [LHsType pass]
669 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'("@,
670 -- 'ApiAnnotation.AnnClose' @')'@
671
672 -- For details on above see note [Api annotations] in ApiAnnotation
673
674 | HsTyLit (XTyLit pass) HsTyLit -- A promoted numeric literal.
675 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
676
677 -- For details on above see note [Api annotations] in ApiAnnotation
678
679 | HsWildCardTy (XWildCardTy pass) -- A type wildcard
680 -- See Note [The wildcard story for types]
681 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
682
683 -- For details on above see note [Api annotations] in ApiAnnotation
684
685 -- For adding new constructors via Trees that Grow
686 | XHsType
687 (XXType pass)
688
689 data NewHsTypeX
690 = NHsCoreTy Type -- An escape hatch for tunnelling a *closed*
691 -- Core Type through HsSyn.
692 deriving Data
693 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
694
695 instance Outputable NewHsTypeX where
696 ppr (NHsCoreTy ty) = ppr ty
697
698 type instance XForAllTy (GhcPass _) = NoExt
699 type instance XQualTy (GhcPass _) = NoExt
700 type instance XTyVar (GhcPass _) = NoExt
701 type instance XAppTy (GhcPass _) = NoExt
702 type instance XFunTy (GhcPass _) = NoExt
703 type instance XListTy (GhcPass _) = NoExt
704 type instance XTupleTy (GhcPass _) = NoExt
705 type instance XSumTy (GhcPass _) = NoExt
706 type instance XOpTy (GhcPass _) = NoExt
707 type instance XParTy (GhcPass _) = NoExt
708 type instance XIParamTy (GhcPass _) = NoExt
709 type instance XStarTy (GhcPass _) = NoExt
710 type instance XKindSig (GhcPass _) = NoExt
711
712 type instance XAppKindTy (GhcPass _) = SrcSpan -- Where the `@` lives
713
714 type instance XSpliceTy GhcPs = NoExt
715 type instance XSpliceTy GhcRn = NoExt
716 type instance XSpliceTy GhcTc = Kind
717
718 type instance XDocTy (GhcPass _) = NoExt
719 type instance XBangTy (GhcPass _) = NoExt
720 type instance XRecTy (GhcPass _) = NoExt
721
722 type instance XExplicitListTy GhcPs = NoExt
723 type instance XExplicitListTy GhcRn = NoExt
724 type instance XExplicitListTy GhcTc = Kind
725
726 type instance XExplicitTupleTy GhcPs = NoExt
727 type instance XExplicitTupleTy GhcRn = NoExt
728 type instance XExplicitTupleTy GhcTc = [Kind]
729
730 type instance XTyLit (GhcPass _) = NoExt
731
732 type instance XWildCardTy (GhcPass _) = NoExt
733
734 type instance XXType (GhcPass _) = NewHsTypeX
735
736
737 -- Note [Literal source text] in BasicTypes for SourceText fields in
738 -- the following
739 -- | Haskell Type Literal
740 data HsTyLit
741 = HsNumTy SourceText Integer
742 | HsStrTy SourceText FastString
743 deriving Data
744
745
746 {-
747 Note [HsForAllTy tyvar binders]
748 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
749 After parsing:
750 * Implicit => empty
751 Explicit => the variables the user wrote
752
753 After renaming
754 * Implicit => the *type* variables free in the type
755 Explicit => the variables the user wrote (renamed)
756
757 Qualified currently behaves exactly as Implicit,
758 but it is deprecated to use it for implicit quantification.
759 In this case, GHC 7.10 gives a warning; see
760 Note [Context quantification] in RnTypes, and #4426.
761 In GHC 8.0, Qualified will no longer bind variables
762 and this will become an error.
763
764 The kind variables bound in the hsq_implicit field come both
765 a) from the kind signatures on the kind vars (eg k1)
766 b) from the scope of the forall (eg k2)
767 Example: f :: forall (a::k1) b. T a (b::k2)
768
769
770 Note [Unit tuples]
771 ~~~~~~~~~~~~~~~~~~
772 Consider the type
773 type instance F Int = ()
774 We want to parse that "()"
775 as HsTupleTy HsBoxedOrConstraintTuple [],
776 NOT as HsTyVar unitTyCon
777
778 Why? Because F might have kind (* -> Constraint), so we when parsing we
779 don't know if that tuple is going to be a constraint tuple or an ordinary
780 unit tuple. The HsTupleSort flag is specifically designed to deal with
781 that, but it has to work for unit tuples too.
782
783 Note [Promotions (HsTyVar)]
784 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
785 HsTyVar: A name in a type or kind.
786 Here are the allowed namespaces for the name.
787 In a type:
788 Var: not allowed
789 Data: promoted data constructor
790 Tv: type variable
791 TcCls before renamer: type constructor, class constructor, or promoted data constructor
792 TcCls after renamer: type constructor or class constructor
793 In a kind:
794 Var, Data: not allowed
795 Tv: kind variable
796 TcCls: kind constructor or promoted type constructor
797
798 The 'Promoted' field in an HsTyVar captures whether the type was promoted in
799 the source code by prefixing an apostrophe.
800
801 Note [HsStarTy]
802 ~~~~~~~~~~~~~~~
803 When the StarIsType extension is enabled, we want to treat '*' and its Unicode
804 variant identically to 'Data.Kind.Type'. Unfortunately, doing so in the parser
805 would mean that when we pretty-print it back, we don't know whether the user
806 wrote '*' or 'Type', and lose the parse/ppr roundtrip property.
807
808 As a workaround, we parse '*' as HsStarTy (if it stands for 'Data.Kind.Type')
809 and then desugar it to 'Data.Kind.Type' in the typechecker (see tc_hs_type).
810 When '*' is a regular type operator (StarIsType is disabled), HsStarTy is not
811 involved.
812
813
814 Note [Promoted lists and tuples]
815 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
816 Notice the difference between
817 HsListTy HsExplicitListTy
818 HsTupleTy HsExplicitListTupleTy
819
820 E.g. f :: [Int] HsListTy
821
822 g3 :: T '[] All these use
823 g2 :: T '[True] HsExplicitListTy
824 g1 :: T '[True,False]
825 g1a :: T [True,False] (can omit ' where unambiguous)
826
827 kind of T :: [Bool] -> * This kind uses HsListTy!
828
829 E.g. h :: (Int,Bool) HsTupleTy; f is a pair
830 k :: S '(True,False) HsExplicitTypleTy; S is indexed by
831 a type-level pair of booleans
832 kind of S :: (Bool,Bool) -> * This kind uses HsExplicitTupleTy
833
834 Note [Distinguishing tuple kinds]
835 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
836
837 Apart from promotion, tuples can have one of three different kinds:
838
839 x :: (Int, Bool) -- Regular boxed tuples
840 f :: Int# -> (# Int#, Int# #) -- Unboxed tuples
841 g :: (Eq a, Ord a) => a -- Constraint tuples
842
843 For convenience, internally we use a single constructor for all of these,
844 namely HsTupleTy, but keep track of the tuple kind (in the first argument to
845 HsTupleTy, a HsTupleSort). We can tell if a tuple is unboxed while parsing,
846 because of the #. However, with -XConstraintKinds we can only distinguish
847 between constraint and boxed tuples during type checking, in general. Hence the
848 four constructors of HsTupleSort:
849
850 HsUnboxedTuple -> Produced by the parser
851 HsBoxedTuple -> Certainly a boxed tuple
852 HsConstraintTuple -> Certainly a constraint tuple
853 HsBoxedOrConstraintTuple -> Could be a boxed or a constraint
854 tuple. Produced by the parser only,
855 disappears after type checking
856 -}
857
858 -- | Haskell Tuple Sort
859 data HsTupleSort = HsUnboxedTuple
860 | HsBoxedTuple
861 | HsConstraintTuple
862 | HsBoxedOrConstraintTuple
863 deriving Data
864
865 -- | Located Constructor Declaration Field
866 type LConDeclField pass = Located (ConDeclField pass)
867 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
868 -- in a list
869
870 -- For details on above see note [Api annotations] in ApiAnnotation
871
872 -- | Constructor Declaration Field
873 data ConDeclField pass -- Record fields have Haddoc docs on them
874 = ConDeclField { cd_fld_ext :: XConDeclField pass,
875 cd_fld_names :: [LFieldOcc pass],
876 -- ^ See Note [ConDeclField passs]
877 cd_fld_type :: LBangType pass,
878 cd_fld_doc :: Maybe LHsDocString }
879 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
880
881 -- For details on above see note [Api annotations] in ApiAnnotation
882 | XConDeclField (XXConDeclField pass)
883
884 type instance XConDeclField (GhcPass _) = NoExt
885 type instance XXConDeclField (GhcPass _) = NoExt
886
887 instance (p ~ GhcPass pass, OutputableBndrId p)
888 => Outputable (ConDeclField p) where
889 ppr (ConDeclField _ fld_n fld_ty _) = ppr fld_n <+> dcolon <+> ppr fld_ty
890 ppr (XConDeclField x) = ppr x
891
892 -- HsConDetails is used for patterns/expressions *and* for data type
893 -- declarations
894 -- | Haskell Constructor Details
895 data HsConDetails arg rec
896 = PrefixCon [arg] -- C p1 p2 p3
897 | RecCon rec -- C { x = p1, y = p2 }
898 | InfixCon arg arg -- p1 `C` p2
899 deriving Data
900
901 instance (Outputable arg, Outputable rec)
902 => Outputable (HsConDetails arg rec) where
903 ppr (PrefixCon args) = text "PrefixCon" <+> ppr args
904 ppr (RecCon rec) = text "RecCon:" <+> ppr rec
905 ppr (InfixCon l r) = text "InfixCon:" <+> ppr [l, r]
906
907 {-
908 Note [ConDeclField passs]
909 ~~~~~~~~~~~~~~~~~~~~~~~~~
910
911 A ConDeclField contains a list of field occurrences: these always
912 include the field label as the user wrote it. After the renamer, it
913 will additionally contain the identity of the selector function in the
914 second component.
915
916 Due to DuplicateRecordFields, the OccName of the selector function
917 may have been mangled, which is why we keep the original field label
918 separately. For example, when DuplicateRecordFields is enabled
919
920 data T = MkT { x :: Int }
921
922 gives
923
924 ConDeclField { cd_fld_names = [L _ (FieldOcc "x" $sel:x:MkT)], ... }.
925 -}
926
927 -----------------------
928 -- A valid type must have a for-all at the top of the type, or of the fn arg
929 -- types
930
931 ---------------------
932 hsWcScopedTvs :: LHsSigWcType GhcRn -> [Name]
933 -- Get the lexically-scoped type variables of a HsSigType
934 -- - the explicitly-given forall'd type variables
935 -- - the implicitly-bound kind variables
936 -- - the named wildcars; see Note [Scoping of named wildcards]
937 -- because they scope in the same way
938 hsWcScopedTvs sig_ty
939 | HsWC { hswc_ext = nwcs, hswc_body = sig_ty1 } <- sig_ty
940 , HsIB { hsib_ext = vars
941 , hsib_body = sig_ty2 } <- sig_ty1
942 = case sig_ty2 of
943 L _ (HsForAllTy { hst_bndrs = tvs }) -> vars ++ nwcs ++
944 hsLTyVarNames tvs
945 -- include kind variables only if the type is headed by forall
946 -- (this is consistent with GHC 7 behaviour)
947 _ -> nwcs
948 hsWcScopedTvs (HsWC _ (XHsImplicitBndrs _)) = panic "hsWcScopedTvs"
949 hsWcScopedTvs (XHsWildCardBndrs _) = panic "hsWcScopedTvs"
950
951 hsScopedTvs :: LHsSigType GhcRn -> [Name]
952 -- Same as hsWcScopedTvs, but for a LHsSigType
953 hsScopedTvs sig_ty
954 | HsIB { hsib_ext = vars
955 , hsib_body = sig_ty2 } <- sig_ty
956 , L _ (HsForAllTy { hst_bndrs = tvs }) <- sig_ty2
957 = vars ++ hsLTyVarNames tvs
958 | otherwise
959 = []
960
961 {- Note [Scoping of named wildcards]
962 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
963 Consider
964 f :: _a -> _a
965 f x = let g :: _a -> _a
966 g = ...
967 in ...
968
969 Currently, for better or worse, the "_a" variables are all the same. So
970 although there is no explicit forall, the "_a" scopes over the definition.
971 I don't know if this is a good idea, but there it is.
972 -}
973
974 ---------------------
975 hsTyVarName :: HsTyVarBndr pass -> IdP pass
976 hsTyVarName (UserTyVar _ (L _ n)) = n
977 hsTyVarName (KindedTyVar _ (L _ n) _) = n
978 hsTyVarName (XTyVarBndr{}) = panic "hsTyVarName"
979
980 hsLTyVarName :: LHsTyVarBndr pass -> IdP pass
981 hsLTyVarName = hsTyVarName . unLoc
982
983 hsLTyVarNames :: [LHsTyVarBndr pass] -> [IdP pass]
984 hsLTyVarNames = map hsLTyVarName
985
986 hsExplicitLTyVarNames :: LHsQTyVars pass -> [IdP pass]
987 -- Explicit variables only
988 hsExplicitLTyVarNames qtvs = map hsLTyVarName (hsQTvExplicit qtvs)
989
990 hsAllLTyVarNames :: LHsQTyVars GhcRn -> [Name]
991 -- All variables
992 hsAllLTyVarNames (HsQTvs { hsq_ext = kvs
993 , hsq_explicit = tvs })
994 = kvs ++ hsLTyVarNames tvs
995 hsAllLTyVarNames (XLHsQTyVars _) = panic "hsAllLTyVarNames"
996
997 hsLTyVarLocName :: LHsTyVarBndr pass -> Located (IdP pass)
998 hsLTyVarLocName = onHasSrcSpan hsTyVarName
999
1000 hsLTyVarLocNames :: LHsQTyVars pass -> [Located (IdP pass)]
1001 hsLTyVarLocNames qtvs = map hsLTyVarLocName (hsQTvExplicit qtvs)
1002
1003 -- | Convert a LHsTyVarBndr to an equivalent LHsType.
1004 hsLTyVarBndrToType :: LHsTyVarBndr (GhcPass p) -> LHsType (GhcPass p)
1005 hsLTyVarBndrToType = onHasSrcSpan cvt
1006 where cvt (UserTyVar _ n) = HsTyVar noExt NotPromoted n
1007 cvt (KindedTyVar _ (L name_loc n) kind)
1008 = HsKindSig noExt
1009 (L name_loc (HsTyVar noExt NotPromoted (L name_loc n))) kind
1010 cvt (XTyVarBndr{}) = panic "hsLTyVarBndrToType"
1011
1012 -- | Convert a LHsTyVarBndrs to a list of types.
1013 -- Works on *type* variable only, no kind vars.
1014 hsLTyVarBndrsToTypes :: LHsQTyVars (GhcPass p) -> [LHsType (GhcPass p)]
1015 hsLTyVarBndrsToTypes (HsQTvs { hsq_explicit = tvbs }) = map hsLTyVarBndrToType tvbs
1016 hsLTyVarBndrsToTypes (XLHsQTyVars _) = panic "hsLTyVarBndrsToTypes"
1017
1018 ---------------------
1019 ignoreParens :: LHsType pass -> LHsType pass
1020 ignoreParens (L _ (HsParTy _ ty)) = ignoreParens ty
1021 ignoreParens ty = ty
1022
1023 isLHsForAllTy :: LHsType p -> Bool
1024 isLHsForAllTy (L _ (HsForAllTy {})) = True
1025 isLHsForAllTy _ = False
1026
1027 {-
1028 ************************************************************************
1029 * *
1030 Building types
1031 * *
1032 ************************************************************************
1033 -}
1034
1035 mkAnonWildCardTy :: HsType GhcPs
1036 mkAnonWildCardTy = HsWildCardTy noExt
1037
1038 mkHsOpTy :: LHsType (GhcPass p) -> Located (IdP (GhcPass p))
1039 -> LHsType (GhcPass p) -> HsType (GhcPass p)
1040 mkHsOpTy ty1 op ty2 = HsOpTy noExt ty1 op ty2
1041
1042 mkHsAppTy :: LHsType (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
1043 mkHsAppTy t1 t2
1044 = addCLoc t1 t2 (HsAppTy noExt t1 (parenthesizeHsType appPrec t2))
1045
1046 mkHsAppTys :: LHsType (GhcPass p) -> [LHsType (GhcPass p)]
1047 -> LHsType (GhcPass p)
1048 mkHsAppTys = foldl' mkHsAppTy
1049
1050 mkHsAppKindTy :: XAppKindTy (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
1051 -> LHsType (GhcPass p)
1052 mkHsAppKindTy ext ty k
1053 = addCLoc ty k (HsAppKindTy ext ty k)
1054
1055 {-
1056 ************************************************************************
1057 * *
1058 Decomposing HsTypes
1059 * *
1060 ************************************************************************
1061 -}
1062
1063 ---------------------------------
1064 -- splitHsFunType decomposes a type (t1 -> t2 ... -> tn)
1065 -- Breaks up any parens in the result type:
1066 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
1067 -- Also deals with (->) t1 t2; that is why it only works on LHsType Name
1068 -- (see #9096)
1069 splitHsFunType :: LHsType GhcRn -> ([LHsType GhcRn], LHsType GhcRn)
1070 splitHsFunType (L _ (HsParTy _ ty))
1071 = splitHsFunType ty
1072
1073 splitHsFunType (L _ (HsFunTy _ x y))
1074 | (args, res) <- splitHsFunType y
1075 = (x:args, res)
1076 {- This is not so correct, because it won't work with visible kind app, in case
1077 someone wants to write '(->) @k1 @k2 t1 t2'. Fixing this would require changing
1078 ConDeclGADT abstract syntax -}
1079 splitHsFunType orig_ty@(L _ (HsAppTy _ t1 t2))
1080 = go t1 [t2]
1081 where -- Look for (->) t1 t2, possibly with parenthesisation
1082 go (L _ (HsTyVar _ _ (L _ fn))) tys | fn == funTyConName
1083 , [t1,t2] <- tys
1084 , (args, res) <- splitHsFunType t2
1085 = (t1:args, res)
1086 go (L _ (HsAppTy _ t1 t2)) tys = go t1 (t2:tys)
1087 go (L _ (HsParTy _ ty)) tys = go ty tys
1088 go _ _ = ([], orig_ty) -- Failure to match
1089
1090 splitHsFunType other = ([], other)
1091
1092 -- retrieve the name of the "head" of a nested type application
1093 -- somewhat like splitHsAppTys, but a little more thorough
1094 -- used to examine the result of a GADT-like datacon, so it doesn't handle
1095 -- *all* cases (like lists, tuples, (~), etc.)
1096 hsTyGetAppHead_maybe :: LHsType (GhcPass p)
1097 -> Maybe (Located (IdP (GhcPass p)))
1098 hsTyGetAppHead_maybe = go
1099 where
1100 go (L _ (HsTyVar _ _ ln)) = Just ln
1101 go (L _ (HsAppTy _ l _)) = go l
1102 go (L _ (HsAppKindTy _ t _)) = go t
1103 go (L _ (HsOpTy _ _ (L loc n) _)) = Just (L loc n)
1104 go (L _ (HsParTy _ t)) = go t
1105 go (L _ (HsKindSig _ t _)) = go t
1106 go _ = Nothing
1107
1108 ------------------------------------------------------------
1109 -- Arguments in an expression/type after splitting
1110 data HsArg tm ty
1111 = HsValArg tm -- Argument is an ordinary expression (f arg)
1112 | HsTypeArg SrcSpan ty -- Argument is a visible type application (f @ty)
1113 -- SrcSpan is location of the `@`
1114 | HsArgPar SrcSpan -- See Note [HsArgPar]
1115
1116 numVisibleArgs :: [HsArg tm ty] -> Arity
1117 numVisibleArgs = count is_vis
1118 where is_vis (HsValArg _) = True
1119 is_vis _ = False
1120
1121 -- type level equivalent
1122 type LHsTypeArg p = HsArg (LHsType p) (LHsKind p)
1123
1124 instance (Outputable tm, Outputable ty) => Outputable (HsArg tm ty) where
1125 ppr (HsValArg tm) = ppr tm
1126 ppr (HsTypeArg _ ty) = char '@' <> ppr ty
1127 ppr (HsArgPar sp) = text "HsArgPar" <+> ppr sp
1128 {-
1129 Note [HsArgPar]
1130 A HsArgPar indicates that everything to the left of this in the argument list is
1131 enclosed in parentheses together with the function itself. It is necessary so
1132 that we can recreate the parenthesis structure in the original source after
1133 typechecking the arguments.
1134
1135 The SrcSpan is the span of the original HsPar
1136
1137 ((f arg1) arg2 arg3) results in an input argument list of
1138 [HsValArg arg1, HsArgPar span1, HsValArg arg2, HsValArg arg3, HsArgPar span2]
1139
1140 -}
1141
1142 --------------------------------
1143
1144 -- | Decompose a pattern synonym type signature into its constituent parts.
1145 --
1146 -- Note that this function looks through parentheses, so it will work on types
1147 -- such as @(forall a. <...>)@. The downside to this is that it is not
1148 -- generally possible to take the returned types and reconstruct the original
1149 -- type (parentheses and all) from them.
1150 splitLHsPatSynTy :: LHsType pass
1151 -> ( [LHsTyVarBndr pass] -- universals
1152 , LHsContext pass -- required constraints
1153 , [LHsTyVarBndr pass] -- existentials
1154 , LHsContext pass -- provided constraints
1155 , LHsType pass) -- body type
1156 splitLHsPatSynTy ty = (univs, reqs, exis, provs, ty4)
1157 where
1158 (univs, ty1) = splitLHsForAllTyInvis ty
1159 (reqs, ty2) = splitLHsQualTy ty1
1160 (exis, ty3) = splitLHsForAllTyInvis ty2
1161 (provs, ty4) = splitLHsQualTy ty3
1162
1163 -- | Decompose a sigma type (of the form @forall <tvs>. context => body@)
1164 -- into its constituent parts.
1165 --
1166 -- Note that this function looks through parentheses, so it will work on types
1167 -- such as @(forall a. <...>)@. The downside to this is that it is not
1168 -- generally possible to take the returned types and reconstruct the original
1169 -- type (parentheses and all) from them.
1170 splitLHsSigmaTy :: LHsType pass
1171 -> ([LHsTyVarBndr pass], LHsContext pass, LHsType pass)
1172 splitLHsSigmaTy ty
1173 | (tvs, ty1) <- splitLHsForAllTy ty
1174 , (ctxt, ty2) <- splitLHsQualTy ty1
1175 = (tvs, ctxt, ty2)
1176
1177 -- | Like 'splitLHsSigmaTy', but only splits type variable binders that were
1178 -- quantified invisibly (e.g., @forall a.@, with a dot).
1179 --
1180 -- This function is used to split apart certain types, such as instance
1181 -- declaration types, which disallow visible @forall@s. For instance, if GHC
1182 -- split apart the @forall@ in @instance forall a -> Show (Blah a)@, then that
1183 -- declaration would mistakenly be accepted!
1184 --
1185 -- Note that this function looks through parentheses, so it will work on types
1186 -- such as @(forall a. <...>)@. The downside to this is that it is not
1187 -- generally possible to take the returned types and reconstruct the original
1188 -- type (parentheses and all) from them.
1189 splitLHsSigmaTyInvis :: LHsType pass
1190 -> ([LHsTyVarBndr pass], LHsContext pass, LHsType pass)
1191 splitLHsSigmaTyInvis ty
1192 | (tvs, ty1) <- splitLHsForAllTyInvis ty
1193 , (ctxt, ty2) <- splitLHsQualTy ty1
1194 = (tvs, ctxt, ty2)
1195
1196 -- | Decompose a type of the form @forall <tvs>. body@) into its constituent
1197 -- parts.
1198 --
1199 -- Note that this function looks through parentheses, so it will work on types
1200 -- such as @(forall a. <...>)@. The downside to this is that it is not
1201 -- generally possible to take the returned types and reconstruct the original
1202 -- type (parentheses and all) from them.
1203 splitLHsForAllTy :: LHsType pass -> ([LHsTyVarBndr pass], LHsType pass)
1204 splitLHsForAllTy (L _ (HsParTy _ ty)) = splitLHsForAllTy ty
1205 splitLHsForAllTy (L _ (HsForAllTy { hst_bndrs = tvs, hst_body = body })) = (tvs, body)
1206 splitLHsForAllTy body = ([], body)
1207
1208 -- | Like 'splitLHsForAllTy', but only splits type variable binders that
1209 -- were quantified invisibly (e.g., @forall a.@, with a dot).
1210 --
1211 -- This function is used to split apart certain types, such as instance
1212 -- declaration types, which disallow visible @forall@s. For instance, if GHC
1213 -- split apart the @forall@ in @instance forall a -> Show (Blah a)@, then that
1214 -- declaration would mistakenly be accepted!
1215 --
1216 -- Note that this function looks through parentheses, so it will work on types
1217 -- such as @(forall a. <...>)@. The downside to this is that it is not
1218 -- generally possible to take the returned types and reconstruct the original
1219 -- type (parentheses and all) from them.
1220 splitLHsForAllTyInvis :: LHsType pass -> ([LHsTyVarBndr pass], LHsType pass)
1221 splitLHsForAllTyInvis lty@(L _ ty) =
1222 case ty of
1223 HsParTy _ ty' -> splitLHsForAllTyInvis ty'
1224 HsForAllTy { hst_fvf = fvf', hst_bndrs = tvs', hst_body = body' }
1225 | fvf' == ForallInvis
1226 -> (tvs', body')
1227 _ -> ([], lty)
1228
1229 -- | Decompose a type of the form @context => body@ into its constituent parts.
1230 --
1231 -- Note that this function looks through parentheses, so it will work on types
1232 -- such as @(context => <...>)@. The downside to this is that it is not
1233 -- generally possible to take the returned types and reconstruct the original
1234 -- type (parentheses and all) from them.
1235 splitLHsQualTy :: LHsType pass -> (LHsContext pass, LHsType pass)
1236 splitLHsQualTy (L _ (HsParTy _ ty)) = splitLHsQualTy ty
1237 splitLHsQualTy (L _ (HsQualTy { hst_ctxt = ctxt, hst_body = body })) = (ctxt, body)
1238 splitLHsQualTy body = (noLHsContext, body)
1239
1240 -- | Decompose a type class instance type (of the form
1241 -- @forall <tvs>. context => instance_head@) into its constituent parts.
1242 --
1243 -- Note that this function looks through parentheses, so it will work on types
1244 -- such as @(forall <tvs>. <...>)@. The downside to this is that it is not
1245 -- generally possible to take the returned types and reconstruct the original
1246 -- type (parentheses and all) from them.
1247 splitLHsInstDeclTy :: LHsSigType GhcRn
1248 -> ([Name], LHsContext GhcRn, LHsType GhcRn)
1249 -- Split up an instance decl type, returning the pieces
1250 splitLHsInstDeclTy (HsIB { hsib_ext = itkvs
1251 , hsib_body = inst_ty })
1252 | (tvs, cxt, body_ty) <- splitLHsSigmaTyInvis inst_ty
1253 = (itkvs ++ hsLTyVarNames tvs, cxt, body_ty)
1254 -- Return implicitly bound type and kind vars
1255 -- For an instance decl, all of them are in scope
1256 splitLHsInstDeclTy (XHsImplicitBndrs _) = panic "splitLHsInstDeclTy"
1257
1258 getLHsInstDeclHead :: LHsSigType pass -> LHsType pass
1259 getLHsInstDeclHead inst_ty
1260 | (_tvs, _cxt, body_ty) <- splitLHsSigmaTyInvis (hsSigType inst_ty)
1261 = body_ty
1262
1263 getLHsInstDeclClass_maybe :: LHsSigType (GhcPass p)
1264 -> Maybe (Located (IdP (GhcPass p)))
1265 -- Works on (HsSigType RdrName)
1266 getLHsInstDeclClass_maybe inst_ty
1267 = do { let head_ty = getLHsInstDeclHead inst_ty
1268 ; cls <- hsTyGetAppHead_maybe head_ty
1269 ; return cls }
1270
1271 {-
1272 ************************************************************************
1273 * *
1274 FieldOcc
1275 * *
1276 ************************************************************************
1277 -}
1278
1279 -- | Located Field Occurrence
1280 type LFieldOcc pass = Located (FieldOcc pass)
1281
1282 -- | Field Occurrence
1283 --
1284 -- Represents an *occurrence* of an unambiguous field. We store
1285 -- both the 'RdrName' the user originally wrote, and after the
1286 -- renamer, the selector function.
1287 data FieldOcc pass = FieldOcc { extFieldOcc :: XCFieldOcc pass
1288 , rdrNameFieldOcc :: Located RdrName
1289 -- ^ See Note [Located RdrNames] in HsExpr
1290 }
1291
1292 | XFieldOcc
1293 (XXFieldOcc pass)
1294 deriving instance (p ~ GhcPass pass, Eq (XCFieldOcc p)) => Eq (FieldOcc p)
1295 deriving instance (p ~ GhcPass pass, Ord (XCFieldOcc p)) => Ord (FieldOcc p)
1296
1297 type instance XCFieldOcc GhcPs = NoExt
1298 type instance XCFieldOcc GhcRn = Name
1299 type instance XCFieldOcc GhcTc = Id
1300
1301 type instance XXFieldOcc (GhcPass _) = NoExt
1302
1303 instance Outputable (FieldOcc pass) where
1304 ppr = ppr . rdrNameFieldOcc
1305
1306 mkFieldOcc :: Located RdrName -> FieldOcc GhcPs
1307 mkFieldOcc rdr = FieldOcc noExt rdr
1308
1309
1310 -- | Ambiguous Field Occurrence
1311 --
1312 -- Represents an *occurrence* of a field that is potentially
1313 -- ambiguous after the renamer, with the ambiguity resolved by the
1314 -- typechecker. We always store the 'RdrName' that the user
1315 -- originally wrote, and store the selector function after the renamer
1316 -- (for unambiguous occurrences) or the typechecker (for ambiguous
1317 -- occurrences).
1318 --
1319 -- See Note [HsRecField and HsRecUpdField] in HsPat and
1320 -- Note [Disambiguating record fields] in TcExpr.
1321 -- See Note [Located RdrNames] in HsExpr
1322 data AmbiguousFieldOcc pass
1323 = Unambiguous (XUnambiguous pass) (Located RdrName)
1324 | Ambiguous (XAmbiguous pass) (Located RdrName)
1325 | XAmbiguousFieldOcc (XXAmbiguousFieldOcc pass)
1326
1327 type instance XUnambiguous GhcPs = NoExt
1328 type instance XUnambiguous GhcRn = Name
1329 type instance XUnambiguous GhcTc = Id
1330
1331 type instance XAmbiguous GhcPs = NoExt
1332 type instance XAmbiguous GhcRn = NoExt
1333 type instance XAmbiguous GhcTc = Id
1334
1335 type instance XXAmbiguousFieldOcc (GhcPass _) = NoExt
1336
1337 instance p ~ GhcPass pass => Outputable (AmbiguousFieldOcc p) where
1338 ppr = ppr . rdrNameAmbiguousFieldOcc
1339
1340 instance p ~ GhcPass pass => OutputableBndr (AmbiguousFieldOcc p) where
1341 pprInfixOcc = pprInfixOcc . rdrNameAmbiguousFieldOcc
1342 pprPrefixOcc = pprPrefixOcc . rdrNameAmbiguousFieldOcc
1343
1344 mkAmbiguousFieldOcc :: Located RdrName -> AmbiguousFieldOcc GhcPs
1345 mkAmbiguousFieldOcc rdr = Unambiguous noExt rdr
1346
1347 rdrNameAmbiguousFieldOcc :: AmbiguousFieldOcc (GhcPass p) -> RdrName
1348 rdrNameAmbiguousFieldOcc (Unambiguous _ (L _ rdr)) = rdr
1349 rdrNameAmbiguousFieldOcc (Ambiguous _ (L _ rdr)) = rdr
1350 rdrNameAmbiguousFieldOcc (XAmbiguousFieldOcc _)
1351 = panic "rdrNameAmbiguousFieldOcc"
1352
1353 selectorAmbiguousFieldOcc :: AmbiguousFieldOcc GhcTc -> Id
1354 selectorAmbiguousFieldOcc (Unambiguous sel _) = sel
1355 selectorAmbiguousFieldOcc (Ambiguous sel _) = sel
1356 selectorAmbiguousFieldOcc (XAmbiguousFieldOcc _)
1357 = panic "selectorAmbiguousFieldOcc"
1358
1359 unambiguousFieldOcc :: AmbiguousFieldOcc GhcTc -> FieldOcc GhcTc
1360 unambiguousFieldOcc (Unambiguous rdr sel) = FieldOcc rdr sel
1361 unambiguousFieldOcc (Ambiguous rdr sel) = FieldOcc rdr sel
1362 unambiguousFieldOcc (XAmbiguousFieldOcc _) = panic "unambiguousFieldOcc"
1363
1364 ambiguousFieldOcc :: FieldOcc GhcTc -> AmbiguousFieldOcc GhcTc
1365 ambiguousFieldOcc (FieldOcc sel rdr) = Unambiguous sel rdr
1366 ambiguousFieldOcc (XFieldOcc _) = panic "ambiguousFieldOcc"
1367
1368 {-
1369 ************************************************************************
1370 * *
1371 \subsection{Pretty printing}
1372 * *
1373 ************************************************************************
1374 -}
1375
1376 instance (p ~ GhcPass pass, OutputableBndrId p) => Outputable (HsType p) where
1377 ppr ty = pprHsType ty
1378
1379 instance Outputable HsTyLit where
1380 ppr = ppr_tylit
1381
1382 instance (p ~ GhcPass pass, OutputableBndrId p)
1383 => Outputable (LHsQTyVars p) where
1384 ppr (HsQTvs { hsq_explicit = tvs }) = interppSP tvs
1385 ppr (XLHsQTyVars x) = ppr x
1386
1387 instance (p ~ GhcPass pass, OutputableBndrId p)
1388 => Outputable (HsTyVarBndr p) where
1389 ppr (UserTyVar _ n) = ppr n
1390 ppr (KindedTyVar _ n k) = parens $ hsep [ppr n, dcolon, ppr k]
1391 ppr (XTyVarBndr n) = ppr n
1392
1393 instance (p ~ GhcPass pass,Outputable thing)
1394 => Outputable (HsImplicitBndrs p thing) where
1395 ppr (HsIB { hsib_body = ty }) = ppr ty
1396 ppr (XHsImplicitBndrs x) = ppr x
1397
1398 instance (p ~ GhcPass pass,Outputable thing)
1399 => Outputable (HsWildCardBndrs p thing) where
1400 ppr (HsWC { hswc_body = ty }) = ppr ty
1401 ppr (XHsWildCardBndrs x) = ppr x
1402
1403 pprAnonWildCard :: SDoc
1404 pprAnonWildCard = char '_'
1405
1406 -- | Prints a forall; When passed an empty list, prints @forall .@/@forall ->@
1407 -- only when @-dppr-debug@ is enabled.
1408 pprHsForAll :: (OutputableBndrId (GhcPass p))
1409 => ForallVisFlag -> [LHsTyVarBndr (GhcPass p)]
1410 -> LHsContext (GhcPass p) -> SDoc
1411 pprHsForAll = pprHsForAllExtra Nothing
1412
1413 -- | Version of 'pprHsForAll' that can also print an extra-constraints
1414 -- wildcard, e.g. @_ => a -> Bool@ or @(Show a, _) => a -> String@. This
1415 -- underscore will be printed when the 'Maybe SrcSpan' argument is a 'Just'
1416 -- containing the location of the extra-constraints wildcard. A special
1417 -- function for this is needed, as the extra-constraints wildcard is removed
1418 -- from the actual context and type, and stored in a separate field, thus just
1419 -- printing the type will not print the extra-constraints wildcard.
1420 pprHsForAllExtra :: (OutputableBndrId (GhcPass p))
1421 => Maybe SrcSpan -> ForallVisFlag
1422 -> [LHsTyVarBndr (GhcPass p)]
1423 -> LHsContext (GhcPass p) -> SDoc
1424 pprHsForAllExtra extra fvf qtvs cxt
1425 = pp_forall <+> pprLHsContextExtra (isJust extra) cxt
1426 where
1427 pp_forall | null qtvs = whenPprDebug (forAllLit <> separator)
1428 | otherwise = forAllLit <+> interppSP qtvs <> separator
1429
1430 separator = ppr_forall_separator fvf
1431
1432 -- | Version of 'pprHsForAll' or 'pprHsForAllExtra' that will always print
1433 -- @forall.@ when passed @Just []@. Prints nothing if passed 'Nothing'
1434 pprHsExplicitForAll :: (OutputableBndrId (GhcPass p))
1435 => ForallVisFlag
1436 -> Maybe [LHsTyVarBndr (GhcPass p)] -> SDoc
1437 pprHsExplicitForAll fvf (Just qtvs) = forAllLit <+> interppSP qtvs
1438 <> ppr_forall_separator fvf
1439 pprHsExplicitForAll _ Nothing = empty
1440
1441 -- | Prints an arrow for visible @forall@s (e.g., @forall a ->@) and a dot for
1442 -- invisible @forall@s (e.g., @forall a.@).
1443 ppr_forall_separator :: ForallVisFlag -> SDoc
1444 ppr_forall_separator ForallVis = space <> arrow
1445 ppr_forall_separator ForallInvis = dot
1446
1447 pprLHsContext :: (OutputableBndrId (GhcPass p))
1448 => LHsContext (GhcPass p) -> SDoc
1449 pprLHsContext lctxt
1450 | null (unLoc lctxt) = empty
1451 | otherwise = pprLHsContextAlways lctxt
1452
1453 -- For use in a HsQualTy, which always gets printed if it exists.
1454 pprLHsContextAlways :: (OutputableBndrId (GhcPass p))
1455 => LHsContext (GhcPass p) -> SDoc
1456 pprLHsContextAlways (L _ ctxt)
1457 = case ctxt of
1458 [] -> parens empty <+> darrow
1459 [L _ ty] -> ppr_mono_ty ty <+> darrow
1460 _ -> parens (interpp'SP ctxt) <+> darrow
1461
1462 -- True <=> print an extra-constraints wildcard, e.g. @(Show a, _) =>@
1463 pprLHsContextExtra :: (OutputableBndrId (GhcPass p))
1464 => Bool -> LHsContext (GhcPass p) -> SDoc
1465 pprLHsContextExtra show_extra lctxt@(L _ ctxt)
1466 | not show_extra = pprLHsContext lctxt
1467 | null ctxt = char '_' <+> darrow
1468 | otherwise = parens (sep (punctuate comma ctxt')) <+> darrow
1469 where
1470 ctxt' = map ppr ctxt ++ [char '_']
1471
1472 pprConDeclFields :: (OutputableBndrId (GhcPass p))
1473 => [LConDeclField (GhcPass p)] -> SDoc
1474 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
1475 where
1476 ppr_fld (L _ (ConDeclField { cd_fld_names = ns, cd_fld_type = ty,
1477 cd_fld_doc = doc }))
1478 = ppr_names ns <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
1479 ppr_fld (L _ (XConDeclField x)) = ppr x
1480 ppr_names [n] = ppr n
1481 ppr_names ns = sep (punctuate comma (map ppr ns))
1482
1483 {-
1484 Note [Printing KindedTyVars]
1485 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1486 #3830 reminded me that we should really only print the kind
1487 signature on a KindedTyVar if the kind signature was put there by the
1488 programmer. During kind inference GHC now adds a PostTcKind to UserTyVars,
1489 rather than converting to KindedTyVars as before.
1490
1491 (As it happens, the message in #3830 comes out a different way now,
1492 and the problem doesn't show up; but having the flag on a KindedTyVar
1493 seems like the Right Thing anyway.)
1494 -}
1495
1496 -- Printing works more-or-less as for Types
1497
1498 pprHsType :: (OutputableBndrId (GhcPass p)) => HsType (GhcPass p) -> SDoc
1499 pprHsType ty = ppr_mono_ty ty
1500
1501 ppr_mono_lty :: (OutputableBndrId (GhcPass p)) => LHsType (GhcPass p) -> SDoc
1502 ppr_mono_lty ty = ppr_mono_ty (unLoc ty)
1503
1504 ppr_mono_ty :: (OutputableBndrId (GhcPass p)) => HsType (GhcPass p) -> SDoc
1505 ppr_mono_ty (HsForAllTy { hst_fvf = fvf, hst_bndrs = tvs, hst_body = ty })
1506 = sep [pprHsForAll fvf tvs noLHsContext, ppr_mono_lty ty]
1507
1508 ppr_mono_ty (HsQualTy { hst_ctxt = ctxt, hst_body = ty })
1509 = sep [pprLHsContextAlways ctxt, ppr_mono_lty ty]
1510
1511 ppr_mono_ty (HsBangTy _ b ty) = ppr b <> ppr_mono_lty ty
1512 ppr_mono_ty (HsRecTy _ flds) = pprConDeclFields flds
1513 ppr_mono_ty (HsTyVar _ prom (L _ name))
1514 | isPromoted prom = quote (pprPrefixOcc name)
1515 | otherwise = pprPrefixOcc name
1516 ppr_mono_ty (HsFunTy _ ty1 ty2) = ppr_fun_ty ty1 ty2
1517 ppr_mono_ty (HsTupleTy _ con tys) = tupleParens std_con (pprWithCommas ppr tys)
1518 where std_con = case con of
1519 HsUnboxedTuple -> UnboxedTuple
1520 _ -> BoxedTuple
1521 ppr_mono_ty (HsSumTy _ tys)
1522 = tupleParens UnboxedTuple (pprWithBars ppr tys)
1523 ppr_mono_ty (HsKindSig _ ty kind)
1524 = ppr_mono_lty ty <+> dcolon <+> ppr kind
1525 ppr_mono_ty (HsListTy _ ty) = brackets (ppr_mono_lty ty)
1526 ppr_mono_ty (HsIParamTy _ n ty) = (ppr n <+> dcolon <+> ppr_mono_lty ty)
1527 ppr_mono_ty (HsSpliceTy _ s) = pprSplice s
1528 ppr_mono_ty (HsExplicitListTy _ prom tys)
1529 | isPromoted prom = quote $ brackets (maybeAddSpace tys $ interpp'SP tys)
1530 | otherwise = brackets (interpp'SP tys)
1531 ppr_mono_ty (HsExplicitTupleTy _ tys)
1532 = quote $ parens (maybeAddSpace tys $ interpp'SP tys)
1533 ppr_mono_ty (HsTyLit _ t) = ppr_tylit t
1534 ppr_mono_ty (HsWildCardTy {}) = char '_'
1535
1536 ppr_mono_ty (HsStarTy _ isUni) = char (if isUni then '' else '*')
1537
1538 ppr_mono_ty (HsAppTy _ fun_ty arg_ty)
1539 = hsep [ppr_mono_lty fun_ty, ppr_mono_lty arg_ty]
1540 ppr_mono_ty (HsAppKindTy _ ty k)
1541 = ppr_mono_lty ty <+> char '@' <> ppr_mono_lty k
1542 ppr_mono_ty (HsOpTy _ ty1 (L _ op) ty2)
1543 = sep [ ppr_mono_lty ty1
1544 , sep [pprInfixOcc op, ppr_mono_lty ty2 ] ]
1545
1546 ppr_mono_ty (HsParTy _ ty)
1547 = parens (ppr_mono_lty ty)
1548 -- Put the parens in where the user did
1549 -- But we still use the precedence stuff to add parens because
1550 -- toHsType doesn't put in any HsParTys, so we may still need them
1551
1552 ppr_mono_ty (HsDocTy _ ty doc)
1553 -- AZ: Should we add parens? Should we introduce "-- ^"?
1554 = ppr_mono_lty ty <+> ppr (unLoc doc)
1555 -- we pretty print Haddock comments on types as if they were
1556 -- postfix operators
1557
1558 ppr_mono_ty (XHsType t) = ppr t
1559
1560 --------------------------
1561 ppr_fun_ty :: (OutputableBndrId (GhcPass p))
1562 => LHsType (GhcPass p) -> LHsType (GhcPass p) -> SDoc
1563 ppr_fun_ty ty1 ty2
1564 = let p1 = ppr_mono_lty ty1
1565 p2 = ppr_mono_lty ty2
1566 in
1567 sep [p1, arrow <+> p2]
1568
1569 --------------------------
1570 ppr_tylit :: HsTyLit -> SDoc
1571 ppr_tylit (HsNumTy _ i) = integer i
1572 ppr_tylit (HsStrTy _ s) = text (show s)
1573
1574
1575 -- | @'hsTypeNeedsParens' p t@ returns 'True' if the type @t@ needs parentheses
1576 -- under precedence @p@.
1577 hsTypeNeedsParens :: PprPrec -> HsType pass -> Bool
1578 hsTypeNeedsParens p = go
1579 where
1580 go (HsForAllTy{}) = p >= funPrec
1581 go (HsQualTy{}) = p >= funPrec
1582 go (HsBangTy{}) = p > topPrec
1583 go (HsRecTy{}) = False
1584 go (HsTyVar{}) = False
1585 go (HsFunTy{}) = p >= funPrec
1586 go (HsTupleTy{}) = False
1587 go (HsSumTy{}) = False
1588 go (HsKindSig{}) = p >= sigPrec
1589 go (HsListTy{}) = False
1590 go (HsIParamTy{}) = p > topPrec
1591 go (HsSpliceTy{}) = False
1592 go (HsExplicitListTy{}) = False
1593 go (HsExplicitTupleTy{}) = False
1594 go (HsTyLit{}) = False
1595 go (HsWildCardTy{}) = False
1596 go (HsStarTy{}) = False
1597 go (HsAppTy{}) = p >= appPrec
1598 go (HsAppKindTy{}) = p >= appPrec
1599 go (HsOpTy{}) = p >= opPrec
1600 go (HsParTy{}) = False
1601 go (HsDocTy _ (L _ t) _) = go t
1602 go (XHsType{}) = False
1603
1604 maybeAddSpace :: [LHsType pass] -> SDoc -> SDoc
1605 -- See Note [Printing promoted type constructors]
1606 -- in IfaceType. This code implements the same
1607 -- logic for printing HsType
1608 maybeAddSpace tys doc
1609 | (ty : _) <- tys
1610 , lhsTypeHasLeadingPromotionQuote ty = space <> doc
1611 | otherwise = doc
1612
1613 lhsTypeHasLeadingPromotionQuote :: LHsType pass -> Bool
1614 lhsTypeHasLeadingPromotionQuote ty
1615 = goL ty
1616 where
1617 goL (L _ ty) = go ty
1618
1619 go (HsForAllTy{}) = False
1620 go (HsQualTy{ hst_ctxt = ctxt, hst_body = body})
1621 | L _ (c:_) <- ctxt = goL c
1622 | otherwise = goL body
1623 go (HsBangTy{}) = False
1624 go (HsRecTy{}) = False
1625 go (HsTyVar _ p _) = isPromoted p
1626 go (HsFunTy _ arg _) = goL arg
1627 go (HsListTy{}) = False
1628 go (HsTupleTy{}) = False
1629 go (HsSumTy{}) = False
1630 go (HsOpTy _ t1 _ _) = goL t1
1631 go (HsKindSig _ t _) = goL t
1632 go (HsIParamTy{}) = False
1633 go (HsSpliceTy{}) = False
1634 go (HsExplicitListTy _ p _) = isPromoted p
1635 go (HsExplicitTupleTy{}) = True
1636 go (HsTyLit{}) = False
1637 go (HsWildCardTy{}) = False
1638 go (HsStarTy{}) = False
1639 go (HsAppTy _ t _) = goL t
1640 go (HsAppKindTy _ t _) = goL t
1641 go (HsParTy{}) = False
1642 go (HsDocTy _ t _) = goL t
1643 go (XHsType{}) = False
1644
1645 -- | @'parenthesizeHsType' p ty@ checks if @'hsTypeNeedsParens' p ty@ is
1646 -- true, and if so, surrounds @ty@ with an 'HsParTy'. Otherwise, it simply
1647 -- returns @ty@.
1648 parenthesizeHsType :: PprPrec -> LHsType (GhcPass p) -> LHsType (GhcPass p)
1649 parenthesizeHsType p lty@(L loc ty)
1650 | hsTypeNeedsParens p ty = L loc (HsParTy NoExt lty)
1651 | otherwise = lty
1652
1653 -- | @'parenthesizeHsContext' p ctxt@ checks if @ctxt@ is a single constraint
1654 -- @c@ such that @'hsTypeNeedsParens' p c@ is true, and if so, surrounds @c@
1655 -- with an 'HsParTy' to form a parenthesized @ctxt@. Otherwise, it simply
1656 -- returns @ctxt@ unchanged.
1657 parenthesizeHsContext :: PprPrec
1658 -> LHsContext (GhcPass p) -> LHsContext (GhcPass p)
1659 parenthesizeHsContext p lctxt@(L loc ctxt) =
1660 case ctxt of
1661 [c] -> L loc [parenthesizeHsType p c]
1662 _ -> lctxt -- Other contexts are already "parenthesized" by virtue of
1663 -- being tuples.