added TODO comments to BuildIR to implement import and packages
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
index 037232e00e796f25ff48437aa91bf3263d4a2034..9e2da3b6433d75d1179f9aa9da285d9e7a54663e 100644 (file)
@@ -1,9 +1,8 @@
 package IR.Tree;
 import IR.*;
-
+import Util.Lattice;
 import java.util.*;
 
-
 public class BuildIR {
   State state;
 
@@ -14,8 +13,18 @@ public class BuildIR {
     this.m_taskexitnum = 0;
   }
 
-  public void buildtree(ParseNode pn, Set toanalyze) {
-    parseFile(pn, toanalyze);
+  public void buildtree(ParseNode pn, Set toanalyze, String sourcefile) {
+    parseFile(pn, toanalyze,sourcefile);
+    
+    // numering the interfaces
+    int if_num = 0;
+    Iterator it_classes = state.getClassSymbolTable().getValueSet().iterator();
+    while(it_classes.hasNext()) {
+      ClassDescriptor cd = (ClassDescriptor)it_classes.next();
+      if(cd.isInterface()) {
+        cd.setInterfaceId(if_num++);
+      }
+    }
   }
 
   Vector singleimports;
@@ -23,16 +32,17 @@ public class BuildIR {
   NameDescriptor packages;
 
   /** Parse the classes in this file */
-  public void parseFile(ParseNode pn, Set toanalyze) {
+  public void parseFile(ParseNode pn, Set toanalyze,String sourcefile) {
     singleimports=new Vector();
     multiimports=new Vector();
-
+    
     ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
     if (ipn!=null) {
       ParseNodeVector pnv=ipn.getChildren();
       for(int i=0; i<pnv.size(); i++) {
        ParseNode pnimport=pnv.elementAt(i);
        NameDescriptor nd=parseName(pnimport.getChild("name"));
+       // TODO need to implement
        if (isNode(pnimport,"import_single"))
          singleimports.add(nd);
        else
@@ -41,6 +51,7 @@ public class BuildIR {
     }
     ParseNode ppn=pn.getChild("packages").getChild("package");
     if (ppn!=null) {
+       // TODO need to implement
       packages=parseName(ppn.getChild("name"));
     }
     ParseNode tpn=pn.getChild("type_declaration_list");
@@ -52,108 +63,111 @@ public class BuildIR {
          continue;
        if (isNode(type_pn,"class_declaration")) {
          ClassDescriptor cn=parseTypeDecl(type_pn);
+         cn.setSourceFileName(sourcefile);
          parseInitializers(cn);
          if (toanalyze!=null)
            toanalyze.add(cn);
          state.addClass(cn);
-      // for inner classes/enum
-      if(state.MGC) {
-        // TODO add version for normal Java later
-        HashSet tovisit = new HashSet();
-        Iterator it_icds = cn.getInnerClasses();
-        while(it_icds.hasNext()) {
-          tovisit.add(it_icds.next());
-        }
-        
-        while(!tovisit.isEmpty()) {
-          ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
-          tovisit.remove(cd);
-         parseInitializers(cd);
-          if(toanalyze != null) {
-            toanalyze.add(cd);
-          }
-          state.addClass(cd);
-          
-          Iterator it_ics = cd.getInnerClasses();
-          while(it_ics.hasNext()) {
-            tovisit.add(it_ics.next());
-          }
-          
-          Iterator it_ienums = cd.getEnum();
-          while(it_ienums.hasNext()) {
-            ClassDescriptor iecd = (ClassDescriptor)it_ienums.next();
-            if(toanalyze != null) {
-              toanalyze.add(iecd);
-            }
-            state.addClass(iecd);
-          }
-        }
-        
-        Iterator it_enums = cn.getEnum();
-        while(it_enums.hasNext()) {
-          ClassDescriptor ecd = (ClassDescriptor)it_enums.next();
-          if(toanalyze != null) {
-            toanalyze.add(ecd);
-          }
-          state.addClass(ecd);
-        }
-      }
+         // for inner classes/enum
+         HashSet tovisit = new HashSet();
+         Iterator it_icds = cn.getInnerClasses();
+         while(it_icds.hasNext()) {
+           tovisit.add(it_icds.next());
+         }
+         
+         while(!tovisit.isEmpty()) {
+           ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
+           tovisit.remove(cd);
+           parseInitializers(cd);
+           if(toanalyze != null) {
+             toanalyze.add(cd);
+           }
+           cd.setSourceFileName(sourcefile);
+           state.addClass(cd);
+           
+           Iterator it_ics = cd.getInnerClasses();
+           while(it_ics.hasNext()) {
+             tovisit.add(it_ics.next());
+           }
+           
+           Iterator it_ienums = cd.getEnum();
+           while(it_ienums.hasNext()) {
+             ClassDescriptor iecd = (ClassDescriptor)it_ienums.next();
+             if(toanalyze != null) {
+               toanalyze.add(iecd);
+             }
+             iecd.setSourceFileName(sourcefile);
+             state.addClass(iecd);
+           }
+         }
+         
+         Iterator it_enums = cn.getEnum();
+         while(it_enums.hasNext()) {
+           ClassDescriptor ecd = (ClassDescriptor)it_enums.next();
+           if(toanalyze != null) {
+             toanalyze.add(ecd);
+           }
+           ecd.setSourceFileName(sourcefile);
+           state.addClass(ecd);
+         }
        } else if (isNode(type_pn,"task_declaration")) {
          TaskDescriptor td=parseTaskDecl(type_pn);
          if (toanalyze!=null)
            toanalyze.add(td);
          state.addTask(td);
-       } else if ((state.MGC) && isNode(type_pn,"interface_declaration")) {
-      // TODO add version for normal Java later
-      ClassDescriptor cn = parseInterfaceDecl(type_pn);
-      if (toanalyze!=null)
-        toanalyze.add(cn);
-      state.addClass(cn);
-      
-      // for enum
-      if(state.MGC) {
-        // TODO add version for normal Java later        
-        Iterator it_enums = cn.getEnum();
-        while(it_enums.hasNext()) {
-          ClassDescriptor ecd = (ClassDescriptor)it_enums.next();
-          if(toanalyze != null) {
-            toanalyze.add(ecd);
-          }
-          state.addClass(ecd);
-        }
-      }
-    } else if ((state.MGC) && isNode(type_pn,"enum_declaration")) {
-      // TODO add version for normal Java later
-      ClassDescriptor cn = parseEnumDecl(null, type_pn);
-      if (toanalyze!=null)
-        toanalyze.add(cn);
-      state.addClass(cn);
-    } else {
+       } else if (isNode(type_pn,"interface_declaration")) {
+         // TODO add version for normal Java later
+         ClassDescriptor cn = parseInterfaceDecl(type_pn);
+         if (toanalyze!=null)
+           toanalyze.add(cn);
+         cn.setSourceFileName(sourcefile);
+         state.addClass(cn);
+         
+         // for enum
+         Iterator it_enums = cn.getEnum();
+         while(it_enums.hasNext()) {
+           ClassDescriptor ecd = (ClassDescriptor)it_enums.next();
+           if(toanalyze != null) {
+             toanalyze.add(ecd);
+           }
+           ecd.setSourceFileName(sourcefile);
+           state.addClass(ecd);
+         }
+       } else if (isNode(type_pn,"enum_declaration")) {
+         // TODO add version for normal Java later
+         ClassDescriptor cn = parseEnumDecl(null, type_pn);
+         if (toanalyze!=null)
+           toanalyze.add(cn);
+         cn.setSourceFileName(sourcefile);
+         state.addClass(cn);
+       } else {
          throw new Error(type_pn.getLabel());
        }
       }
     }
   }
 
- public void parseInitializers(ClassDescriptor cn){
-       Vector fv=cn.getFieldVec();
-       for(int i=0;i<fv.size();i++) {
-           FieldDescriptor fd=(FieldDescriptor)fv.get(i);
-           if(fd.getExpressionNode()!=null) {
-               Iterator methodit = cn.getMethods();
-               while(methodit.hasNext()){
-                   MethodDescriptor currmd=(MethodDescriptor)methodit.next();
-                   if(currmd.isConstructor()){
-                       BlockNode bn=state.getMethodBody(currmd);
-                       NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
-                       AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
-                       bn.addFirstBlockStatement(new BlockExpressionNode(an));                 
-                   }
-               }
-           }
+  public void parseInitializers(ClassDescriptor cn){
+    Vector fv=cn.getFieldVec();
+    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();
+       while(methodit.hasNext()){
+         MethodDescriptor currmd=(MethodDescriptor)methodit.next();
+         if(currmd.isConstructor()){
+           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);
+         }
        }
-    }  
-
+       pos++;
+      }
+    }
+  }  
+  
   private ClassDescriptor parseEnumDecl(ClassDescriptor cn, ParseNode pn) {
     ClassDescriptor ecd=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
     ecd.setAsEnum();
@@ -448,6 +462,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;
   }
 
@@ -462,16 +491,47 @@ public class BuildIR {
        } else if (isNode(decl,"constructor")) {
          parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
        } else if (isNode(decl, "static_block")) {
-      if(state.MGC) {
-        // TODO add version for normal Java later
-      parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
-      } else {
-        throw new Error("Static blocks not implemented");
+         parseStaticBlockDecl(cn, decl.getChild("static_block_declaration"));
+       } else if (isNode(decl,"block")) {
+       } else if (isNode(decl,"location_order_declaration")) {
+         parseLocationOrder(cn,decl.getChild("location_order_list"));
+  } else throw new Error();
+      }
+    }
+  }
+  
+  private void parseLocationOrder(ClassDescriptor cd, ParseNode pn) {
+    ParseNodeVector pnv = pn.getChildren();
+    Lattice<String> locOrder =
+        new Lattice<String>("_top_","_bottom_");            
+    Set<String> spinLocSet=new HashSet<String>();
+    for (int i = 0; i < pnv.size(); i++) {
+      ParseNode loc = pnv.elementAt(i);
+      if(isNode(loc,"location_property")){
+        String spinLoc=loc.getChildren().elementAt(0).getLabel();
+        spinLocSet.add(spinLoc);
+      }else{
+        String lowerLoc=loc.getChildren().elementAt(0).getLabel();
+        String higherLoc= loc.getChildren().elementAt(1).getLabel();
+        locOrder.put(higherLoc, lowerLoc);
+        if (locOrder.isIntroducingCycle(higherLoc)) {
+          throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
+              + " introduces a cycle.");
+        }
       }
-    } else if (isNode(decl,"block")) {
-       } else throw new Error();
+    }
+    if(spinLocSet.size()>0){
+      //checking if location is actually defined in the hierarchy
+      for (Iterator iterator = spinLocSet.iterator(); iterator.hasNext();) {
+        String locID = (String) iterator.next();
+        if(!locOrder.containsKey(locID)){
+          throw new Error("Error: The spinning location '"+
+              locID + "' is not defined in the hierarchy of the class '"+cd +"'.");
+        }
       }
+      state.addLocationPropertySet(cd, spinLocSet);
     }
