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