public class TypedScope extends Scope implements StaticTypedScope<JSType>
TypedScope is also used as a lattice element for flow-sensitive type inference.
As a lattice element, a scope is viewed as a map from names to types. A name
not in the map is considered to have the bottom type. The join of two maps m1
and m2 is the map of the union of names with JSType.getLeastSupertype(com.google.javascript.rhino.jstype.JSType)
to meet the m1 type and m2 type.
| Modifier and Type | Class and Description |
|---|---|
(package private) static interface |
TypedScope.TypeResolver |
| Modifier and Type | Field and Description |
|---|---|
private static com.google.common.base.Predicate<TypedVar> |
DECLARATIVELY_UNBOUND_VARS_WITHOUT_TYPES |
private boolean |
isBottom
Whether this is a bottom scope for the purposes of type inference.
|
private TypedScope |
parent |
private TypedScope.TypeResolver |
typeResolver |
private java.util.Map<java.lang.String,TypedVar> |
vars |
| Modifier | Constructor and Description |
|---|---|
private |
TypedScope(Node rootNode,
boolean isBottom)
Creates a empty Scope (bottom of the lattice).
|
(package private) |
TypedScope(TypedScope parent,
Node rootNode) |
| Modifier and Type | Method and Description |
|---|---|
(package private) static TypedScope |
createGlobalScope(Node rootNode) |
(package private) static TypedScope |
createLatticeBottom(Node rootNode) |
(package private) Var |
declare(java.lang.String name,
Node nameNode,
CompilerInput input)
Declares a variable.
|
(package private) TypedVar |
declare(java.lang.String name,
Node nameNode,
JSType type,
CompilerInput input) |
(package private) TypedVar |
declare(java.lang.String name,
Node nameNode,
JSType type,
CompilerInput input,
boolean inferred) |
java.lang.Iterable<TypedVar> |
getAllSymbols() |
Var |
getArgumentsVar()
Get a unique VAR object to represents "arguments" within this scope
|
Scope |
getClosestHoistScope()
If a var were declared in this scope, return the scope it would be hoisted to.
|
java.util.Iterator<TypedVar> |
getDeclarativelyUnboundVarsWithoutTypes() |
int |
getDepth()
The depth of the scope.
|
(package private) TypedScope |
getGlobalScope() |
TypedVar |
getOwnSlot(java.lang.String name)
Like
getSlot but does not recurse into parent scopes. |
TypedScope |
getParent() |
StaticTypedScope<JSType> |
getParentScope()
Returns the scope enclosing this one or null if none.
|
Node |
getRootNode()
Gets the container node of the scope.
|
TypedVar |
getSlot(java.lang.String name)
Returns any defined slot within this scope for this name.
|
JSType |
getTypeOfThis()
Gets the type of
this in the current scope. |
TypedVar |
getVar(java.lang.String name)
Returns the variable, may be null
|
int |
getVarCount()
Returns number of variables in this scope (excluding the special 'arguments' variable)
|
(package private) java.lang.Iterable<Var> |
getVarIterable()
Return an iterable over all of the variables declared in this scope
(except the special 'arguments' variable).
|
java.util.Iterator<TypedVar> |
getVars()
Return an iterator over all of the variables declared in this scope.
|
boolean |
isBlockScope() |
(package private) boolean |
isBottom()
Whether this is the bottom of the lattice.
|
boolean |
isDeclared(java.lang.String name,
boolean recurse)
Returns true if a variable is declared.
|
boolean |
isFunctionBlockScope() |
boolean |
isGlobal()
Returns whether this is the global scope.
|
boolean |
isLocal()
Returns whether this is a local scope (i.e.
|
(package private) void |
resolveTypes()
Resolve all type references.
|
(package private) void |
setTypeResolver(TypedScope.TypeResolver resolver) |
(package private) void |
undeclare(Var var)
Undeclares a variable, to be used when the compiler optimizes out
a variable and removes it from the scope.
|
isFunctionScope, toStringprivate final java.util.Map<java.lang.String,TypedVar> vars
private final TypedScope parent
private final boolean isBottom
private static final com.google.common.base.Predicate<TypedVar> DECLARATIVELY_UNBOUND_VARS_WITHOUT_TYPES
private TypedScope.TypeResolver typeResolver
TypedScope(TypedScope parent, Node rootNode)
private TypedScope(Node rootNode, boolean isBottom)
rootNode - Typically a FUNCTION node or the global BLOCK node.isBottom - Whether this is the bottom of a lattice. Otherwise,
it must be a global scope.static TypedScope createGlobalScope(Node rootNode)
static TypedScope createLatticeBottom(Node rootNode)
boolean isBottom()
public int getDepth()
Scopepublic Node getRootNode()
ScopegetRootNode in interface StaticScopegetRootNode in class Scopepublic TypedScope getParent()
TypedScope getGlobalScope()
getGlobalScope in class Scopepublic StaticTypedScope<JSType> getParentScope()
StaticScopegetParentScope in interface StaticTypedScope<JSType>getParentScope in interface StaticScopegetParentScope in class Scopepublic JSType getTypeOfThis()
this in the current scope.getTypeOfThis in interface StaticTypedScope<JSType>Var declare(java.lang.String name, Node nameNode, CompilerInput input)
ScopeTypedVar declare(java.lang.String name, Node nameNode, JSType type, CompilerInput input)
TypedVar declare(java.lang.String name, Node nameNode, JSType type, CompilerInput input, boolean inferred)
void undeclare(Var var)
Scopepublic TypedVar getSlot(java.lang.String name)
StaticScopegetSlot in interface StaticTypedScope<JSType>getSlot in interface StaticScopegetSlot in class Scopename - The name of the variable slot to look up.null if no
definition exists.public TypedVar getOwnSlot(java.lang.String name)
StaticScopegetSlot but does not recurse into parent scopes.getOwnSlot in interface StaticTypedScope<JSType>getOwnSlot in interface StaticScopegetOwnSlot in class Scopepublic TypedVar getVar(java.lang.String name)
Scopepublic Var getArgumentsVar()
ScopegetArgumentsVar in class Scopepublic boolean isDeclared(java.lang.String name,
boolean recurse)
ScopeisDeclared in class Scopepublic java.util.Iterator<TypedVar> getVars()
Scopejava.lang.Iterable<Var> getVarIterable()
ScopegetVarIterable in class Scopepublic java.lang.Iterable<TypedVar> getAllSymbols()
getAllSymbols in class Scopepublic int getVarCount()
ScopegetVarCount in class Scopepublic boolean isGlobal()
Scopepublic boolean isLocal()
Scopepublic java.util.Iterator<TypedVar> getDeclarativelyUnboundVarsWithoutTypes()
void resolveTypes()
void setTypeResolver(TypedScope.TypeResolver resolver)
public boolean isBlockScope()
isBlockScope in class Scopepublic boolean isFunctionBlockScope()
isFunctionBlockScope in class Scopepublic Scope getClosestHoistScope()
ScopegetClosestHoistScope in class Scope