+    state.addLocationOrder(cd, locOrder);
   }
   
   private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
@@ -487,29 +547,24 @@ public class BuildIR {
     }
     ParseNode innerclassnode=pn.getChild("inner_class_declaration");
     if (innerclassnode!=null) {
-      if(state.MGC){
-        parseInnerClassDecl(cn,innerclassnode);
-        return;
-      } else {
-        // TODO add version for noraml Java later
-        throw new Error("Error: inner class in Class " + cn.getSymbol() + " is not supported yet");
-      }
+      parseInnerClassDecl(cn,innerclassnode);
+      return;
     }
     ParseNode enumnode=pn.getChild("enum_declaration");
     if (enumnode!=null) {
-      if(state.MGC){
-        parseEnumDecl(cn,enumnode);
-        return;
-      } else {
-        // TODO add version for noraml Java later
-        throw new Error("Error: enumerated type in Class " + cn.getSymbol() + " is not supported yet");
-      }
+      parseEnumDecl(cn,enumnode);
+      return;
     }
     ParseNode flagnode=pn.getChild("flag");
     if (flagnode!=null) {
       parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
       return;
     }
+    // in case there are empty node
+    ParseNode emptynode=pn.getChild("empty");
+    if(emptynode != null) {
+      return;
+    }
     throw new Error();
   }
   
