summaryrefslogtreecommitdiff
path: root/compiler/main/InteractiveEval.hs
diff options
context:
space:
mode:
authorSimon Peyton Jones <simonpj@microsoft.com>2011-08-02 08:18:03 +0100
committerSimon Peyton Jones <simonpj@microsoft.com>2011-08-02 08:18:03 +0100
commit35d213abfe27502fa34b60975c4b18ed51bfeb05 (patch)
treeaab2a30ab9acbf6ab2bc51366530027eab13b8ad /compiler/main/InteractiveEval.hs
parent6059755e045ed8c4a8c3d48cc0ec5733bd950c0f (diff)
downloadhaskell-35d213abfe27502fa34b60975c4b18ed51bfeb05.tar.gz
Refactor the imports of InteractiveContext
Instead of two fields ic_toplev_scope :: [Module] ic_imports :: [ImportDecl RdrName] we now just have one ic_imports :: [InteractiveImport] with the auxiliary data type data InteractiveImport = IIDecl (ImportDecl RdrName) -- Bring the exports of a particular module -- (filtered by an import decl) into scope | IIModule Module -- Bring into scope the entire top-level envt of -- of this module, including the things imported -- into it. This makes lots of code less confusing. No change in behaviour. It's preparatory to fixing Trac #5147. While I was at I also * Cleaned up the handling of the "implicit" Prelude import by adding a ideclImplicit field to ImportDecl. This significantly reduces plumbing in the handling of the implicit Prelude import * Used record notation consistently for ImportDecl
Diffstat (limited to 'compiler/main/InteractiveEval.hs')
-rw-r--r--compiler/main/InteractiveEval.hs53
1 files changed, 28 insertions, 25 deletions
diff --git a/compiler/main/InteractiveEval.hs b/compiler/main/InteractiveEval.hs
index 0386273de8..24f340b33d 100644
--- a/compiler/main/InteractiveEval.hs
+++ b/compiler/main/InteractiveEval.hs
@@ -778,29 +778,32 @@ fromListBL bound l = BL (length l) bound l []
-- Setting the context doesn't throw away any bindings; the bindings
-- we've built up in the InteractiveContext simply move to the new
-- module. They always shadow anything in scope in the current context.
-setContext :: GhcMonad m =>
- [Module] -- ^ entire top level scope of these modules
- -> [ImportDecl RdrName] -- ^ these import declarations
- -> m ()
-setContext toplev_mods import_decls = do
- hsc_env <- getSession
- let old_ic = hsc_IC hsc_env
- hpt = hsc_HPT hsc_env
- imprt_decls = map noLoc import_decls
- --
- import_env <-
- if null imprt_decls then return emptyGlobalRdrEnv else do
- let this_mod | null toplev_mods = pRELUDE
- | otherwise = head toplev_mods
- liftIO $ hscRnImportDecls hsc_env this_mod imprt_decls
-
- toplev_envs <- liftIO $ mapM (mkTopLevEnv hpt) toplev_mods
-
- let all_env = foldr plusGlobalRdrEnv import_env toplev_envs
- modifySession $ \_ ->
- hsc_env{ hsc_IC = old_ic { ic_toplev_scope = toplev_mods,
- ic_imports = import_decls,
- ic_rn_gbl_env = all_env }}
+setContext :: GhcMonad m => [InteractiveImport] -> m ()
+setContext imports
+ = do { hsc_env <- getSession
+ ; let old_ic = hsc_IC hsc_env
+ ; all_env <- liftIO $ findGlobalRdrEnv hsc_env imports
+ ; modifySession $ \_ ->
+ hsc_env{ hsc_IC = old_ic { ic_imports = imports
+ , ic_rn_gbl_env = all_env }}}
+
+findGlobalRdrEnv :: HscEnv -> [InteractiveImport] -> IO GlobalRdrEnv
+-- Compute the GlobalRdrEnv for the interactive context
+findGlobalRdrEnv hsc_env imports
+ = do { idecls_env <- hscRnImportDecls hsc_env this_mod idecls
+ -- This call also loads any orphan modules
+ ; imods_env <- mapM (mkTopLevEnv (hsc_HPT hsc_env)) imods
+ ; return (foldr plusGlobalRdrEnv idecls_env imods_env) }
+ where
+ idecls :: [LImportDecl RdrName]
+ idecls = [noLoc d | IIDecl d <- imports]
+
+ imods :: [Module]
+ imods = [m | IIModule m <- imports]
+
+ this_mod = case imods of
+ [] -> pRELUDE
+ (m:_) -> m
availsToGlobalRdrEnv :: ModuleName -> [AvailInfo] -> GlobalRdrEnv
availsToGlobalRdrEnv mod_name avails
@@ -828,9 +831,9 @@ mkTopLevEnv hpt modl
-- | Get the interactive evaluation context, consisting of a pair of the
-- set of modules from which we take the full top-level scope, and the set
-- of modules from which we take just the exports respectively.
-getContext :: GhcMonad m => m ([Module],[ImportDecl RdrName])
+getContext :: GhcMonad m => m [InteractiveImport]
getContext = withSession $ \HscEnv{ hsc_IC=ic } ->
- return (ic_toplev_scope ic, ic_imports ic)
+ return (ic_imports ic)
-- | Returns @True@ if the specified module is interpreted, and hence has
-- its full top-level scope available.