Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
14 | pmbaty | 1 | //===- CodeCompleteConsumer.h - Code Completion Interface -------*- C++ -*-===// |
2 | // |
||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
||
4 | // See https://llvm.org/LICENSE.txt for license information. |
||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
||
6 | // |
||
7 | //===----------------------------------------------------------------------===// |
||
8 | // |
||
9 | // This file defines the CodeCompleteConsumer class. |
||
10 | // |
||
11 | //===----------------------------------------------------------------------===// |
||
12 | |||
13 | #ifndef LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H |
||
14 | #define LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H |
||
15 | |||
16 | #include "clang-c/Index.h" |
||
17 | #include "clang/AST/Type.h" |
||
18 | #include "clang/Basic/LLVM.h" |
||
19 | #include "clang/Lex/MacroInfo.h" |
||
20 | #include "clang/Sema/CodeCompleteOptions.h" |
||
21 | #include "clang/Sema/DeclSpec.h" |
||
22 | #include "llvm/ADT/ArrayRef.h" |
||
23 | #include "llvm/ADT/DenseMap.h" |
||
24 | #include "llvm/ADT/SmallPtrSet.h" |
||
25 | #include "llvm/ADT/SmallVector.h" |
||
26 | #include "llvm/ADT/StringRef.h" |
||
27 | #include "llvm/Support/Allocator.h" |
||
28 | #include "llvm/Support/type_traits.h" |
||
29 | #include <cassert> |
||
30 | #include <memory> |
||
31 | #include <optional> |
||
32 | #include <string> |
||
33 | #include <utility> |
||
34 | |||
35 | namespace clang { |
||
36 | |||
37 | class ASTContext; |
||
38 | class Decl; |
||
39 | class DeclContext; |
||
40 | class FunctionDecl; |
||
41 | class FunctionTemplateDecl; |
||
42 | class IdentifierInfo; |
||
43 | class LangOptions; |
||
44 | class NamedDecl; |
||
45 | class NestedNameSpecifier; |
||
46 | class Preprocessor; |
||
47 | class RawComment; |
||
48 | class Sema; |
||
49 | class UsingShadowDecl; |
||
50 | |||
51 | /// Default priority values for code-completion results based |
||
52 | /// on their kind. |
||
53 | enum { |
||
54 | /// Priority for the next initialization in a constructor initializer |
||
55 | /// list. |
||
56 | CCP_NextInitializer = 7, |
||
57 | |||
58 | /// Priority for an enumeration constant inside a switch whose |
||
59 | /// condition is of the enumeration type. |
||
60 | CCP_EnumInCase = 7, |
||
61 | |||
62 | /// Priority for a send-to-super completion. |
||
63 | CCP_SuperCompletion = 20, |
||
64 | |||
65 | /// Priority for a declaration that is in the local scope. |
||
66 | CCP_LocalDeclaration = 34, |
||
67 | |||
68 | /// Priority for a member declaration found from the current |
||
69 | /// method or member function. |
||
70 | CCP_MemberDeclaration = 35, |
||
71 | |||
72 | /// Priority for a language keyword (that isn't any of the other |
||
73 | /// categories). |
||
74 | CCP_Keyword = 40, |
||
75 | |||
76 | /// Priority for a code pattern. |
||
77 | CCP_CodePattern = 40, |
||
78 | |||
79 | /// Priority for a non-type declaration. |
||
80 | CCP_Declaration = 50, |
||
81 | |||
82 | /// Priority for a type. |
||
83 | CCP_Type = CCP_Declaration, |
||
84 | |||
85 | /// Priority for a constant value (e.g., enumerator). |
||
86 | CCP_Constant = 65, |
||
87 | |||
88 | /// Priority for a preprocessor macro. |
||
89 | CCP_Macro = 70, |
||
90 | |||
91 | /// Priority for a nested-name-specifier. |
||
92 | CCP_NestedNameSpecifier = 75, |
||
93 | |||
94 | /// Priority for a result that isn't likely to be what the user wants, |
||
95 | /// but is included for completeness. |
||
96 | CCP_Unlikely = 80, |
||
97 | |||
98 | /// Priority for the Objective-C "_cmd" implicit parameter. |
||
99 | CCP_ObjC_cmd = CCP_Unlikely |
||
100 | }; |
||
101 | |||
102 | /// Priority value deltas that are added to code-completion results |
||
103 | /// based on the context of the result. |
||
104 | enum { |
||
105 | /// The result is in a base class. |
||
106 | CCD_InBaseClass = 2, |
||
107 | |||
108 | /// The result is a C++ non-static member function whose qualifiers |
||
109 | /// exactly match the object type on which the member function can be called. |
||
110 | CCD_ObjectQualifierMatch = -1, |
||
111 | |||
112 | /// The selector of the given message exactly matches the selector |
||
113 | /// of the current method, which might imply that some kind of delegation |
||
114 | /// is occurring. |
||
115 | CCD_SelectorMatch = -3, |
||
116 | |||
117 | /// Adjustment to the "bool" type in Objective-C, where the typedef |
||
118 | /// "BOOL" is preferred. |
||
119 | CCD_bool_in_ObjC = 1, |
||
120 | |||
121 | /// Adjustment for KVC code pattern priorities when it doesn't look |
||
122 | /// like the |
||
123 | CCD_ProbablyNotObjCCollection = 15, |
||
124 | |||
125 | /// An Objective-C method being used as a property. |
||
126 | CCD_MethodAsProperty = 2, |
||
127 | |||
128 | /// An Objective-C block property completed as a setter with a |
||
129 | /// block placeholder. |
||
130 | CCD_BlockPropertySetter = 3 |
||
131 | }; |
||
132 | |||
133 | /// Priority value factors by which we will divide or multiply the |
||
134 | /// priority of a code-completion result. |
||
135 | enum { |
||
136 | /// Divide by this factor when a code-completion result's type exactly |
||
137 | /// matches the type we expect. |
||
138 | CCF_ExactTypeMatch = 4, |
||
139 | |||
140 | /// Divide by this factor when a code-completion result's type is |
||
141 | /// similar to the type we expect (e.g., both arithmetic types, both |
||
142 | /// Objective-C object pointer types). |
||
143 | CCF_SimilarTypeMatch = 2 |
||
144 | }; |
||
145 | |||
146 | /// A simplified classification of types used when determining |
||
147 | /// "similar" types for code completion. |
||
148 | enum SimplifiedTypeClass { |
||
149 | STC_Arithmetic, |
||
150 | STC_Array, |
||
151 | STC_Block, |
||
152 | STC_Function, |
||
153 | STC_ObjectiveC, |
||
154 | STC_Other, |
||
155 | STC_Pointer, |
||
156 | STC_Record, |
||
157 | STC_Void |
||
158 | }; |
||
159 | |||
160 | /// Determine the simplified type class of the given canonical type. |
||
161 | SimplifiedTypeClass getSimplifiedTypeClass(CanQualType T); |
||
162 | |||
163 | /// Determine the type that this declaration will have if it is used |
||
164 | /// as a type or in an expression. |
||
165 | QualType getDeclUsageType(ASTContext &C, const NamedDecl *ND); |
||
166 | |||
167 | /// Determine the priority to be given to a macro code completion result |
||
168 | /// with the given name. |
||
169 | /// |
||
170 | /// \param MacroName The name of the macro. |
||
171 | /// |
||
172 | /// \param LangOpts Options describing the current language dialect. |
||
173 | /// |
||
174 | /// \param PreferredTypeIsPointer Whether the preferred type for the context |
||
175 | /// of this macro is a pointer type. |
||
176 | unsigned getMacroUsagePriority(StringRef MacroName, |
||
177 | const LangOptions &LangOpts, |
||
178 | bool PreferredTypeIsPointer = false); |
||
179 | |||
180 | /// Determine the libclang cursor kind associated with the given |
||
181 | /// declaration. |
||
182 | CXCursorKind getCursorKindForDecl(const Decl *D); |
||
183 | |||
184 | /// The context in which code completion occurred, so that the |
||
185 | /// code-completion consumer can process the results accordingly. |
||
186 | class CodeCompletionContext { |
||
187 | public: |
||
188 | enum Kind { |
||
189 | /// An unspecified code-completion context. |
||
190 | CCC_Other, |
||
191 | |||
192 | /// An unspecified code-completion context where we should also add |
||
193 | /// macro completions. |
||
194 | CCC_OtherWithMacros, |
||
195 | |||
196 | /// Code completion occurred within a "top-level" completion context, |
||
197 | /// e.g., at namespace or global scope. |
||
198 | CCC_TopLevel, |
||
199 | |||
200 | /// Code completion occurred within an Objective-C interface, |
||
201 | /// protocol, or category interface. |
||
202 | CCC_ObjCInterface, |
||
203 | |||
204 | /// Code completion occurred within an Objective-C implementation |
||
205 | /// or category implementation. |
||
206 | CCC_ObjCImplementation, |
||
207 | |||
208 | /// Code completion occurred within the instance variable list of |
||
209 | /// an Objective-C interface, implementation, or category implementation. |
||
210 | CCC_ObjCIvarList, |
||
211 | |||
212 | /// Code completion occurred within a class, struct, or union. |
||
213 | CCC_ClassStructUnion, |
||
214 | |||
215 | /// Code completion occurred where a statement (or declaration) is |
||
216 | /// expected in a function, method, or block. |
||
217 | CCC_Statement, |
||
218 | |||
219 | /// Code completion occurred where an expression is expected. |
||
220 | CCC_Expression, |
||
221 | |||
222 | /// Code completion occurred where an Objective-C message receiver |
||
223 | /// is expected. |
||
224 | CCC_ObjCMessageReceiver, |
||
225 | |||
226 | /// Code completion occurred on the right-hand side of a member |
||
227 | /// access expression using the dot operator. |
||
228 | /// |
||
229 | /// The results of this completion are the members of the type being |
||
230 | /// accessed. The type itself is available via |
||
231 | /// \c CodeCompletionContext::getType(). |
||
232 | CCC_DotMemberAccess, |
||
233 | |||
234 | /// Code completion occurred on the right-hand side of a member |
||
235 | /// access expression using the arrow operator. |
||
236 | /// |
||
237 | /// The results of this completion are the members of the type being |
||
238 | /// accessed. The type itself is available via |
||
239 | /// \c CodeCompletionContext::getType(). |
||
240 | CCC_ArrowMemberAccess, |
||
241 | |||
242 | /// Code completion occurred on the right-hand side of an Objective-C |
||
243 | /// property access expression. |
||
244 | /// |
||
245 | /// The results of this completion are the members of the type being |
||
246 | /// accessed. The type itself is available via |
||
247 | /// \c CodeCompletionContext::getType(). |
||
248 | CCC_ObjCPropertyAccess, |
||
249 | |||
250 | /// Code completion occurred after the "enum" keyword, to indicate |
||
251 | /// an enumeration name. |
||
252 | CCC_EnumTag, |
||
253 | |||
254 | /// Code completion occurred after the "union" keyword, to indicate |
||
255 | /// a union name. |
||
256 | CCC_UnionTag, |
||
257 | |||
258 | /// Code completion occurred after the "struct" or "class" keyword, |
||
259 | /// to indicate a struct or class name. |
||
260 | CCC_ClassOrStructTag, |
||
261 | |||
262 | /// Code completion occurred where a protocol name is expected. |
||
263 | CCC_ObjCProtocolName, |
||
264 | |||
265 | /// Code completion occurred where a namespace or namespace alias |
||
266 | /// is expected. |
||
267 | CCC_Namespace, |
||
268 | |||
269 | /// Code completion occurred where a type name is expected. |
||
270 | CCC_Type, |
||
271 | |||
272 | /// Code completion occurred where a new name is expected. |
||
273 | CCC_NewName, |
||
274 | |||
275 | /// Code completion occurred where both a new name and an existing symbol is |
||
276 | /// permissible. |
||
277 | CCC_SymbolOrNewName, |
||
278 | |||
279 | /// Code completion occurred where an existing name(such as type, function |
||
280 | /// or variable) is expected. |
||
281 | CCC_Symbol, |
||
282 | |||
283 | /// Code completion occurred where an macro is being defined. |
||
284 | CCC_MacroName, |
||
285 | |||
286 | /// Code completion occurred where a macro name is expected |
||
287 | /// (without any arguments, in the case of a function-like macro). |
||
288 | CCC_MacroNameUse, |
||
289 | |||
290 | /// Code completion occurred within a preprocessor expression. |
||
291 | CCC_PreprocessorExpression, |
||
292 | |||
293 | /// Code completion occurred where a preprocessor directive is |
||
294 | /// expected. |
||
295 | CCC_PreprocessorDirective, |
||
296 | |||
297 | /// Code completion occurred in a context where natural language is |
||
298 | /// expected, e.g., a comment or string literal. |
||
299 | /// |
||
300 | /// This context usually implies that no completions should be added, |
||
301 | /// unless they come from an appropriate natural-language dictionary. |
||
302 | CCC_NaturalLanguage, |
||
303 | |||
304 | /// Code completion for a selector, as in an \@selector expression. |
||
305 | CCC_SelectorName, |
||
306 | |||
307 | /// Code completion within a type-qualifier list. |
||
308 | CCC_TypeQualifiers, |
||
309 | |||
310 | /// Code completion in a parenthesized expression, which means that |
||
311 | /// we may also have types here in C and Objective-C (as well as in C++). |
||
312 | CCC_ParenthesizedExpression, |
||
313 | |||
314 | /// Code completion where an Objective-C instance message is |
||
315 | /// expected. |
||
316 | CCC_ObjCInstanceMessage, |
||
317 | |||
318 | /// Code completion where an Objective-C class message is expected. |
||
319 | CCC_ObjCClassMessage, |
||
320 | |||
321 | /// Code completion where the name of an Objective-C class is |
||
322 | /// expected. |
||
323 | CCC_ObjCInterfaceName, |
||
324 | |||
325 | /// Code completion where an Objective-C category name is expected. |
||
326 | CCC_ObjCCategoryName, |
||
327 | |||
328 | /// Code completion inside the filename part of a #include directive. |
||
329 | CCC_IncludedFile, |
||
330 | |||
331 | /// Code completion of an attribute name. |
||
332 | CCC_Attribute, |
||
333 | |||
334 | /// An unknown context, in which we are recovering from a parsing |
||
335 | /// error and don't know which completions we should give. |
||
336 | CCC_Recovery |
||
337 | }; |
||
338 | |||
339 | using VisitedContextSet = llvm::SmallPtrSet<DeclContext *, 8>; |
||
340 | |||
341 | private: |
||
342 | Kind CCKind; |
||
343 | |||
344 | /// Indicates whether we are completing a name of a using declaration, e.g. |
||
345 | /// using ^; |
||
346 | /// using a::^; |
||
347 | bool IsUsingDeclaration; |
||
348 | |||
349 | /// The type that would prefer to see at this point (e.g., the type |
||
350 | /// of an initializer or function parameter). |
||
351 | QualType PreferredType; |
||
352 | |||
353 | /// The type of the base object in a member access expression. |
||
354 | QualType BaseType; |
||
355 | |||
356 | /// The identifiers for Objective-C selector parts. |
||
357 | ArrayRef<IdentifierInfo *> SelIdents; |
||
358 | |||
359 | /// The scope specifier that comes before the completion token e.g. |
||
360 | /// "a::b::" |
||
361 | std::optional<CXXScopeSpec> ScopeSpecifier; |
||
362 | |||
363 | /// A set of declaration contexts visited by Sema when doing lookup for |
||
364 | /// code completion. |
||
365 | VisitedContextSet VisitedContexts; |
||
366 | |||
367 | public: |
||
368 | /// Construct a new code-completion context of the given kind. |
||
369 | CodeCompletionContext(Kind CCKind) |
||
370 | : CCKind(CCKind), IsUsingDeclaration(false), SelIdents(std::nullopt) {} |
||
371 | |||
372 | /// Construct a new code-completion context of the given kind. |
||
373 | CodeCompletionContext(Kind CCKind, QualType T, |
||
374 | ArrayRef<IdentifierInfo *> SelIdents = std::nullopt) |
||
375 | : CCKind(CCKind), IsUsingDeclaration(false), SelIdents(SelIdents) { |
||
376 | if (CCKind == CCC_DotMemberAccess || CCKind == CCC_ArrowMemberAccess || |
||
377 | CCKind == CCC_ObjCPropertyAccess || CCKind == CCC_ObjCClassMessage || |
||
378 | CCKind == CCC_ObjCInstanceMessage) |
||
379 | BaseType = T; |
||
380 | else |
||
381 | PreferredType = T; |
||
382 | } |
||
383 | |||
384 | bool isUsingDeclaration() const { return IsUsingDeclaration; } |
||
385 | void setIsUsingDeclaration(bool V) { IsUsingDeclaration = V; } |
||
386 | |||
387 | /// Retrieve the kind of code-completion context. |
||
388 | Kind getKind() const { return CCKind; } |
||
389 | |||
390 | /// Retrieve the type that this expression would prefer to have, e.g., |
||
391 | /// if the expression is a variable initializer or a function argument, the |
||
392 | /// type of the corresponding variable or function parameter. |
||
393 | QualType getPreferredType() const { return PreferredType; } |
||
394 | void setPreferredType(QualType T) { PreferredType = T; } |
||
395 | |||
396 | /// Retrieve the type of the base object in a member-access |
||
397 | /// expression. |
||
398 | QualType getBaseType() const { return BaseType; } |
||
399 | |||
400 | /// Retrieve the Objective-C selector identifiers. |
||
401 | ArrayRef<IdentifierInfo *> getSelIdents() const { return SelIdents; } |
||
402 | |||
403 | /// Determines whether we want C++ constructors as results within this |
||
404 | /// context. |
||
405 | bool wantConstructorResults() const; |
||
406 | |||
407 | /// Sets the scope specifier that comes before the completion token. |
||
408 | /// This is expected to be set in code completions on qualfied specifiers |
||
409 | /// (e.g. "a::b::"). |
||
410 | void setCXXScopeSpecifier(CXXScopeSpec SS) { |
||
411 | this->ScopeSpecifier = std::move(SS); |
||
412 | } |
||
413 | |||
414 | /// Adds a visited context. |
||
415 | void addVisitedContext(DeclContext *Ctx) { |
||
416 | VisitedContexts.insert(Ctx); |
||
417 | } |
||
418 | |||
419 | /// Retrieves all visited contexts. |
||
420 | const VisitedContextSet &getVisitedContexts() const { |
||
421 | return VisitedContexts; |
||
422 | } |
||
423 | |||
424 | std::optional<const CXXScopeSpec *> getCXXScopeSpecifier() { |
||
425 | if (ScopeSpecifier) |
||
426 | return &*ScopeSpecifier; |
||
427 | return std::nullopt; |
||
428 | } |
||
429 | }; |
||
430 | |||
431 | /// Get string representation of \p Kind, useful for debugging. |
||
432 | llvm::StringRef getCompletionKindString(CodeCompletionContext::Kind Kind); |
||
433 | |||
434 | /// A "string" used to describe how code completion can |
||
435 | /// be performed for an entity. |
||
436 | /// |
||
437 | /// A code completion string typically shows how a particular entity can be |
||
438 | /// used. For example, the code completion string for a function would show |
||
439 | /// the syntax to call it, including the parentheses, placeholders for the |
||
440 | /// arguments, etc. |
||
441 | class CodeCompletionString { |
||
442 | public: |
||
443 | /// The different kinds of "chunks" that can occur within a code |
||
444 | /// completion string. |
||
445 | enum ChunkKind { |
||
446 | /// The piece of text that the user is expected to type to |
||
447 | /// match the code-completion string, typically a keyword or the name of a |
||
448 | /// declarator or macro. |
||
449 | CK_TypedText, |
||
450 | |||
451 | /// A piece of text that should be placed in the buffer, e.g., |
||
452 | /// parentheses or a comma in a function call. |
||
453 | CK_Text, |
||
454 | |||
455 | /// A code completion string that is entirely optional. For example, |
||
456 | /// an optional code completion string that describes the default arguments |
||
457 | /// in a function call. |
||
458 | CK_Optional, |
||
459 | |||
460 | /// A string that acts as a placeholder for, e.g., a function |
||
461 | /// call argument. |
||
462 | CK_Placeholder, |
||
463 | |||
464 | /// A piece of text that describes something about the result but |
||
465 | /// should not be inserted into the buffer. |
||
466 | CK_Informative, |
||
467 | /// A piece of text that describes the type of an entity or, for |
||
468 | /// functions and methods, the return type. |
||
469 | CK_ResultType, |
||
470 | |||
471 | /// A piece of text that describes the parameter that corresponds |
||
472 | /// to the code-completion location within a function call, message send, |
||
473 | /// macro invocation, etc. |
||
474 | CK_CurrentParameter, |
||
475 | |||
476 | /// A left parenthesis ('('). |
||
477 | CK_LeftParen, |
||
478 | |||
479 | /// A right parenthesis (')'). |
||
480 | CK_RightParen, |
||
481 | |||
482 | /// A left bracket ('['). |
||
483 | CK_LeftBracket, |
||
484 | |||
485 | /// A right bracket (']'). |
||
486 | CK_RightBracket, |
||
487 | |||
488 | /// A left brace ('{'). |
||
489 | CK_LeftBrace, |
||
490 | |||
491 | /// A right brace ('}'). |
||
492 | CK_RightBrace, |
||
493 | |||
494 | /// A left angle bracket ('<'). |
||
495 | CK_LeftAngle, |
||
496 | |||
497 | /// A right angle bracket ('>'). |
||
498 | CK_RightAngle, |
||
499 | |||
500 | /// A comma separator (','). |
||
501 | CK_Comma, |
||
502 | |||
503 | /// A colon (':'). |
||
504 | CK_Colon, |
||
505 | |||
506 | /// A semicolon (';'). |
||
507 | CK_SemiColon, |
||
508 | |||
509 | /// An '=' sign. |
||
510 | CK_Equal, |
||
511 | |||
512 | /// Horizontal whitespace (' '). |
||
513 | CK_HorizontalSpace, |
||
514 | |||
515 | /// Vertical whitespace ('\\n' or '\\r\\n', depending on the |
||
516 | /// platform). |
||
517 | CK_VerticalSpace |
||
518 | }; |
||
519 | |||
520 | /// One piece of the code completion string. |
||
521 | struct Chunk { |
||
522 | /// The kind of data stored in this piece of the code completion |
||
523 | /// string. |
||
524 | ChunkKind Kind = CK_Text; |
||
525 | |||
526 | union { |
||
527 | /// The text string associated with a CK_Text, CK_Placeholder, |
||
528 | /// CK_Informative, or CK_Comma chunk. |
||
529 | /// The string is owned by the chunk and will be deallocated |
||
530 | /// (with delete[]) when the chunk is destroyed. |
||
531 | const char *Text; |
||
532 | |||
533 | /// The code completion string associated with a CK_Optional chunk. |
||
534 | /// The optional code completion string is owned by the chunk, and will |
||
535 | /// be deallocated (with delete) when the chunk is destroyed. |
||
536 | CodeCompletionString *Optional; |
||
537 | }; |
||
538 | |||
539 | Chunk() : Text(nullptr) {} |
||
540 | |||
541 | explicit Chunk(ChunkKind Kind, const char *Text = ""); |
||
542 | |||
543 | /// Create a new text chunk. |
||
544 | static Chunk CreateText(const char *Text); |
||
545 | |||
546 | /// Create a new optional chunk. |
||
547 | static Chunk CreateOptional(CodeCompletionString *Optional); |
||
548 | |||
549 | /// Create a new placeholder chunk. |
||
550 | static Chunk CreatePlaceholder(const char *Placeholder); |
||
551 | |||
552 | /// Create a new informative chunk. |
||
553 | static Chunk CreateInformative(const char *Informative); |
||
554 | |||
555 | /// Create a new result type chunk. |
||
556 | static Chunk CreateResultType(const char *ResultType); |
||
557 | |||
558 | /// Create a new current-parameter chunk. |
||
559 | static Chunk CreateCurrentParameter(const char *CurrentParameter); |
||
560 | }; |
||
561 | |||
562 | private: |
||
563 | friend class CodeCompletionBuilder; |
||
564 | friend class CodeCompletionResult; |
||
565 | |||
566 | /// The number of chunks stored in this string. |
||
567 | unsigned NumChunks : 16; |
||
568 | |||
569 | /// The number of annotations for this code-completion result. |
||
570 | unsigned NumAnnotations : 16; |
||
571 | |||
572 | /// The priority of this code-completion string. |
||
573 | unsigned Priority : 16; |
||
574 | |||
575 | /// The availability of this code-completion result. |
||
576 | unsigned Availability : 2; |
||
577 | |||
578 | /// The name of the parent context. |
||
579 | StringRef ParentName; |
||
580 | |||
581 | /// A brief documentation comment attached to the declaration of |
||
582 | /// entity being completed by this result. |
||
583 | const char *BriefComment; |
||
584 | |||
585 | CodeCompletionString(const Chunk *Chunks, unsigned NumChunks, |
||
586 | unsigned Priority, CXAvailabilityKind Availability, |
||
587 | const char **Annotations, unsigned NumAnnotations, |
||
588 | StringRef ParentName, |
||
589 | const char *BriefComment); |
||
590 | ~CodeCompletionString() = default; |
||
591 | |||
592 | public: |
||
593 | CodeCompletionString(const CodeCompletionString &) = delete; |
||
594 | CodeCompletionString &operator=(const CodeCompletionString &) = delete; |
||
595 | |||
596 | using iterator = const Chunk *; |
||
597 | |||
598 | iterator begin() const { return reinterpret_cast<const Chunk *>(this + 1); } |
||
599 | iterator end() const { return begin() + NumChunks; } |
||
600 | bool empty() const { return NumChunks == 0; } |
||
601 | unsigned size() const { return NumChunks; } |
||
602 | |||
603 | const Chunk &operator[](unsigned I) const { |
||
604 | assert(I < size() && "Chunk index out-of-range"); |
||
605 | return begin()[I]; |
||
606 | } |
||
607 | |||
608 | /// Returns the text in the first TypedText chunk. |
||
609 | const char *getTypedText() const; |
||
610 | |||
611 | /// Returns the combined text from all TypedText chunks. |
||
612 | std::string getAllTypedText() const; |
||
613 | |||
614 | /// Retrieve the priority of this code completion result. |
||
615 | unsigned getPriority() const { return Priority; } |
||
616 | |||
617 | /// Retrieve the availability of this code completion result. |
||
618 | unsigned getAvailability() const { return Availability; } |
||
619 | |||
620 | /// Retrieve the number of annotations for this code completion result. |
||
621 | unsigned getAnnotationCount() const; |
||
622 | |||
623 | /// Retrieve the annotation string specified by \c AnnotationNr. |
||
624 | const char *getAnnotation(unsigned AnnotationNr) const; |
||
625 | |||
626 | /// Retrieve the name of the parent context. |
||
627 | StringRef getParentContextName() const { |
||
628 | return ParentName; |
||
629 | } |
||
630 | |||
631 | const char *getBriefComment() const { |
||
632 | return BriefComment; |
||
633 | } |
||
634 | |||
635 | /// Retrieve a string representation of the code completion string, |
||
636 | /// which is mainly useful for debugging. |
||
637 | std::string getAsString() const; |
||
638 | }; |
||
639 | |||
640 | /// An allocator used specifically for the purpose of code completion. |
||
641 | class CodeCompletionAllocator : public llvm::BumpPtrAllocator { |
||
642 | public: |
||
643 | /// Copy the given string into this allocator. |
||
644 | const char *CopyString(const Twine &String); |
||
645 | }; |
||
646 | |||
647 | /// Allocator for a cached set of global code completions. |
||
648 | class GlobalCodeCompletionAllocator : public CodeCompletionAllocator {}; |
||
649 | |||
650 | class CodeCompletionTUInfo { |
||
651 | llvm::DenseMap<const DeclContext *, StringRef> ParentNames; |
||
652 | std::shared_ptr<GlobalCodeCompletionAllocator> AllocatorRef; |
||
653 | |||
654 | public: |
||
655 | explicit CodeCompletionTUInfo( |
||
656 | std::shared_ptr<GlobalCodeCompletionAllocator> Allocator) |
||
657 | : AllocatorRef(std::move(Allocator)) {} |
||
658 | |||
659 | std::shared_ptr<GlobalCodeCompletionAllocator> getAllocatorRef() const { |
||
660 | return AllocatorRef; |
||
661 | } |
||
662 | |||
663 | CodeCompletionAllocator &getAllocator() const { |
||
664 | assert(AllocatorRef); |
||
665 | return *AllocatorRef; |
||
666 | } |
||
667 | |||
668 | StringRef getParentName(const DeclContext *DC); |
||
669 | }; |
||
670 | |||
671 | } // namespace clang |
||
672 | |||
673 | namespace clang { |
||
674 | |||
675 | /// A builder class used to construct new code-completion strings. |
||
676 | class CodeCompletionBuilder { |
||
677 | public: |
||
678 | using Chunk = CodeCompletionString::Chunk; |
||
679 | |||
680 | private: |
||
681 | CodeCompletionAllocator &Allocator; |
||
682 | CodeCompletionTUInfo &CCTUInfo; |
||
683 | unsigned Priority = 0; |
||
684 | CXAvailabilityKind Availability = CXAvailability_Available; |
||
685 | StringRef ParentName; |
||
686 | const char *BriefComment = nullptr; |
||
687 | |||
688 | /// The chunks stored in this string. |
||
689 | SmallVector<Chunk, 4> Chunks; |
||
690 | |||
691 | SmallVector<const char *, 2> Annotations; |
||
692 | |||
693 | public: |
||
694 | CodeCompletionBuilder(CodeCompletionAllocator &Allocator, |
||
695 | CodeCompletionTUInfo &CCTUInfo) |
||
696 | : Allocator(Allocator), CCTUInfo(CCTUInfo) {} |
||
697 | |||
698 | CodeCompletionBuilder(CodeCompletionAllocator &Allocator, |
||
699 | CodeCompletionTUInfo &CCTUInfo, |
||
700 | unsigned Priority, CXAvailabilityKind Availability) |
||
701 | : Allocator(Allocator), CCTUInfo(CCTUInfo), Priority(Priority), |
||
702 | Availability(Availability) {} |
||
703 | |||
704 | /// Retrieve the allocator into which the code completion |
||
705 | /// strings should be allocated. |
||
706 | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
||
707 | |||
708 | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } |
||
709 | |||
710 | /// Take the resulting completion string. |
||
711 | /// |
||
712 | /// This operation can only be performed once. |
||
713 | CodeCompletionString *TakeString(); |
||
714 | |||
715 | /// Add a new typed-text chunk. |
||
716 | void AddTypedTextChunk(const char *Text); |
||
717 | |||
718 | /// Add a new text chunk. |
||
719 | void AddTextChunk(const char *Text); |
||
720 | |||
721 | /// Add a new optional chunk. |
||
722 | void AddOptionalChunk(CodeCompletionString *Optional); |
||
723 | |||
724 | /// Add a new placeholder chunk. |
||
725 | void AddPlaceholderChunk(const char *Placeholder); |
||
726 | |||
727 | /// Add a new informative chunk. |
||
728 | void AddInformativeChunk(const char *Text); |
||
729 | |||
730 | /// Add a new result-type chunk. |
||
731 | void AddResultTypeChunk(const char *ResultType); |
||
732 | |||
733 | /// Add a new current-parameter chunk. |
||
734 | void AddCurrentParameterChunk(const char *CurrentParameter); |
||
735 | |||
736 | /// Add a new chunk. |
||
737 | void AddChunk(CodeCompletionString::ChunkKind CK, const char *Text = ""); |
||
738 | |||
739 | void AddAnnotation(const char *A) { Annotations.push_back(A); } |
||
740 | |||
741 | /// Add the parent context information to this code completion. |
||
742 | void addParentContext(const DeclContext *DC); |
||
743 | |||
744 | const char *getBriefComment() const { return BriefComment; } |
||
745 | void addBriefComment(StringRef Comment); |
||
746 | |||
747 | StringRef getParentName() const { return ParentName; } |
||
748 | }; |
||
749 | |||
750 | /// Captures a result of code completion. |
||
751 | class CodeCompletionResult { |
||
752 | public: |
||
753 | /// Describes the kind of result generated. |
||
754 | enum ResultKind { |
||
755 | /// Refers to a declaration. |
||
756 | RK_Declaration = 0, |
||
757 | |||
758 | /// Refers to a keyword or symbol. |
||
759 | RK_Keyword, |
||
760 | |||
761 | /// Refers to a macro. |
||
762 | RK_Macro, |
||
763 | |||
764 | /// Refers to a precomputed pattern. |
||
765 | RK_Pattern |
||
766 | }; |
||
767 | |||
768 | /// When Kind == RK_Declaration or RK_Pattern, the declaration we are |
||
769 | /// referring to. In the latter case, the declaration might be NULL. |
||
770 | const NamedDecl *Declaration = nullptr; |
||
771 | |||
772 | union { |
||
773 | /// When Kind == RK_Keyword, the string representing the keyword |
||
774 | /// or symbol's spelling. |
||
775 | const char *Keyword; |
||
776 | |||
777 | /// When Kind == RK_Pattern, the code-completion string that |
||
778 | /// describes the completion text to insert. |
||
779 | CodeCompletionString *Pattern; |
||
780 | |||
781 | /// When Kind == RK_Macro, the identifier that refers to a macro. |
||
782 | const IdentifierInfo *Macro; |
||
783 | }; |
||
784 | |||
785 | /// The priority of this particular code-completion result. |
||
786 | unsigned Priority; |
||
787 | |||
788 | /// Specifies which parameter (of a function, Objective-C method, |
||
789 | /// macro, etc.) we should start with when formatting the result. |
||
790 | unsigned StartParameter = 0; |
||
791 | |||
792 | /// The kind of result stored here. |
||
793 | ResultKind Kind; |
||
794 | |||
795 | /// The cursor kind that describes this result. |
||
796 | CXCursorKind CursorKind; |
||
797 | |||
798 | /// The availability of this result. |
||
799 | CXAvailabilityKind Availability = CXAvailability_Available; |
||
800 | |||
801 | /// Fix-its that *must* be applied before inserting the text for the |
||
802 | /// corresponding completion. |
||
803 | /// |
||
804 | /// By default, CodeCompletionBuilder only returns completions with empty |
||
805 | /// fix-its. Extra completions with non-empty fix-its should be explicitly |
||
806 | /// requested by setting CompletionOptions::IncludeFixIts. |
||
807 | /// |
||
808 | /// For the clients to be able to compute position of the cursor after |
||
809 | /// applying fix-its, the following conditions are guaranteed to hold for |
||
810 | /// RemoveRange of the stored fix-its: |
||
811 | /// - Ranges in the fix-its are guaranteed to never contain the completion |
||
812 | /// point (or identifier under completion point, if any) inside them, except |
||
813 | /// at the start or at the end of the range. |
||
814 | /// - If a fix-it range starts or ends with completion point (or starts or |
||
815 | /// ends after the identifier under completion point), it will contain at |
||
816 | /// least one character. It allows to unambiguously recompute completion |
||
817 | /// point after applying the fix-it. |
||
818 | /// |
||
819 | /// The intuition is that provided fix-its change code around the identifier |
||
820 | /// we complete, but are not allowed to touch the identifier itself or the |
||
821 | /// completion point. One example of completions with corrections are the ones |
||
822 | /// replacing '.' with '->' and vice versa: |
||
823 | /// |
||
824 | /// std::unique_ptr<std::vector<int>> vec_ptr; |
||
825 | /// In 'vec_ptr.^', one of the completions is 'push_back', it requires |
||
826 | /// replacing '.' with '->'. |
||
827 | /// In 'vec_ptr->^', one of the completions is 'release', it requires |
||
828 | /// replacing '->' with '.'. |
||
829 | std::vector<FixItHint> FixIts; |
||
830 | |||
831 | /// Whether this result is hidden by another name. |
||
832 | bool Hidden : 1; |
||
833 | |||
834 | /// Whether this is a class member from base class. |
||
835 | bool InBaseClass : 1; |
||
836 | |||
837 | /// Whether this result was found via lookup into a base class. |
||
838 | bool QualifierIsInformative : 1; |
||
839 | |||
840 | /// Whether this declaration is the beginning of a |
||
841 | /// nested-name-specifier and, therefore, should be followed by '::'. |
||
842 | bool StartsNestedNameSpecifier : 1; |
||
843 | |||
844 | /// Whether all parameters (of a function, Objective-C |
||
845 | /// method, etc.) should be considered "informative". |
||
846 | bool AllParametersAreInformative : 1; |
||
847 | |||
848 | /// Whether we're completing a declaration of the given entity, |
||
849 | /// rather than a use of that entity. |
||
850 | bool DeclaringEntity : 1; |
||
851 | |||
852 | /// When completing a function, whether it can be a call. This will usually be |
||
853 | /// true, but we have some heuristics, e.g. when a pointer to a non-static |
||
854 | /// member function is completed outside of that class' scope, it can never |
||
855 | /// be a call. |
||
856 | bool FunctionCanBeCall : 1; |
||
857 | |||
858 | /// If the result should have a nested-name-specifier, this is it. |
||
859 | /// When \c QualifierIsInformative, the nested-name-specifier is |
||
860 | /// informative rather than required. |
||
861 | NestedNameSpecifier *Qualifier = nullptr; |
||
862 | |||
863 | /// If this Decl was unshadowed by using declaration, this can store a |
||
864 | /// pointer to the UsingShadowDecl which was used in the unshadowing process. |
||
865 | /// This information can be used to uprank CodeCompletionResults / which have |
||
866 | /// corresponding `using decl::qualified::name;` nearby. |
||
867 | const UsingShadowDecl *ShadowDecl = nullptr; |
||
868 | |||
869 | /// If the result is RK_Macro, this can store the information about the macro |
||
870 | /// definition. This should be set in most cases but can be missing when |
||
871 | /// the macro has been undefined. |
||
872 | const MacroInfo *MacroDefInfo = nullptr; |
||
873 | |||
874 | /// Build a result that refers to a declaration. |
||
875 | CodeCompletionResult(const NamedDecl *Declaration, unsigned Priority, |
||
876 | NestedNameSpecifier *Qualifier = nullptr, |
||
877 | bool QualifierIsInformative = false, |
||
878 | bool Accessible = true, |
||
879 | std::vector<FixItHint> FixIts = std::vector<FixItHint>()) |
||
880 | : Declaration(Declaration), Priority(Priority), Kind(RK_Declaration), |
||
881 | FixIts(std::move(FixIts)), Hidden(false), InBaseClass(false), |
||
882 | QualifierIsInformative(QualifierIsInformative), |
||
883 | StartsNestedNameSpecifier(false), AllParametersAreInformative(false), |
||
884 | DeclaringEntity(false), FunctionCanBeCall(true), Qualifier(Qualifier) { |
||
885 | // FIXME: Add assert to check FixIts range requirements. |
||
886 | computeCursorKindAndAvailability(Accessible); |
||
887 | } |
||
888 | |||
889 | /// Build a result that refers to a keyword or symbol. |
||
890 | CodeCompletionResult(const char *Keyword, unsigned Priority = CCP_Keyword) |
||
891 | : Keyword(Keyword), Priority(Priority), Kind(RK_Keyword), |
||
892 | CursorKind(CXCursor_NotImplemented), Hidden(false), InBaseClass(false), |
||
893 | QualifierIsInformative(false), StartsNestedNameSpecifier(false), |
||
894 | AllParametersAreInformative(false), DeclaringEntity(false), |
||
895 | FunctionCanBeCall(true) {} |
||
896 | |||
897 | /// Build a result that refers to a macro. |
||
898 | CodeCompletionResult(const IdentifierInfo *Macro, |
||
899 | const MacroInfo *MI = nullptr, |
||
900 | unsigned Priority = CCP_Macro) |
||
901 | : Macro(Macro), Priority(Priority), Kind(RK_Macro), |
||
902 | CursorKind(CXCursor_MacroDefinition), Hidden(false), InBaseClass(false), |
||
903 | QualifierIsInformative(false), StartsNestedNameSpecifier(false), |
||
904 | AllParametersAreInformative(false), DeclaringEntity(false), |
||
905 | FunctionCanBeCall(true), MacroDefInfo(MI) {} |
||
906 | |||
907 | /// Build a result that refers to a pattern. |
||
908 | CodeCompletionResult( |
||
909 | CodeCompletionString *Pattern, unsigned Priority = CCP_CodePattern, |
||
910 | CXCursorKind CursorKind = CXCursor_NotImplemented, |
||
911 | CXAvailabilityKind Availability = CXAvailability_Available, |
||
912 | const NamedDecl *D = nullptr) |
||
913 | : Declaration(D), Pattern(Pattern), Priority(Priority), Kind(RK_Pattern), |
||
914 | CursorKind(CursorKind), Availability(Availability), Hidden(false), |
||
915 | InBaseClass(false), QualifierIsInformative(false), |
||
916 | StartsNestedNameSpecifier(false), AllParametersAreInformative(false), |
||
917 | DeclaringEntity(false), FunctionCanBeCall(true) {} |
||
918 | |||
919 | /// Build a result that refers to a pattern with an associated |
||
920 | /// declaration. |
||
921 | CodeCompletionResult(CodeCompletionString *Pattern, const NamedDecl *D, |
||
922 | unsigned Priority) |
||
923 | : Declaration(D), Pattern(Pattern), Priority(Priority), Kind(RK_Pattern), |
||
924 | Hidden(false), InBaseClass(false), QualifierIsInformative(false), |
||
925 | StartsNestedNameSpecifier(false), AllParametersAreInformative(false), |
||
926 | DeclaringEntity(false), FunctionCanBeCall(true) { |
||
927 | computeCursorKindAndAvailability(); |
||
928 | } |
||
929 | |||
930 | /// Retrieve the declaration stored in this result. This might be nullptr if |
||
931 | /// Kind is RK_Pattern. |
||
932 | const NamedDecl *getDeclaration() const { |
||
933 | assert(((Kind == RK_Declaration) || (Kind == RK_Pattern)) && |
||
934 | "Not a declaration or pattern result"); |
||
935 | return Declaration; |
||
936 | } |
||
937 | |||
938 | /// Retrieve the keyword stored in this result. |
||
939 | const char *getKeyword() const { |
||
940 | assert(Kind == RK_Keyword && "Not a keyword result"); |
||
941 | return Keyword; |
||
942 | } |
||
943 | |||
944 | /// Create a new code-completion string that describes how to insert |
||
945 | /// this result into a program. |
||
946 | /// |
||
947 | /// \param S The semantic analysis that created the result. |
||
948 | /// |
||
949 | /// \param Allocator The allocator that will be used to allocate the |
||
950 | /// string itself. |
||
951 | CodeCompletionString *CreateCodeCompletionString(Sema &S, |
||
952 | const CodeCompletionContext &CCContext, |
||
953 | CodeCompletionAllocator &Allocator, |
||
954 | CodeCompletionTUInfo &CCTUInfo, |
||
955 | bool IncludeBriefComments); |
||
956 | CodeCompletionString *CreateCodeCompletionString(ASTContext &Ctx, |
||
957 | Preprocessor &PP, |
||
958 | const CodeCompletionContext &CCContext, |
||
959 | CodeCompletionAllocator &Allocator, |
||
960 | CodeCompletionTUInfo &CCTUInfo, |
||
961 | bool IncludeBriefComments); |
||
962 | /// Creates a new code-completion string for the macro result. Similar to the |
||
963 | /// above overloads, except this only requires preprocessor information. |
||
964 | /// The result kind must be `RK_Macro`. |
||
965 | CodeCompletionString * |
||
966 | CreateCodeCompletionStringForMacro(Preprocessor &PP, |
||
967 | CodeCompletionAllocator &Allocator, |
||
968 | CodeCompletionTUInfo &CCTUInfo); |
||
969 | |||
970 | CodeCompletionString *createCodeCompletionStringForDecl( |
||
971 | Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result, |
||
972 | bool IncludeBriefComments, const CodeCompletionContext &CCContext, |
||
973 | PrintingPolicy &Policy); |
||
974 | |||
975 | CodeCompletionString *createCodeCompletionStringForOverride( |
||
976 | Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result, |
||
977 | bool IncludeBriefComments, const CodeCompletionContext &CCContext, |
||
978 | PrintingPolicy &Policy); |
||
979 | |||
980 | /// Retrieve the name that should be used to order a result. |
||
981 | /// |
||
982 | /// If the name needs to be constructed as a string, that string will be |
||
983 | /// saved into Saved and the returned StringRef will refer to it. |
||
984 | StringRef getOrderedName(std::string &Saved) const; |
||
985 | |||
986 | private: |
||
987 | void computeCursorKindAndAvailability(bool Accessible = true); |
||
988 | }; |
||
989 | |||
990 | bool operator<(const CodeCompletionResult &X, const CodeCompletionResult &Y); |
||
991 | |||
992 | inline bool operator>(const CodeCompletionResult &X, |
||
993 | const CodeCompletionResult &Y) { |
||
994 | return Y < X; |
||
995 | } |
||
996 | |||
997 | inline bool operator<=(const CodeCompletionResult &X, |
||
998 | const CodeCompletionResult &Y) { |
||
999 | return !(Y < X); |
||
1000 | } |
||
1001 | |||
1002 | inline bool operator>=(const CodeCompletionResult &X, |
||
1003 | const CodeCompletionResult &Y) { |
||
1004 | return !(X < Y); |
||
1005 | } |
||
1006 | |||
1007 | /// Abstract interface for a consumer of code-completion |
||
1008 | /// information. |
||
1009 | class CodeCompleteConsumer { |
||
1010 | protected: |
||
1011 | const CodeCompleteOptions CodeCompleteOpts; |
||
1012 | |||
1013 | public: |
||
1014 | class OverloadCandidate { |
||
1015 | public: |
||
1016 | /// Describes the type of overload candidate. |
||
1017 | enum CandidateKind { |
||
1018 | /// The candidate is a function declaration. |
||
1019 | CK_Function, |
||
1020 | |||
1021 | /// The candidate is a function template, arguments are being completed. |
||
1022 | CK_FunctionTemplate, |
||
1023 | |||
1024 | /// The "candidate" is actually a variable, expression, or block |
||
1025 | /// for which we only have a function prototype. |
||
1026 | CK_FunctionType, |
||
1027 | |||
1028 | /// The candidate is a variable or expression of function type |
||
1029 | /// for which we have the location of the prototype declaration. |
||
1030 | CK_FunctionProtoTypeLoc, |
||
1031 | |||
1032 | /// The candidate is a template, template arguments are being completed. |
||
1033 | CK_Template, |
||
1034 | |||
1035 | /// The candidate is aggregate initialization of a record type. |
||
1036 | CK_Aggregate, |
||
1037 | }; |
||
1038 | |||
1039 | private: |
||
1040 | /// The kind of overload candidate. |
||
1041 | CandidateKind Kind; |
||
1042 | |||
1043 | union { |
||
1044 | /// The function overload candidate, available when |
||
1045 | /// Kind == CK_Function. |
||
1046 | FunctionDecl *Function; |
||
1047 | |||
1048 | /// The function template overload candidate, available when |
||
1049 | /// Kind == CK_FunctionTemplate. |
||
1050 | FunctionTemplateDecl *FunctionTemplate; |
||
1051 | |||
1052 | /// The function type that describes the entity being called, |
||
1053 | /// when Kind == CK_FunctionType. |
||
1054 | const FunctionType *Type; |
||
1055 | |||
1056 | /// The location of the function prototype that describes the entity being |
||
1057 | /// called, when Kind == CK_FunctionProtoTypeLoc. |
||
1058 | FunctionProtoTypeLoc ProtoTypeLoc; |
||
1059 | |||
1060 | /// The template overload candidate, available when |
||
1061 | /// Kind == CK_Template. |
||
1062 | const TemplateDecl *Template; |
||
1063 | |||
1064 | /// The class being aggregate-initialized, |
||
1065 | /// when Kind == CK_Aggregate |
||
1066 | const RecordDecl *AggregateType; |
||
1067 | }; |
||
1068 | |||
1069 | public: |
||
1070 | OverloadCandidate(FunctionDecl *Function) |
||
1071 | : Kind(CK_Function), Function(Function) { |
||
1072 | assert(Function != nullptr); |
||
1073 | } |
||
1074 | |||
1075 | OverloadCandidate(FunctionTemplateDecl *FunctionTemplateDecl) |
||
1076 | : Kind(CK_FunctionTemplate), FunctionTemplate(FunctionTemplateDecl) { |
||
1077 | assert(FunctionTemplateDecl != nullptr); |
||
1078 | } |
||
1079 | |||
1080 | OverloadCandidate(const FunctionType *Type) |
||
1081 | : Kind(CK_FunctionType), Type(Type) { |
||
1082 | assert(Type != nullptr); |
||
1083 | } |
||
1084 | |||
1085 | OverloadCandidate(FunctionProtoTypeLoc Prototype) |
||
1086 | : Kind(CK_FunctionProtoTypeLoc), ProtoTypeLoc(Prototype) { |
||
1087 | assert(!Prototype.isNull()); |
||
1088 | } |
||
1089 | |||
1090 | OverloadCandidate(const RecordDecl *Aggregate) |
||
1091 | : Kind(CK_Aggregate), AggregateType(Aggregate) { |
||
1092 | assert(Aggregate != nullptr); |
||
1093 | } |
||
1094 | |||
1095 | OverloadCandidate(const TemplateDecl *Template) |
||
1096 | : Kind(CK_Template), Template(Template) {} |
||
1097 | |||
1098 | /// Determine the kind of overload candidate. |
||
1099 | CandidateKind getKind() const { return Kind; } |
||
1100 | |||
1101 | /// Retrieve the function overload candidate or the templated |
||
1102 | /// function declaration for a function template. |
||
1103 | FunctionDecl *getFunction() const; |
||
1104 | |||
1105 | /// Retrieve the function template overload candidate. |
||
1106 | FunctionTemplateDecl *getFunctionTemplate() const { |
||
1107 | assert(getKind() == CK_FunctionTemplate && "Not a function template"); |
||
1108 | return FunctionTemplate; |
||
1109 | } |
||
1110 | |||
1111 | /// Retrieve the function type of the entity, regardless of how the |
||
1112 | /// function is stored. |
||
1113 | const FunctionType *getFunctionType() const; |
||
1114 | |||
1115 | /// Retrieve the function ProtoTypeLoc candidate. |
||
1116 | /// This can be called for any Kind, but returns null for kinds |
||
1117 | /// other than CK_FunctionProtoTypeLoc. |
||
1118 | const FunctionProtoTypeLoc getFunctionProtoTypeLoc() const; |
||
1119 | |||
1120 | const TemplateDecl *getTemplate() const { |
||
1121 | assert(getKind() == CK_Template && "Not a template"); |
||
1122 | return Template; |
||
1123 | } |
||
1124 | |||
1125 | /// Retrieve the aggregate type being initialized. |
||
1126 | const RecordDecl *getAggregate() const { |
||
1127 | assert(getKind() == CK_Aggregate); |
||
1128 | return AggregateType; |
||
1129 | } |
||
1130 | |||
1131 | /// Get the number of parameters in this signature. |
||
1132 | unsigned getNumParams() const; |
||
1133 | |||
1134 | /// Get the type of the Nth parameter. |
||
1135 | /// Returns null if the type is unknown or N is out of range. |
||
1136 | QualType getParamType(unsigned N) const; |
||
1137 | |||
1138 | /// Get the declaration of the Nth parameter. |
||
1139 | /// Returns null if the decl is unknown or N is out of range. |
||
1140 | const NamedDecl *getParamDecl(unsigned N) const; |
||
1141 | |||
1142 | /// Create a new code-completion string that describes the function |
||
1143 | /// signature of this overload candidate. |
||
1144 | CodeCompletionString * |
||
1145 | CreateSignatureString(unsigned CurrentArg, Sema &S, |
||
1146 | CodeCompletionAllocator &Allocator, |
||
1147 | CodeCompletionTUInfo &CCTUInfo, |
||
1148 | bool IncludeBriefComments, bool Braced) const; |
||
1149 | }; |
||
1150 | |||
1151 | CodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts) |
||
1152 | : CodeCompleteOpts(CodeCompleteOpts) {} |
||
1153 | |||
1154 | /// Whether the code-completion consumer wants to see macros. |
||
1155 | bool includeMacros() const { |
||
1156 | return CodeCompleteOpts.IncludeMacros; |
||
1157 | } |
||
1158 | |||
1159 | /// Whether the code-completion consumer wants to see code patterns. |
||
1160 | bool includeCodePatterns() const { |
||
1161 | return CodeCompleteOpts.IncludeCodePatterns; |
||
1162 | } |
||
1163 | |||
1164 | /// Whether to include global (top-level) declaration results. |
||
1165 | bool includeGlobals() const { return CodeCompleteOpts.IncludeGlobals; } |
||
1166 | |||
1167 | /// Whether to include declarations in namespace contexts (including |
||
1168 | /// the global namespace). If this is false, `includeGlobals()` will be |
||
1169 | /// ignored. |
||
1170 | bool includeNamespaceLevelDecls() const { |
||
1171 | return CodeCompleteOpts.IncludeNamespaceLevelDecls; |
||
1172 | } |
||
1173 | |||
1174 | /// Whether to include brief documentation comments within the set of |
||
1175 | /// code completions returned. |
||
1176 | bool includeBriefComments() const { |
||
1177 | return CodeCompleteOpts.IncludeBriefComments; |
||
1178 | } |
||
1179 | |||
1180 | /// Whether to include completion items with small fix-its, e.g. change |
||
1181 | /// '.' to '->' on member access, etc. |
||
1182 | bool includeFixIts() const { return CodeCompleteOpts.IncludeFixIts; } |
||
1183 | |||
1184 | /// Hint whether to load data from the external AST in order to provide |
||
1185 | /// full results. If false, declarations from the preamble may be omitted. |
||
1186 | bool loadExternal() const { |
||
1187 | return CodeCompleteOpts.LoadExternal; |
||
1188 | } |
||
1189 | |||
1190 | /// Deregisters and destroys this code-completion consumer. |
||
1191 | virtual ~CodeCompleteConsumer(); |
||
1192 | |||
1193 | /// \name Code-completion filtering |
||
1194 | /// Check if the result should be filtered out. |
||
1195 | virtual bool isResultFilteredOut(StringRef Filter, |
||
1196 | CodeCompletionResult Results) { |
||
1197 | return false; |
||
1198 | } |
||
1199 | |||
1200 | /// \name Code-completion callbacks |
||
1201 | //@{ |
||
1202 | /// Process the finalized code-completion results. |
||
1203 | virtual void ProcessCodeCompleteResults(Sema &S, |
||
1204 | CodeCompletionContext Context, |
||
1205 | CodeCompletionResult *Results, |
||
1206 | unsigned NumResults) {} |
||
1207 | |||
1208 | /// \param S the semantic-analyzer object for which code-completion is being |
||
1209 | /// done. |
||
1210 | /// |
||
1211 | /// \param CurrentArg the index of the current argument. |
||
1212 | /// |
||
1213 | /// \param Candidates an array of overload candidates. |
||
1214 | /// |
||
1215 | /// \param NumCandidates the number of overload candidates |
||
1216 | /// |
||
1217 | /// \param OpenParLoc location of the opening parenthesis of the argument |
||
1218 | /// list. |
||
1219 | virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
||
1220 | OverloadCandidate *Candidates, |
||
1221 | unsigned NumCandidates, |
||
1222 | SourceLocation OpenParLoc, |
||
1223 | bool Braced) {} |
||
1224 | //@} |
||
1225 | |||
1226 | /// Retrieve the allocator that will be used to allocate |
||
1227 | /// code completion strings. |
||
1228 | virtual CodeCompletionAllocator &getAllocator() = 0; |
||
1229 | |||
1230 | virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() = 0; |
||
1231 | }; |
||
1232 | |||
1233 | /// Get the documentation comment used to produce |
||
1234 | /// CodeCompletionString::BriefComment for RK_Declaration. |
||
1235 | const RawComment *getCompletionComment(const ASTContext &Ctx, |
||
1236 | const NamedDecl *Decl); |
||
1237 | |||
1238 | /// Get the documentation comment used to produce |
||
1239 | /// CodeCompletionString::BriefComment for RK_Pattern. |
||
1240 | const RawComment *getPatternCompletionComment(const ASTContext &Ctx, |
||
1241 | const NamedDecl *Decl); |
||
1242 | |||
1243 | /// Get the documentation comment used to produce |
||
1244 | /// CodeCompletionString::BriefComment for OverloadCandidate. |
||
1245 | const RawComment * |
||
1246 | getParameterComment(const ASTContext &Ctx, |
||
1247 | const CodeCompleteConsumer::OverloadCandidate &Result, |
||
1248 | unsigned ArgIndex); |
||
1249 | |||
1250 | /// A simple code-completion consumer that prints the results it |
||
1251 | /// receives in a simple format. |
||
1252 | class PrintingCodeCompleteConsumer : public CodeCompleteConsumer { |
||
1253 | /// The raw output stream. |
||
1254 | raw_ostream &OS; |
||
1255 | |||
1256 | CodeCompletionTUInfo CCTUInfo; |
||
1257 | |||
1258 | public: |
||
1259 | /// Create a new printing code-completion consumer that prints its |
||
1260 | /// results to the given raw output stream. |
||
1261 | PrintingCodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts, |
||
1262 | raw_ostream &OS) |
||
1263 | : CodeCompleteConsumer(CodeCompleteOpts), OS(OS), |
||
1264 | CCTUInfo(std::make_shared<GlobalCodeCompletionAllocator>()) {} |
||
1265 | |||
1266 | /// Prints the finalized code-completion results. |
||
1267 | void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, |
||
1268 | CodeCompletionResult *Results, |
||
1269 | unsigned NumResults) override; |
||
1270 | |||
1271 | void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
||
1272 | OverloadCandidate *Candidates, |
||
1273 | unsigned NumCandidates, |
||
1274 | SourceLocation OpenParLoc, |
||
1275 | bool Braced) override; |
||
1276 | |||
1277 | bool isResultFilteredOut(StringRef Filter, CodeCompletionResult Results) override; |
||
1278 | |||
1279 | CodeCompletionAllocator &getAllocator() override { |
||
1280 | return CCTUInfo.getAllocator(); |
||
1281 | } |
||
1282 | |||
1283 | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; } |
||
1284 | }; |
||
1285 | |||
1286 | } // namespace clang |
||
1287 | |||
1288 | #endif // LLVM_CLANG_SEMA_CODECOMPLETECONSUMER_H |