1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 10:15:55 +03:00

Keep additional structured info around in Vertex

This commit is contained in:
Timothy Clem 2018-07-17 10:02:40 -07:00
parent cca39728d0
commit 70f01d0806
3 changed files with 35 additions and 18 deletions

View File

@ -24,6 +24,7 @@ import Data.Abstract.Package (PackageInfo(..))
import Data.ByteString.Builder
import Data.Graph
import Data.Graph.Vertex
import Data.Record
import Data.Sum
import qualified Data.Syntax as Syntax
import Data.Term
@ -49,15 +50,15 @@ graphingTerms :: ( Element Syntax.Identifier syntax
, Member (Reader ModuleInfo) effects
, Member (Env (Hole (Located address))) effects
, Member (State (Graph Vertex)) effects
, Base term ~ TermF (Sum syntax) ann
, HasField fields Span
, Base term ~ TermF (Sum syntax) (Record fields)
)
=> SubtermAlgebra (Base term) term (TermEvaluator term (Hole (Located address)) value effects a)
-> SubtermAlgebra (Base term) term (TermEvaluator term (Hole (Located address)) value effects a)
graphingTerms recur term@(In _ syntax) = do
graphingTerms recur term@(In a syntax) = do
case project syntax of
Just (Syntax.Identifier name) -> do
moduleInclusion (Variable (formatName name))
variableDefinition name
variableDefinition name (getField a)
_ -> pure ()
recur term
@ -130,12 +131,15 @@ moduleInclusion v = do
-- | Add an edge from the passed variable name to the module it originated within.
variableDefinition :: ( Member (Env (Hole (Located address))) effects
, Member (State (Graph Vertex)) effects
, Member (Reader ModuleInfo) effects
)
=> Name
-> Span
-> TermEvaluator term (Hole (Located address)) value effects ()
variableDefinition name = do
graph <- maybe lowerBound (maybe lowerBound (vertex . moduleVertex . addressModule) . toMaybe) <$> TermEvaluator (lookupEnv name)
appendGraph (vertex (Variable (formatName name)) `connect` graph)
variableDefinition name span = do
definedInModule <- currentModule
usedInModuleVertex <- maybe lowerBound (maybe lowerBound (vertex . moduleVertex . addressModule) . toMaybe) <$> TermEvaluator (lookupEnv name)
appendGraph $ vertex (variableVertex (formatName name) definedInModule span) `connect` usedInModuleVertex
appendGraph :: (Effectful m, Member (State (Graph v)) effects) => Graph v -> m effects ()
appendGraph = modify' . (<>)

View File

@ -29,6 +29,7 @@ import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Span
import qualified Data.Vector as Vec
import Data.Word
import GHC.Exts (fromList)
@ -158,7 +159,7 @@ importGraphToGraph (ImportGraph vs es) = simplify built
pbToVertex (Vertex t c _) = case t of
MODULE -> V.Module c
PACKAGE -> V.Package c
VARIABLE -> V.Variable c
VARIABLE -> V.Variable c "unknown" emptySpan
-- | For debugging: returns True if all edges reference a valid vertex tag.

View File

@ -3,34 +3,46 @@ module Data.Graph.Vertex
( Vertex (..)
, moduleVertex
, packageVertex
, variableVertex
, vertexName
, vertexToType
) where
import Prologue hiding (packageName)
import Data.Abstract.Module (ModuleInfo (..))
import Data.Abstract.Name
import Data.Abstract.Package (PackageInfo (..))
import Data.Aeson
import Data.Span
import qualified Data.Text as T
import Data.Abstract.Module (ModuleInfo (..))
import Data.Abstract.Name
import Data.Abstract.Package (PackageInfo (..))
import Prologue hiding (packageName)
-- | A vertex of some specific type.
data Vertex
= Package { vertexName :: Text }
| Module { vertexName :: Text }
| Variable { vertexName :: Text }
= Package { packageName :: Text }
| Module { moduleName :: Text }
| Variable { variableName :: Text, variableModuleName :: Text, variableSpan :: Span }
deriving (Eq, Ord, Show, Generic, Hashable)
packageVertex :: PackageInfo -> Vertex
packageVertex = Package . formatName . packageName
packageVertex (PackageInfo name _) = Package (formatName name)
moduleVertex :: ModuleInfo -> Vertex
moduleVertex = Module . T.pack . modulePath
variableVertex :: Text -> ModuleInfo -> Span -> Vertex
variableVertex name ModuleInfo{..} = Variable name (T.pack modulePath)
instance ToJSON Vertex where
toJSON v = object [ "name" .= vertexName v, "type" .= vertexToType v ]
vertexName :: Vertex -> Text
vertexName Package{..} = packageName <> " (P)"
vertexName Module{..} = moduleName <> " (M)"
vertexName Variable{..} = "[" <> variableModuleName <> "]." <> variableName <> " (V" <> " " <> showSpan variableSpan <> ")"
where showSpan (Span (Pos a b) (Pos c d)) = T.pack $ show a <> "," <> show b
<> " - "
<> show c <> "," <> show d
vertexToType :: Vertex -> Text
vertexToType Package{} = "package"
vertexToType Module{} = "module"