source: src/Parser/StatementNode.cc@ 7527e63

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory 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 7527e63 was 7880579, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

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