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-c/Index.h"
17 #include "clang/AST/CanonicalType.h"
18 #include "clang/AST/Type.h"
19 #include "clang/Sema/CodeCompleteOptions.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/Allocator.h"
29 /// \brief Default priority values for code-completion results based
32 /// \brief Priority for the next initialization in a constructor initializer
34 CCP_NextInitializer = 7,
35 /// \brief Priority for an enumeration constant inside a switch whose
36 /// condition is of the enumeration type.
38 /// \brief Priority for a send-to-super completion.
39 CCP_SuperCompletion = 20,
40 /// \brief Priority for a declaration that is in the local scope.
41 CCP_LocalDeclaration = 34,
42 /// \brief Priority for a member declaration found from the current
43 /// method or member function.
44 CCP_MemberDeclaration = 35,
45 /// \brief Priority for a language keyword (that isn't any of the other
48 /// \brief Priority for a code pattern.
50 /// \brief Priority for a non-type declaration.
52 /// \brief Priority for a type.
53 CCP_Type = CCP_Declaration,
54 /// \brief Priority for a constant value (e.g., enumerator).
56 /// \brief Priority for a preprocessor macro.
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.
64 /// \brief Priority for the Objective-C "_cmd" implicit parameter.
65 CCP_ObjC_cmd = CCP_Unlikely
68 /// \brief Priority value deltas that are added to code-completion results
69 /// based on the context of the result.
71 /// \brief The result is in a base class.
73 /// \brief The result is a C++ non-static member function whose qualifiers
74 /// exactly match the object type on which the member function can be called.
75 CCD_ObjectQualifierMatch = -1,
76 /// \brief The selector of the given message exactly matches the selector
77 /// of the current method, which might imply that some kind of delegation
79 CCD_SelectorMatch = -3,
81 /// \brief Adjustment to the "bool" type in Objective-C, where the typedef
82 /// "BOOL" is preferred.
85 /// \brief Adjustment for KVC code pattern priorities when it doesn't look
87 CCD_ProbablyNotObjCCollection = 15,
89 /// \brief An Objective-C method being used as a property.
90 CCD_MethodAsProperty = 2
93 /// \brief Priority value factors by which we will divide or multiply the
94 /// priority of a code-completion result.
96 /// \brief Divide by this factor when a code-completion result's type exactly
97 /// matches the type we expect.
98 CCF_ExactTypeMatch = 4,
99 /// \brief Divide by this factor when a code-completion result's type is
100 /// similar to the type we expect (e.g., both arithmetic types, both
101 /// Objective-C object pointer types).
102 CCF_SimilarTypeMatch = 2
105 /// \brief A simplified classification of types used when determining
106 /// "similar" types for code completion.
107 enum SimplifiedTypeClass {
119 /// \brief Determine the simplified type class of the given canonical type.
120 SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T);
122 /// \brief Determine the type that this declaration will have if it is used
123 /// as a type or in an expression.
124 QualType getDeclUsageType(ASTContext &C, const NamedDecl *ND);
126 /// \brief Determine the priority to be given to a macro code completion result
127 /// with the given name.
129 /// \param MacroName The name of the macro.
131 /// \param LangOpts Options describing the current language dialect.
133 /// \param PreferredTypeIsPointer Whether the preferred type for the context
134 /// of this macro is a pointer type.
135 unsigned getMacroUsagePriority(StringRef MacroName,
136 const LangOptions &LangOpts,
137 bool PreferredTypeIsPointer = false);
139 /// \brief Determine the libclang cursor kind associated with the given
141 CXCursorKind getCursorKindForDecl(const Decl *D);
145 class FunctionTemplateDecl;
146 class IdentifierInfo;
148 class NestedNameSpecifier;
151 /// \brief The context in which code completion occurred, so that the
152 /// code-completion consumer can process the results accordingly.
153 class CodeCompletionContext {
156 /// \brief An unspecified code-completion context.
158 /// \brief An unspecified code-completion context where we should also add
159 /// macro completions.
161 /// \brief Code completion occurred within a "top-level" completion context,
162 /// e.g., at namespace or global scope.
164 /// \brief Code completion occurred within an Objective-C interface,
165 /// protocol, or category interface.
167 /// \brief Code completion occurred within an Objective-C implementation
168 /// or category implementation.
169 CCC_ObjCImplementation,
170 /// \brief Code completion occurred within the instance variable list of
171 /// an Objective-C interface, implementation, or category implementation.
173 /// \brief Code completion occurred within a class, struct, or union.
174 CCC_ClassStructUnion,
175 /// \brief Code completion occurred where a statement (or declaration) is
176 /// expected in a function, method, or block.
178 /// \brief Code completion occurred where an expression is expected.
180 /// \brief Code completion occurred where an Objective-C message receiver
182 CCC_ObjCMessageReceiver,
183 /// \brief Code completion occurred on the right-hand side of a member
184 /// access expression using the dot operator.
186 /// The results of this completion are the members of the type being
187 /// accessed. The type itself is available via
188 /// \c CodeCompletionContext::getType().
190 /// \brief Code completion occurred on the right-hand side of a member
191 /// access expression using the arrow operator.
193 /// The results of this completion are the members of the type being
194 /// accessed. The type itself is available via
195 /// \c CodeCompletionContext::getType().
196 CCC_ArrowMemberAccess,
197 /// \brief Code completion occurred on the right-hand side of an Objective-C
198 /// property access expression.
200 /// The results of this completion are the members of the type being
201 /// accessed. The type itself is available via
202 /// \c CodeCompletionContext::getType().
203 CCC_ObjCPropertyAccess,
204 /// \brief Code completion occurred after the "enum" keyword, to indicate
205 /// an enumeration name.
207 /// \brief Code completion occurred after the "union" keyword, to indicate
210 /// \brief Code completion occurred after the "struct" or "class" keyword,
211 /// to indicate a struct or class name.
212 CCC_ClassOrStructTag,
213 /// \brief Code completion occurred where a protocol name is expected.
214 CCC_ObjCProtocolName,
215 /// \brief Code completion occurred where a namespace or namespace alias
218 /// \brief Code completion occurred where a type name is expected.
220 /// \brief Code completion occurred where a new name is expected.
222 /// \brief Code completion occurred where a new name is expected and a
223 /// qualified name is permissible.
224 CCC_PotentiallyQualifiedName,
225 /// \brief Code completion occurred where an macro is being defined.
227 /// \brief Code completion occurred where a macro name is expected
228 /// (without any arguments, in the case of a function-like macro).
230 /// \brief Code completion occurred within a preprocessor expression.
231 CCC_PreprocessorExpression,
232 /// \brief Code completion occurred where a preprocessor directive is
234 CCC_PreprocessorDirective,
235 /// \brief Code completion occurred in a context where natural language is
236 /// expected, e.g., a comment or string literal.
238 /// This context usually implies that no completions should be added,
239 /// unless they come from an appropriate natural-language dictionary.
241 /// \brief Code completion for a selector, as in an \@selector expression.
243 /// \brief Code completion within a type-qualifier list.
245 /// \brief Code completion in a parenthesized expression, which means that
246 /// we may also have types here in C and Objective-C (as well as in C++).
247 CCC_ParenthesizedExpression,
248 /// \brief Code completion where an Objective-C instance message is
250 CCC_ObjCInstanceMessage,
251 /// \brief Code completion where an Objective-C class message is expected.
252 CCC_ObjCClassMessage,
253 /// \brief Code completion where the name of an Objective-C class is
255 CCC_ObjCInterfaceName,
256 /// \brief Code completion where an Objective-C category name is expected.
257 CCC_ObjCCategoryName,
258 /// \brief An unknown context, in which we are recovering from a parsing
259 /// error and don't know which completions we should give.
266 /// \brief The type that would prefer to see at this point (e.g., the type
267 /// of an initializer or function parameter).
268 QualType PreferredType;
270 /// \brief The type of the base object in a member access expression.
273 /// \brief The identifiers for Objective-C selector parts.
274 ArrayRef<IdentifierInfo *> SelIdents;
277 /// \brief Construct a new code-completion context of the given kind.
278 CodeCompletionContext(enum Kind Kind) : Kind(Kind), SelIdents(None) { }
280 /// \brief Construct a new code-completion context of the given kind.
281 CodeCompletionContext(enum Kind Kind, QualType T,
282 ArrayRef<IdentifierInfo *> SelIdents = None)
284 SelIdents(SelIdents) {
285 if (Kind == CCC_DotMemberAccess || Kind == CCC_ArrowMemberAccess ||
286 Kind == CCC_ObjCPropertyAccess || Kind == CCC_ObjCClassMessage ||
287 Kind == CCC_ObjCInstanceMessage)
293 /// \brief Retrieve the kind of code-completion context.
294 enum Kind getKind() const { return Kind; }
296 /// \brief Retrieve the type that this expression would prefer to have, e.g.,
297 /// if the expression is a variable initializer or a function argument, the
298 /// type of the corresponding variable or function parameter.
299 QualType getPreferredType() const { return PreferredType; }
301 /// \brief Retrieve the type of the base object in a member-access
303 QualType getBaseType() const { return BaseType; }
305 /// \brief Retrieve the Objective-C selector identifiers.
306 ArrayRef<IdentifierInfo *> getSelIdents() const { return SelIdents; }
308 /// \brief Determines whether we want C++ constructors as results within this
310 bool wantConstructorResults() const;
314 /// \brief A "string" used to describe how code completion can
315 /// be performed for an entity.
317 /// A code completion string typically shows how a particular entity can be
318 /// used. For example, the code completion string for a function would show
319 /// the syntax to call it, including the parentheses, placeholders for the
321 class CodeCompletionString {
323 /// \brief The different kinds of "chunks" that can occur within a code
324 /// completion string.
326 /// \brief The piece of text that the user is expected to type to
327 /// match the code-completion string, typically a keyword or the name of a
328 /// declarator or macro.
330 /// \brief A piece of text that should be placed in the buffer, e.g.,
331 /// parentheses or a comma in a function call.
333 /// \brief A code completion string that is entirely optional. For example,
334 /// an optional code completion string that describes the default arguments
335 /// in a function call.
337 /// \brief A string that acts as a placeholder for, e.g., a function
340 /// \brief A piece of text that describes something about the result but
341 /// should not be inserted into the buffer.
343 /// \brief A piece of text that describes the type of an entity or, for
344 /// functions and methods, the return type.
346 /// \brief A piece of text that describes the parameter that corresponds
347 /// to the code-completion location within a function call, message send,
348 /// macro invocation, etc.
350 /// \brief A left parenthesis ('(').
352 /// \brief A right parenthesis (')').
354 /// \brief A left bracket ('[').
356 /// \brief A right bracket (']').
358 /// \brief A left brace ('{').
360 /// \brief A right brace ('}').
362 /// \brief A left angle bracket ('<').
364 /// \brief A right angle bracket ('>').
366 /// \brief A comma separator (',').
368 /// \brief A colon (':').
370 /// \brief A semicolon (';').
372 /// \brief An '=' sign.
374 /// \brief Horizontal whitespace (' ').
376 /// \brief Vertical whitespace ('\\n' or '\\r\\n', depending on the
381 /// \brief One piece of the code completion string.
383 /// \brief The kind of data stored in this piece of the code completion
388 /// \brief The text string associated with a CK_Text, CK_Placeholder,
389 /// CK_Informative, or CK_Comma chunk.
390 /// The string is owned by the chunk and will be deallocated
391 /// (with delete[]) when the chunk is destroyed.
394 /// \brief The code completion string associated with a CK_Optional chunk.
395 /// The optional code completion string is owned by the chunk, and will
396 /// be deallocated (with delete) when the chunk is destroyed.
397 CodeCompletionString *Optional;
400 Chunk() : Kind(CK_Text), Text(0) { }
402 explicit Chunk(ChunkKind Kind, const char *Text = "");
404 /// \brief Create a new text chunk.
405 static Chunk CreateText(const char *Text);
407 /// \brief Create a new optional chunk.
408 static Chunk CreateOptional(CodeCompletionString *Optional);
410 /// \brief Create a new placeholder chunk.
411 static Chunk CreatePlaceholder(const char *Placeholder);
413 /// \brief Create a new informative chunk.
414 static Chunk CreateInformative(const char *Informative);
416 /// \brief Create a new result type chunk.
417 static Chunk CreateResultType(const char *ResultType);
419 /// \brief Create a new current-parameter chunk.
420 static Chunk CreateCurrentParameter(const char *CurrentParameter);
424 /// \brief The number of chunks stored in this string.
425 unsigned NumChunks : 16;
427 /// \brief The number of annotations for this code-completion result.
428 unsigned NumAnnotations : 16;
430 /// \brief The priority of this code-completion string.
431 unsigned Priority : 16;
433 /// \brief The availability of this code-completion result.
434 unsigned Availability : 2;
436 /// \brief The name of the parent context.
437 StringRef ParentName;
439 /// \brief A brief documentation comment attached to the declaration of
440 /// entity being completed by this result.
441 const char *BriefComment;
443 CodeCompletionString(const CodeCompletionString &) LLVM_DELETED_FUNCTION;
444 void operator=(const CodeCompletionString &) LLVM_DELETED_FUNCTION;
446 CodeCompletionString(const Chunk *Chunks, unsigned NumChunks,
447 unsigned Priority, CXAvailabilityKind Availability,
448 const char **Annotations, unsigned NumAnnotations,
449 StringRef ParentName,
450 const char *BriefComment);
451 ~CodeCompletionString() { }
453 friend class CodeCompletionBuilder;
454 friend class CodeCompletionResult;
457 typedef const Chunk *iterator;
458 iterator begin() const { return reinterpret_cast<const Chunk *>(this + 1); }
459 iterator end() const { return begin() + NumChunks; }
460 bool empty() const { return NumChunks == 0; }
461 unsigned size() const { return NumChunks; }
463 const Chunk &operator[](unsigned I) const {
464 assert(I < size() && "Chunk index out-of-range");
468 /// \brief Returns the text in the TypedText chunk.
469 const char *getTypedText() const;
471 /// \brief Retrieve the priority of this code completion result.
472 unsigned getPriority() const { return Priority; }
474 /// \brief Retrieve the availability of this code completion result.
475 unsigned getAvailability() const { return Availability; }
477 /// \brief Retrieve the number of annotations for this code completion result.
478 unsigned getAnnotationCount() const;
480 /// \brief Retrieve the annotation string specified by \c AnnotationNr.
481 const char *getAnnotation(unsigned AnnotationNr) const;
483 /// \brief Retrieve the name of the parent context.
484 StringRef getParentContextName() const {
488 const char *getBriefComment() const {
492 /// \brief Retrieve a string representation of the code completion string,
493 /// which is mainly useful for debugging.
494 std::string getAsString() const;
497 /// \brief An allocator used specifically for the purpose of code completion.
498 class CodeCompletionAllocator : public llvm::BumpPtrAllocator {
500 /// \brief Copy the given string into this allocator.
501 const char *CopyString(StringRef String);
503 /// \brief Copy the given string into this allocator.
504 const char *CopyString(Twine String);
506 // \brief Copy the given string into this allocator.
507 const char *CopyString(const char *String) {
508 return CopyString(StringRef(String));
511 /// \brief Copy the given string into this allocator.
512 const char *CopyString(const std::string &String) {
513 return CopyString(StringRef(String));
517 /// \brief Allocator for a cached set of global code completions.
518 class GlobalCodeCompletionAllocator
519 : public CodeCompletionAllocator,
520 public RefCountedBase<GlobalCodeCompletionAllocator>
525 class CodeCompletionTUInfo {
526 llvm::DenseMap<const DeclContext *, StringRef> ParentNames;
527 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> AllocatorRef;
530 explicit CodeCompletionTUInfo(
531 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> Allocator)
532 : AllocatorRef(Allocator) { }
534 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> getAllocatorRef() const {
537 CodeCompletionAllocator &getAllocator() const {
538 assert(AllocatorRef);
539 return *AllocatorRef;
542 StringRef getParentName(const DeclContext *DC);
545 } // end namespace clang
548 template <> struct isPodLike<clang::CodeCompletionString::Chunk> {
549 static const bool value = true;
555 /// \brief A builder class used to construct new code-completion strings.
556 class CodeCompletionBuilder {
558 typedef CodeCompletionString::Chunk Chunk;
561 CodeCompletionAllocator &Allocator;
562 CodeCompletionTUInfo &CCTUInfo;
564 CXAvailabilityKind Availability;
565 StringRef ParentName;
566 const char *BriefComment;
568 /// \brief The chunks stored in this string.
569 SmallVector<Chunk, 4> Chunks;
571 SmallVector<const char *, 2> Annotations;
574 CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
575 CodeCompletionTUInfo &CCTUInfo)
576 : Allocator(Allocator), CCTUInfo(CCTUInfo),
577 Priority(0), Availability(CXAvailability_Available),
578 BriefComment(NULL) { }
580 CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
581 CodeCompletionTUInfo &CCTUInfo,
582 unsigned Priority, CXAvailabilityKind Availability)
583 : Allocator(Allocator), CCTUInfo(CCTUInfo),
584 Priority(Priority), Availability(Availability),
585 BriefComment(NULL) { }
587 /// \brief Retrieve the allocator into which the code completion
588 /// strings should be allocated.
589 CodeCompletionAllocator &getAllocator() const { return Allocator; }
591 CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
593 /// \brief Take the resulting completion string.
595 /// This operation can only be performed once.
596 CodeCompletionString *TakeString();
598 /// \brief Add a new typed-text chunk.
599 void AddTypedTextChunk(const char *Text);
601 /// \brief Add a new text chunk.
602 void AddTextChunk(const char *Text);
604 /// \brief Add a new optional chunk.
605 void AddOptionalChunk(CodeCompletionString *Optional);
607 /// \brief Add a new placeholder chunk.
608 void AddPlaceholderChunk(const char *Placeholder);
610 /// \brief Add a new informative chunk.
611 void AddInformativeChunk(const char *Text);
613 /// \brief Add a new result-type chunk.
614 void AddResultTypeChunk(const char *ResultType);
616 /// \brief Add a new current-parameter chunk.
617 void AddCurrentParameterChunk(const char *CurrentParameter);
619 /// \brief Add a new chunk.
620 void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text = "");
622 void AddAnnotation(const char *A) { Annotations.push_back(A); }
624 /// \brief Add the parent context information to this code completion.
625 void addParentContext(const DeclContext *DC);
627 const char *getBriefComment() const { return BriefComment; }
628 void addBriefComment(StringRef Comment);
630 StringRef getParentName() const { return ParentName; }
633 /// \brief Captures a result of code completion.
634 class CodeCompletionResult {
636 /// \brief Describes the kind of result generated.
638 RK_Declaration = 0, ///< Refers to a declaration
639 RK_Keyword, ///< Refers to a keyword or symbol.
640 RK_Macro, ///< Refers to a macro
641 RK_Pattern ///< Refers to a precomputed pattern.
644 /// \brief When Kind == RK_Declaration or RK_Pattern, the declaration we are
645 /// referring to. In the latter case, the declaration might be NULL.
646 const NamedDecl *Declaration;
649 /// \brief When Kind == RK_Keyword, the string representing the keyword
650 /// or symbol's spelling.
653 /// \brief When Kind == RK_Pattern, the code-completion string that
654 /// describes the completion text to insert.
655 CodeCompletionString *Pattern;
657 /// \brief When Kind == RK_Macro, the identifier that refers to a macro.
658 const IdentifierInfo *Macro;
661 /// \brief The priority of this particular code-completion result.
664 /// \brief Specifies which parameter (of a function, Objective-C method,
665 /// macro, etc.) we should start with when formatting the result.
666 unsigned StartParameter;
668 /// \brief The kind of result stored here.
671 /// \brief The cursor kind that describes this result.
672 CXCursorKind CursorKind;
674 /// \brief The availability of this result.
675 CXAvailabilityKind Availability;
677 /// \brief Whether this result is hidden by another name.
680 /// \brief Whether this result was found via lookup into a base class.
681 bool QualifierIsInformative : 1;
683 /// \brief Whether this declaration is the beginning of a
684 /// nested-name-specifier and, therefore, should be followed by '::'.
685 bool StartsNestedNameSpecifier : 1;
687 /// \brief Whether all parameters (of a function, Objective-C
688 /// method, etc.) should be considered "informative".
689 bool AllParametersAreInformative : 1;
691 /// \brief Whether we're completing a declaration of the given entity,
692 /// rather than a use of that entity.
693 bool DeclaringEntity : 1;
695 /// \brief If the result should have a nested-name-specifier, this is it.
696 /// When \c QualifierIsInformative, the nested-name-specifier is
697 /// informative rather than required.
698 NestedNameSpecifier *Qualifier;
700 /// \brief Build a result that refers to a declaration.
701 CodeCompletionResult(const NamedDecl *Declaration,
703 NestedNameSpecifier *Qualifier = 0,
704 bool QualifierIsInformative = false,
705 bool Accessible = true)
706 : Declaration(Declaration), Priority(Priority),
707 StartParameter(0), Kind(RK_Declaration),
708 Availability(CXAvailability_Available), Hidden(false),
709 QualifierIsInformative(QualifierIsInformative),
710 StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
711 DeclaringEntity(false), Qualifier(Qualifier) {
712 computeCursorKindAndAvailability(Accessible);
715 /// \brief Build a result that refers to a keyword or symbol.
716 CodeCompletionResult(const char *Keyword, unsigned Priority = CCP_Keyword)
717 : Declaration(0), Keyword(Keyword), Priority(Priority), StartParameter(0),
718 Kind(RK_Keyword), CursorKind(CXCursor_NotImplemented),
719 Availability(CXAvailability_Available), Hidden(false),
720 QualifierIsInformative(0), StartsNestedNameSpecifier(false),
721 AllParametersAreInformative(false), DeclaringEntity(false), Qualifier(0)
725 /// \brief Build a result that refers to a macro.
726 CodeCompletionResult(const IdentifierInfo *Macro,
727 unsigned Priority = CCP_Macro)
728 : Declaration(0), Macro(Macro), Priority(Priority), StartParameter(0),
729 Kind(RK_Macro), CursorKind(CXCursor_MacroDefinition),
730 Availability(CXAvailability_Available), Hidden(false),
731 QualifierIsInformative(0), StartsNestedNameSpecifier(false),
732 AllParametersAreInformative(false), DeclaringEntity(false), Qualifier(0)
736 /// \brief Build a result that refers to a pattern.
737 CodeCompletionResult(CodeCompletionString *Pattern,
738 unsigned Priority = CCP_CodePattern,
739 CXCursorKind CursorKind = CXCursor_NotImplemented,
740 CXAvailabilityKind Availability = CXAvailability_Available,
741 const NamedDecl *D = 0)
742 : Declaration(D), Pattern(Pattern), Priority(Priority), StartParameter(0),
743 Kind(RK_Pattern), CursorKind(CursorKind), Availability(Availability),
744 Hidden(false), QualifierIsInformative(0),
745 StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
746 DeclaringEntity(false), Qualifier(0)
750 /// \brief Build a result that refers to a pattern with an associated
752 CodeCompletionResult(CodeCompletionString *Pattern, NamedDecl *D,
754 : Declaration(D), Pattern(Pattern), Priority(Priority), StartParameter(0),
755 Kind(RK_Pattern), Availability(CXAvailability_Available), Hidden(false),
756 QualifierIsInformative(false), StartsNestedNameSpecifier(false),
757 AllParametersAreInformative(false), DeclaringEntity(false), Qualifier(0) {
758 computeCursorKindAndAvailability();
761 /// \brief Retrieve the declaration stored in this result.
762 const NamedDecl *getDeclaration() const {
763 assert(Kind == RK_Declaration && "Not a declaration result");
767 /// \brief Retrieve the keyword stored in this result.
768 const char *getKeyword() const {
769 assert(Kind == RK_Keyword && "Not a keyword result");
773 /// \brief Create a new code-completion string that describes how to insert
774 /// this result into a program.
776 /// \param S The semantic analysis that created the result.
778 /// \param Allocator The allocator that will be used to allocate the
780 CodeCompletionString *CreateCodeCompletionString(Sema &S,
781 CodeCompletionAllocator &Allocator,
782 CodeCompletionTUInfo &CCTUInfo,
783 bool IncludeBriefComments);
784 CodeCompletionString *CreateCodeCompletionString(ASTContext &Ctx,
786 CodeCompletionAllocator &Allocator,
787 CodeCompletionTUInfo &CCTUInfo,
788 bool IncludeBriefComments);
791 void computeCursorKindAndAvailability(bool Accessible = true);
794 bool operator<(const CodeCompletionResult &X, const CodeCompletionResult &Y);
796 inline bool operator>(const CodeCompletionResult &X,
797 const CodeCompletionResult &Y) {
801 inline bool operator<=(const CodeCompletionResult &X,
802 const CodeCompletionResult &Y) {
806 inline bool operator>=(const CodeCompletionResult &X,
807 const CodeCompletionResult &Y) {
812 raw_ostream &operator<<(raw_ostream &OS,
813 const CodeCompletionString &CCS);
815 /// \brief Abstract interface for a consumer of code-completion
817 class CodeCompleteConsumer {
819 const CodeCompleteOptions CodeCompleteOpts;
821 /// \brief Whether the output format for the code-completion consumer is
826 class OverloadCandidate {
828 /// \brief Describes the type of overload candidate.
830 /// \brief The candidate is a function declaration.
832 /// \brief The candidate is a function template.
834 /// \brief The "candidate" is actually a variable, expression, or block
835 /// for which we only have a function prototype.
840 /// \brief The kind of overload candidate.
844 /// \brief The function overload candidate, available when
845 /// Kind == CK_Function.
846 FunctionDecl *Function;
848 /// \brief The function template overload candidate, available when
849 /// Kind == CK_FunctionTemplate.
850 FunctionTemplateDecl *FunctionTemplate;
852 /// \brief The function type that describes the entity being called,
853 /// when Kind == CK_FunctionType.
854 const FunctionType *Type;
858 OverloadCandidate(FunctionDecl *Function)
859 : Kind(CK_Function), Function(Function) { }
861 OverloadCandidate(FunctionTemplateDecl *FunctionTemplateDecl)
862 : Kind(CK_FunctionTemplate), FunctionTemplate(FunctionTemplateDecl) { }
864 OverloadCandidate(const FunctionType *Type)
865 : Kind(CK_FunctionType), Type(Type) { }
867 /// \brief Determine the kind of overload candidate.
868 CandidateKind getKind() const { return Kind; }
870 /// \brief Retrieve the function overload candidate or the templated
871 /// function declaration for a function template.
872 FunctionDecl *getFunction() const;
874 /// \brief Retrieve the function template overload candidate.
875 FunctionTemplateDecl *getFunctionTemplate() const {
876 assert(getKind() == CK_FunctionTemplate && "Not a function template");
877 return FunctionTemplate;
880 /// \brief Retrieve the function type of the entity, regardless of how the
881 /// function is stored.
882 const FunctionType *getFunctionType() const;
884 /// \brief Create a new code-completion string that describes the function
885 /// signature of this overload candidate.
886 CodeCompletionString *CreateSignatureString(unsigned CurrentArg,
888 CodeCompletionAllocator &Allocator,
889 CodeCompletionTUInfo &CCTUInfo) const;
892 CodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
894 : CodeCompleteOpts(CodeCompleteOpts), OutputIsBinary(OutputIsBinary)
897 /// \brief Whether the code-completion consumer wants to see macros.
898 bool includeMacros() const {
899 return CodeCompleteOpts.IncludeMacros;
902 /// \brief Whether the code-completion consumer wants to see code patterns.
903 bool includeCodePatterns() const {
904 return CodeCompleteOpts.IncludeCodePatterns;
907 /// \brief Whether to include global (top-level) declaration results.
908 bool includeGlobals() const {
909 return CodeCompleteOpts.IncludeGlobals;
912 /// \brief Whether to include brief documentation comments within the set of
913 /// code completions returned.
914 bool includeBriefComments() const {
915 return CodeCompleteOpts.IncludeBriefComments;
918 /// \brief Determine whether the output of this consumer is binary.
919 bool isOutputBinary() const { return OutputIsBinary; }
921 /// \brief Deregisters and destroys this code-completion consumer.
922 virtual ~CodeCompleteConsumer();
924 /// \name Code-completion callbacks
926 /// \brief Process the finalized code-completion results.
927 virtual void ProcessCodeCompleteResults(Sema &S,
928 CodeCompletionContext Context,
929 CodeCompletionResult *Results,
930 unsigned NumResults) { }
932 /// \param S the semantic-analyzer object for which code-completion is being
935 /// \param CurrentArg the index of the current argument.
937 /// \param Candidates an array of overload candidates.
939 /// \param NumCandidates the number of overload candidates
940 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
941 OverloadCandidate *Candidates,
942 unsigned NumCandidates) { }
945 /// \brief Retrieve the allocator that will be used to allocate
946 /// code completion strings.
947 virtual CodeCompletionAllocator &getAllocator() = 0;
949 virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() = 0;
952 /// \brief A simple code-completion consumer that prints the results it
953 /// receives in a simple format.
954 class PrintingCodeCompleteConsumer : public CodeCompleteConsumer {
955 /// \brief The raw output stream.
958 CodeCompletionTUInfo CCTUInfo;
961 /// \brief Create a new printing code-completion consumer that prints its
962 /// results to the given raw output stream.
963 PrintingCodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
965 : CodeCompleteConsumer(CodeCompleteOpts, false), OS(OS),
966 CCTUInfo(new GlobalCodeCompletionAllocator) {}
968 /// \brief Prints the finalized code-completion results.
969 virtual void ProcessCodeCompleteResults(Sema &S,
970 CodeCompletionContext Context,
971 CodeCompletionResult *Results,
972 unsigned NumResults);
974 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
975 OverloadCandidate *Candidates,
976 unsigned NumCandidates);
978 virtual CodeCompletionAllocator &getAllocator() {
979 return CCTUInfo.getAllocator();
982 virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() { return CCTUInfo; }
985 } // end namespace clang
987 #endif // LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H