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++; } }
bool ReplacementMapTraversal::verifyODR( SgNode* node, SgNode* duplicateNodeFromOriginalAST ) { bool passesODR = false; // printf ("Verify that node = %p is equivalent to duplicateNodeFromOriginalAST = %p = %s \n",node,duplicateNodeFromOriginalAST,duplicateNodeFromOriginalAST->class_name().c_str()); // Verify that these strings match ROSE_ASSERT (duplicateNodeFromOriginalAST->variantT() == node->variantT()); ROSE_ASSERT (duplicateNodeFromOriginalAST->class_name() == node->class_name()); // ROSE_ASSERT (generateUniqueName(duplicateNodeFromOriginalAST) == generateUniqueName(node)); string nodeString; string duplicateNodeFromOriginalASTstring; #if 0 // DQ (2/3/2007): This is a test to debug the ODR checking. // I think that the unparser has some state specific to the output of access protections. // if the unparsing changes the state then the access permission (public, protected, private) // is output and this cause a falue trigger to the ODR string match. This is a temp fix to // absorbe any change of state, but we need a mechanism to clear the state in the unparser. string absorbeUnparserStateChange_A = node->unparseToString(); string absorbeUnparserStateChange_B = duplicateNodeFromOriginalAST->unparseToString(); #endif #if 0 // DQ (2/3/2007): Make sure that there are close to being related. It appears that if these are if (node->get_parent()->variantT() == duplicateNodeFromOriginalAST->get_parent()->variantT()) { nodeString = node->unparseToString(); duplicateNodeFromOriginalASTstring = duplicateNodeFromOriginalAST->unparseToString(); } #endif bool skip_ODR_test = false; bool nodeIsCompilerGenerated = (node->get_file_info() != NULL) ? node->get_file_info()->isCompilerGenerated() : false; bool duplicateNodeFromOriginalASTIsCompilerGenerated = (duplicateNodeFromOriginalAST->get_file_info() != NULL) ? duplicateNodeFromOriginalAST->get_file_info()->isCompilerGenerated() : false; bool nodeIsFrontendSpecific = (node->get_file_info() != NULL) ? node->get_file_info()->isFrontendSpecific() : false; bool duplicateNodeFromOriginalASTIsFrontendSpecific = (duplicateNodeFromOriginalAST->get_file_info() != NULL) ? duplicateNodeFromOriginalAST->get_file_info()->isFrontendSpecific() : false; // If this is a template declaration for a function then it might have been a part of another template declaration // for the template class and thus might not exist explicitly in the AST (and thus not have enough information from // which to generate a meaningful mangled name). Skip ODR testing of these cases. bool isTemplateMemberFunctionInTemplatedClass = false; SgTemplateDeclaration* templateDeclaration = isSgTemplateDeclaration(node); if (templateDeclaration != NULL) { SgTemplateDeclaration* dup_templateDeclaration = isSgTemplateDeclaration(duplicateNodeFromOriginalAST); ROSE_ASSERT(dup_templateDeclaration != NULL); if ( templateDeclaration->get_string().is_null() && dup_templateDeclaration->get_string().is_null() ) { isTemplateMemberFunctionInTemplatedClass = true; } } if (isTemplateMemberFunctionInTemplatedClass == true) { printf ("ODR not tested isTemplateMemberFunctionInTemplatedClass == true. \n"); skip_ODR_test = true; } if (nodeIsFrontendSpecific == true || duplicateNodeFromOriginalASTIsFrontendSpecific == true || nodeIsCompilerGenerated == true || duplicateNodeFromOriginalASTIsCompilerGenerated == true) { // printf ("ODR not tested for frontend specific compiler generated code. \n"); skip_ODR_test = true; } // DQ (1/20/2007): The unparse will not generate a string if the code is frontend specific or compiler generated (I forget which). // if (nodeIsFrontendSpecific == true || duplicateNodeFromOriginalASTIsFrontendSpecific == true) if (skip_ODR_test == true) { // printf ("ODR not tested for frontend specific compiler generated code. \n"); passesODR = true; } else { SgUnparse_Info info_a; SgUnparse_Info info_b; // DQ (2/6/2007): Force qualified names to be used uniformally (note that info.set_requiresGlobalNameQualification() // causes an error) info.set_requiresGlobalNameQualification(); info_a.set_forceQualifiedNames(); info_b.set_forceQualifiedNames(); nodeString = node->unparseToString(&info_a); // DQ (2/6/2007): The SgUnparse_Info object carries state which controls the use of access qualification and the // first call to unparseToString might have set the access (e.g. to "public") and the second call would drop the // access qualification. We unset the access qualification state in the SgUnparse_Info object so that both will // be unparsed the same (we could have alternatively used two separate SgUnparse_Info objects. // info.set_isUnsetAccess(); duplicateNodeFromOriginalASTstring = duplicateNodeFromOriginalAST->unparseToString(&info_b); passesODR = (nodeString == duplicateNodeFromOriginalASTstring); } // Don't count the cases where the unparse fails to to invalid parent in redundant SgClassDeclaration (fix these later) if (passesODR == false && nodeString.empty() == false && duplicateNodeFromOriginalASTstring.empty() == false) { #if 1 if (SgProject::get_verbose() > 0) printf ("##### In ReplacementMapTraversal::verifyODR() is false: node = %p = %s duplicateNodeFromOriginalAST = %p = %s \n", node,node->class_name().c_str(),duplicateNodeFromOriginalAST,duplicateNodeFromOriginalAST->class_name().c_str()); // printf ("##### passesODR = %s \n",passesODR ? "true" : "false"); // printf ("duplicateNodeFromOriginalASTstring = \n---> %s\n",duplicateNodeFromOriginalASTstring.c_str()); // printf ("nodeString = \n---> %s\n",nodeString.c_str()); if (node->get_file_info() != NULL && duplicateNodeFromOriginalAST->get_file_info() != NULL) { if (SgProject::get_verbose() > 0) { SgNode* parent_node = node->get_parent(); // DQ (9/13/2011): Reported as possible NULL value in static analysis of ROSE code. ROSE_ASSERT(parent_node != NULL); printf ("parent_node = %p = %s = %s \n",parent_node,parent_node->class_name().c_str(),SageInterface::get_name(parent_node).c_str()); SgNode* parent_dup = duplicateNodeFromOriginalAST->get_parent(); // DQ (9/13/2011): Reported as possible NULL value in static analysis of ROSE code. ROSE_ASSERT(parent_dup != NULL); printf ("parent_dup = %p = %s = %s \n",parent_dup,parent_dup->class_name().c_str(),SageInterface::get_name(parent_dup).c_str()); printf ("\nPosition of error: \n"); node->get_file_info()->display("In ReplacementMapTraversal::verifyODR(node) is false: debug"); duplicateNodeFromOriginalAST->get_file_info()->display("In ReplacementMapTraversal::verifyODR(duplicateNodeFromOriginalAST) is false: debug"); printf ("\nPosition of error: \n"); printf ("\nPosition of error (parent IR node): \n"); parent_node->get_file_info()->display("In ReplacementMapTraversal::verifyODR(parent_node) is false: debug"); parent_dup->get_file_info()->display("In ReplacementMapTraversal::verifyODR(parent_dup) is false: debug"); printf ("\nPosition of error (parent IR node): \n"); } } else { SgClassType* classType = isSgClassType(node); SgClassType* duplicateNodeFromOriginalAST_classType = isSgClassType(duplicateNodeFromOriginalAST); if (classType != NULL) { if (SgProject::get_verbose() > 0) { SgClassDeclaration* classDeclaration = isSgClassDeclaration(classType->get_declaration()); ROSE_ASSERT(classDeclaration != NULL); classDeclaration->get_file_info()->display("In ReplacementMapTraversal::verifyODR(node) is false (classType)"); printf ("classDeclaration = %p definingDeclaration = %p nondefiningDeclaration = %p \n", classDeclaration, classDeclaration->get_definingDeclaration(), classDeclaration->get_firstNondefiningDeclaration()); ROSE_ASSERT(duplicateNodeFromOriginalAST_classType != NULL); SgClassDeclaration* duplicateNodeFromOriginalAST_classDeclaration = isSgClassDeclaration(duplicateNodeFromOriginalAST_classType->get_declaration()); ROSE_ASSERT(duplicateNodeFromOriginalAST_classDeclaration != NULL); duplicateNodeFromOriginalAST_classDeclaration->get_file_info()->display("In ReplacementMapTraversal::verifyODR(node) is false (duplicateNodeFromOriginalAST_classType)"); printf ("duplicateNodeFromOriginalAST_classDeclaration = %p definingDeclaration = %p nondefiningDeclaration = %p \n", duplicateNodeFromOriginalAST_classDeclaration, duplicateNodeFromOriginalAST_classDeclaration->get_definingDeclaration(), duplicateNodeFromOriginalAST_classDeclaration->get_firstNondefiningDeclaration()); } } } #endif odrViolations.push_back(pair<SgNode*,SgNode*>(node,duplicateNodeFromOriginalAST)); } #if 0 printf ("duplicateNodeFromOriginalASTstring = %p = %s \n---> %s\n", duplicateNodeFromOriginalAST,duplicateNodeFromOriginalAST->class_name().c_str(),duplicateNodeFromOriginalASTstring.c_str()); printf ("nodeString = %p = %s \n---> %s\n", node,node->class_name().c_str(),nodeString.c_str()); #endif #if 0 SgClassType* original = isSgClassType(duplicateNodeFromOriginalAST); SgClassType* target = isSgClassType(node); if (original != NULL && target != NULL) { printf ("original declaration = %p \n",original->get_declaration()); printf ("target declaration = %p \n",target->get_declaration()); } #endif #if 1 if (passesODR == false) { if (SgProject::get_verbose() > 0) { string node_generatedName = SageInterface::generateUniqueName(node,false); string originalNode_generatedName = SageInterface::generateUniqueName(duplicateNodeFromOriginalAST,false); printf ("ODR Violation Source code: nodeString = \n%s\n \n",nodeString.c_str()); printf ("ODR Violation Source code: duplicateNodeFromOriginalASTstring = \n%s\n \n",duplicateNodeFromOriginalASTstring.c_str()); printf ("nodeString = %s \n",nodeString.c_str()); printf ("node_generatedName = %s \n",node_generatedName.c_str()); printf ("originalNode_generatedName = %s \n",originalNode_generatedName.c_str()); printf ("node = %p = %s = %s \n",node,node->class_name().c_str(),SageInterface::get_name(node).c_str()); printf ("duplicateNodeFromOriginalAST = %p = %s = %s \n",duplicateNodeFromOriginalAST,duplicateNodeFromOriginalAST->class_name().c_str(),SageInterface::get_name(duplicateNodeFromOriginalAST).c_str()); printf ("node (unique string) = %s \n",generateUniqueName(node,true).c_str()); printf ("duplicateNodeFromOriginalAST (unique string) = %s \n",generateUniqueName(duplicateNodeFromOriginalAST,true).c_str()); SgDeclarationStatement* declarationStatement = isSgDeclarationStatement(node); if (declarationStatement != NULL) { printf ("declarationStatement->get_definingDeclaration() = %p \n",declarationStatement->get_definingDeclaration()); printf ("declarationStatement->get_firstNondefiningDeclaration() = %p \n",declarationStatement->get_firstNondefiningDeclaration()); } SgDeclarationStatement* declarationStatement2 = isSgDeclarationStatement(duplicateNodeFromOriginalAST); if (declarationStatement2 != NULL) { printf ("declarationStatement2->get_definingDeclaration() = %p \n",declarationStatement2->get_definingDeclaration()); printf ("declarationStatement2->get_firstNondefiningDeclaration() = %p \n",declarationStatement2->get_firstNondefiningDeclaration()); } printf ("Source code positions of ORD violation: \n"); node->get_file_info()->display("In ReplacementMapTraversal::verifyODR(node) is false: debug"); duplicateNodeFromOriginalAST->get_file_info()->display("In ReplacementMapTraversal::verifyODR(duplicateNodeFromOriginalAST) is false: debug"); } } #endif // ROSE_ASSERT(nodeString == duplicateNodeFromOriginalASTstring); ROSE_ASSERT(passesODR == true); return passesODR; }
ATerm convertNodeToAterm(SgNode* n) { if (n == NULL) { #if 0 printf ("convertNodeToAterm(): n = %p = %s \n",n,"NULL"); #endif return ATmake("NULL"); } ROSE_ASSERT(n != NULL); #if 0 printf ("convertNodeToAterm(): n = %p = %s \n",n,n->class_name().c_str()); #endif ATerm term; switch (n->variantT()) { // case V_SgFile: case V_SgSourceFile: // Special case needed to include file name // term = ATmake("File(<str>, <term>)", isSgFile(n)->getFileName(), convertNodeToAterm(isSgFile(n)->get_root())); term = ATmake("File(<str>, <term>)", isSgSourceFile(n)->getFileName().c_str(), convertNodeToAterm(isSgSourceFile(n)->get_globalScope())); break; case V_SgPlusPlusOp: case V_SgMinusMinusOp: // Special cases needed to include prefix/postfix status term = ATmake("<appl(<appl>, <term>)>", getShortVariantName((VariantT)(n->variantT())).c_str(), (isSgUnaryOp(n)->get_mode() == SgUnaryOp::prefix ? "Prefix" : isSgUnaryOp(n)->get_mode() == SgUnaryOp::postfix ? "Postfix" : "Unknown"), convertNodeToAterm(isSgUnaryOp(n)->get_operand())); break; case V_SgExpressionRoot: // Special case to remove this node term = convertNodeToAterm(isSgExpressionRoot(n)->get_operand()); break; case V_SgCastExp: // Special case needed to include type term = ATmake("Cast(<term>, <term>)>", convertNodeToAterm(isSgUnaryOp(n)->get_operand()), convertNodeToAterm(isSgCastExp(n)->get_type())); break; case V_SgVarRefExp: // Special case needed to include id term = ATmake("Var(<str>)", uniqueId(isSgVarRefExp(n)->get_symbol()->get_declaration()).c_str()); break; case V_SgFunctionRefExp: // Special case needed to include id term = ATmake( "Func(<str>)", uniqueId(isSgFunctionRefExp(n)->get_symbol()->get_declaration()).c_str()); break; case V_SgIntVal: // Special case needed to include value term = ATmake("IntC(<int>)", isSgIntVal(n)->get_value()); break; case V_SgUnsignedIntVal: term = ATmake("UnsignedIntC(<int>)", isSgUnsignedIntVal(n)->get_value()); break; case V_SgUnsignedLongVal: { ostringstream s; s << isSgUnsignedLongVal(n)->get_value(); term = ATmake("UnsignedLongC(<str>)", s.str().c_str()); } break; case V_SgUnsignedLongLongIntVal: { ostringstream s; s << isSgUnsignedLongLongIntVal(n)->get_value(); term = ATmake("UnsignedLongLongC(<str>)", s.str().c_str()); } break; case V_SgDoubleVal: term = ATmake("DoubleC(<real>)", isSgDoubleVal(n)->get_value()); break; case V_SgInitializedName: { // Works around double initname problem SgInitializer* initializer = isSgInitializedName(n)->get_initializer(); const SgName& name = isSgInitializedName(n)->get_name(); SgType* type = isSgInitializedName(n)->get_type(); ROSE_ASSERT(type != NULL); #if 0 printf ("convertNodeToAterm(): case V_SgInitializedName: name = %s initializer = %p type = %p = %s \n",name.str(),initializer,type,type->class_name().c_str()); #endif // Works around fact that ... is not really an initname and shouldn't be a type either if (isSgTypeEllipse(type)) { term = ATmake("Ellipses"); } else { std::string uniqueIdString = uniqueId(n); #if 0 printf ("uniqueIdString = %s \n",uniqueIdString.c_str()); printf ("Calling generate ATerm for SgInitializedName->get_name() name = %s \n",name.str()); ATerm name_aterm = ATmake("Name(<str>)",name.str()); // ATerm name_aterm = ATmake(name.str()); printf ("Calling convertNodeToAterm(type) \n"); ATerm type_aterm = convertNodeToAterm(type); printf ("Calling convertNodeToAterm(initializer) \n"); #endif ATerm initializer_aterm = convertNodeToAterm(initializer); #if 0 printf ("Calling ATmake() \n"); #endif #if 1 term = ATmake("InitName(<str>, <term>, <term>) {[id, <str>]}", (name.str() ? name.str() : ""), convertNodeToAterm(type), convertNodeToAterm(initializer), uniqueId(n).c_str()); // uniqueIdString.c_str()); #else term = ATmake("InitName(<term>,<term>)", //(name.str() ? name.str() : ""), // name_aterm, type_aterm, initializer_aterm // uniqueId(n).c_str()); // uniqueIdString.c_str()); ); #endif #if 0 printf ("Calling ATsetAnnotation() \n"); #endif term = ATsetAnnotation(term, ATmake("id"), ATmake("<str>", uniqueId(n).c_str())); #if 0 printf ("DONE: Calling ATsetAnnotation() \n"); #endif } break; } case V_SgFunctionDeclaration: { // Special case needed to include name SgFunctionDeclaration* fd = isSgFunctionDeclaration(n); term = ATmake("Function(<str>, <term>, <term>, <term>)", fd->get_name().str(), convertNodeToAterm(fd->get_orig_return_type()), convertSgNodeRangeToAterm(fd->get_args().begin(), fd->get_args().end()), convertNodeToAterm(fd->get_definition())); term = ATsetAnnotation(term, ATmake("id"), ATmake("<str>", uniqueId(n).c_str())); } break; case V_SgClassDeclaration: { // Special case needed to distinguish forward/full definitions and to // include class name SgClassDeclaration* decl = isSgClassDeclaration(n); assert (decl); SgName sname = decl->get_name(); const char* name = sname.str(); // Suggestion: have a field named local_definition in each class // declaration that is 0 whenever the current declaration doesn't // have a definition attached, even if there is another declaration // which does have a definition attached. SgClassDefinition* defn = decl->get_definition(); // cout << "defn = 0x" << hex << defn << endl << dec; if (decl->isForward()) defn = 0; if (defn) term = ATmake("Class(<str>, <term>)", (name ? name : ""), // Will be simpler when SgName // becomes string convertNodeToAterm(defn)); else term = ATmake("ClassFwd(<str>)", (name ? name : "")); term = ATsetAnnotation(term, ATmake("id"), ATmake("<str>", uniqueId(n).c_str())); } break; case V_SgEnumDeclaration: { // Special case to include enum name and enumerator names which are not // traversal children SgName sname = isSgEnumDeclaration(n)->get_name(); const char* name = sname.str(); const SgInitializedNamePtrList& enumerators = isSgEnumDeclaration(n)->get_enumerators(); term = ATmake("Enum(<str>, <term>)", (name ? name : "{anonymous}"), convertSgNodeRangeToAterm(enumerators.begin(), enumerators.end())); term = ATsetAnnotation(term, ATmake("id"), ATmake("<str>", uniqueId(n).c_str())); } break; case V_SgPointerType: { // Special case because types can't be traversed yet SgType* type = isSgPointerType(n)->get_base_type(); ATerm t = convertNodeToAterm(type); term = ATmake("Pointer(<term>)", t); } break; case V_SgReferenceType: { // Special case because types can't be traversed yet SgType* type = isSgReferenceType(n)->get_base_type(); ATerm t = convertNodeToAterm(type); term = ATmake("Reference(<term>)", t); } break; case V_SgModifierType: { // Special case for type traversal and to prettify modifier names SgType* type = isSgModifierType(n)->get_base_type(); SgTypeModifier& modifier = isSgModifierType(n)->get_typeModifier(); SgConstVolatileModifier& cvmod = modifier.get_constVolatileModifier(); term = convertNodeToAterm(type); if (cvmod.isConst()) term = ATmake("Const(<term>)", term); if (cvmod.isVolatile()) term = ATmake("Volatile(<term>)", term); } break; case V_SgArrayType: { // Special case because types can't be traversed yet, and to get length SgType* type = isSgArrayType(n)->get_base_type(); ATerm t = convertNodeToAterm(type); term = ATmake("Array(<term>, <term>)", t, (isSgArrayType(n)->get_index() ? convertNodeToAterm((n->get_traversalSuccessorContainer())[4]) : ATmake("<str>", "NULL"))); assert (term); } break; case V_SgFunctionType: { // Special case to allow argument list to be traversed SgFunctionType* ft = isSgFunctionType(n); ATerm ret = convertNodeToAterm(ft->get_return_type()); ATerm args_list = convertSgNodeRangeToAterm(ft->get_arguments().begin(), ft->get_arguments().end()); term = ATmake("FunctionType(<term>, <term>)", ret, args_list); } break; case V_SgEnumType: case V_SgClassType: case V_SgTypedefType: { // Special cases to optionally put in type definition instead of // reference SgNamedType* nt = isSgNamedType(n); assert (nt); SgName sname = nt->get_name(); // char* name = sname.str(); SgDeclarationStatement* decl = nt->get_declaration(); assert (decl); SgClassDefinition* defn = isSgClassDeclaration(decl) ? isSgClassDeclaration(decl)->get_definition() : 0; term = ATmake("Type(<term>)", (nt->get_autonomous_declaration() || !defn ? ATmake("id(<str>)", uniqueId(decl).c_str()) : convertNodeToAterm(nt->get_declaration()))); } break; case V_SgLabelStatement: { // Special case to put in label id const char* name = isSgLabelStatement(n)->get_name().str(); term = ATmake("Label(<str>)", (name ? name : "")); term = ATsetAnnotation(term, ATmake("id"), ATmake("<str>", uniqueId(n).c_str())); } break; case V_SgGotoStatement: { // Special case to put in label id term = ATmake("Goto(<str>)", uniqueId(isSgGotoStatement(n)->get_label()).c_str()); } break; case V_SgTypedefDeclaration: { // Special case to put in typedef name const SgName& name = isSgTypedefDeclaration(n)->get_name(); SgType* type = isSgTypedefDeclaration(n)->get_base_type(); term = ATmake("Typedef(<str>, <term>)", (name.str() ? name.str() : ""), convertNodeToAterm(type)); term = ATsetAnnotation(term, ATmake("id"), ATmake("<str>", uniqueId(n).c_str())); } break; case V_SgTemplateDeclaration: { // Traversal doesn't work for these SgTemplateDeclaration* td = isSgTemplateDeclaration(n); ROSE_ASSERT (td); // SgTemplateParameterPtrListPtr paramsPtr = td->get_templateParameters(); // SgTemplateParameterPtrList & paramsPtr = td->get_templateParameters(); // SgTemplateParameterPtrList params = paramsPtr ? *paramsPtr : SgTemplateParameterPtrList(); SgTemplateParameterPtrList & params = td->get_templateParameters(); string templateKindString; switch (td->get_template_kind()) { case SgTemplateDeclaration::e_template_none: templateKindString = "None"; break; case SgTemplateDeclaration::e_template_class: templateKindString = "Class"; break; case SgTemplateDeclaration::e_template_m_class: templateKindString = "MemberClass"; break; case SgTemplateDeclaration::e_template_function: templateKindString = "Function"; break; case SgTemplateDeclaration::e_template_m_function: templateKindString = "MemberFunction"; break; case SgTemplateDeclaration::e_template_m_data: templateKindString = "MemberData"; break; default: templateKindString = "Unknown"; break; } term = ATmake("TemplateDeclaration(<appl>, <str>, <term>, <str>)", templateKindString.c_str(), td->get_name().str(), convertSgNodeRangeToAterm(params.begin(), params.end()), td->get_string().str()); } break; case V_SgTemplateInstantiationDecl: { // Traversal doesn't work for these SgTemplateInstantiationDecl* td = isSgTemplateInstantiationDecl(n); ROSE_ASSERT (td); // SgTemplateArgumentPtrListPtr argsPtr = td->get_templateArguments(); // SgTemplateArgumentPtrList args = argsPtr ? *argsPtr : SgTemplateArgumentPtrList(); SgTemplateArgumentPtrList & args = td->get_templateArguments(); term = ATmake("TemplateInstantiationDecl(<str>, <term>)", td->get_templateDeclaration()->get_name().str(), convertSgNodeRangeToAterm(args.begin(), args.end())); } break; case V_SgTemplateParameter: { // Traversal doesn't work for these SgTemplateParameter* tp = isSgTemplateParameter(n); ROSE_ASSERT (tp); switch (tp->get_parameterType()) { case SgTemplateParameter::parameter_undefined: { term = ATmake("Undefined"); } break; case SgTemplateParameter::type_parameter: { term = ATmake("Type(<term>)", convertNodeToAterm(tp->get_defaultTypeParameter())); } break; case SgTemplateParameter::nontype_parameter: { term = ATmake("Nontype(<term>, <term>)", convertNodeToAterm(tp->get_type()), convertNodeToAterm(tp->get_defaultExpressionParameter())); } break; case SgTemplateParameter::template_parameter: { term = ATmake("Template"); } break; default: term = ATmake("Unknown"); break; } } break; case V_SgTemplateArgument: { // Traversal doesn't work for these SgTemplateArgument* ta = isSgTemplateArgument(n); ROSE_ASSERT (ta); switch (ta->get_argumentType()) { case SgTemplateArgument::argument_undefined: term = ATmake("Undefined"); break; case SgTemplateArgument::type_argument: term = ATmake("Type(<term>)", convertNodeToAterm(ta->get_type())); break; case SgTemplateArgument::nontype_argument: term = ATmake("Nontype(<term>)", convertNodeToAterm(ta->get_expression())); break; // case SgTemplateArgument::template_argument: // term = ATmake("Template"); // break; default: term = ATmake("Unknown"); break; } } break; default: { bool isContainer = (AstTests::numSuccContainers(n) == 1) || (!isSgType(n) && (n->get_traversalSuccessorContainer().size() == 0)); term = ATmake((isContainer ? "<appl(<term>)>" : "<appl(<list>)>"), getShortVariantName((VariantT)(n->variantT())).c_str(), (isSgType(n) ? ATmake("[]") : getTraversalChildrenAsAterm(n))); // Special case for types is because of traversal problems } break; } #if 0 printf ("Base of switch statement in convertNodeToAterm(): n = %p = %s \n",n,n->class_name().c_str()); #endif assert (term); term = ATsetAnnotation(term, ATmake("ptr"), pointerAsAterm(n)); #if 1 if (n->get_file_info() != NULL) { term = ATsetAnnotation(term, ATmake("location"),convertFileInfoToAterm(n->get_file_info())); } if (isSgExpression(n)) term = ATsetAnnotation(term, ATmake("type"), convertNodeToAterm(isSgExpression(n)->get_type())); #endif #if 0 printf ("Leaving convertNodeToAterm(): n = %p = %s \n",n,n->class_name().c_str()); #endif #if 0 printf ("--- n->class_name() = %s ATwriteToString(term) = %s \n",n->class_name().c_str(),ATwriteToString(term)); #endif // cout << n->sage_class_name() << " -> " << ATwriteToString(term) << endl; return term; }