[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
|
---|
[6013bd7] | 11 | // Last Modified By : Peter A. Buhr
|
---|
| 12 | // Last Modified On : Thu Mar 23 16:12:42 2017
|
---|
| 13 | // Update Count : 20
|
---|
[0dd3a2f] | 14 | //
|
---|
| 15 |
|
---|
[51b73452] | 16 | #ifndef INITIALIZER_H
|
---|
| 17 | #define INITIALIZER_H
|
---|
| 18 |
|
---|
[294647b] | 19 | #include <cassert>
|
---|
| 20 |
|
---|
| 21 | #include "BaseSyntaxNode.h"
|
---|
[51b73452] | 22 | #include "Mutator.h"
|
---|
[294647b] | 23 | #include "SynTree.h"
|
---|
[70f89d00] | 24 | #include "Type.h"
|
---|
[294647b] | 25 | #include "Visitor.h"
|
---|
[51b73452] | 26 |
|
---|
[e4d829b] | 27 | // Designation: list of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an object being initialized.
|
---|
| 28 | class Designation : public BaseSyntaxNode {
|
---|
| 29 | public:
|
---|
| 30 | Designation( const std::list< Expression * > & designators );
|
---|
| 31 | Designation( const Designation & other );
|
---|
| 32 | virtual ~Designation();
|
---|
| 33 |
|
---|
| 34 | std::list< Expression * > & get_designators() { return designators; }
|
---|
| 35 |
|
---|
| 36 | virtual Designation * clone() const { return new Designation( *this ); };
|
---|
| 37 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
| 38 | virtual Designation * acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
| 39 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
| 40 | private:
|
---|
| 41 | std::list< Expression * > designators;
|
---|
| 42 | };
|
---|
| 43 |
|
---|
| 44 | const std::list<Designation *> noDesignators;
|
---|
[f0121d7] | 45 |
|
---|
[51b73452] | 46 | // Initializer: base class for object initializers (provide default values)
|
---|
[294647b] | 47 | class Initializer : public BaseSyntaxNode {
|
---|
[d9a0e76] | 48 | public:
|
---|
[974906e2] | 49 | Initializer( bool maybeConstructed );
|
---|
[70f89d00] | 50 | Initializer( const Initializer & other );
|
---|
[0dd3a2f] | 51 | virtual ~Initializer();
|
---|
| 52 |
|
---|
[974906e2] | 53 | bool get_maybeConstructed() { return maybeConstructed; }
|
---|
| 54 |
|
---|
[0dd3a2f] | 55 | virtual Initializer *clone() const = 0;
|
---|
| 56 | virtual void accept( Visitor &v ) = 0;
|
---|
| 57 | virtual Initializer *acceptMutator( Mutator &m ) = 0;
|
---|
[e4d829b] | 58 | virtual void print( std::ostream &os, int indent = 0 ) const = 0;
|
---|
[d9a0e76] | 59 | private:
|
---|
[974906e2] | 60 | bool maybeConstructed;
|
---|
[51b73452] | 61 | };
|
---|
| 62 |
|
---|
| 63 | // SingleInit represents an initializer for a common object (e.g., int x = 4)
|
---|
[d9a0e76] | 64 | class SingleInit : public Initializer {
|
---|
| 65 | public:
|
---|
[e4d829b] | 66 | SingleInit( Expression *value, bool maybeConstructed = false );
|
---|
[0dd3a2f] | 67 | SingleInit( const SingleInit &other );
|
---|
| 68 | virtual ~SingleInit();
|
---|
[974906e2] | 69 |
|
---|
[0dd3a2f] | 70 | Expression *get_value() { return value; }
|
---|
| 71 | void set_value( Expression *newValue ) { value = newValue; }
|
---|
| 72 |
|
---|
[70f89d00] | 73 | virtual SingleInit *clone() const { return new SingleInit( *this); }
|
---|
[0dd3a2f] | 74 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
| 75 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
[e4d829b] | 76 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
[d9a0e76] | 77 | private:
|
---|
[0dd3a2f] | 78 | //Constant *value;
|
---|
| 79 | Expression *value; // has to be a compile-time constant
|
---|
[51b73452] | 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,
|
---|
[e4d829b] | 87 | const std::list<Designation *> &designators = {}, bool maybeConstructed = false );
|
---|
[fc638d2] | 88 | ListInit( const ListInit & other );
|
---|
[0dd3a2f] | 89 | virtual ~ListInit();
|
---|
| 90 |
|
---|
[e4d829b] | 91 | std::list<Designation *> & get_designations() { return designations; }
|
---|
| 92 | std::list<Initializer *> & get_initializers() { return initializers; }
|
---|
[0dd3a2f] | 93 |
|
---|
[4d2434a] | 94 | typedef std::list<Initializer*>::iterator iterator;
|
---|
[e4d829b] | 95 | typedef std::list<Initializer*>::const_iterator const_iterator;
|
---|
[4d2434a] | 96 | iterator begin() { return initializers.begin(); }
|
---|
| 97 | iterator end() { return initializers.end(); }
|
---|
[e4d829b] | 98 | const_iterator begin() const { return initializers.begin(); }
|
---|
| 99 | const_iterator end() const { return initializers.end(); }
|
---|
[0dd3a2f] | 100 |
|
---|
[70f89d00] | 101 | virtual ListInit *clone() const { return new ListInit( *this ); }
|
---|
[0dd3a2f] | 102 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
| 103 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
[e4d829b] | 104 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
[d9a0e76] | 105 | private:
|
---|
[e4d829b] | 106 | std::list<Initializer *> initializers; // order *is* important
|
---|
| 107 | std::list<Designation *> designations; // order/length is consistent with initializers
|
---|
[51b73452] | 108 | };
|
---|
| 109 |
|
---|
[71f4e4f] | 110 | // ConstructorInit represents an initializer that is either a constructor expression or
|
---|
| 111 | // a C-style initializer.
|
---|
[64ac636] | 112 | // It should not be necessary to create ConstructorInit nodes manually. Instead, set maybeConstructed
|
---|
| 113 | // to true on SingleInit or ListInit constructors if object should be constructed.
|
---|
[71f4e4f] | 114 | class ConstructorInit : public Initializer {
|
---|
| 115 | public:
|
---|
[f1b1e4c] | 116 | ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init );
|
---|
[70f89d00] | 117 | ConstructorInit( const ConstructorInit &other );
|
---|
[71f4e4f] | 118 | virtual ~ConstructorInit();
|
---|
| 119 |
|
---|
[5b2f5bb] | 120 | void set_ctor( Statement * newValue ) { ctor = newValue; }
|
---|
| 121 | Statement * get_ctor() const { return ctor; }
|
---|
| 122 | void set_dtor( Statement * newValue ) { dtor = newValue; }
|
---|
| 123 | Statement * get_dtor() const { return dtor; }
|
---|
[71f4e4f] | 124 | void set_init( Initializer * newValue ) { init = newValue; }
|
---|
| 125 | Initializer * get_init() const { return init; }
|
---|
| 126 |
|
---|
[70f89d00] | 127 | ConstructorInit *clone() const { return new ConstructorInit( *this ); }
|
---|
[71f4e4f] | 128 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
| 129 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
[e4d829b] | 130 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
[71f4e4f] | 131 |
|
---|
| 132 | private:
|
---|
[5b2f5bb] | 133 | Statement * ctor;
|
---|
| 134 | Statement * dtor;
|
---|
[71f4e4f] | 135 | // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback
|
---|
| 136 | // if an appropriate constructor definition is not found by the resolver
|
---|
| 137 | Initializer * init;
|
---|
| 138 | };
|
---|
| 139 |
|
---|
[e4d829b] | 140 | std::ostream & operator<<( std::ostream & out, const Initializer * init );
|
---|
| 141 | std::ostream & operator<<( std::ostream & out, const Designation * des );
|
---|
[7a69460] | 142 |
|
---|
[d9a0e76] | 143 | #endif // INITIALIZER_H
|
---|
[0dd3a2f] | 144 |
|
---|
| 145 | // Local Variables: //
|
---|
| 146 | // tab-width: 4 //
|
---|
| 147 | // mode: c++ //
|
---|
| 148 | // compile-command: "make install" //
|
---|
| 149 | // End: //
|
---|