]> granicus.if.org Git - clang/commitdiff
Switch the Objective-C 'Class' type over to a predefined type in the
authorDouglas Gregor <dgregor@apple.com>
Fri, 12 Aug 2011 05:59:41 +0000 (05:59 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 12 Aug 2011 05:59:41 +0000 (05:59 +0000)
AST file format, lazily generating the actual declaration in
ASTContext as needed.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@137431 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ASTContext.h
include/clang/Serialization/ASTBitCodes.h
lib/AST/ASTContext.cpp
lib/Sema/Sema.cpp
lib/Serialization/ASTReader.cpp
lib/Serialization/ASTWriter.cpp

index 6feda32c546d36e6f783297994663378ca598e20..4d5ba677a683aae7e42234d246067b833e0bca07 100644 (file)
@@ -192,9 +192,9 @@ class ASTContext : public llvm::RefCountedBase<ASTContext> {
   QualType ObjCProtoType;
   const RecordType *ProtoStructType;
 
-  /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
-  QualType ObjCClassTypedefType;
-
+  /// \brief The typedef for the predefined 'Class' type.
+  mutable TypedefDecl *ObjCClassDecl;
+  
   // Typedefs which may be provided defining the structure of Objective-C
   // pseudo-builtins
   QualType ObjCIdRedefinitionType;
@@ -968,11 +968,16 @@ public:
   void setObjCProtoType(QualType QT);
   QualType getObjCProtoType() const { return ObjCProtoType; }
 
+  /// \brief Retrieve the typedef declaration corresponding to the predefined
+  /// Objective-C 'Class' type.
+  TypedefDecl *getObjCClassDecl() const;
+  
   /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by
   /// Sema.  'Class' is always a (typedef for a) pointer type, a pointer to a
   /// struct.
-  QualType getObjCClassType() const { return ObjCClassTypedefType; }
-  void setObjCClassType(QualType T);
+  QualType getObjCClassType() const { 
+    return getTypeDeclType(getObjCClassDecl());
+  }
 
   void setBuiltinVaListType(QualType T);
   QualType getBuiltinVaListType() const { return BuiltinVaListType; }
@@ -1423,7 +1428,7 @@ public:
     return T == getObjCIdType();
   }
   bool isObjCClassType(QualType T) const {
-    return T == ObjCClassTypedefType;
+    return T == getObjCClassType();
   }
   bool isObjCSelType(QualType T) const {
     return T == ObjCSelTypedefType;
index f9896e6ab8069394f5f11785a2607249a93e1a7a..c4a7f86622f72dc5939777924dca24a2a29f9ef7 100644 (file)
@@ -644,24 +644,22 @@ namespace clang {
       SPECIAL_TYPE_OBJC_SELECTOR               = 1,
       /// \brief Objective-C Protocol type
       SPECIAL_TYPE_OBJC_PROTOCOL               = 2,
-      /// \brief Objective-C Class type
-      SPECIAL_TYPE_OBJC_CLASS                  = 3,
       /// \brief CFConstantString type
-      SPECIAL_TYPE_CF_CONSTANT_STRING          = 4,
+      SPECIAL_TYPE_CF_CONSTANT_STRING          = 3,
       /// \brief C FILE typedef type
-      SPECIAL_TYPE_FILE                        = 5,
+      SPECIAL_TYPE_FILE                        = 4,
       /// \brief C jmp_buf typedef type
-      SPECIAL_TYPE_jmp_buf                     = 6,
+      SPECIAL_TYPE_jmp_buf                     = 5,
       /// \brief C sigjmp_buf typedef type
-      SPECIAL_TYPE_sigjmp_buf                  = 7,
+      SPECIAL_TYPE_sigjmp_buf                  = 6,
       /// \brief Objective-C "id" redefinition type
-      SPECIAL_TYPE_OBJC_ID_REDEFINITION        = 8,
+      SPECIAL_TYPE_OBJC_ID_REDEFINITION        = 7,
       /// \brief Objective-C "Class" redefinition type
-      SPECIAL_TYPE_OBJC_CLASS_REDEFINITION     = 9,
+      SPECIAL_TYPE_OBJC_CLASS_REDEFINITION     = 8,
       /// \brief Objective-C "SEL" redefinition type
-      SPECIAL_TYPE_OBJC_SEL_REDEFINITION       = 10,
+      SPECIAL_TYPE_OBJC_SEL_REDEFINITION       = 9,
       /// \brief Whether __[u]int128_t identifier is installed.
-      SPECIAL_TYPE_INT128_INSTALLED            = 11
+      SPECIAL_TYPE_INT128_INSTALLED            = 10
     };
 
     /// \brief Predefined declaration IDs.
@@ -678,14 +676,17 @@ namespace clang {
       PREDEF_DECL_TRANSLATION_UNIT_ID = 1,
       
       /// \brief The Objective-C 'id' type.
-      PREDEF_DECL_OBJC_ID_ID = 2
+      PREDEF_DECL_OBJC_ID_ID = 2,
+      
+      /// \brief The Objective-C 'Class' type.
+      PREDEF_DECL_OBJC_CLASS_ID = 3
     };
 
     /// \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 = 3;
+    const unsigned int NUM_PREDEF_DECL_IDS = 4;
     
     /// \brief Record codes for each kind of declaration.
     ///
index cf4535bd90ca46b7085d95bb0a6b1b5411b74f79..5951fcfe0e0f5482e050742516c8e4d151345d74 100644 (file)
@@ -222,7 +222,8 @@ ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
   DependentTemplateSpecializationTypes(this_()),
   SubstTemplateTemplateParmPacks(this_()),
   GlobalNestedNameSpecifier(0), IsInt128Installed(false),
-  ObjCIdDecl(0), CFConstantStringTypeDecl(0),
+  ObjCIdDecl(0), ObjCClassDecl(0),
+  CFConstantStringTypeDecl(0),
   FILEDecl(0), 
   jmp_bufDecl(0), sigjmp_bufDecl(0), BlockDescriptorType(0), 
   BlockDescriptorExtendedType(0), cudaConfigureCallDecl(0),
@@ -430,7 +431,6 @@ void ASTContext::InitBuiltinTypes() {
   BuiltinVaListType = QualType();
 
   // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
-  ObjCClassTypedefType = QualType();
   ObjCSelTypedefType = QualType();
 
   // Builtin types for 'id', 'Class', and 'SEL'.
@@ -4640,8 +4640,18 @@ void ASTContext::setObjCProtoType(QualType QT) {
   ObjCProtoType = QT;
 }
 
-void ASTContext::setObjCClassType(QualType T) {
-  ObjCClassTypedefType = T;
+TypedefDecl *ASTContext::getObjCClassDecl() const {
+  if (!ObjCClassDecl) {
+    QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0);
+    T = getObjCObjectPointerType(T);
+    TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T);
+    ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
+                                        getTranslationUnitDecl(),
+                                        SourceLocation(), SourceLocation(),
+                                        &Idents.get("Class"), ClassInfo);
+  }
+  
+  return ObjCClassDecl;
 }
 
 void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
index adc749dd70441039dd5f949def4f5bbbf3057e20..2377d186ef529371fb7a1fda828514a4564bfe8f 100644 (file)
@@ -105,19 +105,6 @@ void Sema::ActOnTranslationUnitScope(Scope *S) {
                                 SourceLocation(), true);
     Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
     PushOnScopeChains(ProtocolDecl, TUScope, false);
-  }
-  
-  // Create the built-in typedef for 'Class'.
-  if (Context.getObjCClassType().isNull()) {
-    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0);
-    T = Context.getObjCObjectPointerType(T);
-    TypeSourceInfo *ClassInfo = Context.getTrivialTypeSourceInfo(T);
-    TypedefDecl *ClassTypedef
-      = TypedefDecl::Create(Context, CurContext,
-                            SourceLocation(), SourceLocation(),
-                            &Context.Idents.get("Class"), ClassInfo);
-    PushOnScopeChains(ClassTypedef, TUScope);
-    Context.setObjCClassType(Context.getTypeDeclType(ClassTypedef));
   }  
 }
 
