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