org.jruby.parser
Class StaticScope

java.lang.Object
  extended by org.jruby.parser.StaticScope
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
BlockStaticScope, LocalStaticScope

public abstract class StaticScope
extends java.lang.Object
implements java.io.Serializable

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

Field Summary
protected  StaticScope enclosingScope
           
 
Constructor Summary
protected StaticScope(StaticScope enclosingScope, java.lang.String[] names)
          Construct a new static scope.
 
Method Summary
 int addVariable(java.lang.String name)
          Add a new variable to this (current) scope unless it is already defined in any reachable scope.
 int addVariableThisScope(java.lang.String name)
          Add a new variable to this (current) scope unless it is already defined in the current scope.
 AssignableNode assign(ISourcePosition position, java.lang.String name, Node value)
          Make a DASgn or LocalAsgn node based on scope logic
protected abstract  AssignableNode assign(ISourcePosition position, java.lang.String name, Node value, StaticScope topScope, int depth)
           
 void capture(int index)
           
 Node declare(ISourcePosition position, java.lang.String name)
          Make a DVar or LocalVar node based on scoping logic
protected abstract  Node declare(ISourcePosition position, java.lang.String name, int depth)
           
 RubyModule determineModule()
          Update current scoping structure to populate with proper cref scoping values.
 int exists(java.lang.String name)
          Does the variable exist?
abstract  java.lang.String[] getAllNamesInScope()
          Get all visible variables that we can see from this scope that have been assigned (e.g.
 Arity getArity()
           
 IRubyObject getConstant(Ruby runtime, java.lang.String internedName, RubyModule object)
           
 IRubyObject getConstantWithConstMissing(Ruby runtime, java.lang.String internedName, RubyModule object)
           
 DynamicScope getDummyScope()
           
 StaticScope getEnclosingScope()
          Next outer most scope in list of scopes.
abstract  StaticScope getLocalScope()
          Gets the Local Scope relative to the current Scope.
 RubyModule getModule()
          Get the live CRef module associated with this scope.
 int getNumberOfVariables()
           
 int getOptionalArgs()
           
 StaticScope getPreviousCRefScope()
           
 int getRequiredArgs()
           
 int getRestArg()
           
 java.lang.String[] getVariables()
           
 boolean isArgumentScope()
           
 boolean isBackrefLastlineScope()
           
 boolean isCaptured(int index)
           
 int isDefined(java.lang.String name)
          Is this name in the visible to the current scope
protected abstract  int isDefined(java.lang.String name, int depth)
           
 void setArgumentScope(boolean isArgumentScope)
           
 void setArities(int required, int optional, int rest)
           
 void setBackrefLastlineScope(boolean isBackrefLastlineScope)
           
 void setModule(RubyModule module)
           
 void setPreviousCRefScope(StaticScope crefScope)
           
 void setRequiredArgs(int requiredArgs)
           
 void setRestArg(int restArg)
           
 void setVariables(java.lang.String[] names)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

enclosingScope

protected final StaticScope enclosingScope
Constructor Detail

StaticScope

protected StaticScope(StaticScope enclosingScope,
                      java.lang.String[] names)
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:
enclosingScope - The lexically containing scope.
names - The list of interned String variable names.
Method Detail

addVariableThisScope

public int addVariableThisScope(java.lang.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+depth merged location of scope

addVariable

public int addVariable(java.lang.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 java.lang.String[] getVariables()

getNumberOfVariables

public int getNumberOfVariables()

setVariables

public void setVariables(java.lang.String[] names)

getConstantWithConstMissing

public IRubyObject getConstantWithConstMissing(Ruby runtime,
                                               java.lang.String internedName,
                                               RubyModule object)

getConstant

public IRubyObject getConstant(Ruby runtime,
                               java.lang.String internedName,
                               RubyModule object)

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

exists

public int exists(java.lang.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(java.lang.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(ISourcePosition position,
                             java.lang.String name,
                             Node value)
Make a DASgn or LocalAsgn node based on scope logic

Parameters:
position -
name -
value -
Returns:

getAllNamesInScope

public abstract java.lang.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)

isDefined

protected abstract int isDefined(java.lang.String name,
                                 int depth)

assign

protected abstract AssignableNode assign(ISourcePosition position,
                                         java.lang.String name,
                                         Node value,
                                         StaticScope topScope,
                                         int depth)

declare

protected abstract Node declare(ISourcePosition position,
                                java.lang.String name,
                                int depth)

declare

public Node declare(ISourcePosition position,
                    java.lang.String name)
Make a DVar or LocalVar node based on scoping logic

Parameters:
position - the location that in the source that the new node will come from
name - of the variable to be created is named
Returns:
a DVarNode or LocalVarNode

capture

public void capture(int index)

isCaptured

public boolean isCaptured(int index)

getLocalScope

public abstract 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

getOptionalArgs

public int getOptionalArgs()

getRequiredArgs

public int getRequiredArgs()

setRequiredArgs

public void setRequiredArgs(int requiredArgs)

getRestArg

public int getRestArg()

setRestArg

public void setRestArg(int restArg)

isArgumentScope

public boolean isArgumentScope()

setArgumentScope

public void setArgumentScope(boolean isArgumentScope)

isBackrefLastlineScope

public boolean isBackrefLastlineScope()

setBackrefLastlineScope

public void setBackrefLastlineScope(boolean isBackrefLastlineScope)

getArity

public Arity getArity()

setArities

public void setArities(int required,
                       int optional,
                       int rest)

getDummyScope

public DynamicScope getDummyScope()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2002-2009 JRuby Team. All Rights Reserved.