1616#include " swift/AST/AnyFunctionRef.h"
1717#include " swift/AST/Initializer.h"
1818#include " swift/AST/ParameterList.h"
19+ #include " swift/AST/SourceFile.h"
1920#include " swift/ClangImporter/ClangImporter.h"
2021#include " swift/ClangImporter/ClangModule.h"
2122#include " swift/SIL/SILLinkage.h"
@@ -170,14 +171,29 @@ SILDeclRef::SILDeclRef(SILDeclRef::Loc baseLoc,
170171}
171172
172173Optional<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 ())
175177 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 ();
179195 }
180- return AnyFunctionRef (loc. get <AbstractClosureExpr*>() );
196+ llvm_unreachable ( " Unhandled case in switch " );
181197}
182198
183199bool SILDeclRef::isThunk () const {
@@ -226,9 +242,16 @@ bool SILDeclRef::isClangGenerated(ClangNode node) {
226242}
227243
228244bool SILDeclRef::isImplicit () const {
229- if (hasDecl ())
245+ switch (getLocKind ()) {
246+ case LocKind::Decl:
230247 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" );
232255}
233256
234257SILLinkage SILDeclRef::getLinkage (ForDefinition_t forDefinition) const {
@@ -242,6 +265,10 @@ SILLinkage SILDeclRef::getLinkage(ForDefinition_t forDefinition) const {
242265 return isSerialized () ? SILLinkage::Shared : SILLinkage::Private;
243266 }
244267
268+ // The main entry-point is public.
269+ if (kind == Kind::EntryPoint)
270+ return SILLinkage::Public;
271+
245272 // Add External to the linkage (e.g. Public -> PublicExternal) if this is a
246273 // declaration not a definition.
247274 auto maybeAddExternal = [&](SILLinkage linkage) {
@@ -407,6 +434,23 @@ SILDeclRef SILDeclRef::getDefaultArgGenerator(Loc loc,
407434 return result;
408435}
409436
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+
410454bool SILDeclRef::hasClosureExpr () const {
411455 return loc.is <AbstractClosureExpr *>()
412456 && isa<ClosureExpr>(getAbstractClosureExpr ());
@@ -490,6 +534,9 @@ IsSerialized_t SILDeclRef::isSerialized() const {
490534 return IsNotSerialized;
491535 }
492536
537+ if (kind == Kind::EntryPoint)
538+ return IsNotSerialized;
539+
493540 if (isIVarInitializerOrDestroyer ())
494541 return IsNotSerialized;
495542
@@ -681,17 +728,23 @@ bool SILDeclRef::isNativeToForeignThunk() const {
681728 if (!isForeign)
682729 return false ;
683730
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 ;
687737
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:
690745 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" );
695748}
696749
697750// / Use the Clang importer to mangle a Clang declaration.
@@ -781,8 +834,8 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
781834
782835 switch (kind) {
783836 case SILDeclRef::Kind::Func:
784- if (! hasDecl ())
785- return mangler.mangleClosureEntity (getAbstractClosureExpr () , SKind);
837+ if (auto *ACE = getAbstractClosureExpr ())
838+ return mangler.mangleClosureEntity (ACE , SKind);
786839
787840 // As a special case, functions can have manually mangled names.
788841 // Use the SILGen name only for the original non-thunked, non-curried entry
@@ -853,6 +906,10 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
853906 case SILDeclRef::Kind::PropertyWrapperInitFromProjectedValue:
854907 return mangler.mangleInitFromProjectedValueEntity (cast<VarDecl>(getDecl ()),
855908 SKind);
909+
910+ case SILDeclRef::Kind::EntryPoint: {
911+ return getASTContext ().getEntryPointFunctionName ();
912+ }
856913 }
857914
858915 llvm_unreachable (" bad entity kind!" );
@@ -1056,9 +1113,15 @@ SILDeclRef SILDeclRef::getOverriddenVTableEntry() const {
10561113}
10571114
10581115SILLocation SILDeclRef::getAsRegularLocation () const {
1059- if (hasDecl ())
1116+ switch (getLocKind ()) {
1117+ case LocKind::Decl:
10601118 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" );
10621125}
10631126
10641127SubclassScope SILDeclRef::getSubclassScope () const {
@@ -1165,7 +1228,12 @@ SubclassScope SILDeclRef::getSubclassScope() const {
11651228}
11661229
11671230unsigned 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)
11691237 return 1 ;
11701238
11711239 auto *vd = getDecl ();
0 commit comments