CommonScope.java

/**
 * 
 */
package edu.udel.cis.vsl.civl.model.common;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import edu.udel.cis.vsl.civl.model.IF.CIVLFunction;
import edu.udel.cis.vsl.civl.model.IF.CIVLSource;
import edu.udel.cis.vsl.civl.model.IF.Identifier;
import edu.udel.cis.vsl.civl.model.IF.Model;
import edu.udel.cis.vsl.civl.model.IF.Scope;
import edu.udel.cis.vsl.civl.model.IF.type.CIVLArrayType;
import edu.udel.cis.vsl.civl.model.IF.type.CIVLHeapType;
import edu.udel.cis.vsl.civl.model.IF.type.CIVLPointerType;
import edu.udel.cis.vsl.civl.model.IF.type.CIVLStructType;
import edu.udel.cis.vsl.civl.model.IF.type.CIVLType;
import edu.udel.cis.vsl.civl.model.IF.type.StructField;
import edu.udel.cis.vsl.civl.model.IF.variable.Variable;

/**
 * A scope.
 * 
 * @author Timothy K. Zirkel (zirkel)
 * 
 */
public class CommonScope extends CommonSourceable implements Scope {

	private Scope parent;
	private Variable[] variables;
	private Set<Scope> children = new LinkedHashSet<Scope>();
	private Collection<Variable> procRefs = new HashSet<Variable>();
	private Collection<Variable> pointers = new HashSet<Variable>();
	private int id;
	private CIVLFunction function;

	/**
	 * A scope.
	 * 
	 * @param parent
	 *            The containing scope of this scope. Only null for the
	 *            outermost scope of the designated "System" function.
	 * 
	 * @param variables
	 *            The set of variables in this scope.
	 */
	public CommonScope(CIVLSource source, Scope parent,
			Set<Variable> variables, int id) {
		super(source);
		this.parent = parent;
		this.variables = new Variable[variables.size()];
		for (Variable v : variables) {
			assert this.variables[v.vid()] == null;
			this.variables[v.vid()] = v;
			v.setScope(this);
			checkProcRef(v);
			checkPointer(v);
		}
		this.id = id;
	}

	/**
	 * @return The containing scope of this scope.
	 */
	public Scope parent() {
		return parent;
	}

	/**
	 * @return The set of variables contained in this scope.
	 */
	public Set<Variable> variables() {
		return new LinkedHashSet<Variable>(Arrays.asList(variables));
	}

	/**
	 * @return The number of variables contained in this scope.
	 */
	public int numVariables() {
		return variables.length;
	}

	/**
	 * @return Get the variable at position i.
	 */
	public Variable getVariable(int i) {
		return variables[i];
	}

	/**
	 * @return The id of this scope.
	 */
	public int id() {
		return id;
	}

	/**
	 * @return The scopes contained by this scope.
	 */
	public Set<Scope> children() {
		return children;
	}

	/**
	 * @param parent
	 *            The containing scope of this scope.
	 */
	public void setParent(Scope parent) {
		this.parent = parent;
	}

	/**
	 * @param variables
	 *            The set of variables contained in this scope.
	 */
	public void setVariables(Set<Variable> variables) {
		this.variables = new Variable[variables.size()];
		for (Variable v : variables) {
			assert this.variables[v.vid()] == null;
			this.variables[v.vid()] = v;
			checkProcRef(v);
			checkPointer(v);
		}
	}

	/**
	 * @param children
	 *            The scopes contained by this scope.
	 */
	public void setChildren(Set<Scope> children) {
		this.children = children;
	}

	/**
	 * @param A
	 *            new scope contained by this scope.
	 */
	public void addChild(Scope child) {
		children.add(child);
	}

	/**
	 * A new variable in this scope.
	 */
	public void addVariable(Variable variable) {
		Variable[] oldVariables = variables;
		variables = new Variable[oldVariables.length + 1];
		for (int i = 0; i < oldVariables.length; i++) {
			variables[i] = oldVariables[i];
		}
		assert variable.vid() == oldVariables.length;
		variables[oldVariables.length] = variable;
		checkProcRef(variable);
		checkPointer(variable);
		variable.setScope(this);
	}

	/**
	 * Get the variable associated with an identifier. If this scope does not
	 * contain such a variable, parent scopes will be recursively checked.
	 * 
	 * @param name
	 *            The identifier for the variable.
	 * @return The model representation of the variable in this scope hierarchy,
	 *         or null if not found.
	 */
	public Variable variable(Identifier name) {
		for (Variable v : variables) {
			if (v.name().equals(name)) {
				return v;
			}
		}
		if (parent != null) {
			return parent.variable(name);
		}
		return null;
	}

