never executed always true always false
1
2 {-# LANGUAGE ConstraintKinds #-}
3 {-# LANGUAGE DataKinds #-}
4 {-# LANGUAGE DeriveDataTypeable #-}
5 {-# LANGUAGE FlexibleContexts #-}
6 {-# LANGUAGE FlexibleInstances #-}
7 {-# LANGUAGE ScopedTypeVariables #-}
8 {-# LANGUAGE TypeApplications #-}
9 {-# LANGUAGE TypeFamilies #-}
10 {-# LANGUAGE UndecidableInstances #-} -- Wrinkle in Note [Trees That Grow]
11 -- in module Language.Haskell.Syntax.Extension
12 {-# LANGUAGE ViewPatterns #-}
13
14 {-
15 (c) The University of Glasgow 2006
16 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
17 -}
18
19
20 {-# OPTIONS_GHC -Wno-incomplete-record-updates #-}
21
22 -- See Note [Language.Haskell.Syntax.* Hierarchy] for why not GHC.Hs.*
23
24 -- | Abstract syntax of global declarations.
25 --
26 -- Definitions for: @SynDecl@ and @ConDecl@, @ClassDecl@,
27 -- @InstDecl@, @DefaultDecl@ and @ForeignDecl@.
28 module Language.Haskell.Syntax.Decls (
29 -- * Toplevel declarations
30 HsDecl(..), LHsDecl, HsDataDefn(..), HsDeriving, LHsFunDep, FunDep(..),
31 HsDerivingClause(..), LHsDerivingClause, DerivClauseTys(..), LDerivClauseTys,
32 NewOrData(..), newOrDataToFlavour,
33 StandaloneKindSig(..), LStandaloneKindSig,
34
35 -- ** Class or type declarations
36 TyClDecl(..), LTyClDecl, DataDeclRn(..),
37 TyClGroup(..),
38 tyClGroupTyClDecls, tyClGroupInstDecls, tyClGroupRoleDecls,
39 tyClGroupKindSigs,
40 isClassDecl, isDataDecl, isSynDecl,
41 isFamilyDecl, isTypeFamilyDecl, isDataFamilyDecl,
42 isOpenTypeFamilyInfo, isClosedTypeFamilyInfo,
43 countTyClDecls,
44 tyClDeclTyVars,
45 FamilyDecl(..), LFamilyDecl,
46
47 -- ** Instance declarations
48 InstDecl(..), LInstDecl, FamilyInfo(..), pprFlavour,
49 TyFamInstDecl(..), LTyFamInstDecl,
50 TyFamDefltDecl, LTyFamDefltDecl,
51 DataFamInstDecl(..), LDataFamInstDecl,
52 FamEqn(..), TyFamInstEqn, LTyFamInstEqn, HsTyPats,
53 LClsInstDecl, ClsInstDecl(..),
54
55 -- ** Standalone deriving declarations
56 DerivDecl(..), LDerivDecl,
57 -- ** Deriving strategies
58 DerivStrategy(..), LDerivStrategy,
59 derivStrategyName,
60 -- ** @RULE@ declarations
61 LRuleDecls,RuleDecls(..),RuleDecl(..),LRuleDecl,HsRuleRn(..),
62 RuleBndr(..),LRuleBndr,
63 collectRuleBndrSigTys,
64 pprFullRuleName,
65 -- ** @default@ declarations
66 DefaultDecl(..), LDefaultDecl,
67 -- ** Template haskell declaration splice
68 SpliceExplicitFlag(..),
69 SpliceDecl(..), LSpliceDecl,
70 -- ** Foreign function interface declarations
71 ForeignDecl(..), LForeignDecl, ForeignImport(..), ForeignExport(..),
72 CImportSpec(..),
73 -- ** Data-constructor declarations
74 ConDecl(..), LConDecl,
75 HsConDeclH98Details, HsConDeclGADTDetails(..),
76 -- ** Document comments
77 DocDecl(..), LDocDecl, docDeclDoc,
78 -- ** Deprecations
79 WarnDecl(..), LWarnDecl,
80 WarnDecls(..), LWarnDecls,
81 -- ** Annotations
82 AnnDecl(..), LAnnDecl,
83 AnnProvenance(..), annProvenanceName_maybe,
84 -- ** Role annotations
85 RoleAnnotDecl(..), LRoleAnnotDecl,
86 -- ** Injective type families
87 FamilyResultSig(..), LFamilyResultSig, InjectivityAnn(..), LInjectivityAnn,
88
89 -- * Grouping
90 HsGroup(..), hsGroupInstDecls,
91 ) where
92
93 -- friends:
94 import GHC.Prelude
95
96 import {-# SOURCE #-} Language.Haskell.Syntax.Expr
97 ( HsExpr, HsSplice )
98 -- Because Expr imports Decls via HsBracket
99
100 import Language.Haskell.Syntax.Binds
101 import Language.Haskell.Syntax.Type
102 import GHC.Hs.Doc
103 import GHC.Core.TyCon
104 import GHC.Types.Basic
105 import GHC.Types.ForeignCall
106 import Language.Haskell.Syntax.Extension
107 import GHC.Types.Name.Set
108 import GHC.Types.Fixity
109
110 -- others:
111 import GHC.Utils.Outputable
112 import GHC.Utils.Misc
113 import GHC.Types.SrcLoc
114 import GHC.Types.SourceText
115 import GHC.Core.Type
116 import GHC.Unit.Module.Warnings
117
118 import GHC.Data.Maybe
119 import Data.Data hiding (TyCon,Fixity, Infix)
120 import Data.Void
121
122 {-
123 ************************************************************************
124 * *
125 \subsection[HsDecl]{Declarations}
126 * *
127 ************************************************************************
128 -}
129
130 type LHsDecl p = XRec p (HsDecl p)
131 -- ^ When in a list this may have
132 --
133 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnSemi'
134 --
135
136 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
137
138 -- | A Haskell Declaration
139 data HsDecl p
140 = TyClD (XTyClD p) (TyClDecl p) -- ^ Type or Class Declaration
141 | InstD (XInstD p) (InstDecl p) -- ^ Instance declaration
142 | DerivD (XDerivD p) (DerivDecl p) -- ^ Deriving declaration
143 | ValD (XValD p) (HsBind p) -- ^ Value declaration
144 | SigD (XSigD p) (Sig p) -- ^ Signature declaration
145 | KindSigD (XKindSigD p) (StandaloneKindSig p) -- ^ Standalone kind signature
146 | DefD (XDefD p) (DefaultDecl p) -- ^ 'default' declaration
147 | ForD (XForD p) (ForeignDecl p) -- ^ Foreign declaration
148 | WarningD (XWarningD p) (WarnDecls p) -- ^ Warning declaration
149 | AnnD (XAnnD p) (AnnDecl p) -- ^ Annotation declaration
150 | RuleD (XRuleD p) (RuleDecls p) -- ^ Rule declaration
151 | SpliceD (XSpliceD p) (SpliceDecl p) -- ^ Splice declaration
152 -- (Includes quasi-quotes)
153 | DocD (XDocD p) (DocDecl) -- ^ Documentation comment declaration
154 | RoleAnnotD (XRoleAnnotD p) (RoleAnnotDecl p) -- ^Role annotation declaration
155 | XHsDecl !(XXHsDecl p)
156
157 {-
158 Note [Top-level fixity signatures in an HsGroup]
159 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160 An `HsGroup p` stores every top-level fixity declarations in one of two places:
161
162 1. hs_fixds :: [LFixitySig p]
163
164 This stores fixity signatures for top-level declarations (e.g., functions,
165 data constructors, classes, type families, etc.) as well as fixity
166 signatures for class methods written outside of the class, as in this
167 example:
168
169 infixl 4 `m1`
170 class C1 a where
171 m1 :: a -> a -> a
172
173 2. hs_tyclds :: [TyClGroup p]
174
175 Each type class can be found in a TyClDecl inside a TyClGroup, and that
176 TyClDecl stores the fixity signatures for its methods written inside of the
177 class, as in this example:
178
179 class C2 a where
180 infixl 4 `m2`
181 m2 :: a -> a -> a
182
183 The story for fixity signatures for class methods is made slightly complicated
184 by the fact that they can appear both inside and outside of the class itself,
185 and both forms of fixity signatures are considered top-level. This matters
186 in `GHC.Rename.Module.rnSrcDecls`, which must create a fixity environment out
187 of all top-level fixity signatures before doing anything else. Therefore,
188 `rnSrcDecls` must be aware of both (1) and (2) above. The
189 `hsGroupTopLevelFixitySigs` function is responsible for collecting this
190 information from an `HsGroup`.
191
192 One might wonder why we even bother separating top-level fixity signatures
193 into two places at all. That is, why not just take the fixity signatures
194 from `hs_tyclds` and put them into `hs_fixds` so that they are all in one
195 location? This ends up causing problems for `GHC.HsToCore.Quote.repTopDs`,
196 which translates each fixity signature in `hs_fixds` and `hs_tyclds` into a
197 Template Haskell `Dec`. If there are any duplicate signatures between the two
198 fields, this will result in an error (#17608).
199 -}
200
201 -- | Haskell Group
202 --
203 -- A 'HsDecl' is categorised into a 'HsGroup' before being
204 -- fed to the renamer.
205 data HsGroup p
206 = HsGroup {
207 hs_ext :: XCHsGroup p,
208 hs_valds :: HsValBinds p,
209 hs_splcds :: [LSpliceDecl p],
210
211 hs_tyclds :: [TyClGroup p],
212 -- A list of mutually-recursive groups;
213 -- This includes `InstDecl`s as well;
214 -- Parser generates a singleton list;
215 -- renamer does dependency analysis
216
217 hs_derivds :: [LDerivDecl p],
218
219 hs_fixds :: [LFixitySig p],
220 -- A list of fixity signatures defined for top-level
221 -- declarations and class methods (defined outside of the class
222 -- itself).
223 -- See Note [Top-level fixity signatures in an HsGroup]
224
225 hs_defds :: [LDefaultDecl p],
226 hs_fords :: [LForeignDecl p],
227 hs_warnds :: [LWarnDecls p],
228 hs_annds :: [LAnnDecl p],
229 hs_ruleds :: [LRuleDecls p],
230
231 hs_docs :: [LDocDecl p]
232 }
233 | XHsGroup !(XXHsGroup p)
234
235
236 hsGroupInstDecls :: HsGroup id -> [LInstDecl id]
237 hsGroupInstDecls = (=<<) group_instds . hs_tyclds
238
239 -- | Located Splice Declaration
240 type LSpliceDecl pass = XRec pass (SpliceDecl pass)
241
242 -- | Splice Declaration
243 data SpliceDecl p
244 = SpliceDecl -- Top level splice
245 (XSpliceDecl p)
246 (XRec p (HsSplice p))
247 SpliceExplicitFlag
248 | XSpliceDecl !(XXSpliceDecl p)
249
250 {-
251 ************************************************************************
252 * *
253 Type and class declarations
254 * *
255 ************************************************************************
256
257 Note [The Naming story]
258 ~~~~~~~~~~~~~~~~~~~~~~~
259 Here is the story about the implicit names that go with type, class,
260 and instance decls. It's a bit tricky, so pay attention!
261
262 "Implicit" (or "system") binders
263 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
264 Each data type decl defines
265 a worker name for each constructor
266 to-T and from-T convertors
267 Each class decl defines
268 a tycon for the class
269 a data constructor for that tycon
270 the worker for that constructor
271 a selector for each superclass
272
273 All have occurrence names that are derived uniquely from their parent
274 declaration.
275
276 None of these get separate definitions in an interface file; they are
277 fully defined by the data or class decl. But they may *occur* in
278 interface files, of course. Any such occurrence must haul in the
279 relevant type or class decl.
280
281 Plan of attack:
282 - Ensure they "point to" the parent data/class decl
283 when loading that decl from an interface file
284 (See RnHiFiles.getSysBinders)
285
286 - When typechecking the decl, we build the implicit TyCons and Ids.
287 When doing so we look them up in the name cache (GHC.Rename.Env.lookupSysName),
288 to ensure correct module and provenance is set
289
290 These are the two places that we have to conjure up the magic derived
291 names. (The actual magic is in GHC.Types.Name.Occurrence.mkWorkerOcc, etc.)
292
293 Default methods
294 ~~~~~~~~~~~~~~~
295 - Occurrence name is derived uniquely from the method name
296 E.g. $dmmax
297
298 - If there is a default method name at all, it's recorded in
299 the ClassOpSig (in GHC.Hs.Binds), in the DefMethInfo field.
300 (DefMethInfo is defined in GHC.Core.Class)
301
302 Source-code class decls and interface-code class decls are treated subtly
303 differently, which has given me a great deal of confusion over the years.
304 Here's the deal. (We distinguish the two cases because source-code decls
305 have (Just binds) in the tcdMeths field, whereas interface decls have Nothing.
306
307 In *source-code* class declarations:
308
309 - When parsing, every ClassOpSig gets a DefMeth with a suitable RdrName
310 This is done by GHC.Parser.PostProcess.mkClassOpSigDM
311
312 - The renamer renames it to a Name
313
314 - During typechecking, we generate a binding for each $dm for
315 which there's a programmer-supplied default method:
316 class Foo a where
317 op1 :: <type>
318 op2 :: <type>
319 op1 = ...
320 We generate a binding for $dmop1 but not for $dmop2.
321 The Class for Foo has a Nothing for op2 and
322 a Just ($dm_op1, VanillaDM) for op1.
323 The Name for $dmop2 is simply discarded.
324
325 In *interface-file* class declarations:
326 - When parsing, we see if there's an explicit programmer-supplied default method
327 because there's an '=' sign to indicate it:
328 class Foo a where
329 op1 = :: <type> -- NB the '='
330 op2 :: <type>
331 We use this info to generate a DefMeth with a suitable RdrName for op1,
332 and a NoDefMeth for op2
333 - The interface file has a separate definition for $dmop1, with unfolding etc.
334 - The renamer renames it to a Name.
335 - The renamer treats $dmop1 as a free variable of the declaration, so that
336 the binding for $dmop1 will be sucked in. (See RnHsSyn.tyClDeclFVs)
337 This doesn't happen for source code class decls, because they *bind* the default method.
338
339 Dictionary functions
340 ~~~~~~~~~~~~~~~~~~~~
341 Each instance declaration gives rise to one dictionary function binding.
342
343 The type checker makes up new source-code instance declarations
344 (e.g. from 'deriving' or generic default methods --- see
345 GHC.Tc.TyCl.Instance.tcInstDecls1). So we can't generate the names for
346 dictionary functions in advance (we don't know how many we need).
347
348 On the other hand for interface-file instance declarations, the decl
349 specifies the name of the dictionary function, and it has a binding elsewhere
350 in the interface file:
351 instance {Eq Int} = dEqInt
352 dEqInt :: {Eq Int} <pragma info>
353
354 So again we treat source code and interface file code slightly differently.
355
356 Source code:
357 - Source code instance decls have a Nothing in the (Maybe name) field
358 (see data InstDecl below)
359
360 - The typechecker makes up a Local name for the dict fun for any source-code
361 instance decl, whether it comes from a source-code instance decl, or whether
362 the instance decl is derived from some other construct (e.g. 'deriving').
363
364 - The occurrence name it chooses is derived from the instance decl (just for
365 documentation really) --- e.g. dNumInt. Two dict funs may share a common
366 occurrence name, but will have different uniques. E.g.
367 instance Foo [Int] where ...
368 instance Foo [Bool] where ...
369 These might both be dFooList
370
371 - The CoreTidy phase externalises the name, and ensures the occurrence name is
372 unique (this isn't special to dict funs). So we'd get dFooList and dFooList1.
373
374 - We can take this relaxed approach (changing the occurrence name later)
375 because dict fun Ids are not captured in a TyCon or Class (unlike default
376 methods, say). Instead, they are kept separately in the InstEnv. This
377 makes it easy to adjust them after compiling a module. (Once we've finished
378 compiling that module, they don't change any more.)
379
380
381 Interface file code:
382 - The instance decl gives the dict fun name, so the InstDecl has a (Just name)
383 in the (Maybe name) field.
384
385 - RnHsSyn.instDeclFVs treats the dict fun name as free in the decl, so that we
386 suck in the dfun binding
387 -}
388
389 -- | Located Declaration of a Type or Class
390 type LTyClDecl pass = XRec pass (TyClDecl pass)
391
392 -- | A type or class declaration.
393 data TyClDecl pass
394 = -- | @type/data family T :: *->*@
395 --
396 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnType',
397 -- 'GHC.Parser.Annotation.AnnData',
398 -- 'GHC.Parser.Annotation.AnnFamily','GHC.Parser.Annotation.AnnDcolon',
399 -- 'GHC.Parser.Annotation.AnnWhere','GHC.Parser.Annotation.AnnOpenP',
400 -- 'GHC.Parser.Annotation.AnnDcolon','GHC.Parser.Annotation.AnnCloseP',
401 -- 'GHC.Parser.Annotation.AnnEqual','GHC.Parser.Annotation.AnnRarrow',
402 -- 'GHC.Parser.Annotation.AnnVbar'
403
404 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
405 FamDecl { tcdFExt :: XFamDecl pass, tcdFam :: FamilyDecl pass }
406
407 | -- | @type@ declaration
408 --
409 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnType',
410 -- 'GHC.Parser.Annotation.AnnEqual',
411
412 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
413 SynDecl { tcdSExt :: XSynDecl pass -- ^ Post renameer, FVs
414 , tcdLName :: LIdP pass -- ^ Type constructor
415 , tcdTyVars :: LHsQTyVars pass -- ^ Type variables; for an
416 -- associated type these
417 -- include outer binders
418 , tcdFixity :: LexicalFixity -- ^ Fixity used in the declaration
419 , tcdRhs :: LHsType pass } -- ^ RHS of type declaration
420
421 | -- | @data@ declaration
422 --
423 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnData',
424 -- 'GHC.Parser.Annotation.AnnFamily',
425 -- 'GHC.Parser.Annotation.AnnNewType',
426 -- 'GHC.Parser.Annotation.AnnNewType','GHC.Parser.Annotation.AnnDcolon'
427 -- 'GHC.Parser.Annotation.AnnWhere',
428
429 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
430 DataDecl { tcdDExt :: XDataDecl pass -- ^ Post renamer, CUSK flag, FVs
431 , tcdLName :: LIdP pass -- ^ Type constructor
432 , tcdTyVars :: LHsQTyVars pass -- ^ Type variables
433 -- See Note [TyVar binders for associated declarations]
434 , tcdFixity :: LexicalFixity -- ^ Fixity used in the declaration
435 , tcdDataDefn :: HsDataDefn pass }
436
437 | ClassDecl { tcdCExt :: XClassDecl pass, -- ^ Post renamer, FVs
438 tcdCtxt :: Maybe (LHsContext pass), -- ^ Context...
439 tcdLName :: LIdP pass, -- ^ Name of the class
440 tcdTyVars :: LHsQTyVars pass, -- ^ Class type variables
441 tcdFixity :: LexicalFixity, -- ^ Fixity used in the declaration
442 tcdFDs :: [LHsFunDep pass], -- ^ Functional deps
443 tcdSigs :: [LSig pass], -- ^ Methods' signatures
444 tcdMeths :: LHsBinds pass, -- ^ Default methods
445 tcdATs :: [LFamilyDecl pass], -- ^ Associated types;
446 tcdATDefs :: [LTyFamDefltDecl pass], -- ^ Associated type defaults
447 tcdDocs :: [LDocDecl pass] -- ^ Haddock docs
448 }
449 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnClass',
450 -- 'GHC.Parser.Annotation.AnnWhere','GHC.Parser.Annotation.AnnOpen',
451 -- 'GHC.Parser.Annotation.AnnClose'
452 -- - The tcdFDs will have 'GHC.Parser.Annotation.AnnVbar',
453 -- 'GHC.Parser.Annotation.AnnComma'
454 -- 'GHC.Parser.Annotation.AnnRarrow'
455
456 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
457 | XTyClDecl !(XXTyClDecl pass)
458
459 data FunDep pass
460 = FunDep (XCFunDep pass)
461 [LIdP pass]
462 [LIdP pass]
463 | XFunDep !(XXFunDep pass)
464
465 type LHsFunDep pass = XRec pass (FunDep pass)
466
467 data DataDeclRn = DataDeclRn
468 { tcdDataCusk :: Bool -- ^ does this have a CUSK?
469 -- See Note [CUSKs: complete user-supplied kind signatures]
470 , tcdFVs :: NameSet }
471 deriving Data
472
473 {- Note [TyVar binders for associated decls]
474 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
475 For an /associated/ data, newtype, or type-family decl, the LHsQTyVars
476 /includes/ outer binders. For example
477 class T a where
478 data D a c
479 type F a b :: *
480 type F a b = a -> a
481 Here the data decl for 'D', and type-family decl for 'F', both include 'a'
482 in their LHsQTyVars (tcdTyVars and fdTyVars resp).
483
484 Ditto any implicit binders in the hsq_implicit field of the LHSQTyVars.
485
486 The idea is that the associated type is really a top-level decl in its
487 own right. However we are careful to use the same name 'a', so that
488 we can match things up.
489
490 c.f. Note [Associated type tyvar names] in GHC.Core.Class
491 Note [Family instance declaration binders]
492 -}
493
494 {- Note [Class LayoutInfo]
495 ~~~~~~~~~~~~~~~~~~~~~~~~~~
496 The LayoutInfo is used to associate Haddock comments with parts of the declaration.
497 Compare the following examples:
498
499 class C a where
500 f :: a -> Int
501 -- ^ comment on f
502
503 class C a where
504 f :: a -> Int
505 -- ^ comment on C
506
507 Notice how "comment on f" and "comment on C" differ only by indentation level.
508 Thus we have to record the indentation level of the class declarations.
509
510 See also Note [Adding Haddock comments to the syntax tree] in GHC.Parser.PostProcess.Haddock
511 -}
512
513 -- Simple classifiers for TyClDecl
514 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
515
516 -- | @True@ <=> argument is a @data@\/@newtype@
517 -- declaration.
518 isDataDecl :: TyClDecl pass -> Bool
519 isDataDecl (DataDecl {}) = True
520 isDataDecl _other = False
521
522 -- | type or type instance declaration
523 isSynDecl :: TyClDecl pass -> Bool
524 isSynDecl (SynDecl {}) = True
525 isSynDecl _other = False
526
527 -- | type class
528 isClassDecl :: TyClDecl pass -> Bool
529 isClassDecl (ClassDecl {}) = True
530 isClassDecl _ = False
531
532 -- | type/data family declaration
533 isFamilyDecl :: TyClDecl pass -> Bool
534 isFamilyDecl (FamDecl {}) = True
535 isFamilyDecl _other = False
536
537 -- | type family declaration
538 isTypeFamilyDecl :: TyClDecl pass -> Bool
539 isTypeFamilyDecl (FamDecl _ (FamilyDecl { fdInfo = info })) = case info of
540 OpenTypeFamily -> True
541 ClosedTypeFamily {} -> True
542 _ -> False
543 isTypeFamilyDecl _ = False
544
545 -- | open type family info
546 isOpenTypeFamilyInfo :: FamilyInfo pass -> Bool
547 isOpenTypeFamilyInfo OpenTypeFamily = True
548 isOpenTypeFamilyInfo _ = False
549
550 -- | closed type family info
551 isClosedTypeFamilyInfo :: FamilyInfo pass -> Bool
552 isClosedTypeFamilyInfo (ClosedTypeFamily {}) = True
553 isClosedTypeFamilyInfo _ = False
554
555 -- | data family declaration
556 isDataFamilyDecl :: TyClDecl pass -> Bool
557 isDataFamilyDecl (FamDecl _ (FamilyDecl { fdInfo = DataFamily })) = True
558 isDataFamilyDecl _other = False
559
560 -- Dealing with names
561
562 tyClDeclTyVars :: TyClDecl pass -> LHsQTyVars pass
563 tyClDeclTyVars (FamDecl { tcdFam = FamilyDecl { fdTyVars = tvs } }) = tvs
564 tyClDeclTyVars d = tcdTyVars d
565
566 countTyClDecls :: [TyClDecl pass] -> (Int, Int, Int, Int, Int)
567 -- class, synonym decls, data, newtype, family decls
568 countTyClDecls decls
569 = (count isClassDecl decls,
570 count isSynDecl decls, -- excluding...
571 count isDataTy decls, -- ...family...
572 count isNewTy decls, -- ...instances
573 count isFamilyDecl decls)
574 where
575 isDataTy DataDecl{ tcdDataDefn = HsDataDefn { dd_ND = DataType } } = True
576 isDataTy _ = False
577
578 isNewTy DataDecl{ tcdDataDefn = HsDataDefn { dd_ND = NewType } } = True
579 isNewTy _ = False
580
581
582 {- Note [CUSKs: complete user-supplied kind signatures]
583 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
584 We kind-check declarations differently if they have a complete, user-supplied
585 kind signature (CUSK). This is because we can safely generalise a CUSKed
586 declaration before checking all of the others, supporting polymorphic recursion.
587 See https://gitlab.haskell.org/ghc/ghc/wikis/ghc-kinds/kind-inference#proposed-new-strategy
588 and #9200 for lots of discussion of how we got here.
589
590 The detection of CUSKs is enabled by the -XCUSKs extension, switched on by default.
591 Under -XNoCUSKs, all declarations are treated as if they have no CUSK.
592 See https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0036-kind-signatures.rst
593
594 PRINCIPLE:
595 a type declaration has a CUSK iff we could produce a separate kind signature
596 for it, just like a type signature for a function,
597 looking only at the header of the declaration.
598
599 Examples:
600 * data T1 (a :: *->*) (b :: *) = ....
601 -- Has CUSK; equivalant to T1 :: (*->*) -> * -> *
602
603 * data T2 a b = ...
604 -- No CUSK; we do not want to guess T2 :: * -> * -> *
605 -- because the full decl might be data T a b = MkT (a b)
606
607 * data T3 (a :: k -> *) (b :: *) = ...
608 -- CUSK; equivalent to T3 :: (k -> *) -> * -> *
609 -- We lexically generalise over k to get
610 -- T3 :: forall k. (k -> *) -> * -> *
611 -- The generalisation is here is purely lexical, just like
612 -- f3 :: a -> a
613 -- means
614 -- f3 :: forall a. a -> a
615
616 * data T4 (a :: j k) = ...
617 -- CUSK; equivalent to T4 :: j k -> *
618 -- which we lexically generalise to T4 :: forall j k. j k -> *
619 -- and then, if PolyKinds is on, we further generalise to
620 -- T4 :: forall kk (j :: kk -> *) (k :: kk). j k -> *
621 -- Again this is exactly like what happens as the term level
622 -- when you write
623 -- f4 :: forall a b. a b -> Int
624
625 NOTE THAT
626 * A CUSK does /not/ mean that everything about the kind signature is
627 fully specified by the user. Look at T4 and f4: we had to do kind
628 inference to figure out the kind-quantification. But in both cases
629 (T4 and f4) that inference is done looking /only/ at the header of T4
630 (or signature for f4), not at the definition thereof.
631
632 * The CUSK completely fixes the kind of the type constructor, forever.
633
634 * The precise rules, for each declaration form, for whether a declaration
635 has a CUSK are given in the user manual section "Complete user-supplied
636 kind signatures and polymorphic recursion". But they simply implement
637 PRINCIPLE above.
638
639 * Open type families are interesting:
640 type family T5 a b :: *
641 There simply /is/ no accompanying declaration, so that info is all
642 we'll ever get. So we it has a CUSK by definition, and we default
643 any un-fixed kind variables to *.
644
645 * Associated types are a bit tricker:
646 class C6 a where
647 type family T6 a b :: *
648 op :: a Int -> Int
649 Here C6 does not have a CUSK (in fact we ultimately discover that
650 a :: * -> *). And hence neither does T6, the associated family,
651 because we can't fix its kind until we have settled C6. Another
652 way to say it: unlike a top-level, we /may/ discover more about
653 a's kind from C6's definition.
654
655 * A data definition with a top-level :: must explicitly bind all
656 kind variables to the right of the ::. See test
657 dependent/should_compile/KindLevels, which requires this
658 case. (Naturally, any kind variable mentioned before the :: should
659 not be bound after it.)
660
661 This last point is much more debatable than the others; see
662 #15142 comment:22
663
664 Because this is fiddly to check, there is a field in the DataDeclRn
665 structure (included in a DataDecl after the renamer) that stores whether
666 or not the declaration has a CUSK.
667 -}
668
669
670 {- *********************************************************************
671 * *
672 TyClGroup
673 Strongly connected components of
674 type, class, instance, and role declarations
675 * *
676 ********************************************************************* -}
677
678 {- Note [TyClGroups and dependency analysis]
679 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
680 A TyClGroup represents a strongly connected components of type/class/instance
681 decls, together with the role annotations for the type/class declarations.
682
683 The hs_tyclds :: [TyClGroup] field of a HsGroup is a dependency-order
684 sequence of strongly-connected components.
685
686 Invariants
687 * The type and class declarations, group_tyclds, may depend on each
688 other, or earlier TyClGroups, but not on later ones
689
690 * The role annotations, group_roles, are role-annotations for some or
691 all of the types and classes in group_tyclds (only).
692
693 * The instance declarations, group_instds, may (and usually will)
694 depend on group_tyclds, or on earlier TyClGroups, but not on later
695 ones.
696
697 See Note [Dependency analysis of type, class, and instance decls]
698 in GHC.Rename.Module for more info.
699 -}
700
701 -- | Type or Class Group
702 data TyClGroup pass -- See Note [TyClGroups and dependency analysis]
703 = TyClGroup { group_ext :: XCTyClGroup pass
704 , group_tyclds :: [LTyClDecl pass]
705 , group_roles :: [LRoleAnnotDecl pass]
706 , group_kisigs :: [LStandaloneKindSig pass]
707 , group_instds :: [LInstDecl pass] }
708 | XTyClGroup !(XXTyClGroup pass)
709
710
711 tyClGroupTyClDecls :: [TyClGroup pass] -> [LTyClDecl pass]
712 tyClGroupTyClDecls = concatMap group_tyclds
713
714 tyClGroupInstDecls :: [TyClGroup pass] -> [LInstDecl pass]
715 tyClGroupInstDecls = concatMap group_instds
716
717 tyClGroupRoleDecls :: [TyClGroup pass] -> [LRoleAnnotDecl pass]
718 tyClGroupRoleDecls = concatMap group_roles
719
720 tyClGroupKindSigs :: [TyClGroup pass] -> [LStandaloneKindSig pass]
721 tyClGroupKindSigs = concatMap group_kisigs
722
723
724 {- *********************************************************************
725 * *
726 Data and type family declarations
727 * *
728 ********************************************************************* -}
729
730 {- Note [FamilyResultSig]
731 ~~~~~~~~~~~~~~~~~~~~~~~~~
732
733 This data type represents the return signature of a type family. Possible
734 values are:
735
736 * NoSig - the user supplied no return signature:
737 type family Id a where ...
738
739 * KindSig - the user supplied the return kind:
740 type family Id a :: * where ...
741
742 * TyVarSig - user named the result with a type variable and possibly
743 provided a kind signature for that variable:
744 type family Id a = r where ...
745 type family Id a = (r :: *) where ...
746
747 Naming result of a type family is required if we want to provide
748 injectivity annotation for a type family:
749 type family Id a = r | r -> a where ...
750
751 See also: Note [Injectivity annotation]
752
753 Note [Injectivity annotation]
754 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
755
756 A user can declare a type family to be injective:
757
758 type family Id a = r | r -> a where ...
759
760 * The part after the "|" is called "injectivity annotation".
761 * "r -> a" part is called "injectivity condition"; at the moment terms
762 "injectivity annotation" and "injectivity condition" are synonymous
763 because we only allow a single injectivity condition.
764 * "r" is the "LHS of injectivity condition". LHS can only contain the
765 variable naming the result of a type family.
766
767 * "a" is the "RHS of injectivity condition". RHS contains space-separated
768 type and kind variables representing the arguments of a type
769 family. Variables can be omitted if a type family is not injective in
770 these arguments. Example:
771 type family Foo a b c = d | d -> a c where ...
772
773 Note that:
774 (a) naming of type family result is required to provide injectivity
775 annotation
776 (b) for associated types if the result was named then injectivity annotation
777 is mandatory. Otherwise result type variable is indistinguishable from
778 associated type default.
779
780 It is possible that in the future this syntax will be extended to support
781 more complicated injectivity annotations. For example we could declare that
782 if we know the result of Plus and one of its arguments we can determine the
783 other argument:
784
785 type family Plus a b = (r :: Nat) | r a -> b, r b -> a where ...
786
787 Here injectivity annotation would consist of two comma-separated injectivity
788 conditions.
789
790 See also Note [Injective type families] in GHC.Core.TyCon
791 -}
792
793 -- | Located type Family Result Signature
794 type LFamilyResultSig pass = XRec pass (FamilyResultSig pass)
795
796 -- | type Family Result Signature
797 data FamilyResultSig pass = -- see Note [FamilyResultSig]
798 NoSig (XNoSig pass)
799 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' :
800
801 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
802
803 | KindSig (XCKindSig pass) (LHsKind pass)
804 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' :
805 -- 'GHC.Parser.Annotation.AnnOpenP','GHC.Parser.Annotation.AnnDcolon',
806 -- 'GHC.Parser.Annotation.AnnCloseP'
807
808 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
809
810 | TyVarSig (XTyVarSig pass) (LHsTyVarBndr () pass)
811 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' :
812 -- 'GHC.Parser.Annotation.AnnOpenP','GHC.Parser.Annotation.AnnDcolon',
813 -- 'GHC.Parser.Annotation.AnnCloseP', 'GHC.Parser.Annotation.AnnEqual'
814 | XFamilyResultSig !(XXFamilyResultSig pass)
815
816 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
817
818
819 -- | Located type Family Declaration
820 type LFamilyDecl pass = XRec pass (FamilyDecl pass)
821
822 -- | type Family Declaration
823 data FamilyDecl pass = FamilyDecl
824 { fdExt :: XCFamilyDecl pass
825 , fdInfo :: FamilyInfo pass -- type/data, closed/open
826 , fdTopLevel :: TopLevelFlag -- used for printing only
827 , fdLName :: LIdP pass -- type constructor
828 , fdTyVars :: LHsQTyVars pass -- type variables
829 -- See Note [TyVar binders for associated declarations]
830 , fdFixity :: LexicalFixity -- Fixity used in the declaration
831 , fdResultSig :: LFamilyResultSig pass -- result signature
832 , fdInjectivityAnn :: Maybe (LInjectivityAnn pass) -- optional injectivity ann
833 }
834 | XFamilyDecl !(XXFamilyDecl pass)
835 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnType',
836 -- 'GHC.Parser.Annotation.AnnData', 'GHC.Parser.Annotation.AnnFamily',
837 -- 'GHC.Parser.Annotation.AnnWhere', 'GHC.Parser.Annotation.AnnOpenP',
838 -- 'GHC.Parser.Annotation.AnnDcolon', 'GHC.Parser.Annotation.AnnCloseP',
839 -- 'GHC.Parser.Annotation.AnnEqual', 'GHC.Parser.Annotation.AnnRarrow',
840 -- 'GHC.Parser.Annotation.AnnVbar'
841
842 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
843
844
845 -- | Located Injectivity Annotation
846 type LInjectivityAnn pass = XRec pass (InjectivityAnn pass)
847
848 -- | If the user supplied an injectivity annotation it is represented using
849 -- InjectivityAnn. At the moment this is a single injectivity condition - see
850 -- Note [Injectivity annotation]. `Located name` stores the LHS of injectivity
851 -- condition. `[Located name]` stores the RHS of injectivity condition. Example:
852 --
853 -- type family Foo a b c = r | r -> a c where ...
854 --
855 -- This will be represented as "InjectivityAnn `r` [`a`, `c`]"
856 data InjectivityAnn pass
857 = InjectivityAnn (XCInjectivityAnn pass)
858 (LIdP pass) [LIdP pass]
859 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' :
860 -- 'GHC.Parser.Annotation.AnnRarrow', 'GHC.Parser.Annotation.AnnVbar'
861
862 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
863 | XInjectivityAnn !(XXInjectivityAnn pass)
864
865 data FamilyInfo pass
866 = DataFamily
867 | OpenTypeFamily
868 -- | 'Nothing' if we're in an hs-boot file and the user
869 -- said "type family Foo x where .."
870 | ClosedTypeFamily (Maybe [LTyFamInstEqn pass])
871
872
873 ------------- Pretty printing FamilyDecls -----------
874
875 pprFlavour :: FamilyInfo pass -> SDoc
876 pprFlavour DataFamily = text "data"
877 pprFlavour OpenTypeFamily = text "type"
878 pprFlavour (ClosedTypeFamily {}) = text "type"
879
880 instance Outputable (FamilyInfo pass) where
881 ppr info = pprFlavour info <+> text "family"
882
883
884
885 {- *********************************************************************
886 * *
887 Data types and data constructors
888 * *
889 ********************************************************************* -}
890
891 -- | Haskell Data type Definition
892 data HsDataDefn pass -- The payload of a data type defn
893 -- Used *both* for vanilla data declarations,
894 -- *and* for data family instances
895 = -- | Declares a data type or newtype, giving its constructors
896 -- @
897 -- data/newtype T a = <constrs>
898 -- data/newtype instance T [a] = <constrs>
899 -- @
900 HsDataDefn { dd_ext :: XCHsDataDefn pass,
901 dd_ND :: NewOrData,
902 dd_ctxt :: Maybe (LHsContext pass), -- ^ Context
903 dd_cType :: Maybe (XRec pass CType),
904 dd_kindSig:: Maybe (LHsKind pass),
905 -- ^ Optional kind signature.
906 --
907 -- @(Just k)@ for a GADT-style @data@,
908 -- or @data instance@ decl, with explicit kind sig
909 --
910 -- Always @Nothing@ for H98-syntax decls
911
912 dd_cons :: [LConDecl pass],
913 -- ^ Data constructors
914 --
915 -- For @data T a = T1 | T2 a@
916 -- the 'LConDecl's all have 'ConDeclH98'.
917 -- For @data T a where { T1 :: T a }@
918 -- the 'LConDecls' all have 'ConDeclGADT'.
919
920 dd_derivs :: HsDeriving pass -- ^ Optional 'deriving' clause
921
922 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
923 }
924 | XHsDataDefn !(XXHsDataDefn pass)
925
926 -- | Haskell Deriving clause
927 type HsDeriving pass = [LHsDerivingClause pass]
928 -- ^ The optional @deriving@ clauses of a data declaration. "Clauses" is
929 -- plural because one can specify multiple deriving clauses using the
930 -- @-XDerivingStrategies@ language extension.
931 --
932 -- The list of 'LHsDerivingClause's corresponds to exactly what the user
933 -- requested to derive, in order. If no deriving clauses were specified,
934 -- the list is empty.
935
936 type LHsDerivingClause pass = XRec pass (HsDerivingClause pass)
937
938 -- | A single @deriving@ clause of a data declaration.
939 --
940 -- - 'GHC.Parser.Annotation.AnnKeywordId' :
941 -- 'GHC.Parser.Annotation.AnnDeriving', 'GHC.Parser.Annotation.AnnStock',
942 -- 'GHC.Parser.Annotation.AnnAnyClass', 'GHC.Parser.Annotation.AnnNewtype',
943 -- 'GHC.Parser.Annotation.AnnOpen','GHC.Parser.Annotation.AnnClose'
944 data HsDerivingClause pass
945 -- See Note [Deriving strategies] in GHC.Tc.Deriv
946 = HsDerivingClause
947 { deriv_clause_ext :: XCHsDerivingClause pass
948 , deriv_clause_strategy :: Maybe (LDerivStrategy pass)
949 -- ^ The user-specified strategy (if any) to use when deriving
950 -- 'deriv_clause_tys'.
951 , deriv_clause_tys :: LDerivClauseTys pass
952 -- ^ The types to derive.
953 }
954 | XHsDerivingClause !(XXHsDerivingClause pass)
955
956 type LDerivClauseTys pass = XRec pass (DerivClauseTys pass)
957
958 -- | The types mentioned in a single @deriving@ clause. This can come in two
959 -- forms, 'DctSingle' or 'DctMulti', depending on whether the types are
960 -- surrounded by enclosing parentheses or not. These parentheses are
961 -- semantically different than 'HsParTy'. For example, @deriving ()@ means
962 -- \"derive zero classes\" rather than \"derive an instance of the 0-tuple\".
963 --
964 -- 'DerivClauseTys' use 'LHsSigType' because @deriving@ clauses can mention
965 -- type variables that aren't bound by the datatype, e.g.
966 --
967 -- > data T b = ... deriving (C [a])
968 --
969 -- should produce a derived instance for @C [a] (T b)@.
970 data DerivClauseTys pass
971 = -- | A @deriving@ clause with a single type. Moreover, that type can only
972 -- be a type constructor without any arguments.
973 --
974 -- Example: @deriving Eq@
975 DctSingle (XDctSingle pass) (LHsSigType pass)
976
977 -- | A @deriving@ clause with a comma-separated list of types, surrounded
978 -- by enclosing parentheses.
979 --
980 -- Example: @deriving (Eq, C a)@
981 | DctMulti (XDctMulti pass) [LHsSigType pass]
982
983 | XDerivClauseTys !(XXDerivClauseTys pass)
984
985 -- | Located Standalone Kind Signature
986 type LStandaloneKindSig pass = XRec pass (StandaloneKindSig pass)
987
988 data StandaloneKindSig pass
989 = StandaloneKindSig (XStandaloneKindSig pass)
990 (LIdP pass) -- Why a single binder? See #16754
991 (LHsSigType pass) -- Why not LHsSigWcType? See Note [Wildcards in standalone kind signatures]
992 | XStandaloneKindSig !(XXStandaloneKindSig pass)
993
994 {- Note [Wildcards in standalone kind signatures]
995 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
996 Standalone kind signatures enable polymorphic recursion, and it is unclear how
997 to reconcile this with partial type signatures, so we disallow wildcards in
998 them.
999
1000 We reject wildcards in 'rnStandaloneKindSignature' by returning False for
1001 'StandaloneKindSigCtx' in 'wildCardsAllowed'.
1002
1003 The alternative design is to have special treatment for partial standalone kind
1004 signatures, much like we have special treatment for partial type signatures in
1005 terms. However, partial standalone kind signatures are not a proper replacement
1006 for CUSKs, so this would be a separate feature.
1007 -}
1008
1009 data NewOrData
1010 = NewType -- ^ @newtype Blah ...@
1011 | DataType -- ^ @data Blah ...@
1012 deriving( Eq, Data ) -- Needed because Demand derives Eq
1013
1014 -- | Convert a 'NewOrData' to a 'TyConFlavour'
1015 newOrDataToFlavour :: NewOrData -> TyConFlavour
1016 newOrDataToFlavour NewType = NewtypeFlavour
1017 newOrDataToFlavour DataType = DataTypeFlavour
1018
1019
1020 -- | Located data Constructor Declaration
1021 type LConDecl pass = XRec pass (ConDecl pass)
1022 -- ^ May have 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnSemi' when
1023 -- in a GADT constructor list
1024
1025 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1026
1027 -- |
1028 --
1029 -- @
1030 -- data T b = forall a. Eq a => MkT a b
1031 -- MkT :: forall b a. Eq a => MkT a b
1032 --
1033 -- data T b where
1034 -- MkT1 :: Int -> T Int
1035 --
1036 -- data T = Int `MkT` Int
1037 -- | MkT2
1038 --
1039 -- data T a where
1040 -- Int `MkT` Int :: T Int
1041 -- @
1042 --
1043 -- - 'GHC.Parser.Annotation.AnnKeywordId's : 'GHC.Parser.Annotation.AnnOpen',
1044 -- 'GHC.Parser.Annotation.AnnDotdot','GHC.Parser.Annotation.AnnCLose',
1045 -- 'GHC.Parser.Annotation.AnnEqual','GHC.Parser.Annotation.AnnVbar',
1046 -- 'GHC.Parser.Annotation.AnnDarrow','GHC.Parser.Annotation.AnnDarrow',
1047 -- 'GHC.Parser.Annotation.AnnForall','GHC.Parser.Annotation.AnnDot'
1048
1049 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1050
1051 -- | data Constructor Declaration
1052 data ConDecl pass
1053 = ConDeclGADT
1054 { con_g_ext :: XConDeclGADT pass
1055 , con_names :: [LIdP pass]
1056
1057 -- The following fields describe the type after the '::'
1058 -- See Note [GADT abstract syntax]
1059 , con_bndrs :: XRec pass (HsOuterSigTyVarBndrs pass)
1060 -- ^ The outermost type variable binders, be they explicit or
1061 -- implicit. The 'XRec' is used to anchor exact print
1062 -- annotations, AnnForall and AnnDot.
1063 , con_mb_cxt :: Maybe (LHsContext pass) -- ^ User-written context (if any)
1064 , con_g_args :: HsConDeclGADTDetails pass -- ^ Arguments; never infix
1065 , con_res_ty :: LHsType pass -- ^ Result type
1066
1067 , con_doc :: Maybe LHsDocString
1068 -- ^ A possible Haddock comment.
1069 }
1070
1071 | ConDeclH98
1072 { con_ext :: XConDeclH98 pass
1073 , con_name :: LIdP pass
1074
1075 , con_forall :: Bool
1076 -- ^ True <=> explicit user-written forall
1077 -- e.g. data T a = forall b. MkT b (b->a)
1078 -- con_ex_tvs = {b}
1079 -- False => con_ex_tvs is empty
1080 , con_ex_tvs :: [LHsTyVarBndr Specificity pass] -- ^ Existentials only
1081 , con_mb_cxt :: Maybe (LHsContext pass) -- ^ User-written context (if any)
1082 , con_args :: HsConDeclH98Details pass -- ^ Arguments; can be infix
1083
1084 , con_doc :: Maybe LHsDocString
1085 -- ^ A possible Haddock comment.
1086 }
1087 | XConDecl !(XXConDecl pass)
1088
1089 {- Note [GADT abstract syntax]
1090 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1091 The types of both forms of GADT constructors are very structured, as they
1092 must consist of the quantified type variables (if provided), followed by the
1093 context (if provided), followed by the argument types (if provided), followed
1094 by the result type. (See "Wrinkle: No nested foralls or contexts" below for
1095 more discussion on the restrictions imposed here.) As a result, instead of
1096 storing the type of a GADT constructor as a single LHsType, we split it up
1097 into its constituent components for easier access.
1098
1099 There are two broad ways to classify GADT constructors:
1100
1101 * Record-syntax constructors. For example:
1102
1103 data T a where
1104 K :: forall a. Ord a => { x :: [a], ... } -> T a
1105
1106 * Prefix constructors, which do not use record syntax. For example:
1107
1108 data T a where
1109 K :: forall a. Ord a => [a] -> ... -> T a
1110
1111 This distinction is recorded in the `con_args :: HsConDetails pass`, which
1112 tracks if we're dealing with a RecCon or PrefixCon. It is easy to distinguish
1113 the two in the AST since record GADT constructors use HsRecTy. This distinction
1114 is made in GHC.Parser.PostProcess.mkGadtDecl.
1115
1116 It is worth elaborating a bit more on the process of splitting the argument
1117 types of a GADT constructor, since there are some non-obvious details involved.
1118 While splitting the argument types of a record GADT constructor is easy (they
1119 are stored in an HsRecTy), splitting the arguments of a prefix GADT constructor
1120 is trickier. The basic idea is that we must split along the outermost function
1121 arrows ((->) and (%1 ->)) in the type, which GHC.Hs.Type.splitHsFunType
1122 accomplishes. But what about type operators? Consider:
1123
1124 C :: a :*: b -> a :*: b -> a :+: b
1125
1126 This could parse in many different ways depending on the precedences of each
1127 type operator. In particular, if (:*:) were to have lower precedence than (->),
1128 then it could very well parse like this:
1129
1130 a :*: ((b -> a) :*: ((b -> a) :+: b)))
1131
1132 This would give the false impression that the whole type is part of one large
1133 return type, with no arguments. Note that we do not fully resolve the exact
1134 precedences of each user-defined type operator until the renamer, so this a
1135 more difficult task for the parser.
1136
1137 Fortunately, there is no risk of the above happening. GHC's parser gives
1138 special treatment to function arrows, and as a result, they are always parsed
1139 with a lower precedence than any other type operator. As a result, the type
1140 above is actually parsed like this:
1141
1142 (a :*: b) -> ((a :*: b) -> (a :+: b))
1143
1144 While we won't know the exact precedences of (:*:) and (:+:) until the renamer,
1145 all we are concerned about in the parser is identifying the overall shape of
1146 the argument and result types, which we can accomplish by piggybacking on the
1147 special treatment given to function arrows. In a future where function arrows
1148 aren't given special status in the parser, we will likely have to modify
1149 GHC.Parser.PostProcess.mkHsOpTyPV to preserve this trick.
1150
1151 -----
1152 -- Wrinkle: No nested foralls or contexts
1153 -----
1154
1155 GADT constructors provide some freedom to change the order of foralls in their
1156 types (see Note [DataCon user type variable binders] in GHC.Core.DataCon), but
1157 this freedom is still limited. GADTs still require that all quantification
1158 occurs "prenex". That is, any explicitly quantified type variables must occur
1159 at the front of the GADT type, followed by any contexts, followed by the body of
1160 the GADT type, in precisely that order. For instance:
1161
1162 data T where
1163 MkT1 :: forall a b. (Eq a, Eq b) => a -> b -> T
1164 -- OK
1165 MkT2 :: forall a. Eq a => forall b. a -> b -> T
1166 -- Rejected, `forall b` is nested
1167 MkT3 :: forall a b. Eq a => Eq b => a -> b -> T
1168 -- Rejected, `Eq b` is nested
1169 MkT4 :: Int -> forall a. a -> T
1170 -- Rejected, `forall a` is nested
1171 MkT5 :: forall a. Int -> Eq a => a -> T
1172 -- Rejected, `Eq a` is nested
1173 MkT6 :: (forall a. a -> T)
1174 -- Rejected, `forall a` is nested due to the surrounding parentheses
1175 MkT7 :: (Eq a => a -> t)
1176 -- Rejected, `Eq a` is nested due to the surrounding parentheses
1177
1178 For the full details, see the "Formal syntax for GADTs" section of the GHC
1179 User's Guide. GHC enforces that GADT constructors do not have nested `forall`s
1180 or contexts in two parts:
1181
1182 1. GHC, in the process of splitting apart a GADT's type,
1183 extracts out the leading `forall` and context (if they are provided). To
1184 accomplish this splitting, the renamer uses the
1185 GHC.Hs.Type.splitLHsGADTPrefixTy function, which is careful not to remove
1186 parentheses surrounding the leading `forall` or context (as these
1187 parentheses can be syntactically significant). If the third result returned
1188 by splitLHsGADTPrefixTy contains any `forall`s or contexts, then they must
1189 be nested, so they will be rejected.
1190
1191 Note that this step applies to both prefix and record GADTs alike, as they
1192 both have syntax which permits `forall`s and contexts. The difference is
1193 where this step happens:
1194
1195 * For prefix GADTs, this happens in the renamer (in rnConDecl), as we cannot
1196 split until after the type operator fixities have been resolved.
1197 * For record GADTs, this happens in the parser (in mkGadtDecl).
1198 2. If the GADT type is prefix, the renamer (in the ConDeclGADTPrefixPs case of
1199 rnConDecl) will then check for nested `forall`s/contexts in the body of a
1200 prefix GADT type, after it has determined what all of the argument types are.
1201 This step is necessary to catch examples like MkT4 above, where the nested
1202 quantification occurs after a visible argument type.
1203 -}
1204
1205 -- | The arguments in a Haskell98-style data constructor.
1206 type HsConDeclH98Details pass
1207 = HsConDetails Void (HsScaled pass (LBangType pass)) (XRec pass [LConDeclField pass])
1208 -- The Void argument to HsConDetails here is a reflection of the fact that
1209 -- type applications are not allowed in data constructor declarations.
1210
1211 -- | The arguments in a GADT constructor. Unlike Haskell98-style constructors,
1212 -- GADT constructors cannot be declared with infix syntax. As a result, we do
1213 -- not use 'HsConDetails' here, as 'InfixCon' would be an unrepresentable
1214 -- state. (There is a notion of infix GADT constructors for the purposes of
1215 -- derived Show instances—see Note [Infix GADT constructors] in
1216 -- GHC.Tc.TyCl—but that is an orthogonal concern.)
1217 data HsConDeclGADTDetails pass
1218 = PrefixConGADT [HsScaled pass (LBangType pass)]
1219 | RecConGADT (XRec pass [LConDeclField pass]) (LHsUniToken "->" "→" pass)
1220
1221 instance Outputable NewOrData where
1222 ppr NewType = text "newtype"
1223 ppr DataType = text "data"
1224
1225 {-
1226 ************************************************************************
1227 * *
1228 Instance declarations
1229 * *
1230 ************************************************************************
1231
1232 Note [Type family instance declarations in HsSyn]
1233 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1234 The data type FamEqn represents one equation of a type family instance.
1235 Aside from the pass, it is also parameterised over another field, feqn_rhs.
1236 feqn_rhs is either an HsDataDefn (for data family instances) or an LHsType
1237 (for type family instances).
1238
1239 Type family instances also include associated type family default equations.
1240 That is because a default for a type family looks like this:
1241
1242 class C a where
1243 type family F a b :: Type
1244 type F c d = (c,d) -- Default instance
1245
1246 The default declaration is really just a `type instance` declaration, but one
1247 with particularly simple patterns: they must all be distinct type variables.
1248 That's because we will instantiate it (in an instance declaration for `C`) if
1249 we don't give an explicit instance for `F`. Note that the names of the
1250 variables don't need to match those of the class: it really is like a
1251 free-standing `type instance` declaration.
1252 -}
1253
1254 ----------------- Type synonym family instances -------------
1255
1256 -- | Located Type Family Instance Equation
1257 type LTyFamInstEqn pass = XRec pass (TyFamInstEqn pass)
1258 -- ^ May have 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnSemi'
1259 -- when in a list
1260
1261 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1262
1263 -- | Haskell Type Patterns
1264 type HsTyPats pass = [LHsTypeArg pass]
1265
1266 {- Note [Family instance declaration binders]
1267 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1268 The feqn_pats field of FamEqn (family instance equation) stores the LHS type
1269 (and kind) patterns. Any type (and kind) variables contained
1270 in these type patterns are bound in the feqn_bndrs field.
1271 Note that in particular:
1272
1273 * The feqn_bndrs *include* any anonymous wildcards. For example
1274 type instance F a _ = a
1275 The feqn_bndrs will be HsOuterImplicit {a, _}. Remember that each separate
1276 wildcard '_' gets its own unique. In this context wildcards behave just like
1277 an ordinary type variable, only anonymous.
1278
1279 * The feqn_bndrs *include* type variables that are already in scope
1280
1281 Eg class C s t where
1282 type F t p :: *
1283 instance C w (a,b) where
1284 type F (a,b) x = x->a
1285 The feqn_bndrs of the F decl is HsOuterImplicit {a,b,x}, even though the
1286 F decl is nested inside the 'instance' decl.
1287
1288 However after the renamer, the uniques will match up:
1289 instance C w7 (a8,b9) where
1290 type F (a8,b9) x10 = x10->a8
1291 so that we can compare the type pattern in the 'instance' decl and
1292 in the associated 'type' decl
1293
1294 c.f. Note [TyVar binders for associated decls]
1295 -}
1296
1297 -- | Type Family Instance Equation
1298 type TyFamInstEqn pass = FamEqn pass (LHsType pass)
1299 -- Here, the @pats@ are type patterns (with kind and type bndrs).
1300 -- See Note [Family instance declaration binders]
1301
1302 -- | Type family default declarations.
1303 -- A convenient synonym for 'TyFamInstDecl'.
1304 -- See @Note [Type family instance declarations in HsSyn]@.
1305 type TyFamDefltDecl = TyFamInstDecl
1306
1307 -- | Located type family default declarations.
1308 type LTyFamDefltDecl pass = XRec pass (TyFamDefltDecl pass)
1309
1310 -- | Located Type Family Instance Declaration
1311 type LTyFamInstDecl pass = XRec pass (TyFamInstDecl pass)
1312
1313 -- | Type Family Instance Declaration
1314 data TyFamInstDecl pass
1315 = TyFamInstDecl { tfid_xtn :: XCTyFamInstDecl pass
1316 , tfid_eqn :: TyFamInstEqn pass }
1317 -- ^
1318 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnType',
1319 -- 'GHC.Parser.Annotation.AnnInstance',
1320
1321 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1322 | XTyFamInstDecl !(XXTyFamInstDecl pass)
1323
1324 ----------------- Data family instances -------------
1325
1326 -- | Located Data Family Instance Declaration
1327 type LDataFamInstDecl pass = XRec pass (DataFamInstDecl pass)
1328
1329 -- | Data Family Instance Declaration
1330 newtype DataFamInstDecl pass
1331 = DataFamInstDecl { dfid_eqn :: FamEqn pass (HsDataDefn pass) }
1332 -- ^
1333 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnData',
1334 -- 'GHC.Parser.Annotation.AnnNewType','GHC.Parser.Annotation.AnnInstance',
1335 -- 'GHC.Parser.Annotation.AnnDcolon'
1336 -- 'GHC.Parser.Annotation.AnnWhere','GHC.Parser.Annotation.AnnOpen',
1337 -- 'GHC.Parser.Annotation.AnnClose'
1338
1339 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1340
1341 ----------------- Family instances (common types) -------------
1342
1343 -- | Family Equation
1344 --
1345 -- One equation in a type family instance declaration, data family instance
1346 -- declaration, or type family default.
1347 -- See Note [Type family instance declarations in HsSyn]
1348 -- See Note [Family instance declaration binders]
1349 data FamEqn pass rhs
1350 = FamEqn
1351 { feqn_ext :: XCFamEqn pass rhs
1352 , feqn_tycon :: LIdP pass
1353 , feqn_bndrs :: HsOuterFamEqnTyVarBndrs pass -- ^ Optional quantified type vars
1354 , feqn_pats :: HsTyPats pass
1355 , feqn_fixity :: LexicalFixity -- ^ Fixity used in the declaration
1356 , feqn_rhs :: rhs
1357 }
1358 -- ^
1359 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnEqual'
1360 | XFamEqn !(XXFamEqn pass rhs)
1361
1362 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1363
1364 ----------------- Class instances -------------
1365
1366 -- | Located Class Instance Declaration
1367 type LClsInstDecl pass = XRec pass (ClsInstDecl pass)
1368
1369 -- | Class Instance Declaration
1370 data ClsInstDecl pass
1371 = ClsInstDecl
1372 { cid_ext :: XCClsInstDecl pass
1373 , cid_poly_ty :: LHsSigType pass -- Context => Class Instance-type
1374 -- Using a polytype means that the renamer conveniently
1375 -- figures out the quantified type variables for us.
1376 , cid_binds :: LHsBinds pass -- Class methods
1377 , cid_sigs :: [LSig pass] -- User-supplied pragmatic info
1378 , cid_tyfam_insts :: [LTyFamInstDecl pass] -- Type family instances
1379 , cid_datafam_insts :: [LDataFamInstDecl pass] -- Data family instances
1380 , cid_overlap_mode :: Maybe (XRec pass OverlapMode)
1381 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen',
1382 -- 'GHC.Parser.Annotation.AnnClose',
1383
1384 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1385 }
1386 -- ^
1387 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnInstance',
1388 -- 'GHC.Parser.Annotation.AnnWhere',
1389 -- 'GHC.Parser.Annotation.AnnOpen','GHC.Parser.Annotation.AnnClose',
1390
1391 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1392 | XClsInstDecl !(XXClsInstDecl pass)
1393
1394 ----------------- Instances of all kinds -------------
1395
1396 -- | Located Instance Declaration
1397 type LInstDecl pass = XRec pass (InstDecl pass)
1398
1399 -- | Instance Declaration
1400 data InstDecl pass -- Both class and family instances
1401 = ClsInstD
1402 { cid_d_ext :: XClsInstD pass
1403 , cid_inst :: ClsInstDecl pass }
1404 | DataFamInstD -- data family instance
1405 { dfid_ext :: XDataFamInstD pass
1406 , dfid_inst :: DataFamInstDecl pass }
1407 | TyFamInstD -- type family instance
1408 { tfid_ext :: XTyFamInstD pass
1409 , tfid_inst :: TyFamInstDecl pass }
1410 | XInstDecl !(XXInstDecl pass)
1411
1412 {-
1413 ************************************************************************
1414 * *
1415 \subsection[DerivDecl]{A stand-alone instance deriving declaration}
1416 * *
1417 ************************************************************************
1418 -}
1419
1420 -- | Located stand-alone 'deriving instance' declaration
1421 type LDerivDecl pass = XRec pass (DerivDecl pass)
1422
1423 -- | Stand-alone 'deriving instance' declaration
1424 data DerivDecl pass = DerivDecl
1425 { deriv_ext :: XCDerivDecl pass
1426 , deriv_type :: LHsSigWcType pass
1427 -- ^ The instance type to derive.
1428 --
1429 -- It uses an 'LHsSigWcType' because the context is allowed to be a
1430 -- single wildcard:
1431 --
1432 -- > deriving instance _ => Eq (Foo a)
1433 --
1434 -- Which signifies that the context should be inferred.
1435
1436 -- See Note [Inferring the instance context] in GHC.Tc.Deriv.Infer.
1437
1438 , deriv_strategy :: Maybe (LDerivStrategy pass)
1439 , deriv_overlap_mode :: Maybe (XRec pass OverlapMode)
1440 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnDeriving',
1441 -- 'GHC.Parser.Annotation.AnnInstance', 'GHC.Parser.Annotation.AnnStock',
1442 -- 'GHC.Parser.Annotation.AnnAnyClass', 'GHC.Parser.Annotation.AnnNewtype',
1443 -- 'GHC.Parser.Annotation.AnnOpen','GHC.Parser.Annotation.AnnClose'
1444
1445 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1446 }
1447 | XDerivDecl !(XXDerivDecl pass)
1448
1449 {-
1450 ************************************************************************
1451 * *
1452 Deriving strategies
1453 * *
1454 ************************************************************************
1455 -}
1456
1457 -- | A 'Located' 'DerivStrategy'.
1458 type LDerivStrategy pass = XRec pass (DerivStrategy pass)
1459
1460 -- | Which technique the user explicitly requested when deriving an instance.
1461 data DerivStrategy pass
1462 -- See Note [Deriving strategies] in GHC.Tc.Deriv
1463 = StockStrategy (XStockStrategy pass)
1464 -- ^ GHC's \"standard\" strategy, which is to implement a
1465 -- custom instance for the data type. This only works
1466 -- for certain types that GHC knows about (e.g., 'Eq',
1467 -- 'Show', 'Functor' when @-XDeriveFunctor@ is enabled,
1468 -- etc.)
1469 | AnyclassStrategy (XAnyClassStrategy pass) -- ^ @-XDeriveAnyClass@
1470 | NewtypeStrategy (XNewtypeStrategy pass) -- ^ @-XGeneralizedNewtypeDeriving@
1471 | ViaStrategy (XViaStrategy pass)
1472 -- ^ @-XDerivingVia@
1473
1474 -- | A short description of a @DerivStrategy'@.
1475 derivStrategyName :: DerivStrategy a -> SDoc
1476 derivStrategyName = text . go
1477 where
1478 go StockStrategy {} = "stock"
1479 go AnyclassStrategy {} = "anyclass"
1480 go NewtypeStrategy {} = "newtype"
1481 go ViaStrategy {} = "via"
1482
1483 {-
1484 ************************************************************************
1485 * *
1486 \subsection[DefaultDecl]{A @default@ declaration}
1487 * *
1488 ************************************************************************
1489
1490 There can only be one default declaration per module, but it is hard
1491 for the parser to check that; we pass them all through in the abstract
1492 syntax, and that restriction must be checked in the front end.
1493 -}
1494
1495 -- | Located Default Declaration
1496 type LDefaultDecl pass = XRec pass (DefaultDecl pass)
1497
1498 -- | Default Declaration
1499 data DefaultDecl pass
1500 = DefaultDecl (XCDefaultDecl pass) [LHsType pass]
1501 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId's : 'GHC.Parser.Annotation.AnnDefault',
1502 -- 'GHC.Parser.Annotation.AnnOpen','GHC.Parser.Annotation.AnnClose'
1503
1504 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1505 | XDefaultDecl !(XXDefaultDecl pass)
1506
1507 {-
1508 ************************************************************************
1509 * *
1510 \subsection{Foreign function interface declaration}
1511 * *
1512 ************************************************************************
1513 -}
1514
1515 -- foreign declarations are distinguished as to whether they define or use a
1516 -- Haskell name
1517 --
1518 -- * the Boolean value indicates whether the pre-standard deprecated syntax
1519 -- has been used
1520
1521 -- | Located Foreign Declaration
1522 type LForeignDecl pass = XRec pass (ForeignDecl pass)
1523
1524 -- | Foreign Declaration
1525 data ForeignDecl pass
1526 = ForeignImport
1527 { fd_i_ext :: XForeignImport pass -- Post typechecker, rep_ty ~ sig_ty
1528 , fd_name :: LIdP pass -- defines this name
1529 , fd_sig_ty :: LHsSigType pass -- sig_ty
1530 , fd_fi :: ForeignImport }
1531
1532 | ForeignExport
1533 { fd_e_ext :: XForeignExport pass -- Post typechecker, rep_ty ~ sig_ty
1534 , fd_name :: LIdP pass -- uses this name
1535 , fd_sig_ty :: LHsSigType pass -- sig_ty
1536 , fd_fe :: ForeignExport }
1537 -- ^
1538 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnForeign',
1539 -- 'GHC.Parser.Annotation.AnnImport','GHC.Parser.Annotation.AnnExport',
1540 -- 'GHC.Parser.Annotation.AnnDcolon'
1541
1542 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1543 | XForeignDecl !(XXForeignDecl pass)
1544
1545 {-
1546 In both ForeignImport and ForeignExport:
1547 sig_ty is the type given in the Haskell code
1548 rep_ty is the representation for this type, i.e. with newtypes
1549 coerced away and type functions evaluated.
1550 Thus if the declaration is valid, then rep_ty will only use types
1551 such as Int and IO that we know how to make foreign calls with.
1552 -}
1553
1554 -- Specification Of an imported external entity in dependence on the calling
1555 -- convention
1556 --
1557 data ForeignImport = -- import of a C entity
1558 --
1559 -- * the two strings specifying a header file or library
1560 -- may be empty, which indicates the absence of a
1561 -- header or object specification (both are not used
1562 -- in the case of `CWrapper' and when `CFunction'
1563 -- has a dynamic target)
1564 --
1565 -- * the calling convention is irrelevant for code
1566 -- generation in the case of `CLabel', but is needed
1567 -- for pretty printing
1568 --
1569 -- * `Safety' is irrelevant for `CLabel' and `CWrapper'
1570 --
1571 CImport (Located CCallConv) -- ccall or stdcall
1572 (Located Safety) -- interruptible, safe or unsafe
1573 (Maybe Header) -- name of C header
1574 CImportSpec -- details of the C entity
1575 (Located SourceText) -- original source text for
1576 -- the C entity
1577 deriving Data
1578
1579 -- details of an external C entity
1580 --
1581 data CImportSpec = CLabel CLabelString -- import address of a C label
1582 | CFunction CCallTarget -- static or dynamic function
1583 | CWrapper -- wrapper to expose closures
1584 -- (former f.e.d.)
1585 deriving Data
1586
1587 -- specification of an externally exported entity in dependence on the calling
1588 -- convention
1589 --
1590 data ForeignExport = CExport (Located CExportSpec) -- contains the calling
1591 -- convention
1592 (Located SourceText) -- original source text for
1593 -- the C entity
1594 deriving Data
1595
1596 -- pretty printing of foreign declarations
1597 --
1598
1599 instance Outputable ForeignImport where
1600 ppr (CImport cconv safety mHeader spec (L _ srcText)) =
1601 ppr cconv <+> ppr safety
1602 <+> pprWithSourceText srcText (pprCEntity spec "")
1603 where
1604 pp_hdr = case mHeader of
1605 Nothing -> empty
1606 Just (Header _ header) -> ftext header
1607
1608 pprCEntity (CLabel lbl) _ =
1609 doubleQuotes $ text "static" <+> pp_hdr <+> char '&' <> ppr lbl
1610 pprCEntity (CFunction (StaticTarget st _lbl _ isFun)) src =
1611 if dqNeeded then doubleQuotes ce else empty
1612 where
1613 dqNeeded = (take 6 src == "static")
1614 || isJust mHeader
1615 || not isFun
1616 || st /= NoSourceText
1617 ce =
1618 -- We may need to drop leading spaces first
1619 (if take 6 src == "static" then text "static" else empty)
1620 <+> pp_hdr
1621 <+> (if isFun then empty else text "value")
1622 <+> (pprWithSourceText st empty)
1623 pprCEntity (CFunction DynamicTarget) _ =
1624 doubleQuotes $ text "dynamic"
1625 pprCEntity CWrapper _ = doubleQuotes $ text "wrapper"
1626
1627 instance Outputable ForeignExport where
1628 ppr (CExport (L _ (CExportStatic _ lbl cconv)) _) =
1629 ppr cconv <+> char '"' <> ppr lbl <> char '"'
1630
1631 {-
1632 ************************************************************************
1633 * *
1634 \subsection{Rewrite rules}
1635 * *
1636 ************************************************************************
1637 -}
1638
1639 -- | Located Rule Declarations
1640 type LRuleDecls pass = XRec pass (RuleDecls pass)
1641
1642 -- Note [Pragma source text] in GHC.Types.SourceText
1643 -- | Rule Declarations
1644 data RuleDecls pass = HsRules { rds_ext :: XCRuleDecls pass
1645 , rds_src :: SourceText
1646 , rds_rules :: [LRuleDecl pass] }
1647 | XRuleDecls !(XXRuleDecls pass)
1648
1649 -- | Located Rule Declaration
1650 type LRuleDecl pass = XRec pass (RuleDecl pass)
1651
1652 -- | Rule Declaration
1653 data RuleDecl pass
1654 = HsRule -- Source rule
1655 { rd_ext :: XHsRule pass
1656 -- ^ After renamer, free-vars from the LHS and RHS
1657 , rd_name :: XRec pass (SourceText,RuleName)
1658 -- ^ Note [Pragma source text] in "GHC.Types.Basic"
1659 , rd_act :: Activation
1660 , rd_tyvs :: Maybe [LHsTyVarBndr () (NoGhcTc pass)]
1661 -- ^ Forall'd type vars
1662 , rd_tmvs :: [LRuleBndr pass]
1663 -- ^ Forall'd term vars, before typechecking; after typechecking
1664 -- this includes all forall'd vars
1665 , rd_lhs :: XRec pass (HsExpr pass)
1666 , rd_rhs :: XRec pass (HsExpr pass)
1667 }
1668 -- ^
1669 -- - 'GHC.Parser.Annotation.AnnKeywordId' :
1670 -- 'GHC.Parser.Annotation.AnnOpen','GHC.Parser.Annotation.AnnTilde',
1671 -- 'GHC.Parser.Annotation.AnnVal',
1672 -- 'GHC.Parser.Annotation.AnnClose',
1673 -- 'GHC.Parser.Annotation.AnnForall','GHC.Parser.Annotation.AnnDot',
1674 -- 'GHC.Parser.Annotation.AnnEqual',
1675 | XRuleDecl !(XXRuleDecl pass)
1676
1677 data HsRuleRn = HsRuleRn NameSet NameSet -- Free-vars from the LHS and RHS
1678 deriving Data
1679
1680 -- | Located Rule Binder
1681 type LRuleBndr pass = XRec pass (RuleBndr pass)
1682
1683 -- | Rule Binder
1684 data RuleBndr pass
1685 = RuleBndr (XCRuleBndr pass) (LIdP pass)
1686 | RuleBndrSig (XRuleBndrSig pass) (LIdP pass) (HsPatSigType pass)
1687 | XRuleBndr !(XXRuleBndr pass)
1688 -- ^
1689 -- - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen',
1690 -- 'GHC.Parser.Annotation.AnnDcolon','GHC.Parser.Annotation.AnnClose'
1691
1692 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1693
1694 collectRuleBndrSigTys :: [RuleBndr pass] -> [HsPatSigType pass]
1695 collectRuleBndrSigTys bndrs = [ty | RuleBndrSig _ _ ty <- bndrs]
1696
1697 pprFullRuleName :: GenLocated a (SourceText, RuleName) -> SDoc
1698 pprFullRuleName (L _ (st, n)) = pprWithSourceText st (doubleQuotes $ ftext n)
1699
1700 {-
1701 ************************************************************************
1702 * *
1703 \subsection[DocDecl]{Document comments}
1704 * *
1705 ************************************************************************
1706 -}
1707
1708 -- | Located Documentation comment Declaration
1709 type LDocDecl pass = XRec pass (DocDecl)
1710
1711 -- | Documentation comment Declaration
1712 data DocDecl
1713 = DocCommentNext HsDocString
1714 | DocCommentPrev HsDocString
1715 | DocCommentNamed String HsDocString
1716 | DocGroup Int HsDocString
1717 deriving Data
1718
1719 -- Okay, I need to reconstruct the document comments, but for now:
1720 instance Outputable DocDecl where
1721 ppr _ = text "<document comment>"
1722
1723 docDeclDoc :: DocDecl -> HsDocString
1724 docDeclDoc (DocCommentNext d) = d
1725 docDeclDoc (DocCommentPrev d) = d
1726 docDeclDoc (DocCommentNamed _ d) = d
1727 docDeclDoc (DocGroup _ d) = d
1728
1729 {-
1730 ************************************************************************
1731 * *
1732 \subsection[DeprecDecl]{Deprecations}
1733 * *
1734 ************************************************************************
1735
1736 We use exported entities for things to deprecate.
1737 -}
1738
1739 -- | Located Warning Declarations
1740 type LWarnDecls pass = XRec pass (WarnDecls pass)
1741
1742 -- Note [Pragma source text] in GHC.Types.SourceText
1743 -- | Warning pragma Declarations
1744 data WarnDecls pass = Warnings { wd_ext :: XWarnings pass
1745 , wd_src :: SourceText
1746 , wd_warnings :: [LWarnDecl pass]
1747 }
1748 | XWarnDecls !(XXWarnDecls pass)
1749
1750 -- | Located Warning pragma Declaration
1751 type LWarnDecl pass = XRec pass (WarnDecl pass)
1752
1753 -- | Warning pragma Declaration
1754 data WarnDecl pass = Warning (XWarning pass) [LIdP pass] WarningTxt
1755 | XWarnDecl !(XXWarnDecl pass)
1756
1757 {-
1758 ************************************************************************
1759 * *
1760 \subsection[AnnDecl]{Annotations}
1761 * *
1762 ************************************************************************
1763 -}
1764
1765 -- | Located Annotation Declaration
1766 type LAnnDecl pass = XRec pass (AnnDecl pass)
1767
1768 -- | Annotation Declaration
1769 data AnnDecl pass = HsAnnotation
1770 (XHsAnnotation pass)
1771 SourceText -- Note [Pragma source text] in GHC.Types.SourceText
1772 (AnnProvenance pass) (XRec pass (HsExpr pass))
1773 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnOpen',
1774 -- 'GHC.Parser.Annotation.AnnType'
1775 -- 'GHC.Parser.Annotation.AnnModule'
1776 -- 'GHC.Parser.Annotation.AnnClose'
1777
1778 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1779 | XAnnDecl !(XXAnnDecl pass)
1780
1781 -- | Annotation Provenance
1782 data AnnProvenance pass = ValueAnnProvenance (LIdP pass)
1783 | TypeAnnProvenance (LIdP pass)
1784 | ModuleAnnProvenance
1785 -- deriving instance Functor AnnProvenance
1786 -- deriving instance Foldable AnnProvenance
1787 -- deriving instance Traversable AnnProvenance
1788 -- deriving instance (Data pass) => Data (AnnProvenance pass)
1789
1790 annProvenanceName_maybe :: forall p. UnXRec p => AnnProvenance p -> Maybe (IdP p)
1791 annProvenanceName_maybe (ValueAnnProvenance (unXRec @p -> name)) = Just name
1792 annProvenanceName_maybe (TypeAnnProvenance (unXRec @p -> name)) = Just name
1793 annProvenanceName_maybe ModuleAnnProvenance = Nothing
1794
1795 {-
1796 ************************************************************************
1797 * *
1798 \subsection[RoleAnnot]{Role annotations}
1799 * *
1800 ************************************************************************
1801 -}
1802
1803 -- | Located Role Annotation Declaration
1804 type LRoleAnnotDecl pass = XRec pass (RoleAnnotDecl pass)
1805
1806 -- See #8185 for more info about why role annotations are
1807 -- top-level declarations
1808 -- | Role Annotation Declaration
1809 data RoleAnnotDecl pass
1810 = RoleAnnotDecl (XCRoleAnnotDecl pass)
1811 (LIdP pass) -- type constructor
1812 [XRec pass (Maybe Role)] -- optional annotations
1813 -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : 'GHC.Parser.Annotation.AnnType',
1814 -- 'GHC.Parser.Annotation.AnnRole'
1815
1816 -- For details on above see note [exact print annotations] in GHC.Parser.Annotation
1817 | XRoleAnnotDecl !(XXRoleAnnotDecl pass)