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.cpp -- |
---|
8 | // |
---|
9 | // Author : Rodolfo G. Esteves |
---|
10 | // Created On : Sat May 16 13:20:24 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Aug 20 22:36:22 2024 |
---|
13 | // Update Count : 28 |
---|
14 | // |
---|
15 | |
---|
16 | #include "InitializerNode.hpp" |
---|
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.hpp" // for SemanticError |
---|
25 | #include "Common/Utility.hpp" // for maybeBuild |
---|
26 | #include "ExpressionNode.hpp" // for ExpressionNode |
---|
27 | #include "DeclarationNode.hpp" // for buildList |
---|
28 | |
---|
29 | using namespace std; |
---|
30 | |
---|
31 | static ast::ConstructFlag toConstructFlag( bool maybeConstructed ) { |
---|
32 | return maybeConstructed ? ast::MaybeConstruct : ast::NoConstruct; |
---|
33 | } |
---|
34 | |
---|
35 | InitializerNode::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 = next; |
---|
39 | |
---|
40 | if ( kids ) |
---|
41 | set_last( nullptr ); |
---|
42 | } // InitializerNode::InitializerNode |
---|
43 | |
---|
44 | InitializerNode::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 = next; |
---|
51 | |
---|
52 | if ( kids ) |
---|
53 | next = nullptr; |
---|
54 | } // InitializerNode::InitializerNode |
---|
55 | |
---|
56 | InitializerNode::InitializerNode( bool isDelete ) : expr( nullptr ), aggregate( false ), designator( nullptr ), kids( nullptr ), maybeConstructed( false ), isDelete( isDelete ) {} |
---|
57 | |
---|
58 | InitializerNode::~InitializerNode() { |
---|
59 | delete expr; |
---|
60 | delete designator; |
---|
61 | delete kids; |
---|
62 | } // InitializerNode::~InitializerNode |
---|
63 | |
---|
64 | void InitializerNode::print( std::ostream &os, int indent ) const { |
---|
65 | os << std::string( indent, ' ' ) << "Initializer expression" << std::endl; |
---|
66 | } // InitializerNode::print |
---|
67 | |
---|
68 | void 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 = curdes->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 = next ) ) { |
---|
90 | moreInit->printOneLine( os ); |
---|
91 | } // if |
---|
92 | } // InitializerNode::printOneLine |
---|
93 | |
---|
94 | ast::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 = child->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: // |
---|