Changeset a506df4
- Timestamp:
- Sep 12, 2017, 5:49:32 PM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- b3c7963
- Parents:
- 70d826cd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Validate.cc
r70d826cd ra506df4 176 176 }; 177 177 178 class EliminateTypedef : public Mutator { 179 public: 178 struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards { 180 179 EliminateTypedef() : scopeLevel( 0 ) {} 181 180 /// Replaces typedefs by forward declarations 182 181 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 183 203 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 204 template<typename AggDecl> 198 205 AggDecl *handleAggregate( AggDecl * aggDecl ); … … 667 674 668 675 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { 669 EliminateTypedefeliminator;676 PassVisitor<EliminateTypedef> eliminator; 670 677 mutateAll( translationUnit, eliminator ); 671 if ( eliminator. typedefNames.count( "size_t" ) ) {678 if ( eliminator.pass.typedefNames.count( "size_t" ) ) { 672 679 // grab and remember declaration of size_t 673 SizeType = eliminator. typedefNames["size_t"].first->get_base()->clone();680 SizeType = eliminator.pass.typedefNames["size_t"].first->get_base()->clone(); 674 681 } else { 675 682 // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong … … 681 688 } 682 689 683 Type * EliminateTypedef::mutate( TypeInstType * typeInst ) {690 Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) { 684 691 // instances of typedef types will come here. If it is an instance 685 692 // of a typdef type, link the instance to its actual type. … … 696 703 rtt->get_parameters().clear(); 697 704 cloneAll( typeInst->get_parameters(), rtt->get_parameters() ); 698 mutateAll( rtt->get_parameters(), * this); // recursively fix typedefs on parameters705 mutateAll( rtt->get_parameters(), *visitor ); // recursively fix typedefs on parameters 699 706 } // if 700 707 delete typeInst; … … 708 715 } 709 716 710 Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) { 711 Declaration *ret = Mutator::mutate( tyDecl ); 712 717 Declaration *EliminateTypedef::postmutate( TypedefDecl * tyDecl ) { 713 718 if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) { 714 719 // typedef to the same name from the same scope … … 741 746 return new EnumDecl( enumDecl->get_name(), noAttributes, tyDecl->get_linkage() ); 742 747 } else { 743 return ret->clone();744 } // if 745 } 746 747 TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {748 return tyDecl->clone(); 749 } // if 750 } 751 752 void EliminateTypedef::premutate( TypeDecl * typeDecl ) { 748 753 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); 749 754 if ( i != typedefNames.end() ) { … … 752 757 753 758 typedeclNames[ typeDecl->get_name() ] = typeDecl; 754 return dynamic_cast<TypeDecl *>( 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? 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? 770 771 // 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() );772 FunctionDecl * newDecl = new FunctionDecl( objDecl->get_name(), objDecl->get_storageClasses(), objDecl->get_linkage(), funtype, 0, objDecl->get_attributes(), objDecl->get_funcSpec() ); 772 773 objDecl->get_attributes().clear(); 773 774 objDecl->set_type( nullptr ); … … 775 776 return newDecl; 776 777 } // 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(); 778 return objDecl; 779 } 780 781 void EliminateTypedef::premutate( CastExpr * ) { 782 GuardScope( typedefNames ); 783 } 784 785 void EliminateTypedef::premutate( CompoundStmt * ) { 786 GuardScope( typedefNames ); 789 787 scopeLevel += 1; 790 CompoundStmt *ret = Mutator::mutate( compoundStmt ); 791 scopeLevel -= 1; 788 GuardAction( [this](){ scopeLevel -= 1; } ); 789 } 790 791 CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) { 792 792 // remove and delete decl stmts 793 793 filter( compoundStmt->kids, [](Statement * stmt) { … … 799 799 return false; 800 800 }, true); 801 typedefNames.endScope(); 802 return ret; 801 return compoundStmt; 803 802 } 804 803 … … 827 826 } 828 827 829 Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {828 void EliminateTypedef::premutate( StructDecl * structDecl ) { 830 829 addImplicitTypedef( structDecl ); 831 Mutator::mutate( structDecl ); 830 } 831 832 833 Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) { 832 834 return handleAggregate( structDecl ); 833 835 } 834 836 835 Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {837 void EliminateTypedef::premutate( UnionDecl * unionDecl ) { 836 838 addImplicitTypedef( unionDecl ); 837 Mutator::mutate( unionDecl ); 839 } 840 841 Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) { 838 842 return handleAggregate( unionDecl ); 839 843 } 840 844 841 Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {845 void EliminateTypedef::premutate( EnumDecl * enumDecl ) { 842 846 addImplicitTypedef( enumDecl ); 843 Mutator::mutate( enumDecl ); 847 } 848 849 Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) { 844 850 return handleAggregate( enumDecl ); 845 851 } 846 852 847 Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) { 848 Mutator::mutate( contextDecl ); 849 return handleAggregate( contextDecl ); 853 Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) { 854 return handleAggregate( traitDecl ); 850 855 } 851 856
Note: See TracChangeset
for help on using the changeset viewer.