From 58b777d0f15df1ffd1bfa16135030118ba7c26e3 Mon Sep 17 00:00:00 2001 From: brian Date: Mon, 4 Jul 2005 01:10:04 +0000 Subject: [PATCH] beginnings of type unification code darcs-hash:20050704011004-24bed-b6a9105071161062091449ef4c969fd2bc5de9b9.gz --- src/org/ibex/classgen/JSSA.java | 9 +++++++-- src/org/ibex/classgen/Type.java | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/org/ibex/classgen/JSSA.java b/src/org/ibex/classgen/JSSA.java index 664023f..d14efef 100644 --- a/src/org/ibex/classgen/JSSA.java +++ b/src/org/ibex/classgen/JSSA.java @@ -321,7 +321,11 @@ public class JSSA extends MethodGen implements CGConst { 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()); } } @@ -425,8 +429,9 @@ public class JSSA extends MethodGen implements CGConst { 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; diff --git a/src/org/ibex/classgen/Type.java b/src/org/ibex/classgen/Type.java index 28c30dd..3d77ba3 100644 --- a/src/org/ibex/classgen/Type.java +++ b/src/org/ibex/classgen/Type.java @@ -18,6 +18,7 @@ public abstract class Type implements CGConst { 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"); @@ -53,6 +54,15 @@ public abstract class Type implements CGConst { 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; @@ -61,6 +71,10 @@ public abstract class Type implements CGConst { 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; -- 1.7.10.4