diff --git a/src/Analysis/Abstract/Caching.hs b/src/Analysis/Abstract/Caching.hs index b7a83ac3d..9d5a9f274 100644 --- a/src/Analysis/Abstract/Caching.hs +++ b/src/Analysis/Abstract/Caching.hs @@ -77,7 +77,6 @@ cachingTerms recur term = do convergingModules :: ( AbstractValue address value effects , Cacheable term address (Cell address) value - , Member (Allocator address value) effects , Member Fresh effects , Member NonDet effects , Member (Reader (Cache term address (Cell address) value)) effects diff --git a/src/Control/Abstract/Primitive.hs b/src/Control/Abstract/Primitive.hs index 268850a6e..5eeebc4e0 100644 --- a/src/Control/Abstract/Primitive.hs +++ b/src/Control/Abstract/Primitive.hs @@ -32,14 +32,12 @@ defineClass :: ( AbstractValue address value effects , Member (Reader Span) effects ) => Name - -> [Name] + -> [address] -> Evaluator address value effects a -> Evaluator address value effects () -defineClass name superclasses scope = define name $ do - env <- locally $ do - void scope - Env.newEnv . Env.head <$> getEnv - klass name (map (string . formatName) superclasses) env +defineClass name superclasses body = define name $ do + binds <- Env.head <$> locally (body >> getEnv) + klass name superclasses binds defineNamespace :: ( AbstractValue address value effects , HasCallStack diff --git a/src/Control/Abstract/Value.hs b/src/Control/Abstract/Value.hs index 7a713df4d..2d3e8a487 100644 --- a/src/Control/Abstract/Value.hs +++ b/src/Control/Abstract/Value.hs @@ -128,9 +128,9 @@ class (AbstractFunction address value effects, AbstractIntro value) => AbstractV index :: value -> value -> Evaluator address value effects address -- | Build a class value from a name and environment. - klass :: Name -- ^ The new class's identifier - -> [value] -- ^ A list of superclasses - -> Environment address -- ^ The environment to capture + klass :: Name -- ^ The new class's identifier + -> [address] -- ^ A list of superclasses + -> Bindings address -- ^ The environment to capture -> Evaluator address value effects value -- | Build a namespace value from a name and environment stack @@ -141,7 +141,7 @@ class (AbstractFunction address value effects, AbstractIntro value) => AbstractV -> Evaluator address value effects value -- | Extract the environment from any scoped object (e.g. classes, namespaces, etc). - scopedEnvironment :: value -> Evaluator address value effects (Maybe (Environment address)) + scopedEnvironment :: address -> Evaluator address value effects (Maybe (Environment address)) -- | Primitive looping combinator, approximately equivalent to 'fix'. This should be used in place of direct recursion, as it allows abstraction over recursion. -- @@ -189,7 +189,7 @@ makeNamespace :: ( AbstractValue address value effects ) => Name -> address - -> Maybe value + -> Maybe address -> Evaluator address value effects value makeNamespace name addr super = do superEnv <- maybe (pure (Just lowerBound)) scopedEnvironment super @@ -203,11 +203,11 @@ makeNamespace name addr super = do evaluateInScopedEnv :: ( AbstractValue address value effects , Member (Env address) effects ) - => Evaluator address value effects value + => address -> Evaluator address value effects a -> Evaluator address value effects a evaluateInScopedEnv scopedEnvTerm term = do - scopedEnv <- scopedEnvTerm >>= scopedEnvironment + scopedEnv <- scopedEnvironment scopedEnvTerm maybe term (\ env -> locally (bindAll env *> term)) scopedEnv @@ -233,19 +233,17 @@ subtermValue = value <=< subtermRef -- | Returns the address of a value referenced by a 'ValueRef' address :: ( AbstractValue address value effects - , Member (Allocator address value) effects , Member (Env address) effects , Member (Resumable (EnvironmentError address)) effects ) => ValueRef address -> Evaluator address value effects address address (LvalLocal var) = variable var -address (LvalMember obj prop) = evaluateInScopedEnv (deref obj) (variable prop) +address (LvalMember ptr prop) = evaluateInScopedEnv ptr (variable prop) address (Rval addr) = pure addr -- | Evaluates a 'Subterm' to the address of its rval subtermAddress :: ( AbstractValue address value effects - , Member (Allocator address value) effects , Member (Env address) effects , Member (Resumable (EnvironmentError address)) effects ) diff --git a/src/Data/Abstract/Environment.hs b/src/Data/Abstract/Environment.hs index d742a8ef6..198580de9 100644 --- a/src/Data/Abstract/Environment.hs +++ b/src/Data/Abstract/Environment.hs @@ -36,7 +36,7 @@ import Prologue -- | A map of names to values. Represents a single scope level of an environment chain. newtype Bindings address = Bindings { unBindings :: Map.Map Name address } - deriving (Eq, Ord) + deriving (Eq, Ord, Show) instance Semigroup (Bindings address) where (<>) (Bindings a) (Bindings b) = Bindings (a <> b) diff --git a/src/Data/Abstract/Value/Concrete.hs b/src/Data/Abstract/Value/Concrete.hs index 29ad5e8f7..394037f81 100644 --- a/src/Data/Abstract/Value/Concrete.hs +++ b/src/Data/Abstract/Value/Concrete.hs @@ -1,8 +1,8 @@ -{-# LANGUAGE GADTs, RankNTypes, TypeOperators, UndecidableInstances #-} +{-# LANGUAGE GADTs, RankNTypes, TypeOperators, UndecidableInstances, LambdaCase #-} module Data.Abstract.Value.Concrete where import Control.Abstract -import Data.Abstract.Environment (Environment, mergeEnvs) +import Data.Abstract.Environment (Environment, Bindings) import qualified Data.Abstract.Environment as Env import Data.Abstract.Name import qualified Data.Abstract.Number as Number @@ -24,7 +24,7 @@ data Value address body | Symbol Text | Tuple [address] | Array [address] - | Class Name (Environment address) + | Class Name [address] (Bindings address) | Namespace Name (Environment address) | KVPair (Value address body) (Value address body) | Hash [Value address body] @@ -118,10 +118,8 @@ instance ( Coercible body (Eff effects) tuple = pure . Tuple array = pure . Array - klass n [] env = pure $ Class n env - klass n supers env = do - product <- foldl mergeEnvs lowerBound . catMaybes <$> traverse scopedEnvironment supers - pure $ Class n (mergeEnvs product env) + klass n supers binds = do + pure $ Class n supers binds namespace name env = do maybeAddr <- lookupEnv name @@ -131,10 +129,13 @@ instance ( Coercible body (Eff effects) | Namespace _ env' <- v = pure env' | otherwise = throwValueError $ NamespaceError ("expected " <> show v <> " to be a namespace") - scopedEnvironment o - | Class _ env <- o = pure (Just env) - | Namespace _ env <- o = pure (Just env) - | otherwise = pure Nothing + scopedEnvironment ptr = do + ancestors <- ancestorBinds [ptr] + pure (Env.Environment <$> nonEmpty ancestors) + where ancestorBinds = (pure . concat) <=< traverse (deref >=> \case + Class _ supers binds -> (binds :) <$> ancestorBinds (reverse supers) + Namespace _ env -> pure . toList . Env.unEnvironment $ env + _ -> pure []) asString v | String n <- v = pure n diff --git a/src/Data/Syntax/Declaration.hs b/src/Data/Syntax/Declaration.hs index 7cf859c98..148127c94 100644 --- a/src/Data/Syntax/Declaration.hs +++ b/src/Data/Syntax/Declaration.hs @@ -163,11 +163,11 @@ instance Show1 Class where liftShowsPrec = genericLiftShowsPrec instance Evaluatable Class where eval Class{..} = do name <- either (throwEvalError . FreeVariablesError) pure (freeVariable $ subterm classIdentifier) - supers <- traverse subtermValue classSuperclasses + supers <- traverse subtermAddress classSuperclasses (_, addr) <- letrec name $ do void $ subtermValue classBody - classEnv <- newEnv . Env.head <$> getEnv - klass name supers classEnv + classBinds <- Env.head <$> getEnv + klass name supers classBinds bind name addr pure (Rval addr) diff --git a/src/Data/Syntax/Expression.hs b/src/Data/Syntax/Expression.hs index 6ebd10654..aa042a0b4 100644 --- a/src/Data/Syntax/Expression.hs +++ b/src/Data/Syntax/Expression.hs @@ -439,7 +439,7 @@ instance Show1 ScopeResolution where liftShowsPrec = genericLiftShowsPrec instance Evaluatable ScopeResolution where eval (ScopeResolution xs) = Rval <$> foldl1 f (fmap subtermAddress xs) - where f ns = evaluateInScopedEnv (ns >>= deref) + where f ns id = ns >>= flip evaluateInScopedEnv id -- | A non-null expression such as Typescript or Swift's ! expression. diff --git a/src/Language/PHP/Syntax.hs b/src/Language/PHP/Syntax.hs index 4f7205451..4fd906d2f 100644 --- a/src/Language/PHP/Syntax.hs +++ b/src/Language/PHP/Syntax.hs @@ -200,7 +200,9 @@ instance Ord1 QualifiedName where liftCompare = genericLiftCompare instance Show1 QualifiedName where liftShowsPrec = genericLiftShowsPrec instance Evaluatable QualifiedName where - eval (QualifiedName name iden) = Rval <$> evaluateInScopedEnv (subtermValue name) (subtermAddress iden) + eval (QualifiedName name iden) = do + namePtr <- subtermAddress name + Rval <$> evaluateInScopedEnv namePtr (subtermAddress iden) newtype NamespaceName a = NamespaceName (NonEmpty a) deriving (Eq, Ord, Show, Foldable, Traversable, Functor, Generic1, Diffable, Mergeable, FreeVariables1, Declarations1, ToJSONFields1) @@ -212,7 +214,7 @@ instance Show1 NamespaceName where liftShowsPrec = genericLiftShowsPrec instance Evaluatable NamespaceName where eval (NamespaceName xs) = Rval <$> foldl1 f (fmap subtermAddress xs) - where f ns = evaluateInScopedEnv (ns >>= deref) + where f ns id = ns >>= flip evaluateInScopedEnv id newtype ConstDeclaration a = ConstDeclaration [a] deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable) diff --git a/src/Language/Ruby/Syntax.hs b/src/Language/Ruby/Syntax.hs index 8afd9276c..635b97a91 100644 --- a/src/Language/Ruby/Syntax.hs +++ b/src/Language/Ruby/Syntax.hs @@ -54,7 +54,7 @@ instance Evaluatable Send where let sel = case sendSelector of Just sel -> subtermAddress sel Nothing -> variable (name "call") - func <- deref =<< maybe sel (flip evaluateInScopedEnv sel . subtermValue) sendReceiver + func <- deref =<< maybe sel (flip evaluateInScopedEnv sel <=< subtermAddress) sendReceiver Rval <$> call func (map subtermAddress sendArgs) -- TODO pass through sendBlock data Require a = Require { requireRelative :: Bool, requirePath :: !a } @@ -131,7 +131,7 @@ instance Show1 Class where liftShowsPrec = genericLiftShowsPrec instance Evaluatable Class where eval Class{..} = do - super <- traverse subtermValue classSuperClass + super <- traverse subtermAddress classSuperClass name <- either (throwEvalError . FreeVariablesError) pure (freeVariable $ subterm classIdentifier) rvalBox =<< letrec' name (\addr -> subtermValue classBody <* makeNamespace name addr super) diff --git a/src/Language/TypeScript/Syntax.hs b/src/Language/TypeScript/Syntax.hs index 21fbf0227..f515fe218 100644 --- a/src/Language/TypeScript/Syntax.hs +++ b/src/Language/TypeScript/Syntax.hs @@ -742,11 +742,11 @@ instance Declarations a => Declarations (AbstractClass a) where instance Evaluatable AbstractClass where eval AbstractClass{..} = do name <- either (throwEvalError . FreeVariablesError) pure (freeVariable $ subterm abstractClassIdentifier) - supers <- traverse subtermValue classHeritage + supers <- traverse subtermAddress classHeritage (v, addr) <- letrec name $ do void $ subtermValue classBody - classEnv <- newEnv . Env.head <$> getEnv - klass name supers classEnv + classBinds <- Env.head <$> getEnv + klass name supers classBinds rvalBox =<< (v <$ bind name addr)