source: src/Parser/StatementNode.cc@ 8f6f47d7

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 8f6f47d7 was 837a17c, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

elminated multiple memory leaks of expressions

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