forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenReflection.cpp
986 lines (823 loc) · 30.9 KB
/
GenReflection.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
//===--- GenReflection.cpp - IR generation for nominal type 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
//
//===----------------------------------------------------------------------===//
//
// This file implements IR generation of type metadata for struct/class
// stored properties and enum cases for use with reflection.
//===----------------------------------------------------------------------===//
#include "swift/AST/Decl.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/IRGen/Linking.h"
#include "swift/Reflection/MetadataSourceBuilder.h"
#include "swift/Reflection/Records.h"
#include "swift/SIL/SILModule.h"
#include "ConstantBuilder.h"
#include "GenClass.h"
#include "GenDecl.h"
#include "GenEnum.h"
#include "GenHeap.h"
#include "GenProto.h"
#include "GenType.h"
#include "IRGenMangler.h"
#include "IRGenModule.h"
#include "LoadableTypeInfo.h"
using namespace swift;
using namespace irgen;
using namespace reflection;
class MetadataSourceEncoder
: public MetadataSourceVisitor<MetadataSourceEncoder> {
llvm::raw_ostream &OS;
public:
MetadataSourceEncoder(llvm::raw_ostream &OS) : OS(OS) {}
void
visitClosureBindingMetadataSource(const ClosureBindingMetadataSource *CB) {
OS << 'B';
OS << CB->getIndex();
}
void
visitReferenceCaptureMetadataSource(const ReferenceCaptureMetadataSource *RC){
OS << 'R';
OS << RC->getIndex();
}
void
visitMetadataCaptureMetadataSource(const MetadataCaptureMetadataSource *MC) {
OS << 'M';
OS << MC->getIndex();
}
void
visitGenericArgumentMetadataSource(const GenericArgumentMetadataSource *GA) {
OS << 'G';
OS << GA->getIndex();
visit(GA->getSource());
OS << '_';
}
void visitSelfMetadataSource(const SelfMetadataSource *S) {
OS << 'S';
}
void
visitSelfWitnessTableMetadataSource(const SelfWitnessTableMetadataSource *S) {
OS << 'W';
}
};
class PrintMetadataSource
: public MetadataSourceVisitor<PrintMetadataSource, void> {
llvm::raw_ostream &OS;
unsigned Indent;
llvm::raw_ostream &indent(unsigned Amount) {
for (unsigned i = 0; i < Amount; ++i)
OS << ' ';
return OS;
}
llvm::raw_ostream &printHeader(std::string Name) {
indent(Indent) << '(' << Name;
return OS;
}
template<typename T>
llvm::raw_ostream &printField(std::string name, const T &value) {
if (!name.empty())
OS << " " << name << "=" << value;
else
OS << " " << value;
return OS;
}
void printRec(const reflection::MetadataSource *MS) {
OS << "\n";
Indent += 2;
visit(MS);
Indent -= 2;
}
void closeForm() {
OS << ')';
}
public:
PrintMetadataSource(llvm::raw_ostream &OS, unsigned Indent)
: OS(OS), Indent(Indent) {}
void
visitClosureBindingMetadataSource(const ClosureBindingMetadataSource *CB) {
printHeader("closure-binding");
printField("index", CB->getIndex());
closeForm();
}
void
visitReferenceCaptureMetadataSource(const ReferenceCaptureMetadataSource *RC){
printHeader("reference-capture");
printField("index", RC->getIndex());
closeForm();
}
void
visitMetadataCaptureMetadataSource(const MetadataCaptureMetadataSource *MC){
printHeader("metadata-capture");
printField("index", MC->getIndex());
closeForm();
}
void
visitGenericArgumentMetadataSource(const GenericArgumentMetadataSource *GA) {
printHeader("generic-argument");
printField("index", GA->getIndex());
printRec(GA->getSource());
closeForm();
}
void
visitSelfMetadataSource(const SelfMetadataSource *S) {
printHeader("self");
closeForm();
}
void
visitSelfWitnessTableMetadataSource(const SelfWitnessTableMetadataSource *S) {
printHeader("self-witness-table");
closeForm();
}
};
llvm::Constant *IRGenModule::getTypeRef(CanType type) {
IRGenMangler Mangler;
auto SymbolicName = Mangler.mangleTypeForReflection(*this, type);
return getAddrOfStringForTypeRef(SymbolicName);
}
class ReflectionMetadataBuilder {
protected:
IRGenModule &IGM;
ConstantInitBuilder InitBuilder;
ConstantStructBuilder B;
ReflectionMetadataBuilder(IRGenModule &IGM)
: IGM(IGM), InitBuilder(IGM), B(InitBuilder.beginStruct()) {}
virtual ~ReflectionMetadataBuilder() {}
// Collect any builtin types referenced from this type.
void addBuiltinTypeRefs(CanType type) {
type.visit([&](CanType t) {
if (IGM.getSwiftModule()->isStdlibModule() && isa<BuiltinType>(t))
IGM.BuiltinTypes.insert(t);
// We need size/alignment information for imported value types,
// so emit builtin descriptors for them.
//
// In effect they're treated like an opaque blob, which is OK
// for now, at least until we want to import C++ types or
// something like that.
//
// Classes and protocols go down a different path.
if (auto Nominal = t->getAnyNominal())
if (Nominal->hasClangNode()) {
if (auto CD = dyn_cast<ClassDecl>(Nominal))
IGM.ImportedClasses.insert(CD);
else if (auto PD = dyn_cast<ProtocolDecl>(Nominal))
IGM.ImportedProtocols.insert(PD);
else
IGM.OpaqueTypes.insert(Nominal);
}
});
}
/// Add a 32-bit relative offset to a mangled typeref string
/// in the typeref reflection section.
void addTypeRef(CanType type) {
B.addRelativeAddress(IGM.getTypeRef(type));
}
/// Add a 32-bit relative offset to a mangled nominal type string
/// in the typeref reflection section.
void addNominalRef(const NominalTypeDecl *nominal) {
if (auto proto = dyn_cast<ProtocolDecl>(nominal)) {
IRGenMangler mangler;
SymbolicMangling mangledStr;
mangledStr.String = mangler.mangleBareProtocol(proto);
auto mangledName = IGM.getAddrOfStringForTypeRef(mangledStr);
B.addRelativeAddress(mangledName);
} else {
CanType type = nominal->getDeclaredType()->getCanonicalType();
B.addRelativeAddress(IGM.getTypeRef(type));
}
}
// A function signature for a lambda wrapping an IRGenModule::getAddrOf*
// method.
using GetAddrOfEntityFn = llvm::Constant* (IRGenModule &, ConstantInit);
llvm::GlobalVariable *emit(
Optional<llvm::function_ref<GetAddrOfEntityFn>> getAddr,
const char *section) {
layout();
llvm::GlobalVariable *var;
// Some reflection records have a mangled symbol name, for uniquing
// imported type metadata.
if (getAddr) {
auto init = B.finishAndCreateFuture();
var = cast<llvm::GlobalVariable>((*getAddr)(IGM, init));
var->setConstant(true);
// Others, such as capture descriptors, do not have a name.
} else {
var = B.finishAndCreateGlobal("\x01l__swift5_reflection_descriptor",
Alignment(4), /*isConstant*/ true,
llvm::GlobalValue::PrivateLinkage);
}
var->setSection(section);
IGM.addUsedGlobal(var);
disableAddressSanitizer(IGM, var);
return var;
}
// Helpers to guide the C++ type system into converting lambda arguments
// to Optional<function_ref>
llvm::GlobalVariable *emit(llvm::function_ref<GetAddrOfEntityFn> getAddr,
const char *section) {
return emit(Optional<llvm::function_ref<GetAddrOfEntityFn>>(getAddr),
section);
}
llvm::GlobalVariable *emit(NoneType none,
const char *section) {
return emit(Optional<llvm::function_ref<GetAddrOfEntityFn>>(),
section);
}
virtual void layout() = 0;
};
class AssociatedTypeMetadataBuilder : public ReflectionMetadataBuilder {
static const uint32_t AssociatedTypeRecordSize = 8;
const ProtocolConformance *Conformance;
ArrayRef<std::pair<StringRef, CanType>> AssociatedTypes;
void layout() override {
// If the conforming type is generic, we just want to emit the
// unbound generic type here.
auto *Nominal = Conformance->getType()->getAnyNominal();
assert(Nominal && "Structural conformance?");
PrettyStackTraceDecl DebugStack("emitting associated type metadata",
Nominal);
addTypeRef(Nominal->getDeclaredType()->getCanonicalType());
addNominalRef(Conformance->getProtocol());
B.addInt32(AssociatedTypes.size());
B.addInt32(AssociatedTypeRecordSize);
for (auto AssocTy : AssociatedTypes) {
auto NameGlobal = IGM.getAddrOfFieldName(AssocTy.first);
B.addRelativeAddress(NameGlobal);
addBuiltinTypeRefs(AssocTy.second);
addTypeRef(AssocTy.second);
}
}
public:
AssociatedTypeMetadataBuilder(IRGenModule &IGM,
const ProtocolConformance *Conformance,
ArrayRef<std::pair<StringRef, CanType>> AssociatedTypes)
: ReflectionMetadataBuilder(IGM), Conformance(Conformance),
AssociatedTypes(AssociatedTypes) {}
llvm::GlobalVariable *emit() {
auto section = IGM.getAssociatedTypeMetadataSectionName();
return ReflectionMetadataBuilder::emit(
[&](IRGenModule &IGM, ConstantInit init) -> llvm::Constant* {
return IGM.getAddrOfReflectionAssociatedTypeDescriptor(Conformance,init);
},
section);
}
};
class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
const uint32_t fieldRecordSize = 12;
const NominalTypeDecl *NTD;
void addFieldDecl(const ValueDecl *value, CanType type,
bool indirect=false) {
reflection::FieldRecordFlags flags;
flags.setIsIndirectCase(indirect);
if (auto var = dyn_cast<VarDecl>(value))
flags.setIsVar(!var->isLet());
B.addInt32(flags.getRawValue());
if (!type) {
B.addInt32(0);
} else {
addTypeRef(type);
addBuiltinTypeRefs(type);
}
if (IGM.IRGen.Opts.EnableReflectionNames) {
auto name = value->getBaseName().getIdentifier().str();
auto fieldName = IGM.getAddrOfFieldName(name);
B.addRelativeAddress(fieldName);
} else {
B.addInt32(0);
}
}
void layoutRecord() {
auto kind = FieldDescriptorKind::Struct;
if (auto CD = dyn_cast<ClassDecl>(NTD)) {
auto type = CD->getDeclaredType()->getCanonicalType();
auto RC = type->getReferenceCounting();
if (RC == ReferenceCounting::ObjC)
kind = FieldDescriptorKind::ObjCClass;
else
kind = FieldDescriptorKind::Class;
}
B.addInt16(uint16_t(kind));
B.addInt16(fieldRecordSize);
// Imported classes don't need field descriptors
if (NTD->hasClangNode() && isa<ClassDecl>(NTD)) {
B.addInt32(0);
return;
}
assert(!NTD->hasClangNode() || isa<StructDecl>(NTD));
auto properties = NTD->getStoredProperties();
B.addInt32(std::distance(properties.begin(), properties.end()));
for (auto property : properties)
addFieldDecl(property,
property->getInterfaceType()
->getCanonicalType());
}
void layoutEnum() {
auto enumDecl = cast<EnumDecl>(NTD);
auto &strategy = irgen::getEnumImplStrategy(
IGM, enumDecl->getDeclaredTypeInContext()
->getCanonicalType());
auto kind = FieldDescriptorKind::Enum;
// If this is a fixed-size multi-payload enum, we have to emit a descriptor
// with the size and alignment of the type, because the reflection library
// cannot derive this information at runtime.
if (strategy.getElementsWithPayload().size() > 1 &&
!strategy.needsPayloadSizeInMetadata()) {
kind = FieldDescriptorKind::MultiPayloadEnum;
IGM.OpaqueTypes.insert(enumDecl);
}
B.addInt16(uint16_t(kind));
B.addInt16(fieldRecordSize);
B.addInt32(strategy.getElementsWithPayload().size()
+ strategy.getElementsWithNoPayload().size());
for (auto enumCase : strategy.getElementsWithPayload()) {
bool indirect = (enumCase.decl->isIndirect() ||
enumDecl->isIndirect());
addFieldDecl(enumCase.decl,
enumCase.decl->getArgumentInterfaceType()
->getCanonicalType(),
indirect);
}
for (auto enumCase : strategy.getElementsWithNoPayload()) {
addFieldDecl(enumCase.decl, CanType());
}
}
void layoutProtocol() {
auto protocolDecl = cast<ProtocolDecl>(NTD);
FieldDescriptorKind Kind;
if (protocolDecl->isObjC())
Kind = FieldDescriptorKind::ObjCProtocol;
else if (protocolDecl->requiresClass())
Kind = FieldDescriptorKind::ClassProtocol;
else
Kind = FieldDescriptorKind::Protocol;
B.addInt16(uint16_t(Kind));
B.addInt16(fieldRecordSize);
B.addInt32(0);
}
void layout() override {
if (NTD->hasClangNode() &&
!isa<ClassDecl>(NTD) &&
!isa<StructDecl>(NTD) &&
!isa<ProtocolDecl>(NTD))
return;
PrettyStackTraceDecl DebugStack("emitting field type metadata", NTD);
addNominalRef(NTD);
auto *CD = dyn_cast<ClassDecl>(NTD);
if (CD && CD->getSuperclass()) {
addTypeRef(CD->getSuperclass()->getCanonicalType());
} else {
B.addInt32(0);
}
switch (NTD->getKind()) {
case DeclKind::Class:
case DeclKind::Struct:
layoutRecord();
break;
case DeclKind::Enum:
layoutEnum();
break;
case DeclKind::Protocol:
layoutProtocol();
break;
default:
llvm_unreachable("Not a nominal type");
break;
}
}
public:
FieldTypeMetadataBuilder(IRGenModule &IGM,
const NominalTypeDecl * NTD)
: ReflectionMetadataBuilder(IGM), NTD(NTD) {}
llvm::GlobalVariable *emit() {
auto section = IGM.getFieldTypeMetadataSectionName();
return ReflectionMetadataBuilder::emit(
[&](IRGenModule &IGM, ConstantInit definition) -> llvm::Constant* {
return IGM.getAddrOfReflectionFieldDescriptor(
NTD->getDeclaredType()->getCanonicalType(), definition);
},
section);
}
};
class FixedTypeMetadataBuilder : public ReflectionMetadataBuilder {
ModuleDecl *module;
CanType type;
const FixedTypeInfo *ti;
public:
FixedTypeMetadataBuilder(IRGenModule &IGM,
CanType builtinType)
: ReflectionMetadataBuilder(IGM) {
module = builtinType->getASTContext().TheBuiltinModule;
type = builtinType;
ti = &cast<FixedTypeInfo>(IGM.getTypeInfoForUnlowered(builtinType));
}
FixedTypeMetadataBuilder(IRGenModule &IGM,
const NominalTypeDecl *nominalDecl)
: ReflectionMetadataBuilder(IGM) {
module = nominalDecl->getParentModule();
type = nominalDecl->getDeclaredType()->getCanonicalType();
ti = &cast<FixedTypeInfo>(IGM.getTypeInfoForUnlowered(
nominalDecl->getDeclaredTypeInContext()->getCanonicalType()));
}
void layout() override {
addTypeRef(type);
B.addInt32(ti->getFixedSize().getValue());
B.addInt32(ti->getFixedAlignment().getValue());
B.addInt32(ti->getFixedStride().getValue());
B.addInt32(ti->getFixedExtraInhabitantCount(IGM));
}
llvm::GlobalVariable *emit() {
auto section = IGM.getBuiltinTypeMetadataSectionName();
return ReflectionMetadataBuilder::emit(
[&](IRGenModule &IGM, ConstantInit definition) -> llvm::Constant * {
return IGM.getAddrOfReflectionBuiltinDescriptor(type, definition);
},
section);
}
};
void IRGenModule::emitBuiltinTypeMetadataRecord(CanType builtinType) {
FixedTypeMetadataBuilder builder(*this, builtinType);
builder.emit();
}
void IRGenModule::emitOpaqueTypeMetadataRecord(const NominalTypeDecl *nominalDecl) {
FixedTypeMetadataBuilder builder(*this, nominalDecl);
builder.emit();
}
bool IRGenModule::shouldEmitOpaqueTypeMetadataRecord(
const NominalTypeDecl *nominalDecl) {
if (nominalDecl->getAttrs().hasAttribute<AlignmentAttr>()) {
auto &ti = getTypeInfoForUnlowered(nominalDecl->getDeclaredTypeInContext());
if (isa<FixedTypeInfo>(ti))
return true;
}
return false;
}
/// Builds a constant LLVM struct describing the layout of a fixed-size
/// SIL @box. These look like closure contexts, but without any necessary
/// bindings or metadata sources, and only a single captured value.
class BoxDescriptorBuilder : public ReflectionMetadataBuilder {
CanType BoxedType;
public:
BoxDescriptorBuilder(IRGenModule &IGM, CanType BoxedType)
: ReflectionMetadataBuilder(IGM), BoxedType(BoxedType) {}
void layout() override {
B.addInt32(1);
B.addInt32(0); // Number of sources
B.addInt32(0); // Number of generic bindings
addTypeRef(BoxedType);
addBuiltinTypeRefs(BoxedType);
}
llvm::GlobalVariable *emit() {
auto section = IGM.getCaptureDescriptorMetadataSectionName();
return ReflectionMetadataBuilder::emit(None, section);
}
};
/// Builds a constant LLVM struct describing the layout of a heap closure,
/// the types of its captures, and the sources of metadata if any of the
/// captures are generic.
class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
swift::reflection::MetadataSourceBuilder SourceBuilder;
CanSILFunctionType OrigCalleeType;
CanSILFunctionType SubstCalleeType;
SubstitutionMap Subs;
const HeapLayout &Layout;
public:
CaptureDescriptorBuilder(IRGenModule &IGM,
CanSILFunctionType OrigCalleeType,
CanSILFunctionType SubstCalleeType,
SubstitutionMap Subs,
const HeapLayout &Layout)
: ReflectionMetadataBuilder(IGM),
OrigCalleeType(OrigCalleeType),
SubstCalleeType(SubstCalleeType), Subs(Subs),
Layout(Layout) {}
using MetadataSourceMap
= std::vector<std::pair<CanType, const reflection::MetadataSource*>>;
void addMetadataSource(const reflection::MetadataSource *Source) {
if (Source == nullptr) {
B.addInt32(0);
} else {
SmallString<16> EncodeBuffer;
llvm::raw_svector_ostream OS(EncodeBuffer);
MetadataSourceEncoder Encoder(OS);
Encoder.visit(Source);
auto EncodedSource = IGM.getAddrOfStringForTypeRef(OS.str());
B.addRelativeAddress(EncodedSource);
}
}
/// Give up if we captured an opened existential type. Eventually we
/// should figure out how to represent this.
static bool hasOpenedExistential(CanSILFunctionType OrigCalleeType,
const HeapLayout &Layout) {
if (!OrigCalleeType->isPolymorphic() ||
OrigCalleeType->isPseudogeneric())
return false;
auto &Bindings = Layout.getBindings();
for (unsigned i = 0; i < Bindings.size(); ++i) {
// Skip protocol requirements (FIXME: for now?)
if (Bindings[i].Protocol != nullptr)
continue;
if (Bindings[i].TypeParameter->hasOpenedExistential())
return true;
}
auto ElementTypes = Layout.getElementTypes().slice(
Layout.hasBindings() ? 1 : 0);
for (auto ElementType : ElementTypes) {
auto SwiftType = ElementType.getASTType();
if (SwiftType->hasOpenedExistential())
return true;
}
return false;
}
/// Slice off the NecessaryBindings struct at the beginning, if it's there.
/// We'll keep track of how many things are in the bindings struct with its
/// own count in the capture descriptor.
ArrayRef<SILType> getElementTypes() {
return Layout.getElementTypes().slice(Layout.hasBindings() ? 1 : 0);
}
/// Build a map from generic parameter -> source of its metadata at runtime.
///
/// If the callee that we are partially applying to create a box/closure
/// isn't generic, then the map is empty.
MetadataSourceMap getMetadataSourceMap() {
MetadataSourceMap SourceMap;
// Generic parameters of pseudogeneric functions do not have
// runtime metadata.
if (!OrigCalleeType->isPolymorphic() ||
OrigCalleeType->isPseudogeneric())
return SourceMap;
// Any generic parameters that are not fulfilled are passed in via the
// bindings. Structural types are decomposed, so emit the contents of
// the bindings structure directly.
auto &Bindings = Layout.getBindings();
for (unsigned i = 0; i < Bindings.size(); ++i) {
// Skip protocol requirements (FIXME: for now?)
if (Bindings[i].Protocol != nullptr)
continue;
auto Source = SourceBuilder.createClosureBinding(i);
auto BindingType = Bindings[i].TypeParameter;
auto InterfaceType = BindingType->mapTypeOutOfContext();
SourceMap.push_back({InterfaceType->getCanonicalType(), Source});
}
// Check if any requirements were fulfilled by metadata stored inside a
// captured value.
enumerateGenericParamFulfillments(IGM, OrigCalleeType,
[&](CanType GenericParam,
const irgen::MetadataSource &Source,
const MetadataPath &Path) {
const reflection::MetadataSource *Root;
switch (Source.getKind()) {
case irgen::MetadataSource::Kind::SelfMetadata:
case irgen::MetadataSource::Kind::SelfWitnessTable:
// Handled as part of bindings
return;
case irgen::MetadataSource::Kind::GenericLValueMetadata:
// FIXME?
return;
case irgen::MetadataSource::Kind::ClassPointer:
Root = SourceBuilder.createReferenceCapture(Source.getParamIndex());
break;
case irgen::MetadataSource::Kind::Metadata:
Root = SourceBuilder.createMetadataCapture(Source.getParamIndex());
break;
}
// The metadata might be reached via a non-trivial path (eg,
// dereferencing an isa pointer or a generic argument). Record
// the path. We assume captured values map 1-1 with function
// parameters.
auto Src = Path.getMetadataSource(SourceBuilder, Root);
auto SubstType = GenericParam.subst(Subs);
auto InterfaceType = SubstType->mapTypeOutOfContext();
SourceMap.push_back({InterfaceType->getCanonicalType(), Src});
});
return SourceMap;
}
/// Get the interface types of all of the captured values, mapped out of the
/// context of the callee we're partially applying.
std::vector<CanType> getCaptureTypes() {
std::vector<CanType> CaptureTypes;
for (auto ElementType : getElementTypes()) {
auto SwiftType = ElementType.getASTType();
// Erase pseudogeneric captures down to AnyObject.
if (OrigCalleeType->isPseudogeneric()) {
SwiftType = SwiftType.transform([&](Type t) -> Type {
if (auto *archetype = t->getAs<ArchetypeType>()) {
assert(archetype->requiresClass() && "don't know what to do");
return IGM.Context.getAnyObjectType();
}
return t;
})->getCanonicalType();
}
auto InterfaceType = SwiftType->mapTypeOutOfContext();
CaptureTypes.push_back(InterfaceType->getCanonicalType());
}
return CaptureTypes;
}
void layout() override {
auto CaptureTypes = getCaptureTypes();
auto MetadataSources = getMetadataSourceMap();
B.addInt32(CaptureTypes.size());
B.addInt32(MetadataSources.size());
B.addInt32(Layout.getBindings().size());
// Now add typerefs of all of the captures.
for (auto CaptureType : CaptureTypes) {
addTypeRef(CaptureType);
addBuiltinTypeRefs(CaptureType);
}
// Add the pairs that make up the generic param -> metadata source map
// to the struct.
for (auto GenericAndSource : MetadataSources) {
auto GenericParam = GenericAndSource.first->getCanonicalType();
auto Source = GenericAndSource.second;
addTypeRef(GenericParam);
addMetadataSource(Source);
}
}
llvm::GlobalVariable *emit() {
auto section = IGM.getCaptureDescriptorMetadataSectionName();
return ReflectionMetadataBuilder::emit(None, section);
}
};
static std::string getReflectionSectionName(IRGenModule &IGM,
StringRef LongName,
StringRef FourCC) {
SmallString<50> SectionName;
llvm::raw_svector_ostream OS(SectionName);
switch (IGM.TargetInfo.OutputObjectFormat) {
case llvm::Triple::UnknownObjectFormat:
llvm_unreachable("unknown object format");
case llvm::Triple::COFF:
assert(FourCC.size() <= 4 &&
"COFF section name length must be <= 8 characters");
OS << ".sw5" << FourCC << "$B";
break;
case llvm::Triple::ELF:
OS << "swift5_" << LongName;
break;
case llvm::Triple::MachO:
assert(LongName.size() <= 7 &&
"Mach-O section name length must be <= 16 characters");
OS << "__TEXT,__swift5_" << LongName << ", regular, no_dead_strip";
break;
case llvm::Triple::Wasm:
llvm_unreachable("web assembly object format is not supported.");
break;
}
return OS.str();
}
const char *IRGenModule::getFieldTypeMetadataSectionName() {
if (FieldTypeSection.empty())
FieldTypeSection = getReflectionSectionName(*this, "fieldmd", "flmd");
return FieldTypeSection.c_str();
}
const char *IRGenModule::getBuiltinTypeMetadataSectionName() {
if (BuiltinTypeSection.empty())
BuiltinTypeSection = getReflectionSectionName(*this, "builtin", "bltn");
return BuiltinTypeSection.c_str();
}
const char *IRGenModule::getAssociatedTypeMetadataSectionName() {
if (AssociatedTypeSection.empty())
AssociatedTypeSection = getReflectionSectionName(*this, "assocty", "asty");
return AssociatedTypeSection.c_str();
}
const char *IRGenModule::getCaptureDescriptorMetadataSectionName() {
if (CaptureDescriptorSection.empty())
CaptureDescriptorSection = getReflectionSectionName(*this, "capture", "cptr");
return CaptureDescriptorSection.c_str();
}
const char *IRGenModule::getReflectionStringsSectionName() {
if (ReflectionStringsSection.empty())
ReflectionStringsSection = getReflectionSectionName(*this, "reflstr", "rfst");
return ReflectionStringsSection.c_str();
}
const char *IRGenModule::getReflectionTypeRefSectionName() {
if (ReflectionTypeRefSection.empty())
ReflectionTypeRefSection = getReflectionSectionName(*this, "typeref", "tyrf");
return ReflectionTypeRefSection.c_str();
}
llvm::Constant *IRGenModule::getAddrOfFieldName(StringRef Name) {
auto &entry = FieldNames[Name];
if (entry.second)
return entry.second;
entry = createStringConstant(Name, /*willBeRelativelyAddressed*/ true,
getReflectionStringsSectionName());
disableAddressSanitizer(*this, entry.first);
return entry.second;
}
llvm::Constant *
IRGenModule::getAddrOfBoxDescriptor(CanType BoxedType) {
if (!IRGen.Opts.EnableReflectionMetadata)
return llvm::Constant::getNullValue(CaptureDescriptorPtrTy);
BoxDescriptorBuilder builder(*this, BoxedType);
auto var = builder.emit();
return llvm::ConstantExpr::getBitCast(var, CaptureDescriptorPtrTy);
}
llvm::Constant *
IRGenModule::getAddrOfCaptureDescriptor(SILFunction &Caller,
CanSILFunctionType OrigCalleeType,
CanSILFunctionType SubstCalleeType,
SubstitutionMap Subs,
const HeapLayout &Layout) {
if (!IRGen.Opts.EnableReflectionMetadata)
return llvm::Constant::getNullValue(CaptureDescriptorPtrTy);
if (CaptureDescriptorBuilder::hasOpenedExistential(OrigCalleeType, Layout))
return llvm::Constant::getNullValue(CaptureDescriptorPtrTy);
CaptureDescriptorBuilder builder(*this,
OrigCalleeType, SubstCalleeType, Subs,
Layout);
auto var = builder.emit();
return llvm::ConstantExpr::getBitCast(var, CaptureDescriptorPtrTy);
}
void IRGenModule::
emitAssociatedTypeMetadataRecord(const ProtocolConformance *Conformance) {
if (!IRGen.Opts.EnableReflectionMetadata)
return;
SmallVector<std::pair<StringRef, CanType>, 2> AssociatedTypes;
auto collectTypeWitness = [&](const AssociatedTypeDecl *AssocTy,
Type Replacement,
const TypeDecl *TD) -> bool {
AssociatedTypes.push_back({
AssocTy->getNameStr(),
Replacement->getCanonicalType()
});
return false;
};
Conformance->forEachTypeWitness(/*resolver*/ nullptr, collectTypeWitness);
// If there are no associated types, don't bother emitting any
// metadata.
if (AssociatedTypes.empty())
return;
AssociatedTypeMetadataBuilder builder(*this, Conformance, AssociatedTypes);
builder.emit();
}
void IRGenModule::emitBuiltinReflectionMetadata() {
if (getSwiftModule()->isStdlibModule()) {
BuiltinTypes.insert(Context.TheNativeObjectType);
BuiltinTypes.insert(Context.TheUnknownObjectType);
BuiltinTypes.insert(Context.TheBridgeObjectType);
BuiltinTypes.insert(Context.TheRawPointerType);
BuiltinTypes.insert(Context.TheUnsafeValueBufferType);
// This would not be necessary if RawPointer had the same set of
// extra inhabitants as these. But maybe it's best not to codify
// that in the ABI anyway.
CanType thinFunction = CanFunctionType::get(
{}, Context.TheEmptyTupleType,
AnyFunctionType::ExtInfo().withRepresentation(
FunctionTypeRepresentation::Thin));
BuiltinTypes.insert(thinFunction);
CanType anyMetatype = CanExistentialMetatypeType::get(
Context.TheAnyType);
BuiltinTypes.insert(anyMetatype);
}
for (auto CD : ImportedClasses)
emitFieldMetadataRecord(CD);
for (auto PD : ImportedProtocols)
emitFieldMetadataRecord(PD);
for (auto SD : ImportedStructs)
emitFieldMetadataRecord(SD);
for (auto builtinType : BuiltinTypes)
emitBuiltinTypeMetadataRecord(builtinType);
for (auto nominalDecl : OpaqueTypes)
emitOpaqueTypeMetadataRecord(nominalDecl);
}
void IRGenerator::emitBuiltinReflectionMetadata() {
for (auto &m : *this) {
m.second->emitBuiltinReflectionMetadata();
}
}
void IRGenModule::emitFieldMetadataRecord(const NominalTypeDecl *Decl) {
if (!IRGen.Opts.EnableReflectionMetadata)
return;
// @objc enums never have generic parameters or payloads,
// and lower as their raw type.
if (auto *ED = dyn_cast<EnumDecl>(Decl))
if (ED->isObjC()) {
emitOpaqueTypeMetadataRecord(ED);
return;
}
FieldTypeMetadataBuilder builder(*this, Decl);
FieldDescriptors.push_back(builder.emit());
}
void IRGenModule::emitReflectionMetadataVersion() {
auto Init =
llvm::ConstantInt::get(Int16Ty, SWIFT_REFLECTION_METADATA_VERSION);
auto Version = new llvm::GlobalVariable(Module, Int16Ty, /*constant*/ true,
llvm::GlobalValue::LinkOnceODRLinkage,
Init,
"__swift_reflection_version");
Version->setVisibility(llvm::GlobalValue::HiddenVisibility);
addUsedGlobal(Version);
}
void IRGenerator::emitReflectionMetadataVersion() {
for (auto &m : *this) {
m.second->emitReflectionMetadataVersion();
}
}