bool findPublicVarMembers (SgNode *node, string &str) { SgVariableDeclaration *decl; if (decl = isSgVariableDeclaration(node)) { SgDeclarationModifier &dfm = decl->get_declarationModifier(); if (dfm.get_accessModifier().isPublic()) { // check if it belongs to a class SgStatement *block = ((SgVariableDeclaration *) node)->get_scope(); SgClassDefinition *classDef; if (classDef = isSgClassDefinition(block)) { if (classDef->get_declaration()->get_class_type() == SgClassDeclaration::e_class) { str = classDef->get_qualified_name().getString(); return true; } } } } return false; }
//----------------------------------------------------------------------------- // Functions required by the tree traversal mechanism ClasshierarchyInhAttr ClasshierarchyTraversal::evaluateInheritedAttribute ( SgNode* astNode, ClasshierarchyInhAttr inheritedAttribute ) { GlobalDatabaseConnection *gdb; // db connection //long funcId; // id of a function declaration Classhierarchy *classhier = getClasshierarchy(); gdb = getDB(); switch(astNode->variantT()) { case V_SgMemberFunctionDeclaration: { SgMemberFunctionDeclaration *funcDec = isSgMemberFunctionDeclaration( astNode ); //funcDec = funcDef->get_declaration(); //if(isSgMemberFunctionDeclaration(funcDec)) { // add to class hierarchy if member function definition //if(isSgMemberFunctionDeclaration()) { //cerr << " adding CHvinf for MembFunc " << endl; SgClassDefinition *classDef = isSgClassDefinition( funcDec->get_scope() ); //assert(classDef); if(classDef) { string classname = classDef->get_qualified_name().str(); // get the classhier. vertex Classhierarchy::dbgVertex chVert = 0; //?? init necessary bool foundClass = false; Classhierarchy::dbgVertexIterator chvi,chvend; boost::tie(chvi,chvend) = boost::vertices( *getClasshierarchy() ); for(; chvi!=chvend; chvi++) { if( boost::get( vertex_dbg_data, *getClasshierarchy() , *chvi).get_typeName() == classname ) { chVert = *chvi; foundClass = true; } } if(foundClass) { property_map< Classhierarchy::dbgType, boost::vertex_classhierarchy_t>::type chMap = boost::get( boost::vertex_classhierarchy, *getClasshierarchy() ); chMap[ chVert ].defined.insert( funcDec ); //get type? //cerr << " added! "; // debug } } //} cerr << " found V_SgMemberFunctionDeclaration done for " <<funcDec->get_mangled_name().str()<< " " << endl; // debug } break; case V_SgClassDefinition: { cerr << " found V_SgClassDef of "; // debug SgClassDefinition *classDef = isSgClassDefinition( astNode ); assert( classDef ); SgName classname = classDef->get_qualified_name(); // make db entry long typeId = UNKNOWNID; typesTableAccess types( gdb ); typesRowdata newtype( typeId, getProjectId(), classname.str() ); typeId = types.retrieveCreateByColumn( &newtype, "typeName", newtype.get_typeName(), newtype.get_projectId() ); cerr << classname.str()<< ", id:" << newtype.get_id() << endl; // debug //classhier->addNode( newtype, newtype.get_typeName() ); //classhier->insertWithName( newtype, newtype.get_typeName() ); classhier->insertVertex( newtype, newtype.get_typeName() ); SgBaseClassList inherits = classDef->get_inheritances(); for( SgBaseClassList::iterator i=inherits.begin(); i!=inherits.end(); i++) { SgClassDeclaration *parentDecl = (*i).get_base_class(); cerr << " found inheritance from " ; // debug assert( parentDecl ); // add new edge typesRowdata partype( UNKNOWNID, getProjectId(), parentDecl->get_name().str() ); // MANGLE long parentId = types.retrieveCreateByColumn( &partype, "typeName", partype.get_typeName(), partype.get_projectId() ); cerr << parentDecl->get_name().str() << ", id: " << parentId << endl; // add to class hierarchy graph, allow only one edge per inheritance //A classhier->addNode( partype, partype.get_typeName() ); //A classhier->addEdge( newtype, partype, false ); classhier->insertEdge( newtype, partype ); } } break; } // switch node type // Note that we have to use a particular constructor (to pass on context information about source code position). // This allows the Rewrite mechanism to position new source code relative to the current position using a simple interface. ClasshierarchyInhAttr returnAttribute(inheritedAttribute,astNode); // FIXME why not return inheritedAttribute??? return returnAttribute; }
//! search for all possible (virtual) function calls vector<SgMemberFunctionDeclaration*> Classhierarchy::searchMemberFunctionCalls(SgMemberFunctionDeclaration* mfCall) { vector<SgMemberFunctionDeclaration*> retvec; property_map< dbgType, boost::vertex_classhierarchy_t>::type chMap = boost::get( boost::vertex_classhierarchy, *this ); SgClassDefinition *classDef = mfCall->get_scope(); SgName classname = classDef->get_qualified_name(); // MANGLE string cnamestr = classname.str(); graph_traits< dbgType >::vertex_iterator vi,vend; dbgVertex vdesc = *vi; bool foundVertex = false; tie(vi,vend) = vertices( *this ); for(; vi!=vend; vi++) { //cerr << " BCH v i"<< get(vertex_index,*this,*vi)<< " i1" << get(vertex_index1, *this, *vi)<<","<< get(vertex_name, *this, *vi) << endl; if( get(vertex_dbg_data, *this, *vi).get_typeName() == cnamestr ) { //cerr << " SMF srch "<< cnamestr <<" vi "<< get(vertex_index,*this,*vi)<< " i1" << get(vertex_index1, *this, *vi)<<","<< get(vertex_name, *this, *vi) << endl; vdesc = *vi; foundVertex = true; break; } } if(!foundVertex) { cerr << " SMF srch "<< cnamestr <<" vi "<< get(vertex_index,*this,*vi)<< " i1" << get(vertex_index1, *this, *vi)<<","<< get(vertex_name, *this, *vi) << endl; } assert( foundVertex ); set<dbgVertex> treeset; treeset.insert( vdesc ); // first find "highest" class in CH that still provides this MF dbgVertex vhighest = vdesc; // first assume its the current one graph_traits<dbgType>::out_edge_iterator oi,oend; tie(oi,oend) = out_edges( vdesc, *this); for(; oi!=oend; oi++) { //cerr << " SMF inherits from "<< get(vertex_index,*this,target(*oi,*this))<< " i1" << get(vertex_index1, *this, target(*oi,*this))<<","<< get(vertex_name, *this, target(*oi,*this)) << endl; // does any of the base classes implement the member function? bool noParentImpl = true; // check if this base class also implements MF for(set<SgNode*>::iterator chd= chMap[target(*oi,*this)].inherited.begin(); chd!= chMap[target(*oi,*this)].inherited.end(); chd++) { SgFunctionDeclaration *inhFunc = isSgFunctionDeclaration( *chd ); bool virt = false; //cerr << " TOPO v srch1" << endl; if(inhFunc->isVirtual()) virt = true; if( (virt) && (compareFunctionDeclarations(inhFunc,mfCall)) ) { // remeber for traversal treeset.insert( target(*oi, *this) ); noParentImpl = false; } } if(noParentImpl) { // we found it vhighest = target(*oi, *this); break; } } //cerr << " SMF high "<< cnamestr <<" vi "<< get(vertex_index,*this,vhighest)<< " i1" << get(vertex_index1, *this, vhighest)<<","<< get(vertex_name, *this, vhighest) << endl; // now traverse class hierachy downwards, for all children that implement this function, add to set set<dbgVertex> tovisit; set<dbgVertex> visited; tovisit.insert( vhighest ); //hier weiter while( tovisit.size() > 0 ) { dbgVertex currVert = *(tovisit.begin()); tovisit.erase( currVert ); visited.insert( currVert ); //cerr << " SMF visi "<< get(vertex_index,*this,currVert)<< " i1" << get(vertex_index1, *this, currVert)<<","<< get(vertex_name, *this, currVert) << endl; for(set<SgNode*>::iterator chd= chMap[currVert].defined.begin(); chd!= chMap[currVert].defined.end(); chd++) { SgMemberFunctionDeclaration*inhFunc = isSgMemberFunctionDeclaration( *chd ); if(compareFunctionDeclarations(inhFunc,mfCall)) { retvec.push_back( inhFunc ); } } graph_traits<dbgType>::in_edge_iterator ii,iend; tie(ii,iend) = in_edges( currVert, *this); for(; ii!=iend; ii++) { dbgVertex child = source(*ii, *this); // only insert of not already visited set<dbgVertex>::iterator found = visited.find( child ); if(found == visited.end()) tovisit.insert( child ); } } //retvec.push_back( mfCall ); return retvec; }