Skip to content

Commit 64390b4

Browse files
committedDec 18, 2015
Rewrite the TrailingObjects template to provide two new features:
- Automatic alignment of the base type for the alignment requirements of the trailing types. - Support for an arbitrary numbers of trailing types, instead of only 1 or 2, by using a variadic template implementation. Upcoming commits to clang will take advantage of both of these features. Differential Revision: http://reviews.llvm.org/D12439 llvm-svn: 256054
1 parent 31ccb51 commit 64390b4

File tree

2 files changed

+248
-108
lines changed

2 files changed

+248
-108
lines changed
 

‎llvm/include/llvm/Support/TrailingObjects.h

+214-104
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@
5959

6060
namespace llvm {
6161

62+
namespace trailing_objects_internal {
63+
/// Helper template to calculate the max alignment requirement for a set of
64+
/// objects.
65+
template <typename First, typename... Rest> class AlignmentCalcHelper {
66+
private:
67+
enum {
68+
FirstAlignment = AlignOf<First>::Alignment,
69+
RestAlignment = AlignmentCalcHelper<Rest...>::Alignment,
70+
};
71+
72+
public:
73+
enum {
74+
Alignment = FirstAlignment > RestAlignment ? FirstAlignment : RestAlignment
75+
};
76+
};
77+
78+
template <typename First> class AlignmentCalcHelper<First> {
79+
public:
80+
enum { Alignment = AlignOf<First>::Alignment };
81+
};
82+
6283
/// The base class for TrailingObjects* classes.
6384
class TrailingObjectsBase {
6485
protected:
@@ -70,73 +91,210 @@ class TrailingObjectsBase {
7091
template <typename T> struct OverloadToken {};
7192
};
7293

73-
// Internally used to indicate that the user didn't supply this value,
74-
// so the explicit-specialization for fewer args will be used.
75-
class NoTrailingTypeArg {};
94+
/// This helper template works-around MSVC 2013's lack of useful
95+
/// alignas() support. The argument to LLVM_ALIGNAS(), in MSVC, is
96+
/// required to be a literal integer. But, you *can* use template
97+
/// specialization to select between a bunch of different LLVM_ALIGNAS
98+
/// expressions...
99+
template <int Align>
100+
class TrailingObjectsAligner : public TrailingObjectsBase {};
101+
template <>
102+
class LLVM_ALIGNAS(1) TrailingObjectsAligner<1> : public TrailingObjectsBase {};
103+
template <>
104+
class LLVM_ALIGNAS(2) TrailingObjectsAligner<2> : public TrailingObjectsBase {};
105+
template <>
106+
class LLVM_ALIGNAS(4) TrailingObjectsAligner<4> : public TrailingObjectsBase {};
107+
template <>
108+
class LLVM_ALIGNAS(8) TrailingObjectsAligner<8> : public TrailingObjectsBase {};
109+
template <>
110+
class LLVM_ALIGNAS(16) TrailingObjectsAligner<16> : public TrailingObjectsBase {
111+
};
112+
template <>
113+
class LLVM_ALIGNAS(32) TrailingObjectsAligner<32> : public TrailingObjectsBase {
114+
};
76115

77-
// TODO: Consider using a single variadic implementation instead of
78-
// multiple copies of the TrailingObjects template? [but, variadic
79-
// template recursive implementations are annoying...]
116+
// Just a little helper for transforming a type pack into the same
117+
// number of a different type. e.g.:
118+
// ExtractSecondType<Foo..., int>::type
119+
template <typename Ty1, typename Ty2> struct ExtractSecondType {
120+
typedef Ty2 type;
121+
};
122+
123+
// TrailingObjectsImpl is somewhat complicated, because it is a
124+
// recursively inheriting template, in order to handle the template
125+
// varargs. Each level of inheritance picks off a single trailing type
126+
// then recurses on the rest. The "Align", "BaseTy", and
127+
// "TopTrailingObj" arguments are passed through unchanged through the
128+
// recursion. "PrevTy" is, at each level, the type handled by the
129+
// level right above it.
130+
131+
template <int Align, typename BaseTy, typename TopTrailingObj, typename PrevTy,
132+
typename... MoreTys>
133+
struct TrailingObjectsImpl {
134+
// The main template definition is never used -- the two
135+
// specializations cover all possibilities.
136+
};
137+
138+
template <int Align, typename BaseTy, typename TopTrailingObj, typename PrevTy,
139+
typename NextTy, typename... MoreTys>
140+
struct TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, PrevTy, NextTy,
141+
MoreTys...>
142+
: public TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, NextTy,
143+
MoreTys...> {
144+
145+
typedef TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, NextTy, MoreTys...>
146+
ParentType;
147+
148+
// Ensure the methods we inherit are not hidden.
149+
using ParentType::getTrailingObjectsImpl;
150+
using ParentType::additionalSizeToAllocImpl;
151+
152+
static void verifyTrailingObjectsAssertions() {
153+
static_assert(llvm::AlignOf<PrevTy>::Alignment >=
154+
llvm::AlignOf<NextTy>::Alignment,
155+
"A trailing object requires more alignment than the previous "
156+
"trailing object provides");
157+
158+
ParentType::verifyTrailingObjectsAssertions();
159+
}
160+
161+
// These two functions are helper functions for
162+
// TrailingObjects::getTrailingObjects. They recurse to the left --
163+
// the result for each type in the list of trailing types depends on
164+
// the result of calling the function on the type to the
165+
// left. However, the function for the type to the left is
166+
// implemented by a *subclass* of this class, so we invoke it via
167+
// the TopTrailingObj, which is, via the
168+
// curiously-recurring-template-pattern, the most-derived type in
169+
// this recursion, and thus, contains all the overloads.
170+
static const NextTy *
171+
getTrailingObjectsImpl(const BaseTy *Obj,
172+
TrailingObjectsBase::OverloadToken<NextTy>) {
173+
return reinterpret_cast<const NextTy *>(
174+
TopTrailingObj::getTrailingObjectsImpl(
175+
Obj, TrailingObjectsBase::OverloadToken<PrevTy>()) +
176+
TopTrailingObj::callNumTrailingObjects(
177+
Obj, TrailingObjectsBase::OverloadToken<PrevTy>()));
178+
}
179+
180+
static NextTy *
181+
getTrailingObjectsImpl(BaseTy *Obj,
182+
TrailingObjectsBase::OverloadToken<NextTy>) {
183+
return reinterpret_cast<NextTy *>(
184+
TopTrailingObj::getTrailingObjectsImpl(
185+
Obj, TrailingObjectsBase::OverloadToken<PrevTy>()) +
186+
TopTrailingObj::callNumTrailingObjects(
187+
Obj, TrailingObjectsBase::OverloadToken<PrevTy>()));
188+
}
189+
190+
// Helper function for TrailingObjects::additionalSizeToAlloc: this
191+
// function recurses to superclasses, each of which requires one
192+
// fewer size_t argument, and adds its own size.
193+
static LLVM_CONSTEXPR size_t additionalSizeToAllocImpl(
194+
size_t Count1,
195+
typename ExtractSecondType<MoreTys, size_t>::type... MoreCounts) {
196+
return sizeof(NextTy) * Count1 + additionalSizeToAllocImpl(MoreCounts...);
197+
}
198+
};
199+
200+
// The base case of the TrailingObjectsImpl inheritance recursion,
201+
// when there's no more trailing types.
202+
template <int Align, typename BaseTy, typename TopTrailingObj, typename PrevTy>
203+
struct TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, PrevTy>
204+
: public TrailingObjectsAligner<Align> {
205+
// This is a dummy method, only here so the "using" doesn't fail --
206+
// it will never be called, because this function recurses backwards
207+
// up the inheritance chain to subclasses.
208+
static void getTrailingObjectsImpl();
209+
210+
static LLVM_CONSTEXPR size_t additionalSizeToAllocImpl() { return 0; }
211+
212+
static void verifyTrailingObjectsAssertions() {}
213+
};
214+
215+
} // end namespace trailing_objects_internal
216+
217+
// Finally, the main type defined in this file, the one intended for users...
218+
219+
/// See the file comment for details on the usage of the
220+
/// TrailingObjects type.
221+
template <typename BaseTy, typename... TrailingTys>
222+
class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl<
223+
trailing_objects_internal::AlignmentCalcHelper<
224+
TrailingTys...>::Alignment,
225+
BaseTy, TrailingObjects<BaseTy, TrailingTys...>,
226+
BaseTy, TrailingTys...> {
227+
228+
template <int A, typename B, typename T, typename P, typename... M>
229+
friend struct trailing_objects_internal::TrailingObjectsImpl;
230+
231+
template <typename... Tys> class Foo {};
232+
233+
typedef trailing_objects_internal::TrailingObjectsImpl<
234+
trailing_objects_internal::AlignmentCalcHelper<TrailingTys...>::Alignment,
235+
BaseTy, TrailingObjects<BaseTy, TrailingTys...>, BaseTy, TrailingTys...>
236+
ParentType;
237+
using TrailingObjectsBase = trailing_objects_internal::TrailingObjectsBase;
238+
239+
using ParentType::getTrailingObjectsImpl;
80240

81-
/// This is the two-type version of the TrailingObjects template; see
82-
/// file docstring for details.
83-
template <typename BaseTy, typename TrailingTy1,
84-
typename TrailingTy2 = NoTrailingTypeArg>
85-
class TrailingObjects : public TrailingObjectsBase {
86-
private:
87241
// Contains static_assert statements for the alignment of the
88242
// types. Must not be at class-level, because BaseTy isn't complete
89243
// at class instantiation time, but will be by the time this
90-
// function is instantiated.
244+
// function is instantiated. Recurses through the superclasses.
91245
static void verifyTrailingObjectsAssertions() {
92-
static_assert(llvm::AlignOf<BaseTy>::Alignment >=
93-
llvm::AlignOf<TrailingTy1>::Alignment,
94-
"TrailingTy1 requires more alignment than BaseTy provides");
95-
static_assert(
96-
llvm::AlignOf<TrailingTy1>::Alignment >=
97-
llvm::AlignOf<TrailingTy2>::Alignment,
98-
"TrailingTy2 requires more alignment than TrailingTy1 provides");
99-
100246
#ifdef LLVM_IS_FINAL
101247
static_assert(LLVM_IS_FINAL(BaseTy), "BaseTy must be final.");
102248
#endif
249+
ParentType::verifyTrailingObjectsAssertions();
103250
}
104251

105-
// The next four functions are internal helpers for getTrailingObjects.
106-
static const TrailingTy1 *getTrailingObjectsImpl(const BaseTy *Obj,
107-
OverloadToken<TrailingTy1>) {
108-
return reinterpret_cast<const TrailingTy1 *>(Obj + 1);
252+
// These two methods are the base of the recursion for this method.
253+
static const BaseTy *
254+
getTrailingObjectsImpl(const BaseTy *Obj,
255+
TrailingObjectsBase::OverloadToken<BaseTy>) {
256+
return Obj;
109257
}
110258

111-
static TrailingTy1 *getTrailingObjectsImpl(BaseTy *Obj,
112-
OverloadToken<TrailingTy1>) {
113-
return reinterpret_cast<TrailingTy1 *>(Obj + 1);
259+
static BaseTy *
260+
getTrailingObjectsImpl(BaseTy *Obj,
261+
TrailingObjectsBase::OverloadToken<BaseTy>) {
262+
return Obj;
114263
}
115264

116-
static const TrailingTy2 *getTrailingObjectsImpl(const BaseTy *Obj,
117-
OverloadToken<TrailingTy2>) {
118-
return reinterpret_cast<const TrailingTy2 *>(
119-
getTrailingObjectsImpl(Obj, OverloadToken<TrailingTy1>()) +
120-
Obj->numTrailingObjects(OverloadToken<TrailingTy1>()));
265+
// callNumTrailingObjects simply calls numTrailingObjects on the
266+
// provided Obj -- except when the type being queried is BaseTy
267+
// itself. There is always only one of the base object, so that case
268+
// is handled here. (An additional benefit of indirecting through
269+
// this function is that consumers only say "friend
270+
// TrailingObjects", and thus, only this class itself can call the
271+
// numTrailingObjects function.)
272+
static size_t
273+
callNumTrailingObjects(const BaseTy *Obj,
274+
TrailingObjectsBase::OverloadToken<BaseTy>) {
275+
return 1;
121276
}
122277

123-
static TrailingTy2 *getTrailingObjectsImpl(BaseTy *Obj,
124-
OverloadToken<TrailingTy2>) {
125-
return reinterpret_cast<TrailingTy2 *>(
126-
getTrailingObjectsImpl(Obj, OverloadToken<TrailingTy1>()) +
127-
Obj->numTrailingObjects(OverloadToken<TrailingTy1>()));
278+
template <typename T>
279+
static size_t callNumTrailingObjects(const BaseTy *Obj,
280+
TrailingObjectsBase::OverloadToken<T>) {
281+
return Obj->numTrailingObjects(TrailingObjectsBase::OverloadToken<T>());
128282
}
129283

130-
protected:
284+
public:
285+
// make this (privately inherited) class public.
286+
using TrailingObjectsBase::OverloadToken;
287+
131288
/// Returns a pointer to the trailing object array of the given type
132289
/// (which must be one of those specified in the class template). The
133290
/// array may have zero or more elements in it.
134291
template <typename T> const T *getTrailingObjects() const {
135292
verifyTrailingObjectsAssertions();
136293
// Forwards to an impl function with overloads, since member
137294
// function templates can't be specialized.
138-
return getTrailingObjectsImpl(static_cast<const BaseTy *>(this),
139-
OverloadToken<T>());
295+
return this->getTrailingObjectsImpl(
296+
static_cast<const BaseTy *>(this),
297+
TrailingObjectsBase::OverloadToken<T>());
140298
}
141299

142300
/// Returns a pointer to the trailing object array of the given type
@@ -146,8 +304,8 @@ class TrailingObjects : public TrailingObjectsBase {
146304
verifyTrailingObjectsAssertions();
147305
// Forwards to an impl function with overloads, since member
148306
// function templates can't be specialized.
149-
return getTrailingObjectsImpl(static_cast<BaseTy *>(this),
150-
OverloadToken<T>());
307+
return this->getTrailingObjectsImpl(
308+
static_cast<BaseTy *>(this), TrailingObjectsBase::OverloadToken<T>());
151309
}
152310

153311
/// Returns the size of the trailing data, if an object were
@@ -156,73 +314,25 @@ class TrailingObjects : public TrailingObjectsBase {
156314
/// base object. The template arguments must be the same as those
157315
/// used in the class; they are supplied here redundantly only so
158316
/// that it's clear what the counts are counting in callers.
159-
template <typename Ty1, typename Ty2,
160-
typename std::enable_if<std::is_same<Ty1, TrailingTy1>::value &&
161-
std::is_same<Ty2, TrailingTy2>::value,
162-
int>::type = 0>
163-
static LLVM_CONSTEXPR size_t additionalSizeToAlloc(size_t Count1, size_t Count2) {
164-
return sizeof(TrailingTy1) * Count1 + sizeof(TrailingTy2) * Count2;
317+
template <typename... Tys>
318+
static LLVM_CONSTEXPR typename std::enable_if<
319+
std::is_same<Foo<TrailingTys...>, Foo<Tys...>>::value, size_t>::type
320+
additionalSizeToAlloc(
321+
typename trailing_objects_internal::ExtractSecondType<
322+
TrailingTys, size_t>::type... Counts) {
323+
return ParentType::additionalSizeToAllocImpl(Counts...);
165324
}
166325

167326
/// Returns the total size of an object if it were allocated with the
168327
/// given trailing object counts. This is the same as
169328
/// additionalSizeToAlloc, except it *does* include the size of the base
170329
/// object.
171-
template <typename Ty1, typename Ty2>
172-
static LLVM_CONSTEXPR size_t totalSizeToAlloc(size_t Count1, size_t Count2) {
173-
return sizeof(BaseTy) + additionalSizeToAlloc<Ty1, Ty2>(Count1, Count2);
174-
}
175-
};
176-
177-
/// This is the one-type version of the TrailingObjects template. See
178-
/// the two-type version for more documentation.
179-
template <typename BaseTy, typename TrailingTy1>
180-
class TrailingObjects<BaseTy, TrailingTy1, NoTrailingTypeArg>
181-
: public TrailingObjectsBase {
182-
private:
183-
static void verifyTrailingObjectsAssertions() {
184-
static_assert(llvm::AlignOf<BaseTy>::Alignment >=
185-
llvm::AlignOf<TrailingTy1>::Alignment,
186-
"TrailingTy1 requires more alignment than BaseTy provides");
187-
188-
#ifdef LLVM_IS_FINAL
189-
static_assert(LLVM_IS_FINAL(BaseTy), "BaseTy must be final.");
190-
#endif
191-
}
192-
193-
static const TrailingTy1 *getTrailingObjectsImpl(const BaseTy *Obj,
194-
OverloadToken<TrailingTy1>) {
195-
return reinterpret_cast<const TrailingTy1 *>(Obj + 1);
196-
}
197-
198-
static TrailingTy1 *getTrailingObjectsImpl(BaseTy *Obj,
199-
OverloadToken<TrailingTy1>) {
200-
return reinterpret_cast<TrailingTy1 *>(Obj + 1);
201-
}
202-
203-
protected:
204-
template <typename T> const T *getTrailingObjects() const {
205-
verifyTrailingObjectsAssertions();
206-
return getTrailingObjectsImpl(static_cast<const BaseTy *>(this),
207-
OverloadToken<T>());
208-
}
209-
210-
template <typename T> T *getTrailingObjects() {
211-
verifyTrailingObjectsAssertions();
212-
return getTrailingObjectsImpl(static_cast<BaseTy *>(this),
213-
OverloadToken<T>());
214-
}
215-
216-
template <typename Ty1,
217-
typename std::enable_if<std::is_same<Ty1, TrailingTy1>::value,
218-
int>::type = 0>
219-
static LLVM_CONSTEXPR size_t additionalSizeToAlloc(size_t Count1) {
220-
return sizeof(TrailingTy1) * Count1;
221-
}
222-
223-
template <typename Ty1>
224-
static LLVM_CONSTEXPR size_t totalSizeToAlloc(size_t Count1) {
225-
return sizeof(BaseTy) + additionalSizeToAlloc<Ty1>(Count1);
330+
template <typename... Tys>
331+
static LLVM_CONSTEXPR typename std::enable_if<
332+
std::is_same<Foo<TrailingTys...>, Foo<Tys...>>::value, size_t>::type
333+
totalSizeToAlloc(typename trailing_objects_internal::ExtractSecondType<
334+
TrailingTys, size_t>::type... Counts) {
335+
return sizeof(BaseTy) + ParentType::additionalSizeToAllocImpl(Counts...);
226336
}
227337
};
228338

0 commit comments

Comments
 (0)
Please sign in to comment.