Example #1
0
SgVariableSymbol* 
putGlobalVariablesIntoClass (Rose_STL_Container<SgInitializedName*> & globalVariables, SgClassDeclaration* classDeclaration )
   {
  // This function iterates over the list of global variables and inserts them into the iput class definition

     SgVariableSymbol* globalClassVariableSymbol = NULL;

     for (Rose_STL_Container<SgInitializedName*>::iterator var = globalVariables.begin(); var != globalVariables.end(); var++)
        {
       // printf ("Appending global variable = %s to new globalVariableContainer \n",(*var)->get_name().str());
          SgVariableDeclaration* globalVariableDeclaration = isSgVariableDeclaration((*var)->get_parent());
          assert(globalVariableDeclaration != NULL);

       // Get the global scope from the global variable directly
          SgGlobal* globalScope = isSgGlobal(globalVariableDeclaration->get_scope());
          assert(globalScope != NULL);

          if (var == globalVariables.begin())
             {
            // This is the first time in this loop, replace the first global variable with 
            // the class declaration/definition containing all the global variables!
            // Note that initializers in the global variable declarations require modification 
            // of the preinitialization list in the class's constructor!  I am ignoring this for now!
               globalScope->replace_statement(globalVariableDeclaration,classDeclaration);

            // Build source position informaiton (marked as transformation)
               Sg_File_Info* fileInfo = Sg_File_Info::generateDefaultFileInfoForTransformationNode();
               assert(fileInfo != NULL);

            // Add the variable of the class type to the global scope!
               SgClassType* variableType = new SgClassType(classDeclaration->get_firstNondefiningDeclaration());
               assert(variableType != NULL);
               SgVariableDeclaration* variableDeclaration = new SgVariableDeclaration(fileInfo,"AMPI_globals",variableType);
               assert(variableDeclaration != NULL);

               globalScope->insert_statement(classDeclaration,variableDeclaration,false);

               assert(variableDeclaration->get_variables().empty() == false);
               SgInitializedName* variableName = *(variableDeclaration->get_variables().begin());
               assert(variableName != NULL);

            // DQ (9/8/2007): Need to set the scope of the new variable.
               variableName->set_scope(globalScope);

            // build the return value
               globalClassVariableSymbol = new SgVariableSymbol(variableName);

            // DQ (9/8/2007): Need to add the symbol to the global scope (new testing requires this).
               globalScope->insert_symbol(variableName->get_name(),globalClassVariableSymbol);
               ROSE_ASSERT(globalScope->lookup_variable_symbol(variableName->get_name()) != NULL);
             }
            else
             {
            // for all other iterations of this loop ... 
            // remove variable declaration from the global scope
               globalScope->remove_statement(globalVariableDeclaration);
             }

       // add the variable declaration to the class definition
          classDeclaration->get_definition()->append_member(globalVariableDeclaration);
        }

     return globalClassVariableSymbol;
   }
Example #2
0
SgFunctionSymbol*
SimpleInstrumentation::buildNewFunctionDeclaration ( SgStatement* statementLocation, SgFunctionType* previousFunctionType )
   {
  // *****************************************************
  // Create the functionDeclaration
  // *****************************************************

  // Must mark the newly built node to be a part of a transformation so that it will be unparsed!
     Sg_File_Info * file_info = new Sg_File_Info();
     ROSE_ASSERT(file_info != NULL);
     file_info->set_isPartOfTransformation(true);

     SgName function_name = "contest_call";
     ROSE_ASSERT(previousFunctionType != NULL);
     SgFunctionDeclaration* functionDeclaration = new SgFunctionDeclaration(file_info, function_name, previousFunctionType);
     ROSE_ASSERT(functionDeclaration != NULL);
     ROSE_ASSERT(functionDeclaration->get_parameterList() != NULL);

  // ********************************************************************
  // Create the InitializedName for a parameter within the parameter list
  // ********************************************************************
     SgTypePtrList & argList = previousFunctionType->get_arguments();
     SgTypePtrList::iterator i = argList.begin();
     while ( i != argList.end() )
        {
          SgName var_name = "";
          SgInitializer* var_initializer = NULL;
          SgInitializedName *var_init_name = new SgInitializedName(var_name, *i, var_initializer, NULL);
          functionDeclaration->get_parameterList()->append_arg(var_init_name);
          i++;
        }

  // Add any additional function arguments here! Make sure that the function type is consistant.

  // Get the scope
     SgScopeStatement* scope = statementLocation->get_scope();

  // Set the parent node in the AST (this could be done by the AstPostProcessing
     functionDeclaration->set_parent(scope);

  // Set the scope explicitly (since it could be different from the parent?)
     functionDeclaration->set_scope(scope);

  // If it is not a forward declaration then the unparser will skip the ";" at the end (need to fix this better)
     functionDeclaration->setForward();
     ROSE_ASSERT(functionDeclaration->isForward() == true);

  // Mark function as extern "C"
     functionDeclaration->get_declarationModifier().get_storageModifier().setExtern();
     functionDeclaration->set_linkage("C");  // This mechanism could be improved!

     bool inFront = true;
     SgGlobal* globalScope = TransformationSupport::getGlobalScope(statementLocation);
     SgFunctionDeclaration* functionDeclarationInGlobalScope = 
          TransformationSupport::getFunctionDeclaration(statementLocation);
     ROSE_ASSERT(globalScope != NULL);
     ROSE_ASSERT(functionDeclarationInGlobalScope != NULL);
     globalScope->insert_statement(functionDeclarationInGlobalScope,functionDeclaration,inFront);

     SgFunctionSymbol* functionSymbol = new SgFunctionSymbol(functionDeclaration);
     ROSE_ASSERT(functionSymbol != NULL);
     ROSE_ASSERT(functionSymbol->get_type() != NULL);

     return functionSymbol;
   }