source: src/Parser/StatementNode.cc @ 3b0bc16

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 3b0bc16 was 3b0bc16, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

change class name WhileStmt? to WhileDoStmt?, add else clause to WhileDoStmt? and ForStmt?, change names thenPart/ElsePart to then/else_

  • Property mode set to 100644
File size: 14.5 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 : Tue Feb  1 18:39:00 2022
13// Update Count     : 395
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} // 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, std::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_stmt, StatementNode * else_stmt ) {
103        Statement * thenb, * elseb = nullptr;
104        std::list< Statement * > branches;
105        buildMoveList< Statement, StatementNode >( then_stmt, branches );
106        assert( branches.size() == 1 );
107        thenb = branches.front();
108
109        if ( else_stmt ) {
110                std::list< Statement * > branches;
111                buildMoveList< Statement, StatementNode >( else_stmt, branches );
112                assert( branches.size() == 1 );
113                elseb = branches.front();
114        } // if
115
116        std::list< Statement * > init;
117        Expression * cond = build_if_control( ctl, init );
118        return new IfStmt( cond, thenb, elseb, init );
119} // build_if
120
121Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
122        std::list< Statement * > branches;
123        buildMoveList< Statement, StatementNode >( stmt, branches );
124        if ( ! isSwitch ) {                                                                             // choose statement
125                for ( Statement * stmt : branches ) {
126                        CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
127                        if ( ! caseStmt->stmts.empty() ) {                      // code after "case" => end of case list
128                                CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() );
129                                block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) );
130                        } // if
131                } // for
132        } // if
133        // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
134        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
135} // build_switch
136
137Statement * build_case( ExpressionNode * ctl ) {
138        std::list< Statement * > branches;
139        return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
140} // build_case
141
142Statement * build_default() {
143        std::list< Statement * > branches;
144        return new CaseStmt( nullptr, branches, true );
145} // build_default
146
147Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * else_ ) {
148        std::list< Statement * > init;
149        Expression * cond = build_if_control( ctl, init );
150
151        std::list< Statement * > aststmt;
152        buildMoveList< Statement, StatementNode >( stmt, aststmt );
153        assert( aststmt.size() == 1 );
154
155        std::list< Statement * > astelse;
156        buildMoveList< Statement, StatementNode >( else_, astelse );
157
158        return new WhileDoStmt( cond, aststmt.front(), astelse.front(), init, false );
159} // build_while
160
161Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * else_ ) {
162        std::list< Statement * > aststmt;
163        buildMoveList< Statement, StatementNode >( stmt, aststmt );
164        assert( aststmt.size() == 1 );
165
166        std::list< Statement * > astelse;
167        buildMoveList< Statement, StatementNode >( else_, astelse );
168
169        std::list< Statement * > init;
170        return new WhileDoStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), aststmt.front(), astelse.front(), init, true );
171} // build_do_while
172
173Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ ) {
174        std::list< Statement * > init;
175        if ( forctl->init != nullptr ) {
176                buildMoveList( forctl->init, init );
177        } // if
178
179        Expression * cond = nullptr;
180        if ( forctl->condition != nullptr )
181                cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
182
183        Expression * incr = nullptr;
184        if ( forctl->change != nullptr )
185                incr = maybeMoveBuild< Expression >(forctl->change);
186
187        std::list< Statement * > aststmt;
188        buildMoveList< Statement, StatementNode >( stmt, aststmt );
189        assert( aststmt.size() == 1 );
190
191        std::list< Statement * > astelse;
192        buildMoveList< Statement, StatementNode >( else_, astelse );
193
194        delete forctl;
195        return new ForStmt( init, cond, incr, aststmt.front(), astelse.front() );
196} // build_for
197
198Statement * build_branch( BranchStmt::Type kind ) {
199        Statement * ret = new BranchStmt( "", kind );
200        return ret;
201} // build_branch
202
203Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
204        Statement * ret = new BranchStmt( * identifier, kind );
205        delete identifier;                                                                      // allocated by lexer
206        return ret;
207} // build_branch
208
209Statement * build_computedgoto( ExpressionNode * ctl ) {
210        return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
211} // build_computedgoto
212
213Statement * build_return( ExpressionNode * ctl ) {
214        std::list< Expression * > exps;
215        buildMoveList( ctl, exps );
216        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
217} // build_return
218
219Statement * build_throw( ExpressionNode * ctl ) {
220        std::list< Expression * > exps;
221        buildMoveList( ctl, exps );
222        assertf( exps.size() < 2, "This means we are leaking memory");
223        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
224} // build_throw
225
226Statement * build_resume( ExpressionNode * ctl ) {
227        std::list< Expression * > exps;
228        buildMoveList( ctl, exps );
229        assertf( exps.size() < 2, "This means we are leaking memory");
230        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
231} // build_resume
232
233Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
234        (void)ctl;
235        (void)target;
236        assertf( false, "resume at (non-local throw) is not yet supported," );
237} // build_resume_at
238
239Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) {
240        std::list< CatchStmt * > branches;
241        buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
242        CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
243        FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
244        return new TryStmt( tryBlock, branches, finallyBlock );
245} // build_try
246
247Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
248        std::list< Statement * > branches;
249        buildMoveList< Statement, StatementNode >( body, branches );
250        assert( branches.size() == 1 );
251        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
252} // build_catch
253
254Statement * build_finally( StatementNode * stmt ) {
255        std::list< Statement * > branches;
256        buildMoveList< Statement, StatementNode >( stmt, branches );
257        assert( branches.size() == 1 );
258        return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
259} // build_finally
260
261SuspendStmt * build_suspend( StatementNode * then, SuspendStmt::Type type ) {
262        auto node = new SuspendStmt();
263
264        node->type = type;
265
266        std::list< Statement * > stmts;
267        buildMoveList< Statement, StatementNode >( then, stmts );
268        if(!stmts.empty()) {
269                assert( stmts.size() == 1 );
270                node->then = dynamic_cast< CompoundStmt * >( stmts.front() );
271        }
272
273        return node;
274}
275
276WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when ) {
277        auto node = new WaitForStmt();
278
279        WaitForStmt::Target target;
280        target.function = maybeBuild<Expression>( targetExpr );
281
282        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
283        targetExpr->set_next( nullptr );
284        buildMoveList< Expression >( next, target.arguments );
285
286        delete targetExpr;
287
288        node->clauses.push_back( WaitForStmt::Clause{
289                target,
290                maybeMoveBuild<Statement >( stmt ),
291                notZeroExpr( maybeMoveBuild<Expression>( when ) )
292        });
293
294        return node;
295} // build_waitfor
296
297WaitForStmt * build_waitfor( ExpressionNode * targetExpr, StatementNode * stmt, ExpressionNode * when, WaitForStmt * node ) {
298        WaitForStmt::Target target;
299        target.function = maybeBuild<Expression>( targetExpr );
300
301        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
302        targetExpr->set_next( nullptr );
303        buildMoveList< Expression >( next, target.arguments );
304
305        delete targetExpr;
306
307        node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{
308                std::move( target ),
309                maybeMoveBuild<Statement >( stmt ),
310                notZeroExpr( maybeMoveBuild<Expression>( when ) )
311        });
312
313        return node;
314} // build_waitfor
315
316WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when ) {
317        auto node = new WaitForStmt();
318
319        if( timeout ) {
320                node->timeout.time      = maybeMoveBuild<Expression>( timeout );
321                node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
322                node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
323        } else {
324                node->orelse.statement  = maybeMoveBuild<Statement >( stmt );
325                node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( when ) );
326        } // if
327
328        return node;
329} // build_waitfor_timeout
330
331WaitForStmt * build_waitfor_timeout( ExpressionNode * timeout, StatementNode * stmt, ExpressionNode * when,  StatementNode * else_stmt, ExpressionNode * else_when ) {
332        auto node = new WaitForStmt();
333
334        node->timeout.time      = maybeMoveBuild<Expression>( timeout );
335        node->timeout.statement = maybeMoveBuild<Statement >( stmt    );
336        node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
337
338        node->orelse.statement  = maybeMoveBuild<Statement >( else_stmt );
339        node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
340
341        return node;
342} // build_waitfor_timeout
343
344Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
345        std::list< Expression * > e;
346        buildMoveList( exprs, e );
347        Statement * s = maybeMoveBuild<Statement>( stmt );
348        return new DeclStmt( new WithStmt( e, s ) );
349} // build_with
350
351Statement * build_compound( StatementNode * first ) {
352        CompoundStmt * cs = new CompoundStmt();
353        buildMoveList( first, cs->get_kids() );
354        return cs;
355} // build_compound
356
357// A single statement in a control structure is always converted to a compound statement so subsequent generated code
358// can be placed within this compound statement. Otherwise, code generation has to constantly check for a single
359// statement and wrap it into a compound statement to insert additional code. Hence, all control structures have a
360// conical form for code generation.
361StatementNode * maybe_build_compound( StatementNode * first ) {
362        // Optimization: if the control-structure statement is a compound statement, do not wrap it.
363        // e.g., if (...) {...} do not wrap the existing compound statement.
364        if ( ! dynamic_cast<CompoundStmt *>( first->stmt.get() ) ) { // unique_ptr
365                CompoundStmt * cs = new CompoundStmt();
366                buildMoveList( first, cs->get_kids() );
367                return new StatementNode( cs );
368        } // if
369        return first;
370} // maybe_build_compound
371
372Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
373        std::list< Expression * > out, in;
374        std::list< ConstantExpr * > clob;
375
376        buildMoveList( output, out );
377        buildMoveList( input, in );
378        buildMoveList( clobber, clob );
379        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
380} // build_asm
381
382Statement * build_directive( string * directive ) {
383        return new DirectiveStmt( *directive );
384} // build_directive
385
386Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ) {
387        std::list< Expression * > expList;
388        buildMoveList( exprs, expList );
389        Statement * body = maybeMoveBuild<Statement>( stmt );
390        return new MutexStmt( body, expList );
391} // build_mutex
392
393// Local Variables: //
394// tab-width: 4 //
395// mode: c++ //
396// compile-command: "make install" //
397// End: //
Note: See TracBrowser for help on using the repository browser.