+\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
+
+lookupIfaceByModule :: HomePackageTable -> PackageIfaceTable -> Module -> Maybe ModIface
+-- We often have two IfaceTables, and want to do a lookup
+lookupIfaceByModule hpt pit mod
+ = case lookupModuleEnv hpt mod of
+ Just mod_info -> Just (hm_iface mod_info)
+ Nothing -> lookupModuleEnv pit mod
+\end{code}
+
+
+\begin{code}
+hptInstances :: HscEnv -> [(Module, IsBootInterface)] -> [DFunId]
+-- Find all the instance declarations that are in modules imported
+-- by this one, directly or indirectly, and are in the Home Package Table
+-- This ensures that we don't see instances from modules --make compiled
+-- before this one, but which are not below this one
+hptInstances hsc_env deps
+ | isOneShot (hsc_mode hsc_env) = [] -- In one-shot mode, the HPT is empty
+ | otherwise
+ = let
+ hpt = hsc_HPT hsc_env
+ in
+ [ dfun
+ | -- Find each non-hi-boot module below me
+ (mod, False) <- deps
+
+ -- Look it up in the HPT
+ , let mod_info = ASSERT2( mod `elemModuleEnv` hpt, ppr mod $$ vcat (map ppr_hm (moduleEnvElts hpt)))
+ fromJust (lookupModuleEnv hpt mod)
+
+ -- And get its dfuns
+ , dfun <- md_insts (hm_details mod_info) ]
+ where
+ ppr_hm hm = ppr (mi_module (hm_iface hm))
+
+hptRules :: HscEnv -> [(Module, IsBootInterface)] -> [IdCoreRule]
+-- Get rules from modules "below" this one (in the dependency sense)
+-- C.f Inst.hptInstances
+hptRules hsc_env deps
+ | isOneShot (hsc_mode hsc_env) = []
+ | otherwise
+ = let
+ hpt = hsc_HPT hsc_env
+ in
+ [ rule
+ | -- Find each non-hi-boot module below me
+ (mod, False) <- deps
+
+ -- Look it up in the HPT
+ , let mod_info = ASSERT( mod `elemModuleEnv` hpt )
+ fromJust (lookupModuleEnv hpt mod)
+
+ -- And get its dfuns
+ , rule <- md_rules (hm_details mod_info) ]
+\end{code}