@@ -175,6 +162,11 @@ void Sema::Initialize() {
     DeclarationName Id = &Context.Idents.get("id");
     if (IdentifierResolver::begin(Id) == IdentifierResolver::end())
       PushOnScopeChains(Context.getObjCIdDecl(), TUScope);
+    
+    // Create the built-in typedef for 'Class'.
+    DeclarationName Class = &Context.Idents.get("Class");
+    if (IdentifierResolver::begin(Class) == IdentifierResolver::end())
+      PushOnScopeChains(Context.getObjCClassDecl(), TUScope);
   }
 }
 
index 2e9e1bee6842e96229bd8aa0db98c5e949864bf9..2d4cde05a9d53f151d1b6ed0aa771b18ca0f43f4 100644 (file)
@@ -2997,11 +2997,6 @@ void ASTReader::InitializeContext(ASTContext &Ctx) {
       Context->ObjCProtoType = GetType(Proto);
   }
   
-  if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS]) {
-    if (Context->ObjCClassTypedefType.isNull()) 
-      Context->ObjCClassTypedefType = GetType(Class);
-  }
-
   if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
     if (!Context->CFConstantStringTypeDecl)
       Context->setCFConstantStringType(GetType(String));
@@ -4224,6 +4219,10 @@ Decl *ASTReader::GetDecl(DeclID ID) {
     case PREDEF_DECL_OBJC_ID_ID:
       assert(Context && "No context available?");
       return Context->getObjCIdDecl();
+
+    case PREDEF_DECL_OBJC_CLASS_ID:
+      assert(Context && "No context available?");
+      return Context->getObjCClassDecl();
     }
     
     return 0;
index 2377369c74f78edc5dc6e475c83cabe829767386..b92b76086398e711a831e44824b6438db09339ca 100644 (file)
@@ -2810,7 +2810,9 @@ 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.ObjCClassDecl)
+    DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
+  
   if (!Chain) {
     // Make sure that we emit IdentifierInfos (and any attached
     // declarations) for builtins. We don't need to do this when we're
@@ -3019,7 +3021,6 @@ void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
   AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
   AddTypeRef(Context.ObjCSelTypedefType, SpecialTypes);
   AddTypeRef(Context.ObjCProtoType, SpecialTypes);
-  AddTypeRef(Context.ObjCClassTypedefType, SpecialTypes);
   AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
   AddTypeRef(Context.getFILEType(), SpecialTypes);
   AddTypeRef(Context.getjmp_bufType(), SpecialTypes);