// $Id: ArrayVarDescriptor.java,v 1.1.1.1 1999/12/05 22:19:50 mpp Exp $

package IR2;

import java.util.*;

public class ArrayVarDescriptor extends Descriptor {
  private int length;
  private int offset = -1;

  public ArrayVarDescriptor(String name, long l, int type, int line)
    throws SemanticException {
    super(name, type, line); 
    
    length = (int)l;
    
    // Semantic Rule 4:
    if (l <= 0)
      throw new SemanticException("Length of array " + name + " must be greater than zero");
  }


  public int get_length() {
    return length;
  }


  /* Memory slot assignment. */
  public int set_offset(int in_offset) {
    offset = in_offset;
    return offset + length;
  }
  
  public int get_offset() {
    return offset;
  }


  /* Walkable implementation. */
  public String node_name() {
	return "array_var_descriptor";
  }
  public Enumeration neighbors() {
	return new ShortEnumeration();
  }
  public String pretty_print(int indent, boolean recursive) {
	String output = new String();
	
	for (int i = 0; i < indent; i++) output += " ";
	if (recursive) {
	    indent += 2;
	    output += "(" + this.node_name() + "\n";
	    /* stuff - calls to prettyprints of internals of this node*/
	    for (Enumeration e = this.neighbors(); e.hasMoreElements();) {
		Walkable foo = (Walkable)e.nextElement();
		if (foo == null) 
		    output += "-error-";
		else
		    output += foo.pretty_print(indent, true);
	    }
	    for (int i = 0; i < indent; i++) output += " ";
	    output += ") /* "+ this.node_name() + " */\n";
	} else {
	    output += "(" + this.node_name() + ")\n";
	}
	return output;
  }
}
