ROSE  0.9.9.109
AstCombinedProcessingImpl.h
1 // Author: Gergo Barany
2 // $Id: AstCombinedProcessing.C,v 1.1 2008/01/08 02:56:38 dquinlan Exp $
3 
4 #ifndef ASTCOMBINEDPROCESSING_C
5 #define ASTCOMBINEDPROCESSING_C
6 
7 //#include "sage3.h"
8 
9 #include "AstCombinedProcessing.h"
10 
11 // Throughout this file, I is the InheritedAttributeType, S is the
12 // SynthesizedAttributeType -- the type names are still horrible
13 
14 // combined TREE TRAVERSAL implementation -- you will probably only want to
15 // use this to combine traversals of different types
16 template <class I, class S>
19  : traversals()
20 {
21 }
22 
23 template <class I, class S>
26  const typename SgCombinedTreeTraversal<I, S>::TraversalPtrList &t)
27  : traversals(t)
28 {
29 }
30 
31 template <class I, class S>
32 void
35 {
36  traversals.push_back(t);
37 }
38 
39 template <class I, class S>
40 typename SgCombinedTreeTraversal<I, S>::TraversalPtrList &
43 {
44  return traversals;
45 }
46 
47 template <class I, class S>
49  ::InheritedAttributeTypeList *
53  ::InheritedAttributeTypeList *inheritedValues)
54 {
55  InheritedAttributeTypeList *result
56  = new InheritedAttributeTypeList();
57  // Reserve just enough space for one inherited attribute per
58  // traversal, this keeps us from doing expensive resizing later.
59  result->reserve(numberOfTraversals);
60 
61  // Fill the list by evaluating the inherited attributes for each
62  // traversal.
63  typename TraversalPtrList::iterator t = tBegin;
64  typename InheritedAttributeTypeList::iterator i
65  = inheritedValues->begin();
66  typename InheritedAttributeTypeList::const_iterator iEnd
67  = inheritedValues->end();
68 
69  while (t != tEnd && i != iEnd)
70  result->push_back((*t++)->evaluateInheritedAttribute(astNode, *i++));
71  ROSE_ASSERT(t == tEnd && i == iEnd);
72  ROSE_ASSERT(result->size() == numberOfTraversals);
73 
74  // The inherited attribute list allocated here should be deleted in the
75  // evaluateSynthesizedAttribute() function.
76  return result;
77 }
78 
79 template <class I, class S>
81  ::SynthesizedAttributeTypeList *
85  ::InheritedAttributeTypeList *inheritedValues,
87  ::SynthesizedAttributesList synthesizedAttributes)
88 {
89  // Let N = number of traversals, M = number of node successors.
90  // synthesizedAttributes is a list of M lists of N attributes each; we
91  // want to call each of the N traversals with the corresponding M
92  // synthesized attributes.
93  // This is one of the obscure functions.
94 
95  typename SynthesizedAttributesList::size_type M
96  = synthesizedAttributes.size();
97  // Create a container of size M in which we will store the synthesized
98  // attributes for each traversal.
99  typename TraversalType::SynthesizedAttributesList
100  attributesForTraversal(M);
101  // Create a list for the traversal results.
102  SynthesizedAttributeTypeList *result
103  = new SynthesizedAttributeTypeList();
104  result->reserve(numberOfTraversals);
105 
106  typename TraversalPtrList::size_type i;
107  typename SynthesizedAttributesList::size_type j;
108 
109  for (i = 0; i < numberOfTraversals; i++)
110  {
111  for (j = 0; j < M; j++)
112  attributesForTraversal[j] = (*synthesizedAttributes[j])[i];
113 
114  result->push_back(
115  traversals[i]->evaluateSynthesizedAttribute(
116  astNode,
117  (*inheritedValues)[i],
118  attributesForTraversal));
119  }
120 
121  // The lists of synthesized attributes passed to us are not needed
122  // anymore, free them.
123  for (j = 0; j < M; j++)
124  delete synthesizedAttributes[j];
125 
126  // inheritedValues is a pointer to a container that is dynamically
127  // allocated in evaluateInheritedAttribute(). Now that all successor
128  // nodes have been visited, we can free the allocated memory.
129  delete inheritedValues;
130 
131  return result;
132 }
133 
134 template <class I, class S>
136  ::SynthesizedAttributeTypeList *
139  ::InheritedAttributeTypeList *inheritedValues)
140 {
141  // Create a list for the default attributes.
142  SynthesizedAttributeTypeList *result
143  = new SynthesizedAttributeTypeList(numberOfTraversals);
144 
145 #if 1
146  typename TraversalPtrList::size_type i;
147  for (i = 0; i < numberOfTraversals; i++)
148  (*result)[i]
149  = traversals[i]->defaultSynthesizedAttribute((*inheritedValues)[i]);
150 #else
151  typename TraversalPtrList::iterator i = traversals.begin();
152  typename SynthesizedAttributeTypeList::iterator j
153  = result->begin();
154  typename InheritedAttributeTypPtrList::iterator k
155  = inheritedValues->begin();
156  while (i != tEnd)
157  *j++ = (*i++)->defaultSynthesizedAttribute(*k++);
158 #endif
159 
160  return result;
161 }
162 
163 template <class I, class S>
164 void
167 {
168  // Compute some values that are constant during the traversal, saving
169  // lots of method calls. We cannot reliably compute these earlier
170  // because the user is allowed to mess with the traversal container
171  // using the reference we provide.
172  tBegin = traversals.begin();
173  tEnd = traversals.end();
174  numberOfTraversals = traversals.size();
175 
176  // Call this function for all traversals.
177  typename TraversalPtrList::iterator t;
178  for (t = tBegin; t != tEnd; ++t)
179  (*t)->atTraversalStart();
180 }
181 
182 template <class I, class S>
183 void
186 {
187  // Call this function for all traversals.
188  typename TraversalPtrList::iterator t;
189  for (t = tBegin; t != tEnd; ++t)
190  (*t)->atTraversalEnd();
191 }
192 
193 // combined TOP DOWN BOTTOM UP implementation
194 
195 template <class I, class S>
198  : traversals()
199 {
200 }
201 
202 template <class I, class S>
205  const typename AstCombinedTopDownBottomUpProcessing<I, S>::TraversalPtrList &t)
206  : traversals(t)
207 {
208 }
209 
210 template <class I, class S>
211 void
214 {
215  traversals.push_back(t);
216 }
217 
218 template <class I, class S>
219 typename AstCombinedTopDownBottomUpProcessing<I, S>::TraversalPtrList &
222 {
223  return traversals;
224 }
225 
226 template <class I, class S>
228  ::InheritedAttributeTypeList *
232  ::InheritedAttributeTypeList *inheritedValues)
233 {
234  InheritedAttributeTypeList *result
235  = new InheritedAttributeTypeList();
236  // Reserve just enough space for one inherited attribute per
237  // traversal, this keeps us from doing expensive resizing later.
238  result->reserve(numberOfTraversals);
239 
240  // Fill the list by evaluating the inherited attributes for each
241  // traversal.
242  typename TraversalPtrList::iterator t = tBegin;
243  typename InheritedAttributeTypeList::iterator i
244  = inheritedValues->begin();
245  typename InheritedAttributeTypeList::const_iterator iEnd
246  = inheritedValues->end();
247 
248  while (t != tEnd && i != iEnd)
249  result->push_back((*t++)->evaluateInheritedAttribute(astNode, *i++));
250  ROSE_ASSERT(t == tEnd && i == iEnd);
251  ROSE_ASSERT(result->size() == numberOfTraversals);
252 
253  // The inherited attribute list allocated here should be deleted in the
254  // evaluateSynthesizedAttribute() function.
255  return result;
256 }
257 
258 template <class I, class S>
260  ::SynthesizedAttributeTypeList *
264  ::InheritedAttributeTypeList *inheritedValues,
266  ::SynthesizedAttributesList synthesizedAttributes)
267 {
268  // Let N = number of traversals, M = number of node successors.
269  // synthesizedAttributes is a list of M lists of N attributes each; we
270  // want to call each of the N traversals with the corresponding M
271  // synthesized attributes.
272  // This is one of the obscure functions.
273 
274  typename SynthesizedAttributesList::size_type M
275  = synthesizedAttributes.size();
276  // Create a container of size M in which we will store the synthesized
277  // attributes for each traversal.
278  typename TraversalType::SynthesizedAttributesList
279  attributesForTraversal(M);
280  // Create a list for the traversal results.
281  SynthesizedAttributeTypeList *result
282  = new SynthesizedAttributeTypeList();
283  result->reserve(numberOfTraversals);
284 
285  typename TraversalPtrList::size_type i;
286  typename SynthesizedAttributesList::size_type j;
287 
288  for (i = 0; i < numberOfTraversals; i++)
289  {
290  for (j = 0; j < M; j++)
291  attributesForTraversal[j] = (*synthesizedAttributes[j])[i];
292 
293  result->push_back(
294  traversals[i]->evaluateSynthesizedAttribute(
295  astNode,
296  (*inheritedValues)[i],
297  attributesForTraversal));
298  }
299 
300  // The lists of synthesized attributes passed to us are not needed
301  // anymore, free them.
302  for (j = 0; j < M; j++)
303  delete synthesizedAttributes[j];
304 
305  // inheritedValues is a pointer to a container that is dynamically
306  // allocated in evaluateInheritedAttribute(). Now that all successor
307  // nodes have been visited, we can free the allocated memory.
308  delete inheritedValues;
309 
310  return result;
311 }
312 
313 template <class I, class S>
315  ::SynthesizedAttributeTypeList *
318  ::InheritedAttributeTypeList *inheritedValues)
319 {
320  // Create a list for the default attributes.
321  SynthesizedAttributeTypeList *result
322  = new SynthesizedAttributeTypeList(numberOfTraversals);
323 
324  typename TraversalPtrList::size_type i;
325  for (i = 0; i < numberOfTraversals; i++)
326  (*result)[i]
327  = traversals[i]->defaultSynthesizedAttribute((*inheritedValues)[i]);
328 
329  return result;
330 }
331 
332 template <class I, class S>
333 void
336 {
337  // Compute some values that are constant during the traversal, saving
338  // lots of method calls. We cannot reliably compute these earlier
339  // because the user is allowed to mess with the traversal container
340  // using the reference we provide.
341  tBegin = traversals.begin();
342  tEnd = traversals.end();
343  numberOfTraversals = traversals.size();
344 
345  // Call this function for all traversals.
346  typename TraversalPtrList::iterator t;
347  for (t = tBegin; t != tEnd; ++t)
348  (*t)->atTraversalStart();
349 }
350 
351 template <class I, class S>
352 void
355 {
356  // Call this function for all traversals.
357  typename TraversalPtrList::iterator t;
358  for (t = tBegin; t != tEnd; ++t)
359  (*t)->atTraversalEnd();
360 }
361 
362 
363 // combined TOP DOWN implementation
364 
365 template <class I>
368  : traversals()
369 {
370 }
371 
372 template <class I>
375  const typename AstCombinedTopDownProcessing<I>::TraversalPtrList &t)
376  : traversals(t)
377 {
378 }
379 
380 template <class I>
381 void
384 {
385  traversals.push_back(t);
386 }
387 
388 template <class I>
389 typename AstCombinedTopDownProcessing<I>::TraversalPtrList &
392 {
393  return traversals;
394 }
395 
396 template <class I>
398  ::InheritedAttributeTypeList *
402  ::InheritedAttributeTypeList *inheritedValues)
403 {
404  InheritedAttributeTypeList *result
405  = new InheritedAttributeTypeList();
406  // Reserve just enough space for one inherited attribute per
407  // traversal, this keeps us from doing expensive resizing later.
408  result->reserve(numberOfTraversals);
409 
410  // Fill the list by evaluating the inherited attributes for each
411  // traversal.
412  typename TraversalPtrList::iterator t = tBegin;
413  typename InheritedAttributeTypeList::iterator i
414  = inheritedValues->begin();
415  typename InheritedAttributeTypeList::const_iterator iEnd
416  = inheritedValues->end();
417 
418  while (t != tEnd && i != iEnd)
419  result->push_back((*t++)->evaluateInheritedAttribute(astNode, *i++));
420  ROSE_ASSERT(t == tEnd && i == iEnd);
421  ROSE_ASSERT(result->size() == numberOfTraversals);
422 
423  // The list of inherited attributes allocated here should be freed in
424  // the destroyInheritedAttribute() function.
425  return result;
426 }
427 
428 template <class I>
429 void
432 {
433  // Compute some values that are constant during the traversal, saving
434  // lots of method calls. We cannot reliably compute these earlier
435  // because the user is allowed to mess with the traversal container
436  // using the reference we provide.
437  tBegin = traversals.begin();
438  tEnd = traversals.end();
439  numberOfTraversals = traversals.size();
440 
441  // Call this function for all traversals.
442  typename TraversalPtrList::iterator t;
443  for (t = tBegin; t != tEnd; ++t)
444  (*t)->atTraversalStart();
445 }
446 
447 template <class I>
448 void
451 {
452  // Call this function for all traversals.
453  typename TraversalPtrList::iterator t;
454  for (t = tBegin; t != tEnd; ++t)
455  (*t)->atTraversalEnd();
456 }
457 
458 template <class I>
459 void
462  InheritedAttributeTypeList *inheritedValues)
463 {
464  typename TraversalPtrList::iterator t = tBegin;
465  typename InheritedAttributeTypeList::iterator i
466  = inheritedValues->begin();
467  typename InheritedAttributeTypeList::const_iterator iEnd
468  = inheritedValues->end();
469 
470  // Call this function for all traversals.
471  while (t != tEnd && i != iEnd)
472  (*t++)->destroyInheritedValue(node, *i++);
473  ROSE_ASSERT(t == tEnd && i == iEnd);
474 
475  // inheritedValues is a pointer to a container that is dynamically
476  // allocated in evaluateInheritedAttribute(). Now that all successor
477  // nodes have been visited, we can free the allocated memory.
478  delete inheritedValues;
479 }
480 
481 // combined BOTTOM UP implementation
482 
483 template <class S>
486  : traversals()
487 {
488 }
489 
490 template <class S>
493  const typename AstCombinedBottomUpProcessing<S>::TraversalPtrList &t)
494  : traversals(t)
495 {
496 }
497 
498 template <class S>
499 void
502 {
503  traversals.push_back(t);
504 }
505 
506 template <class S>
507 typename AstCombinedBottomUpProcessing<S>::TraversalPtrList &
510 {
511  return traversals;
512 }
513 
514 template <class S>
516  ::SynthesizedAttributeTypeList *
520  ::SynthesizedAttributesList synthesizedAttributes)
521 {
522  // Let N = number of traversals, M = number of node successors.
523  // synthesizedAttributes is a list of M lists of N attributes each; we
524  // want to call each of the N traversals with the corresponding M
525  // synthesized attributes.
526  // This is one of the obscure functions.
527 
528  typename SynthesizedAttributesList::size_type M
529  = synthesizedAttributes.size();
530  // Create a container of size M in which we will store the synthesized
531  // attributes for each traversal.
532  typename TraversalType::SynthesizedAttributesList
533  attributesForTraversal(M, NULL);
534  // Create a list for the traversal results.
535  SynthesizedAttributeTypeList *result
536  = new SynthesizedAttributeTypeList(numberOfTraversals);
537 
538  typename TraversalPtrList::size_type i;
539  typename SynthesizedAttributesList::size_type j;
540 
541  for (i = 0; i < numberOfTraversals; i++)
542  {
543  for (j = 0; j < M; j++)
544  attributesForTraversal[j] = (*synthesizedAttributes[j])[i];
545 
546  (*result)[i]
547  = traversals[i]->evaluateSynthesizedAttribute(
548  astNode,
549  attributesForTraversal);
550  }
551  ROSE_ASSERT(result->size() == numberOfTraversals);
552 
553  // The lists of synthesized attributes passed to us are not needed
554  // anymore, free them.
555  for (j = 0; j < M; j++)
556  delete synthesizedAttributes[j];
557 
558  return result;
559 }
560 
561 template <class S>
563  ::SynthesizedAttributeTypeList *
566 {
567  // Create a list for the default attributes.
568  SynthesizedAttributeTypeList *result
569  = new SynthesizedAttributeTypeList(numberOfTraversals);
570 
571  typename TraversalPtrList::size_type i;
572  for (i = 0; i < numberOfTraversals; i++)
573  (*result)[i] = traversals[i]->defaultSynthesizedAttribute();
574 
575  return result;
576 }
577 
578 template <class S>
579 void
582 {
583  // Compute some values that are constant during the traversal, saving
584  // lots of method calls. We cannot reliably compute these earlier
585  // because the user is allowed to mess with the traversal container
586  // using the reference we provide.
587  tBegin = traversals.begin();
588  tEnd = traversals.end();
589  numberOfTraversals = traversals.size();
590 
591  // Call this function for all traversals.
592  typename TraversalPtrList::iterator t;
593  for (t = tBegin; t != tEnd; ++t)
594  (*t)->atTraversalStart();
595 }
596 
597 template <class S>
598 void
601 {
602  // Call this function for all traversals.
603  typename TraversalPtrList::iterator t;
604  for (t = tBegin; t != tEnd; ++t)
605  (*t)->atTraversalEnd();
606 }
607 
608 #endif
This class is temporary. Do not use.
Definition: AstProcessing.h:97
Attribute Evaluator for synthesized attributes.
void addTraversal(TraversalPtr)
simple function for adding a traversal to the internal list
void addTraversal(TraversalPtr)
simple function for adding a traversal to the internal list
virtual SynthesizedAttributeTypeList * defaultSynthesizedAttribute()
Allows to provide a default value for a synthesized attribute of primitive type (e.g.
virtual void atTraversalStart()
Function called at the start of the traversal, before any node is visited; override if necessary...
Attribute Evaluator for inherited attributes.
TraversalPtrList & get_traversalPtrListRef()
function for obtaining a reference to the internal list of traversals, you can use this for any conta...
virtual void atTraversalStart()
Function called at the start of the traversal, before any node is visited; override if necessary...
TraversalPtrList & get_traversalPtrListRef()
function for obtaining a reference to the internal list of traversals, you can use this for any conta...
AstCombinedTopDownBottomUpProcessing()
default constructor
virtual InheritedAttributeTypeList * evaluateInheritedAttribute(SgNode *astNode, InheritedAttributeTypeList *inheritedValues)
pure virtual function which must be implemented to compute the inherited attribute at a node ...
void addTraversal(TraversalPtr)
simple function for adding a traversal to the internal list
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:8322
void addTraversal(TraversalPtr)
simple function for adding a traversal to the internal list
AstCombinedTopDownProcessing()
default constructor
AstCombinedBottomUpProcessing()
default constructor
virtual void atTraversalStart()
Function called at the start of the traversal, before any node is visited; override if necessary...
TraversalPtrList & get_traversalPtrListRef()
function for obtaining a reference to the internal list of traversals, you can use this for any conta...
SgCombinedTreeTraversal()
default constructor
TraversalPtrList & get_traversalPtrListRef()
function for obtaining a reference to the internal list of traversals, zou can use this for any conta...
Attribute Evaluator for inherited and synthesized attributes.
virtual InheritedAttributeTypeList * evaluateInheritedAttribute(SgNode *astNode, InheritedAttributeTypeList *inheritedValues)
pure virtual function which must be implemented to compute the inherited attribute at a node ...