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 "clang/Sema/CodeCompleteOptions.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Allocator.h"
22 #include "clang-c/Index.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, 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(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 expcted.
249 CCC_ObjCInstanceMessage,
250 /// \brief Code completion where an Objective-C class message is expected.
251 CCC_ObjCClassMessage,
252 /// \brief Code completion where the name of an Objective-C class is
254 CCC_ObjCInterfaceName,
255 /// \brief Code completion where an Objective-C category name is expected.
256 CCC_ObjCCategoryName,
257 /// \brief An unknown context, in which we are recovering from a parsing
258 /// error and don't know which completions we should give.
265 /// \brief The type that would prefer to see at this point (e.g., the type
266 /// of an initializer or function parameter).
267 QualType PreferredType;
269 /// \brief The type of the base object in a member access expression.
272 /// \brief The identifiers for Objective-C selector parts.
273 IdentifierInfo **SelIdents;
275 /// \brief The number of Objective-C selector parts.
276 unsigned NumSelIdents;
279 /// \brief Construct a new code-completion context of the given kind.
280 CodeCompletionContext(enum Kind Kind) : Kind(Kind), SelIdents(NULL),
283 /// \brief Construct a new code-completion context of the given kind.
284 CodeCompletionContext(enum Kind Kind, QualType T,
285 IdentifierInfo **SelIdents = NULL,
286 unsigned NumSelIdents = 0) : Kind(Kind),
287 SelIdents(SelIdents),
288 NumSelIdents(NumSelIdents) {
289 if (Kind == CCC_DotMemberAccess || Kind == CCC_ArrowMemberAccess ||
290 Kind == CCC_ObjCPropertyAccess || Kind == CCC_ObjCClassMessage ||
291 Kind == CCC_ObjCInstanceMessage)
297 /// \brief Retrieve the kind of code-completion context.
298 enum Kind getKind() const { return Kind; }
300 /// \brief Retrieve the type that this expression would prefer to have, e.g.,
301 /// if the expression is a variable initializer or a function argument, the
302 /// type of the corresponding variable or function parameter.
303 QualType getPreferredType() const { return PreferredType; }
305 /// \brief Retrieve the type of the base object in a member-access
307 QualType getBaseType() const { return BaseType; }
309 /// \brief Retrieve the Objective-C selector identifiers.
310 IdentifierInfo **getSelIdents() const { return SelIdents; }
312 /// \brief Retrieve the number of Objective-C selector identifiers.
313 unsigned getNumSelIdents() const { return NumSelIdents; }
315 /// \brief Determines whether we want C++ constructors as results within this
317 bool wantConstructorResults() const;
321 /// \brief A "string" used to describe how code completion can
322 /// be performed for an entity.
324 /// A code completion string typically shows how a particular entity can be
325 /// used. For example, the code completion string for a function would show
326 /// the syntax to call it, including the parentheses, placeholders for the
328 class CodeCompletionString {
330 /// \brief The different kinds of "chunks" that can occur within a code
331 /// completion string.
333 /// \brief The piece of text that the user is expected to type to
334 /// match the code-completion string, typically a keyword or the name of a
335 /// declarator or macro.
337 /// \brief A piece of text that should be placed in the buffer, e.g.,
338 /// parentheses or a comma in a function call.
340 /// \brief A code completion string that is entirely optional. For example,
341 /// an optional code completion string that describes the default arguments
342 /// in a function call.
344 /// \brief A string that acts as a placeholder for, e.g., a function
347 /// \brief A piece of text that describes something about the result but
348 /// should not be inserted into the buffer.
350 /// \brief A piece of text that describes the type of an entity or, for
351 /// functions and methods, the return type.
353 /// \brief A piece of text that describes the parameter that corresponds
354 /// to the code-completion location within a function call, message send,
355 /// macro invocation, etc.
357 /// \brief A left parenthesis ('(').
359 /// \brief A right parenthesis (')').
361 /// \brief A left bracket ('[').
363 /// \brief A right bracket (']').
365 /// \brief A left brace ('{').
367 /// \brief A right brace ('}').
369 /// \brief A left angle bracket ('<').
371 /// \brief A right angle bracket ('>').
373 /// \brief A comma separator (',').
375 /// \brief A colon (':').
377 /// \brief A semicolon (';').
379 /// \brief An '=' sign.
381 /// \brief Horizontal whitespace (' ').
383 /// \brief Vertical whitespace ('\\n' or '\\r\\n', depending on the
388 /// \brief One piece of the code completion string.
390 /// \brief The kind of data stored in this piece of the code completion
395 /// \brief The text string associated with a CK_Text, CK_Placeholder,
396 /// CK_Informative, or CK_Comma chunk.
397 /// The string is owned by the chunk and will be deallocated
398 /// (with delete[]) when the chunk is destroyed.
401 /// \brief The code completion string associated with a CK_Optional chunk.
402 /// The optional code completion string is owned by the chunk, and will
403 /// be deallocated (with delete) when the chunk is destroyed.
404 CodeCompletionString *Optional;
407 Chunk() : Kind(CK_Text), Text(0) { }
409 explicit Chunk(ChunkKind Kind, const char *Text = "");
411 /// \brief Create a new text chunk.
412 static Chunk CreateText(const char *Text);
414 /// \brief Create a new optional chunk.
415 static Chunk CreateOptional(CodeCompletionString *Optional);
417 /// \brief Create a new placeholder chunk.
418 static Chunk CreatePlaceholder(const char *Placeholder);
420 /// \brief Create a new informative chunk.
421 static Chunk CreateInformative(const char *Informative);
423 /// \brief Create a new result type chunk.
424 static Chunk CreateResultType(const char *ResultType);
426 /// \brief Create a new current-parameter chunk.
427 static Chunk CreateCurrentParameter(const char *CurrentParameter);
431 /// \brief The number of chunks stored in this string.
432 unsigned NumChunks : 16;
434 /// \brief The number of annotations for this code-completion result.
435 unsigned NumAnnotations : 16;
437 /// \brief The priority of this code-completion string.
438 unsigned Priority : 16;
440 /// \brief The availability of this code-completion result.
441 unsigned Availability : 2;
443 /// \brief The name of the parent context.
444 StringRef ParentName;
446 /// \brief A brief documentation comment attached to the declaration of
447 /// entity being completed by this result.
448 const char *BriefComment;
450 CodeCompletionString(const CodeCompletionString &) LLVM_DELETED_FUNCTION;
451 void operator=(const CodeCompletionString &) LLVM_DELETED_FUNCTION;
453 CodeCompletionString(const Chunk *Chunks, unsigned NumChunks,
454 unsigned Priority, CXAvailabilityKind Availability,
455 const char **Annotations, unsigned NumAnnotations,
456 StringRef ParentName,
457 const char *BriefComment);
458 ~CodeCompletionString() { }
460 friend class CodeCompletionBuilder;
461 friend class CodeCompletionResult;
464 typedef const Chunk *iterator;
465 iterator begin() const { return reinterpret_cast<const Chunk *>(this + 1); }
466 iterator end() const { return begin() + NumChunks; }
467 bool empty() const { return NumChunks == 0; }
468 unsigned size() const { return NumChunks; }
470 const Chunk &operator[](unsigned I) const {
471 assert(I < size() && "Chunk index out-of-range");
475 /// \brief Returns the text in the TypedText chunk.
476 const char *getTypedText() const;
478 /// \brief Retrieve the priority of this code completion result.
479 unsigned getPriority() const { return Priority; }
481 /// \brief Retrieve the availability of this code completion result.
482 unsigned getAvailability() const { return Availability; }
484 /// \brief Retrieve the number of annotations for this code completion result.
485 unsigned getAnnotationCount() const;
487 /// \brief Retrieve the annotation string specified by \c AnnotationNr.
488 const char *getAnnotation(unsigned AnnotationNr) const;
490 /// \brief Retrieve the name of the parent context.
491 StringRef getParentContextName() const {
495 const char *getBriefComment() const {
499 /// \brief Retrieve a string representation of the code completion string,
500 /// which is mainly useful for debugging.
501 std::string getAsString() const;
504 /// \brief An allocator used specifically for the purpose of code completion.
505 class CodeCompletionAllocator : public llvm::BumpPtrAllocator {
507 /// \brief Copy the given string into this allocator.
508 const char *CopyString(StringRef String);
510 /// \brief Copy the given string into this allocator.
511 const char *CopyString(Twine String);
513 // \brief Copy the given string into this allocator.
514 const char *CopyString(const char *String) {
515 return CopyString(StringRef(String));
518 /// \brief Copy the given string into this allocator.
519 const char *CopyString(const std::string &String) {
520 return CopyString(StringRef(String));
524 /// \brief Allocator for a cached set of global code completions.
525 class GlobalCodeCompletionAllocator
526 : public CodeCompletionAllocator,
527 public RefCountedBase<GlobalCodeCompletionAllocator>
532 class CodeCompletionTUInfo {
533 llvm::DenseMap<DeclContext *, StringRef> ParentNames;
534 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> AllocatorRef;
537 explicit CodeCompletionTUInfo(
538 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> Allocator)
539 : AllocatorRef(Allocator) { }
541 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> getAllocatorRef() const {
544 CodeCompletionAllocator &getAllocator() const {
545 assert(AllocatorRef);
546 return *AllocatorRef;
549 StringRef getParentName(DeclContext *DC);
552 } // end namespace clang
555 template <> struct isPodLike<clang::CodeCompletionString::Chunk> {
556 static const bool value = true;
562 /// \brief A builder class used to construct new code-completion strings.
563 class CodeCompletionBuilder {
565 typedef CodeCompletionString::Chunk Chunk;
568 CodeCompletionAllocator &Allocator;
569 CodeCompletionTUInfo &CCTUInfo;
571 CXAvailabilityKind Availability;
572 StringRef ParentName;
573 const char *BriefComment;
575 /// \brief The chunks stored in this string.
576 SmallVector<Chunk, 4> Chunks;
578 SmallVector<const char *, 2> Annotations;
581 CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
582 CodeCompletionTUInfo &CCTUInfo)
583 : Allocator(Allocator), CCTUInfo(CCTUInfo),
584 Priority(0), Availability(CXAvailability_Available),
585 BriefComment(NULL) { }
587 CodeCompletionBuilder(CodeCompletionAllocator &Allocator,
588 CodeCompletionTUInfo &CCTUInfo,
589 unsigned Priority, CXAvailabilityKind Availability)
590 : Allocator(Allocator), CCTUInfo(CCTUInfo),
591 Priority(Priority), Availability(Availability),
592 BriefComment(NULL) { }
594 /// \brief Retrieve the allocator into which the code completion
595 /// strings should be allocated.
596 CodeCompletionAllocator &getAllocator() const { return Allocator; }
598 CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
600 /// \brief Take the resulting completion string.
602 /// This operation can only be performed once.
603 CodeCompletionString *TakeString();
605 /// \brief Add a new typed-text chunk.
606 void AddTypedTextChunk(const char *Text);
608 /// \brief Add a new text chunk.
609 void AddTextChunk(const char *Text);
611 /// \brief Add a new optional chunk.
612 void AddOptionalChunk(CodeCompletionString *Optional);
614 /// \brief Add a new placeholder chunk.
615 void AddPlaceholderChunk(const char *Placeholder);
617 /// \brief Add a new informative chunk.
618 void AddInformativeChunk(const char *Text);
620 /// \brief Add a new result-type chunk.
621 void AddResultTypeChunk(const char *ResultType);
623 /// \brief Add a new current-parameter chunk.
624 void AddCurrentParameterChunk(const char *CurrentParameter);
626 /// \brief Add a new chunk.
627 void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text = "");
629 void AddAnnotation(const char *A) { Annotations.push_back(A); }
631 /// \brief Add the parent context information to this code completion.
632 void addParentContext(DeclContext *DC);
634 void addBriefComment(StringRef Comment);
636 StringRef getParentName() const { return ParentName; }
639 /// \brief Captures a result of code completion.
640 class CodeCompletionResult {
642 /// \brief Describes the kind of result generated.
644 RK_Declaration = 0, ///< Refers to a declaration
645 RK_Keyword, ///< Refers to a keyword or symbol.
646 RK_Macro, ///< Refers to a macro
647 RK_Pattern ///< Refers to a precomputed pattern.
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 Specifies which parameter (of a function, Objective-C method,
671 /// macro, etc.) we should start with when formatting the result.
672 unsigned StartParameter;
674 /// \brief The kind of result stored here.
677 /// \brief The cursor kind that describes this result.
678 CXCursorKind CursorKind;
680 /// \brief The availability of this result.
681 CXAvailabilityKind Availability;
683 /// \brief Whether this result is hidden by another name.
686 /// \brief Whether this result was found via lookup into a base class.
687 bool QualifierIsInformative : 1;
689 /// \brief Whether this declaration is the beginning of a
690 /// nested-name-specifier and, therefore, should be followed by '::'.
691 bool StartsNestedNameSpecifier : 1;
693 /// \brief Whether all parameters (of a function, Objective-C
694 /// method, etc.) should be considered "informative".
695 bool AllParametersAreInformative : 1;
697 /// \brief Whether we're completing a declaration of the given entity,
698 /// rather than a use of that entity.
699 bool DeclaringEntity : 1;
701 /// \brief If the result should have a nested-name-specifier, this is it.
702 /// When \c QualifierIsInformative, the nested-name-specifier is
703 /// informative rather than required.
704 NestedNameSpecifier *Qualifier;
706 /// \brief Build a result that refers to a declaration.
707 CodeCompletionResult(NamedDecl *Declaration,
708 NestedNameSpecifier *Qualifier = 0,
709 bool QualifierIsInformative = false,
710 bool Accessible = true)
711 : Declaration(Declaration), Priority(getPriorityFromDecl(Declaration)),
712 StartParameter(0), Kind(RK_Declaration),
713 Availability(CXAvailability_Available), Hidden(false),
714 QualifierIsInformative(QualifierIsInformative),
715 StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
716 DeclaringEntity(false), Qualifier(Qualifier) {
717 computeCursorKindAndAvailability(Accessible);
720 /// \brief Build a result that refers to a keyword or symbol.
721 CodeCompletionResult(const char *Keyword, unsigned Priority = CCP_Keyword)
722 : Declaration(0), Keyword(Keyword), Priority(Priority), StartParameter(0),
723 Kind(RK_Keyword), CursorKind(CXCursor_NotImplemented),
724 Availability(CXAvailability_Available), Hidden(false),
725 QualifierIsInformative(0), StartsNestedNameSpecifier(false),
726 AllParametersAreInformative(false), DeclaringEntity(false), Qualifier(0)
730 /// \brief Build a result that refers to a macro.
731 CodeCompletionResult(IdentifierInfo *Macro, unsigned Priority = CCP_Macro)
732 : Declaration(0), Macro(Macro), Priority(Priority), StartParameter(0),
733 Kind(RK_Macro), CursorKind(CXCursor_MacroDefinition),
734 Availability(CXAvailability_Available), Hidden(false),
735 QualifierIsInformative(0), StartsNestedNameSpecifier(false),
736 AllParametersAreInformative(false), DeclaringEntity(false), Qualifier(0)
740 /// \brief Build a result that refers to a pattern.
741 CodeCompletionResult(CodeCompletionString *Pattern,
742 unsigned Priority = CCP_CodePattern,
743 CXCursorKind CursorKind = CXCursor_NotImplemented,
744 CXAvailabilityKind Availability = CXAvailability_Available,
746 : Declaration(D), Pattern(Pattern), Priority(Priority), StartParameter(0),
747 Kind(RK_Pattern), CursorKind(CursorKind), Availability(Availability),
748 Hidden(false), QualifierIsInformative(0),
749 StartsNestedNameSpecifier(false), AllParametersAreInformative(false),
750 DeclaringEntity(false), Qualifier(0)
754 /// \brief Build a result that refers to a pattern with an associated
756 CodeCompletionResult(CodeCompletionString *Pattern, NamedDecl *D,
758 : Declaration(D), Pattern(Pattern), Priority(Priority), StartParameter(0),
759 Kind(RK_Pattern), Availability(CXAvailability_Available), Hidden(false),
760 QualifierIsInformative(false), StartsNestedNameSpecifier(false),
761 AllParametersAreInformative(false), DeclaringEntity(false), Qualifier(0) {
762 computeCursorKindAndAvailability();
765 /// \brief Retrieve the declaration stored in this result.
766 NamedDecl *getDeclaration() const {
767 assert(Kind == RK_Declaration && "Not a declaration result");
771 /// \brief Retrieve the keyword stored in this result.
772 const char *getKeyword() const {
773 assert(Kind == RK_Keyword && "Not a keyword result");
777 /// \brief Create a new code-completion string that describes how to insert
778 /// this result into a program.
780 /// \param S The semantic analysis that created the result.
782 /// \param Allocator The allocator that will be used to allocate the
784 CodeCompletionString *CreateCodeCompletionString(Sema &S,
785 CodeCompletionAllocator &Allocator,
786 CodeCompletionTUInfo &CCTUInfo,
787 bool IncludeBriefComments);
788 CodeCompletionString *CreateCodeCompletionString(ASTContext &Ctx,
790 CodeCompletionAllocator &Allocator,
791 CodeCompletionTUInfo &CCTUInfo,
792 bool IncludeBriefComments);
794 /// \brief Determine a base priority for the given declaration.
795 static unsigned getPriorityFromDecl(NamedDecl *ND);
798 void computeCursorKindAndAvailability(bool Accessible = true);
801 bool operator<(const CodeCompletionResult &X, const CodeCompletionResult &Y);
803 inline bool operator>(const CodeCompletionResult &X,
804 const CodeCompletionResult &Y) {
808 inline bool operator<=(const CodeCompletionResult &X,
809 const CodeCompletionResult &Y) {
813 inline bool operator>=(const CodeCompletionResult &X,
814 const CodeCompletionResult &Y) {
819 raw_ostream &operator<<(raw_ostream &OS,
820 const CodeCompletionString &CCS);
822 /// \brief Abstract interface for a consumer of code-completion
824 class CodeCompleteConsumer {
826 const CodeCompleteOptions CodeCompleteOpts;
828 /// \brief Whether the output format for the code-completion consumer is
833 class OverloadCandidate {
835 /// \brief Describes the type of overload candidate.
837 /// \brief The candidate is a function declaration.
839 /// \brief The candidate is a function template.
841 /// \brief The "candidate" is actually a variable, expression, or block
842 /// for which we only have a function prototype.
847 /// \brief The kind of overload candidate.
851 /// \brief The function overload candidate, available when
852 /// Kind == CK_Function.
853 FunctionDecl *Function;
855 /// \brief The function template overload candidate, available when
856 /// Kind == CK_FunctionTemplate.
857 FunctionTemplateDecl *FunctionTemplate;
859 /// \brief The function type that describes the entity being called,
860 /// when Kind == CK_FunctionType.
861 const FunctionType *Type;
865 OverloadCandidate(FunctionDecl *Function)
866 : Kind(CK_Function), Function(Function) { }
868 OverloadCandidate(FunctionTemplateDecl *FunctionTemplateDecl)
869 : Kind(CK_FunctionTemplate), FunctionTemplate(FunctionTemplateDecl) { }
871 OverloadCandidate(const FunctionType *Type)
872 : Kind(CK_FunctionType), Type(Type) { }
874 /// \brief Determine the kind of overload candidate.
875 CandidateKind getKind() const { return Kind; }
877 /// \brief Retrieve the function overload candidate or the templated
878 /// function declaration for a function template.
879 FunctionDecl *getFunction() const;
881 /// \brief Retrieve the function template overload candidate.
882 FunctionTemplateDecl *getFunctionTemplate() const {
883 assert(getKind() == CK_FunctionTemplate && "Not a function template");
884 return FunctionTemplate;
887 /// \brief Retrieve the function type of the entity, regardless of how the
888 /// function is stored.
889 const FunctionType *getFunctionType() const;
891 /// \brief Create a new code-completion string that describes the function
892 /// signature of this overload candidate.
893 CodeCompletionString *CreateSignatureString(unsigned CurrentArg,
895 CodeCompletionAllocator &Allocator,
896 CodeCompletionTUInfo &CCTUInfo) const;
899 CodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
901 : CodeCompleteOpts(CodeCompleteOpts), OutputIsBinary(OutputIsBinary)
904 /// \brief Whether the code-completion consumer wants to see macros.
905 bool includeMacros() const {
906 return CodeCompleteOpts.IncludeMacros;
909 /// \brief Whether the code-completion consumer wants to see code patterns.
910 bool includeCodePatterns() const {
911 return CodeCompleteOpts.IncludeCodePatterns;
914 /// \brief Whether to include global (top-level) declaration results.
915 bool includeGlobals() const {
916 return CodeCompleteOpts.IncludeGlobals;
919 /// \brief Whether to include brief documentation comments within the set of
920 /// code completions returned.
921 bool includeBriefComments() const {
922 return CodeCompleteOpts.IncludeBriefComments;
925 /// \brief Determine whether the output of this consumer is binary.
926 bool isOutputBinary() const { return OutputIsBinary; }
928 /// \brief Deregisters and destroys this code-completion consumer.
929 virtual ~CodeCompleteConsumer();
931 /// \name Code-completion callbacks
933 /// \brief Process the finalized code-completion results.
934 virtual void ProcessCodeCompleteResults(Sema &S,
935 CodeCompletionContext Context,
936 CodeCompletionResult *Results,
937 unsigned NumResults) { }
939 /// \param S the semantic-analyzer object for which code-completion is being
942 /// \param CurrentArg the index of the current argument.
944 /// \param Candidates an array of overload candidates.
946 /// \param NumCandidates the number of overload candidates
947 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
948 OverloadCandidate *Candidates,
949 unsigned NumCandidates) { }
952 /// \brief Retrieve the allocator that will be used to allocate
953 /// code completion strings.
954 virtual CodeCompletionAllocator &getAllocator() = 0;
956 virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() = 0;
959 /// \brief A simple code-completion consumer that prints the results it
960 /// receives in a simple format.
961 class PrintingCodeCompleteConsumer : public CodeCompleteConsumer {
962 /// \brief The raw output stream.
965 CodeCompletionTUInfo CCTUInfo;
968 /// \brief Create a new printing code-completion consumer that prints its
969 /// results to the given raw output stream.
970 PrintingCodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts,
972 : CodeCompleteConsumer(CodeCompleteOpts, false), OS(OS),
973 CCTUInfo(new GlobalCodeCompletionAllocator) {}
975 /// \brief Prints the finalized code-completion results.
976 virtual void ProcessCodeCompleteResults(Sema &S,
977 CodeCompletionContext Context,
978 CodeCompletionResult *Results,
979 unsigned NumResults);
981 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
982 OverloadCandidate *Candidates,
983 unsigned NumCandidates);
985 virtual CodeCompletionAllocator &getAllocator() {
986 return CCTUInfo.getAllocator();
989 virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() { return CCTUInfo; }
992 } // end namespace clang
994 #endif // LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H