ROSE  0.9.9.109
AstTraverseToRoot.h
1 // ************************************************************************
2 // Traversal To Root
3 // ************************************************************************
4 // This traversal represents a 2nd alternative traversal from any AST node
5 // back up the AST to the AST Root node (SgFile or SgProject) along the
6 // unique chain defined by the parent pointers at each node in the AST.
7 // This traversal can be helpful for queries (or potentially transformations)
8 // on variables where they are defined in any of the defining scopes from
9 // the current scope to the global scope. It was initially developed to
10 // support queries to identify containment of subtrees in specific
11 // language constructs (required for the unparser, which currently does not
12 // use the AST traversal mechanism defined by ROSE, and so can't communicate
13 // context information though an inherited attribute).
14 // ************************************************************************
15 
16 template< class InheritedAttributeType, class SynthesizedAttributeType >
18  {
19  public:
20 // ReverseTraversal();
21 
22  SynthesizedAttributeType traverse (
23  SgNode* astNode,
24  InheritedAttributeType inputInheritedAttribute );
25 
26  virtual InheritedAttributeType evaluateInheritedAttribute (
27  SgNode* astNode,
28  InheritedAttributeType inputInheritedAttribute ) = 0;
29 
30  virtual SynthesizedAttributeType evaluateSynthesizedAttribute (
31  SgNode* astNode,
32  InheritedAttributeType inputInheritedAttribute,
33  SynthesizedAttributeType inputSynthesizedAttribute ) = 0;
34  };
35 
36 
37 // Implementation of traverse function
38 template< class InheritedAttributeType, class SynthesizedAttributeType >
39 SynthesizedAttributeType
41  SgNode* node,
42  InheritedAttributeType inputInheritedAttribute)
43  {
44  // Trace the current node back as far as possible (should be able to reach SgGlobal)
45  // printf ("Starting at node->sage_class_name() = %s \n",node->sage_class_name());
46 #if 1
47  printf ("In traverse: at node->sage_class_name() = %s \n",node->sage_class_name());
48 #endif
49 
50  SynthesizedAttributeType returnAttribute;
51 
52  if (node->get_parent() != NULL)
53  {
54  SgNode* parentNode = node->get_parent();
55  // printf (" parentNode->sage_class_name() = %s \n",parentNode->sage_class_name());
56 
57  InheritedAttributeType localInheritedAttribute = evaluateInheritedAttribute(parentNode,inputInheritedAttribute);
58  SynthesizedAttributeType localSynthesizedAttribute = traverse (parentNode,localInheritedAttribute);
59 
60  returnAttribute =
61  evaluateSynthesizedAttribute (parentNode,localInheritedAttribute,localSynthesizedAttribute);
62  }
63 #if 1
64  else
65  {
66  printf ("final node in chain of parents is a %s \n",node->sage_class_name());
67  }
68 #endif
69 
70  return returnAttribute;
71  }
72 
73 
74 
virtual const char * sage_class_name() const ROSE_DEPRECATED_FUNCTION
generates string representing the class name: (e.g. for SgNode returns "SgNode"). ...
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:8322
SgNode * get_parent() const
Access function for parent node.