AstTraverseToRoot.h

Go to the documentation of this file.
00001 // ************************************************************************
00002 //                           Traversal To Root
00003 // ************************************************************************
00004 // This traversal represents a 2nd alternative traversal from any AST node 
00005 // back up the AST to the AST Root node (SgFile or SgProject) along the
00006 // unique chain defined by the parent pointers at each node in the AST.
00007 // This traversal can be helpful for queries (or potentially transformations)
00008 // on variables where they are defined in any of the defining scopes from
00009 // the current scope to the global scope.  It was initially developed to
00010 // support queries to identify containment of subtrees in specific
00011 // language constructs (required for the unparser, which currently does not
00012 // use the AST traversal mechanism defined by ROSE, and so can't communicate
00013 // context information though an inherited attribute).
00014 // ************************************************************************
00015 
00016 template< class InheritedAttributeType, class SynthesizedAttributeType >
00017 class TraverseToRoot
00018    {
00019      public:
00020 //        ReverseTraversal();
00021 
00022           SynthesizedAttributeType traverse (
00023                SgNode* astNode,
00024                InheritedAttributeType inputInheritedAttribute );
00025 
00026           virtual InheritedAttributeType evaluateInheritedAttribute (
00027                SgNode* astNode,
00028                InheritedAttributeType inputInheritedAttribute ) = 0;
00029 
00030           virtual SynthesizedAttributeType evaluateSynthesizedAttribute (
00031                SgNode* astNode,
00032                InheritedAttributeType inputInheritedAttribute,
00033                SynthesizedAttributeType inputSynthesizedAttribute ) = 0;
00034    };
00035 
00036 
00037 // Implementation of traverse function
00038 template< class InheritedAttributeType, class SynthesizedAttributeType >
00039 SynthesizedAttributeType
00040 TraverseToRoot<InheritedAttributeType,SynthesizedAttributeType>::traverse (
00041    SgNode* node,
00042    InheritedAttributeType inputInheritedAttribute)
00043    {
00044   // Trace the current node back as far as possible (should be able to reach SgGlobal)
00045   // printf ("Starting at node->sage_class_name() = %s \n",node->sage_class_name());
00046 #if 1
00047      printf ("In traverse: at node->sage_class_name() = %s \n",node->sage_class_name());
00048 #endif
00049 
00050      SynthesizedAttributeType returnAttribute;
00051 
00052      if (node->get_parent() != NULL)
00053         {
00054           SgNode* parentNode = node->get_parent();
00055        // printf ("     parentNode->sage_class_name() = %s \n",parentNode->sage_class_name());
00056 
00057           InheritedAttributeType localInheritedAttribute = evaluateInheritedAttribute(parentNode,inputInheritedAttribute);
00058           SynthesizedAttributeType localSynthesizedAttribute = traverse (parentNode,localInheritedAttribute);
00059 
00060           returnAttribute =
00061                evaluateSynthesizedAttribute (parentNode,localInheritedAttribute,localSynthesizedAttribute);
00062         }
00063 #if 1
00064        else
00065         {
00066           printf ("final node in chain of parents is a %s \n",node->sage_class_name());
00067         }
00068 #endif
00069 
00070      return returnAttribute;
00071    }
00072 
00073 
00074 

Generated on Tue Jan 31 05:31:20 2012 for ROSE by  doxygen 1.4.7