Package com.google.javascript.jscomp

# Class SymbolTable

java.lang.Object
com.google.javascript.jscomp.SymbolTable

All Implemented Interfaces:
  `StaticSymbolTable<SymbolTable.Symbol,SymbolTable.Reference>`

public final class SymbolTable extends Object implements StaticSymbolTable<SymbolTable.Symbol,SymbolTable.Reference>
A symbol table for people that want to use Closure Compiler as an indexer. Contains an index of all the symbols in the code within a compilation job. The API is designed for people who want to visit all the symbols, rather than people who want to lookup a specific symbol by a certain key. We can use this to combine different types of symbol tables. For example, one class might have a `StaticSymbolTable` of all variable references, and another class might have a `StaticSymbolTable` of all type names in JSDoc comments. This class allows you to combine them into a unified index. Most passes build their own "partial" symbol table that implements the same interface (StaticSymbolTable, StaticSlot, and friends). Individual compiler passes usually need more or less metadata about the certainty of symbol information. Building a complete symbol table with all the necessary metadata for all passes would be too slow. However, as long as these "partial" symbol tables implement the proper interfaces, we should be able to add them to this symbol table to make it more complete. If clients want fast lookup, they should build their own wrapper around this symbol table that indexes symbols or references by the desired lookup key. By design, when this symbol table creates symbols for types, it tries to mimic the symbol table you would get in an OO language. For example, the "type Foo" and "the constructor that creates objects of type Foo" are the same symbol. The types of "Foo.prototype" and "new Foo()" also have the same symbol. Although JSCompiler internally treats these as distinct symbols, we assume that most clients will not care about the distinction.

See Also:
  - `For more information on how to write plugins for this symbol table.`

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static final class`
`SymbolTable.Reference`
`static final class`
`SymbolTable.Symbol`
`static final class`
`SymbolTable.SymbolScope`

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`GLOBAL_THIS`
The name we use for the JavaScript built-in Global object.

## Method Summary

Modifier and Type
Method
Description
`void`
`addAnonymousFunctions()`
Finds anonymous functions in local scopes, and gives them names and symbols.
`SymbolTable.Symbol`
`declareInferredSymbol(SymbolTable.SymbolScope scope, String name, Node declNode)`
Declare a symbol after the main symbol table was constructed.
`Collection<JSDocInfo>`
`getAllJSDocInfo()`
`Collection<SymbolTable.SymbolScope>`
`getAllScopes()`
Gets all the scopes in this symbol table.
`Iterable<SymbolTable.Symbol>`
`getAllSymbols()`
Returns all variables in this symbol table.
`List<SymbolTable.Symbol>`
`getAllSymbolsForType(JSType type)`
Gets all symbols associated with the given type.
`Iterable<SymbolTable.Symbol>`
`getAllSymbolsForTypeOf(SymbolTable.Symbol sym)`
Get all symbols associated with the type of the given symbol.
`List<SymbolTable.Symbol>`
`getAllSymbolsSorted()`
Get the symbols in their natural ordering.
`SymbolTable.SymbolScope`
`getEnclosingScope(Node n)`
Gets the scope that contains the given node.
`SymbolTable.SymbolScope`
`getGlobalScope()`
Returns the global scope.
`com.google.common.collect.Ordering<SymbolTable.Symbol>`
`getNaturalSymbolOrdering()`
Gets the 'natural' ordering of symbols.
`SymbolTable.Symbol`
`getParameterInFunction(SymbolTable.Symbol sym, String paramName)`
If `sym` is a function, try to find a Symbol for a parameter with the given name.
`List<SymbolTable.Reference>`
`getReferenceList(SymbolTable.Symbol symbol)`
`Iterable<SymbolTable.Reference>`
`getReferences(SymbolTable.Symbol symbol)`
Returns the references that point to the given symbol.
`SymbolTable.SymbolScope`
`getScope(SymbolTable.Symbol slot)`
Returns the scope for a given symbol.
`SymbolTable.Symbol`
`getSymbolDeclaredBy(EnumType enumType)`
Gets the symbol for the given enum.
`SymbolTable.Symbol`
`getSymbolDeclaredBy(FunctionType fn)`
Gets the symbol for the given constructor or interface.
`SymbolTable.Symbol`
`getSymbolForInstancesOf(SymbolTable.Symbol sym)`
Gets the symbol for the prototype if this is the symbol for a constructor or interface.
`SymbolTable.Symbol`
`getSymbolForInstancesOf(FunctionType fn)`
Gets the symbol for the prototype of the given constructor or interface.
`SymbolTable.Symbol`
`getSymbolForScope(SymbolTable.SymbolScope scope)`
All local scopes are associated with a function, and some functions are associated with a symbol.
`String`
`toDebugString()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### GLOBAL_THIS

