Skip to content

Commit 1bc9685

Browse files
authored
Merge pull request #82464 from a7medev/feat/full-documentation-in-code-completion
[IDE] Add full documentation to code completion result
1 parent a1d18cf commit 1bc9685

File tree

56 files changed

+1427
-282
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1427
-282
lines changed

include/swift/AST/ASTDemangler.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
#include <optional>
3333

3434
namespace swift {
35-
35+
36+
class Decl;
3637
class TypeDecl;
38+
class DeclName;
3739

3840
namespace Demangle {
3941
SWIFT_BEGIN_INLINE_NAMESPACE
@@ -50,6 +52,9 @@ TypeDecl *getTypeDeclForUSR(ASTContext &ctx,
5052
llvm::StringRef usr,
5153
GenericSignature genericSig=GenericSignature());
5254

55+
Decl *getDeclForUSR(ASTContext &ctx, llvm::StringRef usr,
56+
GenericSignature genericSig = GenericSignature());
57+
5358
/// An implementation of MetadataReader's BuilderType concept that
5459
/// just finds and builds things in the AST.
5560
class ASTBuilder {
@@ -122,6 +127,17 @@ class ASTBuilder {
122127

123128
Demangle::NodeFactory &getNodeFactory() { return Factory; }
124129

130+
/// Finds the \c Decl associated with the provided \p node.
131+
/// Attempts to find a type declaration using \c createTypeDecl, if not found,
132+
/// it performs a lookup for the declaration and returns the first declaration
133+
/// for which \c isMatchingValueDecl returns true.
134+
///
135+
/// \note \p isMatchingValueDecl is not evaluated for type declarations, it's
136+
/// only used to choose among lookup results when \c createTypeDecl fails.
137+
Decl *
138+
findDecl(NodePointer node,
139+
llvm::function_ref<bool(const ValueDecl *)> isMatchingValueDecl);
140+
125141
Type decodeMangledType(NodePointer node, bool forRequirement = true);
126142
Type createBuiltinType(StringRef builtinName, StringRef mangledName);
127143

@@ -286,7 +302,8 @@ class ASTBuilder {
286302
/// The module name encoded in the node is either the module's real or ABI
287303
/// name. Multiple modules can share the same name. This function returns
288304
/// all modules that contain that name.
289-
llvm::ArrayRef<ModuleDecl *> findPotentialModules(NodePointer node);
305+
llvm::ArrayRef<ModuleDecl *> findPotentialModules(NodePointer node,
306+
ModuleDecl *&scratch);
290307

291308
Demangle::NodePointer findModuleNode(NodePointer node);
292309

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,6 +2917,10 @@ class ValueDecl : public Decl {
29172917
/// a null pointer.
29182918
unsigned noOpaqueResultType : 1;
29192919

2920+
/// Whether the ClangUSRGenerationRequest request was evaluated and produced
2921+
/// a std::nullopt.
2922+
unsigned noClangUSR : 1;
2923+
29202924
/// Whether the "isFinal" bit has been computed yet.
29212925
unsigned isFinalComputed : 1;
29222926

@@ -2951,6 +2955,7 @@ class ValueDecl : public Decl {
29512955
friend class OpaqueResultTypeRequest;
29522956
friend class ApplyAccessNoteRequest;
29532957
friend class AvailabilityDomainForDeclRequest;
2958+
friend class ClangUSRGenerationRequest;
29542959

29552960
friend class Decl;
29562961
SourceLoc getLocFromSource() const { return NameLoc; }
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===--- DeclNameExtractor.h - Decl Name Demangling -------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the DeclNameExtractor utility.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#include "swift/AST/ASTContext.h"
18+
#include "swift/AST/Identifier.h"
19+
20+
namespace swift {
21+
namespace Demangle {
22+
23+
class Node;
24+
25+
class DeclNameExtractor {
26+
private:
27+
ASTContext &Ctx;
28+
29+
public:
30+
DeclNameExtractor(ASTContext &ctx) : Ctx(ctx) {}
31+
32+
/// Extract a DeclName from a demangling node
33+
/// \returns true if a \c DeclName is found, false otherwise.
34+
bool extractDeclName(Node *node, DeclName &name,
35+
Identifier &privateDiscriminator);
36+
37+
private:
38+
bool extractIdentifierName(Node *node, DeclName &declName,
39+
Identifier &privateDiscriminator);
40+
bool extractFunctionLikeName(Node *node, DeclName &declName,
41+
Identifier &privateDiscriminator);
42+
void extractArgLabelsFromLabelList(Node *LabelList,
43+
SmallVectorImpl<Identifier> &ArgLabels);
44+
void extractArgLabelsFromType(Node *Type,
45+
SmallVectorImpl<Identifier> &ArgLabels);
46+
DeclBaseName extractOperatorName(Node *node);
47+
};
48+
49+
/// Returns an identifier with the given name, automatically removing any
50+
/// surrounding backticks that are present for raw identifiers.
51+
Identifier getIdentifier(ASTContext &Ctx, StringRef name);
52+
53+
bool extractNameNodeInfo(ASTContext &Ctx, Node *node, StringRef &name,
54+
StringRef &relatedEntityKind,
55+
Identifier &privateDiscriminator);
56+
57+
} // namespace Demangle
58+
} // namespace swift

include/swift/AST/TypeCheckRequests.h

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -757,20 +757,70 @@ class RequirementRequest :
757757
bool isCached() const;
758758
};
759759

760+
/// Generate the Clang USR for the given declaration.
761+
class ClangUSRGenerationRequest
762+
: public SimpleRequest<ClangUSRGenerationRequest,
763+
std::optional<std::string>(const ValueDecl *),
764+
RequestFlags::SeparatelyCached |
765+
RequestFlags::SplitCached> {
766+
public:
767+
using SimpleRequest::SimpleRequest;
768+
769+
private:
770+
friend SimpleRequest;
771+
772+
// Evaluation.
773+
std::optional<std::string> evaluate(Evaluator &eval,
774+
const ValueDecl *d) const;
775+
776+
public:
777+
// Split caching.
778+
bool isCached() const { return true; }
779+
std::optional<std::optional<std::string>> getCachedResult() const;
780+
void cacheResult(std::optional<std::string> result) const;
781+
};
782+
783+
/// Generate the Swift USR for the given declaration.
784+
class SwiftUSRGenerationRequest
785+
: public SimpleRequest<SwiftUSRGenerationRequest,
786+
std::string(const ValueDecl *),
787+
RequestFlags::Cached> {
788+
public:
789+
using SimpleRequest::SimpleRequest;
790+
791+
private:
792+
friend SimpleRequest;
793+
794+
// Evaluation.
795+
std::string evaluate(Evaluator &eval, const ValueDecl *d) const;
796+
797+
public:
798+
// Caching
799+
bool isCached() const { return true; }
800+
};
801+
760802
struct USRGenerationOptions {
761803
/// @brief Whether to emit USRs using the Swift declaration when it is
762804
/// synthesized from a Clang based declaration. Useful in cases where Swift
763805
/// declarations are synthesized from Clang nodes but the caller actually
764806
/// wants the USR of the Swift declaration.
765807
bool distinguishSynthesizedDecls;
766808

809+
/// @brief Whether to emit USRs using the Swift declaration for all
810+
/// declarations specifically, emits a Swift USR for all Clang-based
811+
/// declarations.
812+
bool useSwiftUSR;
813+
767814
friend llvm::hash_code hash_value(const USRGenerationOptions &options) {
768-
return llvm::hash_value(options.distinguishSynthesizedDecls);
815+
return llvm::hash_combine(
816+
llvm::hash_value(options.distinguishSynthesizedDecls),
817+
llvm::hash_value(options.useSwiftUSR));
769818
}
770819

771820
friend bool operator==(const USRGenerationOptions &lhs,
772821
const USRGenerationOptions &rhs) {
773-
return lhs.distinguishSynthesizedDecls == rhs.distinguishSynthesizedDecls;
822+
return lhs.distinguishSynthesizedDecls == rhs.distinguishSynthesizedDecls &&
823+
lhs.useSwiftUSR == rhs.useSwiftUSR;
774824
}
775825

776826
friend bool operator!=(const USRGenerationOptions &lhs,
@@ -783,10 +833,12 @@ void simple_display(llvm::raw_ostream &out,
783833
const USRGenerationOptions &options);
784834

785835
/// Generate the USR for the given declaration.
836+
/// This is an umbrella request that forwards to ClangUSRGenerationRequest or
837+
/// SwiftUSRGenerationRequest.
786838
class USRGenerationRequest
787839
: public SimpleRequest<USRGenerationRequest,
788840
std::string(const ValueDecl *, USRGenerationOptions),
789-
RequestFlags::Cached> {
841+
RequestFlags::Uncached> {
790842
public:
791843
using SimpleRequest::SimpleRequest;
792844

@@ -796,10 +848,6 @@ class USRGenerationRequest
796848
// Evaluation.
797849
std::string evaluate(Evaluator &eval, const ValueDecl *d,
798850
USRGenerationOptions options) const;
799-
800-
public:
801-
// Caching
802-
bool isCached() const { return true; }
803851
};
804852

805853
/// Generate the mangling for the given local type declaration.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,15 @@ SWIFT_REQUEST(TypeChecker, TypeCheckASTNodeAtLocRequest,
364364
Uncached, NoLocationInfo)
365365
SWIFT_REQUEST(TypeChecker, UnderlyingTypeRequest, Type(TypeAliasDecl *),
366366
SeparatelyCached, NoLocationInfo)
367+
SWIFT_REQUEST(TypeChecker, ClangUSRGenerationRequest,
368+
std::optional<std::string>(const ValueDecl *),
369+
SeparatelyCached | SplitCached, NoLocationInfo)
370+
SWIFT_REQUEST(TypeChecker, SwiftUSRGenerationRequest,
371+
std::string(const ValueDecl *),
372+
Cached, NoLocationInfo)
367373
SWIFT_REQUEST(TypeChecker, USRGenerationRequest,
368374
std::string(const ValueDecl *, USRGenerationOptions),
369-
Cached, NoLocationInfo)
375+
Uncached, NoLocationInfo)
370376
SWIFT_REQUEST(TypeChecker, IsABICompatibleOverrideRequest,
371377
bool(ValueDecl *), Cached, NoLocationInfo)
372378
SWIFT_REQUEST(TypeChecker, IsStaticRequest,

include/swift/AST/USRGeneration.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,20 @@ bool printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS);
4545
/// Prints out the USR for the given ValueDecl.
4646
/// @param distinguishSynthesizedDecls Whether to use the USR of the
4747
/// synthesized declaration instead of the USR of the underlying Clang USR.
48+
/// @param useSwiftUSR Whether to generate a Swift USR for all Clang
49+
/// declarations as well.
4850
/// \returns true if it failed, false on success.
4951
bool printValueDeclUSR(const ValueDecl *D, raw_ostream &OS,
50-
bool distinguishSynthesizedDecls = false);
52+
bool distinguishSynthesizedDecls = false,
53+
bool useSwiftUSR = false);
54+
55+
/// Prints out the Swift USR for the given ValueDecl regardless of its source
56+
/// (Swift or Clang). Equivalent to `printValueDeclUSR(D, OS, false,
57+
/// /*useSwiftUSR=*/true)`
58+
inline bool printValueDeclSwiftUSR(const ValueDecl *D, raw_ostream &OS) {
59+
return printValueDeclUSR(D, OS, /*distinguishSynthesizedDecls=*/false,
60+
/*useSwiftUSR=*/true);
61+
}
5162

5263
/// Prints out the USR for the given ModuleEntity.
5364
/// In case module aliasing is used, it prints the real module name. For example,

include/swift/IDE/CodeCompletionContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class CodeCompletionContext {
8686
return CurrentResults.addCallWithNoDefaultArgs;
8787
}
8888

89+
void setVerifyUSRToDecl(bool flag) { CurrentResults.verifyUSRToDecl = flag; }
90+
bool verifyUSRToDecl() const { return CurrentResults.verifyUSRToDecl; }
91+
8992
/// Allocate a string owned by the code completion context.
9093
StringRef copyString(StringRef Str) {
9194
return Str.copy(*CurrentResults.Allocator);

0 commit comments

Comments
 (0)