Changes in src/CodeGen/CodeGenerator.cc [d48e529:a0c7dc36]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (62 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
rd48e529 ra0c7dc36 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sun Sep 3 20:42:52201713 // Update Count : 4 9011 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Aug 18 15:34:00 2017 13 // Update Count : 488 14 14 // 15 15 #include "CodeGenerator.h" … … 59 59 60 60 void CodeGenerator::asmName( DeclarationWithType * decl ) { 61 if ( ConstantExpr * asmName = d ynamic_cast<ConstantExpr *>(decl->get_asmName()) ) {61 if ( ConstantExpr * asmName = decl->get_asmName() ) { 62 62 output << " asm ( " << asmName->get_constant()->get_value() << " )"; 63 63 } // if … … 89 89 } else if ( currentLocation.followedBy( to, 1 ) ) { 90 90 output << "\n" << indent; 91 currentLocation. first_line+= 1;91 currentLocation.linenumber += 1; 92 92 } else if ( currentLocation.followedBy( to, 2 ) ) { 93 93 output << "\n\n" << indent; 94 currentLocation. first_line+= 2;95 } else { 96 output << "\n# " << to. first_line<< " \"" << to.filename94 currentLocation.linenumber += 2; 95 } else { 96 output << "\n# " << to.linenumber << " \"" << to.filename 97 97 << "\"\n" << indent; 98 98 currentLocation = to; … … 139 139 } // CodeGenerator::genAttributes 140 140 141 // *** BaseSyntaxNode142 void CodeGenerator::previsit( BaseSyntaxNode * ) {143 // turn off automatic recursion for all nodes, to allow each visitor to144 // precisely control the order in which its children are visited.145 visit_children = false;146 }147 148 // *** BaseSyntaxNode149 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 }154 141 155 142 // *** Declarations 156 void CodeGenerator:: postvisit( FunctionDecl * functionDecl ) {143 void CodeGenerator::visit( FunctionDecl * functionDecl ) { 157 144 extension( functionDecl ); 158 145 genAttributes( functionDecl->get_attributes() ); … … 165 152 asmName( functionDecl ); 166 153 167 // acceptAll( functionDecl->get_oldDecls(), * visitor);154 // acceptAll( functionDecl->get_oldDecls(), *this ); 168 155 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 ) { 174 161 if (objectDecl->get_name().empty() && genC ) { 175 162 // only generate an anonymous name when generating C code, otherwise it clutters the output too much … … 188 175 if ( objectDecl->get_init() ) { 189 176 output << " = "; 190 objectDecl->get_init()->accept( * visitor);177 objectDecl->get_init()->accept( *this ); 191 178 } // if 192 179 193 180 if ( objectDecl->get_bitfieldWidth() ) { 194 181 output << ":"; 195 objectDecl->get_bitfieldWidth()->accept( * visitor);182 objectDecl->get_bitfieldWidth()->accept( *this ); 196 183 } // if 197 184 } … … 218 205 updateLocation( *i ); 219 206 output << indent; 220 (*i)->accept( * visitor);207 (*i)->accept( *this ); 221 208 output << ";" << endl; 222 209 } // for … … 228 215 } 229 216 230 void CodeGenerator:: postvisit( StructDecl * structDecl ) {217 void CodeGenerator::visit( StructDecl * structDecl ) { 231 218 extension( structDecl ); 232 219 handleAggregate( structDecl, "struct " ); 233 220 } 234 221 235 void CodeGenerator:: postvisit( UnionDecl * unionDecl ) {222 void CodeGenerator::visit( UnionDecl * unionDecl ) { 236 223 extension( unionDecl ); 237 224 handleAggregate( unionDecl, "union " ); 238 225 } 239 226 240 void CodeGenerator:: postvisit( EnumDecl * enumDecl ) {227 void CodeGenerator::visit( EnumDecl * enumDecl ) { 241 228 extension( enumDecl ); 242 229 updateLocation( enumDecl ); … … 259 246 if ( obj->get_init() ) { 260 247 output << " = "; 261 obj->get_init()->accept( * visitor);248 obj->get_init()->accept( *this ); 262 249 } // if 263 250 output << "," << endl; … … 270 257 } 271 258 272 void CodeGenerator:: postvisit( TraitDecl * traitDecl ) {259 void CodeGenerator::visit( TraitDecl * traitDecl ) { 273 260 assertf( ! genC, "TraitDecls should not reach code generation." ); 274 261 extension( traitDecl ); … … 276 263 } 277 264 278 void CodeGenerator:: postvisit( TypedefDecl * typeDecl ) {265 void CodeGenerator::visit( TypedefDecl * typeDecl ) { 279 266 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); 280 267 updateLocation( typeDecl ); … … 283 270 } 284 271 285 void CodeGenerator:: postvisit( TypeDecl * typeDecl ) {272 void CodeGenerator::visit( TypeDecl * typeDecl ) { 286 273 assertf( ! genC, "TypeDecls should not reach code generation." ); 287 274 output << typeDecl->genTypeString() << " " << typeDecl->get_name(); … … 296 283 } 297 284 298 void CodeGenerator:: postvisit( Designation * designation ) {285 void CodeGenerator::visit( Designation * designation ) { 299 286 std::list< Expression * > designators = designation->get_designators(); 300 287 if ( designators.size() == 0 ) return; … … 303 290 // if expression is a NameExpr or VariableExpr, then initializing aggregate member 304 291 output << "."; 305 des->accept( * visitor);292 des->accept( *this ); 306 293 } else { 307 294 // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt 308 295 output << "["; 309 des->accept( * visitor);296 des->accept( *this ); 310 297 output << "]"; 311 298 } // if … … 314 301 } 315 302 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 ) { 321 308 auto initBegin = init->begin(); 322 309 auto initEnd = init->end(); … … 326 313 output << "{ "; 327 314 for ( ; initBegin != initEnd && desigBegin != desigEnd; ) { 328 (*desigBegin)->accept( * visitor);329 (*initBegin)->accept( * visitor);315 (*desigBegin)->accept( *this ); 316 (*initBegin)->accept( *this ); 330 317 ++initBegin, ++desigBegin; 331 318 if ( initBegin != initEnd ) { … … 337 324 } 338 325 339 void CodeGenerator:: postvisit( __attribute__((unused)) ConstructorInit * init ){326 void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){ 340 327 assertf( ! genC, "ConstructorInit nodes should not reach code generation." ); 341 328 // pseudo-output for constructor/destructor pairs 342 329 output << "<ctorinit>{" << std::endl << ++indent << "ctor: "; 343 maybeAccept( init->get_ctor(), * visitor);330 maybeAccept( init->get_ctor(), *this ); 344 331 output << ", " << std::endl << indent << "dtor: "; 345 maybeAccept( init->get_dtor(), * visitor);332 maybeAccept( init->get_dtor(), *this ); 346 333 output << std::endl << --indent << "}"; 347 334 } 348 335 349 void CodeGenerator:: postvisit( Constant * constant ) {336 void CodeGenerator::visit( Constant * constant ) { 350 337 output << constant->get_value() ; 351 338 } 352 339 353 340 // *** Expressions 354 void CodeGenerator:: postvisit( ApplicationExpr * applicationExpr ) {341 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) { 355 342 extension( applicationExpr ); 356 343 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { … … 361 348 case OT_INDEX: 362 349 assert( applicationExpr->get_args().size() == 2 ); 363 (*arg++)->accept( * visitor);350 (*arg++)->accept( *this ); 364 351 output << "["; 365 (*arg)->accept( * visitor);352 (*arg)->accept( *this ); 366 353 output << "]"; 367 354 break; … … 378 365 // effects, so must still output this expression 379 366 output << "("; 380 (*arg++)->accept( * visitor);367 (*arg++)->accept( *this ); 381 368 output << ") /* " << opInfo.inputName << " */"; 382 369 } else if ( applicationExpr->get_args().size() == 2 ) { 383 370 // intrinsic two parameter constructors are essentially bitwise assignment 384 371 output << "("; 385 (*arg++)->accept( * visitor);372 (*arg++)->accept( *this ); 386 373 output << opInfo.symbol; 387 (*arg)->accept( * visitor);374 (*arg)->accept( *this ); 388 375 output << ") /* " << opInfo.inputName << " */"; 389 376 } else { … … 398 385 output << "("; 399 386 output << opInfo.symbol; 400 (*arg)->accept( * visitor);387 (*arg)->accept( *this ); 401 388 output << ")"; 402 389 break; … … 405 392 case OT_POSTFIXASSIGN: 406 393 assert( applicationExpr->get_args().size() == 1 ); 407 (*arg)->accept( * visitor);394 (*arg)->accept( *this ); 408 395 output << opInfo.symbol; 409 396 break; … … 414 401 assert( applicationExpr->get_args().size() == 2 ); 415 402 output << "("; 416 (*arg++)->accept( * visitor);403 (*arg++)->accept( *this ); 417 404 output << opInfo.symbol; 418 (*arg)->accept( * visitor);405 (*arg)->accept( *this ); 419 406 output << ")"; 420 407 break; … … 426 413 } // switch 427 414 } else { 428 varExpr->accept( * visitor);415 varExpr->accept( *this ); 429 416 output << "("; 430 417 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); … … 432 419 } // if 433 420 } else { 434 applicationExpr->get_function()->accept( * visitor);421 applicationExpr->get_function()->accept( *this ); 435 422 output << "("; 436 423 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); … … 439 426 } 440 427 441 void CodeGenerator:: postvisit( UntypedExpr * untypedExpr ) {428 void CodeGenerator::visit( UntypedExpr * untypedExpr ) { 442 429 extension( untypedExpr ); 443 430 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { … … 448 435 case OT_INDEX: 449 436 assert( untypedExpr->get_args().size() == 2 ); 450 (*arg++)->accept( * visitor);437 (*arg++)->accept( *this ); 451 438 output << "["; 452 (*arg)->accept( * visitor);439 (*arg)->accept( *this ); 453 440 output << "]"; 454 441 break; … … 463 450 // effects, so must still output this expression 464 451 output << "("; 465 (*arg++)->accept( * visitor);452 (*arg++)->accept( *this ); 466 453 output << ") /* " << opInfo.inputName << " */"; 467 454 } else if ( untypedExpr->get_args().size() == 2 ) { 468 455 // intrinsic two parameter constructors are essentially bitwise assignment 469 456 output << "("; 470 (*arg++)->accept( * visitor);457 (*arg++)->accept( *this ); 471 458 output << opInfo.symbol; 472 (*arg)->accept( * visitor);459 (*arg)->accept( *this ); 473 460 output << ") /* " << opInfo.inputName << " */"; 474 461 } else { 475 462 // 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 ); 482 464 } // if 483 465 break; … … 489 471 output << "("; 490 472 output << opInfo.symbol; 491 (*arg)->accept( * visitor);473 (*arg)->accept( *this ); 492 474 output << ")"; 493 475 break; … … 496 478 case OT_POSTFIXASSIGN: 497 479 assert( untypedExpr->get_args().size() == 1 ); 498 (*arg)->accept( * visitor);480 (*arg)->accept( *this ); 499 481 output << opInfo.symbol; 500 482 break; … … 504 486 assert( untypedExpr->get_args().size() == 2 ); 505 487 output << "("; 506 (*arg++)->accept( * visitor);488 (*arg++)->accept( *this ); 507 489 output << opInfo.symbol; 508 (*arg)->accept( * visitor);490 (*arg)->accept( *this ); 509 491 output << ")"; 510 492 break; … … 517 499 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2 518 500 assert( untypedExpr->get_args().size() == 2 ); 519 (*untypedExpr->get_args().begin())->accept( * visitor);501 (*untypedExpr->get_args().begin())->accept( *this ); 520 502 output << " ... "; 521 (*--untypedExpr->get_args().end())->accept( * visitor);503 (*--untypedExpr->get_args().end())->accept( *this ); 522 504 } else { // builtin routines 523 nameExpr->accept( * visitor);505 nameExpr->accept( *this ); 524 506 output << "("; 525 507 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); … … 528 510 } // if 529 511 } else { 530 untypedExpr->get_function()->accept( * visitor);512 untypedExpr->get_function()->accept( *this ); 531 513 output << "("; 532 514 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); … … 535 517 } 536 518 537 void CodeGenerator:: postvisit( RangeExpr * rangeExpr ) {538 rangeExpr->get_low()->accept( * visitor);519 void CodeGenerator::visit( RangeExpr * rangeExpr ) { 520 rangeExpr->get_low()->accept( *this ); 539 521 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 ) { 544 526 extension( nameExpr ); 545 527 OperatorInfo opInfo; … … 552 534 } 553 535 554 void CodeGenerator:: postvisit( AddressExpr * addressExpr ) {536 void CodeGenerator::visit( AddressExpr * addressExpr ) { 555 537 extension( addressExpr ); 556 538 output << "(&"; 557 addressExpr->arg->accept( * visitor);539 addressExpr->arg->accept( *this ); 558 540 output << ")"; 559 541 } 560 542 561 void CodeGenerator:: postvisit( LabelAddressExpr *addressExpr ) {543 void CodeGenerator::visit( LabelAddressExpr *addressExpr ) { 562 544 extension( addressExpr ); 563 545 output << "(&&" << addressExpr->arg << ")"; 564 546 } 565 547 566 void CodeGenerator:: postvisit( CastExpr * castExpr ) {548 void CodeGenerator::visit( CastExpr * castExpr ) { 567 549 extension( castExpr ); 568 550 output << "("; … … 577 559 output << ")"; 578 560 } // if 579 castExpr->get_arg()->accept( * visitor);561 castExpr->get_arg()->accept( *this ); 580 562 output << ")"; 581 563 } 582 564 583 void CodeGenerator:: postvisit( VirtualCastExpr * castExpr ) {565 void CodeGenerator::visit( VirtualCastExpr * castExpr ) { 584 566 assertf( ! genC, "VirtualCastExpr should not reach code generation." ); 585 567 extension( castExpr ); 586 568 output << "(virtual "; 587 castExpr->get_arg()->accept( * visitor);569 castExpr->get_arg()->accept( *this ); 588 570 output << ")"; 589 571 } 590 572 591 void CodeGenerator:: postvisit( UntypedMemberExpr * memberExpr ) {573 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) { 592 574 assertf( ! genC, "UntypedMemberExpr should not reach code generation." ); 593 575 extension( memberExpr ); 594 memberExpr->get_aggregate()->accept( * visitor);576 memberExpr->get_aggregate()->accept( *this ); 595 577 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 ) { 600 582 extension( memberExpr ); 601 memberExpr->get_aggregate()->accept( * visitor);583 memberExpr->get_aggregate()->accept( *this ); 602 584 output << "." << mangleName( memberExpr->get_member() ); 603 585 } 604 586 605 void CodeGenerator:: postvisit( VariableExpr * variableExpr ) {587 void CodeGenerator::visit( VariableExpr * variableExpr ) { 606 588 extension( variableExpr ); 607 589 OperatorInfo opInfo; … … 613 595 } 614 596 615 void CodeGenerator:: postvisit( ConstantExpr * constantExpr ) {597 void CodeGenerator::visit( ConstantExpr * constantExpr ) { 616 598 assert( constantExpr->get_constant() ); 617 599 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 ) { 622 604 extension( sizeofExpr ); 623 605 output << "sizeof("; … … 625 607 output << genType( sizeofExpr->get_type(), "", pretty, genC ); 626 608 } else { 627 sizeofExpr->get_expr()->accept( * visitor);609 sizeofExpr->get_expr()->accept( *this ); 628 610 } // if 629 611 output << ")"; 630 612 } 631 613 632 void CodeGenerator:: postvisit( AlignofExpr * alignofExpr ) {614 void CodeGenerator::visit( AlignofExpr * alignofExpr ) { 633 615 // use GCC extension to avoid bumping std to C11 634 616 extension( alignofExpr ); … … 637 619 output << genType( alignofExpr->get_type(), "", pretty, genC ); 638 620 } else { 639 alignofExpr->get_expr()->accept( * visitor);621 alignofExpr->get_expr()->accept( *this ); 640 622 } // if 641 623 output << ")"; 642 624 } 643 625 644 void CodeGenerator:: postvisit( UntypedOffsetofExpr * offsetofExpr ) {626 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) { 645 627 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." ); 646 628 output << "offsetof("; … … 650 632 } 651 633 652 void CodeGenerator:: postvisit( OffsetofExpr * offsetofExpr ) {634 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) { 653 635 // use GCC builtin 654 636 output << "__builtin_offsetof("; … … 658 640 } 659 641 660 void CodeGenerator:: postvisit( OffsetPackExpr * offsetPackExpr ) {642 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) { 661 643 assertf( ! genC, "OffsetPackExpr should not reach code generation." ); 662 644 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")"; 663 645 } 664 646 665 void CodeGenerator:: postvisit( LogicalExpr * logicalExpr ) {647 void CodeGenerator::visit( LogicalExpr * logicalExpr ) { 666 648 extension( logicalExpr ); 667 649 output << "("; 668 logicalExpr->get_arg1()->accept( * visitor);650 logicalExpr->get_arg1()->accept( *this ); 669 651 if ( logicalExpr->get_isAnd() ) { 670 652 output << " && "; … … 672 654 output << " || "; 673 655 } // if 674 logicalExpr->get_arg2()->accept( * visitor);656 logicalExpr->get_arg2()->accept( *this ); 675 657 output << ")"; 676 658 } 677 659 678 void CodeGenerator:: postvisit( ConditionalExpr * conditionalExpr ) {660 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) { 679 661 extension( conditionalExpr ); 680 662 output << "("; 681 conditionalExpr->get_arg1()->accept( * visitor);663 conditionalExpr->get_arg1()->accept( *this ); 682 664 output << " ? "; 683 conditionalExpr->get_arg2()->accept( * visitor);665 conditionalExpr->get_arg2()->accept( *this ); 684 666 output << " : "; 685 conditionalExpr->get_arg3()->accept( * visitor);667 conditionalExpr->get_arg3()->accept( *this ); 686 668 output << ")"; 687 669 } 688 670 689 void CodeGenerator:: postvisit( CommaExpr * commaExpr ) {671 void CodeGenerator::visit( CommaExpr * commaExpr ) { 690 672 extension( commaExpr ); 691 673 output << "("; … … 694 676 commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) ); 695 677 } 696 commaExpr->get_arg1()->accept( * visitor);678 commaExpr->get_arg1()->accept( *this ); 697 679 output << " , "; 698 commaExpr->get_arg2()->accept( * visitor);680 commaExpr->get_arg2()->accept( *this ); 699 681 output << ")"; 700 682 } 701 683 702 void CodeGenerator:: postvisit( TupleAssignExpr * tupleExpr ) {684 void CodeGenerator::visit( TupleAssignExpr * tupleExpr ) { 703 685 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 ) { 708 690 assertf( ! genC, "UntypedTupleExpr should not reach code generation." ); 709 691 extension( tupleExpr ); … … 713 695 } 714 696 715 void CodeGenerator:: postvisit( TupleExpr * tupleExpr ) {697 void CodeGenerator::visit( TupleExpr * tupleExpr ) { 716 698 assertf( ! genC, "TupleExpr should not reach code generation." ); 717 699 extension( tupleExpr ); … … 721 703 } 722 704 723 void CodeGenerator:: postvisit( TupleIndexExpr * tupleExpr ) {705 void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) { 724 706 assertf( ! genC, "TupleIndexExpr should not reach code generation." ); 725 707 extension( tupleExpr ); 726 tupleExpr->get_tuple()->accept( * visitor);708 tupleExpr->get_tuple()->accept( *this ); 727 709 output << "." << tupleExpr->get_index(); 728 710 } 729 711 730 void CodeGenerator:: postvisit( TypeExpr * typeExpr ) {712 void CodeGenerator::visit( TypeExpr * typeExpr ) { 731 713 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl; 732 714 // assertf( ! genC, "TypeExpr should not reach code generation." ); … … 736 718 } 737 719 738 void CodeGenerator:: postvisit( AsmExpr * asmExpr ) {720 void CodeGenerator::visit( AsmExpr * asmExpr ) { 739 721 if ( asmExpr->get_inout() ) { 740 722 output << "[ "; 741 asmExpr->get_inout()->accept( * visitor);723 asmExpr->get_inout()->accept( *this ); 742 724 output << " ] "; 743 725 } // if 744 asmExpr->get_constraint()->accept( * visitor);726 asmExpr->get_constraint()->accept( *this ); 745 727 output << " ( "; 746 asmExpr->get_operand()->accept( * visitor);728 asmExpr->get_operand()->accept( *this ); 747 729 output << " )"; 748 730 } 749 731 750 void CodeGenerator:: postvisit( CompoundLiteralExpr *compLitExpr ) {732 void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) { 751 733 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); 752 734 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 ) { 757 739 assertf( ! genC, "Unique expressions should not reach code generation." ); 758 740 output << "unq<" << unqExpr->get_id() << ">{ "; 759 unqExpr->get_expr()->accept( * visitor);741 unqExpr->get_expr()->accept( *this ); 760 742 output << " }"; 761 743 } 762 744 763 void CodeGenerator:: postvisit( StmtExpr * stmtExpr ) {745 void CodeGenerator::visit( StmtExpr * stmtExpr ) { 764 746 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 765 747 updateLocation( stmtExpr ); … … 775 757 // cannot cast to void, otherwise the expression statement has no value 776 758 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { 777 exprStmt->get_expr()->accept( * visitor);759 exprStmt->get_expr()->accept( *this ); 778 760 output << ";" << endl; 779 761 ++i; … … 781 763 } 782 764 } 783 stmt->accept( * visitor);765 stmt->accept( *this ); 784 766 output << endl; 785 767 if ( wantSpacing( stmt ) ) { … … 792 774 } 793 775 794 void CodeGenerator::postvisit( ConstructorExpr * expr ) {795 assertf( ! genC, "Unique expressions should not reach code generation." );796 expr->callExpr->accept( *visitor );797 }798 799 776 // *** Statements 800 void CodeGenerator:: postvisit( CompoundStmt * compoundStmt ) {777 void CodeGenerator::visit( CompoundStmt * compoundStmt ) { 801 778 std::list<Statement*> ks = compoundStmt->get_kids(); 802 779 output << "{" << endl; … … 806 783 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { 807 784 output << indent << printLabels( (*i)->get_labels() ); 808 (*i)->accept( * visitor);785 (*i)->accept( *this ); 809 786 810 787 output << endl; … … 818 795 } 819 796 820 void CodeGenerator:: postvisit( ExprStmt * exprStmt ) {797 void CodeGenerator::visit( ExprStmt * exprStmt ) { 821 798 assert( exprStmt ); 822 799 if ( genC ) { … … 824 801 exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) ); 825 802 } 826 exprStmt->get_expr()->accept( * visitor);803 exprStmt->get_expr()->accept( *this ); 827 804 output << ";"; 828 805 } 829 806 830 void CodeGenerator:: postvisit( AsmStmt * asmStmt ) {807 void CodeGenerator::visit( AsmStmt * asmStmt ) { 831 808 output << "asm "; 832 809 if ( asmStmt->get_voltile() ) output << "volatile "; 833 810 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; 834 811 output << "( "; 835 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( * visitor);812 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this ); 836 813 output << " : "; 837 814 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); … … 851 828 } 852 829 853 void CodeGenerator:: postvisit( AsmDecl * asmDecl ) {830 void CodeGenerator::visit( AsmDecl * asmDecl ) { 854 831 output << "asm "; 855 832 AsmStmt * asmStmt = asmDecl->get_stmt(); 856 833 output << "( "; 857 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( * visitor);834 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this ); 858 835 output << " )" ; 859 836 } 860 837 861 void CodeGenerator:: postvisit( IfStmt * ifStmt ) {838 void CodeGenerator::visit( IfStmt * ifStmt ) { 862 839 updateLocation( ifStmt ); 863 840 output << "if ( "; 864 ifStmt->get_condition()->accept( * visitor);841 ifStmt->get_condition()->accept( *this ); 865 842 output << " ) "; 866 843 867 ifStmt->get_thenPart()->accept( * visitor);844 ifStmt->get_thenPart()->accept( *this ); 868 845 869 846 if ( ifStmt->get_elsePart() != 0) { 870 847 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 ) { 876 853 updateLocation( switchStmt ); 877 854 output << "switch ( " ; 878 switchStmt->get_condition()->accept( * visitor);855 switchStmt->get_condition()->accept( *this ); 879 856 output << " ) "; 880 857 881 858 output << "{" << std::endl; 882 859 ++indent; 883 acceptAll( switchStmt->get_statements(), * visitor);860 acceptAll( switchStmt->get_statements(), *this ); 884 861 --indent; 885 862 output << indent << "}"; 886 863 } 887 864 888 void CodeGenerator:: postvisit( CaseStmt * caseStmt ) {865 void CodeGenerator::visit( CaseStmt * caseStmt ) { 889 866 updateLocation( caseStmt ); 890 867 if ( caseStmt->isDefault()) { … … 892 869 } else { 893 870 output << "case "; 894 caseStmt->get_condition()->accept( * visitor);871 caseStmt->get_condition()->accept( *this ); 895 872 } // if 896 873 output << ":\n"; … … 901 878 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 902 879 output << indent << printLabels( (*i)->get_labels() ) ; 903 (*i)->accept( * visitor);880 (*i)->accept( *this ); 904 881 output << endl; 905 882 } // for … … 907 884 } 908 885 909 void CodeGenerator:: postvisit( BranchStmt * branchStmt ) {886 void CodeGenerator::visit( BranchStmt * branchStmt ) { 910 887 switch ( branchStmt->get_type()) { 911 888 case BranchStmt::Goto: … … 915 892 if ( branchStmt->get_computedTarget() != 0 ) { 916 893 output << "goto *"; 917 branchStmt->get_computedTarget()->accept( * visitor);894 branchStmt->get_computedTarget()->accept( *this ); 918 895 } // if 919 896 } // if … … 929 906 } 930 907 931 void CodeGenerator:: postvisit( ReturnStmt * returnStmt ) {908 void CodeGenerator::visit( ReturnStmt * returnStmt ) { 932 909 output << "return "; 933 maybeAccept( returnStmt->get_expr(), * visitor);910 maybeAccept( returnStmt->get_expr(), *this ); 934 911 output << ";"; 935 912 } 936 913 937 void CodeGenerator:: postvisit( ThrowStmt * throwStmt ) {914 void CodeGenerator::visit( ThrowStmt * throwStmt ) { 938 915 assertf( ! genC, "Throw statements should not reach code generation." ); 939 916 … … 942 919 if (throwStmt->get_expr()) { 943 920 output << " "; 944 throwStmt->get_expr()->accept( * visitor);921 throwStmt->get_expr()->accept( *this ); 945 922 } 946 923 if (throwStmt->get_target()) { 947 924 output << " _At "; 948 throwStmt->get_target()->accept( * visitor);925 throwStmt->get_target()->accept( *this ); 949 926 } 950 927 output << ";"; 951 928 } 952 929 953 void CodeGenerator:: postvisit( WhileStmt * whileStmt ) {930 void CodeGenerator::visit( WhileStmt * whileStmt ) { 954 931 if ( whileStmt->get_isDoWhile() ) { 955 932 output << "do" ; 956 933 } else { 957 934 output << "while (" ; 958 whileStmt->get_condition()->accept( * visitor);935 whileStmt->get_condition()->accept( *this ); 959 936 output << ")"; 960 937 } // if … … 962 939 963 940 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); 964 whileStmt->get_body()->accept( * visitor);941 whileStmt->get_body()->accept( *this ); 965 942 966 943 output << indent; … … 968 945 if ( whileStmt->get_isDoWhile() ) { 969 946 output << " while (" ; 970 whileStmt->get_condition()->accept( * visitor);947 whileStmt->get_condition()->accept( *this ); 971 948 output << ");"; 972 949 } // if 973 950 } 974 951 975 void CodeGenerator:: postvisit( ForStmt * forStmt ) {952 void CodeGenerator::visit( ForStmt * forStmt ) { 976 953 // initialization is always hoisted, so don't bother doing anything with that 977 954 output << "for (;"; 978 955 979 956 if ( forStmt->get_condition() != 0 ) { 980 forStmt->get_condition()->accept( * visitor);957 forStmt->get_condition()->accept( *this ); 981 958 } // if 982 959 output << ";"; … … 985 962 // cast the top-level expression to void to reduce gcc warnings. 986 963 Expression * expr = new CastExpr( forStmt->get_increment() ); 987 expr->accept( * visitor);964 expr->accept( *this ); 988 965 } // if 989 966 output << ") "; … … 991 968 if ( forStmt->get_body() != 0 ) { 992 969 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 ) { 998 975 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); 999 976 output << "/* null statement */ ;"; 1000 977 } 1001 978 1002 void CodeGenerator:: postvisit( DeclStmt * declStmt ) {1003 declStmt->get_decl()->accept( * visitor);979 void CodeGenerator::visit( DeclStmt * declStmt ) { 980 declStmt->get_decl()->accept( *this ); 1004 981 1005 982 if ( doSemicolon( declStmt->get_decl() ) ) { 1006 983 output << ";"; 1007 984 } // if 1008 }1009 1010 void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) {1011 assertf( ! genC, "ImplicitCtorDtorStmts should not reach code generation." );1012 stmt->callStmt->accept( *visitor );1013 985 } 1014 986
Note:
See TracChangeset
for help on using the changeset viewer.