1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
|
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
-- in module PlaceHolder
module HsExtension where
-- This module captures the type families to precisely identify the extension
-- points for HsSyn
import GhcPrelude
import GHC.Exts (Constraint)
import Data.Data hiding ( Fixity )
import PlaceHolder
import BasicTypes
import ConLike
import NameSet
import Name
import RdrName
import Var
import Type ( Type )
import Outputable
import SrcLoc (Located)
import Coercion
import TcEvidence
{-
Note [Trees that grow]
~~~~~~~~~~~~~~~~~~~~~~
See https://ghc.haskell.org/trac/ghc/wiki/ImplementingTreesThatGrow
The hsSyn AST is reused across multiple compiler passes. We also have the
Template Haskell AST, and the haskell-src-exts one (outside of GHC)
Supporting multiple passes means the AST has various warts on it to cope with
the specifics for the phases, such as the 'ValBindsOut', 'ConPatOut',
'SigPatOut' etc.
The growable AST will allow each of these variants to be captured explicitly,
such that they only exist in the given compiler pass AST, as selected by the
type parameter to the AST.
In addition it will allow tool writers to define their own extensions to capture
additional information for the tool, in a natural way.
A further goal is to provide a means to harmonise the Template Haskell and
haskell-src-exts ASTs as well.
-}
-- | Used when constructing a term with an unused extension point.
noExt :: PlaceHolder
noExt = PlaceHolder
-- | Used as a data type index for the hsSyn AST
data GhcPass (c :: Pass)
deriving instance Eq (GhcPass c)
deriving instance Typeable c => Data (GhcPass c)
data Pass = Parsed | Renamed | Typechecked
deriving (Data)
-- Type synonyms as a shorthand for tagging
type GhcPs = GhcPass 'Parsed -- Old 'RdrName' type param
type GhcRn = GhcPass 'Renamed -- Old 'Name' type param
type GhcTc = GhcPass 'Typechecked -- Old 'Id' type para,
type GhcTcId = GhcTc -- Old 'TcId' type param
-- | Types that are not defined until after type checking
type family PostTc x ty -- Note [Pass sensitive types] in PlaceHolder
type instance PostTc GhcPs ty = PlaceHolder
type instance PostTc GhcRn ty = PlaceHolder
type instance PostTc GhcTc ty = ty
-- deriving instance (Data ty) => Data (PostTc (GhcPass 'Parsed) ty)
-- | Types that are not defined until after renaming
type family PostRn x ty -- Note [Pass sensitive types] in PlaceHolder
type instance PostRn GhcPs ty = PlaceHolder
type instance PostRn GhcRn ty = ty
type instance PostRn GhcTc ty = ty
-- | Maps the "normal" id type for a given pass
type family IdP p
type instance IdP GhcPs = RdrName
type instance IdP GhcRn = Name
type instance IdP GhcTc = Id
-- type instance IdP (GHC x) = IdP x
type LIdP p = Located (IdP p)
-- ---------------------------------------------------------------------
-- type families for the Pat extension points
type family XWildPat x
type family XVarPat x
type family XLazyPat x
type family XAsPat x
type family XParPat x
type family XBangPat x
type family XListPat x
type family XTuplePat x
type family XSumPat x
type family XPArrPat x
type family XConPat x
type family XViewPat x
type family XSplicePat x
type family XLitPat x
type family XNPat x
type family XNPlusKPat x
type family XSigPat x
type family XCoPat x
type family XXPat x
type ForallXPat (c :: * -> Constraint) (x :: *) =
( c (XWildPat x)
, c (XVarPat x)
, c (XLazyPat x)
, c (XAsPat x)
, c (XParPat x)
, c (XBangPat x)
, c (XListPat x)
, c (XTuplePat x)
, c (XSumPat x)
, c (XPArrPat x)
, c (XViewPat x)
, c (XSplicePat x)
, c (XLitPat x)
, c (XNPat x)
, c (XNPlusKPat x)
, c (XSigPat x)
, c (XCoPat x)
, c (XXPat x)
)
-- ---------------------------------------------------------------------
-- ValBindsLR type families
type family XValBinds x x'
type family XXValBindsLR x x'
type ForallXValBindsLR (c :: * -> Constraint) (x :: *) (x' :: *) =
( c (XValBinds x x')
, c (XXValBindsLR x x')
)
-- We define a type family for each HsLit extension point. This is based on
-- prepending 'X' to the constructor name, for ease of reference.
type family XHsChar x
type family XHsCharPrim x
type family XHsString x
type family XHsStringPrim x
type family XHsInt x
type family XHsIntPrim x
type family XHsWordPrim x
type family XHsInt64Prim x
type family XHsWord64Prim x
type family XHsInteger x
type family XHsRat x
type family XHsFloatPrim x
type family XHsDoublePrim x
type family XXLit x
-- | Helper to apply a constraint to all HsLit extension points. It has one
-- entry per extension point type family.
type ForallXHsLit (c :: * -> Constraint) (x :: *) =
( c (XHsChar x)
, c (XHsCharPrim x)
, c (XHsString x)
, c (XHsStringPrim x)
, c (XHsInt x)
, c (XHsIntPrim x)
, c (XHsWordPrim x)
, c (XHsInt64Prim x)
, c (XHsWord64Prim x)
, c (XHsInteger x)
, c (XHsRat x)
, c (XHsFloatPrim x)
, c (XHsDoublePrim x)
, c (XXLit x)
)
type family XOverLit x
type family XXOverLit x
type ForallXOverLit (c :: * -> Constraint) (x :: *) =
( c (XOverLit x)
, c (XXOverLit x)
)
-- ---------------------------------------------------------------------
-- Type families for the Type type families
type family XForAllTy x
type family XQualTy x
type family XTyVar x
type family XAppsTy x
type family XAppTy x
type family XFunTy x
type family XListTy x
type family XPArrTy x
type family XTupleTy x
type family XSumTy x
type family XOpTy x
type family XParTy x
type family XIParamTy x
type family XEqTy x
type family XKindSig x
type family XSpliceTy x
type family XDocTy x
type family XBangTy x
type family XRecTy x
type family XExplicitListTy x
type family XExplicitTupleTy x
type family XTyLit x
type family XWildCardTy x
type family XXType x
-- | Helper to apply a constraint to all extension points. It has one
-- entry per extension point type family.
type ForallXType (c :: * -> Constraint) (x :: *) =
( c (XForAllTy x)
, c (XQualTy x)
, c (XTyVar x)
, c (XAppsTy x)
, c (XAppTy x)
, c (XFunTy x)
, c (XListTy x)
, c (XPArrTy x)
, c (XTupleTy x)
, c (XSumTy x)
, c (XOpTy x)
, c (XParTy x)
, c (XIParamTy x)
, c (XEqTy x)
, c (XKindSig x)
, c (XSpliceTy x)
, c (XDocTy x)
, c (XBangTy x)
, c (XRecTy x)
, c (XExplicitListTy x)
, c (XExplicitTupleTy x)
, c (XTyLit x)
, c (XWildCardTy x)
, c (XXType x)
)
-- ---------------------------------------------------------------------
type family XUserTyVar x
type family XKindedTyVar x
type family XXTyVarBndr x
type ForallXTyVarBndr (c :: * -> Constraint) (x :: *) =
( c (XUserTyVar x)
, c (XKindedTyVar x)
, c (XXTyVarBndr x)
)
-- ---------------------------------------------------------------------
type family XAppInfix x
type family XAppPrefix x
type family XXAppType x
type ForallXAppType (c :: * -> Constraint) (x :: *) =
( c (XAppInfix x)
, c (XAppPrefix x)
, c (XXAppType x)
)
-- ---------------------------------------------------------------------
type family XFieldOcc x
type family XXFieldOcc x
type ForallXFieldOcc (c :: * -> Constraint) (x :: *) =
( c (XFieldOcc x)
, c (XXFieldOcc x)
)
-- ---------------------------------------------------------------------
type family XUnambiguous x
type family XAmbiguous x
type family XXAmbiguousFieldOcc x
type ForallXAmbiguousFieldOcc (c :: * -> Constraint) (x :: *) =
( c (XUnambiguous x)
, c (XAmbiguous x)
, c (XXAmbiguousFieldOcc x)
)
-- ---------------------------------------------------------------------
-- Type families for the HsExpr type families
type family XVar x
type family XUnboundVar x
type family XConLikeOut x
type family XRecFld x
type family XOverLabel x
type family XIPVar x
type family XOverLitE x
type family XLitE x
type family XLam x
type family XLamCase x
type family XApp x
type family XAppTypeE x
type family XOpApp x
type family XNegApp x
type family XPar x
type family XSectionL x
type family XSectionR x
type family XExplicitTuple x
type family XExplicitSum x
type family XCase x
type family XIf x
type family XMultiIf x
type family XLet x
type family XDo x
type family XExplicitList x
type family XExplicitPArr x
type family XRecordCon x
type family XRecordUpd x
type family XExprWithTySig x
type family XArithSeq x
type family XPArrSeq x
type family XSCC x
type family XCoreAnn x
type family XBracket x
type family XRnBracketOut x
type family XTcBracketOut x
type family XSpliceE x
type family XProc x
type family XStatic x
type family XArrApp x
type family XArrForm x
type family XTick x
type family XBinTick x
type family XTickPragma x
type family XEWildPat x
type family XEAsPat x
type family XEViewPat x
type family XELazyPat x
type family XWrap x
type family XXExpr x
type ForallXExpr (c :: * -> Constraint) (x :: *) =
( c (XVar x)
, c (XUnboundVar x)
, c (XConLikeOut x)
, c (XRecFld x)
, c (XOverLabel x)
, c (XIPVar x)
, c (XOverLitE x)
, c (XLitE x)
, c (XLam x)
, c (XLamCase x)
, c (XApp x)
, c (XAppTypeE x)
, c (XOpApp x)
, c (XNegApp x)
, c (XPar x)
, c (XSectionL x)
, c (XSectionR x)
, c (XExplicitTuple x)
, c (XExplicitSum x)
, c (XCase x)
, c (XIf x)
, c (XMultiIf x)
, c (XLet x)
, c (XDo x)
, c (XExplicitList x)
, c (XExplicitPArr x)
, c (XRecordCon x)
, c (XRecordUpd x)
, c (XExprWithTySig x)
, c (XArithSeq x)
, c (XPArrSeq x)
, c (XSCC x)
, c (XCoreAnn x)
, c (XBracket x)
, c (XRnBracketOut x)
, c (XTcBracketOut x)
, c (XSpliceE x)
, c (XProc x)
, c (XStatic x)
, c (XArrApp x)
, c (XArrForm x)
, c (XTick x)
, c (XBinTick x)
, c (XTickPragma x)
, c (XEWildPat x)
, c (XEAsPat x)
, c (XEViewPat x)
, c (XELazyPat x)
, c (XWrap x)
, c (XXExpr x)
)
-- ---------------------------------------------------------------------
type family XPresent x
type family XMissing x
type family XXTupArg x
type ForallXTupArg (c :: * -> Constraint) (x :: *) =
( c (XPresent x)
, c (XMissing x)
, c (XXTupArg x)
)
-- ---------------------------------------------------------------------
type family XTypedSplice x
type family XUntypedSplice x
type family XQuasiQuote x
type family XSpliced x
type family XXSplice x
type ForallXSplice (c :: * -> Constraint) (x :: *) =
( c (XTypedSplice x)
, c (XUntypedSplice x)
, c (XQuasiQuote x)
, c (XSpliced x)
, c (XXSplice x)
)
-- ---------------------------------------------------------------------
type family XExpBr x
type family XPatBr x
type family XDecBrL x
type family XDecBrG x
type family XTypBr x
type family XVarBr x
type family XTExpBr x
type family XXBracket x
type ForallXBracket (c :: * -> Constraint) (x :: *) =
( c (XExpBr x)
, c (XPatBr x)
, c (XDecBrL x)
, c (XDecBrG x)
, c (XTypBr x)
, c (XVarBr x)
, c (XTExpBr x)
, c (XXBracket x)
)
-- ---------------------------------------------------------------------
type family XCmdTop x
type family XXCmdTop x
type ForallXCmdTop (c :: * -> Constraint) (x :: *) =
( c (XCmdTop x)
, c (XXCmdTop x)
)
-- ---------------------------------------------------------------------
type family XCmdArrApp x
type family XCmdArrForm x
type family XCmdApp x
type family XCmdLam x
type family XCmdPar x
type family XCmdCase x
type family XCmdIf x
type family XCmdLet x
type family XCmdDo x
type family XCmdWrap x
type family XXCmd x
type ForallXCmd (c :: * -> Constraint) (x :: *) =
( c (XCmdArrApp x)
, c (XCmdArrForm x)
, c (XCmdApp x)
, c (XCmdLam x)
, c (XCmdPar x)
, c (XCmdCase x)
, c (XCmdIf x)
, c (XCmdLet x)
, c (XCmdDo x)
, c (XCmdWrap x)
, c (XXCmd x)
)
-- ---------------------------------------------------------------------
type family XParStmtBlock x x'
type family XXParStmtBlock x x'
type ForallXParStmtBlock (c :: * -> Constraint) (x :: *) (x' :: *) =
( c (XParStmtBlock x x')
, c (XXParStmtBlock x x')
)
-- ---------------------------------------------------------------------
-- | The 'SourceText' fields have been moved into the extension fields, thus
-- placing a requirement in the extension field to contain a 'SourceText' so
-- that the pretty printing and round tripping of source can continue to
-- operate.
--
-- The 'HasSourceText' class captures this requirement for the relevant fields.
class HasSourceText a where
-- Provide setters to mimic existing constructors
noSourceText :: a
sourceText :: String -> a
setSourceText :: SourceText -> a
getSourceText :: a -> SourceText
-- | Provide a summary constraint that lists all the extension points requiring
-- the 'HasSourceText' class, so that it can be changed in one place as the
-- named extensions change throughout the AST.
type SourceTextX x =
( HasSourceText (XHsChar x)
, HasSourceText (XHsCharPrim x)
, HasSourceText (XHsString x)
, HasSourceText (XHsStringPrim x)
, HasSourceText (XHsIntPrim x)
, HasSourceText (XHsWordPrim x)
, HasSourceText (XHsInt64Prim x)
, HasSourceText (XHsWord64Prim x)
, HasSourceText (XHsInteger x)
)
-- | 'SourceText' trivially implements 'HasSourceText'
instance HasSourceText SourceText where
noSourceText = NoSourceText
sourceText s = SourceText s
setSourceText s = s
getSourceText a = a
-- ----------------------------------------------------------------------
-- | Conversion of annotations from one type index to another. This is required
-- where the AST is converted from one pass to another, and the extension values
-- need to be brought along if possible. So for example a 'SourceText' is
-- converted via 'id', but needs a type signature to keep the type checker
-- happy.
class Convertable a b | a -> b where
convert :: a -> b
instance Convertable a a where
convert = id
-- | A constraint capturing all the extension points that can be converted via
-- @instance Convertable a a@
type ConvertIdX a b =
(XHsDoublePrim a ~ XHsDoublePrim b,
XHsFloatPrim a ~ XHsFloatPrim b,
XHsRat a ~ XHsRat b,
XHsInteger a ~ XHsInteger b,
XHsWord64Prim a ~ XHsWord64Prim b,
XHsInt64Prim a ~ XHsInt64Prim b,
XHsWordPrim a ~ XHsWordPrim b,
XHsIntPrim a ~ XHsIntPrim b,
XHsInt a ~ XHsInt b,
XHsStringPrim a ~ XHsStringPrim b,
XHsString a ~ XHsString b,
XHsCharPrim a ~ XHsCharPrim b,
XHsChar a ~ XHsChar b,
XXLit a ~ XXLit b)
-- ----------------------------------------------------------------------
-- | Provide a summary constraint that gives all am Outputable constraint to
-- extension points needing one
type OutputableX p =
( Outputable (XXPat p)
, Outputable (XXPat GhcRn)
, Outputable (XSigPat p)
, Outputable (XSigPat GhcRn)
, Outputable (XXLit p)
, Outputable (XXOverLit p)
, Outputable (XXType p)
, Outputable (XExprWithTySig p)
, Outputable (XExprWithTySig GhcRn)
, Outputable (XAppTypeE p)
, Outputable (XAppTypeE GhcRn)
-- , Outputable (XXParStmtBlock (GhcPass idL) idR)
)
-- TODO: Should OutputableX be included in OutputableBndrId?
-- ----------------------------------------------------------------------
--
type DataId p =
( Data p
, ForallXHsLit Data p
, ForallXPat Data p
-- Th following GhcRn constraints should go away once TTG is fully implemented
, ForallXPat Data GhcRn
, ForallXType Data GhcRn
, ForallXExpr Data GhcRn
, ForallXTupArg Data GhcRn
, ForallXSplice Data GhcRn
, ForallXBracket Data GhcRn
, ForallXCmdTop Data GhcRn
, ForallXCmd Data GhcRn
, ForallXOverLit Data p
, ForallXType Data p
, ForallXTyVarBndr Data p
, ForallXAppType Data p
, ForallXFieldOcc Data p
, ForallXAmbiguousFieldOcc Data p
, ForallXExpr Data p
, ForallXTupArg Data p
, ForallXSplice Data p
, ForallXBracket Data p
, ForallXCmdTop Data p
, ForallXCmd Data p
, Data (NameOrRdrName (IdP p))
, Data (IdP p)
, Data (PostRn p (IdP p))
, Data (PostRn p (Located Name))
, Data (PostRn p Bool)
, Data (PostRn p Fixity)
, Data (PostRn p NameSet)
, Data (PostRn p [Name])
, Data (PostTc p (IdP p))
, Data (PostTc p Coercion)
, Data (PostTc p ConLike)
, Data (PostTc p HsWrapper)
, Data (PostTc p Type)
, Data (PostTc p [ConLike])
, Data (PostTc p [Type])
)
type DataIdLR pL pR =
( DataId pL
, DataId pR
, ForallXValBindsLR Data pL pR
, ForallXValBindsLR Data pL pL
, ForallXValBindsLR Data pR pR
, ForallXParStmtBlock Data pL pR
, ForallXParStmtBlock Data pL pL
, ForallXParStmtBlock Data pR pR
, ForallXParStmtBlock Data GhcRn GhcRn
)
-- |Constraint type to bundle up the requirement for 'OutputableBndr' on both
-- the @id@ and the 'NameOrRdrName' type for it
type OutputableBndrId id =
( OutputableBndr (NameOrRdrName (IdP id))
, OutputableBndr (IdP id)
, OutputableX id
)
|