Make a smart mkAppTyM
[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, hsTyGetAppHead_maybe,
61 mkHsOpTy, mkHsAppTy, mkHsAppTys, mkHsAppKindTy,
62 ignoreParens, hsSigType, hsSigWcType,
63 hsLTyVarBndrToType, hsLTyVarBndrsToTypes,
64
65 -- Printing
66 pprHsType, pprHsForAll, pprHsForAllExtra, pprHsExplicitForAll,
67 pprLHsContext,
68 hsTypeNeedsParens, parenthesizeHsType, parenthesizeHsContext
69 ) where
70
71 import GhcPrelude
72
73 import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
74
75 import HsExtension
76 import HsLit () -- for instances
77
78 import Id ( Id )
79 import Name( Name )
80 import RdrName ( RdrName )
81 import NameSet ( NameSet, emptyNameSet )
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. ...
146 HsQualTy represents an /explicit, user-written/ context
147 e.g. (Eq a, Show a) => ...
148 The context can be empty if that's what the user wrote
149 These constructors represent what the user wrote, no more
150 and no less.
151
152 * HsTyVarBndr describes a quantified type variable written by the
153 user. For example
154 f :: forall a (b :: *). blah
155 here 'a' and '(b::*)' are each a HsTyVarBndr. A HsForAllTy has
156 a list of LHsTyVarBndrs.
157
158 * HsImplicitBndrs is a wrapper that gives the implicitly-quantified
159 kind and type variables of the wrapped thing. It is filled in by
160 the renamer. For example, if the user writes
161 f :: a -> a
162 the HsImplicitBinders binds the 'a' (not a HsForAllTy!).
163 NB: this implicit quantification is purely lexical: we bind any
164 type or kind variables that are not in scope. The type checker
165 may subsequently quantify over further kind variables.
166
167 * HsWildCardBndrs is a wrapper that binds the wildcard variables
168 of the wrapped thing. It is filled in by the renamer
169 f :: _a -> _
170 The enclosing HsWildCardBndrs binds the wildcards _a and _.
171
172 * The explicit presence of these wrappers specifies, in the HsSyn,
173 exactly where implicit quantification is allowed, and where
174 wildcards are allowed.
175
176 * LHsQTyVars is used in data/class declarations, where the user gives
177 explicit *type* variable bindings, but we need to implicitly bind
178 *kind* variables. For example
179 class C (a :: k -> *) where ...
180 The 'k' is implicitly bound in the hsq_tvs field of LHsQTyVars
181
182 Note [The wildcard story for types]
183 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184 Types can have wildcards in them, to support partial type signatures,
185 like f :: Int -> (_ , _a) -> _a
186
187 A wildcard in a type can be
188
189 * An anonymous wildcard,
190 written '_'
191 In HsType this is represented by HsWildCardTy.
192 The renamer leaves it untouched, and it is later given fresh meta tyvars in
193 the typechecker.
194
195 * A named wildcard,
196 written '_a', '_foo', etc
197 In HsType this is represented by (HsTyVar "_a")
198 i.e. a perfectly ordinary type variable that happens
199 to start with an underscore
200
201 Note carefully:
202
203 * When NamedWildCards is off, type variables that start with an
204 underscore really /are/ ordinary type variables. And indeed, even
205 when NamedWildCards is on you can bind _a explicitly as an ordinary
206 type variable:
207 data T _a _b = MkT _b _a
208 Or even:
209 f :: forall _a. _a -> _b
210 Here _a is an ordinary forall'd binder, but (With NamedWildCards)
211 _b is a named wildcard. (See the comments in Trac #10982)
212
213 * Named wildcards are bound by the HsWildCardBndrs construct, which wraps
214 types that are allowed to have wildcards. Unnamed wildcards however are left
215 unchanged until typechecking, where we give them fresh wild tyavrs and
216 determine whether or not to emit hole constraints on each wildcard
217 (we don't if it's a visible type/kind argument or a type family pattern).
218 See related notes Note [Wildcards in visible kind application]
219 and Note [Wildcards in visible type application] in TcHsType.hs
220
221 * After type checking is done, we report what types the wildcards
222 got unified with.
223
224 Note [Ordering of implicit variables]
225 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226 Since the advent of -XTypeApplications, GHC makes promises about the ordering
227 of implicit variable quantification. Specifically, we offer that implicitly
228 quantified variables (such as those in const :: a -> b -> a, without a `forall`)
229 will occur in left-to-right order of first occurrence. Here are a few examples:
230
231 const :: a -> b -> a -- forall a b. ...
232 f :: Eq a => b -> a -> a -- forall a b. ... contexts are included
233
234 type a <-< b = b -> a
235 g :: a <-< b -- forall a b. ... type synonyms matter
236
237 class Functor f where
238 fmap :: (a -> b) -> f a -> f b -- forall f a b. ...
239 -- The f is quantified by the class, so only a and b are considered in fmap
240
241 This simple story is complicated by the possibility of dependency: all variables
242 must come after any variables mentioned in their kinds.
243
244 typeRep :: Typeable a => TypeRep (a :: k) -- forall k a. ...
245
246 The k comes first because a depends on k, even though the k appears later than
247 the a in the code. Thus, GHC does a *stable topological sort* on the variables.
248 By "stable", we mean that any two variables who do not depend on each other
249 preserve their existing left-to-right ordering.
250
251 Implicitly bound variables are collected by the extract- family of functions
252 (extractHsTysRdrTyVars, extractHsTyVarBndrsKVs, etc.) in RnTypes.
253 These functions thus promise to keep left-to-right ordering.
254 Look for pointers to this note to see the places where the action happens.
255
256 Note that we also maintain this ordering in kind signatures. Even though
257 there's no visible kind application (yet), having implicit variables be
258 quantified in left-to-right order in kind signatures is nice since:
259
260 * It's consistent with the treatment for type signatures.
261 * It can affect how types are displayed with -fprint-explicit-kinds (see
262 #15568 for an example), which is a situation where knowing the order in
263 which implicit variables are quantified can be useful.
264 * In the event that visible kind application is implemented, the order in
265 which we would expect implicit variables to be ordered in kinds will have
266 already been established.
267 -}
268
269 -- | Located Haskell Context
270 type LHsContext pass = Located (HsContext pass)
271 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnUnit'
272 -- For details on above see note [Api annotations] in ApiAnnotation
273
274 noLHsContext :: LHsContext pass
275 -- Use this when there is no context in the original program
276 -- It would really be more kosher to use a Maybe, to distinguish
277 -- class () => C a where ...
278 -- from
279 -- class C a where ...
280 noLHsContext = noLoc []
281
282 -- | Haskell Context
283 type HsContext pass = [LHsType pass]
284
285 -- | Located Haskell Type
286 type LHsType pass = Located (HsType pass)
287 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
288 -- in a list
289
290 -- For details on above see note [Api annotations] in ApiAnnotation
291
292 -- | Haskell Kind
293 type HsKind pass = HsType pass
294
295 -- | Located Haskell Kind
296 type LHsKind pass = Located (HsKind pass)
297 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
298
299 -- For details on above see note [Api annotations] in ApiAnnotation
300
301 --------------------------------------------------
302 -- LHsQTyVars
303 -- The explicitly-quantified binders in a data/type declaration
304
305 -- | Located Haskell Type Variable Binder
306 type LHsTyVarBndr pass = Located (HsTyVarBndr pass)
307 -- See Note [HsType binders]
308
309 -- | Located Haskell Quantified Type Variables
310 data LHsQTyVars pass -- See Note [HsType binders]
311 = HsQTvs { hsq_ext :: XHsQTvs pass
312
313 , hsq_explicit :: [LHsTyVarBndr pass]
314 -- Explicit variables, written by the user
315 -- See Note [HsForAllTy tyvar binders]
316 }
317 | XLHsQTyVars (XXLHsQTyVars pass)
318
319 data HsQTvsRn
320 = HsQTvsRn
321 { hsq_implicit :: [Name]
322 -- Implicit (dependent) variables
323
324 , hsq_dependent :: NameSet
325 -- Which members of hsq_explicit are dependent; that is,
326 -- mentioned in the kind of a later hsq_explicit,
327 -- or mentioned in a kind in the scope of this HsQTvs
328 -- See Note [Dependent LHsQTyVars] in TcHsType
329 } deriving Data
330
331 type instance XHsQTvs GhcPs = NoExt
332 type instance XHsQTvs GhcRn = HsQTvsRn
333 type instance XHsQTvs GhcTc = HsQTvsRn
334
335 type instance XXLHsQTyVars (GhcPass _) = NoExt
336
337 mkHsQTvs :: [LHsTyVarBndr GhcPs] -> LHsQTyVars GhcPs
338 mkHsQTvs tvs = HsQTvs { hsq_ext = noExt, hsq_explicit = tvs }
339
340 hsQTvExplicit :: LHsQTyVars pass -> [LHsTyVarBndr pass]
341 hsQTvExplicit = hsq_explicit
342
343 emptyLHsQTvs :: LHsQTyVars GhcRn
344 emptyLHsQTvs = HsQTvs (HsQTvsRn [] emptyNameSet) []
345
346 isEmptyLHsQTvs :: LHsQTyVars GhcRn -> Bool
347 isEmptyLHsQTvs (HsQTvs (HsQTvsRn [] _) []) = True
348 isEmptyLHsQTvs _ = False
349
350 ------------------------------------------------
351 -- HsImplicitBndrs
352 -- Used to quantify the implicit binders of a type
353 -- * Implicit binders of a type signature (LHsSigType/LHsSigWcType)
354 -- * Patterns in a type/data family instance (HsTyPats)
355
356 -- | Haskell Implicit Binders
357 data HsImplicitBndrs pass thing -- See Note [HsType binders]
358 = HsIB { hsib_ext :: XHsIB pass thing -- after renamer: [Name]
359 -- Implicitly-bound kind & type vars
360 -- Order is important; see
361 -- Note [Ordering of implicit variables]
362 -- in RnTypes
363
364 , hsib_body :: thing -- Main payload (type or list of types)
365 }
366 | XHsImplicitBndrs (XXHsImplicitBndrs pass thing)
367
368 type instance XHsIB GhcPs _ = NoExt
369 type instance XHsIB GhcRn _ = [Name]
370 type instance XHsIB GhcTc _ = [Name]
371
372 type instance XXHsImplicitBndrs (GhcPass _) _ = NoExt
373
374 -- | Haskell Wildcard Binders
375 data HsWildCardBndrs pass thing
376 -- See Note [HsType binders]
377 -- See Note [The wildcard story for types]
378 = HsWC { hswc_ext :: XHsWC pass thing
379 -- after the renamer
380 -- Wild cards, only named
381 -- See Note [Wildcards in visible kind application]
382
383 , hswc_body :: thing
384 -- Main payload (type or list of types)
385 -- If there is an extra-constraints wildcard,
386 -- it's still there in the hsc_body.
387 }
388 | XHsWildCardBndrs (XXHsWildCardBndrs pass thing)
389
390 type instance XHsWC GhcPs b = NoExt
391 type instance XHsWC GhcRn b = [Name]
392 type instance XHsWC GhcTc b = [Name]
393
394 type instance XXHsWildCardBndrs (GhcPass _) b = NoExt
395
396 -- | Located Haskell Signature Type
397 type LHsSigType pass = HsImplicitBndrs pass (LHsType pass) -- Implicit only
398
399 -- | Located Haskell Wildcard Type
400 type LHsWcType pass = HsWildCardBndrs pass (LHsType pass) -- Wildcard only
401
402 -- | Located Haskell Signature Wildcard Type
403 type LHsSigWcType pass = HsWildCardBndrs pass (LHsSigType pass) -- Both
404
405 -- See Note [Representing type signatures]
406
407 hsImplicitBody :: HsImplicitBndrs pass thing -> thing
408 hsImplicitBody (HsIB { hsib_body = body }) = body
409 hsImplicitBody (XHsImplicitBndrs _) = panic "hsImplicitBody"
410
411 hsSigType :: LHsSigType pass -> LHsType pass
412 hsSigType = hsImplicitBody
413
414 hsSigWcType :: LHsSigWcType pass -> LHsType pass
415 hsSigWcType sig_ty = hsib_body (hswc_body sig_ty)
416
417 dropWildCards :: LHsSigWcType pass -> LHsSigType pass
418 -- Drop the wildcard part of a LHsSigWcType
419 dropWildCards sig_ty = hswc_body sig_ty
420
421 {- Note [Representing type signatures]
422 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
423 HsSigType is used to represent an explicit user type signature
424 such as f :: a -> a
425 or g (x :: a -> a) = x
426
427 A HsSigType is just a HsImplicitBndrs wrapping a LHsType.
428 * The HsImplicitBndrs binds the /implicitly/ quantified tyvars
429 * The LHsType binds the /explicitly/ quantified tyvars
430
431 E.g. For a signature like
432 f :: forall (a::k). blah
433 we get
434 HsIB { hsib_vars = [k]
435 , hsib_body = HsForAllTy { hst_bndrs = [(a::*)]
436 , hst_body = blah }
437 The implicit kind variable 'k' is bound by the HsIB;
438 the explicitly forall'd tyvar 'a' is bound by the HsForAllTy
439 -}
440
441 mkHsImplicitBndrs :: thing -> HsImplicitBndrs GhcPs thing
442 mkHsImplicitBndrs x = HsIB { hsib_ext = noExt
443 , hsib_body = x }
444
445 mkHsWildCardBndrs :: thing -> HsWildCardBndrs GhcPs thing
446 mkHsWildCardBndrs x = HsWC { hswc_body = x
447 , hswc_ext = noExt }
448
449 -- Add empty binders. This is a bit suspicious; what if
450 -- the wrapped thing had free type variables?
451 mkEmptyImplicitBndrs :: thing -> HsImplicitBndrs GhcRn thing
452 mkEmptyImplicitBndrs x = HsIB { hsib_ext = []
453 , hsib_body = x }
454
455 mkEmptyWildCardBndrs :: thing -> HsWildCardBndrs GhcRn thing
456 mkEmptyWildCardBndrs x = HsWC { hswc_body = x
457 , hswc_ext = [] }
458
459
460 --------------------------------------------------
461 -- | These names are used early on to store the names of implicit
462 -- parameters. They completely disappear after type-checking.
463 newtype HsIPName = HsIPName FastString
464 deriving( Eq, Data )
465
466 hsIPNameFS :: HsIPName -> FastString
467 hsIPNameFS (HsIPName n) = n
468
469 instance Outputable HsIPName where
470 ppr (HsIPName n) = char '?' <> ftext n -- Ordinary implicit parameters
471
472 instance OutputableBndr HsIPName where
473 pprBndr _ n = ppr n -- Simple for now
474 pprInfixOcc n = ppr n
475 pprPrefixOcc n = ppr n
476
477 --------------------------------------------------
478
479 -- | Haskell Type Variable Binder
480 data HsTyVarBndr pass
481 = UserTyVar -- no explicit kinding
482 (XUserTyVar pass)
483 (Located (IdP pass))
484 -- See Note [Located RdrNames] in HsExpr
485 | KindedTyVar
486 (XKindedTyVar pass)
487 (Located (IdP pass))
488 (LHsKind pass) -- The user-supplied kind signature
489 -- ^
490 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
491 -- 'ApiAnnotation.AnnDcolon', 'ApiAnnotation.AnnClose'
492
493 -- For details on above see note [Api annotations] in ApiAnnotation
494
495 | XTyVarBndr
496 (XXTyVarBndr pass)
497
498 type instance XUserTyVar (GhcPass _) = NoExt
499 type instance XKindedTyVar (GhcPass _) = NoExt
500 type instance XXTyVarBndr (GhcPass _) = NoExt
501
502 -- | Does this 'HsTyVarBndr' come with an explicit kind annotation?
503 isHsKindedTyVar :: HsTyVarBndr pass -> Bool
504 isHsKindedTyVar (UserTyVar {}) = False
505 isHsKindedTyVar (KindedTyVar {}) = True
506 isHsKindedTyVar (XTyVarBndr{}) = panic "isHsKindedTyVar"
507
508 -- | Do all type variables in this 'LHsQTyVars' come with kind annotations?
509 hsTvbAllKinded :: LHsQTyVars pass -> Bool
510 hsTvbAllKinded = all (isHsKindedTyVar . unLoc) . hsQTvExplicit
511
512 -- | Haskell Type
513 data HsType pass
514 = HsForAllTy -- See Note [HsType binders]
515 { hst_xforall :: XForAllTy pass,
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 Trac #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 map hsLTyVarName 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 ++ map hsLTyVarName 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 hsExplicitLTyVarNames :: LHsQTyVars pass -> [IdP pass]
984 -- Explicit variables only
985 hsExplicitLTyVarNames qtvs = map hsLTyVarName (hsQTvExplicit qtvs)
986
987 hsAllLTyVarNames :: LHsQTyVars GhcRn -> [Name]
988 -- All variables
989 hsAllLTyVarNames (HsQTvs { hsq_ext = HsQTvsRn { hsq_implicit = kvs }
990 , hsq_explicit = tvs })
991 = kvs ++ map hsLTyVarName tvs
992 hsAllLTyVarNames (XLHsQTyVars _) = panic "hsAllLTyVarNames"
993
994 hsLTyVarLocName :: LHsTyVarBndr pass -> Located (IdP pass)
995 hsLTyVarLocName = onHasSrcSpan hsTyVarName
996
997 hsLTyVarLocNames :: LHsQTyVars pass -> [Located (IdP pass)]
998 hsLTyVarLocNames qtvs = map hsLTyVarLocName (hsQTvExplicit qtvs)
999
1000 -- | Convert a LHsTyVarBndr to an equivalent LHsType.
1001 hsLTyVarBndrToType :: LHsTyVarBndr (GhcPass p) -> LHsType (GhcPass p)
1002 hsLTyVarBndrToType = onHasSrcSpan cvt
1003 where cvt (UserTyVar _ n) = HsTyVar noExt NotPromoted n
1004 cvt (KindedTyVar _ (L name_loc n) kind)
1005 = HsKindSig noExt
1006 (L name_loc (HsTyVar noExt NotPromoted (L name_loc n))) kind
1007 cvt (XTyVarBndr{}) = panic "hsLTyVarBndrToType"
1008
1009 -- | Convert a LHsTyVarBndrs to a list of types.
1010 -- Works on *type* variable only, no kind vars.
1011 hsLTyVarBndrsToTypes :: LHsQTyVars (GhcPass p) -> [LHsType (GhcPass p)]
1012 hsLTyVarBndrsToTypes (HsQTvs { hsq_explicit = tvbs }) = map hsLTyVarBndrToType tvbs
1013 hsLTyVarBndrsToTypes (XLHsQTyVars _) = panic "hsLTyVarBndrsToTypes"
1014
1015 ---------------------
1016 ignoreParens :: LHsType pass -> LHsType pass
1017 ignoreParens (L _ (HsParTy _ ty)) = ignoreParens ty
1018 ignoreParens ty = ty
1019
1020 isLHsForAllTy :: LHsType p -> Bool
1021 isLHsForAllTy (L _ (HsForAllTy {})) = True
1022 isLHsForAllTy _ = False
1023
1024 {-
1025 ************************************************************************
1026 * *
1027 Building types
1028 * *
1029 ************************************************************************
1030 -}
1031
1032 mkAnonWildCardTy :: HsType GhcPs
1033 mkAnonWildCardTy = HsWildCardTy noExt
1034
1035 mkHsOpTy :: LHsType (GhcPass p) -> Located (IdP (GhcPass p))
1036 -> LHsType (GhcPass p) -> HsType (GhcPass p)
1037 mkHsOpTy ty1 op ty2 = HsOpTy noExt ty1 op ty2
1038
1039 mkHsAppTy :: LHsType (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
1040 mkHsAppTy t1 t2
1041 = addCLoc t1 t2 (HsAppTy noExt t1 (parenthesizeHsType appPrec t2))
1042
1043 mkHsAppTys :: LHsType (GhcPass p) -> [LHsType (GhcPass p)]
1044 -> LHsType (GhcPass p)
1045 mkHsAppTys = foldl' mkHsAppTy
1046
1047 mkHsAppKindTy :: XAppKindTy (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
1048 -> LHsType (GhcPass p)
1049 mkHsAppKindTy ext ty k
1050 = addCLoc ty k (HsAppKindTy ext ty k)
1051
1052 {-
1053 ************************************************************************
1054 * *
1055 Decomposing HsTypes
1056 * *
1057 ************************************************************************
1058 -}
1059
1060 ---------------------------------
1061 -- splitHsFunType decomposes a type (t1 -> t2 ... -> tn)
1062 -- Breaks up any parens in the result type:
1063 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
1064 -- Also deals with (->) t1 t2; that is why it only works on LHsType Name
1065 -- (see Trac #9096)
1066 splitHsFunType :: LHsType GhcRn -> ([LHsType GhcRn], LHsType GhcRn)
1067 splitHsFunType (L _ (HsParTy _ ty))
1068 = splitHsFunType ty
1069
1070 splitHsFunType (L _ (HsFunTy _ x y))
1071 | (args, res) <- splitHsFunType y
1072 = (x:args, res)
1073 {- This is not so correct, because it won't work with visible kind app, in case
1074 someone wants to write '(->) @k1 @k2 t1 t2'. Fixing this would require changing
1075 ConDeclGADT abstract syntax -}
1076 splitHsFunType orig_ty@(L _ (HsAppTy _ t1 t2))
1077 = go t1 [t2]
1078 where -- Look for (->) t1 t2, possibly with parenthesisation
1079 go (L _ (HsTyVar _ _ (L _ fn))) tys | fn == funTyConName
1080 , [t1,t2] <- tys
1081 , (args, res) <- splitHsFunType t2
1082 = (t1:args, res)
1083 go (L _ (HsAppTy _ t1 t2)) tys = go t1 (t2:tys)
1084 go (L _ (HsParTy _ ty)) tys = go ty tys
1085 go _ _ = ([], orig_ty) -- Failure to match
1086
1087 splitHsFunType other = ([], other)
1088
1089 -- retrieve the name of the "head" of a nested type application
1090 -- somewhat like splitHsAppTys, but a little more thorough
1091 -- used to examine the result of a GADT-like datacon, so it doesn't handle
1092 -- *all* cases (like lists, tuples, (~), etc.)
1093 hsTyGetAppHead_maybe :: LHsType (GhcPass p)
1094 -> Maybe (Located (IdP (GhcPass p)))
1095 hsTyGetAppHead_maybe = go
1096 where
1097 go (L _ (HsTyVar _ _ ln)) = Just ln
1098 go (L _ (HsAppTy _ l _)) = go l
1099 go (L _ (HsAppKindTy _ t _)) = go t
1100 go (L _ (HsOpTy _ _ (L loc n) _)) = Just (L loc n)
1101 go (L _ (HsParTy _ t)) = go t
1102 go (L _ (HsKindSig _ t _)) = go t
1103 go _ = Nothing
1104
1105 ------------------------------------------------------------
1106 -- Arguments in an expression/type after splitting
1107 data HsArg tm ty
1108 = HsValArg tm -- Argument is an ordinary expression (f arg)
1109 | HsTypeArg SrcSpan ty -- Argument is a visible type application (f @ty)
1110 -- SrcSpan is location of the `@`
1111 | HsArgPar SrcSpan -- See Note [HsArgPar]
1112
1113 numVisibleArgs :: [HsArg tm ty] -> Arity
1114 numVisibleArgs = count is_vis
1115 where is_vis (HsValArg _) = True
1116 is_vis _ = False
1117
1118 -- type level equivalent
1119 type LHsTypeArg p = HsArg (LHsType p) (LHsKind p)
1120
1121 instance (Outputable tm, Outputable ty) => Outputable (HsArg tm ty) where
1122 ppr (HsValArg tm) = ppr tm
1123 ppr (HsTypeArg _ ty) = char '@' <> ppr ty
1124 ppr (HsArgPar sp) = text "HsArgPar" <+> ppr sp
1125 {-
1126 Note [HsArgPar]
1127 A HsArgPar indicates that everything to the left of this in the argument list is
1128 enclosed in parentheses together with the function itself. It is necessary so
1129 that we can recreate the parenthesis structure in the original source after
1130 typechecking the arguments.
1131
1132 The SrcSpan is the span of the original HsPar
1133
1134 ((f arg1) arg2 arg3) results in an input argument list of
1135 [HsValArg arg1, HsArgPar span1, HsValArg arg2, HsValArg arg3, HsArgPar span2]
1136
1137 -}
1138
1139 --------------------------------
1140 splitLHsPatSynTy :: LHsType pass
1141 -> ( [LHsTyVarBndr pass] -- universals
1142 , LHsContext pass -- required constraints
1143 , [LHsTyVarBndr pass] -- existentials
1144 , LHsContext pass -- provided constraints
1145 , LHsType pass) -- body type
1146 splitLHsPatSynTy ty = (univs, reqs, exis, provs, ty4)
1147 where
1148 (univs, ty1) = splitLHsForAllTy ty
1149 (reqs, ty2) = splitLHsQualTy ty1
1150 (exis, ty3) = splitLHsForAllTy ty2
1151 (provs, ty4) = splitLHsQualTy ty3
1152
1153 splitLHsSigmaTy :: LHsType pass
1154 -> ([LHsTyVarBndr pass], LHsContext pass, LHsType pass)
1155 splitLHsSigmaTy ty
1156 | (tvs, ty1) <- splitLHsForAllTy ty
1157 , (ctxt, ty2) <- splitLHsQualTy ty1
1158 = (tvs, ctxt, ty2)
1159
1160 splitLHsForAllTy :: LHsType pass -> ([LHsTyVarBndr pass], LHsType pass)
1161 splitLHsForAllTy (L _ (HsParTy _ ty)) = splitLHsForAllTy ty
1162 splitLHsForAllTy (L _ (HsForAllTy { hst_bndrs = tvs, hst_body = body })) = (tvs, body)
1163 splitLHsForAllTy body = ([], body)
1164
1165 splitLHsQualTy :: LHsType pass -> (LHsContext pass, LHsType pass)
1166 splitLHsQualTy (L _ (HsParTy _ ty)) = splitLHsQualTy ty
1167 splitLHsQualTy (L _ (HsQualTy { hst_ctxt = ctxt, hst_body = body })) = (ctxt, body)
1168 splitLHsQualTy body = (noLHsContext, body)
1169
1170 splitLHsInstDeclTy :: LHsSigType GhcRn
1171 -> ([Name], LHsContext GhcRn, LHsType GhcRn)
1172 -- Split up an instance decl type, returning the pieces
1173 splitLHsInstDeclTy (HsIB { hsib_ext = itkvs
1174 , hsib_body = inst_ty })
1175 | (tvs, cxt, body_ty) <- splitLHsSigmaTy inst_ty
1176 = (itkvs ++ map hsLTyVarName tvs, cxt, body_ty)
1177 -- Return implicitly bound type and kind vars
1178 -- For an instance decl, all of them are in scope
1179 splitLHsInstDeclTy (XHsImplicitBndrs _) = panic "splitLHsInstDeclTy"
1180
1181 getLHsInstDeclHead :: LHsSigType pass -> LHsType pass
1182 getLHsInstDeclHead inst_ty
1183 | (_tvs, _cxt, body_ty) <- splitLHsSigmaTy (hsSigType inst_ty)
1184 = body_ty
1185
1186 getLHsInstDeclClass_maybe :: LHsSigType (GhcPass p)
1187 -> Maybe (Located (IdP (GhcPass p)))
1188 -- Works on (HsSigType RdrName)
1189 getLHsInstDeclClass_maybe inst_ty
1190 = do { let head_ty = getLHsInstDeclHead inst_ty
1191 ; cls <- hsTyGetAppHead_maybe head_ty
1192 ; return cls }
1193
1194 {-
1195 ************************************************************************
1196 * *
1197 FieldOcc
1198 * *
1199 ************************************************************************
1200 -}
1201
1202 -- | Located Field Occurrence
1203 type LFieldOcc pass = Located (FieldOcc pass)
1204
1205 -- | Field Occurrence
1206 --
1207 -- Represents an *occurrence* of an unambiguous field. We store
1208 -- both the 'RdrName' the user originally wrote, and after the
1209 -- renamer, the selector function.
1210 data FieldOcc pass = FieldOcc { extFieldOcc :: XCFieldOcc pass
1211 , rdrNameFieldOcc :: Located RdrName
1212 -- ^ See Note [Located RdrNames] in HsExpr
1213 }
1214
1215 | XFieldOcc
1216 (XXFieldOcc pass)
1217 deriving instance (p ~ GhcPass pass, Eq (XCFieldOcc p)) => Eq (FieldOcc p)
1218 deriving instance (p ~ GhcPass pass, Ord (XCFieldOcc p)) => Ord (FieldOcc p)
1219
1220 type instance XCFieldOcc GhcPs = NoExt
1221 type instance XCFieldOcc GhcRn = Name
1222 type instance XCFieldOcc GhcTc = Id
1223
1224 type instance XXFieldOcc (GhcPass _) = NoExt
1225
1226 instance Outputable (FieldOcc pass) where
1227 ppr = ppr . rdrNameFieldOcc
1228
1229 mkFieldOcc :: Located RdrName -> FieldOcc GhcPs
1230 mkFieldOcc rdr = FieldOcc noExt rdr
1231
1232
1233 -- | Ambiguous Field Occurrence
1234 --
1235 -- Represents an *occurrence* of a field that is potentially
1236 -- ambiguous after the renamer, with the ambiguity resolved by the
1237 -- typechecker. We always store the 'RdrName' that the user
1238 -- originally wrote, and store the selector function after the renamer
1239 -- (for unambiguous occurrences) or the typechecker (for ambiguous
1240 -- occurrences).
1241 --
1242 -- See Note [HsRecField and HsRecUpdField] in HsPat and
1243 -- Note [Disambiguating record fields] in TcExpr.
1244 -- See Note [Located RdrNames] in HsExpr
1245 data AmbiguousFieldOcc pass
1246 = Unambiguous (XUnambiguous pass) (Located RdrName)
1247 | Ambiguous (XAmbiguous pass) (Located RdrName)
1248 | XAmbiguousFieldOcc (XXAmbiguousFieldOcc pass)
1249
1250 type instance XUnambiguous GhcPs = NoExt
1251 type instance XUnambiguous GhcRn = Name
1252 type instance XUnambiguous GhcTc = Id
1253
1254 type instance XAmbiguous GhcPs = NoExt
1255 type instance XAmbiguous GhcRn = NoExt
1256 type instance XAmbiguous GhcTc = Id
1257
1258 type instance XXAmbiguousFieldOcc (GhcPass _) = NoExt
1259
1260 instance p ~ GhcPass pass => Outputable (AmbiguousFieldOcc p) where
1261 ppr = ppr . rdrNameAmbiguousFieldOcc
1262
1263 instance p ~ GhcPass pass => OutputableBndr (AmbiguousFieldOcc p) where
1264 pprInfixOcc = pprInfixOcc . rdrNameAmbiguousFieldOcc
1265 pprPrefixOcc = pprPrefixOcc . rdrNameAmbiguousFieldOcc
1266
1267 mkAmbiguousFieldOcc :: Located RdrName -> AmbiguousFieldOcc GhcPs
1268 mkAmbiguousFieldOcc rdr = Unambiguous noExt rdr
1269
1270 rdrNameAmbiguousFieldOcc :: AmbiguousFieldOcc (GhcPass p) -> RdrName
1271 rdrNameAmbiguousFieldOcc (Unambiguous _ (L _ rdr)) = rdr
1272 rdrNameAmbiguousFieldOcc (Ambiguous _ (L _ rdr)) = rdr
1273 rdrNameAmbiguousFieldOcc (XAmbiguousFieldOcc _)
1274 = panic "rdrNameAmbiguousFieldOcc"
1275
1276 selectorAmbiguousFieldOcc :: AmbiguousFieldOcc GhcTc -> Id
1277 selectorAmbiguousFieldOcc (Unambiguous sel _) = sel
1278 selectorAmbiguousFieldOcc (Ambiguous sel _) = sel
1279 selectorAmbiguousFieldOcc (XAmbiguousFieldOcc _)
1280 = panic "selectorAmbiguousFieldOcc"
1281
1282 unambiguousFieldOcc :: AmbiguousFieldOcc GhcTc -> FieldOcc GhcTc
1283 unambiguousFieldOcc (Unambiguous rdr sel) = FieldOcc rdr sel
1284 unambiguousFieldOcc (Ambiguous rdr sel) = FieldOcc rdr sel
1285 unambiguousFieldOcc (XAmbiguousFieldOcc _) = panic "unambiguousFieldOcc"
1286
1287 ambiguousFieldOcc :: FieldOcc GhcTc -> AmbiguousFieldOcc GhcTc
1288 ambiguousFieldOcc (FieldOcc sel rdr) = Unambiguous sel rdr
1289 ambiguousFieldOcc (XFieldOcc _) = panic "ambiguousFieldOcc"
1290
1291 {-
1292 ************************************************************************
1293 * *
1294 \subsection{Pretty printing}
1295 * *
1296 ************************************************************************
1297 -}
1298
1299 instance (p ~ GhcPass pass, OutputableBndrId p) => Outputable (HsType p) where
1300 ppr ty = pprHsType ty
1301
1302 instance Outputable HsTyLit where
1303 ppr = ppr_tylit
1304
1305 instance (p ~ GhcPass pass, OutputableBndrId p)
1306 => Outputable (LHsQTyVars p) where
1307 ppr (HsQTvs { hsq_explicit = tvs }) = interppSP tvs
1308 ppr (XLHsQTyVars x) = ppr x
1309
1310 instance (p ~ GhcPass pass, OutputableBndrId p)
1311 => Outputable (HsTyVarBndr p) where
1312 ppr (UserTyVar _ n) = ppr n
1313 ppr (KindedTyVar _ n k) = parens $ hsep [ppr n, dcolon, ppr k]
1314 ppr (XTyVarBndr n) = ppr n
1315
1316 instance (p ~ GhcPass pass,Outputable thing)
1317 => Outputable (HsImplicitBndrs p thing) where
1318 ppr (HsIB { hsib_body = ty }) = ppr ty
1319 ppr (XHsImplicitBndrs x) = ppr x
1320
1321 instance (p ~ GhcPass pass,Outputable thing)
1322 => Outputable (HsWildCardBndrs p thing) where
1323 ppr (HsWC { hswc_body = ty }) = ppr ty
1324 ppr (XHsWildCardBndrs x) = ppr x
1325
1326 pprAnonWildCard :: SDoc
1327 pprAnonWildCard = char '_'
1328
1329 -- | Prints a forall; When passed an empty list, prints @forall.@ only when
1330 -- @-dppr-debug@
1331 pprHsForAll :: (OutputableBndrId (GhcPass p))
1332 => [LHsTyVarBndr (GhcPass p)] -> LHsContext (GhcPass p) -> SDoc
1333 pprHsForAll = pprHsForAllExtra Nothing
1334
1335 -- | Version of 'pprHsForAll' that can also print an extra-constraints
1336 -- wildcard, e.g. @_ => a -> Bool@ or @(Show a, _) => a -> String@. This
1337 -- underscore will be printed when the 'Maybe SrcSpan' argument is a 'Just'
1338 -- containing the location of the extra-constraints wildcard. A special
1339 -- function for this is needed, as the extra-constraints wildcard is removed
1340 -- from the actual context and type, and stored in a separate field, thus just
1341 -- printing the type will not print the extra-constraints wildcard.
1342 pprHsForAllExtra :: (OutputableBndrId (GhcPass p))
1343 => Maybe SrcSpan -> [LHsTyVarBndr (GhcPass p)]
1344 -> LHsContext (GhcPass p) -> SDoc
1345 pprHsForAllExtra extra qtvs cxt
1346 = pp_forall <+> pprLHsContextExtra (isJust extra) cxt
1347 where
1348 pp_forall | null qtvs = whenPprDebug (forAllLit <> dot)
1349 | otherwise = forAllLit <+> interppSP qtvs <> dot
1350
1351 -- | Version of 'pprHsForall' or 'pprHsForallExtra' that will always print
1352 -- @forall.@ when passed @Just []@. Prints nothing if passed 'Nothing'
1353 pprHsExplicitForAll :: (OutputableBndrId (GhcPass p))
1354 => Maybe [LHsTyVarBndr (GhcPass p)] -> SDoc
1355 pprHsExplicitForAll (Just qtvs) = forAllLit <+> interppSP qtvs <> dot
1356 pprHsExplicitForAll Nothing = empty
1357
1358 pprLHsContext :: (OutputableBndrId (GhcPass p))
1359 => LHsContext (GhcPass p) -> SDoc
1360 pprLHsContext lctxt
1361 | null (unLoc lctxt) = empty
1362 | otherwise = pprLHsContextAlways lctxt
1363
1364 -- For use in a HsQualTy, which always gets printed if it exists.
1365 pprLHsContextAlways :: (OutputableBndrId (GhcPass p))
1366 => LHsContext (GhcPass p) -> SDoc
1367 pprLHsContextAlways (L _ ctxt)
1368 = case ctxt of
1369 [] -> parens empty <+> darrow
1370 [L _ ty] -> ppr_mono_ty ty <+> darrow
1371 _ -> parens (interpp'SP ctxt) <+> darrow
1372
1373 -- True <=> print an extra-constraints wildcard, e.g. @(Show a, _) =>@
1374 pprLHsContextExtra :: (OutputableBndrId (GhcPass p))
1375 => Bool -> LHsContext (GhcPass p) -> SDoc
1376 pprLHsContextExtra show_extra lctxt@(L _ ctxt)
1377 | not show_extra = pprLHsContext lctxt
1378 | null ctxt = char '_' <+> darrow
1379 | otherwise = parens (sep (punctuate comma ctxt')) <+> darrow
1380 where
1381 ctxt' = map ppr ctxt ++ [char '_']
1382
1383 pprConDeclFields :: (OutputableBndrId (GhcPass p))
1384 => [LConDeclField (GhcPass p)] -> SDoc
1385 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
1386 where
1387 ppr_fld (L _ (ConDeclField { cd_fld_names = ns, cd_fld_type = ty,
1388 cd_fld_doc = doc }))
1389 = ppr_names ns <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
1390 ppr_fld (L _ (XConDeclField x)) = ppr x
1391 ppr_names [n] = ppr n
1392 ppr_names ns = sep (punctuate comma (map ppr ns))
1393
1394 {-
1395 Note [Printing KindedTyVars]
1396 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1397 Trac #3830 reminded me that we should really only print the kind
1398 signature on a KindedTyVar if the kind signature was put there by the
1399 programmer. During kind inference GHC now adds a PostTcKind to UserTyVars,
1400 rather than converting to KindedTyVars as before.
1401
1402 (As it happens, the message in #3830 comes out a different way now,
1403 and the problem doesn't show up; but having the flag on a KindedTyVar
1404 seems like the Right Thing anyway.)
1405 -}
1406
1407 -- Printing works more-or-less as for Types
1408
1409 pprHsType :: (OutputableBndrId (GhcPass p)) => HsType (GhcPass p) -> SDoc
1410 pprHsType ty = ppr_mono_ty ty
1411
1412 ppr_mono_lty :: (OutputableBndrId (GhcPass p)) => LHsType (GhcPass p) -> SDoc
1413 ppr_mono_lty ty = ppr_mono_ty (unLoc ty)
1414
1415 ppr_mono_ty :: (OutputableBndrId (GhcPass p)) => HsType (GhcPass p) -> SDoc
1416 ppr_mono_ty (HsForAllTy { hst_bndrs = tvs, hst_body = ty })
1417 = sep [pprHsForAll tvs noLHsContext, ppr_mono_lty ty]
1418
1419 ppr_mono_ty (HsQualTy { hst_ctxt = ctxt, hst_body = ty })
1420 = sep [pprLHsContextAlways ctxt, ppr_mono_lty ty]
1421
1422 ppr_mono_ty (HsBangTy _ b ty) = ppr b <> ppr_mono_lty ty
1423 ppr_mono_ty (HsRecTy _ flds) = pprConDeclFields flds
1424 ppr_mono_ty (HsTyVar _ prom (L _ name))
1425 | isPromoted prom = quote (pprPrefixOcc name)
1426 | otherwise = pprPrefixOcc name
1427 ppr_mono_ty (HsFunTy _ ty1 ty2) = ppr_fun_ty ty1 ty2
1428 ppr_mono_ty (HsTupleTy _ con tys) = tupleParens std_con (pprWithCommas ppr tys)
1429 where std_con = case con of
1430 HsUnboxedTuple -> UnboxedTuple
1431 _ -> BoxedTuple
1432 ppr_mono_ty (HsSumTy _ tys)
1433 = tupleParens UnboxedTuple (pprWithBars ppr tys)
1434 ppr_mono_ty (HsKindSig _ ty kind)
1435 = ppr_mono_lty ty <+> dcolon <+> ppr kind
1436 ppr_mono_ty (HsListTy _ ty) = brackets (ppr_mono_lty ty)
1437 ppr_mono_ty (HsIParamTy _ n ty) = (ppr n <+> dcolon <+> ppr_mono_lty ty)
1438 ppr_mono_ty (HsSpliceTy _ s) = pprSplice s
1439 ppr_mono_ty (HsExplicitListTy _ prom tys)
1440 | isPromoted prom = quote $ brackets (maybeAddSpace tys $ interpp'SP tys)
1441 | otherwise = brackets (interpp'SP tys)
1442 ppr_mono_ty (HsExplicitTupleTy _ tys)
1443 = quote $ parens (maybeAddSpace tys $ interpp'SP tys)
1444 ppr_mono_ty (HsTyLit _ t) = ppr_tylit t
1445 ppr_mono_ty (HsWildCardTy {}) = char '_'
1446
1447 ppr_mono_ty (HsStarTy _ isUni) = char (if isUni then '' else '*')
1448
1449 ppr_mono_ty (HsAppTy _ fun_ty arg_ty)
1450 = hsep [ppr_mono_lty fun_ty, ppr_mono_lty arg_ty]
1451 ppr_mono_ty (HsAppKindTy _ ty k)
1452 = ppr_mono_lty ty <+> char '@' <> ppr_mono_lty k
1453 ppr_mono_ty (HsOpTy _ ty1 (L _ op) ty2)
1454 = sep [ ppr_mono_lty ty1
1455 , sep [pprInfixOcc op, ppr_mono_lty ty2 ] ]
1456
1457 ppr_mono_ty (HsParTy _ ty)
1458 = parens (ppr_mono_lty ty)
1459 -- Put the parens in where the user did
1460 -- But we still use the precedence stuff to add parens because
1461 -- toHsType doesn't put in any HsParTys, so we may still need them
1462
1463 ppr_mono_ty (HsDocTy _ ty doc)
1464 -- AZ: Should we add parens? Should we introduce "-- ^"?
1465 = ppr_mono_lty ty <+> ppr (unLoc doc)
1466 -- we pretty print Haddock comments on types as if they were
1467 -- postfix operators
1468
1469 ppr_mono_ty (XHsType t) = ppr t
1470
1471 --------------------------
1472 ppr_fun_ty :: (OutputableBndrId (GhcPass p))
1473 => LHsType (GhcPass p) -> LHsType (GhcPass p) -> SDoc
1474 ppr_fun_ty ty1 ty2
1475 = let p1 = ppr_mono_lty ty1
1476 p2 = ppr_mono_lty ty2
1477 in
1478 sep [p1, arrow <+> p2]
1479
1480 --------------------------
1481 ppr_tylit :: HsTyLit -> SDoc
1482 ppr_tylit (HsNumTy _ i) = integer i
1483 ppr_tylit (HsStrTy _ s) = text (show s)
1484
1485
1486 -- | @'hsTypeNeedsParens' p t@ returns 'True' if the type @t@ needs parentheses
1487 -- under precedence @p@.
1488 hsTypeNeedsParens :: PprPrec -> HsType pass -> Bool
1489 hsTypeNeedsParens p = go
1490 where
1491 go (HsForAllTy{}) = p >= funPrec
1492 go (HsQualTy{}) = p >= funPrec
1493 go (HsBangTy{}) = p > topPrec
1494 go (HsRecTy{}) = False
1495 go (HsTyVar{}) = False
1496 go (HsFunTy{}) = p >= funPrec
1497 go (HsTupleTy{}) = False
1498 go (HsSumTy{}) = False
1499 go (HsKindSig{}) = p >= sigPrec
1500 go (HsListTy{}) = False
1501 go (HsIParamTy{}) = p > topPrec
1502 go (HsSpliceTy{}) = False
1503 go (HsExplicitListTy{}) = False
1504 go (HsExplicitTupleTy{}) = False
1505 go (HsTyLit{}) = False
1506 go (HsWildCardTy{}) = False
1507 go (HsStarTy{}) = False
1508 go (HsAppTy{}) = p >= appPrec
1509 go (HsAppKindTy{}) = p >= appPrec
1510 go (HsOpTy{}) = p >= opPrec
1511 go (HsParTy{}) = False
1512 go (HsDocTy _ (L _ t) _) = go t
1513 go (XHsType{}) = False
1514
1515 maybeAddSpace :: [LHsType pass] -> SDoc -> SDoc
1516 -- See Note [Printing promoted type constructors]
1517 -- in IfaceType. This code implements the same
1518 -- logic for printing HsType
1519 maybeAddSpace tys doc
1520 | (ty : _) <- tys
1521 , lhsTypeHasLeadingPromotionQuote ty = space <> doc
1522 | otherwise = doc
1523
1524 lhsTypeHasLeadingPromotionQuote :: LHsType pass -> Bool
1525 lhsTypeHasLeadingPromotionQuote ty
1526 = goL ty
1527 where
1528 goL (L _ ty) = go ty
1529
1530 go (HsForAllTy{}) = False
1531 go (HsQualTy{ hst_ctxt = ctxt, hst_body = body})
1532 | L _ (c:_) <- ctxt = goL c
1533 | otherwise = goL body
1534 go (HsBangTy{}) = False
1535 go (HsRecTy{}) = False
1536 go (HsTyVar _ p _) = isPromoted p
1537 go (HsFunTy _ arg _) = goL arg
1538 go (HsListTy{}) = False
1539 go (HsTupleTy{}) = False
1540 go (HsSumTy{}) = False
1541 go (HsOpTy _ t1 _ _) = goL t1
1542 go (HsKindSig _ t _) = goL t
1543 go (HsIParamTy{}) = False
1544 go (HsSpliceTy{}) = False
1545 go (HsExplicitListTy _ p _) = isPromoted p
1546 go (HsExplicitTupleTy{}) = True
1547 go (HsTyLit{}) = False
1548 go (HsWildCardTy{}) = False
1549 go (HsStarTy{}) = False
1550 go (HsAppTy _ t _) = goL t
1551 go (HsAppKindTy _ t _) = goL t
1552 go (HsParTy{}) = False
1553 go (HsDocTy _ t _) = goL t
1554 go (XHsType{}) = False
1555
1556 -- | @'parenthesizeHsType' p ty@ checks if @'hsTypeNeedsParens' p ty@ is
1557 -- true, and if so, surrounds @ty@ with an 'HsParTy'. Otherwise, it simply
1558 -- returns @ty@.
1559 parenthesizeHsType :: PprPrec -> LHsType (GhcPass p) -> LHsType (GhcPass p)
1560 parenthesizeHsType p lty@(L loc ty)
1561 | hsTypeNeedsParens p ty = L loc (HsParTy NoExt lty)
1562 | otherwise = lty
1563
1564 -- | @'parenthesizeHsContext' p ctxt@ checks if @ctxt@ is a single constraint
1565 -- @c@ such that @'hsTypeNeedsParens' p c@ is true, and if so, surrounds @c@
1566 -- with an 'HsParTy' to form a parenthesized @ctxt@. Otherwise, it simply
1567 -- returns @ctxt@ unchanged.
1568 parenthesizeHsContext :: PprPrec
1569 -> LHsContext (GhcPass p) -> LHsContext (GhcPass p)
1570 parenthesizeHsContext p lctxt@(L loc ctxt) =
1571 case ctxt of
1572 [c] -> L loc [parenthesizeHsType p c]
1573 _ -> lctxt -- Other contexts are already "parenthesized" by virtue of
1574 -- being tuples.