source: src/Parser/StatementNode.cc @ 2794fff

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 2794fff was 145f1fc, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

modified ForStmt? to have a list of statements for the initialization portion, and reverted to hoisting the initialization

  • Property mode set to 100644
File size: 10.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 : Rob Schluntz
12// Last Modified On : Tue Jul 14 12:20:44 2015
13// Update Count     : 21
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 "utility.h"
25
26using namespace std;
27
28const char *StatementNode::StType[] = {
29        "Exp",   "If",       "Switch", "Case",    "Default",  "Choose",   "Fallthru", 
30        "While", "Do",       "For", 
31        "Goto",  "Continue", "Break",  "Return",  "Throw",
32        "Try",   "Catch",    "Finally", "Asm",
33        "Decl"
34};
35
36StatementNode::StatementNode() : ParseNode(), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
37
38StatementNode::StatementNode( const string *name_ ) : ParseNode( name_ ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {}
39
40StatementNode::StatementNode( DeclarationNode *decl ) : type( Decl ), control( 0 ), block( 0 ), labels( 0 ), target( 0 ), isCatchRest ( false ) {
41        if ( decl ) {
42                if ( DeclarationNode *agg = decl->extractAggregate() ) {
43                        this->decl = agg;
44                        StatementNode *nextStmt = new StatementNode;
45                        nextStmt->type = Decl;
46                        nextStmt->decl = decl;
47                        next = nextStmt;
48                        if ( decl->get_link() ) {
49                                next->set_next( new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) ) );
50                                decl->set_next( 0 );
51                        } // if
52                } else {
53                        if ( decl->get_link() ) {
54                                next = new StatementNode( dynamic_cast< DeclarationNode* >( decl->get_link() ) );
55                                decl->set_next( 0 );
56                        } // if
57                        this->decl = decl;
58                } // if
59        } // if
60}
61
62StatementNode::StatementNode( Type t, ExpressionNode *ctrl_label, StatementNode *block_ ) :
63                type( t ), control( ctrl_label ), block( block_), labels( 0 ), target( 0 ), decl( 0 ), isCatchRest ( false ) {
64        if ( t == Default )
65                control = 0;
66} 
67
68StatementNode::StatementNode( Type t, string *_target ) :
69                type( t ), control( 0 ), block( 0 ), labels( 0 ), target(_target ), decl( 0 ), isCatchRest ( false ) {}
70
71StatementNode::~StatementNode() {
72        delete control;
73        delete block;
74        delete labels;
75        delete target;
76        delete decl;
77}
78
79StatementNode * StatementNode::newCatchStmt( DeclarationNode *d, StatementNode *s, bool catchRestP ) {
80        StatementNode *ret = new StatementNode( StatementNode::Catch, 0, s ); 
81        ret->addDeclaration( d );
82        ret->setCatchRest( catchRestP );
83
84        return ret;
85}
86
87std::string StatementNode::get_target() const{
88        if ( target )
89                return *target;
90
91        return string("");
92}
93
94StatementNode * StatementNode::clone() const {
95        StatementNode *newnode = new StatementNode( type, maybeClone( control ), maybeClone( block ) );
96        if ( target ) {
97                newnode->target = new string( *target );
98        } else {
99                newnode->target = 0;
100        } // if
101        newnode->decl = maybeClone( decl );
102        return newnode;
103}
104
105void StatementNode::set_control( ExpressionNode *c ) {
106        control = c;
107}
108
109StatementNode * StatementNode::set_block( StatementNode *b ) {
110        block = b;
111
112        return this;
113}
114
115ExpressionNode *StatementNode::get_control() const {
116        return control;
117}
118
119StatementNode *StatementNode::get_block() const {
120        return block;
121}
122
123StatementNode::Type StatementNode::get_type() const {
124        return type;
125}
126
127StatementNode *StatementNode::add_label( const std::string *l ) {
128        if ( l != 0 ) {
129                if ( labels == 0 )
130                        labels = new std::list<std::string>();
131
132                labels->push_front(*l ); 
133                delete l;
134        } // if
135        return this;
136}
137
138std::list<std::string> *StatementNode::get_labels() const { return labels; }
139
140StatementNode *StatementNode::add_controlexp( ExpressionNode *e ) {
141        if ( control && e )
142                control->add_to_list( e ); // xxx - check this
143
144        return this;
145}
146
147StatementNode *StatementNode::append_block( StatementNode *stmt ) {
148        if ( stmt != 0 ) {
149                if ( block == 0 )
150                        block = stmt;
151                else
152                        block->set_link( stmt );
153        } // if
154        return this;
155}
156
157StatementNode *StatementNode::append_last_case( StatementNode *stmt ) {
158        if ( stmt != 0 ) {
159                StatementNode *next = ( StatementNode *)get_link();
160                if ( next && ( next->get_type() == StatementNode::Case || next->get_type() == StatementNode::Default ) )
161                        next->append_last_case ( stmt );
162                else
163                        if ( block == 0 )
164                                block = stmt;
165                        else
166                                block->set_link( stmt );
167        } // if
168        return this;
169}
170
171void StatementNode::print( std::ostream &os, int indent ) const {
172        if ( labels != 0 ) {
173                if ( ! labels->empty()) {
174                        std::list<std::string>::const_iterator i;
175
176                        os << string( indent, ' ' );
177                        for ( i = labels->begin(); i != labels->end(); i++ )
178                                os << *i << ":";
179                        os << endl;
180                } // if
181        } // if
182
183        switch ( type ) {
184          case Decl:
185                decl->print( os, indent );
186                break;
187          case Exp:
188                if ( control ) {
189                        os << string( indent, ' ' );
190                        control->print( os, indent );
191                        os << endl;
192                } else 
193                        os << string( indent, ' ' ) << "Null Statement" << endl;
194                break;
195          default:
196                os << string( indent, ' ' ) << StatementNode::StType[type] << endl;
197                if ( type == Catch ) {
198                        if ( decl ) {
199                                os << string( indent + ParseNode::indent_by, ' ' ) << "Declaration: " << endl;
200                                decl->print( os, indent + 2*ParseNode::indent_by );
201                        } else if ( isCatchRest ) {
202                                os << string( indent + ParseNode::indent_by, ' ' ) << "Catches the rest " << endl;
203                        } else {
204                                ; // should never reach here
205                        } // if
206                } // if
207                if ( control ) {
208                        os << string( indent + ParseNode::indent_by, ' ' ) << "Expression: " << endl;
209                        control->printList( os, indent + 2*ParseNode::indent_by );
210                } // if
211                if ( block ) {
212                        os << string( indent + ParseNode::indent_by, ' ' ) << "Branches of execution: " << endl;
213                        block->printList( os, indent + 2*ParseNode::indent_by ); 
214                } // if
215                if ( target ) {
216                        os << string( indent + ParseNode::indent_by, ' ' ) << "Target: " << get_target() << endl;
217                } // if
218                break;
219        } // switch
220}
221
222Statement *StatementNode::build() const {
223        std::list<Statement *> branches;
224        std::list<Expression *> exps;
225        std::list<Label> labs;
226
227        if ( labels != 0 ) {
228                std::back_insert_iterator< std::list<Label> > lab_it( labs );
229                copy( labels->begin(), labels->end(), lab_it );
230        } // if
231
232        // try {
233        buildList<Statement, StatementNode>( get_block(), branches );
234
235        switch ( type ) {
236          case Decl:
237                return new DeclStmt( labs, maybeBuild< Declaration >( decl ) );
238          case Exp:
239                {
240                        Expression *e = maybeBuild< Expression >( get_control() );
241
242                        if ( e )
243                                return new ExprStmt( labs, e );
244                        else
245                                return new NullStmt( labs );
246                }
247          case If:
248                {
249                        Statement *thenb = 0, *elseb = 0;
250                        assert( branches.size() >= 1 );
251
252                        thenb = branches.front();
253                        branches.pop_front();
254                        if ( ! branches.empty() ) {
255                                elseb = branches.front();
256                                branches.pop_front();
257                        } // if
258                        return new IfStmt( labs, notZeroExpr( get_control()->build() ), thenb, elseb );
259                }
260          case While:
261                assert( branches.size() == 1 );
262                return new WhileStmt( labs, notZeroExpr( get_control()->build() ), branches.front() );
263          case Do:
264                assert( branches.size() == 1 );
265                return new WhileStmt( labs, notZeroExpr( get_control()->build() ), branches.front(), true );
266          case For:
267                {
268                        assert( branches.size() == 1 );
269
270                        ForCtlExprNode *ctl = dynamic_cast<ForCtlExprNode *>( get_control() );
271                        assert( ctl != 0 );
272
273                        std::list<Statement *> init;
274                        if ( ctl->get_init() != 0 ) {
275                                buildList( ctl->get_init(), init );
276                        }
277
278                        Expression *cond = 0;
279                        if ( ctl->get_condition() != 0 )
280                                cond = notZeroExpr( ctl->get_condition()->build() );
281
282                        Expression *incr = 0;
283                        if ( ctl->get_change() != 0 )
284                                incr = ctl->get_change()->build();
285
286                        return new ForStmt( labs, init, cond, incr, branches.front() );
287                }
288          case Switch:
289                return new SwitchStmt( labs, get_control()->build(), branches );
290          case Choose:
291                return new ChooseStmt( labs, get_control()->build(), branches );
292          case Fallthru:
293                return new FallthruStmt( labs );
294          case Case: 
295                return new CaseStmt( labs, get_control()->build(), branches );
296          case Default:
297                return new CaseStmt( labs, 0, branches, true );
298          case Goto:
299                {
300                        if ( get_target() == "" ) {                                     // computed goto
301                                assert( get_control() != 0 );
302                                return new BranchStmt( labs, get_control()->build(), BranchStmt::Goto );
303                        } // if
304
305                        return new BranchStmt( labs, get_target(), BranchStmt::Goto );
306                }
307          case Break:
308                return new BranchStmt( labs, get_target(), BranchStmt::Break );
309          case Continue:
310                return new BranchStmt( labs, get_target(), BranchStmt::Continue );
311          case Return:
312          case Throw :
313                buildList( get_control(), exps );
314                if ( exps.size() ==0 )
315                        return new ReturnStmt( labs, 0, type == Throw );
316                if ( exps.size() > 0 )
317                        return new ReturnStmt( labs, exps.back(), type == Throw );
318          case Try:
319                {
320                        assert( branches.size() >= 0 );
321                        CompoundStmt *tryBlock = dynamic_cast<CompoundStmt *>( branches.front());
322                        branches.pop_front();
323                        FinallyStmt *finallyBlock = 0;
324                        if ( ( finallyBlock = dynamic_cast<FinallyStmt *>( branches.back())) ) {
325                                branches.pop_back();
326                        } // if
327                        return new TryStmt( labs, tryBlock, branches, finallyBlock );
328                }
329          case Catch:
330                {
331                        assert( branches.size() == 1 );
332
333                        return new CatchStmt( labs, maybeBuild< Declaration >( decl ), branches.front(), isCatchRest );
334                }
335          case Finally:
336                {
337                        assert( branches.size() == 1 );
338                        CompoundStmt *block = dynamic_cast<CompoundStmt *>( branches.front() );
339                        assert( block != 0 );
340
341                        return new FinallyStmt( labs, block );
342                }
343          default:
344                // shouldn't be here
345                return 0;
346        } // switch
347}
348
349CompoundStmtNode::CompoundStmtNode() : first( 0 ), last( 0 ) {}
350
351CompoundStmtNode::CompoundStmtNode( const string *name_ ) : StatementNode( name_ ), first( 0 ), last( 0 ) {}
352
353CompoundStmtNode::CompoundStmtNode( StatementNode *stmt ) : first( stmt ) {
354        if ( first ) {
355                last = ( StatementNode *)( stmt->get_last());
356        } else {
357                last = 0;
358        } // if
359}
360
361CompoundStmtNode::~CompoundStmtNode() {
362        delete first;
363}
364
365void CompoundStmtNode::add_statement( StatementNode *stmt ) {
366        if ( stmt != 0 ) {
367                last->set_link( stmt );
368                last = ( StatementNode *)( stmt->get_link());
369        } // if
370}
371
372void CompoundStmtNode::print( ostream &os, int indent ) const {
373        if ( first ) {
374                first->printList( os, indent+2 );
375        } // if
376}
377
378Statement *CompoundStmtNode::build() const {
379        std::list<Label> labs;
380        std::list<std::string> *labels = get_labels();
381
382        if ( labels != 0 ) {
383                std::back_insert_iterator< std::list<Label> > lab_it( labs );
384                copy( labels->begin(), labels->end(), lab_it );
385        } // if
386
387        CompoundStmt *cs = new CompoundStmt( labs );
388        buildList( first, cs->get_kids() );
389        return cs;
390}
391
392void NullStmtNode::print( ostream &os, int indent ) const {
393        os << string( indent, ' ' ) << "Null Statement:" << endl;
394}
395
396Statement *NullStmtNode::build() const { 
397        return new NullStmt;
398}
399
400// Local Variables: //
401// tab-width: 4 //
402// mode: c++ //
403// compile-command: "make install" //
404// End: //
Note: See TracBrowser for help on using the repository browser.