Skip to content

AST: Print attributes on accessor declarations #64311

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
Mar 13, 2023
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
109 changes: 61 additions & 48 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2197,36 +2197,6 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
// Otherwise, print all the concrete defining accessors.
bool PrintAccessorBody = Options.FunctionDefinitions;

// Helper to print an accessor. Returns true if the
// accessor was present but skipped.
auto PrintAccessor = [&](AccessorKind Kind) -> bool {
auto *Accessor = ASD->getAccessor(Kind);
if (!Accessor || !shouldPrint(Accessor))
return true;
if (!PrintAccessorBody) {
Printer << " ";
printSelfAccessKindModifiersIfNeeded(Accessor);

Printer.printKeyword(getAccessorLabel(Accessor->getAccessorKind()), Options);

// handle any effects specifiers
if (Accessor->getAccessorKind() == AccessorKind::Get) {
if (asyncGet) printWithSpace("async");
if (throwsGet) printWithSpace("throws");
}

} else {
{
IndentRAII IndentMore(*this);
indent();
visit(Accessor);
}
indent();
Printer.printNewline();
}
return false;
};

// Determine if we should print the getter without the 'get { ... }'
// block around it.
bool isOnlyGetter = impl.getReadImpl() == ReadImplKind::Get &&
Expand All @@ -2240,28 +2210,31 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
return;
}

Printer << " {";

if (PrintAccessorBody)
Printer.printNewline();
// Collect the accessor declarations that we should print.
SmallVector<AccessorDecl *, 4> accessorsToPrint;
auto AddAccessorToPrint = [&](AccessorKind kind) {
auto *Accessor = ASD->getAccessor(kind);
if (Accessor && shouldPrint(Accessor))
accessorsToPrint.push_back(Accessor);
};

if (PrintAbstract) {
PrintAccessor(AccessorKind::Get);
AddAccessorToPrint(AccessorKind::Get);
if (ASD->supportsMutation())
PrintAccessor(AccessorKind::Set);
AddAccessorToPrint(AccessorKind::Set);
} else {
switch (impl.getReadImpl()) {
case ReadImplKind::Stored:
case ReadImplKind::Inherited:
break;
case ReadImplKind::Get:
PrintAccessor(AccessorKind::Get);
AddAccessorToPrint(AccessorKind::Get);
break;
case ReadImplKind::Address:
PrintAccessor(AccessorKind::Address);
AddAccessorToPrint(AccessorKind::Address);
break;
case ReadImplKind::Read:
PrintAccessor(AccessorKind::Read);
AddAccessorToPrint(AccessorKind::Read);
break;
}
switch (impl.getWriteImpl()) {
Expand All @@ -2271,27 +2244,67 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
llvm_unreachable("simply-stored variable should have been filtered out");
case WriteImplKind::StoredWithObservers:
case WriteImplKind::InheritedWithObservers: {
PrintAccessor(AccessorKind::Get);
PrintAccessor(AccessorKind::Set);
AddAccessorToPrint(AccessorKind::Get);
AddAccessorToPrint(AccessorKind::Set);
break;
}
case WriteImplKind::Set:
PrintAccessor(AccessorKind::Set);
AddAccessorToPrint(AccessorKind::Set);
if (!shouldHideModifyAccessor())
PrintAccessor(AccessorKind::Modify);
AddAccessorToPrint(AccessorKind::Modify);
break;
case WriteImplKind::MutableAddress:
PrintAccessor(AccessorKind::MutableAddress);
PrintAccessor(AccessorKind::WillSet);
PrintAccessor(AccessorKind::DidSet);
AddAccessorToPrint(AccessorKind::MutableAddress);
AddAccessorToPrint(AccessorKind::WillSet);
AddAccessorToPrint(AccessorKind::DidSet);
break;
case WriteImplKind::Modify:
PrintAccessor(AccessorKind::Modify);
AddAccessorToPrint(AccessorKind::Modify);
break;
}
}

if (!PrintAccessorBody)
// If we're not printing the accessor bodies and none of the accessors have
// attributes then we can print in a shorter, compact form.
bool PrintCompactAccessors =
!PrintAccessorBody &&
std::all_of(accessorsToPrint.begin(), accessorsToPrint.end(),
[](AccessorDecl *accessor) {
return accessor->getAttrs().isEmpty();
});

Printer << " {";

if (!PrintCompactAccessors)
Printer.printNewline();

for (auto *accessor : accessorsToPrint) {
if (PrintCompactAccessors) {
Printer << " ";
printSelfAccessKindModifiersIfNeeded(accessor);

Printer.printKeyword(getAccessorLabel(accessor->getAccessorKind()),
Options);

// handle any effects specifiers
if (accessor->getAccessorKind() == AccessorKind::Get) {
if (asyncGet)
printWithSpace("async");
if (throwsGet)
printWithSpace("throws");
}
} else {
{
IndentRAII IndentMore(*this);
indent();
visit(accessor);
}
indent();
Printer.printNewline();
}
}

if (PrintCompactAccessors)
Printer << " ";

Printer << "}";
Expand Down
28 changes: 28 additions & 0 deletions test/ModuleInterface/property_wrapper_availability.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-emit-module-interface(%t/Library.swiftinterface) %s -module-name Library -target %target-swift-abi-5.3-triple
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -module-name Library
// RUN: %FileCheck %s < %t/Library.swiftinterface

@available(SwiftStdlib 5.2, *)
@propertyWrapper
public struct ConditionallyAvailableWrapper<T> {
public var wrappedValue: T

public init(wrappedValue: T) {
self.wrappedValue = wrappedValue
}
}

// CHECK: public struct HasWrappers {
public struct HasWrappers {
// CHECK: @available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *)
// CHECK-NEXT: @Library.ConditionallyAvailableWrapper public var x: Swift.Int {
// CHECK-NEXT: get
// CHECK-NEXT: @available(iOS 13.4, tvOS 13.4, watchOS 6.2, macOS 10.15.4, *)
// CHECK-NEXT: set
// CHECK-NEXT: @available(iOS 13.4, tvOS 13.4, watchOS 6.2, macOS 10.15.4, *)
// CHECK-NEXT: _modify
// CHECK-NEXT: }
@available(SwiftStdlib 5.2, *)
@ConditionallyAvailableWrapper public var x: Int
}