source: src/CodeGen/CodeGenerator.cc@ 70cd431

ADT ast-experimental
Last change on this file since 70cd431 was 25b0fde, checked in by JiadaL <j82liang@…>, 3 years ago

Fix casted enum init

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