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
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
[6a276a0]12// Last Modified On : Thu Mar  8 14:31:32 2018
13// Update Count     : 348
[b87a5ed]14//
15
[e3e16bc]16#include <cassert>                 // for assert, strict_dynamic_cast, assertf
[d180746]17#include <list>                    // for list
18#include <string>                  // for string
[51b7345]19
[d180746]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
[6d49ea3]25#include "SynTree/Declaration.h"
[d180746]26#include "SynTree/Statement.h"     // for Statement, BranchStmt, CaseStmt
27#include "parserutility.h"         // for notZeroExpr
28
29class Declaration;
[51b7345]30
31using namespace std;
32
[b87a5ed]33
[e82aa9df]34StatementNode::StatementNode( DeclarationNode *decl ) {
[c0aa336]35        assert( decl );
36        DeclarationNode *agg = decl->extractAggregate();
37        if ( agg ) {
[ba3706f]38                StatementNode *nextStmt = new StatementNode( new DeclStmt( maybeBuild< Declaration >( decl ) ) );
[c0aa336]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
[8d7bef2]51        stmt = new DeclStmt{ maybeMoveBuild< Declaration >(agg) };
[c0aa336]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() ) {
[e3e16bc]58                StatementNode *node = strict_dynamic_cast< StatementNode * >(curr);
[8d7bef2]59                assert( dynamic_cast< CaseStmt * >(node->stmt) );
[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
[8d7bef2]67        CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt);
[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 )
[ba3706f]76                return new ExprStmt( e );
[8cc5cb0]77        else
[ba3706f]78                return new NullStmt();
[8cc5cb0]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
[135b431]94
[936e9f4]95        std::list< Statement * > init;
96        if ( ctl->init != 0 ) {
97                buildMoveList( ctl->init, init );
98        } // if
99
[a01f7c94]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
[e3e16bc]107                        DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt );
108                        DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl );
[a01f7c94]109                        Expression * nze = notZeroExpr( new VariableExpr( dwt ) );
110                        cond = cond ? new LogicalExpr( cond, nze, true ) : nze;
111                }
112        }
[6d49ea3]113        delete ctl;
[ba3706f]114        return new IfStmt( cond, thenb, elseb, init );
[2f22cc4]115}
116
[6a276a0]117Statement *build_switch( bool isSwitch, ExpressionNode *ctl, StatementNode *stmt ) {
[7880579]118        std::list< Statement * > branches;
[7ecbb7e]119        buildMoveList< Statement, StatementNode >( stmt, branches );
[6a276a0]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
[20519b7]129        // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
[ba3706f]130        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
[2f22cc4]131}
[321f55d]132Statement *build_case( ExpressionNode *ctl ) {
[7880579]133        std::list< Statement * > branches;
[ba3706f]134        return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
[321f55d]135}
136Statement *build_default() {
[7880579]137        std::list< Statement * > branches;
[ba3706f]138        return new CaseStmt( nullptr, branches, true );
[321f55d]139}
[2f22cc4]140
141Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
[7880579]142        std::list< Statement * > branches;
[7ecbb7e]143        buildMoveList< Statement, StatementNode >( stmt, branches );
[2f22cc4]144        assert( branches.size() == 1 );
[ba3706f]145        return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
[2f22cc4]146}
147
148Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
[7880579]149        std::list< Statement * > branches;
[7ecbb7e]150        buildMoveList< Statement, StatementNode >( stmt, branches );
[2f22cc4]151        assert( branches.size() == 1 );
152
[7880579]153        std::list< Statement * > init;
[2f22cc4]154        if ( forctl->init != 0 ) {
[7ecbb7e]155                buildMoveList( forctl->init, init );
[2f22cc4]156        } // if
157
158        Expression *cond = 0;
159        if ( forctl->condition != 0 )
[7ecbb7e]160                cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
[2f22cc4]161
162        Expression *incr = 0;
163        if ( forctl->change != 0 )
[7ecbb7e]164                incr = maybeMoveBuild< Expression >(forctl->change);
[2f22cc4]165
166        delete forctl;
[ba3706f]167        return new ForStmt( init, cond, incr, branches.front() );
[2f22cc4]168}
169
[ab57786]170Statement *build_branch( BranchStmt::Type kind ) {
[ba3706f]171        Statement * ret = new BranchStmt( "", kind );
[ab57786]172        return ret;
173}
174Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) {
[ba3706f]175        Statement * ret = new BranchStmt( *identifier, kind );
[ab57786]176        delete identifier;                                                                      // allocated by lexer
177        return ret;
[321f55d]178}
[8cc5cb0]179Statement *build_computedgoto( ExpressionNode *ctl ) {
[ba3706f]180        return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
[8cc5cb0]181}
182
183Statement *build_return( ExpressionNode *ctl ) {
[7880579]184        std::list< Expression * > exps;
[7ecbb7e]185        buildMoveList( ctl, exps );
[ba3706f]186        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
[8cc5cb0]187}
[daf1af8]188
[8cc5cb0]189Statement *build_throw( ExpressionNode *ctl ) {
[7880579]190        std::list< Expression * > exps;
[7ecbb7e]191        buildMoveList( ctl, exps );
[ac71a86]192        assertf( exps.size() < 2, "This means we are leaking memory");
[ba3706f]193        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
[daf1af8]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");
[ba3706f]200        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
[daf1af8]201}
202
203Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {
[794c15b]204        (void)ctl;
205        (void)target;
206        assertf( false, "resume at (non-local throw) is not yet supported," );
[8cc5cb0]207}
[321f55d]208
[1d4580a]209Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
[046e04a]210        std::list< CatchStmt * > branches;
211        buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
[e3e16bc]212        CompoundStmt *tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
[7ecbb7e]213        FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
[ba3706f]214        return new TryStmt( tryBlock, branches, finallyBlock );
[1d4580a]215}
[ca78437]216Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
[7880579]217        std::list< Statement * > branches;
[ca78437]218        buildMoveList< Statement, StatementNode >( body, branches );
[1d4580a]219        assert( branches.size() == 1 );
[ba3706f]220        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
[1d4580a]221}
222Statement *build_finally( StatementNode *stmt ) {
[7880579]223        std::list< Statement * > branches;
[7ecbb7e]224        buildMoveList< Statement, StatementNode >( stmt, branches );
[1d4580a]225        assert( branches.size() == 1 );
[ba3706f]226        return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
[1d4580a]227}
228
[135b431]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 );
[2065609]234
235        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
236        targetExpr->set_next( nullptr );
237        buildMoveList< Expression >( next, target.arguments );
238
[135b431]239        delete targetExpr;
240
241        node->clauses.push_back( WaitForStmt::Clause{
242                target,
243                maybeMoveBuild<Statement >( stmt ),
[1dcd9554]244                notZeroExpr( maybeMoveBuild<Expression>( when ) )
[135b431]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 );
[2065609]253
254        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
255        targetExpr->set_next( nullptr );
256        buildMoveList< Expression >( next, target.arguments );
257
[135b431]258        delete targetExpr;
259
[310e5b7]260        node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{
[135b431]261                std::move( target ),
262                maybeMoveBuild<Statement >( stmt ),
[1dcd9554]263                notZeroExpr( maybeMoveBuild<Expression>( when ) )
[135b431]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    );
[1dcd9554]275                node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[135b431]276        }
277        else {
[1dcd9554]278                node->orelse.statement  = maybeMoveBuild<Statement >( stmt );
279                node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[135b431]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    );
[1dcd9554]290        node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
[135b431]291
[a378ca7]292        node->orelse.statement  = maybeMoveBuild<Statement >( else_stmt );
[1dcd9554]293        node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
[135b431]294
295        return node;
296}
297
[a378ca7]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
[e82aa9df]305Statement *build_compound( StatementNode *first ) {
[ba3706f]306        CompoundStmt *cs = new CompoundStmt();
[7ecbb7e]307        buildMoveList( first, cs->get_kids() );
[b87a5ed]308        return cs;
[51b7345]309}
310
[e612146c]311Statement *build_asmstmt( bool voltile, Expression *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
[7f5566b]312        std::list< Expression * > out, in;
313        std::list< ConstantExpr * > clob;
[e82aa9df]314
[7ecbb7e]315        buildMoveList( output, out );
316        buildMoveList( input, in );
317        buildMoveList( clobber, clob );
[ba3706f]318        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
[7f5566b]319}
320
[51b7345]321// Local Variables: //
[b87a5ed]322// tab-width: 4 //
323// mode: c++ //
324// compile-command: "make install" //
[51b7345]325// End: //
Note: See TracBrowser for help on using the repository browser.