LLVM 22.0.0git
TrailingObjects.h
Go to the documentation of this file.
1//===--- TrailingObjects.h - Variable-length classes ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This header defines support for implementing classes that have
11/// some trailing object (or arrays of objects) appended to them. The
12/// main purpose is to make it obvious where this idiom is being used,
13/// and to make the usage more idiomatic and more difficult to get
14/// wrong.
15///
16/// The TrailingObject template abstracts away the reinterpret_cast,
17/// pointer arithmetic, and size calculations used for the allocation
18/// and access of appended arrays of objects, and takes care that they
19/// are all allocated at their required alignment. Additionally, it
20/// ensures that the base type is final -- deriving from a class that
21/// expects data appended immediately after it is typically not safe.
22///
23/// Users are expected to derive from this template, and provide
24/// numTrailingObjects implementations for each trailing type except
25/// the last, e.g. like this sample:
26///
27/// \code
28/// class VarLengthObj : private TrailingObjects<VarLengthObj, int, double> {
29/// friend TrailingObjects;
30///
31/// unsigned NumInts, NumDoubles;
32/// size_t numTrailingObjects(OverloadToken<int>) const { return NumInts; }
33/// };
34/// \endcode
35///
36/// You can access the appended arrays via 'getTrailingObjects', and
37/// determine the size needed for allocation via
38/// 'additionalSizeToAlloc' and 'totalSizeToAlloc'.
39///
40/// All the methods implemented by this class are intended for use
41/// by the implementation of the class, not as part of its interface
42/// (thus, private inheritance is suggested).
43///
44//===----------------------------------------------------------------------===//
45
46#ifndef LLVM_SUPPORT_TRAILINGOBJECTS_H
47#define LLVM_SUPPORT_TRAILINGOBJECTS_H
48
49#include "llvm/ADT/ArrayRef.h"
54#include <new>
55#include <type_traits>
56
57namespace llvm {
58
59namespace trailing_objects_internal {
60/// Helper template to calculate the max alignment requirement for a set of
61/// objects.
62template <typename First, typename... Rest> class AlignmentCalcHelper {
63private:
64 enum {
65 FirstAlignment = alignof(First),
67 };
68
69public:
70 enum {
71 Alignment = FirstAlignment > RestAlignment ? FirstAlignment : RestAlignment
72 };
73};
74
75template <typename First> class AlignmentCalcHelper<First> {
76public:
77 enum { Alignment = alignof(First) };
78};
79
80/// The base class for TrailingObjects* classes.
82protected:
83 /// OverloadToken's purpose is to allow specifying function overloads
84 /// for different types, without actually taking the types as
85 /// parameters. (Necessary because member function templates cannot
86 /// be specialized, so overloads must be used instead of
87 /// specialization.)
88 template <typename T> struct OverloadToken {};
89};
90
91// Just a little helper for transforming a type pack into the same
92// number of a different type. e.g.:
93// ExtractSecondType<Foo..., int>::type
94template <typename Ty1, typename Ty2> struct ExtractSecondType {
95 typedef Ty2 type;
96};
97
98// TrailingObjectsImpl is somewhat complicated, because it is a
99// recursively inheriting template, in order to handle the template
100// varargs. Each level of inheritance picks off a single trailing type
101// then recurses on the rest. The "Align", "BaseTy", and
102// "TopTrailingObj" arguments are passed through unchanged through the
103// recursion. "PrevTy" is, at each level, the type handled by the
104// level right above it.
105
106template <int Align, typename BaseTy, typename TopTrailingObj, typename PrevTy,
107 typename... MoreTys>
109 // The main template definition is never used -- the two
110 // specializations cover all possibilities.
111};
112
113template <int Align, typename BaseTy, typename TopTrailingObj, typename PrevTy,
114 typename NextTy, typename... MoreTys>
115class TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, PrevTy, NextTy,
116 MoreTys...>
117 : public TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, NextTy,
118 MoreTys...> {
119
120 typedef TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, NextTy, MoreTys...>
122
123 struct RequiresRealignment {
124 static const bool value = alignof(PrevTy) < alignof(NextTy);
125 };
126
127 static constexpr bool requiresRealignment() {
128 return RequiresRealignment::value;
129 }
130
131protected:
132 // Ensure the inherited getTrailingObjectsImpl is not hidden.
133 using ParentType::getTrailingObjectsImpl;
134
135 // These two functions are helper functions for
136 // TrailingObjects::getTrailingObjects. They recurse to the left --
137 // the result for each type in the list of trailing types depends on
138 // the result of calling the function on the type to the
139 // left. However, the function for the type to the left is
140 // implemented by a *subclass* of this class, so we invoke it via
141 // the TopTrailingObj, which is, via the
142 // curiously-recurring-template-pattern, the most-derived type in
143 // this recursion, and thus, contains all the overloads.
144 static const NextTy *
147 auto *Ptr = TopTrailingObj::getTrailingObjectsImpl(
149 TopTrailingObj::callNumTrailingObjects(
151
152 if (requiresRealignment())
153 return reinterpret_cast<const NextTy *>(
154 alignAddr(Ptr, Align::Of<NextTy>()));
155 else
156 return reinterpret_cast<const NextTy *>(Ptr);
157 }
158
159 static NextTy *
162 auto *Ptr = TopTrailingObj::getTrailingObjectsImpl(
164 TopTrailingObj::callNumTrailingObjects(
166
167 if (requiresRealignment())
168 return reinterpret_cast<NextTy *>(alignAddr(Ptr, Align::Of<NextTy>()));
169 else
170 return reinterpret_cast<NextTy *>(Ptr);
171 }
172
173 // Helper function for TrailingObjects::additionalSizeToAlloc: this
174 // function recurses to superclasses, each of which requires one
175 // fewer size_t argument, and adds its own size.
176 static constexpr size_t additionalSizeToAllocImpl(
177 size_t SizeSoFar, size_t Count1,
178 typename ExtractSecondType<MoreTys, size_t>::type... MoreCounts) {
179 return ParentType::additionalSizeToAllocImpl(
180 (requiresRealignment() ? llvm::alignTo<alignof(NextTy)>(SizeSoFar)
181 : SizeSoFar) +
182 sizeof(NextTy) * Count1,
183 MoreCounts...);
184 }
185};
186
187// The base case of the TrailingObjectsImpl inheritance recursion,
188// when there's no more trailing types.
189template <int Align, typename BaseTy, typename TopTrailingObj, typename PrevTy>
190class alignas(Align) TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, PrevTy>
191 : public TrailingObjectsBase {
192protected:
193 // This is a dummy method, only here so the "using" doesn't fail --
194 // it will never be called, because this function recurses backwards
195 // up the inheritance chain to subclasses.
197
198 static constexpr size_t additionalSizeToAllocImpl(size_t SizeSoFar) {
199 return SizeSoFar;
200 }
201
202 template <bool CheckAlignment> static void verifyTrailingObjectsAlignment() {}
203};
204
205} // end namespace trailing_objects_internal
206
207// Finally, the main type defined in this file, the one intended for users...
208
209/// See the file comment for details on the usage of the
210/// TrailingObjects type.
211template <typename BaseTy, typename... TrailingTys>
213 trailing_objects_internal::AlignmentCalcHelper<
214 TrailingTys...>::Alignment,
215 BaseTy, TrailingObjects<BaseTy, TrailingTys...>,
216 BaseTy, TrailingTys...> {
217
218 template <int A, typename B, typename T, typename P, typename... M>
220
221 template <typename... Tys> class Foo {};
222
224 trailing_objects_internal::AlignmentCalcHelper<TrailingTys...>::Alignment,
225 BaseTy, TrailingObjects<BaseTy, TrailingTys...>, BaseTy, TrailingTys...>
226 ParentType;
227 using TrailingObjectsBase = trailing_objects_internal::TrailingObjectsBase;
228
229 using ParentType::getTrailingObjectsImpl;
230
231 template <bool Strict> static void verifyTrailingObjectsAssertions() {
232 // The static_assert for BaseTy must be in a function, and not at
233 // class-level because BaseTy isn't complete at class instantiation time,
234 // but will be by the time this function is instantiated.
235 static_assert(std::is_final<BaseTy>(), "BaseTy must be final.");
236
237 // Verify that templated getTrailingObjects() is used only with multiple
238 // trailing types. Use getTrailingObjectsNonStrict() which does not check
239 // this.
240 static_assert(!Strict || sizeof...(TrailingTys) > 1,
241 "Use templated getTrailingObjects() only when there are "
242 "multiple trailing types");
243 }
244
245 // These two methods are the base of the recursion for this method.
246 static const BaseTy *
247 getTrailingObjectsImpl(const BaseTy *Obj,
248 TrailingObjectsBase::OverloadToken<BaseTy>) {
249 return Obj;
250 }
251
252 static BaseTy *
253 getTrailingObjectsImpl(BaseTy *Obj,
254 TrailingObjectsBase::OverloadToken<BaseTy>) {
255 return Obj;
256 }
257
258 // callNumTrailingObjects simply calls numTrailingObjects on the
259 // provided Obj -- except when the type being queried is BaseTy
260 // itself. There is always only one of the base object, so that case
261 // is handled here. (An additional benefit of indirecting through
262 // this function is that consumers only say "friend
263 // TrailingObjects", and thus, only this class itself can call the
264 // numTrailingObjects function.)
265 static size_t
266 callNumTrailingObjects(const BaseTy *Obj,
267 TrailingObjectsBase::OverloadToken<BaseTy>) {
268 return 1;
269 }
270
271 template <typename T>
272 static size_t callNumTrailingObjects(const BaseTy *Obj,
273 TrailingObjectsBase::OverloadToken<T>) {
274 return Obj->numTrailingObjects(TrailingObjectsBase::OverloadToken<T>());
275 }
276
277public:
278 // Make this (privately inherited) member public.
279#ifndef _MSC_VER
280 using ParentType::OverloadToken;
281#else
282 // An MSVC bug prevents the above from working, (last tested at CL version
283 // 19.28). "Class5" in TrailingObjectsTest.cpp tests the problematic case.
284 template <typename T>
285 using OverloadToken = typename ParentType::template OverloadToken<T>;
286#endif
287
288 /// Returns a pointer to the trailing object array of the given type
289 /// (which must be one of those specified in the class template). The
290 /// array may have zero or more elements in it.
291 template <typename T> const T *getTrailingObjects() const {
292 verifyTrailingObjectsAssertions<true>();
293 // Forwards to an impl function with overloads, since member
294 // function templates can't be specialized.
295 return this->getTrailingObjectsImpl(
296 static_cast<const BaseTy *>(this),
298 }
299
300 /// Returns a pointer to the trailing object array of the given type
301 /// (which must be one of those specified in the class template). The
302 /// array may have zero or more elements in it.
303 template <typename T> T *getTrailingObjects() {
304 verifyTrailingObjectsAssertions<true>();
305 // Forwards to an impl function with overloads, since member
306 // function templates can't be specialized.
307 return this->getTrailingObjectsImpl(
308 static_cast<BaseTy *>(this), TrailingObjectsBase::OverloadToken<T>());
309 }
310
311 // getTrailingObjects() specialization for a single trailing type.
313 typename std::tuple_element_t<0, std::tuple<TrailingTys...>>;
314
316 static_assert(sizeof...(TrailingTys) == 1,
317 "Can use non-templated getTrailingObjects() only when there "
318 "is a single trailing type");
319 verifyTrailingObjectsAssertions<false>();
320 return this->getTrailingObjectsImpl(
321 static_cast<const BaseTy *>(this),
323 }
324
326 static_assert(sizeof...(TrailingTys) == 1,
327 "Can use non-templated getTrailingObjects() only when there "
328 "is a single trailing type");
329 verifyTrailingObjectsAssertions<false>();
330 return this->getTrailingObjectsImpl(
331 static_cast<BaseTy *>(this),
333 }
334
335 // Functions that return the trailing objects as ArrayRefs.
336 template <typename T> MutableArrayRef<T> getTrailingObjects(size_t N) {
337 return MutableArrayRef(getTrailingObjects<T>(), N);
338 }
339
340 template <typename T> ArrayRef<T> getTrailingObjects(size_t N) const {
341 return ArrayRef(getTrailingObjects<T>(), N);
342 }
343
345 return MutableArrayRef(getTrailingObjects(), N);
346 }
347
349 return ArrayRef(getTrailingObjects(), N);
350 }
351
352 // Non-strict forms of templated `getTrailingObjects` that work with single
353 // trailing type.
354 template <typename T> const T *getTrailingObjectsNonStrict() const {
355 verifyTrailingObjectsAssertions<false>();
356 return this->getTrailingObjectsImpl(
357 static_cast<const BaseTy *>(this),
359 }
360
361 template <typename T> T *getTrailingObjectsNonStrict() {
362 verifyTrailingObjectsAssertions<false>();
363 return this->getTrailingObjectsImpl(
364 static_cast<BaseTy *>(this), TrailingObjectsBase::OverloadToken<T>());
365 }
366
367 template <typename T>
369 return MutableArrayRef(getTrailingObjectsNonStrict<T>(), N);
370 }
371
372 template <typename T>
374 return ArrayRef(getTrailingObjectsNonStrict<T>(), N);
375 }
376
377 /// Returns the size of the trailing data, if an object were
378 /// allocated with the given counts (The counts are in the same order
379 /// as the template arguments). This does not include the size of the
380 /// base object. The template arguments must be the same as those
381 /// used in the class; they are supplied here redundantly only so
382 /// that it's clear what the counts are counting in callers.
383 template <typename... Tys>
384 static constexpr std::enable_if_t<
385 std::is_same_v<Foo<TrailingTys...>, Foo<Tys...>>, size_t>
387 TrailingTys, size_t>::type... Counts) {
388 return ParentType::additionalSizeToAllocImpl(0, Counts...);
389 }
390
391 /// Returns the total size of an object if it were allocated with the
392 /// given trailing object counts. This is the same as
393 /// additionalSizeToAlloc, except it *does* include the size of the base
394 /// object.
395 template <typename... Tys>
396 static constexpr std::enable_if_t<
397 std::is_same_v<Foo<TrailingTys...>, Foo<Tys...>>, size_t>
399 TrailingTys, size_t>::type... Counts) {
400 return sizeof(BaseTy) + ParentType::additionalSizeToAllocImpl(0, Counts...);
401 }
402
403 TrailingObjects() = default;
408
409 /// A type where its ::with_counts template member has a ::type member
410 /// suitable for use as uninitialized storage for an object with the given
411 /// trailing object counts. The template arguments are similar to those
412 /// of additionalSizeToAlloc.
413 ///
414 /// Use with FixedSizeStorageOwner, e.g.:
415 ///
416 /// \code{.cpp}
417 ///
418 /// MyObj::FixedSizeStorage<void *>::with_counts<1u>::type myStackObjStorage;
419 /// MyObj::FixedSizeStorageOwner
420 /// myStackObjOwner(new ((void *)&myStackObjStorage) MyObj);
421 /// MyObj *const myStackObjPtr = myStackObjOwner.get();
422 ///
423 /// \endcode
424 template <typename... Tys> struct FixedSizeStorage {
425 template <size_t... Counts> struct with_counts {
426 enum { Size = totalSizeToAlloc<Tys...>(Counts...) };
427 struct type {
428 alignas(BaseTy) char buffer[Size];
429 };
430 };
431 };
432
433 /// A type that acts as the owner for an object placed into fixed storage.
435 public:
438 assert(p && "FixedSizeStorageOwner owns null?");
439 p->~BaseTy();
440 }
441
442 BaseTy *get() { return p; }
443 const BaseTy *get() const { return p; }
444
445 private:
448 FixedSizeStorageOwner &operator=(const FixedSizeStorageOwner &) = delete;
449 FixedSizeStorageOwner &operator=(FixedSizeStorageOwner &&) = delete;
450
451 BaseTy *const p;
452 };
453};
454
455} // end namespace llvm
456
457#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Given that RA is a live value
uint64_t Size
#define T
#define P(N)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:303
A type that acts as the owner for an object placed into fixed storage.
See the file comment for details on the usage of the TrailingObjects type.
TrailingObjects & operator=(TrailingObjects &&)=delete
const FirstTrailingType * getTrailingObjects() const
MutableArrayRef< T > getTrailingObjects(size_t N)
const T * getTrailingObjectsNonStrict() const
static constexpr std::enable_if_t< std::is_same_v< Foo< TrailingTys... >, Foo< Tys... > >, size_t > totalSizeToAlloc(typename trailing_objects_internal::ExtractSecondType< TrailingTys, size_t >::type... Counts)
Returns the total size of an object if it were allocated with the given trailing object counts.
TrailingObjects & operator=(const TrailingObjects &)=delete
FirstTrailingType * getTrailingObjects()
ArrayRef< FirstTrailingType > getTrailingObjects(size_t N) const
typename std::tuple_element_t< 0, std::tuple< TrailingTys... > > FirstTrailingType
ArrayRef< T > getTrailingObjectsNonStrict(size_t N) const
static constexpr std::enable_if_t< std::is_same_v< Foo< TrailingTys... >, Foo< Tys... > >, size_t > additionalSizeToAlloc(typename trailing_objects_internal::ExtractSecondType< TrailingTys, size_t >::type... Counts)
Returns the size of the trailing data, if an object were allocated with the given counts (The counts ...
TrailingObjects(TrailingObjects &&)=delete
MutableArrayRef< T > getTrailingObjectsNonStrict(size_t N)
T * getTrailingObjects()
Returns a pointer to the trailing object array of the given type (which must be one of those specifie...
MutableArrayRef< FirstTrailingType > getTrailingObjects(size_t N)
const T * getTrailingObjects() const
Returns a pointer to the trailing object array of the given type (which must be one of those specifie...
ArrayRef< T > getTrailingObjects(size_t N) const
TrailingObjects(const TrailingObjects &)=delete
Helper template to calculate the max alignment requirement for a set of objects.
The base class for TrailingObjects* classes.
static const NextTy * getTrailingObjectsImpl(const BaseTy *Obj, TrailingObjectsBase::OverloadToken< NextTy >)
static constexpr size_t additionalSizeToAllocImpl(size_t SizeSoFar, size_t Count1, typename ExtractSecondType< MoreTys, size_t >::type... MoreCounts)
static NextTy * getTrailingObjectsImpl(BaseTy *Obj, TrailingObjectsBase::OverloadToken< NextTy >)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
Definition: Alignment.h:187
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
A type where its ::with_counts template member has a ::type member suitable for use as uninitialized ...
OverloadToken's purpose is to allow specifying function overloads for different types,...