source: src/CodeGen/CodeGenerator.cc@ 0182bfa

new-env with_gc
Last change on this file since 0182bfa was eba74ba, checked in by Aaron Moss <a3moss@…>, 7 years ago

Merge remote-tracking branch 'origin/master' into with_gc

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