source: src/Parser/StatementNode.cc @ 59c05958

ADTast-experimental
Last change on this file since 59c05958 was 9fd9d015, checked in by Peter A. Buhr <pabuhr@…>, 16 months ago

formatting, add new waituntil grammar, rewrite waitfor grammar, simplify waitfor build-routines to match new grammar

  • Property mode set to 100644
File size: 13.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//
[4e7171f]7// StatementNode.cc -- Transform from parse data-structures to AST data-structures, usually deleting the parse
8//     data-structure after the transformation.
[b87a5ed]9//
10// Author           : Rodolfo G. Esteves
11// Created On       : Sat May 16 14:59:41 2015
[a67b60e]12// Last Modified By : Peter A. Buhr
[9fd9d015]13// Last Modified On : Wed Mar 29 08:51:23 2023
14// Update Count     : 454
[b87a5ed]15//
16
[e3e16bc]17#include <cassert>                 // for assert, strict_dynamic_cast, assertf
[d180746]18#include <list>                    // for list
19#include <memory>                  // for unique_ptr
20#include <string>                  // for string
[51b7345]21
[d180746]22#include "Common/SemanticError.h"  // for SemanticError
23#include "Common/utility.h"        // for maybeMoveBuild, maybeBuild
24#include "ParseNode.h"             // for StatementNode, ExpressionNode, bui...
25#include "SynTree/Expression.h"    // for Expression, ConstantExpr
26#include "SynTree/Label.h"         // for Label, noLabels
[6d49ea3]27#include "SynTree/Declaration.h"
[d180746]28#include "SynTree/Statement.h"     // for Statement, BranchStmt, CaseStmt
29#include "parserutility.h"         // for notZeroExpr
30
31class Declaration;
[51b7345]32
33using namespace std;
34
[b87a5ed]35
[61fc4f6]36StatementNode::StatementNode( DeclarationNode * decl ) {
[c0aa336]37        assert( decl );
[61fc4f6]38        DeclarationNode * agg = decl->extractAggregate();
[c0aa336]39        if ( agg ) {
[702e826]40                StatementNode * nextStmt = new StatementNode( new DeclStmt( maybeBuild( decl ) ) );
[c0aa336]41                set_next( nextStmt );
42                if ( decl->get_next() ) {
43                        get_next()->set_next( new StatementNode( dynamic_cast< DeclarationNode * >(decl->get_next()) ) );
44                        decl->set_next( 0 );
[1d4580a]45                } // if
46        } else {
[c0aa336]47                if ( decl->get_next() ) {
48                        set_next( new StatementNode( dynamic_cast< DeclarationNode * >( decl->get_next() ) ) );
49                        decl->set_next( 0 );
50                } // if
51                agg = decl;
[1d4580a]52        } // if
[702e826]53        stmt.reset( new DeclStmt( maybeMoveBuild( agg ) ) );
[c0aa336]54} // StatementNode::StatementNode
[1d4580a]55
[61fc4f6]56StatementNode * StatementNode::append_last_case( StatementNode * stmt ) {
57        StatementNode * prev = this;
[1d4580a]58        // find end of list and maintain previous pointer
59        for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
[61fc4f6]60                StatementNode * node = strict_dynamic_cast< StatementNode * >(curr);
[ac71a86]61                assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
[8cc5cb0]62                prev = curr;
63        } // for
[e82aa9df]64        // convert from StatementNode list to Statement list
[61fc4f6]65        StatementNode * node = dynamic_cast< StatementNode * >(prev);
[436bbe5]66        list< Statement * > stmts;
[7ecbb7e]67        buildMoveList( stmt, stmts );
[e82aa9df]68        // splice any new Statements to end of current Statements
[ac71a86]69        CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get());
[8cc5cb0]70        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
71        return this;
[401e61f]72} // StatementNode::append_last_case
[8cc5cb0]73
[61fc4f6]74Statement * build_expr( ExpressionNode * ctl ) {
[702e826]75        Expression * e = maybeMoveBuild( ctl );
[8cc5cb0]76
[401e61f]77        if ( e ) return new ExprStmt( e );
78        else return new NullStmt();
79} // build_expr
[8cc5cb0]80
[436bbe5]81Expression * build_if_control( CondCtl * ctl, list< Statement * > & init ) {
[936e9f4]82        if ( ctl->init != 0 ) {
83                buildMoveList( ctl->init, init );
84        } // if
85
[a01f7c94]86        Expression * cond = nullptr;
87        if ( ctl->condition ) {
88                // compare the provided condition against 0
[702e826]89                cond = notZeroExpr( maybeMoveBuild( ctl->condition ) );
[a01f7c94]90        } else {
91                for ( Statement * stmt : init ) {
92                        // build the && of all of the declared variables compared against 0
[e3e16bc]93                        DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt );
94                        DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl );
[a01f7c94]95                        Expression * nze = notZeroExpr( new VariableExpr( dwt ) );
96                        cond = cond ? new LogicalExpr( cond, nze, true ) : nze;
97                }
98        }
[6d49ea3]99        delete ctl;
[ee3c93d]100        return cond;
[401e61f]101} // build_if_control
[ee3c93d]102
[436bbe5]103Statement * build_if( CondCtl * ctl, StatementNode * then, StatementNode * else_ ) {
104        list< Statement * > astinit;                                            // maybe empty
105        Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
106
107        Statement * astthen, * astelse = nullptr;
108        list< Statement * > aststmt;
109        buildMoveList< Statement, StatementNode >( then, aststmt );
110        assert( aststmt.size() == 1 );
111        astthen = aststmt.front();
112
113        if ( else_ ) {
114                list< Statement * > aststmt;
115                buildMoveList< Statement, StatementNode >( else_, aststmt );
116                assert( aststmt.size() == 1 );
117                astelse = aststmt.front();
[ee3c93d]118        } // if
119
[436bbe5]120        return new IfStmt( astcond, astthen, astelse, astinit );
[401e61f]121} // build_if
[2f22cc4]122
[61fc4f6]123Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
[436bbe5]124        list< Statement * > aststmt;
125        buildMoveList< Statement, StatementNode >( stmt, aststmt );
126        if ( ! isSwitch ) {                                                                     // choose statement
127                for ( Statement * stmt : aststmt ) {
[6a276a0]128                        CaseStmt * caseStmt = strict_dynamic_cast< CaseStmt * >( stmt );
129                        if ( ! caseStmt->stmts.empty() ) {                      // code after "case" => end of case list
130                                CompoundStmt * block = strict_dynamic_cast< CompoundStmt * >( caseStmt->stmts.front() );
131                                block->kids.push_back( new BranchStmt( "", BranchStmt::Break ) );
132                        } // if
133                } // for
134        } // if
[436bbe5]135        // aststmt.size() == 0 for switch (...) {}, i.e., no declaration or statements
[702e826]136        return new SwitchStmt( maybeMoveBuild( ctl ), aststmt );
[401e61f]137} // build_switch
138
[61fc4f6]139Statement * build_case( ExpressionNode * ctl ) {
[702e826]140        return new CaseStmt( maybeMoveBuild( ctl ), {} ); // stmt starts empty and then added to
[401e61f]141} // build_case
142
[61fc4f6]143Statement * build_default() {
[6180274]144        return new CaseStmt( nullptr, {}, true );                       // stmt starts empty and then added to
[401e61f]145} // build_default
146
[3b0bc16]147Statement * build_while( CondCtl * ctl, StatementNode * stmt, StatementNode * else_ ) {
[436bbe5]148        list< Statement * > astinit;                                            // maybe empty
149        Expression * astcond = build_if_control( ctl, astinit ); // ctl deleted, cond/init set
[3b0bc16]150
[436bbe5]151        list< Statement * > aststmt;                                            // loop body, compound created if empty
[3b0bc16]152        buildMoveList< Statement, StatementNode >( stmt, aststmt );
153        assert( aststmt.size() == 1 );
154
[436bbe5]155        list< Statement * > astelse;                                            // else clause, maybe empty
[3b0bc16]156        buildMoveList< Statement, StatementNode >( else_, astelse );
157
[436bbe5]158        return new WhileDoStmt( astcond, aststmt.front(), astelse.front(), astinit, false );
[401e61f]159} // build_while
[2f22cc4]160
[3b0bc16]161Statement * build_do_while( ExpressionNode * ctl, StatementNode * stmt, StatementNode * else_ ) {
[436bbe5]162        list< Statement * > aststmt;                                            // loop body, compound created if empty
[3b0bc16]163        buildMoveList< Statement, StatementNode >( stmt, aststmt );
[436bbe5]164        assert( aststmt.size() == 1 );                                          // compound created if empty
[3b0bc16]165
[436bbe5]166        list< Statement * > astelse;                                            // else clause, maybe empty
[3b0bc16]167        buildMoveList< Statement, StatementNode >( else_, astelse );
[ee3c93d]168
[4e7171f]169        // do-while cannot have declarations in the contitional, so init is always empty
[702e826]170        return new WhileDoStmt( notZeroExpr( maybeMoveBuild( ctl ) ), aststmt.front(), astelse.front(), {}, true );
[401e61f]171} // build_do_while
[2f22cc4]172
[3b0bc16]173Statement * build_for( ForCtrl * forctl, StatementNode * stmt, StatementNode * else_ ) {
[436bbe5]174        list< Statement * > astinit;                                            // maybe empty
175        buildMoveList( forctl->init, astinit );
[2f22cc4]176
[436bbe5]177        Expression * astcond = nullptr;                                         // maybe empty
[702e826]178        astcond = notZeroExpr( maybeMoveBuild( forctl->condition ) );
[2f22cc4]179
[436bbe5]180        Expression * astincr = nullptr;                                         // maybe empty
[702e826]181        astincr = maybeMoveBuild( forctl->change );
[436bbe5]182        delete forctl;
[2f22cc4]183
[436bbe5]184        list< Statement * > aststmt;                                            // loop body, compound created if empty
[3b0bc16]185        buildMoveList< Statement, StatementNode >( stmt, aststmt );
186        assert( aststmt.size() == 1 );
187
[436bbe5]188        list< Statement * > astelse;                                            // else clause, maybe empty
[3b0bc16]189        buildMoveList< Statement, StatementNode >( else_, astelse );
190
[436bbe5]191        return new ForStmt( astinit, astcond, astincr, aststmt.front(), astelse.front() );
[401e61f]192} // build_for
[2f22cc4]193
[61fc4f6]194Statement * build_branch( BranchStmt::Type kind ) {
[ba3706f]195        Statement * ret = new BranchStmt( "", kind );
[ab57786]196        return ret;
[401e61f]197} // build_branch
198
[436bbe5]199Statement * build_branch( string * identifier, BranchStmt::Type kind ) {
[61fc4f6]200        Statement * ret = new BranchStmt( * identifier, kind );
[702e826]201        delete identifier;                                                                      // allocated by lexer
[ab57786]202        return ret;
[401e61f]203} // build_branch
204
[61fc4f6]205Statement * build_computedgoto( ExpressionNode * ctl ) {
[702e826]206        return new BranchStmt( maybeMoveBuild( ctl ), BranchStmt::Goto );
[401e61f]207} // build_computedgoto
[8cc5cb0]208
[61fc4f6]209Statement * build_return( ExpressionNode * ctl ) {
[436bbe5]210        list< Expression * > exps;
[7ecbb7e]211        buildMoveList( ctl, exps );
[ba3706f]212        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
[401e61f]213} // build_return
[daf1af8]214
[61fc4f6]215Statement * build_throw( ExpressionNode * ctl ) {
[436bbe5]216        list< Expression * > exps;
[7ecbb7e]217        buildMoveList( ctl, exps );
[436bbe5]218        assertf( exps.size() < 2, "CFA internal error: leaking memory" );
[ba3706f]219        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
[401e61f]220} // build_throw
[daf1af8]221
[61fc4f6]222Statement * build_resume( ExpressionNode * ctl ) {
[436bbe5]223        list< Expression * > exps;
[daf1af8]224        buildMoveList( ctl, exps );
[436bbe5]225        assertf( exps.size() < 2, "CFA internal error: leaking memory" );
[ba3706f]226        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
[401e61f]227} // build_resume
[daf1af8]228
[61fc4f6]229Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
[794c15b]230        (void)ctl;
231        (void)target;
232        assertf( false, "resume at (non-local throw) is not yet supported," );
[401e61f]233} // build_resume_at
[321f55d]234
[436bbe5]235Statement * build_try( StatementNode * try_, StatementNode * catch_, StatementNode * finally_ ) {
236        list< CatchStmt * > aststmt;
237        buildMoveList< CatchStmt, StatementNode >( catch_, aststmt );
[702e826]238        CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >( maybeMoveBuild( try_ ) );
239        FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >( maybeMoveBuild( finally_ ) );
[436bbe5]240        return new TryStmt( tryBlock, aststmt, finallyBlock );
[401e61f]241} // build_try
242
[61fc4f6]243Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
[436bbe5]244        list< Statement * > aststmt;
245        buildMoveList< Statement, StatementNode >( body, aststmt );
246        assert( aststmt.size() == 1 );
[702e826]247        return new CatchStmt( kind, maybeMoveBuild( decl ), maybeMoveBuild( cond ), aststmt.front() );
[401e61f]248} // build_catch
249
[61fc4f6]250Statement * build_finally( StatementNode * stmt ) {
[436bbe5]251        list< Statement * > aststmt;
252        buildMoveList< Statement, StatementNode >( stmt, aststmt );
253        assert( aststmt.size() == 1 );
254        return new FinallyStmt( dynamic_cast< CompoundStmt * >( aststmt.front() ) );
[401e61f]255} // build_finally
[1d4580a]256
[427854b]257SuspendStmt * build_suspend( StatementNode * then, SuspendStmt::Type type ) {
258        auto node = new SuspendStmt();
259
260        node->type = type;
261
[436bbe5]262        list< Statement * > stmts;
[427854b]263        buildMoveList< Statement, StatementNode >( then, stmts );
264        if(!stmts.empty()) {
265                assert( stmts.size() == 1 );
266                node->then = dynamic_cast< CompoundStmt * >( stmts.front() );
267        }
268
269        return node;
[9fd9d015]270} // build_suspend
[427854b]271
[9fd9d015]272WaitForStmt * build_waitfor( WaitForStmt * existing, ExpressionNode * when, ExpressionNode * targetExpr, StatementNode * stmt ) {
[135b431]273        WaitForStmt::Target target;
[702e826]274        target.function = maybeBuild( targetExpr );
[2065609]275
276        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
277        targetExpr->set_next( nullptr );
278        buildMoveList< Expression >( next, target.arguments );
279
[135b431]280        delete targetExpr;
281
[9fd9d015]282        existing->clauses.insert( existing->clauses.begin(), WaitForStmt::Clause{
[135b431]283                std::move( target ),
[702e826]284                maybeMoveBuild( stmt ),
285                notZeroExpr( maybeMoveBuild( when ) )
[135b431]286        });
287
[9fd9d015]288        return existing;
[401e61f]289} // build_waitfor
[135b431]290
[9fd9d015]291WaitForStmt * build_waitfor_else( WaitForStmt * existing, ExpressionNode * when, StatementNode * stmt ) {
292        existing->orelse.statement  = maybeMoveBuild( stmt );
293        existing->orelse.condition  = notZeroExpr( maybeMoveBuild( when ) );
[135b431]294
[9fd9d015]295        return existing;
296} // build_waitfor_else
[135b431]297
[9fd9d015]298WaitForStmt * build_waitfor_timeout( WaitForStmt * existing, ExpressionNode * when, ExpressionNode * timeout, StatementNode * stmt ) {
299        existing->timeout.time      = maybeMoveBuild( timeout );
300        existing->timeout.statement = maybeMoveBuild( stmt );
301        existing->timeout.condition = notZeroExpr( maybeMoveBuild( when ) );
[135b431]302
[9fd9d015]303        return existing;
[401e61f]304} // build_waitfor_timeout
[135b431]305
[e67991f]306Statement * build_with( ExpressionNode * exprs, StatementNode * stmt ) {
[436bbe5]307        list< Expression * > e;
[a378ca7]308        buildMoveList( exprs, e );
[702e826]309        Statement * s = maybeMoveBuild( stmt );
[e67991f]310        return new DeclStmt( new WithStmt( e, s ) );
[401e61f]311} // build_with
[a378ca7]312
[61fc4f6]313Statement * build_compound( StatementNode * first ) {
314        CompoundStmt * cs = new CompoundStmt();
[7ecbb7e]315        buildMoveList( first, cs->get_kids() );
[b87a5ed]316        return cs;
[401e61f]317} // build_compound
[51b7345]318
[a025ea8]319// A single statement in a control structure is always converted to a compound statement so subsequent generated code
320// can be placed within this compound statement. Otherwise, code generation has to constantly check for a single
321// statement and wrap it into a compound statement to insert additional code. Hence, all control structures have a
322// conical form for code generation.
323StatementNode * maybe_build_compound( StatementNode * first ) {
324        // Optimization: if the control-structure statement is a compound statement, do not wrap it.
325        // e.g., if (...) {...} do not wrap the existing compound statement.
326        if ( ! dynamic_cast<CompoundStmt *>( first->stmt.get() ) ) { // unique_ptr
327                CompoundStmt * cs = new CompoundStmt();
328                buildMoveList( first, cs->get_kids() );
329                return new StatementNode( cs );
330        } // if
331        return first;
332} // maybe_build_compound
333
[f135b50]334// Question
[6d539f83]335Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
[436bbe5]336        list< Expression * > out, in;
337        list< ConstantExpr * > clob;
[e82aa9df]338
[7ecbb7e]339        buildMoveList( output, out );
340        buildMoveList( input, in );
341        buildMoveList( clobber, clob );
[ba3706f]342        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
[401e61f]343} // build_asm
[7f5566b]344
[6d539f83]345Statement * build_directive( string * directive ) {
[cc32d83]346        return new DirectiveStmt( *directive );
[401e61f]347} // build_directive
[61fc4f6]348
[de52331]349Statement * build_mutex( ExpressionNode * exprs, StatementNode * stmt ) {
[436bbe5]350        list< Expression * > expList;
[de52331]351        buildMoveList( exprs, expList );
[702e826]352        Statement * body = maybeMoveBuild( stmt );
[de52331]353        return new MutexStmt( body, expList );
354} // build_mutex
355
[51b7345]356// Local Variables: //
[b87a5ed]357// tab-width: 4 //
358// mode: c++ //
359// compile-command: "make install" //
[51b7345]360// End: //
Note: See TracBrowser for help on using the repository browser.