2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Desugar]{@deSugar@: the main function}
7 module Desugar ( deSugar, deSugarExpr,
10 #include "HsVersions.h"
12 import CmdLineOpts ( DynFlags, DynFlag(..), dopt, opt_SccProfilingOn )
13 import HscTypes ( ModDetails(..), TypeEnv )
14 import HsSyn ( MonoBinds, RuleDecl(..), RuleBndr(..),
15 HsExpr(..), HsBinds(..), MonoBinds(..) )
16 import TcHsSyn ( TypecheckedRuleDecl, TypecheckedHsExpr,
18 import TcModule ( TcResults(..) )
21 import PprCore ( pprIdRules, pprCoreExpr )
22 import Subst ( substExpr, mkSubst, mkInScopeSet )
24 import DsExpr ( dsExpr )
25 import DsBinds ( dsMonoBinds, AutoScc(..) )
26 import DsForeign ( dsForeigns )
27 import DsExpr () -- Forces DsExpr to be compiled; DsBinds only
28 -- depends on DsExpr.hi-boot.
29 import Module ( Module )
31 import NameEnv ( lookupNameEnv )
34 import Bag ( isEmptyBag )
35 import CoreLint ( showPass, endPass )
36 import ErrUtils ( doIfSet, dumpIfSet_dyn, pprBagOfWarnings )
38 import UniqSupply ( mkSplitUniqSupply )
39 import HscTypes ( HomeSymbolTable, PersistentCompilerState(..), TyThing(..), lookupType, )
42 %************************************************************************
44 %* The main function: deSugar
46 %************************************************************************
48 The only trick here is to get the @DsMonad@ stuff off to a good
53 -> PersistentCompilerState -> HomeSymbolTable
54 -> Module -> PrintUnqualified
56 -> IO (ModDetails, (SDoc, SDoc, [FAST_STRING], [CoreBndr]))
58 deSugar dflags pcs hst mod_name unqual
59 (TcResults {tc_env = type_env,
63 -- tc_cbinds = core_binds,
65 = do { showPass dflags "Desugar"
66 ; us <- mkSplitUniqSupply 'd'
69 ; let (ds_result, ds_warns) = initDs dflags us lookup mod_name
70 (dsProgram mod_name all_binds rules fo_decls)
72 (ds_binds, ds_rules, foreign_stuff) = ds_result
80 mod_details = ModDetails { md_types = type_env,
85 -- Display any warnings
86 ; doIfSet (not (isEmptyBag ds_warns))
87 (printErrs unqual (pprBagOfWarnings ds_warns))
89 -- Lint result if necessary
90 ; endPass dflags "Desugar" Opt_D_dump_ds ds_binds
93 ; doIfSet (dopt Opt_D_dump_ds dflags)
94 (printDump (ppr_ds_rules ds_rules))
96 ; return (mod_details, foreign_stuff)
100 -- The lookup function passed to initDs is used for well-known Ids,
101 -- such as fold, build, cons etc, so the chances are
102 -- it'll be found in the package symbol table. That's
103 -- why we don't merge all these tables
105 lookup n = case lookupType hst pte n of {
108 case lookupNameEnv type_env n of
110 other -> pprPanic "Desugar: lookup:" (ppr n)
113 deSugarExpr :: DynFlags
114 -> PersistentCompilerState -> HomeSymbolTable
115 -> Module -> PrintUnqualified
118 deSugarExpr dflags pcs hst mod_name unqual tc_expr
119 = do { showPass dflags "Desugar"
120 ; us <- mkSplitUniqSupply 'd'
123 ; let (core_expr, ds_warns) = initDs dflags us lookup mod_name (dsExpr tc_expr)
125 -- Display any warnings
126 ; doIfSet (not (isEmptyBag ds_warns))
127 (printErrs unqual (pprBagOfWarnings ds_warns))
130 ; dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr core_expr)
136 lookup n = case lookupType hst pte n of
138 other -> pprPanic "Desugar: lookup:" (ppr n)
140 dsProgram mod_name all_binds rules fo_decls
141 = dsMonoBinds auto_scc all_binds [] `thenDs` \ core_prs ->
142 dsForeigns mod_name fo_decls `thenDs` \ (fe_binders, foreign_binds, h_code, c_code, headers) ->
144 ds_binds = [Rec (foreign_binds ++ core_prs)]
145 -- Notice that we put the whole lot in a big Rec, even the foreign binds
146 -- When compiling PrelFloat, which defines data Float = F# Float#
147 -- we want F# to be in scope in the foreign marshalling code!
148 -- You might think it doesn't matter, but the simplifier brings all top-level
149 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
151 local_binders = mkVarSet (bindersOfBinds ds_binds)
153 mapDs (dsRule local_binders) rules `thenDs` \ rules' ->
154 returnDs (ds_binds, rules', (h_code, c_code, headers, fe_binders))
156 auto_scc | opt_SccProfilingOn = TopLevel
159 ppr_ds_rules [] = empty
161 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
165 Simplest thing in the world, desugaring External Core:
168 deSugarCore :: TypeEnv -> [TypecheckedCoreBind]
169 -> IO (ModDetails, (SDoc, SDoc, [FAST_STRING], [CoreBndr]))
170 deSugarCore type_env cs = do
173 = ModDetails { md_types = type_env
176 , md_binds = [Rec (map (\ (lhs,_,rhs) -> (lhs,rhs)) cs)]
179 no_foreign_stuff = (empty,empty,[],[])
180 return (mod_details, no_foreign_stuff)
185 %************************************************************************
187 %* Desugaring transformation rules
189 %************************************************************************
192 dsRule :: IdSet -> TypecheckedRuleDecl -> DsM (Id, CoreRule)
193 dsRule in_scope (IfaceRuleOut fun rule) -- Built-in rules come this way
194 = returnDs (fun, rule)
196 dsRule in_scope (HsRule name act vars lhs rhs loc)
198 ds_lhs all_vars lhs `thenDs` \ (fn, args) ->
199 dsExpr rhs `thenDs` \ core_rhs ->
200 returnDs (fn, Rule name act tpl_vars args core_rhs)
202 tpl_vars = [var | RuleBndr var <- vars]
203 all_vars = mkInScopeSet (in_scope `unionVarSet` mkVarSet tpl_vars)
207 (dict_binds, body) = case lhs of
208 (HsLet (MonoBind dict_binds _ _) body) -> (dict_binds, body)
209 other -> (EmptyMonoBinds, lhs)
211 ds_dict_binds dict_binds `thenDs` \ dict_binds' ->
212 dsExpr body `thenDs` \ body' ->
214 -- Substitute the dict bindings eagerly,
215 -- and take the body apart into a (f args) form
217 subst_env = mkSubstEnv [id | (id,rhs) <- dict_binds']
218 [ContEx subst_env rhs | (id,rhs) <- dict_binds']
219 -- Note recursion here... substitution won't terminate
220 -- if there is genuine recursion... which there isn't
222 subst = mkSubst all_vars subst_env
223 body'' = substExpr subst body'
226 -- Now unpack the resulting body
228 pair = case collectArgs body'' of
229 (Var fn, args) -> (fn, args)
230 other -> pprPanic "dsRule" (ppr lhs)
234 ds_dict_binds EmptyMonoBinds = returnDs []
235 ds_dict_binds (AndMonoBinds b1 b2) = ds_dict_binds b1 `thenDs` \ env1 ->
236 ds_dict_binds b2 `thenDs` \ env2 ->
237 returnDs (env1 ++ env2)
238 ds_dict_binds (VarMonoBind id rhs) = dsExpr rhs `thenDs` \ rhs' ->