Class StaticScope

java.lang.Object
org.jruby.parser.StaticScope
All Implemented Interfaces:
Serializable, Cloneable

public class StaticScope extends Object implements 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:
  • Field Details

  • Constructor Details

    • StaticScope

      protected StaticScope(StaticScope.Type type, StaticScope enclosingScope, String file)
    • StaticScope

      protected StaticScope(StaticScope.Type type, StaticScope enclosingScope)
      Construct a new static scope.
      Parameters:
      type - the type of scope
      enclosingScope - 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 scope
      enclosingScope - 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

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

    • setKeywordIndices

      public void setKeywordIndices(BitSet keywordIndices)
    • getKeywordIndices

      public BitSet getKeywordIndices()
    • getFirstKeywordIndex

      public int getFirstKeywordIndex()
    • construct

      public DynamicScope construct(DynamicScope parent)
    • getIRScope

      public IRScope getIRScope()
    • getScopeType

      public IRScopeType getScopeType()
    • setScopeType

      public void setScopeType(IRScopeType scopeType)
    • setIRScope

      public void setIRScope(IRScope irScope)
    • addVariableThisScope

      public int addVariableThisScope(String name)
      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

      public int addVariable(String name)
      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

      public String[] getVariables()
    • getNumberOfVariables

      public int getNumberOfVariables()
    • setVariables

      public void setVariables(String[] names)
    • getConstantDefined

      @Deprecated(since="10.0") public IRubyObject getConstantDefined(String internedName)
      Deprecated.
    • getConstantDefined

      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
    • getConstantDefinedNoObject

      @Deprecated(since="10.0") public IRubyObject getConstantDefinedNoObject(String internedName)
      Deprecated.
    • getConstant

      @Deprecated(since="10.0") public IRubyObject getConstant(String internedName)
      Deprecated.
    • getConstant

      public IRubyObject getConstant(ThreadContext context, String internedName)
    • getConstantInner

      @Deprecated(since="10.0") public IRubyObject getConstantInner(String internedName)
      Deprecated.
    • getScopedConstant

      public IRubyObject getScopedConstant(ThreadContext context, String internedName)
    • getEnclosingScope

      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:
      the parent scope
    • setEnclosingScope

      public void setEnclosingScope(StaticScope parent)
    • exists

      public int exists(String name)
      Does the variable exist?
      Parameters:
      name - of the variable to find
      Returns:
      index of variable or -1 if it does not exist
    • isDefined

      public int isDefined(String name)
      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

      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.
    • assignKeyword

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

      public boolean keywordExists(String name)
    • getAllNamesInScope

      public String[] 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 collection
      collectionPopulator - used to pass values into the collection
      Returns:
      populated collection
    • getLocalVariables

      @Deprecated(since="10.0") public RubyArray getLocalVariables(Ruby runtime)
      Deprecated.
    • getLocalVariables

      public RubyArray getLocalVariables(ThreadContext context)
      Get a Ruby Array of all local variables.
      Parameters:
      context - the current context
      Returns:
      populated RubyArray
    • isDefined

      public int isDefined(String name, int depth)
    • addAssign

      public AssignableNode addAssign(int line, RubySymbol symbolID, Node value)
    • assign

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

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

      public Node declare(int line, RubySymbol symbolID)
      Make a DVar or LocalVar node based on scoping logic
      Parameters:
      line - the location that in the source that the new node will come from
      symbolID - of the variable to be created is named
      Returns:
      a DVarNode or LocalVarNode Note: This is private code made public only for parser.
    • getLocalScope

      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:
      localScope
    • getModule

      public RubyModule getModule()
      Get the live CRef module associated with this scope.
      Returns:
      the live module
    • getPreviousCRefScope

      public StaticScope getPreviousCRefScope()
    • setPreviousCRefScope

      public void setPreviousCRefScope(StaticScope crefScope)
    • setModule

      public void setModule(RubyModule module)
    • determineModule

      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:
      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

      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.
    • setSignature

      public void setSignature(Signature signature)
      This happens in when first defining ArgsNodes or when reifying a method from AOT.
    • getDummyScope

      public DynamicScope getDummyScope()
    • isWithinMethod

      public boolean isWithinMethod()
      Determine if we happen to be within a method definition.
      Returns:
      true if so
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getType

      public StaticScope.Type getType()
    • getFile

      public String getFile()
    • setFile

      public void setFile(String file)
    • duplicate

      public StaticScope duplicate()
    • getOverlayModuleForRead

      public RubyModule getOverlayModuleForRead()
    • getOverlayModuleForWrite

      public RubyModule getOverlayModuleForWrite(ThreadContext context)
    • captureParentRefinements

      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.
      Parameters:
      context -
    • getInstanceVariableNames

      public Collection<String> getInstanceVariableNames()
    • setInstanceVariableNames

      public void setInstanceVariableNames(Collection<String> ivarWrites)
    • isRuby2Keywords

      public boolean isRuby2Keywords()