2012-10-16 03:27:35 -07:00
|
|
|
{-# LANGUAGE TupleSections, FlexibleInstances, TypeSynonymInstances #-}
|
2013-07-02 12:48:44 +04:00
|
|
|
{-# LANGUAGE Rank2Types #-}
|
2013-07-05 16:43:54 +09:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2011-08-24 15:58:12 +09:00
|
|
|
|
2013-05-20 14:28:56 +09:00
|
|
|
module Language.Haskell.GhcMod.Info (
|
|
|
|
|
infoExpr
|
|
|
|
|
, info
|
|
|
|
|
, typeExpr
|
|
|
|
|
, typeOf
|
|
|
|
|
) where
|
2010-11-12 16:27:50 +09:00
|
|
|
|
2012-02-14 16:09:53 +09:00
|
|
|
import Control.Applicative
|
2013-04-01 14:16:34 +09:00
|
|
|
import Control.Monad (void, when)
|
2012-02-12 21:04:18 +09:00
|
|
|
import CoreUtils
|
2012-02-14 11:33:27 +09:00
|
|
|
import Data.Function
|
2013-05-19 01:16:37 +04:00
|
|
|
import Data.Generics hiding (typeOf)
|
2011-05-24 16:17:19 +09:00
|
|
|
import Data.List
|
2010-11-15 14:46:59 +09:00
|
|
|
import Data.Maybe
|
2012-02-13 01:01:58 +09:00
|
|
|
import Data.Ord as O
|
2012-09-27 14:32:18 -10:00
|
|
|
import Data.Time.Clock
|
2012-02-12 21:04:18 +09:00
|
|
|
import Desugar
|
2010-11-12 16:27:50 +09:00
|
|
|
import GHC
|
2012-02-14 16:19:48 +09:00
|
|
|
import GHC.SYB.Utils
|
2011-05-24 16:17:19 +09:00
|
|
|
import HscTypes
|
2013-05-17 10:00:01 +09:00
|
|
|
import Language.Haskell.GhcMod.Doc
|
|
|
|
|
import Language.Haskell.GhcMod.GHCApi
|
|
|
|
|
import Language.Haskell.GhcMod.GHCChoice
|
|
|
|
|
import qualified Language.Haskell.GhcMod.Gap as Gap
|
2013-07-02 12:48:44 +04:00
|
|
|
import Language.Haskell.GhcMod.Gap (HasType(..))
|
2013-05-17 10:00:01 +09:00
|
|
|
import Language.Haskell.GhcMod.Types
|
2010-11-12 16:27:50 +09:00
|
|
|
import Outputable
|
|
|
|
|
import PprTyThing
|
2012-02-18 19:14:29 +09:00
|
|
|
import TcHsSyn (hsPatType)
|
2013-03-01 21:17:52 +09:00
|
|
|
import TcRnTypes
|
2010-11-17 17:07:33 +09:00
|
|
|
|
2012-02-14 16:09:53 +09:00
|
|
|
----------------------------------------------------------------
|
2011-08-24 15:58:12 +09:00
|
|
|
|
2013-04-01 14:16:34 +09:00
|
|
|
data Cmd = Info | Type deriving Eq
|
|
|
|
|
|
2010-11-15 14:46:59 +09:00
|
|
|
----------------------------------------------------------------
|
2010-11-12 16:27:50 +09:00
|
|
|
|
2013-05-20 14:28:56 +09:00
|
|
|
-- | Obtaining information of a target expression. (GHCi's info:)
|
|
|
|
|
infoExpr :: Options
|
|
|
|
|
-> Cradle
|
|
|
|
|
-> FilePath -- ^ A target file
|
|
|
|
|
-> ModuleString -- ^ A module name
|
|
|
|
|
-> Expression -- ^ A Haskell expression
|
|
|
|
|
-> IO String
|
|
|
|
|
infoExpr opt cradle file modstr expr = (++ "\n") <$> withGHCDummyFile (info opt cradle file modstr expr)
|
|
|
|
|
|
|
|
|
|
-- | Obtaining information of a target expression. (GHCi's info:)
|
|
|
|
|
info :: Options
|
|
|
|
|
-> Cradle
|
|
|
|
|
-> FilePath -- ^ A target file
|
|
|
|
|
-> ModuleString -- ^ A module name
|
|
|
|
|
-> Expression -- ^ A Haskell expression
|
|
|
|
|
-> Ghc String
|
|
|
|
|
info opt cradle file modstr expr =
|
|
|
|
|
inModuleContext Info opt cradle file modstr exprToInfo "Cannot show info"
|
2011-01-13 17:22:43 +09:00
|
|
|
where
|
2013-07-14 17:07:30 +09:00
|
|
|
exprToInfo = do
|
|
|
|
|
dflag <- getSessionDynFlags
|
|
|
|
|
sdoc <- Gap.infoThing expr
|
|
|
|
|
return $ showUnqualifiedPage dflag sdoc
|
2011-01-14 11:18:33 +09:00
|
|
|
|
2012-02-12 21:04:18 +09:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
2012-02-18 20:02:37 +09:00
|
|
|
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 (LPat Id) where
|
|
|
|
|
getType _ (L spn pat) = return $ Just (spn, hsPatType pat)
|
|
|
|
|
|
2013-05-20 14:28:56 +09:00
|
|
|
----------------------------------------------------------------
|
2012-02-12 21:04:18 +09:00
|
|
|
|
2013-05-20 14:28:56 +09:00
|
|
|
-- | Obtaining type of a target expression. (GHCi's type:)
|
|
|
|
|
typeExpr :: Options
|
|
|
|
|
-> Cradle
|
|
|
|
|
-> FilePath -- ^ A target file
|
|
|
|
|
-> ModuleString -- ^ A odule name
|
|
|
|
|
-> Int -- ^ Line number
|
|
|
|
|
-> Int -- ^ Column number
|
|
|
|
|
-> IO String
|
|
|
|
|
typeExpr opt cradle file modstr lineNo colNo = withGHCDummyFile $ typeOf opt cradle file modstr lineNo colNo
|
|
|
|
|
|
|
|
|
|
-- | Obtaining type of a target expression. (GHCi's type:)
|
|
|
|
|
typeOf :: Options
|
|
|
|
|
-> Cradle
|
|
|
|
|
-> FilePath -- ^ A target file
|
|
|
|
|
-> ModuleString -- ^ A odule name
|
|
|
|
|
-> Int -- ^ Line number
|
|
|
|
|
-> Int -- ^ Column number
|
|
|
|
|
-> Ghc String
|
|
|
|
|
typeOf opt cradle file modstr lineNo colNo =
|
|
|
|
|
inModuleContext Type opt cradle file modstr exprToType errmsg
|
2012-02-12 21:04:18 +09:00
|
|
|
where
|
|
|
|
|
exprToType = do
|
|
|
|
|
modSum <- getModSummary $ mkModuleName modstr
|
|
|
|
|
p <- parseModule modSum
|
2012-02-18 20:02:37 +09:00
|
|
|
tcm@TypecheckedModule{tm_typechecked_source = tcs} <- typecheckModule p
|
2012-02-20 02:46:04 +09:00
|
|
|
let bs = listifySpans tcs (lineNo, colNo) :: [LHsBind Id]
|
2012-02-18 20:02:37 +09: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 22:15:23 +09:00
|
|
|
dflag <- getSessionDynFlags
|
|
|
|
|
let sss = map (toTup dflag) $ sortBy (cmp `on` fst) $ catMaybes $ concat [ets, bts, pts]
|
2012-02-14 11:33:27 +09:00
|
|
|
return $ convert opt sss
|
2012-02-13 20:31:36 +09:00
|
|
|
|
2013-03-12 22:15:23 +09:00
|
|
|
toTup :: DynFlags -> (SrcSpan, Type) -> ((Int,Int,Int,Int),String)
|
|
|
|
|
toTup dflag (spn, typ) = (fourInts spn, pretty dflag typ)
|
2012-02-14 16:09:53 +09:00
|
|
|
|
|
|
|
|
fourInts :: SrcSpan -> (Int,Int,Int,Int)
|
|
|
|
|
fourInts = fromMaybe (0,0,0,0) . Gap.getSrcSpan
|
2012-02-14 11:33:27 +09:00
|
|
|
|
2012-02-13 01:01:58 +09:00
|
|
|
cmp a b
|
|
|
|
|
| a `isSubspanOf` b = O.LT
|
|
|
|
|
| b `isSubspanOf` a = O.GT
|
|
|
|
|
| otherwise = O.EQ
|
2012-02-12 21:04:18 +09:00
|
|
|
|
2012-02-15 14:52:48 +09:00
|
|
|
errmsg = convert opt ([] :: [((Int,Int,Int,Int),String)])
|
|
|
|
|
|
2012-02-18 20:02:37 +09:00
|
|
|
listifySpans :: Typeable a => TypecheckedSource -> (Int, Int) -> [Located a]
|
|
|
|
|
listifySpans tcs lc = listifyStaged TypeChecker p tcs
|
2012-02-18 18:17:47 +09:00
|
|
|
where
|
2012-02-18 20:02:37 +09:00
|
|
|
p (L spn _) = isGoodSrcSpan spn && spn `spans` lc
|
2012-02-18 19:14:29 +09:00
|
|
|
|
2012-02-14 03:20:33 +09:00
|
|
|
listifyStaged :: Typeable r => Stage -> (r -> Bool) -> GenericQ [r]
|
2012-02-14 16:19:48 +09:00
|
|
|
listifyStaged s p = everythingStaged s (++) [] ([] `mkQ` (\x -> [x | p x]))
|
2012-02-12 21:04:18 +09:00
|
|
|
|
2013-03-12 22:15:23 +09:00
|
|
|
pretty :: DynFlags -> Type -> String
|
|
|
|
|
pretty dflag = showUnqualifiedOneLine dflag . pprTypeForUser False
|
2012-02-13 13:23:04 +09:00
|
|
|
|
2010-11-15 14:46:59 +09:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
2013-05-19 01:16:37 +04:00
|
|
|
inModuleContext :: Cmd -> Options -> Cradle -> FilePath -> ModuleString -> Ghc String -> String -> Ghc String
|
2013-05-20 14:28:56 +09:00
|
|
|
inModuleContext cmd opt cradle file modstr action errmsg =
|
2013-05-19 01:16:37 +04:00
|
|
|
valid ||> invalid ||> return errmsg
|
2011-01-13 17:22:43 +09:00
|
|
|
where
|
|
|
|
|
valid = do
|
2013-03-16 11:50:45 +09:00
|
|
|
void $ initializeFlagsWithCradle opt cradle ["-w:"] False
|
2013-04-01 14:22:30 +09:00
|
|
|
when (cmd == Info) setSlowDynFlags
|
2013-05-20 14:28:56 +09:00
|
|
|
setTargetFile file
|
2013-03-15 14:40:36 +09:00
|
|
|
checkSlowAndSet
|
|
|
|
|
void $ load LoadAllTargets
|
2012-02-15 14:52:48 +09:00
|
|
|
doif setContextFromTarget action
|
2011-01-13 17:22:43 +09:00
|
|
|
invalid = do
|
2013-03-16 11:50:45 +09:00
|
|
|
void $ initializeFlagsWithCradle opt cradle ["-w:"] False
|
2011-01-14 11:18:33 +09:00
|
|
|
setTargetBuffer
|
2013-03-15 14:40:36 +09:00
|
|
|
checkSlowAndSet
|
|
|
|
|
void $ load LoadAllTargets
|
2012-02-15 14:52:48 +09:00
|
|
|
doif setContextFromTarget action
|
2011-01-14 11:18:33 +09:00
|
|
|
setTargetBuffer = do
|
2011-01-13 17:22:43 +09:00
|
|
|
modgraph <- depanal [mkModuleName modstr] True
|
2013-03-12 22:15:23 +09:00
|
|
|
dflag <- getSessionDynFlags
|
|
|
|
|
let imports = concatMap (map (showQualifiedPage dflag . ppr . unLoc)) $
|
2011-01-13 17:22:43 +09:00
|
|
|
map ms_imps modgraph ++ map ms_srcimps modgraph
|
|
|
|
|
moddef = "module " ++ sanitize modstr ++ " where"
|
|
|
|
|
header = moddef : imports
|
2012-02-14 16:09:53 +09:00
|
|
|
importsBuf <- Gap.toStringBuffer header
|
2012-09-27 14:32:18 -10:00
|
|
|
clkTime <- Gap.liftIO getCurrentTime
|
2012-10-16 03:27:35 -07:00
|
|
|
setTargets [Gap.mkTarget (TargetModule $ mkModuleName modstr)
|
|
|
|
|
True
|
|
|
|
|
(Just (importsBuf, clkTime))]
|
2012-02-15 14:52:48 +09:00
|
|
|
doif m t = m >>= \ok -> if ok then t else goNext
|
2011-01-13 17:22:43 +09:00
|
|
|
sanitize = fromMaybe "SomeModule" . listToMaybe . words
|
|
|
|
|
|
2010-11-18 16:38:55 +09:00
|
|
|
setContextFromTarget :: Ghc Bool
|
2012-02-14 16:09:53 +09:00
|
|
|
setContextFromTarget = depanal [] False >>= Gap.setCtx
|