Skip to content

Commit 967eb20

Browse files
committed
[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
1 parent ac6e910 commit 967eb20

File tree

12 files changed

+72
-68
lines changed

12 files changed

+72
-68
lines changed

clang/include/clang/AST/DeclCXX.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,8 @@ class CXXMethodDecl : public FunctionDecl {
18881888
/// B(A& a) : A(a), f(3.14159) { }
18891889
/// };
18901890
/// \endcode
1891-
class CXXCtorInitializer {
1891+
class CXXCtorInitializer final
1892+
: private llvm::TrailingObjects<CXXCtorInitializer, VarDecl *> {
18921893
/// \brief Either the base class name/delegating constructor type (stored as
18931894
/// a TypeSourceInfo*), an normal field (FieldDecl), or an anonymous field
18941895
/// (IndirectFieldDecl*) being initialized.
@@ -2104,24 +2105,26 @@ class CXXCtorInitializer {
21042105
/// describe an array member initialization.
21052106
VarDecl *getArrayIndex(unsigned I) {
21062107
assert(I < getNumArrayIndices() && "Out of bounds member array index");
2107-
return reinterpret_cast<VarDecl **>(this + 1)[I];
2108+
return getTrailingObjects<VarDecl *>()[I];
21082109
}
21092110
const VarDecl *getArrayIndex(unsigned I) const {
21102111
assert(I < getNumArrayIndices() && "Out of bounds member array index");
2111-
return reinterpret_cast<const VarDecl * const *>(this + 1)[I];
2112+
return getTrailingObjects<VarDecl *>()[I];
21122113
}
21132114
void setArrayIndex(unsigned I, VarDecl *Index) {
21142115
assert(I < getNumArrayIndices() && "Out of bounds member array index");
2115-
reinterpret_cast<VarDecl **>(this + 1)[I] = Index;
2116+
getTrailingObjects<VarDecl *>()[I] = Index;
21162117
}
21172118
ArrayRef<VarDecl *> getArrayIndexes() {
21182119
assert(getNumArrayIndices() != 0 && "Getting indexes for non-array init");
2119-
return llvm::makeArrayRef(reinterpret_cast<VarDecl **>(this + 1),
2120+
return llvm::makeArrayRef(getTrailingObjects<VarDecl *>(),
21202121
getNumArrayIndices());
21212122
}
21222123

21232124
/// \brief Get the initializer.
21242125
Expr *getInit() const { return static_cast<Expr*>(Init); }
2126+
2127+
friend TrailingObjects;
21252128
};
21262129

21272130
/// \brief Represents a C++ constructor within a class.

clang/include/clang/AST/DeclFriend.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ namespace clang {
3737
/// @endcode
3838
///
3939
/// The semantic context of a friend decl is its declaring class.
40-
class FriendDecl : public Decl {
40+
class FriendDecl final
41+
: public Decl,
42+
private llvm::TrailingObjects<FriendDecl, TemplateParameterList *> {
4143
virtual void anchor();
4244
public:
4345
typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
@@ -62,14 +64,6 @@ class FriendDecl : public Decl {
6264
// template <class T> friend class A<T>::B;
6365
unsigned NumTPLists : 31;
6466

65-
// The tail-allocated friend type template parameter lists (if any).
66-
TemplateParameterList* const *getTPLists() const {
67-
return reinterpret_cast<TemplateParameterList* const *>(this + 1);
68-
}
69-
TemplateParameterList **getTPLists() {
70-
return reinterpret_cast<TemplateParameterList**>(this + 1);
71-
}
72-
7367
friend class CXXRecordDecl::friend_iterator;
7468
friend class CXXRecordDecl;
7569

@@ -83,7 +77,7 @@ class FriendDecl : public Decl {
8377
UnsupportedFriend(false),
8478
NumTPLists(FriendTypeTPLists.size()) {
8579
for (unsigned i = 0; i < NumTPLists; ++i)
86-
getTPLists()[i] = FriendTypeTPLists[i];
80+
getTrailingObjects<TemplateParameterList *>()[i] = FriendTypeTPLists[i];
8781
}
8882

8983
FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
@@ -118,7 +112,7 @@ class FriendDecl : public Decl {
118112
}
119113
TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
120114
assert(N < NumTPLists);
121-
return getTPLists()[N];
115+
return getTrailingObjects<TemplateParameterList *>()[N];
122116
}
123117

124118
/// If this friend declaration doesn't name a type, return the inner
@@ -148,9 +142,10 @@ class FriendDecl : public Decl {
148142
return SourceRange(getFriendLoc(), ND->getLocEnd());
149143
}
150144
else if (TypeSourceInfo *TInfo = getFriendType()) {
151-
SourceLocation StartL = (NumTPLists == 0)
152-
? getFriendLoc()
153-
: getTPLists()[0]->getTemplateLoc();
145+
SourceLocation StartL =
146+
(NumTPLists == 0) ? getFriendLoc()
147+
: getTrailingObjects<TemplateParameterList *>()[0]
148+
->getTemplateLoc();
154149
return SourceRange(StartL, TInfo->getTypeLoc().getEndLoc());
155150
}
156151
else
@@ -171,6 +166,7 @@ class FriendDecl : public Decl {
171166

172167
friend class ASTDeclReader;
173168
friend class ASTDeclWriter;
169+
friend TrailingObjects;
174170
};
175171

176172
/// An iterator over the friend declarations of a class.

clang/include/clang/AST/DeclGroup.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_CLANG_AST_DECLGROUP_H
1616

1717
#include "llvm/Support/DataTypes.h"
18+
#include "llvm/Support/TrailingObjects.h"
1819
#include <cassert>
1920

2021
namespace clang {
@@ -24,13 +25,9 @@ class Decl;
2425
class DeclGroup;
2526
class DeclGroupIterator;
2627

27-
class DeclGroup {
28+
class DeclGroup final : private llvm::TrailingObjects<DeclGroup, Decl *> {
2829
// FIXME: Include a TypeSpecifier object.
29-
union {
30-
unsigned NumDecls;
31-
32-
Decl *Aligner;
33-
};
30+
unsigned NumDecls;
3431

3532
private:
3633
DeclGroup() : NumDecls(0) {}
@@ -43,13 +40,15 @@ class DeclGroup {
4340

4441
Decl*& operator[](unsigned i) {
4542
assert (i < NumDecls && "Out-of-bounds access.");
46-
return ((Decl**) (this+1))[i];
43+
return getTrailingObjects<Decl *>()[i];
4744
}
4845

4946
Decl* const& operator[](unsigned i) const {
5047
assert (i < NumDecls && "Out-of-bounds access.");
51-
return ((Decl* const*) (this+1))[i];
48+
return getTrailingObjects<Decl *>()[i];
5249
}
50+
51+
friend TrailingObjects;
5352
};
5453

5554
class DeclGroupRef {

clang/include/clang/AST/DeclObjC.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,8 @@ class ObjCTypeParamDecl : public TypedefNameDecl {
612612
/// @interface NSArray<T> // stores the <T>
613613
/// @end
614614
/// \endcode
615-
class ObjCTypeParamList {
615+
class ObjCTypeParamList final
616+
: private llvm::TrailingObjects<ObjCTypeParamList, ObjCTypeParamDecl *> {
616617
/// Stores the components of a SourceRange as a POD.
617618
struct PODSourceRange {
618619
unsigned Begin;
@@ -644,7 +645,7 @@ class ObjCTypeParamList {
644645
/// Iterate through the type parameters in the list.
645646
typedef ObjCTypeParamDecl **iterator;
646647

647-
iterator begin() { return reinterpret_cast<ObjCTypeParamDecl **>(this + 1); }
648+
iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); }
648649

649650
iterator end() { return begin() + size(); }
650651

@@ -655,7 +656,7 @@ class ObjCTypeParamList {
655656
typedef ObjCTypeParamDecl * const *const_iterator;
656657

657658
const_iterator begin() const {
658-
return reinterpret_cast<ObjCTypeParamDecl * const *>(this + 1);
659+
return getTrailingObjects<ObjCTypeParamDecl *>();
659660
}
660661

661662
const_iterator end() const {
@@ -685,6 +686,7 @@ class ObjCTypeParamList {
685686
/// Gather the default set of type arguments to be substituted for
686687
/// these type parameters when dealing with an unspecialized type.
687688
void gatherDefaultTypeArgs(SmallVectorImpl<QualType> &typeArgs) const;
689+
friend TrailingObjects;
688690
};
689691

690692
/// ObjCContainerDecl - Represents a container for method declarations.

clang/include/clang/AST/DeclOpenMP.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ class Expr;
3333
/// };
3434
/// \endcode
3535
///
36-
class OMPThreadPrivateDecl : public Decl {
36+
class OMPThreadPrivateDecl final
37+
: public Decl,
38+
private llvm::TrailingObjects<OMPThreadPrivateDecl, Expr *> {
3739
friend class ASTDeclReader;
40+
friend TrailingObjects;
41+
3842
unsigned NumVars;
3943

4044
virtual void anchor();
@@ -43,14 +47,11 @@ class OMPThreadPrivateDecl : public Decl {
4347
Decl(DK, DC, L), NumVars(0) { }
4448

4549
ArrayRef<const Expr *> getVars() const {
46-
return llvm::makeArrayRef(reinterpret_cast<const Expr * const *>(this + 1),
47-
NumVars);
50+
return llvm::makeArrayRef(getTrailingObjects<Expr *>(), NumVars);
4851
}
4952

5053
MutableArrayRef<Expr *> getVars() {
51-
return MutableArrayRef<Expr *>(
52-
reinterpret_cast<Expr **>(this + 1),
53-
NumVars);
54+
return MutableArrayRef<Expr *>(getTrailingObjects<Expr *>(), NumVars);
5455
}
5556

5657
void setVars(ArrayRef<Expr *> VL);

clang/include/clang/AST/DeclTemplate.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef llvm::PointerUnion3<TemplateTypeParmDecl*, NonTypeTemplateParmDecl*,
4545

4646
/// \brief Stores a list of template parameters for a TemplateDecl and its
4747
/// derived classes.
48-
class LLVM_ALIGNAS(/*alignof(void*)*/ LLVM_PTR_SIZE) TemplateParameterList final
48+
class TemplateParameterList final
4949
: private llvm::TrailingObjects<TemplateParameterList, NamedDecl *> {
5050

5151
/// The location of the 'template' keyword.
@@ -169,7 +169,7 @@ template <size_t N> class FixedSizeTemplateParameterListStorage {
169169
};
170170

171171
/// \brief A template argument list.
172-
class LLVM_ALIGNAS(/*alignof(uint64_t)*/ 8) TemplateArgumentList final
172+
class TemplateArgumentList final
173173
: private llvm::TrailingObjects<TemplateArgumentList, TemplateArgument> {
174174
/// \brief The template argument list.
175175
const TemplateArgument *Arguments;
@@ -553,8 +553,7 @@ class MemberSpecializationInfo {
553553
/// friend void foo<>(T);
554554
/// };
555555
/// \endcode
556-
class LLVM_ALIGNAS(/*alignof(uint64_t)*/ 8)
557-
DependentFunctionTemplateSpecializationInfo final
556+
class DependentFunctionTemplateSpecializationInfo final
558557
: private llvm::TrailingObjects<DependentFunctionTemplateSpecializationInfo,
559558
TemplateArgumentLoc,
560559
FunctionTemplateDecl *> {

clang/lib/AST/DeclCXX.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,8 +1675,8 @@ CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
16751675
LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
16761676
IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
16771677
{
1678-
VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1679-
memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1678+
std::uninitialized_copy(Indices, Indices + NumIndices,
1679+
getTrailingObjects<VarDecl *>());
16801680
}
16811681

16821682
CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
@@ -1686,8 +1686,7 @@ CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
16861686
SourceLocation R,
16871687
VarDecl **Indices,
16881688
unsigned NumIndices) {
1689-
void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
1690-
sizeof(VarDecl *) * NumIndices,
1689+
void *Mem = Context.Allocate(totalSizeToAlloc<VarDecl *>(NumIndices),
16911690
llvm::alignOf<CXXCtorInitializer>());
16921691
return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
16931692
Indices, NumIndices);

clang/lib/AST/DeclFriend.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
4646
}
4747
#endif
4848

49-
std::size_t Extra = FriendTypeTPLists.size() * sizeof(TemplateParameterList*);
49+
std::size_t Extra =
50+
FriendDecl::additionalSizeToAlloc<TemplateParameterList *>(
51+
FriendTypeTPLists.size());
5052
FriendDecl *FD = new (C, DC, Extra) FriendDecl(DC, L, Friend, FriendL,
5153
FriendTypeTPLists);
5254
cast<CXXRecordDecl>(DC)->pushFriendDecl(FD);
@@ -55,7 +57,8 @@ FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC,
5557

5658
FriendDecl *FriendDecl::CreateDeserialized(ASTContext &C, unsigned ID,
5759
unsigned FriendTypeNumTPLists) {
58-
std::size_t Extra = FriendTypeNumTPLists * sizeof(TemplateParameterList*);
60+
std::size_t Extra =
61+
additionalSizeToAlloc<TemplateParameterList *>(FriendTypeNumTPLists);
5962
return new (C, ID, Extra) FriendDecl(EmptyShell(), FriendTypeNumTPLists);
6063
}
6164

clang/lib/AST/DeclGroup.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
using namespace clang;
1919

2020
DeclGroup* DeclGroup::Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
21-
static_assert(sizeof(DeclGroup) % llvm::AlignOf<void *>::Alignment == 0,
22-
"Trailing data is unaligned!");
2321
assert(NumDecls > 1 && "Invalid DeclGroup");
24-
unsigned Size = sizeof(DeclGroup) + sizeof(Decl*) * NumDecls;
22+
unsigned Size = totalSizeToAlloc<Decl *>(NumDecls);
2523
void* Mem = C.Allocate(Size, llvm::AlignOf<DeclGroup>::Alignment);
2624
new (Mem) DeclGroup(NumDecls, Decls);
2725
return static_cast<DeclGroup*>(Mem);
@@ -30,5 +28,6 @@ DeclGroup* DeclGroup::Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
3028
DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) : NumDecls(numdecls) {
3129
assert(numdecls > 0);
3230
assert(decls);
33-
memcpy(this+1, decls, numdecls * sizeof(*decls));
31+
std::uninitialized_copy(decls, decls + numdecls,
32+
getTrailingObjects<Decl *>());
3433
}

clang/lib/AST/DeclObjC.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,10 @@ void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
772772
if (Params.empty() && SelLocs.empty())
773773
return;
774774

775+
static_assert(llvm::AlignOf<ParmVarDecl *>::Alignment >=
776+
llvm::AlignOf<SourceLocation>::Alignment,
777+
"Alignment not sufficient for SourceLocation");
778+
775779
unsigned Size = sizeof(ParmVarDecl *) * NumParams +
776780
sizeof(SourceLocation) * SelLocs.size();
777781
ParamsAndSelLocs = C.Allocate(Size);
@@ -1326,13 +1330,9 @@ ObjCTypeParamList *ObjCTypeParamList::create(
13261330
SourceLocation lAngleLoc,
13271331
ArrayRef<ObjCTypeParamDecl *> typeParams,
13281332
SourceLocation rAngleLoc) {
1329-
unsigned size = sizeof(ObjCTypeParamList)
1330-
+ sizeof(ObjCTypeParamDecl *) * typeParams.size();
1331-
static_assert(llvm::AlignOf<ObjCTypeParamList>::Alignment >=
1332-
llvm::AlignOf<ObjCTypeParamDecl *>::Alignment,
1333-
"type parameter list needs greater alignment");
1334-
unsigned align = llvm::alignOf<ObjCTypeParamList>();
1335-
void *mem = ctx.Allocate(size, align);
1333+
void *mem =
1334+
ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
1335+
llvm::alignOf<ObjCTypeParamList>());
13361336
return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
13371337
}
13381338

clang/lib/AST/DeclOpenMP.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ OMPThreadPrivateDecl *OMPThreadPrivateDecl::Create(ASTContext &C,
2929
DeclContext *DC,
3030
SourceLocation L,
3131
ArrayRef<Expr *> VL) {
32-
OMPThreadPrivateDecl *D = new (C, DC, VL.size() * sizeof(Expr *))
33-
OMPThreadPrivateDecl(OMPThreadPrivate, DC, L);
32+
OMPThreadPrivateDecl *D =
33+
new (C, DC, additionalSizeToAlloc<Expr *>(VL.size()))
34+
OMPThreadPrivateDecl(OMPThreadPrivate, DC, L);
3435
D->NumVars = VL.size();
3536
D->setVars(VL);
3637
return D;
@@ -39,7 +40,7 @@ OMPThreadPrivateDecl *OMPThreadPrivateDecl::Create(ASTContext &C,
3940
OMPThreadPrivateDecl *OMPThreadPrivateDecl::CreateDeserialized(ASTContext &C,
4041
unsigned ID,
4142
unsigned N) {
42-
OMPThreadPrivateDecl *D = new (C, ID, N * sizeof(Expr *))
43+
OMPThreadPrivateDecl *D = new (C, ID, additionalSizeToAlloc<Expr *>(N))
4344
OMPThreadPrivateDecl(OMPThreadPrivate, nullptr, SourceLocation());
4445
D->NumVars = N;
4546
return D;
@@ -48,7 +49,6 @@ OMPThreadPrivateDecl *OMPThreadPrivateDecl::CreateDeserialized(ASTContext &C,
4849
void OMPThreadPrivateDecl::setVars(ArrayRef<Expr *> VL) {
4950
assert(VL.size() == NumVars &&
5051
"Number of variables is not the same as the preallocated buffer");
51-
Expr **Vars = reinterpret_cast<Expr **>(this + 1);
52-
std::copy(VL.begin(), VL.end(), Vars);
52+
std::uninitialized_copy(VL.begin(), VL.end(), getTrailingObjects<Expr *>());
5353
}
5454

0 commit comments

Comments
 (0)