xref: /freebsd/contrib/llvm-project/clang/include/clang/Sema/CodeCompleteConsumer.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- CodeCompleteConsumer.h - Code Completion Interface -------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric //  This file defines the CodeCompleteConsumer class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
140b57cec5SDimitry Andric #define LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "clang-c/Index.h"
170b57cec5SDimitry Andric #include "clang/AST/Type.h"
180b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
190b57cec5SDimitry Andric #include "clang/Lex/MacroInfo.h"
200b57cec5SDimitry Andric #include "clang/Sema/CodeCompleteOptions.h"
210b57cec5SDimitry Andric #include "clang/Sema/DeclSpec.h"
220b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
230b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
240b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
250b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
260b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
270b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
280b57cec5SDimitry Andric #include "llvm/Support/type_traits.h"
290b57cec5SDimitry Andric #include <cassert>
300b57cec5SDimitry Andric #include <memory>
31bdd1243dSDimitry Andric #include <optional>
320b57cec5SDimitry Andric #include <string>
330b57cec5SDimitry Andric #include <utility>
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric namespace clang {
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric class ASTContext;
380b57cec5SDimitry Andric class Decl;
390b57cec5SDimitry Andric class DeclContext;
400b57cec5SDimitry Andric class FunctionDecl;
410b57cec5SDimitry Andric class FunctionTemplateDecl;
420b57cec5SDimitry Andric class IdentifierInfo;
430b57cec5SDimitry Andric class LangOptions;
440b57cec5SDimitry Andric class NamedDecl;
450b57cec5SDimitry Andric class NestedNameSpecifier;
460b57cec5SDimitry Andric class Preprocessor;
470b57cec5SDimitry Andric class RawComment;
480b57cec5SDimitry Andric class Sema;
490b57cec5SDimitry Andric class UsingShadowDecl;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric /// Default priority values for code-completion results based
520b57cec5SDimitry Andric /// on their kind.
530b57cec5SDimitry Andric enum {
540b57cec5SDimitry Andric   /// Priority for the next initialization in a constructor initializer
550b57cec5SDimitry Andric   /// list.
560b57cec5SDimitry Andric   CCP_NextInitializer = 7,
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   /// Priority for an enumeration constant inside a switch whose
590b57cec5SDimitry Andric   /// condition is of the enumeration type.
600b57cec5SDimitry Andric   CCP_EnumInCase = 7,
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   /// Priority for a send-to-super completion.
630b57cec5SDimitry Andric   CCP_SuperCompletion = 20,
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   /// Priority for a declaration that is in the local scope.
660b57cec5SDimitry Andric   CCP_LocalDeclaration = 34,
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   /// Priority for a member declaration found from the current
690b57cec5SDimitry Andric   /// method or member function.
700b57cec5SDimitry Andric   CCP_MemberDeclaration = 35,
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   /// Priority for a language keyword (that isn't any of the other
730b57cec5SDimitry Andric   /// categories).
740b57cec5SDimitry Andric   CCP_Keyword = 40,
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   /// Priority for a code pattern.
770b57cec5SDimitry Andric   CCP_CodePattern = 40,
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   /// Priority for a non-type declaration.
800b57cec5SDimitry Andric   CCP_Declaration = 50,
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   /// Priority for a type.
830b57cec5SDimitry Andric   CCP_Type = CCP_Declaration,
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   /// Priority for a constant value (e.g., enumerator).
860b57cec5SDimitry Andric   CCP_Constant = 65,
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   /// Priority for a preprocessor macro.
890b57cec5SDimitry Andric   CCP_Macro = 70,
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   /// Priority for a nested-name-specifier.
920b57cec5SDimitry Andric   CCP_NestedNameSpecifier = 75,
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   /// Priority for a result that isn't likely to be what the user wants,
950b57cec5SDimitry Andric   /// but is included for completeness.
960b57cec5SDimitry Andric   CCP_Unlikely = 80,
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   /// Priority for the Objective-C "_cmd" implicit parameter.
990b57cec5SDimitry Andric   CCP_ObjC_cmd = CCP_Unlikely
1000b57cec5SDimitry Andric };
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric /// Priority value deltas that are added to code-completion results
1030b57cec5SDimitry Andric /// based on the context of the result.
1040b57cec5SDimitry Andric enum {
1050b57cec5SDimitry Andric   /// The result is in a base class.
1060b57cec5SDimitry Andric   CCD_InBaseClass = 2,
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   /// The result is a C++ non-static member function whose qualifiers
1090b57cec5SDimitry Andric   /// exactly match the object type on which the member function can be called.
1100b57cec5SDimitry Andric   CCD_ObjectQualifierMatch = -1,
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   /// The selector of the given message exactly matches the selector
1130b57cec5SDimitry Andric   /// of the current method, which might imply that some kind of delegation
1140b57cec5SDimitry Andric   /// is occurring.
1150b57cec5SDimitry Andric   CCD_SelectorMatch = -3,
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   /// Adjustment to the "bool" type in Objective-C, where the typedef
1180b57cec5SDimitry Andric   /// "BOOL" is preferred.
1190b57cec5SDimitry Andric   CCD_bool_in_ObjC = 1,
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   /// Adjustment for KVC code pattern priorities when it doesn't look
1220b57cec5SDimitry Andric   /// like the
1230b57cec5SDimitry Andric   CCD_ProbablyNotObjCCollection = 15,
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   /// An Objective-C method being used as a property.
1260b57cec5SDimitry Andric   CCD_MethodAsProperty = 2,
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   /// An Objective-C block property completed as a setter with a
1290b57cec5SDimitry Andric   /// block placeholder.
1300b57cec5SDimitry Andric   CCD_BlockPropertySetter = 3
1310b57cec5SDimitry Andric };
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric /// Priority value factors by which we will divide or multiply the
1340b57cec5SDimitry Andric /// priority of a code-completion result.
1350b57cec5SDimitry Andric enum {
1360b57cec5SDimitry Andric   /// Divide by this factor when a code-completion result's type exactly
1370b57cec5SDimitry Andric   /// matches the type we expect.
1380b57cec5SDimitry Andric   CCF_ExactTypeMatch = 4,
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   /// Divide by this factor when a code-completion result's type is
1410b57cec5SDimitry Andric   /// similar to the type we expect (e.g., both arithmetic types, both
1420b57cec5SDimitry Andric   /// Objective-C object pointer types).
1430b57cec5SDimitry Andric   CCF_SimilarTypeMatch = 2
1440b57cec5SDimitry Andric };
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric /// A simplified classification of types used when determining
1470b57cec5SDimitry Andric /// "similar" types for code completion.
1480b57cec5SDimitry Andric enum SimplifiedTypeClass {
1490b57cec5SDimitry Andric   STC_Arithmetic,
1500b57cec5SDimitry Andric   STC_Array,
1510b57cec5SDimitry Andric   STC_Block,
1520b57cec5SDimitry Andric   STC_Function,
1530b57cec5SDimitry Andric   STC_ObjectiveC,
1540b57cec5SDimitry Andric   STC_Other,
1550b57cec5SDimitry Andric   STC_Pointer,
1560b57cec5SDimitry Andric   STC_Record,
1570b57cec5SDimitry Andric   STC_Void
1580b57cec5SDimitry Andric };
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric /// Determine the simplified type class of the given canonical type.
1610b57cec5SDimitry Andric SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T);
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric /// Determine the type that this declaration will have if it is used
1640b57cec5SDimitry Andric /// as a type or in an expression.
1650b57cec5SDimitry Andric QualType getDeclUsageType(ASTContext &C, const NamedDecl *ND);
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric /// Determine the priority to be given to a macro code completion result
1680b57cec5SDimitry Andric /// with the given name.
1690b57cec5SDimitry Andric ///
1700b57cec5SDimitry Andric /// \param MacroName The name of the macro.
1710b57cec5SDimitry Andric ///
1720b57cec5SDimitry Andric /// \param LangOpts Options describing the current language dialect.
1730b57cec5SDimitry Andric ///
1740b57cec5SDimitry Andric /// \param PreferredTypeIsPointer Whether the preferred type for the context
1750b57cec5SDimitry Andric /// of this macro is a pointer type.
1760b57cec5SDimitry Andric unsigned getMacroUsagePriority(StringRef MacroName,
1770b57cec5SDimitry Andric                                const LangOptions &LangOpts,
1780b57cec5SDimitry Andric                                bool PreferredTypeIsPointer = false);
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric /// Determine the libclang cursor kind associated with the given
1810b57cec5SDimitry Andric /// declaration.
1820b57cec5SDimitry Andric CXCursorKind getCursorKindForDecl(const Decl *D);
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric /// The context in which code completion occurred, so that the
1850b57cec5SDimitry Andric /// code-completion consumer can process the results accordingly.
1860b57cec5SDimitry Andric class CodeCompletionContext {
1870b57cec5SDimitry Andric public:
1880b57cec5SDimitry Andric   enum Kind {
1890b57cec5SDimitry Andric     /// An unspecified code-completion context.
1900b57cec5SDimitry Andric     CCC_Other,
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric     /// An unspecified code-completion context where we should also add
1930b57cec5SDimitry Andric     /// macro completions.
1940b57cec5SDimitry Andric     CCC_OtherWithMacros,
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric     /// Code completion occurred within a "top-level" completion context,
1970b57cec5SDimitry Andric     /// e.g., at namespace or global scope.
1980b57cec5SDimitry Andric     CCC_TopLevel,
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric     /// Code completion occurred within an Objective-C interface,
2010b57cec5SDimitry Andric     /// protocol, or category interface.
2020b57cec5SDimitry Andric     CCC_ObjCInterface,
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric     /// Code completion occurred within an Objective-C implementation
2050b57cec5SDimitry Andric     /// or category implementation.
2060b57cec5SDimitry Andric     CCC_ObjCImplementation,
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric     /// Code completion occurred within the instance variable list of
2090b57cec5SDimitry Andric     /// an Objective-C interface, implementation, or category implementation.
2100b57cec5SDimitry Andric     CCC_ObjCIvarList,
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric     /// Code completion occurred within a class, struct, or union.
2130b57cec5SDimitry Andric     CCC_ClassStructUnion,
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric     /// Code completion occurred where a statement (or declaration) is
2160b57cec5SDimitry Andric     /// expected in a function, method, or block.
2170b57cec5SDimitry Andric     CCC_Statement,
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric     /// Code completion occurred where an expression is expected.
2200b57cec5SDimitry Andric     CCC_Expression,
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric     /// Code completion occurred where an Objective-C message receiver
2230b57cec5SDimitry Andric     /// is expected.
2240b57cec5SDimitry Andric     CCC_ObjCMessageReceiver,
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric     /// Code completion occurred on the right-hand side of a member
2270b57cec5SDimitry Andric     /// access expression using the dot operator.
2280b57cec5SDimitry Andric     ///
2290b57cec5SDimitry Andric     /// The results of this completion are the members of the type being
2300b57cec5SDimitry Andric     /// accessed. The type itself is available via
2310b57cec5SDimitry Andric     /// \c CodeCompletionContext::getType().
2320b57cec5SDimitry Andric     CCC_DotMemberAccess,
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric     /// Code completion occurred on the right-hand side of a member
2350b57cec5SDimitry Andric     /// access expression using the arrow operator.
2360b57cec5SDimitry Andric     ///
2370b57cec5SDimitry Andric     /// The results of this completion are the members of the type being
2380b57cec5SDimitry Andric     /// accessed. The type itself is available via
2390b57cec5SDimitry Andric     /// \c CodeCompletionContext::getType().
2400b57cec5SDimitry Andric     CCC_ArrowMemberAccess,
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric     /// Code completion occurred on the right-hand side of an Objective-C
2430b57cec5SDimitry Andric     /// property access expression.
2440b57cec5SDimitry Andric     ///
2450b57cec5SDimitry Andric     /// The results of this completion are the members of the type being
2460b57cec5SDimitry Andric     /// accessed. The type itself is available via
2470b57cec5SDimitry Andric     /// \c CodeCompletionContext::getType().
2480b57cec5SDimitry Andric     CCC_ObjCPropertyAccess,
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric     /// Code completion occurred after the "enum" keyword, to indicate
2510b57cec5SDimitry Andric     /// an enumeration name.
2520b57cec5SDimitry Andric     CCC_EnumTag,
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric     /// Code completion occurred after the "union" keyword, to indicate
2550b57cec5SDimitry Andric     /// a union name.
2560b57cec5SDimitry Andric     CCC_UnionTag,
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric     /// Code completion occurred after the "struct" or "class" keyword,
2590b57cec5SDimitry Andric     /// to indicate a struct or class name.
2600b57cec5SDimitry Andric     CCC_ClassOrStructTag,
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric     /// Code completion occurred where a protocol name is expected.
2630b57cec5SDimitry Andric     CCC_ObjCProtocolName,
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric     /// Code completion occurred where a namespace or namespace alias
2660b57cec5SDimitry Andric     /// is expected.
2670b57cec5SDimitry Andric     CCC_Namespace,
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric     /// Code completion occurred where a type name is expected.
2700b57cec5SDimitry Andric     CCC_Type,
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric     /// Code completion occurred where a new name is expected.
2730b57cec5SDimitry Andric     CCC_NewName,
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric     /// Code completion occurred where both a new name and an existing symbol is
2760b57cec5SDimitry Andric     /// permissible.
2770b57cec5SDimitry Andric     CCC_SymbolOrNewName,
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric     /// Code completion occurred where an existing name(such as type, function
2800b57cec5SDimitry Andric     /// or variable) is expected.
2810b57cec5SDimitry Andric     CCC_Symbol,
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric     /// Code completion occurred where an macro is being defined.
2840b57cec5SDimitry Andric     CCC_MacroName,
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric     /// Code completion occurred where a macro name is expected
2870b57cec5SDimitry Andric     /// (without any arguments, in the case of a function-like macro).
2880b57cec5SDimitry Andric     CCC_MacroNameUse,
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric     /// Code completion occurred within a preprocessor expression.
2910b57cec5SDimitry Andric     CCC_PreprocessorExpression,
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric     /// Code completion occurred where a preprocessor directive is
2940b57cec5SDimitry Andric     /// expected.
2950b57cec5SDimitry Andric     CCC_PreprocessorDirective,
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric     /// Code completion occurred in a context where natural language is
2980b57cec5SDimitry Andric     /// expected, e.g., a comment or string literal.
2990b57cec5SDimitry Andric     ///
3000b57cec5SDimitry Andric     /// This context usually implies that no completions should be added,
3010b57cec5SDimitry Andric     /// unless they come from an appropriate natural-language dictionary.
3020b57cec5SDimitry Andric     CCC_NaturalLanguage,
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric     /// Code completion for a selector, as in an \@selector expression.
3050b57cec5SDimitry Andric     CCC_SelectorName,
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric     /// Code completion within a type-qualifier list.
3080b57cec5SDimitry Andric     CCC_TypeQualifiers,
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric     /// Code completion in a parenthesized expression, which means that
3110b57cec5SDimitry Andric     /// we may also have types here in C and Objective-C (as well as in C++).
3120b57cec5SDimitry Andric     CCC_ParenthesizedExpression,
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric     /// Code completion where an Objective-C instance message is
3150b57cec5SDimitry Andric     /// expected.
3160b57cec5SDimitry Andric     CCC_ObjCInstanceMessage,
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric     /// Code completion where an Objective-C class message is expected.
3190b57cec5SDimitry Andric     CCC_ObjCClassMessage,
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric     /// Code completion where the name of an Objective-C class is
3220b57cec5SDimitry Andric     /// expected.
3230b57cec5SDimitry Andric     CCC_ObjCInterfaceName,
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric     /// Code completion where an Objective-C category name is expected.
3260b57cec5SDimitry Andric     CCC_ObjCCategoryName,
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric     /// Code completion inside the filename part of a #include directive.
3290b57cec5SDimitry Andric     CCC_IncludedFile,
3300b57cec5SDimitry Andric 
331349cc55cSDimitry Andric     /// Code completion of an attribute name.
332349cc55cSDimitry Andric     CCC_Attribute,
333349cc55cSDimitry Andric 
3340b57cec5SDimitry Andric     /// An unknown context, in which we are recovering from a parsing
3350b57cec5SDimitry Andric     /// error and don't know which completions we should give.
33606c3fb27SDimitry Andric     CCC_Recovery,
33706c3fb27SDimitry Andric 
33806c3fb27SDimitry Andric     /// Code completion in a @class forward declaration.
3395f757f3fSDimitry Andric     CCC_ObjCClassForwardDecl,
3405f757f3fSDimitry Andric 
3415f757f3fSDimitry Andric     /// Code completion at a top level, i.e. in a namespace or global scope,
3425f757f3fSDimitry Andric     /// but also in expression statements. This is because REPL inputs can be
3435f757f3fSDimitry Andric     /// declarations or expression statements.
3445f757f3fSDimitry Andric     CCC_TopLevelOrExpression,
3450b57cec5SDimitry Andric   };
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric   using VisitedContextSet = llvm::SmallPtrSet<DeclContext *, 8>;
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric private:
3500b57cec5SDimitry Andric   Kind CCKind;
3510b57cec5SDimitry Andric 
352480093f4SDimitry Andric   /// Indicates whether we are completing a name of a using declaration, e.g.
353480093f4SDimitry Andric   ///     using ^;
354480093f4SDimitry Andric   ///     using a::^;
355480093f4SDimitry Andric   bool IsUsingDeclaration;
356480093f4SDimitry Andric 
3570b57cec5SDimitry Andric   /// The type that would prefer to see at this point (e.g., the type
3580b57cec5SDimitry Andric   /// of an initializer or function parameter).
3590b57cec5SDimitry Andric   QualType PreferredType;
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   /// The type of the base object in a member access expression.
3620b57cec5SDimitry Andric   QualType BaseType;
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   /// The identifiers for Objective-C selector parts.
365*0fca6ea1SDimitry Andric   ArrayRef<const IdentifierInfo *> SelIdents;
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric   /// The scope specifier that comes before the completion token e.g.
3680b57cec5SDimitry Andric   /// "a::b::"
369bdd1243dSDimitry Andric   std::optional<CXXScopeSpec> ScopeSpecifier;
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric   /// A set of declaration contexts visited by Sema when doing lookup for
3720b57cec5SDimitry Andric   /// code completion.
3730b57cec5SDimitry Andric   VisitedContextSet VisitedContexts;
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric public:
3760b57cec5SDimitry Andric   /// Construct a new code-completion context of the given kind.
CodeCompletionContext(Kind CCKind)377480093f4SDimitry Andric   CodeCompletionContext(Kind CCKind)
378bdd1243dSDimitry Andric       : CCKind(CCKind), IsUsingDeclaration(false), SelIdents(std::nullopt) {}
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   /// Construct a new code-completion context of the given kind.
381*0fca6ea1SDimitry Andric   CodeCompletionContext(
382*0fca6ea1SDimitry Andric       Kind CCKind, QualType T,
383*0fca6ea1SDimitry Andric       ArrayRef<const IdentifierInfo *> SelIdents = std::nullopt)
CCKind(CCKind)384480093f4SDimitry Andric       : CCKind(CCKind), IsUsingDeclaration(false), SelIdents(SelIdents) {
3850b57cec5SDimitry Andric     if (CCKind == CCC_DotMemberAccess || CCKind == CCC_ArrowMemberAccess ||
3860b57cec5SDimitry Andric         CCKind == CCC_ObjCPropertyAccess || CCKind == CCC_ObjCClassMessage ||
3870b57cec5SDimitry Andric         CCKind == CCC_ObjCInstanceMessage)
3880b57cec5SDimitry Andric       BaseType = T;
3890b57cec5SDimitry Andric     else
3900b57cec5SDimitry Andric       PreferredType = T;
3910b57cec5SDimitry Andric   }
3920b57cec5SDimitry Andric 
isUsingDeclaration()393480093f4SDimitry Andric   bool isUsingDeclaration() const { return IsUsingDeclaration; }
setIsUsingDeclaration(bool V)394480093f4SDimitry Andric   void setIsUsingDeclaration(bool V) { IsUsingDeclaration = V; }
395480093f4SDimitry Andric 
3960b57cec5SDimitry Andric   /// Retrieve the kind of code-completion context.
getKind()3970b57cec5SDimitry Andric   Kind getKind() const { return CCKind; }
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric   /// Retrieve the type that this expression would prefer to have, e.g.,
4000b57cec5SDimitry Andric   /// if the expression is a variable initializer or a function argument, the
4010b57cec5SDimitry Andric   /// type of the corresponding variable or function parameter.
getPreferredType()4020b57cec5SDimitry Andric   QualType getPreferredType() const { return PreferredType; }
setPreferredType(QualType T)4030b57cec5SDimitry Andric   void setPreferredType(QualType T) { PreferredType = T; }
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric   /// Retrieve the type of the base object in a member-access
4060b57cec5SDimitry Andric   /// expression.
getBaseType()4070b57cec5SDimitry Andric   QualType getBaseType() const { return BaseType; }
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   /// Retrieve the Objective-C selector identifiers.
getSelIdents()410*0fca6ea1SDimitry Andric   ArrayRef<const IdentifierInfo *> getSelIdents() const { return SelIdents; }
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric   /// Determines whether we want C++ constructors as results within this
4130b57cec5SDimitry Andric   /// context.
4140b57cec5SDimitry Andric   bool wantConstructorResults() const;
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric   /// Sets the scope specifier that comes before the completion token.
4170b57cec5SDimitry Andric   /// This is expected to be set in code completions on qualfied specifiers
4180b57cec5SDimitry Andric   /// (e.g. "a::b::").
setCXXScopeSpecifier(CXXScopeSpec SS)4190b57cec5SDimitry Andric   void setCXXScopeSpecifier(CXXScopeSpec SS) {
4200b57cec5SDimitry Andric     this->ScopeSpecifier = std::move(SS);
4210b57cec5SDimitry Andric   }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric   /// Adds a visited context.
addVisitedContext(DeclContext * Ctx)4240b57cec5SDimitry Andric   void addVisitedContext(DeclContext *Ctx) {
4250b57cec5SDimitry Andric     VisitedContexts.insert(Ctx);
4260b57cec5SDimitry Andric   }
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   /// Retrieves all visited contexts.
getVisitedContexts()4290b57cec5SDimitry Andric   const VisitedContextSet &getVisitedContexts() const {
4300b57cec5SDimitry Andric     return VisitedContexts;
4310b57cec5SDimitry Andric   }
4320b57cec5SDimitry Andric 
getCXXScopeSpecifier()433bdd1243dSDimitry Andric   std::optional<const CXXScopeSpec *> getCXXScopeSpecifier() {
4340b57cec5SDimitry Andric     if (ScopeSpecifier)
435bdd1243dSDimitry Andric       return &*ScopeSpecifier;
436bdd1243dSDimitry Andric     return std::nullopt;
4370b57cec5SDimitry Andric   }
4380b57cec5SDimitry Andric };
4390b57cec5SDimitry Andric 
440bdd1243dSDimitry Andric /// Get string representation of \p Kind, useful for debugging.
4410b57cec5SDimitry Andric llvm::StringRef getCompletionKindString(CodeCompletionContext::Kind Kind);
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric /// A "string" used to describe how code completion can
4440b57cec5SDimitry Andric /// be performed for an entity.
4450b57cec5SDimitry Andric ///
4460b57cec5SDimitry Andric /// A code completion string typically shows how a particular entity can be
4470b57cec5SDimitry Andric /// used. For example, the code completion string for a function would show
4480b57cec5SDimitry Andric /// the syntax to call it, including the parentheses, placeholders for the
4490b57cec5SDimitry Andric /// arguments, etc.
4500b57cec5SDimitry Andric class CodeCompletionString {
4510b57cec5SDimitry Andric public:
4520b57cec5SDimitry Andric   /// The different kinds of "chunks" that can occur within a code
4530b57cec5SDimitry Andric   /// completion string.
4540b57cec5SDimitry Andric   enum ChunkKind {
4550b57cec5SDimitry Andric     /// The piece of text that the user is expected to type to
4560b57cec5SDimitry Andric     /// match the code-completion string, typically a keyword or the name of a
4570b57cec5SDimitry Andric     /// declarator or macro.
4580b57cec5SDimitry Andric     CK_TypedText,
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric     /// A piece of text that should be placed in the buffer, e.g.,
4610b57cec5SDimitry Andric     /// parentheses or a comma in a function call.
4620b57cec5SDimitry Andric     CK_Text,
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric     /// A code completion string that is entirely optional. For example,
4650b57cec5SDimitry Andric     /// an optional code completion string that describes the default arguments
4660b57cec5SDimitry Andric     /// in a function call.
4670b57cec5SDimitry Andric     CK_Optional,
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric     /// A string that acts as a placeholder for, e.g., a function
4700b57cec5SDimitry Andric     /// call argument.
4710b57cec5SDimitry Andric     CK_Placeholder,
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric     /// A piece of text that describes something about the result but
4740b57cec5SDimitry Andric     /// should not be inserted into the buffer.
4750b57cec5SDimitry Andric     CK_Informative,
4760b57cec5SDimitry Andric     /// A piece of text that describes the type of an entity or, for
4770b57cec5SDimitry Andric     /// functions and methods, the return type.
4780b57cec5SDimitry Andric     CK_ResultType,
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric     /// A piece of text that describes the parameter that corresponds
4810b57cec5SDimitry Andric     /// to the code-completion location within a function call, message send,
4820b57cec5SDimitry Andric     /// macro invocation, etc.
4830b57cec5SDimitry Andric     CK_CurrentParameter,
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric     /// A left parenthesis ('(').
4860b57cec5SDimitry Andric     CK_LeftParen,
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric     /// A right parenthesis (')').
4890b57cec5SDimitry Andric     CK_RightParen,
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric     /// A left bracket ('[').
4920b57cec5SDimitry Andric     CK_LeftBracket,
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric     /// A right bracket (']').
4950b57cec5SDimitry Andric     CK_RightBracket,
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric     /// A left brace ('{').
4980b57cec5SDimitry Andric     CK_LeftBrace,
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric     /// A right brace ('}').
5010b57cec5SDimitry Andric     CK_RightBrace,
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric     /// A left angle bracket ('<').
5040b57cec5SDimitry Andric     CK_LeftAngle,
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric     /// A right angle bracket ('>').
5070b57cec5SDimitry Andric     CK_RightAngle,
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric     /// A comma separator (',').
5100b57cec5SDimitry Andric     CK_Comma,
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric     /// A colon (':').
5130b57cec5SDimitry Andric     CK_Colon,
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric     /// A semicolon (';').
5160b57cec5SDimitry Andric     CK_SemiColon,
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric     /// An '=' sign.
5190b57cec5SDimitry Andric     CK_Equal,
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric     /// Horizontal whitespace (' ').
5220b57cec5SDimitry Andric     CK_HorizontalSpace,
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric     /// Vertical whitespace ('\\n' or '\\r\\n', depending on the
5250b57cec5SDimitry Andric     /// platform).
5260b57cec5SDimitry Andric     CK_VerticalSpace
5270b57cec5SDimitry Andric   };
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric   /// One piece of the code completion string.
5300b57cec5SDimitry Andric   struct Chunk {
5310b57cec5SDimitry Andric     /// The kind of data stored in this piece of the code completion
5320b57cec5SDimitry Andric     /// string.
5330b57cec5SDimitry Andric     ChunkKind Kind = CK_Text;
5340b57cec5SDimitry Andric 
5350b57cec5SDimitry Andric     union {
5360b57cec5SDimitry Andric       /// The text string associated with a CK_Text, CK_Placeholder,
5370b57cec5SDimitry Andric       /// CK_Informative, or CK_Comma chunk.
5380b57cec5SDimitry Andric       /// The string is owned by the chunk and will be deallocated
5390b57cec5SDimitry Andric       /// (with delete[]) when the chunk is destroyed.
5400b57cec5SDimitry Andric       const char *Text;
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric       /// The code completion string associated with a CK_Optional chunk.
5430b57cec5SDimitry Andric       /// The optional code completion string is owned by the chunk, and will
5440b57cec5SDimitry Andric       /// be deallocated (with delete) when the chunk is destroyed.
5450b57cec5SDimitry Andric       CodeCompletionString *Optional;
5460b57cec5SDimitry Andric     };
5470b57cec5SDimitry Andric 
ChunkChunk5480b57cec5SDimitry Andric     Chunk() : Text(nullptr) {}
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric     explicit Chunk(ChunkKind Kind, const char *Text = "");
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric     /// Create a new text chunk.
5530b57cec5SDimitry Andric     static Chunk CreateText(const char *Text);
5540b57cec5SDimitry Andric 
5550b57cec5SDimitry Andric     /// Create a new optional chunk.
5560b57cec5SDimitry Andric     static Chunk CreateOptional(CodeCompletionString *Optional);
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric     /// Create a new placeholder chunk.
5590b57cec5SDimitry Andric     static Chunk CreatePlaceholder(const char *Placeholder);
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric     /// Create a new informative chunk.
5620b57cec5SDimitry Andric     static Chunk CreateInformative(const char *Informative);
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric     /// Create a new result type chunk.
5650b57cec5SDimitry Andric     static Chunk CreateResultType(const char *ResultType);
5660b57cec5SDimitry Andric 
5670b57cec5SDimitry Andric     /// Create a new current-parameter chunk.
5680b57cec5SDimitry Andric     static Chunk CreateCurrentParameter(const char *CurrentParameter);
5690b57cec5SDimitry Andric   };
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric private:
5720b57cec5SDimitry Andric   friend class CodeCompletionBuilder;
5730b57cec5SDimitry Andric   friend class CodeCompletionResult;
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric   /// The number of chunks stored in this string.
5760b57cec5SDimitry Andric   unsigned NumChunks : 16;
5770b57cec5SDimitry Andric 
5780b57cec5SDimitry Andric   /// The number of annotations for this code-completion result.
5790b57cec5SDimitry Andric   unsigned NumAnnotations : 16;
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric   /// The priority of this code-completion string.
5820b57cec5SDimitry Andric   unsigned Priority : 16;
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric   /// The availability of this code-completion result.
585*0fca6ea1SDimitry Andric   LLVM_PREFERRED_TYPE(CXAvailabilityKind)
5860b57cec5SDimitry Andric   unsigned Availability : 2;
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric   /// The name of the parent context.
5890b57cec5SDimitry Andric   StringRef ParentName;
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric   /// A brief documentation comment attached to the declaration of
5920b57cec5SDimitry Andric   /// entity being completed by this result.
5930b57cec5SDimitry Andric   const char *BriefComment;
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric   CodeCompletionString(const Chunk *Chunks, unsigned NumChunks,
5960b57cec5SDimitry Andric                        unsigned Priority, CXAvailabilityKind Availability,
5970b57cec5SDimitry Andric                        const char **Annotations, unsigned NumAnnotations,
5980b57cec5SDimitry Andric                        StringRef ParentName,
5990b57cec5SDimitry Andric                        const char *BriefComment);
6000b57cec5SDimitry Andric   ~CodeCompletionString() = default;
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric public:
6030b57cec5SDimitry Andric   CodeCompletionString(const CodeCompletionString &) = delete;
6040b57cec5SDimitry Andric   CodeCompletionString &operator=(const CodeCompletionString &) = delete;
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric   using iterator = const Chunk *;
6070b57cec5SDimitry Andric 
begin()6080b57cec5SDimitry Andric   iterator begin() const { return reinterpret_cast<const Chunk *>(this + 1); }
end()6090b57cec5SDimitry Andric   iterator end() const { return begin() + NumChunks; }
empty()6100b57cec5SDimitry Andric   bool empty() const { return NumChunks == 0; }
size()6110b57cec5SDimitry Andric   unsigned size() const { return NumChunks; }
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric   const Chunk &operator[](unsigned I) const {
6140b57cec5SDimitry Andric     assert(I < size() && "Chunk index out-of-range");
6150b57cec5SDimitry Andric     return begin()[I];
6160b57cec5SDimitry Andric   }
6170b57cec5SDimitry Andric 
61881ad6265SDimitry Andric   /// Returns the text in the first TypedText chunk.
6190b57cec5SDimitry Andric   const char *getTypedText() const;
6200b57cec5SDimitry Andric 
62181ad6265SDimitry Andric   /// Returns the combined text from all TypedText chunks.
62281ad6265SDimitry Andric   std::string getAllTypedText() const;
62381ad6265SDimitry Andric 
6240b57cec5SDimitry Andric   /// Retrieve the priority of this code completion result.
getPriority()6250b57cec5SDimitry Andric   unsigned getPriority() const { return Priority; }
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric   /// Retrieve the availability of this code completion result.
getAvailability()6280b57cec5SDimitry Andric   unsigned getAvailability() const { return Availability; }
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric   /// Retrieve the number of annotations for this code completion result.
6310b57cec5SDimitry Andric   unsigned getAnnotationCount() const;
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric   /// Retrieve the annotation string specified by \c AnnotationNr.
6340b57cec5SDimitry Andric   const char *getAnnotation(unsigned AnnotationNr) const;
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric   /// Retrieve the name of the parent context.
getParentContextName()6370b57cec5SDimitry Andric   StringRef getParentContextName() const {
6380b57cec5SDimitry Andric     return ParentName;
6390b57cec5SDimitry Andric   }
6400b57cec5SDimitry Andric 
getBriefComment()6410b57cec5SDimitry Andric   const char *getBriefComment() const {
6420b57cec5SDimitry Andric     return BriefComment;
6430b57cec5SDimitry Andric   }
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric   /// Retrieve a string representation of the code completion string,
6460b57cec5SDimitry Andric   /// which is mainly useful for debugging.
6470b57cec5SDimitry Andric   std::string getAsString() const;
6480b57cec5SDimitry Andric };
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric /// An allocator used specifically for the purpose of code completion.
6510b57cec5SDimitry Andric class CodeCompletionAllocator : public llvm::BumpPtrAllocator {
6520b57cec5SDimitry Andric public:
6530b57cec5SDimitry Andric   /// Copy the given string into this allocator.
6540b57cec5SDimitry Andric   const char *CopyString(const Twine &String);
6550b57cec5SDimitry Andric };
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric /// Allocator for a cached set of global code completions.
6580b57cec5SDimitry Andric class GlobalCodeCompletionAllocator : public CodeCompletionAllocator {};
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric class CodeCompletionTUInfo {
6610b57cec5SDimitry Andric   llvm::DenseMap<const DeclContext *, StringRef> ParentNames;
6620b57cec5SDimitry Andric   std::shared_ptr<GlobalCodeCompletionAllocator> AllocatorRef;
6630b57cec5SDimitry Andric 
6640b57cec5SDimitry Andric public:
CodeCompletionTUInfo(std::shared_ptr<GlobalCodeCompletionAllocator> Allocator)6650b57cec5SDimitry Andric   explicit CodeCompletionTUInfo(
6660b57cec5SDimitry Andric       std::shared_ptr<GlobalCodeCompletionAllocator> Allocator)
6670b57cec5SDimitry Andric       : AllocatorRef(std::move(Allocator)) {}
6680b57cec5SDimitry Andric 
getAllocatorRef()6690b57cec5SDimitry Andric   std::shared_ptr<GlobalCodeCompletionAllocator> getAllocatorRef() const {
6700b57cec5SDimitry Andric     return AllocatorRef;
6710b57cec5SDimitry Andric   }
6720b57cec5SDimitry Andric 
getAllocator()6730b57cec5SDimitry Andric   CodeCompletionAllocator &getAllocator() const {
6740b57cec5SDimitry Andric     assert(AllocatorRef);
6750b57cec5SDimitry Andric     return *AllocatorRef;
6760b57cec5SDimitry Andric   }
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric   StringRef getParentName(const DeclContext *DC);
6790b57cec5SDimitry Andric };
6800b57cec5SDimitry Andric 
6810b57cec5SDimitry Andric } // namespace clang
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric namespace clang {
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric /// A builder class used to construct new code-completion strings.
6860b57cec5SDimitry Andric class CodeCompletionBuilder {
6870b57cec5SDimitry Andric public:
6880b57cec5SDimitry Andric   using Chunk = CodeCompletionString::Chunk;
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric private:
6910b57cec5SDimitry Andric   CodeCompletionAllocator &Allocator;
6920b57cec5SDimitry Andric   CodeCompletionTUInfo &CCTUInfo;
6930b57cec5SDimitry Andric   unsigned Priority = 0;
6940b57cec5SDimitry Andric   CXAvailabilityKind Availability = CXAvailability_Available;
6950b57cec5SDimitry Andric   StringRef ParentName;
6960b57cec5SDimitry Andric   const char *BriefComment = nullptr;
6970b57cec5SDimitry Andric 
6980b57cec5SDimitry Andric   /// The chunks stored in this string.
6990b57cec5SDimitry Andric   SmallVector<Chunk, 4> Chunks;
7000b57cec5SDimitry Andric 
7010b57cec5SDimitry Andric   SmallVector<const char *, 2> Annotations;
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric public:
CodeCompletionBuilder(CodeCompletionAllocator & Allocator,CodeCompletionTUInfo & CCTUInfo)7040b57cec5SDimitry Andric   CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
7050b57cec5SDimitry Andric                         CodeCompletionTUInfo &CCTUInfo)
7060b57cec5SDimitry Andric       : Allocator(Allocator), CCTUInfo(CCTUInfo) {}
7070b57cec5SDimitry Andric 
CodeCompletionBuilder(CodeCompletionAllocator & Allocator,CodeCompletionTUInfo & CCTUInfo,unsigned Priority,CXAvailabilityKind Availability)7080b57cec5SDimitry Andric   CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
7090b57cec5SDimitry Andric                         CodeCompletionTUInfo &CCTUInfo,
7100b57cec5SDimitry Andric                         unsigned Priority, CXAvailabilityKind Availability)
7110b57cec5SDimitry Andric       : Allocator(Allocator), CCTUInfo(CCTUInfo), Priority(Priority),
7120b57cec5SDimitry Andric         Availability(Availability) {}
7130b57cec5SDimitry Andric 
7140b57cec5SDimitry Andric   /// Retrieve the allocator into which the code completion
7150b57cec5SDimitry Andric   /// strings should be allocated.
getAllocator()7160b57cec5SDimitry Andric   CodeCompletionAllocator &getAllocator() const { return Allocator; }
7170b57cec5SDimitry Andric 
getCodeCompletionTUInfo()7180b57cec5SDimitry Andric   CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric   /// Take the resulting completion string.
7210b57cec5SDimitry Andric   ///
7220b57cec5SDimitry Andric   /// This operation can only be performed once.
7230b57cec5SDimitry Andric   CodeCompletionString *TakeString();
7240b57cec5SDimitry Andric 
7250b57cec5SDimitry Andric   /// Add a new typed-text chunk.
7260b57cec5SDimitry Andric   void AddTypedTextChunk(const char *Text);
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric   /// Add a new text chunk.
7290b57cec5SDimitry Andric   void AddTextChunk(const char *Text);
7300b57cec5SDimitry Andric 
7310b57cec5SDimitry Andric   /// Add a new optional chunk.
7320b57cec5SDimitry Andric   void AddOptionalChunk(CodeCompletionString *Optional);
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric   /// Add a new placeholder chunk.
7350b57cec5SDimitry Andric   void AddPlaceholderChunk(const char *Placeholder);
7360b57cec5SDimitry Andric 
7370b57cec5SDimitry Andric   /// Add a new informative chunk.
7380b57cec5SDimitry Andric   void AddInformativeChunk(const char *Text);
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric   /// Add a new result-type chunk.
7410b57cec5SDimitry Andric   void AddResultTypeChunk(const char *ResultType);
7420b57cec5SDimitry Andric 
7430b57cec5SDimitry Andric   /// Add a new current-parameter chunk.
7440b57cec5SDimitry Andric   void AddCurrentParameterChunk(const char *CurrentParameter);
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric   /// Add a new chunk.
7470b57cec5SDimitry Andric   void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text = "");
7480b57cec5SDimitry Andric 
AddAnnotation(const char * A)7490b57cec5SDimitry Andric   void AddAnnotation(const char *A) { Annotations.push_back(A); }
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric   /// Add the parent context information to this code completion.
7520b57cec5SDimitry Andric   void addParentContext(const DeclContext *DC);
7530b57cec5SDimitry Andric 
getBriefComment()7540b57cec5SDimitry Andric   const char *getBriefComment() const { return BriefComment; }
7550b57cec5SDimitry Andric   void addBriefComment(StringRef Comment);
7560b57cec5SDimitry Andric 
getParentName()7570b57cec5SDimitry Andric   StringRef getParentName() const { return ParentName; }
7580b57cec5SDimitry Andric };
7590b57cec5SDimitry Andric 
7600b57cec5SDimitry Andric /// Captures a result of code completion.
7610b57cec5SDimitry Andric class CodeCompletionResult {
7620b57cec5SDimitry Andric public:
7630b57cec5SDimitry Andric   /// Describes the kind of result generated.
7640b57cec5SDimitry Andric   enum ResultKind {
7650b57cec5SDimitry Andric     /// Refers to a declaration.
7660b57cec5SDimitry Andric     RK_Declaration = 0,
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric     /// Refers to a keyword or symbol.
7690b57cec5SDimitry Andric     RK_Keyword,
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric     /// Refers to a macro.
7720b57cec5SDimitry Andric     RK_Macro,
7730b57cec5SDimitry Andric 
7740b57cec5SDimitry Andric     /// Refers to a precomputed pattern.
7750b57cec5SDimitry Andric     RK_Pattern
7760b57cec5SDimitry Andric   };
7770b57cec5SDimitry Andric 
7780b57cec5SDimitry Andric   /// When Kind == RK_Declaration or RK_Pattern, the declaration we are
7790b57cec5SDimitry Andric   /// referring to. In the latter case, the declaration might be NULL.
7800b57cec5SDimitry Andric   const NamedDecl *Declaration = nullptr;
7810b57cec5SDimitry Andric 
7820b57cec5SDimitry Andric   union {
7830b57cec5SDimitry Andric     /// When Kind == RK_Keyword, the string representing the keyword
7840b57cec5SDimitry Andric     /// or symbol's spelling.
7850b57cec5SDimitry Andric     const char *Keyword;
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric     /// When Kind == RK_Pattern, the code-completion string that
7880b57cec5SDimitry Andric     /// describes the completion text to insert.
7890b57cec5SDimitry Andric     CodeCompletionString *Pattern;
7900b57cec5SDimitry Andric 
7910b57cec5SDimitry Andric     /// When Kind == RK_Macro, the identifier that refers to a macro.
7920b57cec5SDimitry Andric     const IdentifierInfo *Macro;
7930b57cec5SDimitry Andric   };
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric   /// The priority of this particular code-completion result.
7960b57cec5SDimitry Andric   unsigned Priority;
7970b57cec5SDimitry Andric 
7980b57cec5SDimitry Andric   /// Specifies which parameter (of a function, Objective-C method,
7990b57cec5SDimitry Andric   /// macro, etc.) we should start with when formatting the result.
8000b57cec5SDimitry Andric   unsigned StartParameter = 0;
8010b57cec5SDimitry Andric 
8020b57cec5SDimitry Andric   /// The kind of result stored here.
8030b57cec5SDimitry Andric   ResultKind Kind;
8040b57cec5SDimitry Andric 
8050b57cec5SDimitry Andric   /// The cursor kind that describes this result.
8060b57cec5SDimitry Andric   CXCursorKind CursorKind;
8070b57cec5SDimitry Andric 
8080b57cec5SDimitry Andric   /// The availability of this result.
8090b57cec5SDimitry Andric   CXAvailabilityKind Availability = CXAvailability_Available;
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric   /// Fix-its that *must* be applied before inserting the text for the
8120b57cec5SDimitry Andric   /// corresponding completion.
8130b57cec5SDimitry Andric   ///
8140b57cec5SDimitry Andric   /// By default, CodeCompletionBuilder only returns completions with empty
8150b57cec5SDimitry Andric   /// fix-its. Extra completions with non-empty fix-its should be explicitly
8160b57cec5SDimitry Andric   /// requested by setting CompletionOptions::IncludeFixIts.
8170b57cec5SDimitry Andric   ///
8180b57cec5SDimitry Andric   /// For the clients to be able to compute position of the cursor after
8190b57cec5SDimitry Andric   /// applying fix-its, the following conditions are guaranteed to hold for
8200b57cec5SDimitry Andric   /// RemoveRange of the stored fix-its:
8210b57cec5SDimitry Andric   ///  - Ranges in the fix-its are guaranteed to never contain the completion
8220b57cec5SDimitry Andric   ///  point (or identifier under completion point, if any) inside them, except
8230b57cec5SDimitry Andric   ///  at the start or at the end of the range.
8240b57cec5SDimitry Andric   ///  - If a fix-it range starts or ends with completion point (or starts or
8250b57cec5SDimitry Andric   ///  ends after the identifier under completion point), it will contain at
8260b57cec5SDimitry Andric   ///  least one character. It allows to unambiguously recompute completion
8270b57cec5SDimitry Andric   ///  point after applying the fix-it.
8280b57cec5SDimitry Andric   ///
8290b57cec5SDimitry Andric   /// The intuition is that provided fix-its change code around the identifier
8300b57cec5SDimitry Andric   /// we complete, but are not allowed to touch the identifier itself or the
8310b57cec5SDimitry Andric   /// completion point. One example of completions with corrections are the ones
8320b57cec5SDimitry Andric   /// replacing '.' with '->' and vice versa:
8330b57cec5SDimitry Andric   ///
8340b57cec5SDimitry Andric   /// std::unique_ptr<std::vector<int>> vec_ptr;
8350b57cec5SDimitry Andric   /// In 'vec_ptr.^', one of the completions is 'push_back', it requires
8360b57cec5SDimitry Andric   /// replacing '.' with '->'.
8370b57cec5SDimitry Andric   /// In 'vec_ptr->^', one of the completions is 'release', it requires
8380b57cec5SDimitry Andric   /// replacing '->' with '.'.
8390b57cec5SDimitry Andric   std::vector<FixItHint> FixIts;
8400b57cec5SDimitry Andric 
8410b57cec5SDimitry Andric   /// Whether this result is hidden by another name.
8420b57cec5SDimitry Andric   bool Hidden : 1;
8430b57cec5SDimitry Andric 
8440b57cec5SDimitry Andric   /// Whether this is a class member from base class.
8450b57cec5SDimitry Andric   bool InBaseClass : 1;
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric   /// Whether this result was found via lookup into a base class.
8480b57cec5SDimitry Andric   bool QualifierIsInformative : 1;
8490b57cec5SDimitry Andric 
8500b57cec5SDimitry Andric   /// Whether this declaration is the beginning of a
8510b57cec5SDimitry Andric   /// nested-name-specifier and, therefore, should be followed by '::'.
8520b57cec5SDimitry Andric   bool StartsNestedNameSpecifier : 1;
8530b57cec5SDimitry Andric 
8540b57cec5SDimitry Andric   /// Whether all parameters (of a function, Objective-C
8550b57cec5SDimitry Andric   /// method, etc.) should be considered "informative".
8560b57cec5SDimitry Andric   bool AllParametersAreInformative : 1;
8570b57cec5SDimitry Andric 
8580b57cec5SDimitry Andric   /// Whether we're completing a declaration of the given entity,
8590b57cec5SDimitry Andric   /// rather than a use of that entity.
8600b57cec5SDimitry Andric   bool DeclaringEntity : 1;
8610b57cec5SDimitry Andric 
862bdd1243dSDimitry Andric   /// When completing a function, whether it can be a call. This will usually be
863bdd1243dSDimitry Andric   /// true, but we have some heuristics, e.g. when a pointer to a non-static
864bdd1243dSDimitry Andric   /// member function is completed outside of that class' scope, it can never
865bdd1243dSDimitry Andric   /// be a call.
866bdd1243dSDimitry Andric   bool FunctionCanBeCall : 1;
867bdd1243dSDimitry Andric 
8680b57cec5SDimitry Andric   /// If the result should have a nested-name-specifier, this is it.
8690b57cec5SDimitry Andric   /// When \c QualifierIsInformative, the nested-name-specifier is
8700b57cec5SDimitry Andric   /// informative rather than required.
8710b57cec5SDimitry Andric   NestedNameSpecifier *Qualifier = nullptr;
8720b57cec5SDimitry Andric 
8730b57cec5SDimitry Andric   /// If this Decl was unshadowed by using declaration, this can store a
8740b57cec5SDimitry Andric   /// pointer to the UsingShadowDecl which was used in the unshadowing process.
8750b57cec5SDimitry Andric   /// This information can be used to uprank CodeCompletionResults / which have
8760b57cec5SDimitry Andric   /// corresponding `using decl::qualified::name;` nearby.
8770b57cec5SDimitry Andric   const UsingShadowDecl *ShadowDecl = nullptr;
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   /// If the result is RK_Macro, this can store the information about the macro
8800b57cec5SDimitry Andric   /// definition. This should be set in most cases but can be missing when
8810b57cec5SDimitry Andric   /// the macro has been undefined.
8820b57cec5SDimitry Andric   const MacroInfo *MacroDefInfo = nullptr;
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric   /// Build a result that refers to a declaration.
8850b57cec5SDimitry Andric   CodeCompletionResult(const NamedDecl *Declaration, unsigned Priority,
8860b57cec5SDimitry Andric                        NestedNameSpecifier *Qualifier = nullptr,
8870b57cec5SDimitry Andric                        bool QualifierIsInformative = false,
8880b57cec5SDimitry Andric                        bool Accessible = true,
8890b57cec5SDimitry Andric                        std::vector<FixItHint> FixIts = std::vector<FixItHint>())
Declaration(Declaration)8900b57cec5SDimitry Andric       : Declaration(Declaration), Priority(Priority), Kind(RK_Declaration),
8910b57cec5SDimitry Andric         FixIts(std::move(FixIts)), Hidden(false), InBaseClass(false),
8920b57cec5SDimitry Andric         QualifierIsInformative(QualifierIsInformative),
8930b57cec5SDimitry Andric         StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
894bdd1243dSDimitry Andric         DeclaringEntity(false), FunctionCanBeCall(true), Qualifier(Qualifier) {
8950b57cec5SDimitry Andric     // FIXME: Add assert to check FixIts range requirements.
8960b57cec5SDimitry Andric     computeCursorKindAndAvailability(Accessible);
8970b57cec5SDimitry Andric   }
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric   /// Build a result that refers to a keyword or symbol.
9000b57cec5SDimitry Andric   CodeCompletionResult(const char *Keyword, unsigned Priority = CCP_Keyword)
Keyword(Keyword)9010b57cec5SDimitry Andric       : Keyword(Keyword), Priority(Priority), Kind(RK_Keyword),
9020b57cec5SDimitry Andric         CursorKind(CXCursor_NotImplemented), Hidden(false), InBaseClass(false),
9030b57cec5SDimitry Andric         QualifierIsInformative(false), StartsNestedNameSpecifier(false),
904bdd1243dSDimitry Andric         AllParametersAreInformative(false), DeclaringEntity(false),
905bdd1243dSDimitry Andric         FunctionCanBeCall(true) {}
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric   /// Build a result that refers to a macro.
9080b57cec5SDimitry Andric   CodeCompletionResult(const IdentifierInfo *Macro,
9090b57cec5SDimitry Andric                        const MacroInfo *MI = nullptr,
9100b57cec5SDimitry Andric                        unsigned Priority = CCP_Macro)
Macro(Macro)9110b57cec5SDimitry Andric       : Macro(Macro), Priority(Priority), Kind(RK_Macro),
9120b57cec5SDimitry Andric         CursorKind(CXCursor_MacroDefinition), Hidden(false), InBaseClass(false),
9130b57cec5SDimitry Andric         QualifierIsInformative(false), StartsNestedNameSpecifier(false),
9140b57cec5SDimitry Andric         AllParametersAreInformative(false), DeclaringEntity(false),
915bdd1243dSDimitry Andric         FunctionCanBeCall(true), MacroDefInfo(MI) {}
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric   /// Build a result that refers to a pattern.
9180b57cec5SDimitry Andric   CodeCompletionResult(
9190b57cec5SDimitry Andric       CodeCompletionString *Pattern, unsigned Priority = CCP_CodePattern,
9200b57cec5SDimitry Andric       CXCursorKind CursorKind = CXCursor_NotImplemented,
9210b57cec5SDimitry Andric       CXAvailabilityKind Availability = CXAvailability_Available,
9220b57cec5SDimitry Andric       const NamedDecl *D = nullptr)
Declaration(D)9230b57cec5SDimitry Andric       : Declaration(D), Pattern(Pattern), Priority(Priority), Kind(RK_Pattern),
9240b57cec5SDimitry Andric         CursorKind(CursorKind), Availability(Availability), Hidden(false),
9250b57cec5SDimitry Andric         InBaseClass(false), QualifierIsInformative(false),
9260b57cec5SDimitry Andric         StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
927bdd1243dSDimitry Andric         DeclaringEntity(false), FunctionCanBeCall(true) {}
9280b57cec5SDimitry Andric 
9290b57cec5SDimitry Andric   /// Build a result that refers to a pattern with an associated
9300b57cec5SDimitry Andric   /// declaration.
CodeCompletionResult(CodeCompletionString * Pattern,const NamedDecl * D,unsigned Priority)9310b57cec5SDimitry Andric   CodeCompletionResult(CodeCompletionString *Pattern, const NamedDecl *D,
9320b57cec5SDimitry Andric                        unsigned Priority)
9330b57cec5SDimitry Andric       : Declaration(D), Pattern(Pattern), Priority(Priority), Kind(RK_Pattern),
9340b57cec5SDimitry Andric         Hidden(false), InBaseClass(false), QualifierIsInformative(false),
9350b57cec5SDimitry Andric         StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
936bdd1243dSDimitry Andric         DeclaringEntity(false), FunctionCanBeCall(true) {
9370b57cec5SDimitry Andric     computeCursorKindAndAvailability();
9380b57cec5SDimitry Andric   }
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric   /// Retrieve the declaration stored in this result. This might be nullptr if
9410b57cec5SDimitry Andric   /// Kind is RK_Pattern.
getDeclaration()9420b57cec5SDimitry Andric   const NamedDecl *getDeclaration() const {
9430b57cec5SDimitry Andric     assert(((Kind == RK_Declaration) || (Kind == RK_Pattern)) &&
9440b57cec5SDimitry Andric            "Not a declaration or pattern result");
9450b57cec5SDimitry Andric     return Declaration;
9460b57cec5SDimitry Andric   }
9470b57cec5SDimitry Andric 
9480b57cec5SDimitry Andric   /// Retrieve the keyword stored in this result.
getKeyword()9490b57cec5SDimitry Andric   const char *getKeyword() const {
9500b57cec5SDimitry Andric     assert(Kind == RK_Keyword && "Not a keyword result");
9510b57cec5SDimitry Andric     return Keyword;
9520b57cec5SDimitry Andric   }
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric   /// Create a new code-completion string that describes how to insert
9550b57cec5SDimitry Andric   /// this result into a program.
9560b57cec5SDimitry Andric   ///
9570b57cec5SDimitry Andric   /// \param S The semantic analysis that created the result.
9580b57cec5SDimitry Andric   ///
9590b57cec5SDimitry Andric   /// \param Allocator The allocator that will be used to allocate the
9600b57cec5SDimitry Andric   /// string itself.
9610b57cec5SDimitry Andric   CodeCompletionString *CreateCodeCompletionString(Sema &S,
9620b57cec5SDimitry Andric                                          const CodeCompletionContext &CCContext,
9630b57cec5SDimitry Andric                                            CodeCompletionAllocator &Allocator,
9640b57cec5SDimitry Andric                                            CodeCompletionTUInfo &CCTUInfo,
9650b57cec5SDimitry Andric                                            bool IncludeBriefComments);
9660b57cec5SDimitry Andric   CodeCompletionString *CreateCodeCompletionString(ASTContext &Ctx,
9670b57cec5SDimitry Andric                                                    Preprocessor &PP,
9680b57cec5SDimitry Andric                                          const CodeCompletionContext &CCContext,
9690b57cec5SDimitry Andric                                            CodeCompletionAllocator &Allocator,
9700b57cec5SDimitry Andric                                            CodeCompletionTUInfo &CCTUInfo,
9710b57cec5SDimitry Andric                                            bool IncludeBriefComments);
9720b57cec5SDimitry Andric   /// Creates a new code-completion string for the macro result. Similar to the
9730b57cec5SDimitry Andric   /// above overloads, except this only requires preprocessor information.
9740b57cec5SDimitry Andric   /// The result kind must be `RK_Macro`.
9750b57cec5SDimitry Andric   CodeCompletionString *
9760b57cec5SDimitry Andric   CreateCodeCompletionStringForMacro(Preprocessor &PP,
9770b57cec5SDimitry Andric                                      CodeCompletionAllocator &Allocator,
9780b57cec5SDimitry Andric                                      CodeCompletionTUInfo &CCTUInfo);
9790b57cec5SDimitry Andric 
9800b57cec5SDimitry Andric   CodeCompletionString *createCodeCompletionStringForDecl(
9810b57cec5SDimitry Andric       Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result,
9820b57cec5SDimitry Andric       bool IncludeBriefComments, const CodeCompletionContext &CCContext,
9830b57cec5SDimitry Andric       PrintingPolicy &Policy);
9840b57cec5SDimitry Andric 
9850b57cec5SDimitry Andric   CodeCompletionString *createCodeCompletionStringForOverride(
9860b57cec5SDimitry Andric       Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result,
9870b57cec5SDimitry Andric       bool IncludeBriefComments, const CodeCompletionContext &CCContext,
9880b57cec5SDimitry Andric       PrintingPolicy &Policy);
9890b57cec5SDimitry Andric 
9900b57cec5SDimitry Andric   /// Retrieve the name that should be used to order a result.
9910b57cec5SDimitry Andric   ///
9920b57cec5SDimitry Andric   /// If the name needs to be constructed as a string, that string will be
9930b57cec5SDimitry Andric   /// saved into Saved and the returned StringRef will refer to it.
9940b57cec5SDimitry Andric   StringRef getOrderedName(std::string &Saved) const;
9950b57cec5SDimitry Andric 
9960b57cec5SDimitry Andric private:
9970b57cec5SDimitry Andric   void computeCursorKindAndAvailability(bool Accessible = true);
9980b57cec5SDimitry Andric };
9990b57cec5SDimitry Andric 
10000b57cec5SDimitry Andric bool operator<(const CodeCompletionResult &X, const CodeCompletionResult &Y);
10010b57cec5SDimitry Andric 
10020b57cec5SDimitry Andric inline bool operator>(const CodeCompletionResult &X,
10030b57cec5SDimitry Andric                       const CodeCompletionResult &Y) {
10040b57cec5SDimitry Andric   return Y < X;
10050b57cec5SDimitry Andric }
10060b57cec5SDimitry Andric 
10070b57cec5SDimitry Andric inline bool operator<=(const CodeCompletionResult &X,
10080b57cec5SDimitry Andric                       const CodeCompletionResult &Y) {
10090b57cec5SDimitry Andric   return !(Y < X);
10100b57cec5SDimitry Andric }
10110b57cec5SDimitry Andric 
10120b57cec5SDimitry Andric inline bool operator>=(const CodeCompletionResult &X,
10130b57cec5SDimitry Andric                        const CodeCompletionResult &Y) {
10140b57cec5SDimitry Andric   return !(X < Y);
10150b57cec5SDimitry Andric }
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric /// Abstract interface for a consumer of code-completion
10180b57cec5SDimitry Andric /// information.
10190b57cec5SDimitry Andric class CodeCompleteConsumer {
10200b57cec5SDimitry Andric protected:
10210b57cec5SDimitry Andric   const CodeCompleteOptions CodeCompleteOpts;
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric public:
10240b57cec5SDimitry Andric   class OverloadCandidate {
10250b57cec5SDimitry Andric   public:
10260b57cec5SDimitry Andric     /// Describes the type of overload candidate.
10270b57cec5SDimitry Andric     enum CandidateKind {
10280b57cec5SDimitry Andric       /// The candidate is a function declaration.
10290b57cec5SDimitry Andric       CK_Function,
10300b57cec5SDimitry Andric 
103104eeddc0SDimitry Andric       /// The candidate is a function template, arguments are being completed.
10320b57cec5SDimitry Andric       CK_FunctionTemplate,
10330b57cec5SDimitry Andric 
10340b57cec5SDimitry Andric       /// The "candidate" is actually a variable, expression, or block
10350b57cec5SDimitry Andric       /// for which we only have a function prototype.
103604eeddc0SDimitry Andric       CK_FunctionType,
103704eeddc0SDimitry Andric 
1038fcaf7f86SDimitry Andric       /// The candidate is a variable or expression of function type
1039fcaf7f86SDimitry Andric       /// for which we have the location of the prototype declaration.
1040fcaf7f86SDimitry Andric       CK_FunctionProtoTypeLoc,
1041fcaf7f86SDimitry Andric 
104204eeddc0SDimitry Andric       /// The candidate is a template, template arguments are being completed.
104304eeddc0SDimitry Andric       CK_Template,
104404eeddc0SDimitry Andric 
104504eeddc0SDimitry Andric       /// The candidate is aggregate initialization of a record type.
104604eeddc0SDimitry Andric       CK_Aggregate,
10470b57cec5SDimitry Andric     };
10480b57cec5SDimitry Andric 
10490b57cec5SDimitry Andric   private:
10500b57cec5SDimitry Andric     /// The kind of overload candidate.
10510b57cec5SDimitry Andric     CandidateKind Kind;
10520b57cec5SDimitry Andric 
10530b57cec5SDimitry Andric     union {
10540b57cec5SDimitry Andric       /// The function overload candidate, available when
10550b57cec5SDimitry Andric       /// Kind == CK_Function.
10560b57cec5SDimitry Andric       FunctionDecl *Function;
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric       /// The function template overload candidate, available when
10590b57cec5SDimitry Andric       /// Kind == CK_FunctionTemplate.
10600b57cec5SDimitry Andric       FunctionTemplateDecl *FunctionTemplate;
10610b57cec5SDimitry Andric 
10620b57cec5SDimitry Andric       /// The function type that describes the entity being called,
10630b57cec5SDimitry Andric       /// when Kind == CK_FunctionType.
10640b57cec5SDimitry Andric       const FunctionType *Type;
106504eeddc0SDimitry Andric 
1066fcaf7f86SDimitry Andric       /// The location of the function prototype that describes the entity being
1067fcaf7f86SDimitry Andric       /// called, when Kind == CK_FunctionProtoTypeLoc.
1068fcaf7f86SDimitry Andric       FunctionProtoTypeLoc ProtoTypeLoc;
1069fcaf7f86SDimitry Andric 
107004eeddc0SDimitry Andric       /// The template overload candidate, available when
107104eeddc0SDimitry Andric       /// Kind == CK_Template.
107204eeddc0SDimitry Andric       const TemplateDecl *Template;
107304eeddc0SDimitry Andric 
107404eeddc0SDimitry Andric       /// The class being aggregate-initialized,
107504eeddc0SDimitry Andric       /// when Kind == CK_Aggregate
107604eeddc0SDimitry Andric       const RecordDecl *AggregateType;
10770b57cec5SDimitry Andric     };
10780b57cec5SDimitry Andric 
10790b57cec5SDimitry Andric   public:
OverloadCandidate(FunctionDecl * Function)10800b57cec5SDimitry Andric     OverloadCandidate(FunctionDecl *Function)
108104eeddc0SDimitry Andric         : Kind(CK_Function), Function(Function) {
108204eeddc0SDimitry Andric       assert(Function != nullptr);
108304eeddc0SDimitry Andric     }
10840b57cec5SDimitry Andric 
OverloadCandidate(FunctionTemplateDecl * FunctionTemplateDecl)10850b57cec5SDimitry Andric     OverloadCandidate(FunctionTemplateDecl *FunctionTemplateDecl)
108604eeddc0SDimitry Andric         : Kind(CK_FunctionTemplate), FunctionTemplate(FunctionTemplateDecl) {
108704eeddc0SDimitry Andric       assert(FunctionTemplateDecl != nullptr);
108804eeddc0SDimitry Andric     }
10890b57cec5SDimitry Andric 
OverloadCandidate(const FunctionType * Type)10900b57cec5SDimitry Andric     OverloadCandidate(const FunctionType *Type)
109104eeddc0SDimitry Andric         : Kind(CK_FunctionType), Type(Type) {
109204eeddc0SDimitry Andric       assert(Type != nullptr);
109304eeddc0SDimitry Andric     }
109404eeddc0SDimitry Andric 
OverloadCandidate(FunctionProtoTypeLoc Prototype)1095fcaf7f86SDimitry Andric     OverloadCandidate(FunctionProtoTypeLoc Prototype)
1096fcaf7f86SDimitry Andric         : Kind(CK_FunctionProtoTypeLoc), ProtoTypeLoc(Prototype) {
1097fcaf7f86SDimitry Andric       assert(!Prototype.isNull());
1098fcaf7f86SDimitry Andric     }
1099fcaf7f86SDimitry Andric 
OverloadCandidate(const RecordDecl * Aggregate)110004eeddc0SDimitry Andric     OverloadCandidate(const RecordDecl *Aggregate)
110104eeddc0SDimitry Andric         : Kind(CK_Aggregate), AggregateType(Aggregate) {
110204eeddc0SDimitry Andric       assert(Aggregate != nullptr);
110304eeddc0SDimitry Andric     }
110404eeddc0SDimitry Andric 
OverloadCandidate(const TemplateDecl * Template)110504eeddc0SDimitry Andric     OverloadCandidate(const TemplateDecl *Template)
110604eeddc0SDimitry Andric         : Kind(CK_Template), Template(Template) {}
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric     /// Determine the kind of overload candidate.
getKind()11090b57cec5SDimitry Andric     CandidateKind getKind() const { return Kind; }
11100b57cec5SDimitry Andric 
11110b57cec5SDimitry Andric     /// Retrieve the function overload candidate or the templated
11120b57cec5SDimitry Andric     /// function declaration for a function template.
11130b57cec5SDimitry Andric     FunctionDecl *getFunction() const;
11140b57cec5SDimitry Andric 
11150b57cec5SDimitry Andric     /// Retrieve the function template overload candidate.
getFunctionTemplate()11160b57cec5SDimitry Andric     FunctionTemplateDecl *getFunctionTemplate() const {
11170b57cec5SDimitry Andric       assert(getKind() == CK_FunctionTemplate && "Not a function template");
11180b57cec5SDimitry Andric       return FunctionTemplate;
11190b57cec5SDimitry Andric     }
11200b57cec5SDimitry Andric 
11210b57cec5SDimitry Andric     /// Retrieve the function type of the entity, regardless of how the
11220b57cec5SDimitry Andric     /// function is stored.
11230b57cec5SDimitry Andric     const FunctionType *getFunctionType() const;
11240b57cec5SDimitry Andric 
1125fcaf7f86SDimitry Andric     /// Retrieve the function ProtoTypeLoc candidate.
1126fcaf7f86SDimitry Andric     /// This can be called for any Kind, but returns null for kinds
1127fcaf7f86SDimitry Andric     /// other than CK_FunctionProtoTypeLoc.
1128fcaf7f86SDimitry Andric     const FunctionProtoTypeLoc getFunctionProtoTypeLoc() const;
1129fcaf7f86SDimitry Andric 
getTemplate()113004eeddc0SDimitry Andric     const TemplateDecl *getTemplate() const {
113104eeddc0SDimitry Andric       assert(getKind() == CK_Template && "Not a template");
113204eeddc0SDimitry Andric       return Template;
113304eeddc0SDimitry Andric     }
113404eeddc0SDimitry Andric 
113504eeddc0SDimitry Andric     /// Retrieve the aggregate type being initialized.
getAggregate()113604eeddc0SDimitry Andric     const RecordDecl *getAggregate() const {
113704eeddc0SDimitry Andric       assert(getKind() == CK_Aggregate);
113804eeddc0SDimitry Andric       return AggregateType;
113904eeddc0SDimitry Andric     }
114004eeddc0SDimitry Andric 
114104eeddc0SDimitry Andric     /// Get the number of parameters in this signature.
114204eeddc0SDimitry Andric     unsigned getNumParams() const;
114304eeddc0SDimitry Andric 
114404eeddc0SDimitry Andric     /// Get the type of the Nth parameter.
114504eeddc0SDimitry Andric     /// Returns null if the type is unknown or N is out of range.
114604eeddc0SDimitry Andric     QualType getParamType(unsigned N) const;
114704eeddc0SDimitry Andric 
114804eeddc0SDimitry Andric     /// Get the declaration of the Nth parameter.
114904eeddc0SDimitry Andric     /// Returns null if the decl is unknown or N is out of range.
115004eeddc0SDimitry Andric     const NamedDecl *getParamDecl(unsigned N) const;
115104eeddc0SDimitry Andric 
11520b57cec5SDimitry Andric     /// Create a new code-completion string that describes the function
11530b57cec5SDimitry Andric     /// signature of this overload candidate.
115404eeddc0SDimitry Andric     CodeCompletionString *
115504eeddc0SDimitry Andric     CreateSignatureString(unsigned CurrentArg, Sema &S,
11560b57cec5SDimitry Andric                           CodeCompletionAllocator &Allocator,
11570b57cec5SDimitry Andric                           CodeCompletionTUInfo &CCTUInfo,
115804eeddc0SDimitry Andric                           bool IncludeBriefComments, bool Braced) const;
11590b57cec5SDimitry Andric   };
11600b57cec5SDimitry Andric 
CodeCompleteConsumer(const CodeCompleteOptions & CodeCompleteOpts)11610b57cec5SDimitry Andric   CodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts)
11620b57cec5SDimitry Andric       : CodeCompleteOpts(CodeCompleteOpts) {}
11630b57cec5SDimitry Andric 
11640b57cec5SDimitry Andric   /// Whether the code-completion consumer wants to see macros.
includeMacros()11650b57cec5SDimitry Andric   bool includeMacros() const {
11660b57cec5SDimitry Andric     return CodeCompleteOpts.IncludeMacros;
11670b57cec5SDimitry Andric   }
11680b57cec5SDimitry Andric 
11690b57cec5SDimitry Andric   /// Whether the code-completion consumer wants to see code patterns.
includeCodePatterns()11700b57cec5SDimitry Andric   bool includeCodePatterns() const {
11710b57cec5SDimitry Andric     return CodeCompleteOpts.IncludeCodePatterns;
11720b57cec5SDimitry Andric   }
11730b57cec5SDimitry Andric 
11740b57cec5SDimitry Andric   /// Whether to include global (top-level) declaration results.
includeGlobals()11750b57cec5SDimitry Andric   bool includeGlobals() const { return CodeCompleteOpts.IncludeGlobals; }
11760b57cec5SDimitry Andric 
11770b57cec5SDimitry Andric   /// Whether to include declarations in namespace contexts (including
11780b57cec5SDimitry Andric   /// the global namespace). If this is false, `includeGlobals()` will be
11790b57cec5SDimitry Andric   /// ignored.
includeNamespaceLevelDecls()11800b57cec5SDimitry Andric   bool includeNamespaceLevelDecls() const {
11810b57cec5SDimitry Andric     return CodeCompleteOpts.IncludeNamespaceLevelDecls;
11820b57cec5SDimitry Andric   }
11830b57cec5SDimitry Andric 
11840b57cec5SDimitry Andric   /// Whether to include brief documentation comments within the set of
11850b57cec5SDimitry Andric   /// code completions returned.
includeBriefComments()11860b57cec5SDimitry Andric   bool includeBriefComments() const {
11870b57cec5SDimitry Andric     return CodeCompleteOpts.IncludeBriefComments;
11880b57cec5SDimitry Andric   }
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric   /// Whether to include completion items with small fix-its, e.g. change
11910b57cec5SDimitry Andric   /// '.' to '->' on member access, etc.
includeFixIts()11920b57cec5SDimitry Andric   bool includeFixIts() const { return CodeCompleteOpts.IncludeFixIts; }
11930b57cec5SDimitry Andric 
11940b57cec5SDimitry Andric   /// Hint whether to load data from the external AST in order to provide
11950b57cec5SDimitry Andric   /// full results. If false, declarations from the preamble may be omitted.
loadExternal()11960b57cec5SDimitry Andric   bool loadExternal() const {
11970b57cec5SDimitry Andric     return CodeCompleteOpts.LoadExternal;
11980b57cec5SDimitry Andric   }
11990b57cec5SDimitry Andric 
12000b57cec5SDimitry Andric   /// Deregisters and destroys this code-completion consumer.
12010b57cec5SDimitry Andric   virtual ~CodeCompleteConsumer();
12020b57cec5SDimitry Andric 
12030b57cec5SDimitry Andric   /// \name Code-completion filtering
12040b57cec5SDimitry Andric   /// Check if the result should be filtered out.
isResultFilteredOut(StringRef Filter,CodeCompletionResult Results)12050b57cec5SDimitry Andric   virtual bool isResultFilteredOut(StringRef Filter,
12060b57cec5SDimitry Andric                                    CodeCompletionResult Results) {
12070b57cec5SDimitry Andric     return false;
12080b57cec5SDimitry Andric   }
12090b57cec5SDimitry Andric 
12100b57cec5SDimitry Andric   /// \name Code-completion callbacks
12110b57cec5SDimitry Andric   //@{
12120b57cec5SDimitry Andric   /// Process the finalized code-completion results.
ProcessCodeCompleteResults(Sema & S,CodeCompletionContext Context,CodeCompletionResult * Results,unsigned NumResults)12130b57cec5SDimitry Andric   virtual void ProcessCodeCompleteResults(Sema &S,
12140b57cec5SDimitry Andric                                           CodeCompletionContext Context,
12150b57cec5SDimitry Andric                                           CodeCompletionResult *Results,
12160b57cec5SDimitry Andric                                           unsigned NumResults) {}
12170b57cec5SDimitry Andric 
12180b57cec5SDimitry Andric   /// \param S the semantic-analyzer object for which code-completion is being
12190b57cec5SDimitry Andric   /// done.
12200b57cec5SDimitry Andric   ///
12210b57cec5SDimitry Andric   /// \param CurrentArg the index of the current argument.
12220b57cec5SDimitry Andric   ///
12230b57cec5SDimitry Andric   /// \param Candidates an array of overload candidates.
12240b57cec5SDimitry Andric   ///
12250b57cec5SDimitry Andric   /// \param NumCandidates the number of overload candidates
12260b57cec5SDimitry Andric   ///
12270b57cec5SDimitry Andric   /// \param OpenParLoc location of the opening parenthesis of the argument
12280b57cec5SDimitry Andric   ///        list.
ProcessOverloadCandidates(Sema & S,unsigned CurrentArg,OverloadCandidate * Candidates,unsigned NumCandidates,SourceLocation OpenParLoc,bool Braced)12290b57cec5SDimitry Andric   virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
12300b57cec5SDimitry Andric                                          OverloadCandidate *Candidates,
12310b57cec5SDimitry Andric                                          unsigned NumCandidates,
123204eeddc0SDimitry Andric                                          SourceLocation OpenParLoc,
123304eeddc0SDimitry Andric                                          bool Braced) {}
12340b57cec5SDimitry Andric   //@}
12350b57cec5SDimitry Andric 
12360b57cec5SDimitry Andric   /// Retrieve the allocator that will be used to allocate
12370b57cec5SDimitry Andric   /// code completion strings.
12380b57cec5SDimitry Andric   virtual CodeCompletionAllocator &getAllocator() = 0;
12390b57cec5SDimitry Andric 
12400b57cec5SDimitry Andric   virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() = 0;
12410b57cec5SDimitry Andric };
12420b57cec5SDimitry Andric 
12430b57cec5SDimitry Andric /// Get the documentation comment used to produce
12440b57cec5SDimitry Andric /// CodeCompletionString::BriefComment for RK_Declaration.
12450b57cec5SDimitry Andric const RawComment *getCompletionComment(const ASTContext &Ctx,
12460b57cec5SDimitry Andric                                        const NamedDecl *Decl);
12470b57cec5SDimitry Andric 
12480b57cec5SDimitry Andric /// Get the documentation comment used to produce
12490b57cec5SDimitry Andric /// CodeCompletionString::BriefComment for RK_Pattern.
12500b57cec5SDimitry Andric const RawComment *getPatternCompletionComment(const ASTContext &Ctx,
12510b57cec5SDimitry Andric                                               const NamedDecl *Decl);
12520b57cec5SDimitry Andric 
12530b57cec5SDimitry Andric /// Get the documentation comment used to produce
12540b57cec5SDimitry Andric /// CodeCompletionString::BriefComment for OverloadCandidate.
12550b57cec5SDimitry Andric const RawComment *
12560b57cec5SDimitry Andric getParameterComment(const ASTContext &Ctx,
12570b57cec5SDimitry Andric                     const CodeCompleteConsumer::OverloadCandidate &Result,
12580b57cec5SDimitry Andric                     unsigned ArgIndex);
12590b57cec5SDimitry Andric 
12600b57cec5SDimitry Andric /// A simple code-completion consumer that prints the results it
12610b57cec5SDimitry Andric /// receives in a simple format.
12620b57cec5SDimitry Andric class PrintingCodeCompleteConsumer : public CodeCompleteConsumer {
12630b57cec5SDimitry Andric   /// The raw output stream.
12640b57cec5SDimitry Andric   raw_ostream &OS;
12650b57cec5SDimitry Andric 
12660b57cec5SDimitry Andric   CodeCompletionTUInfo CCTUInfo;
12670b57cec5SDimitry Andric 
12680b57cec5SDimitry Andric public:
12690b57cec5SDimitry Andric   /// Create a new printing code-completion consumer that prints its
12700b57cec5SDimitry Andric   /// results to the given raw output stream.
PrintingCodeCompleteConsumer(const CodeCompleteOptions & CodeCompleteOpts,raw_ostream & OS)12710b57cec5SDimitry Andric   PrintingCodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
12720b57cec5SDimitry Andric                                raw_ostream &OS)
12730b57cec5SDimitry Andric       : CodeCompleteConsumer(CodeCompleteOpts), OS(OS),
12740b57cec5SDimitry Andric         CCTUInfo(std::make_shared<GlobalCodeCompletionAllocator>()) {}
12750b57cec5SDimitry Andric 
12760b57cec5SDimitry Andric   /// Prints the finalized code-completion results.
12770b57cec5SDimitry Andric   void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
12780b57cec5SDimitry Andric                                   CodeCompletionResult *Results,
12790b57cec5SDimitry Andric                                   unsigned NumResults) override;
12800b57cec5SDimitry Andric 
12810b57cec5SDimitry Andric   void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
12820b57cec5SDimitry Andric                                  OverloadCandidate *Candidates,
12830b57cec5SDimitry Andric                                  unsigned NumCandidates,
128404eeddc0SDimitry Andric                                  SourceLocation OpenParLoc,
128504eeddc0SDimitry Andric                                  bool Braced) override;
12860b57cec5SDimitry Andric 
12870b57cec5SDimitry Andric   bool isResultFilteredOut(StringRef Filter, CodeCompletionResult Results) override;
12880b57cec5SDimitry Andric 
getAllocator()12890b57cec5SDimitry Andric   CodeCompletionAllocator &getAllocator() override {
12900b57cec5SDimitry Andric     return CCTUInfo.getAllocator();
12910b57cec5SDimitry Andric   }
12920b57cec5SDimitry Andric 
getCodeCompletionTUInfo()12930b57cec5SDimitry Andric   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
12940b57cec5SDimitry Andric };
12950b57cec5SDimitry Andric 
12960b57cec5SDimitry Andric } // namespace clang
12970b57cec5SDimitry Andric 
12980b57cec5SDimitry Andric #endif // LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
1299