source: src/Parser/StatementNode.cc @ cc32d83

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 cc32d83 was cc32d83, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Push pragma directives through the translator

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