[cfe-commits] r137429 - in /cfe/trunk: 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

Douglas Gregor dgregor at apple.com
Thu Aug 11 22:46:02 PDT 2011


Author: dgregor
Date: Fri Aug 12 00:46:01 2011
New Revision: 137429

URL: http://llvm.org/viewvc/llvm-project?rev=137429&view=rev
Log:
Move the creation of the predefined typedef for Objective-C's 'id'
type over into the AST context, then make that declaration a
predefined declaration in the AST format. This ensures that different
AST files will at least agree on the (global) declaration ID for 'id',
and eliminates one of the "special" types in the AST file format.


Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/Serialization/ASTBitCodes.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/lib/Serialization/ASTReader.cpp
    cfe/trunk/lib/Serialization/ASTWriter.cpp

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=137429&r1=137428&r2=137429&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Fri Aug 12 00:46:01 2011
@@ -182,9 +182,9 @@
   /// a builtin that takes a valist is encountered.
   QualType BuiltinVaListType;
 
-  /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
-  QualType ObjCIdTypedefType;
-
+  /// \brief The typedef for the predefined 'id' type.
+  mutable TypedefDecl *ObjCIdDecl;
+  
   /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
   QualType ObjCSelTypedefType;
 
@@ -952,11 +952,16 @@
   bool isInt128Installed() const { return IsInt128Installed; }
   void setInt128Installed() { IsInt128Installed = true; }
 
+  /// \brief Retrieve the typedef corresponding to the predefined 'id' type
+  /// in Objective-C.
+  TypedefDecl *getObjCIdDecl() const;
+  
   /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by
   /// Sema.  id is always a (typedef for a) pointer type, a pointer to a struct.
-  QualType getObjCIdType() const { return ObjCIdTypedefType; }
-  void setObjCIdType(QualType T);
-
+  QualType getObjCIdType() const {
+    return getTypeDeclType(getObjCIdDecl());
+  }
+  
   void setObjCSelType(QualType T);
   QualType getObjCSelType() const { return ObjCSelTypedefType; }
 
@@ -1415,7 +1420,7 @@
   bool typesAreBlockPointerCompatible(QualType, QualType); 
 
   bool isObjCIdType(QualType T) const {
-    return T == ObjCIdTypedefType;
+    return T == getObjCIdType();
   }
   bool isObjCClassType(QualType T) const {
     return T == ObjCClassTypedefType;

Modified: cfe/trunk/include/clang/Serialization/ASTBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTBitCodes.h?rev=137429&r1=137428&r2=137429&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTBitCodes.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTBitCodes.h Fri Aug 12 00:46:01 2011
@@ -640,30 +640,28 @@
     enum SpecialTypeIDs {
       /// \brief __builtin_va_list
       SPECIAL_TYPE_BUILTIN_VA_LIST             = 0,
-      /// \brief Objective-C "id" type
-      SPECIAL_TYPE_OBJC_ID                     = 1,
       /// \brief Objective-C selector type
-      SPECIAL_TYPE_OBJC_SELECTOR               = 2,
+      SPECIAL_TYPE_OBJC_SELECTOR               = 1,
       /// \brief Objective-C Protocol type
-      SPECIAL_TYPE_OBJC_PROTOCOL               = 3,
+      SPECIAL_TYPE_OBJC_PROTOCOL               = 2,
       /// \brief Objective-C Class type
-      SPECIAL_TYPE_OBJC_CLASS                  = 4,
+      SPECIAL_TYPE_OBJC_CLASS                  = 3,
       /// \brief CFConstantString type
-      SPECIAL_TYPE_CF_CONSTANT_STRING          = 5,
+      SPECIAL_TYPE_CF_CONSTANT_STRING          = 4,
       /// \brief C FILE typedef type
-      SPECIAL_TYPE_FILE                        = 6,
+      SPECIAL_TYPE_FILE                        = 5,
       /// \brief C jmp_buf typedef type
-      SPECIAL_TYPE_jmp_buf                     = 7,
+      SPECIAL_TYPE_jmp_buf                     = 6,
       /// \brief C sigjmp_buf typedef type
-      SPECIAL_TYPE_sigjmp_buf                  = 8,
+      SPECIAL_TYPE_sigjmp_buf                  = 7,
       /// \brief Objective-C "id" redefinition type
-      SPECIAL_TYPE_OBJC_ID_REDEFINITION        = 9,
+      SPECIAL_TYPE_OBJC_ID_REDEFINITION        = 8,
       /// \brief Objective-C "Class" redefinition type
-      SPECIAL_TYPE_OBJC_CLASS_REDEFINITION     = 10,
+      SPECIAL_TYPE_OBJC_CLASS_REDEFINITION     = 9,
       /// \brief Objective-C "SEL" redefinition type
-      SPECIAL_TYPE_OBJC_SEL_REDEFINITION       = 11,
+      SPECIAL_TYPE_OBJC_SEL_REDEFINITION       = 10,
       /// \brief Whether __[u]int128_t identifier is installed.
-      SPECIAL_TYPE_INT128_INSTALLED            = 12
+      SPECIAL_TYPE_INT128_INSTALLED            = 11
     };
 
     /// \brief Predefined declaration IDs.
@@ -677,14 +675,17 @@
       PREDEF_DECL_NULL_ID       = 0,
       
       /// \brief The translation unit.
-      PREDEF_DECL_TRANSLATION_UNIT_ID = 1
+      PREDEF_DECL_TRANSLATION_UNIT_ID = 1,
+      
+      /// \brief The Objective-C 'id' type.
+      PREDEF_DECL_OBJC_ID_ID = 2
     };
 
     /// \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 = 2;
