source: src/CodeGen/CodeGenerator.cc @ da09ba1

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since da09ba1 was da09ba1, checked in by Andrew Beach <ajbeach@…>, 5 years ago

attribute((unused)) on nameless parameters.

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