[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 |
|
---|
[50377a4] | 34 | void Designation::print( std::ostream &os, Indenter indent ) const {
|
---|
[e4d829b] | 35 | if ( ! designators.empty() ) {
|
---|
[50377a4] | 36 | os << "... designated by: " << std::endl;
|
---|
| 37 | for ( const Expression * d : designators ) {
|
---|
| 38 | os << indent+1;
|
---|
| 39 | d->print(os, indent+1 );
|
---|
| 40 | os << std::endl;
|
---|
[e4d829b] | 41 | }
|
---|
| 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 |
|
---|
| 49 | SingleInit::SingleInit( Expression *v, bool maybeConstructed ) : Initializer( maybeConstructed ), value ( v ) {
|
---|
[51b73452] | 50 | }
|
---|
| 51 |
|
---|
[a5a71d0] | 52 | SingleInit::SingleInit( const SingleInit &other ) : Initializer(other), value ( maybeClone( other.value ) ) {
|
---|
[51b73452] | 53 | }
|
---|
| 54 |
|
---|
[50377a4] | 55 | void SingleInit::print( std::ostream &os, Indenter indent ) const {
|
---|
| 56 | os << "Simple Initializer: ";
|
---|
| 57 | value->print( os, indent );
|
---|
[51b73452] | 58 | }
|
---|
| 59 |
|
---|
[e4d829b] | 60 |
|
---|
[62423350] | 61 | ListInit::ListInit( const std::list<Initializer*> &inits, const std::list<Designation *> &des, bool maybeConstructed )
|
---|
| 62 | : Initializer( maybeConstructed ), initializers( inits ), designations( des ) {
|
---|
| 63 | // 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
|
---|
| 64 | if ( designations.empty() ) {
|
---|
| 65 | for ( auto & i : initializers ) {
|
---|
| 66 | (void)i;
|
---|
| 67 | designations.push_back( new Designation( {} ) );
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
[d82daa1] | 70 | assertf( initializers.size() == designations.size(), "Created ListInit with mismatching initializers (%zd) and designations (%zd)", initializers.size(), designations.size() );
|
---|
[51b73452] | 71 | }
|
---|
| 72 |
|
---|
[fc638d2] | 73 | ListInit::ListInit( const ListInit & other ) : Initializer( other ) {
|
---|
| 74 | cloneAll( other.initializers, initializers );
|
---|
[e4d829b] | 75 | cloneAll( other.designations, designations );
|
---|
[fc638d2] | 76 | }
|
---|
| 77 |
|
---|
[50377a4] | 78 | void ListInit::print( std::ostream &os, Indenter indent ) const {
|
---|
| 79 | os << "Compound initializer: " << std::endl;
|
---|
| 80 | for ( auto p : group_iterate( designations, initializers ) ) {
|
---|
| 81 | const Designation * d = std::get<0>(p);
|
---|
| 82 | const Initializer * init = std::get<1>(p);
|
---|
| 83 | os << indent+1;
|
---|
| 84 | init->print( os, indent+1 );
|
---|
[e4d829b] | 85 | os << std::endl;
|
---|
[50377a4] | 86 | if ( ! d->designators.empty() ) {
|
---|
| 87 | os << indent+1;
|
---|
| 88 | d->print( os, indent+1 );
|
---|
| 89 | }
|
---|
[e4d829b] | 90 | }
|
---|
[51b73452] | 91 | }
|
---|
[71f4e4f] | 92 |
|
---|
| 93 |
|
---|
[f1b1e4c] | 94 | ConstructorInit::ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init ) : Initializer( true ), ctor( ctor ), dtor( dtor ), init( init ) {}
|
---|
| 95 | ConstructorInit::ConstructorInit( const ConstructorInit &other ) : Initializer( other ), ctor( maybeClone( other.ctor ) ), dtor( maybeClone( other.dtor ) ), init( maybeClone( other.init ) ) {
|
---|
[70f89d00] | 96 | }
|
---|
| 97 |
|
---|
[50377a4] | 98 | void ConstructorInit::print( std::ostream &os, Indenter indent ) const {
|
---|
| 99 | os << "Constructor initializer: " << std::endl;
|
---|
[71f4e4f] | 100 | if ( ctor ) {
|
---|
[50377a4] | 101 | os << indent << "... initially constructed with ";
|
---|
| 102 | ctor->print( os, indent+1 );
|
---|
[71f4e4f] | 103 | } // if
|
---|
| 104 |
|
---|
[5b2f5bb] | 105 | if ( dtor ) {
|
---|
[50377a4] | 106 | os << indent << "... destructed with ";
|
---|
| 107 | dtor->print( os, indent+1 );
|
---|
[5b2f5bb] | 108 | }
|
---|
| 109 |
|
---|
[71f4e4f] | 110 | if ( init ) {
|
---|
[50377a4] | 111 | os << indent << "... with fallback C-style initializer: ";
|
---|
| 112 | init->print( os, indent+1 );
|
---|
[71f4e4f] | 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[0dd3a2f] | 116 | // Local Variables: //
|
---|
| 117 | // tab-width: 4 //
|
---|
| 118 | // mode: c++ //
|
---|
| 119 | // compile-command: "make install" //
|
---|
| 120 | // End: //
|
---|