source: src/Parser/StatementNode.cc @ 7069652

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 7069652 was c0aa336, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

third attempt at gcc attributes

  • 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 : Thu Feb  2 22:16:40 2017
13// Update Count     : 327
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}
154Statement *build_throw( ExpressionNode *ctl ) {
155        std::list< Expression * > exps;
156        buildMoveList( ctl, exps );
157        assertf( exps.size() < 2, "This means we are leaking memory");
158        return new ReturnStmt( noLabels, !exps.empty() ? exps.back() : nullptr, true );
159}
160
161Statement *build_try( StatementNode *try_stmt, StatementNode *catch_stmt, StatementNode *finally_stmt ) {
162        std::list< Statement * > branches;
163        buildMoveList< Statement, StatementNode >( catch_stmt, branches );
164        CompoundStmt *tryBlock = safe_dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >(try_stmt));
165        FinallyStmt *finallyBlock = dynamic_cast< FinallyStmt * >(maybeMoveBuild< Statement >(finally_stmt) );
166        return new TryStmt( noLabels, tryBlock, branches, finallyBlock );
167}
168Statement *build_catch( DeclarationNode *decl, StatementNode *stmt, bool catchAny ) {
169        std::list< Statement * > branches;
170        buildMoveList< Statement, StatementNode >( stmt, branches );
171        assert( branches.size() == 1 );
172        return new CatchStmt( noLabels, maybeMoveBuild< Declaration >(decl), branches.front(), catchAny );
173}
174Statement *build_finally( StatementNode *stmt ) {
175        std::list< Statement * > branches;
176        buildMoveList< Statement, StatementNode >( stmt, branches );
177        assert( branches.size() == 1 );
178        return new FinallyStmt( noLabels, dynamic_cast< CompoundStmt * >( branches.front() ) );
179}
180
181Statement *build_compound( StatementNode *first ) {
182        CompoundStmt *cs = new CompoundStmt( noLabels );
183        buildMoveList( first, cs->get_kids() );
184        return cs;
185}
186
187Statement *build_asmstmt( bool voltile, ConstantExpr *instruction, ExpressionNode *output, ExpressionNode *input, ExpressionNode *clobber, LabelNode *gotolabels ) {
188        std::list< Expression * > out, in;
189        std::list< ConstantExpr * > clob;
190
191        buildMoveList( output, out );
192        buildMoveList( input, in );
193        buildMoveList( clobber, clob );
194        return new AsmStmt( noLabels, voltile, instruction, out, in, clob, gotolabels ? gotolabels->labels : noLabels );
195}
196
197// Local Variables: //
198// tab-width: 4 //
199// mode: c++ //
200// compile-command: "make install" //
201// End: //
Note: See TracBrowser for help on using the repository browser.