Skip to content

[5.9] Serialize package and SPI doc comments in swiftsourceinfo #65462

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 7 commits into from
May 3, 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
6 changes: 0 additions & 6 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1522,12 +1522,6 @@ class ASTContext final {
Optional<ExternalSourceLocs *> getExternalSourceLocs(const Decl *D);
void setExternalSourceLocs(const Decl *D, ExternalSourceLocs *Locs);

Optional<std::pair<RawComment, bool>> getRawComment(const Decl *D);
void setRawComment(const Decl *D, RawComment RC, bool FromSerialized);

Optional<StringRef> getBriefComment(const Decl *D);
void setBriefComment(const Decl *D, StringRef Comment);

friend TypeBase;
friend ArchetypeType;
friend OpaqueTypeDecl;
Expand Down
36 changes: 22 additions & 14 deletions include/swift/AST/Comment.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,16 @@ class DocComment {
};

/// Get a parsed documentation comment for the declaration, if there is one.
///
/// \param AllowSerialized Allow loading serialized doc comment data, including
/// comment ranges.
DocComment *getSingleDocComment(swift::markup::MarkupContext &Context,
const Decl *D, bool AllowSerialized = false);
const Decl *D);

/// Get the declaration that actually provides a doc comment for another.
///
/// \param AllowSerialized Allow loading serialized doc comment data, including
/// comment ranges.
const Decl *getDocCommentProvidingDecl(const Decl *D,
bool AllowSerialized = false);
const Decl *getDocCommentProvidingDecl(const Decl *D);

/// Attempt to get a doc comment from the declaration, or other inherited
/// sources, like from base classes or protocols.
///
/// \param AllowSerialized Allow loading serialized doc comment data, including
/// comment ranges.
DocComment *getCascadingDocComment(swift::markup::MarkupContext &MC,
const Decl *D,
bool AllowSerialized = false);
const Decl *D);

/// Extract comments parts from the given Markup node.
swift::markup::CommentParts
Expand All @@ -122,6 +111,25 @@ extractCommentParts(swift::markup::MarkupContext &MC,

/// Extract brief comment from \p RC, and print it to \p OS .
void printBriefComment(RawComment RC, llvm::raw_ostream &OS);

/// Describes the intended serialization target for a doc comment.
enum class DocCommentSerializationTarget : uint8_t {
/// The doc comment should not be serialized.
None = 0,

/// The doc comment should only be serialized in the 'swiftsourceinfo'.
SourceInfoOnly,

/// The doc comment should be serialized in both the 'swiftdoc' and
/// 'swiftsourceinfo'.
SwiftDocAndSourceInfo,
};

/// Retrieve the expected serialization target for a documentation comment
/// attached to the given decl.
DocCommentSerializationTarget
getDocCommentSerializationTargetFor(const Decl *D);

} // namespace swift

#endif // LLVM_SWIFT_AST_COMMENT_H
8 changes: 5 additions & 3 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
friend class IterableDeclContext;
friend class MemberLookupTable;
friend class DeclDeserializer;
friend class RawCommentRequest;

private:
llvm::PointerUnion<DeclContext *, ASTContext *> Context;
Expand Down Expand Up @@ -1015,16 +1016,17 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
}

/// \returns the unparsed comment attached to this declaration.
RawComment getRawComment(bool SerializedOK = false) const;
RawComment getRawComment() const;

Optional<StringRef> getGroupName() const;

Optional<StringRef> getSourceFileName() const;

Optional<unsigned> getSourceOrder() const;

/// \returns the brief comment attached to this declaration.
StringRef getBriefComment() const;
/// \returns The brief comment attached to this declaration, or the brief
/// comment attached to the comment providing decl.
StringRef getSemanticBriefComment() const;

/// Returns true if there is a Clang AST node associated
/// with self.
Expand Down
6 changes: 5 additions & 1 deletion include/swift/AST/FileUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,16 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
///
/// This function is an implementation detail for comment serialization.
/// If you just want to get a comment attached to a decl, use
/// \c Decl::getRawComment() or \c Decl::getBriefComment().
/// \c Decl::getRawComment() or \c Decl::getSemanticBriefComment().
virtual Optional<CommentInfo>
getCommentForDecl(const Decl *D) const {
return None;
}

/// For a serialized AST file, returns \c true if an adjacent swiftdoc has been
/// loaded. Otherwise, returns \c false.
virtual bool hasLoadedSwiftDoc() const { return false; }

