ghc-mod/Info.hs

195 lines
6.1 KiB
Haskell
Raw Normal View History

2012-02-12 12:04:18 +00:00
{-# LANGUAGE CPP, Rank2Types, TupleSections #-}
2011-08-24 06:58:12 +00:00
2012-02-13 04:23:04 +00:00
module Info (infoExpr, typeExpr) where
2010-11-12 07:27:50 +00:00
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
2012-02-12 12:04:18 +00:00
import CoreUtils
2012-02-14 02:33:27 +00:00
import Data.Function
2012-02-12 12:04:18 +00:00
import Data.Generics as G
2011-05-24 07:17:19 +00:00
import Data.List
2010-11-15 05:46:59 +00:00
import Data.Maybe
2012-02-12 16:01:58 +00:00
import Data.Ord as O
2012-02-12 12:04:18 +00:00
import Desugar
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
2012-02-12 12:04:18 +00:00
import TcRnTypes
2011-05-24 07:17:19 +00:00
import Types
2011-08-24 06:58:12 +00:00
#if __GLASGOW_HASKELL__ >= 702
import CoreMonad
#endif
type Expression = String
type ModuleString = String
2010-11-15 05:46:59 +00:00
----------------------------------------------------------------
2010-11-12 07:27:50 +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
2012-02-12 12:04:18 +00:00
----------------------------------------------------------------
2012-02-13 04:23:04 +00:00
typeExpr :: Options -> ModuleString -> Int -> Int -> FilePath -> IO String
2012-02-14 02:33:27 +00:00
typeExpr opt modstr lineNo colNo file = Info.typeOf opt file modstr lineNo colNo
2012-02-12 12:04:18 +00:00
2012-02-13 04:23:04 +00:00
typeOf :: Options -> FilePath -> ModuleString -> Int -> Int -> IO String
typeOf opt fileName modstr lineNo colNo = inModuleContext opt fileName modstr exprToType
2012-02-12 12:04:18 +00:00
where
exprToType = do
modSum <- getModSummary $ mkModuleName modstr
p <- parseModule modSum
tcm <- typecheckModule p
es <- liftIO $ findExpr tcm lineNo colNo
ts <- catMaybes <$> mapM (getType tcm) es
2012-02-14 02:33:27 +00:00
let sss = map toTup $ sortBy (cmp `on` fst) ts
return $ convert opt sss
2012-02-14 02:33:27 +00:00
toTup :: (SrcSpan, Type) -> ((Int,Int,Int,Int),String)
toTup (spn, typ) = (l spn, pretty typ)
2012-02-12 12:04:18 +00:00
2012-02-14 02:33:27 +00:00
l :: SrcSpan -> (Int,Int,Int,Int)
2012-02-13 01:44:37 +00:00
#if __GLASGOW_HASKELL__ >= 702
l (RealSrcSpan spn)
#else
l spn | isGoodSrcSpan spn
#endif
2012-02-14 02:33:27 +00:00
= (srcSpanStartLine spn, srcSpanStartCol spn
, srcSpanEndLine spn, srcSpanEndCol spn)
l _ = (0,0,0,0)
2012-02-12 16:01:58 +00:00
cmp a b
| a `isSubspanOf` b = O.LT
| b `isSubspanOf` a = O.GT
| otherwise = O.EQ
2012-02-12 12:04:18 +00:00
findExpr :: TypecheckedModule -> Int -> Int -> IO [LHsExpr Id]
findExpr tcm line col = do
let src = tm_typechecked_source tcm
ssrc <- everywhereM' sanitize src
return $ listify f ssrc
where
-- It is for GHC's panic!
sanitize :: Data a => a -> IO a
sanitize x = do
mret <- try (evaluate x)
return $ case mret of
Left (SomeException _) -> G.empty
Right ret -> ret
2012-02-14 02:33:27 +00:00
2012-02-12 12:04:18 +00:00
f :: LHsExpr Id -> Bool
f (L spn _) = spn `spans` (line, col)
-- | Monadic variation on everywhere'
everywhereM' :: Monad m => GenericM m -> GenericM m
everywhereM' f x = do
x' <- f x
gmapM (everywhereM' f) x'
getType :: GhcMonad m => TypecheckedModule -> LHsExpr Id -> m (Maybe (SrcSpan, Type))
getType tcm e = do
hs_env <- getSession
(_, mbe) <- liftIO $ deSugarExpr hs_env modu rn_env ty_env e
return $ (getLoc e, ) <$> CoreUtils.exprType <$> mbe
where
modu = ms_mod $ pm_mod_summary $ tm_parsed_module tcm
rn_env = tcg_rdr_env $ fst $ tm_internals_ tcm
ty_env = tcg_type_env $ fst $ tm_internals_ tcm
2012-02-13 04:23:04 +00:00
pretty :: Type -> String
pretty = showSDocForUser neverQualify . pprTypeForUser False
2011-01-14 02:18:33 +00:00
----------------------------------------------------------------
-- 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)]
2012-02-12 12:04:18 +00:00
pprInfo :: PrintExplicitForalls -> (TyThing, GHC.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
2012-02-14 02:33:27 +00:00
show_fixity fx
| fx == defaultFixity = Outputable.empty
| otherwise = ppr fx <+> ppr (getName thing)
2010-11-15 05:46:59 +00:00
----------------------------------------------------------------
2011-08-24 06:58:12 +00:00
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
2011-08-24 06:58:12 +00:00
(file,_) <- initializeGHC opt fileName ["-w"] False
2011-05-24 07:17:19 +00:00
setTargetFile file
2011-08-24 06:58:12 +00:00
load LoadAllTargets
2011-01-14 02:18:33 +00:00
mif setContextFromTarget action invalid
2011-01-13 08:22:43 +00:00
invalid = do
2011-08-24 06:58:12 +00:00
initializeGHC opt fileName ["-w"] False
2011-01-14 02:18:33 +00:00
setTargetBuffer
2011-08-24 06:58:12 +00:00
load 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
2011-08-24 06:58:12 +00:00
#if __GLASGOW_HASKELL__ >= 702
importsBuf = stringToStringBuffer . unlines $ header
#else
2011-01-13 08:22:43 +00:00
importsBuf <- liftIO . stringToStringBuffer . unlines $ header
2011-08-24 06:58:12 +00:00
#endif
2011-01-13 08:22:43 +00:00
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
#if __GLASGOW_HASKELL__ >= 704
top <- map (IIModule . ms_mod) <$> filterM isTop ms
setContext top
#else
2010-11-18 07:38:55 +00:00
top <- map ms_mod <$> filterM isTop ms
setContext top []
#endif
return (not . null $ 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