+import SrcLoc ( SrcSpan )
+import UniqSupply ( UniqSupply )
+import Maybe ( fromJust )
+import FastString ( FastString )
+
+import DATA_IOREF ( IORef, readIORef )
+import Time ( ClockTime )
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{Compilation environment}
+%* *
+%************************************************************************
+
+The HscEnv gives the environment in which to compile a chunk of code.
+
+\begin{code}
+data HscEnv
+ = HscEnv { hsc_mode :: GhciMode,
+ hsc_dflags :: DynFlags,
+
+ hsc_HPT :: HomePackageTable,
+ -- The home package table describes already-compiled
+ -- home-packge modules, *excluding* the module we
+ -- are compiling right now.
+ -- (In one-shot mode the current module is the only
+ -- home-package module, so hsc_HPT is empty. All other
+ -- modules count as "external-package" modules.)
+ -- hsc_HPT is not mutable because we only demand-load
+ -- external packages; the home package is eagerly
+ -- loaded by the compilation manager.
+
+ -- The next two are side-effected by compiling
+ -- to reflect sucking in interface files
+ hsc_EPS :: IORef ExternalPackageState,
+ hsc_NC :: IORef NameCache }
+
+hscEPS :: HscEnv -> IO ExternalPackageState
+hscEPS hsc_env = readIORef (hsc_EPS hsc_env)
+\end{code}
+
+The GhciMode is self-explanatory:
+
+\begin{code}
+data GhciMode = Batch -- ghc --make Main
+ | Interactive -- ghc --interactive
+ | OneShot -- ghc Foo.hs
+ | IDE -- Visual Studio etc
+ deriving Eq
+
+isOneShot :: GhciMode -> Bool
+isOneShot OneShot = True
+isOneShot _other = False
+\end{code}
+
+\begin{code}
+type HomePackageTable = ModuleEnv HomeModInfo -- Domain = modules in the home package
+type PackageIfaceTable = ModuleEnv ModIface -- Domain = modules in the imported packages
+
+emptyHomePackageTable = emptyModuleEnv
+emptyPackageIfaceTable = emptyModuleEnv
+
+data HomeModInfo
+ = HomeModInfo { hm_iface :: ModIface,
+ hm_globals :: Maybe GlobalRdrEnv, -- Its top level environment
+ -- Nothing <-> compiled module
+ hm_details :: ModDetails,
+ hm_linkable :: Linkable }
+\end{code}
+
+Simple lookups in the symbol table.
+
+\begin{code}
+lookupIface :: HomePackageTable -> PackageIfaceTable -> Module -> Maybe ModIface
+-- We often have two IfaceTables, and want to do a lookup
+lookupIface hpt pit mod
+ = case lookupModuleEnv hpt mod of
+ Just mod_info -> Just (hm_iface mod_info)
+ Nothing -> lookupModuleEnv pit mod
+
+lookupIfaceByModName :: HomePackageTable -> PackageIfaceTable -> ModuleName -> Maybe ModIface
+-- We often have two IfaceTables, and want to do a lookup
+lookupIfaceByModName hpt pit mod
+ = case lookupModuleEnvByName hpt mod of
+ Just mod_info -> Just (hm_iface mod_info)
+ Nothing -> lookupModuleEnvByName pit mod
+\end{code}
+
+\begin{code}
+-- Use instead of Finder.findModule if possible: this way doesn't
+-- require filesystem operations, and it is guaranteed not to fail
+-- when the IfaceTables are properly populated (i.e. after the renamer).
+moduleNameToModule :: HomePackageTable -> PackageIfaceTable -> ModuleName -> Module
+moduleNameToModule hpt pit mod
+ = mi_module (fromJust (lookupIfaceByModName hpt pit mod))