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
11 #include "HsVersions.h"
27 import DsExpr () -- Forces DsExpr to be compiled; DsBinds only
28 -- depends on DsExpr.hi-boot.
51 %************************************************************************
53 %* The main function: deSugar
55 %************************************************************************
58 deSugar :: HscEnv -> ModLocation -> TcGblEnv -> IO (Maybe ModGuts)
59 -- Can modify PCS by faulting in more declarations
63 tcg_env@(TcGblEnv { tcg_mod = mod,
65 tcg_type_env = type_env,
66 tcg_imports = imports,
67 tcg_exports = exports,
69 tcg_inst_uses = dfun_uses_var,
72 tcg_rdr_env = rdr_env,
73 tcg_fix_env = fix_env,
74 tcg_fam_inst_env = fam_inst_env,
75 tcg_deprecs = deprecs,
80 tcg_fam_insts = fam_insts })
81 = do { showPass dflags "Desugar"
83 -- Desugar the program
84 ; let export_set = availsToNameSet exports
85 ; let auto_scc = mkAutoScc mod export_set
87 ; mb_res <- case ghcMode dflags of
88 JustTypecheck -> return (Just ([], [], NoStubs, noHpcInfo, noDbgSites))
89 _ -> do (binds_cvr,ds_hpc_info)
91 then addCoverageTicksToBinds dflags mod mod_loc binds
92 else return (binds, noHpcInfo)
93 initDs hsc_env mod rdr_env type_env $ do
94 { core_prs <- dsTopLHsBinds auto_scc binds_cvr
95 ; (ds_fords, foreign_prs) <- dsForeigns fords
96 ; let all_prs = foreign_prs ++ core_prs
97 local_bndrs = mkVarSet (map fst all_prs)
98 ; ds_rules <- mappM (dsRule mod local_bndrs) rules
99 ; return (all_prs, catMaybes ds_rules, ds_fords, ds_hpc_info)
100 ; dbgSites_var <- getBkptSitesDs
101 ; dbgSites <- ioToIOEnv$ readIORef dbgSites_var
102 ; return (all_prs, catMaybes ds_rules, ds_fords, ds_hpc_info, dbgSites)
105 Nothing -> return Nothing ;
106 Just (all_prs, ds_rules, ds_fords,ds_hpc_info, dbgSites) -> do
108 { -- Add export flags to bindings
109 keep_alive <- readIORef keep_var
110 ; let final_prs = addExportFlags ghci_mode export_set
111 keep_alive all_prs ds_rules
112 ds_binds = [Rec final_prs]
113 -- Notice that we put the whole lot in a big Rec, even the foreign binds
114 -- When compiling PrelFloat, which defines data Float = F# Float#
115 -- we want F# to be in scope in the foreign marshalling code!
116 -- You might think it doesn't matter, but the simplifier brings all top-level
117 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
119 -- Lint result if necessary
120 ; endPass dflags "Desugar" Opt_D_dump_ds ds_binds
123 ; doIfSet (dopt Opt_D_dump_ds dflags)
124 (printDump (ppr_ds_rules ds_rules))
126 ; dfun_uses <- readIORef dfun_uses_var -- What dfuns are used
127 ; th_used <- readIORef th_var -- Whether TH is used
128 ; let used_names = allUses dus `unionNameSets` dfun_uses
129 pkgs | th_used = insertList thPackageId (imp_dep_pkgs imports)
130 | otherwise = imp_dep_pkgs imports
132 dep_mods = eltsUFM (delFromUFM (imp_dep_mods imports) (moduleName mod))
133 -- M.hi-boot can be in the imp_dep_mods, but we must remove
134 -- it before recording the modules on which this one depends!
135 -- (We want to retain M.hi-boot in imp_dep_mods so that
136 -- loadHiBootInterface can see if M's direct imports depend
137 -- on M.hi-boot, and hence that we should do the hi-boot consistency
140 dir_imp_mods = imp_mods imports
142 ; usages <- mkUsageInfo hsc_env dir_imp_mods dep_mods used_names
145 -- Modules don't compare lexicographically usually,
146 -- but we want them to do so here.
147 le_mod :: Module -> Module -> Bool
148 le_mod m1 m2 = moduleNameFS (moduleName m1)
149 <= moduleNameFS (moduleName m2)
150 le_dep_mod :: (ModuleName, IsBootInterface) -> (ModuleName, IsBootInterface) -> Bool
151 le_dep_mod (m1,_) (m2,_) = moduleNameFS m1 <= moduleNameFS m2
153 deps = Deps { dep_mods = sortLe le_dep_mod dep_mods,
154 dep_pkgs = sortLe (<=) pkgs,
155 dep_orphs = sortLe le_mod (imp_orphs imports),
156 dep_finsts = sortLe le_mod (imp_finsts imports) }
157 -- sort to get into canonical order
161 mg_boot = isHsBoot hsc_src,
162 mg_exports = exports,
165 mg_dir_imps = [m | (m,_,_) <- moduleEnvElts dir_imp_mods],
166 mg_rdr_env = rdr_env,
167 mg_fix_env = fix_env,
168 mg_deprecs = deprecs,
171 mg_fam_insts = fam_insts,
172 mg_fam_inst_env = fam_inst_env,
175 mg_foreign = ds_fords,
176 mg_hpc_info = ds_hpc_info,
177 mg_dbg_sites = dbgSites }
178 ; return (Just mod_guts)
182 dflags = hsc_dflags hsc_env
183 ghci_mode = ghcMode (hsc_dflags hsc_env)
185 mkAutoScc :: Module -> NameSet -> AutoScc
186 mkAutoScc mod exports
187 | not opt_SccProfilingOn -- No profiling
189 | opt_AutoSccsOnAllToplevs -- Add auto-scc on all top-level things
190 = AddSccs mod (\id -> True)
191 | opt_AutoSccsOnExportedToplevs -- Only on exported things
192 = AddSccs mod (\id -> idName id `elemNameSet` exports)
197 deSugarExpr :: HscEnv
198 -> Module -> GlobalRdrEnv -> TypeEnv
200 -> IO (Maybe CoreExpr)
201 -- Prints its own errors; returns Nothing if error occurred
203 deSugarExpr hsc_env this_mod rdr_env type_env tc_expr
204 = do { let dflags = hsc_dflags hsc_env
205 ; showPass dflags "Desugar"
208 ; mb_core_expr <- initDs hsc_env this_mod rdr_env type_env $
211 ; case mb_core_expr of {
212 Nothing -> return Nothing ;
216 dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr expr)
218 ; return (Just expr) } } }
221 -- Set the no-discard flag if either
222 -- a) the Id is exported
223 -- b) it's mentioned in the RHS of an orphan rule
224 -- c) it's in the keep-alive set
226 -- It means that the binding won't be discarded EVEN if the binding
227 -- ends up being trivial (v = w) -- the simplifier would usually just
228 -- substitute w for v throughout, but we don't apply the substitution to
229 -- the rules (maybe we should?), so this substitution would make the rule
232 -- You might wonder why exported Ids aren't already marked as such;
233 -- it's just because the type checker is rather busy already and
234 -- I didn't want to pass in yet another mapping.
236 addExportFlags ghci_mode exports keep_alive prs rules
237 = [(add_export bndr, rhs) | (bndr,rhs) <- prs]
240 | dont_discard bndr = setIdExported bndr
243 orph_rhs_fvs = unionVarSets [ ruleRhsFreeVars rule
245 not (isLocalRule rule) ]
246 -- A non-local rule keeps alive the free vars of its right-hand side.
247 -- (A "non-local" is one whose head function is not locally defined.)
248 -- Local rules are (later, after gentle simplification)
249 -- attached to the Id, and that keeps the rhs free vars alive.
251 dont_discard bndr = is_exported name
252 || name `elemNameSet` keep_alive
253 || bndr `elemVarSet` orph_rhs_fvs
257 -- In interactive mode, we don't want to discard any top-level
258 -- entities at all (eg. do not inline them away during
259 -- simplification), and retain them all in the TypeEnv so they are
260 -- available from the command line.
262 -- isExternalName separates the user-defined top-level names from those
263 -- introduced by the type checker.
264 is_exported :: Name -> Bool
265 is_exported | ghci_mode == Interactive = isExternalName
266 | otherwise = (`elemNameSet` exports)
268 ppr_ds_rules [] = empty
270 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
276 %************************************************************************
278 %* Desugaring transformation rules
280 %************************************************************************
283 dsRule :: Module -> IdSet -> LRuleDecl Id -> DsM (Maybe CoreRule)
284 dsRule mod in_scope (L loc (HsRule name act vars lhs tv_lhs rhs fv_rhs))
286 do { let bndrs = [var | RuleBndr (L _ var) <- vars]
287 ; lhs' <- dsLExpr lhs
288 ; rhs' <- dsLExpr rhs
290 ; case decomposeRuleLhs bndrs lhs' of {
291 Nothing -> do { warnDs msg; return Nothing } ;
292 Just (bndrs', fn_id, args) -> do
294 -- Substitute the dict bindings eagerly,
295 -- and take the body apart into a (f args) form
296 { let local_rule = nameIsLocalOrFrom mod fn_name
297 -- NB we can't use isLocalId in the orphan test,
298 -- because isLocalId isn't true of class methods
299 fn_name = idName fn_id
301 rule = Rule { ru_name = name, ru_fn = fn_name, ru_act = act,
302 ru_bndrs = bndrs', ru_args = args, ru_rhs = rhs',
303 ru_rough = roughTopNames args,
304 ru_local = local_rule }
308 msg = hang (ptext SLIT("RULE left-hand side too complicated to desugar; ignored"))