[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.cc --
|
---|
[0dd3a2f] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[d58ebf3] | 11 | // Last Modified By : Rob Schluntz
|
---|
[d668182] | 12 | // Last Modified On : Fri May 13 13:23:03 2016
|
---|
[71f4e4f] | 13 | // Update Count : 28
|
---|
[0dd3a2f] | 14 | //
|
---|
| 15 |
|
---|
[51b73452] | 16 | #include "Initializer.h"
|
---|
| 17 | #include "Expression.h"
|
---|
[5b2f5bb] | 18 | #include "Statement.h"
|
---|
[d3b7937] | 19 | #include "Common/utility.h"
|
---|
[51b73452] | 20 |
|
---|
[e4d829b] | 21 | Designation::Designation( const std::list< Expression * > & designators ) : designators( designators ) {}
|
---|
| 22 | Designation::Designation( const Designation & other ) : BaseSyntaxNode( other ) {
|
---|
| 23 | // std::cerr << "cloning designation" << std::endl;
|
---|
| 24 | cloneAll( other.designators, designators );
|
---|
| 25 | // std::cerr << "finished cloning designation" << std::endl;
|
---|
[70f89d00] | 26 | }
|
---|
| 27 |
|
---|
[e4d829b] | 28 | Designation::~Designation() {
|
---|
| 29 | // std::cerr << "destroying designation" << std::endl;
|
---|
| 30 | deleteAll( designators );
|
---|
| 31 | // std::cerr << "finished destroying designation" << std::endl;
|
---|
| 32 | }
|
---|
[51b73452] | 33 |
|
---|
[e4d829b] | 34 | void Designation::print( std::ostream &os, int indent ) const {
|
---|
| 35 | if ( ! designators.empty() ) {
|
---|
| 36 | os << std::string(indent + 2, ' ' ) << "designated by: " << std::endl;
|
---|
| 37 | for ( std::list < Expression * >::const_iterator i = designators.begin(); i != designators.end(); i++ ) {
|
---|
| 38 | os << std::string(indent + 4, ' ' );
|
---|
| 39 | ( *i )->print(os, indent + 4 );
|
---|
| 40 | }
|
---|
| 41 | os << std::endl;
|
---|
| 42 | } // if
|
---|
[51b73452] | 43 | }
|
---|
| 44 |
|
---|
[e4d829b] | 45 | Initializer::Initializer( bool maybeConstructed ) : maybeConstructed( maybeConstructed ) {}
|
---|
| 46 | Initializer::Initializer( const Initializer & other ) : BaseSyntaxNode( other ), maybeConstructed( other.maybeConstructed ) {
|
---|
| 47 | }
|
---|
| 48 | Initializer::~Initializer() {}
|
---|
| 49 |
|
---|
| 50 | SingleInit::SingleInit( Expression *v, bool maybeConstructed ) : Initializer( maybeConstructed ), value ( v ) {
|
---|
[51b73452] | 51 | }
|
---|
| 52 |
|
---|
[a5a71d0] | 53 | SingleInit::SingleInit( const SingleInit &other ) : Initializer(other), value ( maybeClone( other.value ) ) {
|
---|
[51b73452] | 54 | }
|
---|
| 55 |
|
---|
[70f89d00] | 56 | SingleInit::~SingleInit() {
|
---|
[837a17c] | 57 | delete value;
|
---|
[70f89d00] | 58 | }
|
---|
[51b73452] | 59 |
|
---|
[e4d829b] | 60 | void SingleInit::print( std::ostream &os, int indent ) const {
|
---|
| 61 | os << std::string(indent, ' ' ) << "Simple Initializer: " << std::endl;
|
---|
[60089f4] | 62 | os << std::string(indent+4, ' ' );
|
---|
[d58ebf3] | 63 | value->print( os, indent+4 );
|
---|
[51b73452] | 64 | }
|
---|
| 65 |
|
---|
[e4d829b] | 66 |
|
---|
[62423350] | 67 | ListInit::ListInit( const std::list<Initializer*> &inits, const std::list<Designation *> &des, bool maybeConstructed )
|
---|
| 68 | : Initializer( maybeConstructed ), initializers( inits ), designations( des ) {
|
---|
| 69 | // handle the common case where a ListInit is created without designations by making a list of empty designations with the same length as the initializer
|
---|
| 70 | if ( designations.empty() ) {
|
---|
| 71 | for ( auto & i : initializers ) {
|
---|
| 72 | (void)i;
|
---|
| 73 | designations.push_back( new Designation( {} ) );
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | assertf( initializers.size() == designations.size(), "Created ListInit with mismatching initializers (%d) and designations (%d)", initializers.size(), designations.size() );
|
---|
[51b73452] | 77 | }
|
---|
| 78 |
|
---|
[fc638d2] | 79 | ListInit::ListInit( const ListInit & other ) : Initializer( other ) {
|
---|
| 80 | cloneAll( other.initializers, initializers );
|
---|
[e4d829b] | 81 | cloneAll( other.designations, designations );
|
---|
[fc638d2] | 82 | }
|
---|
| 83 |
|
---|
[70f89d00] | 84 | ListInit::~ListInit() {
|
---|
| 85 | deleteAll( initializers );
|
---|
[e4d829b] | 86 | deleteAll( designations );
|
---|
[51b73452] | 87 | }
|
---|
| 88 |
|
---|
[e4d829b] | 89 | void ListInit::print( std::ostream &os, int indent ) const {
|
---|
| 90 | os << std::string(indent, ' ') << "Compound initializer: " << std::endl;
|
---|
| 91 | for ( Designation * d : designations ) {
|
---|
| 92 | d->print( os, indent + 2 );
|
---|
| 93 | }
|
---|
[51b73452] | 94 |
|
---|
[e4d829b] | 95 | for ( const Initializer * init : initializers ) {
|
---|
| 96 | init->print( os, indent + 2 );
|
---|
| 97 | os << std::endl;
|
---|
| 98 | }
|
---|
[51b73452] | 99 | }
|
---|
[71f4e4f] | 100 |
|
---|
| 101 |
|
---|
[f1b1e4c] | 102 | ConstructorInit::ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init ) : Initializer( true ), ctor( ctor ), dtor( dtor ), init( init ) {}
|
---|
| 103 | ConstructorInit::ConstructorInit( const ConstructorInit &other ) : Initializer( other ), ctor( maybeClone( other.ctor ) ), dtor( maybeClone( other.dtor ) ), init( maybeClone( other.init ) ) {
|
---|
[70f89d00] | 104 | }
|
---|
| 105 |
|
---|
[71f4e4f] | 106 | ConstructorInit::~ConstructorInit() {
|
---|
| 107 | delete ctor;
|
---|
[70f89d00] | 108 | delete dtor;
|
---|
[71f4e4f] | 109 | delete init;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[e4d829b] | 112 | void ConstructorInit::print( std::ostream &os, int indent ) const {
|
---|
[84bb4d9] | 113 | os << std::endl << std::string(indent, ' ') << "Constructor initializer: " << std::endl;
|
---|
[71f4e4f] | 114 | if ( ctor ) {
|
---|
[84bb4d9] | 115 | os << std::string(indent+2, ' ');
|
---|
| 116 | os << "initially constructed with ";
|
---|
| 117 | ctor->print( os, indent+4 );
|
---|
[71f4e4f] | 118 | } // if
|
---|
| 119 |
|
---|
[5b2f5bb] | 120 | if ( dtor ) {
|
---|
[84bb4d9] | 121 | os << std::string(indent+2, ' ');
|
---|
| 122 | os << "destructed with ";
|
---|
| 123 | dtor->print( os, indent+4 );
|
---|
[5b2f5bb] | 124 | }
|
---|
| 125 |
|
---|
[71f4e4f] | 126 | if ( init ) {
|
---|
[84bb4d9] | 127 | os << std::string(indent+2, ' ');
|
---|
| 128 | os << "with fallback C-style initializer: ";
|
---|
| 129 | init->print( os, indent+4 );
|
---|
[71f4e4f] | 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[e4d829b] | 133 | std::ostream & operator<<( std::ostream & out, const Initializer * init ) {
|
---|
| 134 | if ( init ) {
|
---|
| 135 | init->print( out );
|
---|
| 136 | } else {
|
---|
| 137 | out << "nullptr";
|
---|
| 138 | }
|
---|
| 139 | return out;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | std::ostream & operator<<( std::ostream & out, const Designation * des ) {
|
---|
| 143 | if ( des ) {
|
---|
| 144 | des->print( out );
|
---|
| 145 | } else {
|
---|
| 146 | out << "nullptr";
|
---|
| 147 | }
|
---|
[7a69460] | 148 | return out;
|
---|
| 149 | }
|
---|
[71f4e4f] | 150 |
|
---|
[0dd3a2f] | 151 | // Local Variables: //
|
---|
| 152 | // tab-width: 4 //
|
---|
| 153 | // mode: c++ //
|
---|
| 154 | // compile-command: "make install" //
|
---|
| 155 | // End: //
|
---|