Skip to content

Commit 544c47c

Browse files
authored
Merge pull request #40767 from etcwilde/ewilde/no-main-in-sil
Don't emit @main into SIL
2 parents 76c9ec7 + 596a2fc commit 544c47c

File tree

8 files changed

+60
-2
lines changed

8 files changed

+60
-2
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ SIMPLE_DECL_ATTR(dynamicCallable, DynamicCallable,
150150
OnNominalType |
151151
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
152152
6)
153-
SIMPLE_DECL_ATTR(main, MainType,
153+
DECL_ATTR(main, MainType,
154154
OnClass | OnStruct | OnEnum | OnExtension |
155155
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
156156
7)

include/swift/AST/Attr.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,25 @@ class ObjCAttr final : public DeclAttribute,
915915
}
916916
};
917917

918+
class MainTypeAttr final : public DeclAttribute {
919+
public:
920+
MainTypeAttr(bool isImplicit)
921+
: DeclAttribute(DAK_MainType, SourceLoc(), SourceLoc(), isImplicit) {}
922+
923+
MainTypeAttr(SourceLoc AtLoc, SourceLoc NameLoc)
924+
: DeclAttribute(DAK_MainType, AtLoc,
925+
SourceRange(AtLoc.isValid() ? AtLoc : NameLoc, NameLoc),
926+
/*Implicit=*/false) {}
927+
928+
MainTypeAttr(SourceLoc NameLoc)
929+
: DeclAttribute(DAK_MainType, SourceLoc(), SourceRange(NameLoc, NameLoc),
930+
/*Implicit=*/false) {}
931+
932+
static bool classof(const DeclAttribute *DA) {
933+
return DA->getKind() == DAK_MainType;
934+
}
935+
};
936+
918937
class PrivateImportAttr final
919938
: public DeclAttribute {
920939
StringRef SourceFile;

lib/AST/Attr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,14 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
895895
}
896896
return true;
897897

898+
case DAK_MainType: {
899+
// Don't print into SIL. Necessary bits have already been generated.
900+
if (Options.PrintForSIL)
901+
return false;
902+
Printer.printSimpleAttr(getAttrName(), /*needAt=*/true);
903+
return true;
904+
}
905+
898906
case DAK_SetterAccess:
899907
Printer.printKeyword(getAttrName(), Options, "(set)");
900908
return true;
@@ -1241,6 +1249,8 @@ StringRef DeclAttribute::getAttrName() const {
12411249
case DAK_ObjC:
12421250
case DAK_ObjCRuntimeName:
12431251
return "objc";
1252+
case DAK_MainType:
1253+
return "main";
12441254
case DAK_DynamicReplacement:
12451255
return "_dynamicReplacement";
12461256
case DAK_TypeEraser:

lib/Parse/ParseDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,11 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
19431943
break;
19441944
#include "swift/AST/Attr.def"
19451945

1946+
case DAK_MainType:
1947+
if (!DiscardAttribute)
1948+
Attributes.add(new (Context) MainTypeAttr(AtLoc, Loc));
1949+
break;
1950+
19461951
case DAK_Effects: {
19471952
auto kind = parseSingleAttrOption<EffectsKind>
19481953
(*this, Loc, AttrRange, AttrName, DK)

lib/Serialization/Deserialization.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4485,6 +4485,14 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
44854485
break;
44864486
}
44874487

4488+
case decls_block::MainType_DECL_ATTR: {
4489+
bool isImplicit;
4490+
serialization::decls_block::MainTypeDeclAttrLayout::readRecord(
4491+
scratch, isImplicit);
4492+
Attr = new (ctx) MainTypeAttr(isImplicit);
4493+
break;
4494+
}
4495+
44884496
case decls_block::Specialize_DECL_ATTR: {
44894497
unsigned exported;
44904498
SpecializeAttr::SpecializationKind specializationKind;

lib/Serialization/ModuleFormat.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5656
/// describe what change you made. The content of this comment isn't important;
5757
/// it just ensures a conflict if two people change the module format.
5858
/// Don't worry about adhering to the 80-column limit for this line.
59-
const uint16_t SWIFTMODULE_VERSION_MINOR = 651; // existential requires any
59+
const uint16_t SWIFTMODULE_VERSION_MINOR = 652; // @main cleanup
6060

6161
/// A standard hash seed used for all string hashes in a serialized module.
6262
///
@@ -1830,6 +1830,11 @@ namespace decls_block {
18301830
IdentifierIDField // name
18311831
>;
18321832

1833+
using MainTypeDeclAttrLayout = BCRecordLayout<
1834+
MainType_DECL_ATTR,
1835+
BCFixed<1> // implicit flag
1836+
>;
1837+
18331838
using SemanticsDeclAttrLayout = BCRecordLayout<
18341839
Semantics_DECL_ATTR,
18351840
BCFixed<1>, // implicit flag

lib/Serialization/Serialization.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,13 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
25902590
return;
25912591
}
25922592

2593+
case DAK_MainType: {
2594+
auto abbrCode = S.DeclTypeAbbrCodes[MainTypeDeclAttrLayout::Code];
2595+
MainTypeDeclAttrLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
2596+
DA->isImplicit());
2597+
return;
2598+
}
2599+
25932600
case DAK_Specialize: {
25942601
auto abbrCode = S.DeclTypeAbbrCodes[SpecializeDeclAttrLayout::Code];
25952602
auto attr = cast<SpecializeAttr>(DA);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s
2+
// RUN: %target-swift-frontend -emit-silgen -parse-as-library %s | %FileCheck %s
23

34
@main
45
struct EntryPoint {
56
static func main() {
67
}
78
}
9+
10+
// CHECK-NOT: @main struct EntryPoint {
11+
// CHECK: struct EntryPoint {

0 commit comments

Comments
 (0)