Fix #14875 by introducing PprPrec, and using it
[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 | HsPArrTy (XPArrTy pass)
517 (LHsType pass) -- Elem. type of parallel array: [:t:]
518 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'[:'@,
519 -- 'ApiAnnotation.AnnClose' @':]'@
520
521 -- For details on above see note [Api annotations] in ApiAnnotation
522
523 | HsTupleTy (XTupleTy pass)
524 HsTupleSort
525 [LHsType pass] -- Element types (length gives arity)
526 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(' or '(#'@,
527 -- 'ApiAnnotation.AnnClose' @')' or '#)'@
528
529 -- For details on above see note [Api annotations] in ApiAnnotation
530
531 | HsSumTy (XSumTy pass)
532 [LHsType pass] -- Element types (length gives arity)
533 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(#'@,
534 -- 'ApiAnnotation.AnnClose' '#)'@
535
536 -- For details on above see note [Api annotations] in ApiAnnotation
537
538 | HsOpTy (XOpTy pass)
539 (LHsType pass) (Located (IdP pass)) (LHsType pass)
540 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
541
542 -- For details on above see note [Api annotations] in ApiAnnotation
543
544 | HsParTy (XParTy pass)
545 (LHsType pass) -- See Note [Parens in HsSyn] in HsExpr
546 -- Parenthesis preserved for the precedence re-arrangement in RnTypes
547 -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
548 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
549 -- 'ApiAnnotation.AnnClose' @')'@
550
551 -- For details on above see note [Api annotations] in ApiAnnotation
552
553 | HsIParamTy (XIParamTy pass)
554 (Located HsIPName) -- (?x :: ty)
555 (LHsType pass) -- Implicit parameters as they occur in
556 -- contexts
557 -- ^
558 -- > (?x :: ty)
559 --
560 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
561
562 -- For details on above see note [Api annotations] in ApiAnnotation
563
564 | HsEqTy (XEqTy pass)
565 (LHsType pass) -- ty1 ~ ty2
566 (LHsType pass) -- Always allowed even without
567 -- TypeOperators, and has special
568 -- kinding rule
569 -- ^
570 -- > ty1 ~ ty2
571 --
572 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnTilde'
573
574 -- For details on above see note [Api annotations] in ApiAnnotation
575
576 | HsKindSig (XKindSig pass)
577 (LHsType pass) -- (ty :: kind)
578 (LHsKind pass) -- A type with a kind signature
579 -- ^
580 -- > (ty :: kind)
581 --
582 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
583 -- 'ApiAnnotation.AnnDcolon','ApiAnnotation.AnnClose' @')'@
584
585 -- For details on above see note [Api annotations] in ApiAnnotation
586
587 | HsSpliceTy (XSpliceTy pass)
588 (HsSplice pass) -- Includes quasi-quotes
589 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'$('@,
590 -- 'ApiAnnotation.AnnClose' @')'@
591
592 -- For details on above see note [Api annotations] in ApiAnnotation
593
594 | HsDocTy (XDocTy pass)
595 (LHsType pass) LHsDocString -- A documented type
596 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
597
598 -- For details on above see note [Api annotations] in ApiAnnotation
599
600 | HsBangTy (XBangTy pass)
601 HsSrcBang (LHsType pass) -- Bang-style type annotations
602 -- ^ - 'ApiAnnotation.AnnKeywordId' :
603 -- 'ApiAnnotation.AnnOpen' @'{-\# UNPACK' or '{-\# NOUNPACK'@,
604 -- 'ApiAnnotation.AnnClose' @'#-}'@
605 -- 'ApiAnnotation.AnnBang' @\'!\'@
606
607 -- For details on above see note [Api annotations] in ApiAnnotation
608
609 | HsRecTy (XRecTy pass)
610 [LConDeclField pass] -- Only in data type declarations
611 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
612 -- 'ApiAnnotation.AnnClose' @'}'@
613
614 -- For details on above see note [Api annotations] in ApiAnnotation
615
616 -- | HsCoreTy (XCoreTy pass) Type -- An escape hatch for tunnelling a *closed*
617 -- -- Core Type through HsSyn.
618 -- -- ^ - 'ApiAnnotation.AnnKeywordId' : None
619
620 -- For details on above see note [Api annotations] in ApiAnnotation
621
622 | HsExplicitListTy -- A promoted explicit list
623 (XExplicitListTy pass)
624 Promoted -- whether explcitly promoted, for pretty printer
625 [LHsType pass]
626 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'["@,
627 -- 'ApiAnnotation.AnnClose' @']'@
628
629 -- For details on above see note [Api annotations] in ApiAnnotation
630
631 | HsExplicitTupleTy -- A promoted explicit tuple
632 (XExplicitTupleTy pass)
633 [LHsType pass]
634 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'("@,
635 -- 'ApiAnnotation.AnnClose' @')'@
636
637 -- For details on above see note [Api annotations] in ApiAnnotation
638
639 | HsTyLit (XTyLit pass) HsTyLit -- A promoted numeric literal.
640 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
641
642 -- For details on above see note [Api annotations] in ApiAnnotation
643
644 | HsWildCardTy (XWildCardTy pass) -- A type wildcard
645 -- See Note [The wildcard story for types]
646 -- A anonymous wild card ('_'). A fresh Name is generated for
647 -- each individual anonymous wildcard during renaming
648 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
649
650 -- For details on above see note [Api annotations] in ApiAnnotation
651
652 -- For adding new constructors via Trees that Grow
653 | XHsType
654 (XXType pass)
655
656 data NewHsTypeX
657 = NHsCoreTy Type -- An escape hatch for tunnelling a *closed*
658 -- Core Type through HsSyn.
659 deriving Data
660 -- ^ - 'ApiAnnotation.AnnKeywordId' : None
661
662 instance Outputable NewHsTypeX where
663 ppr (NHsCoreTy ty) = ppr ty
664
665 type instance XForAllTy (GhcPass _) = NoExt
666 type instance XQualTy (GhcPass _) = NoExt
667 type instance XTyVar (GhcPass _) = NoExt
668 type instance XAppsTy (GhcPass _) = NoExt
669 type instance XAppTy (GhcPass _) = NoExt
670 type instance XFunTy (GhcPass _) = NoExt
671 type instance XListTy (GhcPass _) = NoExt
672 type instance XPArrTy (GhcPass _) = NoExt
673 type instance XTupleTy (GhcPass _) = NoExt
674 type instance XSumTy (GhcPass _) = NoExt
675 type instance XOpTy (GhcPass _) = NoExt
676 type instance XParTy (GhcPass _) = NoExt
677 type instance XIParamTy (GhcPass _) = NoExt
678 type instance XEqTy (GhcPass _) = NoExt
679 type instance XKindSig (GhcPass _) = NoExt
680
681 type instance XSpliceTy GhcPs = NoExt
682 type instance XSpliceTy GhcRn = NoExt
683 type instance XSpliceTy GhcTc = Kind
684
685 type instance XDocTy (GhcPass _) = NoExt
686 type instance XBangTy (GhcPass _) = NoExt
687 type instance XRecTy (GhcPass _) = NoExt
688
689 type instance XExplicitListTy GhcPs = NoExt
690 type instance XExplicitListTy GhcRn = NoExt
691 type instance XExplicitListTy GhcTc = Kind
692
693 type instance XExplicitTupleTy GhcPs = NoExt
694 type instance XExplicitTupleTy GhcRn = NoExt
695 type instance XExplicitTupleTy GhcTc = [Kind]
696
697 type instance XTyLit (GhcPass _) = NoExt
698
699 type instance XWildCardTy GhcPs = NoExt
700 type instance XWildCardTy GhcRn = HsWildCardInfo
701 type instance XWildCardTy GhcTc = HsWildCardInfo
702
703 type instance XXType (GhcPass _) = NewHsTypeX
704
705
706 -- Note [Literal source text] in BasicTypes for SourceText fields in
707 -- the following
708 -- | Haskell Type Literal
709 data HsTyLit
710 = HsNumTy SourceText Integer
711 | HsStrTy SourceText FastString
712 deriving Data
713
714 newtype HsWildCardInfo -- See Note [The wildcard story for types]
715 = AnonWildCard (Located Name)
716 deriving Data
717 -- A anonymous wild card ('_'). A fresh Name is generated for
718 -- each individual anonymous wildcard during renaming
719
720 -- | Located Haskell Application Type
721 type LHsAppType pass = Located (HsAppType pass)
722 -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnSimpleQuote'
723
724 -- | Haskell Application Type
725 data HsAppType pass
726 = HsAppInfix (XAppInfix pass)
727 (Located (IdP pass)) -- either a symbol or an id in backticks
728 | HsAppPrefix (XAppPrefix pass)
729 (LHsType pass) -- anything else, including things like (+)
730 | XAppType
731 (XXAppType pass)
732
733 type instance XAppInfix (GhcPass _) = NoExt
734 type instance XAppPrefix (GhcPass _) = NoExt
735 type instance XXAppType (GhcPass _) = NoExt
736
737 instance (p ~ GhcPass pass, OutputableBndrId p)
738 => Outputable (HsAppType p) where
739 ppr = ppr_app_ty
740
741 {-
742 Note [HsForAllTy tyvar binders]
743 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
744 After parsing:
745 * Implicit => empty
746 Explicit => the variables the user wrote
747
748 After renaming
749 * Implicit => the *type* variables free in the type
750 Explicit => the variables the user wrote (renamed)
751
752 Qualified currently behaves exactly as Implicit,
753 but it is deprecated to use it for implicit quantification.
754 In this case, GHC 7.10 gives a warning; see
755 Note [Context quantification] in RnTypes, and Trac #4426.
756 In GHC 8.0, Qualified will no longer bind variables
757 and this will become an error.
758
759 The kind variables bound in the hsq_implicit field come both
760 a) from the kind signatures on the kind vars (eg k1)
761 b) from the scope of the forall (eg k2)
762 Example: f :: forall (a::k1) b. T a (b::k2)
763
764
765 Note [Unit tuples]
766 ~~~~~~~~~~~~~~~~~~
767 Consider the type
768 type instance F Int = ()
769 We want to parse that "()"
770 as HsTupleTy HsBoxedOrConstraintTuple [],
771 NOT as HsTyVar unitTyCon
772
773 Why? Because F might have kind (* -> Constraint), so we when parsing we
774 don't know if that tuple is going to be a constraint tuple or an ordinary
775 unit tuple. The HsTupleSort flag is specifically designed to deal with
776 that, but it has to work for unit tuples too.
777
778 Note [Promotions (HsTyVar)]
779 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
780 HsTyVar: A name in a type or kind.
781 Here are the allowed namespaces for the name.
782 In a type:
783 Var: not allowed
784 Data: promoted data constructor
785 Tv: type variable
786 TcCls before renamer: type constructor, class constructor, or promoted data constructor
787 TcCls after renamer: type constructor or class constructor
788 In a kind:
789 Var, Data: not allowed
790 Tv: kind variable
791 TcCls: kind constructor or promoted type constructor
792
793 The 'Promoted' field in an HsTyVar captures whether the type was promoted in
794 the source code by prefixing an apostrophe.
795
796 Note [HsAppsTy]
797 ~~~~~~~~~~~~~~~
798 How to parse
799
800 Foo * Int
801
802 ? Is it `(*) Foo Int` or `Foo GHC.Types.* Int`? There's no way to know until renaming.
803 So we just take type expressions like this and put each component in a list, so be
804 sorted out in the renamer. The sorting out is done by RnTypes.mkHsOpTyRn. This means
805 that the parser should never produce HsAppTy or HsOpTy.
806
807 Note [Promoted lists and tuples]
808 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
809 Notice the difference between
810 HsListTy HsExplicitListTy
811 HsTupleTy HsExplicitListTupleTy
812
813 E.g. f :: [Int] HsListTy
814
815 g3 :: T '[] All these use
816 g2 :: T '[True] HsExplicitListTy
817 g1 :: T '[True,False]
818 g1a :: T [True,False] (can omit ' where unambiguous)
819
820 kind of T :: [Bool] -> * This kind uses HsListTy!
821
822 E.g. h :: (Int,Bool) HsTupleTy; f is a pair
823 k :: S '(True,False) HsExplicitTypleTy; S is indexed by
824 a type-level pair of booleans
825 kind of S :: (Bool,Bool) -> * This kind uses HsExplicitTupleTy
826
827 Note [Distinguishing tuple kinds]
828 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
829
830 Apart from promotion, tuples can have one of three different kinds:
831
832 x :: (Int, Bool) -- Regular boxed tuples
833 f :: Int# -> (# Int#, Int# #) -- Unboxed tuples
834 g :: (Eq a, Ord a) => a -- Constraint tuples
835
836 For convenience, internally we use a single constructor for all of these,
837 namely HsTupleTy, but keep track of the tuple kind (in the first argument to
838 HsTupleTy, a HsTupleSort). We can tell if a tuple is unboxed while parsing,
839 because of the #. However, with -XConstraintKinds we can only distinguish
840 between constraint and boxed tuples during type checking, in general. Hence the
841 four constructors of HsTupleSort:
842
843 HsUnboxedTuple -> Produced by the parser
844 HsBoxedTuple -> Certainly a boxed tuple
845 HsConstraintTuple -> Certainly a constraint tuple
846 HsBoxedOrConstraintTuple -> Could be a boxed or a constraint
847 tuple. Produced by the parser only,
848 disappears after type checking
849 -}
850
851 -- | Haskell Tuple Sort
852 data HsTupleSort = HsUnboxedTuple
853 | HsBoxedTuple
854 | HsConstraintTuple
855 | HsBoxedOrConstraintTuple
856 deriving Data
857
858
859 -- | Promoted data types.
860 data Promoted = Promoted
861 | NotPromoted
862 deriving (Data, Eq, Show)
863
864 -- | Located Constructor Declaration Field
865 type LConDeclField pass = Located (ConDeclField pass)
866 -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
867 -- in a list
868
869 -- For details on above see note [Api annotations] in ApiAnnotation
870
871 -- | Constructor Declaration Field
872 data ConDeclField pass -- Record fields have Haddoc docs on them
873 = ConDeclField { cd_fld_ext :: XConDeclField pass,
874 cd_fld_names :: [LFieldOcc pass],
875 -- ^ See Note [ConDeclField passs]
876 cd_fld_type :: LBangType pass,
877 cd_fld_doc :: Maybe LHsDocString }
878 -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
879
880 -- For details on above see note [Api annotations] in ApiAnnotation
881 | XConDeclField (XXConDeclField pass)
882
883 type instance XConDeclField (GhcPass _) = NoExt
884 type instance XXConDeclField (GhcPass _) = NoExt
885
886 instance (p ~ GhcPass pass, OutputableBndrId p)
887 => Outputable (ConDeclField p) where
888 ppr (ConDeclField _ fld_n fld_ty _) = ppr fld_n <+> dcolon <+> ppr fld_ty
889 ppr (XConDeclField x) = ppr x
890
891 -- HsConDetails is used for patterns/expressions *and* for data type
892 -- declarations
893 -- | Haskell Constructor Details
894 data HsConDetails arg rec
895 = PrefixCon [arg] -- C p1 p2 p3
896 | RecCon rec -- C { x = p1, y = p2 }
897 | InfixCon arg arg -- p1 `C` p2
898 deriving Data
899
900 instance (Outputable arg, Outputable rec)
901 => Outputable (HsConDetails arg rec) where
902 ppr (PrefixCon args) = text "PrefixCon" <+> ppr args
903 ppr (RecCon rec) = text "RecCon:" <+> ppr rec
904 ppr (InfixCon l r) = text "InfixCon:" <+> ppr [l, r]
905
906 {-
907 Note [ConDeclField passs]
908 ~~~~~~~~~~~~~~~~~~~~~~~~~
909
910 A ConDeclField contains a list of field occurrences: these always
911 include the field label as the user wrote it. After the renamer, it
912 will additionally contain the identity of the selector function in the
913 second component.
914
915 Due to DuplicateRecordFields, the OccName of the selector function
916 may have been mangled, which is why we keep the original field label
917 separately. For example, when DuplicateRecordFields is enabled
918
919 data T = MkT { x :: Int }
920
921 gives
922
923 ConDeclField { cd_fld_names = [L _ (FieldOcc "x" $sel:x:MkT)], ... }.
924 -}
925
926 -----------------------
927 -- A valid type must have a for-all at the top of the type, or of the fn arg
928 -- types
929
930 ---------------------
931 hsWcScopedTvs :: LHsSigWcType GhcRn -> [Name]
932 -- Get the lexically-scoped type variables of a HsSigType
933 -- - the explicitly-given forall'd type variables
934 -- - the implicitly-bound kind variables
935 -- - the named wildcars; see Note [Scoping of named wildcards]
936 -- because they scope in the same way
937 hsWcScopedTvs sig_ty
938 | HsWC { hswc_ext = nwcs, hswc_body = sig_ty1 } <- sig_ty
939 , HsIB { hsib_ext = HsIBRn { hsib_vars = vars}
940 , hsib_body = sig_ty2 } <- sig_ty1
941 = case sig_ty2 of
942 L _ (HsForAllTy { hst_bndrs = tvs }) -> vars ++ nwcs ++
943 map hsLTyVarName tvs
944 -- include kind variables only if the type is headed by forall
945 -- (this is consistent with GHC 7 behaviour)
946 _ -> nwcs
947 hsWcScopedTvs (HsWC _ (XHsImplicitBndrs _)) = panic "hsWcScopedTvs"
948 hsWcScopedTvs (XHsWildCardBndrs _) = panic "hsWcScopedTvs"
949
950 hsScopedTvs :: LHsSigType GhcRn -> [Name]
951 -- Same as hsWcScopedTvs, but for a LHsSigType
952 hsScopedTvs sig_ty
953 | HsIB { hsib_ext = HsIBRn { hsib_vars = vars }
954 , hsib_body = sig_ty2 } <- sig_ty
955 , L _ (HsForAllTy { hst_bndrs = tvs }) <- sig_ty2
956 = vars ++ map hsLTyVarName tvs
957 | otherwise
958 = []
959
960 {- Note [Scoping of named wildcards]
961 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
962 Consider
963 f :: _a -> _a
964 f x = let g :: _a -> _a
965 g = ...
966 in ...
967
968 Currently, for better or worse, the "_a" variables are all the same. So
969 although there is no explicit forall, the "_a" scopes over the definition.
970 I don't know if this is a good idea, but there it is.
971 -}
972
973 ---------------------
974 hsTyVarName :: HsTyVarBndr pass -> IdP pass
975 hsTyVarName (UserTyVar _ (L _ n)) = n
976 hsTyVarName (KindedTyVar _ (L _ n) _) = n
977 hsTyVarName (XTyVarBndr{}) = panic "hsTyVarName"
978
979 hsLTyVarName :: LHsTyVarBndr pass -> IdP pass
980 hsLTyVarName = hsTyVarName . unLoc
981
982 hsExplicitLTyVarNames :: LHsQTyVars pass -> [IdP pass]
983 -- Explicit variables only
984 hsExplicitLTyVarNames qtvs = map hsLTyVarName (hsQTvExplicit qtvs)
985
986 hsAllLTyVarNames :: LHsQTyVars GhcRn -> [Name]
987 -- All variables
988 hsAllLTyVarNames (HsQTvs { hsq_ext = HsQTvsRn { hsq_implicit = kvs }
989 , hsq_explicit = tvs })
990 = kvs ++ map hsLTyVarName tvs
991 hsAllLTyVarNames (XLHsQTyVars _) = panic "hsAllLTyVarNames"
992
993 hsLTyVarLocName :: LHsTyVarBndr pass -> Located (IdP pass)
994 hsLTyVarLocName = fmap hsTyVarName
995
996 hsLTyVarLocNames :: LHsQTyVars pass -> [Located (IdP pass)]
997 hsLTyVarLocNames qtvs = map hsLTyVarLocName (hsQTvExplicit qtvs)
998
999 -- | Convert a LHsTyVarBndr to an equivalent LHsType.
1000 hsLTyVarBndrToType :: LHsTyVarBndr (GhcPass p) -> LHsType (GhcPass p)
1001 hsLTyVarBndrToType = fmap cvt
1002 where cvt (UserTyVar _ n) = HsTyVar noExt NotPromoted n
1003 cvt (KindedTyVar _ (L name_loc n) kind)
1004 = HsKindSig noExt
1005 (L name_loc (HsTyVar noExt NotPromoted (L name_loc n))) kind
1006 cvt (XTyVarBndr{}) = panic "hsLTyVarBndrToType"
1007
1008 -- | Convert a LHsTyVarBndrs to a list of types.
1009 -- Works on *type* variable only, no kind vars.
1010 hsLTyVarBndrsToTypes :: LHsQTyVars (GhcPass p) -> [LHsType (GhcPass p)]
1011 hsLTyVarBndrsToTypes (HsQTvs { hsq_explicit = tvbs }) = map hsLTyVarBndrToType tvbs
1012 hsLTyVarBndrsToTypes (XLHsQTyVars _) = panic "hsLTyVarBndrsToTypes"
1013
1014 ---------------------
1015 wildCardName :: HsWildCardInfo -> Name
1016 wildCardName (AnonWildCard (L _ n)) = n
1017
1018 -- Two wild cards are the same when they have the same location
1019 sameWildCard :: Located HsWildCardInfo -> Located HsWildCardInfo -> Bool
1020 sameWildCard (L l1 (AnonWildCard _)) (L l2 (AnonWildCard _)) = l1 == l2
1021
1022 ignoreParens :: LHsType pass -> LHsType pass
1023 ignoreParens (L _ (HsParTy _ ty)) = ignoreParens ty
1024 ignoreParens ty = ty
1025
1026 isLHsForAllTy :: LHsType p -> Bool
1027 isLHsForAllTy (L _ (HsForAllTy {})) = True
1028 isLHsForAllTy _ = False
1029
1030 {-
1031 ************************************************************************
1032 * *
1033 Building types
1034 * *
1035 ************************************************************************
1036 -}
1037
1038 mkAnonWildCardTy :: HsType GhcPs
1039 mkAnonWildCardTy = HsWildCardTy noExt
1040
1041 mkHsOpTy :: LHsType (GhcPass p) -> Located (IdP (GhcPass p))
1042 -> LHsType (GhcPass p) -> HsType (GhcPass p)
1043 mkHsOpTy ty1 op ty2 = HsOpTy noExt ty1 op ty2
1044
1045 mkHsAppTy :: LHsType (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
1046 mkHsAppTy t1 t2
1047 = addCLoc t1 t2 (HsAppTy noExt t1 (parenthesizeHsType appPrec t2))
1048
1049 mkHsAppTys :: LHsType (GhcPass p) -> [LHsType (GhcPass p)]
1050 -> LHsType (GhcPass p)
1051 mkHsAppTys = foldl mkHsAppTy
1052
1053 mkHsAppsTy :: [LHsAppType GhcPs] -> HsType GhcPs
1054 -- In the common case of a singleton non-operator,
1055 -- avoid the clutter of wrapping in a HsAppsTy
1056 mkHsAppsTy [L _ (HsAppPrefix _ (L _ ty))] = ty
1057 mkHsAppsTy app_tys = HsAppsTy NoExt app_tys
1058
1059 {-
1060 ************************************************************************
1061 * *
1062 Decomposing HsTypes
1063 * *
1064 ************************************************************************
1065 -}
1066
1067 ---------------------------------
1068 -- splitHsFunType decomposes a type (t1 -> t2 ... -> tn)
1069 -- Breaks up any parens in the result type:
1070 -- splitHsFunType (a -> (b -> c)) = ([a,b], c)
1071 -- Also deals with (->) t1 t2; that is why it only works on LHsType Name
1072 -- (see Trac #9096)
1073 splitHsFunType :: LHsType GhcRn -> ([LHsType GhcRn], LHsType GhcRn)
1074 splitHsFunType (L _ (HsParTy _ ty))
1075 = splitHsFunType ty
1076
1077 splitHsFunType (L _ (HsFunTy _ x y))
1078 | (args, res) <- splitHsFunType y
1079 = (x:args, res)
1080
1081 splitHsFunType orig_ty@(L _ (HsAppTy _ t1 t2))
1082 = go t1 [t2]
1083 where -- Look for (->) t1 t2, possibly with parenthesisation
1084 go (L _ (HsTyVar _ _ (L _ fn))) tys | fn == funTyConName
1085 , [t1,t2] <- tys
1086 , (args, res) <- splitHsFunType t2
1087 = (t1:args, res)
1088 go (L _ (HsAppTy _ t1 t2)) tys = go t1 (t2:tys)
1089 go (L _ (HsParTy _ ty)) tys = go ty tys
1090 go _ _ = ([], orig_ty) -- Failure to match
1091
1092 splitHsFunType other = ([], other)
1093
1094 --------------------------------
1095 -- | Retrieves the head of an HsAppsTy, if this can be done unambiguously,
1096 -- without consulting fixities.
1097 getAppsTyHead_maybe :: [LHsAppType (GhcPass p)]
1098 -> Maybe ( LHsType (GhcPass p)
1099 , [LHsType (GhcPass p)], LexicalFixity)
1100 getAppsTyHead_maybe tys = case splitHsAppsTy tys of
1101 ([app1:apps], []) -> -- no symbols, some normal types
1102 Just (mkHsAppTys app1 apps, [], Prefix)
1103 ([app1l:appsl, app1r:appsr], [L loc op]) -> -- one operator
1104 Just ( L loc (HsTyVar noExt NotPromoted (L loc op))
1105 , [mkHsAppTys app1l appsl, mkHsAppTys app1r appsr], Infix)
1106 _ -> -- can't figure it out
1107 Nothing
1108
1109 -- | Splits a [HsAppType pass] (the payload of an HsAppsTy) into regions of
1110 -- prefix types (normal types) and infix operators.
1111 -- If @splitHsAppsTy tys = (non_syms, syms)@, then @tys@ starts with the first
1112 -- element of @non_syms@ followed by the first element of @syms@ followed by
1113 -- the next element of @non_syms@, etc. It is guaranteed that the non_syms list
1114 -- has one more element than the syms list.
1115 splitHsAppsTy :: [LHsAppType pass] -> ([[LHsType pass]], [Located (IdP pass)])
1116 splitHsAppsTy = go [] [] []
1117 where
1118 go acc acc_non acc_sym [] = (reverse (reverse acc : acc_non), reverse acc_sym)
1119 go acc acc_non acc_sym (L _ (HsAppPrefix _ ty) : rest)
1120 = go (ty : acc) acc_non acc_sym rest
1121 go acc acc_non acc_sym (L _ (HsAppInfix _ op) : rest)
1122 = go [] (reverse acc : acc_non) (op : acc_sym) rest
1123 go _ _ _ (L _ (XAppType _):_) = panic "splitHsAppsTy"
1124
1125 -- Retrieve the name of the "head" of a nested type application
1126 -- somewhat like splitHsAppTys, but a little more thorough
1127 -- used to examine the result of a GADT-like datacon, so it doesn't handle
1128 -- *all* cases (like lists, tuples, (~), etc.)
1129 hsTyGetAppHead_maybe :: LHsType (GhcPass p)
1130 -> Maybe (Located (IdP (GhcPass p)), [LHsType (GhcPass p)])
1131 hsTyGetAppHead_maybe = go []
1132 where
1133 go tys (L _ (HsTyVar _ _ ln)) = Just (ln, tys)
1134 go tys (L _ (HsAppsTy _ apps))
1135 | Just (head, args, _) <- getAppsTyHead_maybe apps
1136 = go (args ++ tys) head
1137 go tys (L _ (HsAppTy _ l r)) = go (r : tys) l
1138 go tys (L _ (HsOpTy _ l (L loc n) r)) = Just (L loc n, l : r : tys)
1139 go tys (L _ (HsParTy _ t)) = go tys t
1140 go tys (L _ (HsKindSig _ t _)) = go tys t
1141 go _ _ = Nothing
1142
1143 splitHsAppTys :: LHsType GhcRn -> [LHsType GhcRn]
1144 -> (LHsType GhcRn, [LHsType GhcRn])
1145 -- no need to worry about HsAppsTy here
1146 splitHsAppTys (L _ (HsAppTy _ f a)) as = splitHsAppTys f (a:as)
1147 splitHsAppTys (L _ (HsParTy _ f)) as = splitHsAppTys f as
1148 splitHsAppTys f as = (f,as)
1149
1150 --------------------------------
1151 splitLHsPatSynTy :: LHsType pass
1152 -> ( [LHsTyVarBndr pass] -- universals
1153 , LHsContext pass -- required constraints
1154 , [LHsTyVarBndr pass] -- existentials
1155 , LHsContext pass -- provided constraints
1156 , LHsType pass) -- body type
1157 splitLHsPatSynTy ty = (univs, reqs, exis, provs, ty4)
1158 where
1159 (univs, ty1) = splitLHsForAllTy ty
1160 (reqs, ty2) = splitLHsQualTy ty1
1161 (exis, ty3) = splitLHsForAllTy ty2
1162 (provs, ty4) = splitLHsQualTy ty3
1163
1164 splitLHsSigmaTy :: LHsType pass
1165 -> ([LHsTyVarBndr pass], LHsContext pass, LHsType pass)
1166 splitLHsSigmaTy ty
1167 | (tvs, ty1) <- splitLHsForAllTy ty
1168 , (ctxt, ty2) <- splitLHsQualTy ty1
1169 = (tvs, ctxt, ty2)
1170
1171 splitLHsForAllTy :: LHsType pass -> ([LHsTyVarBndr pass], LHsType pass)
1172 splitLHsForAllTy (L _ (HsParTy _ ty)) = splitLHsForAllTy ty
1173 splitLHsForAllTy (L _ (HsForAllTy { hst_bndrs = tvs, hst_body = body })) = (tvs, body)
1174 splitLHsForAllTy body = ([], body)
1175
1176 splitLHsQualTy :: LHsType pass -> (LHsContext pass, LHsType pass)
1177 splitLHsQualTy (L _ (HsParTy _ ty)) = splitLHsQualTy ty
1178 splitLHsQualTy (L _ (HsQualTy { hst_ctxt = ctxt, hst_body = body })) = (ctxt, body)
1179 splitLHsQualTy body = (noLoc [], body)
1180
1181 splitLHsInstDeclTy :: LHsSigType GhcRn
1182 -> ([Name], LHsContext GhcRn, LHsType GhcRn)
1183 -- Split up an instance decl type, returning the pieces
1184 splitLHsInstDeclTy (HsIB { hsib_ext = HsIBRn { hsib_vars = itkvs }
1185 , hsib_body = inst_ty })
1186 | (tvs, cxt, body_ty) <- splitLHsSigmaTy inst_ty
1187 = (itkvs ++ map hsLTyVarName tvs, cxt, body_ty)
1188 -- Return implicitly bound type and kind vars
1189 -- For an instance decl, all of them are in scope
1190 splitLHsInstDeclTy (XHsImplicitBndrs _) = panic "splitLHsInstDeclTy"
1191
1192 getLHsInstDeclHead :: LHsSigType pass -> LHsType pass
1193 getLHsInstDeclHead inst_ty
1194 | (_tvs, _cxt, body_ty) <- splitLHsSigmaTy (hsSigType inst_ty)
1195 = body_ty
1196
1197 getLHsInstDeclClass_maybe :: LHsSigType (GhcPass p)
1198 -> Maybe (Located (IdP (GhcPass p)))
1199 -- Works on (HsSigType RdrName)
1200 getLHsInstDeclClass_maybe inst_ty
1201 = do { let head_ty = getLHsInstDeclHead inst_ty
1202 ; (cls, _) <- hsTyGetAppHead_maybe head_ty
1203 ; return cls }
1204
1205 {-
1206 ************************************************************************
1207 * *
1208 FieldOcc
1209 * *
1210 ************************************************************************
1211 -}
1212
1213 -- | Located Field Occurrence
1214 type LFieldOcc pass = Located (FieldOcc pass)
1215
1216 -- | Field Occurrence
1217 --
1218 -- Represents an *occurrence* of an unambiguous field. We store
1219 -- both the 'RdrName' the user originally wrote, and after the
1220 -- renamer, the selector function.
1221 data FieldOcc pass = FieldOcc { extFieldOcc :: XFieldOcc pass
1222 , rdrNameFieldOcc :: Located RdrName
1223 -- ^ See Note [Located RdrNames] in HsExpr
1224 }
1225
1226 | XFieldOcc
1227 (XXFieldOcc pass)
1228 deriving instance (p ~ GhcPass pass, Eq (XFieldOcc p)) => Eq (FieldOcc p)
1229 deriving instance (p ~ GhcPass pass, Ord (XFieldOcc p)) => Ord (FieldOcc p)
1230
1231 type instance XFieldOcc GhcPs = NoExt
1232 type instance XFieldOcc GhcRn = Name
1233 type instance XFieldOcc GhcTc = Id
1234
1235 type instance XXFieldOcc (GhcPass _) = NoExt
1236
1237 instance Outputable (FieldOcc pass) where
1238 ppr = ppr . rdrNameFieldOcc
1239
1240 mkFieldOcc :: Located RdrName -> FieldOcc GhcPs
1241 mkFieldOcc rdr = FieldOcc noExt rdr
1242
1243
1244 -- | Ambiguous Field Occurrence
1245 --
1246 -- Represents an *occurrence* of a field that is potentially
1247 -- ambiguous after the renamer, with the ambiguity resolved by the
1248 -- typechecker. We always store the 'RdrName' that the user
1249 -- originally wrote, and store the selector function after the renamer
1250 -- (for unambiguous occurrences) or the typechecker (for ambiguous
1251 -- occurrences).
1252 --
1253 -- See Note [HsRecField and HsRecUpdField] in HsPat and
1254 -- Note [Disambiguating record fields] in TcExpr.
1255 -- See Note [Located RdrNames] in HsExpr
1256 data AmbiguousFieldOcc pass
1257 = Unambiguous (XUnambiguous pass) (Located RdrName)
1258 | Ambiguous (XAmbiguous pass) (Located RdrName)
1259 | XAmbiguousFieldOcc (XXAmbiguousFieldOcc pass)
1260
1261 type instance XUnambiguous GhcPs = NoExt
1262 type instance XUnambiguous GhcRn = Name
1263 type instance XUnambiguous GhcTc = Id
1264
1265 type instance XAmbiguous GhcPs = NoExt
1266 type instance XAmbiguous GhcRn = NoExt
1267 type instance XAmbiguous GhcTc = Id
1268
1269 type instance XXAmbiguousFieldOcc (GhcPass _) = NoExt
1270
1271 instance p ~ GhcPass pass => Outputable (AmbiguousFieldOcc p) where
1272 ppr = ppr . rdrNameAmbiguousFieldOcc
1273
1274 instance p ~ GhcPass pass => OutputableBndr (AmbiguousFieldOcc p) where
1275 pprInfixOcc = pprInfixOcc . rdrNameAmbiguousFieldOcc
1276 pprPrefixOcc = pprPrefixOcc . rdrNameAmbiguousFieldOcc
1277
1278 mkAmbiguousFieldOcc :: Located RdrName -> AmbiguousFieldOcc GhcPs
1279 mkAmbiguousFieldOcc rdr = Unambiguous noExt rdr
1280
1281 rdrNameAmbiguousFieldOcc :: AmbiguousFieldOcc (GhcPass p) -> RdrName
1282 rdrNameAmbiguousFieldOcc (Unambiguous _ (L _ rdr)) = rdr
1283 rdrNameAmbiguousFieldOcc (Ambiguous _ (L _ rdr)) = rdr
1284 rdrNameAmbiguousFieldOcc (XAmbiguousFieldOcc _)
1285 = panic "rdrNameAmbiguousFieldOcc"
1286
1287 selectorAmbiguousFieldOcc :: AmbiguousFieldOcc GhcTc -> Id
1288 selectorAmbiguousFieldOcc (Unambiguous sel _) = sel
1289 selectorAmbiguousFieldOcc (Ambiguous sel _) = sel
1290 selectorAmbiguousFieldOcc (XAmbiguousFieldOcc _)
1291 = panic "selectorAmbiguousFieldOcc"
1292
1293 unambiguousFieldOcc :: AmbiguousFieldOcc GhcTc -> FieldOcc GhcTc
1294 unambiguousFieldOcc (Unambiguous rdr sel) = FieldOcc rdr sel
1295 unambiguousFieldOcc (Ambiguous rdr sel) = FieldOcc rdr sel
1296 unambiguousFieldOcc (XAmbiguousFieldOcc _) = panic "unambiguousFieldOcc"
1297
1298 ambiguousFieldOcc :: FieldOcc GhcTc -> AmbiguousFieldOcc GhcTc
1299 ambiguousFieldOcc (FieldOcc sel rdr) = Unambiguous sel rdr
1300 ambiguousFieldOcc (XFieldOcc _) = panic "ambiguousFieldOcc"
1301
1302 {-
1303 ************************************************************************
1304 * *
1305 \subsection{Pretty printing}
1306 * *
1307 ************************************************************************
1308 -}
1309
1310 instance (p ~ GhcPass pass, OutputableBndrId p) => Outputable (HsType p) where
1311 ppr ty = pprHsType ty
1312
1313 instance Outputable HsTyLit where
1314 ppr = ppr_tylit
1315
1316 instance (p ~ GhcPass pass, OutputableBndrId p)
1317 => Outputable (LHsQTyVars p) where
1318 ppr (HsQTvs { hsq_explicit = tvs }) = interppSP tvs
1319 ppr (XLHsQTyVars x) = ppr x
1320
1321 instance (p ~ GhcPass pass, OutputableBndrId p)
1322 => Outputable (HsTyVarBndr p) where
1323 ppr (UserTyVar _ n) = ppr n
1324 ppr (KindedTyVar _ n k) = parens $ hsep [ppr n, dcolon, ppr k]
1325 ppr (XTyVarBndr n) = ppr n
1326
1327 instance (p ~ GhcPass pass,Outputable thing)
1328 => Outputable (HsImplicitBndrs p thing) where
1329 ppr (HsIB { hsib_body = ty }) = ppr ty
1330 ppr (XHsImplicitBndrs x) = ppr x
1331
1332 instance (p ~ GhcPass pass,Outputable thing)
1333 => Outputable (HsWildCardBndrs p thing) where
1334 ppr (HsWC { hswc_body = ty }) = ppr ty
1335 ppr (XHsWildCardBndrs x) = ppr x
1336
1337 instance Outputable HsWildCardInfo where
1338 ppr (AnonWildCard _) = char '_'
1339
1340 pprAnonWildCard :: SDoc
1341 pprAnonWildCard = char '_'
1342
1343 pprHsForAll :: (OutputableBndrId (GhcPass p))
1344 => [LHsTyVarBndr (GhcPass p)] -> LHsContext (GhcPass p) -> SDoc
1345 pprHsForAll = pprHsForAllExtra Nothing
1346
1347 -- | Version of 'pprHsForAll' that can also print an extra-constraints
1348 -- wildcard, e.g. @_ => a -> Bool@ or @(Show a, _) => a -> String@. This
1349 -- underscore will be printed when the 'Maybe SrcSpan' argument is a 'Just'
1350 -- containing the location of the extra-constraints wildcard. A special
1351 -- function for this is needed, as the extra-constraints wildcard is removed
1352 -- from the actual context and type, and stored in a separate field, thus just
1353 -- printing the type will not print the extra-constraints wildcard.
1354 pprHsForAllExtra :: (OutputableBndrId (GhcPass p))
1355 => Maybe SrcSpan -> [LHsTyVarBndr (GhcPass p)]
1356 -> LHsContext (GhcPass p) -> SDoc
1357 pprHsForAllExtra extra qtvs cxt
1358 = pprHsForAllTvs qtvs <+> pprHsContextExtra show_extra (unLoc cxt)
1359 where
1360 show_extra = isJust extra
1361
1362 pprHsForAllTvs :: (OutputableBndrId (GhcPass p))
1363 => [LHsTyVarBndr (GhcPass p)] -> SDoc
1364 pprHsForAllTvs qtvs
1365 | null qtvs = whenPprDebug (forAllLit <+> dot)
1366 | otherwise = forAllLit <+> interppSP qtvs <> dot
1367
1368 pprHsContext :: (OutputableBndrId (GhcPass p)) => HsContext (GhcPass p) -> SDoc
1369 pprHsContext = maybe empty (<+> darrow) . pprHsContextMaybe
1370
1371 pprHsContextNoArrow :: (OutputableBndrId (GhcPass p))
1372 => HsContext (GhcPass p) -> SDoc
1373 pprHsContextNoArrow = fromMaybe empty . pprHsContextMaybe
1374
1375 pprHsContextMaybe :: (OutputableBndrId (GhcPass p))
1376 => HsContext (GhcPass p) -> Maybe SDoc
1377 pprHsContextMaybe [] = Nothing
1378 pprHsContextMaybe [L _ pred] = Just $ ppr_mono_ty pred
1379 pprHsContextMaybe cxt = Just $ parens (interpp'SP cxt)
1380
1381 -- For use in a HsQualTy, which always gets printed if it exists.
1382 pprHsContextAlways :: (OutputableBndrId (GhcPass p))
1383 => HsContext (GhcPass p) -> SDoc
1384 pprHsContextAlways [] = parens empty <+> darrow
1385 pprHsContextAlways [L _ ty] = ppr_mono_ty ty <+> darrow
1386 pprHsContextAlways cxt = parens (interpp'SP cxt) <+> darrow
1387
1388 -- True <=> print an extra-constraints wildcard, e.g. @(Show a, _) =>@
1389 pprHsContextExtra :: (OutputableBndrId (GhcPass p))
1390 => Bool -> HsContext (GhcPass p) -> SDoc
1391 pprHsContextExtra show_extra ctxt
1392 | not show_extra
1393 = pprHsContext ctxt
1394 | null ctxt
1395 = char '_' <+> darrow
1396 | otherwise
1397 = parens (sep (punctuate comma ctxt')) <+> darrow
1398 where
1399 ctxt' = map ppr ctxt ++ [char '_']
1400
1401 pprConDeclFields :: (OutputableBndrId (GhcPass p))
1402 => [LConDeclField (GhcPass p)] -> SDoc
1403 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
1404 where
1405 ppr_fld (L _ (ConDeclField { cd_fld_names = ns, cd_fld_type = ty,
1406 cd_fld_doc = doc }))
1407 = ppr_names ns <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
1408 ppr_fld (L _ (XConDeclField x)) = ppr x
1409 ppr_names [n] = ppr n
1410 ppr_names ns = sep (punctuate comma (map ppr ns))
1411
1412 {-
1413 Note [Printing KindedTyVars]
1414 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1415 Trac #3830 reminded me that we should really only print the kind
1416 signature on a KindedTyVar if the kind signature was put there by the
1417 programmer. During kind inference GHC now adds a PostTcKind to UserTyVars,
1418 rather than converting to KindedTyVars as before.
1419
1420 (As it happens, the message in #3830 comes out a different way now,
1421 and the problem doesn't show up; but having the flag on a KindedTyVar
1422 seems like the Right Thing anyway.)
1423 -}
1424
1425 -- Printing works more-or-less as for Types
1426
1427 pprHsType :: (OutputableBndrId (GhcPass p)) => HsType (GhcPass p) -> SDoc
1428 pprHsType ty = ppr_mono_ty ty
1429
1430 ppr_mono_lty :: (OutputableBndrId (GhcPass p)) => LHsType (GhcPass p) -> SDoc
1431 ppr_mono_lty ty = ppr_mono_ty (unLoc ty)
1432
1433 ppr_mono_ty :: (OutputableBndrId (GhcPass p)) => HsType (GhcPass p) -> SDoc
1434 ppr_mono_ty (HsForAllTy { hst_bndrs = tvs, hst_body = ty })
1435 = sep [pprHsForAllTvs tvs, ppr_mono_lty ty]
1436
1437 ppr_mono_ty (HsQualTy { hst_ctxt = L _ ctxt, hst_body = ty })
1438 = sep [pprHsContextAlways ctxt, ppr_mono_lty ty]
1439
1440 ppr_mono_ty (HsBangTy _ b ty) = ppr b <> ppr_mono_lty ty
1441 ppr_mono_ty (HsRecTy _ flds) = pprConDeclFields flds
1442 ppr_mono_ty (HsTyVar _ NotPromoted (L _ name))= pprPrefixOcc name
1443 ppr_mono_ty (HsTyVar _ Promoted (L _ name))
1444 = space <> quote (pprPrefixOcc name)
1445 -- We need a space before the ' above, so the parser
1446 -- does not attach it to the previous symbol
1447 ppr_mono_ty (HsFunTy _ ty1 ty2) = ppr_fun_ty ty1 ty2
1448 ppr_mono_ty (HsTupleTy _ con tys) = tupleParens std_con (pprWithCommas ppr tys)
1449 where std_con = case con of
1450 HsUnboxedTuple -> UnboxedTuple
1451 _ -> BoxedTuple
1452 ppr_mono_ty (HsSumTy _ tys)
1453 = tupleParens UnboxedTuple (pprWithBars ppr tys)
1454 ppr_mono_ty (HsKindSig _ ty kind)
1455 = parens (ppr_mono_lty ty <+> dcolon <+> ppr kind)
1456 ppr_mono_ty (HsListTy _ ty) = brackets (ppr_mono_lty ty)
1457 ppr_mono_ty (HsPArrTy _ ty) = paBrackets (ppr_mono_lty ty)
1458 ppr_mono_ty (HsIParamTy _ n ty) = (ppr n <+> dcolon <+> ppr_mono_lty ty)
1459 ppr_mono_ty (HsSpliceTy _ s) = pprSplice s
1460 ppr_mono_ty (HsExplicitListTy _ Promoted tys)
1461 = quote $ brackets (interpp'SP tys)
1462 ppr_mono_ty (HsExplicitListTy _ NotPromoted tys)
1463 = brackets (interpp'SP tys)
1464 ppr_mono_ty (HsExplicitTupleTy _ tys) = quote $ parens (interpp'SP tys)
1465 ppr_mono_ty (HsTyLit _ t) = ppr_tylit t
1466 ppr_mono_ty (HsWildCardTy {}) = char '_'
1467
1468 ppr_mono_ty (HsEqTy _ ty1 ty2)
1469 = ppr_mono_lty ty1 <+> char '~' <+> ppr_mono_lty ty2
1470
1471 ppr_mono_ty (HsAppsTy _ tys)
1472 = hsep (map (ppr_app_ty . unLoc) tys)
1473
1474 ppr_mono_ty (HsAppTy _ fun_ty arg_ty)
1475 = hsep [ppr_mono_lty fun_ty, ppr_mono_lty arg_ty]
1476
1477 ppr_mono_ty (HsOpTy _ ty1 (L _ op) ty2)
1478 = sep [ ppr_mono_lty ty1
1479 , sep [pprInfixOcc op, ppr_mono_lty ty2 ] ]
1480
1481 ppr_mono_ty (HsParTy _ ty)
1482 = parens (ppr_mono_lty ty)
1483 -- Put the parens in where the user did
1484 -- But we still use the precedence stuff to add parens because
1485 -- toHsType doesn't put in any HsParTys, so we may still need them
1486
1487 ppr_mono_ty (HsDocTy _ ty doc)
1488 -- AZ: Should we add parens? Should we introduce "-- ^"?
1489 = ppr_mono_lty ty <+> ppr (unLoc doc)
1490 -- we pretty print Haddock comments on types as if they were
1491 -- postfix operators
1492
1493 ppr_mono_ty (XHsType t) = ppr t
1494
1495 --------------------------
1496 ppr_fun_ty :: (OutputableBndrId (GhcPass p))
1497 => LHsType (GhcPass p) -> LHsType (GhcPass p) -> SDoc
1498 ppr_fun_ty ty1 ty2
1499 = let p1 = ppr_mono_lty ty1
1500 p2 = ppr_mono_lty ty2
1501 in
1502 sep [p1, text "->" <+> p2]
1503
1504 --------------------------
1505 ppr_app_ty :: (OutputableBndrId (GhcPass p)) => HsAppType (GhcPass p) -> SDoc
1506 ppr_app_ty (HsAppInfix _ (L _ n)) = pprInfixOcc n
1507 ppr_app_ty (HsAppPrefix _ (L _ (HsTyVar _ NotPromoted (L _ n))))
1508 = pprPrefixOcc n
1509 ppr_app_ty (HsAppPrefix _ (L _ (HsTyVar _ Promoted (L _ n))))
1510 = space <> quote (pprPrefixOcc n) -- We need a space before the ' above, so
1511 -- the parser does not attach it to the
1512 -- previous symbol
1513 ppr_app_ty (HsAppPrefix _ ty) = ppr_mono_lty ty
1514
1515 ppr_app_ty (XAppType ty) = ppr ty
1516
1517 --------------------------
1518 ppr_tylit :: HsTyLit -> SDoc
1519 ppr_tylit (HsNumTy _ i) = integer i
1520 ppr_tylit (HsStrTy _ s) = text (show s)
1521
1522
1523 -- | @'hsTypeNeedsParens' p t@ returns 'True' if the type @t@ needs parentheses
1524 -- under precedence @p@.
1525 hsTypeNeedsParens :: PprPrec -> HsType pass -> Bool
1526 hsTypeNeedsParens p = go
1527 where
1528 go (HsForAllTy{}) = False
1529 go (HsQualTy{}) = False
1530 go (HsBangTy{}) = p > topPrec
1531 go (HsRecTy{}) = False
1532 go (HsTyVar{}) = False
1533 go (HsFunTy{}) = p >= funPrec
1534 go (HsTupleTy{}) = False
1535 go (HsSumTy{}) = False
1536 go (HsKindSig{}) = False
1537 go (HsListTy{}) = False
1538 go (HsPArrTy{}) = False
1539 go (HsIParamTy{}) = p > topPrec
1540 go (HsSpliceTy{}) = False
1541 go (HsExplicitListTy{}) = False
1542 go (HsExplicitTupleTy{}) = False
1543 go (HsTyLit{}) = False
1544 go (HsWildCardTy{}) = False
1545 go (HsEqTy{}) = p >= opPrec
1546 go (HsAppsTy _ args) = p >= appPrec && not (null args)
1547 go (HsAppTy{}) = p >= appPrec
1548 go (HsOpTy{}) = p >= opPrec
1549 go (HsParTy{}) = False
1550 go (HsDocTy _ (L _ t) _) = go t
1551 go (XHsType{}) = False
1552
1553 -- | @'parenthesizeHsType' p ty@ checks if @'hsTypeNeedsParens' p ty@ is
1554 -- true, and if so, surrounds @ty@ with an 'HsParTy'. Otherwise, it simply
1555 -- returns @ty@.
1556 parenthesizeHsType :: PprPrec -> LHsType (GhcPass p) -> LHsType (GhcPass p)
1557 parenthesizeHsType p lty@(L loc ty)
1558 | hsTypeNeedsParens p ty = L loc (HsParTy NoExt lty)
1559 | otherwise = lty