ROSE  0.11.145.0
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;
14 
36 class ROSE_DLL_API AstAttribute {
37 public:
45  UNKNOWN_OWNERSHIP
46  };
47 
101  virtual OwnershipPolicy getOwnershipPolicy() const;
102 
104  class ROSE_DLL_API AttributeEdgeInfo {
105  public:
106  SgNode* fromNode;
107  SgNode* toNode;
108  std::string label;
109  std::string options;
110 
111  AttributeEdgeInfo(SgNode* fromNode, SgNode* toNode, std::string label, std::string options)
112  : fromNode(fromNode), toNode(toNode), label(label), options(options) {}
113 
114  ~AttributeEdgeInfo() {
115  fromNode = NULL;
116  toNode = NULL;
117  };
118  };
119 
121  class ROSE_DLL_API AttributeNodeInfo {
122  public:
123  SgNode* nodePtr;
124  std::string label;
125  std::string options;
126 
127  AttributeNodeInfo(SgNode* nodePtr, std::string label, std::string options)
128  : nodePtr(nodePtr), label(label), options(options) {}
129 
130  ~AttributeNodeInfo() {};
131  };
132 
133 public:
134  AstAttribute() {}
135  virtual ~AstAttribute() {}
136 
146  virtual AstAttribute* constructor() const {
147  return new AstAttribute; // it doesn't make much sense to instantiate this type!
148  }
149 
162  virtual AstAttribute* copy() const {
163  return NULL; // attribute will not be copied when the containing obj is copied
164  }
165 
166  // DO NOT DOCUMENT! The original implementation used a non-const copy constructor and many subclasses that implemented a
167  // copy constructor didn't use C++11's "override" word as a defensive measure. Since we don't have access to all those
168  // subclasses, we must continue to support the non-const version. Subclasses should only have to implement one or the
169  // other, not both.
170  virtual AstAttribute* copy() {
171  return const_cast<const AstAttribute*>(this)->copy();
172  }
173 
182  virtual std::string attribute_class_name() const {
183  return "AstAttribute"; // almost certainly not the right dynamic type name!
184  }
185 
191  virtual std::string toString();
192 
196  virtual int packed_size();
197  virtual char* packed_data();
198  virtual void unpacked_data( int size, char* data );
204  virtual std::string additionalNodeOptions();
205  virtual std::vector<AttributeEdgeInfo> additionalEdgeInfo();
206  virtual std::vector<AttributeNodeInfo> additionalNodeInfo();
212  virtual bool commentOutNodeInGraph();
213 };
214 
215 
249 class ROSE_DLL_API AstAttributeMechanism {
250  // Use containment because we want to keep the original API.
251  Sawyer::Attribute::Storage<> attributes_;
252 
253 public:
258 
268  assignFrom(other);
269  }
270 
282  AstAttributeMechanism& operator=(const AstAttributeMechanism &other);
283 
293 
301  bool exists(const std::string &name) const;
302 
319  void set(const std::string &name, AstAttribute *value);
320 
331  bool add(const std::string &name, AstAttribute *value);
332 
343  bool replace(const std::string &name, AstAttribute *value);
344 
358  AstAttribute* operator[](const std::string &name) const;
359 
369  void remove(const std::string &name);
370 
372  typedef std::set<std::string> AttributeIdentifiers;
373 
387  AttributeIdentifiers getAttributeIdentifiers() const;
388 
396  size_t size() const;
397 
398 private:
399  // Called by copy constructor and assignment.
400  void assignFrom(const AstAttributeMechanism &other);
401 };
402 
403 
404 
411 class ROSE_DLL_API MetricAttribute: public AstAttribute {
412 protected:
413  bool is_derived_;
414  double value_;
415 
416 public:
418  : is_derived_(false), value_(0) {}
419 
420  MetricAttribute(double value, bool is_derived=false)
421  : is_derived_(is_derived), value_(value) {}
422 
423  virtual AstAttribute* copy() const override;
424  virtual std::string attribute_class_name() const override;
425 
426  virtual bool isDerived() const { return is_derived_; }
427  virtual double getValue() const { return value_; }
428  virtual void setValue(double newVal) { value_ = newVal; }
429 
430  virtual std::string toString() override;
431 
432  virtual int packed_size() override;
433  virtual char* packed_data() override;
434  virtual void unpacked_data(int size, char* data) override;
435 
436  MetricAttribute& operator+=(const MetricAttribute &other);
437  MetricAttribute& operator-=(const MetricAttribute &other);
438  MetricAttribute& operator*=(const MetricAttribute &other);
439  MetricAttribute& operator/=(const MetricAttribute &other);
440 };
441 
442 
449 template<class T>
450 class ROSE_DLL_API AstValueAttribute: public AstAttribute {
451 public:
453  typedef T Value;
454 private:
455  Value value_;
456 public:
459 
461  explicit AstValueAttribute(const T &value): value_(value) {}
462 
464  AstValueAttribute(const AstValueAttribute &other): value_(other.value_) {}
465 
466  virtual AstAttribute* copy() const override { return new AstValueAttribute(*this); }
467  virtual std::string attribute_class_name() const override { return "AstValueAttribute"; }
468 
472  const T& get() const { return value_; }
473  T& get() { return value_; }
477  void set(const T& value) { value_ = value; }
478 };
479 
480 // DQ (11/21/2009): Added new kind of attribute for handling regex trees.
484 class ROSE_DLL_API AstRegExAttribute: public AstAttribute {
485 public:
486  std::string expression;
487 
488  AstRegExAttribute() {}
489 
490  explicit AstRegExAttribute(const std::string & s)
491  : expression(s) {}
492 
493  virtual AstAttribute* copy() const override {
494  return new AstRegExAttribute(*this);
495  }
496 
497  virtual std::string attribute_class_name() const override {
498  return "AstRegExAttribute";
499  }
500 };
501 
502 
503 // PC (10/21/2012): Added new kind of attribute for handling regex trees.
507 class ROSE_DLL_API AstSgNodeAttribute: public AstValueAttribute<SgNode*> {
508 public:
510  AstSgNodeAttribute(): Super(NULL) {}
511  explicit AstSgNodeAttribute(SgNode *value): Super(value) {}
512  AstSgNodeAttribute(const AstSgNodeAttribute &other): Super(other) {}
513  virtual AstAttribute* copy() const override { return new AstSgNodeAttribute(*this); }
514  virtual std::string attribute_class_name() const override { return "AstSgNodeAttribute"; }
515  SgNode* getNode() { return get(); }
516  void setNode(SgNode *node) { set(node); }
517 };
518 
519 class ROSE_DLL_API AstSgNodeListAttribute: public AstValueAttribute<std::vector<SgNode*> > {
520 public:
523  explicit AstSgNodeListAttribute(std::vector<SgNode *> &value): Super(value) {}
524  AstSgNodeListAttribute(const AstSgNodeListAttribute &other): Super(other) {}
525  virtual AstAttribute* copy() const override { return new AstSgNodeListAttribute(*this); }
526  virtual std::string attribute_class_name() const override { return "AstSgNodeListAttribute"; }
527  std::vector<SgNode*> &getNodeList() { return get(); }
528  void addNode(SgNode *n) { get().push_back(n); }
529  void setNode(SgNode*, int);
530  SgNode *getNode(int);
531  int size() { return get().size(); }
532 };
533 
534 class ROSE_DLL_API AstIntAttribute : public AstValueAttribute<int> {
535 public:
536  typedef AstValueAttribute<int> Super;
537  AstIntAttribute(): Super(0) {}
538  explicit AstIntAttribute(int value): Super(value) {}
539  AstIntAttribute(const AstIntAttribute &other): Super(other) {}
540  virtual AstAttribute* copy() const override { return new AstIntAttribute(*this); }
541  virtual std::string attribute_class_name() const override { return "AstIntAttribute"; }
542  int getValue() { return get(); }
543 };
544 
545 class ROSE_DLL_API AstParameterizedTypeAttribute: public AstAttribute {
546  SgNamedType *genericType;
547  std::list<SgJavaParameterizedType *> parameterizedTypes;
548 
549 public:
551  : genericType(NULL) {}
552 
553  explicit AstParameterizedTypeAttribute(SgNamedType *genericType);
554 
556  : genericType(other.genericType), parameterizedTypes(other.parameterizedTypes) {}
557 
558  virtual AstAttribute* copy() const override {
559  return new AstParameterizedTypeAttribute(*this);
560  }
561 
562  virtual std::string attribute_class_name() const override {
563  return "AstParameterizedTypeAttribute";
564  }
565 
566  bool argumentsMatch(SgTemplateParameterList *type_arg_list, std::vector<SgTemplateParameter *> *new_args);
567 
568  SgJavaParameterizedType *findOrInsertParameterizedType(std::vector<SgTemplateParameter *> *new_args);
569 };
570 
571 #endif
virtual AstAttribute * constructor() const
Virtual default constructor.
virtual std::string attribute_class_name() const override
Attribute class name.
Support for adding nodes to DOT graphs.
AstAttributeMechanism()
Default constructor.
Attribute containing a regex expression as a string.
OwnershipPolicy
Who owns this attribute.
Support for attibutes to specify edges in the dot graphs.
std::set< std::string > AttributeIdentifiers
Set of attribute names.
virtual std::string attribute_class_name() const override
Attribute class name.
Subclass defines ownership policy.
virtual AstAttribute * copy() const override
Virtual copy constructor.
virtual char * packed_data()
Packing support.
virtual std::string toString()
Convert an attribute to a string.
AstValueAttribute(const T &value)
Constructs an attribute containing a specified value.
Attribute corresponding to a metric.
Base class for all IR node attribute values.
virtual std::string attribute_class_name() const
Attribute class name.
Attributes are always leaked.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9846
AstValueAttribute(const AstValueAttribute &other)
Copy constructor.
virtual AstAttribute * copy() const
Virtual copy constructor.
IR node attribute that stores a copyable value.
void set(const T &value)
Assign a new value.
virtual AstAttribute * copy() const override
Virtual copy constructor.
T Value
Type of value this wrapper holds.
Stores named attributes in Sage IR nodes.
virtual std::string attribute_class_name() const override
Attribute class name.
virtual std::string attribute_class_name() const override
Attribute class name.
virtual AstAttribute * copy() const override
Virtual copy constructor.
AstValueAttribute()
Default constructor.
virtual AstAttribute * copy() const override
Virtual copy constructor.
AstAttributeMechanism(const AstAttributeMechanism &other)
Copy constructor.
virtual AstAttribute * copy() const override
Virtual copy constructor.
virtual std::string attribute_class_name() const override
Attribute class name.
Attribute storing an SgNode.
virtual std::string attribute_class_name() const override
Attribute class name.
virtual AstAttribute * copy() const override
Virtual copy constructor.
virtual int packed_size()
Packing support.
virtual void unpacked_data(int size, char *data)
Packing support.