ROSE  0.9.11.42
atermSupport.h
1 
2 // Put non-generated Aterm support code here.
3 
4 // #include "rosePublicConfig.h"
5 
6 // #include "aterm1.h"
7 // #include "aterm2.h"
8 
9 #define LAZY_WRAPPING_MACRO false
10 
11 namespace AtermSupport
12  {
13  // DQ (10/5/2014): This is the part of the latest version of ATerm support (now generated by ROSETTA).
14 #if 0
15  class AtermToNodeConverter
16  {
17  public:
18  // std::map<std::string, std::list<LinkUpdater*> > idUpdates;
19 
20  // map<string, SgNode*> targetLocations;
21  std::map<std::string, SgScopeStatement*> translationScopeMap;
22  std::map<std::string, SgType*> translationTypeMap;
23  std::map<std::string, SgDeclarationStatement*> translationDeclarationMap;
24  std::map<std::string, SgInitializedName*> translationInitializedNameMap;
25 
26  void updateScopeMap (std::string key, SgScopeStatement* scope);
27  SgScopeStatement* lookupScope (std::string key);
28 
29  void updateDeclarationMap (std::string key, SgDeclarationStatement* scope);
30  SgDeclarationStatement* lookupDeclaration (std::string key);
31 
32  void updateTypeMap (std::string key, SgType* scope);
33  SgType* lookupType (std::string key);
34 
35  void updateInitializedNameMap (std::string key, SgInitializedName* scope);
36  SgInitializedName* lookupInitializedName (std::string key);
37 
38  SgNode* convertAtermToNode(ATerm term);
39 
40  private:
41  template <class TranslationMap, class TranslationMapElement>
42  void updateMap_support (std::string key, TranslationMap & translationMap, TranslationMapElement* node);
43 
44  template <class TranslationMap, class TranslationMapElement>
45  TranslationMapElement* lookupMap_support (std::string key, TranslationMap & tranlationMap);
46  };
47 #else
48  extern std::map<std::string, SgNode*> translationNodeMap;
49 
50  extern std::map<std::string, SgScopeStatement*> translationScopeMap;
51  extern std::map<std::string, SgType*> translationTypeMap;
52  extern std::map<std::string, SgDeclarationStatement*> translationDeclarationMap;
53  extern std::map<std::string, SgInitializedName*> translationInitializedNameMap;
54 #endif
55 
56  void initializeTypeSystem();
57 
58  std::string aterm_type_name( ATerm term );
59 
60  std::string uniqueId(SgNode* n);
61 
62  // This supports generation of strings from addresses (mostly so that the SgAsm support will compile).
63  // I don't think that Aterms have a data type for unsigned 64-bit integers.
64  std::string uniqueId(uint64_t n);
65 
66  // This supports generation of strings from rose_rva_t (so that the SgAsm support will compile).
67  // I don't think it is clear how to implement this function (but we can worry about the binary analysis
68  // use of Aterms later).
69  std::string uniqueId(const rose_rva_t & n);
70 
71  // This supports generation of strings from registers (so that the SgAsm support will compile).
72  // I don't think it is clear how to implement this function (but we can worry about the binary analysis
73  // use of Aterms later).
74  std::string uniqueId(Rose::BinaryAnalysis::RegisterDescriptor);
75 
76  ATerm convertFileInfoToAterm(Sg_File_Info* fi);
77  Sg_File_Info* getAtermFileInfo(ATerm term, std::string annotationName);
78 
79  ATerm convertVectorToAterm(const std::vector<ATerm>& v, int start, int len);
80 
81  template <typename Iter>
82  ATerm convertRangeToAterm(Iter b, Iter e);
83 
84  template <typename Iter>
85  ATerm convertSgNodeRangeToAterm(Iter b, Iter e);
86 
87  std::string getShortVariantName(VariantT var);
88 
89  ATerm getTraversalChildrenAsAterm(SgNode* n);
90 
91  ATerm convertNodeToAterm(SgNode* n);
92 
93  ATerm getAtermAnnotation(ATerm term, char* annotation_name );
94 
95  // Function for reading aterms and converting them to ROSE AST IR nodes.
96  SgNode* generate_AST(ATerm & term);
97 
98  // Generate a list of aterms from the input aterm.
99  std::vector<ATerm> getAtermList(ATerm ls);
100 
101  std::string getAtermStringAttribute(ATerm term, const std::string & annotationName );
102  int getAtermIntegerAttribute(ATerm term, const std::string & annotationName );
103 
104  // DQ (10/8/2014): I don't think we need these (Aterm type system is not rich enough to justify these).
105  // int getAtermEnumAttribute(ATerm term, const std::string & annotationName );
106  // std::string getAtermNodeNameAttribute(ATerm term, const std::string & annotationName );
107 
108  // This function is special in that it accesses (internal) maps to lookup the SgNode
109  // using a sting that is saved as an annotation in the aterm and then as a key into the map.
110  // Note that this should work where we see declarations before they are referenced, but
111  // will likely have to be fixed up after the translation where this rule is relaxed in C++
112  // classes (which may refer to constructs before they are declared (and is always an issue)).
113  SgNode* getAtermNodeAttribute(ATerm term, const std::string & annotationName );
114 
115  SgType* getAtermTypeNodeAttribute (ATerm term, const std::string & annotationName );
116  SgScopeStatement* getAtermScopeNodeAttribute (ATerm term, const std::string & annotationName );
117 
118  // Not as clear how to implement this since it is difficult to inspect the class hierarhy in ROSETTA.
119  SgDeclarationStatement* getAtermDeclarationNodeAttribute (ATerm term, const std::string & annotationName );
120 
121  }
122 
123 
124 #ifdef ROSE_USE_ROSE_ATERM_SUPPORT
125 
126 template <typename Iter>
127 ATerm
128 AtermSupport::convertRangeToAterm(Iter b, Iter e)
129  {
130  ATermList ls = ATmakeList0();
131  for (; b != e; ++b)
132  {
133  ls = ATappend(ls, *b);
134  }
135 
136  return ATmake("<term>", ls);
137  }
138 
139 template <typename Iter>
140 ATerm AtermSupport::convertSgNodeRangeToAterm(Iter b, Iter e)
141  {
142  bool lazyWrapping = LAZY_WRAPPING_MACRO;
143 
144  ROSE_ASSERT(lazyWrapping == false);
145 
146  if ( (b != e) && (*b != NULL) )
147  {
148 #if 0
149  SgNode* parent = (*b)->get_parent();
150  if (parent == NULL)
151  {
152  printf ("warning: parent == NULL: *b = %p = %s \n",*b,(*b)->class_name().c_str());
153  }
154 #endif
155  // ROSE_ASSERT(parent != NULL);
156 #if 0
157  printf ("In AtermSupport::convertSgNodeRangeToAterm(): Building an ATerm list for (*b)->get_parent() = %p = %s \n",parent,(parent != NULL) ? parent->class_name().c_str() : "NULL");
158 #endif
159  }
160  else
161  {
162 #if 0
163  printf ("In AtermSupport::convertSgNodeRangeToAterm(): Building an empty list \n");
164 #endif
165  }
166 
167  ATermList ls = ATmakeList0();
168  for (; b != e; ++b)
169  {
170  ATerm child_term = NULL;
171  if (lazyWrapping == true)
172  {
173  child_term = ATmake("lazyWrap(<str>)","lazyWrap");
174  }
175  else
176  {
177 #if 0
178  printf ("In AtermSupport::convertSgNodeRangeToAterm(): Calling convertNodeToAterm() *b = %p = %s\n",*b,(*b != NULL) ? (*b)->class_name().c_str() : "NULL");
179 #endif
180  child_term = convertNodeToAterm(*b);
181 
182 #if 0
183  // I think we are generating invalid aterms in some cases.
184  int atermKind = ATgetType(child_term);
185  printf ("In AtermSupport::convertSgNodeRangeToAterm(): child_term: atermKind = %d = %s \n",atermKind,aterm_type_name(child_term).c_str());
186 #endif
187 #if 0
188  printf ("In AtermSupport::convertSgNodeRangeToAterm(): DONE: Calling convertNodeToAterm() *b = %p = %s \n",*b,(*b != NULL) ? (*b)->class_name().c_str() : "NULL");
189 #endif
190 #if 0
191  cout << "In AtermSupport::convertSgNodeRangeToAterm(): DONE: output child_term: -> " << ATwriteToString(child_term) << endl;
192 #endif
193  }
194 
195 #if 0
196  printf ("In AtermSupport::convertSgNodeRangeToAterm(): Calling ATappend() \n");
197 #endif
198  // ls = ATappend(ls,convertNodeToAterm(*b));
199  ls = ATappend(ls,child_term);
200 
201 #if 0
202  // I think we are generating invalid aterms in some cases.
203  int atermKind = ATgetType(ls);
204  // printf ("In AtermSupport::convertSgNodeRangeToAterm(): ATappend(): atermKind = %d = %s \n",atermKind,aterm_type_name(ls).c_str());
205  printf ("In AtermSupport::convertSgNodeRangeToAterm(): ATappend(): atermKind = %d \n",atermKind);
206 #endif
207 #if 0
208  printf ("In AtermSupport::convertSgNodeRangeToAterm(): DONE: Calling ATappend() \n");
209 #endif
210  }
211 
212 #if 0
213  printf ("In AtermSupport::convertSgNodeRangeToAterm(): building aterm to return \n");
214 #endif
215 
216  // return ATmake("<term>", ls);
217  ATerm returnTerm = ATmake("<term>", ls);
218 
219 #if 0
220  // I think we are generating invalid aterms in some cases.
221  int atermKind = ATgetType(returnTerm);
222  printf ("In AtermSupport::convertSgNodeRangeToAterm(): atermKind = %d = %s \n",atermKind,aterm_type_name(returnTerm).c_str());
223 #endif
224 #if 0
225  printf ("In AtermSupport::convertSgNodeRangeToAterm(): returnTerm = %p ls = %p \n",returnTerm,ls);
226 #endif
227 #if 0
228  cout << "AtermSupport::convertSgNodeRangeToAterm(): returning the aterm -> " << ATwriteToString(returnTerm) << endl;
229 #endif
230 
231  return returnTerm;
232  }
233 
234 // endif for ROSE_USE_ROSE_ATERM_SUPPORT
235 #endif
236 
This class represents the concept of a scope in C++ (e.g. global scope, fuction scope, etc.).
This class represents the base class for all types.
This class represents the location of the code associated with the IR node in the original source cod...
This class represents the notion of a declared variable.
Describes (part of) a physical CPU register.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:8908
A relative virtual address optionally associated with a SgAsmSection.
Definition: Cxx_Grammar.h:7388
virtual std::string class_name() const
returns a string representing the class name
SgNode * get_parent() const
Access function for parent node.
This class represents the concept of a declaration statement.