64fff0d0129fe1d34798e3e7f96b87ad7ebe0c2f
[ghc-hetmet.git] / compiler / deSugar / Desugar.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 The Desugarer: turning HsSyn into Core.
7
8 \begin{code}
9 module Desugar ( deSugar, deSugarExpr ) where
10
11 import DynFlags
12 import StaticFlags
13 import HscTypes
14 import HsSyn
15 import TcRnTypes
16 import MkIface
17 import Id
18 import Name
19 import CoreSyn
20 import PprCore
21 import DsMonad
22 import DsExpr
23 import DsBinds
24 import DsForeign
25 import DsExpr           ()      -- Forces DsExpr to be compiled; DsBinds only
26                                 -- depends on DsExpr.hi-boot.
27 import Module
28 import RdrName
29 import NameSet
30 import Rules
31 import CoreMonad        ( endPass, CoreToDo(..) )
32 import ErrUtils
33 import Outputable
34 import SrcLoc
35 import Maybes
36 import FastString
37 import Coverage
38
39 import Data.IORef
40 \end{code}
41
42 %************************************************************************
43 %*                                                                      *
44 %*              The main function: deSugar
45 %*                                                                      *
46 %************************************************************************
47
48 \begin{code}
49 -- | Main entry point to the desugarer.
50 deSugar :: HscEnv -> ModLocation -> TcGblEnv -> IO (Messages, Maybe ModGuts)
51 -- Can modify PCS by faulting in more declarations
52
53 deSugar hsc_env 
54         mod_loc
55         tcg_env@(TcGblEnv { tcg_mod          = mod,
56                             tcg_src          = hsc_src,
57                             tcg_type_env     = type_env,
58                             tcg_imports      = imports,
59                             tcg_exports      = exports,
60                             tcg_keep         = keep_var,
61                             tcg_rdr_env      = rdr_env,
62                             tcg_fix_env      = fix_env,
63                             tcg_inst_env     = inst_env,
64                             tcg_fam_inst_env = fam_inst_env,
65                             tcg_warns        = warns,
66                             tcg_anns         = anns,
67                             tcg_binds        = binds,
68                             tcg_fords        = fords,
69                             tcg_rules        = rules,
70                             tcg_insts        = insts,
71                             tcg_fam_insts    = fam_insts,
72                             tcg_hpc          = other_hpc_info })
73
74   = do  { let dflags = hsc_dflags hsc_env
75         ; showPass dflags "Desugar"
76
77         -- Desugar the program
78         ; let export_set = availsToNameSet exports
79         ; let auto_scc = mkAutoScc dflags mod export_set
80         ; let target = hscTarget dflags
81         ; let hpcInfo = emptyHpcInfo other_hpc_info
82         ; (msgs, mb_res)
83               <- case target of
84                    HscNothing ->
85                        return (emptyMessages,
86                                Just ([], [], NoStubs, hpcInfo, emptyModBreaks))
87                    _        -> do
88                      (binds_cvr,ds_hpc_info, modBreaks)
89                          <- if (opt_Hpc
90                                   || target == HscInterpreted)
91                                && (not (isHsBoot hsc_src))
92                               then addCoverageTicksToBinds dflags mod mod_loc (typeEnvTyCons type_env) binds 
93                               else return (binds, hpcInfo, emptyModBreaks)
94                      initDs hsc_env mod rdr_env type_env $ do
95                          core_prs <- dsTopLHsBinds auto_scc binds_cvr
96                          (ds_fords, foreign_prs) <- dsForeigns fords
97                          let all_prs = foreign_prs ++ core_prs
98                          ds_rules <- mapM dsRule rules
99                          return (all_prs, catMaybes ds_rules, ds_fords, ds_hpc_info, modBreaks)
100
101         ; case mb_res of {
102            Nothing -> return (msgs, Nothing) ;
103            Just (all_prs, ds_rules, ds_fords,ds_hpc_info, modBreaks) -> do
104
105         {       -- Add export flags to bindings
106           keep_alive <- readIORef keep_var
107         ; let final_prs = addExportFlags target export_set
108                                  keep_alive all_prs 
109               ds_binds  = [Rec final_prs]
110         -- Notice that we put the whole lot in a big Rec, even the foreign binds
111         -- When compiling PrelFloat, which defines data Float = F# Float#
112         -- we want F# to be in scope in the foreign marshalling code!
113         -- You might think it doesn't matter, but the simplifier brings all top-level
114         -- things into the in-scope set before simplifying; so we get no unfolding for F#!
115
116         -- Lint result if necessary
117         ; endPass dflags CoreDesugar ds_binds ds_rules
118
119         -- Dump output
120         ; doIfSet (dopt Opt_D_dump_ds dflags) 
121                   (printDump (ppr_ds_rules ds_rules))
122
123         ; used_names <- mkUsedNames tcg_env
124         ; deps <- mkDependencies tcg_env
125
126         ; let mod_guts = ModGuts {      
127                 mg_module       = mod,
128                 mg_boot         = isHsBoot hsc_src,
129                 mg_exports      = exports,
130                 mg_deps         = deps,
131                 mg_used_names   = used_names,
132                 mg_dir_imps     = imp_mods imports,
133                 mg_rdr_env      = rdr_env,
134                 mg_fix_env      = fix_env,
135                 mg_warns        = warns,
136                 mg_anns         = anns,
137                 mg_types        = type_env,
138                 mg_insts        = insts,
139                 mg_fam_insts    = fam_insts,
140                 mg_inst_env     = inst_env,
141                 mg_fam_inst_env = fam_inst_env,
142                 mg_rules        = ds_rules,
143                 mg_binds        = ds_binds,
144                 mg_foreign      = ds_fords,
145                 mg_hpc_info     = ds_hpc_info,
146                 mg_modBreaks    = modBreaks,
147                 mg_vect_info    = noVectInfo
148               }
149         ; return (msgs, Just mod_guts)
150         }}}
151
152 mkAutoScc :: DynFlags -> Module -> NameSet -> AutoScc
153 mkAutoScc dflags mod exports
154   | not opt_SccProfilingOn      -- No profiling
155   = NoSccs              
156     -- Add auto-scc on all top-level things
157   | dopt Opt_AutoSccsOnAllToplevs dflags
158   = AddSccs mod (\id -> not $ isDerivedOccName $ getOccName id)
159     -- See #1641.  This is pretty yucky, but I can't see a better way
160     -- to identify compiler-generated Ids, and at least this should
161     -- catch them all.
162     -- Only on exported things
163   | dopt Opt_AutoSccsOnExportedToplevs dflags
164   = AddSccs mod (\id -> idName id `elemNameSet` exports)
165   | otherwise
166   = NoSccs
167
168 deSugarExpr :: HscEnv
169             -> Module -> GlobalRdrEnv -> TypeEnv 
170             -> LHsExpr Id
171             -> IO (Messages, Maybe CoreExpr)
172 -- Prints its own errors; returns Nothing if error occurred
173
174 deSugarExpr hsc_env this_mod rdr_env type_env tc_expr = do
175     let dflags = hsc_dflags hsc_env
176     showPass dflags "Desugar"
177
178     -- Do desugaring
179     (msgs, mb_core_expr) <- initDs hsc_env this_mod rdr_env type_env $
180                                    dsLExpr tc_expr
181
182     case mb_core_expr of
183       Nothing   -> return (msgs, Nothing)
184       Just expr -> do
185
186         -- Dump output
187         dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr expr)
188
189         return (msgs, Just expr)
190
191 --              addExportFlags
192 -- Set the no-discard flag if either 
193 --      a) the Id is exported
194 --      b) it's mentioned in the RHS of an orphan rule
195 --      c) it's in the keep-alive set
196 --
197 -- It means that the binding won't be discarded EVEN if the binding
198 -- ends up being trivial (v = w) -- the simplifier would usually just 
199 -- substitute w for v throughout, but we don't apply the substitution to
200 -- the rules (maybe we should?), so this substitution would make the rule
201 -- bogus.
202
203 -- You might wonder why exported Ids aren't already marked as such;
204 -- it's just because the type checker is rather busy already and
205 -- I didn't want to pass in yet another mapping.
206
207 addExportFlags :: HscTarget -> NameSet -> NameSet -> [(Id, t)]
208                -> [(Id, t)]
209 addExportFlags target exports keep_alive prs
210   = [(add_export bndr, rhs) | (bndr,rhs) <- prs]
211   where
212     add_export bndr
213         | dont_discard bndr = setIdExported bndr
214         | otherwise         = bndr
215
216     dont_discard bndr = is_exported name
217                      || name `elemNameSet` keep_alive
218                      where
219                         name = idName bndr
220
221         -- In interactive mode, we don't want to discard any top-level
222         -- entities at all (eg. do not inline them away during
223         -- simplification), and retain them all in the TypeEnv so they are
224         -- available from the command line.
225         --
226         -- isExternalName separates the user-defined top-level names from those
227         -- introduced by the type checker.
228     is_exported :: Name -> Bool
229     is_exported | target == HscInterpreted = isExternalName
230                 | otherwise                = (`elemNameSet` exports)
231
232 ppr_ds_rules :: [CoreRule] -> SDoc
233 ppr_ds_rules [] = empty
234 ppr_ds_rules rules
235   = blankLine $$ text "-------------- DESUGARED RULES -----------------" $$
236     pprRules rules
237 \end{code}
238
239
240
241 %************************************************************************
242 %*                                                                      *
243 %*              Desugaring transformation rules
244 %*                                                                      *
245 %************************************************************************
246
247 \begin{code}
248 dsRule :: LRuleDecl Id -> DsM (Maybe CoreRule)
249 dsRule (L loc (HsRule name act vars lhs _tv_lhs rhs _fv_rhs))
250   = putSrcSpanDs loc $ 
251     do  { let bndrs' = [var | RuleBndr (L _ var) <- vars]
252
253         ; lhs'  <- unsetOptM Opt_EnableRewriteRules $
254                    dsLExpr lhs  -- Note [Desugaring RULE lhss]
255
256         ; rhs'  <- dsLExpr rhs
257
258         -- Substitute the dict bindings eagerly,
259         -- and take the body apart into a (f args) form
260         ; case decomposeRuleLhs (mkLams bndrs' lhs') of {
261                 Nothing -> do { warnDs msg; return Nothing } ;
262                 Just (bndrs, fn_id, args) -> do
263         
264         { let local_rule = isLocalId fn_id
265                 -- NB: isLocalId is False of implicit Ids.  This is good becuase
266                 -- we don't want to attach rules to the bindings of implicit Ids, 
267                 -- because they don't show up in the bindings until just before code gen
268               fn_name = idName fn_id
269               rule    = mkRule local_rule name act fn_name bndrs args rhs' 
270         ; return (Just rule)
271         } } }
272   where
273     msg = hang (ptext (sLit "RULE left-hand side too complicated to desugar; ignored"))
274              2 (ppr lhs)
275 \end{code}
276
277 Note [Desugaring RULE left hand sides]
278 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
279 For the LHS of a RULE we do *not* want to desugar
280     [x]   to    build (\cn. x `c` n)
281 We want to leave explicit lists simply as chains
282 of cons's. We can achieve that slightly indirectly by
283 switching off EnableRewriteRules.  See DsExpr.dsExplicitList.
284
285 That keeps the desugaring of list comprehensions simple too.