Skip to content

Commit 0af5c50

Browse files
committed
Requestify raw and brief comment computation
Turn these ASTContext cached computations into request evaluator cached computations.
1 parent 11cff9e commit 0af5c50

File tree

7 files changed

+95
-93
lines changed

7 files changed

+95
-93
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,12 +1522,6 @@ class ASTContext final {
15221522
Optional<ExternalSourceLocs *> getExternalSourceLocs(const Decl *D);
15231523
void setExternalSourceLocs(const Decl *D, ExternalSourceLocs *Locs);
15241524

1525-
Optional<std::pair<RawComment, bool>> getRawComment(const Decl *D);
1526-
void setRawComment(const Decl *D, RawComment RC, bool FromSerialized);
1527-
1528-
Optional<StringRef> getBriefComment(const Decl *D);
1529-
void setBriefComment(const Decl *D, StringRef Comment);
1530-
15311525
friend TypeBase;
15321526
friend ArchetypeType;
15331527
friend OpaqueTypeDecl;

include/swift/AST/Decl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
747747
friend class IterableDeclContext;
748748
friend class MemberLookupTable;
749749
friend class DeclDeserializer;
750+
friend class RawCommentRequest;
750751

751752
private:
752753
llvm::PointerUnion<DeclContext *, ASTContext *> Context;

include/swift/AST/TypeCheckRequests.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4188,6 +4188,44 @@ class LocalDiscriminatorsRequest
41884188
bool isCached() const { return true; }
41894189
};
41904190

