source: src/CodeGen/CodeGenerator.cc @ 10ba012

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 10ba012 was 9939dc3, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Reduced the number of object files linked into the demangler. Some of the divisions are rather odd, Lvalue2 and FixMain2, but they should be a better base to work from. Also improved the calling of the impurity detector visitors slightly.

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