]> granicus.if.org Git - clang/commitdiff
[TrailingObjects] Convert Decl* classes.
authorJames Y Knight <jyknight@google.com>
Tue, 29 Dec 2015 22:13:13 +0000 (22:13 +0000)
committerJames Y Knight <jyknight@google.com>
Tue, 29 Dec 2015 22:13:13 +0000 (22:13 +0000)
Also remove now-redundant explicit alignment specification on some of
the classes converted prior to TrailingObjects automatically ensuring
proper alignment.

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

12 files changed:
include/clang/AST/DeclCXX.h
include/clang/AST/DeclFriend.h
include/clang/AST/DeclGroup.h
include/clang/AST/DeclObjC.h
include/clang/AST/DeclOpenMP.h
include/clang/AST/DeclTemplate.h
lib/AST/DeclCXX.cpp
lib/AST/DeclFriend.cpp
lib/AST/DeclGroup.cpp
lib/AST/DeclObjC.cpp
lib/AST/DeclOpenMP.cpp
lib/Serialization/ASTReaderDecl.cpp

index 1c54e1bb62dbb1cfb634faf489a7a65406e6f385..c24823f7f17d8c6ee0dc2da739cfd06c9c34618e 100644 (file)
@@ -1888,7 +1888,8 @@ public:
 ///   B(A& a) : A(a), f(3.14159) { }
 /// };
 /// \endcode
