Skip to content

swift-reflection-dump support for unresolved opaque return types. #31026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion include/swift/Reflection/TypeRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ class TupleTypeRef final : public TypeRef {

public:
TupleTypeRef(std::vector<const TypeRef *> Elements, bool Variadic=false)
: TypeRef(TypeRefKind::Tuple), Elements(Elements), Variadic(Variadic) {}
: TypeRef(TypeRefKind::Tuple), Elements(std::move(Elements)),
Variadic(Variadic) {}

template <typename Allocator>
static const TupleTypeRef *create(Allocator &A,
Expand All @@ -338,6 +339,84 @@ class TupleTypeRef final : public TypeRef {
}
};

class OpaqueArchetypeTypeRef final : public TypeRef {
std::string ID;
std::string Description;
unsigned Ordinal;
// Each ArrayRef in ArgumentLists references into the buffer owned by this
// vector, which must not be modified after construction.
std::vector<const TypeRef *> AllArgumentsBuf;
std::vector<ArrayRef<const TypeRef *>> ArgumentLists;

static TypeRefID Profile(StringRef idString,
StringRef description, unsigned ordinal,
ArrayRef<ArrayRef<const TypeRef *>> argumentLists) {
TypeRefID ID;
ID.addString(idString);
ID.addInteger(ordinal);
for (auto argList : argumentLists) {
ID.addInteger(0u);
for (auto arg : argList)
ID.addPointer(arg);
}

return ID;
}

public:
OpaqueArchetypeTypeRef(StringRef id,
StringRef description, unsigned ordinal,
ArrayRef<ArrayRef<const TypeRef *>> argumentLists)
: TypeRef(TypeRefKind::OpaqueArchetype),
ID(id), Description(description), Ordinal(ordinal)
{
std::vector<unsigned> argumentListLengths;

for (auto argList : argumentLists) {
argumentListLengths.push_back(argList.size());
AllArgumentsBuf.insert(AllArgumentsBuf.end(),
argList.begin(), argList.end());
}
auto *data = AllArgumentsBuf.data();
for (auto length : argumentListLengths) {
ArgumentLists.push_back(ArrayRef<const TypeRef *>(data, length));
data += length;
}
assert(data == AllArgumentsBuf.data() + AllArgumentsBuf.size());
}

template <typename Allocator>
static const OpaqueArchetypeTypeRef *create(Allocator &A,
StringRef id, StringRef description,
unsigned ordinal,
ArrayRef<ArrayRef<const TypeRef *>> arguments) {
FIND_OR_CREATE_TYPEREF(A, OpaqueArchetypeTypeRef,
id, description, ordinal, arguments);
}

ArrayRef<ArrayRef<const TypeRef *>> getArgumentLists() const {
return ArgumentLists;
}

unsigned getOrdinal() const {
return Ordinal;
}

/// A stable identifier for the opaque type.
StringRef getID() const {
return ID;
}

/// A human-digestible, but not necessarily stable, description of the opaque type.
StringRef getDescription() const {
return Description;
}

static bool classof(const TypeRef *T) {
return T->getKind() == TypeRefKind::OpaqueArchetype;
}
};

class FunctionTypeRef final : public TypeRef {
using Param = remote::FunctionParam<const TypeRef *>;

Expand Down
11 changes: 10 additions & 1 deletion include/swift/Reflection/TypeRefBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ class TypeRefBuilder {
unsigned ordinal) {
// TODO: Produce a type ref for the opaque type if the underlying type isn't
// available.
opaqueDescriptor->dump();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jckarter Is this intentional? Looks like it causes linker failures in some configurations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, no, it can be removed. I'll take care of it.


// Try to resolve to the underlying type, if we can.
if (opaqueDescriptor->getKind() ==
Expand All @@ -392,7 +393,15 @@ class TypeRefBuilder {

return underlyingTy->subst(*this, subs);
}
return nullptr;

// Otherwise, build a type ref that represents the opaque type.
return OpaqueArchetypeTypeRef::create(*this,
mangleNode(opaqueDescriptor,
SymbolicResolver(),
Dem),
nodeToString(opaqueDescriptor),
ordinal,
genericArgs);
}

const TupleTypeRef *
Expand Down
1 change: 1 addition & 0 deletions include/swift/Reflection/TypeRefs.def
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ TYPEREF(ForeignClass, TypeRef)
TYPEREF(ObjCClass, TypeRef)
TYPEREF(ObjCProtocol, TypeRef)
TYPEREF(Opaque, TypeRef)
TYPEREF(OpaqueArchetype, TypeRef)
#define REF_STORAGE(Name, ...) \
TYPEREF(Name##Storage, TypeRef)
#include "swift/AST/ReferenceStorage.def"
Expand Down
9 changes: 7 additions & 2 deletions include/swift/Remote/MetadataReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -1077,11 +1077,16 @@ class MetadataReader {
demangledSymbol = demangledSymbol->getChild(0);
assert(demangledSymbol->getKind() == Demangle::Node::Kind::Type);
break;
// We don't handle pointers to other symbols yet.
// TODO: Opaque type descriptors could be useful.
// Pointers to opaque type descriptors demangle to the name of the opaque
// type declaration.
case Demangle::Node::Kind::OpaqueTypeDescriptor:
demangledSymbol = demangledSymbol->getChild(0);
break;
// We don't handle pointers to other symbols yet.
default:
return nullptr;
}

return demangledSymbol;
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,10 @@ NodePointer Demangler::demangleSymbolicReference(unsigned char rawKind,
return nullptr;

// Types register as substitutions even when symbolically referenced.
// OOPS: Except for opaque type references!
if (kind == SymbolicReferenceKind::Context &&
resolved->getKind() != Node::Kind::OpaqueTypeDescriptorSymbolicReference)
resolved->getKind() != Node::Kind::OpaqueTypeDescriptorSymbolicReference &&
resolved->getKind() != Node::Kind::OpaqueReturnTypeOf)
addSubstitution(resolved);
return resolved;
}
Expand Down
15 changes: 15 additions & 0 deletions stdlib/public/Reflection/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,10 @@ class HasFixedSize
bool visitOpaqueTypeRef(const OpaqueTypeRef *O) {
return false;
}

bool visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
return false;
}
};

