1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
|
//===--- MetadataImpl.h - Metadata implementation routines ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Declarations used to implement value witnesses for native C/C++ types.
//
// A box class defines some static members which describe the basic
// value-witness properties of a value:
//
// - NativeBox derives a box from a C++ type
// - SwiftRetainableBox is a box for Swift object pointers which uses
// swift_{retain,release}.
// - FunctionPointerBox is a box for function pointers.
// - ObjCRetainableBox is a box for Objective-C object pointers,
// using objc_{retain,release}.
// - UnknownObjectRetainableBox is a box for void* using
// swift_unknownObject{Retain,Release}.
// - AggregateBox<T...> is a box which uses swift layout rules to
// combine a number of different boxes.
//
// ValueWitnesses<T> takes a box class and defines all the necessary
// values and functions necessary to build a value witness table.
//
// ValueWitnessTableGenerator<T> takes an instance of ValueWitnesses
// and uses it to build a static member 'table', which can be used to
// constant-initialize a value witness table.
//
// ValueWitnessTable
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_RUNTIME_METADATAIMPL_H
#define SWIFT_RUNTIME_METADATAIMPL_H
#include "swift/Basic/MathUtils.h"
#include "swift/Runtime/Config.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Runtime/HeapObject.h"
#if SWIFT_OBJC_INTEROP
#include "swift/Runtime/ObjCBridge.h"
#endif
#include "WeakReference.h"
#include "EnumImpl.h"
#include <cstring>
#include <new>
#include <type_traits>
namespace swift {
namespace metadataimpl {
// concept Box<typename T> {
// using type = T;
// static constexpr size_t size;
// static constexpr size_t alignment;
// static constexpr size_t stride;
// static constexpr bool isPOD;
// static constexpr bool isBitwiseTakable;
// static constexpr unsigned numExtraInhabitants;
// static void destroy(T *);
// static T *initializeWithCopy(T *dest, T *src);
// static T *initializeWithTake(T *dest, T *src);
// static T *assignWithCopy(T *dest, T *src);
// static T *assignWithTake(T *dest, T *src);
// // Only if numExtraInhabitants is non-zero:
// static void storeExtraInhabitantTag(T *dest, unsigned index);
// static unsigned getExtraInhabitantTag(const T *src);
// };
/// A box class implemented in terms of C/C++ primitive operations.
/// The type is assumed to be non-polymorphic and to have no extra
/// inhabitants.
///
/// The size/alignment/stride template arguments are for when we want
/// to override the language defaults for a type.
template <class T,
size_t Alignment = alignof(T),
size_t Size = sizeof(T),
size_t Stride = sizeof(T)>
struct NativeBox {
using type = T;
static constexpr size_t size = Size;
static constexpr size_t alignment = Alignment;
static constexpr size_t stride = Stride;
static constexpr size_t isPOD = std::is_pod<T>::value;
static constexpr bool isBitwiseTakable = isPOD;
static constexpr unsigned numExtraInhabitants = 0;
static constexpr bool hasLayoutString = false;
static void destroy(T *value) {
value->T::~T();
}
static T *initializeWithCopy(T *dest, T *src) {
return ::new (dest) T(*src);
}
static T *initializeWithTake(T *dest, T *src) {
T *result = ::new (dest) T(std::move(*src));
src->T::~T();
return result;
}
static T *assignWithCopy(T *dest, T *src) {
*dest = *src;
return dest;
}
static T *assignWithTake(T *dest, T *src) {
*dest = std::move(*src);
src->T::~T();
return dest;
}
private:
static T *next(T *ptr, size_t n = 1) {
return (T*)((char*)ptr + stride * n);
}
static T *prev(T *ptr, size_t n = 1) {
return (T*)((char*)ptr - stride * n);
}
};
/// A CRTP base class for defining boxes of retainable pointers.
template <class Impl, class T> struct RetainableBoxBase {
using type = T;
static constexpr size_t size = sizeof(T);
static constexpr size_t alignment = alignof(T);
static constexpr size_t stride = sizeof(T);
static constexpr bool isPOD = false;
static constexpr bool isBitwiseTakable = true;
#ifdef SWIFT_THREADING_NONE
static constexpr bool isAtomic = false;
#else
static constexpr bool isAtomic = true;
#endif
static constexpr bool hasLayoutString = false;
static void destroy(T *addr) {
Impl::release(*addr);
}
static T *initializeWithCopy(T *dest, T *src) {
*dest = Impl::retain(*src);
return dest;
}
static T *initializeWithTake(T *dest, T *src) {
*dest = *src;
return dest;
}
static T *assignWithCopy(T *dest, T *src) {
T oldValue = *dest;
*dest = Impl::retain(*src);
Impl::release(oldValue);
return dest;
}
static T *assignWithTake(T *dest, T *src) {
T oldValue = *dest;
*dest = *src;
Impl::release(oldValue);
return dest;
}
// Right now, all object pointers are brought down to the least
// common denominator for extra inhabitants, so that we don't have
// to worry about e.g. type substitution on an enum type
// fundamentally changing the layout.
static constexpr unsigned numExtraInhabitants =
swift_getHeapObjectExtraInhabitantCount();
static void storeExtraInhabitantTag(T *dest, unsigned tag) {
swift_storeHeapObjectExtraInhabitant((HeapObject**) dest, tag - 1);
}
static unsigned getExtraInhabitantTag(const T *src) {
return swift_getHeapObjectExtraInhabitantIndex((HeapObject* const *) src) +1;
}
};
/// A box implementation class for Swift object pointers.
struct SwiftRetainableBox :
RetainableBoxBase<SwiftRetainableBox, HeapObject*> {
static HeapObject *retain(HeapObject *obj) {
if (isAtomic) {
swift_retain(obj);
} else {
swift_nonatomic_retain(obj);
}
return obj;
}
static void release(HeapObject *obj) {
if (isAtomic) {
swift_release(obj);
} else {
swift_nonatomic_release(obj);
}
}
};
/// A box implementation class for Swift unowned object pointers.
struct SwiftUnownedRetainableBox :
RetainableBoxBase<SwiftUnownedRetainableBox, HeapObject*> {
static HeapObject *retain(HeapObject *obj) {
if (isAtomic) {
swift_unownedRetain(obj);
} else {
swift_nonatomic_unownedRetain(obj);
}
return obj;
}
static void release(HeapObject *obj) {
if (isAtomic) {
swift_unownedRelease(obj);
} else {
swift_nonatomic_unownedRelease(obj);
}
}
#if SWIFT_OBJC_INTEROP
// The implementation from RetainableBoxBase is valid when interop is
// disabled.
static constexpr unsigned numExtraInhabitants = 1;
static void storeExtraInhabitant(HeapObject **dest, unsigned index) {
assert(index == 1);
*dest = nullptr;
}
static unsigned getExtraInhabitantTag(const HeapObject * const *src) {
return (*src == nullptr ? 1 : 0);
}
#endif
};
/// CRTP base class for weak reference boxes.
template<typename Impl, typename T>
struct WeakRetainableBoxBase {
using type = T;
static constexpr size_t size = sizeof(type);
static constexpr size_t alignment = alignof(type);
static constexpr size_t stride = sizeof(type);
static constexpr bool isPOD = false;
static constexpr bool isBitwiseTakable = false;
static constexpr unsigned numExtraInhabitants = 0;
static constexpr bool hasLayoutString = false;
// The implementation must provide implementations of:
// static void destroy(T *);
// static T *initializeWithCopy(T *dest, T *src);
// static T *initializeWithTake(T *dest, T *src);
// static T *assignWithCopy(T *dest, T *src);
// static T *assignWithTake(T *dest, T *src);
};
/// A box implementation class for Swift weak object pointers.
struct SwiftWeakRetainableBox :
WeakRetainableBoxBase<SwiftWeakRetainableBox, WeakReference> {
static void destroy(WeakReference *ref) {
swift_weakDestroy(ref);
}
static WeakReference *initializeWithCopy(WeakReference *dest,
WeakReference *src) {
swift_weakCopyInit(dest, src);
return dest;
}
static WeakReference *initializeWithTake(WeakReference *dest,
WeakReference *src) {
swift_weakTakeInit(dest, src);
return dest;
}
static WeakReference *assignWithCopy(WeakReference *dest,
WeakReference *src) {
swift_weakCopyAssign(dest, src);
return dest;
}
static WeakReference *assignWithTake(WeakReference *dest,
WeakReference *src) {
swift_weakTakeAssign(dest, src);
return dest;
}
};
#if SWIFT_OBJC_INTEROP
/// A box implementation class for Objective-C object pointers.
struct ObjCRetainableBox : RetainableBoxBase<ObjCRetainableBox, void*> {
static constexpr unsigned numExtraInhabitants =
swift_getHeapObjectExtraInhabitantCount();
static void *retain(void *obj) {
return objc_retain((id)obj);
}
static void release(void *obj) {
objc_release((id)obj);
}
};
/// A box implementation class for unowned Objective-C object pointers.
struct ObjCUnownedRetainableBox
: WeakRetainableBoxBase<ObjCUnownedRetainableBox, UnownedReference> {
static constexpr unsigned numExtraInhabitants = 1;
static void storeExtraInhabitantTag(UnownedReference *dest, unsigned index) {
assert(index == 1);
dest->Value = nullptr;
}
static unsigned getExtraInhabitantTag(const UnownedReference *src) {
return (src->Value == nullptr ? 1 : 0);
}
static void destroy(UnownedReference *ref) {
swift_unknownObjectUnownedDestroy(ref);
}
static UnownedReference *initializeWithCopy(UnownedReference *dest,
UnownedReference *src) {
swift_unknownObjectUnownedCopyInit(dest, src);
return dest;
}
static UnownedReference *initializeWithTake(UnownedReference *dest,
UnownedReference *src) {
swift_unknownObjectUnownedTakeInit(dest, src);
return dest;
}
static UnownedReference *assignWithCopy(UnownedReference *dest,
UnownedReference *src) {
swift_unknownObjectUnownedCopyAssign(dest, src);
return dest;
}
static UnownedReference *assignWithTake(UnownedReference *dest,
UnownedReference *src) {
swift_unknownObjectUnownedTakeAssign(dest, src);
return dest;
}
};
/// A box implementation class for ObjC weak object pointers.
struct ObjCWeakRetainableBox :
WeakRetainableBoxBase<ObjCWeakRetainableBox, WeakReference> {
static void destroy(WeakReference *ref) {
swift_unknownObjectWeakDestroy(ref);
}
static WeakReference *initializeWithCopy(WeakReference *dest,
WeakReference *src) {
swift_unknownObjectWeakCopyInit(dest, src);
return dest;
}
static WeakReference *initializeWithTake(WeakReference *dest,
WeakReference *src) {
swift_unknownObjectWeakTakeInit(dest, src);
return dest;
}
static WeakReference *assignWithCopy(WeakReference *dest,
WeakReference *src) {
swift_unknownObjectWeakCopyAssign(dest, src);
return dest;
}
static WeakReference *assignWithTake(WeakReference *dest,
WeakReference *src) {
swift_unknownObjectWeakTakeAssign(dest, src);
return dest;
}
};
#endif
/// A box implementation class for unknown-retainable object pointers.
struct UnknownObjectRetainableBox
: RetainableBoxBase<UnknownObjectRetainableBox, void *> {
static void *retain(void *obj) {
#if SWIFT_OBJC_INTEROP
swift_unknownObjectRetain(obj);
return obj;
#else
if (isAtomic) {
swift_retain(static_cast<HeapObject *>(obj));
} else {
swift_nonatomic_retain(static_cast<HeapObject *>(obj));
}
return static_cast<HeapObject *>(obj);
#endif
}
static void release(void *obj) {
#if SWIFT_OBJC_INTEROP
swift_unknownObjectRelease(obj);
#else
if (isAtomic) {
swift_release(static_cast<HeapObject *>(obj));
} else {
swift_nonatomic_release(static_cast<HeapObject *>(obj));
}
#endif
}
};
/// A box implementation class for BridgeObject.
struct BridgeObjectBox :
RetainableBoxBase<BridgeObjectBox, void*> {
static void *retain(void *obj) {
return swift_bridgeObjectRetain(obj);
}
static void release(void *obj) {
swift_bridgeObjectRelease(obj);
}
};
/// A box implementation class for unmanaged, pointer-aligned pointers.
/// Metatype values have this layout.
struct PointerPointerBox : NativeBox<void**> {
// TODO: we can do a lot better than this: we don't need to mask off
// the ObjC reserved bits, and we have spare bits.
static constexpr unsigned numExtraInhabitants =
swift_getHeapObjectExtraInhabitantCount();
static void storeExtraInhabitantTag(void ***dest, unsigned tag) {
swift_storeHeapObjectExtraInhabitant((HeapObject**) dest, tag - 1);
}
static unsigned getExtraInhabitantTag(void ** const *src) {
return swift_getHeapObjectExtraInhabitantIndex((HeapObject* const *) src)+1;
}
};
/// A box implementation class for raw pointers.
///
/// Note that this is used for imported `void * _Nonnull`, which may include
/// reinterpret_cast-ed integers, so we only get NULL as an extra inhabitant.
struct RawPointerBox : NativeBox<void*> {
static constexpr unsigned numExtraInhabitants = 1;
static void storeExtraInhabitantTag(void **dest, unsigned tag) {
assert(tag == 1);
*dest = nullptr;
}
static unsigned getExtraInhabitantTag(void* const *src) {
return *src == nullptr ? 1 : 0;
}
};
/// A box implementation class for unmanaged function pointers.
/// @convention(thin) functions have this layout, as do the first elements of
/// Swift thick functions.
struct FunctionPointerBox : NativeBox<void*> {
static constexpr unsigned numExtraInhabitants =
swift_getFunctionPointerExtraInhabitantCount();
static void storeExtraInhabitantTag(void **dest, unsigned tag) {
swift_storeFunctionPointerExtraInhabitant(dest, tag - 1);
}
static unsigned getExtraInhabitantTag(void * const *src) {
return swift_getFunctionPointerExtraInhabitantIndex(src) + 1;
}
};
// A helper template for building an AggregateBox. The more natural
// way to do this would be to left-recurse, but we have to
// right-recurse because C++ only lets you pattern-match things off
// the beginning of a pack.
template <size_t StartOffset, class... EltBoxes>
struct AggregateBoxHelper;
// Base case: empty list.
template <size_t StartOffset>
struct AggregateBoxHelper<StartOffset> {
public:
static constexpr size_t endOffset = StartOffset;
static constexpr size_t alignment = 1;
static constexpr bool isPOD = true;
static constexpr bool isBitwiseTakable = true;
public:
#define COPY_OP(OP) \
static char *OP(char *dest, char *src) { \
return dest; \
}
COPY_OP(initializeWithCopy)
COPY_OP(initializeWithTake)
COPY_OP(assignWithCopy)
COPY_OP(assignWithTake)
#undef COPY_OP
static void destroy(char *addr) {}
};
// Recursive case: add an element to the start.
template <size_t StartOffset, class EltBox, class... NextBoxes>
struct AggregateBoxHelper<StartOffset, EltBox, NextBoxes...> {
private:
static constexpr size_t eltOffset =
roundUpToAlignment(StartOffset, EltBox::alignment);
static constexpr size_t startToEltOffset = (eltOffset - StartOffset);
static constexpr size_t nextOffset = eltOffset + EltBox::size;
using NextHelper = AggregateBoxHelper<nextOffset, NextBoxes...>;
public:
static constexpr size_t endOffset = NextHelper::endOffset;
static constexpr size_t alignment =
(NextHelper::alignment > EltBox::alignment
? NextHelper::alignment : EltBox::alignment);
static constexpr bool isPOD = EltBox::isPOD && NextHelper::isPOD;
static constexpr bool isBitwiseTakable =
EltBox::isBitwiseTakable && NextHelper::isBitwiseTakable;
private:
static constexpr size_t eltToNextOffset = (nextOffset - eltOffset);
static constexpr size_t startToNextOffset = (nextOffset - StartOffset);
public:
#define COPY_OP(OP) \
static char *OP(char *dest, char *src) { \
dest += startToEltOffset; \
src += startToEltOffset; \
dest = (char*) EltBox::OP((typename EltBox::type*) dest, \
(typename EltBox::type*) src); \
dest = NextHelper::OP(dest + eltToNextOffset, src + eltToNextOffset); \
return dest - startToNextOffset; \
}
COPY_OP(initializeWithCopy)
COPY_OP(initializeWithTake)
COPY_OP(assignWithCopy)
COPY_OP(assignWithTake)
#undef COPY_OP
static void destroy(char *addr) {
// We have no particular reason to destroy in either order.
addr += startToEltOffset;
EltBox::destroy((typename EltBox::type*) addr);
NextHelper::destroy(addr + eltToNextOffset);
}
};
/// A class which produces a tuple-like box (with Swift layout rules)
/// for a list of element boxes.
///
/// The aggregate box is monomorphic and has no extra inhabitants.
template <class... EltBoxes>
struct AggregateBox {
using type = char;
using Helper = AggregateBoxHelper<0, EltBoxes...>;
static constexpr size_t size = Helper::endOffset;
static constexpr size_t alignment = Helper::alignment;
static constexpr size_t rawStride = roundUpToAlignment(size, alignment);
static constexpr size_t stride = rawStride == 0 ? 1 : rawStride;
static constexpr bool isPOD = Helper::isPOD;
static constexpr bool isBitwiseTakable = Helper::isBitwiseTakable;
/// Don't collect extra inhabitants from the members by default.
static constexpr unsigned numExtraInhabitants = 0;
static void destroy(char *value) {
Helper::destroy(value);
}
static char *initializeWithCopy(char *dest, char *src) {
return Helper::initializeWithCopy(dest, src);
}
static char *initializeWithTake(char *dest, char *src) {
return Helper::initializeWithTake(dest, src);
}
static char *assignWithCopy(char *dest, char *src) {
return Helper::assignWithCopy(dest, src);
}
static char *assignWithTake(char *dest, char *src) {
return Helper::assignWithTake(dest, src);
}
};
/// A template for using the Swift allocation APIs with a known size
/// and alignment.
template <size_t Size, size_t Alignment>
struct SwiftAllocator {
static void *alloc() {
return swift_slowAlloc(Size, Alignment-1);
}
static void dealloc(void *addr) {
swift_slowDealloc(addr, Size, Alignment-1);
}
};
/// A CRTP class which provides basic implementations for a number of
/// value witnesses relating to buffers.
template <class Impl>
struct BufferValueWitnessesBase {};
/// How should a type be packed into a fixed-size buffer?
enum class FixedPacking {
Allocate,
OffsetZero
};
constexpr FixedPacking getFixedPacking(bool isBitwiseTakable, size_t size,
size_t alignment) {
return (canBeInline(isBitwiseTakable, size, alignment)
? FixedPacking::OffsetZero
: FixedPacking::Allocate);
}
/// A CRTP base class which provides default implementations of a
/// number of value witnesses.
template <class Impl, bool isBitwiseTakable, size_t Size, size_t Alignment,
FixedPacking Packing =
getFixedPacking(isBitwiseTakable, Size, Alignment)>
struct BufferValueWitnesses;
/// An implementation of ValueBase suitable for classes that can be
/// allocated inline.
template <class Impl, bool isBitwiseTakable, size_t Size, size_t Alignment>
struct BufferValueWitnesses<Impl, isBitwiseTakable, Size, Alignment,
FixedPacking::OffsetZero>
: BufferValueWitnessesBase<Impl> {
static constexpr bool isInline = true;
static OpaqueValue *initializeBufferWithCopyOfBuffer(ValueBuffer *dest,
ValueBuffer *src,
const Metadata *self) {
return Impl::initializeWithCopy(reinterpret_cast<OpaqueValue *>(dest),
reinterpret_cast<OpaqueValue *>(src), self);
}
};
/// An implementation of BufferValueWitnesses suitable for types that
/// cannot be allocated inline.
template <class Impl, bool isBitwiseTakable, size_t Size, size_t Alignment>
struct BufferValueWitnesses<Impl, isBitwiseTakable, Size, Alignment,
FixedPacking::Allocate>
: BufferValueWitnessesBase<Impl> {
static constexpr bool isInline = false;
static OpaqueValue *initializeBufferWithCopyOfBuffer(ValueBuffer *dest,
ValueBuffer *src,
const Metadata *self) {
auto wtable = self->getValueWitnesses();
auto reference = src->PrivateData[0];
dest->PrivateData[0] = reference;
swift_retain(reinterpret_cast<HeapObject *>(reference));
// Project the address of the value in the buffer.
unsigned alignMask = wtable->getAlignmentMask();
// Compute the byte offset of the object in the box.
unsigned byteOffset = (sizeof(HeapObject) + alignMask) & ~alignMask;
auto *bytePtr = reinterpret_cast<char *>(reference);
return reinterpret_cast<OpaqueValue *>(bytePtr + byteOffset);
}
};
/// A class which provides BufferValueWitnesses for types that are not
/// fixed in size.
template <class Impl, bool IsKnownAllocated>
struct NonFixedBufferValueWitnesses : BufferValueWitnessesBase<Impl> {
static OpaqueValue *initializeBufferWithCopyOfBuffer(ValueBuffer *dest,
ValueBuffer *src,
const Metadata *self) {
auto vwtable = self->getValueWitnesses();
(void)vwtable;
if (!IsKnownAllocated && vwtable->isValueInline()) {
return Impl::initializeWithCopy(reinterpret_cast<OpaqueValue*>(dest),
reinterpret_cast<OpaqueValue*>(src),
self);
} else {
auto reference = src->PrivateData[0];
dest->PrivateData[0] = reference;
swift_retain(reinterpret_cast<HeapObject*>(reference));
// Project the address of the value in the buffer.
unsigned alignMask = vwtable->getAlignmentMask();
// Compute the byte offset of the object in the box.
unsigned byteOffset = (sizeof(HeapObject) + alignMask) & ~alignMask;
auto *bytePtr = reinterpret_cast<char *>(reference);
return reinterpret_cast<OpaqueValue *>(bytePtr + byteOffset);
}
}
};
/// Provides implementations for
/// getEnumTagSinglePayload/storeEnumTagSinglePayload.
template <class Impl, bool isBitwiseTakable, size_t Size, size_t Alignment,
bool hasExtraInhabitants>
struct FixedSizeBufferValueWitnesses;
/// A fixed size buffer value witness that can rely on the presence of the extra
/// inhabitant functions.
template <class Impl, bool isBitwiseTakable, size_t Size, size_t Alignment>
struct FixedSizeBufferValueWitnesses<Impl, isBitwiseTakable, Size, Alignment,
true /*hasExtraInhabitants*/>
: BufferValueWitnesses<Impl, isBitwiseTakable, Size, Alignment> {
static unsigned getEnumTagSinglePayload(const OpaqueValue *enumAddr,
unsigned numEmptyCases,
const Metadata *self) {
return getEnumTagSinglePayloadImpl(enumAddr, numEmptyCases, self, Size,
Impl::extraInhabitantCount,
Impl::getExtraInhabitantTag);
}
static void storeEnumTagSinglePayload(OpaqueValue *enumAddr,
unsigned whichCase,
unsigned numEmptyCases,
const Metadata *self) {
return storeEnumTagSinglePayloadImpl(enumAddr, whichCase, numEmptyCases,
self, Size, Impl::extraInhabitantCount,
Impl::storeExtraInhabitantTag);
}
};
/// A fixed size buffer value witness that cannot rely on the presence of the
/// extra inhabitant functions.
template <class Impl, bool isBitwiseTakable, size_t Size, size_t Alignment>
struct FixedSizeBufferValueWitnesses<Impl, isBitwiseTakable, Size, Alignment,
false /*hasExtraInhabitants*/>
: BufferValueWitnesses<Impl, isBitwiseTakable, Size, Alignment> {
static unsigned getEnumTagSinglePayload(const OpaqueValue *enumAddr,
unsigned numEmptyCases,
const Metadata *self) {
return getEnumTagSinglePayloadImpl(enumAddr, numEmptyCases, self, Size, 0,
nullptr);
}
static void storeEnumTagSinglePayload(OpaqueValue *enumAddr,
unsigned whichCase,
unsigned numEmptyCases,
const Metadata *self) {
return storeEnumTagSinglePayloadImpl(enumAddr, whichCase, numEmptyCases,
self, Size, 0, nullptr);
}
};
static constexpr bool hasExtraInhabitants(unsigned numExtraInhabitants) {
return numExtraInhabitants != 0;
}
/// A class which provides default implementations of various value
/// witnesses based on a box's value operations.
///
/// The box type has to provide a numExtraInhabitants member, but as
/// long as it's zero, the rest is fine.
template <class Box>
struct ValueWitnesses
: FixedSizeBufferValueWitnesses<
ValueWitnesses<Box>, Box::isBitwiseTakable, Box::size, Box::alignment,
hasExtraInhabitants(Box::numExtraInhabitants)> {
using Base = FixedSizeBufferValueWitnesses<
ValueWitnesses<Box>, Box::isBitwiseTakable, Box::size, Box::alignment,
hasExtraInhabitants(Box::numExtraInhabitants)>;
static constexpr size_t size = Box::size;
static constexpr size_t stride = Box::stride;
static constexpr size_t alignment = Box::alignment;
static constexpr bool isPOD = Box::isPOD;
static constexpr bool isBitwiseTakable = Box::isBitwiseTakable;
static constexpr unsigned extraInhabitantCount = Box::numExtraInhabitants;
static constexpr bool hasExtraInhabitants = (extraInhabitantCount != 0);
static constexpr ValueWitnessFlags flags =
ValueWitnessFlags().withAlignmentMask(alignment - 1)
.withInlineStorage(Base::isInline && isBitwiseTakable)
.withPOD(isPOD)
.withBitwiseTakable(isBitwiseTakable);
static constexpr bool hasLayoutString = Box::hasLayoutString;
static void destroy(OpaqueValue *value, const Metadata *self) {
return Box::destroy((typename Box::type*) value);
}
static OpaqueValue *initializeWithCopy(OpaqueValue *dest, OpaqueValue *src,
const Metadata *self) {
return (OpaqueValue*) Box::initializeWithCopy((typename Box::type*) dest,
(typename Box::type*) src);
}
static OpaqueValue *initializeWithTake(OpaqueValue *dest, OpaqueValue *src,
const Metadata *self) {
return (OpaqueValue*) Box::initializeWithTake((typename Box::type*) dest,
(typename Box::type*) src);
}
static OpaqueValue *assignWithCopy(OpaqueValue *dest, OpaqueValue *src,
const Metadata *self) {
return (OpaqueValue*) Box::assignWithCopy((typename Box::type*) dest,
(typename Box::type*) src);
}
static OpaqueValue *assignWithTake(OpaqueValue *dest, OpaqueValue *src,
const Metadata *self) {
return (OpaqueValue*) Box::assignWithTake((typename Box::type*) dest,
(typename Box::type*) src);
}
// These should not get instantiated if the type doesn't have extra
// inhabitants.
SWIFT_CC(swift)
static void storeExtraInhabitantTag(OpaqueValue *dest, unsigned tag,
unsigned xiCount, const Metadata *self) {
Box::storeExtraInhabitantTag((typename Box::type*) dest, tag);
}
SWIFT_CC(swift)
static unsigned getExtraInhabitantTag(const OpaqueValue *src,
unsigned xiCount,
const Metadata *self) {
return Box::getExtraInhabitantTag((typename Box::type const *) src);
}
};
/// A class which provides basic implementations of various function
/// value witnesses based on a type that is not fixed in size.
///
/// The 'Box' concept here is slightly different from the one for
/// fixed-size types: it does not need to provide size/alignment/isPOD
/// members, and its functions all take an extra 'const Metadata *self'
/// argument.
///
/// \tparam IsKnownAllocated - whether the type is known to not fit in
/// a fixed-size buffer
template <class Box, bool IsKnownAllocated>
struct NonFixedValueWitnesses :
NonFixedBufferValueWitnesses<NonFixedValueWitnesses<Box, IsKnownAllocated>,
IsKnownAllocated>
{
static constexpr unsigned numExtraInhabitants = Box::numExtraInhabitants;
static constexpr bool hasExtraInhabitants = (numExtraInhabitants != 0);
static void destroy(OpaqueValue *value, const Metadata *self) {
return Box::destroy((typename Box::type*) value, self);
}
static OpaqueValue *initializeWithCopy(OpaqueValue *dest, OpaqueValue *src,
const Metadata *self) {
return (OpaqueValue*) Box::initializeWithCopy((typename Box::type*) dest,
(typename Box::type*) src,
self);
}
static OpaqueValue *initializeWithTake(OpaqueValue *dest, OpaqueValue *src,
const Metadata *self) {
return (OpaqueValue*) Box::initializeWithTake((typename Box::type*) dest,
(typename Box::type*) src,
self);
}
static OpaqueValue *assignWithCopy(OpaqueValue *dest, OpaqueValue *src,
const Metadata *self) {
return (OpaqueValue*) Box::assignWithCopy((typename Box::type*) dest,
(typename Box::type*) src,
self);
}
static OpaqueValue *assignWithTake(OpaqueValue *dest, OpaqueValue *src,
const Metadata *self) {
return (OpaqueValue*) Box::assignWithTake((typename Box::type*) dest,
(typename Box::type*) src,
self);
}
static unsigned getEnumTagSinglePayload(const OpaqueValue *enumAddr,
unsigned numEmptyCases,
const Metadata *self) {
auto *payloadWitnesses = self->getValueWitnesses();
auto size = payloadWitnesses->getSize();
auto numExtraInhabitants = payloadWitnesses->getNumExtraInhabitants();
return getEnumTagSinglePayloadImpl(enumAddr, numEmptyCases, self, size,
numExtraInhabitants,
getExtraInhabitantTag);
}
static void storeEnumTagSinglePayload(OpaqueValue *enumAddr,
unsigned whichCase,
unsigned numEmptyCases,
const Metadata *self) {
auto *payloadWitnesses = self->getValueWitnesses();
auto size = payloadWitnesses->getSize();
auto numExtraInhabitants = payloadWitnesses->getNumExtraInhabitants();
storeEnumTagSinglePayloadImpl(enumAddr, whichCase, numEmptyCases, self,
size, numExtraInhabitants,
storeExtraInhabitantTag);
}
// These should not get instantiated if the type doesn't have extra
// inhabitants.
SWIFT_CC(swift)
static void storeExtraInhabitantTag(OpaqueValue *dest, unsigned tag,
unsigned xiCount, const Metadata *self) {
Box::storeExtraInhabitantTag((typename Box::type*) dest, tag);
}
SWIFT_CC(swift)
static unsigned getExtraInhabitantTag(const OpaqueValue *src,
unsigned xiCount,
const Metadata *self) {
return Box::getExtraInhabitantTag((typename Box::type const *) src);
}
};
/// A class which defines a ValueWitnessTable.
template <class Witnesses>
struct ValueWitnessTableGenerator {
static constexpr const ValueWitnessTable table = {
#define WANT_ONLY_REQUIRED_VALUE_WITNESSES
#define VALUE_WITNESS(LOWER_ID, UPPER_ID) Witnesses::LOWER_ID,
#include "swift/ABI/ValueWitness.def"
};
};
/// A convenient way to get the value witness table for a box class.
template <class Box>
using ValueWitnessTableForBox = ValueWitnessTableGenerator<ValueWitnesses<Box>>;
} // end namespace metadataimpl
} // end namespace swift
#endif /* SWIFT_RUNTIME_METADATAIMPL_H */
|