As discussed with Prof. Demsky/Jim, the constructor of inner class now by default...
authorspikeuci <spikeuci>
Tue, 25 Oct 2011 00:01:10 +0000 (00:01 +0000)
committerspikeuci <spikeuci>
Tue, 25 Oct 2011 00:01:10 +0000 (00:01 +0000)
Robust/src/IR/Tree/BuildIR.java
Robust/src/IR/Tree/CreateObjectNode.java
Robust/src/IR/Tree/SemanticCheck.java
Robust/src/Parse/java14.cup

index 661d264cbbd16b30210139b34711d6c6d4f87649..99f1291a4da5f2ad91651790b7442ba417656459 100644 (file)
@@ -603,16 +603,28 @@ public class BuildIR {
     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( false )
+   if( true )
    {
        this.isRunningRecursiveInnerClass = true; //fOR dEBUGGING PURPOSES IN ORDER TO DUMP STRINGS WHILE IN THIS CODE PATH
-       addOuterClassReferences( cn, new Vector< String >() );
+       addOuterClassReferences( cn, 0 );
+       addOuterClassParam( cn, 0 );
        this.isRunningRecursiveInnerClass = false;
     }
     return cn;
   }
 
-//called recursively with the parent class whose reference is to be passed
+private void initializeOuterMember( MethodDescriptor md, String fieldName, String formalParameter ) {
+        BlockNode obn = state.getMethodBody(md);
+         NameNode nn=new NameNode( new NameDescriptor( fieldName ) );
+        NameNode fn = new NameNode ( new NameDescriptor( formalParameter ) );
+          //nn.setNumLine(en.getNumLine())
+         AssignmentNode an=new AssignmentNode(nn,fn,new AssignOperation(1));
+         //an.setNumLine(pn.getLine());
+         obn.addFirstBlockStatement(new BlockExpressionNode(an));
+       // System.out.print( "The code inserted is : " + obn.printNode( 0 ) + "\n" );
+         state.addTreeCode(md, obn);
+}
+
 private void addOuterClassParam( ClassDescriptor cn, int depth )
 {
        Iterator nullCheckItr = cn.getInnerClasses();
@@ -629,7 +641,9 @@ 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) );                 
+                               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" );           
                        }
                }
                addOuterClassParam( icd, depth + 1 );
@@ -637,58 +651,41 @@ private void addOuterClassParam( ClassDescriptor cn, int depth )
        }
        
 }
