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