* The generated file will be divided into two sections: *
* This visitor only needs to be used if are not using the Control Flow Graph.
- *
+ *
* @author Leo Buttiker
*/
-public class ReturnCheckerVisitor extends AstVisitor Note: this list may contain null pointers!
+ *
+ * Note: this list may contain null pointers!
*/
public final List
*
*/
TYPE_ERROR,
-
+
/**
* A class is its own super class
*/
CIRCULAR_INHERITANCE,
-
+
/**
* One of the following:
*
@@ -69,13 +79,13 @@ public class SemanticFailure extends RuntimeException {
*
*/
INVALID_START_POINT,
-
+
/**
* A class {@code Object} was defined. This class is implicitly
* defined and cannot be defined explicitly.
*/
OBJECT_CLASS_DEFINED,
-
+
/**
* A type name was found for which no class declaration exists.
* This can occur in many contexts:
@@ -86,34 +96,36 @@ public class SemanticFailure extends RuntimeException {
*
*/
NO_SUCH_TYPE,
-
+
/**
* The parameters of an overridden method have different types
- * from the base method, there is a different
+ * from the base method, there is a different
* number of parameters, or the return value is different.
*/
INVALID_OVERRIDE,
-
- /** A method was called with the wrong number of arguments */
+
+ /**
+ * A method was called with the wrong number of arguments
+ */
WRONG_NUMBER_OF_ARGUMENTS,
-
- /**
+
+ /**
* Indicates the use of a local variable that may not have been
* initialized (ACD only).
*/
POSSIBLY_UNINITIALIZED,
}
-
+
public final Cause cause;
-
+
public SemanticFailure(Cause cause) {
super(cause.name());
this.cause = cause;
}
-
+
public SemanticFailure(Cause cause, String format, Object... args) {
super(String.format(format, args));
this.cause = cause;
}
-
+
}
diff --git a/src/cd/frontend/semantic/SymTable.java b/src/cd/frontend/semantic/SymTable.java
index b1815aa..59ef455 100644
--- a/src/cd/frontend/semantic/SymTable.java
+++ b/src/cd/frontend/semantic/SymTable.java
@@ -3,24 +3,23 @@ package cd.frontend.semantic;
import cd.frontend.semantic.SemanticFailure.Cause;
import cd.ir.Symbol;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
-/**
- * A simple symbol table, with a pointer to the enclosing scope.
+/**
+ * A simple symbol table, with a pointer to the enclosing scope.
* Used by {@link TypeChecker} to store the various scopes for
- * local, parameter, and field lookup. */
+ * local, parameter, and field lookup.
+ */
public class SymTable {
-
+
private final Map parent;
-
+
public SymTable(SymTable parent) {
this.parent = parent;
}
-
+
public void add(S sym) {
// check that the symbol is not already declared *at this level*
if (containsLocally(sym.name))
@@ -28,10 +27,21 @@ public class SymTable {
map.put(sym.name, sym);
}
+ public List allSymbols() {
+ List result = new ArrayList();
+ SymTable st = this;
+ while (st != null) {
+ for (S sym : st.map.values())
+ result.add(sym);
+ st = st.parent;
+ }
+ return result;
+ }
+
public Collection localSymbols() {
return this.map.values();
}
-
+
/**
* True if there is a declaration with the given name at any
* level in the symbol table
@@ -40,7 +50,7 @@ public class SymTable {
return get(name) != null;
}
- /**
+ /**
* True if there is a declaration at THIS level in the symbol
* table; may return {@code false} even if a declaration exists
* in some enclosing scope
@@ -49,9 +59,10 @@ public class SymTable {
return this.map.containsKey(name);
}
- /**
+ /**
* Base method: returns {@code null} if no symbol by that
- * name can be found, in this table or in its parents */
+ * name can be found, in this table or in its parents
+ */
public S get(String name) {
S res = map.get(name);
if (res != null)
@@ -60,11 +71,12 @@ public class SymTable {
return null;
return parent.get(name);
}
-
+
/**
- * Finds the symbol with the given name, or fails with a
+ * Finds the symbol with the given name, or fails with a
* NO_SUCH_TYPE error. Only really makes sense to use this
- * if S == TypeSymbol, but we don't strictly forbid it... */
+ * if S == TypeSymbol, but we don't strictly forbid it...
+ */
public S getType(String name) {
S res = get(name);
if (res == null)
diff --git a/src/cd/frontend/semantic/SymbolCreator.java b/src/cd/frontend/semantic/SymbolCreator.java
index c53916a..b8b9b21 100644
--- a/src/cd/frontend/semantic/SymbolCreator.java
+++ b/src/cd/frontend/semantic/SymbolCreator.java
@@ -21,10 +21,10 @@ import java.util.Set;
* and local variables.
*/
public class SymbolCreator extends Object {
-
+
final Main main;
final SymTable void add(Map void add(Maptrue
iff A op B == B op A
for this
* operator.
*/
public boolean isCommutative() {
- switch(this) {
- case B_PLUS:
- case B_TIMES:
- case B_AND:
- case B_OR:
- case B_EQUAL:
- case B_NOT_EQUAL:
- return true;
- default:
- return false;
- }
+ switch (this) {
+ case B_PLUS:
+ case B_TIMES:
+ case B_AND:
+ case B_OR:
+ case B_EQUAL:
+ case B_NOT_EQUAL:
+ return true;
+ default:
+ return false;
+ }
}
- };
-
- public BOp operator;
-
- public BinaryOp(Expr left, BOp operator, Expr right) {
- super(left, right);
- this.operator = operator;
- }
+ }
+
+ ;
+
+ public BOp operator;
+
+ public BinaryOp(Expr left, BOp operator, Expr right) {
+ super(left, right);
+ this.operator = operator;
+ }
@Override
public