source: src/CodeGen/CodeGenerator.cc@ ca278c1

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 resolv-new with_gc
Last change on this file since ca278c1 was 50377a4, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Refactor tree print code to use Indenter

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