SynthesizedAttribute Traversal::evaluateSynthesizedAttribute ( SgNode* astNode, InheritedAttribute inheritedAttribute, SynthesizedAttributesList childAttributes ) { SynthesizedAttribute result; #if 1 // Accumulate the names in the children into the names at the parent (current node). SynthesizedAttributesList::iterator i; for (i = childAttributes.begin(); i != childAttributes.end(); ++i) { vector<NameStructureType>::iterator n; for (n = i->nameList.begin(); n != i->nameList.end(); ++n) { result.nameList.push_back(*n); } } #endif if (isSgScopeStatement(astNode) != NULL) { // Now process the collected names. processNames(astNode, result); } else { processNode(astNode,result); } return result; }
// must pass low_ids from children to parents to accumulate. ids do // not need to be passed because they are an accumulative attribute. pair<long, long> TreeSerializer:: defaultSynthesizedAttribute(Tree* node, Tree* inh, SynthesizedAttributesList& synl) { // compute node's low_id long low_id = -1; int nChildren = 0; for (SynthesizedAttributesList::iterator sa_itr = synl.begin(); sa_itr!=synl.end(); ++sa_itr) { if ( (*sa_itr).second>=0 ) { // valid low_id if ( nChildren==0 ) low_id = (*sa_itr).second; else low_id = min(low_id, (*sa_itr).second); nChildren++; } } // These skipped nodes do not store ids or low_ids to save time and space. if ( nChildren<=0 ) return pair<long, long>(-1, -1); // return invalid ids and invalid low_ids; else return pair<long, long>(-1, low_id); // return invalid ids and valid low_ids accumulated from children. }
SynthesizedAttribute Traversal::evaluateSynthesizedAttribute ( SgNode* astNode, InheritedAttribute inheritedAttribute, SynthesizedAttributesList childAttributes ) { if (inheritedAttribute == false) { // The inherited attribute is false, i.e. we are not inside any // function, so there can be no loops here. return false; } else { // Fold up the list of child attributes using logical or, i.e. the local // result will be true iff one of the child attributes is true. SynthesizedAttribute localResult = std::accumulate(childAttributes.begin(), childAttributes.end(), false, std::logical_or<bool>()); if (isSgFunctionDefinition(astNode) && localResult == true) { printf ("Found a function containing a for loop ...\n"); } if (isSgForStatement(astNode)) { localResult = true; } return localResult; } }
std::string evaluateSynthesizedAttribute(SgNode *node, SynthesizedAttributesList synAttributes) { std::string result = ""; SynthesizedAttributesList::iterator s; for (s = synAttributes.begin(); s != synAttributes.end(); ++s) { std::string &str = *s; result += str; if (str.size() > 0 && str[str.size()-1] != '\n') result += "\n"; } return result; }
AstNodePtrSynAttr AstNodePtrs::evaluateSynthesizedAttribute(SgNode* node,SynthesizedAttributesList l) { ROSE_ASSERT(node); ROSE_ASSERT(node->variantT()<V_SgNumVariants); string s=string(node->sage_class_name())+"("; bool nullValueExists=false; for(SynthesizedAttributesList::iterator i=l.begin(); i!=l.end();i++) { if((*i).node==NULL) { nullValueExists=true; s+=", NULL"; } else { s+=string(", ")+(*i).node->sage_class_name(); } } s+=")"; //if(nullValueExists) { // cout << s << endl; // if(SgDotExp* dotNode=dynamic_cast<SgDotExp*>(node)) { // cout << "AST TEST: SgDotExp(" << dotNode->get_lhs_operand_i() << ", " << dotNode->get_rhs_operand_i() << ") found." << endl; // } //} // provide a list of node pointers for some simple operations on the AST AstNodePointersList pl; for(SynthesizedAttributesList::iterator i=l.begin(); i!=l.end();i++) { SgNode* np=(*i).node; pl.push_back(np); } visitWithAstNodePointersList(node,pl); AstNodePtrSynAttr syn; syn.node=node; return syn; }
SynthesizedAttribute visitorTraversal::evaluateSynthesizedAttribute ( SgNode* n, SynthesizedAttributesList childAttributes ) { // Fold up the list of child attributes using logical or, i.e. the local // result will be true iff one of the child attributes is true. SynthesizedAttribute localResult = std::accumulate(childAttributes.begin(), childAttributes.end(), false, std::logical_or<bool>()); if (isSgForStatement(n) != NULL) { printf ("Found a for loop ... \n"); localResult = true; } return localResult; }
// Skipped nodes are not linked into the Sq-Tree. We can use the fact to improve performance. pair<long, long> RelevantNoAtomicParent_TreeSerializer:: defaultSynthesizedAttribute(Tree* node, Tree* inh, SynthesizedAttributesList& synl) { // want to assign each node a unique id, even if the node is // skippable, to make is_tree_in_subtree logically clearer. long low_id = id; // ids always valid for this case. for (SynthesizedAttributesList::iterator sa_itr = synl.begin(); sa_itr!=synl.end(); ++sa_itr) { low_id = min(low_id, (*sa_itr).second); } #ifdef outputnodeids fprintf(stdout, "Skipped tree node type = %d, #tokens = %d, id = %d, low_id = %d\n", node->type, node->terminal_number, id, low_id); #endif node->attributes.insert(pair<NodeAttributeName_t, pair<long, long>*>(NODE_ID, new pair<long, long>(id, low_id))); id++; return pair<long, long>(id-1, low_id); }
pair<long, long> TreeSerializer:: evaluateSynthesizedAttribute(Tree* node, Tree* in, SynthesizedAttributesList& synl) { // establish serialized tree chains: if ( previous_node!=NULL ) { map<NodeAttributeName_t, void*>::iterator attr_itr = previous_node->attributes.find(NODE_SERIALIZED_NEIGHBOR); assert( attr_itr!=previous_node->attributes.end() ); ((pair<Tree*, Tree*>*)(*attr_itr).second)->second = node; node->attributes.insert(pair<NodeAttributeName_t, pair<Tree*, Tree*>*>(NODE_SERIALIZED_NEIGHBOR, new pair<Tree*, Tree*>(previous_node, NULL))); } else { // its the first visited node: serialized_tree.chain_header = node; node->attributes.insert(pair<NodeAttributeName_t, pair<Tree*, Tree*>*>(NODE_SERIALIZED_NEIGHBOR, new pair<Tree*, Tree*>(NULL, NULL))); } previous_node = node; serialized_tree.chain_tail = node; // compute node id and its low_id long low_id = id; // It's the id of the current node. for (SynthesizedAttributesList::iterator sa_itr = synl.begin(); sa_itr!=synl.end(); ++sa_itr) { if ( (*sa_itr).first>=0 ) // valid id and low_id low_id = min(low_id, (*sa_itr).second); } #ifdef outputnodeids fprintf(stdout, "Tree node type = %d(%s), #tokens = %d, id = %d, low_id = %d, value=%s\n", node->type, node->terminal_number, id, low_id, node->toTerminal()?node->toTerminal()->value->c_str():"<NULL>"); #endif node->attributes.insert(pair<NodeAttributeName_t, pair<long, long>*>(NODE_ID, new pair<long, long>(id, low_id))); id2node.insert(pair<long, Tree*>(id, node)); id++; return pair<long, long>(id-1, low_id); }