virtual Optional<StringRef>
getGroupNameForDecl(const Decl *D) const {
return None;
Expand Down
39 changes: 39 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -4188,6 +4188,45 @@ class LocalDiscriminatorsRequest
bool isCached() const { return true; }
};

/// Retrieve the raw documentation comment of a declaration.
class RawCommentRequest
: public SimpleRequest<RawCommentRequest,
RawComment(const Decl *),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
RawComment evaluate(Evaluator &evaluator, const Decl *D) const;

public:
// Separate caching.
bool isCached() const { return true; }
};

/// Retrieve the brief portion of a declaration's document comment, potentially
/// walking to find the comment providing decl if needed.
class SemanticBriefCommentRequest
: public SimpleRequest<SemanticBriefCommentRequest,
StringRef(const Decl *),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
StringRef evaluate(Evaluator &evaluator, const Decl *D) const;

public:
// Separate caching.
bool isCached() const { return true; }
};

/// Checks that all of a class's \c \@objcImplementation extensions provide
/// complete and correct implementations for their corresponding interfaces.
/// This is done on all of a class's implementations at once to improve diagnostics.
Expand Down
6 changes: 6 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,12 @@ SWIFT_REQUEST(TypeChecker, GetRuntimeDiscoverableAttributes,
SWIFT_REQUEST(TypeChecker, LocalDiscriminatorsRequest,
unsigned(DeclContext *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, RawCommentRequest,
RawComment(const Decl *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, SemanticBriefCommentRequest,
StringRef(const Decl *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, IsNonUserModuleRequest,
bool(ModuleDecl *),
Cached, NoLocationInfo)
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Serialization/SerializedModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ class SerializedASTFile final : public LoadedFile {

Optional<CommentInfo> getCommentForDecl(const Decl *D) const override;

bool hasLoadedSwiftDoc() const override;

Optional<StringRef> getGroupNameForDecl(const Decl *D) const override;


Expand Down
32 changes: 0 additions & 32 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,6 @@ struct ASTContext::Implementation {
/// actual \c SourceLocs that require opening their external buffer.
llvm::DenseMap<const Decl *, ExternalSourceLocs *> ExternalSourceLocs;

/// Map from Swift declarations to raw comments.
llvm::DenseMap<const Decl *, std::pair<RawComment, bool>> RawComments;

/// Map from Swift declarations to brief comments.
llvm::DenseMap<const Decl *, StringRef> BriefComments;

/// Map from declarations to foreign error conventions.
/// This applies to both actual imported functions and to @objc functions.
llvm::DenseMap<const AbstractFunctionDecl *,
Expand Down Expand Up @@ -2596,30 +2590,6 @@ void ASTContext::setExternalSourceLocs(const Decl *D,
getImpl().ExternalSourceLocs[D] = Locs;
}

Optional<std::pair<RawComment, bool>> ASTContext::getRawComment(const Decl *D) {
auto Known = getImpl().RawComments.find(D);
if (Known == getImpl().RawComments.end())
return None;

return Known->second;
}

void ASTContext::setRawComment(const Decl *D, RawComment RC, bool FromSerialized) {
getImpl().RawComments[D] = std::make_pair(RC, FromSerialized);
}

Optional<StringRef> ASTContext::getBriefComment(const Decl *D) {
auto Known = getImpl().BriefComments.find(D);
if (Known == getImpl().BriefComments.end())
return None;

return Known->second;
}

void ASTContext::setBriefComment(const Decl *D, StringRef Comment) {
getImpl().BriefComments[D] = Comment;
}

NormalProtocolConformance *
ASTContext::getConformance(Type conformingType,
ProtocolDecl *protocol,
Expand Down Expand Up @@ -2922,8 +2892,6 @@ size_t ASTContext::getTotalMemory() const {
getImpl().Allocator.getTotalMemory() +
getImpl().Cleanups.capacity() +
llvm::capacity_in_bytes(getImpl().ModuleLoaders) +
llvm::capacity_in_bytes(getImpl().RawComments) +
llvm::capacity_in_bytes(getImpl().BriefComments) +
llvm::capacity_in_bytes(getImpl().ModuleTypes) +
llvm::capacity_in_bytes(getImpl().GenericParamTypes) +
// getImpl().GenericFunctionTypes ?
Expand Down
Loading