public class Return extends Op {
final Expr e;
public Return() { this(VOID_EXPR); }
- public Return(Expr e) { this.e = e; }
+ public Return(Expr e) {
+ this.e = e;
+ if(Type.unify(method.getReturnType(),e.getType()) != method.getReturnType())
+ throw new IllegalArgumentException("type mismatch");
+ }
public String toString() { return e.getType() == Type.VOID ? "return" : ("return "+e.toString()); }
}
private final Object o;
public Constant(int i) { this(new Integer(i)); }
public Constant(Object o) { this.o = o; }
- public String _toString() { return o instanceof String ? "\"" + o + "\"" : o.toString(); }
+ public String _toString() { return o == null ? "null" : o instanceof String ? "\"" + o + "\"" : o.toString(); }
public Type getType() {
+ if (o == null) return Type.NULL;
if (o instanceof Byte) return Type.BYTE;
if (o instanceof Short) return Type.SHORT;
if (o instanceof Character) return Type.CHAR;
public static final Type BYTE = new Primitive("B", "byte");
public static final Type CHAR = new Primitive("C", "char");
public static final Type SHORT = new Primitive("S", "short");
+ public static final Type NULL = new Null();
public static final Type.Class OBJECT = Type.Class.instance("java.lang.Object");
public static final Type.Class STRING = Type.Class.instance("java.lang.String");
public boolean isClass() { return false; }
public boolean isArray() { return false; }
+ public static Type unify(Type t1, Type t2) {
+ if(t1 == Type.NULL) return t2;
+ if(t2 == Type.NULL) return t1;
+ if(t1 == t2) return t1;
+ // FIXME: This needs to do a lot more (subclasses, etc)
+ // it probably should be in Context.java
+ return null;
+ }
+
// Protected/Private //////////////////////////////////////////////////////////////////////////////
protected final String descriptor;
this.descriptor = descriptor;
instances.put(descriptor, this);
}
+
+ public static class Null extends Type {
+ protected Null() { super(""); } // not really correct....
+ }
public static class Primitive extends Type {
private String humanReadable;