Program Listing for File AstAttributeMechanism.h

Program Listing for File AstAttributeMechanism.h#

Return to documentation for file (src/midend/astProcessing/AstAttributeMechanism.h)

#ifndef ROSE_AstAttributeMechanism_H
#define ROSE_AstAttributeMechanism_H

#include "rosedll.h"
#include <string>
#include <list>
#include <set>
#include <map>

class SgNode;
class SgNamedType;
class SgTemplateParameter;
class SgTemplateParameterList;

class ROSE_DLL_API AstAttribute {
public:
    enum OwnershipPolicy {
        CONTAINER_OWNERSHIP,
        NO_OWNERSHIP,
        CUSTOM_OWNERSHIP,
        UNKNOWN_OWNERSHIP
    };

    virtual OwnershipPolicy getOwnershipPolicy() const;

    class ROSE_DLL_API AttributeEdgeInfo {
    public:
        SgNode* fromNode;
        SgNode* toNode;
        std::string label;
        std::string options;

        AttributeEdgeInfo(SgNode* fromNode, SgNode* toNode, std::string label, std::string options)
            : fromNode(fromNode), toNode(toNode), label(label), options(options) {}

        ~AttributeEdgeInfo() {
            fromNode = NULL;
            toNode = NULL;
        };
    };

    class ROSE_DLL_API AttributeNodeInfo {
    public:
        SgNode* nodePtr;
        std::string label;
        std::string options;

        AttributeNodeInfo(SgNode* nodePtr, std::string label, std::string options)
            : nodePtr(nodePtr), label(label), options(options) {}

        ~AttributeNodeInfo() {};
    };

public:
    AstAttribute() {}
    virtual ~AstAttribute() {}

    virtual AstAttribute* constructor() const {
        return new AstAttribute;                        // it doesn't make much sense to instantiate this type!
    }

    virtual AstAttribute* copy() const {
        return NULL;                                    // attribute will not be copied when the containing obj is copied
    }

    // DO NOT DOCUMENT!  The original implementation used a non-const copy constructor and many subclasses that implemented a
    // copy constructor didn't use C++11's "override" word as a defensive measure. Since we don't have access to all those
    // subclasses, we must continue to support the non-const version.  Subclasses should only have to implement one or the
    // other, not both.
    virtual AstAttribute* copy() {
        return const_cast<const AstAttribute*>(this)->copy();
    }

    virtual std::string attribute_class_name() const {
        return "AstAttribute";                          // almost certainly not the right dynamic type name!
    }

    virtual std::string toString();

    virtual int packed_size();
    virtual char* packed_data();
    virtual void unpacked_data( int size, char* data );
    virtual std::string additionalNodeOptions();
    virtual std::vector<AttributeEdgeInfo> additionalEdgeInfo();
    virtual std::vector<AttributeNodeInfo> additionalNodeInfo();
    virtual bool commentOutNodeInGraph();
};


class ROSE_DLL_API AstAttributeMechanism {
    typedef std::map<std::string, AstAttribute*> attributeMap_;
    attributeMap_ attributes_;

public:
    AstAttributeMechanism() {}

    AstAttributeMechanism(const AstAttributeMechanism &other) {
        assignFrom(other);
    }

    AstAttributeMechanism& operator=(const AstAttributeMechanism &other);

    ~AstAttributeMechanism();

    bool exists(const std::string &name) const;

    void set(const std::string &name, AstAttribute *value);

    bool add(const std::string &name, AstAttribute *value);

    bool replace(const std::string &name, AstAttribute *value);

    AstAttribute* operator[](const std::string &name) const;

    void remove(const std::string &name);

    typedef std::set<std::string> AttributeIdentifiers;

    AttributeIdentifiers getAttributeIdentifiers() const;

    size_t size() const;

private:
    // Called by copy constructor and assignment.
    void assignFrom(const AstAttributeMechanism &other);
};



class ROSE_DLL_API MetricAttribute: public AstAttribute {
protected:
    bool is_derived_;
    double value_;

public:
    MetricAttribute()
        : is_derived_(false), value_(0) {}

    MetricAttribute(double value, bool is_derived=false)
        : is_derived_(is_derived), value_(value) {}

    virtual AstAttribute* copy() const override;
    virtual std::string attribute_class_name() const override;

    virtual bool isDerived() const { return is_derived_; }
    virtual double getValue()  const { return value_; }
    virtual void setValue(double newVal) { value_ = newVal; }

    virtual std::string toString() override;

    virtual int packed_size() override;
    virtual char* packed_data() override;
    virtual void unpacked_data(int size, char* data) override;

    MetricAttribute& operator+=(const MetricAttribute &other);
    MetricAttribute& operator-=(const MetricAttribute &other);
    MetricAttribute& operator*=(const MetricAttribute &other);
    MetricAttribute& operator/=(const MetricAttribute &other);
};


template<class T>
class ROSE_DLL_API AstValueAttribute: public AstAttribute {
public:
    typedef T Value;
private:
    Value value_;
public:
    AstValueAttribute() {}

    explicit AstValueAttribute(const T &value): value_(value) {}

    AstValueAttribute(const AstValueAttribute &other): value_(other.value_) {}

    virtual AstAttribute* copy() const override { return new AstValueAttribute(*this); }
    virtual std::string attribute_class_name() const override { return "AstValueAttribute"; }

    const T& get() const { return value_; }
    T& get() { return value_; }
    void set(const T& value) { value_ = value; }
};

// DQ (11/21/2009): Added new kind of attribute for handling regex trees.
class ROSE_DLL_API AstRegExAttribute: public AstAttribute {
public:
    std::string expression;

    AstRegExAttribute() {}

    explicit AstRegExAttribute(const std::string & s)
        : expression(s) {}

    virtual AstAttribute* copy() const override {
        return new AstRegExAttribute(*this);
    }

    virtual std::string attribute_class_name() const override {
        return "AstRegExAttribute";
    }
};


// PC (10/21/2012): Added new kind of attribute for handling regex trees.
class ROSE_DLL_API AstSgNodeAttribute: public AstValueAttribute<SgNode*> {
public:
    typedef AstValueAttribute<SgNode*> Super;
    AstSgNodeAttribute(): Super(NULL) {}
    explicit AstSgNodeAttribute(SgNode *value): Super(value) {}
    AstSgNodeAttribute(const AstSgNodeAttribute &other): Super(other) {}
    virtual AstAttribute* copy() const override { return new AstSgNodeAttribute(*this); }
    virtual std::string attribute_class_name() const override { return "AstSgNodeAttribute"; }
    SgNode* getNode() { return get(); }
    void setNode(SgNode *node) { set(node); }
};

class ROSE_DLL_API AstIntAttribute : public AstValueAttribute<int> {
public:
    typedef AstValueAttribute<int> Super;
    AstIntAttribute(): Super(0) {}
    explicit AstIntAttribute(int value): Super(value) {}
    AstIntAttribute(const AstIntAttribute &other): Super(other) {}
    virtual AstAttribute* copy() const override { return new AstIntAttribute(*this); }
    virtual std::string attribute_class_name() const override { return "AstIntAttribute"; }
    int getValue() { return get(); }
};

#endif