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