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 CmdLineOpts ( DynFlag(..), dopt, opt_SccProfilingOn )
12 import HscTypes ( ModGuts(..), ModGuts, HscEnv(..), GhciMode(..),
13 Dependencies(..), TypeEnv,
14 unQualInScope, availsToNameSet )
15 import HsSyn ( RuleDecl(..), RuleBndr(..), HsExpr(..), LHsExpr,
16 HsBindGroup(..), LRuleDecl, HsBind(..) )
17 import TcRnTypes ( TcGblEnv(..), ImportAvails(..) )
18 import MkIface ( mkUsageInfo )
19 import Id ( Id, setIdLocalExported, idName )
20 import Name ( Name, isExternalName )
22 import PprCore ( pprIdRules, pprCoreExpr )
23 import Subst ( substExpr, mkSubst, mkInScopeSet )
25 import DsExpr ( dsLExpr )
26 import DsBinds ( dsHsBinds, AutoScc(..) )
27 import DsForeign ( dsForeigns )
28 import DsExpr () -- Forces DsExpr to be compiled; DsBinds only
29 -- depends on DsExpr.hi-boot.
30 import Module ( Module, moduleEnvElts, emptyModuleEnv )
32 import RdrName ( GlobalRdrEnv )
36 import Bag ( Bag, isEmptyBag, mapBag, emptyBag, bagToList )
37 import CoreLint ( showPass, endPass )
38 import CoreFVs ( ruleRhsFreeVars )
39 import ErrUtils ( doIfSet, dumpIfSet_dyn, pprBagOfWarnings,
40 mkWarnMsg, errorsFound, WarnMsg )
42 import UniqSupply ( mkSplitUniqSupply )
43 import SrcLoc ( Located(..), SrcSpan, unLoc )
44 import DATA_IOREF ( readIORef )
48 %************************************************************************
50 %* The main function: deSugar
52 %************************************************************************
55 deSugar :: HscEnv -> TcGblEnv -> IO (Bag WarnMsg, Maybe ModGuts)
56 -- Can modify PCS by faulting in more declarations
59 tcg_env@(TcGblEnv { tcg_mod = mod,
60 tcg_type_env = type_env,
61 tcg_imports = imports,
62 tcg_exports = exports,
64 tcg_inst_uses = dfun_uses_var,
65 tcg_rdr_env = rdr_env,
66 tcg_fix_env = fix_env,
67 tcg_deprecs = deprecs,
69 = do { showPass dflags "Desugar"
72 ; let { is_boot = imp_dep_mods imports }
73 ; (results, warnings) <- initDs hsc_env mod type_env is_boot $
74 dsProgram ghci_mode tcg_env
76 ; let { (ds_binds, ds_rules, ds_fords) = results
77 ; warns = mapBag mk_warn warnings
80 -- If warnings are considered errors, leave.
81 ; if errorsFound dflags (warns, emptyBag)
82 then return (warns, Nothing)
85 -- Lint result if necessary
86 { endPass dflags "Desugar" Opt_D_dump_ds ds_binds
89 ; doIfSet (dopt Opt_D_dump_ds dflags)
90 (printDump (ppr_ds_rules ds_rules))
92 ; dfun_uses <- readIORef dfun_uses_var -- What dfuns are used
93 ; let used_names = allUses dus `unionNameSets` dfun_uses
94 ; usages <- mkUsageInfo hsc_env imports used_names
96 deps = Deps { dep_mods = moduleEnvElts (imp_dep_mods imports),
97 dep_pkgs = imp_dep_pkgs imports,
98 dep_orphs = imp_orphs imports }
101 mg_exports = exports,
104 mg_dir_imps = [m | (m,_) <- moduleEnvElts (imp_mods imports)],
105 mg_rdr_env = rdr_env,
106 mg_fix_env = fix_env,
107 mg_deprecs = deprecs,
112 mg_foreign = ds_fords }
114 ; return (warns, Just mod_guts)
118 dflags = hsc_dflags hsc_env
119 ghci_mode = hsc_mode hsc_env
120 print_unqual = unQualInScope rdr_env
122 -- Desugarer warnings are SDocs; here we
123 -- add the info about whether or not to print unqualified
124 mk_warn :: (SrcSpan,SDoc) -> WarnMsg
125 mk_warn (loc, sdoc) = mkWarnMsg loc print_unqual sdoc
128 deSugarExpr :: HscEnv
129 -> Module -> GlobalRdrEnv -> TypeEnv
132 deSugarExpr hsc_env this_mod rdr_env type_env tc_expr
133 = do { showPass dflags "Desugar"
134 ; us <- mkSplitUniqSupply 'd'
137 ; let { is_boot = emptyModuleEnv } -- Assume no hi-boot files when
138 -- doing stuff from the command line
139 ; (core_expr, ds_warns) <- initDs hsc_env this_mod type_env is_boot $
142 -- Display any warnings
143 -- Note: if -Werror is used, we don't signal an error here.
144 ; doIfSet (not (isEmptyBag ds_warns))
145 (printErrs (pprBagOfWarnings (mapBag mk_warn ds_warns)))
148 ; dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr core_expr)
153 dflags = hsc_dflags hsc_env
154 print_unqual = unQualInScope rdr_env
156 mk_warn :: (SrcSpan,SDoc) -> WarnMsg
157 mk_warn (loc,sdoc) = mkWarnMsg loc print_unqual sdoc
160 dsProgram ghci_mode (TcGblEnv { tcg_exports = exports,
161 tcg_keep = keep_alive,
165 = dsHsBinds auto_scc binds [] `thenDs` \ core_prs ->
166 dsForeigns fords `thenDs` \ (ds_fords, foreign_prs) ->
168 all_prs = foreign_prs ++ core_prs
169 local_bndrs = mkVarSet (map fst all_prs)
171 mappM (dsRule local_bndrs) rules `thenDs` \ ds_rules ->
173 final_prs = addExportFlags ghci_mode exports keep_alive
174 local_bndrs all_prs ds_rules
175 ds_binds = [Rec final_prs]
176 -- Notice that we put the whole lot in a big Rec, even the foreign binds
177 -- When compiling PrelFloat, which defines data Float = F# Float#
178 -- we want F# to be in scope in the foreign marshalling code!
179 -- You might think it doesn't matter, but the simplifier brings all top-level
180 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
182 returnDs (ds_binds, ds_rules, ds_fords)
184 auto_scc | opt_SccProfilingOn = TopLevel
188 -- Set the no-discard flag if either
189 -- a) the Id is exported
190 -- b) it's mentioned in the RHS of an orphan rule
191 -- c) it's in the keep-alive set
193 -- It means that the binding won't be discarded EVEN if the binding
194 -- ends up being trivial (v = w) -- the simplifier would usually just
195 -- substitute w for v throughout, but we don't apply the substitution to
196 -- the rules (maybe we should?), so this substitution would make the rule
199 -- You might wonder why exported Ids aren't already marked as such;
200 -- it's just because the type checker is rather busy already and
201 -- I didn't want to pass in yet another mapping.
203 addExportFlags ghci_mode exports keep_alive bndrs prs rules
204 = [(add_export bndr, rhs) | (bndr,rhs) <- prs]
206 add_export bndr | dont_discard bndr = setIdLocalExported bndr
209 orph_rhs_fvs = unionVarSets [ ruleRhsFreeVars rule
210 | (id, rule) <- rules,
211 not (id `elemVarSet` bndrs) ]
212 -- An orphan rule must keep alive the free vars
213 -- of its right-hand side.
214 -- Non-orphan rules are attached to the Id (bndr_with_rules above)
215 -- and that keeps the rhs free vars alive
217 dont_discard bndr = is_exported name
218 || name `elemNameSet` keep_alive
219 || bndr `elemVarSet` orph_rhs_fvs
223 -- In interactive mode, we don't want to discard any top-level
224 -- entities at all (eg. do not inline them away during
225 -- simplification), and retain them all in the TypeEnv so they are
226 -- available from the command line.
228 -- isExternalName separates the user-defined top-level names from those
229 -- introduced by the type checker.
230 is_exported :: Name -> Bool
231 is_exported | ghci_mode == Interactive = isExternalName
232 | otherwise = (`elemNameSet` exports)
234 ppr_ds_rules [] = empty
236 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
242 %************************************************************************
244 %* Desugaring transformation rules
246 %************************************************************************
249 dsRule :: IdSet -> LRuleDecl Id -> DsM (Id, CoreRule)
250 dsRule in_scope (L loc (HsRule name act vars lhs rhs))
252 ds_lhs all_vars lhs `thenDs` \ (fn, args) ->
253 dsLExpr rhs `thenDs` \ core_rhs ->
254 returnDs (fn, Rule name act tpl_vars args core_rhs)
256 tpl_vars = [var | RuleBndr (L _ var) <- vars]
257 all_vars = mkInScopeSet (extendVarSetList in_scope tpl_vars)
263 (HsLet [HsBindGroup dict_binds _ _] body) -> (dict_binds, body)
264 other -> (emptyBag, lhs)
266 mappM ds_dict_bind (bagToList dict_binds) `thenDs` \ dict_binds' ->
267 dsLExpr body `thenDs` \ body' ->
269 -- Substitute the dict bindings eagerly,
270 -- and take the body apart into a (f args) form
272 subst_env = mkSubstEnv [id | (id,rhs) <- dict_binds']
273 [ContEx subst_env rhs | (id,rhs) <- dict_binds']
274 -- Note recursion here... substitution won't terminate
275 -- if there is genuine recursion... which there isn't
277 subst = mkSubst all_vars subst_env
278 body'' = substExpr subst body'
281 -- Now unpack the resulting body
283 pair = case collectArgs body'' of
284 (Var fn, args) -> (fn, args)
285 other -> pprPanic "dsRule" (ppr lhs)
289 ds_dict_bind (L _ (VarBind id rhs)) =
290 dsLExpr rhs `thenDs` \ rhs' ->