package som.compiler;

import som.interpreter.nodes.ArgumentReadNode;
import som.interpreter.nodes.ExpressionNode;
import som.interpreter.nodes.UninitializedVariableNode.UninitializedVariableReadNode;
import som.interpreter.nodes.UninitializedVariableNode.UninitializedVariableWriteNode;

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.frame.FrameSlot;

public abstract class Variable {

  Variable(final String name) {
    this.name      = name;
    this.isRead    = false;
    this.isWritten = false;
    this.isReadOutOfContext    = false;
    this.isWrittenOutOfContext = false;
  }

  public final String name;

  @CompilationFinal private boolean isRead;
  @CompilationFinal private boolean isWritten;
  @CompilationFinal private boolean isReadOutOfContext;
  @CompilationFinal private boolean isWrittenOutOfContext;

  public void setIsRead() {
    isRead = true;
  }

  public void setIsReadOutOfContext() {
    isReadOutOfContext = true;
  }

  public void setIsWritten() {
    isWritten = true;
  }

  public void setIsWrittenOutOfContext() {
    isWrittenOutOfContext = true;
  }

  public boolean isAccessed() {
    return isRead || isWritten;
  }

  public boolean isAccessedOutOfContext() {
    return isReadOutOfContext || isWrittenOutOfContext;
  }


  // TODO: something more specific than an expression node?
  public abstract ExpressionNode getReadNode(int contextLevel);

  public static final class Argument extends Variable {
    public final int index;

    Argument(final String name, final int index) {
      super(name);
      this.index = index;
    }

    @Override
    public ExpressionNode getReadNode(final int contextLevel) {
      return new ArgumentReadNode(this, contextLevel);
    }
  }

  public static final class Local extends Variable {
    public final FrameSlot slot;
    @CompilationFinal public int upvalueIndex;

    Local(final String name, final FrameSlot slot) {
      super(name);
      this.slot = slot;
    }

    @Override
    public ExpressionNode getReadNode(final int contextLevel) {
      return new UninitializedVariableReadNode(this, contextLevel);
    }

    public ExpressionNode getWriteNode(final int contextLevel,
        final ExpressionNode valueExpr) {
      return new UninitializedVariableWriteNode(this, contextLevel, valueExpr);
    }
  }
}

@@ -55,32 +55,77 @@
 gumentReadNode;%0A
+import som.interpreter.nodes.ContextualNode;%0A
 import som.inter
@@ -1358,69 +1358,8 @@
  %7D%0A%0A
-%0A  // TODO: something more specific than an expression node?%0A
   pu
@@ -1372,26 +1372,26 @@
 bstract 
-Expression
+Contextual
 Node get
@@ -1628,34 +1628,34 @@
 %0A    public 
-Expression
+Contextual
 Node getRead
@@ -2009,34 +2009,34 @@
 %0A    public 
-Expression
+Contextual
 Node getRead

Use ContextualNode as return type of getReadNode