From 7a27ea52b7bd635c89bec5a9c521a3bf7d204238 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 12 Aug 2011 06:17:30 +0000 Subject: [PATCH] Switch the Objective-C 'SEL' type over to a predefined type in the AST file format, lazily generating the actual declaration in ASTContext as needed. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@137434 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/ASTContext.h | 18 ++++++++++----- include/clang/Serialization/ASTBitCodes.h | 27 ++++++++++++----------- lib/AST/ASTContext.cpp | 17 +++++++++----- lib/Sema/Sema.cpp | 19 +++++----------- lib/Serialization/ASTReader.cpp | 9 ++++---- lib/Serialization/ASTWriter.cpp | 3 ++- 6 files changed, 49 insertions(+), 44 deletions(-) diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index 4d5ba677a6..2625f81eb7 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -185,10 +185,9 @@ class ASTContext : public llvm::RefCountedBase { /// \brief The typedef for the predefined 'id' type. mutable TypedefDecl *ObjCIdDecl; - /// ObjCSelType - another pseudo built-in typedef type (set by Sema). - QualType ObjCSelTypedefType; + /// \brief The typedef for the predefined 'SEL' type. + mutable TypedefDecl *ObjCSelDecl; - /// ObjCProtoType - another pseudo built-in typedef type (set by Sema). QualType ObjCProtoType; const RecordType *ProtoStructType; @@ -961,9 +960,16 @@ public: QualType getObjCIdType() const { return getTypeDeclType(getObjCIdDecl()); } + + /// \brief Retrieve the typedef corresponding to the predefined 'SEL' type + /// in Objective-C. + TypedefDecl *getObjCSelDecl() const; - void setObjCSelType(QualType T); - QualType getObjCSelType() const { return ObjCSelTypedefType; } + /// \brief Retrieve the type that corresponds to the predefined Objective-C + /// 'SEL' type. + QualType getObjCSelType() const { + return getTypeDeclType(getObjCSelDecl()); + } void setObjCProtoType(QualType QT); QualType getObjCProtoType() const { return ObjCProtoType; } @@ -1431,7 +1437,7 @@ public: return T == getObjCClassType(); } bool isObjCSelType(QualType T) const { - return T == ObjCSelTypedefType; + return T == getObjCSelType(); } bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h index c4a7f86622..56a0f52920 100644 --- a/include/clang/Serialization/ASTBitCodes.h +++ b/include/clang/Serialization/ASTBitCodes.h @@ -640,26 +640,24 @@ namespace clang { enum SpecialTypeIDs { /// \brief __builtin_va_list SPECIAL_TYPE_BUILTIN_VA_LIST = 0, - /// \brief Objective-C selector type - SPECIAL_TYPE_OBJC_SELECTOR = 1, /// \brief Objective-C Protocol type - SPECIAL_TYPE_OBJC_PROTOCOL = 2, + SPECIAL_TYPE_OBJC_PROTOCOL = 1, /// \brief CFConstantString type - SPECIAL_TYPE_CF_CONSTANT_STRING = 3, + SPECIAL_TYPE_CF_CONSTANT_STRING = 2, /// \brief C FILE typedef type - SPECIAL_TYPE_FILE = 4, + SPECIAL_TYPE_FILE = 3, /// \brief C jmp_buf typedef type - SPECIAL_TYPE_jmp_buf = 5, + SPECIAL_TYPE_jmp_buf = 4, /// \brief C sigjmp_buf typedef type - SPECIAL_TYPE_sigjmp_buf = 6, + SPECIAL_TYPE_sigjmp_buf = 5, /// \brief Objective-C "id" redefinition type - SPECIAL_TYPE_OBJC_ID_REDEFINITION = 7, + SPECIAL_TYPE_OBJC_ID_REDEFINITION = 6, /// \brief Objective-C "Class" redefinition type - SPECIAL_TYPE_OBJC_CLASS_REDEFINITION = 8, + SPECIAL_TYPE_OBJC_CLASS_REDEFINITION = 7, /// \brief Objective-C "SEL" redefinition type - SPECIAL_TYPE_OBJC_SEL_REDEFINITION = 9, + SPECIAL_TYPE_OBJC_SEL_REDEFINITION = 8, /// \brief Whether __[u]int128_t identifier is installed. - SPECIAL_TYPE_INT128_INSTALLED = 10 + SPECIAL_TYPE_INT128_INSTALLED = 9 }; /// \brief Predefined declaration IDs. @@ -678,15 +676,18 @@ namespace clang { /// \brief The Objective-C 'id' type. PREDEF_DECL_OBJC_ID_ID = 2, + /// \brief The Objective-C 'SEL' type. + PREDEF_DECL_OBJC_SEL_ID = 3, + /// \brief The Objective-C 'Class' type. - PREDEF_DECL_OBJC_CLASS_ID = 3 + PREDEF_DECL_OBJC_CLASS_ID = 4 }; /// \brief The number of declaration IDs that are predefined. /// /// For more information about predefined declarations, see the /// \c PredefinedDeclIDs type and the PREDEF_DECL_*_ID constants. - const unsigned int NUM_PREDEF_DECL_IDS = 4; + const unsigned int NUM_PREDEF_DECL_IDS = 5; /// \brief Record codes for each kind of declaration. /// diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 5951fcfe0e..8a4c9d3338 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -222,7 +222,7 @@ ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, DependentTemplateSpecializationTypes(this_()), SubstTemplateTemplateParmPacks(this_()), GlobalNestedNameSpecifier(0), IsInt128Installed(false), - ObjCIdDecl(0), ObjCClassDecl(0), + ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), CFConstantStringTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), sigjmp_bufDecl(0), BlockDescriptorType(0), @@ -430,9 +430,6 @@ void ASTContext::InitBuiltinTypes() { BuiltinVaListType = QualType(); - // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). - ObjCSelTypedefType = QualType(); - // Builtin types for 'id', 'Class', and 'SEL'. InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); @@ -4632,8 +4629,16 @@ TypedefDecl *ASTContext::getObjCIdDecl() const { return ObjCIdDecl; } -void ASTContext::setObjCSelType(QualType T) { - ObjCSelTypedefType = T; +TypedefDecl *ASTContext::getObjCSelDecl() const { + if (!ObjCSelDecl) { + QualType SelT = getPointerType(ObjCBuiltinSelTy); + TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); + ObjCSelDecl = TypedefDecl::Create(const_cast(*this), + getTranslationUnitDecl(), + SourceLocation(), SourceLocation(), + &Idents.get("SEL"), SelInfo); + } + return ObjCSelDecl; } void ASTContext::setObjCProtoType(QualType QT) { diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp index 2377d186ef..a4abae42ac 100644 --- a/lib/Sema/Sema.cpp +++ b/lib/Sema/Sema.cpp @@ -84,19 +84,6 @@ void Sema::ActOnTranslationUnitScope(Scope *S) { if (!PP.getLangOptions().ObjC1) return; - // Built-in ObjC types may already be set by ASTReader (hence isNull checks). - if (Context.getObjCSelType().isNull()) { - // Create the built-in typedef for 'SEL'. - QualType SelT = Context.getPointerType(Context.ObjCBuiltinSelTy); - TypeSourceInfo *SelInfo = Context.getTrivialTypeSourceInfo(SelT); - TypedefDecl *SelTypedef - = TypedefDecl::Create(Context, CurContext, - SourceLocation(), SourceLocation(), - &Context.Idents.get("SEL"), SelInfo); - PushOnScopeChains(SelTypedef, TUScope); - Context.setObjCSelType(Context.getTypeDeclType(SelTypedef)); - } - // Synthesize "@class Protocol; if (Context.getObjCProtoType().isNull()) { ObjCInterfaceDecl *ProtocolDecl = @@ -157,6 +144,12 @@ void Sema::Initialize() { // Initialize predefined Objective-C types: if (PP.getLangOptions().ObjC1) { + // If 'SEL' does not yet refer to any declarations, make it refer to the + // predefined 'SEL'. + DeclarationName SEL = &Context.Idents.get("SEL"); + if (IdentifierResolver::begin(SEL) == IdentifierResolver::end()) + PushOnScopeChains(Context.getObjCSelDecl(), TUScope); + // If 'id' does not yet refer to any declarations, make it refer to the // predefined 'id'. DeclarationName Id = &Context.Idents.get("id"); diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 2d4cde05a9..c048f58fdb 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -2987,11 +2987,6 @@ void ASTReader::InitializeContext(ASTContext &Ctx) { GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST])); } - if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR]) { - if (Context->ObjCSelTypedefType.isNull()) - Context->ObjCSelTypedefType = GetType(Sel); - } - if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) { if (Context->ObjCProtoType.isNull()) Context->ObjCProtoType = GetType(Proto); @@ -4220,6 +4215,10 @@ Decl *ASTReader::GetDecl(DeclID ID) { assert(Context && "No context available?"); return Context->getObjCIdDecl(); + case PREDEF_DECL_OBJC_SEL_ID: + assert(Context && "No context available?"); + return Context->getObjCSelDecl(); + case PREDEF_DECL_OBJC_CLASS_ID: assert(Context && "No context available?"); return Context->getObjCClassDecl(); diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index b92b760863..47bcd6fdee 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -2810,6 +2810,8 @@ void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls, DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID; if (Context.ObjCIdDecl) DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID; + if (Context.ObjCSelDecl) + DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID; if (Context.ObjCClassDecl) DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID; @@ -3019,7 +3021,6 @@ void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls, // Form the record of special types. RecordData SpecialTypes; AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes); - AddTypeRef(Context.ObjCSelTypedefType, SpecialTypes); AddTypeRef(Context.ObjCProtoType, SpecialTypes); AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes); AddTypeRef(Context.getFILEType(), SpecialTypes); -- 2.40.0