2 % (c) The AQUA Project, Glasgow University, 1994-1996
4 \section{Read parse tree built by Yacc parser}
7 #include "HsVersions.h"
9 module ReadPrefix ( rdModule ) where
12 IMPORT_1_3(IO(hPutStr, stderr))
13 IMPORT_1_3(GHCio(stThen))
15 import UgenAll -- all Yacc parser gumpff...
16 import PrefixSyn -- and various syntaxen.
18 import HsTypes ( HsTyVar(..) )
19 import HsPragmas ( noDataPragmas, noClassPragmas, noInstancePragmas, noGenPragmas )
23 import ErrUtils ( addErrLoc, ghcExit )
24 import FiniteMap ( elemFM, FiniteMap )
25 import Name ( RdrName(..), OccName(..) )
26 import Lex ( isLexConId )
27 import PprStyle ( PprStyle(..) )
30 import SrcLoc ( mkGeneratedSrcLoc, noSrcLoc, SrcLoc )
31 import Util ( nOfThem, pprError, panic )
34 %************************************************************************
36 \subsection[ReadPrefix-help]{Help Functions}
38 %************************************************************************
41 wlkList :: (U_VOID_STAR -> UgnM a) -> U_list -> UgnM [a]
43 wlkList wlk_it U_lnil = returnUgn []
45 wlkList wlk_it (U_lcons hd tl)
46 = wlk_it hd `thenUgn` \ hd_it ->
47 wlkList wlk_it tl `thenUgn` \ tl_it ->
48 returnUgn (hd_it : tl_it)
52 wlkMaybe :: (U_VOID_STAR -> UgnM a) -> U_maybe -> UgnM (Maybe a)
54 wlkMaybe wlk_it U_nothing = returnUgn Nothing
55 wlkMaybe wlk_it (U_just x)
56 = wlk_it x `thenUgn` \ it ->
61 wlkTvId = wlkQid TvOcc
62 wlkTCId = wlkQid TCOcc
63 wlkVarId = wlkQid VarOcc
64 wlkDataId = wlkQid VarOcc
65 wlkEntId = wlkQid (\occ -> if isLexConId occ
69 wlkQid :: (FAST_STRING -> OccName) -> U_qid -> UgnM RdrName
70 wlkQid mk_occ_name (U_noqual name)
71 = returnUgn (Unqual (mk_occ_name name))
72 wlkQid mk_occ_name (U_aqual mod name)
73 = returnUgn (Qual mod (mk_occ_name name))
75 -- I don't understand this one! It is what shows up when we meet (), [], or (,,,).
76 wlkQid mk_occ_name (U_gid n name)
77 = returnUgn (Unqual (mk_occ_name name))
79 rdTCId pt = rdU_qid pt `thenUgn` \ qid -> wlkTCId qid
80 rdVarId pt = rdU_qid pt `thenUgn` \ qid -> wlkVarId qid
82 cvFlag :: U_long -> Bool
87 %************************************************************************
89 \subsection[rdModule]{@rdModule@: reads in a Haskell module}
91 %************************************************************************
94 #if __GLASGOW_HASKELL__ >= 200
95 # define PACK_STR packCString
96 # define CCALL_THEN `stThen`
98 # define PACK_STR _packCString
99 # define CCALL_THEN `thenPrimIO`
102 rdModule :: IO (Module, -- this module's name
103 RdrNameHsModule) -- the main goods
106 = _ccall_ hspmain CCALL_THEN \ pt -> -- call the Yacc parser!
108 srcfile = PACK_STR ``input_filename'' -- What A Great Hack! (TM)
111 rdU_tree pt `thenUgn` \ (U_hmodule modname himplist hexplist hfixlist
112 hmodlist srciface_version srcline) ->
114 setSrcFileUgn srcfile $
115 setSrcModUgn modname $
116 mkSrcLocUgn srcline $ \ src_loc ->
118 wlkMaybe rdEntities hexplist `thenUgn` \ exports ->
119 wlkList rdImport himplist `thenUgn` \ imports ->
120 wlkList rdFixOp hfixlist `thenUgn` \ fixities ->
121 wlkBinding hmodlist `thenUgn` \ binding ->
124 val_decl = ValD (cvBinds srcfile cvValSig binding)
125 other_decls = cvOtherDecls binding
129 (case srciface_version of { 0 -> Nothing; n -> Just n })
133 (val_decl: other_decls)
138 %************************************************************************
140 \subsection[wlkExprOrPat]{@wlkExpr@ and @wlkPat@}
142 %************************************************************************
145 rdExpr :: ParseTree -> UgnM RdrNameHsExpr
146 rdPat :: ParseTree -> UgnM RdrNamePat
148 rdExpr pt = rdU_tree pt `thenUgn` \ tree -> wlkExpr tree
149 rdPat pt = rdU_tree pt `thenUgn` \ tree -> wlkPat tree
151 wlkExpr :: U_tree -> UgnM RdrNameHsExpr
152 wlkPat :: U_tree -> UgnM RdrNamePat
156 U_par pexpr -> -- parenthesised expr
157 wlkExpr pexpr `thenUgn` \ expr ->
158 returnUgn (HsPar expr)
160 U_lsection lsexp lop -> -- left section
161 wlkExpr lsexp `thenUgn` \ expr ->
162 wlkVarId lop `thenUgn` \ op ->
163 returnUgn (SectionL expr (HsVar op))
165 U_rsection rop rsexp -> -- right section
166 wlkVarId rop `thenUgn` \ op ->
167 wlkExpr rsexp `thenUgn` \ expr ->
168 returnUgn (SectionR (HsVar op) expr)
170 U_ccall fun flavor ccargs -> -- ccall/casm
171 wlkList rdExpr ccargs `thenUgn` \ args ->
175 returnUgn (CCall fun args
176 (tag == 'p' || tag == 'P') -- may invoke GC
177 (tag == 'N' || tag == 'P') -- really a "casm"
178 (panic "CCall:result_ty"))
180 U_scc label sccexp -> -- scc (set-cost-centre) expression
181 wlkExpr sccexp `thenUgn` \ expr ->
182 returnUgn (HsSCC label expr)
184 U_lambda lampats lamexpr srcline -> -- lambda expression
185 mkSrcLocUgn srcline $ \ src_loc ->
186 wlkList rdPat lampats `thenUgn` \ pats ->
187 wlkExpr lamexpr `thenUgn` \ body ->
189 HsLam (foldr PatMatch
190 (GRHSMatch (GRHSsAndBindsIn
191 [OtherwiseGRHS body src_loc]
196 U_casee caseexpr casebody srcline -> -- case expression
197 mkSrcLocUgn srcline $ \ src_loc ->
198 wlkExpr caseexpr `thenUgn` \ expr ->
199 wlkList rdMatch casebody `thenUgn` \ mats ->
200 getSrcFileUgn `thenUgn` \ sf ->
202 matches = cvMatches sf True mats
204 returnUgn (HsCase expr matches src_loc)
206 U_ife ifpred ifthen ifelse srcline -> -- if expression
207 mkSrcLocUgn srcline $ \ src_loc ->
208 wlkExpr ifpred `thenUgn` \ e1 ->
209 wlkExpr ifthen `thenUgn` \ e2 ->
210 wlkExpr ifelse `thenUgn` \ e3 ->
211 returnUgn (HsIf e1 e2 e3 src_loc)
213 U_let letvdefs letvexpr -> -- let expression
214 wlkBinding letvdefs `thenUgn` \ binding ->
215 wlkExpr letvexpr `thenUgn` \ expr ->
216 getSrcFileUgn `thenUgn` \ sf ->
218 binds = cvBinds sf cvValSig binding
220 returnUgn (HsLet binds expr)
222 U_doe gdo srcline -> -- do expression
223 mkSrcLocUgn srcline $ \ src_loc ->
224 wlkList rd_stmt gdo `thenUgn` \ stmts ->
225 returnUgn (HsDo stmts src_loc)
228 = rdU_tree pt `thenUgn` \ bind ->
230 U_doexp exp srcline ->
231 mkSrcLocUgn srcline $ \ src_loc ->
232 wlkExpr exp `thenUgn` \ expr ->
233 returnUgn (ExprStmt expr src_loc)
235 U_dobind pat exp srcline ->
236 mkSrcLocUgn srcline $ \ src_loc ->
237 wlkPat pat `thenUgn` \ patt ->
238 wlkExpr exp `thenUgn` \ expr ->
239 returnUgn (BindStmt patt expr src_loc)
242 wlkBinding seqlet `thenUgn` \ bs ->
243 getSrcFileUgn `thenUgn` \ sf ->
245 binds = cvBinds sf cvValSig bs
247 returnUgn (LetStmt binds)
249 U_comprh cexp cquals -> -- list comprehension
250 wlkExpr cexp `thenUgn` \ expr ->
251 wlkList rd_qual cquals `thenUgn` \ quals ->
252 returnUgn (ListComp expr quals)
255 = rdU_tree pt `thenUgn` \ qual ->
261 wlkExpr exp `thenUgn` \ expr ->
262 returnUgn (FilterQual expr)
265 wlkPat qpat `thenUgn` \ pat ->
266 wlkExpr qexp `thenUgn` \ expr ->
267 returnUgn (GeneratorQual pat expr)
270 wlkBinding seqlet `thenUgn` \ bs ->
271 getSrcFileUgn `thenUgn` \ sf ->
273 binds = cvBinds sf cvValSig bs
275 returnUgn (LetQual binds)
277 U_eenum efrom estep eto -> -- arithmetic sequence
278 wlkExpr efrom `thenUgn` \ e1 ->
279 wlkMaybe rdExpr estep `thenUgn` \ es2 ->
280 wlkMaybe rdExpr eto `thenUgn` \ es3 ->
281 returnUgn (cv_arith_seq e1 es2 es3)
283 cv_arith_seq e1 Nothing Nothing = ArithSeqIn (From e1)
284 cv_arith_seq e1 Nothing (Just e3) = ArithSeqIn (FromTo e1 e3)
285 cv_arith_seq e1 (Just e2) Nothing = ArithSeqIn (FromThen e1 e2)
286 cv_arith_seq e1 (Just e2) (Just e3) = ArithSeqIn (FromThenTo e1 e2 e3)
288 U_restr restre restrt -> -- expression with type signature
289 wlkExpr restre `thenUgn` \ expr ->
290 wlkHsType restrt `thenUgn` \ ty ->
291 returnUgn (ExprWithTySig expr ty)
293 --------------------------------------------------------------
294 -- now the prefix items that can either be an expression or
295 -- pattern, except we know they are *expressions* here
296 -- (this code could be commoned up with the pattern version;
297 -- but it probably isn't worth it)
298 --------------------------------------------------------------
300 wlkLiteral lit `thenUgn` \ lit ->
301 returnUgn (HsLit lit)
303 U_ident n -> -- simple identifier
304 wlkVarId n `thenUgn` \ var ->
305 returnUgn (HsVar var)
307 U_ap fun arg -> -- application
308 wlkExpr fun `thenUgn` \ expr1 ->
309 wlkExpr arg `thenUgn` \ expr2 ->
310 returnUgn (HsApp expr1 expr2)
312 U_infixap fun arg1 arg2 -> -- infix application
313 wlkVarId fun `thenUgn` \ op ->
314 wlkExpr arg1 `thenUgn` \ expr1 ->
315 wlkExpr arg2 `thenUgn` \ expr2 ->
316 returnUgn (mkOpApp expr1 op expr2)
318 U_negate nexp -> -- prefix negation
319 wlkExpr nexp `thenUgn` \ expr ->
320 returnUgn (NegApp expr (HsVar dummyRdrVarName))
322 U_llist llist -> -- explicit list
323 wlkList rdExpr llist `thenUgn` \ exprs ->
324 returnUgn (ExplicitList exprs)
326 U_tuple tuplelist -> -- explicit tuple
327 wlkList rdExpr tuplelist `thenUgn` \ exprs ->
328 returnUgn (ExplicitTuple exprs)
330 U_record con rbinds -> -- record construction
331 wlkDataId con `thenUgn` \ rcon ->
332 wlkList rdRbind rbinds `thenUgn` \ recbinds ->
333 returnUgn (RecordCon (HsVar rcon) recbinds)
335 U_rupdate updexp updbinds -> -- record update
336 wlkExpr updexp `thenUgn` \ aexp ->
337 wlkList rdRbind updbinds `thenUgn` \ recbinds ->
338 returnUgn (RecordUpd aexp recbinds)
341 U_hmodule _ _ _ _ _ _ _ -> error "U_hmodule"
342 U_as _ _ -> error "U_as"
343 U_lazyp _ -> error "U_lazyp"
344 U_wildp -> error "U_wildp"
345 U_qual _ _ -> error "U_qual"
346 U_guard _ -> error "U_guard"
347 U_seqlet _ -> error "U_seqlet"
348 U_dobind _ _ _ -> error "U_dobind"
349 U_doexp _ _ -> error "U_doexp"
350 U_rbind _ _ -> error "U_rbind"
351 U_fixop _ _ _ -> error "U_fixop"
355 = rdU_tree pt `thenUgn` \ (U_rbind var exp) ->
356 wlkVarId var `thenUgn` \ rvar ->
357 wlkMaybe rdExpr exp `thenUgn` \ expr_maybe ->
360 Nothing -> (rvar, HsVar rvar, True{-pun-})
361 Just re -> (rvar, re, False)
365 Patterns: just bear in mind that lists of patterns are represented as
366 a series of ``applications''.
370 U_par ppat -> -- parenthesised pattern
371 wlkPat ppat `thenUgn` \ pat ->
372 -- tidy things up a little:
377 other -> ParPatIn pat
380 U_as avar as_pat -> -- "as" pattern
381 wlkVarId avar `thenUgn` \ var ->
382 wlkPat as_pat `thenUgn` \ pat ->
383 returnUgn (AsPatIn var pat)
385 U_lazyp lazyp -> -- irrefutable ("twiddle") pattern
386 wlkPat lazyp `thenUgn` \ pat ->
387 returnUgn (LazyPatIn pat)
389 U_wildp -> returnUgn WildPatIn -- wildcard pattern
391 U_lit lit -> -- literal pattern
392 wlkLiteral lit `thenUgn` \ lit ->
393 returnUgn (LitPatIn lit)
395 U_ident nn -> -- simple identifier
396 wlkVarId nn `thenUgn` \ n ->
399 VarOcc occ | isLexConId occ -> ConPatIn n []
403 U_ap l r -> -- "application": there's a list of patterns lurking here!
404 wlkPat r `thenUgn` \ rpat ->
405 collect_pats l [rpat] `thenUgn` \ (lpat,lpats) ->
407 VarPatIn x -> returnUgn (x, lpats)
408 ConPatIn x [] -> returnUgn (x, lpats)
409 ConOpPatIn x op _ y -> returnUgn (op, x:y:lpats)
410 _ -> getSrcLocUgn `thenUgn` \ loc ->
412 err = addErrLoc loc "Illegal pattern `application'"
413 (\sty -> ppInterleave ppSP (map (ppr sty) (lpat:lpats)))
414 msg = ppShow 100 (err PprForUser)
416 #if __GLASGOW_HASKELL__ >= 200
417 ioToUgnM (GHCbase.ioToPrimIO (hPutStr stderr msg)) `thenUgn` \ _ ->
418 ioToUgnM (GHCbase.ioToPrimIO (ghcExit 1)) `thenUgn` \ _ ->
420 ioToUgnM (hPutStr stderr msg) `thenUgn` \ _ ->
421 ioToUgnM (ghcExit 1) `thenUgn` \ _ ->
423 returnUgn (error "ReadPrefix")
425 ) `thenUgn` \ (n, arg_pats) ->
426 returnUgn (ConPatIn n arg_pats)
431 wlkPat r `thenUgn` \ rpat ->
432 collect_pats l (rpat:acc)
434 wlkPat other `thenUgn` \ pat ->
437 U_infixap fun arg1 arg2 -> -- infix pattern
438 wlkVarId fun `thenUgn` \ op ->
439 wlkPat arg1 `thenUgn` \ pat1 ->
440 wlkPat arg2 `thenUgn` \ pat2 ->
441 returnUgn (ConOpPatIn pat1 op (error "ConOpPatIn:fixity") pat2)
443 U_negate npat -> -- negated pattern
444 wlkPat npat `thenUgn` \ pat ->
445 returnUgn (NegPatIn pat)
447 U_llist llist -> -- explicit list
448 wlkList rdPat llist `thenUgn` \ pats ->
449 returnUgn (ListPatIn pats)
451 U_tuple tuplelist -> -- explicit tuple
452 wlkList rdPat tuplelist `thenUgn` \ pats ->
453 returnUgn (TuplePatIn pats)
455 U_record con rpats -> -- record destruction
456 wlkDataId con `thenUgn` \ rcon ->
457 wlkList rdRpat rpats `thenUgn` \ recpats ->
458 returnUgn (RecPatIn rcon recpats)
461 = rdU_tree pt `thenUgn` \ (U_rbind var pat) ->
462 wlkVarId var `thenUgn` \ rvar ->
463 wlkMaybe rdPat pat `thenUgn` \ pat_maybe ->
466 Nothing -> (rvar, VarPatIn rvar, True{-pun-})
467 Just rp -> (rvar, rp, False)
472 wlkLiteral :: U_literal -> UgnM HsLit
477 U_integer s -> HsInt (as_integer s)
478 U_floatr s -> HsFrac (as_rational s)
479 U_intprim s -> HsIntPrim (as_integer s)
480 U_doubleprim s -> HsDoublePrim (as_rational s)
481 U_floatprim s -> HsFloatPrim (as_rational s)
482 U_charr s -> HsChar (as_char s)
483 U_charprim s -> HsCharPrim (as_char s)
484 U_string s -> HsString (as_string s)
485 U_stringprim s -> HsStringPrim (as_string s)
486 U_clitlit s -> HsLitLit (as_string s)
490 as_integer s = readInteger (_UNPK_ s)
491 #if __GLASGOW_HASKELL__ >= 200
492 as_rational s = GHCbase.readRational__ (_UNPK_ s) -- non-std
494 as_rational s = _readRational (_UNPK_ s) -- non-std
499 %************************************************************************
501 \subsection{wlkBinding}
503 %************************************************************************
506 wlkBinding :: U_binding -> UgnM RdrBinding
512 returnUgn RdrNullBind
514 -- "and" binding (just glue, really)
516 wlkBinding a `thenUgn` \ binding1 ->
517 wlkBinding b `thenUgn` \ binding2 ->
518 returnUgn (RdrAndBindings binding1 binding2)
520 -- "data" declaration
521 U_tbind tctxt ttype tcons tderivs srcline ->
522 mkSrcLocUgn srcline $ \ src_loc ->
523 wlkContext tctxt `thenUgn` \ ctxt ->
524 wlkTyConAndTyVars ttype `thenUgn` \ (tycon, tyvars) ->
525 wlkList rdConDecl tcons `thenUgn` \ cons ->
526 wlkDerivings tderivs `thenUgn` \ derivings ->
527 returnUgn (RdrTyDecl (TyData ctxt tycon tyvars cons derivings noDataPragmas src_loc))
529 -- "newtype" declaration
530 U_ntbind ntctxt nttype ntcon ntderivs srcline ->
531 mkSrcLocUgn srcline $ \ src_loc ->
532 wlkContext ntctxt `thenUgn` \ ctxt ->
533 wlkTyConAndTyVars nttype `thenUgn` \ (tycon, tyvars) ->
534 wlkList rdConDecl ntcon `thenUgn` \ [con] ->
535 wlkDerivings ntderivs `thenUgn` \ derivings ->
536 returnUgn (RdrTyDecl (TyNew ctxt tycon tyvars con derivings noDataPragmas src_loc))
538 -- "type" declaration
539 U_nbind nbindid nbindas srcline ->
540 mkSrcLocUgn srcline $ \ src_loc ->
541 wlkTyConAndTyVars nbindid `thenUgn` \ (tycon, tyvars) ->
542 wlkMonoType nbindas `thenUgn` \ expansion ->
543 returnUgn (RdrTyDecl (TySynonym tycon tyvars expansion src_loc))
546 U_fbind fbindl srcline ->
547 mkSrcLocUgn srcline $ \ src_loc ->
548 wlkList rdMatch fbindl `thenUgn` \ matches ->
549 returnUgn (RdrFunctionBinding srcline matches)
552 U_pbind pbindl srcline ->
553 mkSrcLocUgn srcline $ \ src_loc ->
554 wlkList rdMatch pbindl `thenUgn` \ matches ->
555 returnUgn (RdrPatternBinding srcline matches)
557 -- "class" declaration
558 U_cbind cbindc cbindid cbindw srcline ->
559 mkSrcLocUgn srcline $ \ src_loc ->
560 wlkContext cbindc `thenUgn` \ ctxt ->
561 wlkClassAssertTy cbindid `thenUgn` \ (clas, tyvar)->
562 wlkBinding cbindw `thenUgn` \ binding ->
563 getSrcFileUgn `thenUgn` \ sf ->
565 (final_methods, final_sigs) = cvMonoBindsAndSigs sf cvClassOpSig binding
567 returnUgn (RdrClassDecl
568 (ClassDecl ctxt clas tyvar final_sigs final_methods noClassPragmas src_loc))
570 -- "instance" declaration
571 U_ibind ibindc iclas ibindi ibindw srcline ->
572 mkSrcLocUgn srcline $ \ src_loc ->
573 wlkContext ibindc `thenUgn` \ ctxt ->
574 wlkTCId iclas `thenUgn` \ clas ->
575 wlkMonoType ibindi `thenUgn` \ at_ty ->
576 wlkBinding ibindw `thenUgn` \ binding ->
577 getSrcModUgn `thenUgn` \ modname ->
578 getSrcFileUgn `thenUgn` \ sf ->
580 (binds,uprags) = cvMonoBindsAndSigs sf cvInstDeclSig binding
581 inst_ty = HsPreForAllTy ctxt (MonoDictTy clas at_ty)
583 returnUgn (RdrInstDecl
584 (InstDecl inst_ty binds uprags Nothing {- No dfun id -} src_loc))
586 -- "default" declaration
587 U_dbind dbindts srcline ->
588 mkSrcLocUgn srcline $ \ src_loc ->
589 wlkList rdMonoType dbindts `thenUgn` \ tys ->
590 returnUgn (RdrDefaultDecl (DefaultDecl tys src_loc))
593 -- signature(-like) things, including user pragmas
594 wlk_sig_thing a_sig_we_hope
598 wlkDerivings :: U_maybe -> UgnM (Maybe [RdrName])
600 wlkDerivings (U_nothing) = returnUgn Nothing
601 wlkDerivings (U_just pt)
602 = rdU_list pt `thenUgn` \ ds ->
603 wlkList rdTCId ds `thenUgn` \ derivs ->
604 returnUgn (Just derivs)
609 wlk_sig_thing (U_sbind sbindids sbindid srcline)
610 = mkSrcLocUgn srcline $ \ src_loc ->
611 wlkList rdVarId sbindids `thenUgn` \ vars ->
612 wlkHsType sbindid `thenUgn` \ poly_ty ->
613 returnUgn (RdrTySig vars poly_ty src_loc)
615 -- value specialisation user-pragma
616 wlk_sig_thing (U_vspec_uprag uvar vspec_tys srcline)
617 = mkSrcLocUgn srcline $ \ src_loc ->
618 wlkVarId uvar `thenUgn` \ var ->
619 wlkList rd_ty_and_id vspec_tys `thenUgn` \ tys_and_ids ->
620 returnUgn (RdrSpecValSig [SpecSig var ty using_id src_loc
621 | (ty, using_id) <- tys_and_ids ])
623 rd_ty_and_id :: ParseTree -> UgnM (RdrNameHsType, Maybe RdrName)
625 = rdU_binding pt `thenUgn` \ (U_vspec_ty_and_id vspec_ty vspec_id) ->
626 wlkHsType vspec_ty `thenUgn` \ ty ->
627 wlkMaybe rdVarId vspec_id `thenUgn` \ id_maybe ->
628 returnUgn(ty, id_maybe)
630 -- instance specialisation user-pragma
631 wlk_sig_thing (U_ispec_uprag iclas ispec_ty srcline)
632 = mkSrcLocUgn srcline $ \ src_loc ->
633 wlkTCId iclas `thenUgn` \ clas ->
634 wlkMonoType ispec_ty `thenUgn` \ ty ->
635 returnUgn (RdrSpecInstSig (SpecInstSig clas ty src_loc))
637 -- data specialisation user-pragma
638 wlk_sig_thing (U_dspec_uprag itycon dspec_tys srcline)
639 = mkSrcLocUgn srcline $ \ src_loc ->
640 wlkTCId itycon `thenUgn` \ tycon ->
641 wlkList rdMonoType dspec_tys `thenUgn` \ tys ->
642 returnUgn (RdrSpecDataSig (SpecDataSig tycon (foldl MonoTyApp (MonoTyVar tycon) tys) src_loc))
644 -- value inlining user-pragma
645 wlk_sig_thing (U_inline_uprag ivar srcline)
646 = mkSrcLocUgn srcline $ \ src_loc ->
647 wlkVarId ivar `thenUgn` \ var ->
648 returnUgn (RdrInlineValSig (InlineSig var src_loc))
650 -- "deforest me" user-pragma
651 wlk_sig_thing (U_deforest_uprag ivar srcline)
652 = mkSrcLocUgn srcline $ \ src_loc ->
653 wlkVarId ivar `thenUgn` \ var ->
654 returnUgn (RdrDeforestSig (DeforestSig var src_loc))
656 -- "magic" unfolding user-pragma
657 wlk_sig_thing (U_magicuf_uprag ivar str srcline)
658 = mkSrcLocUgn srcline $ \ src_loc ->
659 wlkVarId ivar `thenUgn` \ var ->
660 returnUgn (RdrMagicUnfoldingSig (MagicUnfoldingSig var str src_loc))
663 %************************************************************************
665 \subsection[wlkTypes]{Reading in types in various forms (and data constructors)}
667 %************************************************************************
670 rdHsType :: ParseTree -> UgnM RdrNameHsType
671 rdMonoType :: ParseTree -> UgnM RdrNameHsType
673 rdHsType pt = rdU_ttype pt `thenUgn` \ ttype -> wlkHsType ttype
674 rdMonoType pt = rdU_ttype pt `thenUgn` \ ttype -> wlkMonoType ttype
676 wlkHsType :: U_ttype -> UgnM RdrNameHsType
677 wlkMonoType :: U_ttype -> UgnM RdrNameHsType
681 U_context tcontextl tcontextt -> -- context
682 wlkContext tcontextl `thenUgn` \ ctxt ->
683 wlkMonoType tcontextt `thenUgn` \ ty ->
684 returnUgn (HsPreForAllTy ctxt ty)
686 other -> -- something else
687 wlkMonoType other `thenUgn` \ ty ->
688 returnUgn (HsPreForAllTy [{-no context-}] ty)
692 U_namedtvar tv -> -- type variable
693 wlkTvId tv `thenUgn` \ tyvar ->
694 returnUgn (MonoTyVar tyvar)
696 U_tname tcon -> -- type constructor
697 wlkTCId tcon `thenUgn` \ tycon ->
698 returnUgn (MonoTyVar tycon)
701 wlkMonoType t1 `thenUgn` \ ty1 ->
702 wlkMonoType t2 `thenUgn` \ ty2 ->
703 returnUgn (MonoTyApp ty1 ty2)
705 U_tllist tlist -> -- list type
706 wlkMonoType tlist `thenUgn` \ ty ->
707 returnUgn (MonoListTy dummyRdrTcName ty)
710 wlkList rdMonoType ttuple `thenUgn` \ tys ->
711 returnUgn (MonoTupleTy dummyRdrTcName tys)
714 wlkMonoType tfun `thenUgn` \ ty1 ->
715 wlkMonoType targ `thenUgn` \ ty2 ->
716 returnUgn (MonoFunTy ty1 ty2)
721 wlkTyConAndTyVars :: U_ttype -> UgnM (RdrName, [HsTyVar RdrName])
722 wlkContext :: U_list -> UgnM RdrNameContext
723 wlkClassAssertTy :: U_ttype -> UgnM (RdrName, HsTyVar RdrName)
725 wlkTyConAndTyVars ttype
726 = wlkMonoType ttype `thenUgn` \ ty ->
728 split (MonoTyApp fun (MonoTyVar arg)) args = split fun (UserTyVar arg : args)
729 split (MonoTyVar tycon) args = (tycon,args)
731 returnUgn (split ty [])
734 = wlkList rdMonoType list `thenUgn` \ tys ->
735 returnUgn (map mk_class_assertion tys)
738 = wlkMonoType xs `thenUgn` \ mono_ty ->
739 returnUgn (case mk_class_assertion mono_ty of
740 (clas, MonoTyVar tyvar) -> (clas, UserTyVar tyvar)
743 mk_class_assertion :: RdrNameHsType -> (RdrName, RdrNameHsType)
745 mk_class_assertion (MonoTyApp (MonoTyVar name) ty@(MonoTyVar tyname)) = (name, ty)
746 mk_class_assertion other
747 = pprError "ERROR: malformed type context: " (ppr PprForUser other)
748 -- regrettably, the parser does let some junk past
749 -- e.g., f :: Num {-nothing-} => a -> ...
753 rdConDecl :: ParseTree -> UgnM RdrNameConDecl
755 = rdU_constr pt `thenUgn` \ blah ->
758 wlkConDecl :: U_constr -> UgnM RdrNameConDecl
760 wlkConDecl (U_constrpre ccon ctys srcline)
761 = mkSrcLocUgn srcline $ \ src_loc ->
762 wlkDataId ccon `thenUgn` \ con ->
763 wlkList rdBangType ctys `thenUgn` \ tys ->
764 returnUgn (ConDecl con tys src_loc)
766 wlkConDecl (U_constrinf cty1 cop cty2 srcline)
767 = mkSrcLocUgn srcline $ \ src_loc ->
768 wlkBangType cty1 `thenUgn` \ ty1 ->
769 wlkDataId cop `thenUgn` \ op ->
770 wlkBangType cty2 `thenUgn` \ ty2 ->
771 returnUgn (ConOpDecl ty1 op ty2 src_loc)
773 wlkConDecl (U_constrnew ccon cty srcline)
774 = mkSrcLocUgn srcline $ \ src_loc ->
775 wlkDataId ccon `thenUgn` \ con ->
776 wlkMonoType cty `thenUgn` \ ty ->
777 returnUgn (NewConDecl con ty src_loc)
779 wlkConDecl (U_constrrec ccon cfields srcline)
780 = mkSrcLocUgn srcline $ \ src_loc ->
781 wlkDataId ccon `thenUgn` \ con ->
782 wlkList rd_field cfields `thenUgn` \ fields_lists ->
783 returnUgn (RecConDecl con fields_lists src_loc)
785 rd_field :: ParseTree -> UgnM ([RdrName], BangType RdrName)
787 = rdU_constr pt `thenUgn` \ (U_field fvars fty) ->
788 wlkList rdVarId fvars `thenUgn` \ vars ->
789 wlkBangType fty `thenUgn` \ ty ->
793 rdBangType pt = rdU_ttype pt `thenUgn` \ ty -> wlkBangType ty
795 wlkBangType :: U_ttype -> UgnM (BangType RdrName)
797 wlkBangType (U_tbang bty) = wlkMonoType bty `thenUgn` \ ty ->
798 returnUgn (Banged ty)
799 wlkBangType uty = wlkMonoType uty `thenUgn` \ ty ->
800 returnUgn (Unbanged ty)
803 %************************************************************************
805 \subsection{Read a ``match''}
807 %************************************************************************
810 rdMatch :: ParseTree -> UgnM RdrMatch
813 = rdU_pbinding pt `thenUgn` \ (U_pgrhs gpat gdexprs gbind gsrcfun srcline) ->
814 mkSrcLocUgn srcline $ \ src_loc ->
815 wlkPat gpat `thenUgn` \ pat ->
816 wlkBinding gbind `thenUgn` \ binding ->
817 wlkVarId gsrcfun `thenUgn` \ srcfun ->
819 wlk_guards (U_pnoguards exp)
820 = wlkExpr exp `thenUgn` \ expr ->
821 returnUgn (RdrMatch_NoGuard srcline srcfun pat expr binding)
823 wlk_guards (U_pguards gs)
824 = wlkList rd_gd_expr gs `thenUgn` \ gd_exps ->
825 returnUgn (RdrMatch_Guards srcline srcfun pat gd_exps binding)
830 = rdU_pbinding pt `thenUgn` \ (U_pgdexp g e) ->
831 wlkExpr g `thenUgn` \ guard ->
832 wlkExpr e `thenUgn` \ expr ->
833 returnUgn (guard, expr)
836 %************************************************************************
838 \subsection[rdFixOp]{Read in a fixity declaration}
840 %************************************************************************
843 rdFixOp :: ParseTree -> UgnM RdrNameFixityDecl
845 = rdU_tree pt `thenUgn` \ fix ->
847 U_fixop op dir_n prec -> wlkVarId op `thenUgn` \ op ->
848 returnUgn (FixityDecl op (Fixity prec dir) noSrcLoc)
855 _ -> error "ReadPrefix:rdFixOp"
858 %************************************************************************
860 \subsection[rdImport]{Read an import decl}
862 %************************************************************************
865 rdImport :: ParseTree
866 -> UgnM RdrNameImportDecl
869 = rdU_binding pt `thenUgn` \ (U_import imod iqual ias ispec srcline) ->
870 mkSrcLocUgn srcline $ \ src_loc ->
871 wlkMaybe rdU_stringId ias `thenUgn` \ maybe_as ->
872 wlkMaybe rd_spec ispec `thenUgn` \ maybe_spec ->
873 returnUgn (ImportDecl imod (cvFlag iqual) maybe_as maybe_spec src_loc)
875 rd_spec pt = rdU_either pt `thenUgn` \ spec ->
877 U_left pt -> rdEntities pt `thenUgn` \ ents ->
878 returnUgn (False, ents)
879 U_right pt -> rdEntities pt `thenUgn` \ ents ->
880 returnUgn (True, ents)
885 = rdU_list pt `thenUgn` \ list ->
886 wlkList rdEntity list
888 rdEntity :: ParseTree -> UgnM (IE RdrName)
891 = rdU_entidt pt `thenUgn` \ entity ->
893 U_entid evar -> -- just a value
894 wlkEntId evar `thenUgn` \ var ->
895 returnUgn (IEVar var)
897 U_enttype x -> -- abstract type constructor/class
898 wlkTCId x `thenUgn` \ thing ->
899 returnUgn (IEThingAbs thing)
901 U_enttypeall x -> -- non-abstract type constructor/class
902 wlkTCId x `thenUgn` \ thing ->
903 returnUgn (IEThingAll thing)
905 U_enttypenamed x ns -> -- non-abstract type constructor/class
906 -- with specified constrs/methods
907 wlkTCId x `thenUgn` \ thing ->
908 wlkList rdVarId ns `thenUgn` \ names ->
909 returnUgn (IEThingWith thing names)
911 U_entmod mod -> -- everything provided unqualified by a module
912 returnUgn (IEModuleContents mod)