Skip to content

Commit 1a99438

Browse files
committed
Add OGTypeNominalDescriptorName
1 parent e2c7ebc commit 1a99438

File tree

6 files changed

+146
-56
lines changed

6 files changed

+146
-56
lines changed

Package.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ if warningsAsErrorsCondition {
113113

114114
let openGraphShimsTarget = Target.target(
115115
name: "OpenGraphShims",
116+
cSettings: sharedCSettings,
116117
swiftSettings: sharedSwiftSettings
117118
)
118119

@@ -122,6 +123,7 @@ let openGraphShimsTestTarget = Target.testTarget(
122123
"OpenGraphShims",
123124
],
124125
exclude: ["README.md"],
126+
cSettings: sharedCSettings,
125127
swiftSettings: sharedSwiftSettings
126128
)
127129

@@ -131,6 +133,7 @@ let openGraphTestTarget = Target.testTarget(
131133
"OpenGraph",
132134
],
133135
exclude: ["README.md"],
136+
cSettings: sharedCSettings,
134137
swiftSettings: sharedSwiftSettings
135138
)
136139
let openGraphCompatibilityTestTarget = Target.testTarget(
@@ -139,6 +142,7 @@ let openGraphCompatibilityTestTarget = Target.testTarget(
139142
.product(name: "RealModule", package: "swift-numerics"),
140143
],
141144
exclude: ["README.md"],
145+
cSettings: sharedCSettings,
142146
swiftSettings: sharedSwiftSettings
143147
)
144148

