Top Description Inners Fields Constructors Methods
org.jruby.parser

public Class StaticScope

extends Object
implements Serializable, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, java.io.Serializable
Imports
java.io.Serializable, java.lang.invoke.MethodHandle, java.util.ArrayList, .Arrays, .BitSet, .Collection, .Collections, .HashMap, java.util.function.BiConsumer, .BiFunction, .IntFunction, org.jruby.Ruby, .RubyArray, .RubyBasicObject, .RubyModule, .RubySymbol, org.jruby.api.Convert, .Create, org.jruby.ast.AssignableNode, .DAsgnNode, .DVarNode, .LocalAsgnNode, .LocalVarNode, .Node, .VCallNode, org.jruby.ir.IRMethod, .IRScope, .IRScopeType, org.jruby.runtime.DynamicScope, .Helpers, .Signature, .ThreadContext, org.jruby.runtime.builtin.IRubyObject, org.jruby.runtime.scope.DynamicScopeGenerator, .ManyVarsDynamicScope

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).

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Field Summary

Modifier and TypeField and Description
private volatile MethodHandle
private transient RubyModule
private DynamicScope
protected StaticScope
private String
private IRScope
private boolean
private boolean
private volatile Collection<String>
private BitSet
public static final int
private static final String[]
private RubyModule
private StaticScope
protected IRScopeType
private static final long
private Signature
private StaticScope.Type
private String[]
private int

Constructor Summary

AccessConstructor and Description
protected
StaticScope(StaticScope.Type type, StaticScope enclosingScope, String file)

protected
StaticScope(StaticScope.Type
the type of scope
type
,
StaticScope
the lexically containing scope.
enclosingScope
)

Construct a new static scope.

protected
StaticScope(StaticScope.Type
the type of scope
type
,
StaticScope
the lexically containing scope.
enclosingScope
,
String[]
The list of interned String variable names.
names
,
int firstKeywordIndex)

Construct a new static scope.

protected
StaticScope(StaticScope.Type type, StaticScope enclosingScope, String file, String[] names, int firstKeywordIndex)

protected
StaticScope(StaticScope.Type type, StaticScope enclosingScope, String[] names)

Method Summary

Modifier and TypeMethod and Description
private synchronized MethodHandle
public AssignableNode
addAssign(int line, RubySymbol symbolID, Node value)

public int

Returns:

index+depth merged location of scope
addVariable
(String
of new variable
name
)

Add a new variable to this (current) scope unless it is already defined in any reachable scope.

public int

Returns:

index of variable
addVariableThisScope
(String
of new variable
name
)

Add a new variable to this (current) scope unless it is already defined in the current scope.

public AssignableNode
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.

public AssignableNode
assign(int line, RubySymbol symbolID, Node value, StaticScope topScope, int depth)

public AssignableNode
assignKeyword(int line, RubySymbol symbolID, Node value)

Register a keyword argument with this staticScope.

public void
captureParentRefinements(ThreadContext context)

Duplicate the parent scope's refinements overlay to get a moment-in-time snapshot.

public <T> T
collectVariables(IntFunction<T> collectionFactory, BiConsumer<T, String> collectionPopulator)

public <
resulting collection type
T
>
T

Returns:

populated collection
collectVariables
(ThreadContext context, BiFunction<ThreadContext, Integer, T>
used to construct the collection
collectionFactory
,
BiConsumer<T, String>
used to pass values into the collection
collectionPopulator
)

Populate a deduplicated collection of variable names in scope using the given functions.

public DynamicScope
public Node
declare(int line, RubySymbol symbolID, int depth)

public Node

Returns:

a DVarNode or LocalVarNode Note: This is private code made public only for parser.
declare
(int
the location that in the source that the new node will come from
line
,
RubySymbol
of the variable to be created is named
symbolID
)

Make a DVar or LocalVar node based on scoping logic

public RubyModule

Returns:

the current cref, though this is largely an implementation detail
determineModule
()

Update current scoping structure to populate with proper cref scoping values.

public StaticScope
public int

Returns:

index of variable or -1 if it does not exist
exists
(String
of the variable to find
name
)

Does the variable exist?

