[cfe-commits] r95597 - in /cfe/trunk: include/clang/AST/Type.h lib/AST/ASTContext.cpp lib/AST/Type.cpp

Douglas Gregor dgregor at apple.com
Mon Feb 8 14:59:26 PST 2010


Author: dgregor
Date: Mon Feb  8 16:59:26 2010
New Revision: 95597

URL: http://llvm.org/viewvc/llvm-project?rev=95597&view=rev
Log:
Eliminate a pointer of storage in each ObjCInterfaceType and
ObjCObjectPointerType AST node by allocating the list of protocols
after the type node itself. No functionality change.

Modified:
    cfe/trunk/include/clang/AST/Type.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/Type.cpp

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=95597&r1=95596&r2=95597&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Mon Feb  8 16:59:26 2010
@@ -2559,12 +2559,12 @@
 class ObjCInterfaceType : public Type, public llvm::FoldingSetNode {
   ObjCInterfaceDecl *Decl;
 
-  // List of protocols for this protocol conforming object type
-  // List is sorted on protocol name. No protocol is enterred more than once.
-  ObjCProtocolDecl **Protocols;
+  /// \brief The number of protocols stored after the ObjCInterfaceType node.
+  /// The list of protocols is sorted on protocol name. No protocol is enterred 
+  /// more than once.
   unsigned NumProtocols;
 
-  ObjCInterfaceType(ASTContext &Ctx, QualType Canonical, ObjCInterfaceDecl *D,
+  ObjCInterfaceType(QualType Canonical, ObjCInterfaceDecl *D,
                     ObjCProtocolDecl **Protos, unsigned NumP);
   friend class ASTContext;  // ASTContext creates these.
 public:
@@ -2580,10 +2580,10 @@
   /// list of protocols qualifying this interface.
   typedef ObjCProtocolDecl*  const * qual_iterator;
   qual_iterator qual_begin() const {
-    return Protocols;
+    return reinterpret_cast<qual_iterator>(this + 1);
   }
   qual_iterator qual_end() const   {
-    return Protocols ? Protocols + NumProtocols : 0;
+    return qual_begin() + NumProtocols;
   }
   bool qual_empty() const { return NumProtocols == 0; }
 
@@ -2593,7 +2593,8 @@
   void Profile(llvm::FoldingSetNodeID &ID);
   static void Profile(llvm::FoldingSetNodeID &ID,
                       const ObjCInterfaceDecl *Decl,
-                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
+                      ObjCProtocolDecl * const *protocols, 
+                      unsigned NumProtocols);
 
   virtual Linkage getLinkage() const;
 
@@ -2611,12 +2612,14 @@
 class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode {
   QualType PointeeType; // A builtin or interface type.
 
-  // List of protocols for this protocol conforming object type
-  // List is sorted on protocol name. No protocol is entered more than once.
-  ObjCProtocolDecl **Protocols;
+  /// \brief The number of protocols stored after the ObjCObjectPointerType 
+  /// node.
+  ///
+  /// The list of protocols is sorted on protocol name. No protocol is enterred 
+  /// more than once.
   unsigned NumProtocols;
 
-  ObjCObjectPointerType(ASTContext &Ctx, QualType Canonical, QualType T,
+  ObjCObjectPointerType(QualType Canonical, QualType T,
                         ObjCProtocolDecl **Protos, unsigned NumP);
   friend class ASTContext;  // ASTContext creates these.
 
@@ -2663,10 +2666,10 @@
   typedef ObjCProtocolDecl*  const * qual_iterator;
 
   qual_iterator qual_begin() const {
-    return Protocols;
+    return reinterpret_cast<qual_iterator> (this + 1);
   }
   qual_iterator qual_end() const   {
-    return Protocols ? Protocols + NumProtocols : NULL;
+    return qual_begin() + NumProtocols;
   }
   bool qual_empty() const { return NumProtocols == 0; }
 
@@ -2681,7 +2684,8 @@
 
   void Profile(llvm::FoldingSetNodeID &ID);
   static void Profile(llvm::FoldingSetNodeID &ID, QualType T,
-                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
+                      ObjCProtocolDecl *const *protocols, 
+                      unsigned NumProtocols);
   static bool classof(const Type *T) {
     return T->getTypeClass() == ObjCObjectPointer;
   }

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=95597&r1=95596&r2=95597&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Mon Feb  8 16:59:26 2010
@@ -2163,10 +2163,14 @@
     ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
   }
 
-  // No Match;
-  ObjCObjectPointerType *QType = new (*this, TypeAlignment)
-    ObjCObjectPointerType(*this, Canonical, InterfaceT, Protocols,
-                          NumProtocols);
+  // No match.
+  unsigned Size = sizeof(ObjCObjectPointerType) 
+                + NumProtocols * sizeof(ObjCProtocolDecl *);
+  void *Mem = Allocate(Size, TypeAlignment);
+  ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical, 
+                                                                 InterfaceT, 
+                                                                 Protocols,
+                                                                 NumProtocols);
 
   Types.push_back(QType);
   ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
@@ -2199,9 +2203,13 @@
     ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
   }
 
-  ObjCInterfaceType *QType = new (*this, TypeAlignment)
-    ObjCInterfaceType(*this, Canonical, const_cast<ObjCInterfaceDecl*>(Decl),
-                      Protocols, NumProtocols);
+  unsigned Size = sizeof(ObjCInterfaceType) 
+    + NumProtocols * sizeof(ObjCProtocolDecl *);
+  void *Mem = Allocate(Size, TypeAlignment);
+  ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical, 
+                                        const_cast<ObjCInterfaceDecl*>(Decl),
+                                                         Protocols, 
+                                                         NumProtocols);
 
   Types.push_back(QType);
   ObjCInterfaceTypes.InsertNode(QType, InsertPos);

