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