Skip to content

Don't emit @main into SIL #40767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ SIMPLE_DECL_ATTR(dynamicCallable, DynamicCallable,
OnNominalType |
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
6)
SIMPLE_DECL_ATTR(main, MainType,
DECL_ATTR(main, MainType,
OnClass | OnStruct | OnEnum | OnExtension |
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
7)
Expand Down
19 changes: 19 additions & 0 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,25 @@ class ObjCAttr final : public DeclAttribute,
}
};

class MainTypeAttr final : public DeclAttribute {
public:
MainTypeAttr(bool isImplicit)
: DeclAttribute(DAK_MainType, SourceLoc(), SourceLoc(), isImplicit) {}

MainTypeAttr(SourceLoc AtLoc, SourceLoc NameLoc)
: DeclAttribute(DAK_MainType, AtLoc,
SourceRange(AtLoc.isValid() ? AtLoc : NameLoc, NameLoc),
/*Implicit=*/false) {}

MainTypeAttr(SourceLoc NameLoc)
: DeclAttribute(DAK_MainType, SourceLoc(), SourceRange(NameLoc, NameLoc),
/*Implicit=*/false) {}

static bool classof(const DeclAttribute *DA) {
return DA->getKind() == DAK_MainType;
}
};

class PrivateImportAttr final
: public DeclAttribute {
StringRef SourceFile;
Expand Down
10 changes: 10 additions & 0 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,14 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
}
return true;

case DAK_MainType: {
// Don't print into SIL. Necessary bits have already been generated.
if (Options.PrintForSIL)
return false;
Printer.printSimpleAttr(getAttrName(), /*needAt=*/true);
return true;
}

case DAK_SetterAccess:
Printer.printKeyword(getAttrName(), Options, "(set)");
return true;
Expand Down Expand Up @@ -1241,6 +1249,8 @@ StringRef DeclAttribute::getAttrName() const {
case DAK_ObjC:
case DAK_ObjCRuntimeName:
return "objc";
case DAK_MainType:
return "main";
case DAK_DynamicReplacement:
return "_dynamicReplacement";
case DAK_TypeEraser:
Expand Down
5 changes: 5 additions & 0 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,11 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
break;
#include "swift/AST/Attr.def"

case DAK_MainType:
if (!DiscardAttribute)
Attributes.add(new (Context) MainTypeAttr(AtLoc, Loc));
break;

case DAK_Effects: {
auto kind = parseSingleAttrOption<EffectsKind>
(*this, Loc, AttrRange, AttrName, DK)
Expand Down
8 changes: 8 additions & 0 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4485,6 +4485,14 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
break;
}

case decls_block::MainType_DECL_ATTR: {
bool isImplicit;
serialization::decls_block::MainTypeDeclAttrLayout::readRecord(
scratch, isImplicit);
Attr = new (ctx) MainTypeAttr(isImplicit);
break;
}

case decls_block::Specialize_DECL_ATTR: {
unsigned exported;
SpecializeAttr::SpecializationKind specializationKind;
Expand Down
7 changes: 6 additions & 1 deletion lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 651; // existential requires any
const uint16_t SWIFTMODULE_VERSION_MINOR = 652; // @main cleanup

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down Expand Up @@ -1830,6 +1830,11 @@ namespace decls_block {
IdentifierIDField // name
>;

using MainTypeDeclAttrLayout = BCRecordLayout<
MainType_DECL_ATTR,
BCFixed<1> // implicit flag
>;

using SemanticsDeclAttrLayout = BCRecordLayout<
Semantics_DECL_ATTR,
BCFixed<1>, // implicit flag
Expand Down
7 changes: 7 additions & 0 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,13 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
return;
}

case DAK_MainType: {
auto abbrCode = S.DeclTypeAbbrCodes[MainTypeDeclAttrLayout::Code];
MainTypeDeclAttrLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
DA->isImplicit());
return;
}

case DAK_Specialize: {
auto abbrCode = S.DeclTypeAbbrCodes[SpecializeDeclAttrLayout::Code];
auto attr = cast<SpecializeAttr>(DA);
Expand Down
4 changes: 4 additions & 0 deletions test/attr/ApplicationMain/attr_main_struct.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s
// RUN: %target-swift-frontend -emit-silgen -parse-as-library %s | %FileCheck %s

@main
struct EntryPoint {
static func main() {
}
}

// CHECK-NOT: @main struct EntryPoint {
// CHECK: struct EntryPoint {