ROSE  0.9.9.109
AstAttributeMechanism.h
1 #ifndef ROSE_AstAttributeMechanism_H
2 #define ROSE_AstAttributeMechanism_H
3 
4 #include "rosedll.h"
5 #include "rose_override.h"
6 #include <Sawyer/Attribute.h>
7 #include <list>
8 #include <set>
9 
10 class SgNode;
11 class SgNamedType;
15 
37 class ROSE_DLL_API AstAttribute {
38 public:
42  enum OwnershipPolicy {
43  CONTAINER_OWNERSHIP,
44  NO_OWNERSHIP,
45  CUSTOM_OWNERSHIP,
46  UNKNOWN_OWNERSHIP
47  };
48 
102  virtual OwnershipPolicy getOwnershipPolicy() const;
103 
105  class ROSE_DLL_API AttributeEdgeInfo {
106  public:
107  SgNode* fromNode;
108  SgNode* toNode;
109  std::string label;
110  std::string options;
111 
112  AttributeEdgeInfo(SgNode* fromNode, SgNode* toNode, std::string label, std::string options)
113  : fromNode(fromNode), toNode(toNode), label(label), options(options) {}
114 
115  ~AttributeEdgeInfo() {
116  fromNode = NULL;
117  toNode = NULL;
118  };
119  };
120 
122  class ROSE_DLL_API AttributeNodeInfo {
123  public:
124  SgNode* nodePtr;
125  std::string label;
126  std::string options;
127 
128  AttributeNodeInfo(SgNode* nodePtr, std::string label, std::string options)
129  : nodePtr(nodePtr), label(label), options(options) {}
130 
131  ~AttributeNodeInfo() {};
132  };
133 
134 public:
135  AstAttribute() {}
136  virtual ~AstAttribute() {}
137 
147  virtual AstAttribute* constructor() const {
148  return new AstAttribute; // it doesn't make much sense to instantiate this type!
149  }
150 
163  virtual AstAttribute* copy() const {
164  return NULL; // attribute will not be copied when the containing obj is copied
165  }
166 
167  // DO NOT DOCUMENT! The original implementation used a non-const copy constructor and many subclasses that implemented a
168  // copy constructor didn't use C++11's "override" (ROSE_OVERRIDE) word as a defensive measure. Since we don't have access
169  // to all those subclasses, we must continue to support the non-const version. Subclasses should only have to implement
170  // one or the other, not both.
171  virtual AstAttribute* copy() {
172  return const_cast<const AstAttribute*>(this)->copy();
173  }
174 
183  virtual std::string attribute_class_name() const {
184  return "AstAttribute"; // almost certainly not the right dynamic type name!
185  }
186 
192  virtual std::string toString();
193 
197  virtual int packed_size();
198  virtual char* packed_data();
199  virtual void unpacked_data( int size, char* data );
205  virtual std::string additionalNodeOptions();
206  virtual std::vector<AttributeEdgeInfo> additionalEdgeInfo();
207  virtual std::vector<AttributeNodeInfo> additionalNodeInfo();
213  virtual bool commentOutNodeInGraph();
214 };
215 
216 
250 class ROSE_DLL_API AstAttributeMechanism {
251  // Use containment because we want to keep the original API.
252  Sawyer::Attribute::Storage<> attributes_;
253 
254 public:
258  AstAttributeMechanism() {}
259 
268  AstAttributeMechanism(const AstAttributeMechanism &other) {
269  assignFrom(other);
270  }
271 
283  AstAttributeMechanism& operator=(const AstAttributeMechanism &other);
284 
293  ~AstAttributeMechanism();
294 
302  bool exists(const std::string &name) const;
303 
320  void set(const std::string &name, AstAttribute *value);
321 
332  bool add(const std::string &name, AstAttribute *value);
333 
344  bool replace(const std::string &name, AstAttribute *value);
345 
359  AstAttribute* operator[](const std::string &name) const;
360 
370  void remove(const std::string &name);
371 
373  typedef std::set<std::string> AttributeIdentifiers;
374 
388  AttributeIdentifiers getAttributeIdentifiers() const;
389 
397  size_t size() const;
398 
399 private:
400  // Called by copy constructor and assignment.
401  void assignFrom(const AstAttributeMechanism &other);
402 };
403 
404 
405 
412 class ROSE_DLL_API MetricAttribute: public AstAttribute {
413 protected:
414  bool is_derived_;
415  double value_;
416 
417 public:
418  MetricAttribute()
419  : is_derived_(false), value_(0) {}
420 
421  MetricAttribute(double value, bool is_derived=false)
422  : is_derived_(is_derived), value_(value) {}
423 
424  virtual AstAttribute* copy() const ROSE_OVERRIDE;
425  virtual std::string attribute_class_name() const ROSE_OVERRIDE;
426 
427  virtual bool isDerived() const { return is_derived_; }
428  virtual double getValue() const { return value_; }
429  virtual void setValue(double newVal) { value_ = newVal; }
430 
431  virtual std::string toString() ROSE_OVERRIDE;
432 
433  virtual int packed_size() ROSE_OVERRIDE;
434  virtual char* packed_data() ROSE_OVERRIDE;
435  virtual void unpacked_data(int size, char* data) ROSE_OVERRIDE;
436 
437  MetricAttribute& operator+=(const MetricAttribute &other);
438  MetricAttribute& operator-=(const MetricAttribute &other);
439  MetricAttribute& operator*=(const MetricAttribute &other);
440  MetricAttribute& operator/=(const MetricAttribute &other);
441 };
442 
443 
450 template<class T>
451 class ROSE_DLL_API AstValueAttribute: public AstAttribute {
452 public:
454  typedef T Value;
455 private:
456  Value value_;
457 public:
459  AstValueAttribute() {}
460 
462  explicit AstValueAttribute(const T &value): value_(value) {}
463 
465  AstValueAttribute(const AstValueAttribute &other): value_(other.value_) {}
466 
467  virtual AstAttribute* copy() const ROSE_OVERRIDE { return new AstValueAttribute(*this); }
468  virtual std::string attribute_class_name() const ROSE_OVERRIDE { return "AstValueAttribute"; }
469 
473  const T& get() const { return value_; }
474  T& get() { return value_; }
478  void set(const T& value) { value_ = value; }
479 };
480 
481 // DQ (11/21/2009): Added new kind of attribute for handling regex trees.
485 class ROSE_DLL_API AstRegExAttribute: public AstAttribute {
486 public:
487  std::string expression;
488 
489  AstRegExAttribute() {}
490 
491  explicit AstRegExAttribute(const std::string & s)
492  : expression(s) {}
493 
494  virtual AstAttribute* copy() const ROSE_OVERRIDE {
495  return new AstRegExAttribute(*this);
496  }
497 
498  virtual std::string attribute_class_name() const ROSE_OVERRIDE {
499  return "AstRegExAttribute";
500  }
501 };
502 
503 
504 // PC (10/21/2012): Added new kind of attribute for handling regex trees.
508 class ROSE_DLL_API AstSgNodeAttribute: public AstValueAttribute<SgNode*> {
509 public:
510  typedef AstValueAttribute<SgNode*> Super;
511  AstSgNodeAttribute(): Super(NULL) {}
512  explicit AstSgNodeAttribute(SgNode *value): Super(value) {}
513  AstSgNodeAttribute(const AstSgNodeAttribute &other): Super(other) {}
514  virtual AstAttribute* copy() const ROSE_OVERRIDE { return new AstSgNodeAttribute(*this); }
515  virtual std::string attribute_class_name() const ROSE_OVERRIDE { return "AstSgNodeAttribute"; }
516  SgNode* getNode() { return get(); }
517  void setNode(SgNode *node) { set(node); }
518 };
519 
520 class ROSE_DLL_API AstSgNodeListAttribute: public AstValueAttribute<std::vector<SgNode*> > {
521 public:
522  typedef AstValueAttribute<std::vector<SgNode*> > Super;
523  AstSgNodeListAttribute() {}
524  explicit AstSgNodeListAttribute(std::vector<SgNode *> &value): Super(value) {}
525  AstSgNodeListAttribute(const AstSgNodeListAttribute &other): Super(other) {}
526  virtual AstAttribute* copy() const ROSE_OVERRIDE { return new AstSgNodeListAttribute(*this); }
527  virtual std::string attribute_class_name() const ROSE_OVERRIDE { return "AstSgNodeListAttribute"; }
528  std::vector<SgNode*> &getNodeList() { return get(); }
529  void addNode(SgNode *n) { get().push_back(n); }
530  void setNode(SgNode*, int);
531  SgNode *getNode(int);
532  int size() { return get().size(); }
533 };
534 
535 class ROSE_DLL_API AstIntAttribute : public AstValueAttribute<int> {
536 public:
537  typedef AstValueAttribute<int> Super;
538  AstIntAttribute(): Super(0) {}
539  explicit AstIntAttribute(int value): Super(value) {}
540  AstIntAttribute(const AstIntAttribute &other): Super(other) {}
541  virtual AstAttribute* copy() const ROSE_OVERRIDE { return new AstIntAttribute(*this); }
542  virtual std::string attribute_class_name() const ROSE_OVERRIDE { return "AstIntAttribute"; }
543  int getValue() { return get(); }
544 };
545 
546 class ROSE_DLL_API AstParameterizedTypeAttribute: public AstAttribute {
547  SgNamedType *genericType;
548  std::list<SgJavaParameterizedType *> parameterizedTypes;
549 
550 public:
551  AstParameterizedTypeAttribute()
552  : genericType(NULL) {}
553 
554  explicit AstParameterizedTypeAttribute(SgNamedType *genericType);
555 
556  AstParameterizedTypeAttribute(const AstParameterizedTypeAttribute &other)
557  : genericType(other.genericType), parameterizedTypes(other.parameterizedTypes) {}
558 
559  virtual AstAttribute* copy() const ROSE_OVERRIDE {
560  return new AstParameterizedTypeAttribute(*this);
561  }
562 
563  virtual std::string attribute_class_name() const ROSE_OVERRIDE {
564  return "AstParameterizedTypeAttribute";
565  }
566 
567  bool argumentsMatch(SgTemplateParameterList *type_arg_list, std::vector<SgTemplateParameter *> *new_args);
568 
569  SgJavaParameterizedType *findOrInsertParameterizedType(std::vector<SgTemplateParameter *> *new_args);
570 };
571 
572 #endif
void copy(const Word *src, const BitRange &srcRange, Word *dst, const BitRange &dstRange)
Copy some bits.
ROSE_DLL_API std::string toString(const Attributes &)
Convert attributes to GraphViz language string.
STL namespace.
void setValue(Word *words, const BitRange &where, bool value)
Set or clear some bits.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:8322
void set(Word *words, const BitRange &where)
Set some bits.