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(..),
13 Dependencies(..), TypeEnv,
15 import HsSyn ( MonoBinds, RuleDecl(..), RuleBndr(..),
16 HsExpr(..), HsBinds(..), MonoBinds(..) )
17 import TcHsSyn ( TypecheckedRuleDecl, TypecheckedHsExpr )
18 import TcRnTypes ( TcGblEnv(..), ImportAvails(..) )
19 import MkIface ( mkUsageInfo )
22 import PprCore ( pprIdRules, pprCoreExpr )
23 import Subst ( substExpr, mkSubst, mkInScopeSet )
25 import DsExpr ( dsExpr )
26 import DsBinds ( dsMonoBinds, 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 ( isEmptyBag, mapBag, emptyBag )
37 import CoreLint ( showPass, endPass )
38 import ErrUtils ( doIfSet, dumpIfSet_dyn, pprBagOfWarnings,
39 addShortWarnLocLine, errorsFound )
41 import qualified Pretty
42 import UniqSupply ( mkSplitUniqSupply )
43 import SrcLoc ( SrcLoc )
44 import DATA_IOREF ( readIORef )
48 %************************************************************************
50 %* The main function: deSugar
52 %************************************************************************
55 deSugar :: HscEnv -> TcGblEnv -> IO (Maybe ModGuts)
56 -- Can modify PCS by faulting in more declarations
59 (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,
72 = do { showPass dflags "Desugar"
75 ; let { is_boot = imp_dep_mods imports }
76 ; (results, warnings) <- initDs hsc_env mod type_env is_boot $
77 dsProgram binds rules fords
79 ; let { (ds_binds, ds_rules, ds_fords) = results
80 ; warns = mapBag mk_warn warnings
81 ; warn_doc = pprBagOfWarnings warns }
83 -- Display any warnings
84 ; doIfSet (not (isEmptyBag warnings))
87 -- if warnings are considered errors, leave.
88 ; if errorsFound dflags (warns, emptyBag)
92 -- Lint result if necessary
93 endPass dflags "Desugar" Opt_D_dump_ds ds_binds
96 ; doIfSet (dopt Opt_D_dump_ds dflags)
97 (printDump (ppr_ds_rules ds_rules))
99 ; dfun_uses <- readIORef dfun_uses_var -- What dfuns are used
100 ; let used_names = allUses dus emptyNameSet `unionNameSets` dfun_uses
101 ; usages <- mkUsageInfo hsc_env imports used_names
103 deps = Deps { dep_mods = moduleEnvElts (imp_dep_mods imports),
104 dep_pkgs = imp_dep_pkgs imports,
105 dep_orphs = imp_orphs imports }
108 mg_exports = exports,
111 mg_dir_imps = [m | (m,_) <- moduleEnvElts (imp_mods imports)],
112 mg_rdr_env = rdr_env,
113 mg_fix_env = fix_env,
114 mg_deprecs = deprecs,
119 mg_foreign = ds_fords }
121 ; return (Just mod_guts)
125 dflags = hsc_dflags hsc_env
126 print_unqual = unQualInScope rdr_env
128 -- Desugarer warnings are SDocs; here we
129 -- add the info about whether or not to print unqualified
130 mk_warn :: (SrcLoc,SDoc) -> (SrcLoc, Pretty.Doc)
131 mk_warn (loc, sdoc) = addShortWarnLocLine loc print_unqual sdoc
134 deSugarExpr :: HscEnv
135 -> Module -> GlobalRdrEnv -> TypeEnv
138 deSugarExpr hsc_env this_mod rdr_env type_env tc_expr
139 = do { showPass dflags "Desugar"
140 ; us <- mkSplitUniqSupply 'd'
143 ; let { is_boot = emptyModuleEnv } -- Assume no hi-boot files when
144 -- doing stuff from the command line
145 ; (core_expr, ds_warns) <- initDs hsc_env this_mod type_env is_boot $
148 -- Display any warnings
149 -- Note: if -Werror is used, we don't signal an error here.
150 ; doIfSet (not (isEmptyBag ds_warns))
151 (printErrs (pprBagOfWarnings (mapBag mk_warn ds_warns)))
154 ; dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr core_expr)
159 dflags = hsc_dflags hsc_env
160 print_unqual = unQualInScope rdr_env
162 mk_warn :: (SrcLoc,SDoc) -> (SrcLoc, Pretty.Doc)
163 mk_warn (loc,sdoc) = addShortWarnLocLine loc print_unqual sdoc
166 dsProgram all_binds rules fo_decls
167 = dsMonoBinds auto_scc all_binds [] `thenDs` \ core_prs ->
168 dsForeigns fo_decls `thenDs` \ (ds_fords, foreign_binds) ->
170 ds_binds = [Rec (foreign_binds ++ core_prs)]
171 -- Notice that we put the whole lot in a big Rec, even the foreign binds
172 -- When compiling PrelFloat, which defines data Float = F# Float#
173 -- we want F# to be in scope in the foreign marshalling code!
174 -- You might think it doesn't matter, but the simplifier brings all top-level
175 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
177 local_binders = mkVarSet (bindersOfBinds ds_binds)
179 mappM (dsRule local_binders) rules `thenDs` \ ds_rules ->
180 returnDs (ds_binds, ds_rules, ds_fords)
182 auto_scc | opt_SccProfilingOn = TopLevel
185 ppr_ds_rules [] = empty
187 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
193 %************************************************************************
195 %* Desugaring transformation rules
197 %************************************************************************
200 dsRule :: IdSet -> TypecheckedRuleDecl -> DsM (Id, CoreRule)
201 dsRule in_scope (HsRule name act vars lhs rhs loc)
203 ds_lhs all_vars lhs `thenDs` \ (fn, args) ->
204 dsExpr rhs `thenDs` \ core_rhs ->
205 returnDs (fn, Rule name act tpl_vars args core_rhs)
207 tpl_vars = [var | RuleBndr var <- vars]
208 all_vars = mkInScopeSet (in_scope `unionVarSet` mkVarSet tpl_vars)
212 (dict_binds, body) = case lhs of
213 (HsLet (MonoBind dict_binds _ _) body) -> (dict_binds, body)
214 other -> (EmptyMonoBinds, lhs)
216 ds_dict_binds dict_binds `thenDs` \ dict_binds' ->
217 dsExpr body `thenDs` \ body' ->
219 -- Substitute the dict bindings eagerly,
220 -- and take the body apart into a (f args) form
222 subst_env = mkSubstEnv [id | (id,rhs) <- dict_binds']
223 [ContEx subst_env rhs | (id,rhs) <- dict_binds']
224 -- Note recursion here... substitution won't terminate
225 -- if there is genuine recursion... which there isn't
227 subst = mkSubst all_vars subst_env
228 body'' = substExpr subst body'
231 -- Now unpack the resulting body
233 pair = case collectArgs body'' of
234 (Var fn, args) -> (fn, args)
235 other -> pprPanic "dsRule" (ppr lhs)
239 ds_dict_binds EmptyMonoBinds = returnDs []
240 ds_dict_binds (AndMonoBinds b1 b2) = ds_dict_binds b1 `thenDs` \ env1 ->
241 ds_dict_binds b2 `thenDs` \ env2 ->
242 returnDs (env1 ++ env2)
243 ds_dict_binds (VarMonoBind id rhs) = dsExpr rhs `thenDs` \ rhs' ->