2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Desugar]{@deSugar@: the main function}
7 module Desugar ( deSugar ) where
9 #include "HsVersions.h"
11 import CmdLineOpts ( DynFlags, DynFlag(..), dopt, opt_SccProfilingOn )
12 import HsSyn ( MonoBinds, RuleDecl(..), RuleBndr(..),
13 HsExpr(..), HsBinds(..), MonoBinds(..) )
14 import TcHsSyn ( TypecheckedRuleDecl )
15 import TcModule ( TcResults(..) )
18 import PprCore ( pprIdCoreRule )
19 import Subst ( substExpr, mkSubst, mkInScopeSet )
21 import DsExpr ( dsExpr )
22 import DsBinds ( dsMonoBinds, AutoScc(..) )
23 import DsForeign ( dsForeigns )
24 import DsExpr () -- Forces DsExpr to be compiled; DsBinds only
25 -- depends on DsExpr.hi-boot.
26 import Module ( Module )
30 import Bag ( isEmptyBag )
31 import CoreLint ( showPass, endPass )
32 import ErrUtils ( doIfSet, pprBagOfWarnings )
34 import UniqSupply ( mkSplitUniqSupply )
35 import HscTypes ( HomeSymbolTable )
38 %************************************************************************
40 %* The main function: deSugar
42 %************************************************************************
44 The only trick here is to get the @DsMonad@ stuff off to a good
49 -> Module -> PrintUnqualified
52 -> IO ([CoreBind], [(Id,CoreRule)], SDoc, SDoc, [CoreBndr])
54 deSugar dflags mod_name unqual hst
55 (TcResults {tc_env = global_val_env,
60 = do { showPass dflags "Desugar"
61 ; us <- mkSplitUniqSupply 'd'
64 ; let (result, ds_warns) = initDs dflags us (hst,pcs,global_val_env) mod_name
65 (dsProgram mod_name all_binds rules fo_decls)
66 (ds_binds, ds_rules, _, _, _) = result
68 -- Display any warnings
69 ; doIfSet (not (isEmptyBag ds_warns))
70 (printErrs unqual (pprBagOfWarnings ds_warns))
72 -- Lint result if necessary
73 ; let do_dump_ds = dopt Opt_D_dump_ds dflags
74 ; endPass dflags "Desugar" do_dump_ds ds_binds
77 ; doIfSet do_dump_ds (printDump (ppr_ds_rules ds_rules))
82 -- deSugarExpr dflags unqual hst tc_expr
85 dsProgram mod_name all_binds rules fo_decls
86 = dsMonoBinds auto_scc all_binds [] `thenDs` \ core_prs ->
87 dsForeigns mod_name fo_decls `thenDs` \ (fe_binders, foreign_binds, h_code, c_code) ->
89 ds_binds = [Rec (foreign_binds ++ core_prs)]
90 -- Notice that we put the whole lot in a big Rec, even the foreign binds
91 -- When compiling PrelFloat, which defines data Float = F# Float#
92 -- we want F# to be in scope in the foreign marshalling code!
93 -- You might think it doesn't matter, but the simplifier brings all top-level
94 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
96 local_binders = mkVarSet (bindersOfBinds ds_binds)
98 mapDs (dsRule local_binders) rules `thenDs` \ rules' ->
99 returnDs (ds_binds, rules', h_code, c_code, fe_binders)
101 auto_scc | opt_SccProfilingOn = TopLevel
104 ppr_ds_rules [] = empty
106 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
107 vcat (map pprIdCoreRule rules)
111 %************************************************************************
113 %* Desugaring transformation rules
115 %************************************************************************
118 dsRule :: IdSet -> TypecheckedRuleDecl -> DsM (Id, CoreRule)
119 dsRule in_scope (HsRule name sig_tvs vars lhs rhs loc)
121 ds_lhs all_vars lhs `thenDs` \ (fn, args) ->
122 dsExpr rhs `thenDs` \ core_rhs ->
123 returnDs (fn, Rule name tpl_vars args core_rhs)
125 tpl_vars = sig_tvs ++ [var | RuleBndr var <- vars]
126 all_vars = mkInScopeSet (in_scope `unionVarSet` mkVarSet tpl_vars)
130 (dict_binds, body) = case lhs of
131 (HsLet (MonoBind dict_binds _ _) body) -> (dict_binds, body)
132 other -> (EmptyMonoBinds, lhs)
134 ds_dict_binds dict_binds `thenDs` \ dict_binds' ->
135 dsExpr body `thenDs` \ body' ->
137 -- Substitute the dict bindings eagerly,
138 -- and take the body apart into a (f args) form
140 subst_env = mkSubstEnv [id | (id,rhs) <- dict_binds']
141 [ContEx subst_env rhs | (id,rhs) <- dict_binds']
142 -- Note recursion here... substitution won't terminate
143 -- if there is genuine recursion... which there isn't
145 subst = mkSubst all_vars subst_env
146 body'' = substExpr subst body'
149 -- Now unpack the resulting body
151 pair = case collectArgs body'' of
152 (Var fn, args) -> (fn, args)
153 other -> pprPanic "dsRule" (ppr lhs)
157 ds_dict_binds EmptyMonoBinds = returnDs []
158 ds_dict_binds (AndMonoBinds b1 b2) = ds_dict_binds b1 `thenDs` \ env1 ->
159 ds_dict_binds b2 `thenDs` \ env2 ->
160 returnDs (env1 ++ env2)
161 ds_dict_binds (VarMonoBind id rhs) = dsExpr rhs `thenDs` \ rhs' ->