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

171 lines
6.4 KiB
Haskell
Raw Normal View History

2013-11-12 23:44:34 +00:00
{-# LANGUAGE TupleSections, FlexibleInstances, TypeSynonymInstances, CPP #-}
{-# LANGUAGE Rank2Types #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
2011-08-24 06:58:12 +00:00
2013-05-20 05:28:56 +00:00
module Language.Haskell.GhcMod.Info (
infoExpr
, info
, typeExpr
, typeOf
) where
2010-11-12 07:27:50 +00:00
2014-01-08 03:03:32 +00:00
import Control.Applicative ((<$>))
2014-03-17 06:56:00 +00:00
import Control.Monad (void)
2014-03-27 06:08:07 +00:00
import CoreMonad (liftIO)
2014-01-08 03:03:32 +00:00
import CoreUtils (exprType)
import Data.Function (on)
2014-03-31 03:31:21 +00:00
--import Data.Generics (Typeable, GenericQ, mkQ)
2013-05-18 21:16:37 +00:00
import Data.Generics hiding (typeOf)
2014-01-08 03:03:32 +00:00
import Data.List (sortBy)
import Data.Maybe (catMaybes, fromMaybe, listToMaybe)
2012-02-12 16:01:58 +00:00
import Data.Ord as O
2014-01-08 03:03:32 +00:00
import Data.Time.Clock (getCurrentTime)
2014-03-27 06:56:14 +00:00
import GHC (Ghc, LHsBind, LHsExpr, LPat, Id, TypecheckedModule(..), DynFlags, SrcSpan, Type, Located, TypecheckedSource, GenLocated(L), LoadHowMuch(..), TargetId(..))
import qualified GHC as G
2014-01-08 03:03:32 +00:00
import GHC.SYB.Utils (Stage(TypeChecker), everythingStaged)
import HscTypes (ms_imps)
import Language.Haskell.GhcMod.Doc (showUnqualifiedPage, showUnqualifiedOneLine, showQualifiedPage)
2013-05-17 01:00:01 +00:00
import Language.Haskell.GhcMod.GHCApi
2014-01-08 03:03:32 +00:00
import Language.Haskell.GhcMod.GHCChoice ((||>), goNext)
import Language.Haskell.GhcMod.Gap (HasType(..))
2014-03-27 06:56:14 +00:00
import qualified Language.Haskell.GhcMod.Gap as Gap
2013-05-17 01:00:01 +00:00
import Language.Haskell.GhcMod.Types
2014-01-08 03:03:32 +00:00
import Outputable (ppr)
2012-02-18 10:14:29 +00:00
import TcHsSyn (hsPatType)
2012-02-14 07:09:53 +00:00
----------------------------------------------------------------
2011-08-24 06:58:12 +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 05:28:56 +00:00
-- | Obtaining information of a target expression. (GHCi's info:)
infoExpr :: Options
-> Cradle
2013-09-05 05:35:28 +00:00
-> FilePath -- ^ A target file.
-> ModuleString -- ^ A module name.
-> Expression -- ^ A Haskell expression.
2013-05-20 05:28:56 +00:00
-> 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
2013-09-05 05:35:28 +00:00
-> FilePath -- ^ A target file.
-> ModuleString -- ^ A module name.
-> Expression -- ^ A Haskell expression.
2013-05-20 05:28:56 +00:00
-> Ghc String
info opt cradle file modstr expr =
inModuleContext Info opt cradle file modstr exprToInfo "Cannot show info"
2011-01-13 08:22:43 +00:00
where
2013-07-14 08:07:30 +00:00
exprToInfo = do
2014-03-27 06:56:14 +00:00
dflag <- G.getSessionDynFlags
2013-07-14 08:07:30 +00:00
sdoc <- Gap.infoThing expr
return $ showUnqualifiedPage dflag sdoc
2011-01-14 02:18:33 +00:00
2012-02-12 12:04:18 +00:00
----------------------------------------------------------------
2013-11-12 23:44:34 +00:00
instance HasType (LHsExpr Id) where
getType tcm e = do
2014-03-27 06:56:14 +00:00
hs_env <- G.getSession
2014-03-27 06:08:07 +00:00
mbe <- liftIO $ Gap.deSugar tcm e hs_env
2014-03-27 06:56:14 +00:00
return $ (G.getLoc e, ) <$> CoreUtils.exprType <$> mbe
2012-02-18 11:02:37 +00:00
instance HasType (LPat Id) where
2014-03-27 06:56:14 +00:00
getType _ (G.L spn pat) = return $ Just (spn, hsPatType pat)
2012-02-18 11:02:37 +00:00
2013-05-20 05:28:56 +00:00
----------------------------------------------------------------
2012-02-12 12:04:18 +00:00
2013-05-20 05:28:56 +00:00
-- | Obtaining type of a target expression. (GHCi's type:)
typeExpr :: Options
-> Cradle
2013-09-05 05:35:28 +00:00
-> FilePath -- ^ A target file.
-> ModuleString -- ^ A odule name.
-> Int -- ^ Line number.
-> Int -- ^ Column number.
2013-05-20 05:28:56 +00:00
-> 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
2013-09-05 05:35:28 +00:00
-> FilePath -- ^ A target file.
-> ModuleString -- ^ A odule name.
-> Int -- ^ Line number.
-> Int -- ^ Column number.
2013-05-20 05:28:56 +00:00
-> Ghc String
typeOf opt cradle file modstr lineNo colNo =
inModuleContext Type opt cradle file modstr exprToType errmsg
2012-02-12 12:04:18 +00:00
where
exprToType = do
2014-03-27 06:56:14 +00:00
modSum <- G.getModSummary $ G.mkModuleName modstr
p <- G.parseModule modSum
tcm@TypecheckedModule{tm_typechecked_source = tcs} <- G.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
2014-03-27 06:56:14 +00:00
dflag <- G.getSessionDynFlags
2013-03-12 13:15:23 +00:00
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
2014-03-27 06:56:14 +00:00
| a `G.isSubspanOf` b = O.LT
| b `G.isSubspanOf` a = O.GT
2012-02-12 16:01:58 +00:00
| 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
2014-03-27 06:56:14 +00:00
p (L spn _) = G.isGoodSrcSpan spn && spn `G.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
2013-11-19 03:35:42 +00:00
pretty dflag = showUnqualifiedOneLine dflag . Gap.typeForUser
2012-02-13 04:23:04 +00:00
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
2014-03-17 06:56:00 +00:00
inModuleContext _ opt cradle file 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
setTargetFiles [file]
2014-03-27 06:56:14 +00:00
void $ G.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
2014-03-27 06:56:14 +00:00
void $ G.load LoadAllTargets
2012-02-15 05:52:48 +00:00
doif setContextFromTarget action
2011-01-14 02:18:33 +00:00
setTargetBuffer = do
2014-03-27 06:56:14 +00:00
modgraph <- G.depanal [G.mkModuleName modstr] True
dflag <- G.getSessionDynFlags
let imports = concatMap (map (showQualifiedPage dflag . ppr . G.unLoc)) $
map ms_imps modgraph ++ map G.ms_srcimps modgraph
2011-01-13 08:22:43 +00:00
moddef = "module " ++ sanitize modstr ++ " where"
header = moddef : imports
2012-02-14 07:09:53 +00:00
importsBuf <- Gap.toStringBuffer header
2014-03-27 06:08:07 +00:00
clkTime <- liftIO getCurrentTime
2014-03-27 06:56:14 +00:00
G.setTargets [Gap.mkTarget (TargetModule $ G.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
2014-03-27 06:56:14 +00:00
setContextFromTarget = G.depanal [] False >>= Gap.setCtx