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