source: src/Parser/StatementNode.cc@ 21f0aa8

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 21f0aa8 was 936e9f4, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

first attempt, add declarations into "if" conditional

  • Property mode set to 100644
File size: 8.4 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
[936e9f4]12// Last Modified On : Wed Aug 16 16:39:43 2017
13// Update Count : 340
[b87a5ed]14//
15
[d180746]16#include <cassert> // for assert, safe_dynamic_cast, assertf
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
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 ) {
38 StatementNode *nextStmt = new StatementNode( new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) ) );
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
[c0aa336]51 stmt.reset( new DeclStmt( noLabels, maybeMoveBuild< Declaration >(agg) ) );
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() ) {
[4f147cc]58 StatementNode *node = safe_dynamic_cast< StatementNode * >(curr);
[ac71a86]59 assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
[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
[ac71a86]67 CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get());
[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 )
76 return new ExprStmt( noLabels, e );
77 else
78 return new NullStmt( noLabels );
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
[936e9f4]94
95 std::list< Statement * > init;
96 if ( ctl->init != 0 ) {
97 buildMoveList( ctl->init, init );
98 } // if
99
100 return new IfStmt( noLabels, notZeroExpr(
101 /*ctl->condition
102 ?*/ maybeMoveBuild< Expression >(ctl->condition)
103 /*: new VariableExpr( init.end() )*/ )
104 , thenb, elseb );
105 // ret->initialization = init;
106 // delete ctl;
107 // assert( ret );
108 // return ret;
[2f22cc4]109}
110
111Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
[7880579]112 std::list< Statement * > branches;
[7ecbb7e]113 buildMoveList< Statement, StatementNode >( stmt, branches );
[20519b7]114 // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
[7ecbb7e]115 return new SwitchStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches );
[2f22cc4]116}
[321f55d]117Statement *build_case( ExpressionNode *ctl ) {
[7880579]118 std::list< Statement * > branches;
[7ecbb7e]119 return new CaseStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches );
[321f55d]120}
121Statement *build_default() {
[7880579]122 std::list< Statement * > branches;
[321f55d]123 return new CaseStmt( noLabels, nullptr, branches, true );
124}
[2f22cc4]125
126Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
[7880579]127 std::list< Statement * > branches;
[7ecbb7e]128 buildMoveList< Statement, StatementNode >( stmt, branches );
[2f22cc4]129 assert( branches.size() == 1 );
[7ecbb7e]130 return new WhileStmt( noLabels, notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
[2f22cc4]131}
132
133Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
[7880579]134 std::list< Statement * > branches;
[7ecbb7e]135 buildMoveList< Statement, StatementNode >( stmt, branches );
[2f22cc4]136 assert( branches.size() == 1 );
137
[7880579]138 std::list< Statement * > init;
[2f22cc4]139 if ( forctl->init != 0 ) {
[7ecbb7e]140 buildMoveList( forctl->init, init );
[2f22cc4]141 } // if
142
143 Expression *cond = 0;
144 if ( forctl->condition != 0 )
[7ecbb7e]145 cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
[2f22cc4]146
147 Expression *incr = 0;
148 if ( forctl->change != 0 )
[7ecbb7e]149 incr = maybeMoveBuild< Expression >(forctl->change);
[2f22cc4]150
151 delete forctl;
152 return new ForStmt( noLabels, init, cond, incr, branches.front() );
153}
154
[ab57786]155Statement *build_branch( BranchStmt::Type kind ) {
156 Statement * ret = new BranchStmt( noLabels, "", kind );
157 return ret;
158}
159Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) {
160 Statement * ret = new BranchStmt( noLabels, *identifier, kind );
161 delete identifier; // allocated by lexer
162 return ret;
[321f55d]163}
[8cc5cb0]164Statement *build_computedgoto( ExpressionNode *ctl ) {
[7ecbb7e]165 return new BranchStmt( noLabels, maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
[8cc5cb0]166}
167
168Statement *build_return( ExpressionNode *ctl ) {
[7880579]169 std::list< Expression * > exps;
[7ecbb7e]170 buildMoveList( ctl, exps );
[8cc5cb0]171 return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr );
172}
[daf1af8]173
[8cc5cb0]174Statement *build_throw( ExpressionNode *ctl ) {
[7880579]175 std::list< Expression * > exps;
[7ecbb7e]176 buildMoveList( ctl, exps );
[ac71a86]177 assertf( exps.size() < 2, "This means we are leaking memory");
[daf1af8]178 return new ThrowStmt( noLabels, ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
179}
180
181Statement *build_resume( ExpressionNode *ctl ) {
182 std::list< Expression * > exps;
183 buildMoveList( ctl, exps );
184 assertf( exps.size() < 2, "This means we are leaking memory");
185 return new ThrowStmt( noLabels, ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
186}
187
188Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {
[794c15b]189 (void)ctl;
190 (void)target;
191 assertf( false, "resume at (non-local throw) is not yet supported," );
[8cc5cb0]192}
[321f55d]193
[1d4580a]194Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
[046e04a]195 std::list< CatchStmt * > branches;
196 buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
[4f147cc]197 CompoundStmt *tryBlock = safe_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
[7ecbb7e]198 FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
[1d4580a]199 return new TryStmt( noLabels, tryBlock, branches, finallyBlock );
200}
[ca78437]201Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
[7880579]202 std::list< Statement * > branches;
[ca78437]203 buildMoveList< Statement, StatementNode >( body, branches );
[1d4580a]204 assert( branches.size() == 1 );
[ca78437]205 return new CatchStmt( noLabels, kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
[1d4580a]206}
207Statement *build_finally( StatementNode *stmt ) {
[7880579]208 std::list< Statement * > branches;
[7ecbb7e]209 buildMoveList< Statement, StatementNode >( stmt, branches );
[1d4580a]210 assert( branches.size() == 1 );
[7880579]211 return new FinallyStmt( noLabels, dynamic_cast< CompoundStmt * >( branches.front() ) );
[1d4580a]212}
213
[e82aa9df]214Statement *build_compound( StatementNode *first ) {
215 CompoundStmt *cs = new CompoundStmt( noLabels );
[7ecbb7e]216 buildMoveList( first, cs->get_kids() );
[b87a5ed]217 return cs;
[51b73452]218}
219
[e82aa9df]220Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
[7f5566b]221 std::list< Expression * > out, in;
222 std::list< ConstantExpr * > clob;
[e82aa9df]223
[7ecbb7e]224 buildMoveList( output, out );
225 buildMoveList( input, in );
226 buildMoveList( clobber, clob );
[e82aa9df]227 return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
[7f5566b]228}
229
[51b73452]230// Local Variables: //
[b87a5ed]231// tab-width: 4 //
232// mode: c++ //
233// compile-command: "make install" //
[51b73452]234// End: //
Note: See TracBrowser for help on using the repository browser.