+    const unsigned int NUM_PREDEF_DECL_IDS = 3;
     
     /// \brief Record codes for each kind of declaration.
     ///

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=137429&r1=137428&r2=137429&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Fri Aug 12 00:46:01 2011
@@ -222,7 +222,7 @@
   DependentTemplateSpecializationTypes(this_()),
   SubstTemplateTemplateParmPacks(this_()),
   GlobalNestedNameSpecifier(0), IsInt128Installed(false),
-  CFConstantStringTypeDecl(0),
+  ObjCIdDecl(0), CFConstantStringTypeDecl(0),
   FILEDecl(0), 
   jmp_bufDecl(0), sigjmp_bufDecl(0), BlockDescriptorType(0), 
   BlockDescriptorExtendedType(0), cudaConfigureCallDecl(0),
@@ -430,7 +430,6 @@
   BuiltinVaListType = QualType();
 
   // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
-  ObjCIdTypedefType = QualType();
   ObjCClassTypedefType = QualType();
   ObjCSelTypedefType = QualType();
 
@@ -4619,8 +4618,18 @@
   BuiltinVaListType = T;
 }
 
-void ASTContext::setObjCIdType(QualType T) {
-  ObjCIdTypedefType = T;
+TypedefDecl *ASTContext::getObjCIdDecl() const {
+  if (!ObjCIdDecl) {
+    QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0);
+    T = getObjCObjectPointerType(T);
+    TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T);
+    ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
+                                     getTranslationUnitDecl(),
+                                     SourceLocation(), SourceLocation(),
+                                     &Idents.get("id"), IdInfo);
+  }
+  
+  return ObjCIdDecl;
 }
 
 void ASTContext::setObjCSelType(QualType T) {

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=137429&r1=137428&r2=137429&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Fri Aug 12 00:46:01 2011
@@ -106,18 +106,7 @@
     Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
     PushOnScopeChains(ProtocolDecl, TUScope, false);
   }
-  // Create the built-in typedef for 'id'.
-  if (Context.getObjCIdType().isNull()) {
-    QualType T = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, 0, 0);
-    T = Context.getObjCObjectPointerType(T);
-    TypeSourceInfo *IdInfo = Context.getTrivialTypeSourceInfo(T);
-    TypedefDecl *IdTypedef
-      = TypedefDecl::Create(Context, CurContext,
-                            SourceLocation(), SourceLocation(),
-                            &Context.Idents.get("id"), IdInfo);
-    PushOnScopeChains(IdTypedef, TUScope);
-    Context.setObjCIdType(Context.getTypeDeclType(IdTypedef));
-  }
+  
   // Create the built-in typedef for 'Class'.
   if (Context.getObjCClassType().isNull()) {
     QualType T = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, 0, 0);
@@ -178,6 +167,15 @@
   if (ExternalSemaSource *ExternalSema
       = dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
     ExternalSema->InitializeSema(*this);
+
+  // Initialize predefined Objective-C types:
+  if (PP.getLangOptions().ObjC1) {
+    // If 'id' does not yet refer to any declarations, make it refer to the
+    // predefined 'id'.
+    DeclarationName Id = &Context.Idents.get("id");
+    if (IdentifierResolver::begin(Id) == IdentifierResolver::end())
+      PushOnScopeChains(Context.getObjCIdDecl(), TUScope);
+  }
 }
 
 Sema::~Sema() {

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=137429&r1=137428&r2=137429&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Fri Aug 12 00:46:01 2011
@@ -2987,11 +2987,6 @@
       GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
   }
   
-  if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID]) {
-    if (Context->ObjCIdTypedefType.isNull())
-      Context->ObjCIdTypedefType = GetType(Id);
-  }
-  
   if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR]) {
     if (Context->ObjCSelTypedefType.isNull())
       Context->ObjCSelTypedefType = GetType(Sel);
@@ -4225,6 +4220,10 @@
     case PREDEF_DECL_TRANSLATION_UNIT_ID:
       assert(Context && "No context available?");
       return Context->getTranslationUnitDecl();
+        
+    case PREDEF_DECL_OBJC_ID_ID:
+      assert(Context && "No context available?");
+      return Context->getObjCIdDecl();
     }
     
     return 0;

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=137429&r1=137428&r2=137429&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Fri Aug 12 00:46:01 2011
@@ -2808,7 +2808,9 @@
 
   // Set up predefined declaration IDs.
   DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
-
+  if (Context.ObjCIdDecl)
+    DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_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
@@ -3015,7 +3017,6 @@
   // Form the record of special types.
   RecordData SpecialTypes;
   AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
-  AddTypeRef(Context.ObjCIdTypedefType, SpecialTypes);
   AddTypeRef(Context.ObjCSelTypedefType, SpecialTypes);
   AddTypeRef(Context.ObjCProtoType, SpecialTypes);
   AddTypeRef(Context.ObjCClassTypedefType, SpecialTypes);





More information about the cfe-commits mailing list