source: src/CodeGen/CodeGenerator.cc @ e93f1d2

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since e93f1d2 was 593370c, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Modify codgen to put struct/union attributes in the correct position

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