Modified: cfe/trunk/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Type.cpp?rev=95597&r1=95596&r2=95597&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Mon Feb  8 16:59:26 2010
@@ -339,21 +339,18 @@
   return 0;
 }
 
-ObjCInterfaceType::ObjCInterfaceType(ASTContext &Ctx, QualType Canonical,
+ObjCInterfaceType::ObjCInterfaceType(QualType Canonical,
                                      ObjCInterfaceDecl *D,
                                      ObjCProtocolDecl **Protos, unsigned NumP) :
   Type(ObjCInterface, Canonical, /*Dependent=*/false),
-  Decl(D), Protocols(0), NumProtocols(NumP)
+  Decl(D), NumProtocols(NumP)
 {
-  if (NumProtocols) {
-    Protocols = new (Ctx) ObjCProtocolDecl*[NumProtocols];
-    memcpy(Protocols, Protos, NumProtocols * sizeof(*Protocols));
-  }
+  if (NumProtocols)
+    memcpy(reinterpret_cast<ObjCProtocolDecl**>(this + 1), Protos, 
+           NumProtocols * sizeof(*Protos));
 }
 
 void ObjCInterfaceType::Destroy(ASTContext& C) {
-  if (Protocols)
-    C.Deallocate(Protocols);
   this->~ObjCInterfaceType();
   C.Deallocate(this);
 }
@@ -372,22 +369,18 @@
   return getAsObjCQualifiedInterfaceType() != 0;
 }
 
-ObjCObjectPointerType::ObjCObjectPointerType(ASTContext &Ctx,
-                                             QualType Canonical, QualType T,
+ObjCObjectPointerType::ObjCObjectPointerType(QualType Canonical, QualType T,
                                              ObjCProtocolDecl **Protos,
                                              unsigned NumP) :
   Type(ObjCObjectPointer, Canonical, /*Dependent=*/false),
-  PointeeType(T), Protocols(NULL), NumProtocols(NumP)
+  PointeeType(T), NumProtocols(NumP)
 {
-  if (NumProtocols) {
-    Protocols = new (Ctx) ObjCProtocolDecl*[NumProtocols];
-    memcpy(Protocols, Protos, NumProtocols * sizeof(*Protocols));
-  }
+  if (NumProtocols)
+    memcpy(reinterpret_cast<ObjCProtocolDecl **>(this + 1), Protos, 
+           NumProtocols * sizeof(*Protos));
 }
 
 void ObjCObjectPointerType::Destroy(ASTContext& C) {
-  if (Protocols)
-    C.Deallocate(Protocols);
   this->~ObjCObjectPointerType();
   C.Deallocate(this);
 }
@@ -851,7 +844,8 @@
 }
 
 void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID,
-                                    QualType OIT, ObjCProtocolDecl **protocols,
+                                    QualType OIT, 
+                                    ObjCProtocolDecl * const *protocols,
                                     unsigned NumProtocols) {
   ID.AddPointer(OIT.getAsOpaquePtr());
   for (unsigned i = 0; i != NumProtocols; i++)
@@ -859,10 +853,7 @@
 }
 
 void ObjCObjectPointerType::Profile(llvm::FoldingSetNodeID &ID) {
-  if (getNumProtocols())
-    Profile(ID, getPointeeType(), &Protocols[0], getNumProtocols());
-  else
-    Profile(ID, getPointeeType(), 0, 0);
+  Profile(ID, getPointeeType(), qual_begin(), getNumProtocols());
 }
 
 /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
@@ -1050,7 +1041,7 @@
 
 void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
                                          const ObjCInterfaceDecl *Decl,
-                                         ObjCProtocolDecl **protocols,
+                                         ObjCProtocolDecl * const *protocols,
                                          unsigned NumProtocols) {
   ID.AddPointer(Decl);
   for (unsigned i = 0; i != NumProtocols; i++)
@@ -1058,10 +1049,7 @@
 }
 
 void ObjCInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
-  if (getNumProtocols())
-    Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
-  else
-    Profile(ID, getDecl(), 0, 0);
+  Profile(ID, getDecl(), qual_begin(), getNumProtocols());
 }
 
 Linkage Type::getLinkage() const { 





More information about the cfe-commits mailing list