forked from rose-compiler/rose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritedAndSynthesizedAttributeTraversal.C
91 lines (78 loc) · 2.71 KB
/
inheritedAndSynthesizedAttributeTraversal.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// ROSE is a tool for building preprocessors, this file is an example preprocessor built with ROSE.
#include "rose.h"
#include <algorithm>
#include <functional>
#include <numeric>
typedef bool InheritedAttribute;
typedef bool SynthesizedAttribute;
class Traversal : public SgTopDownBottomUpProcessing<InheritedAttribute,SynthesizedAttribute>
{
public:
// Functions required
InheritedAttribute evaluateInheritedAttribute (
SgNode* astNode,
InheritedAttribute inheritedAttribute );
SynthesizedAttribute evaluateSynthesizedAttribute (
SgNode* astNode,
InheritedAttribute inheritedAttribute,
SubTreeSynthesizedAttributes synthesizedAttributeList );
};
InheritedAttribute
Traversal::evaluateInheritedAttribute (
SgNode* astNode,
InheritedAttribute inheritedAttribute )
{
if (isSgFunctionDefinition(astNode))
{
// The inherited attribute is true iff we are inside a function.
return true;
}
return inheritedAttribute;
}
SynthesizedAttribute
Traversal::evaluateSynthesizedAttribute (
SgNode* astNode,
InheritedAttribute inheritedAttribute,
SynthesizedAttributesList childAttributes )
{
if (inheritedAttribute == false)
{
// The inherited attribute is false, i.e. we are not inside any
// function, so there can be no loops here.
return false;
}
else
{
// Fold up the list of child attributes using logical or, i.e. the local
// result will be true iff one of the child attributes is true.
SynthesizedAttribute localResult =
std::accumulate(childAttributes.begin(), childAttributes.end(),
false, std::logical_or<bool>());
if (isSgFunctionDefinition(astNode) && localResult == true)
{
printf ("Found a function containing a for loop ...\n");
}
if (isSgForStatement(astNode))
{
localResult = true;
}
return localResult;
}
}
int
main ( int argc, char* argv[] )
{
// Initialize and check compatibility. See Rose::initialize
ROSE_INITIALIZE;
// Build the abstract syntax tree
SgProject* project = frontend(argc,argv);
ROSE_ASSERT (project != NULL);
// Build the inherited attribute
InheritedAttribute inheritedAttribute = false;
// Define the traversal
Traversal myTraversal;
// Call the traversal starting at the project (root) node of the AST
myTraversal.traverseInputFiles(project,inheritedAttribute);
// This program only does analysis, so it need not call the backend to generate code.
return 0;
}