+ ; let { is_boot = emptyModuleEnv } -- Assume no hi-boot files when
+ -- doing stuff from the command line
+ ; (core_expr, ds_warns) <- initDs hsc_env this_mod type_env is_boot $
+ dsLExpr tc_expr
+
+ -- Display any warnings
+ -- Note: if -Werror is used, we don't signal an error here.
+ ; doIfSet (not (isEmptyBag ds_warns))
+ (printErrs (pprBagOfWarnings (mapBag mk_warn ds_warns)))
+
+ -- Dump output
+ ; dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr core_expr)
+
+ ; return core_expr
+ }
+ where
+ dflags = hsc_dflags hsc_env
+ print_unqual = unQualInScope rdr_env
+
+ mk_warn :: (SrcSpan,SDoc) -> WarnMsg
+ mk_warn (loc,sdoc) = mkWarnMsg loc print_unqual sdoc
+
+
+dsProgram ghci_mode (TcGblEnv { tcg_exports = exports,
+ tcg_keep = keep_alive,
+ tcg_binds = binds,
+ tcg_fords = fords,
+ tcg_rules = rules })
+ = dsHsBinds auto_scc binds [] `thenDs` \ core_prs ->
+ dsForeigns fords `thenDs` \ (ds_fords, foreign_prs) ->
+ let
+ all_prs = foreign_prs ++ core_prs
+ local_bndrs = mkVarSet (map fst all_prs)
+ in
+ mappM (dsRule local_bndrs) rules `thenDs` \ ds_rules ->
+ let
+ final_prs = addExportFlags ghci_mode exports keep_alive
+ local_bndrs all_prs ds_rules
+ ds_binds = [Rec final_prs]
+ -- Notice that we put the whole lot in a big Rec, even the foreign binds
+ -- When compiling PrelFloat, which defines data Float = F# Float#
+ -- we want F# to be in scope in the foreign marshalling code!
+ -- You might think it doesn't matter, but the simplifier brings all top-level
+ -- things into the in-scope set before simplifying; so we get no unfolding for F#!
+ in
+ returnDs (ds_binds, ds_rules, ds_fords)
+ where
+ auto_scc | opt_SccProfilingOn = TopLevel
+ | otherwise = NoSccs
+
+-- addExportFlags
+-- Set the no-discard flag if either
+-- a) the Id is exported
+-- b) it's mentioned in the RHS of an orphan rule
+-- c) it's in the keep-alive set
+--
+-- It means that the binding won't be discarded EVEN if the binding
+-- ends up being trivial (v = w) -- the simplifier would usually just
+-- substitute w for v throughout, but we don't apply the substitution to
+-- the rules (maybe we should?), so this substitution would make the rule
+-- bogus.
+
+-- You might wonder why exported Ids aren't already marked as such;
+-- it's just because the type checker is rather busy already and
+-- I didn't want to pass in yet another mapping.
+
+addExportFlags ghci_mode exports keep_alive bndrs prs rules
+ = [(add_export bndr, rhs) | (bndr,rhs) <- prs]
+ where
+ add_export bndr | dont_discard bndr = setIdLocalExported bndr
+ | otherwise = bndr