Skip to content

Commit

Permalink
Get typerefs for tuple type metadata, including their elements
Browse files Browse the repository at this point in the history
  • Loading branch information
bitjammer committed Mar 5, 2016
1 parent 5196f5b commit 04444c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
33 changes: 28 additions & 5 deletions include/swift/Reflection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct ReflectionInfo {
template <typename Runtime>
class ReflectionContext {
using StoredPointer = typename Runtime::StoredPointer;
using StoredSize = typename Runtime::StoredSize;

std::vector<ReflectionInfo> ReflectionInfos;
std::unordered_map<StoredPointer, TypeRefPointer> TypeRefCache;
Expand All @@ -69,8 +70,7 @@ class ReflectionContext {
}

template <typename M>
SharedTargetMetadataRef<Runtime> _readMetadata(StoredPointer Address) {
auto Size = sizeof(M);
SharedTargetMetadataRef<Runtime> _readMetadata(StoredPointer Address, size_t Size = sizeof(M)) {
uint8_t *Buffer = (uint8_t *)malloc(Size);
if (!Reader.readBytes(Address, Buffer, Size)) {
free(Buffer);
Expand All @@ -83,6 +83,7 @@ class ReflectionContext {
free((void*)Meta);
});
}


public:
ReflectionContext(MemoryReader &Reader) : Reader(Reader) {}
Expand Down Expand Up @@ -167,8 +168,17 @@ class ReflectionContext {
return _readMetadata<TargetEnumMetadata<Runtime>>(Address);
case MetadataKind::Struct:
return _readMetadata<TargetStructMetadata<Runtime>>(Address);
case MetadataKind::Tuple:
return _readMetadata<TargetTupleTypeMetadata<Runtime>>(Address);
case MetadataKind::Tuple: {
auto NumElementsAddress = Address +
TargetTupleTypeMetadata<Runtime>::OffsetToNumElements;
StoredSize NumElements;
if (!Reader.readInteger(NumElementsAddress, &NumElements))
return nullptr;
auto TotalSize = sizeof(TargetTupleTypeMetadata<Runtime>) +
NumElements * sizeof(StoredPointer);
return _readMetadata<TargetTupleTypeMetadata<Runtime>>(Address,
TotalSize);
}
default:
return nullptr;
}
Expand Down Expand Up @@ -302,7 +312,20 @@ class ReflectionContext {
case MetadataKind::Tuple: {
auto TupleMeta = cast<TargetTupleTypeMetadata<Runtime>>(Meta.get());
TypeRefVector Elements;
llvm_unreachable("todo");
StoredPointer ElementAddress = MetadataAddress +
sizeof(TargetTupleTypeMetadata<Runtime>);
using Element = typename TargetTupleTypeMetadata<Runtime>::Element;
for (StoredPointer i = 0; i < TupleMeta->NumElements; ++i,
ElementAddress += sizeof(Element)) {
Element E;
if (!Reader.readBytes(ElementAddress, (uint8_t*)&E, sizeof(Element)))
return nullptr;

if (auto ElementTypeRef = getTypeRef(E.Type))
Elements.push_back(ElementTypeRef);
else
return nullptr;
}
return TupleTypeRef::create(Elements);
}
case MetadataKind::Function: {
Expand Down
6 changes: 4 additions & 2 deletions include/swift/Runtime/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -2101,11 +2101,11 @@ struct TargetTupleTypeMetadata : public TargetMetadata<Runtime> {
}
};

Element *getElements() {
TargetPointer<Runtime, Element> getElements() {
return reinterpret_cast<TargetPointer<Runtime, Element>>(this + 1);
}

const Element *getElements() const {
TargetPointer<Runtime, const Element> getElements() const {
return reinterpret_cast<TargetPointer<Runtime, const Element>>(this + 1);
}

Expand All @@ -2117,6 +2117,8 @@ struct TargetTupleTypeMetadata : public TargetMetadata<Runtime> {
return getElements()[i];
}

static constexpr StoredSize OffsetToNumElements = sizeof(TargetMetadata<Runtime>);

static bool classof(const TargetMetadata<Runtime> *metadata) {
return metadata->getKind() == MetadataKind::Tuple;
}
Expand Down

0 comments on commit 04444c4

Please sign in to comment.