2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 The Desugarer: turning HsSyn into Core.
9 module Desugar ( deSugar, deSugarExpr ) where
25 import DsExpr () -- Forces DsExpr to be compiled; DsBinds only
26 -- depends on DsExpr.hi-boot.
44 %************************************************************************
46 %* The main function: deSugar
48 %************************************************************************
51 deSugar :: HscEnv -> ModLocation -> TcGblEnv -> IO (Messages, Maybe ModGuts)
52 -- Can modify PCS by faulting in more declarations
56 tcg_env@(TcGblEnv { tcg_mod = mod,
58 tcg_type_env = type_env,
59 tcg_imports = imports,
60 tcg_exports = exports,
62 tcg_rdr_env = rdr_env,
63 tcg_fix_env = fix_env,
64 tcg_inst_env = inst_env,
65 tcg_fam_inst_env = fam_inst_env,
71 tcg_fam_insts = fam_insts,
72 tcg_hpc = other_hpc_info })
74 = do { let dflags = hsc_dflags hsc_env
75 ; showPass dflags "Desugar"
77 -- Desugar the program
78 ; let export_set = availsToNameSet exports
79 ; let auto_scc = mkAutoScc mod export_set
80 ; let target = hscTarget dflags
81 ; let hpcInfo = emptyHpcInfo other_hpc_info
85 return (emptyMessages,
86 Just ([], [], NoStubs, hpcInfo, emptyModBreaks))
88 (binds_cvr,ds_hpc_info, modBreaks)
90 || target == HscInterpreted)
91 && (not (isHsBoot hsc_src))
92 then addCoverageTicksToBinds dflags mod mod_loc (typeEnvTyCons type_env) binds
93 else return (binds, hpcInfo, emptyModBreaks)
94 initDs hsc_env mod rdr_env type_env $ do
95 core_prs <- dsTopLHsBinds auto_scc binds_cvr
96 (ds_fords, foreign_prs) <- dsForeigns fords
97 let all_prs = foreign_prs ++ core_prs
98 ds_rules <- mapM dsRule rules
99 return (all_prs, catMaybes ds_rules, ds_fords, ds_hpc_info, modBreaks)
102 Nothing -> return (msgs, Nothing) ;
103 Just (all_prs, ds_rules, ds_fords,ds_hpc_info, modBreaks) -> do
105 { -- Add export flags to bindings
106 keep_alive <- readIORef keep_var
107 ; let final_prs = addExportFlags target export_set
108 keep_alive all_prs ds_rules
109 ds_binds = [Rec final_prs]
110 -- Notice that we put the whole lot in a big Rec, even the foreign binds
111 -- When compiling PrelFloat, which defines data Float = F# Float#
112 -- we want F# to be in scope in the foreign marshalling code!
113 -- You might think it doesn't matter, but the simplifier brings all top-level
114 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
116 -- Lint result if necessary
117 ; endPass dflags "Desugar" Opt_D_dump_ds ds_binds
120 ; doIfSet (dopt Opt_D_dump_ds dflags)
121 (printDump (ppr_ds_rules ds_rules))
123 ; used_names <- mkUsedNames tcg_env
124 ; deps <- mkDependencies tcg_env
126 ; let mod_guts = ModGuts {
128 mg_boot = isHsBoot hsc_src,
129 mg_exports = exports,
131 mg_used_names = used_names,
132 mg_dir_imps = imp_mods imports,
133 mg_rdr_env = rdr_env,
134 mg_fix_env = fix_env,
138 mg_fam_insts = fam_insts,
139 mg_inst_env = inst_env,
140 mg_fam_inst_env = fam_inst_env,
143 mg_foreign = ds_fords,
144 mg_hpc_info = ds_hpc_info,
145 mg_modBreaks = modBreaks,
146 mg_vect_info = noVectInfo
148 ; return (msgs, Just mod_guts)
151 mkAutoScc :: Module -> NameSet -> AutoScc
152 mkAutoScc mod exports
153 | not opt_SccProfilingOn -- No profiling
155 | opt_AutoSccsOnAllToplevs -- Add auto-scc on all top-level things
156 = AddSccs mod (\id -> not $ isDerivedOccName $ getOccName id)
157 -- See #1641. This is pretty yucky, but I can't see a better way
158 -- to identify compiler-generated Ids, and at least this should
160 | opt_AutoSccsOnExportedToplevs -- Only on exported things
161 = AddSccs mod (\id -> idName id `elemNameSet` exports)
165 deSugarExpr :: HscEnv
166 -> Module -> GlobalRdrEnv -> TypeEnv
168 -> IO (Messages, Maybe CoreExpr)
169 -- Prints its own errors; returns Nothing if error occurred
171 deSugarExpr hsc_env this_mod rdr_env type_env tc_expr = do
172 let dflags = hsc_dflags hsc_env
173 showPass dflags "Desugar"
176 (msgs, mb_core_expr) <- initDs hsc_env this_mod rdr_env type_env $
180 Nothing -> return (msgs, Nothing)
184 dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr expr)
186 return (msgs, Just expr)
189 -- Set the no-discard flag if either
190 -- a) the Id is exported
191 -- b) it's mentioned in the RHS of an orphan rule
192 -- c) it's in the keep-alive set
194 -- It means that the binding won't be discarded EVEN if the binding
195 -- ends up being trivial (v = w) -- the simplifier would usually just
196 -- substitute w for v throughout, but we don't apply the substitution to
197 -- the rules (maybe we should?), so this substitution would make the rule
200 -- You might wonder why exported Ids aren't already marked as such;
201 -- it's just because the type checker is rather busy already and
202 -- I didn't want to pass in yet another mapping.
204 addExportFlags :: HscTarget -> NameSet -> NameSet -> [(Id, t)] -> [CoreRule]
206 addExportFlags target exports keep_alive prs rules
207 = [(add_export bndr, rhs) | (bndr,rhs) <- prs]
210 | dont_discard bndr = setIdExported bndr
213 orph_rhs_fvs = unionVarSets [ ruleRhsFreeVars rule
215 not (isLocalRule rule) ]
216 -- A non-local rule keeps alive the free vars of its right-hand side.
217 -- (A "non-local" is one whose head function is not locally defined.)
218 -- Local rules are (later, after gentle simplification)
219 -- attached to the Id, and that keeps the rhs free vars alive.
221 dont_discard bndr = is_exported name
222 || name `elemNameSet` keep_alive
223 || bndr `elemVarSet` orph_rhs_fvs
227 -- In interactive mode, we don't want to discard any top-level
228 -- entities at all (eg. do not inline them away during
229 -- simplification), and retain them all in the TypeEnv so they are
230 -- available from the command line.
232 -- isExternalName separates the user-defined top-level names from those
233 -- introduced by the type checker.
234 is_exported :: Name -> Bool
235 is_exported | target == HscInterpreted = isExternalName
236 | otherwise = (`elemNameSet` exports)
238 ppr_ds_rules :: [CoreRule] -> SDoc
239 ppr_ds_rules [] = empty
241 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
247 %************************************************************************
249 %* Desugaring transformation rules
251 %************************************************************************
254 dsRule :: LRuleDecl Id -> DsM (Maybe CoreRule)
255 dsRule (L loc (HsRule name act vars lhs _tv_lhs rhs _fv_rhs))
257 do { let bndrs' = [var | RuleBndr (L _ var) <- vars]
258 ; lhs' <- dsLExpr lhs
259 ; rhs' <- dsLExpr rhs
261 -- Substitute the dict bindings eagerly,
262 -- and take the body apart into a (f args) form
263 ; case decomposeRuleLhs (mkLams bndrs' lhs') of {
264 Nothing -> do { warnDs msg; return Nothing } ;
265 Just (bndrs, fn_id, args) -> do
267 { let local_rule = isLocalId fn_id
268 -- NB: isLocalId is False of implicit Ids. This is good becuase
269 -- we don't want to attach rules to the bindings of implicit Ids,
270 -- because they don't show up in the bindings until just before code gen
271 fn_name = idName fn_id
273 rule = Rule { ru_name = name, ru_fn = fn_name, ru_act = act,
274 ru_bndrs = bndrs, ru_args = args, ru_rhs = rhs',
275 ru_rough = roughTopNames args,
276 ru_local = local_rule }
280 msg = hang (ptext (sLit "RULE left-hand side too complicated to desugar; ignored"))