Skip to content

Commit cb3818e

Browse files
committed
[NFC] Split the basic target-layout logic into its own file
1 parent 1b910f5 commit cb3818e

File tree

3 files changed

+221
-208
lines changed

3 files changed

+221
-208
lines changed

include/swift/ABI/Metadata.h

Lines changed: 32 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "swift/Runtime/Once.h"
3434
#include "swift/ABI/MetadataValues.h"
3535
#include "swift/ABI/System.h"
36+
#include "swift/ABI/TargetLayout.h"
3637
#include "swift/ABI/TrailingObjects.h"
3738
#include "swift/Basic/Malloc.h"
3839
#include "swift/Basic/FlaggedPointer.h"
@@ -66,146 +67,6 @@ template <typename Runtime> class TargetEnumDescriptor;
6667
template <typename Runtime> class TargetStructDescriptor;
6768
template <typename Runtime> struct TargetGenericMetadataPattern;
6869

69-
template <unsigned PointerSize>
70-
struct RuntimeTarget;
71-
72-
template <>
73-
struct RuntimeTarget<4> {
74-
using StoredPointer = uint32_t;
75-
// To avoid implicit conversions from StoredSignedPointer to StoredPointer.
76-
using StoredSignedPointer = struct {
77-
uint32_t SignedValue;
78-
};
79-
using StoredSize = uint32_t;
80-
using StoredPointerDifference = int32_t;
81-
static constexpr size_t PointerSize = 4;
82-
};
83-
84-
template <>
85-
struct RuntimeTarget<8> {
86-
using StoredPointer = uint64_t;
87-
// To avoid implicit conversions from StoredSignedPointer to StoredPointer.
88-
using StoredSignedPointer = struct {
89-
uint64_t SignedValue;
90-
};
91-
using StoredSize = uint64_t;
92-
using StoredPointerDifference = int64_t;
93-
static constexpr size_t PointerSize = 8;
94-
};
95-
96-
namespace reflection {
97-
class FieldDescriptor;
98-
}
99-
100-
/// In-process native runtime target.
101-
///
102-
/// For interactions in the runtime, this should be the equivalent of working
103-
/// with a plain old pointer type.
104-
struct InProcess {
105-
static constexpr size_t PointerSize = sizeof(uintptr_t);
106-
using StoredPointer = uintptr_t;
107-
using StoredSignedPointer = uintptr_t;
108-
using StoredSize = size_t;
109-
using StoredPointerDifference = ptrdiff_t;
110-
111-
#if SWIFT_OBJC_INTEROP
112-
static constexpr bool ObjCInterop = true;
113-
template <typename T>
114-
using TargetAnyClassMetadata = TargetAnyClassMetadataObjCInterop<T>;
115-
#else
116-
static constexpr bool ObjCInterop = false;
117-
template <typename T>
118-
using TargetAnyClassMetadata = TargetAnyClassMetadata<T>;
119-
#endif
120-
template <typename T>
121-
using TargetClassMetadata = TargetClassMetadata<T, TargetAnyClassMetadata<T>>;
122-
123-
static_assert(sizeof(StoredSize) == sizeof(StoredPointerDifference),
124-
"target uses differently-sized size_t and ptrdiff_t");
125-
126-
template <typename T>
127-
using Pointer = T*;
128-
129-
template <typename T>
130-
using SignedPointer = T;
131-
132-
template <typename T, bool Nullable = false>
133-
using FarRelativeDirectPointer = FarRelativeDirectPointer<T, Nullable>;
134-
135-
template <typename T, bool Nullable = false>
136-
using RelativeIndirectablePointer =
137-
RelativeIndirectablePointer<T, Nullable>;
138-
139-
template <typename T, bool Nullable = true>
140-
using RelativeDirectPointer = RelativeDirectPointer<T, Nullable>;
141-
};
142-
143-
/// Represents a pointer in another address space.
144-
///
145-
/// This type should not have * or -> operators -- you must as a memory reader
146-
/// to read the data at the stored address on your behalf.
147-
template <typename Runtime, typename Pointee>
148-
struct ExternalPointer {
149-
using StoredPointer = typename Runtime::StoredPointer;
150-
StoredPointer PointerValue;
151-
};
152-
153-
template <typename Runtime> struct WithObjCInterop {
154-
using StoredPointer = typename Runtime::StoredPointer;
155-
using StoredSignedPointer = typename Runtime::StoredSignedPointer;
156-
using StoredSize = typename Runtime::StoredSize;
157-
using StoredPointerDifference = typename Runtime::StoredPointerDifference;
158-
static constexpr size_t PointerSize = Runtime::PointerSize;
159-
static constexpr bool ObjCInterop = true;
160-
template <typename T>
161-
using TargetAnyClassMetadata = TargetAnyClassMetadataObjCInterop<T>;
162-
};
163-
164-
template <typename Runtime> struct NoObjCInterop {
165-
using StoredPointer = typename Runtime::StoredPointer;
166-
using StoredSignedPointer = typename Runtime::StoredSignedPointer;
167-
using StoredSize = typename Runtime::StoredSize;
168-
using StoredPointerDifference = typename Runtime::StoredPointerDifference;
169-
static constexpr size_t PointerSize = Runtime::PointerSize;
170-
static constexpr bool ObjCInterop = false;
171-
template <typename T>
172-
using TargetAnyClassMetadata = TargetAnyClassMetadata<T>;
173-
};
174-
175-
/// An external process's runtime target, which may be a different architecture,
176-
/// and may or may not have Objective-C interoperability.
177-
template <typename Runtime>
178-
struct External {
179-
using StoredPointer = typename Runtime::StoredPointer;
180-
using StoredSignedPointer = typename Runtime::StoredSignedPointer;
181-
using StoredSize = typename Runtime::StoredSize;
182-
using StoredPointerDifference = typename Runtime::StoredPointerDifference;
183-
template <typename T>
184-
using TargetAnyClassMetadata =
185-
typename Runtime::template TargetAnyClassMetadata<T>;
186-
template <typename T>
187-
using TargetClassMetadata = TargetClassMetadata<T, TargetAnyClassMetadata<T>>;
188-
189-
static constexpr size_t PointerSize = Runtime::PointerSize;
190-
static constexpr bool ObjCInterop = Runtime::ObjCInterop;
191-
const StoredPointer PointerValue;
192-
193-
template <typename T>
194-
using Pointer = StoredPointer;
195-
196-
template <typename T>
197-
using SignedPointer = StoredSignedPointer;
198-
199-
template <typename T, bool Nullable = false>
200-
using FarRelativeDirectPointer = StoredPointer;
201-
202-
template <typename T, bool Nullable = false>
203-
using RelativeIndirectablePointer = int32_t;
204-
205-
template <typename T, bool Nullable = true>
206-
using RelativeDirectPointer = int32_t;
207-
};
208-
20970
/// Template for branching on native pointer types versus external ones
21071
template <typename Runtime, template <typename> class Pointee>
21172
using TargetMetadataPointer
@@ -214,37 +75,28 @@ using TargetMetadataPointer
21475
template <typename Runtime, template <typename> class Pointee>
21576
using ConstTargetMetadataPointer
21677
= typename Runtime::template Pointer<const Pointee<Runtime>>;
217-
218-
template <typename Runtime, typename T>
219-
using TargetPointer = typename Runtime::template Pointer<T>;
220-
221-
template <typename Runtime, typename T>
222-
using TargetSignedPointer = typename Runtime::template SignedPointer<T>;
223-
224-
template <typename Runtime, typename T>
225-
using ConstTargetPointer = typename Runtime::template Pointer<const T>;
226-
227-
228-
template <typename Runtime, template <typename> class Pointee,
229-
bool Nullable = true>
230-
using ConstTargetFarRelativeDirectPointer
231-
= typename Runtime::template FarRelativeDirectPointer<const Pointee<Runtime>,
232-
Nullable>;
233-
234-
template <typename Runtime, typename Pointee, bool Nullable = true>
235-
using TargetRelativeDirectPointer
236-
= typename Runtime::template RelativeDirectPointer<Pointee, Nullable>;
237-
238-
template <typename Runtime, typename Pointee, bool Nullable = true>
239-
using TargetRelativeIndirectablePointer
240-
= typename Runtime::template RelativeIndirectablePointer<Pointee,Nullable>;
24178

