+ let
+ hi_exts =
+ [ (package_hisuf, mkPackageModLocation package_hisuf mod_name) ]
+
+ source_exts =
+ [ ("hs", mkPackageModLocation package_hisuf mod_name)
+ , ("lhs", mkPackageModLocation package_hisuf mod_name)
+ ]
+
+ -- mkdependHS needs to look for source files in packages too, so
+ -- that we can make dependencies between package before they have
+ -- been built.
+ exts
+ | mode == DoMkDependHS = hi_exts ++ source_exts
+ | otherwise = hi_exts
+
+ -- we never look for a .hi-boot file in an external package;
+ -- .hi-boot files only make sense for the home package.
+ searchPathExts imp_dirs mod_name exts
+
+-- -----------------------------------------------------------------------------
+-- General path searching
+
+searchPathExts
+ :: [FilePath] -- paths to search
+ -> ModuleName -- module name
+ -> [ (
+ String, -- suffix
+ String -> String -> String -> IO (Module, ModLocation) -- action
+ )
+ ]
+ -> IO (Either [FilePath] (Module, ModLocation))
+
+searchPathExts path mod_name exts = search to_search
+ where
+ basename = dots_to_slashes (moduleNameUserString mod_name)
+
+ to_search :: [(FilePath, IO (Module,ModLocation))]
+ to_search = [ (file, fn p basename ext)
+ | p <- path,
+ (ext,fn) <- exts,
+ let base | p == "." = basename
+ | otherwise = p ++ '/':basename
+ file = base ++ '.':ext
+ ]
+
+ search [] = return (Left (map fst to_search))
+ search ((file, result) : rest) = do
+ b <- doesFileExist file
+ if b
+ then Right `liftM` result
+ else search rest
+
+-- -----------------------------------------------------------------------------
+-- Building ModLocations
+
+mkHiOnlyModLocation hisuf mod_name path basename _ext = do
+ -- basename == dots_to_slashes (moduleNameUserString mod_name)
+ loc <- hiOnlyModLocation path basename hisuf
+ let result = (mkHomeModule mod_name, loc)
+ addToFinderCache mod_name result
+ return result
+
+mkPackageModLocation hisuf mod_name path basename _ext = do
+ -- basename == dots_to_slashes (moduleNameUserString mod_name)
+ loc <- hiOnlyModLocation path basename hisuf
+ let result = (mkPackageModule mod_name, loc)
+ addToFinderCache mod_name result
+ return result
+
+hiOnlyModLocation path basename hisuf
+ = do { obj_fn <- mkObjPath path basename ;
+ return (ModLocation{ ml_hspp_file = Nothing,
+ ml_hs_file = Nothing,
+ ml_hi_file = path ++ '/':basename ++ '.':hisuf,
+ -- Remove the .hi-boot suffix from
+ -- hi_file, if it had one. We always
+ -- want the name of the real .hi file
+ -- in the ml_hi_file field.
+ ml_obj_file = obj_fn
+ })}
+
+-- -----------------------------------------------------------------------------
+-- Constructing a home module location
+
+-- This is where we construct the ModLocation for a module in the home
+-- package, for which we have a source file. It is called from three
+-- places:
+--
+-- (a) Here in the finder, when we are searching for a module to import,
+-- using the search path (-i option).
+--
+-- (b) The compilation manager, when constructing the ModLocation for
+-- a "root" module (a source file named explicitly on the command line
+-- or in a :load command in GHCi).
+--
+-- (c) The driver in one-shot mode, when we need to construct a
+-- ModLocation for a source file named on the command-line.
+--
+-- Parameters are:
+--
+-- mod_name
+-- The name of the module
+--
+-- path
+-- (a): The search path component where the source file was found.
+-- (b) and (c): "."
+--
+-- src_basename
+-- (a): dots_to_slashes (moduleNameUserString mod_name)
+-- (b) and (c): The filename of the source file, minus its extension
+--
+-- ext
+-- The filename extension of the source file (usually "hs" or "lhs").
+
+mkHomeModLocation mod_name src_filename = do
+ let mod_basename = dots_to_slashes (moduleNameUserString mod_name)
+ (basename,extension) = splitFilename src_filename
+
+ case my_prefix_match (reverse mod_basename) (reverse basename) of
+ Just "" ->
+ mkHomeModLocationSearched mod_name "." mod_basename extension
+ Just rest -> do
+ let path = reverse (dropWhile (=='/') rest)
+ mkHomeModLocationSearched mod_name path mod_basename extension
+ Nothing -> do
+ hPutStrLn stderr ("Warning: " ++ src_filename ++
+ ": filename and module name do not match")
+ let (dir,basename,ext) = splitFilename3 src_filename
+ mkHomeModLocationSearched mod_name dir basename ext
+
+mkHomeModLocationSearched mod_name path src_basename ext = do
+ hisuf <- readIORef v_Hi_suf
+ hidir <- readIORef v_Hi_dir