-class CXXCtorInitializer {
+class CXXCtorInitializer final
+    : private llvm::TrailingObjects<CXXCtorInitializer, VarDecl *> {
   /// \brief Either the base class name/delegating constructor type (stored as
   /// a TypeSourceInfo*), an normal field (FieldDecl), or an anonymous field
   /// (IndirectFieldDecl*) being initialized.
@@ -2104,24 +2105,26 @@ public:
   /// describe an array member initialization.
   VarDecl *getArrayIndex(unsigned I) {
     assert(I < getNumArrayIndices() && "Out of bounds member array index");
-    return reinterpret_cast<VarDecl **>(this + 1)[I];
+    return getTrailingObjects<VarDecl *>()[I];
   }
   const VarDecl *getArrayIndex(unsigned I) const {
     assert(I < getNumArrayIndices() && "Out of bounds member array index");
-    return reinterpret_cast<const VarDecl * const *>(this + 1)[I];
+    return getTrailingObjects<VarDecl *>()[I];
   }
   void setArrayIndex(unsigned I, VarDecl *Index) {
     assert(I < getNumArrayIndices() && "Out of bounds member array index");
-    reinterpret_cast<VarDecl **>(this + 1)[I] = Index;
+    getTrailingObjects<VarDecl *>()[I] = Index;
   }
   ArrayRef<VarDecl *> getArrayIndexes() {
     assert(getNumArrayIndices() != 0 && "Getting indexes for non-array init");
-    return llvm::makeArrayRef(reinterpret_cast<VarDecl **>(this + 1),
+    return llvm::makeArrayRef(getTrailingObjects<VarDecl *>(),
                               getNumArrayIndices());
   }
 
   /// \brief Get the initializer.
   Expr *getInit() const { return static_cast<Expr*>(Init); }
+
+  friend TrailingObjects;
 };
 
 /// \brief Represents a C++ constructor within a class.
index 12b93b408a7037c99518e3fb08efe2843294b701..27b0388007a1cd1d0243ae9922ffb307f4583e33 100644 (file)
@@ -37,7 +37,9 @@ namespace clang {
 /// @endcode
 ///
 /// The semantic context of a friend decl is its declaring class.
-class FriendDecl : public Decl {
+class FriendDecl final
+    : public Decl,
+      private llvm::TrailingObjects<FriendDecl, TemplateParameterList *> {
   virtual void anchor();
 public:
   typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
@@ -62,14 +64,6 @@ private:
   //     template <class T> friend class A<T>::B;
   unsigned NumTPLists : 31;
 
-  // The tail-allocated friend type template parameter lists (if any).
-  TemplateParameterList* const *getTPLists() const {
-    return reinterpret_cast<TemplateParameterList* const *>(this + 1);
-  }
-  TemplateParameterList **getTPLists() {
-    return reinterpret_cast<TemplateParameterList**>(this + 1);
-  }
-
   friend class CXXRecordDecl::friend_iterator;
   friend class CXXRecordDecl;
 
@@ -83,7 +77,7 @@ private:
       UnsupportedFriend(false),
       NumTPLists(FriendTypeTPLists.size()) {
     for (unsigned i = 0; i < NumTPLists; ++i)
-      getTPLists()[i] = FriendTypeTPLists[i];
+      getTrailingObjects<TemplateParameterList *>()[i] = FriendTypeTPLists[i];
   }
 
   FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
@@ -118,7 +112,7 @@ public:
   }
   TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
     assert(N < NumTPLists);
-    return getTPLists()[N];
+    return getTrailingObjects<TemplateParameterList *>()[N];
   }
 
   /// If this friend declaration doesn't name a type, return the inner
@@ -148,9 +142,10 @@ public:
       return SourceRange(getFriendLoc(), ND->getLocEnd());
     }
     else if (TypeSourceInfo *TInfo = getFriendType()) {
-      SourceLocation StartL = (NumTPLists == 0)
-        ? getFriendLoc()
-        : getTPLists()[0]->getTemplateLoc();
+      SourceLocation StartL =
+          (NumTPLists == 0) ? getFriendLoc()
+                            : getTrailingObjects<TemplateParameterList *>()[0]
+                                  ->getTemplateLoc();
       return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
     }
     else
@@ -171,6 +166,7 @@ public:
 
   friend class ASTDeclReader;
   friend class ASTDeclWriter;
+  friend TrailingObjects;
 };
 
 /// An iterator over the friend declarations of a class.
index bd3dbd8fa7876ac9a70e98962b2e3430cc5af92e..c84bb5e604812d463dbd77eefd1a73db59183f4f 100644 (file)
@@ -15,6 +15,7 @@
 #define LLVM_CLANG_AST_DECLGROUP_H
 
 #include "llvm/Support/DataTypes.h"
+#include "llvm/Support/TrailingObjects.h"
 #include <cassert>
 
 namespace clang {
@@ -24,13 +25,9 @@ class Decl;
 class DeclGroup;
 class DeclGroupIterator;
 
-class DeclGroup {
+class DeclGroup final : private llvm::TrailingObjects<DeclGroup, Decl *> {
   // FIXME: Include a TypeSpecifier object.
-  union {
-    unsigned NumDecls;
-
-    Decl *Aligner;
-  };
+  unsigned NumDecls;
 
 private:
   DeclGroup() : NumDecls(0) {}
@@ -43,13 +40,15 @@ public:
 
   Decl*& operator[](unsigned i) {
     assert (i < NumDecls && "Out-of-bounds access.");
-    return ((Decl**) (this+1))[i];
+    return getTrailingObjects<Decl *>()[i];
   }
 
   Decl* const& operator[](unsigned i) const {
     assert (i < NumDecls && "Out-of-bounds access.");
-    return ((Decl* const*) (this+1))[i];
+    return getTrailingObjects<Decl *>()[i];
   }
+
+  friend TrailingObjects;
 };
 
 class DeclGroupRef {
index f1e48a1296e90378583bcc44827eb9c0d0868988..f46078f28a7d82c57c2b8a83912b44892c3d9e1a 100644 (file)
@@ -612,7 +612,8 @@ public:
 /// @interface NSArray<T> // stores the <T>
 /// @end
 /// \endcode
-class ObjCTypeParamList {
+class ObjCTypeParamList final
+    : private llvm::TrailingObjects<ObjCTypeParamList, ObjCTypeParamDecl *> {
   /// Stores the components of a SourceRange as a POD.
   struct PODSourceRange {
     unsigned Begin;
@@ -644,7 +645,7 @@ public:
   /// Iterate through the type parameters in the list.
   typedef ObjCTypeParamDecl **iterator;
 
-  iterator begin() { return reinterpret_cast<ObjCTypeParamDecl **>(this + 1); }
+  iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); }
 
   iterator end() { return begin() + size(); }
 
@@ -655,7 +656,7 @@ public:
   typedef ObjCTypeParamDecl * const *const_iterator;
 
   const_iterator begin() const {
-    return reinterpret_cast<ObjCTypeParamDecl * const *>(this + 1);
+    return getTrailingObjects<ObjCTypeParamDecl *>();
   }
 
   const_iterator end() const {
@@ -685,6 +686,7 @@ public:
   /// Gather the default set of type arguments to be substituted for
   /// these type parameters when dealing with an unspecialized type.
   void gatherDefaultTypeArgs(SmallVectorImpl<QualType> &typeArgs) const;
+  friend TrailingObjects;
 };
 
 /// ObjCContainerDecl - Represents a container for method declarations.
index 7f0616f1e603abd287021f007f9e6b6bc5d3f9da..598f418f7e3555edf68e0d7733dab5279a5c2a44 100644 (file)
@@ -33,8 +33,12 @@ class Expr;
 /// };
 /// \endcode
 ///
-class OMPThreadPrivateDecl : public Decl {
+class OMPThreadPrivateDecl final
+    : public Decl,
+      private llvm::TrailingObjects<OMPThreadPrivateDecl, Expr *> {
   friend class ASTDeclReader;
+  friend TrailingObjects;
+
   unsigned NumVars;
 
   virtual void anchor();
@@ -43,14 +47,11 @@ class OMPThreadPrivateDecl : public Decl {
     Decl(DK, DC, L), NumVars(0) { }
 
   ArrayRef<const Expr *> getVars() const {
-    return llvm::makeArrayRef(reinterpret_cast<const Expr * const *>(this + 1),
-                              NumVars);
+    return llvm::makeArrayRef(getTrailingObjects<Expr *>(), NumVars);
   }
 
   MutableArrayRef<Expr *> getVars() {
-    return MutableArrayRef<Expr *>(
-                           reinterpret_cast<Expr **>(this + 1),
-                           NumVars);
+    return MutableArrayRef<Expr *>(getTrailingObjects<Expr *>(), NumVars);
   }
 
   void setVars(ArrayRef<Expr *> VL);
index 8657baeb921191ed0203f9d6fb574de7d955a5e5..a9109ef114264331aedfdfd362b2cd3a487734ef 100644 (file)
@@ -45,7 +45,7 @@ typedef llvm::PointerUnion3<TemplateTypeParmDecl*, NonTypeTemplateParmDecl*,
 
 /// \brief Stores a list of template parameters for a TemplateDecl and its
 /// derived classes.
-class LLVM_ALIGNAS(/*alignof(void*)*/ LLVM_PTR_SIZE) TemplateParameterList final
+class TemplateParameterList final
     : private llvm::TrailingObjects<TemplateParameterList, NamedDecl *> {
 
   /// The location of the 'template' keyword.
@@ -169,7 +169,7 @@ public:
 };
 
 /// \brief A template argument list.
-class LLVM_ALIGNAS(/*alignof(uint64_t)*/ 8) TemplateArgumentList final
+class TemplateArgumentList final
     : private llvm::TrailingObjects<TemplateArgumentList, TemplateArgument> {
   /// \brief The template argument list.
   const TemplateArgument *Arguments;
@@ -553,8 +553,7 @@ public:
 ///     friend void foo<>(T);
 ///   };
 /// \endcode
-class LLVM_ALIGNAS(/*alignof(uint64_t)*/ 8)
-    DependentFunctionTemplateSpecializationInfo final
+class DependentFunctionTemplateSpecializationInfo final
     : private llvm::TrailingObjects<DependentFunctionTemplateSpecializationInfo,
                                     TemplateArgumentLoc,
                                     FunctionTemplateDecl *> {
index 32e64b6e4eca960a959bcbc9f79dfbc3c1ea96de..71bc247a007f95084b0cc3a798d3fe2a0dc6992f 100644 (file)
@@ -1675,8 +1675,8 @@ CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
     LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
     IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
 {
-  VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
-  memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
+  std::uninitialized_copy(Indices, Indices + NumIndices,
+                          getTrailingObjects<VarDecl *>());
 }
 
 CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
@@ -1686,8 +1686,7 @@ CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
                                                SourceLocation R,
                                                VarDecl **Indices,
                                                unsigned NumIndices) {
-  void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
-                               sizeof(VarDecl *) * NumIndices,
+  void *Mem = Context.Allocate(totalSizeToAlloc<VarDecl *>(NumIndices),
                                llvm::alignOf<CXXCtorInitializer>());
   return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
                                       Indices, NumIndices);
index a996cab093afb4e3e28614da468a53b60ba3abd4..121403b07e575f6f06bf9214c9497da1c453b26f 100644 (file)
@@ -46,7 +46,9 @@ FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
   }
 #endif
 
-  std::size_t Extra = FriendTypeTPLists.size() * sizeof(TemplateParameterList*);
+  std::size_t Extra =
+      FriendDecl::additionalSizeToAlloc<TemplateParameterList *>(
+          FriendTypeTPLists.size());
   FriendDecl *FD = new (C, DC, Extra) FriendDecl(DC, L, Friend, FriendL,
                                                  FriendTypeTPLists);
   cast<CXXRecordDecl>(DC)->pushFriendDecl(FD);
@@ -55,7 +57,8 @@ FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
 
 FriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, unsigned ID,
                                            unsigned FriendTypeNumTPLists) {
-  std::size_t Extra = FriendTypeNumTPLists * sizeof(TemplateParameterList*);
+  std::size_t Extra =
+      additionalSizeToAlloc<TemplateParameterList *>(FriendTypeNumTPLists);
   return new (C, ID, Extra) FriendDecl(EmptyShell(), FriendTypeNumTPLists);
 }
 
index 512837fdf3f484f19bc3fc315e86e4332eec060b..f162e6d40c48ef3ce6f0724b9e3177d1b0eb3146 100644 (file)
 using namespace clang;
 
 DeclGroup* DeclGroup::Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
-  static_assert(sizeof(DeclGroup) % llvm::AlignOf<void *>::Alignment == 0,
-                "Trailing data is unaligned!");
   assert(NumDecls > 1 && "Invalid DeclGroup");
-  unsigned Size = sizeof(DeclGroup) + sizeof(Decl*) * NumDecls;
+  unsigned Size = totalSizeToAlloc<Decl *>(NumDecls);
   void* Mem = C.Allocate(Size, llvm::AlignOf<DeclGroup>::Alignment);
   new (Mem) DeclGroup(NumDecls, Decls);
   return static_cast<DeclGroup*>(Mem);
@@ -30,5 +28,6 @@ DeclGroup* DeclGroup::Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
 DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) : NumDecls(numdecls) {
   assert(numdecls > 0);
   assert(decls);
-  memcpy(this+1, decls, numdecls * sizeof(*decls));
+  std::uninitialized_copy(decls, decls + numdecls,
+                          getTrailingObjects<Decl *>());
 }
index b5dc9e122c304170f301a5026230a4ebe4c13605..050a0f53f1e51014f8a607392070f7c3deec1ce3 100644 (file)
@@ -772,6 +772,10 @@ void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
   if (Params.empty() && SelLocs.empty())
     return;
 
+  static_assert(llvm::AlignOf<ParmVarDecl *>::Alignment >=
+                    llvm::AlignOf<SourceLocation>::Alignment,
+                "Alignment not sufficient for SourceLocation");
+
   unsigned Size = sizeof(ParmVarDecl *) * NumParams +
                   sizeof(SourceLocation) * SelLocs.size();
   ParamsAndSelLocs = C.Allocate(Size);
@@ -1326,13 +1330,9 @@ ObjCTypeParamList *ObjCTypeParamList::create(
                      SourceLocation lAngleLoc,
                      ArrayRef<ObjCTypeParamDecl *> typeParams,
                      SourceLocation rAngleLoc) {
-  unsigned size = sizeof(ObjCTypeParamList)
-                + sizeof(ObjCTypeParamDecl *) * typeParams.size();
-  static_assert(llvm::AlignOf<ObjCTypeParamList>::Alignment >=
-                    llvm::AlignOf<ObjCTypeParamDecl *>::Alignment,
-                "type parameter list needs greater alignment");
-  unsigned align = llvm::alignOf<ObjCTypeParamList>();
-  void *mem = ctx.Allocate(size, align);
+  void *mem =
+      ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
+                   llvm::alignOf<ObjCTypeParamList>());
   return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
 }
 
index 5f8b42b3f9644a2e5b8a70459f407007c0babae5..493e2cd41226bde471bf8e1c3df6458dd6010cc2 100644 (file)
@@ -29,8 +29,9 @@ OMPThreadPrivateDecl *OMPThreadPrivateDecl::Create(ASTContext &C,
                                                    DeclContext *DC,
                                                    SourceLocation L,
                                                    ArrayRef<Expr *> VL) {
-  OMPThreadPrivateDecl *D = new (C, DC, VL.size() * sizeof(Expr *))
-      OMPThreadPrivateDecl(OMPThreadPrivate, DC, L);
+  OMPThreadPrivateDecl *D =
+      new (C, DC, additionalSizeToAlloc<Expr *>(VL.size()))
+          OMPThreadPrivateDecl(OMPThreadPrivate, DC, L);
   D->NumVars = VL.size();
   D->setVars(VL);
   return D;
@@ -39,7 +40,7 @@ OMPThreadPrivateDecl *OMPThreadPrivateDecl::Create(ASTContext &C,
 OMPThreadPrivateDecl *OMPThreadPrivateDecl::CreateDeserialized(ASTContext &C,
                                                                unsigned ID,
                                                                unsigned N) {
-  OMPThreadPrivateDecl *D = new (C, ID, N * sizeof(Expr *))
+  OMPThreadPrivateDecl *D = new (C, ID, additionalSizeToAlloc<Expr *>(N))
       OMPThreadPrivateDecl(OMPThreadPrivate, nullptr, SourceLocation());
   D->NumVars = N;
   return D;
@@ -48,7 +49,6 @@ OMPThreadPrivateDecl *OMPThreadPrivateDecl::CreateDeserialized(ASTContext &C,
 void OMPThreadPrivateDecl::setVars(ArrayRef<Expr *> VL) {
   assert(VL.size() == NumVars &&
          "Number of variables is not the same as the preallocated buffer");
-  Expr **Vars = reinterpret_cast<Expr **>(this + 1);
-  std::copy(VL.begin(), VL.end(), Vars);
+  std::uninitialized_copy(VL.begin(), VL.end(), getTrailingObjects<Expr *>());
 }
 
index 2c868cbb9fca333a09ff8d4017d83a1807e24017..8fb110e4551d08c46a70b795f5421c4cf613af16 100644 (file)
@@ -1731,7 +1731,7 @@ void ASTDeclReader::VisitImportDecl(ImportDecl *D) {
   VisitDecl(D);
   D->ImportedAndComplete.setPointer(readModule(Record, Idx));
   D->ImportedAndComplete.setInt(Record[Idx++]);
-  SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(D + 1);
+  SourceLocation *StoredLocs = D->getTrailingObjects<SourceLocation>();
   for (unsigned I = 0, N = Record.back(); I != N; ++I)
     StoredLocs[I] = ReadSourceLocation(Record, Idx);
   ++Idx; // The number of stored source locations.
@@ -1749,7 +1749,8 @@ void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
   else
     D->Friend = GetTypeSourceInfo(Record, Idx);
   for (unsigned i = 0; i != D->NumTPLists; ++i)
-    D->getTPLists()[i] = Reader.ReadTemplateParameterList(F, Record, Idx);
+    D->getTrailingObjects<TemplateParameterList *>()[i] =
+        Reader.ReadTemplateParameterList(F, Record, Idx);
   D->NextFriend = ReadDeclID(Record, Idx);
   D->UnsupportedFriend = (Record[Idx++] != 0);
   D->FriendLoc = ReadSourceLocation(Record, Idx);
@@ -2098,10 +2099,11 @@ void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
   D->setDepth(Record[Idx++]);
   D->setPosition(Record[Idx++]);
   if (D->isExpandedParameterPack()) {
-    void **Data = reinterpret_cast<void **>(D + 1);
+    auto TypesAndInfos =
+        D->getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
     for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
-      Data[2*I] = Reader.readType(F, Record, Idx).getAsOpaquePtr();
-      Data[2*I + 1] = GetTypeSourceInfo(Record, Idx);
+      new (&TypesAndInfos[I].first) QualType(Reader.readType(F, Record, Idx));
+      TypesAndInfos[I].second = GetTypeSourceInfo(Record, Idx);
     }
   } else {
     // Rest of NonTypeTemplateParmDecl.
@@ -2117,7 +2119,8 @@ void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
   D->setDepth(Record[Idx++]);
   D->setPosition(Record[Idx++]);
   if (D->isExpandedParameterPack()) {
-    void **Data = reinterpret_cast<void **>(D + 1);
+    TemplateParameterList **Data =
+        D->getTrailingObjects<TemplateParameterList *>();
     for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
          I != N; ++I)
       Data[I] = Reader.ReadTemplateParameterList(F, Record, Idx);