private int
public String[]

Returns:

a list of all names (sans $~ and $_ which are special names)
getAllNamesInScope
()

Get all visible variables that we can see from this scope that have been assigned (e.g. seen so far)

public IRubyObject
getConstant(String internedName)
Deprecated since 10.0.

public IRubyObject
getConstant(ThreadContext context, String internedName)

public IRubyObject
getConstantDefined(String internedName)
Deprecated since 10.0.

public IRubyObject
getConstantDefined(ThreadContext context, String internedName)

Gets a constant back from lexical search from the cref in this scope.

public IRubyObject
getConstantDefinedNoObject(String internedName)
Deprecated since 10.0.

private IRubyObject
public IRubyObject
getConstantInner(String internedName)
Deprecated since 10.0.

private IRubyObject
public DynamicScope
public StaticScope

Returns:

the parent scope
getEnclosingScope
()

Next outer most scope in list of scopes.

public String
public int
public Collection<String>
public IRScope
public BitSet
public StaticScope

Returns:

localScope
getLocalScope
()

Gets the Local Scope relative to the current Scope.

public RubyArray<T extends IRubyObject>
getLocalVariables(Ruby runtime)
Deprecated since 10.0.

public RubyArray<T extends IRubyObject>

Returns:

populated RubyArray
getLocalVariables
(ThreadContext
the current context
context
)

Get a Ruby Array of all local variables.

public RubyModule

Returns:

the live module
getModule
()

Get the live CRef module associated with this scope.

public int
public RubyModule
public RubyModule
public StaticScope
public IRubyObject
getScopedConstant(ThreadContext context, String internedName)

public IRScopeType
public Signature
getSignature()

For all block or method associated with static scopes this will return the signature for that signature-providing scope.

public StaticScope.Type
public String[]
private void
public boolean
isArgumentScope()

Argument scopes represent scopes which contain arguments for zsuper.

public boolean
public int

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
isDefined
(String
to be looked for
name
)

Is this name in the visible to the current scope

public int
isDefined(String name, int depth)

public boolean
public boolean

Returns:

true if so
isWithinMethod
()

Determine if we happen to be within a method definition.

public boolean
public void
public void
public void
public void
public void
public void
setKeywordIndices(BitSet keywordIndices)

public void
public void
public void
public void
setSignature(Signature signature)

This happens in when first defining ArgsNodes or when reifying a method from AOT.

public void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

constructorback to summary
private volatile MethodHandle constructor
crefback to summary
private transient RubyModule cref
dummyScopeback to summary
private DynamicScope dummyScope
enclosingScopeback to summary
protected StaticScope enclosingScope
fileback to summary
private String file
irScopeback to summary
private IRScope irScope
isArgumentScopeback to summary
private boolean isArgumentScope
isBlockOrEvalback to summary
private boolean isBlockOrEval
ivarNamesback to summary
private volatile Collection<String> ivarNames
keywordIndicesback to summary
private BitSet keywordIndices
MAX_SPECIALIZED_SIZEback to summary
public static final int MAX_SPECIALIZED_SIZE
NO_NAMESback to summary
private static final String[] NO_NAMES
overlayModuleback to summary
private RubyModule overlayModule
previousCRefScopeback to summary
private StaticScope previousCRefScope
scopeTypeback to summary
protected IRScopeType scopeType
serialVersionUIDback to summary
private static final long serialVersionUID
signatureback to summary
private Signature signature
typeback to summary
private StaticScope.Type type
variableNamesback to summary
private String[] variableNames
variableNamesLengthback to summary
private int variableNamesLength

Constructor Detail

StaticScopeback to summary
protected StaticScope(StaticScope.Type type, StaticScope enclosingScope, String file)
StaticScopeback to summary
protected StaticScope(StaticScope.Type type, StaticScope enclosingScope)

Construct a new static scope.

Parameters
type:StaticScope.Type

the type of scope

enclosingScope:StaticScope

the lexically containing scope.

StaticScopeback to summary
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:StaticScope.Type

the type of scope

enclosingScope:StaticScope

the lexically containing scope.

names:String[]

The list of interned String variable names.

