bool ClangToSageTranslator::VisitTranslationUnitDecl(clang::TranslationUnitDecl * translation_unit_decl, SgNode ** node) { #if DEBUG_VISIT_DECL std::cerr << "ClangToSageTranslator::VisitTranslationUnitDecl" << std::endl; #endif if (*node != NULL) { std::cerr << "Runtime error: The TranslationUnitDecl is already associated to a SAGE node." << std::endl; return false; } // Create the SAGE node: SgGlobal if (p_global_scope != NULL) { std::cerr << "Runtime error: Global Scope have already been set !" << std::endl; return false; } *node = p_global_scope = new SgGlobal(); p_decl_translation_map.insert(std::pair<clang::Decl *, SgNode *>(translation_unit_decl, p_global_scope)); // Traverse the children SageBuilder::pushScopeStack(*node); clang::DeclContext * decl_context = (clang::DeclContext *)translation_unit_decl; // useless but more clear bool res = true; clang::DeclContext::decl_iterator it; for (it = decl_context->decls_begin(); it != decl_context->decls_end(); it++) { if (*it == NULL) continue; SgNode * child = Traverse(*it); SgDeclarationStatement * decl_stmt = isSgDeclarationStatement(child); if (decl_stmt == NULL && child != NULL) { std::cerr << "Runtime error: the node produce for a clang::Decl is not a SgDeclarationStatement !" << std::endl; std::cerr << " class = " << child->class_name() << std::endl; res = false; } else if (child != NULL) { // FIXME This is a hack to avoid autonomous decl of unnamed type to being added to the global scope.... SgClassDeclaration * class_decl = isSgClassDeclaration(child); if (class_decl != NULL && (class_decl->get_name() == "" || class_decl->get_isUnNamed())) continue; SgEnumDeclaration * enum_decl = isSgEnumDeclaration(child); if (enum_decl != NULL && (enum_decl->get_name() == "" || enum_decl->get_isUnNamed())) continue; p_global_scope->append_declaration(decl_stmt); } } SageBuilder::popScopeStack(); // Traverse the class hierarchy return VisitDecl(translation_unit_decl, node) && res; }
bool ClangToSageTranslator::VisitEnumDecl(clang::EnumDecl * enum_decl, SgNode ** node) { #if DEBUG_VISIT_DECL std::cerr << "ClangToSageTranslator::VisitEnumDecl" << std::endl; #endif bool res = true; SgName name(enum_decl->getNameAsString()); clang::EnumDecl * prev_enum_decl = enum_decl->getPreviousDeclaration(); SgEnumSymbol * sg_prev_enum_sym = isSgEnumSymbol(GetSymbolFromSymbolTable(prev_enum_decl)); SgEnumDeclaration * sg_prev_enum_decl = sg_prev_enum_sym == NULL ? NULL : isSgEnumDeclaration(sg_prev_enum_sym->get_declaration()); sg_prev_enum_decl = sg_prev_enum_decl == NULL ? NULL : isSgEnumDeclaration(sg_prev_enum_decl->get_definingDeclaration()); SgEnumDeclaration * sg_enum_decl = SageBuilder::buildEnumDeclaration(name, SageBuilder::topScopeStack()); *node = sg_enum_decl; if (sg_prev_enum_decl == NULL || sg_prev_enum_decl->get_enumerators().size() == 0) { clang::EnumDecl::enumerator_iterator it; for (it = enum_decl->enumerator_begin(); it != enum_decl->enumerator_end(); it++) { SgNode * tmp_enumerator = Traverse(*it); SgInitializedName * enumerator = isSgInitializedName(tmp_enumerator); ROSE_ASSERT(enumerator); enumerator->set_scope(SageBuilder::topScopeStack()); sg_enum_decl->append_enumerator(enumerator); } } else { sg_enum_decl->set_definingDeclaration(sg_prev_enum_decl); sg_enum_decl->set_firstNondefiningDeclaration(sg_prev_enum_decl->get_firstNondefiningDeclaration()); } return VisitDecl(enum_decl, node) && res; }
void SgEnumDeclaration::fixupCopy_symbols(SgNode* copy, SgCopyHelp & help) const { #if DEBUG_FIXUP_COPY printf ("Inside of SgEnumDeclaration::fixupCopy_symbols() for %p = %s copy = %p (defining = %p firstNondefining = %p) \n", this,this->class_name().c_str(),copy,this->get_definingDeclaration(),this->get_firstNondefiningDeclaration()); #endif // Call the base class fixupCopy member function SgDeclarationStatement::fixupCopy_symbols(copy,help); SgEnumDeclaration* enumDeclaration_copy = isSgEnumDeclaration(copy); ROSE_ASSERT(enumDeclaration_copy != NULL); // DQ (10/17/2007): fixup the type used to make sure it has the declaration set the AST copy. SgEnumType* enum_type_original = this->get_type(); ROSE_ASSERT(enum_type_original != NULL); SgEnumType* enum_type_copy = enumDeclaration_copy->get_type(); ROSE_ASSERT(enum_type_copy != NULL); // Now reset the enum fields. const SgInitializedNamePtrList & enumFieldList_original = this->get_enumerators(); SgInitializedNamePtrList & enumFieldList_copy = enumDeclaration_copy->get_enumerators(); SgInitializedNamePtrList::const_iterator i_original = enumFieldList_original.begin(); SgInitializedNamePtrList::iterator i_copy = enumFieldList_copy.begin(); // Iterate over both lists to match up the correct pairs of SgInitializedName objects while ( (i_original != enumFieldList_original.end()) && (i_copy != enumFieldList_copy.end()) ) { (*i_original)->fixupCopy_symbols(*i_copy,help); i_original++; i_copy++; } }
const SgEnumDeclaration *Type::getEnumDeclaration() const { if( const SgEnumType *enumt = isSgEnumType( t_ ) ) return isSgEnumDeclaration( enumt->get_declaration() ); return 0; }
void getSgDeclarationStatement(SgDeclarationStatement* declStat) { VariantT var = declStat->variantT(); std::string declStatStr = ""; switch (var) { case V_SgEnumDeclaration: { SgEnumDeclaration* enum_decl = isSgEnumDeclaration(declStat); std::string enum_str_name = enum_decl->get_name().getString(); std::cout << ";enum_str_name = " << enum_str_name << std::endl; if (enums_defined.find(enum_str_name) == enums_defined.end()) { std::cout << ";enum not yet defined" << std::endl; std::string enum_name = enum_decl->get_name().getString(); std::stringstream enum_z3; enum_z3 << "(declare-datatypes () ((" << enum_name; SgInitializedNamePtrList enumerated_values = enum_decl->get_enumerators(); for (SgInitializedNamePtrList::iterator j = enumerated_values.begin(); j != enumerated_values.end(); j++) { SgName enum_value_name = (*j)->get_name(); enum_z3 << " " << enum_value_name.getString(); } enum_z3 << ")))"; std::cout << enum_z3.str() << std::endl; enums_defined[enum_str_name] = enum_z3.str(); } break; } case V_SgFunctionParameterList: { std::cout << "SgFunctionParameterList is not yet implemented" << std::endl; ROSE_ASSERT(false); break; } case V_SgTypedefDeclaration: { std::cout << "SgTypedefDeclaration is not yet implemented" << std::endl; ROSE_ASSERT(false); break; } case V_SgVariableDeclaration: { SgVariableDeclaration* varDecl = isSgVariableDeclaration(declStat); SgInitializedName* nam = SageInterface::getFirstInitializedName(varDecl); /* SgInitializedNamePtrList list = varDecl->get_variables(); std::stringstream declStatStrStream; int checkOne = 0; for (SgInitializedNamePtrList::iterator i = list.begin(); i != list.end(); i++) { if (checkOne == 1) { std::cout << "InitializedNamePtrList should have only one InitializedName" << std::endl; ROSE_ASSERT(false); } SgInitializedName* ithName = isSgInitializedName(*i); declStatStrStream << getSgInitializedName(*i) << "\n"; checkOne++; } */ declStatStr = getSgInitializedName(nam) + "\n"; break; } case V_SgVariableDefinition: { std::cout << "SgVariableDefinition is not yet implemented" << std::endl; ROSE_ASSERT(false); break; } case V_SgAttributeSpecificationStatement: { std::cout << "SgAttributeSpecificationStatement is not yet implemented" << std::endl; ; ROSE_ASSERT(false); break; } case V_SgCommonBlock: { std::cout << "SgCommonBlock is not yet implemented" << std::endl; ; ROSE_ASSERT(false); break; } case V_SgEquivalenceStatement: { std::cout << "SgEquivalenceStatement is not yet implemented" << std::endl; ; ROSE_ASSERT(false); break; } case V_SgImplicitStatement: { std::cout << "SgImplicitStatement is not yet implemented" << std::endl; ; ROSE_ASSERT(false); break; } case V_SgNamelistStatement: { std::cout << "SgNamelistStatement is not yet implemented" << std::endl; ; ROSE_ASSERT(false); break; } case V_SgNamespaceDeclarationStatement: { std::cout << "SgNamespaceDeclarationStatement is not yet implemented" << std::endl; ; ROSE_ASSERT(false); break; } case V_SgTemplateInstantiationDirectiveStatement: { std::cout << "SgTemplateInstantiationDirectiveStatement is not yet implemented" << std::endl; ; ROSE_ASSERT(false); break; } case V_SgUsingDeclarationStatement: { std::cout << "SgUsingDeclarationStatement is not yet implemented" << std::endl; ; ROSE_ASSERT(false); break; } case V_SgUsingDirectiveStatement: { std::cout << "SgUsingDirectiveStatement is not yet implemented" << std::endl; ; ROSE_ASSERT(false); break; } case V_SgClassDeclaration: { std::cout << "SgClassDeclaration should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgContainsStatement: { std::cout << "SgContainsStatement should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgFunctionDeclaration: { std::cout << "SgFunctionDeclaration should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgTemplateDeclaration: { std::cout << "SgTemplateDeclaration should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgAsmStmt: { std::cout << "SgAsmStmt should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgC_PreprocessorDirectiveStatement: { std::cout << "SgC_PreprocessorDirectiveStatement should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgClinkageDeclarationStatement: { std::cout << "SgClinkageDeclarationStatement should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgCtorInitializerList: { std::cout << "SgCtorInitializerList should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgFormatStatement: { std::cout << "SgFormatStatement should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgFortranIncludeLine: { std::cout << "SgFortranIncludeLine should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgImportStatement: { std::cout << "SgImportStatement should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgInterfaceStatement: { std::cout << "SgInterfaceStatement should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgJavaImportStatement: { std::cout << "SgJavaImportStatement should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgNamespaceAliasDeclarationStatement: { std::cout << "SgNamespaceAliasDeclarationStatement should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgStmtDeclarationStatement: { std::cout << "SgStmtDeclarationStatement should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgUseStatement: { std::cout << "SgUseStatement should not be found here! " << std::endl; ROSE_ASSERT(false); break; } case V_SgPragmaDeclaration: { //std::cout << "pragmas skipped" << std::endl; // declStatStr=";;pragma\n"; break; } default: { std::cout << " Unknown node type!: " << declStat->class_name() << std::endl; ROSE_ASSERT(false); } } declarations.push_back(declStatStr); return; }
std::string initializeVariable(SgInitializedName* initName) { //if array type we need to get the index expression std::string index_expression_string; std::stringstream nameStringStream; SgName initNameName = initName->get_qualified_name(); SgSymbol* initNameSym = initName->search_for_symbol_from_symbol_table(); if (variablesOfNameX.find(initNameName.getString()) == variablesOfNameX.end()) { nameStringStream << initNameName.getString() << "_0"; variablesOfNameX[initNameName.getString()] = 1; } else { int occurrence = variablesOfNameX[initNameName.getString()]; nameStringStream << initNameName.getString() << "_" << occurrence; variablesOfNameX[initNameName.getString()] = occurrence+1; } SymbolToZ3[initNameSym] = nameStringStream.str(); SymbolToInstances[initNameSym] = 0; SgType* initNameType = initName->get_type(); std::string typeZ3; if (initNameType->isIntegerType()) { typeZ3 = "Int"; } else if (initNameType->isFloatType()) { typeZ3 = "Real"; } else if (isSgArrayType(initNameType)) { SgArrayType* arrTyp = isSgArrayType(initNameType); ROSE_ASSERT(arrTyp != NULL); SgType* underlying_type = arrTyp->get_base_type(); std::string array_typeZ3; if (underlying_type->isIntegerType()) { array_typeZ3 = "Int"; } else if (underlying_type->isFloatType()) { array_typeZ3 = "Real"; } else { std::cout << "unknown underlying type of array!" << std::endl; std::cout << underlying_type->class_name() << std::endl; ROSE_ASSERT(false); } SgExpression* ind = arrTyp->get_index(); std::stringstream arrStr; index_expression_string = getSgExpressionString(ind); typeZ3 = "(Array Int " + array_typeZ3 + ")"; } else if (isSgClassType(initNameType)) { std::cout << "structs are not yet implemented" << std::endl; ROSE_ASSERT(false); } else if (isSgPointerType(initNameType)) { std::cout << "pointers are not yet implemented" << std::endl; ROSE_ASSERT(false); } else if (isSgEnumType(initNameType)) { SgEnumType* et = isSgEnumType(initNameType); SgEnumDeclaration* enum_d = isSgEnumDeclaration(et->getAssociatedDeclaration()); getSgDeclarationStatement(enum_d); typeZ3 = et->get_name().getString(); } else { std::cout << "unknown type: " << initNameType->class_name() << std::endl; ROSE_ASSERT(false); } std::string name = nameStringStream.str() + "_0"; std::stringstream streamZ3; if (isSgArrayType(initNameType)) { streamZ3 << "(declare-const " << name << " " << typeZ3 << ")"; streamZ3 << "\n(declare-fun " << name << "_len () Int)"; streamZ3 << "\n(assert (= " << name << "_len " << index_expression_string << "))"; #ifdef ARRAY_TEST std::cout << "arrStream: " << streamZ3.str() << std::endl; #endif } else if (isSgEnumType(initNameType)) { streamZ3 << "(declare-const " << name << " " << typeZ3 << ")"; } else { streamZ3 << "(declare-fun " << name << " () " << typeZ3 << ")"; } return streamZ3.str(); }