[b87a5ed] | 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 | // InitializerNode.cc -- |
---|
| 8 | // |
---|
[b87a5ed] | 9 | // Author : Rodolfo G. Esteves |
---|
| 10 | // Created On : Sat May 16 13:20:24 2015 |
---|
[8e9cbb2] | 11 | // Last Modified By : Peter A. Buhr |
---|
[faddbd8] | 12 | // Last Modified On : Sat Oct 1 23:09:51 2016 |
---|
| 13 | // Update Count : 21 |
---|
[974906e2] | 14 | // |
---|
[b87a5ed] | 15 | |
---|
[51b7345] | 16 | #include <cassert> |
---|
[d9a0e76] | 17 | #include <iostream> |
---|
| 18 | using namespace std; |
---|
[51b7345] | 19 | |
---|
[bdd516a] | 20 | #include "ParseNode.h" |
---|
| 21 | #include "SynTree/Expression.h" |
---|
| 22 | #include "SynTree/Initializer.h" |
---|
| 23 | |
---|
[51b7345] | 24 | InitializerNode::InitializerNode( ExpressionNode *_expr, bool aggrp, ExpressionNode *des ) |
---|
[faddbd8] | 25 | : expr( _expr ), aggregate( aggrp ), designator( des ), kids( 0 ), maybeConstructed( true ) { |
---|
[b87a5ed] | 26 | if ( aggrp ) |
---|
[7880579] | 27 | kids = dynamic_cast< InitializerNode * >( get_next() ); |
---|
[51b7345] | 28 | |
---|
[b87a5ed] | 29 | if ( kids != 0 ) |
---|
[1d4580a] | 30 | set_last( 0 ); |
---|
[51b7345] | 31 | } |
---|
| 32 | |
---|
| 33 | InitializerNode::InitializerNode( InitializerNode *init, bool aggrp, ExpressionNode *des ) |
---|
[faddbd8] | 34 | : expr( 0 ), aggregate( aggrp ), designator( des ), kids( 0 ), maybeConstructed( true ) { |
---|
[b87a5ed] | 35 | if ( init != 0 ) |
---|
[1d4580a] | 36 | set_last( init ); |
---|
[51b7345] | 37 | |
---|
[b87a5ed] | 38 | if ( aggrp ) |
---|
[7880579] | 39 | kids = dynamic_cast< InitializerNode * >( get_next() ); |
---|
[51b7345] | 40 | |
---|
[b87a5ed] | 41 | if ( kids != 0 ) |
---|
| 42 | set_next( 0 ); |
---|
[51b7345] | 43 | } |
---|
| 44 | |
---|
| 45 | InitializerNode::~InitializerNode() { |
---|
[b87a5ed] | 46 | delete expr; |
---|
[4f147cc] | 47 | delete designator; |
---|
| 48 | delete kids; |
---|
[51b7345] | 49 | } |
---|
| 50 | |
---|
| 51 | void InitializerNode::print( std::ostream &os, int indent ) const { |
---|
[59db689] | 52 | os << std::string( indent, ' ' ) << "Initializer expression" << std::endl; |
---|
[51b7345] | 53 | } |
---|
| 54 | |
---|
| 55 | void InitializerNode::printOneLine( std::ostream &os ) const { |
---|
[b87a5ed] | 56 | if ( ! aggregate ) { |
---|
| 57 | if ( designator != 0 ) { |
---|
| 58 | os << "designated by: ("; |
---|
| 59 | ExpressionNode *curdes = designator; |
---|
[a32b204] | 60 | while ( curdes != 0) { |
---|
[b87a5ed] | 61 | curdes->printOneLine(os); |
---|
[1d4580a] | 62 | curdes = (ExpressionNode *)(curdes->get_next()); |
---|
[b87a5ed] | 63 | if ( curdes ) os << ", "; |
---|
| 64 | } // while |
---|
| 65 | os << ")"; |
---|
| 66 | } // if |
---|
| 67 | if ( expr ) expr->printOneLine(os); |
---|
| 68 | } else { // It's an aggregate |
---|
| 69 | os << "[--"; |
---|
| 70 | if ( next_init() != 0 ) |
---|
| 71 | next_init()->printOneLine(os); |
---|
| 72 | if (aggregate) os << "--]"; |
---|
[17cd4eb] | 73 | } // if |
---|
[b87a5ed] | 74 | |
---|
| 75 | InitializerNode *moreInit; |
---|
[1d4580a] | 76 | if ( get_next() != 0 && ((moreInit = dynamic_cast< InitializerNode * >( get_next() ) ) != 0) ) |
---|
[b87a5ed] | 77 | moreInit->printOneLine( os ); |
---|
[51b7345] | 78 | } |
---|
| 79 | |
---|
| 80 | Initializer *InitializerNode::build() const { |
---|
[b87a5ed] | 81 | if ( aggregate ) { |
---|
[7880579] | 82 | std::list< Initializer * > initlist; |
---|
| 83 | buildList< Initializer, InitializerNode >( next_init(), initlist ); |
---|
[51b7345] | 84 | |
---|
[7880579] | 85 | std::list< Expression * > designlist; |
---|
[d9a0e76] | 86 | |
---|
[b87a5ed] | 87 | if ( designator != 0 ) { |
---|
[7880579] | 88 | buildList< Expression, ExpressionNode >( designator, designlist ); |
---|
[b87a5ed] | 89 | } // if |
---|
[51b7345] | 90 | |
---|
[974906e2] | 91 | return new ListInit( initlist, designlist, maybeConstructed ); |
---|
[b87a5ed] | 92 | } else { |
---|
[7880579] | 93 | std::list< Expression * > designators; |
---|
[51b7345] | 94 | |
---|
[b87a5ed] | 95 | if ( designator != 0 ) |
---|
[7880579] | 96 | buildList< Expression, ExpressionNode >( designator, designators ); |
---|
[51b7345] | 97 | |
---|
[b87a5ed] | 98 | if ( get_expression() != 0) |
---|
[7880579] | 99 | return new SingleInit( maybeBuild< Expression >( get_expression() ), designators, maybeConstructed ); |
---|
[b87a5ed] | 100 | } // if |
---|
[51b7345] | 101 | |
---|
[b87a5ed] | 102 | return 0; |
---|
[51b7345] | 103 | } |
---|
[b87a5ed] | 104 | |
---|
| 105 | // Local Variables: // |
---|
| 106 | // tab-width: 4 // |
---|
| 107 | // mode: c++ // |
---|
| 108 | // compile-command: "make install" // |
---|
| 109 | // End: // |
---|