[Prev][Next][Index]

Re: generating declaration statements



Erik wrote:

I'm hitting an unexpected error when I try to generate the variable  
declaration

int var_name;

in a program.  Here's my code:

void decl_test(SgFile& file)
{
  SgStatement *main, *decl_stmt;
  SgType *int_type;
  SgSymbol *var_symb;
  SgExpression *var_exp;
  

  main = file.mainProgram();
  int_type=SgTypeInt();

  var_symb = new SgVariableSymb("var_name",*int_type);
  var_exp = new SgVarRefExp(*var_symb);
  decl_stmt = new SgVarDeclStmt(*var_exp,*int_type);

  (main->childList1(0))->insertStmtBefore(*decl_stmt);
}
  

And here's what it adds to my dep file:

  ------TYPE ERROR---- var_name;


----

Do you have any idea what might be going wrong?

__________


Yes, you need to put SgVarRefExp inside an SgExprListExp (this is  
because, in general, a declaration can contain a list of variables,  
as in
int i, j, k;

So, the code should look like:

  var_symb = new SgVariableSymb("var_name",*int_type);
  var_exp = new SgVarRefExp(*var_symb);
  SgExpression * elst = new SgExprListExp(*var_exp); // add this
  decl_stmt = new SgVarDeclStmt(*elst,*int_type);   //modify this

  (main->childList1(0))->insertStmtBefore(*decl_stmt);

Another way to achieve what you want is by using makeVarDeclStmt  
method:

  var_symb = new SgVariableSymb("var_name",*int_type);

  decl_stmt=var_symb->makeVarDeclStmt();
  (main->childList1(0))->insertStmtBefore(*decl_stmt);

BTW, you might want to read a document entitled:
"How to construct Declarations and New .dep Files" (it should be  
included with the distribution)   --Beata