StaticScopeback to summary
protected StaticScope(StaticScope.Type type, StaticScope enclosingScope, String file, String[] names, int firstKeywordIndex)
StaticScopeback to summary
protected StaticScope(StaticScope.Type type, StaticScope enclosingScope, String[] names)

Method Detail

acquireConstructorback to summary
private synchronized MethodHandle acquireConstructor()
addAssignback to summary
public AssignableNode addAssign(int line, RubySymbol symbolID, Node value)
addVariableback to summary
public int addVariable(String name)

Add a new variable to this (current) scope unless it is already defined in any reachable scope.

Parameters
name:String

of new variable

Returns:int

index+depth merged location of scope

addVariableThisScopeback to summary
public int addVariableThisScope(String name)

Add a new variable to this (current) scope unless it is already defined in the current scope.

Parameters
name:String

of new variable

Returns:int

index of variable

assignback to summary
public AssignableNode 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.

assignback to summary
public AssignableNode assign(int line, RubySymbol symbolID, Node value, StaticScope topScope, int depth)
assignKeywordback to summary
public AssignableNode assignKeyword(int line, RubySymbol symbolID, Node value)

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).

captureParentRefinementsback to summary
public void captureParentRefinements(ThreadContext context)

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.

collectVariablesback to summary
public <T> T collectVariables(IntFunction<T> collectionFactory, BiConsumer<T, String> collectionPopulator)
collectVariablesback to summary
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.

Parameters
<T>
resulting collection type
collectionFactory:BiFunction<ThreadContext, Integer, T>

used to construct the collection

collectionPopulator:BiConsumer<T, String>

used to pass values into the collection

Returns:T

populated collection

constructback to summary
public DynamicScope construct(DynamicScope parent)
declareback to summary
public Node declare(int line, RubySymbol symbolID, int depth)
declareback to summary
public Node declare(int line, RubySymbol symbolID)

Make a DVar or LocalVar node based on scoping logic

Parameters
line:int

the location that in the source that the new node will come from

symbolID:RubySymbol

of the variable to be created is named

Returns:Node

a DVarNode or LocalVarNode Note: This is private code made public only for parser.

determineModuleback to summary
public RubyModule 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:RubyModule

the current cref, though this is largely an implementation detail

duplicateback to summary
public StaticScope duplicate()
existsback to summary
public int exists(String name)

Does the variable exist?

Parameters
name:String

of the variable to find

Returns:int

index of variable or -1 if it does not exist

findVariableNameback to summary
private int findVariableName(String name)
getAllNamesInScopeback to summary
public String[] getAllNamesInScope()

Get all visible variables that we can see from this scope that have been assigned (e.g. seen so far)

Returns:String[]

a list of all names (sans $~ and $_ which are special names)

getConstantback to summary
public IRubyObject getConstant(String internedName)

Deprecated

since 10.0.

Annotations
@Deprecated
since:10.0
getConstantback to summary
public IRubyObject getConstant(ThreadContext context, String internedName)
getConstantDefinedback to summary
public IRubyObject getConstantDefined(String internedName)

Deprecated

since 10.0.

Annotations
@Deprecated
since:10.0
getConstantDefinedback to summary
public IRubyObject getConstantDefined(ThreadContext context, String internedName)

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

getConstantDefinedNoObjectback to summary
public IRubyObject getConstantDefinedNoObject(String internedName)

Deprecated

since 10.0.

Annotations
@Deprecated
since:10.0
getConstantDefinedNoObjectback to summary
private IRubyObject getConstantDefinedNoObject(ThreadContext context, String internedName)
getConstantInnerback to summary
public IRubyObject getConstantInner(String internedName)

Deprecated

since 10.0.

Annotations
@Deprecated
since:10.0
getConstantInnerNoObjectback to summary
private IRubyObject getConstantInnerNoObject(ThreadContext context, String internedName)
getDummyScopeback to summary
public DynamicScope getDummyScope()
getEnclosingScopeback to summary
public StaticScope 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:StaticScope

the parent scope

