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 #if __GLASGOW_HASKELL__ == 201
15 #elif __GLASGOW_HASKELL__ >= 202
21 import UgenAll -- all Yacc parser gumpff...
22 import PrefixSyn -- and various syntaxen.
24 import HsTypes ( HsTyVar(..) )
25 import HsPragmas ( noDataPragmas, noClassPragmas, noInstancePragmas, noGenPragmas )
27 import BasicTypes ( Fixity(..), FixityDirection(..), NewOrData(..) )
30 import CmdLineOpts ( opt_PprUserLength )
31 import ErrUtils ( addErrLoc, ghcExit )
32 import FiniteMap ( elemFM, FiniteMap )
33 import Name ( OccName(..), SYN_IE(Module) )
34 import Lex ( isLexConId )
35 import Outputable ( Outputable(..), PprStyle(..) )
38 import SrcLoc ( mkGeneratedSrcLoc, noSrcLoc, SrcLoc )
39 import Util ( nOfThem, pprError, panic )
42 %************************************************************************
44 \subsection[ReadPrefix-help]{Help Functions}
46 %************************************************************************
49 wlkList :: (U_VOID_STAR -> UgnM a) -> U_list -> UgnM [a]
51 wlkList wlk_it U_lnil = returnUgn []
53 wlkList wlk_it (U_lcons hd tl)
54 = wlk_it hd `thenUgn` \ hd_it ->
55 wlkList wlk_it tl `thenUgn` \ tl_it ->
56 returnUgn (hd_it : tl_it)
60 wlkMaybe :: (U_VOID_STAR -> UgnM a) -> U_maybe -> UgnM (Maybe a)
62 wlkMaybe wlk_it U_nothing = returnUgn Nothing
63 wlkMaybe wlk_it (U_just x)
64 = wlk_it x `thenUgn` \ it ->
69 wlkTvId = wlkQid TvOcc
70 wlkTCId = wlkQid TCOcc
71 wlkVarId = wlkQid VarOcc
72 wlkDataId = wlkQid VarOcc
73 wlkEntId = wlkQid (\occ -> if isLexConId occ
77 wlkQid :: (FAST_STRING -> OccName) -> U_qid -> UgnM RdrName
78 wlkQid mk_occ_name (U_noqual name)
79 = returnUgn (Unqual (mk_occ_name name))
80 wlkQid mk_occ_name (U_aqual mod name)
81 = returnUgn (Qual mod (mk_occ_name name))
83 -- I don't understand this one! It is what shows up when we meet (), [], or (,,,).
84 wlkQid mk_occ_name (U_gid n name)
85 = returnUgn (Unqual (mk_occ_name name))
87 rdTCId pt = rdU_qid pt `thenUgn` \ qid -> wlkTCId qid
88 rdVarId pt = rdU_qid pt `thenUgn` \ qid -> wlkVarId qid
90 cvFlag :: U_long -> Bool
95 %************************************************************************
97 \subsection[rdModule]{@rdModule@: reads in a Haskell module}
99 %************************************************************************
102 #if __GLASGOW_HASKELL__ == 201
103 # define PACK_STR packCString
104 #elif __GLASGOW_HASKELL__ >= 202
105 # define PACK_STR mkFastCharString
107 # define PACK_STR mkFastCharString
110 rdModule :: IO (Module, -- this module's name
111 RdrNameHsModule) -- the main goods
114 = _ccall_ hspmain `CCALL_THEN` \ pt -> -- call the Yacc parser!
116 srcfile = PACK_STR ``input_filename'' -- What A Great Hack! (TM)
119 rdU_tree pt `thenUgn` \ (U_hmodule modname himplist hexplist hfixlist
120 hmodlist srciface_version srcline) ->
122 setSrcFileUgn srcfile $
123 setSrcModUgn modname $
124 mkSrcLocUgn srcline $ \ src_loc ->
126 wlkMaybe rdEntities hexplist `thenUgn` \ exports ->
127 wlkList rdImport himplist `thenUgn` \ imports ->
128 wlkList rdFixOp hfixlist `thenUgn` \ fixities ->
129 wlkBinding hmodlist `thenUgn` \ binding ->
132 val_decl = ValD (cvBinds srcfile cvValSig binding)
133 other_decls = cvOtherDecls binding
137 (case srciface_version of { 0 -> Nothing; n -> Just n })
141 (val_decl: other_decls)
146 %************************************************************************
148 \subsection[wlkExprOrPat]{@wlkExpr@ and @wlkPat@}
150 %************************************************************************
153 rdExpr :: ParseTree -> UgnM RdrNameHsExpr
154 rdPat :: ParseTree -> UgnM RdrNamePat
156 rdExpr pt = rdU_tree pt `thenUgn` \ tree -> wlkExpr tree
157 rdPat pt = rdU_tree pt `thenUgn` \ tree -> wlkPat tree
159 wlkExpr :: U_tree -> UgnM RdrNameHsExpr
160 wlkPat :: U_tree -> UgnM RdrNamePat
164 U_par pexpr -> -- parenthesised expr
165 wlkExpr pexpr `thenUgn` \ expr ->
166 returnUgn (HsPar expr)
168 U_lsection lsexp lop -> -- left section
169 wlkExpr lsexp `thenUgn` \ expr ->
170 wlkVarId lop `thenUgn` \ op ->
171 returnUgn (SectionL expr (HsVar op))
173 U_rsection rop rsexp -> -- right section
174 wlkVarId rop `thenUgn` \ op ->
175 wlkExpr rsexp `thenUgn` \ expr ->
176 returnUgn (SectionR (HsVar op) expr)
178 U_ccall fun flavor ccargs -> -- ccall/casm
179 wlkList rdExpr ccargs `thenUgn` \ args ->
183 returnUgn (CCall fun args
184 (tag == 'p' || tag == 'P') -- may invoke GC
185 (tag == 'N' || tag == 'P') -- really a "casm"
186 (panic "CCall:result_ty"))
188 U_scc label sccexp -> -- scc (set-cost-centre) expression
189 wlkExpr sccexp `thenUgn` \ expr ->
190 returnUgn (HsSCC label expr)
192 U_lambda lampats lamexpr srcline -> -- lambda expression
193 mkSrcLocUgn srcline $ \ src_loc ->
194 wlkList rdPat lampats `thenUgn` \ pats ->
195 wlkExpr lamexpr `thenUgn` \ body ->
197 HsLam (foldr PatMatch
198 (GRHSMatch (GRHSsAndBindsIn
199 [OtherwiseGRHS body src_loc]
204 U_casee caseexpr casebody srcline -> -- case expression
205 mkSrcLocUgn srcline $ \ src_loc ->
206 wlkExpr caseexpr `thenUgn` \ expr ->
207 wlkList rdMatch casebody `thenUgn` \ mats ->
208 getSrcFileUgn `thenUgn` \ sf ->
210 matches = cvMatches sf True mats
212 returnUgn (HsCase expr matches src_loc)
214 U_ife ifpred ifthen ifelse srcline -> -- if expression
215 mkSrcLocUgn srcline $ \ src_loc ->
216 wlkExpr ifpred `thenUgn` \ e1 ->
217 wlkExpr ifthen `thenUgn` \ e2 ->
218 wlkExpr ifelse `thenUgn` \ e3 ->
219 returnUgn (HsIf e1 e2 e3 src_loc)
221 U_let letvdefs letvexpr -> -- let expression
222 wlkBinding letvdefs `thenUgn` \ binding ->
223 wlkExpr letvexpr `thenUgn` \ expr ->
224 getSrcFileUgn `thenUgn` \ sf ->
226 binds = cvBinds sf cvValSig binding
228 returnUgn (HsLet binds expr)
230 U_doe gdo srcline -> -- do expression
231 mkSrcLocUgn srcline $ \ src_loc ->
232 wlkList rd_stmt gdo `thenUgn` \ stmts ->
233 returnUgn (HsDo DoStmt stmts src_loc)
236 = rdU_tree pt `thenUgn` \ bind ->
238 U_doexp exp srcline ->
239 mkSrcLocUgn srcline $ \ src_loc ->
240 wlkExpr exp `thenUgn` \ expr ->
241 returnUgn (ExprStmt expr src_loc)
243 U_dobind pat exp srcline ->
244 mkSrcLocUgn srcline $ \ src_loc ->
245 wlkPat pat `thenUgn` \ patt ->
246 wlkExpr exp `thenUgn` \ expr ->
247 returnUgn (BindStmt patt expr src_loc)
250 wlkBinding seqlet `thenUgn` \ bs ->
251 getSrcFileUgn `thenUgn` \ sf ->
253 binds = cvBinds sf cvValSig bs
255 returnUgn (LetStmt binds)
257 U_comprh cexp cquals -> -- list comprehension
258 wlkExpr cexp `thenUgn` \ expr ->
259 wlkQuals cquals `thenUgn` \ quals ->
260 getSrcLocUgn `thenUgn` \ loc ->
261 returnUgn (HsDo ListComp (quals ++ [ReturnStmt expr]) loc)
263 U_eenum efrom estep eto -> -- arithmetic sequence
264 wlkExpr efrom `thenUgn` \ e1 ->
265 wlkMaybe rdExpr estep `thenUgn` \ es2 ->
266 wlkMaybe rdExpr eto `thenUgn` \ es3 ->
267 returnUgn (cv_arith_seq e1 es2 es3)
269 cv_arith_seq e1 Nothing Nothing = ArithSeqIn (From e1)
270 cv_arith_seq e1 Nothing (Just e3) = ArithSeqIn (FromTo e1 e3)
271 cv_arith_seq e1 (Just e2) Nothing = ArithSeqIn (FromThen e1 e2)
272 cv_arith_seq e1 (Just e2) (Just e3) = ArithSeqIn (FromThenTo e1 e2 e3)
274 U_restr restre restrt -> -- expression with type signature
275 wlkExpr restre `thenUgn` \ expr ->
276 wlkHsType restrt `thenUgn` \ ty ->
277 returnUgn (ExprWithTySig expr ty)
279 --------------------------------------------------------------
280 -- now the prefix items that can either be an expression or
281 -- pattern, except we know they are *expressions* here
282 -- (this code could be commoned up with the pattern version;
283 -- but it probably isn't worth it)
284 --------------------------------------------------------------
286 wlkLiteral lit `thenUgn` \ lit ->
287 returnUgn (HsLit lit)
289 U_ident n -> -- simple identifier
290 wlkVarId n `thenUgn` \ var ->
291 returnUgn (HsVar var)
293 U_ap fun arg -> -- application
294 wlkExpr fun `thenUgn` \ expr1 ->
295 wlkExpr arg `thenUgn` \ expr2 ->
296 returnUgn (HsApp expr1 expr2)
298 U_infixap fun arg1 arg2 -> -- infix application
299 wlkVarId fun `thenUgn` \ op ->
300 wlkExpr arg1 `thenUgn` \ expr1 ->
301 wlkExpr arg2 `thenUgn` \ expr2 ->
302 returnUgn (mkOpApp expr1 op expr2)
304 U_negate nexp -> -- prefix negation
305 wlkExpr nexp `thenUgn` \ expr ->
306 returnUgn (NegApp expr (HsVar dummyRdrVarName))
308 U_llist llist -> -- explicit list
309 wlkList rdExpr llist `thenUgn` \ exprs ->
310 returnUgn (ExplicitList exprs)
312 U_tuple tuplelist -> -- explicit tuple
313 wlkList rdExpr tuplelist `thenUgn` \ exprs ->
314 returnUgn (ExplicitTuple exprs)
316 U_record con rbinds -> -- record construction
317 wlkDataId con `thenUgn` \ rcon ->
318 wlkList rdRbind rbinds `thenUgn` \ recbinds ->
319 returnUgn (RecordCon (HsVar rcon) recbinds)
321 U_rupdate updexp updbinds -> -- record update
322 wlkExpr updexp `thenUgn` \ aexp ->
323 wlkList rdRbind updbinds `thenUgn` \ recbinds ->
324 returnUgn (RecordUpd aexp recbinds)
327 U_hmodule _ _ _ _ _ _ _ -> error "U_hmodule"
328 U_as _ _ -> error "U_as"
329 U_lazyp _ -> error "U_lazyp"
330 U_wildp -> error "U_wildp"
331 U_qual _ _ -> error "U_qual"
332 U_guard _ -> error "U_guard"
333 U_seqlet _ -> error "U_seqlet"
334 U_dobind _ _ _ -> error "U_dobind"
335 U_doexp _ _ -> error "U_doexp"
336 U_rbind _ _ -> error "U_rbind"
337 U_fixop _ _ _ -> error "U_fixop"
341 = rdU_tree pt `thenUgn` \ (U_rbind var exp) ->
342 wlkVarId var `thenUgn` \ rvar ->
343 wlkMaybe rdExpr exp `thenUgn` \ expr_maybe ->
346 Nothing -> (rvar, HsVar rvar, True{-pun-})
347 Just re -> (rvar, re, False)
351 = wlkList rd_qual cquals
354 = rdU_tree pt `thenUgn` \ qual ->
360 wlkExpr exp `thenUgn` \ expr ->
361 getSrcLocUgn `thenUgn` \ loc ->
362 returnUgn (GuardStmt expr loc)
365 wlkPat qpat `thenUgn` \ pat ->
366 wlkExpr qexp `thenUgn` \ expr ->
367 getSrcLocUgn `thenUgn` \ loc ->
368 returnUgn (BindStmt pat expr loc)
371 wlkBinding seqlet `thenUgn` \ bs ->
372 getSrcFileUgn `thenUgn` \ sf ->
374 binds = cvBinds sf cvValSig bs
376 returnUgn (LetStmt binds)
379 Patterns: just bear in mind that lists of patterns are represented as
380 a series of ``applications''.
384 U_par ppat -> -- parenthesised pattern
385 wlkPat ppat `thenUgn` \ pat ->
386 -- tidy things up a little:
391 other -> ParPatIn pat
394 U_as avar as_pat -> -- "as" pattern
395 wlkVarId avar `thenUgn` \ var ->
396 wlkPat as_pat `thenUgn` \ pat ->
397 returnUgn (AsPatIn var pat)
399 U_lazyp lazyp -> -- irrefutable ("twiddle") pattern
400 wlkPat lazyp `thenUgn` \ pat ->
401 returnUgn (LazyPatIn pat)
404 wlkVarId avar `thenUgn` \ var ->
405 wlkLiteral lit `thenUgn` \ lit ->
406 returnUgn (NPlusKPatIn var lit)
408 U_wildp -> returnUgn WildPatIn -- wildcard pattern
410 U_lit lit -> -- literal pattern
411 wlkLiteral lit `thenUgn` \ lit ->
412 returnUgn (LitPatIn lit)
414 U_ident nn -> -- simple identifier
415 wlkVarId nn `thenUgn` \ n ->
418 VarOcc occ | isLexConId occ -> ConPatIn n []
422 U_ap l r -> -- "application": there's a list of patterns lurking here!
423 wlkPat r `thenUgn` \ rpat ->
424 collect_pats l [rpat] `thenUgn` \ (lpat,lpats) ->
426 VarPatIn x -> returnUgn (x, lpats)
427 ConPatIn x [] -> returnUgn (x, lpats)
428 ConOpPatIn x op _ y -> returnUgn (op, x:y:lpats)
429 _ -> getSrcLocUgn `thenUgn` \ loc ->
431 err = addErrLoc loc "Illegal pattern `application'"
432 (\sty -> hsep (map (ppr sty) (lpat:lpats)))
433 msg = show (err (PprForUser opt_PprUserLength))
435 #if __GLASGOW_HASKELL__ == 201
436 ioToUgnM (GHCbase.ioToPrimIO (hPutStr stderr msg)) `thenUgn` \ _ ->
437 ioToUgnM (GHCbase.ioToPrimIO (ghcExit 1)) `thenUgn` \ _ ->
438 #elif __GLASGOW_HASKELL__ >= 202
439 ioToUgnM (IOBase.ioToPrimIO (hPutStr stderr msg)) `thenUgn` \ _ ->
440 ioToUgnM (IOBase.ioToPrimIO (ghcExit 1)) `thenUgn` \ _ ->
442 ioToUgnM (hPutStr stderr msg) `thenUgn` \ _ ->
443 ioToUgnM (ghcExit 1) `thenUgn` \ _ ->
445 returnUgn (error "ReadPrefix")
447 ) `thenUgn` \ (n, arg_pats) ->
448 returnUgn (ConPatIn n arg_pats)
453 wlkPat r `thenUgn` \ rpat ->
454 collect_pats l (rpat:acc)
456 wlkPat other `thenUgn` \ pat ->
459 U_infixap fun arg1 arg2 -> -- infix pattern
460 wlkVarId fun `thenUgn` \ op ->
461 wlkPat arg1 `thenUgn` \ pat1 ->
462 wlkPat arg2 `thenUgn` \ pat2 ->
463 returnUgn (ConOpPatIn pat1 op (error "ConOpPatIn:fixity") pat2)
465 U_negate npat -> -- negated pattern
466 wlkPat npat `thenUgn` \ pat ->
467 returnUgn (NegPatIn pat)
469 U_llist llist -> -- explicit list
470 wlkList rdPat llist `thenUgn` \ pats ->
471 returnUgn (ListPatIn pats)
473 U_tuple tuplelist -> -- explicit tuple
474 wlkList rdPat tuplelist `thenUgn` \ pats ->
475 returnUgn (TuplePatIn pats)
477 U_record con rpats -> -- record destruction
478 wlkDataId con `thenUgn` \ rcon ->
479 wlkList rdRpat rpats `thenUgn` \ recpats ->
480 returnUgn (RecPatIn rcon recpats)
483 = rdU_tree pt `thenUgn` \ (U_rbind var pat) ->
484 wlkVarId var `thenUgn` \ rvar ->
485 wlkMaybe rdPat pat `thenUgn` \ pat_maybe ->
488 Nothing -> (rvar, VarPatIn rvar, True{-pun-})
489 Just rp -> (rvar, rp, False)
494 wlkLiteral :: U_literal -> UgnM HsLit
499 U_integer s -> HsInt (as_integer s)
500 U_floatr s -> HsFrac (as_rational s)
501 U_intprim s -> HsIntPrim (as_integer s)
502 U_doubleprim s -> HsDoublePrim (as_rational s)
503 U_floatprim s -> HsFloatPrim (as_rational s)
504 U_charr s -> HsChar (as_char s)
505 U_charprim s -> HsCharPrim (as_char s)
506 U_string s -> HsString (as_string s)
507 U_stringprim s -> HsStringPrim (as_string s)
508 U_clitlit s -> HsLitLit (as_string s)
512 as_integer s = readInteger (_UNPK_ s)
513 #if __GLASGOW_HASKELL__ == 201
514 as_rational s = GHCbase.readRational__ (_UNPK_ s) -- non-std
515 #elif __GLASGOW_HASKELL__ >= 202
516 as_rational s = case readRational (_UNPK_ s) of { [(a,_)] -> a } -- ToDo, use non-std readRational__
518 as_rational s = _readRational (_UNPK_ s) -- non-std
523 %************************************************************************
525 \subsection{wlkBinding}
527 %************************************************************************
530 wlkBinding :: U_binding -> UgnM RdrBinding
536 returnUgn RdrNullBind
538 -- "and" binding (just glue, really)
540 wlkBinding a `thenUgn` \ binding1 ->
541 wlkBinding b `thenUgn` \ binding2 ->
542 returnUgn (RdrAndBindings binding1 binding2)
544 -- "data" declaration
545 U_tbind tctxt ttype tcons tderivs srcline ->
546 mkSrcLocUgn srcline $ \ src_loc ->
547 wlkContext tctxt `thenUgn` \ ctxt ->
548 wlkTyConAndTyVars ttype `thenUgn` \ (tycon, tyvars) ->
549 wlkList rdConDecl tcons `thenUgn` \ cons ->
550 wlkDerivings tderivs `thenUgn` \ derivings ->
551 returnUgn (RdrTyDecl (TyData DataType ctxt tycon tyvars cons derivings noDataPragmas src_loc))
553 -- "newtype" declaration
554 U_ntbind ntctxt nttype ntcon ntderivs srcline ->
555 mkSrcLocUgn srcline $ \ src_loc ->
556 wlkContext ntctxt `thenUgn` \ ctxt ->
557 wlkTyConAndTyVars nttype `thenUgn` \ (tycon, tyvars) ->
558 wlkList rdConDecl ntcon `thenUgn` \ cons ->
559 wlkDerivings ntderivs `thenUgn` \ derivings ->
560 returnUgn (RdrTyDecl (TyData NewType ctxt tycon tyvars cons derivings noDataPragmas src_loc))
562 -- "type" declaration
563 U_nbind nbindid nbindas srcline ->
564 mkSrcLocUgn srcline $ \ src_loc ->
565 wlkTyConAndTyVars nbindid `thenUgn` \ (tycon, tyvars) ->
566 wlkMonoType nbindas `thenUgn` \ expansion ->
567 returnUgn (RdrTyDecl (TySynonym tycon tyvars expansion src_loc))
570 U_fbind fbindl srcline ->
571 mkSrcLocUgn srcline $ \ src_loc ->
572 wlkList rdMatch fbindl `thenUgn` \ matches ->
573 returnUgn (RdrFunctionBinding srcline matches)
576 U_pbind pbindl srcline ->
577 mkSrcLocUgn srcline $ \ src_loc ->
578 wlkList rdMatch pbindl `thenUgn` \ matches ->
579 returnUgn (RdrPatternBinding srcline matches)
581 -- "class" declaration
582 U_cbind cbindc cbindid cbindw srcline ->
583 mkSrcLocUgn srcline $ \ src_loc ->
584 wlkContext cbindc `thenUgn` \ ctxt ->
585 wlkClassAssertTy cbindid `thenUgn` \ (clas, tyvar)->
586 wlkBinding cbindw `thenUgn` \ binding ->
587 getSrcFileUgn `thenUgn` \ sf ->
589 (final_methods, final_sigs) = cvMonoBindsAndSigs sf cvClassOpSig binding
591 returnUgn (RdrClassDecl
592 (ClassDecl ctxt clas tyvar final_sigs final_methods noClassPragmas src_loc))
594 -- "instance" declaration
595 U_ibind ibindc iclas ibindi ibindw srcline ->
596 mkSrcLocUgn srcline $ \ src_loc ->
597 wlkContext ibindc `thenUgn` \ ctxt ->
598 wlkTCId iclas `thenUgn` \ clas ->
599 wlkMonoType ibindi `thenUgn` \ at_ty ->
600 wlkBinding ibindw `thenUgn` \ binding ->
601 getSrcModUgn `thenUgn` \ modname ->
602 getSrcFileUgn `thenUgn` \ sf ->
604 (binds,uprags) = cvMonoBindsAndSigs sf cvInstDeclSig binding
605 inst_ty = HsPreForAllTy ctxt (MonoDictTy clas at_ty)
607 returnUgn (RdrInstDecl
608 (InstDecl inst_ty binds uprags Nothing {- No dfun id -} src_loc))
610 -- "default" declaration
611 U_dbind dbindts srcline ->
612 mkSrcLocUgn srcline $ \ src_loc ->
613 wlkList rdMonoType dbindts `thenUgn` \ tys ->
614 returnUgn (RdrDefaultDecl (DefaultDecl tys src_loc))
617 -- signature(-like) things, including user pragmas
618 wlk_sig_thing a_sig_we_hope
622 wlkDerivings :: U_maybe -> UgnM (Maybe [RdrName])
624 wlkDerivings (U_nothing) = returnUgn Nothing
625 wlkDerivings (U_just pt)
626 = rdU_list pt `thenUgn` \ ds ->
627 wlkList rdTCId ds `thenUgn` \ derivs ->
628 returnUgn (Just derivs)
633 wlk_sig_thing (U_sbind sbindids sbindid srcline)
634 = mkSrcLocUgn srcline $ \ src_loc ->
635 wlkList rdVarId sbindids `thenUgn` \ vars ->
636 wlkHsType sbindid `thenUgn` \ poly_ty ->
637 returnUgn (RdrTySig vars poly_ty src_loc)
639 -- value specialisation user-pragma
640 wlk_sig_thing (U_vspec_uprag uvar vspec_tys srcline)
641 = mkSrcLocUgn srcline $ \ src_loc ->
642 wlkVarId uvar `thenUgn` \ var ->
643 wlkList rd_ty_and_id vspec_tys `thenUgn` \ tys_and_ids ->
644 returnUgn (RdrSpecValSig [SpecSig var ty using_id src_loc
645 | (ty, using_id) <- tys_and_ids ])
647 rd_ty_and_id :: ParseTree -> UgnM (RdrNameHsType, Maybe RdrName)
649 = rdU_binding pt `thenUgn` \ (U_vspec_ty_and_id vspec_ty vspec_id) ->
650 wlkHsType vspec_ty `thenUgn` \ ty ->
651 wlkMaybe rdVarId vspec_id `thenUgn` \ id_maybe ->
652 returnUgn(ty, id_maybe)
654 -- instance specialisation user-pragma
655 wlk_sig_thing (U_ispec_uprag iclas ispec_ty srcline)
656 = mkSrcLocUgn srcline $ \ src_loc ->
657 wlkTCId iclas `thenUgn` \ clas ->
658 wlkMonoType ispec_ty `thenUgn` \ ty ->
659 returnUgn (RdrSpecInstSig (SpecInstSig clas ty src_loc))
661 -- data specialisation user-pragma
662 wlk_sig_thing (U_dspec_uprag itycon dspec_tys srcline)
663 = mkSrcLocUgn srcline $ \ src_loc ->
664 wlkTCId itycon `thenUgn` \ tycon ->
665 wlkList rdMonoType dspec_tys `thenUgn` \ tys ->
666 returnUgn (RdrSpecDataSig (SpecDataSig tycon (foldl MonoTyApp (MonoTyVar tycon) tys) src_loc))
668 -- value inlining user-pragma
669 wlk_sig_thing (U_inline_uprag ivar srcline)
670 = mkSrcLocUgn srcline $ \ src_loc ->
671 wlkVarId ivar `thenUgn` \ var ->
672 returnUgn (RdrInlineValSig (InlineSig var src_loc))
674 -- "deforest me" user-pragma
675 wlk_sig_thing (U_deforest_uprag ivar srcline)
676 = mkSrcLocUgn srcline $ \ src_loc ->
677 wlkVarId ivar `thenUgn` \ var ->
678 returnUgn (RdrDeforestSig (DeforestSig var src_loc))
680 -- "magic" unfolding user-pragma
681 wlk_sig_thing (U_magicuf_uprag ivar str srcline)
682 = mkSrcLocUgn srcline $ \ src_loc ->
683 wlkVarId ivar `thenUgn` \ var ->
684 returnUgn (RdrMagicUnfoldingSig (MagicUnfoldingSig var str src_loc))
687 %************************************************************************
689 \subsection[wlkTypes]{Reading in types in various forms (and data constructors)}
691 %************************************************************************
694 rdHsType :: ParseTree -> UgnM RdrNameHsType
695 rdMonoType :: ParseTree -> UgnM RdrNameHsType
697 rdHsType pt = rdU_ttype pt `thenUgn` \ ttype -> wlkHsType ttype
698 rdMonoType pt = rdU_ttype pt `thenUgn` \ ttype -> wlkMonoType ttype
700 wlkHsType :: U_ttype -> UgnM RdrNameHsType
701 wlkMonoType :: U_ttype -> UgnM RdrNameHsType
705 U_context tcontextl tcontextt -> -- context
706 wlkContext tcontextl `thenUgn` \ ctxt ->
707 wlkMonoType tcontextt `thenUgn` \ ty ->
708 returnUgn (HsPreForAllTy ctxt ty)
710 other -> -- something else
711 wlkMonoType other `thenUgn` \ ty ->
712 returnUgn (HsPreForAllTy [{-no context-}] ty)
716 -- Glasgow extension: nested polymorhism
717 U_context tcontextl tcontextt -> -- context
718 wlkContext tcontextl `thenUgn` \ ctxt ->
719 wlkMonoType tcontextt `thenUgn` \ ty ->
720 returnUgn (HsPreForAllTy ctxt ty)
722 U_namedtvar tv -> -- type variable
723 wlkTvId tv `thenUgn` \ tyvar ->
724 returnUgn (MonoTyVar tyvar)
726 U_tname tcon -> -- type constructor
727 wlkTCId tcon `thenUgn` \ tycon ->
728 returnUgn (MonoTyVar tycon)
731 wlkMonoType t1 `thenUgn` \ ty1 ->
732 wlkMonoType t2 `thenUgn` \ ty2 ->
733 returnUgn (MonoTyApp ty1 ty2)
735 U_tllist tlist -> -- list type
736 wlkMonoType tlist `thenUgn` \ ty ->
737 returnUgn (MonoListTy dummyRdrTcName ty)
740 wlkList rdMonoType ttuple `thenUgn` \ tys ->
741 returnUgn (MonoTupleTy dummyRdrTcName tys)
744 wlkMonoType tfun `thenUgn` \ ty1 ->
745 wlkMonoType targ `thenUgn` \ ty2 ->
746 returnUgn (MonoFunTy ty1 ty2)
751 wlkTyConAndTyVars :: U_ttype -> UgnM (RdrName, [HsTyVar RdrName])
752 wlkContext :: U_list -> UgnM RdrNameContext
753 wlkClassAssertTy :: U_ttype -> UgnM (RdrName, HsTyVar RdrName)
755 wlkTyConAndTyVars ttype
756 = wlkMonoType ttype `thenUgn` \ ty ->
758 split (MonoTyApp fun (MonoTyVar arg)) args = split fun (UserTyVar arg : args)
759 split (MonoTyVar tycon) args = (tycon,args)
761 returnUgn (split ty [])
764 = wlkList rdMonoType list `thenUgn` \ tys ->
765 returnUgn (map mk_class_assertion tys)
768 = wlkMonoType xs `thenUgn` \ mono_ty ->
769 returnUgn (case mk_class_assertion mono_ty of
770 (clas, MonoTyVar tyvar) -> (clas, UserTyVar tyvar)
773 mk_class_assertion :: RdrNameHsType -> (RdrName, RdrNameHsType)
775 mk_class_assertion (MonoTyApp (MonoTyVar name) ty@(MonoTyVar tyname)) = (name, ty)
776 mk_class_assertion other
777 = pprError "ERROR: malformed type context: " (ppr (PprForUser opt_PprUserLength) other)
778 -- regrettably, the parser does let some junk past
779 -- e.g., f :: Num {-nothing-} => a -> ...
783 rdConDecl :: ParseTree -> UgnM RdrNameConDecl
785 = rdU_constr pt `thenUgn` \ blah ->
788 wlkConDecl :: U_constr -> UgnM RdrNameConDecl
790 wlkConDecl (U_constrcxt ccxt ccdecl)
791 = wlkContext ccxt `thenUgn` \ theta ->
792 wlkConDecl ccdecl `thenUgn` \ (ConDecl con _ details loc) ->
793 returnUgn (ConDecl con theta details loc)
795 wlkConDecl (U_constrpre ccon ctys srcline)
796 = mkSrcLocUgn srcline $ \ src_loc ->
797 wlkDataId ccon `thenUgn` \ con ->
798 wlkList rdBangType ctys `thenUgn` \ tys ->
799 returnUgn (ConDecl con [] (VanillaCon tys) src_loc)
801 wlkConDecl (U_constrinf cty1 cop cty2 srcline)
802 = mkSrcLocUgn srcline $ \ src_loc ->
803 wlkBangType cty1 `thenUgn` \ ty1 ->
804 wlkDataId cop `thenUgn` \ op ->
805 wlkBangType cty2 `thenUgn` \ ty2 ->
806 returnUgn (ConDecl op [] (InfixCon ty1 ty2) src_loc)
808 wlkConDecl (U_constrnew ccon cty srcline)
809 = mkSrcLocUgn srcline $ \ src_loc ->
810 wlkDataId ccon `thenUgn` \ con ->
811 wlkMonoType cty `thenUgn` \ ty ->
812 returnUgn (ConDecl con [] (NewCon ty) src_loc)
814 wlkConDecl (U_constrrec ccon cfields srcline)
815 = mkSrcLocUgn srcline $ \ src_loc ->
816 wlkDataId ccon `thenUgn` \ con ->
817 wlkList rd_field cfields `thenUgn` \ fields_lists ->
818 returnUgn (ConDecl con [] (RecCon fields_lists) src_loc)
820 rd_field :: ParseTree -> UgnM ([RdrName], BangType RdrName)
822 = rdU_constr pt `thenUgn` \ (U_field fvars fty) ->
823 wlkList rdVarId fvars `thenUgn` \ vars ->
824 wlkBangType fty `thenUgn` \ ty ->
828 rdBangType pt = rdU_ttype pt `thenUgn` \ ty -> wlkBangType ty
830 wlkBangType :: U_ttype -> UgnM (BangType RdrName)
832 wlkBangType (U_tbang bty) = wlkMonoType bty `thenUgn` \ ty ->
833 returnUgn (Banged ty)
834 wlkBangType uty = wlkMonoType uty `thenUgn` \ ty ->
835 returnUgn (Unbanged ty)
838 %************************************************************************
840 \subsection{Read a ``match''}
842 %************************************************************************
845 rdMatch :: ParseTree -> UgnM RdrMatch
848 = rdU_pbinding pt `thenUgn` \ (U_pgrhs gpat gdexprs gbind gsrcfun srcline) ->
849 mkSrcLocUgn srcline $ \ src_loc ->
850 wlkPat gpat `thenUgn` \ pat ->
851 wlkBinding gbind `thenUgn` \ binding ->
852 wlkVarId gsrcfun `thenUgn` \ srcfun ->
854 wlk_guards (U_pnoguards exp)
855 = wlkExpr exp `thenUgn` \ expr ->
856 returnUgn (RdrMatch_NoGuard srcline srcfun pat expr binding)
858 wlk_guards (U_pguards gs)
859 = wlkList rd_gd_expr gs `thenUgn` \ gd_exps ->
860 returnUgn (RdrMatch_Guards srcline srcfun pat gd_exps binding)
865 = rdU_pbinding pt `thenUgn` \ (U_pgdexp g e) ->
866 wlkQuals g `thenUgn` \ guard ->
867 wlkExpr e `thenUgn` \ expr ->
868 returnUgn (guard, expr)
871 %************************************************************************
873 \subsection[rdFixOp]{Read in a fixity declaration}
875 %************************************************************************
878 rdFixOp :: ParseTree -> UgnM RdrNameFixityDecl
880 = rdU_tree pt `thenUgn` \ fix ->
882 U_fixop op dir_n prec -> wlkVarId op `thenUgn` \ op ->
883 returnUgn (FixityDecl op (Fixity prec dir) noSrcLoc)
890 _ -> error "ReadPrefix:rdFixOp"
893 %************************************************************************
895 \subsection[rdImport]{Read an import decl}
897 %************************************************************************
900 rdImport :: ParseTree
901 -> UgnM RdrNameImportDecl
904 = rdU_binding pt `thenUgn` \ (U_import imod iqual ias ispec srcline) ->
905 mkSrcLocUgn srcline $ \ src_loc ->
906 wlkMaybe rdU_stringId ias `thenUgn` \ maybe_as ->
907 wlkMaybe rd_spec ispec `thenUgn` \ maybe_spec ->
908 returnUgn (ImportDecl imod (cvFlag iqual) maybe_as maybe_spec src_loc)
910 rd_spec pt = rdU_either pt `thenUgn` \ spec ->
912 U_left pt -> rdEntities pt `thenUgn` \ ents ->
913 returnUgn (False, ents)
914 U_right pt -> rdEntities pt `thenUgn` \ ents ->
915 returnUgn (True, ents)
920 = rdU_list pt `thenUgn` \ list ->
921 wlkList rdEntity list
923 rdEntity :: ParseTree -> UgnM (IE RdrName)
926 = rdU_entidt pt `thenUgn` \ entity ->
928 U_entid evar -> -- just a value
929 wlkEntId evar `thenUgn` \ var ->
930 returnUgn (IEVar var)
932 U_enttype x -> -- abstract type constructor/class
933 wlkTCId x `thenUgn` \ thing ->
934 returnUgn (IEThingAbs thing)
936 U_enttypeall x -> -- non-abstract type constructor/class
937 wlkTCId x `thenUgn` \ thing ->
938 returnUgn (IEThingAll thing)
940 U_enttypenamed x ns -> -- non-abstract type constructor/class
941 -- with specified constrs/methods
942 wlkTCId x `thenUgn` \ thing ->
943 wlkList rdVarId ns `thenUgn` \ names ->
944 returnUgn (IEThingWith thing names)
946 U_entmod mod -> -- everything provided unqualified by a module
947 returnUgn (IEModuleContents mod)