module GHC.Unit.Module.ModSummary
( ExtendedModSummary (..)
, extendModSummaryNoDeps
, ModSummary (..)
, ms_installed_mod
, ms_mod_name
, ms_imps
, ms_home_allimps
, ms_home_srcimps
, ms_home_imps
, msHiFilePath
, msHsFilePath
, msObjFilePath
, msDynObjFilePath
, isBootSummary
, findTarget
)
where
import GHC.Prelude
import GHC.Hs
import GHC.Driver.Session
import GHC.Unit.Types
import GHC.Unit.Module
import GHC.Types.SourceFile ( HscSource(..), hscSourceString )
import GHC.Types.SrcLoc
import GHC.Types.Target
import GHC.Data.Maybe
import GHC.Data.FastString
import GHC.Data.StringBuffer ( StringBuffer )
import GHC.Utils.Outputable
import Data.Time
data ExtendedModSummary = ExtendedModSummary
{ emsModSummary :: !ModSummary
, emsInstantiatedUnits :: [InstantiatedUnit]
}
instance Outputable ExtendedModSummary where
ppr = \case
ExtendedModSummary ms bds -> ppr ms <+> ppr bds
extendModSummaryNoDeps :: ModSummary -> ExtendedModSummary
extendModSummaryNoDeps ms = ExtendedModSummary ms []
data ModSummary
= ModSummary {
ms_mod :: Module,
ms_hsc_src :: HscSource,
ms_location :: ModLocation,
ms_hs_date :: UTCTime,
ms_obj_date :: Maybe UTCTime,
ms_iface_date :: Maybe UTCTime,
ms_hie_date :: Maybe UTCTime,
ms_srcimps :: [(Maybe FastString, Located ModuleName)],
ms_textual_imps :: [(Maybe FastString, Located ModuleName)],
ms_parsed_mod :: Maybe HsParsedModule,
ms_hspp_file :: FilePath,
ms_hspp_opts :: DynFlags,
ms_hspp_buf :: Maybe StringBuffer
}
ms_installed_mod :: ModSummary -> InstalledModule
ms_installed_mod = fst . getModuleInstantiation . ms_mod
ms_mod_name :: ModSummary -> ModuleName
ms_mod_name = moduleName . ms_mod
ms_imps :: ModSummary -> [(Maybe FastString, Located ModuleName)]
ms_imps ms =
ms_textual_imps ms ++
map mk_additional_import (dynFlagDependencies (ms_hspp_opts ms))
where
mk_additional_import mod_nm = (Nothing, noLoc mod_nm)
home_imps :: [(Maybe FastString, Located ModuleName)] -> [Located ModuleName]
home_imps imps = [ lmodname | (mb_pkg, lmodname) <- imps,
isLocal mb_pkg ]
where isLocal Nothing = True
isLocal (Just pkg) | pkg == fsLit "this" = True
isLocal _ = False
ms_home_allimps :: ModSummary -> [ModuleName]
ms_home_allimps ms = map unLoc (ms_home_srcimps ms ++ ms_home_imps ms)
ms_home_srcimps :: ModSummary -> [Located ModuleName]
ms_home_srcimps = home_imps . ms_srcimps
ms_home_imps :: ModSummary -> [Located ModuleName]
ms_home_imps = home_imps . ms_imps
msHsFilePath, msHiFilePath, msObjFilePath :: ModSummary -> FilePath
msHsFilePath ms = expectJust "msHsFilePath" (ml_hs_file (ms_location ms))
msHiFilePath ms = ml_hi_file (ms_location ms)
msObjFilePath ms = ml_obj_file (ms_location ms)
msDynObjFilePath :: ModSummary -> DynFlags -> FilePath
msDynObjFilePath ms dflags = dynamicOutputFile dflags (msObjFilePath ms)
isBootSummary :: ModSummary -> IsBootInterface
isBootSummary ms = if ms_hsc_src ms == HsBootFile then IsBoot else NotBoot
instance Outputable ModSummary where
ppr ms
= sep [text "ModSummary {",
nest 3 (sep [text "ms_hs_date = " <> text (show (ms_hs_date ms)),
text "ms_mod =" <+> ppr (ms_mod ms)
<> text (hscSourceString (ms_hsc_src ms)) <> comma,
text "ms_textual_imps =" <+> ppr (ms_textual_imps ms),
text "ms_srcimps =" <+> ppr (ms_srcimps ms)]),
char '}'
]
findTarget :: ModSummary -> [Target] -> Maybe Target
findTarget ms ts =
case filter (matches ms) ts of
[] -> Nothing
(t:_) -> Just t
where
summary `matches` Target (TargetModule m) _ _
= ms_mod_name summary == m
summary `matches` Target (TargetFile f _) _ _
| Just f' <- ml_hs_file (ms_location summary)
= f == f'
_ `matches` _
= False