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