Skip to content

Commit 385eb8a

Browse files
committed
AST: Print attributes on accessor declarations.
1 parent 8862b1b commit 385eb8a

File tree

1 file changed

+61
-48
lines changed

1 file changed

+61
-48
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,36 +2197,6 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
21972197
// Otherwise, print all the concrete defining accessors.
21982198
bool PrintAccessorBody = Options.FunctionDefinitions;
21992199

2200-
// Helper to print an accessor. Returns true if the
2201-
// accessor was present but skipped.
2202-
auto PrintAccessor = [&](AccessorKind Kind) -> bool {
2203-
auto *Accessor = ASD->getAccessor(Kind);
2204-
if (!Accessor || !shouldPrint(Accessor))
2205-
return true;
2206-
if (!PrintAccessorBody) {
2207-
Printer << " ";
2208-
printSelfAccessKindModifiersIfNeeded(Accessor);
2209-
2210-
Printer.printKeyword(getAccessorLabel(Accessor->getAccessorKind()), Options);
2211-
2212-
// handle any effects specifiers
2213-
if (Accessor->getAccessorKind() == AccessorKind::Get) {
2214-
if (asyncGet) printWithSpace("async");
2215-
if (throwsGet) printWithSpace("throws");
2216-
}
2217-
2218-
} else {
2219-
{
2220-
IndentRAII IndentMore(*this);
2221-
indent();
2222-
visit(Accessor);
2223-
}
2224-
indent();
2225-
Printer.printNewline();
2226-
}
2227-
return false;
2228-
};
2229-
22302200
// Determine if we should print the getter without the 'get { ... }'
22312201
// block around it.
22322202
bool isOnlyGetter = impl.getReadImpl() == ReadImplKind::Get &&
@@ -2240,28 +2210,31 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
22402210
return;
22412211
}
22422212

2243-
Printer << " {";
2244-
2245-
if (PrintAccessorBody)
2246-
Printer.printNewline();
2213+
// Collect the accessor declarations that we should print.
2214+
SmallVector<AccessorDecl *, 4> accessorsToPrint;
2215+
auto AddAccessorToPrint = [&](AccessorKind kind) {
2216+
auto *Accessor = ASD->getAccessor(kind);
2217+
if (Accessor && shouldPrint(Accessor))
2218+
accessorsToPrint.push_back(Accessor);
2219+
};
22472220

22482221
if (PrintAbstract) {
2249-
PrintAccessor(AccessorKind::Get);
2222+
AddAccessorToPrint(AccessorKind::Get);
22502223
if (ASD->supportsMutation())
2251-
PrintAccessor(AccessorKind::Set);
2224+
AddAccessorToPrint(AccessorKind::Set);
22522225
} else {
22532226
switch (impl.getReadImpl()) {
22542227
case ReadImplKind::Stored:
22552228
case ReadImplKind::Inherited:
22562229
break;
22572230
case ReadImplKind::Get:
2258-
PrintAccessor(AccessorKind::Get);
2231+
AddAccessorToPrint(AccessorKind::Get);
22592232
break;
22602233
case ReadImplKind::Address:
2261-
PrintAccessor(AccessorKind::Address);
2234+
AddAccessorToPrint(AccessorKind::Address);
22622235
break;
22632236
case ReadImplKind::Read:
2264-
PrintAccessor(AccessorKind::Read);
2237+
AddAccessorToPrint(AccessorKind::Read);
22652238
break;
22662239
}
22672240
switch (impl.getWriteImpl()) {
@@ -2271,27 +2244,67 @@ void PrintAST::printAccessors(const AbstractStorageDecl *ASD) {
22712244
llvm_unreachable("simply-stored variable should have been filtered out");
22722245
case WriteImplKind::StoredWithObservers:
22732246
case WriteImplKind::InheritedWithObservers: {
2274-
PrintAccessor(AccessorKind::Get);
2275-
PrintAccessor(AccessorKind::Set);
2247+
AddAccessorToPrint(AccessorKind::Get);
2248+
AddAccessorToPrint(AccessorKind::Set);
22762249
break;
22772250
}
22782251
case WriteImplKind::Set:
2279-
PrintAccessor(AccessorKind::Set);
2252+
AddAccessorToPrint(AccessorKind::Set);
22802253
if (!shouldHideModifyAccessor())
2281-
PrintAccessor(AccessorKind::Modify);
2254+
AddAccessorToPrint(AccessorKind::Modify);
22822255
break;
22832256
case WriteImplKind::MutableAddress:
2284-
PrintAccessor(AccessorKind::MutableAddress);
2285-
PrintAccessor(AccessorKind::WillSet);
2286-
PrintAccessor(AccessorKind::DidSet);
2257+
AddAccessorToPrint(AccessorKind::MutableAddress);
2258+
AddAccessorToPrint(AccessorKind::WillSet);
2259+
AddAccessorToPrint(AccessorKind::DidSet);
22872260
break;
22882261
case WriteImplKind::Modify:
2289-
PrintAccessor(AccessorKind::Modify);
2262+
AddAccessorToPrint(AccessorKind::Modify);
22902263
break;
22912264
}
22922265
}
22932266

2294-
if (!PrintAccessorBody)
2267+
// If we're not printing the accessor bodies and none of the accessors have
2268+
// attributes then we can print in a shorter, compact form.
2269+
bool PrintCompactAccessors =
2270+
!PrintAccessorBody &&
2271+
std::all_of(accessorsToPrint.begin(), accessorsToPrint.end(),
2272+
[](AccessorDecl *accessor) {
2273+
return accessor->getAttrs().isEmpty();
2274+
});
2275+
2276+
Printer << " {";
2277+
2278+
if (!PrintCompactAccessors)
2279+
Printer.printNewline();
2280+
2281+
for (auto *accessor : accessorsToPrint) {
2282+
if (PrintCompactAccessors) {
2283+
Printer << " ";
2284+
printSelfAccessKindModifiersIfNeeded(accessor);
2285+
2286+
Printer.printKeyword(getAccessorLabel(accessor->getAccessorKind()),
2287+
Options);
2288+
2289+
// handle any effects specifiers
2290+
if (accessor->getAccessorKind() == AccessorKind::Get) {
2291+
if (asyncGet)
2292+
printWithSpace("async");
2293+
if (throwsGet)
2294+
printWithSpace("throws");
2295+
}
2296+
} else {
2297+
{
2298+
IndentRAII IndentMore(*this);
2299+
indent();
2300+
visit(accessor);
2301+
}
2302+
indent();
2303+
Printer.printNewline();
2304+
}
2305+
}
2306+
2307+
if (PrintCompactAccessors)
22952308
Printer << " ";
22962309

22972310
Printer << "}";

0 commit comments

Comments
 (0)