-cmSetContext cmstate dflags toplevs exports = do
- let CmState{ hpt=hpt, pcs=pcs, ic=old_ic } = cmstate
- hsc_env = HscEnv { hsc_mode = Interactive, hsc_dflags = dflags,
- hsc_HPT = hpt }
-
- toplev_mods <- mapM (getTopLevModule hpt) (map mkModuleName toplevs)
- export_mods <- mapM (moduleNameToModule hpt) (map mkModuleName exports)
-
- (new_pcs, maybe_env)
- <- mkGlobalContext hsc_env pcs toplev_mods export_mods
-
- case maybe_env of
- Nothing -> return cmstate
- Just env -> return cmstate{ pcs = new_pcs,
- ic = old_ic{ ic_toplev_scope = toplev_mods,
- ic_exports = export_mods,
- ic_rn_gbl_env = env } }
-
-getTopLevModule hpt mn =
- case lookupModuleEnvByName hpt mn of
-
- Just mod_info
- | isJust (mi_globals iface) -> return (mi_module iface)
- where
- iface = hm_iface mod_info
-
- _other -> throwDyn (CmdLineError (
- "cannot enter the top-level scope of a compiled module (module `" ++
- moduleNameUserString mn ++ "')"))
-
-moduleNameToModule :: HomePackageTable -> ModuleName -> IO Module
-moduleNameToModule hpt mn = do
- case lookupModuleEnvByName hpt mn of
- Just mod_info -> return (mi_module (hm_iface mod_info))
- _not_a_home_module -> do
- maybe_stuff <- findModule mn
- case maybe_stuff of
- Nothing -> throwDyn (CmdLineError ("can't find module `"
- ++ moduleNameUserString mn ++ "'"))
- Just (m,_) -> return m
+cmSetContext cmstate toplevs exports = do
+ let old_ic = cm_ic cmstate
+ hsc_env = cm_hsc cmstate
+ hpt = hsc_HPT hsc_env
+
+ export_env <- mkExportEnv hsc_env (map mkModuleName exports)
+ toplev_envs <- mapM (mkTopLevEnv hpt) toplevs
+
+ let all_env = foldr plusGlobalRdrEnv export_env toplev_envs
+ return cmstate{ cm_ic = old_ic { ic_toplev_scope = toplevs,
+ ic_exports = exports,
+ ic_rn_gbl_env = all_env } }
+
+mkTopLevEnv :: HomePackageTable -> String -> IO GlobalRdrEnv
+mkTopLevEnv hpt mod
+ = case lookupModuleEnvByName hpt (mkModuleName mod) of
+ Nothing -> throwDyn (ProgramError ("mkTopLevEnv: not a home module " ++ mod))
+ Just details -> case hm_globals details of
+ Nothing -> throwDyn (ProgramError ("mkTopLevEnv: not interpreted " ++ mod))
+ Just env -> return env