import CoreSyn
import HscTypes
import TyCon
+import Class
import TypeRep
import Type
import DataCon
import CoreSyn
import Var
import IdInfo
+import Id( idUnfolding )
import Literal
import Name
import CostCentre
import ForeignCall
import PprExternalCore
import CmdLineOpts
+import Maybes( orElse )
import IO
+import FastString
-emitExternalCore :: DynFlags -> ModIface -> ModDetails -> IO ()
-emitExternalCore dflags iface details
+emitExternalCore :: DynFlags -> ModGuts -> IO ()
+emitExternalCore dflags mod_impl
| opt_EmitExternalCore
= (do handle <- openFile corename WriteMode
- hPutStr handle (show (mkExternalCore iface details))
+ hPutStr handle (show (mkExternalCore mod_impl))
hClose handle)
`catch` (\err -> pprPanic "Failed to open or write external core output file"
(text corename))
where corename = extCoreName dflags
-emitExternalCore _ _ _
+emitExternalCore _ _
| otherwise
= return ()
-mkExternalCore :: ModIface -> ModDetails -> C.Module
-mkExternalCore (ModIface {mi_module=mi_module,mi_exports=mi_exports})
- (ModDetails {md_types=md_types,md_binds=md_binds}) =
- C.Module mname tdefs vdefs
+mkExternalCore :: ModGuts -> C.Module
+mkExternalCore (ModGuts {mg_module=this_mod, mg_types = type_env, mg_binds = binds})
+ = C.Module mname tdefs vdefs
where
- mname = make_mid mi_module
- tdefs = foldr collect_tdefs [] (typeEnvTyCons md_types)
- vdefs = map make_vdef md_binds
+ mname = make_mid this_mod
+ tdefs = foldr collect_tdefs [] tycons
+ vdefs = map make_vdef (implicit_binds ++ binds)
+ tycons = map classTyCon (typeEnvClasses type_env) ++ typeEnvTyCons type_env
+
+ -- Don't forget to include the implicit bindings!
+ implicit_binds = map get_defn (concatMap implicit_ids (typeEnvElts type_env))
+
+implicit_ids :: TyThing -> [Id]
+-- C.f. HscTypes.mkImplicitBinds, but we do not include constructor workers
+implicit_ids (ATyCon tc) = map dataConWrapId (tyConDataCons_maybe tc `orElse` [])
+ ++ tyConSelIds tc ++ tyConGenIds tc
+implicit_ids (AClass cl) = classSelIds cl
+implicit_ids other = []
+
+get_defn :: Id -> CoreBind
+get_defn id = NonRec id (unfoldingTemplate (idUnfolding id))
collect_tdefs :: TyCon -> [C.Tdef] -> [C.Tdef]
collect_tdefs tcon tdefs
make_cdef :: DataCon -> C.Cdef
make_cdef dcon = C.Constr dcon_name existentials tys
where
- dcon_name = make_con_qid (idName (dataConWorkId dcon))
+ dcon_name = make_con_qid (dataConName dcon)
existentials = map make_tbind ex_tyvars
ex_tyvars = dataConExistentialTyVars dcon
tys = map make_ty (dataConRepArgTys dcon)
make_exp :: CoreExpr -> C.Exp
make_exp (Var v) =
case globalIdDetails v of
- DataConId _ -> C.Dcon (make_con_qid (Var.varName v))
- FCallId (CCall (CCallSpec (StaticTarget nm) _ _)) -> C.External (_UNPK_ nm) (make_ty (varType v))
+ -- a DataConId represents the Id of a worker, which is a varName. -- sof 4/02
+-- DataConId _ -> C.Dcon (make_con_qid (Var.varName v))
+ FCallId (CCall (CCallSpec (StaticTarget nm) _ _)) -> C.External (unpackFS nm) (make_ty (varType v))
FCallId _ -> error "MkExternalCore died: can't handle non-static-C foreign call"
_ -> C.Var (make_var_qid (Var.varName v))
-make_exp (Lit (l@(MachLabel s))) = C.External (_UNPK_ s) (make_ty (literalType l))
+make_exp (Lit (l@(MachLabel s))) = C.External (unpackFS s) (make_ty (literalType l))
make_exp (Lit l) = C.Lit (make_lit l)
make_exp (App e (Type t)) = C.Appt (make_exp e) (make_ty t)
make_exp (App e1 e2) = C.App (make_exp e1) (make_exp e2)
make_alt :: CoreAlt -> C.Alt
make_alt (DataAlt dcon, vs, e) =
- C.Acon (make_con_qid (idName (dataConWorkId dcon))) (map make_tbind tbs) (map make_vbind vbs) (make_exp e)
+ C.Acon (make_con_qid (dataConName dcon))
+ (map make_tbind tbs)
+ (map make_vbind vbs)
+ (make_exp e)
where (tbs,vbs) = span isTyVar vs
make_alt (LitAlt l,_,e) = C.Alit (make_lit l) (make_exp e)
make_alt (DEFAULT,[],e) = C.Adefault (make_exp e)
case l of
MachChar i | i <= 0xff -> C.Lchar (chr i) t
MachChar i | otherwise -> C.Lint (toEnum i) t
- MachStr s -> C.Lstring (_UNPK_ s) t
+ MachStr s -> C.Lstring (unpackFS s) t
MachAddr i -> C.Lint i t
MachInt i -> C.Lint i t
MachInt64 i -> C.Lint i t
{- Use encoded strings.
Also, adjust casing to work around some badly-chosen internal names. -}
make_id :: Bool -> Name -> C.Id
+make_id is_var nm = (occNameString . nameOccName) nm
+
+{- SIMON thinks this stuff isn't necessary
make_id is_var nm =
case n of
'Z':cs | is_var -> 'z':cs
c:cs | isLower c && (not is_var) -> 'Z':'d':n
_ -> n
where n = (occNameString . nameOccName) nm
+-}
make_var_id :: Name -> C.Id
make_var_id = make_id True