xref: /openbsd-src/gnu/llvm/clang/include/clang/Sema/CodeCompleteConsumer.h (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===- CodeCompleteConsumer.h - Code Completion Interface -------*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick //  This file defines the CodeCompleteConsumer class.
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12e5dd7070Spatrick 
13e5dd7070Spatrick #ifndef LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
14e5dd7070Spatrick #define LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
15e5dd7070Spatrick 
16e5dd7070Spatrick #include "clang-c/Index.h"
17e5dd7070Spatrick #include "clang/AST/Type.h"
18e5dd7070Spatrick #include "clang/Basic/LLVM.h"
19e5dd7070Spatrick #include "clang/Lex/MacroInfo.h"
20e5dd7070Spatrick #include "clang/Sema/CodeCompleteOptions.h"
21e5dd7070Spatrick #include "clang/Sema/DeclSpec.h"
22e5dd7070Spatrick #include "llvm/ADT/ArrayRef.h"
23e5dd7070Spatrick #include "llvm/ADT/DenseMap.h"
24e5dd7070Spatrick #include "llvm/ADT/SmallPtrSet.h"
25e5dd7070Spatrick #include "llvm/ADT/SmallVector.h"
26e5dd7070Spatrick #include "llvm/ADT/StringRef.h"
27e5dd7070Spatrick #include "llvm/Support/Allocator.h"
28e5dd7070Spatrick #include "llvm/Support/type_traits.h"
29e5dd7070Spatrick #include <cassert>
30e5dd7070Spatrick #include <memory>
31*12c85518Srobert #include <optional>
32e5dd7070Spatrick #include <string>
33e5dd7070Spatrick #include <utility>
34e5dd7070Spatrick 
35e5dd7070Spatrick namespace clang {
36e5dd7070Spatrick 
37e5dd7070Spatrick class ASTContext;
38e5dd7070Spatrick class Decl;
39e5dd7070Spatrick class DeclContext;
40e5dd7070Spatrick class FunctionDecl;
41e5dd7070Spatrick class FunctionTemplateDecl;
42e5dd7070Spatrick class IdentifierInfo;
43e5dd7070Spatrick class LangOptions;
44e5dd7070Spatrick class NamedDecl;
45e5dd7070Spatrick class NestedNameSpecifier;
46e5dd7070Spatrick class Preprocessor;
47e5dd7070Spatrick class RawComment;
48e5dd7070Spatrick class Sema;
49e5dd7070Spatrick class UsingShadowDecl;
50e5dd7070Spatrick 
51e5dd7070Spatrick /// Default priority values for code-completion results based
52e5dd7070Spatrick /// on their kind.
53e5dd7070Spatrick enum {
54e5dd7070Spatrick   /// Priority for the next initialization in a constructor initializer
55e5dd7070Spatrick   /// list.
56e5dd7070Spatrick   CCP_NextInitializer = 7,
57e5dd7070Spatrick 
58e5dd7070Spatrick   /// Priority for an enumeration constant inside a switch whose
59e5dd7070Spatrick   /// condition is of the enumeration type.
60e5dd7070Spatrick   CCP_EnumInCase = 7,
61e5dd7070Spatrick 
62e5dd7070Spatrick   /// Priority for a send-to-super completion.
63e5dd7070Spatrick   CCP_SuperCompletion = 20,
64e5dd7070Spatrick 
65e5dd7070Spatrick   /// Priority for a declaration that is in the local scope.
66e5dd7070Spatrick   CCP_LocalDeclaration = 34,
67e5dd7070Spatrick 
68e5dd7070Spatrick   /// Priority for a member declaration found from the current
69e5dd7070Spatrick   /// method or member function.
70e5dd7070Spatrick   CCP_MemberDeclaration = 35,
71e5dd7070Spatrick 
72e5dd7070Spatrick   /// Priority for a language keyword (that isn't any of the other
73e5dd7070Spatrick   /// categories).
74e5dd7070Spatrick   CCP_Keyword = 40,
75e5dd7070Spatrick 
76e5dd7070Spatrick   /// Priority for a code pattern.
77e5dd7070Spatrick   CCP_CodePattern = 40,
78e5dd7070Spatrick 
79e5dd7070Spatrick   /// Priority for a non-type declaration.
80e5dd7070Spatrick   CCP_Declaration = 50,
81e5dd7070Spatrick 
82e5dd7070Spatrick   /// Priority for a type.
83e5dd7070Spatrick   CCP_Type = CCP_Declaration,
84e5dd7070Spatrick 
85e5dd7070Spatrick   /// Priority for a constant value (e.g., enumerator).
86e5dd7070Spatrick   CCP_Constant = 65,
87e5dd7070Spatrick 
88e5dd7070Spatrick   /// Priority for a preprocessor macro.
89e5dd7070Spatrick   CCP_Macro = 70,
90e5dd7070Spatrick 
91e5dd7070Spatrick   /// Priority for a nested-name-specifier.
92e5dd7070Spatrick   CCP_NestedNameSpecifier = 75,
93e5dd7070Spatrick 
94e5dd7070Spatrick   /// Priority for a result that isn't likely to be what the user wants,
95e5dd7070Spatrick   /// but is included for completeness.
96e5dd7070Spatrick   CCP_Unlikely = 80,
97e5dd7070Spatrick 
98e5dd7070Spatrick   /// Priority for the Objective-C "_cmd" implicit parameter.
99e5dd7070Spatrick   CCP_ObjC_cmd = CCP_Unlikely
100e5dd7070Spatrick };
101e5dd7070Spatrick 
102e5dd7070Spatrick /// Priority value deltas that are added to code-completion results
103e5dd7070Spatrick /// based on the context of the result.
104e5dd7070Spatrick enum {
105e5dd7070Spatrick   /// The result is in a base class.
106e5dd7070Spatrick   CCD_InBaseClass = 2,
107e5dd7070Spatrick 
108e5dd7070Spatrick   /// The result is a C++ non-static member function whose qualifiers
109e5dd7070Spatrick   /// exactly match the object type on which the member function can be called.
110e5dd7070Spatrick   CCD_ObjectQualifierMatch = -1,
111e5dd7070Spatrick 
112e5dd7070Spatrick   /// The selector of the given message exactly matches the selector
113e5dd7070Spatrick   /// of the current method, which might imply that some kind of delegation
114e5dd7070Spatrick   /// is occurring.
115e5dd7070Spatrick   CCD_SelectorMatch = -3,
116e5dd7070Spatrick 
117e5dd7070Spatrick   /// Adjustment to the "bool" type in Objective-C, where the typedef
118e5dd7070Spatrick   /// "BOOL" is preferred.
119e5dd7070Spatrick   CCD_bool_in_ObjC = 1,
120e5dd7070Spatrick 
121e5dd7070Spatrick   /// Adjustment for KVC code pattern priorities when it doesn't look
122e5dd7070Spatrick   /// like the
123e5dd7070Spatrick   CCD_ProbablyNotObjCCollection = 15,
124e5dd7070Spatrick 
125e5dd7070Spatrick   /// An Objective-C method being used as a property.
126e5dd7070Spatrick   CCD_MethodAsProperty = 2,
127e5dd7070Spatrick 
128e5dd7070Spatrick   /// An Objective-C block property completed as a setter with a
129e5dd7070Spatrick   /// block placeholder.
130e5dd7070Spatrick   CCD_BlockPropertySetter = 3
131e5dd7070Spatrick };
132e5dd7070Spatrick 
133e5dd7070Spatrick /// Priority value factors by which we will divide or multiply the
134e5dd7070Spatrick /// priority of a code-completion result.
135e5dd7070Spatrick enum {
136e5dd7070Spatrick   /// Divide by this factor when a code-completion result's type exactly
137e5dd7070Spatrick   /// matches the type we expect.
138e5dd7070Spatrick   CCF_ExactTypeMatch = 4,
139e5dd7070Spatrick 
140e5dd7070Spatrick   /// Divide by this factor when a code-completion result's type is
141e5dd7070Spatrick   /// similar to the type we expect (e.g., both arithmetic types, both
142e5dd7070Spatrick   /// Objective-C object pointer types).
143e5dd7070Spatrick   CCF_SimilarTypeMatch = 2
144e5dd7070Spatrick };
145e5dd7070Spatrick 
146e5dd7070Spatrick /// A simplified classification of types used when determining
147e5dd7070Spatrick /// "similar" types for code completion.
148e5dd7070Spatrick enum SimplifiedTypeClass {
149e5dd7070Spatrick   STC_Arithmetic,
150e5dd7070Spatrick   STC_Array,
151e5dd7070Spatrick   STC_Block,
152e5dd7070Spatrick   STC_Function,
153e5dd7070Spatrick   STC_ObjectiveC,
154e5dd7070Spatrick   STC_Other,
155e5dd7070Spatrick   STC_Pointer,
156e5dd7070Spatrick   STC_Record,
157e5dd7070Spatrick   STC_Void
158e5dd7070Spatrick };
159e5dd7070Spatrick 
160e5dd7070Spatrick /// Determine the simplified type class of the given canonical type.
161e5dd7070Spatrick SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T);
162e5dd7070Spatrick 
163e5dd7070Spatrick /// Determine the type that this declaration will have if it is used
164e5dd7070Spatrick /// as a type or in an expression.
165e5dd7070Spatrick QualType getDeclUsageType(ASTContext &C, const NamedDecl *ND);
166e5dd7070Spatrick 
167e5dd7070Spatrick /// Determine the priority to be given to a macro code completion result
168e5dd7070Spatrick /// with the given name.
169e5dd7070Spatrick ///
170e5dd7070Spatrick /// \param MacroName The name of the macro.
171e5dd7070Spatrick ///
172e5dd7070Spatrick /// \param LangOpts Options describing the current language dialect.
173e5dd7070Spatrick ///
174e5dd7070Spatrick /// \param PreferredTypeIsPointer Whether the preferred type for the context
175e5dd7070Spatrick /// of this macro is a pointer type.
176e5dd7070Spatrick unsigned getMacroUsagePriority(StringRef MacroName,
177e5dd7070Spatrick                                const LangOptions &LangOpts,
178e5dd7070Spatrick                                bool PreferredTypeIsPointer = false);
179e5dd7070Spatrick 
180e5dd7070Spatrick /// Determine the libclang cursor kind associated with the given
181e5dd7070Spatrick /// declaration.
182e5dd7070Spatrick CXCursorKind getCursorKindForDecl(const Decl *D);
183e5dd7070Spatrick 
184e5dd7070Spatrick /// The context in which code completion occurred, so that the
185e5dd7070Spatrick /// code-completion consumer can process the results accordingly.
186e5dd7070Spatrick class CodeCompletionContext {
187e5dd7070Spatrick public:
188e5dd7070Spatrick   enum Kind {
189e5dd7070Spatrick     /// An unspecified code-completion context.
190e5dd7070Spatrick     CCC_Other,
191e5dd7070Spatrick 
192e5dd7070Spatrick     /// An unspecified code-completion context where we should also add
193e5dd7070Spatrick     /// macro completions.
194e5dd7070Spatrick     CCC_OtherWithMacros,
195e5dd7070Spatrick 
196e5dd7070Spatrick     /// Code completion occurred within a "top-level" completion context,
197e5dd7070Spatrick     /// e.g., at namespace or global scope.
198e5dd7070Spatrick     CCC_TopLevel,
199e5dd7070Spatrick 
200e5dd7070Spatrick     /// Code completion occurred within an Objective-C interface,
201e5dd7070Spatrick     /// protocol, or category interface.
202e5dd7070Spatrick     CCC_ObjCInterface,
203e5dd7070Spatrick 
204e5dd7070Spatrick     /// Code completion occurred within an Objective-C implementation
205e5dd7070Spatrick     /// or category implementation.
206e5dd7070Spatrick     CCC_ObjCImplementation,
207e5dd7070Spatrick 
208e5dd7070Spatrick     /// Code completion occurred within the instance variable list of
209e5dd7070Spatrick     /// an Objective-C interface, implementation, or category implementation.
210e5dd7070Spatrick     CCC_ObjCIvarList,
211e5dd7070Spatrick 
212e5dd7070Spatrick     /// Code completion occurred within a class, struct, or union.
213e5dd7070Spatrick     CCC_ClassStructUnion,
214e5dd7070Spatrick 
215e5dd7070Spatrick     /// Code completion occurred where a statement (or declaration) is
216e5dd7070Spatrick     /// expected in a function, method, or block.
217e5dd7070Spatrick     CCC_Statement,
218e5dd7070Spatrick 
219e5dd7070Spatrick     /// Code completion occurred where an expression is expected.
220e5dd7070Spatrick     CCC_Expression,
221e5dd7070Spatrick 
222e5dd7070Spatrick     /// Code completion occurred where an Objective-C message receiver
223e5dd7070Spatrick     /// is expected.
224e5dd7070Spatrick     CCC_ObjCMessageReceiver,
225e5dd7070Spatrick 
226e5dd7070Spatrick     /// Code completion occurred on the right-hand side of a member
227e5dd7070Spatrick     /// access expression using the dot operator.
228e5dd7070Spatrick     ///
229e5dd7070Spatrick     /// The results of this completion are the members of the type being
230e5dd7070Spatrick     /// accessed. The type itself is available via
231e5dd7070Spatrick     /// \c CodeCompletionContext::getType().
232e5dd7070Spatrick     CCC_DotMemberAccess,
233e5dd7070Spatrick 
234e5dd7070Spatrick     /// Code completion occurred on the right-hand side of a member
235e5dd7070Spatrick     /// access expression using the arrow operator.
236e5dd7070Spatrick     ///
237e5dd7070Spatrick     /// The results of this completion are the members of the type being
238e5dd7070Spatrick     /// accessed. The type itself is available via
239e5dd7070Spatrick     /// \c CodeCompletionContext::getType().
240e5dd7070Spatrick     CCC_ArrowMemberAccess,
241e5dd7070Spatrick 
242e5dd7070Spatrick     /// Code completion occurred on the right-hand side of an Objective-C
243e5dd7070Spatrick     /// property access expression.
244e5dd7070Spatrick     ///
245e5dd7070Spatrick     /// The results of this completion are the members of the type being
246e5dd7070Spatrick     /// accessed. The type itself is available via
247e5dd7070Spatrick     /// \c CodeCompletionContext::getType().
248e5dd7070Spatrick     CCC_ObjCPropertyAccess,
249e5dd7070Spatrick 
250e5dd7070Spatrick     /// Code completion occurred after the "enum" keyword, to indicate
251e5dd7070Spatrick     /// an enumeration name.
252e5dd7070Spatrick     CCC_EnumTag,
253e5dd7070Spatrick 
254e5dd7070Spatrick     /// Code completion occurred after the "union" keyword, to indicate
255e5dd7070Spatrick     /// a union name.
256e5dd7070Spatrick     CCC_UnionTag,
257e5dd7070Spatrick 
258e5dd7070Spatrick     /// Code completion occurred after the "struct" or "class" keyword,
259e5dd7070Spatrick     /// to indicate a struct or class name.
260e5dd7070Spatrick     CCC_ClassOrStructTag,
261e5dd7070Spatrick 
262e5dd7070Spatrick     /// Code completion occurred where a protocol name is expected.
263e5dd7070Spatrick     CCC_ObjCProtocolName,
264e5dd7070Spatrick 
265e5dd7070Spatrick     /// Code completion occurred where a namespace or namespace alias
266e5dd7070Spatrick     /// is expected.
267e5dd7070Spatrick     CCC_Namespace,
268e5dd7070Spatrick 
269e5dd7070Spatrick     /// Code completion occurred where a type name is expected.
270e5dd7070Spatrick     CCC_Type,
271e5dd7070Spatrick 
272e5dd7070Spatrick     /// Code completion occurred where a new name is expected.
273e5dd7070Spatrick     CCC_NewName,
274e5dd7070Spatrick 
275e5dd7070Spatrick     /// Code completion occurred where both a new name and an existing symbol is
276e5dd7070Spatrick     /// permissible.
277e5dd7070Spatrick     CCC_SymbolOrNewName,
278e5dd7070Spatrick 
279e5dd7070Spatrick     /// Code completion occurred where an existing name(such as type, function
280e5dd7070Spatrick     /// or variable) is expected.
281e5dd7070Spatrick     CCC_Symbol,
282e5dd7070Spatrick 
283e5dd7070Spatrick     /// Code completion occurred where an macro is being defined.
284e5dd7070Spatrick     CCC_MacroName,
285e5dd7070Spatrick 
286e5dd7070Spatrick     /// Code completion occurred where a macro name is expected
287e5dd7070Spatrick     /// (without any arguments, in the case of a function-like macro).
288e5dd7070Spatrick     CCC_MacroNameUse,
289e5dd7070Spatrick 
290e5dd7070Spatrick     /// Code completion occurred within a preprocessor expression.
291e5dd7070Spatrick     CCC_PreprocessorExpression,
292e5dd7070Spatrick 
293e5dd7070Spatrick     /// Code completion occurred where a preprocessor directive is
294e5dd7070Spatrick     /// expected.
295e5dd7070Spatrick     CCC_PreprocessorDirective,
296e5dd7070Spatrick 
297e5dd7070Spatrick     /// Code completion occurred in a context where natural language is
298e5dd7070Spatrick     /// expected, e.g., a comment or string literal.
299e5dd7070Spatrick     ///
300e5dd7070Spatrick     /// This context usually implies that no completions should be added,
301e5dd7070Spatrick     /// unless they come from an appropriate natural-language dictionary.
302e5dd7070Spatrick     CCC_NaturalLanguage,
303e5dd7070Spatrick 
304e5dd7070Spatrick     /// Code completion for a selector, as in an \@selector expression.
305e5dd7070Spatrick     CCC_SelectorName,
306e5dd7070Spatrick 
307e5dd7070Spatrick     /// Code completion within a type-qualifier list.
308e5dd7070Spatrick     CCC_TypeQualifiers,
309e5dd7070Spatrick 
310e5dd7070Spatrick     /// Code completion in a parenthesized expression, which means that
311e5dd7070Spatrick     /// we may also have types here in C and Objective-C (as well as in C++).
312e5dd7070Spatrick     CCC_ParenthesizedExpression,
313e5dd7070Spatrick 
314e5dd7070Spatrick     /// Code completion where an Objective-C instance message is
315e5dd7070Spatrick     /// expected.
316e5dd7070Spatrick     CCC_ObjCInstanceMessage,
317e5dd7070Spatrick 
318e5dd7070Spatrick     /// Code completion where an Objective-C class message is expected.
319e5dd7070Spatrick     CCC_ObjCClassMessage,
320e5dd7070Spatrick 
321e5dd7070Spatrick     /// Code completion where the name of an Objective-C class is
322e5dd7070Spatrick     /// expected.
323e5dd7070Spatrick     CCC_ObjCInterfaceName,
324e5dd7070Spatrick 
325e5dd7070Spatrick     /// Code completion where an Objective-C category name is expected.
326e5dd7070Spatrick     CCC_ObjCCategoryName,
327e5dd7070Spatrick 
328e5dd7070Spatrick     /// Code completion inside the filename part of a #include directive.
329e5dd7070Spatrick     CCC_IncludedFile,
330e5dd7070Spatrick 
331*12c85518Srobert     /// Code completion of an attribute name.
332*12c85518Srobert     CCC_Attribute,
333*12c85518Srobert 
334e5dd7070Spatrick     /// An unknown context, in which we are recovering from a parsing
335e5dd7070Spatrick     /// error and don't know which completions we should give.
336e5dd7070Spatrick     CCC_Recovery
337e5dd7070Spatrick   };
338e5dd7070Spatrick 
339e5dd7070Spatrick   using VisitedContextSet = llvm::SmallPtrSet<DeclContext *, 8>;
340e5dd7070Spatrick 
341e5dd7070Spatrick private:
342e5dd7070Spatrick   Kind CCKind;
343e5dd7070Spatrick 
344e5dd7070Spatrick   /// Indicates whether we are completing a name of a using declaration, e.g.
345e5dd7070Spatrick   ///     using ^;
346e5dd7070Spatrick   ///     using a::^;
347e5dd7070Spatrick   bool IsUsingDeclaration;
348e5dd7070Spatrick 
349e5dd7070Spatrick   /// The type that would prefer to see at this point (e.g., the type
350e5dd7070Spatrick   /// of an initializer or function parameter).
351e5dd7070Spatrick   QualType PreferredType;
352e5dd7070Spatrick 
353e5dd7070Spatrick   /// The type of the base object in a member access expression.
354e5dd7070Spatrick   QualType BaseType;
355e5dd7070Spatrick 
356e5dd7070Spatrick   /// The identifiers for Objective-C selector parts.
357e5dd7070Spatrick   ArrayRef<IdentifierInfo *> SelIdents;
358e5dd7070Spatrick 
359e5dd7070Spatrick   /// The scope specifier that comes before the completion token e.g.
360e5dd7070Spatrick   /// "a::b::"
361*12c85518Srobert   std::optional<CXXScopeSpec> ScopeSpecifier;
362e5dd7070Spatrick 
363e5dd7070Spatrick   /// A set of declaration contexts visited by Sema when doing lookup for
364e5dd7070Spatrick   /// code completion.
365e5dd7070Spatrick   VisitedContextSet VisitedContexts;
366e5dd7070Spatrick 
367e5dd7070Spatrick public:
368e5dd7070Spatrick   /// Construct a new code-completion context of the given kind.
CodeCompletionContext(Kind CCKind)369e5dd7070Spatrick   CodeCompletionContext(Kind CCKind)
370*12c85518Srobert       : CCKind(CCKind), IsUsingDeclaration(false), SelIdents(std::nullopt) {}
371e5dd7070Spatrick 
372e5dd7070Spatrick   /// Construct a new code-completion context of the given kind.
373e5dd7070Spatrick   CodeCompletionContext(Kind CCKind, QualType T,
374*12c85518Srobert                         ArrayRef<IdentifierInfo *> SelIdents = std::nullopt)
CCKind(CCKind)375e5dd7070Spatrick       : CCKind(CCKind), IsUsingDeclaration(false), SelIdents(SelIdents) {
376e5dd7070Spatrick     if (CCKind == CCC_DotMemberAccess || CCKind == CCC_ArrowMemberAccess ||
377e5dd7070Spatrick         CCKind == CCC_ObjCPropertyAccess || CCKind == CCC_ObjCClassMessage ||
378e5dd7070Spatrick         CCKind == CCC_ObjCInstanceMessage)
379e5dd7070Spatrick       BaseType = T;
380e5dd7070Spatrick     else
381e5dd7070Spatrick       PreferredType = T;
382e5dd7070Spatrick   }
383e5dd7070Spatrick 
isUsingDeclaration()384e5dd7070Spatrick   bool isUsingDeclaration() const { return IsUsingDeclaration; }
setIsUsingDeclaration(bool V)385e5dd7070Spatrick   void setIsUsingDeclaration(bool V) { IsUsingDeclaration = V; }
386e5dd7070Spatrick 
387e5dd7070Spatrick   /// Retrieve the kind of code-completion context.
getKind()388e5dd7070Spatrick   Kind getKind() const { return CCKind; }
389e5dd7070Spatrick 
390e5dd7070Spatrick   /// Retrieve the type that this expression would prefer to have, e.g.,
391e5dd7070Spatrick   /// if the expression is a variable initializer or a function argument, the
392e5dd7070Spatrick   /// type of the corresponding variable or function parameter.
getPreferredType()393e5dd7070Spatrick   QualType getPreferredType() const { return PreferredType; }
setPreferredType(QualType T)394e5dd7070Spatrick   void setPreferredType(QualType T) { PreferredType = T; }
395e5dd7070Spatrick 
396e5dd7070Spatrick   /// Retrieve the type of the base object in a member-access
397e5dd7070Spatrick   /// expression.
getBaseType()398e5dd7070Spatrick   QualType getBaseType() const { return BaseType; }
399e5dd7070Spatrick 
400e5dd7070Spatrick   /// Retrieve the Objective-C selector identifiers.
getSelIdents()401e5dd7070Spatrick   ArrayRef<IdentifierInfo *> getSelIdents() const { return SelIdents; }
402e5dd7070Spatrick 
403e5dd7070Spatrick   /// Determines whether we want C++ constructors as results within this
404e5dd7070Spatrick   /// context.
405e5dd7070Spatrick   bool wantConstructorResults() const;
406e5dd7070Spatrick 
407e5dd7070Spatrick   /// Sets the scope specifier that comes before the completion token.
408e5dd7070Spatrick   /// This is expected to be set in code completions on qualfied specifiers
409e5dd7070Spatrick   /// (e.g. "a::b::").
setCXXScopeSpecifier(CXXScopeSpec SS)410e5dd7070Spatrick   void setCXXScopeSpecifier(CXXScopeSpec SS) {
411e5dd7070Spatrick     this->ScopeSpecifier = std::move(SS);
412e5dd7070Spatrick   }
413e5dd7070Spatrick 
414e5dd7070Spatrick   /// Adds a visited context.
addVisitedContext(DeclContext * Ctx)415e5dd7070Spatrick   void addVisitedContext(DeclContext *Ctx) {
416e5dd7070Spatrick     VisitedContexts.insert(Ctx);
417e5dd7070Spatrick   }
418e5dd7070Spatrick 
419e5dd7070Spatrick   /// Retrieves all visited contexts.
getVisitedContexts()420e5dd7070Spatrick   const VisitedContextSet &getVisitedContexts() const {
421e5dd7070Spatrick     return VisitedContexts;
422e5dd7070Spatrick   }
423e5dd7070Spatrick 
getCXXScopeSpecifier()424*12c85518Srobert   std::optional<const CXXScopeSpec *> getCXXScopeSpecifier() {
425e5dd7070Spatrick     if (ScopeSpecifier)
426*12c85518Srobert       return &*ScopeSpecifier;
427*12c85518Srobert     return std::nullopt;
428e5dd7070Spatrick   }
429e5dd7070Spatrick };
430e5dd7070Spatrick 
431*12c85518Srobert /// Get string representation of \p Kind, useful for debugging.
432e5dd7070Spatrick llvm::StringRef getCompletionKindString(CodeCompletionContext::Kind Kind);
433e5dd7070Spatrick 
434e5dd7070Spatrick /// A "string" used to describe how code completion can
435e5dd7070Spatrick /// be performed for an entity.
436e5dd7070Spatrick ///
437e5dd7070Spatrick /// A code completion string typically shows how a particular entity can be
438e5dd7070Spatrick /// used. For example, the code completion string for a function would show
439e5dd7070Spatrick /// the syntax to call it, including the parentheses, placeholders for the
440e5dd7070Spatrick /// arguments, etc.
441e5dd7070Spatrick class CodeCompletionString {
442e5dd7070Spatrick public:
443e5dd7070Spatrick   /// The different kinds of "chunks" that can occur within a code
444e5dd7070Spatrick   /// completion string.
445e5dd7070Spatrick   enum ChunkKind {
446e5dd7070Spatrick     /// The piece of text that the user is expected to type to
447e5dd7070Spatrick     /// match the code-completion string, typically a keyword or the name of a
448e5dd7070Spatrick     /// declarator or macro.
449e5dd7070Spatrick     CK_TypedText,
450e5dd7070Spatrick 
451e5dd7070Spatrick     /// A piece of text that should be placed in the buffer, e.g.,
452e5dd7070Spatrick     /// parentheses or a comma in a function call.
453e5dd7070Spatrick     CK_Text,
454e5dd7070Spatrick 
455e5dd7070Spatrick     /// A code completion string that is entirely optional. For example,
456e5dd7070Spatrick     /// an optional code completion string that describes the default arguments
457e5dd7070Spatrick     /// in a function call.
458e5dd7070Spatrick     CK_Optional,
459e5dd7070Spatrick 
460e5dd7070Spatrick     /// A string that acts as a placeholder for, e.g., a function
461e5dd7070Spatrick     /// call argument.
462e5dd7070Spatrick     CK_Placeholder,
463e5dd7070Spatrick 
464e5dd7070Spatrick     /// A piece of text that describes something about the result but
465e5dd7070Spatrick     /// should not be inserted into the buffer.
466e5dd7070Spatrick     CK_Informative,
467e5dd7070Spatrick     /// A piece of text that describes the type of an entity or, for
468e5dd7070Spatrick     /// functions and methods, the return type.
469e5dd7070Spatrick     CK_ResultType,
470e5dd7070Spatrick 
471e5dd7070Spatrick     /// A piece of text that describes the parameter that corresponds
472e5dd7070Spatrick     /// to the code-completion location within a function call, message send,
473e5dd7070Spatrick     /// macro invocation, etc.
474e5dd7070Spatrick     CK_CurrentParameter,
475e5dd7070Spatrick 
476e5dd7070Spatrick     /// A left parenthesis ('(').
477e5dd7070Spatrick     CK_LeftParen,
478e5dd7070Spatrick 
479e5dd7070Spatrick     /// A right parenthesis (')').
480e5dd7070Spatrick     CK_RightParen,
481e5dd7070Spatrick 
482e5dd7070Spatrick     /// A left bracket ('[').
483e5dd7070Spatrick     CK_LeftBracket,
484e5dd7070Spatrick 
485e5dd7070Spatrick     /// A right bracket (']').
486e5dd7070Spatrick     CK_RightBracket,
487e5dd7070Spatrick 
488e5dd7070Spatrick     /// A left brace ('{').
489e5dd7070Spatrick     CK_LeftBrace,
490e5dd7070Spatrick 
491e5dd7070Spatrick     /// A right brace ('}').
492e5dd7070Spatrick     CK_RightBrace,
493e5dd7070Spatrick 
494e5dd7070Spatrick     /// A left angle bracket ('<').
495e5dd7070Spatrick     CK_LeftAngle,
496e5dd7070Spatrick 
497e5dd7070Spatrick     /// A right angle bracket ('>').
498e5dd7070Spatrick     CK_RightAngle,
499e5dd7070Spatrick 
500e5dd7070Spatrick     /// A comma separator (',').
501e5dd7070Spatrick     CK_Comma,
502e5dd7070Spatrick 
503e5dd7070Spatrick     /// A colon (':').
504e5dd7070Spatrick     CK_Colon,
505e5dd7070Spatrick 
506e5dd7070Spatrick     /// A semicolon (';').
507e5dd7070Spatrick     CK_SemiColon,
508e5dd7070Spatrick 
509e5dd7070Spatrick     /// An '=' sign.
510e5dd7070Spatrick     CK_Equal,
511e5dd7070Spatrick 
512e5dd7070Spatrick     /// Horizontal whitespace (' ').
513e5dd7070Spatrick     CK_HorizontalSpace,
514e5dd7070Spatrick 
515e5dd7070Spatrick     /// Vertical whitespace ('\\n' or '\\r\\n', depending on the
516e5dd7070Spatrick     /// platform).
517e5dd7070Spatrick     CK_VerticalSpace
518e5dd7070Spatrick   };
519e5dd7070Spatrick 
520e5dd7070Spatrick   /// One piece of the code completion string.
521e5dd7070Spatrick   struct Chunk {
522e5dd7070Spatrick     /// The kind of data stored in this piece of the code completion
523e5dd7070Spatrick     /// string.
524e5dd7070Spatrick     ChunkKind Kind = CK_Text;
525e5dd7070Spatrick 
526e5dd7070Spatrick     union {
527e5dd7070Spatrick       /// The text string associated with a CK_Text, CK_Placeholder,
528e5dd7070Spatrick       /// CK_Informative, or CK_Comma chunk.
529e5dd7070Spatrick       /// The string is owned by the chunk and will be deallocated
530e5dd7070Spatrick       /// (with delete[]) when the chunk is destroyed.
531e5dd7070Spatrick       const char *Text;
532e5dd7070Spatrick 
533e5dd7070Spatrick       /// The code completion string associated with a CK_Optional chunk.
534e5dd7070Spatrick       /// The optional code completion string is owned by the chunk, and will
535e5dd7070Spatrick       /// be deallocated (with delete) when the chunk is destroyed.
536e5dd7070Spatrick       CodeCompletionString *Optional;
537e5dd7070Spatrick     };
538e5dd7070Spatrick 
ChunkChunk539e5dd7070Spatrick     Chunk() : Text(nullptr) {}
540e5dd7070Spatrick 
541e5dd7070Spatrick     explicit Chunk(ChunkKind Kind, const char *Text = "");
542e5dd7070Spatrick 
543e5dd7070Spatrick     /// Create a new text chunk.
544e5dd7070Spatrick     static Chunk CreateText(const char *Text);
545e5dd7070Spatrick 
546e5dd7070Spatrick     /// Create a new optional chunk.
547e5dd7070Spatrick     static Chunk CreateOptional(CodeCompletionString *Optional);
548e5dd7070Spatrick 
549e5dd7070Spatrick     /// Create a new placeholder chunk.
550e5dd7070Spatrick     static Chunk CreatePlaceholder(const char *Placeholder);
551e5dd7070Spatrick 
552e5dd7070Spatrick     /// Create a new informative chunk.
553e5dd7070Spatrick     static Chunk CreateInformative(const char *Informative);
554e5dd7070Spatrick 
555e5dd7070Spatrick     /// Create a new result type chunk.
556e5dd7070Spatrick     static Chunk CreateResultType(const char *ResultType);
557e5dd7070Spatrick 
558e5dd7070Spatrick     /// Create a new current-parameter chunk.
559e5dd7070Spatrick     static Chunk CreateCurrentParameter(const char *CurrentParameter);
560e5dd7070Spatrick   };
561e5dd7070Spatrick 
562e5dd7070Spatrick private:
563e5dd7070Spatrick   friend class CodeCompletionBuilder;
564e5dd7070Spatrick   friend class CodeCompletionResult;
565e5dd7070Spatrick 
566e5dd7070Spatrick   /// The number of chunks stored in this string.
567e5dd7070Spatrick   unsigned NumChunks : 16;
568e5dd7070Spatrick 
569e5dd7070Spatrick   /// The number of annotations for this code-completion result.
570e5dd7070Spatrick   unsigned NumAnnotations : 16;
571e5dd7070Spatrick 
572e5dd7070Spatrick   /// The priority of this code-completion string.
573e5dd7070Spatrick   unsigned Priority : 16;
574e5dd7070Spatrick 
575e5dd7070Spatrick   /// The availability of this code-completion result.
576e5dd7070Spatrick   unsigned Availability : 2;
577e5dd7070Spatrick 
578e5dd7070Spatrick   /// The name of the parent context.
579e5dd7070Spatrick   StringRef ParentName;
580e5dd7070Spatrick 
581e5dd7070Spatrick   /// A brief documentation comment attached to the declaration of
582e5dd7070Spatrick   /// entity being completed by this result.
583e5dd7070Spatrick   const char *BriefComment;
584e5dd7070Spatrick 
585e5dd7070Spatrick   CodeCompletionString(const Chunk *Chunks, unsigned NumChunks,
586e5dd7070Spatrick                        unsigned Priority, CXAvailabilityKind Availability,
587e5dd7070Spatrick                        const char **Annotations, unsigned NumAnnotations,
588e5dd7070Spatrick                        StringRef ParentName,
589e5dd7070Spatrick                        const char *BriefComment);
590e5dd7070Spatrick   ~CodeCompletionString() = default;
591e5dd7070Spatrick 
592e5dd7070Spatrick public:
593e5dd7070Spatrick   CodeCompletionString(const CodeCompletionString &) = delete;
594e5dd7070Spatrick   CodeCompletionString &operator=(const CodeCompletionString &) = delete;
595e5dd7070Spatrick 
596e5dd7070Spatrick   using iterator = const Chunk *;
597e5dd7070Spatrick 
begin()598e5dd7070Spatrick   iterator begin() const { return reinterpret_cast<const Chunk *>(this + 1); }
end()599e5dd7070Spatrick   iterator end() const { return begin() + NumChunks; }
empty()600e5dd7070Spatrick   bool empty() const { return NumChunks == 0; }
size()601e5dd7070Spatrick   unsigned size() const { return NumChunks; }
602e5dd7070Spatrick 
603e5dd7070Spatrick   const Chunk &operator[](unsigned I) const {
604e5dd7070Spatrick     assert(I < size() && "Chunk index out-of-range");
605e5dd7070Spatrick     return begin()[I];
606e5dd7070Spatrick   }
607e5dd7070Spatrick 
608*12c85518Srobert   /// Returns the text in the first TypedText chunk.
609e5dd7070Spatrick   const char *getTypedText() const;
610e5dd7070Spatrick 
611*12c85518Srobert   /// Returns the combined text from all TypedText chunks.
612*12c85518Srobert   std::string getAllTypedText() const;
613*12c85518Srobert 
614e5dd7070Spatrick   /// Retrieve the priority of this code completion result.
getPriority()615e5dd7070Spatrick   unsigned getPriority() const { return Priority; }
616e5dd7070Spatrick 
617e5dd7070Spatrick   /// Retrieve the availability of this code completion result.
getAvailability()618e5dd7070Spatrick   unsigned getAvailability() const { return Availability; }
619e5dd7070Spatrick 
620e5dd7070Spatrick   /// Retrieve the number of annotations for this code completion result.
621e5dd7070Spatrick   unsigned getAnnotationCount() const;
622e5dd7070Spatrick 
623e5dd7070Spatrick   /// Retrieve the annotation string specified by \c AnnotationNr.
624e5dd7070Spatrick   const char *getAnnotation(unsigned AnnotationNr) const;
625e5dd7070Spatrick 
626e5dd7070Spatrick   /// Retrieve the name of the parent context.
getParentContextName()627e5dd7070Spatrick   StringRef getParentContextName() const {
628e5dd7070Spatrick     return ParentName;
629e5dd7070Spatrick   }
630e5dd7070Spatrick 
getBriefComment()631e5dd7070Spatrick   const char *getBriefComment() const {
632e5dd7070Spatrick     return BriefComment;
633e5dd7070Spatrick   }
634e5dd7070Spatrick 
635e5dd7070Spatrick   /// Retrieve a string representation of the code completion string,
636e5dd7070Spatrick   /// which is mainly useful for debugging.
637e5dd7070Spatrick   std::string getAsString() const;
638e5dd7070Spatrick };
639e5dd7070Spatrick 
640e5dd7070Spatrick /// An allocator used specifically for the purpose of code completion.
641e5dd7070Spatrick class CodeCompletionAllocator : public llvm::BumpPtrAllocator {
642e5dd7070Spatrick public:
643e5dd7070Spatrick   /// Copy the given string into this allocator.
644e5dd7070Spatrick   const char *CopyString(const Twine &String);
645e5dd7070Spatrick };
646e5dd7070Spatrick 
647e5dd7070Spatrick /// Allocator for a cached set of global code completions.
648e5dd7070Spatrick class GlobalCodeCompletionAllocator : public CodeCompletionAllocator {};
649e5dd7070Spatrick 
650e5dd7070Spatrick class CodeCompletionTUInfo {
651e5dd7070Spatrick   llvm::DenseMap<const DeclContext *, StringRef> ParentNames;
652e5dd7070Spatrick   std::shared_ptr<GlobalCodeCompletionAllocator> AllocatorRef;
653e5dd7070Spatrick 
654e5dd7070Spatrick public:
CodeCompletionTUInfo(std::shared_ptr<GlobalCodeCompletionAllocator> Allocator)655e5dd7070Spatrick   explicit CodeCompletionTUInfo(
656e5dd7070Spatrick       std::shared_ptr<GlobalCodeCompletionAllocator> Allocator)
657e5dd7070Spatrick       : AllocatorRef(std::move(Allocator)) {}
658e5dd7070Spatrick 
getAllocatorRef()659e5dd7070Spatrick   std::shared_ptr<GlobalCodeCompletionAllocator> getAllocatorRef() const {
660e5dd7070Spatrick     return AllocatorRef;
661e5dd7070Spatrick   }
662e5dd7070Spatrick 
getAllocator()663e5dd7070Spatrick   CodeCompletionAllocator &getAllocator() const {
664e5dd7070Spatrick     assert(AllocatorRef);
665e5dd7070Spatrick     return *AllocatorRef;
666e5dd7070Spatrick   }
667e5dd7070Spatrick 
668e5dd7070Spatrick   StringRef getParentName(const DeclContext *DC);
669e5dd7070Spatrick };
670e5dd7070Spatrick 
671e5dd7070Spatrick } // namespace clang
672e5dd7070Spatrick 
673e5dd7070Spatrick namespace clang {
674e5dd7070Spatrick 
675e5dd7070Spatrick /// A builder class used to construct new code-completion strings.
676e5dd7070Spatrick class CodeCompletionBuilder {
677e5dd7070Spatrick public:
678e5dd7070Spatrick   using Chunk = CodeCompletionString::Chunk;
679e5dd7070Spatrick 
680e5dd7070Spatrick private:
681e5dd7070Spatrick   CodeCompletionAllocator &Allocator;
682e5dd7070Spatrick   CodeCompletionTUInfo &CCTUInfo;
683e5dd7070Spatrick   unsigned Priority = 0;
684e5dd7070Spatrick   CXAvailabilityKind Availability = CXAvailability_Available;
685e5dd7070Spatrick   StringRef ParentName;
686e5dd7070Spatrick   const char *BriefComment = nullptr;
687e5dd7070Spatrick 
688e5dd7070Spatrick   /// The chunks stored in this string.
689e5dd7070Spatrick   SmallVector<Chunk, 4> Chunks;
690e5dd7070Spatrick 
691e5dd7070Spatrick   SmallVector<const char *, 2> Annotations;
692e5dd7070Spatrick 
693e5dd7070Spatrick public:
CodeCompletionBuilder(CodeCompletionAllocator & Allocator,CodeCompletionTUInfo & CCTUInfo)694e5dd7070Spatrick   CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
695e5dd7070Spatrick                         CodeCompletionTUInfo &CCTUInfo)
696e5dd7070Spatrick       : Allocator(Allocator), CCTUInfo(CCTUInfo) {}
697e5dd7070Spatrick 
CodeCompletionBuilder(CodeCompletionAllocator & Allocator,CodeCompletionTUInfo & CCTUInfo,unsigned Priority,CXAvailabilityKind Availability)698e5dd7070Spatrick   CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
699e5dd7070Spatrick                         CodeCompletionTUInfo &CCTUInfo,
700e5dd7070Spatrick                         unsigned Priority, CXAvailabilityKind Availability)
701e5dd7070Spatrick       : Allocator(Allocator), CCTUInfo(CCTUInfo), Priority(Priority),
702e5dd7070Spatrick         Availability(Availability) {}
703e5dd7070Spatrick 
704e5dd7070Spatrick   /// Retrieve the allocator into which the code completion
705e5dd7070Spatrick   /// strings should be allocated.
getAllocator()706e5dd7070Spatrick   CodeCompletionAllocator &getAllocator() const { return Allocator; }
707e5dd7070Spatrick 
getCodeCompletionTUInfo()708e5dd7070Spatrick   CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
709e5dd7070Spatrick 
710e5dd7070Spatrick   /// Take the resulting completion string.
711e5dd7070Spatrick   ///
712e5dd7070Spatrick   /// This operation can only be performed once.
713e5dd7070Spatrick   CodeCompletionString *TakeString();
714e5dd7070Spatrick 
715e5dd7070Spatrick   /// Add a new typed-text chunk.
716e5dd7070Spatrick   void AddTypedTextChunk(const char *Text);
717e5dd7070Spatrick 
718e5dd7070Spatrick   /// Add a new text chunk.
719e5dd7070Spatrick   void AddTextChunk(const char *Text);
720e5dd7070Spatrick 
721e5dd7070Spatrick   /// Add a new optional chunk.
722e5dd7070Spatrick   void AddOptionalChunk(CodeCompletionString *Optional);
723e5dd7070Spatrick 
724e5dd7070Spatrick   /// Add a new placeholder chunk.
725e5dd7070Spatrick   void AddPlaceholderChunk(const char *Placeholder);
726e5dd7070Spatrick 
727e5dd7070Spatrick   /// Add a new informative chunk.
728e5dd7070Spatrick   void AddInformativeChunk(const char *Text);
729e5dd7070Spatrick 
730e5dd7070Spatrick   /// Add a new result-type chunk.
731e5dd7070Spatrick   void AddResultTypeChunk(const char *ResultType);
732e5dd7070Spatrick 
733e5dd7070Spatrick   /// Add a new current-parameter chunk.
734e5dd7070Spatrick   void AddCurrentParameterChunk(const char *CurrentParameter);
735e5dd7070Spatrick 
736e5dd7070Spatrick   /// Add a new chunk.
737e5dd7070Spatrick   void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text = "");
738e5dd7070Spatrick 
AddAnnotation(const char * A)739e5dd7070Spatrick   void AddAnnotation(const char *A) { Annotations.push_back(A); }
740e5dd7070Spatrick 
741e5dd7070Spatrick   /// Add the parent context information to this code completion.
742e5dd7070Spatrick   void addParentContext(const DeclContext *DC);
743e5dd7070Spatrick 
getBriefComment()744e5dd7070Spatrick   const char *getBriefComment() const { return BriefComment; }
745e5dd7070Spatrick   void addBriefComment(StringRef Comment);
746e5dd7070Spatrick 
getParentName()747e5dd7070Spatrick   StringRef getParentName() const { return ParentName; }
748e5dd7070Spatrick };
749e5dd7070Spatrick 
750e5dd7070Spatrick /// Captures a result of code completion.
751e5dd7070Spatrick class CodeCompletionResult {
752e5dd7070Spatrick public:
753e5dd7070Spatrick   /// Describes the kind of result generated.
754e5dd7070Spatrick   enum ResultKind {
755e5dd7070Spatrick     /// Refers to a declaration.
756e5dd7070Spatrick     RK_Declaration = 0,
757e5dd7070Spatrick 
758e5dd7070Spatrick     /// Refers to a keyword or symbol.
759e5dd7070Spatrick     RK_Keyword,
760e5dd7070Spatrick 
761e5dd7070Spatrick     /// Refers to a macro.
762e5dd7070Spatrick     RK_Macro,
763e5dd7070Spatrick 
764e5dd7070Spatrick     /// Refers to a precomputed pattern.
765e5dd7070Spatrick     RK_Pattern
766e5dd7070Spatrick   };
767e5dd7070Spatrick 
768e5dd7070Spatrick   /// When Kind == RK_Declaration or RK_Pattern, the declaration we are
769e5dd7070Spatrick   /// referring to. In the latter case, the declaration might be NULL.
770e5dd7070Spatrick   const NamedDecl *Declaration = nullptr;
771e5dd7070Spatrick 
772e5dd7070Spatrick   union {
773e5dd7070Spatrick     /// When Kind == RK_Keyword, the string representing the keyword
774e5dd7070Spatrick     /// or symbol's spelling.
775e5dd7070Spatrick     const char *Keyword;
776e5dd7070Spatrick 
777e5dd7070Spatrick     /// When Kind == RK_Pattern, the code-completion string that
778e5dd7070Spatrick     /// describes the completion text to insert.
779e5dd7070Spatrick     CodeCompletionString *Pattern;
780e5dd7070Spatrick 
781e5dd7070Spatrick     /// When Kind == RK_Macro, the identifier that refers to a macro.
782e5dd7070Spatrick     const IdentifierInfo *Macro;
783e5dd7070Spatrick   };
784e5dd7070Spatrick 
785e5dd7070Spatrick   /// The priority of this particular code-completion result.
786e5dd7070Spatrick   unsigned Priority;
787e5dd7070Spatrick 
788e5dd7070Spatrick   /// Specifies which parameter (of a function, Objective-C method,
789e5dd7070Spatrick   /// macro, etc.) we should start with when formatting the result.
790e5dd7070Spatrick   unsigned StartParameter = 0;
791e5dd7070Spatrick 
792e5dd7070Spatrick   /// The kind of result stored here.
793e5dd7070Spatrick   ResultKind Kind;
794e5dd7070Spatrick 
795e5dd7070Spatrick   /// The cursor kind that describes this result.
796e5dd7070Spatrick   CXCursorKind CursorKind;
797e5dd7070Spatrick 
798e5dd7070Spatrick   /// The availability of this result.
799e5dd7070Spatrick   CXAvailabilityKind Availability = CXAvailability_Available;
800e5dd7070Spatrick 
801e5dd7070Spatrick   /// Fix-its that *must* be applied before inserting the text for the
802e5dd7070Spatrick   /// corresponding completion.
803e5dd7070Spatrick   ///
804e5dd7070Spatrick   /// By default, CodeCompletionBuilder only returns completions with empty
805e5dd7070Spatrick   /// fix-its. Extra completions with non-empty fix-its should be explicitly
806e5dd7070Spatrick   /// requested by setting CompletionOptions::IncludeFixIts.
807e5dd7070Spatrick   ///
808e5dd7070Spatrick   /// For the clients to be able to compute position of the cursor after
809e5dd7070Spatrick   /// applying fix-its, the following conditions are guaranteed to hold for
810e5dd7070Spatrick   /// RemoveRange of the stored fix-its:
811e5dd7070Spatrick   ///  - Ranges in the fix-its are guaranteed to never contain the completion
812e5dd7070Spatrick   ///  point (or identifier under completion point, if any) inside them, except
813e5dd7070Spatrick   ///  at the start or at the end of the range.
814e5dd7070Spatrick   ///  - If a fix-it range starts or ends with completion point (or starts or
815e5dd7070Spatrick   ///  ends after the identifier under completion point), it will contain at
816e5dd7070Spatrick   ///  least one character. It allows to unambiguously recompute completion
817e5dd7070Spatrick   ///  point after applying the fix-it.
818e5dd7070Spatrick   ///
819e5dd7070Spatrick   /// The intuition is that provided fix-its change code around the identifier
820e5dd7070Spatrick   /// we complete, but are not allowed to touch the identifier itself or the
821e5dd7070Spatrick   /// completion point. One example of completions with corrections are the ones
822e5dd7070Spatrick   /// replacing '.' with '->' and vice versa:
823e5dd7070Spatrick   ///
824e5dd7070Spatrick   /// std::unique_ptr<std::vector<int>> vec_ptr;
825e5dd7070Spatrick   /// In 'vec_ptr.^', one of the completions is 'push_back', it requires
826e5dd7070Spatrick   /// replacing '.' with '->'.
827e5dd7070Spatrick   /// In 'vec_ptr->^', one of the completions is 'release', it requires
828e5dd7070Spatrick   /// replacing '->' with '.'.
829e5dd7070Spatrick   std::vector<FixItHint> FixIts;
830e5dd7070Spatrick 
831e5dd7070Spatrick   /// Whether this result is hidden by another name.
832e5dd7070Spatrick   bool Hidden : 1;
833e5dd7070Spatrick 
834e5dd7070Spatrick   /// Whether this is a class member from base class.
835e5dd7070Spatrick   bool InBaseClass : 1;
836e5dd7070Spatrick 
837e5dd7070Spatrick   /// Whether this result was found via lookup into a base class.
838e5dd7070Spatrick   bool QualifierIsInformative : 1;
839e5dd7070Spatrick 
840e5dd7070Spatrick   /// Whether this declaration is the beginning of a
841e5dd7070Spatrick   /// nested-name-specifier and, therefore, should be followed by '::'.
842e5dd7070Spatrick   bool StartsNestedNameSpecifier : 1;
843e5dd7070Spatrick 
844e5dd7070Spatrick   /// Whether all parameters (of a function, Objective-C
845e5dd7070Spatrick   /// method, etc.) should be considered "informative".
846e5dd7070Spatrick   bool AllParametersAreInformative : 1;
847e5dd7070Spatrick 
848e5dd7070Spatrick   /// Whether we're completing a declaration of the given entity,
849e5dd7070Spatrick   /// rather than a use of that entity.
850e5dd7070Spatrick   bool DeclaringEntity : 1;
851e5dd7070Spatrick 
852*12c85518Srobert   /// When completing a function, whether it can be a call. This will usually be
853*12c85518Srobert   /// true, but we have some heuristics, e.g. when a pointer to a non-static
854*12c85518Srobert   /// member function is completed outside of that class' scope, it can never
855*12c85518Srobert   /// be a call.
856*12c85518Srobert   bool FunctionCanBeCall : 1;
857*12c85518Srobert 
858e5dd7070Spatrick   /// If the result should have a nested-name-specifier, this is it.
859e5dd7070Spatrick   /// When \c QualifierIsInformative, the nested-name-specifier is
860e5dd7070Spatrick   /// informative rather than required.
861e5dd7070Spatrick   NestedNameSpecifier *Qualifier = nullptr;
862e5dd7070Spatrick 
863e5dd7070Spatrick   /// If this Decl was unshadowed by using declaration, this can store a
864e5dd7070Spatrick   /// pointer to the UsingShadowDecl which was used in the unshadowing process.
865e5dd7070Spatrick   /// This information can be used to uprank CodeCompletionResults / which have
866e5dd7070Spatrick   /// corresponding `using decl::qualified::name;` nearby.
867e5dd7070Spatrick   const UsingShadowDecl *ShadowDecl = nullptr;
868e5dd7070Spatrick 
869e5dd7070Spatrick   /// If the result is RK_Macro, this can store the information about the macro
870e5dd7070Spatrick   /// definition. This should be set in most cases but can be missing when
871e5dd7070Spatrick   /// the macro has been undefined.
872e5dd7070Spatrick   const MacroInfo *MacroDefInfo = nullptr;
873e5dd7070Spatrick 
874e5dd7070Spatrick   /// Build a result that refers to a declaration.
875e5dd7070Spatrick   CodeCompletionResult(const NamedDecl *Declaration, unsigned Priority,
876e5dd7070Spatrick                        NestedNameSpecifier *Qualifier = nullptr,
877e5dd7070Spatrick                        bool QualifierIsInformative = false,
878e5dd7070Spatrick                        bool Accessible = true,
879e5dd7070Spatrick                        std::vector<FixItHint> FixIts = std::vector<FixItHint>())
Declaration(Declaration)880e5dd7070Spatrick       : Declaration(Declaration), Priority(Priority), Kind(RK_Declaration),
881e5dd7070Spatrick         FixIts(std::move(FixIts)), Hidden(false), InBaseClass(false),
882e5dd7070Spatrick         QualifierIsInformative(QualifierIsInformative),
883e5dd7070Spatrick         StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
884*12c85518Srobert         DeclaringEntity(false), FunctionCanBeCall(true), Qualifier(Qualifier) {
885e5dd7070Spatrick     // FIXME: Add assert to check FixIts range requirements.
886e5dd7070Spatrick     computeCursorKindAndAvailability(Accessible);
887e5dd7070Spatrick   }
888e5dd7070Spatrick 
889e5dd7070Spatrick   /// Build a result that refers to a keyword or symbol.
890e5dd7070Spatrick   CodeCompletionResult(const char *Keyword, unsigned Priority = CCP_Keyword)
Keyword(Keyword)891e5dd7070Spatrick       : Keyword(Keyword), Priority(Priority), Kind(RK_Keyword),
892e5dd7070Spatrick         CursorKind(CXCursor_NotImplemented), Hidden(false), InBaseClass(false),
893e5dd7070Spatrick         QualifierIsInformative(false), StartsNestedNameSpecifier(false),
894*12c85518Srobert         AllParametersAreInformative(false), DeclaringEntity(false),
895*12c85518Srobert         FunctionCanBeCall(true) {}
896e5dd7070Spatrick 
897e5dd7070Spatrick   /// Build a result that refers to a macro.
898e5dd7070Spatrick   CodeCompletionResult(const IdentifierInfo *Macro,
899e5dd7070Spatrick                        const MacroInfo *MI = nullptr,
900e5dd7070Spatrick                        unsigned Priority = CCP_Macro)
Macro(Macro)901e5dd7070Spatrick       : Macro(Macro), Priority(Priority), Kind(RK_Macro),
902e5dd7070Spatrick         CursorKind(CXCursor_MacroDefinition), Hidden(false), InBaseClass(false),
903e5dd7070Spatrick         QualifierIsInformative(false), StartsNestedNameSpecifier(false),
904e5dd7070Spatrick         AllParametersAreInformative(false), DeclaringEntity(false),
905*12c85518Srobert         FunctionCanBeCall(true), MacroDefInfo(MI) {}
906e5dd7070Spatrick 
907e5dd7070Spatrick   /// Build a result that refers to a pattern.
908e5dd7070Spatrick   CodeCompletionResult(
909e5dd7070Spatrick       CodeCompletionString *Pattern, unsigned Priority = CCP_CodePattern,
910e5dd7070Spatrick       CXCursorKind CursorKind = CXCursor_NotImplemented,
911e5dd7070Spatrick       CXAvailabilityKind Availability = CXAvailability_Available,
912e5dd7070Spatrick       const NamedDecl *D = nullptr)
Declaration(D)913e5dd7070Spatrick       : Declaration(D), Pattern(Pattern), Priority(Priority), Kind(RK_Pattern),
914e5dd7070Spatrick         CursorKind(CursorKind), Availability(Availability), Hidden(false),
915e5dd7070Spatrick         InBaseClass(false), QualifierIsInformative(false),
916e5dd7070Spatrick         StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
917*12c85518Srobert         DeclaringEntity(false), FunctionCanBeCall(true) {}
918e5dd7070Spatrick 
919e5dd7070Spatrick   /// Build a result that refers to a pattern with an associated
920e5dd7070Spatrick   /// declaration.
CodeCompletionResult(CodeCompletionString * Pattern,const NamedDecl * D,unsigned Priority)921e5dd7070Spatrick   CodeCompletionResult(CodeCompletionString *Pattern, const NamedDecl *D,
922e5dd7070Spatrick                        unsigned Priority)
923e5dd7070Spatrick       : Declaration(D), Pattern(Pattern), Priority(Priority), Kind(RK_Pattern),
924e5dd7070Spatrick         Hidden(false), InBaseClass(false), QualifierIsInformative(false),
925e5dd7070Spatrick         StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
926*12c85518Srobert         DeclaringEntity(false), FunctionCanBeCall(true) {
927e5dd7070Spatrick     computeCursorKindAndAvailability();
928e5dd7070Spatrick   }
929e5dd7070Spatrick 
930e5dd7070Spatrick   /// Retrieve the declaration stored in this result. This might be nullptr if
931e5dd7070Spatrick   /// Kind is RK_Pattern.
getDeclaration()932e5dd7070Spatrick   const NamedDecl *getDeclaration() const {
933e5dd7070Spatrick     assert(((Kind == RK_Declaration) || (Kind == RK_Pattern)) &&
934e5dd7070Spatrick            "Not a declaration or pattern result");
935e5dd7070Spatrick     return Declaration;
936e5dd7070Spatrick   }
937e5dd7070Spatrick 
938e5dd7070Spatrick   /// Retrieve the keyword stored in this result.
getKeyword()939e5dd7070Spatrick   const char *getKeyword() const {
940e5dd7070Spatrick     assert(Kind == RK_Keyword && "Not a keyword result");
941e5dd7070Spatrick     return Keyword;
942e5dd7070Spatrick   }
943e5dd7070Spatrick 
944e5dd7070Spatrick   /// Create a new code-completion string that describes how to insert
945e5dd7070Spatrick   /// this result into a program.
946e5dd7070Spatrick   ///
947e5dd7070Spatrick   /// \param S The semantic analysis that created the result.
948e5dd7070Spatrick   ///
949e5dd7070Spatrick   /// \param Allocator The allocator that will be used to allocate the
950e5dd7070Spatrick   /// string itself.
951e5dd7070Spatrick   CodeCompletionString *CreateCodeCompletionString(Sema &S,
952e5dd7070Spatrick                                          const CodeCompletionContext &CCContext,
953e5dd7070Spatrick                                            CodeCompletionAllocator &Allocator,
954e5dd7070Spatrick                                            CodeCompletionTUInfo &CCTUInfo,
955e5dd7070Spatrick                                            bool IncludeBriefComments);
956e5dd7070Spatrick   CodeCompletionString *CreateCodeCompletionString(ASTContext &Ctx,
957e5dd7070Spatrick                                                    Preprocessor &PP,
958e5dd7070Spatrick                                          const CodeCompletionContext &CCContext,
959e5dd7070Spatrick                                            CodeCompletionAllocator &Allocator,
960e5dd7070Spatrick                                            CodeCompletionTUInfo &CCTUInfo,
961e5dd7070Spatrick                                            bool IncludeBriefComments);
962e5dd7070Spatrick   /// Creates a new code-completion string for the macro result. Similar to the
963e5dd7070Spatrick   /// above overloads, except this only requires preprocessor information.
964e5dd7070Spatrick   /// The result kind must be `RK_Macro`.
965e5dd7070Spatrick   CodeCompletionString *
966e5dd7070Spatrick   CreateCodeCompletionStringForMacro(Preprocessor &PP,
967e5dd7070Spatrick                                      CodeCompletionAllocator &Allocator,
968e5dd7070Spatrick                                      CodeCompletionTUInfo &CCTUInfo);
969e5dd7070Spatrick 
970e5dd7070Spatrick   CodeCompletionString *createCodeCompletionStringForDecl(
971e5dd7070Spatrick       Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result,
972e5dd7070Spatrick       bool IncludeBriefComments, const CodeCompletionContext &CCContext,
973e5dd7070Spatrick       PrintingPolicy &Policy);
974e5dd7070Spatrick 
975e5dd7070Spatrick   CodeCompletionString *createCodeCompletionStringForOverride(
976e5dd7070Spatrick       Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result,
977e5dd7070Spatrick       bool IncludeBriefComments, const CodeCompletionContext &CCContext,
978e5dd7070Spatrick       PrintingPolicy &Policy);
979e5dd7070Spatrick 
980e5dd7070Spatrick   /// Retrieve the name that should be used to order a result.
981e5dd7070Spatrick   ///
982e5dd7070Spatrick   /// If the name needs to be constructed as a string, that string will be
983e5dd7070Spatrick   /// saved into Saved and the returned StringRef will refer to it.
984e5dd7070Spatrick   StringRef getOrderedName(std::string &Saved) const;
985e5dd7070Spatrick 
986e5dd7070Spatrick private:
987e5dd7070Spatrick   void computeCursorKindAndAvailability(bool Accessible = true);
988e5dd7070Spatrick };
989e5dd7070Spatrick 
990e5dd7070Spatrick bool operator<(const CodeCompletionResult &X, const CodeCompletionResult &Y);
991e5dd7070Spatrick 
992e5dd7070Spatrick inline bool operator>(const CodeCompletionResult &X,
993e5dd7070Spatrick                       const CodeCompletionResult &Y) {
994e5dd7070Spatrick   return Y < X;
995e5dd7070Spatrick }
996e5dd7070Spatrick 
997e5dd7070Spatrick inline bool operator<=(const CodeCompletionResult &X,
998e5dd7070Spatrick                       const CodeCompletionResult &Y) {
999e5dd7070Spatrick   return !(Y < X);
1000e5dd7070Spatrick }
1001e5dd7070Spatrick 
1002e5dd7070Spatrick inline bool operator>=(const CodeCompletionResult &X,
1003e5dd7070Spatrick                        const CodeCompletionResult &Y) {
1004e5dd7070Spatrick   return !(X < Y);
1005e5dd7070Spatrick }
1006e5dd7070Spatrick 
1007e5dd7070Spatrick /// Abstract interface for a consumer of code-completion
1008e5dd7070Spatrick /// information.
1009e5dd7070Spatrick class CodeCompleteConsumer {
1010e5dd7070Spatrick protected:
1011e5dd7070Spatrick   const CodeCompleteOptions CodeCompleteOpts;
1012e5dd7070Spatrick 
1013e5dd7070Spatrick public:
1014e5dd7070Spatrick   class OverloadCandidate {
1015e5dd7070Spatrick   public:
1016e5dd7070Spatrick     /// Describes the type of overload candidate.
1017e5dd7070Spatrick     enum CandidateKind {
1018e5dd7070Spatrick       /// The candidate is a function declaration.
1019e5dd7070Spatrick       CK_Function,
1020e5dd7070Spatrick 
1021*12c85518Srobert       /// The candidate is a function template, arguments are being completed.
1022e5dd7070Spatrick       CK_FunctionTemplate,
1023e5dd7070Spatrick 
1024e5dd7070Spatrick       /// The "candidate" is actually a variable, expression, or block
1025e5dd7070Spatrick       /// for which we only have a function prototype.
1026*12c85518Srobert       CK_FunctionType,
1027*12c85518Srobert 
1028*12c85518Srobert       /// The candidate is a variable or expression of function type
1029*12c85518Srobert       /// for which we have the location of the prototype declaration.
1030*12c85518Srobert       CK_FunctionProtoTypeLoc,
1031*12c85518Srobert 
1032*12c85518Srobert       /// The candidate is a template, template arguments are being completed.
1033*12c85518Srobert       CK_Template,
1034*12c85518Srobert 
1035*12c85518Srobert       /// The candidate is aggregate initialization of a record type.
1036*12c85518Srobert       CK_Aggregate,
1037e5dd7070Spatrick     };
1038e5dd7070Spatrick 
1039e5dd7070Spatrick   private:
1040e5dd7070Spatrick     /// The kind of overload candidate.
1041e5dd7070Spatrick     CandidateKind Kind;
1042e5dd7070Spatrick 
1043e5dd7070Spatrick     union {
1044e5dd7070Spatrick       /// The function overload candidate, available when
1045e5dd7070Spatrick       /// Kind == CK_Function.
1046e5dd7070Spatrick       FunctionDecl *Function;
1047e5dd7070Spatrick 
1048e5dd7070Spatrick       /// The function template overload candidate, available when
1049e5dd7070Spatrick       /// Kind == CK_FunctionTemplate.
1050e5dd7070Spatrick       FunctionTemplateDecl *FunctionTemplate;
1051e5dd7070Spatrick 
1052e5dd7070Spatrick       /// The function type that describes the entity being called,
1053e5dd7070Spatrick       /// when Kind == CK_FunctionType.
1054e5dd7070Spatrick       const FunctionType *Type;
1055*12c85518Srobert 
1056*12c85518Srobert       /// The location of the function prototype that describes the entity being
1057*12c85518Srobert       /// called, when Kind == CK_FunctionProtoTypeLoc.
1058*12c85518Srobert       FunctionProtoTypeLoc ProtoTypeLoc;
1059*12c85518Srobert 
1060*12c85518Srobert       /// The template overload candidate, available when
1061*12c85518Srobert       /// Kind == CK_Template.
1062*12c85518Srobert       const TemplateDecl *Template;
1063*12c85518Srobert 
1064*12c85518Srobert       /// The class being aggregate-initialized,
1065*12c85518Srobert       /// when Kind == CK_Aggregate
1066*12c85518Srobert       const RecordDecl *AggregateType;
1067e5dd7070Spatrick     };
1068e5dd7070Spatrick 
1069e5dd7070Spatrick   public:
OverloadCandidate(FunctionDecl * Function)1070e5dd7070Spatrick     OverloadCandidate(FunctionDecl *Function)
1071*12c85518Srobert         : Kind(CK_Function), Function(Function) {
1072*12c85518Srobert       assert(Function != nullptr);
1073*12c85518Srobert     }
1074e5dd7070Spatrick 
OverloadCandidate(FunctionTemplateDecl * FunctionTemplateDecl)1075e5dd7070Spatrick     OverloadCandidate(FunctionTemplateDecl *FunctionTemplateDecl)
1076*12c85518Srobert         : Kind(CK_FunctionTemplate), FunctionTemplate(FunctionTemplateDecl) {
1077*12c85518Srobert       assert(FunctionTemplateDecl != nullptr);
1078*12c85518Srobert     }
1079e5dd7070Spatrick 
OverloadCandidate(const FunctionType * Type)1080e5dd7070Spatrick     OverloadCandidate(const FunctionType *Type)
1081*12c85518Srobert         : Kind(CK_FunctionType), Type(Type) {
1082*12c85518Srobert       assert(Type != nullptr);
1083*12c85518Srobert     }
1084*12c85518Srobert 
OverloadCandidate(FunctionProtoTypeLoc Prototype)1085*12c85518Srobert     OverloadCandidate(FunctionProtoTypeLoc Prototype)
1086*12c85518Srobert         : Kind(CK_FunctionProtoTypeLoc), ProtoTypeLoc(Prototype) {
1087*12c85518Srobert       assert(!Prototype.isNull());
1088*12c85518Srobert     }
1089*12c85518Srobert 
OverloadCandidate(const RecordDecl * Aggregate)1090*12c85518Srobert     OverloadCandidate(const RecordDecl *Aggregate)
1091*12c85518Srobert         : Kind(CK_Aggregate), AggregateType(Aggregate) {
1092*12c85518Srobert       assert(Aggregate != nullptr);
1093*12c85518Srobert     }
1094*12c85518Srobert 
OverloadCandidate(const TemplateDecl * Template)1095*12c85518Srobert     OverloadCandidate(const TemplateDecl *Template)
1096*12c85518Srobert         : Kind(CK_Template), Template(Template) {}
1097e5dd7070Spatrick 
1098e5dd7070Spatrick     /// Determine the kind of overload candidate.
getKind()1099e5dd7070Spatrick     CandidateKind getKind() const { return Kind; }
1100e5dd7070Spatrick 
1101e5dd7070Spatrick     /// Retrieve the function overload candidate or the templated
1102e5dd7070Spatrick     /// function declaration for a function template.
1103e5dd7070Spatrick     FunctionDecl *getFunction() const;
1104e5dd7070Spatrick 
1105e5dd7070Spatrick     /// Retrieve the function template overload candidate.
getFunctionTemplate()1106e5dd7070Spatrick     FunctionTemplateDecl *getFunctionTemplate() const {
1107e5dd7070Spatrick       assert(getKind() == CK_FunctionTemplate && "Not a function template");
1108e5dd7070Spatrick       return FunctionTemplate;
1109e5dd7070Spatrick     }
1110e5dd7070Spatrick 
1111e5dd7070Spatrick     /// Retrieve the function type of the entity, regardless of how the
1112e5dd7070Spatrick     /// function is stored.
1113e5dd7070Spatrick     const FunctionType *getFunctionType() const;
1114e5dd7070Spatrick 
1115*12c85518Srobert     /// Retrieve the function ProtoTypeLoc candidate.
1116*12c85518Srobert     /// This can be called for any Kind, but returns null for kinds
1117*12c85518Srobert     /// other than CK_FunctionProtoTypeLoc.
1118*12c85518Srobert     const FunctionProtoTypeLoc getFunctionProtoTypeLoc() const;
1119*12c85518Srobert 
getTemplate()1120*12c85518Srobert     const TemplateDecl *getTemplate() const {
1121*12c85518Srobert       assert(getKind() == CK_Template && "Not a template");
1122*12c85518Srobert       return Template;
1123*12c85518Srobert     }
1124*12c85518Srobert 
1125*12c85518Srobert     /// Retrieve the aggregate type being initialized.
getAggregate()1126*12c85518Srobert     const RecordDecl *getAggregate() const {
1127*12c85518Srobert       assert(getKind() == CK_Aggregate);
1128*12c85518Srobert       return AggregateType;
1129*12c85518Srobert     }
1130*12c85518Srobert 
1131*12c85518Srobert     /// Get the number of parameters in this signature.
1132*12c85518Srobert     unsigned getNumParams() const;
1133*12c85518Srobert 
1134*12c85518Srobert     /// Get the type of the Nth parameter.
1135*12c85518Srobert     /// Returns null if the type is unknown or N is out of range.
1136*12c85518Srobert     QualType getParamType(unsigned N) const;
1137*12c85518Srobert 
1138*12c85518Srobert     /// Get the declaration of the Nth parameter.
1139*12c85518Srobert     /// Returns null if the decl is unknown or N is out of range.
1140*12c85518Srobert     const NamedDecl *getParamDecl(unsigned N) const;
1141*12c85518Srobert 
1142e5dd7070Spatrick     /// Create a new code-completion string that describes the function
1143e5dd7070Spatrick     /// signature of this overload candidate.
1144*12c85518Srobert     CodeCompletionString *
1145*12c85518Srobert     CreateSignatureString(unsigned CurrentArg, Sema &S,
1146e5dd7070Spatrick                           CodeCompletionAllocator &Allocator,
1147e5dd7070Spatrick                           CodeCompletionTUInfo &CCTUInfo,
1148*12c85518Srobert                           bool IncludeBriefComments, bool Braced) const;
1149e5dd7070Spatrick   };
1150e5dd7070Spatrick 
CodeCompleteConsumer(const CodeCompleteOptions & CodeCompleteOpts)1151e5dd7070Spatrick   CodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts)
1152e5dd7070Spatrick       : CodeCompleteOpts(CodeCompleteOpts) {}
1153e5dd7070Spatrick 
1154e5dd7070Spatrick   /// Whether the code-completion consumer wants to see macros.
includeMacros()1155e5dd7070Spatrick   bool includeMacros() const {
1156e5dd7070Spatrick     return CodeCompleteOpts.IncludeMacros;
1157e5dd7070Spatrick   }
1158e5dd7070Spatrick 
1159e5dd7070Spatrick   /// Whether the code-completion consumer wants to see code patterns.
includeCodePatterns()1160e5dd7070Spatrick   bool includeCodePatterns() const {
1161e5dd7070Spatrick     return CodeCompleteOpts.IncludeCodePatterns;
1162e5dd7070Spatrick   }
1163e5dd7070Spatrick 
1164e5dd7070Spatrick   /// Whether to include global (top-level) declaration results.
includeGlobals()1165e5dd7070Spatrick   bool includeGlobals() const { return CodeCompleteOpts.IncludeGlobals; }
1166e5dd7070Spatrick 
1167e5dd7070Spatrick   /// Whether to include declarations in namespace contexts (including
1168e5dd7070Spatrick   /// the global namespace). If this is false, `includeGlobals()` will be
1169e5dd7070Spatrick   /// ignored.
includeNamespaceLevelDecls()1170e5dd7070Spatrick   bool includeNamespaceLevelDecls() const {
1171e5dd7070Spatrick     return CodeCompleteOpts.IncludeNamespaceLevelDecls;
1172e5dd7070Spatrick   }
1173e5dd7070Spatrick 
1174e5dd7070Spatrick   /// Whether to include brief documentation comments within the set of
1175e5dd7070Spatrick   /// code completions returned.
includeBriefComments()1176e5dd7070Spatrick   bool includeBriefComments() const {
1177e5dd7070Spatrick     return CodeCompleteOpts.IncludeBriefComments;
1178e5dd7070Spatrick   }
1179e5dd7070Spatrick 
1180e5dd7070Spatrick   /// Whether to include completion items with small fix-its, e.g. change
1181e5dd7070Spatrick   /// '.' to '->' on member access, etc.
includeFixIts()1182e5dd7070Spatrick   bool includeFixIts() const { return CodeCompleteOpts.IncludeFixIts; }
1183e5dd7070Spatrick 
1184e5dd7070Spatrick   /// Hint whether to load data from the external AST in order to provide
1185e5dd7070Spatrick   /// full results. If false, declarations from the preamble may be omitted.
loadExternal()1186e5dd7070Spatrick   bool loadExternal() const {
1187e5dd7070Spatrick     return CodeCompleteOpts.LoadExternal;
1188e5dd7070Spatrick   }
1189e5dd7070Spatrick 
1190e5dd7070Spatrick   /// Deregisters and destroys this code-completion consumer.
1191e5dd7070Spatrick   virtual ~CodeCompleteConsumer();
1192e5dd7070Spatrick 
1193e5dd7070Spatrick   /// \name Code-completion filtering
1194e5dd7070Spatrick   /// Check if the result should be filtered out.
isResultFilteredOut(StringRef Filter,CodeCompletionResult Results)1195e5dd7070Spatrick   virtual bool isResultFilteredOut(StringRef Filter,
1196e5dd7070Spatrick                                    CodeCompletionResult Results) {
1197e5dd7070Spatrick     return false;
1198e5dd7070Spatrick   }
1199e5dd7070Spatrick 
1200e5dd7070Spatrick   /// \name Code-completion callbacks
1201e5dd7070Spatrick   //@{
1202e5dd7070Spatrick   /// Process the finalized code-completion results.
ProcessCodeCompleteResults(Sema & S,CodeCompletionContext Context,CodeCompletionResult * Results,unsigned NumResults)1203e5dd7070Spatrick   virtual void ProcessCodeCompleteResults(Sema &S,
1204e5dd7070Spatrick                                           CodeCompletionContext Context,
1205e5dd7070Spatrick                                           CodeCompletionResult *Results,
1206e5dd7070Spatrick                                           unsigned NumResults) {}
1207e5dd7070Spatrick 
1208e5dd7070Spatrick   /// \param S the semantic-analyzer object for which code-completion is being
1209e5dd7070Spatrick   /// done.
1210e5dd7070Spatrick   ///
1211e5dd7070Spatrick   /// \param CurrentArg the index of the current argument.
1212e5dd7070Spatrick   ///
1213e5dd7070Spatrick   /// \param Candidates an array of overload candidates.
1214e5dd7070Spatrick   ///
1215e5dd7070Spatrick   /// \param NumCandidates the number of overload candidates
1216e5dd7070Spatrick   ///
1217e5dd7070Spatrick   /// \param OpenParLoc location of the opening parenthesis of the argument
1218e5dd7070Spatrick   ///        list.
ProcessOverloadCandidates(Sema & S,unsigned CurrentArg,OverloadCandidate * Candidates,unsigned NumCandidates,SourceLocation OpenParLoc,bool Braced)1219e5dd7070Spatrick   virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1220e5dd7070Spatrick                                          OverloadCandidate *Candidates,
1221e5dd7070Spatrick                                          unsigned NumCandidates,
1222*12c85518Srobert                                          SourceLocation OpenParLoc,
1223*12c85518Srobert                                          bool Braced) {}
1224e5dd7070Spatrick   //@}
1225e5dd7070Spatrick 
1226e5dd7070Spatrick   /// Retrieve the allocator that will be used to allocate
1227e5dd7070Spatrick   /// code completion strings.
1228e5dd7070Spatrick   virtual CodeCompletionAllocator &getAllocator() = 0;
1229e5dd7070Spatrick 
1230e5dd7070Spatrick   virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() = 0;
1231e5dd7070Spatrick };
1232e5dd7070Spatrick 
1233e5dd7070Spatrick /// Get the documentation comment used to produce
1234e5dd7070Spatrick /// CodeCompletionString::BriefComment for RK_Declaration.
1235e5dd7070Spatrick const RawComment *getCompletionComment(const ASTContext &Ctx,
1236e5dd7070Spatrick                                        const NamedDecl *Decl);
1237e5dd7070Spatrick 
1238e5dd7070Spatrick /// Get the documentation comment used to produce
1239e5dd7070Spatrick /// CodeCompletionString::BriefComment for RK_Pattern.
1240e5dd7070Spatrick const RawComment *getPatternCompletionComment(const ASTContext &Ctx,
1241e5dd7070Spatrick                                               const NamedDecl *Decl);
1242e5dd7070Spatrick 
1243e5dd7070Spatrick /// Get the documentation comment used to produce
1244e5dd7070Spatrick /// CodeCompletionString::BriefComment for OverloadCandidate.
1245e5dd7070Spatrick const RawComment *
1246e5dd7070Spatrick getParameterComment(const ASTContext &Ctx,
1247e5dd7070Spatrick                     const CodeCompleteConsumer::OverloadCandidate &Result,
1248e5dd7070Spatrick                     unsigned ArgIndex);
1249e5dd7070Spatrick 
1250e5dd7070Spatrick /// A simple code-completion consumer that prints the results it
1251e5dd7070Spatrick /// receives in a simple format.
1252e5dd7070Spatrick class PrintingCodeCompleteConsumer : public CodeCompleteConsumer {
1253e5dd7070Spatrick   /// The raw output stream.
1254e5dd7070Spatrick   raw_ostream &OS;
1255e5dd7070Spatrick 
1256e5dd7070Spatrick   CodeCompletionTUInfo CCTUInfo;
1257e5dd7070Spatrick 
1258e5dd7070Spatrick public:
1259e5dd7070Spatrick   /// Create a new printing code-completion consumer that prints its
1260e5dd7070Spatrick   /// results to the given raw output stream.
PrintingCodeCompleteConsumer(const CodeCompleteOptions & CodeCompleteOpts,raw_ostream & OS)1261e5dd7070Spatrick   PrintingCodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
1262e5dd7070Spatrick                                raw_ostream &OS)
1263e5dd7070Spatrick       : CodeCompleteConsumer(CodeCompleteOpts), OS(OS),
1264e5dd7070Spatrick         CCTUInfo(std::make_shared<GlobalCodeCompletionAllocator>()) {}
1265e5dd7070Spatrick 
1266e5dd7070Spatrick   /// Prints the finalized code-completion results.
1267e5dd7070Spatrick   void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
1268e5dd7070Spatrick                                   CodeCompletionResult *Results,
1269e5dd7070Spatrick                                   unsigned NumResults) override;
1270e5dd7070Spatrick 
1271e5dd7070Spatrick   void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
1272e5dd7070Spatrick                                  OverloadCandidate *Candidates,
1273e5dd7070Spatrick                                  unsigned NumCandidates,
1274*12c85518Srobert                                  SourceLocation OpenParLoc,
1275*12c85518Srobert                                  bool Braced) override;
1276e5dd7070Spatrick 
1277e5dd7070Spatrick   bool isResultFilteredOut(StringRef Filter, CodeCompletionResult Results) override;
1278e5dd7070Spatrick 
getAllocator()1279e5dd7070Spatrick   CodeCompletionAllocator &getAllocator() override {
1280e5dd7070Spatrick     return CCTUInfo.getAllocator();
1281e5dd7070Spatrick   }
1282e5dd7070Spatrick 
getCodeCompletionTUInfo()1283e5dd7070Spatrick   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
1284e5dd7070Spatrick };
1285e5dd7070Spatrick 
1286e5dd7070Spatrick } // namespace clang
1287e5dd7070Spatrick 
1288e5dd7070Spatrick #endif // LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
1289