2016-01-17 20:03:28 +00:00
|
|
|
{-# LANGUAGE CPP, BangPatterns, TupleSections, DeriveGeneric #-}
|
2014-04-24 08:02:50 +00:00
|
|
|
|
2017-02-28 02:01:14 +00:00
|
|
|
module GhcModExe.Find
|
2014-07-17 14:15:02 +00:00
|
|
|
#ifndef SPEC
|
2015-06-01 14:54:50 +00:00
|
|
|
( Symbol
|
2014-07-16 09:14:12 +00:00
|
|
|
, SymbolDb
|
2014-07-17 05:04:28 +00:00
|
|
|
, loadSymbolDb
|
2014-07-16 09:14:12 +00:00
|
|
|
, lookupSymbol
|
|
|
|
, dumpSymbol
|
|
|
|
, findSymbol
|
2014-07-18 06:13:30 +00:00
|
|
|
, lookupSym
|
2014-09-20 03:25:46 +00:00
|
|
|
, isOutdated
|
2016-01-04 04:27:31 +00:00
|
|
|
-- * Load 'SymbolDb' asynchronously
|
|
|
|
, AsyncSymbolDb
|
|
|
|
, newAsyncSymbolDb
|
|
|
|
, getAsyncSymbolDb
|
2014-07-17 14:15:02 +00:00
|
|
|
)
|
|
|
|
#endif
|
|
|
|
where
|
2014-04-24 08:02:50 +00:00
|
|
|
|
2016-01-08 15:58:40 +00:00
|
|
|
import qualified GHC as G
|
|
|
|
import FastString
|
|
|
|
import Module
|
|
|
|
import OccName
|
|
|
|
import HscTypes
|
2016-01-17 20:03:28 +00:00
|
|
|
import Exception
|
|
|
|
|
2014-05-11 22:40:00 +00:00
|
|
|
import Language.Haskell.GhcMod.Convert
|
2016-01-04 04:27:31 +00:00
|
|
|
import Language.Haskell.GhcMod.Gap
|
2014-07-16 09:14:12 +00:00
|
|
|
import Language.Haskell.GhcMod.Monad
|
2016-01-04 04:27:31 +00:00
|
|
|
import Language.Haskell.GhcMod.Output
|
2014-04-24 12:08:45 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
2014-09-23 04:47:32 +00:00
|
|
|
import Language.Haskell.GhcMod.Utils
|
2016-01-04 04:27:31 +00:00
|
|
|
import Language.Haskell.GhcMod.World
|
2016-01-08 15:58:40 +00:00
|
|
|
import Language.Haskell.GhcMod.LightGhc
|
2016-01-04 04:27:31 +00:00
|
|
|
|
|
|
|
import Control.Applicative
|
2016-01-17 20:03:28 +00:00
|
|
|
import Control.DeepSeq
|
2016-01-04 04:27:31 +00:00
|
|
|
import Control.Monad
|
|
|
|
import Control.Monad.Trans.Control
|
|
|
|
import Control.Concurrent
|
2016-01-17 20:03:28 +00:00
|
|
|
|
2016-01-04 04:27:31 +00:00
|
|
|
import Data.List
|
2016-01-09 14:27:21 +00:00
|
|
|
import Data.Binary
|
2016-01-04 04:27:31 +00:00
|
|
|
import Data.Function
|
2016-01-17 20:03:28 +00:00
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import qualified Data.ByteString.Lazy as LBS
|
|
|
|
import Data.IORef
|
|
|
|
|
2015-11-26 13:48:26 +00:00
|
|
|
import System.Directory.ModTime
|
2016-01-10 20:48:03 +00:00
|
|
|
import System.IO.Unsafe
|
2016-01-17 20:03:28 +00:00
|
|
|
|
2016-01-09 14:27:21 +00:00
|
|
|
import GHC.Generics (Generic)
|
2014-04-24 08:02:50 +00:00
|
|
|
|
2016-01-09 22:39:04 +00:00
|
|
|
import Data.Map (Map)
|
|
|
|
import qualified Data.Map as M
|
2016-01-08 15:58:40 +00:00
|
|
|
import Data.Set (Set)
|
|
|
|
import qualified Data.Set as S
|
2016-01-09 22:19:15 +00:00
|
|
|
import Language.Haskell.GhcMod.PathsAndFiles
|
|
|
|
import System.Directory
|
2015-08-03 01:09:56 +00:00
|
|
|
import Prelude
|
2014-04-24 08:02:50 +00:00
|
|
|
|
2014-07-16 09:14:12 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-07-17 05:04:28 +00:00
|
|
|
-- | Type of function and operation names.
|
2016-01-09 22:39:04 +00:00
|
|
|
type Symbol = BS.ByteString
|
|
|
|
type ModuleNameBS = BS.ByteString
|
2016-01-08 15:58:40 +00:00
|
|
|
|
2014-07-17 05:04:28 +00:00
|
|
|
-- | Database from 'Symbol' to \['ModuleString'\].
|
2015-06-01 12:59:38 +00:00
|
|
|
data SymbolDb = SymbolDb
|
2016-01-17 20:03:28 +00:00
|
|
|
{ sdTable :: Map Symbol (Set ModuleNameBS)
|
2016-01-09 21:23:20 +00:00
|
|
|
, sdTimestamp :: ModTime
|
2016-01-09 14:27:21 +00:00
|
|
|
} deriving (Generic)
|
|
|
|
|
2016-07-10 15:35:44 +00:00
|
|
|
#if __GLASGOW_HASKELL__ >= 708
|
2016-01-09 14:27:21 +00:00
|
|
|
instance Binary SymbolDb
|
2016-07-10 15:35:44 +00:00
|
|
|
#else
|
|
|
|
instance Binary SymbolDb where
|
|
|
|
put (SymbolDb a b) = put a >> put b
|
|
|
|
get = do
|
|
|
|
a <- get
|
|
|
|
b <- get
|
|
|
|
return (SymbolDb a b)
|
|
|
|
#endif
|
2016-01-09 14:27:21 +00:00
|
|
|
instance NFData SymbolDb
|
2014-09-20 03:25:46 +00:00
|
|
|
|
2015-08-07 04:47:34 +00:00
|
|
|
isOutdated :: IOish m => SymbolDb -> GhcModT m Bool
|
2015-06-01 12:59:38 +00:00
|
|
|
isOutdated db =
|
2016-01-09 21:23:20 +00:00
|
|
|
isOlderThan (sdTimestamp db) <$> timedPackageCaches
|
2014-07-16 09:14:12 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-07-18 06:42:05 +00:00
|
|
|
-- | Looking up 'SymbolDb' with 'Symbol' to \['ModuleString'\]
|
|
|
|
-- which will be concatenated. 'loadSymbolDb' is called internally.
|
2016-01-08 15:58:40 +00:00
|
|
|
findSymbol :: IOish m => String -> GhcModT m String
|
2016-01-09 22:19:15 +00:00
|
|
|
findSymbol sym = loadSymbolDb' >>= lookupSymbol sym
|
2014-07-16 09:14:12 +00:00
|
|
|
|
2014-07-17 05:04:28 +00:00
|
|
|
-- | Looking up 'SymbolDb' with 'Symbol' to \['ModuleString'\]
|
|
|
|
-- which will be concatenated.
|
2016-01-08 15:58:40 +00:00
|
|
|
lookupSymbol :: IOish m => String -> SymbolDb -> GhcModT m String
|
|
|
|
lookupSymbol sym db = convert' $ lookupSym (fastStringToByteString $ mkFastString sym) db
|
2014-07-18 06:13:30 +00:00
|
|
|
|
|
|
|
lookupSym :: Symbol -> SymbolDb -> [ModuleString]
|
2016-01-17 20:03:28 +00:00
|
|
|
lookupSym sym db = map (ModuleString . unpackFS . mkFastStringByteString') $ S.toList $ M.findWithDefault S.empty sym $ sdTable db
|
2014-07-16 09:14:12 +00:00
|
|
|
|
|
|
|
---------------------------------------------------------------
|
|
|
|
|
2016-01-09 22:19:15 +00:00
|
|
|
loadSymbolDb' :: IOish m => GhcModT m SymbolDb
|
|
|
|
loadSymbolDb' = do
|
|
|
|
cache <- symbolCache <$> cradle
|
|
|
|
let doLoad True = do
|
|
|
|
db <- decode <$> liftIO (LBS.readFile cache)
|
|
|
|
outdated <- isOutdated db
|
|
|
|
if outdated
|
|
|
|
then doLoad False
|
|
|
|
else return db
|
|
|
|
doLoad False = do
|
|
|
|
db <- loadSymbolDb
|
|
|
|
liftIO $ LBS.writeFile cache $ encode db
|
|
|
|
return db
|
|
|
|
doLoad =<< liftIO (doesFileExist cache)
|
|
|
|
|
2014-07-17 05:04:28 +00:00
|
|
|
-- | Loading a file and creates 'SymbolDb'.
|
2016-01-05 10:38:25 +00:00
|
|
|
loadSymbolDb :: IOish m => GhcModT m SymbolDb
|
|
|
|
loadSymbolDb = do
|
2015-06-01 14:54:50 +00:00
|
|
|
ghcMod <- liftIO ghcModExecutable
|
2016-01-09 14:27:21 +00:00
|
|
|
readProc <- gmReadProcess'
|
|
|
|
out <- liftIO $ readProc ghcMod ["--verbose", "error", "dumpsym"] ""
|
|
|
|
return $!! decode out
|
2014-07-16 09:14:12 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
-- used 'ghc-mod dumpsym'
|
|
|
|
|
2016-01-09 14:27:21 +00:00
|
|
|
-- | Dumps a 'Binary' representation of 'SymbolDb' to stdout
|
|
|
|
dumpSymbol :: IOish m => GhcModT m ()
|
2016-01-05 10:38:25 +00:00
|
|
|
dumpSymbol = do
|
2016-01-09 14:27:21 +00:00
|
|
|
ts <- liftIO getCurrentModTime
|
2016-07-16 01:42:59 +00:00
|
|
|
st <- runGmPkgGhc $ getGlobalSymbolTable
|
2016-01-17 20:03:28 +00:00
|
|
|
liftIO . LBS.putStr $ encode SymbolDb {
|
|
|
|
sdTable = st
|
2016-01-09 21:23:20 +00:00
|
|
|
, sdTimestamp = ts
|
2016-01-09 14:27:21 +00:00
|
|
|
}
|
2014-07-22 20:54:18 +00:00
|
|
|
|
2015-06-01 12:59:38 +00:00
|
|
|
-- | Check whether given file is older than any file from the given set.
|
|
|
|
-- Returns True if given file does not exist.
|
2016-01-05 10:38:25 +00:00
|
|
|
isOlderThan :: ModTime -> [TimedFile] -> Bool
|
|
|
|
isOlderThan tCache files =
|
|
|
|
any (tCache <=) $ map tfTime files -- including equal just in case
|
2014-05-14 16:54:56 +00:00
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
-- | Browsing all functions in all system modules.
|
2016-07-16 01:42:59 +00:00
|
|
|
getGlobalSymbolTable :: (G.GhcMonad m, MonadIO m)
|
|
|
|
=> m (Map Symbol (Set ModuleNameBS))
|
|
|
|
getGlobalSymbolTable =
|
|
|
|
foldM extend M.empty =<< (listVisibleModules <$> G.getSessionDynFlags)
|
2016-01-08 15:58:40 +00:00
|
|
|
|
2016-07-16 01:42:59 +00:00
|
|
|
extend :: (G.GhcMonad m, MonadIO m)
|
|
|
|
=> Map Symbol (Set ModuleNameBS)
|
2016-01-08 15:58:40 +00:00
|
|
|
-> Module
|
2016-07-16 01:42:59 +00:00
|
|
|
-> m (Map Symbol (Set ModuleNameBS))
|
|
|
|
extend mm mdl = do
|
|
|
|
hsc_env <- G.getSession
|
|
|
|
eps <- liftIO $ readIORef $ hsc_EPS hsc_env
|
|
|
|
modinfo <- liftIO $ unsafeInterleaveIO $ runLightGhc hsc_env $ do
|
2016-01-08 15:58:40 +00:00
|
|
|
G.getModuleInfo mdl <* liftIO (writeIORef (hsc_EPS hsc_env) eps)
|
|
|
|
return $ M.unionWith S.union mm $ extractBindings modinfo mdl
|
|
|
|
|
|
|
|
extractBindings :: Maybe G.ModuleInfo
|
|
|
|
-> G.Module
|
|
|
|
-> Map Symbol (Set ModuleNameBS)
|
|
|
|
extractBindings Nothing _ = M.empty
|
|
|
|
extractBindings (Just inf) mdl = M.fromList $ do
|
|
|
|
name <- G.modInfoExports inf
|
|
|
|
let sym = fastStringToByteString $ occNameFS $ G.getOccName name
|
|
|
|
mdls = S.singleton $ fastStringToByteString $ moduleNameFS $ moduleName mdl
|
|
|
|
return (sym, mdls)
|
2016-01-04 04:27:31 +00:00
|
|
|
|
2016-01-09 22:39:04 +00:00
|
|
|
mkFastStringByteString' :: BS.ByteString -> FastString
|
|
|
|
#if !MIN_VERSION_ghc(7,8,0)
|
|
|
|
fastStringToByteString :: FastString -> BS.ByteString
|
|
|
|
fastStringToByteString = BS.pack . bytesFS
|
|
|
|
|
|
|
|
mkFastStringByteString' = mkFastStringByteList . BS.unpack
|
|
|
|
#elif __GLASGOW_HASKELL__ == 708
|
|
|
|
mkFastStringByteString' = unsafePerformIO . mkFastStringByteString
|
|
|
|
#else
|
|
|
|
mkFastStringByteString' = mkFastStringByteString
|
|
|
|
#endif
|
|
|
|
|
2016-01-04 04:27:31 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2016-01-05 10:38:25 +00:00
|
|
|
data AsyncSymbolDb = AsyncSymbolDb (MVar (Either SomeException SymbolDb))
|
2016-01-04 04:27:31 +00:00
|
|
|
|
|
|
|
asyncLoadSymbolDb :: IOish m
|
2016-01-05 10:38:25 +00:00
|
|
|
=> MVar (Either SomeException SymbolDb)
|
2016-01-04 04:27:31 +00:00
|
|
|
-> GhcModT m ()
|
2016-01-05 10:38:25 +00:00
|
|
|
asyncLoadSymbolDb mv = void $
|
2016-01-04 04:27:31 +00:00
|
|
|
liftBaseWith $ \run -> forkIO $ void $ run $ do
|
2016-01-05 10:38:25 +00:00
|
|
|
edb <- gtry loadSymbolDb
|
2016-01-04 04:27:31 +00:00
|
|
|
liftIO $ putMVar mv edb
|
|
|
|
|
2016-01-05 10:38:25 +00:00
|
|
|
newAsyncSymbolDb :: IOish m => GhcModT m AsyncSymbolDb
|
|
|
|
newAsyncSymbolDb = do
|
2016-01-04 04:27:31 +00:00
|
|
|
mv <- liftIO newEmptyMVar
|
2016-01-05 10:38:25 +00:00
|
|
|
asyncLoadSymbolDb mv
|
|
|
|
return $ AsyncSymbolDb mv
|
2016-01-04 04:27:31 +00:00
|
|
|
|
|
|
|
getAsyncSymbolDb :: forall m. IOish m => AsyncSymbolDb -> GhcModT m SymbolDb
|
2016-01-05 10:38:25 +00:00
|
|
|
getAsyncSymbolDb (AsyncSymbolDb mv) = do
|
2016-01-04 05:02:30 +00:00
|
|
|
db <- liftIO $ handleEx <$> takeMVar mv
|
|
|
|
outdated <- isOutdated db
|
|
|
|
if outdated
|
|
|
|
then do
|
2016-01-05 10:38:25 +00:00
|
|
|
asyncLoadSymbolDb mv
|
2016-01-04 05:02:30 +00:00
|
|
|
liftIO $ handleEx <$> readMVar mv
|
|
|
|
else do
|
|
|
|
liftIO $ putMVar mv $ Right db
|
|
|
|
return db
|
2016-01-04 04:27:31 +00:00
|
|
|
where
|
2016-01-04 05:02:30 +00:00
|
|
|
handleEx edb =
|
2016-01-04 04:27:31 +00:00
|
|
|
case edb of
|
|
|
|
Left ex -> throw ex
|
2016-01-04 05:02:30 +00:00
|
|
|
Right db -> db
|