bool TypeConverter::hasFixedSize(const TypeRef *TR) {
Expand Down Expand Up @@ -1705,6 +1709,10 @@ class HasSingletonMetatype
MetatypeRepresentation visitOpaqueTypeRef(const OpaqueTypeRef *O) {
return MetatypeRepresentation::Unknown;
}

MetatypeRepresentation visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
return MetatypeRepresentation::Unknown;
}
};

class EnumTypeInfoBuilder {
Expand Down Expand Up @@ -2153,6 +2161,13 @@ class LowerType
DEBUG_LOG(fprintf(stderr, "Can't lower opaque TypeRef"));
return nullptr;
}

const TypeInfo *visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
// TODO: Provide a hook for the client to try to resolve the opaque archetype
// with additional information?
DEBUG_LOG(fprintf(stderr, "Can't lower unresolved opaque archetype TypeRef"));
return nullptr;
}
};

const TypeInfo *TypeConverter::getTypeInfo(const TypeRef *TR) {
Expand Down
70 changes: 70 additions & 0 deletions stdlib/public/Reflection/TypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@ class PrintTypeRef : public TypeRefVisitor<PrintTypeRef, void> {
fprintf(file, ")");
}

void visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
printHeader("opaque_archetype");
printField("id", O->getID());
printField("description", O->getDescription());
fprintf(file, " ordinal %u ", O->getOrdinal());
for (auto argList : O->getArgumentLists()) {
fprintf(file, "\n");
fprintf(indent(Indent + 2), "args: <");
for (auto arg : argList) {
printRec(arg);
}
fprintf(file, ">");
}
fprintf(file, ")");
}

void visitOpaqueTypeRef(const OpaqueTypeRef *O) {
printHeader("opaque");
fprintf(file, ")");
Expand Down Expand Up @@ -348,6 +364,10 @@ struct TypeRefIsConcrete
bool visitOpaqueTypeRef(const OpaqueTypeRef *O) {
return true;
}

bool visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
return false;
}

#define REF_STORAGE(Name, name, ...) \
bool visit##Name##StorageTypeRef(const Name##StorageTypeRef *US) { \
Expand Down Expand Up @@ -660,6 +680,36 @@ class DemanglingForTypeRef
Demangle::NodePointer visitOpaqueTypeRef(const OpaqueTypeRef *O) {
return Dem.createNode(Node::Kind::OpaqueType);
}

Demangle::NodePointer visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
auto decl = Dem.demangleSymbol(O->getID());
if (!decl)
return nullptr;

auto index = Dem.createNode(Node::Kind::Index, O->getOrdinal());

auto argNodeLists = Dem.createNode(Node::Kind::TypeList);
for (auto argList : O->getArgumentLists()) {
auto argNodeList = Dem.createNode(Node::Kind::TypeList);

for (auto arg : argList) {
auto argNode = visit(arg);
if (!argNode)
return nullptr;

argNodeList->addChild(argNode, Dem);
}

argNodeLists->addChild(argNodeList, Dem);
}

auto node = Dem.createNode(Node::Kind::OpaqueType);
node->addChild(decl, Dem);
node->addChild(index, Dem);
node->addChild(argNodeLists, Dem);

return node;
}
};

Demangle::NodePointer TypeRef::getDemangling(Demangle::Demangler &Dem) const {
Expand Down Expand Up @@ -831,6 +881,11 @@ class ThickenMetatype
const TypeRef *visitOpaqueTypeRef(const OpaqueTypeRef *O) {
return O;
}

const TypeRef *visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
return O;
}

};

static const TypeRef *
Expand Down Expand Up @@ -1009,6 +1064,21 @@ class TypeRefSubstitution
const TypeRef *visitOpaqueTypeRef(const OpaqueTypeRef *O) {
return O;
}

const TypeRef *visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
std::vector<const TypeRef *> newArgsBuffer;
for (auto argList : O->getArgumentLists()) {
for (auto arg : argList) {
newArgsBuffer.push_back(visit(arg));
}
}

std::vector<ArrayRef<const TypeRef *>> newArgLists;

return OpaqueArchetypeTypeRef::create(Builder, O->getID(), O->getDescription(),
O->getOrdinal(),
newArgLists);
}
};

const TypeRef *
Expand Down