source: src/Parser/InitializerNode.cc @ 835d6e8

ADTast-experimental
Last change on this file since 835d6e8 was bb7422a, checked in by Andrew Beach <ajbeach@…>, 14 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
Line 
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//
7// InitializerNode.cc --
8//
9// Author           : Rodolfo G. Esteves
10// Created On       : Sat May 16 13:20:24 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Tue Apr  4 11:18:00 2023
13// Update Count     : 27
14//
15
16#include <iostream>                // for operator<<, ostream, basic_ostream
17#include <list>                    // for list
18#include <string>                  // for operator<<, string
19
20using namespace std;
21
22#include "AST/Expr.hpp"            // for Expr
23#include "AST/Init.hpp"            // for Designator, Init, ListInit, Sing...
24#include "Common/SemanticError.h"  // for SemanticError
25#include "Common/utility.h"        // for maybeBuild
26#include "ParseNode.h"             // for InitializerNode, ExpressionNode
27
28static ast::ConstructFlag toConstructFlag( bool maybeConstructed ) {
29        return maybeConstructed ? ast::MaybeConstruct : ast::NoConstruct;
30}
31
32InitializerNode::InitializerNode( ExpressionNode * _expr, bool aggrp, ExpressionNode * des )
33                : expr( _expr ), aggregate( aggrp ), designator( des ), kids( nullptr ), maybeConstructed( true ), isDelete( false ) {
34        if ( aggrp )
35                kids = dynamic_cast< InitializerNode * >( get_next() );
36
37        if ( kids )
38                set_last( nullptr );
39} // InitializerNode::InitializerNode
40
41InitializerNode::InitializerNode( InitializerNode * init, bool aggrp, ExpressionNode * des )
42                : expr( nullptr ), aggregate( aggrp ), designator( des ), kids( nullptr ), maybeConstructed( true ), isDelete( false ) {
43        if ( init )
44                set_last( init );
45
46        if ( aggrp )
47                kids = dynamic_cast< InitializerNode * >( get_next() );
48
49        if ( kids )
50                set_next( nullptr );
51} // InitializerNode::InitializerNode
52
53InitializerNode::InitializerNode( bool isDelete ) : expr( nullptr ), aggregate( false ), designator( nullptr ), kids( nullptr ), maybeConstructed( false ), isDelete( isDelete ) {}
54
55InitializerNode::~InitializerNode() {
56        delete expr;
57        delete designator;
58        delete kids;
59} // InitializerNode::~InitializerNode
60
61void InitializerNode::print( std::ostream &os, int indent ) const {
62        os << std::string( indent, ' ' ) << "Initializer expression" << std::endl;
63} // InitializerNode::print
64
65void InitializerNode::printOneLine( std::ostream &os ) const {
66        if ( ! aggregate ) {
67                if ( designator ) {
68                        os << "designated by: (";
69                        ExpressionNode *curdes = designator;
70                        while ( curdes != nullptr) {
71                                curdes->printOneLine(os);
72                                curdes = (ExpressionNode *)(curdes->get_next());
73                                if ( curdes ) os << ", ";
74                        } // while
75                        os << ")";
76                } // if
77                if ( expr ) expr->printOneLine( os );
78        } else {  // It's an aggregate
79                os << "[--";
80                if ( next_init() != nullptr )
81                        next_init()->printOneLine( os );
82                if (aggregate) os << "--]";
83        } // if
84
85        InitializerNode *moreInit;
86        if ( (moreInit = dynamic_cast< InitializerNode * >( get_next() ) ) ) {
87                moreInit->printOneLine( os );
88        } // if
89} // InitializerNode::printOneLine
90
91ast::Init * InitializerNode::build() const {
92        assertf( ! isDelete, "Should not build delete stmt InitializerNode" );
93        if ( aggregate ) {
94                // steal designators from children
95                std::vector<ast::ptr<ast::Designation>> designlist;
96                InitializerNode * child = next_init();
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 ) ) );
102                } // for
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                );
116        } // if
117        return nullptr;
118} // InitializerNode::build
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.