source: src/CodeGen/CodeGenerator.cc@ 42b739d7

ADT ast-experimental stuck-waitfor-destruct
Last change on this file since 42b739d7 was 9749d2fa, checked in by JiadaL <j82liang@…>, 3 years ago

Fix CodeGen for function type enum

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