@@ -165,6 +169,7 @@ let package = Package(
165169
.target(
166170
name: "OpenGraph",
167171
dependencies: ["OpenGraph_SPI"],
172+
cSettings: sharedCSettings,
168173
swiftSettings: sharedSwiftSettings
169174
),
170175
openGraphShimsTarget,

Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
OGTypeKind OGTypeGetKind(OGTypeID typeID) {
1616
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
17-
OG::swift::metadata const *metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
17+
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
1818
switch (metadata->getKind()) {
1919
case swift::MetadataKind::Class: // 0x0
2020
return OGTypeKindClass;
@@ -44,7 +44,7 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) {
4444

4545
void const* OGTypeGetSignature(OGTypeID typeID) {
4646
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
47-
OG::swift::metadata const *metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
47+
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
4848
// TODO
4949
return nullptr;
5050
#else
@@ -53,7 +53,7 @@ void const* OGTypeGetSignature(OGTypeID typeID) {
5353
}
5454
void const* OGTypeGetDescriptor(OGTypeID typeID) {
5555
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
56-
OG::swift::metadata const *metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
56+
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
5757
return metadata->descriptor();
5858
#else
5959
return nullptr;
@@ -65,21 +65,31 @@ void const* OGTypeGetDescriptor(OGTypeID typeID) {
6565
CFStringRef OGTypeDescription(OGTypeID typeID) {
6666
CFMutableStringRef ref = CFStringCreateMutable(CFAllocatorGetDefault(), 0);
6767
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
68-
OG::swift::metadata const *metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
68+
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
6969
metadata->append_description(ref);
7070
#endif
7171
return ref;
7272
}
7373

7474
void const* OGTypeNominalDescriptor(OGTypeID typeID) {
7575
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
76-
OG::swift::metadata const *metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
76+
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
77+
7778
return metadata->nominal_descriptor();
7879
#else
7980
return nullptr;
8081
#endif
8182
}
8283

83-
CFStringRef OGTypeNominalDescriptorName(OGTypeID typeID) {
84-
// TODO
84+
char const* OGTypeNominalDescriptorName(OGTypeID typeID) {
85+
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
86+
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
87+
auto nominal_descriptor = metadata->nominal_descriptor();
88+
if (nominal_descriptor == nullptr) {
89+
return nullptr;
90+
}
91+
return nominal_descriptor->Name.get();
92+
#else
93+
return nullptr;
94+
#endif
8595
}

Sources/OpenGraph_SPI/Runtime/OGTypeID.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.kind(sel
3737

3838
OG_EXPORT
3939
OG_REFINED_FOR_SWIFT
40-
void const* OGTypeGetSignature(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.signature(self:));
40+
void const* _Nullable OGTypeGetSignature(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.signature(self:));
4141

4242
OG_EXPORT
4343
OG_REFINED_FOR_SWIFT
44-
void const* OGTypeGetDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.descriptor(self:));
44+
void const* _Nullable OGTypeGetDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.descriptor(self:));
4545

4646
#endif /* OPENGRAPH_RELEASE */
4747

@@ -51,11 +51,11 @@ CFStringRef OGTypeDescription(OGTypeID typeID);
5151

5252
OG_EXPORT
5353
OG_REFINED_FOR_SWIFT
54-
void const* OGTypeNominalDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptor(self:));
54+
void const* _Nullable OGTypeNominalDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptor(self:));
5555

5656
OG_EXPORT
5757
OG_REFINED_FOR_SWIFT
58-
CFStringRef OGTypeNominalDescriptorName(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptorName(self:));
58+
char const* _Nullable OGTypeNominalDescriptorName(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptorName(self:));
5959

6060
OG_EXTERN_C_END
6161

Sources/OpenGraph_SPI/Runtime/metadata.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,6 @@
1111

1212
using OG::swift::metadata;
1313

14-
void const* metadata::descriptor() const OG_NOEXCEPT {
15-
// TODO
16-
return nullptr;
17-
}
18-
19-
void const* metadata::nominal_descriptor() const OG_NOEXCEPT {
20-
void const* descriptor = this->descriptor();
21-
if (descriptor == nullptr) {
22-
return nullptr;
23-
}
24-
// TODO
25-
return nullptr;
26-
}
27-
28-
2914
void metadata::append_description(CFMutableStringRef description) const OG_NOEXCEPT {
3015
// TODO
3116
}

Sources/OpenGraph_SPI/Runtime/metadata.hpp

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,65 @@
1212

1313
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
1414
#include <swift/Runtime/Metadata.h>
15-
#endif
15+
#include <swift/Runtime/HeapObject.h>
16+
using namespace swift;
17+
#endif /* OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED */
1618

1719
namespace OG {
1820
namespace swift {
1921
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
20-
class metadata: public ::swift::Metadata {
22+
class metadata: public Metadata {
2123
public:
22-
void const* descriptor() const OG_NOEXCEPT;
23-
void const* nominal_descriptor() const OG_NOEXCEPT;
24+
OG_INLINE OG_CONSTEXPR
25+
Metadata const* getType() const OG_NOEXCEPT {
26+
return this;
27+
}
28+
29+
OG_INLINE OG_CONSTEXPR
30+
TypeNamePair const name(bool qualified) const OG_NOEXCEPT {
31+
return swift_getTypeName(getType(), qualified);
32+
}
33+
34+
OG_INLINE OG_CONSTEXPR
35+
MetadataKind const getKind() const OG_NOEXCEPT {
36+
return getType()->getKind();
37+
}
38+
39+
OG_INLINE OG_CONSTEXPR
40+
TypeContextDescriptor const* descriptor() const OG_NOEXCEPT {
41+
switch (getKind()) {
42+
case MetadataKind::Class: {
43+
const auto cls = static_cast<const ClassMetadata *>(getType());
44+
// We may build this with a newer OS SDK but run on old OS.
45+
// So instead of using `isTypeMetadata` / `(Data & SWIFT_CLASS_IS_SWIFT_MASK)`,
46+
// we manully use 3 here to check isTypeMetadata
47+
if ((cls->Data & 3) == 0) return nullptr;
48+
return cls->getDescription();
49+
}
50+
case MetadataKind::Struct:
51+
case MetadataKind::Enum:
52+
case MetadataKind::Optional: {
53+
return static_cast<const TargetValueMetadata<InProcess> *>(getType())->Description;
54+
}
55+
default:
56+
return nullptr;
57+
}
58+
}
59+
60+
OG_INLINE OG_CONSTEXPR
61+
TypeContextDescriptor const* nominal_descriptor() const OG_NOEXCEPT {
62+
auto descriptor = this->descriptor();
63+
if (descriptor == nullptr) {
64+
return nullptr;
65+
}
66+
switch(descriptor->getKind()) {
67+
case ContextDescriptorKind::Struct:
68+
case ContextDescriptorKind::Enum:
69+
return descriptor;
70+
default:
71+
return nullptr;
72+
}
73+
}
2474

2575
void append_description(CFMutableStringRef description) const OG_NOEXCEPT;
2676
}; /* OG::swift::metadata */

Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,27 @@
55
import Testing
66

77
struct MetadataTests {
8+
class T1 {
9+
var a = 0
10+
var b: Double = 0
11+
}
12+
13+
struct T2 {
14+
var a: Int
15+
var b: Double
16+
}
17+
18+
enum T3 {
19+
case a, b
20+
}
21+
22+
protocol P {
23+
var a: Int { get }
24+
var b: Double { get }
25+
}
26+
827
@Test(.enabled(if: swiftToolchainSupported))
9-
func kindCases() throws {
10-
class T1 {}
11-
struct T2 {}
12-
enum T3 {}
13-
protocol P {}
14-
28+
func kind() throws {
1529
#expect(Metadata(T1.self).kind == .class)
1630
#expect(Metadata(T2.self).kind == .struct)
1731
#expect(Metadata(T3.self).kind == .enum)
@@ -34,29 +48,25 @@ struct MetadataTests {
3448
#expect(Metadata(type(of: Int.self)).kind == .metatype)
3549
}
3650

37-
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
38-
func descriptor() throws {
39-
let n1 = try #require(Metadata(Int.self).nominalDescriptor)
40-
let n2 = try #require(Metadata(String.self).nominalDescriptor)
41-
let n3 = try #require(Metadata(Int.self).nominalDescriptor)
42-
43-
#expect(n1 != n2)
44-
#expect(n1 == n3)
45-
}
46-
47-
class T1 {
48-
var a = 0
49-
var b: Double = 0
50-
}
51+
#if OPENGRAPH_RELEASE_2024
52+
@Test(.enabled(if: swiftToolchainSupported))
53+
func descriptor() {
54+
let t1 = Metadata(T1.self).descriptor
55+
let t2 = Metadata(T2.self).descriptor
56+
let t3 = Metadata(T3.self).descriptor
57+
let p = Metadata(P.self).descriptor
58+
let optionalP = Metadata(P?.self).descriptor
5159

52-
struct T2 {
53-
var a: Int
54-
var b: Double
55-
}
60+
#expect(t1 != nil)
61+
#expect(t2 != nil)
62+
#expect(t3 != nil)
5663

57-
enum T3 {
58-
case a, b
64+
#expect(p == nil)
65+
#expect(optionalP != nil)
66+
67+
#expect(t1 == Metadata(T1.self).descriptor)
5968
}
69+
#endif
6070

6171
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
6272
func description() {
@@ -65,6 +75,36 @@ struct MetadataTests {
6575
#expect(Metadata(T3.self).description == "MetadataTests.T3")
6676
}
6777

78+
@Test(.enabled(if: swiftToolchainSupported))
79+
func nominalDescriptor() {
80+
let t1 = Metadata(T1.self).nominalDescriptor
81+
let t2 = Metadata(T2.self).nominalDescriptor
82+
let t3 = Metadata(T3.self).nominalDescriptor
83+
let p = Metadata(P.self).nominalDescriptor
84+
let optionalP = Metadata(P?.self).nominalDescriptor
85+
86+
#expect(t1 == nil)
87+
#expect(t2 != nil)
88+
#expect(t3 != nil)
89+
#expect(p == nil)
90+
#expect(optionalP != nil)
91+
}
92+
93+
@Test(.enabled(if: swiftToolchainSupported))
94+
func nominalDescriptorName() throws {
95+
let t1 = Metadata(T1.self).nominalDescriptorName
96+
let t2 = Metadata(T2.self).nominalDescriptorName
97+
let t3 = Metadata(T3.self).nominalDescriptorName
98+
let p = Metadata(P.self).nominalDescriptorName
99+
let optionalP = Metadata(P?.self).nominalDescriptorName
100+
101+
#expect(t1 == nil)
102+
try #expect(String(cString: #require(t2)) == "T2")
103+
try #expect(String(cString: #require(t3)) == "T3")
104+
#expect(p == nil)
105+
try #expect(String(cString: #require(optionalP)) == "Optional")
106+
}
107+
68108
@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
69109
func forEachField() throws {
70110
for options in [OGTypeApplyOptions._1] {

0 commit comments

Comments
 (0)