protected String identifier;
protected ExpressionNode en;
private boolean isglobal;
+ private boolean isenum;
+ private int enumvalue;
public FieldDescriptor(Modifiers m, TypeDescriptor t, String identifier, ExpressionNode e, boolean isglobal) {
super(identifier);
this.safename = "___" + name + "___";
this.uniqueid=count++;
this.isglobal=isglobal;
+ this.isenum = false;
+ this.enumvalue = -1;
+ }
+
+ public boolean isEnum() {
+ return this.isenum;
+ }
+
+ public int enumValue() {
+ return this.enumvalue;
+ }
+
+ public void setAsEnum() {
+ this.isenum = true;
+ }
+
+ public void setEnumValue(int value) {
+ this.enumvalue = value;
}
public ExpressionNode getExpressionNode(){
for(int i=0; i<fields.size(); i++) {
FieldDescriptor fd=(FieldDescriptor)fields.get(i);
- if (fd.getType().isClass()||fd.getType().isArray())
+ if (state.MGC && fd.getType().isClass()
+ && fd.getType().getClassDesc().isEnum()) {
+ classdefout.println(" int " + fd.getSafeSymbol() + ";");
+ } else if (fd.getType().isClass()||fd.getType().isArray())
classdefout.println(" struct "+fd.getType().getSafeSymbol()+" * "+fd.getSafeSymbol()+";");
else if ((state.MGC) && (fd.isStatic())) {
// TODO add version for normal Java later
TypeDescriptor type=td.getType();
if (type.isNull())
output.println(" void * "+td.getSafeSymbol()+";");
- else if (type.isClass()||type.isArray())
+ else if (state.MGC && type.isClass() && type.getClassDesc().isEnum()) {
+ output.println(" int " + td.getSafeSymbol() + ";");
+ } else if (type.isClass()||type.isArray())
output.println(" struct "+type.getSafeSymbol()+" * "+td.getSafeSymbol()+";");
else
output.println(" "+type.getSafeSymbol()+" "+td.getSafeSymbol()+";");
output.println(generateTemp(fm, ffn.getDst(),lb)+"=*"+ generateTemp(fm,ffn.getSrc(),lb)+"->"+ ffn.getField().getSafeSymbol()+";");
}
//output.println(generateTemp(fm, ffn.getDst(),lb)+"=global_defs_p->"+ffn.getSrc().getType().getClassDesc().getSafeSymbol()+"->"+ ffn.getField().getSafeSymbol()+";");
- } else {
+ } else if (ffn.getField().isEnum()) {
+ // an Enum value, directly replace the field access as int
+ output.println(generateTemp(fm, ffn.getDst(), lb) + "=" + ffn.getField().enumValue() + ";");
+ } else {
output.println(generateTemp(fm, ffn.getDst(),lb)+"="+ generateTemp(fm,ffn.getSrc(),lb)+"->"+ ffn.getField().getSafeSymbol()+";");
}
} else {
if(cn != null) {
ecd.setSurroundingClass(cn.getSymbol());
ecd.setSurrounding(cn);
+ cn.addEnum(ecd);
}
- cn.addEnum(ecd);
if (!(ecd.getSymbol().equals(TypeUtil.ObjectClass)||
ecd.getSymbol().equals(TypeUtil.TagClass))) {
ecd.setSuper(TypeUtil.ObjectClass);
checkField(cd,fd);
}
+ boolean hasConstructor = false;
for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
MethodDescriptor md=(MethodDescriptor)method_it.next();
checkMethod(cd,md);
+ hasConstructor |= md.isConstructor();
+ }
+ if(!hasConstructor) {
+ // add a default constructor for this class
+ MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
+ cd.getSymbol(), false);
+ BlockNode bn=new BlockNode();
+ state.addTreeCode(md,bn);
+ cd.addMethod(md);
+ checkMethod(cd,md);
}
}
}
FieldDescriptor fd=null;
if (ltd.isArray()&&fieldname.equals("length"))
fd=FieldDescriptor.arrayLength;
- else
+ else
fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
if(state.MGC) {
// TODO add version for normal Java later
if(ltd.isStatic()) {
- // check if this field is a static field
- if(!fd.isStatic()) {
+ if(ltd.getClassDesc().isEnum()) {
+ int value = ltd.getClassDesc().getEnumConstant(fieldname);
+ if(-1 == value) {
+ // check if this field is an enum constant
+ throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
+ }
+ fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
+ fd.setAsEnum();
+ fd.setEnumValue(value);
+ } else if(!fd.isStatic()) {
+ // check if this field is a static field
throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
}
}
ExpressionNode en=min.getArg(i);
checkExpressionNode(md,nametable,en,null);
tdarray[i]=en.getType();
+ if(state.MGC && en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
+ tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
+ }
}
TypeDescriptor typetolookin=null;
if (min.getExpression()!=null) {
}
public boolean isSuperorType(TypeDescriptor possiblesuper, TypeDescriptor cd2) {
+ if(state.MGC) {
+ if(possiblesuper.isClass() && possiblesuper.class_desc.isEnum() && cd2.isInt()) {
+ return true;
+ }
+ }
if (possiblesuper.isOffset() || cd2.isOffset()) return true;
//Matching type are always okay
if (possiblesuper.equals(cd2))
if (s.equals("false")) return new BooleanLiteral(false);
// Check against keywords.
// pre-java 1.5 compatibility:
- if (!isJava15 && s.equals("enum")) return new Identifier(s);
+ //if (!isJava15 && s.equals("enum")) return new Identifier(s);
// pre-java 1.4 compatibility:
if (!isJava14 && s.equals("assert")) return new Identifier(s);
// pre-java 1.2 compatibility:
{: RESULT=eco; :}
;
enum_constants_opt ::=
- {: RESULT=new ParseNode("empty"); :}
+ {: RESULT=new ParseNode("empty"); :}
| enum_constants:ecs
{: RESULT=ecs; :}
;
dotest ReadFile ReadFile.java
dotest FileLength FileLength.java
dotest IntegerTest IntegerTest.java
-dotest InitializerTest InitializerTest.java
\ No newline at end of file
+#dotest InitializerTest InitializerTest.java
public class EnumTest {
- public EnumTest(){}
+ public Spiciness1 howHot;
+
+ public enum Spiciness1 {
+ NOT, FLAMING, MILD, MEDIUM, HOT
+ } ///:~
+
+ //public EnumTest(){}
public static void main(String[] args) {
Spiciness howHot = Spiciness.MEDIUM;
System.out.println(howHot);
+
+ EnumTest et = new EnumTest();
+ et.howHot = Spiciness1.MEDIUM;
+ System.out.println(et.howHot);
}
} /* Output:
MEDIUM