implemented PCLOC annotation.
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
index a12dcf0a36aa0595d9c70d0598a4969b12f15c0b..c8efb8e7bc29898770e26fdbb7b1bb8a90733140 100644 (file)
@@ -184,18 +184,41 @@ public class BuildIR {
 
   public void parseInitializers(ClassDescriptor cn) {
     Vector fv=cn.getFieldVec();
+    Iterator methodit = cn.getMethods();
+    HashMap<MethodDescriptor, Integer> md2pos = new HashMap<MethodDescriptor, Integer>();
+    while(methodit.hasNext()) {
+      MethodDescriptor currmd=(MethodDescriptor)methodit.next();
+      if(currmd.isConstructor()) {
+        BlockNode bn=state.getMethodBody(currmd);
+        // if there are super(...) invokation, the initializers should be invoked after that
+        int i = 0;
+        for(; i < bn.size(); i++) {
+          if(Kind.BlockExpressionNode==bn.get(i).kind()
+                 &&(((BlockExpressionNode)bn.get(i)).getExpression() instanceof MethodInvokeNode)
+                 &&((MethodInvokeNode)(((BlockExpressionNode)bn.get(i)).getExpression())).getMethodName().equals("super")) {
+            break;
+          }
+        }
+        if(i==bn.size()) {
+          md2pos.put(currmd, 0);
+        } else {
+          md2pos.put(currmd, i+1);
+        }
+      }
+    }
     int pos = 0;
     for(int i=0; i<fv.size(); i++) {
       FieldDescriptor fd=(FieldDescriptor)fv.get(i);
       if(fd.getExpressionNode()!=null) {
-        Iterator methodit = cn.getMethods();
+        methodit = cn.getMethods();
         while(methodit.hasNext()) {
           MethodDescriptor currmd=(MethodDescriptor)methodit.next();
           if(currmd.isConstructor()) {
+            int offset = md2pos.get(currmd);
             BlockNode bn=state.getMethodBody(currmd);
             NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
             AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
-            bn.addBlockStatementAt(new BlockExpressionNode(an), pos);
+            bn.addBlockStatementAt(new BlockExpressionNode(an), pos+offset);
           }
         }
         pos++;
@@ -230,10 +253,7 @@ public class BuildIR {
     ecd.setModifiers(parseModifiersList(pn.getChild("modifiers")));
     parseEnumBody(ecd, pn.getChild("enumbody"));
 
-    if (analyzeset != null)
-      analyzeset.add(ecd);
-    ecd.setSourceFileName(currsourcefile);
-    state.addClass(ecd);
+    addClass2State(ecd);
 
     popChainMaps();
     return ecd;
@@ -267,10 +287,7 @@ public class BuildIR {
     parseAnnotationTypeBody(cn,pn.getChild("body"));
     popChainMaps();
 
-    if (analyzeset != null)
-      analyzeset.add(cn);
-    cn.setSourceFileName(currsourcefile);
-    state.addClass(cn);
+    addClass2State(cn);
 
     return cn;
   }
@@ -332,10 +349,7 @@ public class BuildIR {
     }
     cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
     parseInterfaceBody(cn, pn.getChild("interfacebody"));
-    if (analyzeset != null)
-      analyzeset.add(cn);
-    cn.setSourceFileName(currsourcefile);
-    state.addClass(cn);
+    addClass2State(cn);
     popChainMaps();
     return cn;
   }
@@ -546,6 +560,22 @@ public class BuildIR {
     return tel;
   }
 
+  private void addClass2State(ClassDescriptor cn) {
+    if (analyzeset != null)
+      analyzeset.add(cn);
+    cn.setSourceFileName(currsourcefile);
+    state.addClass(cn);
+    // create this$n representing a final reference to the next surrounding class. each inner class should have whatever inner class
+    // pointers the surrounding class has + a pointer to the surrounding class.
+    if( true )
+    {
+      this.isRunningRecursiveInnerClass = true; //fOR dEBUGGING PURPOSES IN ORDER TO DUMP STRINGS WHILE IN THIS CODE PATH
+      addOuterClassReferences( cn, cn, 0 );
+      addOuterClassParam( cn, cn, 0 );
+      this.isRunningRecursiveInnerClass = false;
+    }
+  }
+  
   public ClassDescriptor parseTypeDecl(ParseNode pn) {
     ClassDescriptor cn=new ClassDescriptor(packageName, pn.getChild("name").getTerminal(), false);
     pushChainMaps();
@@ -595,21 +625,9 @@ public class BuildIR {
     popChainMaps();
 
     cn.setSourceFileName(currsourcefile);
-
-
     
-    if (analyzeset != null)
-      analyzeset.add(cn);
-    state.addClass(cn);
-//create this$n representing a final reference to the next surrounding class. each inner class should have whatever inner class
-//pointers the surrounding class has + a pointer to the surrounding class.
-   if( true )
-   {
-       this.isRunningRecursiveInnerClass = true; //fOR dEBUGGING PURPOSES IN ORDER TO DUMP STRINGS WHILE IN THIS CODE PATH
-       addOuterClassReferences( cn, 0 );
-       addOuterClassParam( cn, 0 );
-       this.isRunningRecursiveInnerClass = false;
-    }
+    addClass2State(cn);
+
     return cn;
   }
 
@@ -625,14 +643,14 @@ private void initializeOuterMember( MethodDescriptor md, String fieldName, Strin
          state.addTreeCode(md, obn);
 }
 
-private void addOuterClassParam( ClassDescriptor cn, int depth )
+private void addOuterClassParam( ClassDescriptor cn, ClassDescriptor ocn, int depth )
 {
        Iterator nullCheckItr = cn.getInnerClasses();
        if( false == nullCheckItr.hasNext() )
                return;
 
        //create a typedescriptor of type cn
-       TypeDescriptor theTypeDesc = new TypeDescriptor( cn );
+       TypeDescriptor theTypeDesc = new TypeDescriptor( ocn );
        
        for(Iterator it=cn.getInnerClasses(); it.hasNext(); ) {
                ClassDescriptor icd=(ClassDescriptor)it.next();
@@ -644,24 +662,24 @@ private void addOuterClassParam( ClassDescriptor cn, int depth )
                for(Iterator method_it=icd.getMethods(); method_it.hasNext(); ) {
                         MethodDescriptor md=(MethodDescriptor)method_it.next();
                         if( md.isConstructor() ){
-                               md.addParameter( theTypeDesc, "surrounding$" + String.valueOf(depth) ); 
-                               initializeOuterMember( md, "this$" + String.valueOf( depth ), "surrounding$" + String.valueOf(depth) );
-                               //System.out.println( "The added param is " + md.toString() + "\n" );           
+                               md.addParameter( theTypeDesc, "surrounding___DOLLAR___" + String.valueOf(depth) );      
+                               initializeOuterMember( md, "this___DOLLAR___" + String.valueOf( depth ), "surrounding___DOLLAR___" + String.valueOf(depth) );
+                               //System.out.println( "The added param is " + md.toString() + "\n" );
                        }
                }
-               addOuterClassParam( icd, depth + 1 );
+               addOuterClassParam( icd, ocn, depth + 1 );
                
        }
        
 }
-private void addOuterClassReferences( ClassDescriptor cn, int depth )
+private void addOuterClassReferences( ClassDescriptor cn, ClassDescriptor ocn, int depth )
 {
        //SYMBOLTABLE does not have a length or empty method, hence could not define a hasInnerClasses method in classDescriptor
        Iterator nullCheckItr = cn.getInnerClasses();
        if( false == nullCheckItr.hasNext() )
                return;
 
-       String tempCopy = cn.getClassName();
+       String tempCopy = ocn.getClassName();
        //MESSY HACK FOLLOWS
        int i = 0;
 
@@ -673,7 +691,7 @@ private void addOuterClassReferences( ClassDescriptor cn, int depth )
        tempChildNode.addChild("identifier").addChild ( tempCopy );
        theNode.addChild("type").addChild( theTypeNode );
        ParseNode variableDeclaratorID = new ParseNode("single");
-       String theStr = "this$" + String.valueOf( depth );
+       String theStr = "this___DOLLAR___" + String.valueOf( depth );
        variableDeclaratorID.addChild( theStr );
        ParseNode variableDeclarator = new ParseNode( "variable_declarator" );
        variableDeclarator.addChild( variableDeclaratorID );
@@ -692,7 +710,7 @@ private void addOuterClassReferences( ClassDescriptor cn, int depth )
                        //System.out.println( fieldTable.toString() );
                }*/
                icd.setInnerDepth( depth + 1 );
-               addOuterClassReferences( icd, depth + 1 );      
+               addOuterClassReferences( icd, ocn, depth + 1 ); 
        }
 }
 
@@ -821,10 +839,7 @@ private void addOuterClassReferences( ClassDescriptor cn, int depth )
    }
     popChainMaps();
 
-     if (analyzeset != null)
-      analyzeset.add(icn);
-    icn.setSourceFileName(currsourcefile);
-    state.addClass(icn);
+    addClass2State(icn);
 
     return icn;
   }
@@ -1183,10 +1198,7 @@ private void addOuterClassReferences( ClassDescriptor cn, int depth )
       }
       popChainMaps();
 
-      if (analyzeset != null)
-       analyzeset.add(cnnew);
-      cnnew.setSourceFileName(currsourcefile);
-      state.addClass(cnnew);
+      addClass2State(cnnew);
 
       return con;
     } else if (isNode(pn,"createarray")) {