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

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

Push pragma directives through the translator

  • Property mode set to 100644
File size: 11.6 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 : Mon Apr 30 09:21:16 2018
13// Update Count     : 354
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}
72
73Statement * build_expr( ExpressionNode * ctl ) {
74        Expression * e = maybeMoveBuild< Expression >( ctl );
75
76        if ( e )
77                return new ExprStmt( e );
78        else
79                return new NullStmt();
80}
81
82Statement * build_if( IfCtl * ctl, StatementNode * then_stmt, StatementNode * else_stmt ) {
83        Statement * thenb, * elseb = 0;
84        std::list< Statement * > branches;
85        buildMoveList< Statement, StatementNode >( then_stmt, branches );
86        assert( branches.size() == 1 );
87        thenb = branches.front();
88
89        if ( else_stmt ) {
90                std::list< Statement * > branches;
91                buildMoveList< Statement, StatementNode >( else_stmt, branches );
92                assert( branches.size() == 1 );
93                elseb = branches.front();
94        } // if
95
96        std::list< Statement * > init;
97        if ( ctl->init != 0 ) {
98                buildMoveList( ctl->init, init );
99        } // if
100
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
108                        DeclStmt * declStmt = strict_dynamic_cast< DeclStmt * >( stmt );
109                        DeclarationWithType * dwt = strict_dynamic_cast< DeclarationWithType * >( declStmt->decl );
110                        Expression * nze = notZeroExpr( new VariableExpr( dwt ) );
111                        cond = cond ? new LogicalExpr( cond, nze, true ) : nze;
112                }
113        }
114        delete ctl;
115        return new IfStmt( cond, thenb, elseb, init );
116}
117
118Statement * build_switch( bool isSwitch, ExpressionNode * ctl, StatementNode * stmt ) {
119        std::list< Statement * > branches;
120        buildMoveList< Statement, StatementNode >( stmt, branches );
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
130        // branches.size() == 0 for switch (...) {}, i.e., no declaration or statements
131        return new SwitchStmt( maybeMoveBuild< Expression >(ctl), branches );
132}
133Statement * build_case( ExpressionNode * ctl ) {
134        std::list< Statement * > branches;
135        return new CaseStmt( maybeMoveBuild< Expression >(ctl), branches );
136}
137Statement * build_default() {
138        std::list< Statement * > branches;
139        return new CaseStmt( nullptr, branches, true );
140}
141
142Statement * build_while( ExpressionNode * ctl, StatementNode * stmt, bool kind ) {
143        std::list< Statement * > branches;
144        buildMoveList< Statement, StatementNode >( stmt, branches );
145        assert( branches.size() == 1 );
146        return new WhileStmt( notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
147}
148
149Statement * build_for( ForCtl * forctl, StatementNode * stmt ) {
150        std::list< Statement * > branches;
151        buildMoveList< Statement, StatementNode >( stmt, branches );
152        assert( branches.size() == 1 );
153
154        std::list< Statement * > init;
155        if ( forctl->init != 0 ) {
156                buildMoveList( forctl->init, init );
157        } // if
158
159        Expression * cond = 0;
160        if ( forctl->condition != 0 )
161                cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
162
163        Expression * incr = 0;
164        if ( forctl->change != 0 )
165                incr = maybeMoveBuild< Expression >(forctl->change);
166
167        delete forctl;
168        return new ForStmt( init, cond, incr, branches.front() );
169}
170
171Statement * build_branch( BranchStmt::Type kind ) {
172        Statement * ret = new BranchStmt( "", kind );
173        return ret;
174}
175Statement * build_branch( std::string * identifier, BranchStmt::Type kind ) {
176        Statement * ret = new BranchStmt( * identifier, kind );
177        delete identifier;                                                                      // allocated by lexer
178        return ret;
179}
180Statement * build_computedgoto( ExpressionNode * ctl ) {
181        return new BranchStmt( maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
182}
183
184Statement * build_return( ExpressionNode * ctl ) {
185        std::list< Expression * > exps;
186        buildMoveList( ctl, exps );
187        return new ReturnStmt( exps.size() > 0 ? exps.back() : nullptr );
188}
189
190Statement * build_throw( ExpressionNode * ctl ) {
191        std::list< Expression * > exps;
192        buildMoveList( ctl, exps );
193        assertf( exps.size() < 2, "This means we are leaking memory");
194        return new ThrowStmt( ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
195}
196
197Statement * build_resume( ExpressionNode * ctl ) {
198        std::list< Expression * > exps;
199        buildMoveList( ctl, exps );
200        assertf( exps.size() < 2, "This means we are leaking memory");
201        return new ThrowStmt( ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
202}
203
204Statement * build_resume_at( ExpressionNode * ctl, ExpressionNode * target ) {
205        (void)ctl;
206        (void)target;
207        assertf( false, "resume at (non-local throw) is not yet supported," );
208}
209
210Statement * build_try( StatementNode * try_stmt, StatementNode * catch_stmt, StatementNode * finally_stmt ) {
211        std::list< CatchStmt * > branches;
212        buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
213        CompoundStmt * tryBlock = strict_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
214        FinallyStmt * finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
215        return new TryStmt( tryBlock, branches, finallyBlock );
216}
217Statement * build_catch( CatchStmt::Kind kind, DeclarationNode * decl, ExpressionNode * cond, StatementNode * body ) {
218        std::list< Statement * > branches;
219        buildMoveList< Statement, StatementNode >( body, branches );
220        assert( branches.size() == 1 );
221        return new CatchStmt( kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
222}
223Statement * build_finally( StatementNode * stmt ) {
224        std::list< Statement * > branches;
225        buildMoveList< Statement, StatementNode >( stmt, branches );
226        assert( branches.size() == 1 );
227        return new FinallyStmt( dynamic_cast< CompoundStmt * >( branches.front() ) );
228}
229
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 );
235
236        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
237        targetExpr->set_next( nullptr );
238        buildMoveList< Expression >( next, target.arguments );
239
240        delete targetExpr;
241
242        node->clauses.push_back( WaitForStmt::Clause{
243                target,
244                maybeMoveBuild<Statement >( stmt ),
245                notZeroExpr( maybeMoveBuild<Expression>( when ) )
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 );
254
255        ExpressionNode * next = dynamic_cast<ExpressionNode *>( targetExpr->get_next() );
256        targetExpr->set_next( nullptr );
257        buildMoveList< Expression >( next, target.arguments );
258
259        delete targetExpr;
260
261        node->clauses.insert( node->clauses.begin(), WaitForStmt::Clause{
262                std::move( target ),
263                maybeMoveBuild<Statement >( stmt ),
264                notZeroExpr( maybeMoveBuild<Expression>( when ) )
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    );
276                node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
277        }
278        else {
279                node->orelse.statement  = maybeMoveBuild<Statement >( stmt );
280                node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( when ) );
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    );
291        node->timeout.condition = notZeroExpr( maybeMoveBuild<Expression>( when ) );
292
293        node->orelse.statement  = maybeMoveBuild<Statement >( else_stmt );
294        node->orelse.condition  = notZeroExpr( maybeMoveBuild<Expression>( else_when ) );
295
296        return node;
297}
298
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
306Statement * build_compound( StatementNode * first ) {
307        CompoundStmt * cs = new CompoundStmt();
308        buildMoveList( first, cs->get_kids() );
309        return cs;
310}
311
312Statement * build_asm( bool voltile, Expression * instruction, ExpressionNode * output, ExpressionNode * input, ExpressionNode * clobber, LabelNode * gotolabels ) {
313        std::list< Expression * > out, in;
314        std::list< ConstantExpr * > clob;
315
316        buildMoveList( output, out );
317        buildMoveList( input, in );
318        buildMoveList( clobber, clob );
319        return new AsmStmt( voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
320}
321
322Statement * build_directive( string * directive ) {
323        return new DirectiveStmt( *directive );
324}
325
326// Local Variables: //
327// tab-width: 4 //
328// mode: c++ //
329// compile-command: "make install" //
330// End: //
Note: See TracBrowser for help on using the repository browser.