source: src/Parser/StatementNode.cc@ ff3b0249

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since ff3b0249 was 436bbe5, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

remove unnecessary std:: qualification, clean up build_* functions, continue renaming to then/else_

  • Property mode set to 100644
File size: 14.7 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//
[0f8e4ac]7// StatementNode.cc --
[b87a5ed]8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 14:59:41 2015
[a67b60e]11// Last Modified By : Peter A. Buhr
[436bbe5]12// Last Modified On : Wed Feb 2 09:45:28 2022
13// Update Count : 415
[b87a5ed]14//
15
[e3e16bc]16#include <cassert> // for assert, strict_dynamic_cast, assertf
[d180746]17#include <list> // for list
18#include <memory> // for unique_ptr
19#include <string> // for string
[51b73452]20
[d180746]21#include "Common/SemanticError.h" // for SemanticError
22#include "Common/utility.h" // for maybeMoveBuild, maybeBuild
23#include "ParseNode.h" // for StatementNode, ExpressionNode, bui...
24#include "SynTree/Expression.h" // for Expression, ConstantExpr
25#include "SynTree/Label.h" // for Label, noLabels
[6d49ea3]26#include "SynTree/Declaration.h"
[d180746]27#include "SynTree/Statement.h" // for Statement, BranchStmt, CaseStmt
28#include "parserutility.h" // for notZeroExpr
29
30class Declaration;
[51b73452]31
32using namespace std;
33
[b87a5ed]34
[61fc4f6]35StatementNode::StatementNode( DeclarationNode * decl ) {
[c0aa336]36 assert( decl );
[61fc4f6]37 DeclarationNode * agg = decl->extractAggregate();
[c0aa336]38 if ( agg ) {
[61fc4f6]39 StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
[c0aa336]40 set_next( nextStmt );
41 if ( decl->get_next() ) {
42 get_next()->set_next( new StatementNode( dynamic_cast< DeclarationNode * >(decl->get_next()) ) );
43 decl->set_next( 0 );
[1d4580a]44 } // if
45 } else {
[c0aa336]46 if ( decl->get_next() ) {
47 set_next( new StatementNode( dynamic_cast< DeclarationNode * >( decl->get_next() ) ) );
48 decl->set_next( 0 );
49 } // if
50 agg = decl;
[1d4580a]51 } // if
[ba3706f]52 stmt.reset( new DeclStmt( maybeMoveBuild< Declaration >(agg) ) );
[c0aa336]53} // StatementNode::StatementNode
[1d4580a]54
[61fc4f6]55StatementNode * StatementNode::append_last_case( StatementNode * stmt ) {
56 StatementNode * prev = this;
[1d4580a]57 // find end of list and maintain previous pointer
58 for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
[61fc4f6]59 StatementNode * node = strict_dynamic_cast< StatementNode * >(curr);
[ac71a86]60 assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
[8cc5cb0]61 prev = curr;
62 } // for
[e82aa9df]63 // convert from StatementNode list to Statement list
[61fc4f6]64 StatementNode * node = dynamic_cast< StatementNode * >(prev);
[436bbe5]65 list< Statement * > stmts;
[7ecbb7e]66 buildMoveList( stmt, stmts );
[e82aa9df]67 // splice any new Statements to end of current Statements
[ac71a86]68 CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get());
[8cc5cb0]69 caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
70 return this;
[401e61f]71} // StatementNode::append_last_case
[8cc5cb0]72
[61fc4f6]73Statement * build_expr( ExpressionNode * ctl ) {
74 Expression * e = maybeMoveBuild< Expression >( ctl );
[8cc5cb0]75
[401e61f]76 if ( e ) return new ExprStmt( e );
77 else return new NullStmt();
78} // build_expr
[8cc5cb0]79
[436bbe5]80Expression * build_if_control( CondCtl * ctl, list< Statement * > & init ) {
[936e9f4]81 if ( ctl->init != 0 ) {
82 buildMoveList( ctl->init, init );
83 } // if
84
[a01f7c94]85 Expression * cond = nullptr;
86 if ( ctl->condition ) {
87 // compare the provided condition against 0
[ee3c93d]88 cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
[a01f7c94]89 } else {
90 for ( Statement * stmt : init ) {
91 // build the && of all of the declared variables compared against 0
[e3e16bc]92 DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt );
93 DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl );
[a01f7c94]94 Expression * nze = notZeroExpr( new VariableExpr( dwt ) );
95 cond = cond ? new LogicalExpr( cond, nze, true ) : nze;
96 }
97 }
[6d49ea3]98 delete ctl;
[ee3c93d]99 return cond;
[401e61f]100} // build_if_control
[ee3c93d]101
[436bbe5]102Statement * build_if( CondCtl * ctl, StatementNode * then, StatementNode * else_ ) {
103 list< Statement * > astinit; // maybe empty
104 Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
105
106 Statement * astthen, * astelse = nullptr;
107 list< Statement * > aststmt;
108 buildMoveList< Statement, StatementNode >( then, aststmt );
109 assert( aststmt.size() == 1 );
110 astthen = aststmt.front();
111
112 if ( else_ ) {
113 list< Statement * > aststmt;
114 buildMoveList< Statement, StatementNode >( else_, aststmt );
115 assert( aststmt.size() == 1 );
116 astelse = aststmt.front();
[ee3c93d]117 } // if
118
[436bbe5]119 return new IfStmt( astcond, astthen, astelse, astinit );
[401e61f]120} // build_if
[2f22cc4]121
[61fc4f6]122Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
[436bbe5]123 list< Statement * > aststmt;
124 buildMoveList< Statement, StatementNode >( stmt, aststmt );
125 if ( ! isSwitch ) { // choose statement
126 for ( Statement * stmt : aststmt ) {
[6a276a0]127 CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
128 if ( ! caseStmt->stmts.empty() ) { // code after "case" => end of case list
129 CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() );
130 block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) );
131 } // if
132 } // for
133 } // if
[436bbe5]134 // aststmt.size() == 0 for switch (...) {}, i.e., no declaration or statements
135 return new SwitchStmt( maybeMoveBuild< Expression >(ctl), aststmt );
[401e61f]136} // build_switch
137
[61fc4f6]138Statement * build_case( ExpressionNode * ctl ) {
[436bbe5]139 return new CaseStmt( maybeMoveBuild< Expression >(ctl), list< Statement * >{} );
[401e61f]140} // build_case
141
[61fc4f6]142Statement * build_default() {
[436bbe5]143 return new CaseStmt( nullptr, list< Statement * >{}, true );
[401e61f]144} // build_default
145
[3b0bc16]146Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * else_ ) {
[436bbe5]147 list< Statement * > astinit; // maybe empty
148 Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
[3b0bc16]149
[436bbe5]150 list< Statement * > aststmt; // loop body, compound created if empty
[3b0bc16]151 buildMoveList< Statement, StatementNode >( stmt, aststmt );
152 assert( aststmt.size() == 1 );
153
[436bbe5]154 list< Statement * > astelse; // else clause, maybe empty
[3b0bc16]155 buildMoveList< Statement, StatementNode >( else_, astelse );
156
[436bbe5]157 return new WhileDoStmt( astcond, aststmt.front(), astelse.front(), astinit, false );
[401e61f]158} // build_while
[2f22cc4]159
[3b0bc16]160Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * else_ ) {
[436bbe5]161 // do-while cannot have declarations in the contitional, so always empty
162 list< Statement * > astinit;
163
164 list< Statement * > aststmt; // loop body, compound created if empty
[3b0bc16]165 buildMoveList< Statement, StatementNode >( stmt, aststmt );
[436bbe5]166 assert( aststmt.size() == 1 ); // compound created if empty
[3b0bc16]167
[436bbe5]168 list< Statement * > astelse; // else clause, maybe empty
[3b0bc16]169 buildMoveList< Statement, StatementNode >( else_, astelse );
[ee3c93d]170
[436bbe5]171 return new WhileDoStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), aststmt.front(), astelse.front(), astinit, true );
[401e61f]172} // build_do_while
[2f22cc4]173
[3b0bc16]174Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ ) {
[436bbe5]175 list< Statement * > astinit; // maybe empty
176 buildMoveList( forctl->init, astinit );
[2f22cc4]177
[436bbe5]178 Expression * astcond = nullptr; // maybe empty
179 astcond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
[2f22cc4]180
[436bbe5]181 Expression * astincr = nullptr; // maybe empty
182 astincr = maybeMoveBuild< Expression >(forctl->change);
183 delete forctl;
[2f22cc4]184
[436bbe5]185 list< Statement * > aststmt; // loop body, compound created if empty
[3b0bc16]186 buildMoveList< Statement, StatementNode >( stmt, aststmt );
187 assert( aststmt.size() == 1 );
188
[436bbe5]189 list< Statement * > astelse; // else clause, maybe empty
[3b0bc16]190 buildMoveList< Statement, StatementNode >( else_, astelse );
191
[436bbe5]192 return new ForStmt( astinit, astcond, astincr, aststmt.front(), astelse.front() );
[401e61f]193} // build_for
[2f22cc4]194
[61fc4f6]195Statement * build_branch( BranchStmt::Type kind ) {
[ba3706f]196 Statement * ret = new BranchStmt( "", kind );
[ab57786]197 return ret;
[401e61f]198} // build_branch
199
[436bbe5]200Statement * build_branch( string * identifier, BranchStmt::Type kind ) {
[61fc4f6]201 Statement * ret = new BranchStmt( * identifier, kind );
[ab57786]202 delete identifier; // allocated by lexer
203 return ret;
[401e61f]204} // build_branch
205
[61fc4f6]206Statement * build_computedgoto( ExpressionNode * ctl ) {
[ba3706f]207 return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
[401e61f]208} // build_computedgoto
[8cc5cb0]209
[61fc4f6]210Statement * build_return( ExpressionNode * ctl ) {
[436bbe5]211 list< Expression * > exps;
[7ecbb7e]212 buildMoveList( ctl, exps );
[ba3706f]213 return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
[401e61f]214} // build_return
[daf1af8]215
[61fc4f6]216Statement * build_throw( ExpressionNode * ctl ) {
[436bbe5]217 list< Expression * > exps;
[7ecbb7e]218 buildMoveList( ctl, exps );
[436bbe5]219 assertf( exps.size() < 2, "CFA internal error: leaking memory" );
[ba3706f]220 return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
[401e61f]221} // build_throw
[daf1af8]222
[61fc4f6]223Statement * build_resume( ExpressionNode * ctl ) {
[436bbe5]224 list< Expression * > exps;
[daf1af8]225 buildMoveList( ctl, exps );
[436bbe5]226 assertf( exps.size() < 2, "CFA internal error: leaking memory" );
[ba3706f]227 return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
[401e61f]228} // build_resume
[daf1af8]229
[61fc4f6]230Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
[794c15b]231 (void)ctl;
232 (void)target;
233 assertf( false, "resume at (non-local throw) is not yet supported," );
[401e61f]234} // build_resume_at
[321f55d]235
[436bbe5]236Statement * build_try( StatementNode * try_, StatementNode * catch_, StatementNode * finally_ ) {
237 list< CatchStmt * > aststmt;
238 buildMoveList< CatchStmt, StatementNode >( catch_, aststmt );
239 CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_));
240 FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_) );
241 return new TryStmt( tryBlock, aststmt, finallyBlock );
[401e61f]242} // build_try
243
[61fc4f6]244Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
[436bbe5]245 list< Statement * > aststmt;
246 buildMoveList< Statement, StatementNode >( body, aststmt );
247 assert( aststmt.size() == 1 );
248 return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), aststmt.front() );
[401e61f]249} // build_catch
250
[61fc4f6]251Statement * build_finally( StatementNode * stmt ) {
[436bbe5]252 list< Statement * > aststmt;
253 buildMoveList< Statement, StatementNode >( stmt, aststmt );
254 assert( aststmt.size() == 1 );
255 return new FinallyStmt( dynamic_cast< CompoundStmt * >( aststmt.front() ) );
[401e61f]256} // build_finally
[1d4580a]257
[427854b]258SuspendStmt * build_suspend( StatementNode * then, SuspendStmt::Type type ) {
259 auto node = new SuspendStmt();
260
261 node->type = type;
262
[436bbe5]263 list< Statement * > stmts;
[427854b]264 buildMoveList< Statement, StatementNode >( then, stmts );
265 if(!stmts.empty()) {
266 assert( stmts.size() == 1 );
267 node->then = dynamic_cast< CompoundStmt * >( stmts.front() );
268 }
269
270 return node;
271}
272
[135b431]273WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
274 auto node = new WaitForStmt();
275
276 WaitForStmt::Target target;
277 target.function = maybeBuild<Expression>( targetExpr );
[2065609]278
279 ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
280 targetExpr->set_next( nullptr );
281 buildMoveList< Expression >( next, target.arguments );
282
[135b431]283 delete targetExpr;
284
285 node->clauses.push_back( WaitForStmt::Clause{
286 target,
287 maybeMoveBuild<Statement >( stmt ),
[1dcd9554]288 notZeroExpr( maybeMoveBuild<Expression>( when ) )
[135b431]289 });
290
291 return node;
[401e61f]292} // build_waitfor
[135b431]293
294WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
295 WaitForStmt::Target target;
296 target.function = maybeBuild<Expression>( targetExpr );
[2065609]297
298 ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
299 targetExpr->set_next( nullptr );
300 buildMoveList< Expression >( next, target.arguments );
301
[135b431]302 delete targetExpr;
303
[310e5b7]304 node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{
[135b431]305 std::move( target ),
306 maybeMoveBuild<Statement >( stmt ),
[1dcd9554]307 notZeroExpr( maybeMoveBuild<Expression>( when ) )
[135b431]308 });
309
310 return node;
[401e61f]311} // build_waitfor
[135b431]312
313WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
314 auto node = new WaitForStmt();
315
316 if( timeout ) {
317 node->timeout.time = maybeMoveBuild<Expression>( timeout );
318 node->timeout.statement = maybeMoveBuild<Statement >( stmt );
[1dcd9554]319 node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[401e61f]320 } else {
[1dcd9554]321 node->orelse.statement = maybeMoveBuild<Statement >( stmt );
322 node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[401e61f]323 } // if
[135b431]324
325 return node;
[401e61f]326} // build_waitfor_timeout
[135b431]327
[436bbe5]328WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_, ExpressionNode * else_when ) {
[135b431]329 auto node = new WaitForStmt();
330
331 node->timeout.time = maybeMoveBuild<Expression>( timeout );
332 node->timeout.statement = maybeMoveBuild<Statement >( stmt );
[1dcd9554]333 node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[135b431]334
[436bbe5]335 node->orelse.statement = maybeMoveBuild<Statement >( else_ );
[1dcd9554]336 node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
[135b431]337
338 return node;
[401e61f]339} // build_waitfor_timeout
[135b431]340
[e67991f]341Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
[436bbe5]342 list< Expression * > e;
[a378ca7]343 buildMoveList( exprs, e );
344 Statement * s = maybeMoveBuild<Statement>( stmt );
[e67991f]345 return new DeclStmt( new WithStmt( e, s ) );
[401e61f]346} // build_with
[a378ca7]347
[61fc4f6]348Statement * build_compound( StatementNode * first ) {
349 CompoundStmt * cs = new CompoundStmt();
[7ecbb7e]350 buildMoveList( first, cs->get_kids() );
[b87a5ed]351 return cs;
[401e61f]352} // build_compound
[51b73452]353
[a025ea8]354// A single statement in a control structure is always converted to a compound statement so subsequent generated code
355// can be placed within this compound statement. Otherwise, code generation has to constantly check for a single
356// statement and wrap it into a compound statement to insert additional code. Hence, all control structures have a
357// conical form for code generation.
358StatementNode * maybe_build_compound( StatementNode * first ) {
359 // Optimization: if the control-structure statement is a compound statement, do not wrap it.
360 // e.g., if (...) {...} do not wrap the existing compound statement.
361 if ( ! dynamic_cast<CompoundStmt *>( first->stmt.get() ) ) { // unique_ptr
362 CompoundStmt * cs = new CompoundStmt();
363 buildMoveList( first, cs->get_kids() );
364 return new StatementNode( cs );
365 } // if
366 return first;
367} // maybe_build_compound
368
[6d539f83]369Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
[436bbe5]370 list< Expression * > out, in;
371 list< ConstantExpr * > clob;
[e82aa9df]372
[7ecbb7e]373 buildMoveList( output, out );
374 buildMoveList( input, in );
375 buildMoveList( clobber, clob );
[ba3706f]376 return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
[401e61f]377} // build_asm
[7f5566b]378
[6d539f83]379Statement * build_directive( string * directive ) {
[cc32d83]380 return new DirectiveStmt( *directive );
[401e61f]381} // build_directive
[61fc4f6]382
[de52331]383Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ) {
[436bbe5]384 list< Expression * > expList;
[de52331]385 buildMoveList( exprs, expList );
386 Statement * body = maybeMoveBuild<Statement>( stmt );
387 return new MutexStmt( body, expList );
388} // build_mutex
389
[51b73452]390// Local Variables: //
[b87a5ed]391// tab-width: 4 //
392// mode: c++ //
393// compile-command: "make install" //
[51b73452]394// End: //
Note: See TracBrowser for help on using the repository browser.