// SgScopeStatement* SgNode* SourceLocationInheritedAttribute::getParentScope() const { SgScopeStatement* scope = NULL; int numberOfScopes = scopeList.size(); // ROSE_ASSERT (numberOfScopes > 0); // printf ("In SourceLocationInheritedAttribute::getParentScope(): numberOfScopes = %d \n",numberOfScopes); if (numberOfScopes > 1) { scope = scopeList[numberOfScopes-2]; // We don't want to return a for loop statement as a parent scope statement since insert_statement // is not well defined for such scope statements. // if (scope->variantT() == V_SgForStatement) int i = 1; while (scope->variantT() == V_SgForStatement) { ROSE_ASSERT (numberOfScopes-(2+i) >= 0); scope = scopeList[numberOfScopes-(2+i)]; i++; } } else { scope = isSgGlobal( getGlobalScope() ); } printf ("In SourceLocationInheritedAttribute::getParentScope(): parent scope is %s \n",scope->sage_class_name()); ROSE_ASSERT (scope->variantT() != V_SgForStatement); ROSE_ASSERT (scope != NULL); return scope; }
void visitorTraversal::visit(SgNode* n) { // There are three types ir IR nodes that can be queried for scope: // - SgStatement, and // - SgInitializedName SgStatement* statement = isSgStatement(n); if (statement != NULL) { SgScopeStatement* scope = statement->get_scope(); ROSE_ASSERT(scope != NULL); printf ("SgStatement = %12p = %30s has scope = %12p = %s (total number = %d) \n", statement,statement->class_name().c_str(), scope,scope->class_name().c_str(),(int)scope->numberOfNodes()); } SgInitializedName* initializedName = isSgInitializedName(n); if (initializedName != NULL) { SgScopeStatement* scope = initializedName->get_scope(); ROSE_ASSERT(scope != NULL); printf ("SgInitializedName = %12p = %30s has scope = %12p = %s (total number = %d)\n", initializedName,initializedName->get_name().str(), scope,scope->class_name().c_str(),(int)scope->numberOfNodes()); } }
/** * handling error occured due to failed assertion * this error handler only works during parser phase since it uses * Token_t to know the line number * TODO: we need to make this function more general **/ void fortran_error_handler(int signum) { // get the current filename std::string sFilename = getCurrentFilename(); if (sFilename.size()==0) { fprintf(stderr, "ERROR while parsing the source code\n"); } else { SgScopeStatement* scope = astScopeStack.front(); SgStatement* lastStatement = scope; SgStatementPtrList statementList = scope->generateStatementList(); if (statementList.empty() == false) { lastStatement = statementList.back(); } int lineNumberOfLastStatement = (astScopeStack.empty() == false) ? lastStatement->get_file_info()->get_line() : 0; // get the latest token parsed if (lineNumberOfLastStatement > 0) std::cerr <<"FATAL ERROR in file "<<sFilename<<":"<<lineNumberOfLastStatement<<std::endl; else std::cerr <<"FATAL ERROR while parsing "<<sFilename<<std::endl; } fflush(NULL); // flush all stdio fortran_error_handler_end(); exit(-1); }
/* * The function * findScope() * takes as a parameter a SgNode* which is a SgStatement*. It returns a SgNodePtrVector of all * preceding scopes the SgStatement is in. * */ SgNodePtrVector findScopes (SgNode * astNode) { ROSE_ASSERT (isSgStatement (astNode)); SgNodePtrVector returnVector; SgScopeStatement *currentScope; if (isSgScopeStatement (astNode)) { currentScope = isSgScopeStatement (astNode); ROSE_ASSERT (currentScope != NULL); returnVector.push_back (astNode); } else { SgStatement *sageStatement = isSgStatement (astNode); ROSE_ASSERT (sageStatement != NULL); currentScope = sageStatement->get_scope (); ROSE_ASSERT (currentScope != NULL); returnVector.push_back (currentScope); } while (currentScope->variantT () != V_SgGlobal) { currentScope = currentScope->get_scope (); ROSE_ASSERT (currentScope != NULL); returnVector.push_back (currentScope); } //Must also include the Global Scopes of the other files in the project if (currentScope->variantT () == V_SgGlobal) { SgFile *sageFile = isSgFile ((currentScope)->get_parent ()); ROSE_ASSERT (sageFile != NULL); SgProject *sageProject = isSgProject (sageFile->get_parent ()); ROSE_ASSERT (sageProject != NULL); //Get a list of all files in the current project const SgFilePtrList& sageFilePtrList = sageProject->get_fileList (); //Iterate over the list of files to find all Global Scopes SgNodePtrVector globalScopes; for (unsigned int i = 0; i < sageFilePtrList.size (); i += 1) { const SgSourceFile *sageFile = isSgSourceFile (sageFilePtrList[i]); ROSE_ASSERT (sageFile != NULL); SgGlobal *sageGlobal = sageFile->get_globalScope(); ROSE_ASSERT (sageGlobal != NULL); returnVector.push_back (sageGlobal); } } return returnVector; };
void RtedTransformation::changeReturnStmt(ReturnInfo rinfo) { SgReturnStmt* const rstmt = rinfo.stmt; SgExpression* const returnExpr = rstmt->get_expression(); requiresParentIsBasicBlock(*rstmt); if (!returnExpr) { // function returns a value but return statement has no expression insertErrorReport(*rstmt, "return statement is expected to return a value"); return; } // We need to build a new variable of type returnExpr // \pp why do we have to do that? // most likely b/c exitScope clears all local pointers ... SgScopeStatement* scope = rstmt->get_scope(); SgType* const typeRet = returnExpr->get_type(); std::string name = "rstmt"; ROSE_ASSERT(scope); name.append(scope->get_qualified_name().str()); SgName rName( name ); SgAssignInitializer* init = SB::buildAssignInitializer(returnExpr); SgVariableDeclaration* resDecl = SB::buildVariableDeclaration( rName, typeRet, init, scope ); SgInitializedName& resVar = SI::getFirstVariable(*resDecl); SgVarRefExp* const vexp = SB::buildVarRefExp(rName, scope); SgStatement* const newRtnStmt = SB::buildReturnStmt( vexp ); SI::replaceStatement( rstmt, newRtnStmt ); SI::insertStatementBefore( newRtnStmt, resDecl ); Sg_File_Info* const fileinfo = scope->get_endOfConstruct(); // handle C++ only if the function returns a pointer if (rinfo.filetype != ftCxx) { SgStatement* const exitStmt = (rinfo.expected_return == ReturnInfo::rtValue) ? buildExitBlockStmt(rinfo.open_blocks, *scope, fileinfo) : buildDelayedLeakCheckExitStmt(rinfo.filetype, rinfo.open_blocks, *scope, resVar, fileinfo) ; SI::insertStatementBefore( newRtnStmt, exitStmt ); } else if (rinfo.expected_return == ReturnInfo::rtIndirection) { SgStatement* const exitStmt = buildDelayedLeakCheckExitStmt(rinfo.filetype, rinfo.open_blocks, *scope, resVar, fileinfo); SI::insertStatementBefore( newRtnStmt, exitStmt ); } }
void initAstFromString(std::ifstream & in_file) { SgScopeStatement * scope = SageBuilder::buildBasicBlock(); AstFromString::c_sgnode = scope; SageBuilder::pushScopeStack(scope); SgName pow_name("pow"); SgFunctionDeclaration * pow_decl = SageBuilder::buildNondefiningFunctionDeclaration( pow_name, SageBuilder::buildDoubleType(), SageBuilder::buildFunctionParameterList( SageBuilder::buildInitializedName("base", SageBuilder::buildDoubleType()), SageBuilder::buildInitializedName("exp", SageBuilder::buildDoubleType()) ), scope, NULL ); scope->insert_symbol(pow_name, new SgFunctionSymbol(pow_decl)); SgName sqrt_name("sqrt"); SgFunctionDeclaration * sqrt_decl = SageBuilder::buildNondefiningFunctionDeclaration( sqrt_name, SageBuilder::buildDoubleType(), SageBuilder::buildFunctionParameterList( SageBuilder::buildInitializedName("v", SageBuilder::buildDoubleType()) ), scope, NULL ); scope->insert_symbol(sqrt_name, new SgFunctionSymbol(sqrt_decl)); SgName abs_name("fabs"); SgFunctionDeclaration * abs_decl = SageBuilder::buildNondefiningFunctionDeclaration( abs_name, SageBuilder::buildDoubleType(), SageBuilder::buildFunctionParameterList( SageBuilder::buildInitializedName("v", SageBuilder::buildDoubleType()) ), scope, NULL ); scope->insert_symbol(abs_name, new SgFunctionSymbol(abs_decl)); in_file.seekg (0, in_file.end); int length = in_file.tellg(); in_file.seekg (0, in_file.beg); char * tmp_char_str = new char [length + 1]; in_file.read(tmp_char_str, length); tmp_char_str[length] = 0; AstFromString::c_char = tmp_char_str; AstFromString::afs_skip_whitespace(); }
/*! * \brief Creates an assignment to "pack" a local variable back into * an outlined-function parameter that has been passed as a pointer * value. * * This routine takes the original "unpack" definition, of the form * * TYPE local_unpack_var = *outlined_func_arg; * int i = *(int *)(__out_argv[1]); // parameter wrapping case * * and creates the "re-pack" assignment expression, * * *outlined_func_arg = local_unpack_var * *(int *)(__out_argv[1]) =i; // parameter wrapping case * * C++ variables of reference types do not need this step. */ static SgAssignOp * createPackExpr (SgInitializedName* local_unpack_def) { if (!Outliner::temp_variable) { if (is_C_language()) //skip for pointer dereferencing used in C language return NULL; } // reference types do not need copy the value back in any cases if (isSgReferenceType (local_unpack_def->get_type ())) return NULL; if (local_unpack_def && !isReadOnlyType (local_unpack_def->get_type ())) // && !isSgReferenceType (local_unpack_def->get_type ())) { SgName local_var_name (local_unpack_def->get_name ()); SgAssignInitializer* local_var_init = isSgAssignInitializer (local_unpack_def->get_initializer ()); ROSE_ASSERT (local_var_init); // Create the LHS, which derefs the function argument, by // copying the original dereference expression. // SgPointerDerefExp* param_deref_unpack = isSgPointerDerefExp (local_var_init->get_operand_i ()); if (param_deref_unpack == NULL) { cout<<"packing statement is:"<<local_unpack_def->get_declaration()->unparseToString()<<endl; cout<<"local unpacking stmt's initializer's operand has non-pointer deferencing type:"<<local_var_init->get_operand_i ()->class_name()<<endl; ROSE_ASSERT (param_deref_unpack); } SgPointerDerefExp* param_deref_pack = isSgPointerDerefExp (ASTtools::deepCopy (param_deref_unpack)); ROSE_ASSERT (param_deref_pack); // Create the RHS, which references the local variable. SgScopeStatement* scope = local_unpack_def->get_scope (); ROSE_ASSERT (scope); SgVariableSymbol* local_var_sym = scope->lookup_var_symbol (local_var_name); ROSE_ASSERT (local_var_sym); SgVarRefExp* local_var_ref = SageBuilder::buildVarRefExp (local_var_sym); ROSE_ASSERT (local_var_ref); // Assemble the final assignment expression. return SageBuilder::buildAssignOp (param_deref_pack, local_var_ref); } return 0; }
void Jovial_to_C::translateProgramHeaderStatement(SgProgramHeaderStatement* programHeaderStatement) { // Get scopeStatement from SgProgramHeaderStatement SgScopeStatement* scopeStatement = programHeaderStatement->get_scope(); ROSE_ASSERT(scopeStatement); // Get ParameterList and DecoratorList SgFunctionParameterList* functionParameterList = buildFunctionParameterList(); SgExprListExp* decoratorList = deepCopy(programHeaderStatement->get_decoratorList()); // Reuse FunctionDefinition from Fortran programHeaderStatement SgFunctionDefinition* functionDefinition = programHeaderStatement->get_definition(); // Get basicBlock from SgProgramHeaderStatement SgBasicBlock* basicBlock = functionDefinition->get_body(); ROSE_ASSERT(basicBlock); SgSymbolTable* symbolTable = basicBlock->get_symbol_table(); ROSE_ASSERT(symbolTable); // The main function return type is int SgType* mainType = SgTypeInt::createType(); // Remove original function symbol. Keep the new function symbol with name of "main" SgFunctionSymbol* functionSymbol = isSgFunctionSymbol(scopeStatement->lookup_symbol(programHeaderStatement->get_name())); SgSymbolTable* globalSymbolTable = isSgSymbolTable(functionSymbol->get_parent()); globalSymbolTable->remove(functionSymbol); functionSymbol->set_parent(NULL); delete(functionSymbol); // Create SgFunctionDeclaration for C main function. Name must be "main". SgFunctionDeclaration* cFunctionDeclaration = buildDefiningFunctionDeclaration("main", mainType, functionParameterList, scopeStatement); // Setup the C function declaration. removeList.push_back(cFunctionDeclaration->get_definition()); functionDefinition->set_parent(cFunctionDeclaration); cFunctionDeclaration->set_definition(functionDefinition); programHeaderStatement->set_definition(NULL); // Replace the SgProgramHeaderStatement with SgFunctionDeclaration. replaceStatement(programHeaderStatement,cFunctionDeclaration,true); cFunctionDeclaration->set_decoratorList(decoratorList); // cFunctionDeclaration->set_startOfConstruct(functionDefinition->get_startOfConstruct()); // cFunctionDeclaration->set_endOfConstruct(functionDefinition->get_endOfConstruct()); // cFunctionDeclaration->get_file_info()->set_physical_filename(cFunctionDeclaration->get_file_info()->get_filenameString()); programHeaderStatement->set_parent(NULL); } // End of Jovial_to_C::translateProgramHeaderStatement
void SgScopeStatement::fixupCopy_symbols(SgNode* copy, SgCopyHelp & help) const { #if DEBUG_FIXUP_COPY printf ("Inside of SgScopeStatement::fixupCopy_symbols() for %p = %s copy = %p \n",this,this->class_name().c_str(),copy); #endif #if 0 printf ("Inside of SgScopeStatement::fixupCopy_symbols() for %p = %s copy = %p (calling SgStatement::fixupCopy_symbols()) \n",this,this->class_name().c_str(),copy); #endif // Call the base class fixupCopy member function SgStatement::fixupCopy_symbols(copy,help); #if 0 printf ("DONE: Inside of SgScopeStatement::fixupCopy_symbols() for %p = %s copy = %p (calling SgStatement::fixupCopy_symbols()) \n",this,this->class_name().c_str(),copy); #endif SgScopeStatement* copyScopeStatement = isSgScopeStatement(copy); ROSE_ASSERT(copyScopeStatement != NULL); // The symbol table should not have been setup yet! // ROSE_ASSERT(copyScopeStatement->get_symbol_table()->size() == 0); ROSE_ASSERT(copyScopeStatement->symbol_table_size() == 0); #if 0 printf ("Inside of SgScopeStatement::fixupCopy_symbols() for %p = %s copy = %p (calling SageInterface::rebuildSymbolTable()) \n",this,this->class_name().c_str(),copy); #endif SageInterface::rebuildSymbolTable(copyScopeStatement); #if 0 printf ("DONE: Inside of SgScopeStatement::fixupCopy_symbols() for %p = %s copy = %p (calling SageInterface::rebuildSymbolTable()) \n",this,this->class_name().c_str(),copy); printf ("Inside of SgScopeStatement::fixupCopy_symbols() for %p = %s copy = %p (calling SageInterface::fixupReferencesToSymbols()) \n",this,this->class_name().c_str(),copy); #endif // DQ (3/1/2009): After rebuilding the symbol table, we have to reset references // to old symbols (from the original symbol table) to the new symbols just built. SageInterface::fixupReferencesToSymbols(this,copyScopeStatement,help); #if 0 printf ("DONE: Inside of SgScopeStatement::fixupCopy_symbols() for %p = %s copy = %p (calling SageInterface::fixupReferencesToSymbols()) \n",this,this->class_name().c_str(),copy); #endif // printf ("\nLeaving SgScopeStatement::fixupCopy_symbols() for %p = %s copy = %p \n\n",this,this->class_name().c_str(),copy); }
string Doxygen::getProtoName(SgDeclarationStatement *st) { SgScopeStatement *scope; if (SgVariableDeclaration* varDeclSt = isSgVariableDeclaration(st)) { // TODO: uncomment SgVariableDeclaration::get_scope removing need for this code scope = varDeclSt->get_variables().front()->get_scope(); } else { scope = st->get_scope(); } if (isSgGlobal(scope)) { return getDeclStmtName(st); } else { SgUnparse_Info info; info.set_SkipSemiColon(); info.set_SkipFunctionDefinition(); info.set_forceQualifiedNames(); info.set_skipCheckAccess(); info.set_SkipInitializer(); info.set_SkipClassSpecifier(); //AS(091507) In the new version of ROSE global qualifiers are paret of the qualified name of //a scope statement. For the documentation work we do not want that and we therefore use string::substr() //to trim of "::" from the front of the qualified name. if( scope->get_qualified_name().getString().length() > 2 ) { return scope->get_qualified_name().getString().substr(2)+("::"+getDeclStmtName(st)); } else { return getDeclStmtName(st); } //return scope->get_qualified_name().str()+("::"+getDeclStmtName(st)); } }
SgScopeStatement* AtermSupport::getAtermScopeNodeAttribute (ATerm term, const std::string & annotationName ) { SgScopeStatement* returnNode = NULL; ATerm idannot = ATgetAnnotation(term, ATmake(annotationName.c_str())); if (idannot) { #if 1 printf ("In getAtermScopeNodeAttribute(): Found an annotation: annotationName = %s \n",annotationName.c_str()); #endif char* id = NULL; // Get the associated annotation string. if (ATmatch(idannot, "<str>", &id)) { #if 1 printf ("In getAtermScopeNodeAttribute(): Found an string in the annotation: annotationName = %s id = %s \n",annotationName.c_str(),id); #endif if (translationScopeMap.find(id) != translationScopeMap.end()) { returnNode = translationScopeMap[id]; ROSE_ASSERT(returnNode != NULL); #if 1 printf ("In getAtermScopeNodeAttribute translationScopeMap: id = %s returnNode = %p = %s \n",id,returnNode,returnNode->class_name().c_str()); #endif } else { #if 1 printf ("In getAtermScopeNodeAttribute(): Node not found in translationNodeMap: returing NULL pointer \n"); #endif } } else { printf ("Error: The nested aterm associated with the annotation must be available on the aterm: annotationName = %s \n",annotationName.c_str()); ROSE_ASSERT(false); } } else { printf ("Error: The annotation not found on the aterm: annotationName = %s \n",annotationName.c_str()); ROSE_ASSERT(false); } #if 0 printf ("In AtermSupport::getAtermScopeNodeAttribute(): not yet implemented \n"); ROSE_ASSERT(false); #endif return returnNode; }
const SgFunctionDefinition* findRootFunc (const SgScopeStatement* scope) { // DQ (12/13/2011): This function is being called recursively (infinite recursion) for test2011_187.C (added support for SgTemplateFunctionDefinition). // printf ("Inside of findRootFunc(scope = %p) \n",scope); if (scope != NULL) { if (scope->variantT () == V_SgFunctionDefinition) { return isSgFunctionDefinition (scope); } else { if (scope->variantT () == V_SgTemplateFunctionDefinition) { return isSgTemplateFunctionDefinition (scope); } else { // DQ (12/13/2011): Adding test for improperly set scope. // printf ("In findRootFunc(): scope = %p = %s \n",scope,scope->class_name().c_str()); SgScopeStatement* nextOuterScope = scope->get_scope(); ROSE_ASSERT(nextOuterScope != NULL); #if 0 printf ("nextOuterScope = %p = %s \n",nextOuterScope,nextOuterScope->class_name().c_str()); #endif ROSE_ASSERT(nextOuterScope != scope); return findRootFunc(scope->get_scope()); } } } // Not found. return NULL; }
void MangledNameMapTraversal::visit ( SgNode* node) { ROSE_ASSERT(node != NULL); #if 0 printf ("MangledNameMapTraversal::visit: node = %s \n",node->class_name().c_str()); #endif // Keep track of the number of IR nodes visited numberOfNodes++; // DQ (7/4/2010): Optimizations: // 1) Only process each IR node once // 2) Only process declarations that we want to share (can we be selective?). // DQ (7/4/2010): To optimize performance, build a set of previously visited IR nodes // so that we only test IR nodes once to add them into the mangled name map. This // should be especially important where the AST is sharing nodes since shared nodes // are visited multiple times (as if they were not shared). // We need to tet if this actually optimizes the performance. if (setOfNodesPreviouslyVisited.find(node) == setOfNodesPreviouslyVisited.end()) { setOfNodesPreviouslyVisited.insert(node); } else { return; } bool sharable = shareableIRnode(node); #if 0 printf ("MangledNameMapTraversal::visit: node = %p = %s sharable = %s \n",node,node->class_name().c_str(),sharable ? "true" : "false"); #endif // DQ (7/10/2010): This is a test of the AST merge to investigate robustness. #if 1 if (sharable == true) { // Initially we will only merge things in global scope! Then // we will operate on namespaces! Then I think we are done! // Basically we can simplify the problem by skipping merging of things in // function definitions since if the function definitions are the same they // will be merged directly. // Keep track of the number of IR nodes that were considered sharable numberOfNodesSharable++; // Here is where we get much more specific about what is sharable! switch (node->variantT()) { // Since we abstract out the generation of the key we can simplify this code! #if 1 // DQ (7/11/2010): This fails for tests/nonsmoke/functional/CompileTests/mergeAST_tests/mergeTest_06.C, I don't know why! case V_SgFunctionDeclaration: #endif #if 1 // DQ (7/20/2010): Testing this case... case V_SgVariableDeclaration: case V_SgClassDeclaration: // DQ (2/10/2007): These need to be shared (but I still see "xxxxx____Lnnnn" based names) case V_SgTemplateInstantiationDecl: // DQ (2/10/2007): These should be shared case V_SgPragmaDeclaration: case V_SgTemplateInstantiationDirectiveStatement: case V_SgTypedefDeclaration: case V_SgEnumDeclaration: case V_SgTemplateDeclaration: case V_SgUsingDeclarationStatement: case V_SgUsingDirectiveStatement: // DQ (2/3/2007): Added additional declarations that we should share case V_SgMemberFunctionDeclaration: case V_SgTemplateInstantiationFunctionDecl: case V_SgTemplateInstantiationMemberFunctionDecl: #endif #if 1 // DQ (2/3/2007): Added support for symbols case V_SgClassSymbol: case V_SgEnumFieldSymbol: case V_SgEnumSymbol: case V_SgFunctionSymbol: case V_SgMemberFunctionSymbol: case V_SgLabelSymbol: case V_SgNamespaceSymbol: // DQ (2/10/2007): This case has been a problem previously case V_SgTemplateSymbol: case V_SgTypedefSymbol: case V_SgVariableSymbol: #endif #if 0 // DQ (7/20/2010): These nodes are a problem to merge, but also not important to merge // since they are contained within associated declarations. // DQ (2/20/2007): Added to list so that it could be process to build the delete list // statement fo the SgBasicBlock have to be considerd for the delete list. However, // it is still not meaningful since we don't generate a unique name for the SgBasicBlock // so it will never be shared. // case V_SgBasicBlock: case V_SgClassDefinition: case V_SgTemplateInstantiationDefn: case V_SgFunctionDefinition: case V_SgVariableDefinition: #endif #if 1 // DQ (5/29/2006): Added support for types case V_SgFunctionType: case V_SgMemberFunctionType: case V_SgModifierType: case V_SgPointerType: // DQ (5/29/2006): Added support for types case V_SgClassType: case V_SgEnumType: case V_SgTypedefType: // DQ (2/10/2007): Add this case case V_SgTemplateArgument: // DQ (3/17/2007): These should be shared, I think! case V_SgPragma: // DQ (5/20/2006): Initialized names are held in SgVariableDeclaration IR // nodes or other sharable structures so we don't have to share these. // But we have to permit them all to be shared because all pointers to // them need to be reset they all need to be reset. case V_SgInitializedName: #endif #if 1 { // DQ (7/4/2010): To improve the performance avoid regenerating the unique name for the same IR nodes when it is revisited! // Make the use of false in generateUniqueName() more clear. We need to // distinguish between defining and non-defining declarations in the generation // of unique names for the AST merge. // string key = generateUniqueName(node,false); bool ignoreDifferenceBetweenDefiningAndNondefiningDeclarations = false; string key = SageInterface::generateUniqueName(node,ignoreDifferenceBetweenDefiningAndNondefiningDeclarations); ROSE_ASSERT(key.empty() == false); #if 1 SgDeclarationStatement* declaration = isSgDeclarationStatement(node); if (declaration != NULL) { // ROSE_ASSERT(declaration->get_symbol_from_symbol_table() != NULL); // DQ (7/4/2007): Some SgDeclarationStatement IR nodes don't have a representation // in the symbol table (the list of SgInitializedName object have them instead). if (isSgVariableDeclaration(declaration) == NULL && isSgVariableDefinition(declaration) == NULL && isSgUsingDeclarationStatement(declaration) == NULL && isSgUsingDirectiveStatement(declaration) == NULL && isSgTemplateInstantiationDirectiveStatement(declaration) == NULL && isSgPragmaDeclaration(declaration) == NULL) { // DQ (6/8/2010): Only do this test for non compiler generated variable...(e.g. __default_member_function_pointer_name // is compiler generated to handle function pointers where no member function id specified). if (declaration->get_startOfConstruct()->isCompilerGenerated() == false) { SgSymbol* symbol = declaration->search_for_symbol_from_symbol_table(); if (symbol == NULL) { // Output more information to support debugging! printf ("declaration = %p = %s = %s \n",declaration,declaration->class_name().c_str(),SageInterface::get_name(declaration).c_str()); SgScopeStatement* scope = declaration->get_scope(); ROSE_ASSERT(scope != NULL); printf (" scope = %p = %s = %s \n",scope,scope->class_name().c_str(),SageInterface::get_name(scope).c_str()); declaration->get_startOfConstruct()->display("declaration->search_for_symbol_from_symbol_table() == NULL"); } ROSE_ASSERT(symbol != NULL); } } #if 0 // DQ (6/23/2010): Added the base type of the typedef SgTypedefDeclaration* typedefDeclaration = isSgTypedefDeclaration(declaration); if (typedefDeclaration != NULL) { } #endif } #endif addToMap(key,node); // Keep track of the number of IR nodes that were evaluated for mangled name matching numberOfNodesEvaluated++; break; } #endif default: { // Nothing to do here } } } #endif }
void MidLevelRewrite<MidLevelInterfaceNodeCollection>:: insert ( SgStatement* target, const string & transformationString, ScopeIdentifierEnum inputRelativeScope, PlacementPositionEnum locationInScope ) { ROSE_ASSERT (target != NULL); #if 0 printf ("MidLevelRewrite<MidLevelInterfaceNodeCollection>::insert(): inputRelativeScope = %s \n", MidLevelCollectionTypedefs::getRelativeScopeString(inputRelativeScope).c_str()); #endif // Error Reporting // Test to see if this is a supported statement (a few are not supported in the rewrite mechanism) if ( insertSupported(target,inputRelativeScope) == false ) { printf ("ERROR (MidLevelRewrite<MidLevelInterfaceNodeCollection>::insert): \n"); printf (" This %s statement is not currently supported (or not supported \n",target->sage_class_name()); printf (" in its position relative to other statements) within the AST Rewrite Mechanism. \n"); printf ("Work Around: \n"); printf (" Use the parent node as a target instead (and specify corresponding appropriate (longer) string. \n"); printf (" exiting ... \n"); ROSE_ABORT(); } // Use a more general interface to allow relative strings to be used in the high level interface // even though they are not required mid level interface. MidLevelInterfaceNodeCollection stringAndNodeCollection; // DQ (1/15/2003): Needed to add handling of StatementScope insert handling. // The specification for scope can be either surrounding scope of statement scope // If it is statement scope then the target must contain a list of statements. ScopeIdentifierEnum scope = inputRelativeScope; if (scope == MidLevelCollectionTypedefs::StatementScope) { // Not clear if this is a sufficent test ROSE_ASSERT (isSgScopeStatement(target) != NULL); // Now specify a new target (use any statement inside the scope pointer to by target) // printf ("Before resetting target is a: %s \n",target->sage_class_name()); // Not clear if this works for ALL scope statements! SgScopeStatement* scope = isSgScopeStatement(target); ROSE_ASSERT (scope != NULL); SgStatementPtrList statementList = scope->generateStatementList(); resetTargetStatementAndLocation (target,statementList,locationInScope); // Reset the target to a statement in the scope pointed to by the target ROSE_ASSERT (target != NULL); // printf ("Reset target is a: %s \n",target->sage_class_name()); // printf ("Reset target location is: %s \n",MidLevelCollectionTypedefs::getRelativeLocationString(locationInScope).c_str()); } bool buildInNewScope = false; TransformationStringTemplatedType<MidLevelCollectionTypedefs> transformation (target,transformationString,scope,locationInScope,buildInNewScope); #if 0 transformation.display("In mid level insert"); #endif #if 0 printf ("After display ... exiting ... \n"); ROSE_ASSERT (false); #endif stringAndNodeCollection.addString(target,transformation); #if 0 // PreamblePositionInScope = 1 /*!< Source code to be placed at the top of a specified scope */ , // TopOfCurrentScope = 2 /*!< Top of scope (current location must be a scope) */ , // BeforeCurrentPosition = 3 /*!< Before */ , // ReplaceCurrentPosition = 4 /*!< Replace */ , // AfterCurrentPosition = 5 /*!< After */ , // BottomOfCurrentScope = 6 /*!< Bottom of scope (current location must be a scope) */ , #endif // Be careful to include/exclude the current statement when generating the prefix! bool prefixIncludesCurrentStatement = true; switch(locationInScope) { case MidLevelCollectionTypedefs::TopOfCurrentScope: case MidLevelCollectionTypedefs::BeforeCurrentPosition: case MidLevelCollectionTypedefs::ReplaceCurrentPosition: prefixIncludesCurrentStatement = false; break; case MidLevelCollectionTypedefs::AfterCurrentPosition: case MidLevelCollectionTypedefs::BottomOfCurrentScope: prefixIncludesCurrentStatement = true; break; default: printf ("Error, default reached in MidLevelRewrite<MidLevelInterfaceNodeCollection>::insert() \n"); ROSE_ASSERT (false); } #if 0 printf ("In MidLevelRewrite<MidLevelInterfaceNodeCollection>::insert() prefixIncludesCurrentStatement = %s \n", (prefixIncludesCurrentStatement == true) ? "true" : "false"); #endif stringAndNodeCollection.writeAllChangeRequests(target,prefixIncludesCurrentStatement); #if 0 printf ("Exiting in MidLevelRewrite<MidLevelInterfaceNodeCollection>::insert() \n"); ROSE_ABORT(); #endif }
void FixupAstSymbolTablesToSupportAliasedSymbols::visit ( SgNode* node ) { // DQ (11/24/2007): Output the current IR node for debugging the traversal of the Fortran AST. #if ALIAS_SYMBOL_DEBUGGING printf ("In FixupAstSymbolTablesToSupportAliasedSymbols::visit() (preorder AST traversal) node = %p = %s \n",node,node->class_name().c_str()); #endif #if 0 // DQ (7/23/2011): New support for linking namespaces sharing the same name (mangled name). // std::map<SgName,std::vector<SgNamespaceDefinition*> > namespaceMap; SgNamespaceDefinitionStatement* namespaceDefinition = isSgNamespaceDefinitionStatement(node); if (namespaceDefinition != NULL) { // DQ (7/23/2011): Assemble namespaces with the same name into vectors defined in the map // accessed using the name of the namespace as a key. #error "DEAD CODE" SgName name = namespaceDefinition->get_namespaceDeclaration()->get_name(); #if ALIAS_SYMBOL_DEBUGGING printf ("In FixupAstSymbolTablesToSupportAliasedSymbols: namespace definition found for name = %s #symbols = %d \n",name.str(),namespaceDefinition->get_symbol_table()->size()); #endif // It is important to use mangled names to define unique names when namespaces are nested. SgName mangledNamespaceName = namespaceDefinition->get_namespaceDeclaration()->get_mangled_name(); #if ALIAS_SYMBOL_DEBUGGING printf ("In FixupAstSymbolTablesToSupportAliasedSymbols: namespace definition associated mangled name = %s \n",mangledNamespaceName.str()); #endif // DQ (7/23/2011): Fixup the name we use as a key in the map to relect that some namespaces don't have a name. if (name == "") { // Modify the mangled name to reflect the unnamed namespace... #if ALIAS_SYMBOL_DEBUGGING printf ("Warning in FixupAstSymbolTablesToSupportAliasedSymbols::visit(): Unnamed namespaces shuld be mangled to reflect the lack of a name \n"); #endif mangledNamespaceName += "_unnamed_namespace"; } #if ALIAS_SYMBOL_DEBUGGING printf ("namespace definition associated mangled name = %s \n",mangledNamespaceName.str()); #endif #if ALIAS_SYMBOL_DEBUGGING printf ("In FixupAstSymbolTablesToSupportAliasedSymbols: associated mangled name = %s namespaceMap size = %" PRIuPTR " \n",mangledNamespaceName.str(),namespaceMap.size()); #endif std::map<SgName,std::vector<SgNamespaceDefinitionStatement*> >::iterator i = namespaceMap.find(mangledNamespaceName); if (i != namespaceMap.end()) { std::vector<SgNamespaceDefinitionStatement*> & namespaceVector = i->second; #if ALIAS_SYMBOL_DEBUGGING printf ("In FixupAstSymbolTablesToSupportAliasedSymbols: (found an entry): Namespace vector size = %" PRIuPTR " \n",namespaceVector.size()); #endif // Testing each entry... for (size_t j = 0; j < namespaceVector.size(); j++) { ROSE_ASSERT(namespaceVector[j] != NULL); SgName existingNamespaceName = namespaceVector[j]->get_namespaceDeclaration()->get_name(); #if ALIAS_SYMBOL_DEBUGGING printf ("Existing namespace (SgNamespaceDefinitionStatement) %p = %s \n",namespaceVector[j],existingNamespaceName.str()); #endif if (j > 0) { ROSE_ASSERT(namespaceVector[j]->get_previousNamespaceDefinition() != NULL); } if (namespaceVector.size() > 1 && j < namespaceVector.size() - 2) { ROSE_ASSERT(namespaceVector[j]->get_nextNamespaceDefinition() != NULL); } } #error "DEAD CODE" size_t namespaceListSize = namespaceVector.size(); if (namespaceListSize > 0) { size_t lastNamespaceIndex = namespaceListSize - 1; // DQ (5/9/2013): Before setting these, I think they should be unset (to NULL values). // ROSE_ASSERT(namespaceVector[lastNamespaceIndex]->get_nextNamespaceDefinition() == NULL); // ROSE_ASSERT(namespaceDefinition->get_previousNamespaceDefinition() == NULL); // ROSE_ASSERT(namespaceVector[lastNamespaceIndex]->get_nextNamespaceDefinition() == NULL); ROSE_ASSERT(namespaceDefinition->get_previousNamespaceDefinition() != NULL); // namespaceVector[lastNamespaceIndex]->set_nextNamespaceDefinition(namespaceDefinition); #if 1 printf ("namespaceVector[lastNamespaceIndex]->get_nextNamespaceDefinition() = %p \n",namespaceVector[lastNamespaceIndex]->get_nextNamespaceDefinition()); #endif if (namespaceVector[lastNamespaceIndex]->get_nextNamespaceDefinition() == NULL) { namespaceVector[lastNamespaceIndex]->set_nextNamespaceDefinition(namespaceDefinition); } else { // DQ (5/9/2013): If this is already set then make sure it was set to the correct value. ROSE_ASSERT(namespaceVector[lastNamespaceIndex]->get_nextNamespaceDefinition() == namespaceDefinition); } #error "DEAD CODE" // DQ (5/9/2013): If this is already set then make sure it was set to the correct value. // namespaceDefinition->set_previousNamespaceDefinition(namespaceVector[lastNamespaceIndex]); ROSE_ASSERT(namespaceDefinition->get_previousNamespaceDefinition() != NULL); ROSE_ASSERT(namespaceDefinition->get_previousNamespaceDefinition() == namespaceVector[lastNamespaceIndex]); // DQ (5/9/2013): I think I can assert this. ROSE_ASSERT(namespaceVector[lastNamespaceIndex]->get_namespaceDeclaration()->get_name() == namespaceDefinition->get_namespaceDeclaration()->get_name()); ROSE_ASSERT(namespaceDefinition->get_previousNamespaceDefinition() != NULL); #if 1 printf ("namespaceDefinition = %p namespaceDefinition->get_nextNamespaceDefinition() = %p \n",namespaceDefinition,namespaceDefinition->get_nextNamespaceDefinition()); #endif // ROSE_ASSERT(namespaceDefinition->get_nextNamespaceDefinition() == NULL); // ROSE_ASSERT(namespaceVector[lastNamespaceIndex]->get_nextNamespaceDefinition() == NULL); } // Add the namespace matching a previous name to the list. namespaceVector.push_back(namespaceDefinition); #error "DEAD CODE" // Setup scopes as sources and distinations of alias symbols. SgNamespaceDefinitionStatement* referencedScope = namespaceDefinition->get_previousNamespaceDefinition(); ROSE_ASSERT(referencedScope != NULL); SgNamespaceDefinitionStatement* currentScope = namespaceDefinition; ROSE_ASSERT(currentScope != NULL); #if ALIAS_SYMBOL_DEBUGGING printf ("In FixupAstSymbolTablesToSupportAliasedSymbols: Suppress injection of symbols from one namespace to the other for each reintrant namespace \n"); printf ("In FixupAstSymbolTablesToSupportAliasedSymbols: referencedScope #symbols = %d currentScope #symbols = %d \n",referencedScope->get_symbol_table()->size(),currentScope->get_symbol_table()->size()); printf ("In FixupAstSymbolTablesToSupportAliasedSymbols: referencedScope = %p currentScope = %p \n",referencedScope,currentScope); #endif #if 1 // Generate the alias symbols from the referencedScope and inject into the currentScope. injectSymbolsFromReferencedScopeIntoCurrentScope(referencedScope,currentScope,SgAccessModifier::e_default); #endif } else { #if ALIAS_SYMBOL_DEBUGGING printf ("In FixupAstSymbolTablesToSupportAliasedSymbols: (entry NOT found): Insert namespace %p for name = %s into the namespaceMap \n",namespaceDefinition,mangledNamespaceName.str()); #endif std::vector<SgNamespaceDefinitionStatement*> list(1); ROSE_ASSERT(list.size() == 1); #error "DEAD CODE" list[0] = namespaceDefinition; #if 0 // DQ (3/11/2012): New code, but maybe we should instead put the implicit "std" namespace into the global scope more directly. if (mangledNamespaceName == "std" && false) { // This case has to be handled special since the implicit "std" namespace primary declaration was // constructed but not added to the global scope. But maybe it should be. } else { // DQ (7/24/2011): get_nextNamespaceDefinition() == NULL is false in the case of the AST copy tests // (see tests/nonsmoke/functional/CompileTests/copyAST_tests/copytest2007_30.C). Only get_nextNamespaceDefinition() // appears to sometimes be non-null, so we reset them both to NULL just to make sure. namespaceDefinition->set_nextNamespaceDefinition(NULL); namespaceDefinition->set_previousNamespaceDefinition(NULL); ROSE_ASSERT(namespaceDefinition->get_nextNamespaceDefinition() == NULL); ROSE_ASSERT(namespaceDefinition->get_previousNamespaceDefinition() == NULL); } #else // DQ (7/24/2011): get_nextNamespaceDefinition() == NULL is false in the case of the AST copy tests // (see tests/nonsmoke/functional/CompileTests/copyAST_tests/copytest2007_30.C). Only get_nextNamespaceDefinition() // appears to sometimes be non-null, so we reset them both to NULL just to make sure. namespaceDefinition->set_nextNamespaceDefinition(NULL); namespaceDefinition->set_previousNamespaceDefinition(NULL); ROSE_ASSERT(namespaceDefinition->get_nextNamespaceDefinition() == NULL); ROSE_ASSERT(namespaceDefinition->get_previousNamespaceDefinition() == NULL); #endif namespaceMap.insert(std::pair<SgName,std::vector<SgNamespaceDefinitionStatement*> >(mangledNamespaceName,list)); #error "DEAD CODE" #if ALIAS_SYMBOL_DEBUGGING printf ("namespaceMap.size() = %" PRIuPTR " \n",namespaceMap.size()); #endif } } #error "DEAD CODE" #else // DQ (5/23/2013): Commented out since we now have a newer and better namespace support for symbol handling. // printf ("NOTE:: COMMENTED OUT old support for namespace declarations in FixupAstSymbolTablesToSupportAliasedSymbols traversal \n"); #endif SgUseStatement* useDeclaration = isSgUseStatement(node); if (useDeclaration != NULL) { // This must be done in the Fortran AST construction since aliased symbols must be inserted // before they are looked up as part of name resolution of variable, functions, and types. // For C++ we can be more flexible and support the construction of symbol aliases within // post-processing. } // DQ (4/14/2010): Added this C++ specific support. // In the future we may want to support the injection of alias symbols for C++ "using" directives and "using" declarations. SgUsingDeclarationStatement* usingDeclarationStatement = isSgUsingDeclarationStatement(node); if (usingDeclarationStatement != NULL) { #if ALIAS_SYMBOL_DEBUGGING printf ("Found the SgUsingDeclarationStatement \n"); #endif SgScopeStatement* currentScope = usingDeclarationStatement->get_scope(); ROSE_ASSERT(currentScope != NULL); SgDeclarationStatement* declaration = usingDeclarationStatement->get_declaration(); SgInitializedName* initializedName = usingDeclarationStatement->get_initializedName(); // Only one of these can be non-null. ROSE_ASSERT(initializedName != NULL || declaration != NULL); ROSE_ASSERT( (initializedName != NULL && declaration != NULL) == false); if (declaration != NULL) { #if ALIAS_SYMBOL_DEBUGGING printf ("In FixupAstSymbolTablesToSupportAliasedSymbols::visit(): declaration = %p = %s \n",declaration,declaration->class_name().c_str()); #endif } else { if (initializedName != NULL) { #if ALIAS_SYMBOL_DEBUGGING printf ("In FixupAstSymbolTablesToSupportAliasedSymbols::visit(): initializedName = %s \n",initializedName->get_name().str()); #endif } else { printf ("Error: both declaration and initializedName in SgUsingDeclarationStatement are NULL \n"); ROSE_ASSERT(false); } } #if 0 printf ("Exiting at the base of FixupAstSymbolTablesToSupportAliasedSymbols::visit() \n"); ROSE_ASSERT(false); #endif } SgUsingDirectiveStatement* usingDirectiveStatement = isSgUsingDirectiveStatement(node); if (usingDirectiveStatement != NULL) { #if ALIAS_SYMBOL_DEBUGGING printf ("Found the SgUsingDirectiveStatement \n"); #endif SgNamespaceDeclarationStatement* namespaceDeclaration = usingDirectiveStatement->get_namespaceDeclaration(); ROSE_ASSERT(namespaceDeclaration != NULL); SgScopeStatement* currentScope = usingDirectiveStatement->get_scope(); // To be more specific this is really a SgNamespaceDefinitionStatement SgScopeStatement* referencedScope = namespaceDeclaration->get_definition(); if (referencedScope == NULL) { // DQ (5/21/2010): Handle case of using "std" (predefined namespace in C++), but it not having been explicitly defined (see test2005_57.C). if (namespaceDeclaration->get_name() != "std") { printf ("ERROR: namespaceDeclaration has no valid definition \n"); namespaceDeclaration->get_startOfConstruct()->display("ERROR: namespaceDeclaration has no valid definition"); // DQ (5/20/2010): Added assertion to trap this case. printf ("Exiting because referencedScope could not be identified.\n"); ROSE_ASSERT(false); } } // Note that "std", as a predefined namespace, can have a null definition, so we can't // insist that we inject all symbols in namespaces that we can't see explicitly. if (referencedScope != NULL) { ROSE_ASSERT(referencedScope != NULL); ROSE_ASSERT(currentScope != NULL); #if 0 printf ("Calling injectSymbolsFromReferencedScopeIntoCurrentScope() for usingDirectiveStatement = %p = %s \n",node,node->class_name().c_str()); #endif injectSymbolsFromReferencedScopeIntoCurrentScope(referencedScope,currentScope,usingDirectiveStatement,SgAccessModifier::e_default); } #if 0 printf ("Exiting at the base of FixupAstSymbolTablesToSupportAliasedSymbols::visit() \n"); ROSE_ASSERT(false); #endif } // DQ (5/6/2011): Added support to build SgAliasSymbols in derived class scopes that reference the symbols of the base classes associated with protected and public declarations. SgClassDefinition* classDefinition = isSgClassDefinition(node); if (classDefinition != NULL) { // Handle any derived classes. SgBaseClassPtrList & baseClassList = classDefinition->get_inheritances(); SgBaseClassPtrList::iterator i = baseClassList.begin(); for ( ; i != baseClassList.end(); ++i) { // Check each base class. SgBaseClass* baseClass = *i; ROSE_ASSERT(baseClass != NULL); /* skip processing for SgExpBaseClasses (which don't have to define p_base_class) */ if (baseClass->variantT() == V_SgExpBaseClass) { continue; } // printf ("baseClass->get_baseClassModifier().displayString() = %s \n",baseClass->get_baseClassModifier().displayString().c_str()); // printf ("baseClass->get_baseClassModifier().get_accessModifier().displayString() = %s \n",baseClass->get_baseClassModifier().get_accessModifier().displayString().c_str()); // if (baseClass->get_modifier() == SgBaseClass::e_virtual) if (baseClass->get_baseClassModifier().get_modifier() == SgBaseClassModifier::e_virtual) { // Not clear if virtual as a modifier effects the handling of alias symbols. // printf ("Not clear if virtual as a modifier effects the handling of alias symbols. \n"); } // DQ (6/22/2011): Define the access level for alias symbol's declarations to be included. SgAccessModifier::access_modifier_enum accessLevel = baseClass->get_baseClassModifier().get_accessModifier().get_modifier(); SgClassDeclaration* tmpClassDeclaration = baseClass->get_base_class(); ROSE_ASSERT(tmpClassDeclaration != NULL); #if 0 // ROSE_ASSERT(tmpClassDeclaration->get_definingDeclaration() != NULL); SgClassDeclaration* targetClassDeclaration = isSgClassDeclaration(tmpClassDeclaration->get_definingDeclaration()); ROSE_ASSERT(targetClassDeclaration != NULL); SgScopeStatement* referencedScope = targetClassDeclaration->get_definition(); // We need this function to restrict it's injection of symbol to just those that are associated with public and protected declarations. injectSymbolsFromReferencedScopeIntoCurrentScope(referencedScope,classDefinition,accessLevel); #else // DQ (2/25/2012) We only want to inject the symbol where we have identified the defining scope. if (tmpClassDeclaration->get_definingDeclaration() != NULL) { SgClassDeclaration* targetClassDeclaration = isSgClassDeclaration(tmpClassDeclaration->get_definingDeclaration()); ROSE_ASSERT(targetClassDeclaration != NULL); SgScopeStatement* referencedScope = targetClassDeclaration->get_definition(); #if 0 printf ("Calling injectSymbolsFromReferencedScopeIntoCurrentScope() for classDefinition = %p = %s baseClass = %p accessLevel = %d \n", node,node->class_name().c_str(),baseClass,accessLevel); #endif // DQ (7/12/2014): Use the SgBaseClass as the causal node that has triggered the insertion of the SgAliasSymbols. // We need this function to restrict it's injection of symbol to just those that are associated with public and protected declarations. injectSymbolsFromReferencedScopeIntoCurrentScope(referencedScope,classDefinition,baseClass,accessLevel); } else { // DQ (2/25/2012): Print a warning message when this happens (so far only test2012_08.C). if (SgProject::get_verbose() > 0) { mprintf ("WARNING: In FixupAstSymbolTablesToSupportAliasedSymbols::visit(): Not really clear how to handle this case where tmpClassDeclaration->get_definingDeclaration() == NULL! \n"); } } #endif } } SgFunctionDeclaration* functionDeclaration = isSgFunctionDeclaration(node); if (functionDeclaration != NULL) { #if ALIAS_SYMBOL_DEBUGGING printf ("Found a the SgFunctionDeclaration \n"); #endif // SgScopeStatement* functionScope = functionDeclaration->get_scope(); SgScopeStatement* currentScope = isSgScopeStatement(functionDeclaration->get_parent()); SgClassDefinition* classDefinition = isSgClassDefinition(currentScope); if (classDefinition != NULL) { // This is a function declared in a class definition, test of friend (forget why it is important to test for isOperator(). if (functionDeclaration->get_declarationModifier().isFriend() == true || functionDeclaration->get_specialFunctionModifier().isOperator() == true) { // printf ("Process all friend function with a SgAliasSymbol to where they are declared in another scope (usually global scope) \n"); #if 0 SgName name = functionDeclaration->get_name(); SgSymbol* symbol = functionDeclaration->search_for_symbol_from_symbol_table(); ROSE_ASSERT ( symbol != NULL ); SgAliasSymbol* aliasSymbol = new SgAliasSymbol (symbol); // Use the current name and the alias to the symbol currentScope->insert_symbol(name,aliasSymbol); #endif #if 0 printf ("Error: friend functions not processed yet! \n"); ROSE_ASSERT(false); #endif } } } #if ALIAS_SYMBOL_DEBUGGING printf ("Leaving FixupAstSymbolTablesToSupportAliasedSymbols::visit() (preorder AST traversal) node = %p = %s \n",node,node->class_name().c_str()); #endif }
void SimpleInstrumentation::visit ( SgNode* astNode ) { switch(astNode->variantT()) { case V_SgFunctionCallExp: { SgFunctionCallExp *functionCallExp = isSgFunctionCallExp(astNode); SgExpression *function = functionCallExp->get_function(); ROSE_ASSERT(function); switch (function->variantT()) { case V_SgFunctionRefExp: { SgFunctionRefExp *functionRefExp = isSgFunctionRefExp(function); SgFunctionSymbol *symbol = functionRefExp->get_symbol(); ROSE_ASSERT(symbol != NULL); SgFunctionDeclaration *functionDeclaration = symbol->get_declaration(); ROSE_ASSERT(functionDeclaration != NULL); if (symbol == functionSymbol) { // Now we know that we have found the correct function call // (even in the presence of overloading or other forms of hidding) // Now fixup the symbol and type of the SgFunctionRefExp object to // reflect the new function to be called (after this we still have to // fixup the argument list in the SgFunctionCallExp. // We only want to build the decalration once (and insert it into the global scope) // after that we save the symbol and reuse it. if (newFunctionSymbol == NULL) { SgFunctionType* originalFunctionType = isSgFunctionType(functionSymbol->get_type()); ROSE_ASSERT(originalFunctionType != NULL); newFunctionSymbol = buildNewFunctionDeclaration (TransformationSupport::getStatement(astNode),originalFunctionType); } ROSE_ASSERT(newFunctionSymbol != NULL); ROSE_ASSERT(newFunctionSymbol->get_type() != NULL); functionRefExp->set_symbol(newFunctionSymbol); } break; } default: cerr<<"warning: unrecognized variant: "<<function->class_name(); } break; } case V_SgFunctionDeclaration: { SgFunctionDeclaration* functionDeclaration = isSgFunctionDeclaration(astNode); string functionName = functionDeclaration->get_name().str(); if (functionName == "send") { SgFunctionType *functionType = functionDeclaration->get_type(); ROSE_ASSERT(functionType != NULL); bool foundFunction = false; if (functionType->get_return_type()->unparseToString() == "ssize_t") { SgTypePtrList & argumentList = functionType->get_arguments(); SgTypePtrList::iterator i = argumentList.begin(); if ( (*i++)->unparseToString() == "int" ) if ( (*i++)->unparseToString() == "const void *" ) if ( (*i++)->unparseToString() == "size_t" ) if ( (*i++)->unparseToString() == "int" ) foundFunction = true; } if (foundFunction == true) { // Now get the sysmbol using functionType SgScopeStatement *scope = functionDeclaration->get_scope(); ROSE_ASSERT(scope != NULL); functionSymbol = scope->lookup_function_symbol (functionName,functionType); } } break; } default: { // No other special cases } } }
string nodeColor( SgStatement* statement ) { /* color: colorCode:red:on color: colorCode:orange:on color: colorCode:yellow:on color: colorCode:blue:on color: colorCode:green:on color: colorCode:violet:on color: colorCode:brown:on color: colorCode:purple:on color: colorCode:lightblue:on color: colorCode:lightgreen:on color: colorCode:lightred:on color: colorCode:black:on color: colorCode:darkblue:on color: colorCode:grey:on color: colorCode:darkgrey:on color: colorCode:olivegreen:on color: colorCode:darkgreen:on */ string returnString; SgDeclarationStatement* declarationStatement = isSgDeclarationStatement(statement); if (declarationStatement != NULL) { switch (declarationStatement->variantT()) { case V_SgFunctionDeclaration: case V_SgMemberFunctionDeclaration: case V_SgTemplateInstantiationFunctionDecl: case V_SgTemplateInstantiationMemberFunctionDecl: returnString = "orange"; break; case V_SgClassDeclaration: case V_SgTemplateInstantiationDecl: returnString = "yellow"; break; case V_SgAsmStmt: case V_SgCtorInitializerList: case V_SgEnumDeclaration: case V_SgFunctionParameterList: case V_SgNamespaceAliasDeclarationStatement: case V_SgNamespaceDeclarationStatement: case V_SgPragmaDeclaration: case V_SgTemplateDeclaration: case V_SgTemplateInstantiationDirectiveStatement: case V_SgTypedefDeclaration: case V_SgUsingDeclarationStatement: case V_SgUsingDirectiveStatement: case V_SgVariableDeclaration: case V_SgVariableDefinition: returnString = "lightred"; break; // DQ (11/11/2012): Added support for newer IR nodes in edg4x work. case V_SgTemplateMemberFunctionDeclaration: case V_SgTemplateClassDeclaration: case V_SgTemplateFunctionDeclaration: case V_SgTemplateVariableDeclaration: returnString = "red"; break; default: returnString = "ERROR DEFAULT REACHED"; printf ("Default reached in nodeColor() exiting ... (%s) \n",declarationStatement->class_name().c_str()); ROSE_ASSERT(false); break; } } SgScopeStatement* scopeStatement = isSgScopeStatement(statement); if (scopeStatement != NULL) { switch (scopeStatement->variantT()) { case V_SgBasicBlock: returnString = "lightblue"; break; case V_SgClassDefinition: returnString = "lightblue"; break; case V_SgTemplateInstantiationDefn: case V_SgFunctionDefinition: returnString = "lightblue"; break; case V_SgWhileStmt: case V_SgDoWhileStmt: case V_SgForStatement: returnString = "darkblue"; break; case V_SgGlobal: case V_SgIfStmt: case V_SgNamespaceDefinitionStatement: case V_SgSwitchStatement: case V_SgCatchOptionStmt: returnString = "black"; break; // DQ (11/11/2012): Added support for newer IR nodes in edg4x work. case V_SgTemplateClassDefinition: case V_SgTemplateFunctionDefinition: returnString = "red"; break; default: returnString = "ERROR DEFAULT REACHED"; printf ("Default reached in nodeColor() exiting ... (%s) \n",scopeStatement->class_name().c_str()); ROSE_ASSERT(false); break; } } if (scopeStatement == NULL && declarationStatement == NULL) { switch (statement->variantT()) { case V_SgExprStatement: returnString = "violet"; break; case V_SgBreakStmt: case V_SgCaseOptionStmt: case V_SgCatchStatementSeq: case V_SgContinueStmt: case V_SgDefaultOptionStmt: case V_SgClinkageStartStatement: case V_SgForInitStatement: case V_SgFunctionTypeTable: case V_SgGotoStatement: case V_SgLabelStatement: case V_SgNullStatement: case V_SgReturnStmt: case V_SgSpawnStmt: case V_SgTryStmt: case V_SgVariantStatement: returnString = "brown"; break; default: returnString = "ERROR DEFAULT REACHED"; printf ("Default reached in nodeColor() exiting ... (%s) \n",statement->class_name().c_str()); ROSE_ASSERT(false); break; } } return returnString; }
SgSymbol * ClangToSageTranslator::GetSymbolFromSymbolTable(clang::NamedDecl * decl) { if (decl == NULL) return NULL; SgScopeStatement * scope = SageBuilder::topScopeStack(); SgName name(decl->getNameAsString()); #if DEBUG_SYMBOL_TABLE_LOOKUP std::cerr << "Lookup symbol for: " << name << std::endl; #endif if (name == "") { return NULL; } std::list<SgScopeStatement *>::reverse_iterator it; SgSymbol * sym = NULL; switch (decl->getKind()) { case clang::Decl::Typedef: { it = SageBuilder::ScopeStack.rbegin(); while (it != SageBuilder::ScopeStack.rend() && sym == NULL) { sym = (*it)->lookup_typedef_symbol(name); it++; } break; } case clang::Decl::Var: case clang::Decl::ParmVar: { it = SageBuilder::ScopeStack.rbegin(); while (it != SageBuilder::ScopeStack.rend() && sym == NULL) { sym = (*it)->lookup_variable_symbol(name); it++; } break; } case clang::Decl::Function: { SgType * tmp_type = buildTypeFromQualifiedType(((clang::FunctionDecl *)decl)->getType()); SgFunctionType * type = isSgFunctionType(tmp_type); ROSE_ASSERT(type); it = SageBuilder::ScopeStack.rbegin(); while (it != SageBuilder::ScopeStack.rend() && sym == NULL) { sym = (*it)->lookup_function_symbol(name, type); it++; } break; } case clang::Decl::Field: { SgClassDeclaration * sg_class_decl = isSgClassDeclaration(Traverse(((clang::FieldDecl *)decl)->getParent())); ROSE_ASSERT(sg_class_decl != NULL); if (sg_class_decl->get_definingDeclaration() == NULL) std::cerr << "Runtime Error: cannot find the definition of the class/struct associate to the field: " << name << std::endl; else { scope = isSgClassDeclaration(sg_class_decl->get_definingDeclaration())->get_definition(); // TODO: for C++, if 'scope' is in 'SageBuilder::ScopeStack': problem!!! // It means that we are currently building the class while (scope != NULL && sym == NULL) { sym = scope->lookup_variable_symbol(name); scope = scope->get_scope(); } } break; } case clang::Decl::CXXRecord: case clang::Decl::Record: { it = SageBuilder::ScopeStack.rbegin(); while (it != SageBuilder::ScopeStack.rend() && sym == NULL) { sym = (*it)->lookup_class_symbol(name); it++; } break; } case clang::Decl::Label: { // Should not be reach as we use Traverse to retrieve Label (they are "terminal" statements) (it avoids the problem of forward use of label: goto before declaration) name = SgName(((clang::LabelDecl *)decl)->getStmt()->getName()); it = SageBuilder::ScopeStack.rbegin(); while (it != SageBuilder::ScopeStack.rend() && sym == NULL) { sym = (*it)->lookup_label_symbol(name); it++; } break; } case clang::Decl::EnumConstant: { name = SgName(((clang::EnumConstantDecl *)decl)->getName()); it = SageBuilder::ScopeStack.rbegin(); while (it != SageBuilder::ScopeStack.rend() && sym == NULL) { sym = (*it)->lookup_enum_field_symbol(name); it++; } break; } case clang::Decl::Enum: { name = SgName(((clang::EnumDecl *)decl)->getName()); it = SageBuilder::ScopeStack.rbegin(); while (it != SageBuilder::ScopeStack.rend() && sym == NULL) { sym = (*it)->lookup_enum_symbol(name); it++; } break; } default: std::cerr << "Runtime Error: Unknown type of Decl. (" << decl->getDeclKindName() << ")" << std::endl; } return sym; }
void fixupAstDeclarationScope( SgNode* node ) { // This function was designed to fixup what I thought were inconsistancies in how the // defining and some non-defining declarations associated with friend declarations had // their scope set. I now know this this was not a problem, but it is helpful to enforce the // consistancy. It might also be useful to process declarations with scopes set to // namespace definitions, so that the namespace definition can be normalized to be // consistant across all of the different re-entrant namespace definitions. This is // possible within the new namespace support in ROSE. TimingPerformance timer ("Fixup declaration scopes:"); // This simplifies how the traversal is called! FixupAstDeclarationScope astFixupTraversal; // DQ (1/29/2007): This traversal now uses the memory pool (so that we will visit declaration hidden in types (e.g. SgClassType) // SgClassType::traverseMemoryPoolNodes(v); astFixupTraversal.traverseMemoryPool(); // Now process the map of sets of declarations. std::map<SgDeclarationStatement*,std::set<SgDeclarationStatement*>* > & mapOfSets = astFixupTraversal.mapOfSets; #if 0 printf ("In fixupAstDeclarationScope(): mapOfSets.size() = %" PRIuPTR " \n",mapOfSets.size()); #endif std::map<SgDeclarationStatement*,std::set<SgDeclarationStatement*>* >::iterator i = mapOfSets.begin(); while (i != mapOfSets.end()) { SgDeclarationStatement* firstNondefiningDeclaration = i->first; // DQ (3/2/2015): Added assertion. ROSE_ASSERT(firstNondefiningDeclaration != NULL); // DQ (3/2/2015): Added assertion. ROSE_ASSERT(firstNondefiningDeclaration->get_firstNondefiningDeclaration() != NULL); // DQ (3/2/2015): Make this assertion a warning: fails in outlining example seq7a_test2006_78.C. // ROSE_ASSERT(firstNondefiningDeclaration == firstNondefiningDeclaration->get_firstNondefiningDeclaration()); if (firstNondefiningDeclaration != firstNondefiningDeclaration->get_firstNondefiningDeclaration()) { printf ("WARNING: In fixupAstDeclarationScope(): firstNondefiningDeclaration != firstNondefiningDeclaration->get_firstNondefiningDeclaration() \n"); printf (" --- firstNondefiningDeclaration = %p = %s \n", firstNondefiningDeclaration,firstNondefiningDeclaration->class_name().c_str()); printf (" --- firstNondefiningDeclaration->get_firstNondefiningDeclaration() = %p = %s \n", firstNondefiningDeclaration->get_firstNondefiningDeclaration(),firstNondefiningDeclaration->get_firstNondefiningDeclaration()->class_name().c_str()); } SgScopeStatement* correctScope = firstNondefiningDeclaration->get_scope(); ROSE_ASSERT(correctScope != NULL); #if 0 printf ("In FixupAstDeclarationScope::visit(): node = %p = %s firstNondefiningDeclaration = %p correctScope = %p = %s \n",node,node->class_name().c_str(),firstNondefiningDeclaration,correctScope,correctScope->class_name().c_str()); #endif std::set<SgDeclarationStatement*>* declarationSet = i->second; ROSE_ASSERT(declarationSet != NULL); #if 0 printf ("In fixupAstDeclarationScope(): mapOfSets[%p]->size() = %" PRIuPTR " \n",firstNondefiningDeclaration,mapOfSets[firstNondefiningDeclaration]->size()); #endif std::set<SgDeclarationStatement*>::iterator j = declarationSet->begin(); while (j != declarationSet->end()) { SgScopeStatement* associatedScope = (*j)->get_scope(); ROSE_ASSERT(associatedScope != NULL); // DQ (6/11/2013): This is triggered by namespace definition scopes that are different // due to re-entrant namespace declarations. We should maybe fix this. // TV (7/22/13): This is also triggered when for global scope accross files. if (associatedScope != correctScope) { // DQ (1/30/2014): Cleaning up some output spew. if (SgProject::get_verbose() > 0) { mprintf ("WARNING: This is the wrong scope (declaration = %p = %s): associatedScope = %p = %s correctScope = %p = %s \n", *j,(*j)->class_name().c_str(),associatedScope,associatedScope->class_name().c_str(),correctScope,correctScope->class_name().c_str()); } #if 0 printf ("Make this an error for now! \n"); ROSE_ASSERT(false); #endif } j++; } i++; } #if 0 printf ("Leaving fixupAstDeclarationScope() node = %p = %s \n",node,node->class_name().c_str()); #endif }
static void set_rtedenum(SgScopeStatement& n, const std::string& name, SgEnumDeclaration*& res) { res = n.lookup_enum_symbol(rtedIdentifier(name))->get_declaration(); presence_test(name, res); }
void FunctionCallNormalization::visit( SgNode *astNode ) { SgStatement *stm = isSgStatement( astNode ); // visiting all statements which may contain function calls; // Note 1: we do not look at the body of loops, or sequences of statements, but only // at statements which may contain directly function calls; all other statements will have their component parts visited in turn if ( isSgEnumDeclaration( astNode ) || isSgVariableDeclaration( astNode ) || isSgVariableDefinition( astNode ) || isSgExprStatement( astNode ) || isSgForStatement( astNode ) || isSgReturnStmt( astNode ) || isSgSwitchStatement( astNode ) ) { // maintain the mappings from function calls to expressions (variables or dereferenced variables) map<SgFunctionCallExp *, SgExpression *> fct2Var; // list of Declaration structures, one structure per function call DeclarationPtrList declarations; bool variablesDefined = false; // list of function calls, in correnspondence with the inForTest list below list<SgNode*> functionCallExpList; list<bool> inForTest; SgForStatement *forStm = isSgForStatement( stm ); SgSwitchStatement *swStm = isSgSwitchStatement( stm ); list<SgNode*> temp1, temp2; // for-loops and Switch statements have conditions ( and increment ) expressed as expressions // and not as standalone statements; this will change in future Sage versions // TODO: when for-loops and switch statements have conditions expressed via SgStatements // these cases won't be treated separately; however, do-while will have condition expressed via expression // so that will be the only exceptional case to be treated separately if (forStm != NULL) { // create a list of function calls in the condition and increment expression // the order is important, the condition is evaluated after the increment expression // temp1 = FEOQueryForNodes( forStm->get_increment_expr_root(), V_SgFunctionCallExp ); // temp2 = FEOQueryForNodes( forStm->get_test_expr_root(), V_SgFunctionCallExp ); temp1 = FEOQueryForNodes( forStm->get_increment(), V_SgFunctionCallExp ); temp2 = FEOQueryForNodes( forStm->get_test_expr(), V_SgFunctionCallExp ); functionCallExpList = temp1; functionCallExpList.splice( functionCallExpList.end(), temp2 ); } else { if (swStm != NULL) { // create a list of function calls in the condition in the order of function evaluation // DQ (11/23/2005): Fixed SgSwitchStmt to have SgStatement for conditional. // list<SgNode*> temp1 = FEOQueryForNodes( swStm->get_item_selector_root(), V_SgFunctionCallExp ); list<SgNode*> temp1 = FEOQueryForNodes( swStm->get_item_selector(), V_SgFunctionCallExp ); functionCallExpList = temp1; } else { // create a list of function calls in the statement in the order of function evaluation functionCallExpList = FEOQueryForNodes( stm, V_SgFunctionCallExp ); } } // all function calls get replaced: this is because they can occur in expressions (e.g. for-loops) // which makes it difficult to build control flow graphs if ( functionCallExpList.size() > 0 ) { cout << "--------------------------------------\nStatement "; cout << stm->unparseToString() << "\n";; // traverse the list of function calls in the current statement, generate a structure Declaration for each call // put these structures in a list to be inserted in the code later for ( list<SgNode *>::iterator i = functionCallExpList.begin(); i != functionCallExpList.end(); i++ ) { variablesDefined = true; // get function call exp SgFunctionCallExp *exp = isSgFunctionCallExp( *i ); ROSE_ASSERT ( exp ); // get type of expression, generate unique variable name SgType *expType = exp->get_type(); ROSE_ASSERT ( expType ); Sg_File_Info *location = Sg_File_Info::generateDefaultFileInfoForTransformationNode(); ROSE_ASSERT ( location ); ostringstream os; os << "__tempVar__" << location; SgName name = os.str().c_str(); // replace previous variable bindings in the AST SgExprListExp *paramsList = exp->get_args(); SgExpression *function = exp->get_function(); ROSE_ASSERT ( paramsList && function ); replaceFunctionCallsInExpression( paramsList, fct2Var ); replaceFunctionCallsInExpression( function, fct2Var ); // duplicate function call expression, for the initialization declaration and the assignment SgTreeCopy treeCopy; SgFunctionCallExp *newExpInit = isSgFunctionCallExp( exp->copy( treeCopy ) ); ROSE_ASSERT ( newExpInit ); SgFunctionCallExp *newExpAssign = isSgFunctionCallExp( exp->copy( treeCopy ) ); ROSE_ASSERT ( newExpAssign ); // variables Sg_File_Info *initLoc = Sg_File_Info::generateDefaultFileInfoForTransformationNode(), *nonInitLoc = Sg_File_Info::generateDefaultFileInfoForTransformationNode(), *assignLoc = Sg_File_Info::generateDefaultFileInfoForTransformationNode(); Declaration *newDecl = new Declaration(); SgStatement *nonInitVarDeclaration, *initVarDeclaration, *assignStmt; SgExpression *varRefExp; SgVariableSymbol *varSymbol; SgAssignOp *assignOp; SgInitializedName *initName; bool pointerTypeNeeded = false; // mark whether to replace inside or outside of ForStatement due to the // function call being inside the test or the increment for a for-loop statement // the 'inForTest' list is in 1:1 ordered correpondence with the 'declarations' list if ( forStm ) { // SgExpressionRoot // *testExp = isSgForStatement( astNode )->get_test_expr_root(), // *incrExp = isSgForStatement( astNode )->get_increment_expr_root(); SgExpression *testExp = isSgForStatement( astNode )->get_test_expr(), *incrExp = isSgForStatement( astNode )->get_increment(); SgNode *up = exp; while ( up && up != testExp && up != incrExp ) up = up->get_parent(); ROSE_ASSERT ( up ); // function call is in the condition of the for-loop if ( up == testExp ) inForTest.push_back( true ); // function call is in the increment expression else { inForTest.push_back( false ); // for increment expressions we need to be able to reassign the return value // of the function; if the ret value is a reference, we need to generate a // pointer of that type (to be able to reassign it later) if ( isSgReferenceType( expType ) ) pointerTypeNeeded = true; } } // for do-while statements: we need to generate declaration of type pointer to be able to have // non-assigned references when looping and assign them at the end of the body of the loop if ( isSgDoWhileStmt( stm->get_parent() ) && isSgReferenceType( expType ) ) pointerTypeNeeded = true; // we have a function call returning a reference and we can't initialize the variable // at the point of declaration; we need to define the variable as a pointer if ( pointerTypeNeeded ) { // create 'address of' term for function expression, so we can assign it to the pointer SgAddressOfOp *addressOp = new SgAddressOfOp( assignLoc, newExpAssign, expType ); // create noninitialized declaration SgType *base = isSgReferenceType( expType )->get_base_type(); ROSE_ASSERT( base ); SgPointerType *ptrType = SgPointerType::createType( isSgReferenceType( expType )->get_base_type() ); ROSE_ASSERT ( ptrType ); nonInitVarDeclaration = new SgVariableDeclaration ( nonInitLoc, name, ptrType ); // create assignment (symbol, varRefExp, assignment) initName = isSgVariableDeclaration( nonInitVarDeclaration )->get_decl_item( name ); ROSE_ASSERT ( initName ); varSymbol = new SgVariableSymbol( initName ); ROSE_ASSERT ( varSymbol ); varRefExp = new SgVarRefExp( assignLoc, varSymbol ); SgPointerDerefExp *ptrDeref= new SgPointerDerefExp( assignLoc, varRefExp, expType ); ROSE_ASSERT ( isSgExpression( varRefExp ) && ptrDeref ); assignOp = new SgAssignOp( assignLoc, varRefExp, addressOp, ptrType ); assignStmt = new SgExprStatement( assignLoc, assignOp ); ROSE_ASSERT ( assignStmt && nonInitVarDeclaration ); // we don't need initialized declarations in this case initVarDeclaration = NULL; // save new mapping fct2Var.insert( Fct2Var( exp, ptrDeref ) ); } else { // create (non- &)initialized declarations, initialized name & symbol SgAssignInitializer *declInit = new SgAssignInitializer( initLoc, newExpInit, expType ); ROSE_ASSERT ( declInit ); initVarDeclaration = new SgVariableDeclaration ( initLoc, name, expType, declInit ); nonInitVarDeclaration = new SgVariableDeclaration ( nonInitLoc, name, expType ); ROSE_ASSERT ( initVarDeclaration && nonInitVarDeclaration ); initName = isSgVariableDeclaration( nonInitVarDeclaration )->get_decl_item( name ); ROSE_ASSERT ( initName ); newExpInit->set_parent( initName ); varSymbol = new SgVariableSymbol( initName ); ROSE_ASSERT ( varSymbol ); // create variable ref exp varRefExp = new SgVarRefExp( assignLoc, varSymbol ); ROSE_ASSERT ( isSgVarRefExp( varRefExp ) ); // create the assignment assignOp = new SgAssignOp( assignLoc, varRefExp, newExpAssign, expType ); assignStmt = new SgExprStatement( assignLoc, assignOp ); ROSE_ASSERT ( assignStmt ); initVarDeclaration->set_parent( stm->get_parent() ); isSgVariableDeclaration( initVarDeclaration )->set_definingDeclaration( isSgDeclarationStatement( initVarDeclaration ) ); // save new mapping fct2Var.insert( Fct2Var( exp, varRefExp ) ); } // save the 'declaration' structure, with all 3 statements and the variable name newDecl->nonInitVarDeclaration = nonInitVarDeclaration; newDecl->initVarDeclaration = initVarDeclaration; newDecl->assignment = assignStmt; newDecl->name = name; nonInitVarDeclaration->set_parent( stm->get_parent() ); isSgVariableDeclaration( nonInitVarDeclaration )->set_definingDeclaration( isSgVariableDeclaration( nonInitVarDeclaration ) ); assignStmt->set_parent( stm->get_parent() ); declarations.push_back( newDecl ); } // end for } // end if fct calls in crt stmt > 1 SgScopeStatement *scope = stm->get_scope(); ROSE_ASSERT ( scope ); // insert function bindings to variables; each 'declaration' structure in the list // corresponds to one function call for ( DeclarationPtrList::iterator i = declarations.begin(); i != declarations.end(); i++ ) { Declaration *d = *i; ROSE_ASSERT ( d && d->assignment && d->nonInitVarDeclaration ); // if the current statement is a for-loop, we insert Declarations before & in the loop body, depending on the case if ( forStm ) { SgStatement *parentScope = isSgStatement( stm->get_scope() ); SgBasicBlock *body = SageInterface::ensureBasicBlockAsBodyOfFor(forStm); ROSE_ASSERT ( !inForTest.empty() && body && parentScope ); // SgStatementPtrList &list = body->get_statements(); // if function call is in loop condition, we add initialized variable before the loop and at its end // hoist initialized variable declarations outside the loop if ( inForTest.front() ) { ROSE_ASSERT ( d->initVarDeclaration ); parentScope->insert_statement( stm, d->initVarDeclaration ); // set the scope of the initializedName SgInitializedName *initName = isSgVariableDeclaration( d->initVarDeclaration )->get_decl_item( d->name ); ROSE_ASSERT ( initName ); initName->set_scope( isSgScopeStatement( parentScope ) ); ROSE_ASSERT ( initName->get_scope() ); } // function call is in loop post increment so add noninitialized variable decls above the loop else { parentScope->insert_statement( stm, d->nonInitVarDeclaration ); // set the scope of the initializedName SgInitializedName *initName = isSgVariableDeclaration( d->nonInitVarDeclaration )->get_decl_item( d->name ); ROSE_ASSERT ( initName ); initName->set_scope( isSgScopeStatement( parentScope ) ); ROSE_ASSERT ( initName->get_scope() ); } // in a for-loop, always insert assignments at the end of the loop body->get_statements().push_back( d->assignment ); d->assignment->set_parent( body ); // remove marker inForTest.pop_front(); } else { // look at the type of the enclosing scope switch ( scope->variantT() ) { // while stmts have to repeat the function calls at the end of the loop; // note there is no "break" statement, since we want to also add initialized // declarations before the while-loop case V_SgWhileStmt: { // assignments need to be inserted at the end of each while loop SgBasicBlock *body = SageInterface::ensureBasicBlockAsBodyOfWhile(isSgWhileStmt( scope ) ); ROSE_ASSERT ( body ); d->assignment->set_parent( body ); body->get_statements().push_back( d->assignment ); } // SgForInitStatement has scope SgForStatement, move declarations before the for loop; // same thing if the enclosing scope is an If, or Switch statement case V_SgForStatement: case V_SgIfStmt: case V_SgSwitchStatement: { // adding bindings (initialized variable declarations only, not assignments) // outside the statement, in the parent scope SgStatement *parentScope = isSgStatement( scope->get_parent() ); ROSE_ASSERT ( parentScope ); parentScope->insert_statement( scope, d->initVarDeclaration, true );\ // setting the scope of the initializedName SgInitializedName *initName = isSgVariableDeclaration( d->initVarDeclaration )->get_decl_item( d->name ); ROSE_ASSERT ( initName ); initName->set_scope( scope->get_scope() ); ROSE_ASSERT ( initName->get_scope() ); } break; // do-while needs noninitialized declarations before the loop, with assignments inside the loop case V_SgDoWhileStmt: { // adding noninitialized variable declarations before the body of the loop SgStatement *parentScope = isSgStatement( scope->get_parent() ); ROSE_ASSERT ( parentScope ); parentScope->insert_statement( scope, d->nonInitVarDeclaration, true ); // initialized name scope setting SgInitializedName *initName = isSgVariableDeclaration( d->nonInitVarDeclaration )->get_decl_item( d->name ); ROSE_ASSERT ( initName ); initName->set_scope( scope->get_scope() ); ROSE_ASSERT ( initName->get_scope() ); // adding assignemts at the end of the do-while loop SgBasicBlock *body = SageInterface::ensureBasicBlockAsBodyOfDoWhile( isSgDoWhileStmt(scope) ); ROSE_ASSERT ( body ); body->get_statements().push_back( d->assignment ); d->assignment->set_parent(body); } break; // for all other scopes, add bindings ( initialized declarations ) before the statement, in the same scope default: scope->insert_statement( stm, d->initVarDeclaration, true ); // initialized name scope setting SgInitializedName *initName = isSgVariableDeclaration( d->initVarDeclaration )->get_decl_item( d->name ); ROSE_ASSERT ( initName ); initName->set_scope( scope->get_scope() ); ROSE_ASSERT ( initName->get_scope() ); } } } // once we have inserted all variable declarations, we need to replace top-level calls in the original statement if ( variablesDefined ) { cout << "\tReplacing in the expression " << stm->unparseToString() << "\n"; // for ForStatements, replace expressions in condition and increment expressions, // not in the body, since those get replace later if ( forStm ) { // SgExpressionRoot *testExp = forStm->get_test_expr_root(), *incrExp = forStm->get_increment_expr_root(); SgExpression *testExp = forStm->get_test_expr(), *incrExp = forStm->get_increment(); replaceFunctionCallsInExpression( incrExp, fct2Var ); replaceFunctionCallsInExpression( testExp, fct2Var ); } else if ( swStm ) { // DQ (11/23/2005): Fixed SgSwitch to permit use of declaration for conditional // replaceFunctionCallsInExpression( swStm->get_item_selector_root(), fct2Var ); replaceFunctionCallsInExpression( swStm->get_item_selector(), fct2Var ); } else replaceFunctionCallsInExpression( stm, fct2Var ); } } // end if isSgStatement block }
void fixupInstantiatedTemplates ( SgProject* project ) { // DQ (7/12/2005): Introduce tracking of performance of ROSE. TimingPerformance timer ("AST Object Code Generation (fixupInstantiatedTemplates): time (sec) = "); // DQ (9/6/2005): I think these operations have been superseded // by the AST post processing mechanism which is more complete. printf ("In fixupInstantiatedTemplates(): I think this may be dead code! \n"); ROSE_ASSERT(false); // This must be done prior to the unparsing of the SAGE III AST, so that any transformations // (new function prototypes for the specialized template function) can be inserted before we // unparse the final code! Could be done in AST fixup! Not clear if it should be done in // the EDG/SAGE III connection! It is currently called from the backend() function just // before the unparser! // Add forward references for instantiated template functions and member functions // (which are by default defined at the bottom of the file (but should be declared // at the top once we know what instantiations should be built)). They must be // defined at the bottom since they could call other functions not yet declared in // the file. Note that this fixup is required since we have skipped the class template // definitions which would contain the declarations that we are generating. We might // need that as a solution at some point if this fails to be sufficently robust. // Build a lists of intatiatied templates Rose_STL_Container<SgNode*> classList = NodeQuery::querySubTree (project,V_SgTemplateInstantiationDecl); Rose_STL_Container<SgNode*> functionList = NodeQuery::querySubTree (project,V_SgTemplateInstantiationFunctionDecl); Rose_STL_Container<SgNode*> memberFunctionList = NodeQuery::querySubTree (project,V_SgTemplateInstantiationMemberFunctionDecl); #if 1 printf ("In fixupInstantiatedTemplates SgTemplateInstantiationDecl: classList.size() = %ld \n",classList.size()); printf ("In fixupInstantiatedTemplates SgTemplateInstantiationFunctionDecl: functionList.size() = %ld \n",functionList.size()); printf ("In fixupInstantiatedTemplates SgTemplateInstantiationMemberFunctionDecl: memberFunctionList.size() = %ld \n",memberFunctionList.size()); #endif // These are not handled yet! // ROSE_ASSERT(classList.size() == 0); // ROSE_ASSERT(functionList.size() == 0); Rose_STL_Container<SgNode*>::iterator functionIndex = functionList.begin(); while ( functionIndex != functionList.end() ) { // SgDeclarationStatement* declaration = isSgDeclarationStatement(*i); SgTemplateInstantiationFunctionDecl* templateInstantiationFunctionDeclaration = isSgTemplateInstantiationFunctionDecl(*functionIndex); ROSE_ASSERT (templateInstantiationFunctionDeclaration != NULL); // printf ("SgTemplateInstantiationFunctionDecl: *i = %p = %s \n",*functionIndex,(*functionIndex)->unparseToString().c_str()); // Mark this function as a specialization, since we have transformed it into one when we converted the // name format (e.g.from __A45_ to A<int>). The handling of templates in ROSE is one of converting // the templae instantiations (generated by EDG) into explicit template specializations so that they // can be operated upon within ROSE (for translation/optimization). printf ("Error in fixupInstantiatedTemplates (function templates): It is now an error to mark something that was not explicitly a specialization in the original source code as a specialization ... \n"); templateInstantiationFunctionDeclaration->set_specialization(SgTemplateInstantiationFunctionDecl::e_specialization); bool generateForwardDeclarationForFunction = (templateInstantiationFunctionDeclaration->isForward() == false); if ( generateForwardDeclarationForFunction == true ) { // This is just a regular member function inside of a templated class (or a class nested in a templated class) // it's associated template declaration will not have the template text (appears to be lost in EDG), but it // is not essential if we unparse the instantated template for the member function. // This is the instantiate template member function definition, the steps are: // 1) Build a forward declaration for the instantiated template (member function). // 2) Place the new forward declaration after the class containing the templated member function. } functionIndex++; } Rose_STL_Container<SgNode*>::iterator classIndex = classList.begin(); while ( classIndex != classList.end() ) { SgTemplateInstantiationDecl* templateInstantiationClassDeclaration = isSgTemplateInstantiationDecl(*classIndex); ROSE_ASSERT (templateInstantiationClassDeclaration != NULL); // printf ("SgTemplateInstantiationDecl: *i = %p = %s \n",*classIndex,(*classIndex)->unparseToString().c_str()); #if 0 printf ("In fixupInstantiatedTemplates: templateInstantiationClassDeclaration = %p compilerGenerated = %s \n", templateInstantiationClassDeclaration, templateInstantiationClassDeclaration->get_file_info()->isCompilerGenerated() ? "true" : "false"); templateInstantiationClassDeclaration->get_file_info()->display("In fixupInstantiatedTemplates: templateInstantiationClassDeclaration"); #endif // Mark this class as a specialization, since we have transformed it into one when we converted the // name format (e.g.from __A45_ to A<int>). The handling of templates in ROSE is one of converting // the templae instantiations (generated by EDG) into explicit template specializations so that they // can be operated upon within ROSE (for translation/optimization). printf ("Error in fixupInstantiatedTemplates (class templates): It is now an error to mark something that was not explicitly a specialization in the original source code as a specialization ... \n"); templateInstantiationClassDeclaration->set_specialization(SgTemplateInstantiationDecl::e_specialization); bool generateForwardDeclarationForClass = (templateInstantiationClassDeclaration->isForward() == false); if ( generateForwardDeclarationForClass == true ) { // Nothing to do in this case since EDG should have already instered the forward class declaration! } classIndex++; } // Loop over the SgTemplateInstantiationMemberFunction objects and insert a function prototype. // We need the function prototypes because the template instatiation function definitions appear // at the end of the file! Rose_STL_Container<SgNode*>::iterator i = memberFunctionList.begin(); while ( i != memberFunctionList.end() ) { // SgDeclarationStatement* declaration = isSgDeclarationStatement(*i); SgTemplateInstantiationMemberFunctionDecl* templateInstantiationMemberFunctionDeclaration = isSgTemplateInstantiationMemberFunctionDecl(*i); ROSE_ASSERT (templateInstantiationMemberFunctionDeclaration != NULL); #if 0 printf ("templateInstantiationMemberFunctionDeclaration->isTemplateFunction() == %s \n", templateInstantiationMemberFunctionDeclaration->isTemplateFunction() ? "true" : "false"); #endif // Mark this function as a specialization, since we have transformed it into one when we converted the // name formate (e.g.from __A45_ to A<int>). The handling of templates in ROSE is one of converting // the templae instantiations (generated by EDG) into explicit template specializations so that they // can be operated upon within ROSE (for translation/optimization). // If this is not a truely templated function then it can't be a specialization! // if it is not a templated function then is is likely just a member function of a templated class. if (templateInstantiationMemberFunctionDeclaration->isTemplateFunction() == true) { printf ("Error in fixupInstantiatedTemplates (member function templates): It is now an error to mark something that was not explicitly a specialization in the original source code as a specialization ... \n"); templateInstantiationMemberFunctionDeclaration->set_specialization(SgTemplateInstantiationMemberFunctionDecl::e_specialization); } #if 0 // DQ (9/5/2005): Updated comment. // DQ (5/31/2005): Commented out since the class declaration itself is the forward declaration // for the member function. I don't think we need another one! Actually we do but this is now // handled within ROSE/src/frontend/SageIII/astPostProcessing/ files. bool generateForwardDeclarationForInlinedMemberFunction = (templateInstantiationMemberFunctionDeclaration->isForward() == false) && (templateInstantiationMemberFunctionDeclaration->isTemplateFunction() == false); if ( generateForwardDeclarationForInlinedMemberFunction == true ) { // This is just a regular member function inside of a templated class (or a class nested in a templated class) // it's associated template declaration will not have the templae text (appears to be lost in EDG), but it // is not essential if we unparse the instantated template for the member function. printf ("For inlined template member functions get the templateDeclaration from the class declaration \n"); // This is the instantiate template member function definition, the steps are: // 1) Build a forward declaration for the instantiated template (member function). // 2) Place the new forward declaration after the class containing the templated member function. printf ("SgTemplateInstantiationMemberFunctionDecl: *i = %p = %s \n",*i,(*i)->unparseToString().c_str()); // Call the AST's copy mechanism SgShallowCopy shallow; SgNode * forwardDeclarationNode = templateInstantiationMemberFunctionDeclaration->copy(shallow); SgTemplateInstantiationMemberFunctionDecl* forwardDeclaration = isSgTemplateInstantiationMemberFunctionDecl(forwardDeclarationNode); ROSE_ASSERT(forwardDeclaration != NULL); // find the template declaration of the class contining the member function SgClassDeclaration* classDeclaration = templateInstantiationMemberFunctionDeclaration->get_class_scope()->get_declaration(); ROSE_ASSERT(classDeclaration != NULL); SgTemplateInstantiationDecl* templateInstantiationDeclaration = isSgTemplateInstantiationDecl(classDeclaration); ROSE_ASSERT(templateInstantiationDeclaration != NULL); SgTemplateDeclaration* templateDeclaration = templateInstantiationDeclaration->get_templateDeclaration(); ROSE_ASSERT (templateDeclaration != NULL); // Reset the file info object so that we can mark this as compiler generated (copy builds a new Sg_File_Info object) // ROSE_ASSERT (forwardDeclaration->get_file_info() != NULL); // forwardDeclaration->set_file_info(new Sg_File_Info(*(forwardDeclaration->get_file_info()))); ROSE_ASSERT(forwardDeclaration->get_file_info() != templateInstantiationMemberFunctionDeclaration->get_file_info()); // Both of these may be set (implemented as bit flags internally) forwardDeclaration->get_file_info()->setCompilerGenerated(); forwardDeclaration->get_file_info()->setTransformation(); // Remove the shallow copy of the function definition forwardDeclaration->set_definition(NULL); // Mark the declaration as a forward declarations forwardDeclaration->setForward(); // Mark this function as a specialization (should be done within copy function) // forwardDeclaration->set_specialization(SgTemplateInstantiationMemberFunctionDecl::e_specialization); ROSE_ASSERT(forwardDeclaration->isSpecialization() == true); ROSE_ASSERT(forwardDeclaration->isPartialSpecialization() == false); // Now insert the forwardDeclaration after the templateDeclaration! // templateDeclaration.insert_statement(forwardDeclaration,true); SgScopeStatement* templateDeclarationScope = templateDeclaration->get_scope(); ROSE_ASSERT (templateDeclarationScope != NULL); printf ("BEFORE loop: Insert before: templateDeclarationScope = %p = %s \n",templateDeclarationScope,templateDeclarationScope->sage_class_name()); // Trace back through the scopes to find a non class declaration scope into which to put the forward declaration // Does this then work with nested template classes????? while (isSgTemplateInstantiationDefn(templateDeclarationScope) != NULL) { templateDeclarationScope = templateDeclarationScope->get_scope(); printf ("In loop templateDeclarationScope = %p = %s \n",templateDeclarationScope,templateDeclarationScope->sage_class_name()); } ROSE_ASSERT (templateDeclarationScope != NULL); printf ("AFTER loop: Insert before: templateDeclarationScope = %p = %s \n",templateDeclarationScope,templateDeclarationScope->sage_class_name()); templateDeclaration->get_file_info()->display("templateDeclaration"); templateDeclarationScope->get_file_info()->display("templateDeclarationScope"); int insertBeforeStatement = false; // printf ("Calling templateDeclarationScope->insert_statement() \n"); // templateDeclaration->insert_statement ( templateDeclaration, forwardDeclaration, insertBeforeStatement ); SgTemplateInstantiationDefn *templateClassDefinition = isSgTemplateInstantiationDefn(templateDeclarationScope); if (templateClassDefinition != NULL) { SgDeclarationStatementPtrList::iterator start = templateClassDefinition->get_members().begin(); SgDeclarationStatementPtrList::iterator end = templateClassDefinition->get_members().end(); printf ("templateDeclaration unparsed = %s \n",templateDeclaration->unparseToString().c_str()); printf ("templateDeclaration name = %s string = %s \n", templateDeclaration->get_name().str(),templateDeclaration->get_string().str()); for (SgDeclarationStatementPtrList::iterator i = start; i != end; i++) { string s = (*i)->unparseToString(); printf ("(*i)->unparseToString() = %s \n",s.c_str()); } ROSE_ASSERT(find(start,end,templateInstantiationMemberFunctionDeclaration) != end); templateDeclarationScope->insert_statement ( templateInstantiationMemberFunctionDeclaration, forwardDeclaration, insertBeforeStatement ); } else { // ROSE_ASSERT(find(templateDeclarationScope->get_members().begin(),templateDeclarationScope->get_members().end(),templateDeclaration) != templateDeclarationScope->get_members().end() ); templateDeclarationScope->insert_statement ( templateDeclaration, forwardDeclaration, insertBeforeStatement ); } printf ("forwardDeclaration = %s \n",forwardDeclaration->unparseToString().c_str()); // printf ("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD \n"); // printf ("Exiting after construction of forward declaration for template instantiation! \n"); // ROSE_ASSERT (false); } else { // This is a forward declaration (does it have the template arguments!) printf ("SgTemplateInstantiationMemberFunctionDecl: (forward) *i = %p = %s \n",*i,(*i)->unparseToString().c_str()); } #endif i++; } }
// Count the load and store bytes for the // I think we can only return expressions to calculate the value, not the actual values, // since sizeof(type) is machine dependent // Consider both scalar and array accesses by default. Consider both floating point and integer types by default. // return a pair of expressions: // load_byte_exp, and // store_byte_exp // Algorithm: // 1. Call side effect analysis to find read/write variables, some reference may trigger both read and write accesses // Accesses to the same array/scalar variable are grouped into one read (or write) access // e.g. array[i][j], array[i][j+1], array[i][j-1], etc are counted a single access // 2. Group accesses based on the types (same type? increment the same counter to shorten expression length) // 4. Iterate on the results to generate expression like 2*sizeof(float) + 5* sizeof(double) // As an approximate, we use simple analysis here assuming no function calls. std::pair <SgExpression*, SgExpression*> CountLoadStoreBytes (SgLocatedNode* input, bool includeScalars /* = true */, bool includeIntType /* = true */) { std::pair <SgExpression*, SgExpression*> result; assert (input != NULL); // the input is essentially the loop body, a scope statement SgScopeStatement* scope = isSgScopeStatement(input); // We need to record the associated loop info. //SgStatement* loop= NULL; SgForStatement* forloop = isSgForStatement(scope->get_scope()); SgFortranDo* doloop = isSgFortranDo(scope->get_scope()); if (forloop) { //loop = forloop; } else if (doloop) { //loop = doloop; } else { cerr<<"Error in CountLoadStoreBytes (): input is not loop body type:"<< input->class_name()<<endl; assert(false); } //Plan A: use and extend Qing's side effect analysis std::set<SgInitializedName*> readVars; std::set<SgInitializedName*> writeVars; bool success = SageInterface::collectReadWriteVariables (isSgStatement(input), readVars, writeVars); if (success!= true) { cout<<"Warning: CountLoadStoreBytes(): failed to collect load/store, mostly due to existence of function calls inside of loop body @ "<<input->get_file_info()->get_line()<<endl; } std::set<SgInitializedName*>::iterator it; if (debug) cout<<"debug: found read variables (SgInitializedName) count = "<<readVars.size()<<endl; for (it=readVars.begin(); it!=readVars.end(); it++) { SgInitializedName* iname = (*it); if (debug) cout<<scalar_or_array (iname->get_type()) <<" "<<iname->get_name()<<"@"<<iname->get_file_info()->get_line()<<endl; } if (!includeScalars ) readVars = filterVariables (readVars); if (debug) cout<<"debug: found write variables (SgInitializedName) count = "<<writeVars.size()<<endl; for (it=writeVars.begin(); it!=writeVars.end(); it++) { SgInitializedName* iname = (*it); if (debug) cout<<scalar_or_array(iname->get_type()) <<" "<<iname->get_name()<<"@"<<iname->get_file_info()->get_line()<<endl; } if (!includeScalars ) writeVars = filterVariables (writeVars); result.first = calculateBytes (readVars, scope, true); result.second = calculateBytes (writeVars, scope, false); return result; }
int main(int argc,char** argv) { SgProject* proj = frontend(argc,argv); fixAllPrefixPostfix(proj); initializeScopeInformation(proj); SgFunctionDeclaration* mainDecl = SageInterface::findMain(proj); SgFunctionDefinition* mainDef = mainDecl->get_definition(); //SageInterface::rebuildSymbolTable(mainDef); StaticCFG::CFG cfg(mainDef); SgIncidenceDirectedGraph *g = cfg.getGraph(); PathCollector* pathCollector = new PathCollector(g,&cfg); std::vector<SgNode*> scopeNodes = NodeQuery::querySubTree(mainDef,V_SgScopeStatement); std::vector<SgGraphNode*> visited; std::vector<SgNode*> nodes = NodeQuery::querySubTree(mainDef,V_SgWhileStmt); std::vector<SgNode*>::iterator node = nodes.begin(); for (; node != nodes.end(); node++) { SgScopeStatement* scopeOfWhile = SageInterface::getEnclosingScope(*node); SgStatementPtrList statementsInScope = scopeOfWhile->getStatementList(); SgStatementPtrList::iterator statPtr = statementsInScope.begin(); std::set<SgPragmaDeclaration*> prdecls; for (; statPtr!=statementsInScope.end();statPtr++) { if (isSgPragmaDeclaration(*statPtr)) { prdecls.insert(isSgPragmaDeclaration(*statPtr)); } } //SgExprStatement* boundingConditionStatement = isSgExprStatement(isSgWhileStmt(*node)->get_condition()); //SgExpression* boundingCondition = boundingConditionStatement->get_expression(); SgStatement* body = (isSgWhileStmt(*node)->get_body()); std::vector<std::vector<SgGraphNode*> > paths = pathCollector->getPaths(); std::cout << getPrelude() << std::endl; SgGraphNode* whileStart = cfg.cfgForBeginning(isSgWhileStmt(*node)); SgGraphNode* whileEnd = cfg.cfgForEnd(isSgWhileStmt(*node)); collectPaths(whileStart,whileEnd, pathCollector); SgGraphNode* whileOut = getWhileEndNode(isSgWhileStmt(*node),pathCollector); SgGraphNode* bodyStart = cfg.cfgForBeginning(isSgWhileStmt(*node)->get_body()); SgGraphNode* bodyEnd = cfg.cfgForEnd(isSgWhileStmt(*node)->get_body()); pathCollector->clearPaths(); collectPaths(bodyStart,whileOut,pathCollector); paths.clear(); paths = pathCollector->getPaths(); std::vector<std::vector<SgGraphNode*> >::iterator i = paths.begin(); std::set<SgVariableSymbol*> vars = getVars(pathCollector); std::string vardecls; std::string initrule; std::vector<std::string> rules= getRules(*node,pathCollector, vars, vardecls,initrule); std::cout << vardecls << std::endl; for (int i = 0; i < rules.size(); i++) { std::cout << rules[i] << std::endl; } std::set<SgPragmaDeclaration*>::iterator pr = prdecls.begin(); for (; pr != prdecls.end(); pr++) { std::set<std::string> variables; std::vector<std::string> s_expressions; std::string prag_str = get_pragma_string(*pr); bool initPrag; /*std::string rhs =*/ translateToS_Expr(prag_str,variables,s_expressions,initPrag); if (s_expressions.size() > 0) { std::string queryResult; if (initPrag) { queryResult = assumptionPragma(s_expressions,initrule); } else { queryResult = queryPragma(s_expressions,initrule); } std::cout << queryResult << std::endl; } } } backend(proj); return 0; }
void Traversal::processNames(SgNode* n, SynthesizedAttribute& synthesizedAttribute ) { // Now process the list of names for matches // Matching names (eventually we have to map this back to the AST) vector< std::pair<NameStructureType,NameStructureType> > results; SgScopeStatement* scopeStatement = isSgScopeStatement(n); ROSE_ASSERT(scopeStatement != NULL); int i_index = 0; vector<NameStructureType>::iterator i; for (i = synthesizedAttribute.nameList.begin(); i != synthesizedAttribute.nameList.end(); ++i) { ++i_index; // size_t i_length = i->size(); int j_index = 0; vector<NameStructureType>::iterator j; for (j = synthesizedAttribute.nameList.begin(); j != synthesizedAttribute.nameList.end(); ++j) { ++j_index; // We only want to visit the lower triangular part of the n^2 // matchings of names to each other. This reduces the number of // comparisions required. if (j_index <= i_index) { #if DEBUG > 1 printf ("Skipping case of " "j_index = %d <= i_index = %d (%s,%s) \n", j_index, i_index, i->c_str(), j->c_str()); #endif continue; } #if DEBUG > 2 printf ("Evaluating greatestPossibleSimilarity of " "j_index = %d <= i_index = %d (%s,%s) \n", j_index, i_index, i->c_str(), j->c_str()); #endif size_t i_length = i->size(); size_t j_length = j->size(); float greatestPossibleSimilarity = ((float)j_length) / ((float)i_length); if (greatestPossibleSimilarity > 1.0) { greatestPossibleSimilarity = 1.0 / greatestPossibleSimilarity; } if (greatestPossibleSimilarity < similarity_threshold) { #if DEBUG > 1 printf ("Skipping case of " "j_index = %d i_index = %d (%s,%s) " "greatestPossibleSimilarity = %f \n", j_index, i_index, i->c_str(), j->c_str(), greatestPossibleSimilarity); #endif continue; } #if DEBUG > 2 printf ("Evaluating similarityMetric of" "j_index = %d <= i_index = %d (%s,%s) \n", j_index, i_index, i->c_str(), j->c_str()); #endif float similarity = similarityMetric( i->c_str(), j->c_str()); if (similarity > similarity_threshold) { string lcs = longestCommonSubstring(i->c_str(), j->c_str()); #if DEBUG > 1 printf("\n\"%s\" and \"%s\" are %3.0f%% similar.\n" "One of the longest common sequences is \"%s\".\n\n", i->c_str(), j->c_str(), similarity*100, lcs.c_str()); #endif results.push_back( std::pair<NameStructureType, NameStructureType> (*i, *j)); } } }// for each synthesized attribute // Output the resulting matches of any non-empty list of results if (results.empty() == false) { if (SgProject::get_verbose() > 2) { printf ("Processing matches of name in " "scope = %p = %s = %s \n", scopeStatement, scopeStatement->class_name().c_str(), SageInterface::get_name(scopeStatement).c_str()); } vector< std::pair<NameStructureType, NameStructureType> >::iterator i; for (i = results.begin(); i != results.end(); ++i) { // Output the matching names SgNode* firstNode = i->first.associatedNode; ROSE_ASSERT(firstNode != NULL); SgNode* secondNode = i->second.associatedNode; ROSE_ASSERT(secondNode != NULL); float similarity = similarityMetric(i->first.c_str(), i->second.c_str()); int similarityPercentage = 100 * similarity; SgLocatedNode* first_node = isSgLocatedNode(i->first.associatedNode); SgLocatedNode* second_node = isSgLocatedNode(i->second.associatedNode); if (first_node != NULL && second_node != NULL && first_node != second_node) { if (Compass::IsNodeInUserLocation(first_node, source_directory_) && Compass::IsNodeInUserLocation(second_node, source_directory_)) { output_->addOutput( CompassAnalyses::VariableNameSimilarity:: CreateCheckerOutput( similarityPercentage, first_node, second_node)); } } } } }
bool FixupTemplateArguments::contains_private_type (SgType* type, SgScopeStatement* targetScope) { // DQ (4/2/2018): Note that this function now addresses requirements of supporting both private and protected types. #if DEBUGGING_USING_RECURSIVE_DEPTH // For debugging, keep track of the recursive depth. static size_t depth = 0; printf ("In contains_private_type(SgType*): depth = %zu \n",depth); ROSE_ASSERT(depth < 500); printf ("In contains_private_type(SgType*): global_depth = %zu \n",global_depth); ROSE_ASSERT(global_depth < 55); #endif // Note this is the recursive function. bool returnValue = false; #if DEBUG_PRIVATE_TYPE || 0 // DQ (1/7/2016): It is a problem to do this for some files (failing about 35 files in Cxx_tests). // The issues appears to be in the unparsing of the template arguments of the qualified names for the types. // printf ("In contains_private_type(SgType*): type = %p = %s = %s \n",type,type->class_name().c_str(),type->unparseToString().c_str()); printf ("In contains_private_type(SgType*): type = %p = %s \n",type,type->class_name().c_str()); #endif SgTypedefType* typedefType = isSgTypedefType(type); if (typedefType != NULL) { // Get the associated declaration. SgTypedefDeclaration* typedefDeclaration = isSgTypedefDeclaration(typedefType->get_declaration()); ROSE_ASSERT(typedefDeclaration != NULL); #if 0 bool isPrivate = typedefDeclaration->get_declarationModifier().get_accessModifier().isPrivate(); #else // DQ (4/2/2018): Fix this to address requirements of both private and protected class members (see Cxx11_tests/test2018_71.C). bool isPrivate = typedefDeclaration->get_declarationModifier().get_accessModifier().isPrivate() || typedefDeclaration->get_declarationModifier().get_accessModifier().isProtected(); #endif #if DEBUG_PRIVATE_TYPE || 0 printf ("typedefDeclaration isPrivate = %s \n",isPrivate ? "true" : "false"); #endif // First we need to know if this is a visable type. bool isVisable = false; #if 0 printf ("targetScope = %p = %s \n",targetScope,targetScope->class_name().c_str()); // printf ("typedefDeclaration = %p = %s \n",typedefDeclaration,typedefDeclaration->class_name().c_str()); printf ("typedefDeclaration->get_scope() = %p = %s \n",typedefDeclaration->get_scope(),typedefDeclaration->get_scope()->class_name().c_str()); #endif #if 0 printf ("SageInterface::whereAmI(targetScope): \n"); SageInterface::whereAmI(targetScope); printf ("SageInterface::whereAmI(typedefDeclaration): \n"); SageInterface::whereAmI(typedefDeclaration); #endif #if 0 printf ("\ntargetScope symbol table: \n"); targetScope->get_symbol_table()->print("targetScope"); printf ("end of symbol table \n"); printf ("\ntypedefDeclaration->get_scope() symbol table: \n"); typedefDeclaration->get_scope()->get_symbol_table()->print("typedefDeclaration->get_scope()"); printf ("end of symbol table \n\n"); #endif // Test for the trivial case of matching scope (an even better test (below) is be to make sure that the targetScope is nested in the typedef scope). if (typedefDeclaration->get_scope() == targetScope) { #if 0 printf ("In contains_private_type(SgType*): This is a typedef type from the same scope as the target declaration \n"); #endif // ROSE_ASSERT(false); // return false; isVisable = true; } else { // SgTypedefSymbol* lookupTypedefSymbolInParentScopes (const SgName & name, SgScopeStatement *currentScope = NULL); SgTypedefSymbol* typedefSymbol = SageInterface::lookupTypedefSymbolInParentScopes (typedefDeclaration->get_name(),targetScope); if (typedefSymbol != NULL) { #if 0 printf ("In contains_private_type(SgType*): This is not in the current scope but can be reached from the current scope \n"); #endif // ROSE_ASSERT(false); // return false; isVisable = true; } else { #if 0 printf ("Symbol for typedef name = %s not found in parent scopes \n",typedefDeclaration->get_name().str()); #endif // ROSE_ASSERT(false); } } #if 0 // Testing codes because it seems that "BitSet" shuld be visiable and so we need to debug this first. if (typedefDeclaration->get_name() == "BitSet") { printf ("Exiting as a test! \n"); ROSE_ASSERT(false); } #endif // If this is not private, then we are looking at what would be possbile template arguments used in a possible name qualification. // if (isPrivate == false) // if (isPrivate == false && isVisable == false) if (isVisable == false) { if (isPrivate == true) { return true; } else { // Get the scope and see if it is a template instantiation. SgScopeStatement* scope = typedefDeclaration->get_scope(); #if DEBUG_PRIVATE_TYPE || 0 printf ("++++++++++++++ Looking in parent scope for template arguments: scope = %p = %s \n",scope,scope->class_name().c_str()); #endif // Get the associated declaration. switch (scope->variantT()) { case V_SgTemplateInstantiationDefn: { SgTemplateInstantiationDefn* templateInstantiationDefinition = isSgTemplateInstantiationDefn(scope); ROSE_ASSERT(templateInstantiationDefinition != NULL); SgTemplateInstantiationDecl* templateInstantiationDeclaration = isSgTemplateInstantiationDecl(templateInstantiationDefinition->get_declaration()); ROSE_ASSERT(templateInstantiationDeclaration != NULL); SgTemplateArgumentPtrList & templateArgumentPtrList = templateInstantiationDeclaration->get_templateArguments(); for (SgTemplateArgumentPtrList::iterator i = templateArgumentPtrList.begin(); i != templateArgumentPtrList.end(); i++) { #if DEBUG_PRIVATE_TYPE printf ("recursive call to contains_private_type(%p): name = %s = %s \n",*i,(*i)->class_name().c_str(),(*i)->unparseToString().c_str()); #endif #if DEBUGGING_USING_RECURSIVE_DEPTH global_depth++; #endif bool isPrivateType = contains_private_type(*i,targetScope); #if DEBUGGING_USING_RECURSIVE_DEPTH global_depth--; #endif returnValue |= isPrivateType; } break; } default: { #if DEBUG_PRIVATE_TYPE printf ("Ignoring non-SgTemplateInstantiationDefn \n"); #endif } } } } else { // If it is visible then it need not be qualified and we don't care about if it was private. ROSE_ASSERT(isVisable == true); // returnValue = true; returnValue = false; } } else { #if DEBUG_PRIVATE_TYPE || 0 printf ("could be a wrapped type: type = %p = %s (not a template class instantiaton) \n",type,type->class_name().c_str()); if (isSgModifierType(type) != NULL) { SgModifierType* modifierType = isSgModifierType(type); SgType* base_type = modifierType->get_base_type(); printf ("--- base_type = %p = %s \n",base_type,base_type->class_name().c_str()); SgNamedType* namedType = isSgNamedType(base_type); if (namedType != NULL) { printf ("--- base_type: name = %s \n",namedType->get_name().str()); } } #endif // If this is a default SgModifierType then unwrap it. #if 0 SgModifierType* modifierType = isSgModifierType(type); if (modifierType != NULL) { #error "DEAD CODE!" // What kind of modifier is this? printf ("What kind of type modifier: %s \n",modifierType->get_typeModifier().displayString().c_str()); if (modifierType->get_typeModifier().isDefault() == true) { // This is a default mode modifier (acting as a wrapper type). type = modifierType->get_base_type(); } else { printf ("Not a default modifierType wrapper (need to handle this case) \n"); ROSE_ASSERT(false); } } #else // Strip past pointers and other wrapping modifiers (but not the typedef types, since the whole point is to detect private instatances). type = type->stripType(SgType::STRIP_MODIFIER_TYPE|SgType::STRIP_REFERENCE_TYPE|SgType::STRIP_RVALUE_REFERENCE_TYPE|SgType::STRIP_POINTER_TYPE|SgType::STRIP_ARRAY_TYPE); #endif #if 0 printf ("After stripType(): type = %p = %s \n",type,type->class_name().c_str()); SgNamedType* namedType = isSgNamedType(type); if (namedType != NULL) { printf ("--- stripType: name = %s \n",namedType->get_name().str()); } #endif ROSE_ASSERT(type != NULL); // Make sure this is not a simple template type (else we will have infinite recursion). // if (type != NULL && type->isIntegerType() == false && type->isFloatType() == false) // if (type != NULL) SgTemplateType* templateType = isSgTemplateType(type); SgClassType* classType = isSgClassType(type); SgTypeVoid* voidType = isSgTypeVoid(type); SgRvalueReferenceType* rvalueReferenceType = isSgRvalueReferenceType(type); SgFunctionType* functionType = isSgFunctionType(type); SgDeclType* declType = isSgDeclType(type); // DQ (12/7/2016): An enum type needs to be handled since the declaration might be private (but still debugging this for now). SgEnumType* enumType = isSgEnumType(type); // DQ (2/12/2017): Added specific type (causing infinite recursion for CompileTests/RoseExample_tests/testRoseHeaders_03.C. SgTypeEllipse* typeEllipse = isSgTypeEllipse(type); SgTypeUnknown* typeUnknown = isSgTypeUnknown(type); SgTypeComplex* typeComplex = isSgTypeComplex(type); // DQ (2/16/2017): This is a case causeing many C codes to fail. SgTypeOfType* typeOfType = isSgTypeOfType(type); if (type != NULL && templateType == NULL && classType == NULL && voidType == NULL && rvalueReferenceType == NULL && functionType == NULL && declType == NULL && enumType == NULL && typeEllipse == NULL && typeUnknown == NULL && typeComplex == NULL && typeOfType == NULL) { #if DEBUG_PRIVATE_TYPE || 0 printf ("found unwrapped type = %p = %s = %s (not a template class instantiaton) \n",type,type->class_name().c_str(),type->unparseToString().c_str()); #endif // if (type->isIntegerType() == false && type->isFloatType() == false) // if (type->isIntegerType() == false && type->isFloatType() == false) if (type->isIntegerType() == false && type->isFloatType() == false) { #if DEBUG_PRIVATE_TYPE || 0 printf ("Making a recursive call to contains_private_type(type): not integer or float type: type = %p = %s \n",type,type->class_name().c_str()); #endif #if DEBUGGING_USING_RECURSIVE_DEPTH depth++; global_depth++; #endif bool isPrivateType = contains_private_type(type,targetScope); #if DEBUGGING_USING_RECURSIVE_DEPTH depth--; global_depth--; #endif returnValue = isPrivateType; } else { // This can't be a private type. #if DEBUG_PRIVATE_TYPE printf ("This is an integer or float type (of some sort): type = %p = %s = %s \n",type,type->class_name().c_str(),type->unparseToString().c_str()); #endif returnValue = false; } } else { // This is where we need to resolve is any types that are associated with declarations might be private (e.g. SgEnumType). if (classType != NULL) { // Check if this is associated with a template class instantiation. #if 0 SgClassDeclaration* classDeclaration = isSgClassDeclaration(classType->get_declaration()); ROSE_ASSERT(classDeclaration != NULL); printf ("--------- classDeclaration = %p = %s = %s \n",classDeclaration,classDeclaration->class_name().c_str(),classDeclaration->get_name().str()); #endif SgTemplateInstantiationDecl* templateInstantiationDeclaration = isSgTemplateInstantiationDecl(classType->get_declaration()); if (templateInstantiationDeclaration != NULL) { #if DEBUGGING_USING_RECURSIVE_DEPTH global_depth++; #endif #if 0 printf ("Calling contains_private_type(SgTemplateArgumentPtrList): templateInstantiationDeclaration = %p = %s \n", templateInstantiationDeclaration,templateInstantiationDeclaration->get_name().str()); #endif returnValue = contains_private_type(templateInstantiationDeclaration->get_templateArguments(),targetScope); #if DEBUGGING_USING_RECURSIVE_DEPTH global_depth--; #endif #if 0 printf ("DONE: Calling contains_private_type(SgTemplateArgumentPtrList): templateInstantiationDeclaration = %p = %s \n", templateInstantiationDeclaration,templateInstantiationDeclaration->get_name().str()); #endif } #if 0 printf ("DONE: --- classDeclaration = %p = %s = %s \n",classDeclaration,classDeclaration->class_name().c_str(),classDeclaration->get_name().str()); #endif } } } #if DEBUG_PRIVATE_TYPE || 0 printf ("Leaving contains_private_type(SgType*): type = %p = %s = %s returnValue = %s \n",type,type->class_name().c_str(),type->unparseToString().c_str(),returnValue ? "true" : "false"); #endif return returnValue; }
void FixupAstSymbolTables::visit ( SgNode* node ) { // DQ (6/27/2005): Output the local symbol table from each scope. // printf ("node = %s \n",node->sage_class_name()); SgScopeStatement* scope = isSgScopeStatement(node); if (scope != NULL) { #if 0 printf ("AST Fixup: Fixup Symbol Table for %p = %s at: \n",scope,scope->class_name().c_str()); #endif SgSymbolTable* symbolTable = scope->get_symbol_table(); if (symbolTable == NULL) { #if 0 printf ("AST Fixup: Fixup Symbol Table for %p = %s at: \n",scope,scope->class_name().c_str()); scope->get_file_info()->display("Symbol Table Location"); #endif SgSymbolTable* tempSymbolTable = new SgSymbolTable(); ROSE_ASSERT(tempSymbolTable != NULL); // name this table as compiler generated! The name is a static member used to store // state for the next_symbol() functions. It is meaningless to set these. // tempSymbolTable->set_name("compiler-generated symbol table"); scope->set_symbol_table(tempSymbolTable); // reset the symbolTable using the get_symbol_table() member function symbolTable = scope->get_symbol_table(); ROSE_ASSERT(symbolTable != NULL); // DQ (2/16/2006): Set this parent directly (now tested) symbolTable->set_parent(scope); ROSE_ASSERT(symbolTable->get_parent() != NULL); } ROSE_ASSERT(symbolTable != NULL); if (symbolTable->get_parent() == NULL) { printf ("Warning: Fixing up symbolTable, calling symbolTable->set_parent() (parent not previously set) \n"); symbolTable->set_parent(scope); } ROSE_ASSERT(symbolTable->get_parent() != NULL); // Make sure that the internal hash table used in the symbol table is also present! if (symbolTable->get_table() == NULL) { // DQ (6/27/2005): There are a lot of these built, perhaps more than we really need! #if 0 printf ("AST Fixup: Building internal Symbol Table hash table (rose_hash_multimap) for %p = %s at: \n", scope,scope->sage_class_name()); scope->get_file_info()->display("Symbol Table Location"); #endif rose_hash_multimap* internalHashTable = new rose_hash_multimap(); ROSE_ASSERT(internalHashTable != NULL); symbolTable->set_table(internalHashTable); } ROSE_ASSERT(symbolTable->get_table() != NULL); SgSymbolTable::BaseHashType* internalTable = symbolTable->get_table(); ROSE_ASSERT(internalTable != NULL); // DQ (6/23/2011): Note: Declarations that reference types that have not been seen yet may be placed into the // wronge scope, then later when we see the correct scope we have a symbol in two or more symbol tables. The // code below detects and fixes this problem. // DQ (6/16/2011): List of symbols we need to remove from symbol tables where they are multibily represented. std::vector<SgSymbol*> listOfSymbolsToRemove; // DQ (6/12/2011): Fixup symbol table by removing symbols that are not associated with a declaration in the current scope. int idx = 0; SgSymbolTable::hash_iterator i = internalTable->begin(); while (i != internalTable->end()) { // DQ: removed SgName casting operator to char* // cout << "[" << idx << "] " << (*i).first.str(); ROSE_ASSERT ( (*i).first.str() != NULL ); ROSE_ASSERT ( isSgSymbol( (*i).second ) != NULL ); // printf ("Symbol number: %d (pair.first (SgName) = %s) pair.second (SgSymbol) sage_class_name() = %s \n", // idx,(*i).first.str(),(*i).second->sage_class_name()); SgSymbol* symbol = isSgSymbol((*i).second); ROSE_ASSERT ( symbol != NULL ); // We have to look at each type of symbol separately! This is because there is no virtual function, // the reason for this is that each get_declaration() function returns a different type! // ROSE_ASSERT ( symbol->get_declaration() != NULL ); switch(symbol->variantT()) { case V_SgClassSymbol: { SgClassSymbol* classSymbol = isSgClassSymbol(symbol); ROSE_ASSERT(classSymbol != NULL); ROSE_ASSERT(classSymbol->get_declaration() != NULL); SgDeclarationStatement* declarationToFindInScope = NULL; // Search for the declaration in the associated scope. declarationToFindInScope = classSymbol->get_declaration(); ROSE_ASSERT(declarationToFindInScope != NULL); SgClassDeclaration* classDeclaration = isSgClassDeclaration(declarationToFindInScope); ROSE_ASSERT(classDeclaration != NULL); SgName name = classDeclaration->get_name(); // SgType* declarationType = declarationToFindInScope->get_type(); SgType* declarationType = classDeclaration->get_type(); ROSE_ASSERT(declarationType != NULL); if (declarationToFindInScope->get_definingDeclaration() != NULL) { declarationToFindInScope = declarationToFindInScope->get_definingDeclaration(); SgClassDeclaration* definingClassDeclaration = isSgClassDeclaration(declarationToFindInScope); ROSE_ASSERT(definingClassDeclaration != NULL); // SgType* definingDeclarationType = declarationToFindInScope->get_type(); SgType* definingDeclarationType = definingClassDeclaration->get_type(); ROSE_ASSERT(definingDeclarationType != NULL); // DQ (6/22/2011): This assertion fails for CompileTests/copyAST_tests/copytest2007_24.C // A simple rule that all declarations should follow (now that we have proper global type tables). // ROSE_ASSERT(definingDeclarationType == declarationType); if (definingDeclarationType != declarationType) { printf ("In fixupSymbolTables.C: Note that definingDeclarationType != declarationType \n"); } } SgNamedType* namedType = isSgNamedType(declarationType); ROSE_ASSERT(namedType != NULL); SgDeclarationStatement* declarationAssociatedToType = namedType->get_declaration(); ROSE_ASSERT(declarationAssociatedToType != NULL); #if 0 printf ("Found a symbol without a matching declaration in the current scope (declList): declarationToFindInScope = %p = %s \n",declarationToFindInScope,declarationToFindInScope->class_name().c_str()); printf ("Symbol number: %d (pair.first (SgName) = %s) pair.second (SgSymbol) class_name() = %s \n",idx,(*i).first.str(),(*i).second->class_name().c_str()); #endif SgScopeStatement* scopeOfDeclarationToFindInScope = declarationToFindInScope->get_scope(); SgScopeStatement* scopeOfDeclarationAssociatedWithType = declarationAssociatedToType->get_scope(); #if 0 printf ("declarationToFindInScope = %p declarationToFindInScope->get_scope() = %p = %s \n",declarationToFindInScope,declarationToFindInScope->get_scope(),declarationToFindInScope->get_scope()->class_name().c_str()); printf ("declarationAssociatedToType = %p declarationAssociatedToType->get_scope() = %p = %s \n",declarationAssociatedToType,declarationAssociatedToType->get_scope(),declarationAssociatedToType->get_scope()->class_name().c_str()); #endif if (scopeOfDeclarationToFindInScope != scopeOfDeclarationAssociatedWithType) { // DQ (6/12/2011): Houston, we have a problem! The trick is to fix it... // A symbol has been placed into a scope when we could not be certain which scope it should be placed. // We have a default of placing such symbols into the global scope, but it might be better to just have // a special scope where such symbols could be placed so that we could have them separate from the global // scope and then fix them up more clearly. // Note that test2011_80.C still fails but the AST is at least correct (I think). SgGlobal* scopeOfDeclarationToFindInScope_GlobalScope = isSgGlobal(scopeOfDeclarationToFindInScope); // SgGlobal* scopeOfDeclarationAssociatedWithType_GlobalScope = isSgGlobal(scopeOfDeclarationAssociatedWithType); if (scopeOfDeclarationToFindInScope_GlobalScope != NULL) { // In general which ever scope is the global scope is where the error is...??? // This is because when we don't know where to put a symbol (e.g. from a declaration of a pointer) we put it into global scope. // There is even an agrument that this is correct as a default for C/C++, but only if it must exist (see test2011_80.C). // Remove the symbol from the symbol table of the global scope. printf ("Remove the associated symbol in the current symbol table \n"); // DQ (6/22/2011): This assertion fails for CompileTests/copyAST_tests/copytest2007_24.C // ROSE_ASSERT (declarationToFindInScope->get_scope() == declarationAssociatedToType->get_scope()); if (declarationToFindInScope->get_scope() != declarationAssociatedToType->get_scope()) printf ("In fixupSymbolTables.C: Note that declarationToFindInScope->get_scope() != declarationAssociatedToType->get_scope() \n"); } else { listOfSymbolsToRemove.push_back(classSymbol); } } // ROSE_ASSERT (declarationToFindInScope->get_scope() == declarationAssociatedToType->get_scope()); break; } default: { // It night be there are are no other types of symbols to consider... // printf ("Ignoring non SgClassSymbols (fixupSymbolTables.C) symbol = %s \n",symbol->class_name().c_str()); // ROSE_ASSERT(false); } } // Increment iterator! i++; // Increment counter! idx++; } // DQ (6/18/2011): Now that we are through with the symbol table we can support removal of any // identified problematic symbol without worrying about STL iterator invalidation. for (size_t j = 0; j < listOfSymbolsToRemove.size(); j++) { // Remove these symbols. SgSymbol* removeSymbol = listOfSymbolsToRemove[j]; ROSE_ASSERT(removeSymbol != NULL); SgSymbolTable* associatedSymbolTable = isSgSymbolTable(removeSymbol->get_parent()); ROSE_ASSERT(associatedSymbolTable != NULL); ROSE_ASSERT(associatedSymbolTable == symbolTable); associatedSymbolTable->remove(removeSymbol); printf ("Redundant symbol removed...from symbol table \n"); // ROSE_ASSERT(false); } #if 0 // debugging symbolTable->print("In FixupAstSymbolTables::visit(): printing out the symbol tables"); #endif } }
bool FixupTemplateArguments::contains_private_type (SgTemplateArgument* templateArgument, SgScopeStatement* targetScope) { // Note that within EDG and ROSE the template arguments may be shared so that we can support testing for equivalence. // static std::list<SgTemplateArgument*> templateArgumentList; // templateArgumentList.push_back(templateArgument); static std::set<SgTemplateArgument*> templateArgumentSet; if (templateArgumentSet.find(templateArgument) == templateArgumentSet.end()) { templateArgumentSet.insert(templateArgument); } else { #if DEBUGGING_USING_RECURSIVE_DEPTH printf ("@@@@@@@@@@@@@@@@@ Already been or being processed: templateArgument = %p = %s templateArgumentSet.size() = %zu \n",templateArgument,templateArgument->unparseToString().c_str(),templateArgumentSet.size()); #endif #if 0 printf ("Leaving contains_private_type(SgTemplateArgument): templateArgument = %p returning FALSE \n",templateArgument); #endif // DQ (2/15/2017): Unclear if this is the correct return value, it might be that we want to record // the associated value from the first time the argument list was processed and use that value. // Then again, if the value had already been substituted into the template argument then no further // processing is required. return false; } #if DEBUGGING_USING_RECURSIVE_DEPTH printf ("--- added templateArgument = %p templateArgumentSet.size() = %zu \n",templateArgument,templateArgumentSet.size()); #endif #if DEBUGGING_USING_RECURSIVE_DEPTH // For debugging, keep track of the recursive depth. static size_t depth = 0; printf ("In contains_private_type(SgTemplateArgument*): depth = %zu \n",depth); ROSE_ASSERT(depth < 500); printf ("In contains_private_type(SgTemplateArgument*): global_depth = %zu \n",global_depth); if (global_depth >= 50) { // output the list of SgTemplateArgument in the list printf ("Error: too many elements in list: recursuion too deep \n"); size_t counter = 0; for (std::set<SgTemplateArgument*>::iterator i = templateArgumentSet.begin(); i != templateArgumentSet.end(); i++) { printf ("--- templateArgumentSet[counter] = %p = %s \n",*i,templateArgument->unparseToString().c_str()); counter++; } } ROSE_ASSERT(global_depth < 50); #endif // Note this is the recursive function. bool returnValue = false; #if DEBUG_PRIVATE_TYPE printf ("In contains_private_type(SgTemplateArgument*): templateArgument = %p = %s = %s \n",templateArgument,templateArgument->class_name().c_str(),templateArgument->unparseToString().c_str()); #endif switch (templateArgument->get_argumentType()) { case SgTemplateArgument::type_argument: { ROSE_ASSERT (templateArgument->get_type() != NULL); SgType* templateArgumentType = templateArgument->get_type(); #if DEBUG_PRIVATE_TYPE printf ("templateArgumentType = %p = %s \n",templateArgumentType,templateArgumentType->class_name().c_str()); if (isSgModifierType(templateArgumentType) != NULL) { SgModifierType* modifierType = isSgModifierType(templateArgumentType); SgType* base_type = modifierType->get_base_type(); printf ("--- base_type = %p = %s \n",base_type,base_type->class_name().c_str()); SgNamedType* namedType = isSgNamedType(base_type); if (namedType != NULL) { printf ("--- base_type: name = %s \n",namedType->get_name().str()); } } #endif #if DEBUGGING_USING_RECURSIVE_DEPTH depth++; global_depth++; #endif #if 0 printf ("In contains_private_type(SgTemplateArgument*): case SgTemplateArgument::type_argument: Calling contains_private_type(templateArgumentType) \n"); #endif // DQ (2/14/2017): We might want to generate a list of the private types used so // that we can check them against the scope of the declaration where they occur. // Note also that this does not address types that might appear in name qualification. returnValue = contains_private_type(templateArgumentType,targetScope); #if DEBUGGING_USING_RECURSIVE_DEPTH depth--; global_depth--; #endif #if 0 printf ("In contains_private_type(SgTemplateArgument*): case SgTemplateArgument::type_argument: DONE calling contains_private_type(templateArgumentType): returnValue = %s \n",returnValue ? "true" : "false"); #endif if (returnValue == true) { // Find an alternative typedef to use instead. // Note that this need not be a SgTypedefType (the lists are available in every SgType). SgTypedefType* typedefType = isSgTypedefType(templateArgumentType); if (typedefType == NULL && isSgModifierType(templateArgumentType) != NULL) { SgModifierType* modifierType = isSgModifierType(templateArgumentType); SgType* base_type = modifierType->get_base_type(); #if 0 printf ("Found SgModifierType: --- base_type = %p = %s \n",base_type,base_type->class_name().c_str()); SgNamedType* namedType = isSgNamedType(base_type); if (namedType != NULL) { printf ("--- base_type: name = %s \n",namedType->get_name().str()); } #endif #if 0 printf ("******* Reset the typedefType to what was found in the modifier type as a base type = %p = %s \n",base_type,base_type->class_name().c_str()); #endif typedefType = isSgTypedefType(base_type); } if (typedefType != NULL) { // Check if this is a type from a typedef that is in the same scope as the target declaration (variable declaration). SgTypedefDeclaration* typedefDeclaration = isSgTypedefDeclaration(typedefType->get_declaration()); ROSE_ASSERT(typedefDeclaration != NULL); // Test for the matching scope (an even better test would be to make sure that the targetScope is nested in the typedef scope). SgScopeStatement* typedefDeclarationScope = typedefDeclaration->get_scope(); ROSE_ASSERT(targetScope != NULL); ROSE_ASSERT(typedefDeclarationScope != NULL); #if 0 printf ("targetScope = %p = %s \n",targetScope,targetScope->class_name().c_str()); printf ("typedefDeclarationScope = %p = %s \n",typedefDeclarationScope,typedefDeclarationScope->class_name().c_str()); if (typedefDeclarationScope == targetScope) { printf ("In contains_private_type(SgTemplateArgument*): This is a typedef type from the same scope as the target declaration \n"); ROSE_ASSERT(false); } #endif // Consult the list of alreanative typedefs. SgTypedefSeq* typedef_table = typedefType->get_typedefs(); ROSE_ASSERT(typedef_table != NULL); #if DEBUG_PRIVATE_TYPE || 0 printf ("Looking at typedef typedefType = %p = %s = %s \n",typedefType,typedefType->class_name().c_str(),typedefType->unparseToString().c_str()); #endif SgTypePtrList & typedefList = typedef_table->get_typedefs(); bool foundNonPrivateTypeAlias = false; SgType* suitableTypeAlias = NULL; int counter = 0; SgTypePtrList::iterator i = typedefList.begin(); while (foundNonPrivateTypeAlias == false && i != typedefList.end()) { ROSE_ASSERT(*i != NULL); #if DEBUG_PRIVATE_TYPE || 0 printf ("Looking for suitable type alias (#%d): *i = %p = %s = %s \n",counter,*i,(*i)->class_name().c_str(),(*i)->unparseToString().c_str()); #endif #if DEBUGGING_USING_RECURSIVE_DEPTH global_depth++; #endif bool isPrivateType = contains_private_type(*i,targetScope); #if DEBUGGING_USING_RECURSIVE_DEPTH global_depth--; #endif if (isPrivateType == false) { suitableTypeAlias = *i; foundNonPrivateTypeAlias = true; } // foundNonPrivateTypeAlias = !isPrivateType; i++; counter++; } #if 0 printf ("foundNonPrivateTypeAlias = %s \n",foundNonPrivateTypeAlias ? "true" : "false"); #endif if (foundNonPrivateTypeAlias == true) { ROSE_ASSERT(suitableTypeAlias != NULL); #if DEBUG_PRIVATE_TYPE_TRANSFORMATION || 0 printf ("targetScope = %p = %s \n",targetScope,targetScope->class_name().c_str()); printf ("typedefDeclarationScope = %p = %s \n",typedefDeclarationScope,typedefDeclarationScope->class_name().c_str()); targetScope->get_file_info()->display("targetScope: debug"); typedefDeclarationScope->get_file_info()->display("typedefDeclarationScope: debug"); printf ("Found private type to be replaced: typedefType = %p = %s = %s \n",typedefType,typedefType->class_name().c_str(),typedefType->unparseToString().c_str()); printf ("Found suitable type alias: suitableTypeAlias = %p = %s = %s \n",suitableTypeAlias,suitableTypeAlias->class_name().c_str(),suitableTypeAlias->unparseToString().c_str()); #endif #if 0 printf ("SageInterface::whereAmI(targetScope): \n"); SageInterface::whereAmI(targetScope); printf ("SageInterface::whereAmI(typedefDeclaration): \n"); SageInterface::whereAmI(typedefDeclaration); #endif #if 0 printf ("Selecting alternative type to use for unparsing: \n"); printf ("--- were going to use: %s \n",templateArgument->unparseToString().c_str()); printf ("--- selecing instead : %s \n",suitableTypeAlias->unparseToString().c_str()); #endif // TV (10/05/2018): (ROSE-1431) Traverse the chain of all associated template arguments (coming from the same EDG template argument) SgTemplateArgument * templateArgument_it = templateArgument; while (templateArgument_it->get_previous_instance() != NULL) { templateArgument_it = templateArgument_it->get_previous_instance(); } ROSE_ASSERT(templateArgument_it != NULL && templateArgument_it->get_previous_instance() == NULL); do { #if 0 printf (" Update templateArgument = %p\n", templateArgument); #endif templateArgument_it->set_unparsable_type_alias(suitableTypeAlias); // DQ (1/9/2017): Also set the return result from get_type() so that the name qualification will be handled correctly. templateArgument_it->set_type(suitableTypeAlias); templateArgument_it = templateArgument_it->get_next_instance(); } while (templateArgument_it != NULL); ROSE_ASSERT(templateArgument_it == NULL); // #if DEBUG_PRIVATE_TYPE_TRANSFORMATION #if 0 string typedefType_typeName = generate_string_name (typedefType,NULL); printf ("typedefType_typeName size = %zu \n",typedefType_typeName.length()); printf ("typedefType_typeName = %s \n",typedefType_typeName.c_str()); string suitableTypeAlias_typeName = generate_string_name (suitableTypeAlias,NULL); printf ("suitableTypeAlias_typeName size = %zu \n",suitableTypeAlias_typeName.length()); printf ("suitableTypeAlias_typeName size = %s \n",suitableTypeAlias_typeName.c_str()); ROSE_ASSERT(suitableTypeAlias_typeName.length() < typedefType_typeName.length() * 100); ROSE_ASSERT(suitableTypeAlias_typeName.length() < 40000); #endif } } else { #if DEBUG_PRIVATE_TYPE printf ("Alternative types not searched for in nontypedef types (not implemented) \n"); #endif #if 0 printf ("####### Alternative types not searched: templateArgumentType = %p = %s \n",templateArgumentType,templateArgumentType->class_name().c_str()); if (isSgModifierType(templateArgumentType) != NULL) { SgModifierType* modifierType = isSgModifierType(templateArgumentType); SgType* base_type = modifierType->get_base_type(); printf ("--- base_type = %p = %s \n",base_type,base_type->class_name().c_str()); SgNamedType* namedType = isSgNamedType(base_type); if (namedType != NULL) { printf ("--- base_type: name = %s \n",namedType->get_name().str()); } } #endif } } break; } default: { #if DEBUG_PRIVATE_TYPE printf ("Ignoring non-type template arguments \n"); #endif } } #if DEBUG_PRIVATE_TYPE printf ("Leaving contains_private_type(SgTemplateArgument*): templateArgument = %p = %s = %s \n",templateArgument,templateArgument->class_name().c_str(),templateArgument->unparseToString().c_str()); #endif // templateArgumentList.pop_back(); templateArgumentSet.erase(templateArgument); #if DEBUGGING_USING_RECURSIVE_DEPTH printf ("--- pop templateArgument = %p templateArgumentSet.size() = %zu \n",templateArgument,templateArgumentSet.size()); #endif #if 0 printf ("Leaving contains_private_type(SgTemplateArgument): templateArgument = %p returnValue = %s \n",templateArgument,returnValue ? "true" : "false"); #endif return returnValue; }
bool ClangToSageTranslator::VisitRecordDecl(clang::RecordDecl * record_decl, SgNode ** node) { #if DEBUG_VISIT_DECL std::cerr << "ClangToSageTranslator::VisitRecordDecl" << std::endl; #endif // FIXME May have to check the symbol table first, because of out-of-order traversal of C++ classes (Could be done in CxxRecord class...) bool res = true; SgClassDeclaration * sg_class_decl = NULL; // Find previous declaration clang::RecordDecl * prev_record_decl = record_decl->getPreviousDeclaration(); SgClassSymbol * sg_prev_class_sym = isSgClassSymbol(GetSymbolFromSymbolTable(prev_record_decl)); SgClassDeclaration * sg_prev_class_decl = sg_prev_class_sym == NULL ? NULL : isSgClassDeclaration(sg_prev_class_sym->get_declaration()); SgClassDeclaration * sg_first_class_decl = sg_prev_class_decl == NULL ? NULL : isSgClassDeclaration(sg_prev_class_decl->get_firstNondefiningDeclaration()); SgClassDeclaration * sg_def_class_decl = sg_prev_class_decl == NULL ? NULL : isSgClassDeclaration(sg_prev_class_decl->get_definingDeclaration()); ROSE_ASSERT(sg_first_class_decl != NULL || sg_def_class_decl == NULL); bool had_prev_decl = sg_first_class_decl != NULL; // Name SgName name(record_decl->getNameAsString()); // Type of class SgClassDeclaration::class_types type_of_class; switch (record_decl->getTagKind()) { case clang::TTK_Struct: type_of_class = SgClassDeclaration::e_struct; break; case clang::TTK_Class: type_of_class = SgClassDeclaration::e_class; break; case clang::TTK_Union: type_of_class = SgClassDeclaration::e_union; break; default: std::cerr << "Runtime error: RecordDecl can only be a struct/class/union." << std::endl; res = false; } // Build declaration(s) sg_class_decl = new SgClassDeclaration(name, type_of_class, NULL, NULL); sg_class_decl->set_scope(SageBuilder::topScopeStack()); sg_class_decl->set_parent(SageBuilder::topScopeStack()); SgClassType * type = NULL; if (sg_first_class_decl != NULL) { type = sg_first_class_decl->get_type(); } else { type = SgClassType::createType(sg_class_decl); } ROSE_ASSERT(type != NULL); sg_class_decl->set_type(type); if (record_decl->isAnonymousStructOrUnion()) sg_class_decl->set_isUnNamed(true); if (!had_prev_decl) { sg_first_class_decl = sg_class_decl; sg_first_class_decl->set_firstNondefiningDeclaration(sg_first_class_decl); sg_first_class_decl->set_definingDeclaration(NULL); sg_first_class_decl->set_definition(NULL); sg_first_class_decl->setForward(); if (!record_decl->field_empty()) { sg_def_class_decl = new SgClassDeclaration(name, type_of_class, type, NULL); sg_def_class_decl->set_scope(SageBuilder::topScopeStack()); if (record_decl->isAnonymousStructOrUnion()) sg_def_class_decl->set_isUnNamed(true); sg_def_class_decl->set_parent(SageBuilder::topScopeStack()); sg_class_decl = sg_def_class_decl; // we return thew defining decl sg_def_class_decl->set_firstNondefiningDeclaration(sg_first_class_decl); sg_def_class_decl->set_definingDeclaration(sg_def_class_decl); sg_first_class_decl->set_definingDeclaration(sg_def_class_decl); setCompilerGeneratedFileInfo(sg_first_class_decl); } } else if (!record_decl->field_empty()) { if (sg_def_class_decl != NULL) { delete sg_class_decl; *node = sg_def_class_decl; return true; } sg_def_class_decl = sg_class_decl; sg_def_class_decl->set_firstNondefiningDeclaration(sg_first_class_decl); sg_def_class_decl->set_definingDeclaration(sg_def_class_decl); sg_first_class_decl->set_definingDeclaration(sg_def_class_decl); } else // second (or more) non-defining declaration return false; // FIXME ROSE need only one non-defining declaration (SageBuilder don't let me build another one....) // Update symbol table if (!had_prev_decl) { SgScopeStatement * scope = SageBuilder::topScopeStack(); SgClassSymbol * class_symbol = new SgClassSymbol(sg_first_class_decl); scope->insert_symbol(name, class_symbol); } // Build ClassDefinition if (!record_decl->field_empty()) { SgClassDefinition * sg_class_def = isSgClassDefinition(sg_def_class_decl->get_definition()); if (sg_class_def == NULL) { sg_class_def = SageBuilder::buildClassDefinition_nfi(sg_def_class_decl); } sg_def_class_decl->set_definition(sg_class_def); ROSE_ASSERT(sg_class_def->get_symbol_table() != NULL); applySourceRange(sg_class_def, record_decl->getSourceRange()); SageBuilder::pushScopeStack(sg_class_def); clang::RecordDecl::field_iterator it; for (it = record_decl->field_begin(); it != record_decl->field_end(); it++) { SgNode * tmp_field = Traverse(*it); SgDeclarationStatement * field_decl = isSgDeclarationStatement(tmp_field); ROSE_ASSERT(field_decl != NULL); sg_class_def->append_member(field_decl); field_decl->set_parent(sg_class_def); } SageBuilder::popScopeStack(); } ROSE_ASSERT(sg_class_decl->get_definingDeclaration() == NULL || isSgClassDeclaration(sg_class_decl->get_definingDeclaration())->get_definition() != NULL); ROSE_ASSERT(sg_first_class_decl->get_definition() == NULL); ROSE_ASSERT(sg_def_class_decl == NULL || sg_def_class_decl->get_definition() != NULL); *node = sg_class_decl; return VisitTagDecl(record_decl, node) && res; }
void FixupTemplateArguments::visit ( SgNode* node ) { ROSE_ASSERT(node != NULL); SgVariableDeclaration* variableDeclaration = isSgVariableDeclaration(node); if (variableDeclaration != NULL) { // Check the type of the variable declaration, and any template arguments if it is a template type with template arguments. // SgType* type = variableDeclaration->get_type(); // ROSE_ASSERT(type != NULL); SgInitializedName* initializedName = SageInterface::getFirstInitializedName(variableDeclaration); ROSE_ASSERT(initializedName != NULL); SgType* type = initializedName->get_type(); ROSE_ASSERT(type != NULL); #if 0 printf ("\n**************************************************************************** \n"); printf ("FixupTemplateArguments::visit(): variableDeclaration = %p = %s initializedName = %s \n",variableDeclaration,variableDeclaration->class_name().c_str(),initializedName->get_name().str()); printf (" --- type = %p = %s \n",type,type->class_name().c_str()); string filename = initializedName->get_file_info()->get_filename(); int linenumber = initializedName->get_file_info()->get_line(); printf (" --- filename = %s line = %d \n",filename.c_str(),linenumber); #endif SgScopeStatement* targetScope = variableDeclaration->get_scope(); ROSE_ASSERT(targetScope != NULL); #if 0 printf ("In FixupTemplateArguments::visit(): targetScope for variableDeclaration = %p = %s \n",targetScope,targetScope->class_name().c_str()); #endif // DQ (2/16/2017): Don't process code in template instantiations. SgTemplateInstantiationDefn* templateInstantiationDefn = isSgTemplateInstantiationDefn(targetScope); SgFunctionDeclaration* functionDeclaration = TransformationSupport::getFunctionDeclaration(targetScope); SgTemplateInstantiationFunctionDecl* templateInstantiationFunctionDec = isSgTemplateInstantiationFunctionDecl(functionDeclaration); SgTemplateInstantiationMemberFunctionDecl* templateInstantiationMemberFunctionDec = isSgTemplateInstantiationMemberFunctionDecl(functionDeclaration); // if (templateInstantiationDefn == NULL) if (templateInstantiationDefn == NULL && templateInstantiationFunctionDec == NULL && templateInstantiationMemberFunctionDec == NULL) { #if 1 // DQ (2/15/2017): When this is run, we cause transformations that cause ROSE to have an infinte loop. // Since this is a second (redundant) invocaion, we likely should just not run this. But it is not // clear if this truely fixes the problem that I am seeing. bool result = contains_private_type(type,targetScope); // DQ (3/25/2017): Added a trivial use to eliminate Clang warning about the return value not being used. // But it might be that we should not run the function, however this is a complex subject from last month // that I don't wish to revisit at the moment while being focused om eliminating warnings from Clang. ROSE_ASSERT(result == true || result == false); #endif #if 0 if (result == true) { printf ("******** contains private type: variableDeclaration = %p = %s initializedName = %s \n",variableDeclaration,variableDeclaration->class_name().c_str(),initializedName->get_name().str()); } #endif } #if 0 printf ("DONE: FixupTemplateArguments::visit(): variableDeclaration = %p = %s initializedName = %s \n",variableDeclaration,variableDeclaration->class_name().c_str(),initializedName->get_name().str()); #endif #if 0 printf ("Exiting as a test! \n"); ROSE_ASSERT(false); #endif } }