From fd6a0887a099256c35a5b23e9afd517ffe95fa0a Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Mon, 8 Feb 2010 22:59:26 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@95597 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Type.h | 32 ++++++++++++++++-------------- lib/AST/ASTContext.cpp | 22 ++++++++++++++------- lib/AST/Type.cpp | 42 ++++++++++++++-------------------------- 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index 911c30d65b..47b7d4811c 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -2559,12 +2559,12 @@ public: 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 @@ public: /// list of protocols qualifying this interface. typedef ObjCProtocolDecl* const * qual_iterator; qual_iterator qual_begin() const { - return Protocols; + return reinterpret_cast(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 @@ public: 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 @@ public: 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 @@ public: typedef ObjCProtocolDecl* const * qual_iterator; qual_iterator qual_begin() const { - return Protocols; + return reinterpret_cast (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 @@ public: 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; } diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 386a5f3c7b..c9d85c84f9 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -2163,10 +2163,14 @@ QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, 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 @@ QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos); } - ObjCInterfaceType *QType = new (*this, TypeAlignment) - ObjCInterfaceType(*this, Canonical, const_cast(Decl), - Protocols, NumProtocols); + unsigned Size = sizeof(ObjCInterfaceType) + + NumProtocols * sizeof(ObjCProtocolDecl *); + void *Mem = Allocate(Size, TypeAlignment); + ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical, + const_cast(Decl), + Protocols, + NumProtocols); Types.push_back(QType); ObjCInterfaceTypes.InsertNode(QType, InsertPos); diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp index 92498be2f5..52d734ca7e 100644 --- a/lib/AST/Type.cpp +++ b/lib/AST/Type.cpp @@ -339,21 +339,18 @@ const RecordType *Type::getAsUnionType() const { 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(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 @@ bool Type::isObjCQualifiedInterfaceType() const { 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(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 FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) { } 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, } 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 @@ QualType QualifierCollector::apply(const Type *T) const { 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, } 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 { -- 2.40.0