source: src/Parser/InitializerNode.cc @ 52a5262e

Last change on this file since 52a5262e was c468150, checked in by Andrew Beach <ajbeach@…>, 19 months ago

Split up ParseNode?.h so that headers match implementation. May have a bit less to include total because of it.

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