|
| 1 | +//===--- CompletionOverrideLookup.h ---------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 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 | +#ifndef SWIFT_IDE_COMPLETION_OVERRIDE_LOOKUP_H |
| 14 | +#define SWIFT_IDE_COMPLETION_OVERRIDE_LOOKUP_H |
| 15 | + |
| 16 | +#include "swift/AST/NameLookup.h" |
| 17 | +#include "swift/IDE/CodeCompletion.h" |
| 18 | +#include "swift/Sema/IDETypeChecking.h" |
| 19 | + |
| 20 | +namespace swift { |
| 21 | +namespace ide { |
| 22 | + |
| 23 | +class CompletionOverrideLookup : public swift::VisibleDeclConsumer { |
| 24 | + CodeCompletionResultSink &Sink; |
| 25 | + ASTContext &Ctx; |
| 26 | + const DeclContext *CurrDeclContext; |
| 27 | + SmallVectorImpl<StringRef> &ParsedKeywords; |
| 28 | + SourceLoc introducerLoc; |
| 29 | + |
| 30 | + bool hasFuncIntroducer = false; |
| 31 | + bool hasVarIntroducer = false; |
| 32 | + bool hasTypealiasIntroducer = false; |
| 33 | + bool hasInitializerModifier = false; |
| 34 | + bool hasAccessModifier = false; |
| 35 | + bool hasOverride = false; |
| 36 | + bool hasOverridabilityModifier = false; |
| 37 | + bool hasStaticOrClass = false; |
| 38 | + |
| 39 | +public: |
| 40 | + CompletionOverrideLookup(CodeCompletionResultSink &Sink, ASTContext &Ctx, |
| 41 | + const DeclContext *CurrDeclContext, |
| 42 | + SmallVectorImpl<StringRef> &ParsedKeywords, |
| 43 | + SourceLoc introducerLoc) |
| 44 | + : Sink(Sink), Ctx(Ctx), CurrDeclContext(CurrDeclContext), |
| 45 | + ParsedKeywords(ParsedKeywords), introducerLoc(introducerLoc) { |
| 46 | + hasFuncIntroducer = isKeywordSpecified("func"); |
| 47 | + hasVarIntroducer = isKeywordSpecified("var") || |
| 48 | + isKeywordSpecified("let"); |
| 49 | + hasTypealiasIntroducer = isKeywordSpecified("typealias"); |
| 50 | + hasInitializerModifier = isKeywordSpecified("required") || |
| 51 | + isKeywordSpecified("convenience"); |
| 52 | + hasAccessModifier = isKeywordSpecified("private") || |
| 53 | + isKeywordSpecified("fileprivate") || |
| 54 | + isKeywordSpecified("internal") || |
| 55 | + isKeywordSpecified("public") || |
| 56 | + isKeywordSpecified("open"); |
| 57 | + hasOverride = isKeywordSpecified("override"); |
| 58 | + hasOverridabilityModifier = |
| 59 | + isKeywordSpecified("final") || isKeywordSpecified("open"); |
| 60 | + hasStaticOrClass = isKeywordSpecified(getTokenText(tok::kw_class)) || |
| 61 | + isKeywordSpecified(getTokenText(tok::kw_static)); |
| 62 | + } |
| 63 | + |
| 64 | + bool isKeywordSpecified(StringRef Word) { |
| 65 | + return std::find(ParsedKeywords.begin(), ParsedKeywords.end(), Word) != |
| 66 | + ParsedKeywords.end(); |
| 67 | + } |
| 68 | + |
| 69 | + bool missingOverride(DeclVisibilityKind Reason) { |
| 70 | + return !hasOverride && Reason == DeclVisibilityKind::MemberOfSuper && |
| 71 | + !CurrDeclContext->getSelfProtocolDecl(); |
| 72 | + } |
| 73 | + |
| 74 | + /// Add an access modifier (i.e. `public`) to \p Builder is necessary. |
| 75 | + /// Returns \c true if the modifier is actually added, \c false otherwise. |
| 76 | + bool addAccessControl(const ValueDecl *VD, |
| 77 | + CodeCompletionResultBuilder &Builder); |
| 78 | + |
| 79 | + /// Return type if the result type if \p VD should be represented as opaque |
| 80 | + /// result type. |
| 81 | + Type getOpaqueResultType(const ValueDecl *VD, DeclVisibilityKind Reason, |
| 82 | + DynamicLookupInfo dynamicLookupInfo); |
| 83 | + |
| 84 | + void addValueOverride(const ValueDecl *VD, DeclVisibilityKind Reason, |
| 85 | + DynamicLookupInfo dynamicLookupInfo, |
| 86 | + CodeCompletionResultBuilder &Builder, |
| 87 | + bool hasDeclIntroducer); |
| 88 | + |
| 89 | + void addMethodOverride(const FuncDecl *FD, DeclVisibilityKind Reason, |
| 90 | + DynamicLookupInfo dynamicLookupInfo); |
| 91 | + |
| 92 | + void addVarOverride(const VarDecl *VD, DeclVisibilityKind Reason, |
| 93 | + DynamicLookupInfo dynamicLookupInfo); |
| 94 | + |
| 95 | + void addSubscriptOverride(const SubscriptDecl *SD, DeclVisibilityKind Reason, |
| 96 | + DynamicLookupInfo dynamicLookupInfo); |
| 97 | + |
| 98 | + void addTypeAlias(const AssociatedTypeDecl *ATD, DeclVisibilityKind Reason, |
| 99 | + DynamicLookupInfo dynamicLookupInfo); |
| 100 | + |
| 101 | + void addConstructor(const ConstructorDecl *CD, DeclVisibilityKind Reason, |
| 102 | + DynamicLookupInfo dynamicLookupInfo); |
| 103 | + |
| 104 | + // Implement swift::VisibleDeclConsumer. |
| 105 | + void foundDecl(ValueDecl *D, DeclVisibilityKind Reason, |
| 106 | + DynamicLookupInfo dynamicLookupInfo) override; |
| 107 | + |
| 108 | + void addDesignatedInitializers(NominalTypeDecl *NTD); |
| 109 | + |
| 110 | + void addAssociatedTypes(NominalTypeDecl *NTD); |
| 111 | + |
| 112 | + static StringRef |
| 113 | + getResultBuilderDocComment(ResultBuilderBuildFunction function); |
| 114 | + |
| 115 | + void addResultBuilderBuildCompletion(NominalTypeDecl *builder, |
| 116 | + Type componentType, |
| 117 | + ResultBuilderBuildFunction function); |
| 118 | + |
| 119 | + /// Add completions for the various "build" functions in a result builder. |
| 120 | + void addResultBuilderBuildCompletions(NominalTypeDecl *builder); |
| 121 | + |
| 122 | + void getOverrideCompletions(SourceLoc Loc); |
| 123 | +}; |
| 124 | + |
| 125 | +} // end namespace ide |
| 126 | +} // end namespace swift |
| 127 | + |
| 128 | +#endif // SWIFT_IDE_COMPLETION_OVERRIDE_LOOKUP_H |
0 commit comments