ScopeModule.java

  1. package org.jruby.ir.operands;

  2. import org.jruby.ir.IRVisitor;
  3. import org.jruby.ir.transformations.inlining.CloneInfo;
  4. import org.jruby.parser.StaticScope;
  5. import org.jruby.runtime.DynamicScope;
  6. import org.jruby.runtime.Helpers;
  7. import org.jruby.runtime.ThreadContext;
  8. import org.jruby.runtime.builtin.IRubyObject;

  9. import java.util.List;

  10. /**
  11.  * Wrap a scope for the purpose of finding live module which happens to be associated with it.
  12.  */
  13. public class ScopeModule extends Operand {
  14.     // First four scopes are so common and this operand is immutable so we share them.
  15.     public static final ScopeModule[] SCOPE_MODULE = {
  16.             new ScopeModule(0), new ScopeModule(1), new ScopeModule(2), new ScopeModule(3), new ScopeModule(4)
  17.     };

  18.     public static ScopeModule ModuleFor(int depth) {
  19.         return depth < SCOPE_MODULE.length ? SCOPE_MODULE[depth] : new ScopeModule(depth);
  20.     }
  21.    
  22.     private final int scopeModuleDepth;

  23.     public ScopeModule(int scopeModuleDepth) {
  24.         super(OperandType.SCOPE_MODULE);

  25.         this.scopeModuleDepth = scopeModuleDepth;
  26.     }

  27.     @Override
  28.     public void addUsedVariables(List<Variable> l) {
  29.         /* Do nothing */
  30.     }

  31.     @Override
  32.     public Operand cloneForInlining(CloneInfo ii) {
  33.         return this;
  34.     }

  35.     @Override
  36.     public int hashCode() {
  37.         return scopeModuleDepth;
  38.     }

  39.     public int getScopeModuleDepth() {
  40.         return scopeModuleDepth;
  41.     }

  42.     @Override
  43.     public boolean equals(Object other) {
  44.         return other instanceof ScopeModule && scopeModuleDepth == ((ScopeModule) other).scopeModuleDepth;
  45.     }

  46.     @Override
  47.     public boolean canCopyPropagate() {
  48.         return true;
  49.     }

  50.     @Override
  51.     public String toString() {
  52.         return "module<" + scopeModuleDepth + ">";
  53.     }

  54.     @Override
  55.     public Object retrieve(ThreadContext context, IRubyObject self, StaticScope currScope, DynamicScope currDynScope, Object[] temp) {
  56.         return Helpers.getNthScopeModule(currScope, scopeModuleDepth);
  57.     }

  58.     @Override
  59.     public void visit(IRVisitor visitor) {
  60.         visitor.ScopeModule(this);
  61.     }
  62. }