Class CallGraph
- All Implemented Interfaces:
CompilerPass
DefinitionProvider
to compute a call graph for an
AST.
A CallGraph
connects CallGraph.Function
s to CallGraph.Callsite
s and
vice versa: each function in the graph links to the callsites it contains and
each callsite links to the functions it could call. Similarly, each callsite
links to the function that contains it and each function links to the
callsites that could call it.
The callgraph is not precise. That is, a callsite may indicate it can call a function when in fact it does not do so in the running program.
The callgraph is also not complete: in some cases it may be unable to determine some targets of a callsite. In this case, Callsite.hasUnknownTarget() will return true.
The CallGraph doesn't (currently) have functions for externally defined functions; however, callsites that target externs will have hasExternTarget() return true.
TODO(dcc): Have CallGraph (optionally?) include functions for externs.
-
Nested Class Summary
Modifier and TypeClassDescriptionclass
An inner class that represents call sites in the call graph.class
An inner class that represents functions in the call graph. -
Field Summary
-
Constructor Summary
ConstructorDescriptionCallGraph
(AbstractCompiler compiler) Creates a call graph object support both forward and backward lookups.CallGraph
(AbstractCompiler compiler, boolean computeForwardGraph, boolean computeBackwardGraph) Creates a call graph object supporting the specified lookups. -
Method Summary
Modifier and TypeMethodDescriptionReturns a collection of all callsites in the call graph.Returns a collection of all functions (including the main function) in the call graph.Constructs and returns a directed graph where the nodes are functions and the edges are callsites connecting callees to callers.getCallsiteForAstNode
(Node callsiteNode) Returns the call graph Callsite object corresponding to the provided AST Token.CALL or Token.NEW node, or null if no such object exists.Constructs and returns a directed graph where the nodes are functions and the edges are callsites connecting callers to callees.getFunctionForAstNode
(Node functionNode) Returns the call graph Function object corresponding to the provided AST Token.FUNCTION node, or null if no such object exists.Returns a Function object representing the "main" global function.getUniqueFunctionWithName
(String desiredName) Finds a function with the given name.void
Builds a call graph for the given externsRoot and jsRoot.
-
Field Details
-
MAIN_FUNCTION_NAME
The name we give the main function.- See Also:
-
-
Constructor Details
-
CallGraph
public CallGraph(AbstractCompiler compiler, boolean computeForwardGraph, boolean computeBackwardGraph) Creates a call graph object supporting the specified lookups. At least one (and possibly both) of computeForwardGraph and computeBackwardGraph must be true.- Parameters:
compiler
- The compilercomputeForwardGraph
- Should the call graph allow lookup of the target functions a given callsite could call?computeBackwardGraph
- Should the call graph allow lookup of the callsites that could call a given function?
-
CallGraph
Creates a call graph object support both forward and backward lookups.
-
-
Method Details
-
process
Builds a call graph for the given externsRoot and jsRoot. This method must not be called more than once per CallGraph instance.- Specified by:
process
in interfaceCompilerPass
- Parameters:
externsRoot
- Top of external JS treejsRoot
- Top of JS tree
-
getFunctionForAstNode
Returns the call graph Function object corresponding to the provided AST Token.FUNCTION node, or null if no such object exists. -
getMainFunction
Returns a Function object representing the "main" global function. -
getAllFunctions
Returns a collection of all functions (including the main function) in the call graph. -
getUniqueFunctionWithName
Finds a function with the given name. Throws an exception if there are no functions or multiple functions with the name. This is for testing purposes only. -
getCallsiteForAstNode
Returns the call graph Callsite object corresponding to the provided AST Token.CALL or Token.NEW node, or null if no such object exists. -
getAllCallsites
Returns a collection of all callsites in the call graph. -
getForwardDirectedGraph
Constructs and returns a directed graph where the nodes are functions and the edges are callsites connecting callers to callees. It is safe to call this method on both forward and backwardly constructed CallGraphs. -
getBackwardDirectedGraph
Constructs and returns a directed graph where the nodes are functions and the edges are callsites connecting callees to callers. It is safe to call this method on both forward and backwardly constructed CallGraphs.
-