1198092Srdivacky//===---- CodeCompleteConsumer.h - Code Completion Interface ----*- C++ -*-===//
2198092Srdivacky//
3198092Srdivacky//                     The LLVM Compiler Infrastructure
4198092Srdivacky//
5198092Srdivacky// This file is distributed under the University of Illinois Open Source
6198092Srdivacky// License. See LICENSE.TXT for details.
7198092Srdivacky//
8198092Srdivacky//===----------------------------------------------------------------------===//
9198092Srdivacky//
10198092Srdivacky//  This file defines the CodeCompleteConsumer class.
11198092Srdivacky//
12198092Srdivacky//===----------------------------------------------------------------------===//
13198092Srdivacky#ifndef LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
14198092Srdivacky#define LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
15198092Srdivacky
16252723Sdim#include "clang-c/Index.h"
17252723Sdim#include "clang/AST/CanonicalType.h"
18212904Sdim#include "clang/AST/Type.h"
19245431Sdim#include "clang/Sema/CodeCompleteOptions.h"
20198092Srdivacky#include "llvm/ADT/SmallVector.h"
21199482Srdivacky#include "llvm/ADT/StringRef.h"
22218893Sdim#include "llvm/Support/Allocator.h"
23198092Srdivacky#include <string>
24198092Srdivacky
25198092Srdivackynamespace clang {
26198092Srdivacky
27212904Sdimclass Decl;
28235633Sdim
29208600Srdivacky/// \brief Default priority values for code-completion results based
30208600Srdivacky/// on their kind.
31208600Srdivackyenum {
32212904Sdim  /// \brief Priority for the next initialization in a constructor initializer
33212904Sdim  /// list.
34212904Sdim  CCP_NextInitializer = 7,
35235633Sdim  /// \brief Priority for an enumeration constant inside a switch whose
36218893Sdim  /// condition is of the enumeration type.
37218893Sdim  CCP_EnumInCase = 7,
38212904Sdim  /// \brief Priority for a send-to-super completion.
39218893Sdim  CCP_SuperCompletion = 20,
40208600Srdivacky  /// \brief Priority for a declaration that is in the local scope.
41218893Sdim  CCP_LocalDeclaration = 34,
42208600Srdivacky  /// \brief Priority for a member declaration found from the current
43208600Srdivacky  /// method or member function.
44218893Sdim  CCP_MemberDeclaration = 35,
45208600Srdivacky  /// \brief Priority for a language keyword (that isn't any of the other
46208600Srdivacky  /// categories).
47218893Sdim  CCP_Keyword = 40,
48208600Srdivacky  /// \brief Priority for a code pattern.
49218893Sdim  CCP_CodePattern = 40,
50208600Srdivacky  /// \brief Priority for a non-type declaration.
51208600Srdivacky  CCP_Declaration = 50,
52218893Sdim  /// \brief Priority for a type.
53218893Sdim  CCP_Type = CCP_Declaration,
54208600Srdivacky  /// \brief Priority for a constant value (e.g., enumerator).
55218893Sdim  CCP_Constant = 65,
56208600Srdivacky  /// \brief Priority for a preprocessor macro.
57208600Srdivacky  CCP_Macro = 70,
58208600Srdivacky  /// \brief Priority for a nested-name-specifier.
59208600Srdivacky  CCP_NestedNameSpecifier = 75,
60208600Srdivacky  /// \brief Priority for a result that isn't likely to be what the user wants,
61208600Srdivacky  /// but is included for completeness.
62218893Sdim  CCP_Unlikely = 80,
63235633Sdim
64218893Sdim  /// \brief Priority for the Objective-C "_cmd" implicit parameter.
65218893Sdim  CCP_ObjC_cmd = CCP_Unlikely
66208600Srdivacky};
67208600Srdivacky
68212904Sdim/// \brief Priority value deltas that are added to code-completion results
69208600Srdivacky/// based on the context of the result.
70208600Srdivackyenum {
71208600Srdivacky  /// \brief The result is in a base class.
72212904Sdim  CCD_InBaseClass = 2,
73212904Sdim  /// \brief The result is a C++ non-static member function whose qualifiers
74212904Sdim  /// exactly match the object type on which the member function can be called.
75212904Sdim  CCD_ObjectQualifierMatch = -1,
76212904Sdim  /// \brief The selector of the given message exactly matches the selector
77212904Sdim  /// of the current method, which might imply that some kind of delegation
78212904Sdim  /// is occurring.
79218893Sdim  CCD_SelectorMatch = -3,
80235633Sdim
81218893Sdim  /// \brief Adjustment to the "bool" type in Objective-C, where the typedef
82218893Sdim  /// "BOOL" is preferred.
83218893Sdim  CCD_bool_in_ObjC = 1,
84235633Sdim
85218893Sdim  /// \brief Adjustment for KVC code pattern priorities when it doesn't look
86218893Sdim  /// like the
87223017Sdim  CCD_ProbablyNotObjCCollection = 15,
88235633Sdim
89223017Sdim  /// \brief An Objective-C method being used as a property.
90223017Sdim  CCD_MethodAsProperty = 2
91208600Srdivacky};
92208600Srdivacky
93210299Sed/// \brief Priority value factors by which we will divide or multiply the
94210299Sed/// priority of a code-completion result.
95210299Sedenum {
96210299Sed  /// \brief Divide by this factor when a code-completion result's type exactly
97210299Sed  /// matches the type we expect.
98210299Sed  CCF_ExactTypeMatch = 4,
99210299Sed  /// \brief Divide by this factor when a code-completion result's type is
100210299Sed  /// similar to the type we expect (e.g., both arithmetic types, both
101210299Sed  /// Objective-C object pointer types).
102210299Sed  CCF_SimilarTypeMatch = 2
103210299Sed};
104212904Sdim
105212904Sdim/// \brief A simplified classification of types used when determining
106212904Sdim/// "similar" types for code completion.
107212904Sdimenum SimplifiedTypeClass {
108212904Sdim  STC_Arithmetic,
109212904Sdim  STC_Array,
110212904Sdim  STC_Block,
111212904Sdim  STC_Function,
112212904Sdim  STC_ObjectiveC,
113212904Sdim  STC_Other,
114212904Sdim  STC_Pointer,
115212904Sdim  STC_Record,
116212904Sdim  STC_Void
117212904Sdim};
118235633Sdim
119212904Sdim/// \brief Determine the simplified type class of the given canonical type.
120212904SdimSimplifiedTypeClass getSimplifiedTypeClass(CanQualType T);
121235633Sdim
122212904Sdim/// \brief Determine the type that this declaration will have if it is used
123212904Sdim/// as a type or in an expression.
124252723SdimQualType getDeclUsageType(ASTContext &C, const NamedDecl *ND);
125235633Sdim
126212904Sdim/// \brief Determine the priority to be given to a macro code completion result
127212904Sdim/// with the given name.
128212904Sdim///
129212904Sdim/// \param MacroName The name of the macro.
130212904Sdim///
131218893Sdim/// \param LangOpts Options describing the current language dialect.
132218893Sdim///
133212904Sdim/// \param PreferredTypeIsPointer Whether the preferred type for the context
134212904Sdim/// of this macro is a pointer type.
135235633Sdimunsigned getMacroUsagePriority(StringRef MacroName,
136218893Sdim                               const LangOptions &LangOpts,
137212904Sdim                               bool PreferredTypeIsPointer = false);
138212904Sdim
139212904Sdim/// \brief Determine the libclang cursor kind associated with the given
140212904Sdim/// declaration.
141252723SdimCXCursorKind getCursorKindForDecl(const Decl *D);
142235633Sdim
143198092Srdivackyclass FunctionDecl;
144198092Srdivackyclass FunctionType;
145198092Srdivackyclass FunctionTemplateDecl;
146198893Srdivackyclass IdentifierInfo;
147198092Srdivackyclass NamedDecl;
148198092Srdivackyclass NestedNameSpecifier;
149198092Srdivackyclass Sema;
150198092Srdivacky
151212904Sdim/// \brief The context in which code completion occurred, so that the
152212904Sdim/// code-completion consumer can process the results accordingly.
153212904Sdimclass CodeCompletionContext {
154212904Sdimpublic:
155212904Sdim  enum Kind {
156212904Sdim    /// \brief An unspecified code-completion context.
157212904Sdim    CCC_Other,
158218893Sdim    /// \brief An unspecified code-completion context where we should also add
159218893Sdim    /// macro completions.
160218893Sdim    CCC_OtherWithMacros,
161212904Sdim    /// \brief Code completion occurred within a "top-level" completion context,
162212904Sdim    /// e.g., at namespace or global scope.
163212904Sdim    CCC_TopLevel,
164212904Sdim    /// \brief Code completion occurred within an Objective-C interface,
165212904Sdim    /// protocol, or category interface.
166212904Sdim    CCC_ObjCInterface,
167212904Sdim    /// \brief Code completion occurred within an Objective-C implementation
168212904Sdim    /// or category implementation.
169212904Sdim    CCC_ObjCImplementation,
170212904Sdim    /// \brief Code completion occurred within the instance variable list of
171212904Sdim    /// an Objective-C interface, implementation, or category implementation.
172212904Sdim    CCC_ObjCIvarList,
173212904Sdim    /// \brief Code completion occurred within a class, struct, or union.
174212904Sdim    CCC_ClassStructUnion,
175212904Sdim    /// \brief Code completion occurred where a statement (or declaration) is
176212904Sdim    /// expected in a function, method, or block.
177212904Sdim    CCC_Statement,
178212904Sdim    /// \brief Code completion occurred where an expression is expected.
179212904Sdim    CCC_Expression,
180212904Sdim    /// \brief Code completion occurred where an Objective-C message receiver
181212904Sdim    /// is expected.
182212904Sdim    CCC_ObjCMessageReceiver,
183212904Sdim    /// \brief Code completion occurred on the right-hand side of a member
184224145Sdim    /// access expression using the dot operator.
185212904Sdim    ///
186235633Sdim    /// The results of this completion are the members of the type being
187235633Sdim    /// accessed. The type itself is available via
188212904Sdim    /// \c CodeCompletionContext::getType().
189224145Sdim    CCC_DotMemberAccess,
190224145Sdim    /// \brief Code completion occurred on the right-hand side of a member
191224145Sdim    /// access expression using the arrow operator.
192224145Sdim    ///
193235633Sdim    /// The results of this completion are the members of the type being
194235633Sdim    /// accessed. The type itself is available via
195224145Sdim    /// \c CodeCompletionContext::getType().
196224145Sdim    CCC_ArrowMemberAccess,
197224145Sdim    /// \brief Code completion occurred on the right-hand side of an Objective-C
198224145Sdim    /// property access expression.
199224145Sdim    ///
200235633Sdim    /// The results of this completion are the members of the type being
201235633Sdim    /// accessed. The type itself is available via
202224145Sdim    /// \c CodeCompletionContext::getType().
203224145Sdim    CCC_ObjCPropertyAccess,
204212904Sdim    /// \brief Code completion occurred after the "enum" keyword, to indicate
205212904Sdim    /// an enumeration name.
206212904Sdim    CCC_EnumTag,
207212904Sdim    /// \brief Code completion occurred after the "union" keyword, to indicate
208212904Sdim    /// a union name.
209212904Sdim    CCC_UnionTag,
210212904Sdim    /// \brief Code completion occurred after the "struct" or "class" keyword,
211212904Sdim    /// to indicate a struct or class name.
212212904Sdim    CCC_ClassOrStructTag,
213212904Sdim    /// \brief Code completion occurred where a protocol name is expected.
214212904Sdim    CCC_ObjCProtocolName,
215212904Sdim    /// \brief Code completion occurred where a namespace or namespace alias
216212904Sdim    /// is expected.
217212904Sdim    CCC_Namespace,
218212904Sdim    /// \brief Code completion occurred where a type name is expected.
219212904Sdim    CCC_Type,
220212904Sdim    /// \brief Code completion occurred where a new name is expected.
221212904Sdim    CCC_Name,
222212904Sdim    /// \brief Code completion occurred where a new name is expected and a
223212904Sdim    /// qualified name is permissible.
224212904Sdim    CCC_PotentiallyQualifiedName,
225212904Sdim    /// \brief Code completion occurred where an macro is being defined.
226212904Sdim    CCC_MacroName,
227212904Sdim    /// \brief Code completion occurred where a macro name is expected
228212904Sdim    /// (without any arguments, in the case of a function-like macro).
229212904Sdim    CCC_MacroNameUse,
230212904Sdim    /// \brief Code completion occurred within a preprocessor expression.
231212904Sdim    CCC_PreprocessorExpression,
232235633Sdim    /// \brief Code completion occurred where a preprocessor directive is
233212904Sdim    /// expected.
234212904Sdim    CCC_PreprocessorDirective,
235212904Sdim    /// \brief Code completion occurred in a context where natural language is
236212904Sdim    /// expected, e.g., a comment or string literal.
237212904Sdim    ///
238212904Sdim    /// This context usually implies that no completions should be added,
239212904Sdim    /// unless they come from an appropriate natural-language dictionary.
240212904Sdim    CCC_NaturalLanguage,
241245431Sdim    /// \brief Code completion for a selector, as in an \@selector expression.
242212904Sdim    CCC_SelectorName,
243212904Sdim    /// \brief Code completion within a type-qualifier list.
244218893Sdim    CCC_TypeQualifiers,
245218893Sdim    /// \brief Code completion in a parenthesized expression, which means that
246218893Sdim    /// we may also have types here in C and Objective-C (as well as in C++).
247218893Sdim    CCC_ParenthesizedExpression,
248252723Sdim    /// \brief Code completion where an Objective-C instance message is
249252723Sdim    /// expected.
250224145Sdim    CCC_ObjCInstanceMessage,
251235633Sdim    /// \brief Code completion where an Objective-C class message is expected.
252224145Sdim    CCC_ObjCClassMessage,
253226890Sdim    /// \brief Code completion where the name of an Objective-C class is
254224145Sdim    /// expected.
255226890Sdim    CCC_ObjCInterfaceName,
256224145Sdim    /// \brief Code completion where an Objective-C category name is expected.
257224145Sdim    CCC_ObjCCategoryName,
258235633Sdim    /// \brief An unknown context, in which we are recovering from a parsing
259218893Sdim    /// error and don't know which completions we should give.
260218893Sdim    CCC_Recovery
261212904Sdim  };
262212904Sdim
263212904Sdimprivate:
264212904Sdim  enum Kind Kind;
265212904Sdim
266212904Sdim  /// \brief The type that would prefer to see at this point (e.g., the type
267212904Sdim  /// of an initializer or function parameter).
268212904Sdim  QualType PreferredType;
269235633Sdim
270212904Sdim  /// \brief The type of the base object in a member access expression.
271212904Sdim  QualType BaseType;
272235633Sdim
273226890Sdim  /// \brief The identifiers for Objective-C selector parts.
274263509Sdim  ArrayRef<IdentifierInfo *> SelIdents;
275235633Sdim
276212904Sdimpublic:
277212904Sdim  /// \brief Construct a new code-completion context of the given kind.
278263509Sdim  CodeCompletionContext(enum Kind Kind) : Kind(Kind), SelIdents(None) { }
279235633Sdim
280212904Sdim  /// \brief Construct a new code-completion context of the given kind.
281226890Sdim  CodeCompletionContext(enum Kind Kind, QualType T,
282263509Sdim                        ArrayRef<IdentifierInfo *> SelIdents = None)
283263509Sdim                        : Kind(Kind),
284263509Sdim                          SelIdents(SelIdents) {
285224145Sdim    if (Kind == CCC_DotMemberAccess || Kind == CCC_ArrowMemberAccess ||
286226890Sdim        Kind == CCC_ObjCPropertyAccess || Kind == CCC_ObjCClassMessage ||
287226890Sdim        Kind == CCC_ObjCInstanceMessage)
288212904Sdim      BaseType = T;
289212904Sdim    else
290212904Sdim      PreferredType = T;
291212904Sdim  }
292235633Sdim
293212904Sdim  /// \brief Retrieve the kind of code-completion context.
294212904Sdim  enum Kind getKind() const { return Kind; }
295235633Sdim
296212904Sdim  /// \brief Retrieve the type that this expression would prefer to have, e.g.,
297212904Sdim  /// if the expression is a variable initializer or a function argument, the
298212904Sdim  /// type of the corresponding variable or function parameter.
299212904Sdim  QualType getPreferredType() const { return PreferredType; }
300235633Sdim
301235633Sdim  /// \brief Retrieve the type of the base object in a member-access
302212904Sdim  /// expression.
303212904Sdim  QualType getBaseType() const { return BaseType; }
304235633Sdim
305226890Sdim  /// \brief Retrieve the Objective-C selector identifiers.
306263509Sdim  ArrayRef<IdentifierInfo *> getSelIdents() const { return SelIdents; }
307235633Sdim
308218893Sdim  /// \brief Determines whether we want C++ constructors as results within this
309218893Sdim  /// context.
310218893Sdim  bool wantConstructorResults() const;
311212904Sdim};
312212904Sdim
313212904Sdim
314198092Srdivacky/// \brief A "string" used to describe how code completion can
315198092Srdivacky/// be performed for an entity.
316198092Srdivacky///
317235633Sdim/// A code completion string typically shows how a particular entity can be
318198092Srdivacky/// used. For example, the code completion string for a function would show
319235633Sdim/// the syntax to call it, including the parentheses, placeholders for the
320235633Sdim/// arguments, etc.
321198092Srdivackyclass CodeCompletionString {
322198092Srdivackypublic:
323198092Srdivacky  /// \brief The different kinds of "chunks" that can occur within a code
324198092Srdivacky  /// completion string.
325198092Srdivacky  enum ChunkKind {
326199482Srdivacky    /// \brief The piece of text that the user is expected to type to
327199482Srdivacky    /// match the code-completion string, typically a keyword or the name of a
328199482Srdivacky    /// declarator or macro.
329199482Srdivacky    CK_TypedText,
330198092Srdivacky    /// \brief A piece of text that should be placed in the buffer, e.g.,
331198092Srdivacky    /// parentheses or a comma in a function call.
332198092Srdivacky    CK_Text,
333198092Srdivacky    /// \brief A code completion string that is entirely optional. For example,
334198092Srdivacky    /// an optional code completion string that describes the default arguments
335198092Srdivacky    /// in a function call.
336198092Srdivacky    CK_Optional,
337235633Sdim    /// \brief A string that acts as a placeholder for, e.g., a function
338198092Srdivacky    /// call argument.
339198092Srdivacky    CK_Placeholder,
340198092Srdivacky    /// \brief A piece of text that describes something about the result but
341198092Srdivacky    /// should not be inserted into the buffer.
342199482Srdivacky    CK_Informative,
343201361Srdivacky    /// \brief A piece of text that describes the type of an entity or, for
344201361Srdivacky    /// functions and methods, the return type.
345201361Srdivacky    CK_ResultType,
346199482Srdivacky    /// \brief A piece of text that describes the parameter that corresponds
347199482Srdivacky    /// to the code-completion location within a function call, message send,
348199482Srdivacky    /// macro invocation, etc.
349199482Srdivacky    CK_CurrentParameter,
350199482Srdivacky    /// \brief A left parenthesis ('(').
351199482Srdivacky    CK_LeftParen,
352199482Srdivacky    /// \brief A right parenthesis (')').
353199482Srdivacky    CK_RightParen,
354199482Srdivacky    /// \brief A left bracket ('[').
355199482Srdivacky    CK_LeftBracket,
356199482Srdivacky    /// \brief A right bracket (']').
357199482Srdivacky    CK_RightBracket,
358199482Srdivacky    /// \brief A left brace ('{').
359199482Srdivacky    CK_LeftBrace,
360199482Srdivacky    /// \brief A right brace ('}').
361199482Srdivacky    CK_RightBrace,
362199482Srdivacky    /// \brief A left angle bracket ('<').
363199482Srdivacky    CK_LeftAngle,
364199482Srdivacky    /// \brief A right angle bracket ('>').
365199482Srdivacky    CK_RightAngle,
366199482Srdivacky    /// \brief A comma separator (',').
367202379Srdivacky    CK_Comma,
368202379Srdivacky    /// \brief A colon (':').
369202379Srdivacky    CK_Colon,
370202379Srdivacky    /// \brief A semicolon (';').
371202379Srdivacky    CK_SemiColon,
372202379Srdivacky    /// \brief An '=' sign.
373202379Srdivacky    CK_Equal,
374202379Srdivacky    /// \brief Horizontal whitespace (' ').
375202379Srdivacky    CK_HorizontalSpace,
376245431Sdim    /// \brief Vertical whitespace ('\\n' or '\\r\\n', depending on the
377202379Srdivacky    /// platform).
378202379Srdivacky    CK_VerticalSpace
379198092Srdivacky  };
380235633Sdim
381198092Srdivacky  /// \brief One piece of the code completion string.
382198092Srdivacky  struct Chunk {
383235633Sdim    /// \brief The kind of data stored in this piece of the code completion
384198092Srdivacky    /// string.
385198092Srdivacky    ChunkKind Kind;
386235633Sdim
387198092Srdivacky    union {
388198092Srdivacky      /// \brief The text string associated with a CK_Text, CK_Placeholder,
389199482Srdivacky      /// CK_Informative, or CK_Comma chunk.
390235633Sdim      /// The string is owned by the chunk and will be deallocated
391198092Srdivacky      /// (with delete[]) when the chunk is destroyed.
392198092Srdivacky      const char *Text;
393235633Sdim
394198092Srdivacky      /// \brief The code completion string associated with a CK_Optional chunk.
395198092Srdivacky      /// The optional code completion string is owned by the chunk, and will
396198092Srdivacky      /// be deallocated (with delete) when the chunk is destroyed.
397198092Srdivacky      CodeCompletionString *Optional;
398198092Srdivacky    };
399235633Sdim
400198092Srdivacky    Chunk() : Kind(CK_Text), Text(0) { }
401235633Sdim
402235633Sdim    explicit Chunk(ChunkKind Kind, const char *Text = "");
403235633Sdim
404198092Srdivacky    /// \brief Create a new text chunk.
405218893Sdim    static Chunk CreateText(const char *Text);
406198092Srdivacky
407198092Srdivacky    /// \brief Create a new optional chunk.
408218893Sdim    static Chunk CreateOptional(CodeCompletionString *Optional);
409198092Srdivacky
410198092Srdivacky    /// \brief Create a new placeholder chunk.
411218893Sdim    static Chunk CreatePlaceholder(const char *Placeholder);
412198092Srdivacky
413198092Srdivacky    /// \brief Create a new informative chunk.
414218893Sdim    static Chunk CreateInformative(const char *Informative);
415198092Srdivacky
416201361Srdivacky    /// \brief Create a new result type chunk.
417218893Sdim    static Chunk CreateResultType(const char *ResultType);
418201361Srdivacky
419199482Srdivacky    /// \brief Create a new current-parameter chunk.
420218893Sdim    static Chunk CreateCurrentParameter(const char *CurrentParameter);
421198092Srdivacky  };
422235633Sdim
423198092Srdivackyprivate:
424218893Sdim  /// \brief The number of chunks stored in this string.
425226890Sdim  unsigned NumChunks : 16;
426235633Sdim
427226890Sdim  /// \brief The number of annotations for this code-completion result.
428226890Sdim  unsigned NumAnnotations : 16;
429226890Sdim
430218893Sdim  /// \brief The priority of this code-completion string.
431235633Sdim  unsigned Priority : 16;
432235633Sdim
433218893Sdim  /// \brief The availability of this code-completion result.
434218893Sdim  unsigned Availability : 2;
435235633Sdim
436235633Sdim  /// \brief The name of the parent context.
437235633Sdim  StringRef ParentName;
438245431Sdim
439245431Sdim  /// \brief A brief documentation comment attached to the declaration of
440245431Sdim  /// entity being completed by this result.
441245431Sdim  const char *BriefComment;
442235633Sdim
443245431Sdim  CodeCompletionString(const CodeCompletionString &) LLVM_DELETED_FUNCTION;
444245431Sdim  void operator=(const CodeCompletionString &) LLVM_DELETED_FUNCTION;
445235633Sdim
446218893Sdim  CodeCompletionString(const Chunk *Chunks, unsigned NumChunks,
447226890Sdim                       unsigned Priority, CXAvailabilityKind Availability,
448235633Sdim                       const char **Annotations, unsigned NumAnnotations,
449245431Sdim                       StringRef ParentName,
450245431Sdim                       const char *BriefComment);
451218893Sdim  ~CodeCompletionString() { }
452235633Sdim
453218893Sdim  friend class CodeCompletionBuilder;
454218893Sdim  friend class CodeCompletionResult;
455235633Sdim
456198092Srdivackypublic:
457218893Sdim  typedef const Chunk *iterator;
458218893Sdim  iterator begin() const { return reinterpret_cast<const Chunk *>(this + 1); }
459218893Sdim  iterator end() const { return begin() + NumChunks; }
460218893Sdim  bool empty() const { return NumChunks == 0; }
461218893Sdim  unsigned size() const { return NumChunks; }
462235633Sdim
463218893Sdim  const Chunk &operator[](unsigned I) const {
464199482Srdivacky    assert(I < size() && "Chunk index out-of-range");
465218893Sdim    return begin()[I];
466199482Srdivacky  }
467235633Sdim
468218893Sdim  /// \brief Returns the text in the TypedText chunk.
469218893Sdim  const char *getTypedText() const;
470199482Srdivacky
471218893Sdim  /// \brief Retrieve the priority of this code completion result.
472218893Sdim  unsigned getPriority() const { return Priority; }
473235633Sdim
474226890Sdim  /// \brief Retrieve the availability of this code completion result.
475218893Sdim  unsigned getAvailability() const { return Availability; }
476226890Sdim
477226890Sdim  /// \brief Retrieve the number of annotations for this code completion result.
478226890Sdim  unsigned getAnnotationCount() const;
479226890Sdim
480226890Sdim  /// \brief Retrieve the annotation string specified by \c AnnotationNr.
481226890Sdim  const char *getAnnotation(unsigned AnnotationNr) const;
482218893Sdim
483235633Sdim  /// \brief Retrieve the name of the parent context.
484235633Sdim  StringRef getParentContextName() const {
485235633Sdim    return ParentName;
486235633Sdim  }
487245431Sdim
488245431Sdim  const char *getBriefComment() const {
489245431Sdim    return BriefComment;
490245431Sdim  }
491235633Sdim
492218893Sdim  /// \brief Retrieve a string representation of the code completion string,
493218893Sdim  /// which is mainly useful for debugging.
494235633Sdim  std::string getAsString() const;
495218893Sdim};
496218893Sdim
497218893Sdim/// \brief An allocator used specifically for the purpose of code completion.
498235633Sdimclass CodeCompletionAllocator : public llvm::BumpPtrAllocator {
499218893Sdimpublic:
500218893Sdim  /// \brief Copy the given string into this allocator.
501226890Sdim  const char *CopyString(StringRef String);
502218893Sdim
503218893Sdim  /// \brief Copy the given string into this allocator.
504226890Sdim  const char *CopyString(Twine String);
505235633Sdim
506218893Sdim  // \brief Copy the given string into this allocator.
507218893Sdim  const char *CopyString(const char *String) {
508226890Sdim    return CopyString(StringRef(String));
509199482Srdivacky  }
510235633Sdim
511218893Sdim  /// \brief Copy the given string into this allocator.
512218893Sdim  const char *CopyString(const std::string &String) {
513226890Sdim    return CopyString(StringRef(String));
514218893Sdim  }
515218893Sdim};
516235633Sdim
517235633Sdim/// \brief Allocator for a cached set of global code completions.
518235633Sdimclass GlobalCodeCompletionAllocator
519235633Sdim  : public CodeCompletionAllocator,
520235633Sdim    public RefCountedBase<GlobalCodeCompletionAllocator>
521235633Sdim{
522235633Sdim
523235633Sdim};
524235633Sdim
525235633Sdimclass CodeCompletionTUInfo {
526252723Sdim  llvm::DenseMap<const DeclContext *, StringRef> ParentNames;
527235633Sdim  IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> AllocatorRef;
528235633Sdim
529235633Sdimpublic:
530235633Sdim  explicit CodeCompletionTUInfo(
531235633Sdim                    IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> Allocator)
532235633Sdim    : AllocatorRef(Allocator) { }
533235633Sdim
534235633Sdim  IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> getAllocatorRef() const {
535235633Sdim    return AllocatorRef;
536235633Sdim  }
537235633Sdim  CodeCompletionAllocator &getAllocator() const {
538235633Sdim    assert(AllocatorRef);
539235633Sdim    return *AllocatorRef;
540235633Sdim  }
541235633Sdim
542252723Sdim  StringRef getParentName(const DeclContext *DC);
543235633Sdim};
544235633Sdim
545235633Sdim} // end namespace clang
546235633Sdim
547235633Sdimnamespace llvm {
548235633Sdim  template <> struct isPodLike<clang::CodeCompletionString::Chunk> {
549235633Sdim    static const bool value = true;
550235633Sdim  };
551235633Sdim}
552235633Sdim
553235633Sdimnamespace clang {
554235633Sdim
555218893Sdim/// \brief A builder class used to construct new code-completion strings.
556218893Sdimclass CodeCompletionBuilder {
557235633Sdimpublic:
558218893Sdim  typedef CodeCompletionString::Chunk Chunk;
559235633Sdim
560218893Sdimprivate:
561218893Sdim  CodeCompletionAllocator &Allocator;
562235633Sdim  CodeCompletionTUInfo &CCTUInfo;
563218893Sdim  unsigned Priority;
564218893Sdim  CXAvailabilityKind Availability;
565235633Sdim  StringRef ParentName;
566245431Sdim  const char *BriefComment;
567218893Sdim
568218893Sdim  /// \brief The chunks stored in this string.
569226890Sdim  SmallVector<Chunk, 4> Chunks;
570226890Sdim
571226890Sdim  SmallVector<const char *, 2> Annotations;
572235633Sdim
573218893Sdimpublic:
574218893Sdim  CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
575235633Sdim                        CodeCompletionTUInfo &CCTUInfo)
576235633Sdim    : Allocator(Allocator), CCTUInfo(CCTUInfo),
577235633Sdim      Priority(0), Availability(CXAvailability_Available),
578245431Sdim      BriefComment(NULL) { }
579218893Sdim
580235633Sdim  CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
581235633Sdim                        CodeCompletionTUInfo &CCTUInfo,
582235633Sdim                        unsigned Priority, CXAvailabilityKind Availability)
583235633Sdim    : Allocator(Allocator), CCTUInfo(CCTUInfo),
584235633Sdim      Priority(Priority), Availability(Availability),
585245431Sdim      BriefComment(NULL) { }
586235633Sdim
587218893Sdim  /// \brief Retrieve the allocator into which the code completion
588218893Sdim  /// strings should be allocated.
589218893Sdim  CodeCompletionAllocator &getAllocator() const { return Allocator; }
590235633Sdim
591235633Sdim  CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
592235633Sdim
593235633Sdim  /// \brief Take the resulting completion string.
594218893Sdim  ///
595218893Sdim  /// This operation can only be performed once.
596218893Sdim  CodeCompletionString *TakeString();
597235633Sdim
598199482Srdivacky  /// \brief Add a new typed-text chunk.
599235633Sdim  void AddTypedTextChunk(const char *Text);
600235633Sdim
601198092Srdivacky  /// \brief Add a new text chunk.
602235633Sdim  void AddTextChunk(const char *Text);
603218893Sdim
604198092Srdivacky  /// \brief Add a new optional chunk.
605235633Sdim  void AddOptionalChunk(CodeCompletionString *Optional);
606235633Sdim
607198092Srdivacky  /// \brief Add a new placeholder chunk.
608235633Sdim  void AddPlaceholderChunk(const char *Placeholder);
609235633Sdim
610198092Srdivacky  /// \brief Add a new informative chunk.
611235633Sdim  void AddInformativeChunk(const char *Text);
612235633Sdim
613201361Srdivacky  /// \brief Add a new result-type chunk.
614235633Sdim  void AddResultTypeChunk(const char *ResultType);
615235633Sdim
616199482Srdivacky  /// \brief Add a new current-parameter chunk.
617235633Sdim  void AddCurrentParameterChunk(const char *CurrentParameter);
618235633Sdim
619199482Srdivacky  /// \brief Add a new chunk.
620235633Sdim  void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text = "");
621226890Sdim
622226890Sdim  void AddAnnotation(const char *A) { Annotations.push_back(A); }
623235633Sdim
624235633Sdim  /// \brief Add the parent context information to this code completion.
625252723Sdim  void addParentContext(const DeclContext *DC);
626245431Sdim
627252723Sdim  const char *getBriefComment() const { return BriefComment; }
628245431Sdim  void addBriefComment(StringRef Comment);
629235633Sdim
630235633Sdim  StringRef getParentName() const { return ParentName; }
631218893Sdim};
632235633Sdim
633212904Sdim/// \brief Captures a result of code completion.
634212904Sdimclass CodeCompletionResult {
635198092Srdivackypublic:
636212904Sdim  /// \brief Describes the kind of result generated.
637212904Sdim  enum ResultKind {
638245431Sdim    RK_Declaration = 0, ///< Refers to a declaration
639245431Sdim    RK_Keyword,         ///< Refers to a keyword or symbol.
640245431Sdim    RK_Macro,           ///< Refers to a macro
641245431Sdim    RK_Pattern          ///< Refers to a precomputed pattern.
642212904Sdim  };
643235633Sdim
644235633Sdim  /// \brief When Kind == RK_Declaration or RK_Pattern, the declaration we are
645235633Sdim  /// referring to. In the latter case, the declaration might be NULL.
646252723Sdim  const NamedDecl *Declaration;
647235633Sdim
648212904Sdim  union {
649235633Sdim    /// \brief When Kind == RK_Keyword, the string representing the keyword
650212904Sdim    /// or symbol's spelling.
651212904Sdim    const char *Keyword;
652235633Sdim
653212904Sdim    /// \brief When Kind == RK_Pattern, the code-completion string that
654212904Sdim    /// describes the completion text to insert.
655212904Sdim    CodeCompletionString *Pattern;
656235633Sdim
657212904Sdim    /// \brief When Kind == RK_Macro, the identifier that refers to a macro.
658252723Sdim    const IdentifierInfo *Macro;
659212904Sdim  };
660208600Srdivacky
661212904Sdim  /// \brief The priority of this particular code-completion result.
662212904Sdim  unsigned Priority;
663208600Srdivacky
664245431Sdim  /// \brief Specifies which parameter (of a function, Objective-C method,
665245431Sdim  /// macro, etc.) we should start with when formatting the result.
666245431Sdim  unsigned StartParameter;
667245431Sdim
668245431Sdim  /// \brief The kind of result stored here.
669245431Sdim  ResultKind Kind;
670245431Sdim
671212904Sdim  /// \brief The cursor kind that describes this result.
672212904Sdim  CXCursorKind CursorKind;
673235633Sdim
674212904Sdim  /// \brief The availability of this result.
675212904Sdim  CXAvailabilityKind Availability;
676235633Sdim
677212904Sdim  /// \brief Whether this result is hidden by another name.
678212904Sdim  bool Hidden : 1;
679235633Sdim
680212904Sdim  /// \brief Whether this result was found via lookup into a base class.
681212904Sdim  bool QualifierIsInformative : 1;
682235633Sdim
683235633Sdim  /// \brief Whether this declaration is the beginning of a
684212904Sdim  /// nested-name-specifier and, therefore, should be followed by '::'.
685212904Sdim  bool StartsNestedNameSpecifier : 1;
686199512Srdivacky
687212904Sdim  /// \brief Whether all parameters (of a function, Objective-C
688212904Sdim  /// method, etc.) should be considered "informative".
689212904Sdim  bool AllParametersAreInformative : 1;
690199512Srdivacky
691212904Sdim  /// \brief Whether we're completing a declaration of the given entity,
692212904Sdim  /// rather than a use of that entity.
693212904Sdim  bool DeclaringEntity : 1;
694235633Sdim
695212904Sdim  /// \brief If the result should have a nested-name-specifier, this is it.
696235633Sdim  /// When \c QualifierIsInformative, the nested-name-specifier is
697212904Sdim  /// informative rather than required.
698212904Sdim  NestedNameSpecifier *Qualifier;
699235633Sdim
700212904Sdim  /// \brief Build a result that refers to a declaration.
701252723Sdim  CodeCompletionResult(const NamedDecl *Declaration,
702252723Sdim                       unsigned Priority,
703212904Sdim                       NestedNameSpecifier *Qualifier = 0,
704226890Sdim                       bool QualifierIsInformative = false,
705226890Sdim                       bool Accessible = true)
706252723Sdim    : Declaration(Declaration), Priority(Priority),
707245431Sdim      StartParameter(0), Kind(RK_Declaration),
708245431Sdim      Availability(CXAvailability_Available), Hidden(false),
709245431Sdim      QualifierIsInformative(QualifierIsInformative),
710212904Sdim      StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
711235633Sdim      DeclaringEntity(false), Qualifier(Qualifier) {
712226890Sdim    computeCursorKindAndAvailability(Accessible);
713212904Sdim  }
714235633Sdim
715212904Sdim  /// \brief Build a result that refers to a keyword or symbol.
716212904Sdim  CodeCompletionResult(const char *Keyword, unsigned Priority = CCP_Keyword)
717245431Sdim    : Declaration(0), Keyword(Keyword), Priority(Priority), StartParameter(0),
718245431Sdim      Kind(RK_Keyword), CursorKind(CXCursor_NotImplemented),
719245431Sdim      Availability(CXAvailability_Available), Hidden(false),
720245431Sdim      QualifierIsInformative(0), StartsNestedNameSpecifier(false),
721245431Sdim      AllParametersAreInformative(false), DeclaringEntity(false), Qualifier(0)
722245431Sdim  {
723212904Sdim  }
724235633Sdim
725212904Sdim  /// \brief Build a result that refers to a macro.
726252723Sdim  CodeCompletionResult(const IdentifierInfo *Macro,
727252723Sdim                       unsigned Priority = CCP_Macro)
728245431Sdim    : Declaration(0), Macro(Macro), Priority(Priority), StartParameter(0),
729245431Sdim      Kind(RK_Macro), CursorKind(CXCursor_MacroDefinition),
730245431Sdim      Availability(CXAvailability_Available), Hidden(false),
731245431Sdim      QualifierIsInformative(0), StartsNestedNameSpecifier(false),
732245431Sdim      AllParametersAreInformative(false), DeclaringEntity(false), Qualifier(0)
733245431Sdim  {
734212904Sdim  }
735199512Srdivacky
736212904Sdim  /// \brief Build a result that refers to a pattern.
737212904Sdim  CodeCompletionResult(CodeCompletionString *Pattern,
738212904Sdim                       unsigned Priority = CCP_CodePattern,
739212904Sdim                       CXCursorKind CursorKind = CXCursor_NotImplemented,
740235633Sdim                   CXAvailabilityKind Availability = CXAvailability_Available,
741252723Sdim                       const NamedDecl *D = 0)
742245431Sdim    : Declaration(D), Pattern(Pattern), Priority(Priority), StartParameter(0),
743245431Sdim      Kind(RK_Pattern), CursorKind(CursorKind), Availability(Availability),
744235633Sdim      Hidden(false), QualifierIsInformative(0),
745235633Sdim      StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
746235633Sdim      DeclaringEntity(false), Qualifier(0)
747235633Sdim  {
748212904Sdim  }
749235633Sdim
750235633Sdim  /// \brief Build a result that refers to a pattern with an associated
751235633Sdim  /// declaration.
752235633Sdim  CodeCompletionResult(CodeCompletionString *Pattern, NamedDecl *D,
753235633Sdim                       unsigned Priority)
754245431Sdim    : Declaration(D), Pattern(Pattern), Priority(Priority), StartParameter(0),
755245431Sdim      Kind(RK_Pattern), Availability(CXAvailability_Available), Hidden(false),
756245431Sdim      QualifierIsInformative(false), StartsNestedNameSpecifier(false),
757245431Sdim      AllParametersAreInformative(false), DeclaringEntity(false), Qualifier(0) {
758235633Sdim    computeCursorKindAndAvailability();
759235633Sdim  }
760235633Sdim
761212904Sdim  /// \brief Retrieve the declaration stored in this result.
762252723Sdim  const NamedDecl *getDeclaration() const {
763212904Sdim    assert(Kind == RK_Declaration && "Not a declaration result");
764212904Sdim    return Declaration;
765212904Sdim  }
766235633Sdim
767212904Sdim  /// \brief Retrieve the keyword stored in this result.
768212904Sdim  const char *getKeyword() const {
769212904Sdim    assert(Kind == RK_Keyword && "Not a keyword result");
770212904Sdim    return Keyword;
771212904Sdim  }
772235633Sdim
773212904Sdim  /// \brief Create a new code-completion string that describes how to insert
774212904Sdim  /// this result into a program.
775212904Sdim  ///
776212904Sdim  /// \param S The semantic analysis that created the result.
777212904Sdim  ///
778218893Sdim  /// \param Allocator The allocator that will be used to allocate the
779218893Sdim  /// string itself.
780212904Sdim  CodeCompletionString *CreateCodeCompletionString(Sema &S,
781235633Sdim                                           CodeCompletionAllocator &Allocator,
782245431Sdim                                           CodeCompletionTUInfo &CCTUInfo,
783245431Sdim                                           bool IncludeBriefComments);
784235633Sdim  CodeCompletionString *CreateCodeCompletionString(ASTContext &Ctx,
785235633Sdim                                                   Preprocessor &PP,
786235633Sdim                                           CodeCompletionAllocator &Allocator,
787245431Sdim                                           CodeCompletionTUInfo &CCTUInfo,
788245431Sdim                                           bool IncludeBriefComments);
789235633Sdim
790212904Sdimprivate:
791226890Sdim  void computeCursorKindAndAvailability(bool Accessible = true);
792212904Sdim};
793235633Sdim
794212904Sdimbool operator<(const CodeCompletionResult &X, const CodeCompletionResult &Y);
795235633Sdim
796235633Sdiminline bool operator>(const CodeCompletionResult &X,
797212904Sdim                      const CodeCompletionResult &Y) {
798212904Sdim  return Y < X;
799212904Sdim}
800235633Sdim
801235633Sdiminline bool operator<=(const CodeCompletionResult &X,
802212904Sdim                      const CodeCompletionResult &Y) {
803212904Sdim  return !(Y < X);
804212904Sdim}
805212904Sdim
806235633Sdiminline bool operator>=(const CodeCompletionResult &X,
807212904Sdim                       const CodeCompletionResult &Y) {
808212904Sdim  return !(X < Y);
809212904Sdim}
810212904Sdim
811235633Sdim
812235633Sdimraw_ostream &operator<<(raw_ostream &OS,
813212904Sdim                              const CodeCompletionString &CCS);
814212904Sdim
815235633Sdim/// \brief Abstract interface for a consumer of code-completion
816212904Sdim/// information.
817212904Sdimclass CodeCompleteConsumer {
818212904Sdimprotected:
819245431Sdim  const CodeCompleteOptions CodeCompleteOpts;
820212904Sdim
821212904Sdim  /// \brief Whether the output format for the code-completion consumer is
822212904Sdim  /// binary.
823212904Sdim  bool OutputIsBinary;
824235633Sdim
825212904Sdimpublic:
826198092Srdivacky  class OverloadCandidate {
827198092Srdivacky  public:
828198092Srdivacky    /// \brief Describes the type of overload candidate.
829198092Srdivacky    enum CandidateKind {
830198092Srdivacky      /// \brief The candidate is a function declaration.
831198092Srdivacky      CK_Function,
832198092Srdivacky      /// \brief The candidate is a function template.
833198092Srdivacky      CK_FunctionTemplate,
834198092Srdivacky      /// \brief The "candidate" is actually a variable, expression, or block
835198092Srdivacky      /// for which we only have a function prototype.
836198092Srdivacky      CK_FunctionType
837198092Srdivacky    };
838235633Sdim
839198092Srdivacky  private:
840198092Srdivacky    /// \brief The kind of overload candidate.
841198092Srdivacky    CandidateKind Kind;
842235633Sdim
843198092Srdivacky    union {
844235633Sdim      /// \brief The function overload candidate, available when
845198092Srdivacky      /// Kind == CK_Function.
846198092Srdivacky      FunctionDecl *Function;
847235633Sdim
848198092Srdivacky      /// \brief The function template overload candidate, available when
849198092Srdivacky      /// Kind == CK_FunctionTemplate.
850198092Srdivacky      FunctionTemplateDecl *FunctionTemplate;
851235633Sdim
852198092Srdivacky      /// \brief The function type that describes the entity being called,
853198092Srdivacky      /// when Kind == CK_FunctionType.
854198092Srdivacky      const FunctionType *Type;
855198092Srdivacky    };
856235633Sdim
857198092Srdivacky  public:
858198092Srdivacky    OverloadCandidate(FunctionDecl *Function)
859198092Srdivacky      : Kind(CK_Function), Function(Function) { }
860198092Srdivacky
861198092Srdivacky    OverloadCandidate(FunctionTemplateDecl *FunctionTemplateDecl)
862218893Sdim      : Kind(CK_FunctionTemplate), FunctionTemplate(FunctionTemplateDecl) { }
863198092Srdivacky
864198092Srdivacky    OverloadCandidate(const FunctionType *Type)
865198092Srdivacky      : Kind(CK_FunctionType), Type(Type) { }
866198092Srdivacky
867198092Srdivacky    /// \brief Determine the kind of overload candidate.
868198092Srdivacky    CandidateKind getKind() const { return Kind; }
869235633Sdim
870235633Sdim    /// \brief Retrieve the function overload candidate or the templated
871198092Srdivacky    /// function declaration for a function template.
872198092Srdivacky    FunctionDecl *getFunction() const;
873235633Sdim
874198092Srdivacky    /// \brief Retrieve the function template overload candidate.
875198092Srdivacky    FunctionTemplateDecl *getFunctionTemplate() const {
876198092Srdivacky      assert(getKind() == CK_FunctionTemplate && "Not a function template");
877198092Srdivacky      return FunctionTemplate;
878198092Srdivacky    }
879235633Sdim
880198092Srdivacky    /// \brief Retrieve the function type of the entity, regardless of how the
881198092Srdivacky    /// function is stored.
882198092Srdivacky    const FunctionType *getFunctionType() const;
883235633Sdim
884198092Srdivacky    /// \brief Create a new code-completion string that describes the function
885198092Srdivacky    /// signature of this overload candidate.
886235633Sdim    CodeCompletionString *CreateSignatureString(unsigned CurrentArg,
887218893Sdim                                                Sema &S,
888235633Sdim                                      CodeCompletionAllocator &Allocator,
889235633Sdim                                      CodeCompletionTUInfo &CCTUInfo) const;
890198092Srdivacky  };
891235633Sdim
892245431Sdim  CodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
893245431Sdim                       bool OutputIsBinary)
894245431Sdim    : CodeCompleteOpts(CodeCompleteOpts), OutputIsBinary(OutputIsBinary)
895245431Sdim  { }
896235633Sdim
897199482Srdivacky  /// \brief Whether the code-completion consumer wants to see macros.
898245431Sdim  bool includeMacros() const {
899245431Sdim    return CodeCompleteOpts.IncludeMacros;
900245431Sdim  }
901199990Srdivacky
902208600Srdivacky  /// \brief Whether the code-completion consumer wants to see code patterns.
903245431Sdim  bool includeCodePatterns() const {
904245431Sdim    return CodeCompleteOpts.IncludeCodePatterns;
905245431Sdim  }
906235633Sdim
907212904Sdim  /// \brief Whether to include global (top-level) declaration results.
908245431Sdim  bool includeGlobals() const {
909245431Sdim    return CodeCompleteOpts.IncludeGlobals;
910245431Sdim  }
911235633Sdim
912245431Sdim  /// \brief Whether to include brief documentation comments within the set of
913245431Sdim  /// code completions returned.
914245431Sdim  bool includeBriefComments() const {
915245431Sdim    return CodeCompleteOpts.IncludeBriefComments;
916245431Sdim  }
917245431Sdim
918199990Srdivacky  /// \brief Determine whether the output of this consumer is binary.
919199990Srdivacky  bool isOutputBinary() const { return OutputIsBinary; }
920235633Sdim
921198092Srdivacky  /// \brief Deregisters and destroys this code-completion consumer.
922198092Srdivacky  virtual ~CodeCompleteConsumer();
923199990Srdivacky
924198092Srdivacky  /// \name Code-completion callbacks
925198092Srdivacky  //@{
926198092Srdivacky  /// \brief Process the finalized code-completion results.
927235633Sdim  virtual void ProcessCodeCompleteResults(Sema &S,
928212904Sdim                                          CodeCompletionContext Context,
929212904Sdim                                          CodeCompletionResult *Results,
930198092Srdivacky                                          unsigned NumResults) { }
931199482Srdivacky
932199482Srdivacky  /// \param S the semantic-analyzer object for which code-completion is being
933199482Srdivacky  /// done.
934198092Srdivacky  ///
935198092Srdivacky  /// \param CurrentArg the index of the current argument.
936198092Srdivacky  ///
937198092Srdivacky  /// \param Candidates an array of overload candidates.
938198092Srdivacky  ///
939198092Srdivacky  /// \param NumCandidates the number of overload candidates
940199482Srdivacky  virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
941198092Srdivacky                                         OverloadCandidate *Candidates,
942198092Srdivacky                                         unsigned NumCandidates) { }
943198092Srdivacky  //@}
944235633Sdim
945218893Sdim  /// \brief Retrieve the allocator that will be used to allocate
946218893Sdim  /// code completion strings.
947218893Sdim  virtual CodeCompletionAllocator &getAllocator() = 0;
948235633Sdim
949235633Sdim  virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() = 0;
950198092Srdivacky};
951212904Sdim
952235633Sdim/// \brief A simple code-completion consumer that prints the results it
953198092Srdivacky/// receives in a simple format.
954198092Srdivackyclass PrintingCodeCompleteConsumer : public CodeCompleteConsumer {
955198092Srdivacky  /// \brief The raw output stream.
956226890Sdim  raw_ostream &OS;
957235633Sdim
958235633Sdim  CodeCompletionTUInfo CCTUInfo;
959235633Sdim
960198092Srdivackypublic:
961198092Srdivacky  /// \brief Create a new printing code-completion consumer that prints its
962198092Srdivacky  /// results to the given raw output stream.
963245431Sdim  PrintingCodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
964226890Sdim                               raw_ostream &OS)
965245431Sdim    : CodeCompleteConsumer(CodeCompleteOpts, false), OS(OS),
966235633Sdim      CCTUInfo(new GlobalCodeCompletionAllocator) {}
967235633Sdim
968198092Srdivacky  /// \brief Prints the finalized code-completion results.
969235633Sdim  virtual void ProcessCodeCompleteResults(Sema &S,
970212904Sdim                                          CodeCompletionContext Context,
971212904Sdim                                          CodeCompletionResult *Results,
972198092Srdivacky                                          unsigned NumResults);
973235633Sdim
974199482Srdivacky  virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
975198092Srdivacky                                         OverloadCandidate *Candidates,
976235633Sdim                                         unsigned NumCandidates);
977235633Sdim
978235633Sdim  virtual CodeCompletionAllocator &getAllocator() {
979235633Sdim    return CCTUInfo.getAllocator();
980235633Sdim  }
981235633Sdim
982235633Sdim  virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() { return CCTUInfo; }
983198092Srdivacky};
984235633Sdim
985198092Srdivacky} // end namespace clang
986198092Srdivacky
987198092Srdivacky#endif // LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
988