source: src/Parser/StatementNode.cc @ 1cdfa82

new-envwith_gc
Last change on this file since 1cdfa82 was 1cdfa82, checked in by Aaron Moss <a3moss@…>, 6 years ago

Merge remote-tracking branch 'origin/master' into with_gc

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