Changeset 9857e8d
- Timestamp:
- Sep 18, 2017, 1:25:03 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 698ec72
- Parents:
- acdfb45
- Location:
- src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
racdfb45 r9857e8d 139 139 } // CodeGenerator::genAttributes 140 140 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 } 141 154 142 155 // *** Declarations 143 void CodeGenerator:: visit( FunctionDecl * functionDecl ) {156 void CodeGenerator::postvisit( FunctionDecl * functionDecl ) { 144 157 extension( functionDecl ); 145 158 genAttributes( functionDecl->get_attributes() ); … … 152 165 asmName( functionDecl ); 153 166 154 // acceptAll( functionDecl->get_oldDecls(), * this);167 // acceptAll( functionDecl->get_oldDecls(), *visitor ); 155 168 if ( functionDecl->get_statements() ) { 156 functionDecl->get_statements()->accept( * this);157 } // if 158 } 159 160 void CodeGenerator:: visit( ObjectDecl * objectDecl ) {169 functionDecl->get_statements()->accept( *visitor ); 170 } // if 171 } 172 173 void CodeGenerator::postvisit( ObjectDecl * objectDecl ) { 161 174 if (objectDecl->get_name().empty() && genC ) { 162 175 // only generate an anonymous name when generating C code, otherwise it clutters the output too much … … 175 188 if ( objectDecl->get_init() ) { 176 189 output << " = "; 177 objectDecl->get_init()->accept( * this);190 objectDecl->get_init()->accept( *visitor ); 178 191 } // if 179 192 180 193 if ( objectDecl->get_bitfieldWidth() ) { 181 194 output << ":"; 182 objectDecl->get_bitfieldWidth()->accept( * this);195 objectDecl->get_bitfieldWidth()->accept( *visitor ); 183 196 } // if 184 197 } … … 205 218 updateLocation( *i ); 206 219 output << indent; 207 (*i)->accept( * this);220 (*i)->accept( *visitor ); 208 221 output << ";" << endl; 209 222 } // for … … 215 228 } 216 229 217 void CodeGenerator:: visit( StructDecl * structDecl ) {230 void CodeGenerator::postvisit( StructDecl * structDecl ) { 218 231 extension( structDecl ); 219 232 handleAggregate( structDecl, "struct " ); 220 233 } 221 234 222 void CodeGenerator:: visit( UnionDecl * unionDecl ) {235 void CodeGenerator::postvisit( UnionDecl * unionDecl ) { 223 236 extension( unionDecl ); 224 237 handleAggregate( unionDecl, "union " ); 225 238 } 226 239 227 void CodeGenerator:: visit( EnumDecl * enumDecl ) {240 void CodeGenerator::postvisit( EnumDecl * enumDecl ) { 228 241 extension( enumDecl ); 229 242 updateLocation( enumDecl ); … … 246 259 if ( obj->get_init() ) { 247 260 output << " = "; 248 obj->get_init()->accept( * this);261 obj->get_init()->accept( *visitor ); 249 262 } // if 250 263 output << "," << endl; … … 257 270 } 258 271 259 void CodeGenerator:: visit( TraitDecl * traitDecl ) {272 void CodeGenerator::postvisit( TraitDecl * traitDecl ) { 260 273 assertf( ! genC, "TraitDecls should not reach code generation." ); 261 274 extension( traitDecl ); … … 263 276 } 264 277 265 void CodeGenerator:: visit( TypedefDecl * typeDecl ) {278 void CodeGenerator::postvisit( TypedefDecl * typeDecl ) { 266 279 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); 267 280 updateLocation( typeDecl ); … … 270 283 } 271 284 272 void CodeGenerator:: visit( TypeDecl * typeDecl ) {285 void CodeGenerator::postvisit( TypeDecl * typeDecl ) { 273 286 assertf( ! genC, "TypeDecls should not reach code generation." ); 274 287 output << typeDecl->genTypeString() << " " << typeDecl->get_name(); … … 283 296 } 284 297 285 void CodeGenerator:: visit( Designation * designation ) {298 void CodeGenerator::postvisit( Designation * designation ) { 286 299 std::list< Expression * > designators = designation->get_designators(); 287 300 if ( designators.size() == 0 ) return; … … 290 303 // if expression is a NameExpr or VariableExpr, then initializing aggregate member 291 304 output << "."; 292 des->accept( * this);305 des->accept( *visitor ); 293 306 } else { 294 307 // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt 295 308 output << "["; 296 des->accept( * this);309 des->accept( *visitor ); 297 310 output << "]"; 298 311 } // if … … 301 314 } 302 315 303 void CodeGenerator:: visit( SingleInit * init ) {304 init->get_value()->accept( * this);305 } 306 307 void CodeGenerator:: visit( ListInit * init ) {316 void CodeGenerator::postvisit( SingleInit * init ) { 317 init->get_value()->accept( *visitor ); 318 } 319 320 void CodeGenerator::postvisit( ListInit * init ) { 308 321 auto initBegin = init->begin(); 309 322 auto initEnd = init->end(); … … 313 326 output << "{ "; 314 327 for ( ; initBegin != initEnd && desigBegin != desigEnd; ) { 315 (*desigBegin)->accept( * this);316 (*initBegin)->accept( * this);328 (*desigBegin)->accept( *visitor ); 329 (*initBegin)->accept( *visitor ); 317 330 ++initBegin, ++desigBegin; 318 331 if ( initBegin != initEnd ) { … … 324 337 } 325 338 326 void CodeGenerator:: visit( __attribute__((unused)) ConstructorInit * init ){339 void CodeGenerator::postvisit( __attribute__((unused)) ConstructorInit * init ){ 327 340 assertf( ! genC, "ConstructorInit nodes should not reach code generation." ); 328 341 // pseudo-output for constructor/destructor pairs 329 342 output << "<ctorinit>{" << std::endl << ++indent << "ctor: "; 330 maybeAccept( init->get_ctor(), * this);343 maybeAccept( init->get_ctor(), *visitor ); 331 344 output << ", " << std::endl << indent << "dtor: "; 332 maybeAccept( init->get_dtor(), * this);345 maybeAccept( init->get_dtor(), *visitor ); 333 346 output << std::endl << --indent << "}"; 334 347 } 335 348 336 void CodeGenerator:: visit( Constant * constant ) {349 void CodeGenerator::postvisit( Constant * constant ) { 337 350 output << constant->get_value() ; 338 351 } 339 352 340 353 // *** Expressions 341 void CodeGenerator:: visit( ApplicationExpr * applicationExpr ) {354 void CodeGenerator::postvisit( ApplicationExpr * applicationExpr ) { 342 355 extension( applicationExpr ); 343 356 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { … … 348 361 case OT_INDEX: 349 362 assert( applicationExpr->get_args().size() == 2 ); 350 (*arg++)->accept( * this);363 (*arg++)->accept( *visitor ); 351 364 output << "["; 352 (*arg)->accept( * this);365 (*arg)->accept( *visitor ); 353 366 output << "]"; 354 367 break; … … 365 378 // effects, so must still output this expression 366 379 output << "("; 367 (*arg++)->accept( * this);380 (*arg++)->accept( *visitor ); 368 381 output << ") /* " << opInfo.inputName << " */"; 369 382 } else if ( applicationExpr->get_args().size() == 2 ) { 370 383 // intrinsic two parameter constructors are essentially bitwise assignment 371 384 output << "("; 372 (*arg++)->accept( * this);385 (*arg++)->accept( *visitor ); 373 386 output << opInfo.symbol; 374 (*arg)->accept( * this);387 (*arg)->accept( *visitor ); 375 388 output << ") /* " << opInfo.inputName << " */"; 376 389 } else { … … 385 398 output << "("; 386 399 output << opInfo.symbol; 387 (*arg)->accept( * this);400 (*arg)->accept( *visitor ); 388 401 output << ")"; 389 402 break; … … 392 405 case OT_POSTFIXASSIGN: 393 406 assert( applicationExpr->get_args().size() == 1 ); 394 (*arg)->accept( * this);407 (*arg)->accept( *visitor ); 395 408 output << opInfo.symbol; 396 409 break; … … 401 414 assert( applicationExpr->get_args().size() == 2 ); 402 415 output << "("; 403 (*arg++)->accept( * this);416 (*arg++)->accept( *visitor ); 404 417 output << opInfo.symbol; 405 (*arg)->accept( * this);418 (*arg)->accept( *visitor ); 406 419 output << ")"; 407 420 break; … … 413 426 } // switch 414 427 } else { 415 varExpr->accept( * this);428 varExpr->accept( *visitor ); 416 429 output << "("; 417 430 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); … … 419 432 } // if 420 433 } else { 421 applicationExpr->get_function()->accept( * this);434 applicationExpr->get_function()->accept( *visitor ); 422 435 output << "("; 423 436 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); … … 426 439 } 427 440 428 void CodeGenerator:: visit( UntypedExpr * untypedExpr ) {441 void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) { 429 442 extension( untypedExpr ); 430 443 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { … … 435 448 case OT_INDEX: 436 449 assert( untypedExpr->get_args().size() == 2 ); 437 (*arg++)->accept( * this);450 (*arg++)->accept( *visitor ); 438 451 output << "["; 439 (*arg)->accept( * this);452 (*arg)->accept( *visitor ); 440 453 output << "]"; 441 454 break; … … 450 463 // effects, so must still output this expression 451 464 output << "("; 452 (*arg++)->accept( * this);465 (*arg++)->accept( *visitor ); 453 466 output << ") /* " << opInfo.inputName << " */"; 454 467 } else if ( untypedExpr->get_args().size() == 2 ) { 455 468 // intrinsic two parameter constructors are essentially bitwise assignment 456 469 output << "("; 457 (*arg++)->accept( * this);470 (*arg++)->accept( *visitor ); 458 471 output << opInfo.symbol; 459 (*arg)->accept( * this);472 (*arg)->accept( *visitor ); 460 473 output << ") /* " << opInfo.inputName << " */"; 461 474 } else { … … 471 484 output << "("; 472 485 output << opInfo.symbol; 473 (*arg)->accept( * this);486 (*arg)->accept( *visitor ); 474 487 output << ")"; 475 488 break; … … 478 491 case OT_POSTFIXASSIGN: 479 492 assert( untypedExpr->get_args().size() == 1 ); 480 (*arg)->accept( * this);493 (*arg)->accept( *visitor ); 481 494 output << opInfo.symbol; 482 495 break; … … 486 499 assert( untypedExpr->get_args().size() == 2 ); 487 500 output << "("; 488 (*arg++)->accept( * this);501 (*arg++)->accept( *visitor ); 489 502 output << opInfo.symbol; 490 (*arg)->accept( * this);503 (*arg)->accept( *visitor ); 491 504 output << ")"; 492 505 break; … … 499 512 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2 500 513 assert( untypedExpr->get_args().size() == 2 ); 501 (*untypedExpr->get_args().begin())->accept( * this);514 (*untypedExpr->get_args().begin())->accept( *visitor ); 502 515 output << " ... "; 503 (*--untypedExpr->get_args().end())->accept( * this);516 (*--untypedExpr->get_args().end())->accept( *visitor ); 504 517 } else { // builtin routines 505 nameExpr->accept( * this);518 nameExpr->accept( *visitor ); 506 519 output << "("; 507 520 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); … … 510 523 } // if 511 524 } else { 512 untypedExpr->get_function()->accept( * this);525 untypedExpr->get_function()->accept( *visitor ); 513 526 output << "("; 514 527 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); … … 517 530 } 518 531 519 void CodeGenerator:: visit( RangeExpr * rangeExpr ) {520 rangeExpr->get_low()->accept( * this);532 void CodeGenerator::postvisit( RangeExpr * rangeExpr ) { 533 rangeExpr->get_low()->accept( *visitor ); 521 534 output << " ... "; 522 rangeExpr->get_high()->accept( * this);523 } 524 525 void CodeGenerator:: visit( NameExpr * nameExpr ) {535 rangeExpr->get_high()->accept( *visitor ); 536 } 537 538 void CodeGenerator::postvisit( NameExpr * nameExpr ) { 526 539 extension( nameExpr ); 527 540 OperatorInfo opInfo; … … 534 547 } 535 548 536 void CodeGenerator:: visit( AddressExpr * addressExpr ) {549 void CodeGenerator::postvisit( AddressExpr * addressExpr ) { 537 550 extension( addressExpr ); 538 551 output << "(&"; 539 addressExpr->arg->accept( * this);552 addressExpr->arg->accept( *visitor ); 540 553 output << ")"; 541 554 } 542 555 543 void CodeGenerator:: visit( LabelAddressExpr *addressExpr ) {556 void CodeGenerator::postvisit( LabelAddressExpr *addressExpr ) { 544 557 extension( addressExpr ); 545 558 output << "(&&" << addressExpr->arg << ")"; 546 559 } 547 560 548 void CodeGenerator:: visit( CastExpr * castExpr ) {561 void CodeGenerator::postvisit( CastExpr * castExpr ) { 549 562 extension( castExpr ); 550 563 output << "("; … … 559 572 output << ")"; 560 573 } // if 561 castExpr->get_arg()->accept( * this);574 castExpr->get_arg()->accept( *visitor ); 562 575 output << ")"; 563 576 } 564 577 565 void CodeGenerator:: visit( VirtualCastExpr * castExpr ) {578 void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) { 566 579 assertf( ! genC, "VirtualCastExpr should not reach code generation." ); 567 580 extension( castExpr ); 568 581 output << "(virtual "; 569 castExpr->get_arg()->accept( * this);582 castExpr->get_arg()->accept( *visitor ); 570 583 output << ")"; 571 584 } 572 585 573 void CodeGenerator:: visit( UntypedMemberExpr * memberExpr ) {586 void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) { 574 587 assertf( ! genC, "UntypedMemberExpr should not reach code generation." ); 575 588 extension( memberExpr ); 576 memberExpr->get_aggregate()->accept( * this);589 memberExpr->get_aggregate()->accept( *visitor ); 577 590 output << "."; 578 memberExpr->get_member()->accept( * this);579 } 580 581 void CodeGenerator:: visit( MemberExpr * memberExpr ) {591 memberExpr->get_member()->accept( *visitor ); 592 } 593 594 void CodeGenerator::postvisit( MemberExpr * memberExpr ) { 582 595 extension( memberExpr ); 583 memberExpr->get_aggregate()->accept( * this);596 memberExpr->get_aggregate()->accept( *visitor ); 584 597 output << "." << mangleName( memberExpr->get_member() ); 585 598 } 586 599 587 void CodeGenerator:: visit( VariableExpr * variableExpr ) {600 void CodeGenerator::postvisit( VariableExpr * variableExpr ) { 588 601 extension( variableExpr ); 589 602 OperatorInfo opInfo; … … 595 608 } 596 609 597 void CodeGenerator:: visit( ConstantExpr * constantExpr ) {610 void CodeGenerator::postvisit( ConstantExpr * constantExpr ) { 598 611 assert( constantExpr->get_constant() ); 599 612 extension( constantExpr ); 600 constantExpr->get_constant()->accept( * this);601 } 602 603 void CodeGenerator:: visit( SizeofExpr * sizeofExpr ) {613 constantExpr->get_constant()->accept( *visitor ); 614 } 615 616 void CodeGenerator::postvisit( SizeofExpr * sizeofExpr ) { 604 617 extension( sizeofExpr ); 605 618 output << "sizeof("; … … 607 620 output << genType( sizeofExpr->get_type(), "", pretty, genC ); 608 621 } else { 609 sizeofExpr->get_expr()->accept( * this);622 sizeofExpr->get_expr()->accept( *visitor ); 610 623 } // if 611 624 output << ")"; 612 625 } 613 626 614 void CodeGenerator:: visit( AlignofExpr * alignofExpr ) {627 void CodeGenerator::postvisit( AlignofExpr * alignofExpr ) { 615 628 // use GCC extension to avoid bumping std to C11 616 629 extension( alignofExpr ); … … 619 632 output << genType( alignofExpr->get_type(), "", pretty, genC ); 620 633 } else { 621 alignofExpr->get_expr()->accept( * this);634 alignofExpr->get_expr()->accept( *visitor ); 622 635 } // if 623 636 output << ")"; 624 637 } 625 638 626 void CodeGenerator:: visit( UntypedOffsetofExpr * offsetofExpr ) {639 void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) { 627 640 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." ); 628 641 output << "offsetof("; … … 632 645 } 633 646 634 void CodeGenerator:: visit( OffsetofExpr * offsetofExpr ) {647 void CodeGenerator::postvisit( OffsetofExpr * offsetofExpr ) { 635 648 // use GCC builtin 636 649 output << "__builtin_offsetof("; … … 640 653 } 641 654 642 void CodeGenerator:: visit( OffsetPackExpr * offsetPackExpr ) {655 void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) { 643 656 assertf( ! genC, "OffsetPackExpr should not reach code generation." ); 644 657 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")"; 645 658 } 646 659 647 void CodeGenerator:: visit( LogicalExpr * logicalExpr ) {660 void CodeGenerator::postvisit( LogicalExpr * logicalExpr ) { 648 661 extension( logicalExpr ); 649 662 output << "("; 650 logicalExpr->get_arg1()->accept( * this);663 logicalExpr->get_arg1()->accept( *visitor ); 651 664 if ( logicalExpr->get_isAnd() ) { 652 665 output << " && "; … … 654 667 output << " || "; 655 668 } // if 656 logicalExpr->get_arg2()->accept( * this);669 logicalExpr->get_arg2()->accept( *visitor ); 657 670 output << ")"; 658 671 } 659 672 660 void CodeGenerator:: visit( ConditionalExpr * conditionalExpr ) {673 void CodeGenerator::postvisit( ConditionalExpr * conditionalExpr ) { 661 674 extension( conditionalExpr ); 662 675 output << "("; 663 conditionalExpr->get_arg1()->accept( * this);676 conditionalExpr->get_arg1()->accept( *visitor ); 664 677 output << " ? "; 665 conditionalExpr->get_arg2()->accept( * this);678 conditionalExpr->get_arg2()->accept( *visitor ); 666 679 output << " : "; 667 conditionalExpr->get_arg3()->accept( * this);680 conditionalExpr->get_arg3()->accept( *visitor ); 668 681 output << ")"; 669 682 } 670 683 671 void CodeGenerator:: visit( CommaExpr * commaExpr ) {684 void CodeGenerator::postvisit( CommaExpr * commaExpr ) { 672 685 extension( commaExpr ); 673 686 output << "("; … … 676 689 commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) ); 677 690 } 678 commaExpr->get_arg1()->accept( * this);691 commaExpr->get_arg1()->accept( *visitor ); 679 692 output << " , "; 680 commaExpr->get_arg2()->accept( * this);693 commaExpr->get_arg2()->accept( *visitor ); 681 694 output << ")"; 682 695 } 683 696 684 void CodeGenerator:: visit( TupleAssignExpr * tupleExpr ) {697 void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) { 685 698 assertf( ! genC, "TupleAssignExpr should not reach code generation." ); 686 tupleExpr->stmtExpr->accept( * this);687 } 688 689 void CodeGenerator:: visit( UntypedTupleExpr * tupleExpr ) {699 tupleExpr->stmtExpr->accept( *visitor ); 700 } 701 702 void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) { 690 703 assertf( ! genC, "UntypedTupleExpr should not reach code generation." ); 691 704 extension( tupleExpr ); … … 695 708 } 696 709 697 void CodeGenerator:: visit( TupleExpr * tupleExpr ) {710 void CodeGenerator::postvisit( TupleExpr * tupleExpr ) { 698 711 assertf( ! genC, "TupleExpr should not reach code generation." ); 699 712 extension( tupleExpr ); … … 703 716 } 704 717 705 void CodeGenerator:: visit( TupleIndexExpr * tupleExpr ) {718 void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) { 706 719 assertf( ! genC, "TupleIndexExpr should not reach code generation." ); 707 720 extension( tupleExpr ); 708 tupleExpr->get_tuple()->accept( * this);721 tupleExpr->get_tuple()->accept( *visitor ); 709 722 output << "." << tupleExpr->get_index(); 710 723 } 711 724 712 void CodeGenerator:: visit( TypeExpr * typeExpr ) {725 void CodeGenerator::postvisit( TypeExpr * typeExpr ) { 713 726 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl; 714 727 // assertf( ! genC, "TypeExpr should not reach code generation." ); … … 718 731 } 719 732 720 void CodeGenerator:: visit( AsmExpr * asmExpr ) {733 void CodeGenerator::postvisit( AsmExpr * asmExpr ) { 721 734 if ( asmExpr->get_inout() ) { 722 735 output << "[ "; 723 asmExpr->get_inout()->accept( * this);736 asmExpr->get_inout()->accept( *visitor ); 724 737 output << " ] "; 725 738 } // if 726 asmExpr->get_constraint()->accept( * this);739 asmExpr->get_constraint()->accept( *visitor ); 727 740 output << " ( "; 728 asmExpr->get_operand()->accept( * this);741 asmExpr->get_operand()->accept( *visitor ); 729 742 output << " )"; 730 743 } 731 744 732 void CodeGenerator:: visit( CompoundLiteralExpr *compLitExpr ) {745 void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) { 733 746 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); 734 747 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")"; 735 compLitExpr->get_initializer()->accept( * this);736 } 737 738 void CodeGenerator:: visit( UniqueExpr * unqExpr ) {748 compLitExpr->get_initializer()->accept( *visitor ); 749 } 750 751 void CodeGenerator::postvisit( UniqueExpr * unqExpr ) { 739 752 assertf( ! genC, "Unique expressions should not reach code generation." ); 740 753 output << "unq<" << unqExpr->get_id() << ">{ "; 741 unqExpr->get_expr()->accept( * this);754 unqExpr->get_expr()->accept( *visitor ); 742 755 output << " }"; 743 756 } 744 757 745 void CodeGenerator:: visit( StmtExpr * stmtExpr ) {758 void CodeGenerator::postvisit( StmtExpr * stmtExpr ) { 746 759 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 747 760 updateLocation( stmtExpr ); … … 757 770 // cannot cast to void, otherwise the expression statement has no value 758 771 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { 759 exprStmt->get_expr()->accept( * this);772 exprStmt->get_expr()->accept( *visitor ); 760 773 output << ";" << endl; 761 774 ++i; … … 763 776 } 764 777 } 765 stmt->accept( * this);778 stmt->accept( *visitor ); 766 779 output << endl; 767 780 if ( wantSpacing( stmt ) ) { … … 775 788 776 789 // *** Statements 777 void CodeGenerator:: visit( CompoundStmt * compoundStmt ) {790 void CodeGenerator::postvisit( CompoundStmt * compoundStmt ) { 778 791 std::list<Statement*> ks = compoundStmt->get_kids(); 779 792 output << "{" << endl; … … 783 796 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { 784 797 output << indent << printLabels( (*i)->get_labels() ); 785 (*i)->accept( * this);798 (*i)->accept( *visitor ); 786 799 787 800 output << endl; … … 795 808 } 796 809 797 void CodeGenerator:: visit( ExprStmt * exprStmt ) {810 void CodeGenerator::postvisit( ExprStmt * exprStmt ) { 798 811 assert( exprStmt ); 799 812 if ( genC ) { … … 801 814 exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) ); 802 815 } 803 exprStmt->get_expr()->accept( * this);816 exprStmt->get_expr()->accept( *visitor ); 804 817 output << ";"; 805 818 } 806 819 807 void CodeGenerator:: visit( AsmStmt * asmStmt ) {820 void CodeGenerator::postvisit( AsmStmt * asmStmt ) { 808 821 output << "asm "; 809 822 if ( asmStmt->get_voltile() ) output << "volatile "; 810 823 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; 811 824 output << "( "; 812 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( * this);825 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); 813 826 output << " : "; 814 827 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); … … 828 841 } 829 842 830 void CodeGenerator:: visit( AsmDecl * asmDecl ) {843 void CodeGenerator::postvisit( AsmDecl * asmDecl ) { 831 844 output << "asm "; 832 845 AsmStmt * asmStmt = asmDecl->get_stmt(); 833 846 output << "( "; 834 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( * this);847 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); 835 848 output << " )" ; 836 849 } 837 850 838 void CodeGenerator:: visit( IfStmt * ifStmt ) {851 void CodeGenerator::postvisit( IfStmt * ifStmt ) { 839 852 updateLocation( ifStmt ); 840 853 output << "if ( "; 841 ifStmt->get_condition()->accept( * this);854 ifStmt->get_condition()->accept( *visitor ); 842 855 output << " ) "; 843 856 844 ifStmt->get_thenPart()->accept( * this);857 ifStmt->get_thenPart()->accept( *visitor ); 845 858 846 859 if ( ifStmt->get_elsePart() != 0) { 847 860 output << " else "; 848 ifStmt->get_elsePart()->accept( * this);849 } // if 850 } 851 852 void CodeGenerator:: visit( SwitchStmt * switchStmt ) {861 ifStmt->get_elsePart()->accept( *visitor ); 862 } // if 863 } 864 865 void CodeGenerator::postvisit( SwitchStmt * switchStmt ) { 853 866 updateLocation( switchStmt ); 854 867 output << "switch ( " ; 855 switchStmt->get_condition()->accept( * this);868 switchStmt->get_condition()->accept( *visitor ); 856 869 output << " ) "; 857 870 858 871 output << "{" << std::endl; 859 872 ++indent; 860 acceptAll( switchStmt->get_statements(), * this);873 acceptAll( switchStmt->get_statements(), *visitor ); 861 874 --indent; 862 875 output << indent << "}"; 863 876 } 864 877 865 void CodeGenerator:: visit( CaseStmt * caseStmt ) {878 void CodeGenerator::postvisit( CaseStmt * caseStmt ) { 866 879 updateLocation( caseStmt ); 867 880 if ( caseStmt->isDefault()) { … … 869 882 } else { 870 883 output << "case "; 871 caseStmt->get_condition()->accept( * this);884 caseStmt->get_condition()->accept( *visitor ); 872 885 } // if 873 886 output << ":\n"; … … 878 891 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 879 892 output << indent << printLabels( (*i)->get_labels() ) ; 880 (*i)->accept( * this);893 (*i)->accept( *visitor ); 881 894 output << endl; 882 895 } // for … … 884 897 } 885 898 886 void CodeGenerator:: visit( BranchStmt * branchStmt ) {899 void CodeGenerator::postvisit( BranchStmt * branchStmt ) { 887 900 switch ( branchStmt->get_type()) { 888 901 case BranchStmt::Goto: … … 892 905 if ( branchStmt->get_computedTarget() != 0 ) { 893 906 output << "goto *"; 894 branchStmt->get_computedTarget()->accept( * this);907 branchStmt->get_computedTarget()->accept( *visitor ); 895 908 } // if 896 909 } // if … … 906 919 } 907 920 908 void CodeGenerator:: visit( ReturnStmt * returnStmt ) {921 void CodeGenerator::postvisit( ReturnStmt * returnStmt ) { 909 922 output << "return "; 910 maybeAccept( returnStmt->get_expr(), * this);923 maybeAccept( returnStmt->get_expr(), *visitor ); 911 924 output << ";"; 912 925 } 913 926 914 void CodeGenerator:: visit( ThrowStmt * throwStmt ) {927 void CodeGenerator::postvisit( ThrowStmt * throwStmt ) { 915 928 assertf( ! genC, "Throw statements should not reach code generation." ); 916 929 … … 919 932 if (throwStmt->get_expr()) { 920 933 output << " "; 921 throwStmt->get_expr()->accept( * this);934 throwStmt->get_expr()->accept( *visitor ); 922 935 } 923 936 if (throwStmt->get_target()) { 924 937 output << " _At "; 925 throwStmt->get_target()->accept( * this);938 throwStmt->get_target()->accept( *visitor ); 926 939 } 927 940 output << ";"; 928 941 } 929 942 930 void CodeGenerator:: visit( WhileStmt * whileStmt ) {943 void CodeGenerator::postvisit( WhileStmt * whileStmt ) { 931 944 if ( whileStmt->get_isDoWhile() ) { 932 945 output << "do" ; 933 946 } else { 934 947 output << "while (" ; 935 whileStmt->get_condition()->accept( * this);948 whileStmt->get_condition()->accept( *visitor ); 936 949 output << ")"; 937 950 } // if … … 939 952 940 953 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); 941 whileStmt->get_body()->accept( * this);954 whileStmt->get_body()->accept( *visitor ); 942 955 943 956 output << indent; … … 945 958 if ( whileStmt->get_isDoWhile() ) { 946 959 output << " while (" ; 947 whileStmt->get_condition()->accept( * this);960 whileStmt->get_condition()->accept( *visitor ); 948 961 output << ");"; 949 962 } // if 950 963 } 951 964 952 void CodeGenerator:: visit( ForStmt * forStmt ) {965 void CodeGenerator::postvisit( ForStmt * forStmt ) { 953 966 // initialization is always hoisted, so don't bother doing anything with that 954 967 output << "for (;"; 955 968 956 969 if ( forStmt->get_condition() != 0 ) { 957 forStmt->get_condition()->accept( * this);970 forStmt->get_condition()->accept( *visitor ); 958 971 } // if 959 972 output << ";"; … … 962 975 // cast the top-level expression to void to reduce gcc warnings. 963 976 Expression * expr = new CastExpr( forStmt->get_increment() ); 964 expr->accept( * this);977 expr->accept( *visitor ); 965 978 } // if 966 979 output << ") "; … … 968 981 if ( forStmt->get_body() != 0 ) { 969 982 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); 970 forStmt->get_body()->accept( * this);971 } // if 972 } 973 974 void CodeGenerator:: visit( __attribute__((unused)) NullStmt * nullStmt ) {983 forStmt->get_body()->accept( *visitor ); 984 } // if 985 } 986 987 void CodeGenerator::postvisit( __attribute__((unused)) NullStmt * nullStmt ) { 975 988 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); 976 989 output << "/* null statement */ ;"; 977 990 } 978 991 979 void CodeGenerator:: visit( DeclStmt * declStmt ) {980 declStmt->get_decl()->accept( * this);992 void CodeGenerator::postvisit( DeclStmt * declStmt ) { 993 declStmt->get_decl()->accept( *visitor ); 981 994 982 995 if ( doSemicolon( declStmt->get_decl() ) ) { 983 996 output << ";"; 984 997 } // if 998 } 999 1000 void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) { 1001 assertf( ! genC, "ImplicitCtorDtorStmts should not reach code generation." ); 1002 stmt->callStmt->accept( *visitor ); 985 1003 } 986 1004 -
src/CodeGen/CodeGenerator.h
racdfb45 r9857e8d 21 21 22 22 #include "Common/Indenter.h" // for Indenter 23 #include "Common/PassVisitor.h" // for PassVisitor 23 24 #include "SynTree/Declaration.h" // for DeclarationWithType (ptr only), Fun... 24 25 #include "SynTree/Visitor.h" // for Visitor … … 26 27 27 28 namespace CodeGen { 28 class CodeGenerator : public Visitor { 29 public: 30 static int tabsize; 29 struct CodeGenerator : public WithShortCircuiting, public WithVisitorRef<CodeGenerator> { 30 static int tabsize; 31 31 32 32 CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false ); … … 34 34 CodeGenerator( std::ostream &os, char *, int indent = 0, bool infun = false ); 35 35 36 //*** Turn off visit_children for all nodes 37 void previsit( BaseSyntaxNode * ); 38 39 //*** Error for unhandled node types 40 void postvisit( BaseSyntaxNode * ); 41 36 42 //*** Declaration 37 v irtual voidvisit( StructDecl * );38 v irtual voidvisit( FunctionDecl * );39 v irtual voidvisit( ObjectDecl * );40 v irtual voidvisit( UnionDecl *aggregateDecl );41 v irtual voidvisit( EnumDecl *aggregateDecl );42 v irtual voidvisit( TraitDecl *aggregateDecl );43 v irtual voidvisit( TypedefDecl *typeDecl );44 v irtual voidvisit( TypeDecl *typeDecl );43 void postvisit( StructDecl * ); 44 void postvisit( FunctionDecl * ); 45 void postvisit( ObjectDecl * ); 46 void postvisit( UnionDecl *aggregateDecl ); 47 void postvisit( EnumDecl *aggregateDecl ); 48 void postvisit( TraitDecl *aggregateDecl ); 49 void postvisit( TypedefDecl *typeDecl ); 50 void postvisit( TypeDecl *typeDecl ); 45 51 46 52 //*** Initializer 47 v irtual voidvisit( Designation * );48 v irtual voidvisit( SingleInit * );49 v irtual voidvisit( ListInit * );50 v irtual voidvisit( ConstructorInit * );53 void postvisit( Designation * ); 54 void postvisit( SingleInit * ); 55 void postvisit( ListInit * ); 56 void postvisit( ConstructorInit * ); 51 57 52 58 //*** Constant 53 v irtual voidvisit( Constant * );59 void postvisit( Constant * ); 54 60 55 61 //*** Expression 56 v irtual voidvisit( ApplicationExpr *applicationExpr );57 v irtual voidvisit( UntypedExpr *untypedExpr );58 v irtual voidvisit( RangeExpr * rangeExpr );59 v irtual voidvisit( NameExpr *nameExpr );60 v irtual voidvisit( AddressExpr *addressExpr );61 v irtual voidvisit( LabelAddressExpr *addressExpr );62 v irtual voidvisit( CastExpr *castExpr );63 v irtual voidvisit( VirtualCastExpr *castExpr );64 v irtual voidvisit( UntypedMemberExpr *memberExpr );65 v irtual voidvisit( MemberExpr *memberExpr );66 v irtual voidvisit( VariableExpr *variableExpr );67 v irtual voidvisit( ConstantExpr *constantExpr );68 v irtual voidvisit( SizeofExpr *sizeofExpr );69 v irtual voidvisit( AlignofExpr *alignofExpr );70 v irtual voidvisit( UntypedOffsetofExpr *offsetofExpr );71 v irtual voidvisit( OffsetofExpr *offsetofExpr );72 v irtual voidvisit( OffsetPackExpr *offsetPackExpr );73 v irtual voidvisit( LogicalExpr *logicalExpr );74 v irtual voidvisit( ConditionalExpr *conditionalExpr );75 v irtual voidvisit( CommaExpr *commaExpr );76 v irtual voidvisit( CompoundLiteralExpr *compLitExpr );77 v irtual voidvisit( UniqueExpr * );78 v irtual voidvisit( TupleAssignExpr * tupleExpr );79 v irtual voidvisit( UntypedTupleExpr *tupleExpr );80 v irtual voidvisit( TupleExpr *tupleExpr );81 v irtual voidvisit( TupleIndexExpr * tupleExpr );82 v irtual voidvisit( TypeExpr *typeExpr );83 v irtual voidvisit( AsmExpr * );84 v irtual voidvisit( StmtExpr * );62 void postvisit( ApplicationExpr *applicationExpr ); 63 void postvisit( UntypedExpr *untypedExpr ); 64 void postvisit( RangeExpr * rangeExpr ); 65 void postvisit( NameExpr *nameExpr ); 66 void postvisit( AddressExpr *addressExpr ); 67 void postvisit( LabelAddressExpr *addressExpr ); 68 void postvisit( CastExpr *castExpr ); 69 void postvisit( VirtualCastExpr *castExpr ); 70 void postvisit( UntypedMemberExpr *memberExpr ); 71 void postvisit( MemberExpr *memberExpr ); 72 void postvisit( VariableExpr *variableExpr ); 73 void postvisit( ConstantExpr *constantExpr ); 74 void postvisit( SizeofExpr *sizeofExpr ); 75 void postvisit( AlignofExpr *alignofExpr ); 76 void postvisit( UntypedOffsetofExpr *offsetofExpr ); 77 void postvisit( OffsetofExpr *offsetofExpr ); 78 void postvisit( OffsetPackExpr *offsetPackExpr ); 79 void postvisit( LogicalExpr *logicalExpr ); 80 void postvisit( ConditionalExpr *conditionalExpr ); 81 void postvisit( CommaExpr *commaExpr ); 82 void postvisit( CompoundLiteralExpr *compLitExpr ); 83 void postvisit( UniqueExpr * ); 84 void postvisit( TupleAssignExpr * tupleExpr ); 85 void postvisit( UntypedTupleExpr *tupleExpr ); 86 void postvisit( TupleExpr *tupleExpr ); 87 void postvisit( TupleIndexExpr * tupleExpr ); 88 void postvisit( TypeExpr *typeExpr ); 89 void postvisit( AsmExpr * ); 90 void postvisit( StmtExpr * ); 85 91 86 92 //*** Statements 87 virtual void visit( CompoundStmt * ); 88 virtual void visit( ExprStmt * ); 89 virtual void visit( AsmStmt * ); 90 virtual void visit( AsmDecl * ); // special: statement in declaration context 91 virtual void visit( IfStmt * ); 92 virtual void visit( SwitchStmt * ); 93 virtual void visit( CaseStmt * ); 94 virtual void visit( BranchStmt * ); 95 virtual void visit( ReturnStmt * ); 96 virtual void visit( ThrowStmt * ); 97 virtual void visit( WhileStmt * ); 98 virtual void visit( ForStmt * ); 99 virtual void visit( NullStmt * ); 100 virtual void visit( DeclStmt * ); 93 void postvisit( CompoundStmt * ); 94 void postvisit( ExprStmt * ); 95 void postvisit( AsmStmt * ); 96 void postvisit( AsmDecl * ); // special: statement in declaration context 97 void postvisit( IfStmt * ); 98 void postvisit( SwitchStmt * ); 99 void postvisit( CaseStmt * ); 100 void postvisit( BranchStmt * ); 101 void postvisit( ReturnStmt * ); 102 void postvisit( ThrowStmt * ); 103 void postvisit( WhileStmt * ); 104 void postvisit( ForStmt * ); 105 void postvisit( NullStmt * ); 106 void postvisit( DeclStmt * ); 107 void postvisit( ImplicitCtorDtorStmt * ); 101 108 102 109 void genAttributes( std::list< Attribute * > & attributes ); … … 140 147 if ( begin == end ) return; 141 148 for ( ;; ) { 142 (*begin++)->accept( * this);149 (*begin++)->accept( *visitor ); 143 150 if ( begin == end ) break; 144 151 output << ", "; // separator -
src/CodeGen/GenType.cc
racdfb45 r9857e8d 63 63 64 64 if ( ! type->get_attributes().empty() ) { 65 CodeGeneratorcg( os, pretty, genC, lineMarks );66 cg. genAttributes( type->get_attributes() );65 PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); 66 cg.pass.genAttributes( type->get_attributes() ); 67 67 } // if 68 68 … … 116 116 } // if 117 117 if ( dimension != 0 ) { 118 CodeGeneratorcg( os, pretty, genC, lineMarks );118 PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); 119 119 dimension->accept( cg ); 120 120 } else if ( isVarLen ) { … … 178 178 } // if 179 179 } else { 180 CodeGeneratorcg( os, pretty, genC, lineMarks );180 PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); 181 181 os << "(" ; 182 182 183 cg. genCommaList( pars.begin(), pars.end() );183 cg.pass.genCommaList( pars.begin(), pars.end() ); 184 184 185 185 if ( funcType->get_isVarArgs() ) { … … 201 201 // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); 202 202 std::ostringstream os; 203 CodeGeneratorcg( os, pretty, genC, lineMarks );203 PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); 204 204 os << "forall("; 205 cg. genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() );205 cg.pass.genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() ); 206 206 os << ")" << std::endl; 207 207 typeString = os.str() + typeString; … … 212 212 if ( ! refType->get_parameters().empty() ) { 213 213 std::ostringstream os; 214 CodeGeneratorcg( os, pretty, genC, lineMarks );214 PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks ); 215 215 os << "("; 216 cg. genCommaList( refType->get_parameters().begin(), refType->get_parameters().end() );216 cg.pass.genCommaList( refType->get_parameters().begin(), refType->get_parameters().end() ); 217 217 os << ") "; 218 218 return os.str(); -
src/CodeGen/Generate.cc
racdfb45 r9857e8d 49 49 cleanTree( translationUnit ); 50 50 51 CodeGen::CodeGeneratorcgv( os, pretty, generateC, lineMarks );51 PassVisitor<CodeGenerator> cgv( os, pretty, generateC, lineMarks ); 52 52 for ( auto & dcl : translationUnit ) { 53 53 if ( LinkageSpec::isGeneratable( dcl->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( dcl->get_linkage() ) ) ) { 54 cgv. updateLocation( dcl );54 cgv.pass.updateLocation( dcl ); 55 55 dcl->accept(cgv); 56 56 if ( doSemicolon( dcl ) ) { -
src/SynTree/BaseSyntaxNode.h
racdfb45 r9857e8d 26 26 27 27 virtual void accept( Visitor & v ) = 0; 28 virtual void print( std::ostream & os, int indent = 0 ) const = 0; 28 29 }; 29 30
Note: See TracChangeset
for help on using the changeset viewer.