1 //===---- CodeCompleteConsumer.h - Code Completion Interface ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the CodeCompleteConsumer class.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
14 #define LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H
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 "llvm/Support/Allocator.h"
21 #include "clang-c/Index.h"
28 /// \brief Default priority values for code-completion results based
31 /// \brief Priority for the next initialization in a constructor initializer
33 CCP_NextInitializer = 7,
34 /// \brief Priority for an enumeration constant inside a switch whose
35 /// condition is of the enumeration type.
37 /// \brief Priority for a send-to-super completion.
38 CCP_SuperCompletion = 20,
39 /// \brief Priority for a declaration that is in the local scope.
40 CCP_LocalDeclaration = 34,
41 /// \brief Priority for a member declaration found from the current
42 /// method or member function.
43 CCP_MemberDeclaration = 35,
44 /// \brief Priority for a language keyword (that isn't any of the other
47 /// \brief Priority for a code pattern.
49 /// \brief Priority for a non-type declaration.
51 /// \brief Priority for a type.
52 CCP_Type = CCP_Declaration,
53 /// \brief Priority for a constant value (e.g., enumerator).
55 /// \brief Priority for a preprocessor macro.
57 /// \brief Priority for a nested-name-specifier.
58 CCP_NestedNameSpecifier = 75,
59 /// \brief Priority for a result that isn't likely to be what the user wants,
60 /// but is included for completeness.
63 /// \brief Priority for the Objective-C "_cmd" implicit parameter.
64 CCP_ObjC_cmd = CCP_Unlikely
67 /// \brief Priority value deltas that are added to code-completion results
68 /// based on the context of the result.
70 /// \brief The result is in a base class.
72 /// \brief The result is a C++ non-static member function whose qualifiers
73 /// exactly match the object type on which the member function can be called.
74 CCD_ObjectQualifierMatch = -1,
75 /// \brief The selector of the given message exactly matches the selector
76 /// of the current method, which might imply that some kind of delegation
78 CCD_SelectorMatch = -3,
80 /// \brief Adjustment to the "bool" type in Objective-C, where the typedef
81 /// "BOOL" is preferred.
84 /// \brief Adjustment for KVC code pattern priorities when it doesn't look
86 CCD_ProbablyNotObjCCollection = 15,
88 /// \brief An Objective-C method being used as a property.
89 CCD_MethodAsProperty = 2
92 /// \brief Priority value factors by which we will divide or multiply the
93 /// priority of a code-completion result.
95 /// \brief Divide by this factor when a code-completion result's type exactly
96 /// matches the type we expect.
97 CCF_ExactTypeMatch = 4,
98 /// \brief Divide by this factor when a code-completion result's type is
99 /// similar to the type we expect (e.g., both arithmetic types, both
100 /// Objective-C object pointer types).
101 CCF_SimilarTypeMatch = 2
104 /// \brief A simplified classification of types used when determining
105 /// "similar" types for code completion.
106 enum SimplifiedTypeClass {
118 /// \brief Determine the simplified type class of the given canonical type.
119 SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T);
121 /// \brief Determine the type that this declaration will have if it is used
122 /// as a type or in an expression.
123 QualType getDeclUsageType(ASTContext &C, NamedDecl *ND);
125 /// \brief Determine the priority to be given to a macro code completion result
126 /// with the given name.
128 /// \param MacroName The name of the macro.
130 /// \param LangOpts Options describing the current language dialect.
132 /// \param PreferredTypeIsPointer Whether the preferred type for the context
133 /// of this macro is a pointer type.
134 unsigned getMacroUsagePriority(StringRef MacroName,
135 const LangOptions &LangOpts,
136 bool PreferredTypeIsPointer = false);
138 /// \brief Determine the libclang cursor kind associated with the given
140 CXCursorKind getCursorKindForDecl(Decl *D);
144 class FunctionTemplateDecl;
145 class IdentifierInfo;
147 class NestedNameSpecifier;
150 /// \brief The context in which code completion occurred, so that the
151 /// code-completion consumer can process the results accordingly.
152 class CodeCompletionContext {
155 /// \brief An unspecified code-completion context.
157 /// \brief An unspecified code-completion context where we should also add
158 /// macro completions.
160 /// \brief Code completion occurred within a "top-level" completion context,
161 /// e.g., at namespace or global scope.
163 /// \brief Code completion occurred within an Objective-C interface,
164 /// protocol, or category interface.
166 /// \brief Code completion occurred within an Objective-C implementation
167 /// or category implementation.
168 CCC_ObjCImplementation,
169 /// \brief Code completion occurred within the instance variable list of
170 /// an Objective-C interface, implementation, or category implementation.
172 /// \brief Code completion occurred within a class, struct, or union.
173 CCC_ClassStructUnion,
174 /// \brief Code completion occurred where a statement (or declaration) is
175 /// expected in a function, method, or block.
177 /// \brief Code completion occurred where an expression is expected.
179 /// \brief Code completion occurred where an Objective-C message receiver
181 CCC_ObjCMessageReceiver,
182 /// \brief Code completion occurred on the right-hand side of a member
183 /// access expression using the dot operator.
185 /// The results of this completion are the members of the type being
186 /// accessed. The type itself is available via
187 /// \c CodeCompletionContext::getType().
189 /// \brief Code completion occurred on the right-hand side of a member
190 /// access expression using the arrow operator.
192 /// The results of this completion are the members of the type being
193 /// accessed. The type itself is available via
194 /// \c CodeCompletionContext::getType().
195 CCC_ArrowMemberAccess,
196 /// \brief Code completion occurred on the right-hand side of an Objective-C
197 /// property access expression.
199 /// The results of this completion are the members of the type being
200 /// accessed. The type itself is available via
201 /// \c CodeCompletionContext::getType().
202 CCC_ObjCPropertyAccess,
203 /// \brief Code completion occurred after the "enum" keyword, to indicate
204 /// an enumeration name.
206 /// \brief Code completion occurred after the "union" keyword, to indicate
209 /// \brief Code completion occurred after the "struct" or "class" keyword,
210 /// to indicate a struct or class name.
211 CCC_ClassOrStructTag,
212 /// \brief Code completion occurred where a protocol name is expected.
213 CCC_ObjCProtocolName,
214 /// \brief Code completion occurred where a namespace or namespace alias
217 /// \brief Code completion occurred where a type name is expected.
219 /// \brief Code completion occurred where a new name is expected.
221 /// \brief Code completion occurred where a new name is expected and a
222 /// qualified name is permissible.
223 CCC_PotentiallyQualifiedName,
224 /// \brief Code completion occurred where an macro is being defined.
226 /// \brief Code completion occurred where a macro name is expected
227 /// (without any arguments, in the case of a function-like macro).
229 /// \brief Code completion occurred within a preprocessor expression.
230 CCC_PreprocessorExpression,
231 /// \brief Code completion occurred where a preprocessor directive is
233 CCC_PreprocessorDirective,
234 /// \brief Code completion occurred in a context where natural language is
235 /// expected, e.g., a comment or string literal.
237 /// This context usually implies that no completions should be added,
238 /// unless they come from an appropriate natural-language dictionary.
240 /// \brief Code completion for a selector, as in an @selector expression.
242 /// \brief Code completion within a type-qualifier list.
244 /// \brief Code completion in a parenthesized expression, which means that
245 /// we may also have types here in C and Objective-C (as well as in C++).
246 CCC_ParenthesizedExpression,
247 /// \brief Code completion where an Objective-C instance message is expcted.
248 CCC_ObjCInstanceMessage,
249 /// \brief Code completion where an Objective-C class message is expected.
250 CCC_ObjCClassMessage,
251 /// \brief Code completion where the name of an Objective-C class is
253 CCC_ObjCInterfaceName,
254 /// \brief Code completion where an Objective-C category name is expected.
255 CCC_ObjCCategoryName,
256 /// \brief An unknown context, in which we are recovering from a parsing
257 /// error and don't know which completions we should give.
264 /// \brief The type that would prefer to see at this point (e.g., the type
265 /// of an initializer or function parameter).
266 QualType PreferredType;
268 /// \brief The type of the base object in a member access expression.
271 /// \brief The identifiers for Objective-C selector parts.
272 IdentifierInfo **SelIdents;
274 /// \brief The number of Objective-C selector parts.
275 unsigned NumSelIdents;
278 /// \brief Construct a new code-completion context of the given kind.
279 CodeCompletionContext(enum Kind Kind) : Kind(Kind), SelIdents(NULL),
282 /// \brief Construct a new code-completion context of the given kind.
283 CodeCompletionContext(enum Kind Kind, QualType T,
284 IdentifierInfo **SelIdents = NULL,
285 unsigned NumSelIdents = 0) : Kind(Kind),
286 SelIdents(SelIdents),
287 NumSelIdents(NumSelIdents) {
288 if (Kind == CCC_DotMemberAccess || Kind == CCC_ArrowMemberAccess ||
289 Kind == CCC_ObjCPropertyAccess || Kind == CCC_ObjCClassMessage ||
290 Kind == CCC_ObjCInstanceMessage)
296 /// \brief Retrieve the kind of code-completion context.
297 enum Kind getKind() const { return Kind; }
299 /// \brief Retrieve the type that this expression would prefer to have, e.g.,
300 /// if the expression is a variable initializer or a function argument, the
301 /// type of the corresponding variable or function parameter.
302 QualType getPreferredType() const { return PreferredType; }
304 /// \brief Retrieve the type of the base object in a member-access
306 QualType getBaseType() const { return BaseType; }
308 /// \brief Retrieve the Objective-C selector identifiers.
309 IdentifierInfo **getSelIdents() const { return SelIdents; }
311 /// \brief Retrieve the number of Objective-C selector identifiers.
312 unsigned getNumSelIdents() const { return NumSelIdents; }
314 /// \brief Determines whether we want C++ constructors as results within this
316 bool wantConstructorResults() const;
320 /// \brief A "string" used to describe how code completion can
321 /// be performed for an entity.
323 /// A code completion string typically shows how a particular entity can be
324 /// used. For example, the code completion string for a function would show
325 /// the syntax to call it, including the parentheses, placeholders for the
327 class CodeCompletionString {
329 /// \brief The different kinds of "chunks" that can occur within a code
330 /// completion string.
332 /// \brief The piece of text that the user is expected to type to
333 /// match the code-completion string, typically a keyword or the name of a
334 /// declarator or macro.
336 /// \brief A piece of text that should be placed in the buffer, e.g.,
337 /// parentheses or a comma in a function call.
339 /// \brief A code completion string that is entirely optional. For example,
340 /// an optional code completion string that describes the default arguments
341 /// in a function call.
343 /// \brief A string that acts as a placeholder for, e.g., a function
346 /// \brief A piece of text that describes something about the result but
347 /// should not be inserted into the buffer.
349 /// \brief A piece of text that describes the type of an entity or, for
350 /// functions and methods, the return type.
352 /// \brief A piece of text that describes the parameter that corresponds
353 /// to the code-completion location within a function call, message send,
354 /// macro invocation, etc.
356 /// \brief A left parenthesis ('(').
358 /// \brief A right parenthesis (')').
360 /// \brief A left bracket ('[').
362 /// \brief A right bracket (']').
364 /// \brief A left brace ('{').
366 /// \brief A right brace ('}').
368 /// \brief A left angle bracket ('<').
370 /// \brief A right angle bracket ('>').
372 /// \brief A comma separator (',').
374 /// \brief A colon (':').
376 /// \brief A semicolon (';').
378 /// \brief An '=' sign.
380 /// \brief Horizontal whitespace (' ').
382 /// \brief Verticle whitespace ('\n' or '\r\n', depending on the
387 /// \brief One piece of the code completion string.
389 /// \brief The kind of data stored in this piece of the code completion
394 /// \brief The text string associated with a CK_Text, CK_Placeholder,
395 /// CK_Informative, or CK_Comma chunk.
396 /// The string is owned by the chunk and will be deallocated
397 /// (with delete[]) when the chunk is destroyed.
400 /// \brief The code completion string associated with a CK_Optional chunk.
401 /// The optional code completion string is owned by the chunk, and will
402 /// be deallocated (with delete) when the chunk is destroyed.
403 CodeCompletionString *Optional;
406 Chunk() : Kind(CK_Text), Text(0) { }
408 explicit Chunk(ChunkKind Kind, const char *Text = "");
410 /// \brief Create a new text chunk.
411 static Chunk CreateText(const char *Text);
413 /// \brief Create a new optional chunk.
414 static Chunk CreateOptional(CodeCompletionString *Optional);
416 /// \brief Create a new placeholder chunk.
417 static Chunk CreatePlaceholder(const char *Placeholder);
419 /// \brief Create a new informative chunk.
420 static Chunk CreateInformative(const char *Informative);
422 /// \brief Create a new result type chunk.
423 static Chunk CreateResultType(const char *ResultType);
425 /// \brief Create a new current-parameter chunk.
426 static Chunk CreateCurrentParameter(const char *CurrentParameter);
430 /// \brief The number of chunks stored in this string.
431 unsigned NumChunks : 16;
433 /// \brief The number of annotations for this code-completion result.
434 unsigned NumAnnotations : 16;
436 /// \brief The priority of this code-completion string.
437 unsigned Priority : 16;
439 /// \brief The availability of this code-completion result.
440 unsigned Availability : 2;
442 /// \brief The kind of the parent context.
443 unsigned ParentKind : 14;
445 /// \brief The name of the parent context.
446 StringRef ParentName;
448 CodeCompletionString(const CodeCompletionString &); // DO NOT IMPLEMENT
449 CodeCompletionString &operator=(const CodeCompletionString &); // DITTO
451 CodeCompletionString(const Chunk *Chunks, unsigned NumChunks,
452 unsigned Priority, CXAvailabilityKind Availability,
453 const char **Annotations, unsigned NumAnnotations,
454 CXCursorKind ParentKind, StringRef ParentName);
455 ~CodeCompletionString() { }
457 friend class CodeCompletionBuilder;
458 friend class CodeCompletionResult;
461 typedef const Chunk *iterator;
462 iterator begin() const { return reinterpret_cast<const Chunk *>(this + 1); }
463 iterator end() const { return begin() + NumChunks; }
464 bool empty() const { return NumChunks == 0; }
465 unsigned size() const { return NumChunks; }
467 const Chunk &operator[](unsigned I) const {
468 assert(I < size() && "Chunk index out-of-range");
472 /// \brief Returns the text in the TypedText chunk.
473 const char *getTypedText() const;
475 /// \brief Retrieve the priority of this code completion result.
476 unsigned getPriority() const { return Priority; }
478 /// \brief Retrieve the availability of this code completion result.
479 unsigned getAvailability() const { return Availability; }
481 /// \brief Retrieve the number of annotations for this code completion result.
482 unsigned getAnnotationCount() const;
484 /// \brief Retrieve the annotation string specified by \c AnnotationNr.
485 const char *getAnnotation(unsigned AnnotationNr) const;
487 /// \brief Retrieve parent context's cursor kind.
488 CXCursorKind getParentContextKind() const {
489 return (CXCursorKind)ParentKind;
492 /// \brief Retrieve the name of the parent context.
493 StringRef getParentContextName() const {
497 /// \brief Retrieve a string representation of the code completion string,
498 /// which is mainly useful for debugging.
499 std::string getAsString() const;
502 /// \brief An allocator used specifically for the purpose of code completion.
503 class CodeCompletionAllocator : public llvm::BumpPtrAllocator {
505 /// \brief Copy the given string into this allocator.
506 const char *CopyString(StringRef String);
508 /// \brief Copy the given string into this allocator.
509 const char *CopyString(Twine String);
511 // \brief Copy the given string into this allocator.
512 const char *CopyString(const char *String) {
513 return CopyString(StringRef(String));
516 /// \brief Copy the given string into this allocator.
517 const char *CopyString(const std::string &String) {
518 return CopyString(StringRef(String));
522 /// \brief Allocator for a cached set of global code completions.
523 class GlobalCodeCompletionAllocator
524 : public CodeCompletionAllocator,
525 public RefCountedBase<GlobalCodeCompletionAllocator>
530 class CodeCompletionTUInfo {
531 llvm::DenseMap<DeclContext *, StringRef> ParentNames;
532 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> AllocatorRef;
535 explicit CodeCompletionTUInfo(
536 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> Allocator)
537 : AllocatorRef(Allocator) { }
539 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> getAllocatorRef() const {
542 CodeCompletionAllocator &getAllocator() const {
543 assert(AllocatorRef);
544 return *AllocatorRef;
547 StringRef getParentName(DeclContext *DC);
550 } // end namespace clang
553 template <> struct isPodLike<clang::CodeCompletionString::Chunk> {
554 static const bool value = true;
560 /// \brief A builder class used to construct new code-completion strings.
561 class CodeCompletionBuilder {
563 typedef CodeCompletionString::Chunk Chunk;
566 CodeCompletionAllocator &Allocator;
567 CodeCompletionTUInfo &CCTUInfo;
569 CXAvailabilityKind Availability;
570 CXCursorKind ParentKind;
571 StringRef ParentName;
573 /// \brief The chunks stored in this string.
574 SmallVector<Chunk, 4> Chunks;
576 SmallVector<const char *, 2> Annotations;
579 CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
580 CodeCompletionTUInfo &CCTUInfo)
581 : Allocator(Allocator), CCTUInfo(CCTUInfo),
582 Priority(0), Availability(CXAvailability_Available),
583 ParentKind(CXCursor_NotImplemented) { }
585 CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
586 CodeCompletionTUInfo &CCTUInfo,
587 unsigned Priority, CXAvailabilityKind Availability)
588 : Allocator(Allocator), CCTUInfo(CCTUInfo),
589 Priority(Priority), Availability(Availability),
590 ParentKind(CXCursor_NotImplemented) { }
592 /// \brief Retrieve the allocator into which the code completion
593 /// strings should be allocated.
594 CodeCompletionAllocator &getAllocator() const { return Allocator; }
596 CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
598 /// \brief Take the resulting completion string.
600 /// This operation can only be performed once.
601 CodeCompletionString *TakeString();
603 /// \brief Add a new typed-text chunk.
604 void AddTypedTextChunk(const char *Text);
606 /// \brief Add a new text chunk.
607 void AddTextChunk(const char *Text);
609 /// \brief Add a new optional chunk.
610 void AddOptionalChunk(CodeCompletionString *Optional);
612 /// \brief Add a new placeholder chunk.
613 void AddPlaceholderChunk(const char *Placeholder);
615 /// \brief Add a new informative chunk.
616 void AddInformativeChunk(const char *Text);
618 /// \brief Add a new result-type chunk.
619 void AddResultTypeChunk(const char *ResultType);
621 /// \brief Add a new current-parameter chunk.
622 void AddCurrentParameterChunk(const char *CurrentParameter);
624 /// \brief Add a new chunk.
625 void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text = "");
627 void AddAnnotation(const char *A) { Annotations.push_back(A); }
629 /// \brief Add the parent context information to this code completion.
630 void addParentContext(DeclContext *DC);
632 CXCursorKind getParentKind() const { return ParentKind; }
633 StringRef getParentName() const { return ParentName; }
636 /// \brief Captures a result of code completion.
637 class CodeCompletionResult {
639 /// \brief Describes the kind of result generated.
641 RK_Declaration = 0, //< Refers to a declaration
642 RK_Keyword, //< Refers to a keyword or symbol.
643 RK_Macro, //< Refers to a macro
644 RK_Pattern //< Refers to a precomputed pattern.
647 /// \brief The kind of result stored here.
650 /// \brief When Kind == RK_Declaration or RK_Pattern, the declaration we are
651 /// referring to. In the latter case, the declaration might be NULL.
652 NamedDecl *Declaration;
655 /// \brief When Kind == RK_Keyword, the string representing the keyword
656 /// or symbol's spelling.
659 /// \brief When Kind == RK_Pattern, the code-completion string that
660 /// describes the completion text to insert.
661 CodeCompletionString *Pattern;
663 /// \brief When Kind == RK_Macro, the identifier that refers to a macro.
664 IdentifierInfo *Macro;
667 /// \brief The priority of this particular code-completion result.
670 /// \brief The cursor kind that describes this result.
671 CXCursorKind CursorKind;
673 /// \brief The availability of this result.
674 CXAvailabilityKind Availability;
676 /// \brief Specifies which parameter (of a function, Objective-C method,
677 /// macro, etc.) we should start with when formatting the result.
678 unsigned StartParameter;
680 /// \brief Whether this result is hidden by another name.
683 /// \brief Whether this result was found via lookup into a base class.
684 bool QualifierIsInformative : 1;
686 /// \brief Whether this declaration is the beginning of a
687 /// nested-name-specifier and, therefore, should be followed by '::'.
688 bool StartsNestedNameSpecifier : 1;
690 /// \brief Whether all parameters (of a function, Objective-C
691 /// method, etc.) should be considered "informative".
692 bool AllParametersAreInformative : 1;
694 /// \brief Whether we're completing a declaration of the given entity,
695 /// rather than a use of that entity.
696 bool DeclaringEntity : 1;
698 /// \brief If the result should have a nested-name-specifier, this is it.
699 /// When \c QualifierIsInformative, the nested-name-specifier is
700 /// informative rather than required.
701 NestedNameSpecifier *Qualifier;
703 /// \brief Build a result that refers to a declaration.
704 CodeCompletionResult(NamedDecl *Declaration,
705 NestedNameSpecifier *Qualifier = 0,
706 bool QualifierIsInformative = false,
707 bool Accessible = true)
708 : Kind(RK_Declaration), Declaration(Declaration),
709 Priority(getPriorityFromDecl(Declaration)),
710 Availability(CXAvailability_Available), StartParameter(0),
711 Hidden(false), QualifierIsInformative(QualifierIsInformative),
712 StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
713 DeclaringEntity(false), Qualifier(Qualifier) {
714 computeCursorKindAndAvailability(Accessible);
717 /// \brief Build a result that refers to a keyword or symbol.
718 CodeCompletionResult(const char *Keyword, unsigned Priority = CCP_Keyword)
719 : Kind(RK_Keyword), Declaration(0), Keyword(Keyword), Priority(Priority),
720 Availability(CXAvailability_Available),
721 StartParameter(0), Hidden(false), QualifierIsInformative(0),
722 StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
723 DeclaringEntity(false), Qualifier(0) {
724 computeCursorKindAndAvailability();
727 /// \brief Build a result that refers to a macro.
728 CodeCompletionResult(IdentifierInfo *Macro, unsigned Priority = CCP_Macro)
729 : Kind(RK_Macro), Declaration(0), Macro(Macro), Priority(Priority),
730 Availability(CXAvailability_Available), StartParameter(0),
731 Hidden(false), QualifierIsInformative(0),
732 StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
733 DeclaringEntity(false), Qualifier(0) {
734 computeCursorKindAndAvailability();
737 /// \brief Build a result that refers to a pattern.
738 CodeCompletionResult(CodeCompletionString *Pattern,
739 unsigned Priority = CCP_CodePattern,
740 CXCursorKind CursorKind = CXCursor_NotImplemented,
741 CXAvailabilityKind Availability = CXAvailability_Available,
743 : Kind(RK_Pattern), Declaration(D), Pattern(Pattern), Priority(Priority),
744 CursorKind(CursorKind), Availability(Availability), StartParameter(0),
745 Hidden(false), QualifierIsInformative(0),
746 StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
747 DeclaringEntity(false), Qualifier(0)
751 /// \brief Build a result that refers to a pattern with an associated
753 CodeCompletionResult(CodeCompletionString *Pattern, NamedDecl *D,
755 : Kind(RK_Pattern), Declaration(D), Pattern(Pattern), Priority(Priority),
756 Availability(CXAvailability_Available), StartParameter(0),
757 Hidden(false), QualifierIsInformative(false),
758 StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
759 DeclaringEntity(false), Qualifier(0) {
760 computeCursorKindAndAvailability();
763 /// \brief Retrieve the declaration stored in this result.
764 NamedDecl *getDeclaration() const {
765 assert(Kind == RK_Declaration && "Not a declaration result");
769 /// \brief Retrieve the keyword stored in this result.
770 const char *getKeyword() const {
771 assert(Kind == RK_Keyword && "Not a keyword result");
775 /// \brief Create a new code-completion string that describes how to insert
776 /// this result into a program.
778 /// \param S The semantic analysis that created the result.
780 /// \param Allocator The allocator that will be used to allocate the
782 CodeCompletionString *CreateCodeCompletionString(Sema &S,
783 CodeCompletionAllocator &Allocator,
784 CodeCompletionTUInfo &CCTUInfo);
785 CodeCompletionString *CreateCodeCompletionString(ASTContext &Ctx,
787 CodeCompletionAllocator &Allocator,
788 CodeCompletionTUInfo &CCTUInfo);
790 /// \brief Determine a base priority for the given declaration.
791 static unsigned getPriorityFromDecl(NamedDecl *ND);
794 void computeCursorKindAndAvailability(bool Accessible = true);
797 bool operator<(const CodeCompletionResult &X, const CodeCompletionResult &Y);
799 inline bool operator>(const CodeCompletionResult &X,
800 const CodeCompletionResult &Y) {
804 inline bool operator<=(const CodeCompletionResult &X,
805 const CodeCompletionResult &Y) {
809 inline bool operator>=(const CodeCompletionResult &X,
810 const CodeCompletionResult &Y) {
815 raw_ostream &operator<<(raw_ostream &OS,
816 const CodeCompletionString &CCS);
818 /// \brief Abstract interface for a consumer of code-completion
820 class CodeCompleteConsumer {
822 /// \brief Whether to include macros in the code-completion results.
825 /// \brief Whether to include code patterns (such as for loops) within
826 /// the completion results.
827 bool IncludeCodePatterns;
829 /// \brief Whether to include global (top-level) declarations and names in
830 /// the completion results.
833 /// \brief Whether the output format for the code-completion consumer is
838 class OverloadCandidate {
840 /// \brief Describes the type of overload candidate.
842 /// \brief The candidate is a function declaration.
844 /// \brief The candidate is a function template.
846 /// \brief The "candidate" is actually a variable, expression, or block
847 /// for which we only have a function prototype.
852 /// \brief The kind of overload candidate.
856 /// \brief The function overload candidate, available when
857 /// Kind == CK_Function.
858 FunctionDecl *Function;
860 /// \brief The function template overload candidate, available when
861 /// Kind == CK_FunctionTemplate.
862 FunctionTemplateDecl *FunctionTemplate;
864 /// \brief The function type that describes the entity being called,
865 /// when Kind == CK_FunctionType.
866 const FunctionType *Type;
870 OverloadCandidate(FunctionDecl *Function)
871 : Kind(CK_Function), Function(Function) { }
873 OverloadCandidate(FunctionTemplateDecl *FunctionTemplateDecl)
874 : Kind(CK_FunctionTemplate), FunctionTemplate(FunctionTemplateDecl) { }
876 OverloadCandidate(const FunctionType *Type)
877 : Kind(CK_FunctionType), Type(Type) { }
879 /// \brief Determine the kind of overload candidate.
880 CandidateKind getKind() const { return Kind; }
882 /// \brief Retrieve the function overload candidate or the templated
883 /// function declaration for a function template.
884 FunctionDecl *getFunction() const;
886 /// \brief Retrieve the function template overload candidate.
887 FunctionTemplateDecl *getFunctionTemplate() const {
888 assert(getKind() == CK_FunctionTemplate && "Not a function template");
889 return FunctionTemplate;
892 /// \brief Retrieve the function type of the entity, regardless of how the
893 /// function is stored.
894 const FunctionType *getFunctionType() const;
896 /// \brief Create a new code-completion string that describes the function
897 /// signature of this overload candidate.
898 CodeCompletionString *CreateSignatureString(unsigned CurrentArg,
900 CodeCompletionAllocator &Allocator,
901 CodeCompletionTUInfo &CCTUInfo) const;
904 CodeCompleteConsumer() : IncludeMacros(false), IncludeCodePatterns(false),
905 IncludeGlobals(true), OutputIsBinary(false) { }
907 CodeCompleteConsumer(bool IncludeMacros, bool IncludeCodePatterns,
908 bool IncludeGlobals, bool OutputIsBinary)
909 : IncludeMacros(IncludeMacros), IncludeCodePatterns(IncludeCodePatterns),
910 IncludeGlobals(IncludeGlobals), OutputIsBinary(OutputIsBinary) { }
912 /// \brief Whether the code-completion consumer wants to see macros.
913 bool includeMacros() const { return IncludeMacros; }
915 /// \brief Whether the code-completion consumer wants to see code patterns.
916 bool includeCodePatterns() const { return IncludeCodePatterns; }
918 /// \brief Whether to include global (top-level) declaration results.
919 bool includeGlobals() const { return IncludeGlobals; }
921 /// \brief Determine whether the output of this consumer is binary.
922 bool isOutputBinary() const { return OutputIsBinary; }
924 /// \brief Deregisters and destroys this code-completion consumer.
925 virtual ~CodeCompleteConsumer();
927 /// \name Code-completion callbacks
929 /// \brief Process the finalized code-completion results.
930 virtual void ProcessCodeCompleteResults(Sema &S,
931 CodeCompletionContext Context,
932 CodeCompletionResult *Results,
933 unsigned NumResults) { }
935 /// \param S the semantic-analyzer object for which code-completion is being
938 /// \param CurrentArg the index of the current argument.
940 /// \param Candidates an array of overload candidates.
942 /// \param NumCandidates the number of overload candidates
943 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
944 OverloadCandidate *Candidates,
945 unsigned NumCandidates) { }
948 /// \brief Retrieve the allocator that will be used to allocate
949 /// code completion strings.
950 virtual CodeCompletionAllocator &getAllocator() = 0;
952 virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() = 0;
955 /// \brief A simple code-completion consumer that prints the results it
956 /// receives in a simple format.
957 class PrintingCodeCompleteConsumer : public CodeCompleteConsumer {
958 /// \brief The raw output stream.
961 CodeCompletionTUInfo CCTUInfo;
964 /// \brief Create a new printing code-completion consumer that prints its
965 /// results to the given raw output stream.
966 PrintingCodeCompleteConsumer(bool IncludeMacros, bool IncludeCodePatterns,
969 : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals,
971 CCTUInfo(new GlobalCodeCompletionAllocator) {}
973 /// \brief Prints the finalized code-completion results.
974 virtual void ProcessCodeCompleteResults(Sema &S,
975 CodeCompletionContext Context,
976 CodeCompletionResult *Results,
977 unsigned NumResults);
979 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
980 OverloadCandidate *Candidates,
981 unsigned NumCandidates);
983 virtual CodeCompletionAllocator &getAllocator() {
984 return CCTUInfo.getAllocator();
987 virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() { return CCTUInfo; }
990 } // end namespace clang
992 #endif // LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H