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(..), ExternalPackageState(..),
13 PersistentCompilerState(..),
14 lookupType, unQualInScope )
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 )
32 import NameEnv ( lookupNameEnv )
35 import Bag ( isEmptyBag, mapBag )
36 import CoreLint ( showPass, endPass )
37 import ErrUtils ( doIfSet, dumpIfSet_dyn, pprBagOfWarnings, addShortWarnLocLine )
39 import qualified Pretty
40 import UniqSupply ( mkSplitUniqSupply )
41 import SrcLoc ( SrcLoc )
43 import DATA_IOREF ( readIORef )
46 %************************************************************************
48 %* The main function: deSugar
50 %************************************************************************
53 deSugar :: HscEnv -> PersistentCompilerState
54 -> TcGblEnv -> IO ModGuts
57 (TcGblEnv { tcg_mod = mod,
58 tcg_type_env = type_env,
59 tcg_usages = usage_var,
60 tcg_imports = imports,
61 tcg_exports = exports,
62 tcg_rdr_env = rdr_env,
63 tcg_fix_env = fix_env,
64 tcg_deprecs = deprecs,
69 = do { showPass dflags "Desugar"
70 ; us <- mkSplitUniqSupply 'd'
71 ; usages <- readIORef usage_var
74 ; let ((ds_binds, ds_rules, ds_fords), ds_warns)
75 = initDs dflags us lookup mod
76 (dsProgram binds rules fords)
78 warn_doc = pprBagOfWarnings (mapBag mk_warn ds_warns)
80 -- Display any warnings
81 ; doIfSet (not (isEmptyBag ds_warns))
84 -- Lint result if necessary
85 ; endPass dflags "Desugar" Opt_D_dump_ds ds_binds
88 ; doIfSet (dopt Opt_D_dump_ds dflags)
89 (printDump (ppr_ds_rules ds_rules))
92 deps = (moduleEnvElts (dep_mods imports), dep_pkgs imports)
97 mg_usages = mkUsageInfo hsc_env eps imports usages,
98 mg_dir_imps = [m | (m,_) <- moduleEnvElts (imp_mods imports)],
100 mg_fix_env = fix_env,
101 mg_deprecs = deprecs,
106 mg_foreign = ds_fords }
112 dflags = hsc_dflags hsc_env
113 print_unqual = unQualInScope rdr_env
115 -- Desugarer warnings are SDocs; here we
116 -- add the info about whether or not to print unqualified
117 mk_warn :: (SrcLoc,SDoc) -> (SrcLoc, Pretty.Doc)
118 mk_warn (loc, sdoc) = addShortWarnLocLine loc print_unqual sdoc
120 -- The lookup function passed to initDs is used for well-known Ids,
121 -- such as fold, build, cons etc, so the chances are
122 -- it'll be found in the package symbol table. That's
123 -- why we don't merge all these tables
126 hpt = hsc_HPT hsc_env
127 lookup n = case lookupType hpt pte n of {
130 case lookupNameEnv type_env n of
132 other -> pprPanic "Desugar: lookup:" (ppr n)
135 deSugarExpr :: HscEnv
136 -> PersistentCompilerState
137 -> Module -> PrintUnqualified
140 deSugarExpr hsc_env pcs mod_name unqual tc_expr
141 = do { showPass dflags "Desugar"
142 ; us <- mkSplitUniqSupply 'd'
145 ; let (core_expr, ds_warns) = initDs dflags us lookup mod_name (dsExpr tc_expr)
146 warn_doc = pprBagOfWarnings (mapBag mk_warn ds_warns)
147 mk_warn :: (SrcLoc,SDoc) -> (SrcLoc, Pretty.Doc)
148 mk_warn (loc,sdoc) = addShortWarnLocLine loc unqual sdoc
150 -- Display any warnings
151 ; doIfSet (not (isEmptyBag ds_warns))
155 ; dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr core_expr)
160 dflags = hsc_dflags hsc_env
161 hpt = hsc_HPT hsc_env
162 pte = eps_PTE (pcs_EPS pcs)
163 lookup n = case lookupType hpt pte n of
165 other -> pprPanic "Desugar: lookup:" (ppr n)
167 dsProgram all_binds rules fo_decls
168 = dsMonoBinds auto_scc all_binds [] `thenDs` \ core_prs ->
169 dsForeigns fo_decls `thenDs` \ (ds_fords, foreign_binds) ->
171 ds_binds = [Rec (foreign_binds ++ core_prs)]
172 -- Notice that we put the whole lot in a big Rec, even the foreign binds
173 -- When compiling PrelFloat, which defines data Float = F# Float#
174 -- we want F# to be in scope in the foreign marshalling code!
175 -- You might think it doesn't matter, but the simplifier brings all top-level
176 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
178 local_binders = mkVarSet (bindersOfBinds ds_binds)
180 mapDs (dsRule local_binders) rules `thenDs` \ ds_rules ->
181 returnDs (ds_binds, ds_rules, ds_fords)
183 auto_scc | opt_SccProfilingOn = TopLevel
186 ppr_ds_rules [] = empty
188 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
194 %************************************************************************
196 %* Desugaring transformation rules
198 %************************************************************************
201 dsRule :: IdSet -> TypecheckedRuleDecl -> DsM (Id, CoreRule)
202 dsRule in_scope (IfaceRuleOut fun rule) -- Built-in rules come this way
203 = returnDs (fun, rule)
205 dsRule in_scope (HsRule name act vars lhs rhs loc)
207 ds_lhs all_vars lhs `thenDs` \ (fn, args) ->
208 dsExpr rhs `thenDs` \ core_rhs ->
209 returnDs (fn, Rule name act tpl_vars args core_rhs)
211 tpl_vars = [var | RuleBndr var <- vars]
212 all_vars = mkInScopeSet (in_scope `unionVarSet` mkVarSet tpl_vars)
216 (dict_binds, body) = case lhs of
217 (HsLet (MonoBind dict_binds _ _) body) -> (dict_binds, body)
218 other -> (EmptyMonoBinds, lhs)
220 ds_dict_binds dict_binds `thenDs` \ dict_binds' ->
221 dsExpr body `thenDs` \ body' ->
223 -- Substitute the dict bindings eagerly,
224 -- and take the body apart into a (f args) form
226 subst_env = mkSubstEnv [id | (id,rhs) <- dict_binds']
227 [ContEx subst_env rhs | (id,rhs) <- dict_binds']
228 -- Note recursion here... substitution won't terminate
229 -- if there is genuine recursion... which there isn't
231 subst = mkSubst all_vars subst_env
232 body'' = substExpr subst body'
235 -- Now unpack the resulting body
237 pair = case collectArgs body'' of
238 (Var fn, args) -> (fn, args)
239 other -> pprPanic "dsRule" (ppr lhs)
243 ds_dict_binds EmptyMonoBinds = returnDs []
244 ds_dict_binds (AndMonoBinds b1 b2) = ds_dict_binds b1 `thenDs` \ env1 ->
245 ds_dict_binds b2 `thenDs` \ env2 ->
246 returnDs (env1 ++ env2)
247 ds_dict_binds (VarMonoBind id rhs) = dsExpr rhs `thenDs` \ rhs' ->