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