2016-05-18 15:58:34 +00:00
|
|
|
-- TODO: remove CPP once Gap(ed)
|
2016-05-19 16:25:05 +00:00
|
|
|
{-# LANGUAGE CPP, TupleSections, FlexibleInstances, Rank2Types #-}
|
2014-06-21 09:38:44 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2017-05-28 02:22:56 +00:00
|
|
|
module GhcMod.SrcUtils where
|
2014-06-21 09:38:44 +00:00
|
|
|
|
2015-08-03 01:09:56 +00:00
|
|
|
import Control.Applicative
|
2014-06-21 09:38:44 +00:00
|
|
|
import CoreUtils (exprType)
|
|
|
|
import Data.Generics
|
2016-01-19 19:50:14 +00:00
|
|
|
import Data.Maybe
|
2014-06-21 09:38:44 +00:00
|
|
|
import Data.Ord as O
|
2014-06-28 21:18:49 +00:00
|
|
|
import GHC (LHsExpr, LPat, Id, DynFlags, SrcSpan, Type, Located, ParsedSource, RenamedSource, TypecheckedSource, GenLocated(L))
|
2016-01-19 19:50:14 +00:00
|
|
|
import Var (Var)
|
2014-06-21 09:38:44 +00:00
|
|
|
import qualified GHC as G
|
2016-01-19 19:50:14 +00:00
|
|
|
import qualified Var as G
|
|
|
|
import qualified Type as G
|
|
|
|
import GHC.SYB.Utils
|
2014-07-18 05:05:20 +00:00
|
|
|
import GhcMonad
|
2016-08-02 03:35:56 +00:00
|
|
|
import qualified Language.Haskell.Exts as HE
|
2017-05-28 02:22:56 +00:00
|
|
|
import GhcMod.Doc
|
|
|
|
import GhcMod.Gap
|
|
|
|
import qualified GhcMod.Gap as Gap
|
2014-07-18 05:05:20 +00:00
|
|
|
import OccName (OccName)
|
2014-06-21 09:38:44 +00:00
|
|
|
import Outputable (PprStyle)
|
|
|
|
import TcHsSyn (hsPatType)
|
2015-08-03 01:09:56 +00:00
|
|
|
import Prelude
|
2016-01-19 19:50:14 +00:00
|
|
|
import Control.Monad
|
2016-01-20 00:11:50 +00:00
|
|
|
import Data.List (nub)
|
2016-01-19 19:50:14 +00:00
|
|
|
import Control.Arrow
|
|
|
|
import qualified Data.Map as M
|
2014-06-21 09:38:44 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
instance HasType (LHsExpr Id) where
|
|
|
|
getType tcm e = do
|
|
|
|
hs_env <- G.getSession
|
|
|
|
mbe <- liftIO $ Gap.deSugar tcm e hs_env
|
|
|
|
return $ (G.getLoc e, ) <$> CoreUtils.exprType <$> mbe
|
|
|
|
|
|
|
|
instance HasType (LPat Id) where
|
|
|
|
getType _ (G.L spn pat) = return $ Just (spn, hsPatType pat)
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2016-03-14 19:12:04 +00:00
|
|
|
-- | Stores mapping from monomorphic to polymorphic types
|
2016-01-19 19:50:14 +00:00
|
|
|
type CstGenQS = M.Map Var Type
|
2016-03-14 19:12:04 +00:00
|
|
|
-- | Generic type to simplify SYB definition
|
2016-01-19 23:23:51 +00:00
|
|
|
type CstGenQT a = forall m. GhcMonad m => a Id -> CstGenQS -> (m [(SrcSpan, Type)], CstGenQS)
|
2016-01-19 19:50:14 +00:00
|
|
|
|
2016-01-19 22:29:33 +00:00
|
|
|
collectSpansTypes :: (GhcMonad m) => Bool -> G.TypecheckedModule -> (Int, Int) -> m [(SrcSpan, Type)]
|
|
|
|
collectSpansTypes withConstraints tcs lc =
|
2016-03-14 19:12:04 +00:00
|
|
|
-- This walks AST top-down, left-to-right, while carrying CstGenQS down the tree
|
|
|
|
-- (but not left-to-right)
|
2016-01-19 22:00:13 +00:00
|
|
|
everythingStagedWithContext TypeChecker M.empty (liftM2 (++))
|
|
|
|
(return [])
|
2016-01-19 23:23:51 +00:00
|
|
|
((return [],)
|
2016-03-14 19:12:04 +00:00
|
|
|
`mkQ` (hsBind :: CstGenQT G.LHsBind) -- matches on binds
|
|
|
|
`extQ` (genericCT :: CstGenQT G.LHsExpr) -- matches on expressions
|
|
|
|
`extQ` (genericCT :: CstGenQT G.LPat) -- matches on patterns
|
2016-01-19 23:23:51 +00:00
|
|
|
)
|
2016-01-19 19:50:14 +00:00
|
|
|
(G.tm_typechecked_source tcs)
|
|
|
|
where
|
2016-03-14 19:12:04 +00:00
|
|
|
-- Helper function to insert mapping into CstGenQS
|
2016-01-19 19:50:14 +00:00
|
|
|
insExp x = M.insert (G.abe_mono x) (G.varType $ G.abe_poly x)
|
2016-03-14 19:12:04 +00:00
|
|
|
-- If there is AbsBinds here, insert mapping into CstGenQS if needed
|
2016-01-19 19:50:14 +00:00
|
|
|
hsBind (L _ G.AbsBinds{abs_exports = es'}) s
|
2016-01-19 22:29:33 +00:00
|
|
|
| withConstraints = (return [], foldr insExp s es')
|
|
|
|
| otherwise = (return [], s)
|
2016-06-05 03:59:39 +00:00
|
|
|
#if __GLASGOW_HASKELL__ >= 800
|
|
|
|
-- TODO: move to Gap
|
|
|
|
-- Note: this deals with bindings with explicit type signature, e.g.
|
|
|
|
-- double :: Num a => a -> a
|
|
|
|
-- double x = 2*x
|
|
|
|
hsBind (L _ G.AbsBindsSig{abs_sig_export = poly, abs_sig_bind = bind}) s
|
|
|
|
| withConstraints =
|
|
|
|
let new_s =
|
|
|
|
case bind of
|
|
|
|
G.L _ G.FunBind{fun_id = i} -> M.insert (G.unLoc i) (G.varType poly) s
|
|
|
|
_ -> s
|
|
|
|
in (return [], new_s)
|
|
|
|
| otherwise = (return [], s)
|
|
|
|
#endif
|
2016-03-14 19:12:04 +00:00
|
|
|
-- Otherwise, it's the same as other cases
|
2016-01-19 23:23:51 +00:00
|
|
|
hsBind x s = genericCT x s
|
2016-03-14 19:12:04 +00:00
|
|
|
-- Generic SYB function to get type
|
|
|
|
genericCT x s
|
|
|
|
| withConstraints
|
|
|
|
= (maybe [] (uncurry $ constrainedType (collectBinders x) s) <$> getType' x, s)
|
|
|
|
| otherwise = (maybeToList <$> getType' x, s)
|
|
|
|
-- Collects everything with Id from LHsBind, LHsExpr, or LPat
|
2016-01-19 23:23:51 +00:00
|
|
|
collectBinders :: Data a => a -> [Id]
|
|
|
|
collectBinders = listifyStaged TypeChecker (const True)
|
2016-03-14 19:12:04 +00:00
|
|
|
-- Gets monomorphic type with location
|
2016-01-19 19:50:14 +00:00
|
|
|
getType' x@(L spn _)
|
|
|
|
| G.isGoodSrcSpan spn && spn `G.spans` lc
|
|
|
|
= getType tcs x
|
|
|
|
| otherwise = return Nothing
|
2016-03-14 19:12:04 +00:00
|
|
|
-- Gets constrained type
|
|
|
|
constrainedType :: [Var] -- ^ Binders in expression, i.e. anything with Id
|
|
|
|
-> CstGenQS -- ^ Map from Id to polymorphic type
|
|
|
|
-> SrcSpan -- ^ extent of expression, copied to result
|
|
|
|
-> Type -- ^ monomorphic type
|
|
|
|
-> [(SrcSpan, Type)] -- ^ result
|
2016-01-19 19:50:14 +00:00
|
|
|
constrainedType pids s spn genTyp =
|
|
|
|
let
|
2016-03-14 19:12:04 +00:00
|
|
|
-- runs build on every binder.
|
2016-01-20 00:11:50 +00:00
|
|
|
ctys = mapMaybe build (nub pids)
|
2016-03-14 19:12:04 +00:00
|
|
|
-- Computes constrained type for x. Returns (constraints, substitutions)
|
|
|
|
-- Substitutions are needed because type variables don't match
|
|
|
|
-- between polymorphic and monomorphic types.
|
|
|
|
-- E.g. poly type might be `Monad m => m ()`, while monomorphic might be `f ()`
|
2016-01-19 19:50:14 +00:00
|
|
|
build x | Just cti <- x `M.lookup` s
|
|
|
|
= let
|
|
|
|
(preds', ctt) = getPreds cti
|
2016-03-14 19:12:04 +00:00
|
|
|
-- list of type variables in monomorphic type
|
2016-01-19 21:01:41 +00:00
|
|
|
vts = listifyStaged TypeChecker G.isTyVar $ G.varType x
|
2016-03-14 19:12:04 +00:00
|
|
|
-- list of type variables in polymorphic type
|
2016-01-19 21:01:41 +00:00
|
|
|
tvm = listifyStaged TypeChecker G.isTyVarTy ctt
|
|
|
|
in Just (preds', zip vts tvm)
|
2016-01-19 19:50:14 +00:00
|
|
|
| otherwise = Nothing
|
2016-03-14 19:12:04 +00:00
|
|
|
-- list of constraints
|
2016-01-19 19:50:14 +00:00
|
|
|
preds = concatMap fst ctys
|
2016-03-14 19:12:04 +00:00
|
|
|
-- Type variable substitutions
|
2016-05-18 15:58:34 +00:00
|
|
|
#if __GLASGOW_HASKELL__ >= 800
|
|
|
|
-- TODO: move to Gap
|
|
|
|
subs = G.mkTvSubstPrs $ concatMap snd ctys
|
|
|
|
#else
|
2016-01-19 21:01:41 +00:00
|
|
|
subs = G.mkTopTvSubst $ concatMap snd ctys
|
2016-05-18 15:58:34 +00:00
|
|
|
#endif
|
2016-03-14 19:12:04 +00:00
|
|
|
-- Constrained type
|
2016-01-19 19:50:14 +00:00
|
|
|
ty = G.substTy subs $ G.mkFunTys preds genTyp
|
2016-01-19 21:10:18 +00:00
|
|
|
in [(spn, ty)]
|
2016-03-14 19:12:04 +00:00
|
|
|
-- Splits a given type into list of constraints and simple type. Drops foralls.
|
|
|
|
getPreds :: Type -> ([Type], Type)
|
2016-01-19 19:50:14 +00:00
|
|
|
getPreds x | G.isForAllTy x = getPreds $ G.dropForAlls x
|
|
|
|
| Just (c, t) <- G.splitFunTy_maybe x
|
|
|
|
, G.isPredTy c = first (c:) $ getPreds t
|
|
|
|
| otherwise = ([], x)
|
|
|
|
|
2014-06-21 09:38:44 +00:00
|
|
|
listifySpans :: Typeable a => TypecheckedSource -> (Int, Int) -> [Located a]
|
|
|
|
listifySpans tcs lc = listifyStaged TypeChecker p tcs
|
|
|
|
where
|
|
|
|
p (L spn _) = G.isGoodSrcSpan spn && spn `G.spans` lc
|
|
|
|
|
|
|
|
listifyParsedSpans :: Typeable a => ParsedSource -> (Int, Int) -> [Located a]
|
|
|
|
listifyParsedSpans pcs lc = listifyStaged Parser p pcs
|
|
|
|
where
|
|
|
|
p (L spn _) = G.isGoodSrcSpan spn && spn `G.spans` lc
|
|
|
|
|
|
|
|
listifyRenamedSpans :: Typeable a => RenamedSource -> (Int, Int) -> [Located a]
|
|
|
|
listifyRenamedSpans pcs lc = listifyStaged Renamer p pcs
|
|
|
|
where
|
|
|
|
p (L spn _) = G.isGoodSrcSpan spn && spn `G.spans` lc
|
|
|
|
|
|
|
|
listifyStaged :: Typeable r => Stage -> (r -> Bool) -> GenericQ [r]
|
|
|
|
listifyStaged s p = everythingStaged s (++) [] ([] `mkQ` (\x -> [x | p x]))
|
|
|
|
|
|
|
|
cmp :: SrcSpan -> SrcSpan -> Ordering
|
|
|
|
cmp a b
|
|
|
|
| a `G.isSubspanOf` b = O.LT
|
|
|
|
| b `G.isSubspanOf` a = O.GT
|
|
|
|
| otherwise = O.EQ
|
|
|
|
|
|
|
|
toTup :: DynFlags -> PprStyle -> (SrcSpan, Type) -> ((Int,Int,Int,Int),String)
|
|
|
|
toTup dflag style (spn, typ) = (fourInts spn, pretty dflag style typ)
|
|
|
|
|
|
|
|
fourInts :: SrcSpan -> (Int,Int,Int,Int)
|
|
|
|
fourInts = fromMaybe (0,0,0,0) . Gap.getSrcSpan
|
|
|
|
|
2014-06-27 16:38:15 +00:00
|
|
|
fourIntsHE :: HE.SrcSpan -> (Int,Int,Int,Int)
|
|
|
|
fourIntsHE loc = ( HE.srcSpanStartLine loc, HE.srcSpanStartColumn loc
|
|
|
|
, HE.srcSpanEndLine loc, HE.srcSpanEndColumn loc)
|
|
|
|
|
|
|
|
-- Check whether (line,col) is inside a given SrcSpanInfo
|
|
|
|
typeSigInRangeHE :: Int -> Int -> HE.Decl HE.SrcSpanInfo -> Bool
|
2014-07-24 18:20:30 +00:00
|
|
|
typeSigInRangeHE lineNo colNo (HE.TypeSig (HE.SrcSpanInfo s _) _ _) =
|
|
|
|
HE.srcSpanStart s <= (lineNo, colNo) && HE.srcSpanEnd s >= (lineNo, colNo)
|
2016-08-02 03:35:56 +00:00
|
|
|
typeSigInRangeHE lineNo colNo (HE.TypeFamDecl (HE.SrcSpanInfo s _) _ _ _) =
|
2014-07-24 18:20:30 +00:00
|
|
|
HE.srcSpanStart s <= (lineNo, colNo) && HE.srcSpanEnd s >= (lineNo, colNo)
|
|
|
|
typeSigInRangeHE lineNo colNo (HE.DataFamDecl (HE.SrcSpanInfo s _) _ _ _) =
|
2014-06-27 16:38:15 +00:00
|
|
|
HE.srcSpanStart s <= (lineNo, colNo) && HE.srcSpanEnd s >= (lineNo, colNo)
|
|
|
|
typeSigInRangeHE _ _ _= False
|
|
|
|
|
2014-06-21 09:38:44 +00:00
|
|
|
pretty :: DynFlags -> PprStyle -> Type -> String
|
|
|
|
pretty dflag style = showOneLine dflag style . Gap.typeForUser
|
|
|
|
|
2014-06-27 16:38:15 +00:00
|
|
|
showName :: DynFlags -> PprStyle -> G.Name -> String
|
|
|
|
showName dflag style name = showOneLine dflag style $ Gap.nameForUser name
|
|
|
|
|
|
|
|
showOccName :: DynFlags -> PprStyle -> OccName -> String
|
|
|
|
showOccName dflag style name = showOneLine dflag style $ Gap.occNameForUser name
|