@@ -606,7 +661,7 @@ public class BuildIR {
   private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
     ParseNode mn=pn.getChild("modifier");
     Modifiers m=parseModifiersList(mn);
-    if((state.MGC) && cn.isInterface()) {
+    if(cn.isInterface()) {
       // TODO add version for normal Java later
       // Can only be PUBLIC or STATIC or FINAL
       if((m.isAbstract()) || (m.isAtomic()) || (m.isNative()) 
@@ -620,6 +675,7 @@ public class BuildIR {
 
     ParseNode tn=pn.getChild("type");
     TypeDescriptor t=parseTypeDescriptor(tn);
+    assignAnnotationsToType(m,t);
     ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
     ParseNodeVector pnv=vn.getChildren();
     boolean isglobal=pn.getChild("global")!=null;
@@ -636,12 +692,61 @@ public class BuildIR {
       ParseNode epn=vardecl.getChild("initializer");
 
       ExpressionNode en=null;
-      if (epn!=null)
-       en=parseExpression(epn.getFirstChild());
+      if (epn!=null) {
+        en=parseExpression(epn.getFirstChild());
+        en.setNumLine(epn.getFirstChild().getLine());
+        if(m.isStatic()) {
+          // for static field, the initializer should be considered as a 
+          // static block
+          boolean isfirst = false;
+          MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
+          if(md == null) {
+            // the first static block for this class
+            Modifiers m_i=new Modifiers();
+            m_i.addModifier(Modifiers.STATIC);
+            md = new MethodDescriptor(m_i, "staticblocks", false);
+            md.setAsStaticBlock();
+            isfirst = true;
+          }
+          if(isfirst) {
+            cn.addMethod(md);
+          }
+          cn.incStaticBlocks();
+          BlockNode bn=new BlockNode();
+          NameNode nn=new NameNode(new NameDescriptor(identifier));
+          nn.setNumLine(en.getNumLine());
+          AssignmentNode an=new AssignmentNode(nn,en,new AssignOperation(1));
+          an.setNumLine(pn.getLine());
+          bn.addBlockStatement(new BlockExpressionNode(an));
+          if(isfirst) {
+            state.addTreeCode(md,bn);
+          } else {
+            BlockNode obn = state.getMethodBody(md);
+            for(int ii = 0; ii < bn.size(); ii++) {
+              BlockStatementNode bsn = bn.get(ii);
+              obn.addBlockStatement(bsn);
+            }
+            state.addTreeCode(md, obn);
+            bn = null;
+          }
+          en = null;
+        }
+      }
 
       cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
     }
   }
+  
+  private void assignAnnotationsToType(Modifiers modifiers, TypeDescriptor type){
+    Vector<AnnotationDescriptor> annotations=modifiers.getAnnotations();
+    for(int i=0; i<annotations.size(); i++) {
+      // it only supports a marker annotation
+      AnnotationDescriptor an=annotations.elementAt(i);
+      type.addAnnotationMarker(an);           
+    }    
+  }
+
+  int innerCount=0;
 
   private ExpressionNode parseExpression(ParseNode pn) {
     if (isNode(pn,"assignment"))
@@ -660,31 +765,41 @@ public class BuildIR {
       ParseNode left=pnv.elementAt(0);
       ParseNode right=pnv.elementAt(1);
       Operation op=new Operation(pn.getLabel());
-      return new OpNode(parseExpression(left),parseExpression(right),op);
+      OpNode on=new OpNode(parseExpression(left),parseExpression(right),op);
+      on.setNumLine(pn.getLine());
+      return on;
     } else if (isNode(pn,"unaryplus")||
                isNode(pn,"unaryminus")||
                isNode(pn,"not")||
                isNode(pn,"comp")) {
       ParseNode left=pn.getFirstChild();
       Operation op=new Operation(pn.getLabel());
-      return new OpNode(parseExpression(left),op);
+      OpNode on=new OpNode(parseExpression(left),op);
+      on.setNumLine(pn.getLine());
+      return on;
     } else if (isNode(pn,"postinc")||
                isNode(pn,"postdec")) {
       ParseNode left=pn.getFirstChild();
       AssignOperation op=new AssignOperation(pn.getLabel());
-      return new AssignmentNode(parseExpression(left),null,op);
+      AssignmentNode an=new AssignmentNode(parseExpression(left),null,op);
+      an.setNumLine(pn.getLine());
+      return an;
 
     } else if (isNode(pn,"preinc")||
                isNode(pn,"predec")) {
       ParseNode left=pn.getFirstChild();
       AssignOperation op=isNode(pn,"preinc") ? new AssignOperation(AssignOperation.PLUSEQ) : new AssignOperation(AssignOperation.MINUSEQ);
-      return new AssignmentNode(parseExpression(left),
-                                new LiteralNode("integer",new Integer(1)),op);
+      AssignmentNode an=new AssignmentNode(parseExpression(left),
+          new LiteralNode("integer",new Integer(1)),op);
+      an.setNumLine(pn.getLine());
+      return an;
     } else if (isNode(pn,"literal")) {
       String literaltype=pn.getTerminal();
       ParseNode literalnode=pn.getChild(literaltype);
-      Object literal_obj=literalnode.getLiteral();
-      return new LiteralNode(literaltype, literal_obj);
+      Object literal_obj=literalnode.getLiteral();      
+      LiteralNode ln=new LiteralNode(literaltype, literal_obj);
+      ln.setNumLine(pn.getLine());
+      return ln;
     } else if (isNode(pn,"createobject")) {
       TypeDescriptor td=parseTypeDescriptor(pn);
       
@@ -696,6 +811,7 @@ public class BuildIR {
        disjointId = pn.getChild("disjoint").getTerminal();
       }
       CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
+      con.setNumLine(pn.getLine());
       for(int i=0; i<args.size(); i++) {
        con.addArgument((ExpressionNode)args.get(i));
       }
@@ -710,6 +826,22 @@ public class BuildIR {
        con.addFlagEffects(fe);
       }
 
+      return con;
+    } else if (isNode(pn,"createobjectcls")) {
+      //TODO:::  FIX BUG!!!  static fields in caller context need to become parameters
+      TypeDescriptor td=parseTypeDescriptor(pn);
+      innerCount++;
+      ClassDescriptor cnnew=new ClassDescriptor(td.getSymbol()+"$"+innerCount, false);
+      cnnew.setSuper(td.getSymbol());
+      parseClassBody(cnnew, pn.getChild("decl").getChild("classbody"));
+      Vector args=parseArgumentList(pn);
+
+      CreateObjectNode con=new CreateObjectNode(td, false, null);
+      con.setNumLine(pn.getLine());
+      for(int i=0; i<args.size(); i++) {
+       con.addArgument((ExpressionNode)args.get(i));
+      }
+
       return con;
     } else if (isNode(pn,"createarray")) {
       //System.out.println(pn.PPrint(3,true));
@@ -727,23 +859,46 @@ public class BuildIR {
       for(int i=0; i<(args.size()+num); i++)
        td=td.makeArray(state);
       CreateObjectNode con=new CreateObjectNode(td, isglobal, disjointId);
+      con.setNumLine(pn.getLine());
       for(int i=0; i<args.size(); i++) {
        con.addArgument((ExpressionNode)args.get(i));
       }
       return con;
+    } if (isNode(pn,"createarray2")) {
+      TypeDescriptor td=parseTypeDescriptor(pn);
+      int num=0;
+      if (pn.getChild("dims_opt").getLiteral()!=null)
+    num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
+      for(int i=0; i<num; i++)
+    td=td.makeArray(state);
+      CreateObjectNode con=new CreateObjectNode(td, false, null);
+      con.setNumLine(pn.getLine());
+      ParseNode ipn = pn.getChild("initializer");     
+      Vector initializers=parseVariableInitializerList(ipn);
+      ArrayInitializerNode ain = new ArrayInitializerNode(initializers);
+      ain.setNumLine(pn.getLine());
+      con.addArrayInitializer(ain);
+      return con;
     } else if (isNode(pn,"name")) {
       NameDescriptor nd=parseName(pn);
-      return new NameNode(nd);
+      NameNode nn=new NameNode(nd);
+      nn.setNumLine(pn.getLine());
+      return nn;
     } else if (isNode(pn,"this")) {
       NameDescriptor nd=new NameDescriptor("this");
-      return new NameNode(nd);
+      NameNode nn=new NameNode(nd);
+      nn.setNumLine(pn.getLine());
+      return nn;
     } else if (isNode(pn,"isavailable")) {
       NameDescriptor nd=new NameDescriptor(pn.getTerminal());
-      return new OpNode(new NameNode(nd),null,new Operation(Operation.ISAVAILABLE));
+      NameNode nn=new NameNode(nd);
+      nn.setNumLine(pn.getLine());
+      return new OpNode(nn,null,new Operation(Operation.ISAVAILABLE));
     } else if (isNode(pn,"methodinvoke1")) {
       NameDescriptor nd=parseName(pn.getChild("name"));
       Vector args=parseArgumentList(pn);
       MethodInvokeNode min=new MethodInvokeNode(nd);
+      min.setNumLine(pn.getLine());
       for(int i=0; i<args.size(); i++) {
        min.addArgument((ExpressionNode)args.get(i));
       }
@@ -753,6 +908,7 @@ public class BuildIR {
       ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
       Vector args=parseArgumentList(pn);
       MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
+      min.setNumLine(pn.getLine());
       for(int i=0; i<args.size(); i++) {
        min.addArgument((ExpressionNode)args.get(i));
       }
@@ -760,40 +916,59 @@ public class BuildIR {
     } else if (isNode(pn,"fieldaccess")) {
       ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
       String fieldname=pn.getChild("field").getTerminal();
-      return new FieldAccessNode(en,fieldname);
+      
+      FieldAccessNode fan=new FieldAccessNode(en,fieldname);
+      fan.setNumLine(pn.getLine());
+      return fan;
     } else if (isNode(pn,"arrayaccess")) {
       ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
       ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
-      return new ArrayAccessNode(en,index);
+      ArrayAccessNode aan=new ArrayAccessNode(en,index);
+      aan.setNumLine(pn.getLine());
+      return aan;
     } else if (isNode(pn,"cast1")) {
       try {
-       return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
+  CastNode cn=new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
+  cn.setNumLine(pn.getLine());      
+       return cn;
       } catch (Exception e) {
        System.out.println(pn.PPrint(1,true));
        e.printStackTrace();
        throw new Error();
       }
     } else if (isNode(pn,"cast2")) {
-      return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
+      CastNode cn=new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
+      cn.setNumLine(pn.getLine());
+      return cn;
     } else if (isNode(pn, "getoffset")) {
       TypeDescriptor td=parseTypeDescriptor(pn);
       String fieldname = pn.getChild("field").getTerminal();
       //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ "  fieldname= " + fieldname);
       return new OffsetNode(td, fieldname);
     } else if (isNode(pn, "tert")) {
-      return new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
-                             parseExpression(pn.getChild("trueexpr").getFirstChild()),
-                             parseExpression(pn.getChild("falseexpr").getFirstChild()) );
+      
+      TertiaryNode tn=new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
+          parseExpression(pn.getChild("trueexpr").getFirstChild()),
+          parseExpression(pn.getChild("falseexpr").getFirstChild()) );
+      tn.setNumLine(pn.getLine());
+      
+      return tn;
     } else if (isNode(pn, "instanceof")) {
       ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
       TypeDescriptor t=parseTypeDescriptor(pn);
-      return new InstanceOfNode(exp,t);
-    } else if (isNode(pn, "array_initializer")) {
-      System.out.println( "Array initializers not implemented yet." );
-      throw new Error();
-      //TypeDescriptor td=parseTypeDescriptor(pn);      
-      //Vector initializers=parseVariableInitializerList(pn);
-      //return new ArrayInitializerNode(td, initializers);
+      InstanceOfNode ion=new InstanceOfNode(exp,t);
+      ion.setNumLine(pn.getLine());
+      return ion;
+    } else if (isNode(pn, "array_initializer")) {  
+      Vector initializers=parseVariableInitializerList(pn);
+      return new ArrayInitializerNode(initializers);
+    } else if (isNode(pn, "class_type")) {
+      TypeDescriptor td=parseTypeDescriptor(pn);
+      ClassTypeNode ctn=new ClassTypeNode(td);
+      ctn.setNumLine(pn.getLine());
+      return ctn;
+    } else if (isNode(pn, "empty")) {
+      return null;
     } else {
       System.out.println("---------------------");
       System.out.println(pn.PPrint(3,true));
@@ -844,7 +1019,7 @@ public class BuildIR {
 
   private Vector parseVariableInitializerList(ParseNode pn) {
     Vector varInitList=new Vector();
-    ParseNode vin=pn.getChild("variable_init_list");
+    ParseNode vin=pn.getChild("var_init_list");
     if (vin==null)       /* No argument list */
       return varInitList;
     ParseNodeVector vinv=vin.getChildren();
@@ -859,6 +1034,7 @@ public class BuildIR {
     ParseNodeVector pnv=pn.getChild("args").getChildren();
 
     AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
+    an.setNumLine(pn.getLine());
     return an;
   }
 
@@ -869,6 +1045,7 @@ public class BuildIR {
     MethodDescriptor md=parseMethodHeader(headern);
     try {
       BlockNode bn=parseBlock(bodyn);
+      bn.setNumLine(pn.getLine()); // assume that method header is located at the beginning of method body
       cn.addMethod(md);
       state.addTreeCode(md,bn);
 
@@ -915,6 +1092,7 @@ public class BuildIR {
       NameDescriptor nd=new NameDescriptor("super");
       Vector args=parseArgumentList(sin);
       MethodInvokeNode min=new MethodInvokeNode(nd);
+      min.setNumLine(sin.getLine());
       for(int i=0; i<args.size(); i++) {
        min.addArgument((ExpressionNode)args.get(i));
       }
@@ -926,10 +1104,12 @@ public class BuildIR {
       NameDescriptor nd=new NameDescriptor(cn.getSymbol());
       Vector args=parseArgumentList(eci);
       MethodInvokeNode min=new MethodInvokeNode(nd);
+      min.setNumLine(eci.getLine());
       for(int i=0; i<args.size(); i++) {
        min.addArgument((ExpressionNode)args.get(i));
       }
       BlockExpressionNode ben=new BlockExpressionNode(min);
+      ben.setNumLine(eci.getLine());
       bn.addFirstBlockStatement(ben);
     }
     state.addTreeCode(md,bn);
@@ -939,12 +1119,12 @@ public class BuildIR {
     // Each class maintains one MethodDecscriptor which combines all its 
     // static blocks in their declaration order
     boolean isfirst = false;
-    MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().get("staticblocks");
+    MethodDescriptor md = (MethodDescriptor)cn.getMethodTable().getFromSameScope("staticblocks");
     if(md == null) {
       // the first static block for this class
-      Modifiers m=new Modifiers();
-      m.addModifier(Modifiers.STATIC);
-      md = new MethodDescriptor(m, "staticblocks", false);
+      Modifiers m_i=new Modifiers();
+      m_i.addModifier(Modifiers.STATIC);
+      md = new MethodDescriptor(m_i, "staticblocks", false);
       md.setAsStaticBlock();
       isfirst = true;
     }
@@ -966,7 +1146,7 @@ public class BuildIR {
         BlockStatementNode bsn = bn.get(i);
         obn.addBlockStatement(bsn);
       }
-      //TODO state.addTreeCode(md, obn);
+      state.addTreeCode(md, obn);
       bn = null;
     }
   }
@@ -1016,9 +1196,18 @@ public class BuildIR {
       String name=pn.getChild("single").getTerminal();
       String type=pn.getChild("type").getTerminal();
 
-      blockstatements.add(new TagDeclarationNode(name, type));
+      TagDeclarationNode tdn=new TagDeclarationNode(name, type);
+      tdn.setNumLine(pn.getLine());
+      
+      blockstatements.add(tdn);
     } else if (isNode(pn,"local_variable_declaration")) {
+      
+      ParseNode mn=pn.getChild("modifiers");         
       TypeDescriptor t=parseTypeDescriptor(pn);
+      if(mn!=null){
+        Modifiers m=parseModifiersList(mn);
+        assignAnnotationsToType(m, t);        
+      }   
       ParseNode vn=pn.getChild("variable_declarators_list");
       ParseNodeVector pnv=vn.getChildren();
       for(int i=0; i<pnv.size(); i++) {
@@ -1027,6 +1216,7 @@ public class BuildIR {
 
        ParseNode tmp=vardecl;
        TypeDescriptor arrayt=t;
+
        while (tmp.getChild("single")==null) {
          arrayt=arrayt.makeArray(state);
          tmp=tmp.getChild("array");
@@ -1039,22 +1229,32 @@ public class BuildIR {
        ExpressionNode en=null;
        if (epn!=null)
          en=parseExpression(epn.getFirstChild());
+       
+       DeclarationNode dn=new DeclarationNode(new VarDescriptor(arrayt, identifier),en);
+       dn.setNumLine(tmp.getLine());
 
-       blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
+       blockstatements.add(dn);
       }
     } else if (isNode(pn,"nop")) {
       /* Do Nothing */
     } else if (isNode(pn,"expression")) {
-      blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
+      BlockExpressionNode ben=new BlockExpressionNode(parseExpression(pn.getFirstChild()));
+      ben.setNumLine(pn.getLine());
+      blockstatements.add(ben);
     } else if (isNode(pn,"ifstatement")) {
-      blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
-                                              parseSingleBlock(pn.getChild("statement").getFirstChild()),
-                                              pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null));
-    } else if ((state.MGC) && (isNode(pn,"switch_statement"))) {
+      IfStatementNode isn=new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
+          parseSingleBlock(pn.getChild("statement").getFirstChild()),
+          pn.getChild("else_statement")!=null ? parseSingleBlock(pn.getChild("else_statement").getFirstChild()) : null);
+      isn.setNumLine(pn.getLine());
+      
+      blockstatements.add(isn);
+    } else if (isNode(pn,"switch_statement")) {
       // TODO add version for normal Java later
-      blockstatements.add(new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
-          parseSingleBlock(pn.getChild("statement").getFirstChild())));
-    } else if ((state.MGC) && (isNode(pn,"switch_block_list"))) {
+      SwitchStatementNode ssn=new SwitchStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
+          parseSingleBlock(pn.getChild("statement").getFirstChild()));
+      ssn.setNumLine(pn.getLine());
+      blockstatements.add(ssn);
+    } else if (isNode(pn,"switch_block_list")) {
       // TODO add version for normal Java later
       ParseNodeVector pnv=pn.getChildren();
       for(int i=0; i<pnv.size(); i++) {
@@ -1067,17 +1267,41 @@ public class BuildIR {
           for(int j=0; j<labelv.size(); j++) {
             ParseNode labeldecl=labelv.elementAt(j);
             if(isNode(labeldecl, "switch_label")) {
-              slv.addElement(new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false));
+              SwitchLabelNode sln=new SwitchLabelNode(parseExpression(labeldecl.getChild("constant_expression").getFirstChild()), false);
+              sln.setNumLine(labeldecl.getLine());
+              slv.addElement(sln);
             } else if(isNode(labeldecl, "default_switch_label")) {
-              slv.addElement(new SwitchLabelNode(null, true));
+              SwitchLabelNode sln=new SwitchLabelNode(null, true);
+              sln.setNumLine(labeldecl.getLine());
+              slv.addElement(sln);
             }
           }
           
-          blockstatements.add(new SwitchBlockNode(slv, 
-              parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild())));
+          SwitchBlockNode sbn=new SwitchBlockNode(slv, 
+              parseSingleBlock(sblockdecl.getChild("switch_statements").getFirstChild()));
+          sbn.setNumLine(sblockdecl.getLine());
+          
+          blockstatements.add(sbn);
           
         }
       }
+    } else if (isNode(pn, "trycatchstatement")) {
+      // TODO add version for normal Java later
+      // Do not fully support exceptions now. Only make sure that if there are no
+      // exceptions thrown, the execution is right
+      ParseNode tpn = pn.getChild("tryblock").getFirstChild();
+      BlockNode bn=parseBlockHelper(tpn);
+      blockstatements.add(new SubBlockNode(bn));
+      
+      ParseNode fbk = pn.getChild("finallyblock");
+      if(fbk != null) {
+        ParseNode fpn = fbk.getFirstChild();
+        BlockNode fbn=parseBlockHelper(fpn);
+        blockstatements.add(new SubBlockNode(fbn));
+      }
+    } else if (isNode(pn, "throwstatement")) {
+      // TODO Simply return here
+      //blockstatements.add(new ReturnNode());
     } else if (isNode(pn,"taskexit")) {
       Vector vfe=null;
       if (pn.getChild("flag_effects_list")!=null)
@@ -1085,21 +1309,28 @@ public class BuildIR {
       Vector ccs=null;
       if (pn.getChild("cons_checks")!=null)
        ccs=parseChecks(pn.getChild("cons_checks"));
-
-      blockstatements.add(new TaskExitNode(vfe, ccs, this.m_taskexitnum++));
+      TaskExitNode ten=new TaskExitNode(vfe, ccs, this.m_taskexitnum++);
+      ten.setNumLine(pn.getLine());
+      blockstatements.add(ten);
     } else if (isNode(pn,"atomic")) {
       BlockNode bn=parseBlockHelper(pn);
-      blockstatements.add(new AtomicNode(bn));
+      AtomicNode an=new AtomicNode(bn);
+      an.setNumLine(pn.getLine());
+      blockstatements.add(an);
     } else if (isNode(pn,"synchronized")) {
       BlockNode bn=parseBlockHelper(pn.getChild("block"));
       ExpressionNode en=parseExpression(pn.getChild("expr").getFirstChild());
-      blockstatements.add(new SynchronizedNode(en, bn));
+      SynchronizedNode sn=new SynchronizedNode(en, bn);
+      sn.setNumLine(pn.getLine());
+      blockstatements.add(sn);
     } else if (isNode(pn,"return")) {
       if (isEmpty(pn.getTerminal()))
        blockstatements.add(new ReturnNode());
       else {
        ExpressionNode en=parseExpression(pn.getFirstChild());
-       blockstatements.add(new ReturnNode(en));
+       ReturnNode rn=new ReturnNode(en);
+       rn.setNumLine(pn.getLine());
+       blockstatements.add(rn);
       }
     } else if (isNode(pn,"block_statement_list")) {
       BlockNode bn=parseBlockHelper(pn);
@@ -1119,20 +1350,35 @@ public class BuildIR {
       BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
-      blockstatements.add(new LoopNode(init,condition,update,body));
+      if(condition == null) {
+        // no condition clause, make a 'true' expression as the condition
+        condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
+      }
+      LoopNode ln=new LoopNode(init,condition,update,body);
+      ln.setNumLine(pn.getLine());
+      blockstatements.add(ln);
     } else if (isNode(pn,"whilestatement")) {
       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
+      if(condition == null) {
+        // no condition clause, make a 'true' expression as the condition
+        condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
+      }
       blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
     } else if (isNode(pn,"dowhilestatement")) {
       ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
       BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
+      if(condition == null) {
+        // no condition clause, make a 'true' expression as the condition
+        condition = (ExpressionNode)new LiteralNode("boolean", new Boolean(true));
+      }
       blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
     } else if (isNode(pn,"sese")) {
       ParseNode pnID=pn.getChild("identifier");
       String stID=null;
       if( pnID != null ) { stID=pnID.getFirstChild().getTerminal(); }
       SESENode start=new SESENode(stID);
+      start.setNumLine(pn.getLine());
       SESENode end  =new SESENode(stID);
       start.setEnd( end   );
       end.setStart( start );
@@ -1140,9 +1386,13 @@ public class BuildIR {
       blockstatements.addAll(parseSESEBlock(blockstatements,pn.getChild("body").getFirstChild()));
       blockstatements.add(end);
     } else if (isNode(pn,"continue")) {
-      blockstatements.add(new ContinueBreakNode(false));
+      ContinueBreakNode cbn=new ContinueBreakNode(false);
+      cbn.setNumLine(pn.getLine());
+      blockstatements.add(cbn);
     } else if (isNode(pn,"break")) {
-      blockstatements.add(new ContinueBreakNode(true));
+      ContinueBreakNode cbn=new ContinueBreakNode(true);
+      cbn.setNumLine(pn.getLine());
+      blockstatements.add(cbn);
 
     } else if (isNode(pn,"genreach")) {
       String graphName = pn.getChild("graphName").getTerminal();
@@ -1188,7 +1438,8 @@ public class BuildIR {
        String paramname=paramn.getChild("single").getTerminal();
        TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
        md.addTagParameter(type, paramname);
-      } else {
+      } else  {
+        
        TypeDescriptor type=parseTypeDescriptor(paramn);
 
        ParseNode tmp=paramn;
@@ -1199,6 +1450,11 @@ public class BuildIR {
        String paramname=tmp.getChild("single").getTerminal();
 
        md.addParameter(type, paramname);
+  if(isNode(paramn, "annotation_parameter")){
+    ParseNode bodynode=paramn.getChild("annotation_body");
+    parseParameterAnnotation(bodynode,type);
+  }
+  
       }
     }
   }
@@ -1209,7 +1465,7 @@ public class BuildIR {
     if (modlist!=null) {
       ParseNodeVector pnv=modlist.getChildren();
       for(int i=0; i<pnv.size(); i++) {
-       ParseNode modn=pnv.elementAt(i);
+       ParseNode modn=pnv.elementAt(i);        
        if (isNode(modn,"public"))
          m.addModifier(Modifiers.PUBLIC);
        else if (isNode(modn,"protected"))
@@ -1230,12 +1486,47 @@ public class BuildIR {
       m.addModifier(Modifiers.ABSTRACT);
     else if (isNode(modn,"volatile"))
       m.addModifier(Modifiers.VOLATILE);
-       else throw new Error("Unrecognized Modifier");
+    else if (isNode(modn,"transient"))
+      m.addModifier(Modifiers.TRANSIENT);
+    else if(isNode(modn,"annotation_list"))
+      parseAnnotationList(modn,m);    
+       else{     
+         throw new Error("Unrecognized Modifier:"+modn.getLabel());}
       }
     }
     return m;
   }
-
+  
+  private void parseAnnotationList(ParseNode pn, Modifiers m) {
+    ParseNodeVector pnv = pn.getChildren();
+    for (int i = 0; i < pnv.size(); i++) {
+      ParseNode body_list = pnv.elementAt(i);
+      if (isNode(body_list, "annotation_body")) {
+        ParseNode body_node = body_list.getFirstChild();
+        if (isNode(body_node, "marker_annotation")) {
+          m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
+        } else if (isNode(body_node, "single_annotation")) {
+          m.addAnnotation(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
+              body_node.getChild("element_value").getTerminal()));
+        } else if (isNode(body_node, "normal_annotation")) {
+          throw new Error("Annotation with multiple data members is not supported yet.");
+        }
+      }
+    }
+  }
+  
+  private void parseParameterAnnotation(ParseNode body_list,TypeDescriptor type){
+    ParseNode body_node = body_list.getFirstChild();
+    if (isNode(body_node, "marker_annotation")) {
+      type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal()));
+    } else if (isNode(body_node, "single_annotation")) {
+      type.addAnnotationMarker(new AnnotationDescriptor(body_node.getChild("name").getTerminal(),
+          body_node.getChild("element_value").getTerminal()));
+    } else if (isNode(body_node, "normal_annotation")) {
+      throw new Error("Annotation with multiple data members is not supported yet.");
+    }
+  }
+  
   private boolean isNode(ParseNode pn, String label) {
     if (pn.getLabel().equals(label))
       return true;