{-
(c) The University of Glasgow, 2000-2006

-}

{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}

-- | Module finder
module GHC.Unit.Finder (
    FindResult(..),
    InstalledFindResult(..),
    FinderCache,
    flushFinderCaches,
    findImportedModule,
    findPluginModule,
    findExactModule,
    findHomeModule,
    findExposedPackageModule,
    mkHomeModLocation,
    mkHomeModLocation2,
    mkHiOnlyModLocation,
    mkHiPath,
    mkObjPath,
    addHomeModuleToFinder,
    uncacheModule,
    mkStubPaths,

    findObjectLinkableMaybe,
    findObjectLinkable,

  ) where

#include "HsVersions.h"

import GHC.Prelude

import GHC.Driver.Env
import GHC.Driver.Session

import GHC.Platform.Ways

import GHC.Builtin.Names ( gHC_PRIM )

import GHC.Unit.Types
import GHC.Unit.Module
import GHC.Unit.Home
import GHC.Unit.State
import GHC.Unit.Finder.Types

import GHC.Data.FastString
import GHC.Data.Maybe    ( expectJust )
import qualified GHC.Data.ShortText as ST

import GHC.Utils.Misc
import GHC.Utils.Outputable as Outputable
import GHC.Utils.Panic

import GHC.Linker.Types

