Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SynTree/Initializer.cc

    r62423350 rd7dc824  
    1919#include "Common/utility.h"
    2020
    21 Designation::Designation( const std::list< Expression * > & designators ) : designators( designators ) {}
    22 Designation::Designation( const Designation & other ) : BaseSyntaxNode( other ) {
    23         // std::cerr << "cloning designation" << std::endl;
    24         cloneAll( other.designators, designators );
    25         // std::cerr << "finished cloning designation" << std::endl;
    26 }
    27 
    28 Designation::~Designation() {
    29         // std::cerr << "destroying designation" << std::endl;
    30         deleteAll( designators );
    31         // std::cerr << "finished destroying designation" << std::endl;
    32 }
    33 
    34 void Designation::print( std::ostream &os, int indent ) const {
    35         if ( ! designators.empty() ) {
    36                 os << std::string(indent + 2, ' ' ) << "designated by: " << std::endl;
    37                 for ( std::list < Expression * >::const_iterator i = designators.begin(); i != designators.end(); i++ ) {
    38                         os << std::string(indent + 4, ' ' );
    39                         ( *i )->print(os, indent + 4 );
    40                 }
    41                 os << std::endl;
    42         } // if
    43 }
    44 
    4521Initializer::Initializer( bool maybeConstructed ) : maybeConstructed( maybeConstructed ) {}
    4622Initializer::Initializer( const Initializer & other ) : BaseSyntaxNode( other ), maybeConstructed( other.maybeConstructed ) {
    4723}
     24
     25
    4826Initializer::~Initializer() {}
    4927
    50 SingleInit::SingleInit( Expression *v, bool maybeConstructed ) : Initializer( maybeConstructed ), value ( v ) {
     28std::string Initializer::designator_name( Expression *des ) {
     29        if ( NameExpr *n = dynamic_cast<NameExpr *>(des) )
     30                return n->get_name();
     31        else
     32                throw 0;
     33}
     34
     35// void Initializer::print( __attribute__((unused)) std::ostream &os, __attribute__((unused)) int indent ) {}
     36
     37SingleInit::SingleInit( Expression *v, const std::list< Expression *> &_designators, bool maybeConstructed ) : Initializer( maybeConstructed ), value ( v ), designators( _designators ) {
    5138}
    5239
    5340SingleInit::SingleInit( const SingleInit &other ) : Initializer(other), value ( maybeClone( other.value ) ) {
     41        cloneAll(other.designators, designators );
    5442}
    5543
    5644SingleInit::~SingleInit() {
    5745        delete value;
     46        deleteAll(designators);
    5847}
    5948
    60 void SingleInit::print( std::ostream &os, int indent ) const {
    61         os << std::string(indent, ' ' ) << "Simple Initializer: " << std::endl;
     49void SingleInit::print( std::ostream &os, int indent ) {
     50        os << std::endl << std::string(indent, ' ' ) << "Simple Initializer: " << std::endl;
    6251        os << std::string(indent+4, ' ' );
    6352        value->print( os, indent+4 );
     53
     54        if ( ! designators.empty() ) {
     55                os << std::endl << std::string(indent + 2, ' ' ) << "designated by: " << std::endl;
     56                for ( std::list < Expression * >::iterator i = designators.begin(); i != designators.end(); i++ ) {
     57                        os << std::string(indent + 4, ' ' );
     58                        ( *i )->print(os, indent + 4 );
     59                }
     60        } // if
    6461}
    6562
    66 
    67 ListInit::ListInit( const std::list<Initializer*> &inits, const std::list<Designation *> &des, bool maybeConstructed )
    68         : Initializer( maybeConstructed ), initializers( inits ), designations( des ) {
    69                 // 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
    70                 if ( designations.empty() ) {
    71                         for ( auto & i : initializers ) {
    72                                 (void)i;
    73                                 designations.push_back( new Designation( {} ) );
    74                         }
    75                 }
    76                 assertf( initializers.size() == designations.size(), "Created ListInit with mismatching initializers (%d) and designations (%d)", initializers.size(), designations.size() );
     63ListInit::ListInit( const std::list<Initializer*> &_initializers, const std::list<Expression *> &_designators, bool maybeConstructed )
     64        : Initializer( maybeConstructed ), initializers( _initializers ), designators( _designators ) {
    7765}
    7866
    7967ListInit::ListInit( const ListInit & other ) : Initializer( other ) {
    8068        cloneAll( other.initializers, initializers );
    81         cloneAll( other.designations, designations );
     69        cloneAll( other.designators, designators );
    8270}
     71
    8372
    8473ListInit::~ListInit() {
    8574        deleteAll( initializers );
    86         deleteAll( designations );
     75        deleteAll( designators );
    8776}
    8877
    89 void ListInit::print( std::ostream &os, int indent ) const {
    90         os << std::string(indent, ' ') << "Compound initializer:  " << std::endl;
    91         for ( Designation * d : designations ) {
    92                 d->print( os, indent + 2 );
    93         }
     78void ListInit::print( std::ostream &os, int indent ) {
     79        os << std::endl << std::string(indent, ' ') << "Compound initializer:  ";
     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++ ) {
     84                        ( *i )->print(os, indent + 4 );
     85                } // for
    9486
    95         for ( const Initializer * init : initializers ) {
    96                 init->print( os, indent + 2 );
    97                 os << std::endl;
    98         }
     87                os << std::string(indent + 2, ' ' ) << "]";
     88        } // if
     89
     90        for ( std::list<Initializer *>::iterator i = initializers.begin(); i != initializers.end(); i++ )
     91                (*i)->print( os, indent + 2 );
    9992}
    10093
     
    110103}
    111104
    112 void ConstructorInit::print( std::ostream &os, int indent ) const {
     105void ConstructorInit::print( std::ostream &os, int indent ) {
    113106        os << std::endl << std::string(indent, ' ') << "Constructor initializer: " << std::endl;
    114107        if ( ctor ) {
     
    131124}
    132125
    133 std::ostream & operator<<( std::ostream & out, const Initializer * init ) {
    134         if ( init ) {
    135                 init->print( out );
    136         } else {
    137                 out << "nullptr";
    138         }
    139         return out;
    140 }
    141 
    142 std::ostream & operator<<( std::ostream & out, const Designation * des ) {
    143         if ( des ) {
    144                 des->print( out );
    145         } else {
    146                 out << "nullptr";
    147         }
     126std::ostream & operator<<( std::ostream & out, Initializer * init ) {
     127        init->print( out );
    148128        return out;
    149129}
Note: See TracChangeset for help on using the changeset viewer.