source: src/Parser/InitializerNode.cc @ e5e2334

ADTast-experimentalpthread-emulation
Last change on this file since e5e2334 was 374cb117, checked in by JiadaL <j82liang@…>, 3 years ago

Replace the interface for EnumDecl? node construction to support generic enum types

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