Skip to content

Commit 9f41261

Browse files
committed
Remove AsyncFunctionName from serialization
We can get the DeclName of the async function from the resolved function decl itself, so we don't actually need to serialize it. This patch pulls the declNameRef from the serialization.
1 parent 5b17c7c commit 9f41261

File tree

6 files changed

+45
-39
lines changed

6 files changed

+45
-39
lines changed

include/swift/AST/Attr.h

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,19 +2151,27 @@ class TransposeAttr final
21512151
/// alternative, optionally providing a name (for cases when the alternative
21522152
/// has a different name).
21532153
class CompletionHandlerAsyncAttr final : public DeclAttribute {
2154+
private:
2155+
/// DeclName of the async function in the attribute
2156+
const DeclNameRef AsyncFunctionName;
2157+
21542158
public:
21552159
/// Source location of the async function name in the attribute
21562160
const SourceLoc AsyncFunctionNameLoc;
21572161

2158-
/// DeclName of the async function in the attribute
2159-
const DeclNameRef AsyncFunctionName;
2160-
2161-
/// Source location of the completion handler index passed to the index
2162-
const SourceLoc CompletionHandlerIndexLoc;
2162+
/// Get the name of the async function
2163+
///
2164+
/// The name will come from the AsyncFunctionDecl if available, otherwise will
2165+
/// fall back on the user-provided name. If that is not defined, this function
2166+
/// will abort.
2167+
DeclNameRef getAsyncFunctionName() const;
21632168

21642169
/// The index of the completion handler
21652170
const size_t CompletionHandlerIndex;
21662171

2172+
/// Source location of the completion handler index passed to the index
2173+
const SourceLoc CompletionHandlerIndexLoc;
2174+
21672175
/// True when the completion handler was specified explicitly
21682176
const bool ExplicitCompletionHandlerIndex;
21692177

@@ -2177,12 +2185,26 @@ class CompletionHandlerAsyncAttr final : public DeclAttribute {
21772185
SourceLoc atLoc, SourceRange range)
21782186
: DeclAttribute(DAK_CompletionHandlerAsync, atLoc, range,
21792187
/*implicit*/ false),
2180-
AsyncFunctionNameLoc(asyncFunctionNameLoc),
21812188
AsyncFunctionName(asyncFunctionName),
2182-
CompletionHandlerIndexLoc(completionHandlerIndexLoc),
2189+
AsyncFunctionNameLoc(asyncFunctionNameLoc),
21832190
CompletionHandlerIndex(completionHandlerIndex),
2191+
CompletionHandlerIndexLoc(completionHandlerIndexLoc),
21842192
ExplicitCompletionHandlerIndex(explicitCompletionHandlerIndex) {}
21852193

2194+
CompletionHandlerAsyncAttr(AbstractFunctionDecl &asyncFunctionDecl,
2195+
bool explicitCompletionHandlerIndex,
2196+
size_t completionHandlerIndex,
2197+
SourceLoc completionHandlerIndexLoc,
2198+
SourceLoc atLoc, SourceRange range)
2199+
: DeclAttribute(DAK_CompletionHandlerAsync, atLoc, range,
2200+
/*implicit*/ false),
2201+
CompletionHandlerIndex(completionHandlerIndex),
2202+
CompletionHandlerIndexLoc(completionHandlerIndexLoc),
2203+
ExplicitCompletionHandlerIndex(explicitCompletionHandlerIndex),
2204+
AsyncFunctionDecl(&asyncFunctionDecl) {}
2205+
2206+
2207+
21862208
static bool classof(const DeclAttribute *DA) {
21872209
return DA->getKind() == DAK_CompletionHandlerAsync;
21882210
}

lib/AST/Attr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,3 +2002,11 @@ void swift::simple_display(llvm::raw_ostream &out, const DeclAttribute *attr) {
20022002
if (attr)
20032003
attr->print(out);
20042004
}
2005+
2006+
DeclNameRef CompletionHandlerAsyncAttr::getAsyncFunctionName() const {
2007+
if (AsyncFunctionDecl)
2008+
return DeclNameRef(AsyncFunctionDecl->getName());
2009+
if (AsyncFunctionName)
2010+
return AsyncFunctionName;
2011+
llvm_unreachable("completionHandlerAsync attr missing async function name");
2012+
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5804,7 +5804,7 @@ void AttributeChecker::visitCompletionHandlerAsyncAttr(
58045804
// should return all of those types in a tuple
58055805

58065806
SmallVector<ValueDecl *, 2> allCandidates;
5807-
lookupReplacedDecl(attr->AsyncFunctionName, attr, attachedFunctionDecl,
5807+
lookupReplacedDecl(attr->getAsyncFunctionName(), attr, attachedFunctionDecl,
58085808
allCandidates);
58095809
SmallVector<AbstractFunctionDecl *, 2> candidates;
58105810
candidates.reserve(allCandidates.size());
@@ -5821,12 +5821,12 @@ void AttributeChecker::visitCompletionHandlerAsyncAttr(
58215821
if (candidates.empty()) {
58225822
diagnose(attr->AsyncFunctionNameLoc,
58235823
diag::attr_completion_handler_async_no_suitable_function,
5824-
attr->AsyncFunctionName);
5824+
attr->getAsyncFunctionName());
58255825
return;
58265826
} else if (candidates.size() > 1) {
58275827
diagnose(attr->AsyncFunctionNameLoc,
58285828
diag::attr_completion_handler_async_ambiguous_function, attr,
5829-
attr->AsyncFunctionName);
5829+
attr->getAsyncFunctionName());
58305830

58315831
for (AbstractFunctionDecl *candidate : candidates) {
58325832
diagnose(candidate->getLoc(), diag::decl_declared_here,

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4553,34 +4553,19 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
45534553
}
45544554

45554555
case decls_block::CompletionHandlerAsync_DECL_ATTR: {
4556-
bool isCompound;
4557-
ArrayRef<uint64_t> rawPieces;
45584556
uint64_t handlerIndex;
45594557
bool explicitHandlerIndex;
45604558
uint64_t asyncFunctionDeclID;
45614559
serialization::decls_block::CompletionHandlerAsyncDeclAttrLayout::
45624560
readRecord(scratch, explicitHandlerIndex, handlerIndex,
4563-
asyncFunctionDeclID, isCompound, rawPieces);
4564-
4565-
DeclNameRef asyncFunctionName;
4566-
if (!rawPieces.empty()) {
4567-
auto baseName = MF.getDeclBaseName(rawPieces[0]);
4568-
SmallVector<Identifier, 4> pieces;
4569-
for (auto rawPiece : rawPieces.drop_front())
4570-
pieces.push_back(MF.getIdentifier(rawPiece));
4571-
asyncFunctionName = !isCompound
4572-
? DeclNameRef({baseName})
4573-
: DeclNameRef({ctx, baseName, pieces});
4574-
}
4561+
asyncFunctionDeclID);
45754562

45764563
auto mappedFunctionDecl =
45774564
cast<AbstractFunctionDecl>(MF.getDecl(asyncFunctionDeclID));
45784565
Attr = new (ctx) CompletionHandlerAsyncAttr(
4579-
asyncFunctionName, /*functionNameLoc*/ SourceLoc(),
4580-
explicitHandlerIndex, handlerIndex, /*handlerIndexLoc*/ SourceLoc(),
4566+
*mappedFunctionDecl, explicitHandlerIndex, handlerIndex,
4567+
/*handlerIndexLoc*/ SourceLoc(),
45814568
/*atLoc*/ SourceLoc(), /*range*/ SourceRange());
4582-
static_cast<CompletionHandlerAsyncAttr *>(Attr)->AsyncFunctionDecl =
4583-
mappedFunctionDecl;
45844569
break;
45854570
}
45864571

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,9 +1928,7 @@ namespace decls_block {
19281928
CompletionHandlerAsync_DECL_ATTR,
19291929
BCFixed<1>, // True if explicit handler index
19301930
BCVBR<5>, // Completion handler index
1931-
DeclIDField, // Mapped async function decl
1932-
BCFixed<1>, // True if compound name
1933-
BCArray<IdentifierIDField> // Name and parameters
1931+
DeclIDField // Mapped async function decl
19341932
>;
19351933

19361934
#define SIMPLE_DECL_ATTR(X, CLASS, ...) \

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,17 +2631,10 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
26312631
"Serializing unresolved completion handler async function decl");
26322632
auto asyncFuncDeclID = S.addDeclRef(attr->AsyncFunctionDecl);
26332633

2634-
SmallVector<IdentifierID, 4> pieces;
2635-
2636-
pieces.push_back(
2637-
S.addDeclBaseNameRef(attr->AsyncFunctionName.getBaseName()));
2638-
for (auto argName : attr->AsyncFunctionName.getArgumentNames())
2639-
pieces.push_back(S.addDeclBaseNameRef(argName));
2640-
26412634
CompletionHandlerAsyncDeclAttrLayout::emitRecord(
26422635
S.Out, S.ScratchRecord, abbrCode,
26432636
attr->ExplicitCompletionHandlerIndex, attr->CompletionHandlerIndex,
2644-
asyncFuncDeclID, attr->AsyncFunctionName.isCompoundName(), pieces);
2637+
asyncFuncDeclID);
26452638
return;
26462639
}
26472640
}

0 commit comments

Comments
 (0)