source: src/Parser/InitializerNode.cc@ fa2c005

ADT
Last change on this file since fa2c005 was c468150, checked in by Andrew Beach <ajbeach@…>, 2 years 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
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 "InitializerNode.h"
17
18#include <iostream> // for operator<<, ostream, basic_ostream
19#include <list> // for list
20#include <string> // for operator<<, string
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 "ExpressionNode.h" // for ExpressionNode
27#include "DeclarationNode.h" // for buildList
28
29using namespace std;
30
31static ast::ConstructFlag toConstructFlag( bool maybeConstructed ) {
32 return maybeConstructed ? ast::MaybeConstruct : ast::NoConstruct;
33}
34
35InitializerNode::InitializerNode( ExpressionNode * _expr, bool aggrp, ExpressionNode * des )
36 : expr( _expr ), aggregate( aggrp ), designator( des ), kids( nullptr ), maybeConstructed( true ), isDelete( false ) {
37 if ( aggrp )
38 kids = dynamic_cast< InitializerNode * >( get_next() );
39
40 if ( kids )
41 set_last( nullptr );
42} // InitializerNode::InitializerNode
43
44InitializerNode::InitializerNode( InitializerNode * init, bool aggrp, ExpressionNode * des )
45 : expr( nullptr ), aggregate( aggrp ), designator( des ), kids( nullptr ), maybeConstructed( true ), isDelete( false ) {
46 if ( init )
47 set_last( init );
48
49 if ( aggrp )
50 kids = dynamic_cast< InitializerNode * >( get_next() );
51
52 if ( kids )
53 set_next( nullptr );
54} // InitializerNode::InitializerNode
55
56InitializerNode::InitializerNode( bool isDelete ) : expr( nullptr ), aggregate( false ), designator( nullptr ), kids( nullptr ), maybeConstructed( false ), isDelete( isDelete ) {}
57
58InitializerNode::~InitializerNode() {
59 delete expr;
60 delete designator;
61 delete kids;
62} // InitializerNode::~InitializerNode
63
64void InitializerNode::print( std::ostream &os, int indent ) const {
65 os << std::string( indent, ' ' ) << "Initializer expression" << std::endl;
66} // InitializerNode::print
67
68void InitializerNode::printOneLine( std::ostream &os ) const {
69 if ( ! aggregate ) {
70 if ( designator ) {
71 os << "designated by: (";
72 ExpressionNode *curdes = designator;
73 while ( curdes != nullptr) {
74 curdes->printOneLine(os);
75 curdes = (ExpressionNode *)(curdes->get_next());
76 if ( curdes ) os << ", ";
77 } // while
78 os << ")";
79 } // if
80 if ( expr ) expr->printOneLine( os );
81 } else { // It's an aggregate
82 os << "[--";
83 if ( next_init() != nullptr )
84 next_init()->printOneLine( os );
85 if (aggregate) os << "--]";
86 } // if
87
88 InitializerNode *moreInit;
89 if ( (moreInit = dynamic_cast< InitializerNode * >( get_next() ) ) ) {
90 moreInit->printOneLine( os );
91 } // if
92} // InitializerNode::printOneLine
93
94ast::Init * InitializerNode::build() const {
95 assertf( ! isDelete, "Should not build delete stmt InitializerNode" );
96 if ( aggregate ) {
97 // steal designators from children
98 std::vector<ast::ptr<ast::Designation>> designlist;
99 InitializerNode * child = next_init();
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 ) ) );
105 } // for
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 );
119 } // if
120 return nullptr;
121} // InitializerNode::build
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.