Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rd48e529 ra0c7dc36  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Sep  3 20:42:52 2017
    13 // Update Count     : 490
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Fri Aug 18 15:34:00 2017
     13// Update Count     : 488
    1414//
    1515#include "CodeGenerator.h"
     
    5959
    6060        void CodeGenerator::asmName( DeclarationWithType * decl ) {
    61                 if ( ConstantExpr * asmName = dynamic_cast<ConstantExpr *>(decl->get_asmName()) ) {
     61                if ( ConstantExpr * asmName = decl->get_asmName() ) {
    6262                        output << " asm ( " << asmName->get_constant()->get_value() << " )";
    6363                } // if
     
    8989                } else if ( currentLocation.followedBy( to, 1 ) ) {
    9090                        output << "\n" << indent;
    91                         currentLocation.first_line += 1;
     91                        currentLocation.linenumber += 1;
    9292                } else if ( currentLocation.followedBy( to, 2 ) ) {
    9393                        output << "\n\n" << indent;
    94                         currentLocation.first_line += 2;
    95                 } else {
    96                         output << "\n# " << to.first_line << " \"" << to.filename
     94                        currentLocation.linenumber += 2;
     95                } else {
     96                        output << "\n# " << to.linenumber << " \"" << to.filename
    9797                               << "\"\n" << indent;
    9898                        currentLocation = to;
     
    139139        } // CodeGenerator::genAttributes
    140140
    141         // *** BaseSyntaxNode
    142         void CodeGenerator::previsit( BaseSyntaxNode * ) {
    143                 // turn off automatic recursion for all nodes, to allow each visitor to
    144                 // precisely control the order in which its children are visited.
    145                 visit_children = false;
    146         }
    147 
    148         // *** BaseSyntaxNode
    149         void CodeGenerator::postvisit( BaseSyntaxNode * node ) {
    150                 std::stringstream ss;
    151                 node->print( ss );
    152                 assertf( false, "Unhandled node reached in CodeGenerator: %s", ss.str().c_str() );
    153         }
    154141
    155142        // *** Declarations
    156         void CodeGenerator::postvisit( FunctionDecl * functionDecl ) {
     143        void CodeGenerator::visit( FunctionDecl * functionDecl ) {
    157144                extension( functionDecl );
    158145                genAttributes( functionDecl->get_attributes() );
     
    165152                asmName( functionDecl );
    166153
    167                 // acceptAll( functionDecl->get_oldDecls(), *visitor );
     154                // acceptAll( functionDecl->get_oldDecls(), *this );
    168155                if ( functionDecl->get_statements() ) {
    169                         functionDecl->get_statements()->accept( *visitor );
    170                 } // if
    171         }
    172 
    173         void CodeGenerator::postvisit( ObjectDecl * objectDecl ) {
     156                        functionDecl->get_statements()->accept( *this );
     157                } // if
     158        }
     159
     160        void CodeGenerator::visit( ObjectDecl * objectDecl ) {
    174161                if (objectDecl->get_name().empty() && genC ) {
    175162                        // only generate an anonymous name when generating C code, otherwise it clutters the output too much
     
    188175                if ( objectDecl->get_init() ) {
    189176                        output << " = ";
    190                         objectDecl->get_init()->accept( *visitor );
     177                        objectDecl->get_init()->accept( *this );
    191178                } // if
    192179
    193180                if ( objectDecl->get_bitfieldWidth() ) {
    194181                        output << ":";
    195                         objectDecl->get_bitfieldWidth()->accept( *visitor );
     182                        objectDecl->get_bitfieldWidth()->accept( *this );
    196183                } // if
    197184        }
     
    218205                                updateLocation( *i );
    219206                                output << indent;
    220                                 (*i)->accept( *visitor );
     207                                (*i)->accept( *this );
    221208                                output << ";" << endl;
    222209                        } // for
     
    228215        }
    229216
    230         void CodeGenerator::postvisit( StructDecl * structDecl ) {
     217        void CodeGenerator::visit( StructDecl * structDecl ) {
    231218                extension( structDecl );
    232219                handleAggregate( structDecl, "struct " );
    233220        }
    234221
    235         void CodeGenerator::postvisit( UnionDecl * unionDecl ) {
     222        void CodeGenerator::visit( UnionDecl * unionDecl ) {
    236223                extension( unionDecl );
    237224                handleAggregate( unionDecl, "union " );
    238225        }
    239226
    240         void CodeGenerator::postvisit( EnumDecl * enumDecl ) {
     227        void CodeGenerator::visit( EnumDecl * enumDecl ) {
    241228                extension( enumDecl );
    242229                updateLocation( enumDecl );
     
    259246                                if ( obj->get_init() ) {
    260247                                        output << " = ";
    261                                         obj->get_init()->accept( *visitor );
     248                                        obj->get_init()->accept( *this );
    262249                                } // if
    263250                                output << "," << endl;
     
    270257        }
    271258
    272         void CodeGenerator::postvisit( TraitDecl * traitDecl ) {
     259        void CodeGenerator::visit( TraitDecl * traitDecl ) {
    273260                assertf( ! genC, "TraitDecls should not reach code generation." );
    274261                extension( traitDecl );
     
    276263        }
    277264
    278         void CodeGenerator::postvisit( TypedefDecl * typeDecl ) {
     265        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    279266                assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
    280267                updateLocation( typeDecl );
     
    283270        }
    284271
    285         void CodeGenerator::postvisit( TypeDecl * typeDecl ) {
     272        void CodeGenerator::visit( TypeDecl * typeDecl ) {
    286273                assertf( ! genC, "TypeDecls should not reach code generation." );
    287274                output << typeDecl->genTypeString() << " " << typeDecl->get_name();
     
    296283        }
    297284
    298         void CodeGenerator::postvisit( Designation * designation ) {
     285        void CodeGenerator::visit( Designation * designation ) {
    299286                std::list< Expression * > designators = designation->get_designators();
    300287                if ( designators.size() == 0 ) return;
     
    303290                                // if expression is a NameExpr or VariableExpr, then initializing aggregate member
    304291                                output << ".";
    305                                 des->accept( *visitor );
     292                                des->accept( *this );
    306293                        } else {
    307294                                // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt
    308295                                output << "[";
    309                                 des->accept( *visitor );
     296                                des->accept( *this );
    310297                                output << "]";
    311298                        } // if
     
    314301        }
    315302
    316         void CodeGenerator::postvisit( SingleInit * init ) {
    317                 init->get_value()->accept( *visitor );
    318         }
    319 
    320         void CodeGenerator::postvisit( ListInit * init ) {
     303        void CodeGenerator::visit( SingleInit * init ) {
     304                init->get_value()->accept( *this );
     305        }
     306
     307        void CodeGenerator::visit( ListInit * init ) {
    321308                auto initBegin = init->begin();
    322309                auto initEnd = init->end();
     
    326313                output << "{ ";
    327314                for ( ; initBegin != initEnd && desigBegin != desigEnd; ) {
    328                         (*desigBegin)->accept( *visitor );
    329                         (*initBegin)->accept( *visitor );
     315                        (*desigBegin)->accept( *this );
     316                        (*initBegin)->accept( *this );
    330317                        ++initBegin, ++desigBegin;
    331318                        if ( initBegin != initEnd ) {
     
    337324        }
    338325
    339         void CodeGenerator::postvisit( __attribute__((unused)) ConstructorInit * init ){
     326        void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){
    340327                assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
    341328                // pseudo-output for constructor/destructor pairs
    342329                output << "<ctorinit>{" << std::endl << ++indent << "ctor: ";
    343                 maybeAccept( init->get_ctor(), *visitor );
     330                maybeAccept( init->get_ctor(), *this );
    344331                output << ", " << std::endl << indent << "dtor: ";
    345                 maybeAccept( init->get_dtor(), *visitor );
     332                maybeAccept( init->get_dtor(), *this );
    346333                output << std::endl << --indent << "}";
    347334        }
    348335
    349         void CodeGenerator::postvisit( Constant * constant ) {
     336        void CodeGenerator::visit( Constant * constant ) {
    350337                output << constant->get_value() ;
    351338        }
    352339
    353340        // *** Expressions
    354         void CodeGenerator::postvisit( ApplicationExpr * applicationExpr ) {
     341        void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
    355342                extension( applicationExpr );
    356343                if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
     
    361348                                  case OT_INDEX:
    362349                                        assert( applicationExpr->get_args().size() == 2 );
    363                                         (*arg++)->accept( *visitor );
     350                                        (*arg++)->accept( *this );
    364351                                        output << "[";
    365                                         (*arg)->accept( *visitor );
     352                                        (*arg)->accept( *this );
    366353                                        output << "]";
    367354                                        break;
     
    378365                                                // effects, so must still output this expression
    379366                                                output << "(";
    380                                                 (*arg++)->accept( *visitor );
     367                                                (*arg++)->accept( *this );
    381368                                                output << ") /* " << opInfo.inputName << " */";
    382369                                        } else if ( applicationExpr->get_args().size() == 2 ) {
    383370                                                // intrinsic two parameter constructors are essentially bitwise assignment
    384371                                                output << "(";
    385                                                 (*arg++)->accept( *visitor );
     372                                                (*arg++)->accept( *this );
    386373                                                output << opInfo.symbol;
    387                                                 (*arg)->accept( *visitor );
     374                                                (*arg)->accept( *this );
    388375                                                output << ") /* " << opInfo.inputName << " */";
    389376                                        } else {
     
    398385                                        output << "(";
    399386                                        output << opInfo.symbol;
    400                                         (*arg)->accept( *visitor );
     387                                        (*arg)->accept( *this );
    401388                                        output << ")";
    402389                                        break;
     
    405392                                  case OT_POSTFIXASSIGN:
    406393                                        assert( applicationExpr->get_args().size() == 1 );
    407                                         (*arg)->accept( *visitor );
     394                                        (*arg)->accept( *this );
    408395                                        output << opInfo.symbol;
    409396                                        break;
     
    414401                                        assert( applicationExpr->get_args().size() == 2 );
    415402                                        output << "(";
    416                                         (*arg++)->accept( *visitor );
     403                                        (*arg++)->accept( *this );
    417404                                        output << opInfo.symbol;
    418                                         (*arg)->accept( *visitor );
     405                                        (*arg)->accept( *this );
    419406                                        output << ")";
    420407                                        break;
     
    426413                                } // switch
    427414                        } else {
    428                                 varExpr->accept( *visitor );
     415                                varExpr->accept( *this );
    429416                                output << "(";
    430417                                genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
     
    432419                        } // if
    433420                } else {
    434                         applicationExpr->get_function()->accept( *visitor );
     421                        applicationExpr->get_function()->accept( *this );
    435422                        output << "(";
    436423                        genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
     
    439426        }
    440427
    441         void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) {
     428        void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
    442429                extension( untypedExpr );
    443430                if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     
    448435                                  case OT_INDEX:
    449436                                        assert( untypedExpr->get_args().size() == 2 );
    450                                         (*arg++)->accept( *visitor );
     437                                        (*arg++)->accept( *this );
    451438                                        output << "[";
    452                                         (*arg)->accept( *visitor );
     439                                        (*arg)->accept( *this );
    453440                                        output << "]";
    454441                                        break;
     
    463450                                                // effects, so must still output this expression
    464451                                                output << "(";
    465                                                 (*arg++)->accept( *visitor );
     452                                                (*arg++)->accept( *this );
    466453                                                output << ") /* " << opInfo.inputName << " */";
    467454                                        } else if ( untypedExpr->get_args().size() == 2 ) {
    468455                                                // intrinsic two parameter constructors are essentially bitwise assignment
    469456                                                output << "(";
    470                                                 (*arg++)->accept( *visitor );
     457                                                (*arg++)->accept( *this );
    471458                                                output << opInfo.symbol;
    472                                                 (*arg)->accept( *visitor );
     459                                                (*arg)->accept( *this );
    473460                                                output << ") /* " << opInfo.inputName << " */";
    474461                                        } else {
    475462                                                // no constructors with 0 or more than 2 parameters
    476                                                 assertf( ! genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." );
    477                                                 output << "(";
    478                                                 (*arg++)->accept( *visitor );
    479                                                 output << opInfo.symbol << "{ ";
    480                                                 genCommaList( arg, untypedExpr->get_args().end() );
    481                                                 output << "}) /* " << opInfo.inputName << " */";
     463                                                assert( false );
    482464                                        } // if
    483465                                        break;
     
    489471                                        output << "(";
    490472                                        output << opInfo.symbol;
    491                                         (*arg)->accept( *visitor );
     473                                        (*arg)->accept( *this );
    492474                                        output << ")";
    493475                                        break;
     
    496478                                  case OT_POSTFIXASSIGN:
    497479                                        assert( untypedExpr->get_args().size() == 1 );
    498                                         (*arg)->accept( *visitor );
     480                                        (*arg)->accept( *this );
    499481                                        output << opInfo.symbol;
    500482                                        break;
     
    504486                                        assert( untypedExpr->get_args().size() == 2 );
    505487                                        output << "(";
    506                                         (*arg++)->accept( *visitor );
     488                                        (*arg++)->accept( *this );
    507489                                        output << opInfo.symbol;
    508                                         (*arg)->accept( *visitor );
     490                                        (*arg)->accept( *this );
    509491                                        output << ")";
    510492                                        break;
     
    517499                                if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
    518500                                        assert( untypedExpr->get_args().size() == 2 );
    519                                         (*untypedExpr->get_args().begin())->accept( *visitor );
     501                                        (*untypedExpr->get_args().begin())->accept( *this );
    520502                                        output << " ... ";
    521                                         (*--untypedExpr->get_args().end())->accept( *visitor );
     503                                        (*--untypedExpr->get_args().end())->accept( *this );
    522504                                } else {                                                                // builtin routines
    523                                         nameExpr->accept( *visitor );
     505                                        nameExpr->accept( *this );
    524506                                        output << "(";
    525507                                        genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
     
    528510                        } // if
    529511                } else {
    530                         untypedExpr->get_function()->accept( *visitor );
     512                        untypedExpr->get_function()->accept( *this );
    531513                        output << "(";
    532514                        genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
     
    535517        }
    536518
    537         void CodeGenerator::postvisit( RangeExpr * rangeExpr ) {
    538                 rangeExpr->get_low()->accept( *visitor );
     519        void CodeGenerator::visit( RangeExpr * rangeExpr ) {
     520                rangeExpr->get_low()->accept( *this );
    539521                output << " ... ";
    540                 rangeExpr->get_high()->accept( *visitor );
    541         }
    542 
    543         void CodeGenerator::postvisit( NameExpr * nameExpr ) {
     522                rangeExpr->get_high()->accept( *this );
     523        }
     524
     525        void CodeGenerator::visit( NameExpr * nameExpr ) {
    544526                extension( nameExpr );
    545527                OperatorInfo opInfo;
     
    552534        }
    553535
    554         void CodeGenerator::postvisit( AddressExpr * addressExpr ) {
     536        void CodeGenerator::visit( AddressExpr * addressExpr ) {
    555537                extension( addressExpr );
    556538                output << "(&";
    557                 addressExpr->arg->accept( *visitor );
     539                addressExpr->arg->accept( *this );
    558540                output << ")";
    559541        }
    560542
    561         void CodeGenerator::postvisit( LabelAddressExpr *addressExpr ) {
     543        void CodeGenerator::visit( LabelAddressExpr *addressExpr ) {
    562544                extension( addressExpr );
    563545                output << "(&&" << addressExpr->arg << ")";
    564546        }
    565547
    566         void CodeGenerator::postvisit( CastExpr * castExpr ) {
     548        void CodeGenerator::visit( CastExpr * castExpr ) {
    567549                extension( castExpr );
    568550                output << "(";
     
    577559                        output << ")";
    578560                } // if
    579                 castExpr->get_arg()->accept( *visitor );
     561                castExpr->get_arg()->accept( *this );
    580562                output << ")";
    581563        }
    582564
    583         void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) {
     565        void CodeGenerator::visit( VirtualCastExpr * castExpr ) {
    584566                assertf( ! genC, "VirtualCastExpr should not reach code generation." );
    585567                extension( castExpr );
    586568                output << "(virtual ";
    587                 castExpr->get_arg()->accept( *visitor );
     569                castExpr->get_arg()->accept( *this );
    588570                output << ")";
    589571        }
    590572
    591         void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) {
     573        void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
    592574                assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
    593575                extension( memberExpr );
    594                 memberExpr->get_aggregate()->accept( *visitor );
     576                memberExpr->get_aggregate()->accept( *this );
    595577                output << ".";
    596                 memberExpr->get_member()->accept( *visitor );
    597         }
    598 
    599         void CodeGenerator::postvisit( MemberExpr * memberExpr ) {
     578                memberExpr->get_member()->accept( *this );
     579        }
     580
     581        void CodeGenerator::visit( MemberExpr * memberExpr ) {
    600582                extension( memberExpr );
    601                 memberExpr->get_aggregate()->accept( *visitor );
     583                memberExpr->get_aggregate()->accept( *this );
    602584                output << "." << mangleName( memberExpr->get_member() );
    603585        }
    604586
    605         void CodeGenerator::postvisit( VariableExpr * variableExpr ) {
     587        void CodeGenerator::visit( VariableExpr * variableExpr ) {
    606588                extension( variableExpr );
    607589                OperatorInfo opInfo;
     
    613595        }
    614596
    615         void CodeGenerator::postvisit( ConstantExpr * constantExpr ) {
     597        void CodeGenerator::visit( ConstantExpr * constantExpr ) {
    616598                assert( constantExpr->get_constant() );
    617599                extension( constantExpr );
    618                 constantExpr->get_constant()->accept( *visitor );
    619         }
    620 
    621         void CodeGenerator::postvisit( SizeofExpr * sizeofExpr ) {
     600                constantExpr->get_constant()->accept( *this );
     601        }
     602
     603        void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
    622604                extension( sizeofExpr );
    623605                output << "sizeof(";
     
    625607                        output << genType( sizeofExpr->get_type(), "", pretty, genC );
    626608                } else {
    627                         sizeofExpr->get_expr()->accept( *visitor );
     609                        sizeofExpr->get_expr()->accept( *this );
    628610                } // if
    629611                output << ")";
    630612        }
    631613
    632         void CodeGenerator::postvisit( AlignofExpr * alignofExpr ) {
     614        void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
    633615                // use GCC extension to avoid bumping std to C11
    634616                extension( alignofExpr );
     
    637619                        output << genType( alignofExpr->get_type(), "", pretty, genC );
    638620                } else {
    639                         alignofExpr->get_expr()->accept( *visitor );
     621                        alignofExpr->get_expr()->accept( *this );
    640622                } // if
    641623                output << ")";
    642624        }
    643625
    644         void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) {
     626        void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
    645627                assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
    646628                output << "offsetof(";
     
    650632        }
    651633
    652         void CodeGenerator::postvisit( OffsetofExpr * offsetofExpr ) {
     634        void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
    653635                // use GCC builtin
    654636                output << "__builtin_offsetof(";
     
    658640        }
    659641
    660         void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) {
     642        void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
    661643                assertf( ! genC, "OffsetPackExpr should not reach code generation." );
    662644                output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
    663645        }
    664646
    665         void CodeGenerator::postvisit( LogicalExpr * logicalExpr ) {
     647        void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
    666648                extension( logicalExpr );
    667649                output << "(";
    668                 logicalExpr->get_arg1()->accept( *visitor );
     650                logicalExpr->get_arg1()->accept( *this );
    669651                if ( logicalExpr->get_isAnd() ) {
    670652                        output << " && ";
     
    672654                        output << " || ";
    673655                } // if
    674                 logicalExpr->get_arg2()->accept( *visitor );
     656                logicalExpr->get_arg2()->accept( *this );
    675657                output << ")";
    676658        }
    677659
    678         void CodeGenerator::postvisit( ConditionalExpr * conditionalExpr ) {
     660        void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
    679661                extension( conditionalExpr );
    680662                output << "(";
    681                 conditionalExpr->get_arg1()->accept( *visitor );
     663                conditionalExpr->get_arg1()->accept( *this );
    682664                output << " ? ";
    683                 conditionalExpr->get_arg2()->accept( *visitor );
     665                conditionalExpr->get_arg2()->accept( *this );
    684666                output << " : ";
    685                 conditionalExpr->get_arg3()->accept( *visitor );
     667                conditionalExpr->get_arg3()->accept( *this );
    686668                output << ")";
    687669        }
    688670
    689         void CodeGenerator::postvisit( CommaExpr * commaExpr ) {
     671        void CodeGenerator::visit( CommaExpr * commaExpr ) {
    690672                extension( commaExpr );
    691673                output << "(";
     
    694676                        commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) );
    695677                }
    696                 commaExpr->get_arg1()->accept( *visitor );
     678                commaExpr->get_arg1()->accept( *this );
    697679                output << " , ";
    698                 commaExpr->get_arg2()->accept( *visitor );
     680                commaExpr->get_arg2()->accept( *this );
    699681                output << ")";
    700682        }
    701683
    702         void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) {
     684        void CodeGenerator::visit( TupleAssignExpr * tupleExpr ) {
    703685                assertf( ! genC, "TupleAssignExpr should not reach code generation." );
    704                 tupleExpr->stmtExpr->accept( *visitor );
    705         }
    706 
    707         void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) {
     686                tupleExpr->stmtExpr->accept( *this );
     687        }
     688
     689        void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) {
    708690                assertf( ! genC, "UntypedTupleExpr should not reach code generation." );
    709691                extension( tupleExpr );
     
    713695        }
    714696
    715         void CodeGenerator::postvisit( TupleExpr * tupleExpr ) {
     697        void CodeGenerator::visit( TupleExpr * tupleExpr ) {
    716698                assertf( ! genC, "TupleExpr should not reach code generation." );
    717699                extension( tupleExpr );
     
    721703        }
    722704
    723         void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) {
     705        void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) {
    724706                assertf( ! genC, "TupleIndexExpr should not reach code generation." );
    725707                extension( tupleExpr );
    726                 tupleExpr->get_tuple()->accept( *visitor );
     708                tupleExpr->get_tuple()->accept( *this );
    727709                output << "." << tupleExpr->get_index();
    728710        }
    729711
    730         void CodeGenerator::postvisit( TypeExpr * typeExpr ) {
     712        void CodeGenerator::visit( TypeExpr * typeExpr ) {
    731713                // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
    732714                // assertf( ! genC, "TypeExpr should not reach code generation." );
     
    736718        }
    737719
    738         void CodeGenerator::postvisit( AsmExpr * asmExpr ) {
     720        void CodeGenerator::visit( AsmExpr * asmExpr ) {
    739721                if ( asmExpr->get_inout() ) {
    740722                        output << "[ ";
    741                         asmExpr->get_inout()->accept( *visitor );
     723                        asmExpr->get_inout()->accept( *this );
    742724                        output << " ] ";
    743725                } // if
    744                 asmExpr->get_constraint()->accept( *visitor );
     726                asmExpr->get_constraint()->accept( *this );
    745727                output << " ( ";
    746                 asmExpr->get_operand()->accept( *visitor );
     728                asmExpr->get_operand()->accept( *this );
    747729                output << " )";
    748730        }
    749731
    750         void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) {
     732        void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
    751733                assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
    752734                output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
    753                 compLitExpr->get_initializer()->accept( *visitor );
    754         }
    755 
    756         void CodeGenerator::postvisit( UniqueExpr * unqExpr ) {
     735                compLitExpr->get_initializer()->accept( *this );
     736        }
     737
     738        void CodeGenerator::visit( UniqueExpr * unqExpr ) {
    757739                assertf( ! genC, "Unique expressions should not reach code generation." );
    758740                output << "unq<" << unqExpr->get_id() << ">{ ";
    759                 unqExpr->get_expr()->accept( *visitor );
     741                unqExpr->get_expr()->accept( *this );
    760742                output << " }";
    761743        }
    762744
    763         void CodeGenerator::postvisit( StmtExpr * stmtExpr ) {
     745        void CodeGenerator::visit( StmtExpr * stmtExpr ) {
    764746                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
    765747                updateLocation( stmtExpr );
     
    775757                                // cannot cast to void, otherwise the expression statement has no value
    776758                                if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
    777                                         exprStmt->get_expr()->accept( *visitor );
     759                                        exprStmt->get_expr()->accept( *this );
    778760                                        output << ";" << endl;
    779761                                        ++i;
     
    781763                                }
    782764                        }
    783                         stmt->accept( *visitor );
     765                        stmt->accept( *this );
    784766                        output << endl;
    785767                        if ( wantSpacing( stmt ) ) {
     
    792774        }
    793775
    794         void CodeGenerator::postvisit( ConstructorExpr * expr ) {
    795                 assertf( ! genC, "Unique expressions should not reach code generation." );
    796                 expr->callExpr->accept( *visitor );
    797         }
    798 
    799776        // *** Statements
    800         void CodeGenerator::postvisit( CompoundStmt * compoundStmt ) {
     777        void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
    801778                std::list<Statement*> ks = compoundStmt->get_kids();
    802779                output << "{" << endl;
     
    806783                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
    807784                        output << indent << printLabels( (*i)->get_labels() );
    808                         (*i)->accept( *visitor );
     785                        (*i)->accept( *this );
    809786
    810787                        output << endl;
     
    818795        }
    819796
    820         void CodeGenerator::postvisit( ExprStmt * exprStmt ) {
     797        void CodeGenerator::visit( ExprStmt * exprStmt ) {
    821798                assert( exprStmt );
    822799                if ( genC ) {
     
    824801                        exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) );
    825802                }
    826                 exprStmt->get_expr()->accept( *visitor );
     803                exprStmt->get_expr()->accept( *this );
    827804                output << ";";
    828805        }
    829806
    830         void CodeGenerator::postvisit( AsmStmt * asmStmt ) {
     807        void CodeGenerator::visit( AsmStmt * asmStmt ) {
    831808                output << "asm ";
    832809                if ( asmStmt->get_voltile() ) output << "volatile ";
    833810                if ( ! asmStmt->get_gotolabels().empty()  ) output << "goto ";
    834811                output << "( ";
    835                 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor );
     812                if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
    836813                output << " : ";
    837814                genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
     
    851828        }
    852829
    853         void CodeGenerator::postvisit( AsmDecl * asmDecl ) {
     830        void CodeGenerator::visit( AsmDecl * asmDecl ) {
    854831                output << "asm ";
    855832                AsmStmt * asmStmt = asmDecl->get_stmt();
    856833                output << "( ";
    857                 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor );
     834                if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
    858835                output << " )" ;
    859836        }
    860837
    861         void CodeGenerator::postvisit( IfStmt * ifStmt ) {
     838        void CodeGenerator::visit( IfStmt * ifStmt ) {
    862839                updateLocation( ifStmt );
    863840                output << "if ( ";
    864                 ifStmt->get_condition()->accept( *visitor );
     841                ifStmt->get_condition()->accept( *this );
    865842                output << " ) ";
    866843
    867                 ifStmt->get_thenPart()->accept( *visitor );
     844                ifStmt->get_thenPart()->accept( *this );
    868845
    869846                if ( ifStmt->get_elsePart() != 0) {
    870847                        output << " else ";
    871                         ifStmt->get_elsePart()->accept( *visitor );
    872                 } // if
    873         }
    874 
    875         void CodeGenerator::postvisit( SwitchStmt * switchStmt ) {
     848                        ifStmt->get_elsePart()->accept( *this );
     849                } // if
     850        }
     851
     852        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
    876853                updateLocation( switchStmt );
    877854                output << "switch ( " ;
    878                 switchStmt->get_condition()->accept( *visitor );
     855                switchStmt->get_condition()->accept( *this );
    879856                output << " ) ";
    880857
    881858                output << "{" << std::endl;
    882859                ++indent;
    883                 acceptAll( switchStmt->get_statements(), *visitor );
     860                acceptAll( switchStmt->get_statements(), *this );
    884861                --indent;
    885862                output << indent << "}";
    886863        }
    887864
    888         void CodeGenerator::postvisit( CaseStmt * caseStmt ) {
     865        void CodeGenerator::visit( CaseStmt * caseStmt ) {
    889866                updateLocation( caseStmt );
    890867                if ( caseStmt->isDefault()) {
     
    892869                } else {
    893870                        output << "case ";
    894                         caseStmt->get_condition()->accept( *visitor );
     871                        caseStmt->get_condition()->accept( *this );
    895872                } // if
    896873                output << ":\n";
     
    901878                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    902879                        output << indent << printLabels( (*i)->get_labels() )  ;
    903                         (*i)->accept( *visitor );
     880                        (*i)->accept( *this );
    904881                        output << endl;
    905882                } // for
     
    907884        }
    908885
    909         void CodeGenerator::postvisit( BranchStmt * branchStmt ) {
     886        void CodeGenerator::visit( BranchStmt * branchStmt ) {
    910887                switch ( branchStmt->get_type()) {
    911888                  case BranchStmt::Goto:
     
    915892                                if ( branchStmt->get_computedTarget() != 0 ) {
    916893                                        output << "goto *";
    917                                         branchStmt->get_computedTarget()->accept( *visitor );
     894                                        branchStmt->get_computedTarget()->accept( *this );
    918895                                } // if
    919896                        } // if
     
    929906        }
    930907
    931         void CodeGenerator::postvisit( ReturnStmt * returnStmt ) {
     908        void CodeGenerator::visit( ReturnStmt * returnStmt ) {
    932909                output << "return ";
    933                 maybeAccept( returnStmt->get_expr(), *visitor );
     910                maybeAccept( returnStmt->get_expr(), *this );
    934911                output << ";";
    935912        }
    936913
    937         void CodeGenerator::postvisit( ThrowStmt * throwStmt ) {
     914        void CodeGenerator::visit( ThrowStmt * throwStmt ) {
    938915                assertf( ! genC, "Throw statements should not reach code generation." );
    939916
     
    942919                if (throwStmt->get_expr()) {
    943920                        output << " ";
    944                         throwStmt->get_expr()->accept( *visitor );
     921                        throwStmt->get_expr()->accept( *this );
    945922                }
    946923                if (throwStmt->get_target()) {
    947924                        output << " _At ";
    948                         throwStmt->get_target()->accept( *visitor );
     925                        throwStmt->get_target()->accept( *this );
    949926                }
    950927                output << ";";
    951928        }
    952929
    953         void CodeGenerator::postvisit( WhileStmt * whileStmt ) {
     930        void CodeGenerator::visit( WhileStmt * whileStmt ) {
    954931                if ( whileStmt->get_isDoWhile() ) {
    955932                        output << "do" ;
    956933                } else {
    957934                        output << "while (" ;
    958                         whileStmt->get_condition()->accept( *visitor );
     935                        whileStmt->get_condition()->accept( *this );
    959936                        output << ")";
    960937                } // if
     
    962939
    963940                output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
    964                 whileStmt->get_body()->accept( *visitor );
     941                whileStmt->get_body()->accept( *this );
    965942
    966943                output << indent;
     
    968945                if ( whileStmt->get_isDoWhile() ) {
    969946                        output << " while (" ;
    970                         whileStmt->get_condition()->accept( *visitor );
     947                        whileStmt->get_condition()->accept( *this );
    971948                        output << ");";
    972949                } // if
    973950        }
    974951
    975         void CodeGenerator::postvisit( ForStmt * forStmt ) {
     952        void CodeGenerator::visit( ForStmt * forStmt ) {
    976953                // initialization is always hoisted, so don't bother doing anything with that
    977954                output << "for (;";
    978955
    979956                if ( forStmt->get_condition() != 0 ) {
    980                         forStmt->get_condition()->accept( *visitor );
     957                        forStmt->get_condition()->accept( *this );
    981958                } // if
    982959                output << ";";
     
    985962                        // cast the top-level expression to void to reduce gcc warnings.
    986963                        Expression * expr = new CastExpr( forStmt->get_increment() );
    987                         expr->accept( *visitor );
     964                        expr->accept( *this );
    988965                } // if
    989966                output << ") ";
     
    991968                if ( forStmt->get_body() != 0 ) {
    992969                        output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
    993                         forStmt->get_body()->accept( *visitor );
    994                 } // if
    995         }
    996 
    997         void CodeGenerator::postvisit( __attribute__((unused)) NullStmt * nullStmt ) {
     970                        forStmt->get_body()->accept( *this );
     971                } // if
     972        }
     973
     974        void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) {
    998975                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    999976                output << "/* null statement */ ;";
    1000977        }
    1001978
    1002         void CodeGenerator::postvisit( DeclStmt * declStmt ) {
    1003                 declStmt->get_decl()->accept( *visitor );
     979        void CodeGenerator::visit( DeclStmt * declStmt ) {
     980                declStmt->get_decl()->accept( *this );
    1004981
    1005982                if ( doSemicolon( declStmt->get_decl() ) ) {
    1006983                        output << ";";
    1007984                } // if
    1008         }
    1009 
    1010         void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) {
    1011                 assertf( ! genC, "ImplicitCtorDtorStmts should not reach code generation." );
    1012                 stmt->callStmt->accept( *visitor );
    1013985        }
    1014986
Note: See TracChangeset for help on using the changeset viewer.