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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
|
%
% (c) The AQUA Project, Glasgow University, 1994-1996
%
\section{Read parse tree built by Yacc parser}
\begin{code}
#include "HsVersions.h"
module ReadPrefix ( rdModule ) where
IMP_Ubiq()
IMPORT_1_3(IO(hPutStr, stderr))
IMPORT_1_3(GHCio(stThen))
import UgenAll -- all Yacc parser gumpff...
import PrefixSyn -- and various syntaxen.
import HsSyn
import HsTypes ( HsTyVar(..) )
import HsPragmas ( noDataPragmas, noClassPragmas, noInstancePragmas, noGenPragmas )
import RdrHsSyn
import PrefixToHs
import ErrUtils ( addErrLoc, ghcExit )
import FiniteMap ( elemFM, FiniteMap )
import Name ( RdrName(..), OccName(..) )
import Lex ( isLexConId )
import PprStyle ( PprStyle(..) )
import PrelMods
import Pretty
import SrcLoc ( mkGeneratedSrcLoc, noSrcLoc, SrcLoc )
import Util ( nOfThem, pprError, panic )
\end{code}
%************************************************************************
%* *
\subsection[ReadPrefix-help]{Help Functions}
%* *
%************************************************************************
\begin{code}
wlkList :: (U_VOID_STAR -> UgnM a) -> U_list -> UgnM [a]
wlkList wlk_it U_lnil = returnUgn []
wlkList wlk_it (U_lcons hd tl)
= wlk_it hd `thenUgn` \ hd_it ->
wlkList wlk_it tl `thenUgn` \ tl_it ->
returnUgn (hd_it : tl_it)
\end{code}
\begin{code}
wlkMaybe :: (U_VOID_STAR -> UgnM a) -> U_maybe -> UgnM (Maybe a)
wlkMaybe wlk_it U_nothing = returnUgn Nothing
wlkMaybe wlk_it (U_just x)
= wlk_it x `thenUgn` \ it ->
returnUgn (Just it)
\end{code}
\begin{code}
wlkTvId = wlkQid TvOcc
wlkTCId = wlkQid TCOcc
wlkVarId = wlkQid VarOcc
wlkDataId = wlkQid VarOcc
wlkEntId = wlkQid (\occ -> if isLexConId occ
then TCOcc occ
else VarOcc occ)
wlkQid :: (FAST_STRING -> OccName) -> U_qid -> UgnM RdrName
wlkQid mk_occ_name (U_noqual name)
= returnUgn (Unqual (mk_occ_name name))
wlkQid mk_occ_name (U_aqual mod name)
= returnUgn (Qual mod (mk_occ_name name))
-- I don't understand this one! It is what shows up when we meet (), [], or (,,,).
wlkQid mk_occ_name (U_gid n name)
= returnUgn (Unqual (mk_occ_name name))
rdTCId pt = rdU_qid pt `thenUgn` \ qid -> wlkTCId qid
rdVarId pt = rdU_qid pt `thenUgn` \ qid -> wlkVarId qid
cvFlag :: U_long -> Bool
cvFlag 0 = False
cvFlag 1 = True
\end{code}
%************************************************************************
%* *
\subsection[rdModule]{@rdModule@: reads in a Haskell module}
%* *
%************************************************************************
\begin{code}
#if __GLASGOW_HASKELL__ >= 200
# define PACK_STR packCString
# define CCALL_THEN `stThen`
#else
# define PACK_STR _packCString
# define CCALL_THEN `thenPrimIO`
#endif
rdModule :: IO (Module, -- this module's name
RdrNameHsModule) -- the main goods
rdModule
= _ccall_ hspmain CCALL_THEN \ pt -> -- call the Yacc parser!
let
srcfile = PACK_STR ``input_filename'' -- What A Great Hack! (TM)
in
initUgn $
rdU_tree pt `thenUgn` \ (U_hmodule modname himplist hexplist hfixlist
hmodlist srciface_version srcline) ->
setSrcFileUgn srcfile $
setSrcModUgn modname $
mkSrcLocUgn srcline $ \ src_loc ->
wlkMaybe rdEntities hexplist `thenUgn` \ exports ->
wlkList rdImport himplist `thenUgn` \ imports ->
wlkList rdFixOp hfixlist `thenUgn` \ fixities ->
wlkBinding hmodlist `thenUgn` \ binding ->
let
val_decl = ValD (cvBinds srcfile cvValSig binding)
other_decls = cvOtherDecls binding
in
returnUgn (modname,
HsModule modname
(case srciface_version of { 0 -> Nothing; n -> Just n })
exports
imports
fixities
(val_decl: other_decls)
src_loc
)
\end{code}
%************************************************************************
%* *
\subsection[wlkExprOrPat]{@wlkExpr@ and @wlkPat@}
%* *
%************************************************************************
\begin{code}
rdExpr :: ParseTree -> UgnM RdrNameHsExpr
rdPat :: ParseTree -> UgnM RdrNamePat
rdExpr pt = rdU_tree pt `thenUgn` \ tree -> wlkExpr tree
rdPat pt = rdU_tree pt `thenUgn` \ tree -> wlkPat tree
wlkExpr :: U_tree -> UgnM RdrNameHsExpr
wlkPat :: U_tree -> UgnM RdrNamePat
wlkExpr expr
= case expr of
U_par pexpr -> -- parenthesised expr
wlkExpr pexpr `thenUgn` \ expr ->
returnUgn (HsPar expr)
U_lsection lsexp lop -> -- left section
wlkExpr lsexp `thenUgn` \ expr ->
wlkVarId lop `thenUgn` \ op ->
returnUgn (SectionL expr (HsVar op))
U_rsection rop rsexp -> -- right section
wlkVarId rop `thenUgn` \ op ->
wlkExpr rsexp `thenUgn` \ expr ->
returnUgn (SectionR (HsVar op) expr)
U_ccall fun flavor ccargs -> -- ccall/casm
wlkList rdExpr ccargs `thenUgn` \ args ->
let
tag = _HEAD_ flavor
in
returnUgn (CCall fun args
(tag == 'p' || tag == 'P') -- may invoke GC
(tag == 'N' || tag == 'P') -- really a "casm"
(panic "CCall:result_ty"))
U_scc label sccexp -> -- scc (set-cost-centre) expression
wlkExpr sccexp `thenUgn` \ expr ->
returnUgn (HsSCC label expr)
U_lambda lampats lamexpr srcline -> -- lambda expression
mkSrcLocUgn srcline $ \ src_loc ->
wlkList rdPat lampats `thenUgn` \ pats ->
wlkExpr lamexpr `thenUgn` \ body ->
returnUgn (
HsLam (foldr PatMatch
(GRHSMatch (GRHSsAndBindsIn
[OtherwiseGRHS body src_loc]
EmptyBinds))
pats)
)
U_casee caseexpr casebody srcline -> -- case expression
mkSrcLocUgn srcline $ \ src_loc ->
wlkExpr caseexpr `thenUgn` \ expr ->
wlkList rdMatch casebody `thenUgn` \ mats ->
getSrcFileUgn `thenUgn` \ sf ->
let
matches = cvMatches sf True mats
in
returnUgn (HsCase expr matches src_loc)
U_ife ifpred ifthen ifelse srcline -> -- if expression
mkSrcLocUgn srcline $ \ src_loc ->
wlkExpr ifpred `thenUgn` \ e1 ->
wlkExpr ifthen `thenUgn` \ e2 ->
wlkExpr ifelse `thenUgn` \ e3 ->
returnUgn (HsIf e1 e2 e3 src_loc)
U_let letvdefs letvexpr -> -- let expression
wlkBinding letvdefs `thenUgn` \ binding ->
wlkExpr letvexpr `thenUgn` \ expr ->
getSrcFileUgn `thenUgn` \ sf ->
let
binds = cvBinds sf cvValSig binding
in
returnUgn (HsLet binds expr)
U_doe gdo srcline -> -- do expression
mkSrcLocUgn srcline $ \ src_loc ->
wlkList rd_stmt gdo `thenUgn` \ stmts ->
returnUgn (HsDo stmts src_loc)
where
rd_stmt pt
= rdU_tree pt `thenUgn` \ bind ->
case bind of
U_doexp exp srcline ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkExpr exp `thenUgn` \ expr ->
returnUgn (ExprStmt expr src_loc)
U_dobind pat exp srcline ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkPat pat `thenUgn` \ patt ->
wlkExpr exp `thenUgn` \ expr ->
returnUgn (BindStmt patt expr src_loc)
U_seqlet seqlet ->
wlkBinding seqlet `thenUgn` \ bs ->
getSrcFileUgn `thenUgn` \ sf ->
let
binds = cvBinds sf cvValSig bs
in
returnUgn (LetStmt binds)
U_comprh cexp cquals -> -- list comprehension
wlkExpr cexp `thenUgn` \ expr ->
wlkList rd_qual cquals `thenUgn` \ quals ->
returnUgn (ListComp expr quals)
where
rd_qual pt
= rdU_tree pt `thenUgn` \ qual ->
wlk_qual qual
wlk_qual qual
= case qual of
U_guard exp ->
wlkExpr exp `thenUgn` \ expr ->
returnUgn (FilterQual expr)
U_qual qpat qexp ->
wlkPat qpat `thenUgn` \ pat ->
wlkExpr qexp `thenUgn` \ expr ->
returnUgn (GeneratorQual pat expr)
U_seqlet seqlet ->
wlkBinding seqlet `thenUgn` \ bs ->
getSrcFileUgn `thenUgn` \ sf ->
let
binds = cvBinds sf cvValSig bs
in
returnUgn (LetQual binds)
U_eenum efrom estep eto -> -- arithmetic sequence
wlkExpr efrom `thenUgn` \ e1 ->
wlkMaybe rdExpr estep `thenUgn` \ es2 ->
wlkMaybe rdExpr eto `thenUgn` \ es3 ->
returnUgn (cv_arith_seq e1 es2 es3)
where
cv_arith_seq e1 Nothing Nothing = ArithSeqIn (From e1)
cv_arith_seq e1 Nothing (Just e3) = ArithSeqIn (FromTo e1 e3)
cv_arith_seq e1 (Just e2) Nothing = ArithSeqIn (FromThen e1 e2)
cv_arith_seq e1 (Just e2) (Just e3) = ArithSeqIn (FromThenTo e1 e2 e3)
U_restr restre restrt -> -- expression with type signature
wlkExpr restre `thenUgn` \ expr ->
wlkHsType restrt `thenUgn` \ ty ->
returnUgn (ExprWithTySig expr ty)
--------------------------------------------------------------
-- now the prefix items that can either be an expression or
-- pattern, except we know they are *expressions* here
-- (this code could be commoned up with the pattern version;
-- but it probably isn't worth it)
--------------------------------------------------------------
U_lit lit ->
wlkLiteral lit `thenUgn` \ lit ->
returnUgn (HsLit lit)
U_ident n -> -- simple identifier
wlkVarId n `thenUgn` \ var ->
returnUgn (HsVar var)
U_ap fun arg -> -- application
wlkExpr fun `thenUgn` \ expr1 ->
wlkExpr arg `thenUgn` \ expr2 ->
returnUgn (HsApp expr1 expr2)
U_infixap fun arg1 arg2 -> -- infix application
wlkVarId fun `thenUgn` \ op ->
wlkExpr arg1 `thenUgn` \ expr1 ->
wlkExpr arg2 `thenUgn` \ expr2 ->
returnUgn (mkOpApp expr1 op expr2)
U_negate nexp -> -- prefix negation
wlkExpr nexp `thenUgn` \ expr ->
returnUgn (NegApp expr (HsVar dummyRdrVarName))
U_llist llist -> -- explicit list
wlkList rdExpr llist `thenUgn` \ exprs ->
returnUgn (ExplicitList exprs)
U_tuple tuplelist -> -- explicit tuple
wlkList rdExpr tuplelist `thenUgn` \ exprs ->
returnUgn (ExplicitTuple exprs)
U_record con rbinds -> -- record construction
wlkDataId con `thenUgn` \ rcon ->
wlkList rdRbind rbinds `thenUgn` \ recbinds ->
returnUgn (RecordCon (HsVar rcon) recbinds)
U_rupdate updexp updbinds -> -- record update
wlkExpr updexp `thenUgn` \ aexp ->
wlkList rdRbind updbinds `thenUgn` \ recbinds ->
returnUgn (RecordUpd aexp recbinds)
#ifdef DEBUG
U_hmodule _ _ _ _ _ _ _ -> error "U_hmodule"
U_as _ _ -> error "U_as"
U_lazyp _ -> error "U_lazyp"
U_wildp -> error "U_wildp"
U_qual _ _ -> error "U_qual"
U_guard _ -> error "U_guard"
U_seqlet _ -> error "U_seqlet"
U_dobind _ _ _ -> error "U_dobind"
U_doexp _ _ -> error "U_doexp"
U_rbind _ _ -> error "U_rbind"
U_fixop _ _ _ -> error "U_fixop"
#endif
rdRbind pt
= rdU_tree pt `thenUgn` \ (U_rbind var exp) ->
wlkVarId var `thenUgn` \ rvar ->
wlkMaybe rdExpr exp `thenUgn` \ expr_maybe ->
returnUgn (
case expr_maybe of
Nothing -> (rvar, HsVar rvar, True{-pun-})
Just re -> (rvar, re, False)
)
\end{code}
Patterns: just bear in mind that lists of patterns are represented as
a series of ``applications''.
\begin{code}
wlkPat pat
= case pat of
U_par ppat -> -- parenthesised pattern
wlkPat ppat `thenUgn` \ pat ->
-- tidy things up a little:
returnUgn (
case pat of
VarPatIn _ -> pat
WildPatIn -> pat
other -> ParPatIn pat
)
U_as avar as_pat -> -- "as" pattern
wlkVarId avar `thenUgn` \ var ->
wlkPat as_pat `thenUgn` \ pat ->
returnUgn (AsPatIn var pat)
U_lazyp lazyp -> -- irrefutable ("twiddle") pattern
wlkPat lazyp `thenUgn` \ pat ->
returnUgn (LazyPatIn pat)
U_wildp -> returnUgn WildPatIn -- wildcard pattern
U_lit lit -> -- literal pattern
wlkLiteral lit `thenUgn` \ lit ->
returnUgn (LitPatIn lit)
U_ident nn -> -- simple identifier
wlkVarId nn `thenUgn` \ n ->
returnUgn (
case rdrNameOcc n of
VarOcc occ | isLexConId occ -> ConPatIn n []
other -> VarPatIn n
)
U_ap l r -> -- "application": there's a list of patterns lurking here!
wlkPat r `thenUgn` \ rpat ->
collect_pats l [rpat] `thenUgn` \ (lpat,lpats) ->
(case lpat of
VarPatIn x -> returnUgn (x, lpats)
ConPatIn x [] -> returnUgn (x, lpats)
ConOpPatIn x op _ y -> returnUgn (op, x:y:lpats)
_ -> getSrcLocUgn `thenUgn` \ loc ->
let
err = addErrLoc loc "Illegal pattern `application'"
(\sty -> ppInterleave ppSP (map (ppr sty) (lpat:lpats)))
msg = ppShow 100 (err PprForUser)
in
#if __GLASGOW_HASKELL__ >= 200
ioToUgnM (GHCbase.ioToPrimIO (hPutStr stderr msg)) `thenUgn` \ _ ->
ioToUgnM (GHCbase.ioToPrimIO (ghcExit 1)) `thenUgn` \ _ ->
#else
ioToUgnM (hPutStr stderr msg) `thenUgn` \ _ ->
ioToUgnM (ghcExit 1) `thenUgn` \ _ ->
#endif
returnUgn (error "ReadPrefix")
) `thenUgn` \ (n, arg_pats) ->
returnUgn (ConPatIn n arg_pats)
where
collect_pats pat acc
= case pat of
U_ap l r ->
wlkPat r `thenUgn` \ rpat ->
collect_pats l (rpat:acc)
other ->
wlkPat other `thenUgn` \ pat ->
returnUgn (pat,acc)
U_infixap fun arg1 arg2 -> -- infix pattern
wlkVarId fun `thenUgn` \ op ->
wlkPat arg1 `thenUgn` \ pat1 ->
wlkPat arg2 `thenUgn` \ pat2 ->
returnUgn (ConOpPatIn pat1 op (error "ConOpPatIn:fixity") pat2)
U_negate npat -> -- negated pattern
wlkPat npat `thenUgn` \ pat ->
returnUgn (NegPatIn pat)
U_llist llist -> -- explicit list
wlkList rdPat llist `thenUgn` \ pats ->
returnUgn (ListPatIn pats)
U_tuple tuplelist -> -- explicit tuple
wlkList rdPat tuplelist `thenUgn` \ pats ->
returnUgn (TuplePatIn pats)
U_record con rpats -> -- record destruction
wlkDataId con `thenUgn` \ rcon ->
wlkList rdRpat rpats `thenUgn` \ recpats ->
returnUgn (RecPatIn rcon recpats)
where
rdRpat pt
= rdU_tree pt `thenUgn` \ (U_rbind var pat) ->
wlkVarId var `thenUgn` \ rvar ->
wlkMaybe rdPat pat `thenUgn` \ pat_maybe ->
returnUgn (
case pat_maybe of
Nothing -> (rvar, VarPatIn rvar, True{-pun-})
Just rp -> (rvar, rp, False)
)
\end{code}
\begin{code}
wlkLiteral :: U_literal -> UgnM HsLit
wlkLiteral ulit
= returnUgn (
case ulit of
U_integer s -> HsInt (as_integer s)
U_floatr s -> HsFrac (as_rational s)
U_intprim s -> HsIntPrim (as_integer s)
U_doubleprim s -> HsDoublePrim (as_rational s)
U_floatprim s -> HsFloatPrim (as_rational s)
U_charr s -> HsChar (as_char s)
U_charprim s -> HsCharPrim (as_char s)
U_string s -> HsString (as_string s)
U_stringprim s -> HsStringPrim (as_string s)
U_clitlit s -> HsLitLit (as_string s)
)
where
as_char s = _HEAD_ s
as_integer s = readInteger (_UNPK_ s)
#if __GLASGOW_HASKELL__ >= 200
as_rational s = GHCbase.readRational__ (_UNPK_ s) -- non-std
#else
as_rational s = _readRational (_UNPK_ s) -- non-std
#endif
as_string s = s
\end{code}
%************************************************************************
%* *
\subsection{wlkBinding}
%* *
%************************************************************************
\begin{code}
wlkBinding :: U_binding -> UgnM RdrBinding
wlkBinding binding
= case binding of
-- null binding
U_nullbind ->
returnUgn RdrNullBind
-- "and" binding (just glue, really)
U_abind a b ->
wlkBinding a `thenUgn` \ binding1 ->
wlkBinding b `thenUgn` \ binding2 ->
returnUgn (RdrAndBindings binding1 binding2)
-- "data" declaration
U_tbind tctxt ttype tcons tderivs srcline ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkContext tctxt `thenUgn` \ ctxt ->
wlkTyConAndTyVars ttype `thenUgn` \ (tycon, tyvars) ->
wlkList rdConDecl tcons `thenUgn` \ cons ->
wlkDerivings tderivs `thenUgn` \ derivings ->
returnUgn (RdrTyDecl (TyData ctxt tycon tyvars cons derivings noDataPragmas src_loc))
-- "newtype" declaration
U_ntbind ntctxt nttype ntcon ntderivs srcline ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkContext ntctxt `thenUgn` \ ctxt ->
wlkTyConAndTyVars nttype `thenUgn` \ (tycon, tyvars) ->
wlkList rdConDecl ntcon `thenUgn` \ [con] ->
wlkDerivings ntderivs `thenUgn` \ derivings ->
returnUgn (RdrTyDecl (TyNew ctxt tycon tyvars con derivings noDataPragmas src_loc))
-- "type" declaration
U_nbind nbindid nbindas srcline ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkTyConAndTyVars nbindid `thenUgn` \ (tycon, tyvars) ->
wlkMonoType nbindas `thenUgn` \ expansion ->
returnUgn (RdrTyDecl (TySynonym tycon tyvars expansion src_loc))
-- function binding
U_fbind fbindl srcline ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkList rdMatch fbindl `thenUgn` \ matches ->
returnUgn (RdrFunctionBinding srcline matches)
-- pattern binding
U_pbind pbindl srcline ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkList rdMatch pbindl `thenUgn` \ matches ->
returnUgn (RdrPatternBinding srcline matches)
-- "class" declaration
U_cbind cbindc cbindid cbindw srcline ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkContext cbindc `thenUgn` \ ctxt ->
wlkClassAssertTy cbindid `thenUgn` \ (clas, tyvar)->
wlkBinding cbindw `thenUgn` \ binding ->
getSrcFileUgn `thenUgn` \ sf ->
let
(final_methods, final_sigs) = cvMonoBindsAndSigs sf cvClassOpSig binding
in
returnUgn (RdrClassDecl
(ClassDecl ctxt clas tyvar final_sigs final_methods noClassPragmas src_loc))
-- "instance" declaration
U_ibind ibindc iclas ibindi ibindw srcline ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkContext ibindc `thenUgn` \ ctxt ->
wlkTCId iclas `thenUgn` \ clas ->
wlkMonoType ibindi `thenUgn` \ at_ty ->
wlkBinding ibindw `thenUgn` \ binding ->
getSrcModUgn `thenUgn` \ modname ->
getSrcFileUgn `thenUgn` \ sf ->
let
(binds,uprags) = cvMonoBindsAndSigs sf cvInstDeclSig binding
inst_ty = HsPreForAllTy ctxt (MonoDictTy clas at_ty)
in
returnUgn (RdrInstDecl
(InstDecl inst_ty binds uprags Nothing {- No dfun id -} src_loc))
-- "default" declaration
U_dbind dbindts srcline ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkList rdMonoType dbindts `thenUgn` \ tys ->
returnUgn (RdrDefaultDecl (DefaultDecl tys src_loc))
a_sig_we_hope ->
-- signature(-like) things, including user pragmas
wlk_sig_thing a_sig_we_hope
\end{code}
\begin{code}
wlkDerivings :: U_maybe -> UgnM (Maybe [RdrName])
wlkDerivings (U_nothing) = returnUgn Nothing
wlkDerivings (U_just pt)
= rdU_list pt `thenUgn` \ ds ->
wlkList rdTCId ds `thenUgn` \ derivs ->
returnUgn (Just derivs)
\end{code}
\begin{code}
-- type signature
wlk_sig_thing (U_sbind sbindids sbindid srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkList rdVarId sbindids `thenUgn` \ vars ->
wlkHsType sbindid `thenUgn` \ poly_ty ->
returnUgn (RdrTySig vars poly_ty src_loc)
-- value specialisation user-pragma
wlk_sig_thing (U_vspec_uprag uvar vspec_tys srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkVarId uvar `thenUgn` \ var ->
wlkList rd_ty_and_id vspec_tys `thenUgn` \ tys_and_ids ->
returnUgn (RdrSpecValSig [SpecSig var ty using_id src_loc
| (ty, using_id) <- tys_and_ids ])
where
rd_ty_and_id :: ParseTree -> UgnM (RdrNameHsType, Maybe RdrName)
rd_ty_and_id pt
= rdU_binding pt `thenUgn` \ (U_vspec_ty_and_id vspec_ty vspec_id) ->
wlkHsType vspec_ty `thenUgn` \ ty ->
wlkMaybe rdVarId vspec_id `thenUgn` \ id_maybe ->
returnUgn(ty, id_maybe)
-- instance specialisation user-pragma
wlk_sig_thing (U_ispec_uprag iclas ispec_ty srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkTCId iclas `thenUgn` \ clas ->
wlkMonoType ispec_ty `thenUgn` \ ty ->
returnUgn (RdrSpecInstSig (SpecInstSig clas ty src_loc))
-- data specialisation user-pragma
wlk_sig_thing (U_dspec_uprag itycon dspec_tys srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkTCId itycon `thenUgn` \ tycon ->
wlkList rdMonoType dspec_tys `thenUgn` \ tys ->
returnUgn (RdrSpecDataSig (SpecDataSig tycon (foldl MonoTyApp (MonoTyVar tycon) tys) src_loc))
-- value inlining user-pragma
wlk_sig_thing (U_inline_uprag ivar srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkVarId ivar `thenUgn` \ var ->
returnUgn (RdrInlineValSig (InlineSig var src_loc))
-- "deforest me" user-pragma
wlk_sig_thing (U_deforest_uprag ivar srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkVarId ivar `thenUgn` \ var ->
returnUgn (RdrDeforestSig (DeforestSig var src_loc))
-- "magic" unfolding user-pragma
wlk_sig_thing (U_magicuf_uprag ivar str srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkVarId ivar `thenUgn` \ var ->
returnUgn (RdrMagicUnfoldingSig (MagicUnfoldingSig var str src_loc))
\end{code}
%************************************************************************
%* *
\subsection[wlkTypes]{Reading in types in various forms (and data constructors)}
%* *
%************************************************************************
\begin{code}
rdHsType :: ParseTree -> UgnM RdrNameHsType
rdMonoType :: ParseTree -> UgnM RdrNameHsType
rdHsType pt = rdU_ttype pt `thenUgn` \ ttype -> wlkHsType ttype
rdMonoType pt = rdU_ttype pt `thenUgn` \ ttype -> wlkMonoType ttype
wlkHsType :: U_ttype -> UgnM RdrNameHsType
wlkMonoType :: U_ttype -> UgnM RdrNameHsType
wlkHsType ttype
= case ttype of
U_context tcontextl tcontextt -> -- context
wlkContext tcontextl `thenUgn` \ ctxt ->
wlkMonoType tcontextt `thenUgn` \ ty ->
returnUgn (HsPreForAllTy ctxt ty)
other -> -- something else
wlkMonoType other `thenUgn` \ ty ->
returnUgn (HsPreForAllTy [{-no context-}] ty)
wlkMonoType ttype
= case ttype of
U_namedtvar tv -> -- type variable
wlkTvId tv `thenUgn` \ tyvar ->
returnUgn (MonoTyVar tyvar)
U_tname tcon -> -- type constructor
wlkTCId tcon `thenUgn` \ tycon ->
returnUgn (MonoTyVar tycon)
U_tapp t1 t2 ->
wlkMonoType t1 `thenUgn` \ ty1 ->
wlkMonoType t2 `thenUgn` \ ty2 ->
returnUgn (MonoTyApp ty1 ty2)
U_tllist tlist -> -- list type
wlkMonoType tlist `thenUgn` \ ty ->
returnUgn (MonoListTy dummyRdrTcName ty)
U_ttuple ttuple ->
wlkList rdMonoType ttuple `thenUgn` \ tys ->
returnUgn (MonoTupleTy dummyRdrTcName tys)
U_tfun tfun targ ->
wlkMonoType tfun `thenUgn` \ ty1 ->
wlkMonoType targ `thenUgn` \ ty2 ->
returnUgn (MonoFunTy ty1 ty2)
\end{code}
\begin{code}
wlkTyConAndTyVars :: U_ttype -> UgnM (RdrName, [HsTyVar RdrName])
wlkContext :: U_list -> UgnM RdrNameContext
wlkClassAssertTy :: U_ttype -> UgnM (RdrName, HsTyVar RdrName)
wlkTyConAndTyVars ttype
= wlkMonoType ttype `thenUgn` \ ty ->
let
split (MonoTyApp fun (MonoTyVar arg)) args = split fun (UserTyVar arg : args)
split (MonoTyVar tycon) args = (tycon,args)
in
returnUgn (split ty [])
wlkContext list
= wlkList rdMonoType list `thenUgn` \ tys ->
returnUgn (map mk_class_assertion tys)
wlkClassAssertTy xs
= wlkMonoType xs `thenUgn` \ mono_ty ->
returnUgn (case mk_class_assertion mono_ty of
(clas, MonoTyVar tyvar) -> (clas, UserTyVar tyvar)
)
mk_class_assertion :: RdrNameHsType -> (RdrName, RdrNameHsType)
mk_class_assertion (MonoTyApp (MonoTyVar name) ty@(MonoTyVar tyname)) = (name, ty)
mk_class_assertion other
= pprError "ERROR: malformed type context: " (ppr PprForUser other)
-- regrettably, the parser does let some junk past
-- e.g., f :: Num {-nothing-} => a -> ...
\end{code}
\begin{code}
rdConDecl :: ParseTree -> UgnM RdrNameConDecl
rdConDecl pt
= rdU_constr pt `thenUgn` \ blah ->
wlkConDecl blah
wlkConDecl :: U_constr -> UgnM RdrNameConDecl
wlkConDecl (U_constrpre ccon ctys srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkDataId ccon `thenUgn` \ con ->
wlkList rdBangType ctys `thenUgn` \ tys ->
returnUgn (ConDecl con tys src_loc)
wlkConDecl (U_constrinf cty1 cop cty2 srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkBangType cty1 `thenUgn` \ ty1 ->
wlkDataId cop `thenUgn` \ op ->
wlkBangType cty2 `thenUgn` \ ty2 ->
returnUgn (ConOpDecl ty1 op ty2 src_loc)
wlkConDecl (U_constrnew ccon cty srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkDataId ccon `thenUgn` \ con ->
wlkMonoType cty `thenUgn` \ ty ->
returnUgn (NewConDecl con ty src_loc)
wlkConDecl (U_constrrec ccon cfields srcline)
= mkSrcLocUgn srcline $ \ src_loc ->
wlkDataId ccon `thenUgn` \ con ->
wlkList rd_field cfields `thenUgn` \ fields_lists ->
returnUgn (RecConDecl con fields_lists src_loc)
where
rd_field :: ParseTree -> UgnM ([RdrName], BangType RdrName)
rd_field pt
= rdU_constr pt `thenUgn` \ (U_field fvars fty) ->
wlkList rdVarId fvars `thenUgn` \ vars ->
wlkBangType fty `thenUgn` \ ty ->
returnUgn (vars, ty)
-----------------
rdBangType pt = rdU_ttype pt `thenUgn` \ ty -> wlkBangType ty
wlkBangType :: U_ttype -> UgnM (BangType RdrName)
wlkBangType (U_tbang bty) = wlkMonoType bty `thenUgn` \ ty ->
returnUgn (Banged ty)
wlkBangType uty = wlkMonoType uty `thenUgn` \ ty ->
returnUgn (Unbanged ty)
\end{code}
%************************************************************************
%* *
\subsection{Read a ``match''}
%* *
%************************************************************************
\begin{code}
rdMatch :: ParseTree -> UgnM RdrMatch
rdMatch pt
= rdU_pbinding pt `thenUgn` \ (U_pgrhs gpat gdexprs gbind gsrcfun srcline) ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkPat gpat `thenUgn` \ pat ->
wlkBinding gbind `thenUgn` \ binding ->
wlkVarId gsrcfun `thenUgn` \ srcfun ->
let
wlk_guards (U_pnoguards exp)
= wlkExpr exp `thenUgn` \ expr ->
returnUgn (RdrMatch_NoGuard srcline srcfun pat expr binding)
wlk_guards (U_pguards gs)
= wlkList rd_gd_expr gs `thenUgn` \ gd_exps ->
returnUgn (RdrMatch_Guards srcline srcfun pat gd_exps binding)
in
wlk_guards gdexprs
where
rd_gd_expr pt
= rdU_pbinding pt `thenUgn` \ (U_pgdexp g e) ->
wlkExpr g `thenUgn` \ guard ->
wlkExpr e `thenUgn` \ expr ->
returnUgn (guard, expr)
\end{code}
%************************************************************************
%* *
\subsection[rdFixOp]{Read in a fixity declaration}
%* *
%************************************************************************
\begin{code}
rdFixOp :: ParseTree -> UgnM RdrNameFixityDecl
rdFixOp pt
= rdU_tree pt `thenUgn` \ fix ->
case fix of
U_fixop op dir_n prec -> wlkVarId op `thenUgn` \ op ->
returnUgn (FixityDecl op (Fixity prec dir) noSrcLoc)
-- ToDo: add SrcLoc!
where
dir = case dir_n of
(-1) -> InfixL
0 -> InfixN
1 -> InfixR
_ -> error "ReadPrefix:rdFixOp"
\end{code}
%************************************************************************
%* *
\subsection[rdImport]{Read an import decl}
%* *
%************************************************************************
\begin{code}
rdImport :: ParseTree
-> UgnM RdrNameImportDecl
rdImport pt
= rdU_binding pt `thenUgn` \ (U_import imod iqual ias ispec srcline) ->
mkSrcLocUgn srcline $ \ src_loc ->
wlkMaybe rdU_stringId ias `thenUgn` \ maybe_as ->
wlkMaybe rd_spec ispec `thenUgn` \ maybe_spec ->
returnUgn (ImportDecl imod (cvFlag iqual) maybe_as maybe_spec src_loc)
where
rd_spec pt = rdU_either pt `thenUgn` \ spec ->
case spec of
U_left pt -> rdEntities pt `thenUgn` \ ents ->
returnUgn (False, ents)
U_right pt -> rdEntities pt `thenUgn` \ ents ->
returnUgn (True, ents)
\end{code}
\begin{code}
rdEntities pt
= rdU_list pt `thenUgn` \ list ->
wlkList rdEntity list
rdEntity :: ParseTree -> UgnM (IE RdrName)
rdEntity pt
= rdU_entidt pt `thenUgn` \ entity ->
case entity of
U_entid evar -> -- just a value
wlkEntId evar `thenUgn` \ var ->
returnUgn (IEVar var)
U_enttype x -> -- abstract type constructor/class
wlkTCId x `thenUgn` \ thing ->
returnUgn (IEThingAbs thing)
U_enttypeall x -> -- non-abstract type constructor/class
wlkTCId x `thenUgn` \ thing ->
returnUgn (IEThingAll thing)
U_enttypenamed x ns -> -- non-abstract type constructor/class
-- with specified constrs/methods
wlkTCId x `thenUgn` \ thing ->
wlkList rdVarId ns `thenUgn` \ names ->
returnUgn (IEThingWith thing names)
U_entmod mod -> -- everything provided unqualified by a module
returnUgn (IEModuleContents mod)
\end{code}
|