getFileback to summary
public String getFile()
getFirstKeywordIndexback to summary
public int getFirstKeywordIndex()
getInstanceVariableNamesback to summary
public Collection<String> getInstanceVariableNames()
getIRScopeback to summary
public IRScope getIRScope()
getKeywordIndicesback to summary
public BitSet getKeywordIndices()
getLocalScopeback to summary
public StaticScope getLocalScope()

Gets the Local Scope relative to the current Scope. For LocalScopes this will be itself. Blocks will contain the LocalScope it contains.

Returns:StaticScope

localScope

getLocalVariablesback to summary
public RubyArray<T extends IRubyObject> getLocalVariables(Ruby runtime)

Deprecated

since 10.0.

Annotations
@Deprecated
since:10.0
getLocalVariablesback to summary
public RubyArray<T extends IRubyObject> getLocalVariables(ThreadContext context)

Get a Ruby Array of all local variables.

Parameters
context:ThreadContext

the current context

Returns:RubyArray<T extends IRubyObject>

populated RubyArray

getModuleback to summary
public RubyModule getModule()

Get the live CRef module associated with this scope.

Returns:RubyModule

the live module

getNumberOfVariablesback to summary
public int getNumberOfVariables()
getOverlayModuleForReadback to summary
public RubyModule getOverlayModuleForRead()
getOverlayModuleForWriteback to summary
public RubyModule getOverlayModuleForWrite(ThreadContext context)
getPreviousCRefScopeback to summary
public StaticScope getPreviousCRefScope()
getScopedConstantback to summary
public IRubyObject getScopedConstant(ThreadContext context, String internedName)
getScopeTypeback to summary
public IRScopeType getScopeType()
getSignatureback to summary
public Signature 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.

getTypeback to summary
public StaticScope.Type getType()
getVariablesback to summary
public String[] getVariables()
growVariableNamesback to summary
private void growVariableNames(String name)
isArgumentScopeback to summary
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.

isBlockScopeback to summary
public boolean isBlockScope()
isDefinedback to summary
public int isDefined(String name)

Is this name in the visible to the current scope

Parameters
name:String

to be looked for

Returns:int

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

isDefinedback to summary
public int isDefined(String name, int depth)
isRuby2Keywordsback to summary
public boolean isRuby2Keywords()
isWithinMethodback to summary
public boolean isWithinMethod()

Determine if we happen to be within a method definition.

Returns:boolean

true if so

keywordExistsback to summary
public boolean keywordExists(String name)
makeArgumentScopeback to summary
public void makeArgumentScope()
setEnclosingScopeback to summary
public void setEnclosingScope(StaticScope parent)
setFileback to summary
public void setFile(String file)
setInstanceVariableNamesback to summary
public void setInstanceVariableNames(Collection<String> ivarWrites)
setIRScopeback to summary
public void setIRScope(IRScope irScope)
setKeywordIndicesback to summary
public void setKeywordIndices(BitSet keywordIndices)
setModuleback to summary
public void setModule(RubyModule module)
setPreviousCRefScopeback to summary
public void setPreviousCRefScope(StaticScope crefScope)
setScopeTypeback to summary
public void setScopeType(IRScopeType scopeType)
setSignatureback to summary
public void setSignature(Signature signature)

This happens in when first defining ArgsNodes or when reifying a method from AOT.

setVariablesback to summary
public void setVariables(String[] names)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.jruby.parser back to summary

public final Enum StaticScope.Type

extends Enum<StaticScope.Type>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final StaticScope.Type
public static final StaticScope.Type
public static final StaticScope.Type

Constructor Summary

AccessConstructor and Description
private
Type()

Method Summary

Modifier and TypeMethod and Description
public static StaticScope.Type
fromOrdinal(int value)

public static StaticScope.Type
public static StaticScope.Type[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BLOCKback to summary
public static final StaticScope.Type BLOCK
EVALback to summary
public static final StaticScope.Type EVAL
LOCALback to summary
public static final StaticScope.Type LOCAL

Constructor Detail

Typeback to summary
private Type()

Method Detail

fromOrdinalback to summary
public static StaticScope.Type fromOrdinal(int value)
valueOfback to summary
public static StaticScope.Type valueOf(String name)
valuesback to summary
public static StaticScope.Type[] values()