	/**
	 * Get the variable at the specified array index.
	 * 
	 * @param vid
	 *            The index of the variable. Should be in the range
	 *            [0,numVariable()-1].
	 * @return The variable at the index.
	 */
	public Variable variable(int vid) {
		return variables[vid];
	}

	/**
	 * @param function
	 *            The function containing this scope.
	 */
	public void setFunction(CIVLFunction function) {
		this.function = function;
	}

	/**
	 * @return The function containing this scope.
	 */
	public CIVLFunction function() {
		return function;
	}

	/**
	 * @return The identifier of the function containing this scope.
	 */
	public Identifier functionName() {
		return function.name();
	}

	/**
	 * A variable has a "procRefType" if it is of type Process or if it is an
	 * array with element of procRefType.
	 * 
	 * @return A collection of the variables in this scope with a procRefType.
	 */
	public Collection<Variable> variablesWithProcrefs() {
		return procRefs;
	}

	/**
	 * A variable contains a pointer type if it is of type PointerType, if it is
	 * an array with elements containing pointer type, or if it is a struct with
	 * fields containing pointer type.
	 * 
	 * @return A collection of the variables in this scope containing pointer
	 *         types.
	 */
	public Collection<Variable> variablesWithPointers() {
		return pointers;
	}

	/**
	 * Checks if a variables is a procRefType. If it is, it gets added to
	 * procRefs.
	 * 
	 * @param v
	 *            The variable being checked.
	 */
	private void checkProcRef(Variable variable) {
		boolean procRefType = false;
		CIVLType type = variable.type();

		if (type.isProcessType()) {
			procRefType = true;
		} else if (variable.type() instanceof CIVLArrayType) {
			CIVLType baseType = ((CIVLArrayType) variable.type()).elementType();

			while (baseType instanceof CIVLArrayType) {
				baseType = ((CIVLArrayType) baseType).elementType();
			}
			if (baseType.isProcessType()) {
				procRefType = true;
			}
		}
		if (procRefType) {
			procRefs.add(variable);
		}
	}

	/**
	 * Checks if a variables is a pointer. If it is, it gets added to pointers.
	 * 
	 * @param v
	 *            The variable being checked.
	 */
	private void checkPointer(Variable variable) {
		boolean pointerType = containsPointerType(variable.type());

		if (pointerType) {
			pointers.add(variable);
		}
	}

	private boolean containsPointerType(CIVLType type) {
		boolean containsPointerType = false;

		if (type instanceof CIVLPointerType) {
			containsPointerType = true;
		} else if (type instanceof CIVLArrayType) {
			containsPointerType = containsPointerType(((CIVLArrayType) type)
					.elementType());
		} else if (type instanceof CIVLStructType) {
			for (StructField f : ((CIVLStructType) type).fields()) {
				boolean fieldContainsPointer = containsPointerType(f.type());

				containsPointerType = containsPointerType
						|| fieldContainsPointer;
			}
		} else if (type instanceof CIVLHeapType) {
			// Heaps start out incomplete, so let's assume this is true for now.
			// Ultimately we'd like to only have this be true if the heap
			// contains pointer types.
			containsPointerType = true;
		}
		return containsPointerType;
	}

	@Override
	public String toString() {
		String result = "Variables : {";

		for (int i = 0; i < variables.length; i++) {
			if (i != 0) {
				result += ", ";
			}
			result += variables[i].toString();
		}
		result += "}";
		return result;
	}

	/**
	 * Print the scope and all children.
	 * 
	 * @param prefix
	 *            String prefix to print on each line
	 * @param out
	 *            The PrintStream to use for printing.
	 */
	public void print(String prefix, PrintStream out) {
		String parentID = "";

		if (parent == null) {
			parentID += "null";
		} else {
			parentID += parent.id();
		}
		out.println(prefix + "scope " + id + " (parent: " + parentID + ")");
		for (Variable v : variables) {
			if(v.purelyLocal()){
				out.println(prefix + "| " + v + "#");
			}
			else out.println(prefix + "| " + v);
		}
		for (Scope child : children) {
			if (child.function().equals(function)) {
				child.print(prefix + "| ", out);
			}
		}
		out.flush();
	}

	public int getVid(Variable staticVariable) {
		int result = -1;

		for (int i = 0; i < variables.length; i++) {
			if (variables[i].equals(staticVariable)) {
				result = i;
				break;
			}
		}
		// TODO If not found, either throw error here or catch higher up
		return result;
	}

	@Override
	public Model model() {
		return function.model();
	}

	@Override
	public boolean isDescendantOf(Scope anc) {
		Scope parent = this.parent;
		
		while(parent != null){
			if(parent.id() == anc.id())
				return true;
			parent = parent.parent();
		}

		return false;
	}

}