@@ -9,16 +9,19 @@ module Haskell.Ide.Engine.TypeMap where
99import qualified Data.IntervalMap.FingerTree as IM
1010
1111import qualified GHC
12- import GHC ( TypecheckedModule )
12+ import GHC ( TypecheckedModule , GhcMonad )
13+ import Bag
14+ import BasicTypes
1315
1416import Data.Data as Data
1517import Control.Monad.IO.Class
18+ import Control.Applicative
1619import Data.Maybe
1720import qualified TcHsSyn
1821import qualified CoreUtils
1922import qualified Type
2023import qualified Desugar
21- import Haskell.Ide.Engine.Compat
24+ import qualified Haskell.Ide.Engine.Compat as Compat
2225
2326import Haskell.Ide.Engine.ArtifactMap
2427
@@ -27,44 +30,57 @@ import Haskell.Ide.Engine.ArtifactMap
2730genTypeMap :: GHC. GhcMonad m => TypecheckedModule -> m TypeMap
2831genTypeMap tm = do
2932 let typecheckedSource = GHC. tm_typechecked_source tm
30- hs_env <- GHC. getSession
31- liftIO $ types hs_env typecheckedSource
33+ everythingInTypecheckedSourceM typecheckedSource
3234
3335
3436everythingInTypecheckedSourceM
35- :: Data x => (forall a . Data a => a -> IO TypeMap ) -> x -> IO TypeMap
36- everythingInTypecheckedSourceM = everythingButTypeM @ GHC. Id
37-
37+ :: GhcMonad m => GHC. TypecheckedSource -> m TypeMap
38+ everythingInTypecheckedSourceM xs = bs
39+ where
40+ bs = foldBag (liftA2 IM. union) processBind (return IM. empty) xs
41+
42+ processBind :: GhcMonad m => GHC. LHsBindLR Compat. GhcTc Compat. GhcTc -> m TypeMap
43+ processBind x@ (GHC. L (GHC. RealSrcSpan spn) b) =
44+ case b of
45+ Compat. FunBindGen t fmatches ->
46+ case GHC. mg_origin fmatches of
47+ Generated -> return IM. empty
48+ FromSource -> do
49+ im <- types fmatches
50+ return $ IM. singleton (rspToInt spn) t `IM.union` im
51+ Compat. AbsBinds bs -> everythingInTypecheckedSourceM bs
52+ _ -> types x
53+ processBind _ = return IM. empty
3854
3955-- | Obtain details map for types.
40- types :: GHC. HscEnv -> GHC. TypecheckedSource -> IO TypeMap
41- types hs_env = everythingInTypecheckedSourceM (ty `combineM` fun `combineM` funBind)
56+ types :: forall m a . ( GhcMonad m , Data a ) => a -> m TypeMap
57+ types = everythingButTypeM @ GHC. Id (ty `combineM` fun `combineM` funBind)
4258 where
43- ty :: forall a . Data a => a -> IO TypeMap
59+ ty :: forall a' . ( GhcMonad m , Data a' ) => a' -> m TypeMap
4460 ty term = case cast term of
4561 (Just lhsExprGhc@ (GHC. L (GHC. RealSrcSpan spn) _)) ->
46- getType hs_env lhsExprGhc >>= \ case
62+ getType lhsExprGhc >>= \ case
4763 Nothing -> return IM. empty
4864 Just (_, typ) -> return (IM. singleton (rspToInt spn) typ)
4965 _ -> return IM. empty
5066
51- fun :: forall a . Data a => a -> IO TypeMap
67+ fun :: forall a' . ( GhcMonad m , Data a' ) => a' -> m TypeMap
5268 fun term = case cast term of
5369 (Just (GHC. L (GHC. RealSrcSpan spn) hsPatType)) ->
5470 return (IM. singleton (rspToInt spn) (TcHsSyn. hsPatType hsPatType))
5571 _ -> return IM. empty
5672
57- funBind :: forall a . Data a => a -> IO TypeMap
73+ funBind :: forall a' . ( GhcMonad m , Data a' ) => a' -> m TypeMap
5874 funBind term = case cast term of
59- (Just (GHC. L (GHC. RealSrcSpan spn) (FunBindType t))) ->
75+ (Just (GHC. L (GHC. RealSrcSpan spn) (Compat. FunBindType t))) ->
6076 return (IM. singleton (rspToInt spn) t)
6177 _ -> return IM. empty
6278
6379-- | Combine two queries into one using alternative combinator.
6480combineM
65- :: (forall a . Data a => a -> IO TypeMap )
66- -> (forall a . Data a => a -> IO TypeMap )
67- -> (forall a . Data a => a -> IO TypeMap )
81+ :: (forall a . ( Monad m , Data a ) => a -> m TypeMap )
82+ -> (forall a . ( Monad m , Data a ) => a -> m TypeMap )
83+ -> (forall a . ( Monad m , Data a ) => a -> m TypeMap )
6884combineM f g x = do
6985 a <- f x
7086 b <- g x
@@ -73,10 +89,10 @@ combineM f g x = do
7389-- | Variation of "everything" that does not recurse into children of type t
7490-- requires AllowAmbiguousTypes
7591everythingButTypeM
76- :: forall t
92+ :: forall t m
7793 . (Typeable t )
78- => (forall a . Data a => a -> IO TypeMap )
79- -> (forall a . Data a => a -> IO TypeMap )
94+ => (forall a . ( Monad m , Data a ) => a -> m TypeMap )
95+ -> (forall a . ( Monad m , Data a ) => a -> m TypeMap )
8096everythingButTypeM f = everythingButM $ (,) <$> f <*> isType @ t
8197
8298-- | Returns true if a == t.
@@ -87,8 +103,8 @@ isType _ = isJust $ eqT @a @b
87103-- | Variation of "everything" with an added stop condition
88104-- Just like 'everything', this is stolen from SYB package.
89105everythingButM
90- :: (forall a . Data a => a -> (IO TypeMap , Bool ))
91- -> (forall a . Data a => a -> IO TypeMap )
106+ :: forall m . (forall a . ( Monad m , Data a ) => a -> (m TypeMap , Bool ))
107+ -> (forall a . ( Monad m , Data a ) => a -> m TypeMap )
92108everythingButM f x = do
93109 let (v, stop) = f x
94110 if stop
@@ -111,27 +127,28 @@ everythingButM f x = do
111127--
112128-- See #16233<https://gitlab.haskell.org/ghc/ghc/issues/16233>
113129getType
114- :: GHC. HscEnv - > GHC. LHsExpr GhcTc -> IO (Maybe (GHC. SrcSpan , Type. Type ))
115- getType hs_env e@ (GHC. L spn e') =
130+ :: GhcMonad m = > GHC. LHsExpr Compat. GhcTc -> m (Maybe (GHC. SrcSpan , Type. Type ))
131+ getType e@ (GHC. L spn e') =
116132 -- Some expression forms have their type immediately available
117133 let
118134 tyOpt = case e' of
119- HsOverLitType t -> Just t
120- HsLitType t -> Just t
121- HsLamType t -> Just t
122- HsLamCaseType t -> Just t
123- HsCaseType t -> Just t
124- ExplicitListType t -> Just t
125- ExplicitSumType t -> Just t
126- HsMultiIfType t -> Just t
135+ Compat. HsOverLitType t -> Just t
136+ Compat. HsLitType t -> Just t
137+ Compat. HsLamType t -> Just t
138+ Compat. HsLamCaseType t -> Just t
139+ Compat. HsCaseType t -> Just t
140+ Compat. ExplicitListType t -> Just t
141+ Compat. ExplicitSumType t -> Just t
142+ Compat. HsMultiIfType t -> Just t
127143
128144 _ -> Nothing
129145 in case tyOpt of
130146 Just t -> return $ Just (spn ,t)
131147 Nothing
132148 | skipDesugaring e' -> pure Nothing
133149 | otherwise -> do
134- (_, mbe) <- Desugar. deSugarExpr hs_env e
150+ hsc_env <- GHC. getSession
151+ (_, mbe) <- liftIO $ Desugar. deSugarExpr hsc_env e
135152 let res = (spn, ) . CoreUtils. exprType <$> mbe
136153 pure res
137154 where
0 commit comments