ROSE  0.11.96.11
AstAttributeMechanism.h
1 #ifndef ROSE_AstAttributeMechanism_H
2 #define ROSE_AstAttributeMechanism_H
3 
4 #include "rosedll.h"
5 #include <Sawyer/Attribute.h>
6 #include <list>
7 #include <set>
8 
9 class SgNode;
10 class SgNamedType;
13 
35 class ROSE_DLL_API AstAttribute {
36 public:
44  UNKNOWN_OWNERSHIP
45  };
46 
100  virtual OwnershipPolicy getOwnershipPolicy() const;
101 
103  class ROSE_DLL_API AttributeEdgeInfo {
104  public:
105  SgNode* fromNode;
106  SgNode* toNode;
107  std::string label;
108  std::string options;
109 
110  AttributeEdgeInfo(SgNode* fromNode, SgNode* toNode, std::string label, std::string options)
111  : fromNode(fromNode), toNode(toNode), label(label), options(options) {}
112 
113  ~AttributeEdgeInfo() {
114  fromNode = NULL;
115  toNode = NULL;
116  };
117  };
118 
120  class ROSE_DLL_API AttributeNodeInfo {
121  public:
122  SgNode* nodePtr;
123  std::string label;
124  std::string options;
125 
126  AttributeNodeInfo(SgNode* nodePtr, std::string label, std::string options)
127  : nodePtr(nodePtr), label(label), options(options) {}
128 
129  ~AttributeNodeInfo() {};
130  };
131 
132 public:
133  AstAttribute() {}
134  virtual ~AstAttribute() {}
135 
145  virtual AstAttribute* constructor() const {
146  return new AstAttribute; // it doesn't make much sense to instantiate this type!
147  }
148 
161  virtual AstAttribute* copy() const {
162  return NULL; // attribute will not be copied when the containing obj is copied
163  }
164 
165  // DO NOT DOCUMENT! The original implementation used a non-const copy constructor and many subclasses that implemented a
166  // copy constructor didn't use C++11's "override" word as a defensive measure. Since we don't have access to all those
167  // subclasses, we must continue to support the non-const version. Subclasses should only have to implement one or the
168  // other, not both.
169  virtual AstAttribute* copy() {
170  return const_cast<const AstAttribute*>(this)->copy();
171  }
172 
181  virtual std::string attribute_class_name() const {
182  return "AstAttribute"; // almost certainly not the right dynamic type name!
183  }
184 
190  virtual std::string toString();
191 
195  virtual int packed_size();
196  virtual char* packed_data();
197  virtual void unpacked_data( int size, char* data );
203  virtual std::string additionalNodeOptions();
204  virtual std::vector<AttributeEdgeInfo> additionalEdgeInfo();
205  virtual std::vector<AttributeNodeInfo> additionalNodeInfo();
211  virtual bool commentOutNodeInGraph();
212 };
213 
214 
248 class ROSE_DLL_API AstAttributeMechanism {
249  // Use containment because we want to keep the original API.
250  Sawyer::Attribute::Storage<> attributes_;
251 
252 public:
257 
267  assignFrom(other);
268  }
269 
281  AstAttributeMechanism& operator=(const AstAttributeMechanism &other);
282 
292 
300  bool exists(const std::string &name) const;
301 
318  void set(const std::string &name, AstAttribute *value);
319 
330  bool add(const std::string &name, AstAttribute *value);
331 
342  bool replace(const std::string &name, AstAttribute *value);
343 
357  AstAttribute* operator[](const std::string &name) const;
358 
368  void remove(const std::string &name);
369 
371  typedef std::set<std::string> AttributeIdentifiers;
372 
386  AttributeIdentifiers getAttributeIdentifiers() const;
387 
395  size_t size() const;
396 
397 private:
398  // Called by copy constructor and assignment.
399  void assignFrom(const AstAttributeMechanism &other);
400 };
401 
402 
403 
410 class ROSE_DLL_API MetricAttribute: public AstAttribute {
411 protected:
412  bool is_derived_;
413  double value_;
414 
415 public:
417  : is_derived_(false), value_(0) {}
418 
419  MetricAttribute(double value, bool is_derived=false)
420  : is_derived_(is_derived), value_(value) {}
421 
422  virtual AstAttribute* copy() const override;
423  virtual std::string attribute_class_name() const override;
424 
425  virtual bool isDerived() const { return is_derived_; }
426  virtual double getValue() const { return value_; }
427  virtual void setValue(double newVal) { value_ = newVal; }
428 
429  virtual std::string toString() override;
430 
431  virtual int packed_size() override;
432  virtual char* packed_data() override;
433  virtual void unpacked_data(int size, char* data) override;
434 
435  MetricAttribute& operator+=(const MetricAttribute &other);
436  MetricAttribute& operator-=(const MetricAttribute &other);
437  MetricAttribute& operator*=(const MetricAttribute &other);
438  MetricAttribute& operator/=(const MetricAttribute &other);
439 };
440 
441 
448 template<class T>
449 class ROSE_DLL_API AstValueAttribute: public AstAttribute {
450 public:
452  typedef T Value;
453 private:
454  Value value_;
455 public:
458 
460  explicit AstValueAttribute(const T &value): value_(value) {}
461 
463  AstValueAttribute(const AstValueAttribute &other): value_(other.value_) {}
464 
465  virtual AstAttribute* copy() const override { return new AstValueAttribute(*this); }
466  virtual std::string attribute_class_name() const override { return "AstValueAttribute"; }
467 
471  const T& get() const { return value_; }
472  T& get() { return value_; }
476  void set(const T& value) { value_ = value; }
477 };
478 
479 // DQ (11/21/2009): Added new kind of attribute for handling regex trees.
483 class ROSE_DLL_API AstRegExAttribute: public AstAttribute {
484 public:
485  std::string expression;
486 
487  AstRegExAttribute() {}
488 
489  explicit AstRegExAttribute(const std::string & s)
490  : expression(s) {}
491 
492  virtual AstAttribute* copy() const override {
493  return new AstRegExAttribute(*this);
494  }
495 
496  virtual std::string attribute_class_name() const override {
497  return "AstRegExAttribute";
498  }
499 };
500 
501 
502 // PC (10/21/2012): Added new kind of attribute for handling regex trees.
506 class ROSE_DLL_API AstSgNodeAttribute: public AstValueAttribute<SgNode*> {
507 public:
509  AstSgNodeAttribute(): Super(NULL) {}
510  explicit AstSgNodeAttribute(SgNode *value): Super(value) {}
511  AstSgNodeAttribute(const AstSgNodeAttribute &other): Super(other) {}
512  virtual AstAttribute* copy() const override { return new AstSgNodeAttribute(*this); }
513  virtual std::string attribute_class_name() const override { return "AstSgNodeAttribute"; }
514  SgNode* getNode() { return get(); }
515  void setNode(SgNode *node) { set(node); }
516 };
517 
518 class ROSE_DLL_API AstSgNodeListAttribute: public AstValueAttribute<std::vector<SgNode*> > {
519 public:
522  explicit AstSgNodeListAttribute(std::vector<SgNode *> &value): Super(value) {}
523  AstSgNodeListAttribute(const AstSgNodeListAttribute &other): Super(other) {}
524  virtual AstAttribute* copy() const override { return new AstSgNodeListAttribute(*this); }
525  virtual std::string attribute_class_name() const override { return "AstSgNodeListAttribute"; }
526  std::vector<SgNode*> &getNodeList() { return get(); }
527  void addNode(SgNode *n) { get().push_back(n); }
528  void setNode(SgNode*, int);
529  SgNode *getNode(int);
530  int size() { return get().size(); }
531 };
532 
533 class ROSE_DLL_API AstIntAttribute : public AstValueAttribute<int> {
534 public:
536  AstIntAttribute(): Super(0) {}
537  explicit AstIntAttribute(int value): Super(value) {}
538  AstIntAttribute(const AstIntAttribute &other): Super(other) {}
539  virtual AstAttribute* copy() const override { return new AstIntAttribute(*this); }
540  virtual std::string attribute_class_name() const override { return "AstIntAttribute"; }
541  int getValue() { return get(); }
542 };
543 
544 #endif
AstSgNodeAttribute::attribute_class_name
virtual std::string attribute_class_name() const override
Attribute class name.
Definition: AstAttributeMechanism.h:513
AstAttributeMechanism::AstAttributeMechanism
AstAttributeMechanism()
Default constructor.
Definition: AstAttributeMechanism.h:256
AstAttribute::AttributeEdgeInfo
Support for attibutes to specify edges in the dot graphs.
Definition: AstAttributeMechanism.h:103
AstSgNodeListAttribute
Definition: AstAttributeMechanism.h:518
AstAttribute::CONTAINER_OWNERSHIP
@ CONTAINER_OWNERSHIP
Container owns attribute.
Definition: AstAttributeMechanism.h:41
Sawyer::Attribute::Storage
API and storage for attributes.
Definition: Attribute.h:208
AstIntAttribute
Definition: AstAttributeMechanism.h:533
AstValueAttribute::get
T & get()
Return the stored value by reference.
Definition: AstAttributeMechanism.h:472
AstAttribute
Base class for all IR node attribute values.
Definition: AstAttributeMechanism.h:35
AstSgNodeListAttribute::copy
virtual AstAttribute * copy() const override
Virtual copy constructor.
Definition: AstAttributeMechanism.h:524
SgTemplateParameterList
Definition: Cxx_Grammar.h:32974
AstAttribute::toString
virtual std::string toString()
Convert an attribute to a string.
AstValueAttribute::AstValueAttribute
AstValueAttribute(const T &value)
Constructs an attribute containing a specified value.
Definition: AstAttributeMechanism.h:460
MetricAttribute
Attribute corresponding to a metric.
Definition: AstAttributeMechanism.h:410
AstAttribute::constructor
virtual AstAttribute * constructor() const
Virtual default constructor.
Definition: AstAttributeMechanism.h:145
AstValueAttribute::attribute_class_name
virtual std::string attribute_class_name() const override
Attribute class name.
Definition: AstAttributeMechanism.h:466
AstSgNodeAttribute::copy
virtual AstAttribute * copy() const override
Virtual copy constructor.
Definition: AstAttributeMechanism.h:512
AstValueAttribute::AstValueAttribute
AstValueAttribute(const AstValueAttribute &other)
Copy constructor.
Definition: AstAttributeMechanism.h:463
SgTemplateParameter
Definition: Cxx_Grammar.h:28952
AstAttribute::packed_data
virtual char * packed_data()
Packing support.
AstValueAttribute
IR node attribute that stores a copyable value.
Definition: AstAttributeMechanism.h:449
AstAttribute::copy
virtual AstAttribute * copy() const
Virtual copy constructor.
Definition: AstAttributeMechanism.h:161
AstValueAttribute::get
const T & get() const
Return the stored value by reference.
Definition: AstAttributeMechanism.h:471
AstIntAttribute::copy
virtual AstAttribute * copy() const override
Virtual copy constructor.
Definition: AstAttributeMechanism.h:539
AstAttribute::unpacked_data
virtual void unpacked_data(int size, char *data)
Packing support.
AstAttribute::CUSTOM_OWNERSHIP
@ CUSTOM_OWNERSHIP
Subclass defines ownership policy.
Definition: AstAttributeMechanism.h:43
AstAttribute::attribute_class_name
virtual std::string attribute_class_name() const
Attribute class name.
Definition: AstAttributeMechanism.h:181
AstAttributeMechanism::AstAttributeMechanism
AstAttributeMechanism(const AstAttributeMechanism &other)
Copy constructor.
Definition: AstAttributeMechanism.h:266
AstIntAttribute::attribute_class_name
virtual std::string attribute_class_name() const override
Attribute class name.
Definition: AstAttributeMechanism.h:540
SgNode
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:6739
AstAttributeMechanism::AttributeIdentifiers
std::set< std::string > AttributeIdentifiers
Set of attribute names.
Definition: AstAttributeMechanism.h:371
AstAttributeMechanism
Stores named attributes in Sage IR nodes.
Definition: AstAttributeMechanism.h:248
SgNamedType
Definition: Cxx_Grammar.h:58956
AstValueAttribute::set
void set(const T &value)
Assign a new value.
Definition: AstAttributeMechanism.h:476
AstValueAttribute::Value
T Value
Type of value this wrapper holds.
Definition: AstAttributeMechanism.h:452
AstRegExAttribute::copy
virtual AstAttribute * copy() const override
Virtual copy constructor.
Definition: AstAttributeMechanism.h:492
AstAttribute::packed_size
virtual int packed_size()
Packing support.
AstRegExAttribute
Attribute containing a regex expression as a string.
Definition: AstAttributeMechanism.h:483
AstValueAttribute::AstValueAttribute
AstValueAttribute()
Default constructor.
Definition: AstAttributeMechanism.h:457
AstSgNodeAttribute
Attribute storing an SgNode.
Definition: AstAttributeMechanism.h:506
AstValueAttribute::copy
virtual AstAttribute * copy() const override
Virtual copy constructor.
Definition: AstAttributeMechanism.h:465
AstAttribute::AttributeNodeInfo
Support for adding nodes to DOT graphs.
Definition: AstAttributeMechanism.h:120
AstRegExAttribute::attribute_class_name
virtual std::string attribute_class_name() const override
Attribute class name.
Definition: AstAttributeMechanism.h:496
AstAttribute::NO_OWNERSHIP
@ NO_OWNERSHIP
Attributes are always leaked.
Definition: AstAttributeMechanism.h:42
AstSgNodeListAttribute::attribute_class_name
virtual std::string attribute_class_name() const override
Attribute class name.
Definition: AstAttributeMechanism.h:525
AstAttribute::OwnershipPolicy
OwnershipPolicy
Who owns this attribute.
Definition: AstAttributeMechanism.h:40