import Data.IORef       ( IORef, readIORef, atomicModifyIORef' )
import System.Directory
import System.FilePath
import Control.Monad
import Data.Time


type FileExt = String   -- Filename extension
type BaseName = String  -- Basename of file

-- -----------------------------------------------------------------------------
-- The Finder

-- The Finder provides a thin filesystem abstraction to the rest of
-- the compiler.  For a given module, it can tell you where the
-- source, interface, and object files for that module live.

-- It does *not* know which particular package a module lives in.  Use
-- Packages.lookupModuleInAllUnits for that.

-- -----------------------------------------------------------------------------
-- The finder's cache

-- remove all the home modules from the cache; package modules are
-- assumed to not move around during a session.
flushFinderCaches :: HscEnv -> IO ()
flushFinderCaches :: HscEnv -> IO ()
flushFinderCaches HscEnv
hsc_env =
  IORef FinderCache -> (FinderCache -> (FinderCache, ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef FinderCache
fc_ref ((FinderCache -> (FinderCache, ())) -> IO ())
-> (FinderCache -> (FinderCache, ())) -> IO ()
forall a b. (a -> b) -> a -> b
$ \FinderCache
fm -> ((InstalledModule -> InstalledFindResult -> Bool)
-> FinderCache -> FinderCache
forall a.
(InstalledModule -> a -> Bool)
-> InstalledModuleEnv a -> InstalledModuleEnv a
filterInstalledModuleEnv InstalledModule -> InstalledFindResult -> Bool
is_ext FinderCache
fm, ())
 where
        fc_ref :: IORef FinderCache
fc_ref       = HscEnv -> IORef FinderCache
hsc_FC HscEnv
hsc_env
        home_unit :: HomeUnit
home_unit    = HscEnv -> HomeUnit
hsc_home_unit HscEnv
hsc_env
        is_ext :: InstalledModule -> InstalledFindResult -> Bool
is_ext InstalledModule
mod InstalledFindResult
_ = Bool -> Bool
not (HomeUnit -> InstalledModule -> Bool
forall u. GenHomeUnit u -> InstalledModule -> Bool
isHomeInstalledModule HomeUnit
home_unit InstalledModule
mod)

addToFinderCache :: IORef FinderCache -> InstalledModule -> InstalledFindResult -> IO ()
addToFinderCache :: IORef FinderCache
-> InstalledModule -> InstalledFindResult -> IO ()
addToFinderCache IORef FinderCache
ref InstalledModule
key InstalledFindResult
val =
  IORef FinderCache -> (FinderCache -> (FinderCache, ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef FinderCache
ref ((FinderCache -> (FinderCache, ())) -> IO ())
-> (FinderCache -> (FinderCache, ())) -> IO ()
forall a b. (a -> b) -> a -> b
$ \FinderCache
c -> (FinderCache
-> InstalledModule -> InstalledFindResult -> FinderCache
forall a.
InstalledModuleEnv a
-> InstalledModule -> a -> InstalledModuleEnv a
extendInstalledModuleEnv FinderCache
c InstalledModule
key InstalledFindResult
val, ())

removeFromFinderCache :: IORef FinderCache -> InstalledModule -> IO ()
removeFromFinderCache :: IORef FinderCache -> InstalledModule -> IO ()
removeFromFinderCache IORef FinderCache
ref InstalledModule
key =
  IORef FinderCache -> (FinderCache -> (FinderCache, ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef FinderCache
ref ((FinderCache -> (FinderCache, ())) -> IO ())
-> (FinderCache -> (FinderCache, ())) -> IO ()
forall a b. (a -> b) -> a -> b
$ \FinderCache
c -> (FinderCache -> InstalledModule -> FinderCache
forall a.
InstalledModuleEnv a -> InstalledModule -> InstalledModuleEnv a
delInstalledModuleEnv FinderCache
c InstalledModule
key, ())

lookupFinderCache :: IORef FinderCache -> InstalledModule -> IO (Maybe InstalledFindResult)
lookupFinderCache :: IORef FinderCache
-> InstalledModule -> IO (Maybe InstalledFindResult)
lookupFinderCache IORef FinderCache
ref InstalledModule
key = do
   FinderCache
c <- IORef FinderCache -> IO FinderCache
forall a. IORef a -> IO a
readIORef IORef FinderCache
ref
   Maybe InstalledFindResult -> IO (Maybe InstalledFindResult)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe InstalledFindResult -> IO (Maybe InstalledFindResult))
-> Maybe InstalledFindResult -> IO (Maybe InstalledFindResult)
forall a b. (a -> b) -> a -> b
$! FinderCache -> InstalledModule -> Maybe InstalledFindResult
forall a. InstalledModuleEnv a -> InstalledModule -> Maybe a
lookupInstalledModuleEnv FinderCache
c InstalledModule
key

-- -----------------------------------------------------------------------------
-- The three external entry points

-- | Locate a module that was imported by the user.  We have the
-- module's name, and possibly a package name.  Without a package
-- name, this function will use the search path and the known exposed
-- packages to find the module, if a package is specified then only
-- that package is searched for the module.

findImportedModule :: HscEnv -> ModuleName -> Maybe FastString -> IO FindResult
findImportedModule :: HscEnv -> ModuleName -> Maybe FastString -> IO FindResult
findImportedModule HscEnv
hsc_env ModuleName
mod_name Maybe FastString
mb_pkg =
  case Maybe FastString
mb_pkg of
        Maybe FastString
Nothing                        -> IO FindResult
unqual_import
        Just FastString
pkg | FastString
pkg FastString -> FastString -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath -> FastString
fsLit FilePath
"this" -> IO FindResult
home_import -- "this" is special
                 | Bool
otherwise           -> IO FindResult
pkg_import
  where
    home_import :: IO FindResult
home_import   = HscEnv -> ModuleName -> IO FindResult
findHomeModule HscEnv
hsc_env ModuleName
mod_name

    pkg_import :: IO FindResult
pkg_import    = HscEnv -> ModuleName -> Maybe FastString -> IO FindResult
findExposedPackageModule HscEnv
hsc_env ModuleName
mod_name Maybe FastString
mb_pkg

    unqual_import :: IO FindResult
unqual_import = IO FindResult
home_import
                    IO FindResult -> IO FindResult -> IO FindResult
forall (m :: * -> *).
Monad m =>
m FindResult -> m FindResult -> m FindResult
`orIfNotFound`
                    HscEnv -> ModuleName -> Maybe FastString -> IO FindResult
findExposedPackageModule HscEnv
hsc_env ModuleName
mod_name Maybe FastString
forall a. Maybe a
Nothing

-- | Locate a plugin module requested by the user, for a compiler
-- plugin.  This consults the same set of exposed packages as
-- 'findImportedModule', unless @-hide-all-plugin-packages@ or
-- @-plugin-package@ are specified.
findPluginModule :: HscEnv -> ModuleName -> IO FindResult
findPluginModule :: HscEnv -> ModuleName -> IO FindResult
findPluginModule HscEnv
hsc_env ModuleName
mod_name =
  HscEnv -> ModuleName -> IO FindResult
findHomeModule HscEnv
hsc_env ModuleName
mod_name
  IO FindResult -> IO FindResult -> IO FindResult
forall (m :: * -> *).
Monad m =>
m FindResult -> m FindResult -> m FindResult
`orIfNotFound`
  HscEnv -> ModuleName -> IO FindResult
findExposedPluginPackageModule HscEnv
hsc_env ModuleName
mod_name

-- | Locate a specific 'Module'.  The purpose of this function is to
-- create a 'ModLocation' for a given 'Module', that is to find out
-- where the files associated with this module live.  It is used when
-- reading the interface for a module mentioned by another interface,
-- for example (a "system import").

findExactModule :: HscEnv -> InstalledModule -> IO InstalledFindResult
findExactModule :: HscEnv -> InstalledModule -> IO InstalledFindResult
findExactModule HscEnv
hsc_env InstalledModule
mod =
    let home_unit :: HomeUnit
home_unit = HscEnv -> HomeUnit
hsc_home_unit HscEnv
hsc_env
    in if HomeUnit -> InstalledModule -> Bool
forall u. GenHomeUnit u -> InstalledModule -> Bool
isHomeInstalledModule HomeUnit
home_unit InstalledModule
mod
       then HscEnv -> ModuleName -> IO InstalledFindResult
findInstalledHomeModule HscEnv
hsc_env (InstalledModule -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName InstalledModule
mod)
       else HscEnv -> InstalledModule -> IO InstalledFindResult
findPackageModule HscEnv
hsc_env InstalledModule
mod

-- -----------------------------------------------------------------------------
-- Helpers

-- | Given a monadic actions @this@ and @or_this@, first execute
-- @this@.  If the returned 'FindResult' is successful, return
-- it; otherwise, execute @or_this@.  If both failed, this function
-- also combines their failure messages in a reasonable way.
orIfNotFound :: Monad m => m FindResult -> m FindResult -> m FindResult
orIfNotFound :: forall (m :: * -> *).
Monad m =>
m FindResult -> m FindResult -> m FindResult
orIfNotFound m FindResult
this m FindResult
or_this = do
  FindResult
res <- m FindResult
this
  case FindResult
res of
    NotFound { fr_paths :: FindResult -> [FilePath]
fr_paths = [FilePath]
paths1, fr_mods_hidden :: FindResult -> [Unit]
fr_mods_hidden = [Unit]
mh1
             , fr_pkgs_hidden :: FindResult -> [Unit]
fr_pkgs_hidden = [Unit]
ph1, fr_unusables :: FindResult -> [(Unit, UnusableUnitReason)]
fr_unusables = [(Unit, UnusableUnitReason)]
u1, fr_suggestions :: FindResult -> [ModuleSuggestion]
fr_suggestions = [ModuleSuggestion]
s1 }
     -> do FindResult
res2 <- m FindResult
or_this
           case FindResult
res2 of
             NotFound { fr_paths :: FindResult -> [FilePath]
fr_paths = [FilePath]
paths2, fr_pkg :: FindResult -> Maybe Unit
fr_pkg = Maybe Unit
mb_pkg2, fr_mods_hidden :: FindResult -> [Unit]
fr_mods_hidden = [Unit]
mh2
                      , fr_pkgs_hidden :: FindResult -> [Unit]
fr_pkgs_hidden = [Unit]
ph2, fr_unusables :: FindResult -> [(Unit, UnusableUnitReason)]
fr_unusables = [(Unit, UnusableUnitReason)]
u2
                      , fr_suggestions :: FindResult -> [ModuleSuggestion]
fr_suggestions = [ModuleSuggestion]
s2 }
              -> FindResult -> m FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (NotFound { fr_paths :: [FilePath]
fr_paths = [FilePath]
paths1 [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ [FilePath]
paths2
                                  , fr_pkg :: Maybe Unit
fr_pkg = Maybe Unit
mb_pkg2 -- snd arg is the package search
                                  , fr_mods_hidden :: [Unit]
fr_mods_hidden = [Unit]
mh1 [Unit] -> [Unit] -> [Unit]
forall a. [a] -> [a] -> [a]
++ [Unit]
mh2
                                  , fr_pkgs_hidden :: [Unit]
fr_pkgs_hidden = [Unit]
ph1 [Unit] -> [Unit] -> [Unit]
forall a. [a] -> [a] -> [a]
++ [Unit]
ph2
                                  , fr_unusables :: [(Unit, UnusableUnitReason)]
fr_unusables = [(Unit, UnusableUnitReason)]
u1 [(Unit, UnusableUnitReason)]
-> [(Unit, UnusableUnitReason)] -> [(Unit, UnusableUnitReason)]
forall a. [a] -> [a] -> [a]
++ [(Unit, UnusableUnitReason)]
u2
                                  , fr_suggestions :: [ModuleSuggestion]
fr_suggestions = [ModuleSuggestion]
s1  [ModuleSuggestion] -> [ModuleSuggestion] -> [ModuleSuggestion]
forall a. [a] -> [a] -> [a]
++ [ModuleSuggestion]
s2 })
             FindResult
_other -> FindResult -> m FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return FindResult
res2
    FindResult
_other -> FindResult -> m FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return FindResult
res

-- | Helper function for 'findHomeModule': this function wraps an IO action
-- which would look up @mod_name@ in the file system (the home package),
-- and first consults the 'hsc_FC' cache to see if the lookup has already
-- been done.  Otherwise, do the lookup (with the IO action) and save
-- the result in the finder cache and the module location cache (if it
-- was successful.)
homeSearchCache :: HscEnv -> ModuleName -> IO InstalledFindResult -> IO InstalledFindResult
homeSearchCache :: HscEnv
-> ModuleName -> IO InstalledFindResult -> IO InstalledFindResult
homeSearchCache HscEnv
hsc_env ModuleName
mod_name IO InstalledFindResult
do_this = do
  let home_unit :: HomeUnit
home_unit = HscEnv -> HomeUnit
hsc_home_unit HscEnv
hsc_env
      mod :: InstalledModule
mod = HomeUnit -> ModuleName -> InstalledModule
forall u. GenHomeUnit u -> ModuleName -> InstalledModule
mkHomeInstalledModule HomeUnit
home_unit ModuleName
mod_name
  HscEnv
-> InstalledModule
-> IO InstalledFindResult
-> IO InstalledFindResult
modLocationCache HscEnv
hsc_env InstalledModule
mod IO InstalledFindResult
do_this

findExposedPackageModule :: HscEnv -> ModuleName -> Maybe FastString
                         -> IO FindResult
findExposedPackageModule :: HscEnv -> ModuleName -> Maybe FastString -> IO FindResult
findExposedPackageModule HscEnv
hsc_env ModuleName
mod_name Maybe FastString
mb_pkg
  = HscEnv -> LookupResult -> IO FindResult
findLookupResult HscEnv
hsc_env
  (LookupResult -> IO FindResult) -> LookupResult -> IO FindResult
forall a b. (a -> b) -> a -> b
$ UnitState -> ModuleName -> Maybe FastString -> LookupResult
lookupModuleWithSuggestions
        (HscEnv -> UnitState
hsc_units HscEnv
hsc_env) ModuleName
mod_name Maybe FastString
mb_pkg

findExposedPluginPackageModule :: HscEnv -> ModuleName
                               -> IO FindResult
findExposedPluginPackageModule :: HscEnv -> ModuleName -> IO FindResult
findExposedPluginPackageModule HscEnv
hsc_env ModuleName
mod_name
  = HscEnv -> LookupResult -> IO FindResult
findLookupResult HscEnv
hsc_env
  (LookupResult -> IO FindResult) -> LookupResult -> IO FindResult
forall a b. (a -> b) -> a -> b
$ UnitState -> ModuleName -> Maybe FastString -> LookupResult
lookupPluginModuleWithSuggestions
        (HscEnv -> UnitState
hsc_units HscEnv
hsc_env) ModuleName
mod_name Maybe FastString
forall a. Maybe a
Nothing

findLookupResult :: HscEnv -> LookupResult -> IO FindResult
findLookupResult :: HscEnv -> LookupResult -> IO FindResult
findLookupResult HscEnv
hsc_env LookupResult
r = case LookupResult
r of
     LookupFound Module
m UnitInfo
pkg_conf -> do
       let im :: InstalledModule
im = (InstalledModule, Maybe InstantiatedModule) -> InstalledModule
forall a b. (a, b) -> a
fst (Module -> (InstalledModule, Maybe InstantiatedModule)
getModuleInstantiation Module
m)
       InstalledFindResult
r' <- HscEnv -> InstalledModule -> UnitInfo -> IO InstalledFindResult
findPackageModule_ HscEnv
hsc_env InstalledModule
im UnitInfo
pkg_conf
       case InstalledFindResult
r' of
        -- TODO: ghc -M is unlikely to do the right thing
        -- with just the location of the thing that was
        -- instantiated; you probably also need all of the
        -- implicit locations from the instances
        InstalledFound ModLocation
loc   InstalledModule
_ -> FindResult -> IO FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (ModLocation -> Module -> FindResult
Found ModLocation
loc Module
m)
        InstalledNoPackage   UnitId
_ -> FindResult -> IO FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (Unit -> FindResult
NoPackage (Module -> Unit
forall unit. GenModule unit -> unit
moduleUnit Module
m))
        InstalledNotFound [FilePath]
fp Maybe UnitId
_ -> FindResult -> IO FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (NotFound{ fr_paths :: [FilePath]
fr_paths = [FilePath]
fp, fr_pkg :: Maybe Unit
fr_pkg = Unit -> Maybe Unit
forall a. a -> Maybe a
Just (Module -> Unit
forall unit. GenModule unit -> unit
moduleUnit Module
m)
                                         , fr_pkgs_hidden :: [Unit]
fr_pkgs_hidden = []
                                         , fr_mods_hidden :: [Unit]
fr_mods_hidden = []
                                         , fr_unusables :: [(Unit, UnusableUnitReason)]
fr_unusables = []
                                         , fr_suggestions :: [ModuleSuggestion]
fr_suggestions = []})
     LookupMultiple [(Module, ModuleOrigin)]
rs ->
       FindResult -> IO FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Module, ModuleOrigin)] -> FindResult
FoundMultiple [(Module, ModuleOrigin)]
rs)
     LookupHidden [(Module, ModuleOrigin)]
pkg_hiddens [(Module, ModuleOrigin)]
mod_hiddens ->
       FindResult -> IO FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (NotFound{ fr_paths :: [FilePath]
fr_paths = [], fr_pkg :: Maybe Unit
fr_pkg = Maybe Unit
forall a. Maybe a
Nothing
                       , fr_pkgs_hidden :: [Unit]
fr_pkgs_hidden = ((Module, ModuleOrigin) -> Unit)
-> [(Module, ModuleOrigin)] -> [Unit]
forall a b. (a -> b) -> [a] -> [b]
map (Module -> Unit
forall unit. GenModule unit -> unit
moduleUnit(Module -> Unit)
-> ((Module, ModuleOrigin) -> Module)
-> (Module, ModuleOrigin)
-> Unit
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Module, ModuleOrigin) -> Module
forall a b. (a, b) -> a
fst) [(Module, ModuleOrigin)]
pkg_hiddens
                       , fr_mods_hidden :: [Unit]
fr_mods_hidden = ((Module, ModuleOrigin) -> Unit)
-> [(Module, ModuleOrigin)] -> [Unit]
forall a b. (a -> b) -> [a] -> [b]
map (Module -> Unit
forall unit. GenModule unit -> unit
moduleUnit(Module -> Unit)
-> ((Module, ModuleOrigin) -> Module)
-> (Module, ModuleOrigin)
-> Unit
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Module, ModuleOrigin) -> Module
forall a b. (a, b) -> a
fst) [(Module, ModuleOrigin)]
mod_hiddens
                       , fr_unusables :: [(Unit, UnusableUnitReason)]
fr_unusables = []
                       , fr_suggestions :: [ModuleSuggestion]
fr_suggestions = [] })
     LookupUnusable [(Module, ModuleOrigin)]
unusable ->
       let unusables' :: [(Unit, UnusableUnitReason)]
unusables' = ((Module, ModuleOrigin) -> (Unit, UnusableUnitReason))
-> [(Module, ModuleOrigin)] -> [(Unit, UnusableUnitReason)]
forall a b. (a -> b) -> [a] -> [b]
map (Module, ModuleOrigin) -> (Unit, UnusableUnitReason)
forall {a}. (GenModule a, ModuleOrigin) -> (a, UnusableUnitReason)
get_unusable [(Module, ModuleOrigin)]
unusable
           get_unusable :: (GenModule a, ModuleOrigin) -> (a, UnusableUnitReason)
get_unusable (GenModule a
m, ModUnusable UnusableUnitReason
r) = (GenModule a -> a
forall unit. GenModule unit -> unit
moduleUnit GenModule a
m, UnusableUnitReason
r)
           get_unusable (GenModule a
_, ModuleOrigin
r)             =
             FilePath -> SDoc -> (a, UnusableUnitReason)
forall a. HasCallStack => FilePath -> SDoc -> a
pprPanic FilePath
"findLookupResult: unexpected origin" (ModuleOrigin -> SDoc
forall a. Outputable a => a -> SDoc
ppr ModuleOrigin
r)
       in FindResult -> IO FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (NotFound{ fr_paths :: [FilePath]
fr_paths = [], fr_pkg :: Maybe Unit
fr_pkg = Maybe Unit
forall a. Maybe a
Nothing
                          , fr_pkgs_hidden :: [Unit]
fr_pkgs_hidden = []
                          , fr_mods_hidden :: [Unit]
fr_mods_hidden = []
                          , fr_unusables :: [(Unit, UnusableUnitReason)]
fr_unusables = [(Unit, UnusableUnitReason)]
unusables'
                          , fr_suggestions :: [ModuleSuggestion]
fr_suggestions = [] })
     LookupNotFound [ModuleSuggestion]
suggest -> do
       let suggest' :: [ModuleSuggestion]
suggest'
             | GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_HelpfulErrors (HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env) = [ModuleSuggestion]
suggest
             | Bool
otherwise = []
       FindResult -> IO FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (NotFound{ fr_paths :: [FilePath]
fr_paths = [], fr_pkg :: Maybe Unit
fr_pkg = Maybe Unit
forall a. Maybe a
Nothing
                       , fr_pkgs_hidden :: [Unit]
fr_pkgs_hidden = []
                       , fr_mods_hidden :: [Unit]
fr_mods_hidden = []
                       , fr_unusables :: [(Unit, UnusableUnitReason)]
fr_unusables = []
                       , fr_suggestions :: [ModuleSuggestion]
fr_suggestions = [ModuleSuggestion]
suggest' })

modLocationCache :: HscEnv -> InstalledModule -> IO InstalledFindResult -> IO InstalledFindResult
modLocationCache :: HscEnv
-> InstalledModule
-> IO InstalledFindResult
-> IO InstalledFindResult
modLocationCache HscEnv
hsc_env InstalledModule
mod IO InstalledFindResult
do_this = do
  Maybe InstalledFindResult
m <- IORef FinderCache
-> InstalledModule -> IO (Maybe InstalledFindResult)
lookupFinderCache (HscEnv -> IORef FinderCache
hsc_FC HscEnv
hsc_env) InstalledModule
mod
  case Maybe InstalledFindResult
m of
    Just InstalledFindResult
result -> InstalledFindResult -> IO InstalledFindResult
forall (m :: * -> *) a. Monad m => a -> m a
return InstalledFindResult
result
    Maybe InstalledFindResult
Nothing     -> do
        InstalledFindResult
result <- IO InstalledFindResult
do_this
        IORef FinderCache
-> InstalledModule -> InstalledFindResult -> IO ()
addToFinderCache (HscEnv -> IORef FinderCache
hsc_FC HscEnv
hsc_env) InstalledModule
mod InstalledFindResult
result
        InstalledFindResult -> IO InstalledFindResult
forall (m :: * -> *) a. Monad m => a -> m a
return InstalledFindResult
result

-- This returns a module because it's more convenient for users
addHomeModuleToFinder :: HscEnv -> ModuleName -> ModLocation -> IO Module
addHomeModuleToFinder :: HscEnv -> ModuleName -> ModLocation -> IO Module
addHomeModuleToFinder HscEnv
hsc_env ModuleName
mod_name ModLocation
loc = do
  let home_unit :: HomeUnit
home_unit = HscEnv -> HomeUnit
hsc_home_unit HscEnv
hsc_env
      mod :: InstalledModule
mod = HomeUnit -> ModuleName -> InstalledModule
forall u. GenHomeUnit u -> ModuleName -> InstalledModule
mkHomeInstalledModule HomeUnit
home_unit ModuleName
mod_name
  IORef FinderCache
-> InstalledModule -> InstalledFindResult -> IO ()
addToFinderCache (HscEnv -> IORef FinderCache
hsc_FC HscEnv
hsc_env) InstalledModule
mod (ModLocation -> InstalledModule -> InstalledFindResult
InstalledFound ModLocation
loc InstalledModule
mod)
  Module -> IO Module
forall (m :: * -> *) a. Monad m => a -> m a
return (HomeUnit -> ModuleName -> Module
mkHomeModule HomeUnit
home_unit ModuleName
mod_name)

uncacheModule :: HscEnv -> ModuleName -> IO ()
uncacheModule :: HscEnv -> ModuleName -> IO ()
uncacheModule HscEnv
hsc_env ModuleName
mod_name = do
  let home_unit :: HomeUnit
home_unit = HscEnv -> HomeUnit
hsc_home_unit HscEnv
hsc_env
      mod :: InstalledModule
mod = HomeUnit -> ModuleName -> InstalledModule
forall u. GenHomeUnit u -> ModuleName -> InstalledModule
mkHomeInstalledModule HomeUnit
home_unit ModuleName
mod_name
  IORef FinderCache -> InstalledModule -> IO ()
removeFromFinderCache (HscEnv -> IORef FinderCache
hsc_FC HscEnv
hsc_env) InstalledModule
mod

-- -----------------------------------------------------------------------------
--      The internal workers

findHomeModule :: HscEnv -> ModuleName -> IO FindResult
findHomeModule :: HscEnv -> ModuleName -> IO FindResult
findHomeModule HscEnv
hsc_env ModuleName
mod_name = do
  InstalledFindResult
r <- HscEnv -> ModuleName -> IO InstalledFindResult
findInstalledHomeModule HscEnv
hsc_env ModuleName
mod_name
  FindResult -> IO FindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (FindResult -> IO FindResult) -> FindResult -> IO FindResult
forall a b. (a -> b) -> a -> b
$ case InstalledFindResult
r of
    InstalledFound ModLocation
loc InstalledModule
_ -> ModLocation -> Module -> FindResult
Found ModLocation
loc (HomeUnit -> ModuleName -> Module
mkHomeModule HomeUnit
home_unit ModuleName
mod_name)
    InstalledNoPackage UnitId
_ -> Unit -> FindResult
NoPackage Unit
uid -- impossible
    InstalledNotFound [FilePath]
fps Maybe UnitId
_ -> NotFound {
        fr_paths :: [FilePath]
fr_paths = [FilePath]
fps,
        fr_pkg :: Maybe Unit
fr_pkg = Unit -> Maybe Unit
forall a. a -> Maybe a
Just Unit
uid,
        fr_mods_hidden :: [Unit]
fr_mods_hidden = [],
        fr_pkgs_hidden :: [Unit]
fr_pkgs_hidden = [],
        fr_unusables :: [(Unit, UnusableUnitReason)]
fr_unusables = [],
        fr_suggestions :: [ModuleSuggestion]
fr_suggestions = []
      }
 where
  home_unit :: HomeUnit
home_unit = HscEnv -> HomeUnit
hsc_home_unit HscEnv
hsc_env
  uid :: Unit
uid       = HomeUnit -> Unit
homeUnitAsUnit HomeUnit
home_unit

-- | Implements the search for a module name in the home package only.  Calling
-- this function directly is usually *not* what you want; currently, it's used
-- as a building block for the following operations:
--
--  1. When you do a normal package lookup, we first check if the module
--  is available in the home module, before looking it up in the package
--  database.
--
--  2. When you have a package qualified import with package name "this",
--  we shortcut to the home module.
--
--  3. When we look up an exact 'Module', if the unit id associated with
--  the module is the current home module do a look up in the home module.
--
--  4. Some special-case code in GHCi (ToDo: Figure out why that needs to
--  call this.)
findInstalledHomeModule :: HscEnv -> ModuleName -> IO InstalledFindResult
findInstalledHomeModule :: HscEnv -> ModuleName -> IO InstalledFindResult
findInstalledHomeModule HscEnv
hsc_env ModuleName
mod_name =
   HscEnv
-> ModuleName -> IO InstalledFindResult -> IO InstalledFindResult
homeSearchCache HscEnv
hsc_env ModuleName
mod_name (IO InstalledFindResult -> IO InstalledFindResult)
-> IO InstalledFindResult -> IO InstalledFindResult
forall a b. (a -> b) -> a -> b
$
   let
     dflags :: DynFlags
dflags = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env
     home_unit :: HomeUnit
home_unit = HscEnv -> HomeUnit
hsc_home_unit HscEnv
hsc_env
     home_path :: [FilePath]
home_path = DynFlags -> [FilePath]
importPaths DynFlags
dflags
     hisuf :: FilePath
hisuf = DynFlags -> FilePath
hiSuf DynFlags
dflags
     mod :: InstalledModule
mod = HomeUnit -> ModuleName -> InstalledModule
forall u. GenHomeUnit u -> ModuleName -> InstalledModule
mkHomeInstalledModule HomeUnit
home_unit ModuleName
mod_name

     source_exts :: [(FilePath, FilePath -> FilePath -> IO ModLocation)]
source_exts =
      [ (FilePath
"hs",   DynFlags
-> ModuleName -> FilePath -> FilePath -> FilePath -> IO ModLocation
mkHomeModLocationSearched DynFlags
dflags ModuleName
mod_name FilePath
"hs")
      , (FilePath
"lhs",  DynFlags
-> ModuleName -> FilePath -> FilePath -> FilePath -> IO ModLocation
mkHomeModLocationSearched DynFlags
dflags ModuleName
mod_name FilePath
"lhs")
      , (FilePath
"hsig",  DynFlags
-> ModuleName -> FilePath -> FilePath -> FilePath -> IO ModLocation
mkHomeModLocationSearched DynFlags
dflags ModuleName
mod_name FilePath
"hsig")
      , (FilePath
"lhsig",  DynFlags
-> ModuleName -> FilePath -> FilePath -> FilePath -> IO ModLocation
mkHomeModLocationSearched DynFlags
dflags ModuleName
mod_name FilePath
"lhsig")
      ]

     -- we use mkHomeModHiOnlyLocation instead of mkHiOnlyModLocation so that
     -- when hiDir field is set in dflags, we know to look there (see #16500)
     hi_exts :: [(FilePath, FilePath -> FilePath -> IO ModLocation)]
hi_exts = [ (FilePath
hisuf,                DynFlags -> ModuleName -> FilePath -> FilePath -> IO ModLocation
mkHomeModHiOnlyLocation DynFlags
dflags ModuleName
mod_name)
               , (FilePath -> FilePath
addBootSuffix FilePath
hisuf,  DynFlags -> ModuleName -> FilePath -> FilePath -> IO ModLocation
mkHomeModHiOnlyLocation DynFlags
dflags ModuleName
mod_name)
               ]

        -- In compilation manager modes, we look for source files in the home
        -- package because we can compile these automatically.  In one-shot
        -- compilation mode we look for .hi and .hi-boot files only.
     exts :: [(FilePath, FilePath -> FilePath -> IO ModLocation)]
exts | GhcMode -> Bool
isOneShot (DynFlags -> GhcMode
ghcMode DynFlags
dflags) = [(FilePath, FilePath -> FilePath -> IO ModLocation)]
hi_exts
          | Bool
otherwise                  = [(FilePath, FilePath -> FilePath -> IO ModLocation)]
source_exts
   in

  -- special case for GHC.Prim; we won't find it in the filesystem.
  -- This is important only when compiling the base package (where GHC.Prim
  -- is a home module).
  if InstalledModule
mod InstalledModule -> Module -> Bool
`installedModuleEq` Module
gHC_PRIM
        then InstalledFindResult -> IO InstalledFindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (ModLocation -> InstalledModule -> InstalledFindResult
InstalledFound (FilePath -> ModLocation
forall a. HasCallStack => FilePath -> a
error FilePath
"GHC.Prim ModLocation") InstalledModule
mod)
        else [FilePath]
-> InstalledModule
-> [(FilePath, FilePath -> FilePath -> IO ModLocation)]
-> IO InstalledFindResult
searchPathExts [FilePath]
home_path InstalledModule
mod [(FilePath, FilePath -> FilePath -> IO ModLocation)]
exts


-- | Search for a module in external packages only.
findPackageModule :: HscEnv -> InstalledModule -> IO InstalledFindResult
findPackageModule :: HscEnv -> InstalledModule -> IO InstalledFindResult
findPackageModule HscEnv
hsc_env InstalledModule
mod = do
  let pkg_id :: UnitId
pkg_id = InstalledModule -> UnitId
forall unit. GenModule unit -> unit
moduleUnit InstalledModule
mod
  case UnitState -> UnitId -> Maybe UnitInfo
lookupUnitId (HscEnv -> UnitState
hsc_units HscEnv
hsc_env) UnitId
pkg_id of
     Maybe UnitInfo
Nothing -> InstalledFindResult -> IO InstalledFindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (UnitId -> InstalledFindResult
InstalledNoPackage UnitId
pkg_id)
     Just UnitInfo
u  -> HscEnv -> InstalledModule -> UnitInfo -> IO InstalledFindResult
findPackageModule_ HscEnv
hsc_env InstalledModule
mod UnitInfo
u

-- | Look up the interface file associated with module @mod@.  This function
-- requires a few invariants to be upheld: (1) the 'Module' in question must
-- be the module identifier of the *original* implementation of a module,
-- not a reexport (this invariant is upheld by "GHC.Unit.State") and (2)
-- the 'UnitInfo' must be consistent with the unit id in the 'Module'.
-- The redundancy is to avoid an extra lookup in the package state
-- for the appropriate config.
findPackageModule_ :: HscEnv -> InstalledModule -> UnitInfo -> IO InstalledFindResult
findPackageModule_ :: HscEnv -> InstalledModule -> UnitInfo -> IO InstalledFindResult
findPackageModule_ HscEnv
hsc_env InstalledModule
mod UnitInfo
pkg_conf =
  ASSERT2( moduleUnit mod == unitId pkg_conf, ppr (moduleUnit mod) <+> ppr (unitId pkg_conf) )
  HscEnv
-> InstalledModule
-> IO InstalledFindResult
-> IO InstalledFindResult
modLocationCache HscEnv
hsc_env InstalledModule
mod (IO InstalledFindResult -> IO InstalledFindResult)
-> IO InstalledFindResult -> IO InstalledFindResult
forall a b. (a -> b) -> a -> b
$

  -- special case for GHC.Prim; we won't find it in the filesystem.
  if InstalledModule
mod InstalledModule -> Module -> Bool
`installedModuleEq` Module
gHC_PRIM
        then InstalledFindResult -> IO InstalledFindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (ModLocation -> InstalledModule -> InstalledFindResult
InstalledFound (FilePath -> ModLocation
forall a. HasCallStack => FilePath -> a
error FilePath
"GHC.Prim ModLocation") InstalledModule
mod)
        else

  let
     dflags :: DynFlags
dflags = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env
     tag :: FilePath
tag = Ways -> FilePath
waysBuildTag (DynFlags -> Ways
ways DynFlags
dflags)

           -- hi-suffix for packages depends on the build tag.
     package_hisuf :: FilePath
package_hisuf | FilePath -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null FilePath
tag  = FilePath
"hi"
                   | Bool
otherwise = FilePath
tag FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"_hi"

     mk_hi_loc :: FilePath -> FilePath -> IO ModLocation
mk_hi_loc = DynFlags -> FilePath -> FilePath -> FilePath -> IO ModLocation
mkHiOnlyModLocation DynFlags
dflags FilePath
package_hisuf

     import_dirs :: [FilePath]
import_dirs = (FilePathST -> FilePath) -> [FilePathST] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map FilePathST -> FilePath
ST.unpack ([FilePathST] -> [FilePath]) -> [FilePathST] -> [FilePath]
forall a b. (a -> b) -> a -> b
$ UnitInfo -> [FilePathST]
forall compid srcpkgid srcpkgname uid modulename mod.
GenericUnitInfo compid srcpkgid srcpkgname uid modulename mod
-> [FilePathST]
unitImportDirs UnitInfo
pkg_conf
      -- we never look for a .hi-boot file in an external package;
      -- .hi-boot files only make sense for the home package.
  in
  case [FilePath]
import_dirs of
    [FilePath
one] | GhcMode
MkDepend <- DynFlags -> GhcMode
ghcMode DynFlags
dflags -> do
          -- there's only one place that this .hi file can be, so
          -- don't bother looking for it.
          let basename :: FilePath
basename = ModuleName -> FilePath
moduleNameSlashes (InstalledModule -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName InstalledModule
mod)
          ModLocation
loc <- FilePath -> FilePath -> IO ModLocation
mk_hi_loc FilePath
one FilePath
basename
          InstalledFindResult -> IO InstalledFindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (ModLocation -> InstalledModule -> InstalledFindResult
InstalledFound ModLocation
loc InstalledModule
mod)
    [FilePath]
_otherwise ->
          [FilePath]
-> InstalledModule
-> [(FilePath, FilePath -> FilePath -> IO ModLocation)]
-> IO InstalledFindResult
searchPathExts [FilePath]
import_dirs InstalledModule
mod [(FilePath
package_hisuf, FilePath -> FilePath -> IO ModLocation
mk_hi_loc)]

-- -----------------------------------------------------------------------------
-- General path searching

searchPathExts :: [FilePath]      -- paths to search
               -> InstalledModule -- module name
               -> [ (
                     FileExt,                                -- suffix
                     FilePath -> BaseName -> IO ModLocation  -- action
                    )
                  ]
               -> IO InstalledFindResult

searchPathExts :: [FilePath]
-> InstalledModule
-> [(FilePath, FilePath -> FilePath -> IO ModLocation)]
-> IO InstalledFindResult
searchPathExts [FilePath]
paths InstalledModule
mod [(FilePath, FilePath -> FilePath -> IO ModLocation)]
exts = [(FilePath, IO ModLocation)] -> IO InstalledFindResult
search [(FilePath, IO ModLocation)]
to_search
  where
    basename :: FilePath
basename = ModuleName -> FilePath
moduleNameSlashes (InstalledModule -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName InstalledModule
mod)

    to_search :: [(FilePath, IO ModLocation)]
    to_search :: [(FilePath, IO ModLocation)]
to_search = [ (FilePath
file, FilePath -> FilePath -> IO ModLocation
fn FilePath
path FilePath
basename)
                | FilePath
path <- [FilePath]
paths,
                  (FilePath
ext,FilePath -> FilePath -> IO ModLocation
fn) <- [(FilePath, FilePath -> FilePath -> IO ModLocation)]
exts,
                  let base :: FilePath
base | FilePath
path FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath
"." = FilePath
basename
                           | Bool
otherwise   = FilePath
path FilePath -> FilePath -> FilePath
</> FilePath
basename
                      file :: FilePath
file = FilePath
base FilePath -> FilePath -> FilePath
<.> FilePath
ext
                ]

    search :: [(FilePath, IO ModLocation)] -> IO InstalledFindResult
search [] = InstalledFindResult -> IO InstalledFindResult
forall (m :: * -> *) a. Monad m => a -> m a
return ([FilePath] -> Maybe UnitId -> InstalledFindResult
InstalledNotFound (((FilePath, IO ModLocation) -> FilePath)
-> [(FilePath, IO ModLocation)] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map (FilePath, IO ModLocation) -> FilePath
forall a b. (a, b) -> a
fst [(FilePath, IO ModLocation)]
to_search) (UnitId -> Maybe UnitId
forall a. a -> Maybe a
Just (InstalledModule -> UnitId
forall unit. GenModule unit -> unit
moduleUnit InstalledModule
mod)))

    search ((FilePath
file, IO ModLocation
mk_result) : [(FilePath, IO ModLocation)]
rest) = do
      Bool
b <- FilePath -> IO Bool
doesFileExist FilePath
file
      if Bool
b
        then do { ModLocation
loc <- IO ModLocation
mk_result; InstalledFindResult -> IO InstalledFindResult
forall (m :: * -> *) a. Monad m => a -> m a
return (ModLocation -> InstalledModule -> InstalledFindResult
InstalledFound ModLocation
loc InstalledModule
mod) }
        else [(FilePath, IO ModLocation)] -> IO InstalledFindResult
search [(FilePath, IO ModLocation)]
rest

mkHomeModLocationSearched :: DynFlags -> ModuleName -> FileExt
                          -> FilePath -> BaseName -> IO ModLocation
mkHomeModLocationSearched :: DynFlags
-> ModuleName -> FilePath -> FilePath -> FilePath -> IO ModLocation
mkHomeModLocationSearched DynFlags
dflags ModuleName
mod FilePath
suff FilePath
path FilePath
basename =
  DynFlags -> ModuleName -> FilePath -> FilePath -> IO ModLocation
mkHomeModLocation2 DynFlags
dflags ModuleName
mod (FilePath
path FilePath -> FilePath -> FilePath
</> FilePath
basename) FilePath
suff

-- -----------------------------------------------------------------------------
-- 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
--      The name of the module
--
-- path
--      (a): The search path component where the source file was found.
--      (b) and (c): "."
--
-- src_basename
--      (a): (moduleNameSlashes mod)
--      (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 :: DynFlags -> ModuleName -> FilePath -> IO ModLocation
mkHomeModLocation :: DynFlags -> ModuleName -> FilePath -> IO ModLocation
mkHomeModLocation DynFlags
dflags ModuleName
mod FilePath
src_filename = do
   let (FilePath
basename,FilePath
extension) = FilePath -> (FilePath, FilePath)
splitExtension FilePath
src_filename
   DynFlags -> ModuleName -> FilePath -> FilePath -> IO ModLocation
mkHomeModLocation2 DynFlags
dflags ModuleName
mod FilePath
basename FilePath
extension

mkHomeModLocation2 :: DynFlags
                   -> ModuleName
                   -> FilePath  -- Of source module, without suffix
                   -> String    -- Suffix
                   -> IO ModLocation
mkHomeModLocation2 :: DynFlags -> ModuleName -> FilePath -> FilePath -> IO ModLocation
mkHomeModLocation2 DynFlags
dflags ModuleName
mod FilePath
src_basename FilePath
ext = do
   let mod_basename :: FilePath
mod_basename = ModuleName -> FilePath
moduleNameSlashes ModuleName
mod

       obj_fn :: FilePath
obj_fn = DynFlags -> FilePath -> FilePath -> FilePath
mkObjPath  DynFlags
dflags FilePath
src_basename FilePath
mod_basename
       hi_fn :: FilePath
hi_fn  = DynFlags -> FilePath -> FilePath -> FilePath
mkHiPath   DynFlags
dflags FilePath
src_basename FilePath
mod_basename
       hie_fn :: FilePath
hie_fn = DynFlags -> FilePath -> FilePath -> FilePath
mkHiePath  DynFlags
dflags FilePath
src_basename FilePath
mod_basename

   ModLocation -> IO ModLocation
forall (m :: * -> *) a. Monad m => a -> m a
return (ModLocation{ ml_hs_file :: Maybe FilePath
ml_hs_file   = FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just (FilePath
src_basename FilePath -> FilePath -> FilePath
<.> FilePath
ext),
                        ml_hi_file :: FilePath
ml_hi_file   = FilePath
hi_fn,
                        ml_obj_file :: FilePath
ml_obj_file  = FilePath
obj_fn,
                        ml_hie_file :: FilePath
ml_hie_file  = FilePath
hie_fn })

mkHomeModHiOnlyLocation :: DynFlags
                        -> ModuleName
                        -> FilePath
                        -> BaseName
                        -> IO ModLocation
mkHomeModHiOnlyLocation :: DynFlags -> ModuleName -> FilePath -> FilePath -> IO ModLocation
mkHomeModHiOnlyLocation DynFlags
dflags ModuleName
mod FilePath
path FilePath
basename = do
   ModLocation
loc <- DynFlags -> ModuleName -> FilePath -> FilePath -> IO ModLocation
mkHomeModLocation2 DynFlags
dflags ModuleName
mod (FilePath
path FilePath -> FilePath -> FilePath
</> FilePath
basename) FilePath
""
   ModLocation -> IO ModLocation
forall (m :: * -> *) a. Monad m => a -> m a
return ModLocation
loc { ml_hs_file :: Maybe FilePath
ml_hs_file = Maybe FilePath
forall a. Maybe a
Nothing }

mkHiOnlyModLocation :: DynFlags -> Suffix -> FilePath -> String
                    -> IO ModLocation
mkHiOnlyModLocation :: DynFlags -> FilePath -> FilePath -> FilePath -> IO ModLocation
mkHiOnlyModLocation DynFlags
dflags FilePath
hisuf FilePath
path FilePath
basename
 = do let full_basename :: FilePath
full_basename = FilePath
path FilePath -> FilePath -> FilePath
</> FilePath
basename
          obj_fn :: FilePath
obj_fn = DynFlags -> FilePath -> FilePath -> FilePath
mkObjPath  DynFlags
dflags FilePath
full_basename FilePath
basename
          hie_fn :: FilePath
hie_fn = DynFlags -> FilePath -> FilePath -> FilePath
mkHiePath  DynFlags
dflags FilePath
full_basename FilePath
basename
      ModLocation -> IO ModLocation
forall (m :: * -> *) a. Monad m => a -> m a
return ModLocation{    ml_hs_file :: Maybe FilePath
ml_hs_file   = Maybe FilePath
forall a. Maybe a
Nothing,
                             ml_hi_file :: FilePath
ml_hi_file   = FilePath
full_basename FilePath -> FilePath -> FilePath
<.> FilePath
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 :: FilePath
ml_obj_file  = FilePath
obj_fn,
                             ml_hie_file :: FilePath
ml_hie_file  = FilePath
hie_fn
                  }

-- | Constructs the filename of a .o file for a given source file.
-- Does /not/ check whether the .o file exists
mkObjPath
  :: DynFlags
  -> FilePath           -- the filename of the source file, minus the extension
  -> String             -- the module name with dots replaced by slashes
  -> FilePath
mkObjPath :: DynFlags -> FilePath -> FilePath -> FilePath
mkObjPath DynFlags
dflags FilePath
basename FilePath
mod_basename = FilePath
obj_basename FilePath -> FilePath -> FilePath
<.> FilePath
osuf
  where
                odir :: Maybe FilePath
odir = DynFlags -> Maybe FilePath
objectDir DynFlags
dflags
                osuf :: FilePath
osuf = DynFlags -> FilePath
objectSuf DynFlags
dflags

                obj_basename :: FilePath
obj_basename | Just FilePath
dir <- Maybe FilePath
odir = FilePath
dir FilePath -> FilePath -> FilePath
</> FilePath
mod_basename
                             | Bool
otherwise        = FilePath
basename


-- | Constructs the filename of a .hi file for a given source file.
-- Does /not/ check whether the .hi file exists
mkHiPath
  :: DynFlags
  -> FilePath           -- the filename of the source file, minus the extension
  -> String             -- the module name with dots replaced by slashes
  -> FilePath
mkHiPath :: DynFlags -> FilePath -> FilePath -> FilePath
mkHiPath DynFlags
dflags FilePath
basename FilePath
mod_basename = FilePath
hi_basename FilePath -> FilePath -> FilePath
<.> FilePath
hisuf
 where
                hidir :: Maybe FilePath
hidir = DynFlags -> Maybe FilePath
hiDir DynFlags
dflags
                hisuf :: FilePath
hisuf = DynFlags -> FilePath
hiSuf DynFlags
dflags

                hi_basename :: FilePath
hi_basename | Just FilePath
dir <- Maybe FilePath
hidir = FilePath
dir FilePath -> FilePath -> FilePath
</> FilePath
mod_basename
                            | Bool
otherwise         = FilePath
basename

-- | Constructs the filename of a .hie file for a given source file.
-- Does /not/ check whether the .hie file exists
mkHiePath
  :: DynFlags
  -> FilePath           -- the filename of the source file, minus the extension
  -> String             -- the module name with dots replaced by slashes
  -> FilePath
mkHiePath :: DynFlags -> FilePath -> FilePath -> FilePath
mkHiePath DynFlags
dflags FilePath
basename FilePath
mod_basename = FilePath
hie_basename FilePath -> FilePath -> FilePath
<.> FilePath
hiesuf
 where
                hiedir :: Maybe FilePath
hiedir = DynFlags -> Maybe FilePath
hieDir DynFlags
dflags
                hiesuf :: FilePath
hiesuf = DynFlags -> FilePath
hieSuf DynFlags
dflags

                hie_basename :: FilePath
hie_basename | Just FilePath
dir <- Maybe FilePath
hiedir = FilePath
dir FilePath -> FilePath -> FilePath
</> FilePath
mod_basename
                             | Bool
otherwise          = FilePath
basename



-- -----------------------------------------------------------------------------
-- Filenames of the stub files

-- We don't have to store these in ModLocations, because they can be derived
-- from other available information, and they're only rarely needed.

mkStubPaths
  :: DynFlags
  -> ModuleName
  -> ModLocation
  -> FilePath

mkStubPaths :: DynFlags -> ModuleName -> ModLocation -> FilePath
mkStubPaths DynFlags
dflags ModuleName
mod ModLocation
location
  = let
        stubdir :: Maybe FilePath
stubdir = DynFlags -> Maybe FilePath
stubDir DynFlags
dflags

        mod_basename :: FilePath
mod_basename = ModuleName -> FilePath
moduleNameSlashes ModuleName
mod
        src_basename :: FilePath
src_basename = FilePath -> FilePath
dropExtension (FilePath -> FilePath) -> FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> Maybe FilePath -> FilePath
forall a. HasCallStack => FilePath -> Maybe a -> a
expectJust FilePath
"mkStubPaths"
                                                  (ModLocation -> Maybe FilePath
ml_hs_file ModLocation
location)

        stub_basename0 :: FilePath
stub_basename0
            | Just FilePath
dir <- Maybe FilePath
stubdir = FilePath
dir FilePath -> FilePath -> FilePath
</> FilePath
mod_basename
            | Bool
otherwise           = FilePath
src_basename

        stub_basename :: FilePath
stub_basename = FilePath
stub_basename0 FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"_stub"
     in
        FilePath
stub_basename FilePath -> FilePath -> FilePath
<.> FilePath
"h"

-- -----------------------------------------------------------------------------
-- findLinkable isn't related to the other stuff in here,
-- but there's no other obvious place for it

findObjectLinkableMaybe :: Module -> ModLocation -> IO (Maybe Linkable)
findObjectLinkableMaybe :: Module -> ModLocation -> IO (Maybe Linkable)
findObjectLinkableMaybe Module
mod ModLocation
locn
   = do let obj_fn :: FilePath
obj_fn = ModLocation -> FilePath
ml_obj_file ModLocation
locn
        Maybe UTCTime
maybe_obj_time <- FilePath -> IO (Maybe UTCTime)
modificationTimeIfExists FilePath
obj_fn
        case Maybe UTCTime
maybe_obj_time of
          Maybe UTCTime
Nothing -> Maybe Linkable -> IO (Maybe Linkable)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Linkable
forall a. Maybe a
Nothing
          Just UTCTime
obj_time -> (Linkable -> Maybe Linkable) -> IO Linkable -> IO (Maybe Linkable)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Linkable -> Maybe Linkable
forall a. a -> Maybe a
Just (Module -> FilePath -> UTCTime -> IO Linkable
findObjectLinkable Module
mod FilePath
obj_fn UTCTime
obj_time)

-- Make an object linkable when we know the object file exists, and we know
-- its modification time.
findObjectLinkable :: Module -> FilePath -> UTCTime -> IO Linkable
findObjectLinkable :: Module -> FilePath -> UTCTime -> IO Linkable
findObjectLinkable Module
mod FilePath
obj_fn UTCTime
obj_time = Linkable -> IO Linkable
forall (m :: * -> *) a. Monad m => a -> m a
return (UTCTime -> Module -> [Unlinked] -> Linkable
LM UTCTime
obj_time Module
mod [FilePath -> Unlinked
DotO FilePath
obj_fn])
  -- We used to look for _stub.o files here, but that was a bug (#706)
  -- Now GHC merges the stub.o into the main .o (#3687)