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,
72 tcg_fam_insts = fam_insts,
73 tcg_hpc = other_hpc_info })
75 = do { let dflags = hsc_dflags hsc_env
76 ; showPass dflags "Desugar"
78 -- Desugar the program
79 ; let export_set = availsToNameSet exports
80 ; let auto_scc = mkAutoScc dflags mod export_set
81 ; let target = hscTarget dflags
82 ; let hpcInfo = emptyHpcInfo other_hpc_info
86 return (emptyMessages,
87 Just ([], [], NoStubs, hpcInfo, emptyModBreaks))
89 (binds_cvr,ds_hpc_info, modBreaks)
91 || target == HscInterpreted)
92 && (not (isHsBoot hsc_src))
93 then addCoverageTicksToBinds dflags mod mod_loc (typeEnvTyCons type_env) binds
94 else return (binds, hpcInfo, emptyModBreaks)
95 initDs hsc_env mod rdr_env type_env $ do
96 core_prs <- dsTopLHsBinds auto_scc binds_cvr
97 (ds_fords, foreign_prs) <- dsForeigns fords
98 let all_prs = foreign_prs ++ core_prs
99 ds_rules <- mapM dsRule rules
100 return (all_prs, catMaybes ds_rules, ds_fords, ds_hpc_info, modBreaks)
103 Nothing -> return (msgs, Nothing) ;
104 Just (all_prs, ds_rules, ds_fords,ds_hpc_info, modBreaks) -> do
106 { -- Add export flags to bindings
107 keep_alive <- readIORef keep_var
108 ; let final_prs = addExportFlags target export_set
109 keep_alive all_prs ds_rules
110 ds_binds = [Rec final_prs]
111 -- Notice that we put the whole lot in a big Rec, even the foreign binds
112 -- When compiling PrelFloat, which defines data Float = F# Float#
113 -- we want F# to be in scope in the foreign marshalling code!
114 -- You might think it doesn't matter, but the simplifier brings all top-level
115 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
117 -- Lint result if necessary
118 ; endPass dflags "Desugar" Opt_D_dump_ds ds_binds
121 ; doIfSet (dopt Opt_D_dump_ds dflags)
122 (printDump (ppr_ds_rules ds_rules))
124 ; used_names <- mkUsedNames tcg_env
125 ; deps <- mkDependencies tcg_env
127 ; let mod_guts = ModGuts {
129 mg_boot = isHsBoot hsc_src,
130 mg_exports = exports,
132 mg_used_names = used_names,
133 mg_dir_imps = imp_mods imports,
134 mg_rdr_env = rdr_env,
135 mg_fix_env = fix_env,
140 mg_fam_insts = fam_insts,
141 mg_inst_env = inst_env,
142 mg_fam_inst_env = fam_inst_env,
145 mg_foreign = ds_fords,
146 mg_hpc_info = ds_hpc_info,
147 mg_modBreaks = modBreaks,
148 mg_vect_info = noVectInfo
150 ; return (msgs, Just mod_guts)
153 mkAutoScc :: DynFlags -> Module -> NameSet -> AutoScc
154 mkAutoScc dflags mod exports
155 | not opt_SccProfilingOn -- No profiling
157 -- Add auto-scc on all top-level things
158 | dopt Opt_AutoSccsOnAllToplevs dflags
159 = AddSccs mod (\id -> not $ isDerivedOccName $ getOccName id)
160 -- See #1641. This is pretty yucky, but I can't see a better way
161 -- to identify compiler-generated Ids, and at least this should
163 -- Only on exported things
164 | dopt Opt_AutoSccsOnExportedToplevs dflags
165 = AddSccs mod (\id -> idName id `elemNameSet` exports)
169 deSugarExpr :: HscEnv
170 -> Module -> GlobalRdrEnv -> TypeEnv
172 -> IO (Messages, Maybe CoreExpr)
173 -- Prints its own errors; returns Nothing if error occurred
175 deSugarExpr hsc_env this_mod rdr_env type_env tc_expr = do
176 let dflags = hsc_dflags hsc_env
177 showPass dflags "Desugar"
180 (msgs, mb_core_expr) <- initDs hsc_env this_mod rdr_env type_env $
184 Nothing -> return (msgs, Nothing)
188 dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr expr)
190 return (msgs, Just expr)
193 -- Set the no-discard flag if either
194 -- a) the Id is exported
195 -- b) it's mentioned in the RHS of an orphan rule
196 -- c) it's in the keep-alive set
198 -- It means that the binding won't be discarded EVEN if the binding
199 -- ends up being trivial (v = w) -- the simplifier would usually just
200 -- substitute w for v throughout, but we don't apply the substitution to
201 -- the rules (maybe we should?), so this substitution would make the rule
204 -- You might wonder why exported Ids aren't already marked as such;
205 -- it's just because the type checker is rather busy already and
206 -- I didn't want to pass in yet another mapping.
208 addExportFlags :: HscTarget -> NameSet -> NameSet -> [(Id, t)] -> [CoreRule]
210 addExportFlags target exports keep_alive prs rules
211 = [(add_export bndr, rhs) | (bndr,rhs) <- prs]
214 | dont_discard bndr = setIdExported bndr
217 orph_rhs_fvs = unionVarSets [ ruleRhsFreeVars rule
219 not (isLocalRule rule) ]
220 -- A non-local rule keeps alive the free vars of its right-hand side.
221 -- (A "non-local" is one whose head function is not locally defined.)
222 -- Local rules are (later, after gentle simplification)
223 -- attached to the Id, and that keeps the rhs free vars alive.
225 dont_discard bndr = is_exported name
226 || name `elemNameSet` keep_alive
227 || bndr `elemVarSet` orph_rhs_fvs
231 -- In interactive mode, we don't want to discard any top-level
232 -- entities at all (eg. do not inline them away during
233 -- simplification), and retain them all in the TypeEnv so they are
234 -- available from the command line.
236 -- isExternalName separates the user-defined top-level names from those
237 -- introduced by the type checker.
238 is_exported :: Name -> Bool
239 is_exported | target == HscInterpreted = isExternalName
240 | otherwise = (`elemNameSet` exports)
242 ppr_ds_rules :: [CoreRule] -> SDoc
243 ppr_ds_rules [] = empty
245 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
251 %************************************************************************
253 %* Desugaring transformation rules
255 %************************************************************************
258 dsRule :: LRuleDecl Id -> DsM (Maybe CoreRule)
259 dsRule (L loc (HsRule name act vars lhs _tv_lhs rhs _fv_rhs))
261 do { let bndrs' = [var | RuleBndr (L _ var) <- vars]
262 ; lhs' <- dsLExpr lhs
263 ; rhs' <- dsLExpr rhs
265 -- Substitute the dict bindings eagerly,
266 -- and take the body apart into a (f args) form
267 ; case decomposeRuleLhs (mkLams bndrs' lhs') of {
268 Nothing -> do { warnDs msg; return Nothing } ;
269 Just (bndrs, fn_id, args) -> do
271 { let local_rule = isLocalId fn_id
272 -- NB: isLocalId is False of implicit Ids. This is good becuase
273 -- we don't want to attach rules to the bindings of implicit Ids,
274 -- because they don't show up in the bindings until just before code gen
275 fn_name = idName fn_id
277 rule = Rule { ru_name = name, ru_fn = fn_name, ru_act = act,
278 ru_bndrs = bndrs, ru_args = args, ru_rhs = rhs',
279 ru_rough = roughTopNames args,
280 ru_local = local_rule }
284 msg = hang (ptext (sLit "RULE left-hand side too complicated to desugar; ignored"))