/** * class: SgVarRefExp * term: var_ref_exp_annotation(tpe,name,static,scope) * arg tpe: type * arg name: name * arg static: wether the declaration was static * arg scope: scope name (either from a namespace, a class or "null") */ PrologCompTerm* RoseToTerm::getVarRefExpSpecific(SgVarRefExp* vr) { SgInitializedName* n = vr->get_symbol()->get_declaration(); /* type: in general, this can be taken "as is" from the ROSE AST. However, * ROSE (up to 0.9.4a-8xxx at least) gets a detail wrong: a declaration * like "int arr[]" as a function parameter declares a *pointer*, not an * array. Thus we check whether the variable might be of this sort, and if * yes, we must make a pointer type for it. */ PrologTerm* typeSpecific = NULL; SgInitializedName* vardecl = vr->get_symbol()->get_declaration(); SgType* t = vr->get_type()->stripType(SgType::STRIP_MODIFIER_TYPE | SgType::STRIP_REFERENCE_TYPE | SgType::STRIP_TYPEDEF_TYPE); if (isSgFunctionParameterList(vardecl->get_parent()) && isSgArrayType(t)) { SgType* baseType = isSgArrayType(t)->get_base_type(); PrologTerm* baseTypeSpecific = getTypeSpecific(baseType); typeSpecific = new PrologCompTerm("pointer_type", /*1,*/ baseTypeSpecific); } else { typeSpecific = getTypeSpecific(n->get_typeptr()); } /* static? (relevant for unparsing if scope is a class)*/ PrologTerm *isStatic; SgDeclarationStatement* vdec = n->get_declaration(); if (vdec != NULL) { isStatic = getEnum(vdec->get_declarationModifier().get_storageModifier().isStatic(), re.static_flags); } else { isStatic = getEnum(0, re.static_flags); } PrologTerm* scope; if (vdec != NULL) { /* named scope or irrelevant?*/ if (SgNamespaceDefinitionStatement* scn = isSgNamespaceDefinitionStatement(vdec->get_parent())) { scope = getNamespaceScopeName(scn); } else if (SgClassDefinition* scn = isSgClassDefinition(vdec->get_parent())) { scope = getClassScopeName(scn); } else { scope = new PrologAtom("null"); } } else { scope = new PrologAtom("null"); } return new PrologCompTerm ("var_ref_exp_annotation", //5, typeSpecific, /* name*/ new PrologAtom(n->get_name().getString()), isStatic, scope, PPI(vr)); }
SynthesizedAttribute Traversal::evaluateSynthesizedAttribute ( SgNode* astNode, InheritedAttribute inheritedAttribute, SynthesizedAttributesList childAttributes ) { SynthesizedAttribute localResult; // printf ("evaluateSynthesizedAttribute(): astNode = %p = %s inheritedAttribute.isFirstFile = %s \n",astNode,astNode->class_name().c_str(),inheritedAttribute.isFirstFile ? "true" : "false"); // Accumulate any valid pointer to main on a child node and pass it to the local synthesized attribute. for (size_t i = 0; i < childAttributes.size(); i++) { if (childAttributes[i].main_function != NULL) { localResult.main_function = childAttributes[i].main_function; } } if (inheritedAttribute.isFirstFile == true) { SgGlobal* globalScope = isSgGlobal(astNode); if (globalScope != NULL) { // Gather all of the functions in global scope of the first file. vector<SgDeclarationStatement*> globalScopeDeclarationsToMove = globalScope->get_declarations(); inheritedAttribute.statements_from_first_file = globalScopeDeclarationsToMove; // printf ("evaluateSynthesizedAttribute(): Gather all of the functions in global scope of the first file inheritedAttribute.statements_from_first_file.size() = %zu \n",inheritedAttribute.statements_from_first_file.size()); // Erase the declarations in the global scope of the first file. globalScope->get_declarations().clear(); } SgDeclarationStatement* declarationStatement = isSgDeclarationStatement(astNode); if (declarationStatement != NULL && isSgGlobal(declarationStatement->get_parent()) != NULL) { // Mark as a transformation (recursively mark the whole subtree). // printf ("*** Mark as a transformation: declarationStatement = %p \n",declarationStatement); markAsTransformation(declarationStatement); if (declarationStatement->get_firstNondefiningDeclaration() != NULL) markAsTransformation(declarationStatement->get_firstNondefiningDeclaration()); } } else { SgFunctionDeclaration* functionDeclaration = isSgFunctionDeclaration(astNode); if (functionDeclaration != NULL && functionDeclaration->get_name() == "main") { // Save the pointer to the main function (in the second file). localResult.main_function = functionDeclaration; // printf ("Found the main function ...(saved pointer) inheritedAttribute.main_function = %p \n",localResult.main_function); } // printf ("evaluateSynthesizedAttribute(): localResult.main_function = %p \n",localResult.main_function); // Test for the selected insertion point in the 2nd file for the declarations gathered from the first file. SgGlobal* globalScope = isSgGlobal(astNode); if (globalScope != NULL && localResult.main_function != NULL) { printf ("evaluateSynthesizedAttribute(): Found the main function ...\n"); vector<SgDeclarationStatement*>::iterator i = find(globalScope->get_declarations().begin(),globalScope->get_declarations().end(),localResult.main_function); globalScope->get_declarations().insert(i,inheritedAttribute.statements_from_first_file.begin(),inheritedAttribute.statements_from_first_file.end()); #if 0 // Set the parents of each declaration to match the new location (avoids warning that might later be an error). for (size_t i = 0; i < inheritedAttribute.statements_from_first_file.size(); i++) { inheritedAttribute.statements_from_first_file[i]->set_parent(globalScope); } #endif } } return localResult; }