Skip to content

Commit 3dbfdc0

Browse files
committed
Requestify raw and brief comment computation
Turn these ASTContext cached computations into request evaluator cached computations.
1 parent 92b36a3 commit 3dbfdc0

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
@@ -1548,12 +1548,6 @@ class ASTContext final {
15481548
Optional<ExternalSourceLocs *> getExternalSourceLocs(const Decl *D);
15491549
void setExternalSourceLocs(const Decl *D, ExternalSourceLocs *Locs);
15501550

1551-
Optional<std::pair<RawComment, bool>> getRawComment(const Decl *D);
1552-
void setRawComment(const Decl *D, RawComment RC, bool FromSerialized);
1553-
1554-
Optional<StringRef> getBriefComment(const Decl *D);
1555-
void setBriefComment(const Decl *D, StringRef Comment);
1556-
15571551
void createModuleToExecutablePluginMap();
15581552

15591553
friend TypeBase;

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
@@ -4168,6 +4168,44 @@ class LocalDiscriminatorsRequest
41684168
bool isCached() const { return true; }
41694169
};
41704170

4171+
/// Retrieve the raw documentation comment of a declaration.
4172+
class RawCommentRequest
4173+
: public SimpleRequest<RawCommentRequest,
4174+
RawComment(const Decl *),
4175+
RequestFlags::Cached> {
4176+
public:
4177+
using SimpleRequest::SimpleRequest;
4178+
4179+
private:
4180+
friend SimpleRequest;
4181+
4182+
// Evaluation.
4183+
RawComment evaluate(Evaluator &evaluator, const Decl *D) const;
4184+
4185+
public:
4186+
// Separate caching.
4187+
bool isCached() const { return true; }
4188+
};
4189+
4190+
/// Retrieve the brief portion of a declaration's document comment.
4191+
class BriefCommentRequest
4192+
: public SimpleRequest<BriefCommentRequest,
4193+
StringRef(const Decl *),
4194+
RequestFlags::Cached> {
4195+
public:
4196+
using SimpleRequest::SimpleRequest;
4197+
4198+
private:
4199+
friend SimpleRequest;
4200+
4201+
// Evaluation.
4202+
StringRef evaluate(Evaluator &evaluator, const Decl *D) const;
4203+
4204+
public:
4205+
// Separate caching.
4206+
bool isCached() const { return true; }
4207+
};
4208+
41714209
/// Checks that all of a class's \c \@objcImplementation extensions provide
41724210
/// complete and correct implementations for their corresponding interfaces.
41734211
/// 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
@@ -466,6 +466,12 @@ SWIFT_REQUEST(TypeChecker, GetRuntimeDiscoverableAttributes,
466466
SWIFT_REQUEST(TypeChecker, LocalDiscriminatorsRequest,
467467
unsigned(DeclContext *),
468468
Cached, NoLocationInfo)
469+
SWIFT_REQUEST(TypeChecker, RawCommentRequest,
470+
RawComment(const Decl *),
471+
Cached, NoLocationInfo)
472+
SWIFT_REQUEST(TypeChecker, BriefCommentRequest,
473+
StringRef(const Decl *),
474+
Cached, NoLocationInfo)
469475
SWIFT_REQUEST(TypeChecker, IsNonUserModuleRequest,
470476
bool(ModuleDecl *),
471477
Cached, NoLocationInfo)

lib/AST/ASTContext.cpp

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

320-
/// Map from Swift declarations to raw comments.
321-
llvm::DenseMap<const Decl *, std::pair<RawComment, bool>> RawComments;
322-
323-
/// Map from Swift declarations to brief comments.
324-
llvm::DenseMap<const Decl *, StringRef> BriefComments;
325-
326320
/// Map from declarations to foreign error conventions.
327321
/// This applies to both actual imported functions and to @objc functions.
328322
llvm::DenseMap<const AbstractFunctionDecl *,
@@ -2622,30 +2616,6 @@ void ASTContext::setExternalSourceLocs(const Decl *D,
26222616
getImpl().ExternalSourceLocs[D] = Locs;
26232617
}
26242618

2625-
Optional<std::pair<RawComment, bool>> ASTContext::getRawComment(const Decl *D) {
2626-
auto Known = getImpl().RawComments.find(D);
2627-
if (Known == getImpl().RawComments.end())
2628-
return None;
2629-
2630-
return Known->second;
2631-
}
2632-
2633-
void ASTContext::setRawComment(const Decl *D, RawComment RC, bool FromSerialized) {
2634-
getImpl().RawComments[D] = std::make_pair(RC, FromSerialized);
2635-
}
2636-
2637-
Optional<StringRef> ASTContext::getBriefComment(const Decl *D) {
2638-
auto Known = getImpl().BriefComments.find(D);
2639-
if (Known == getImpl().BriefComments.end())
2640-
return None;
2641-
2642-
return Known->second;
2643-
}
2644-
2645-
void ASTContext::setBriefComment(const Decl *D, StringRef Comment) {
2646-
getImpl().BriefComments[D] = Comment;
2647-
}
2648-
26492619
NormalProtocolConformance *
26502620
ASTContext::getConformance(Type conformingType,
26512621
ProtocolDecl *protocol,
@@ -2948,8 +2918,6 @@ size_t ASTContext::getTotalMemory() const {
29482918
getImpl().Allocator.getTotalMemory() +
29492919
getImpl().Cleanups.capacity() +
29502920
llvm::capacity_in_bytes(getImpl().ModuleLoaders) +
2951-
llvm::capacity_in_bytes(getImpl().RawComments) +
2952-
llvm::capacity_in_bytes(getImpl().BriefComments) +
29532921
llvm::capacity_in_bytes(getImpl().ModuleTypes) +
29542922
llvm::capacity_in_bytes(getImpl().GenericParamTypes) +
29552923
// 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)