Skip to content
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
2 changes: 2 additions & 0 deletions include/swift/AST/ASTDemangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ class ASTBuilder {

Type createBoundGenericType(GenericTypeDecl *decl, ArrayRef<Type> args);

OpaqueTypeDecl *resolveOpaqueTypeDecl(NodePointer opaqueDescriptor);

Type resolveOpaqueType(NodePointer opaqueDescriptor,
ArrayRef<ArrayRef<Type>> args,
unsigned ordinal);
Expand Down
96 changes: 45 additions & 51 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,24 +332,15 @@ Type ASTBuilder::createTypeAliasType(GenericTypeDecl *decl, Type parent) {

static SubstitutionMap
createSubstitutionMapFromGenericArgs(GenericSignature genericSig,
ArrayRef<Type> args,
LookupConformanceFn lookupConformance) {
ArrayRef<Type> args) {
if (!genericSig)
return SubstitutionMap();

if (genericSig.getGenericParams().size() != args.size())
return SubstitutionMap();

return SubstitutionMap::get(
genericSig,
[&](SubstitutableType *t) -> Type {
auto *gp = cast<GenericTypeParamType>(t);
unsigned ordinal = genericSig->getGenericParamOrdinal(gp);
if (ordinal < args.size())
return args[ordinal];
return Type();
},
lookupConformance);
genericSig, args, LookUpConformanceInModule());
}

Type ASTBuilder::createBoundGenericType(GenericTypeDecl *decl,
Expand All @@ -364,8 +355,7 @@ Type ASTBuilder::createBoundGenericType(GenericTypeDecl *decl,

// Build a SubstitutionMap.
auto genericSig = nominalDecl->getGenericSignature();
auto subs = createSubstitutionMapFromGenericArgs(
genericSig, args, LookUpConformanceInModule());
auto subs = createSubstitutionMapFromGenericArgs(genericSig, args);
if (!subs)
return Type();
auto origType = nominalDecl->getDeclaredInterfaceType();
Expand All @@ -375,48 +365,53 @@ Type ASTBuilder::createBoundGenericType(GenericTypeDecl *decl,
return origType.subst(subs);
}

Type ASTBuilder::resolveOpaqueType(NodePointer opaqueDescriptor,
ArrayRef<ArrayRef<Type>> args,
unsigned ordinal) {
if (opaqueDescriptor->getKind() == Node::Kind::OpaqueReturnTypeOf) {
auto definingDecl = opaqueDescriptor->getChild(0);
auto definingGlobal = Factory.createNode(Node::Kind::Global);
definingGlobal->addChild(definingDecl, Factory);
auto mangling = mangleNode(definingGlobal, ManglingFlavor);
if (!mangling.isSuccess())
return Type();
auto mangledName = mangling.result();
OpaqueTypeDecl *ASTBuilder::resolveOpaqueTypeDecl(NodePointer opaqueDescriptor) {
if (opaqueDescriptor->getKind() != Node::Kind::OpaqueReturnTypeOf)
return nullptr;

auto moduleNode = findModuleNode(definingDecl);
if (!moduleNode)
return Type();
auto definingDecl = opaqueDescriptor->getChild(0);
auto definingGlobal = Factory.createNode(Node::Kind::Global);
definingGlobal->addChild(definingDecl, Factory);
auto mangling = mangleNode(definingGlobal, ManglingFlavor);
if (!mangling.isSuccess())
return nullptr;
auto mangledName = mangling.result();

ModuleDecl *scratch;
auto potentialParentModules = findPotentialModules(moduleNode, scratch);
if (potentialParentModules.empty())
return Type();
auto moduleNode = findModuleNode(definingDecl);
if (!moduleNode)
return nullptr;

OpaqueTypeDecl *opaqueDecl = nullptr;
for (auto module : potentialParentModules)
if (auto decl = module->lookupOpaqueResultType(mangledName))
opaqueDecl = decl;
ModuleDecl *scratch;
auto potentialParentModules = findPotentialModules(moduleNode, scratch);
if (potentialParentModules.empty())
return nullptr;

if (!opaqueDecl)
return Type();
SmallVector<Type, 8> allArgs;
for (auto argSet : args) {
allArgs.append(argSet.begin(), argSet.end());
}
for (auto module : potentialParentModules)
if (auto decl = module->lookupOpaqueResultType(mangledName))
return decl;

return nullptr;
}

Type ASTBuilder::resolveOpaqueType(NodePointer opaqueDescriptor,
ArrayRef<ArrayRef<Type>> args,
unsigned ordinal) {
OpaqueTypeDecl *opaqueDecl = resolveOpaqueTypeDecl(opaqueDescriptor);
if (!opaqueDecl)
return Type();

SubstitutionMap subs = createSubstitutionMapFromGenericArgs(
opaqueDecl->getGenericSignature(), allArgs,
LookUpConformanceInModule());
Type interfaceType = opaqueDecl->getOpaqueGenericParams()[ordinal];
return OpaqueTypeArchetypeType::get(opaqueDecl, interfaceType, subs);
SmallVector<Type, 8> allArgs;
for (auto argSet : args) {
allArgs.append(argSet.begin(), argSet.end());
}

// TODO: named opaque types
return Type();

if (ordinal >= opaqueDecl->getOpaqueGenericParams().size())
return Type();

SubstitutionMap subs = createSubstitutionMapFromGenericArgs(
opaqueDecl->getGenericSignature(), allArgs);
Type interfaceType = opaqueDecl->getOpaqueGenericParams()[ordinal];
return OpaqueTypeArchetypeType::get(opaqueDecl, interfaceType, subs);
}

Type ASTBuilder::createBoundGenericType(GenericTypeDecl *decl,
Expand Down Expand Up @@ -1143,8 +1138,7 @@ Type ASTBuilder::createSILBoxTypeWithLayout(
SubstitutionMap substs;
if (signature)
substs = createSubstitutionMapFromGenericArgs(
signature, replacements,
LookUpConformanceInModule());
signature, replacements);
return SILBoxType::get(Ctx, layout, substs);
}

Expand Down
33 changes: 30 additions & 3 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7515,10 +7515,37 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
Printer << ") __";

if (genericSig) {
printGenericArgs(decl->getASTContext(),
genericSig.getGenericParams(),
T->getSubstitutions().getReplacementTypes());
auto &ctx = decl->getASTContext();
auto params = genericSig.getGenericParams();
auto args = T->getSubstitutions().getReplacementTypes();

// Use the new "nested" syntax if there is at least one parameter pack,
// because that case didn't round trip at all before anyway. Otherwise,
// use the old "flat" syntax, even when the owner declaration is in a
// nested generic context, because we want the generated swiftinterface
// to continue to work on old compilers.
if (genericSig->hasParameterPack()) {
bool first = true;

while (!params.empty()) {
if (!first) { Printer << ".__"; }
first = false;

unsigned end = 1;
unsigned depth = params.front()->getDepth();
while (end < params.size() && params[end]->getDepth() == depth) {
++end;
}

printGenericArgs(ctx, params.take_front(end), args.take_front(end));
params = params.slice(end);
args = args.slice(end);
}
} else {
printGenericArgs(ctx, params, args);
}
}

return;
}
case PrintOptions::OpaqueReturnTypePrintingMode::Description: {
Expand Down
3 changes: 1 addition & 2 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,7 @@ namespace {
void addGenericParameters() {
GenericSignature sig = asImpl().getGenericSignature();
auto metadata =
irgen::addGenericParameters(IGM, B,
asImpl().getGenericSignature(),
irgen::addGenericParameters(IGM, B, sig,
/*implicit=*/false);
assert(metadata.NumParams == metadata.NumParamsEmitted &&
"We can't use implicit GenericParamDescriptors here");
Expand Down
Loading