- Timestamp:
- Jun 28, 2018, 3:10:22 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, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
- Children:
- afcb0a3
- Parents:
- d419d8e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Validate.cc
rd419d8e r48ed81c 173 173 }; 174 174 175 struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards{176 EliminateTypedef() : scopeLevel( 0 ) {}175 struct ReplaceTypedef final : public WithVisitorRef<ReplaceTypedef>, public WithGuards, public WithShortCircuiting, public WithDeclsToAdd { 176 ReplaceTypedef() : scopeLevel( 0 ) {} 177 177 /// Replaces typedefs by forward declarations 178 static void eliminateTypedef( std::list< Declaration * > &translationUnit ); 179 178 static void replaceTypedef( std::list< Declaration * > &translationUnit ); 179 180 void premutate( QualifiedType * ); 181 Type * postmutate( QualifiedType * qualType ); 180 182 Type * postmutate( TypeInstType * aggregateUseType ); 181 183 Declaration * postmutate( TypedefDecl * typeDecl ); … … 188 190 189 191 void premutate( CompoundStmt * compoundStmt ); 190 CompoundStmt * postmutate( CompoundStmt * compoundStmt );191 192 192 193 void premutate( StructDecl * structDecl ); 193 Declaration * postmutate( StructDecl * structDecl );194 194 void premutate( UnionDecl * unionDecl ); 195 Declaration * postmutate( UnionDecl * unionDecl );196 195 void premutate( EnumDecl * enumDecl ); 197 Declaration * postmutate( EnumDecl * enumDecl );198 Declaration * postmutate( TraitDecl * contextDecl );199 196 200 197 void premutate( FunctionType * ftype ); … … 202 199 private: 203 200 template<typename AggDecl> 204 AggDecl *handleAggregate( AggDecl * aggDecl );205 206 template<typename AggDecl>207 201 void addImplicitTypedef( AggDecl * aggDecl ); 202 template< typename AggDecl > 203 void handleAggregate( AggDecl * aggr ); 208 204 209 205 typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr; … … 274 270 275 271 acceptAll( translationUnit, hoistDecls ); 276 EliminateTypedef::eliminateTypedef( translationUnit );272 ReplaceTypedef::replaceTypedef( translationUnit ); 277 273 ReturnTypeFixer::fix( translationUnit ); // must happen before autogen 278 274 acceptAll( translationUnit, epc ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes because it is an indexer and needs correct types for mangling … … 731 727 732 728 733 bool isTypedef( Declaration *decl ) { 734 return dynamic_cast< TypedefDecl * >( decl ); 735 } 736 737 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { 738 PassVisitor<EliminateTypedef> eliminator; 729 void ReplaceTypedef::replaceTypedef( std::list< Declaration * > &translationUnit ) { 730 PassVisitor<ReplaceTypedef> eliminator; 739 731 mutateAll( translationUnit, eliminator ); 740 732 if ( eliminator.pass.typedefNames.count( "size_t" ) ) { … … 746 738 SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 747 739 } 748 filter( translationUnit, isTypedef, true ); 749 } 750 751 Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) { 740 } 741 742 void ReplaceTypedef::premutate( QualifiedType * ) { 743 visit_children = false; 744 } 745 746 Type * ReplaceTypedef::postmutate( QualifiedType * qualType ) { 747 // replacing typedefs only makes sense for the 'oldest ancestor' of the qualified type 748 qualType->parent = qualType->parent->acceptMutator( *visitor ); 749 return qualType; 750 } 751 752 Type * ReplaceTypedef::postmutate( TypeInstType * typeInst ) { 752 753 // instances of typedef types will come here. If it is an instance 753 754 // of a typdef type, link the instance to its actual type. … … 797 798 } 798 799 799 Declaration * EliminateTypedef::postmutate( TypedefDecl * tyDecl ) {800 Declaration * ReplaceTypedef::postmutate( TypedefDecl * tyDecl ) { 800 801 if ( typedefNames.count( tyDecl->name ) == 1 && typedefNames[ tyDecl->name ].second == scopeLevel ) { 801 802 // typedef to the same name from the same scope … … 829 830 Type *designatorType = tyDecl->base->stripDeclarator(); 830 831 if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) { 831 return new StructDecl( aggDecl->name, DeclarationNode::Struct, noAttributes, tyDecl->linkage);832 declsToAddBefore.push_back( new StructDecl( aggDecl->name, DeclarationNode::Struct, noAttributes, tyDecl->linkage ) ); 832 833 } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) { 833 return new UnionDecl( aggDecl->name, noAttributes, tyDecl->linkage);834 declsToAddBefore.push_back( new UnionDecl( aggDecl->name, noAttributes, tyDecl->linkage ) ); 834 835 } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) { 835 return new EnumDecl( enumDecl->name, noAttributes, tyDecl->linkage ); 836 } else { 837 return tyDecl->clone(); 838 } // if 839 } 840 841 void EliminateTypedef::premutate( TypeDecl * typeDecl ) { 836 declsToAddBefore.push_back( new EnumDecl( enumDecl->name, noAttributes, tyDecl->linkage ) ); 837 } // if 838 return tyDecl->clone(); 839 } 840 841 void ReplaceTypedef::premutate( TypeDecl * typeDecl ) { 842 842 TypedefMap::iterator i = typedefNames.find( typeDecl->name ); 843 843 if ( i != typedefNames.end() ) { … … 848 848 } 849 849 850 void EliminateTypedef::premutate( FunctionDecl * ) {850 void ReplaceTypedef::premutate( FunctionDecl * ) { 851 851 GuardScope( typedefNames ); 852 852 } 853 853 854 void EliminateTypedef::premutate( ObjectDecl * ) {854 void ReplaceTypedef::premutate( ObjectDecl * ) { 855 855 GuardScope( typedefNames ); 856 856 } 857 857 858 DeclarationWithType * EliminateTypedef::postmutate( ObjectDecl * objDecl ) {858 DeclarationWithType * ReplaceTypedef::postmutate( ObjectDecl * objDecl ) { 859 859 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->type ) ) { // function type? 860 860 // replace the current object declaration with a function declaration … … 868 868 } 869 869 870 void EliminateTypedef::premutate( CastExpr * ) {870 void ReplaceTypedef::premutate( CastExpr * ) { 871 871 GuardScope( typedefNames ); 872 872 } 873 873 874 void EliminateTypedef::premutate( CompoundStmt * ) {874 void ReplaceTypedef::premutate( CompoundStmt * ) { 875 875 GuardScope( typedefNames ); 876 876 scopeLevel += 1; … … 878 878 } 879 879 880 CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) {881 // remove and delete decl stmts882 filter( compoundStmt->kids, [](Statement * stmt) {883 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {884 if ( dynamic_cast< TypedefDecl * >( declStmt->decl ) ) {885 return true;886 } // if887 } // if888 return false;889 }, true);890 return compoundStmt;891 }892 893 // there may be typedefs nested within aggregates. in order for everything to work properly, these should be removed894 // as well895 880 template<typename AggDecl> 896 AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) { 897 filter( aggDecl->members, isTypedef, true ); 898 return aggDecl; 899 } 900 901 template<typename AggDecl> 902 void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) { 881 void ReplaceTypedef::addImplicitTypedef( AggDecl * aggDecl ) { 903 882 if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { 904 883 Type *type = nullptr; … … 912 891 TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type, aggDecl->get_linkage() ) ); 913 892 typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel ); 914 } // if 915 } 916 917 void EliminateTypedef::premutate( StructDecl * structDecl ) { 893 // add the implicit typedef to the AST 894 declsToAddBefore.push_back( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type->clone(), aggDecl->get_linkage() ) ); 895 } // if 896 } 897 898 template< typename AggDecl > 899 void ReplaceTypedef::handleAggregate( AggDecl * aggr ) { 900 SemanticErrorException errors; 901 902 ValueGuard< std::list<Declaration * > > oldBeforeDecls( declsToAddBefore ); 903 ValueGuard< std::list<Declaration * > > oldAfterDecls ( declsToAddAfter ); 904 declsToAddBefore.clear(); 905 declsToAddAfter.clear(); 906 907 GuardScope( typedefNames ); 908 mutateAll( aggr->parameters, *visitor ); 909 910 // unroll mutateAll for aggr->members so that implicit typedefs for nested types are added to the aggregate body. 911 for ( std::list< Declaration * >::iterator i = aggr->members.begin(); i != aggr->members.end(); ++i ) { 912 if ( !declsToAddAfter.empty() ) { aggr->members.splice( i, declsToAddAfter ); } 913 914 try { 915 *i = maybeMutate( *i, *visitor ); 916 } catch ( SemanticErrorException &e ) { 917 errors.append( e ); 918 } 919 920 if ( !declsToAddBefore.empty() ) { aggr->members.splice( i, declsToAddBefore ); } 921 } 922 923 if ( !declsToAddAfter.empty() ) { aggr->members.splice( aggr->members.end(), declsToAddAfter ); } 924 if ( !errors.isEmpty() ) { throw errors; } 925 } 926 927 void ReplaceTypedef::premutate( StructDecl * structDecl ) { 928 visit_children = false; 918 929 addImplicitTypedef( structDecl ); 919 } 920 921 922 Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) { 923 return handleAggregate( structDecl ); 924 } 925 926 void EliminateTypedef::premutate( UnionDecl * unionDecl ) { 930 handleAggregate( structDecl ); 931 } 932 933 void ReplaceTypedef::premutate( UnionDecl * unionDecl ) { 934 visit_children = false; 927 935 addImplicitTypedef( unionDecl ); 928 } 929 930 Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) { 931 return handleAggregate( unionDecl ); 932 } 933 934 void EliminateTypedef::premutate( EnumDecl * enumDecl ) { 936 handleAggregate( unionDecl ); 937 } 938 939 void ReplaceTypedef::premutate( EnumDecl * enumDecl ) { 935 940 addImplicitTypedef( enumDecl ); 936 941 } 937 942 938 Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) { 939 return handleAggregate( enumDecl ); 940 } 941 942 Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) { 943 return handleAggregate( traitDecl ); 944 } 945 946 void EliminateTypedef::premutate( FunctionType * ) { 943 void ReplaceTypedef::premutate( FunctionType * ) { 947 944 GuardValue( inFunctionType ); 948 945 inFunctionType = true;
Note: See TracChangeset
for help on using the changeset viewer.