source: src/Parser/StatementNode.cc @ 6a0d1b1e

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 6a0d1b1e was 046e04a, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Now only CatchStmt? can be a handler on a TryStmt?.

  • Property mode set to 100644
File size: 7.7 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 : Andrew Beach
12// Last Modified On : Mon Jun 12 13:03:00 2017
13// Update Count     : 329
14//
15
16#include <list>
17#include <algorithm>
18#include <cassert>
19
20#include "ParseNode.h"
21#include "SynTree/Statement.h"
22#include "SynTree/Expression.h"
23#include "parseutility.h"
24#include "Common/utility.h"
25
26using namespace std;
27
28
29StatementNode::StatementNode( DeclarationNode *decl ) {
30        assert( decl );
31        DeclarationNode *agg = decl->extractAggregate();
32        if ( agg ) {
33                StatementNode *nextStmt = new StatementNode( new DeclStmt( noLabels, maybeBuild< Declaration >( decl ) ) );
34                set_next( nextStmt );
35                if ( decl->get_next() ) {
36                        get_next()->set_next( new StatementNode( dynamic_cast< DeclarationNode * >(decl->get_next()) ) );
37                        decl->set_next( 0 );
38                } // if
39        } else {
40                if ( decl->get_next() ) {
41                        set_next( new StatementNode( dynamic_cast< DeclarationNode * >( decl->get_next() ) ) );
42                        decl->set_next( 0 );
43                } // if
44                agg = decl;
45        } // if
46        stmt.reset( new DeclStmt( noLabels, maybeMoveBuild< Declaration >(agg) ) );
47} // StatementNode::StatementNode
48
49StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
50        StatementNode *prev = this;
51        // find end of list and maintain previous pointer
52        for ( StatementNode * curr = prev; curr != nullptr; curr = (StatementNode *)curr->get_next() ) {
53                StatementNode *node = safe_dynamic_cast< StatementNode * >(curr);
54                assert( dynamic_cast< CaseStmt * >(node->stmt.get()) );
55                prev = curr;
56        } // for
57        // convert from StatementNode list to Statement list
58        StatementNode *node = dynamic_cast< StatementNode * >(prev);
59        std::list< Statement * > stmts;
60        buildMoveList( stmt, stmts );
61        // splice any new Statements to end of current Statements
62        CaseStmt * caseStmt = dynamic_cast< CaseStmt * >(node->stmt.get());
63        caseStmt->get_statements().splice( caseStmt->get_statements().end(), stmts );
64        return this;
65}
66
67Statement *build_expr( ExpressionNode *ctl ) {
68        Expression *e = maybeMoveBuild< Expression >( ctl );
69
70        if ( e )
71                return new ExprStmt( noLabels, e );
72        else
73                return new NullStmt( noLabels );
74}
75
76Statement *build_if( ExpressionNode *ctl, StatementNode *then_stmt, StatementNode *else_stmt ) {
77        Statement *thenb, *elseb = 0;
78        std::list< Statement * > branches;
79        buildMoveList< Statement, StatementNode >( then_stmt, branches );
80        assert( branches.size() == 1 );
81        thenb = branches.front();
82
83        if ( else_stmt ) {
84                std::list< Statement * > branches;
85                buildMoveList< Statement, StatementNode >( else_stmt, branches );
86                assert( branches.size() == 1 );
87                elseb = branches.front();
88        } // if
89        return new IfStmt( noLabels, notZeroExpr( maybeMoveBuild< Expression >(ctl) ), thenb, elseb );
90}
91
92Statement *build_switch( ExpressionNode *ctl, StatementNode *stmt ) {
93        std::list< Statement * > branches;
94        buildMoveList< Statement, StatementNode >( stmt, branches );
95        assert( branches.size() >= 0 );                                         // size == 0 for switch (...) {}, i.e., no declaration or statements
96        return new SwitchStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches );
97}
98Statement *build_case( ExpressionNode *ctl ) {
99        std::list< Statement * > branches;
100        return new CaseStmt( noLabels, maybeMoveBuild< Expression >(ctl), branches );
101}
102Statement *build_default() {
103        std::list< Statement * > branches;
104        return new CaseStmt( noLabels, nullptr, branches, true );
105}
106
107Statement *build_while( ExpressionNode *ctl, StatementNode *stmt, bool kind ) {
108        std::list< Statement * > branches;
109        buildMoveList< Statement, StatementNode >( stmt, branches );
110        assert( branches.size() == 1 );
111        return new WhileStmt( noLabels, notZeroExpr( maybeMoveBuild< Expression >(ctl) ), branches.front(), kind );
112}
113
114Statement *build_for( ForCtl *forctl, StatementNode *stmt ) {
115        std::list< Statement * > branches;
116        buildMoveList< Statement, StatementNode >( stmt, branches );
117        assert( branches.size() == 1 );
118
119        std::list< Statement * > init;
120        if ( forctl->init != 0 ) {
121                buildMoveList( forctl->init, init );
122        } // if
123
124        Expression *cond = 0;
125        if ( forctl->condition != 0 )
126                cond = notZeroExpr( maybeMoveBuild< Expression >(forctl->condition) );
127
128        Expression *incr = 0;
129        if ( forctl->change != 0 )
130                incr = maybeMoveBuild< Expression >(forctl->change);
131
132        delete forctl;
133        return new ForStmt( noLabels, init, cond, incr, branches.front() );
134}
135
136Statement *build_branch( BranchStmt::Type kind ) {
137        Statement * ret = new BranchStmt( noLabels, "", kind );
138        return ret;
139}
140Statement *build_branch( std::string *identifier, BranchStmt::Type kind ) {
141        Statement * ret = new BranchStmt( noLabels, *identifier, kind );
142        delete identifier;                                                                      // allocated by lexer
143        return ret;
144}
145Statement *build_computedgoto( ExpressionNode *ctl ) {
146        return new BranchStmt( noLabels, maybeMoveBuild< Expression >(ctl), BranchStmt::Goto );
147}
148
149Statement *build_return( ExpressionNode *ctl ) {
150        std::list< Expression * > exps;
151        buildMoveList( ctl, exps );
152        return new ReturnStmt( noLabels, exps.size() > 0 ? exps.back() : nullptr );
153}
154
155Statement *build_throw( ExpressionNode *ctl ) {
156        std::list< Expression * > exps;
157        buildMoveList( ctl, exps );
158        assertf( exps.size() < 2, "This means we are leaking memory");
159        return new ThrowStmt( noLabels, ThrowStmt::Terminate, !exps.empty() ? exps.back() : nullptr );
160}
161
162Statement *build_resume( ExpressionNode *ctl ) {
163        std::list< Expression * > exps;
164        buildMoveList( ctl, exps );
165        assertf( exps.size() < 2, "This means we are leaking memory");
166        return new ThrowStmt( noLabels, ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
167}
168
169Statement *build_resume_at( ExpressionNode *ctl, ExpressionNode *target ) {
170        std::list< Expression * > exps;
171        buildMoveList( ctl, exps );
172        assertf( exps.size() < 2, "This means we are leaking memory");
173        return new ThrowStmt( noLabels, ThrowStmt::Resume, !exps.empty() ? exps.back() : nullptr );
174}
175
176Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
177        std::list< CatchStmt * > branches;
178        buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
179        CompoundStmt *tryBlock = safe_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
180        FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
181        return new TryStmt( noLabels, tryBlock, branches, finallyBlock );
182}
183Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
184        std::list< Statement * > branches;
185        buildMoveList< Statement, StatementNode >( body, branches );
186        assert( branches.size() == 1 );
187        return new CatchStmt( noLabels, kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
188}
189Statement *build_finally( StatementNode *stmt ) {
190        std::list< Statement * > branches;
191        buildMoveList< Statement, StatementNode >( stmt, branches );
192        assert( branches.size() == 1 );
193        return new FinallyStmt( noLabels, dynamic_cast< CompoundStmt * >( branches.front() ) );
194}
195
196Statement *build_compound( StatementNode *first ) {
197        CompoundStmt *cs = new CompoundStmt( noLabels );
198        buildMoveList( first, cs->get_kids() );
199        return cs;
200}
201
202Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
203        std::list< Expression * > out, in;
204        std::list< ConstantExpr * > clob;
205
206        buildMoveList( output, out );
207        buildMoveList( input, in );
208        buildMoveList( clobber, clob );
209        return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
210}
211
212// Local Variables: //
213// tab-width: 4 //
214// mode: c++ //
215// compile-command: "make install" //
216// End: //
Note: See TracBrowser for help on using the repository browser.