source: src/Parser/StatementNode.cc @ 1db21619

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

add CFA flag, remove -p from cc1, typedef on functions become function declarations, move isInline/isNoreturn to Declaration, cleaned up label code

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