forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSILDeclRef.cpp
917 lines (775 loc) · 29.2 KB
/
SILDeclRef.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
//===--- SILDeclRef.cpp - Implements SILDeclRef ---------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/SILDeclRef.h"
#include "swift/SIL/SILLocation.h"
#include "swift/AST/AnyFunctionRef.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTMangler.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/ParameterList.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/SIL/SILLinkage.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/raw_ostream.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
using namespace swift;
/// Get the method dispatch mechanism for a method.
MethodDispatch
swift::getMethodDispatch(AbstractFunctionDecl *method) {
// Some methods are forced to be statically dispatched.
if (method->hasForcedStaticDispatch())
return MethodDispatch::Static;
// Import-as-member declarations are always statically referenced.
if (method->isImportAsMember())
return MethodDispatch::Static;
auto dc = method->getDeclContext();
if (dc->getSelfClassDecl()) {
if (method->isDynamic())
return MethodDispatch::Class;
// Final methods can be statically referenced.
if (method->isFinal())
return MethodDispatch::Static;
// Members defined directly inside a class are dynamically dispatched.
if (isa<ClassDecl>(dc))
return MethodDispatch::Class;
// Imported class methods are dynamically dispatched.
if (method->isObjC() && method->hasClangNode())
return MethodDispatch::Class;
}
// Otherwise, it can be referenced statically.
return MethodDispatch::Static;
}
bool swift::requiresForeignToNativeThunk(ValueDecl *vd) {
// Functions imported from C, Objective-C methods imported from Objective-C,
// as well as methods in @objc protocols (even protocols defined in Swift)
// require a foreign to native thunk.
auto dc = vd->getDeclContext();
if (auto proto = dyn_cast<ProtocolDecl>(dc))
if (proto->isObjC())
return true;
if (auto fd = dyn_cast<FuncDecl>(vd))
return fd->hasClangNode();
return false;
}
bool swift::requiresForeignEntryPoint(ValueDecl *vd) {
assert(!isa<AbstractStorageDecl>(vd));
if (vd->isDynamic())
return true;
if (vd->isObjC() && isa<ProtocolDecl>(vd->getDeclContext()))
return true;
if (vd->isImportAsMember())
return true;
if (vd->hasClangNode())
return true;
if (auto *accessor = dyn_cast<AccessorDecl>(vd)) {
// Property accessors should be generated alongside the property.
if (accessor->isGetterOrSetter()) {
auto *asd = accessor->getStorage();
if (asd->isObjC() && asd->hasClangNode())
return true;
}
}
return false;
}
SILDeclRef::SILDeclRef(ValueDecl *vd, SILDeclRef::Kind kind,
bool isCurried, bool isForeign)
: loc(vd), kind(kind),
isCurried(isCurried), isForeign(isForeign),
isDirectReference(0), defaultArgIndex(0)
{}
SILDeclRef::SILDeclRef(SILDeclRef::Loc baseLoc,
bool isCurried, bool asForeign)
: isCurried(isCurried), isDirectReference(0), defaultArgIndex(0)
{
if (auto *vd = baseLoc.dyn_cast<ValueDecl*>()) {
if (auto *fd = dyn_cast<FuncDecl>(vd)) {
// Map FuncDecls directly to Func SILDeclRefs.
loc = fd;
kind = Kind::Func;
}
// Map ConstructorDecls to the Allocator SILDeclRef of the constructor.
else if (auto *cd = dyn_cast<ConstructorDecl>(vd)) {
loc = cd;
kind = Kind::Allocator;
}
// Map EnumElementDecls to the EnumElement SILDeclRef of the element.
else if (auto *ed = dyn_cast<EnumElementDecl>(vd)) {
loc = ed;
kind = Kind::EnumElement;
}
// VarDecl constants require an explicit kind.
else if (isa<VarDecl>(vd)) {
llvm_unreachable("must create SILDeclRef for VarDecl with explicit kind");
}
// Map DestructorDecls to the Deallocator of the destructor.
else if (auto dtor = dyn_cast<DestructorDecl>(vd)) {
loc = dtor;
kind = Kind::Deallocator;
}
else {
llvm_unreachable("invalid loc decl for SILDeclRef!");
}
} else if (auto *ACE = baseLoc.dyn_cast<AbstractClosureExpr *>()) {
loc = ACE;
kind = Kind::Func;
} else {
llvm_unreachable("impossible SILDeclRef loc");
}
isForeign = asForeign;
}
Optional<AnyFunctionRef> SILDeclRef::getAnyFunctionRef() const {
if (auto vd = loc.dyn_cast<ValueDecl*>()) {
if (auto afd = dyn_cast<AbstractFunctionDecl>(vd)) {
return AnyFunctionRef(afd);
} else {
return None;
}
}
return AnyFunctionRef(loc.get<AbstractClosureExpr*>());
}
bool SILDeclRef::isThunk() const {
return isCurried || isForeignToNativeThunk() || isNativeToForeignThunk();
}
bool SILDeclRef::isClangImported() const {
if (!hasDecl())
return false;
ValueDecl *d = getDecl();
DeclContext *moduleContext = d->getDeclContext()->getModuleScopeContext();
if (isa<ClangModuleUnit>(moduleContext)) {
if (isClangGenerated())
return true;
if (isa<ConstructorDecl>(d) || isa<EnumElementDecl>(d))
return !isForeign;
if (auto *FD = dyn_cast<FuncDecl>(d))
if (isa<AccessorDecl>(FD) ||
isa<NominalTypeDecl>(d->getDeclContext()))
return !isForeign;
}
return false;
}
bool SILDeclRef::isClangGenerated() const {
if (!hasDecl())
return false;
return isClangGenerated(getDecl()->getClangNode());
}
// FIXME: this is a weird predicate.
bool SILDeclRef::isClangGenerated(ClangNode node) {
if (auto nd = dyn_cast_or_null<clang::NamedDecl>(node.getAsDecl())) {
// ie, 'static inline' functions for which we must ask Clang to emit a body
// for explicitly
if (!nd->isExternallyVisible())
return true;
}
return false;
}
bool SILDeclRef::isImplicit() const {
if (hasDecl())
return getDecl()->isImplicit();
return getAbstractClosureExpr()->isImplicit();
}
SILLinkage SILDeclRef::getLinkage(ForDefinition_t forDefinition) const {
if (getAbstractClosureExpr()) {
return isSerialized() ? SILLinkage::Shared : SILLinkage::Private;
}
// Add External to the linkage (e.g. Public -> PublicExternal) if this is a
// declaration not a definition.
auto maybeAddExternal = [&](SILLinkage linkage) {
return forDefinition ? linkage : addExternalToLinkage(linkage);
};
// Native function-local declarations have shared linkage.
// FIXME: @objc declarations should be too, but we currently have no way
// of marking them "used" other than making them external.
ValueDecl *d = getDecl();
DeclContext *moduleContext = d->getDeclContext();
while (!moduleContext->isModuleScopeContext()) {
if (!isForeign && moduleContext->isLocalContext()) {
return isSerialized() ? SILLinkage::Shared : SILLinkage::Private;
}
moduleContext = moduleContext->getParent();
}
// Enum constructors and curry thunks either have private or shared
// linkage, dependings are essentially the same as thunks, they are
// emitted by need and have shared linkage.
if (isEnumElement() || isCurried) {
switch (d->getEffectiveAccess()) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
return maybeAddExternal(SILLinkage::Private);
case AccessLevel::Internal:
case AccessLevel::Public:
case AccessLevel::Open:
return SILLinkage::Shared;
}
}
// Calling convention thunks have shared linkage.
if (isForeignToNativeThunk())
return SILLinkage::Shared;
// If a function declares a @_cdecl name, its native-to-foreign thunk
// is exported with the visibility of the function.
if (isNativeToForeignThunk() && !d->getAttrs().hasAttribute<CDeclAttr>())
return SILLinkage::Shared;
// Declarations imported from Clang modules have shared linkage.
if (isClangImported())
return SILLinkage::Shared;
// Default argument generators of Public functions have PublicNonABI linkage
// if the function was type-checked in Swift 4 mode.
if (kind == SILDeclRef::Kind::DefaultArgGenerator) {
if (isSerialized())
return maybeAddExternal(SILLinkage::PublicNonABI);
}
enum class Limit {
/// No limit.
None,
/// The declaration is emitted on-demand; it should end up with internal
/// or shared linkage.
OnDemand,
/// The declaration should never be made public.
NeverPublic
};
auto limit = Limit::None;
// ivar initializers and destroyers are completely contained within the class
// from which they come, and never get seen externally.
if (isIVarInitializerOrDestroyer()) {
limit = Limit::NeverPublic;
}
// Stored property initializers get the linkage of their containing type.
if (isStoredPropertyInitializer()) {
// Three cases:
//
// 1) Type is formally @_fixed_layout. Root initializers can be declared
// @inlinable. The property initializer must only reference
// public symbols, and is serialized, so we give it PublicNonABI linkage.
//
// 2) Type is not formally @_fixed_layout and the module is not resilient.
// Root initializers can be declared @inlinable. This is the annoying
// case. We give the initializer public linkage if the type is public.
//
// 3) Type is resilient. The property initializer is never public because
// root initializers cannot be @inlinable.
//
// FIXME: Get rid of case 2 somehow.
if (isSerialized())
return maybeAddExternal(SILLinkage::PublicNonABI);
d = cast<NominalTypeDecl>(d->getDeclContext());
// FIXME: This should always be true.
if (d->getDeclContext()->getParentModule()->getResilienceStrategy() ==
ResilienceStrategy::Resilient)
limit = Limit::NeverPublic;
}
// The global addressor is never public for resilient globals.
if (kind == Kind::GlobalAccessor) {
if (cast<VarDecl>(d)->isResilient()) {
limit = Limit::NeverPublic;
}
}
// Forced-static-dispatch functions are created on-demand and have
// at best shared linkage.
if (auto fn = dyn_cast<FuncDecl>(d)) {
if (fn->hasForcedStaticDispatch()) {
limit = Limit::OnDemand;
}
}
auto effectiveAccess = d->getEffectiveAccess();
// Private setter implementations for an internal storage declaration should
// be internal as well, so that a dynamically-writable
// keypath can be formed from other files.
if (auto accessor = dyn_cast<AccessorDecl>(d)) {
if (accessor->isSetter()
&& accessor->getStorage()->getEffectiveAccess() == AccessLevel::Internal)
effectiveAccess = AccessLevel::Internal;
}
switch (effectiveAccess) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
return maybeAddExternal(SILLinkage::Private);
case AccessLevel::Internal:
if (limit == Limit::OnDemand)
return SILLinkage::Shared;
return maybeAddExternal(SILLinkage::Hidden);
case AccessLevel::Public:
case AccessLevel::Open:
if (limit == Limit::OnDemand)
return SILLinkage::Shared;
if (limit == Limit::NeverPublic)
return maybeAddExternal(SILLinkage::Hidden);
return maybeAddExternal(SILLinkage::Public);
}
}
SILDeclRef SILDeclRef::getDefaultArgGenerator(Loc loc,
unsigned defaultArgIndex) {
SILDeclRef result;
result.loc = loc;
result.kind = Kind::DefaultArgGenerator;
result.defaultArgIndex = defaultArgIndex;
return result;
}
bool SILDeclRef::hasClosureExpr() const {
return loc.is<AbstractClosureExpr *>()
&& isa<ClosureExpr>(getAbstractClosureExpr());
}
bool SILDeclRef::hasAutoClosureExpr() const {
return loc.is<AbstractClosureExpr *>()
&& isa<AutoClosureExpr>(getAbstractClosureExpr());
}
bool SILDeclRef::hasFuncDecl() const {
return loc.is<ValueDecl *>() && isa<FuncDecl>(getDecl());
}
ClosureExpr *SILDeclRef::getClosureExpr() const {
return dyn_cast<ClosureExpr>(getAbstractClosureExpr());
}
AutoClosureExpr *SILDeclRef::getAutoClosureExpr() const {
return dyn_cast<AutoClosureExpr>(getAbstractClosureExpr());
}
FuncDecl *SILDeclRef::getFuncDecl() const {
return dyn_cast<FuncDecl>(getDecl());
}
bool SILDeclRef::isSetter() const {
if (!hasDecl())
return false;
if (auto accessor = dyn_cast<AccessorDecl>(getDecl()))
return accessor->isSetter();
return false;
}
AbstractFunctionDecl *SILDeclRef::getAbstractFunctionDecl() const {
return dyn_cast<AbstractFunctionDecl>(getDecl());
}
/// \brief True if the function should be treated as transparent.
bool SILDeclRef::isTransparent() const {
if (isEnumElement())
return true;
if (isStoredPropertyInitializer())
return true;
if (hasAutoClosureExpr())
return true;
if (hasDecl()) {
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(getDecl()))
return AFD->isTransparent();
if (auto *ASD = dyn_cast<AbstractStorageDecl>(getDecl()))
return ASD->isTransparent();
}
return false;
}
/// \brief True if the function should have its body serialized.
IsSerialized_t SILDeclRef::isSerialized() const {
DeclContext *dc;
if (auto closure = getAbstractClosureExpr())
dc = closure->getLocalContext();
else {
auto *d = getDecl();
// Default argument generators are serialized if the function was
// type-checked in Swift 4 mode.
if (kind == SILDeclRef::Kind::DefaultArgGenerator) {
auto *afd = cast<AbstractFunctionDecl>(d);
switch (afd->getDefaultArgumentResilienceExpansion()) {
case ResilienceExpansion::Minimal:
return IsSerialized;
case ResilienceExpansion::Maximal:
return IsNotSerialized;
}
}
// 'read' and 'modify' accessors synthesized on-demand are serialized if
// visible outside the module.
if (auto fn = dyn_cast<FuncDecl>(d))
if (!isClangImported() &&
fn->hasForcedStaticDispatch() &&
fn->getEffectiveAccess() >= AccessLevel::Public)
return IsSerialized;
dc = getDecl()->getInnermostDeclContext();
// Enum element constructors are serialized if the enum is
// @usableFromInline or public.
if (isEnumElement())
if (d->getEffectiveAccess() >= AccessLevel::Public)
return IsSerialized;
// Currying thunks are serialized if referenced from an inlinable
// context -- Sema's semantic checks ensure the serialization of
// such a thunk is valid, since it must in turn reference a public
// symbol, or dispatch via class_method or witness_method.
if (isCurried)
if (d->getEffectiveAccess() >= AccessLevel::Public)
return IsSerializable;
if (isForeignToNativeThunk())
return IsSerializable;
// The allocating entry point for designated initializers are serialized
// if the class is @usableFromInline or public.
if (kind == SILDeclRef::Kind::Allocator) {
auto *ctor = cast<ConstructorDecl>(d);
if (ctor->isDesignatedInit() &&
ctor->getDeclContext()->getSelfClassDecl()) {
if (ctor->getEffectiveAccess() >= AccessLevel::Public &&
!ctor->hasClangNode())
return IsSerialized;
}
}
// Stored property initializers are inlinable if the type is explicitly
// marked as @_fixed_layout.
if (isStoredPropertyInitializer()) {
auto *nominal = cast<NominalTypeDecl>(d->getDeclContext());
auto scope =
nominal->getFormalAccessScope(/*useDC=*/nullptr,
/*treatUsableFromInlineAsPublic=*/true);
if (!scope.isPublic())
return IsNotSerialized;
if (nominal->isFormallyResilient())
return IsNotSerialized;
return IsSerialized;
}
}
// Declarations imported from Clang modules are serialized if
// referenced from an inlinable context.
if (isClangImported())
return IsSerializable;
// Otherwise, ask the AST if we're inside an @inlinable context.
if (dc->getResilienceExpansion() == ResilienceExpansion::Minimal)
return IsSerialized;
return IsNotSerialized;
}
/// \brief True if the function has noinline attribute.
bool SILDeclRef::isNoinline() const {
if (!hasDecl())
return false;
if (auto InlineA = getDecl()->getAttrs().getAttribute<InlineAttr>())
if (InlineA->getKind() == InlineKind::Never)
return true;
return false;
}
/// \brief True if the function has noinline attribute.
bool SILDeclRef::isAlwaysInline() const {
if (!hasDecl())
return false;
if (auto InlineA = getDecl()->getAttrs().getAttribute<InlineAttr>())
if (InlineA->getKind() == InlineKind::Always)
return true;
return false;
}
bool SILDeclRef::hasEffectsAttribute() const {
if (!hasDecl())
return false;
return getDecl()->getAttrs().hasAttribute<EffectsAttr>();
}
EffectsKind SILDeclRef::getEffectsAttribute() const {
assert(hasEffectsAttribute());
EffectsAttr *MA = getDecl()->getAttrs().getAttribute<EffectsAttr>();
return MA->getKind();
}
bool SILDeclRef::isForeignToNativeThunk() const {
// Non-decl entry points are never natively foreign, so they would never
// have a foreign-to-native thunk.
if (!hasDecl())
return false;
if (requiresForeignToNativeThunk(getDecl()))
return !isForeign;
// ObjC initializing constructors and factories are foreign.
// We emit a special native allocating constructor though.
if (isa<ConstructorDecl>(getDecl())
&& (kind == Kind::Initializer
|| cast<ConstructorDecl>(getDecl())->isFactoryInit())
&& getDecl()->hasClangNode())
return !isForeign;
return false;
}
bool SILDeclRef::isNativeToForeignThunk() const {
// We can have native-to-foreign thunks over closures.
if (!hasDecl())
return isForeign;
// We can have native-to-foreign thunks over global or local native functions.
// TODO: Static functions too.
if (auto func = dyn_cast<FuncDecl>(getDecl())) {
if (!func->getDeclContext()->isTypeContext()
&& !func->hasClangNode())
return isForeign;
}
return false;
}
/// Use the Clang importer to mangle a Clang declaration.
static void mangleClangDecl(raw_ostream &buffer,
const clang::NamedDecl *clangDecl,
ASTContext &ctx) {
auto *importer = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
importer->getMangledName(buffer, clangDecl);
}
std::string SILDeclRef::mangle(ManglingKind MKind) const {
using namespace Mangle;
ASTMangler mangler;
// As a special case, Clang functions and globals don't get mangled at all.
if (hasDecl()) {
if (auto clangDecl = getDecl()->getClangDecl()) {
if (!isForeignToNativeThunk() && !isNativeToForeignThunk()
&& !isCurried) {
if (auto namedClangDecl = dyn_cast<clang::DeclaratorDecl>(clangDecl)) {
if (auto asmLabel = namedClangDecl->getAttr<clang::AsmLabelAttr>()) {
std::string s(1, '\01');
s += asmLabel->getLabel();
return s;
} else if (namedClangDecl->hasAttr<clang::OverloadableAttr>()) {
std::string storage;
llvm::raw_string_ostream SS(storage);
// FIXME: When we can import C++, use Clang's mangler all the time.
mangleClangDecl(SS, namedClangDecl, getDecl()->getASTContext());
return SS.str();
}
return namedClangDecl->getName();
}
}
}
}
ASTMangler::SymbolKind SKind = ASTMangler::SymbolKind::Default;
switch (MKind) {
case SILDeclRef::ManglingKind::Default:
if (isForeign) {
SKind = ASTMangler::SymbolKind::SwiftAsObjCThunk;
} else if (isDirectReference) {
SKind = ASTMangler::SymbolKind::DirectMethodReferenceThunk;
} else if (isForeignToNativeThunk()) {
SKind = ASTMangler::SymbolKind::ObjCAsSwiftThunk;
}
break;
case SILDeclRef::ManglingKind::DynamicThunk:
SKind = ASTMangler::SymbolKind::DynamicThunk;
break;
}
switch (kind) {
case SILDeclRef::Kind::Func:
if (!hasDecl())
return mangler.mangleClosureEntity(getAbstractClosureExpr(), SKind);
// As a special case, functions can have manually mangled names.
// Use the SILGen name only for the original non-thunked, non-curried entry
// point.
if (auto NameA = getDecl()->getAttrs().getAttribute<SILGenNameAttr>())
if (!NameA->Name.empty() &&
!isForeignToNativeThunk() && !isNativeToForeignThunk()
&& !isCurried) {
return NameA->Name;
}
// Use a given cdecl name for native-to-foreign thunks.
if (auto CDeclA = getDecl()->getAttrs().getAttribute<CDeclAttr>())
if (isNativeToForeignThunk()) {
return CDeclA->Name;
}
// Otherwise, fall through into the 'other decl' case.
LLVM_FALLTHROUGH;
case SILDeclRef::Kind::EnumElement:
return mangler.mangleEntity(getDecl(), isCurried, SKind);
case SILDeclRef::Kind::Deallocator:
assert(!isCurried);
return mangler.mangleDestructorEntity(cast<DestructorDecl>(getDecl()),
/*isDeallocating*/ true,
SKind);
case SILDeclRef::Kind::Destroyer:
assert(!isCurried);
return mangler.mangleDestructorEntity(cast<DestructorDecl>(getDecl()),
/*isDeallocating*/ false,
SKind);
case SILDeclRef::Kind::Allocator:
return mangler.mangleConstructorEntity(cast<ConstructorDecl>(getDecl()),
/*allocating*/ true,
isCurried,
SKind);
case SILDeclRef::Kind::Initializer:
return mangler.mangleConstructorEntity(cast<ConstructorDecl>(getDecl()),
/*allocating*/ false,
isCurried,
SKind);
case SILDeclRef::Kind::IVarInitializer:
case SILDeclRef::Kind::IVarDestroyer:
assert(!isCurried);
return mangler.mangleIVarInitDestroyEntity(cast<ClassDecl>(getDecl()),
kind == SILDeclRef::Kind::IVarDestroyer,
SKind);
case SILDeclRef::Kind::GlobalAccessor:
assert(!isCurried);
return mangler.mangleAccessorEntity(AccessorKind::MutableAddress,
AddressorKind::Unsafe,
cast<AbstractStorageDecl>(getDecl()),
/*isStatic*/ false,
SKind);
case SILDeclRef::Kind::DefaultArgGenerator:
assert(!isCurried);
return mangler.mangleDefaultArgumentEntity(
cast<AbstractFunctionDecl>(getDecl()),
defaultArgIndex,
SKind);
case SILDeclRef::Kind::StoredPropertyInitializer:
assert(!isCurried);
return mangler.mangleInitializerEntity(cast<VarDecl>(getDecl()), SKind);
}
llvm_unreachable("bad entity kind!");
}
bool SILDeclRef::requiresNewVTableEntry() const {
if (cast<AbstractFunctionDecl>(getDecl())->needsNewVTableEntry())
return true;
if (kind == SILDeclRef::Kind::Allocator) {
auto *cd = cast<ConstructorDecl>(getDecl());
if (cd->isRequired()) {
auto *baseCD = cd->getOverriddenDecl();
if(!baseCD ||
!baseCD->isRequired() ||
baseCD->hasClangNode())
return true;
}
}
return false;
}
bool SILDeclRef::requiresNewWitnessTableEntry() const {
return requiresNewWitnessTableEntry(cast<AbstractFunctionDecl>(getDecl()));
}
bool SILDeclRef::requiresNewWitnessTableEntry(AbstractFunctionDecl *func) {
return func->getOverriddenDecls().empty();
}
SILDeclRef SILDeclRef::getOverridden() const {
if (!hasDecl())
return SILDeclRef();
auto overridden = getDecl()->getOverriddenDecl();
if (!overridden)
return SILDeclRef();
return SILDeclRef(overridden, kind, isCurried);
}
SILDeclRef SILDeclRef::getNextOverriddenVTableEntry() const {
if (auto overridden = getOverridden()) {
// If we overrode a foreign decl, a dynamic method, this is an
// accessor for a property that overrides an ObjC decl, or if it is an
// @NSManaged property, then it won't be in the vtable.
if (overridden.getDecl()->hasClangNode())
return SILDeclRef();
// If we overrode a non-required initializer, there won't be a vtable
// slot for the allocator.
if (overridden.kind == SILDeclRef::Kind::Allocator) {
if (!cast<ConstructorDecl>(overridden.getDecl())->isRequired())
return SILDeclRef();
} else if (overridden.getDecl()->isDynamic()) {
return SILDeclRef();
}
if (auto *accessor = dyn_cast<AccessorDecl>(overridden.getDecl())) {
auto *asd = accessor->getStorage();
if (asd->hasClangNode())
return SILDeclRef();
if (asd->isDynamic())
return SILDeclRef();
}
// If we overrode a decl from an extension, it won't be in a vtable
// either. This can occur for extensions to ObjC classes.
if (isa<ExtensionDecl>(overridden.getDecl()->getDeclContext()))
return SILDeclRef();
return overridden;
}
return SILDeclRef();
}
SILDeclRef SILDeclRef::getOverriddenWitnessTableEntry() const {
auto bestOverridden =
getOverriddenWitnessTableEntry(cast<AbstractFunctionDecl>(getDecl()));
return SILDeclRef(bestOverridden, kind, isCurried);
}
AbstractFunctionDecl *SILDeclRef::getOverriddenWitnessTableEntry(
AbstractFunctionDecl *func) {
if (!isa<ProtocolDecl>(func->getDeclContext()))
return func;
AbstractFunctionDecl *bestOverridden = nullptr;
SmallVector<AbstractFunctionDecl *, 4> stack;
SmallPtrSet<AbstractFunctionDecl *, 4> visited;
stack.push_back(func);
visited.insert(func);
while (!stack.empty()) {
auto current = stack.back();
stack.pop_back();
auto overriddenDecls = current->getOverriddenDecls();
if (overriddenDecls.empty()) {
// This entry introduced a witness table entry. Determine whether it is
// better than the best entry we've seen thus far.
if (!bestOverridden ||
ProtocolDecl::compare(
cast<ProtocolDecl>(current->getDeclContext()),
cast<ProtocolDecl>(bestOverridden->getDeclContext()))
< 0) {
bestOverridden = cast<AbstractFunctionDecl>(current);
}
continue;
}
// Add overridden declarations to the stack.
for (auto overridden : overriddenDecls) {
auto overriddenFunc = cast<AbstractFunctionDecl>(overridden);
if (visited.insert(overriddenFunc).second)
stack.push_back(overriddenFunc);
}
}
return bestOverridden;
}
SILDeclRef SILDeclRef::getOverriddenVTableEntry() const {
SILDeclRef cur = *this, next = *this;
do {
cur = next;
if (cur.requiresNewVTableEntry())
return cur;
next = cur.getNextOverriddenVTableEntry();
} while (next);
return cur;
}
SILLocation SILDeclRef::getAsRegularLocation() const {
if (hasDecl())
return RegularLocation(getDecl());
return RegularLocation(getAbstractClosureExpr());
}
SubclassScope SILDeclRef::getSubclassScope() const {
if (!hasDecl())
return SubclassScope::NotApplicable;
// If this declaration is a function which goes into a vtable, then it's
// symbol must be as visible as its class. Derived classes even have to put
// all less visible methods of the base class into their vtables.
auto *FD = dyn_cast<AbstractFunctionDecl>(getDecl());
if (!FD)
return SubclassScope::NotApplicable;
DeclContext *context = FD->getDeclContext();
// Methods from extensions don't go into vtables (yet).
if (context->isExtensionContext())
return SubclassScope::NotApplicable;
// Various forms of thunks don't either.
if (isThunk() || isForeign)
return SubclassScope::NotApplicable;
// Default arg generators only need to be visible in Swift 3.
if (isDefaultArgGenerator() && !context->getASTContext().isSwiftVersion3())
return SubclassScope::NotApplicable;
auto *classType = context->getSelfClassDecl();
if (!classType || classType->isFinal())
return SubclassScope::NotApplicable;
if (FD->isFinal())
return SubclassScope::NotApplicable;
assert(FD->getEffectiveAccess() <= classType->getEffectiveAccess() &&
"class must be as visible as its members");
switch (classType->getEffectiveAccess()) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
return SubclassScope::NotApplicable;
case AccessLevel::Internal:
case AccessLevel::Public:
return SubclassScope::Internal;
case AccessLevel::Open:
return SubclassScope::External;
}
llvm_unreachable("Unhandled access level in switch.");
}
unsigned SILDeclRef::getParameterListCount() const {
if (isCurried || !hasDecl() || kind == Kind::DefaultArgGenerator)
return 1;
auto *vd = getDecl();
if (auto *func = dyn_cast<AbstractFunctionDecl>(vd)) {
return func->hasImplicitSelfDecl() ? 2 : 1;
} else if (auto *ed = dyn_cast<EnumElementDecl>(vd)) {
return ed->hasAssociatedValues() ? 2 : 1;
} else if (isa<ClassDecl>(vd)) {
return 2;
} else if (isa<VarDecl>(vd)) {
return 1;
} else {
llvm_unreachable("Unhandled ValueDecl for SILDeclRef");
}
}