/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaCodeComplete.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===---------------- SemaCodeComplete.cpp - Code Completion ----*- 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 code-completion semantic actions. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | #include "clang/AST/ASTConcept.h" |
13 | | #include "clang/AST/Decl.h" |
14 | | #include "clang/AST/DeclBase.h" |
15 | | #include "clang/AST/DeclCXX.h" |
16 | | #include "clang/AST/DeclObjC.h" |
17 | | #include "clang/AST/DeclTemplate.h" |
18 | | #include "clang/AST/Expr.h" |
19 | | #include "clang/AST/ExprCXX.h" |
20 | | #include "clang/AST/ExprConcepts.h" |
21 | | #include "clang/AST/ExprObjC.h" |
22 | | #include "clang/AST/NestedNameSpecifier.h" |
23 | | #include "clang/AST/QualTypeNames.h" |
24 | | #include "clang/AST/RecursiveASTVisitor.h" |
25 | | #include "clang/AST/Type.h" |
26 | | #include "clang/Basic/CharInfo.h" |
27 | | #include "clang/Basic/OperatorKinds.h" |
28 | | #include "clang/Basic/Specifiers.h" |
29 | | #include "clang/Lex/HeaderSearch.h" |
30 | | #include "clang/Lex/MacroInfo.h" |
31 | | #include "clang/Lex/Preprocessor.h" |
32 | | #include "clang/Sema/CodeCompleteConsumer.h" |
33 | | #include "clang/Sema/DeclSpec.h" |
34 | | #include "clang/Sema/Designator.h" |
35 | | #include "clang/Sema/Lookup.h" |
36 | | #include "clang/Sema/Overload.h" |
37 | | #include "clang/Sema/Scope.h" |
38 | | #include "clang/Sema/ScopeInfo.h" |
39 | | #include "clang/Sema/Sema.h" |
40 | | #include "clang/Sema/SemaInternal.h" |
41 | | #include "llvm/ADT/ArrayRef.h" |
42 | | #include "llvm/ADT/DenseSet.h" |
43 | | #include "llvm/ADT/SmallBitVector.h" |
44 | | #include "llvm/ADT/SmallPtrSet.h" |
45 | | #include "llvm/ADT/SmallString.h" |
46 | | #include "llvm/ADT/StringExtras.h" |
47 | | #include "llvm/ADT/StringSwitch.h" |
48 | | #include "llvm/ADT/Twine.h" |
49 | | #include "llvm/ADT/iterator_range.h" |
50 | | #include "llvm/Support/Casting.h" |
51 | | #include "llvm/Support/Path.h" |
52 | | #include "llvm/Support/raw_ostream.h" |
53 | | #include <list> |
54 | | #include <map> |
55 | | #include <string> |
56 | | #include <vector> |
57 | | |
58 | | using namespace clang; |
59 | | using namespace sema; |
60 | | |
61 | | namespace { |
62 | | /// A container of code-completion results. |
63 | | class ResultBuilder { |
64 | | public: |
65 | | /// The type of a name-lookup filter, which can be provided to the |
66 | | /// name-lookup routines to specify which declarations should be included in |
67 | | /// the result set (when it returns true) and which declarations should be |
68 | | /// filtered out (returns false). |
69 | | typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const; |
70 | | |
71 | | typedef CodeCompletionResult Result; |
72 | | |
73 | | private: |
74 | | /// The actual results we have found. |
75 | | std::vector<Result> Results; |
76 | | |
77 | | /// A record of all of the declarations we have found and placed |
78 | | /// into the result set, used to ensure that no declaration ever gets into |
79 | | /// the result set twice. |
80 | | llvm::SmallPtrSet<const Decl *, 16> AllDeclsFound; |
81 | | |
82 | | typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair; |
83 | | |
84 | | /// An entry in the shadow map, which is optimized to store |
85 | | /// a single (declaration, index) mapping (the common case) but |
86 | | /// can also store a list of (declaration, index) mappings. |
87 | | class ShadowMapEntry { |
88 | | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; |
89 | | |
90 | | /// Contains either the solitary NamedDecl * or a vector |
91 | | /// of (declaration, index) pairs. |
92 | | llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector *> DeclOrVector; |
93 | | |
94 | | /// When the entry contains a single declaration, this is |
95 | | /// the index associated with that entry. |
96 | | unsigned SingleDeclIndex; |
97 | | |
98 | | public: |
99 | 1.01k | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) {} |
100 | | ShadowMapEntry(const ShadowMapEntry &) = delete; |
101 | 0 | ShadowMapEntry(ShadowMapEntry &&Move) { *this = std::move(Move); } |
102 | | ShadowMapEntry &operator=(const ShadowMapEntry &) = delete; |
103 | 0 | ShadowMapEntry &operator=(ShadowMapEntry &&Move) { |
104 | 0 | SingleDeclIndex = Move.SingleDeclIndex; |
105 | 0 | DeclOrVector = Move.DeclOrVector; |
106 | 0 | Move.DeclOrVector = nullptr; |
107 | 0 | return *this; |
108 | 0 | } |
109 | | |
110 | 1.20k | void Add(const NamedDecl *ND, unsigned Index) { |
111 | 1.20k | if (DeclOrVector.isNull()) { |
112 | | // 0 - > 1 elements: just set the single element information. |
113 | 1.01k | DeclOrVector = ND; |
114 | 1.01k | SingleDeclIndex = Index; |
115 | 1.01k | return; |
116 | 1.01k | } |
117 | | |
118 | 196 | if (const NamedDecl *PrevND = |
119 | 196 | DeclOrVector.dyn_cast<const NamedDecl *>()) { |
120 | | // 1 -> 2 elements: create the vector of results and push in the |
121 | | // existing declaration. |
122 | 131 | DeclIndexPairVector *Vec = new DeclIndexPairVector; |
123 | 131 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); |
124 | 131 | DeclOrVector = Vec; |
125 | 131 | } |
126 | | |
127 | | // Add the new element to the end of the vector. |
128 | 196 | DeclOrVector.get<DeclIndexPairVector *>()->push_back( |
129 | 196 | DeclIndexPair(ND, Index)); |
130 | 196 | } |
131 | | |
132 | 1.01k | ~ShadowMapEntry() { |
133 | 1.01k | if (DeclIndexPairVector *Vec = |
134 | 1.01k | DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { |
135 | 131 | delete Vec; |
136 | 131 | DeclOrVector = ((NamedDecl *)nullptr); |
137 | 131 | } |
138 | 1.01k | } |
139 | | |
140 | | // Iteration. |
141 | | class iterator; |
142 | | iterator begin() const; |
143 | | iterator end() const; |
144 | | }; |
145 | | |
146 | | /// A mapping from declaration names to the declarations that have |
147 | | /// this name within a particular scope and their index within the list of |
148 | | /// results. |
149 | | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; |
150 | | |
151 | | /// The semantic analysis object for which results are being |
152 | | /// produced. |
153 | | Sema &SemaRef; |
154 | | |
155 | | /// The allocator used to allocate new code-completion strings. |
156 | | CodeCompletionAllocator &Allocator; |
157 | | |
158 | | CodeCompletionTUInfo &CCTUInfo; |
159 | | |
160 | | /// If non-NULL, a filter function used to remove any code-completion |
161 | | /// results that are not desirable. |
162 | | LookupFilter Filter; |
163 | | |
164 | | /// Whether we should allow declarations as |
165 | | /// nested-name-specifiers that would otherwise be filtered out. |
166 | | bool AllowNestedNameSpecifiers; |
167 | | |
168 | | /// If set, the type that we would prefer our resulting value |
169 | | /// declarations to have. |
170 | | /// |
171 | | /// Closely matching the preferred type gives a boost to a result's |
172 | | /// priority. |
173 | | CanQualType PreferredType; |
174 | | |
175 | | /// A list of shadow maps, which is used to model name hiding at |
176 | | /// different levels of, e.g., the inheritance hierarchy. |
177 | | std::list<ShadowMap> ShadowMaps; |
178 | | |
179 | | /// Overloaded C++ member functions found by SemaLookup. |
180 | | /// Used to determine when one overload is dominated by another. |
181 | | llvm::DenseMap<std::pair<DeclContext *, /*Name*/uintptr_t>, ShadowMapEntry> |
182 | | OverloadMap; |
183 | | |
184 | | /// If we're potentially referring to a C++ member function, the set |
185 | | /// of qualifiers applied to the object type. |
186 | | Qualifiers ObjectTypeQualifiers; |
187 | | /// The kind of the object expression, for rvalue/lvalue overloads. |
188 | | ExprValueKind ObjectKind; |
189 | | |
190 | | /// Whether the \p ObjectTypeQualifiers field is active. |
191 | | bool HasObjectTypeQualifiers; |
192 | | |
193 | | /// The selector that we prefer. |
194 | | Selector PreferredSelector; |
195 | | |
196 | | /// The completion context in which we are gathering results. |
197 | | CodeCompletionContext CompletionContext; |
198 | | |
199 | | /// If we are in an instance method definition, the \@implementation |
200 | | /// object. |
201 | | ObjCImplementationDecl *ObjCImplementation; |
202 | | |
203 | | void AdjustResultPriorityForDecl(Result &R); |
204 | | |
205 | | void MaybeAddConstructorResults(Result R); |
206 | | |
207 | | public: |
208 | | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, |
209 | | CodeCompletionTUInfo &CCTUInfo, |
210 | | const CodeCompletionContext &CompletionContext, |
211 | | LookupFilter Filter = nullptr) |
212 | | : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), |
213 | | Filter(Filter), AllowNestedNameSpecifiers(false), |
214 | | HasObjectTypeQualifiers(false), CompletionContext(CompletionContext), |
215 | 1.29k | ObjCImplementation(nullptr) { |
216 | | // If this is an Objective-C instance method definition, dig out the |
217 | | // corresponding implementation. |
218 | 1.29k | switch (CompletionContext.getKind()) { |
219 | 382 | case CodeCompletionContext::CCC_Expression: |
220 | 389 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
221 | 425 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
222 | 564 | case CodeCompletionContext::CCC_Statement: |
223 | 670 | case CodeCompletionContext::CCC_Recovery: |
224 | 670 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) |
225 | 33 | if (Method->isInstanceMethod()) |
226 | 30 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) |
227 | 30 | ObjCImplementation = Interface->getImplementation(); |
228 | 670 | break; |
229 | | |
230 | 623 | default: |
231 | 623 | break; |
232 | 1.29k | } |
233 | 1.29k | } |
234 | | |
235 | | /// Determine the priority for a reference to the given declaration. |
236 | | unsigned getBasePriority(const NamedDecl *D); |
237 | | |
238 | | /// Whether we should include code patterns in the completion |
239 | | /// results. |
240 | 682 | bool includeCodePatterns() const { |
241 | 682 | return SemaRef.CodeCompleter && |
242 | 682 | SemaRef.CodeCompleter->includeCodePatterns(); |
243 | 682 | } |
244 | | |
245 | | /// Set the filter used for code-completion results. |
246 | 680 | void setFilter(LookupFilter Filter) { this->Filter = Filter; } |
247 | | |
248 | 1.39k | Result *data() { return Results.empty() ? nullptr20 : &Results.front()1.37k ; } |
249 | 1.29k | unsigned size() const { return Results.size(); } |
250 | 165 | bool empty() const { return Results.empty(); } |
251 | | |
252 | | /// Specify the preferred type. |
253 | 308 | void setPreferredType(QualType T) { |
254 | 308 | PreferredType = SemaRef.Context.getCanonicalType(T); |
255 | 308 | } |
256 | | |
257 | | /// Set the cv-qualifiers on the object type, for us in filtering |
258 | | /// calls to member functions. |
259 | | /// |
260 | | /// When there are qualifiers in this set, they will be used to filter |
261 | | /// out member functions that aren't available (because there will be a |
262 | | /// cv-qualifier mismatch) or prefer functions with an exact qualifier |
263 | | /// match. |
264 | 153 | void setObjectTypeQualifiers(Qualifiers Quals, ExprValueKind Kind) { |
265 | 153 | ObjectTypeQualifiers = Quals; |
266 | 153 | ObjectKind = Kind; |
267 | 153 | HasObjectTypeQualifiers = true; |
268 | 153 | } |
269 | | |
270 | | /// Set the preferred selector. |
271 | | /// |
272 | | /// When an Objective-C method declaration result is added, and that |
273 | | /// method's selector matches this preferred selector, we give that method |
274 | | /// a slight priority boost. |
275 | 29 | void setPreferredSelector(Selector Sel) { PreferredSelector = Sel; } |
276 | | |
277 | | /// Retrieve the code-completion context for which results are |
278 | | /// being collected. |
279 | 1.19k | const CodeCompletionContext &getCompletionContext() const { |
280 | 1.19k | return CompletionContext; |
281 | 1.19k | } |
282 | | |
283 | | /// Specify whether nested-name-specifiers are allowed. |
284 | 142 | void allowNestedNameSpecifiers(bool Allow = true) { |
285 | 142 | AllowNestedNameSpecifiers = Allow; |
286 | 142 | } |
287 | | |
288 | | /// Return the semantic analysis object for which we are collecting |
289 | | /// code completion results. |
290 | 18.2k | Sema &getSema() const { return SemaRef; } |
291 | | |
292 | | /// Retrieve the allocator used to allocate code completion strings. |
293 | 6.46k | CodeCompletionAllocator &getAllocator() const { return Allocator; } |
294 | | |
295 | 2.03k | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } |
296 | | |
297 | | /// Determine whether the given declaration is at all interesting |
298 | | /// as a code-completion result. |
299 | | /// |
300 | | /// \param ND the declaration that we are inspecting. |
301 | | /// |
302 | | /// \param AsNestedNameSpecifier will be set true if this declaration is |
303 | | /// only interesting when it is a nested-name-specifier. |
304 | | bool isInterestingDecl(const NamedDecl *ND, |
305 | | bool &AsNestedNameSpecifier) const; |
306 | | |
307 | | /// Check whether the result is hidden by the Hiding declaration. |
308 | | /// |
309 | | /// \returns true if the result is hidden and cannot be found, false if |
310 | | /// the hidden result could still be found. When false, \p R may be |
311 | | /// modified to describe how the result can be found (e.g., via extra |
312 | | /// qualification). |
313 | | bool CheckHiddenResult(Result &R, DeclContext *CurContext, |
314 | | const NamedDecl *Hiding); |
315 | | |
316 | | /// Add a new result to this result set (if it isn't already in one |
317 | | /// of the shadow maps), or replace an existing result (for, e.g., a |
318 | | /// redeclaration). |
319 | | /// |
320 | | /// \param R the result to add (if it is unique). |
321 | | /// |
322 | | /// \param CurContext the context in which this result will be named. |
323 | | void MaybeAddResult(Result R, DeclContext *CurContext = nullptr); |
324 | | |
325 | | /// Add a new result to this result set, where we already know |
326 | | /// the hiding declaration (if any). |
327 | | /// |
328 | | /// \param R the result to add (if it is unique). |
329 | | /// |
330 | | /// \param CurContext the context in which this result will be named. |
331 | | /// |
332 | | /// \param Hiding the declaration that hides the result. |
333 | | /// |
334 | | /// \param InBaseClass whether the result was found in a base |
335 | | /// class of the searched context. |
336 | | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, |
337 | | bool InBaseClass); |
338 | | |
339 | | /// Add a new non-declaration result to this result set. |
340 | | void AddResult(Result R); |
341 | | |
342 | | /// Enter into a new scope. |
343 | | void EnterNewScope(); |
344 | | |
345 | | /// Exit from the current scope. |
346 | | void ExitScope(); |
347 | | |
348 | | /// Ignore this declaration, if it is seen again. |
349 | 62 | void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } |
350 | | |
351 | | /// Add a visited context. |
352 | 1.02k | void addVisitedContext(DeclContext *Ctx) { |
353 | 1.02k | CompletionContext.addVisitedContext(Ctx); |
354 | 1.02k | } |
355 | | |
356 | | /// \name Name lookup predicates |
357 | | /// |
358 | | /// These predicates can be passed to the name lookup functions to filter the |
359 | | /// results of name lookup. All of the predicates have the same type, so that |
360 | | /// |
361 | | //@{ |
362 | | bool IsOrdinaryName(const NamedDecl *ND) const; |
363 | | bool IsOrdinaryNonTypeName(const NamedDecl *ND) const; |
364 | | bool IsIntegralConstantValue(const NamedDecl *ND) const; |
365 | | bool IsOrdinaryNonValueName(const NamedDecl *ND) const; |
366 | | bool IsNestedNameSpecifier(const NamedDecl *ND) const; |
367 | | bool IsEnum(const NamedDecl *ND) const; |
368 | | bool IsClassOrStruct(const NamedDecl *ND) const; |
369 | | bool IsUnion(const NamedDecl *ND) const; |
370 | | bool IsNamespace(const NamedDecl *ND) const; |
371 | | bool IsNamespaceOrAlias(const NamedDecl *ND) const; |
372 | | bool IsType(const NamedDecl *ND) const; |
373 | | bool IsMember(const NamedDecl *ND) const; |
374 | | bool IsObjCIvar(const NamedDecl *ND) const; |
375 | | bool IsObjCMessageReceiver(const NamedDecl *ND) const; |
376 | | bool IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const; |
377 | | bool IsObjCCollection(const NamedDecl *ND) const; |
378 | | bool IsImpossibleToSatisfy(const NamedDecl *ND) const; |
379 | | //@} |
380 | | }; |
381 | | } // namespace |
382 | | |
383 | 4.84M | void PreferredTypeBuilder::enterReturn(Sema &S, SourceLocation Tok) { |
384 | 4.84M | if (!Enabled) |
385 | 4.84M | return; |
386 | 52 | if (isa<BlockDecl>(S.CurContext)) { |
387 | 0 | if (sema::BlockScopeInfo *BSI = S.getCurBlock()) { |
388 | 0 | ComputeType = nullptr; |
389 | 0 | Type = BSI->ReturnType; |
390 | 0 | ExpectedLoc = Tok; |
391 | 0 | } |
392 | 52 | } else if (const auto *Function = dyn_cast<FunctionDecl>(S.CurContext)) { |
393 | 46 | ComputeType = nullptr; |
394 | 46 | Type = Function->getReturnType(); |
395 | 46 | ExpectedLoc = Tok; |
396 | 46 | } else if (const auto *6 Method6 = dyn_cast<ObjCMethodDecl>(S.CurContext)) { |
397 | 6 | ComputeType = nullptr; |
398 | 6 | Type = Method->getReturnType(); |
399 | 6 | ExpectedLoc = Tok; |
400 | 6 | } |
401 | 52 | } |
402 | | |
403 | 630k | void PreferredTypeBuilder::enterVariableInit(SourceLocation Tok, Decl *D) { |
404 | 630k | if (!Enabled) |
405 | 629k | return; |
406 | 357 | auto *VD = llvm::dyn_cast_or_null<ValueDecl>(D); |
407 | 357 | ComputeType = nullptr; |
408 | 357 | Type = VD ? VD->getType() : QualType()0 ; |
409 | 357 | ExpectedLoc = Tok; |
410 | 357 | } |
411 | | |
412 | | static QualType getDesignatedType(QualType BaseType, const Designation &Desig); |
413 | | |
414 | | void PreferredTypeBuilder::enterDesignatedInitializer(SourceLocation Tok, |
415 | | QualType BaseType, |
416 | 1.81k | const Designation &D) { |
417 | 1.81k | if (!Enabled) |
418 | 1.76k | return; |
419 | 44 | ComputeType = nullptr; |
420 | 44 | Type = getDesignatedType(BaseType, D); |
421 | 44 | ExpectedLoc = Tok; |
422 | 44 | } |
423 | | |
424 | | void PreferredTypeBuilder::enterFunctionArgument( |
425 | 17.0M | SourceLocation Tok, llvm::function_ref<QualType()> ComputeType) { |
426 | 17.0M | if (!Enabled) |
427 | 17.0M | return; |
428 | 2.75k | this->ComputeType = ComputeType; |
429 | 2.75k | Type = QualType(); |
430 | 2.75k | ExpectedLoc = Tok; |
431 | 2.75k | } |
432 | | |
433 | | void PreferredTypeBuilder::enterParenExpr(SourceLocation Tok, |
434 | 21.4M | SourceLocation LParLoc) { |
435 | 21.4M | if (!Enabled) |
436 | 21.4M | return; |
437 | | // expected type for parenthesized expression does not change. |
438 | 232 | if (ExpectedLoc == LParLoc) |
439 | 179 | ExpectedLoc = Tok; |
440 | 232 | } |
441 | | |
442 | | static QualType getPreferredTypeOfBinaryRHS(Sema &S, Expr *LHS, |
443 | 586 | tok::TokenKind Op) { |
444 | 586 | if (!LHS) |
445 | 20 | return QualType(); |
446 | | |
447 | 566 | QualType LHSType = LHS->getType(); |
448 | 566 | if (LHSType->isPointerType()) { |
449 | 61 | if (Op == tok::plus || Op == tok::plusequal56 || Op == tok::minusequal54 ) |
450 | 8 | return S.getASTContext().getPointerDiffType(); |
451 | | // Pointer difference is more common than subtracting an int from a pointer. |
452 | 53 | if (Op == tok::minus) |
453 | 2 | return LHSType; |
454 | 53 | } |
455 | | |
456 | 556 | switch (Op) { |
457 | | // No way to infer the type of RHS from LHS. |
458 | 7 | case tok::comma: |
459 | 7 | return QualType(); |
460 | | // Prefer the type of the left operand for all of these. |
461 | | // Arithmetic operations. |
462 | 43 | case tok::plus: |
463 | 68 | case tok::plusequal: |
464 | 92 | case tok::minus: |
465 | 114 | case tok::minusequal: |
466 | 129 | case tok::percent: |
467 | 142 | case tok::percentequal: |
468 | 160 | case tok::slash: |
469 | 176 | case tok::slashequal: |
470 | 199 | case tok::star: |
471 | 218 | case tok::starequal: |
472 | | // Assignment. |
473 | 342 | case tok::equal: |
474 | | // Comparison operators. |
475 | 361 | case tok::equalequal: |
476 | 374 | case tok::exclaimequal: |
477 | 394 | case tok::less: |
478 | 415 | case tok::lessequal: |
479 | 429 | case tok::greater: |
480 | 444 | case tok::greaterequal: |
481 | 444 | case tok::spaceship: |
482 | 444 | return LHS->getType(); |
483 | | // Binary shifts are often overloaded, so don't try to guess those. |
484 | 14 | case tok::greatergreater: |
485 | 20 | case tok::greatergreaterequal: |
486 | 43 | case tok::lessless: |
487 | 58 | case tok::lesslessequal: |
488 | 58 | if (LHSType->isIntegralOrEnumerationType()) |
489 | 36 | return S.getASTContext().IntTy; |
490 | 22 | return QualType(); |
491 | | // Logical operators, assume we want bool. |
492 | 8 | case tok::ampamp: |
493 | 13 | case tok::pipepipe: |
494 | 13 | case tok::caretcaret: |
495 | 13 | return S.getASTContext().BoolTy; |
496 | | // Operators often used for bit manipulation are typically used with the type |
497 | | // of the left argument. |
498 | 22 | case tok::pipe: |
499 | 24 | case tok::pipeequal: |
500 | 24 | case tok::caret: |
501 | 24 | case tok::caretequal: |
502 | 33 | case tok::amp: |
503 | 34 | case tok::ampequal: |
504 | 34 | if (LHSType->isIntegralOrEnumerationType()) |
505 | 20 | return LHSType; |
506 | 14 | return QualType(); |
507 | | // RHS should be a pointer to a member of the 'LHS' type, but we can't give |
508 | | // any particular type here. |
509 | 0 | case tok::periodstar: |
510 | 0 | case tok::arrowstar: |
511 | 0 | return QualType(); |
512 | 0 | default: |
513 | | // FIXME(ibiryukov): handle the missing op, re-add the assertion. |
514 | | // assert(false && "unhandled binary op"); |
515 | 0 | return QualType(); |
516 | 556 | } |
517 | 556 | } |
518 | | |
519 | | /// Get preferred type for an argument of an unary expression. \p ContextType is |
520 | | /// preferred type of the whole unary expression. |
521 | | static QualType getPreferredTypeOfUnaryArg(Sema &S, QualType ContextType, |
522 | 75 | tok::TokenKind Op) { |
523 | 75 | switch (Op) { |
524 | 31 | case tok::exclaim: |
525 | 31 | return S.getASTContext().BoolTy; |
526 | 18 | case tok::amp: |
527 | 18 | if (!ContextType.isNull() && ContextType->isPointerType()2 ) |
528 | 1 | return ContextType->getPointeeType(); |
529 | 17 | return QualType(); |
530 | 16 | case tok::star: |
531 | 16 | if (ContextType.isNull()) |
532 | 11 | return QualType(); |
533 | 5 | return S.getASTContext().getPointerType(ContextType.getNonReferenceType()); |
534 | 4 | case tok::plus: |
535 | 7 | case tok::minus: |
536 | 7 | case tok::tilde: |
537 | 8 | case tok::minusminus: |
538 | 10 | case tok::plusplus: |
539 | 10 | if (ContextType.isNull()) |
540 | 0 | return S.getASTContext().IntTy; |
541 | | // leave as is, these operators typically return the same type. |
542 | 10 | return ContextType; |
543 | 0 | case tok::kw___real: |
544 | 0 | case tok::kw___imag: |
545 | 0 | return QualType(); |
546 | 0 | default: |
547 | 0 | assert(false && "unhandled unary op"); |
548 | 0 | return QualType(); |
549 | 75 | } |
550 | 75 | } |
551 | | |
552 | | void PreferredTypeBuilder::enterBinary(Sema &S, SourceLocation Tok, Expr *LHS, |
553 | 3.46M | tok::TokenKind Op) { |
554 | 3.46M | if (!Enabled) |
555 | 3.46M | return; |
556 | 586 | ComputeType = nullptr; |
557 | 586 | Type = getPreferredTypeOfBinaryRHS(S, LHS, Op); |
558 | 586 | ExpectedLoc = Tok; |
559 | 586 | } |
560 | | |
561 | | void PreferredTypeBuilder::enterMemAccess(Sema &S, SourceLocation Tok, |
562 | 1.17M | Expr *Base) { |
563 | 1.17M | if (!Enabled || !Base812 ) |
564 | 1.17M | return; |
565 | | // Do we have expected type for Base? |
566 | 811 | if (ExpectedLoc != Base->getBeginLoc()) |
567 | 737 | return; |
568 | | // Keep the expected type, only update the location. |
569 | 74 | ExpectedLoc = Tok; |
570 | 74 | return; |
571 | 811 | } |
572 | | |
573 | | void PreferredTypeBuilder::enterUnary(Sema &S, SourceLocation Tok, |
574 | | tok::TokenKind OpKind, |
575 | 2.12M | SourceLocation OpLoc) { |
576 | 2.12M | if (!Enabled) |
577 | 2.12M | return; |
578 | 75 | ComputeType = nullptr; |
579 | 75 | Type = getPreferredTypeOfUnaryArg(S, this->get(OpLoc), OpKind); |
580 | 75 | ExpectedLoc = Tok; |
581 | 75 | } |
582 | | |
583 | | void PreferredTypeBuilder::enterSubscript(Sema &S, SourceLocation Tok, |
584 | 266k | Expr *LHS) { |
585 | 266k | if (!Enabled) |
586 | 266k | return; |
587 | 0 | ComputeType = nullptr; |
588 | 0 | Type = S.getASTContext().IntTy; |
589 | 0 | ExpectedLoc = Tok; |
590 | 0 | } |
591 | | |
592 | | void PreferredTypeBuilder::enterTypeCast(SourceLocation Tok, |
593 | 12.6M | QualType CastType) { |
594 | 12.6M | if (!Enabled) |
595 | 12.6M | return; |
596 | 184 | ComputeType = nullptr; |
597 | 184 | Type = !CastType.isNull() ? CastType.getCanonicalType()157 : QualType()27 ; |
598 | 184 | ExpectedLoc = Tok; |
599 | 184 | } |
600 | | |
601 | 654k | void PreferredTypeBuilder::enterCondition(Sema &S, SourceLocation Tok) { |
602 | 654k | if (!Enabled) |
603 | 654k | return; |
604 | 40 | ComputeType = nullptr; |
605 | 40 | Type = S.getASTContext().BoolTy; |
606 | 40 | ExpectedLoc = Tok; |
607 | 40 | } |
608 | | |
609 | | class ResultBuilder::ShadowMapEntry::iterator { |
610 | | llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator; |
611 | | unsigned SingleDeclIndex; |
612 | | |
613 | | public: |
614 | | typedef DeclIndexPair value_type; |
615 | | typedef value_type reference; |
616 | | typedef std::ptrdiff_t difference_type; |
617 | | typedef std::input_iterator_tag iterator_category; |
618 | | |
619 | | class pointer { |
620 | | DeclIndexPair Value; |
621 | | |
622 | | public: |
623 | 4 | pointer(const DeclIndexPair &Value) : Value(Value) {} |
624 | | |
625 | 4 | const DeclIndexPair *operator->() const { return &Value; } |
626 | | }; |
627 | | |
628 | 2.18k | iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {} |
629 | | |
630 | | iterator(const NamedDecl *SingleDecl, unsigned Index) |
631 | 138 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) {} |
632 | | |
633 | | iterator(const DeclIndexPair *Iterator) |
634 | 132 | : DeclOrIterator(Iterator), SingleDeclIndex(0) {} |
635 | | |
636 | 344 | iterator &operator++() { |
637 | 344 | if (DeclOrIterator.is<const NamedDecl *>()) { |
638 | 131 | DeclOrIterator = (NamedDecl *)nullptr; |
639 | 131 | SingleDeclIndex = 0; |
640 | 131 | return *this; |
641 | 131 | } |
642 | | |
643 | 213 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair *>(); |
644 | 213 | ++I; |
645 | 213 | DeclOrIterator = I; |
646 | 213 | return *this; |
647 | 344 | } |
648 | | |
649 | | /*iterator operator++(int) { |
650 | | iterator tmp(*this); |
651 | | ++(*this); |
652 | | return tmp; |
653 | | }*/ |
654 | | |
655 | 354 | reference operator*() const { |
656 | 354 | if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>()) |
657 | 140 | return reference(ND, SingleDeclIndex); |
658 | | |
659 | 214 | return *DeclOrIterator.get<const DeclIndexPair *>(); |
660 | 354 | } |
661 | | |
662 | 4 | pointer operator->() const { return pointer(**this); } |
663 | | |
664 | 1.56k | friend bool operator==(const iterator &X, const iterator &Y) { |
665 | 1.56k | return X.DeclOrIterator.getOpaqueValue() == |
666 | 1.56k | Y.DeclOrIterator.getOpaqueValue() && |
667 | 1.56k | X.SingleDeclIndex == Y.SingleDeclIndex1.21k ; |
668 | 1.56k | } |
669 | | |
670 | 1.56k | friend bool operator!=(const iterator &X, const iterator &Y) { |
671 | 1.56k | return !(X == Y); |
672 | 1.56k | } |
673 | | }; |
674 | | |
675 | | ResultBuilder::ShadowMapEntry::iterator |
676 | 803 | ResultBuilder::ShadowMapEntry::begin() const { |
677 | 803 | if (DeclOrVector.isNull()) |
678 | 599 | return iterator(); |
679 | | |
680 | 204 | if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>()) |
681 | 138 | return iterator(ND, SingleDeclIndex); |
682 | | |
683 | 66 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); |
684 | 204 | } |
685 | | |
686 | | ResultBuilder::ShadowMapEntry::iterator |
687 | 803 | ResultBuilder::ShadowMapEntry::end() const { |
688 | 803 | if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull()66 ) |
689 | 737 | return iterator(); |
690 | | |
691 | 66 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); |
692 | 803 | } |
693 | | |
694 | | /// Compute the qualification required to get from the current context |
695 | | /// (\p CurContext) to the target context (\p TargetContext). |
696 | | /// |
697 | | /// \param Context the AST context in which the qualification will be used. |
698 | | /// |
699 | | /// \param CurContext the context where an entity is being named, which is |
700 | | /// typically based on the current scope. |
701 | | /// |
702 | | /// \param TargetContext the context in which the named entity actually |
703 | | /// resides. |
704 | | /// |
705 | | /// \returns a nested name specifier that refers into the target context, or |
706 | | /// NULL if no qualification is needed. |
707 | | static NestedNameSpecifier * |
708 | | getRequiredQualification(ASTContext &Context, const DeclContext *CurContext, |
709 | 250 | const DeclContext *TargetContext) { |
710 | 250 | SmallVector<const DeclContext *, 4> TargetParents; |
711 | | |
712 | 250 | for (const DeclContext *CommonAncestor = TargetContext; |
713 | 357 | CommonAncestor && !CommonAncestor->Encloses(CurContext); |
714 | 250 | CommonAncestor = CommonAncestor->getLookupParent()107 ) { |
715 | 107 | if (CommonAncestor->isTransparentContext() || |
716 | 107 | CommonAncestor->isFunctionOrMethod()100 ) |
717 | 7 | continue; |
718 | | |
719 | 100 | TargetParents.push_back(CommonAncestor); |
720 | 100 | } |
721 | | |
722 | 250 | NestedNameSpecifier *Result = nullptr; |
723 | 350 | while (!TargetParents.empty()) { |
724 | 100 | const DeclContext *Parent = TargetParents.pop_back_val(); |
725 | | |
726 | 100 | if (const auto *Namespace = dyn_cast<NamespaceDecl>(Parent)) { |
727 | 8 | if (!Namespace->getIdentifier()) |
728 | 0 | continue; |
729 | | |
730 | 8 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); |
731 | 92 | } else if (const auto *TD = dyn_cast<TagDecl>(Parent)) |
732 | 92 | Result = NestedNameSpecifier::Create( |
733 | 92 | Context, Result, false, Context.getTypeDeclType(TD).getTypePtr()); |
734 | 100 | } |
735 | 250 | return Result; |
736 | 250 | } |
737 | | |
738 | | // Some declarations have reserved names that we don't want to ever show. |
739 | | // Filter out names reserved for the implementation if they come from a |
740 | | // system header. |
741 | 17.6k | static bool shouldIgnoreDueToReservedName(const NamedDecl *ND, Sema &SemaRef) { |
742 | 17.6k | const IdentifierInfo *Id = ND->getIdentifier(); |
743 | 17.6k | if (!Id) |
744 | 1.58k | return false; |
745 | | |
746 | | // Ignore reserved names for compiler provided decls. |
747 | 16.0k | if (Id->isReservedName() && ND->getLocation().isInvalid()5.80k ) |
748 | 5.79k | return true; |
749 | | |
750 | | // For system headers ignore only double-underscore names. |
751 | | // This allows for system headers providing private symbols with a single |
752 | | // underscore. |
753 | 10.2k | if (Id->isReservedName(/*doubleUnderscoreOnly=*/true) && |
754 | 10.2k | SemaRef.SourceMgr.isInSystemHeader( |
755 | 2 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))) |
756 | 2 | return true; |
757 | | |
758 | 10.2k | return false; |
759 | 10.2k | } |
760 | | |
761 | | bool ResultBuilder::isInterestingDecl(const NamedDecl *ND, |
762 | 17.6k | bool &AsNestedNameSpecifier) const { |
763 | 17.6k | AsNestedNameSpecifier = false; |
764 | | |
765 | 17.6k | auto *Named = ND; |
766 | 17.6k | ND = ND->getUnderlyingDecl(); |
767 | | |
768 | | // Skip unnamed entities. |
769 | 17.6k | if (!ND->getDeclName()) |
770 | 0 | return false; |
771 | | |
772 | | // Friend declarations and declarations introduced due to friends are never |
773 | | // added as results. |
774 | 17.6k | if (ND->getFriendObjectKind() == Decl::FOK_Undeclared) |
775 | 0 | return false; |
776 | | |
777 | | // Class template (partial) specializations are never added as results. |
778 | 17.6k | if (isa<ClassTemplateSpecializationDecl>(ND) || |
779 | 17.6k | isa<ClassTemplatePartialSpecializationDecl>(ND)) |
780 | 0 | return false; |
781 | | |
782 | | // Using declarations themselves are never added as results. |
783 | 17.6k | if (isa<UsingDecl>(ND)) |
784 | 2 | return false; |
785 | | |
786 | 17.6k | if (shouldIgnoreDueToReservedName(ND, SemaRef)) |
787 | 5.79k | return false; |
788 | | |
789 | 11.8k | if (Filter == &ResultBuilder::IsNestedNameSpecifier || |
790 | 11.8k | (11.8k isa<NamespaceDecl>(ND)11.8k && Filter != &ResultBuilder::IsNamespace139 && |
791 | 11.8k | Filter != &ResultBuilder::IsNamespaceOrAlias137 && Filter != nullptr123 )) |
792 | 162 | AsNestedNameSpecifier = true; |
793 | | |
794 | | // Filter out any unwanted results. |
795 | 11.8k | if (Filter && !(this->*Filter)(Named)10.5k ) { |
796 | | // Check whether it is interesting as a nested-name-specifier. |
797 | 533 | if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus147 && |
798 | 533 | IsNestedNameSpecifier(ND)147 && |
799 | 533 | (143 Filter != &ResultBuilder::IsMember143 || |
800 | 143 | (115 isa<CXXRecordDecl>(ND)115 && |
801 | 134 | cast<CXXRecordDecl>(ND)->isInjectedClassName()109 ))) { |
802 | 134 | AsNestedNameSpecifier = true; |
803 | 134 | return true; |
804 | 134 | } |
805 | | |
806 | 399 | return false; |
807 | 533 | } |
808 | | // ... then it must be interesting! |
809 | 11.3k | return true; |
810 | 11.8k | } |
811 | | |
812 | | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, |
813 | 1.84k | const NamedDecl *Hiding) { |
814 | | // In C, there is no way to refer to a hidden name. |
815 | | // FIXME: This isn't true; we can find a tag name hidden by an ordinary |
816 | | // name if we introduce the tag type. |
817 | 1.84k | if (!SemaRef.getLangOpts().CPlusPlus) |
818 | 424 | return true; |
819 | | |
820 | 1.42k | const DeclContext *HiddenCtx = |
821 | 1.42k | R.Declaration->getDeclContext()->getRedeclContext(); |
822 | | |
823 | | // There is no way to qualify a name declared in a function or method. |
824 | 1.42k | if (HiddenCtx->isFunctionOrMethod()) |
825 | 0 | return true; |
826 | | |
827 | 1.42k | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) |
828 | 1.18k | return true; |
829 | | |
830 | | // We can refer to the result with the appropriate qualification. Do it. |
831 | 238 | R.Hidden = true; |
832 | 238 | R.QualifierIsInformative = false; |
833 | | |
834 | 238 | if (!R.Qualifier) |
835 | 238 | R.Qualifier = getRequiredQualification(SemaRef.Context, CurContext, |
836 | 238 | R.Declaration->getDeclContext()); |
837 | 238 | return false; |
838 | 1.42k | } |
839 | | |
840 | | /// A simplified classification of types used to determine whether two |
841 | | /// types are "similar enough" when adjusting priorities. |
842 | 4.11k | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { |
843 | 4.11k | switch (T->getTypeClass()) { |
844 | 2.59k | case Type::Builtin: |
845 | 2.59k | switch (cast<BuiltinType>(T)->getKind()) { |
846 | 1.16k | case BuiltinType::Void: |
847 | 1.16k | return STC_Void; |
848 | | |
849 | 0 | case BuiltinType::NullPtr: |
850 | 0 | return STC_Pointer; |
851 | | |
852 | 0 | case BuiltinType::Overload: |
853 | 39 | case BuiltinType::Dependent: |
854 | 39 | return STC_Other; |
855 | | |
856 | 0 | case BuiltinType::ObjCId: |
857 | 0 | case BuiltinType::ObjCClass: |
858 | 0 | case BuiltinType::ObjCSel: |
859 | 0 | return STC_ObjectiveC; |
860 | | |
861 | 1.38k | default: |
862 | 1.38k | return STC_Arithmetic; |
863 | 2.59k | } |
864 | | |
865 | 0 | case Type::Complex: |
866 | 0 | return STC_Arithmetic; |
867 | | |
868 | 516 | case Type::Pointer: |
869 | 516 | return STC_Pointer; |
870 | | |
871 | 0 | case Type::BlockPointer: |
872 | 0 | return STC_Block; |
873 | | |
874 | 0 | case Type::LValueReference: |
875 | 0 | case Type::RValueReference: |
876 | 0 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); |
877 | | |
878 | 0 | case Type::ConstantArray: |
879 | 0 | case Type::IncompleteArray: |
880 | 0 | case Type::VariableArray: |
881 | 0 | case Type::DependentSizedArray: |
882 | 0 | return STC_Array; |
883 | | |
884 | 0 | case Type::DependentSizedExtVector: |
885 | 0 | case Type::Vector: |
886 | 0 | case Type::ExtVector: |
887 | 0 | return STC_Arithmetic; |
888 | | |
889 | 0 | case Type::FunctionProto: |
890 | 0 | case Type::FunctionNoProto: |
891 | 0 | return STC_Function; |
892 | | |
893 | 370 | case Type::Record: |
894 | 370 | return STC_Record; |
895 | | |
896 | 126 | case Type::Enum: |
897 | 126 | return STC_Arithmetic; |
898 | | |
899 | 0 | case Type::ObjCObject: |
900 | 71 | case Type::ObjCInterface: |
901 | 180 | case Type::ObjCObjectPointer: |
902 | 180 | return STC_ObjectiveC; |
903 | | |
904 | 322 | default: |
905 | 322 | return STC_Other; |
906 | 4.11k | } |
907 | 4.11k | } |
908 | | |
909 | | /// Get the type that a given expression will have if this declaration |
910 | | /// is used as an expression in its "typical" code-completion form. |
911 | 2.87k | QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) { |
912 | 2.87k | ND = ND->getUnderlyingDecl(); |
913 | | |
914 | 2.87k | if (const auto *Type = dyn_cast<TypeDecl>(ND)) |
915 | 548 | return C.getTypeDeclType(Type); |
916 | 2.33k | if (const auto *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) |
917 | 131 | return C.getObjCInterfaceType(Iface); |
918 | | |
919 | 2.20k | QualType T; |
920 | 2.20k | if (const FunctionDecl *Function = ND->getAsFunction()) |
921 | 1.36k | T = Function->getCallResultType(); |
922 | 836 | else if (const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) |
923 | 18 | T = Method->getSendResultType(); |
924 | 818 | else if (const auto *Enumerator = dyn_cast<EnumConstantDecl>(ND)) |
925 | 64 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); |
926 | 754 | else if (const auto *Property = dyn_cast<ObjCPropertyDecl>(ND)) |
927 | 0 | T = Property->getType(); |
928 | 754 | else if (const auto *Value = dyn_cast<ValueDecl>(ND)) |
929 | 620 | T = Value->getType(); |
930 | | |
931 | 2.20k | if (T.isNull()) |
932 | 134 | return QualType(); |
933 | | |
934 | | // Dig through references, function pointers, and block pointers to |
935 | | // get down to the likely type of an expression when the entity is |
936 | | // used. |
937 | 2.20k | do 2.06k { |
938 | 2.20k | if (const auto *Ref = T->getAs<ReferenceType>()) { |
939 | 121 | T = Ref->getPointeeType(); |
940 | 121 | continue; |
941 | 121 | } |
942 | | |
943 | 2.08k | if (const auto *Pointer = T->getAs<PointerType>()) { |
944 | 144 | if (Pointer->getPointeeType()->isFunctionType()) { |
945 | 3 | T = Pointer->getPointeeType(); |
946 | 3 | continue; |
947 | 3 | } |
948 | | |
949 | 141 | break; |
950 | 144 | } |
951 | | |
952 | 1.93k | if (const auto *Block = T->getAs<BlockPointerType>()) { |
953 | 4 | T = Block->getPointeeType(); |
954 | 4 | continue; |
955 | 4 | } |
956 | | |
957 | 1.93k | if (const auto *Function = T->getAs<FunctionType>()) { |
958 | 7 | T = Function->getReturnType(); |
959 | 7 | continue; |
960 | 7 | } |
961 | | |
962 | 1.92k | break; |
963 | 1.93k | } while (true135 ); |
964 | | |
965 | 0 | return T; |
966 | 2.20k | } |
967 | | |
968 | 17.7k | unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) { |
969 | 17.7k | if (!ND) |
970 | 0 | return CCP_Unlikely; |
971 | | |
972 | | // Context-based decisions. |
973 | 17.7k | const DeclContext *LexicalDC = ND->getLexicalDeclContext(); |
974 | 17.7k | if (LexicalDC->isFunctionOrMethod()) { |
975 | | // _cmd is relatively rare |
976 | 805 | if (const auto *ImplicitParam = dyn_cast<ImplicitParamDecl>(ND)) |
977 | 68 | if (ImplicitParam->getIdentifier() && |
978 | 68 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
979 | 34 | return CCP_ObjC_cmd; |
980 | | |
981 | 771 | return CCP_LocalDeclaration; |
982 | 805 | } |
983 | | |
984 | 16.9k | const DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
985 | 16.9k | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)14.7k ) { |
986 | | // Explicit destructor calls are very rare. |
987 | 2.77k | if (isa<CXXDestructorDecl>(ND)) |
988 | 207 | return CCP_Unlikely; |
989 | | // Explicit operator and conversion function calls are also very rare. |
990 | 2.56k | auto DeclNameKind = ND->getDeclName().getNameKind(); |
991 | 2.56k | if (DeclNameKind == DeclarationName::CXXOperatorName || |
992 | 2.56k | DeclNameKind == DeclarationName::CXXLiteralOperatorName2.14k || |
993 | 2.56k | DeclNameKind == DeclarationName::CXXConversionFunctionName2.14k ) |
994 | 419 | return CCP_Unlikely; |
995 | 2.14k | return CCP_MemberDeclaration; |
996 | 2.56k | } |
997 | | |
998 | | // Content-based decisions. |
999 | 14.1k | if (isa<EnumConstantDecl>(ND)) |
1000 | 124 | return CCP_Constant; |
1001 | | |
1002 | | // Use CCP_Type for type declarations unless we're in a statement, Objective-C |
1003 | | // message receiver, or parenthesized expression context. There, it's as |
1004 | | // likely that the user will want to write a type as other declarations. |
1005 | 14.0k | if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)5.62k ) && |
1006 | 14.0k | !(8.98k CompletionContext.getKind() == CodeCompletionContext::CCC_Statement8.98k || |
1007 | 8.98k | CompletionContext.getKind() == |
1008 | 6.75k | CodeCompletionContext::CCC_ObjCMessageReceiver || |
1009 | 8.98k | CompletionContext.getKind() == |
1010 | 6.58k | CodeCompletionContext::CCC_ParenthesizedExpression)) |
1011 | 6.23k | return CCP_Type; |
1012 | | |
1013 | 7.83k | return CCP_Declaration; |
1014 | 14.0k | } |
1015 | | |
1016 | 7.17k | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { |
1017 | | // If this is an Objective-C method declaration whose selector matches our |
1018 | | // preferred selector, give it a priority boost. |
1019 | 7.17k | if (!PreferredSelector.isNull()) |
1020 | 83 | if (const auto *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) |
1021 | 83 | if (PreferredSelector == Method->getSelector()) |
1022 | 14 | R.Priority += CCD_SelectorMatch; |
1023 | | |
1024 | | // If we have a preferred type, adjust the priority for results with exactly- |
1025 | | // matching or nearly-matching types. |
1026 | 7.17k | if (!PreferredType.isNull()) { |
1027 | 2.17k | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); |
1028 | 2.17k | if (!T.isNull()) { |
1029 | 2.06k | CanQualType TC = SemaRef.Context.getCanonicalType(T); |
1030 | | // Check for exactly-matching types (modulo qualifiers). |
1031 | 2.06k | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) |
1032 | 351 | R.Priority /= CCF_ExactTypeMatch; |
1033 | | // Check for nearly-matching types, based on classification of each. |
1034 | 1.71k | else if ((getSimplifiedTypeClass(PreferredType) == |
1035 | 1.71k | getSimplifiedTypeClass(TC)) && |
1036 | 1.71k | !(209 PreferredType->isEnumeralType()209 && TC->isEnumeralType()42 )) |
1037 | 180 | R.Priority /= CCF_SimilarTypeMatch; |
1038 | 2.06k | } |
1039 | 2.17k | } |
1040 | 7.17k | } |
1041 | | |
1042 | | static DeclContext::lookup_result getConstructors(ASTContext &Context, |
1043 | 484 | const CXXRecordDecl *Record) { |
1044 | 484 | QualType RecordTy = Context.getTypeDeclType(Record); |
1045 | 484 | DeclarationName ConstructorName = |
1046 | 484 | Context.DeclarationNames.getCXXConstructorName( |
1047 | 484 | Context.getCanonicalType(RecordTy)); |
1048 | 484 | return Record->lookup(ConstructorName); |
1049 | 484 | } |
1050 | | |
1051 | 6.91k | void ResultBuilder::MaybeAddConstructorResults(Result R) { |
1052 | 6.91k | if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration5.19k || |
1053 | 6.91k | !CompletionContext.wantConstructorResults()5.19k ) |
1054 | 2.63k | return; |
1055 | | |
1056 | 4.27k | const NamedDecl *D = R.Declaration; |
1057 | 4.27k | const CXXRecordDecl *Record = nullptr; |
1058 | 4.27k | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) |
1059 | 152 | Record = ClassTemplate->getTemplatedDecl(); |
1060 | 4.12k | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { |
1061 | | // Skip specializations and partial specializations. |
1062 | 400 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
1063 | 0 | return; |
1064 | 3.72k | } else { |
1065 | | // There are no constructors here. |
1066 | 3.72k | return; |
1067 | 3.72k | } |
1068 | | |
1069 | 552 | Record = Record->getDefinition(); |
1070 | 552 | if (!Record) |
1071 | 82 | return; |
1072 | | |
1073 | 470 | for (NamedDecl *Ctor : getConstructors(SemaRef.Context, Record)) { |
1074 | 440 | R.Declaration = Ctor; |
1075 | 440 | R.CursorKind = getCursorKindForDecl(R.Declaration); |
1076 | 440 | Results.push_back(R); |
1077 | 440 | } |
1078 | 470 | } |
1079 | | |
1080 | 16.9k | static bool isConstructor(const Decl *ND) { |
1081 | 16.9k | if (const auto *Tmpl = dyn_cast<FunctionTemplateDecl>(ND)) |
1082 | 1.89k | ND = Tmpl->getTemplatedDecl(); |
1083 | 16.9k | return isa<CXXConstructorDecl>(ND); |
1084 | 16.9k | } |
1085 | | |
1086 | 495 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { |
1087 | 495 | assert(!ShadowMaps.empty() && "Must enter into a results scope"); |
1088 | | |
1089 | 495 | if (R.Kind != Result::RK_Declaration) { |
1090 | | // For non-declaration results, just add the result. |
1091 | 71 | Results.push_back(R); |
1092 | 71 | return; |
1093 | 71 | } |
1094 | | |
1095 | | // Look through using declarations. |
1096 | 424 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
1097 | 0 | CodeCompletionResult Result(Using->getTargetDecl(), |
1098 | 0 | getBasePriority(Using->getTargetDecl()), |
1099 | 0 | R.Qualifier); |
1100 | 0 | Result.ShadowDecl = Using; |
1101 | 0 | MaybeAddResult(Result, CurContext); |
1102 | 0 | return; |
1103 | 0 | } |
1104 | | |
1105 | 424 | const Decl *CanonDecl = R.Declaration->getCanonicalDecl(); |
1106 | 424 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); |
1107 | | |
1108 | 424 | bool AsNestedNameSpecifier = false; |
1109 | 424 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
1110 | 0 | return; |
1111 | | |
1112 | | // C++ constructors are never found by name lookup. |
1113 | 424 | if (isConstructor(R.Declaration)) |
1114 | 0 | return; |
1115 | | |
1116 | 424 | ShadowMap &SMap = ShadowMaps.back(); |
1117 | 424 | ShadowMapEntry::iterator I, IEnd; |
1118 | 424 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); |
1119 | 424 | if (NamePos != SMap.end()) { |
1120 | 2 | I = NamePos->second.begin(); |
1121 | 2 | IEnd = NamePos->second.end(); |
1122 | 2 | } |
1123 | | |
1124 | 426 | for (; I != IEnd; ++I2 ) { |
1125 | 2 | const NamedDecl *ND = I->first; |
1126 | 2 | unsigned Index = I->second; |
1127 | 2 | if (ND->getCanonicalDecl() == CanonDecl) { |
1128 | | // This is a redeclaration. Always pick the newer declaration. |
1129 | 0 | Results[Index].Declaration = R.Declaration; |
1130 | | |
1131 | | // We're done. |
1132 | 0 | return; |
1133 | 0 | } |
1134 | 2 | } |
1135 | | |
1136 | | // This is a new declaration in this scope. However, check whether this |
1137 | | // declaration name is hidden by a similarly-named declaration in an outer |
1138 | | // scope. |
1139 | 424 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); |
1140 | 424 | --SMEnd; |
1141 | 424 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM0 ) { |
1142 | 0 | ShadowMapEntry::iterator I, IEnd; |
1143 | 0 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); |
1144 | 0 | if (NamePos != SM->end()) { |
1145 | 0 | I = NamePos->second.begin(); |
1146 | 0 | IEnd = NamePos->second.end(); |
1147 | 0 | } |
1148 | 0 | for (; I != IEnd; ++I) { |
1149 | | // A tag declaration does not hide a non-tag declaration. |
1150 | 0 | if (I->first->hasTagIdentifierNamespace() && |
1151 | 0 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | |
1152 | 0 | Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol))) |
1153 | 0 | continue; |
1154 | | |
1155 | | // Protocols are in distinct namespaces from everything else. |
1156 | 0 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) || |
1157 | 0 | (IDNS & Decl::IDNS_ObjCProtocol)) && |
1158 | 0 | I->first->getIdentifierNamespace() != IDNS) |
1159 | 0 | continue; |
1160 | | |
1161 | | // The newly-added result is hidden by an entry in the shadow map. |
1162 | 0 | if (CheckHiddenResult(R, CurContext, I->first)) |
1163 | 0 | return; |
1164 | | |
1165 | 0 | break; |
1166 | 0 | } |
1167 | 0 | } |
1168 | | |
1169 | | // Make sure that any given declaration only shows up in the result set once. |
1170 | 424 | if (!AllDeclsFound.insert(CanonDecl).second) |
1171 | 9 | return; |
1172 | | |
1173 | | // If the filter is for nested-name-specifiers, then this result starts a |
1174 | | // nested-name-specifier. |
1175 | 415 | if (AsNestedNameSpecifier) { |
1176 | 0 | R.StartsNestedNameSpecifier = true; |
1177 | 0 | R.Priority = CCP_NestedNameSpecifier; |
1178 | 0 | } else |
1179 | 415 | AdjustResultPriorityForDecl(R); |
1180 | | |
1181 | | // If this result is supposed to have an informative qualifier, add one. |
1182 | 415 | if (R.QualifierIsInformative && !R.Qualifier0 && |
1183 | 415 | !R.StartsNestedNameSpecifier0 ) { |
1184 | 0 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
1185 | 0 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
1186 | 0 | R.Qualifier = |
1187 | 0 | NestedNameSpecifier::Create(SemaRef.Context, nullptr, Namespace); |
1188 | 0 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) |
1189 | 0 | R.Qualifier = NestedNameSpecifier::Create( |
1190 | 0 | SemaRef.Context, nullptr, false, |
1191 | 0 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
1192 | 0 | else |
1193 | 0 | R.QualifierIsInformative = false; |
1194 | 0 | } |
1195 | | |
1196 | | // Insert this result into the set of results and into the current shadow |
1197 | | // map. |
1198 | 415 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); |
1199 | 415 | Results.push_back(R); |
1200 | | |
1201 | 415 | if (!AsNestedNameSpecifier) |
1202 | 415 | MaybeAddConstructorResults(R); |
1203 | 415 | } |
1204 | | |
1205 | 388 | static void setInBaseClass(ResultBuilder::Result &R) { |
1206 | 388 | R.Priority += CCD_InBaseClass; |
1207 | 388 | R.InBaseClass = true; |
1208 | 388 | } |
1209 | | |
1210 | | enum class OverloadCompare { BothViable, Dominates, Dominated }; |
1211 | | // Will Candidate ever be called on the object, when overloaded with Incumbent? |
1212 | | // Returns Dominates if Candidate is always called, Dominated if Incumbent is |
1213 | | // always called, BothViable if either may be called dependending on arguments. |
1214 | | // Precondition: must actually be overloads! |
1215 | | static OverloadCompare compareOverloads(const CXXMethodDecl &Candidate, |
1216 | | const CXXMethodDecl &Incumbent, |
1217 | | const Qualifiers &ObjectQuals, |
1218 | 350 | ExprValueKind ObjectKind) { |
1219 | | // Base/derived shadowing is handled elsewhere. |
1220 | 350 | if (Candidate.getDeclContext() != Incumbent.getDeclContext()) |
1221 | 184 | return OverloadCompare::BothViable; |
1222 | 166 | if (Candidate.isVariadic() != Incumbent.isVariadic() || |
1223 | 166 | Candidate.getNumParams() != Incumbent.getNumParams() || |
1224 | 166 | Candidate.getMinRequiredArguments() != |
1225 | 166 | Incumbent.getMinRequiredArguments()) |
1226 | 0 | return OverloadCompare::BothViable; |
1227 | 173 | for (unsigned I = 0, E = Candidate.getNumParams(); 166 I != E; ++I7 ) |
1228 | 165 | if (Candidate.parameters()[I]->getType().getCanonicalType() != |
1229 | 165 | Incumbent.parameters()[I]->getType().getCanonicalType()) |
1230 | 158 | return OverloadCompare::BothViable; |
1231 | 8 | if (!llvm::empty(Candidate.specific_attrs<EnableIfAttr>()) || |
1232 | 8 | !llvm::empty(Incumbent.specific_attrs<EnableIfAttr>())) |
1233 | 0 | return OverloadCompare::BothViable; |
1234 | | // At this point, we know calls can't pick one or the other based on |
1235 | | // arguments, so one of the two must win. (Or both fail, handled elsewhere). |
1236 | 8 | RefQualifierKind CandidateRef = Candidate.getRefQualifier(); |
1237 | 8 | RefQualifierKind IncumbentRef = Incumbent.getRefQualifier(); |
1238 | 8 | if (CandidateRef != IncumbentRef) { |
1239 | | // If the object kind is LValue/RValue, there's one acceptable ref-qualifier |
1240 | | // and it can't be mixed with ref-unqualified overloads (in valid code). |
1241 | | |
1242 | | // For xvalue objects, we prefer the rvalue overload even if we have to |
1243 | | // add qualifiers (which is rare, because const&& is rare). |
1244 | 2 | if (ObjectKind == clang::VK_XValue) |
1245 | 1 | return CandidateRef == RQ_RValue ? OverloadCompare::Dominates |
1246 | 1 | : OverloadCompare::Dominated0 ; |
1247 | 2 | } |
1248 | | // Now the ref qualifiers are the same (or we're in some invalid state). |
1249 | | // So make some decision based on the qualifiers. |
1250 | 7 | Qualifiers CandidateQual = Candidate.getMethodQualifiers(); |
1251 | 7 | Qualifiers IncumbentQual = Incumbent.getMethodQualifiers(); |
1252 | 7 | bool CandidateSuperset = CandidateQual.compatiblyIncludes(IncumbentQual); |
1253 | 7 | bool IncumbentSuperset = IncumbentQual.compatiblyIncludes(CandidateQual); |
1254 | 7 | if (CandidateSuperset == IncumbentSuperset) |
1255 | 0 | return OverloadCompare::BothViable; |
1256 | 7 | return IncumbentSuperset ? OverloadCompare::Dominates2 |
1257 | 7 | : OverloadCompare::Dominated5 ; |
1258 | 7 | } |
1259 | | |
1260 | | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, |
1261 | 17.3k | NamedDecl *Hiding, bool InBaseClass = false) { |
1262 | 17.3k | if (R.Kind != Result::RK_Declaration) { |
1263 | | // For non-declaration results, just add the result. |
1264 | 0 | Results.push_back(R); |
1265 | 0 | return; |
1266 | 0 | } |
1267 | | |
1268 | | // Look through using declarations. |
1269 | 17.3k | if (const auto *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { |
1270 | 121 | CodeCompletionResult Result(Using->getTargetDecl(), |
1271 | 121 | getBasePriority(Using->getTargetDecl()), |
1272 | 121 | R.Qualifier); |
1273 | 121 | Result.ShadowDecl = Using; |
1274 | 121 | AddResult(Result, CurContext, Hiding); |
1275 | 121 | return; |
1276 | 121 | } |
1277 | | |
1278 | 17.2k | bool AsNestedNameSpecifier = false; |
1279 | 17.2k | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) |
1280 | 6.19k | return; |
1281 | | |
1282 | | // C++ constructors are never found by name lookup. |
1283 | 11.0k | if (isConstructor(R.Declaration)) |
1284 | 607 | return; |
1285 | | |
1286 | 10.4k | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)1.84k ) |
1287 | 1.60k | return; |
1288 | | |
1289 | | // Make sure that any given declaration only shows up in the result set once. |
1290 | 8.82k | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second) |
1291 | 2.06k | return; |
1292 | | |
1293 | | // If the filter is for nested-name-specifiers, then this result starts a |
1294 | | // nested-name-specifier. |
1295 | 6.76k | if (AsNestedNameSpecifier) { |
1296 | 204 | R.StartsNestedNameSpecifier = true; |
1297 | 204 | R.Priority = CCP_NestedNameSpecifier; |
1298 | 6.56k | } else if (Filter == &ResultBuilder::IsMember && !R.Qualifier895 && |
1299 | 6.56k | InBaseClass840 && |
1300 | 6.56k | isa<CXXRecordDecl>( |
1301 | 84 | R.Declaration->getDeclContext()->getRedeclContext())) |
1302 | 84 | R.QualifierIsInformative = true; |
1303 | | |
1304 | | // If this result is supposed to have an informative qualifier, add one. |
1305 | 6.76k | if (R.QualifierIsInformative && !R.Qualifier84 && |
1306 | 6.76k | !R.StartsNestedNameSpecifier84 ) { |
1307 | 84 | const DeclContext *Ctx = R.Declaration->getDeclContext(); |
1308 | 84 | if (const auto *Namespace = dyn_cast<NamespaceDecl>(Ctx)) |
1309 | 0 | R.Qualifier = |
1310 | 0 | NestedNameSpecifier::Create(SemaRef.Context, nullptr, Namespace); |
1311 | 84 | else if (const auto *Tag = dyn_cast<TagDecl>(Ctx)) |
1312 | 84 | R.Qualifier = NestedNameSpecifier::Create( |
1313 | 84 | SemaRef.Context, nullptr, false, |
1314 | 84 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); |
1315 | 0 | else |
1316 | 0 | R.QualifierIsInformative = false; |
1317 | 84 | } |
1318 | | |
1319 | | // Adjust the priority if this result comes from a base class. |
1320 | 6.76k | if (InBaseClass) |
1321 | 258 | setInBaseClass(R); |
1322 | | |
1323 | 6.76k | AdjustResultPriorityForDecl(R); |
1324 | | |
1325 | 6.76k | if (HasObjectTypeQualifiers) |
1326 | 1.57k | if (const auto *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) |
1327 | 884 | if (Method->isInstance()) { |
1328 | 853 | Qualifiers MethodQuals = Method->getMethodQualifiers(); |
1329 | 853 | if (ObjectTypeQualifiers == MethodQuals) |
1330 | 759 | R.Priority += CCD_ObjectQualifierMatch; |
1331 | 94 | else if (ObjectTypeQualifiers - MethodQuals) { |
1332 | | // The method cannot be invoked, because doing so would drop |
1333 | | // qualifiers. |
1334 | 49 | return; |
1335 | 49 | } |
1336 | | // Detect cases where a ref-qualified method cannot be invoked. |
1337 | 804 | switch (Method->getRefQualifier()) { |
1338 | 7 | case RQ_LValue: |
1339 | 7 | if (ObjectKind != VK_LValue && !MethodQuals.hasConst()4 ) |
1340 | 2 | return; |
1341 | 5 | break; |
1342 | 5 | case RQ_RValue: |
1343 | 3 | if (ObjectKind == VK_LValue) |
1344 | 1 | return; |
1345 | 2 | break; |
1346 | 794 | case RQ_None: |
1347 | 794 | break; |
1348 | 804 | } |
1349 | | |
1350 | | /// Check whether this dominates another overloaded method, which should |
1351 | | /// be suppressed (or vice versa). |
1352 | | /// Motivating case is const_iterator begin() const vs iterator begin(). |
1353 | 801 | auto &OverloadSet = OverloadMap[std::make_pair( |
1354 | 801 | CurContext, Method->getDeclName().getAsOpaqueInteger())]; |
1355 | 801 | for (const DeclIndexPair Entry : OverloadSet) { |
1356 | 350 | Result &Incumbent = Results[Entry.second]; |
1357 | 350 | switch (compareOverloads(*Method, |
1358 | 350 | *cast<CXXMethodDecl>(Incumbent.Declaration), |
1359 | 350 | ObjectTypeQualifiers, ObjectKind)) { |
1360 | 3 | case OverloadCompare::Dominates: |
1361 | | // Replace the dominated overload with this one. |
1362 | | // FIXME: if the overload dominates multiple incumbents then we |
1363 | | // should remove all. But two overloads is by far the common case. |
1364 | 3 | Incumbent = std::move(R); |
1365 | 3 | return; |
1366 | 5 | case OverloadCompare::Dominated: |
1367 | | // This overload can't be called, drop it. |
1368 | 5 | return; |
1369 | 342 | case OverloadCompare::BothViable: |
1370 | 342 | break; |
1371 | 350 | } |
1372 | 350 | } |
1373 | 793 | OverloadSet.Add(Method, Results.size()); |
1374 | 793 | } |
1375 | | |
1376 | | // Insert this result into the set of results. |
1377 | 6.70k | Results.push_back(R); |
1378 | | |
1379 | 6.70k | if (!AsNestedNameSpecifier) |
1380 | 6.50k | MaybeAddConstructorResults(R); |
1381 | 6.70k | } |
1382 | | |
1383 | 164k | void ResultBuilder::AddResult(Result R) { |
1384 | 164k | assert(R.Kind != Result::RK_Declaration && |
1385 | 164k | "Declaration results need more context"); |
1386 | 0 | Results.push_back(R); |
1387 | 164k | } |
1388 | | |
1389 | | /// Enter into a new scope. |
1390 | 2.02k | void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); } |
1391 | | |
1392 | | /// Exit from the current scope. |
1393 | 2.02k | void ResultBuilder::ExitScope() { |
1394 | 2.02k | ShadowMaps.pop_back(); |
1395 | 2.02k | } |
1396 | | |
1397 | | /// Determines whether this given declaration will be found by |
1398 | | /// ordinary name lookup. |
1399 | 7.46k | bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const { |
1400 | 7.46k | ND = ND->getUnderlyingDecl(); |
1401 | | |
1402 | | // If name lookup finds a local extern declaration, then we are in a |
1403 | | // context where it behaves like an ordinary name. |
1404 | 7.46k | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
1405 | 7.46k | if (SemaRef.getLangOpts().CPlusPlus) |
1406 | 7.05k | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
1407 | 410 | else if (SemaRef.getLangOpts().ObjC) { |
1408 | 285 | if (isa<ObjCIvarDecl>(ND)) |
1409 | 36 | return true; |
1410 | 285 | } |
1411 | | |
1412 | 7.43k | return ND->getIdentifierNamespace() & IDNS; |
1413 | 7.46k | } |
1414 | | |
1415 | | /// Determines whether this given declaration will be found by |
1416 | | /// ordinary name lookup but is not a type name. |
1417 | 772 | bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const { |
1418 | 772 | ND = ND->getUnderlyingDecl(); |
1419 | 772 | if (isa<TypeDecl>(ND)) |
1420 | 152 | return false; |
1421 | | // Objective-C interfaces names are not filtered by this method because they |
1422 | | // can be used in a class property expression. We can still filter out |
1423 | | // @class declarations though. |
1424 | 620 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) { |
1425 | 92 | if (!ID->getDefinition()) |
1426 | 56 | return false; |
1427 | 92 | } |
1428 | | |
1429 | 564 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
1430 | 564 | if (SemaRef.getLangOpts().CPlusPlus) |
1431 | 0 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; |
1432 | 564 | else if (SemaRef.getLangOpts().ObjC) { |
1433 | 377 | if (isa<ObjCIvarDecl>(ND)) |
1434 | 10 | return true; |
1435 | 377 | } |
1436 | | |
1437 | 554 | return ND->getIdentifierNamespace() & IDNS; |
1438 | 564 | } |
1439 | | |
1440 | 18 | bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const { |
1441 | 18 | if (!IsOrdinaryNonTypeName(ND)) |
1442 | 0 | return 0; |
1443 | | |
1444 | 18 | if (const auto *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) |
1445 | 18 | if (VD->getType()->isIntegralOrEnumerationType()) |
1446 | 16 | return true; |
1447 | | |
1448 | 2 | return false; |
1449 | 18 | } |
1450 | | |
1451 | | /// Determines whether this given declaration will be found by |
1452 | | /// ordinary name lookup. |
1453 | 453 | bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const { |
1454 | 453 | ND = ND->getUnderlyingDecl(); |
1455 | | |
1456 | 453 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; |
1457 | 453 | if (SemaRef.getLangOpts().CPlusPlus) |
1458 | 139 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; |
1459 | | |
1460 | 453 | return (ND->getIdentifierNamespace() & IDNS) && !isa<ValueDecl>(ND)449 && |
1461 | 453 | !isa<FunctionTemplateDecl>(ND)368 && !isa<ObjCPropertyDecl>(ND)368 ; |
1462 | 453 | } |
1463 | | |
1464 | | /// Determines whether the given declaration is suitable as the |
1465 | | /// start of a C++ nested-name-specifier, e.g., a class or namespace. |
1466 | 202 | bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const { |
1467 | | // Allow us to find class templates, too. |
1468 | 202 | if (const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
1469 | 1 | ND = ClassTemplate->getTemplatedDecl(); |
1470 | | |
1471 | 202 | return SemaRef.isAcceptableNestedNameSpecifier(ND); |
1472 | 202 | } |
1473 | | |
1474 | | /// Determines whether the given declaration is an enumeration. |
1475 | 11 | bool ResultBuilder::IsEnum(const NamedDecl *ND) const { |
1476 | 11 | return isa<EnumDecl>(ND); |
1477 | 11 | } |
1478 | | |
1479 | | /// Determines whether the given declaration is a class or struct. |
1480 | 34 | bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const { |
1481 | | // Allow us to find class templates, too. |
1482 | 34 | if (const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
1483 | 1 | ND = ClassTemplate->getTemplatedDecl(); |
1484 | | |
1485 | | // For purposes of this check, interfaces match too. |
1486 | 34 | if (const auto *RD = dyn_cast<RecordDecl>(ND)) |
1487 | 34 | return RD->getTagKind() == TTK_Class || RD->getTagKind() == TTK_Struct24 || |
1488 | 34 | RD->getTagKind() == TTK_Interface0 ; |
1489 | | |
1490 | 0 | return false; |
1491 | 34 | } |
1492 | | |
1493 | | /// Determines whether the given declaration is a union. |
1494 | 0 | bool ResultBuilder::IsUnion(const NamedDecl *ND) const { |
1495 | | // Allow us to find class templates, too. |
1496 | 0 | if (const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) |
1497 | 0 | ND = ClassTemplate->getTemplatedDecl(); |
1498 | |
|
1499 | 0 | if (const auto *RD = dyn_cast<RecordDecl>(ND)) |
1500 | 0 | return RD->getTagKind() == TTK_Union; |
1501 | | |
1502 | 0 | return false; |
1503 | 0 | } |
1504 | | |
1505 | | /// Determines whether the given declaration is a namespace. |
1506 | 2 | bool ResultBuilder::IsNamespace(const NamedDecl *ND) const { |
1507 | 2 | return isa<NamespaceDecl>(ND); |
1508 | 2 | } |
1509 | | |
1510 | | /// Determines whether the given declaration is a namespace or |
1511 | | /// namespace alias. |
1512 | 19 | bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const { |
1513 | 19 | return isa<NamespaceDecl>(ND->getUnderlyingDecl()); |
1514 | 19 | } |
1515 | | |
1516 | | /// Determines whether the given declaration is a type. |
1517 | 9 | bool ResultBuilder::IsType(const NamedDecl *ND) const { |
1518 | 9 | ND = ND->getUnderlyingDecl(); |
1519 | 9 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)4 ; |
1520 | 9 | } |
1521 | | |
1522 | | /// Determines which members of a class should be visible via |
1523 | | /// "." or "->". Only value declarations, nested name specifiers, and |
1524 | | /// using declarations thereof should show up. |
1525 | 1.47k | bool ResultBuilder::IsMember(const NamedDecl *ND) const { |
1526 | 1.47k | ND = ND->getUnderlyingDecl(); |
1527 | 1.47k | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)200 || |
1528 | 1.47k | isa<ObjCPropertyDecl>(ND)194 ; |
1529 | 1.47k | } |
1530 | | |
1531 | 175 | static bool isObjCReceiverType(ASTContext &C, QualType T) { |
1532 | 175 | T = C.getCanonicalType(T); |
1533 | 175 | switch (T->getTypeClass()) { |
1534 | 0 | case Type::ObjCObject: |
1535 | 56 | case Type::ObjCInterface: |
1536 | 101 | case Type::ObjCObjectPointer: |
1537 | 101 | return true; |
1538 | | |
1539 | 50 | case Type::Builtin: |
1540 | 50 | switch (cast<BuiltinType>(T)->getKind()) { |
1541 | 0 | case BuiltinType::ObjCId: |
1542 | 0 | case BuiltinType::ObjCClass: |
1543 | 0 | case BuiltinType::ObjCSel: |
1544 | 0 | return true; |
1545 | | |
1546 | 50 | default: |
1547 | 50 | break; |
1548 | 50 | } |
1549 | 50 | return false; |
1550 | | |
1551 | 24 | default: |
1552 | 24 | break; |
1553 | 175 | } |
1554 | | |
1555 | 24 | if (!C.getLangOpts().CPlusPlus) |
1556 | 14 | return false; |
1557 | | |
1558 | | // FIXME: We could perform more analysis here to determine whether a |
1559 | | // particular class type has any conversions to Objective-C types. For now, |
1560 | | // just accept all class types. |
1561 | 10 | return T->isDependentType() || T->isRecordType(); |
1562 | 24 | } |
1563 | | |
1564 | 175 | bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const { |
1565 | 175 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
1566 | 175 | if (T.isNull()) |
1567 | 0 | return false; |
1568 | | |
1569 | 175 | T = SemaRef.Context.getBaseElementType(T); |
1570 | 175 | return isObjCReceiverType(SemaRef.Context, T); |
1571 | 175 | } |
1572 | | |
1573 | | bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture( |
1574 | 43 | const NamedDecl *ND) const { |
1575 | 43 | if (IsObjCMessageReceiver(ND)) |
1576 | 26 | return true; |
1577 | | |
1578 | 17 | const auto *Var = dyn_cast<VarDecl>(ND); |
1579 | 17 | if (!Var) |
1580 | 9 | return false; |
1581 | | |
1582 | 8 | return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>()4 ; |
1583 | 17 | } |
1584 | | |
1585 | 34 | bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const { |
1586 | 34 | if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)0 ) || |
1587 | 34 | (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND))) |
1588 | 16 | return false; |
1589 | | |
1590 | 18 | QualType T = getDeclUsageType(SemaRef.Context, ND); |
1591 | 18 | if (T.isNull()) |
1592 | 0 | return false; |
1593 | | |
1594 | 18 | T = SemaRef.Context.getBaseElementType(T); |
1595 | 18 | return T->isObjCObjectType() || T->isObjCObjectPointerType()14 || |
1596 | 18 | T->isObjCIdType()8 || |
1597 | 18 | (8 SemaRef.getLangOpts().CPlusPlus8 && T->isRecordType()0 ); |
1598 | 18 | } |
1599 | | |
1600 | 26 | bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const { |
1601 | 26 | return false; |
1602 | 26 | } |
1603 | | |
1604 | | /// Determines whether the given declaration is an Objective-C |
1605 | | /// instance variable. |
1606 | 28 | bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const { |
1607 | 28 | return isa<ObjCIvarDecl>(ND); |
1608 | 28 | } |
1609 | | |
1610 | | namespace { |
1611 | | |
1612 | | /// Visible declaration consumer that adds a code-completion result |
1613 | | /// for each visible declaration. |
1614 | | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { |
1615 | | ResultBuilder &Results; |
1616 | | DeclContext *InitialLookupCtx; |
1617 | | // NamingClass and BaseType are used for access-checking. See |
1618 | | // Sema::IsSimplyAccessible for details. |
1619 | | CXXRecordDecl *NamingClass; |
1620 | | QualType BaseType; |
1621 | | std::vector<FixItHint> FixIts; |
1622 | | |
1623 | | public: |
1624 | | CodeCompletionDeclConsumer( |
1625 | | ResultBuilder &Results, DeclContext *InitialLookupCtx, |
1626 | | QualType BaseType = QualType(), |
1627 | | std::vector<FixItHint> FixIts = std::vector<FixItHint>()) |
1628 | | : Results(Results), InitialLookupCtx(InitialLookupCtx), |
1629 | 957 | FixIts(std::move(FixIts)) { |
1630 | 957 | NamingClass = llvm::dyn_cast<CXXRecordDecl>(InitialLookupCtx); |
1631 | | // If BaseType was not provided explicitly, emulate implicit 'this->'. |
1632 | 957 | if (BaseType.isNull()) { |
1633 | 819 | auto ThisType = Results.getSema().getCurrentThisType(); |
1634 | 819 | if (!ThisType.isNull()) { |
1635 | 41 | assert(ThisType->isPointerType()); |
1636 | 0 | BaseType = ThisType->getPointeeType(); |
1637 | 41 | if (!NamingClass) |
1638 | 37 | NamingClass = BaseType->getAsCXXRecordDecl(); |
1639 | 41 | } |
1640 | 819 | } |
1641 | 0 | this->BaseType = BaseType; |
1642 | 957 | } |
1643 | | |
1644 | | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, |
1645 | 17.0k | bool InBaseClass) override { |
1646 | 17.0k | ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr, |
1647 | 17.0k | false, IsAccessible(ND, Ctx), FixIts); |
1648 | 17.0k | Results.AddResult(Result, InitialLookupCtx, Hiding, InBaseClass); |
1649 | 17.0k | } |
1650 | | |
1651 | 1.02k | void EnteredContext(DeclContext *Ctx) override { |
1652 | 1.02k | Results.addVisitedContext(Ctx); |
1653 | 1.02k | } |
1654 | | |
1655 | | private: |
1656 | 17.0k | bool IsAccessible(NamedDecl *ND, DeclContext *Ctx) { |
1657 | | // Naming class to use for access check. In most cases it was provided |
1658 | | // explicitly (e.g. member access (lhs.foo) or qualified lookup (X::)), |
1659 | | // for unqualified lookup we fallback to the \p Ctx in which we found the |
1660 | | // member. |
1661 | 17.0k | auto *NamingClass = this->NamingClass; |
1662 | 17.0k | QualType BaseType = this->BaseType; |
1663 | 17.0k | if (auto *Cls = llvm::dyn_cast_or_null<CXXRecordDecl>(Ctx)) { |
1664 | 2.14k | if (!NamingClass) |
1665 | 0 | NamingClass = Cls; |
1666 | | // When we emulate implicit 'this->' in an unqualified lookup, we might |
1667 | | // end up with an invalid naming class. In that case, we avoid emulating |
1668 | | // 'this->' qualifier to satisfy preconditions of the access checking. |
1669 | 2.14k | if (NamingClass->getCanonicalDecl() != Cls->getCanonicalDecl() && |
1670 | 2.14k | !NamingClass->isDerivedFrom(Cls)407 ) { |
1671 | 17 | NamingClass = Cls; |
1672 | 17 | BaseType = QualType(); |
1673 | 17 | } |
1674 | 14.9k | } else { |
1675 | | // The decl was found outside the C++ class, so only ObjC access checks |
1676 | | // apply. Those do not rely on NamingClass and BaseType, so we clear them |
1677 | | // out. |
1678 | 14.9k | NamingClass = nullptr; |
1679 | 14.9k | BaseType = QualType(); |
1680 | 14.9k | } |
1681 | 17.0k | return Results.getSema().IsSimplyAccessible(ND, NamingClass, BaseType); |
1682 | 17.0k | } |
1683 | | }; |
1684 | | } // namespace |
1685 | | |
1686 | | /// Add type specifiers for the current language as keyword results. |
1687 | | static void AddTypeSpecifierResults(const LangOptions &LangOpts, |
1688 | 539 | ResultBuilder &Results) { |
1689 | 539 | typedef CodeCompletionResult Result; |
1690 | 539 | Results.AddResult(Result("short", CCP_Type)); |
1691 | 539 | Results.AddResult(Result("long", CCP_Type)); |
1692 | 539 | Results.AddResult(Result("signed", CCP_Type)); |
1693 | 539 | Results.AddResult(Result("unsigned", CCP_Type)); |
1694 | 539 | Results.AddResult(Result("void", CCP_Type)); |
1695 | 539 | Results.AddResult(Result("char", CCP_Type)); |
1696 | 539 | Results.AddResult(Result("int", CCP_Type)); |
1697 | 539 | Results.AddResult(Result("float", CCP_Type)); |
1698 | 539 | Results.AddResult(Result("double", CCP_Type)); |
1699 | 539 | Results.AddResult(Result("enum", CCP_Type)); |
1700 | 539 | Results.AddResult(Result("struct", CCP_Type)); |
1701 | 539 | Results.AddResult(Result("union", CCP_Type)); |
1702 | 539 | Results.AddResult(Result("const", CCP_Type)); |
1703 | 539 | Results.AddResult(Result("volatile", CCP_Type)); |
1704 | | |
1705 | 539 | if (LangOpts.C99) { |
1706 | | // C99-specific |
1707 | 97 | Results.AddResult(Result("_Complex", CCP_Type)); |
1708 | 97 | Results.AddResult(Result("_Imaginary", CCP_Type)); |
1709 | 97 | Results.AddResult(Result("_Bool", CCP_Type)); |
1710 | 97 | Results.AddResult(Result("restrict", CCP_Type)); |
1711 | 97 | } |
1712 | | |
1713 | 539 | CodeCompletionBuilder Builder(Results.getAllocator(), |
1714 | 539 | Results.getCodeCompletionTUInfo()); |
1715 | 539 | if (LangOpts.CPlusPlus) { |
1716 | | // C++-specific |
1717 | 442 | Results.AddResult( |
1718 | 442 | Result("bool", CCP_Type + (LangOpts.ObjC ? CCD_bool_in_ObjC40 : 0402 ))); |
1719 | 442 | Results.AddResult(Result("class", CCP_Type)); |
1720 | 442 | Results.AddResult(Result("wchar_t", CCP_Type)); |
1721 | | |
1722 | | // typename name |
1723 | 442 | Builder.AddTypedTextChunk("typename"); |
1724 | 442 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
1725 | 442 | Builder.AddPlaceholderChunk("name"); |
1726 | 442 | Results.AddResult(Result(Builder.TakeString())); |
1727 | | |
1728 | 442 | if (LangOpts.CPlusPlus11) { |
1729 | 433 | Results.AddResult(Result("auto", CCP_Type)); |
1730 | 433 | Results.AddResult(Result("char16_t", CCP_Type)); |
1731 | 433 | Results.AddResult(Result("char32_t", CCP_Type)); |
1732 | | |
1733 | 433 | Builder.AddTypedTextChunk("decltype"); |
1734 | 433 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
1735 | 433 | Builder.AddPlaceholderChunk("expression"); |
1736 | 433 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
1737 | 433 | Results.AddResult(Result(Builder.TakeString())); |
1738 | 433 | } |
1739 | 442 | } else |
1740 | 97 | Results.AddResult(Result("__auto_type", CCP_Type)); |
1741 | | |
1742 | | // GNU keywords |
1743 | 539 | if (LangOpts.GNUKeywords) { |
1744 | | // FIXME: Enable when we actually support decimal floating point. |
1745 | | // Results.AddResult(Result("_Decimal32")); |
1746 | | // Results.AddResult(Result("_Decimal64")); |
1747 | | // Results.AddResult(Result("_Decimal128")); |
1748 | | |
1749 | 366 | Builder.AddTypedTextChunk("typeof"); |
1750 | 366 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
1751 | 366 | Builder.AddPlaceholderChunk("expression"); |
1752 | 366 | Results.AddResult(Result(Builder.TakeString())); |
1753 | | |
1754 | 366 | Builder.AddTypedTextChunk("typeof"); |
1755 | 366 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
1756 | 366 | Builder.AddPlaceholderChunk("type"); |
1757 | 366 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
1758 | 366 | Results.AddResult(Result(Builder.TakeString())); |
1759 | 366 | } |
1760 | | |
1761 | | // Nullability |
1762 | 539 | Results.AddResult(Result("_Nonnull", CCP_Type)); |
1763 | 539 | Results.AddResult(Result("_Null_unspecified", CCP_Type)); |
1764 | 539 | Results.AddResult(Result("_Nullable", CCP_Type)); |
1765 | 539 | } |
1766 | | |
1767 | | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, |
1768 | | const LangOptions &LangOpts, |
1769 | 217 | ResultBuilder &Results) { |
1770 | 217 | typedef CodeCompletionResult Result; |
1771 | | // Note: we don't suggest either "auto" or "register", because both |
1772 | | // are pointless as storage specifiers. Elsewhere, we suggest "auto" |
1773 | | // in C++0x as a type specifier. |
1774 | 217 | Results.AddResult(Result("extern")); |
1775 | 217 | Results.AddResult(Result("static")); |
1776 | | |
1777 | 217 | if (LangOpts.CPlusPlus11) { |
1778 | 128 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
1779 | 128 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
1780 | | |
1781 | | // alignas |
1782 | 128 | Builder.AddTypedTextChunk("alignas"); |
1783 | 128 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
1784 | 128 | Builder.AddPlaceholderChunk("expression"); |
1785 | 128 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
1786 | 128 | Results.AddResult(Result(Builder.TakeString())); |
1787 | | |
1788 | 128 | Results.AddResult(Result("constexpr")); |
1789 | 128 | Results.AddResult(Result("thread_local")); |
1790 | 128 | } |
1791 | 217 | } |
1792 | | |
1793 | | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, |
1794 | | const LangOptions &LangOpts, |
1795 | 63 | ResultBuilder &Results) { |
1796 | 63 | typedef CodeCompletionResult Result; |
1797 | 63 | switch (CCC) { |
1798 | 9 | case Sema::PCC_Class: |
1799 | 9 | case Sema::PCC_MemberTemplate: |
1800 | 9 | if (LangOpts.CPlusPlus) { |
1801 | 9 | Results.AddResult(Result("explicit")); |
1802 | 9 | Results.AddResult(Result("friend")); |
1803 | 9 | Results.AddResult(Result("mutable")); |
1804 | 9 | Results.AddResult(Result("virtual")); |
1805 | 9 | } |
1806 | 9 | LLVM_FALLTHROUGH; |
1807 | | |
1808 | 10 | case Sema::PCC_ObjCInterface: |
1809 | 12 | case Sema::PCC_ObjCImplementation: |
1810 | 63 | case Sema::PCC_Namespace: |
1811 | 63 | case Sema::PCC_Template: |
1812 | 63 | if (LangOpts.CPlusPlus || LangOpts.C9928 ) |
1813 | 63 | Results.AddResult(Result("inline")); |
1814 | 63 | break; |
1815 | | |
1816 | 0 | case Sema::PCC_ObjCInstanceVariableList: |
1817 | 0 | case Sema::PCC_Expression: |
1818 | 0 | case Sema::PCC_Statement: |
1819 | 0 | case Sema::PCC_ForInit: |
1820 | 0 | case Sema::PCC_Condition: |
1821 | 0 | case Sema::PCC_RecoveryInFunction: |
1822 | 0 | case Sema::PCC_Type: |
1823 | 0 | case Sema::PCC_ParenthesizedExpression: |
1824 | 0 | case Sema::PCC_LocalDeclarationSpecifiers: |
1825 | 0 | break; |
1826 | 63 | } |
1827 | 63 | } |
1828 | | |
1829 | | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); |
1830 | | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); |
1831 | | static void AddObjCVisibilityResults(const LangOptions &LangOpts, |
1832 | | ResultBuilder &Results, bool NeedAt); |
1833 | | static void AddObjCImplementationResults(const LangOptions &LangOpts, |
1834 | | ResultBuilder &Results, bool NeedAt); |
1835 | | static void AddObjCInterfaceResults(const LangOptions &LangOpts, |
1836 | | ResultBuilder &Results, bool NeedAt); |
1837 | | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); |
1838 | | |
1839 | 202 | static void AddTypedefResult(ResultBuilder &Results) { |
1840 | 202 | CodeCompletionBuilder Builder(Results.getAllocator(), |
1841 | 202 | Results.getCodeCompletionTUInfo()); |
1842 | 202 | Builder.AddTypedTextChunk("typedef"); |
1843 | 202 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
1844 | 202 | Builder.AddPlaceholderChunk("type"); |
1845 | 202 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
1846 | 202 | Builder.AddPlaceholderChunk("name"); |
1847 | 202 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
1848 | 202 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
1849 | 202 | } |
1850 | | |
1851 | | // using name = type |
1852 | | static void AddUsingAliasResult(CodeCompletionBuilder &Builder, |
1853 | 122 | ResultBuilder &Results) { |
1854 | 122 | Builder.AddTypedTextChunk("using"); |
1855 | 122 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
1856 | 122 | Builder.AddPlaceholderChunk("name"); |
1857 | 122 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
1858 | 122 | Builder.AddPlaceholderChunk("type"); |
1859 | 122 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
1860 | 122 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
1861 | 122 | } |
1862 | | |
1863 | | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, |
1864 | 1.16k | const LangOptions &LangOpts) { |
1865 | 1.16k | switch (CCC) { |
1866 | 51 | case Sema::PCC_Namespace: |
1867 | 60 | case Sema::PCC_Class: |
1868 | 61 | case Sema::PCC_ObjCInstanceVariableList: |
1869 | 61 | case Sema::PCC_Template: |
1870 | 61 | case Sema::PCC_MemberTemplate: |
1871 | 335 | case Sema::PCC_Statement: |
1872 | 338 | case Sema::PCC_RecoveryInFunction: |
1873 | 346 | case Sema::PCC_Type: |
1874 | 406 | case Sema::PCC_ParenthesizedExpression: |
1875 | 407 | case Sema::PCC_LocalDeclarationSpecifiers: |
1876 | 407 | return true; |
1877 | | |
1878 | 733 | case Sema::PCC_Expression: |
1879 | 745 | case Sema::PCC_Condition: |
1880 | 745 | return LangOpts.CPlusPlus; |
1881 | | |
1882 | 1 | case Sema::PCC_ObjCInterface: |
1883 | 3 | case Sema::PCC_ObjCImplementation: |
1884 | 3 | return false; |
1885 | | |
1886 | 12 | case Sema::PCC_ForInit: |
1887 | 12 | return LangOpts.CPlusPlus || LangOpts.ObjC || LangOpts.C990 ; |
1888 | 1.16k | } |
1889 | | |
1890 | 1.16k | llvm_unreachable0 ("Invalid ParserCompletionContext!"); |
1891 | 1.16k | } |
1892 | | |
1893 | | static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context, |
1894 | 71.8k | const Preprocessor &PP) { |
1895 | 71.8k | PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP); |
1896 | 71.8k | Policy.AnonymousTagLocations = false; |
1897 | 71.8k | Policy.SuppressStrongLifetime = true; |
1898 | 71.8k | Policy.SuppressUnwrittenScope = true; |
1899 | 71.8k | Policy.SuppressScope = true; |
1900 | 71.8k | return Policy; |
1901 | 71.8k | } |
1902 | | |
1903 | | /// Retrieve a printing policy suitable for code completion. |
1904 | 511 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { |
1905 | 511 | return getCompletionPrintingPolicy(S.Context, S.PP); |
1906 | 511 | } |
1907 | | |
1908 | | /// Retrieve the string representation of the given type as a string |
1909 | | /// that has the appropriate lifetime for code completion. |
1910 | | /// |
1911 | | /// This routine provides a fast path where we provide constant strings for |
1912 | | /// common type names. |
1913 | | static const char *GetCompletionTypeString(QualType T, ASTContext &Context, |
1914 | | const PrintingPolicy &Policy, |
1915 | 4.28k | CodeCompletionAllocator &Allocator) { |
1916 | 4.28k | if (!T.getLocalQualifiers()) { |
1917 | | // Built-in type names are constant strings. |
1918 | 4.25k | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) |
1919 | 3.12k | return BT->getNameAsCString(Policy); |
1920 | | |
1921 | | // Anonymous tag types are constant strings. |
1922 | 1.13k | if (const TagType *TagT = dyn_cast<TagType>(T)) |
1923 | 123 | if (TagDecl *Tag = TagT->getDecl()) |
1924 | 123 | if (!Tag->hasNameForLinkage()) { |
1925 | 4 | switch (Tag->getTagKind()) { |
1926 | 0 | case TTK_Struct: |
1927 | 0 | return "struct <anonymous>"; |
1928 | 0 | case TTK_Interface: |
1929 | 0 | return "__interface <anonymous>"; |
1930 | 0 | case TTK_Class: |
1931 | 0 | return "class <anonymous>"; |
1932 | 0 | case TTK_Union: |
1933 | 0 | return "union <anonymous>"; |
1934 | 4 | case TTK_Enum: |
1935 | 4 | return "enum <anonymous>"; |
1936 | 4 | } |
1937 | 4 | } |
1938 | 1.13k | } |
1939 | | |
1940 | | // Slow path: format the type as a string. |
1941 | 1.15k | std::string Result; |
1942 | 1.15k | T.getAsStringInternal(Result, Policy); |
1943 | 1.15k | return Allocator.CopyString(Result); |
1944 | 4.28k | } |
1945 | | |
1946 | | /// Add a completion for "this", if we're in a member function. |
1947 | 412 | static void addThisCompletion(Sema &S, ResultBuilder &Results) { |
1948 | 412 | QualType ThisTy = S.getCurrentThisType(); |
1949 | 412 | if (ThisTy.isNull()) |
1950 | 372 | return; |
1951 | | |
1952 | 40 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
1953 | 40 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
1954 | 40 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
1955 | 40 | Builder.AddResultTypeChunk( |
1956 | 40 | GetCompletionTypeString(ThisTy, S.Context, Policy, Allocator)); |
1957 | 40 | Builder.AddTypedTextChunk("this"); |
1958 | 40 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
1959 | 40 | } |
1960 | | |
1961 | | static void AddStaticAssertResult(CodeCompletionBuilder &Builder, |
1962 | | ResultBuilder &Results, |
1963 | 177 | const LangOptions &LangOpts) { |
1964 | 177 | if (!LangOpts.CPlusPlus11) |
1965 | 55 | return; |
1966 | | |
1967 | 122 | Builder.AddTypedTextChunk("static_assert"); |
1968 | 122 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
1969 | 122 | Builder.AddPlaceholderChunk("expression"); |
1970 | 122 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
1971 | 122 | Builder.AddPlaceholderChunk("message"); |
1972 | 122 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
1973 | 122 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
1974 | 122 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); |
1975 | 122 | } |
1976 | | |
1977 | | static void AddOverrideResults(ResultBuilder &Results, |
1978 | | const CodeCompletionContext &CCContext, |
1979 | 9 | CodeCompletionBuilder &Builder) { |
1980 | 9 | Sema &S = Results.getSema(); |
1981 | 9 | const auto *CR = llvm::dyn_cast<CXXRecordDecl>(S.CurContext); |
1982 | | // If not inside a class/struct/union return empty. |
1983 | 9 | if (!CR) |
1984 | 0 | return; |
1985 | | // First store overrides within current class. |
1986 | | // These are stored by name to make querying fast in the later step. |
1987 | 9 | llvm::StringMap<std::vector<FunctionDecl *>> Overrides; |
1988 | 9 | for (auto *Method : CR->methods()) { |
1989 | 7 | if (!Method->isVirtual() || !Method->getIdentifier()3 ) |
1990 | 4 | continue; |
1991 | 3 | Overrides[Method->getName()].push_back(Method); |
1992 | 3 | } |
1993 | | |
1994 | 9 | for (const auto &Base : CR->bases()) { |
1995 | 2 | const auto *BR = Base.getType().getTypePtr()->getAsCXXRecordDecl(); |
1996 | 2 | if (!BR) |
1997 | 0 | continue; |
1998 | 16 | for (auto *Method : BR->methods())2 { |
1999 | 16 | if (!Method->isVirtual() || !Method->getIdentifier()4 ) |
2000 | 12 | continue; |
2001 | 4 | const auto it = Overrides.find(Method->getName()); |
2002 | 4 | bool IsOverriden = false; |
2003 | 4 | if (it != Overrides.end()) { |
2004 | 2 | for (auto *MD : it->second) { |
2005 | | // If the method in current body is not an overload of this virtual |
2006 | | // function, then it overrides this one. |
2007 | 2 | if (!S.IsOverload(MD, Method, false)) { |
2008 | 0 | IsOverriden = true; |
2009 | 0 | break; |
2010 | 0 | } |
2011 | 2 | } |
2012 | 2 | } |
2013 | 4 | if (!IsOverriden) { |
2014 | | // Generates a new CodeCompletionResult by taking this function and |
2015 | | // converting it into an override declaration with only one chunk in the |
2016 | | // final CodeCompletionString as a TypedTextChunk. |
2017 | 4 | std::string OverrideSignature; |
2018 | 4 | llvm::raw_string_ostream OS(OverrideSignature); |
2019 | 4 | CodeCompletionResult CCR(Method, 0); |
2020 | 4 | PrintingPolicy Policy = |
2021 | 4 | getCompletionPrintingPolicy(S.getASTContext(), S.getPreprocessor()); |
2022 | 4 | auto *CCS = CCR.createCodeCompletionStringForOverride( |
2023 | 4 | S.getPreprocessor(), S.getASTContext(), Builder, |
2024 | 4 | /*IncludeBriefComments=*/false, CCContext, Policy); |
2025 | 4 | Results.AddResult(CodeCompletionResult(CCS, Method, CCP_CodePattern)); |
2026 | 4 | } |
2027 | 4 | } |
2028 | 2 | } |
2029 | 9 | } |
2030 | | |
2031 | | /// Add language constructs that show up for "ordinary" names. |
2032 | | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, Scope *S, |
2033 | 625 | Sema &SemaRef, ResultBuilder &Results) { |
2034 | 625 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
2035 | 625 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
2036 | | |
2037 | 625 | typedef CodeCompletionResult Result; |
2038 | 625 | switch (CCC) { |
2039 | 51 | case Sema::PCC_Namespace: |
2040 | 51 | if (SemaRef.getLangOpts().CPlusPlus) { |
2041 | 26 | if (Results.includeCodePatterns()) { |
2042 | | // namespace <identifier> { declarations } |
2043 | 3 | Builder.AddTypedTextChunk("namespace"); |
2044 | 3 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2045 | 3 | Builder.AddPlaceholderChunk("identifier"); |
2046 | 3 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2047 | 3 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
2048 | 3 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2049 | 3 | Builder.AddPlaceholderChunk("declarations"); |
2050 | 3 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2051 | 3 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
2052 | 3 | Results.AddResult(Result(Builder.TakeString())); |
2053 | 3 | } |
2054 | | |
2055 | | // namespace identifier = identifier ; |
2056 | 26 | Builder.AddTypedTextChunk("namespace"); |
2057 | 26 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2058 | 26 | Builder.AddPlaceholderChunk("name"); |
2059 | 26 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
2060 | 26 | Builder.AddPlaceholderChunk("namespace"); |
2061 | 26 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2062 | 26 | Results.AddResult(Result(Builder.TakeString())); |
2063 | | |
2064 | | // Using directives |
2065 | 26 | Builder.AddTypedTextChunk("using namespace"); |
2066 | 26 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2067 | 26 | Builder.AddPlaceholderChunk("identifier"); |
2068 | 26 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2069 | 26 | Results.AddResult(Result(Builder.TakeString())); |
2070 | | |
2071 | | // asm(string-literal) |
2072 | 26 | Builder.AddTypedTextChunk("asm"); |
2073 | 26 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2074 | 26 | Builder.AddPlaceholderChunk("string-literal"); |
2075 | 26 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2076 | 26 | Results.AddResult(Result(Builder.TakeString())); |
2077 | | |
2078 | 26 | if (Results.includeCodePatterns()) { |
2079 | | // Explicit template instantiation |
2080 | 3 | Builder.AddTypedTextChunk("template"); |
2081 | 3 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2082 | 3 | Builder.AddPlaceholderChunk("declaration"); |
2083 | 3 | Results.AddResult(Result(Builder.TakeString())); |
2084 | 23 | } else { |
2085 | 23 | Results.AddResult(Result("template", CodeCompletionResult::RK_Keyword)); |
2086 | 23 | } |
2087 | 26 | } |
2088 | | |
2089 | 51 | if (SemaRef.getLangOpts().ObjC) |
2090 | 13 | AddObjCTopLevelResults(Results, true); |
2091 | | |
2092 | 51 | AddTypedefResult(Results); |
2093 | 51 | LLVM_FALLTHROUGH; |
2094 | | |
2095 | 60 | case Sema::PCC_Class: |
2096 | 60 | if (SemaRef.getLangOpts().CPlusPlus) { |
2097 | | // Using declaration |
2098 | 35 | Builder.AddTypedTextChunk("using"); |
2099 | 35 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2100 | 35 | Builder.AddPlaceholderChunk("qualifier"); |
2101 | 35 | Builder.AddTextChunk("::"); |
2102 | 35 | Builder.AddPlaceholderChunk("name"); |
2103 | 35 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2104 | 35 | Results.AddResult(Result(Builder.TakeString())); |
2105 | | |
2106 | 35 | if (SemaRef.getLangOpts().CPlusPlus11) |
2107 | 33 | AddUsingAliasResult(Builder, Results); |
2108 | | |
2109 | | // using typename qualifier::name (only in a dependent context) |
2110 | 35 | if (SemaRef.CurContext->isDependentContext()) { |
2111 | 0 | Builder.AddTypedTextChunk("using typename"); |
2112 | 0 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2113 | 0 | Builder.AddPlaceholderChunk("qualifier"); |
2114 | 0 | Builder.AddTextChunk("::"); |
2115 | 0 | Builder.AddPlaceholderChunk("name"); |
2116 | 0 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2117 | 0 | Results.AddResult(Result(Builder.TakeString())); |
2118 | 0 | } |
2119 | | |
2120 | 35 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
2121 | | |
2122 | 35 | if (CCC == Sema::PCC_Class) { |
2123 | 9 | AddTypedefResult(Results); |
2124 | | |
2125 | 9 | bool IsNotInheritanceScope = |
2126 | 9 | !(S->getFlags() & Scope::ClassInheritanceScope); |
2127 | | // public: |
2128 | 9 | Builder.AddTypedTextChunk("public"); |
2129 | 9 | if (IsNotInheritanceScope && Results.includeCodePatterns()8 ) |
2130 | 3 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
2131 | 9 | Results.AddResult(Result(Builder.TakeString())); |
2132 | | |
2133 | | // protected: |
2134 | 9 | Builder.AddTypedTextChunk("protected"); |
2135 | 9 | if (IsNotInheritanceScope && Results.includeCodePatterns()8 ) |
2136 | 3 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
2137 | 9 | Results.AddResult(Result(Builder.TakeString())); |
2138 | | |
2139 | | // private: |
2140 | 9 | Builder.AddTypedTextChunk("private"); |
2141 | 9 | if (IsNotInheritanceScope && Results.includeCodePatterns()8 ) |
2142 | 3 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
2143 | 9 | Results.AddResult(Result(Builder.TakeString())); |
2144 | | |
2145 | | // FIXME: This adds override results only if we are at the first word of |
2146 | | // the declaration/definition. Also call this from other sides to have |
2147 | | // more use-cases. |
2148 | 9 | AddOverrideResults(Results, CodeCompletionContext::CCC_ClassStructUnion, |
2149 | 9 | Builder); |
2150 | 9 | } |
2151 | 35 | } |
2152 | 60 | LLVM_FALLTHROUGH; |
2153 | | |
2154 | 60 | case Sema::PCC_Template: |
2155 | 60 | case Sema::PCC_MemberTemplate: |
2156 | 60 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()35 ) { |
2157 | | // template < parameters > |
2158 | 7 | Builder.AddTypedTextChunk("template"); |
2159 | 7 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
2160 | 7 | Builder.AddPlaceholderChunk("parameters"); |
2161 | 7 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
2162 | 7 | Results.AddResult(Result(Builder.TakeString())); |
2163 | 53 | } else { |
2164 | 53 | Results.AddResult(Result("template", CodeCompletionResult::RK_Keyword)); |
2165 | 53 | } |
2166 | | |
2167 | 60 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
2168 | 60 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
2169 | 60 | break; |
2170 | | |
2171 | 1 | case Sema::PCC_ObjCInterface: |
2172 | 1 | AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true); |
2173 | 1 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
2174 | 1 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
2175 | 1 | break; |
2176 | | |
2177 | 2 | case Sema::PCC_ObjCImplementation: |
2178 | 2 | AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true); |
2179 | 2 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
2180 | 2 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
2181 | 2 | break; |
2182 | | |
2183 | 1 | case Sema::PCC_ObjCInstanceVariableList: |
2184 | 1 | AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true); |
2185 | 1 | break; |
2186 | | |
2187 | 3 | case Sema::PCC_RecoveryInFunction: |
2188 | 142 | case Sema::PCC_Statement: { |
2189 | 142 | if (SemaRef.getLangOpts().CPlusPlus11) |
2190 | 89 | AddUsingAliasResult(Builder, Results); |
2191 | | |
2192 | 142 | AddTypedefResult(Results); |
2193 | | |
2194 | 142 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()93 && |
2195 | 142 | SemaRef.getLangOpts().CXXExceptions17 ) { |
2196 | 2 | Builder.AddTypedTextChunk("try"); |
2197 | 2 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2198 | 2 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
2199 | 2 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2200 | 2 | Builder.AddPlaceholderChunk("statements"); |
2201 | 2 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2202 | 2 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
2203 | 2 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2204 | 2 | Builder.AddTextChunk("catch"); |
2205 | 2 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2206 | 2 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2207 | 2 | Builder.AddPlaceholderChunk("declaration"); |
2208 | 2 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2209 | 2 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2210 | 2 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
2211 | 2 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2212 | 2 | Builder.AddPlaceholderChunk("statements"); |
2213 | 2 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2214 | 2 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
2215 | 2 | Results.AddResult(Result(Builder.TakeString())); |
2216 | 2 | } |
2217 | 142 | if (SemaRef.getLangOpts().ObjC) |
2218 | 43 | AddObjCStatementResults(Results, true); |
2219 | | |
2220 | 142 | if (Results.includeCodePatterns()) { |
2221 | | // if (condition) { statements } |
2222 | 20 | Builder.AddTypedTextChunk("if"); |
2223 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2224 | 20 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2225 | 20 | if (SemaRef.getLangOpts().CPlusPlus) |
2226 | 17 | Builder.AddPlaceholderChunk("condition"); |
2227 | 3 | else |
2228 | 3 | Builder.AddPlaceholderChunk("expression"); |
2229 | 20 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2230 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2231 | 20 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
2232 | 20 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2233 | 20 | Builder.AddPlaceholderChunk("statements"); |
2234 | 20 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2235 | 20 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
2236 | 20 | Results.AddResult(Result(Builder.TakeString())); |
2237 | | |
2238 | | // switch (condition) { } |
2239 | 20 | Builder.AddTypedTextChunk("switch"); |
2240 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2241 | 20 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2242 | 20 | if (SemaRef.getLangOpts().CPlusPlus) |
2243 | 17 | Builder.AddPlaceholderChunk("condition"); |
2244 | 3 | else |
2245 | 3 | Builder.AddPlaceholderChunk("expression"); |
2246 | 20 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2247 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2248 | 20 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
2249 | 20 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2250 | 20 | Builder.AddPlaceholderChunk("cases"); |
2251 | 20 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2252 | 20 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
2253 | 20 | Results.AddResult(Result(Builder.TakeString())); |
2254 | 20 | } |
2255 | | |
2256 | | // Switch-specific statements. |
2257 | 142 | if (SemaRef.getCurFunction() && |
2258 | 142 | !SemaRef.getCurFunction()->SwitchStack.empty()) { |
2259 | | // case expression: |
2260 | 0 | Builder.AddTypedTextChunk("case"); |
2261 | 0 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2262 | 0 | Builder.AddPlaceholderChunk("expression"); |
2263 | 0 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
2264 | 0 | Results.AddResult(Result(Builder.TakeString())); |
2265 | | |
2266 | | // default: |
2267 | 0 | Builder.AddTypedTextChunk("default"); |
2268 | 0 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
2269 | 0 | Results.AddResult(Result(Builder.TakeString())); |
2270 | 0 | } |
2271 | | |
2272 | 142 | if (Results.includeCodePatterns()) { |
2273 | | /// while (condition) { statements } |
2274 | 20 | Builder.AddTypedTextChunk("while"); |
2275 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2276 | 20 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2277 | 20 | if (SemaRef.getLangOpts().CPlusPlus) |
2278 | 17 | Builder.AddPlaceholderChunk("condition"); |
2279 | 3 | else |
2280 | 3 | Builder.AddPlaceholderChunk("expression"); |
2281 | 20 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2282 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2283 | 20 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
2284 | 20 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2285 | 20 | Builder.AddPlaceholderChunk("statements"); |
2286 | 20 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2287 | 20 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
2288 | 20 | Results.AddResult(Result(Builder.TakeString())); |
2289 | | |
2290 | | // do { statements } while ( expression ); |
2291 | 20 | Builder.AddTypedTextChunk("do"); |
2292 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2293 | 20 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
2294 | 20 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2295 | 20 | Builder.AddPlaceholderChunk("statements"); |
2296 | 20 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2297 | 20 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
2298 | 20 | Builder.AddTextChunk("while"); |
2299 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2300 | 20 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2301 | 20 | Builder.AddPlaceholderChunk("expression"); |
2302 | 20 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2303 | 20 | Results.AddResult(Result(Builder.TakeString())); |
2304 | | |
2305 | | // for ( for-init-statement ; condition ; expression ) { statements } |
2306 | 20 | Builder.AddTypedTextChunk("for"); |
2307 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2308 | 20 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2309 | 20 | if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C993 ) |
2310 | 20 | Builder.AddPlaceholderChunk("init-statement"); |
2311 | 0 | else |
2312 | 0 | Builder.AddPlaceholderChunk("init-expression"); |
2313 | 20 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2314 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2315 | 20 | Builder.AddPlaceholderChunk("condition"); |
2316 | 20 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2317 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2318 | 20 | Builder.AddPlaceholderChunk("inc-expression"); |
2319 | 20 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2320 | 20 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2321 | 20 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
2322 | 20 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2323 | 20 | Builder.AddPlaceholderChunk("statements"); |
2324 | 20 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2325 | 20 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
2326 | 20 | Results.AddResult(Result(Builder.TakeString())); |
2327 | | |
2328 | 20 | if (SemaRef.getLangOpts().CPlusPlus11 || SemaRef.getLangOpts().ObjC7 ) { |
2329 | | // for ( range_declaration (:|in) range_expression ) { statements } |
2330 | 15 | Builder.AddTypedTextChunk("for"); |
2331 | 15 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2332 | 15 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2333 | 15 | Builder.AddPlaceholderChunk("range-declaration"); |
2334 | 15 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2335 | 15 | if (SemaRef.getLangOpts().ObjC) |
2336 | 2 | Builder.AddTextChunk("in"); |
2337 | 13 | else |
2338 | 13 | Builder.AddChunk(CodeCompletionString::CK_Colon); |
2339 | 15 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2340 | 15 | Builder.AddPlaceholderChunk("range-expression"); |
2341 | 15 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2342 | 15 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2343 | 15 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); |
2344 | 15 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2345 | 15 | Builder.AddPlaceholderChunk("statements"); |
2346 | 15 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); |
2347 | 15 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); |
2348 | 15 | Results.AddResult(Result(Builder.TakeString())); |
2349 | 15 | } |
2350 | 20 | } |
2351 | | |
2352 | 142 | if (S->getContinueParent()) { |
2353 | | // continue ; |
2354 | 4 | Builder.AddTypedTextChunk("continue"); |
2355 | 4 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2356 | 4 | Results.AddResult(Result(Builder.TakeString())); |
2357 | 4 | } |
2358 | | |
2359 | 142 | if (S->getBreakParent()) { |
2360 | | // break ; |
2361 | 4 | Builder.AddTypedTextChunk("break"); |
2362 | 4 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2363 | 4 | Results.AddResult(Result(Builder.TakeString())); |
2364 | 4 | } |
2365 | | |
2366 | | // "return expression ;" or "return ;", depending on the return type. |
2367 | 142 | QualType ReturnType; |
2368 | 142 | if (const auto *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) |
2369 | 128 | ReturnType = Function->getReturnType(); |
2370 | 14 | else if (const auto *Method = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) |
2371 | 14 | ReturnType = Method->getReturnType(); |
2372 | 0 | else if (SemaRef.getCurBlock() && |
2373 | 0 | !SemaRef.getCurBlock()->ReturnType.isNull()) |
2374 | 0 | ReturnType = SemaRef.getCurBlock()->ReturnType;; |
2375 | 142 | if (ReturnType.isNull() || ReturnType->isVoidType()) { |
2376 | 115 | Builder.AddTypedTextChunk("return"); |
2377 | 115 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2378 | 115 | Results.AddResult(Result(Builder.TakeString())); |
2379 | 115 | } else { |
2380 | 27 | assert(!ReturnType.isNull()); |
2381 | | // "return expression ;" |
2382 | 0 | Builder.AddTypedTextChunk("return"); |
2383 | 27 | Builder.AddChunk(clang::CodeCompletionString::CK_HorizontalSpace); |
2384 | 27 | Builder.AddPlaceholderChunk("expression"); |
2385 | 27 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2386 | 27 | Results.AddResult(Result(Builder.TakeString())); |
2387 | | // When boolean, also add 'return true;' and 'return false;'. |
2388 | 27 | if (ReturnType->isBooleanType()) { |
2389 | 1 | Builder.AddTypedTextChunk("return true"); |
2390 | 1 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2391 | 1 | Results.AddResult(Result(Builder.TakeString())); |
2392 | | |
2393 | 1 | Builder.AddTypedTextChunk("return false"); |
2394 | 1 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2395 | 1 | Results.AddResult(Result(Builder.TakeString())); |
2396 | 1 | } |
2397 | | // For pointers, suggest 'return nullptr' in C++. |
2398 | 27 | if (SemaRef.getLangOpts().CPlusPlus11 && |
2399 | 27 | (13 ReturnType->isPointerType()13 || ReturnType->isMemberPointerType()12 )) { |
2400 | 2 | Builder.AddTypedTextChunk("return nullptr"); |
2401 | 2 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2402 | 2 | Results.AddResult(Result(Builder.TakeString())); |
2403 | 2 | } |
2404 | 27 | } |
2405 | | |
2406 | | // goto identifier ; |
2407 | 0 | Builder.AddTypedTextChunk("goto"); |
2408 | 142 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2409 | 142 | Builder.AddPlaceholderChunk("label"); |
2410 | 142 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2411 | 142 | Results.AddResult(Result(Builder.TakeString())); |
2412 | | |
2413 | | // Using directives |
2414 | 142 | Builder.AddTypedTextChunk("using namespace"); |
2415 | 142 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2416 | 142 | Builder.AddPlaceholderChunk("identifier"); |
2417 | 142 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); |
2418 | 142 | Results.AddResult(Result(Builder.TakeString())); |
2419 | | |
2420 | 142 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); |
2421 | 142 | } |
2422 | 142 | LLVM_FALLTHROUGH; |
2423 | | |
2424 | | // Fall through (for statement expressions). |
2425 | 148 | case Sema::PCC_ForInit: |
2426 | 154 | case Sema::PCC_Condition: |
2427 | 154 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); |
2428 | | // Fall through: conditions and statements can have expressions. |
2429 | 154 | LLVM_FALLTHROUGH; |
2430 | | |
2431 | 184 | case Sema::PCC_ParenthesizedExpression: |
2432 | 184 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
2433 | 184 | CCC == Sema::PCC_ParenthesizedExpression7 ) { |
2434 | | // (__bridge <type>)<expression> |
2435 | 1 | Builder.AddTypedTextChunk("__bridge"); |
2436 | 1 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2437 | 1 | Builder.AddPlaceholderChunk("type"); |
2438 | 1 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2439 | 1 | Builder.AddPlaceholderChunk("expression"); |
2440 | 1 | Results.AddResult(Result(Builder.TakeString())); |
2441 | | |
2442 | | // (__bridge_transfer <Objective-C type>)<expression> |
2443 | 1 | Builder.AddTypedTextChunk("__bridge_transfer"); |
2444 | 1 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2445 | 1 | Builder.AddPlaceholderChunk("Objective-C type"); |
2446 | 1 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2447 | 1 | Builder.AddPlaceholderChunk("expression"); |
2448 | 1 | Results.AddResult(Result(Builder.TakeString())); |
2449 | | |
2450 | | // (__bridge_retained <CF type>)<expression> |
2451 | 1 | Builder.AddTypedTextChunk("__bridge_retained"); |
2452 | 1 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2453 | 1 | Builder.AddPlaceholderChunk("CF type"); |
2454 | 1 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2455 | 1 | Builder.AddPlaceholderChunk("expression"); |
2456 | 1 | Results.AddResult(Result(Builder.TakeString())); |
2457 | 1 | } |
2458 | | // Fall through |
2459 | 184 | LLVM_FALLTHROUGH; |
2460 | | |
2461 | 552 | case Sema::PCC_Expression: { |
2462 | 552 | if (SemaRef.getLangOpts().CPlusPlus) { |
2463 | | // 'this', if we're in a non-static member function. |
2464 | 406 | addThisCompletion(SemaRef, Results); |
2465 | | |
2466 | | // true |
2467 | 406 | Builder.AddResultTypeChunk("bool"); |
2468 | 406 | Builder.AddTypedTextChunk("true"); |
2469 | 406 | Results.AddResult(Result(Builder.TakeString())); |
2470 | | |
2471 | | // false |
2472 | 406 | Builder.AddResultTypeChunk("bool"); |
2473 | 406 | Builder.AddTypedTextChunk("false"); |
2474 | 406 | Results.AddResult(Result(Builder.TakeString())); |
2475 | | |
2476 | 406 | if (SemaRef.getLangOpts().RTTI) { |
2477 | | // dynamic_cast < type-id > ( expression ) |
2478 | 404 | Builder.AddTypedTextChunk("dynamic_cast"); |
2479 | 404 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
2480 | 404 | Builder.AddPlaceholderChunk("type"); |
2481 | 404 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
2482 | 404 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2483 | 404 | Builder.AddPlaceholderChunk("expression"); |
2484 | 404 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2485 | 404 | Results.AddResult(Result(Builder.TakeString())); |
2486 | 404 | } |
2487 | | |
2488 | | // static_cast < type-id > ( expression ) |
2489 | 406 | Builder.AddTypedTextChunk("static_cast"); |
2490 | 406 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
2491 | 406 | Builder.AddPlaceholderChunk("type"); |
2492 | 406 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
2493 | 406 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2494 | 406 | Builder.AddPlaceholderChunk("expression"); |
2495 | 406 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2496 | 406 | Results.AddResult(Result(Builder.TakeString())); |
2497 | | |
2498 | | // reinterpret_cast < type-id > ( expression ) |
2499 | 406 | Builder.AddTypedTextChunk("reinterpret_cast"); |
2500 | 406 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
2501 | 406 | Builder.AddPlaceholderChunk("type"); |
2502 | 406 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
2503 | 406 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2504 | 406 | Builder.AddPlaceholderChunk("expression"); |
2505 | 406 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2506 | 406 | Results.AddResult(Result(Builder.TakeString())); |
2507 | | |
2508 | | // const_cast < type-id > ( expression ) |
2509 | 406 | Builder.AddTypedTextChunk("const_cast"); |
2510 | 406 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); |
2511 | 406 | Builder.AddPlaceholderChunk("type"); |
2512 | 406 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); |
2513 | 406 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2514 | 406 | Builder.AddPlaceholderChunk("expression"); |
2515 | 406 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2516 | 406 | Results.AddResult(Result(Builder.TakeString())); |
2517 | | |
2518 | 406 | if (SemaRef.getLangOpts().RTTI) { |
2519 | | // typeid ( expression-or-type ) |
2520 | 404 | Builder.AddResultTypeChunk("std::type_info"); |
2521 | 404 | Builder.AddTypedTextChunk("typeid"); |
2522 | 404 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2523 | 404 | Builder.AddPlaceholderChunk("expression-or-type"); |
2524 | 404 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2525 | 404 | Results.AddResult(Result(Builder.TakeString())); |
2526 | 404 | } |
2527 | | |
2528 | | // new T ( ... ) |
2529 | 406 | Builder.AddTypedTextChunk("new"); |
2530 | 406 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2531 | 406 | Builder.AddPlaceholderChunk("type"); |
2532 | 406 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2533 | 406 | Builder.AddPlaceholderChunk("expressions"); |
2534 | 406 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2535 | 406 | Results.AddResult(Result(Builder.TakeString())); |
2536 | | |
2537 | | // new T [ ] ( ... ) |
2538 | 406 | Builder.AddTypedTextChunk("new"); |
2539 | 406 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2540 | 406 | Builder.AddPlaceholderChunk("type"); |
2541 | 406 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
2542 | 406 | Builder.AddPlaceholderChunk("size"); |
2543 | 406 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
2544 | 406 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2545 | 406 | Builder.AddPlaceholderChunk("expressions"); |
2546 | 406 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2547 | 406 | Results.AddResult(Result(Builder.TakeString())); |
2548 | | |
2549 | | // delete expression |
2550 | 406 | Builder.AddResultTypeChunk("void"); |
2551 | 406 | Builder.AddTypedTextChunk("delete"); |
2552 | 406 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2553 | 406 | Builder.AddPlaceholderChunk("expression"); |
2554 | 406 | Results.AddResult(Result(Builder.TakeString())); |
2555 | | |
2556 | | // delete [] expression |
2557 | 406 | Builder.AddResultTypeChunk("void"); |
2558 | 406 | Builder.AddTypedTextChunk("delete"); |
2559 | 406 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2560 | 406 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); |
2561 | 406 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); |
2562 | 406 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2563 | 406 | Builder.AddPlaceholderChunk("expression"); |
2564 | 406 | Results.AddResult(Result(Builder.TakeString())); |
2565 | | |
2566 | 406 | if (SemaRef.getLangOpts().CXXExceptions) { |
2567 | | // throw expression |
2568 | 283 | Builder.AddResultTypeChunk("void"); |
2569 | 283 | Builder.AddTypedTextChunk("throw"); |
2570 | 283 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
2571 | 283 | Builder.AddPlaceholderChunk("expression"); |
2572 | 283 | Results.AddResult(Result(Builder.TakeString())); |
2573 | 283 | } |
2574 | | |
2575 | | // FIXME: Rethrow? |
2576 | | |
2577 | 406 | if (SemaRef.getLangOpts().CPlusPlus11) { |
2578 | | // nullptr |
2579 | 399 | Builder.AddResultTypeChunk("std::nullptr_t"); |
2580 | 399 | Builder.AddTypedTextChunk("nullptr"); |
2581 | 399 | Results.AddResult(Result(Builder.TakeString())); |
2582 | | |
2583 | | // alignof |
2584 | 399 | Builder.AddResultTypeChunk("size_t"); |
2585 | 399 | Builder.AddTypedTextChunk("alignof"); |
2586 | 399 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2587 | 399 | Builder.AddPlaceholderChunk("type"); |
2588 | 399 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2589 | 399 | Results.AddResult(Result(Builder.TakeString())); |
2590 | | |
2591 | | // noexcept |
2592 | 399 | Builder.AddResultTypeChunk("bool"); |
2593 | 399 | Builder.AddTypedTextChunk("noexcept"); |
2594 | 399 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2595 | 399 | Builder.AddPlaceholderChunk("expression"); |
2596 | 399 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2597 | 399 | Results.AddResult(Result(Builder.TakeString())); |
2598 | | |
2599 | | // sizeof... expression |
2600 | 399 | Builder.AddResultTypeChunk("size_t"); |
2601 | 399 | Builder.AddTypedTextChunk("sizeof..."); |
2602 | 399 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2603 | 399 | Builder.AddPlaceholderChunk("parameter-pack"); |
2604 | 399 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2605 | 399 | Results.AddResult(Result(Builder.TakeString())); |
2606 | 399 | } |
2607 | 406 | } |
2608 | | |
2609 | 552 | if (SemaRef.getLangOpts().ObjC) { |
2610 | | // Add "super", if we're in an Objective-C class with a superclass. |
2611 | 101 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { |
2612 | | // The interface can be NULL. |
2613 | 28 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) |
2614 | 28 | if (ID->getSuperClass()) { |
2615 | 5 | std::string SuperType; |
2616 | 5 | SuperType = ID->getSuperClass()->getNameAsString(); |
2617 | 5 | if (Method->isInstanceMethod()) |
2618 | 5 | SuperType += " *"; |
2619 | | |
2620 | 5 | Builder.AddResultTypeChunk(Allocator.CopyString(SuperType)); |
2621 | 5 | Builder.AddTypedTextChunk("super"); |
2622 | 5 | Results.AddResult(Result(Builder.TakeString())); |
2623 | 5 | } |
2624 | 28 | } |
2625 | | |
2626 | 101 | AddObjCExpressionResults(Results, true); |
2627 | 101 | } |
2628 | | |
2629 | 552 | if (SemaRef.getLangOpts().C11) { |
2630 | | // _Alignof |
2631 | 143 | Builder.AddResultTypeChunk("size_t"); |
2632 | 143 | if (SemaRef.PP.isMacroDefined("alignof")) |
2633 | 0 | Builder.AddTypedTextChunk("alignof"); |
2634 | 143 | else |
2635 | 143 | Builder.AddTypedTextChunk("_Alignof"); |
2636 | 143 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2637 | 143 | Builder.AddPlaceholderChunk("type"); |
2638 | 143 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2639 | 143 | Results.AddResult(Result(Builder.TakeString())); |
2640 | 143 | } |
2641 | | |
2642 | | // sizeof expression |
2643 | 552 | Builder.AddResultTypeChunk("size_t"); |
2644 | 552 | Builder.AddTypedTextChunk("sizeof"); |
2645 | 552 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
2646 | 552 | Builder.AddPlaceholderChunk("expression-or-type"); |
2647 | 552 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
2648 | 552 | Results.AddResult(Result(Builder.TakeString())); |
2649 | 552 | break; |
2650 | 184 | } |
2651 | | |
2652 | 8 | case Sema::PCC_Type: |
2653 | 9 | case Sema::PCC_LocalDeclarationSpecifiers: |
2654 | 9 | break; |
2655 | 625 | } |
2656 | | |
2657 | 625 | if (WantTypesInContext(CCC, SemaRef.getLangOpts())) |
2658 | 538 | AddTypeSpecifierResults(SemaRef.getLangOpts(), Results); |
2659 | | |
2660 | 625 | if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type441 ) |
2661 | 441 | Results.AddResult(Result("operator")); |
2662 | 625 | } |
2663 | | |
2664 | | /// If the given declaration has an associated type, add it as a result |
2665 | | /// type chunk. |
2666 | | static void AddResultTypeChunk(ASTContext &Context, |
2667 | | const PrintingPolicy &Policy, |
2668 | | const NamedDecl *ND, QualType BaseType, |
2669 | 5.52k | CodeCompletionBuilder &Result) { |
2670 | 5.52k | if (!ND) |
2671 | 0 | return; |
2672 | | |
2673 | | // Skip constructors and conversion functions, which have their return types |
2674 | | // built into their names. |
2675 | 5.52k | if (isConstructor(ND) || isa<CXXConversionDecl>(ND)5.16k ) |
2676 | 368 | return; |
2677 | | |
2678 | | // Determine the type of the declaration (if it has a type). |
2679 | 5.15k | QualType T; |
2680 | 5.15k | if (const FunctionDecl *Function = ND->getAsFunction()) |
2681 | 2.66k | T = Function->getReturnType(); |
2682 | 2.49k | else if (const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
2683 | 359 | if (!BaseType.isNull()) |
2684 | 298 | T = Method->getSendResultType(BaseType); |
2685 | 61 | else |
2686 | 61 | T = Method->getReturnType(); |
2687 | 2.13k | } else if (const auto *Enumerator = dyn_cast<EnumConstantDecl>(ND)) { |
2688 | 115 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); |
2689 | 115 | T = clang::TypeName::getFullyQualifiedType(T, Context); |
2690 | 2.01k | } else if (isa<UnresolvedUsingValueDecl>(ND)) { |
2691 | | /* Do nothing: ignore unresolved using declarations*/ |
2692 | 2.01k | } else if (const auto *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { |
2693 | 61 | if (!BaseType.isNull()) |
2694 | 28 | T = Ivar->getUsageType(BaseType); |
2695 | 33 | else |
2696 | 33 | T = Ivar->getType(); |
2697 | 1.95k | } else if (const auto *Value = dyn_cast<ValueDecl>(ND)) { |
2698 | 771 | T = Value->getType(); |
2699 | 1.18k | } else if (const auto *Property = dyn_cast<ObjCPropertyDecl>(ND)) { |
2700 | 113 | if (!BaseType.isNull()) |
2701 | 89 | T = Property->getUsageType(BaseType); |
2702 | 24 | else |
2703 | 24 | T = Property->getType(); |
2704 | 113 | } |
2705 | | |
2706 | 5.15k | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)4.08k ) |
2707 | 1.07k | return; |
2708 | | |
2709 | 4.08k | Result.AddResultTypeChunk( |
2710 | 4.08k | GetCompletionTypeString(T, Context, Policy, Result.getAllocator())); |
2711 | 4.08k | } |
2712 | | |
2713 | | static void MaybeAddSentinel(Preprocessor &PP, |
2714 | | const NamedDecl *FunctionOrMethod, |
2715 | 29 | CodeCompletionBuilder &Result) { |
2716 | 29 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) |
2717 | 9 | if (Sentinel->getSentinel() == 0) { |
2718 | 9 | if (PP.getLangOpts().ObjC && PP.isMacroDefined("nil")3 ) |
2719 | 3 | Result.AddTextChunk(", nil"); |
2720 | 6 | else if (PP.isMacroDefined("NULL")) |
2721 | 6 | Result.AddTextChunk(", NULL"); |
2722 | 0 | else |
2723 | 0 | Result.AddTextChunk(", (void*)0"); |
2724 | 9 | } |
2725 | 29 | } |
2726 | | |
2727 | | static std::string formatObjCParamQualifiers(unsigned ObjCQuals, |
2728 | 386 | QualType &Type) { |
2729 | 386 | std::string Result; |
2730 | 386 | if (ObjCQuals & Decl::OBJC_TQ_In) |
2731 | 2 | Result += "in "; |
2732 | 384 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) |
2733 | 6 | Result += "inout "; |
2734 | 378 | else if (ObjCQuals & Decl::OBJC_TQ_Out) |
2735 | 1 | Result += "out "; |
2736 | 386 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) |
2737 | 1 | Result += "bycopy "; |
2738 | 385 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) |
2739 | 1 | Result += "byref "; |
2740 | 386 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) |
2741 | 1 | Result += "oneway "; |
2742 | 386 | if (ObjCQuals & Decl::OBJC_TQ_CSNullability) { |
2743 | 6 | if (auto nullability = AttributedType::stripOuterNullability(Type)) { |
2744 | 1 | switch (*nullability) { |
2745 | 0 | case NullabilityKind::NonNull: |
2746 | 0 | Result += "nonnull "; |
2747 | 0 | break; |
2748 | | |
2749 | 1 | case NullabilityKind::Nullable: |
2750 | 1 | Result += "nullable "; |
2751 | 1 | break; |
2752 | | |
2753 | 0 | case NullabilityKind::Unspecified: |
2754 | 0 | Result += "null_unspecified "; |
2755 | 0 | break; |
2756 | | |
2757 | 0 | case NullabilityKind::NullableResult: |
2758 | 0 | llvm_unreachable("Not supported as a context-sensitive keyword!"); |
2759 | 0 | break; |
2760 | 1 | } |
2761 | 1 | } |
2762 | 6 | } |
2763 | 386 | return Result; |
2764 | 386 | } |
2765 | | |
2766 | | /// Tries to find the most appropriate type location for an Objective-C |
2767 | | /// block placeholder. |
2768 | | /// |
2769 | | /// This function ignores things like typedefs and qualifiers in order to |
2770 | | /// present the most relevant and accurate block placeholders in code completion |
2771 | | /// results. |
2772 | | static void findTypeLocationForBlockDecl(const TypeSourceInfo *TSInfo, |
2773 | | FunctionTypeLoc &Block, |
2774 | | FunctionProtoTypeLoc &BlockProto, |
2775 | 87 | bool SuppressBlock = false) { |
2776 | 87 | if (!TSInfo) |
2777 | 4 | return; |
2778 | 83 | TypeLoc TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); |
2779 | 105 | while (true) { |
2780 | | // Look through typedefs. |
2781 | 105 | if (!SuppressBlock) { |
2782 | 100 | if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) { |
2783 | 18 | if (TypeSourceInfo *InnerTSInfo = |
2784 | 18 | TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { |
2785 | 18 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); |
2786 | 18 | continue; |
2787 | 18 | } |
2788 | 18 | } |
2789 | | |
2790 | | // Look through qualified types |
2791 | 82 | if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) { |
2792 | 0 | TL = QualifiedTL.getUnqualifiedLoc(); |
2793 | 0 | continue; |
2794 | 0 | } |
2795 | | |
2796 | 82 | if (AttributedTypeLoc AttrTL = TL.getAs<AttributedTypeLoc>()) { |
2797 | 4 | TL = AttrTL.getModifiedLoc(); |
2798 | 4 | continue; |
2799 | 4 | } |
2800 | 82 | } |
2801 | | |
2802 | | // Try to get the function prototype behind the block pointer type, |
2803 | | // then we're done. |
2804 | 83 | if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) { |
2805 | 78 | TL = BlockPtr.getPointeeLoc().IgnoreParens(); |
2806 | 78 | Block = TL.getAs<FunctionTypeLoc>(); |
2807 | 78 | BlockProto = TL.getAs<FunctionProtoTypeLoc>(); |
2808 | 78 | } |
2809 | 83 | break; |
2810 | 105 | } |
2811 | 83 | } |
2812 | | |
2813 | | static std::string |
2814 | | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
2815 | | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
2816 | | bool SuppressBlockName = false, |
2817 | | bool SuppressBlock = false, |
2818 | | Optional<ArrayRef<QualType>> ObjCSubsts = None); |
2819 | | |
2820 | | static std::string |
2821 | | FormatFunctionParameter(const PrintingPolicy &Policy, const ParmVarDecl *Param, |
2822 | | bool SuppressName = false, bool SuppressBlock = false, |
2823 | 3.73k | Optional<ArrayRef<QualType>> ObjCSubsts = None) { |
2824 | | // Params are unavailable in FunctionTypeLoc if the FunctionType is invalid. |
2825 | | // It would be better to pass in the param Type, which is usually avaliable. |
2826 | | // But this case is rare, so just pretend we fell back to int as elsewhere. |
2827 | 3.73k | if (!Param) |
2828 | 1 | return "int"; |
2829 | 3.73k | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); |
2830 | 3.73k | if (Param->getType()->isDependentType() || |
2831 | 3.73k | !Param->getType()->isBlockPointerType()2.82k ) { |
2832 | | // The argument for a dependent or non-block parameter is a placeholder |
2833 | | // containing that parameter's type. |
2834 | 3.68k | std::string Result; |
2835 | | |
2836 | 3.68k | if (Param->getIdentifier() && !ObjCMethodParam689 && !SuppressName689 ) |
2837 | 689 | Result = std::string(Param->getIdentifier()->getName()); |
2838 | | |
2839 | 3.68k | QualType Type = Param->getType(); |
2840 | 3.68k | if (ObjCSubsts) |
2841 | 8 | Type = Type.substObjCTypeArgs(Param->getASTContext(), *ObjCSubsts, |
2842 | 8 | ObjCSubstitutionContext::Parameter); |
2843 | 3.68k | if (ObjCMethodParam) { |
2844 | 0 | Result = |
2845 | 0 | "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), Type); |
2846 | 0 | Result += Type.getAsString(Policy) + ")"; |
2847 | 0 | if (Param->getIdentifier() && !SuppressName) |
2848 | 0 | Result += Param->getIdentifier()->getName(); |
2849 | 3.68k | } else { |
2850 | 3.68k | Type.getAsStringInternal(Result, Policy); |
2851 | 3.68k | } |
2852 | 3.68k | return Result; |
2853 | 3.68k | } |
2854 | | |
2855 | | // The argument for a block pointer parameter is a block literal with |
2856 | | // the appropriate type. |
2857 | 44 | FunctionTypeLoc Block; |
2858 | 44 | FunctionProtoTypeLoc BlockProto; |
2859 | 44 | findTypeLocationForBlockDecl(Param->getTypeSourceInfo(), Block, BlockProto, |
2860 | 44 | SuppressBlock); |
2861 | | // Try to retrieve the block type information from the property if this is a |
2862 | | // parameter in a setter. |
2863 | 44 | if (!Block && ObjCMethodParam9 && |
2864 | 44 | cast<ObjCMethodDecl>(Param->getDeclContext())->isPropertyAccessor()4 ) { |
2865 | 4 | if (const auto *PD = cast<ObjCMethodDecl>(Param->getDeclContext()) |
2866 | 4 | ->findPropertyDecl(/*CheckOverrides=*/false)) |
2867 | 4 | findTypeLocationForBlockDecl(PD->getTypeSourceInfo(), Block, BlockProto, |
2868 | 4 | SuppressBlock); |
2869 | 4 | } |
2870 | | |
2871 | 44 | if (!Block) { |
2872 | | // We were unable to find a FunctionProtoTypeLoc with parameter names |
2873 | | // for the block; just use the parameter type as a placeholder. |
2874 | 5 | std::string Result; |
2875 | 5 | if (!ObjCMethodParam && Param->getIdentifier()) |
2876 | 5 | Result = std::string(Param->getIdentifier()->getName()); |
2877 | | |
2878 | 5 | QualType Type = Param->getType().getUnqualifiedType(); |
2879 | | |
2880 | 5 | if (ObjCMethodParam) { |
2881 | 0 | Result = Type.getAsString(Policy); |
2882 | 0 | std::string Quals = |
2883 | 0 | formatObjCParamQualifiers(Param->getObjCDeclQualifier(), Type); |
2884 | 0 | if (!Quals.empty()) |
2885 | 0 | Result = "(" + Quals + " " + Result + ")"; |
2886 | 0 | if (Result.back() != ')') |
2887 | 0 | Result += " "; |
2888 | 0 | if (Param->getIdentifier()) |
2889 | 0 | Result += Param->getIdentifier()->getName(); |
2890 | 5 | } else { |
2891 | 5 | Type.getAsStringInternal(Result, Policy); |
2892 | 5 | } |
2893 | | |
2894 | 5 | return Result; |
2895 | 5 | } |
2896 | | |
2897 | | // We have the function prototype behind the block pointer type, as it was |
2898 | | // written in the source. |
2899 | 39 | return formatBlockPlaceholder(Policy, Param, Block, BlockProto, |
2900 | 39 | /*SuppressBlockName=*/false, SuppressBlock, |
2901 | 39 | ObjCSubsts); |
2902 | 44 | } |
2903 | | |
2904 | | /// Returns a placeholder string that corresponds to an Objective-C block |
2905 | | /// declaration. |
2906 | | /// |
2907 | | /// \param BlockDecl A declaration with an Objective-C block type. |
2908 | | /// |
2909 | | /// \param Block The most relevant type location for that block type. |
2910 | | /// |
2911 | | /// \param SuppressBlockName Determines whether or not the name of the block |
2912 | | /// declaration is included in the resulting string. |
2913 | | static std::string |
2914 | | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, |
2915 | | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, |
2916 | | bool SuppressBlockName, bool SuppressBlock, |
2917 | 58 | Optional<ArrayRef<QualType>> ObjCSubsts) { |
2918 | 58 | std::string Result; |
2919 | 58 | QualType ResultType = Block.getTypePtr()->getReturnType(); |
2920 | 58 | if (ObjCSubsts) |
2921 | 4 | ResultType = |
2922 | 4 | ResultType.substObjCTypeArgs(BlockDecl->getASTContext(), *ObjCSubsts, |
2923 | 4 | ObjCSubstitutionContext::Result); |
2924 | 58 | if (!ResultType->isVoidType() || SuppressBlock27 ) |
2925 | 31 | ResultType.getAsStringInternal(Result, Policy); |
2926 | | |
2927 | | // Format the parameter list. |
2928 | 58 | std::string Params; |
2929 | 58 | if (!BlockProto || Block.getNumParams() == 052 ) { |
2930 | 9 | if (BlockProto && BlockProto.getTypePtr()->isVariadic()3 ) |
2931 | 0 | Params = "(...)"; |
2932 | 9 | else |
2933 | 9 | Params = "(void)"; |
2934 | 49 | } else { |
2935 | 49 | Params += "("; |
2936 | 133 | for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I84 ) { |
2937 | 84 | if (I) |
2938 | 35 | Params += ", "; |
2939 | 84 | Params += FormatFunctionParameter(Policy, Block.getParam(I), |
2940 | 84 | /*SuppressName=*/false, |
2941 | 84 | /*SuppressBlock=*/true, ObjCSubsts); |
2942 | | |
2943 | 84 | if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()49 ) |
2944 | 0 | Params += ", ..."; |
2945 | 84 | } |
2946 | 49 | Params += ")"; |
2947 | 49 | } |
2948 | | |
2949 | 58 | if (SuppressBlock) { |
2950 | | // Format as a parameter. |
2951 | 0 | Result = Result + " (^"; |
2952 | 0 | if (!SuppressBlockName && BlockDecl->getIdentifier()) |
2953 | 0 | Result += BlockDecl->getIdentifier()->getName(); |
2954 | 0 | Result += ")"; |
2955 | 0 | Result += Params; |
2956 | 58 | } else { |
2957 | | // Format as a block literal argument. |
2958 | 58 | Result = '^' + Result; |
2959 | 58 | Result += Params; |
2960 | | |
2961 | 58 | if (!SuppressBlockName && BlockDecl->getIdentifier()39 ) |
2962 | 39 | Result += BlockDecl->getIdentifier()->getName(); |
2963 | 58 | } |
2964 | | |
2965 | 58 | return Result; |
2966 | 58 | } |
2967 | | |
2968 | | static std::string GetDefaultValueString(const ParmVarDecl *Param, |
2969 | | const SourceManager &SM, |
2970 | 92 | const LangOptions &LangOpts) { |
2971 | 92 | const SourceRange SrcRange = Param->getDefaultArgRange(); |
2972 | 92 | CharSourceRange CharSrcRange = CharSourceRange::getTokenRange(SrcRange); |
2973 | 92 | bool Invalid = CharSrcRange.isInvalid(); |
2974 | 92 | if (Invalid) |
2975 | 0 | return ""; |
2976 | 92 | StringRef srcText = |
2977 | 92 | Lexer::getSourceText(CharSrcRange, SM, LangOpts, &Invalid); |
2978 | 92 | if (Invalid) |
2979 | 0 | return ""; |
2980 | | |
2981 | 92 | if (srcText.empty() || srcText == "=") { |
2982 | | // Lexer can't determine the value. |
2983 | | // This happens if the code is incorrect (for example class is forward |
2984 | | // declared). |
2985 | 7 | return ""; |
2986 | 7 | } |
2987 | 85 | std::string DefValue(srcText.str()); |
2988 | | // FIXME: remove this check if the Lexer::getSourceText value is fixed and |
2989 | | // this value always has (or always does not have) '=' in front of it |
2990 | 85 | if (DefValue.at(0) != '=') { |
2991 | | // If we don't have '=' in front of value. |
2992 | | // Lexer returns built-in types values without '=' and user-defined types |
2993 | | // values with it. |
2994 | 78 | return " = " + DefValue; |
2995 | 78 | } |
2996 | 7 | return " " + DefValue; |
2997 | 85 | } |
2998 | | |
2999 | | /// Add function parameter chunks to the given code completion string. |
3000 | | static void AddFunctionParameterChunks(Preprocessor &PP, |
3001 | | const PrintingPolicy &Policy, |
3002 | | const FunctionDecl *Function, |
3003 | | CodeCompletionBuilder &Result, |
3004 | | unsigned Start = 0, |
3005 | 2.90k | bool InOptional = false) { |
3006 | 2.90k | bool FirstParameter = true; |
3007 | | |
3008 | 6.09k | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P3.18k ) { |
3009 | 3.26k | const ParmVarDecl *Param = Function->getParamDecl(P); |
3010 | | |
3011 | 3.26k | if (Param->hasDefaultArg() && !InOptional146 ) { |
3012 | | // When we see an optional default argument, put that argument and |
3013 | | // the remaining default arguments into a new, optional string. |
3014 | 73 | CodeCompletionBuilder Opt(Result.getAllocator(), |
3015 | 73 | Result.getCodeCompletionTUInfo()); |
3016 | 73 | if (!FirstParameter) |
3017 | 46 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
3018 | 73 | AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true); |
3019 | 73 | Result.AddOptionalChunk(Opt.TakeString()); |
3020 | 73 | break; |
3021 | 73 | } |
3022 | | |
3023 | 3.18k | if (FirstParameter) |
3024 | 1.99k | FirstParameter = false; |
3025 | 1.19k | else |
3026 | 1.19k | Result.AddChunk(CodeCompletionString::CK_Comma); |
3027 | | |
3028 | 3.18k | InOptional = false; |
3029 | | |
3030 | | // Format the placeholder string. |
3031 | 3.18k | std::string PlaceholderStr = FormatFunctionParameter(Policy, Param); |
3032 | 3.18k | if (Param->hasDefaultArg()) |
3033 | 73 | PlaceholderStr += |
3034 | 73 | GetDefaultValueString(Param, PP.getSourceManager(), PP.getLangOpts()); |
3035 | | |
3036 | 3.18k | if (Function->isVariadic() && P == N - 126 ) |
3037 | 19 | PlaceholderStr += ", ..."; |
3038 | | |
3039 | | // Add the placeholder string. |
3040 | 3.18k | Result.AddPlaceholderChunk( |
3041 | 3.18k | Result.getAllocator().CopyString(PlaceholderStr)); |
3042 | 3.18k | } |
3043 | | |
3044 | 2.90k | if (const auto *Proto = Function->getType()->getAs<FunctionProtoType>()) |
3045 | 2.83k | if (Proto->isVariadic()) { |
3046 | 25 | if (Proto->getNumParams() == 0) |
3047 | 0 | Result.AddPlaceholderChunk("..."); |
3048 | | |
3049 | 25 | MaybeAddSentinel(PP, Function, Result); |
3050 | 25 | } |
3051 | 2.90k | } |
3052 | | |
3053 | | /// Add template parameter chunks to the given code completion string. |
3054 | | static void AddTemplateParameterChunks( |
3055 | | ASTContext &Context, const PrintingPolicy &Policy, |
3056 | | const TemplateDecl *Template, CodeCompletionBuilder &Result, |
3057 | 258 | unsigned MaxParameters = 0, unsigned Start = 0, bool InDefaultArg = false) { |
3058 | 258 | bool FirstParameter = true; |
3059 | | |
3060 | | // Prefer to take the template parameter names from the first declaration of |
3061 | | // the template. |
3062 | 258 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); |
3063 | | |
3064 | 258 | TemplateParameterList *Params = Template->getTemplateParameters(); |
3065 | 258 | TemplateParameterList::iterator PEnd = Params->end(); |
3066 | 258 | if (MaxParameters) |
3067 | 74 | PEnd = Params->begin() + MaxParameters; |
3068 | 522 | for (TemplateParameterList::iterator P = Params->begin() + Start; P != PEnd; |
3069 | 273 | ++P264 ) { |
3070 | 273 | bool HasDefaultArg = false; |
3071 | 273 | std::string PlaceholderStr; |
3072 | 273 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
3073 | 273 | if (TTP->wasDeclaredWithTypename()) |
3074 | 46 | PlaceholderStr = "typename"; |
3075 | 227 | else if (const auto *TC = TTP->getTypeConstraint()) { |
3076 | 0 | llvm::raw_string_ostream OS(PlaceholderStr); |
3077 | 0 | TC->print(OS, Policy); |
3078 | 0 | OS.flush(); |
3079 | 0 | } else |
3080 | 227 | PlaceholderStr = "class"; |
3081 | | |
3082 | 273 | if (TTP->getIdentifier()) { |
3083 | 271 | PlaceholderStr += ' '; |
3084 | 271 | PlaceholderStr += TTP->getIdentifier()->getName(); |
3085 | 271 | } |
3086 | | |
3087 | 273 | HasDefaultArg = TTP->hasDefaultArgument(); |
3088 | 273 | } else if (NonTypeTemplateParmDecl *0 NTTP0 = |
3089 | 0 | dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
3090 | 0 | if (NTTP->getIdentifier()) |
3091 | 0 | PlaceholderStr = std::string(NTTP->getIdentifier()->getName()); |
3092 | 0 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); |
3093 | 0 | HasDefaultArg = NTTP->hasDefaultArgument(); |
3094 | 0 | } else { |
3095 | 0 | assert(isa<TemplateTemplateParmDecl>(*P)); |
3096 | 0 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
3097 | | |
3098 | | // Since putting the template argument list into the placeholder would |
3099 | | // be very, very long, we just use an abbreviation. |
3100 | 0 | PlaceholderStr = "template<...> class"; |
3101 | 0 | if (TTP->getIdentifier()) { |
3102 | 0 | PlaceholderStr += ' '; |
3103 | 0 | PlaceholderStr += TTP->getIdentifier()->getName(); |
3104 | 0 | } |
3105 | |
|
3106 | 0 | HasDefaultArg = TTP->hasDefaultArgument(); |
3107 | 0 | } |
3108 | | |
3109 | 273 | if (HasDefaultArg && !InDefaultArg18 ) { |
3110 | | // When we see an optional default argument, put that argument and |
3111 | | // the remaining default arguments into a new, optional string. |
3112 | 9 | CodeCompletionBuilder Opt(Result.getAllocator(), |
3113 | 9 | Result.getCodeCompletionTUInfo()); |
3114 | 9 | if (!FirstParameter) |
3115 | 9 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
3116 | 9 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, |
3117 | 9 | P - Params->begin(), true); |
3118 | 9 | Result.AddOptionalChunk(Opt.TakeString()); |
3119 | 9 | break; |
3120 | 9 | } |
3121 | | |
3122 | 264 | InDefaultArg = false; |
3123 | | |
3124 | 264 | if (FirstParameter) |
3125 | 258 | FirstParameter = false; |
3126 | 6 | else |
3127 | 6 | Result.AddChunk(CodeCompletionString::CK_Comma); |
3128 | | |
3129 | | // Add the placeholder string. |
3130 | 264 | Result.AddPlaceholderChunk( |
3131 | 264 | Result.getAllocator().CopyString(PlaceholderStr)); |
3132 | 264 | } |
3133 | 258 | } |
3134 | | |
3135 | | /// Add a qualifier to the given code-completion string, if the |
3136 | | /// provided nested-name-specifier is non-NULL. |
3137 | | static void AddQualifierToCompletionString(CodeCompletionBuilder &Result, |
3138 | | NestedNameSpecifier *Qualifier, |
3139 | | bool QualifierIsInformative, |
3140 | | ASTContext &Context, |
3141 | 3.01k | const PrintingPolicy &Policy) { |
3142 | 3.01k | if (!Qualifier) |
3143 | 2.82k | return; |
3144 | | |
3145 | 195 | std::string PrintedNNS; |
3146 | 195 | { |
3147 | 195 | llvm::raw_string_ostream OS(PrintedNNS); |
3148 | 195 | Qualifier->print(OS, Policy); |
3149 | 195 | } |
3150 | 195 | if (QualifierIsInformative) |
3151 | 84 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); |
3152 | 111 | else |
3153 | 111 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); |
3154 | 195 | } |
3155 | | |
3156 | | static void |
3157 | | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, |
3158 | 2.79k | const FunctionDecl *Function) { |
3159 | 2.79k | const auto *Proto = Function->getType()->getAs<FunctionProtoType>(); |
3160 | 2.79k | if (!Proto || !Proto->getMethodQuals()2.72k ) |
3161 | 2.72k | return; |
3162 | | |
3163 | | // FIXME: Add ref-qualifier! |
3164 | | |
3165 | | // Handle single qualifiers without copying |
3166 | 78 | if (Proto->getMethodQuals().hasOnlyConst()) { |
3167 | 52 | Result.AddInformativeChunk(" const"); |
3168 | 52 | return; |
3169 | 52 | } |
3170 | | |
3171 | 26 | if (Proto->getMethodQuals().hasOnlyVolatile()) { |
3172 | 6 | Result.AddInformativeChunk(" volatile"); |
3173 | 6 | return; |
3174 | 6 | } |
3175 | | |
3176 | 20 | if (Proto->getMethodQuals().hasOnlyRestrict()) { |
3177 | 0 | Result.AddInformativeChunk(" restrict"); |
3178 | 0 | return; |
3179 | 0 | } |
3180 | | |
3181 | | // Handle multiple qualifiers. |
3182 | 20 | std::string QualsStr; |
3183 | 20 | if (Proto->isConst()) |
3184 | 20 | QualsStr += " const"; |
3185 | 20 | if (Proto->isVolatile()) |
3186 | 20 | QualsStr += " volatile"; |
3187 | 20 | if (Proto->isRestrict()) |
3188 | 0 | QualsStr += " restrict"; |
3189 | 20 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); |
3190 | 20 | } |
3191 | | |
3192 | | /// Add the name of the given declaration |
3193 | | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, |
3194 | | const NamedDecl *ND, |
3195 | 2.83k | CodeCompletionBuilder &Result) { |
3196 | 2.83k | DeclarationName Name = ND->getDeclName(); |
3197 | 2.83k | if (!Name) |
3198 | 0 | return; |
3199 | | |
3200 | 2.83k | switch (Name.getNameKind()) { |
3201 | 286 | case DeclarationName::CXXOperatorName: { |
3202 | 286 | const char *OperatorName = nullptr; |
3203 | 286 | switch (Name.getCXXOverloadedOperator()) { |
3204 | 0 | case OO_None: |
3205 | 0 | case OO_Conditional: |
3206 | 0 | case NUM_OVERLOADED_OPERATORS: |
3207 | 0 | OperatorName = "operator"; |
3208 | 0 | break; |
3209 | | |
3210 | 0 | #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ |
3211 | 284 | case OO_##Name: \ |
3212 | 284 | OperatorName = "operator" Spelling; \ |
3213 | 284 | break; |
3214 | 0 | #define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemberOnly) |
3215 | 0 | #include "clang/Basic/OperatorKinds.def" |
3216 | | |
3217 | 0 | case OO_New: |
3218 | 0 | OperatorName = "operator new"; |
3219 | 0 | break; |
3220 | 0 | case OO_Delete: |
3221 | 0 | OperatorName = "operator delete"; |
3222 | 0 | break; |
3223 | 0 | case OO_Array_New: |
3224 | 0 | OperatorName = "operator new[]"; |
3225 | 0 | break; |
3226 | 0 | case OO_Array_Delete: |
3227 | 0 | OperatorName = "operator delete[]"; |
3228 | 0 | break; |
3229 | 1 | case OO_Call: |
3230 | 1 | OperatorName = "operator()"; |
3231 | 1 | break; |
3232 | 1 | case OO_Subscript: |
3233 | 1 | OperatorName = "operator[]"; |
3234 | 1 | break; |
3235 | 286 | } |
3236 | 286 | Result.AddTypedTextChunk(OperatorName); |
3237 | 286 | break; |
3238 | 286 | } |
3239 | | |
3240 | 2.11k | case DeclarationName::Identifier: |
3241 | 2.11k | case DeclarationName::CXXConversionFunctionName: |
3242 | 2.24k | case DeclarationName::CXXDestructorName: |
3243 | 2.24k | case DeclarationName::CXXLiteralOperatorName: |
3244 | 2.24k | Result.AddTypedTextChunk( |
3245 | 2.24k | Result.getAllocator().CopyString(ND->getNameAsString())); |
3246 | 2.24k | break; |
3247 | | |
3248 | 0 | case DeclarationName::CXXDeductionGuideName: |
3249 | 0 | case DeclarationName::CXXUsingDirective: |
3250 | 0 | case DeclarationName::ObjCZeroArgSelector: |
3251 | 0 | case DeclarationName::ObjCOneArgSelector: |
3252 | 0 | case DeclarationName::ObjCMultiArgSelector: |
3253 | 0 | break; |
3254 | | |
3255 | 310 | case DeclarationName::CXXConstructorName: { |
3256 | 310 | CXXRecordDecl *Record = nullptr; |
3257 | 310 | QualType Ty = Name.getCXXNameType(); |
3258 | 310 | if (const auto *RecordTy = Ty->getAs<RecordType>()) |
3259 | 284 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
3260 | 26 | else if (const auto *InjectedTy = Ty->getAs<InjectedClassNameType>()) |
3261 | 26 | Record = InjectedTy->getDecl(); |
3262 | 0 | else { |
3263 | 0 | Result.AddTypedTextChunk( |
3264 | 0 | Result.getAllocator().CopyString(ND->getNameAsString())); |
3265 | 0 | break; |
3266 | 0 | } |
3267 | | |
3268 | 310 | Result.AddTypedTextChunk( |
3269 | 310 | Result.getAllocator().CopyString(Record->getNameAsString())); |
3270 | 310 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { |
3271 | 26 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
3272 | 26 | AddTemplateParameterChunks(Context, Policy, Template, Result); |
3273 | 26 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
3274 | 26 | } |
3275 | 310 | break; |
3276 | 310 | } |
3277 | 2.83k | } |
3278 | 2.83k | } |
3279 | | |
3280 | | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString( |
3281 | | Sema &S, const CodeCompletionContext &CCContext, |
3282 | | CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, |
3283 | 201k | bool IncludeBriefComments) { |
3284 | 201k | return CreateCodeCompletionString(S.Context, S.PP, CCContext, Allocator, |
3285 | 201k | CCTUInfo, IncludeBriefComments); |
3286 | 201k | } |
3287 | | |
3288 | | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionStringForMacro( |
3289 | | Preprocessor &PP, CodeCompletionAllocator &Allocator, |
3290 | 130k | CodeCompletionTUInfo &CCTUInfo) { |
3291 | 130k | assert(Kind == RK_Macro); |
3292 | 0 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
3293 | 130k | const MacroInfo *MI = PP.getMacroInfo(Macro); |
3294 | 130k | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Macro->getName())); |
3295 | | |
3296 | 130k | if (!MI || !MI->isFunctionLike()130k ) |
3297 | 129k | return Result.TakeString(); |
3298 | | |
3299 | | // Format a function-like macro with placeholders for the arguments. |
3300 | 185 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
3301 | 185 | MacroInfo::param_iterator A = MI->param_begin(), AEnd = MI->param_end(); |
3302 | | |
3303 | | // C99 variadic macros add __VA_ARGS__ at the end. Skip it. |
3304 | 185 | if (MI->isC99Varargs()) { |
3305 | 20 | --AEnd; |
3306 | | |
3307 | 20 | if (A == AEnd) { |
3308 | 8 | Result.AddPlaceholderChunk("..."); |
3309 | 8 | } |
3310 | 20 | } |
3311 | | |
3312 | 377 | for (MacroInfo::param_iterator A = MI->param_begin(); A != AEnd; ++A192 ) { |
3313 | 216 | if (A != MI->param_begin()) |
3314 | 39 | Result.AddChunk(CodeCompletionString::CK_Comma); |
3315 | | |
3316 | 216 | if (MI->isVariadic() && (A + 1) == AEnd48 ) { |
3317 | 24 | SmallString<32> Arg = (*A)->getName(); |
3318 | 24 | if (MI->isC99Varargs()) |
3319 | 12 | Arg += ", ..."; |
3320 | 12 | else |
3321 | 12 | Arg += "..."; |
3322 | 24 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
3323 | 24 | break; |
3324 | 24 | } |
3325 | | |
3326 | | // Non-variadic macros are simple. |
3327 | 192 | Result.AddPlaceholderChunk( |
3328 | 192 | Result.getAllocator().CopyString((*A)->getName())); |
3329 | 192 | } |
3330 | 185 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
3331 | 185 | return Result.TakeString(); |
3332 | 130k | } |
3333 | | |
3334 | | /// If possible, create a new code completion string for the given |
3335 | | /// result. |
3336 | | /// |
3337 | | /// \returns Either a new, heap-allocated code completion string describing |
3338 | | /// how to use this result, or NULL to indicate that the string or name of the |
3339 | | /// result is all that is needed. |
3340 | | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString( |
3341 | | ASTContext &Ctx, Preprocessor &PP, const CodeCompletionContext &CCContext, |
3342 | | CodeCompletionAllocator &Allocator, CodeCompletionTUInfo &CCTUInfo, |
3343 | 201k | bool IncludeBriefComments) { |
3344 | 201k | if (Kind == RK_Macro) |
3345 | 130k | return CreateCodeCompletionStringForMacro(PP, Allocator, CCTUInfo); |
3346 | | |
3347 | 71.3k | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); |
3348 | | |
3349 | 71.3k | PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP); |
3350 | 71.3k | if (Kind == RK_Pattern) { |
3351 | 58.7k | Pattern->Priority = Priority; |
3352 | 58.7k | Pattern->Availability = Availability; |
3353 | | |
3354 | 58.7k | if (Declaration) { |
3355 | 202 | Result.addParentContext(Declaration->getDeclContext()); |
3356 | 202 | Pattern->ParentName = Result.getParentName(); |
3357 | 202 | if (const RawComment *RC = |
3358 | 202 | getPatternCompletionComment(Ctx, Declaration)) { |
3359 | 4 | Result.addBriefComment(RC->getBriefText(Ctx)); |
3360 | 4 | Pattern->BriefComment = Result.getBriefComment(); |
3361 | 4 | } |
3362 | 202 | } |
3363 | | |
3364 | 58.7k | return Pattern; |
3365 | 58.7k | } |
3366 | | |
3367 | 12.6k | if (Kind == RK_Keyword) { |
3368 | 7.20k | Result.AddTypedTextChunk(Keyword); |
3369 | 7.20k | return Result.TakeString(); |
3370 | 7.20k | } |
3371 | 5.43k | assert(Kind == RK_Declaration && "Missed a result kind?"); |
3372 | 0 | return createCodeCompletionStringForDecl( |
3373 | 5.43k | PP, Ctx, Result, IncludeBriefComments, CCContext, Policy); |
3374 | 12.6k | } |
3375 | | |
3376 | | static void printOverrideString(const CodeCompletionString &CCS, |
3377 | | std::string &BeforeName, |
3378 | 6 | std::string &NameAndSignature) { |
3379 | 6 | bool SeenTypedChunk = false; |
3380 | 32 | for (auto &Chunk : CCS) { |
3381 | 32 | if (Chunk.Kind == CodeCompletionString::CK_Optional) { |
3382 | 2 | assert(SeenTypedChunk && "optional parameter before name"); |
3383 | | // Note that we put all chunks inside into NameAndSignature. |
3384 | 0 | printOverrideString(*Chunk.Optional, NameAndSignature, NameAndSignature); |
3385 | 2 | continue; |
3386 | 2 | } |
3387 | 30 | SeenTypedChunk |= Chunk.Kind == CodeCompletionString::CK_TypedText; |
3388 | 30 | if (SeenTypedChunk) |
3389 | 22 | NameAndSignature += Chunk.Text; |
3390 | 8 | else |
3391 | 8 | BeforeName += Chunk.Text; |
3392 | 30 | } |
3393 | 6 | } |
3394 | | |
3395 | | CodeCompletionString * |
3396 | | CodeCompletionResult::createCodeCompletionStringForOverride( |
3397 | | Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result, |
3398 | | bool IncludeBriefComments, const CodeCompletionContext &CCContext, |
3399 | 4 | PrintingPolicy &Policy) { |
3400 | 4 | auto *CCS = createCodeCompletionStringForDecl(PP, Ctx, Result, |
3401 | 4 | /*IncludeBriefComments=*/false, |
3402 | 4 | CCContext, Policy); |
3403 | 4 | std::string BeforeName; |
3404 | 4 | std::string NameAndSignature; |
3405 | | // For overrides all chunks go into the result, none are informative. |
3406 | 4 | printOverrideString(*CCS, BeforeName, NameAndSignature); |
3407 | 4 | NameAndSignature += " override"; |
3408 | | |
3409 | 4 | Result.AddTextChunk(Result.getAllocator().CopyString(BeforeName)); |
3410 | 4 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
3411 | 4 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(NameAndSignature)); |
3412 | 4 | return Result.TakeString(); |
3413 | 4 | } |
3414 | | |
3415 | | // FIXME: Right now this works well with lambdas. Add support for other functor |
3416 | | // types like std::function. |
3417 | 3.10k | static const NamedDecl *extractFunctorCallOperator(const NamedDecl *ND) { |
3418 | 3.10k | const auto *VD = dyn_cast<VarDecl>(ND); |
3419 | 3.10k | if (!VD) |
3420 | 2.50k | return nullptr; |
3421 | 593 | const auto *RecordDecl = VD->getType()->getAsCXXRecordDecl(); |
3422 | 593 | if (!RecordDecl || !RecordDecl->isLambda()79 ) |
3423 | 590 | return nullptr; |
3424 | 3 | return RecordDecl->getLambdaCallOperator(); |
3425 | 593 | } |
3426 | | |
3427 | | CodeCompletionString *CodeCompletionResult::createCodeCompletionStringForDecl( |
3428 | | Preprocessor &PP, ASTContext &Ctx, CodeCompletionBuilder &Result, |
3429 | | bool IncludeBriefComments, const CodeCompletionContext &CCContext, |
3430 | 5.43k | PrintingPolicy &Policy) { |
3431 | 5.43k | const NamedDecl *ND = Declaration; |
3432 | 5.43k | Result.addParentContext(ND->getDeclContext()); |
3433 | | |
3434 | 5.43k | if (IncludeBriefComments) { |
3435 | | // Add documentation comment, if it exists. |
3436 | 283 | if (const RawComment *RC = getCompletionComment(Ctx, Declaration)) { |
3437 | 114 | Result.addBriefComment(RC->getBriefText(Ctx)); |
3438 | 114 | } |
3439 | 283 | } |
3440 | | |
3441 | 5.43k | if (StartsNestedNameSpecifier) { |
3442 | 189 | Result.AddTypedTextChunk( |
3443 | 189 | Result.getAllocator().CopyString(ND->getNameAsString())); |
3444 | 189 | Result.AddTextChunk("::"); |
3445 | 189 | return Result.TakeString(); |
3446 | 189 | } |
3447 | | |
3448 | 5.24k | for (const auto *I : ND->specific_attrs<AnnotateAttr>()) |
3449 | 6 | Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); |
3450 | | |
3451 | 5.24k | auto AddFunctionTypeAndResult = [&](const FunctionDecl *Function) { |
3452 | 2.14k | AddResultTypeChunk(Ctx, Policy, Function, CCContext.getBaseType(), Result); |
3453 | 2.14k | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
3454 | 2.14k | Ctx, Policy); |
3455 | 2.14k | AddTypedNameChunk(Ctx, Policy, ND, Result); |
3456 | 2.14k | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
3457 | 2.14k | AddFunctionParameterChunks(PP, Policy, Function, Result); |
3458 | 2.14k | Result.AddChunk(CodeCompletionString::CK_RightParen); |
3459 | 2.14k | AddFunctionTypeQualsToCompletionString(Result, Function); |
3460 | 2.14k | }; |
3461 | | |
3462 | 5.24k | if (const auto *Function = dyn_cast<FunctionDecl>(ND)) { |
3463 | 2.14k | AddFunctionTypeAndResult(Function); |
3464 | 2.14k | return Result.TakeString(); |
3465 | 2.14k | } |
3466 | | |
3467 | 3.10k | if (const auto *CallOperator = |
3468 | 3.10k | dyn_cast_or_null<FunctionDecl>(extractFunctorCallOperator(ND))) { |
3469 | 3 | AddFunctionTypeAndResult(CallOperator); |
3470 | 3 | return Result.TakeString(); |
3471 | 3 | } |
3472 | | |
3473 | 3.09k | AddResultTypeChunk(Ctx, Policy, ND, CCContext.getBaseType(), Result); |
3474 | | |
3475 | 3.09k | if (const FunctionTemplateDecl *FunTmpl = |
3476 | 3.09k | dyn_cast<FunctionTemplateDecl>(ND)) { |
3477 | 650 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
3478 | 650 | Ctx, Policy); |
3479 | 650 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
3480 | 650 | AddTypedNameChunk(Ctx, Policy, Function, Result); |
3481 | | |
3482 | | // Figure out which template parameters are deduced (or have default |
3483 | | // arguments). |
3484 | 650 | llvm::SmallBitVector Deduced; |
3485 | 650 | Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced); |
3486 | 650 | unsigned LastDeducibleArgument; |
3487 | 1.29k | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; |
3488 | 721 | --LastDeducibleArgument647 ) { |
3489 | 721 | if (!Deduced[LastDeducibleArgument - 1]) { |
3490 | | // C++0x: Figure out if the template argument has a default. If so, |
3491 | | // the user doesn't need to type this argument. |
3492 | | // FIXME: We need to abstract template parameters better! |
3493 | 74 | bool HasDefaultArg = false; |
3494 | 74 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( |
3495 | 74 | LastDeducibleArgument - 1); |
3496 | 74 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
3497 | 74 | HasDefaultArg = TTP->hasDefaultArgument(); |
3498 | 0 | else if (NonTypeTemplateParmDecl *NTTP = |
3499 | 0 | dyn_cast<NonTypeTemplateParmDecl>(Param)) |
3500 | 0 | HasDefaultArg = NTTP->hasDefaultArgument(); |
3501 | 0 | else { |
3502 | 0 | assert(isa<TemplateTemplateParmDecl>(Param)); |
3503 | 0 | HasDefaultArg = |
3504 | 0 | cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); |
3505 | 0 | } |
3506 | | |
3507 | 74 | if (!HasDefaultArg) |
3508 | 74 | break; |
3509 | 74 | } |
3510 | 721 | } |
3511 | | |
3512 | 650 | if (LastDeducibleArgument) { |
3513 | | // Some of the function template arguments cannot be deduced from a |
3514 | | // function call, so we introduce an explicit template argument list |
3515 | | // containing all of the arguments up to the first deducible argument. |
3516 | 74 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
3517 | 74 | AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result, |
3518 | 74 | LastDeducibleArgument); |
3519 | 74 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
3520 | 74 | } |
3521 | | |
3522 | | // Add the function parameters |
3523 | 650 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
3524 | 650 | AddFunctionParameterChunks(PP, Policy, Function, Result); |
3525 | 650 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
3526 | 650 | AddFunctionTypeQualsToCompletionString(Result, Function); |
3527 | 650 | return Result.TakeString(); |
3528 | 650 | } |
3529 | | |
3530 | 2.44k | if (const auto *Template = dyn_cast<TemplateDecl>(ND)) { |
3531 | 149 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
3532 | 149 | Ctx, Policy); |
3533 | 149 | Result.AddTypedTextChunk( |
3534 | 149 | Result.getAllocator().CopyString(Template->getNameAsString())); |
3535 | 149 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); |
3536 | 149 | AddTemplateParameterChunks(Ctx, Policy, Template, Result); |
3537 | 149 | Result.AddChunk(CodeCompletionString::CK_RightAngle); |
3538 | 149 | return Result.TakeString(); |
3539 | 149 | } |
3540 | | |
3541 | 2.29k | if (const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) { |
3542 | 335 | Selector Sel = Method->getSelector(); |
3543 | 335 | if (Sel.isUnarySelector()) { |
3544 | 159 | Result.AddTypedTextChunk( |
3545 | 159 | Result.getAllocator().CopyString(Sel.getNameForSlot(0))); |
3546 | 159 | return Result.TakeString(); |
3547 | 159 | } |
3548 | | |
3549 | 176 | std::string SelName = Sel.getNameForSlot(0).str(); |
3550 | 176 | SelName += ':'; |
3551 | 176 | if (StartParameter == 0) |
3552 | 139 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); |
3553 | 37 | else { |
3554 | 37 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); |
3555 | | |
3556 | | // If there is only one parameter, and we're past it, add an empty |
3557 | | // typed-text chunk since there is nothing to type. |
3558 | 37 | if (Method->param_size() == 1) |
3559 | 1 | Result.AddTypedTextChunk(""); |
3560 | 37 | } |
3561 | 176 | unsigned Idx = 0; |
3562 | | // The extra Idx < Sel.getNumArgs() check is needed due to legacy C-style |
3563 | | // method parameters. |
3564 | 176 | for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(), |
3565 | 176 | PEnd = Method->param_end(); |
3566 | 505 | P != PEnd && Idx < Sel.getNumArgs()329 ; (void)++P, ++Idx329 ) { |
3567 | 329 | if (Idx > 0) { |
3568 | 153 | std::string Keyword; |
3569 | 153 | if (Idx > StartParameter) |
3570 | 105 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
3571 | 153 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) |
3572 | 153 | Keyword += II->getName(); |
3573 | 153 | Keyword += ":"; |
3574 | 153 | if (Idx < StartParameter || AllParametersAreInformative140 ) |
3575 | 13 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); |
3576 | 140 | else |
3577 | 140 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); |
3578 | 153 | } |
3579 | | |
3580 | | // If we're before the starting parameter, skip the placeholder. |
3581 | 329 | if (Idx < StartParameter) |
3582 | 50 | continue; |
3583 | | |
3584 | 279 | std::string Arg; |
3585 | 279 | QualType ParamType = (*P)->getType(); |
3586 | 279 | Optional<ArrayRef<QualType>> ObjCSubsts; |
3587 | 279 | if (!CCContext.getBaseType().isNull()) |
3588 | 221 | ObjCSubsts = CCContext.getBaseType()->getObjCSubstitutions(Method); |
3589 | | |
3590 | 279 | if (ParamType->isBlockPointerType() && !DeclaringEntity16 ) |
3591 | 16 | Arg = FormatFunctionParameter(Policy, *P, true, |
3592 | 16 | /*SuppressBlock=*/false, ObjCSubsts); |
3593 | 263 | else { |
3594 | 263 | if (ObjCSubsts) |
3595 | 4 | ParamType = ParamType.substObjCTypeArgs( |
3596 | 4 | Ctx, *ObjCSubsts, ObjCSubstitutionContext::Parameter); |
3597 | 263 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier(), |
3598 | 263 | ParamType); |
3599 | 263 | Arg += ParamType.getAsString(Policy) + ")"; |
3600 | 263 | if (IdentifierInfo *II = (*P)->getIdentifier()) |
3601 | 263 | if (DeclaringEntity || AllParametersAreInformative246 ) |
3602 | 24 | Arg += II->getName(); |
3603 | 263 | } |
3604 | | |
3605 | 279 | if (Method->isVariadic() && (P + 1) == PEnd4 ) |
3606 | 4 | Arg += ", ..."; |
3607 | | |
3608 | 279 | if (DeclaringEntity) |
3609 | 17 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); |
3610 | 262 | else if (AllParametersAreInformative) |
3611 | 7 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); |
3612 | 255 | else |
3613 | 255 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); |
3614 | 279 | } |
3615 | | |
3616 | 176 | if (Method->isVariadic()) { |
3617 | 4 | if (Method->param_size() == 0) { |
3618 | 0 | if (DeclaringEntity) |
3619 | 0 | Result.AddTextChunk(", ..."); |
3620 | 0 | else if (AllParametersAreInformative) |
3621 | 0 | Result.AddInformativeChunk(", ..."); |
3622 | 0 | else |
3623 | 0 | Result.AddPlaceholderChunk(", ..."); |
3624 | 0 | } |
3625 | | |
3626 | 4 | MaybeAddSentinel(PP, Method, Result); |
3627 | 4 | } |
3628 | | |
3629 | 176 | return Result.TakeString(); |
3630 | 335 | } |
3631 | | |
3632 | 1.96k | if (Qualifier) |
3633 | 68 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, |
3634 | 68 | Ctx, Policy); |
3635 | | |
3636 | 1.96k | Result.AddTypedTextChunk( |
3637 | 1.96k | Result.getAllocator().CopyString(ND->getNameAsString())); |
3638 | 1.96k | return Result.TakeString(); |
3639 | 2.29k | } |
3640 | | |
3641 | | const RawComment *clang::getCompletionComment(const ASTContext &Ctx, |
3642 | 283 | const NamedDecl *ND) { |
3643 | 283 | if (!ND) |
3644 | 0 | return nullptr; |
3645 | 283 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(ND)) |
3646 | 65 | return RC; |
3647 | | |
3648 | | // Try to find comment from a property for ObjC methods. |
3649 | 218 | const auto *M = dyn_cast<ObjCMethodDecl>(ND); |
3650 | 218 | if (!M) |
3651 | 159 | return nullptr; |
3652 | 59 | const ObjCPropertyDecl *PDecl = M->findPropertyDecl(); |
3653 | 59 | if (!PDecl) |
3654 | 9 | return nullptr; |
3655 | | |
3656 | 50 | return Ctx.getRawCommentForAnyRedecl(PDecl); |
3657 | 59 | } |
3658 | | |
3659 | | const RawComment *clang::getPatternCompletionComment(const ASTContext &Ctx, |
3660 | 202 | const NamedDecl *ND) { |
3661 | 202 | const auto *M = dyn_cast_or_null<ObjCMethodDecl>(ND); |
3662 | 202 | if (!M || !M->isPropertyAccessor()113 ) |
3663 | 171 | return nullptr; |
3664 | | |
3665 | | // Provide code completion comment for self.GetterName where |
3666 | | // GetterName is the getter method for a property with name |
3667 | | // different from the property name (declared via a property |
3668 | | // getter attribute. |
3669 | 31 | const ObjCPropertyDecl *PDecl = M->findPropertyDecl(); |
3670 | 31 | if (!PDecl) |
3671 | 0 | return nullptr; |
3672 | 31 | if (PDecl->getGetterName() == M->getSelector() && |
3673 | 31 | PDecl->getIdentifier() != M->getIdentifier()18 ) { |
3674 | 18 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(M)) |
3675 | 1 | return RC; |
3676 | 17 | if (auto *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) |
3677 | 3 | return RC; |
3678 | 17 | } |
3679 | 27 | return nullptr; |
3680 | 31 | } |
3681 | | |
3682 | | const RawComment *clang::getParameterComment( |
3683 | | const ASTContext &Ctx, |
3684 | 0 | const CodeCompleteConsumer::OverloadCandidate &Result, unsigned ArgIndex) { |
3685 | 0 | auto FDecl = Result.getFunction(); |
3686 | 0 | if (!FDecl) |
3687 | 0 | return nullptr; |
3688 | 0 | if (ArgIndex < FDecl->getNumParams()) |
3689 | 0 | return Ctx.getRawCommentForAnyRedecl(FDecl->getParamDecl(ArgIndex)); |
3690 | 0 | return nullptr; |
3691 | 0 | } |
3692 | | |
3693 | | /// Add function overload parameter chunks to the given code completion |
3694 | | /// string. |
3695 | | static void AddOverloadParameterChunks(ASTContext &Context, |
3696 | | const PrintingPolicy &Policy, |
3697 | | const FunctionDecl *Function, |
3698 | | const FunctionProtoType *Prototype, |
3699 | | CodeCompletionBuilder &Result, |
3700 | | unsigned CurrentArg, unsigned Start = 0, |
3701 | 261 | bool InOptional = false) { |
3702 | 261 | bool FirstParameter = true; |
3703 | 261 | unsigned NumParams = |
3704 | 261 | Function ? Function->getNumParams()256 : Prototype->getNumParams()5 ; |
3705 | | |
3706 | 670 | for (unsigned P = Start; P != NumParams; ++P409 ) { |
3707 | 428 | if (Function && Function->getParamDecl(P)->hasDefaultArg()422 && !InOptional38 ) { |
3708 | | // When we see an optional default argument, put that argument and |
3709 | | // the remaining default arguments into a new, optional string. |
3710 | 19 | CodeCompletionBuilder Opt(Result.getAllocator(), |
3711 | 19 | Result.getCodeCompletionTUInfo()); |
3712 | 19 | if (!FirstParameter) |
3713 | 15 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
3714 | | // Optional sections are nested. |
3715 | 19 | AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, |
3716 | 19 | CurrentArg, P, /*InOptional=*/true); |
3717 | 19 | Result.AddOptionalChunk(Opt.TakeString()); |
3718 | 19 | return; |
3719 | 19 | } |
3720 | | |
3721 | 409 | if (FirstParameter) |
3722 | 238 | FirstParameter = false; |
3723 | 171 | else |
3724 | 171 | Result.AddChunk(CodeCompletionString::CK_Comma); |
3725 | | |
3726 | 409 | InOptional = false; |
3727 | | |
3728 | | // Format the placeholder string. |
3729 | 409 | std::string Placeholder; |
3730 | 409 | if (Function) { |
3731 | 403 | const ParmVarDecl *Param = Function->getParamDecl(P); |
3732 | 403 | Placeholder = FormatFunctionParameter(Policy, Param); |
3733 | 403 | if (Param->hasDefaultArg()) |
3734 | 19 | Placeholder += GetDefaultValueString(Param, Context.getSourceManager(), |
3735 | 19 | Context.getLangOpts()); |
3736 | 403 | } else { |
3737 | 6 | Placeholder = Prototype->getParamType(P).getAsString(Policy); |
3738 | 6 | } |
3739 | | |
3740 | 409 | if (P == CurrentArg) |
3741 | 218 | Result.AddCurrentParameterChunk( |
3742 | 218 | Result.getAllocator().CopyString(Placeholder)); |
3743 | 191 | else |
3744 | 191 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder)); |
3745 | 409 | } |
3746 | | |
3747 | 242 | if (Prototype && Prototype->isVariadic()241 ) { |
3748 | 9 | CodeCompletionBuilder Opt(Result.getAllocator(), |
3749 | 9 | Result.getCodeCompletionTUInfo()); |
3750 | 9 | if (!FirstParameter) |
3751 | 9 | Opt.AddChunk(CodeCompletionString::CK_Comma); |
3752 | | |
3753 | 9 | if (CurrentArg < NumParams) |
3754 | 4 | Opt.AddPlaceholderChunk("..."); |
3755 | 5 | else |
3756 | 5 | Opt.AddCurrentParameterChunk("..."); |
3757 | | |
3758 | 9 | Result.AddOptionalChunk(Opt.TakeString()); |
3759 | 9 | } |
3760 | 242 | } |
3761 | | |
3762 | | CodeCompletionString * |
3763 | | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( |
3764 | | unsigned CurrentArg, Sema &S, CodeCompletionAllocator &Allocator, |
3765 | 242 | CodeCompletionTUInfo &CCTUInfo, bool IncludeBriefComments) const { |
3766 | 242 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
3767 | | // Show signatures of constructors as they are declared: |
3768 | | // vector(int n) rather than vector<string>(int n) |
3769 | | // This is less noisy without being less clear, and avoids tricky cases. |
3770 | 242 | Policy.SuppressTemplateArgsInCXXConstructors = true; |
3771 | | |
3772 | | // FIXME: Set priority, availability appropriately. |
3773 | 242 | CodeCompletionBuilder Result(Allocator, CCTUInfo, 1, |
3774 | 242 | CXAvailability_Available); |
3775 | 242 | FunctionDecl *FDecl = getFunction(); |
3776 | 242 | const FunctionProtoType *Proto = |
3777 | 242 | dyn_cast<FunctionProtoType>(getFunctionType()); |
3778 | 242 | if (!FDecl && !Proto5 ) { |
3779 | | // Function without a prototype. Just give the return type and a |
3780 | | // highlighted ellipsis. |
3781 | 0 | const FunctionType *FT = getFunctionType(); |
3782 | 0 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
3783 | 0 | FT->getReturnType().getAsString(Policy))); |
3784 | 0 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
3785 | 0 | Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); |
3786 | 0 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
3787 | 0 | return Result.TakeString(); |
3788 | 0 | } |
3789 | | |
3790 | 242 | if (FDecl) { |
3791 | 237 | if (IncludeBriefComments) { |
3792 | 0 | if (auto RC = getParameterComment(S.getASTContext(), *this, CurrentArg)) |
3793 | 0 | Result.addBriefComment(RC->getBriefText(S.getASTContext())); |
3794 | 0 | } |
3795 | 237 | AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result); |
3796 | | |
3797 | 237 | std::string Name; |
3798 | 237 | llvm::raw_string_ostream OS(Name); |
3799 | 237 | FDecl->getDeclName().print(OS, Policy); |
3800 | 237 | Result.AddTextChunk(Result.getAllocator().CopyString(OS.str())); |
3801 | 237 | } else { |
3802 | 5 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( |
3803 | 5 | Proto->getReturnType().getAsString(Policy))); |
3804 | 5 | } |
3805 | | |
3806 | 242 | Result.AddChunk(CodeCompletionString::CK_LeftParen); |
3807 | 242 | AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, |
3808 | 242 | CurrentArg); |
3809 | 242 | Result.AddChunk(CodeCompletionString::CK_RightParen); |
3810 | | |
3811 | 242 | return Result.TakeString(); |
3812 | 242 | } |
3813 | | |
3814 | | unsigned clang::getMacroUsagePriority(StringRef MacroName, |
3815 | | const LangOptions &LangOpts, |
3816 | 144k | bool PreferredTypeIsPointer) { |
3817 | 144k | unsigned Priority = CCP_Macro; |
3818 | | |
3819 | | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. |
3820 | 144k | if (MacroName.equals("nil") || MacroName.equals("NULL")144k || |
3821 | 144k | MacroName.equals("Nil")144k ) { |
3822 | 39 | Priority = CCP_Constant; |
3823 | 39 | if (PreferredTypeIsPointer) |
3824 | 7 | Priority = Priority / CCF_SimilarTypeMatch; |
3825 | 39 | } |
3826 | | // Treat "YES", "NO", "true", and "false" as constants. |
3827 | 144k | else if (MacroName.equals("YES") || MacroName.equals("NO")144k || |
3828 | 144k | MacroName.equals("true")144k || MacroName.equals("false")144k ) |
3829 | 16 | Priority = CCP_Constant; |
3830 | | // Treat "bool" as a type. |
3831 | 144k | else if (MacroName.equals("bool")) |
3832 | 8 | Priority = CCP_Type + (LangOpts.ObjC ? CCD_bool_in_ObjC : 00 ); |
3833 | | |
3834 | 144k | return Priority; |
3835 | 144k | } |
3836 | | |
3837 | 57.6k | CXCursorKind clang::getCursorKindForDecl(const Decl *D) { |
3838 | 57.6k | if (!D) |
3839 | 0 | return CXCursor_UnexposedDecl; |
3840 | | |
3841 | 57.6k | switch (D->getKind()) { |
3842 | 315 | case Decl::Enum: |
3843 | 315 | return CXCursor_EnumDecl; |
3844 | 688 | case Decl::EnumConstant: |
3845 | 688 | return CXCursor_EnumConstantDecl; |
3846 | 1.49k | case Decl::Field: |
3847 | 1.49k | return CXCursor_FieldDecl; |
3848 | 8.53k | case Decl::Function: |
3849 | 8.53k | return CXCursor_FunctionDecl; |
3850 | 171 | case Decl::ObjCCategory: |
3851 | 171 | return CXCursor_ObjCCategoryDecl; |
3852 | 10 | case Decl::ObjCCategoryImpl: |
3853 | 10 | return CXCursor_ObjCCategoryImplDecl; |
3854 | 144 | case Decl::ObjCImplementation: |
3855 | 144 | return CXCursor_ObjCImplementationDecl; |
3856 | | |
3857 | 1.51k | case Decl::ObjCInterface: |
3858 | 1.51k | return CXCursor_ObjCInterfaceDecl; |
3859 | 283 | case Decl::ObjCIvar: |
3860 | 283 | return CXCursor_ObjCIvarDecl; |
3861 | 1.55k | case Decl::ObjCMethod: |
3862 | 1.55k | return cast<ObjCMethodDecl>(D)->isInstanceMethod() |
3863 | 1.55k | ? CXCursor_ObjCInstanceMethodDecl1.36k |
3864 | 1.55k | : CXCursor_ObjCClassMethodDecl189 ; |
3865 | 16.8k | case Decl::CXXMethod: |
3866 | 16.8k | return CXCursor_CXXMethod; |
3867 | 1.54k | case Decl::CXXConstructor: |
3868 | 1.54k | return CXCursor_Constructor; |
3869 | 382 | case Decl::CXXDestructor: |
3870 | 382 | return CXCursor_Destructor; |
3871 | 9 | case Decl::CXXConversion: |
3872 | 9 | return CXCursor_ConversionFunction; |
3873 | 276 | case Decl::ObjCProperty: |
3874 | 276 | return CXCursor_ObjCPropertyDecl; |
3875 | 226 | case Decl::ObjCProtocol: |
3876 | 226 | return CXCursor_ObjCProtocolDecl; |
3877 | 3.20k | case Decl::ParmVar: |
3878 | 3.20k | return CXCursor_ParmDecl; |
3879 | 9.57k | case Decl::Typedef: |
3880 | 9.57k | return CXCursor_TypedefDecl; |
3881 | 64 | case Decl::TypeAlias: |
3882 | 64 | return CXCursor_TypeAliasDecl; |
3883 | 16 | case Decl::TypeAliasTemplate: |
3884 | 16 | return CXCursor_TypeAliasTemplateDecl; |
3885 | 2.29k | case Decl::Var: |
3886 | 2.29k | return CXCursor_VarDecl; |
3887 | 628 | case Decl::Namespace: |
3888 | 628 | return CXCursor_Namespace; |
3889 | 27 | case Decl::NamespaceAlias: |
3890 | 27 | return CXCursor_NamespaceAlias; |
3891 | 539 | case Decl::TemplateTypeParm: |
3892 | 539 | return CXCursor_TemplateTypeParameter; |
3893 | 45 | case Decl::NonTypeTemplateParm: |
3894 | 45 | return CXCursor_NonTypeTemplateParameter; |
3895 | 26 | case Decl::TemplateTemplateParm: |
3896 | 26 | return CXCursor_TemplateTemplateParameter; |
3897 | 1.44k | case Decl::FunctionTemplate: |
3898 | 1.44k | return CXCursor_FunctionTemplate; |
3899 | 789 | case Decl::ClassTemplate: |
3900 | 789 | return CXCursor_ClassTemplate; |
3901 | 69 | case Decl::AccessSpec: |
3902 | 69 | return CXCursor_CXXAccessSpecifier; |
3903 | 22 | case Decl::ClassTemplatePartialSpecialization: |
3904 | 22 | return CXCursor_ClassTemplatePartialSpecialization; |
3905 | 7 | case Decl::UsingDirective: |
3906 | 7 | return CXCursor_UsingDirective; |
3907 | 1 | case Decl::StaticAssert: |
3908 | 1 | return CXCursor_StaticAssert; |
3909 | 41 | case Decl::Friend: |
3910 | 41 | return CXCursor_FriendDecl; |
3911 | 408 | case Decl::TranslationUnit: |
3912 | 408 | return CXCursor_TranslationUnit; |
3913 | | |
3914 | 10 | case Decl::Using: |
3915 | 14 | case Decl::UnresolvedUsingValue: |
3916 | 17 | case Decl::UnresolvedUsingTypename: |
3917 | 17 | return CXCursor_UsingDeclaration; |
3918 | | |
3919 | 47 | case Decl::ObjCPropertyImpl: |
3920 | 47 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { |
3921 | 11 | case ObjCPropertyImplDecl::Dynamic: |
3922 | 11 | return CXCursor_ObjCDynamicDecl; |
3923 | | |
3924 | 36 | case ObjCPropertyImplDecl::Synthesize: |
3925 | 36 | return CXCursor_ObjCSynthesizeDecl; |
3926 | 47 | } |
3927 | 47 | llvm_unreachable0 ("Unexpected Kind!"); |
3928 | | |
3929 | 29 | case Decl::Import: |
3930 | 29 | return CXCursor_ModuleImportDecl; |
3931 | | |
3932 | 18 | case Decl::ObjCTypeParam: |
3933 | 18 | return CXCursor_TemplateTypeParameter; |
3934 | | |
3935 | 4.32k | default: |
3936 | 4.32k | if (const auto *TD = dyn_cast<TagDecl>(D)) { |
3937 | 4.00k | switch (TD->getTagKind()) { |
3938 | 0 | case TTK_Interface: // fall through |
3939 | 2.56k | case TTK_Struct: |
3940 | 2.56k | return CXCursor_StructDecl; |
3941 | 1.31k | case TTK_Class: |
3942 | 1.31k | return CXCursor_ClassDecl; |
3943 | 126 | case TTK_Union: |
3944 | 126 | return CXCursor_UnionDecl; |
3945 | 0 | case TTK_Enum: |
3946 | 0 | return CXCursor_EnumDecl; |
3947 | 4.00k | } |
3948 | 4.00k | } |
3949 | 57.6k | } |
3950 | | |
3951 | 321 | return CXCursor_UnexposedDecl; |
3952 | 57.6k | } |
3953 | | |
3954 | | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, |
3955 | | bool LoadExternal, bool IncludeUndefined, |
3956 | 327 | bool TargetTypeIsPointer = false) { |
3957 | 327 | typedef CodeCompletionResult Result; |
3958 | | |
3959 | 327 | Results.EnterNewScope(); |
3960 | | |
3961 | 327 | for (Preprocessor::macro_iterator M = PP.macro_begin(LoadExternal), |
3962 | 327 | MEnd = PP.macro_end(LoadExternal); |
3963 | 131k | M != MEnd; ++M130k ) { |
3964 | 130k | auto MD = PP.getMacroDefinition(M->first); |
3965 | 130k | if (IncludeUndefined || MD89.8k ) { |
3966 | 130k | MacroInfo *MI = MD.getMacroInfo(); |
3967 | 130k | if (MI && MI->isUsedForHeaderGuard()130k ) |
3968 | 29 | continue; |
3969 | | |
3970 | 130k | Results.AddResult( |
3971 | 130k | Result(M->first, MI, |
3972 | 130k | getMacroUsagePriority(M->first->getName(), PP.getLangOpts(), |
3973 | 130k | TargetTypeIsPointer))); |
3974 | 130k | } |
3975 | 130k | } |
3976 | | |
3977 | 327 | Results.ExitScope(); |
3978 | 327 | } |
3979 | | |
3980 | | static void AddPrettyFunctionResults(const LangOptions &LangOpts, |
3981 | 516 | ResultBuilder &Results) { |
3982 | 516 | typedef CodeCompletionResult Result; |
3983 | | |
3984 | 516 | Results.EnterNewScope(); |
3985 | | |
3986 | 516 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); |
3987 | 516 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); |
3988 | 516 | if (LangOpts.C99 || LangOpts.CPlusPlus11385 ) |
3989 | 506 | Results.AddResult(Result("__func__", CCP_Constant)); |
3990 | 516 | Results.ExitScope(); |
3991 | 516 | } |
3992 | | |
3993 | | static void HandleCodeCompleteResults(Sema *S, |
3994 | | CodeCompleteConsumer *CodeCompleter, |
3995 | | CodeCompletionContext Context, |
3996 | | CodeCompletionResult *Results, |
3997 | 1.23k | unsigned NumResults) { |
3998 | 1.23k | if (CodeCompleter) |
3999 | 1.23k | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); |
4000 | 1.23k | } |
4001 | | |
4002 | | static CodeCompletionContext |
4003 | 222 | mapCodeCompletionContext(Sema &S, Sema::ParserCompletionContext PCC) { |
4004 | 222 | switch (PCC) { |
4005 | 51 | case Sema::PCC_Namespace: |
4006 | 51 | return CodeCompletionContext::CCC_TopLevel; |
4007 | | |
4008 | 9 | case Sema::PCC_Class: |
4009 | 9 | return CodeCompletionContext::CCC_ClassStructUnion; |
4010 | | |
4011 | 1 | case Sema::PCC_ObjCInterface: |
4012 | 1 | return CodeCompletionContext::CCC_ObjCInterface; |
4013 | | |
4014 | 2 | case Sema::PCC_ObjCImplementation: |
4015 | 2 | return CodeCompletionContext::CCC_ObjCImplementation; |
4016 | | |
4017 | 1 | case Sema::PCC_ObjCInstanceVariableList: |
4018 | 1 | return CodeCompletionContext::CCC_ObjCIvarList; |
4019 | | |
4020 | 0 | case Sema::PCC_Template: |
4021 | 0 | case Sema::PCC_MemberTemplate: |
4022 | 0 | if (S.CurContext->isFileContext()) |
4023 | 0 | return CodeCompletionContext::CCC_TopLevel; |
4024 | 0 | if (S.CurContext->isRecord()) |
4025 | 0 | return CodeCompletionContext::CCC_ClassStructUnion; |
4026 | 0 | return CodeCompletionContext::CCC_Other; |
4027 | | |
4028 | 3 | case Sema::PCC_RecoveryInFunction: |
4029 | 3 | return CodeCompletionContext::CCC_Recovery; |
4030 | | |
4031 | 6 | case Sema::PCC_ForInit: |
4032 | 6 | if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 || |
4033 | 6 | S.getLangOpts().ObjC0 ) |
4034 | 6 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
4035 | 0 | else |
4036 | 0 | return CodeCompletionContext::CCC_Expression; |
4037 | | |
4038 | 1 | case Sema::PCC_Expression: |
4039 | 1 | return CodeCompletionContext::CCC_Expression; |
4040 | 6 | case Sema::PCC_Condition: |
4041 | 6 | return CodeCompletionContext(CodeCompletionContext::CCC_Expression, |
4042 | 6 | S.getASTContext().BoolTy); |
4043 | | |
4044 | 139 | case Sema::PCC_Statement: |
4045 | 139 | return CodeCompletionContext::CCC_Statement; |
4046 | | |
4047 | 2 | case Sema::PCC_Type: |
4048 | 2 | return CodeCompletionContext::CCC_Type; |
4049 | | |
4050 | 0 | case Sema::PCC_ParenthesizedExpression: |
4051 | 0 | return CodeCompletionContext::CCC_ParenthesizedExpression; |
4052 | | |
4053 | 1 | case Sema::PCC_LocalDeclarationSpecifiers: |
4054 | 1 | return CodeCompletionContext::CCC_Type; |
4055 | 222 | } |
4056 | | |
4057 | 222 | llvm_unreachable0 ("Invalid ParserCompletionContext!"); |
4058 | 222 | } |
4059 | | |
4060 | | /// If we're in a C++ virtual member function, add completion results |
4061 | | /// that invoke the functions we override, since it's common to invoke the |
4062 | | /// overridden function as well as adding new functionality. |
4063 | | /// |
4064 | | /// \param S The semantic analysis object for which we are generating results. |
4065 | | /// |
4066 | | /// \param InContext This context in which the nested-name-specifier preceding |
4067 | | /// the code-completion point |
4068 | | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, |
4069 | 143 | ResultBuilder &Results) { |
4070 | | // Look through blocks. |
4071 | 143 | DeclContext *CurContext = S.CurContext; |
4072 | 143 | while (isa<BlockDecl>(CurContext)) |
4073 | 0 | CurContext = CurContext->getParent(); |
4074 | | |
4075 | 143 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); |
4076 | 143 | if (!Method || !Method->isVirtual()37 ) |
4077 | 140 | return; |
4078 | | |
4079 | | // We need to have names for all of the parameters, if we're going to |
4080 | | // generate a forwarding call. |
4081 | 3 | for (auto P : Method->parameters()) |
4082 | 5 | if (!P->getDeclName()) |
4083 | 0 | return; |
4084 | | |
4085 | 3 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); |
4086 | 3 | for (const CXXMethodDecl *Overridden : Method->overridden_methods()) { |
4087 | 3 | CodeCompletionBuilder Builder(Results.getAllocator(), |
4088 | 3 | Results.getCodeCompletionTUInfo()); |
4089 | 3 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) |
4090 | 0 | continue; |
4091 | | |
4092 | | // If we need a nested-name-specifier, add one now. |
4093 | 3 | if (!InContext) { |
4094 | 2 | NestedNameSpecifier *NNS = getRequiredQualification( |
4095 | 2 | S.Context, CurContext, Overridden->getDeclContext()); |
4096 | 2 | if (NNS) { |
4097 | 2 | std::string Str; |
4098 | 2 | llvm::raw_string_ostream OS(Str); |
4099 | 2 | NNS->print(OS, Policy); |
4100 | 2 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); |
4101 | 2 | } |
4102 | 2 | } else if (1 !InContext->Equals(Overridden->getDeclContext())1 ) |
4103 | 0 | continue; |
4104 | | |
4105 | 3 | Builder.AddTypedTextChunk( |
4106 | 3 | Results.getAllocator().CopyString(Overridden->getNameAsString())); |
4107 | 3 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
4108 | 3 | bool FirstParam = true; |
4109 | 5 | for (auto P : Method->parameters()) { |
4110 | 5 | if (FirstParam) |
4111 | 3 | FirstParam = false; |
4112 | 2 | else |
4113 | 2 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
4114 | | |
4115 | 5 | Builder.AddPlaceholderChunk( |
4116 | 5 | Results.getAllocator().CopyString(P->getIdentifier()->getName())); |
4117 | 5 | } |
4118 | 3 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
4119 | 3 | Results.AddResult(CodeCompletionResult( |
4120 | 3 | Builder.TakeString(), CCP_SuperCompletion, CXCursor_CXXMethod, |
4121 | 3 | CXAvailability_Available, Overridden)); |
4122 | 3 | Results.Ignore(Overridden); |
4123 | 3 | } |
4124 | 3 | } |
4125 | | |
4126 | | void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc, |
4127 | 3 | ModuleIdPath Path) { |
4128 | 3 | typedef CodeCompletionResult Result; |
4129 | 3 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
4130 | 3 | CodeCompleter->getCodeCompletionTUInfo(), |
4131 | 3 | CodeCompletionContext::CCC_Other); |
4132 | 3 | Results.EnterNewScope(); |
4133 | | |
4134 | 3 | CodeCompletionAllocator &Allocator = Results.getAllocator(); |
4135 | 3 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); |
4136 | 3 | typedef CodeCompletionResult Result; |
4137 | 3 | if (Path.empty()) { |
4138 | | // Enumerate all top-level modules. |
4139 | 2 | SmallVector<Module *, 8> Modules; |
4140 | 2 | PP.getHeaderSearchInfo().collectAllModules(Modules); |
4141 | 396 | for (unsigned I = 0, N = Modules.size(); I != N; ++I394 ) { |
4142 | 394 | Builder.AddTypedTextChunk( |
4143 | 394 | Builder.getAllocator().CopyString(Modules[I]->Name)); |
4144 | 394 | Results.AddResult(Result( |
4145 | 394 | Builder.TakeString(), CCP_Declaration, CXCursor_ModuleImportDecl, |
4146 | 394 | Modules[I]->isAvailable() ? CXAvailability_Available |
4147 | 394 | : CXAvailability_NotAvailable0 )); |
4148 | 394 | } |
4149 | 2 | } else if (1 getLangOpts().Modules1 ) { |
4150 | | // Load the named module. |
4151 | 1 | Module *Mod = |
4152 | 1 | PP.getModuleLoader().loadModule(ImportLoc, Path, Module::AllVisible, |
4153 | 1 | /*IsInclusionDirective=*/false); |
4154 | | // Enumerate submodules. |
4155 | 1 | if (Mod) { |
4156 | 1 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), |
4157 | 1 | SubEnd = Mod->submodule_end(); |
4158 | 2 | Sub != SubEnd; ++Sub1 ) { |
4159 | | |
4160 | 1 | Builder.AddTypedTextChunk( |
4161 | 1 | Builder.getAllocator().CopyString((*Sub)->Name)); |
4162 | 1 | Results.AddResult(Result( |
4163 | 1 | Builder.TakeString(), CCP_Declaration, CXCursor_ModuleImportDecl, |
4164 | 1 | (*Sub)->isAvailable() ? CXAvailability_Available |
4165 | 1 | : CXAvailability_NotAvailable0 )); |
4166 | 1 | } |
4167 | 1 | } |
4168 | 1 | } |
4169 | 3 | Results.ExitScope(); |
4170 | 3 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
4171 | 3 | Results.data(), Results.size()); |
4172 | 3 | } |
4173 | | |
4174 | | void Sema::CodeCompleteOrdinaryName(Scope *S, |
4175 | 218 | ParserCompletionContext CompletionContext) { |
4176 | 218 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), |
4177 | 218 | CodeCompleter->getCodeCompletionTUInfo(), |
4178 | 218 | mapCodeCompletionContext(*this, CompletionContext)); |
4179 | 218 | Results.EnterNewScope(); |
4180 | | |
4181 | | // Determine how to filter results, e.g., so that the names of |
4182 | | // values (functions, enumerators, function templates, etc.) are |
4183 | | // only allowed where we can have an expression. |
4184 | 218 | switch (CompletionContext) { |
4185 | 51 | case PCC_Namespace: |
4186 | 60 | case PCC_Class: |
4187 | 61 | case PCC_ObjCInterface: |
4188 | 63 | case PCC_ObjCImplementation: |
4189 | 64 | case PCC_ObjCInstanceVariableList: |
4190 | 64 | case PCC_Template: |
4191 | 64 | case PCC_MemberTemplate: |
4192 | 66 | case PCC_Type: |
4193 | 67 | case PCC_LocalDeclarationSpecifiers: |
4194 | 67 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); |
4195 | 67 | break; |
4196 | | |
4197 | 135 | case PCC_Statement: |
4198 | 135 | case PCC_ParenthesizedExpression: |
4199 | 136 | case PCC_Expression: |
4200 | 142 | case PCC_ForInit: |
4201 | 148 | case PCC_Condition: |
4202 | 148 | if (WantTypesInContext(CompletionContext, getLangOpts())) |
4203 | 147 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
4204 | 1 | else |
4205 | 1 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
4206 | | |
4207 | 148 | if (getLangOpts().CPlusPlus) |
4208 | 95 | MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results); |
4209 | 148 | break; |
4210 | | |
4211 | 3 | case PCC_RecoveryInFunction: |
4212 | | // Unfiltered |
4213 | 3 | break; |
4214 | 218 | } |
4215 | | |
4216 | | // If we are in a C++ non-static member function, check the qualifiers on |
4217 | | // the member function to filter/prioritize the results list. |
4218 | 218 | auto ThisType = getCurrentThisType(); |
4219 | 218 | if (!ThisType.isNull()) |
4220 | 27 | Results.setObjectTypeQualifiers(ThisType->getPointeeType().getQualifiers(), |
4221 | 27 | VK_LValue); |
4222 | | |
4223 | 218 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
4224 | 218 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
4225 | 218 | CodeCompleter->includeGlobals(), |
4226 | 218 | CodeCompleter->loadExternal()); |
4227 | | |
4228 | 218 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); |
4229 | 218 | Results.ExitScope(); |
4230 | | |
4231 | 218 | switch (CompletionContext) { |
4232 | 0 | case PCC_ParenthesizedExpression: |
4233 | 1 | case PCC_Expression: |
4234 | 136 | case PCC_Statement: |
4235 | 139 | case PCC_RecoveryInFunction: |
4236 | 139 | if (S->getFnParent()) |
4237 | 139 | AddPrettyFunctionResults(getLangOpts(), Results); |
4238 | 139 | break; |
4239 | | |
4240 | 51 | case PCC_Namespace: |
4241 | 60 | case PCC_Class: |
4242 | 61 | case PCC_ObjCInterface: |
4243 | 63 | case PCC_ObjCImplementation: |
4244 | 64 | case PCC_ObjCInstanceVariableList: |
4245 | 64 | case PCC_Template: |
4246 | 64 | case PCC_MemberTemplate: |
4247 | 70 | case PCC_ForInit: |
4248 | 76 | case PCC_Condition: |
4249 | 78 | case PCC_Type: |
4250 | 79 | case PCC_LocalDeclarationSpecifiers: |
4251 | 79 | break; |
4252 | 218 | } |
4253 | | |
4254 | 218 | if (CodeCompleter->includeMacros()) |
4255 | 63 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false); |
4256 | | |
4257 | 218 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
4258 | 218 | Results.data(), Results.size()); |
4259 | 218 | } |
4260 | | |
4261 | | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, |
4262 | | ParsedType Receiver, |
4263 | | ArrayRef<IdentifierInfo *> SelIdents, |
4264 | | bool AtArgumentExpression, bool IsSuper, |
4265 | | ResultBuilder &Results); |
4266 | | |
4267 | | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, |
4268 | | bool AllowNonIdentifiers, |
4269 | 35 | bool AllowNestedNameSpecifiers) { |
4270 | 35 | typedef CodeCompletionResult Result; |
4271 | 35 | ResultBuilder Results( |
4272 | 35 | *this, CodeCompleter->getAllocator(), |
4273 | 35 | CodeCompleter->getCodeCompletionTUInfo(), |
4274 | 35 | AllowNestedNameSpecifiers |
4275 | | // FIXME: Try to separate codepath leading here to deduce whether we |
4276 | | // need an existing symbol or a new one. |
4277 | 35 | ? CodeCompletionContext::CCC_SymbolOrNewName17 |
4278 | 35 | : CodeCompletionContext::CCC_NewName18 ); |
4279 | 35 | Results.EnterNewScope(); |
4280 | | |
4281 | | // Type qualifiers can come after names. |
4282 | 35 | Results.AddResult(Result("const")); |
4283 | 35 | Results.AddResult(Result("volatile")); |
4284 | 35 | if (getLangOpts().C99) |
4285 | 7 | Results.AddResult(Result("restrict")); |
4286 | | |
4287 | 35 | if (getLangOpts().CPlusPlus) { |
4288 | 28 | if (getLangOpts().CPlusPlus11 && |
4289 | 28 | (DS.getTypeSpecType() == DeclSpec::TST_class || |
4290 | 28 | DS.getTypeSpecType() == DeclSpec::TST_struct26 )) |
4291 | 3 | Results.AddResult("final"); |
4292 | | |
4293 | 28 | if (AllowNonIdentifiers) { |
4294 | 22 | Results.AddResult(Result("operator")); |
4295 | 22 | } |
4296 | | |
4297 | | // Add nested-name-specifiers. |
4298 | 28 | if (AllowNestedNameSpecifiers) { |
4299 | 15 | Results.allowNestedNameSpecifiers(); |
4300 | 15 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); |
4301 | 15 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
4302 | 15 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, |
4303 | 15 | CodeCompleter->includeGlobals(), |
4304 | 15 | CodeCompleter->loadExternal()); |
4305 | 15 | Results.setFilter(nullptr); |
4306 | 15 | } |
4307 | 28 | } |
4308 | 35 | Results.ExitScope(); |
4309 | | |
4310 | | // If we're in a context where we might have an expression (rather than a |
4311 | | // declaration), and what we've seen so far is an Objective-C type that could |
4312 | | // be a receiver of a class message, this may be a class message send with |
4313 | | // the initial opening bracket '[' missing. Add appropriate completions. |
4314 | 35 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers28 && |
4315 | 35 | DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier11 && |
4316 | 35 | DS.getTypeSpecType() == DeclSpec::TST_typename11 && |
4317 | 35 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified2 && |
4318 | 35 | DS.getTypeSpecSign() == TypeSpecifierSign::Unspecified2 && |
4319 | 35 | !DS.isTypeAltiVecVector()2 && S2 && |
4320 | 35 | (S->getFlags() & Scope::DeclScope) != 02 && |
4321 | 35 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | |
4322 | 2 | Scope::FunctionPrototypeScope | Scope::AtCatchScope)) == |
4323 | 2 | 0) { |
4324 | 2 | ParsedType T = DS.getRepAsType(); |
4325 | 2 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) |
4326 | 2 | AddClassMessageCompletions(*this, S, T, None, false, false, Results); |
4327 | 2 | } |
4328 | | |
4329 | | // Note that we intentionally suppress macro results here, since we do not |
4330 | | // encourage using macros to produce the names of entities. |
4331 | | |
4332 | 35 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
4333 | 35 | Results.data(), Results.size()); |
4334 | 35 | } |
4335 | | |
4336 | | struct Sema::CodeCompleteExpressionData { |
4337 | | CodeCompleteExpressionData(QualType PreferredType = QualType(), |
4338 | | bool IsParenthesized = false) |
4339 | | : PreferredType(PreferredType), IntegralConstantExpression(false), |
4340 | 397 | ObjCCollection(false), IsParenthesized(IsParenthesized) {} |
4341 | | |
4342 | | QualType PreferredType; |
4343 | | bool IntegralConstantExpression; |
4344 | | bool ObjCCollection; |
4345 | | bool IsParenthesized; |
4346 | | SmallVector<Decl *, 4> IgnoreDecls; |
4347 | | }; |
4348 | | |
4349 | | namespace { |
4350 | | /// Information that allows to avoid completing redundant enumerators. |
4351 | | struct CoveredEnumerators { |
4352 | | llvm::SmallPtrSet<EnumConstantDecl *, 8> Seen; |
4353 | | NestedNameSpecifier *SuggestedQualifier = nullptr; |
4354 | | }; |
4355 | | } // namespace |
4356 | | |
4357 | | static void AddEnumerators(ResultBuilder &Results, ASTContext &Context, |
4358 | | EnumDecl *Enum, DeclContext *CurContext, |
4359 | 23 | const CoveredEnumerators &Enumerators) { |
4360 | 23 | NestedNameSpecifier *Qualifier = Enumerators.SuggestedQualifier; |
4361 | 23 | if (Context.getLangOpts().CPlusPlus && !Qualifier12 && Enumerators.Seen.empty()10 ) { |
4362 | | // If there are no prior enumerators in C++, check whether we have to |
4363 | | // qualify the names of the enumerators that we suggest, because they |
4364 | | // may not be visible in this scope. |
4365 | 10 | Qualifier = getRequiredQualification(Context, CurContext, Enum); |
4366 | 10 | } |
4367 | | |
4368 | 23 | Results.EnterNewScope(); |
4369 | 66 | for (auto *E : Enum->enumerators()) { |
4370 | 66 | if (Enumerators.Seen.count(E)) |
4371 | 5 | continue; |
4372 | | |
4373 | 61 | CodeCompletionResult R(E, CCP_EnumInCase, Qualifier); |
4374 | 61 | Results.AddResult(R, CurContext, nullptr, false); |
4375 | 61 | } |
4376 | 23 | Results.ExitScope(); |
4377 | 23 | } |
4378 | | |
4379 | | /// Try to find a corresponding FunctionProtoType for function-like types (e.g. |
4380 | | /// function pointers, std::function, etc). |
4381 | 223 | static const FunctionProtoType *TryDeconstructFunctionLike(QualType T) { |
4382 | 223 | assert(!T.isNull()); |
4383 | | // Try to extract first template argument from std::function<> and similar. |
4384 | | // Note we only handle the sugared types, they closely match what users wrote. |
4385 | | // We explicitly choose to not handle ClassTemplateSpecializationDecl. |
4386 | 223 | if (auto *Specialization = T->getAs<TemplateSpecializationType>()) { |
4387 | 10 | if (Specialization->getNumArgs() != 1) |
4388 | 1 | return nullptr; |
4389 | 9 | const TemplateArgument &Argument = Specialization->getArg(0); |
4390 | 9 | if (Argument.getKind() != TemplateArgument::Type) |
4391 | 0 | return nullptr; |
4392 | 9 | return Argument.getAsType()->getAs<FunctionProtoType>(); |
4393 | 9 | } |
4394 | | // Handle other cases. |
4395 | 213 | if (T->isPointerType()) |
4396 | 51 | T = T->getPointeeType(); |
4397 | 213 | return T->getAs<FunctionProtoType>(); |
4398 | 223 | } |
4399 | | |
4400 | | /// Adds a pattern completion for a lambda expression with the specified |
4401 | | /// parameter types and placeholders for parameter names. |
4402 | | static void AddLambdaCompletion(ResultBuilder &Results, |
4403 | | llvm::ArrayRef<QualType> Parameters, |
4404 | 9 | const LangOptions &LangOpts) { |
4405 | 9 | if (!Results.includeCodePatterns()) |
4406 | 1 | return; |
4407 | 8 | CodeCompletionBuilder Completion(Results.getAllocator(), |
4408 | 8 | Results.getCodeCompletionTUInfo()); |
4409 | | // [](<parameters>) {} |
4410 | 8 | Completion.AddChunk(CodeCompletionString::CK_LeftBracket); |
4411 | 8 | Completion.AddPlaceholderChunk("="); |
4412 | 8 | Completion.AddChunk(CodeCompletionString::CK_RightBracket); |
4413 | 8 | if (!Parameters.empty()) { |
4414 | 5 | Completion.AddChunk(CodeCompletionString::CK_LeftParen); |
4415 | 5 | bool First = true; |
4416 | 7 | for (auto Parameter : Parameters) { |
4417 | 7 | if (!First) |
4418 | 2 | Completion.AddChunk(CodeCompletionString::ChunkKind::CK_Comma); |
4419 | 5 | else |
4420 | 5 | First = false; |
4421 | | |
4422 | 7 | constexpr llvm::StringLiteral NamePlaceholder = "!#!NAME_GOES_HERE!#!"; |
4423 | 7 | std::string Type = std::string(NamePlaceholder); |
4424 | 7 | Parameter.getAsStringInternal(Type, PrintingPolicy(LangOpts)); |
4425 | 7 | llvm::StringRef Prefix, Suffix; |
4426 | 7 | std::tie(Prefix, Suffix) = llvm::StringRef(Type).split(NamePlaceholder); |
4427 | 7 | Prefix = Prefix.rtrim(); |
4428 | 7 | Suffix = Suffix.ltrim(); |
4429 | | |
4430 | 7 | Completion.AddTextChunk(Completion.getAllocator().CopyString(Prefix)); |
4431 | 7 | Completion.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
4432 | 7 | Completion.AddPlaceholderChunk("parameter"); |
4433 | 7 | Completion.AddTextChunk(Completion.getAllocator().CopyString(Suffix)); |
4434 | 7 | }; |
4435 | 5 | Completion.AddChunk(CodeCompletionString::CK_RightParen); |
4436 | 5 | } |
4437 | 8 | Completion.AddChunk(clang::CodeCompletionString::CK_HorizontalSpace); |
4438 | 8 | Completion.AddChunk(CodeCompletionString::CK_LeftBrace); |
4439 | 8 | Completion.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
4440 | 8 | Completion.AddPlaceholderChunk("body"); |
4441 | 8 | Completion.AddChunk(CodeCompletionString::CK_HorizontalSpace); |
4442 | 8 | Completion.AddChunk(CodeCompletionString::CK_RightBrace); |
4443 | | |
4444 | 8 | Results.AddResult(Completion.TakeString()); |
4445 | 8 | } |
4446 | | |
4447 | | /// Perform code-completion in an expression context when we know what |
4448 | | /// type we're looking for. |
4449 | | void Sema::CodeCompleteExpression(Scope *S, |
4450 | 397 | const CodeCompleteExpressionData &Data) { |
4451 | 397 | ResultBuilder Results( |
4452 | 397 | *this, CodeCompleter->getAllocator(), |
4453 | 397 | CodeCompleter->getCodeCompletionTUInfo(), |
4454 | 397 | CodeCompletionContext( |
4455 | 397 | Data.IsParenthesized |
4456 | 397 | ? CodeCompletionContext::CCC_ParenthesizedExpression30 |
4457 | 397 | : CodeCompletionContext::CCC_Expression367 , |
4458 | 397 | Data.PreferredType)); |
4459 | 397 | auto PCC = |
4460 | 397 | Data.IsParenthesized ? PCC_ParenthesizedExpression30 : PCC_Expression367 ; |
4461 | 397 | if (Data.ObjCCollection) |
4462 | 2 | Results.setFilter(&ResultBuilder::IsObjCCollection); |
4463 | 395 | else if (Data.IntegralConstantExpression) |
4464 | 1 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); |
4465 | 394 | else if (WantTypesInContext(PCC, getLangOpts())) |
4466 | 314 | Results.setFilter(&ResultBuilder::IsOrdinaryName); |
4467 | 80 | else |
4468 | 80 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); |
4469 | | |
4470 | 397 | if (!Data.PreferredType.isNull()) |
4471 | 285 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); |
4472 | | |
4473 | | // Ignore any declarations that we were told that we don't care about. |
4474 | 441 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I44 ) |
4475 | 44 | Results.Ignore(Data.IgnoreDecls[I]); |
4476 | | |
4477 | 397 | CodeCompletionDeclConsumer Consumer(Results, CurContext); |
4478 | 397 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, |
4479 | 397 | CodeCompleter->includeGlobals(), |
4480 | 397 | CodeCompleter->loadExternal()); |
4481 | | |
4482 | 397 | Results.EnterNewScope(); |
4483 | 397 | AddOrdinaryNameResults(PCC, S, *this, Results); |
4484 | 397 | Results.ExitScope(); |
4485 | | |
4486 | 397 | bool PreferredTypeIsPointer = false; |
4487 | 397 | if (!Data.PreferredType.isNull()) { |
4488 | 285 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() || |
4489 | 285 | Data.PreferredType->isMemberPointerType()227 || |
4490 | 285 | Data.PreferredType->isBlockPointerType()227 ; |
4491 | 285 | if (Data.PreferredType->isEnumeralType()) { |
4492 | 15 | EnumDecl *Enum = Data.PreferredType->castAs<EnumType>()->getDecl(); |
4493 | 15 | if (auto *Def = Enum->getDefinition()) |
4494 | 15 | Enum = Def; |
4495 | | // FIXME: collect covered enumerators in cases like: |
4496 | | // if (x == my_enum::one) { ... } else if (x == ^) {} |
4497 | 15 | AddEnumerators(Results, Context, Enum, CurContext, CoveredEnumerators()); |
4498 | 15 | } |
4499 | 285 | } |
4500 | | |
4501 | 397 | if (S->getFnParent() && !Data.ObjCCollection376 && |
4502 | 397 | !Data.IntegralConstantExpression374 ) |
4503 | 373 | AddPrettyFunctionResults(getLangOpts(), Results); |
4504 | | |
4505 | 397 | if (CodeCompleter->includeMacros()) |
4506 | 140 | AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false, |
4507 | 140 | PreferredTypeIsPointer); |
4508 | | |
4509 | | // Complete a lambda expression when preferred type is a function. |
4510 | 397 | if (!Data.PreferredType.isNull() && getLangOpts().CPlusPlus11285 ) { |
4511 | 223 | if (const FunctionProtoType *F = |
4512 | 223 | TryDeconstructFunctionLike(Data.PreferredType)) |
4513 | 9 | AddLambdaCompletion(Results, F->getParamTypes(), getLangOpts()); |
4514 | 223 | } |
4515 | | |
4516 | 397 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), |
4517 | 397 | Results.data(), Results.size()); |
4518 | 397 | } |
4519 | | |
4520 | | void Sema::CodeCompleteExpression(Scope *S, QualType PreferredType, |
4521 | 352 | bool IsParenthesized) { |
4522 | 352 | return CodeCompleteExpression( |
4523 | 352 | S, CodeCompleteExpressionData(PreferredType, IsParenthesized)); |
4524 | 352 | } |
4525 | | |
4526 | | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E, |
4527 | 10 | QualType PreferredType) { |
4528 | 10 | if (E.isInvalid()) |
4529 | 2 | CodeCompleteExpression(S, PreferredType); |
4530 | 8 | else if (getLangOpts().ObjC) |
4531 | 3 | CodeCompleteObjCInstanceMessage(S, E.get(), None, false); |
4532 | 10 | } |
4533 | | |
4534 | | /// The set of properties that have already been added, referenced by |
4535 | | /// property name. |
4536 | | typedef llvm::SmallPtrSet<IdentifierInfo *, 16> AddedPropertiesSet; |
4537 | | |
4538 | | /// Retrieve the container definition, if any? |
4539 | 227 | static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) { |
4540 | 227 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
4541 | 152 | if (Interface->hasDefinition()) |
4542 | 152 | return Interface->getDefinition(); |
4543 | | |
4544 | 0 | return Interface; |
4545 | 152 | } |
4546 | | |
4547 | 75 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
4548 | 24 | if (Protocol->hasDefinition()) |
4549 | 24 | return Protocol->getDefinition(); |
4550 | | |
4551 | 0 | return Protocol; |
4552 | 24 | } |
4553 | 51 | return Container; |
4554 | 75 | } |
4555 | | |
4556 | | /// Adds a block invocation code completion result for the given block |
4557 | | /// declaration \p BD. |
4558 | | static void AddObjCBlockCall(ASTContext &Context, const PrintingPolicy &Policy, |
4559 | | CodeCompletionBuilder &Builder, |
4560 | | const NamedDecl *BD, |
4561 | | const FunctionTypeLoc &BlockLoc, |
4562 | 39 | const FunctionProtoTypeLoc &BlockProtoLoc) { |
4563 | 39 | Builder.AddResultTypeChunk( |
4564 | 39 | GetCompletionTypeString(BlockLoc.getReturnLoc().getType(), Context, |
4565 | 39 | Policy, Builder.getAllocator())); |
4566 | | |
4567 | 39 | AddTypedNameChunk(Context, Policy, BD, Builder); |
4568 | 39 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); |
4569 | | |
4570 | 39 | if (BlockProtoLoc && BlockProtoLoc.getTypePtr()->isVariadic()31 ) { |
4571 | 0 | Builder.AddPlaceholderChunk("..."); |
4572 | 39 | } else { |
4573 | 79 | for (unsigned I = 0, N = BlockLoc.getNumParams(); I != N; ++I40 ) { |
4574 | 40 | if (I) |
4575 | 9 | Builder.AddChunk(CodeCompletionString::CK_Comma); |
4576 | | |
4577 | | // Format the placeholder string. |
4578 | 40 | std::string PlaceholderStr = |
4579 | 40 | FormatFunctionParameter(Policy, BlockLoc.getParam(I)); |
4580 | | |
4581 | 40 | if (I == N - 1 && BlockProtoLoc31 && |
4582 | 40 | BlockProtoLoc.getTypePtr()->isVariadic()31 ) |
4583 | 0 | PlaceholderStr += ", ..."; |
4584 | | |
4585 | | // Add the placeholder string. |
4586 | 40 | Builder.AddPlaceholderChunk( |
4587 | 40 | Builder.getAllocator().CopyString(PlaceholderStr)); |
4588 | 40 | } |
4589 | 39 | } |
4590 | | |
4591 | 39 | Builder.AddChunk(CodeCompletionString::CK_RightParen); |
4592 | 39 | } |
4593 | | |
4594 | | static void |
4595 | | AddObjCProperties(const CodeCompletionContext &CCContext, |
4596 | | ObjCContainerDecl *Container, bool AllowCategories, |
4597 | | bool AllowNullaryMethods, DeclContext *CurContext, |
4598 | | AddedPropertiesSet &AddedProperties, ResultBuilder &Results, |
4599 | | bool IsBaseExprStatement = false, |
4600 | 66 | bool IsClassProperty = false, bool InOriginalClass = true) { |
4601 | 66 | typedef CodeCompletionResult Result; |
4602 | | |
4603 | | // Retrieve the definition. |
4604 | 66 | Container = getContainerDef(Container); |
4605 | | |
4606 | | // Add properties in this container. |
4607 | 134 | const auto AddProperty = [&](const ObjCPropertyDecl *P) { |
4608 | 134 | if (!AddedProperties.insert(P->getIdentifier()).second) |
4609 | 2 | return; |
4610 | | |
4611 | | // FIXME: Provide block invocation completion for non-statement |
4612 | | // expressions. |
4613 | 132 | if (!P->getType().getTypePtr()->isBlockPointerType() || |
4614 | 132 | !IsBaseExprStatement63 ) { |
4615 | 93 | Result R = Result(P, Results.getBasePriority(P), nullptr); |
4616 | 93 | if (!InOriginalClass) |
4617 | 40 | setInBaseClass(R); |
4618 | 93 | Results.MaybeAddResult(R, CurContext); |
4619 | 93 | return; |
4620 | 93 | } |
4621 | | |
4622 | | // Block setter and invocation completion is provided only when we are able |
4623 | | // to find the FunctionProtoTypeLoc with parameter names for the block. |
4624 | 39 | FunctionTypeLoc BlockLoc; |
4625 | 39 | FunctionProtoTypeLoc BlockProtoLoc; |
4626 | 39 | findTypeLocationForBlockDecl(P->getTypeSourceInfo(), BlockLoc, |
4627 | 39 | BlockProtoLoc); |
4628 | 39 | if (!BlockLoc) { |
4629 | 0 | Result R = Result(P, Results.getBasePriority(P), nullptr); |
4630 | 0 | if (!InOriginalClass) |
4631 | 0 | setInBaseClass(R); |
4632 | 0 | Results.MaybeAddResult(R, CurContext); |
4633 | 0 | return; |
4634 | 0 | } |
4635 | | |
4636 | | // The default completion result for block properties should be the block |
4637 | | // invocation completion when the base expression is a statement. |
4638 | 39 | CodeCompletionBuilder Builder(Results.getAllocator(), |
4639 | 39 | Results.getCodeCompletionTUInfo()); |
4640 | 39 | AddObjCBlockCall(Container->getASTContext(), |
4641 | 39 | getCompletionPrintingPolicy(Results.getSema()), Builder, P, |
4642 | 39 | BlockLoc, BlockProtoLoc); |
4643 | 39 | Result R = Result(Builder.TakeString(), P, Results.getBasePriority(P)); |
4644 | 39 | if (!InOriginalClass) |
4645 | 16 | setInBaseClass(R); |
4646 | 39 | Results.MaybeAddResult(R, CurContext); |
4647 | | |
4648 | | // Provide additional block setter completion iff the base expression is a |
4649 | | // statement and the block property is mutable. |
4650 | 39 | if (!P->isReadOnly()) { |
4651 | 19 | CodeCompletionBuilder Builder(Results.getAllocator(), |
4652 | 19 | Results.getCodeCompletionTUInfo()); |
4653 | 19 | AddResultTypeChunk(Container->getASTContext(), |
4654 | 19 | getCompletionPrintingPolicy(Results.getSema()), P, |
4655 | 19 | CCContext.getBaseType(), Builder); |
4656 | 19 | Builder.AddTypedTextChunk( |
4657 | 19 | Results.getAllocator().CopyString(P->getName())); |
4658 | 19 | Builder.AddChunk(CodeCompletionString::CK_Equal); |
4659 | | |
4660 | 19 | std::string PlaceholderStr = formatBlockPlaceholder( |
4661 | 19 | getCompletionPrintingPolicy(Results.getSema()), P, BlockLoc, |
4662 | 19 | BlockProtoLoc, /*SuppressBlockName=*/true); |
4663 | | // Add the placeholder string. |
4664 | 19 | Builder.AddPlaceholderChunk( |
4665 | 19 | Builder.getAllocator().CopyString(PlaceholderStr)); |
4666 | | |
4667 | | // When completing blocks properties that return void the default |
4668 | | // property completion result should show up before the setter, |
4669 | | // otherwise the setter completion should show up before the default |
4670 | | // property completion, as we normally want to use the result of the |
4671 | | // call. |
4672 | 19 | Result R = |
4673 | 19 | Result(Builder.TakeString(), P, |
4674 | 19 | Results.getBasePriority(P) + |
4675 | 19 | (BlockLoc.getTypePtr()->getReturnType()->isVoidType() |
4676 | 19 | ? CCD_BlockPropertySetter12 |
4677 | 19 | : -CCD_BlockPropertySetter7 )); |
4678 | 19 | if (!InOriginalClass) |
4679 | 10 | setInBaseClass(R); |
4680 | 19 | Results.MaybeAddResult(R, CurContext); |
4681 | 19 | } |
4682 | 39 | }; |
4683 | | |
4684 | 66 | if (IsClassProperty) { |
4685 | 7 | for (const auto *P : Container->class_properties()) |
4686 | 8 | AddProperty(P); |
4687 | 59 | } else { |
4688 | 59 | for (const auto *P : Container->instance_properties()) |
4689 | 126 | AddProperty(P); |
4690 | 59 | } |
4691 | | |
4692 | | // Add nullary methods or implicit class properties |
4693 | 66 | if (AllowNullaryMethods) { |
4694 | 58 | ASTContext &Context = Container->getASTContext(); |
4695 | 58 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); |
4696 | | // Adds a method result |
4697 | 130 | const auto AddMethod = [&](const ObjCMethodDecl *M) { |
4698 | 130 | IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0); |
4699 | 130 | if (!Name) |
4700 | 0 | return; |
4701 | 130 | if (!AddedProperties.insert(Name).second) |
4702 | 117 | return; |
4703 | 13 | CodeCompletionBuilder Builder(Results.getAllocator(), |
4704 | 13 | Results.getCodeCompletionTUInfo()); |
4705 | 13 | AddResultTypeChunk(Context, Policy, M, CCContext.getBaseType(), Builder); |
4706 | 13 | Builder.AddTypedTextChunk( |
4707 | 13 | Results.getAllocator().CopyString(Name->getName())); |
4708 | 13 | Result R = Result(Builder.TakeString(), M, |
4709 | 13 | CCP_MemberDeclaration + CCD_MethodAsProperty); |
4710 | 13 | if (!InOriginalClass) |
4711 | 3 | setInBaseClass(R); |
4712 | 13 | Results.MaybeAddResult(R, CurContext); |
4713 | 13 | }; |
4714 | | |
4715 | 58 | if (IsClassProperty) { |
4716 | 30 | for (const auto *M : Container->methods()) { |
4717 | | // Gather the class method that can be used as implicit property |
4718 | | // getters. Methods with arguments or methods that return void aren't |
4719 | | // added to the results as they can't be used as a getter. |
4720 | 30 | if (!M->getSelector().isUnarySelector() || |
4721 | 30 | M->getReturnType()->isVoidType()22 || M->isInstanceMethod()20 ) |
4722 | 14 | continue; |
4723 | 16 | AddMethod(M); |
4724 | 16 | } |
4725 | 51 | } else { |
4726 | 195 | for (auto *M : Container->methods()) { |
4727 | 195 | if (M->getSelector().isUnarySelector()) |
4728 | 114 | AddMethod(M); |
4729 | 195 | } |
4730 | 51 | } |
4731 | 58 | } |
4732 | | |
4733 | | // Add properties in referenced protocols. |
4734 | 66 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
4735 | 7 | for (auto *P : Protocol->protocols()) |
4736 | 1 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
4737 | 1 | CurContext, AddedProperties, Results, |
4738 | 1 | IsBaseExprStatement, IsClassProperty, |
4739 | 1 | /*InOriginalClass*/ false); |
4740 | 59 | } else if (ObjCInterfaceDecl *IFace = |
4741 | 59 | dyn_cast<ObjCInterfaceDecl>(Container)) { |
4742 | 55 | if (AllowCategories) { |
4743 | | // Look through categories. |
4744 | 48 | for (auto *Cat : IFace->known_categories()) |
4745 | 4 | AddObjCProperties(CCContext, Cat, AllowCategories, AllowNullaryMethods, |
4746 | 4 | CurContext, AddedProperties, Results, |
4747 | 4 | IsBaseExprStatement, IsClassProperty, |
4748 | 4 | InOriginalClass); |
4749 | 48 | } |
4750 | | |
4751 | | // Look through protocols. |
4752 | 55 | for (auto *I : IFace->all_referenced_protocols()) |
4753 | 5 | AddObjCProperties(CCContext, I, AllowCategories, AllowNullaryMethods, |
4754 | 5 | CurContext, AddedProperties, Results, |
4755 | 5 | IsBaseExprStatement, IsClassProperty, |
4756 | 5 | /*InOriginalClass*/ false); |
4757 | | |
4758 | | // Look in the superclass. |
4759 | 55 | if (IFace->getSuperClass()) |
4760 | 26 | AddObjCProperties(CCContext, IFace->getSuperClass(), AllowCategories, |
4761 | 26 | AllowNullaryMethods, CurContext, AddedProperties, |
4762 | 26 | Results, IsBaseExprStatement, IsClassProperty, |
4763 | 26 | /*InOriginalClass*/ false); |
4764 | 55 | } else if (const auto *4 Category4 = |
4765 | 4 | dyn_cast<ObjCCategoryDecl>(Container)) { |
4766 | | // Look through protocols. |
4767 | 4 | for (auto *P : Category->protocols()) |
4768 | 0 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, |
4769 | 0 | CurContext, AddedProperties, Results, |
4770 | 0 | IsBaseExprStatement, IsClassProperty, |
4771 | 0 | /*InOriginalClass*/ false); |
4772 | 4 | } |
4773 | 66 | } |
4774 | | |
4775 | | static void AddRecordMembersCompletionResults( |
4776 | | Sema &SemaRef, ResultBuilder &Results, Scope *S, QualType BaseType, |
4777 | 126 | ExprValueKind BaseKind, RecordDecl *RD, Optional<FixItHint> AccessOpFixIt) { |
4778 | | // Indicate that we are performing a member access, and the cv-qualifiers |
4779 | | // for the base object type. |
4780 | 126 | Results.setObjectTypeQualifiers(BaseType.getQualifiers(), BaseKind); |
4781 | | |
4782 | | // Access to a C/C++ class, struct, or union. |
4783 | 126 | Results.allowNestedNameSpecifiers(); |
4784 | 126 | std::vector<FixItHint> FixIts; |
4785 | 126 | if (AccessOpFixIt) |
4786 | 6 | FixIts.emplace_back(AccessOpFixIt.getValue()); |
4787 | 126 | CodeCompletionDeclConsumer Consumer(Results, RD, BaseType, std::move(FixIts)); |
4788 | 126 | SemaRef.LookupVisibleDecls(RD, Sema::LookupMemberName, Consumer, |
4789 | 126 | SemaRef.CodeCompleter->includeGlobals(), |
4790 | 126 | /*IncludeDependentBases=*/true, |
4791 | 126 | SemaRef.CodeCompleter->loadExternal()); |
4792 | | |
4793 | 126 | if (SemaRef.getLangOpts().CPlusPlus) { |
4794 | 107 | if (!Results.empty()) { |
4795 | | // The "template" keyword can follow "->" or "." in the grammar. |
4796 | | // However, we only want to suggest the template keyword if something |
4797 | | // is dependent. |
4798 | 107 | bool IsDependent = BaseType->isDependentType(); |
4799 | 107 | if (!IsDependent) { |
4800 | 106 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()4 ) |
4801 | 106 | if (DeclContext *Ctx = DepScope->getEntity()) { |
4802 | 102 | IsDependent = Ctx->isDependentContext(); |
4803 | 102 | break; |
4804 | 102 | } |
4805 | 102 | } |
4806 | | |
4807 | 107 | if (IsDependent) |
4808 | 7 | Results.AddResult(CodeCompletionResult("template")); |
4809 | 107 | } |
4810 | 107 | } |
4811 | 126 | } |
4812 | | |
4813 | | // Returns the RecordDecl inside the BaseType, falling back to primary template |
4814 | | // in case of specializations. Since we might not have a decl for the |
4815 | | // instantiation/specialization yet, e.g. dependent code. |
4816 | 235 | static RecordDecl *getAsRecordDecl(const QualType BaseType) { |
4817 | 235 | if (auto *RD = BaseType->getAsRecordDecl()) { |
4818 | 186 | if (const auto *CTSD = |
4819 | 186 | llvm::dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
4820 | | // Template might not be instantiated yet, fall back to primary template |
4821 | | // in such cases. |
4822 | 10 | if (CTSD->getTemplateSpecializationKind() == TSK_Undeclared) |
4823 | 2 | RD = CTSD->getSpecializedTemplate()->getTemplatedDecl(); |
4824 | 10 | } |
4825 | 186 | return RD; |
4826 | 186 | } |
4827 | | |
4828 | 49 | if (const auto *TST = BaseType->getAs<TemplateSpecializationType>()) { |
4829 | 9 | if (const auto *TD = dyn_cast_or_null<ClassTemplateDecl>( |
4830 | 9 | TST->getTemplateName().getAsTemplateDecl())) { |
4831 | 9 | return TD->getTemplatedDecl(); |
4832 | 9 | } |
4833 | 9 | } |
4834 | | |
4835 | 40 | return nullptr; |
4836 | 49 | } |
4837 | | |
4838 | | namespace { |
4839 | | // Collects completion-relevant information about a concept-constrainted type T. |
4840 | | // In particular, examines the constraint expressions to find members of T. |
4841 | | // |
4842 | | // The design is very simple: we walk down each constraint looking for |
4843 | | // expressions of the form T.foo(). |
4844 | | // If we're extra lucky, the return type is specified. |
4845 | | // We don't do any clever handling of && or || in constraint expressions, we |
4846 | | // take members from both branches. |
4847 | | // |
4848 | | // For example, given: |
4849 | | // template <class T> concept X = requires (T t, string& s) { t.print(s); }; |
4850 | | // template <X U> void foo(U u) { u.^ } |
4851 | | // We want to suggest the inferred member function 'print(string)'. |
4852 | | // We see that u has type U, so X<U> holds. |
4853 | | // X<U> requires t.print(s) to be valid, where t has type U (substituted for T). |
4854 | | // By looking at the CallExpr we find the signature of print(). |
4855 | | // |
4856 | | // While we tend to know in advance which kind of members (access via . -> ::) |
4857 | | // we want, it's simpler just to gather them all and post-filter. |
4858 | | // |
4859 | | // FIXME: some of this machinery could be used for non-concept type-parms too, |
4860 | | // enabling completion for type parameters based on other uses of that param. |
4861 | | // |
4862 | | // FIXME: there are other cases where a type can be constrained by a concept, |
4863 | | // e.g. inside `if constexpr(ConceptSpecializationExpr) { ... }` |
4864 | | class ConceptInfo { |
4865 | | public: |
4866 | | // Describes a likely member of a type, inferred by concept constraints. |
4867 | | // Offered as a code completion for T. T-> and T:: contexts. |
4868 | | struct Member { |
4869 | | // Always non-null: we only handle members with ordinary identifier names. |
4870 | | const IdentifierInfo *Name = nullptr; |
4871 | | // Set for functions we've seen called. |
4872 | | // We don't have the declared parameter types, only the actual types of |
4873 | | // arguments we've seen. These are still valuable, as it's hard to render |
4874 | | // a useful function completion with neither parameter types nor names! |
4875 | | llvm::Optional<SmallVector<QualType, 1>> ArgTypes; |
4876 | | // Whether this is accessed as T.member, T->member, or T::member. |
4877 | | enum AccessOperator { |
4878 | | Colons, |
4879 | | Arrow, |
4880 | | Dot, |
4881 | | } Operator = Dot; |
4882 | | // What's known about the type of a variable or return type of a function. |
4883 | | const TypeConstraint *ResultType = nullptr; |
4884 | | // FIXME: also track: |
4885 | | // - kind of entity (function/variable/type), to expose structured results |
4886 | | // - template args kinds/types, as a proxy for template params |
4887 | | |
4888 | | // For now we simply return these results as "pattern" strings. |
4889 | | CodeCompletionString *render(Sema &S, CodeCompletionAllocator &Alloc, |
4890 | 16 | CodeCompletionTUInfo &Info) const { |
4891 | 16 | CodeCompletionBuilder B(Alloc, Info); |
4892 | | // Result type |
4893 | 16 | if (ResultType) { |
4894 | 6 | std::string AsString; |
4895 | 6 | { |
4896 | 6 | llvm::raw_string_ostream OS(AsString); |
4897 | 6 | QualType ExactType = deduceType(*ResultType); |
4898 | 6 | if (!ExactType.isNull()) |
4899 | 2 | ExactType.print(OS, getCompletionPrintingPolicy(S)); |
4900 | 4 | else |
4901 | 4 | ResultType->print(OS, getCompletionPrintingPolicy(S)); |
4902 | 6 | } |
4903 | 6 | B.AddResultTypeChunk(Alloc.CopyString(AsString)); |
4904 | 6 | } |
4905 | | // Member name |
4906 | 16 | B.AddTypedTextChunk(Alloc.CopyString(Name->getName())); |
4907 | | // Function argument list |
4908 | 16 | if (ArgTypes) { |
4909 | 12 | B.AddChunk(clang::CodeCompletionString::CK_LeftParen); |
4910 | 12 | bool First = true; |
4911 | 12 | for (QualType Arg : *ArgTypes) { |
4912 | 4 | if (First) |
4913 | 4 | First = false; |
4914 | 0 | else { |
4915 | 0 | B.AddChunk(clang::CodeCompletionString::CK_Comma); |
4916 | 0 | B.AddChunk(clang::CodeCompletionString::CK_HorizontalSpace); |
4917 | 0 | } |
4918 | 4 | B.AddPlaceholderChunk(Alloc.CopyString( |
4919 | 4 | Arg.getAsString(getCompletionPrintingPolicy(S)))); |
4920 | 4 | } |
4921 | 12 | B.AddChunk(clang::CodeCompletionString::CK_RightParen); |
4922 | 12 | } |
4923 | 16 | return B.TakeString(); |
4924 | 16 | } |
4925 | | }; |
4926 | | |
4927 | | // BaseType is the type parameter T to infer members from. |
4928 | | // T must be accessible within S, as we use it to find the template entity |
4929 | | // that T is attached to in order to gather the relevant constraints. |
4930 | 5 | ConceptInfo(const TemplateTypeParmType &BaseType, Scope *S) { |
4931 | 5 | auto *TemplatedEntity = getTemplatedEntity(BaseType.getDecl(), S); |
4932 | 5 | for (const Expr *E : constraintsForTemplatedEntity(TemplatedEntity)) |
4933 | 15 | believe(E, &BaseType); |
4934 | 5 | } |
4935 | | |
4936 | 5 | std::vector<Member> members() { |
4937 | 5 | std::vector<Member> Results; |
4938 | 5 | for (const auto &E : this->Results) |
4939 | 45 | Results.push_back(E.second); |
4940 | 100 | llvm::sort(Results, [](const Member &L, const Member &R) { |
4941 | 100 | return L.Name->getName() < R.Name->getName(); |
4942 | 100 | }); |
4943 | 5 | return Results; |
4944 | 5 | } |
4945 | | |
4946 | | private: |
4947 | | // Infer members of T, given that the expression E (dependent on T) is true. |
4948 | 60 | void believe(const Expr *E, const TemplateTypeParmType *T) { |
4949 | 60 | if (!E || !T) |
4950 | 0 | return; |
4951 | 60 | if (auto *CSE = dyn_cast<ConceptSpecializationExpr>(E)) { |
4952 | | // If the concept is |
4953 | | // template <class A, class B> concept CD = f<A, B>(); |
4954 | | // And the concept specialization is |
4955 | | // CD<int, T> |
4956 | | // Then we're substituting T for B, so we want to make f<A, B>() true |
4957 | | // by adding members to B - i.e. believe(f<A, B>(), B); |
4958 | | // |
4959 | | // For simplicity: |
4960 | | // - we don't attempt to substitute int for A |
4961 | | // - when T is used in other ways (like CD<T*>) we ignore it |
4962 | 20 | ConceptDecl *CD = CSE->getNamedConcept(); |
4963 | 20 | TemplateParameterList *Params = CD->getTemplateParameters(); |
4964 | 20 | unsigned Index = 0; |
4965 | 30 | for (const auto &Arg : CSE->getTemplateArguments()) { |
4966 | 30 | if (Index >= Params->size()) |
4967 | 0 | break; // Won't happen in valid code. |
4968 | 30 | if (isApprox(Arg, T)) { |
4969 | 20 | auto *TTPD = dyn_cast<TemplateTypeParmDecl>(Params->getParam(Index)); |
4970 | 20 | if (!TTPD) |
4971 | 0 | continue; |
4972 | | // T was used as an argument, and bound to the parameter TT. |
4973 | 20 | auto *TT = cast<TemplateTypeParmType>(TTPD->getTypeForDecl()); |
4974 | | // So now we know the constraint as a function of TT is true. |
4975 | 20 | believe(CD->getConstraintExpr(), TT); |
4976 | | // (concepts themselves have no associated constraints to require) |
4977 | 20 | } |
4978 | | |
4979 | 30 | ++Index; |
4980 | 30 | } |
4981 | 40 | } else if (auto *BO = dyn_cast<BinaryOperator>(E)) { |
4982 | | // For A && B, we can infer members from both branches. |
4983 | | // For A || B, the union is still more useful than the intersection. |
4984 | 10 | if (BO->getOpcode() == BO_LAnd || BO->getOpcode() == BO_LOr5 ) { |
4985 | 10 | believe(BO->getLHS(), T); |
4986 | 10 | believe(BO->getRHS(), T); |
4987 | 10 | } |
4988 | 30 | } else if (auto *RE = dyn_cast<RequiresExpr>(E)) { |
4989 | | // A requires(){...} lets us infer members from each requirement. |
4990 | 50 | for (const concepts::Requirement *Req : RE->getRequirements()) { |
4991 | 50 | if (!Req->isDependent()) |
4992 | 0 | continue; // Can't tell us anything about T. |
4993 | | // Now Req cannot a substitution-error: those aren't dependent. |
4994 | | |
4995 | 50 | if (auto *TR = dyn_cast<concepts::TypeRequirement>(Req)) { |
4996 | | // Do a full traversal so we get `foo` from `typename T::foo::bar`. |
4997 | 5 | QualType AssertedType = TR->getType()->getType(); |
4998 | 5 | ValidVisitor(this, T).TraverseType(AssertedType); |
4999 | 45 | } else if (auto *ER = dyn_cast<concepts::ExprRequirement>(Req)) { |
5000 | 40 | ValidVisitor Visitor(this, T); |
5001 | | // If we have a type constraint on the value of the expression, |
5002 | | // AND the whole outer expression describes a member, then we'll |
5003 | | // be able to use the constraint to provide the return type. |
5004 | 40 | if (ER->getReturnTypeRequirement().isTypeConstraint()) { |
5005 | 15 | Visitor.OuterType = |
5006 | 15 | ER->getReturnTypeRequirement().getTypeConstraint(); |
5007 | 15 | Visitor.OuterExpr = ER->getExpr(); |
5008 | 15 | } |
5009 | 40 | Visitor.TraverseStmt(ER->getExpr()); |
5010 | 40 | } else if (auto *5 NR5 = dyn_cast<concepts::NestedRequirement>(Req)) { |
5011 | 5 | believe(NR->getConstraintExpr(), T); |
5012 | 5 | } |
5013 | 50 | } |
5014 | 30 | } |
5015 | 60 | } |
5016 | | |
5017 | | // This visitor infers members of T based on traversing expressions/types |
5018 | | // that involve T. It is invoked with code known to be valid for T. |
5019 | | class ValidVisitor : public RecursiveASTVisitor<ValidVisitor> { |
5020 | | ConceptInfo *Outer; |
5021 | | const TemplateTypeParmType *T; |
5022 | | |
5023 | | CallExpr *Caller = nullptr; |
5024 | | Expr *Callee = nullptr; |
5025 | | |
5026 | | public: |
5027 | | // If set, OuterExpr is constrained by OuterType. |
5028 | | Expr *OuterExpr = nullptr; |
5029 | | const TypeConstraint *OuterType = nullptr; |
5030 | | |
5031 | | ValidVisitor(ConceptInfo *Outer, const TemplateTypeParmType *T) |
5032 | 45 | : Outer(Outer), T(T) { |
5033 | 45 | assert(T); |
5034 | 45 | } |
5035 | | |
5036 | | // In T.foo or T->foo, `foo` is a member function/variable. |
5037 | 35 | bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { |
5038 | 35 | const Type *Base = E->getBaseType().getTypePtr(); |
5039 | 35 | bool IsArrow = E->isArrow(); |
5040 | 35 | if (Base->isPointerType() && IsArrow5 ) { |
5041 | 5 | IsArrow = false; |
5042 | 5 | Base = Base->getPointeeType().getTypePtr(); |
5043 | 5 | } |
5044 | 35 | if (isApprox(Base, T)) |
5045 | 35 | addValue(E, E->getMember(), IsArrow ? Member::Arrow5 : Member::Dot30 ); |
5046 | 35 | return true; |
5047 | 35 | } |
5048 | | |
5049 | | // In T::foo, `foo` is a static member function/variable. |
5050 | 5 | bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { |
5051 | 5 | if (E->getQualifier() && isApprox(E->getQualifier()->getAsType(), T)) |
5052 | 0 | addValue(E, E->getDeclName(), Member::Colons); |
5053 | 5 | return true; |
5054 | 5 | } |
5055 | | |
5056 | | // In T::typename foo, `foo` is a type. |
5057 | 5 | bool VisitDependentNameType(DependentNameType *DNT) { |
5058 | 5 | const auto *Q = DNT->getQualifier(); |
5059 | 5 | if (Q && isApprox(Q->getAsType(), T)) |
5060 | 5 | addType(DNT->getIdentifier()); |
5061 | 5 | return true; |
5062 | 5 | } |
5063 | | |
5064 | | // In T::foo::bar, `foo` must be a type. |
5065 | | // VisitNNS() doesn't exist, and TraverseNNS isn't always called :-( |
5066 | 85 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSL) { |
5067 | 85 | if (NNSL) { |
5068 | 10 | NestedNameSpecifier *NNS = NNSL.getNestedNameSpecifier(); |
5069 | 10 | const auto *Q = NNS->getPrefix(); |
5070 | 10 | if (Q && isApprox(Q->getAsType(), T)5 ) |
5071 | 5 | addType(NNS->getAsIdentifier()); |
5072 | 10 | } |
5073 | | // FIXME: also handle T::foo<X>::bar |
5074 | 85 | return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(NNSL); |
5075 | 85 | } |
5076 | | |
5077 | | // FIXME also handle T::foo<X> |
5078 | | |
5079 | | // Track the innermost caller/callee relationship so we can tell if a |
5080 | | // nested expr is being called as a function. |
5081 | 30 | bool VisitCallExpr(CallExpr *CE) { |
5082 | 30 | Caller = CE; |
5083 | 30 | Callee = CE->getCallee(); |
5084 | 30 | return true; |
5085 | 30 | } |
5086 | | |
5087 | | private: |
5088 | 45 | void addResult(Member &&M) { |
5089 | 45 | auto R = Outer->Results.try_emplace(M.Name); |
5090 | 45 | Member &O = R.first->second; |
5091 | | // Overwrite existing if the new member has more info. |
5092 | | // The preference of . vs :: vs -> is fairly arbitrary. |
5093 | 45 | if (/*Inserted*/ R.second || |
5094 | 45 | std::make_tuple(M.ArgTypes.hasValue(), M.ResultType != nullptr, |
5095 | 0 | M.Operator) > std::make_tuple(O.ArgTypes.hasValue(), |
5096 | 0 | O.ResultType != nullptr, |
5097 | 0 | O.Operator)) |
5098 | 45 | O = std::move(M); |
5099 | 45 | } |
5100 | | |
5101 | 10 | void addType(const IdentifierInfo *Name) { |
5102 | 10 | if (!Name) |
5103 | 0 | return; |
5104 | 10 | Member M; |
5105 | 10 | M.Name = Name; |
5106 | 10 | M.Operator = Member::Colons; |
5107 | 10 | addResult(std::move(M)); |
5108 | 10 | } |
5109 | | |
5110 | | void addValue(Expr *E, DeclarationName Name, |
5111 | 35 | Member::AccessOperator Operator) { |
5112 | 35 | if (!Name.isIdentifier()) |
5113 | 0 | return; |
5114 | 35 | Member Result; |
5115 | 35 | Result.Name = Name.getAsIdentifierInfo(); |
5116 | 35 | Result.Operator = Operator; |
5117 | | // If this is the callee of an immediately-enclosing CallExpr, then |
5118 | | // treat it as a method, otherwise it's a variable. |
5119 | 35 | if (Caller != nullptr && Callee == E30 ) { |
5120 | 30 | Result.ArgTypes.emplace(); |
5121 | 30 | for (const auto *Arg : Caller->arguments()) |
5122 | 10 | Result.ArgTypes->push_back(Arg->getType()); |
5123 | 30 | if (Caller == OuterExpr) { |
5124 | 10 | Result.ResultType = OuterType; |
5125 | 10 | } |
5126 | 30 | } else { |
5127 | 5 | if (E == OuterExpr) |
5128 | 5 | Result.ResultType = OuterType; |
5129 | 5 | } |
5130 | 35 | addResult(std::move(Result)); |
5131 | 35 | } |
5132 | | }; |
5133 | | |
5134 | 30 | static bool isApprox(const TemplateArgument &Arg, const Type *T) { |
5135 | 30 | return Arg.getKind() == TemplateArgument::Type && |
5136 | 30 | isApprox(Arg.getAsType().getTypePtr(), T); |
5137 | 30 | } |
5138 | | |
5139 | 80 | static bool isApprox(const Type *T1, const Type *T2) { |
5140 | 80 | return T1 && T275 && |
5141 | 80 | T1->getCanonicalTypeUnqualified() == |
5142 | 75 | T2->getCanonicalTypeUnqualified(); |
5143 | 80 | } |
5144 | | |
5145 | | // Returns the DeclContext immediately enclosed by the template parameter |
5146 | | // scope. For primary templates, this is the templated (e.g.) CXXRecordDecl. |
5147 | | // For specializations, this is e.g. ClassTemplatePartialSpecializationDecl. |
5148 | | static DeclContext *getTemplatedEntity(const TemplateTypeParmDecl *D, |
5149 | 5 | Scope *S) { |
5150 | 5 | if (D == nullptr) |
5151 | 0 | return nullptr; |
5152 | 5 | Scope *Inner = nullptr; |
5153 | 10 | while (S) { |
5154 | 10 | if (S->isTemplateParamScope() && S->isDeclScope(D)5 ) |
5155 | 5 | return Inner ? Inner->getEntity() : nullptr0 ; |
5156 | 5 | Inner = S; |
5157 | 5 | S = S->getParent(); |
5158 | 5 | } |
5159 | 0 | return nullptr; |
5160 | 5 | } |
5161 | | |
5162 | | // Gets all
|