2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Desugar]{@deSugar@: the main function}
7 module Desugar ( deSugar, deSugarExpr ) where
9 #include "HsVersions.h"
11 import DynFlags ( DynFlag(..), DynFlags(..), dopt, GhcMode(..) )
12 import StaticFlags ( opt_SccProfilingOn,
13 opt_AutoSccsOnAllToplevs,
14 opt_AutoSccsOnExportedToplevs )
15 import DriverPhases ( isHsBoot )
16 import HscTypes ( ModGuts(..), HscEnv(..),
17 Dependencies(..), ForeignStubs(..), TypeEnv, IsBootInterface )
18 import HsSyn ( RuleDecl(..), RuleBndr(..), LHsExpr, LRuleDecl )
19 import TcRnTypes ( TcGblEnv(..), ImportAvails(..) )
20 import MkIface ( mkUsageInfo )
21 import Id ( Id, setIdExported, idName )
22 import Name ( Name, isExternalName, nameIsLocalOrFrom, nameOccName )
24 import PprCore ( pprRules, pprCoreExpr )
26 import DsExpr ( dsLExpr )
27 import DsBinds ( dsTopLHsBinds, decomposeRuleLhs, AutoScc(..) )
28 import DsForeign ( dsForeigns )
29 import DsExpr () -- Forces DsExpr to be compiled; DsBinds only
30 -- depends on DsExpr.hi-boot.
32 import UniqFM ( eltsUFM, delFromUFM )
33 import PackageConfig ( thPackageId )
34 import RdrName ( GlobalRdrEnv )
37 import Rules ( roughTopNames )
38 import CoreLint ( showPass, endPass )
39 import CoreFVs ( ruleRhsFreeVars, exprsFreeNames )
40 import ErrUtils ( doIfSet, dumpIfSet_dyn )
41 import ListSetOps ( insertList )
43 import SrcLoc ( Located(..) )
44 import DATA_IOREF ( readIORef )
45 import Maybes ( catMaybes )
47 import Util ( sortLe )
50 %************************************************************************
52 %* The main function: deSugar
54 %************************************************************************
57 deSugar :: HscEnv -> TcGblEnv -> IO (Maybe ModGuts)
58 -- Can modify PCS by faulting in more declarations
61 tcg_env@(TcGblEnv { tcg_mod = mod,
63 tcg_type_env = type_env,
64 tcg_imports = imports,
65 tcg_exports = exports,
67 tcg_inst_uses = dfun_uses_var,
70 tcg_rdr_env = rdr_env,
71 tcg_fix_env = fix_env,
72 tcg_deprecs = deprecs,
77 tcg_fam_insts = fam_insts })
78 = do { showPass dflags "Desugar"
80 -- Desugar the program
81 ; let auto_scc = mkAutoScc mod exports
83 ; mb_res <- case ghcMode dflags of
84 JustTypecheck -> return (Just ([], [], NoStubs))
85 _ -> initDs hsc_env mod rdr_env type_env $ do
86 { core_prs <- dsTopLHsBinds auto_scc binds
87 ; (ds_fords, foreign_prs) <- dsForeigns fords
88 ; let all_prs = foreign_prs ++ core_prs
89 local_bndrs = mkVarSet (map fst all_prs)
90 ; ds_rules <- mappM (dsRule mod local_bndrs) rules
91 ; return (all_prs, catMaybes ds_rules, ds_fords)
94 Nothing -> return Nothing ;
95 Just (all_prs, ds_rules, ds_fords) -> do
97 { -- Add export flags to bindings
98 keep_alive <- readIORef keep_var
99 ; let final_prs = addExportFlags ghci_mode exports keep_alive
101 ds_binds = [Rec final_prs]
102 -- Notice that we put the whole lot in a big Rec, even the foreign binds
103 -- When compiling PrelFloat, which defines data Float = F# Float#
104 -- we want F# to be in scope in the foreign marshalling code!
105 -- You might think it doesn't matter, but the simplifier brings all top-level
106 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
108 -- Lint result if necessary
109 ; endPass dflags "Desugar" Opt_D_dump_ds ds_binds
112 ; doIfSet (dopt Opt_D_dump_ds dflags)
113 (printDump (ppr_ds_rules ds_rules))
115 ; dfun_uses <- readIORef dfun_uses_var -- What dfuns are used
116 ; th_used <- readIORef th_var -- Whether TH is used
117 ; let used_names = allUses dus `unionNameSets` dfun_uses
118 pkgs | th_used = insertList thPackageId (imp_dep_pkgs imports)
119 | otherwise = imp_dep_pkgs imports
121 dep_mods = eltsUFM (delFromUFM (imp_dep_mods imports) (moduleName mod))
122 -- M.hi-boot can be in the imp_dep_mods, but we must remove
123 -- it before recording the modules on which this one depends!
124 -- (We want to retain M.hi-boot in imp_dep_mods so that
125 -- loadHiBootInterface can see if M's direct imports depend
126 -- on M.hi-boot, and hence that we should do the hi-boot consistency
129 dir_imp_mods = imp_mods imports
131 ; usages <- mkUsageInfo hsc_env dir_imp_mods dep_mods used_names
134 -- Modules don't compare lexicographically usually,
135 -- but we want them to do so here.
136 le_mod :: Module -> Module -> Bool
137 le_mod m1 m2 = moduleNameFS (moduleName m1)
138 <= moduleNameFS (moduleName m2)
139 le_dep_mod :: (ModuleName, IsBootInterface) -> (ModuleName, IsBootInterface) -> Bool
140 le_dep_mod (m1,_) (m2,_) = moduleNameFS m1 <= moduleNameFS m2
142 deps = Deps { dep_mods = sortLe le_dep_mod dep_mods,
143 dep_pkgs = sortLe (<=) pkgs,
144 dep_orphs = sortLe le_mod (imp_orphs imports) }
145 -- sort to get into canonical order
149 mg_boot = isHsBoot hsc_src,
150 mg_exports = exports,
153 mg_dir_imps = [m | (m,_,_) <- moduleEnvElts dir_imp_mods],
154 mg_rdr_env = rdr_env,
155 mg_fix_env = fix_env,
156 mg_deprecs = deprecs,
159 mg_fam_insts = fam_insts,
162 mg_foreign = ds_fords }
164 ; return (Just mod_guts)
168 dflags = hsc_dflags hsc_env
169 ghci_mode = ghcMode (hsc_dflags hsc_env)
171 mkAutoScc :: Module -> NameSet -> AutoScc
172 mkAutoScc mod exports
173 | not opt_SccProfilingOn -- No profiling
175 | opt_AutoSccsOnAllToplevs -- Add auto-scc on all top-level things
176 = AddSccs mod (\id -> True)
177 | opt_AutoSccsOnExportedToplevs -- Only on exported things
178 = AddSccs mod (\id -> idName id `elemNameSet` exports)
183 deSugarExpr :: HscEnv
184 -> Module -> GlobalRdrEnv -> TypeEnv
186 -> IO (Maybe CoreExpr)
187 -- Prints its own errors; returns Nothing if error occurred
189 deSugarExpr hsc_env this_mod rdr_env type_env tc_expr
190 = do { let dflags = hsc_dflags hsc_env
191 ; showPass dflags "Desugar"
194 ; mb_core_expr <- initDs hsc_env this_mod rdr_env type_env $
197 ; case mb_core_expr of {
198 Nothing -> return Nothing ;
202 dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr expr)
204 ; return (Just expr) } } }
207 -- Set the no-discard flag if either
208 -- a) the Id is exported
209 -- b) it's mentioned in the RHS of an orphan rule
210 -- c) it's in the keep-alive set
212 -- It means that the binding won't be discarded EVEN if the binding
213 -- ends up being trivial (v = w) -- the simplifier would usually just
214 -- substitute w for v throughout, but we don't apply the substitution to
215 -- the rules (maybe we should?), so this substitution would make the rule
218 -- You might wonder why exported Ids aren't already marked as such;
219 -- it's just because the type checker is rather busy already and
220 -- I didn't want to pass in yet another mapping.
222 addExportFlags ghci_mode exports keep_alive prs rules
223 = [(add_export bndr, rhs) | (bndr,rhs) <- prs]
226 | dont_discard bndr = setIdExported bndr
229 orph_rhs_fvs = unionVarSets [ ruleRhsFreeVars rule
231 not (isLocalRule rule) ]
232 -- A non-local rule keeps alive the free vars of its right-hand side.
233 -- (A "non-local" is one whose head function is not locally defined.)
234 -- Local rules are (later, after gentle simplification)
235 -- attached to the Id, and that keeps the rhs free vars alive.
237 dont_discard bndr = is_exported name
238 || name `elemNameSet` keep_alive
239 || bndr `elemVarSet` orph_rhs_fvs
243 -- In interactive mode, we don't want to discard any top-level
244 -- entities at all (eg. do not inline them away during
245 -- simplification), and retain them all in the TypeEnv so they are
246 -- available from the command line.
248 -- isExternalName separates the user-defined top-level names from those
249 -- introduced by the type checker.
250 is_exported :: Name -> Bool
251 is_exported | ghci_mode == Interactive = isExternalName
252 | otherwise = (`elemNameSet` exports)
254 ppr_ds_rules [] = empty
256 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
262 %************************************************************************
264 %* Desugaring transformation rules
266 %************************************************************************
269 dsRule :: Module -> IdSet -> LRuleDecl Id -> DsM (Maybe CoreRule)
270 dsRule mod in_scope (L loc (HsRule name act vars lhs tv_lhs rhs fv_rhs))
272 do { let bndrs = [var | RuleBndr (L _ var) <- vars]
273 ; lhs' <- dsLExpr lhs
274 ; rhs' <- dsLExpr rhs
276 ; case decomposeRuleLhs bndrs lhs' of {
277 Nothing -> do { warnDs msg; return Nothing } ;
278 Just (bndrs', fn_id, args) -> do
280 -- Substitute the dict bindings eagerly,
281 -- and take the body apart into a (f args) form
282 { let local_rule = nameIsLocalOrFrom mod fn_name
283 -- NB we can't use isLocalId in the orphan test,
284 -- because isLocalId isn't true of class methods
285 fn_name = idName fn_id
286 lhs_names = fn_name : nameSetToList (exprsFreeNames args)
287 -- No need to delete bndrs, because
288 -- exprsFreeNames finds only External names
290 -- A rule is an orphan only if none of the variables
291 -- mentioned on its left-hand side are locally defined
292 orph = case filter (nameIsLocalOrFrom mod) lhs_names of
293 (n:ns) -> Just (nameOccName n)
296 rule = Rule { ru_name = name, ru_fn = fn_name, ru_act = act,
297 ru_bndrs = bndrs', ru_args = args, ru_rhs = rhs',
298 ru_rough = roughTopNames args,
299 ru_local = local_rule, ru_orph = orph }
303 msg = hang (ptext SLIT("RULE left-hand side too complicated to desugar; ignored"))