Skip to content

Commit 961dc59

Browse files
committed
Thread Labels through TupleTypeRef.
1 parent 4b9cf31 commit 961dc59

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

include/swift/Reflection/TypeRef.h

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,27 +301,48 @@ class BoundGenericTypeRef final : public TypeRef, public NominalTypeTrait {
301301
};
302302

303303
class TupleTypeRef final : public TypeRef {
304+
protected:
304305
std::vector<const TypeRef *> Elements;
306+
std::string Labels;
305307

306-
static TypeRefID Profile(const std::vector<const TypeRef *> &Elements) {
308+
static TypeRefID Profile(const std::vector<const TypeRef *> &Elements,
309+
const std::string &Labels) {
307310
TypeRefID ID;
308311
for (auto Element : Elements)
309312
ID.addPointer(Element);
313+
ID.addString(Labels);
310314
return ID;
311315
}
312316

313317
public:
314-
TupleTypeRef(std::vector<const TypeRef *> Elements)
315-
: TypeRef(TypeRefKind::Tuple), Elements(std::move(Elements)) {}
318+
TupleTypeRef(std::vector<const TypeRef *> Elements, std::string &&Labels)
319+
: TypeRef(TypeRefKind::Tuple), Elements(std::move(Elements)),
320+
Labels(Labels) {}
316321

317322
template <typename Allocator>
318323
static const TupleTypeRef *create(Allocator &A,
319-
std::vector<const TypeRef *> Elements) {
320-
FIND_OR_CREATE_TYPEREF(A, TupleTypeRef, Elements);
321-
}
322-
323-
const std::vector<const TypeRef *> &getElements() const {
324-
return Elements;
324+
std::vector<const TypeRef *> Elements,
325+
std::string &&Labels) {
326+
FIND_OR_CREATE_TYPEREF(A, TupleTypeRef, Elements, Labels);
327+
}
328+
329+
const std::vector<const TypeRef *> &getElements() const { return Elements; };
330+
const std::string &getLabelString() const { return Labels; };
331+
std::vector<llvm::StringRef> getLabels() const {
332+
std::vector<llvm::StringRef> Vec;
333+
std::string::size_type End, Start = 0;
334+
while (true) {
335+
End = Labels.find(' ', Start);
336+
if (End == std::string::npos)
337+
break;
338+
Vec.push_back(llvm::StringRef(Labels.data() + Start, End - Start));
339+
Start = End + 1;
340+
}
341+
// A canonicalized TypeRef has an empty label string.
342+
// Pad the vector with empty labels.
343+
for (unsigned N = Vec.size(); N < Elements.size(); ++N)
344+
Vec.push_back({});
345+
return Vec;
325346
};
326347

327348
static bool classof(const TypeRef *TR) {

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,7 @@ class TypeRefBuilder {
405405

406406
const TupleTypeRef *createTupleType(llvm::ArrayRef<const TypeRef *> elements,
407407
std::string &&labels) {
408-
// FIXME: Add uniqueness checks in TupleTypeRef::Profile and
409-
// unittests/Reflection/TypeRef.cpp if using labels for identity.
410-
return TupleTypeRef::create(*this, elements);
408+
return TupleTypeRef::create(*this, elements, std::move(labels));
411409
}
412410

413411
const FunctionTypeRef *createFunctionType(

stdlib/public/Reflection/TypeLowering.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,7 @@ class LowerType
20212021
const TypeInfo *visitTupleTypeRef(const TupleTypeRef *T) {
20222022
RecordTypeInfoBuilder builder(TC, RecordKind::Tuple);
20232023
for (auto Element : T->getElements())
2024+
// The label is not going to be relevant/harmful for looking up type info.
20242025
builder.addField("", Element, ExternalTypeInfo);
20252026
return builder.build();
20262027
}

stdlib/public/Reflection/TypeRef.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,14 @@ class PrintTypeRef : public TypeRefVisitor<PrintTypeRef, void> {
109109

110110
void visitTupleTypeRef(const TupleTypeRef *T) {
111111
printHeader("tuple");
112-
for (auto element : T->getElements())
113-
printRec(element);
112+
T->getLabels();
113+
auto Labels = T->getLabels();
114+
for (auto NameElement : llvm::zip_first(Labels, T->getElements())) {
115+
auto Label = std::get<0>(NameElement);
116+
if (!Label.empty())
117+
fprintf(file, "%s = ", Label.str().c_str());
118+
printRec(std::get<1>(NameElement));
119+
}
114120
fprintf(file, ")");
115121
}
116122

@@ -459,9 +465,15 @@ class DemanglingForTypeRef
459465
Demangle::NodePointer visitTupleTypeRef(const TupleTypeRef *T) {
460466
auto tuple = Dem.createNode(Node::Kind::Tuple);
461467

462-
for (auto element : T->getElements()) {
468+
auto Labels = T->getLabels();
469+
for (auto LabelElement : llvm::zip(Labels, T->getElements())) {
463470
auto tupleElt = Dem.createNode(Node::Kind::TupleElement);
464-
tupleElt->addChild(visit(element), Dem);
471+
auto Label = std::get<0>(LabelElement);
472+
if (!Label.empty()) {
473+
auto name = Dem.createNode(Node::Kind::TupleElementName, Label);
474+
tupleElt->addChild(name, Dem);
475+
}
476+
tupleElt->addChild(visit(std::get<1>(LabelElement)), Dem);
465477
tuple->addChild(tupleElt, Dem);
466478
}
467479
return tuple;
@@ -816,7 +828,8 @@ class ThickenMetatype
816828
std::vector<const TypeRef *> Elements;
817829
for (auto Element : T->getElements())
818830
Elements.push_back(visit(Element));
819-
return TupleTypeRef::create(Builder, Elements);
831+
std::string Labels = T->getLabelString();
832+
return TupleTypeRef::create(Builder, Elements, std::move(Labels));
820833
}
821834

822835
const TypeRef *visitFunctionTypeRef(const FunctionTypeRef *F) {
@@ -929,7 +942,8 @@ class TypeRefSubstitution
929942
std::vector<const TypeRef *> Elements;
930943
for (auto Element : T->getElements())
931944
Elements.push_back(visit(Element));
932-
return TupleTypeRef::create(Builder, Elements);
945+
std::string Labels = T->getLabelString();
946+
return TupleTypeRef::create(Builder, Elements, std::move(Labels));
933947
}
934948

935949
const TypeRef *visitFunctionTypeRef(const FunctionTypeRef *F) {

0 commit comments

Comments
 (0)