2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section{Read parse tree built by Yacc parser}
7 module ReadPrefix ( rdModule ) where
9 #include "HsVersions.h"
11 import UgenAll -- all Yacc parser gumpff...
12 import PrefixSyn -- and various syntaxen.
14 import HsTypes ( HsTyVar(..) )
15 import HsPragmas ( noDataPragmas, noClassPragmas )
17 import BasicTypes ( Fixity(..), FixityDirection(..), NewOrData(..) )
18 import PrelMods ( pRELUDE_Name )
22 import CmdLineOpts ( opt_NoImplicitPrelude, opt_GlasgowExts, opt_D_dump_rdr )
23 import Module ( ModuleName, mkSrcModuleFS, WhereFrom(..) )
24 import OccName ( NameSpace, tcName, clsName, tcClsName, varName, dataName, tvName,
27 import RdrName ( RdrName, isRdrDataCon, mkSrcQual, mkSrcUnqual, mkPreludeQual,
31 import ErrUtils ( dumpIfSet )
32 import SrcLoc ( SrcLoc )
33 import FastString ( mkFastCharString )
34 import PrelRead ( readRational__ )
37 %************************************************************************
39 \subsection[rdModule]{@rdModule@: reads in a Haskell module}
41 %************************************************************************
44 rdModule :: IO (ModuleName, -- this module's name
45 RdrNameHsModule) -- the main goods
48 = -- call the Yacc parser!
49 _ccall_ hspmain >>= \ pt ->
51 -- Read from the Yacc tree
52 initUgn (read_module pt) >>= \ (mod_name, rdr_module) ->
55 dumpIfSet opt_D_dump_rdr "Reader"
59 return (mod_name, rdr_module)
61 read_module :: ParseTree -> UgnM (ModuleName, RdrNameHsModule)
63 = rdU_tree pt `thenUgn` \ (U_hmodule mod_fs himplist hexplist
64 hmodlist srciface_version srcline) ->
66 srcfile = mkFastCharString ``input_filename'' -- What A Great Hack! (TM)
67 mod_name = mkSrcModuleFS mod_fs
70 setSrcFileUgn srcfile $
71 mkSrcLocUgn srcline $ \ src_loc ->
73 wlkMaybe rdEntities hexplist `thenUgn` \ exports ->
74 wlkList rdImport himplist `thenUgn` \ imports ->
75 wlkBinding hmodlist `thenUgn` \ binding ->
78 top_decls = cvTopDecls srcfile binding
79 rdr_module = HsModule mod_name
80 (case srciface_version of { 0 -> Nothing; n -> Just n })
86 returnUgn (mod_name, rdr_module)
89 %************************************************************************
91 \subsection[wlkExprOrPat]{@wlkExpr@ and @wlkPat@}
93 %************************************************************************
96 rdExpr :: ParseTree -> UgnM RdrNameHsExpr
97 rdPat :: ParseTree -> UgnM RdrNamePat
99 rdExpr pt = rdU_tree pt `thenUgn` wlkExpr
100 rdPat pt = rdU_tree pt `thenUgn` wlkPat
102 wlkExpr :: U_tree -> UgnM RdrNameHsExpr
103 wlkPat :: U_tree -> UgnM RdrNamePat
107 U_par pexpr -> -- parenthesised expr
108 wlkExpr pexpr `thenUgn` \ expr ->
109 returnUgn (HsPar expr)
111 U_lsection lsexp lop -> -- left section
112 wlkExpr lsexp `thenUgn` \ expr ->
113 wlkVarId lop `thenUgn` \ op ->
114 returnUgn (SectionL expr (HsVar op))
116 U_rsection rop rsexp -> -- right section
117 wlkVarId rop `thenUgn` \ op ->
118 wlkExpr rsexp `thenUgn` \ expr ->
119 returnUgn (SectionR (HsVar op) expr)
121 U_ccall fun flavor ccargs -> -- ccall/casm
122 wlkList rdExpr ccargs `thenUgn` \ args ->
126 returnUgn (CCall fun args
127 (tag == 'p' || tag == 'P') -- may invoke GC
128 (tag == 'N' || tag == 'P') -- really a "casm"
129 (panic "CCall:result_ty"))
131 U_scc label sccexp -> -- scc (set-cost-centre) expression
132 wlkExpr sccexp `thenUgn` \ expr ->
133 returnUgn (HsSCC label expr)
135 U_lambda match -> -- lambda expression
136 wlkMatch match `thenUgn` \ match' ->
137 returnUgn (HsLam match')
139 U_casee caseexpr casebody srcline -> -- case expression
140 mkSrcLocUgn srcline $ \ src_loc ->
141 wlkExpr caseexpr `thenUgn` \ expr ->
142 wlkList rdMatch casebody `thenUgn` \ mats ->
143 returnUgn (HsCase expr mats src_loc)
145 U_ife ifpred ifthen ifelse srcline -> -- if expression
146 mkSrcLocUgn srcline $ \ src_loc ->
147 wlkExpr ifpred `thenUgn` \ e1 ->
148 wlkExpr ifthen `thenUgn` \ e2 ->
149 wlkExpr ifelse `thenUgn` \ e3 ->
150 returnUgn (HsIf e1 e2 e3 src_loc)
152 U_let letvdefs letvexpr -> -- let expression
153 wlkLocalBinding letvdefs `thenUgn` \ binding ->
154 wlkExpr letvexpr `thenUgn` \ expr ->
155 returnUgn (HsLet binding expr)
157 U_doe gdo srcline -> -- do expression
158 mkSrcLocUgn srcline $ \ src_loc ->
159 wlkList rd_stmt gdo `thenUgn` \ stmts ->
160 returnUgn (HsDo DoStmt stmts src_loc)
163 = rdU_tree pt `thenUgn` \ bind ->
165 U_doexp exp srcline ->
166 mkSrcLocUgn srcline $ \ src_loc ->
167 wlkExpr exp `thenUgn` \ expr ->
168 returnUgn (ExprStmt expr src_loc)
170 U_dobind pat exp srcline ->
171 mkSrcLocUgn srcline $ \ src_loc ->
172 wlkPat pat `thenUgn` \ patt ->
173 wlkExpr exp `thenUgn` \ expr ->
174 returnUgn (BindStmt patt expr src_loc)
177 wlkLocalBinding seqlet `thenUgn` \ binds ->
178 returnUgn (LetStmt binds)
180 U_comprh cexp cquals -> -- list comprehension
181 wlkExpr cexp `thenUgn` \ expr ->
182 wlkQuals cquals `thenUgn` \ quals ->
183 getSrcLocUgn `thenUgn` \ loc ->
184 returnUgn (HsDo ListComp (quals ++ [ReturnStmt expr]) loc)
186 U_eenum efrom estep eto -> -- arithmetic sequence
187 wlkExpr efrom `thenUgn` \ e1 ->
188 wlkMaybe rdExpr estep `thenUgn` \ es2 ->
189 wlkMaybe rdExpr eto `thenUgn` \ es3 ->
190 returnUgn (cv_arith_seq e1 es2 es3)
192 cv_arith_seq e1 Nothing Nothing = ArithSeqIn (From e1)
193 cv_arith_seq e1 Nothing (Just e3) = ArithSeqIn (FromTo e1 e3)
194 cv_arith_seq e1 (Just e2) Nothing = ArithSeqIn (FromThen e1 e2)
195 cv_arith_seq e1 (Just e2) (Just e3) = ArithSeqIn (FromThenTo e1 e2 e3)
197 U_restr restre restrt -> -- expression with type signature
198 wlkExpr restre `thenUgn` \ expr ->
199 wlkHsSigType restrt `thenUgn` \ ty ->
200 returnUgn (ExprWithTySig expr ty)
202 --------------------------------------------------------------
203 -- now the prefix items that can either be an expression or
204 -- pattern, except we know they are *expressions* here
205 -- (this code could be commoned up with the pattern version;
206 -- but it probably isn't worth it)
207 --------------------------------------------------------------
209 wlkLiteral lit `thenUgn` \ lit ->
210 returnUgn (HsLit lit)
212 U_ident n -> -- simple identifier
213 wlkVarId n `thenUgn` \ var ->
214 returnUgn (HsVar var)
216 U_ap fun arg -> -- application
217 wlkExpr fun `thenUgn` \ expr1 ->
218 wlkExpr arg `thenUgn` \ expr2 ->
219 returnUgn (HsApp expr1 expr2)
221 U_infixap fun arg1 arg2 -> -- infix application
222 wlkVarId fun `thenUgn` \ op ->
223 wlkExpr arg1 `thenUgn` \ expr1 ->
224 wlkExpr arg2 `thenUgn` \ expr2 ->
225 returnUgn (mkOpApp expr1 op expr2)
227 U_negate nexp -> -- prefix negation
228 wlkExpr nexp `thenUgn` \ expr ->
229 returnUgn (NegApp expr (HsVar dummyRdrVarName))
231 U_llist llist -> -- explicit list
232 wlkList rdExpr llist `thenUgn` \ exprs ->
233 returnUgn (ExplicitList exprs)
235 U_tuple tuplelist -> -- explicit tuple
236 wlkList rdExpr tuplelist `thenUgn` \ exprs ->
237 returnUgn (ExplicitTuple exprs True)
239 U_utuple tuplelist -> -- explicit tuple
240 wlkList rdExpr tuplelist `thenUgn` \ exprs ->
241 returnUgn (ExplicitTuple exprs False)
243 U_record con rbinds -> -- record construction
244 wlkDataId con `thenUgn` \ rcon ->
245 wlkList rdRbind rbinds `thenUgn` \ recbinds ->
246 returnUgn (RecordCon rcon recbinds)
248 U_rupdate updexp updbinds -> -- record update
249 wlkExpr updexp `thenUgn` \ aexp ->
250 wlkList rdRbind updbinds `thenUgn` \ recbinds ->
251 returnUgn (RecordUpd aexp recbinds)
254 U_hmodule _ _ _ _ _ _ -> error "U_hmodule"
255 U_as _ _ -> error "U_as"
256 U_lazyp _ -> error "U_lazyp"
257 U_qual _ _ -> error "U_qual"
258 U_guard _ -> error "U_guard"
259 U_seqlet _ -> error "U_seqlet"
260 U_dobind _ _ _ -> error "U_dobind"
261 U_doexp _ _ -> error "U_doexp"
262 U_rbind _ _ -> error "U_rbind"
266 = rdU_tree pt `thenUgn` \ (U_rbind var exp) ->
267 wlkVarId var `thenUgn` \ rvar ->
268 wlkMaybe rdExpr exp `thenUgn` \ expr_maybe ->
271 Nothing -> (rvar, HsVar rvar, True{-pun-})
272 Just re -> (rvar, re, False)
276 = wlkList rd_qual cquals
279 = rdU_tree pt `thenUgn` \ qual ->
285 wlkExpr exp `thenUgn` \ expr ->
286 getSrcLocUgn `thenUgn` \ loc ->
287 returnUgn (GuardStmt expr loc)
290 wlkPat qpat `thenUgn` \ pat ->
291 wlkExpr qexp `thenUgn` \ expr ->
292 getSrcLocUgn `thenUgn` \ loc ->
293 returnUgn (BindStmt pat expr loc)
296 wlkLocalBinding seqlet `thenUgn` \ binds ->
297 returnUgn (LetStmt binds)
299 U_let letvdefs letvexpr ->
300 wlkLocalBinding letvdefs `thenUgn` \ binds ->
301 wlkExpr letvexpr `thenUgn` \ expr ->
302 getSrcLocUgn `thenUgn` \ loc ->
303 returnUgn (GuardStmt (HsLet binds expr) loc)
306 Patterns: just bear in mind that lists of patterns are represented as
307 a series of ``applications''.
311 U_par ppat -> -- parenthesised pattern
312 wlkPat ppat `thenUgn` \ pat ->
313 -- tidy things up a little:
318 other -> ParPatIn pat
321 U_as avar as_pat -> -- "as" pattern
322 wlkVarId avar `thenUgn` \ var ->
323 wlkPat as_pat `thenUgn` \ pat ->
324 returnUgn (AsPatIn var pat)
327 wlkPat pat `thenUgn` \ pat' ->
328 wlkHsType ty `thenUgn` \ ty' ->
329 returnUgn (SigPatIn pat' ty')
331 U_lazyp lazyp -> -- irrefutable ("twiddle") pattern
332 wlkPat lazyp `thenUgn` \ pat ->
333 returnUgn (LazyPatIn pat)
336 wlkVarId avar `thenUgn` \ var ->
337 wlkLiteral lit `thenUgn` \ lit ->
338 returnUgn (NPlusKPatIn var lit)
340 U_lit lit -> -- literal pattern
341 wlkLiteral lit `thenUgn` \ lit ->
342 returnUgn (LitPatIn lit)
344 U_ident (U_noqual s) | s == SLIT("_")-> returnUgn WildPatIn -- Wild-card pattern
346 U_ident nn -> -- simple identifier
347 wlkVarId nn `thenUgn` \ n ->
349 if isRdrDataCon n then
355 U_ap l r -> -- "application": there's a list of patterns lurking here!
356 wlkPat r `thenUgn` \ rpat ->
357 collect_pats l [rpat] `thenUgn` \ (lpat,lpats) ->
359 VarPatIn x -> returnUgn (x, lpats)
360 ConPatIn x [] -> returnUgn (x, lpats)
361 ConOpPatIn x op _ y -> returnUgn (op, x:y:lpats)
362 _ -> getSrcLocUgn `thenUgn` \ loc ->
363 pprPanic "Illegal pattern `application'"
364 (ppr loc <> colon <+> hsep (map ppr (lpat:lpats)))
366 ) `thenUgn` \ (n, arg_pats) ->
367 returnUgn (ConPatIn n arg_pats)
372 wlkPat r `thenUgn` \ rpat ->
373 collect_pats l (rpat:acc)
377 wlkPat other `thenUgn` \ pat ->
380 U_infixap fun arg1 arg2 -> -- infix pattern
381 wlkVarId fun `thenUgn` \ op ->
382 wlkPat arg1 `thenUgn` \ pat1 ->
383 wlkPat arg2 `thenUgn` \ pat2 ->
384 returnUgn (ConOpPatIn pat1 op (error "ConOpPatIn:fixity") pat2)
386 U_negate npat -> -- negated pattern
387 wlkPat npat `thenUgn` \ pat ->
388 returnUgn (NegPatIn pat)
390 U_llist llist -> -- explicit list
391 wlkList rdPat llist `thenUgn` \ pats ->
392 returnUgn (ListPatIn pats)
394 U_tuple tuplelist -> -- explicit tuple
395 wlkList rdPat tuplelist `thenUgn` \ pats ->
396 returnUgn (TuplePatIn pats True)
398 U_utuple tuplelist -> -- explicit tuple
399 wlkList rdPat tuplelist `thenUgn` \ pats ->
400 returnUgn (TuplePatIn pats False)
402 U_record con rpats -> -- record destruction
403 wlkDataId con `thenUgn` \ rcon ->
404 wlkList rdRpat rpats `thenUgn` \ recpats ->
405 returnUgn (RecPatIn rcon recpats)
408 = rdU_tree pt `thenUgn` \ (U_rbind var pat) ->
409 wlkVarId var `thenUgn` \ rvar ->
410 wlkMaybe rdPat pat `thenUgn` \ pat_maybe ->
413 Nothing -> (rvar, VarPatIn rvar, True{-pun-})
414 Just rp -> (rvar, rp, False)
419 wlkLiteral :: U_literal -> UgnM HsLit
424 U_integer s -> HsInt (as_integer s)
425 U_floatr s -> HsFrac (as_rational s)
426 U_intprim s -> HsIntPrim (as_integer s)
427 U_doubleprim s -> HsDoublePrim (as_rational s)
428 U_floatprim s -> HsFloatPrim (as_rational s)
429 U_charr s -> HsChar (as_char s)
430 U_charprim s -> HsCharPrim (as_char s)
431 U_string s -> HsString (as_string s)
432 U_stringprim s -> HsStringPrim (as_string s)
433 U_clitlit s -> HsLitLit (as_string s)
437 as_integer s = readInteger (_UNPK_ s)
438 as_rational s = readRational__ (_UNPK_ s) -- use non-std readRational__
439 -- to handle rationals with leading '-'
443 %************************************************************************
445 \subsection{wlkBinding}
447 %************************************************************************
451 = wlkBinding bind `thenUgn` \ bind' ->
452 getSrcFileUgn `thenUgn` \ sf ->
453 returnUgn (cvBinds sf cvValSig bind')
455 wlkBinding :: U_binding -> UgnM RdrBinding
461 returnUgn RdrNullBind
463 -- "and" binding (just glue, really)
465 wlkBinding a `thenUgn` \ binding1 ->
466 wlkBinding b `thenUgn` \ binding2 ->
467 returnUgn (RdrAndBindings binding1 binding2)
469 -- fixity declaration
470 U_fixd op dir_n prec srcline ->
477 wlkVarId op `thenUgn` \ op ->
478 mkSrcLocUgn srcline $ \ src_loc ->
479 returnUgn (RdrSig (FixSig (FixitySig op (Fixity prec dir) src_loc)))
482 -- "data" declaration
483 U_tbind tctxt ttype tcons tderivs srcline ->
484 mkSrcLocUgn srcline $ \ src_loc ->
485 wlkContext tctxt `thenUgn` \ ctxt ->
486 wlkConAndTyVars ttype `thenUgn` \ (tycon, tyvars) ->
487 wlkList rdConDecl tcons `thenUgn` \ cons ->
488 wlkDerivings tderivs `thenUgn` \ derivings ->
489 returnUgn (RdrHsDecl (TyClD (TyData DataType ctxt tycon tyvars cons
490 derivings noDataPragmas src_loc)))
492 -- "newtype" declaration
493 U_ntbind ntctxt nttype ntcon ntderivs srcline ->
494 mkSrcLocUgn srcline $ \ src_loc ->
495 wlkContext ntctxt `thenUgn` \ ctxt ->
496 wlkConAndTyVars nttype `thenUgn` \ (tycon, tyvars) ->
497 wlkList rdConDecl ntcon `thenUgn` \ cons ->
498 wlkDerivings ntderivs `thenUgn` \ derivings ->
499 returnUgn (RdrHsDecl (TyClD (TyData NewType ctxt tycon tyvars cons
500 derivings noDataPragmas src_loc)))
502 -- "type" declaration
503 U_nbind nbindid nbindas srcline ->
504 mkSrcLocUgn srcline $ \ src_loc ->
505 wlkConAndTyVars nbindid `thenUgn` \ (tycon, tyvars) ->
506 wlkHsType nbindas `thenUgn` \ expansion ->
507 returnUgn (RdrHsDecl (TyClD (TySynonym tycon tyvars expansion src_loc)))
510 U_fbind fbindm srcline ->
511 mkSrcLocUgn srcline $ \ src_loc ->
512 wlkList rdMatch fbindm `thenUgn` \ matches ->
513 returnUgn (RdrValBinding (mkRdrFunctionBinding matches src_loc))
516 U_pbind pbindl pbindr srcline ->
517 mkSrcLocUgn srcline $ \ src_loc ->
518 rdPat pbindl `thenUgn` \ pat ->
519 rdGRHSs pbindr `thenUgn` \ grhss ->
520 returnUgn (RdrValBinding (PatMonoBind pat grhss src_loc))
522 -- "class" declaration
523 U_cbind cbindc cbindid cbindw srcline ->
524 mkSrcLocUgn srcline $ \ src_loc ->
525 wlkContext cbindc `thenUgn` \ ctxt ->
526 wlkConAndTyVars cbindid `thenUgn` \ (clas, tyvars) ->
527 wlkBinding cbindw `thenUgn` \ binding ->
528 getSrcFileUgn `thenUgn` \ sf ->
530 (final_methods, final_sigs) = cvMonoBindsAndSigs sf cvClassOpSig binding
532 returnUgn (RdrHsDecl (TyClD (mkClassDecl ctxt clas tyvars final_sigs
533 final_methods noClassPragmas src_loc)))
535 -- "instance" declaration
536 U_ibind ty ibindw srcline ->
537 -- The "ty" contains the instance context too
538 -- So for "instance Eq a => Eq [a]" the type will be
540 mkSrcLocUgn srcline $ \ src_loc ->
541 wlkInstType ty `thenUgn` \ inst_ty ->
542 wlkBinding ibindw `thenUgn` \ binding ->
543 getSrcFileUgn `thenUgn` \ sf ->
545 (binds,uprags) = cvMonoBindsAndSigs sf cvInstDeclSig binding
547 returnUgn (RdrHsDecl (InstD (InstDecl inst_ty binds uprags
548 dummyRdrVarName {- No dfun id yet -}
551 -- "default" declaration
552 U_dbind dbindts srcline ->
553 mkSrcLocUgn srcline $ \ src_loc ->
554 wlkList rdMonoType dbindts `thenUgn` \ tys ->
555 returnUgn (RdrHsDecl (DefD (DefaultDecl tys src_loc)))
557 -- "foreign" declaration
558 U_fobind id ty ext_name unsafe_flag cconv imp_exp srcline ->
559 mkSrcLocUgn srcline $ \ src_loc ->
560 wlkVarId id `thenUgn` \ h_id ->
561 wlkHsSigType ty `thenUgn` \ h_ty ->
562 wlkExtName ext_name `thenUgn` \ h_ext_name ->
563 rdCallConv cconv `thenUgn` \ h_cconv ->
564 rdForKind imp_exp (cvFlag unsafe_flag) `thenUgn` \ h_imp_exp ->
565 returnUgn (RdrHsDecl (ForD (ForeignDecl h_id h_imp_exp h_ty h_ext_name h_cconv src_loc)))
567 U_sbind sbindids sbindid srcline ->
569 mkSrcLocUgn srcline $ \ src_loc ->
570 wlkList rdVarId sbindids `thenUgn` \ vars ->
571 wlkHsSigType sbindid `thenUgn` \ poly_ty ->
572 returnUgn (foldr1 RdrAndBindings [RdrSig (Sig var poly_ty src_loc) | var <- vars])
574 U_vspec_uprag uvar vspec_tys srcline ->
575 -- value specialisation user-pragma
576 mkSrcLocUgn srcline $ \ src_loc ->
577 wlkVarId uvar `thenUgn` \ var ->
578 wlkList rdHsSigType vspec_tys `thenUgn` \ tys ->
579 returnUgn (foldr1 RdrAndBindings [ RdrSig (SpecSig var ty src_loc)
582 U_ispec_uprag ispec_ty srcline ->
583 -- instance specialisation user-pragma
584 mkSrcLocUgn srcline $ \ src_loc ->
585 wlkInstType ispec_ty `thenUgn` \ ty ->
586 returnUgn (RdrSig (SpecInstSig ty src_loc))
588 U_inline_uprag ivar srcline ->
589 -- value inlining user-pragma
590 mkSrcLocUgn srcline $ \ src_loc ->
591 wlkVarId ivar `thenUgn` \ var ->
592 returnUgn (RdrSig (InlineSig var src_loc))
594 U_noinline_uprag ivar srcline ->
596 mkSrcLocUgn srcline $ \ src_loc ->
597 wlkVarId ivar `thenUgn` \ var ->
598 returnUgn (RdrSig (NoInlineSig var src_loc))
600 U_rule_prag name ivars ilhs irhs srcline ->
601 -- Transforamation rule
602 mkSrcLocUgn srcline $ \ src_loc ->
603 wlkList rdRuleBndr ivars `thenUgn` \ vars ->
604 rdExpr ilhs `thenUgn` \ lhs ->
605 rdExpr irhs `thenUgn` \ rhs ->
606 returnUgn (RdrHsDecl (RuleD (RuleDecl name [] vars lhs rhs src_loc)))
608 mkRdrFunctionBinding :: [RdrNameMatch] -> SrcLoc -> RdrNameMonoBinds
609 mkRdrFunctionBinding fun_matches src_loc
610 = FunMonoBind (head fns) (head infs) matches src_loc
612 (fns, infs, matches) = unzip3 (map de_fun_match fun_matches)
614 de_fun_match (Match _ [ConPatIn fn pats] sig grhss) = (fn, False, Match [] pats sig grhss)
615 de_fun_match (Match _ [ConOpPatIn p1 fn _ p2] sig grhss) = (fn, True, Match [] [p1,p2] sig grhss)
618 rdRuleBndr :: ParseTree -> UgnM RdrNameRuleBndr
619 rdRuleBndr pt = rdU_rulevar pt `thenUgn` wlkRuleBndr
621 wlkRuleBndr :: U_rulevar -> UgnM RdrNameRuleBndr
622 wlkRuleBndr (U_prulevar v)
623 = returnUgn (RuleBndr (mkSrcUnqual varName v))
624 wlkRuleBndr (U_prulevarsig v ty)
625 = wlkHsType ty `thenUgn` \ ty' ->
626 returnUgn (RuleBndrSig (mkSrcUnqual varName v) ty')
630 rdGRHSs :: ParseTree -> UgnM RdrNameGRHSs
631 rdGRHSs pt = rdU_grhsb pt `thenUgn` wlkGRHSs
633 wlkGRHSs :: U_grhsb -> UgnM RdrNameGRHSs
634 wlkGRHSs (U_pguards rhss bind)
635 = wlkList rdGdExp rhss `thenUgn` \ gdexps ->
636 wlkLocalBinding bind `thenUgn` \ bind' ->
637 returnUgn (GRHSs gdexps bind' Nothing)
638 wlkGRHSs (U_pnoguards srcline rhs bind)
639 = mkSrcLocUgn srcline $ \ src_loc ->
640 rdExpr rhs `thenUgn` \ rhs' ->
641 wlkLocalBinding bind `thenUgn` \ bind' ->
642 returnUgn (GRHSs (unguardedRHS rhs' src_loc) bind' Nothing)
645 rdGdExp :: ParseTree -> UgnM RdrNameGRHS
646 rdGdExp pt = rdU_gdexp pt `thenUgn` \ (U_pgdexp guards srcline rhs) ->
647 wlkQuals guards `thenUgn` \ guards' ->
648 mkSrcLocUgn srcline $ \ src_loc ->
649 wlkExpr rhs `thenUgn` \ expr' ->
650 returnUgn (GRHS (guards' ++ [ExprStmt expr' src_loc]) src_loc)
654 wlkDerivings :: U_maybe -> UgnM (Maybe [RdrName])
656 wlkDerivings (U_nothing) = returnUgn Nothing
657 wlkDerivings (U_just pt)
658 = rdU_list pt `thenUgn` \ ds ->
659 wlkList rdTCId ds `thenUgn` \ derivs ->
660 returnUgn (Just derivs)
663 %************************************************************************
665 \subsection[wlkTypes]{Reading in types in various forms (and data constructors)}
667 %************************************************************************
670 rdHsSigType :: ParseTree -> UgnM RdrNameHsType
671 rdHsType :: ParseTree -> UgnM RdrNameHsType
672 rdMonoType :: ParseTree -> UgnM RdrNameHsType
674 rdHsSigType pt = rdU_ttype pt `thenUgn` wlkHsSigType
675 rdHsType pt = rdU_ttype pt `thenUgn` wlkHsType
676 rdMonoType pt = rdU_ttype pt `thenUgn` wlkHsType
678 wlkHsConstrArgType ttype
679 -- Used for the argument types of contructors
680 -- Only an implicit quantification point if -fglasgow-exts
681 | opt_GlasgowExts = wlkHsSigType ttype
682 | otherwise = wlkHsType ttype
684 -- wlkHsSigType is used for type signatures: any place there
685 -- should be *implicit* quantification
687 = wlkHsType ttype `thenUgn` \ ty ->
688 -- This is an implicit quantification point, so
689 -- make sure it starts with a ForAll
691 HsForAllTy _ _ _ -> returnUgn ty
692 other -> returnUgn (HsForAllTy Nothing [] ty)
694 wlkHsType :: U_ttype -> UgnM RdrNameHsType
697 U_forall u_tyvars u_theta u_ty -> -- Explicit forall
698 wlkList rdTvId u_tyvars `thenUgn` \ tyvars ->
699 wlkContext u_theta `thenUgn` \ theta ->
700 wlkHsType u_ty `thenUgn` \ ty ->
701 returnUgn (HsForAllTy (Just (map UserTyVar tyvars)) theta ty)
703 U_imp_forall u_theta u_ty -> -- Implicit forall
704 wlkContext u_theta `thenUgn` \ theta ->
705 wlkHsType u_ty `thenUgn` \ ty ->
706 returnUgn (HsForAllTy Nothing theta ty)
708 U_namedtvar tv -> -- type variable
709 wlkTvId tv `thenUgn` \ tyvar ->
710 returnUgn (MonoTyVar tyvar)
712 U_tname tcon -> -- type constructor
713 wlkTcId tcon `thenUgn` \ tycon ->
714 returnUgn (MonoTyVar tycon)
717 wlkHsType t1 `thenUgn` \ ty1 ->
718 wlkHsType t2 `thenUgn` \ ty2 ->
719 returnUgn (MonoTyApp ty1 ty2)
721 U_tllist tlist -> -- list type
722 wlkHsType tlist `thenUgn` \ ty ->
723 returnUgn (MonoListTy ty)
726 wlkList rdMonoType ttuple `thenUgn` \ tys ->
727 returnUgn (MonoTupleTy tys True)
730 wlkList rdMonoType ttuple `thenUgn` \ tys ->
731 returnUgn (MonoTupleTy tys False)
734 wlkHsType tfun `thenUgn` \ ty1 ->
735 wlkHsType targ `thenUgn` \ ty2 ->
736 returnUgn (MonoFunTy ty1 ty2)
740 U_forall u_tyvars u_theta inst_head ->
741 wlkList rdTvId u_tyvars `thenUgn` \ tyvars ->
742 wlkContext u_theta `thenUgn` \ theta ->
743 wlkClsTys inst_head `thenUgn` \ (clas, tys) ->
744 returnUgn (HsForAllTy (Just (map UserTyVar tyvars)) theta (MonoDictTy clas tys))
746 U_imp_forall u_theta inst_head ->
747 wlkContext u_theta `thenUgn` \ theta ->
748 wlkClsTys inst_head `thenUgn` \ (clas, tys) ->
749 returnUgn (HsForAllTy Nothing theta (MonoDictTy clas tys))
751 other -> -- something else
752 wlkClsTys other `thenUgn` \ (clas, tys) ->
753 returnUgn (HsForAllTy Nothing [] (MonoDictTy clas tys))
757 wlkConAndTyVars :: U_ttype -> UgnM (RdrName, [HsTyVar RdrName])
758 wlkConAndTyVars ttype
759 = wlkHsType ttype `thenUgn` \ ty ->
761 split (MonoTyApp fun (MonoTyVar arg)) args = split fun (UserTyVar arg : args)
762 split (MonoTyVar tycon) args = (tycon,args)
763 split other args = pprPanic "ERROR: malformed type: "
766 returnUgn (split ty [])
769 wlkContext :: U_list -> UgnM RdrNameContext
770 rdClsTys :: ParseTree -> UgnM (RdrName, [HsType RdrName])
772 wlkContext list = wlkList rdClsTys list
774 rdClsTys pt = rdU_ttype pt `thenUgn` wlkClsTys
779 go (U_tname tcon) tys = wlkClsId tcon `thenUgn` \ cls ->
782 go (U_tapp t1 t2) tys = wlkHsType t2 `thenUgn` \ ty2 ->
787 rdConDecl :: ParseTree -> UgnM RdrNameConDecl
788 rdConDecl pt = rdU_constr pt `thenUgn` wlkConDecl
790 wlkConDecl :: U_constr -> UgnM RdrNameConDecl
792 wlkConDecl (U_constrex u_tvs ccxt ccdecl)
793 = wlkList rdTvId u_tvs `thenUgn` \ tyvars ->
794 wlkContext ccxt `thenUgn` \ theta ->
795 wlkConDecl ccdecl `thenUgn` \ (ConDecl con _ _ details loc) ->
796 returnUgn (ConDecl con (map UserTyVar tyvars) theta details loc)
798 wlkConDecl (U_constrpre ccon ctys srcline)
799 = mkSrcLocUgn srcline $ \ src_loc ->
800 wlkDataId ccon `thenUgn` \ con ->
801 wlkList rdBangType ctys `thenUgn` \ tys ->
802 returnUgn (ConDecl con [] [] (VanillaCon tys) src_loc)
804 wlkConDecl (U_constrinf cty1 cop cty2 srcline)
805 = mkSrcLocUgn srcline $ \ src_loc ->
806 wlkBangType cty1 `thenUgn` \ ty1 ->
807 wlkDataId cop `thenUgn` \ op ->
808 wlkBangType cty2 `thenUgn` \ ty2 ->
809 returnUgn (ConDecl op [] [] (InfixCon ty1 ty2) src_loc)
811 wlkConDecl (U_constrnew ccon cty mb_lab srcline)
812 = mkSrcLocUgn srcline $ \ src_loc ->
813 wlkDataId ccon `thenUgn` \ con ->
814 wlkHsSigType cty `thenUgn` \ ty ->
815 wlkMaybe rdVarId mb_lab `thenUgn` \ mb_lab ->
816 returnUgn (ConDecl con [] [] (NewCon ty mb_lab) src_loc)
818 wlkConDecl (U_constrrec ccon cfields srcline)
819 = mkSrcLocUgn srcline $ \ src_loc ->
820 wlkDataId ccon `thenUgn` \ con ->
821 wlkList rd_field cfields `thenUgn` \ fields_lists ->
822 returnUgn (ConDecl con [] [] (RecCon fields_lists) src_loc)
824 rd_field :: ParseTree -> UgnM ([RdrName], BangType RdrName)
826 rdU_constr pt `thenUgn` \ (U_field fvars fty) ->
827 wlkList rdVarId fvars `thenUgn` \ vars ->
828 wlkBangType fty `thenUgn` \ ty ->
832 rdBangType pt = rdU_ttype pt `thenUgn` wlkBangType
834 wlkBangType :: U_ttype -> UgnM (BangType RdrName)
836 wlkBangType (U_tbang bty) = wlkHsConstrArgType bty `thenUgn` \ ty ->
837 returnUgn (Banged ty)
838 wlkBangType uty = wlkHsConstrArgType uty `thenUgn` \ ty ->
839 returnUgn (Unbanged ty)
842 %************************************************************************
844 \subsection{Read a ``match''}
846 %************************************************************************
849 rdMatch :: ParseTree -> UgnM RdrNameMatch
850 rdMatch pt = rdU_match pt `thenUgn` wlkMatch
852 wlkMatch :: U_match -> UgnM RdrNameMatch
853 wlkMatch (U_pmatch pats sig grhsb)
854 = wlkList rdPat pats `thenUgn` \ pats' ->
855 wlkMaybe rdHsType sig `thenUgn` \ maybe_ty ->
856 wlkGRHSs grhsb `thenUgn` \ grhss' ->
857 returnUgn (Match [] pats' maybe_ty grhss')
860 %************************************************************************
862 \subsection[rdImport]{Read an import decl}
864 %************************************************************************
867 rdImport :: ParseTree
868 -> UgnM RdrNameImportDecl
871 = rdU_binding pt `thenUgn` \ (U_import imod iqual ias ispec isrc srcline) ->
872 mkSrcLocUgn srcline $ \ src_loc ->
873 wlkMaybe rdU_stringId ias `thenUgn` \ maybe_as ->
874 wlkMaybe rd_spec ispec `thenUgn` \ maybe_spec ->
875 returnUgn (ImportDecl (mkSrcModuleFS imod)
876 (cvImportSource isrc)
878 (case maybe_as of { Just m -> Just (mkSrcModuleFS m); Nothing -> Nothing })
881 rd_spec pt = rdU_either pt `thenUgn` \ spec ->
883 U_left pt -> rdEntities pt `thenUgn` \ ents ->
884 returnUgn (False, ents)
885 U_right pt -> rdEntities pt `thenUgn` \ ents ->
886 returnUgn (True, ents)
888 cvImportSource 0 = ImportByUser -- No pragam
889 cvImportSource 1 = ImportByUserSource -- {-# SOURCE #-}
893 rdEntities pt = rdU_list pt `thenUgn` wlkList rdEntity
895 rdEntity :: ParseTree -> UgnM (IE RdrName)
898 = rdU_entidt pt `thenUgn` \ entity ->
900 U_entid evar -> -- just a value
901 wlkEntId evar `thenUgn` \ var ->
902 returnUgn (IEVar var)
904 U_enttype x -> -- abstract type constructor/class
905 wlkTcClsId x `thenUgn` \ thing ->
906 returnUgn (IEThingAbs thing)
908 U_enttypeall x -> -- non-abstract type constructor/class
909 wlkTcClsId x `thenUgn` \ thing ->
910 returnUgn (IEThingAll thing)
912 U_enttypenamed x ns -> -- non-abstract type constructor/class
913 -- with specified constrs/methods
914 wlkTcClsId x `thenUgn` \ thing ->
915 wlkList rdVarId ns `thenUgn` \ names ->
916 returnUgn (IEThingWith thing names)
918 U_entmod mod -> -- everything provided unqualified by a module
919 returnUgn (IEModuleContents (mkSrcModuleFS mod))
923 %************************************************************************
925 \subsection[rdExtName]{Read an external name}
927 %************************************************************************
930 wlkExtName :: U_maybe -> UgnM ExtName
931 wlkExtName (U_nothing) = returnUgn Dynamic
932 wlkExtName (U_just pt)
933 = rdU_list pt `thenUgn` \ ds ->
934 wlkList rdU_hstring ds `thenUgn` \ ss ->
936 [nm] -> returnUgn (ExtName nm Nothing)
937 [mod,nm] -> returnUgn (ExtName nm (Just mod))
939 rdCallConv :: Int -> UgnM CallConv
941 -- this tracks the #defines in parser/utils.h
943 (-1) -> -- no calling convention specified, use default.
944 returnUgn defaultCallConv
947 rdForKind :: Int -> Bool -> UgnM ForKind
948 rdForKind 0 isUnsafe = -- foreign import
949 returnUgn (FoImport isUnsafe)
950 rdForKind 1 _ = -- foreign export
952 rdForKind 2 _ = -- foreign label
957 %************************************************************************
959 \subsection[ReadPrefix-help]{Help Functions}
961 %************************************************************************
964 wlkList :: (U_VOID_STAR -> UgnM a) -> U_list -> UgnM [a]
966 wlkList wlk_it U_lnil = returnUgn []
968 wlkList wlk_it (U_lcons hd tl)
969 = wlk_it hd `thenUgn` \ hd_it ->
970 wlkList wlk_it tl `thenUgn` \ tl_it ->
971 returnUgn (hd_it : tl_it)
975 wlkMaybe :: (U_VOID_STAR -> UgnM a) -> U_maybe -> UgnM (Maybe a)
977 wlkMaybe wlk_it U_nothing = returnUgn Nothing
978 wlkMaybe wlk_it (U_just x)
979 = wlk_it x `thenUgn` \ it ->
984 wlkTcClsId = wlkQid (\_ -> tcClsName)
985 wlkTcId = wlkQid (\_ -> tcName)
986 wlkClsId = wlkQid (\_ -> clsName)
987 wlkVarId = wlkQid (\occ -> if isLexCon occ
990 wlkDataId = wlkQid (\_ -> dataName)
991 wlkEntId = wlkQid (\occ -> if isLexCon occ
995 wlkQid :: (FAST_STRING -> NameSpace) -> U_qid -> UgnM RdrName
997 -- There are three kinds of qid:
998 -- qualified name (aqual) A.x
999 -- unqualified name (noqual) x
1000 -- special name (gid) [], (), ->, (,,,)
1001 -- The special names always mean "Prelude.whatever"; that's why
1002 -- they are distinct. So if you write "()", it's just as if you
1003 -- had written "Prelude.()".
1004 -- NB: The (qualified) prelude is always in scope, so the renamer will find it.
1006 -- EXCEPT: when we're compiling with -fno-implicit-prelude, in which
1007 -- case we need to unqualify these things. -- SDM.
1009 wlkQid mk_name_space (U_noqual name)
1010 = returnUgn (mkSrcUnqual (mk_name_space name) name)
1011 wlkQid mk_name_space (U_aqual mod name)
1012 = returnUgn (mkSrcQual (mk_name_space name) mod name)
1013 wlkQid mk_name_space (U_gid n name) -- Built in Prelude things
1014 | opt_NoImplicitPrelude
1015 = returnUgn (mkSrcUnqual (mk_name_space name) name)
1017 = returnUgn (mkPreludeQual (mk_name_space name) pRELUDE_Name name)
1020 rdTCId pt = rdU_qid pt `thenUgn` wlkTcId
1021 rdVarId pt = rdU_qid pt `thenUgn` wlkVarId
1023 rdTvId pt = rdU_stringId pt `thenUgn` \ string -> wlkTvId string
1024 wlkTvId string = returnUgn (mkSrcUnqual tvName string)
1026 -- Unqualified variables, used in the 'forall' of a RULE
1027 rdUVarId pt = rdU_stringId pt `thenUgn` \ string -> wlkUVarId string
1028 wlkUVarId string = returnUgn (mkSrcUnqual varName string)
1030 cvFlag :: U_long -> Bool