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