source: src/Parser/StatementNode.cc@ dbc2c2c

new-env with_gc
Last change on this file since dbc2c2c was 8d7bef2, checked in by Aaron Moss <a3moss@…>, 8 years ago

First compiling build of CFA-CC with GC

  • Property mode set to 100644
File size: 11.0 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
[e612146c]12// Last Modified On : Fri Sep 1 23:25:23 2017
13// Update Count : 346
[b87a5ed]14//
15
[e3e16bc]16#include <cassert> // for assert, strict_dynamic_cast, assertf
[d180746]17#include <list> // for list
18#include <string> // for string
[51b73452]19
[d180746]20#include "Common/SemanticError.h" // for SemanticError
21#include "Common/utility.h" // for maybeMoveBuild, maybeBuild
22#include "ParseNode.h" // for StatementNode, ExpressionNode, bui...
23#include "SynTree/Expression.h" // for Expression, ConstantExpr
24#include "SynTree/Label.h" // for Label, noLabels
[6d49ea3]25#include "SynTree/Declaration.h"
[d180746]26#include "SynTree/Statement.h" // for Statement, BranchStmt, CaseStmt
27#include "parserutility.h" // for notZeroExpr
28
29class Declaration;
[51b73452]30
31using namespace std;
32
[b87a5ed]33
[e82aa9df]34StatementNode::StatementNode( DeclarationNode *decl ) {
[c0aa336]35 assert( decl );
36 DeclarationNode *agg = decl->extractAggregate();
37 if ( agg ) {
[ba3706f]38 StatementNode *nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
[c0aa336]39 set_next( nextStmt );
40 if ( decl->get_next() ) {
41 get_next()->set_next( new StatementNode( dynamic_cast< DeclarationNode * >(decl->get_next()) ) );
42 decl->set_next( 0 );
[1d4580a]43 } // if
44 } else {
[c0aa336]45 if ( decl->get_next() ) {
46 set_next( new StatementNode( dynamic_cast< DeclarationNode * >( decl->get_next() ) ) );
47 decl->set_next( 0 );
48 } // if
49 agg = decl;
[1d4580a]50 } // if
[8d7bef2]51 stmt = new DeclStmt{ maybeMoveBuild< Declaration >(agg) };
[c0aa336]52} // StatementNode::StatementNode
[1d4580a]53
[b87a5ed]54StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
[8cc5cb0]55 StatementNode *prev = this;
[1d4580a]56 // find end of list and maintain previous pointer
57 for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
[e3e16bc]58 StatementNode *node = strict_dynamic_cast< StatementNode * >(curr);
[8d7bef2]59 assert( dynamic_cast< CaseStmt * >(node->stmt) );
[8cc5cb0]60 prev = curr;
61 } // for
[e82aa9df]62 // convert from StatementNode list to Statement list
[7880579]63 StatementNode *node = dynamic_cast< StatementNode * >(prev);
64 std::list< Statement * > stmts;
[7ecbb7e]65 buildMoveList( stmt, stmts );
[e82aa9df]66 // splice any new Statements to end of current Statements
[8d7bef2]67 CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt);
[8cc5cb0]68 caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
69 return this;
70}
71
72Statement *build_expr( ExpressionNode *ctl ) {
[7ecbb7e]73 Expression *e = maybeMoveBuild< Expression >( ctl );
[8cc5cb0]74
75 if ( e )
[ba3706f]76 return new ExprStmt( e );
[8cc5cb0]77 else
[ba3706f]78 return new NullStmt();
[8cc5cb0]79}
80
[936e9f4]81Statement *build_if( IfCtl * ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {
[2f22cc4]82 Statement *thenb, *elseb = 0;
[7880579]83 std::list< Statement * > branches;
[7ecbb7e]84 buildMoveList< Statement, StatementNode >( then_stmt, branches );
[321f55d]85 assert( branches.size() == 1 );
[2f22cc4]86 thenb = branches.front();
87
88 if ( else_stmt ) {
[7880579]89 std::list< Statement * > branches;
[7ecbb7e]90 buildMoveList< Statement, StatementNode >( else_stmt, branches );
[321f55d]91 assert( branches.size() == 1 );
[2f22cc4]92 elseb = branches.front();
93 } // if
[135b431]94
[936e9f4]95 std::list< Statement * > init;
96 if ( ctl->init != 0 ) {
97 buildMoveList( ctl->init, init );
98 } // if
99
[a01f7c94]100 Expression * cond = nullptr;
101 if ( ctl->condition ) {
102 // compare the provided condition against 0
103 cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
104 } else {
105 for ( Statement * stmt : init ) {
106 // build the && of all of the declared variables compared against 0
[e3e16bc]107 DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt );
108 DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl );
[a01f7c94]109 Expression * nze = notZeroExpr( new VariableExpr( dwt ) );
110 cond = cond ? new LogicalExpr( cond, nze, true ) : nze;
111 }
112 }
[6d49ea3]113 delete ctl;
[ba3706f]114 return new IfStmt( cond, thenb, elseb, init );
[2f22cc4]115}
116
117Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
[7880579]118 std::list< Statement * > branches;
[7ecbb7e]119 buildMoveList< Statement, StatementNode >( stmt, branches );
[20519b7]120 // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
[ba3706f]121 return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
[2f22cc4]122}
[321f55d]123Statement *build_case( ExpressionNode *ctl ) {
[7880579]124 std::list< Statement * > branches;
[ba3706f]125 return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
[321f55d]126}
127Statement *build_default() {
[7880579]128 std::list< Statement * > branches;
[ba3706f]129 return new CaseStmt( nullptr, branches, true );
[321f55d]130}
[2f22cc4]131
132Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
[7880579]133 std::list< Statement * > branches;
[7ecbb7e]134 buildMoveList< Statement, StatementNode >( stmt, branches );
[2f22cc4]135 assert( branches.size() == 1 );
[ba3706f]136 return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
[2f22cc4]137}
138
139Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
[7880579]140 std::list< Statement * > branches;
[7ecbb7e]141 buildMoveList< Statement, StatementNode >( stmt, branches );
[2f22cc4]142 assert( branches.size() == 1 );
143
[7880579]144 std::list< Statement * > init;
[2f22cc4]145 if ( forctl->init != 0 ) {
[7ecbb7e]146 buildMoveList( forctl->init, init );
[2f22cc4]147 } // if
148
149 Expression *cond = 0;
150 if ( forctl->condition != 0 )
[7ecbb7e]151 cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
[2f22cc4]152
153 Expression *incr = 0;
154 if ( forctl->change != 0 )
[7ecbb7e]155 incr = maybeMoveBuild< Expression >(forctl->change);
[2f22cc4]156
157 delete forctl;
[ba3706f]158 return new ForStmt( init, cond, incr, branches.front() );
[2f22cc4]159}
160
[ab57786]161Statement *build_branch( BranchStmt::Type kind ) {
[ba3706f]162 Statement * ret = new BranchStmt( "", kind );
[ab57786]163 return ret;
164}
165Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) {
[ba3706f]166 Statement * ret = new BranchStmt( *identifier, kind );
[ab57786]167 delete identifier; // allocated by lexer
168 return ret;
[321f55d]169}
[8cc5cb0]170Statement *build_computedgoto( ExpressionNode *ctl ) {
[ba3706f]171 return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
[8cc5cb0]172}
173
174Statement *build_return( ExpressionNode *ctl ) {
[7880579]175 std::list< Expression * > exps;
[7ecbb7e]176 buildMoveList( ctl, exps );
[ba3706f]177 return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
[8cc5cb0]178}
[daf1af8]179
[8cc5cb0]180Statement *build_throw( ExpressionNode *ctl ) {
[7880579]181 std::list< Expression * > exps;
[7ecbb7e]182 buildMoveList( ctl, exps );
[ac71a86]183 assertf( exps.size() < 2, "This means we are leaking memory");
[ba3706f]184 return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
[daf1af8]185}
186
187Statement *build_resume( ExpressionNode *ctl ) {
188 std::list< Expression * > exps;
189 buildMoveList( ctl, exps );
190 assertf( exps.size() < 2, "This means we are leaking memory");
[ba3706f]191 return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
[daf1af8]192}
193
194Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {
[794c15b]195 (void)ctl;
196 (void)target;
197 assertf( false, "resume at (non-local throw) is not yet supported," );
[8cc5cb0]198}
[321f55d]199
[1d4580a]200Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
[046e04a]201 std::list< CatchStmt * > branches;
202 buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
[e3e16bc]203 CompoundStmt *tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
[7ecbb7e]204 FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
[ba3706f]205 return new TryStmt( tryBlock, branches, finallyBlock );
[1d4580a]206}
[ca78437]207Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
[7880579]208 std::list< Statement * > branches;
[ca78437]209 buildMoveList< Statement, StatementNode >( body, branches );
[1d4580a]210 assert( branches.size() == 1 );
[ba3706f]211 return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
[1d4580a]212}
213Statement *build_finally( StatementNode *stmt ) {
[7880579]214 std::list< Statement * > branches;
[7ecbb7e]215 buildMoveList< Statement, StatementNode >( stmt, branches );
[1d4580a]216 assert( branches.size() == 1 );
[ba3706f]217 return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
[1d4580a]218}
219
[135b431]220WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
221 auto node = new WaitForStmt();
222
223 WaitForStmt::Target target;
224 target.function = maybeBuild<Expression>( targetExpr );
[2065609]225
226 ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
227 targetExpr->set_next( nullptr );
228 buildMoveList< Expression >( next, target.arguments );
229
[135b431]230 delete targetExpr;
231
232 node->clauses.push_back( WaitForStmt::Clause{
233 target,
234 maybeMoveBuild<Statement >( stmt ),
[1dcd9554]235 notZeroExpr( maybeMoveBuild<Expression>( when ) )
[135b431]236 });
237
238 return node;
239}
240
241WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
242 WaitForStmt::Target target;
243 target.function = maybeBuild<Expression>( targetExpr );
[2065609]244
245 ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
246 targetExpr->set_next( nullptr );
247 buildMoveList< Expression >( next, target.arguments );
248
[135b431]249 delete targetExpr;
250
[310e5b7]251 node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{
[135b431]252 std::move( target ),
253 maybeMoveBuild<Statement >( stmt ),
[1dcd9554]254 notZeroExpr( maybeMoveBuild<Expression>( when ) )
[135b431]255 });
256
257 return node;
258}
259
260WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
261 auto node = new WaitForStmt();
262
263 if( timeout ) {
264 node->timeout.time = maybeMoveBuild<Expression>( timeout );
265 node->timeout.statement = maybeMoveBuild<Statement >( stmt );
[1dcd9554]266 node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[135b431]267 }
268 else {
[1dcd9554]269 node->orelse.statement = maybeMoveBuild<Statement >( stmt );
270 node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[135b431]271 }
272
273 return node;
274}
275
276WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when, StatementNode * else_stmt, ExpressionNode * else_when ) {
277 auto node = new WaitForStmt();
278
279 node->timeout.time = maybeMoveBuild<Expression>( timeout );
280 node->timeout.statement = maybeMoveBuild<Statement >( stmt );
[1dcd9554]281 node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[135b431]282
[a378ca7]283 node->orelse.statement = maybeMoveBuild<Statement >( else_stmt );
[1dcd9554]284 node->orelse.condition = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
[135b431]285
286 return node;
287}
288
[a378ca7]289WithStmt * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
290 std::list< Expression * > e;
291 buildMoveList( exprs, e );
292 Statement * s = maybeMoveBuild<Statement>( stmt );
293 return new WithStmt( e, s );
294}
295
[e82aa9df]296Statement *build_compound( StatementNode *first ) {
[ba3706f]297 CompoundStmt *cs = new CompoundStmt();
[7ecbb7e]298 buildMoveList( first, cs->get_kids() );
[b87a5ed]299 return cs;
[51b73452]300}
301
[e612146c]302Statement *build_asmstmt( bool voltile, Expression *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
[7f5566b]303 std::list< Expression * > out, in;
304 std::list< ConstantExpr * > clob;
[e82aa9df]305
[7ecbb7e]306 buildMoveList( output, out );
307 buildMoveList( input, in );
308 buildMoveList( clobber, clob );
[ba3706f]309 return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
[7f5566b]310}
311
[51b73452]312// Local Variables: //
[b87a5ed]313// tab-width: 4 //
314// mode: c++ //
315// compile-command: "make install" //
[51b73452]316// End: //
Note: See TracBrowser for help on using the repository browser.