source: src/Parser/StatementNode.cc @ ee3c93d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since ee3c93d was ee3c93d, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Add support for while loops with control declarations

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