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