+-----------------------------------------------------------------------------
+-- Grab information from the CmState
+
+cmGetModuleGraph = mg
+cmGetLinkables = ui
+
+cmGetBindings cmstate = nameEnvElts (ic_type_env (ic cmstate))
+cmGetPrintUnqual cmstate = ic_print_unqual (ic cmstate)
+
+-----------------------------------------------------------------------------
+-- 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.
+
+cmSetContext
+ :: CmState -> DynFlags
+ -> [String] -- take the top-level scopes of these modules
+ -> [String] -- and the just the exports from these
+ -> IO CmState
+cmSetContext cmstate dflags toplevs exports = do
+ let CmState{ hit=hit, hst=hst, pcs=pcs, ic=old_ic } = cmstate
+
+ toplev_mods <- mapM (getTopLevModule hit) (map mkModuleName toplevs)
+ export_mods <- mapM (moduleNameToModule hit) (map mkModuleName exports)
+
+ (new_pcs, print_unqual, maybe_env)
+ <- mkGlobalContext dflags hit hst 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,
+ ic_print_unqual = print_unqual } }
+
+getTopLevModule hit mn =
+ case lookupModuleEnvByName hit mn of
+ Just iface
+ | Just _ <- mi_globals iface -> return (mi_module iface)
+ _other -> throwDyn (CmdLineError (
+ "cannot enter the top-level scope of a compiled module (module `" ++
+ moduleNameUserString mn ++ "')"))
+
+moduleNameToModule :: HomeIfaceTable -> ModuleName -> IO Module
+moduleNameToModule hit mn = do
+ case lookupModuleEnvByName hit mn of
+ Just iface -> return (mi_module iface)
+ _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
+
+cmGetContext :: CmState -> IO ([String],[String])
+cmGetContext CmState{ic=ic} =
+ return (map moduleUserString (ic_toplev_scope ic),
+ map moduleUserString (ic_exports ic))
+
+cmModuleIsInterpreted :: CmState -> String -> IO Bool
+cmModuleIsInterpreted cmstate str
+ = case lookupModuleEnvByName (hit cmstate) (mkModuleName str) of
+ Just iface -> return (not (isNothing (mi_globals iface)))
+ _not_a_home_module -> return False
+
+-----------------------------------------------------------------------------
+-- cmInfoThing: convert a String to a TyThing
+
+-- A string may refer to more than one TyThing (eg. a constructor,
+-- and type constructor), so we return a list of all the possible TyThings.
+
+#ifdef GHCI
+cmInfoThing :: CmState -> DynFlags -> String -> IO (CmState, [(TyThing,Fixity)])
+cmInfoThing cmstate dflags id
+ = do (new_pcs, things) <- hscThing dflags hst hit pcs icontext id
+ let pairs = map (\x -> (x, getFixity new_pcs (getName x))) things
+ return (cmstate{ pcs=new_pcs }, pairs)
+ where
+ CmState{ hst=hst, hit=hit, pcs=pcs, pls=pls, ic=icontext } = cmstate
+
+ getFixity :: PersistentCompilerState -> Name -> Fixity
+ getFixity pcs name
+ | isGlobalName name,
+ Just iface <- lookupModuleEnv iface_table (nameModule name),
+ Just fixity <- lookupNameEnv (mi_fixities iface) name
+ = fixity
+ | otherwise
+ = defaultFixity
+ where iface_table | isHomePackageName name = hit
+ | otherwise = pcs_PIT pcs
+#endif
+
+-- ---------------------------------------------------------------------------
+-- cmBrowseModule: get all the TyThings defined in a module
+
+#ifdef GHCI
+cmBrowseModule :: CmState -> DynFlags -> String -> Bool
+ -> IO (CmState, [TyThing])
+cmBrowseModule cmstate dflags str exports_only = do
+ let mn = mkModuleName str
+ mod <- moduleNameToModule hit mn
+ (pcs1, maybe_ty_things)
+ <- hscModuleContents dflags hst hit pcs mod exports_only
+ case maybe_ty_things of
+ Nothing -> return (cmstate{pcs=pcs1}, [])
+ Just ty_things -> return (cmstate{pcs=pcs1}, ty_things)
+ where
+ CmState{ hst=hst, hit=hit, pcs=pcs, pls=pls, ic=icontext } = cmstate
+#endif
+
+-----------------------------------------------------------------------------
+-- cmRunStmt: Run a statement/expr.
+
+#ifdef GHCI
+data CmRunResult
+ = CmRunOk [Name] -- names bound by this evaluation
+ | CmRunFailed
+ | CmRunException Exception -- statement raised an exception
+
+cmRunStmt :: CmState -> DynFlags -> String -> IO (CmState, CmRunResult)
+cmRunStmt cmstate@CmState{ hst=hst, hit=hit, pcs=pcs, pls=pls, ic=icontext }
+ dflags expr
+ = do
+ let InteractiveContext {
+ ic_rn_local_env = rn_env,
+ ic_type_env = type_env } = icontext
+
+ (new_pcs, maybe_stuff)
+ <- hscStmt dflags hst hit pcs icontext expr False{-stmt-}
+
+ case maybe_stuff of
+ Nothing -> return (cmstate{ pcs=new_pcs }, CmRunFailed)
+ Just (ids, _, bcos) -> do
+
+ -- update the interactive context
+ let
+ names = map idName ids
+
+ -- these names have just been shadowed
+ shadowed = [ n | r <- map nameRdrName names,
+ Just n <- [lookupRdrEnv rn_env r] ]
+
+ new_rn_env = extendLocalRdrEnv rn_env names
+
+ -- remove any shadowed bindings from the type_env
+ filtered_type_env = delListFromNameEnv type_env shadowed
+
+ new_type_env = extendNameEnvList filtered_type_env
+ [ (getName id, AnId id) | id <- ids]
+
+ new_ic = icontext { ic_rn_local_env = new_rn_env,
+ ic_type_env = new_type_env }
+
+ -- link it
+ hval <- linkExpr pls bcos
+
+ -- run it!
+ let thing_to_run = unsafeCoerce# hval :: IO [HValue]
+ either_hvals <- sandboxIO thing_to_run
+ case either_hvals of
+ Left err
+ -> do hPutStrLn stderr ("unknown failure, code " ++ show err)
+ return ( cmstate{ pcs=new_pcs, ic=new_ic }, CmRunFailed )
+
+ Right maybe_hvals ->
+ case maybe_hvals of
+ Left e ->
+ return ( cmstate{ pcs=new_pcs, ic=new_ic },
+ CmRunException e )
+ Right hvals -> do
+ -- Get the newly bound things, and bind them.
+ -- Don't forget to delete any shadowed bindings from the
+ -- closure_env, lest we end up with a space leak.
+ pls <- delListFromClosureEnv pls shadowed
+ new_pls <- addListToClosureEnv pls (zip names hvals)
+
+ return (cmstate{ pcs=new_pcs, pls=new_pls, ic=new_ic },
+ CmRunOk names)
+
+
+-- We run the statement in a "sandbox" to protect the rest of the
+-- system from anything the expression might do. For now, this
+-- consists of just wrapping it in an exception handler, but see below
+-- for another version.
+
+sandboxIO :: IO a -> IO (Either Int (Either Exception a))
+sandboxIO thing = do
+ r <- Exception.try thing
+ return (Right r)
+
+{-
+-- This version of sandboxIO runs the expression in a completely new
+-- RTS main thread. It is disabled for now because ^C exceptions
+-- won't be delivered to the new thread, instead they'll be delivered
+-- to the (blocked) GHCi main thread.
+
+sandboxIO :: IO a -> IO (Either Int (Either Exception a))
+sandboxIO thing = do
+ st_thing <- newStablePtr (Exception.try thing)
+ alloca $ \ p_st_result -> do
+ stat <- rts_evalStableIO st_thing p_st_result
+ freeStablePtr st_thing
+ if stat == 1
+ then do st_result <- peek p_st_result
+ result <- deRefStablePtr st_result
+ freeStablePtr st_result
+ return (Right result)
+ else do
+ return (Left (fromIntegral stat))
+
+foreign import "rts_evalStableIO" {- safe -}
+ rts_evalStableIO :: StablePtr (IO a) -> Ptr (StablePtr a) -> IO CInt
+ -- more informative than the C type!
+-}
+#endif
+
+-----------------------------------------------------------------------------
+-- cmTypeOfExpr: returns a string representing the type of an expression
+
+#ifdef GHCI
+cmTypeOfExpr :: CmState -> DynFlags -> String -> IO (CmState, Maybe String)
+cmTypeOfExpr cmstate dflags expr
+ = do (new_pcs, maybe_stuff)
+ <- hscStmt dflags hst hit pcs ic expr True{-just an expr-}
+
+ let new_cmstate = cmstate{pcs = new_pcs}
+
+ case maybe_stuff of
+ Nothing -> return (new_cmstate, Nothing)
+ Just (_, ty, _) -> return (new_cmstate, Just str)
+ where
+ str = showSDocForUser unqual (ppr tidy_ty)
+ unqual = ic_print_unqual ic
+ tidy_ty = tidyType emptyTidyEnv ty
+ where
+ CmState{ hst=hst, hit=hit, pcs=pcs, ic=ic } = cmstate
+#endif
+
+-----------------------------------------------------------------------------
+-- cmTypeOfName: returns a string representing the type of a name.
+
+#ifdef GHCI
+cmTypeOfName :: CmState -> Name -> IO (Maybe String)
+cmTypeOfName CmState{ hit=hit, pcs=pcs, ic=ic } name
+ = case lookupNameEnv (ic_type_env ic) name of
+ Nothing -> return Nothing
+ Just (AnId id) -> return (Just str)
+ where
+ unqual = ic_print_unqual ic
+ ty = tidyType emptyTidyEnv (idType id)
+ str = showSDocForUser unqual (ppr ty)
+
+ _ -> panic "cmTypeOfName"
+#endif
+
+-----------------------------------------------------------------------------
+-- cmCompileExpr: compile an expression and deliver an HValue
+
+#ifdef GHCI
+cmCompileExpr :: CmState -> DynFlags -> String -> IO (CmState, Maybe HValue)
+cmCompileExpr cmstate dflags expr
+ = do
+ let InteractiveContext {
+ ic_rn_local_env = rn_env,
+ ic_type_env = type_env } = icontext
+
+ (new_pcs, maybe_stuff)
+ <- hscStmt dflags hst hit pcs icontext
+ ("let __cmCompileExpr = "++expr) False{-stmt-}
+
+ case maybe_stuff of
+ Nothing -> return (cmstate{ pcs=new_pcs }, Nothing)
+ Just (ids, _, bcos) -> do
+
+ -- link it
+ hval <- linkExpr pls bcos
+
+ -- run it!
+ let thing_to_run = unsafeCoerce# hval :: IO [HValue]
+ hvals <- thing_to_run
+
+ case (ids,hvals) of
+ ([id],[hv]) -> return (cmstate{ pcs=new_pcs }, Just hv)
+ _ -> panic "cmCompileExpr"
+
+ where
+ CmState{ hst=hst, hit=hit, pcs=pcs, pls=pls, ic=icontext } = cmstate
+#endif
+
+-----------------------------------------------------------------------------
+-- Unload the compilation manager's state: everything it knows about the
+-- current collection of modules in the Home package.
+
+cmUnload :: CmState -> DynFlags -> IO CmState
+cmUnload state@CmState{ gmode=mode, pls=pls, pcs=pcs } dflags