source: src/Parser/StatementNode.cc @ b72d4ed

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 b72d4ed was a67b60e, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

rename files and adjust includes

  • Property mode set to 100644
File size: 7.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 : Wed Jun 28 21:08:37 2017
13// Update Count     : 330
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 "parserutility.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        (void)ctl;
171        (void)target;
172        assertf( false, "resume at (non-local throw) is not yet supported," );
173}
174
175Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
176        std::list< CatchStmt * > branches;
177        buildMoveList< CatchStmt, StatementNode >( catch_stmt, branches );
178        CompoundStmt *tryBlock = safe_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
179        FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
180        return new TryStmt( noLabels, tryBlock, branches, finallyBlock );
181}
182Statement *build_catch( CatchStmt::Kind kind, DeclarationNode *decl, ExpressionNode *cond, StatementNode *body ) {
183        std::list< Statement * > branches;
184        buildMoveList< Statement, StatementNode >( body, branches );
185        assert( branches.size() == 1 );
186        return new CatchStmt( noLabels, kind, maybeMoveBuild< Declaration >(decl), maybeMoveBuild< Expression >(cond), branches.front() );
187}
188Statement *build_finally( StatementNode *stmt ) {
189        std::list< Statement * > branches;
190        buildMoveList< Statement, StatementNode >( stmt, branches );
191        assert( branches.size() == 1 );
192        return new FinallyStmt( noLabels, dynamic_cast< CompoundStmt * >( branches.front() ) );
193}
194
195Statement *build_compound( StatementNode *first ) {
196        CompoundStmt *cs = new CompoundStmt( noLabels );
197        buildMoveList( first, cs->get_kids() );
198        return cs;
199}
200
201Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
202        std::list< Expression * > out, in;
203        std::list< ConstantExpr * > clob;
204
205        buildMoveList( output, out );
206        buildMoveList( input, in );
207        buildMoveList( clobber, clob );
208        return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
209}
210
211// Local Variables: //
212// tab-width: 4 //
213// mode: c++ //
214// compile-command: "make install" //
215// End: //
Note: See TracBrowser for help on using the repository browser.