ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
functionNames.h
Go to the documentation of this file.
1 
2 #ifndef PARALLEL_COMPASS_FUNCTIONNAMES_H
3 #define PARALLEL_COMPASS_FUNCTIONNAMES_H
4 
6 
7 
8 #define DEBUG_OUTPUT_FUNCNAMES false
9 
10 // ----------------------------------------------------
11 // preTraversal
12 // ----------------------------------------------------
13 
14 // The pre-traversal runs before the distributed part of the analysis and is used to propagate context information down
15 // to the individual function definitions in the AST. Here, it just computes the depth of nodes in the AST.
17 {
18 protected:
19  int evaluateInheritedAttribute(SgNode *node, int depth)
20  {
21 #if DEBUG_OUTPUT_FUNCNAMES
22  std::cout << " eval inherited: node: " << node->class_name() << " depth:" << depth << std::endl;
23 #endif
24  return depth + 1;
25  }
26 };
27 
28 
29 // ----------------------------------------------------
30 // postTraversal
31 // ----------------------------------------------------
32 
33 // The post-traversal runs after the distributed part of the analysis and is used to collect the information it
34 // computed. Here, the synthesized attributes computed by the distributed analysis are strings representing information
35 // about functions. These strings are concatenated by the post-traversal (and interleaved with newlines where necessary).
37 {
38 protected:
40  {
41  std::string result = "";
43  for (s = synAttributes.begin(); s != synAttributes.end(); ++s)
44  {
45  std::string &str = *s;
46  result += str;
47  if (str.size() > 0 && str[str.size()-1] != '\n')
48  result += "\n";
49  }
50 #if DEBUG_OUTPUT_FUNCNAMES
51  std::cout << " eval synthesized: node: " << node->class_name() << " result:" << result << " ..............\n" <<std::endl;
52 #endif
53  return result;
54  }
55 
57  {
58  return "";
59  }
60 };
61 
62 
63 // ----------------------------------------------------
64 // FunctionNames
65 // ----------------------------------------------------
66 
67 // This is the distributed part of the analysis. The DistributedMemoryTraversal base class is a template taking an
68 // inherited and a synthesized attribute type as template parameters; these are the same types used by the pre- and
69 // post-traversals.
70 class FunctionNames: public DistributedMemoryTraversal<int, std::string>
71 {
72 protected:
73  // The analyzeSubtree() method is called for every defining function declaration in the AST. Its second argument is the
74  // inherited attribute computed for this node by the pre-traversal, the value it returns becomes the synthesized
75  // attribute used by the post-traversal.
76  std::string analyzeSubtree(SgFunctionDeclaration *funcDecl, int depth)
77  {
78  std::string funcName = funcDecl->get_name().str();
79  std::stringstream s;
80  s << "process " << myID() << ": at depth " << depth << ": function " << funcName;
81 #if DEBUG_OUTPUT_FUNCNAMES
82  std::cout << " analyzeSubtree of funcDecl: " << funcName << " id:" << myID()
83  << " result: " << s.str() << std::endl;
84 #endif
85  return s.str();
86  }
87 
88  // The user must implement this method to pack a synthesized attribute (a string in this case) into an array of bytes
89  // for communication. The first component of the pair is the number of bytes in the buffer.
90  std::pair<int, void *> serializeAttribute(std::string attribute) const
91  {
92  int len = attribute.size() + 1;
93  char *str = strdup(attribute.c_str());
94  return std::make_pair(len, str);
95  }
96 
97  // This method must be implemented to convert the serialized data to the application's synthesized attribute type.
98  std::string deserializeAttribute(std::pair<int, void *> serializedAttribute) const
99  {
100  return std::string((const char *) serializedAttribute.second);
101  }
102 
103  // This method is optional (the default implementation is empty). Its job is to free memory that may have been
104  // allocated by the serializeAttribute() method.
105  void deleteSerializedAttribute(std::pair<int, void *> serializedAttribute) const
106  {
107  std::free(serializedAttribute.second);
108  }
109 };
110 
111 #endif