Skip to content

Commit 20930c3

Browse files
Merge pull request #35595 from nate-chandler/other-main
[SILGen] Enable alternative entry point name.
2 parents 80322a5 + e21550a commit 20930c3

File tree

14 files changed

+61
-22
lines changed

14 files changed

+61
-22
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,10 @@ class ASTContext final {
11541154
/// standard library's String implementation.
11551155
bool isASCIIString(StringRef s) const;
11561156

1157+
/// Retrieve the name of to be used for the entry point, either main or an
1158+
/// alternative specified via the -entry-point-function-name frontend flag.
1159+
std::string getEntryPointFunctionName() const;
1160+
11571161
private:
11581162
friend Decl;
11591163
Optional<RawComment> getRawComment(const Decl *D);

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ namespace swift {
8282
/// The target variant SDK version, if known.
8383
Optional<llvm::VersionTuple> VariantSDKVersion;
8484

85+
/// The alternate name to use for the entry point instead of main.
86+
std::string entryPointFunctionName = "main";
87+
8588
///
8689
/// Language features
8790
///

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,9 @@ def disable_interface_lockfile : Flag<["-"], "disable-interface-lock">,
733733
def bridging_header_directory_for_print: Separate<["-"], "bridging-header-directory-for-print">, MetaVarName<"<path>">,
734734
HelpText<"Directory for bridging header to be printed in compatibility header">;
735735

736+
def entry_point_function_name: Separate<["-"], "entry-point-function-name">, MetaVarName<"<string>">,
737+
HelpText<"Name of the entry point function">;
738+
736739
def target_sdk_version : Separate<["-"], "target-sdk-version">,
737740
HelpText<"The version of target SDK used for compilation">;
738741

include/swift/SIL/SILFunction.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
#include "swift/SIL/SILLinkage.h"
2828
#include "swift/SIL/SILPrintContext.h"
2929

30-
/// The symbol name used for the program entry point function.
31-
#define SWIFT_ENTRY_POINT_FUNCTION "main"
32-
3330
namespace swift {
3431

3532
class ASTContext;

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4841,6 +4841,10 @@ ASTContext::SILTransformCtors ASTContext::getIRGenSILTransforms() const {
48414841
return passes;
48424842
}
48434843

4844+
std::string ASTContext::getEntryPointFunctionName() const {
4845+
return LangOpts.entryPointFunctionName;
4846+
}
4847+
48444848
SILLayout *SILLayout::get(ASTContext &C,
48454849
CanGenericSignature Generics,
48464850
ArrayRef<SILField> Fields) {

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,10 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
661661
}
662662
}
663663

664+
if (const Arg *A = Args.getLastArg(OPT_entry_point_function_name)) {
665+
Opts.entryPointFunctionName = A->getValue();
666+
}
667+
664668
if (FrontendOpts.RequestedAction == FrontendOptions::ActionType::EmitSyntax) {
665669
Opts.BuildSyntaxTree = true;
666670
Opts.VerifySyntaxTree = true;

lib/IRGen/GenDecl.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,9 @@ void IRGenModule::emitRuntimeRegistration() {
590590
return;
591591

592592
// Find the entry point.
593-
SILFunction *EntryPoint =
594-
getSILModule().lookUpFunction(SWIFT_ENTRY_POINT_FUNCTION);
595-
593+
SILFunction *EntryPoint = getSILModule().lookUpFunction(
594+
getSILModule().getASTContext().getEntryPointFunctionName());
595+
596596
// If we're debugging (and not in the REPL), we don't have a
597597
// main. Find a function marked with the LLDBDebuggerFunction
598598
// attribute instead.
@@ -1894,7 +1894,8 @@ static std::string getEntryPointSection(IRGenModule &IGM) {
18941894

18951895
void IRGenerator::emitEntryPointInfo() {
18961896
SILFunction *entrypoint = nullptr;
1897-
if (!(entrypoint = SIL.lookUpFunction(SWIFT_ENTRY_POINT_FUNCTION))) {
1897+
if (!(entrypoint = SIL.lookUpFunction(
1898+
SIL.getASTContext().getEntryPointFunctionName()))) {
18981899
return;
18991900
}
19001901
auto &IGM = *getGenModule(entrypoint);

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
132132
llvm::DIFile *MainFile = nullptr;
133133
/// The current module.
134134
llvm::DIModule *MainModule = nullptr;
135-
/// Scope of SWIFT_ENTRY_POINT_FUNCTION.
135+
/// Scope of entry point function (main by default).
136136
llvm::DIScope *EntryPointFn = nullptr;
137137
/// The artificial type decls for named archetypes.
138138
llvm::StringMap<TypeAliasDecl *> MetadataTypeDeclCache;
@@ -2084,7 +2084,8 @@ llvm::DIScope *IRGenDebugInfoImpl::getEntryPointFn() {
20842084
// Lazily create EntryPointFn.
20852085
if (!EntryPointFn) {
20862086
EntryPointFn = DBuilder.createReplaceableCompositeType(
2087-
llvm::dwarf::DW_TAG_subroutine_type, SWIFT_ENTRY_POINT_FUNCTION,
2087+
llvm::dwarf::DW_TAG_subroutine_type,
2088+
IGM.getSILModule().getASTContext().getEntryPointFunctionName(),
20882089
MainFile, MainFile, 0);
20892090
}
20902091
return EntryPointFn;
@@ -2219,7 +2220,8 @@ IRGenDebugInfoImpl::emitFunction(const SILDebugScope *DS, llvm::Function *Fn,
22192220
Scope = getOrCreateContext(SILFn->getDeclContext()->getParent());
22202221

22212222
// We know that main always comes from MainFile.
2222-
if (LinkageName == SWIFT_ENTRY_POINT_FUNCTION) {
2223+
if (LinkageName ==
2224+
IGM.getSILModule().getASTContext().getEntryPointFunctionName()) {
22232225
File = MainFile;
22242226
Line = 1;
22252227
Name = LinkageName;
@@ -2242,7 +2244,9 @@ IRGenDebugInfoImpl::emitFunction(const SILDebugScope *DS, llvm::Function *Fn,
22422244
// have a Swift name, does appear prominently in the source code.
22432245
// ObjC thunks should also not show up in the linetable, because we
22442246
// never want to set a breakpoint there.
2245-
if ((Name.empty() && LinkageName != SWIFT_ENTRY_POINT_FUNCTION &&
2247+
if ((Name.empty() &&
2248+
LinkageName !=
2249+
IGM.getSILModule().getASTContext().getEntryPointFunctionName() &&
22462250
!isExplicitClosure(SILFn)) ||
22472251
(Rep == SILFunctionTypeRepresentation::ObjCMethod) ||
22482252
isAllocatingConstructor(Rep, DeclCtx)) {
@@ -2279,7 +2283,8 @@ IRGenDebugInfoImpl::emitFunction(const SILDebugScope *DS, llvm::Function *Fn,
22792283
Fn->setSubprogram(SP);
22802284

22812285
// RAUW the entry point function forward declaration with the real thing.
2282-
if (LinkageName == SWIFT_ENTRY_POINT_FUNCTION) {
2286+
if (LinkageName ==
2287+
IGM.getSILModule().getASTContext().getEntryPointFunctionName()) {
22832288
if (EntryPointFn) {
22842289
assert(EntryPointFn->isTemporary() &&
22852290
"more than one entry point function");

lib/IRGen/IRGenDebugInfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ class IRGenDebugInfo {
8989
/// artificial inline location pointing to the user's instruction.
9090
void setInlinedTrapLocation(IRBuilder &Builder, const SILDebugScope *Scope);
9191

92-
/// Set the location for SWIFT_ENTRY_POINT_FUNCTION.
92+
/// Set the location for entry point function (main by default).
9393
void setEntryPointLoc(IRBuilder &Builder);
9494

95-
/// Return the scope for SWIFT_ENTRY_POINT_FUNCTION.
95+
/// Return the scope for the entry point function (main by default).
9696
llvm::DIScope *getEntryPointFn();
9797

9898
/// Translate a SILDebugScope into an llvm::DIDescriptor.

lib/IRGen/TypeLayoutVerifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ void IRGenModule::emitTypeVerifier() {
291291
return;
292292

293293
// Find the entry point.
294-
SILFunction *EntryPoint =
295-
getSILModule().lookUpFunction(SWIFT_ENTRY_POINT_FUNCTION);
294+
SILFunction *EntryPoint = getSILModule().lookUpFunction(
295+
getSILModule().getASTContext().getEntryPointFunctionName());
296296

297297
if (!EntryPoint)
298298
return;

lib/SILGen/SILGen.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,10 @@ SILFunction *SILGenModule::emitTopLevelFunction(SILLocation Loc) {
569569
SubstitutionMap(), SubstitutionMap(),
570570
C);
571571

572+
auto name = getASTContext().getEntryPointFunctionName();
572573
SILGenFunctionBuilder builder(*this);
573574
return builder.createFunction(
574-
SILLinkage::Public, SWIFT_ENTRY_POINT_FUNCTION, topLevelType, nullptr,
575+
SILLinkage::Public, name, topLevelType, nullptr,
575576
Loc, IsBare, IsNotTransparent, IsNotSerialized, IsNotDynamic,
576577
ProfileCounter(), IsNotThunk, SubclassScope::NotApplicable);
577578
}
@@ -1817,8 +1818,9 @@ class SourceFileScope {
18171818
// If this is the script-mode file for the module, create a toplevel.
18181819
if (sf->isScriptMode()) {
18191820
assert(!sgm.TopLevelSGF && "already emitted toplevel?!");
1820-
assert(!sgm.M.lookUpFunction(SWIFT_ENTRY_POINT_FUNCTION)
1821-
&& "already emitted toplevel?!");
1821+
assert(!sgm.M.lookUpFunction(
1822+
sgm.getASTContext().getEntryPointFunctionName()) &&
1823+
"already emitted toplevel?!");
18221824

18231825
RegularLocation TopLevelLoc = RegularLocation::getModuleLocation();
18241826
SILFunction *toplevel = sgm.emitTopLevelFunction(TopLevelLoc);
@@ -1940,8 +1942,9 @@ class SourceFileScope {
19401942
// If the source file contains an artificial main, emit the implicit
19411943
// toplevel code.
19421944
if (auto mainDecl = sf->getMainDecl()) {
1943-
assert(!sgm.M.lookUpFunction(SWIFT_ENTRY_POINT_FUNCTION)
1944-
&& "already emitted toplevel before main class?!");
1945+
assert(!sgm.M.lookUpFunction(
1946+
sgm.getASTContext().getEntryPointFunctionName()) &&
1947+
"already emitted toplevel before main class?!");
19451948

19461949
RegularLocation TopLevelLoc = RegularLocation::getModuleLocation();
19471950
SILFunction *toplevel = sgm.emitTopLevelFunction(TopLevelLoc);

lib/TBDGen/TBDGenVisitor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ class TBDGenVisitor : public ASTVisitor<TBDGenVisitor> {
165165
// TBD for, and not for any statically linked libraries.
166166
// FIXME: We should have a SymbolSource for main.
167167
if (file->hasEntryPoint() && file->getParentModule() == SwiftModule)
168-
addSymbol("main", SymbolSource::forUnknown());
168+
addSymbol(SwiftModule->getASTContext().getEntryPointFunctionName(),
169+
SymbolSource::forUnknown());
169170
}
170171

171172
/// Adds the global symbols associated with the first file.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern int foobar(int argc, char *argv[]);
2+
3+
int main(int argc, char *argv[]) {
4+
return foobar(argc, argv);
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-clang -c --language=c %S/Inputs/forward-to-foobar.c -o %t/forward.o
3+
// RUN: %target-build-swift %s %t/forward.o -Xfrontend -entry-point-function-name -Xfrontend foobar -o %t/main
4+
// RUN: %target-run %t/main | %FileCheck %s
5+
6+
// CHECK: howdy from foobar
7+
print("howdy from foobar")
8+
9+

0 commit comments

Comments
 (0)