/* * This file is part of the Cforall project * * $Id: Initializer.h,v 1.14 2005/08/29 20:59:25 rcbilson Exp $ * */ #ifndef INITIALIZER_H #define INITIALIZER_H #include "SynTree.h" #include "Visitor.h" #include "Mutator.h" #include // Initializer: base class for object initializers (provide default values) class Initializer { public: // Initializer( std::string _name = std::string(""), int _pos = 0 ); Initializer( ); virtual ~Initializer(); static std::string designator_name( Expression *designator ); // void set_name( std::string newValue ) { name = newValue; } // std::string get_name() const { return name; } // void set_pos( int newValue ) { pos = newValue; } // int get_pos() const { return pos; } virtual void set_designators( std::list & ) { assert(false); } virtual std::list &get_designators() { assert(false); std::list *ret = 0; return *ret; // never reached } virtual Initializer *clone() const = 0; virtual void accept( Visitor &v ) = 0; virtual Initializer *acceptMutator( Mutator &m ) = 0; virtual void print( std::ostream &os, int indent = 0 ); private: // std::string name; // int pos; }; // SingleInit represents an initializer for a common object (e.g., int x = 4) class SingleInit : public Initializer { public: SingleInit( Expression *value, std::list< Expression *> &designators ); SingleInit( const SingleInit &other ); virtual ~SingleInit(); Expression *get_value() { return value; } void set_value( Expression *newValue ) { value = newValue; } void set_designators( std::list &newValue ) { designators = newValue; } std::list &get_designators() { return designators; } virtual SingleInit *clone() const; virtual void accept( Visitor &v ) { v.visit( this ); } virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } virtual void print( std::ostream &os, int indent = 0 ); private: //Constant *value; Expression *value; // has to be a compile-time constant std::list< Expression * > designators; }; // MemberInit represents an initializer for a member of an aggregate object // (e.g., struct q { int a } x = { a: 4 } ) class MemberInit : public Initializer { public: MemberInit( Expression *value, std::string member = std::string("") ); virtual ~MemberInit(); std::string get_member() { return member; } void set_member( std::string newValue ) { member = newValue; } Expression *get_value() { return value; } void set_value( Expression *newValue ) { value = newValue; } virtual MemberInit *clone() const; virtual void accept( Visitor &v ) { v.visit( this ); } virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } virtual void print( std::ostream &os, int indent = 0 ); private: std::string member; Expression *value; }; // ElementInit represents an initializer of an element of an array // (e.g., [10] int x = { [7]: 4 } class ElementInit : public Initializer { public: ElementInit( Expression *value ); virtual ~ElementInit(); int get_index() { return index; } void set_index( int newValue ) { index = newValue; } Expression *get_value() { return value; } void set_value( Expression *newValue ) { value = newValue; } virtual ElementInit *clone() const; virtual void accept( Visitor &v ) { v.visit( this ); } virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } virtual void print( std::ostream &os, int indent = 0 ); private: int index; Expression *value; }; // ListInit represents an initializer that is composed recursively of a list of initializers; this // is used to initialize an array or aggregate class ListInit : public Initializer { public: ListInit( std::list &, std::list &designators = *(new std::list()) ); virtual ~ListInit(); void set_designators( std::list &newValue ) { designators = newValue; } std::list &get_designators() { return designators; } void set_initializers( std::list &newValue ) { initializers = newValue; } std::list &get_initializers() { return initializers; } std::list::iterator begin_initializers() { return initializers.begin(); } std::list::iterator end_initializers() { return initializers.end(); } virtual ListInit *clone() const; virtual void accept( Visitor &v ) { v.visit( this ); } virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } virtual void print( std::ostream &os, int indent = 0 ); private: std::list initializers; // order *is* important std::list designators; }; #endif /* #ifndef INITIALIZER_H */ /* Local Variables: mode: c++ End: */