void
SourceLocationInheritedAttribute::
refineClassification ( SgNode* astNode )
   {
     ROSE_ASSERT (astNode != NULL);
  // printf ("Inside of SourceLocationInheritedAttribute::refineClassification() astNode->sage_class_name() = %s \n",astNode->sage_class_name());

  // Always update this variable
     currentNode = astNode;

  // Only update the currentStatement if the current node is a SgStatement node
     if (isSgStatement(astNode) != NULL)
        {
          currentStatement = isSgStatement(astNode);

       // With this stack we never have to pop anything off the stack
          statementList.push_back(currentStatement);

       // IDEA: Store the current statement with the scope as we push the scope onto the stack
       // Then we can more accurately associate the statement with a given scope.
       // if (isSgScopeStatement(astNode) != NULL)
       // if ( (isSgScopeStatement(astNode) != NULL) && (astNode->get_traversalSuccessorNamesContainer().size() > 0) )

       // Only save the scoep if it is a global scope or basic block
          if ( (isSgGlobal(astNode) != NULL) ||
               (isSgBasicBlock(astNode) != NULL) )
             {
               SgScopeStatement* scope = isSgScopeStatement(astNode);
               scopeList.push_back(scope);
             }

       // printf ("Need to store the current statement with the scope (or something like that) \n");
        }

#if ROSE_INTERNAL_DEBUG
  // error checking
     unsigned int i;
     for (i = 0; i < scopeList.size(); i++)
        {
          ROSE_ASSERT (scopeList[i] != NULL);
        }
     for (i = 0; i < statementList.size(); i++)
        {
          ROSE_ASSERT (statementList[i] != NULL);
        }
#endif

  // Only update the currentExpression if the current node is a SgExpression node
     if (isSgExpression(astNode) != NULL)
          currentExpression = isSgExpression(astNode);

  // currentFunctionBasicBlockScope = NULL;

  // Refine the information with a classification of the current node
     ROSE_ASSERT (astNode != NULL);
     switch (astNode->variantT())
        {
          case V_SgProject:
               project = isSgProject(astNode);
               break;

       // case V_SgFile:
          case V_SgSourceFile:
          case V_SgBinaryComposite:
               file = isSgFile(astNode);
               break;

          case V_SgGlobal:
               globalScope = isSgGlobal(astNode);

            // This will be reset when we hit a SgBasicBlock (but should be set here
            // so that the local scope can be refered to even in the global scope).
               localScope  = globalScope;

            // With this stack we never have to pop anything off the stack
            // scopeList.push_back(globalScope);
               break;

          case V_SgFunctionDeclaration:
#if 0
               currentFunctionDeclaration = isSgFunctionDeclaration(astNode);
#endif
               break;

          case V_SgFunctionDefinition:
             {
               currentFunctionDefinitionScope = isSgFunctionDefinition(astNode);

            // With this stack we heve have to pop anything off the stack (don't add function definition to scope list)
            // scopeList.push_back(currentFunctionDefinitionScope);
#if 0
            // Maybe the function scope should be set when we process the basic block?
               SgFunctionDefenition functionDefinition = isSgFunctionDefinition(astNode);
               if (functionDefinition != NULL)
                    currentFunctionScope = functionDefinition->get_block();
#endif
               break;
             }

          case V_SgBasicBlock:
             {
            // printf ("Case V_SgBasicBlock \n");
               localScope = isSgBasicBlock(astNode);
               if ( isSgFunctionDefinition(localScope->get_parent()) != NULL )
                  {
                    currentFunctionBasicBlockScope = isSgBasicBlock(astNode);
                    ROSE_ASSERT ( currentFunctionBasicBlockScope == currentFunctionDefinitionScope->get_body() );
                  }

            // loop back through the loop nest to recover the parent scope of the loop nest
               SgForStatement* forStatement = isSgForStatement(localScope->get_parent());
               while ( forStatement != NULL )
                  {
                    SgStatement* parentStatement = isSgStatement(forStatement->get_parent());
                    loopNestParentScope = isSgBasicBlock(parentStatement);
                    ROSE_ASSERT (loopNestParentScope != NULL);
                    forStatement = isSgForStatement(loopNestParentScope->get_parent());
                  }

            // don't worry about nested conditionals (not yet at least)
               SgIfStmt* ifStatement = isSgIfStmt(localScope->get_parent());
               if ( ifStatement != NULL )
                  {
                    SgStatement* parentStatement = isSgStatement(ifStatement->get_parent());
                    conditionalParentScope = isSgBasicBlock(parentStatement);
                    ROSE_ASSERT (conditionalParentScope != NULL);
                  }

            // With this stack we never have to pop anything off the stack
            // scopeList.push_back(localScope);
               break;
             }

       // For now all these cases are the same
          case V_SgCatchOptionStmt:
          case V_SgClassDefinition:
          case V_SgDoWhileStmt:
          case V_SgForStatement:
          case V_SgIfStmt:
          case V_SgSwitchStatement:
          case V_SgWhileStmt:
             {
               SgScopeStatement* scope = isSgScopeStatement(astNode);
               ROSE_ASSERT (scope != NULL);

            // With this stack we never have to pop anything off the stack
            // scopeList.push_back(scope);
               break;
             }

          default:
            // astNode is some other type of AST node which we don't keep track of in this inherited
            // attribute.
#if 0
               printf ("Default case inside of SourceLocationInheritedAttribute::refineClassification(astNode = %p) \n",astNode);
               ROSE_ABORT();
#endif
               break;
        }

  // assertValidPointers();

#if 0
     printf ("Inside of SourceLocationInheritedAttribute::refineClassification(astNode = %p) \n",astNode);
     display("Inside of SourceLocationInheritedAttribute::refineClassification()");
#endif
#if 0
     printf ("Inside of SourceLocationInheritedAttribute::refineClassification(astNode = %p) \n",astNode);
     ROSE_ABORT();
#endif
   }
