source: src/Parser/InitializerNode.cpp

Last change on this file was cc0aa8c, checked in by Peter A. Buhr <pabuhr@…>, 3 months ago

formatting

  • Property mode set to 100644
File size: 4.0 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//
[c92bdcc]7// InitializerNode.cpp --
[974906e2]8//
[b87a5ed]9// Author           : Rodolfo G. Esteves
10// Created On       : Sat May 16 13:20:24 2015
[cc0aa8c]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Aug 20 22:36:22 2024
13// Update Count     : 28
[974906e2]14//
[b87a5ed]15
[c92bdcc]16#include "InitializerNode.hpp"
[c468150]17
[c92bdcc]18#include <iostream>                  // for operator<<, ostream, basic_ostream
19#include <list>                      // for list
20#include <string>                    // for operator<<, string
[d180746]21
[c92bdcc]22#include "AST/Expr.hpp"              // for Expr
23#include "AST/Init.hpp"              // for Designator, Init, ListInit, Sing...
24#include "Common/SemanticError.hpp"  // for SemanticError
25#include "Common/Utility.hpp"        // for maybeBuild
26#include "ExpressionNode.hpp"        // for ExpressionNode
27#include "DeclarationNode.hpp"       // for buildList
[c468150]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 )
[44adf1b]38                kids = 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 )
[44adf1b]50                kids = next;
[51b7345]51
[1131392]52        if ( kids )
[44adf1b]53                next = nullptr;
[1131392]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);
[44adf1b]75                                curdes = curdes->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
[cc0aa8c]88        InitializerNode * moreInit;
[44adf1b]89        if ( ( moreInit = 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();
[44adf1b]100                for ( ; child != nullptr ; child = child->next ) {
[bb7422a]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.