[project @ 2000-10-25 14:42:31 by sewardj]
[ghc-hetmet.git] / ghc / compiler / deSugar / Desugar.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[Desugar]{@deSugar@: the main function}
5
6 \begin{code}
7 module Desugar ( deSugar ) where
8
9 #include "HsVersions.h"
10
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(..) )
16 import Id               ( Id )
17 import CoreSyn
18 import PprCore          ( pprIdCoreRule )
19 import Subst            ( substExpr, mkSubst, mkInScopeSet )
20 import DsMonad
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 )
27 import Id               ( Id )
28 import VarEnv
29 import VarSet
30 import Bag              ( isEmptyBag )
31 import CoreLint         ( beginPass, endPass )
32 import ErrUtils         ( doIfSet, pprBagOfWarnings )
33 import Outputable
34 import UniqSupply       ( UniqSupply )
35 import HscTypes         ( HomeSymbolTable )
36 \end{code}
37
38 %************************************************************************
39 %*                                                                      *
40 %*              The main function: deSugar
41 %*                                                                      *
42 %************************************************************************
43
44 The only trick here is to get the @DsMonad@ stuff off to a good
45 start.
46
47 \begin{code}
48 deSugar :: DynFlags
49         -> Module 
50         -> UniqSupply
51         -> HomeSymbolTable
52         -> TcResults
53         -> IO ([CoreBind], [(Id,CoreRule)], SDoc, SDoc, [CoreBndr])
54
55 deSugar dflags mod_name us hst
56         (TcResults {tc_env   = global_val_env,
57                     tc_pcs   = pcs,
58                     tc_binds = all_binds,
59                     tc_rules = rules,
60                     tc_fords = fo_decls})
61   = do
62         beginPass dflags "Desugar"
63         -- Do desugaring
64         let (result, ds_warns) = 
65                 initDs dflags us (hst,pcs,global_val_env) mod_name
66                         (dsProgram mod_name all_binds rules fo_decls)    
67             (ds_binds, ds_rules, _, _, _) = result
68
69          -- Display any warnings
70         doIfSet (not (isEmptyBag ds_warns))
71                 (printErrs (pprBagOfWarnings ds_warns))
72
73          -- Lint result if necessary
74         let do_dump_ds = dopt Opt_D_dump_ds dflags
75         endPass dflags "Desugar" do_dump_ds ds_binds
76
77         doIfSet do_dump_ds (printDump (ppr_ds_rules ds_rules))
78
79         return result
80
81 dsProgram mod_name all_binds rules fo_decls
82   = dsMonoBinds auto_scc all_binds []   `thenDs` \ core_prs ->
83     dsForeigns mod_name fo_decls        `thenDs` \ (fe_binders, foreign_binds, h_code, c_code) ->
84     let
85         ds_binds      = [Rec (foreign_binds ++ core_prs)]
86         -- Notice that we put the whole lot in a big Rec, even the foreign binds
87         -- When compiling PrelFloat, which defines data Float = F# Float#
88         -- we want F# to be in scope in the foreign marshalling code!
89         -- You might think it doesn't matter, but the simplifier brings all top-level
90         -- things into the in-scope set before simplifying; so we get no unfolding for F#!
91
92         local_binders = mkVarSet (bindersOfBinds ds_binds)
93     in
94     mapDs (dsRule local_binders) rules  `thenDs` \ rules' ->
95     returnDs (ds_binds, rules', h_code, c_code, fe_binders)
96   where
97     auto_scc | opt_SccProfilingOn = TopLevel
98              | otherwise          = NoSccs
99
100 ppr_ds_rules [] = empty
101 ppr_ds_rules rules
102   = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
103     vcat (map pprIdCoreRule rules)
104 \end{code}
105
106
107 %************************************************************************
108 %*                                                                      *
109 %*              Desugaring transformation rules
110 %*                                                                      *
111 %************************************************************************
112
113 \begin{code}
114 dsRule :: IdSet -> TypecheckedRuleDecl -> DsM (Id, CoreRule)
115 dsRule in_scope (HsRule name sig_tvs vars lhs rhs loc)
116   = putSrcLocDs loc             $
117     ds_lhs all_vars lhs         `thenDs` \ (fn, args) ->
118     dsExpr rhs                  `thenDs` \ core_rhs ->
119     returnDs (fn, Rule name tpl_vars args core_rhs)
120   where
121     tpl_vars = sig_tvs ++ [var | RuleBndr var <- vars]
122     all_vars = mkInScopeSet (in_scope `unionVarSet` mkVarSet tpl_vars)
123
124 ds_lhs all_vars lhs
125   = let
126         (dict_binds, body) = case lhs of
127                 (HsLet (MonoBind dict_binds _ _) body) -> (dict_binds, body)
128                 other                                  -> (EmptyMonoBinds, lhs)
129     in
130     ds_dict_binds dict_binds    `thenDs` \ dict_binds' ->
131     dsExpr body                 `thenDs` \ body' ->
132
133         -- Substitute the dict bindings eagerly,
134         -- and take the body apart into a (f args) form
135     let
136         subst_env = mkSubstEnv [id                   | (id,rhs) <- dict_binds']
137                                [ContEx subst_env rhs | (id,rhs) <- dict_binds']
138                         -- Note recursion here... substitution won't terminate
139                         -- if there is genuine recursion... which there isn't
140
141         subst = mkSubst all_vars subst_env
142         body'' = substExpr subst body'
143     in
144         
145         -- Now unpack the resulting body
146     let
147         pair = case collectArgs body'' of
148                         (Var fn, args) -> (fn, args)
149                         other          -> pprPanic "dsRule" (ppr lhs)
150     in
151     returnDs pair
152
153 ds_dict_binds EmptyMonoBinds       = returnDs []
154 ds_dict_binds (AndMonoBinds b1 b2) = ds_dict_binds b1   `thenDs` \ env1 ->
155                                      ds_dict_binds b2   `thenDs` \ env2 ->
156                                      returnDs (env1 ++ env2)
157 ds_dict_binds (VarMonoBind id rhs) = dsExpr rhs         `thenDs` \ rhs' ->
158                                      returnDs [(id,rhs')]
159 \end{code}