| 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 | //
 | 
|---|
| 7 | // Initializer.cc --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Mon Aug 21 09:53:15 2017
 | 
|---|
| 13 | // Update Count     : 30
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "Initializer.h"
 | 
|---|
| 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
 | 
|---|
| 26 | 
 | 
|---|
| 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;
 | 
|---|
| 32 | }
 | 
|---|
| 33 | 
 | 
|---|
| 34 | Designation::~Designation() {
 | 
|---|
| 35 |         // std::cerr << "destroying designation" << std::endl;
 | 
|---|
| 36 |         deleteAll( designators );
 | 
|---|
| 37 |         // std::cerr << "finished destroying designation" << std::endl;
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | void Designation::print( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 41 |         if ( ! designators.empty() ) {
 | 
|---|
| 42 |                 os << "... designated by: " << std::endl;
 | 
|---|
| 43 |                 for ( const Expression * d : designators ) {
 | 
|---|
| 44 |                         os << indent+1;
 | 
|---|
| 45 |                         d->print(os, indent+1 );
 | 
|---|
| 46 |                         os << std::endl;
 | 
|---|
| 47 |                 }
 | 
|---|
| 48 |         } // if
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 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 ) {
 | 
|---|
| 57 | }
 | 
|---|
| 58 | 
 | 
|---|
| 59 | SingleInit::SingleInit( const SingleInit &other ) : Initializer(other), value ( maybeClone( other.value ) ) {
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | SingleInit::~SingleInit() {
 | 
|---|
| 63 |         delete value;
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | void SingleInit::print( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 67 |         os << "Simple Initializer: ";
 | 
|---|
| 68 |         value->print( os, indent );
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | 
 | 
|---|
| 72 | ListInit::ListInit( const std::list<Initializer*> &inits, const std::list<Designation *> &des, bool maybeConstructed )
 | 
|---|
| 73 |         : Initializer( maybeConstructed ), initializers( inits ), designations( des ) {
 | 
|---|
| 74 |                 // 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
 | 
|---|
| 75 |                 if ( designations.empty() ) {
 | 
|---|
| 76 |                         for ( auto & i : initializers ) {
 | 
|---|
| 77 |                                 (void)i;
 | 
|---|
| 78 |                                 designations.push_back( new Designation( {} ) );
 | 
|---|
| 79 |                         }
 | 
|---|
| 80 |                 }
 | 
|---|
| 81 |                 assertf( initializers.size() == designations.size(), "Created ListInit with mismatching initializers (%zd) and designations (%zd)", initializers.size(), designations.size() );
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | ListInit::ListInit( const ListInit & other ) : Initializer( other ) {
 | 
|---|
| 85 |         cloneAll( other.initializers, initializers );
 | 
|---|
| 86 |         cloneAll( other.designations, designations );
 | 
|---|
| 87 | }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | ListInit::~ListInit() {
 | 
|---|
| 90 |         deleteAll( initializers );
 | 
|---|
| 91 |         deleteAll( designations );
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | void ListInit::print( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 95 |         os << "Compound initializer: " << std::endl;
 | 
|---|
| 96 |         for ( auto p : group_iterate( designations, initializers ) ) {
 | 
|---|
| 97 |                 const Designation * d = std::get<0>(p);
 | 
|---|
| 98 |                 const Initializer * init = std::get<1>(p);
 | 
|---|
| 99 |                 os << indent+1;
 | 
|---|
| 100 |                 init->print( os, indent+1 );
 | 
|---|
| 101 |                 os << std::endl;
 | 
|---|
| 102 |                 if ( ! d->designators.empty() ) {
 | 
|---|
| 103 |                         os << indent+1;
 | 
|---|
| 104 |                         d->print( os, indent+1 );
 | 
|---|
| 105 |                 }
 | 
|---|
| 106 |         }
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | 
 | 
|---|
| 110 | ConstructorInit::ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init ) : Initializer( true ), ctor( ctor ), dtor( dtor ), init( init ) {}
 | 
|---|
| 111 | ConstructorInit::ConstructorInit( const ConstructorInit &other ) : Initializer( other ), ctor( maybeClone( other.ctor ) ), dtor( maybeClone( other.dtor ) ), init( maybeClone( other.init ) ) {
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | ConstructorInit::~ConstructorInit() {
 | 
|---|
| 115 |         delete ctor;
 | 
|---|
| 116 |         delete dtor;
 | 
|---|
| 117 |         delete init;
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|
| 120 | void ConstructorInit::print( std::ostream &os, Indenter indent ) const {
 | 
|---|
| 121 |         os << "Constructor initializer: " << std::endl;
 | 
|---|
| 122 |         if ( ctor ) {
 | 
|---|
| 123 |                 os << indent << "... initially constructed with ";
 | 
|---|
| 124 |                 ctor->print( os, indent+1 );
 | 
|---|
| 125 |         } // if
 | 
|---|
| 126 | 
 | 
|---|
| 127 |         if ( dtor ) {
 | 
|---|
| 128 |                 os << indent << "... destructed with ";
 | 
|---|
| 129 |                 dtor->print( os, indent+1 );
 | 
|---|
| 130 |         }
 | 
|---|
| 131 | 
 | 
|---|
| 132 |         if ( init ) {
 | 
|---|
| 133 |                 os << indent << "... with fallback C-style initializer: ";
 | 
|---|
| 134 |                 init->print( os, indent+1 );
 | 
|---|
| 135 |         }
 | 
|---|
| 136 | }
 | 
|---|
| 137 | 
 | 
|---|
| 138 | // Local Variables: //
 | 
|---|
| 139 | // tab-width: 4 //
 | 
|---|
| 140 | // mode: c++ //
 | 
|---|
| 141 | // compile-command: "make install" //
 | 
|---|
| 142 | // End: //
 | 
|---|