ghc-mod/Language/Haskell/GhcMod/Info.hs

178 lines
6.6 KiB
Haskell
Raw Normal View History

{-# LANGUAGE TupleSections, FlexibleInstances, TypeSynonymInstances #-}
{-# LANGUAGE Rank2Types #-}
2011-08-24 06:58:12 +00:00
2013-05-18 21:16:37 +00:00
module Language.Haskell.GhcMod.Info (infoExpr, info, typeExpr, typeOf) where
2010-11-12 07:27:50 +00:00
2012-02-14 07:09:53 +00:00
import Control.Applicative
import Control.Monad (void, when)
2012-02-12 12:04:18 +00:00
import CoreUtils
2012-02-14 02:33:27 +00:00
import Data.Function
2013-05-18 21:16:37 +00:00
import Data.Generics hiding (typeOf)
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
import Data.Time.Clock
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
2011-05-24 07:17:19 +00:00
import HscTypes
2013-05-17 01:00:01 +00:00
import Language.Haskell.GhcMod.Doc
import Language.Haskell.GhcMod.GHCApi
import Language.Haskell.GhcMod.GHCChoice
import qualified Language.Haskell.GhcMod.Gap as Gap
import Language.Haskell.GhcMod.Types
2011-05-24 07:17:19 +00:00
import NameSet
2010-11-12 07:27:50 +00:00
import Outputable
import PprTyThing
2012-02-18 10:14:29 +00:00
import TcHsSyn (hsPatType)
import TcRnTypes
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
data Cmd = Info | Type deriving Eq
2010-11-15 05:46:59 +00:00
----------------------------------------------------------------
2010-11-12 07:27:50 +00:00
2013-05-20 02:29:44 +00:00
infoExpr :: Options -> Cradle -> ModuleString -> Expression -> FilePath -> IO String
infoExpr opt cradle modstr expr file = (++ "\n") <$> withGHCDummyFile (info opt cradle file modstr expr)
2010-11-15 05:46:59 +00:00
2013-05-18 21:16:37 +00:00
info :: Options -> Cradle -> FilePath -> ModuleString -> Expression -> Ghc String
2013-03-02 07:14:55 +00:00
info opt cradle fileName modstr expr =
inModuleContext Info opt cradle 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
2012-02-19 17:46:04 +00:00
getType _ (L spn FunBind{fun_matches = MatchGroup _ typ}) = return $ Just (spn, typ)
2012-02-18 11:02:37 +00:00
getType _ _ = return Nothing
instance HasType (LPat Id) where
getType _ (L spn pat) = return $ Just (spn, hsPatType pat)
2013-05-20 02:29:44 +00:00
typeExpr :: Options -> Cradle -> ModuleString -> Int -> Int -> FilePath -> IO String
typeExpr opt cradle modstr lineNo colNo file = withGHCDummyFile $ typeOf opt cradle file modstr lineNo colNo
2012-02-12 12:04:18 +00:00
2013-05-18 21:16:37 +00:00
typeOf :: Options -> Cradle -> FilePath -> ModuleString -> Int -> Int -> Ghc String
2013-03-02 07:14:55 +00:00
typeOf opt cradle fileName modstr lineNo colNo =
inModuleContext Type opt cradle 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
2012-02-19 17:46:04 +00:00
let bs = listifySpans tcs (lineNo, colNo) :: [LHsBind Id]
2012-02-18 11:02:37 +00:00
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
2013-03-12 13:15:23 +00:00
dflag <- getSessionDynFlags
let sss = map (toTup dflag) $ sortBy (cmp `on` fst) $ catMaybes $ concat [ets, bts, pts]
2012-02-14 02:33:27 +00:00
return $ convert opt sss
2013-03-12 13:15:23 +00:00
toTup :: DynFlags -> (SrcSpan, Type) -> ((Int,Int,Int,Int),String)
toTup dflag (spn, typ) = (fourInts spn, pretty dflag typ)
2012-02-14 07:09:53 +00:00
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
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
2013-03-12 13:15:23 +00:00
pretty :: DynFlags -> Type -> String
pretty dflag = showUnqualifiedOneLine dflag . 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)
2013-03-12 13:15:23 +00:00
dflag <- getSessionDynFlags
return $ showUnqualifiedPage dflag $ vcat (intersperse (text "") $ map (pprInfo False) filtered)
2010-11-15 05:46:59 +00:00
filterOutChildren :: (a -> TyThing) -> [a] -> [a]
filterOutChildren get_thing xs
2013-03-12 07:19:44 +00:00
= [x | x <- xs, not (getName (get_thing x) `elemNameSet` implicits)]
2010-11-15 05:46:59 +00:00
where
2013-03-12 07:19:44 +00:00
implicits = mkNameSet [getName t | x <- xs, t <- implicitTyThings (get_thing x)]
2010-11-15 05:46:59 +00:00
pprInfo :: PrintExplicitForalls -> (TyThing, GHC.Fixity, [Gap.ClsInst]) -> 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
2013-03-12 07:19:44 +00:00
| fx == defaultFixity = Outputable.empty
| otherwise = ppr fx <+> ppr (getName thing)
2010-11-15 05:46:59 +00:00
----------------------------------------------------------------
2013-05-18 21:16:37 +00:00
inModuleContext :: Cmd -> Options -> Cradle -> FilePath -> ModuleString -> Ghc String -> String -> Ghc String
inModuleContext cmd opt cradle fileName modstr action errmsg =
2013-05-18 21:16:37 +00:00
valid ||> invalid ||> return errmsg
2011-01-13 08:22:43 +00:00
where
valid = do
void $ initializeFlagsWithCradle opt cradle ["-w:"] False
2013-04-01 05:22:30 +00:00
when (cmd == Info) setSlowDynFlags
2013-03-01 07:42:22 +00:00
setTargetFile fileName
2013-03-15 05:40:36 +00:00
checkSlowAndSet
void $ load LoadAllTargets
2012-02-15 05:52:48 +00:00
doif setContextFromTarget action
2011-01-13 08:22:43 +00:00
invalid = do
void $ initializeFlagsWithCradle opt cradle ["-w:"] False
2011-01-14 02:18:33 +00:00
setTargetBuffer
2013-03-15 05:40:36 +00:00
checkSlowAndSet
void $ 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
2013-03-12 13:15:23 +00:00
dflag <- getSessionDynFlags
let imports = concatMap (map (showQualifiedPage dflag . ppr . unLoc)) $
2011-01-13 08:22:43 +00:00
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 getCurrentTime
setTargets [Gap.mkTarget (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