Package org.jruby.parser
Class StaticScope
java.lang.Object
org.jruby.parser.StaticScope
- All Implemented Interfaces:
Serializable
,Cloneable
StaticScope represents lexical scoping of variables and module/class constants.
At a very high level every scopes enclosing scope contains variables in the next outer
lexical layer. The enclosing scopes variables may or may not be reachable depending
on the scoping rules for variables (governed by BlockStaticScope and LocalStaticScope).
StaticScope also keeps track of current module/class that is in scope. previousCRefScope
will point to the previous scope of the enclosing module/class (cref).
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
StaticScope
(StaticScope.Type type, StaticScope enclosingScope) Construct a new static scope.protected
StaticScope
(StaticScope.Type type, StaticScope enclosingScope, String file) protected
StaticScope
(StaticScope.Type type, StaticScope enclosingScope, String[] names) protected
StaticScope
(StaticScope.Type type, StaticScope enclosingScope, String[] names, int firstKeywordIndex) Construct a new static scope.protected
StaticScope
(StaticScope.Type type, StaticScope enclosingScope, String file, String[] names, int firstKeywordIndex) -
Method Summary
Modifier and TypeMethodDescriptionaddAssign
(int line, RubySymbol symbolID, Node value) int
addVariable
(String name) Add a new variable to this (current) scope unless it is already defined in any reachable scope.int
addVariableThisScope
(String name) Add a new variable to this (current) scope unless it is already defined in the current scope.assign
(int line, RubySymbol name, Node value) Make a DASgn or LocalAsgn node based on scope logic Note: This is private code made public only for parser.assign
(int line, RubySymbol symbolID, Node value, StaticScope topScope, int depth) assignKeyword
(int line, RubySymbol symbolID, Node value) Register a keyword argument with this staticScope.void
captureParentRefinements
(ThreadContext context) Duplicate the parent scope's refinements overlay to get a moment-in-time snapshot.<T> T
collectVariables
(IntFunction<T> collectionFactory, BiConsumer<T, String> collectionPopulator) <T> T
collectVariables
(ThreadContext context, BiFunction<ThreadContext, Integer, T> collectionFactory, BiConsumer<T, String> collectionPopulator) Populate a deduplicated collection of variable names in scope using the given functions.construct
(DynamicScope parent) declare
(int line, RubySymbol symbolID) Make a DVar or LocalVar node based on scoping logicdeclare
(int line, RubySymbol symbolID, int depth) Update current scoping structure to populate with proper cref scoping values.int
Does the variable exist?String[]
Get all visible variables that we can see from this scope that have been assigned (e.g.getConstant
(String internedName) Deprecated.getConstant
(ThreadContext context, String internedName) getConstantDefined
(String internedName) Deprecated.getConstantDefined
(ThreadContext context, String internedName) Gets a constant back from lexical search from the cref in this scope.getConstantDefinedNoObject
(String internedName) Deprecated.getConstantInner
(String internedName) Deprecated.Next outer most scope in list of scopes.getFile()
int
Gets the Local Scope relative to the current Scope.getLocalVariables
(Ruby runtime) Deprecated.getLocalVariables
(ThreadContext context) Get a Ruby Array of all local variables.Get the live CRef module associated with this scope.int
getOverlayModuleForWrite
(ThreadContext context) getScopedConstant
(ThreadContext context, String internedName) For all block or method associated with static scopes this will return the signature for that signature-providing scope.getType()
String[]
boolean
Argument scopes represent scopes which contain arguments for zsuper.boolean
int
Is this name in the visible to the current scopeint
boolean
boolean
Determine if we happen to be within a method definition.boolean
keywordExists
(String name) void
void
setEnclosingScope
(StaticScope parent) void
void
setInstanceVariableNames
(Collection<String> ivarWrites) void
setIRScope
(IRScope irScope) void
setKeywordIndices
(BitSet keywordIndices) void
setModule
(RubyModule module) void
setPreviousCRefScope
(StaticScope crefScope) void
setScopeType
(IRScopeType scopeType) void
setSignature
(Signature signature) This happens in when first defining ArgsNodes or when reifying a method from AOT.void
setVariables
(String[] names) toString()
-
Field Details
-
MAX_SPECIALIZED_SIZE
public static final int MAX_SPECIALIZED_SIZE- See Also:
-
enclosingScope
-
scopeType
-
-
Constructor Details
-
StaticScope
-
StaticScope
Construct a new static scope.- Parameters:
type
- the type of scopeenclosingScope
- the lexically containing scope.
-
StaticScope
protected StaticScope(StaticScope.Type type, StaticScope enclosingScope, String[] names, int firstKeywordIndex) Construct a new static scope. The array of strings should all be the interned versions, since several other optimizations depend on being able to do object equality checks.- Parameters:
type
- the type of scopeenclosingScope
- the lexically containing scope.names
- The list of interned String variable names.
-
StaticScope
protected StaticScope(StaticScope.Type type, StaticScope enclosingScope, String file, String[] names, int firstKeywordIndex) -
StaticScope
-
-
Method Details
-
setKeywordIndices
-
getKeywordIndices
-
getFirstKeywordIndex
public int getFirstKeywordIndex() -
construct
-
getIRScope
-
getScopeType
-
setScopeType
-
setIRScope
-
addVariableThisScope
Add a new variable to this (current) scope unless it is already defined in the current scope.- Parameters:
name
- of new variable- Returns:
- index of variable
-
addVariable
Add a new variable to this (current) scope unless it is already defined in any reachable scope.- Parameters:
name
- of new variable- Returns:
- index+depth merged location of scope
-
getVariables
-
getNumberOfVariables
public int getNumberOfVariables() -
setVariables
-
getConstantDefined
Deprecated. -
getConstantDefined
Gets a constant back from lexical search from the cref in this scope. As it is for defined? we will not forced resolution of autoloads nor call const_defined -
getConstantDefinedNoObject
Deprecated. -
getConstant
Deprecated. -
getConstant
-
getConstantInner
Deprecated. -
getScopedConstant
-
getEnclosingScope
Next outer most scope in list of scopes. An enclosing scope may have no direct scoping relationship to its child. If I am in a localScope and then I enter something which creates another localScope the enclosing scope will be the first scope, but there are no valid scoping relationships between the two. Methods which walk the enclosing scopes are responsible for enforcing appropriate scoping relationships.- Returns:
- the parent scope
-
setEnclosingScope
-
exists
Does the variable exist?- Parameters:
name
- of the variable to find- Returns:
- index of variable or -1 if it does not exist
-
isDefined
Is this name in the visible to the current scope- Parameters:
name
- to be looked for- Returns:
- a location where the left-most 16 bits of number of scopes down it is and the right-most 16 bits represents its index in that scope
-
assign
Make a DASgn or LocalAsgn node based on scope logic Note: This is private code made public only for parser. -
assignKeyword
Register a keyword argument with this staticScope. It additionally will track where the first keyword argument started so we can test and tell whether we have a kwarg or an ordinary variable during live execution (See keywordExists). -
keywordExists
-
getAllNamesInScope
Get all visible variables that we can see from this scope that have been assigned (e.g. seen so far)- Returns:
- a list of all names (sans $~ and $_ which are special names)
-
collectVariables
public <T> T collectVariables(IntFunction<T> collectionFactory, BiConsumer<T, String> collectionPopulator) -
collectVariables
public <T> T collectVariables(ThreadContext context, BiFunction<ThreadContext, Integer, T> collectionFactory, BiConsumer<T, String> collectionPopulator) Populate a deduplicated collection of variable names in scope using the given functions. This may include variables that are not strictly Ruby local variable names, so the consumer should validate names as appropriate.- Type Parameters:
T
- resulting collection type- Parameters:
collectionFactory
- used to construct the collectioncollectionPopulator
- used to pass values into the collection- Returns:
- populated collection
-
getLocalVariables
Deprecated. -
getLocalVariables
Get a Ruby Array of all local variables.- Parameters:
context
- the current context- Returns:
- populated RubyArray
-
isDefined
-
addAssign
-
assign
public AssignableNode assign(int line, RubySymbol symbolID, Node value, StaticScope topScope, int depth) -
declare
-
declare
Make a DVar or LocalVar node based on scoping logic- Parameters:
line
- the location that in the source that the new node will come fromsymbolID
- of the variable to be created is named- Returns:
- a DVarNode or LocalVarNode Note: This is private code made public only for parser.
-
getLocalScope
Gets the Local Scope relative to the current Scope. For LocalScopes this will be itself. Blocks will contain the LocalScope it contains.- Returns:
- localScope
-
getModule
Get the live CRef module associated with this scope.- Returns:
- the live module
-
getPreviousCRefScope
-
setPreviousCRefScope
-
setModule
-
determineModule
Update current scoping structure to populate with proper cref scoping values. This should be called at any point when you reference a scope for the first time. For the interpreter this is done in a small number of places (defnNode, defsNode, and getBlock). The compiler does this in the same places.- Returns:
- the current cref, though this is largely an implementation detail
-
isBlockScope
public boolean isBlockScope() -
isArgumentScope
public boolean isArgumentScope()Argument scopes represent scopes which contain arguments for zsuper. All LocalStaticScopes are argument scopes and BlockStaticScopes can be when they are used by define_method. -
makeArgumentScope
public void makeArgumentScope() -
getSignature
For all block or method associated with static scopes this will return the signature for that signature-providing scope. module bodies and other non-arity specific code will return null. -
setSignature
This happens in when first defining ArgsNodes or when reifying a method from AOT. -
getDummyScope
-
isWithinMethod
public boolean isWithinMethod()Determine if we happen to be within a method definition.- Returns:
- true if so
-
toString
-
getType
-
getFile
-
setFile
-
duplicate
-
getOverlayModuleForRead
-
getOverlayModuleForWrite
-
captureParentRefinements
Duplicate the parent scope's refinements overlay to get a moment-in-time snapshot. Caller must decide whether this scope is using (or maybe) using refinements.- Parameters:
context
-
-
getInstanceVariableNames
-
setInstanceVariableNames
-
isRuby2Keywords
public boolean isRuby2Keywords()
-