Skip to content

Commit 5f133d4

Browse files
committed
[IRGen] NFC, move MetadataSource into its own header
1 parent ef5d11a commit 5f133d4

File tree

2 files changed

+109
-84
lines changed

2 files changed

+109
-84
lines changed

lib/IRGen/GenProto.h

+1-84
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "Fulfillment.h"
2323
#include "GenericRequirement.h"
24+
#include "MetadataSource.h"
2425

2526
namespace llvm {
2627
class Type;
@@ -179,90 +180,6 @@ namespace irgen {
179180
CanType srcType,
180181
ProtocolConformanceRef conformance);
181182

182-
class MetadataSource {
183-
public:
184-
enum class Kind {
185-
/// Metadata is derived from a source class pointer.
186-
ClassPointer,
187-
188-
/// Metadata is derived from a type metadata pointer.
189-
Metadata,
190-
191-
/// Metadata is derived from the origin type parameter.
192-
GenericLValueMetadata,
193-
194-
/// Metadata is obtained directly from the from a Self metadata
195-
/// parameter passed via the WitnessMethod convention.
196-
SelfMetadata,
197-
198-
/// Metadata is derived from the Self witness table parameter
199-
/// passed via the WitnessMethod convention.
200-
SelfWitnessTable,
201-
202-
/// Metadata is obtained directly from the FixedType indicated. Used with
203-
/// Objective-C generics, where the actual argument is erased at runtime
204-
/// and its existential bound is used instead.
205-
ErasedTypeMetadata,
206-
};
207-
208-
static bool requiresSourceIndex(Kind kind) {
209-
return (kind == Kind::ClassPointer ||
210-
kind == Kind::Metadata ||
211-
kind == Kind::GenericLValueMetadata);
212-
}
213-
214-
static bool requiresFixedType(Kind kind) {
215-
return (kind == Kind::ErasedTypeMetadata);
216-
}
217-
218-
enum : unsigned { InvalidSourceIndex = ~0U };
219-
220-
private:
221-
/// The kind of source this is.
222-
Kind TheKind;
223-
224-
/// For ClassPointer, Metadata, and GenericLValueMetadata, the source index;
225-
/// for ErasedTypeMetadata, the type; for others, Index should be set to
226-
/// InvalidSourceIndex.
227-
union {
228-
unsigned Index;
229-
CanType FixedType;
230-
};
231-
232-
public:
233-
CanType Type;
234-
235-
MetadataSource(Kind kind, CanType type)
236-
: TheKind(kind), Index(InvalidSourceIndex), Type(type)
237-
{
238-
assert(!requiresSourceIndex(kind) && !requiresFixedType(kind));
239-
}
240-
241-
242-
MetadataSource(Kind kind, CanType type, unsigned index)
243-
: TheKind(kind), Index(index), Type(type) {
244-
assert(requiresSourceIndex(kind));
245-
assert(index != InvalidSourceIndex);
246-
}
247-
248-
MetadataSource(Kind kind, CanType type, CanType fixedType)
249-
: TheKind(kind), FixedType(fixedType), Type(type) {
250-
assert(requiresFixedType(kind));
251-
}
252-
253-
Kind getKind() const { return TheKind; }
254-
255-
unsigned getParamIndex() const {
256-
assert(requiresSourceIndex(getKind()));
257-
return Index;
258-
}
259-
260-
CanType getFixedType() const {
261-
assert(requiresFixedType(getKind()));
262-
return FixedType;
263-
}
264-
};
265-
266183
using GenericParamFulfillmentCallback =
267184
llvm::function_ref<void(CanType genericParamType,
268185
const MetadataSource &source,

lib/IRGen/MetadataSource.h

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//===--- MetadataSource.h - structure for the source of metadata *- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_IRGEN_METADATA_SOURCE_H
14+
#define SWIFT_IRGEN_METADATA_SOURCE_H
15+
16+
#include "swift/AST/Types.h"
17+
18+
namespace swift {
19+
namespace irgen {
20+
21+
class MetadataSource {
22+
public:
23+
enum class Kind {
24+
/// Metadata is derived from a source class pointer.
25+
ClassPointer,
26+
27+
/// Metadata is derived from a type metadata pointer.
28+
Metadata,
29+
30+
/// Metadata is derived from the origin type parameter.
31+
GenericLValueMetadata,
32+
33+
/// Metadata is obtained directly from the from a Self metadata
34+
/// parameter passed via the WitnessMethod convention.
35+
SelfMetadata,
36+
37+
/// Metadata is derived from the Self witness table parameter
38+
/// passed via the WitnessMethod convention.
39+
SelfWitnessTable,
40+
41+
/// Metadata is obtained directly from the FixedType indicated. Used with
42+
/// Objective-C generics, where the actual argument is erased at runtime
43+
/// and its existential bound is used instead.
44+
ErasedTypeMetadata,
45+
};
46+
47+
static bool requiresSourceIndex(Kind kind) {
48+
return (kind == Kind::ClassPointer ||
49+
kind == Kind::Metadata ||
50+
kind == Kind::GenericLValueMetadata);
51+
}
52+
53+
static bool requiresFixedType(Kind kind) {
54+
return (kind == Kind::ErasedTypeMetadata);
55+
}
56+
57+
enum : unsigned { InvalidSourceIndex = ~0U };
58+
59+
private:
60+
/// The kind of source this is.
61+
Kind TheKind;
62+
63+
/// For ClassPointer, Metadata, and GenericLValueMetadata, the source index;
64+
/// for ErasedTypeMetadata, the type; for others, Index should be set to
65+
/// InvalidSourceIndex.
66+
union {
67+
unsigned Index;
68+
CanType FixedType;
69+
};
70+
71+
public:
72+
CanType Type;
73+
74+
MetadataSource(Kind kind, CanType type)
75+
: TheKind(kind), Index(InvalidSourceIndex), Type(type)
76+
{
77+
assert(!requiresSourceIndex(kind) && !requiresFixedType(kind));
78+
}
79+
80+
81+
MetadataSource(Kind kind, CanType type, unsigned index)
82+
: TheKind(kind), Index(index), Type(type) {
83+
assert(requiresSourceIndex(kind));
84+
assert(index != InvalidSourceIndex);
85+
}
86+
87+
MetadataSource(Kind kind, CanType type, CanType fixedType)
88+
: TheKind(kind), FixedType(fixedType), Type(type) {
89+
assert(requiresFixedType(kind));
90+
}
91+
92+
Kind getKind() const { return TheKind; }
93+
94+
unsigned getParamIndex() const {
95+
assert(requiresSourceIndex(getKind()));
96+
return Index;
97+
}
98+
99+
CanType getFixedType() const {
100+
assert(requiresFixedType(getKind()));
101+
return FixedType;
102+
}
103+
};
104+
105+
} // end namespace irgen
106+
} // end namespace swift
107+
108+
#endif

0 commit comments

Comments
 (0)