Changes in src/CodeGen/CodeGenerator.cc [d22e90f:e149f77]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (60 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
rd22e90f re149f77 79 79 } 80 80 81 /* Using updateLocation at the beginning of a node and endl81 /* Using updateLocation at the beginning of a node and nextLine 82 82 * within a node should become the method of formating. 83 83 */ 84 84 void CodeGenerator::updateLocation( CodeLocation const & to ) { 85 // skip if linemarks shouldn't appear or if codelocation is unset 86 if ( !lineMarks || to.isUnset() ) return; 87 88 if ( currentLocation.followedBy( to, 0 ) ) { 85 if ( !lineMarks ) { 86 return; 87 } else if ( currentLocation.followedBy( to, 0 ) ) { 89 88 return; 90 89 } else if ( currentLocation.followedBy( to, 1 ) ) { 91 90 output << "\n" << indent; 92 currentLocation. first_line+= 1;91 currentLocation.linenumber += 1; 93 92 } else if ( currentLocation.followedBy( to, 2 ) ) { 94 93 output << "\n\n" << indent; 95 currentLocation. first_line+= 2;96 } else { 97 output << "\n# " << to. first_line<< " \"" << to.filename94 currentLocation.linenumber += 2; 95 } else { 96 output << "\n# " << to.linenumber << " \"" << to.filename 98 97 << "\"\n" << indent; 99 98 currentLocation = to; … … 106 105 } 107 106 108 // replace endl 109 ostream & CodeGenerator::LineEnder::operator()( ostream & os ) const { 110 // if ( !cg.lineMarks ) { 111 // os << "\n" << cg.indent << std::flush; 112 // } 113 os << "\n" << std::flush; 114 cg.currentLocation.first_line++; 115 // os << "/* did endl; current loc is: " << cg.currentLocation.first_line << "*/"; 116 return os; 117 } 118 119 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ), endl( *this ) {} 107 void CodeGenerator::nextLine() { 108 if ( !lineMarks ) { 109 output << "\n" << indent << std::flush; 110 } 111 } 112 113 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {} 120 114 121 115 string CodeGenerator::mangleName( DeclarationWithType * decl ) { … … 145 139 } // CodeGenerator::genAttributes 146 140 147 // *** BaseSyntaxNode148 void CodeGenerator::previsit( BaseSyntaxNode * node ) {149 // turn off automatic recursion for all nodes, to allow each visitor to150 // precisely control the order in which its children are visited.151 visit_children = false;152 updateLocation( node );153 }154 155 // *** BaseSyntaxNode156 void CodeGenerator::postvisit( BaseSyntaxNode * node ) {157 std::stringstream ss;158 node->print( ss );159 assertf( false, "Unhandled node reached in CodeGenerator: %s", ss.str().c_str() );160 }161 141 162 142 // *** Declarations 163 void CodeGenerator:: postvisit( FunctionDecl * functionDecl ) {143 void CodeGenerator::visit( FunctionDecl * functionDecl ) { 164 144 extension( functionDecl ); 165 145 genAttributes( functionDecl->get_attributes() ); … … 172 152 asmName( functionDecl ); 173 153 154 // acceptAll( functionDecl->get_oldDecls(), *this ); 174 155 if ( functionDecl->get_statements() ) { 175 functionDecl->get_statements()->accept( * visitor);176 } // if 177 } 178 179 void CodeGenerator:: postvisit( ObjectDecl * objectDecl ) {156 functionDecl->get_statements()->accept( *this ); 157 } // if 158 } 159 160 void CodeGenerator::visit( ObjectDecl * objectDecl ) { 180 161 if (objectDecl->get_name().empty() && genC ) { 181 162 // only generate an anonymous name when generating C code, otherwise it clutters the output too much … … 194 175 if ( objectDecl->get_init() ) { 195 176 output << " = "; 196 objectDecl->get_init()->accept( * visitor);177 objectDecl->get_init()->accept( *this ); 197 178 } // if 198 179 199 180 if ( objectDecl->get_bitfieldWidth() ) { 200 181 output << ":"; 201 objectDecl->get_bitfieldWidth()->accept( * visitor);182 objectDecl->get_bitfieldWidth()->accept( *this ); 202 183 } // if 203 184 } … … 222 203 ++indent; 223 204 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { 205 updateLocation( *i ); 224 206 output << indent; 225 (*i)->accept( * visitor);207 (*i)->accept( *this ); 226 208 output << ";" << endl; 227 209 } // for … … 233 215 } 234 216 235 void CodeGenerator:: postvisit( StructDecl * structDecl ) {217 void CodeGenerator::visit( StructDecl * structDecl ) { 236 218 extension( structDecl ); 237 219 handleAggregate( structDecl, "struct " ); 238 220 } 239 221 240 void CodeGenerator:: postvisit( UnionDecl * unionDecl ) {222 void CodeGenerator::visit( UnionDecl * unionDecl ) { 241 223 extension( unionDecl ); 242 224 handleAggregate( unionDecl, "union " ); 243 225 } 244 226 245 void CodeGenerator:: postvisit( EnumDecl * enumDecl ) {227 void CodeGenerator::visit( EnumDecl * enumDecl ) { 246 228 extension( enumDecl ); 229 updateLocation( enumDecl ); 247 230 output << "enum "; 248 231 genAttributes( enumDecl->get_attributes() ); … … 259 242 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); 260 243 assert( obj ); 244 updateLocation( obj ); 261 245 output << indent << mangleName( obj ); 262 246 if ( obj->get_init() ) { 263 247 output << " = "; 264 obj->get_init()->accept( * visitor);248 obj->get_init()->accept( *this ); 265 249 } // if 266 250 output << "," << endl; … … 273 257 } 274 258 275 void CodeGenerator:: postvisit( TraitDecl * traitDecl ) {259 void CodeGenerator::visit( TraitDecl * traitDecl ) { 276 260 assertf( ! genC, "TraitDecls should not reach code generation." ); 277 261 extension( traitDecl ); … … 279 263 } 280 264 281 void CodeGenerator:: postvisit( TypedefDecl * typeDecl ) {265 void CodeGenerator::visit( TypedefDecl * typeDecl ) { 282 266 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); 267 updateLocation( typeDecl ); 283 268 output << "typedef "; 284 269 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl; 285 270 } 286 271 287 void CodeGenerator:: postvisit( TypeDecl * typeDecl ) {272 void CodeGenerator::visit( TypeDecl * typeDecl ) { 288 273 assertf( ! genC, "TypeDecls should not reach code generation." ); 289 274 output << typeDecl->genTypeString() << " " << typeDecl->get_name(); … … 298 283 } 299 284 300 void CodeGenerator:: postvisit( Designation * designation ) {285 void CodeGenerator::visit( Designation * designation ) { 301 286 std::list< Expression * > designators = designation->get_designators(); 302 287 if ( designators.size() == 0 ) return; … … 305 290 // if expression is a NameExpr or VariableExpr, then initializing aggregate member 306 291 output << "."; 307 des->accept( * visitor);292 des->accept( *this ); 308 293 } else { 309 294 // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt 310 295 output << "["; 311 des->accept( * visitor);296 des->accept( *this ); 312 297 output << "]"; 313 298 } // if … … 316 301 } 317 302 318 void CodeGenerator:: postvisit( SingleInit * init ) {319 init->get_value()->accept( * visitor);320 } 321 322 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 ) { 323 308 auto initBegin = init->begin(); 324 309 auto initEnd = init->end(); … … 328 313 output << "{ "; 329 314 for ( ; initBegin != initEnd && desigBegin != desigEnd; ) { 330 (*desigBegin)->accept( * visitor);331 (*initBegin)->accept( * visitor);315 (*desigBegin)->accept( *this ); 316 (*initBegin)->accept( *this ); 332 317 ++initBegin, ++desigBegin; 333 318 if ( initBegin != initEnd ) { … … 339 324 } 340 325 341 void CodeGenerator:: postvisit(ConstructorInit * init ){326 void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){ 342 327 assertf( ! genC, "ConstructorInit nodes should not reach code generation." ); 343 328 // pseudo-output for constructor/destructor pairs 344 output << "<ctorinit>{" << endl << ++indent << "ctor: ";345 maybeAccept( init->get_ctor(), * visitor);346 output << ", " << endl << indent << "dtor: ";347 maybeAccept( init->get_dtor(), * visitor);348 output << endl << --indent << "}";349 } 350 351 void CodeGenerator:: postvisit( Constant * constant ) {329 output << "<ctorinit>{" << std::endl << ++indent << "ctor: "; 330 maybeAccept( init->get_ctor(), *this ); 331 output << ", " << std::endl << indent << "dtor: "; 332 maybeAccept( init->get_dtor(), *this ); 333 output << std::endl << --indent << "}"; 334 } 335 336 void CodeGenerator::visit( Constant * constant ) { 352 337 output << constant->get_value() ; 353 338 } 354 339 355 340 // *** Expressions 356 void CodeGenerator:: postvisit( ApplicationExpr * applicationExpr ) {341 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) { 357 342 extension( applicationExpr ); 358 343 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { … … 363 348 case OT_INDEX: 364 349 assert( applicationExpr->get_args().size() == 2 ); 365 (*arg++)->accept( * visitor);350 (*arg++)->accept( *this ); 366 351 output << "["; 367 (*arg)->accept( * visitor);352 (*arg)->accept( *this ); 368 353 output << "]"; 369 354 break; … … 380 365 // effects, so must still output this expression 381 366 output << "("; 382 (*arg++)->accept( * visitor);367 (*arg++)->accept( *this ); 383 368 output << ") /* " << opInfo.inputName << " */"; 384 369 } else if ( applicationExpr->get_args().size() == 2 ) { 385 370 // intrinsic two parameter constructors are essentially bitwise assignment 386 371 output << "("; 387 (*arg++)->accept( * visitor);372 (*arg++)->accept( *this ); 388 373 output << opInfo.symbol; 389 (*arg)->accept( * visitor);374 (*arg)->accept( *this ); 390 375 output << ") /* " << opInfo.inputName << " */"; 391 376 } else { … … 400 385 output << "("; 401 386 output << opInfo.symbol; 402 (*arg)->accept( * visitor);387 (*arg)->accept( *this ); 403 388 output << ")"; 404 389 break; … … 407 392 case OT_POSTFIXASSIGN: 408 393 assert( applicationExpr->get_args().size() == 1 ); 409 (*arg)->accept( * visitor);394 (*arg)->accept( *this ); 410 395 output << opInfo.symbol; 411 396 break; … … 416 401 assert( applicationExpr->get_args().size() == 2 ); 417 402 output << "("; 418 (*arg++)->accept( * visitor);403 (*arg++)->accept( *this ); 419 404 output << opInfo.symbol; 420 (*arg)->accept( * visitor);405 (*arg)->accept( *this ); 421 406 output << ")"; 422 407 break; … … 428 413 } // switch 429 414 } else { 430 varExpr->accept( * visitor);415 varExpr->accept( *this ); 431 416 output << "("; 432 417 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); … … 434 419 } // if 435 420 } else { 436 applicationExpr->get_function()->accept( * visitor);421 applicationExpr->get_function()->accept( *this ); 437 422 output << "("; 438 423 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); … … 441 426 } 442 427 443 void CodeGenerator:: postvisit( UntypedExpr * untypedExpr ) {428 void CodeGenerator::visit( UntypedExpr * untypedExpr ) { 444 429 extension( untypedExpr ); 445 430 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { … … 450 435 case OT_INDEX: 451 436 assert( untypedExpr->get_args().size() == 2 ); 452 (*arg++)->accept( * visitor);437 (*arg++)->accept( *this ); 453 438 output << "["; 454 (*arg)->accept( * visitor);439 (*arg)->accept( *this ); 455 440 output << "]"; 456 441 break; … … 465 450 // effects, so must still output this expression 466 451 output << "("; 467 (*arg++)->accept( * visitor);452 (*arg++)->accept( *this ); 468 453 output << ") /* " << opInfo.inputName << " */"; 469 454 } else if ( untypedExpr->get_args().size() == 2 ) { 470 455 // intrinsic two parameter constructors are essentially bitwise assignment 471 456 output << "("; 472 (*arg++)->accept( * visitor);457 (*arg++)->accept( *this ); 473 458 output << opInfo.symbol; 474 (*arg)->accept( * visitor);459 (*arg)->accept( *this ); 475 460 output << ") /* " << opInfo.inputName << " */"; 476 461 } else { 477 462 // no constructors with 0 or more than 2 parameters 478 assertf( ! genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." ); 479 output << "("; 480 (*arg++)->accept( *visitor ); 481 output << opInfo.symbol << "{ "; 482 genCommaList( arg, untypedExpr->get_args().end() ); 483 output << "}) /* " << opInfo.inputName << " */"; 463 assert( false ); 484 464 } // if 485 465 break; … … 491 471 output << "("; 492 472 output << opInfo.symbol; 493 (*arg)->accept( * visitor);473 (*arg)->accept( *this ); 494 474 output << ")"; 495 475 break; … … 498 478 case OT_POSTFIXASSIGN: 499 479 assert( untypedExpr->get_args().size() == 1 ); 500 (*arg)->accept( * visitor);480 (*arg)->accept( *this ); 501 481 output << opInfo.symbol; 502 482 break; … … 506 486 assert( untypedExpr->get_args().size() == 2 ); 507 487 output << "("; 508 (*arg++)->accept( * visitor);488 (*arg++)->accept( *this ); 509 489 output << opInfo.symbol; 510 (*arg)->accept( * visitor);490 (*arg)->accept( *this ); 511 491 output << ")"; 512 492 break; … … 519 499 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2 520 500 assert( untypedExpr->get_args().size() == 2 ); 521 (*untypedExpr->get_args().begin())->accept( * visitor);501 (*untypedExpr->get_args().begin())->accept( *this ); 522 502 output << " ... "; 523 (*--untypedExpr->get_args().end())->accept( * visitor);503 (*--untypedExpr->get_args().end())->accept( *this ); 524 504 } else { // builtin routines 525 nameExpr->accept( * visitor);505 nameExpr->accept( *this ); 526 506 output << "("; 527 507 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); … … 530 510 } // if 531 511 } else { 532 untypedExpr->get_function()->accept( * visitor);512 untypedExpr->get_function()->accept( *this ); 533 513 output << "("; 534 514 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); … … 537 517 } 538 518 539 void CodeGenerator:: postvisit( RangeExpr * rangeExpr ) {540 rangeExpr->get_low()->accept( * visitor);519 void CodeGenerator::visit( RangeExpr * rangeExpr ) { 520 rangeExpr->get_low()->accept( *this ); 541 521 output << " ... "; 542 rangeExpr->get_high()->accept( * visitor);543 } 544 545 void CodeGenerator:: postvisit( NameExpr * nameExpr ) {522 rangeExpr->get_high()->accept( *this ); 523 } 524 525 void CodeGenerator::visit( NameExpr * nameExpr ) { 546 526 extension( nameExpr ); 547 527 OperatorInfo opInfo; … … 554 534 } 555 535 556 void CodeGenerator:: postvisit( AddressExpr * addressExpr ) {536 void CodeGenerator::visit( AddressExpr * addressExpr ) { 557 537 extension( addressExpr ); 558 538 output << "(&"; 559 addressExpr->arg->accept( * visitor);539 addressExpr->arg->accept( *this ); 560 540 output << ")"; 561 541 } 562 542 563 void CodeGenerator:: postvisit( LabelAddressExpr *addressExpr ) {543 void CodeGenerator::visit( LabelAddressExpr *addressExpr ) { 564 544 extension( addressExpr ); 565 545 output << "(&&" << addressExpr->arg << ")"; 566 546 } 567 547 568 void CodeGenerator:: postvisit( CastExpr * castExpr ) {548 void CodeGenerator::visit( CastExpr * castExpr ) { 569 549 extension( castExpr ); 570 550 output << "("; … … 579 559 output << ")"; 580 560 } // if 581 castExpr->get_arg()->accept( * visitor);561 castExpr->get_arg()->accept( *this ); 582 562 output << ")"; 583 563 } 584 564 585 void CodeGenerator:: postvisit( VirtualCastExpr * castExpr ) {565 void CodeGenerator::visit( VirtualCastExpr * castExpr ) { 586 566 assertf( ! genC, "VirtualCastExpr should not reach code generation." ); 587 567 extension( castExpr ); 588 568 output << "(virtual "; 589 castExpr->get_arg()->accept( * visitor);569 castExpr->get_arg()->accept( *this ); 590 570 output << ")"; 591 571 } 592 572 593 void CodeGenerator:: postvisit( UntypedMemberExpr * memberExpr ) {573 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) { 594 574 assertf( ! genC, "UntypedMemberExpr should not reach code generation." ); 595 575 extension( memberExpr ); 596 memberExpr->get_aggregate()->accept( * visitor);576 memberExpr->get_aggregate()->accept( *this ); 597 577 output << "."; 598 memberExpr->get_member()->accept( * visitor);599 } 600 601 void CodeGenerator:: postvisit( MemberExpr * memberExpr ) {578 memberExpr->get_member()->accept( *this ); 579 } 580 581 void CodeGenerator::visit( MemberExpr * memberExpr ) { 602 582 extension( memberExpr ); 603 memberExpr->get_aggregate()->accept( * visitor);583 memberExpr->get_aggregate()->accept( *this ); 604 584 output << "." << mangleName( memberExpr->get_member() ); 605 585 } 606 586 607 void CodeGenerator:: postvisit( VariableExpr * variableExpr ) {587 void CodeGenerator::visit( VariableExpr * variableExpr ) { 608 588 extension( variableExpr ); 609 589 OperatorInfo opInfo; … … 615 595 } 616 596 617 void CodeGenerator:: postvisit( ConstantExpr * constantExpr ) {597 void CodeGenerator::visit( ConstantExpr * constantExpr ) { 618 598 assert( constantExpr->get_constant() ); 619 599 extension( constantExpr ); 620 constantExpr->get_constant()->accept( * visitor);621 } 622 623 void CodeGenerator:: postvisit( SizeofExpr * sizeofExpr ) {600 constantExpr->get_constant()->accept( *this ); 601 } 602 603 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) { 624 604 extension( sizeofExpr ); 625 605 output << "sizeof("; … … 627 607 output << genType( sizeofExpr->get_type(), "", pretty, genC ); 628 608 } else { 629 sizeofExpr->get_expr()->accept( * visitor);609 sizeofExpr->get_expr()->accept( *this ); 630 610 } // if 631 611 output << ")"; 632 612 } 633 613 634 void CodeGenerator:: postvisit( AlignofExpr * alignofExpr ) {614 void CodeGenerator::visit( AlignofExpr * alignofExpr ) { 635 615 // use GCC extension to avoid bumping std to C11 636 616 extension( alignofExpr ); … … 639 619 output << genType( alignofExpr->get_type(), "", pretty, genC ); 640 620 } else { 641 alignofExpr->get_expr()->accept( * visitor);621 alignofExpr->get_expr()->accept( *this ); 642 622 } // if 643 623 output << ")"; 644 624 } 645 625 646 void CodeGenerator:: postvisit( UntypedOffsetofExpr * offsetofExpr ) {626 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) { 647 627 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." ); 648 628 output << "offsetof("; … … 652 632 } 653 633 654 void CodeGenerator:: postvisit( OffsetofExpr * offsetofExpr ) {634 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) { 655 635 // use GCC builtin 656 636 output << "__builtin_offsetof("; … … 660 640 } 661 641 662 void CodeGenerator:: postvisit( OffsetPackExpr * offsetPackExpr ) {642 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) { 663 643 assertf( ! genC, "OffsetPackExpr should not reach code generation." ); 664 644 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")"; 665 645 } 666 646 667 void CodeGenerator:: postvisit( LogicalExpr * logicalExpr ) {647 void CodeGenerator::visit( LogicalExpr * logicalExpr ) { 668 648 extension( logicalExpr ); 669 649 output << "("; 670 logicalExpr->get_arg1()->accept( * visitor);650 logicalExpr->get_arg1()->accept( *this ); 671 651 if ( logicalExpr->get_isAnd() ) { 672 652 output << " && "; … … 674 654 output << " || "; 675 655 } // if 676 logicalExpr->get_arg2()->accept( * visitor);656 logicalExpr->get_arg2()->accept( *this ); 677 657 output << ")"; 678 658 } 679 659 680 void CodeGenerator:: postvisit( ConditionalExpr * conditionalExpr ) {660 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) { 681 661 extension( conditionalExpr ); 682 662 output << "("; 683 conditionalExpr->get_arg1()->accept( * visitor);663 conditionalExpr->get_arg1()->accept( *this ); 684 664 output << " ? "; 685 conditionalExpr->get_arg2()->accept( * visitor);665 conditionalExpr->get_arg2()->accept( *this ); 686 666 output << " : "; 687 conditionalExpr->get_arg3()->accept( * visitor);667 conditionalExpr->get_arg3()->accept( *this ); 688 668 output << ")"; 689 669 } 690 670 691 void CodeGenerator:: postvisit( CommaExpr * commaExpr ) {671 void CodeGenerator::visit( CommaExpr * commaExpr ) { 692 672 extension( commaExpr ); 693 673 output << "("; … … 696 676 commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) ); 697 677 } 698 commaExpr->get_arg1()->accept( * visitor);678 commaExpr->get_arg1()->accept( *this ); 699 679 output << " , "; 700 commaExpr->get_arg2()->accept( * visitor);680 commaExpr->get_arg2()->accept( *this ); 701 681 output << ")"; 702 682 } 703 683 704 void CodeGenerator:: postvisit( TupleAssignExpr * tupleExpr ) {684 void CodeGenerator::visit( TupleAssignExpr * tupleExpr ) { 705 685 assertf( ! genC, "TupleAssignExpr should not reach code generation." ); 706 tupleExpr->stmtExpr->accept( * visitor);707 } 708 709 void CodeGenerator:: postvisit( UntypedTupleExpr * tupleExpr ) {686 tupleExpr->stmtExpr->accept( *this ); 687 } 688 689 void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) { 710 690 assertf( ! genC, "UntypedTupleExpr should not reach code generation." ); 711 691 extension( tupleExpr ); … … 715 695 } 716 696 717 void CodeGenerator:: postvisit( TupleExpr * tupleExpr ) {697 void CodeGenerator::visit( TupleExpr * tupleExpr ) { 718 698 assertf( ! genC, "TupleExpr should not reach code generation." ); 719 699 extension( tupleExpr ); … … 723 703 } 724 704 725 void CodeGenerator:: postvisit( TupleIndexExpr * tupleExpr ) {705 void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) { 726 706 assertf( ! genC, "TupleIndexExpr should not reach code generation." ); 727 707 extension( tupleExpr ); 728 tupleExpr->get_tuple()->accept( * visitor);708 tupleExpr->get_tuple()->accept( *this ); 729 709 output << "." << tupleExpr->get_index(); 730 710 } 731 711 732 void CodeGenerator:: postvisit( TypeExpr * typeExpr ) {712 void CodeGenerator::visit( TypeExpr * typeExpr ) { 733 713 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl; 734 714 // assertf( ! genC, "TypeExpr should not reach code generation." ); … … 738 718 } 739 719 740 void CodeGenerator:: postvisit( AsmExpr * asmExpr ) {720 void CodeGenerator::visit( AsmExpr * asmExpr ) { 741 721 if ( asmExpr->get_inout() ) { 742 722 output << "[ "; 743 asmExpr->get_inout()->accept( * visitor);723 asmExpr->get_inout()->accept( *this ); 744 724 output << " ] "; 745 725 } // if 746 asmExpr->get_constraint()->accept( * visitor);726 asmExpr->get_constraint()->accept( *this ); 747 727 output << " ( "; 748 asmExpr->get_operand()->accept( * visitor);728 asmExpr->get_operand()->accept( *this ); 749 729 output << " )"; 750 730 } 751 731 752 void CodeGenerator:: postvisit( CompoundLiteralExpr *compLitExpr ) {732 void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) { 753 733 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); 754 734 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")"; 755 compLitExpr->get_initializer()->accept( * visitor);756 } 757 758 void CodeGenerator:: postvisit( UniqueExpr * unqExpr ) {735 compLitExpr->get_initializer()->accept( *this ); 736 } 737 738 void CodeGenerator::visit( UniqueExpr * unqExpr ) { 759 739 assertf( ! genC, "Unique expressions should not reach code generation." ); 760 740 output << "unq<" << unqExpr->get_id() << ">{ "; 761 unqExpr->get_expr()->accept( * visitor);741 unqExpr->get_expr()->accept( *this ); 762 742 output << " }"; 763 743 } 764 744 765 void CodeGenerator:: postvisit( StmtExpr * stmtExpr ) {745 void CodeGenerator::visit( StmtExpr * stmtExpr ) { 766 746 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 767 output << "({" << endl; 747 updateLocation( stmtExpr ); 748 output << "({" << std::endl; 768 749 ++indent; 769 750 unsigned int numStmts = stmts.size(); 770 751 unsigned int i = 0; 771 752 for ( Statement * stmt : stmts ) { 753 updateLocation( stmt ); 772 754 output << indent << printLabels( stmt->get_labels() ); 773 755 if ( i+1 == numStmts ) { … … 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 ) { 839 updateLocation( ifStmt ); 862 840 output << "if ( "; 863 ifStmt->get_condition()->accept( * visitor);841 ifStmt->get_condition()->accept( *this ); 864 842 output << " ) "; 865 843 866 ifStmt->get_thenPart()->accept( * visitor);844 ifStmt->get_thenPart()->accept( *this ); 867 845 868 846 if ( ifStmt->get_elsePart() != 0) { 869 847 output << " else "; 870 ifStmt->get_elsePart()->accept( *visitor ); 871 } // if 872 } 873 874 void CodeGenerator::postvisit( SwitchStmt * switchStmt ) { 848 ifStmt->get_elsePart()->accept( *this ); 849 } // if 850 } 851 852 void CodeGenerator::visit( SwitchStmt * switchStmt ) { 853 updateLocation( switchStmt ); 875 854 output << "switch ( " ; 876 switchStmt->get_condition()->accept( * visitor);855 switchStmt->get_condition()->accept( *this ); 877 856 output << " ) "; 878 857 879 output << "{" << endl;858 output << "{" << std::endl; 880 859 ++indent; 881 acceptAll( switchStmt->get_statements(), * visitor);860 acceptAll( switchStmt->get_statements(), *this ); 882 861 --indent; 883 862 output << indent << "}"; 884 863 } 885 864 886 void CodeGenerator::postvisit( CaseStmt * caseStmt ) { 865 void CodeGenerator::visit( CaseStmt * caseStmt ) { 866 updateLocation( caseStmt ); 867 output << indent; 887 868 if ( caseStmt->isDefault()) { 888 869 output << "default"; 889 870 } else { 890 871 output << "case "; 891 caseStmt->get_condition()->accept( * visitor);892 } // if 893 output << ": " << endl;872 caseStmt->get_condition()->accept( *this ); 873 } // if 874 output << ":\n"; 894 875 895 876 std::list<Statement *> sts = caseStmt->get_statements(); … … 898 879 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 899 880 output << indent << printLabels( (*i)->get_labels() ) ; 900 (*i)->accept( * visitor);881 (*i)->accept( *this ); 901 882 output << endl; 902 883 } // for … … 904 885 } 905 886 906 void CodeGenerator:: postvisit( BranchStmt * branchStmt ) {887 void CodeGenerator::visit( BranchStmt * branchStmt ) { 907 888 switch ( branchStmt->get_type()) { 908 889 case BranchStmt::Goto: … … 912 893 if ( branchStmt->get_computedTarget() != 0 ) { 913 894 output << "goto *"; 914 branchStmt->get_computedTarget()->accept( * visitor);895 branchStmt->get_computedTarget()->accept( *this ); 915 896 } // if 916 897 } // if … … 926 907 } 927 908 928 void CodeGenerator:: postvisit( ReturnStmt * returnStmt ) {909 void CodeGenerator::visit( ReturnStmt * returnStmt ) { 929 910 output << "return "; 930 maybeAccept( returnStmt->get_expr(), * visitor);911 maybeAccept( returnStmt->get_expr(), *this ); 931 912 output << ";"; 932 913 } 933 914 934 void CodeGenerator:: postvisit( ThrowStmt * throwStmt ) {915 void CodeGenerator::visit( ThrowStmt * throwStmt ) { 935 916 assertf( ! genC, "Throw statements should not reach code generation." ); 936 917 … … 939 920 if (throwStmt->get_expr()) { 940 921 output << " "; 941 throwStmt->get_expr()->accept( * visitor);922 throwStmt->get_expr()->accept( *this ); 942 923 } 943 924 if (throwStmt->get_target()) { 944 925 output << " _At "; 945 throwStmt->get_target()->accept( * visitor);926 throwStmt->get_target()->accept( *this ); 946 927 } 947 928 output << ";"; 948 929 } 949 930 950 void CodeGenerator:: postvisit( WhileStmt * whileStmt ) {931 void CodeGenerator::visit( WhileStmt * whileStmt ) { 951 932 if ( whileStmt->get_isDoWhile() ) { 952 933 output << "do" ; 953 934 } else { 954 935 output << "while (" ; 955 whileStmt->get_condition()->accept( * visitor);936 whileStmt->get_condition()->accept( *this ); 956 937 output << ")"; 957 938 } // if … … 959 940 960 941 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); 961 whileStmt->get_body()->accept( * visitor);942 whileStmt->get_body()->accept( *this ); 962 943 963 944 output << indent; … … 965 946 if ( whileStmt->get_isDoWhile() ) { 966 947 output << " while (" ; 967 whileStmt->get_condition()->accept( * visitor);948 whileStmt->get_condition()->accept( *this ); 968 949 output << ");"; 969 950 } // if 970 951 } 971 952 972 void CodeGenerator:: postvisit( ForStmt * forStmt ) {953 void CodeGenerator::visit( ForStmt * forStmt ) { 973 954 // initialization is always hoisted, so don't bother doing anything with that 974 955 output << "for (;"; 975 956 976 957 if ( forStmt->get_condition() != 0 ) { 977 forStmt->get_condition()->accept( * visitor);958 forStmt->get_condition()->accept( *this ); 978 959 } // if 979 960 output << ";"; … … 982 963 // cast the top-level expression to void to reduce gcc warnings. 983 964 Expression * expr = new CastExpr( forStmt->get_increment() ); 984 expr->accept( * visitor);965 expr->accept( *this ); 985 966 } // if 986 967 output << ") "; … … 988 969 if ( forStmt->get_body() != 0 ) { 989 970 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); 990 forStmt->get_body()->accept( * visitor);991 } // if 992 } 993 994 void CodeGenerator:: postvisit( __attribute__((unused)) NullStmt * nullStmt ) {971 forStmt->get_body()->accept( *this ); 972 } // if 973 } 974 975 void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) { 995 976 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); 996 977 output << "/* null statement */ ;"; 997 978 } 998 979 999 void CodeGenerator:: postvisit( DeclStmt * declStmt ) {1000 declStmt->get_decl()->accept( * visitor);980 void CodeGenerator::visit( DeclStmt * declStmt ) { 981 declStmt->get_decl()->accept( *this ); 1001 982 1002 983 if ( doSemicolon( declStmt->get_decl() ) ) { 1003 984 output << ";"; 1004 985 } // if 1005 }1006 1007 void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) {1008 assertf( ! genC, "ImplicitCtorDtorStmts should not reach code generation." );1009 stmt->callStmt->accept( *visitor );1010 986 } 1011 987 … … 1025 1001 } 1026 1002 } // namespace CodeGen 1003 1004 std::ostream & operator<<( std::ostream & out, const BaseSyntaxNode * node ) { 1005 if ( node ) { 1006 node->print( out ); 1007 } else { 1008 out << "nullptr"; 1009 } 1010 return out; 1011 } 1027 1012 1028 1013 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.