source: src/Parser/StatementNode.cc @ 4f147cc

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 4f147cc was 4f147cc, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

fixed some more memory leaks and added safe_dynamic_cast to assert.h

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