Changes in src/SymTab/Validate.cc [e3e16bc:522363e]
- File:
-
- 1 edited
-
src/SymTab/Validate.cc (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Validate.cc
re3e16bc r522363e 123 123 124 124 /// Associates forward declarations of aggregates with their definitions 125 class LinkReferenceToTypes final : public Indexer { 126 typedef Indexer Parent; 127 public: 128 LinkReferenceToTypes( bool doDebug, const Indexer *indexer ); 129 using Parent::visit; 130 void visit( TypeInstType *typeInst ) final; 131 132 void visit( EnumInstType *enumInst ) final; 133 void visit( StructInstType *structInst ) final; 134 void visit( UnionInstType *unionInst ) final; 135 void visit( TraitInstType *traitInst ) final; 136 137 void visit( EnumDecl *enumDecl ) final; 138 void visit( StructDecl *structDecl ) final; 139 void visit( UnionDecl *unionDecl ) final; 140 void visit( TraitDecl * traitDecl ) final; 125 struct LinkReferenceToTypes final : public WithIndexer { 126 LinkReferenceToTypes( const Indexer *indexer ); 127 void postvisit( TypeInstType *typeInst ); 128 129 void postvisit( EnumInstType *enumInst ); 130 void postvisit( StructInstType *structInst ); 131 void postvisit( UnionInstType *unionInst ); 132 void postvisit( TraitInstType *traitInst ); 133 134 void postvisit( EnumDecl *enumDecl ); 135 void postvisit( StructDecl *structDecl ); 136 void postvisit( UnionDecl *unionDecl ); 137 void postvisit( TraitDecl * traitDecl ); 141 138 142 139 private: 143 const Indexer * indexer;140 const Indexer *local_indexer; 144 141 145 142 typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType; … … 152 149 153 150 /// Replaces array and function types in forall lists by appropriate pointer type and assigns each Object and Function declaration a unique ID. 154 class ForallPointerDecay final : public Indexer { 155 typedef Indexer Parent; 156 public: 157 using Parent::visit; 158 ForallPointerDecay( const Indexer *indexer ); 159 160 virtual void visit( ObjectDecl *object ) override; 161 virtual void visit( FunctionDecl *func ) override; 162 163 const Indexer *indexer; 151 struct ForallPointerDecay final { 152 void previsit( ObjectDecl *object ); 153 void previsit( FunctionDecl *func ); 164 154 }; 165 155 … … 176 166 }; 177 167 178 class EliminateTypedef : public Mutator { 179 public: 168 struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards { 180 169 EliminateTypedef() : scopeLevel( 0 ) {} 181 170 /// Replaces typedefs by forward declarations 182 171 static void eliminateTypedef( std::list< Declaration * > &translationUnit ); 172 173 Type * postmutate( TypeInstType * aggregateUseType ); 174 Declaration * postmutate( TypedefDecl * typeDecl ); 175 void premutate( TypeDecl * typeDecl ); 176 void premutate( FunctionDecl * funcDecl ); 177 void premutate( ObjectDecl * objDecl ); 178 DeclarationWithType * postmutate( ObjectDecl * objDecl ); 179 180 void premutate( CastExpr * castExpr ); 181 182 void premutate( CompoundStmt * compoundStmt ); 183 CompoundStmt * postmutate( CompoundStmt * compoundStmt ); 184 185 void premutate( StructDecl * structDecl ); 186 Declaration * postmutate( StructDecl * structDecl ); 187 void premutate( UnionDecl * unionDecl ); 188 Declaration * postmutate( UnionDecl * unionDecl ); 189 void premutate( EnumDecl * enumDecl ); 190 Declaration * postmutate( EnumDecl * enumDecl ); 191 Declaration * postmutate( TraitDecl * contextDecl ); 192 183 193 private: 184 virtual Declaration *mutate( TypedefDecl *typeDecl );185 virtual TypeDecl *mutate( TypeDecl *typeDecl );186 virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );187 virtual DeclarationWithType *mutate( ObjectDecl *objDecl );188 virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );189 virtual Type *mutate( TypeInstType *aggregateUseType );190 virtual Expression *mutate( CastExpr *castExpr );191 192 virtual Declaration *mutate( StructDecl * structDecl );193 virtual Declaration *mutate( UnionDecl * unionDecl );194 virtual Declaration *mutate( EnumDecl * enumDecl );195 virtual Declaration *mutate( TraitDecl * contextDecl );196 197 194 template<typename AggDecl> 198 195 AggDecl *handleAggregate( AggDecl * aggDecl ); … … 256 253 }; 257 254 258 void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {255 void validate( std::list< Declaration * > &translationUnit, __attribute__((unused)) bool doDebug ) { 259 256 PassVisitor<EnumAndPointerDecay> epc; 260 LinkReferenceToTypes lrt( doDebug, 0);261 ForallPointerDecay fpd( 0 );257 PassVisitor<LinkReferenceToTypes> lrt( nullptr ); 258 PassVisitor<ForallPointerDecay> fpd; 262 259 PassVisitor<CompoundLiteral> compoundliteral; 263 260 PassVisitor<ValidateGenericParameters> genericParams; … … 286 283 void validateType( Type *type, const Indexer *indexer ) { 287 284 PassVisitor<EnumAndPointerDecay> epc; 288 LinkReferenceToTypes lrt( false,indexer );289 ForallPointerDecay fpd( indexer );285 PassVisitor<LinkReferenceToTypes> lrt( indexer ); 286 PassVisitor<ForallPointerDecay> fpd; 290 287 type->accept( epc ); 291 288 type->accept( lrt ); … … 401 398 } 402 399 403 LinkReferenceToTypes::LinkReferenceToTypes( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug) {400 LinkReferenceToTypes::LinkReferenceToTypes( const Indexer *other_indexer ) { 404 401 if ( other_indexer ) { 405 indexer = other_indexer;402 local_indexer = other_indexer; 406 403 } else { 407 indexer = this; 408 } // if 409 } 410 411 void LinkReferenceToTypes::visit( EnumInstType *enumInst ) { 412 Parent::visit( enumInst ); 413 EnumDecl *st = indexer->lookupEnum( enumInst->get_name() ); 404 local_indexer = &indexer; 405 } // if 406 } 407 408 void LinkReferenceToTypes::postvisit( EnumInstType *enumInst ) { 409 EnumDecl *st = local_indexer->lookupEnum( enumInst->get_name() ); 414 410 // it's not a semantic error if the enum is not found, just an implicit forward declaration 415 411 if ( st ) { … … 423 419 } 424 420 425 void LinkReferenceToTypes::visit( StructInstType *structInst ) { 426 Parent::visit( structInst ); 427 StructDecl *st = indexer->lookupStruct( structInst->get_name() ); 421 void LinkReferenceToTypes::postvisit( StructInstType *structInst ) { 422 StructDecl *st = local_indexer->lookupStruct( structInst->get_name() ); 428 423 // it's not a semantic error if the struct is not found, just an implicit forward declaration 429 424 if ( st ) { … … 437 432 } 438 433 439 void LinkReferenceToTypes::visit( UnionInstType *unionInst ) { 440 Parent::visit( unionInst ); 441 UnionDecl *un = indexer->lookupUnion( unionInst->get_name() ); 434 void LinkReferenceToTypes::postvisit( UnionInstType *unionInst ) { 435 UnionDecl *un = local_indexer->lookupUnion( unionInst->get_name() ); 442 436 // it's not a semantic error if the union is not found, just an implicit forward declaration 443 437 if ( un ) { … … 492 486 } 493 487 494 void LinkReferenceToTypes::visit( TraitDecl * traitDecl ) { 495 Parent::visit( traitDecl ); 496 488 void LinkReferenceToTypes::postvisit( TraitDecl * traitDecl ) { 497 489 if ( traitDecl->name == "sized" ) { 498 490 // "sized" is a special trait - flick the sized status on for the type variable … … 516 508 } 517 509 518 void LinkReferenceToTypes::visit( TraitInstType * traitInst ) { 519 Parent::visit( traitInst ); 510 void LinkReferenceToTypes::postvisit( TraitInstType * traitInst ) { 520 511 // handle other traits 521 TraitDecl *traitDecl = indexer->lookupTrait( traitInst->name );512 TraitDecl *traitDecl = local_indexer->lookupTrait( traitInst->name ); 522 513 if ( ! traitDecl ) { 523 514 throw SemanticError( "use of undeclared trait " + traitInst->name ); … … 540 531 } 541 532 542 void LinkReferenceToTypes:: visit( EnumDecl *enumDecl ) {533 void LinkReferenceToTypes::postvisit( EnumDecl *enumDecl ) { 543 534 // visit enum members first so that the types of self-referencing members are updated properly 544 Parent::visit( enumDecl );545 535 if ( ! enumDecl->get_members().empty() ) { 546 536 ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->get_name() ); … … 554 544 } 555 545 556 void LinkReferenceToTypes:: visit( StructDecl *structDecl ) {546 void LinkReferenceToTypes::postvisit( StructDecl *structDecl ) { 557 547 // visit struct members first so that the types of self-referencing members are updated properly 558 // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and and their defaults) 559 Parent::visit( structDecl ); 548 // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults) 560 549 if ( ! structDecl->get_members().empty() ) { 561 550 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() ); … … 569 558 } 570 559 571 void LinkReferenceToTypes::visit( UnionDecl *unionDecl ) { 572 Parent::visit( unionDecl ); 560 void LinkReferenceToTypes::postvisit( UnionDecl *unionDecl ) { 573 561 if ( ! unionDecl->get_members().empty() ) { 574 562 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() ); … … 582 570 } 583 571 584 void LinkReferenceToTypes:: visit( TypeInstType *typeInst ) {585 if ( NamedTypeDecl *namedTypeDecl = lo okupType( typeInst->get_name() ) ) {572 void LinkReferenceToTypes::postvisit( TypeInstType *typeInst ) { 573 if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->get_name() ) ) { 586 574 if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { 587 575 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); 588 576 } // if 589 } // if590 }591 592 ForallPointerDecay::ForallPointerDecay( const Indexer *other_indexer ) : Indexer( false ) {593 if ( other_indexer ) {594 indexer = other_indexer;595 } else {596 indexer = this;597 577 } // if 598 578 } … … 626 606 } 627 607 628 void ForallPointerDecay:: visit( ObjectDecl *object ) {608 void ForallPointerDecay::previsit( ObjectDecl *object ) { 629 609 forallFixer( object->get_type() ); 630 610 if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) { 631 611 forallFixer( pointer->get_base() ); 632 612 } // if 633 Parent::visit( object );634 613 object->fixUniqueId(); 635 614 } 636 615 637 void ForallPointerDecay:: visit( FunctionDecl *func ) {616 void ForallPointerDecay::previsit( FunctionDecl *func ) { 638 617 forallFixer( func->get_type() ); 639 Parent::visit( func );640 618 func->fixUniqueId(); 641 619 } … … 667 645 668 646 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { 669 EliminateTypedefeliminator;647 PassVisitor<EliminateTypedef> eliminator; 670 648 mutateAll( translationUnit, eliminator ); 671 if ( eliminator. typedefNames.count( "size_t" ) ) {649 if ( eliminator.pass.typedefNames.count( "size_t" ) ) { 672 650 // grab and remember declaration of size_t 673 SizeType = eliminator. typedefNames["size_t"].first->get_base()->clone();651 SizeType = eliminator.pass.typedefNames["size_t"].first->get_base()->clone(); 674 652 } else { 675 653 // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong … … 681 659 } 682 660 683 Type * EliminateTypedef::mutate( TypeInstType * typeInst ) {661 Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) { 684 662 // instances of typedef types will come here. If it is an instance 685 663 // of a typdef type, link the instance to its actual type. … … 696 674 rtt->get_parameters().clear(); 697 675 cloneAll( typeInst->get_parameters(), rtt->get_parameters() ); 698 mutateAll( rtt->get_parameters(), * this); // recursively fix typedefs on parameters676 mutateAll( rtt->get_parameters(), *visitor ); // recursively fix typedefs on parameters 699 677 } // if 700 678 delete typeInst; … … 708 686 } 709 687 710 Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) { 711 Declaration *ret = Mutator::mutate( tyDecl ); 712 688 Declaration *EliminateTypedef::postmutate( TypedefDecl * tyDecl ) { 713 689 if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) { 714 690 // typedef to the same name from the same scope … … 741 717 return new EnumDecl( enumDecl->get_name(), noAttributes, tyDecl->get_linkage() ); 742 718 } else { 743 return ret->clone();744 } // if 745 } 746 747 TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {719 return tyDecl->clone(); 720 } // if 721 } 722 723 void EliminateTypedef::premutate( TypeDecl * typeDecl ) { 748 724 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); 749 725 if ( i != typedefNames.end() ) { … … 752 728 753 729 typedeclNames[ typeDecl->get_name() ] = typeDecl; 754 return Mutator::mutate( typeDecl ); 755 } 756 757 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) { 758 typedefNames.beginScope(); 759 DeclarationWithType *ret = Mutator::mutate( funcDecl ); 760 typedefNames.endScope(); 761 return ret; 762 } 763 764 DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) { 765 typedefNames.beginScope(); 766 DeclarationWithType *ret = Mutator::mutate( objDecl ); 767 typedefNames.endScope(); 768 769 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) { // function type? 730 } 731 732 void EliminateTypedef::premutate( FunctionDecl * ) { 733 GuardScope( typedefNames ); 734 } 735 736 void EliminateTypedef::premutate( ObjectDecl * ) { 737 GuardScope( typedefNames ); 738 } 739 740 DeclarationWithType *EliminateTypedef::postmutate( ObjectDecl * objDecl ) { 741 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->get_type() ) ) { // function type? 770 742 // replace the current object declaration with a function declaration 771 FunctionDecl * newDecl = new FunctionDecl( ret->get_name(), ret->get_storageClasses(), ret->get_linkage(), funtype, 0, objDecl->get_attributes(), ret->get_funcSpec() );743 FunctionDecl * newDecl = new FunctionDecl( objDecl->get_name(), objDecl->get_storageClasses(), objDecl->get_linkage(), funtype, 0, objDecl->get_attributes(), objDecl->get_funcSpec() ); 772 744 objDecl->get_attributes().clear(); 773 745 objDecl->set_type( nullptr ); … … 775 747 return newDecl; 776 748 } // if 777 return ret; 778 } 779 780 Expression *EliminateTypedef::mutate( CastExpr * castExpr ) { 781 typedefNames.beginScope(); 782 Expression *ret = Mutator::mutate( castExpr ); 783 typedefNames.endScope(); 784 return ret; 785 } 786 787 CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) { 788 typedefNames.beginScope(); 749 return objDecl; 750 } 751 752 void EliminateTypedef::premutate( CastExpr * ) { 753 GuardScope( typedefNames ); 754 } 755 756 void EliminateTypedef::premutate( CompoundStmt * ) { 757 GuardScope( typedefNames ); 789 758 scopeLevel += 1; 790 CompoundStmt *ret = Mutator::mutate( compoundStmt ); 791 scopeLevel -= 1; 759 GuardAction( [this](){ scopeLevel -= 1; } ); 760 } 761 762 CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) { 792 763 // remove and delete decl stmts 793 764 filter( compoundStmt->kids, [](Statement * stmt) { … … 799 770 return false; 800 771 }, true); 801 typedefNames.endScope(); 802 return ret; 772 return compoundStmt; 803 773 } 804 774 … … 827 797 } 828 798 829 Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {799 void EliminateTypedef::premutate( StructDecl * structDecl ) { 830 800 addImplicitTypedef( structDecl ); 831 Mutator::mutate( structDecl ); 801 } 802 803 804 Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) { 832 805 return handleAggregate( structDecl ); 833 806 } 834 807 835 Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {808 void EliminateTypedef::premutate( UnionDecl * unionDecl ) { 836 809 addImplicitTypedef( unionDecl ); 837 Mutator::mutate( unionDecl ); 810 } 811 812 Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) { 838 813 return handleAggregate( unionDecl ); 839 814 } 840 815 841 Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {816 void EliminateTypedef::premutate( EnumDecl * enumDecl ) { 842 817 addImplicitTypedef( enumDecl ); 843 Mutator::mutate( enumDecl ); 818 } 819 820 Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) { 844 821 return handleAggregate( enumDecl ); 845 822 } 846 823 847 Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) { 848 Mutator::mutate( contextDecl ); 849 return handleAggregate( contextDecl ); 824 Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) { 825 return handleAggregate( traitDecl ); 850 826 } 851 827
Note:
See TracChangeset
for help on using the changeset viewer.