ROSE  0.11.96.11
abstract_handle.h
1 #ifndef abstract_handle_INCLUDED
2 #define abstract_handle_INCLUDED
3 
4 #include <string>
5 #include <utility>
6 #include <limits.h>
7 #include <map>
8 #include "rosedll.h"
9 
10 #ifdef _MSC_VER
11 // DQ (11/26/2009): Required for definition of PATH_MAX in MSVC.
12 #include "rose_msvc.h"
13 #endif
14 
15 // DQ (4/17/2016): This appears to be needed for ROSE to emulate Clang
16 // in compiling ROSE files in tests/nonsmoke/functional/CompileTests/RoseExample_tests.
17 #ifdef __clang__
18 // Find value for PATH_MAX
19 // Pei-hung (06/22/2016): OSX has limits.h in /usr/include
20 #if defined(__APPLE__) && defined(__MACH__)
21 #include<limits.h>
22 #else
23 #include<linux/limits.h>
24 #endif
25 #endif
26 
56 namespace AbstractHandle{
57 
60  {
61  size_t line;
62  size_t column;
63  };
64 
66  {
67  source_position first;
68  source_position second;
69  };
70 
71  std::string toString(const source_position &);
72  std::string toString(const source_position_pair &);
73  bool isEqual(const source_position &pos1, const source_position &pos2);
74  bool isEqual(const source_position_pair &pair1, const source_position_pair &pair2);
75 
77  void fromString(source_position&, const std::string& input);
79  void fromString(source_position_pair&, const std::string& input);
80 
81  //----------specifiers------------------------------------
83  typedef enum {e_name, e_position, e_numbering, e_int_label, e_str_label}
85 
86  typedef union u_specifier_value
87  {
88  char str_v[PATH_MAX+256];
89  size_t int_v;
90  source_position_pair positions;
92 
96  class specifier
97  {
98  public:
99  specifier(){};
100  specifier (specifier_type_t t, specifier_value_t v):type(t),value(v){}
101  specifier_type_t get_type(){return type;}
102  specifier_value_t get_value(){return value;}
103  void set_type(specifier_type_t stype) { type=stype;}
104  void set_value(specifier_value_t svalue) {value=svalue;}
105  std::string toString();
106  private:
107  specifier_type_t type;
108  specifier_value_t value ;
109  };
110 
111  // Parse a string into a specifier
112  void fromString(specifier&, const std::string& input);
113 
114  //----------------Abstract AST/IR node---------------
120  {
121  public:
122  virtual ~abstract_node(){}
123 
125  virtual std::string getConstructTypeName() const;
126 
128  virtual bool hasSourcePos() const;
129 
131  virtual bool hasName() const;
132 
134  virtual std::string getName() const;
135 
138  virtual abstract_node* getFileNode() const;
139 
141  virtual abstract_node* getParent() const;
142 
143 #ifdef _MSC_VER
144 // DQ (11/27/2009): MSVC does not appear to support covariant types, but no message is required.
145  #pragma message ("WARNING: MSVC does not handle covariant return types properly.")
146 #else
147  virtual void * getNode() const {return NULL;};
149 #endif
150 
153  virtual abstract_node* findNode(std::string handle_str) const;
154 
156  virtual abstract_node* findNode(std::string construct_type_str, specifier mspecifier) const;
157 
158  virtual std::string getFileName() const;
159 
160  virtual source_position_pair getSourcePos() const;
161  virtual source_position getStartPos() const;
162  virtual source_position getEndPos() const;
163 
164  virtual std::string toString() const;
165 
167  virtual size_t getIntLabel() const;
169  virtual std::string getStringLabel() const;
170 
174  virtual size_t getNumbering( const abstract_node* another_node) const;
175 
176  virtual bool operator == (const abstract_node & x) const=0;
177  }; // end class abstract_node
178 
179  //---------------- An abstract handle --------------------------
182  class ROSE_DLL_API abstract_handle
183  {
184  public:
185 
187  abstract_handle();
191 
197  specifier_value_t svalue, abstract_handle* p_handle=NULL);
198 
204  abstract_handle* p_handle=NULL);
205 
208  abstract_handle(abstract_handle* phandle, const std::string& handle_string);
209 
210  virtual ~abstract_handle() { if (m_specifier !=NULL) delete m_specifier;}
211 
213  virtual std::string get_construct_type_name();
214 
216  virtual std::string toString();
217 
219  virtual bool fromString(abstract_handle* ancester_handle, const std::string &handle_str_multiple);
220 
222  virtual std::string toStringSelf();
223 
225  virtual void fromStringSelf(abstract_handle* p_handle, const std::string& handle_str_item);
226 
227  virtual abstract_node* getNode() const {return m_node;}
229  specifier* get_specifier(){return m_specifier;}
230 
231  abstract_handle* get_parent_handle(){return parent_handle; }
232  void set_parent_handle(abstract_handle* parent){ parent_handle= parent;}
233 
235  //Any other way to implement this??
236  bool operator == (const abstract_handle& x) const
237  { return m_node == x.getNode();}
238 
239  private:
241  void init();
242  abstract_handle * parent_handle;
243  // shared handles cannot have a single next handle
244  // abstract_handle * next_handle; // expose them or not?
245 
247  abstract_node* m_node;
248  specifier * m_specifier;
249  abstract_handle (const abstract_handle &); // disallow copy
250  abstract_handle & operator = (const abstract_handle &);
251 
252  };
253 
257  extern std::map<abstract_node*, abstract_handle*> handle_map;
258 
259 }// end namespace
260 
261 #endif
AbstractHandle::abstract_node::getName
virtual std::string getName() const
Get the name of the construct if it is named, like function name.
AbstractHandle::abstract_node::hasSourcePos
virtual bool hasSourcePos() const
If the node has meaningful line and column numbers associated with a file.
AbstractHandle::abstract_node::getConstructTypeName
virtual std::string getConstructTypeName() const
Get the construct' s type name, like function.
AbstractHandle::abstract_node::hasName
virtual bool hasName() const
If the node has legal names defined by language standards.
AbstractHandle::abstract_node::getParent
virtual abstract_node * getParent() const
Get parent node, used for generate parent handle automatically.
AbstractHandle::specifier_type_t
specifier_type_t
specifier type and values
Definition: abstract_handle.h:83
AbstractHandle::abstract_node::getIntLabel
virtual size_t getIntLabel() const
Get integer label.
AbstractHandle::abstract_node::getStringLabel
virtual std::string getStringLabel() const
Get string label.
AbstractHandle::source_position_pair
Definition: abstract_handle.h:65
AbstractHandle::abstract_node::getNode
virtual void * getNode() const
Get the raw IR node associated with the current abstract node.
Definition: abstract_handle.h:148
AbstractHandle::abstract_node
Users should provide a concrete node implementation especially a constructor/builder to avoid duplica...
Definition: abstract_handle.h:119
AbstractHandle::abstract_handle
to specify a construct using a specifier Can be used alone or with parent handles when relative speci...
Definition: abstract_handle.h:182
AbstractHandle::handle_map
std::map< abstract_node *, abstract_handle * > handle_map
maintain a map between nodes and handles, used for fast indexing and avoid redundant creation of hand...
AbstractHandle::specifier
construct specifier could be used to specify a construct by name, position, numbering,...
Definition: abstract_handle.h:96
AbstractHandle::abstract_node::getFileNode
virtual abstract_node * getFileNode() const
Get the start source file position of the construct Get the abstract node for file containing the cur...
AbstractHandle::fromString
void fromString(source_position &, const std::string &input)
Parse a string (such as 12.10, 13, 0 etc )into source position data structure.
AbstractHandle::u_specifier_value
Definition: abstract_handle.h:86
AbstractHandle::abstract_handle::get_specifier
specifier * get_specifier()
allow multiple specifiers or not?
Definition: abstract_handle.h:229
AbstractHandle::abstract_node::getNumbering
virtual size_t getNumbering(const abstract_node *another_node) const
Get the ordering of the construct relative to another construct in a higher scope Numbering start fro...
AbstractHandle::abstract_node::findNode
virtual abstract_node * findNode(std::string handle_str) const
Find a node from a string for a abstract handle's string format, starting from this node eg.
AbstractHandle::source_position
source position information:
Definition: abstract_handle.h:59
AbstractHandle
String annotations to uniquely indicate any source constructs.
Definition: abstract_handle.h:56