16
16
#include " swift/AST/AnyFunctionRef.h"
17
17
#include " swift/AST/Initializer.h"
18
18
#include " swift/AST/ParameterList.h"
19
+ #include " swift/AST/SourceFile.h"
19
20
#include " swift/ClangImporter/ClangImporter.h"
20
21
#include " swift/ClangImporter/ClangModule.h"
21
22
#include " swift/SIL/SILLinkage.h"
@@ -170,14 +171,29 @@ SILDeclRef::SILDeclRef(SILDeclRef::Loc baseLoc,
170
171
}
171
172
172
173
Optional<AnyFunctionRef> SILDeclRef::getAnyFunctionRef () const {
173
- if (auto vd = loc.dyn_cast <ValueDecl*>()) {
174
- if (auto afd = dyn_cast<AbstractFunctionDecl>(vd)) {
174
+ switch (getLocKind ()) {
175
+ case LocKind::Decl:
176
+ if (auto *afd = getAbstractFunctionDecl ())
175
177
return AnyFunctionRef (afd);
176
- } else {
177
- return None;
178
- }
178
+ return None;
179
+ case LocKind::Closure:
180
+ return AnyFunctionRef (getAbstractClosureExpr ());
181
+ case LocKind::File:
182
+ return None;
183
+ }
184
+ llvm_unreachable (" Unhandled case in switch" );
185
+ }
186
+
187
+ ASTContext &SILDeclRef::getASTContext () const {
188
+ switch (getLocKind ()) {
189
+ case LocKind::Decl:
190
+ return getDecl ()->getASTContext ();
191
+ case LocKind::Closure:
192
+ return getAbstractClosureExpr ()->getASTContext ();
193
+ case LocKind::File:
194
+ return getFileUnit ()->getASTContext ();
179
195
}
180
- return AnyFunctionRef (loc. get <AbstractClosureExpr*>() );
196
+ llvm_unreachable ( " Unhandled case in switch " );
181
197
}
182
198
183
199
bool SILDeclRef::isThunk () const {
@@ -226,9 +242,16 @@ bool SILDeclRef::isClangGenerated(ClangNode node) {
226
242
}
227
243
228
244
bool SILDeclRef::isImplicit () const {
229
- if (hasDecl ())
245
+ switch (getLocKind ()) {
246
+ case LocKind::Decl:
230
247
return getDecl ()->isImplicit ();
231
- return getAbstractClosureExpr ()->isImplicit ();
248
+ case LocKind::Closure:
249
+ return getAbstractClosureExpr ()->isImplicit ();
250
+ case LocKind::File:
251
+ // Files are currently never considered implicit.
252
+ return false ;
253
+ }
254
+ llvm_unreachable (" Unhandled case in switch" );
232
255
}
233
256
234
257
SILLinkage SILDeclRef::getLinkage (ForDefinition_t forDefinition) const {
@@ -242,6 +265,10 @@ SILLinkage SILDeclRef::getLinkage(ForDefinition_t forDefinition) const {
242
265
return isSerialized () ? SILLinkage::Shared : SILLinkage::Private;
243
266
}
244
267
268
+ // The main entry-point is public.
269
+ if (kind == Kind::EntryPoint)
270
+ return SILLinkage::Public;
271
+
245
272
// Add External to the linkage (e.g. Public -> PublicExternal) if this is a
246
273
// declaration not a definition.
247
274
auto maybeAddExternal = [&](SILLinkage linkage) {
@@ -407,6 +434,23 @@ SILDeclRef SILDeclRef::getDefaultArgGenerator(Loc loc,
407
434
return result;
408
435
}
409
436
437
+ SILDeclRef SILDeclRef::getMainDeclEntryPoint (ValueDecl *decl) {
438
+ auto *file = cast<FileUnit>(decl->getDeclContext ()->getModuleScopeContext ());
439
+ assert (file->getMainDecl () == decl);
440
+ SILDeclRef result;
441
+ result.loc = decl;
442
+ result.kind = Kind::EntryPoint;
443
+ return result;
444
+ }
445
+
446
+ SILDeclRef SILDeclRef::getMainFileEntryPoint (FileUnit *file) {
447
+ assert (file->hasEntryPoint () && !file->getMainDecl ());
448
+ SILDeclRef result;
449
+ result.loc = file;
450
+ result.kind = Kind::EntryPoint;
451
+ return result;
452
+ }
453
+
410
454
bool SILDeclRef::hasClosureExpr () const {
411
455
return loc.is <AbstractClosureExpr *>()
412
456
&& isa<ClosureExpr>(getAbstractClosureExpr ());
@@ -490,6 +534,9 @@ IsSerialized_t SILDeclRef::isSerialized() const {
490
534
return IsNotSerialized;
491
535
}
492
536
537
+ if (kind == Kind::EntryPoint)
538
+ return IsNotSerialized;
539
+
493
540
if (isIVarInitializerOrDestroyer ())
494
541
return IsNotSerialized;
495
542
@@ -681,17 +728,23 @@ bool SILDeclRef::isNativeToForeignThunk() const {
681
728
if (!isForeign)
682
729
return false ;
683
730
684
- // We can have native-to-foreign thunks over closures.
685
- if (!hasDecl ())
686
- return true ;
731
+ switch (getLocKind ()) {
732
+ case LocKind::Decl:
733
+ // A decl with a clang node doesn't have a native entry-point to forward
734
+ // onto.
735
+ if (getDecl ()->hasClangNode ())
736
+ return false ;
687
737
688
- // A decl with a clang node doesn't have a native entry-point to forward onto.
689
- if (getDecl ()->hasClangNode ())
738
+ // Only certain kinds of SILDeclRef can expose native-to-foreign thunks.
739
+ return kind == Kind::Func || kind == Kind::Initializer ||
740
+ kind == Kind::Deallocator;
741
+ case LocKind::Closure:
742
+ // We can have native-to-foreign thunks over closures.
743
+ return true ;
744
+ case LocKind::File:
690
745
return false ;
691
-
692
- // Only certain kinds of SILDeclRef can expose native-to-foreign thunks.
693
- return kind == Kind::Func || kind == Kind::Initializer ||
694
- kind == Kind::Deallocator;
746
+ }
747
+ llvm_unreachable (" Unhandled case in switch" );
695
748
}
696
749
697
750
// / Use the Clang importer to mangle a Clang declaration.
@@ -781,8 +834,8 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
781
834
782
835
switch (kind) {
783
836
case SILDeclRef::Kind::Func:
784
- if (! hasDecl ())
785
- return mangler.mangleClosureEntity (getAbstractClosureExpr () , SKind);
837
+ if (auto *ACE = getAbstractClosureExpr ())
838
+ return mangler.mangleClosureEntity (ACE , SKind);
786
839
787
840
// As a special case, functions can have manually mangled names.
788
841
// Use the SILGen name only for the original non-thunked, non-curried entry
@@ -853,6 +906,10 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
853
906
case SILDeclRef::Kind::PropertyWrapperInitFromProjectedValue:
854
907
return mangler.mangleInitFromProjectedValueEntity (cast<VarDecl>(getDecl ()),
855
908
SKind);
909
+
910
+ case SILDeclRef::Kind::EntryPoint: {
911
+ return getASTContext ().getEntryPointFunctionName ();
912
+ }
856
913
}
857
914
858
915
llvm_unreachable (" bad entity kind!" );
@@ -1056,9 +1113,15 @@ SILDeclRef SILDeclRef::getOverriddenVTableEntry() const {
1056
1113
}
1057
1114
1058
1115
SILLocation SILDeclRef::getAsRegularLocation () const {
1059
- if (hasDecl ())
1116
+ switch (getLocKind ()) {
1117
+ case LocKind::Decl:
1060
1118
return RegularLocation (getDecl ());
1061
- return RegularLocation (getAbstractClosureExpr ());
1119
+ case LocKind::Closure:
1120
+ return RegularLocation (getAbstractClosureExpr ());
1121
+ case LocKind::File:
1122
+ return RegularLocation::getModuleLocation ();
1123
+ }
1124
+ llvm_unreachable (" Unhandled case in switch" );
1062
1125
}
1063
1126
1064
1127
SubclassScope SILDeclRef::getSubclassScope () const {
@@ -1165,7 +1228,12 @@ SubclassScope SILDeclRef::getSubclassScope() const {
1165
1228
}
1166
1229
1167
1230
unsigned SILDeclRef::getParameterListCount () const {
1168
- if (!hasDecl () || kind == Kind::DefaultArgGenerator)
1231
+ // Only decls can introduce currying.
1232
+ if (!hasDecl ())
1233
+ return 1 ;
1234
+
1235
+ // Always uncurried even if the underlying function is curried.
1236
+ if (kind == Kind::DefaultArgGenerator || kind == Kind::EntryPoint)
1169
1237
return 1 ;
1170
1238
1171
1239
auto *vd = getDecl ();
0 commit comments