module GHC.Unit.Home.ModInfo
( HomeModInfo (..)
, HomeModLinkable(..)
, homeModInfoObject
, homeModInfoByteCode
, emptyHomeModInfoLinkable
, justBytecode
, justObjects
, bytecodeAndObjects
, HomePackageTable
, emptyHomePackageTable
, lookupHpt
, eltsHpt
, concatHpt
, filterHpt
, allHpt
, anyHpt
, mapHpt
, delFromHpt
, addToHpt
, addHomeModInfoToHpt
, addListToHpt
, lookupHptDirectly
, lookupHptByModule
, listToHpt
, listHMIToHpt
, pprHPT
)
where
import GHC.Prelude
import GHC.Unit.Module.ModIface
import GHC.Unit.Module.ModDetails
import GHC.Unit.Module
import GHC.Linker.Types ( Linkable(..), linkableIsNativeCodeOnly )
import GHC.Types.Unique
import GHC.Types.Unique.DFM
import GHC.Utils.Outputable
import Data.List (sortOn)
import Data.Ord
import GHC.Utils.Panic
data HomeModInfo = HomeModInfo
{ HomeModInfo -> ModIface
hm_iface :: !ModIface
, HomeModInfo -> ModDetails
hm_details :: ModDetails
, HomeModInfo -> HomeModLinkable
hm_linkable :: !HomeModLinkable
}
homeModInfoByteCode :: HomeModInfo -> Maybe Linkable
homeModInfoByteCode :: HomeModInfo -> Maybe Linkable
homeModInfoByteCode = HomeModLinkable -> Maybe Linkable
homeMod_bytecode (HomeModLinkable -> Maybe Linkable)
-> (HomeModInfo -> HomeModLinkable)
-> HomeModInfo
-> Maybe Linkable
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HomeModInfo -> HomeModLinkable
hm_linkable
homeModInfoObject :: HomeModInfo -> Maybe Linkable
homeModInfoObject :: HomeModInfo -> Maybe Linkable
homeModInfoObject = HomeModLinkable -> Maybe Linkable
homeMod_object (HomeModLinkable -> Maybe Linkable)
-> (HomeModInfo -> HomeModLinkable)
-> HomeModInfo
-> Maybe Linkable
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HomeModInfo -> HomeModLinkable
hm_linkable
emptyHomeModInfoLinkable :: HomeModLinkable
emptyHomeModInfoLinkable :: HomeModLinkable
emptyHomeModInfoLinkable = Maybe Linkable -> Maybe Linkable -> HomeModLinkable
HomeModLinkable Maybe Linkable
forall a. Maybe a
Nothing Maybe Linkable
forall a. Maybe a
Nothing
data HomeModLinkable = HomeModLinkable { HomeModLinkable -> Maybe Linkable
homeMod_bytecode :: !(Maybe Linkable)
, HomeModLinkable -> Maybe Linkable
homeMod_object :: !(Maybe Linkable) }
instance Outputable HomeModLinkable where
ppr :: HomeModLinkable -> SDoc
ppr (HomeModLinkable Maybe Linkable
l1 Maybe Linkable
l2) = Maybe Linkable -> SDoc
forall a. Outputable a => a -> SDoc
ppr Maybe Linkable
l1 SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ Maybe Linkable -> SDoc
forall a. Outputable a => a -> SDoc
ppr Maybe Linkable
l2
justBytecode :: Linkable -> HomeModLinkable
justBytecode :: Linkable -> HomeModLinkable
justBytecode Linkable
lm =
Bool -> SDoc -> HomeModLinkable -> HomeModLinkable
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr (Bool -> Bool
not (Linkable -> Bool
linkableIsNativeCodeOnly Linkable
lm)) (Linkable -> SDoc
forall a. Outputable a => a -> SDoc
ppr Linkable
lm)
(HomeModLinkable -> HomeModLinkable)
-> HomeModLinkable -> HomeModLinkable
forall a b. (a -> b) -> a -> b
$ HomeModLinkable
emptyHomeModInfoLinkable { homeMod_bytecode = Just lm }
justObjects :: Linkable -> HomeModLinkable
justObjects :: Linkable -> HomeModLinkable
justObjects Linkable
lm =
Bool -> SDoc -> HomeModLinkable -> HomeModLinkable
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr (Linkable -> Bool
linkableIsNativeCodeOnly Linkable
lm) (Linkable -> SDoc
forall a. Outputable a => a -> SDoc
ppr Linkable
lm)
(HomeModLinkable -> HomeModLinkable)
-> HomeModLinkable -> HomeModLinkable
forall a b. (a -> b) -> a -> b
$ HomeModLinkable
emptyHomeModInfoLinkable { homeMod_object = Just lm }
bytecodeAndObjects :: Linkable -> Linkable -> HomeModLinkable
bytecodeAndObjects :: Linkable -> Linkable -> HomeModLinkable
bytecodeAndObjects Linkable
bc Linkable
o =
Bool -> SDoc -> HomeModLinkable -> HomeModLinkable
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr (Bool -> Bool
not (Linkable -> Bool
linkableIsNativeCodeOnly Linkable
bc) Bool -> Bool -> Bool
&& Linkable -> Bool
linkableIsNativeCodeOnly Linkable
o) (Linkable -> SDoc
forall a. Outputable a => a -> SDoc
ppr Linkable
bc SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ Linkable -> SDoc
forall a. Outputable a => a -> SDoc
ppr Linkable
o)
(Maybe Linkable -> Maybe Linkable -> HomeModLinkable
HomeModLinkable (Linkable -> Maybe Linkable
forall a. a -> Maybe a
Just Linkable
bc) (Linkable -> Maybe Linkable
forall a. a -> Maybe a
Just Linkable
o))
type HomePackageTable = DModuleNameEnv HomeModInfo
emptyHomePackageTable :: HomePackageTable
emptyHomePackageTable :: HomePackageTable
emptyHomePackageTable = HomePackageTable
forall {k} (key :: k) elt. UniqDFM key elt
emptyUDFM
lookupHpt :: HomePackageTable -> ModuleName -> Maybe HomeModInfo
lookupHpt :: HomePackageTable -> ModuleName -> Maybe HomeModInfo
lookupHpt = HomePackageTable -> ModuleName -> Maybe HomeModInfo
forall key elt.
Uniquable key =>
UniqDFM key elt -> key -> Maybe elt
lookupUDFM
lookupHptDirectly :: HomePackageTable -> Unique -> Maybe HomeModInfo
lookupHptDirectly :: HomePackageTable -> Unique -> Maybe HomeModInfo
lookupHptDirectly = HomePackageTable -> Unique -> Maybe HomeModInfo
forall {k} (key :: k) elt. UniqDFM key elt -> Unique -> Maybe elt
lookupUDFM_Directly
eltsHpt :: HomePackageTable -> [HomeModInfo]
eltsHpt :: HomePackageTable -> [HomeModInfo]
eltsHpt = HomePackageTable -> [HomeModInfo]
forall {k} (key :: k) elt. UniqDFM key elt -> [elt]
eltsUDFM
concatHpt :: (HomeModInfo -> [a]) -> HomePackageTable -> [a]
concatHpt :: forall a. (HomeModInfo -> [a]) -> HomePackageTable -> [a]
concatHpt HomeModInfo -> [a]
f = [[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[a]] -> [a])
-> (HomePackageTable -> [[a]]) -> HomePackageTable -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UniqDFM ModuleName [a] -> [[a]]
forall {k} (key :: k) elt. UniqDFM key elt -> [elt]
eltsUDFM (UniqDFM ModuleName [a] -> [[a]])
-> (HomePackageTable -> UniqDFM ModuleName [a])
-> HomePackageTable
-> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HomeModInfo -> Maybe [a])
-> HomePackageTable -> UniqDFM ModuleName [a]
forall {k} elt1 elt2 (key :: k).
(elt1 -> Maybe elt2) -> UniqDFM key elt1 -> UniqDFM key elt2
mapMaybeUDFM HomeModInfo -> Maybe [a]
g
where
g :: HomeModInfo -> Maybe [a]
g HomeModInfo
hmi = case HomeModInfo -> [a]
f HomeModInfo
hmi of { [] -> Maybe [a]
forall a. Maybe a
Nothing; [a]
as -> [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a]
as }
filterHpt :: (HomeModInfo -> Bool) -> HomePackageTable -> HomePackageTable
filterHpt :: (HomeModInfo -> Bool) -> HomePackageTable -> HomePackageTable
filterHpt = (HomeModInfo -> Bool) -> HomePackageTable -> HomePackageTable
forall {k} elt (key :: k).
(elt -> Bool) -> UniqDFM key elt -> UniqDFM key elt
filterUDFM
allHpt :: (HomeModInfo -> Bool) -> HomePackageTable -> Bool
allHpt :: (HomeModInfo -> Bool) -> HomePackageTable -> Bool
allHpt = (HomeModInfo -> Bool) -> HomePackageTable -> Bool
forall {k} elt (key :: k). (elt -> Bool) -> UniqDFM key elt -> Bool
allUDFM
anyHpt :: (HomeModInfo -> Bool) -> HomePackageTable -> Bool
anyHpt :: (HomeModInfo -> Bool) -> HomePackageTable -> Bool
anyHpt = (HomeModInfo -> Bool) -> HomePackageTable -> Bool
forall {k} elt (key :: k). (elt -> Bool) -> UniqDFM key elt -> Bool
anyUDFM
mapHpt :: (HomeModInfo -> HomeModInfo) -> HomePackageTable -> HomePackageTable
mapHpt :: (HomeModInfo -> HomeModInfo)
-> HomePackageTable -> HomePackageTable
mapHpt = (HomeModInfo -> HomeModInfo)
-> HomePackageTable -> HomePackageTable
forall {k} elt1 elt2 (key :: k).
(elt1 -> elt2) -> UniqDFM key elt1 -> UniqDFM key elt2
mapUDFM
delFromHpt :: HomePackageTable -> ModuleName -> HomePackageTable
delFromHpt :: HomePackageTable -> ModuleName -> HomePackageTable
delFromHpt = HomePackageTable -> ModuleName -> HomePackageTable
forall key elt.
Uniquable key =>
UniqDFM key elt -> key -> UniqDFM key elt
delFromUDFM
addToHpt :: HomePackageTable -> ModuleName -> HomeModInfo -> HomePackageTable
addToHpt :: HomePackageTable -> ModuleName -> HomeModInfo -> HomePackageTable
addToHpt = HomePackageTable -> ModuleName -> HomeModInfo -> HomePackageTable
forall key elt.
Uniquable key =>
UniqDFM key elt -> key -> elt -> UniqDFM key elt
addToUDFM
addHomeModInfoToHpt :: HomeModInfo -> HomePackageTable -> HomePackageTable
addHomeModInfoToHpt :: HomeModInfo -> HomePackageTable -> HomePackageTable
addHomeModInfoToHpt HomeModInfo
hmi HomePackageTable
hpt = HomePackageTable -> ModuleName -> HomeModInfo -> HomePackageTable
addToHpt HomePackageTable
hpt (Module -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName (ModIface -> Module
forall (phase :: ModIfacePhase). ModIface_ phase -> Module
mi_module (HomeModInfo -> ModIface
hm_iface HomeModInfo
hmi))) HomeModInfo
hmi
addListToHpt
:: HomePackageTable -> [(ModuleName, HomeModInfo)] -> HomePackageTable
addListToHpt :: HomePackageTable -> [(ModuleName, HomeModInfo)] -> HomePackageTable
addListToHpt = HomePackageTable -> [(ModuleName, HomeModInfo)] -> HomePackageTable
forall key elt.
Uniquable key =>
UniqDFM key elt -> [(key, elt)] -> UniqDFM key elt
addListToUDFM
listToHpt :: [(ModuleName, HomeModInfo)] -> HomePackageTable
listToHpt :: [(ModuleName, HomeModInfo)] -> HomePackageTable
listToHpt = [(ModuleName, HomeModInfo)] -> HomePackageTable
forall key elt. Uniquable key => [(key, elt)] -> UniqDFM key elt
listToUDFM
listHMIToHpt :: [HomeModInfo] -> HomePackageTable
listHMIToHpt :: [HomeModInfo] -> HomePackageTable
listHMIToHpt [HomeModInfo]
hmis =
[(ModuleName, HomeModInfo)] -> HomePackageTable
listToHpt [(Module -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName (ModIface -> Module
forall (phase :: ModIfacePhase). ModIface_ phase -> Module
mi_module (HomeModInfo -> ModIface
hm_iface HomeModInfo
hmi)), HomeModInfo
hmi) | HomeModInfo
hmi <- [HomeModInfo]
sorted_hmis]
where
sorted_hmis :: [HomeModInfo]
sorted_hmis = (HomeModInfo -> Down IsBootInterface)
-> [HomeModInfo] -> [HomeModInfo]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (IsBootInterface -> Down IsBootInterface
forall a. a -> Down a
Down (IsBootInterface -> Down IsBootInterface)
-> (HomeModInfo -> IsBootInterface)
-> HomeModInfo
-> Down IsBootInterface
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModIface -> IsBootInterface
mi_boot (ModIface -> IsBootInterface)
-> (HomeModInfo -> ModIface) -> HomeModInfo -> IsBootInterface
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HomeModInfo -> ModIface
hm_iface) [HomeModInfo]
hmis
lookupHptByModule :: HomePackageTable -> Module -> Maybe HomeModInfo
lookupHptByModule :: HomePackageTable -> Module -> Maybe HomeModInfo
lookupHptByModule HomePackageTable
hpt Module
mod
= case HomePackageTable -> ModuleName -> Maybe HomeModInfo
lookupHpt HomePackageTable
hpt (Module -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName Module
mod) of
Just HomeModInfo
hm | ModIface -> Module
forall (phase :: ModIfacePhase). ModIface_ phase -> Module
mi_module (HomeModInfo -> ModIface
hm_iface HomeModInfo
hm) Module -> Module -> Bool
forall a. Eq a => a -> a -> Bool
== Module
mod -> HomeModInfo -> Maybe HomeModInfo
forall a. a -> Maybe a
Just HomeModInfo
hm
Maybe HomeModInfo
_otherwise -> Maybe HomeModInfo
forall a. Maybe a
Nothing
pprHPT :: HomePackageTable -> SDoc
pprHPT :: HomePackageTable -> SDoc
pprHPT HomePackageTable
hpt = HomePackageTable -> ([HomeModInfo] -> SDoc) -> SDoc
forall {k} (key :: k) a. UniqDFM key a -> ([a] -> SDoc) -> SDoc
pprUDFM HomePackageTable
hpt (([HomeModInfo] -> SDoc) -> SDoc)
-> ([HomeModInfo] -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \[HomeModInfo]
hms ->
[SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat [ Module -> SDoc
forall a. Outputable a => a -> SDoc
ppr (ModIface -> Module
forall (phase :: ModIfacePhase). ModIface_ phase -> Module
mi_module (HomeModInfo -> ModIface
hm_iface HomeModInfo
hm))
| HomeModInfo
hm <- [HomeModInfo]
hms ]