[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 | |
---|
[f0121d7] | 26 | const std::list<Expression*> noDesignators; |
---|
| 27 | |
---|
[51b7345] | 28 | // Initializer: base class for object initializers (provide default values) |
---|
[d9a0e76] | 29 | class Initializer { |
---|
| 30 | public: |
---|
[0dd3a2f] | 31 | // Initializer( std::string _name = std::string(""), int _pos = 0 ); |
---|
[974906e2] | 32 | Initializer( bool maybeConstructed ); |
---|
[70f89d00] | 33 | Initializer( const Initializer & other ); |
---|
[0dd3a2f] | 34 | virtual ~Initializer(); |
---|
| 35 | |
---|
| 36 | static std::string designator_name( Expression *designator ); |
---|
| 37 | |
---|
| 38 | // void set_name( std::string newValue ) { name = newValue; } |
---|
| 39 | // std::string get_name() const { return name; } |
---|
| 40 | |
---|
| 41 | // void set_pos( int newValue ) { pos = newValue; } |
---|
| 42 | // int get_pos() const { return pos; } |
---|
| 43 | virtual void set_designators( std::list<Expression *> & ) { assert(false); } |
---|
| 44 | virtual std::list<Expression *> &get_designators() { |
---|
| 45 | assert(false); |
---|
| 46 | std::list<Expression *> *ret = 0; return *ret; // never reached |
---|
| 47 | } |
---|
| 48 | |
---|
[974906e2] | 49 | bool get_maybeConstructed() { return maybeConstructed; } |
---|
| 50 | |
---|
[0dd3a2f] | 51 | virtual Initializer *clone() const = 0; |
---|
| 52 | virtual void accept( Visitor &v ) = 0; |
---|
| 53 | virtual Initializer *acceptMutator( Mutator &m ) = 0; |
---|
| 54 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
[d9a0e76] | 55 | private: |
---|
[0dd3a2f] | 56 | // std::string name; |
---|
| 57 | // int pos; |
---|
[974906e2] | 58 | bool maybeConstructed; |
---|
[51b7345] | 59 | }; |
---|
| 60 | |
---|
| 61 | // SingleInit represents an initializer for a common object (e.g., int x = 4) |
---|
[d9a0e76] | 62 | class SingleInit : public Initializer { |
---|
| 63 | public: |
---|
[db4ecc5] | 64 | SingleInit( Expression *value, const std::list< Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false ); |
---|
[0dd3a2f] | 65 | SingleInit( const SingleInit &other ); |
---|
| 66 | virtual ~SingleInit(); |
---|
[974906e2] | 67 | |
---|
[0dd3a2f] | 68 | Expression *get_value() { return value; } |
---|
| 69 | void set_value( Expression *newValue ) { value = newValue; } |
---|
| 70 | |
---|
| 71 | void set_designators( std::list<Expression *> &newValue ) { designators = newValue; } |
---|
| 72 | std::list<Expression *> &get_designators() { return designators; } |
---|
| 73 | |
---|
[70f89d00] | 74 | virtual SingleInit *clone() const { return new SingleInit( *this); } |
---|
[0dd3a2f] | 75 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
| 76 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
| 77 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
[d9a0e76] | 78 | private: |
---|
[0dd3a2f] | 79 | //Constant *value; |
---|
| 80 | Expression *value; // has to be a compile-time constant |
---|
| 81 | std::list< Expression * > designators; |
---|
[51b7345] | 82 | }; |
---|
| 83 | |
---|
[d9a0e76] | 84 | // ListInit represents an initializer that is composed recursively of a list of initializers; this is used to initialize |
---|
| 85 | // an array or aggregate |
---|
| 86 | class ListInit : public Initializer { |
---|
| 87 | public: |
---|
[5b40f30] | 88 | ListInit( const std::list<Initializer*> &initializers, |
---|
[db4ecc5] | 89 | const std::list<Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false ); |
---|
[fc638d2] | 90 | ListInit( const ListInit & other ); |
---|
[0dd3a2f] | 91 | virtual ~ListInit(); |
---|
| 92 | |
---|
| 93 | void set_designators( std::list<Expression *> &newValue ) { designators = newValue; } |
---|
| 94 | std::list<Expression *> &get_designators() { return designators; } |
---|
| 95 | void set_initializers( std::list<Initializer*> &newValue ) { initializers = newValue; } |
---|
| 96 | std::list<Initializer*> &get_initializers() { return initializers; } |
---|
| 97 | |
---|
[4d2434a] | 98 | typedef std::list<Initializer*>::iterator iterator; |
---|
| 99 | iterator begin() { return initializers.begin(); } |
---|
| 100 | iterator end() { return initializers.end(); } |
---|
[0dd3a2f] | 101 | |
---|
[70f89d00] | 102 | virtual ListInit *clone() const { return new ListInit( *this ); } |
---|
[0dd3a2f] | 103 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
| 104 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
| 105 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
[d9a0e76] | 106 | private: |
---|
[0dd3a2f] | 107 | std::list<Initializer*> initializers; // order *is* important |
---|
| 108 | std::list<Expression *> designators; |
---|
[51b7345] | 109 | }; |
---|
| 110 | |
---|
[71f4e4f] | 111 | // ConstructorInit represents an initializer that is either a constructor expression or |
---|
| 112 | // a C-style initializer. |
---|
| 113 | class ConstructorInit : public Initializer { |
---|
| 114 | public: |
---|
[f1b1e4c] | 115 | ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init ); |
---|
[70f89d00] | 116 | ConstructorInit( const ConstructorInit &other ); |
---|
[71f4e4f] | 117 | virtual ~ConstructorInit(); |
---|
| 118 | |
---|
[5b2f5bb] | 119 | void set_ctor( Statement * newValue ) { ctor = newValue; } |
---|
| 120 | Statement * get_ctor() const { return ctor; } |
---|
| 121 | void set_dtor( Statement * newValue ) { dtor = newValue; } |
---|
| 122 | Statement * get_dtor() const { return dtor; } |
---|
[71f4e4f] | 123 | void set_init( Initializer * newValue ) { init = newValue; } |
---|
| 124 | Initializer * get_init() const { return init; } |
---|
| 125 | |
---|
[70f89d00] | 126 | ConstructorInit *clone() const { return new ConstructorInit( *this ); } |
---|
[71f4e4f] | 127 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
| 128 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
| 129 | virtual void print( std::ostream &os, int indent = 0 ); |
---|
| 130 | |
---|
| 131 | private: |
---|
[5b2f5bb] | 132 | Statement * ctor; |
---|
| 133 | Statement * dtor; |
---|
[71f4e4f] | 134 | // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback |
---|
| 135 | // if an appropriate constructor definition is not found by the resolver |
---|
| 136 | Initializer * init; |
---|
| 137 | }; |
---|
| 138 | |
---|
[7a69460] | 139 | std::ostream & operator<<( std::ostream & out, Initializer * init ); |
---|
| 140 | |
---|
[d9a0e76] | 141 | #endif // INITIALIZER_H |
---|
[0dd3a2f] | 142 | |
---|
| 143 | // Local Variables: // |
---|
| 144 | // tab-width: 4 // |
---|
| 145 | // mode: c++ // |
---|
| 146 | // compile-command: "make install" // |
---|
| 147 | // End: // |
---|