Skip to content

Commit 5f169b4

Browse files
committed
Get typerefs for function type metadata
1 parent 04444c4 commit 5f169b4

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

include/swift/ABI/MetadataValues.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,9 @@ enum class FunctionMetadataConvention: uint8_t {
448448
};
449449

450450
/// Flags in a function type metadata record.
451-
class FunctionTypeFlags {
452-
typedef size_t int_type;
451+
template <typename Runtime>
452+
class TargetFunctionTypeFlags {
453+
using int_type = typename Runtime::StoredSize;
453454
enum : int_type {
454455
NumArgumentsMask = 0x00FFFFFFU,
455456
ConventionMask = 0x0F000000U,
@@ -458,22 +459,24 @@ class FunctionTypeFlags {
458459
};
459460
int_type Data;
460461

461-
constexpr FunctionTypeFlags(int_type Data) : Data(Data) {}
462+
constexpr TargetFunctionTypeFlags(int_type Data) : Data(Data) {}
462463
public:
463-
constexpr FunctionTypeFlags() : Data(0) {}
464+
constexpr TargetFunctionTypeFlags() : Data(0) {}
464465

465-
constexpr FunctionTypeFlags withNumArguments(unsigned numArguments) const {
466-
return FunctionTypeFlags((Data & ~NumArgumentsMask) | numArguments);
466+
constexpr TargetFunctionTypeFlags withNumArguments(unsigned numArguments) const {
467+
return TargetFunctionTypeFlags((Data & ~NumArgumentsMask) | numArguments);
467468
}
468469

469-
constexpr FunctionTypeFlags withConvention(FunctionMetadataConvention c) const {
470-
return FunctionTypeFlags((Data & ~ConventionMask)
470+
constexpr TargetFunctionTypeFlags<Runtime>
471+
withConvention(FunctionMetadataConvention c) const {
472+
return TargetFunctionTypeFlags((Data & ~ConventionMask)
471473
| (int_type(c) << ConventionShift));
472474
}
473475

474-
constexpr FunctionTypeFlags withThrows(bool throws) const {
475-
return FunctionTypeFlags((Data & ~ThrowsMask)
476-
| (throws ? ThrowsMask : 0));
476+
constexpr TargetFunctionTypeFlags<Runtime>
477+
withThrows(bool throws) const {
478+
return TargetFunctionTypeFlags<Runtime>((Data & ~ThrowsMask) |
479+
(throws ? ThrowsMask : 0));
477480
}
478481

479482
unsigned getNumArguments() const {
@@ -492,17 +495,18 @@ class FunctionTypeFlags {
492495
return Data;
493496
}
494497

495-
static FunctionTypeFlags fromIntValue(int_type Data) {
496-
return FunctionTypeFlags(Data);
498+
static TargetFunctionTypeFlags<Runtime> fromIntValue(int_type Data) {
499+
return TargetFunctionTypeFlags(Data);
497500
}
498501

499-
bool operator==(FunctionTypeFlags other) const {
502+
bool operator==(TargetFunctionTypeFlags<Runtime> other) const {
500503
return Data == other.Data;
501504
}
502-
bool operator!=(FunctionTypeFlags other) const {
505+
bool operator!=(TargetFunctionTypeFlags<Runtime> other) const {
503506
return Data != other.Data;
504507
}
505508
};
509+
using FunctionTypeFlags = TargetFunctionTypeFlags<InProcess>;
506510

507511
/// Field types and flags as represented in a nominal type's field/case type
508512
/// vector.

include/swift/Reflection/ReflectionContext.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,27 @@ class ReflectionContext {
330330
}
331331
case MetadataKind::Function: {
332332
auto Function = cast<TargetFunctionTypeMetadata<Runtime>>(Meta.get());
333+
StoredPointer FlagsAddress = MetadataAddress +
334+
TargetFunctionTypeMetadata<Runtime>::OffsetToFlags;
335+
TargetFunctionTypeFlags<Runtime> Flags;
336+
if (!Reader.readBytes(FlagsAddress, (uint8_t*)&Flags, sizeof(Flags)))
337+
return nullptr;
333338
TypeRefVector Arguments;
334-
llvm_unreachable("todo");
339+
StoredPointer ArgumentAddress = MetadataAddress +
340+
sizeof(TargetFunctionTypeMetadata<Runtime>);
341+
for (StoredPointer i = 0; i < Function->getNumArguments(); ++i,
342+
ArgumentAddress += sizeof(StoredPointer)) {
343+
StoredPointer FlaggedArgumentAddress;
344+
if (!Reader.readInteger(ArgumentAddress, &FlaggedArgumentAddress))
345+
return nullptr;
346+
// TODO: Use target-agnostic FlaggedPointer to mask this!
347+
FlaggedArgumentAddress &= ~((StoredPointer)1);
348+
if (auto ArgumentTypeRef = getTypeRef(FlaggedArgumentAddress))
349+
Arguments.push_back(ArgumentTypeRef);
350+
else
351+
return nullptr;
352+
}
353+
335354
auto Result = getTypeRef(Function->ResultType);
336355
return FunctionTypeRef::create(Arguments, Result);
337356
}

include/swift/Runtime/Metadata.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,19 +2030,21 @@ using EnumMetadata = TargetEnumMetadata<InProcess>;
20302030
template <typename Runtime>
20312031
struct TargetFunctionTypeMetadata : public TargetMetadata<Runtime> {
20322032
using StoredSize = typename Runtime::StoredSize;
2033+
2034+
// TODO: Make this target agnostic
20332035
using Argument = FlaggedPointer<const TargetMetadata<Runtime> *, 0>;
20342036

2035-
FunctionTypeFlags Flags;
2037+
TargetFunctionTypeFlags<Runtime> Flags;
20362038

20372039
/// The type metadata for the result type.
20382040
ConstTargetMetadataPointer<Runtime, TargetMetadata> ResultType;
20392041

2040-
Argument *getArguments() {
2041-
return reinterpret_cast<Argument *>(this + 1);
2042+
TargetPointer<Runtime, Argument> getArguments() {
2043+
return reinterpret_cast<TargetPointer<Runtime, Argument>>(this + 1);
20422044
}
20432045

2044-
const Argument *getArguments() const {
2045-
return reinterpret_cast<const Argument *>(this + 1);
2046+
TargetPointer<Runtime, const Argument> getArguments() const {
2047+
return reinterpret_cast<TargetPointer<Runtime, const Argument>>(this + 1);
20462048
}
20472049

20482050
StoredSize getNumArguments() const {
@@ -2053,6 +2055,8 @@ struct TargetFunctionTypeMetadata : public TargetMetadata<Runtime> {
20532055
}
20542056
bool throws() const { return Flags.throws(); }
20552057

2058+
static constexpr StoredSize OffsetToFlags = sizeof(TargetMetadata<Runtime>);
2059+
20562060
static bool classof(const TargetMetadata<Runtime> *metadata) {
20572061
return metadata->getKind() == MetadataKind::Function;
20582062
}

0 commit comments

Comments
 (0)