ghc-mod/Info.hs

114 lines
3.9 KiB
Haskell
Raw Normal View History

2010-11-12 07:27:50 +00:00
module Info where
2011-05-24 07:17:19 +00:00
import Cabal
2010-11-15 05:46:59 +00:00
import Control.Applicative hiding (empty)
2011-05-24 07:17:19 +00:00
import Control.Exception
2010-11-18 07:38:55 +00:00
import Control.Monad
2011-05-24 07:17:19 +00:00
import Data.List
2010-11-15 05:46:59 +00:00
import Data.Maybe
2010-11-12 07:27:50 +00:00
import GHC
2011-05-24 07:17:19 +00:00
import HscTypes
import NameSet
2010-11-12 07:27:50 +00:00
import Outputable
import PprTyThing
import StringBuffer
import System.Time
2011-05-24 07:17:19 +00:00
import Types
type Expression = String
type ModuleString = String
2010-11-15 05:46:59 +00:00
----------------------------------------------------------------
2010-11-12 07:27:50 +00:00
typeExpr :: Options -> ModuleString -> Expression -> FilePath -> IO String
typeExpr opt modstr expr file = (++ "\n") <$> typeOf opt file modstr expr
2010-11-12 07:27:50 +00:00
typeOf :: Options -> FilePath -> ModuleString -> Expression -> IO String
typeOf opt fileName modstr expr = inModuleContext opt fileName modstr exprToType
2011-01-13 08:22:43 +00:00
where
2011-01-14 02:18:33 +00:00
exprToType = pretty <$> exprType expr
2011-01-13 08:22:43 +00:00
pretty = showSDocForUser neverQualify . pprTypeForUser False
2010-11-15 05:46:59 +00:00
----------------------------------------------------------------
infoExpr :: Options -> ModuleString -> Expression -> FilePath -> IO String
infoExpr opt modstr expr file = (++ "\n") <$> info opt file modstr expr
2010-11-15 05:46:59 +00:00
info :: Options -> FilePath -> ModuleString -> FilePath -> IO String
info opt fileName modstr expr = inModuleContext opt fileName modstr exprToInfo
2011-01-13 08:22:43 +00:00
where
2011-01-14 02:18:33 +00:00
exprToInfo = infoThing expr
----------------------------------------------------------------
-- from ghc/InteractiveUI.hs
infoThing :: String -> Ghc String
infoThing str = do
names <- parseName str
mb_stuffs <- mapM getInfo names
let filtered = filterOutChildren (\(t,_f,_i) -> t) (catMaybes mb_stuffs)
unqual <- getPrintUnqual
return $ showSDocForUser unqual $ vcat (intersperse (text "") $ map (pprInfo False) filtered)
2010-11-15 05:46:59 +00:00
filterOutChildren :: (a -> TyThing) -> [a] -> [a]
filterOutChildren get_thing xs
= [x | x <- xs, not (getName (get_thing x) `elemNameSet` implicits)]
where
implicits = mkNameSet [getName t | x <- xs, t <- implicitTyThings (get_thing x)]
2011-01-14 04:55:58 +00:00
pprInfo :: PrintExplicitForalls -> (TyThing, Fixity, [Instance]) -> SDoc
2010-11-15 05:46:59 +00:00
pprInfo pefas (thing, fixity, insts)
2011-01-13 08:22:43 +00:00
= pprTyThingInContextLoc pefas thing
$$ show_fixity fixity
$$ vcat (map pprInstance insts)
2010-11-15 05:46:59 +00:00
where
show_fixity fix
| fix == defaultFixity = empty
| otherwise = ppr fix <+> ppr (getName thing)
----------------------------------------------------------------
inModuleContext
:: Options -> FilePath -> ModuleString -> Ghc String -> IO String
inModuleContext opt fileName modstr action = withGHC valid
2011-01-13 08:22:43 +00:00
where
valid = do
file <- initializeGHC opt fileName ["-w"]
2011-05-24 07:17:19 +00:00
setTargetFile file
2011-01-13 08:22:43 +00:00
loadWithLogger (\_ -> return ()) LoadAllTargets
2011-01-14 02:18:33 +00:00
mif setContextFromTarget action invalid
2011-01-13 08:22:43 +00:00
invalid = do
initializeGHC opt fileName ["-w"]
2011-01-14 02:18:33 +00:00
setTargetBuffer
2011-01-13 08:22:43 +00:00
loadWithLogger defaultWarnErrLogger LoadAllTargets
2011-01-14 02:18:33 +00:00
mif setContextFromTarget action (return errorMessage)
setTargetBuffer = do
2011-01-13 08:22:43 +00:00
modgraph <- depanal [mkModuleName modstr] True
let imports = concatMap (map (showSDoc . ppr . unLoc)) $
map ms_imps modgraph ++ map ms_srcimps modgraph
moddef = "module " ++ sanitize modstr ++ " where"
header = moddef : imports
importsBuf <- liftIO . stringToStringBuffer . unlines $ header
clkTime <- liftIO getClockTime
setTargets [Target (TargetModule $ mkModuleName modstr) True (Just (importsBuf, clkTime))]
2011-01-14 02:18:33 +00:00
mif m t e = m >>= \ok -> if ok then t else e
2011-01-13 08:22:43 +00:00
sanitize = fromMaybe "SomeModule" . listToMaybe . words
errorMessage = "Couldn't determine type"
2010-11-18 07:38:55 +00:00
setContextFromTarget :: Ghc Bool
2010-11-15 05:46:59 +00:00
setContextFromTarget = do
ms <- depanal [] False
2010-11-18 07:38:55 +00:00
top <- map ms_mod <$> filterM isTop ms
setContext top []
2011-01-13 08:22:43 +00:00
return (top /= [])
2010-11-18 07:38:55 +00:00
where
isTop ms = lookupMod `gcatch` returnFalse
where
lookupMod = lookupModule (ms_mod_name ms) Nothing >> return True
returnFalse = constE $ return False
----------------------------------------------------------------
constE :: a -> (SomeException -> a)
2011-01-13 08:22:43 +00:00
constE func = \_ -> func