mirror of
https://github.com/github/semantic.git
synced 2024-12-19 21:01:35 +03:00
Merge pull request #2452 from github/filter-gensymed-names
Filter gensymed names
This commit is contained in:
commit
d6bc4f401f
@ -45,9 +45,9 @@ defineBuiltIn declaration rel accessControl value = withCurrentCallStack callSta
|
||||
-- TODO: This span is still wrong.
|
||||
declare declaration rel accessControl emptySpan ScopeGraph.Unknown (Just associatedScope)
|
||||
|
||||
param <- gensym
|
||||
withScope associatedScope $ do
|
||||
declare (Declaration param) rel accessControl emptySpan ScopeGraph.Unknown Nothing
|
||||
param <- gensym
|
||||
declare (Declaration param) ScopeGraph.Gensym accessControl emptySpan ScopeGraph.Unknown Nothing
|
||||
|
||||
slot <- lookupSlot declaration
|
||||
value <- builtIn associatedScope value
|
||||
|
@ -3,6 +3,7 @@
|
||||
module Control.Abstract.ScopeGraph
|
||||
( lookup
|
||||
, declare
|
||||
, declareMaybeName
|
||||
, reference
|
||||
, newScope
|
||||
, newPreludeScope
|
||||
@ -79,6 +80,27 @@ declare decl rel accessControl span kind scope = do
|
||||
moduleInfo <- ask @ModuleInfo
|
||||
modify (fst . ScopeGraph.declare decl moduleInfo rel accessControl span kind scope currentAddress)
|
||||
|
||||
-- | If the provided name is 'Nothing' we want to reflect that the declaration's name was a generated name (gensym).
|
||||
-- We use the 'Gensym' relation to indicate that. Otherwise, we use the provided 'relation'.
|
||||
declareMaybeName :: ( Carrier sig m
|
||||
, Member (State (ScopeGraph address)) sig
|
||||
, Member (Reader (CurrentScope address)) sig
|
||||
, Member (Reader ModuleInfo) sig
|
||||
, Member Fresh sig
|
||||
, Ord address
|
||||
)
|
||||
=> Maybe Name
|
||||
-> Relation
|
||||
-> AccessControl
|
||||
-> Span
|
||||
-> Kind
|
||||
-> Maybe address
|
||||
-> Evaluator term address value m Name
|
||||
declareMaybeName maybeName relation ac span kind scope = do
|
||||
case maybeName of
|
||||
Just name -> declare (Declaration name) relation ac span kind scope >> pure name
|
||||
_ -> gensym >>= \name -> declare (Declaration name) Gensym ac span kind scope >> pure name
|
||||
|
||||
putDeclarationScope :: ( Ord address
|
||||
, Member (Reader (CurrentScope address)) sig
|
||||
, Member (State (ScopeGraph address)) sig
|
||||
|
@ -30,7 +30,7 @@ import Data.Abstract.Declarations as X
|
||||
import Data.Abstract.FreeVariables as X
|
||||
import Data.Abstract.Module
|
||||
import Data.Abstract.Name as X
|
||||
import qualified Data.Abstract.ScopeGraph as ScopeGraph
|
||||
import qualified Data.Abstract.ScopeGraph as ScopeGraph
|
||||
import Data.Abstract.ScopeGraph (Relation(..))
|
||||
import Data.Abstract.AccessControls.Class as X
|
||||
import Data.Language
|
||||
@ -192,7 +192,7 @@ defineSelf :: ( Carrier sig m
|
||||
=> Evaluator term address value m ()
|
||||
defineSelf = do
|
||||
let self = Declaration X.__self
|
||||
declare self Default Public emptySpan ScopeGraph.Unknown Nothing
|
||||
declare self ScopeGraph.Gensym Public emptySpan ScopeGraph.Unknown Nothing
|
||||
slot <- lookupSlot self
|
||||
assign slot =<< object =<< currentFrame
|
||||
|
||||
|
@ -86,7 +86,7 @@ instance Ord AccessControl where
|
||||
(<=) Public _ = False
|
||||
|
||||
|
||||
data Relation = Default | Instance | Prelude
|
||||
data Relation = Default | Instance | Prelude | Gensym
|
||||
deriving (Eq, Show, Ord, Generic, NFData)
|
||||
|
||||
instance Lower Relation where
|
||||
|
@ -28,15 +28,10 @@ instance Diffable Function where
|
||||
|
||||
instance Evaluatable Function where
|
||||
eval _ _ Function{..} = do
|
||||
name <- maybeM (throwNoNameError functionName) (declaredName functionName)
|
||||
span <- ask @Span
|
||||
associatedScope <- declareFunction name ScopeGraph.Public span ScopeGraph.Function
|
||||
(name, associatedScope) <- declareFunction (declaredName functionName) ScopeGraph.Public span ScopeGraph.Function
|
||||
|
||||
params <- withScope associatedScope . for functionParameters $ \paramNode -> do
|
||||
param <- maybeM (throwNoNameError paramNode) (declaredName paramNode)
|
||||
|
||||
let paramSpan = getSpan paramNode
|
||||
param <$ declare (Declaration param) Default ScopeGraph.Public paramSpan ScopeGraph.Parameter Nothing
|
||||
params <- withScope associatedScope . for functionParameters $ \paramNode -> declareMaybeName (declaredName paramNode) Default ScopeGraph.Public (getSpan paramNode) ScopeGraph.Parameter Nothing
|
||||
|
||||
addr <- lookupSlot (Declaration name)
|
||||
v <- function name params functionBody associatedScope
|
||||
@ -50,17 +45,17 @@ declareFunction :: ( Carrier sig m
|
||||
, Member Fresh sig
|
||||
, Ord address
|
||||
)
|
||||
=> Name
|
||||
=> Maybe Name
|
||||
-> ScopeGraph.AccessControl
|
||||
-> Span
|
||||
-> ScopeGraph.Kind
|
||||
-> Evaluator term address value m address
|
||||
-> Evaluator term address value m (Name, address)
|
||||
declareFunction name accessControl span kind = do
|
||||
currentScope' <- currentScope
|
||||
let lexicalEdges = Map.singleton Lexical [ currentScope' ]
|
||||
associatedScope <- newScope lexicalEdges
|
||||
declare (Declaration name) Default accessControl span kind (Just associatedScope)
|
||||
pure associatedScope
|
||||
name' <- declareMaybeName name Default accessControl span kind (Just associatedScope)
|
||||
pure (name', associatedScope)
|
||||
|
||||
instance Tokenize Function where
|
||||
tokenize Function{..} = within' Scope.Function $ do
|
||||
@ -92,16 +87,13 @@ instance Diffable Method where
|
||||
-- local environment.
|
||||
instance Evaluatable Method where
|
||||
eval _ _ Method{..} = do
|
||||
name <- maybeM (throwNoNameError methodName) (declaredName methodName)
|
||||
span <- ask @Span
|
||||
associatedScope <- declareFunction name methodAccessControl span ScopeGraph.Method
|
||||
(name, associatedScope) <- declareFunction (declaredName methodName) methodAccessControl span ScopeGraph.Method
|
||||
|
||||
params <- withScope associatedScope $ do
|
||||
-- TODO: Should we give `self` a special Relation?
|
||||
declare (Declaration __self) Prelude ScopeGraph.Public emptySpan ScopeGraph.Unknown Nothing
|
||||
for methodParameters $ \paramNode -> do
|
||||
param <- maybeM (throwNoNameError paramNode) (declaredName paramNode)
|
||||
param <$ declare (Declaration param) Default ScopeGraph.Public span ScopeGraph.Parameter Nothing
|
||||
declare (Declaration __self) ScopeGraph.Prelude ScopeGraph.Public emptySpan ScopeGraph.Unknown Nothing
|
||||
for methodParameters $ \paramNode -> declareMaybeName (declaredName paramNode) Default ScopeGraph.Public (getSpan paramNode) ScopeGraph.Parameter Nothing
|
||||
|
||||
addr <- lookupSlot (Declaration name)
|
||||
v <- function name params methodBody associatedScope
|
||||
@ -144,9 +136,8 @@ instance Declarations1 RequiredParameter where
|
||||
-- TODO: Implement Eval instance for RequiredParameter
|
||||
instance Evaluatable RequiredParameter where
|
||||
eval _ _ RequiredParameter{..} = do
|
||||
name <- maybeM (throwNoNameError requiredParameter) (declaredName requiredParameter)
|
||||
span <- ask @Span
|
||||
declare (Declaration name) Default ScopeGraph.Public span ScopeGraph.RequiredParameter Nothing
|
||||
_ <- declareMaybeName (declaredName requiredParameter) Default ScopeGraph.Public span ScopeGraph.RequiredParameter Nothing
|
||||
unit
|
||||
|
||||
|
||||
@ -170,9 +161,8 @@ instance Evaluatable VariableDeclaration where
|
||||
eval _ _ (VariableDeclaration []) = unit
|
||||
eval eval _ (VariableDeclaration decs) = do
|
||||
for_ decs $ \declaration -> do
|
||||
name <- maybeM (throwNoNameError declaration) (declaredName declaration)
|
||||
let declarationSpan = getSpan declaration
|
||||
declare (Declaration name) Default ScopeGraph.Public declarationSpan ScopeGraph.VariableDeclaration Nothing
|
||||
let span = getSpan declaration
|
||||
_ <- declareMaybeName (declaredName declaration) Default ScopeGraph.Public span ScopeGraph.VariableDeclaration Nothing
|
||||
eval declaration
|
||||
unit
|
||||
|
||||
@ -209,10 +199,8 @@ data PublicFieldDefinition a = PublicFieldDefinition
|
||||
instance Evaluatable PublicFieldDefinition where
|
||||
eval eval _ PublicFieldDefinition{..} = do
|
||||
span <- ask @Span
|
||||
propertyName <- maybeM (throwNoNameError publicFieldPropertyName) (declaredName publicFieldPropertyName)
|
||||
|
||||
declare (Declaration propertyName) Instance publicFieldAccessControl span ScopeGraph.PublicField Nothing
|
||||
slot <- lookupSlot (Declaration propertyName)
|
||||
name <- declareMaybeName (declaredName publicFieldPropertyName) Instance publicFieldAccessControl span ScopeGraph.PublicField Nothing
|
||||
slot <- lookupSlot (Declaration name)
|
||||
value <- eval publicFieldValue
|
||||
assign slot value
|
||||
unit
|
||||
@ -236,12 +224,13 @@ instance Diffable Class where
|
||||
|
||||
instance Evaluatable Class where
|
||||
eval eval _ Class{..} = do
|
||||
name <- maybeM (throwNoNameError classIdentifier) (declaredName classIdentifier)
|
||||
span <- ask @Span
|
||||
currentScope' <- currentScope
|
||||
|
||||
superScopes <- for classSuperclasses $ \superclass -> do
|
||||
name <- maybeM (throwNoNameError superclass) (declaredName superclass)
|
||||
name <- case declaredName superclass of
|
||||
Just name -> pure name
|
||||
Nothing -> gensym
|
||||
scope <- associatedScope (Declaration name)
|
||||
slot <- lookupSlot (Declaration name)
|
||||
superclassFrame <- scopedEnvironment =<< deref slot
|
||||
@ -253,7 +242,7 @@ instance Evaluatable Class where
|
||||
current = (Lexical, ) <$> pure (pure currentScope')
|
||||
edges = Map.fromList (superclassEdges <> current)
|
||||
classScope <- newScope edges
|
||||
declare (Declaration name) Default ScopeGraph.Public span ScopeGraph.Class (Just classScope)
|
||||
name <- declareMaybeName (declaredName classIdentifier) Default ScopeGraph.Public span ScopeGraph.Class (Just classScope)
|
||||
|
||||
let frameEdges = Map.singleton Superclass (Map.fromList (catMaybes superScopes))
|
||||
classFrame <- newFrame classScope frameEdges
|
||||
@ -323,13 +312,11 @@ data TypeAlias a = TypeAlias { typeAliasContext :: ![a], typeAliasIdentifier ::
|
||||
|
||||
instance Evaluatable TypeAlias where
|
||||
eval _ _ TypeAlias{..} = do
|
||||
name <- maybeM (throwNoNameError typeAliasIdentifier) (declaredName typeAliasIdentifier)
|
||||
-- This use of `throwNoNameError` is good -- we aren't declaring something new so `declareMaybeName` is not useful here.
|
||||
kindName <- maybeM (throwNoNameError typeAliasKind) (declaredName typeAliasKind)
|
||||
|
||||
span <- ask @Span
|
||||
assocScope <- associatedScope (Declaration kindName)
|
||||
-- TODO: Should we consider a special Relation for `TypeAlias`?
|
||||
declare (Declaration name) Default ScopeGraph.Public span ScopeGraph.TypeAlias assocScope
|
||||
name <- declareMaybeName (declaredName typeAliasIdentifier) Default ScopeGraph.Public span ScopeGraph.TypeAlias assocScope
|
||||
|
||||
slot <- lookupSlot (Declaration name)
|
||||
kindSlot <- lookupSlot (Declaration kindName)
|
||||
|
@ -427,6 +427,7 @@ instance Evaluatable MemberAccess where
|
||||
let lhsAccessControl = fromMaybe Public (termToAccessControl lhs)
|
||||
infos <- declarationsByAccessControl rhsScope lhsAccessControl
|
||||
|
||||
-- This means we always throw an 'AccessControlError' whenever we have a rhs term whose 'declaredName' is 'Nothing'.
|
||||
rhsName <- maybeM (throwNoNameError rhs) (declaredName rhs)
|
||||
rhsValue' <- case find (\Info{..} -> Declaration rhsName == infoDeclaration) infos of
|
||||
Just _ -> pure rhsValue
|
||||
|
@ -121,13 +121,13 @@ data Let a = Let { letVariable :: !a, letValue :: !a, letBody :: !a }
|
||||
|
||||
instance Evaluatable Let where
|
||||
eval eval _ Let{..} = do
|
||||
name <- maybeM (throwNoNameError letVariable) (declaredName letVariable)
|
||||
letSpan <- ask @Span
|
||||
-- This use of 'throwNoNameError' is okay until we have a better way of mapping gensym names to terms in the scope graph.
|
||||
valueName <- maybeM (throwNoNameError letValue) (declaredName letValue)
|
||||
assocScope <- associatedScope (Declaration valueName)
|
||||
|
||||
_ <- withLexicalScopeAndFrame $ do
|
||||
declare (Declaration name) Default Public letSpan ScopeGraph.Let assocScope
|
||||
letSpan <- ask @Span
|
||||
name <- declareMaybeName (declaredName letVariable) Default Public letSpan ScopeGraph.Let assocScope
|
||||
letVal <- eval letValue
|
||||
slot <- lookupSlot (Declaration name)
|
||||
assign slot letVal
|
||||
|
@ -75,11 +75,10 @@ data QualifiedImport a = QualifiedImport { qualifiedImportFrom :: !ImportPath, q
|
||||
instance Evaluatable QualifiedImport where
|
||||
eval _ _ (QualifiedImport importPath aliasTerm) = do
|
||||
paths <- resolveGoImport importPath
|
||||
alias <- maybeM (throwNoNameError aliasTerm) (declaredName aliasTerm)
|
||||
span <- ask @Span
|
||||
scopeAddress <- newScope mempty
|
||||
declare (Declaration alias) Default Public span ScopeGraph.QualifiedImport (Just scopeAddress)
|
||||
aliasSlot <- lookupSlot (Declaration alias)
|
||||
name <- declareMaybeName (declaredName aliasTerm) Default Public span ScopeGraph.QualifiedImport (Just scopeAddress)
|
||||
aliasSlot <- lookupSlot (Declaration name)
|
||||
|
||||
withScope scopeAddress $ do
|
||||
let
|
||||
|
@ -172,6 +172,7 @@ data QualifiedName a = QualifiedName { name :: a, identifier :: a }
|
||||
|
||||
instance Evaluatable QualifiedName where
|
||||
eval _ _ (QualifiedName obj iden) = do
|
||||
-- TODO: Consider gensym'ed names used for References.
|
||||
name <- maybeM (throwNoNameError obj) (declaredName obj)
|
||||
let objSpan = getSpan obj
|
||||
reference (Reference name) objSpan ScopeGraph.Identifier (Declaration name)
|
||||
|
@ -187,6 +187,7 @@ newtype QualifiedImport a = QualifiedImport { qualifiedImportFrom :: NonEmpty a
|
||||
-- import a.b.c
|
||||
instance Evaluatable QualifiedImport where
|
||||
eval _ _ (QualifiedImport qualifiedNames) = do
|
||||
-- TODO: Consider gensym'ed names for imports.
|
||||
qualifiedName <- fmap (T.unpack . formatName) <$> traverse (\term -> maybeM (throwNoNameError term) (declaredName term)) qualifiedNames
|
||||
modulePaths <- resolvePythonModules (QualifiedName qualifiedName)
|
||||
let namesAndPaths = toList (NonEmpty.zip (NonEmpty.zip qualifiedNames (Data.Abstract.Evaluatable.name . T.pack <$> qualifiedName)) modulePaths)
|
||||
|
@ -181,7 +181,9 @@ instance Diffable Class where
|
||||
|
||||
instance Evaluatable Class where
|
||||
eval eval _ Class{..} = do
|
||||
name <- maybeM (throwNoNameError classIdentifier) (declaredName classIdentifier)
|
||||
(name, relation) <- case declaredName classIdentifier of
|
||||
Just name -> pure (name, Default)
|
||||
_ -> gensym >>= \name -> pure (name, Gensym)
|
||||
span <- ask @Span
|
||||
currentScope' <- currentScope
|
||||
|
||||
@ -210,7 +212,7 @@ instance Evaluatable Class where
|
||||
current = (Lexical, ) <$> pure (pure currentScope')
|
||||
edges = Map.fromList (superclassEdges <> current)
|
||||
classScope <- newScope edges
|
||||
declare (Declaration name) Default Public span ScopeGraph.Class (Just classScope)
|
||||
declare (Declaration name) relation Public span ScopeGraph.Class (Just classScope)
|
||||
|
||||
let frameEdges = Map.singleton Superclass (Map.fromList (catMaybes superScopes))
|
||||
childFrame <- newFrame classScope frameEdges
|
||||
@ -241,7 +243,9 @@ data Module a = Module { moduleIdentifier :: !a, moduleStatements :: ![a] }
|
||||
|
||||
instance Evaluatable Module where
|
||||
eval eval _ Module{..} = do
|
||||
name <- maybeM (throwNoNameError moduleIdentifier) (declaredName moduleIdentifier)
|
||||
(name, relation) <- case declaredName moduleIdentifier of
|
||||
Just name -> pure (name, Default)
|
||||
_ -> gensym >>= \name -> pure (name, Gensym)
|
||||
span <- ask @Span
|
||||
currentScope' <- currentScope
|
||||
|
||||
@ -260,7 +264,7 @@ instance Evaluatable Module where
|
||||
Nothing -> do
|
||||
let edges = Map.singleton Lexical [ currentScope' ]
|
||||
classScope <- newScope edges
|
||||
declare (Declaration name) Default Public span ScopeGraph.Module (Just classScope)
|
||||
declare (Declaration name) relation Public span ScopeGraph.Module (Just classScope)
|
||||
|
||||
currentFrame' <- currentFrame
|
||||
let frameEdges = Map.singleton Lexical (Map.singleton currentScope' currentFrame')
|
||||
@ -323,10 +327,12 @@ instance Declarations1 Assignment where
|
||||
|
||||
instance Evaluatable Assignment where
|
||||
eval eval ref Assignment{..} = do
|
||||
lhsName <- maybeM (throwNoNameError assignmentTarget) (declaredName assignmentTarget)
|
||||
(lhsName, relation) <- case declaredName assignmentTarget of
|
||||
Just name -> pure (name, Default)
|
||||
_ -> gensym >>= \name -> pure (name, Gensym)
|
||||
maybeSlot <- maybeLookupDeclaration (Declaration lhsName)
|
||||
assignmentSpan <- ask @Span
|
||||
maybe (declare (Declaration lhsName) Default Public assignmentSpan ScopeGraph.Assignment Nothing) (const (pure ())) maybeSlot
|
||||
maybe (declare (Declaration lhsName) relation Public assignmentSpan ScopeGraph.Assignment Nothing) (const (pure ())) maybeSlot
|
||||
|
||||
lhs <- ref assignmentTarget
|
||||
rhs <- eval assignmentValue
|
||||
|
@ -58,10 +58,8 @@ instance Evaluatable QualifiedAliasedImport where
|
||||
importScope <- newScope (Map.singleton ScopeGraph.Import [ moduleScope ])
|
||||
let scopeMap = Map.singleton moduleScope moduleFrame
|
||||
aliasFrame <- newFrame importScope (Map.singleton ScopeGraph.Import scopeMap)
|
||||
|
||||
alias <- maybeM (throwNoNameError aliasTerm) (declaredName aliasTerm)
|
||||
declare (Declaration alias) Default Public span ScopeGraph.QualifiedAliasedImport (Just importScope)
|
||||
aliasSlot <- lookupSlot (Declaration alias)
|
||||
name <- declareMaybeName (declaredName aliasTerm) Default Public span ScopeGraph.QualifiedAliasedImport (Just importScope)
|
||||
aliasSlot <- lookupSlot (Declaration name)
|
||||
assign aliasSlot =<< object aliasFrame
|
||||
|
||||
unit
|
||||
|
@ -76,9 +76,8 @@ instance Declarations1 RequiredParameter where
|
||||
|
||||
instance Evaluatable RequiredParameter where
|
||||
eval eval ref RequiredParameter{..} = do
|
||||
name <- maybeM (throwNoNameError requiredParameterSubject) (declaredName requiredParameterSubject)
|
||||
span <- ask @Span
|
||||
declare (Declaration name) Default Public span ScopeGraph.RequiredParameter Nothing
|
||||
_ <- declareMaybeName (declaredName requiredParameterSubject) Default Public span ScopeGraph.RequiredParameter Nothing
|
||||
|
||||
lhs <- ref requiredParameterSubject
|
||||
rhs <- eval requiredParameterValue
|
||||
|
@ -193,7 +193,9 @@ declareModule :: ( AbstractValue term address value m
|
||||
-> [term]
|
||||
-> Evaluator term address value m value
|
||||
declareModule eval identifier statements = do
|
||||
name <- maybeM (throwNoNameError identifier) (declaredName identifier)
|
||||
(name, relation) <- case declaredName identifier of
|
||||
Just name -> pure (name, Default)
|
||||
_ -> gensym >>= \name -> pure (name, Gensym)
|
||||
span <- ask @Span
|
||||
currentScope' <- currentScope
|
||||
|
||||
@ -212,7 +214,7 @@ declareModule eval identifier statements = do
|
||||
Nothing -> do
|
||||
let edges = Map.singleton Lexical [ currentScope' ]
|
||||
childScope <- newScope edges
|
||||
declare (Declaration name) Default Public span ScopeGraph.Module (Just childScope)
|
||||
declare (Declaration name) relation Public span ScopeGraph.Module (Just childScope)
|
||||
|
||||
currentFrame' <- currentFrame
|
||||
let frameEdges = Map.singleton Lexical (Map.singleton currentScope' currentFrame')
|
||||
@ -257,7 +259,6 @@ instance Declarations a => Declarations (AbstractClass a) where
|
||||
|
||||
instance Evaluatable AbstractClass where
|
||||
eval eval _ AbstractClass{..} = do
|
||||
name <- maybeM (throwNoNameError abstractClassIdentifier) (declaredName abstractClassIdentifier)
|
||||
span <- ask @Span
|
||||
currentScope' <- currentScope
|
||||
|
||||
@ -274,7 +275,7 @@ instance Evaluatable AbstractClass where
|
||||
current = (Lexical, ) <$> pure (pure currentScope')
|
||||
edges = Map.fromList (superclassEdges <> current)
|
||||
classScope <- newScope edges
|
||||
declare (Declaration name) Default Public span ScopeGraph.AbstractClass (Just classScope)
|
||||
name <- declareMaybeName (declaredName abstractClassIdentifier) Default Public span ScopeGraph.AbstractClass (Just classScope)
|
||||
|
||||
let frameEdges = Map.singleton Superclass (Map.fromList (catMaybes superScopes))
|
||||
childFrame <- newFrame classScope frameEdges
|
||||
|
Loading…
Reference in New Issue
Block a user