Class TypedScope
- java.lang.Object
-
- com.google.javascript.jscomp.AbstractScope<TypedScope,TypedVar>
-
- com.google.javascript.jscomp.TypedScope
-
- All Implemented Interfaces:
StaticTypedScope,StaticScope,java.io.Serializable
public class TypedScope extends AbstractScope<TypedScope,TypedVar> implements StaticTypedScope
TypedScope contains information about variables and their types. Scopes can be nested; a scope points back to its parent scope.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.TypedScopes also have a concept of a set of 'reserved' names. These are names that will be declared as actual TypedVars later in the compilation. Looking up any of these reserved names will always return null, even if the name is available in a parent scope.
- See Also:
NodeTraversal,Several methods in this class, such as throw an exception when called. The reason for this is that we want to shadow methods from the parent class, to avoid calling them accidentally., Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Iterable<TypedVar>getDeclarativelyUnboundVarsWithoutTypes()Returns the variables in this scope that have been declared with 'var' and not yet declared with a known type.intgetDepth()The depth of the scope.TypedScopegetParent()Returns the parent scope, or null if this is the global scope.StaticScopegetTopmostScopeOfEventualDeclaration(java.lang.String name)Returns the topmost slot containing this name, or null if no slots do.JSTypegetTypeOfThis()Gets the type ofthisin the current scope.TypedVargetVar(java.lang.String name)Returns the slot forname, considering shadowing of qualified names.protected booleanhasOwnImplicitSlot(com.google.javascript.jscomp.AbstractScope.ImplicitVar name)Returns true iff this scope implies a slot with the given name.TypedScopetyped()-
Methods inherited from class com.google.javascript.jscomp.AbstractScope
getAllAccessibleVariables, getAllSymbols, getArgumentsVar, getClosestContainerScope, getClosestHoistScope, getGlobalScope, getOwnSlot, getParentScope, getRootNode, getSlot, getVarCount, getVarIterable, hasOwnSlot, hasSlot, isBlockScope, isCatchScope, isFunctionBlockScope, isFunctionScope, isGlobal, isLocal, isModuleScope, toString, untyped
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.google.javascript.rhino.StaticScope
getRootNode
-
Methods inherited from interface com.google.javascript.rhino.jstype.StaticTypedScope
getOwnSlot, getParentScope, getSlot, lookupQualifiedName
-
-
-
-
Method Detail
-
typed
public TypedScope typed()
- Overrides:
typedin classAbstractScope<TypedScope,TypedVar>
-
getDepth
public int getDepth()
Description copied from class:AbstractScopeThe depth of the scope. The global scope has depth 0.- Specified by:
getDepthin classAbstractScope<TypedScope,TypedVar>
-
getParent
public TypedScope getParent()
Description copied from class:AbstractScopeReturns the parent scope, or null if this is the global scope.- Specified by:
getParentin classAbstractScope<TypedScope,TypedVar>
-
getTypeOfThis
public JSType getTypeOfThis()
Gets the type ofthisin the current scope.- Specified by:
getTypeOfThisin interfaceStaticTypedScope
-
hasOwnImplicitSlot
protected boolean hasOwnImplicitSlot(@Nullable com.google.javascript.jscomp.AbstractScope.ImplicitVar name)Description copied from class:AbstractScopeReturns true iff this scope implies a slot with the given name.- Overrides:
hasOwnImplicitSlotin classAbstractScope<TypedScope,TypedVar>
-
getDeclarativelyUnboundVarsWithoutTypes
public java.lang.Iterable<TypedVar> getDeclarativelyUnboundVarsWithoutTypes()
Returns the variables in this scope that have been declared with 'var' and not yet declared with a known type.These variables can safely be set to undefined (rather than unknown) at the start of type inference, and will be reset to the correct type when analyzing the first assignment to them. Parameters and externs are excluded because they are not initialized in the function body, and lexically-bound variables (let and const) are excluded because they are initialized when inferring the LET/CONST node, which is guaranteed to occur before any use, since they are not hoisted.
-
getVar
public final TypedVar getVar(java.lang.String name)
Returns the slot forname, considering shadowing of qualified names.The superclass method does not handle shadowing.
Lookup of qualified names (i.e. names with dots) executes against scopes in the following precedence:
- This
Scope. - The first ancestor
Scope, if any, that declares the root of the qualified name. - The global
Scope.
An example of where this is necessary: say the global scope contains "a" and "a.b" and a function scope contains "a". When looking up "a.b" in the function scope, AbstractScope::getVar returns "a.b". This method returns null because the global "a" is shadowed.
- Overrides:
getVarin classAbstractScope<TypedScope,TypedVar>
- This
-
getTopmostScopeOfEventualDeclaration
public StaticScope getTopmostScopeOfEventualDeclaration(java.lang.String name)
Description copied from interface:StaticScopeReturns the topmost slot containing this name, or null if no slots do. May return true in cases whereStaticScope.getSlot(java.lang.String)returns null, though. Do not rely on this method if you need an actual slot.This method is intended for use while scopes are still being built, hence the name 'eventual' declaration. Once scope building is complete, the scope returned from this method must be equivalent to "getSlot(name).getScope()" or null
- Specified by:
getTopmostScopeOfEventualDeclarationin interfaceStaticScope
-
-