|
| 1 | +module Finder4Lsk where |
| 2 | +import qualified Finder as F |
| 3 | +import HscTypes |
| 4 | +import Module |
| 5 | +import FastString |
| 6 | +import DynFlags |
| 7 | +import Data.IORef ( IORef, writeIORef, readIORef, modifyIORef ) |
| 8 | +import LazyUniqFM |
| 9 | +import PrelNames ( gHC_PRIM ) |
| 10 | +import System.Directory |
| 11 | +import System.FilePath |
| 12 | +import FiniteMap |
| 13 | +import Util |
| 14 | + |
| 15 | +findImportedModule :: HscEnv -> ModuleName -> Maybe FastString -> IO FindResult |
| 16 | +findImportedModule hsc_env mod_name mb_pkg = |
| 17 | + case mb_pkg of |
| 18 | + Nothing -> unqual_import |
| 19 | + Just pkg | pkg == fsLit "this" -> home_import -- "this" is special |
| 20 | + | otherwise -> pkg_import |
| 21 | + where |
| 22 | + home_import = findHomeModule hsc_env mod_name |
| 23 | + |
| 24 | + pkg_import = F.findImportedModule hsc_env mod_name mb_pkg |
| 25 | + |
| 26 | + unqual_import = home_import |
| 27 | + `orIfNotFound` |
| 28 | + F.findImportedModule hsc_env mod_name Nothing |
| 29 | + |
| 30 | +findHomeModule :: HscEnv -> ModuleName -> IO FindResult |
| 31 | +findHomeModule hsc_env mod_name = |
| 32 | + homeSearchCache hsc_env mod_name $ |
| 33 | + let |
| 34 | + dflags = hsc_dflags hsc_env |
| 35 | + home_path = importPaths dflags |
| 36 | + hisuf = hiSuf dflags |
| 37 | + mod = mkModule (thisPackage dflags) mod_name |
| 38 | + |
| 39 | + source_exts = |
| 40 | + [ ("hs", mkHomeModLocationSearched dflags mod_name "hs") |
| 41 | + , ("lhs", mkHomeModLocationSearched dflags mod_name "lhs") |
| 42 | + , ("lsk", mkHomeModLocationSearched dflags mod_name "lsk") |
| 43 | + ] |
| 44 | + |
| 45 | + hi_exts = [ (hisuf, mkHiOnlyModLocation dflags hisuf) |
| 46 | + , (addBootSuffix hisuf, mkHiOnlyModLocation dflags hisuf) |
| 47 | + ] |
| 48 | + |
| 49 | + -- In compilation manager modes, we look for source files in the home |
| 50 | + -- package because we can compile these automatically. In one-shot |
| 51 | + -- compilation mode we look for .hi and .hi-boot files only. |
| 52 | + exts | isOneShot (ghcMode dflags) = hi_exts |
| 53 | + | otherwise = source_exts |
| 54 | + in |
| 55 | + |
| 56 | + -- special case for GHC.Prim; we won't find it in the filesystem. |
| 57 | + -- This is important only when compiling the base package (where GHC.Prim |
| 58 | + -- is a home module). |
| 59 | + if mod == gHC_PRIM |
| 60 | + then return (Found (error "GHC.Prim ModLocation") mod) |
| 61 | + else |
| 62 | + |
| 63 | + searchPathExts home_path mod exts |
| 64 | + |
| 65 | +orIfNotFound :: IO FindResult -> IO FindResult -> IO FindResult |
| 66 | +this `orIfNotFound` or_this = do |
| 67 | + res <- this |
| 68 | + case res of |
| 69 | + NotFound here _ -> do |
| 70 | + res2 <- or_this |
| 71 | + case res2 of |
| 72 | + NotFound or_here pkg -> return (NotFound (here ++ or_here) pkg) |
| 73 | + _other -> return res2 |
| 74 | + _other -> return res |
| 75 | + |
| 76 | +homeSearchCache :: HscEnv -> ModuleName -> IO FindResult -> IO FindResult |
| 77 | +homeSearchCache hsc_env mod_name do_this = do |
| 78 | + m <- lookupFinderCache (hsc_FC hsc_env) mod_name |
| 79 | + case m of |
| 80 | + Just result -> return result |
| 81 | + Nothing -> do |
| 82 | + result <- do_this |
| 83 | + addToFinderCache (hsc_FC hsc_env) mod_name result |
| 84 | + case result of |
| 85 | + Found loc mod -> addToModLocationCache (hsc_MLC hsc_env) mod loc |
| 86 | + _other -> return () |
| 87 | + return result |
| 88 | + |
| 89 | +mkHomeModLocationSearched :: DynFlags -> ModuleName -> FileExt |
| 90 | + -> FilePath -> BaseName -> IO ModLocation |
| 91 | +mkHomeModLocationSearched dflags mod suff path basename = do |
| 92 | + F.mkHomeModLocation2 dflags mod (path </> basename) suff |
| 93 | + |
| 94 | +addToFinderCache :: IORef FinderCache -> ModuleName -> FindResult -> IO () |
| 95 | +addToFinderCache ref key val = modifyIORef ref $ \c -> addToUFM c key val |
| 96 | + |
| 97 | +addToModLocationCache :: IORef ModLocationCache -> Module -> ModLocation -> IO () |
| 98 | +addToModLocationCache ref key val = modifyIORef ref $ \c -> addToFM c key val |
| 99 | + |
| 100 | +lookupFinderCache :: IORef FinderCache -> ModuleName -> IO (Maybe FindResult) |
| 101 | +lookupFinderCache ref key = do |
| 102 | + c <- readIORef ref |
| 103 | + return $! lookupUFM c key |
| 104 | + |
| 105 | +searchPathExts |
| 106 | + :: [FilePath] -- paths to search |
| 107 | + -> Module -- module name |
| 108 | + -> [ ( |
| 109 | + FileExt, -- suffix |
| 110 | + FilePath -> BaseName -> IO ModLocation -- action |
| 111 | + ) |
| 112 | + ] |
| 113 | + -> IO FindResult |
| 114 | + |
| 115 | +searchPathExts paths mod exts |
| 116 | + = do result <- search to_search |
| 117 | +{- |
| 118 | + hPutStrLn stderr (showSDoc $ |
| 119 | + vcat [text "Search" <+> ppr mod <+> sep (map (text. fst) exts) |
| 120 | + , nest 2 (vcat (map text paths)) |
| 121 | + , case result of |
| 122 | + Succeeded (loc, p) -> text "Found" <+> ppr loc |
| 123 | + Failed fs -> text "not found"]) |
| 124 | +-} |
| 125 | + return result |
| 126 | + |
| 127 | + where |
| 128 | + basename = moduleNameSlashes (moduleName mod) |
| 129 | + |
| 130 | + to_search :: [(FilePath, IO ModLocation)] |
| 131 | + to_search = [ (file, fn path basename) |
| 132 | + | path <- paths, |
| 133 | + (ext,fn) <- exts, |
| 134 | + let base | path == "." = basename |
| 135 | + | otherwise = path </> basename |
| 136 | + file = base <.> ext |
| 137 | + ] |
| 138 | + |
| 139 | + search [] = return (NotFound (map fst to_search) (Just (modulePackageId mod))) |
| 140 | + search ((file, mk_result) : rest) = do |
| 141 | + b <- doesFileExist file |
| 142 | + if b |
| 143 | + then do { loc <- mk_result; return (Found loc mod) } |
| 144 | + else search rest |
| 145 | + |
| 146 | +type FileExt = String -- Filename extension |
| 147 | +type BaseName = String -- Basename of file |
| 148 | + |
| 149 | +mkHiOnlyModLocation :: DynFlags -> Suffix -> FilePath -> String |
| 150 | + -> IO ModLocation |
| 151 | +mkHiOnlyModLocation dflags hisuf path basename |
| 152 | + = do let full_basename = path </> basename |
| 153 | + obj_fn <- mkObjPath dflags full_basename basename |
| 154 | + return ModLocation{ ml_hs_file = Nothing, |
| 155 | + ml_hi_file = full_basename <.> hisuf, |
| 156 | + -- Remove the .hi-boot suffix from |
| 157 | + -- hi_file, if it had one. We always |
| 158 | + -- want the name of the real .hi file |
| 159 | + -- in the ml_hi_file field. |
| 160 | + ml_obj_file = obj_fn |
| 161 | + } |
| 162 | + |
| 163 | +mkObjPath |
| 164 | + :: DynFlags |
| 165 | + -> FilePath -- the filename of the source file, minus the extension |
| 166 | + -> String -- the module name with dots replaced by slashes |
| 167 | + -> IO FilePath |
| 168 | +mkObjPath dflags basename mod_basename |
| 169 | + = do let |
| 170 | + odir = objectDir dflags |
| 171 | + osuf = objectSuf dflags |
| 172 | + |
| 173 | + obj_basename | Just dir <- odir = dir </> mod_basename |
| 174 | + | otherwise = basename |
| 175 | + |
| 176 | + return (obj_basename <.> osuf) |
0 commit comments