remove empty dir
[ghc-hetmet.git] / compiler / coreSyn / MkExternalCore.lhs
1 %
2 % (c) The University of Glasgow 2001
3 %
4 \begin{code}
5
6 module MkExternalCore (
7         emitExternalCore
8 ) where
9
10 #include "HsVersions.h"
11
12 import qualified ExternalCore as C
13 import Char
14 import Module
15 import CoreSyn
16 import HscTypes 
17 import TyCon
18 import TypeRep
19 import Type
20 import PprExternalCore  -- Instances
21 import DataCon  ( DataCon, dataConTyVars, dataConRepArgTys, 
22                   dataConName, dataConTyCon )
23 import CoreSyn
24 import Var
25 import IdInfo
26 import Kind
27 import Literal
28 import Name
29 import Outputable
30 import ForeignCall
31 import DynFlags ( DynFlags(..) )
32 import StaticFlags      ( opt_EmitExternalCore )
33 import IO
34 import FastString
35
36 emitExternalCore :: DynFlags -> CgGuts -> IO ()
37 emitExternalCore dflags cg_guts
38  | opt_EmitExternalCore 
39  = (do handle <- openFile corename WriteMode
40        hPutStrLn handle (show (mkExternalCore cg_guts))      
41        hClose handle)
42    `catch` (\err -> pprPanic "Failed to open or write external core output file" 
43                              (text corename))
44    where corename = extCoreName dflags
45 emitExternalCore _ _
46  | otherwise
47  = return ()
48
49
50 mkExternalCore :: CgGuts -> C.Module
51 -- The ModGuts has been tidied, but the implicit bindings have
52 -- not been injected, so we have to add them manually here
53 -- We don't include the strange data-con *workers* because they are
54 -- implicit in the data type declaration itself
55 mkExternalCore (CgGuts {cg_module=this_mod, cg_tycons = tycons, cg_binds = binds})
56   = C.Module mname tdefs (map make_vdef binds)
57   where
58     mname  = make_mid this_mod
59     tdefs  = foldr collect_tdefs [] tycons
60
61 collect_tdefs :: TyCon -> [C.Tdef] -> [C.Tdef]
62 collect_tdefs tcon tdefs 
63   | isAlgTyCon tcon = tdef: tdefs
64   where
65     tdef | isNewTyCon tcon = 
66                 C.Newtype (make_con_qid (tyConName tcon)) (map make_tbind tyvars) repclause 
67          | null (tyConDataCons tcon) = error "MkExternalCore died: can't handle datatype declarations with no data constructors"
68          | otherwise = 
69                 C.Data (make_con_qid (tyConName tcon)) (map make_tbind tyvars) (map make_cdef (tyConDataCons tcon)) 
70          where repclause | isRecursiveTyCon tcon = Nothing
71                          | otherwise = Just (make_ty rep)
72                                            where (_, rep) = newTyConRep tcon
73     tyvars = tyConTyVars tcon
74
75 collect_tdefs _ tdefs = tdefs
76
77
78 make_cdef :: DataCon -> C.Cdef
79 make_cdef dcon =  C.Constr dcon_name existentials tys
80   where 
81     dcon_name    = make_var_id (dataConName dcon)
82     existentials = map make_tbind ex_tyvars
83     ex_tyvars    = drop (tyConArity (dataConTyCon dcon)) (dataConTyVars dcon)
84     tys          = map make_ty (dataConRepArgTys dcon)
85
86 make_tbind :: TyVar -> C.Tbind
87 make_tbind tv = (make_var_id (tyVarName tv), make_kind (tyVarKind tv))
88     
89 make_vbind :: Var -> C.Vbind
90 make_vbind v = (make_var_id  (Var.varName v), make_ty (idType v))
91
92 make_vdef :: CoreBind -> C.Vdefg
93 make_vdef b = 
94   case b of
95     NonRec v e -> C.Nonrec (f (v,e))
96     Rec ves -> C.Rec (map f ves)
97   where f (v,e) = (make_var_id (Var.varName v), make_ty (idType v),make_exp e)
98         -- Top level bindings are unqualified now
99
100 make_exp :: CoreExpr -> C.Exp
101 make_exp (Var v) =  
102   case globalIdDetails v of
103      -- a DataConId represents the Id of a worker, which is a varName. -- sof 4/02
104 --    DataConId _ -> C.Dcon (make_con_qid (Var.varName v))
105     FCallId (CCall (CCallSpec (StaticTarget nm) _ _)) -> C.External (unpackFS nm) (make_ty (idType v))
106     FCallId _ -> error "MkExternalCore died: can't handle non-static-C foreign call"
107     _ -> C.Var (make_var_qid (Var.varName v))
108 make_exp (Lit (l@(MachLabel s _))) = error "MkExternalCore died: can't handle \"foreign label\" declarations"
109 make_exp (Lit l) = C.Lit (make_lit l)
110 make_exp (App e (Type t)) = C.Appt (make_exp e) (make_ty t)
111 make_exp (App e1 e2) = C.App (make_exp e1) (make_exp e2)
112 make_exp (Lam v e) | isTyVar v = C.Lam (C.Tb (make_tbind v)) (make_exp e)
113 make_exp (Lam v e) | otherwise = C.Lam (C.Vb (make_vbind v)) (make_exp e)
114 make_exp (Let b e) = C.Let (make_vdef b) (make_exp e)
115 -- gaw 2004
116 make_exp (Case e v ty alts) = C.Case (make_exp e) (make_vbind v) (make_ty ty) (map make_alt alts)
117 make_exp (Note (SCC cc) e) = C.Note "SCC"  (make_exp e) -- temporary
118 make_exp (Note (Coerce t_to t_from) e) = C.Coerce (make_ty t_to) (make_exp e)
119 make_exp (Note InlineCall e) = C.Note "InlineCall" (make_exp e)
120 make_exp (Note (CoreNote s) e) = C.Note s (make_exp e)  -- hdaume: core annotations
121 make_exp (Note InlineMe e) = C.Note "InlineMe" (make_exp e)
122 make_exp _ = error "MkExternalCore died: make_exp"
123
124 make_alt :: CoreAlt -> C.Alt
125 make_alt (DataAlt dcon, vs, e) = 
126     C.Acon (make_con_qid (dataConName dcon))
127            (map make_tbind tbs)
128            (map make_vbind vbs)
129            (make_exp e)    
130         where (tbs,vbs) = span isTyVar vs
131 make_alt (LitAlt l,_,e) = C.Alit (make_lit l) (make_exp e)
132 make_alt (DEFAULT,[],e) = C.Adefault (make_exp e)
133
134 make_lit :: Literal -> C.Lit
135 make_lit l = 
136   case l of
137     MachChar i -> C.Lchar i t
138     MachStr s -> C.Lstring (unpackFS s) t
139     MachNullAddr -> C.Lint 0 t
140     MachInt i -> C.Lint i t
141     MachInt64 i -> C.Lint i t
142     MachWord i -> C.Lint i t
143     MachWord64 i -> C.Lint i t
144     MachFloat r -> C.Lrational r t
145     MachDouble r -> C.Lrational r t
146     _ -> error "MkExternalCore died: make_lit"
147   where 
148     t = make_ty (literalType l)
149
150 make_ty :: Type -> C.Ty
151 make_ty (TyVarTy tv)             = C.Tvar (make_var_id (tyVarName tv))
152 make_ty (AppTy t1 t2)            = C.Tapp (make_ty t1) (make_ty t2)
153 make_ty (FunTy t1 t2)            = make_ty (TyConApp funTyCon [t1,t2])
154 make_ty (ForAllTy tv t)          = C.Tforall (make_tbind tv) (make_ty t)
155 make_ty (TyConApp tc ts)         = foldl C.Tapp (C.Tcon (make_con_qid (tyConName tc))) 
156                                          (map make_ty ts)
157 -- Newtypes are treated just like any other type constructor; not expanded
158 -- Reason: predTypeRep does substitution and, while substitution deals
159 --         correctly with name capture, it's only correct if you see the uniques!
160 --         If you just see occurrence names, name capture may occur.
161 -- Example: newtype A a = A (forall b. b -> a)
162 --          test :: forall q b. q -> A b
163 --          test _ = undefined
164 --      Here the 'a' gets substituted by 'b', which is captured.
165 -- Another solution would be to expand newtypes before tidying; but that would
166 -- expose the representation in interface files, which definitely isn't right.
167 -- Maybe CoreTidy should know whether to expand newtypes or not?
168
169 make_ty (PredTy p)      = make_ty (predTypeRep p)
170 make_ty (NoteTy _ t)    = make_ty t
171
172
173
174 make_kind :: Kind -> C.Kind
175 make_kind (FunKind k1 k2)  = C.Karrow (make_kind k1) (make_kind k2)
176 make_kind LiftedTypeKind   = C.Klifted
177 make_kind UnliftedTypeKind = C.Kunlifted
178 make_kind OpenTypeKind     = C.Kopen
179 make_kind _ = error "MkExternalCore died: make_kind"
180
181 {- Id generation. -}
182
183 {- Use encoded strings.
184    Also, adjust casing to work around some badly-chosen internal names. -}
185 make_id :: Bool -> Name -> C.Id
186 make_id is_var nm = (occNameString . nameOccName) nm
187
188 {-      SIMON thinks this stuff isn't necessary
189 make_id is_var nm = 
190   case n of
191     'Z':cs | is_var -> 'z':cs 
192     'z':cs | not is_var -> 'Z':cs 
193     c:cs | isUpper c && is_var -> 'z':'d':n
194     c:cs | isLower c && (not is_var) -> 'Z':'d':n
195     _ -> n
196   where n = (occNameString . nameOccName) nm
197 -}
198
199 make_var_id :: Name -> C.Id
200 make_var_id = make_id True
201
202 make_mid :: Module -> C.Id
203 make_mid = moduleString
204
205 make_qid :: Bool -> Name -> C.Qual C.Id
206 make_qid is_var n = (mname,make_id is_var n)
207     where mname = 
208            case nameModule_maybe n of
209             Just m -> make_mid m
210             Nothing -> "" 
211
212 make_var_qid :: Name -> C.Qual C.Id
213 make_var_qid = make_qid True
214
215 make_con_qid :: Name -> C.Qual C.Id
216 make_con_qid = make_qid False
217
218 \end{code}
219
220
221
222