Skip to content

Commit a97842f

Browse files
authored
Merge pull request swiftlang#16693 from dcci/typerecogenerictypealiases
[TypeReconstruction] Add support for generic typealiases.
2 parents aa16c6a + 2a91041 commit a97842f

File tree

5 files changed

+84
-4
lines changed

5 files changed

+84
-4
lines changed

lib/IDE/TypeReconstruction.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,51 @@ static void VisitNodeAssociatedTypeRef(
755755
ident->getText().str().c_str());
756756
}
757757

758+
static void VisitNodeGenericTypealias(ASTContext *ast,
759+
Demangle::NodePointer cur_node,
760+
VisitNodeResult &result) {
761+
VisitNodeResult generic_type_result;
762+
VisitNodeResult template_types_result;
763+
764+
Demangle::Node::iterator end = cur_node->end();
765+
for (Demangle::Node::iterator pos = cur_node->begin(); pos != end; ++pos) {
766+
const Demangle::Node::Kind child_node_kind = (*pos)->getKind();
767+
switch (child_node_kind) {
768+
case Demangle::Node::Kind::Type:
769+
VisitNode(ast, *pos, generic_type_result);
770+
break;
771+
case Demangle::Node::Kind::TypeList:
772+
VisitNode(ast, *pos, template_types_result);
773+
break;
774+
default:
775+
break;
776+
}
777+
}
778+
779+
if (generic_type_result._decls.size() != 1 ||
780+
generic_type_result._types.size() != 1 ||
781+
template_types_result._types.empty())
782+
return;
783+
784+
auto *genericTypeAlias =
785+
cast<TypeAliasDecl>(generic_type_result._decls.front());
786+
GenericSignature *signature = genericTypeAlias->getGenericSignature();
787+
// FIXME: handle conformances.
788+
SubstitutionMap subMap =
789+
SubstitutionMap::get(signature, template_types_result._types,
790+
ArrayRef<ProtocolConformanceRef>({}));
791+
Type parentType;
792+
if (auto nominal = genericTypeAlias->getDeclContext()
793+
->getAsNominalTypeOrNominalTypeExtensionContext()) {
794+
parentType = nominal->getDeclaredInterfaceType();
795+
}
796+
NameAliasType *NAT =
797+
NameAliasType::get(genericTypeAlias, parentType, subMap,
798+
genericTypeAlias->getUnderlyingTypeLoc().getType());
799+
result._types.push_back(NAT);
800+
result._decls.push_back(genericTypeAlias);
801+
}
802+
758803
static void VisitNodeBoundGeneric(
759804
ASTContext *ast,
760805
Demangle::NodePointer cur_node, VisitNodeResult &result) {
@@ -2203,6 +2248,10 @@ static void VisitNode(
22032248
VisitNodeBoundGeneric(ast, node, result);
22042249
break;
22052250

2251+
case Demangle::Node::Kind::BoundGenericTypeAlias:
2252+
VisitNodeGenericTypealias(ast, node, result);
2253+
break;
2254+
22062255
case Demangle::Node::Kind::BuiltinTypeName:
22072256
VisitNodeBuiltinTypeName(ast, node, result);
22082257
break;

test/DebugInfo/DumpDeclFromMangledName.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,19 @@ func patatino() -> Int {
3939
}
4040

4141
patatino()
42+
43+
class Foo<T> {
44+
var x : T
45+
init(_ x : T) {
46+
self.x = x
47+
}
48+
}
49+
50+
typealias Patatino<T> = Foo<T>
51+
52+
func main() -> Int {
53+
var p : Patatino<Int> = Patatino(23);
54+
return 0
55+
}
56+
57+
let _ = main()

test/DebugInfo/DumpTypeFromMangledName.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,27 @@
1212
// RUN: diff %t.check %t.output
1313

1414
// REQUIRES: executable_test
15-
func main() {
16-
struct Patatino {}
17-
}
18-
1915
extension Collection where Element: Equatable {
2016
func split<C: Collection>(separatedBy separator: C) -> [SubSequence]
2117
where C.Element == Element {
2218
var results = [SubSequence]()
2319
return results
2420
}
2521
}
22+
23+
class Foo<T> {
24+
var x : T
25+
init(_ x : T) {
26+
self.x = x
27+
}
28+
}
29+
30+
typealias Patatino<T> = Foo<T>
31+
32+
func main() -> Int {
33+
struct patatino {}
34+
var p : Patatino<Int> = Patatino(23);
35+
return 0
36+
}
37+
38+
let _ = main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
$S12DeclReconstr8patatinoSiyF ---> func patatino() -> Int
22
$S12DeclReconstr1SVACycfC ---> init()
3+
$S12DeclReconstr8PatatinoaySiGD ---> typealias Patatino<T> = Foo<T>

test/DebugInfo/Inputs/type-reconstr-names.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ $Ss5Int32VD ---> Int32
44
$S4blah4mainyyF8PatatinoL_VMa ---> Can't resolve type of $S4blah4mainyyF8PatatinoL_VMa
55
$Ss10CollectionP7Element ---> Can't resolve type of $Ss10CollectionP7Element
66
$Ss15ContiguousArrayV9formIndex5afterySiz_tFSS_Tg5 ---> (inout Int) -> ()
7+
$S12TypeReconstr8PatatinoaySiGD ---> Patatino<Int>

0 commit comments

Comments
 (0)