24279
struct HeapObject;
24380
class WeakReference;
24481
struct UnownedReference;
24582

246-
template <typename Runtime> struct TargetMetadata;
247-
using Metadata = TargetMetadata<InProcess>;
83+
template <typename Runtime, bool ObjCInterop = Runtime::ObjCInterop>
84+
struct TargetAnyClassMetadataTypeImpl;
85+
template <typename Runtime>
86+
struct TargetAnyClassMetadataTypeImpl<Runtime, /*ObjCInterop*/ true> {
87+
using type = TargetAnyClassMetadataObjCInterop<Runtime>;
88+
};
89+
template <typename Runtime>
90+
struct TargetAnyClassMetadataTypeImpl<Runtime, /*ObjCInterop*/ false> {
91+
using type = TargetAnyClassMetadata<Runtime>;
92+
};
93+
template <typename Runtime>
94+
using TargetAnyClassMetadataType =
95+
typename TargetAnyClassMetadataTypeImpl<Runtime>::type;
96+
97+
template <typename Runtime>
98+
using TargetClassMetadataType =
99+
TargetClassMetadata<Runtime, TargetAnyClassMetadataType<Runtime>>;
248100

249101
/// The result of requesting type metadata. Generally the return value of
250102
/// a function.
@@ -730,23 +582,13 @@ struct TargetMetadata {
730582
getTypeContextDescriptor() const {
731583
switch (getKind()) {
732584
case MetadataKind::Class: {
733-
if (Runtime::ObjCInterop) {
734-
const auto cls = static_cast<const TargetClassMetadata<
735-
Runtime, TargetAnyClassMetadataObjCInterop<Runtime>> *>(this);
736-
if (!cls->isTypeMetadata())
737-
return nullptr;
738-
if (cls->isArtificialSubclass())
739-
return nullptr;
740-
return cls->getDescription();
741-
} else {
742-
const auto cls = static_cast<const TargetClassMetadata<
743-
Runtime, TargetAnyClassMetadata<Runtime>> *>(this);
744-
if (!cls->isTypeMetadata())
745-
return nullptr;
746-
if (cls->isArtificialSubclass())
747-
return nullptr;
748-
return cls->getDescription();
749-
}
585+
const auto cls =
586+
static_cast<const TargetClassMetadataType<Runtime> *>(this);
587+
if (!cls->isTypeMetadata())
588+
return nullptr;
589+
if (cls->isArtificialSubclass())
590+
return nullptr;
591+
return cls->getDescription();
750592
}
751593
case MetadataKind::Struct:
752594
case MetadataKind::Enum:
@@ -763,7 +605,7 @@ struct TargetMetadata {
763605

764606
/// Get the class object for this type if it has one, or return null if the
765607
/// type is not a class (or not a class with a class object).
766-
const typename Runtime::template TargetClassMetadata<Runtime> *
608+
const TargetClassMetadataType<Runtime> *
767609
getClassObject() const;
768610

769611
/// Retrieve the generic arguments of this type, if it has any.
@@ -1008,8 +850,7 @@ struct TargetClassMetadataBounds : TargetMetadataBounds<Runtime> {
1008850

1009851
using TargetMetadataBounds<Runtime>::NegativeSizeInWords;
1010852
using TargetMetadataBounds<Runtime>::PositiveSizeInWords;
1011-
using TargetClassMetadata =
1012-
typename Runtime::template TargetClassMetadata<Runtime>;
853+
using TargetClassMetadata = TargetClassMetadataType<Runtime>;
1013854

1014855
/// The offset from the address point of the metadata to the immediate
1015856
/// members.
@@ -1022,13 +863,10 @@ struct TargetClassMetadataBounds : TargetMetadataBounds<Runtime> {
1022863
: TargetMetadataBounds<Runtime>{negativeSizeInWords, positiveSizeInWords},
1023864
ImmediateMembersOffset(immediateMembersOffset) {}
1024865

1025-
template <typename T>
1026-
using TargetClassMetadataT = typename Runtime::template TargetClassMetadata<T>;
1027-
1028866
/// Return the basic bounds of all Swift class metadata.
1029867
/// The immediate members offset will not be meaningful.
1030868
static constexpr TargetClassMetadataBounds<Runtime> forSwiftRootClass() {
1031-
using Metadata = FullMetadata<TargetClassMetadataT<Runtime>>;
869+
using Metadata = FullMetadata<TargetClassMetadataType<Runtime>>;
1032870
return forAddressPointAndSize(sizeof(typename Metadata::HeaderType),
1033871
sizeof(Metadata));
1034872
}
@@ -1071,8 +909,7 @@ template <typename Runtime>
1071909
struct TargetAnyClassMetadata : public TargetHeapMetadata<Runtime> {
1072910
using StoredPointer = typename Runtime::StoredPointer;
1073911
using StoredSize = typename Runtime::StoredSize;
1074-
using TargetClassMetadata =
1075-
typename Runtime::template TargetClassMetadata<Runtime>;
912+
using TargetClassMetadata = TargetClassMetadataType<Runtime>;
1076913

1077914
protected:
1078915
constexpr TargetAnyClassMetadata(
@@ -1109,9 +946,7 @@ struct TargetAnyClassMetadataObjCInterop
1109946
: public TargetAnyClassMetadata<Runtime> {
1110947
using StoredPointer = typename Runtime::StoredPointer;
1111948
using StoredSize = typename Runtime::StoredSize;
1112-
1113-
using TargetClassMetadataObjCInterop =
1114-
swift::TargetClassMetadata<Runtime, TargetAnyClassMetadataObjCInterop<Runtime>>;
949+
using TargetClassMetadataObjCInterop = TargetClassMetadataType<Runtime>;
1115950

1116951
constexpr TargetAnyClassMetadataObjCInterop(
1117952
TargetAnyClassMetadataObjCInterop<Runtime> *isa,
@@ -1156,11 +991,7 @@ struct TargetAnyClassMetadataObjCInterop
1156991
}
1157992
};
1158993

1159-
#if SWIFT_OBJC_INTEROP
1160-
using AnyClassMetadata = TargetAnyClassMetadataObjCInterop<InProcess>;
1161-
#else
1162-
using AnyClassMetadata = TargetAnyClassMetadata<InProcess>;
1163-
#endif
994+
using AnyClassMetadata = TargetAnyClassMetadataType<InProcess>;
1164995

1165996
using ClassIVarDestroyer =
1166997
SWIFT_CC(swift) void(SWIFT_CONTEXT HeapObject *);
@@ -2594,7 +2425,7 @@ class RelativeTargetProtocolDescriptorPointer {
25942425
template <typename Runtime>
25952426
struct TargetTypeReference {
25962427
template <typename T>
2597-
using TargetClassMetadata = typename T::template TargetClassMetadata<T>;
2428+
using TargetClassMetadata = TargetClassMetadataType<T>;
25982429

25992430
union {
26002431
/// A direct reference to a TypeContextDescriptor or ProtocolDescriptor.

0 commit comments

Comments
 (0)