AstCombinedProcessingImpl.h

Go to the documentation of this file.
00001 // Author: Gergo Barany
00002 // $Id: AstCombinedProcessing.C,v 1.1 2008/01/08 02:56:38 dquinlan Exp $
00003 
00004 #ifndef ASTCOMBINEDPROCESSING_C
00005 #define ASTCOMBINEDPROCESSING_C
00006 
00007 //#include "sage3.h"
00008 
00009 #include "AstCombinedProcessing.h"
00010 
00011 // Throughout this file, I is the InheritedAttributeType, S is the
00012 // SynthesizedAttributeType -- the type names are still horrible
00013 
00014 // combined TREE TRAVERSAL implementation -- you will probably only want to
00015 // use this to combine traversals of different types
00016 template <class I, class S>
00017 SgCombinedTreeTraversal<I, S>::
00018 SgCombinedTreeTraversal()
00019     : traversals()
00020 {
00021 }
00022 
00023 template <class I, class S>
00024 SgCombinedTreeTraversal<I, S>::
00025 SgCombinedTreeTraversal(
00026             const typename SgCombinedTreeTraversal<I, S>::TraversalPtrList &t)
00027     : traversals(t)
00028 {
00029 }
00030 
00031 template <class I, class S>
00032 void
00033 SgCombinedTreeTraversal<I, S>::
00034 addTraversal(typename SgCombinedTreeTraversal<I, S>::TraversalPtr t)
00035 {
00036     traversals.push_back(t);
00037 }
00038 
00039 template <class I, class S>
00040 typename SgCombinedTreeTraversal<I, S>::TraversalPtrList &
00041 SgCombinedTreeTraversal<I, S>::
00042 get_traversalPtrListRef()
00043 {
00044     return traversals;
00045 }
00046 
00047 template <class I, class S>
00048 typename SgCombinedTreeTraversal<I, S>
00049     ::InheritedAttributeTypeList *
00050 SgCombinedTreeTraversal<I, S>::
00051 evaluateInheritedAttribute(SgNode *astNode,
00052         typename SgCombinedTreeTraversal<I, S>
00053         ::InheritedAttributeTypeList *inheritedValues)
00054 {
00055     InheritedAttributeTypeList *result
00056         = new InheritedAttributeTypeList();
00057     // Reserve just enough space for one inherited attribute per
00058     // traversal, this keeps us from doing expensive resizing later.
00059     result->reserve(numberOfTraversals);
00060 
00061     // Fill the list by evaluating the inherited attributes for each
00062     // traversal.
00063     typename TraversalPtrList::iterator t = tBegin;
00064     typename InheritedAttributeTypeList::iterator i
00065         = inheritedValues->begin();
00066     typename InheritedAttributeTypeList::const_iterator iEnd
00067         = inheritedValues->end();
00068 
00069     while (t != tEnd && i != iEnd)
00070         result->push_back((*t++)->evaluateInheritedAttribute(astNode, *i++));
00071     ROSE_ASSERT(t == tEnd && i == iEnd);
00072     ROSE_ASSERT(result->size() == numberOfTraversals);
00073 
00074     // The inherited attribute list allocated here should be deleted in the
00075     // evaluateSynthesizedAttribute() function.
00076     return result;
00077 }
00078 
00079 template <class I, class S>
00080 typename SgCombinedTreeTraversal<I, S>
00081     ::SynthesizedAttributeTypeList *
00082 SgCombinedTreeTraversal<I, S>::
00083 evaluateSynthesizedAttribute(SgNode *astNode,
00084         typename SgCombinedTreeTraversal<I, S>
00085         ::InheritedAttributeTypeList *inheritedValues,
00086         typename SgCombinedTreeTraversal<I, S>
00087         ::SynthesizedAttributesList synthesizedAttributes)
00088 {
00089     // Let N = number of traversals, M = number of node successors.
00090     // synthesizedAttributes is a list of M lists of N attributes each; we
00091     // want to call each of the N traversals with the corresponding M
00092     // synthesized attributes.
00093     // This is one of the obscure functions.
00094 
00095     typename SynthesizedAttributesList::size_type M
00096         = synthesizedAttributes.size();
00097     // Create a container of size M in which we will store the synthesized
00098     // attributes for each traversal.
00099     typename TraversalType::SynthesizedAttributesList
00100         attributesForTraversal(M);
00101     // Create a list for the traversal results.
00102     SynthesizedAttributeTypeList *result
00103         = new SynthesizedAttributeTypeList();
00104     result->reserve(numberOfTraversals);
00105 
00106     typename TraversalPtrList::size_type i;
00107     typename SynthesizedAttributesList::size_type j;
00108 
00109     for (i = 0; i < numberOfTraversals; i++)
00110     {
00111         for (j = 0; j < M; j++)
00112             attributesForTraversal[j] = (*synthesizedAttributes[j])[i];
00113 
00114         result->push_back(
00115                 traversals[i]->evaluateSynthesizedAttribute(
00116                     astNode,
00117                     (*inheritedValues)[i],
00118                     attributesForTraversal));
00119     }
00120 
00121     // The lists of synthesized attributes passed to us are not needed
00122     // anymore, free them.
00123     for (j = 0; j < M; j++)
00124         delete synthesizedAttributes[j];
00125 
00126     // inheritedValues is a pointer to a container that is dynamically
00127     // allocated in evaluateInheritedAttribute(). Now that all successor
00128     // nodes have been visited, we can free the allocated memory.
00129     delete inheritedValues;
00130 
00131     return result;
00132 }
00133 
00134 template <class I, class S>
00135 typename SgCombinedTreeTraversal<I, S>
00136     ::SynthesizedAttributeTypeList *
00137 SgCombinedTreeTraversal<I, S>::
00138 defaultSynthesizedAttribute(typename SgCombinedTreeTraversal<I, S>
00139         ::InheritedAttributeTypeList *inheritedValues)
00140 {
00141     // Create a list for the default attributes.
00142     SynthesizedAttributeTypeList *result
00143         = new SynthesizedAttributeTypeList(numberOfTraversals);
00144 
00145 #if 1
00146     typename TraversalPtrList::size_type i;
00147     for (i = 0; i < numberOfTraversals; i++)
00148         (*result)[i]
00149             = traversals[i]->defaultSynthesizedAttribute((*inheritedValues)[i]);
00150 #else
00151     typename TraversalPtrList::iterator i = traversals.begin();
00152     typename SynthesizedAttributeTypeList::iterator j
00153         = result->begin();
00154     typename InheritedAttributeTypPtrList::iterator k
00155         = inheritedValues->begin();
00156     while (i != tEnd)
00157         *j++ = (*i++)->defaultSynthesizedAttribute(*k++);
00158 #endif
00159 
00160     return result;
00161 }
00162 
00163 template <class I, class S>
00164 void
00165 SgCombinedTreeTraversal<I, S>::
00166 atTraversalStart()
00167 {
00168     // Compute some values that are constant during the traversal, saving
00169     // lots of method calls. We cannot reliably compute these earlier
00170     // because the user is allowed to mess with the traversal container
00171     // using the reference we provide.
00172     tBegin = traversals.begin();
00173     tEnd = traversals.end();
00174     numberOfTraversals = traversals.size();
00175 
00176     // Call this function for all traversals.
00177     typename TraversalPtrList::iterator t;
00178     for (t = tBegin; t != tEnd; ++t)
00179         (*t)->atTraversalStart();
00180 }
00181 
00182 template <class I, class S>
00183 void
00184 SgCombinedTreeTraversal<I, S>::
00185 atTraversalEnd()
00186 {
00187     // Call this function for all traversals.
00188     typename TraversalPtrList::iterator t;
00189     for (t = tBegin; t != tEnd; ++t)
00190         (*t)->atTraversalEnd();
00191 }
00192 
00193 // combined TOP DOWN BOTTOM UP implementation
00194 
00195 template <class I, class S>
00196 AstCombinedTopDownBottomUpProcessing<I, S>::
00197 AstCombinedTopDownBottomUpProcessing()
00198     : traversals()
00199 {
00200 }
00201 
00202 template <class I, class S>
00203 AstCombinedTopDownBottomUpProcessing<I, S>::
00204 AstCombinedTopDownBottomUpProcessing(
00205         const typename AstCombinedTopDownBottomUpProcessing<I, S>::TraversalPtrList &t)
00206     : traversals(t)
00207 {
00208 }
00209 
00210 template <class I, class S>
00211 void
00212 AstCombinedTopDownBottomUpProcessing<I, S>::
00213 addTraversal(typename AstCombinedTopDownBottomUpProcessing<I, S>::TraversalPtr t)
00214 {
00215     traversals.push_back(t);
00216 }
00217 
00218 template <class I, class S>
00219 typename AstCombinedTopDownBottomUpProcessing<I, S>::TraversalPtrList &
00220 AstCombinedTopDownBottomUpProcessing<I, S>::
00221 get_traversalPtrListRef()
00222 {
00223     return traversals;
00224 }
00225 
00226 template <class I, class S>
00227 typename AstCombinedTopDownBottomUpProcessing<I, S>
00228     ::InheritedAttributeTypeList *
00229 AstCombinedTopDownBottomUpProcessing<I, S>::
00230 evaluateInheritedAttribute(SgNode *astNode,
00231         typename AstCombinedTopDownBottomUpProcessing<I, S>
00232         ::InheritedAttributeTypeList *inheritedValues)
00233 {
00234     InheritedAttributeTypeList *result
00235         = new InheritedAttributeTypeList();
00236     // Reserve just enough space for one inherited attribute per
00237     // traversal, this keeps us from doing expensive resizing later.
00238     result->reserve(numberOfTraversals);
00239 
00240     // Fill the list by evaluating the inherited attributes for each
00241     // traversal.
00242     typename TraversalPtrList::iterator t = tBegin;
00243     typename InheritedAttributeTypeList::iterator i
00244         = inheritedValues->begin();
00245     typename InheritedAttributeTypeList::const_iterator iEnd
00246         = inheritedValues->end();
00247 
00248     while (t != tEnd && i != iEnd)
00249         result->push_back((*t++)->evaluateInheritedAttribute(astNode, *i++));
00250     ROSE_ASSERT(t == tEnd && i == iEnd);
00251     ROSE_ASSERT(result->size() == numberOfTraversals);
00252 
00253     // The inherited attribute list allocated here should be deleted in the
00254     // evaluateSynthesizedAttribute() function.
00255     return result;
00256 }
00257 
00258 template <class I, class S>
00259 typename AstCombinedTopDownBottomUpProcessing<I, S>
00260     ::SynthesizedAttributeTypeList *
00261 AstCombinedTopDownBottomUpProcessing<I, S>::
00262 evaluateSynthesizedAttribute(SgNode *astNode,
00263         typename AstCombinedTopDownBottomUpProcessing<I, S>
00264         ::InheritedAttributeTypeList *inheritedValues,
00265         typename AstCombinedTopDownBottomUpProcessing<I, S>
00266         ::SynthesizedAttributesList synthesizedAttributes)
00267 {
00268     // Let N = number of traversals, M = number of node successors.
00269     // synthesizedAttributes is a list of M lists of N attributes each; we
00270     // want to call each of the N traversals with the corresponding M
00271     // synthesized attributes.
00272     // This is one of the obscure functions.
00273 
00274     typename SynthesizedAttributesList::size_type M
00275         = synthesizedAttributes.size();
00276     // Create a container of size M in which we will store the synthesized
00277     // attributes for each traversal.
00278     typename TraversalType::SynthesizedAttributesList
00279         attributesForTraversal(M);
00280     // Create a list for the traversal results.
00281     SynthesizedAttributeTypeList *result
00282         = new SynthesizedAttributeTypeList();
00283     result->reserve(numberOfTraversals);
00284 
00285     typename TraversalPtrList::size_type i;
00286     typename SynthesizedAttributesList::size_type j;
00287 
00288     for (i = 0; i < numberOfTraversals; i++)
00289     {
00290         for (j = 0; j < M; j++)
00291             attributesForTraversal[j] = (*synthesizedAttributes[j])[i];
00292 
00293         result->push_back(
00294                 traversals[i]->evaluateSynthesizedAttribute(
00295                     astNode,
00296                     (*inheritedValues)[i],
00297                     attributesForTraversal));
00298     }
00299 
00300     // The lists of synthesized attributes passed to us are not needed
00301     // anymore, free them.
00302     for (j = 0; j < M; j++)
00303         delete synthesizedAttributes[j];
00304 
00305     // inheritedValues is a pointer to a container that is dynamically
00306     // allocated in evaluateInheritedAttribute(). Now that all successor
00307     // nodes have been visited, we can free the allocated memory.
00308     delete inheritedValues;
00309 
00310     return result;
00311 }
00312 
00313 template <class I, class S>
00314 typename AstCombinedTopDownBottomUpProcessing<I, S>
00315     ::SynthesizedAttributeTypeList *
00316 AstCombinedTopDownBottomUpProcessing<I, S>::
00317 defaultSynthesizedAttribute(typename AstCombinedTopDownBottomUpProcessing<I, S>
00318         ::InheritedAttributeTypeList *inheritedValues)
00319 {
00320     // Create a list for the default attributes.
00321     SynthesizedAttributeTypeList *result
00322         = new SynthesizedAttributeTypeList(numberOfTraversals);
00323 
00324     typename TraversalPtrList::size_type i;
00325     for (i = 0; i < numberOfTraversals; i++)
00326         (*result)[i]
00327             = traversals[i]->defaultSynthesizedAttribute((*inheritedValues)[i]);
00328 
00329     return result;
00330 }
00331 
00332 template <class I, class S>
00333 void
00334 AstCombinedTopDownBottomUpProcessing<I, S>::
00335 atTraversalStart()
00336 {
00337     // Compute some values that are constant during the traversal, saving
00338     // lots of method calls. We cannot reliably compute these earlier
00339     // because the user is allowed to mess with the traversal container
00340     // using the reference we provide.
00341     tBegin = traversals.begin();
00342     tEnd = traversals.end();
00343     numberOfTraversals = traversals.size();
00344 
00345     // Call this function for all traversals.
00346     typename TraversalPtrList::iterator t;
00347     for (t = tBegin; t != tEnd; ++t)
00348         (*t)->atTraversalStart();
00349 }
00350 
00351 template <class I, class S>
00352 void
00353 AstCombinedTopDownBottomUpProcessing<I, S>::
00354 atTraversalEnd()
00355 {
00356     // Call this function for all traversals.
00357     typename TraversalPtrList::iterator t;
00358     for (t = tBegin; t != tEnd; ++t)
00359         (*t)->atTraversalEnd();
00360 }
00361 
00362 
00363 // combined TOP DOWN implementation
00364 
00365 template <class I>
00366 AstCombinedTopDownProcessing<I>::
00367 AstCombinedTopDownProcessing()
00368     : traversals()
00369 {
00370 }
00371 
00372 template <class I>
00373 AstCombinedTopDownProcessing<I>::
00374 AstCombinedTopDownProcessing(
00375         const typename AstCombinedTopDownProcessing<I>::TraversalPtrList &t)
00376     : traversals(t)
00377 {
00378 }
00379 
00380 template <class I>
00381 void
00382 AstCombinedTopDownProcessing<I>::
00383 addTraversal(typename AstCombinedTopDownProcessing<I>::TraversalPtr t)
00384 {
00385     traversals.push_back(t);
00386 }
00387 
00388 template <class I>
00389 typename AstCombinedTopDownProcessing<I>::TraversalPtrList &
00390 AstCombinedTopDownProcessing<I>::
00391 get_traversalPtrListRef()
00392 {
00393     return traversals;
00394 }
00395 
00396 template <class I>
00397 typename AstCombinedTopDownProcessing<I>
00398     ::InheritedAttributeTypeList *
00399 AstCombinedTopDownProcessing<I>::
00400 evaluateInheritedAttribute(SgNode *astNode,
00401         typename AstCombinedTopDownProcessing<I>
00402         ::InheritedAttributeTypeList *inheritedValues)
00403 {
00404     InheritedAttributeTypeList *result
00405         = new InheritedAttributeTypeList();
00406     // Reserve just enough space for one inherited attribute per
00407     // traversal, this keeps us from doing expensive resizing later.
00408     result->reserve(numberOfTraversals);
00409 
00410     // Fill the list by evaluating the inherited attributes for each
00411     // traversal.
00412     typename TraversalPtrList::iterator t = tBegin;
00413     typename InheritedAttributeTypeList::iterator i
00414         = inheritedValues->begin();
00415     typename InheritedAttributeTypeList::const_iterator iEnd
00416         = inheritedValues->end();
00417 
00418     while (t != tEnd && i != iEnd)
00419         result->push_back((*t++)->evaluateInheritedAttribute(astNode, *i++));
00420     ROSE_ASSERT(t == tEnd && i == iEnd);
00421     ROSE_ASSERT(result->size() == numberOfTraversals);
00422 
00423     // The list of inherited attributes allocated here should be freed in
00424     // the destroyInheritedAttribute() function.
00425     return result;
00426 }
00427 
00428 template <class I>
00429 void
00430 AstCombinedTopDownProcessing<I>::
00431 atTraversalStart()
00432 {
00433     // Compute some values that are constant during the traversal, saving
00434     // lots of method calls. We cannot reliably compute these earlier
00435     // because the user is allowed to mess with the traversal container
00436     // using the reference we provide.
00437     tBegin = traversals.begin();
00438     tEnd = traversals.end();
00439     numberOfTraversals = traversals.size();
00440 
00441     // Call this function for all traversals.
00442     typename TraversalPtrList::iterator t;
00443     for (t = tBegin; t != tEnd; ++t)
00444         (*t)->atTraversalStart();
00445 }
00446 
00447 template <class I>
00448 void
00449 AstCombinedTopDownProcessing<I>::
00450 atTraversalEnd()
00451 {
00452     // Call this function for all traversals.
00453     typename TraversalPtrList::iterator t;
00454     for (t = tBegin; t != tEnd; ++t)
00455         (*t)->atTraversalEnd();
00456 }
00457 
00458 template <class I>
00459 void
00460 AstCombinedTopDownProcessing<I>::
00461 destroyInheritedValue(SgNode *node, typename AstCombinedTopDownProcessing<I>::
00462         InheritedAttributeTypeList *inheritedValues)
00463 {
00464     typename TraversalPtrList::iterator t = tBegin;
00465     typename InheritedAttributeTypeList::iterator i
00466         = inheritedValues->begin();
00467     typename InheritedAttributeTypeList::const_iterator iEnd
00468         = inheritedValues->end();
00469 
00470     // Call this function for all traversals.
00471     while (t != tEnd && i != iEnd)
00472         (*t++)->destroyInheritedValue(node, *i++);
00473     ROSE_ASSERT(t == tEnd && i == iEnd);
00474 
00475     // inheritedValues is a pointer to a container that is dynamically
00476     // allocated in evaluateInheritedAttribute(). Now that all successor
00477     // nodes have been visited, we can free the allocated memory.
00478     delete inheritedValues;
00479 }
00480 
00481 // combined BOTTOM UP implementation
00482 
00483 template <class S>
00484 AstCombinedBottomUpProcessing<S>::
00485 AstCombinedBottomUpProcessing()
00486     : traversals()
00487 {
00488 }
00489 
00490 template <class S>
00491 AstCombinedBottomUpProcessing<S>::
00492 AstCombinedBottomUpProcessing(
00493         const typename AstCombinedBottomUpProcessing<S>::TraversalPtrList &t)
00494     : traversals(t)
00495 {
00496 }
00497 
00498 template <class S>
00499 void
00500 AstCombinedBottomUpProcessing<S>::
00501 addTraversal(typename AstCombinedBottomUpProcessing<S>::TraversalPtr t)
00502 {
00503     traversals.push_back(t);
00504 }
00505 
00506 template <class S>
00507 typename AstCombinedBottomUpProcessing<S>::TraversalPtrList &
00508 AstCombinedBottomUpProcessing<S>::
00509 get_traversalPtrListRef()
00510 {
00511     return traversals;
00512 }
00513 
00514 template <class S>
00515 typename AstCombinedBottomUpProcessing<S>
00516     ::SynthesizedAttributeTypeList *
00517 AstCombinedBottomUpProcessing<S>::
00518 evaluateSynthesizedAttribute(SgNode *astNode,
00519         typename AstCombinedBottomUpProcessing<S>
00520         ::SynthesizedAttributesList synthesizedAttributes)
00521 {
00522     // Let N = number of traversals, M = number of node successors.
00523     // synthesizedAttributes is a list of M lists of N attributes each; we
00524     // want to call each of the N traversals with the corresponding M
00525     // synthesized attributes.
00526     // This is one of the obscure functions.
00527 
00528     typename SynthesizedAttributesList::size_type M
00529         = synthesizedAttributes.size();
00530     // Create a container of size M in which we will store the synthesized
00531     // attributes for each traversal.
00532     typename TraversalType::SynthesizedAttributesList
00533         attributesForTraversal(M, NULL);
00534     // Create a list for the traversal results.
00535     SynthesizedAttributeTypeList *result
00536         = new SynthesizedAttributeTypeList(numberOfTraversals);
00537 
00538     typename TraversalPtrList::size_type i;
00539     typename SynthesizedAttributesList::size_type j;
00540 
00541     for (i = 0; i < numberOfTraversals; i++)
00542     {
00543         for (j = 0; j < M; j++)
00544             attributesForTraversal[j] = (*synthesizedAttributes[j])[i];
00545 
00546         (*result)[i]
00547             = traversals[i]->evaluateSynthesizedAttribute(
00548                     astNode,
00549                     attributesForTraversal);
00550     }
00551     ROSE_ASSERT(result->size() == numberOfTraversals);
00552 
00553     // The lists of synthesized attributes passed to us are not needed
00554     // anymore, free them.
00555     for (j = 0; j < M; j++)
00556         delete synthesizedAttributes[j];
00557 
00558     return result;
00559 }
00560 
00561 template <class S>
00562 typename AstCombinedBottomUpProcessing<S>
00563     ::SynthesizedAttributeTypeList *
00564 AstCombinedBottomUpProcessing<S>::
00565 defaultSynthesizedAttribute()
00566 {
00567     // Create a list for the default attributes.
00568     SynthesizedAttributeTypeList *result
00569         = new SynthesizedAttributeTypeList(numberOfTraversals);
00570 
00571     typename TraversalPtrList::size_type i;
00572     for (i = 0; i < numberOfTraversals; i++)
00573         (*result)[i] = traversals[i]->defaultSynthesizedAttribute();
00574 
00575     return result;
00576 }
00577 
00578 template <class S>
00579 void
00580 AstCombinedBottomUpProcessing<S>::
00581 atTraversalStart()
00582 {
00583     // Compute some values that are constant during the traversal, saving
00584     // lots of method calls. We cannot reliably compute these earlier
00585     // because the user is allowed to mess with the traversal container
00586     // using the reference we provide.
00587     tBegin = traversals.begin();
00588     tEnd = traversals.end();
00589     numberOfTraversals = traversals.size();
00590 
00591     // Call this function for all traversals.
00592     typename TraversalPtrList::iterator t;
00593     for (t = tBegin; t != tEnd; ++t)
00594         (*t)->atTraversalStart();
00595 }
00596 
00597 template <class S>
00598 void
00599 AstCombinedBottomUpProcessing<S>::
00600 atTraversalEnd()
00601 {
00602     // Call this function for all traversals.
00603     typename TraversalPtrList::iterator t;
00604     for (t = tBegin; t != tEnd; ++t)
00605         (*t)->atTraversalEnd();
00606 }
00607 
00608 #endif

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