[Prev][Next][Index]

Re: Help me!



Michael, below is the part of your make_new_decls function, rewritten  
so it can handle function prototypes (at least using the newest  
version of Sage). Given the input:

	test()
	{
    		int original1;
    		int *original2;
    		int (*original3)();
    		int original4(int);
                int original5();
	}

it transforms it into:

  test(){
       int original5_copy();
       int original4_copy(int );
       int  (*original3_copy)();
       int  *original2_copy;
       int original1_copy;
       int original1;
       int  *original2;
       int  (*original3)();
       int original4(int );
       int original5();
}

The code ain't pretty (in fact, it is quite ugly).  It also generates  
a warning message from an unparser. But, it does what you want to do.  
What it means for us is that the DeclareTheSymbol and
DeclareTheSymbolWithParamList methods still need a lot of work. 


You can't use DeclareTheSymbol for function prototypes, because, as  
Dennis has pointed out 

"the problem with declaring function prototypes is that
they can have very complicated lists of arguments."
DeclareTheSymbolWithParamList is supposed to take care of that  
problem by requiring  that the list of arguments be explicitly given  
to it. But it has a bug and cannot handle empty lists. So, I had to  
use a hack:
                          
new_symbol->declareTheSymbolWithParamList(*current_scope,
                                  SgExprListExp());

This will eventually be fixed so that declareTheSymbolWithParamList  
will take a pointer to the parameter list and handle null pointers as  
well. But, for now here's the code.   --Beata


	if (variant == VAR_DECL) {
	    var_decl = (SgVarDeclStmt *) current_stmt;

	    type = var_decl->symbol(0)->type();

	    /* Make a name oldname_copy */
	    old_symbol_name = var_decl->symbol(0)->identifier();
	    new_symbol_name = 

                 (char *) malloc (strlen(old_symbol_name) + 6);
	    sprintf(new_symbol_name,"%s_copy",old_symbol_name);

	    /* Make the new symbol, and then declare it */
            if(isSgVariableSymb(var_decl->symbol(0))){
                 printf("making variable %s\n", new_symbol_name); 

	         new_symbol = new SgVariableSymb(new_symbol_name,
					    *type);
	         new_symbol->declareTheSymbol(*current_scope);}
            else if(isSgFunctionSymb(var_decl->symbol(0))){
	         new_symbol = 

                  new SgFunctionSymb(FUNCTION_NAME,new_symbol_name,
					    *type, *current_scope);
                 SgExpression *ap = var_decl->expr(0);
                 SgExpression * bp = ap->lhs(); 

                    if(bp->variant() == FUNCTION_OP)
                         {
                       if((bp->operand(2))!=NULL)
                         new_symbol->
                 declareTheSymbolWithParamList(*current_scope,  

                                            *(bp->operand(2)));
                       else
                         new_symbol->
                  declareTheSymbolWithParamList(*current_scope,
                                              SgExprListExp());
                         } 

                    else if(bp->variant() == FUNCTION_REF){ 

                       if((bp->operand(1))!=NULL)
                         new_symbol->
                  declareTheSymbolWithParamList(*current_scope, 

                                             *(bp->operand(1)));
                       else
                         new_symbol->
                 declareTheSymbolWithParamList(*current_scope,
                                  SgExprListExp());
                       }
                    else
                        printf("cannot handle this case\n");
                    }
            else printf("cannot handle this case\n");

 

	}