]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Sema/CodeCompleteConsumer.h
Merge ACPICA 20101013.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Sema / CodeCompleteConsumer.h
1 //===---- CodeCompleteConsumer.h - Code Completion Interface ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the CodeCompleteConsumer class.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
14 #define LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
15
16 #include "clang/AST/Type.h"
17 #include "clang/AST/CanonicalType.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "clang-c/Index.h"
21 #include <memory>
22 #include <string>
23
24 namespace llvm {
25   class raw_ostream;
26 }
27
28 namespace clang {
29
30 class Decl;
31   
32 /// \brief Default priority values for code-completion results based
33 /// on their kind.
34 enum {
35   /// \brief Priority for the next initialization in a constructor initializer
36   /// list.
37   CCP_NextInitializer = 7,
38   /// \brief Priority for a send-to-super completion.
39   CCP_SuperCompletion = 8,
40   /// \brief Priority for a declaration that is in the local scope.
41   CCP_LocalDeclaration = 8,
42   /// \brief Priority for a member declaration found from the current
43   /// method or member function.
44   CCP_MemberDeclaration = 20,
45   /// \brief Priority for a language keyword (that isn't any of the other
46   /// categories).
47   CCP_Keyword = 30,
48   /// \brief Priority for a code pattern.
49   CCP_CodePattern = 30,
50   /// \brief Priority for a non-type declaration.
51   CCP_Declaration = 50,
52   /// \brief Priority for a constant value (e.g., enumerator).
53   CCP_Constant = 60,
54   /// \brief Priority for a type.
55   CCP_Type = 65,
56   /// \brief Priority for a preprocessor macro.
57   CCP_Macro = 70,
58   /// \brief Priority for a nested-name-specifier.
59   CCP_NestedNameSpecifier = 75,
60   /// \brief Priority for a result that isn't likely to be what the user wants,
61   /// but is included for completeness.
62   CCP_Unlikely = 80
63 };
64
65 /// \brief Priority value deltas that are added to code-completion results
66 /// based on the context of the result.
67 enum {
68   /// \brief The result is in a base class.
69   CCD_InBaseClass = 2,
70   /// \brief The result is a type match against void.
71   ///
72   /// Since everything converts to "void", we don't give as drastic an 
73   /// adjustment for matching void.
74   CCD_VoidMatch = -5,
75   /// \brief The result is a C++ non-static member function whose qualifiers
76   /// exactly match the object type on which the member function can be called.
77   CCD_ObjectQualifierMatch = -1,
78   /// \brief The selector of the given message exactly matches the selector
79   /// of the current method, which might imply that some kind of delegation
80   /// is occurring.
81   CCD_SelectorMatch = -3
82 };
83
84 /// \brief Priority value factors by which we will divide or multiply the
85 /// priority of a code-completion result.
86 enum {
87   /// \brief Divide by this factor when a code-completion result's type exactly
88   /// matches the type we expect.
89   CCF_ExactTypeMatch = 4,
90   /// \brief Divide by this factor when a code-completion result's type is
91   /// similar to the type we expect (e.g., both arithmetic types, both
92   /// Objective-C object pointer types).
93   CCF_SimilarTypeMatch = 2
94 };
95
96 /// \brief A simplified classification of types used when determining
97 /// "similar" types for code completion.
98 enum SimplifiedTypeClass {
99   STC_Arithmetic,
100   STC_Array,
101   STC_Block,
102   STC_Function,
103   STC_ObjectiveC,
104   STC_Other,
105   STC_Pointer,
106   STC_Record,
107   STC_Void
108 };
109   
110 /// \brief Determine the simplified type class of the given canonical type.
111 SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T);
112   
113 /// \brief Determine the type that this declaration will have if it is used
114 /// as a type or in an expression.
115 QualType getDeclUsageType(ASTContext &C, NamedDecl *ND);
116   
117 /// \brief Determine the priority to be given to a macro code completion result
118 /// with the given name.
119 ///
120 /// \param MacroName The name of the macro.
121 ///
122 /// \param PreferredTypeIsPointer Whether the preferred type for the context
123 /// of this macro is a pointer type.
124 unsigned getMacroUsagePriority(llvm::StringRef MacroName, 
125                                bool PreferredTypeIsPointer = false);
126
127 /// \brief Determine the libclang cursor kind associated with the given
128 /// declaration.
129 CXCursorKind getCursorKindForDecl(Decl *D);
130   
131 class FunctionDecl;
132 class FunctionType;
133 class FunctionTemplateDecl;
134 class IdentifierInfo;
135 class NamedDecl;
136 class NestedNameSpecifier;
137 class Sema;
138
139 /// \brief The context in which code completion occurred, so that the
140 /// code-completion consumer can process the results accordingly.
141 class CodeCompletionContext {
142 public:
143   enum Kind {
144     /// \brief An unspecified code-completion context.
145     CCC_Other,
146     /// \brief Code completion occurred within a "top-level" completion context,
147     /// e.g., at namespace or global scope.
148     CCC_TopLevel,
149     /// \brief Code completion occurred within an Objective-C interface,
150     /// protocol, or category interface.
151     CCC_ObjCInterface,
152     /// \brief Code completion occurred within an Objective-C implementation
153     /// or category implementation.
154     CCC_ObjCImplementation,
155     /// \brief Code completion occurred within the instance variable list of
156     /// an Objective-C interface, implementation, or category implementation.
157     CCC_ObjCIvarList,
158     /// \brief Code completion occurred within a class, struct, or union.
159     CCC_ClassStructUnion,
160     /// \brief Code completion occurred where a statement (or declaration) is
161     /// expected in a function, method, or block.
162     CCC_Statement,
163     /// \brief Code completion occurred where an expression is expected.
164     CCC_Expression,
165     /// \brief Code completion occurred where an Objective-C message receiver
166     /// is expected.
167     CCC_ObjCMessageReceiver,
168     /// \brief Code completion occurred on the right-hand side of a member
169     /// access expression.
170     ///
171     /// The results of this completion are the members of the type being 
172     /// accessed. The type itself is available via 
173     /// \c CodeCompletionContext::getType().
174     CCC_MemberAccess,
175     /// \brief Code completion occurred after the "enum" keyword, to indicate
176     /// an enumeration name.
177     CCC_EnumTag,
178     /// \brief Code completion occurred after the "union" keyword, to indicate
179     /// a union name.
180     CCC_UnionTag,
181     /// \brief Code completion occurred after the "struct" or "class" keyword,
182     /// to indicate a struct or class name.
183     CCC_ClassOrStructTag,
184     /// \brief Code completion occurred where a protocol name is expected.
185     CCC_ObjCProtocolName,
186     /// \brief Code completion occurred where a namespace or namespace alias
187     /// is expected.
188     CCC_Namespace,
189     /// \brief Code completion occurred where a type name is expected.
190     CCC_Type,
191     /// \brief Code completion occurred where a new name is expected.
192     CCC_Name,
193     /// \brief Code completion occurred where a new name is expected and a
194     /// qualified name is permissible.
195     CCC_PotentiallyQualifiedName,
196     /// \brief Code completion occurred where an macro is being defined.
197     CCC_MacroName,
198     /// \brief Code completion occurred where a macro name is expected
199     /// (without any arguments, in the case of a function-like macro).
200     CCC_MacroNameUse,
201     /// \brief Code completion occurred within a preprocessor expression.
202     CCC_PreprocessorExpression,
203     /// \brief Code completion occurred where a preprocessor directive is 
204     /// expected.
205     CCC_PreprocessorDirective,
206     /// \brief Code completion occurred in a context where natural language is
207     /// expected, e.g., a comment or string literal.
208     ///
209     /// This context usually implies that no completions should be added,
210     /// unless they come from an appropriate natural-language dictionary.
211     CCC_NaturalLanguage,
212     /// \brief Code completion for a selector, as in an @selector expression.
213     CCC_SelectorName,
214     /// \brief Code completion within a type-qualifier list.
215     CCC_TypeQualifiers
216   };
217
218 private:
219   enum Kind Kind;
220
221   /// \brief The type that would prefer to see at this point (e.g., the type
222   /// of an initializer or function parameter).
223   QualType PreferredType;
224   
225   /// \brief The type of the base object in a member access expression.
226   QualType BaseType;
227   
228 public:
229   /// \brief Construct a new code-completion context of the given kind.
230   CodeCompletionContext(enum Kind Kind) : Kind(Kind) { }
231   
232   /// \brief Construct a new code-completion context of the given kind.
233   CodeCompletionContext(enum Kind Kind, QualType T) : Kind(Kind) { 
234     if (Kind == CCC_MemberAccess)
235       BaseType = T;
236     else
237       PreferredType = T;
238   }
239   
240   /// \brief Retrieve the kind of code-completion context.
241   enum Kind getKind() const { return Kind; }
242   
243   /// \brief Retrieve the type that this expression would prefer to have, e.g.,
244   /// if the expression is a variable initializer or a function argument, the
245   /// type of the corresponding variable or function parameter.
246   QualType getPreferredType() const { return PreferredType; }
247   
248   /// \brief Retrieve the type of the base object in a member-access 
249   /// expression.
250   QualType getBaseType() const { return BaseType; }
251 };
252
253
254 /// \brief A "string" used to describe how code completion can
255 /// be performed for an entity.
256 ///
257 /// A code completion string typically shows how a particular entity can be 
258 /// used. For example, the code completion string for a function would show
259 /// the syntax to call it, including the parentheses, placeholders for the 
260 /// arguments, etc.  
261 class CodeCompletionString {
262 public:
263   /// \brief The different kinds of "chunks" that can occur within a code
264   /// completion string.
265   enum ChunkKind {
266     /// \brief The piece of text that the user is expected to type to
267     /// match the code-completion string, typically a keyword or the name of a
268     /// declarator or macro.
269     CK_TypedText,
270     /// \brief A piece of text that should be placed in the buffer, e.g.,
271     /// parentheses or a comma in a function call.
272     CK_Text,
273     /// \brief A code completion string that is entirely optional. For example,
274     /// an optional code completion string that describes the default arguments
275     /// in a function call.
276     CK_Optional,
277     /// \brief A string that acts as a placeholder for, e.g., a function 
278     /// call argument.
279     CK_Placeholder,
280     /// \brief A piece of text that describes something about the result but
281     /// should not be inserted into the buffer.
282     CK_Informative,
283     /// \brief A piece of text that describes the type of an entity or, for
284     /// functions and methods, the return type.
285     CK_ResultType,
286     /// \brief A piece of text that describes the parameter that corresponds
287     /// to the code-completion location within a function call, message send,
288     /// macro invocation, etc.
289     CK_CurrentParameter,
290     /// \brief A left parenthesis ('(').
291     CK_LeftParen,
292     /// \brief A right parenthesis (')').
293     CK_RightParen,
294     /// \brief A left bracket ('[').
295     CK_LeftBracket,
296     /// \brief A right bracket (']').
297     CK_RightBracket,
298     /// \brief A left brace ('{').
299     CK_LeftBrace,
300     /// \brief A right brace ('}').
301     CK_RightBrace,
302     /// \brief A left angle bracket ('<').
303     CK_LeftAngle,
304     /// \brief A right angle bracket ('>').
305     CK_RightAngle,
306     /// \brief A comma separator (',').
307     CK_Comma,
308     /// \brief A colon (':').
309     CK_Colon,
310     /// \brief A semicolon (';').
311     CK_SemiColon,
312     /// \brief An '=' sign.
313     CK_Equal,
314     /// \brief Horizontal whitespace (' ').
315     CK_HorizontalSpace,
316     /// \brief Verticle whitespace ('\n' or '\r\n', depending on the
317     /// platform).
318     CK_VerticalSpace
319   };
320   
321   /// \brief One piece of the code completion string.
322   struct Chunk {
323     /// \brief The kind of data stored in this piece of the code completion 
324     /// string.
325     ChunkKind Kind;
326     
327     union {
328       /// \brief The text string associated with a CK_Text, CK_Placeholder,
329       /// CK_Informative, or CK_Comma chunk.
330       /// The string is owned by the chunk and will be deallocated 
331       /// (with delete[]) when the chunk is destroyed.
332       const char *Text;
333       
334       /// \brief The code completion string associated with a CK_Optional chunk.
335       /// The optional code completion string is owned by the chunk, and will
336       /// be deallocated (with delete) when the chunk is destroyed.
337       CodeCompletionString *Optional;
338     };
339     
340     Chunk() : Kind(CK_Text), Text(0) { }
341     
342     Chunk(ChunkKind Kind, llvm::StringRef Text = "");
343     
344     /// \brief Create a new text chunk.
345     static Chunk CreateText(llvm::StringRef Text);
346
347     /// \brief Create a new optional chunk.
348     static Chunk CreateOptional(std::auto_ptr<CodeCompletionString> Optional);
349
350     /// \brief Create a new placeholder chunk.
351     static Chunk CreatePlaceholder(llvm::StringRef Placeholder);
352
353     /// \brief Create a new informative chunk.
354     static Chunk CreateInformative(llvm::StringRef Informative);
355
356     /// \brief Create a new result type chunk.
357     static Chunk CreateResultType(llvm::StringRef ResultType);
358
359     /// \brief Create a new current-parameter chunk.
360     static Chunk CreateCurrentParameter(llvm::StringRef CurrentParameter);
361
362     /// \brief Clone the given chunk.
363     Chunk Clone() const;
364     
365     /// \brief Destroy this chunk, deallocating any memory it owns.
366     void Destroy();
367   };
368   
369 private:
370   /// \brief The chunks stored in this string.
371   llvm::SmallVector<Chunk, 4> Chunks;
372   
373   CodeCompletionString(const CodeCompletionString &); // DO NOT IMPLEMENT
374   CodeCompletionString &operator=(const CodeCompletionString &); // DITTO
375   
376 public:
377   CodeCompletionString() { }
378   ~CodeCompletionString() { clear(); }
379   
380   typedef llvm::SmallVector<Chunk, 4>::const_iterator iterator;
381   iterator begin() const { return Chunks.begin(); }
382   iterator end() const { return Chunks.end(); }
383   bool empty() const { return Chunks.empty(); }
384   unsigned size() const { return Chunks.size(); }
385   void clear();
386   
387   Chunk &operator[](unsigned I) {
388     assert(I < size() && "Chunk index out-of-range");
389     return Chunks[I];
390   }
391
392   const Chunk &operator[](unsigned I) const {
393     assert(I < size() && "Chunk index out-of-range");
394     return Chunks[I];
395   }
396   
397   /// \brief Add a new typed-text chunk.
398   /// The text string will be copied.
399   void AddTypedTextChunk(llvm::StringRef Text) { 
400     Chunks.push_back(Chunk(CK_TypedText, Text));
401   }
402   
403   /// \brief Add a new text chunk.
404   /// The text string will be copied.
405   void AddTextChunk(llvm::StringRef Text) { 
406     Chunks.push_back(Chunk::CreateText(Text)); 
407   }
408   
409   /// \brief Add a new optional chunk.
410   void AddOptionalChunk(std::auto_ptr<CodeCompletionString> Optional) {
411     Chunks.push_back(Chunk::CreateOptional(Optional));
412   }
413   
414   /// \brief Add a new placeholder chunk.
415   /// The placeholder text will be copied.
416   void AddPlaceholderChunk(llvm::StringRef Placeholder) {
417     Chunks.push_back(Chunk::CreatePlaceholder(Placeholder));
418   }
419
420   /// \brief Add a new informative chunk.
421   /// The text will be copied.
422   void AddInformativeChunk(llvm::StringRef Text) {
423     Chunks.push_back(Chunk::CreateInformative(Text));
424   }
425
426   /// \brief Add a new result-type chunk.
427   /// The text will be copied.
428   void AddResultTypeChunk(llvm::StringRef ResultType) {
429     Chunks.push_back(Chunk::CreateResultType(ResultType));
430   }
431   
432   /// \brief Add a new current-parameter chunk.
433   /// The text will be copied.
434   void AddCurrentParameterChunk(llvm::StringRef CurrentParameter) {
435     Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter));
436   }
437   
438   /// \brief Add a new chunk.
439   void AddChunk(Chunk C) { Chunks.push_back(C); }
440   
441   /// \brief Returns the text in the TypedText chunk.
442   const char *getTypedText() const;
443
444   /// \brief Retrieve a string representation of the code completion string,
445   /// which is mainly useful for debugging.
446   std::string getAsString() const; 
447   
448   /// \brief Clone this code-completion string.
449   ///
450   /// \param Result If non-NULL, points to an empty code-completion
451   /// result that will be given a cloned copy of
452   CodeCompletionString *Clone(CodeCompletionString *Result = 0) const;
453   
454   /// \brief Serialize this code-completion string to the given stream.
455   void Serialize(llvm::raw_ostream &OS) const;
456   
457   /// \brief Deserialize a code-completion string from the given string.
458   ///
459   /// \returns true if successful, false otherwise.
460   bool Deserialize(const char *&Str, const char *StrEnd);
461 };
462
463 /// \brief Captures a result of code completion.
464 class CodeCompletionResult {
465 public:
466   /// \brief Describes the kind of result generated.
467   enum ResultKind {
468     RK_Declaration = 0, //< Refers to a declaration
469     RK_Keyword,         //< Refers to a keyword or symbol.
470     RK_Macro,           //< Refers to a macro
471     RK_Pattern          //< Refers to a precomputed pattern.
472   };
473     
474   /// \brief The kind of result stored here.
475   ResultKind Kind;
476     
477   union {
478     /// \brief When Kind == RK_Declaration, the declaration we are referring
479     /// to.
480     NamedDecl *Declaration;
481       
482     /// \brief When Kind == RK_Keyword, the string representing the keyword 
483     /// or symbol's spelling.
484     const char *Keyword;
485       
486     /// \brief When Kind == RK_Pattern, the code-completion string that
487     /// describes the completion text to insert.
488     CodeCompletionString *Pattern;
489       
490     /// \brief When Kind == RK_Macro, the identifier that refers to a macro.
491     IdentifierInfo *Macro;
492   };
493
494   /// \brief The priority of this particular code-completion result.
495   unsigned Priority;
496
497   /// \brief The cursor kind that describes this result.
498   CXCursorKind CursorKind;
499     
500   /// \brief The availability of this result.
501   CXAvailabilityKind Availability;
502     
503   /// \brief Specifies which parameter (of a function, Objective-C method,
504   /// macro, etc.) we should start with when formatting the result.
505   unsigned StartParameter;
506     
507   /// \brief Whether this result is hidden by another name.
508   bool Hidden : 1;
509     
510   /// \brief Whether this result was found via lookup into a base class.
511   bool QualifierIsInformative : 1;
512     
513   /// \brief Whether this declaration is the beginning of a 
514   /// nested-name-specifier and, therefore, should be followed by '::'.
515   bool StartsNestedNameSpecifier : 1;
516
517   /// \brief Whether all parameters (of a function, Objective-C
518   /// method, etc.) should be considered "informative".
519   bool AllParametersAreInformative : 1;
520
521   /// \brief Whether we're completing a declaration of the given entity,
522   /// rather than a use of that entity.
523   bool DeclaringEntity : 1;
524     
525   /// \brief If the result should have a nested-name-specifier, this is it.
526   /// When \c QualifierIsInformative, the nested-name-specifier is 
527   /// informative rather than required.
528   NestedNameSpecifier *Qualifier;
529     
530   /// \brief Build a result that refers to a declaration.
531   CodeCompletionResult(NamedDecl *Declaration, 
532                        NestedNameSpecifier *Qualifier = 0,
533                        bool QualifierIsInformative = false)
534     : Kind(RK_Declaration), Declaration(Declaration), 
535       Priority(getPriorityFromDecl(Declaration)), 
536       Availability(CXAvailability_Available), StartParameter(0), 
537       Hidden(false), QualifierIsInformative(QualifierIsInformative),
538       StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
539       DeclaringEntity(false), Qualifier(Qualifier) { 
540     computeCursorKindAndAvailability();
541   }
542     
543   /// \brief Build a result that refers to a keyword or symbol.
544   CodeCompletionResult(const char *Keyword, unsigned Priority = CCP_Keyword)
545     : Kind(RK_Keyword), Keyword(Keyword), Priority(Priority), 
546       Availability(CXAvailability_Available), 
547       StartParameter(0), Hidden(false), QualifierIsInformative(0), 
548       StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
549       DeclaringEntity(false), Qualifier(0) {
550     computeCursorKindAndAvailability();
551   }
552     
553   /// \brief Build a result that refers to a macro.
554   CodeCompletionResult(IdentifierInfo *Macro, unsigned Priority = CCP_Macro)
555     : Kind(RK_Macro), Macro(Macro), Priority(Priority), 
556       Availability(CXAvailability_Available), StartParameter(0), 
557       Hidden(false), QualifierIsInformative(0), 
558       StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
559       DeclaringEntity(false), Qualifier(0) { 
560     computeCursorKindAndAvailability();
561   }
562
563   /// \brief Build a result that refers to a pattern.
564   CodeCompletionResult(CodeCompletionString *Pattern,
565                        unsigned Priority = CCP_CodePattern,
566                        CXCursorKind CursorKind = CXCursor_NotImplemented,
567                    CXAvailabilityKind Availability = CXAvailability_Available)
568     : Kind(RK_Pattern), Pattern(Pattern), Priority(Priority), 
569       CursorKind(CursorKind), Availability(Availability), StartParameter(0), 
570       Hidden(false), QualifierIsInformative(0), 
571       StartsNestedNameSpecifier(false), AllParametersAreInformative(false), 
572       DeclaringEntity(false), Qualifier(0) 
573   { 
574   }
575     
576   /// \brief Retrieve the declaration stored in this result.
577   NamedDecl *getDeclaration() const {
578     assert(Kind == RK_Declaration && "Not a declaration result");
579     return Declaration;
580   }
581     
582   /// \brief Retrieve the keyword stored in this result.
583   const char *getKeyword() const {
584     assert(Kind == RK_Keyword && "Not a keyword result");
585     return Keyword;
586   }
587     
588   /// \brief Create a new code-completion string that describes how to insert
589   /// this result into a program.
590   ///
591   /// \param S The semantic analysis that created the result.
592   ///
593   /// \param Result If non-NULL, the already-allocated, empty
594   /// code-completion string that will be populated with the
595   /// appropriate code completion string for this result.
596   CodeCompletionString *CreateCodeCompletionString(Sema &S,
597                                            CodeCompletionString *Result = 0);
598     
599   void Destroy();
600     
601   /// brief Determine a base priority for the given declaration.
602   static unsigned getPriorityFromDecl(NamedDecl *ND);
603     
604 private:
605   void computeCursorKindAndAvailability();
606 };
607   
608 bool operator<(const CodeCompletionResult &X, const CodeCompletionResult &Y);
609   
610 inline bool operator>(const CodeCompletionResult &X, 
611                       const CodeCompletionResult &Y) {
612   return Y < X;
613 }
614   
615 inline bool operator<=(const CodeCompletionResult &X, 
616                       const CodeCompletionResult &Y) {
617   return !(Y < X);
618 }
619
620 inline bool operator>=(const CodeCompletionResult &X, 
621                        const CodeCompletionResult &Y) {
622   return !(X < Y);
623 }
624
625   
626 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, 
627                               const CodeCompletionString &CCS);
628
629 /// \brief Abstract interface for a consumer of code-completion 
630 /// information.
631 class CodeCompleteConsumer {
632 protected:
633   /// \brief Whether to include macros in the code-completion results.
634   bool IncludeMacros;
635
636   /// \brief Whether to include code patterns (such as for loops) within
637   /// the completion results.
638   bool IncludeCodePatterns;
639   
640   /// \brief Whether to include global (top-level) declarations and names in
641   /// the completion results.
642   bool IncludeGlobals;
643   
644   /// \brief Whether the output format for the code-completion consumer is
645   /// binary.
646   bool OutputIsBinary;
647   
648 public:
649   class OverloadCandidate {
650   public:
651     /// \brief Describes the type of overload candidate.
652     enum CandidateKind {
653       /// \brief The candidate is a function declaration.
654       CK_Function,
655       /// \brief The candidate is a function template.
656       CK_FunctionTemplate,
657       /// \brief The "candidate" is actually a variable, expression, or block
658       /// for which we only have a function prototype.
659       CK_FunctionType
660     };
661     
662   private:
663     /// \brief The kind of overload candidate.
664     CandidateKind Kind;
665     
666     union {
667       /// \brief The function overload candidate, available when 
668       /// Kind == CK_Function.
669       FunctionDecl *Function;
670       
671       /// \brief The function template overload candidate, available when
672       /// Kind == CK_FunctionTemplate.
673       FunctionTemplateDecl *FunctionTemplate;
674       
675       /// \brief The function type that describes the entity being called,
676       /// when Kind == CK_FunctionType.
677       const FunctionType *Type;
678     };
679     
680   public:
681     OverloadCandidate(FunctionDecl *Function)
682       : Kind(CK_Function), Function(Function) { }
683
684     OverloadCandidate(FunctionTemplateDecl *FunctionTemplateDecl)
685       : Kind(CK_FunctionTemplate), FunctionTemplate(FunctionTemplate) { }
686
687     OverloadCandidate(const FunctionType *Type)
688       : Kind(CK_FunctionType), Type(Type) { }
689
690     /// \brief Determine the kind of overload candidate.
691     CandidateKind getKind() const { return Kind; }
692     
693     /// \brief Retrieve the function overload candidate or the templated 
694     /// function declaration for a function template.
695     FunctionDecl *getFunction() const;
696     
697     /// \brief Retrieve the function template overload candidate.
698     FunctionTemplateDecl *getFunctionTemplate() const {
699       assert(getKind() == CK_FunctionTemplate && "Not a function template");
700       return FunctionTemplate;
701     }
702     
703     /// \brief Retrieve the function type of the entity, regardless of how the
704     /// function is stored.
705     const FunctionType *getFunctionType() const;
706     
707     /// \brief Create a new code-completion string that describes the function
708     /// signature of this overload candidate.
709     CodeCompletionString *CreateSignatureString(unsigned CurrentArg, 
710                                                 Sema &S) const;    
711   };
712   
713   CodeCompleteConsumer() : IncludeMacros(false), IncludeCodePatterns(false),
714                            IncludeGlobals(true), OutputIsBinary(false) { }
715   
716   CodeCompleteConsumer(bool IncludeMacros, bool IncludeCodePatterns,
717                        bool IncludeGlobals, bool OutputIsBinary)
718     : IncludeMacros(IncludeMacros), IncludeCodePatterns(IncludeCodePatterns),
719       IncludeGlobals(IncludeGlobals), OutputIsBinary(OutputIsBinary) { }
720   
721   /// \brief Whether the code-completion consumer wants to see macros.
722   bool includeMacros() const { return IncludeMacros; }
723
724   /// \brief Whether the code-completion consumer wants to see code patterns.
725   bool includeCodePatterns() const { return IncludeCodePatterns; }
726   
727   /// \brief Whether to include global (top-level) declaration results.
728   bool includeGlobals() const { return IncludeGlobals; }
729   
730   /// \brief Determine whether the output of this consumer is binary.
731   bool isOutputBinary() const { return OutputIsBinary; }
732   
733   /// \brief Deregisters and destroys this code-completion consumer.
734   virtual ~CodeCompleteConsumer();
735
736   /// \name Code-completion callbacks
737   //@{
738   /// \brief Process the finalized code-completion results.
739   virtual void ProcessCodeCompleteResults(Sema &S, 
740                                           CodeCompletionContext Context,
741                                           CodeCompletionResult *Results,
742                                           unsigned NumResults) { }
743
744   /// \param S the semantic-analyzer object for which code-completion is being
745   /// done.
746   ///
747   /// \param CurrentArg the index of the current argument.
748   ///
749   /// \param Candidates an array of overload candidates.
750   ///
751   /// \param NumCandidates the number of overload candidates
752   virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
753                                          OverloadCandidate *Candidates,
754                                          unsigned NumCandidates) { }
755   //@}
756 };
757
758 /// \brief A simple code-completion consumer that prints the results it 
759 /// receives in a simple format.
760 class PrintingCodeCompleteConsumer : public CodeCompleteConsumer {
761   /// \brief The raw output stream.
762   llvm::raw_ostream &OS;
763     
764 public:
765   /// \brief Create a new printing code-completion consumer that prints its
766   /// results to the given raw output stream.
767   PrintingCodeCompleteConsumer(bool IncludeMacros, bool IncludeCodePatterns,
768                                bool IncludeGlobals,
769                                llvm::raw_ostream &OS)
770     : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
771                            false), OS(OS) {}
772   
773   /// \brief Prints the finalized code-completion results.
774   virtual void ProcessCodeCompleteResults(Sema &S, 
775                                           CodeCompletionContext Context,
776                                           CodeCompletionResult *Results,
777                                           unsigned NumResults);
778   
779   virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
780                                          OverloadCandidate *Candidates,
781                                          unsigned NumCandidates);  
782 };
783   
784 /// \brief A code-completion consumer that prints the results it receives
785 /// in a format that is parsable by the CIndex library.
786 class CIndexCodeCompleteConsumer : public CodeCompleteConsumer {
787   /// \brief The raw output stream.
788   llvm::raw_ostream &OS;
789   
790 public:
791   /// \brief Create a new CIndex code-completion consumer that prints its
792   /// results to the given raw output stream in a format readable to the CIndex
793   /// library.
794   CIndexCodeCompleteConsumer(bool IncludeMacros, bool IncludeCodePatterns,
795                              bool IncludeGlobals, llvm::raw_ostream &OS)
796     : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
797                            true), OS(OS) {}
798   
799   /// \brief Prints the finalized code-completion results.
800   virtual void ProcessCodeCompleteResults(Sema &S, 
801                                           CodeCompletionContext Context,
802                                           CodeCompletionResult *Results,
803                                           unsigned NumResults);
804   
805   virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
806                                          OverloadCandidate *Candidates,
807                                          unsigned NumCandidates);  
808 };
809
810 } // end namespace clang
811
812 #endif // LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H