source: src/CodeGen/CodeGenerator.cc@ c0bf94e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors 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 with_gc
Last change on this file since c0bf94e was 5f08961d, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add debug codegen for types in postfix comments for expressions

  • Property mode set to 100644
File size: 34.9 KB
RevLine 
[51587aa]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//
[71f4e4f]7// CodeGenerator.cc --
[51587aa]8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
[e612146c]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Sep 3 20:42:52 2017
13// Update Count : 490
[51587aa]14//
[3268a58]15#include "CodeGenerator.h"
[51587aa]16
[bf2438c]17#include <cassert> // for assert, assertf
18#include <list> // for _List_iterator, list, list<>::it...
[51b73452]19
[bf2438c]20#include "Common/UniqueName.h" // for UniqueName
21#include "Common/utility.h" // for CodeLocation, toString
22#include "GenType.h" // for genType
23#include "InitTweak/InitTweak.h" // for getPointerBase
24#include "OperatorTable.h" // for OperatorInfo, operatorLookup
25#include "Parser/LinkageSpec.h" // for Spec, Intrinsic
26#include "SynTree/Attribute.h" // for Attribute
27#include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode
28#include "SynTree/Constant.h" // for Constant
29#include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl
30#include "SynTree/Expression.h" // for Expression, UntypedExpr, Applica...
31#include "SynTree/Initializer.h" // for Initializer, ListInit, Designation
32#include "SynTree/Label.h" // for Label, operator<<
33#include "SynTree/Statement.h" // for Statement, AsmStmt, BranchStmt
34#include "SynTree/Type.h" // for Type, Type::StorageClasses, Func...
[10a7775]35
[51b73452]36using namespace std;
37
38namespace CodeGen {
[6c4ff37]39 int CodeGenerator::tabsize = 4;
[51587aa]40
[145f1fc]41 // the kinds of statements that would ideally be followed by whitespace
[2b6c1e0]42 bool wantSpacing( Statement * stmt) {
43 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
[08061589]44 dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
[2b6c1e0]45 }
46
[8688ce1]47 void CodeGenerator::extension( Expression * expr ) {
[8e9cbb2]48 if ( expr->get_extension() ) {
49 output << "__extension__ ";
50 } // if
51 } // extension
52
[8688ce1]53 void CodeGenerator::extension( Declaration * decl ) {
[8e9cbb2]54 if ( decl->get_extension() ) {
55 output << "__extension__ ";
56 } // if
57 } // extension
58
[58dd019]59 void CodeGenerator::asmName( DeclarationWithType * decl ) {
[e612146c]60 if ( ConstantExpr * asmName = dynamic_cast<ConstantExpr *>(decl->get_asmName()) ) {
[58dd019]61 output << " asm ( " << asmName->get_constant()->get_value() << " )";
62 } // if
63 } // extension
64
[888cbe4]65 CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
66 labels = &l;
67 return *this;
68 }
69
[8688ce1]70 ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) {
[888cbe4]71 std::list< Label > & labs = *printLabels.labels;
72 // l.unique(); // assumes a sorted list. Why not use set? Does order matter?
73 for ( Label & l : labs ) {
74 output << l.get_name() + ": ";
75 printLabels.cg.genAttributes( l.get_attributes() );
[8688ce1]76 } // for
[888cbe4]77 return output;
78 }
79
[d22e90f]80 /* Using updateLocation at the beginning of a node and endl
[8bafacc]81 * within a node should become the method of formating.
82 */
83 void CodeGenerator::updateLocation( CodeLocation const & to ) {
[d22e90f]84 // skip if linemarks shouldn't appear or if codelocation is unset
85 if ( !lineMarks || to.isUnset() ) return;
86
87 if ( currentLocation.followedBy( to, 0 ) ) {
[8bafacc]88 return;
89 } else if ( currentLocation.followedBy( to, 1 ) ) {
90 output << "\n" << indent;
[d48e529]91 currentLocation.first_line += 1;
[8bafacc]92 } else if ( currentLocation.followedBy( to, 2 ) ) {
93 output << "\n\n" << indent;
[d48e529]94 currentLocation.first_line += 2;
[8bafacc]95 } else {
[d48e529]96 output << "\n# " << to.first_line << " \"" << to.filename
[8bafacc]97 << "\"\n" << indent;
98 currentLocation = to;
99 }
100 output << std::flush;
101 }
[c850687]102
[8bafacc]103 void CodeGenerator::updateLocation( BaseSyntaxNode const * to ) {
104 updateLocation( to->location );
[c850687]105 }
106
[d22e90f]107 // replace endl
108 ostream & CodeGenerator::LineEnder::operator()( ostream & os ) const {
109 // if ( !cg.lineMarks ) {
110 // os << "\n" << cg.indent << std::flush;
111 // }
112 os << "\n" << std::flush;
113 cg.currentLocation.first_line++;
114 // os << "/* did endl; current loc is: " << cg.currentLocation.first_line << "*/";
115 return os;
[c850687]116 }
117
[5f08961d]118 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks, bool printExprTypes ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ), printExprTypes( printExprTypes ), endl( *this ) {}
[cda48b6]119
[486341f]120 string CodeGenerator::mangleName( DeclarationWithType * decl ) {
[35b1bf4]121 if ( pretty ) return decl->get_name();
[51587aa]122 if ( decl->get_mangleName() != "" ) {
[f326f99]123 // need to incorporate scope level in order to differentiate names for destructors
124 return decl->get_scopedMangleName();
[51587aa]125 } else {
126 return decl->get_name();
127 } // if
128 }
[94b4364]129
[44a81853]130 void CodeGenerator::genAttributes( list< Attribute * > & attributes ) {
131 if ( attributes.empty() ) return;
132 output << "__attribute__ ((";
133 for ( list< Attribute * >::iterator attr( attributes.begin() );; ) {
134 output << (*attr)->get_name();
135 if ( ! (*attr)->get_parameters().empty() ) {
136 output << "(";
137 genCommaList( (*attr)->get_parameters().begin(), (*attr)->get_parameters().end() );
138 output << ")";
139 } // if
140 if ( ++attr == attributes.end() ) break;
141 output << ","; // separator
142 } // for
143 output << ")) ";
144 } // CodeGenerator::genAttributes
[7baed7d]145
[9857e8d]146 // *** BaseSyntaxNode
[d22e90f]147 void CodeGenerator::previsit( BaseSyntaxNode * node ) {
[9857e8d]148 // turn off automatic recursion for all nodes, to allow each visitor to
149 // precisely control the order in which its children are visited.
150 visit_children = false;
[d22e90f]151 updateLocation( node );
[9857e8d]152 }
153
154 // *** BaseSyntaxNode
155 void CodeGenerator::postvisit( BaseSyntaxNode * node ) {
156 std::stringstream ss;
157 node->print( ss );
158 assertf( false, "Unhandled node reached in CodeGenerator: %s", ss.str().c_str() );
159 }
[7baed7d]160
[5f08961d]161 // *** Expression
162 void CodeGenerator::previsit( Expression * node ) {
163 previsit( (BaseSyntaxNode *)node );
164 GuardAction( [this, node](){
165 if ( printExprTypes ) {
166 output << " /* " << genType( node->result, "", pretty, genC ) << " */ ";
167 }
168 } );
169 }
170
[4810867]171 // *** Declarations
[9857e8d]172 void CodeGenerator::postvisit( FunctionDecl * functionDecl ) {
[8e9cbb2]173 extension( functionDecl );
[7baed7d]174 genAttributes( functionDecl->get_attributes() );
175
[51587aa]176 handleStorageClass( functionDecl );
[6e8bd43]177 functionDecl->get_funcSpec().print( output );
[dd020c0]178
[e39241b]179 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty, genC );
[51587aa]180
[58dd019]181 asmName( functionDecl );
182
[51587aa]183 if ( functionDecl->get_statements() ) {
[9857e8d]184 functionDecl->get_statements()->accept( *visitor );
[51587aa]185 } // if
186 }
187
[9857e8d]188 void CodeGenerator::postvisit( ObjectDecl * objectDecl ) {
[e39241b]189 if (objectDecl->get_name().empty() && genC ) {
190 // only generate an anonymous name when generating C code, otherwise it clutters the output too much
[d9c8a59]191 static UniqueName name = { "__anonymous_object" };
192 objectDecl->set_name( name.newName() );
193 }
194
[8e9cbb2]195 extension( objectDecl );
[f9cebb5]196 genAttributes( objectDecl->get_attributes() );
197
[51587aa]198 handleStorageClass( objectDecl );
[e39241b]199 output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty, genC );
[71f4e4f]200
[58dd019]201 asmName( objectDecl );
202
[51587aa]203 if ( objectDecl->get_init() ) {
[6c4ff37]204 output << " = ";
[9857e8d]205 objectDecl->get_init()->accept( *visitor );
[51587aa]206 } // if
[3778cb2]207
[51587aa]208 if ( objectDecl->get_bitfieldWidth() ) {
[6c4ff37]209 output << ":";
[9857e8d]210 objectDecl->get_bitfieldWidth()->accept( *visitor );
[51587aa]211 } // if
212 }
213
[5f642e38]214 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) {
[e39241b]215 if( ! aggDecl->get_parameters().empty() && ! genC ) {
216 // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
217 output << "forall(";
218 genCommaList( aggDecl->get_parameters().begin(), aggDecl->get_parameters().end() );
219 output << ")" << endl;
[e6cee92]220 output << indent;
[e39241b]221 }
222
[593370c]223 output << kind;
224 genAttributes( aggDecl->get_attributes() );
225 output << aggDecl->get_name();
[71f4e4f]226
[2c57025]227 if ( aggDecl->has_body() ) {
228 std::list< Declaration * > & memb = aggDecl->get_members();
[94b4364]229 output << " {" << endl;
[51587aa]230
[f7cb0bc]231 ++indent;
[3778cb2]232 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
[87e08e24]233 output << indent;
[9857e8d]234 (*i)->accept( *visitor );
[6c4ff37]235 output << ";" << endl;
[3778cb2]236 } // for
[51587aa]237
[f7cb0bc]238 --indent;
[51587aa]239
[cda48b6]240 output << indent << "}";
[17cd4eb]241 } // if
[51587aa]242 }
[17cd4eb]243
[9857e8d]244 void CodeGenerator::postvisit( StructDecl * structDecl ) {
[8e9cbb2]245 extension( structDecl );
[5f642e38]246 handleAggregate( structDecl, "struct " );
[51587aa]247 }
[17cd4eb]248
[9857e8d]249 void CodeGenerator::postvisit( UnionDecl * unionDecl ) {
[8e9cbb2]250 extension( unionDecl );
[5f642e38]251 handleAggregate( unionDecl, "union " );
[51587aa]252 }
[71f4e4f]253
[9857e8d]254 void CodeGenerator::postvisit( EnumDecl * enumDecl ) {
[8e9cbb2]255 extension( enumDecl );
[6c4ff37]256 output << "enum ";
[c0aa336]257 genAttributes( enumDecl->get_attributes() );
[51587aa]258
[a984e65]259 output << enumDecl->get_name();
[71f4e4f]260
[8e9cbb2]261 std::list< Declaration* > &memb = enumDecl->get_members();
[51587aa]262
263 if ( ! memb.empty() ) {
[cda48b6]264 output << " {" << endl;
[51587aa]265
[f7cb0bc]266 ++indent;
[51587aa]267 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
[8688ce1]268 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
[51587aa]269 assert( obj );
[87e08e24]270 output << indent << mangleName( obj );
[51587aa]271 if ( obj->get_init() ) {
[6c4ff37]272 output << " = ";
[9857e8d]273 obj->get_init()->accept( *visitor );
[51587aa]274 } // if
[6c4ff37]275 output << "," << endl;
[51587aa]276 } // for
277
[f7cb0bc]278 --indent;
[51587aa]279
[cda48b6]280 output << indent << "}";
[51587aa]281 } // if
282 }
[71f4e4f]283
[9857e8d]284 void CodeGenerator::postvisit( TraitDecl * traitDecl ) {
[a0c7dc36]285 assertf( ! genC, "TraitDecls should not reach code generation." );
[a984e65]286 extension( traitDecl );
287 handleAggregate( traitDecl, "trait " );
288 }
[71f4e4f]289
[9857e8d]290 void CodeGenerator::postvisit( TypedefDecl * typeDecl ) {
[e39241b]291 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
292 output << "typedef ";
293 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
[51587aa]294 }
[71f4e4f]295
[9857e8d]296 void CodeGenerator::postvisit( TypeDecl * typeDecl ) {
[a0c7dc36]297 assertf( ! genC, "TypeDecls should not reach code generation." );
[ab4bff5]298 output << typeDecl->genTypeString() << " " << typeDecl->name;
[f0ecf9b]299 if ( typeDecl->sized ) {
[ab4bff5]300 output << " | sized(" << typeDecl->name << ")";
[a0c7dc36]301 }
[ab4bff5]302 if ( ! typeDecl->assertions.empty() ) {
[a0c7dc36]303 output << " | { ";
[ab4bff5]304 for ( DeclarationWithType * assert : typeDecl->assertions ) {
305 assert->accept( *visitor );
306 output << "; ";
307 }
[a0c7dc36]308 output << " }";
[e39241b]309 }
[51587aa]310 }
311
[92fea32]312 void CodeGenerator::postvisit( StaticAssertDecl * assertDecl ) {
313 output << "_Static_assert(";
314 assertDecl->condition->accept( *visitor );
315 output << ", ";
316 assertDecl->message->accept( *visitor );
317 output << ")";
318 }
319
[9857e8d]320 void CodeGenerator::postvisit( Designation * designation ) {
[e4d829b]321 std::list< Expression * > designators = designation->get_designators();
[e45215c]322 if ( designators.size() == 0 ) return;
[e4d829b]323 for ( Expression * des : designators ) {
[62423350]324 if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) {
325 // if expression is a NameExpr or VariableExpr, then initializing aggregate member
326 output << ".";
[9857e8d]327 des->accept( *visitor );
[e4d829b]328 } else {
[62423350]329 // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt
330 output << "[";
[9857e8d]331 des->accept( *visitor );
[62423350]332 output << "]";
[3778cb2]333 } // if
334 } // for
[e45215c]335 output << " = ";
336 }
337
[9857e8d]338 void CodeGenerator::postvisit( SingleInit * init ) {
339 init->get_value()->accept( *visitor );
[51587aa]340 }
341
[9857e8d]342 void CodeGenerator::postvisit( ListInit * init ) {
[e4d829b]343 auto initBegin = init->begin();
344 auto initEnd = init->end();
345 auto desigBegin = init->get_designations().begin();
346 auto desigEnd = init->get_designations().end();
347
[6c4ff37]348 output << "{ ";
[e4d829b]349 for ( ; initBegin != initEnd && desigBegin != desigEnd; ) {
[9857e8d]350 (*desigBegin)->accept( *visitor );
351 (*initBegin)->accept( *visitor );
[e4d829b]352 ++initBegin, ++desigBegin;
353 if ( initBegin != initEnd ) {
354 output << ", ";
355 }
356 }
[6c4ff37]357 output << " }";
[e4d829b]358 assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() );
[51587aa]359 }
360
[d22e90f]361 void CodeGenerator::postvisit( ConstructorInit * init ){
[e39241b]362 assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
[e6cee92]363 // pseudo-output for constructor/destructor pairs
[d22e90f]364 output << "<ctorinit>{" << endl << ++indent << "ctor: ";
[9857e8d]365 maybeAccept( init->get_ctor(), *visitor );
[d22e90f]366 output << ", " << endl << indent << "dtor: ";
[9857e8d]367 maybeAccept( init->get_dtor(), *visitor );
[d22e90f]368 output << endl << --indent << "}";
[fc638d2]369 }
370
[9857e8d]371 void CodeGenerator::postvisit( Constant * constant ) {
[6c4ff37]372 output << constant->get_value() ;
[51587aa]373 }
374
[4810867]375 // *** Expressions
[9857e8d]376 void CodeGenerator::postvisit( ApplicationExpr * applicationExpr ) {
[e04ef3a]377 extension( applicationExpr );
[8688ce1]378 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
[51587aa]379 OperatorInfo opInfo;
380 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
381 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
382 switch ( opInfo.type ) {
383 case OT_INDEX:
384 assert( applicationExpr->get_args().size() == 2 );
[9857e8d]385 (*arg++)->accept( *visitor );
[6c4ff37]386 output << "[";
[9857e8d]387 (*arg)->accept( *visitor );
[6c4ff37]388 output << "]";
[51587aa]389 break;
[71f4e4f]390
[51587aa]391 case OT_CALL:
[356189a]392 // there are no intrinsic definitions of the function call operator
[51587aa]393 assert( false );
394 break;
[71f4e4f]395
[f1e012b]396 case OT_CTOR:
[c2ce2350]397 case OT_DTOR:
[356189a]398 if ( applicationExpr->get_args().size() == 1 ) {
[8e9cbb2]399 // the expression fed into a single parameter constructor or destructor may contain side
400 // effects, so must still output this expression
[64071c2]401 output << "(";
[9857e8d]402 (*arg++)->accept( *visitor );
[64071c2]403 output << ") /* " << opInfo.inputName << " */";
[356189a]404 } else if ( applicationExpr->get_args().size() == 2 ) {
[c2ce2350]405 // intrinsic two parameter constructors are essentially bitwise assignment
[356189a]406 output << "(";
[9857e8d]407 (*arg++)->accept( *visitor );
[356189a]408 output << opInfo.symbol;
[9857e8d]409 (*arg)->accept( *visitor );
[c2ce2350]410 output << ") /* " << opInfo.inputName << " */";
[356189a]411 } else {
[c2ce2350]412 // no constructors with 0 or more than 2 parameters
[356189a]413 assert( false );
[8688ce1]414 } // if
[356189a]415 break;
[f1e012b]416
[51587aa]417 case OT_PREFIX:
418 case OT_PREFIXASSIGN:
419 assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]420 output << "(";
421 output << opInfo.symbol;
[9857e8d]422 (*arg)->accept( *visitor );
[6c4ff37]423 output << ")";
[51587aa]424 break;
[71f4e4f]425
[51587aa]426 case OT_POSTFIX:
427 case OT_POSTFIXASSIGN:
428 assert( applicationExpr->get_args().size() == 1 );
[9857e8d]429 (*arg)->accept( *visitor );
[6c4ff37]430 output << opInfo.symbol;
[51587aa]431 break;
432
[f1e012b]433
[51587aa]434 case OT_INFIX:
435 case OT_INFIXASSIGN:
436 assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]437 output << "(";
[9857e8d]438 (*arg++)->accept( *visitor );
[6c4ff37]439 output << opInfo.symbol;
[9857e8d]440 (*arg)->accept( *visitor );
[6c4ff37]441 output << ")";
[51587aa]442 break;
[71f4e4f]443
[51587aa]444 case OT_CONSTANT:
[721f17a]445 case OT_LABELADDRESS:
446 // there are no intrinsic definitions of 0/1 or label addresses as functions
[51587aa]447 assert( false );
[3778cb2]448 } // switch
[17cd4eb]449 } else {
[9857e8d]450 varExpr->accept( *visitor );
[6c4ff37]451 output << "(";
[51587aa]452 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]453 output << ")";
[17cd4eb]454 } // if
[51587aa]455 } else {
[9857e8d]456 applicationExpr->get_function()->accept( *visitor );
[6c4ff37]457 output << "(";
[51587aa]458 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]459 output << ")";
[51587aa]460 } // if
461 }
[71f4e4f]462
[9857e8d]463 void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) {
[e04ef3a]464 extension( untypedExpr );
[22bc276]465 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->function ) ) {
[51587aa]466 OperatorInfo opInfo;
[22bc276]467 if ( operatorLookup( nameExpr->name, opInfo ) ) {
468 std::list< Expression* >::iterator arg = untypedExpr->args.begin();
[51587aa]469 switch ( opInfo.type ) {
470 case OT_INDEX:
[22bc276]471 assert( untypedExpr->args.size() == 2 );
[9857e8d]472 (*arg++)->accept( *visitor );
[6c4ff37]473 output << "[";
[9857e8d]474 (*arg)->accept( *visitor );
[6c4ff37]475 output << "]";
[51587aa]476 break;
[71f4e4f]477
[51587aa]478 case OT_CALL:
[f1e012b]479 assert( false );
480
[c2ce2350]481 case OT_CTOR:
482 case OT_DTOR:
[22bc276]483 if ( untypedExpr->args.size() == 1 ) {
[8e9cbb2]484 // the expression fed into a single parameter constructor or destructor may contain side
485 // effects, so must still output this expression
[64071c2]486 output << "(";
[9857e8d]487 (*arg++)->accept( *visitor );
[64071c2]488 output << ") /* " << opInfo.inputName << " */";
[c2ce2350]489 } else if ( untypedExpr->get_args().size() == 2 ) {
490 // intrinsic two parameter constructors are essentially bitwise assignment
491 output << "(";
[9857e8d]492 (*arg++)->accept( *visitor );
[c2ce2350]493 output << opInfo.symbol;
[9857e8d]494 (*arg)->accept( *visitor );
[c2ce2350]495 output << ") /* " << opInfo.inputName << " */";
496 } else {
497 // no constructors with 0 or more than 2 parameters
[4e8949f]498 assertf( ! genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." );
499 output << "(";
500 (*arg++)->accept( *visitor );
501 output << opInfo.symbol << "{ ";
[22bc276]502 genCommaList( arg, untypedExpr->args.end() );
[4e8949f]503 output << "}) /* " << opInfo.inputName << " */";
[3778cb2]504 } // if
[51587aa]505 break;
[71f4e4f]506
[51587aa]507 case OT_PREFIX:
508 case OT_PREFIXASSIGN:
[de62360d]509 case OT_LABELADDRESS:
[22bc276]510 assert( untypedExpr->args.size() == 1 );
[6c4ff37]511 output << "(";
512 output << opInfo.symbol;
[9857e8d]513 (*arg)->accept( *visitor );
[6c4ff37]514 output << ")";
[51587aa]515 break;
[71f4e4f]516
[51587aa]517 case OT_POSTFIX:
518 case OT_POSTFIXASSIGN:
[22bc276]519 assert( untypedExpr->args.size() == 1 );
[9857e8d]520 (*arg)->accept( *visitor );
[6c4ff37]521 output << opInfo.symbol;
[51587aa]522 break;
[71f4e4f]523
[51587aa]524 case OT_INFIX:
525 case OT_INFIXASSIGN:
[22bc276]526 assert( untypedExpr->args.size() == 2 );
[6c4ff37]527 output << "(";
[9857e8d]528 (*arg++)->accept( *visitor );
[6c4ff37]529 output << opInfo.symbol;
[9857e8d]530 (*arg)->accept( *visitor );
[6c4ff37]531 output << ")";
[51587aa]532 break;
[71f4e4f]533
[51587aa]534 case OT_CONSTANT:
535 // there are no intrinsic definitions of 0 or 1 as functions
536 assert( false );
[3778cb2]537 } // switch
[51587aa]538 } else {
[22bc276]539 // builtin routines
540 nameExpr->accept( *visitor );
541 output << "(";
542 genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() );
543 output << ")";
[51587aa]544 } // if
545 } else {
[22bc276]546 untypedExpr->function->accept( *visitor );
[6c4ff37]547 output << "(";
[22bc276]548 genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() );
[6c4ff37]549 output << ")";
[51587aa]550 } // if
551 }
[71f4e4f]552
[9857e8d]553 void CodeGenerator::postvisit( RangeExpr * rangeExpr ) {
[22bc276]554 rangeExpr->low->accept( *visitor );
[064e3ff]555 output << " ... ";
[22bc276]556 rangeExpr->high->accept( *visitor );
[064e3ff]557 }
558
[9857e8d]559 void CodeGenerator::postvisit( NameExpr * nameExpr ) {
[e04ef3a]560 extension( nameExpr );
[51587aa]561 OperatorInfo opInfo;
[5ea7a22]562 if ( operatorLookup( nameExpr->name, opInfo ) ) {
563 if ( opInfo.type == OT_CONSTANT ) {
564 output << opInfo.symbol;
565 } else {
566 output << opInfo.outputName;
567 }
[51587aa]568 } else {
[6c4ff37]569 output << nameExpr->get_name();
[51587aa]570 } // if
571 }
[71f4e4f]572
[9857e8d]573 void CodeGenerator::postvisit( AddressExpr * addressExpr ) {
[e04ef3a]574 extension( addressExpr );
[6c4ff37]575 output << "(&";
[9857e8d]576 addressExpr->arg->accept( *visitor );
[6c4ff37]577 output << ")";
[51587aa]578 }
579
[9857e8d]580 void CodeGenerator::postvisit( LabelAddressExpr *addressExpr ) {
[5809461]581 extension( addressExpr );
582 output << "(&&" << addressExpr->arg << ")";
583 }
584
[9857e8d]585 void CodeGenerator::postvisit( CastExpr * castExpr ) {
[e04ef3a]586 extension( castExpr );
[803deb1]587 output << "(";
[906e24d]588 if ( castExpr->get_result()->isVoid() ) {
[803deb1]589 output << "(void)" ;
[d104b02]590 } else {
591 // at least one result type of cast.
592 // Note: previously, lvalue casts were skipped. Since it's now impossible for the user to write
593 // an lvalue cast, this has been taken out.
[803deb1]594 output << "(";
[e39241b]595 output << genType( castExpr->get_result(), "", pretty, genC );
[71f4e4f]596 output << ")";
[3778cb2]597 } // if
[9857e8d]598 castExpr->get_arg()->accept( *visitor );
[803deb1]599 output << ")";
[51587aa]600 }
[71f4e4f]601
[9857e8d]602 void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) {
[a5f0529]603 assertf( ! genC, "VirtualCastExpr should not reach code generation." );
604 extension( castExpr );
605 output << "(virtual ";
[9857e8d]606 castExpr->get_arg()->accept( *visitor );
[a5f0529]607 output << ")";
608 }
609
[9857e8d]610 void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) {
[e39241b]611 assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
612 extension( memberExpr );
[9857e8d]613 memberExpr->get_aggregate()->accept( *visitor );
[5f642e38]614 output << ".";
[9857e8d]615 memberExpr->get_member()->accept( *visitor );
[51587aa]616 }
[71f4e4f]617
[9857e8d]618 void CodeGenerator::postvisit( MemberExpr * memberExpr ) {
[e04ef3a]619 extension( memberExpr );
[9857e8d]620 memberExpr->get_aggregate()->accept( *visitor );
[6c4ff37]621 output << "." << mangleName( memberExpr->get_member() );
[51587aa]622 }
[71f4e4f]623
[9857e8d]624 void CodeGenerator::postvisit( VariableExpr * variableExpr ) {
[e04ef3a]625 extension( variableExpr );
[51587aa]626 OperatorInfo opInfo;
627 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]628 output << opInfo.symbol;
[51587aa]629 } else {
[6c4ff37]630 output << mangleName( variableExpr->get_var() );
[51587aa]631 } // if
632 }
[71f4e4f]633
[9857e8d]634 void CodeGenerator::postvisit( ConstantExpr * constantExpr ) {
[51587aa]635 assert( constantExpr->get_constant() );
[e04ef3a]636 extension( constantExpr );
[9857e8d]637 constantExpr->get_constant()->accept( *visitor );
[51587aa]638 }
[71f4e4f]639
[9857e8d]640 void CodeGenerator::postvisit( SizeofExpr * sizeofExpr ) {
[e04ef3a]641 extension( sizeofExpr );
[6c4ff37]642 output << "sizeof(";
[51587aa]643 if ( sizeofExpr->get_isType() ) {
[e39241b]644 output << genType( sizeofExpr->get_type(), "", pretty, genC );
[51587aa]645 } else {
[9857e8d]646 sizeofExpr->get_expr()->accept( *visitor );
[51587aa]647 } // if
[6c4ff37]648 output << ")";
[51587aa]649 }
[47534159]650
[9857e8d]651 void CodeGenerator::postvisit( AlignofExpr * alignofExpr ) {
[47534159]652 // use GCC extension to avoid bumping std to C11
[8e9cbb2]653 extension( alignofExpr );
[47534159]654 output << "__alignof__(";
[25a054f]655 if ( alignofExpr->get_isType() ) {
[e39241b]656 output << genType( alignofExpr->get_type(), "", pretty, genC );
[47534159]657 } else {
[9857e8d]658 alignofExpr->get_expr()->accept( *visitor );
[47534159]659 } // if
660 output << ")";
661 }
[71f4e4f]662
[9857e8d]663 void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) {
[e39241b]664 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
665 output << "offsetof(";
666 output << genType( offsetofExpr->get_type(), "", pretty, genC );
667 output << ", " << offsetofExpr->get_member();
668 output << ")";
[2a4b088]669 }
670
[9857e8d]671 void CodeGenerator::postvisit( OffsetofExpr * offsetofExpr ) {
[25a054f]672 // use GCC builtin
673 output << "__builtin_offsetof(";
[e39241b]674 output << genType( offsetofExpr->get_type(), "", pretty, genC );
[e551c69]675 output << ", " << mangleName( offsetofExpr->get_member() );
[25a054f]676 output << ")";
677 }
[d63eeb0]678
[9857e8d]679 void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) {
[e39241b]680 assertf( ! genC, "OffsetPackExpr should not reach code generation." );
681 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
[afc1045]682 }
[70a06f6]683
[9857e8d]684 void CodeGenerator::postvisit( LogicalExpr * logicalExpr ) {
[e04ef3a]685 extension( logicalExpr );
[6c4ff37]686 output << "(";
[9857e8d]687 logicalExpr->get_arg1()->accept( *visitor );
[51587aa]688 if ( logicalExpr->get_isAnd() ) {
[6c4ff37]689 output << " && ";
[51587aa]690 } else {
[6c4ff37]691 output << " || ";
[51587aa]692 } // if
[9857e8d]693 logicalExpr->get_arg2()->accept( *visitor );
[6c4ff37]694 output << ")";
[51587aa]695 }
[71f4e4f]696
[9857e8d]697 void CodeGenerator::postvisit( ConditionalExpr * conditionalExpr ) {
[e04ef3a]698 extension( conditionalExpr );
[6c4ff37]699 output << "(";
[9857e8d]700 conditionalExpr->get_arg1()->accept( *visitor );
[6c4ff37]701 output << " ? ";
[9857e8d]702 conditionalExpr->get_arg2()->accept( *visitor );
[6c4ff37]703 output << " : ";
[9857e8d]704 conditionalExpr->get_arg3()->accept( *visitor );
[6c4ff37]705 output << ")";
[51587aa]706 }
[71f4e4f]707
[9857e8d]708 void CodeGenerator::postvisit( CommaExpr * commaExpr ) {
[e04ef3a]709 extension( commaExpr );
[6c4ff37]710 output << "(";
[8a6cf7e]711 if ( genC ) {
712 // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings.
713 commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) );
714 }
[9857e8d]715 commaExpr->get_arg1()->accept( *visitor );
[6c4ff37]716 output << " , ";
[9857e8d]717 commaExpr->get_arg2()->accept( *visitor );
[6c4ff37]718 output << ")";
[51587aa]719 }
[71f4e4f]720
[9857e8d]721 void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) {
[d104b02]722 assertf( ! genC, "TupleAssignExpr should not reach code generation." );
[9857e8d]723 tupleExpr->stmtExpr->accept( *visitor );
[d104b02]724 }
725
[9857e8d]726 void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) {
[e39241b]727 assertf( ! genC, "UntypedTupleExpr should not reach code generation." );
[f975c65]728 extension( tupleExpr );
[e39241b]729 output << "[";
730 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
731 output << "]";
732 }
[907eccb]733
[9857e8d]734 void CodeGenerator::postvisit( TupleExpr * tupleExpr ) {
[e39241b]735 assertf( ! genC, "TupleExpr should not reach code generation." );
[f975c65]736 extension( tupleExpr );
[e39241b]737 output << "[";
738 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
739 output << "]";
740 }
[71f4e4f]741
[9857e8d]742 void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) {
[f975c65]743 assertf( ! genC, "TupleIndexExpr should not reach code generation." );
744 extension( tupleExpr );
[9857e8d]745 tupleExpr->get_tuple()->accept( *visitor );
[f975c65]746 output << "." << tupleExpr->get_index();
747 }
748
[9857e8d]749 void CodeGenerator::postvisit( TypeExpr * typeExpr ) {
[e4d829b]750 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
751 // assertf( ! genC, "TypeExpr should not reach code generation." );
752 if ( ! genC ) {
753 output<< genType( typeExpr->get_type(), "", pretty, genC );
754 }
[e39241b]755 }
[2b6c1e0]756
[9857e8d]757 void CodeGenerator::postvisit( AsmExpr * asmExpr ) {
[7f5566b]758 if ( asmExpr->get_inout() ) {
759 output << "[ ";
[9857e8d]760 asmExpr->get_inout()->accept( *visitor );
[7f5566b]761 output << " ] ";
762 } // if
[9857e8d]763 asmExpr->get_constraint()->accept( *visitor );
[7f5566b]764 output << " ( ";
[9857e8d]765 asmExpr->get_operand()->accept( *visitor );
[7f5566b]766 output << " )";
767 }
768
[9857e8d]769 void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) {
[fbcde64]770 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
[e39241b]771 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
[9857e8d]772 compLitExpr->get_initializer()->accept( *visitor );
[3c13c03]773 }
774
[9857e8d]775 void CodeGenerator::postvisit( UniqueExpr * unqExpr ) {
[d104b02]776 assertf( ! genC, "Unique expressions should not reach code generation." );
777 output << "unq<" << unqExpr->get_id() << ">{ ";
[9857e8d]778 unqExpr->get_expr()->accept( *visitor );
[d104b02]779 output << " }";
780 }
781
[9857e8d]782 void CodeGenerator::postvisit( StmtExpr * stmtExpr ) {
[44b4114]783 std::list< Statement * > & stmts = stmtExpr->statements->kids;
[d22e90f]784 output << "({" << endl;
[f7cb0bc]785 ++indent;
[3c13c03]786 unsigned int numStmts = stmts.size();
787 unsigned int i = 0;
788 for ( Statement * stmt : stmts ) {
[058f549]789 output << indent << printLabels( stmt->get_labels() );
[3c13c03]790 if ( i+1 == numStmts ) {
791 // last statement in a statement expression needs to be handled specially -
792 // cannot cast to void, otherwise the expression statement has no value
793 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
[44b4114]794 exprStmt->expr->accept( *visitor );
[3c13c03]795 output << ";" << endl;
796 ++i;
797 break;
798 }
799 }
[9857e8d]800 stmt->accept( *visitor );
[3c13c03]801 output << endl;
802 if ( wantSpacing( stmt ) ) {
803 output << endl;
804 } // if
805 ++i;
806 }
[f7cb0bc]807 --indent;
[3c13c03]808 output << indent << "})";
[6eb8948]809 }
810
[4e8949f]811 void CodeGenerator::postvisit( ConstructorExpr * expr ) {
812 assertf( ! genC, "Unique expressions should not reach code generation." );
813 expr->callExpr->accept( *visitor );
814 }
815
[44b4114]816 void CodeGenerator::postvisit( DeletedExpr * expr ) {
817 assertf( ! genC, "Deleted expressions should not reach code generation." );
818 expr->expr->accept( *visitor );
819 }
820
[4810867]821 // *** Statements
[9857e8d]822 void CodeGenerator::postvisit( CompoundStmt * compoundStmt ) {
[51587aa]823 std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]824 output << "{" << endl;
[51587aa]825
[f7cb0bc]826 ++indent;
[51587aa]827
[7f5566b]828 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
[cda48b6]829 output << indent << printLabels( (*i)->get_labels() );
[9857e8d]830 (*i)->accept( *visitor );
[2b6c1e0]831
[6c4ff37]832 output << endl;
[2b6c1e0]833 if ( wantSpacing( *i ) ) {
834 output << endl;
[3778cb2]835 } // if
[8688ce1]836 } // for
[f7cb0bc]837 --indent;
[51587aa]838
[cda48b6]839 output << indent << "}";
[51587aa]840 }
841
[9857e8d]842 void CodeGenerator::postvisit( ExprStmt * exprStmt ) {
[6c4ff37]843 assert( exprStmt );
[262f085f]844 if ( genC ) {
845 // cast the top-level expression to void to reduce gcc warnings.
[8a6cf7e]846 exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) );
[262f085f]847 }
[9857e8d]848 exprStmt->get_expr()->accept( *visitor );
[321a2481]849 output << ";";
[51587aa]850 }
851
[9857e8d]852 void CodeGenerator::postvisit( AsmStmt * asmStmt ) {
[7f5566b]853 output << "asm ";
854 if ( asmStmt->get_voltile() ) output << "volatile ";
855 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
856 output << "( ";
[9857e8d]857 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor );
[7f5566b]858 output << " : ";
859 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
860 output << " : ";
861 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
862 output << " : ";
863 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
864 if ( ! asmStmt->get_gotolabels().empty() ) {
865 output << " : ";
866 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
867 output << *begin++;
868 if ( begin == asmStmt->get_gotolabels().end() ) break;
869 output << ", ";
870 } // for
871 } // if
872 output << " );" ;
873 }
874
[9857e8d]875 void CodeGenerator::postvisit( AsmDecl * asmDecl ) {
[e994912]876 output << "asm ";
877 AsmStmt * asmStmt = asmDecl->get_stmt();
878 output << "( ";
[9857e8d]879 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor );
[e994912]880 output << " )" ;
881 }
882
[9857e8d]883 void CodeGenerator::postvisit( IfStmt * ifStmt ) {
[7f5566b]884 output << "if ( ";
[9857e8d]885 ifStmt->get_condition()->accept( *visitor );
[7f5566b]886 output << " ) ";
[51587aa]887
[9857e8d]888 ifStmt->get_thenPart()->accept( *visitor );
[51587aa]889
890 if ( ifStmt->get_elsePart() != 0) {
[2b6c1e0]891 output << " else ";
[9857e8d]892 ifStmt->get_elsePart()->accept( *visitor );
[51587aa]893 } // if
894 }
895
[9857e8d]896 void CodeGenerator::postvisit( SwitchStmt * switchStmt ) {
[7f5566b]897 output << "switch ( " ;
[9857e8d]898 switchStmt->get_condition()->accept( *visitor );
[7f5566b]899 output << " ) ";
[71f4e4f]900
[d22e90f]901 output << "{" << endl;
[f7cb0bc]902 ++indent;
[9857e8d]903 acceptAll( switchStmt->get_statements(), *visitor );
[f7cb0bc]904 --indent;
[cda48b6]905 output << indent << "}";
[51587aa]906 }
907
[9857e8d]908 void CodeGenerator::postvisit( CaseStmt * caseStmt ) {
[8bafacc]909 updateLocation( caseStmt );
[1dcd9554]910 output << indent;
[eb3261f]911 if ( caseStmt->isDefault()) {
[2b6c1e0]912 output << "default";
[eb3261f]913 } else {
[2b6c1e0]914 output << "case ";
[9857e8d]915 caseStmt->get_condition()->accept( *visitor );
[17cd4eb]916 } // if
[d22e90f]917 output << ":" << endl;
[71f4e4f]918
[51587aa]919 std::list<Statement *> sts = caseStmt->get_statements();
920
[f7cb0bc]921 ++indent;
[51587aa]922 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
[cda48b6]923 output << indent << printLabels( (*i)->get_labels() ) ;
[9857e8d]924 (*i)->accept( *visitor );
[6c4ff37]925 output << endl;
[3778cb2]926 } // for
[f7cb0bc]927 --indent;
[51587aa]928 }
929
[9857e8d]930 void CodeGenerator::postvisit( BranchStmt * branchStmt ) {
[51587aa]931 switch ( branchStmt->get_type()) {
932 case BranchStmt::Goto:
933 if ( ! branchStmt->get_target().empty() )
[6c4ff37]934 output << "goto " << branchStmt->get_target();
[71f4e4f]935 else {
[51587aa]936 if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]937 output << "goto *";
[9857e8d]938 branchStmt->get_computedTarget()->accept( *visitor );
[51587aa]939 } // if
940 } // if
941 break;
942 case BranchStmt::Break:
[6c4ff37]943 output << "break";
[51587aa]944 break;
945 case BranchStmt::Continue:
[6c4ff37]946 output << "continue";
[51587aa]947 break;
[7c2a7b6]948 case BranchStmt::FallThrough:
949 case BranchStmt::FallThroughDefault:
950 assertf( ! genC, "fallthru should not reach code generation." );
951 output << "fallthru";
952 break;
[3778cb2]953 } // switch
[7c2a7b6]954 // print branch target for labelled break/continue/fallthru in debug mode
955 if ( ! genC && branchStmt->get_type() != BranchStmt::Goto ) {
956 if ( ! branchStmt->get_target().empty() ) {
957 output << " " << branchStmt->get_target();
958 } else if ( branchStmt->get_type() == BranchStmt::FallThrough ) {
959 output << " default";
960 }
961 }
[2b6c1e0]962 output << ";";
[51587aa]963 }
964
[9857e8d]965 void CodeGenerator::postvisit( ReturnStmt * returnStmt ) {
[6c4ff37]966 output << "return ";
[9857e8d]967 maybeAccept( returnStmt->get_expr(), *visitor );
[6c4ff37]968 output << ";";
[51587aa]969 }
970
[9857e8d]971 void CodeGenerator::postvisit( ThrowStmt * throwStmt ) {
[daf1af8]972 assertf( ! genC, "Throw statements should not reach code generation." );
973
974 output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
975 "throw" : "throwResume");
976 if (throwStmt->get_expr()) {
977 output << " ";
[9857e8d]978 throwStmt->get_expr()->accept( *visitor );
[daf1af8]979 }
980 if (throwStmt->get_target()) {
981 output << " _At ";
[9857e8d]982 throwStmt->get_target()->accept( *visitor );
[daf1af8]983 }
984 output << ";";
985 }
[e4ea10b7]986 void CodeGenerator::postvisit( CatchStmt * stmt ) {
987 assertf( ! genC, "Catch statements should not reach code generation." );
988
989 output << ((stmt->get_kind() == CatchStmt::Terminate) ?
990 "catch" : "catchResume");
991 output << "( ";
992 stmt->decl->accept( *visitor );
993 output << " ) ";
994
995 if( stmt->cond ) {
996 output << "if/when(?) (";
997 stmt->cond->accept( *visitor );
998 output << ") ";
999 }
1000 stmt->body->accept( *visitor );
1001 }
1002
1003 void CodeGenerator::postvisit( WaitForStmt * stmt ) {
1004 assertf( ! genC, "Waitfor statements should not reach code generation." );
1005
1006 bool first = true;
1007 for( auto & clause : stmt->clauses ) {
1008 if(first) { output << "or "; first = false; }
1009 if( clause.condition ) {
1010 output << "when(";
1011 stmt->timeout.condition->accept( *visitor );
1012 output << ") ";
1013 }
1014 output << "waitfor(";
1015 clause.target.function->accept( *visitor );
1016 for( Expression * expr : clause.target.arguments ) {
1017 output << ",";
1018 expr->accept( *visitor );
1019 }
1020 output << ") ";
1021 clause.statement->accept( *visitor );
1022 }
1023
1024 if( stmt->timeout.statement ) {
1025 output << "or ";
1026 if( stmt->timeout.condition ) {
1027 output << "when(";
1028 stmt->timeout.condition->accept( *visitor );
1029 output << ") ";
1030 }
1031 output << "timeout(";
1032 stmt->timeout.time->accept( *visitor );
1033 output << ") ";
1034 stmt->timeout.statement->accept( *visitor );
1035 }
1036
1037 if( stmt->orelse.statement ) {
1038 output << "or ";
1039 if( stmt->orelse.condition ) {
1040 output << "when(";
1041 stmt->orelse.condition->accept( *visitor );
1042 output << ")";
1043 }
1044 output << "else ";
1045 stmt->orelse.statement->accept( *visitor );
1046 }
1047 }
1048
[55d6e8de]1049 void CodeGenerator::postvisit( WithStmt * with ) {
1050 if ( ! genC ) {
1051 output << "with ( ";
1052 genCommaList( with->exprs.begin(), with->exprs.end() );
1053 output << " ) ";
1054 }
1055 with->stmt->accept( *visitor );
1056 }
[daf1af8]1057
[9857e8d]1058 void CodeGenerator::postvisit( WhileStmt * whileStmt ) {
[321a2481]1059 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]1060 output << "do" ;
[321a2481]1061 } else {
[6c4ff37]1062 output << "while (" ;
[9857e8d]1063 whileStmt->get_condition()->accept( *visitor );
[6c4ff37]1064 output << ")";
[51587aa]1065 } // if
[2b6c1e0]1066 output << " ";
[51587aa]1067
[2b6c1e0]1068 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[9857e8d]1069 whileStmt->get_body()->accept( *visitor );
[51587aa]1070
[cda48b6]1071 output << indent;
[51587aa]1072
1073 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]1074 output << " while (" ;
[9857e8d]1075 whileStmt->get_condition()->accept( *visitor );
[6c4ff37]1076 output << ");";
[51587aa]1077 } // if
1078 }
1079
[9857e8d]1080 void CodeGenerator::postvisit( ForStmt * forStmt ) {
[8e9cbb2]1081 // initialization is always hoisted, so don't bother doing anything with that
[145f1fc]1082 output << "for (;";
[51587aa]1083
[321a2481]1084 if ( forStmt->get_condition() != 0 ) {
[9857e8d]1085 forStmt->get_condition()->accept( *visitor );
[3778cb2]1086 } // if
[6c4ff37]1087 output << ";";
[51587aa]1088
[321a2481]1089 if ( forStmt->get_increment() != 0 ) {
1090 // cast the top-level expression to void to reduce gcc warnings.
1091 Expression * expr = new CastExpr( forStmt->get_increment() );
[9857e8d]1092 expr->accept( *visitor );
[3778cb2]1093 } // if
[2b6c1e0]1094 output << ") ";
[51587aa]1095
1096 if ( forStmt->get_body() != 0 ) {
[2b6c1e0]1097 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[9857e8d]1098 forStmt->get_body()->accept( *visitor );
[51587aa]1099 } // if
1100 }
1101
[9857e8d]1102 void CodeGenerator::postvisit( __attribute__((unused)) NullStmt * nullStmt ) {
[cda48b6]1103 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]1104 output << "/* null statement */ ;";
[51587aa]1105 }
1106
[9857e8d]1107 void CodeGenerator::postvisit( DeclStmt * declStmt ) {
1108 declStmt->get_decl()->accept( *visitor );
[71f4e4f]1109
[51587aa]1110 if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]1111 output << ";";
[51587aa]1112 } // if
1113 }
1114
[9857e8d]1115 void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) {
1116 assertf( ! genC, "ImplicitCtorDtorStmts should not reach code generation." );
1117 stmt->callStmt->accept( *visitor );
1118 }
1119
[dd020c0]1120 void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
[fb04321]1121 if ( decl->get_storageClasses().any() ) {
[6e8bd43]1122 decl->get_storageClasses().print( output );
[a7c90d4]1123 } // if
[dd020c0]1124 } // CodeGenerator::handleStorageClass
[9facf3b]1125
1126 std::string genName( DeclarationWithType * decl ) {
1127 CodeGen::OperatorInfo opInfo;
1128 if ( operatorLookup( decl->get_name(), opInfo ) ) {
1129 return opInfo.outputName;
1130 } else {
1131 return decl->get_name();
1132 } // if
1133 }
[51b73452]1134} // namespace CodeGen
[51587aa]1135
[50377a4]1136
1137unsigned Indenter::tabsize = 2;
1138
[e149f77]1139std::ostream & operator<<( std::ostream & out, const BaseSyntaxNode * node ) {
1140 if ( node ) {
1141 node->print( out );
1142 } else {
1143 out << "nullptr";
1144 }
1145 return out;
1146}
1147
[51587aa]1148// Local Variables: //
1149// tab-width: 4 //
1150// mode: c++ //
1151// compile-command: "make install" //
1152// End: //
Note: See TracBrowser for help on using the repository browser.