From 272974eec802562039323ef1d05b65d3114d7f68 Mon Sep 17 00:00:00 2001 From: jzhou Date: Thu, 24 Feb 2011 20:09:26 +0000 Subject: [PATCH] Fix a bug of default constructor: the default constructor should construct the super class if there is any and also initialize the fields with initializers. --- Robust/src/IR/MethodDescriptor.java | 12 ++++++++++++ Robust/src/IR/Tree/BuildIR.java | 15 +++++++++++++++ Robust/src/IR/Tree/SemanticCheck.java | 21 +++++++++------------ 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Robust/src/IR/MethodDescriptor.java b/Robust/src/IR/MethodDescriptor.java index 3f58a7ce..00842897 100644 --- a/Robust/src/IR/MethodDescriptor.java +++ b/Robust/src/IR/MethodDescriptor.java @@ -21,6 +21,8 @@ public class MethodDescriptor extends Descriptor { protected boolean isglobal; protected boolean isstaticblock; // flag to indicate if this is a static block protected boolean isinvokedbystatic; // flag to indicate if this method is invoked by some static block + + protected boolean isdefaultconstructor; // flag to indicate if this is a default constructor public MethodDescriptor(Modifiers m, TypeDescriptor rt, String identifier) { super(identifier); @@ -33,6 +35,8 @@ public class MethodDescriptor extends Descriptor { paramtable=new SymbolTable(); thisvd=null; isstaticblock = false; + this.isinvokedbystatic = false; + this.isdefaultconstructor = false; } public Modifiers getModifiers() { @@ -208,4 +212,12 @@ public class MethodDescriptor extends Descriptor { st+=")"; return st; } + + public boolean isDefaultConstructor() { + return this.isdefaultconstructor; + } + + public void setDefaultConstructor() { + this.isdefaultconstructor = true; + } } diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java index 7d7121e5..4e9c36e3 100644 --- a/Robust/src/IR/Tree/BuildIR.java +++ b/Robust/src/IR/Tree/BuildIR.java @@ -448,6 +448,21 @@ public class BuildIR { } cn.setModifiers(parseModifiersList(pn.getChild("modifiers"))); parseClassBody(cn, pn.getChild("classbody")); + + boolean hasConstructor = false; + for(Iterator method_it=cn.getMethods(); method_it.hasNext();) { + MethodDescriptor md=(MethodDescriptor)method_it.next(); + hasConstructor |= md.isConstructor(); + } + if((!hasConstructor) && (!cn.isEnum())) { + // add a default constructor for this class + MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC), + cn.getSymbol(), false); + BlockNode bn=new BlockNode(); + state.addTreeCode(md,bn); + md.setDefaultConstructor(); + cn.addMethod(md); + } return cn; } diff --git a/Robust/src/IR/Tree/SemanticCheck.java b/Robust/src/IR/Tree/SemanticCheck.java index 7e475f72..97dec032 100644 --- a/Robust/src/IR/Tree/SemanticCheck.java +++ b/Robust/src/IR/Tree/SemanticCheck.java @@ -64,20 +64,17 @@ public class SemanticCheck { 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) && (!cd.isEnum())) { - // 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); + MethodDescriptor md=(MethodDescriptor)method_it.next(); checkMethod(cd,md); + if(md.isDefaultConstructor() && (cd.getSuperDesc() != null)) { + // add the construction of it super class, can only be super() + NameDescriptor nd=new NameDescriptor("super"); + MethodInvokeNode min=new MethodInvokeNode(nd); + BlockExpressionNode ben=new BlockExpressionNode(min); + BlockNode bn = state.getMethodBody(md); + bn.addFirstBlockStatement(ben); + } } } } -- 2.34.1