2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1995
4 \section[ReadPragmas2]{Read pragmatic interface info, including Core}
7 #include "HsVersions.h"
19 IMPORT_Trace -- ToDo: rm (debugging)
24 import AbsPrel ( nilDataCon, readUnfoldingPrimOp, PrimOp(..)
25 IF_ATTACK_PRAGMAS(COMMA tagOf_PrimOp)
26 IF_ATTACK_PRAGMAS(COMMA pprPrimOp)
28 import PrimKind ( guessPrimKind, PrimKind )
30 import BasicLit ( mkMachInt, BasicLit(..) )
31 import HsCore -- ****** NEED TO SEE CONSTRUCTORS ******
32 import HsPragmas -- ****** NEED TO SEE CONSTRUCTORS ******
33 import Id ( mkTupleCon )
34 import IdInfo -- ( UnfoldingGuidance(..) )
35 import Maybes ( Maybe(..) )
40 import ReadPrefix2 ( wlkList, rdConDecl, wlkMonoType )
45 wlkDataPragma :: U_hpragma -> UgnM ProtoNameDataPragmas
49 U_no_pragma -> returnUgn (DataPragmas [] [])
50 U_idata_pragma cs ss ->
51 wlkList rdConDecl cs `thenUgn` \ cons ->
52 wlkList rd_spec ss `thenUgn` \ specs ->
53 returnUgn (DataPragmas cons specs)
56 = rdU_hpragma pt `thenUgn` \ stuff ->
57 case stuff of { U_idata_pragma_4s ss ->
59 wlkList rdMonoTypeMaybe ss `thenUgn` \ specs ->
64 wlkTypePragma :: U_hpragma -> UgnM TypePragmas
68 U_no_pragma -> returnUgn NoTypePragmas
69 U_itype_pragma -> returnUgn AbstractTySynonym
73 wlkClassPragma :: U_hpragma -> UgnM ProtoNameClassPragmas
77 U_no_pragma -> returnUgn NoClassPragmas
78 U_iclas_pragma gens ->
79 wlkList rdGenPragma gens `thenUgn` \ gen_pragmas ->
80 ASSERT(not (null gen_pragmas))
81 returnUgn (SuperDictPragmas gen_pragmas)
85 wlkInstPragma :: U_hpragma -> UgnM (Maybe FAST_STRING, ProtoNameInstancePragmas)
89 U_no_pragma -> returnUgn (Nothing, NoInstancePragmas)
91 U_iinst_simpl_pragma modname dfun_gen ->
92 wlkGenPragma dfun_gen `thenUgn` \ gen_pragmas ->
93 returnUgn (Just modname, SimpleInstancePragma gen_pragmas)
95 U_iinst_const_pragma modname dfun_gen constm_stuff ->
96 wlkGenPragma dfun_gen `thenUgn` \ gen_pragma ->
97 wlkList rd_constm constm_stuff `thenUgn` \ constm_pragmas ->
98 returnUgn (Just modname, ConstantInstancePragma gen_pragma constm_pragmas)
100 U_iinst_spec_pragma modname dfun_gen spec_stuff ->
101 wlkGenPragma dfun_gen `thenUgn` \ gen_pragma ->
102 wlkList rd_spec spec_stuff `thenUgn` \ spec_pragmas ->
103 returnUgn (Just modname, SpecialisedInstancePragma gen_pragma spec_pragmas)
106 = rdU_hpragma pt `thenUgn` \ stuff ->
107 case stuff of { U_iinst_pragma_3s maybe_tys num_dicts gen consts ->
109 wlkList rdMonoTypeMaybe maybe_tys `thenUgn` \ mono_tys_maybe ->
110 wlkGenPragma gen `thenUgn` \ gen_prag ->
111 wlkList rd_constm consts `thenUgn` \ constms ->
114 = if null constms then
115 if null_gen_prag gen_prag
116 then NoInstancePragmas
117 else SimpleInstancePragma gen_prag
118 else -- some constms...
119 ConstantInstancePragma gen_prag constms
121 returnUgn (mono_tys_maybe, num_dicts, inst_prag) }
123 null_gen_prag NoGenPragmas = True
124 null_gen_prag _ = False
127 = rdU_hpragma pt `thenUgn` \ stuff ->
128 case stuff of { U_iname_pragma_pr name gen ->
130 wlkGenPragma gen `thenUgn` \ prag ->
131 returnUgn (name, prag) }
135 rdGenPragma :: ParseTree -> UgnM ProtoNameGenPragmas
137 rdGenPragma pt = rdU_hpragma pt `thenUgn` \ prag -> wlkGenPragma prag
139 wlkGenPragma :: U_hpragma -> UgnM ProtoNameGenPragmas
143 U_no_pragma -> returnUgn NoGenPragmas
145 U_igen_pragma aritee update deforest strct uf speccs ->
146 wlk_arity aritee `thenUgn` \ arity ->
147 wlk_update update `thenUgn` \ upd ->
148 wlk_deforest deforest `thenUgn` \ def ->
149 wlk_strict strct `thenUgn` \ strict ->
150 wlk_unfold uf `thenUgn` \ unfold ->
151 wlkList rd_spec speccs `thenUgn` \ specs ->
152 returnUgn (GenPragmas arity upd def strict unfold specs)
156 U_no_pragma -> returnUgn Nothing
157 U_iarity_pragma arity ->
158 returnUgn (Just arity)
163 U_no_pragma -> returnUgn Nothing
164 U_iupdate_pragma upd_spec ->
165 returnUgn (Just ((read (_UNPK_ upd_spec))::UpdateInfo))
170 U_no_pragma -> returnUgn Don'tDeforest
171 U_ideforest_pragma -> returnUgn DoDeforest
176 U_no_pragma -> returnUgn NoImpUnfolding
178 U_imagic_unfolding_pragma magic ->
179 returnUgn (ImpMagicUnfolding magic)
181 U_iunfolding_pragma guide core ->
182 wlkGuidance guide `thenUgn` \ guidance ->
183 wlkCoreExpr core `thenUgn` \ coresyn ->
184 returnUgn (ImpUnfolding guidance coresyn)
189 U_no_pragma -> returnUgn NoImpStrictness
191 U_istrictness_pragma strict_spec wrkr_stuff ->
192 wlkGenPragma wrkr_stuff `thenUgn` \ wrkr_pragma ->
194 strict_spec_str = _UNPK_ strict_spec
195 (is_bot, ww_strict_info)
196 = if (strict_spec_str == "B")
198 else (False, (read strict_spec_str)::[Demand])
200 returnUgn (ImpStrictness is_bot ww_strict_info wrkr_pragma)
204 = rdU_hpragma pt `thenUgn` \ stuff ->
205 case stuff of { U_itype_pragma_pr maybe_tys num_dicts prag ->
207 wlkList rdMonoTypeMaybe maybe_tys `thenUgn` \ mono_tys_maybe ->
208 wlkGenPragma prag `thenUgn` \ gen_prag ->
209 returnUgn (mono_tys_maybe, num_dicts, gen_prag) }
212 The only tricky case is pragmas on signatures; we have no way of
213 knowing whether it is a @GenPragma@ or a @ClassOp@ pragma. So we read
214 whatever comes, store it in a @RdrTySigPragmas@ structure, and someone
215 will sort it out later.
217 wlkTySigPragmas :: U_hpragma -> UgnM RdrTySigPragmas
219 wlkTySigPragmas pragma
221 U_no_pragma -> returnUgn RdrNoPragma
223 U_iclasop_pragma dsel defm ->
224 wlkGenPragma dsel `thenUgn` \ dsel_pragma ->
225 wlkGenPragma defm `thenUgn` \ defm_pragma ->
226 returnUgn (RdrClassOpPragmas (ClassOpPragmas dsel_pragma defm_pragma))
229 wlkGenPragma other `thenUgn` \ gen_pragmas ->
230 returnUgn (RdrGenPragmas gen_pragmas)
236 U_iunfold_always -> returnUgn UnfoldAlways
238 U_iunfold_if_args num_ty_args num_val_args con_arg_spec size ->
240 con_arg_info = take num_val_args (map cvt (_UNPK_ con_arg_spec))
241 -- if there were 0 args, we want to throw away
242 -- any dummy con_arg_spec stuff...
244 returnUgn (UnfoldIfGoodArgs num_ty_args num_val_args
247 cvt 'C' = True -- want a constructor in this arg position
252 wlkCoreExpr :: U_coresyn -> UgnM ProtoNameUnfoldingCoreExpr
254 wlkCoreExpr core_expr
257 wlkCoreId v `thenUgn` \ var ->
258 returnUgn (UfCoVar var)
261 wlkBasicLit l `thenUgn` \ lit ->
262 returnUgn (UfCoLit lit)
265 wlkCoreId c `thenUgn` \ (BoringUfId con) ->
266 wlkList rdCoreType ts `thenUgn` \ tys ->
267 wlkList rdCoreAtom as `thenUgn` \ vs ->
268 returnUgn (UfCoCon con tys vs)
271 wlk_primop o `thenUgn` \ op ->
272 wlkList rdCoreType ts `thenUgn` \ tys ->
273 wlkList rdCoreAtom as `thenUgn` \ vs ->
275 fixed_vs = case op of { UfOtherOp pop -> fixup pop vs ; _ -> vs }
277 returnUgn (UfCoPrim op tys fixed_vs)
280 -- Question: why did ccall once panic if you looked at the
281 -- maygc flag? Was this just laziness or is it not needed?
282 -- In that case, modify the stuff that writes them to pragmas
283 -- so that it never adds the _GC_ tag. ADR
287 U_co_primop op_str ->
288 returnUgn (UfOtherOp (readUnfoldingPrimOp op_str))
290 U_co_ccall fun_str may_gc a_tys r_ty ->
291 wlkList rdCoreType a_tys `thenUgn` \ arg_tys ->
292 wlkCoreType r_ty `thenUgn` \ res_ty ->
293 returnUgn (UfCCallOp fun_str False (is_T_or_F may_gc) arg_tys res_ty)
295 U_co_casm litlit may_gc a_tys r_ty ->
296 wlkBasicLit litlit `thenUgn` \ (MachLitLit casm_str _) ->
297 wlkList rdCoreType a_tys `thenUgn` \ arg_tys ->
298 wlkCoreType r_ty `thenUgn` \ res_ty ->
299 returnUgn (UfCCallOp casm_str True (is_T_or_F may_gc) arg_tys res_ty)
304 -- Now *this* is a hack: we can't distinguish Int# literals
305 -- from Word# literals as they come in; this is only likely
306 -- to bite on the args of certain PrimOps (shifts, etc); so
307 -- we look for those and fix things up!!! (WDP 95/05)
309 fixup AndOp [a1, a2] = [fixarg a1, fixarg a2]
310 fixup OrOp [a1, a2] = [fixarg a1, fixarg a2]
311 fixup NotOp [a1] = [fixarg a1]
312 fixup SllOp [a1, a2] = [fixarg a1, a2]
313 fixup SraOp [a1, a2] = [fixarg a1, a2]
314 fixup SrlOp [a1, a2] = [fixarg a1, a2]
315 fixup WordGtOp [a1, a2] = [fixarg a1, fixarg a2]
316 fixup WordGeOp [a1, a2] = [fixarg a1, fixarg a2]
317 fixup WordLtOp [a1, a2] = [fixarg a1, fixarg a2]
318 fixup WordLeOp [a1, a2] = [fixarg a1, fixarg a2]
319 fixup WordEqOp [a1, a2] = [fixarg a1, fixarg a2]
320 fixup WordNeOp [a1, a2] = [fixarg a1, fixarg a2]
323 fixarg (UfCoLitAtom (MachInt i _)) = UfCoLitAtom (MachInt i False{-unsigned-})
327 wlkList rdCoreBinder vars `thenUgn` \ bs ->
328 wlkCoreExpr expr `thenUgn` \ body ->
329 returnUgn (UfCoLam bs body)
331 U_cotylam vars expr ->
332 wlkList rdU_unkId vars `thenUgn` \ tvs ->
333 wlkCoreExpr expr `thenUgn` \ body ->
334 returnUgn (foldr UfCoTyLam body tvs)
337 wlkCoreExpr f `thenUgn` \ fun ->
338 wlkList rdCoreAtom as `thenUgn` \ args ->
339 returnUgn (foldl UfCoApp fun args)
342 wlkCoreExpr e `thenUgn` \ expr ->
343 wlkCoreType t `thenUgn` \ ty ->
344 returnUgn (UfCoTyApp expr ty)
347 wlkCoreExpr s `thenUgn` \ scrut ->
348 wlk_alts as `thenUgn` \ alts ->
349 returnUgn (UfCoCase scrut alts)
351 wlk_alts (U_coalg_alts as d)
352 = wlkList rd_alg_alt as `thenUgn` \ alts ->
353 wlk_deflt d `thenUgn` \ deflt ->
354 returnUgn (UfCoAlgAlts alts deflt)
357 = rdU_coresyn pt `thenUgn` \ (U_coalg_alt c bs exp) ->
359 wlkCoreId c `thenUgn` \ (BoringUfId con) ->
360 wlkList rdCoreBinder bs `thenUgn` \ params ->
361 wlkCoreExpr exp `thenUgn` \ rhs ->
362 returnUgn (con, params, rhs)
364 wlk_alts (U_coprim_alts as d)
365 = wlkList rd_prim_alt as `thenUgn` \ alts ->
366 wlk_deflt d `thenUgn` \ deflt ->
367 returnUgn (UfCoPrimAlts alts deflt)
370 = rdU_coresyn pt `thenUgn` \ (U_coprim_alt l exp) ->
372 wlkBasicLit l `thenUgn` \ lit ->
373 wlkCoreExpr exp `thenUgn` \ rhs ->
376 wlk_deflt U_conodeflt = returnUgn UfCoNoDefault
377 wlk_deflt (U_cobinddeflt v exp)
378 = wlkCoreBinder v `thenUgn` \ b ->
379 wlkCoreExpr exp `thenUgn` \ rhs ->
380 returnUgn (UfCoBindDefault b rhs)
383 wlk_bind b `thenUgn` \ bind ->
384 wlkCoreExpr expr `thenUgn` \ body ->
385 returnUgn (UfCoLet bind body)
387 wlk_bind (U_cononrec v expr)
388 = wlkCoreBinder v `thenUgn` \ b ->
389 wlkCoreExpr expr `thenUgn` \ rhs ->
390 returnUgn (UfCoNonRec b rhs)
392 wlk_bind (U_corec prs)
393 = wlkList rd_pair prs `thenUgn` \ pairs ->
394 returnUgn (UfCoRec pairs)
397 = rdU_coresyn pt `thenUgn` \ (U_corec_pair v expr) ->
399 wlkCoreBinder v `thenUgn` \ b ->
400 wlkCoreExpr expr `thenUgn` \ rhs ->
404 wlk_cc c `thenUgn` \ cc ->
405 wlkCoreExpr expr `thenUgn` \ body ->
406 returnUgn (UfCoSCC cc body)
408 wlk_cc (U_co_preludedictscc dupd)
409 = wlk_dupd dupd `thenUgn` \ is_dupd ->
410 returnUgn (UfPreludeDictsCC is_dupd)
412 wlk_cc (U_co_alldictscc m g dupd)
413 = wlk_dupd dupd `thenUgn` \ is_dupd ->
414 returnUgn (UfAllDictsCC m g is_dupd)
416 wlk_cc (U_co_usercc n m g dupd cafd)
417 = wlk_dupd dupd `thenUgn` \ is_dupd ->
418 wlk_cafd cafd `thenUgn` \ is_cafd ->
419 returnUgn (UfUserCC n m g is_dupd is_cafd)
421 wlk_cc (U_co_autocc id m g dupd cafd)
422 = wlkCoreId id `thenUgn` \ i ->
423 wlk_dupd dupd `thenUgn` \ is_dupd ->
424 wlk_cafd cafd `thenUgn` \ is_cafd ->
425 returnUgn (UfAutoCC i m g is_dupd is_cafd)
427 wlk_cc (U_co_dictcc id m g dupd cafd)
428 = wlkCoreId id `thenUgn` \ i ->
429 wlk_dupd dupd `thenUgn` \ is_dupd ->
430 wlk_cafd cafd `thenUgn` \ is_cafd ->
431 returnUgn (UfDictCC i m g is_dupd is_cafd)
434 wlk_cafd U_co_scc_noncaf = returnUgn False
435 wlk_cafd U_co_scc_caf = returnUgn True
437 wlk_dupd U_co_scc_nondupd = returnUgn False
438 wlk_dupd U_co_scc_dupd = returnUgn True
442 type ProtoUfBinder = (ProtoName, PolyType ProtoName)
444 rdCoreBinder :: ParseTree -> UgnM ProtoUfBinder
446 rdCoreBinder pt = rdU_coresyn pt `thenUgn` \ x -> wlkCoreBinder x
448 wlkCoreBinder :: U_coresyn -> UgnM ProtoUfBinder
450 wlkCoreBinder (U_cobinder b t)
451 = wlkCoreType t `thenUgn` \ ty ->
455 = rdU_coresyn pt `thenUgn` \ atom ->
458 wlkBasicLit l `thenUgn` \ lit ->
459 returnUgn (UfCoLitAtom lit)
462 wlkCoreId var `thenUgn` \ v ->
463 returnUgn (UfCoVarAtom v)
467 rdCoreType :: ParseTree -> UgnM ProtoNamePolyType
469 rdCoreType pt = rdU_ttype pt `thenUgn` \ ttype -> wlkCoreType ttype
471 wlkCoreType :: U_ttype -> UgnM ProtoNamePolyType
473 wlkCoreType (U_uniforall ts t)
474 = wlkList rdU_unkId ts `thenUgn` \ tvs ->
475 wlkMonoType t `thenUgn` \ ty ->
476 returnUgn (ForAllTy tvs ty)
479 = wlkMonoType other `thenUgn` \ ty ->
480 returnUgn (UnoverloadedTy ty)
485 wlkCoreTypeMaybe :: ParseTree -> RETN_TYPE(Maybe ProtoNamePolyType, FAST_STRING)
487 wlkCoreTypeMaybe ('2' : 'D' : xs) = RETN(Nothing, xs)
488 wlkCoreTypeMaybe ('2' : 'E' : xs)
489 = wlkCoreType xs) `thenUgn` \ (ty, xs1) ->
495 = rdU_ttype pt `thenUgn` \ ty ->
497 U_ty_maybe_nothing -> returnUgn Nothing
500 wlkMonoType t `thenUgn` \ mono_ty ->
501 returnUgn (Just mono_ty)
505 wlkCoreId :: U_coresyn -> UgnM (UfId ProtoName)
507 wlkCoreId (U_co_id v)
508 = returnUgn (BoringUfId (cvt_IdString v))
510 wlkCoreId (U_co_orig_id mod nm)
511 = returnUgn (BoringUfId (Imp mod nm [mod]{-dubious, but doesn't matter-} nm))
513 wlkCoreId (U_co_sdselid clas super_clas)
514 = returnUgn (SuperDictSelUfId clas super_clas)
516 wlkCoreId (U_co_classopid clas method)
517 = returnUgn (ClassOpUfId clas method)
519 wlkCoreId (U_co_defmid clas method)
520 = returnUgn (DefaultMethodUfId clas method)
522 wlkCoreId (U_co_dfunid clas t)
523 = wlkCoreType t `thenUgn` \ ty ->
524 returnUgn (DictFunUfId clas ty)
526 wlkCoreId (U_co_constmid clas op t)
527 = wlkCoreType t `thenUgn` \ ty ->
528 returnUgn (ConstMethodUfId clas op ty)
530 wlkCoreId (U_co_specid id tys)
531 = wlkCoreId id `thenUgn` \ unspec ->
532 wlkList rdMonoTypeMaybe tys `thenUgn` \ ty_maybes ->
533 returnUgn (SpecUfId unspec ty_maybes)
535 wlkCoreId (U_co_wrkrid un)
536 = wlkCoreId un `thenUgn` \ unwrkr ->
537 returnUgn (WorkerUfId unwrkr)
540 cvt_IdString :: FAST_STRING -> ProtoName
543 = if (_HEAD_ s /= '_') then
544 -- trace (show s++(show (_HEAD_ s /= '_'))++(_HEAD_ s):'_':"/*0*/\n") (
547 else if (sub_s == SLIT("NIL_")) then
548 -- trace (show s++"/*1*/"++show sub_s++"/"++show (_SUBSTR_ s 5 99999)++"\n") (
549 Prel (WiredInVal nilDataCon)
551 else if (sub_s == SLIT("TUP_")) then
552 -- trace (show s++"/*2*/"++show sub_s++"/"++show (_SUBSTR_ s 5 99999)++"\n") (
553 Prel (WiredInVal (mkTupleCon arity))
556 -- trace (show s++"/*3*/"++show sub_s++"/"++show (_SUBSTR_ s 5 99999)++"\n") (
561 sub_s = _SUBSTR_ s 1 4 -- chars 1--4 (0-origin)
562 arity = read (_UNPK_ (_SUBSTR_ s 5 999999))
563 -- chars 5 onwards give the arity
567 wlkBasicLit :: U_literal -> UgnM BasicLit
569 wlkBasicLit (U_norepr n d)
571 num = ((read (_UNPK_ n)) :: Integer)
572 den = ((read (_UNPK_ d)) :: Integer)
574 returnUgn (NoRepRational (num % den))
579 U_intprim s -> mkMachInt (as_integer s)
580 U_doubleprim s -> MachDouble (as_rational s)
581 U_floatprim s -> MachFloat (as_rational s)
582 U_charprim s -> MachChar (as_char s)
583 U_stringprim s -> MachStr (as_string s)
585 U_clitlit s k -> MachLitLit (as_string s) (guessPrimKind (_UNPK_ k))
587 U_norepi s -> NoRepInteger (as_integer s)
588 U_noreps s -> NoRepStr (as_string s)
592 as_integer s = readInteger (_UNPK_ s)
593 as_rational s = _readRational (_UNPK_ s) -- non-std