[0dd3a2f] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[974906e2] | 7 | // Initializer.h -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[974906e2] | 11 | // Last Modified By : Rob Schluntz |
---|
[db4ecc5] | 12 | // Last Modified On : Tue Apr 12 13:49:13 2016 |
---|
[d63eeb0] | 13 | // Update Count : 19 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #ifndef INITIALIZER_H |
---|
| 17 | #define INITIALIZER_H |
---|
| 18 | |
---|
| 19 | #include "SynTree.h" |
---|
| 20 | #include "Visitor.h" |
---|
| 21 | #include "Mutator.h" |
---|
[70f89d00] | 22 | #include "Type.h" |
---|
[51b7345] | 23 | |
---|
| 24 | #include <cassert> |
---|
| 25 | |
---|
| 26 | // Initializer: base class for object initializers (provide default values) |
---|
[d9a0e76] | 27 | class Initializer { |
---|
| 28 | public: |
---|
[0dd3a2f] | 29 | // Initializer( std::string _name = std::string(""), int _pos = 0 ); |
---|
[974906e2] | 30 | Initializer( bool maybeConstructed ); |
---|
[70f89d00] | 31 | Initializer( const Initializer & other ); |
---|
[0dd3a2f] | 32 | virtual ~Initializer(); |
---|
| 33 | |
---|
| 34 | static std::string designator_name( Expression *designator ); |
---|
| 35 | |
---|
| 36 | // void set_name( std::string newValue ) { name = newValue; } |
---|
| 37 | // std::string get_name() const { return name; } |
---|
| 38 | |
---|
| 39 | // void set_pos( int newValue ) { pos = newValue; } |
---|
| 40 | // int get_pos() const { return pos; } |
---|
| 41 | virtual void set_designators( std::list<Expression *> & ) { assert(false); } |
---|
| 42 | virtual std::list<Expression *> &get_designators() { |
---|
| 43 | assert(false); |
---|
| 44 | std::list<Expression *> *ret = 0; return *ret; // never reached |
---|
| 45 | } |
---|
| 46 | |
---|
[974906e2] | 47 | bool get_maybeConstructed() { return maybeConstructed; } |
---|
| 48 | |
---|
[0dd3a2f] | 49 | virtual Initializer *clone() const = 0; |
---|
| 50 | virtual void accept( Visitor &v ) = 0; |
---|
| 51 | virtual Initializer *acceptMutator( Mutator &m ) = 0; |
---|
| 52 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
[d9a0e76] | 53 | private: |
---|
[0dd3a2f] | 54 | // std::string name; |
---|
| 55 | // int pos; |
---|
[974906e2] | 56 | bool maybeConstructed; |
---|
[51b7345] | 57 | }; |
---|
| 58 | |
---|
| 59 | // SingleInit represents an initializer for a common object (e.g., int x = 4) |
---|
[d9a0e76] | 60 | class SingleInit : public Initializer { |
---|
| 61 | public: |
---|
[db4ecc5] | 62 | SingleInit( Expression *value, const std::list< Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false ); |
---|
[0dd3a2f] | 63 | SingleInit( const SingleInit &other ); |
---|
| 64 | virtual ~SingleInit(); |
---|
[974906e2] | 65 | |
---|
[0dd3a2f] | 66 | Expression *get_value() { return value; } |
---|
| 67 | void set_value( Expression *newValue ) { value = newValue; } |
---|
| 68 | |
---|
| 69 | void set_designators( std::list<Expression *> &newValue ) { designators = newValue; } |
---|
| 70 | std::list<Expression *> &get_designators() { return designators; } |
---|
| 71 | |
---|
[70f89d00] | 72 | virtual SingleInit *clone() const { return new SingleInit( *this); } |
---|
[0dd3a2f] | 73 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
| 74 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
| 75 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
[d9a0e76] | 76 | private: |
---|
[0dd3a2f] | 77 | //Constant *value; |
---|
| 78 | Expression *value; // has to be a compile-time constant |
---|
| 79 | std::list< Expression * > designators; |
---|
[51b7345] | 80 | }; |
---|
| 81 | |
---|
[d9a0e76] | 82 | // ListInit represents an initializer that is composed recursively of a list of initializers; this is used to initialize |
---|
| 83 | // an array or aggregate |
---|
| 84 | class ListInit : public Initializer { |
---|
| 85 | public: |
---|
[5b40f30] | 86 | ListInit( const std::list<Initializer*> &initializers, |
---|
[db4ecc5] | 87 | const std::list<Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false ); |
---|
[0dd3a2f] | 88 | virtual ~ListInit(); |
---|
| 89 | |
---|
| 90 | void set_designators( std::list<Expression *> &newValue ) { designators = newValue; } |
---|
| 91 | std::list<Expression *> &get_designators() { return designators; } |
---|
| 92 | void set_initializers( std::list<Initializer*> &newValue ) { initializers = newValue; } |
---|
| 93 | std::list<Initializer*> &get_initializers() { return initializers; } |
---|
| 94 | |
---|
| 95 | std::list<Initializer*>::iterator begin_initializers() { return initializers.begin(); } |
---|
| 96 | std::list<Initializer*>::iterator end_initializers() { return initializers.end(); } |
---|
| 97 | |
---|
[70f89d00] | 98 | virtual ListInit *clone() const { return new ListInit( *this ); } |
---|
[0dd3a2f] | 99 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
| 100 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
| 101 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
[d9a0e76] | 102 | private: |
---|
[0dd3a2f] | 103 | std::list<Initializer*> initializers; // order *is* important |
---|
| 104 | std::list<Expression *> designators; |
---|
[51b7345] | 105 | }; |
---|
| 106 | |
---|
[71f4e4f] | 107 | // ConstructorInit represents an initializer that is either a constructor expression or |
---|
| 108 | // a C-style initializer. |
---|
| 109 | class ConstructorInit : public Initializer { |
---|
| 110 | public: |
---|
[f1b1e4c] | 111 | ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init ); |
---|
[70f89d00] | 112 | ConstructorInit( const ConstructorInit &other ); |
---|
[71f4e4f] | 113 | virtual ~ConstructorInit(); |
---|
| 114 | |
---|
[5b2f5bb] | 115 | void set_ctor( Statement * newValue ) { ctor = newValue; } |
---|
| 116 | Statement * get_ctor() const { return ctor; } |
---|
| 117 | void set_dtor( Statement * newValue ) { dtor = newValue; } |
---|
| 118 | Statement * get_dtor() const { return dtor; } |
---|
[71f4e4f] | 119 | void set_init( Initializer * newValue ) { init = newValue; } |
---|
| 120 | Initializer * get_init() const { return init; } |
---|
| 121 | |
---|
[70f89d00] | 122 | ConstructorInit *clone() const { return new ConstructorInit( *this ); } |
---|
[71f4e4f] | 123 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
| 124 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
| 125 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
| 126 | |
---|
| 127 | private: |
---|
[5b2f5bb] | 128 | Statement * ctor; |
---|
| 129 | Statement * dtor; |
---|
[71f4e4f] | 130 | // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback |
---|
| 131 | // if an appropriate constructor definition is not found by the resolver |
---|
| 132 | Initializer * init; |
---|
| 133 | }; |
---|
| 134 | |
---|
[7a69460] | 135 | std::ostream & operator<<( std::ostream & out, Initializer * init ); |
---|
| 136 | |
---|
[d9a0e76] | 137 | #endif // INITIALIZER_H |
---|
[0dd3a2f] | 138 | |
---|
| 139 | // Local Variables: // |
---|
| 140 | // tab-width: 4 // |
---|
| 141 | // mode: c++ // |
---|
| 142 | // compile-command: "make install" // |
---|
| 143 | // End: // |
---|