1
1
mirror of https://github.com/github/semantic.git synced 2024-12-30 02:14:20 +03:00

Replace Maybe Kind with an Unknown case in Kind

This commit is contained in:
joshvera 2018-12-19 16:44:29 -05:00
parent 85b6e1e9f6
commit 72a8382104
13 changed files with 33 additions and 32 deletions

View File

@ -55,7 +55,7 @@ import qualified Data.Abstract.Heap as Heap
import Data.Abstract.Live
import Data.Abstract.Module (ModuleInfo)
import Data.Abstract.Name
import Data.Abstract.ScopeGraph (Path (..), Relation(..), putDeclarationScopeAtPosition)
import Data.Abstract.ScopeGraph (Kind(..), Path (..), Relation(..), putDeclarationScopeAtPosition)
import qualified Data.Map.Strict as Map
import Data.Span (Span, emptySpan)
import Prologue
@ -179,7 +179,7 @@ define :: ( HasCallStack
-> Evaluator term address value m ()
define declaration rel def = withCurrentCallStack callStack $ do
-- TODO: This span is still wrong.
declare declaration rel emptySpan Nothing Nothing
declare declaration rel emptySpan Unknown Nothing
slot <- lookupDeclaration declaration
value <- def
assign slot value

View File

@ -11,6 +11,7 @@ import Control.Abstract.Heap
import Control.Abstract.ScopeGraph
import Control.Abstract.Value
import Data.Abstract.BaseError
import qualified Data.Abstract.ScopeGraph as ScopeGraph
import Data.Abstract.Name
import Data.Map.Strict as Map
import Data.Span
@ -41,11 +42,11 @@ defineBuiltIn declaration rel value = withCurrentCallStack callStack $ do
let lexicalEdges = Map.singleton Lexical [ currentScope' ]
associatedScope <- newPreludeScope lexicalEdges
-- TODO: This span is still wrong.
declare declaration rel emptySpan Nothing (Just associatedScope)
declare declaration rel emptySpan ScopeGraph.Unknown (Just associatedScope)
param <- gensym
withScope associatedScope $ do
declare (Declaration param) rel emptySpan Nothing Nothing
declare (Declaration param) rel emptySpan ScopeGraph.Unknown Nothing
slot <- lookupDeclaration declaration
value <- builtIn associatedScope value

View File

@ -62,7 +62,7 @@ declare :: ( Carrier sig m
=> Declaration
-> Relation
-> Span
-> Maybe Kind
-> Kind
-> Maybe address
-> Evaluator term address value m ()
declare decl rel span kind scope = do

View File

@ -27,7 +27,7 @@ import Data.Abstract.Declarations as X
import Data.Abstract.FreeVariables as X
import Data.Abstract.Module
import Data.Abstract.Name as X
import Data.Abstract.ScopeGraph (Relation(..))
import qualified Data.Abstract.ScopeGraph as ScopeGraph
import Data.Language
import Data.Scientific (Scientific)
import Data.Semigroup.App
@ -174,7 +174,7 @@ defineSelf :: ( AbstractValue term address value m
=> Evaluator term address value m ()
defineSelf = do
let self = Declaration X.__self
declare self Prelude emptySpan Nothing Nothing
declare self Prelude emptySpan ScopeGraph.Unknown Nothing
slot <- lookupDeclaration self
assign slot =<< object =<< currentFrame

View File

@ -54,11 +54,11 @@ data Info scopeAddress = Info
{ infoDeclaration :: Declaration
, infoRelation :: Relation
, infoSpan :: Span
, infoKind :: Maybe Kind
, infoKind :: Kind
, infoAssociatedScope :: Maybe scopeAddress
} deriving (Eq, Show, Ord, Generic, NFData)
data Kind = TypeAlias | Class | Method | QualifiedAliasedImport | DefaultExport | Module | AbstractClass | Let | QualifiedImport | UnqualifiedImport | Assignment | RequiredParameter | PublicField | VariableDeclaration | Function | Parameter
data Kind = TypeAlias | Class | Method | QualifiedAliasedImport | DefaultExport | Module | AbstractClass | Let | QualifiedImport | UnqualifiedImport | Assignment | RequiredParameter | PublicField | VariableDeclaration | Function | Parameter | Unknown
deriving (Eq, Show, Ord, Generic, NFData)
-- Offsets and frame addresses in the heap should be addresses?
@ -146,7 +146,7 @@ lookupScope scope = Map.lookup scope . unScopeGraph
-- Declare a declaration with a span and an associated scope in the scope graph.
-- TODO: Return the whole value in Maybe or Either.
declare :: Ord scope => Declaration -> Relation -> Span -> Maybe Kind -> Maybe scope -> scope -> ScopeGraph scope -> (ScopeGraph scope, Maybe Position)
declare :: Ord scope => Declaration -> Relation -> Span -> Kind -> Maybe scope -> scope -> ScopeGraph scope -> (ScopeGraph scope, Maybe Position)
declare decl rel declSpan kind assocScope currentScope g = fromMaybe (g, Nothing) $ do
scope <- lookupScope currentScope g

View File

@ -39,7 +39,7 @@ instance Evaluatable Function where
param <- maybeM (throwNoNameError paramNode) (declaredName paramNode)
-- TODO: This might be a motivation for a typeclass for `declarationKind` since we
-- sometimes create declarations for our children.
param <$ declare (Declaration param) Default span (Just ScopeGraph.Parameter) Nothing
param <$ declare (Declaration param) Default span ScopeGraph.Parameter Nothing
addr <- lookupDeclaration (Declaration name)
v <- function name params functionBody associatedScope
@ -60,7 +60,7 @@ declareFunction name span kind = do
currentScope' <- currentScope
let lexicalEdges = Map.singleton Lexical [ currentScope' ]
associatedScope <- newScope lexicalEdges
declare (Declaration name) Default span (Just kind) (Just associatedScope)
declare (Declaration name) Default span kind (Just associatedScope)
pure associatedScope
instance Tokenize Function where
@ -96,10 +96,10 @@ instance Evaluatable Method where
params <- withScope associatedScope $ do
-- TODO: Should we give `self` a special Relation?
declare (Declaration __self) Prelude emptySpan Nothing Nothing
declare (Declaration __self) Prelude emptySpan ScopeGraph.Unknown Nothing
for methodParameters $ \paramNode -> do
param <- maybeM (throwNoNameError paramNode) (declaredName paramNode)
param <$ declare (Declaration param) Default span (Just ScopeGraph.Parameter) Nothing
param <$ declare (Declaration param) Default span ScopeGraph.Parameter Nothing
addr <- lookupDeclaration (Declaration name)
v <- function name params methodBody associatedScope
@ -168,7 +168,7 @@ instance Evaluatable VariableDeclaration where
eval eval _ (VariableDeclaration decs) = do
for_ decs $ \declaration -> do
name <- maybeM (throwNoNameError declaration) (declaredName declaration)
declare (Declaration name) Default emptySpan (Just ScopeGraph.VariableDeclaration) Nothing
declare (Declaration name) Default emptySpan ScopeGraph.VariableDeclaration Nothing
(span, _) <- do
ref <- eval declaration
subtermSpan <- get @Span
@ -213,7 +213,7 @@ instance Evaluatable PublicFieldDefinition where
span <- ask @Span
propertyName <- maybeM (throwNoNameError publicFieldPropertyName) (declaredName publicFieldPropertyName)
declare (Declaration propertyName) Instance span (Just ScopeGraph.PublicField) Nothing
declare (Declaration propertyName) Instance span ScopeGraph.PublicField Nothing
slot <- lookupDeclaration (Declaration propertyName)
value <- eval publicFieldValue
assign slot value
@ -261,7 +261,7 @@ instance Evaluatable Class where
current = (Lexical, ) <$> pure (pure currentScope')
edges = Map.fromList (superclassEdges <> current)
classScope <- newScope edges
declare (Declaration name) Default span (Just ScopeGraph.Class) (Just classScope)
declare (Declaration name) Default span ScopeGraph.Class (Just classScope)
let frameEdges = Map.singleton Superclass (Map.fromList (catMaybes superScopes))
classFrame <- newFrame classScope frameEdges
@ -355,7 +355,7 @@ instance Evaluatable TypeAlias where
span <- ask @Span
assocScope <- associatedScope (Declaration kindName)
-- TODO: Should we consider a special Relation for `TypeAlias`?
declare (Declaration name) Default span (Just ScopeGraph.TypeAlias) assocScope
declare (Declaration name) Default span ScopeGraph.TypeAlias assocScope
slot <- lookupDeclaration (Declaration name)
kindSlot <- lookupDeclaration (Declaration kindName)

View File

@ -152,7 +152,7 @@ instance Evaluatable Let where
assocScope <- associatedScope (Declaration valueName)
_ <- withLexicalScopeAndFrame $ do
declare (Declaration name) Default letSpan (Just ScopeGraph.Let) assocScope
declare (Declaration name) Default letSpan ScopeGraph.Let assocScope
letVal <- eval letValue
slot <- lookupDeclaration (Declaration name)
assign slot letVal

View File

@ -85,7 +85,7 @@ instance Evaluatable QualifiedImport where
alias <- maybeM (throwNoNameError aliasTerm) (declaredName aliasTerm)
span <- ask @Span
scopeAddress <- newScope mempty
declare (Declaration alias) Default span (Just ScopeGraph.QualifiedImport) (Just scopeAddress)
declare (Declaration alias) Default span ScopeGraph.QualifiedImport (Just scopeAddress)
aliasSlot <- lookupDeclaration (Declaration alias)
withScope scopeAddress $ do

View File

@ -159,7 +159,7 @@ instance Evaluatable Import where
aliasFrame <- newFrame importScope (Map.singleton ScopeGraph.Import scopeMap)
-- Add declaration of the alias name to the current scope (within our current module).
declare (Declaration aliasName) Default span (Just ScopeGraph.UnqualifiedImport) (Just importScope)
declare (Declaration aliasName) Default span ScopeGraph.UnqualifiedImport (Just importScope)
-- Retrieve the frame slot for the new declaration.
aliasSlot <- lookupDeclaration (Declaration aliasName)
assign aliasSlot =<< object aliasFrame
@ -230,7 +230,7 @@ instance Evaluatable QualifiedImport where
go ((name, modulePath) : namesAndPaths) = do
span <- ask @Span
scopeAddress <- newScope mempty
declare (Declaration name) Default span (Just ScopeGraph.QualifiedImport) (Just scopeAddress)
declare (Declaration name) Default span ScopeGraph.QualifiedImport (Just scopeAddress)
aliasSlot <- lookupDeclaration (Declaration name)
-- a.b.c
withScope scopeAddress $
@ -266,7 +266,7 @@ instance Evaluatable QualifiedAliasedImport where
span <- ask @Span
scopeAddress <- newScope mempty
alias <- maybeM (throwNoNameError aliasTerm) (declaredName aliasTerm)
declare (Declaration alias) Default span (Just ScopeGraph.QualifiedAliasedImport) (Just scopeAddress)
declare (Declaration alias) Default span ScopeGraph.QualifiedAliasedImport (Just scopeAddress)
objFrame <- newFrame scopeAddress mempty
val <- object objFrame
aliasSlot <- lookupDeclaration (Declaration alias)

View File

@ -222,7 +222,7 @@ instance Evaluatable Class where
current = (Lexical, ) <$> pure (pure currentScope')
edges = Map.fromList (superclassEdges <> current)
classScope <- newScope edges
declare (Declaration name) Default span (Just ScopeGraph.Class) (Just classScope)
declare (Declaration name) Default span ScopeGraph.Class (Just classScope)
let frameEdges = Map.singleton Superclass (Map.fromList (catMaybes superScopes))
childFrame <- newFrame classScope frameEdges
@ -275,7 +275,7 @@ instance Evaluatable Module where
Nothing -> do
let edges = Map.singleton Lexical [ currentScope' ]
classScope <- newScope edges
declare (Declaration name) Default span (Just ScopeGraph.Module) (Just classScope)
declare (Declaration name) Default span ScopeGraph.Module (Just classScope)
currentFrame' <- currentFrame
let frameEdges = Map.singleton Lexical (Map.singleton currentScope' currentFrame')
@ -351,7 +351,7 @@ instance Evaluatable Assignment where
lhsName <- maybeM (throwNoNameError assignmentTarget) (declaredName assignmentTarget)
maybeSlot <- maybeLookupDeclaration (Declaration lhsName)
assignmentSpan <- ask @Span
maybe (declare (Declaration lhsName) Default assignmentSpan (Just ScopeGraph.Assignment) Nothing) (const (pure ())) maybeSlot
maybe (declare (Declaration lhsName) Default assignmentSpan ScopeGraph.Assignment Nothing) (const (pure ())) maybeSlot
lhs <- ref assignmentTarget
rhs <- eval assignmentValue

View File

@ -99,7 +99,7 @@ instance Evaluatable RequiredParameter where
eval eval ref RequiredParameter{..} = do
name <- maybeM (throwNoNameError requiredParameterSubject) (declaredName requiredParameterSubject)
span <- ask @Span
declare (Declaration name) Default span (Just ScopeGraph.RequiredParameter) Nothing
declare (Declaration name) Default span ScopeGraph.RequiredParameter Nothing
lhs <- ref requiredParameterSubject
rhs <- eval requiredParameterValue

View File

@ -31,7 +31,7 @@ instance Evaluatable JavaScriptRequire where
Just alias -> do
span <- ask @Span
importScope <- newScope (Map.singleton ScopeGraph.Import [ moduleScope ])
declare (Declaration alias) Default span (Just ScopeGraph.UnqualifiedImport) (Just importScope)
declare (Declaration alias) Default span ScopeGraph.UnqualifiedImport (Just importScope)
let scopeMap = Map.singleton moduleScope moduleFrame
aliasFrame <- newFrame importScope (Map.singleton ScopeGraph.Import scopeMap)
aliasSlot <- lookupDeclaration (Declaration alias)

View File

@ -68,7 +68,7 @@ instance Evaluatable QualifiedAliasedImport where
aliasFrame <- newFrame importScope (Map.singleton ScopeGraph.Import scopeMap)
alias <- maybeM (throwNoNameError aliasTerm) (declaredName aliasTerm)
declare (Declaration alias) Default span (Just ScopeGraph.QualifiedAliasedImport) (Just importScope)
declare (Declaration alias) Default span ScopeGraph.QualifiedAliasedImport (Just importScope)
aliasSlot <- lookupDeclaration (Declaration alias)
assign aliasSlot =<< object aliasFrame
@ -159,7 +159,7 @@ instance Evaluatable DefaultExport where
withScopeAndFrame exportFrame $ do
valueRef <- eval term
let declaration = Declaration $ Name.name "__default"
declare declaration Default exportSpan (Just ScopeGraph.DefaultExport) Nothing
declare declaration Default exportSpan ScopeGraph.DefaultExport Nothing
defaultSlot <- lookupDeclaration declaration
assign defaultSlot valueRef
@ -598,7 +598,7 @@ declareModule eval identifier statements = do
Nothing -> do
let edges = Map.singleton Lexical [ currentScope' ]
childScope <- newScope edges
declare (Declaration name) Default span (Just ScopeGraph.Module) (Just childScope)
declare (Declaration name) Default span ScopeGraph.Module (Just childScope)
currentFrame' <- currentFrame
let frameEdges = Map.singleton Lexical (Map.singleton currentScope' currentFrame')
@ -676,7 +676,7 @@ instance Evaluatable AbstractClass where
current = (Lexical, ) <$> pure (pure currentScope')
edges = Map.fromList (superclassEdges <> current)
classScope <- newScope edges
declare (Declaration name) Default span (Just ScopeGraph.AbstractClass) (Just classScope)
declare (Declaration name) Default span ScopeGraph.AbstractClass (Just classScope)
let frameEdges = Map.singleton Superclass (Map.fromList (catMaybes superScopes))
childFrame <- newFrame classScope frameEdges