source: src/CodeGen/CodeGenerator.cc @ 9d5fb67

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 9d5fb67 was da9d79b, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Add debug codegen for KeywordCasts?, TraitInstType?, TypeofType?

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