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, toString
private 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()
Scope
public Node getRootNode()
Scope
getRootNode
in interface StaticScope
getRootNode
in class Scope
public TypedScope getParent()
TypedScope getGlobalScope()
getGlobalScope
in class Scope
public StaticTypedScope<JSType> getParentScope()
StaticScope
getParentScope
in interface StaticTypedScope<JSType>
getParentScope
in interface StaticScope
getParentScope
in class Scope
public JSType getTypeOfThis()
this
in the current scope.getTypeOfThis
in interface StaticTypedScope<JSType>
Var declare(java.lang.String name, Node nameNode, CompilerInput input)
Scope
TypedVar 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)
Scope
public TypedVar getSlot(java.lang.String name)
StaticScope
getSlot
in interface StaticTypedScope<JSType>
getSlot
in interface StaticScope
getSlot
in class Scope
name
- The name of the variable slot to look up.null
if no
definition exists.public TypedVar getOwnSlot(java.lang.String name)
StaticScope
getSlot
but does not recurse into parent scopes.getOwnSlot
in interface StaticTypedScope<JSType>
getOwnSlot
in interface StaticScope
getOwnSlot
in class Scope
public TypedVar getVar(java.lang.String name)
Scope
public Var getArgumentsVar()
Scope
getArgumentsVar
in class Scope
public boolean isDeclared(java.lang.String name, boolean recurse)
Scope
isDeclared
in class Scope
public java.util.Iterator<TypedVar> getVars()
Scope
java.lang.Iterable<Var> getVarIterable()
Scope
getVarIterable
in class Scope
public java.lang.Iterable<TypedVar> getAllSymbols()
getAllSymbols
in class Scope
public int getVarCount()
Scope
getVarCount
in class Scope
public boolean isGlobal()
Scope
public boolean isLocal()
Scope
public java.util.Iterator<TypedVar> getDeclarativelyUnboundVarsWithoutTypes()
void resolveTypes()
void setTypeResolver(TypedScope.TypeResolver resolver)
public boolean isBlockScope()
isBlockScope
in class Scope
public boolean isFunctionBlockScope()
isFunctionBlockScope
in class Scope
public Scope getClosestHoistScope()
Scope
getClosestHoistScope
in class Scope