-private void addOuterClassReferences( ClassDescriptor cn, Vector< String > runningClassNames )
+private void addOuterClassReferences( ClassDescriptor cn, 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;
-      //logic: for each inner class present in current cn add this$runningIndex and recursively invoke this method on that inner class
-      
-      //JAVA might be passing it by reference , so creating a copy for myself. 
-       
-       Vector tempCopy = new Vector< String >( runningClassNames );
-       tempCopy.add( cn.getClassName() );
 
-       Vector< ParseNode > vecParses = new Vector< ParseNode >();
+       String tempCopy = cn.getClassName();
        //MESSY HACK FOLLOWS
        int i = 0;
-       for( Iterator itr = tempCopy.listIterator(); itr.hasNext() ; ++i ) {
-               ParseNode theNode = new ParseNode( "field_declaration" );
-               theNode.addChild("modifier").addChild( new ParseNode( "modifier_list" ) ).addChild("final");
-               ParseNode theTypeNode = new ParseNode("type");
-               ParseNode tempChildNode = theTypeNode.addChild("class").addChild( "name" );
+
+       ParseNode theNode = new ParseNode( "field_declaration" );
+       theNode.addChild("modifier").addChild( new ParseNode( "modifier_list" ) ).addChild("final");
+       ParseNode theTypeNode = new ParseNode("type");
+       ParseNode tempChildNode = theTypeNode.addChild("class").addChild( "name" );
                //tempChildNode.addChild("base").addChild( new ParseNode("empty") );
-               tempChildNode.addChild("identifier").addChild ( ( String ) itr.next() );
-               theNode.addChild("type").addChild( theTypeNode );
-               ParseNode variableDeclaratorID = new ParseNode("single");
-               String theStr = "this$" + String.valueOf( i );
-               variableDeclaratorID.addChild( theStr );
-               ParseNode variableDeclarator = new ParseNode( "variable_declarator" );
-               variableDeclarator.addChild( variableDeclaratorID );
-               ParseNode variableDeclaratorList = new ParseNode("variable_declarators_list");
-               variableDeclaratorList.addChild( variableDeclarator );
-               theNode.addChild("variables").addChild( variableDeclaratorList );
-               //finally
-               
-               vecParses.add( theNode );
-               
-               
-       }
-       
+       tempChildNode.addChild("identifier").addChild ( tempCopy );
+       theNode.addChild("type").addChild( theTypeNode );
+       ParseNode variableDeclaratorID = new ParseNode("single");
+       String theStr = "this$" + String.valueOf( depth );
+       variableDeclaratorID.addChild( theStr );
+       ParseNode variableDeclarator = new ParseNode( "variable_declarator" );
+       variableDeclarator.addChild( variableDeclaratorID );
+       ParseNode variableDeclaratorList = new ParseNode("variable_declarators_list");
+       variableDeclaratorList.addChild( variableDeclarator );
+       theNode.addChild("variables").addChild( variableDeclaratorList );
+
        for(Iterator it=cn.getInnerClasses(); it.hasNext(); ) {
                ClassDescriptor icd=(ClassDescriptor)it.next();
-               for( Iterator vecParsesItr = vecParses.listIterator(); vecParsesItr.hasNext(); ) {
-                       ParseNode theDummy = ( ParseNode ) vecParsesItr.next();
-                       //System.out.println( ( theDummy ).PPrint( 0, true ) );
-                       parseFieldDecl( icd, theDummy );                
-               }
-               if( true ) {
+               parseFieldDecl( icd, theNode );         
+               /*if( true ) {
                        SymbolTable fieldTable = icd.getFieldTable();
-                       System.out.println( fieldTable.toString() );
-               }
-               addOuterClassReferences( icd, tempCopy );
-               
+                       //System.out.println( fieldTable.toString() );
+               }*/
+               addOuterClassReferences( icd, depth + 1 );      
        }
 }
 
@@ -823,10 +820,6 @@ private void addOuterClassReferences( ClassDescriptor cn, Vector< String > runni
     return icn;
   }
 
-private void AddSurroundingClassParamToCtor( ClassDescriptor icn, ParseNode pn )
-{
-       
-}
   private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
     ParseNode tn=pn.getChild("type");
     String type_st=tn.getTerminal();
@@ -1090,7 +1083,24 @@ private void AddSurroundingClassParamToCtor( ClassDescriptor icn, ParseNode pn )
       if (pn.getChild("disjoint") != null) {
         disjointId = pn.getChild("disjoint").getTerminal();
       }
+      ParseNode idChild = (pn.getChild( "id" ));
+      ParseNode baseChild = (pn.getChild( "base" ));
+      
       CreateObjectNode con = new CreateObjectNode(td, isglobal, disjointId);
+      if( null != idChild && null != idChild.getFirstChild() ) {
+       idChild = idChild.getFirstChild();
+       //System.out.println( "\nThe object passed has this expression " + idChild.PPrint( 0, true ) );
+       ExpressionNode en = parseExpression( idChild ); 
+       //System.out.println( "\nThe object passed has this expression " + en.printNode( 0 ) );
+       con.setSurroundingExpression( en );
+      }
+      else if( null != baseChild  && null != baseChild.getFirstChild()  ) {
+       baseChild = baseChild.getFirstChild();
+       //System.out.println( "\nThe object passed has this expression " + baseChild.PPrint( 0, true ) );
+       ExpressionNode en = parseExpression( baseChild ); 
+       //System.out.println( "\nThe object passed has this expression " + en.printNode( 0 ) );
+       con.setSurroundingExpression( en );     
+      }
       con.setNumLine(pn.getLine());
       for (int i = 0; i < args.size(); i++) {
         con.addArgument((ExpressionNode) args.get(i));
@@ -1121,7 +1131,13 @@ private void AddSurroundingClassParamToCtor( ClassDescriptor icn, ParseNode pn )
       TypeDescriptor tdnew=state.getTypeDescriptor(cnnew.getSymbol());
 
       Vector args=parseArgumentList(pn);
-
+      ParseNode idChild = pn.getChild( "id" );
+      ParseNode baseChild = pn.getChild( "base" );
+      //System.out.println("\n to print idchild and basechild for ");
+      /*if( null != idChild )
+       System.out.println( "\n trying to create an inner class and the id child passed is "  + idChild.PPrint( 0, true ) );
+      if( null != baseChild )
+       System.out.println( "\n trying to create an inner class and the base child passed is "  + baseChild.PPrint( 0, true ) );*/
       CreateObjectNode con=new CreateObjectNode(tdnew, false, null);
       con.setNumLine(pn.getLine());
       for(int i=0; i<args.size(); i++) {
index bae15edfc1de4ff0a1a120228edc938060a57e90..b6c777298b2a27ac1324d69b6f292df83bc4cba0 100644 (file)
@@ -2,6 +2,7 @@ package IR.Tree;
 import java.util.Vector;
 import IR.TypeDescriptor;
 import IR.MethodDescriptor;
+import IR.Tree.ExpressionNode;
 
 public class CreateObjectNode extends ExpressionNode {
   TypeDescriptor td;
@@ -11,13 +12,21 @@ public class CreateObjectNode extends ExpressionNode {
   boolean isglobal;
   String disjointId;
   ArrayInitializerNode ain;
+//The next 2 are unused but i will delete them once i am fully done with inner classes.
+  TypeDescriptor surroundingClass;
+  boolean isCreatedFromSurroundingClassName;
+  
+  ExpressionNode surroundingClassObject;
+  boolean isSurroundingClassExpSet;
 
   public CreateObjectNode(TypeDescriptor type, boolean isglobal, String disjointId) {
     td=type;
+   // surroundingClass = new TypeDescriptor("becauseTDdoesnthavedefaultconstructor");
     argumentlist=new Vector();
     this.isglobal=isglobal;
     this.disjointId=disjointId;
     this.ain = null;
+    isSurroundingClassExpSet = false;
   }
 
   public boolean isGlobal() {
@@ -105,4 +114,18 @@ public class CreateObjectNode extends ExpressionNode {
     eval = null;
     return eval; //null;
   }
+
+  public void setSurroundingExpression( ExpressionNode en ) {
+       
+       //System.out.println( "The expression node is : " + en );
+       surroundingClassObject = en ;
+       //System.out.println( "The expression node is : " + surroundingClassObject );
+       isSurroundingClassExpSet = true;
+  }
+  
+  public ExpressionNode getSurroundingClassExpression() {
+       if( false == isSurroundingClassExpSet )
+               return null;
+       return surroundingClassObject;
+  }
 }
index f82fe80c457b9772525a79e56785d2af5038671e..d13a76503753857712cc7f122a524ef87a7fc05b 100644 (file)
@@ -1049,7 +1049,33 @@ public class SemanticCheck {
     loopstack.pop();
   }
 
-
+  void InnerClassAddParamToCtor( MethodDescriptor md, ClassDescriptor cd, SymbolTable nametable, 
+                                CreateObjectNode con, TypeDescriptor td ) {
+       
+       TypeDescriptor cdsType = new TypeDescriptor( cd );
+       ExpressionNode conExp = con.getSurroundingClassExpression();
+       //System.out.println( "The surrounding class expression si " + con );
+       if( null == conExp ) {
+               if( md.isStatic() ) {
+                       throw new Error("trying to instantiate inner class: " +  con.getType() + " in a static scope" );
+               }
+               VarDescriptor thisVD = md.getThis();
+               if( null == thisVD ) {
+                       throw new Error( "this pointer is not defined in a non static scope" ); 
+               }                       
+               if( cdsType.equals( thisVD.getType() ) == false ) {
+                       throw new Error( "the type of this pointer is different than the type expected for inner class constructor. Initializing the inner class: "                             +  con.getType() + " in the wrong scope" );             
+               }       
+               //make this into an expression node.
+               NameNode nThis=new NameNode( new NameDescriptor( "this" ) );
+               con.addArgument( nThis );
+       }
+       else {
+               //REVISIT : here i am storing the expression as an expressionNode which does not implement type, there is no way for me to semantic check this argument.
+               con.addArgument( conExp );
+       }
+       //System.out.println( " the modified createObjectNode is " + con.printNode( 0 ) + "\n" );
+}
   void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con,
                              TypeDescriptor td) {
     TypeDescriptor[] tdarray = new TypeDescriptor[con.numArgs()];
@@ -1106,7 +1132,15 @@ public class SemanticCheck {
       // Array's don't need constructor calls
       ClassDescriptor classtolookin = typetolookin.getClassDesc();
       checkClass(classtolookin, INIT);
-
+      if( classtolookin.isInnerClass() ) {
+       InnerClassAddParamToCtor( (MethodDescriptor)md, ((MethodDescriptor)md).getClassDesc() , nametable, con, td );
+       tdarray = new TypeDescriptor[con.numArgs()];
+       for (int i = 0; i < con.numArgs(); i++) {
+               ExpressionNode en = con.getArg(i);
+               checkExpressionNode(md, nametable, en, null);
+               tdarray[i] = en.getType();
+        }
+      }
       Set methoddescriptorset = classtolookin.getMethodTable().getSet(classtolookin.getSymbol());
       MethodDescriptor bestmd = null;
 NextMethod: for (Iterator methodit = methoddescriptorset.iterator(); methodit.hasNext(); ) {
index 8e7784f6e93a63c350627fc1f4686efb1f6ede2b..6dde3b9dacdc2f7c148e57e676c6317ec6550842 100644 (file)
@@ -2020,35 +2020,39 @@ class_instance_creation_expression ::=
                pn.addChild("decl").addChild("classbody").addChild(body);
                RESULT=pn;
        :}
-       |       primary DOT NEW class_or_interface_type:type
+       |       primary: base DOT NEW class_or_interface_type:type
                        LPAREN argument_list_opt:args RPAREN {: 
                ParseNode pn=new ParseNode("createobject",parser.lexer.line_num);
                pn.addChild(type);
                pn.addChild(args);
+               pn.addChild( "base" ).addChild( base );
                RESULT=pn;
                
        :}
-       |       primary DOT NEW class_or_interface_type:type
+       |       primary: base DOT NEW class_or_interface_type:type
                        LPAREN argument_list_opt:args RPAREN class_body:body {:
                ParseNode pn=new ParseNode("createobjectcls",parser.lexer.line_num);          
                pn.addChild(type);
                pn.addChild(args);
+               pn.addChild( "base" ).addChild( base );
                pn.addChild("decl").addChild("classbody").addChild(body);
                RESULT=pn;
        :}
-       |       name DOT NEW class_or_interface_type:type
+       |       name:id DOT NEW class_or_interface_type:type
                        LPAREN argument_list_opt:args RPAREN {:
                ParseNode pn=new ParseNode("createobject",parser.lexer.line_num);
                pn.addChild(type);
                pn.addChild(args);
+               pn.addChild( "id" ).addChild( id );
                RESULT=pn;
        :}
-       |       name DOT NEW class_or_interface_type:type
+       |       name:id DOT NEW class_or_interface_type:type
                        LPAREN argument_list_opt:args RPAREN class_body:body {:
                ParseNode pn=new ParseNode("createobjectcls",parser.lexer.line_num);          
                pn.addChild(type);
                pn.addChild(args);
                pn.addChild("decl").addChild("classbody").addChild(body);
+               pn.addChild( "id" ).addChild( id );
                RESULT=pn;
        :}
        ;