181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor//===---- CodeCompleteConsumer.h - Code Completion Interface ----*- C++ -*-===//
281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor//
381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor//                     The LLVM Compiler Infrastructure
481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor//
581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor// This file is distributed under the University of Illinois Open Source
681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor// License. See LICENSE.TXT for details.
781b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor//
881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor//===----------------------------------------------------------------------===//
981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor//
1081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor//  This file defines the CodeCompleteConsumer class.
1181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor//
1281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor//===----------------------------------------------------------------------===//
1381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor#ifndef LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
1481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor#define LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
1581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
1630a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang-c/Index.h"
171827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor#include "clang/AST/CanonicalType.h"
1830a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/AST/Type.h"
19d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko#include "clang/Sema/CodeCompleteOptions.h"
200e2c34f92f00628d48968dfea096d36381f494cbStephen Hines#include "llvm/ADT/DenseMap.h"
21e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor#include "llvm/ADT/SmallVector.h"
220c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor#include "llvm/ADT/StringRef.h"
23218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor#include "llvm/Support/Allocator.h"
24e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor#include <string>
2581b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
2681b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregornamespace clang {
270594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor
28e8d7bebc0dd037516dd695ee1ac2eb0923753b46Douglas Gregorclass Decl;
29ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
3012e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor/// \brief Default priority values for code-completion results based
3112e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor/// on their kind.
3212e131385e892e3723483a1081a89bcad29c8a84Douglas Gregorenum {
330c431c8c001e4c1f7488663d98700a8ce1e89e2aDouglas Gregor  /// \brief Priority for the next initialization in a constructor initializer
340c431c8c001e4c1f7488663d98700a8ce1e89e2aDouglas Gregor  /// list.
350c431c8c001e4c1f7488663d98700a8ce1e89e2aDouglas Gregor  CCP_NextInitializer = 7,
36ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// \brief Priority for an enumeration constant inside a switch whose
375c722c7020b33da57090422b854072258a50b3f0Douglas Gregor  /// condition is of the enumeration type.
385c722c7020b33da57090422b854072258a50b3f0Douglas Gregor  CCP_EnumInCase = 7,
3903d8aec611380d30ddb0659bb61be9289c3871b3Douglas Gregor  /// \brief Priority for a send-to-super completion.
408ec904c151abf7ed0dc911258574a904ed97ff15Douglas Gregor  CCP_SuperCompletion = 20,
4112e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// \brief Priority for a declaration that is in the local scope.
428ec904c151abf7ed0dc911258574a904ed97ff15Douglas Gregor  CCP_LocalDeclaration = 34,
4312e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// \brief Priority for a member declaration found from the current
4412e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// method or member function.
458ec904c151abf7ed0dc911258574a904ed97ff15Douglas Gregor  CCP_MemberDeclaration = 35,
4612e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// \brief Priority for a language keyword (that isn't any of the other
4712e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// categories).
488ec904c151abf7ed0dc911258574a904ed97ff15Douglas Gregor  CCP_Keyword = 40,
4912e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// \brief Priority for a code pattern.
508ec904c151abf7ed0dc911258574a904ed97ff15Douglas Gregor  CCP_CodePattern = 40,
5112e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// \brief Priority for a non-type declaration.
5212e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  CCP_Declaration = 50,
53d43dd83a973c585c39a9a5e5e904282bc2371986Douglas Gregor  /// \brief Priority for a type.
5408f43cd5d966fd31b32bd5c76c0645ebbf08b2f1Douglas Gregor  CCP_Type = CCP_Declaration,
55d754d5546ef1faa4216298fd95bc6a7f15808fd2Douglas Gregor  /// \brief Priority for a constant value (e.g., enumerator).
56d754d5546ef1faa4216298fd95bc6a7f15808fd2Douglas Gregor  CCP_Constant = 65,
5712e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// \brief Priority for a preprocessor macro.
5812e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  CCP_Macro = 70,
5912e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// \brief Priority for a nested-name-specifier.
6012e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  CCP_NestedNameSpecifier = 75,
6112e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// \brief Priority for a result that isn't likely to be what the user wants,
6212e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// but is included for completeness.
6397fe61ca1749110c28eb4570a710c8983711c7b3Douglas Gregor  CCP_Unlikely = 80,
64ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6597fe61ca1749110c28eb4570a710c8983711c7b3Douglas Gregor  /// \brief Priority for the Objective-C "_cmd" implicit parameter.
6697fe61ca1749110c28eb4570a710c8983711c7b3Douglas Gregor  CCP_ObjC_cmd = CCP_Unlikely
6712e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor};
6812e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor
693cdee121daa13403335094ce0e181b9911c2124cDouglas Gregor/// \brief Priority value deltas that are added to code-completion results
7012e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor/// based on the context of the result.
7112e131385e892e3723483a1081a89bcad29c8a84Douglas Gregorenum {
7212e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor  /// \brief The result is in a base class.
73eb0d0145910f3024d9bdefe279a1e9b95495f2fdDouglas Gregor  CCD_InBaseClass = 2,
743cdee121daa13403335094ce0e181b9911c2124cDouglas Gregor  /// \brief The result is a C++ non-static member function whose qualifiers
753cdee121daa13403335094ce0e181b9911c2124cDouglas Gregor  /// exactly match the object type on which the member function can be called.
76265f749edb28a80db517cc9051bd21092720d0f0Douglas Gregor  CCD_ObjectQualifierMatch = -1,
77265f749edb28a80db517cc9051bd21092720d0f0Douglas Gregor  /// \brief The selector of the given message exactly matches the selector
78265f749edb28a80db517cc9051bd21092720d0f0Douglas Gregor  /// of the current method, which might imply that some kind of delegation
79265f749edb28a80db517cc9051bd21092720d0f0Douglas Gregor  /// is occurring.
80b05496dbd63f03bf474dae2c4d1e2142608780ccDouglas Gregor  CCD_SelectorMatch = -3,
81ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
82b05496dbd63f03bf474dae2c4d1e2142608780ccDouglas Gregor  /// \brief Adjustment to the "bool" type in Objective-C, where the typedef
83b05496dbd63f03bf474dae2c4d1e2142608780ccDouglas Gregor  /// "BOOL" is preferred.
84577cdfdb20840350e841a483df630237326126d5Douglas Gregor  CCD_bool_in_ObjC = 1,
85ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
86577cdfdb20840350e841a483df630237326126d5Douglas Gregor  /// \brief Adjustment for KVC code pattern priorities when it doesn't look
87577cdfdb20840350e841a483df630237326126d5Douglas Gregor  /// like the
884b81cde48b8b56c219e8d665884f5f33139b5ecaDouglas Gregor  CCD_ProbablyNotObjCCollection = 15,
89ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
904b81cde48b8b56c219e8d665884f5f33139b5ecaDouglas Gregor  /// \brief An Objective-C method being used as a property.
914b81cde48b8b56c219e8d665884f5f33139b5ecaDouglas Gregor  CCD_MethodAsProperty = 2
9212e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor};
9312e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor
945ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregor/// \brief Priority value factors by which we will divide or multiply the
955ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregor/// priority of a code-completion result.
965ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregorenum {
975ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregor  /// \brief Divide by this factor when a code-completion result's type exactly
985ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregor  /// matches the type we expect.
995ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregor  CCF_ExactTypeMatch = 4,
1005ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregor  /// \brief Divide by this factor when a code-completion result's type is
1015ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregor  /// similar to the type we expect (e.g., both arithmetic types, both
1025ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregor  /// Objective-C object pointer types).
1035ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregor  CCF_SimilarTypeMatch = 2
1045ac3bdb2cb0113b640c54f01468d21985c08b252Douglas Gregor};
1051827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor
1061827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor/// \brief A simplified classification of types used when determining
1071827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor/// "similar" types for code completion.
1081827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregorenum SimplifiedTypeClass {
1091827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  STC_Arithmetic,
1101827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  STC_Array,
1111827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  STC_Block,
1121827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  STC_Function,
1131827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  STC_ObjectiveC,
1141827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  STC_Other,
1151827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  STC_Pointer,
1161827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  STC_Record,
1171827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  STC_Void
1181827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor};
119ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1201827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor/// \brief Determine the simplified type class of the given canonical type.
1211827e10051638770ad9ccf3e285caf95f995afd1Douglas GregorSimplifiedTypeClass getSimplifiedTypeClass(CanQualType T);
122ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1231827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor/// \brief Determine the type that this declaration will have if it is used
1241827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor/// as a type or in an expression.
12589cf425f1136f8d24a64ed94450e488b6794dfa4Dmitri GribenkoQualType getDeclUsageType(ASTContext &C, const NamedDecl *ND);
126ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1271827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor/// \brief Determine the priority to be given to a macro code completion result
1281827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor/// with the given name.
1291827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor///
1301827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor/// \param MacroName The name of the macro.
1311827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor///
132b05496dbd63f03bf474dae2c4d1e2142608780ccDouglas Gregor/// \param LangOpts Options describing the current language dialect.
133b05496dbd63f03bf474dae2c4d1e2142608780ccDouglas Gregor///
1341827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor/// \param PreferredTypeIsPointer Whether the preferred type for the context
1351827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor/// of this macro is a pointer type.
136ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikieunsigned getMacroUsagePriority(StringRef MacroName,
137b05496dbd63f03bf474dae2c4d1e2142608780ccDouglas Gregor                               const LangOptions &LangOpts,
1381827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor                               bool PreferredTypeIsPointer = false);
1390a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall
140e8d7bebc0dd037516dd695ee1ac2eb0923753b46Douglas Gregor/// \brief Determine the libclang cursor kind associated with the given
141e8d7bebc0dd037516dd695ee1ac2eb0923753b46Douglas Gregor/// declaration.
14206d8c60e37cd8cfdff1bf73ec8d58c2373cfd8f1Dmitri GribenkoCXCursorKind getCursorKindForDecl(const Decl *D);
143ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1440594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregorclass FunctionDecl;
1450594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregorclass FunctionType;
1460594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregorclass FunctionTemplateDecl;
1473f7c7f48654230d8e379214cfe49fcf2fde0a2c6Douglas Gregorclass IdentifierInfo;
14881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregorclass NamedDecl;
149b9d0ef76347574a147b8456b018fe94dc1e2be26Douglas Gregorclass NestedNameSpecifier;
15081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregorclass Sema;
151e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor
152e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor/// \brief The context in which code completion occurred, so that the
153e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor/// code-completion consumer can process the results accordingly.
154e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregorclass CodeCompletionContext {
155e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregorpublic:
156e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor  enum Kind {
15768e3c2eef52701a178fbcc2bb424f31dd4b36931Douglas Gregor    /// \brief An unspecified code-completion context.
158e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_Other,
1595c722c7020b33da57090422b854072258a50b3f0Douglas Gregor    /// \brief An unspecified code-completion context where we should also add
1605c722c7020b33da57090422b854072258a50b3f0Douglas Gregor    /// macro completions.
1615c722c7020b33da57090422b854072258a50b3f0Douglas Gregor    CCC_OtherWithMacros,
162e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred within a "top-level" completion context,
163e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// e.g., at namespace or global scope.
164e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_TopLevel,
165e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred within an Objective-C interface,
166e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// protocol, or category interface.
167e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_ObjCInterface,
168e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred within an Objective-C implementation
169e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// or category implementation.
170e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_ObjCImplementation,
171e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred within the instance variable list of
172e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// an Objective-C interface, implementation, or category implementation.
173e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_ObjCIvarList,
174e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred within a class, struct, or union.
175e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_ClassStructUnion,
176e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred where a statement (or declaration) is
177e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// expected in a function, method, or block.
178e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_Statement,
179e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred where an expression is expected.
180e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_Expression,
181e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred where an Objective-C message receiver
182e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// is expected.
183e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_ObjCMessageReceiver,
184e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred on the right-hand side of a member
1853da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    /// access expression using the dot operator.
186e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    ///
187ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// The results of this completion are the members of the type being
188ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// accessed. The type itself is available via
189e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \c CodeCompletionContext::getType().
1903da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    CCC_DotMemberAccess,
1913da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    /// \brief Code completion occurred on the right-hand side of a member
1923da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    /// access expression using the arrow operator.
1933da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    ///
194ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// The results of this completion are the members of the type being
195ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// accessed. The type itself is available via
1963da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    /// \c CodeCompletionContext::getType().
1973da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    CCC_ArrowMemberAccess,
1983da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    /// \brief Code completion occurred on the right-hand side of an Objective-C
1993da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    /// property access expression.
2003da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    ///
201ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// The results of this completion are the members of the type being
202ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// accessed. The type itself is available via
2033da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    /// \c CodeCompletionContext::getType().
2043da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    CCC_ObjCPropertyAccess,
205e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred after the "enum" keyword, to indicate
206e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// an enumeration name.
207e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_EnumTag,
208e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred after the "union" keyword, to indicate
209e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// a union name.
210e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_UnionTag,
211e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred after the "struct" or "class" keyword,
212e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// to indicate a struct or class name.
213e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    CCC_ClassOrStructTag,
214e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor    /// \brief Code completion occurred where a protocol name is expected.
2158071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor    CCC_ObjCProtocolName,
2168071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor    /// \brief Code completion occurred where a namespace or namespace alias
2178071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor    /// is expected.
2188071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor    CCC_Namespace,
2198071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor    /// \brief Code completion occurred where a type name is expected.
2202ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor    CCC_Type,
2212ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor    /// \brief Code completion occurred where a new name is expected.
2222ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor    CCC_Name,
2232ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor    /// \brief Code completion occurred where a new name is expected and a
2242ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor    /// qualified name is permissible.
2251fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor    CCC_PotentiallyQualifiedName,
2261fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor    /// \brief Code completion occurred where an macro is being defined.
2271fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor    CCC_MacroName,
2281fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor    /// \brief Code completion occurred where a macro name is expected
2291fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor    /// (without any arguments, in the case of a function-like macro).
230f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor    CCC_MacroNameUse,
231f29c5233085a5af795c3c01b94d319e5b3235d56Douglas Gregor    /// \brief Code completion occurred within a preprocessor expression.
232af1c6b532e6e1e944db987f29062e5efce71a573Douglas Gregor    CCC_PreprocessorExpression,
233ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// \brief Code completion occurred where a preprocessor directive is
234721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor    /// expected.
235721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor    CCC_PreprocessorDirective,
236af1c6b532e6e1e944db987f29062e5efce71a573Douglas Gregor    /// \brief Code completion occurred in a context where natural language is
237af1c6b532e6e1e944db987f29062e5efce71a573Douglas Gregor    /// expected, e.g., a comment or string literal.
238af1c6b532e6e1e944db987f29062e5efce71a573Douglas Gregor    ///
239af1c6b532e6e1e944db987f29062e5efce71a573Douglas Gregor    /// This context usually implies that no completions should be added,
240af1c6b532e6e1e944db987f29062e5efce71a573Douglas Gregor    /// unless they come from an appropriate natural-language dictionary.
241458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    CCC_NaturalLanguage,
242809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett    /// \brief Code completion for a selector, as in an \@selector expression.
2431a480c403a3b141ab89c9c59cf7b681102a1bfabDouglas Gregor    CCC_SelectorName,
2441a480c403a3b141ab89c9c59cf7b681102a1bfabDouglas Gregor    /// \brief Code completion within a type-qualifier list.
2450268810a46780144a2d5fb5a017c938d1199189cDouglas Gregor    CCC_TypeQualifiers,
2460268810a46780144a2d5fb5a017c938d1199189cDouglas Gregor    /// \brief Code completion in a parenthesized expression, which means that
2470268810a46780144a2d5fb5a017c938d1199189cDouglas Gregor    /// we may also have types here in C and Objective-C (as well as in C++).
24852779fb71795534d0447f6f4d4a6f6a7b09c4639Douglas Gregor    CCC_ParenthesizedExpression,
249d1f09b482b3874be07dfe9dd24bfad98915989c3Douglas Gregor    /// \brief Code completion where an Objective-C instance message is
250d1f09b482b3874be07dfe9dd24bfad98915989c3Douglas Gregor    /// expected.
2513da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    CCC_ObjCInstanceMessage,
252ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// \brief Code completion where an Objective-C class message is expected.
2533da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    CCC_ObjCClassMessage,
2540f91c8ccb398be2bd40dc402309bd79737542396Douglas Gregor    /// \brief Code completion where the name of an Objective-C class is
2553da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    /// expected.
2560f91c8ccb398be2bd40dc402309bd79737542396Douglas Gregor    CCC_ObjCInterfaceName,
2573da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    /// \brief Code completion where an Objective-C category name is expected.
2583da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    CCC_ObjCCategoryName,
259ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// \brief An unknown context, in which we are recovering from a parsing
26052779fb71795534d0447f6f4d4a6f6a7b09c4639Douglas Gregor    /// error and don't know which completions we should give.
26152779fb71795534d0447f6f4d4a6f6a7b09c4639Douglas Gregor    CCC_Recovery
262e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor  };
263e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor
264e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregorprivate:
265e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor  enum Kind Kind;
266e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor
2671827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  /// \brief The type that would prefer to see at this point (e.g., the type
2681827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  /// of an initializer or function parameter).
2691827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  QualType PreferredType;
270ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
271e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor  /// \brief The type of the base object in a member access expression.
2721827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  QualType BaseType;
273ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2740a47d69af8bda945352997af3da4687a3356096aDouglas Gregor  /// \brief The identifiers for Objective-C selector parts.
275050315bfd0473323a68f2da99b51bbe2842f6c36Dmitri Gribenko  ArrayRef<IdentifierInfo *> SelIdents;
276ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
277e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregorpublic:
278e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor  /// \brief Construct a new code-completion context of the given kind.
279050315bfd0473323a68f2da99b51bbe2842f6c36Dmitri Gribenko  CodeCompletionContext(enum Kind Kind) : Kind(Kind), SelIdents(None) { }
280ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
281e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor  /// \brief Construct a new code-completion context of the given kind.
2820a47d69af8bda945352997af3da4687a3356096aDouglas Gregor  CodeCompletionContext(enum Kind Kind, QualType T,
283050315bfd0473323a68f2da99b51bbe2842f6c36Dmitri Gribenko                        ArrayRef<IdentifierInfo *> SelIdents = None)
284050315bfd0473323a68f2da99b51bbe2842f6c36Dmitri Gribenko                        : Kind(Kind),
285050315bfd0473323a68f2da99b51bbe2842f6c36Dmitri Gribenko                          SelIdents(SelIdents) {
2863da626b4f38eb0350de960d71271ca77af7a9cc8Douglas Gregor    if (Kind == CCC_DotMemberAccess || Kind == CCC_ArrowMemberAccess ||
287e081a61bb0dc546fd623bf259435d17c9a4ea0d5Douglas Gregor        Kind == CCC_ObjCPropertyAccess || Kind == CCC_ObjCClassMessage ||
288e081a61bb0dc546fd623bf259435d17c9a4ea0d5Douglas Gregor        Kind == CCC_ObjCInstanceMessage)
2891827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor      BaseType = T;
2901827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor    else
2911827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor      PreferredType = T;
292e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor  }
293ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
294e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor  /// \brief Retrieve the kind of code-completion context.
295e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor  enum Kind getKind() const { return Kind; }
296ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2971827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  /// \brief Retrieve the type that this expression would prefer to have, e.g.,
2981827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  /// if the expression is a variable initializer or a function argument, the
2991827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  /// type of the corresponding variable or function parameter.
3001827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  QualType getPreferredType() const { return PreferredType; }
301ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
302ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// \brief Retrieve the type of the base object in a member-access
303e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor  /// expression.
3041827e10051638770ad9ccf3e285caf95f995afd1Douglas Gregor  QualType getBaseType() const { return BaseType; }
305ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
3060a47d69af8bda945352997af3da4687a3356096aDouglas Gregor  /// \brief Retrieve the Objective-C selector identifiers.
30768effa0d764cf6fc7b6f9729481ee2c6c5143f3bDmitri Gribenko  ArrayRef<IdentifierInfo *> getSelIdents() const { return SelIdents; }
3086f942b2cabf32b96f9901b889d8e44a34e0e7c62Douglas Gregor
3096f942b2cabf32b96f9901b889d8e44a34e0e7c62Douglas Gregor  /// \brief Determines whether we want C++ constructors as results within this
3106f942b2cabf32b96f9901b889d8e44a34e0e7c62Douglas Gregor  /// context.
3116f942b2cabf32b96f9901b889d8e44a34e0e7c62Douglas Gregor  bool wantConstructorResults() const;
312e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor};
313e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor
314e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor
315e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor/// \brief A "string" used to describe how code completion can
316e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor/// be performed for an entity.
317e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor///
318ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie/// A code completion string typically shows how a particular entity can be
319e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor/// used. For example, the code completion string for a function would show
320ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie/// the syntax to call it, including the parentheses, placeholders for the
321ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie/// arguments, etc.
322e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregorclass CodeCompletionString {
323e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregorpublic:
324e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor  /// \brief The different kinds of "chunks" that can occur within a code
325e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor  /// completion string.
326e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor  enum ChunkKind {
3270c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief The piece of text that the user is expected to type to
3280c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// match the code-completion string, typically a keyword or the name of a
3290c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// declarator or macro.
3300c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_TypedText,
331e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    /// \brief A piece of text that should be placed in the buffer, e.g.,
332e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    /// parentheses or a comma in a function call.
333e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    CK_Text,
334e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    /// \brief A code completion string that is entirely optional. For example,
335e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    /// an optional code completion string that describes the default arguments
336e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    /// in a function call.
337e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    CK_Optional,
338ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// \brief A string that acts as a placeholder for, e.g., a function
339e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    /// call argument.
3400563c2677cfc20c9450342ddc53aa96971671c71Douglas Gregor    CK_Placeholder,
3410563c2677cfc20c9450342ddc53aa96971671c71Douglas Gregor    /// \brief A piece of text that describes something about the result but
3420563c2677cfc20c9450342ddc53aa96971671c71Douglas Gregor    /// should not be inserted into the buffer.
3430c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_Informative,
344ff5ce6eefc7c253ef6edf4d4bfc996fdd82d09aaDouglas Gregor    /// \brief A piece of text that describes the type of an entity or, for
345ff5ce6eefc7c253ef6edf4d4bfc996fdd82d09aaDouglas Gregor    /// functions and methods, the return type.
346ff5ce6eefc7c253ef6edf4d4bfc996fdd82d09aaDouglas Gregor    CK_ResultType,
3470c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief A piece of text that describes the parameter that corresponds
3480c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// to the code-completion location within a function call, message send,
3490c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// macro invocation, etc.
3500c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_CurrentParameter,
3510c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief A left parenthesis ('(').
3520c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_LeftParen,
3530c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief A right parenthesis (')').
3540c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_RightParen,
3550c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief A left bracket ('[').
3560c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_LeftBracket,
3570c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief A right bracket (']').
3580c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_RightBracket,
3590c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief A left brace ('{').
3600c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_LeftBrace,
3610c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief A right brace ('}').
3620c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_RightBrace,
3630c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief A left angle bracket ('<').
3640c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_LeftAngle,
3650c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief A right angle bracket ('>').
3660c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    CK_RightAngle,
3670c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief A comma separator (',').
36801dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    CK_Comma,
36901dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    /// \brief A colon (':').
37001dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    CK_Colon,
37101dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    /// \brief A semicolon (';').
37201dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    CK_SemiColon,
37301dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    /// \brief An '=' sign.
37401dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    CK_Equal,
37501dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    /// \brief Horizontal whitespace (' ').
37601dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    CK_HorizontalSpace,
377809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett    /// \brief Vertical whitespace ('\\n' or '\\r\\n', depending on the
37801dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    /// platform).
37901dfea02d1da297e8b53db8eea3d3cc652acda8dDouglas Gregor    CK_VerticalSpace
380e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor  };
381ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
382e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor  /// \brief One piece of the code completion string.
383e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor  struct Chunk {
384ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// \brief The kind of data stored in this piece of the code completion
385e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    /// string.
386e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    ChunkKind Kind;
387ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
388e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    union {
3890563c2677cfc20c9450342ddc53aa96971671c71Douglas Gregor      /// \brief The text string associated with a CK_Text, CK_Placeholder,
3900c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor      /// CK_Informative, or CK_Comma chunk.
391ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie      /// The string is owned by the chunk and will be deallocated
392e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor      /// (with delete[]) when the chunk is destroyed.
393e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor      const char *Text;
394ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
395e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor      /// \brief The code completion string associated with a CK_Optional chunk.
396e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor      /// The optional code completion string is owned by the chunk, and will
397e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor      /// be deallocated (with delete) when the chunk is destroyed.
398e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor      CodeCompletionString *Optional;
399e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    };
400ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
4016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Chunk() : Kind(CK_Text), Text(nullptr) { }
402ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
4031eb18afd931f626713c066ede0707ceb522fa061Benjamin Kramer    explicit Chunk(ChunkKind Kind, const char *Text = "");
404ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
405e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    /// \brief Create a new text chunk.
406218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor    static Chunk CreateText(const char *Text);
407e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor
408e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    /// \brief Create a new optional chunk.
409218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor    static Chunk CreateOptional(CodeCompletionString *Optional);
410e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor
411e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor    /// \brief Create a new placeholder chunk.
412218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor    static Chunk CreatePlaceholder(const char *Placeholder);
4130563c2677cfc20c9450342ddc53aa96971671c71Douglas Gregor
4140563c2677cfc20c9450342ddc53aa96971671c71Douglas Gregor    /// \brief Create a new informative chunk.
415218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor    static Chunk CreateInformative(const char *Informative);
4160563c2677cfc20c9450342ddc53aa96971671c71Douglas Gregor
417ff5ce6eefc7c253ef6edf4d4bfc996fdd82d09aaDouglas Gregor    /// \brief Create a new result type chunk.
418218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor    static Chunk CreateResultType(const char *ResultType);
419ff5ce6eefc7c253ef6edf4d4bfc996fdd82d09aaDouglas Gregor
4200c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    /// \brief Create a new current-parameter chunk.
421218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor    static Chunk CreateCurrentParameter(const char *CurrentParameter);
422e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor  };
423ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
424e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregorprivate:
425218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief The number of chunks stored in this string.
4266164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen  unsigned NumChunks : 16;
427ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
4286164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen  /// \brief The number of annotations for this code-completion result.
4296164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen  unsigned NumAnnotations : 16;
4306164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen
431218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief The priority of this code-completion string.
432ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  unsigned Priority : 16;
433ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
434218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief The availability of this code-completion result.
435b51eee43676c2f4b7c4e1fa1648b438051b2b098Francois Pichet  unsigned Availability : 2;
436ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor
437ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  /// \brief The name of the parent context.
438ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  StringRef ParentName;
439d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko
440d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// \brief A brief documentation comment attached to the declaration of
441d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// entity being completed by this result.
442d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  const char *BriefComment;
443ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor
4440e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  CodeCompletionString(const CodeCompletionString &) = delete;
4450e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void operator=(const CodeCompletionString &) = delete;
446ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
447218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  CodeCompletionString(const Chunk *Chunks, unsigned NumChunks,
4486164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen                       unsigned Priority, CXAvailabilityKind Availability,
449ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor                       const char **Annotations, unsigned NumAnnotations,
450526d24444c91404dc4165b141e5ec095125c1bc8Argyrios Kyrtzidis                       StringRef ParentName,
451d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                       const char *BriefComment);
45233337ca4d89605025818daf83390ab4271d598d9Pirama Arumuga Nainar  ~CodeCompletionString() = default;
453ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
454218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  friend class CodeCompletionBuilder;
455218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  friend class CodeCompletionResult;
456ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
457218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregorpublic:
458218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  typedef const Chunk *iterator;
459218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  iterator begin() const { return reinterpret_cast<const Chunk *>(this + 1); }
460218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  iterator end() const { return begin() + NumChunks; }
461218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  bool empty() const { return NumChunks == 0; }
462218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  unsigned size() const { return NumChunks; }
463ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
464218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  const Chunk &operator[](unsigned I) const {
4650c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor    assert(I < size() && "Chunk index out-of-range");
466218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor    return begin()[I];
4670c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor  }
468ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
469218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Returns the text in the TypedText chunk.
470218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  const char *getTypedText() const;
4710c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor
472218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Retrieve the priority of this code completion result.
473218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  unsigned getPriority() const { return Priority; }
474ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
4756164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen  /// \brief Retrieve the availability of this code completion result.
476218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  unsigned getAvailability() const { return Availability; }
4776164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen
4786164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen  /// \brief Retrieve the number of annotations for this code completion result.
4796164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen  unsigned getAnnotationCount() const;
4806164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen
4816164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen  /// \brief Retrieve the annotation string specified by \c AnnotationNr.
4826164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen  const char *getAnnotation(unsigned AnnotationNr) const;
483ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor
484ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  /// \brief Retrieve the name of the parent context.
485ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  StringRef getParentContextName() const {
486ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor    return ParentName;
487ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  }
488d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko
489d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  const char *getBriefComment() const {
490d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko    return BriefComment;
491d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  }
492ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor
493218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Retrieve a string representation of the code completion string,
494218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// which is mainly useful for debugging.
495ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  std::string getAsString() const;
496218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor};
497218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor
498dae687575010c9c49a4b552f5eef82cd6279d9acDouglas Gregor/// \brief An allocator used specifically for the purpose of code completion.
499ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikieclass CodeCompletionAllocator : public llvm::BumpPtrAllocator {
500dae687575010c9c49a4b552f5eef82cd6279d9acDouglas Gregorpublic:
501dae687575010c9c49a4b552f5eef82cd6279d9acDouglas Gregor  /// \brief Copy the given string into this allocator.
5023ea9e33ea25e0c2b12db56418ba3f994eb662c04Pirama Arumuga Nainar  const char *CopyString(const Twine &String);
50328a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis};
50428a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
50528a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis/// \brief Allocator for a cached set of global code completions.
50628a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidisclass GlobalCodeCompletionAllocator
50728a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  : public CodeCompletionAllocator,
50828a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    public RefCountedBase<GlobalCodeCompletionAllocator>
50928a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis{
51028a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
51128a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis};
51228a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
51328a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidisclass CodeCompletionTUInfo {
51489cf425f1136f8d24a64ed94450e488b6794dfa4Dmitri Gribenko  llvm::DenseMap<const DeclContext *, StringRef> ParentNames;
51528a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> AllocatorRef;
51628a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
51728a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidispublic:
51828a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  explicit CodeCompletionTUInfo(
51928a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis                    IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> Allocator)
52028a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    : AllocatorRef(Allocator) { }
52128a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
52228a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> getAllocatorRef() const {
52328a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    return AllocatorRef;
52428a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  }
52528a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  CodeCompletionAllocator &getAllocator() const {
52628a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    assert(AllocatorRef);
52728a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    return *AllocatorRef;
528ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  }
52928a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
53089cf425f1136f8d24a64ed94450e488b6794dfa4Dmitri Gribenko  StringRef getParentName(const DeclContext *DC);
531dae687575010c9c49a4b552f5eef82cd6279d9acDouglas Gregor};
532abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramer
533abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramer} // end namespace clang
534abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramer
535abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramernamespace llvm {
536abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramer  template <> struct isPodLike<clang::CodeCompletionString::Chunk> {
537abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramer    static const bool value = true;
538abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramer  };
539abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramer}
540abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramer
541abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramernamespace clang {
542abd2a3e8bc251a693d4d09819fd76f2e482c21ffBenjamin Kramer
543218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor/// \brief A builder class used to construct new code-completion strings.
544218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregorclass CodeCompletionBuilder {
545ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikiepublic:
546218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  typedef CodeCompletionString::Chunk Chunk;
547ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
548218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregorprivate:
549dae687575010c9c49a4b552f5eef82cd6279d9acDouglas Gregor  CodeCompletionAllocator &Allocator;
55028a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  CodeCompletionTUInfo &CCTUInfo;
551218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  unsigned Priority;
552218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  CXAvailabilityKind Availability;
553ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  StringRef ParentName;
554d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  const char *BriefComment;
555ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor
556218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief The chunks stored in this string.
557686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<Chunk, 4> Chunks;
5586164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen
5596164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen  SmallVector<const char *, 2> Annotations;
560ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
561218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregorpublic:
56228a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
56328a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis                        CodeCompletionTUInfo &CCTUInfo)
56428a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    : Allocator(Allocator), CCTUInfo(CCTUInfo),
56528a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis      Priority(0), Availability(CXAvailability_Available),
5666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      BriefComment(nullptr) { }
567ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
568dae687575010c9c49a4b552f5eef82cd6279d9acDouglas Gregor  CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
56928a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis                        CodeCompletionTUInfo &CCTUInfo,
570ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                        unsigned Priority, CXAvailabilityKind Availability)
57128a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    : Allocator(Allocator), CCTUInfo(CCTUInfo),
57228a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis      Priority(Priority), Availability(Availability),
5736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      BriefComment(nullptr) { }
574218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor
575218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Retrieve the allocator into which the code completion
576577cdfdb20840350e841a483df630237326126d5Douglas Gregor  /// strings should be allocated.
577dae687575010c9c49a4b552f5eef82cd6279d9acDouglas Gregor  CodeCompletionAllocator &getAllocator() const { return Allocator; }
578ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
57928a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
58028a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
581ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// \brief Take the resulting completion string.
582218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  ///
583218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// This operation can only be performed once.
584218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  CodeCompletionString *TakeString();
585ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
5860c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor  /// \brief Add a new typed-text chunk.
5871eb18afd931f626713c066ede0707ceb522fa061Benjamin Kramer  void AddTypedTextChunk(const char *Text);
588ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
589e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor  /// \brief Add a new text chunk.
5901eb18afd931f626713c066ede0707ceb522fa061Benjamin Kramer  void AddTextChunk(const char *Text);
591577cdfdb20840350e841a483df630237326126d5Douglas Gregor
592e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor  /// \brief Add a new optional chunk.
5931eb18afd931f626713c066ede0707ceb522fa061Benjamin Kramer  void AddOptionalChunk(CodeCompletionString *Optional);
594ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
595e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor  /// \brief Add a new placeholder chunk.
5961eb18afd931f626713c066ede0707ceb522fa061Benjamin Kramer  void AddPlaceholderChunk(const char *Placeholder);
597ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
5980563c2677cfc20c9450342ddc53aa96971671c71Douglas Gregor  /// \brief Add a new informative chunk.
5991eb18afd931f626713c066ede0707ceb522fa061Benjamin Kramer  void AddInformativeChunk(const char *Text);
600ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
601ff5ce6eefc7c253ef6edf4d4bfc996fdd82d09aaDouglas Gregor  /// \brief Add a new result-type chunk.
6021eb18afd931f626713c066ede0707ceb522fa061Benjamin Kramer  void AddResultTypeChunk(const char *ResultType);
603ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6040c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor  /// \brief Add a new current-parameter chunk.
6051eb18afd931f626713c066ede0707ceb522fa061Benjamin Kramer  void AddCurrentParameterChunk(const char *CurrentParameter);
606ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6070c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor  /// \brief Add a new chunk.
6081eb18afd931f626713c066ede0707ceb522fa061Benjamin Kramer  void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text = "");
6096164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen
6106164ea1d75385b6fc3c19e5ab9bb686298436a5aErik Verbruggen  void AddAnnotation(const char *A) { Annotations.push_back(A); }
611ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor
612ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  /// \brief Add the parent context information to this code completion.
61389cf425f1136f8d24a64ed94450e488b6794dfa4Dmitri Gribenko  void addParentContext(const DeclContext *DC);
614d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko
615c02ddb23c0a27ff95859b3eacab134613b0b1d1dFariborz Jahanian  const char *getBriefComment() const { return BriefComment; }
616d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  void addBriefComment(StringRef Comment);
617ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor
618ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  StringRef getParentName() const { return ParentName; }
619e6e0361984b077d205e9a45d64257d41d2c788c8Douglas Gregor};
620ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6210a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall/// \brief Captures a result of code completion.
6220a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCallclass CodeCompletionResult {
62381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregorpublic:
6240a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Describes the kind of result generated.
6250a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  enum ResultKind {
62649fdccb4595862828aa5cadc1497d466a8031ddaDmitri Gribenko    RK_Declaration = 0, ///< Refers to a declaration
62749fdccb4595862828aa5cadc1497d466a8031ddaDmitri Gribenko    RK_Keyword,         ///< Refers to a keyword or symbol.
62849fdccb4595862828aa5cadc1497d466a8031ddaDmitri Gribenko    RK_Macro,           ///< Refers to a macro
62949fdccb4595862828aa5cadc1497d466a8031ddaDmitri Gribenko    RK_Pattern          ///< Refers to a precomputed pattern.
6300a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  };
631ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
632ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  /// \brief When Kind == RK_Declaration or RK_Pattern, the declaration we are
633ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  /// referring to. In the latter case, the declaration might be NULL.
63489cf425f1136f8d24a64ed94450e488b6794dfa4Dmitri Gribenko  const NamedDecl *Declaration;
635ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
636ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  union {
637ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// \brief When Kind == RK_Keyword, the string representing the keyword
6380a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall    /// or symbol's spelling.
6390a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall    const char *Keyword;
640ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6410a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall    /// \brief When Kind == RK_Pattern, the code-completion string that
6420a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall    /// describes the completion text to insert.
6430a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall    CodeCompletionString *Pattern;
644ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6450a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall    /// \brief When Kind == RK_Macro, the identifier that refers to a macro.
646b3958476b21702a15f64f09d2862506d03dffe7bDmitri Gribenko    const IdentifierInfo *Macro;
6470a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  };
64812e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor
6490a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief The priority of this particular code-completion result.
6500a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  unsigned Priority;
65112e131385e892e3723483a1081a89bcad29c8a84Douglas Gregor
6520a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Specifies which parameter (of a function, Objective-C method,
6530a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// macro, etc.) we should start with when formatting the result.
6540a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  unsigned StartParameter;
655ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
656e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer  /// \brief The kind of result stored here.
65711ed43dabf272fe80cc3820bb1a043ca7c1d53acBenjamin Kramer  ResultKind Kind;
658e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer
659e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer  /// \brief The cursor kind that describes this result.
66011ed43dabf272fe80cc3820bb1a043ca7c1d53acBenjamin Kramer  CXCursorKind CursorKind;
661e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer
662e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer  /// \brief The availability of this result.
66311ed43dabf272fe80cc3820bb1a043ca7c1d53acBenjamin Kramer  CXAvailabilityKind Availability;
664e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer
6650a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Whether this result is hidden by another name.
6660a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  bool Hidden : 1;
667ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6680a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Whether this result was found via lookup into a base class.
6690a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  bool QualifierIsInformative : 1;
670ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
671ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// \brief Whether this declaration is the beginning of a
6720a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// nested-name-specifier and, therefore, should be followed by '::'.
6730a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  bool StartsNestedNameSpecifier : 1;
6744ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor
6750a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Whether all parameters (of a function, Objective-C
6760a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// method, etc.) should be considered "informative".
6770a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  bool AllParametersAreInformative : 1;
6784ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor
6790a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Whether we're completing a declaration of the given entity,
6800a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// rather than a use of that entity.
6810a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  bool DeclaringEntity : 1;
682ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6830a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief If the result should have a nested-name-specifier, this is it.
684ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// When \c QualifierIsInformative, the nested-name-specifier is
6850a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// informative rather than required.
6860a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  NestedNameSpecifier *Qualifier;
687ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6880a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Build a result that refers to a declaration.
68989cf425f1136f8d24a64ed94450e488b6794dfa4Dmitri Gribenko  CodeCompletionResult(const NamedDecl *Declaration,
690d1f09b482b3874be07dfe9dd24bfad98915989c3Douglas Gregor                       unsigned Priority,
6916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                       NestedNameSpecifier *Qualifier = nullptr,
692d12059673dcef32bc2b6bae5321654d33863afe6Erik Verbruggen                       bool QualifierIsInformative = false,
693d12059673dcef32bc2b6bae5321654d33863afe6Erik Verbruggen                       bool Accessible = true)
694d1f09b482b3874be07dfe9dd24bfad98915989c3Douglas Gregor    : Declaration(Declaration), Priority(Priority),
695e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      StartParameter(0), Kind(RK_Declaration),
696e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      Availability(CXAvailability_Available), Hidden(false),
697e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      QualifierIsInformative(QualifierIsInformative),
6980a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall      StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
699ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie      DeclaringEntity(false), Qualifier(Qualifier) {
700d12059673dcef32bc2b6bae5321654d33863afe6Erik Verbruggen    computeCursorKindAndAvailability(Accessible);
7010a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  }
702ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
7030a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Build a result that refers to a keyword or symbol.
7040a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  CodeCompletionResult(const char *Keyword, unsigned Priority = CCP_Keyword)
7056bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    : Declaration(nullptr), Keyword(Keyword), Priority(Priority),
7066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      StartParameter(0), Kind(RK_Keyword), CursorKind(CXCursor_NotImplemented),
707e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      Availability(CXAvailability_Available), Hidden(false),
708e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      QualifierIsInformative(0), StartsNestedNameSpecifier(false),
7096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      AllParametersAreInformative(false), DeclaringEntity(false),
7106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Qualifier(nullptr) {}
711ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
7120a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Build a result that refers to a macro.
713b3958476b21702a15f64f09d2862506d03dffe7bDmitri Gribenko  CodeCompletionResult(const IdentifierInfo *Macro,
714b3958476b21702a15f64f09d2862506d03dffe7bDmitri Gribenko                       unsigned Priority = CCP_Macro)
7156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    : Declaration(nullptr), Macro(Macro), Priority(Priority), StartParameter(0),
716e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      Kind(RK_Macro), CursorKind(CXCursor_MacroDefinition),
717e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      Availability(CXAvailability_Available), Hidden(false),
718e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      QualifierIsInformative(0), StartsNestedNameSpecifier(false),
7196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      AllParametersAreInformative(false), DeclaringEntity(false),
7206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Qualifier(nullptr) {}
72154f016150acf7e0d4dab702d3d7d5e40ba1fdebfDouglas Gregor
7220a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Build a result that refers to a pattern.
7230a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  CodeCompletionResult(CodeCompletionString *Pattern,
7240a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall                       unsigned Priority = CCP_CodePattern,
7250a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall                       CXCursorKind CursorKind = CXCursor_NotImplemented,
726ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor                   CXAvailabilityKind Availability = CXAvailability_Available,
7276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                       const NamedDecl *D = nullptr)
728e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer    : Declaration(D), Pattern(Pattern), Priority(Priority), StartParameter(0),
729e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      Kind(RK_Pattern), CursorKind(CursorKind), Availability(Availability),
730ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie      Hidden(false), QualifierIsInformative(0),
731ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie      StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
7326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      DeclaringEntity(false), Qualifier(nullptr)
733ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  {
7340a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  }
735ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
736ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  /// \brief Build a result that refers to a pattern with an associated
737ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  /// declaration.
738ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  CodeCompletionResult(CodeCompletionString *Pattern, NamedDecl *D,
739ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor                       unsigned Priority)
740e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer    : Declaration(D), Pattern(Pattern), Priority(Priority), StartParameter(0),
741e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      Kind(RK_Pattern), Availability(CXAvailability_Available), Hidden(false),
742e5f8372b5dccf5894f0263d018599c14d8f31273Benjamin Kramer      QualifierIsInformative(false), StartsNestedNameSpecifier(false),
7436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      AllParametersAreInformative(false), DeclaringEntity(false),
7446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Qualifier(nullptr) {
745ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor    computeCursorKindAndAvailability();
746ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor  }
747ba1030698dbc276db86b11c5329a1edee8a1805eDouglas Gregor
7480a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Retrieve the declaration stored in this result.
74989cf425f1136f8d24a64ed94450e488b6794dfa4Dmitri Gribenko  const NamedDecl *getDeclaration() const {
7500a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall    assert(Kind == RK_Declaration && "Not a declaration result");
7510a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall    return Declaration;
7520a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  }
753ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
7540a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Retrieve the keyword stored in this result.
7550a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  const char *getKeyword() const {
7560a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall    assert(Kind == RK_Keyword && "Not a keyword result");
7570a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall    return Keyword;
7580a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  }
759ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
7600a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Create a new code-completion string that describes how to insert
7610a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// this result into a program.
7620a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  ///
7630a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \param S The semantic analysis that created the result.
7640a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  ///
765218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \param Allocator The allocator that will be used to allocate the
766218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// string itself.
7670a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  CodeCompletionString *CreateCodeCompletionString(Sema &S,
76828a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis                                           CodeCompletionAllocator &Allocator,
769d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                                           CodeCompletionTUInfo &CCTUInfo,
770d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                                           bool IncludeBriefComments);
771ea8c59aaa6bd19976879142296f8fd12f8926738Argyrios Kyrtzidis  CodeCompletionString *CreateCodeCompletionString(ASTContext &Ctx,
772ea8c59aaa6bd19976879142296f8fd12f8926738Argyrios Kyrtzidis                                                   Preprocessor &PP,
77328a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis                                           CodeCompletionAllocator &Allocator,
774d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                                           CodeCompletionTUInfo &CCTUInfo,
775d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                                           bool IncludeBriefComments);
776ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
7770a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCallprivate:
778d12059673dcef32bc2b6bae5321654d33863afe6Erik Verbruggen  void computeCursorKindAndAvailability(bool Accessible = true);
7790a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall};
780ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
781721f359a350059a81945baa08f63b2e5feceb044Douglas Gregorbool operator<(const CodeCompletionResult &X, const CodeCompletionResult &Y);
782ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
783ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikieinline bool operator>(const CodeCompletionResult &X,
784721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor                      const CodeCompletionResult &Y) {
785721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor  return Y < X;
786721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor}
787ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
788ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikieinline bool operator<=(const CodeCompletionResult &X,
789721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor                      const CodeCompletionResult &Y) {
790721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor  return !(Y < X);
791721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor}
792721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor
793ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikieinline bool operator>=(const CodeCompletionResult &X,
794721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor                       const CodeCompletionResult &Y) {
795721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor  return !(X < Y);
796721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor}
797721f359a350059a81945baa08f63b2e5feceb044Douglas Gregor
798ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
799ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikieraw_ostream &operator<<(raw_ostream &OS,
8000a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall                              const CodeCompletionString &CCS);
8010a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall
802ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie/// \brief Abstract interface for a consumer of code-completion
8030a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall/// information.
8040a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCallclass CodeCompleteConsumer {
8050a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCallprotected:
806d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  const CodeCompleteOptions CodeCompleteOpts;
807ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8080a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// \brief Whether the output format for the code-completion consumer is
8090a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  /// binary.
8100a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall  bool OutputIsBinary;
811ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8120a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCallpublic:
8130594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  class OverloadCandidate {
8140594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  public:
8150594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    /// \brief Describes the type of overload candidate.
8160594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    enum CandidateKind {
8170594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      /// \brief The candidate is a function declaration.
8180594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      CK_Function,
8190594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      /// \brief The candidate is a function template.
8200594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      CK_FunctionTemplate,
8210594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      /// \brief The "candidate" is actually a variable, expression, or block
8220594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      /// for which we only have a function prototype.
8230594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      CK_FunctionType
8240594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    };
825ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8260594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  private:
8270594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    /// \brief The kind of overload candidate.
8280594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    CandidateKind Kind;
829ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8300594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    union {
831ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie      /// \brief The function overload candidate, available when
8320594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      /// Kind == CK_Function.
8330594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      FunctionDecl *Function;
834ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8350594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      /// \brief The function template overload candidate, available when
8360594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      /// Kind == CK_FunctionTemplate.
8370594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      FunctionTemplateDecl *FunctionTemplate;
838ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8390594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      /// \brief The function type that describes the entity being called,
8400594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      /// when Kind == CK_FunctionType.
8410594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      const FunctionType *Type;
8420594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    };
843ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8440594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  public:
8450594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    OverloadCandidate(FunctionDecl *Function)
8460594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      : Kind(CK_Function), Function(Function) { }
8470594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor
8480594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    OverloadCandidate(FunctionTemplateDecl *FunctionTemplateDecl)
849c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin      : Kind(CK_FunctionTemplate), FunctionTemplate(FunctionTemplateDecl) { }
8500594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor
8510594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    OverloadCandidate(const FunctionType *Type)
8520594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      : Kind(CK_FunctionType), Type(Type) { }
8530594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor
8540594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    /// \brief Determine the kind of overload candidate.
8550594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    CandidateKind getKind() const { return Kind; }
856ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
857ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    /// \brief Retrieve the function overload candidate or the templated
8580594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    /// function declaration for a function template.
8590594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    FunctionDecl *getFunction() const;
860ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8610594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    /// \brief Retrieve the function template overload candidate.
8620594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    FunctionTemplateDecl *getFunctionTemplate() const {
8630594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      assert(getKind() == CK_FunctionTemplate && "Not a function template");
8640594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor      return FunctionTemplate;
8650594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    }
866ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8670594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    /// \brief Retrieve the function type of the entity, regardless of how the
8680594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    /// function is stored.
8690594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor    const FunctionType *getFunctionType() const;
870ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
87186d802e03a267af79663990c39865c67e0645238Douglas Gregor    /// \brief Create a new code-completion string that describes the function
87286d802e03a267af79663990c39865c67e0645238Douglas Gregor    /// signature of this overload candidate.
873ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    CodeCompletionString *CreateSignatureString(unsigned CurrentArg,
87432be4a588fbb87d0d163ead49c42f5438bf0b2b7Douglas Gregor                                                Sema &S,
87528a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis                                      CodeCompletionAllocator &Allocator,
8760e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                      CodeCompletionTUInfo &CCTUInfo,
8770e2c34f92f00628d48968dfea096d36381f494cbStephen Hines                                      bool IncludeBriefComments) const;
8780594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  };
879ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
880d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  CodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
881d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko                       bool OutputIsBinary)
882d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko    : CodeCompleteOpts(CodeCompleteOpts), OutputIsBinary(OutputIsBinary)
883d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  { }
884ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8850c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor  /// \brief Whether the code-completion consumer wants to see macros.
886d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  bool includeMacros() const {
887d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko    return CodeCompleteOpts.IncludeMacros;
888d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  }
8892b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor
890d8e8a58ee35ab334ab9d0c2154dca029c1822e8aDouglas Gregor  /// \brief Whether the code-completion consumer wants to see code patterns.
891d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  bool includeCodePatterns() const {
892d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko    return CodeCompleteOpts.IncludeCodePatterns;
893d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  }
894ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8958071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor  /// \brief Whether to include global (top-level) declaration results.
896d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  bool includeGlobals() const {
897d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko    return CodeCompleteOpts.IncludeGlobals;
898d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  }
899d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko
900d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// \brief Whether to include brief documentation comments within the set of
901d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  /// code completions returned.
902d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  bool includeBriefComments() const {
903d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko    return CodeCompleteOpts.IncludeBriefComments;
904d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  }
905ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
9062b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor  /// \brief Determine whether the output of this consumer is binary.
9072b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor  bool isOutputBinary() const { return OutputIsBinary; }
908ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
90981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// \brief Deregisters and destroys this code-completion consumer.
91081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  virtual ~CodeCompleteConsumer();
9112b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor
91281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// \name Code-completion callbacks
91381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  //@{
91481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// \brief Process the finalized code-completion results.
915ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  virtual void ProcessCodeCompleteResults(Sema &S,
916e6b1bb6e7fe906d164637ca33503b8fafdbc99e5Douglas Gregor                                          CodeCompletionContext Context,
9170a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall                                          CodeCompletionResult *Results,
91881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor                                          unsigned NumResults) { }
9193a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar
9203a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar  /// \param S the semantic-analyzer object for which code-completion is being
9213a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar  /// done.
9220594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  ///
9230594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  /// \param CurrentArg the index of the current argument.
9240594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  ///
9250594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  /// \param Candidates an array of overload candidates.
9260594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  ///
9270594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor  /// \param NumCandidates the number of overload candidates
9283a2838d14251427089c39caec90c8abbc27f7a14Daniel Dunbar  virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
9290594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor                                         OverloadCandidate *Candidates,
9300594438e06f58ab3798416324780ab39ca9c8f54Douglas Gregor                                         unsigned NumCandidates) { }
931b1c28a1f7ef3bd3616df6ddcdb503f4b480a8df6Douglas Gregor  //@}
932ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
933218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// \brief Retrieve the allocator that will be used to allocate
934218937c13ef5b0625a70aad41ca7a92da9278bd2Douglas Gregor  /// code completion strings.
935dae687575010c9c49a4b552f5eef82cd6279d9acDouglas Gregor  virtual CodeCompletionAllocator &getAllocator() = 0;
93628a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
93728a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() = 0;
93881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor};
9390a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall
940ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie/// \brief A simple code-completion consumer that prints the results it
94181b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor/// receives in a simple format.
94281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregorclass PrintingCodeCompleteConsumer : public CodeCompleteConsumer {
94381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// \brief The raw output stream.
9448cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner  raw_ostream &OS;
945ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
94628a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  CodeCompletionTUInfo CCTUInfo;
947ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
94881b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregorpublic:
94981b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// \brief Create a new printing code-completion consumer that prints its
95081b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor  /// results to the given raw output stream.
951d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko  PrintingCodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
9528cc488fefb2fb04bc8d5398da29f0182f97934cfChris Lattner                               raw_ostream &OS)
953d99ef536b241071b6f4c01db6525dc03242ac30bDmitri Gribenko    : CodeCompleteConsumer(CodeCompleteOpts, false), OS(OS),
95428a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis      CCTUInfo(new GlobalCodeCompletionAllocator) {}
955ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
9560c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor  /// \brief Prints the finalized code-completion results.
957651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context,
958651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                  CodeCompletionResult *Results,
959651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                  unsigned NumResults) override;
960ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
961651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
962651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                 OverloadCandidate *Candidates,
963651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                 unsigned NumCandidates) override;
964ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
965651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CodeCompletionAllocator &getAllocator() override {
96628a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis    return CCTUInfo.getAllocator();
96728a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis  }
96828a83f57003469fb615ad27dd34bcf5b0a10da8cArgyrios Kyrtzidis
969651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; }
9700c8296dfb495f41d6f0de6fe1d03014ffd063674Douglas Gregor};
971ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
97281b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor} // end namespace clang
97381b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor
97481b747b7fcc91c2fba9a3183d8fac80adbfc1d3eDouglas Gregor#endif // LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
975