-pkgInfoToId :: Maybe (PackageConfig,Bool) -> IfacePackage
-pkgInfoToId (Just (pkg,_)) = ExternalPackage (mkPackageId (package pkg))
-pkgInfoToId Nothing = ThisPackage
-
--- Is a module visible or not? Returns Nothing if the import is ok,
--- or Just err if there's a visibility error.
-visible :: Bool -> Maybe (PackageConfig,Bool) -> Maybe FindResult
-visible explicit maybe_pkg
- | Nothing <- maybe_pkg = Nothing -- home module ==> YES
- | not explicit = Nothing -- implicit import ==> YES
- | Just (pkg, exposed_module) <- maybe_pkg
- = case () of
- _ | not exposed_module -> Just (ModuleHidden pkgname)
- | not (exposed pkg) -> Just (PackageHidden pkgname)
- | otherwise -> Nothing
- where
- pkgname = packageConfigId pkg
-
+findPackageModule :: HscEnv -> Module -> Bool -> IO FindResult
+findPackageModule = findModule' False
+
+
+data LocalFindResult
+ = Ok FinderCacheEntry
+ | CantFindAmongst [FilePath]
+ | MultiplePackages [PackageId]
+
+findModule' :: Bool -> HscEnv -> Module -> Bool -> IO FindResult
+findModule' home_allowed hsc_env name explicit
+ = do -- First try the cache
+ mb_entry <- lookupFinderCache cache name
+ case mb_entry of
+ Just old_entry -> return $! found old_entry
+ Nothing -> not_cached
+
+ where
+ cache = hsc_FC hsc_env
+ dflags = hsc_dflags hsc_env
+
+ -- We've found the module, so the remaining question is
+ -- whether it's visible or not
+ found :: FinderCacheEntry -> FindResult
+ found (loc, Nothing)
+ | home_allowed = Found loc HomePackage
+ | otherwise = NotFound []
+ found (loc, Just (pkg, exposed_mod))
+ | explicit && not exposed_mod = ModuleHidden pkg_name
+ | explicit && not (exposed pkg) = PackageHidden pkg_name
+ | otherwise =
+ Found loc (ExtPackage (mkPackageId (package pkg)))
+ where
+ pkg_name = packageConfigId pkg
+
+ found_new entry = do
+ addToFinderCache cache name entry
+ return $! found entry
+
+ not_cached
+ | not home_allowed = do
+ j <- findPackageModule' dflags name
+ case j of
+ Ok entry -> found_new entry
+ MultiplePackages pkgs -> return (FoundMultiple pkgs)
+ CantFindAmongst paths -> return (NotFound paths)
+
+ | otherwise = do
+ j <- findHomeModule' dflags name
+ case j of
+ Ok entry -> found_new entry
+ MultiplePackages pkgs -> return (FoundMultiple pkgs)
+ CantFindAmongst home_files -> do
+ r <- findPackageModule' dflags name
+ case r of
+ CantFindAmongst pkg_files ->
+ return (NotFound (home_files ++ pkg_files))
+ MultiplePackages pkgs ->
+ return (FoundMultiple pkgs)
+ Ok entry ->
+ found_new entry
+
+addHomeModuleToFinder :: HscEnv -> Module -> ModLocation -> IO ()
+addHomeModuleToFinder hsc_env mod loc
+ = addToFinderCache (hsc_FC hsc_env) mod (loc, Nothing)
+
+uncacheModule :: HscEnv -> Module -> IO ()
+uncacheModule hsc_env mod = removeFromFinderCache (hsc_FC hsc_env) mod