예제 #2
0
// Move variables declared in a for statement to just outside that statement.
void moveForDeclaredVariables(SgNode* root)
   {
     vector<SgForStatement*> for_statements;
     FindForStatementsVisitor(for_statements).traverse(root, preorder);

     for (unsigned int i = 0; i < for_statements.size(); ++i)
        {
          SgForStatement* stmt = for_statements[i];
#ifdef FD_DEBUG
          cout << "moveForDeclaredVariables: " << stmt->unparseToString() << endl;
#endif
          SgForInitStatement* init = stmt->get_for_init_stmt();
          if (!init) continue;
          SgStatementPtrList& inits = init->get_init_stmt();
          vector<SgVariableDeclaration*> decls;
          for (SgStatementPtrList::iterator j = inits.begin(); j != inits.end(); ++j) {
            SgStatement* one_init = *j;
            if (isSgVariableDeclaration(one_init))
            {
              decls.push_back(isSgVariableDeclaration(one_init));
            }
          }
          if (decls.empty()) continue;
          SgStatement* parent = isSgStatement(stmt->get_parent());
          assert (parent);
          SgBasicBlock* bb = new SgBasicBlock(SgNULL_FILE);
          stmt->set_parent(bb);
          bb->set_parent(parent);
          SgStatementPtrList ls;
          for (unsigned int j = 0; j < decls.size(); ++j)
             {
               for (SgInitializedNamePtrList::iterator k = decls[j]->get_variables().begin(); k != decls[j]->get_variables().end(); ++k)
                  {
#ifdef FD_DEBUG
                    cout << "Working on variable " << (*k)->get_name().getString() << endl;
#endif
                    SgVariableSymbol* sym = new SgVariableSymbol(*k);
                    bb->insert_symbol((*k)->get_name(), sym);
                    (*k)->set_scope(bb);
                    SgAssignInitializer* kinit = 0;
                    if (isSgAssignInitializer((*k)->get_initializer()))
                       {
                         kinit = isSgAssignInitializer((*k)->get_initializer());
                         (*k)->set_initializer(0);
                       }

                    if (kinit)
                       {
                         SgVarRefExp* vr = new SgVarRefExp(SgNULL_FILE, sym);
                         vr->set_endOfConstruct(SgNULL_FILE);
                         vr->set_lvalue(true);
                         SgAssignOp* assignment = new SgAssignOp(SgNULL_FILE,vr,kinit->get_operand());
                         vr->set_parent(assignment);
                         kinit->get_operand()->set_parent(assignment);
                         SgExprStatement* expr = new SgExprStatement(SgNULL_FILE, assignment);
                         assignment->set_parent(expr);
                         ls.push_back(expr);
                         expr->set_parent(init);
                       }
                  }

#if 0
               SgStatementPtrList::iterator fiiter = std::find(inits.begin(), inits.end(), decls[j]);
               assert (fiiter != inits.end());
               size_t idx = fiiter - inits.begin();
               inits.erase(inits.begin() + idx);
               inits.insert(inits.begin() + idx, ls.begin(), ls.end());
#endif
               bb->get_statements().push_back(decls[j]);
               decls[j]->set_parent(bb);
             }
          inits = ls;
          bb->get_statements().push_back(stmt);
       // printf ("In moveForDeclaredVariables(): parent = %p = %s bb = %p stmt = %p = %s \n",parent,parent->class_name().c_str(),bb,stmt,stmt->class_name().c_str());
          ROSE_ASSERT(stmt->get_parent() == bb);
          parent->replace_statement(stmt, bb);
        }
   }