11d301d816376571db89d1a4c926068569196624
[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 StandaloneDeriving #-}
12 {-# LANGUAGE TypeSynonymInstances #-}
13 {-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
14 -- in module PlaceHolder
15 {-# LANGUAGE ConstraintKinds #-}
16 {-# LANGUAGE CPP #-}
17 {-# LANGUAGE TypeFamilies #-}
18
19 module HsTypes (
20 HsType(..), NewHsTypeX(..), LHsType, HsKind, LHsKind,
21 HsTyVarBndr(..), LHsTyVarBndr,
22 LHsQTyVars(..), HsQTvsRn(..),
23 HsImplicitBndrs(..), HsIBRn(..),
24 HsWildCardBndrs(..),
25 LHsSigType, LHsSigWcType, LHsWcType,
26 HsTupleSort(..),
27 Promoted(..),
28 HsContext, LHsContext,
29 HsTyLit(..),
30 HsIPName(..), hsIPNameFS,
31 HsAppType(..),LHsAppType,
32
33 LBangType, BangType,
34 HsSrcBang(..), HsImplBang(..),
35 SrcStrictness(..), SrcUnpackedness(..),
36 getBangType, getBangStrictness,
37
38 ConDeclField(..), LConDeclField, pprConDeclFields,
39
40 HsConDetails(..),
41
42 FieldOcc(..), LFieldOcc, mkFieldOcc,
43 AmbiguousFieldOcc(..), mkAmbiguousFieldOcc,
44 rdrNameAmbiguousFieldOcc, selectorAmbiguousFieldOcc,
45 unambiguousFieldOcc, ambiguousFieldOcc,
46
47 HsWildCardInfo(..), mkAnonWildCardTy, pprAnonWildCard,
48 wildCardName, sameWildCard,
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, splitHsAppsTy,
61 splitHsAppTys, getAppsTyHead_maybe, hsTyGetAppHead_maybe,
62 mkHsOpTy, mkHsAppTy, mkHsAppTys, mkHsAppsTy,
63 ignoreParens, hsSigType, hsSigWcType,
64 hsLTyVarBndrToType, hsLTyVarBndrsToTypes,
65
66 -- Printing
67 pprHsType, pprHsForAll, pprHsForAllTvs, pprHsForAllExtra,
68 pprHsContext, pprHsContextNoArrow, pprHsContextMaybe,
69 hsTypeNeedsParens, parenthesizeHsType
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
94 import Data.Data hiding ( Fixity, Prefix, Infix )
95 import Data.Maybe ( fromMaybe )
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 After the renamer, this contains a Name which uniquely
194 identifies this particular occurrence.
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 * All wildcards, whether named or anonymous, are bound by the
215 HsWildCardBndrs construct, which wraps types that are allowed
216 to have wildcards.
217
218 * After type checking is done, we report what types the wildcards
219 got unified with.
220
221 -}
222
223 -- | Located Haskell Context
224 type LHsContext pass = Located (HsContext pass)
225 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnUnit'
226
227 -- For details on above see note [Api annotations] in ApiAnnotation
228
229 -- | Haskell Context
230 type HsContext pass = [LHsType pass]
231
232 -- | Located Haskell Type
233 type LHsType pass = Located (HsType pass)
234 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
235 -- in a list
236
237 -- For details on above see note [Api annotations] in ApiAnnotation
238
239 -- | Haskell Kind
240 type HsKind pass = HsType pass
241
242 -- | Located Haskell Kind
243 type LHsKind pass = Located (HsKind pass)
244 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
245
246 -- For details on above see note [Api annotations] in ApiAnnotation
247
248 --------------------------------------------------
249 -- LHsQTyVars
250 -- The explicitly-quantified binders in a data/type declaration
251
252 -- | Located Haskell Type Variable Binder
253 type LHsTyVarBndr pass = Located (HsTyVarBndr pass)
254 -- See Note [HsType binders]
255
256 -- | Located Haskell Quantified Type Variables
257 data LHsQTyVars pass -- See Note [HsType binders]
258 = HsQTvs { hsq_ext :: XHsQTvs pass
259
260 , hsq_explicit :: [LHsTyVarBndr pass]
261 -- Explicit variables, written by the user
262 -- See Note [HsForAllTy tyvar binders]
263 }
264 | XLHsQTyVars (XXLHsQTyVars pass)
265
266 data HsQTvsRn
267 = HsQTvsRn
268 { hsq_implicit :: [Name]
269 -- Implicit (dependent) variables
270
271 , hsq_dependent :: NameSet
272 -- Which members of hsq_explicit are dependent; that is,
273 -- mentioned in the kind of a later hsq_explicit,
274 -- or mentioned in a kind in the scope of this HsQTvs
275 -- See Note [Dependent LHsQTyVars] in TcHsType
276 } deriving Data
277
278 type instance XHsQTvs GhcPs = NoExt
279 type instance XHsQTvs GhcRn = HsQTvsRn
280 type instance XHsQTvs GhcTc = HsQTvsRn
281
282 type instance XXLHsQTyVars (GhcPass _) = NoExt
283
284 mkHsQTvs :: [LHsTyVarBndr GhcPs] -> LHsQTyVars GhcPs
285 mkHsQTvs tvs = HsQTvs { hsq_ext = noExt, hsq_explicit = tvs }
286
287 hsQTvExplicit :: LHsQTyVars pass -> [LHsTyVarBndr pass]
288 hsQTvExplicit = hsq_explicit
289
290 emptyLHsQTvs :: LHsQTyVars GhcRn
291 emptyLHsQTvs = HsQTvs (HsQTvsRn [] emptyNameSet) []
292
293 isEmptyLHsQTvs :: LHsQTyVars GhcRn -> Bool
294 isEmptyLHsQTvs (HsQTvs (HsQTvsRn [] _) []) = True
295 isEmptyLHsQTvs _ = False
296
297 ------------------------------------------------
298 -- HsImplicitBndrs
299 -- Used to quantify the implicit binders of a type
300 -- * Implicit binders of a type signature (LHsSigType/LHsSigWcType)
301 -- * Patterns in a type/data family instance (HsTyPats)
302
303 -- | Haskell Implicit Binders
304 data HsImplicitBndrs pass thing -- See Note [HsType binders]
305 = HsIB { hsib_ext :: XHsIB pass thing
306 , hsib_body :: thing -- Main payload (type or list of types)
307 }
308 | XHsImplicitBndrs (XXHsImplicitBndrs pass thing)
309
310 data HsIBRn
311 = HsIBRn { hsib_vars :: [Name] -- Implicitly-bound kind & type vars
312 , hsib_closed :: Bool -- Taking the hsib_vars into account,
313 -- is the payload closed? Used in
314 -- TcHsType.decideKindGeneralisationPlan
315 } deriving Data
316
317 type instance XHsIB GhcPs _ = NoExt
318 type instance XHsIB GhcRn _ = HsIBRn
319 type instance XHsIB GhcTc _ = HsIBRn
320
321 type instance XXHsImplicitBndrs (GhcPass _) _ = NoExt
322
323 -- | Haskell Wildcard Binders
324 data HsWildCardBndrs pass thing
325 -- See Note [HsType binders]
326 -- See Note [The wildcard story for types]
327 = HsWC { hswc_ext :: XHsWC pass thing
328 -- after the renamer
329 -- Wild cards, both named and anonymous
330
331 , hswc_body :: thing
332 -- Main payload (type or list of types)
333 -- If there is an extra-constraints wildcard,
334 -- it's still there in the hsc_body.
335 }
336 | XHsWildCardBndrs (XXHsWildCardBndrs pass thing)
337
338 type instance XHsWC GhcPs b = NoExt
339 type instance XHsWC GhcRn b = [Name]
340 type instance XHsWC GhcTc b = [Name]
341
342 type instance XXHsWildCardBndrs (GhcPass _) b = NoExt
343
344 -- | Located Haskell Signature Type
345 type LHsSigType pass = HsImplicitBndrs pass (LHsType pass) -- Implicit only
346
347 -- | Located Haskell Wildcard Type
348 type LHsWcType pass = HsWildCardBndrs pass (LHsType pass) -- Wildcard only
349
350 -- | Located Haskell Signature Wildcard Type
351 type LHsSigWcType pass = HsWildCardBndrs pass (LHsSigType pass) -- Both
352
353 -- See Note [Representing type signatures]
354
355 hsImplicitBody :: HsImplicitBndrs pass thing -> thing
356 hsImplicitBody (HsIB { hsib_body = body }) = body
357 hsImplicitBody (XHsImplicitBndrs _) = panic "hsImplicitBody"
358
359 hsSigType :: LHsSigType pass -> LHsType pass
360 hsSigType = hsImplicitBody
361
362 hsSigWcType :: LHsSigWcType pass -> LHsType pass
363 hsSigWcType sig_ty = hsib_body (hswc_body sig_ty)
364
365 dropWildCards :: LHsSigWcType pass -> LHsSigType pass
366 -- Drop the wildcard part of a LHsSigWcType
367 dropWildCards sig_ty = hswc_body sig_ty
368
369 {- Note [Representing type signatures]
370 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
371 HsSigType is used to represent an explicit user type signature
372 such as f :: a -> a
373 or g (x :: a -> a) = x
374
375 A HsSigType is just a HsImplicitBndrs wrapping a LHsType.
376 * The HsImplicitBndrs binds the /implicitly/ quantified tyvars
377 * The LHsType binds the /explicitly/ quantified tyvars
378
379 E.g. For a signature like
380 f :: forall (a::k). blah
381 we get
382 HsIB { hsib_vars = [k]
383 , hsib_body = HsForAllTy { hst_bndrs = [(a::*)]
384 , hst_body = blah }
385 The implicit kind variable 'k' is bound by the HsIB;
386 the explicitly forall'd tyvar 'a' is bound by the HsForAllTy
387 -}
388
389 mkHsImplicitBndrs :: thing -> HsImplicitBndrs GhcPs thing
390 mkHsImplicitBndrs x = HsIB { hsib_ext = noExt
391 , hsib_body = x }
392
393 mkHsWildCardBndrs :: thing -> HsWildCardBndrs GhcPs thing
394 mkHsWildCardBndrs x = HsWC { hswc_body = x
395 , hswc_ext = noExt }
396
397 -- Add empty binders. This is a bit suspicious; what if
398 -- the wrapped thing had free type variables?
399 mkEmptyImplicitBndrs :: thing -> HsImplicitBndrs GhcRn thing
400 mkEmptyImplicitBndrs x = HsIB { hsib_ext = HsIBRn
401 { hsib_vars = []
402 , hsib_closed = False }
403 , hsib_body = x }
404
405 mkEmptyWildCardBndrs :: thing -> HsWildCardBndrs GhcRn thing
406 mkEmptyWildCardBndrs x = HsWC { hswc_body = x
407 , hswc_ext = [] }
408
409
410 --------------------------------------------------
411 -- | These names are used early on to store the names of implicit
412 -- parameters. They completely disappear after type-checking.
413 newtype HsIPName = HsIPName FastString
414 deriving( Eq, Data )
415
416 hsIPNameFS :: HsIPName -> FastString
417 hsIPNameFS (HsIPName n) = n
418
419 instance Outputable HsIPName where
420 ppr (HsIPName n) = char '?' <> ftext n -- Ordinary implicit parameters
421
422 instance OutputableBndr HsIPName where
423 pprBndr _ n = ppr n -- Simple for now
424 pprInfixOcc n = ppr n
425 pprPrefixOcc n = ppr n
426
427 --------------------------------------------------
428
429 -- | Haskell Type Variable Binder
430 data HsTyVarBndr pass
431 = UserTyVar -- no explicit kinding
432 (XUserTyVar pass)
433 (Located (IdP pass))
434 -- See Note [Located RdrNames] in HsExpr
435 | KindedTyVar
436 (XKindedTyVar pass)
437 (Located (IdP pass))
438 (LHsKind pass) -- The user-supplied kind signature
439 -- ^
440 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
441 -- 'ApiAnnotation.AnnDcolon', 'ApiAnnotation.AnnClose'
442
443 -- For details on above see note [Api annotations] in ApiAnnotation
444
445 | XTyVarBndr
446 (XXTyVarBndr pass)
447
448 type instance XUserTyVar (GhcPass _) = NoExt
449 type instance XKindedTyVar (GhcPass _) = NoExt
450 type instance XXTyVarBndr (GhcPass _) = NoExt
451
452 -- | Does this 'HsTyVarBndr' come with an explicit kind annotation?
453 isHsKindedTyVar :: HsTyVarBndr pass -> Bool
454 isHsKindedTyVar (UserTyVar {}) = False
455 isHsKindedTyVar (KindedTyVar {}) = True
456 isHsKindedTyVar (XTyVarBndr{}) = panic "isHsKindedTyVar"
457
458 -- | Do all type variables in this 'LHsQTyVars' come with kind annotations?
459 hsTvbAllKinded :: LHsQTyVars pass -> Bool
460 hsTvbAllKinded = all (isHsKindedTyVar . unLoc) . hsQTvExplicit
461
462 -- | Haskell Type
463 data HsType pass
464 = HsForAllTy -- See Note [HsType binders]
465 { hst_xforall :: XForAllTy pass,
466 hst_bndrs :: [LHsTyVarBndr pass]
467 -- Explicit, user-supplied 'forall a b c'
468 , hst_body :: LHsType pass -- body type
469 }
470 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnForall',
471 -- 'ApiAnnotation.AnnDot','ApiAnnotation.AnnDarrow'
472 -- For details on above see note [Api annotations] in ApiAnnotation
473
474 | HsQualTy -- See Note [HsType binders]
475 { hst_xqual :: XQualTy pass
476 , hst_ctxt :: LHsContext pass -- Context C => blah
477 , hst_body :: LHsType pass }
478
479 | HsTyVar (XTyVar pass)
480 Promoted -- whether explicitly promoted, for the pretty
481 -- printer
482 (Located (IdP pass))
483 -- Type variable, type constructor, or data constructor
484 -- see Note [Promotions (HsTyVar)]
485 -- See Note [Located RdrNames] in HsExpr
486 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
487
488 -- For details on above see note [Api annotations] in ApiAnnotation
489
490 | HsAppsTy (XAppsTy pass)
491 [LHsAppType pass] -- Used only before renaming,
492 -- Note [HsAppsTy]
493 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
494
495 | HsAppTy (XAppTy pass)
496 (LHsType pass)
497 (LHsType pass)
498 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
499
500 -- For details on above see note [Api annotations] in ApiAnnotation
501
502 | HsFunTy (XFunTy pass)
503 (LHsType pass) -- function type
504 (LHsType pass)
505 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow',
506
507 -- For details on above see note [Api annotations] in ApiAnnotation
508
509 | HsListTy (XListTy pass)
510 (LHsType pass) -- Element type
511 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
512 -- 'ApiAnnotation.AnnClose' @']'@
513
514 -- For details on above see note [Api annotations] in ApiAnnotation
515
516 | HsTupleTy (XTupleTy pass)
517 HsTupleSort
518 [LHsType pass] -- Element types (length gives arity)
519 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(' or '(#'@,
520 -- 'ApiAnnotation.AnnClose' @')' or '#)'@
521
522 -- For details on above see note [Api annotations] in ApiAnnotation
523
524 | HsSumTy (XSumTy pass)
525 [LHsType pass] -- Element types (length gives arity)
526 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(#'@,
527 -- 'ApiAnnotation.AnnClose' '#)'@
528
529 -- For details on above see note [Api annotations] in ApiAnnotation
530
531 | HsOpTy (XOpTy pass)
532 (LHsType pass) (Located (IdP pass)) (LHsType pass)
533 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
534
535 -- For details on above see note [Api annotations] in ApiAnnotation
536
537 | HsParTy (XParTy pass)
538 (LHsType pass) -- See Note [Parens in HsSyn] in HsExpr
539 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
540 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
541 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
542 -- 'ApiAnnotation.AnnClose' @')'@
543
544 -- For details on above see note [Api annotations] in ApiAnnotation
545
546 | HsIParamTy (XIParamTy pass)
547 (Located HsIPName) -- (?x :: ty)
548 (LHsType pass) -- Implicit parameters as they occur in
549 -- contexts
550 -- ^
551 -- > (?x :: ty)
552 --
553 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
554
555 -- For details on above see note [Api annotations] in ApiAnnotation
556
557 | HsEqTy (XEqTy pass)
558 (LHsType pass) -- ty1 ~ ty2
559 (LHsType pass) -- Always allowed even without
560 -- TypeOperators, and has special
561 -- kinding rule
562 -- ^
563 -- > ty1 ~ ty2
564 --
565 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'
566
567 -- For details on above see note [Api annotations] in ApiAnnotation
568
569 | HsKindSig (XKindSig pass)
570 (LHsType pass) -- (ty :: kind)
571 (LHsKind pass) -- A type with a kind signature
572 -- ^
573 -- > (ty :: kind)
574 --
575 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
576 -- 'ApiAnnotation.AnnDcolon','ApiAnnotation.AnnClose' @')'@
577
578 -- For details on above see note [Api annotations] in ApiAnnotation
579
580 | HsSpliceTy (XSpliceTy pass)
581 (HsSplice pass) -- Includes quasi-quotes
582 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'$('@,
583 -- 'ApiAnnotation.AnnClose' @')'@
584
585 -- For details on above see note [Api annotations] in ApiAnnotation
586
587 | HsDocTy (XDocTy pass)
588 (LHsType pass) LHsDocString -- A documented type
589 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
590
591 -- For details on above see note [Api annotations] in ApiAnnotation
592
593 | HsBangTy (XBangTy pass)
594 HsSrcBang (LHsType pass) -- Bang-style type annotations
595 -- ^ - 'ApiAnnotation.AnnKeywordId' :
596 -- 'ApiAnnotation.AnnOpen' @'{-\# UNPACK' or '{-\# NOUNPACK'@,
597 -- 'ApiAnnotation.AnnClose' @'#-}'@
598 -- 'ApiAnnotation.AnnBang' @\'!\'@
599
600 -- For details on above see note [Api annotations] in ApiAnnotation
601
602 | HsRecTy (XRecTy pass)
603 [LConDeclField pass] -- Only in data type declarations
604 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
605 -- 'ApiAnnotation.AnnClose' @'}'@
606
607 -- For details on above see note [Api annotations] in ApiAnnotation
608
609 -- | HsCoreTy (XCoreTy pass) Type -- An escape hatch for tunnelling a *closed*
610 -- -- Core Type through HsSyn.
611 -- -- ^ - 'ApiAnnotation.AnnKeywordId' : None
612
613 -- For details on above see note [Api annotations] in ApiAnnotation
614
615 | HsExplicitListTy -- A promoted explicit list
616 (XExplicitListTy pass)
617 Promoted -- whether explcitly promoted, for pretty printer
618 [LHsType pass]
619 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'["@,
620 -- 'ApiAnnotation.AnnClose' @']'@
621
622 -- For details on above see note [Api annotations] in ApiAnnotation
623
624 | HsExplicitTupleTy -- A promoted explicit tuple
625 (XExplicitTupleTy pass)
626 [LHsType pass]
627 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'("@,
628 -- 'ApiAnnotation.AnnClose' @')'@
629
630 -- For details on above see note [Api annotations] in ApiAnnotation
631
632 | HsTyLit (XTyLit pass) HsTyLit -- A promoted numeric literal.
633 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
634
635 -- For details on above see note [Api annotations] in ApiAnnotation
636
637 | HsWildCardTy (XWildCardTy pass) -- A type wildcard
638 -- See Note [The wildcard story for types]
639 -- A anonymous wild card ('_'). A fresh Name is generated for
640 -- each individual anonymous wildcard during renaming
641 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
642
643 -- For details on above see note [Api annotations] in ApiAnnotation
644
645 -- For adding new constructors via Trees that Grow
646 | XHsType
647 (XXType pass)
648
649 data NewHsTypeX
650 = NHsCoreTy Type -- An escape hatch for tunnelling a *closed*
651 -- Core Type through HsSyn.
652 deriving Data
653 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
654
655 instance Outputable NewHsTypeX where
656 ppr (NHsCoreTy ty) = ppr ty
657
658 type instance XForAllTy (GhcPass _) = NoExt
659 type instance XQualTy (GhcPass _) = NoExt
660 type instance XTyVar (GhcPass _) = NoExt
661 type instance XAppsTy (GhcPass _) = NoExt
662 type instance XAppTy (GhcPass _) = NoExt
663 type instance XFunTy (GhcPass _) = NoExt
664 type instance XListTy (GhcPass _) = NoExt
665 type instance XTupleTy (GhcPass _) = NoExt
666 type instance XSumTy (GhcPass _) = NoExt
667 type instance XOpTy (GhcPass _) = NoExt
668 type instance XParTy (GhcPass _) = NoExt
669 type instance XIParamTy (GhcPass _) = NoExt
670 type instance XEqTy (GhcPass _) = NoExt
671 type instance XKindSig (GhcPass _) = NoExt
672
673 type instance XSpliceTy GhcPs = NoExt
674 type instance XSpliceTy GhcRn = NoExt
675 type instance XSpliceTy GhcTc = Kind
676
677 type instance XDocTy (GhcPass _) = NoExt
678 type instance XBangTy (GhcPass _) = NoExt
679 type instance XRecTy (GhcPass _) = NoExt
680
681 type instance XExplicitListTy GhcPs = NoExt
682 type instance XExplicitListTy GhcRn = NoExt
683 type instance XExplicitListTy GhcTc = Kind
684
685 type instance XExplicitTupleTy GhcPs = NoExt
686 type instance XExplicitTupleTy GhcRn = NoExt
687 type instance XExplicitTupleTy GhcTc = [Kind]
688
689 type instance XTyLit (GhcPass _) = NoExt
690
691 type instance XWildCardTy GhcPs = NoExt
692 type instance XWildCardTy GhcRn = HsWildCardInfo
693 type instance XWildCardTy GhcTc = HsWildCardInfo
694
695 type instance XXType (GhcPass _) = NewHsTypeX
696
697
698 -- Note [Literal source text] in BasicTypes for SourceText fields in
699 -- the following
700 -- | Haskell Type Literal
701 data HsTyLit
702 = HsNumTy SourceText Integer
703 | HsStrTy SourceText FastString
704 deriving Data
705
706 newtype HsWildCardInfo -- See Note [The wildcard story for types]
707 = AnonWildCard (Located Name)
708 deriving Data
709 -- A anonymous wild card ('_'). A fresh Name is generated for
710 -- each individual anonymous wildcard during renaming
711
712 -- | Located Haskell Application Type
713 type LHsAppType pass = Located (HsAppType pass)
714 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnSimpleQuote'
715
716 -- | Haskell Application Type
717 data HsAppType pass
718 = HsAppInfix (XAppInfix pass)
719 (Located (IdP pass)) -- either a symbol or an id in backticks
720 | HsAppPrefix (XAppPrefix pass)
721 (LHsType pass) -- anything else, including things like (+)
722 | XAppType
723 (XXAppType pass)
724
725 type instance XAppInfix (GhcPass _) = NoExt
726 type instance XAppPrefix (GhcPass _) = NoExt
727 type instance XXAppType (GhcPass _) = NoExt
728
729 instance (p ~ GhcPass pass, OutputableBndrId p)
730 => Outputable (HsAppType p) where
731 ppr = ppr_app_ty
732
733 {-
734 Note [HsForAllTy tyvar binders]
735 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
736 After parsing:
737 * Implicit => empty
738 Explicit => the variables the user wrote
739
740 After renaming
741 * Implicit => the *type* variables free in the type
742 Explicit => the variables the user wrote (renamed)
743
744 Qualified currently behaves exactly as Implicit,
745 but it is deprecated to use it for implicit quantification.
746 In this case, GHC 7.10 gives a warning; see
747 Note [Context quantification] in RnTypes, and Trac #4426.
748 In GHC 8.0, Qualified will no longer bind variables
749 and this will become an error.
750
751 The kind variables bound in the hsq_implicit field come both
752 a) from the kind signatures on the kind vars (eg k1)
753 b) from the scope of the forall (eg k2)
754 Example: f :: forall (a::k1) b. T a (b::k2)
755
756
757 Note [Unit tuples]
758 ~~~~~~~~~~~~~~~~~~
759 Consider the type
760 type instance F Int = ()
761 We want to parse that "()"
762 as HsTupleTy HsBoxedOrConstraintTuple [],
763 NOT as HsTyVar unitTyCon
764
765 Why? Because F might have kind (* -> Constraint), so we when parsing we
766 don't know if that tuple is going to be a constraint tuple or an ordinary
767 unit tuple. The HsTupleSort flag is specifically designed to deal with
768 that, but it has to work for unit tuples too.
769
770 Note [Promotions (HsTyVar)]
771 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
772 HsTyVar: A name in a type or kind.
773 Here are the allowed namespaces for the name.
774 In a type:
775 Var: not allowed
776 Data: promoted data constructor
777 Tv: type variable
778 TcCls before renamer: type constructor, class constructor, or promoted data constructor
779 TcCls after renamer: type constructor or class constructor
780 In a kind:
781 Var, Data: not allowed
782 Tv: kind variable
783 TcCls: kind constructor or promoted type constructor
784
785 The 'Promoted' field in an HsTyVar captures whether the type was promoted in
786 the source code by prefixing an apostrophe.
787
788 Note [HsAppsTy]
789 ~~~~~~~~~~~~~~~
790 How to parse
791
792 Foo * Int
793
794 ? Is it `(*) Foo Int` or `Foo GHC.Types.* Int`? There's no way to know until renaming.
795 So we just take type expressions like this and put each component in a list, so be
796 sorted out in the renamer. The sorting out is done by RnTypes.mkHsOpTyRn. This means
797 that the parser should never produce HsAppTy or HsOpTy.
798
799 Note [Promoted lists and tuples]
800 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
801 Notice the difference between
802 HsListTy HsExplicitListTy
803 HsTupleTy HsExplicitListTupleTy
804
805 E.g. f :: [Int] HsListTy
806
807 g3 :: T '[] All these use
808 g2 :: T '[True] HsExplicitListTy
809 g1 :: T '[True,False]
810 g1a :: T [True,False] (can omit ' where unambiguous)
811
812 kind of T :: [Bool] -> * This kind uses HsListTy!
813
814 E.g. h :: (Int,Bool) HsTupleTy; f is a pair
815 k :: S '(True,False) HsExplicitTypleTy; S is indexed by
816 a type-level pair of booleans
817 kind of S :: (Bool,Bool) -> * This kind uses HsExplicitTupleTy
818
819 Note [Distinguishing tuple kinds]
820 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
821
822 Apart from promotion, tuples can have one of three different kinds:
823
824 x :: (Int, Bool) -- Regular boxed tuples
825 f :: Int# -> (# Int#, Int# #) -- Unboxed tuples
826 g :: (Eq a, Ord a) => a -- Constraint tuples
827
828 For convenience, internally we use a single constructor for all of these,
829 namely HsTupleTy, but keep track of the tuple kind (in the first argument to
830 HsTupleTy, a HsTupleSort). We can tell if a tuple is unboxed while parsing,
831 because of the #. However, with -XConstraintKinds we can only distinguish
832 between constraint and boxed tuples during type checking, in general. Hence the
833 four constructors of HsTupleSort:
834
835 HsUnboxedTuple -> Produced by the parser
836 HsBoxedTuple -> Certainly a boxed tuple
837 HsConstraintTuple -> Certainly a constraint tuple
838 HsBoxedOrConstraintTuple -> Could be a boxed or a constraint
839 tuple. Produced by the parser only,
840 disappears after type checking
841 -}
842
843 -- | Haskell Tuple Sort
844 data HsTupleSort = HsUnboxedTuple
845 | HsBoxedTuple
846 | HsConstraintTuple
847 | HsBoxedOrConstraintTuple
848 deriving Data
849
850
851 -- | Promoted data types.
852 data Promoted = Promoted
853 | NotPromoted
854 deriving (Data, Eq, Show)
855
856 -- | Located Constructor Declaration Field
857 type LConDeclField pass = Located (ConDeclField pass)
858 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
859 -- in a list
860
861 -- For details on above see note [Api annotations] in ApiAnnotation
862
863 -- | Constructor Declaration Field
864 data ConDeclField pass -- Record fields have Haddoc docs on them
865 = ConDeclField { cd_fld_ext :: XConDeclField pass,
866 cd_fld_names :: [LFieldOcc pass],
867 -- ^ See Note [ConDeclField passs]
868 cd_fld_type :: LBangType pass,
869 cd_fld_doc :: Maybe LHsDocString }
870 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
871
872 -- For details on above see note [Api annotations] in ApiAnnotation
873 | XConDeclField (XXConDeclField pass)
874
875 type instance XConDeclField (GhcPass _) = NoExt
876 type instance XXConDeclField (GhcPass _) = NoExt
877
878 instance (p ~ GhcPass pass, OutputableBndrId p)
879 => Outputable (ConDeclField p) where
880 ppr (ConDeclField _ fld_n fld_ty _) = ppr fld_n <+> dcolon <+> ppr fld_ty
881 ppr (XConDeclField x) = ppr x
882
883 -- HsConDetails is used for patterns/expressions *and* for data type
884 -- declarations
885 -- | Haskell Constructor Details
886 data HsConDetails arg rec
887 = PrefixCon [arg] -- C p1 p2 p3
888 | RecCon rec -- C { x = p1, y = p2 }
889 | InfixCon arg arg -- p1 `C` p2
890 deriving Data
891
892 instance (Outputable arg, Outputable rec)
893 => Outputable (HsConDetails arg rec) where
894 ppr (PrefixCon args) = text "PrefixCon" <+> ppr args
895 ppr (RecCon rec) = text "RecCon:" <+> ppr rec
896 ppr (InfixCon l r) = text "InfixCon:" <+> ppr [l, r]
897
898 {-
899 Note [ConDeclField passs]
900 ~~~~~~~~~~~~~~~~~~~~~~~~~
901
902 A ConDeclField contains a list of field occurrences: these always
903 include the field label as the user wrote it. After the renamer, it
904 will additionally contain the identity of the selector function in the
905 second component.
906
907 Due to DuplicateRecordFields, the OccName of the selector function
908 may have been mangled, which is why we keep the original field label
909 separately. For example, when DuplicateRecordFields is enabled
910
911 data T = MkT { x :: Int }
912
913 gives
914
915 ConDeclField { cd_fld_names = [L _ (FieldOcc "x" $sel:x:MkT)], ... }.
916 -}
917
918 -----------------------
919 -- A valid type must have a for-all at the top of the type, or of the fn arg
920 -- types
921
922 ---------------------
923 hsWcScopedTvs :: LHsSigWcType GhcRn -> [Name]
924 -- Get the lexically-scoped type variables of a HsSigType
925 -- - the explicitly-given forall'd type variables
926 -- - the implicitly-bound kind variables
927 -- - the named wildcars; see Note [Scoping of named wildcards]
928 -- because they scope in the same way
929 hsWcScopedTvs sig_ty
930 | HsWC { hswc_ext = nwcs, hswc_body = sig_ty1 } <- sig_ty
931 , HsIB { hsib_ext = HsIBRn { hsib_vars = vars}
932 , hsib_body = sig_ty2 } <- sig_ty1
933 = case sig_ty2 of
934 L _ (HsForAllTy { hst_bndrs = tvs }) -> vars ++ nwcs ++
935 map hsLTyVarName tvs
936 -- include kind variables only if the type is headed by forall
937 -- (this is consistent with GHC 7 behaviour)
938 _ -> nwcs
939 hsWcScopedTvs (HsWC _ (XHsImplicitBndrs _)) = panic "hsWcScopedTvs"
940 hsWcScopedTvs (XHsWildCardBndrs _) = panic "hsWcScopedTvs"
941
942 hsScopedTvs :: LHsSigType GhcRn -> [Name]
943 -- Same as hsWcScopedTvs, but for a LHsSigType
944 hsScopedTvs sig_ty
945 | HsIB { hsib_ext = HsIBRn { hsib_vars = vars }
946 , hsib_body = sig_ty2 } <- sig_ty
947 , L _ (HsForAllTy { hst_bndrs = tvs }) <- sig_ty2
948 = vars ++ map hsLTyVarName tvs
949 | otherwise
950 = []
951
952 {- Note [Scoping of named wildcards]
953 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
954 Consider
955 f :: _a -> _a
956 f x = let g :: _a -> _a
957 g = ...
958 in ...
959
960 Currently, for better or worse, the "_a" variables are all the same. So
961 although there is no explicit forall, the "_a" scopes over the definition.
962 I don't know if this is a good idea, but there it is.
963 -}
964
965 ---------------------
966 hsTyVarName :: HsTyVarBndr pass -> IdP pass
967 hsTyVarName (UserTyVar _ (L _ n)) = n
968 hsTyVarName (KindedTyVar _ (L _ n) _) = n
969 hsTyVarName (XTyVarBndr{}) = panic "hsTyVarName"
970
971 hsLTyVarName :: LHsTyVarBndr pass -> IdP pass
972 hsLTyVarName = hsTyVarName . unLoc
973
974 hsExplicitLTyVarNames :: LHsQTyVars pass -> [IdP pass]
975 -- Explicit variables only
976 hsExplicitLTyVarNames qtvs = map hsLTyVarName (hsQTvExplicit qtvs)
977
978 hsAllLTyVarNames :: LHsQTyVars GhcRn -> [Name]
979 -- All variables
980 hsAllLTyVarNames (HsQTvs { hsq_ext = HsQTvsRn { hsq_implicit = kvs }
981 , hsq_explicit = tvs })
982 = kvs ++ map hsLTyVarName tvs
983 hsAllLTyVarNames (XLHsQTyVars _) = panic "hsAllLTyVarNames"
984
985 hsLTyVarLocName :: LHsTyVarBndr pass -> Located (IdP pass)
986 hsLTyVarLocName = fmap hsTyVarName
987
988 hsLTyVarLocNames :: LHsQTyVars pass -> [Located (IdP pass)]
989 hsLTyVarLocNames qtvs = map hsLTyVarLocName (hsQTvExplicit qtvs)
990
991 -- | Convert a LHsTyVarBndr to an equivalent LHsType.
992 hsLTyVarBndrToType :: LHsTyVarBndr (GhcPass p) -> LHsType (GhcPass p)
993 hsLTyVarBndrToType = fmap cvt
994 where cvt (UserTyVar _ n) = HsTyVar noExt NotPromoted n
995 cvt (KindedTyVar _ (L name_loc n) kind)
996 = HsKindSig noExt
997 (L name_loc (HsTyVar noExt NotPromoted (L name_loc n))) kind
998 cvt (XTyVarBndr{}) = panic "hsLTyVarBndrToType"
999
1000 -- | Convert a LHsTyVarBndrs to a list of types.
1001 -- Works on *type* variable only, no kind vars.
1002 hsLTyVarBndrsToTypes :: LHsQTyVars (GhcPass p) -> [LHsType (GhcPass p)]
1003 hsLTyVarBndrsToTypes (HsQTvs { hsq_explicit = tvbs }) = map hsLTyVarBndrToType tvbs
1004 hsLTyVarBndrsToTypes (XLHsQTyVars _) = panic "hsLTyVarBndrsToTypes"
1005
1006 ---------------------
1007 wildCardName :: HsWildCardInfo -> Name
1008 wildCardName (AnonWildCard (L _ n)) = n
1009
1010 -- Two wild cards are the same when they have the same location
1011 sameWildCard :: Located HsWildCardInfo -> Located HsWildCardInfo -> Bool
1012 sameWildCard (L l1 (AnonWildCard _)) (L l2 (AnonWildCard _)) = l1 == l2
1013
1014 ignoreParens :: LHsType pass -> LHsType pass
1015 ignoreParens (L _ (HsParTy _ ty)) = ignoreParens ty
1016 ignoreParens ty = ty
1017
1018 isLHsForAllTy :: LHsType p -> Bool
1019 isLHsForAllTy (L _ (HsForAllTy {})) = True
1020 isLHsForAllTy _ = False
1021
1022 {-
1023 ************************************************************************
1024 * *
1025 Building types
1026 * *
1027 ************************************************************************
1028 -}
1029
1030 mkAnonWildCardTy :: HsType GhcPs
1031 mkAnonWildCardTy = HsWildCardTy noExt
1032
1033 mkHsOpTy :: LHsType (GhcPass p) -> Located (IdP (GhcPass p))
1034 -> LHsType (GhcPass p) -> HsType (GhcPass p)
1035 mkHsOpTy ty1 op ty2 = HsOpTy noExt ty1 op ty2
1036
1037 mkHsAppTy :: LHsType (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
1038 mkHsAppTy t1 t2
1039 = addCLoc t1 t2 (HsAppTy noExt t1 (parenthesizeHsType appPrec t2))
1040
1041 mkHsAppTys :: LHsType (GhcPass p) -> [LHsType (GhcPass p)]
1042 -> LHsType (GhcPass p)
1043 mkHsAppTys = foldl mkHsAppTy
1044
1045 mkHsAppsTy :: [LHsAppType GhcPs] -> HsType GhcPs
1046 -- In the common case of a singleton non-operator,
1047 -- avoid the clutter of wrapping in a HsAppsTy
1048 mkHsAppsTy [L _ (HsAppPrefix _ (L _ ty))] = ty
1049 mkHsAppsTy app_tys = HsAppsTy NoExt app_tys
1050
1051 {-
1052 ************************************************************************
1053 * *
1054 Decomposing HsTypes
1055 * *
1056 ************************************************************************
1057 -}
1058
1059 ---------------------------------
1060 -- splitHsFunType decomposes a type (t1 -> t2 ... -> tn)
1061 -- Breaks up any parens in the result type:
1062 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
1063 -- Also deals with (->) t1 t2; that is why it only works on LHsType Name
1064 -- (see Trac #9096)
1065 splitHsFunType :: LHsType GhcRn -> ([LHsType GhcRn], LHsType GhcRn)
1066 splitHsFunType (L _ (HsParTy _ ty))
1067 = splitHsFunType ty
1068
1069 splitHsFunType (L _ (HsFunTy _ x y))
1070 | (args, res) <- splitHsFunType y
1071 = (x:args, res)
1072
1073 splitHsFunType orig_ty@(L _ (HsAppTy _ t1 t2))
1074 = go t1 [t2]
1075 where -- Look for (->) t1 t2, possibly with parenthesisation
1076 go (L _ (HsTyVar _ _ (L _ fn))) tys | fn == funTyConName
1077 , [t1,t2] <- tys
1078 , (args, res) <- splitHsFunType t2
1079 = (t1:args, res)
1080 go (L _ (HsAppTy _ t1 t2)) tys = go t1 (t2:tys)
1081 go (L _ (HsParTy _ ty)) tys = go ty tys
1082 go _ _ = ([], orig_ty) -- Failure to match
1083
1084 splitHsFunType other = ([], other)
1085
1086 --------------------------------
1087 -- | Retrieves the head of an HsAppsTy, if this can be done unambiguously,
1088 -- without consulting fixities.
1089 getAppsTyHead_maybe :: [LHsAppType (GhcPass p)]
1090 -> Maybe ( LHsType (GhcPass p)
1091 , [LHsType (GhcPass p)], LexicalFixity)
1092 getAppsTyHead_maybe tys = case splitHsAppsTy tys of
1093 ([app1:apps], []) -> -- no symbols, some normal types
1094 Just (mkHsAppTys app1 apps, [], Prefix)
1095 ([app1l:appsl, app1r:appsr], [L loc op]) -> -- one operator
1096 Just ( L loc (HsTyVar noExt NotPromoted (L loc op))
1097 , [mkHsAppTys app1l appsl, mkHsAppTys app1r appsr], Infix)
1098 _ -> -- can't figure it out
1099 Nothing
1100
1101 -- | Splits a [HsAppType pass] (the payload of an HsAppsTy) into regions of
1102 -- prefix types (normal types) and infix operators.
1103 -- If @splitHsAppsTy tys = (non_syms, syms)@, then @tys@ starts with the first
1104 -- element of @non_syms@ followed by the first element of @syms@ followed by
1105 -- the next element of @non_syms@, etc. It is guaranteed that the non_syms list
1106 -- has one more element than the syms list.
1107 splitHsAppsTy :: [LHsAppType pass] -> ([[LHsType pass]], [Located (IdP pass)])
1108 splitHsAppsTy = go [] [] []
1109 where
1110 go acc acc_non acc_sym [] = (reverse (reverse acc : acc_non), reverse acc_sym)
1111 go acc acc_non acc_sym (L _ (HsAppPrefix _ ty) : rest)
1112 = go (ty : acc) acc_non acc_sym rest
1113 go acc acc_non acc_sym (L _ (HsAppInfix _ op) : rest)
1114 = go [] (reverse acc : acc_non) (op : acc_sym) rest
1115 go _ _ _ (L _ (XAppType _):_) = panic "splitHsAppsTy"
1116
1117 -- Retrieve the name of the "head" of a nested type application
1118 -- somewhat like splitHsAppTys, but a little more thorough
1119 -- used to examine the result of a GADT-like datacon, so it doesn't handle
1120 -- *all* cases (like lists, tuples, (~), etc.)
1121 hsTyGetAppHead_maybe :: LHsType (GhcPass p)
1122 -> Maybe (Located (IdP (GhcPass p)), [LHsType (GhcPass p)])
1123 hsTyGetAppHead_maybe = go []
1124 where
1125 go tys (L _ (HsTyVar _ _ ln)) = Just (ln, tys)
1126 go tys (L _ (HsAppsTy _ apps))
1127 | Just (head, args, _) <- getAppsTyHead_maybe apps
1128 = go (args ++ tys) head
1129 go tys (L _ (HsAppTy _ l r)) = go (r : tys) l
1130 go tys (L _ (HsOpTy _ l (L loc n) r)) = Just (L loc n, l : r : tys)
1131 go tys (L _ (HsParTy _ t)) = go tys t
1132 go tys (L _ (HsKindSig _ t _)) = go tys t
1133 go _ _ = Nothing
1134
1135 splitHsAppTys :: LHsType GhcRn -> [LHsType GhcRn]
1136 -> (LHsType GhcRn, [LHsType GhcRn])
1137 -- no need to worry about HsAppsTy here
1138 splitHsAppTys (L _ (HsAppTy _ f a)) as = splitHsAppTys f (a:as)
1139 splitHsAppTys (L _ (HsParTy _ f)) as = splitHsAppTys f as
1140 splitHsAppTys f as = (f,as)
1141
1142 --------------------------------
1143 splitLHsPatSynTy :: LHsType pass
1144 -> ( [LHsTyVarBndr pass] -- universals
1145 , LHsContext pass -- required constraints
1146 , [LHsTyVarBndr pass] -- existentials
1147 , LHsContext pass -- provided constraints
1148 , LHsType pass) -- body type
1149 splitLHsPatSynTy ty = (univs, reqs, exis, provs, ty4)
1150 where
1151 (univs, ty1) = splitLHsForAllTy ty
1152 (reqs, ty2) = splitLHsQualTy ty1
1153 (exis, ty3) = splitLHsForAllTy ty2
1154 (provs, ty4) = splitLHsQualTy ty3
1155
1156 splitLHsSigmaTy :: LHsType pass
1157 -> ([LHsTyVarBndr pass], LHsContext pass, LHsType pass)
1158 splitLHsSigmaTy ty
1159 | (tvs, ty1) <- splitLHsForAllTy ty
1160 , (ctxt, ty2) <- splitLHsQualTy ty1
1161 = (tvs, ctxt, ty2)
1162
1163 splitLHsForAllTy :: LHsType pass -> ([LHsTyVarBndr pass], LHsType pass)
1164 splitLHsForAllTy (L _ (HsParTy _ ty)) = splitLHsForAllTy ty
1165 splitLHsForAllTy (L _ (HsForAllTy { hst_bndrs = tvs, hst_body = body })) = (tvs, body)
1166 splitLHsForAllTy body = ([], body)
1167
1168 splitLHsQualTy :: LHsType pass -> (LHsContext pass, LHsType pass)
1169 splitLHsQualTy (L _ (HsParTy _ ty)) = splitLHsQualTy ty
1170 splitLHsQualTy (L _ (HsQualTy { hst_ctxt = ctxt, hst_body = body })) = (ctxt, body)
1171 splitLHsQualTy body = (noLoc [], body)
1172
1173 splitLHsInstDeclTy :: LHsSigType GhcRn
1174 -> ([Name], LHsContext GhcRn, LHsType GhcRn)
1175 -- Split up an instance decl type, returning the pieces
1176 splitLHsInstDeclTy (HsIB { hsib_ext = HsIBRn { hsib_vars = itkvs }
1177 , hsib_body = inst_ty })
1178 | (tvs, cxt, body_ty) <- splitLHsSigmaTy inst_ty
1179 = (itkvs ++ map hsLTyVarName tvs, cxt, body_ty)
1180 -- Return implicitly bound type and kind vars
1181 -- For an instance decl, all of them are in scope
1182 splitLHsInstDeclTy (XHsImplicitBndrs _) = panic "splitLHsInstDeclTy"
1183
1184 getLHsInstDeclHead :: LHsSigType pass -> LHsType pass
1185 getLHsInstDeclHead inst_ty
1186 | (_tvs, _cxt, body_ty) <- splitLHsSigmaTy (hsSigType inst_ty)
1187 = body_ty
1188
1189 getLHsInstDeclClass_maybe :: LHsSigType (GhcPass p)
1190 -> Maybe (Located (IdP (GhcPass p)))
1191 -- Works on (HsSigType RdrName)
1192 getLHsInstDeclClass_maybe inst_ty
1193 = do { let head_ty = getLHsInstDeclHead inst_ty
1194 ; (cls, _) <- hsTyGetAppHead_maybe head_ty
1195 ; return cls }
1196
1197 {-
1198 ************************************************************************
1199 * *
1200 FieldOcc
1201 * *
1202 ************************************************************************
1203 -}
1204
1205 -- | Located Field Occurrence
1206 type LFieldOcc pass = Located (FieldOcc pass)
1207
1208 -- | Field Occurrence
1209 --
1210 -- Represents an *occurrence* of an unambiguous field. We store
1211 -- both the 'RdrName' the user originally wrote, and after the
1212 -- renamer, the selector function.
1213 data FieldOcc pass = FieldOcc { extFieldOcc :: XFieldOcc pass
1214 , rdrNameFieldOcc :: Located RdrName
1215 -- ^ See Note [Located RdrNames] in HsExpr
1216 }
1217
1218 | XFieldOcc
1219 (XXFieldOcc pass)
1220 deriving instance (p ~ GhcPass pass, Eq (XFieldOcc p)) => Eq (FieldOcc p)
1221 deriving instance (p ~ GhcPass pass, Ord (XFieldOcc p)) => Ord (FieldOcc p)
1222
1223 type instance XFieldOcc GhcPs = NoExt
1224 type instance XFieldOcc GhcRn = Name
1225 type instance XFieldOcc GhcTc = Id
1226
1227 type instance XXFieldOcc (GhcPass _) = NoExt
1228
1229 instance Outputable (FieldOcc pass) where
1230 ppr = ppr . rdrNameFieldOcc
1231
1232 mkFieldOcc :: Located RdrName -> FieldOcc GhcPs
1233 mkFieldOcc rdr = FieldOcc noExt rdr
1234
1235
1236 -- | Ambiguous Field Occurrence
1237 --
1238 -- Represents an *occurrence* of a field that is potentially
1239 -- ambiguous after the renamer, with the ambiguity resolved by the
1240 -- typechecker. We always store the 'RdrName' that the user
1241 -- originally wrote, and store the selector function after the renamer
1242 -- (for unambiguous occurrences) or the typechecker (for ambiguous
1243 -- occurrences).
1244 --
1245 -- See Note [HsRecField and HsRecUpdField] in HsPat and
1246 -- Note [Disambiguating record fields] in TcExpr.
1247 -- See Note [Located RdrNames] in HsExpr
1248 data AmbiguousFieldOcc pass
1249 = Unambiguous (XUnambiguous pass) (Located RdrName)
1250 | Ambiguous (XAmbiguous pass) (Located RdrName)
1251 | XAmbiguousFieldOcc (XXAmbiguousFieldOcc pass)
1252
1253 type instance XUnambiguous GhcPs = NoExt
1254 type instance XUnambiguous GhcRn = Name
1255 type instance XUnambiguous GhcTc = Id
1256
1257 type instance XAmbiguous GhcPs = NoExt
1258 type instance XAmbiguous GhcRn = NoExt
1259 type instance XAmbiguous GhcTc = Id
1260
1261 type instance XXAmbiguousFieldOcc (GhcPass _) = NoExt
1262
1263 instance p ~ GhcPass pass => Outputable (AmbiguousFieldOcc p) where
1264 ppr = ppr . rdrNameAmbiguousFieldOcc
1265
1266 instance p ~ GhcPass pass => OutputableBndr (AmbiguousFieldOcc p) where
1267 pprInfixOcc = pprInfixOcc . rdrNameAmbiguousFieldOcc
1268 pprPrefixOcc = pprPrefixOcc . rdrNameAmbiguousFieldOcc
1269
1270 mkAmbiguousFieldOcc :: Located RdrName -> AmbiguousFieldOcc GhcPs
1271 mkAmbiguousFieldOcc rdr = Unambiguous noExt rdr
1272
1273 rdrNameAmbiguousFieldOcc :: AmbiguousFieldOcc (GhcPass p) -> RdrName
1274 rdrNameAmbiguousFieldOcc (Unambiguous _ (L _ rdr)) = rdr
1275 rdrNameAmbiguousFieldOcc (Ambiguous _ (L _ rdr)) = rdr
1276 rdrNameAmbiguousFieldOcc (XAmbiguousFieldOcc _)
1277 = panic "rdrNameAmbiguousFieldOcc"
1278
1279 selectorAmbiguousFieldOcc :: AmbiguousFieldOcc GhcTc -> Id
1280 selectorAmbiguousFieldOcc (Unambiguous sel _) = sel
1281 selectorAmbiguousFieldOcc (Ambiguous sel _) = sel
1282 selectorAmbiguousFieldOcc (XAmbiguousFieldOcc _)
1283 = panic "selectorAmbiguousFieldOcc"
1284
1285 unambiguousFieldOcc :: AmbiguousFieldOcc GhcTc -> FieldOcc GhcTc
1286 unambiguousFieldOcc (Unambiguous rdr sel) = FieldOcc rdr sel
1287 unambiguousFieldOcc (Ambiguous rdr sel) = FieldOcc rdr sel
1288 unambiguousFieldOcc (XAmbiguousFieldOcc _) = panic "unambiguousFieldOcc"
1289
1290 ambiguousFieldOcc :: FieldOcc GhcTc -> AmbiguousFieldOcc GhcTc
1291 ambiguousFieldOcc (FieldOcc sel rdr) = Unambiguous sel rdr
1292 ambiguousFieldOcc (XFieldOcc _) = panic "ambiguousFieldOcc"
1293
1294 {-
1295 ************************************************************************
1296 * *
1297 \subsection{Pretty printing}
1298 * *
1299 ************************************************************************
1300 -}
1301
1302 instance (p ~ GhcPass pass, OutputableBndrId p) => Outputable (HsType p) where
1303 ppr ty = pprHsType ty
1304
1305 instance Outputable HsTyLit where
1306 ppr = ppr_tylit
1307
1308 instance (p ~ GhcPass pass, OutputableBndrId p)
1309 => Outputable (LHsQTyVars p) where
1310 ppr (HsQTvs { hsq_explicit = tvs }) = interppSP tvs
1311 ppr (XLHsQTyVars x) = ppr x
1312
1313 instance (p ~ GhcPass pass, OutputableBndrId p)
1314 => Outputable (HsTyVarBndr p) where
1315 ppr (UserTyVar _ n) = ppr n
1316 ppr (KindedTyVar _ n k) = parens $ hsep [ppr n, dcolon, ppr k]
1317 ppr (XTyVarBndr n) = ppr n
1318
1319 instance (p ~ GhcPass pass,Outputable thing)
1320 => Outputable (HsImplicitBndrs p thing) where
1321 ppr (HsIB { hsib_body = ty }) = ppr ty
1322 ppr (XHsImplicitBndrs x) = ppr x
1323
1324 instance (p ~ GhcPass pass,Outputable thing)
1325 => Outputable (HsWildCardBndrs p thing) where
1326 ppr (HsWC { hswc_body = ty }) = ppr ty
1327 ppr (XHsWildCardBndrs x) = ppr x
1328
1329 instance Outputable HsWildCardInfo where
1330 ppr (AnonWildCard _) = char '_'
1331
1332 pprAnonWildCard :: SDoc
1333 pprAnonWildCard = char '_'
1334
1335 pprHsForAll :: (OutputableBndrId (GhcPass p))
1336 => [LHsTyVarBndr (GhcPass p)] -> LHsContext (GhcPass p) -> SDoc
1337 pprHsForAll = pprHsForAllExtra Nothing
1338
1339 -- | Version of 'pprHsForAll' that can also print an extra-constraints
1340 -- wildcard, e.g. @_ => a -> Bool@ or @(Show a, _) => a -> String@. This
1341 -- underscore will be printed when the 'Maybe SrcSpan' argument is a 'Just'
1342 -- containing the location of the extra-constraints wildcard. A special
1343 -- function for this is needed, as the extra-constraints wildcard is removed
1344 -- from the actual context and type, and stored in a separate field, thus just
1345 -- printing the type will not print the extra-constraints wildcard.
1346 pprHsForAllExtra :: (OutputableBndrId (GhcPass p))
1347 => Maybe SrcSpan -> [LHsTyVarBndr (GhcPass p)]
1348 -> LHsContext (GhcPass p) -> SDoc
1349 pprHsForAllExtra extra qtvs cxt
1350 = pprHsForAllTvs qtvs <+> pprHsContextExtra show_extra (unLoc cxt)
1351 where
1352 show_extra = isJust extra
1353
1354 pprHsForAllTvs :: (OutputableBndrId (GhcPass p))
1355 => [LHsTyVarBndr (GhcPass p)] -> SDoc
1356 pprHsForAllTvs qtvs
1357 | null qtvs = whenPprDebug (forAllLit <+> dot)
1358 | otherwise = forAllLit <+> interppSP qtvs <> dot
1359
1360 pprHsContext :: (OutputableBndrId (GhcPass p)) => HsContext (GhcPass p) -> SDoc
1361 pprHsContext = maybe empty (<+> darrow) . pprHsContextMaybe
1362
1363 pprHsContextNoArrow :: (OutputableBndrId (GhcPass p))
1364 => HsContext (GhcPass p) -> SDoc
1365 pprHsContextNoArrow = fromMaybe empty . pprHsContextMaybe
1366
1367 pprHsContextMaybe :: (OutputableBndrId (GhcPass p))
1368 => HsContext (GhcPass p) -> Maybe SDoc
1369 pprHsContextMaybe [] = Nothing
1370 pprHsContextMaybe [L _ pred] = Just $ ppr_mono_ty pred
1371 pprHsContextMaybe cxt = Just $ parens (interpp'SP cxt)
1372
1373 -- For use in a HsQualTy, which always gets printed if it exists.
1374 pprHsContextAlways :: (OutputableBndrId (GhcPass p))
1375 => HsContext (GhcPass p) -> SDoc
1376 pprHsContextAlways [] = parens empty <+> darrow
1377 pprHsContextAlways [L _ ty] = ppr_mono_ty ty <+> darrow
1378 pprHsContextAlways cxt = parens (interpp'SP cxt) <+> darrow
1379
1380 -- True <=> print an extra-constraints wildcard, e.g. @(Show a, _) =>@
1381 pprHsContextExtra :: (OutputableBndrId (GhcPass p))
1382 => Bool -> HsContext (GhcPass p) -> SDoc
1383 pprHsContextExtra show_extra ctxt
1384 | not show_extra
1385 = pprHsContext ctxt
1386 | null ctxt
1387 = char '_' <+> darrow
1388 | otherwise
1389 = 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 [pprHsForAllTvs tvs, ppr_mono_lty ty]
1428
1429 ppr_mono_ty (HsQualTy { hst_ctxt = L _ ctxt, hst_body = ty })
1430 = sep [pprHsContextAlways 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 _ NotPromoted (L _ name))= pprPrefixOcc name
1435 ppr_mono_ty (HsTyVar _ Promoted (L _ name))
1436 = space <> quote (pprPrefixOcc name)
1437 -- We need a space before the ' above, so the parser
1438 -- does not attach it to the previous symbol
1439 ppr_mono_ty (HsFunTy _ ty1 ty2) = ppr_fun_ty ty1 ty2
1440 ppr_mono_ty (HsTupleTy _ con tys) = tupleParens std_con (pprWithCommas ppr tys)
1441 where std_con = case con of
1442 HsUnboxedTuple -> UnboxedTuple
1443 _ -> BoxedTuple
1444 ppr_mono_ty (HsSumTy _ tys)
1445 = tupleParens UnboxedTuple (pprWithBars ppr tys)
1446 ppr_mono_ty (HsKindSig _ ty kind)
1447 = parens (ppr_mono_lty ty <+> dcolon <+> ppr kind)
1448 ppr_mono_ty (HsListTy _ ty) = brackets (ppr_mono_lty ty)
1449 ppr_mono_ty (HsIParamTy _ n ty) = (ppr n <+> dcolon <+> ppr_mono_lty ty)
1450 ppr_mono_ty (HsSpliceTy _ s) = pprSplice s
1451 ppr_mono_ty (HsExplicitListTy _ Promoted tys)
1452 = quote $ brackets (interpp'SP tys)
1453 ppr_mono_ty (HsExplicitListTy _ NotPromoted tys)
1454 = brackets (interpp'SP tys)
1455 ppr_mono_ty (HsExplicitTupleTy _ tys) = quote $ parens (interpp'SP tys)
1456 ppr_mono_ty (HsTyLit _ t) = ppr_tylit t
1457 ppr_mono_ty (HsWildCardTy {}) = char '_'
1458
1459 ppr_mono_ty (HsEqTy _ ty1 ty2)
1460 = ppr_mono_lty ty1 <+> char '~' <+> ppr_mono_lty ty2
1461
1462 ppr_mono_ty (HsAppsTy _ tys)
1463 = hsep (map (ppr_app_ty . unLoc) tys)
1464
1465 ppr_mono_ty (HsAppTy _ fun_ty arg_ty)
1466 = hsep [ppr_mono_lty fun_ty, ppr_mono_lty arg_ty]
1467
1468 ppr_mono_ty (HsOpTy _ ty1 (L _ op) ty2)
1469 = sep [ ppr_mono_lty ty1
1470 , sep [pprInfixOcc op, ppr_mono_lty ty2 ] ]
1471
1472 ppr_mono_ty (HsParTy _ ty)
1473 = parens (ppr_mono_lty ty)
1474 -- Put the parens in where the user did
1475 -- But we still use the precedence stuff to add parens because
1476 -- toHsType doesn't put in any HsParTys, so we may still need them
1477
1478 ppr_mono_ty (HsDocTy _ ty doc)
1479 -- AZ: Should we add parens? Should we introduce "-- ^"?
1480 = ppr_mono_lty ty <+> ppr (unLoc doc)
1481 -- we pretty print Haddock comments on types as if they were
1482 -- postfix operators
1483
1484 ppr_mono_ty (XHsType t) = ppr t
1485
1486 --------------------------
1487 ppr_fun_ty :: (OutputableBndrId (GhcPass p))
1488 => LHsType (GhcPass p) -> LHsType (GhcPass p) -> SDoc
1489 ppr_fun_ty ty1 ty2
1490 = let p1 = ppr_mono_lty ty1
1491 p2 = ppr_mono_lty ty2
1492 in
1493 sep [p1, text "->" <+> p2]
1494
1495 --------------------------
1496 ppr_app_ty :: (OutputableBndrId (GhcPass p)) => HsAppType (GhcPass p) -> SDoc
1497 ppr_app_ty (HsAppInfix _ (L _ n)) = pprInfixOcc n
1498 ppr_app_ty (HsAppPrefix _ (L _ (HsTyVar _ NotPromoted (L _ n))))
1499 = pprPrefixOcc n
1500 ppr_app_ty (HsAppPrefix _ (L _ (HsTyVar _ Promoted (L _ n))))
1501 = space <> quote (pprPrefixOcc n) -- We need a space before the ' above, so
1502 -- the parser does not attach it to the
1503 -- previous symbol
1504 ppr_app_ty (HsAppPrefix _ ty) = ppr_mono_lty ty
1505
1506 ppr_app_ty (XAppType ty) = ppr ty
1507
1508 --------------------------
1509 ppr_tylit :: HsTyLit -> SDoc
1510 ppr_tylit (HsNumTy _ i) = integer i
1511 ppr_tylit (HsStrTy _ s) = text (show s)
1512
1513
1514 -- | @'hsTypeNeedsParens' p t@ returns 'True' if the type @t@ needs parentheses
1515 -- under precedence @p@.
1516 hsTypeNeedsParens :: PprPrec -> HsType pass -> Bool
1517 hsTypeNeedsParens p = go
1518 where
1519 go (HsForAllTy{}) = False
1520 go (HsQualTy{}) = False
1521 go (HsBangTy{}) = p > topPrec
1522 go (HsRecTy{}) = False
1523 go (HsTyVar{}) = False
1524 go (HsFunTy{}) = p >= funPrec
1525 go (HsTupleTy{}) = False
1526 go (HsSumTy{}) = False
1527 go (HsKindSig{}) = False
1528 go (HsListTy{}) = False
1529 go (HsIParamTy{}) = p > topPrec
1530 go (HsSpliceTy{}) = False
1531 go (HsExplicitListTy{}) = False
1532 go (HsExplicitTupleTy{}) = False
1533 go (HsTyLit{}) = False
1534 go (HsWildCardTy{}) = False
1535 go (HsEqTy{}) = p >= opPrec
1536 go (HsAppsTy _ args) = p >= appPrec && not (null args)
1537 go (HsAppTy{}) = p >= appPrec
1538 go (HsOpTy{}) = p >= opPrec
1539 go (HsParTy{}) = False
1540 go (HsDocTy _ (L _ t) _) = go t
1541 go (XHsType{}) = False
1542
1543 -- | @'parenthesizeHsType' p ty@ checks if @'hsTypeNeedsParens' p ty@ is
1544 -- true, and if so, surrounds @ty@ with an 'HsParTy'. Otherwise, it simply
1545 -- returns @ty@.
1546 parenthesizeHsType :: PprPrec -> LHsType (GhcPass p) -> LHsType (GhcPass p)
1547 parenthesizeHsType p lty@(L loc ty)
1548 | hsTypeNeedsParens p ty = L loc (HsParTy NoExt lty)
1549 | otherwise = lty