diff options
| author | James Y Knight <jyknight@google.com> | 2015-12-29 22:13:13 +0000 |
|---|---|---|
| committer | James Y Knight <jyknight@google.com> | 2015-12-29 22:13:13 +0000 |
| commit | 967eb20ebe8b399a83ac7c24da7808bd8f9011b0 (patch) | |
| tree | 1b7c83ffd077bdca30d0de1a3799fb90e406d4bf | |
| parent | ac6e910c427cea30423f3c28e6912c5a7d8886bf (diff) | |
| download | bcm5719-llvm-967eb20ebe8b399a83ac7c24da7808bd8f9011b0.tar.gz bcm5719-llvm-967eb20ebe8b399a83ac7c24da7808bd8f9011b0.zip | |
[TrailingObjects] Convert Decl* classes.
Also remove now-redundant explicit alignment specification on some of
the classes converted prior to TrailingObjects automatically ensuring
proper alignment.
llvm-svn: 256585
| -rw-r--r-- | clang/include/clang/AST/DeclCXX.h | 13 | ||||
| -rw-r--r-- | clang/include/clang/AST/DeclFriend.h | 24 | ||||
| -rw-r--r-- | clang/include/clang/AST/DeclGroup.h | 15 | ||||
| -rw-r--r-- | clang/include/clang/AST/DeclObjC.h | 8 | ||||
| -rw-r--r-- | clang/include/clang/AST/DeclOpenMP.h | 13 | ||||
| -rw-r--r-- | clang/include/clang/AST/DeclTemplate.h | 7 | ||||
| -rw-r--r-- | clang/lib/AST/DeclCXX.cpp | 7 | ||||
| -rw-r--r-- | clang/lib/AST/DeclFriend.cpp | 7 | ||||
| -rw-r--r-- | clang/lib/AST/DeclGroup.cpp | 7 | ||||
| -rw-r--r-- | clang/lib/AST/DeclObjC.cpp | 14 | ||||
| -rw-r--r-- | clang/lib/AST/DeclOpenMP.cpp | 10 | ||||
| -rw-r--r-- | clang/lib/Serialization/ASTReaderDecl.cpp | 15 |
12 files changed, 72 insertions, 68 deletions
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h index 1c54e1bb62d..c24823f7f17 100644 --- a/clang/include/clang/AST/DeclCXX.h +++ b/clang/include/clang/AST/DeclCXX.h @@ -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. diff --git a/clang/include/clang/AST/DeclFriend.h b/clang/include/clang/AST/DeclFriend.h index 12b93b408a7..27b0388007a 100644 --- a/clang/include/clang/AST/DeclFriend.h +++ b/clang/include/clang/AST/DeclFriend.h @@ -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. diff --git a/clang/include/clang/AST/DeclGroup.h b/clang/include/clang/AST/DeclGroup.h index bd3dbd8fa78..c84bb5e6048 100644 --- a/clang/include/clang/AST/DeclGroup.h +++ b/clang/include/clang/AST/DeclGroup.h @@ -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 { diff --git a/clang/include/clang/AST/DeclObjC.h b/clang/include/clang/AST/DeclObjC.h index f1e48a1296e..f46078f28a7 100644 --- a/clang/include/clang/AST/DeclObjC.h +++ b/clang/include/clang/AST/DeclObjC.h @@ -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. diff --git a/clang/include/clang/AST/DeclOpenMP.h b/clang/include/clang/AST/DeclOpenMP.h index 7f0616f1e60..598f418f7e3 100644 --- a/clang/include/clang/AST/DeclOpenMP.h +++ b/clang/include/clang/AST/DeclOpenMP.h @@ -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); diff --git a/clang/include/clang/AST/DeclTemplate.h b/clang/include/clang/AST/DeclTemplate.h index 8657baeb921..a9109ef1142 100644 --- a/clang/include/clang/AST/DeclTemplate.h +++ b/clang/include/clang/AST/DeclTemplate.h @@ -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 *> { diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp index 32e64b6e4ec..71bc247a007 100644 --- a/clang/lib/AST/DeclCXX.cpp +++ b/clang/lib/AST/DeclCXX.cpp @@ -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); diff --git a/clang/lib/AST/DeclFriend.cpp b/clang/lib/AST/DeclFriend.cpp index a996cab093a..121403b07e5 100644 --- a/clang/lib/AST/DeclFriend.cpp +++ b/clang/lib/AST/DeclFriend.cpp @@ -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); } diff --git a/clang/lib/AST/DeclGroup.cpp b/clang/lib/AST/DeclGroup.cpp index 512837fdf3f..f162e6d40c4 100644 --- a/clang/lib/AST/DeclGroup.cpp +++ b/clang/lib/AST/DeclGroup.cpp @@ -18,10 +18,8 @@ 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 *>()); } diff --git a/clang/lib/AST/DeclObjC.cpp b/clang/lib/AST/DeclObjC.cpp index b5dc9e122c3..050a0f53f1e 100644 --- a/clang/lib/AST/DeclObjC.cpp +++ b/clang/lib/AST/DeclObjC.cpp @@ -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); } diff --git a/clang/lib/AST/DeclOpenMP.cpp b/clang/lib/AST/DeclOpenMP.cpp index 5f8b42b3f96..493e2cd4122 100644 --- a/clang/lib/AST/DeclOpenMP.cpp +++ b/clang/lib/AST/DeclOpenMP.cpp @@ -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 *>()); } diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index 2c868cbb9fc..8fb110e4551 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -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); |

