AstProcessing.h

Go to the documentation of this file.
00001 // Original Author (AstProcessing classes): Markus Schordan
00002 // Rewritten by: Gergo Barany
00003 
00004 #ifndef ASTPROCESSING_H
00005 #define ASTPROCESSING_H
00006 #define INHERITED 0
00007 #define SYNTHESIZED 1
00008 #define BOTH 2
00009 
00010 
00011 
00012 #ifndef ROSE_USE_INTERNAL_FRONTEND_DEVELOPMENT
00013    #include "staticCFG.h"
00014 #endif
00015 
00016 #include <vector>
00017 #include <algorithm>
00018 #include <utility>
00019 #include <iostream>
00020 
00021 //using namespace boost;
00022 
00023 //bool end = false;
00024 
00025 //using namespace std;
00026 
00027 // tps (01/08/2010) Added sage3basic since this doesnt compile under gcc4.1.2
00028 //#include "sage3basic.h"
00029 //#include "sage3.h"
00030 // Non-templated helper function in AstProcessing.C
00031 
00032 
00033 
00034 
00035 bool 
00036 SgTreeTraversal_inFileToTraverse(SgNode* node, bool traversalConstraint, SgFile* fileToVisit);
00037 
00038 /*
00039    GB (06/01/2007):
00040    Changes to the original code were roughly:
00041    - removed unneeded includes and other code
00042    - removed everything related to visit flags
00043    - changed support for SynthesizedAttributesList; synthesized attributes are
00044      now handled in a smarter way, by keeping them on a stack and (in
00045      principle) only passing iterators instead of copying a container;
00046      traverse() is now only a thin wrapper around performTraversal() that
00047      takes care of visiting nodes and pushing/popping synthesized attributes
00048    - rewrote inFileToTraverse() to use new AST features instead of lots of
00049      string comparisons; it is now explicitly stated (at least in this
00050      comment) that we *do* visit subtrees from other files unless their root
00051      is in global or namespace scope, which is how we hope to avoid headers
00052      but still handle all included "code"
00053    - renamed class _DummyAttribute because that identifier is reserved for the
00054      implementation
00055    - everything that was not changed in some other way was totally
00056      reformatted, so diffing the old and new files won't do anything
00057      meaningful
00058    - appended "" suffix to pretty much any global identifier that would
00059      clash with the existing ones; if this code is ever moved into ROSE to
00060      replace the old version, it should not be exceedingly hard to remove the
00061      suffix everywhere
00062    - added some new virtual functions the user may choose to implement:
00063         atTraversalStart(), atTraversalEnd(), destroyInheritedValue()
00064    - other stuff I have probably forgotten
00065    GB (7/6/2007):
00066    - added new class AstPrePostProcessing and changed the traverseOrder
00067      flag so that it can now be pre and post order at the same time
00068  */
00069 
00070 
00071 
00072 
00073 
00074 #include "AstSuccessorsSelectors.h"
00075 #include "StackFrameVector.h"
00076 
00077 // This type is used as a dummy template parameter for those traversals
00078 // that do not use inherited or synthesized attributes.
00079 typedef void *DummyAttribute;
00080 // We initialize DummyAttributes to this value to avoid "may not be
00081 // initialized" warnings. If you change the typedef, adjust the constant...
00082 static const DummyAttribute defaultDummyAttribute = NULL;
00083 // The attribute _DummyAttribute is reserved for the implementation, so we
00084 // should deprecate it, but there is code using it explicitly. Which it
00085 // shouldn't.
00086 typedef DummyAttribute _DummyAttribute;
00087 
00088 
00089 template <class InheritedAttributeType, class SynthesizedAttributeType>
00090 class SgCombinedTreeTraversal;
00091 
00092 
00093 
00094 
00095 // Base class for all traversals.
00096 template <class InheritedAttributeType, class SynthesizedAttributeType>
00097 class SgTreeTraversal
00098 {
00099 public:  
00100     typedef StackFrameVector<SynthesizedAttributeType> SynthesizedAttributesList;
00101 
00102     SynthesizedAttributeType traverse(SgNode* basenode,
00103             InheritedAttributeType inheritedValue,
00104             t_traverseOrder travOrder = preandpostorder);
00105 
00106     SynthesizedAttributeType traverseWithinFile(SgNode* basenode,
00107             InheritedAttributeType inheritedValue,
00108             t_traverseOrder travOrder = preandpostorder);
00109 
00110     void traverseInputFiles(SgProject* projectNode,
00111             InheritedAttributeType inheritedValue,
00112             t_traverseOrder travOrder = preandpostorder);
00113 
00114     // Default destructor/constructor
00115     virtual ~SgTreeTraversal();
00116     SgTreeTraversal();
00117 
00118     // Copy operations
00119     SgTreeTraversal(const SgTreeTraversal &);
00120     const SgTreeTraversal &operator=(const SgTreeTraversal &);
00121 
00122     friend class SgCombinedTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>;
00123    
00124 
00125 #include "Cxx_GrammarTreeTraversalAccessEnums.h"
00126 
00127 protected:
00128     virtual InheritedAttributeType evaluateInheritedAttribute(SgNode* astNode,
00129             InheritedAttributeType inheritedValue) = 0;
00130 
00131     
00132     virtual SynthesizedAttributeType evaluateSynthesizedAttribute(SgNode* n,
00133             InheritedAttributeType in,
00134             SynthesizedAttributesList l) = 0;
00135 
00136     
00137     typedef typename AstSuccessorsSelectors::SuccessorsContainer SuccessorsContainer;
00138     typedef SuccessorsContainer& SuccessorsContainerRef;
00139 
00140     // GB (09/25/2007): Feel free to override this to implement custom traversals, but see the
00141     // comment for set_useDefaultIndexBasedTraversal() below!
00142     virtual void setNodeSuccessors(SgNode* node, SuccessorsContainer& succContainer);
00143 
00144     virtual SynthesizedAttributeType defaultSynthesizedAttribute(InheritedAttributeType inh);
00145 
00146     // GB (06/04/2007): A new virtual function called at the start of the
00147     // traversal, before any node is actually visited; can be used to
00148     // compute attributes that may have changed since the constructor was
00149     // executed, but are constant during the traversal itself. A no-op by
00150     // default. If you don't know what you would use this for, ignore it.
00151     virtual void atTraversalStart();
00152     // GB (06/13/2007): Added this just for symmetry, not sure if it is
00153     // useful, but it won't hurt to have it.
00154     virtual void atTraversalEnd();
00155 
00156     // GB (09/25/2007): This flag determines whether the new index-based traversal mechanism or the more general
00157     // mechanism based on successor containers is to be used. Indexing should be faster, but it would be quite hard to
00158     // adapt it to the reverse traversal and other specialized traversals. Thus: This is true by default, and anybody
00159     // who overrides setNodeSuccessors() *must* change this to false to force the traversal to use their custom
00160     // successor container.
00161     void set_useDefaultIndexBasedTraversal(bool);
00162 private:
00163     void performTraversal(SgNode *basenode,
00164             InheritedAttributeType inheritedValue,
00165             t_traverseOrder travOrder);
00166     SynthesizedAttributeType traversalResult();
00167 
00168     bool useDefaultIndexBasedTraversal;
00169     bool traversalConstraint;
00170     SgFile *fileToVisit;
00171 
00172     // stack of synthesized attributes; evaluateSynthesizedAttribute() is
00173     // automagically called with the appropriate stack frame, which
00174     // behaves like a non-resizable std::vector
00175     SynthesizedAttributesList *synthesizedAttributes;
00176 };
00177 
00178 
00179 
00180 template <class InheritedAttributeType, class SynthesizedAttributeType>
00181 class AstCombinedTopDownBottomUpProcessing;
00182 
00183 template <class InheritedAttributeType, class SynthesizedAttributeType>
00184 class AstTopDownBottomUpProcessing
00185     : public SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>
00186 { 
00187 public:
00188     typedef typename SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>
00189         ::SynthesizedAttributesList SynthesizedAttributesList;
00190 
00191     // deprecated
00192     typedef SynthesizedAttributesList SubTreeSynthesizedAttributes;
00193 
00195     SynthesizedAttributeType traverse(SgNode* node, InheritedAttributeType inheritedValue);
00196     
00197     
00198     
00199 
00200     
00201 
00203     SynthesizedAttributeType traverseWithinFile(SgNode* node, InheritedAttributeType inheritedValue);
00204     
00205 
00206     friend class AstCombinedTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType>;
00207 
00208 protected:
00210     virtual InheritedAttributeType evaluateInheritedAttribute(SgNode* astNode,
00211             InheritedAttributeType inheritedValue) = 0;
00212     
00217     virtual SynthesizedAttributeType evaluateSynthesizedAttribute(SgNode*,
00218             InheritedAttributeType,
00219             SynthesizedAttributesList) = 0;
00220 
00221 
00222     
00226     virtual void atTraversalStart();
00227     virtual void atTraversalEnd();
00228 };
00229 
00230 template <class InheritedAttributeType>
00231 class AstCombinedTopDownProcessing;
00232 
00233 template <class InheritedAttributeType>
00234 class DistributedMemoryAnalysisPreTraversal;
00235 
00236 template <class InheritedAttributeType>
00237 class AstTopDownProcessing
00238     : public SgTreeTraversal<InheritedAttributeType, DummyAttribute>
00239 {
00240 public:
00241     typedef typename SgTreeTraversal<InheritedAttributeType, DummyAttribute>
00242         ::SynthesizedAttributesList SynthesizedAttributesList;
00243 
00245     void traverse(SgNode* node, InheritedAttributeType inheritedValue);
00246     
00247  
00249     void traverseWithinFile(SgNode* node, InheritedAttributeType inheritedValue);
00250     
00251 
00252     friend class AstCombinedTopDownProcessing<InheritedAttributeType>;
00253     friend class DistributedMemoryAnalysisPreTraversal<InheritedAttributeType>;
00254 
00255 protected:
00257     virtual InheritedAttributeType evaluateInheritedAttribute(SgNode* astNode,
00258             InheritedAttributeType inheritedValue) = 0;
00259 
00260 
00264     virtual void atTraversalStart();
00265     virtual void atTraversalEnd();
00266     // GB (06/04/2007): This is a new virtual function, a no-op by
00267     // default. It is called for every node, after its successors have
00268     // been visited, with the inherited attribute computed at this node.
00269     // The intention is to be able to free any memory (or other resources)
00270     // allocated by evaluateInheritedAttribute().
00271     virtual void destroyInheritedValue(SgNode*, InheritedAttributeType);
00272 
00273 private:
00274     DummyAttribute evaluateSynthesizedAttribute(SgNode* astNode,
00275             InheritedAttributeType inheritedValue,
00276             SynthesizedAttributesList l);
00277 
00278    
00279     DummyAttribute defaultSynthesizedAttribute(InheritedAttributeType inh);
00280 };
00281 
00282 template <class SynthesizedAttributeType>
00283 class AstCombinedBottomUpProcessing;
00284 
00285 template <class InheritedAttributeType>
00286 class DistributedMemoryAnalysisPostTraversal;
00287 
00288 template <class SynthesizedAttributeType>
00289 class AstBottomUpProcessing
00290     : public SgTreeTraversal<DummyAttribute,SynthesizedAttributeType>
00291 {
00292 public:
00293     typedef typename SgTreeTraversal<DummyAttribute, SynthesizedAttributeType>
00294         ::SynthesizedAttributesList SynthesizedAttributesList;
00295  
00296     // deprecated
00297     typedef SynthesizedAttributesList SubTreeSynthesizedAttributes; 
00298 
00299     
00300 
00302     SynthesizedAttributeType traverse(SgNode* node);
00303     
00304 
00306     SynthesizedAttributeType traverseWithinFile(SgNode* node);
00307     
00308 
00310     void traverseInputFiles(SgProject* projectNode);
00311 
00312     friend class AstCombinedBottomUpProcessing<SynthesizedAttributeType>;
00313     friend class DistributedMemoryAnalysisPostTraversal<SynthesizedAttributeType>;
00314 
00315 protected:
00320     virtual SynthesizedAttributeType evaluateSynthesizedAttribute(SgNode*, SynthesizedAttributesList) = 0;
00321     
00322 
00326     virtual SynthesizedAttributeType defaultSynthesizedAttribute();
00330     virtual void atTraversalStart();
00331     virtual void atTraversalEnd();
00332 
00333 private:
00334     virtual DummyAttribute evaluateInheritedAttribute(SgNode* astNode, DummyAttribute inheritedValue);
00335     
00336 
00337     virtual SynthesizedAttributeType evaluateSynthesizedAttribute(SgNode* astNode, DummyAttribute inheritedValue, SynthesizedAttributesList l);
00338        
00339 
00340      
00341     virtual SynthesizedAttributeType defaultSynthesizedAttribute(DummyAttribute inheritedValue);
00342 };
00343 
00344 // deprecated classes (provided for compatibility with existing user code - will be removed at some point in future)
00345 class AstSynthesizedAttribute {};
00346 class AstInheritedAttribute {};
00347 
00349 class SgSynthesizedAttribute : public AstSynthesizedAttribute {};
00350 
00352 class SgInheritedAttribute : public AstInheritedAttribute {};
00353 
00354 
00357 template <class InheritedAttributeType, class SynthesizedAttributeType>
00358 class SgTopDownBottomUpProcessing : public AstTopDownBottomUpProcessing <InheritedAttributeType, SynthesizedAttributeType> {};
00359 
00361 template <class InheritedAttributeType>
00362 class SgTopDownProcessing : public AstTopDownProcessing <InheritedAttributeType> {};
00363 
00365 template <class SynthesizedAttributeType>
00366 class SgBottomUpProcessing : public AstBottomUpProcessing <SynthesizedAttributeType> {};
00367 
00368 // Original Author (AstProcessing classes): Markus Schordan
00369 // Rewritten by: Gergo Barany
00370 // $Id: AstProcessing.C,v 1.10 2008/01/25 02:25:48 dquinlan Exp $
00371 
00372 // For information about the changes introduced during the rewrite, see
00373 // the comment in AstProcessing.h
00374 
00375 template<class InheritedAttributeType, class SynthesizedAttributeType>
00376 void
00377 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00378 setNodeSuccessors(SgNode* node, SuccessorsContainer& succContainer)
00379 {
00380     AstSuccessorsSelectors::selectDefaultSuccessors(node, succContainer);
00381 }
00382 
00383 
00384 
00385 
00386 // The default constructor of the internal tree traversal class
00387 template<class InheritedAttributeType, class SynthesizedAttributeType>
00388 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00389 SgTreeTraversal() 
00390   : useDefaultIndexBasedTraversal(true),
00391     traversalConstraint(false),
00392     fileToVisit(NULL),
00393     synthesizedAttributes(new SynthesizedAttributesList())
00394 {
00395 }
00396 
00397 #ifndef SWIG
00398 // The destructor of the internal tree traversal class
00399 template<class InheritedAttributeType, class SynthesizedAttributeType>
00400 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00401 ~SgTreeTraversal()
00402 {
00403     ROSE_ASSERT(synthesizedAttributes != NULL);
00404     delete synthesizedAttributes;
00405     synthesizedAttributes = NULL;
00406 }
00407 
00408 
00409 #endif
00410 
00411 template<class InheritedAttributeType, class SynthesizedAttributeType>
00412 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00413 SgTreeTraversal(const SgTreeTraversal &other)
00414   : useDefaultIndexBasedTraversal(other.useDefaultIndexBasedTraversal),
00415     traversalConstraint(other.traversalConstraint),
00416     fileToVisit(other.fileToVisit),
00417     synthesizedAttributes(other.synthesizedAttributes->deepCopy())
00418 {
00419 }
00420 
00421 template<class InheritedAttributeType, class SynthesizedAttributeType>
00422 const SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType> &
00423 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00424 operator=(const SgTreeTraversal &other)
00425 {
00426     useDefaultIndexBasedTraversal = other.useDefaultIndexBasedTraversal;
00427     traversalConstraint = other.traversalConstraint;
00428     fileToVisit = other.fileToVisit;
00429 
00430     ROSE_ASSERT(synthesizedAttributes != NULL);
00431     delete synthesizedAttributes;
00432     synthesizedAttributes = other.synthesizedAttributes->deepCopy();
00433 
00434     return *this;
00435 }
00436 
00437 template<class InheritedAttributeType, class SynthesizedAttributeType>
00438 void
00439 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00440 set_useDefaultIndexBasedTraversal(bool val)
00441 {
00442     useDefaultIndexBasedTraversal = val;
00443 }
00444 
00445 // MS: 03/22/02ROSE/tests/roseTests/astProcessingTests/
00446 // function to traverse all ASTs representing inputfiles (excluding include files), 
00447 template<class InheritedAttributeType, class SynthesizedAttributeType>
00448 void
00449 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00450 traverseInputFiles(SgProject* projectNode,
00451         InheritedAttributeType inheritedValue,
00452         t_traverseOrder travOrder)
00453    {
00454      const SgFilePtrList& fList = projectNode->get_fileList();
00455 
00456   // DQ (9/1/2008): It is observed that this prevents a SgProject from being built on the generated DOT file!
00457   // We might want a better design to be used here or call the evaluation directly to force the handling of 
00458   // inherited and synthesized attributes on the SgProject.  This detail effect the handling of multiple
00459   // files on the command line (something we want to get a global perspective on if possible).
00460      if ( SgProject::get_verbose() > 0 )
00461           printf ("Warning: The traverseInputFiles() iteration over the file list prevents the evaluation of inherited and synthesized attributes on the SgProject IR node! \n");
00462 
00463      for (SgFilePtrList::const_iterator fl_iter = fList.begin(); fl_iter != fList.end(); fl_iter++)
00464         {
00465           ROSE_ASSERT(*fl_iter != NULL);
00466           traverseWithinFile((*fl_iter), inheritedValue, travOrder);
00467         }
00468    }
00469 
00470 
00471 
00472 
00473                 
00477 
00478 
00479 // MS: 04/25/02
00480 template <class InheritedAttributeType, class SynthesizedAttributeType>
00481 SynthesizedAttributeType 
00482 AstTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType>::
00483 traverse(SgNode* node, InheritedAttributeType inheritedValue)
00484 {
00485     // this is now explicitly marked as a pre *and* post order traversal
00486     return SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>
00487         ::traverse(node, inheritedValue, preandpostorder);
00488 }
00489 
00490 
00491 // MS: 04/25/02
00492 template <class InheritedAttributeType, class SynthesizedAttributeType>
00493 SynthesizedAttributeType 
00494 AstTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType>::
00495 traverseWithinFile(SgNode* node, InheritedAttributeType inheritedValue)
00496 {
00497     // this is now explicitly marked as a pre *and* post order traversal
00498     return SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::traverseWithinFile(node, inheritedValue, preandpostorder);
00499 }
00500 
00504 
00505 // MS: 04/25/02
00506 template <class InheritedAttributeType>
00507 DummyAttribute
00508 AstTopDownProcessing<InheritedAttributeType>::
00509 evaluateSynthesizedAttribute(SgNode* astNode,
00510         InheritedAttributeType inheritedValue,
00511         typename AstTopDownProcessing<InheritedAttributeType>::SynthesizedAttributesList l)
00512 {
00513     // call the cleanup function
00514     destroyInheritedValue(astNode, inheritedValue);
00515     // return value is not used
00516     DummyAttribute a = defaultDummyAttribute;
00517     return a;
00518 }
00519 
00520 
00521 // MS: 07/30/04
00522 template <class InheritedAttributeType>
00523 DummyAttribute
00524 AstTopDownProcessing<InheritedAttributeType>::
00525 defaultSynthesizedAttribute(InheritedAttributeType inh)
00526 {
00527     // called but not used
00528     DummyAttribute a = defaultDummyAttribute;
00529     return a;
00530 }
00531 
00532 // MS: 04/25/02
00533 template <class InheritedAttributeType>
00534 void
00535 AstTopDownProcessing<InheritedAttributeType>::
00536 traverse(SgNode* node, InheritedAttributeType inheritedValue)
00537 {
00538     // "top down" is now marked as a pre *and* post order traversal because
00539     // there is a post order component (the call to destroyInheritedAttribute)
00540     SgTreeTraversal<InheritedAttributeType, DummyAttribute>
00541         ::traverse(node, inheritedValue, preandpostorder);
00542 }
00543 
00544 
00545 // MS: 09/30/02
00546 template <class InheritedAttributeType>
00547 void
00548 AstTopDownProcessing<InheritedAttributeType>::
00549 traverseWithinFile(SgNode* node, InheritedAttributeType inheritedValue)
00550 {
00551     // "top down" is now marked as a pre *and* post order traversal because
00552     // there is a post order component (the call to destroyInheritedAttribute)
00553     SgTreeTraversal<InheritedAttributeType, DummyAttribute>::traverseWithinFile(node, inheritedValue, preandpostorder);
00554 }
00555 
00556 
00560 
00561 // MS: 04/25/02
00562 template <class SynthesizedAttributeType>
00563 DummyAttribute
00564 AstBottomUpProcessing<SynthesizedAttributeType>::
00565 evaluateInheritedAttribute(SgNode* astNode, DummyAttribute inheritedValue)
00566 {
00567     /* called but not used */
00568     DummyAttribute a = defaultDummyAttribute;
00569     return a;
00570 }
00571 
00572 
00573 
00574 
00575 // MS: 30/07/04
00576 template <class SynthesizedAttributeType>
00577 SynthesizedAttributeType AstBottomUpProcessing<SynthesizedAttributeType>::
00578 defaultSynthesizedAttribute()
00579 {
00580     // GB (8/6/2007): This can give "may not be initialized" warnings when
00581     // compiling with optimization (because -O flags cause gcc to perform
00582     // data-flow analysis). I wonder how this might be fixed.
00583     SynthesizedAttributeType s = SynthesizedAttributeType();
00584     return s;
00585 }
00586 
00587 // MS: 30/07/04
00588 template <class SynthesizedAttributeType>
00589 SynthesizedAttributeType AstBottomUpProcessing<SynthesizedAttributeType>::
00590 defaultSynthesizedAttribute(DummyAttribute inheritedValue)
00591 {
00592     return defaultSynthesizedAttribute();
00593 }
00594 
00595 // MS: 04/25/02//ENDEDIT
00596 template <class SynthesizedAttributeType>
00597 SynthesizedAttributeType AstBottomUpProcessing<SynthesizedAttributeType>::
00598 evaluateSynthesizedAttribute(SgNode* astNode,
00599         DummyAttribute inheritedValue,
00600         SynthesizedAttributesList l)
00601 {
00602     return evaluateSynthesizedAttribute(astNode, l);
00603 }
00604 
00605 
00606 
00607 
00608 
00609 // MS: 04/25/02
00610 template <class SynthesizedAttributeType>
00611 SynthesizedAttributeType AstBottomUpProcessing<SynthesizedAttributeType>::
00612 traverse(SgNode* node)
00613 {
00614 
00615     static DummyAttribute da;
00616     return SgTreeTraversal<DummyAttribute, SynthesizedAttributeType>
00617         ::traverse(node, da, postorder);
00618 
00619 }
00620 
00621 // MS: 04/25/02
00622 template <class SynthesizedAttributeType>
00623 SynthesizedAttributeType AstBottomUpProcessing<SynthesizedAttributeType>::
00624 traverseWithinFile(SgNode* node)
00625 {
00626     static DummyAttribute da;
00627     return SgTreeTraversal<DummyAttribute, SynthesizedAttributeType>::traverseWithinFile(node, da, postorder);
00628 }
00629 
00630 
00631 
00632 // MS: 04/25/02
00633 template <class SynthesizedAttributeType>
00634 void AstBottomUpProcessing<SynthesizedAttributeType>::
00635 traverseInputFiles(SgProject* projectNode)
00636 {
00637     static DummyAttribute da;
00638     // GB (8/6/2007): This is now a postorder traversal; this did not really
00639     // matter until now, but now evaluateSynthesizedAttribute is only called
00640     // for traversals that have the postorder bit set.
00641     SgTreeTraversal<DummyAttribute, SynthesizedAttributeType>
00642         ::traverseInputFiles(projectNode, da, postorder);
00643 }
00644 #ifdef _MSC_VER
00645 //class BooleanQueryInheritedAttributeType;
00646 #include "../astQuery/booleanQuery.h"
00647 #include "../astQuery/booleanQueryInheritedAttribute.h"
00648 #endif
00649 // MS: 07/29/04
00650 template <class InheritedAttributeType, class SynthesizedAttributeType>
00651 SynthesizedAttributeType SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00652 defaultSynthesizedAttribute(InheritedAttributeType inh)
00653 {
00654     // we provide 'inh' but do not use it in the constructor of 's' to allow primitive types
00655     SynthesizedAttributeType s = SynthesizedAttributeType(); 
00656     return s;
00657 }
00658 
00659 // MS: 09/30/02
00660 template <class InheritedAttributeType, class SynthesizedAttributeType>
00661 SynthesizedAttributeType
00662 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00663 traverseWithinFile(SgNode* node,
00664         InheritedAttributeType inheritedValue,
00665         t_traverseOrder treeTraversalOrder)
00666 {
00667   // DQ (1/18/2006): debugging
00668      ROSE_ASSERT(this != NULL);
00669      traversalConstraint = true;
00670 
00671      SgFile* filenode = isSgFile(node);
00672      ROSE_ASSERT(filenode != NULL); // this function will be extended to work with all nodes soon
00673 
00674      // GB (05/30/2007): changed to a SgFile* instead of a file name,
00675      // comparisons are much cheaper this way
00676      fileToVisit = filenode;
00677 
00678      ROSE_ASSERT(SgTreeTraversal_inFileToTraverse(node, traversalConstraint, fileToVisit) == true);
00679      
00680      SynthesizedAttributeType synth = traverse(node, inheritedValue, treeTraversalOrder);
00681      traversalConstraint = false;
00682      return synth;
00683 }
00684 
00685 
00686 
00687 
00688 // GB (06/31/2007): Wrapper function around the performT
00689 //raversal()
00690 // function that does the real work; when that function is done, we call
00691 // traversalResult() to get the final result off the stack of synthesized
00692 // attributes.
00693 /*
00694 template <class InheritedAttributeType>
00695 DummyAttribute
00696 AstTopDownProcessing<InheritedAttributeType>::
00697 defaultSynthesizedAttribute(InheritedAttributeType inh)
00698 {arentset.begin(); i != parentset.end(); i++) {
00699      map<SgGraphNode*, InheritedAttribute
00700     // called but not used
00701     DummyAttribute a = defaultDummyAttribute;
00702     return a;
00703 }
00704 */
00705 template <class InheritedAttributeType, class SynthesizedAttributeType>
00706 SynthesizedAttributeType SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00707 traverse(SgNode *node, InheritedAttributeType inheritedValue,
00708         t_traverseOrder treeTraversalOrder)
00709 {
00710     // make sure the stack is empty
00711     synthesizedAttributes->resetStack();
00712     ROSE_ASSERT(synthesizedAttributes->debugSize() == 0);
00713 
00714     // notify the concrete traversal class that a traversal is starting
00715     atTraversalStart();
00716 
00717     // perform the actual traversal
00718     performTraversal(node, inheritedValue, treeTraversalOrder);
00719 
00720     // notify the traversal that we are done
00721     atTraversalEnd();
00722 
00723     // get the result off the stack
00724     return traversalResult();
00725 }
00726 
00727 
00728 
00729 template<class InheritedAttributeType, class SynthesizedAttributeType>
00730 void
00731 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00732 performTraversal(SgNode* node,
00733         InheritedAttributeType inheritedValue,
00734         t_traverseOrder treeTraversalOrder)
00735    {
00736     //cout << "In SgNode version" << endl;
00737   // 1. node can be a null pointer, only traverse it if !
00738 
00739   //    (since the SuccessorContainer is order preserving we require 0 values as well!)
00740   // 2. inFileToTraverse is false if we are trying to go to a different file (than the input file)
00741   //    and only if traverseInputFiles was invoked, otherwise it's always true
00742 
00743      if (node && SgTreeTraversal_inFileToTraverse(node, traversalConstraint, fileToVisit))
00744         {
00745        // In case of a preorder traversal call the function to be applied to each node of the AST
00746        // GB (7/6/2007): Because AstPrePostProcessing was introduced, a
00747        // treeTraversalOrder can now be pre *and* post at the same time! The
00748        // == comparison was therefore replaced by a bit mask check.
00749           if (treeTraversalOrder & preorder)
00750                inheritedValue = evaluateInheritedAttribute(node, inheritedValue);
00751   
00752        // Visit the traversable data members of this AST node.
00753        // GB (09/25/2007): Added support for index-based traversals. The useDefaultIndexBasedTraversal flag tells us
00754        // whether to use successor containers or direct index-based access to the node's successors.
00755           AstSuccessorsSelectors::SuccessorsContainer succContainer;
00756           size_t numberOfSuccessors;
00757           if (!useDefaultIndexBasedTraversal)
00758              {
00759                setNodeSuccessors(node, succContainer);
00760                numberOfSuccessors = succContainer.size();
00761              }
00762             else
00763              {
00764                numberOfSuccessors = node->get_numberOfTraversalSuccessors();
00765              }
00766 
00767           for (size_t idx = 0; idx < numberOfSuccessors; idx++)
00768              {
00769                SgNode *child = NULL;
00770 
00771                if (useDefaultIndexBasedTraversal)
00772                   {
00773                  // ROSE_ASSERT(node->get_traversalSuccessorByIndex(idx) != NULL || node->get_traversalSuccessorByIndex(idx) == NULL);
00774                     child = node->get_traversalSuccessorByIndex(idx);
00775                   }
00776                  else
00777                   {
00778                  // ROSE_ASSERT(succContainer[idx] != NULL || succContainer[idx] == NULL);
00779                     child = succContainer[idx];
00780                   }
00781 
00782                if (child != NULL)
00783                   {
00784                 
00785                     
00786                    
00787                         performTraversal(child, inheritedValue, treeTraversalOrder);
00788                         
00789                 
00790                    
00791                 //ENDEDIT
00792                    }
00793                  else
00794                   {
00795                  // null pointer (not traversed): we put the default value(s) of SynthesizedAttribute onto the stack
00796                     if (treeTraversalOrder & postorder)
00797                          synthesizedAttributes->push(defaultSynthesizedAttribute(inheritedValue));
00798                   }
00799              }
00800  
00801        // In case of a postorder traversal call the function to be applied to each node of the AST
00802        // GB (7/6/2007): Because AstPrePostProcessing was introduced, a
00803        // treeTraversalOrder can now be pre *and* post at the same time! The
00804        // == comparison was therefore replaced by a bit mask check.
00805        // The call to evaluateInheritedAttribute at this point also had to be
00806        // changed; it was never elegant anyway as we were not really
00807        // evaluating attributes here.
00808           if (treeTraversalOrder & postorder)
00809              {
00810             // Now that every child's synthesized attributes are on the stack:
00811             // Tell the stack how big the stack frame containing those
00812             // attributes is to be, and pass that frame to
00813             // evaluateSynthesizedAttribute(); then replace those results by
00814             // pushing the computed value onto the stack (which pops off the
00815             // previous stack frame).
00816                synthesizedAttributes->setFrameSize(numberOfSuccessors);
00817                ROSE_ASSERT(synthesizedAttributes->size() == numberOfSuccessors);
00818                synthesizedAttributes->push(evaluateSynthesizedAttribute(node, inheritedValue, *synthesizedAttributes));
00819              }
00820         }
00821        else // if (node && inFileToTraverse(node))
00822         {
00823           if (treeTraversalOrder & postorder)
00824                synthesizedAttributes->push(defaultSynthesizedAttribute(inheritedValue));
00825         }
00826        } // function body
00827 
00828 
00829 // GB (05/30/2007)
00830 template <class InheritedAttributeType, class SynthesizedAttributeType>
00831 SynthesizedAttributeType SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00832 traversalResult()
00833 {
00834     // If the stack of synthesizedAttributes contains exactly one object, then
00835     // that one is the valid final result of the computation, so we should
00836     // return it. Otherwise, either there are no objects on the stack (because
00837     // the traversal didn't use any attributes), or there are more than one
00838     // (usually because the traversal exited prematurely by throwing an
00839     // exception); in this case, just return a default attribute.
00840     if (synthesizedAttributes->debugSize() == 1)
00841     {
00842         return synthesizedAttributes->pop();
00843     }
00844     else
00845     {
00846         static SynthesizedAttributeType sa;
00847         return sa;
00848     }
00849 }
00850 
00851 
00852 // GB (05/30/2007)
00853 template <class InheritedAttributeType, class SynthesizedAttributeType>
00854 void
00855 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00856 atTraversalStart()
00857 {
00858 }
00859 
00860 template <class InheritedAttributeType, class SynthesizedAttributeType>
00861 void
00862 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00863 atTraversalEnd()
00864 {
00865 }
00866 /*
00867 template <class InheritedAttributeType, class SynthesizedAttributeType>
00868 void
00869 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00870 afterSingleTraversal()
00871 {
00872 }
00873 
00874 template <class InheritedAttributeType, class SynthesizedAttributeType>
00875 void
00876 SgTreeTraversal<InheritedAttributeType, SynthesizedAttributeType>::
00877 
00878 beforeSingleTraversal()
00879 {
00880 }
00881 */
00882 
00883 template <class InheritedAttributeType, class SynthesizedAttributeType>
00884 void
00885 AstTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType>::
00886 atTraversalStart()
00887 {
00888 }
00889 
00890 template <class InheritedAttributeType, class SynthesizedAttributeType>
00891 void
00892 AstTopDownBottomUpProcessing<InheritedAttributeType, SynthesizedAttributeType>::
00893 atTraversalEnd()
00894 {
00895 }
00896 
00897 template <class InheritedAttributeType>
00898 void
00899 AstTopDownProcessing<InheritedAttributeType>::
00900 destroyInheritedValue(SgNode*, InheritedAttributeType)
00901 {
00902 }
00903 
00904 template <class InheritedAttributeType>
00905 void
00906 AstTopDownProcessing<InheritedAttributeType>::
00907 atTraversalStart()
00908 {
00909 }
00910 
00911 template <class InheritedAttributeType>
00912 void
00913 AstTopDownProcessing<InheritedAttributeType>::
00914 atTraversalEnd()
00915 {
00916 }
00917 
00918 template <class SynthesizedAttributeType>
00919 void
00920 AstBottomUpProcessing<SynthesizedAttributeType>::
00921 atTraversalStart()
00922 {
00923 }
00924 
00925 template <class SynthesizedAttributeType>
00926 void
00927 AstBottomUpProcessing<SynthesizedAttributeType>::
00928 atTraversalEnd()
00929 {
00930 }
00931 // #endif 
00932 
00933 #include "AstSimpleProcessing.h" // that's a non-templated class which is put in a different file (for gcc to compile&link properly)
00934 
00935 #include "AstCombinedProcessing.h"
00936 
00937 // DQ (3/20/2009): Wrap this in a test to make sure that Cygwin is not being used.
00938 // This causes a problem:
00939 //      error: there are no arguments to �cvLoadImage� that depend on a template parameter, so a declaration of <function name> must be available
00940 // which requires:
00941 //      -fpermissive to compile without error (and then it generates a lot of warnings).
00942 #if !_MSC_VER
00943   #include "AstSharedMemoryParallelProcessing.h"
00944 #endif
00945 
00946 #endif

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