source: src/Parser/InitializerNode.cc @ f1a4ccb

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f1a4ccb was 1131392, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

convert to implicit check for != 0

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[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
[1131392]12// Last Modified On : Fri Jul 28 23:27:20 2017
13// Update Count     : 26
[974906e2]14//
[b87a5ed]15
[51b7345]16#include <cassert>
[d9a0e76]17#include <iostream>
18using namespace std;
[51b7345]19
[bdd516a]20#include "ParseNode.h"
21#include "SynTree/Expression.h"
22#include "SynTree/Initializer.h"
23
[1131392]24InitializerNode::InitializerNode( ExpressionNode * _expr, bool aggrp, ExpressionNode * des )
25                : expr( _expr ), aggregate( aggrp ), designator( des ), kids( nullptr ), maybeConstructed( true ) {
[b87a5ed]26        if ( aggrp )
[7880579]27                kids = dynamic_cast< InitializerNode * >( get_next() );
[51b7345]28
[1131392]29        if ( kids )
30                set_last( nullptr );
31} // InitializerNode::InitializerNode
[51b7345]32
[1131392]33InitializerNode::InitializerNode( InitializerNode * init, bool aggrp, ExpressionNode * des )
34                : expr( nullptr ), aggregate( aggrp ), designator( des ), kids( nullptr ), maybeConstructed( true ) {
35        if ( init )
[1d4580a]36                set_last( init );
[51b7345]37
[b87a5ed]38        if ( aggrp )
[7880579]39                kids = dynamic_cast< InitializerNode * >( get_next() );
[51b7345]40
[1131392]41        if ( kids )
42                set_next( nullptr );
43} // InitializerNode::InitializerNode
[51b7345]44
45InitializerNode::~InitializerNode() {
[b87a5ed]46        delete expr;
[4f147cc]47        delete designator;
48        delete kids;
[1131392]49} // InitializerNode::~InitializerNode
[51b7345]50
51void InitializerNode::print( std::ostream &os, int indent ) const {
[59db689]52        os << std::string( indent, ' ' ) << "Initializer expression" << std::endl;
[1131392]53} // InitializerNode::print
[51b7345]54
55void InitializerNode::printOneLine( std::ostream &os ) const {
[b87a5ed]56        if ( ! aggregate ) {
[1131392]57                if ( designator ) {
[b87a5ed]58                        os << "designated by: (";
59                        ExpressionNode *curdes = designator;
[1131392]60                        while ( curdes != nullptr) {
[b87a5ed]61                                curdes->printOneLine(os);
[1d4580a]62                                curdes = (ExpressionNode *)(curdes->get_next());
[b87a5ed]63                                if ( curdes ) os << ", ";
64                        } // while
65                        os << ")";
66                } // if
[1131392]67                if ( expr ) expr->printOneLine( os );
[b87a5ed]68        } else {  // It's an aggregate
69                os << "[--";
[1131392]70                if ( next_init() != nullptr )
71                        next_init()->printOneLine( os );
[b87a5ed]72                if (aggregate) os << "--]";
[17cd4eb]73        } // if
[b87a5ed]74
75        InitializerNode *moreInit;
[e4d829b]76        if ( (moreInit = dynamic_cast< InitializerNode * >( get_next() ) ) ) {
[b87a5ed]77                moreInit->printOneLine( os );
[1131392]78        } // if
79} // InitializerNode::printOneLine
[51b7345]80
[1131392]81Initializer * InitializerNode::build() const {
[b87a5ed]82        if ( aggregate ) {
[e4d829b]83                // steal designators from children
84                std::list< Designation * > designlist;
85                InitializerNode * child = next_init();
86                for ( ; child != nullptr; child = dynamic_cast< InitializerNode * >( child->get_next() ) ) {
87                        std::list< Expression * > desList;
88                        buildList< Expression, ExpressionNode >( child->designator, desList );
89                        designlist.push_back( new Designation( desList ) );
90                } // for
[7880579]91                std::list< Initializer * > initlist;
92                buildList< Initializer, InitializerNode >( next_init(), initlist );
[974906e2]93                return new ListInit( initlist, designlist, maybeConstructed );
[b87a5ed]94        } else {
[1131392]95                if ( get_expression() ) {
[e4d829b]96                        return new SingleInit( maybeBuild< Expression >( get_expression() ), maybeConstructed );
[1131392]97                } // if
[b87a5ed]98        } // if
[1131392]99        return nullptr;
100} // InitializerNode::build
[b87a5ed]101
102// Local Variables: //
103// tab-width: 4 //
104// mode: c++ //
105// compile-command: "make install" //
106// End: //
Note: See TracBrowser for help on using the repository browser.