source: src/Parser/StatementNode.cc @ ab57786

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since ab57786 was ab57786, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more refactoring of parser code

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