4191+
/// Retrieve the raw documentation comment of a declaration.
4192+
class RawCommentRequest
4193+
: public SimpleRequest<RawCommentRequest,
4194+
RawComment(const Decl *),
4195+
RequestFlags::Cached> {
4196+
public:
4197+
using SimpleRequest::SimpleRequest;
4198+
4199+
private:
4200+
friend SimpleRequest;
4201+
4202+
// Evaluation.
4203+
RawComment evaluate(Evaluator &evaluator, const Decl *D) const;
4204+
4205+
public:
4206+
// Separate caching.
4207+
bool isCached() const { return true; }
4208+
};
4209+
4210+
/// Retrieve the brief portion of a declaration's document comment.
4211+
class BriefCommentRequest
4212+
: public SimpleRequest<BriefCommentRequest,
4213+
StringRef(const Decl *),
4214+
RequestFlags::Cached> {
4215+
public:
4216+
using SimpleRequest::SimpleRequest;
4217+
4218+
private:
4219+
friend SimpleRequest;
4220+
4221+
// Evaluation.
4222+
StringRef evaluate(Evaluator &evaluator, const Decl *D) const;
4223+
4224+
public:
4225+
// Separate caching.
4226+
bool isCached() const { return true; }
4227+
};
4228+
41914229
/// Checks that all of a class's \c \@objcImplementation extensions provide
41924230
/// complete and correct implementations for their corresponding interfaces.
41934231
/// This is done on all of a class's implementations at once to improve diagnostics.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,12 @@ SWIFT_REQUEST(TypeChecker, GetRuntimeDiscoverableAttributes,
469469
SWIFT_REQUEST(TypeChecker, LocalDiscriminatorsRequest,
470470
unsigned(DeclContext *),
471471
Cached, NoLocationInfo)
472+
SWIFT_REQUEST(TypeChecker, RawCommentRequest,
473+
RawComment(const Decl *),
474+
Cached, NoLocationInfo)
475+
SWIFT_REQUEST(TypeChecker, BriefCommentRequest,
476+
StringRef(const Decl *),
477+
Cached, NoLocationInfo)
472478
SWIFT_REQUEST(TypeChecker, IsNonUserModuleRequest,
473479
bool(ModuleDecl *),
474480
Cached, NoLocationInfo)

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,6 @@ struct ASTContext::Implementation {
314314
/// actual \c SourceLocs that require opening their external buffer.
315315
llvm::DenseMap<const Decl *, ExternalSourceLocs *> ExternalSourceLocs;
316316

317-
/// Map from Swift declarations to raw comments.
318-
llvm::DenseMap<const Decl *, std::pair<RawComment, bool>> RawComments;
319-
320-
/// Map from Swift declarations to brief comments.
321-
llvm::DenseMap<const Decl *, StringRef> BriefComments;
322-
323317
/// Map from declarations to foreign error conventions.
324318
/// This applies to both actual imported functions and to @objc functions.
325319
llvm::DenseMap<const AbstractFunctionDecl *,
@@ -2596,30 +2590,6 @@ void ASTContext::setExternalSourceLocs(const Decl *D,
25962590
getImpl().ExternalSourceLocs[D] = Locs;
25972591
}
25982592

2599-
Optional<std::pair<RawComment, bool>> ASTContext::getRawComment(const Decl *D) {
2600-
auto Known = getImpl().RawComments.find(D);
2601-
if (Known == getImpl().RawComments.end())
2602-
return None;
2603-
2604-
return Known->second;
2605-
}
2606-
2607-
void ASTContext::setRawComment(const Decl *D, RawComment RC, bool FromSerialized) {
2608-
getImpl().RawComments[D] = std::make_pair(RC, FromSerialized);
2609-
}
2610-
2611-
Optional<StringRef> ASTContext::getBriefComment(const Decl *D) {
2612-
auto Known = getImpl().BriefComments.find(D);
2613-
if (Known == getImpl().BriefComments.end())
2614-
return None;
2615-
2616-
return Known->second;
2617-
}
2618-
2619-
void ASTContext::setBriefComment(const Decl *D, StringRef Comment) {
2620-
getImpl().BriefComments[D] = Comment;
2621-
}
2622-
26232593
NormalProtocolConformance *
26242594
ASTContext::getConformance(Type conformingType,
26252595
ProtocolDecl *protocol,
@@ -2922,8 +2892,6 @@ size_t ASTContext::getTotalMemory() const {
29222892
getImpl().Allocator.getTotalMemory() +
29232893
getImpl().Cleanups.capacity() +
29242894
llvm::capacity_in_bytes(getImpl().ModuleLoaders) +
2925-
llvm::capacity_in_bytes(getImpl().RawComments) +
2926-
llvm::capacity_in_bytes(getImpl().BriefComments) +
29272895
llvm::capacity_in_bytes(getImpl().ModuleTypes) +
29282896
llvm::capacity_in_bytes(getImpl().GenericParamTypes) +
29292897
// getImpl().GenericFunctionTypes ?

lib/AST/DocComment.cpp

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/Comment.h"
2121
#include "swift/AST/Decl.h"
2222
#include "swift/AST/FileUnit.h"
23+
#include "swift/AST/TypeCheckRequests.h"
2324
#include "swift/AST/Types.h"
2425
#include "swift/AST/PrettyStackTrace.h"
2526
#include "swift/AST/RawComment.h"
@@ -498,39 +499,37 @@ DocComment *swift::getCascadingDocComment(swift::markup::MarkupContext &MC,
498499
return doc;
499500
}
500501

501-
StringRef Decl::getBriefComment() const {
502-
if (!this->canHaveComment())
503-
return StringRef();
504-
505-
// Check the cache in ASTContext.
506-
auto &Context = getASTContext();
507-
if (Optional<StringRef> Comment = Context.getBriefComment(this))
508-
return Comment.value();
502+
StringRef
503+
BriefCommentRequest::evaluate(Evaluator &evaluator, const Decl *D) const {
504+
auto *DC = D->getDeclContext();
505+
auto &ctx = DC->getASTContext();
509506

510-
// Check if the serialized module may have the brief comment available.
511-
if (auto *Unit =
512-
dyn_cast<FileUnit>(this->getDeclContext()->getModuleScopeContext())) {
513-
if (Optional<CommentInfo> C = Unit->getCommentForDecl(this)) {
514-
Context.setBriefComment(this, C->Brief);
507+
// Check if the brief comment is available in the swiftdoc.
508+
if (auto *Unit = dyn_cast<FileUnit>(DC->getModuleScopeContext())) {
509+
if (auto C = Unit->getCommentForDecl(D))
515510
return C->Brief;
516-
}
517511
}
518512

519-
// Otherwise, parse the brief from the raw comment itself.
520-
auto RC = getRawComment();
521-
522-
StringRef Result;
523-
if (RC.isEmpty())
524-
if (auto *docD = getDocCommentProvidingDecl(this))
513+
// Otherwise, parse the brief from the raw comment itself. This will look into the
514+
// swiftsourceinfo if needed.
515+
auto RC = D->getRawComment();
516+
if (RC.isEmpty()) {
517+
if (auto *docD = getDocCommentProvidingDecl(D))
525518
RC = docD->getRawComment();
526-
if (!RC.isEmpty()) {
527-
SmallString<256> BriefStr;
528-
llvm::raw_svector_ostream OS(BriefStr);
529-
printBriefComment(RC, OS);
530-
Result = Context.AllocateCopy(BriefStr.str());
531519
}
520+
if (RC.isEmpty())
521+
return StringRef();
522+
523+
SmallString<256> BriefStr;
524+
llvm::raw_svector_ostream OS(BriefStr);
525+
printBriefComment(RC, OS);
526+
return ctx.AllocateCopy(BriefStr.str());
527+
}
528+
529+
StringRef Decl::getBriefComment() const {
530+
if (!this->canHaveComment())
531+
return StringRef();
532532

533-
// Cache it.
534-
Context.setBriefComment(this, Result);
535-
return Result;
533+
auto &eval = getASTContext().evaluator;
534+
return evaluateOrDefault(eval, BriefCommentRequest{this}, StringRef());
536535
}

lib/AST/RawComment.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/Module.h"
2424
#include "swift/AST/PrettyStackTrace.h"
2525
#include "swift/AST/SourceFile.h"
26+
#include "swift/AST/TypeCheckRequests.h"
2627
#include "swift/AST/Types.h"
2728
#include "swift/Basic/Defer.h"
2829
#include "swift/Basic/PrimitiveParsing.h"
@@ -130,37 +131,28 @@ static RawComment toRawComment(ASTContext &Context, CharSourceRange Range) {
130131
return Result;
131132
}
132133

133-
RawComment Decl::getRawComment() const {
134-
if (!this->canHaveComment())
135-
return RawComment();
136-
137-
// Check the cache in ASTContext.
138-
auto &Context = getASTContext();
139-
if (Optional<std::pair<RawComment, bool>> RC = Context.getRawComment(this)) {
140-
return RC.value().first;
141-
}
134+
RawComment RawCommentRequest::evaluate(Evaluator &eval, const Decl *D) const {
135+
auto *DC = D->getDeclContext();
136+
auto &ctx = DC->getASTContext();
142137

143138
// Check the declaration itself.
144-
if (auto *Attr = getAttrs().getAttribute<RawDocCommentAttr>()) {
145-
RawComment Result = toRawComment(Context, Attr->getCommentRange());
146-
Context.setRawComment(this, Result, true);
147-
return Result;
148-
}
139+
if (auto *Attr = D->getAttrs().getAttribute<RawDocCommentAttr>())
140+
return toRawComment(ctx, Attr->getCommentRange());
149141

150-
if (!getDeclContext())
151-
return RawComment();
152-
auto *Unit = dyn_cast<FileUnit>(getDeclContext()->getModuleScopeContext());
142+
auto *Unit = dyn_cast<FileUnit>(DC->getModuleScopeContext());
153143
if (!Unit)
154144
return RawComment();
155145

156146
switch (Unit->getKind()) {
157147
case FileUnitKind::SerializedAST: {
158-
auto *CachedLocs = getSerializedLocs();
148+
// First check to see if we have the comment location available in the
149+
// swiftsourceinfo, allowing us to grab it from the original file.
150+
auto *CachedLocs = D->getSerializedLocs();
159151
if (!CachedLocs->DocRanges.empty()) {
160152
SmallVector<SingleRawComment, 4> SRCs;
161153
for (const auto &Range : CachedLocs->DocRanges) {
162154
if (Range.isValid()) {
163-
SRCs.push_back({Range, Context.SourceMgr});
155+
SRCs.push_back({Range, ctx.SourceMgr});
164156
} else {
165157
// if we've run into an invalid range, don't bother trying to load
166158
// any of the other comments
@@ -169,17 +161,13 @@ RawComment Decl::getRawComment() const {
169161
}
170162
}
171163

172-
if (!SRCs.empty()) {
173-
auto RC = RawComment(Context.AllocateCopy(llvm::makeArrayRef(SRCs)));
174-
Context.setRawComment(this, RC, true);
175-
return RC;
176-
}
164+
if (!SRCs.empty())
165+
return RawComment(ctx.AllocateCopy(llvm::makeArrayRef(SRCs)));
177166
}
178167

179-
if (Optional<CommentInfo> C = Unit->getCommentForDecl(this)) {
180-
Context.setRawComment(this, C->Raw, false);
168+
// Otherwise check to see if we have a comment available in the swiftdoc.
169+
if (auto C = Unit->getCommentForDecl(D))
181170
return C->Raw;
182-
}
183171

184172
return RawComment();
185173
}
@@ -193,6 +181,14 @@ RawComment Decl::getRawComment() const {
193181
llvm_unreachable("invalid file kind");
194182
}
195183

184+
RawComment Decl::getRawComment() const {
185+
if (!this->canHaveComment())
186+
return RawComment();
187+
188+
auto &eval = getASTContext().evaluator;
189+
return evaluateOrDefault(eval, RawCommentRequest{this}, RawComment());
190+
}
191+
196192
Optional<StringRef> Decl::getGroupName() const {
197193
if (hasClangNode())
198194
return None;

0 commit comments

Comments
 (0)