source: src/Parser/StatementNode.cc @ 436bbe5

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 436bbe5 was 436bbe5, checked in by Peter A. Buhr <pabuhr@…>, 2 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
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 : Wed Feb  2 09:45:28 2022
13// Update Count     : 415
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        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} // StatementNode::append_last_case
72
73Statement * build_expr( ExpressionNode * ctl ) {
74        Expression * e = maybeMoveBuild< Expression >( ctl );
75
76        if ( e ) return new ExprStmt( e );
77        else return new NullStmt();
78} // build_expr
79
80Expression * build_if_control( CondCtl * ctl, list< Statement * > & init ) {
81        if ( ctl->init != 0 ) {
82                buildMoveList( ctl->init, init );
83        } // if
84
85        Expression * cond = nullptr;
86        if ( ctl->condition ) {
87                // compare the provided condition against 0
88                cond = notZeroExpr( maybeMoveBuild< Expression >(ctl->condition) );
89        } else {
90                for ( Statement * stmt : init ) {
91                        // build the && of all of the declared variables compared against 0
92                        DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt );
93                        DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl );
94                        Expression * nze = notZeroExpr( new VariableExpr( dwt ) );
95                        cond = cond ? new LogicalExpr( cond, nze, true ) : nze;
96                }
97        }
98        delete ctl;
99        return cond;
100} // build_if_control
101
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();
117        } // if
118
119        return new IfStmt( astcond, astthen, astelse, astinit );
120} // build_if
121
122Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
123        list< Statement * > aststmt;
124        buildMoveList< Statement, StatementNode >( stmt, aststmt );
125        if ( ! isSwitch ) {                                                                     // choose statement
126                for ( Statement * stmt : aststmt ) {
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
134        // aststmt.size() == 0 for switch (...) {}, i.e., no declaration or statements
135        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), aststmt );
136} // build_switch
137
138Statement * build_case( ExpressionNode * ctl ) {
139        return new CaseStmt( maybeMoveBuild< Expression >(ctl), list< Statement * >{} );
140} // build_case
141
142Statement * build_default() {
143        return new CaseStmt( nullptr, list< Statement * >{}, true );
144} // build_default
145
146Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * else_ ) {
147        list< Statement * > astinit;                                            // maybe empty
148        Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
149
150        list< Statement * > aststmt;                                            // loop body, compound created if empty
151        buildMoveList< Statement, StatementNode >( stmt, aststmt );
152        assert( aststmt.size() == 1 );
153
154        list< Statement * > astelse;                                            // else clause, maybe empty
155        buildMoveList< Statement, StatementNode >( else_, astelse );
156
157        return new WhileDoStmt( astcond, aststmt.front(), astelse.front(), astinit, false );
158} // build_while
159
160Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * else_ ) {
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
165        buildMoveList< Statement, StatementNode >( stmt, aststmt );
166        assert( aststmt.size() == 1 );                                          // compound created if empty
167
168        list< Statement * > astelse;                                            // else clause, maybe empty
169        buildMoveList< Statement, StatementNode >( else_, astelse );
170
171        return new WhileDoStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), aststmt.front(), astelse.front(), astinit, true );
172} // build_do_while
173
174Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ ) {
175        list< Statement * > astinit;                                            // maybe empty
176        buildMoveList( forctl->init, astinit );
177
178        Expression * astcond = nullptr;                                         // maybe empty
179        astcond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
180
181        Expression * astincr = nullptr;                                         // maybe empty
182        astincr = maybeMoveBuild< Expression >(forctl->change);
183        delete forctl;
184
185        list< Statement * > aststmt;                                            // loop body, compound created if empty
186        buildMoveList< Statement, StatementNode >( stmt, aststmt );
187        assert( aststmt.size() == 1 );
188
189        list< Statement * > astelse;                                            // else clause, maybe empty
190        buildMoveList< Statement, StatementNode >( else_, astelse );
191
192        return new ForStmt( astinit, astcond, astincr, aststmt.front(), astelse.front() );
193} // build_for
194
195Statement * build_branch( BranchStmt::Type kind ) {
196        Statement * ret = new BranchStmt( "", kind );
197        return ret;
198} // build_branch
199
200Statement * build_branch( string * identifier, BranchStmt::Type kind ) {
201        Statement * ret = new BranchStmt( * identifier, kind );
202        delete identifier;                                                                      // allocated by lexer
203        return ret;
204} // build_branch
205
206Statement * build_computedgoto( ExpressionNode * ctl ) {
207        return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
208} // build_computedgoto
209
210Statement * build_return( ExpressionNode * ctl ) {
211        list< Expression * > exps;
212        buildMoveList( ctl, exps );
213        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
214} // build_return
215
216Statement * build_throw( ExpressionNode * ctl ) {
217        list< Expression * > exps;
218        buildMoveList( ctl, exps );
219        assertf( exps.size() < 2, "CFA internal error: leaking memory" );
220        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
221} // build_throw
222
223Statement * build_resume( ExpressionNode * ctl ) {
224        list< Expression * > exps;
225        buildMoveList( ctl, exps );
226        assertf( exps.size() < 2, "CFA internal error: leaking memory" );
227        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
228} // build_resume
229
230Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
231        (void)ctl;
232        (void)target;
233        assertf( false, "resume at (non-local throw) is not yet supported," );
234} // build_resume_at
235
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 );
242} // build_try
243
244Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
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() );
249} // build_catch
250
251Statement * build_finally( StatementNode * stmt ) {
252        list< Statement * > aststmt;
253        buildMoveList< Statement, StatementNode >( stmt, aststmt );
254        assert( aststmt.size() == 1 );
255        return new FinallyStmt( dynamic_cast< CompoundStmt * >( aststmt.front() ) );
256} // build_finally
257
258SuspendStmt * build_suspend( StatementNode * then, SuspendStmt::Type type ) {
259        auto node = new SuspendStmt();
260
261        node->type = type;
262
263        list< Statement * > stmts;
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
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 );
278
279        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
280        targetExpr->set_next( nullptr );
281        buildMoveList< Expression >( next, target.arguments );
282
283        delete targetExpr;
284
285        node->clauses.push_back( WaitForStmt::Clause{
286                target,
287                maybeMoveBuild<Statement >( stmt ),
288                notZeroExpr( maybeMoveBuild<Expression>( when ) )
289        });
290
291        return node;
292} // build_waitfor
293
294WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
295        WaitForStmt::Target target;
296        target.function = maybeBuild<Expression>( targetExpr );
297
298        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
299        targetExpr->set_next( nullptr );
300        buildMoveList< Expression >( next, target.arguments );
301
302        delete targetExpr;
303
304        node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{
305                std::move( target ),
306                maybeMoveBuild<Statement >( stmt ),
307                notZeroExpr( maybeMoveBuild<Expression>( when ) )
308        });
309
310        return node;
311} // build_waitfor
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    );
319                node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
320        } else {
321                node->orelse.statement  = maybeMoveBuild<Statement >( stmt );
322                node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( when ) );
323        } // if
324
325        return node;
326} // build_waitfor_timeout
327
328WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_, ExpressionNode * else_when ) {
329        auto node = new WaitForStmt();
330
331        node->timeout.time      = maybeMoveBuild<Expression>( timeout );
332        node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
333        node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
334
335        node->orelse.statement  = maybeMoveBuild<Statement >( else_ );
336        node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
337
338        return node;
339} // build_waitfor_timeout
340
341Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
342        list< Expression * > e;
343        buildMoveList( exprs, e );
344        Statement * s = maybeMoveBuild<Statement>( stmt );
345        return new DeclStmt( new WithStmt( e, s ) );
346} // build_with
347
348Statement * build_compound( StatementNode * first ) {
349        CompoundStmt * cs = new CompoundStmt();
350        buildMoveList( first, cs->get_kids() );
351        return cs;
352} // build_compound
353
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
369Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
370        list< Expression * > out, in;
371        list< ConstantExpr * > clob;
372
373        buildMoveList( output, out );
374        buildMoveList( input, in );
375        buildMoveList( clobber, clob );
376        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
377} // build_asm
378
379Statement * build_directive( string * directive ) {
380        return new DirectiveStmt( *directive );
381} // build_directive
382
383Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ) {
384        list< Expression * > expList;
385        buildMoveList( exprs, expList );
386        Statement * body = maybeMoveBuild<Statement>( stmt );
387        return new MutexStmt( body, expList );
388} // build_mutex
389
390// Local Variables: //
391// tab-width: 4 //
392// mode: c++ //
393// compile-command: "make install" //
394// End: //
Note: See TracBrowser for help on using the repository browser.