ghc-mod/Info.hs

175 lines
6.1 KiB
Haskell
Raw Normal View History

2012-02-18 11:02:37 +00:00
{-# LANGUAGE TupleSections, FlexibleInstances #-}
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
2012-02-14 07:09:53 +00:00
import Control.Applicative
2012-02-12 12:04:18 +00:00
import CoreUtils
2012-02-14 02:33:27 +00:00
import Data.Function
2012-02-13 18:20:33 +00:00
import Data.Generics
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
2012-02-14 07:19:48 +00:00
import GHC.SYB.Utils
2012-02-14 07:09:53 +00:00
import GHCApi
2012-02-16 05:44:20 +00:00
import GHCChoice
2012-02-14 07:09:53 +00:00
import qualified Gap
2011-05-24 07:17:19 +00:00
import HscTypes
import NameSet
2010-11-12 07:27:50 +00:00
import Outputable
import PprTyThing
2012-02-15 12:22:04 +00:00
import Pretty (showDocWith, Mode(OneLineMode))
import System.Time
2012-02-12 12:04:18 +00:00
import TcRnTypes
2012-02-18 10:14:29 +00:00
import TcHsSyn (hsPatType)
2011-05-24 07:17:19 +00:00
import Types
2012-02-14 07:09:53 +00:00
----------------------------------------------------------------
2011-08-24 06:58:12 +00:00
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
2012-02-15 05:52:48 +00:00
info opt fileName modstr expr =
inModuleContext opt fileName modstr exprToInfo "Cannot show info"
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-18 11:02:37 +00:00
class HasType a where
getType :: GhcMonad m => TypecheckedModule -> a -> m (Maybe (SrcSpan, Type))
instance HasType (LHsExpr Id) where
getType tcm e = do
hs_env <- getSession
(_, mbe) <- Gap.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
instance HasType (LHsBind Id) where
getType _ (L _ FunBind{fun_id = L spn _, fun_matches = MatchGroup _ typ}) =
return $ Just (spn, typ)
getType _ _ = return Nothing
instance HasType (LPat Id) where
getType _ (L spn pat) = return $ Just (spn, hsPatType pat)
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
2012-02-15 05:52:48 +00:00
typeOf opt fileName modstr lineNo colNo =
inModuleContext opt fileName modstr exprToType errmsg
2012-02-12 12:04:18 +00:00
where
exprToType = do
modSum <- getModSummary $ mkModuleName modstr
p <- parseModule modSum
2012-02-18 11:02:37 +00:00
tcm@TypecheckedModule{tm_typechecked_source = tcs} <- typecheckModule p
let bs = listifyBinds tcs (lineNo, colNo)
es = listifySpans tcs (lineNo, colNo) :: [LHsExpr Id]
ps = listifySpans tcs (lineNo, colNo) :: [LPat Id]
bts <- mapM (getType tcm) bs
ets <- mapM (getType tcm) es
pts <- mapM (getType tcm) ps
let sss = map toTup $ sortBy (cmp `on` fst) $ catMaybes $ concat [ets, bts, pts]
2012-02-14 02:33:27 +00:00
return $ convert opt sss
2012-02-14 02:33:27 +00:00
toTup :: (SrcSpan, Type) -> ((Int,Int,Int,Int),String)
2012-02-14 07:09:53 +00:00
toTup (spn, typ) = (fourInts spn, pretty typ)
fourInts :: SrcSpan -> (Int,Int,Int,Int)
fourInts = fromMaybe (0,0,0,0) . Gap.getSrcSpan
2012-02-14 02:33:27 +00:00
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
2012-02-15 05:52:48 +00:00
errmsg = convert opt ([] :: [((Int,Int,Int,Int),String)])
2012-02-18 11:02:37 +00:00
listifyBinds tcs lc = listifyStaged TypeChecker f tcs
where
f :: LHsBind Id -> Bool
f (L _ (FunBind{fun_id = L spn _})) = isGoodSrcSpan spn && spn `spans` lc
f _ = False
2012-02-12 12:04:18 +00:00
2012-02-18 11:02:37 +00:00
listifySpans :: Typeable a => TypecheckedSource -> (Int, Int) -> [Located a]
listifySpans tcs lc = listifyStaged TypeChecker p tcs
2012-02-18 09:17:47 +00:00
where
2012-02-18 11:02:37 +00:00
p (L spn _) = isGoodSrcSpan spn && spn `spans` lc
2012-02-18 10:14:29 +00:00
2012-02-13 18:20:33 +00:00
listifyStaged :: Typeable r => Stage -> (r -> Bool) -> GenericQ [r]
2012-02-14 07:19:48 +00:00
listifyStaged s p = everythingStaged s (++) [] ([] `mkQ` (\x -> [x | p x]))
2012-02-12 12:04:18 +00:00
2012-02-13 04:23:04 +00:00
pretty :: Type -> String
2012-02-15 12:22:04 +00:00
pretty = showDocWith OneLineMode . withPprStyleDoc (mkUserStyle neverQualify AllTheWay) . pprTypeForUser False
2012-02-13 04:23:04 +00:00
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
----------------------------------------------------------------
2012-02-15 05:52:48 +00:00
inModuleContext :: Options -> FilePath -> ModuleString -> Ghc String -> String -> IO String
inModuleContext opt fileName modstr action errmsg =
2012-02-16 05:44:20 +00:00
withGHC (valid ||> invalid ||> return errmsg)
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
2012-02-15 05:52:48 +00:00
doif setContextFromTarget action
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
2012-02-15 05:52:48 +00:00
doif setContextFromTarget action
2011-01-14 02:18:33 +00:00
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
2012-02-14 07:09:53 +00:00
importsBuf <- Gap.toStringBuffer header
clkTime <- Gap.liftIO getClockTime
2011-01-13 08:22:43 +00:00
setTargets [Target (TargetModule $ mkModuleName modstr) True (Just (importsBuf, clkTime))]
2012-02-15 05:52:48 +00:00
doif m t = m >>= \ok -> if ok then t else goNext
2011-01-13 08:22:43 +00:00
sanitize = fromMaybe "SomeModule" . listToMaybe . words
2010-11-18 07:38:55 +00:00
setContextFromTarget :: Ghc Bool
2012-02-14 07:09:53 +00:00
setContextFromTarget = depanal [] False >>= Gap.setCtx