Don't allocate type number for interfaces so that we do not need allocate lines in...
authorjzhou <jzhou>
Thu, 28 Oct 2010 18:58:57 +0000 (18:58 +0000)
committerjzhou <jzhou>
Thu, 28 Oct 2010 18:58:57 +0000 (18:58 +0000)
Robust/src/IR/ClassDescriptor.java
Robust/src/IR/Flat/BuildCode.java
Robust/src/IR/State.java
Robust/src/IR/Tree/BuildIR.java
Robust/src/IR/Virtual.java
Robust/src/Parse/java14.cup
Robust/src/Tests/InterfaceTest.java

index 6c3735b283105feae79bcbf61546ce79cf7ac6a6..d16940e49af6a32cca6960a7becfcdd71a7113f7 100644 (file)
@@ -21,22 +21,25 @@ public class ClassDescriptor extends Descriptor {
   int numstaticfields = 0;
   
   // for interfaces
-  boolean isInterface=false;
   Vector<String> superinterfaces;
   SymbolTable superIFdesc;
 
-  public ClassDescriptor(String classname) {
-    this("", classname);
+  public ClassDescriptor(String classname, boolean isInterface) {
+    this("", classname, isInterface);
   }
 
-  public ClassDescriptor(String packagename, String classname) {
+  public ClassDescriptor(String packagename, String classname, boolean isInterface) {
     super(classname);
     superclass=null;
     flags=new SymbolTable();
     fields=new SymbolTable();
     fieldvec=new Vector();
     methods=new SymbolTable();
-    classid=UIDCount++;
+    if(isInterface) {
+      classid = -2;
+    } else {
+      classid=UIDCount++;
+    }
     this.packagename=packagename;
     superinterfaces = new Vector<String>();
     superIFdesc = new SymbolTable();
@@ -212,10 +215,6 @@ public class ClassDescriptor extends Descriptor {
   }
   
   public boolean isInterface() {
-    return this.isInterface;
-  }
-  
-  public void setAsInterface() {
-    this.isInterface = true;
+    return this.classid == -2;
   }
 }
index 8e80c4f6e38ef00b987e763079bc74280706fd02..19e884d6855a79a09234c5a58c5ff4e012fc04ff 100644 (file)
@@ -1150,6 +1150,9 @@ public class BuildCode {
     classit=state.getClassSymbolTable().getDescriptorsIterator();
     while(classit.hasNext()) {
       ClassDescriptor cd=(ClassDescriptor)classit.next();
+      if(cd.isInterface()) {
+        continue;
+      }
       if (state.DSM||state.SINGLETM)
        fillinRow(cd, lbvirtualtable, cd.getId());
       else
@@ -1272,7 +1275,9 @@ public class BuildCode {
     cdarray[0] = null;
     while(it.hasNext()) {
       ClassDescriptor cd=(ClassDescriptor)it.next();
-      cdarray[cd.getId()]=cd;
+      if(!cd.isInterface()) {
+        cdarray[cd.getId()]=cd;
+      }
     }
 
     arraytable=new TypeDescriptor[state.numArrays()];
index cf36777042903ba82e7c2bef4a5fa91a1cff910f..459021b14e5c5cdf1a573b6f14480ea72f69e99e 100644 (file)
@@ -229,7 +229,9 @@ public class State {
     if (classes.contains(tdn.getSymbol()))
       throw new Error("Class "+tdn.getSymbol()+" defined twice");
     classes.add(tdn);
-    numclasses++;
+    if(!tdn.isInterface()) {
+      numclasses++;
+    }
     if((tdn.numstaticfields != 0) || (tdn.numstaticblocks != 0)) {
       sclasses.add(tdn);
     }
index ddae6ac02042445defe27975d8d093e6caef9381..f1372753f6ee0f72ea519da589c2939ce2b042db 100644 (file)
@@ -74,8 +74,8 @@ public class BuildIR {
   }
   
   public ClassDescriptor parseInterfaceDecl(ParseNode pn) {
-    ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
-    cn.setAsInterface();
+    ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
+    //cn.setAsInterface();
     if (!isEmpty(pn.getChild("superIF").getTerminal())) {
       /* parse inherited interface name */
       ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
@@ -307,7 +307,7 @@ public class BuildIR {
   }
 
   public ClassDescriptor parseTypeDecl(ParseNode pn) {
-    ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
+    ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
     if (!isEmpty(pn.getChild("super").getTerminal())) {
       /* parse superclass name */
       ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
index 907ce865c8e4ac7a29a5f18c23cf587fcb09bc3a..87abaaecc7a54fca7be52867443583df5a0a0740 100644 (file)
@@ -109,43 +109,43 @@ public class Virtual {
     for(Iterator it=cd.getMethods(); it.hasNext();) {
       MethodDescriptor md=(MethodDescriptor)it.next();
       if (md.isStatic()||md.getReturnType()==null)
-       continue;
+        continue;
       if (superdesc!=null) {
-       Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
-       boolean foundmatch=false;
-       for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
-         MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
-         if (md.matches(matchmd)) {
-           int num=((Integer)methodnumber.get(matchmd)).intValue();
-           methodnumber.put(md, new Integer(num));
-           foundmatch=true;
-           break;
-         }
-       }
-    if(state.MGC) {
-      // TODO add version for normal Java later
-      if(!foundmatch) {
-        // check if there is a matched method in inherited interfaces
-        Iterator it_sifs = cd.getSuperInterfaces();
-        while(it_sifs.hasNext() && !foundmatch) {
-          ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
-          Set possiblematches_if=superif.getMethodTable().getSet(md.getSymbol());
-          for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
-            MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
-            if (md.matches(matchmd)) {
-              int num=((Integer)methodnumber.get(matchmd)).intValue();
-              methodnumber.put(md, new Integer(num));
-              foundmatch=true;
-              break;
+        Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
+        boolean foundmatch=false;
+        for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
+          MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+          if (md.matches(matchmd)) {
+            int num=((Integer)methodnumber.get(matchmd)).intValue();
+            methodnumber.put(md, new Integer(num));
+            foundmatch=true;
+            break;
+          }
+        }
+        if(state.MGC) {
+          // TODO add version for normal Java later
+          if(!foundmatch) {
+            // check if there is a matched method in inherited interfaces
+            Iterator it_sifs = cd.getSuperInterfaces();
+            while(it_sifs.hasNext() && !foundmatch) {
+              ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
+              Set possiblematches_if=superif.getMethodTable().getSet(md.getSymbol());
+              for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
+                MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+                if (md.matches(matchmd)) {
+                  int num=((Integer)methodnumber.get(matchmd)).intValue();
+                  methodnumber.put(md, new Integer(num));
+                  foundmatch=true;
+                  break;
+                }
+              }
             }
           }
         }
-      }
-    }
-       if (!foundmatch)
-         methodnumber.put(md, new Integer(start++));
+        if (!foundmatch)
+          methodnumber.put(md, new Integer(start++));
       } else {
-       methodnumber.put(md, new Integer(start++));
+        methodnumber.put(md, new Integer(start++));
       }
     }
     classmethodcount.put(cd, new Integer(start));
index c1b5470aec617a09116e123087745fcced8e694e..866831ee50a979bd2b5c40321b1451a9e1c53bac 100644 (file)
@@ -820,7 +820,16 @@ class_member_declaration ::=
        RESULT=(new ParseNode("method")).addChild(method).getRoot(); 
        :}
        /* repeat the prod for 'class_declaration' here: */
-//     |       modifiers_opt CLASS IDENTIFIER super_opt class_body
+//     |       modifiers_opt:mo CLASS IDENTIFIER:id super_opt:so interfaces_opt:ifo class_body:body
+//     {:
+//     ParseNode pn=new ParseNode("inner_class_declaration");
+//     pn.addChild("modifiers").addChild(mo);
+//     pn.addChild("name").addChild(id);
+//     pn.addChild("super").addChild(so);
+//     pn.addChild("superIF").addChild(ifo);
+//     pn.addChild("classbody").addChild(body);
+//     RESULT=pn;
+//     :}
     |       interface_declaration:interfaced {: 
        RESULT=(new ParseNode("interface")).addChild(interfaced).getRoot(); 
        :}
index 80eec8f8903b2af746da65c993fd06192defcbf4..d1a93f5554505b5fe3cbaa685060b65b7c09bf66 100644 (file)
@@ -6,7 +6,12 @@ public interface Instrument {
   void adjust();
 }
 
-class Wind implements Instrument {
+public interface Instrument2 {
+  // Cannot have method definitions:
+  void play(int n); // Automatically public
+}
+
+class Wind implements Instrument,Instrument2 {
   public Wind(){}
   public void play(int n) {
     System.out.println("Wind.play() " + n);
@@ -15,7 +20,7 @@ class Wind implements Instrument {
   public void adjust() { System.out.println("Wind.adjust()"); }
 }
 
-class Percussion implements Instrument {
+class Percussion implements Instrument,Instrument2 {
   public Percussion(){}
   public void play(int n) {
     System.out.println("Percussion.play() " + n);
@@ -24,7 +29,7 @@ class Percussion implements Instrument {
   public void adjust() { System.out.println("Percussion.adjust()"); }
 }
 
-class Stringed implements Instrument {
+class Stringed implements Instrument,Instrument2 {
   public Stringed(){}
   public void play(int n) {
     System.out.println("Stringed.play() " + n);
@@ -58,6 +63,16 @@ public class InterfaceTest {
       tune(i);
     }
   }
+  static void tune2(Instrument2 i) {
+    // ...
+    i.play(9);
+  }
+  static void tuneAll2(Instrument2[] e) {
+    for(int k = 0; k < e.length; k++) {
+      Instrument2 i = e[k];
+      tune2(i);
+    }
+  }
   public static void main(String[] args) {
     // Upcasting during addition to the array:
     Instrument.VALUE=5;
@@ -68,6 +83,13 @@ public class InterfaceTest {
     orchestra[3] = new Brass();
     orchestra[4] = new Woodwind();
     tuneAll(orchestra);
+    Instrument2[] orchestra2 = new Instrument2[5];
+    orchestra2[0] = new Wind();
+    orchestra2[1] = new Percussion();
+    orchestra2[2] = new Stringed();
+    orchestra2[3] = new Brass();
+    orchestra2[4] = new Woodwind();
+    tuneAll2(orchestra2);
   }
 } /* Output:
 Wind.play() MIDDLE_C