[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 | |
---|
[51b7345] | 16 | #include "Initializer.h" |
---|
| 17 | #include "Expression.h" |
---|
[5b2f5bb] | 18 | #include "Statement.h" |
---|
[d3b7937] | 19 | #include "Common/utility.h" |
---|
[51b7345] | 20 | |
---|
[974906e2] | 21 | Initializer::Initializer( bool maybeConstructed ) : maybeConstructed( maybeConstructed ) {} |
---|
[64ac636] | 22 | Initializer::Initializer( const Initializer & other ) : BaseSyntaxNode( other ), maybeConstructed( other.maybeConstructed ) { |
---|
[70f89d00] | 23 | } |
---|
| 24 | |
---|
[51b7345] | 25 | |
---|
[d9a0e76] | 26 | Initializer::~Initializer() {} |
---|
[51b7345] | 27 | |
---|
| 28 | std::string Initializer::designator_name( Expression *des ) { |
---|
[0dd3a2f] | 29 | if ( NameExpr *n = dynamic_cast<NameExpr *>(des) ) |
---|
| 30 | return n->get_name(); |
---|
| 31 | else |
---|
| 32 | throw 0; |
---|
[51b7345] | 33 | } |
---|
| 34 | |
---|
[d7dc824] | 35 | // void Initializer::print( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent ) {} |
---|
[51b7345] | 36 | |
---|
[620cb95] | 37 | SingleInit::SingleInit( Expression *v, const std::list< Expression *> &_designators, bool maybeConstructed ) : Initializer( maybeConstructed ), value ( v ), designators( _designators ) { |
---|
[51b7345] | 38 | } |
---|
| 39 | |
---|
[a5a71d0] | 40 | SingleInit::SingleInit( const SingleInit &other ) : Initializer(other), value ( maybeClone( other.value ) ) { |
---|
[0dd3a2f] | 41 | cloneAll(other.designators, designators ); |
---|
[51b7345] | 42 | } |
---|
| 43 | |
---|
[70f89d00] | 44 | SingleInit::~SingleInit() { |
---|
[837a17c] | 45 | delete value; |
---|
[70f89d00] | 46 | deleteAll(designators); |
---|
| 47 | } |
---|
[51b7345] | 48 | |
---|
[d9a0e76] | 49 | void SingleInit::print( std::ostream &os, int indent ) { |
---|
[d58ebf3] | 50 | os << std::endl << std::string(indent, ' ' ) << "Simple Initializer: " << std::endl; |
---|
[60089f4] | 51 | os << std::string(indent+4, ' ' ); |
---|
[d58ebf3] | 52 | value->print( os, indent+4 ); |
---|
[0dd3a2f] | 53 | |
---|
| 54 | if ( ! designators.empty() ) { |
---|
[bb8ea30] | 55 | os << std::endl << std::string(indent + 2, ' ' ) << "designated by: " << std::endl; |
---|
[d58ebf3] | 56 | for ( std::list < Expression * >::iterator i = designators.begin(); i != designators.end(); i++ ) { |
---|
[bb8ea30] | 57 | os << std::string(indent + 4, ' ' ); |
---|
[0dd3a2f] | 58 | ( *i )->print(os, indent + 4 ); |
---|
[d58ebf3] | 59 | } |
---|
[0dd3a2f] | 60 | } // if |
---|
[51b7345] | 61 | } |
---|
| 62 | |
---|
[5b40f30] | 63 | ListInit::ListInit( const std::list<Initializer*> &_initializers, const std::list<Expression *> &_designators, bool maybeConstructed ) |
---|
[70f89d00] | 64 | : Initializer( maybeConstructed ), initializers( _initializers ), designators( _designators ) { |
---|
[51b7345] | 65 | } |
---|
| 66 | |
---|
[fc638d2] | 67 | ListInit::ListInit( const ListInit & other ) : Initializer( other ) { |
---|
| 68 | cloneAll( other.initializers, initializers ); |
---|
| 69 | cloneAll( other.designators, designators ); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
[70f89d00] | 73 | ListInit::~ListInit() { |
---|
| 74 | deleteAll( initializers ); |
---|
| 75 | deleteAll( designators ); |
---|
[51b7345] | 76 | } |
---|
| 77 | |
---|
[d9a0e76] | 78 | void ListInit::print( std::ostream &os, int indent ) { |
---|
[974906e2] | 79 | os << std::endl << std::string(indent, ' ') << "Compound initializer: "; |
---|
[0dd3a2f] | 80 | if ( ! designators.empty() ) { |
---|
| 81 | os << std::string(indent + 2, ' ' ) << "designated by: ["; |
---|
| 82 | for ( std::list < Expression * >::iterator i = designators.begin(); |
---|
| 83 | i != designators.end(); i++ ) { |
---|
[974906e2] | 84 | ( *i )->print(os, indent + 4 ); |
---|
[0dd3a2f] | 85 | } // for |
---|
[974906e2] | 86 | |
---|
[0dd3a2f] | 87 | os << std::string(indent + 2, ' ' ) << "]"; |
---|
| 88 | } // if |
---|
[51b7345] | 89 | |
---|
[974906e2] | 90 | for ( std::list<Initializer *>::iterator i = initializers.begin(); i != initializers.end(); i++ ) |
---|
[0dd3a2f] | 91 | (*i)->print( os, indent + 2 ); |
---|
[51b7345] | 92 | } |
---|
[71f4e4f] | 93 | |
---|
| 94 | |
---|
[f1b1e4c] | 95 | ConstructorInit::ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init ) : Initializer( true ), ctor( ctor ), dtor( dtor ), init( init ) {} |
---|
| 96 | ConstructorInit::ConstructorInit( const ConstructorInit &other ) : Initializer( other ), ctor( maybeClone( other.ctor ) ), dtor( maybeClone( other.dtor ) ), init( maybeClone( other.init ) ) { |
---|
[70f89d00] | 97 | } |
---|
| 98 | |
---|
[71f4e4f] | 99 | ConstructorInit::~ConstructorInit() { |
---|
| 100 | delete ctor; |
---|
[70f89d00] | 101 | delete dtor; |
---|
[71f4e4f] | 102 | delete init; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void ConstructorInit::print( std::ostream &os, int indent ) { |
---|
[84bb4d9] | 106 | os << std::endl << std::string(indent, ' ') << "Constructor initializer: " << std::endl; |
---|
[71f4e4f] | 107 | if ( ctor ) { |
---|
[84bb4d9] | 108 | os << std::string(indent+2, ' '); |
---|
| 109 | os << "initially constructed with "; |
---|
| 110 | ctor->print( os, indent+4 ); |
---|
[71f4e4f] | 111 | } // if |
---|
| 112 | |
---|
[5b2f5bb] | 113 | if ( dtor ) { |
---|
[84bb4d9] | 114 | os << std::string(indent+2, ' '); |
---|
| 115 | os << "destructed with "; |
---|
| 116 | dtor->print( os, indent+4 ); |
---|
[5b2f5bb] | 117 | } |
---|
| 118 | |
---|
[71f4e4f] | 119 | if ( init ) { |
---|
[84bb4d9] | 120 | os << std::string(indent+2, ' '); |
---|
| 121 | os << "with fallback C-style initializer: "; |
---|
| 122 | init->print( os, indent+4 ); |
---|
[71f4e4f] | 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
[7a69460] | 126 | std::ostream & operator<<( std::ostream & out, Initializer * init ) { |
---|
| 127 | init->print( out ); |
---|
| 128 | return out; |
---|
| 129 | } |
---|
[71f4e4f] | 130 | |
---|
[0dd3a2f] | 131 | // Local Variables: // |
---|
| 132 | // tab-width: 4 // |
---|
| 133 | // mode: c++ // |
---|
| 134 | // compile-command: "make install" // |
---|
| 135 | // End: // |
---|