forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeRef.cpp
1625 lines (1370 loc) · 49.6 KB
/
TypeRef.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- TypeRef.cpp - Swift Type References for Reflection ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Implements the structures of type references for property and enum
// case reflection.
//
//===----------------------------------------------------------------------===//
#if SWIFT_ENABLE_REFLECTION
#include "swift/Basic/Range.h"
#include "swift/Demangling/Demangle.h"
#include "swift/RemoteInspection/TypeRef.h"
#include "swift/RemoteInspection/TypeRefBuilder.h"
#include <iostream>
using namespace swift;
using namespace reflection;
class PrintTypeRef : public TypeRefVisitor<PrintTypeRef, void> {
std::ostream &stream;
unsigned Indent;
std::ostream &indent(unsigned Amount) {
for (unsigned i = 0; i < Amount; ++i)
stream << " ";
return stream;
}
std::ostream &printHeader(std::string Name) {
indent(Indent) << "(" << Name;
return stream;
}
std::ostream &printField(std::string name, std::string value) {
if (!name.empty())
stream << " " << name << "=" << value;
else
stream << " " << value;
return stream;
}
void printRec(const TypeRef *typeRef) {
stream << "\n";
Indent += 2;
visit(typeRef);
Indent -= 2;
}
public:
PrintTypeRef(std::ostream &stream, unsigned Indent)
: stream(stream), Indent(Indent) {}
void visitBuiltinTypeRef(const BuiltinTypeRef *B) {
printHeader("builtin");
auto demangled = Demangle::demangleTypeAsString(B->getMangledName());
printField("", demangled);
stream << ")";
}
void visitNominalTypeRef(const NominalTypeRef *N) {
StringRef mangledName = N->getMangledName();
if (N->isStruct())
printHeader("struct");
else if (N->isEnum())
printHeader("enum");
else if (N->isClass())
printHeader("class");
else if (N->isProtocol()) {
printHeader("protocol");
mangledName = Demangle::dropSwiftManglingPrefix(mangledName);
} else if (N->isAlias())
printHeader("alias");
else
printHeader("nominal");
auto demangled = Demangle::demangleTypeAsString(mangledName);
printField("", demangled);
if (auto parent = N->getParent())
printRec(parent);
stream << ")";
}
void visitBoundGenericTypeRef(const BoundGenericTypeRef *BG) {
if (BG->isStruct())
printHeader("bound_generic_struct");
else if (BG->isEnum())
printHeader("bound_generic_enum");
else if (BG->isClass())
printHeader("bound_generic_class");
else
printHeader("bound_generic");
auto demangled = Demangle::demangleTypeAsString(BG->getMangledName());
printField("", demangled);
for (auto param : BG->getGenericParams())
printRec(param);
if (auto parent = BG->getParent())
printRec(parent);
stream << ")";
}
void visitTupleTypeRef(const TupleTypeRef *T) {
printHeader("tuple");
auto Labels = T->getLabels();
for (auto NameElement : llvm::zip_first(Labels, T->getElements())) {
auto Label = std::get<0>(NameElement);
if (!Label.empty())
stream << Label << " = ";
printRec(std::get<1>(NameElement));
}
stream << ")";
}
void visitFunctionTypeRef(const FunctionTypeRef *F) {
printHeader("function");
switch (F->getFlags().getConvention()) {
case FunctionMetadataConvention::Swift:
break;
case FunctionMetadataConvention::Block:
printField("convention", "block");
break;
case FunctionMetadataConvention::Thin:
printField("convention", "thin");
break;
case FunctionMetadataConvention::CFunctionPointer:
printField("convention", "c");
break;
}
switch (F->getDifferentiabilityKind().Value) {
case FunctionMetadataDifferentiabilityKind::NonDifferentiable:
break;
case FunctionMetadataDifferentiabilityKind::Forward:
printField("differentiable", "forward");
break;
case FunctionMetadataDifferentiabilityKind::Reverse:
printField("differentiable", "reverse");
break;
case FunctionMetadataDifferentiabilityKind::Normal:
printField("differentiable", "normal");
break;
case FunctionMetadataDifferentiabilityKind::Linear:
printField("differentiable", "linear");
break;
}
if (auto globalActor = F->getGlobalActor()) {
stream << "\n";
Indent += 2;
printHeader("global-actor");
{
Indent += 2;
printRec(globalActor);
stream << ")";
Indent -= 2;
}
Indent += 2;
}
stream << "\n";
Indent += 2;
printHeader("parameters");
auto ¶meters = F->getParameters();
for (const auto ¶m : parameters) {
auto flags = param.getFlags();
if (!flags.isNone()) {
Indent += 2;
stream << "\n";
}
switch (flags.getValueOwnership()) {
case ValueOwnership::Default:
/* nothing */
break;
case ValueOwnership::InOut:
printHeader("inout");
break;
case ValueOwnership::Shared:
printHeader("shared");
break;
case ValueOwnership::Owned:
printHeader("owned");
break;
}
if (flags.isIsolated())
printHeader("isolated");
if (flags.isVariadic())
printHeader("variadic");
printRec(param.getType());
if (!flags.isNone()) {
Indent -= 2;
stream << ")";
}
}
if (parameters.empty())
stream << ")";
stream << "\n";
printHeader("result");
printRec(F->getResult());
stream << ")";
Indent -= 2;
}
void visitProtocolCompositionTypeRef(const ProtocolCompositionTypeRef *PC) {
printHeader("protocol_composition");
if (PC->hasExplicitAnyObject())
stream << " any_object";
if (auto superclass = PC->getSuperclass())
printRec(superclass);
for (auto protocol : PC->getProtocols())
printRec(protocol);
stream << ")";
}
void
visitConstrainedExistentialTypeRef(const ConstrainedExistentialTypeRef *CET) {
printHeader("constrained_existential_type");
printRec(CET->getBase());
for (auto req : CET->getRequirements())
visitTypeRefRequirement(req);
stream << ")";
}
void visitMetatypeTypeRef(const MetatypeTypeRef *M) {
printHeader("metatype");
if (M->wasAbstract())
printField("", "was_abstract");
printRec(M->getInstanceType());
stream << ")";
}
void visitExistentialMetatypeTypeRef(const ExistentialMetatypeTypeRef *EM) {
printHeader("existential_metatype");
printRec(EM->getInstanceType());
stream << ")";
}
void
visitGenericTypeParameterTypeRef(const GenericTypeParameterTypeRef *GTP) {
printHeader("generic_type_parameter");
printField("depth", std::to_string(GTP->getDepth()));
printField("index", std::to_string(GTP->getIndex()));
stream << ")";
}
void visitDependentMemberTypeRef(const DependentMemberTypeRef *DM) {
printHeader("dependent_member");
printField("protocol", DM->getProtocol());
printRec(DM->getBase());
printField("member", DM->getMember());
stream << ")";
}
void visitForeignClassTypeRef(const ForeignClassTypeRef *F) {
printHeader("foreign");
if (!F->getName().empty())
printField("name", F->getName());
stream << ")";
}
void visitObjCClassTypeRef(const ObjCClassTypeRef *OC) {
printHeader("objective_c_class");
if (!OC->getName().empty())
printField("name", OC->getName());
stream << ")";
}
void visitObjCProtocolTypeRef(const ObjCProtocolTypeRef *OC) {
printHeader("objective_c_protocol");
if (!OC->getName().empty())
printField("name", OC->getName());
stream << ")";
}
#define REF_STORAGE(Name, name, ...) \
void visit##Name##StorageTypeRef(const Name##StorageTypeRef *US) { \
printHeader(#name "_storage"); \
printRec(US->getType()); \
stream << ")"; \
}
#include "swift/AST/ReferenceStorage.def"
void visitSILBoxTypeRef(const SILBoxTypeRef *SB) {
printHeader("sil_box");
printRec(SB->getBoxedType());
stream << ")";
}
void visitTypeRefRequirement(const TypeRefRequirement &req) {
printHeader("requirement ");
switch (req.getKind()) {
case RequirementKind::SameShape:
printRec(req.getFirstType());
stream << ".shape == ";
printRec(req.getSecondType());
stream << ".shape";
break;
case RequirementKind::Conformance:
case RequirementKind::Superclass:
printRec(req.getFirstType());
stream << " : ";
printRec(req.getSecondType());
break;
case RequirementKind::SameType:
printRec(req.getFirstType());
stream << " == ";
printRec(req.getSecondType());
break;
case RequirementKind::Layout:
stream << "layout requirement";
break;
}
stream << ")";
}
void visitSILBoxTypeWithLayoutTypeRef(const SILBoxTypeWithLayoutTypeRef *SB) {
printHeader("sil_box_with_layout\n");
Indent += 2;
printHeader("layout\n");
Indent += 2;
for (auto &f : SB->getFields()) {
printHeader(f.isMutable() ? "var" : "let");
printRec(f.getType());
stream << ")";
}
Indent -= 2;
stream << ")\n";
printHeader("generic_signature\n");
Indent += 2;
for (auto &subst : SB->getSubstitutions()) {
printHeader("substitution");
printRec(subst.first);
printRec(subst.second);
stream << ")";
}
Indent -= 2;
for (auto &req : SB->getRequirements()) {
visitTypeRefRequirement(req);
}
stream << ")";
stream << ")";
}
void visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
printHeader("opaque_archetype");
printField("id", O->getID().str());
printField("description", O->getDescription().str());
stream << " ordinal " << O->getOrdinal() << " ";
for (auto argList : O->getArgumentLists()) {
stream << "\n";
indent(Indent + 2) << "args: <";
for (auto arg : argList) {
printRec(arg);
}
stream << ">";
}
stream << ")";
}
void visitOpaqueTypeRef(const OpaqueTypeRef *O) {
printHeader("opaque");
stream << ")";
}
};
struct TypeRefIsConcrete
: public TypeRefVisitor<TypeRefIsConcrete, bool> {
const GenericArgumentMap &Subs;
TypeRefIsConcrete(const GenericArgumentMap &Subs) : Subs(Subs) {}
bool visitBuiltinTypeRef(const BuiltinTypeRef *B) {
return true;
}
bool visitNominalTypeRef(const NominalTypeRef *N) {
if (N->getParent())
return visit(N->getParent());
return true;
}
bool visitBoundGenericTypeRef(const BoundGenericTypeRef *BG) {
if (BG->getParent())
if (!visit(BG->getParent()))
return false;
for (auto Param : BG->getGenericParams())
if (!visit(Param))
return false;
return true;
}
bool visitTupleTypeRef(const TupleTypeRef *T) {
for (auto Element : T->getElements()) {
if (!visit(Element))
return false;
}
return true;
}
bool visitFunctionTypeRef(const FunctionTypeRef *F) {
for (const auto &Param : F->getParameters())
if (!visit(Param.getType()))
return false;
return visit(F->getResult());
}
bool
visitProtocolCompositionTypeRef(const ProtocolCompositionTypeRef *PC) {
for (auto Protocol : PC->getProtocols())
if (!visit(Protocol))
return false;
if (auto Superclass = PC->getSuperclass())
if (!visit(Superclass))
return false;
return true;
}
bool
visitConstrainedExistentialTypeRef(const ConstrainedExistentialTypeRef *CET) {
return visit(CET->getBase());
}
bool visitMetatypeTypeRef(const MetatypeTypeRef *M) {
return visit(M->getInstanceType());
}
bool
visitExistentialMetatypeTypeRef(const ExistentialMetatypeTypeRef *EM) {
return visit(EM->getInstanceType());
}
bool
visitGenericTypeParameterTypeRef(const GenericTypeParameterTypeRef *GTP) {
return Subs.find({GTP->getDepth(), GTP->getIndex()}) != Subs.end();
}
bool
visitDependentMemberTypeRef(const DependentMemberTypeRef *DM) {
return visit(DM->getBase());
}
bool visitForeignClassTypeRef(const ForeignClassTypeRef *F) {
return true;
}
bool visitObjCClassTypeRef(const ObjCClassTypeRef *OC) {
return true;
}
bool visitObjCProtocolTypeRef(const ObjCProtocolTypeRef *OC) {
return true;
}
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) { \
return visit(US->getType()); \
}
#include "swift/AST/ReferenceStorage.def"
bool visitSILBoxTypeRef(const SILBoxTypeRef *SB) {
return visit(SB->getBoxedType());
}
bool visitSILBoxTypeWithLayoutTypeRef(const SILBoxTypeWithLayoutTypeRef *SB) {
return true;
}
};
const OpaqueTypeRef *
OpaqueTypeRef::Singleton = new OpaqueTypeRef();
const OpaqueTypeRef *OpaqueTypeRef::get() {
return Singleton;
}
void TypeRef::dump() const { dump(std::cerr); }
void TypeRef::dump(std::ostream &stream, unsigned Indent) const {
PrintTypeRef(stream, Indent).visit(this);
stream << "\n";
}
class DemanglingForTypeRef
: public TypeRefVisitor<DemanglingForTypeRef, Demangle::NodePointer> {
Demangle::Demangler &Dem;
/// Demangle a type and dive into the outermost Type node.
Demangle::NodePointer demangleAndUnwrapType(llvm::StringRef mangledName) {
auto node = Dem.demangleType(mangledName);
if (node && node->getKind() == Node::Kind::Type && node->getNumChildren())
node = node->getFirstChild();
return node;
}
public:
DemanglingForTypeRef(Demangle::Demangler &Dem) : Dem(Dem) {}
Demangle::NodePointer visit(const TypeRef *typeRef) {
auto node = TypeRefVisitor<DemanglingForTypeRef,
Demangle::NodePointer>::visit(typeRef);
// Wrap all nodes in a Type node, as consumers generally expect.
auto typeNode = Dem.createNode(Node::Kind::Type);
typeNode->addChild(node, Dem);
return typeNode;
}
Demangle::NodePointer visitBuiltinTypeRef(const BuiltinTypeRef *B) {
return demangleAndUnwrapType(B->getMangledName());
}
Demangle::NodePointer visitNominalTypeRef(const NominalTypeRef *N) {
auto node = demangleAndUnwrapType(N->getMangledName());
if (!node || node->getNumChildren() != 2)
return node;
auto parent = N->getParent();
if (!parent)
return node;
// Swap in the richer parent that is stored in the NominalTypeRef
// instead of what is encoded in the mangled name. The mangled name's
// context has been "unspecialized" by NodeBuilder.
auto parentNode = visit(parent);
if (!parentNode)
return node;
if (parentNode->getKind() == Node::Kind::Type &&
parentNode->getNumChildren())
parentNode = parentNode->getFirstChild();
auto contextualizedNode = Dem.createNode(node->getKind());
contextualizedNode->addChild(parentNode, Dem);
contextualizedNode->addChild(node->getChild(1), Dem);
return contextualizedNode;
}
Demangle::NodePointer
visitBoundGenericTypeRef(const BoundGenericTypeRef *BG) {
Node::Kind genericNodeKind;
if (BG->isStruct()) {
genericNodeKind = Node::Kind::BoundGenericStructure;
} else if (BG->isEnum()) {
genericNodeKind = Node::Kind::BoundGenericEnum;
} else if (BG->isClass()) {
genericNodeKind = Node::Kind::BoundGenericClass;
} else {
genericNodeKind = Node::Kind::BoundGenericOtherNominalType;
}
auto unspecializedType = Dem.demangleType(BG->getMangledName());
auto genericArgsList = Dem.createNode(Node::Kind::TypeList);
for (auto param : BG->getGenericParams())
genericArgsList->addChild(visit(param), Dem);
auto genericNode = Dem.createNode(genericNodeKind);
genericNode->addChild(unspecializedType, Dem);
genericNode->addChild(genericArgsList, Dem);
auto parent = BG->getParent();
if (!parent)
return genericNode;
auto parentNode = visit(parent);
if (!parentNode || !parentNode->hasChildren() ||
parentNode->getKind() != Node::Kind::Type ||
!unspecializedType->hasChildren())
return genericNode;
// Peel off the "Type" node.
parentNode = parentNode->getFirstChild();
auto nominalNode = unspecializedType->getFirstChild();
if (nominalNode->getNumChildren() != 2)
return genericNode;
// Save identifier for reinsertion later, we have to remove it
// so we can insert the parent node as the first child.
auto identifierNode = nominalNode->getLastChild();
// Remove all children.
nominalNode->removeChildAt(1);
nominalNode->removeChildAt(0);
// Add the parent we just visited back in, followed by the identifier.
nominalNode->addChild(parentNode, Dem);
nominalNode->addChild(identifierNode, Dem);
return genericNode;
}
Demangle::NodePointer visitTupleTypeRef(const TupleTypeRef *T) {
auto tuple = Dem.createNode(Node::Kind::Tuple);
auto Labels = T->getLabels();
for (auto LabelElement : llvm::zip(Labels, T->getElements())) {
auto tupleElt = Dem.createNode(Node::Kind::TupleElement);
auto Label = std::get<0>(LabelElement);
if (!Label.empty()) {
auto name = Dem.createNode(Node::Kind::TupleElementName, Label);
tupleElt->addChild(name, Dem);
}
tupleElt->addChild(visit(std::get<1>(LabelElement)), Dem);
tuple->addChild(tupleElt, Dem);
}
return tuple;
}
Demangle::NodePointer visitFunctionTypeRef(const FunctionTypeRef *F) {
Node::Kind kind;
switch (F->getFlags().getConvention()) {
case FunctionMetadataConvention::Swift:
kind = !F->getFlags().isEscaping() ? Node::Kind::NoEscapeFunctionType
: Node::Kind::FunctionType;
break;
case FunctionMetadataConvention::Block:
kind = Node::Kind::ObjCBlock;
break;
case FunctionMetadataConvention::Thin:
kind = Node::Kind::ThinFunctionType;
break;
case FunctionMetadataConvention::CFunctionPointer:
kind = Node::Kind::CFunctionPointer;
break;
}
llvm::SmallVector<std::pair<NodePointer, bool>, 8> inputs;
for (const auto ¶m : F->getParameters()) {
auto flags = param.getFlags();
auto input = visit(param.getType());
auto wrapInput = [&](Node::Kind kind) {
auto parent = Dem.createNode(kind);
parent->addChild(input, Dem);
input = parent;
};
if (flags.isNoDerivative()) {
wrapInput(Node::Kind::NoDerivative);
}
switch (flags.getValueOwnership()) {
case ValueOwnership::Default:
/* nothing */
break;
case ValueOwnership::InOut:
wrapInput(Node::Kind::InOut);
break;
case ValueOwnership::Shared:
wrapInput(Node::Kind::Shared);
break;
case ValueOwnership::Owned:
wrapInput(Node::Kind::Owned);
break;
}
if (flags.isIsolated()) {
wrapInput(Node::Kind::Isolated);
}
inputs.push_back({input, flags.isVariadic()});
}
NodePointer totalInput = nullptr;
// FIXME: this is copy&paste from Demangle.cpp
switch (inputs.size()) {
case 1: {
auto singleParam = inputs.front();
// If the sole unlabeled parameter has a non-tuple type, encode
// the parameter list as a single type.
if (!singleParam.second) {
auto singleType = singleParam.first;
if (singleType->getKind() == Node::Kind::Type)
singleType = singleType->getFirstChild();
if (singleType->getKind() != Node::Kind::Tuple) {
totalInput = singleParam.first;
break;
}
}
// Otherwise it requires a tuple wrapper.
SWIFT_FALLTHROUGH;
}
// This covers both none and multiple parameters.
default:
auto tuple = Dem.createNode(Node::Kind::Tuple);
for (auto &input : inputs) {
NodePointer eltType;
bool isVariadic;
std::tie(eltType, isVariadic) = input;
// Tuple element := variadic-marker label? type
auto tupleElt = Dem.createNode(Node::Kind::TupleElement);
if (isVariadic)
tupleElt->addChild(Dem.createNode(Node::Kind::VariadicMarker), Dem);
if (eltType->getKind() == Node::Kind::Type) {
tupleElt->addChild(eltType, Dem);
} else {
auto type = Dem.createNode(Node::Kind::Type);
type->addChild(eltType, Dem);
tupleElt->addChild(type, Dem);
}
tuple->addChild(tupleElt, Dem);
}
totalInput = tuple;
break;
}
NodePointer parameters = Dem.createNode(Node::Kind::ArgumentTuple);
NodePointer paramType = Dem.createNode(Node::Kind::Type);
paramType->addChild(totalInput, Dem);
parameters->addChild(paramType, Dem);
NodePointer resultTy = visit(F->getResult());
NodePointer result = Dem.createNode(Node::Kind::ReturnType);
result->addChild(resultTy, Dem);
auto funcNode = Dem.createNode(kind);
if (auto globalActor = F->getGlobalActor()) {
auto node = Dem.createNode(Node::Kind::GlobalActorFunctionType);
auto globalActorNode = visit(globalActor);
node->addChild(globalActorNode, Dem);
funcNode->addChild(node, Dem);
}
if (F->getFlags().isDifferentiable()) {
MangledDifferentiabilityKind mangledKind;
switch (F->getDifferentiabilityKind().Value) {
#define CASE(X) case FunctionMetadataDifferentiabilityKind::X: \
mangledKind = MangledDifferentiabilityKind::X; break;
CASE(NonDifferentiable)
CASE(Forward)
CASE(Reverse)
CASE(Normal)
CASE(Linear)
#undef CASE
}
funcNode->addChild(
Dem.createNode(
Node::Kind::DifferentiableFunctionType,
(Node::IndexType)mangledKind),
Dem);
}
if (F->getFlags().isThrowing())
funcNode->addChild(Dem.createNode(Node::Kind::ThrowsAnnotation), Dem);
if (F->getFlags().isSendable()) {
funcNode->addChild(
Dem.createNode(Node::Kind::ConcurrentFunctionType), Dem);
}
if (F->getFlags().isAsync())
funcNode->addChild(Dem.createNode(Node::Kind::AsyncAnnotation), Dem);
funcNode->addChild(parameters, Dem);
funcNode->addChild(result, Dem);
return funcNode;
}
Demangle::NodePointer
visitProtocolCompositionTypeRef(const ProtocolCompositionTypeRef *PC) {
auto type_list = Dem.createNode(Node::Kind::TypeList);
for (auto protocol : PC->getProtocols())
type_list->addChild(visit(protocol), Dem);
auto proto_list = Dem.createNode(Node::Kind::ProtocolList);
proto_list->addChild(type_list, Dem);
auto node = proto_list;
if (auto superclass = PC->getSuperclass()) {
node = Dem.createNode(Node::Kind::ProtocolListWithClass);
node->addChild(proto_list, Dem);
node->addChild(visit(superclass), Dem);
} else if (PC->hasExplicitAnyObject()) {
node = Dem.createNode(Node::Kind::ProtocolListWithAnyObject);
node->addChild(proto_list, Dem);
}
return node;
}
Demangle::NodePointer
visitConstrainedExistentialTypeRef(const ConstrainedExistentialTypeRef *CET) {
auto node = Dem.createNode(Node::Kind::ConstrainedExistential);
node->addChild(visit(CET->getBase()), Dem);
auto constraintList =
Dem.createNode(Node::Kind::ConstrainedExistentialRequirementList);
for (auto req : CET->getRequirements())
constraintList->addChild(visitTypeRefRequirement(req), Dem);
node->addChild(constraintList, Dem);
return node;
}
Demangle::NodePointer visitMetatypeTypeRef(const MetatypeTypeRef *M) {
auto node = Dem.createNode(Node::Kind::Metatype);
// FIXME: This is lossy. @objc_metatype is also abstract.
auto repr = Dem.createNode(Node::Kind::MetatypeRepresentation,
M->wasAbstract() ? "@thick" : "@thin");
node->addChild(repr, Dem);
node->addChild(visit(M->getInstanceType()), Dem);
return node;
}
Demangle::NodePointer
visitExistentialMetatypeTypeRef(const ExistentialMetatypeTypeRef *EM) {
auto node = Dem.createNode(Node::Kind::Metatype);
node->addChild(visit(EM->getInstanceType()), Dem);
return node;
}
Demangle::NodePointer
visitGenericTypeParameterTypeRef(const GenericTypeParameterTypeRef *GTP) {
auto node = Dem.createNode(Node::Kind::DependentGenericParamType);
node->addChild(Dem.createNode(Node::Kind::Index, GTP->getDepth()), Dem);
node->addChild(Dem.createNode(Node::Kind::Index, GTP->getIndex()), Dem);
return node;
}
Demangle::NodePointer
visitDependentMemberTypeRef(const DependentMemberTypeRef *DM) {
auto node = Dem.createNode(Node::Kind::DependentMemberType);
auto Base = visit(DM->getBase());
node->addChild(Base, Dem);
auto MemberId = Dem.createNode(Node::Kind::Identifier, DM->getMember());
auto MangledProtocol = DM->getProtocol();
if (MangledProtocol.empty()) {
// If there's no protocol, add the Member as an Identifier node
node->addChild(MemberId, Dem);
} else {
// Otherwise, build up a DependentAssociatedTR node with
// the member Identifer and protocol
auto AssocTy = Dem.createNode(Node::Kind::DependentAssociatedTypeRef);
AssocTy->addChild(MemberId, Dem);
auto Proto = Dem.demangleType(MangledProtocol);
assert(Proto && "Failed to demangle");
assert(Proto->getKind() == Node::Kind::Type && "Protocol type is not a type?!");
AssocTy->addChild(Proto, Dem);
node->addChild(AssocTy, Dem);
}
return node;
}
Demangle::NodePointer visitForeignClassTypeRef(const ForeignClassTypeRef *F) {
return demangleAndUnwrapType(F->getName());
}
Demangle::NodePointer visitObjCClassTypeRef(const ObjCClassTypeRef *OC) {
auto module = Dem.createNode(Node::Kind::Module, MANGLING_MODULE_OBJC);
auto node = Dem.createNode(Node::Kind::Class);
node->addChild(module, Dem);
node->addChild(Dem.createNode(Node::Kind::Identifier, OC->getName()), Dem);
return node;
}
Demangle::NodePointer
visitObjCProtocolTypeRef(const ObjCProtocolTypeRef *OC) {
auto module = Dem.createNode(Node::Kind::Module, MANGLING_MODULE_OBJC);
auto node = Dem.createNode(Node::Kind::Protocol);
node->addChild(module, Dem);
node->addChild(Dem.createNode(Node::Kind::Identifier, OC->getName()), Dem);
return node;
}
#define REF_STORAGE(Name, name, ...) \
Demangle::NodePointer visit##Name##StorageTypeRef( \
const Name##StorageTypeRef *US) { \
auto node = Dem.createNode(Node::Kind::Name); \
node->addChild(visit(US->getType()), Dem); \
return node; \
}
#include "swift/AST/ReferenceStorage.def"
Demangle::NodePointer visitSILBoxTypeRef(const SILBoxTypeRef *SB) {
auto node = Dem.createNode(Node::Kind::SILBoxType);
node->addChild(visit(SB->getBoxedType()), Dem);
return node;
}
Demangle::NodePointer visitTypeRefRequirement(const TypeRefRequirement &req) {
switch (req.getKind()) {
case RequirementKind::SameShape: {
// Not implemented.
return nullptr;
}
case RequirementKind::Conformance: {
auto r = Dem.createNode(Node::Kind::DependentGenericConformanceRequirement);
r->addChild(visit(req.getFirstType()), Dem);
r->addChild(visit(req.getSecondType()), Dem);
return r;
}
case RequirementKind::Superclass: {
auto r = Dem.createNode(Node::Kind::DependentGenericConformanceRequirement);
r->addChild(visit(req.getFirstType()), Dem);
r->addChild(visit(req.getSecondType()), Dem);
return r;
}
case RequirementKind::SameType: {
auto r = Dem.createNode(Node::Kind::DependentGenericSameTypeRequirement);
r->addChild(visit(req.getFirstType()), Dem);
r->addChild(visit(req.getSecondType()), Dem);
return r;
}
case RequirementKind::Layout:
// Not implemented.
return nullptr;
}
}
Demangle::NodePointer
visitSILBoxTypeWithLayoutTypeRef(const SILBoxTypeWithLayoutTypeRef *SB) {
auto node = Dem.createNode(Node::Kind::SILBoxTypeWithLayout);
auto layout = Dem.createNode(Node::Kind::SILBoxLayout);
for (auto &f : SB->getFields()) {
auto field =
Dem.createNode(f.isMutable() ? Node::Kind::SILBoxMutableField
: Node::Kind::SILBoxImmutableField);
field->addChild(visit(f.getType()), Dem);
layout->addChild(field, Dem);
}
node->addChild(layout, Dem);
auto signature = Dem.createNode(Node::Kind::DependentGenericSignature);
auto addCount = [&](unsigned count) {
signature->addChild(
Dem.createNode(Node::Kind::DependentGenericParamCount, count), Dem);
};
unsigned depth = 0;
unsigned index = 0;
for (auto &s : SB->getSubstitutions())
if (auto *param = dyn_cast<GenericTypeParameterTypeRef>(s.first)) {
while (param->getDepth() > depth) {
addCount(index);
++depth, index = 0;
}
assert(index == param->getIndex() && "generic params out of order");
++index;
}
for (auto &req : SB->getRequirements()) {
auto *r = visitTypeRefRequirement(req);
if (!r)
continue;
signature->addChild(r, Dem);
}
node->addChild(signature, Dem);
auto list = Dem.createNode(Node::Kind::TypeList);
for (auto &subst : SB->getSubstitutions())
list->addChild(visit(subst.second), Dem);
node->addChild(list, Dem);
return node;
}
Demangle::NodePointer visitOpaqueTypeRef(const OpaqueTypeRef *O) {
return Dem.createNode(Node::Kind::OpaqueType);
}
Demangle::NodePointer visitOpaqueArchetypeTypeRef(const OpaqueArchetypeTypeRef *O) {
auto decl = Dem.demangleType(O->getID());
if (!decl)
return nullptr;
auto index = Dem.createNode(Node::Kind::Index, O->getOrdinal());
auto argNodeLists = Dem.createNode(Node::Kind::TypeList);