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