public static final String GLOBAL_THIS
The name we use for the JavaScript built-in Global object. It's anonymous in JavaScript, so we have to give it an invalid identifier to avoid conflicts with user-defined property names.

See Also:
  - Constant Field Values

## Method Details

### getReferences

public Iterable<SymbolTable.Reference> getReferences(SymbolTable.Symbol symbol)
Description copied from interface: `StaticSymbolTable`
Returns the references that point to the given symbol.

Specified by:
  `getReferences` in interface `StaticSymbolTable<SymbolTable.Symbol,SymbolTable.Reference>`

### getReferenceList

public List<SymbolTable.Reference> getReferenceList(SymbolTable.Symbol symbol)

### getAllSymbols

public Iterable<SymbolTable.Symbol> getAllSymbols()
Description copied from interface: `StaticSymbolTable`
Returns all variables in this symbol table.

Specified by:
  `getAllSymbols` in interface `StaticSymbolTable<SymbolTable.Symbol,SymbolTable.Reference>`

### getAllSymbolsSorted

public List<SymbolTable.Symbol> getAllSymbolsSorted()
Get the symbols in their natural ordering. Always returns a mutable list.

### getNaturalSymbolOrdering

public com.google.common.collect.Ordering<SymbolTable.Symbol> getNaturalSymbolOrdering()
Gets the 'natural' ordering of symbols. Right now, we only guarantee that symbols in the global scope will come before symbols in local scopes. After that, the order is deterministic but undefined.

### getScope

public SymbolTable.SymbolScope getScope(SymbolTable.Symbol slot)
Description copied from interface: `StaticSymbolTable`
Returns the scope for a given symbol.

Specified by:
  `getScope` in interface `StaticSymbolTable<SymbolTable.Symbol,SymbolTable.Reference>`

### getAllJSDocInfo

public Collection<JSDocInfo> getAllJSDocInfo()

### declareInferredSymbol

public SymbolTable.Symbol declareInferredSymbol(SymbolTable.SymbolScope scope, String name, Node declNode)
Declare a symbol after the main symbol table was constructed. Throws an exception if you try to declare a symbol twice.

### getEnclosingScope

public SymbolTable.SymbolScope getEnclosingScope(Node n)
Gets the scope that contains the given node. If `n` is a function name, we return the scope that contains the function, not the function itself.

### getParameterInFunction

public SymbolTable.Symbol getParameterInFunction(SymbolTable.Symbol sym, String paramName)
If `sym` is a function, try to find a Symbol for a parameter with the given name. Returns null if we couldn't find one. Notice that this just makes a best effort, and may not be able to find parameters for non-conventional function definitions. For example, we would not be able to find "y" in this code: `var x = x() ? function(y) {} : function(y) {};`

### getSymbolForScope

public SymbolTable.Symbol getSymbolForScope(SymbolTable.SymbolScope scope)
All local scopes are associated with a function, and some functions are associated with a symbol. Returns the symbol associated with the given scope.

### getAllSymbolsForTypeOf

public Iterable<SymbolTable.Symbol> getAllSymbolsForTypeOf(SymbolTable.Symbol sym)
Get all symbols associated with the type of the given symbol. For example, given a variable x declared as /* @type {Array|Date} / var x = f(); this will return the constructors for Array and Date.

### getGlobalScope

public SymbolTable.SymbolScope getGlobalScope()
Returns the global scope.

### getSymbolDeclaredBy

public SymbolTable.Symbol getSymbolDeclaredBy(FunctionType fn)
Gets the symbol for the given constructor or interface.

### getSymbolDeclaredBy

public SymbolTable.Symbol getSymbolDeclaredBy(EnumType enumType)
Gets the symbol for the given enum.

### getSymbolForInstancesOf

public SymbolTable.Symbol getSymbolForInstancesOf(SymbolTable.Symbol sym)
Gets the symbol for the prototype if this is the symbol for a constructor or interface.

### getSymbolForInstancesOf

public SymbolTable.Symbol getSymbolForInstancesOf(FunctionType fn)
Gets the symbol for the prototype of the given constructor or interface.

### getAllSymbolsForType

public List<SymbolTable.Symbol> getAllSymbolsForType(JSType type)
Gets all symbols associated with the given type. For union types, this may be multiple symbols. For instance types, this will return the constructor of that instance.

### toDebugString

public String toDebugString()

### getAllScopes

public Collection<SymbolTable.SymbolScope> getAllScopes()
Gets all the scopes in this symbol table.

### addAnonymousFunctions

public void addAnonymousFunctions()
Finds anonymous functions in local scopes, and gives them names and symbols. They will show up as local variables with names "function%0", "function%1", etc.