Changes in src/SymTab/Validate.cc [a506df4:5809461]
- File:
-
- 1 edited
-
src/SymTab/Validate.cc (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Validate.cc
ra506df4 r5809461 176 176 }; 177 177 178 struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards { 178 class EliminateTypedef : public Mutator { 179 public: 179 180 EliminateTypedef() : scopeLevel( 0 ) {} 180 181 /// Replaces typedefs by forward declarations 181 182 static void eliminateTypedef( std::list< Declaration * > &translationUnit ); 182 183 Type * postmutate( TypeInstType * aggregateUseType );184 Declaration * postmutate( TypedefDecl * typeDecl );185 void premutate( TypeDecl * typeDecl );186 void premutate( FunctionDecl * funcDecl );187 void premutate( ObjectDecl * objDecl );188 DeclarationWithType * postmutate( ObjectDecl * objDecl );189 190 void premutate( CastExpr * castExpr );191 192 void premutate( CompoundStmt * compoundStmt );193 CompoundStmt * postmutate( CompoundStmt * compoundStmt );194 195 void premutate( StructDecl * structDecl );196 Declaration * postmutate( StructDecl * structDecl );197 void premutate( UnionDecl * unionDecl );198 Declaration * postmutate( UnionDecl * unionDecl );199 void premutate( EnumDecl * enumDecl );200 Declaration * postmutate( EnumDecl * enumDecl );201 Declaration * postmutate( TraitDecl * contextDecl );202 203 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 204 197 template<typename AggDecl> 205 198 AggDecl *handleAggregate( AggDecl * aggDecl ); … … 674 667 675 668 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { 676 PassVisitor<EliminateTypedef>eliminator;669 EliminateTypedef eliminator; 677 670 mutateAll( translationUnit, eliminator ); 678 if ( eliminator. pass.typedefNames.count( "size_t" ) ) {671 if ( eliminator.typedefNames.count( "size_t" ) ) { 679 672 // grab and remember declaration of size_t 680 SizeType = eliminator. pass.typedefNames["size_t"].first->get_base()->clone();673 SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone(); 681 674 } else { 682 675 // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong … … 688 681 } 689 682 690 Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) {683 Type *EliminateTypedef::mutate( TypeInstType * typeInst ) { 691 684 // instances of typedef types will come here. If it is an instance 692 685 // of a typdef type, link the instance to its actual type. … … 703 696 rtt->get_parameters().clear(); 704 697 cloneAll( typeInst->get_parameters(), rtt->get_parameters() ); 705 mutateAll( rtt->get_parameters(), * visitor); // recursively fix typedefs on parameters698 mutateAll( rtt->get_parameters(), *this ); // recursively fix typedefs on parameters 706 699 } // if 707 700 delete typeInst; … … 715 708 } 716 709 717 Declaration *EliminateTypedef::postmutate( TypedefDecl * tyDecl ) { 710 Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) { 711 Declaration *ret = Mutator::mutate( tyDecl ); 712 718 713 if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) { 719 714 // typedef to the same name from the same scope … … 746 741 return new EnumDecl( enumDecl->get_name(), noAttributes, tyDecl->get_linkage() ); 747 742 } else { 748 return tyDecl->clone();749 } // if 750 } 751 752 void EliminateTypedef::premutate( TypeDecl * typeDecl ) {743 return ret->clone(); 744 } // if 745 } 746 747 TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) { 753 748 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); 754 749 if ( i != typedefNames.end() ) { … … 757 752 758 753 typedeclNames[ typeDecl->get_name() ] = typeDecl; 759 } 760 761 void EliminateTypedef::premutate( FunctionDecl * ) { 762 GuardScope( typedefNames ); 763 } 764 765 void EliminateTypedef::premutate( ObjectDecl * ) { 766 GuardScope( typedefNames ); 767 } 768 769 DeclarationWithType *EliminateTypedef::postmutate( ObjectDecl * objDecl ) { 770 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? 771 770 // replace the current object declaration with a function declaration 772 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() ); 773 772 objDecl->get_attributes().clear(); 774 773 objDecl->set_type( nullptr ); … … 776 775 return newDecl; 777 776 } // if 778 return objDecl; 779 } 780 781 void EliminateTypedef::premutate( CastExpr * ) { 782 GuardScope( typedefNames ); 783 } 784 785 void EliminateTypedef::premutate( CompoundStmt * ) { 786 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(); 787 789 scopeLevel += 1; 788 GuardAction( [this](){ scopeLevel -= 1; } ); 789 } 790 791 CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) { 790 CompoundStmt *ret = Mutator::mutate( compoundStmt ); 791 scopeLevel -= 1; 792 792 // remove and delete decl stmts 793 793 filter( compoundStmt->kids, [](Statement * stmt) { … … 799 799 return false; 800 800 }, true); 801 return compoundStmt; 801 typedefNames.endScope(); 802 return ret; 802 803 } 803 804 … … 826 827 } 827 828 828 void EliminateTypedef::premutate( StructDecl * structDecl ) {829 Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) { 829 830 addImplicitTypedef( structDecl ); 830 } 831 832 833 Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) { 831 Mutator::mutate( structDecl ); 834 832 return handleAggregate( structDecl ); 835 833 } 836 834 837 void EliminateTypedef::premutate( UnionDecl * unionDecl ) {835 Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) { 838 836 addImplicitTypedef( unionDecl ); 839 } 840 841 Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) { 837 Mutator::mutate( unionDecl ); 842 838 return handleAggregate( unionDecl ); 843 839 } 844 840 845 void EliminateTypedef::premutate( EnumDecl * enumDecl ) {841 Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) { 846 842 addImplicitTypedef( enumDecl ); 847 } 848 849 Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) { 843 Mutator::mutate( enumDecl ); 850 844 return handleAggregate( enumDecl ); 851 845 } 852 846 853 Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) { 854 return handleAggregate( traitDecl ); 847 Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) { 848 Mutator::mutate( contextDecl ); 849 return handleAggregate( contextDecl ); 855 850 } 856 851
Note:
See TracChangeset
for help on using the changeset viewer.