Changeset 9939dc3 for src/SymTab
- Timestamp:
- May 17, 2022, 3:44:08 PM (3 years ago)
- Branches:
- ADT, ast-experimental, master, pthread-emulation, qualifiedEnum
- Children:
- 767a8ef
- Parents:
- fa2a3b1
- Location:
- src/SymTab
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Validate.cc
rfa2a3b1 r9939dc3 10 10 // Created On : Sun May 17 21:50:04 2015 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Apr 29 9:45:00 202213 // Update Count : 36 512 // Last Modified On : Tue May 17 14:36:00 2022 13 // Update Count : 366 14 14 // 15 15 … … 74 74 #include "ResolvExpr/ResolveTypeof.h" // for resolveTypeof 75 75 #include "SymTab/Autogen.h" // for SizeType 76 #include "SymTab/ValidateType.h" // for decayEnumsAndPointers, decayFo... 76 77 #include "SynTree/LinkageSpec.h" // for C 77 78 #include "SynTree/Attribute.h" // for noAttributes, Attribute … … 134 135 }; 135 136 136 /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.137 struct EnumAndPointerDecay_old {138 void previsit( EnumDecl * aggregateDecl );139 void previsit( FunctionType * func );140 };141 142 /// Associates forward declarations of aggregates with their definitions143 struct LinkReferenceToTypes_old final : public WithIndexer, public WithGuards, public WithVisitorRef<LinkReferenceToTypes_old>, public WithShortCircuiting {144 LinkReferenceToTypes_old( const Indexer * indexer );145 146 void postvisit( TypeInstType * typeInst );147 148 void postvisit( EnumInstType * enumInst );149 void postvisit( StructInstType * structInst );150 void postvisit( UnionInstType * unionInst );151 void postvisit( TraitInstType * traitInst );152 void previsit( QualifiedType * qualType );153 void postvisit( QualifiedType * qualType );154 155 void postvisit( EnumDecl * enumDecl );156 void postvisit( StructDecl * structDecl );157 void postvisit( UnionDecl * unionDecl );158 void postvisit( TraitDecl * traitDecl );159 160 void previsit( StructDecl * structDecl );161 void previsit( UnionDecl * unionDecl );162 163 void renameGenericParams( std::list< TypeDecl * > & params );164 165 private:166 const Indexer * local_indexer;167 168 typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType;169 typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;170 typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;171 ForwardEnumsType forwardEnums;172 ForwardStructsType forwardStructs;173 ForwardUnionsType forwardUnions;174 /// true if currently in a generic type body, so that type parameter instances can be renamed appropriately175 bool inGeneric = false;176 };177 178 137 /// Does early resolution on the expressions that give enumeration constants their values 179 138 struct ResolveEnumInitializers final : public WithIndexer, public WithGuards, public WithVisitorRef<ResolveEnumInitializers>, public WithShortCircuiting { … … 193 152 void previsit( StructDecl * aggrDecl ); 194 153 void previsit( UnionDecl * aggrDecl ); 195 };196 197 // These structs are the sub-sub-passes of ForallPointerDecay_old.198 199 struct TraitExpander_old final {200 void previsit( FunctionType * );201 void previsit( StructDecl * );202 void previsit( UnionDecl * );203 };204 205 struct AssertionFixer_old final {206 void previsit( FunctionType * );207 void previsit( StructDecl * );208 void previsit( UnionDecl * );209 };210 211 struct CheckOperatorTypes_old final {212 void previsit( ObjectDecl * );213 };214 215 struct FixUniqueIds_old final {216 void previsit( DeclarationWithType * );217 154 }; 218 155 … … 358 295 359 296 void validate_A( std::list< Declaration * > & translationUnit ) { 360 PassVisitor<EnumAndPointerDecay_old> epc;361 297 PassVisitor<HoistTypeDecls> hoistDecls; 362 298 { … … 367 303 ReplaceTypedef::replaceTypedef( translationUnit ); 368 304 ReturnTypeFixer::fix( translationUnit ); // must happen before autogen 369 acceptAll( translationUnit, epc ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes_old because it is an indexer and needs correct types for mangling 370 } 371 } 372 373 void linkReferenceToTypes( std::list< Declaration * > & translationUnit ) { 374 PassVisitor<LinkReferenceToTypes_old> lrt( nullptr ); 375 acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions 305 decayEnumsAndPointers( translationUnit ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes_old because it is an indexer and needs correct types for mangling 306 } 376 307 } 377 308 … … 412 343 }); 413 344 } 414 }415 416 static void decayForallPointers( std::list< Declaration * > & translationUnit ) {417 PassVisitor<TraitExpander_old> te;418 acceptAll( translationUnit, te );419 PassVisitor<AssertionFixer_old> af;420 acceptAll( translationUnit, af );421 PassVisitor<CheckOperatorTypes_old> cot;422 acceptAll( translationUnit, cot );423 PassVisitor<FixUniqueIds_old> fui;424 acceptAll( translationUnit, fui );425 345 } 426 346 … … 501 421 } 502 422 503 void validateType( Type * type, const Indexer * indexer ) {504 PassVisitor<EnumAndPointerDecay_old> epc;505 PassVisitor<LinkReferenceToTypes_old> lrt( indexer );506 PassVisitor<TraitExpander_old> te;507 PassVisitor<AssertionFixer_old> af;508 PassVisitor<CheckOperatorTypes_old> cot;509 PassVisitor<FixUniqueIds_old> fui;510 type->accept( epc );511 type->accept( lrt );512 type->accept( te );513 type->accept( af );514 type->accept( cot );515 type->accept( fui );516 }517 518 423 void HoistTypeDecls::handleType( Type * type ) { 519 424 // some type declarations are buried in expressions and not easy to hoist during parsing; hoist them here … … 708 613 } 709 614 710 void EnumAndPointerDecay_old::previsit( EnumDecl * enumDecl ) {711 // Set the type of each member of the enumeration to be EnumConstant712 for ( std::list< Declaration * >::iterator i = enumDecl->members.begin(); i != enumDecl->members.end(); ++i ) {713 ObjectDecl * obj = dynamic_cast< ObjectDecl * >( * i );714 assert( obj );715 obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->name ) );716 } // for717 }718 719 namespace {720 template< typename DWTList >721 void fixFunctionList( DWTList & dwts, bool isVarArgs, FunctionType * func ) {722 auto nvals = dwts.size();723 bool containsVoid = false;724 for ( auto & dwt : dwts ) {725 // fix each DWT and record whether a void was found726 containsVoid |= fixFunction( dwt );727 }728 729 // the only case in which "void" is valid is where it is the only one in the list730 if ( containsVoid && ( nvals > 1 || isVarArgs ) ) {731 SemanticError( func, "invalid type void in function type " );732 }733 734 // one void is the only thing in the list; remove it.735 if ( containsVoid ) {736 delete dwts.front();737 dwts.clear();738 }739 }740 }741 742 void EnumAndPointerDecay_old::previsit( FunctionType * func ) {743 // Fix up parameters and return types744 fixFunctionList( func->parameters, func->isVarArgs, func );745 fixFunctionList( func->returnVals, false, func );746 }747 748 LinkReferenceToTypes_old::LinkReferenceToTypes_old( const Indexer * other_indexer ) : WithIndexer( false ) {749 if ( other_indexer ) {750 local_indexer = other_indexer;751 } else {752 local_indexer = &indexer;753 } // if754 }755 756 void LinkReferenceToTypes_old::postvisit( EnumInstType * enumInst ) {757 const EnumDecl * st = local_indexer->lookupEnum( enumInst->name );758 // it's not a semantic error if the enum is not found, just an implicit forward declaration759 if ( st ) {760 enumInst->baseEnum = const_cast<EnumDecl *>(st); // Just linking in the node761 } // if762 if ( ! st || ! st->body ) {763 // use of forward declaration764 forwardEnums[ enumInst->name ].push_back( enumInst );765 } // if766 }767 void LinkReferenceToTypes_old::postvisit( StructInstType * structInst ) {768 const StructDecl * st = local_indexer->lookupStruct( structInst->name );769 // it's not a semantic error if the struct is not found, just an implicit forward declaration770 if ( st ) {771 structInst->baseStruct = const_cast<StructDecl *>(st); // Just linking in the node772 } // if773 if ( ! st || ! st->body ) {774 // use of forward declaration775 forwardStructs[ structInst->name ].push_back( structInst );776 } // if777 }778 779 void LinkReferenceToTypes_old::postvisit( UnionInstType * unionInst ) {780 const UnionDecl * un = local_indexer->lookupUnion( unionInst->name );781 // it's not a semantic error if the union is not found, just an implicit forward declaration782 if ( un ) {783 unionInst->baseUnion = const_cast<UnionDecl *>(un); // Just linking in the node784 } // if785 if ( ! un || ! un->body ) {786 // use of forward declaration787 forwardUnions[ unionInst->name ].push_back( unionInst );788 } // if789 }790 791 void LinkReferenceToTypes_old::previsit( QualifiedType * ) {792 visit_children = false;793 }794 795 void LinkReferenceToTypes_old::postvisit( QualifiedType * qualType ) {796 // linking only makes sense for the 'oldest ancestor' of the qualified type797 qualType->parent->accept( * visitor );798 }799 800 template< typename Decl >801 void normalizeAssertions( std::list< Decl * > & assertions ) {802 // ensure no duplicate trait members after the clone803 auto pred = [](Decl * d1, Decl * d2) {804 // only care if they're equal805 DeclarationWithType * dwt1 = dynamic_cast<DeclarationWithType *>( d1 );806 DeclarationWithType * dwt2 = dynamic_cast<DeclarationWithType *>( d2 );807 if ( dwt1 && dwt2 ) {808 if ( dwt1->name == dwt2->name && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) {809 // std::cerr << "=========== equal:" << std::endl;810 // std::cerr << "d1: " << d1 << std::endl;811 // std::cerr << "d2: " << d2 << std::endl;812 return false;813 }814 }815 return d1 < d2;816 };817 std::set<Decl *, decltype(pred)> unique_members( assertions.begin(), assertions.end(), pred );818 // if ( unique_members.size() != assertions.size() ) {819 // std::cerr << "============different" << std::endl;820 // std::cerr << unique_members.size() << " " << assertions.size() << std::endl;821 // }822 823 std::list< Decl * > order;824 order.splice( order.end(), assertions );825 std::copy_if( order.begin(), order.end(), back_inserter( assertions ), [&]( Decl * decl ) {826 return unique_members.count( decl );827 });828 }829 830 615 // expand assertions from trait instance, performing the appropriate type variable substitutions 831 616 template< typename Iterator > … … 838 623 // substitute trait decl parameters for instance parameters 839 624 applySubstitution( inst->baseTrait->parameters.begin(), inst->baseTrait->parameters.end(), inst->parameters.begin(), asserts.begin(), asserts.end(), out ); 840 }841 842 void LinkReferenceToTypes_old::postvisit( TraitDecl * traitDecl ) {843 if ( traitDecl->name == "sized" ) {844 // "sized" is a special trait - flick the sized status on for the type variable845 assertf( traitDecl->parameters.size() == 1, "Built-in trait 'sized' has incorrect number of parameters: %zd", traitDecl->parameters.size() );846 TypeDecl * td = traitDecl->parameters.front();847 td->set_sized( true );848 }849 850 // move assertions from type parameters into the body of the trait851 for ( TypeDecl * td : traitDecl->parameters ) {852 for ( DeclarationWithType * assert : td->assertions ) {853 if ( TraitInstType * inst = dynamic_cast< TraitInstType * >( assert->get_type() ) ) {854 expandAssertions( inst, back_inserter( traitDecl->members ) );855 } else {856 traitDecl->members.push_back( assert->clone() );857 }858 }859 deleteAll( td->assertions );860 td->assertions.clear();861 } // for862 }863 864 void LinkReferenceToTypes_old::postvisit( TraitInstType * traitInst ) {865 // handle other traits866 const TraitDecl * traitDecl = local_indexer->lookupTrait( traitInst->name );867 if ( ! traitDecl ) {868 SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name );869 } // if870 if ( traitDecl->parameters.size() != traitInst->parameters.size() ) {871 SemanticError( traitInst, "incorrect number of trait parameters: " );872 } // if873 traitInst->baseTrait = const_cast<TraitDecl *>(traitDecl); // Just linking in the node874 875 // need to carry over the 'sized' status of each decl in the instance876 for ( auto p : group_iterate( traitDecl->parameters, traitInst->parameters ) ) {877 TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) );878 if ( ! expr ) {879 SemanticError( std::get<1>(p), "Expression parameters for trait instances are currently unsupported: " );880 }881 if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) {882 TypeDecl * formalDecl = std::get<0>(p);883 TypeDecl * instDecl = inst->baseType;884 if ( formalDecl->get_sized() ) instDecl->set_sized( true );885 }886 }887 // normalizeAssertions( traitInst->members );888 }889 890 void LinkReferenceToTypes_old::postvisit( EnumDecl * enumDecl ) {891 // visit enum members first so that the types of self-referencing members are updated properly892 // Replace the enum base; right now it works only for StructEnum893 if ( enumDecl->base && dynamic_cast<TypeInstType*>(enumDecl->base) ) {894 std::string baseName = static_cast<TypeInstType*>(enumDecl->base)->name;895 const StructDecl * st = local_indexer->lookupStruct( baseName );896 if ( st ) {897 enumDecl->base = new StructInstType(Type::Qualifiers(),const_cast<StructDecl *>(st)); // Just linking in the node898 }899 }900 if ( enumDecl->body ) {901 ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->name );902 if ( fwds != forwardEnums.end() ) {903 for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {904 (* inst)->baseEnum = enumDecl;905 } // for906 forwardEnums.erase( fwds );907 } // if908 } // if909 }910 911 void LinkReferenceToTypes_old::renameGenericParams( std::list< TypeDecl * > & params ) {912 // rename generic type parameters uniquely so that they do not conflict with user-defined function forall parameters, e.g.913 // forall(otype T)914 // struct Box {915 // T x;916 // };917 // forall(otype T)918 // void f(Box(T) b) {919 // ...920 // }921 // The T in Box and the T in f are different, so internally the naming must reflect that.922 GuardValue( inGeneric );923 inGeneric = ! params.empty();924 for ( TypeDecl * td : params ) {925 td->name = "__" + td->name + "_generic_";926 }927 }928 929 void LinkReferenceToTypes_old::previsit( StructDecl * structDecl ) {930 renameGenericParams( structDecl->parameters );931 }932 933 void LinkReferenceToTypes_old::previsit( UnionDecl * unionDecl ) {934 renameGenericParams( unionDecl->parameters );935 }936 937 void LinkReferenceToTypes_old::postvisit( StructDecl * structDecl ) {938 // visit struct members first so that the types of self-referencing members are updated properly939 // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults)940 if ( structDecl->body ) {941 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->name );942 if ( fwds != forwardStructs.end() ) {943 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {944 (* inst)->baseStruct = structDecl;945 } // for946 forwardStructs.erase( fwds );947 } // if948 } // if949 }950 951 void LinkReferenceToTypes_old::postvisit( UnionDecl * unionDecl ) {952 if ( unionDecl->body ) {953 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->name );954 if ( fwds != forwardUnions.end() ) {955 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {956 (* inst)->baseUnion = unionDecl;957 } // for958 forwardUnions.erase( fwds );959 } // if960 } // if961 }962 963 void LinkReferenceToTypes_old::postvisit( TypeInstType * typeInst ) {964 // ensure generic parameter instances are renamed like the base type965 if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name;966 if ( const NamedTypeDecl * namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) {967 if ( const TypeDecl * typeDecl = dynamic_cast< const TypeDecl * >( namedTypeDecl ) ) {968 typeInst->set_isFtype( typeDecl->kind == TypeDecl::Ftype );969 } // if970 } // if971 625 } 972 626 … … 997 651 } 998 652 } 999 1000 653 } 1001 654 } … … 1085 738 void ForallPointerDecay_old::previsit( UnionDecl * aggrDecl ) { 1086 739 forallFixer( aggrDecl->parameters, aggrDecl ); 1087 }1088 1089 void TraitExpander_old::previsit( FunctionType * ftype ) {1090 expandTraits( ftype->forall );1091 }1092 1093 void TraitExpander_old::previsit( StructDecl * aggrDecl ) {1094 expandTraits( aggrDecl->parameters );1095 }1096 1097 void TraitExpander_old::previsit( UnionDecl * aggrDecl ) {1098 expandTraits( aggrDecl->parameters );1099 }1100 1101 void AssertionFixer_old::previsit( FunctionType * ftype ) {1102 fixAssertions( ftype->forall, ftype );1103 }1104 1105 void AssertionFixer_old::previsit( StructDecl * aggrDecl ) {1106 fixAssertions( aggrDecl->parameters, aggrDecl );1107 }1108 1109 void AssertionFixer_old::previsit( UnionDecl * aggrDecl ) {1110 fixAssertions( aggrDecl->parameters, aggrDecl );1111 }1112 1113 void CheckOperatorTypes_old::previsit( ObjectDecl * object ) {1114 // ensure that operator names only apply to functions or function pointers1115 if ( CodeGen::isOperator( object->name ) && ! dynamic_cast< FunctionType * >( object->type->stripDeclarator() ) ) {1116 SemanticError( object->location, toCString( "operator ", object->name.c_str(), " is not a function or function pointer." ) );1117 }1118 }1119 1120 void FixUniqueIds_old::previsit( DeclarationWithType * decl ) {1121 decl->fixUniqueId();1122 740 } 1123 741 -
src/SymTab/Validate.h
rfa2a3b1 r9939dc3 10 10 // Author : Richard C. Bilson 11 11 // Created On : Sun May 17 21:53:34 2015 12 // Last Modified By : Peter A. Buhr13 // Last Modified On : Sat Jul 22 09:46:07 201714 // Update Count : 412 // Last Modified By : Andrew Beach 13 // Last Modified On : Tue May 17 14:35:00 2022 14 // Update Count : 5 15 15 // 16 16 … … 33 33 /// Normalizes struct and function declarations 34 34 void validate( std::list< Declaration * > &translationUnit, bool doDebug = false ); 35 void validateType( Type *type, const Indexer *indexer );36 35 37 36 // Sub-passes of validate. … … 42 41 void validate_E( std::list< Declaration * > &translationUnit ); 43 42 void validate_F( std::list< Declaration * > &translationUnit ); 44 void linkReferenceToTypes( std::list< Declaration * > &translationUnit );45 46 const ast::Type * validateType(47 const CodeLocation & loc, const ast::Type * type, const ast::SymbolTable & symtab );48 43 } // namespace SymTab 49 44 -
src/SymTab/module.mk
rfa2a3b1 r9939dc3 11 11 ## Created On : Mon Jun 1 17:49:17 2015 12 12 ## Last Modified By : Andrew Beach 13 ## Last Modified On : T hr Aug 10 16:08:00 201714 ## Update Count : 413 ## Last Modified On : Tue May 17 14:46:00 2022 14 ## Update Count : 5 15 15 ############################################################################### 16 16 17 17 SRC_SYMTAB = \ 18 19 20 21 22 23 24 25 26 27 SymTab/Validate.cc \28 SymTab/Validate.h18 SymTab/Autogen.cc \ 19 SymTab/Autogen.h \ 20 SymTab/FixFunction.cc \ 21 SymTab/FixFunction.h \ 22 SymTab/Indexer.cc \ 23 SymTab/Indexer.h \ 24 SymTab/Mangler.cc \ 25 SymTab/ManglerCommon.cc \ 26 SymTab/Mangler.h \ 27 SymTab/ValidateType.cc \ 28 SymTab/ValidateType.h 29 29 30 SRC += $(SRC_SYMTAB) 30 SRC += $(SRC_SYMTAB) \ 31 SymTab/Validate.cc \ 32 SymTab/Validate.h 33 31 34 SRCDEMANGLE += $(SRC_SYMTAB) \ 32 35 SymTab/Demangle.cc \
Note: See TracChangeset
for help on using the changeset viewer.