source: src/Parser/InitializerNode.cc @ e9ed2a1

ADTast-experimental
Last change on this file since e9ed2a1 was bb7422a, checked in by Andrew Beach <ajbeach@…>, 15 months ago

Translated parser to the new ast. This incuded a small fix in the resolver so larger expressions can be used in with statements and some updated tests. errors/declaration just is a formatting update. attributes now actually preserves more attributes (unknown if all versions work).

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