File: | tools/clang/lib/Sema/SemaCodeComplete.cpp |
Warning: | line 6556, column 13 Dereference of null pointer |
1 | //===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | // | |||
10 | // This file defines the code-completion semantic actions. | |||
11 | // | |||
12 | //===----------------------------------------------------------------------===// | |||
13 | #include "clang/Sema/SemaInternal.h" | |||
14 | #include "clang/AST/DeclObjC.h" | |||
15 | #include "clang/AST/ExprCXX.h" | |||
16 | #include "clang/AST/ExprObjC.h" | |||
17 | #include "clang/Basic/CharInfo.h" | |||
18 | #include "clang/Lex/HeaderSearch.h" | |||
19 | #include "clang/Lex/MacroInfo.h" | |||
20 | #include "clang/Lex/Preprocessor.h" | |||
21 | #include "clang/Sema/CodeCompleteConsumer.h" | |||
22 | #include "clang/Sema/Lookup.h" | |||
23 | #include "clang/Sema/Overload.h" | |||
24 | #include "clang/Sema/Scope.h" | |||
25 | #include "clang/Sema/ScopeInfo.h" | |||
26 | #include "llvm/ADT/DenseSet.h" | |||
27 | #include "llvm/ADT/SmallBitVector.h" | |||
28 | #include "llvm/ADT/SmallPtrSet.h" | |||
29 | #include "llvm/ADT/SmallString.h" | |||
30 | #include "llvm/ADT/StringExtras.h" | |||
31 | #include "llvm/ADT/StringSwitch.h" | |||
32 | #include "llvm/ADT/Twine.h" | |||
33 | #include <list> | |||
34 | #include <map> | |||
35 | #include <vector> | |||
36 | ||||
37 | using namespace clang; | |||
38 | using namespace sema; | |||
39 | ||||
40 | namespace { | |||
41 | /// \brief A container of code-completion results. | |||
42 | class ResultBuilder { | |||
43 | public: | |||
44 | /// \brief The type of a name-lookup filter, which can be provided to the | |||
45 | /// name-lookup routines to specify which declarations should be included in | |||
46 | /// the result set (when it returns true) and which declarations should be | |||
47 | /// filtered out (returns false). | |||
48 | typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const; | |||
49 | ||||
50 | typedef CodeCompletionResult Result; | |||
51 | ||||
52 | private: | |||
53 | /// \brief The actual results we have found. | |||
54 | std::vector<Result> Results; | |||
55 | ||||
56 | /// \brief A record of all of the declarations we have found and placed | |||
57 | /// into the result set, used to ensure that no declaration ever gets into | |||
58 | /// the result set twice. | |||
59 | llvm::SmallPtrSet<const Decl*, 16> AllDeclsFound; | |||
60 | ||||
61 | typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair; | |||
62 | ||||
63 | /// \brief An entry in the shadow map, which is optimized to store | |||
64 | /// a single (declaration, index) mapping (the common case) but | |||
65 | /// can also store a list of (declaration, index) mappings. | |||
66 | class ShadowMapEntry { | |||
67 | typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector; | |||
68 | ||||
69 | /// \brief Contains either the solitary NamedDecl * or a vector | |||
70 | /// of (declaration, index) pairs. | |||
71 | llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector*> DeclOrVector; | |||
72 | ||||
73 | /// \brief When the entry contains a single declaration, this is | |||
74 | /// the index associated with that entry. | |||
75 | unsigned SingleDeclIndex; | |||
76 | ||||
77 | public: | |||
78 | ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { } | |||
79 | ||||
80 | void Add(const NamedDecl *ND, unsigned Index) { | |||
81 | if (DeclOrVector.isNull()) { | |||
82 | // 0 - > 1 elements: just set the single element information. | |||
83 | DeclOrVector = ND; | |||
84 | SingleDeclIndex = Index; | |||
85 | return; | |||
86 | } | |||
87 | ||||
88 | if (const NamedDecl *PrevND = | |||
89 | DeclOrVector.dyn_cast<const NamedDecl *>()) { | |||
90 | // 1 -> 2 elements: create the vector of results and push in the | |||
91 | // existing declaration. | |||
92 | DeclIndexPairVector *Vec = new DeclIndexPairVector; | |||
93 | Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex)); | |||
94 | DeclOrVector = Vec; | |||
95 | } | |||
96 | ||||
97 | // Add the new element to the end of the vector. | |||
98 | DeclOrVector.get<DeclIndexPairVector*>()->push_back( | |||
99 | DeclIndexPair(ND, Index)); | |||
100 | } | |||
101 | ||||
102 | void Destroy() { | |||
103 | if (DeclIndexPairVector *Vec | |||
104 | = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) { | |||
105 | delete Vec; | |||
106 | DeclOrVector = ((NamedDecl *)nullptr); | |||
107 | } | |||
108 | } | |||
109 | ||||
110 | // Iteration. | |||
111 | class iterator; | |||
112 | iterator begin() const; | |||
113 | iterator end() const; | |||
114 | }; | |||
115 | ||||
116 | /// \brief A mapping from declaration names to the declarations that have | |||
117 | /// this name within a particular scope and their index within the list of | |||
118 | /// results. | |||
119 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; | |||
120 | ||||
121 | /// \brief The semantic analysis object for which results are being | |||
122 | /// produced. | |||
123 | Sema &SemaRef; | |||
124 | ||||
125 | /// \brief The allocator used to allocate new code-completion strings. | |||
126 | CodeCompletionAllocator &Allocator; | |||
127 | ||||
128 | CodeCompletionTUInfo &CCTUInfo; | |||
129 | ||||
130 | /// \brief If non-NULL, a filter function used to remove any code-completion | |||
131 | /// results that are not desirable. | |||
132 | LookupFilter Filter; | |||
133 | ||||
134 | /// \brief Whether we should allow declarations as | |||
135 | /// nested-name-specifiers that would otherwise be filtered out. | |||
136 | bool AllowNestedNameSpecifiers; | |||
137 | ||||
138 | /// \brief If set, the type that we would prefer our resulting value | |||
139 | /// declarations to have. | |||
140 | /// | |||
141 | /// Closely matching the preferred type gives a boost to a result's | |||
142 | /// priority. | |||
143 | CanQualType PreferredType; | |||
144 | ||||
145 | /// \brief A list of shadow maps, which is used to model name hiding at | |||
146 | /// different levels of, e.g., the inheritance hierarchy. | |||
147 | std::list<ShadowMap> ShadowMaps; | |||
148 | ||||
149 | /// \brief If we're potentially referring to a C++ member function, the set | |||
150 | /// of qualifiers applied to the object type. | |||
151 | Qualifiers ObjectTypeQualifiers; | |||
152 | ||||
153 | /// \brief Whether the \p ObjectTypeQualifiers field is active. | |||
154 | bool HasObjectTypeQualifiers; | |||
155 | ||||
156 | /// \brief The selector that we prefer. | |||
157 | Selector PreferredSelector; | |||
158 | ||||
159 | /// \brief The completion context in which we are gathering results. | |||
160 | CodeCompletionContext CompletionContext; | |||
161 | ||||
162 | /// \brief If we are in an instance method definition, the \@implementation | |||
163 | /// object. | |||
164 | ObjCImplementationDecl *ObjCImplementation; | |||
165 | ||||
166 | void AdjustResultPriorityForDecl(Result &R); | |||
167 | ||||
168 | void MaybeAddConstructorResults(Result R); | |||
169 | ||||
170 | public: | |||
171 | explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator, | |||
172 | CodeCompletionTUInfo &CCTUInfo, | |||
173 | const CodeCompletionContext &CompletionContext, | |||
174 | LookupFilter Filter = nullptr) | |||
175 | : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo), | |||
176 | Filter(Filter), | |||
177 | AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false), | |||
178 | CompletionContext(CompletionContext), | |||
179 | ObjCImplementation(nullptr) | |||
180 | { | |||
181 | // If this is an Objective-C instance method definition, dig out the | |||
182 | // corresponding implementation. | |||
183 | switch (CompletionContext.getKind()) { | |||
184 | case CodeCompletionContext::CCC_Expression: | |||
185 | case CodeCompletionContext::CCC_ObjCMessageReceiver: | |||
186 | case CodeCompletionContext::CCC_ParenthesizedExpression: | |||
187 | case CodeCompletionContext::CCC_Statement: | |||
188 | case CodeCompletionContext::CCC_Recovery: | |||
189 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) | |||
190 | if (Method->isInstanceMethod()) | |||
191 | if (ObjCInterfaceDecl *Interface = Method->getClassInterface()) | |||
192 | ObjCImplementation = Interface->getImplementation(); | |||
193 | break; | |||
194 | ||||
195 | default: | |||
196 | break; | |||
197 | } | |||
198 | } | |||
199 | ||||
200 | /// \brief Determine the priority for a reference to the given declaration. | |||
201 | unsigned getBasePriority(const NamedDecl *D); | |||
202 | ||||
203 | /// \brief Whether we should include code patterns in the completion | |||
204 | /// results. | |||
205 | bool includeCodePatterns() const { | |||
206 | return SemaRef.CodeCompleter && | |||
207 | SemaRef.CodeCompleter->includeCodePatterns(); | |||
208 | } | |||
209 | ||||
210 | /// \brief Set the filter used for code-completion results. | |||
211 | void setFilter(LookupFilter Filter) { | |||
212 | this->Filter = Filter; | |||
213 | } | |||
214 | ||||
215 | Result *data() { return Results.empty()? nullptr : &Results.front(); } | |||
216 | unsigned size() const { return Results.size(); } | |||
217 | bool empty() const { return Results.empty(); } | |||
218 | ||||
219 | /// \brief Specify the preferred type. | |||
220 | void setPreferredType(QualType T) { | |||
221 | PreferredType = SemaRef.Context.getCanonicalType(T); | |||
222 | } | |||
223 | ||||
224 | /// \brief Set the cv-qualifiers on the object type, for us in filtering | |||
225 | /// calls to member functions. | |||
226 | /// | |||
227 | /// When there are qualifiers in this set, they will be used to filter | |||
228 | /// out member functions that aren't available (because there will be a | |||
229 | /// cv-qualifier mismatch) or prefer functions with an exact qualifier | |||
230 | /// match. | |||
231 | void setObjectTypeQualifiers(Qualifiers Quals) { | |||
232 | ObjectTypeQualifiers = Quals; | |||
233 | HasObjectTypeQualifiers = true; | |||
234 | } | |||
235 | ||||
236 | /// \brief Set the preferred selector. | |||
237 | /// | |||
238 | /// When an Objective-C method declaration result is added, and that | |||
239 | /// method's selector matches this preferred selector, we give that method | |||
240 | /// a slight priority boost. | |||
241 | void setPreferredSelector(Selector Sel) { | |||
242 | PreferredSelector = Sel; | |||
243 | } | |||
244 | ||||
245 | /// \brief Retrieve the code-completion context for which results are | |||
246 | /// being collected. | |||
247 | const CodeCompletionContext &getCompletionContext() const { | |||
248 | return CompletionContext; | |||
249 | } | |||
250 | ||||
251 | /// \brief Specify whether nested-name-specifiers are allowed. | |||
252 | void allowNestedNameSpecifiers(bool Allow = true) { | |||
253 | AllowNestedNameSpecifiers = Allow; | |||
254 | } | |||
255 | ||||
256 | /// \brief Return the semantic analysis object for which we are collecting | |||
257 | /// code completion results. | |||
258 | Sema &getSema() const { return SemaRef; } | |||
259 | ||||
260 | /// \brief Retrieve the allocator used to allocate code completion strings. | |||
261 | CodeCompletionAllocator &getAllocator() const { return Allocator; } | |||
262 | ||||
263 | CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; } | |||
264 | ||||
265 | /// \brief Determine whether the given declaration is at all interesting | |||
266 | /// as a code-completion result. | |||
267 | /// | |||
268 | /// \param ND the declaration that we are inspecting. | |||
269 | /// | |||
270 | /// \param AsNestedNameSpecifier will be set true if this declaration is | |||
271 | /// only interesting when it is a nested-name-specifier. | |||
272 | bool isInterestingDecl(const NamedDecl *ND, | |||
273 | bool &AsNestedNameSpecifier) const; | |||
274 | ||||
275 | /// \brief Check whether the result is hidden by the Hiding declaration. | |||
276 | /// | |||
277 | /// \returns true if the result is hidden and cannot be found, false if | |||
278 | /// the hidden result could still be found. When false, \p R may be | |||
279 | /// modified to describe how the result can be found (e.g., via extra | |||
280 | /// qualification). | |||
281 | bool CheckHiddenResult(Result &R, DeclContext *CurContext, | |||
282 | const NamedDecl *Hiding); | |||
283 | ||||
284 | /// \brief Add a new result to this result set (if it isn't already in one | |||
285 | /// of the shadow maps), or replace an existing result (for, e.g., a | |||
286 | /// redeclaration). | |||
287 | /// | |||
288 | /// \param R the result to add (if it is unique). | |||
289 | /// | |||
290 | /// \param CurContext the context in which this result will be named. | |||
291 | void MaybeAddResult(Result R, DeclContext *CurContext = nullptr); | |||
292 | ||||
293 | /// \brief Add a new result to this result set, where we already know | |||
294 | /// the hiding declaration (if any). | |||
295 | /// | |||
296 | /// \param R the result to add (if it is unique). | |||
297 | /// | |||
298 | /// \param CurContext the context in which this result will be named. | |||
299 | /// | |||
300 | /// \param Hiding the declaration that hides the result. | |||
301 | /// | |||
302 | /// \param InBaseClass whether the result was found in a base | |||
303 | /// class of the searched context. | |||
304 | void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding, | |||
305 | bool InBaseClass); | |||
306 | ||||
307 | /// \brief Add a new non-declaration result to this result set. | |||
308 | void AddResult(Result R); | |||
309 | ||||
310 | /// \brief Enter into a new scope. | |||
311 | void EnterNewScope(); | |||
312 | ||||
313 | /// \brief Exit from the current scope. | |||
314 | void ExitScope(); | |||
315 | ||||
316 | /// \brief Ignore this declaration, if it is seen again. | |||
317 | void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); } | |||
318 | ||||
319 | /// \name Name lookup predicates | |||
320 | /// | |||
321 | /// These predicates can be passed to the name lookup functions to filter the | |||
322 | /// results of name lookup. All of the predicates have the same type, so that | |||
323 | /// | |||
324 | //@{ | |||
325 | bool IsOrdinaryName(const NamedDecl *ND) const; | |||
326 | bool IsOrdinaryNonTypeName(const NamedDecl *ND) const; | |||
327 | bool IsIntegralConstantValue(const NamedDecl *ND) const; | |||
328 | bool IsOrdinaryNonValueName(const NamedDecl *ND) const; | |||
329 | bool IsNestedNameSpecifier(const NamedDecl *ND) const; | |||
330 | bool IsEnum(const NamedDecl *ND) const; | |||
331 | bool IsClassOrStruct(const NamedDecl *ND) const; | |||
332 | bool IsUnion(const NamedDecl *ND) const; | |||
333 | bool IsNamespace(const NamedDecl *ND) const; | |||
334 | bool IsNamespaceOrAlias(const NamedDecl *ND) const; | |||
335 | bool IsType(const NamedDecl *ND) const; | |||
336 | bool IsMember(const NamedDecl *ND) const; | |||
337 | bool IsObjCIvar(const NamedDecl *ND) const; | |||
338 | bool IsObjCMessageReceiver(const NamedDecl *ND) const; | |||
339 | bool IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const; | |||
340 | bool IsObjCCollection(const NamedDecl *ND) const; | |||
341 | bool IsImpossibleToSatisfy(const NamedDecl *ND) const; | |||
342 | //@} | |||
343 | }; | |||
344 | } | |||
345 | ||||
346 | class ResultBuilder::ShadowMapEntry::iterator { | |||
347 | llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator; | |||
348 | unsigned SingleDeclIndex; | |||
349 | ||||
350 | public: | |||
351 | typedef DeclIndexPair value_type; | |||
352 | typedef value_type reference; | |||
353 | typedef std::ptrdiff_t difference_type; | |||
354 | typedef std::input_iterator_tag iterator_category; | |||
355 | ||||
356 | class pointer { | |||
357 | DeclIndexPair Value; | |||
358 | ||||
359 | public: | |||
360 | pointer(const DeclIndexPair &Value) : Value(Value) { } | |||
361 | ||||
362 | const DeclIndexPair *operator->() const { | |||
363 | return &Value; | |||
364 | } | |||
365 | }; | |||
366 | ||||
367 | iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {} | |||
368 | ||||
369 | iterator(const NamedDecl *SingleDecl, unsigned Index) | |||
370 | : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { } | |||
371 | ||||
372 | iterator(const DeclIndexPair *Iterator) | |||
373 | : DeclOrIterator(Iterator), SingleDeclIndex(0) { } | |||
374 | ||||
375 | iterator &operator++() { | |||
376 | if (DeclOrIterator.is<const NamedDecl *>()) { | |||
377 | DeclOrIterator = (NamedDecl *)nullptr; | |||
378 | SingleDeclIndex = 0; | |||
379 | return *this; | |||
380 | } | |||
381 | ||||
382 | const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>(); | |||
383 | ++I; | |||
384 | DeclOrIterator = I; | |||
385 | return *this; | |||
386 | } | |||
387 | ||||
388 | /*iterator operator++(int) { | |||
389 | iterator tmp(*this); | |||
390 | ++(*this); | |||
391 | return tmp; | |||
392 | }*/ | |||
393 | ||||
394 | reference operator*() const { | |||
395 | if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>()) | |||
396 | return reference(ND, SingleDeclIndex); | |||
397 | ||||
398 | return *DeclOrIterator.get<const DeclIndexPair*>(); | |||
399 | } | |||
400 | ||||
401 | pointer operator->() const { | |||
402 | return pointer(**this); | |||
403 | } | |||
404 | ||||
405 | friend bool operator==(const iterator &X, const iterator &Y) { | |||
406 | return X.DeclOrIterator.getOpaqueValue() | |||
407 | == Y.DeclOrIterator.getOpaqueValue() && | |||
408 | X.SingleDeclIndex == Y.SingleDeclIndex; | |||
409 | } | |||
410 | ||||
411 | friend bool operator!=(const iterator &X, const iterator &Y) { | |||
412 | return !(X == Y); | |||
413 | } | |||
414 | }; | |||
415 | ||||
416 | ResultBuilder::ShadowMapEntry::iterator | |||
417 | ResultBuilder::ShadowMapEntry::begin() const { | |||
418 | if (DeclOrVector.isNull()) | |||
419 | return iterator(); | |||
420 | ||||
421 | if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>()) | |||
422 | return iterator(ND, SingleDeclIndex); | |||
423 | ||||
424 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin()); | |||
425 | } | |||
426 | ||||
427 | ResultBuilder::ShadowMapEntry::iterator | |||
428 | ResultBuilder::ShadowMapEntry::end() const { | |||
429 | if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull()) | |||
430 | return iterator(); | |||
431 | ||||
432 | return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end()); | |||
433 | } | |||
434 | ||||
435 | /// \brief Compute the qualification required to get from the current context | |||
436 | /// (\p CurContext) to the target context (\p TargetContext). | |||
437 | /// | |||
438 | /// \param Context the AST context in which the qualification will be used. | |||
439 | /// | |||
440 | /// \param CurContext the context where an entity is being named, which is | |||
441 | /// typically based on the current scope. | |||
442 | /// | |||
443 | /// \param TargetContext the context in which the named entity actually | |||
444 | /// resides. | |||
445 | /// | |||
446 | /// \returns a nested name specifier that refers into the target context, or | |||
447 | /// NULL if no qualification is needed. | |||
448 | static NestedNameSpecifier * | |||
449 | getRequiredQualification(ASTContext &Context, | |||
450 | const DeclContext *CurContext, | |||
451 | const DeclContext *TargetContext) { | |||
452 | SmallVector<const DeclContext *, 4> TargetParents; | |||
453 | ||||
454 | for (const DeclContext *CommonAncestor = TargetContext; | |||
455 | CommonAncestor && !CommonAncestor->Encloses(CurContext); | |||
456 | CommonAncestor = CommonAncestor->getLookupParent()) { | |||
457 | if (CommonAncestor->isTransparentContext() || | |||
458 | CommonAncestor->isFunctionOrMethod()) | |||
459 | continue; | |||
460 | ||||
461 | TargetParents.push_back(CommonAncestor); | |||
462 | } | |||
463 | ||||
464 | NestedNameSpecifier *Result = nullptr; | |||
465 | while (!TargetParents.empty()) { | |||
466 | const DeclContext *Parent = TargetParents.pop_back_val(); | |||
467 | ||||
468 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) { | |||
469 | if (!Namespace->getIdentifier()) | |||
470 | continue; | |||
471 | ||||
472 | Result = NestedNameSpecifier::Create(Context, Result, Namespace); | |||
473 | } | |||
474 | else if (const TagDecl *TD = dyn_cast<TagDecl>(Parent)) | |||
475 | Result = NestedNameSpecifier::Create(Context, Result, | |||
476 | false, | |||
477 | Context.getTypeDeclType(TD).getTypePtr()); | |||
478 | } | |||
479 | return Result; | |||
480 | } | |||
481 | ||||
482 | /// Determine whether \p Id is a name reserved for the implementation (C99 | |||
483 | /// 7.1.3, C++ [lib.global.names]). | |||
484 | static bool isReservedName(const IdentifierInfo *Id, | |||
485 | bool doubleUnderscoreOnly = false) { | |||
486 | if (Id->getLength() < 2) | |||
487 | return false; | |||
488 | const char *Name = Id->getNameStart(); | |||
489 | return Name[0] == '_' && | |||
490 | (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z' && | |||
491 | !doubleUnderscoreOnly)); | |||
492 | } | |||
493 | ||||
494 | // Some declarations have reserved names that we don't want to ever show. | |||
495 | // Filter out names reserved for the implementation if they come from a | |||
496 | // system header. | |||
497 | static bool shouldIgnoreDueToReservedName(const NamedDecl *ND, Sema &SemaRef) { | |||
498 | const IdentifierInfo *Id = ND->getIdentifier(); | |||
499 | if (!Id) | |||
500 | return false; | |||
501 | ||||
502 | // Ignore reserved names for compiler provided decls. | |||
503 | if (isReservedName(Id) && ND->getLocation().isInvalid()) | |||
504 | return true; | |||
505 | ||||
506 | // For system headers ignore only double-underscore names. | |||
507 | // This allows for system headers providing private symbols with a single | |||
508 | // underscore. | |||
509 | if (isReservedName(Id, /*doubleUnderscoreOnly=*/true) && | |||
510 | SemaRef.SourceMgr.isInSystemHeader( | |||
511 | SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))) | |||
512 | return true; | |||
513 | ||||
514 | return false; | |||
515 | } | |||
516 | ||||
517 | bool ResultBuilder::isInterestingDecl(const NamedDecl *ND, | |||
518 | bool &AsNestedNameSpecifier) const { | |||
519 | AsNestedNameSpecifier = false; | |||
520 | ||||
521 | auto *Named = ND; | |||
522 | ND = ND->getUnderlyingDecl(); | |||
523 | ||||
524 | // Skip unnamed entities. | |||
525 | if (!ND->getDeclName()) | |||
526 | return false; | |||
527 | ||||
528 | // Friend declarations and declarations introduced due to friends are never | |||
529 | // added as results. | |||
530 | if (ND->getFriendObjectKind() == Decl::FOK_Undeclared) | |||
531 | return false; | |||
532 | ||||
533 | // Class template (partial) specializations are never added as results. | |||
534 | if (isa<ClassTemplateSpecializationDecl>(ND) || | |||
535 | isa<ClassTemplatePartialSpecializationDecl>(ND)) | |||
536 | return false; | |||
537 | ||||
538 | // Using declarations themselves are never added as results. | |||
539 | if (isa<UsingDecl>(ND)) | |||
540 | return false; | |||
541 | ||||
542 | if (shouldIgnoreDueToReservedName(ND, SemaRef)) | |||
543 | return false; | |||
544 | ||||
545 | if (Filter == &ResultBuilder::IsNestedNameSpecifier || | |||
546 | (isa<NamespaceDecl>(ND) && | |||
547 | Filter != &ResultBuilder::IsNamespace && | |||
548 | Filter != &ResultBuilder::IsNamespaceOrAlias && | |||
549 | Filter != nullptr)) | |||
550 | AsNestedNameSpecifier = true; | |||
551 | ||||
552 | // Filter out any unwanted results. | |||
553 | if (Filter && !(this->*Filter)(Named)) { | |||
554 | // Check whether it is interesting as a nested-name-specifier. | |||
555 | if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus && | |||
556 | IsNestedNameSpecifier(ND) && | |||
557 | (Filter != &ResultBuilder::IsMember || | |||
558 | (isa<CXXRecordDecl>(ND) && | |||
559 | cast<CXXRecordDecl>(ND)->isInjectedClassName()))) { | |||
560 | AsNestedNameSpecifier = true; | |||
561 | return true; | |||
562 | } | |||
563 | ||||
564 | return false; | |||
565 | } | |||
566 | // ... then it must be interesting! | |||
567 | return true; | |||
568 | } | |||
569 | ||||
570 | bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext, | |||
571 | const NamedDecl *Hiding) { | |||
572 | // In C, there is no way to refer to a hidden name. | |||
573 | // FIXME: This isn't true; we can find a tag name hidden by an ordinary | |||
574 | // name if we introduce the tag type. | |||
575 | if (!SemaRef.getLangOpts().CPlusPlus) | |||
576 | return true; | |||
577 | ||||
578 | const DeclContext *HiddenCtx = | |||
579 | R.Declaration->getDeclContext()->getRedeclContext(); | |||
580 | ||||
581 | // There is no way to qualify a name declared in a function or method. | |||
582 | if (HiddenCtx->isFunctionOrMethod()) | |||
583 | return true; | |||
584 | ||||
585 | if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext()) | |||
586 | return true; | |||
587 | ||||
588 | // We can refer to the result with the appropriate qualification. Do it. | |||
589 | R.Hidden = true; | |||
590 | R.QualifierIsInformative = false; | |||
591 | ||||
592 | if (!R.Qualifier) | |||
593 | R.Qualifier = getRequiredQualification(SemaRef.Context, | |||
594 | CurContext, | |||
595 | R.Declaration->getDeclContext()); | |||
596 | return false; | |||
597 | } | |||
598 | ||||
599 | /// \brief A simplified classification of types used to determine whether two | |||
600 | /// types are "similar enough" when adjusting priorities. | |||
601 | SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) { | |||
602 | switch (T->getTypeClass()) { | |||
603 | case Type::Builtin: | |||
604 | switch (cast<BuiltinType>(T)->getKind()) { | |||
605 | case BuiltinType::Void: | |||
606 | return STC_Void; | |||
607 | ||||
608 | case BuiltinType::NullPtr: | |||
609 | return STC_Pointer; | |||
610 | ||||
611 | case BuiltinType::Overload: | |||
612 | case BuiltinType::Dependent: | |||
613 | return STC_Other; | |||
614 | ||||
615 | case BuiltinType::ObjCId: | |||
616 | case BuiltinType::ObjCClass: | |||
617 | case BuiltinType::ObjCSel: | |||
618 | return STC_ObjectiveC; | |||
619 | ||||
620 | default: | |||
621 | return STC_Arithmetic; | |||
622 | } | |||
623 | ||||
624 | case Type::Complex: | |||
625 | return STC_Arithmetic; | |||
626 | ||||
627 | case Type::Pointer: | |||
628 | return STC_Pointer; | |||
629 | ||||
630 | case Type::BlockPointer: | |||
631 | return STC_Block; | |||
632 | ||||
633 | case Type::LValueReference: | |||
634 | case Type::RValueReference: | |||
635 | return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType()); | |||
636 | ||||
637 | case Type::ConstantArray: | |||
638 | case Type::IncompleteArray: | |||
639 | case Type::VariableArray: | |||
640 | case Type::DependentSizedArray: | |||
641 | return STC_Array; | |||
642 | ||||
643 | case Type::DependentSizedExtVector: | |||
644 | case Type::Vector: | |||
645 | case Type::ExtVector: | |||
646 | return STC_Arithmetic; | |||
647 | ||||
648 | case Type::FunctionProto: | |||
649 | case Type::FunctionNoProto: | |||
650 | return STC_Function; | |||
651 | ||||
652 | case Type::Record: | |||
653 | return STC_Record; | |||
654 | ||||
655 | case Type::Enum: | |||
656 | return STC_Arithmetic; | |||
657 | ||||
658 | case Type::ObjCObject: | |||
659 | case Type::ObjCInterface: | |||
660 | case Type::ObjCObjectPointer: | |||
661 | return STC_ObjectiveC; | |||
662 | ||||
663 | default: | |||
664 | return STC_Other; | |||
665 | } | |||
666 | } | |||
667 | ||||
668 | /// \brief Get the type that a given expression will have if this declaration | |||
669 | /// is used as an expression in its "typical" code-completion form. | |||
670 | QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) { | |||
671 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); | |||
672 | ||||
673 | if (const TypeDecl *Type = dyn_cast<TypeDecl>(ND)) | |||
674 | return C.getTypeDeclType(Type); | |||
675 | if (const ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND)) | |||
676 | return C.getObjCInterfaceType(Iface); | |||
677 | ||||
678 | QualType T; | |||
679 | if (const FunctionDecl *Function = ND->getAsFunction()) | |||
680 | T = Function->getCallResultType(); | |||
681 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) | |||
682 | T = Method->getSendResultType(); | |||
683 | else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) | |||
684 | T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext())); | |||
685 | else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) | |||
686 | T = Property->getType(); | |||
687 | else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) | |||
688 | T = Value->getType(); | |||
689 | else | |||
690 | return QualType(); | |||
691 | ||||
692 | // Dig through references, function pointers, and block pointers to | |||
693 | // get down to the likely type of an expression when the entity is | |||
694 | // used. | |||
695 | do { | |||
696 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) { | |||
697 | T = Ref->getPointeeType(); | |||
698 | continue; | |||
699 | } | |||
700 | ||||
701 | if (const PointerType *Pointer = T->getAs<PointerType>()) { | |||
702 | if (Pointer->getPointeeType()->isFunctionType()) { | |||
703 | T = Pointer->getPointeeType(); | |||
704 | continue; | |||
705 | } | |||
706 | ||||
707 | break; | |||
708 | } | |||
709 | ||||
710 | if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) { | |||
711 | T = Block->getPointeeType(); | |||
712 | continue; | |||
713 | } | |||
714 | ||||
715 | if (const FunctionType *Function = T->getAs<FunctionType>()) { | |||
716 | T = Function->getReturnType(); | |||
717 | continue; | |||
718 | } | |||
719 | ||||
720 | break; | |||
721 | } while (true); | |||
722 | ||||
723 | return T; | |||
724 | } | |||
725 | ||||
726 | unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) { | |||
727 | if (!ND) | |||
728 | return CCP_Unlikely; | |||
729 | ||||
730 | // Context-based decisions. | |||
731 | const DeclContext *LexicalDC = ND->getLexicalDeclContext(); | |||
732 | if (LexicalDC->isFunctionOrMethod()) { | |||
733 | // _cmd is relatively rare | |||
734 | if (const ImplicitParamDecl *ImplicitParam = | |||
735 | dyn_cast<ImplicitParamDecl>(ND)) | |||
736 | if (ImplicitParam->getIdentifier() && | |||
737 | ImplicitParam->getIdentifier()->isStr("_cmd")) | |||
738 | return CCP_ObjC_cmd; | |||
739 | ||||
740 | return CCP_LocalDeclaration; | |||
741 | } | |||
742 | ||||
743 | const DeclContext *DC = ND->getDeclContext()->getRedeclContext(); | |||
744 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) | |||
745 | return CCP_MemberDeclaration; | |||
746 | ||||
747 | // Content-based decisions. | |||
748 | if (isa<EnumConstantDecl>(ND)) | |||
749 | return CCP_Constant; | |||
750 | ||||
751 | // Use CCP_Type for type declarations unless we're in a statement, Objective-C | |||
752 | // message receiver, or parenthesized expression context. There, it's as | |||
753 | // likely that the user will want to write a type as other declarations. | |||
754 | if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) && | |||
755 | !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement || | |||
756 | CompletionContext.getKind() | |||
757 | == CodeCompletionContext::CCC_ObjCMessageReceiver || | |||
758 | CompletionContext.getKind() | |||
759 | == CodeCompletionContext::CCC_ParenthesizedExpression)) | |||
760 | return CCP_Type; | |||
761 | ||||
762 | return CCP_Declaration; | |||
763 | } | |||
764 | ||||
765 | void ResultBuilder::AdjustResultPriorityForDecl(Result &R) { | |||
766 | // If this is an Objective-C method declaration whose selector matches our | |||
767 | // preferred selector, give it a priority boost. | |||
768 | if (!PreferredSelector.isNull()) | |||
769 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration)) | |||
770 | if (PreferredSelector == Method->getSelector()) | |||
771 | R.Priority += CCD_SelectorMatch; | |||
772 | ||||
773 | // If we have a preferred type, adjust the priority for results with exactly- | |||
774 | // matching or nearly-matching types. | |||
775 | if (!PreferredType.isNull()) { | |||
776 | QualType T = getDeclUsageType(SemaRef.Context, R.Declaration); | |||
777 | if (!T.isNull()) { | |||
778 | CanQualType TC = SemaRef.Context.getCanonicalType(T); | |||
779 | // Check for exactly-matching types (modulo qualifiers). | |||
780 | if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) | |||
781 | R.Priority /= CCF_ExactTypeMatch; | |||
782 | // Check for nearly-matching types, based on classification of each. | |||
783 | else if ((getSimplifiedTypeClass(PreferredType) | |||
784 | == getSimplifiedTypeClass(TC)) && | |||
785 | !(PreferredType->isEnumeralType() && TC->isEnumeralType())) | |||
786 | R.Priority /= CCF_SimilarTypeMatch; | |||
787 | } | |||
788 | } | |||
789 | } | |||
790 | ||||
791 | void ResultBuilder::MaybeAddConstructorResults(Result R) { | |||
792 | if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration || | |||
793 | !CompletionContext.wantConstructorResults()) | |||
794 | return; | |||
795 | ||||
796 | ASTContext &Context = SemaRef.Context; | |||
797 | const NamedDecl *D = R.Declaration; | |||
798 | const CXXRecordDecl *Record = nullptr; | |||
799 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D)) | |||
800 | Record = ClassTemplate->getTemplatedDecl(); | |||
801 | else if ((Record = dyn_cast<CXXRecordDecl>(D))) { | |||
802 | // Skip specializations and partial specializations. | |||
803 | if (isa<ClassTemplateSpecializationDecl>(Record)) | |||
804 | return; | |||
805 | } else { | |||
806 | // There are no constructors here. | |||
807 | return; | |||
808 | } | |||
809 | ||||
810 | Record = Record->getDefinition(); | |||
811 | if (!Record) | |||
812 | return; | |||
813 | ||||
814 | ||||
815 | QualType RecordTy = Context.getTypeDeclType(Record); | |||
816 | DeclarationName ConstructorName | |||
817 | = Context.DeclarationNames.getCXXConstructorName( | |||
818 | Context.getCanonicalType(RecordTy)); | |||
819 | DeclContext::lookup_result Ctors = Record->lookup(ConstructorName); | |||
820 | for (DeclContext::lookup_iterator I = Ctors.begin(), | |||
821 | E = Ctors.end(); | |||
822 | I != E; ++I) { | |||
823 | R.Declaration = *I; | |||
824 | R.CursorKind = getCursorKindForDecl(R.Declaration); | |||
825 | Results.push_back(R); | |||
826 | } | |||
827 | } | |||
828 | ||||
829 | void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) { | |||
830 | assert(!ShadowMaps.empty() && "Must enter into a results scope")((!ShadowMaps.empty() && "Must enter into a results scope" ) ? static_cast<void> (0) : __assert_fail ("!ShadowMaps.empty() && \"Must enter into a results scope\"" , "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/clang/lib/Sema/SemaCodeComplete.cpp" , 830, __PRETTY_FUNCTION__)); | |||
831 | ||||
832 | if (R.Kind != Result::RK_Declaration) { | |||
833 | // For non-declaration results, just add the result. | |||
834 | Results.push_back(R); | |||
835 | return; | |||
836 | } | |||
837 | ||||
838 | // Look through using declarations. | |||
839 | if (const UsingShadowDecl *Using = | |||
840 | dyn_cast<UsingShadowDecl>(R.Declaration)) { | |||
841 | MaybeAddResult(Result(Using->getTargetDecl(), | |||
842 | getBasePriority(Using->getTargetDecl()), | |||
843 | R.Qualifier), | |||
844 | CurContext); | |||
845 | return; | |||
846 | } | |||
847 | ||||
848 | const Decl *CanonDecl = R.Declaration->getCanonicalDecl(); | |||
849 | unsigned IDNS = CanonDecl->getIdentifierNamespace(); | |||
850 | ||||
851 | bool AsNestedNameSpecifier = false; | |||
852 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) | |||
853 | return; | |||
854 | ||||
855 | // C++ constructors are never found by name lookup. | |||
856 | if (isa<CXXConstructorDecl>(R.Declaration)) | |||
857 | return; | |||
858 | ||||
859 | ShadowMap &SMap = ShadowMaps.back(); | |||
860 | ShadowMapEntry::iterator I, IEnd; | |||
861 | ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName()); | |||
862 | if (NamePos != SMap.end()) { | |||
863 | I = NamePos->second.begin(); | |||
864 | IEnd = NamePos->second.end(); | |||
865 | } | |||
866 | ||||
867 | for (; I != IEnd; ++I) { | |||
868 | const NamedDecl *ND = I->first; | |||
869 | unsigned Index = I->second; | |||
870 | if (ND->getCanonicalDecl() == CanonDecl) { | |||
871 | // This is a redeclaration. Always pick the newer declaration. | |||
872 | Results[Index].Declaration = R.Declaration; | |||
873 | ||||
874 | // We're done. | |||
875 | return; | |||
876 | } | |||
877 | } | |||
878 | ||||
879 | // This is a new declaration in this scope. However, check whether this | |||
880 | // declaration name is hidden by a similarly-named declaration in an outer | |||
881 | // scope. | |||
882 | std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end(); | |||
883 | --SMEnd; | |||
884 | for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) { | |||
885 | ShadowMapEntry::iterator I, IEnd; | |||
886 | ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName()); | |||
887 | if (NamePos != SM->end()) { | |||
888 | I = NamePos->second.begin(); | |||
889 | IEnd = NamePos->second.end(); | |||
890 | } | |||
891 | for (; I != IEnd; ++I) { | |||
892 | // A tag declaration does not hide a non-tag declaration. | |||
893 | if (I->first->hasTagIdentifierNamespace() && | |||
894 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | | |||
895 | Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol))) | |||
896 | continue; | |||
897 | ||||
898 | // Protocols are in distinct namespaces from everything else. | |||
899 | if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) | |||
900 | || (IDNS & Decl::IDNS_ObjCProtocol)) && | |||
901 | I->first->getIdentifierNamespace() != IDNS) | |||
902 | continue; | |||
903 | ||||
904 | // The newly-added result is hidden by an entry in the shadow map. | |||
905 | if (CheckHiddenResult(R, CurContext, I->first)) | |||
906 | return; | |||
907 | ||||
908 | break; | |||
909 | } | |||
910 | } | |||
911 | ||||
912 | // Make sure that any given declaration only shows up in the result set once. | |||
913 | if (!AllDeclsFound.insert(CanonDecl).second) | |||
914 | return; | |||
915 | ||||
916 | // If the filter is for nested-name-specifiers, then this result starts a | |||
917 | // nested-name-specifier. | |||
918 | if (AsNestedNameSpecifier) { | |||
919 | R.StartsNestedNameSpecifier = true; | |||
920 | R.Priority = CCP_NestedNameSpecifier; | |||
921 | } else | |||
922 | AdjustResultPriorityForDecl(R); | |||
923 | ||||
924 | // If this result is supposed to have an informative qualifier, add one. | |||
925 | if (R.QualifierIsInformative && !R.Qualifier && | |||
926 | !R.StartsNestedNameSpecifier) { | |||
927 | const DeclContext *Ctx = R.Declaration->getDeclContext(); | |||
928 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) | |||
929 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, | |||
930 | Namespace); | |||
931 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) | |||
932 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, | |||
933 | false, SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); | |||
934 | else | |||
935 | R.QualifierIsInformative = false; | |||
936 | } | |||
937 | ||||
938 | // Insert this result into the set of results and into the current shadow | |||
939 | // map. | |||
940 | SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size()); | |||
941 | Results.push_back(R); | |||
942 | ||||
943 | if (!AsNestedNameSpecifier) | |||
944 | MaybeAddConstructorResults(R); | |||
945 | } | |||
946 | ||||
947 | void ResultBuilder::AddResult(Result R, DeclContext *CurContext, | |||
948 | NamedDecl *Hiding, bool InBaseClass = false) { | |||
949 | if (R.Kind != Result::RK_Declaration) { | |||
950 | // For non-declaration results, just add the result. | |||
951 | Results.push_back(R); | |||
952 | return; | |||
953 | } | |||
954 | ||||
955 | // Look through using declarations. | |||
956 | if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) { | |||
957 | AddResult(Result(Using->getTargetDecl(), | |||
958 | getBasePriority(Using->getTargetDecl()), | |||
959 | R.Qualifier), | |||
960 | CurContext, Hiding); | |||
961 | return; | |||
962 | } | |||
963 | ||||
964 | bool AsNestedNameSpecifier = false; | |||
965 | if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier)) | |||
966 | return; | |||
967 | ||||
968 | // C++ constructors are never found by name lookup. | |||
969 | if (isa<CXXConstructorDecl>(R.Declaration)) | |||
970 | return; | |||
971 | ||||
972 | if (Hiding && CheckHiddenResult(R, CurContext, Hiding)) | |||
973 | return; | |||
974 | ||||
975 | // Make sure that any given declaration only shows up in the result set once. | |||
976 | if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second) | |||
977 | return; | |||
978 | ||||
979 | // If the filter is for nested-name-specifiers, then this result starts a | |||
980 | // nested-name-specifier. | |||
981 | if (AsNestedNameSpecifier) { | |||
982 | R.StartsNestedNameSpecifier = true; | |||
983 | R.Priority = CCP_NestedNameSpecifier; | |||
984 | } | |||
985 | else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass && | |||
986 | isa<CXXRecordDecl>(R.Declaration->getDeclContext() | |||
987 | ->getRedeclContext())) | |||
988 | R.QualifierIsInformative = true; | |||
989 | ||||
990 | // If this result is supposed to have an informative qualifier, add one. | |||
991 | if (R.QualifierIsInformative && !R.Qualifier && | |||
992 | !R.StartsNestedNameSpecifier) { | |||
993 | const DeclContext *Ctx = R.Declaration->getDeclContext(); | |||
994 | if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx)) | |||
995 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, | |||
996 | Namespace); | |||
997 | else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx)) | |||
998 | R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, false, | |||
999 | SemaRef.Context.getTypeDeclType(Tag).getTypePtr()); | |||
1000 | else | |||
1001 | R.QualifierIsInformative = false; | |||
1002 | } | |||
1003 | ||||
1004 | // Adjust the priority if this result comes from a base class. | |||
1005 | if (InBaseClass) | |||
1006 | R.Priority += CCD_InBaseClass; | |||
1007 | ||||
1008 | AdjustResultPriorityForDecl(R); | |||
1009 | ||||
1010 | if (HasObjectTypeQualifiers) | |||
1011 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration)) | |||
1012 | if (Method->isInstance()) { | |||
1013 | Qualifiers MethodQuals | |||
1014 | = Qualifiers::fromCVRMask(Method->getTypeQualifiers()); | |||
1015 | if (ObjectTypeQualifiers == MethodQuals) | |||
1016 | R.Priority += CCD_ObjectQualifierMatch; | |||
1017 | else if (ObjectTypeQualifiers - MethodQuals) { | |||
1018 | // The method cannot be invoked, because doing so would drop | |||
1019 | // qualifiers. | |||
1020 | return; | |||
1021 | } | |||
1022 | } | |||
1023 | ||||
1024 | // Insert this result into the set of results. | |||
1025 | Results.push_back(R); | |||
1026 | ||||
1027 | if (!AsNestedNameSpecifier) | |||
1028 | MaybeAddConstructorResults(R); | |||
1029 | } | |||
1030 | ||||
1031 | void ResultBuilder::AddResult(Result R) { | |||
1032 | assert(R.Kind != Result::RK_Declaration &&((R.Kind != Result::RK_Declaration && "Declaration results need more context" ) ? static_cast<void> (0) : __assert_fail ("R.Kind != Result::RK_Declaration && \"Declaration results need more context\"" , "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/clang/lib/Sema/SemaCodeComplete.cpp" , 1033, __PRETTY_FUNCTION__)) | |||
1033 | "Declaration results need more context")((R.Kind != Result::RK_Declaration && "Declaration results need more context" ) ? static_cast<void> (0) : __assert_fail ("R.Kind != Result::RK_Declaration && \"Declaration results need more context\"" , "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/clang/lib/Sema/SemaCodeComplete.cpp" , 1033, __PRETTY_FUNCTION__)); | |||
1034 | Results.push_back(R); | |||
1035 | } | |||
1036 | ||||
1037 | /// \brief Enter into a new scope. | |||
1038 | void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); } | |||
1039 | ||||
1040 | /// \brief Exit from the current scope. | |||
1041 | void ResultBuilder::ExitScope() { | |||
1042 | for (ShadowMap::iterator E = ShadowMaps.back().begin(), | |||
1043 | EEnd = ShadowMaps.back().end(); | |||
1044 | E != EEnd; | |||
1045 | ++E) | |||
1046 | E->second.Destroy(); | |||
1047 | ||||
1048 | ShadowMaps.pop_back(); | |||
1049 | } | |||
1050 | ||||
1051 | /// \brief Determines whether this given declaration will be found by | |||
1052 | /// ordinary name lookup. | |||
1053 | bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const { | |||
1054 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); | |||
1055 | ||||
1056 | // If name lookup finds a local extern declaration, then we are in a | |||
1057 | // context where it behaves like an ordinary name. | |||
1058 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; | |||
1059 | if (SemaRef.getLangOpts().CPlusPlus) | |||
1060 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; | |||
1061 | else if (SemaRef.getLangOpts().ObjC1) { | |||
1062 | if (isa<ObjCIvarDecl>(ND)) | |||
1063 | return true; | |||
1064 | } | |||
1065 | ||||
1066 | return ND->getIdentifierNamespace() & IDNS; | |||
1067 | } | |||
1068 | ||||
1069 | /// \brief Determines whether this given declaration will be found by | |||
1070 | /// ordinary name lookup but is not a type name. | |||
1071 | bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const { | |||
1072 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); | |||
1073 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) | |||
1074 | return false; | |||
1075 | ||||
1076 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; | |||
1077 | if (SemaRef.getLangOpts().CPlusPlus) | |||
1078 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member; | |||
1079 | else if (SemaRef.getLangOpts().ObjC1) { | |||
1080 | if (isa<ObjCIvarDecl>(ND)) | |||
1081 | return true; | |||
1082 | } | |||
1083 | ||||
1084 | return ND->getIdentifierNamespace() & IDNS; | |||
1085 | } | |||
1086 | ||||
1087 | bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const { | |||
1088 | if (!IsOrdinaryNonTypeName(ND)) | |||
1089 | return 0; | |||
1090 | ||||
1091 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl())) | |||
1092 | if (VD->getType()->isIntegralOrEnumerationType()) | |||
1093 | return true; | |||
1094 | ||||
1095 | return false; | |||
1096 | } | |||
1097 | ||||
1098 | /// \brief Determines whether this given declaration will be found by | |||
1099 | /// ordinary name lookup. | |||
1100 | bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const { | |||
1101 | ND = cast<NamedDecl>(ND->getUnderlyingDecl()); | |||
1102 | ||||
1103 | unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern; | |||
1104 | if (SemaRef.getLangOpts().CPlusPlus) | |||
1105 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace; | |||
1106 | ||||
1107 | return (ND->getIdentifierNamespace() & IDNS) && | |||
1108 | !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) && | |||
1109 | !isa<ObjCPropertyDecl>(ND); | |||
1110 | } | |||
1111 | ||||
1112 | /// \brief Determines whether the given declaration is suitable as the | |||
1113 | /// start of a C++ nested-name-specifier, e.g., a class or namespace. | |||
1114 | bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const { | |||
1115 | // Allow us to find class templates, too. | |||
1116 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) | |||
1117 | ND = ClassTemplate->getTemplatedDecl(); | |||
1118 | ||||
1119 | return SemaRef.isAcceptableNestedNameSpecifier(ND); | |||
1120 | } | |||
1121 | ||||
1122 | /// \brief Determines whether the given declaration is an enumeration. | |||
1123 | bool ResultBuilder::IsEnum(const NamedDecl *ND) const { | |||
1124 | return isa<EnumDecl>(ND); | |||
1125 | } | |||
1126 | ||||
1127 | /// \brief Determines whether the given declaration is a class or struct. | |||
1128 | bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const { | |||
1129 | // Allow us to find class templates, too. | |||
1130 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) | |||
1131 | ND = ClassTemplate->getTemplatedDecl(); | |||
1132 | ||||
1133 | // For purposes of this check, interfaces match too. | |||
1134 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) | |||
1135 | return RD->getTagKind() == TTK_Class || | |||
1136 | RD->getTagKind() == TTK_Struct || | |||
1137 | RD->getTagKind() == TTK_Interface; | |||
1138 | ||||
1139 | return false; | |||
1140 | } | |||
1141 | ||||
1142 | /// \brief Determines whether the given declaration is a union. | |||
1143 | bool ResultBuilder::IsUnion(const NamedDecl *ND) const { | |||
1144 | // Allow us to find class templates, too. | |||
1145 | if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND)) | |||
1146 | ND = ClassTemplate->getTemplatedDecl(); | |||
1147 | ||||
1148 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) | |||
1149 | return RD->getTagKind() == TTK_Union; | |||
1150 | ||||
1151 | return false; | |||
1152 | } | |||
1153 | ||||
1154 | /// \brief Determines whether the given declaration is a namespace. | |||
1155 | bool ResultBuilder::IsNamespace(const NamedDecl *ND) const { | |||
1156 | return isa<NamespaceDecl>(ND); | |||
1157 | } | |||
1158 | ||||
1159 | /// \brief Determines whether the given declaration is a namespace or | |||
1160 | /// namespace alias. | |||
1161 | bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const { | |||
1162 | return isa<NamespaceDecl>(ND->getUnderlyingDecl()); | |||
1163 | } | |||
1164 | ||||
1165 | /// \brief Determines whether the given declaration is a type. | |||
1166 | bool ResultBuilder::IsType(const NamedDecl *ND) const { | |||
1167 | ND = ND->getUnderlyingDecl(); | |||
1168 | return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND); | |||
1169 | } | |||
1170 | ||||
1171 | /// \brief Determines which members of a class should be visible via | |||
1172 | /// "." or "->". Only value declarations, nested name specifiers, and | |||
1173 | /// using declarations thereof should show up. | |||
1174 | bool ResultBuilder::IsMember(const NamedDecl *ND) const { | |||
1175 | ND = ND->getUnderlyingDecl(); | |||
1176 | return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) || | |||
1177 | isa<ObjCPropertyDecl>(ND); | |||
1178 | } | |||
1179 | ||||
1180 | static bool isObjCReceiverType(ASTContext &C, QualType T) { | |||
1181 | T = C.getCanonicalType(T); | |||
1182 | switch (T->getTypeClass()) { | |||
1183 | case Type::ObjCObject: | |||
1184 | case Type::ObjCInterface: | |||
1185 | case Type::ObjCObjectPointer: | |||
1186 | return true; | |||
1187 | ||||
1188 | case Type::Builtin: | |||
1189 | switch (cast<BuiltinType>(T)->getKind()) { | |||
1190 | case BuiltinType::ObjCId: | |||
1191 | case BuiltinType::ObjCClass: | |||
1192 | case BuiltinType::ObjCSel: | |||
1193 | return true; | |||
1194 | ||||
1195 | default: | |||
1196 | break; | |||
1197 | } | |||
1198 | return false; | |||
1199 | ||||
1200 | default: | |||
1201 | break; | |||
1202 | } | |||
1203 | ||||
1204 | if (!C.getLangOpts().CPlusPlus) | |||
1205 | return false; | |||
1206 | ||||
1207 | // FIXME: We could perform more analysis here to determine whether a | |||
1208 | // particular class type has any conversions to Objective-C types. For now, | |||
1209 | // just accept all class types. | |||
1210 | return T->isDependentType() || T->isRecordType(); | |||
1211 | } | |||
1212 | ||||
1213 | bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const { | |||
1214 | QualType T = getDeclUsageType(SemaRef.Context, ND); | |||
1215 | if (T.isNull()) | |||
1216 | return false; | |||
1217 | ||||
1218 | T = SemaRef.Context.getBaseElementType(T); | |||
1219 | return isObjCReceiverType(SemaRef.Context, T); | |||
1220 | } | |||
1221 | ||||
1222 | bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const { | |||
1223 | if (IsObjCMessageReceiver(ND)) | |||
1224 | return true; | |||
1225 | ||||
1226 | const VarDecl *Var = dyn_cast<VarDecl>(ND); | |||
1227 | if (!Var) | |||
1228 | return false; | |||
1229 | ||||
1230 | return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>(); | |||
1231 | } | |||
1232 | ||||
1233 | bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const { | |||
1234 | if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) || | |||
1235 | (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND))) | |||
1236 | return false; | |||
1237 | ||||
1238 | QualType T = getDeclUsageType(SemaRef.Context, ND); | |||
1239 | if (T.isNull()) | |||
1240 | return false; | |||
1241 | ||||
1242 | T = SemaRef.Context.getBaseElementType(T); | |||
1243 | return T->isObjCObjectType() || T->isObjCObjectPointerType() || | |||
1244 | T->isObjCIdType() || | |||
1245 | (SemaRef.getLangOpts().CPlusPlus && T->isRecordType()); | |||
1246 | } | |||
1247 | ||||
1248 | bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const { | |||
1249 | return false; | |||
1250 | } | |||
1251 | ||||
1252 | /// \brief Determines whether the given declaration is an Objective-C | |||
1253 | /// instance variable. | |||
1254 | bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const { | |||
1255 | return isa<ObjCIvarDecl>(ND); | |||
1256 | } | |||
1257 | ||||
1258 | namespace { | |||
1259 | /// \brief Visible declaration consumer that adds a code-completion result | |||
1260 | /// for each visible declaration. | |||
1261 | class CodeCompletionDeclConsumer : public VisibleDeclConsumer { | |||
1262 | ResultBuilder &Results; | |||
1263 | DeclContext *CurContext; | |||
1264 | ||||
1265 | public: | |||
1266 | CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext) | |||
1267 | : Results(Results), CurContext(CurContext) { } | |||
1268 | ||||
1269 | void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx, | |||
1270 | bool InBaseClass) override { | |||
1271 | bool Accessible = true; | |||
1272 | if (Ctx) | |||
1273 | Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx); | |||
1274 | ||||
1275 | ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr, | |||
1276 | false, Accessible); | |||
1277 | Results.AddResult(Result, CurContext, Hiding, InBaseClass); | |||
1278 | } | |||
1279 | }; | |||
1280 | } | |||
1281 | ||||
1282 | /// \brief Add type specifiers for the current language as keyword results. | |||
1283 | static void AddTypeSpecifierResults(const LangOptions &LangOpts, | |||
1284 | ResultBuilder &Results) { | |||
1285 | typedef CodeCompletionResult Result; | |||
1286 | Results.AddResult(Result("short", CCP_Type)); | |||
1287 | Results.AddResult(Result("long", CCP_Type)); | |||
1288 | Results.AddResult(Result("signed", CCP_Type)); | |||
1289 | Results.AddResult(Result("unsigned", CCP_Type)); | |||
1290 | Results.AddResult(Result("void", CCP_Type)); | |||
1291 | Results.AddResult(Result("char", CCP_Type)); | |||
1292 | Results.AddResult(Result("int", CCP_Type)); | |||
1293 | Results.AddResult(Result("float", CCP_Type)); | |||
1294 | Results.AddResult(Result("double", CCP_Type)); | |||
1295 | Results.AddResult(Result("enum", CCP_Type)); | |||
1296 | Results.AddResult(Result("struct", CCP_Type)); | |||
1297 | Results.AddResult(Result("union", CCP_Type)); | |||
1298 | Results.AddResult(Result("const", CCP_Type)); | |||
1299 | Results.AddResult(Result("volatile", CCP_Type)); | |||
1300 | ||||
1301 | if (LangOpts.C99) { | |||
1302 | // C99-specific | |||
1303 | Results.AddResult(Result("_Complex", CCP_Type)); | |||
1304 | Results.AddResult(Result("_Imaginary", CCP_Type)); | |||
1305 | Results.AddResult(Result("_Bool", CCP_Type)); | |||
1306 | Results.AddResult(Result("restrict", CCP_Type)); | |||
1307 | } | |||
1308 | ||||
1309 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
1310 | Results.getCodeCompletionTUInfo()); | |||
1311 | if (LangOpts.CPlusPlus) { | |||
1312 | // C++-specific | |||
1313 | Results.AddResult(Result("bool", CCP_Type + | |||
1314 | (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); | |||
1315 | Results.AddResult(Result("class", CCP_Type)); | |||
1316 | Results.AddResult(Result("wchar_t", CCP_Type)); | |||
1317 | ||||
1318 | // typename qualified-id | |||
1319 | Builder.AddTypedTextChunk("typename"); | |||
1320 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1321 | Builder.AddPlaceholderChunk("qualifier"); | |||
1322 | Builder.AddTextChunk("::"); | |||
1323 | Builder.AddPlaceholderChunk("name"); | |||
1324 | Results.AddResult(Result(Builder.TakeString())); | |||
1325 | ||||
1326 | if (LangOpts.CPlusPlus11) { | |||
1327 | Results.AddResult(Result("auto", CCP_Type)); | |||
1328 | Results.AddResult(Result("char16_t", CCP_Type)); | |||
1329 | Results.AddResult(Result("char32_t", CCP_Type)); | |||
1330 | ||||
1331 | Builder.AddTypedTextChunk("decltype"); | |||
1332 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1333 | Builder.AddPlaceholderChunk("expression"); | |||
1334 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1335 | Results.AddResult(Result(Builder.TakeString())); | |||
1336 | } | |||
1337 | } else | |||
1338 | Results.AddResult(Result("__auto_type", CCP_Type)); | |||
1339 | ||||
1340 | // GNU extensions | |||
1341 | if (LangOpts.GNUMode) { | |||
1342 | // FIXME: Enable when we actually support decimal floating point. | |||
1343 | // Results.AddResult(Result("_Decimal32")); | |||
1344 | // Results.AddResult(Result("_Decimal64")); | |||
1345 | // Results.AddResult(Result("_Decimal128")); | |||
1346 | ||||
1347 | Builder.AddTypedTextChunk("typeof"); | |||
1348 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1349 | Builder.AddPlaceholderChunk("expression"); | |||
1350 | Results.AddResult(Result(Builder.TakeString())); | |||
1351 | ||||
1352 | Builder.AddTypedTextChunk("typeof"); | |||
1353 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1354 | Builder.AddPlaceholderChunk("type"); | |||
1355 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1356 | Results.AddResult(Result(Builder.TakeString())); | |||
1357 | } | |||
1358 | ||||
1359 | // Nullability | |||
1360 | Results.AddResult(Result("_Nonnull", CCP_Type)); | |||
1361 | Results.AddResult(Result("_Null_unspecified", CCP_Type)); | |||
1362 | Results.AddResult(Result("_Nullable", CCP_Type)); | |||
1363 | } | |||
1364 | ||||
1365 | static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC, | |||
1366 | const LangOptions &LangOpts, | |||
1367 | ResultBuilder &Results) { | |||
1368 | typedef CodeCompletionResult Result; | |||
1369 | // Note: we don't suggest either "auto" or "register", because both | |||
1370 | // are pointless as storage specifiers. Elsewhere, we suggest "auto" | |||
1371 | // in C++0x as a type specifier. | |||
1372 | Results.AddResult(Result("extern")); | |||
1373 | Results.AddResult(Result("static")); | |||
1374 | ||||
1375 | if (LangOpts.CPlusPlus11) { | |||
1376 | CodeCompletionAllocator &Allocator = Results.getAllocator(); | |||
1377 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); | |||
1378 | ||||
1379 | // alignas | |||
1380 | Builder.AddTypedTextChunk("alignas"); | |||
1381 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1382 | Builder.AddPlaceholderChunk("expression"); | |||
1383 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1384 | Results.AddResult(Result(Builder.TakeString())); | |||
1385 | ||||
1386 | Results.AddResult(Result("constexpr")); | |||
1387 | Results.AddResult(Result("thread_local")); | |||
1388 | } | |||
1389 | } | |||
1390 | ||||
1391 | static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, | |||
1392 | const LangOptions &LangOpts, | |||
1393 | ResultBuilder &Results) { | |||
1394 | typedef CodeCompletionResult Result; | |||
1395 | switch (CCC) { | |||
1396 | case Sema::PCC_Class: | |||
1397 | case Sema::PCC_MemberTemplate: | |||
1398 | if (LangOpts.CPlusPlus) { | |||
1399 | Results.AddResult(Result("explicit")); | |||
1400 | Results.AddResult(Result("friend")); | |||
1401 | Results.AddResult(Result("mutable")); | |||
1402 | Results.AddResult(Result("virtual")); | |||
1403 | } | |||
1404 | // Fall through | |||
1405 | ||||
1406 | case Sema::PCC_ObjCInterface: | |||
1407 | case Sema::PCC_ObjCImplementation: | |||
1408 | case Sema::PCC_Namespace: | |||
1409 | case Sema::PCC_Template: | |||
1410 | if (LangOpts.CPlusPlus || LangOpts.C99) | |||
1411 | Results.AddResult(Result("inline")); | |||
1412 | break; | |||
1413 | ||||
1414 | case Sema::PCC_ObjCInstanceVariableList: | |||
1415 | case Sema::PCC_Expression: | |||
1416 | case Sema::PCC_Statement: | |||
1417 | case Sema::PCC_ForInit: | |||
1418 | case Sema::PCC_Condition: | |||
1419 | case Sema::PCC_RecoveryInFunction: | |||
1420 | case Sema::PCC_Type: | |||
1421 | case Sema::PCC_ParenthesizedExpression: | |||
1422 | case Sema::PCC_LocalDeclarationSpecifiers: | |||
1423 | break; | |||
1424 | } | |||
1425 | } | |||
1426 | ||||
1427 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt); | |||
1428 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt); | |||
1429 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, | |||
1430 | ResultBuilder &Results, | |||
1431 | bool NeedAt); | |||
1432 | static void AddObjCImplementationResults(const LangOptions &LangOpts, | |||
1433 | ResultBuilder &Results, | |||
1434 | bool NeedAt); | |||
1435 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, | |||
1436 | ResultBuilder &Results, | |||
1437 | bool NeedAt); | |||
1438 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt); | |||
1439 | ||||
1440 | static void AddTypedefResult(ResultBuilder &Results) { | |||
1441 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
1442 | Results.getCodeCompletionTUInfo()); | |||
1443 | Builder.AddTypedTextChunk("typedef"); | |||
1444 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1445 | Builder.AddPlaceholderChunk("type"); | |||
1446 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1447 | Builder.AddPlaceholderChunk("name"); | |||
1448 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); | |||
1449 | } | |||
1450 | ||||
1451 | static bool WantTypesInContext(Sema::ParserCompletionContext CCC, | |||
1452 | const LangOptions &LangOpts) { | |||
1453 | switch (CCC) { | |||
1454 | case Sema::PCC_Namespace: | |||
1455 | case Sema::PCC_Class: | |||
1456 | case Sema::PCC_ObjCInstanceVariableList: | |||
1457 | case Sema::PCC_Template: | |||
1458 | case Sema::PCC_MemberTemplate: | |||
1459 | case Sema::PCC_Statement: | |||
1460 | case Sema::PCC_RecoveryInFunction: | |||
1461 | case Sema::PCC_Type: | |||
1462 | case Sema::PCC_ParenthesizedExpression: | |||
1463 | case Sema::PCC_LocalDeclarationSpecifiers: | |||
1464 | return true; | |||
1465 | ||||
1466 | case Sema::PCC_Expression: | |||
1467 | case Sema::PCC_Condition: | |||
1468 | return LangOpts.CPlusPlus; | |||
1469 | ||||
1470 | case Sema::PCC_ObjCInterface: | |||
1471 | case Sema::PCC_ObjCImplementation: | |||
1472 | return false; | |||
1473 | ||||
1474 | case Sema::PCC_ForInit: | |||
1475 | return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99; | |||
1476 | } | |||
1477 | ||||
1478 | llvm_unreachable("Invalid ParserCompletionContext!")::llvm::llvm_unreachable_internal("Invalid ParserCompletionContext!" , "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/clang/lib/Sema/SemaCodeComplete.cpp" , 1478); | |||
1479 | } | |||
1480 | ||||
1481 | static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context, | |||
1482 | const Preprocessor &PP) { | |||
1483 | PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP); | |||
1484 | Policy.AnonymousTagLocations = false; | |||
1485 | Policy.SuppressStrongLifetime = true; | |||
1486 | Policy.SuppressUnwrittenScope = true; | |||
1487 | return Policy; | |||
1488 | } | |||
1489 | ||||
1490 | /// \brief Retrieve a printing policy suitable for code completion. | |||
1491 | static PrintingPolicy getCompletionPrintingPolicy(Sema &S) { | |||
1492 | return getCompletionPrintingPolicy(S.Context, S.PP); | |||
1493 | } | |||
1494 | ||||
1495 | /// \brief Retrieve the string representation of the given type as a string | |||
1496 | /// that has the appropriate lifetime for code completion. | |||
1497 | /// | |||
1498 | /// This routine provides a fast path where we provide constant strings for | |||
1499 | /// common type names. | |||
1500 | static const char *GetCompletionTypeString(QualType T, | |||
1501 | ASTContext &Context, | |||
1502 | const PrintingPolicy &Policy, | |||
1503 | CodeCompletionAllocator &Allocator) { | |||
1504 | if (!T.getLocalQualifiers()) { | |||
1505 | // Built-in type names are constant strings. | |||
1506 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(T)) | |||
1507 | return BT->getNameAsCString(Policy); | |||
1508 | ||||
1509 | // Anonymous tag types are constant strings. | |||
1510 | if (const TagType *TagT = dyn_cast<TagType>(T)) | |||
1511 | if (TagDecl *Tag = TagT->getDecl()) | |||
1512 | if (!Tag->hasNameForLinkage()) { | |||
1513 | switch (Tag->getTagKind()) { | |||
1514 | case TTK_Struct: return "struct <anonymous>"; | |||
1515 | case TTK_Interface: return "__interface <anonymous>"; | |||
1516 | case TTK_Class: return "class <anonymous>"; | |||
1517 | case TTK_Union: return "union <anonymous>"; | |||
1518 | case TTK_Enum: return "enum <anonymous>"; | |||
1519 | } | |||
1520 | } | |||
1521 | } | |||
1522 | ||||
1523 | // Slow path: format the type as a string. | |||
1524 | std::string Result; | |||
1525 | T.getAsStringInternal(Result, Policy); | |||
1526 | return Allocator.CopyString(Result); | |||
1527 | } | |||
1528 | ||||
1529 | /// \brief Add a completion for "this", if we're in a member function. | |||
1530 | static void addThisCompletion(Sema &S, ResultBuilder &Results) { | |||
1531 | QualType ThisTy = S.getCurrentThisType(); | |||
1532 | if (ThisTy.isNull()) | |||
1533 | return; | |||
1534 | ||||
1535 | CodeCompletionAllocator &Allocator = Results.getAllocator(); | |||
1536 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); | |||
1537 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); | |||
1538 | Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy, | |||
1539 | S.Context, | |||
1540 | Policy, | |||
1541 | Allocator)); | |||
1542 | Builder.AddTypedTextChunk("this"); | |||
1543 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); | |||
1544 | } | |||
1545 | ||||
1546 | static void AddStaticAssertResult(CodeCompletionBuilder &Builder, | |||
1547 | ResultBuilder &Results, | |||
1548 | const LangOptions &LangOpts) { | |||
1549 | if (!LangOpts.CPlusPlus11) | |||
1550 | return; | |||
1551 | ||||
1552 | Builder.AddTypedTextChunk("static_assert"); | |||
1553 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1554 | Builder.AddPlaceholderChunk("expression"); | |||
1555 | Builder.AddChunk(CodeCompletionString::CK_Comma); | |||
1556 | Builder.AddPlaceholderChunk("message"); | |||
1557 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1558 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); | |||
1559 | } | |||
1560 | ||||
1561 | /// \brief Add language constructs that show up for "ordinary" names. | |||
1562 | static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, | |||
1563 | Scope *S, | |||
1564 | Sema &SemaRef, | |||
1565 | ResultBuilder &Results) { | |||
1566 | CodeCompletionAllocator &Allocator = Results.getAllocator(); | |||
1567 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); | |||
1568 | ||||
1569 | typedef CodeCompletionResult Result; | |||
1570 | switch (CCC) { | |||
1571 | case Sema::PCC_Namespace: | |||
1572 | if (SemaRef.getLangOpts().CPlusPlus) { | |||
1573 | if (Results.includeCodePatterns()) { | |||
1574 | // namespace <identifier> { declarations } | |||
1575 | Builder.AddTypedTextChunk("namespace"); | |||
1576 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1577 | Builder.AddPlaceholderChunk("identifier"); | |||
1578 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
1579 | Builder.AddPlaceholderChunk("declarations"); | |||
1580 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
1581 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
1582 | Results.AddResult(Result(Builder.TakeString())); | |||
1583 | } | |||
1584 | ||||
1585 | // namespace identifier = identifier ; | |||
1586 | Builder.AddTypedTextChunk("namespace"); | |||
1587 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1588 | Builder.AddPlaceholderChunk("name"); | |||
1589 | Builder.AddChunk(CodeCompletionString::CK_Equal); | |||
1590 | Builder.AddPlaceholderChunk("namespace"); | |||
1591 | Results.AddResult(Result(Builder.TakeString())); | |||
1592 | ||||
1593 | // Using directives | |||
1594 | Builder.AddTypedTextChunk("using"); | |||
1595 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1596 | Builder.AddTextChunk("namespace"); | |||
1597 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1598 | Builder.AddPlaceholderChunk("identifier"); | |||
1599 | Results.AddResult(Result(Builder.TakeString())); | |||
1600 | ||||
1601 | // asm(string-literal) | |||
1602 | Builder.AddTypedTextChunk("asm"); | |||
1603 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1604 | Builder.AddPlaceholderChunk("string-literal"); | |||
1605 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1606 | Results.AddResult(Result(Builder.TakeString())); | |||
1607 | ||||
1608 | if (Results.includeCodePatterns()) { | |||
1609 | // Explicit template instantiation | |||
1610 | Builder.AddTypedTextChunk("template"); | |||
1611 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1612 | Builder.AddPlaceholderChunk("declaration"); | |||
1613 | Results.AddResult(Result(Builder.TakeString())); | |||
1614 | } | |||
1615 | } | |||
1616 | ||||
1617 | if (SemaRef.getLangOpts().ObjC1) | |||
1618 | AddObjCTopLevelResults(Results, true); | |||
1619 | ||||
1620 | AddTypedefResult(Results); | |||
1621 | // Fall through | |||
1622 | ||||
1623 | case Sema::PCC_Class: | |||
1624 | if (SemaRef.getLangOpts().CPlusPlus) { | |||
1625 | // Using declaration | |||
1626 | Builder.AddTypedTextChunk("using"); | |||
1627 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1628 | Builder.AddPlaceholderChunk("qualifier"); | |||
1629 | Builder.AddTextChunk("::"); | |||
1630 | Builder.AddPlaceholderChunk("name"); | |||
1631 | Results.AddResult(Result(Builder.TakeString())); | |||
1632 | ||||
1633 | // using typename qualifier::name (only in a dependent context) | |||
1634 | if (SemaRef.CurContext->isDependentContext()) { | |||
1635 | Builder.AddTypedTextChunk("using"); | |||
1636 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1637 | Builder.AddTextChunk("typename"); | |||
1638 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1639 | Builder.AddPlaceholderChunk("qualifier"); | |||
1640 | Builder.AddTextChunk("::"); | |||
1641 | Builder.AddPlaceholderChunk("name"); | |||
1642 | Results.AddResult(Result(Builder.TakeString())); | |||
1643 | } | |||
1644 | ||||
1645 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); | |||
1646 | ||||
1647 | if (CCC == Sema::PCC_Class) { | |||
1648 | AddTypedefResult(Results); | |||
1649 | ||||
1650 | // public: | |||
1651 | Builder.AddTypedTextChunk("public"); | |||
1652 | if (Results.includeCodePatterns()) | |||
1653 | Builder.AddChunk(CodeCompletionString::CK_Colon); | |||
1654 | Results.AddResult(Result(Builder.TakeString())); | |||
1655 | ||||
1656 | // protected: | |||
1657 | Builder.AddTypedTextChunk("protected"); | |||
1658 | if (Results.includeCodePatterns()) | |||
1659 | Builder.AddChunk(CodeCompletionString::CK_Colon); | |||
1660 | Results.AddResult(Result(Builder.TakeString())); | |||
1661 | ||||
1662 | // private: | |||
1663 | Builder.AddTypedTextChunk("private"); | |||
1664 | if (Results.includeCodePatterns()) | |||
1665 | Builder.AddChunk(CodeCompletionString::CK_Colon); | |||
1666 | Results.AddResult(Result(Builder.TakeString())); | |||
1667 | } | |||
1668 | } | |||
1669 | // Fall through | |||
1670 | ||||
1671 | case Sema::PCC_Template: | |||
1672 | case Sema::PCC_MemberTemplate: | |||
1673 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) { | |||
1674 | // template < parameters > | |||
1675 | Builder.AddTypedTextChunk("template"); | |||
1676 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); | |||
1677 | Builder.AddPlaceholderChunk("parameters"); | |||
1678 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); | |||
1679 | Results.AddResult(Result(Builder.TakeString())); | |||
1680 | } | |||
1681 | ||||
1682 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); | |||
1683 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); | |||
1684 | break; | |||
1685 | ||||
1686 | case Sema::PCC_ObjCInterface: | |||
1687 | AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true); | |||
1688 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); | |||
1689 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); | |||
1690 | break; | |||
1691 | ||||
1692 | case Sema::PCC_ObjCImplementation: | |||
1693 | AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true); | |||
1694 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); | |||
1695 | AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results); | |||
1696 | break; | |||
1697 | ||||
1698 | case Sema::PCC_ObjCInstanceVariableList: | |||
1699 | AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true); | |||
1700 | break; | |||
1701 | ||||
1702 | case Sema::PCC_RecoveryInFunction: | |||
1703 | case Sema::PCC_Statement: { | |||
1704 | AddTypedefResult(Results); | |||
1705 | ||||
1706 | if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() && | |||
1707 | SemaRef.getLangOpts().CXXExceptions) { | |||
1708 | Builder.AddTypedTextChunk("try"); | |||
1709 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
1710 | Builder.AddPlaceholderChunk("statements"); | |||
1711 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
1712 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
1713 | Builder.AddTextChunk("catch"); | |||
1714 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1715 | Builder.AddPlaceholderChunk("declaration"); | |||
1716 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1717 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
1718 | Builder.AddPlaceholderChunk("statements"); | |||
1719 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
1720 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
1721 | Results.AddResult(Result(Builder.TakeString())); | |||
1722 | } | |||
1723 | if (SemaRef.getLangOpts().ObjC1) | |||
1724 | AddObjCStatementResults(Results, true); | |||
1725 | ||||
1726 | if (Results.includeCodePatterns()) { | |||
1727 | // if (condition) { statements } | |||
1728 | Builder.AddTypedTextChunk("if"); | |||
1729 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1730 | if (SemaRef.getLangOpts().CPlusPlus) | |||
1731 | Builder.AddPlaceholderChunk("condition"); | |||
1732 | else | |||
1733 | Builder.AddPlaceholderChunk("expression"); | |||
1734 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1735 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
1736 | Builder.AddPlaceholderChunk("statements"); | |||
1737 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
1738 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
1739 | Results.AddResult(Result(Builder.TakeString())); | |||
1740 | ||||
1741 | // switch (condition) { } | |||
1742 | Builder.AddTypedTextChunk("switch"); | |||
1743 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1744 | if (SemaRef.getLangOpts().CPlusPlus) | |||
1745 | Builder.AddPlaceholderChunk("condition"); | |||
1746 | else | |||
1747 | Builder.AddPlaceholderChunk("expression"); | |||
1748 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1749 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
1750 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
1751 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
1752 | Results.AddResult(Result(Builder.TakeString())); | |||
1753 | } | |||
1754 | ||||
1755 | // Switch-specific statements. | |||
1756 | if (!SemaRef.getCurFunction()->SwitchStack.empty()) { | |||
1757 | // case expression: | |||
1758 | Builder.AddTypedTextChunk("case"); | |||
1759 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1760 | Builder.AddPlaceholderChunk("expression"); | |||
1761 | Builder.AddChunk(CodeCompletionString::CK_Colon); | |||
1762 | Results.AddResult(Result(Builder.TakeString())); | |||
1763 | ||||
1764 | // default: | |||
1765 | Builder.AddTypedTextChunk("default"); | |||
1766 | Builder.AddChunk(CodeCompletionString::CK_Colon); | |||
1767 | Results.AddResult(Result(Builder.TakeString())); | |||
1768 | } | |||
1769 | ||||
1770 | if (Results.includeCodePatterns()) { | |||
1771 | /// while (condition) { statements } | |||
1772 | Builder.AddTypedTextChunk("while"); | |||
1773 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1774 | if (SemaRef.getLangOpts().CPlusPlus) | |||
1775 | Builder.AddPlaceholderChunk("condition"); | |||
1776 | else | |||
1777 | Builder.AddPlaceholderChunk("expression"); | |||
1778 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1779 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
1780 | Builder.AddPlaceholderChunk("statements"); | |||
1781 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
1782 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
1783 | Results.AddResult(Result(Builder.TakeString())); | |||
1784 | ||||
1785 | // do { statements } while ( expression ); | |||
1786 | Builder.AddTypedTextChunk("do"); | |||
1787 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
1788 | Builder.AddPlaceholderChunk("statements"); | |||
1789 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
1790 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
1791 | Builder.AddTextChunk("while"); | |||
1792 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1793 | Builder.AddPlaceholderChunk("expression"); | |||
1794 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1795 | Results.AddResult(Result(Builder.TakeString())); | |||
1796 | ||||
1797 | // for ( for-init-statement ; condition ; expression ) { statements } | |||
1798 | Builder.AddTypedTextChunk("for"); | |||
1799 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1800 | if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99) | |||
1801 | Builder.AddPlaceholderChunk("init-statement"); | |||
1802 | else | |||
1803 | Builder.AddPlaceholderChunk("init-expression"); | |||
1804 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); | |||
1805 | Builder.AddPlaceholderChunk("condition"); | |||
1806 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); | |||
1807 | Builder.AddPlaceholderChunk("inc-expression"); | |||
1808 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1809 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
1810 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
1811 | Builder.AddPlaceholderChunk("statements"); | |||
1812 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
1813 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
1814 | Results.AddResult(Result(Builder.TakeString())); | |||
1815 | } | |||
1816 | ||||
1817 | if (S->getContinueParent()) { | |||
1818 | // continue ; | |||
1819 | Builder.AddTypedTextChunk("continue"); | |||
1820 | Results.AddResult(Result(Builder.TakeString())); | |||
1821 | } | |||
1822 | ||||
1823 | if (S->getBreakParent()) { | |||
1824 | // break ; | |||
1825 | Builder.AddTypedTextChunk("break"); | |||
1826 | Results.AddResult(Result(Builder.TakeString())); | |||
1827 | } | |||
1828 | ||||
1829 | // "return expression ;" or "return ;", depending on whether we | |||
1830 | // know the function is void or not. | |||
1831 | bool isVoid = false; | |||
1832 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext)) | |||
1833 | isVoid = Function->getReturnType()->isVoidType(); | |||
1834 | else if (ObjCMethodDecl *Method | |||
1835 | = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext)) | |||
1836 | isVoid = Method->getReturnType()->isVoidType(); | |||
1837 | else if (SemaRef.getCurBlock() && | |||
1838 | !SemaRef.getCurBlock()->ReturnType.isNull()) | |||
1839 | isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType(); | |||
1840 | Builder.AddTypedTextChunk("return"); | |||
1841 | if (!isVoid) { | |||
1842 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1843 | Builder.AddPlaceholderChunk("expression"); | |||
1844 | } | |||
1845 | Results.AddResult(Result(Builder.TakeString())); | |||
1846 | ||||
1847 | // goto identifier ; | |||
1848 | Builder.AddTypedTextChunk("goto"); | |||
1849 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1850 | Builder.AddPlaceholderChunk("label"); | |||
1851 | Results.AddResult(Result(Builder.TakeString())); | |||
1852 | ||||
1853 | // Using directives | |||
1854 | Builder.AddTypedTextChunk("using"); | |||
1855 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1856 | Builder.AddTextChunk("namespace"); | |||
1857 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1858 | Builder.AddPlaceholderChunk("identifier"); | |||
1859 | Results.AddResult(Result(Builder.TakeString())); | |||
1860 | ||||
1861 | AddStaticAssertResult(Builder, Results, SemaRef.getLangOpts()); | |||
1862 | } | |||
1863 | LLVM_FALLTHROUGH[[clang::fallthrough]]; | |||
1864 | ||||
1865 | // Fall through (for statement expressions). | |||
1866 | case Sema::PCC_ForInit: | |||
1867 | case Sema::PCC_Condition: | |||
1868 | AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results); | |||
1869 | // Fall through: conditions and statements can have expressions. | |||
1870 | LLVM_FALLTHROUGH[[clang::fallthrough]]; | |||
1871 | ||||
1872 | case Sema::PCC_ParenthesizedExpression: | |||
1873 | if (SemaRef.getLangOpts().ObjCAutoRefCount && | |||
1874 | CCC == Sema::PCC_ParenthesizedExpression) { | |||
1875 | // (__bridge <type>)<expression> | |||
1876 | Builder.AddTypedTextChunk("__bridge"); | |||
1877 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1878 | Builder.AddPlaceholderChunk("type"); | |||
1879 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1880 | Builder.AddPlaceholderChunk("expression"); | |||
1881 | Results.AddResult(Result(Builder.TakeString())); | |||
1882 | ||||
1883 | // (__bridge_transfer <Objective-C type>)<expression> | |||
1884 | Builder.AddTypedTextChunk("__bridge_transfer"); | |||
1885 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1886 | Builder.AddPlaceholderChunk("Objective-C type"); | |||
1887 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1888 | Builder.AddPlaceholderChunk("expression"); | |||
1889 | Results.AddResult(Result(Builder.TakeString())); | |||
1890 | ||||
1891 | // (__bridge_retained <CF type>)<expression> | |||
1892 | Builder.AddTypedTextChunk("__bridge_retained"); | |||
1893 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1894 | Builder.AddPlaceholderChunk("CF type"); | |||
1895 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1896 | Builder.AddPlaceholderChunk("expression"); | |||
1897 | Results.AddResult(Result(Builder.TakeString())); | |||
1898 | } | |||
1899 | // Fall through | |||
1900 | LLVM_FALLTHROUGH[[clang::fallthrough]]; | |||
1901 | ||||
1902 | case Sema::PCC_Expression: { | |||
1903 | if (SemaRef.getLangOpts().CPlusPlus) { | |||
1904 | // 'this', if we're in a non-static member function. | |||
1905 | addThisCompletion(SemaRef, Results); | |||
1906 | ||||
1907 | // true | |||
1908 | Builder.AddResultTypeChunk("bool"); | |||
1909 | Builder.AddTypedTextChunk("true"); | |||
1910 | Results.AddResult(Result(Builder.TakeString())); | |||
1911 | ||||
1912 | // false | |||
1913 | Builder.AddResultTypeChunk("bool"); | |||
1914 | Builder.AddTypedTextChunk("false"); | |||
1915 | Results.AddResult(Result(Builder.TakeString())); | |||
1916 | ||||
1917 | if (SemaRef.getLangOpts().RTTI) { | |||
1918 | // dynamic_cast < type-id > ( expression ) | |||
1919 | Builder.AddTypedTextChunk("dynamic_cast"); | |||
1920 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); | |||
1921 | Builder.AddPlaceholderChunk("type"); | |||
1922 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); | |||
1923 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1924 | Builder.AddPlaceholderChunk("expression"); | |||
1925 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1926 | Results.AddResult(Result(Builder.TakeString())); | |||
1927 | } | |||
1928 | ||||
1929 | // static_cast < type-id > ( expression ) | |||
1930 | Builder.AddTypedTextChunk("static_cast"); | |||
1931 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); | |||
1932 | Builder.AddPlaceholderChunk("type"); | |||
1933 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); | |||
1934 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1935 | Builder.AddPlaceholderChunk("expression"); | |||
1936 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1937 | Results.AddResult(Result(Builder.TakeString())); | |||
1938 | ||||
1939 | // reinterpret_cast < type-id > ( expression ) | |||
1940 | Builder.AddTypedTextChunk("reinterpret_cast"); | |||
1941 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); | |||
1942 | Builder.AddPlaceholderChunk("type"); | |||
1943 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); | |||
1944 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1945 | Builder.AddPlaceholderChunk("expression"); | |||
1946 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1947 | Results.AddResult(Result(Builder.TakeString())); | |||
1948 | ||||
1949 | // const_cast < type-id > ( expression ) | |||
1950 | Builder.AddTypedTextChunk("const_cast"); | |||
1951 | Builder.AddChunk(CodeCompletionString::CK_LeftAngle); | |||
1952 | Builder.AddPlaceholderChunk("type"); | |||
1953 | Builder.AddChunk(CodeCompletionString::CK_RightAngle); | |||
1954 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1955 | Builder.AddPlaceholderChunk("expression"); | |||
1956 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1957 | Results.AddResult(Result(Builder.TakeString())); | |||
1958 | ||||
1959 | if (SemaRef.getLangOpts().RTTI) { | |||
1960 | // typeid ( expression-or-type ) | |||
1961 | Builder.AddResultTypeChunk("std::type_info"); | |||
1962 | Builder.AddTypedTextChunk("typeid"); | |||
1963 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1964 | Builder.AddPlaceholderChunk("expression-or-type"); | |||
1965 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1966 | Results.AddResult(Result(Builder.TakeString())); | |||
1967 | } | |||
1968 | ||||
1969 | // new T ( ... ) | |||
1970 | Builder.AddTypedTextChunk("new"); | |||
1971 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1972 | Builder.AddPlaceholderChunk("type"); | |||
1973 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1974 | Builder.AddPlaceholderChunk("expressions"); | |||
1975 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1976 | Results.AddResult(Result(Builder.TakeString())); | |||
1977 | ||||
1978 | // new T [ ] ( ... ) | |||
1979 | Builder.AddTypedTextChunk("new"); | |||
1980 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1981 | Builder.AddPlaceholderChunk("type"); | |||
1982 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); | |||
1983 | Builder.AddPlaceholderChunk("size"); | |||
1984 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); | |||
1985 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
1986 | Builder.AddPlaceholderChunk("expressions"); | |||
1987 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
1988 | Results.AddResult(Result(Builder.TakeString())); | |||
1989 | ||||
1990 | // delete expression | |||
1991 | Builder.AddResultTypeChunk("void"); | |||
1992 | Builder.AddTypedTextChunk("delete"); | |||
1993 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
1994 | Builder.AddPlaceholderChunk("expression"); | |||
1995 | Results.AddResult(Result(Builder.TakeString())); | |||
1996 | ||||
1997 | // delete [] expression | |||
1998 | Builder.AddResultTypeChunk("void"); | |||
1999 | Builder.AddTypedTextChunk("delete"); | |||
2000 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
2001 | Builder.AddChunk(CodeCompletionString::CK_LeftBracket); | |||
2002 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); | |||
2003 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
2004 | Builder.AddPlaceholderChunk("expression"); | |||
2005 | Results.AddResult(Result(Builder.TakeString())); | |||
2006 | ||||
2007 | if (SemaRef.getLangOpts().CXXExceptions) { | |||
2008 | // throw expression | |||
2009 | Builder.AddResultTypeChunk("void"); | |||
2010 | Builder.AddTypedTextChunk("throw"); | |||
2011 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
2012 | Builder.AddPlaceholderChunk("expression"); | |||
2013 | Results.AddResult(Result(Builder.TakeString())); | |||
2014 | } | |||
2015 | ||||
2016 | // FIXME: Rethrow? | |||
2017 | ||||
2018 | if (SemaRef.getLangOpts().CPlusPlus11) { | |||
2019 | // nullptr | |||
2020 | Builder.AddResultTypeChunk("std::nullptr_t"); | |||
2021 | Builder.AddTypedTextChunk("nullptr"); | |||
2022 | Results.AddResult(Result(Builder.TakeString())); | |||
2023 | ||||
2024 | // alignof | |||
2025 | Builder.AddResultTypeChunk("size_t"); | |||
2026 | Builder.AddTypedTextChunk("alignof"); | |||
2027 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
2028 | Builder.AddPlaceholderChunk("type"); | |||
2029 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
2030 | Results.AddResult(Result(Builder.TakeString())); | |||
2031 | ||||
2032 | // noexcept | |||
2033 | Builder.AddResultTypeChunk("bool"); | |||
2034 | Builder.AddTypedTextChunk("noexcept"); | |||
2035 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
2036 | Builder.AddPlaceholderChunk("expression"); | |||
2037 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
2038 | Results.AddResult(Result(Builder.TakeString())); | |||
2039 | ||||
2040 | // sizeof... expression | |||
2041 | Builder.AddResultTypeChunk("size_t"); | |||
2042 | Builder.AddTypedTextChunk("sizeof..."); | |||
2043 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
2044 | Builder.AddPlaceholderChunk("parameter-pack"); | |||
2045 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
2046 | Results.AddResult(Result(Builder.TakeString())); | |||
2047 | } | |||
2048 | } | |||
2049 | ||||
2050 | if (SemaRef.getLangOpts().ObjC1) { | |||
2051 | // Add "super", if we're in an Objective-C class with a superclass. | |||
2052 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { | |||
2053 | // The interface can be NULL. | |||
2054 | if (ObjCInterfaceDecl *ID = Method->getClassInterface()) | |||
2055 | if (ID->getSuperClass()) { | |||
2056 | std::string SuperType; | |||
2057 | SuperType = ID->getSuperClass()->getNameAsString(); | |||
2058 | if (Method->isInstanceMethod()) | |||
2059 | SuperType += " *"; | |||
2060 | ||||
2061 | Builder.AddResultTypeChunk(Allocator.CopyString(SuperType)); | |||
2062 | Builder.AddTypedTextChunk("super"); | |||
2063 | Results.AddResult(Result(Builder.TakeString())); | |||
2064 | } | |||
2065 | } | |||
2066 | ||||
2067 | AddObjCExpressionResults(Results, true); | |||
2068 | } | |||
2069 | ||||
2070 | if (SemaRef.getLangOpts().C11) { | |||
2071 | // _Alignof | |||
2072 | Builder.AddResultTypeChunk("size_t"); | |||
2073 | if (SemaRef.PP.isMacroDefined("alignof")) | |||
2074 | Builder.AddTypedTextChunk("alignof"); | |||
2075 | else | |||
2076 | Builder.AddTypedTextChunk("_Alignof"); | |||
2077 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
2078 | Builder.AddPlaceholderChunk("type"); | |||
2079 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
2080 | Results.AddResult(Result(Builder.TakeString())); | |||
2081 | } | |||
2082 | ||||
2083 | // sizeof expression | |||
2084 | Builder.AddResultTypeChunk("size_t"); | |||
2085 | Builder.AddTypedTextChunk("sizeof"); | |||
2086 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
2087 | Builder.AddPlaceholderChunk("expression-or-type"); | |||
2088 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
2089 | Results.AddResult(Result(Builder.TakeString())); | |||
2090 | break; | |||
2091 | } | |||
2092 | ||||
2093 | case Sema::PCC_Type: | |||
2094 | case Sema::PCC_LocalDeclarationSpecifiers: | |||
2095 | break; | |||
2096 | } | |||
2097 | ||||
2098 | if (WantTypesInContext(CCC, SemaRef.getLangOpts())) | |||
2099 | AddTypeSpecifierResults(SemaRef.getLangOpts(), Results); | |||
2100 | ||||
2101 | if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type) | |||
2102 | Results.AddResult(Result("operator")); | |||
2103 | } | |||
2104 | ||||
2105 | /// \brief If the given declaration has an associated type, add it as a result | |||
2106 | /// type chunk. | |||
2107 | static void AddResultTypeChunk(ASTContext &Context, | |||
2108 | const PrintingPolicy &Policy, | |||
2109 | const NamedDecl *ND, | |||
2110 | QualType BaseType, | |||
2111 | CodeCompletionBuilder &Result) { | |||
2112 | if (!ND) | |||
2113 | return; | |||
2114 | ||||
2115 | // Skip constructors and conversion functions, which have their return types | |||
2116 | // built into their names. | |||
2117 | if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND)) | |||
2118 | return; | |||
2119 | ||||
2120 | // Determine the type of the declaration (if it has a type). | |||
2121 | QualType T; | |||
2122 | if (const FunctionDecl *Function = ND->getAsFunction()) | |||
2123 | T = Function->getReturnType(); | |||
2124 | else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { | |||
2125 | if (!BaseType.isNull()) | |||
2126 | T = Method->getSendResultType(BaseType); | |||
2127 | else | |||
2128 | T = Method->getReturnType(); | |||
2129 | } else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND)) | |||
2130 | T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext())); | |||
2131 | else if (isa<UnresolvedUsingValueDecl>(ND)) { | |||
2132 | /* Do nothing: ignore unresolved using declarations*/ | |||
2133 | } else if (const ObjCIvarDecl *Ivar = dyn_cast<ObjCIvarDecl>(ND)) { | |||
2134 | if (!BaseType.isNull()) | |||
2135 | T = Ivar->getUsageType(BaseType); | |||
2136 | else | |||
2137 | T = Ivar->getType(); | |||
2138 | } else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) { | |||
2139 | T = Value->getType(); | |||
2140 | } else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND)) { | |||
2141 | if (!BaseType.isNull()) | |||
2142 | T = Property->getUsageType(BaseType); | |||
2143 | else | |||
2144 | T = Property->getType(); | |||
2145 | } | |||
2146 | ||||
2147 | if (T.isNull() || Context.hasSameType(T, Context.DependentTy)) | |||
2148 | return; | |||
2149 | ||||
2150 | Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy, | |||
2151 | Result.getAllocator())); | |||
2152 | } | |||
2153 | ||||
2154 | static void MaybeAddSentinel(Preprocessor &PP, | |||
2155 | const NamedDecl *FunctionOrMethod, | |||
2156 | CodeCompletionBuilder &Result) { | |||
2157 | if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>()) | |||
2158 | if (Sentinel->getSentinel() == 0) { | |||
2159 | if (PP.getLangOpts().ObjC1 && PP.isMacroDefined("nil")) | |||
2160 | Result.AddTextChunk(", nil"); | |||
2161 | else if (PP.isMacroDefined("NULL")) | |||
2162 | Result.AddTextChunk(", NULL"); | |||
2163 | else | |||
2164 | Result.AddTextChunk(", (void*)0"); | |||
2165 | } | |||
2166 | } | |||
2167 | ||||
2168 | static std::string formatObjCParamQualifiers(unsigned ObjCQuals, | |||
2169 | QualType &Type) { | |||
2170 | std::string Result; | |||
2171 | if (ObjCQuals & Decl::OBJC_TQ_In) | |||
2172 | Result += "in "; | |||
2173 | else if (ObjCQuals & Decl::OBJC_TQ_Inout) | |||
2174 | Result += "inout "; | |||
2175 | else if (ObjCQuals & Decl::OBJC_TQ_Out) | |||
2176 | Result += "out "; | |||
2177 | if (ObjCQuals & Decl::OBJC_TQ_Bycopy) | |||
2178 | Result += "bycopy "; | |||
2179 | else if (ObjCQuals & Decl::OBJC_TQ_Byref) | |||
2180 | Result += "byref "; | |||
2181 | if (ObjCQuals & Decl::OBJC_TQ_Oneway) | |||
2182 | Result += "oneway "; | |||
2183 | if (ObjCQuals & Decl::OBJC_TQ_CSNullability) { | |||
2184 | if (auto nullability = AttributedType::stripOuterNullability(Type)) { | |||
2185 | switch (*nullability) { | |||
2186 | case NullabilityKind::NonNull: | |||
2187 | Result += "nonnull "; | |||
2188 | break; | |||
2189 | ||||
2190 | case NullabilityKind::Nullable: | |||
2191 | Result += "nullable "; | |||
2192 | break; | |||
2193 | ||||
2194 | case NullabilityKind::Unspecified: | |||
2195 | Result += "null_unspecified "; | |||
2196 | break; | |||
2197 | } | |||
2198 | } | |||
2199 | } | |||
2200 | return Result; | |||
2201 | } | |||
2202 | ||||
2203 | /// \brief Tries to find the most appropriate type location for an Objective-C | |||
2204 | /// block placeholder. | |||
2205 | /// | |||
2206 | /// This function ignores things like typedefs and qualifiers in order to | |||
2207 | /// present the most relevant and accurate block placeholders in code completion | |||
2208 | /// results. | |||
2209 | static void findTypeLocationForBlockDecl(const TypeSourceInfo *TSInfo, | |||
2210 | FunctionTypeLoc &Block, | |||
2211 | FunctionProtoTypeLoc &BlockProto, | |||
2212 | bool SuppressBlock = false) { | |||
2213 | if (!TSInfo) | |||
2214 | return; | |||
2215 | TypeLoc TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); | |||
2216 | while (true) { | |||
2217 | // Look through typedefs. | |||
2218 | if (!SuppressBlock) { | |||
2219 | if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) { | |||
2220 | if (TypeSourceInfo *InnerTSInfo = | |||
2221 | TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { | |||
2222 | TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); | |||
2223 | continue; | |||
2224 | } | |||
2225 | } | |||
2226 | ||||
2227 | // Look through qualified types | |||
2228 | if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) { | |||
2229 | TL = QualifiedTL.getUnqualifiedLoc(); | |||
2230 | continue; | |||
2231 | } | |||
2232 | ||||
2233 | if (AttributedTypeLoc AttrTL = TL.getAs<AttributedTypeLoc>()) { | |||
2234 | TL = AttrTL.getModifiedLoc(); | |||
2235 | continue; | |||
2236 | } | |||
2237 | } | |||
2238 | ||||
2239 | // Try to get the function prototype behind the block pointer type, | |||
2240 | // then we're done. | |||
2241 | if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) { | |||
2242 | TL = BlockPtr.getPointeeLoc().IgnoreParens(); | |||
2243 | Block = TL.getAs<FunctionTypeLoc>(); | |||
2244 | BlockProto = TL.getAs<FunctionProtoTypeLoc>(); | |||
2245 | } | |||
2246 | break; | |||
2247 | } | |||
2248 | } | |||
2249 | ||||
2250 | static std::string | |||
2251 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, | |||
2252 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, | |||
2253 | bool SuppressBlockName = false, | |||
2254 | bool SuppressBlock = false, | |||
2255 | Optional<ArrayRef<QualType>> ObjCSubsts = None); | |||
2256 | ||||
2257 | static std::string FormatFunctionParameter(const PrintingPolicy &Policy, | |||
2258 | const ParmVarDecl *Param, | |||
2259 | bool SuppressName = false, | |||
2260 | bool SuppressBlock = false, | |||
2261 | Optional<ArrayRef<QualType>> ObjCSubsts = None) { | |||
2262 | bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext()); | |||
2263 | if (Param->getType()->isDependentType() || | |||
2264 | !Param->getType()->isBlockPointerType()) { | |||
2265 | // The argument for a dependent or non-block parameter is a placeholder | |||
2266 | // containing that parameter's type. | |||
2267 | std::string Result; | |||
2268 | ||||
2269 | if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName) | |||
2270 | Result = Param->getIdentifier()->getName(); | |||
2271 | ||||
2272 | QualType Type = Param->getType(); | |||
2273 | if (ObjCSubsts) | |||
2274 | Type = Type.substObjCTypeArgs(Param->getASTContext(), *ObjCSubsts, | |||
2275 | ObjCSubstitutionContext::Parameter); | |||
2276 | if (ObjCMethodParam) { | |||
2277 | Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(), | |||
2278 | Type); | |||
2279 | Result += Type.getAsString(Policy) + ")"; | |||
2280 | if (Param->getIdentifier() && !SuppressName) | |||
2281 | Result += Param->getIdentifier()->getName(); | |||
2282 | } else { | |||
2283 | Type.getAsStringInternal(Result, Policy); | |||
2284 | } | |||
2285 | return Result; | |||
2286 | } | |||
2287 | ||||
2288 | // The argument for a block pointer parameter is a block literal with | |||
2289 | // the appropriate type. | |||
2290 | FunctionTypeLoc Block; | |||
2291 | FunctionProtoTypeLoc BlockProto; | |||
2292 | findTypeLocationForBlockDecl(Param->getTypeSourceInfo(), Block, BlockProto, | |||
2293 | SuppressBlock); | |||
2294 | // Try to retrieve the block type information from the property if this is a | |||
2295 | // parameter in a setter. | |||
2296 | if (!Block && ObjCMethodParam && | |||
2297 | cast<ObjCMethodDecl>(Param->getDeclContext())->isPropertyAccessor()) { | |||
2298 | if (const auto *PD = cast<ObjCMethodDecl>(Param->getDeclContext()) | |||
2299 | ->findPropertyDecl(/*CheckOverrides=*/false)) | |||
2300 | findTypeLocationForBlockDecl(PD->getTypeSourceInfo(), Block, BlockProto, | |||
2301 | SuppressBlock); | |||
2302 | } | |||
2303 | ||||
2304 | if (!Block) { | |||
2305 | // We were unable to find a FunctionProtoTypeLoc with parameter names | |||
2306 | // for the block; just use the parameter type as a placeholder. | |||
2307 | std::string Result; | |||
2308 | if (!ObjCMethodParam && Param->getIdentifier()) | |||
2309 | Result = Param->getIdentifier()->getName(); | |||
2310 | ||||
2311 | QualType Type = Param->getType().getUnqualifiedType(); | |||
2312 | ||||
2313 | if (ObjCMethodParam) { | |||
2314 | Result = Type.getAsString(Policy); | |||
2315 | std::string Quals = | |||
2316 | formatObjCParamQualifiers(Param->getObjCDeclQualifier(), Type); | |||
2317 | if (!Quals.empty()) | |||
2318 | Result = "(" + Quals + " " + Result + ")"; | |||
2319 | if (Result.back() != ')') | |||
2320 | Result += " "; | |||
2321 | if (Param->getIdentifier()) | |||
2322 | Result += Param->getIdentifier()->getName(); | |||
2323 | } else { | |||
2324 | Type.getAsStringInternal(Result, Policy); | |||
2325 | } | |||
2326 | ||||
2327 | return Result; | |||
2328 | } | |||
2329 | ||||
2330 | // We have the function prototype behind the block pointer type, as it was | |||
2331 | // written in the source. | |||
2332 | return formatBlockPlaceholder(Policy, Param, Block, BlockProto, | |||
2333 | /*SuppressBlockName=*/false, SuppressBlock, | |||
2334 | ObjCSubsts); | |||
2335 | } | |||
2336 | ||||
2337 | /// \brief Returns a placeholder string that corresponds to an Objective-C block | |||
2338 | /// declaration. | |||
2339 | /// | |||
2340 | /// \param BlockDecl A declaration with an Objective-C block type. | |||
2341 | /// | |||
2342 | /// \param Block The most relevant type location for that block type. | |||
2343 | /// | |||
2344 | /// \param SuppressBlockName Determines wether or not the name of the block | |||
2345 | /// declaration is included in the resulting string. | |||
2346 | static std::string | |||
2347 | formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, | |||
2348 | FunctionTypeLoc &Block, FunctionProtoTypeLoc &BlockProto, | |||
2349 | bool SuppressBlockName, bool SuppressBlock, | |||
2350 | Optional<ArrayRef<QualType>> ObjCSubsts) { | |||
2351 | std::string Result; | |||
2352 | QualType ResultType = Block.getTypePtr()->getReturnType(); | |||
2353 | if (ObjCSubsts) | |||
2354 | ResultType = | |||
2355 | ResultType.substObjCTypeArgs(BlockDecl->getASTContext(), *ObjCSubsts, | |||
2356 | ObjCSubstitutionContext::Result); | |||
2357 | if (!ResultType->isVoidType() || SuppressBlock) | |||
2358 | ResultType.getAsStringInternal(Result, Policy); | |||
2359 | ||||
2360 | // Format the parameter list. | |||
2361 | std::string Params; | |||
2362 | if (!BlockProto || Block.getNumParams() == 0) { | |||
2363 | if (BlockProto && BlockProto.getTypePtr()->isVariadic()) | |||
2364 | Params = "(...)"; | |||
2365 | else | |||
2366 | Params = "(void)"; | |||
2367 | } else { | |||
2368 | Params += "("; | |||
2369 | for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) { | |||
2370 | if (I) | |||
2371 | Params += ", "; | |||
2372 | Params += FormatFunctionParameter(Policy, Block.getParam(I), | |||
2373 | /*SuppressName=*/false, | |||
2374 | /*SuppressBlock=*/true, ObjCSubsts); | |||
2375 | ||||
2376 | if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) | |||
2377 | Params += ", ..."; | |||
2378 | } | |||
2379 | Params += ")"; | |||
2380 | } | |||
2381 | ||||
2382 | if (SuppressBlock) { | |||
2383 | // Format as a parameter. | |||
2384 | Result = Result + " (^"; | |||
2385 | if (!SuppressBlockName && BlockDecl->getIdentifier()) | |||
2386 | Result += BlockDecl->getIdentifier()->getName(); | |||
2387 | Result += ")"; | |||
2388 | Result += Params; | |||
2389 | } else { | |||
2390 | // Format as a block literal argument. | |||
2391 | Result = '^' + Result; | |||
2392 | Result += Params; | |||
2393 | ||||
2394 | if (!SuppressBlockName && BlockDecl->getIdentifier()) | |||
2395 | Result += BlockDecl->getIdentifier()->getName(); | |||
2396 | } | |||
2397 | ||||
2398 | return Result; | |||
2399 | } | |||
2400 | ||||
2401 | /// \brief Add function parameter chunks to the given code completion string. | |||
2402 | static void AddFunctionParameterChunks(Preprocessor &PP, | |||
2403 | const PrintingPolicy &Policy, | |||
2404 | const FunctionDecl *Function, | |||
2405 | CodeCompletionBuilder &Result, | |||
2406 | unsigned Start = 0, | |||
2407 | bool InOptional = false) { | |||
2408 | bool FirstParameter = true; | |||
2409 | ||||
2410 | for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) { | |||
2411 | const ParmVarDecl *Param = Function->getParamDecl(P); | |||
2412 | ||||
2413 | if (Param->hasDefaultArg() && !InOptional) { | |||
2414 | // When we see an optional default argument, put that argument and | |||
2415 | // the remaining default arguments into a new, optional string. | |||
2416 | CodeCompletionBuilder Opt(Result.getAllocator(), | |||
2417 | Result.getCodeCompletionTUInfo()); | |||
2418 | if (!FirstParameter) | |||
2419 | Opt.AddChunk(CodeCompletionString::CK_Comma); | |||
2420 | AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true); | |||
2421 | Result.AddOptionalChunk(Opt.TakeString()); | |||
2422 | break; | |||
2423 | } | |||
2424 | ||||
2425 | if (FirstParameter) | |||
2426 | FirstParameter = false; | |||
2427 | else | |||
2428 | Result.AddChunk(CodeCompletionString::CK_Comma); | |||
2429 | ||||
2430 | InOptional = false; | |||
2431 | ||||
2432 | // Format the placeholder string. | |||
2433 | std::string PlaceholderStr = FormatFunctionParameter(Policy, Param); | |||
2434 | ||||
2435 | if (Function->isVariadic() && P == N - 1) | |||
2436 | PlaceholderStr += ", ..."; | |||
2437 | ||||
2438 | // Add the placeholder string. | |||
2439 | Result.AddPlaceholderChunk( | |||
2440 | Result.getAllocator().CopyString(PlaceholderStr)); | |||
2441 | } | |||
2442 | ||||
2443 | if (const FunctionProtoType *Proto | |||
2444 | = Function->getType()->getAs<FunctionProtoType>()) | |||
2445 | if (Proto->isVariadic()) { | |||
2446 | if (Proto->getNumParams() == 0) | |||
2447 | Result.AddPlaceholderChunk("..."); | |||
2448 | ||||
2449 | MaybeAddSentinel(PP, Function, Result); | |||
2450 | } | |||
2451 | } | |||
2452 | ||||
2453 | /// \brief Add template parameter chunks to the given code completion string. | |||
2454 | static void AddTemplateParameterChunks(ASTContext &Context, | |||
2455 | const PrintingPolicy &Policy, | |||
2456 | const TemplateDecl *Template, | |||
2457 | CodeCompletionBuilder &Result, | |||
2458 | unsigned MaxParameters = 0, | |||
2459 | unsigned Start = 0, | |||
2460 | bool InDefaultArg = false) { | |||
2461 | bool FirstParameter = true; | |||
2462 | ||||
2463 | // Prefer to take the template parameter names from the first declaration of | |||
2464 | // the template. | |||
2465 | Template = cast<TemplateDecl>(Template->getCanonicalDecl()); | |||
2466 | ||||
2467 | TemplateParameterList *Params = Template->getTemplateParameters(); | |||
2468 | TemplateParameterList::iterator PEnd = Params->end(); | |||
2469 | if (MaxParameters) | |||
2470 | PEnd = Params->begin() + MaxParameters; | |||
2471 | for (TemplateParameterList::iterator P = Params->begin() + Start; | |||
2472 | P != PEnd; ++P) { | |||
2473 | bool HasDefaultArg = false; | |||
2474 | std::string PlaceholderStr; | |||
2475 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { | |||
2476 | if (TTP->wasDeclaredWithTypename()) | |||
2477 | PlaceholderStr = "typename"; | |||
2478 | else | |||
2479 | PlaceholderStr = "class"; | |||
2480 | ||||
2481 | if (TTP->getIdentifier()) { | |||
2482 | PlaceholderStr += ' '; | |||
2483 | PlaceholderStr += TTP->getIdentifier()->getName(); | |||
2484 | } | |||
2485 | ||||
2486 | HasDefaultArg = TTP->hasDefaultArgument(); | |||
2487 | } else if (NonTypeTemplateParmDecl *NTTP | |||
2488 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { | |||
2489 | if (NTTP->getIdentifier()) | |||
2490 | PlaceholderStr = NTTP->getIdentifier()->getName(); | |||
2491 | NTTP->getType().getAsStringInternal(PlaceholderStr, Policy); | |||
2492 | HasDefaultArg = NTTP->hasDefaultArgument(); | |||
2493 | } else { | |||
2494 | assert(isa<TemplateTemplateParmDecl>(*P))((isa<TemplateTemplateParmDecl>(*P)) ? static_cast<void > (0) : __assert_fail ("isa<TemplateTemplateParmDecl>(*P)" , "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/clang/lib/Sema/SemaCodeComplete.cpp" , 2494, __PRETTY_FUNCTION__)); | |||
2495 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); | |||
2496 | ||||
2497 | // Since putting the template argument list into the placeholder would | |||
2498 | // be very, very long, we just use an abbreviation. | |||
2499 | PlaceholderStr = "template<...> class"; | |||
2500 | if (TTP->getIdentifier()) { | |||
2501 | PlaceholderStr += ' '; | |||
2502 | PlaceholderStr += TTP->getIdentifier()->getName(); | |||
2503 | } | |||
2504 | ||||
2505 | HasDefaultArg = TTP->hasDefaultArgument(); | |||
2506 | } | |||
2507 | ||||
2508 | if (HasDefaultArg && !InDefaultArg) { | |||
2509 | // When we see an optional default argument, put that argument and | |||
2510 | // the remaining default arguments into a new, optional string. | |||
2511 | CodeCompletionBuilder Opt(Result.getAllocator(), | |||
2512 | Result.getCodeCompletionTUInfo()); | |||
2513 | if (!FirstParameter) | |||
2514 | Opt.AddChunk(CodeCompletionString::CK_Comma); | |||
2515 | AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters, | |||
2516 | P - Params->begin(), true); | |||
2517 | Result.AddOptionalChunk(Opt.TakeString()); | |||
2518 | break; | |||
2519 | } | |||
2520 | ||||
2521 | InDefaultArg = false; | |||
2522 | ||||
2523 | if (FirstParameter) | |||
2524 | FirstParameter = false; | |||
2525 | else | |||
2526 | Result.AddChunk(CodeCompletionString::CK_Comma); | |||
2527 | ||||
2528 | // Add the placeholder string. | |||
2529 | Result.AddPlaceholderChunk( | |||
2530 | Result.getAllocator().CopyString(PlaceholderStr)); | |||
2531 | } | |||
2532 | } | |||
2533 | ||||
2534 | /// \brief Add a qualifier to the given code-completion string, if the | |||
2535 | /// provided nested-name-specifier is non-NULL. | |||
2536 | static void | |||
2537 | AddQualifierToCompletionString(CodeCompletionBuilder &Result, | |||
2538 | NestedNameSpecifier *Qualifier, | |||
2539 | bool QualifierIsInformative, | |||
2540 | ASTContext &Context, | |||
2541 | const PrintingPolicy &Policy) { | |||
2542 | if (!Qualifier) | |||
2543 | return; | |||
2544 | ||||
2545 | std::string PrintedNNS; | |||
2546 | { | |||
2547 | llvm::raw_string_ostream OS(PrintedNNS); | |||
2548 | Qualifier->print(OS, Policy); | |||
2549 | } | |||
2550 | if (QualifierIsInformative) | |||
2551 | Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS)); | |||
2552 | else | |||
2553 | Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS)); | |||
2554 | } | |||
2555 | ||||
2556 | static void | |||
2557 | AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result, | |||
2558 | const FunctionDecl *Function) { | |||
2559 | const FunctionProtoType *Proto | |||
2560 | = Function->getType()->getAs<FunctionProtoType>(); | |||
2561 | if (!Proto || !Proto->getTypeQuals()) | |||
2562 | return; | |||
2563 | ||||
2564 | // FIXME: Add ref-qualifier! | |||
2565 | ||||
2566 | // Handle single qualifiers without copying | |||
2567 | if (Proto->getTypeQuals() == Qualifiers::Const) { | |||
2568 | Result.AddInformativeChunk(" const"); | |||
2569 | return; | |||
2570 | } | |||
2571 | ||||
2572 | if (Proto->getTypeQuals() == Qualifiers::Volatile) { | |||
2573 | Result.AddInformativeChunk(" volatile"); | |||
2574 | return; | |||
2575 | } | |||
2576 | ||||
2577 | if (Proto->getTypeQuals() == Qualifiers::Restrict) { | |||
2578 | Result.AddInformativeChunk(" restrict"); | |||
2579 | return; | |||
2580 | } | |||
2581 | ||||
2582 | // Handle multiple qualifiers. | |||
2583 | std::string QualsStr; | |||
2584 | if (Proto->isConst()) | |||
2585 | QualsStr += " const"; | |||
2586 | if (Proto->isVolatile()) | |||
2587 | QualsStr += " volatile"; | |||
2588 | if (Proto->isRestrict()) | |||
2589 | QualsStr += " restrict"; | |||
2590 | Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr)); | |||
2591 | } | |||
2592 | ||||
2593 | /// \brief Add the name of the given declaration | |||
2594 | static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy, | |||
2595 | const NamedDecl *ND, | |||
2596 | CodeCompletionBuilder &Result) { | |||
2597 | DeclarationName Name = ND->getDeclName(); | |||
2598 | if (!Name) | |||
2599 | return; | |||
2600 | ||||
2601 | switch (Name.getNameKind()) { | |||
2602 | case DeclarationName::CXXOperatorName: { | |||
2603 | const char *OperatorName = nullptr; | |||
2604 | switch (Name.getCXXOverloadedOperator()) { | |||
2605 | case OO_None: | |||
2606 | case OO_Conditional: | |||
2607 | case NUM_OVERLOADED_OPERATORS: | |||
2608 | OperatorName = "operator"; | |||
2609 | break; | |||
2610 | ||||
2611 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ | |||
2612 | case OO_##Name: OperatorName = "operator" Spelling; break; | |||
2613 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) | |||
2614 | #include "clang/Basic/OperatorKinds.def" | |||
2615 | ||||
2616 | case OO_New: OperatorName = "operator new"; break; | |||
2617 | case OO_Delete: OperatorName = "operator delete"; break; | |||
2618 | case OO_Array_New: OperatorName = "operator new[]"; break; | |||
2619 | case OO_Array_Delete: OperatorName = "operator delete[]"; break; | |||
2620 | case OO_Call: OperatorName = "operator()"; break; | |||
2621 | case OO_Subscript: OperatorName = "operator[]"; break; | |||
2622 | } | |||
2623 | Result.AddTypedTextChunk(OperatorName); | |||
2624 | break; | |||
2625 | } | |||
2626 | ||||
2627 | case DeclarationName::Identifier: | |||
2628 | case DeclarationName::CXXConversionFunctionName: | |||
2629 | case DeclarationName::CXXDestructorName: | |||
2630 | case DeclarationName::CXXLiteralOperatorName: | |||
2631 | Result.AddTypedTextChunk( | |||
2632 | Result.getAllocator().CopyString(ND->getNameAsString())); | |||
2633 | break; | |||
2634 | ||||
2635 | case DeclarationName::CXXDeductionGuideName: | |||
2636 | case DeclarationName::CXXUsingDirective: | |||
2637 | case DeclarationName::ObjCZeroArgSelector: | |||
2638 | case DeclarationName::ObjCOneArgSelector: | |||
2639 | case DeclarationName::ObjCMultiArgSelector: | |||
2640 | break; | |||
2641 | ||||
2642 | case DeclarationName::CXXConstructorName: { | |||
2643 | CXXRecordDecl *Record = nullptr; | |||
2644 | QualType Ty = Name.getCXXNameType(); | |||
2645 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) | |||
2646 | Record = cast<CXXRecordDecl>(RecordTy->getDecl()); | |||
2647 | else if (const InjectedClassNameType *InjectedTy | |||
2648 | = Ty->getAs<InjectedClassNameType>()) | |||
2649 | Record = InjectedTy->getDecl(); | |||
2650 | else { | |||
2651 | Result.AddTypedTextChunk( | |||
2652 | Result.getAllocator().CopyString(ND->getNameAsString())); | |||
2653 | break; | |||
2654 | } | |||
2655 | ||||
2656 | Result.AddTypedTextChunk( | |||
2657 | Result.getAllocator().CopyString(Record->getNameAsString())); | |||
2658 | if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) { | |||
2659 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); | |||
2660 | AddTemplateParameterChunks(Context, Policy, Template, Result); | |||
2661 | Result.AddChunk(CodeCompletionString::CK_RightAngle); | |||
2662 | } | |||
2663 | break; | |||
2664 | } | |||
2665 | } | |||
2666 | } | |||
2667 | ||||
2668 | CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S, | |||
2669 | const CodeCompletionContext &CCContext, | |||
2670 | CodeCompletionAllocator &Allocator, | |||
2671 | CodeCompletionTUInfo &CCTUInfo, | |||
2672 | bool IncludeBriefComments) { | |||
2673 | return CreateCodeCompletionString(S.Context, S.PP, CCContext, Allocator, | |||
2674 | CCTUInfo, IncludeBriefComments); | |||
2675 | } | |||
2676 | ||||
2677 | /// \brief If possible, create a new code completion string for the given | |||
2678 | /// result. | |||
2679 | /// | |||
2680 | /// \returns Either a new, heap-allocated code completion string describing | |||
2681 | /// how to use this result, or NULL to indicate that the string or name of the | |||
2682 | /// result is all that is needed. | |||
2683 | CodeCompletionString * | |||
2684 | CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx, | |||
2685 | Preprocessor &PP, | |||
2686 | const CodeCompletionContext &CCContext, | |||
2687 | CodeCompletionAllocator &Allocator, | |||
2688 | CodeCompletionTUInfo &CCTUInfo, | |||
2689 | bool IncludeBriefComments) { | |||
2690 | CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability); | |||
2691 | ||||
2692 | PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP); | |||
2693 | if (Kind == RK_Pattern) { | |||
2694 | Pattern->Priority = Priority; | |||
2695 | Pattern->Availability = Availability; | |||
2696 | ||||
2697 | if (Declaration) { | |||
2698 | Result.addParentContext(Declaration->getDeclContext()); | |||
2699 | Pattern->ParentName = Result.getParentName(); | |||
2700 | // Provide code completion comment for self.GetterName where | |||
2701 | // GetterName is the getter method for a property with name | |||
2702 | // different from the property name (declared via a property | |||
2703 | // getter attribute. | |||
2704 | const NamedDecl *ND = Declaration; | |||
2705 | if (const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND)) | |||
2706 | if (M->isPropertyAccessor()) | |||
2707 | if (const ObjCPropertyDecl *PDecl = M->findPropertyDecl()) | |||
2708 | if (PDecl->getGetterName() == M->getSelector() && | |||
2709 | PDecl->getIdentifier() != M->getIdentifier()) { | |||
2710 | if (const RawComment *RC = | |||
2711 | Ctx.getRawCommentForAnyRedecl(M)) { | |||
2712 | Result.addBriefComment(RC->getBriefText(Ctx)); | |||
2713 | Pattern->BriefComment = Result.getBriefComment(); | |||
2714 | } | |||
2715 | else if (const RawComment *RC = | |||
2716 | Ctx.getRawCommentForAnyRedecl(PDecl)) { | |||
2717 | Result.addBriefComment(RC->getBriefText(Ctx)); | |||
2718 | Pattern->BriefComment = Result.getBriefComment(); | |||
2719 | } | |||
2720 | } | |||
2721 | } | |||
2722 | ||||
2723 | return Pattern; | |||
2724 | } | |||
2725 | ||||
2726 | if (Kind == RK_Keyword) { | |||
2727 | Result.AddTypedTextChunk(Keyword); | |||
2728 | return Result.TakeString(); | |||
2729 | } | |||
2730 | ||||
2731 | if (Kind == RK_Macro) { | |||
2732 | const MacroInfo *MI = PP.getMacroInfo(Macro); | |||
2733 | Result.AddTypedTextChunk( | |||
2734 | Result.getAllocator().CopyString(Macro->getName())); | |||
2735 | ||||
2736 | if (!MI || !MI->isFunctionLike()) | |||
2737 | return Result.TakeString(); | |||
2738 | ||||
2739 | // Format a function-like macro with placeholders for the arguments. | |||
2740 | Result.AddChunk(CodeCompletionString::CK_LeftParen); | |||
2741 | MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end(); | |||
2742 | ||||
2743 | // C99 variadic macros add __VA_ARGS__ at the end. Skip it. | |||
2744 | if (MI->isC99Varargs()) { | |||
2745 | --AEnd; | |||
2746 | ||||
2747 | if (A == AEnd) { | |||
2748 | Result.AddPlaceholderChunk("..."); | |||
2749 | } | |||
2750 | } | |||
2751 | ||||
2752 | for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) { | |||
2753 | if (A != MI->arg_begin()) | |||
2754 | Result.AddChunk(CodeCompletionString::CK_Comma); | |||
2755 | ||||
2756 | if (MI->isVariadic() && (A+1) == AEnd) { | |||
2757 | SmallString<32> Arg = (*A)->getName(); | |||
2758 | if (MI->isC99Varargs()) | |||
2759 | Arg += ", ..."; | |||
2760 | else | |||
2761 | Arg += "..."; | |||
2762 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); | |||
2763 | break; | |||
2764 | } | |||
2765 | ||||
2766 | // Non-variadic macros are simple. | |||
2767 | Result.AddPlaceholderChunk( | |||
2768 | Result.getAllocator().CopyString((*A)->getName())); | |||
2769 | } | |||
2770 | Result.AddChunk(CodeCompletionString::CK_RightParen); | |||
2771 | return Result.TakeString(); | |||
2772 | } | |||
2773 | ||||
2774 | assert(Kind == RK_Declaration && "Missed a result kind?")((Kind == RK_Declaration && "Missed a result kind?") ? static_cast<void> (0) : __assert_fail ("Kind == RK_Declaration && \"Missed a result kind?\"" , "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/clang/lib/Sema/SemaCodeComplete.cpp" , 2774, __PRETTY_FUNCTION__)); | |||
2775 | const NamedDecl *ND = Declaration; | |||
2776 | Result.addParentContext(ND->getDeclContext()); | |||
2777 | ||||
2778 | if (IncludeBriefComments) { | |||
2779 | // Add documentation comment, if it exists. | |||
2780 | if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) { | |||
2781 | Result.addBriefComment(RC->getBriefText(Ctx)); | |||
2782 | } | |||
2783 | else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) | |||
2784 | if (OMD->isPropertyAccessor()) | |||
2785 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) | |||
2786 | if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(PDecl)) | |||
2787 | Result.addBriefComment(RC->getBriefText(Ctx)); | |||
2788 | } | |||
2789 | ||||
2790 | if (StartsNestedNameSpecifier) { | |||
2791 | Result.AddTypedTextChunk( | |||
2792 | Result.getAllocator().CopyString(ND->getNameAsString())); | |||
2793 | Result.AddTextChunk("::"); | |||
2794 | return Result.TakeString(); | |||
2795 | } | |||
2796 | ||||
2797 | for (const auto *I : ND->specific_attrs<AnnotateAttr>()) | |||
2798 | Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); | |||
2799 | ||||
2800 | AddResultTypeChunk(Ctx, Policy, ND, CCContext.getBaseType(), Result); | |||
2801 | ||||
2802 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) { | |||
2803 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, | |||
2804 | Ctx, Policy); | |||
2805 | AddTypedNameChunk(Ctx, Policy, ND, Result); | |||
2806 | Result.AddChunk(CodeCompletionString::CK_LeftParen); | |||
2807 | AddFunctionParameterChunks(PP, Policy, Function, Result); | |||
2808 | Result.AddChunk(CodeCompletionString::CK_RightParen); | |||
2809 | AddFunctionTypeQualsToCompletionString(Result, Function); | |||
2810 | return Result.TakeString(); | |||
2811 | } | |||
2812 | ||||
2813 | if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) { | |||
2814 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, | |||
2815 | Ctx, Policy); | |||
2816 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); | |||
2817 | AddTypedNameChunk(Ctx, Policy, Function, Result); | |||
2818 | ||||
2819 | // Figure out which template parameters are deduced (or have default | |||
2820 | // arguments). | |||
2821 | llvm::SmallBitVector Deduced; | |||
2822 | Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced); | |||
2823 | unsigned LastDeducibleArgument; | |||
2824 | for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0; | |||
2825 | --LastDeducibleArgument) { | |||
2826 | if (!Deduced[LastDeducibleArgument - 1]) { | |||
2827 | // C++0x: Figure out if the template argument has a default. If so, | |||
2828 | // the user doesn't need to type this argument. | |||
2829 | // FIXME: We need to abstract template parameters better! | |||
2830 | bool HasDefaultArg = false; | |||
2831 | NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam( | |||
2832 | LastDeducibleArgument - 1); | |||
2833 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) | |||
2834 | HasDefaultArg = TTP->hasDefaultArgument(); | |||
2835 | else if (NonTypeTemplateParmDecl *NTTP | |||
2836 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) | |||
2837 | HasDefaultArg = NTTP->hasDefaultArgument(); | |||
2838 | else { | |||
2839 | assert(isa<TemplateTemplateParmDecl>(Param))((isa<TemplateTemplateParmDecl>(Param)) ? static_cast< void> (0) : __assert_fail ("isa<TemplateTemplateParmDecl>(Param)" , "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/clang/lib/Sema/SemaCodeComplete.cpp" , 2839, __PRETTY_FUNCTION__)); | |||
2840 | HasDefaultArg | |||
2841 | = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument(); | |||
2842 | } | |||
2843 | ||||
2844 | if (!HasDefaultArg) | |||
2845 | break; | |||
2846 | } | |||
2847 | } | |||
2848 | ||||
2849 | if (LastDeducibleArgument) { | |||
2850 | // Some of the function template arguments cannot be deduced from a | |||
2851 | // function call, so we introduce an explicit template argument list | |||
2852 | // containing all of the arguments up to the first deducible argument. | |||
2853 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); | |||
2854 | AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result, | |||
2855 | LastDeducibleArgument); | |||
2856 | Result.AddChunk(CodeCompletionString::CK_RightAngle); | |||
2857 | } | |||
2858 | ||||
2859 | // Add the function parameters | |||
2860 | Result.AddChunk(CodeCompletionString::CK_LeftParen); | |||
2861 | AddFunctionParameterChunks(PP, Policy, Function, Result); | |||
2862 | Result.AddChunk(CodeCompletionString::CK_RightParen); | |||
2863 | AddFunctionTypeQualsToCompletionString(Result, Function); | |||
2864 | return Result.TakeString(); | |||
2865 | } | |||
2866 | ||||
2867 | if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) { | |||
2868 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, | |||
2869 | Ctx, Policy); | |||
2870 | Result.AddTypedTextChunk( | |||
2871 | Result.getAllocator().CopyString(Template->getNameAsString())); | |||
2872 | Result.AddChunk(CodeCompletionString::CK_LeftAngle); | |||
2873 | AddTemplateParameterChunks(Ctx, Policy, Template, Result); | |||
2874 | Result.AddChunk(CodeCompletionString::CK_RightAngle); | |||
2875 | return Result.TakeString(); | |||
2876 | } | |||
2877 | ||||
2878 | if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) { | |||
2879 | Selector Sel = Method->getSelector(); | |||
2880 | if (Sel.isUnarySelector()) { | |||
2881 | Result.AddTypedTextChunk(Result.getAllocator().CopyString( | |||
2882 | Sel.getNameForSlot(0))); | |||
2883 | return Result.TakeString(); | |||
2884 | } | |||
2885 | ||||
2886 | std::string SelName = Sel.getNameForSlot(0).str(); | |||
2887 | SelName += ':'; | |||
2888 | if (StartParameter == 0) | |||
2889 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName)); | |||
2890 | else { | |||
2891 | Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName)); | |||
2892 | ||||
2893 | // If there is only one parameter, and we're past it, add an empty | |||
2894 | // typed-text chunk since there is nothing to type. | |||
2895 | if (Method->param_size() == 1) | |||
2896 | Result.AddTypedTextChunk(""); | |||
2897 | } | |||
2898 | unsigned Idx = 0; | |||
2899 | for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(), | |||
2900 | PEnd = Method->param_end(); | |||
2901 | P != PEnd; (void)++P, ++Idx) { | |||
2902 | if (Idx > 0) { | |||
2903 | std::string Keyword; | |||
2904 | if (Idx > StartParameter) | |||
2905 | Result.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
2906 | if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx)) | |||
2907 | Keyword += II->getName(); | |||
2908 | Keyword += ":"; | |||
2909 | if (Idx < StartParameter || AllParametersAreInformative) | |||
2910 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword)); | |||
2911 | else | |||
2912 | Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword)); | |||
2913 | } | |||
2914 | ||||
2915 | // If we're before the starting parameter, skip the placeholder. | |||
2916 | if (Idx < StartParameter) | |||
2917 | continue; | |||
2918 | ||||
2919 | std::string Arg; | |||
2920 | QualType ParamType = (*P)->getType(); | |||
2921 | Optional<ArrayRef<QualType>> ObjCSubsts; | |||
2922 | if (!CCContext.getBaseType().isNull()) | |||
2923 | ObjCSubsts = CCContext.getBaseType()->getObjCSubstitutions(Method); | |||
2924 | ||||
2925 | if (ParamType->isBlockPointerType() && !DeclaringEntity) | |||
2926 | Arg = FormatFunctionParameter(Policy, *P, true, | |||
2927 | /*SuppressBlock=*/false, | |||
2928 | ObjCSubsts); | |||
2929 | else { | |||
2930 | if (ObjCSubsts) | |||
2931 | ParamType = ParamType.substObjCTypeArgs(Ctx, *ObjCSubsts, | |||
2932 | ObjCSubstitutionContext::Parameter); | |||
2933 | Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier(), | |||
2934 | ParamType); | |||
2935 | Arg += ParamType.getAsString(Policy) + ")"; | |||
2936 | if (IdentifierInfo *II = (*P)->getIdentifier()) | |||
2937 | if (DeclaringEntity || AllParametersAreInformative) | |||
2938 | Arg += II->getName(); | |||
2939 | } | |||
2940 | ||||
2941 | if (Method->isVariadic() && (P + 1) == PEnd) | |||
2942 | Arg += ", ..."; | |||
2943 | ||||
2944 | if (DeclaringEntity) | |||
2945 | Result.AddTextChunk(Result.getAllocator().CopyString(Arg)); | |||
2946 | else if (AllParametersAreInformative) | |||
2947 | Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg)); | |||
2948 | else | |||
2949 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg)); | |||
2950 | } | |||
2951 | ||||
2952 | if (Method->isVariadic()) { | |||
2953 | if (Method->param_size() == 0) { | |||
2954 | if (DeclaringEntity) | |||
2955 | Result.AddTextChunk(", ..."); | |||
2956 | else if (AllParametersAreInformative) | |||
2957 | Result.AddInformativeChunk(", ..."); | |||
2958 | else | |||
2959 | Result.AddPlaceholderChunk(", ..."); | |||
2960 | } | |||
2961 | ||||
2962 | MaybeAddSentinel(PP, Method, Result); | |||
2963 | } | |||
2964 | ||||
2965 | return Result.TakeString(); | |||
2966 | } | |||
2967 | ||||
2968 | if (Qualifier) | |||
2969 | AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative, | |||
2970 | Ctx, Policy); | |||
2971 | ||||
2972 | Result.AddTypedTextChunk( | |||
2973 | Result.getAllocator().CopyString(ND->getNameAsString())); | |||
2974 | return Result.TakeString(); | |||
2975 | } | |||
2976 | ||||
2977 | /// \brief Add function overload parameter chunks to the given code completion | |||
2978 | /// string. | |||
2979 | static void AddOverloadParameterChunks(ASTContext &Context, | |||
2980 | const PrintingPolicy &Policy, | |||
2981 | const FunctionDecl *Function, | |||
2982 | const FunctionProtoType *Prototype, | |||
2983 | CodeCompletionBuilder &Result, | |||
2984 | unsigned CurrentArg, | |||
2985 | unsigned Start = 0, | |||
2986 | bool InOptional = false) { | |||
2987 | bool FirstParameter = true; | |||
2988 | unsigned NumParams = Function ? Function->getNumParams() | |||
2989 | : Prototype->getNumParams(); | |||
2990 | ||||
2991 | for (unsigned P = Start; P != NumParams; ++P) { | |||
2992 | if (Function && Function->getParamDecl(P)->hasDefaultArg() && !InOptional) { | |||
2993 | // When we see an optional default argument, put that argument and | |||
2994 | // the remaining default arguments into a new, optional string. | |||
2995 | CodeCompletionBuilder Opt(Result.getAllocator(), | |||
2996 | Result.getCodeCompletionTUInfo()); | |||
2997 | if (!FirstParameter) | |||
2998 | Opt.AddChunk(CodeCompletionString::CK_Comma); | |||
2999 | // Optional sections are nested. | |||
3000 | AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt, | |||
3001 | CurrentArg, P, /*InOptional=*/true); | |||
3002 | Result.AddOptionalChunk(Opt.TakeString()); | |||
3003 | return; | |||
3004 | } | |||
3005 | ||||
3006 | if (FirstParameter) | |||
3007 | FirstParameter = false; | |||
3008 | else | |||
3009 | Result.AddChunk(CodeCompletionString::CK_Comma); | |||
3010 | ||||
3011 | InOptional = false; | |||
3012 | ||||
3013 | // Format the placeholder string. | |||
3014 | std::string Placeholder; | |||
3015 | if (Function) | |||
3016 | Placeholder = FormatFunctionParameter(Policy, Function->getParamDecl(P)); | |||
3017 | else | |||
3018 | Placeholder = Prototype->getParamType(P).getAsString(Policy); | |||
3019 | ||||
3020 | if (P == CurrentArg) | |||
3021 | Result.AddCurrentParameterChunk( | |||
3022 | Result.getAllocator().CopyString(Placeholder)); | |||
3023 | else | |||
3024 | Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder)); | |||
3025 | } | |||
3026 | ||||
3027 | if (Prototype && Prototype->isVariadic()) { | |||
3028 | CodeCompletionBuilder Opt(Result.getAllocator(), | |||
3029 | Result.getCodeCompletionTUInfo()); | |||
3030 | if (!FirstParameter) | |||
3031 | Opt.AddChunk(CodeCompletionString::CK_Comma); | |||
3032 | ||||
3033 | if (CurrentArg < NumParams) | |||
3034 | Opt.AddPlaceholderChunk("..."); | |||
3035 | else | |||
3036 | Opt.AddCurrentParameterChunk("..."); | |||
3037 | ||||
3038 | Result.AddOptionalChunk(Opt.TakeString()); | |||
3039 | } | |||
3040 | } | |||
3041 | ||||
3042 | CodeCompletionString * | |||
3043 | CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( | |||
3044 | unsigned CurrentArg, Sema &S, | |||
3045 | CodeCompletionAllocator &Allocator, | |||
3046 | CodeCompletionTUInfo &CCTUInfo, | |||
3047 | bool IncludeBriefComments) const { | |||
3048 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); | |||
3049 | ||||
3050 | // FIXME: Set priority, availability appropriately. | |||
3051 | CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available); | |||
3052 | FunctionDecl *FDecl = getFunction(); | |||
3053 | const FunctionProtoType *Proto | |||
3054 | = dyn_cast<FunctionProtoType>(getFunctionType()); | |||
3055 | if (!FDecl && !Proto) { | |||
3056 | // Function without a prototype. Just give the return type and a | |||
3057 | // highlighted ellipsis. | |||
3058 | const FunctionType *FT = getFunctionType(); | |||
3059 | Result.AddResultTypeChunk(Result.getAllocator().CopyString( | |||
3060 | FT->getReturnType().getAsString(Policy))); | |||
3061 | Result.AddChunk(CodeCompletionString::CK_LeftParen); | |||
3062 | Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); | |||
3063 | Result.AddChunk(CodeCompletionString::CK_RightParen); | |||
3064 | return Result.TakeString(); | |||
3065 | } | |||
3066 | ||||
3067 | if (FDecl) { | |||
3068 | if (IncludeBriefComments && CurrentArg < FDecl->getNumParams()) | |||
3069 | if (auto RC = S.getASTContext().getRawCommentForAnyRedecl( | |||
3070 | FDecl->getParamDecl(CurrentArg))) | |||
3071 | Result.addBriefComment(RC->getBriefText(S.getASTContext())); | |||
3072 | AddResultTypeChunk(S.Context, Policy, FDecl, QualType(), Result); | |||
3073 | Result.AddTextChunk( | |||
3074 | Result.getAllocator().CopyString(FDecl->getNameAsString())); | |||
3075 | } else { | |||
3076 | Result.AddResultTypeChunk( | |||
3077 | Result.getAllocator().CopyString( | |||
3078 | Proto->getReturnType().getAsString(Policy))); | |||
3079 | } | |||
3080 | ||||
3081 | Result.AddChunk(CodeCompletionString::CK_LeftParen); | |||
3082 | AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, | |||
3083 | CurrentArg); | |||
3084 | Result.AddChunk(CodeCompletionString::CK_RightParen); | |||
3085 | ||||
3086 | return Result.TakeString(); | |||
3087 | } | |||
3088 | ||||
3089 | unsigned clang::getMacroUsagePriority(StringRef MacroName, | |||
3090 | const LangOptions &LangOpts, | |||
3091 | bool PreferredTypeIsPointer) { | |||
3092 | unsigned Priority = CCP_Macro; | |||
3093 | ||||
3094 | // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. | |||
3095 | if (MacroName.equals("nil") || MacroName.equals("NULL") || | |||
3096 | MacroName.equals("Nil")) { | |||
3097 | Priority = CCP_Constant; | |||
3098 | if (PreferredTypeIsPointer) | |||
3099 | Priority = Priority / CCF_SimilarTypeMatch; | |||
3100 | } | |||
3101 | // Treat "YES", "NO", "true", and "false" as constants. | |||
3102 | else if (MacroName.equals("YES") || MacroName.equals("NO") || | |||
3103 | MacroName.equals("true") || MacroName.equals("false")) | |||
3104 | Priority = CCP_Constant; | |||
3105 | // Treat "bool" as a type. | |||
3106 | else if (MacroName.equals("bool")) | |||
3107 | Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); | |||
3108 | ||||
3109 | ||||
3110 | return Priority; | |||
3111 | } | |||
3112 | ||||
3113 | CXCursorKind clang::getCursorKindForDecl(const Decl *D) { | |||
3114 | if (!D) | |||
3115 | return CXCursor_UnexposedDecl; | |||
3116 | ||||
3117 | switch (D->getKind()) { | |||
3118 | case Decl::Enum: return CXCursor_EnumDecl; | |||
3119 | case Decl::EnumConstant: return CXCursor_EnumConstantDecl; | |||
3120 | case Decl::Field: return CXCursor_FieldDecl; | |||
3121 | case Decl::Function: | |||
3122 | return CXCursor_FunctionDecl; | |||
3123 | case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; | |||
3124 | case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; | |||
3125 | case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; | |||
3126 | ||||
3127 | case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; | |||
3128 | case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; | |||
3129 | case Decl::ObjCMethod: | |||
3130 | return cast<ObjCMethodDecl>(D)->isInstanceMethod() | |||
3131 | ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl; | |||
3132 | case Decl::CXXMethod: return CXCursor_CXXMethod; | |||
3133 | case Decl::CXXConstructor: return CXCursor_Constructor; | |||
3134 | case Decl::CXXDestructor: return CXCursor_Destructor; | |||
3135 | case Decl::CXXConversion: return CXCursor_ConversionFunction; | |||
3136 | case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl; | |||
3137 | case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; | |||
3138 | case Decl::ParmVar: return CXCursor_ParmDecl; | |||
3139 | case Decl::Typedef: return CXCursor_TypedefDecl; | |||
3140 | case Decl::TypeAlias: return CXCursor_TypeAliasDecl; | |||
3141 | case Decl::TypeAliasTemplate: return CXCursor_TypeAliasTemplateDecl; | |||
3142 | case Decl::Var: return CXCursor_VarDecl; | |||
3143 | case Decl::Namespace: return CXCursor_Namespace; | |||
3144 | case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; | |||
3145 | case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; | |||
3146 | case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; | |||
3147 | case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; | |||
3148 | case Decl::FunctionTemplate: return CXCursor_FunctionTemplate; | |||
3149 | case Decl::ClassTemplate: return CXCursor_ClassTemplate; | |||
3150 | case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier; | |||
3151 | case Decl::ClassTemplatePartialSpecialization: | |||
3152 | return CXCursor_ClassTemplatePartialSpecialization; | |||
3153 | case Decl::UsingDirective: return CXCursor_UsingDirective; | |||
3154 | case Decl::StaticAssert: return CXCursor_StaticAssert; | |||
3155 | case Decl::Friend: return CXCursor_FriendDecl; | |||
3156 | case Decl::TranslationUnit: return CXCursor_TranslationUnit; | |||
3157 | ||||
3158 | case Decl::Using: | |||
3159 | case Decl::UnresolvedUsingValue: | |||
3160 | case Decl::UnresolvedUsingTypename: | |||
3161 | return CXCursor_UsingDeclaration; | |||
3162 | ||||
3163 | case Decl::ObjCPropertyImpl: | |||
3164 | switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) { | |||
3165 | case ObjCPropertyImplDecl::Dynamic: | |||
3166 | return CXCursor_ObjCDynamicDecl; | |||
3167 | ||||
3168 | case ObjCPropertyImplDecl::Synthesize: | |||
3169 | return CXCursor_ObjCSynthesizeDecl; | |||
3170 | } | |||
3171 | ||||
3172 | case Decl::Import: | |||
3173 | return CXCursor_ModuleImportDecl; | |||
3174 | ||||
3175 | case Decl::ObjCTypeParam: return CXCursor_TemplateTypeParameter; | |||
3176 | ||||
3177 | default: | |||
3178 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { | |||
3179 | switch (TD->getTagKind()) { | |||
3180 | case TTK_Interface: // fall through | |||
3181 | case TTK_Struct: return CXCursor_StructDecl; | |||
3182 | case TTK_Class: return CXCursor_ClassDecl; | |||
3183 | case TTK_Union: return CXCursor_UnionDecl; | |||
3184 | case TTK_Enum: return CXCursor_EnumDecl; | |||
3185 | } | |||
3186 | } | |||
3187 | } | |||
3188 | ||||
3189 | return CXCursor_UnexposedDecl; | |||
3190 | } | |||
3191 | ||||
3192 | static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, | |||
3193 | bool IncludeUndefined, | |||
3194 | bool TargetTypeIsPointer = false) { | |||
3195 | typedef CodeCompletionResult Result; | |||
3196 | ||||
3197 | Results.EnterNewScope(); | |||
3198 | ||||
3199 | for (Preprocessor::macro_iterator M = PP.macro_begin(), | |||
3200 | MEnd = PP.macro_end(); | |||
3201 | M != MEnd; ++M) { | |||
3202 | auto MD = PP.getMacroDefinition(M->first); | |||
3203 | if (IncludeUndefined || MD) { | |||
3204 | if (MacroInfo *MI = MD.getMacroInfo()) | |||
3205 | if (MI->isUsedForHeaderGuard()) | |||
3206 | continue; | |||
3207 | ||||
3208 | Results.AddResult(Result(M->first, | |||
3209 | getMacroUsagePriority(M->first->getName(), | |||
3210 | PP.getLangOpts(), | |||
3211 | TargetTypeIsPointer))); | |||
3212 | } | |||
3213 | } | |||
3214 | ||||
3215 | Results.ExitScope(); | |||
3216 | ||||
3217 | } | |||
3218 | ||||
3219 | static void AddPrettyFunctionResults(const LangOptions &LangOpts, | |||
3220 | ResultBuilder &Results) { | |||
3221 | typedef CodeCompletionResult Result; | |||
3222 | ||||
3223 | Results.EnterNewScope(); | |||
3224 | ||||
3225 | Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant)); | |||
3226 | Results.AddResult(Result("__FUNCTION__", CCP_Constant)); | |||
3227 | if (LangOpts.C99 || LangOpts.CPlusPlus11) | |||
3228 | Results.AddResult(Result("__func__", CCP_Constant)); | |||
3229 | Results.ExitScope(); | |||
3230 | } | |||
3231 | ||||
3232 | static void HandleCodeCompleteResults(Sema *S, | |||
3233 | CodeCompleteConsumer *CodeCompleter, | |||
3234 | CodeCompletionContext Context, | |||
3235 | CodeCompletionResult *Results, | |||
3236 | unsigned NumResults) { | |||
3237 | if (CodeCompleter) | |||
3238 | CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults); | |||
3239 | } | |||
3240 | ||||
3241 | static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S, | |||
3242 | Sema::ParserCompletionContext PCC) { | |||
3243 | switch (PCC) { | |||
3244 | case Sema::PCC_Namespace: | |||
3245 | return CodeCompletionContext::CCC_TopLevel; | |||
3246 | ||||
3247 | case Sema::PCC_Class: | |||
3248 | return CodeCompletionContext::CCC_ClassStructUnion; | |||
3249 | ||||
3250 | case Sema::PCC_ObjCInterface: | |||
3251 | return CodeCompletionContext::CCC_ObjCInterface; | |||
3252 | ||||
3253 | case Sema::PCC_ObjCImplementation: | |||
3254 | return CodeCompletionContext::CCC_ObjCImplementation; | |||
3255 | ||||
3256 | case Sema::PCC_ObjCInstanceVariableList: | |||
3257 | return CodeCompletionContext::CCC_ObjCIvarList; | |||
3258 | ||||
3259 | case Sema::PCC_Template: | |||
3260 | case Sema::PCC_MemberTemplate: | |||
3261 | if (S.CurContext->isFileContext()) | |||
3262 | return CodeCompletionContext::CCC_TopLevel; | |||
3263 | if (S.CurContext->isRecord()) | |||
3264 | return CodeCompletionContext::CCC_ClassStructUnion; | |||
3265 | return CodeCompletionContext::CCC_Other; | |||
3266 | ||||
3267 | case Sema::PCC_RecoveryInFunction: | |||
3268 | return CodeCompletionContext::CCC_Recovery; | |||
3269 | ||||
3270 | case Sema::PCC_ForInit: | |||
3271 | if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 || | |||
3272 | S.getLangOpts().ObjC1) | |||
3273 | return CodeCompletionContext::CCC_ParenthesizedExpression; | |||
3274 | else | |||
3275 | return CodeCompletionContext::CCC_Expression; | |||
3276 | ||||
3277 | case Sema::PCC_Expression: | |||
3278 | case Sema::PCC_Condition: | |||
3279 | return CodeCompletionContext::CCC_Expression; | |||
3280 | ||||
3281 | case Sema::PCC_Statement: | |||
3282 | return CodeCompletionContext::CCC_Statement; | |||
3283 | ||||
3284 | case Sema::PCC_Type: | |||
3285 | return CodeCompletionContext::CCC_Type; | |||
3286 | ||||
3287 | case Sema::PCC_ParenthesizedExpression: | |||
3288 | return CodeCompletionContext::CCC_ParenthesizedExpression; | |||
3289 | ||||
3290 | case Sema::PCC_LocalDeclarationSpecifiers: | |||
3291 | return CodeCompletionContext::CCC_Type; | |||
3292 | } | |||
3293 | ||||
3294 | llvm_unreachable("Invalid ParserCompletionContext!")::llvm::llvm_unreachable_internal("Invalid ParserCompletionContext!" , "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/clang/lib/Sema/SemaCodeComplete.cpp" , 3294); | |||
3295 | } | |||
3296 | ||||
3297 | /// \brief If we're in a C++ virtual member function, add completion results | |||
3298 | /// that invoke the functions we override, since it's common to invoke the | |||
3299 | /// overridden function as well as adding new functionality. | |||
3300 | /// | |||
3301 | /// \param S The semantic analysis object for which we are generating results. | |||
3302 | /// | |||
3303 | /// \param InContext This context in which the nested-name-specifier preceding | |||
3304 | /// the code-completion point | |||
3305 | static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, | |||
3306 | ResultBuilder &Results) { | |||
3307 | // Look through blocks. | |||
3308 | DeclContext *CurContext = S.CurContext; | |||
3309 | while (isa<BlockDecl>(CurContext)) | |||
3310 | CurContext = CurContext->getParent(); | |||
3311 | ||||
3312 | ||||
3313 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext); | |||
3314 | if (!Method || !Method->isVirtual()) | |||
3315 | return; | |||
3316 | ||||
3317 | // We need to have names for all of the parameters, if we're going to | |||
3318 | // generate a forwarding call. | |||
3319 | for (auto P : Method->parameters()) | |||
3320 | if (!P->getDeclName()) | |||
3321 | return; | |||
3322 | ||||
3323 | PrintingPolicy Policy = getCompletionPrintingPolicy(S); | |||
3324 | for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(), | |||
3325 | MEnd = Method->end_overridden_methods(); | |||
3326 | M != MEnd; ++M) { | |||
3327 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
3328 | Results.getCodeCompletionTUInfo()); | |||
3329 | const CXXMethodDecl *Overridden = *M; | |||
3330 | if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl()) | |||
3331 | continue; | |||
3332 | ||||
3333 | // If we need a nested-name-specifier, add one now. | |||
3334 | if (!InContext) { | |||
3335 | NestedNameSpecifier *NNS | |||
3336 | = getRequiredQualification(S.Context, CurContext, | |||
3337 | Overridden->getDeclContext()); | |||
3338 | if (NNS) { | |||
3339 | std::string Str; | |||
3340 | llvm::raw_string_ostream OS(Str); | |||
3341 | NNS->print(OS, Policy); | |||
3342 | Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str())); | |||
3343 | } | |||
3344 | } else if (!InContext->Equals(Overridden->getDeclContext())) | |||
3345 | continue; | |||
3346 | ||||
3347 | Builder.AddTypedTextChunk(Results.getAllocator().CopyString( | |||
3348 | Overridden->getNameAsString())); | |||
3349 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
3350 | bool FirstParam = true; | |||
3351 | for (auto P : Method->parameters()) { | |||
3352 | if (FirstParam) | |||
3353 | FirstParam = false; | |||
3354 | else | |||
3355 | Builder.AddChunk(CodeCompletionString::CK_Comma); | |||
3356 | ||||
3357 | Builder.AddPlaceholderChunk( | |||
3358 | Results.getAllocator().CopyString(P->getIdentifier()->getName())); | |||
3359 | } | |||
3360 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
3361 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), | |||
3362 | CCP_SuperCompletion, | |||
3363 | CXCursor_CXXMethod, | |||
3364 | CXAvailability_Available, | |||
3365 | Overridden)); | |||
3366 | Results.Ignore(Overridden); | |||
3367 | } | |||
3368 | } | |||
3369 | ||||
3370 | void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc, | |||
3371 | ModuleIdPath Path) { | |||
3372 | typedef CodeCompletionResult Result; | |||
3373 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
3374 | CodeCompleter->getCodeCompletionTUInfo(), | |||
3375 | CodeCompletionContext::CCC_Other); | |||
3376 | Results.EnterNewScope(); | |||
3377 | ||||
3378 | CodeCompletionAllocator &Allocator = Results.getAllocator(); | |||
3379 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); | |||
3380 | typedef CodeCompletionResult Result; | |||
3381 | if (Path.empty()) { | |||
3382 | // Enumerate all top-level modules. | |||
3383 | SmallVector<Module *, 8> Modules; | |||
3384 | PP.getHeaderSearchInfo().collectAllModules(Modules); | |||
3385 | for (unsigned I = 0, N = Modules.size(); I != N; ++I) { | |||
3386 | Builder.AddTypedTextChunk( | |||
3387 | Builder.getAllocator().CopyString(Modules[I]->Name)); | |||
3388 | Results.AddResult(Result(Builder.TakeString(), | |||
3389 | CCP_Declaration, | |||
3390 | CXCursor_ModuleImportDecl, | |||
3391 | Modules[I]->isAvailable() | |||
3392 | ? CXAvailability_Available | |||
3393 | : CXAvailability_NotAvailable)); | |||
3394 | } | |||
3395 | } else if (getLangOpts().Modules) { | |||
3396 | // Load the named module. | |||
3397 | Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path, | |||
3398 | Module::AllVisible, | |||
3399 | /*IsInclusionDirective=*/false); | |||
3400 | // Enumerate submodules. | |||
3401 | if (Mod) { | |||
3402 | for (Module::submodule_iterator Sub = Mod->submodule_begin(), | |||
3403 | SubEnd = Mod->submodule_end(); | |||
3404 | Sub != SubEnd; ++Sub) { | |||
3405 | ||||
3406 | Builder.AddTypedTextChunk( | |||
3407 | Builder.getAllocator().CopyString((*Sub)->Name)); | |||
3408 | Results.AddResult(Result(Builder.TakeString(), | |||
3409 | CCP_Declaration, | |||
3410 | CXCursor_ModuleImportDecl, | |||
3411 | (*Sub)->isAvailable() | |||
3412 | ? CXAvailability_Available | |||
3413 | : CXAvailability_NotAvailable)); | |||
3414 | } | |||
3415 | } | |||
3416 | } | |||
3417 | Results.ExitScope(); | |||
3418 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | |||
3419 | Results.data(),Results.size()); | |||
3420 | } | |||
3421 | ||||
3422 | void Sema::CodeCompleteOrdinaryName(Scope *S, | |||
3423 | ParserCompletionContext CompletionContext) { | |||
3424 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
3425 | CodeCompleter->getCodeCompletionTUInfo(), | |||
3426 | mapCodeCompletionContext(*this, CompletionContext)); | |||
3427 | Results.EnterNewScope(); | |||
3428 | ||||
3429 | // Determine how to filter results, e.g., so that the names of | |||
3430 | // values (functions, enumerators, function templates, etc.) are | |||
3431 | // only allowed where we can have an expression. | |||
3432 | switch (CompletionContext) { | |||
3433 | case PCC_Namespace: | |||
3434 | case PCC_Class: | |||
3435 | case PCC_ObjCInterface: | |||
3436 | case PCC_ObjCImplementation: | |||
3437 | case PCC_ObjCInstanceVariableList: | |||
3438 | case PCC_Template: | |||
3439 | case PCC_MemberTemplate: | |||
3440 | case PCC_Type: | |||
3441 | case PCC_LocalDeclarationSpecifiers: | |||
3442 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); | |||
3443 | break; | |||
3444 | ||||
3445 | case PCC_Statement: | |||
3446 | case PCC_ParenthesizedExpression: | |||
3447 | case PCC_Expression: | |||
3448 | case PCC_ForInit: | |||
3449 | case PCC_Condition: | |||
3450 | if (WantTypesInContext(CompletionContext, getLangOpts())) | |||
3451 | Results.setFilter(&ResultBuilder::IsOrdinaryName); | |||
3452 | else | |||
3453 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); | |||
3454 | ||||
3455 | if (getLangOpts().CPlusPlus) | |||
3456 | MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results); | |||
3457 | break; | |||
3458 | ||||
3459 | case PCC_RecoveryInFunction: | |||
3460 | // Unfiltered | |||
3461 | break; | |||
3462 | } | |||
3463 | ||||
3464 | // If we are in a C++ non-static member function, check the qualifiers on | |||
3465 | // the member function to filter/prioritize the results list. | |||
3466 | if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext)) | |||
3467 | if (CurMethod->isInstance()) | |||
3468 | Results.setObjectTypeQualifiers( | |||
3469 | Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers())); | |||
3470 | ||||
3471 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
3472 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | |||
3473 | CodeCompleter->includeGlobals()); | |||
3474 | ||||
3475 | AddOrdinaryNameResults(CompletionContext, S, *this, Results); | |||
3476 | Results.ExitScope(); | |||
3477 | ||||
3478 | switch (CompletionContext) { | |||
3479 | case PCC_ParenthesizedExpression: | |||
3480 | case PCC_Expression: | |||
3481 | case PCC_Statement: | |||
3482 | case PCC_RecoveryInFunction: | |||
3483 | if (S->getFnParent()) | |||
3484 | AddPrettyFunctionResults(getLangOpts(), Results); | |||
3485 | break; | |||
3486 | ||||
3487 | case PCC_Namespace: | |||
3488 | case PCC_Class: | |||
3489 | case PCC_ObjCInterface: | |||
3490 | case PCC_ObjCImplementation: | |||
3491 | case PCC_ObjCInstanceVariableList: | |||
3492 | case PCC_Template: | |||
3493 | case PCC_MemberTemplate: | |||
3494 | case PCC_ForInit: | |||
3495 | case PCC_Condition: | |||
3496 | case PCC_Type: | |||
3497 | case PCC_LocalDeclarationSpecifiers: | |||
3498 | break; | |||
3499 | } | |||
3500 | ||||
3501 | if (CodeCompleter->includeMacros()) | |||
3502 | AddMacroResults(PP, Results, false); | |||
3503 | ||||
3504 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | |||
3505 | Results.data(),Results.size()); | |||
3506 | } | |||
3507 | ||||
3508 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, | |||
3509 | ParsedType Receiver, | |||
3510 | ArrayRef<IdentifierInfo *> SelIdents, | |||
3511 | bool AtArgumentExpression, | |||
3512 | bool IsSuper, | |||
3513 | ResultBuilder &Results); | |||
3514 | ||||
3515 | void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS, | |||
3516 | bool AllowNonIdentifiers, | |||
3517 | bool AllowNestedNameSpecifiers) { | |||
3518 | typedef CodeCompletionResult Result; | |||
3519 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
3520 | CodeCompleter->getCodeCompletionTUInfo(), | |||
3521 | AllowNestedNameSpecifiers | |||
3522 | ? CodeCompletionContext::CCC_PotentiallyQualifiedName | |||
3523 | : CodeCompletionContext::CCC_Name); | |||
3524 | Results.EnterNewScope(); | |||
3525 | ||||
3526 | // Type qualifiers can come after names. | |||
3527 | Results.AddResult(Result("const")); | |||
3528 | Results.AddResult(Result("volatile")); | |||
3529 | if (getLangOpts().C99) | |||
3530 | Results.AddResult(Result("restrict")); | |||
3531 | ||||
3532 | if (getLangOpts().CPlusPlus) { | |||
3533 | if (getLangOpts().CPlusPlus11 && | |||
3534 | (DS.getTypeSpecType() == DeclSpec::TST_class || | |||
3535 | DS.getTypeSpecType() == DeclSpec::TST_struct)) | |||
3536 | Results.AddResult("final"); | |||
3537 | ||||
3538 | if (AllowNonIdentifiers) { | |||
3539 | Results.AddResult(Result("operator")); | |||
3540 | } | |||
3541 | ||||
3542 | // Add nested-name-specifiers. | |||
3543 | if (AllowNestedNameSpecifiers) { | |||
3544 | Results.allowNestedNameSpecifiers(); | |||
3545 | Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy); | |||
3546 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
3547 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer, | |||
3548 | CodeCompleter->includeGlobals()); | |||
3549 | Results.setFilter(nullptr); | |||
3550 | } | |||
3551 | } | |||
3552 | Results.ExitScope(); | |||
3553 | ||||
3554 | // If we're in a context where we might have an expression (rather than a | |||
3555 | // declaration), and what we've seen so far is an Objective-C type that could | |||
3556 | // be a receiver of a class message, this may be a class message send with | |||
3557 | // the initial opening bracket '[' missing. Add appropriate completions. | |||
3558 | if (AllowNonIdentifiers && !AllowNestedNameSpecifiers && | |||
3559 | DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier && | |||
3560 | DS.getTypeSpecType() == DeclSpec::TST_typename && | |||
3561 | DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified && | |||
3562 | DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && | |||
3563 | !DS.isTypeAltiVecVector() && | |||
3564 | S && | |||
3565 | (S->getFlags() & Scope::DeclScope) != 0 && | |||
3566 | (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope | | |||
3567 | Scope::FunctionPrototypeScope | | |||
3568 | Scope::AtCatchScope)) == 0) { | |||
3569 | ParsedType T = DS.getRepAsType(); | |||
3570 | if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType()) | |||
3571 | AddClassMessageCompletions(*this, S, T, None, false, false, Results); | |||
3572 | } | |||
3573 | ||||
3574 | // Note that we intentionally suppress macro results here, since we do not | |||
3575 | // encourage using macros to produce the names of entities. | |||
3576 | ||||
3577 | HandleCodeCompleteResults(this, CodeCompleter, | |||
3578 | Results.getCompletionContext(), | |||
3579 | Results.data(), Results.size()); | |||
3580 | } | |||
3581 | ||||
3582 | struct Sema::CodeCompleteExpressionData { | |||
3583 | CodeCompleteExpressionData(QualType PreferredType = QualType()) | |||
3584 | : PreferredType(PreferredType), IntegralConstantExpression(false), | |||
3585 | ObjCCollection(false) { } | |||
3586 | ||||
3587 | QualType PreferredType; | |||
3588 | bool IntegralConstantExpression; | |||
3589 | bool ObjCCollection; | |||
3590 | SmallVector<Decl *, 4> IgnoreDecls; | |||
3591 | }; | |||
3592 | ||||
3593 | /// \brief Perform code-completion in an expression context when we know what | |||
3594 | /// type we're looking for. | |||
3595 | void Sema::CodeCompleteExpression(Scope *S, | |||
3596 | const CodeCompleteExpressionData &Data) { | |||
3597 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
3598 | CodeCompleter->getCodeCompletionTUInfo(), | |||
3599 | CodeCompletionContext::CCC_Expression); | |||
3600 | if (Data.ObjCCollection) | |||
3601 | Results.setFilter(&ResultBuilder::IsObjCCollection); | |||
3602 | else if (Data.IntegralConstantExpression) | |||
3603 | Results.setFilter(&ResultBuilder::IsIntegralConstantValue); | |||
3604 | else if (WantTypesInContext(PCC_Expression, getLangOpts())) | |||
3605 | Results.setFilter(&ResultBuilder::IsOrdinaryName); | |||
3606 | else | |||
3607 | Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName); | |||
3608 | ||||
3609 | if (!Data.PreferredType.isNull()) | |||
3610 | Results.setPreferredType(Data.PreferredType.getNonReferenceType()); | |||
3611 | ||||
3612 | // Ignore any declarations that we were told that we don't care about. | |||
3613 | for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I) | |||
3614 | Results.Ignore(Data.IgnoreDecls[I]); | |||
3615 | ||||
3616 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
3617 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | |||
3618 | CodeCompleter->includeGlobals()); | |||
3619 | ||||
3620 | Results.EnterNewScope(); | |||
3621 | AddOrdinaryNameResults(PCC_Expression, S, *this, Results); | |||
3622 | Results.ExitScope(); | |||
3623 | ||||
3624 | bool PreferredTypeIsPointer = false; | |||
3625 | if (!Data.PreferredType.isNull()) | |||
3626 | PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType() | |||
3627 | || Data.PreferredType->isMemberPointerType() | |||
3628 | || Data.PreferredType->isBlockPointerType(); | |||
3629 | ||||
3630 | if (S->getFnParent() && | |||
3631 | !Data.ObjCCollection && | |||
3632 | !Data.IntegralConstantExpression) | |||
3633 | AddPrettyFunctionResults(getLangOpts(), Results); | |||
3634 | ||||
3635 | if (CodeCompleter->includeMacros()) | |||
3636 | AddMacroResults(PP, Results, false, PreferredTypeIsPointer); | |||
3637 | HandleCodeCompleteResults(this, CodeCompleter, | |||
3638 | CodeCompletionContext(CodeCompletionContext::CCC_Expression, | |||
3639 | Data.PreferredType), | |||
3640 | Results.data(),Results.size()); | |||
3641 | } | |||
3642 | ||||
3643 | void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) { | |||
3644 | if (E.isInvalid()) | |||
3645 | CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction); | |||
3646 | else if (getLangOpts().ObjC1) | |||
3647 | CodeCompleteObjCInstanceMessage(S, E.get(), None, false); | |||
3648 | } | |||
3649 | ||||
3650 | /// \brief The set of properties that have already been added, referenced by | |||
3651 | /// property name. | |||
3652 | typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet; | |||
3653 | ||||
3654 | /// \brief Retrieve the container definition, if any? | |||
3655 | static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) { | |||
3656 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { | |||
3657 | if (Interface->hasDefinition()) | |||
3658 | return Interface->getDefinition(); | |||
3659 | ||||
3660 | return Interface; | |||
3661 | } | |||
3662 | ||||
3663 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { | |||
3664 | if (Protocol->hasDefinition()) | |||
3665 | return Protocol->getDefinition(); | |||
3666 | ||||
3667 | return Protocol; | |||
3668 | } | |||
3669 | return Container; | |||
3670 | } | |||
3671 | ||||
3672 | /// \brief Adds a block invocation code completion result for the given block | |||
3673 | /// declaration \p BD. | |||
3674 | static void AddObjCBlockCall(ASTContext &Context, const PrintingPolicy &Policy, | |||
3675 | CodeCompletionBuilder &Builder, | |||
3676 | const NamedDecl *BD, | |||
3677 | const FunctionTypeLoc &BlockLoc, | |||
3678 | const FunctionProtoTypeLoc &BlockProtoLoc) { | |||
3679 | Builder.AddResultTypeChunk( | |||
3680 | GetCompletionTypeString(BlockLoc.getReturnLoc().getType(), Context, | |||
3681 | Policy, Builder.getAllocator())); | |||
3682 | ||||
3683 | AddTypedNameChunk(Context, Policy, BD, Builder); | |||
3684 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
3685 | ||||
3686 | if (BlockProtoLoc && BlockProtoLoc.getTypePtr()->isVariadic()) { | |||
3687 | Builder.AddPlaceholderChunk("..."); | |||
3688 | } else { | |||
3689 | for (unsigned I = 0, N = BlockLoc.getNumParams(); I != N; ++I) { | |||
3690 | if (I) | |||
3691 | Builder.AddChunk(CodeCompletionString::CK_Comma); | |||
3692 | ||||
3693 | // Format the placeholder string. | |||
3694 | std::string PlaceholderStr = | |||
3695 | FormatFunctionParameter(Policy, BlockLoc.getParam(I)); | |||
3696 | ||||
3697 | if (I == N - 1 && BlockProtoLoc && | |||
3698 | BlockProtoLoc.getTypePtr()->isVariadic()) | |||
3699 | PlaceholderStr += ", ..."; | |||
3700 | ||||
3701 | // Add the placeholder string. | |||
3702 | Builder.AddPlaceholderChunk( | |||
3703 | Builder.getAllocator().CopyString(PlaceholderStr)); | |||
3704 | } | |||
3705 | } | |||
3706 | ||||
3707 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
3708 | } | |||
3709 | ||||
3710 | static void AddObjCProperties( | |||
3711 | const CodeCompletionContext &CCContext, ObjCContainerDecl *Container, | |||
3712 | bool AllowCategories, bool AllowNullaryMethods, DeclContext *CurContext, | |||
3713 | AddedPropertiesSet &AddedProperties, ResultBuilder &Results, | |||
3714 | bool IsBaseExprStatement = false, bool IsClassProperty = false) { | |||
3715 | typedef CodeCompletionResult Result; | |||
3716 | ||||
3717 | // Retrieve the definition. | |||
3718 | Container = getContainerDef(Container); | |||
3719 | ||||
3720 | // Add properties in this container. | |||
3721 | const auto AddProperty = [&](const ObjCPropertyDecl *P) { | |||
3722 | if (!AddedProperties.insert(P->getIdentifier()).second) | |||
3723 | return; | |||
3724 | ||||
3725 | // FIXME: Provide block invocation completion for non-statement | |||
3726 | // expressions. | |||
3727 | if (!P->getType().getTypePtr()->isBlockPointerType() || | |||
3728 | !IsBaseExprStatement) { | |||
3729 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), | |||
3730 | CurContext); | |||
3731 | return; | |||
3732 | } | |||
3733 | ||||
3734 | // Block setter and invocation completion is provided only when we are able | |||
3735 | // to find the FunctionProtoTypeLoc with parameter names for the block. | |||
3736 | FunctionTypeLoc BlockLoc; | |||
3737 | FunctionProtoTypeLoc BlockProtoLoc; | |||
3738 | findTypeLocationForBlockDecl(P->getTypeSourceInfo(), BlockLoc, | |||
3739 | BlockProtoLoc); | |||
3740 | if (!BlockLoc) { | |||
3741 | Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr), | |||
3742 | CurContext); | |||
3743 | return; | |||
3744 | } | |||
3745 | ||||
3746 | // The default completion result for block properties should be the block | |||
3747 | // invocation completion when the base expression is a statement. | |||
3748 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
3749 | Results.getCodeCompletionTUInfo()); | |||
3750 | AddObjCBlockCall(Container->getASTContext(), | |||
3751 | getCompletionPrintingPolicy(Results.getSema()), Builder, P, | |||
3752 | BlockLoc, BlockProtoLoc); | |||
3753 | Results.MaybeAddResult( | |||
3754 | Result(Builder.TakeString(), P, Results.getBasePriority(P)), | |||
3755 | CurContext); | |||
3756 | ||||
3757 | // Provide additional block setter completion iff the base expression is a | |||
3758 | // statement and the block property is mutable. | |||
3759 | if (!P->isReadOnly()) { | |||
3760 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
3761 | Results.getCodeCompletionTUInfo()); | |||
3762 | AddResultTypeChunk(Container->getASTContext(), | |||
3763 | getCompletionPrintingPolicy(Results.getSema()), P, | |||
3764 | CCContext.getBaseType(), Builder); | |||
3765 | Builder.AddTypedTextChunk( | |||
3766 | Results.getAllocator().CopyString(P->getName())); | |||
3767 | Builder.AddChunk(CodeCompletionString::CK_Equal); | |||
3768 | ||||
3769 | std::string PlaceholderStr = formatBlockPlaceholder( | |||
3770 | getCompletionPrintingPolicy(Results.getSema()), P, BlockLoc, | |||
3771 | BlockProtoLoc, /*SuppressBlockName=*/true); | |||
3772 | // Add the placeholder string. | |||
3773 | Builder.AddPlaceholderChunk( | |||
3774 | Builder.getAllocator().CopyString(PlaceholderStr)); | |||
3775 | ||||
3776 | // When completing blocks properties that return void the default | |||
3777 | // property completion result should show up before the setter, | |||
3778 | // otherwise the setter completion should show up before the default | |||
3779 | // property completion, as we normally want to use the result of the | |||
3780 | // call. | |||
3781 | Results.MaybeAddResult( | |||
3782 | Result(Builder.TakeString(), P, | |||
3783 | Results.getBasePriority(P) + | |||
3784 | (BlockLoc.getTypePtr()->getReturnType()->isVoidType() | |||
3785 | ? CCD_BlockPropertySetter | |||
3786 | : -CCD_BlockPropertySetter)), | |||
3787 | CurContext); | |||
3788 | } | |||
3789 | }; | |||
3790 | ||||
3791 | if (IsClassProperty) { | |||
3792 | for (const auto *P : Container->class_properties()) | |||
3793 | AddProperty(P); | |||
3794 | } else { | |||
3795 | for (const auto *P : Container->instance_properties()) | |||
3796 | AddProperty(P); | |||
3797 | } | |||
3798 | ||||
3799 | // Add nullary methods or implicit class properties | |||
3800 | if (AllowNullaryMethods) { | |||
3801 | ASTContext &Context = Container->getASTContext(); | |||
3802 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); | |||
3803 | // Adds a method result | |||
3804 | const auto AddMethod = [&](const ObjCMethodDecl *M) { | |||
3805 | IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0); | |||
3806 | if (!Name) | |||
3807 | return; | |||
3808 | if (!AddedProperties.insert(Name).second) | |||
3809 | return; | |||
3810 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
3811 | Results.getCodeCompletionTUInfo()); | |||
3812 | AddResultTypeChunk(Context, Policy, M, CCContext.getBaseType(), Builder); | |||
3813 | Builder.AddTypedTextChunk( | |||
3814 | Results.getAllocator().CopyString(Name->getName())); | |||
3815 | Results.MaybeAddResult( | |||
3816 | Result(Builder.TakeString(), M, | |||
3817 | CCP_MemberDeclaration + CCD_MethodAsProperty), | |||
3818 | CurContext); | |||
3819 | }; | |||
3820 | ||||
3821 | if (IsClassProperty) { | |||
3822 | for (const auto *M : Container->methods()) { | |||
3823 | // Gather the class method that can be used as implicit property | |||
3824 | // getters. Methods with arguments or methods that return void aren't | |||
3825 | // added to the results as they can't be used as a getter. | |||
3826 | if (!M->getSelector().isUnarySelector() || | |||
3827 | M->getReturnType()->isVoidType() || M->isInstanceMethod()) | |||
3828 | continue; | |||
3829 | AddMethod(M); | |||
3830 | } | |||
3831 | } else { | |||
3832 | for (auto *M : Container->methods()) { | |||
3833 | if (M->getSelector().isUnarySelector()) | |||
3834 | AddMethod(M); | |||
3835 | } | |||
3836 | } | |||
3837 | } | |||
3838 | ||||
3839 | // Add properties in referenced protocols. | |||
3840 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { | |||
3841 | for (auto *P : Protocol->protocols()) | |||
3842 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, | |||
3843 | CurContext, AddedProperties, Results, | |||
3844 | IsBaseExprStatement, IsClassProperty); | |||
3845 | } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){ | |||
3846 | if (AllowCategories) { | |||
3847 | // Look through categories. | |||
3848 | for (auto *Cat : IFace->known_categories()) | |||
3849 | AddObjCProperties(CCContext, Cat, AllowCategories, AllowNullaryMethods, | |||
3850 | CurContext, AddedProperties, Results, | |||
3851 | IsBaseExprStatement, IsClassProperty); | |||
3852 | } | |||
3853 | ||||
3854 | // Look through protocols. | |||
3855 | for (auto *I : IFace->all_referenced_protocols()) | |||
3856 | AddObjCProperties(CCContext, I, AllowCategories, AllowNullaryMethods, | |||
3857 | CurContext, AddedProperties, Results, | |||
3858 | IsBaseExprStatement, IsClassProperty); | |||
3859 | ||||
3860 | // Look in the superclass. | |||
3861 | if (IFace->getSuperClass()) | |||
3862 | AddObjCProperties(CCContext, IFace->getSuperClass(), AllowCategories, | |||
3863 | AllowNullaryMethods, CurContext, AddedProperties, | |||
3864 | Results, IsBaseExprStatement, IsClassProperty); | |||
3865 | } else if (const ObjCCategoryDecl *Category | |||
3866 | = dyn_cast<ObjCCategoryDecl>(Container)) { | |||
3867 | // Look through protocols. | |||
3868 | for (auto *P : Category->protocols()) | |||
3869 | AddObjCProperties(CCContext, P, AllowCategories, AllowNullaryMethods, | |||
3870 | CurContext, AddedProperties, Results, | |||
3871 | IsBaseExprStatement, IsClassProperty); | |||
3872 | } | |||
3873 | } | |||
3874 | ||||
3875 | static void AddRecordMembersCompletionResults(Sema &SemaRef, | |||
3876 | ResultBuilder &Results, Scope *S, | |||
3877 | QualType BaseType, | |||
3878 | RecordDecl *RD) { | |||
3879 | // Indicate that we are performing a member access, and the cv-qualifiers | |||
3880 | // for the base object type. | |||
3881 | Results.setObjectTypeQualifiers(BaseType.getQualifiers()); | |||
3882 | ||||
3883 | // Access to a C/C++ class, struct, or union. | |||
3884 | Results.allowNestedNameSpecifiers(); | |||
3885 | CodeCompletionDeclConsumer Consumer(Results, SemaRef.CurContext); | |||
3886 | SemaRef.LookupVisibleDecls(RD, Sema::LookupMemberName, Consumer, | |||
3887 | SemaRef.CodeCompleter->includeGlobals(), | |||
3888 | /*IncludeDependentBases=*/true); | |||
3889 | ||||
3890 | if (SemaRef.getLangOpts().CPlusPlus) { | |||
3891 | if (!Results.empty()) { | |||
3892 | // The "template" keyword can follow "->" or "." in the grammar. | |||
3893 | // However, we only want to suggest the template keyword if something | |||
3894 | // is dependent. | |||
3895 | bool IsDependent = BaseType->isDependentType(); | |||
3896 | if (!IsDependent) { | |||
3897 | for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent()) | |||
3898 | if (DeclContext *Ctx = DepScope->getEntity()) { | |||
3899 | IsDependent = Ctx->isDependentContext(); | |||
3900 | break; | |||
3901 | } | |||
3902 | } | |||
3903 | ||||
3904 | if (IsDependent) | |||
3905 | Results.AddResult(CodeCompletionResult("template")); | |||
3906 | } | |||
3907 | } | |||
3908 | } | |||
3909 | ||||
3910 | void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base, | |||
3911 | SourceLocation OpLoc, bool IsArrow, | |||
3912 | bool IsBaseExprStatement) { | |||
3913 | if (!Base || !CodeCompleter) | |||
3914 | return; | |||
3915 | ||||
3916 | ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow); | |||
3917 | if (ConvertedBase.isInvalid()) | |||
3918 | return; | |||
3919 | Base = ConvertedBase.get(); | |||
3920 | ||||
3921 | QualType BaseType = Base->getType(); | |||
3922 | ||||
3923 | if (IsArrow) { | |||
3924 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) | |||
3925 | BaseType = Ptr->getPointeeType(); | |||
3926 | else if (BaseType->isObjCObjectPointerType()) | |||
3927 | /*Do nothing*/ ; | |||
3928 | else | |||
3929 | return; | |||
3930 | } | |||
3931 | ||||
3932 | enum CodeCompletionContext::Kind contextKind; | |||
3933 | ||||
3934 | if (IsArrow) { | |||
3935 | contextKind = CodeCompletionContext::CCC_ArrowMemberAccess; | |||
3936 | } | |||
3937 | else { | |||
3938 | if (BaseType->isObjCObjectPointerType() || | |||
3939 | BaseType->isObjCObjectOrInterfaceType()) { | |||
3940 | contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess; | |||
3941 | } | |||
3942 | else { | |||
3943 | contextKind = CodeCompletionContext::CCC_DotMemberAccess; | |||
3944 | } | |||
3945 | } | |||
3946 | ||||
3947 | CodeCompletionContext CCContext(contextKind, BaseType); | |||
3948 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
3949 | CodeCompleter->getCodeCompletionTUInfo(), | |||
3950 | CCContext, | |||
3951 | &ResultBuilder::IsMember); | |||
3952 | Results.EnterNewScope(); | |||
3953 | if (const RecordType *Record = BaseType->getAs<RecordType>()) { | |||
3954 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, | |||
3955 | Record->getDecl()); | |||
3956 | } else if (const auto *TST = BaseType->getAs<TemplateSpecializationType>()) { | |||
3957 | TemplateName TN = TST->getTemplateName(); | |||
3958 | if (const auto *TD = | |||
3959 | dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl())) { | |||
3960 | CXXRecordDecl *RD = TD->getTemplatedDecl(); | |||
3961 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, RD); | |||
3962 | } | |||
3963 | } else if (const auto *ICNT = BaseType->getAs<InjectedClassNameType>()) { | |||
3964 | if (auto *RD = ICNT->getDecl()) | |||
3965 | AddRecordMembersCompletionResults(*this, Results, S, BaseType, RD); | |||
3966 | } else if (!IsArrow && BaseType->isObjCObjectPointerType()) { | |||
3967 | // Objective-C property reference. | |||
3968 | AddedPropertiesSet AddedProperties; | |||
3969 | ||||
3970 | if (const ObjCObjectPointerType *ObjCPtr = | |||
3971 | BaseType->getAsObjCInterfacePointerType()) { | |||
3972 | // Add property results based on our interface. | |||
3973 | assert(ObjCPtr && "Non-NULL pointer guaranteed above!")((ObjCPtr && "Non-NULL pointer guaranteed above!") ? static_cast <void> (0) : __assert_fail ("ObjCPtr && \"Non-NULL pointer guaranteed above!\"" , "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/clang/lib/Sema/SemaCodeComplete.cpp" , 3973, __PRETTY_FUNCTION__)); | |||
3974 | AddObjCProperties(CCContext, ObjCPtr->getInterfaceDecl(), true, | |||
3975 | /*AllowNullaryMethods=*/true, CurContext, | |||
3976 | AddedProperties, Results, IsBaseExprStatement); | |||
3977 | } | |||
3978 | ||||
3979 | // Add properties from the protocols in a qualified interface. | |||
3980 | for (auto *I : BaseType->getAs<ObjCObjectPointerType>()->quals()) | |||
3981 | AddObjCProperties(CCContext, I, true, /*AllowNullaryMethods=*/true, | |||
3982 | CurContext, AddedProperties, Results, | |||
3983 | IsBaseExprStatement); | |||
3984 | } else if ((IsArrow && BaseType->isObjCObjectPointerType()) || | |||
3985 | (!IsArrow && BaseType->isObjCObjectType())) { | |||
3986 | // Objective-C instance variable access. | |||
3987 | ObjCInterfaceDecl *Class = nullptr; | |||
3988 | if (const ObjCObjectPointerType *ObjCPtr | |||
3989 | = BaseType->getAs<ObjCObjectPointerType>()) | |||
3990 | Class = ObjCPtr->getInterfaceDecl(); | |||
3991 | else | |||
3992 | Class = BaseType->getAs<ObjCObjectType>()->getInterface(); | |||
3993 | ||||
3994 | // Add all ivars from this class and its superclasses. | |||
3995 | if (Class) { | |||
3996 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
3997 | Results.setFilter(&ResultBuilder::IsObjCIvar); | |||
3998 | LookupVisibleDecls(Class, LookupMemberName, Consumer, | |||
3999 | CodeCompleter->includeGlobals()); | |||
4000 | } | |||
4001 | } | |||
4002 | ||||
4003 | // FIXME: How do we cope with isa? | |||
4004 | ||||
4005 | Results.ExitScope(); | |||
4006 | ||||
4007 | // Hand off the results found for code completion. | |||
4008 | HandleCodeCompleteResults(this, CodeCompleter, | |||
4009 | Results.getCompletionContext(), | |||
4010 | Results.data(),Results.size()); | |||
4011 | } | |||
4012 | ||||
4013 | void Sema::CodeCompleteObjCClassPropertyRefExpr(Scope *S, | |||
4014 | IdentifierInfo &ClassName, | |||
4015 | SourceLocation ClassNameLoc, | |||
4016 | bool IsBaseExprStatement) { | |||
4017 | IdentifierInfo *ClassNamePtr = &ClassName; | |||
4018 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(ClassNamePtr, ClassNameLoc); | |||
4019 | if (!IFace) | |||
4020 | return; | |||
4021 | CodeCompletionContext CCContext( | |||
4022 | CodeCompletionContext::CCC_ObjCPropertyAccess); | |||
4023 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4024 | CodeCompleter->getCodeCompletionTUInfo(), CCContext, | |||
4025 | &ResultBuilder::IsMember); | |||
4026 | Results.EnterNewScope(); | |||
4027 | AddedPropertiesSet AddedProperties; | |||
4028 | AddObjCProperties(CCContext, IFace, true, | |||
4029 | /*AllowNullaryMethods=*/true, CurContext, AddedProperties, | |||
4030 | Results, IsBaseExprStatement, | |||
4031 | /*IsClassProperty=*/true); | |||
4032 | Results.ExitScope(); | |||
4033 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | |||
4034 | Results.data(), Results.size()); | |||
4035 | } | |||
4036 | ||||
4037 | void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) { | |||
4038 | if (!CodeCompleter) | |||
4039 | return; | |||
4040 | ||||
4041 | ResultBuilder::LookupFilter Filter = nullptr; | |||
4042 | enum CodeCompletionContext::Kind ContextKind | |||
4043 | = CodeCompletionContext::CCC_Other; | |||
4044 | switch ((DeclSpec::TST)TagSpec) { | |||
4045 | case DeclSpec::TST_enum: | |||
4046 | Filter = &ResultBuilder::IsEnum; | |||
4047 | ContextKind = CodeCompletionContext::CCC_EnumTag; | |||
4048 | break; | |||
4049 | ||||
4050 | case DeclSpec::TST_union: | |||
4051 | Filter = &ResultBuilder::IsUnion; | |||
4052 | ContextKind = CodeCompletionContext::CCC_UnionTag; | |||
4053 | break; | |||
4054 | ||||
4055 | case DeclSpec::TST_struct: | |||
4056 | case DeclSpec::TST_class: | |||
4057 | case DeclSpec::TST_interface: | |||
4058 | Filter = &ResultBuilder::IsClassOrStruct; | |||
4059 | ContextKind = CodeCompletionContext::CCC_ClassOrStructTag; | |||
4060 | break; | |||
4061 | ||||
4062 | default: | |||
4063 | llvm_unreachable("Unknown type specifier kind in CodeCompleteTag")::llvm::llvm_unreachable_internal("Unknown type specifier kind in CodeCompleteTag" , "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/clang/lib/Sema/SemaCodeComplete.cpp" , 4063); | |||
4064 | } | |||
4065 | ||||
4066 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4067 | CodeCompleter->getCodeCompletionTUInfo(), ContextKind); | |||
4068 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
4069 | ||||
4070 | // First pass: look for tags. | |||
4071 | Results.setFilter(Filter); | |||
4072 | LookupVisibleDecls(S, LookupTagName, Consumer, | |||
4073 | CodeCompleter->includeGlobals()); | |||
4074 | ||||
4075 | if (CodeCompleter->includeGlobals()) { | |||
4076 | // Second pass: look for nested name specifiers. | |||
4077 | Results.setFilter(&ResultBuilder::IsNestedNameSpecifier); | |||
4078 | LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer); | |||
4079 | } | |||
4080 | ||||
4081 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | |||
4082 | Results.data(),Results.size()); | |||
4083 | } | |||
4084 | ||||
4085 | static void AddTypeQualifierResults(DeclSpec &DS, ResultBuilder &Results, | |||
4086 | const LangOptions &LangOpts) { | |||
4087 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const)) | |||
4088 | Results.AddResult("const"); | |||
4089 | if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile)) | |||
4090 | Results.AddResult("volatile"); | |||
4091 | if (LangOpts.C99 && !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict)) | |||
4092 | Results.AddResult("restrict"); | |||
4093 | if (LangOpts.C11 && !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic)) | |||
4094 | Results.AddResult("_Atomic"); | |||
4095 | if (LangOpts.MSVCCompat && !(DS.getTypeQualifiers() & DeclSpec::TQ_unaligned)) | |||
4096 | Results.AddResult("__unaligned"); | |||
4097 | } | |||
4098 | ||||
4099 | void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) { | |||
4100 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4101 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4102 | CodeCompletionContext::CCC_TypeQualifiers); | |||
4103 | Results.EnterNewScope(); | |||
4104 | AddTypeQualifierResults(DS, Results, LangOpts); | |||
4105 | Results.ExitScope(); | |||
4106 | HandleCodeCompleteResults(this, CodeCompleter, | |||
4107 | Results.getCompletionContext(), | |||
4108 | Results.data(), Results.size()); | |||
4109 | } | |||
4110 | ||||
4111 | void Sema::CodeCompleteFunctionQualifiers(DeclSpec &DS, Declarator &D, | |||
4112 | const VirtSpecifiers *VS) { | |||
4113 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4114 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4115 | CodeCompletionContext::CCC_TypeQualifiers); | |||
4116 | Results.EnterNewScope(); | |||
4117 | AddTypeQualifierResults(DS, Results, LangOpts); | |||
4118 | if (LangOpts.CPlusPlus11) { | |||
4119 | Results.AddResult("noexcept"); | |||
4120 | if (D.getContext() == Declarator::MemberContext && !D.isCtorOrDtor() && | |||
4121 | !D.isStaticMember()) { | |||
4122 | if (!VS || !VS->isFinalSpecified()) | |||
4123 | Results.AddResult("final"); | |||
4124 | if (!VS || !VS->isOverrideSpecified()) | |||
4125 | Results.AddResult("override"); | |||
4126 | } | |||
4127 | } | |||
4128 | Results.ExitScope(); | |||
4129 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | |||
4130 | Results.data(), Results.size()); | |||
4131 | } | |||
4132 | ||||
4133 | void Sema::CodeCompleteBracketDeclarator(Scope *S) { | |||
4134 | CodeCompleteExpression(S, QualType(getASTContext().getSizeType())); | |||
4135 | } | |||
4136 | ||||
4137 | void Sema::CodeCompleteCase(Scope *S) { | |||
4138 | if (getCurFunction()->SwitchStack.empty() || !CodeCompleter) | |||
4139 | return; | |||
4140 | ||||
4141 | SwitchStmt *Switch = getCurFunction()->SwitchStack.back(); | |||
4142 | QualType type = Switch->getCond()->IgnoreImplicit()->getType(); | |||
4143 | if (!type->isEnumeralType()) { | |||
4144 | CodeCompleteExpressionData Data(type); | |||
4145 | Data.IntegralConstantExpression = true; | |||
4146 | CodeCompleteExpression(S, Data); | |||
4147 | return; | |||
4148 | } | |||
4149 | ||||
4150 | // Code-complete the cases of a switch statement over an enumeration type | |||
4151 | // by providing the list of | |||
4152 | EnumDecl *Enum = type->castAs<EnumType>()->getDecl(); | |||
4153 | if (EnumDecl *Def = Enum->getDefinition()) | |||
4154 | Enum = Def; | |||
4155 | ||||
4156 | // Determine which enumerators we have already seen in the switch statement. | |||
4157 | // FIXME: Ideally, we would also be able to look *past* the code-completion | |||
4158 | // token, in case we are code-completing in the middle of the switch and not | |||
4159 | // at the end. However, we aren't able to do so at the moment. | |||
4160 | llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen; | |||
4161 | NestedNameSpecifier *Qualifier = nullptr; | |||
4162 | for (SwitchCase *SC = Switch->getSwitchCaseList(); SC; | |||
4163 | SC = SC->getNextSwitchCase()) { | |||
4164 | CaseStmt *Case = dyn_cast<CaseStmt>(SC); | |||
4165 | if (!Case) | |||
4166 | continue; | |||
4167 | ||||
4168 | Expr *CaseVal = Case->getLHS()->IgnoreParenCasts(); | |||
4169 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal)) | |||
4170 | if (EnumConstantDecl *Enumerator | |||
4171 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { | |||
4172 | // We look into the AST of the case statement to determine which | |||
4173 | // enumerator was named. Alternatively, we could compute the value of | |||
4174 | // the integral constant expression, then compare it against the | |||
4175 | // values of each enumerator. However, value-based approach would not | |||
4176 | // work as well with C++ templates where enumerators declared within a | |||
4177 | // template are type- and value-dependent. | |||
4178 | EnumeratorsSeen.insert(Enumerator); | |||
4179 | ||||
4180 | // If this is a qualified-id, keep track of the nested-name-specifier | |||
4181 | // so that we can reproduce it as part of code completion, e.g., | |||
4182 | // | |||
4183 | // switch (TagD.getKind()) { | |||
4184 | // case TagDecl::TK_enum: | |||
4185 | // break; | |||
4186 | // case XXX | |||
4187 | // | |||
4188 | // At the XXX, our completions are TagDecl::TK_union, | |||
4189 | // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union, | |||
4190 | // TK_struct, and TK_class. | |||
4191 | Qualifier = DRE->getQualifier(); | |||
4192 | } | |||
4193 | } | |||
4194 | ||||
4195 | if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) { | |||
4196 | // If there are no prior enumerators in C++, check whether we have to | |||
4197 | // qualify the names of the enumerators that we suggest, because they | |||
4198 | // may not be visible in this scope. | |||
4199 | Qualifier = getRequiredQualification(Context, CurContext, Enum); | |||
4200 | } | |||
4201 | ||||
4202 | // Add any enumerators that have not yet been mentioned. | |||
4203 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4204 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4205 | CodeCompletionContext::CCC_Expression); | |||
4206 | Results.EnterNewScope(); | |||
4207 | for (auto *E : Enum->enumerators()) { | |||
4208 | if (EnumeratorsSeen.count(E)) | |||
4209 | continue; | |||
4210 | ||||
4211 | CodeCompletionResult R(E, CCP_EnumInCase, Qualifier); | |||
4212 | Results.AddResult(R, CurContext, nullptr, false); | |||
4213 | } | |||
4214 | Results.ExitScope(); | |||
4215 | ||||
4216 | //We need to make sure we're setting the right context, | |||
4217 | //so only say we include macros if the code completer says we do | |||
4218 | enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other; | |||
4219 | if (CodeCompleter->includeMacros()) { | |||
4220 | AddMacroResults(PP, Results, false); | |||
4221 | kind = CodeCompletionContext::CCC_OtherWithMacros; | |||
4222 | } | |||
4223 | ||||
4224 | HandleCodeCompleteResults(this, CodeCompleter, | |||
4225 | kind, | |||
4226 | Results.data(),Results.size()); | |||
4227 | } | |||
4228 | ||||
4229 | static bool anyNullArguments(ArrayRef<Expr *> Args) { | |||
4230 | if (Args.size() && !Args.data()) | |||
4231 | return true; | |||
4232 | ||||
4233 | for (unsigned I = 0; I != Args.size(); ++I) | |||
4234 | if (!Args[I]) | |||
4235 | return true; | |||
4236 | ||||
4237 | return false; | |||
4238 | } | |||
4239 | ||||
4240 | typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; | |||
4241 | ||||
4242 | static void mergeCandidatesWithResults(Sema &SemaRef, | |||
4243 | SmallVectorImpl<ResultCandidate> &Results, | |||
4244 | OverloadCandidateSet &CandidateSet, | |||
4245 | SourceLocation Loc) { | |||
4246 | if (!CandidateSet.empty()) { | |||
4247 | // Sort the overload candidate set by placing the best overloads first. | |||
4248 | std::stable_sort( | |||
4249 | CandidateSet.begin(), CandidateSet.end(), | |||
4250 | [&](const OverloadCandidate &X, const OverloadCandidate &Y) { | |||
4251 | return isBetterOverloadCandidate(SemaRef, X, Y, Loc); | |||
4252 | }); | |||
4253 | ||||
4254 | // Add the remaining viable overload candidates as code-completion results. | |||
4255 | for (auto &Candidate : CandidateSet) | |||
4256 | if (Candidate.Viable) | |||
4257 | Results.push_back(ResultCandidate(Candidate.Function)); | |||
4258 | } | |||
4259 | } | |||
4260 | ||||
4261 | /// \brief Get the type of the Nth parameter from a given set of overload | |||
4262 | /// candidates. | |||
4263 | static QualType getParamType(Sema &SemaRef, | |||
4264 | ArrayRef<ResultCandidate> Candidates, | |||
4265 | unsigned N) { | |||
4266 | ||||
4267 | // Given the overloads 'Candidates' for a function call matching all arguments | |||
4268 | // up to N, return the type of the Nth parameter if it is the same for all | |||
4269 | // overload candidates. | |||
4270 | QualType ParamType; | |||
4271 | for (auto &Candidate : Candidates) { | |||
4272 | if (auto FType = Candidate.getFunctionType()) | |||
4273 | if (auto Proto = dyn_cast<FunctionProtoType>(FType)) | |||
4274 | if (N < Proto->getNumParams()) { | |||
4275 | if (ParamType.isNull()) | |||
4276 | ParamType = Proto->getParamType(N); | |||
4277 | else if (!SemaRef.Context.hasSameUnqualifiedType( | |||
4278 | ParamType.getNonReferenceType(), | |||
4279 | Proto->getParamType(N).getNonReferenceType())) | |||
4280 | // Otherwise return a default-constructed QualType. | |||
4281 | return QualType(); | |||
4282 | } | |||
4283 | } | |||
4284 | ||||
4285 | return ParamType; | |||
4286 | } | |||
4287 | ||||
4288 | static void CodeCompleteOverloadResults(Sema &SemaRef, Scope *S, | |||
4289 | MutableArrayRef<ResultCandidate> Candidates, | |||
4290 | unsigned CurrentArg, | |||
4291 | bool CompleteExpressionWithCurrentArg = true) { | |||
4292 | QualType ParamType; | |||
4293 | if (CompleteExpressionWithCurrentArg) | |||
4294 | ParamType = getParamType(SemaRef, Candidates, CurrentArg); | |||
4295 | ||||
4296 | if (ParamType.isNull()) | |||
4297 | SemaRef.CodeCompleteOrdinaryName(S, Sema::PCC_Expression); | |||
4298 | else | |||
4299 | SemaRef.CodeCompleteExpression(S, ParamType); | |||
4300 | ||||
4301 | if (!Candidates.empty()) | |||
4302 | SemaRef.CodeCompleter->ProcessOverloadCandidates(SemaRef, CurrentArg, | |||
4303 | Candidates.data(), | |||
4304 | Candidates.size()); | |||
4305 | } | |||
4306 | ||||
4307 | void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args) { | |||
4308 | if (!CodeCompleter) | |||
4309 | return; | |||
4310 | ||||
4311 | // When we're code-completing for a call, we fall back to ordinary | |||
4312 | // name code-completion whenever we can't produce specific | |||
4313 | // results. We may want to revisit this strategy in the future, | |||
4314 | // e.g., by merging the two kinds of results. | |||
4315 | ||||
4316 | // FIXME: Provide support for variadic template functions. | |||
4317 | ||||
4318 | // Ignore type-dependent call expressions entirely. | |||
4319 | if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) || | |||
4320 | Expr::hasAnyTypeDependentArguments(Args)) { | |||
4321 | CodeCompleteOrdinaryName(S, PCC_Expression); | |||
4322 | return; | |||
4323 | } | |||
4324 | ||||
4325 | // Build an overload candidate set based on the functions we find. | |||
4326 | SourceLocation Loc = Fn->getExprLoc(); | |||
4327 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); | |||
4328 | ||||
4329 | SmallVector<ResultCandidate, 8> Results; | |||
4330 | ||||
4331 | Expr *NakedFn = Fn->IgnoreParenCasts(); | |||
4332 | if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn)) | |||
4333 | AddOverloadedCallCandidates(ULE, Args, CandidateSet, | |||
4334 | /*PartialOverloading=*/true); | |||
4335 | else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { | |||
4336 | TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr; | |||
4337 | if (UME->hasExplicitTemplateArgs()) { | |||
4338 | UME->copyTemplateArgumentsInto(TemplateArgsBuffer); | |||
4339 | TemplateArgs = &TemplateArgsBuffer; | |||
4340 | } | |||
4341 | ||||
4342 | // Add the base as first argument (use a nullptr if the base is implicit). | |||
4343 | SmallVector<Expr *, 12> ArgExprs( | |||
4344 | 1, UME->isImplicitAccess() ? nullptr : UME->getBase()); | |||
4345 | ArgExprs.append(Args.begin(), Args.end()); | |||
4346 | UnresolvedSet<8> Decls; | |||
4347 | Decls.append(UME->decls_begin(), UME->decls_end()); | |||
4348 | AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs, | |||
4349 | /*SuppressUsedConversions=*/false, | |||
4350 | /*PartialOverloading=*/true); | |||
4351 | } else { | |||
4352 | FunctionDecl *FD = nullptr; | |||
4353 | if (auto MCE = dyn_cast<MemberExpr>(NakedFn)) | |||
4354 | FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl()); | |||
4355 | else if (auto DRE = dyn_cast<DeclRefExpr>(NakedFn)) | |||
4356 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); | |||
4357 | if (FD) { // We check whether it's a resolved function declaration. | |||
4358 | if (!getLangOpts().CPlusPlus || | |||
4359 | !FD->getType()->getAs<FunctionProtoType>()) | |||
4360 | Results.push_back(ResultCandidate(FD)); | |||
4361 | else | |||
4362 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()), | |||
4363 | Args, CandidateSet, | |||
4364 | /*SuppressUsedConversions=*/false, | |||
4365 | /*PartialOverloading=*/true); | |||
4366 | ||||
4367 | } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) { | |||
4368 | // If expression's type is CXXRecordDecl, it may overload the function | |||
4369 | // call operator, so we check if it does and add them as candidates. | |||
4370 | // A complete type is needed to lookup for member function call operators. | |||
4371 | if (isCompleteType(Loc, NakedFn->getType())) { | |||
4372 | DeclarationName OpName = Context.DeclarationNames | |||
4373 | .getCXXOperatorName(OO_Call); | |||
4374 | LookupResult R(*this, OpName, Loc, LookupOrdinaryName); | |||
4375 | LookupQualifiedName(R, DC); | |||
4376 | R.suppressDiagnostics(); | |||
4377 | SmallVector<Expr *, 12> ArgExprs(1, NakedFn); | |||
4378 | ArgExprs.append(Args.begin(), Args.end()); | |||
4379 | AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet, | |||
4380 | /*ExplicitArgs=*/nullptr, | |||
4381 | /*SuppressUsedConversions=*/false, | |||
4382 | /*PartialOverloading=*/true); | |||
4383 | } | |||
4384 | } else { | |||
4385 | // Lastly we check whether expression's type is function pointer or | |||
4386 | // function. | |||
4387 | QualType T = NakedFn->getType(); | |||
4388 | if (!T->getPointeeType().isNull()) | |||
4389 | T = T->getPointeeType(); | |||
4390 | ||||
4391 | if (auto FP = T->getAs<FunctionProtoType>()) { | |||
4392 | if (!TooManyArguments(FP->getNumParams(), Args.size(), | |||
4393 | /*PartialOverloading=*/true) || | |||
4394 | FP->isVariadic()) | |||
4395 | Results.push_back(ResultCandidate(FP)); | |||
4396 | } else if (auto FT = T->getAs<FunctionType>()) | |||
4397 | // No prototype and declaration, it may be a K & R style function. | |||
4398 | Results.push_back(ResultCandidate(FT)); | |||
4399 | } | |||
4400 | } | |||
4401 | ||||
4402 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); | |||
4403 | CodeCompleteOverloadResults(*this, S, Results, Args.size(), | |||
4404 | !CandidateSet.empty()); | |||
4405 | } | |||
4406 | ||||
4407 | void Sema::CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc, | |||
4408 | ArrayRef<Expr *> Args) { | |||
4409 | if (!CodeCompleter) | |||
4410 | return; | |||
4411 | ||||
4412 | // A complete type is needed to lookup for constructors. | |||
4413 | if (!isCompleteType(Loc, Type)) | |||
4414 | return; | |||
4415 | ||||
4416 | CXXRecordDecl *RD = Type->getAsCXXRecordDecl(); | |||
4417 | if (!RD) { | |||
4418 | CodeCompleteExpression(S, Type); | |||
4419 | return; | |||
4420 | } | |||
4421 | ||||
4422 | // FIXME: Provide support for member initializers. | |||
4423 | // FIXME: Provide support for variadic template constructors. | |||
4424 | ||||
4425 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); | |||
4426 | ||||
4427 | for (auto C : LookupConstructors(RD)) { | |||
4428 | if (auto FD = dyn_cast<FunctionDecl>(C)) { | |||
4429 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()), | |||
4430 | Args, CandidateSet, | |||
4431 | /*SuppressUsedConversions=*/false, | |||
4432 | /*PartialOverloading=*/true); | |||
4433 | } else if (auto FTD = dyn_cast<FunctionTemplateDecl>(C)) { | |||
4434 | AddTemplateOverloadCandidate(FTD, | |||
4435 | DeclAccessPair::make(FTD, C->getAccess()), | |||
4436 | /*ExplicitTemplateArgs=*/nullptr, | |||
4437 | Args, CandidateSet, | |||
4438 | /*SuppressUsedConversions=*/false, | |||
4439 | /*PartialOverloading=*/true); | |||
4440 | } | |||
4441 | } | |||
4442 | ||||
4443 | SmallVector<ResultCandidate, 8> Results; | |||
4444 | mergeCandidatesWithResults(*this, Results, CandidateSet, Loc); | |||
4445 | CodeCompleteOverloadResults(*this, S, Results, Args.size()); | |||
4446 | } | |||
4447 | ||||
4448 | void Sema::CodeCompleteInitializer(Scope *S, Decl *D) { | |||
4449 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D); | |||
4450 | if (!VD) { | |||
4451 | CodeCompleteOrdinaryName(S, PCC_Expression); | |||
4452 | return; | |||
4453 | } | |||
4454 | ||||
4455 | CodeCompleteExpression(S, VD->getType()); | |||
4456 | } | |||
4457 | ||||
4458 | void Sema::CodeCompleteReturn(Scope *S) { | |||
4459 | QualType ResultType; | |||
4460 | if (isa<BlockDecl>(CurContext)) { | |||
4461 | if (BlockScopeInfo *BSI = getCurBlock()) | |||
4462 | ResultType = BSI->ReturnType; | |||
4463 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext)) | |||
4464 | ResultType = Function->getReturnType(); | |||
4465 | else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext)) | |||
4466 | ResultType = Method->getReturnType(); | |||
4467 | ||||
4468 | if (ResultType.isNull()) | |||
4469 | CodeCompleteOrdinaryName(S, PCC_Expression); | |||
4470 | else | |||
4471 | CodeCompleteExpression(S, ResultType); | |||
4472 | } | |||
4473 | ||||
4474 | void Sema::CodeCompleteAfterIf(Scope *S) { | |||
4475 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4476 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4477 | mapCodeCompletionContext(*this, PCC_Statement)); | |||
4478 | Results.setFilter(&ResultBuilder::IsOrdinaryName); | |||
4479 | Results.EnterNewScope(); | |||
4480 | ||||
4481 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
4482 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | |||
4483 | CodeCompleter->includeGlobals()); | |||
4484 | ||||
4485 | AddOrdinaryNameResults(PCC_Statement, S, *this, Results); | |||
4486 | ||||
4487 | // "else" block | |||
4488 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
4489 | Results.getCodeCompletionTUInfo()); | |||
4490 | Builder.AddTypedTextChunk("else"); | |||
4491 | if (Results.includeCodePatterns()) { | |||
4492 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4493 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
4494 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
4495 | Builder.AddPlaceholderChunk("statements"); | |||
4496 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
4497 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
4498 | } | |||
4499 | Results.AddResult(Builder.TakeString()); | |||
4500 | ||||
4501 | // "else if" block | |||
4502 | Builder.AddTypedTextChunk("else"); | |||
4503 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4504 | Builder.AddTextChunk("if"); | |||
4505 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4506 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
4507 | if (getLangOpts().CPlusPlus) | |||
4508 | Builder.AddPlaceholderChunk("condition"); | |||
4509 | else | |||
4510 | Builder.AddPlaceholderChunk("expression"); | |||
4511 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
4512 | if (Results.includeCodePatterns()) { | |||
4513 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4514 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
4515 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
4516 | Builder.AddPlaceholderChunk("statements"); | |||
4517 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
4518 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
4519 | } | |||
4520 | Results.AddResult(Builder.TakeString()); | |||
4521 | ||||
4522 | Results.ExitScope(); | |||
4523 | ||||
4524 | if (S->getFnParent()) | |||
4525 | AddPrettyFunctionResults(getLangOpts(), Results); | |||
4526 | ||||
4527 | if (CodeCompleter->includeMacros()) | |||
4528 | AddMacroResults(PP, Results, false); | |||
4529 | ||||
4530 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | |||
4531 | Results.data(),Results.size()); | |||
4532 | } | |||
4533 | ||||
4534 | void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) { | |||
4535 | if (LHS) | |||
4536 | CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType()); | |||
4537 | else | |||
4538 | CodeCompleteOrdinaryName(S, PCC_Expression); | |||
4539 | } | |||
4540 | ||||
4541 | void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, | |||
4542 | bool EnteringContext) { | |||
4543 | if (!SS.getScopeRep() || !CodeCompleter) | |||
4544 | return; | |||
4545 | ||||
4546 | // Always pretend to enter a context to ensure that a dependent type | |||
4547 | // resolves to a dependent record. | |||
4548 | DeclContext *Ctx = computeDeclContext(SS, /*EnteringContext=*/true); | |||
4549 | if (!Ctx) | |||
4550 | return; | |||
4551 | ||||
4552 | // Try to instantiate any non-dependent declaration contexts before | |||
4553 | // we look in them. | |||
4554 | if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) | |||
4555 | return; | |||
4556 | ||||
4557 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4558 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4559 | CodeCompletionContext::CCC_Name); | |||
4560 | Results.EnterNewScope(); | |||
4561 | ||||
4562 | // The "template" keyword can follow "::" in the grammar, but only | |||
4563 | // put it into the grammar if the nested-name-specifier is dependent. | |||
4564 | NestedNameSpecifier *NNS = SS.getScopeRep(); | |||
4565 | if (!Results.empty() && NNS->isDependent()) | |||
4566 | Results.AddResult("template"); | |||
4567 | ||||
4568 | // Add calls to overridden virtual functions, if there are any. | |||
4569 | // | |||
4570 | // FIXME: This isn't wonderful, because we don't know whether we're actually | |||
4571 | // in a context that permits expressions. This is a general issue with | |||
4572 | // qualified-id completions. | |||
4573 | if (!EnteringContext) | |||
4574 | MaybeAddOverrideCalls(*this, Ctx, Results); | |||
4575 | Results.ExitScope(); | |||
4576 | ||||
4577 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
4578 | LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer, | |||
4579 | /*IncludeGlobalScope=*/true, | |||
4580 | /*IncludeDependentBases=*/true); | |||
4581 | ||||
4582 | HandleCodeCompleteResults(this, CodeCompleter, | |||
4583 | Results.getCompletionContext(), | |||
4584 | Results.data(),Results.size()); | |||
4585 | } | |||
4586 | ||||
4587 | void Sema::CodeCompleteUsing(Scope *S) { | |||
4588 | if (!CodeCompleter) | |||
4589 | return; | |||
4590 | ||||
4591 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4592 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4593 | CodeCompletionContext::CCC_PotentiallyQualifiedName, | |||
4594 | &ResultBuilder::IsNestedNameSpecifier); | |||
4595 | Results.EnterNewScope(); | |||
4596 | ||||
4597 | // If we aren't in class scope, we could see the "namespace" keyword. | |||
4598 | if (!S->isClassScope()) | |||
4599 | Results.AddResult(CodeCompletionResult("namespace")); | |||
4600 | ||||
4601 | // After "using", we can see anything that would start a | |||
4602 | // nested-name-specifier. | |||
4603 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
4604 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | |||
4605 | CodeCompleter->includeGlobals()); | |||
4606 | Results.ExitScope(); | |||
4607 | ||||
4608 | HandleCodeCompleteResults(this, CodeCompleter, | |||
4609 | CodeCompletionContext::CCC_PotentiallyQualifiedName, | |||
4610 | Results.data(),Results.size()); | |||
4611 | } | |||
4612 | ||||
4613 | void Sema::CodeCompleteUsingDirective(Scope *S) { | |||
4614 | if (!CodeCompleter) | |||
4615 | return; | |||
4616 | ||||
4617 | // After "using namespace", we expect to see a namespace name or namespace | |||
4618 | // alias. | |||
4619 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4620 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4621 | CodeCompletionContext::CCC_Namespace, | |||
4622 | &ResultBuilder::IsNamespaceOrAlias); | |||
4623 | Results.EnterNewScope(); | |||
4624 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
4625 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | |||
4626 | CodeCompleter->includeGlobals()); | |||
4627 | Results.ExitScope(); | |||
4628 | HandleCodeCompleteResults(this, CodeCompleter, | |||
4629 | CodeCompletionContext::CCC_Namespace, | |||
4630 | Results.data(),Results.size()); | |||
4631 | } | |||
4632 | ||||
4633 | void Sema::CodeCompleteNamespaceDecl(Scope *S) { | |||
4634 | if (!CodeCompleter) | |||
4635 | return; | |||
4636 | ||||
4637 | DeclContext *Ctx = S->getEntity(); | |||
4638 | if (!S->getParent()) | |||
4639 | Ctx = Context.getTranslationUnitDecl(); | |||
4640 | ||||
4641 | bool SuppressedGlobalResults | |||
4642 | = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx); | |||
4643 | ||||
4644 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4645 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4646 | SuppressedGlobalResults | |||
4647 | ? CodeCompletionContext::CCC_Namespace | |||
4648 | : CodeCompletionContext::CCC_Other, | |||
4649 | &ResultBuilder::IsNamespace); | |||
4650 | ||||
4651 | if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) { | |||
4652 | // We only want to see those namespaces that have already been defined | |||
4653 | // within this scope, because its likely that the user is creating an | |||
4654 | // extended namespace declaration. Keep track of the most recent | |||
4655 | // definition of each namespace. | |||
4656 | std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest; | |||
4657 | for (DeclContext::specific_decl_iterator<NamespaceDecl> | |||
4658 | NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end()); | |||
4659 | NS != NSEnd; ++NS) | |||
4660 | OrigToLatest[NS->getOriginalNamespace()] = *NS; | |||
4661 | ||||
4662 | // Add the most recent definition (or extended definition) of each | |||
4663 | // namespace to the list of results. | |||
4664 | Results.EnterNewScope(); | |||
4665 | for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator | |||
4666 | NS = OrigToLatest.begin(), | |||
4667 | NSEnd = OrigToLatest.end(); | |||
4668 | NS != NSEnd; ++NS) | |||
4669 | Results.AddResult(CodeCompletionResult( | |||
4670 | NS->second, Results.getBasePriority(NS->second), | |||
4671 | nullptr), | |||
4672 | CurContext, nullptr, false); | |||
4673 | Results.ExitScope(); | |||
4674 | } | |||
4675 | ||||
4676 | HandleCodeCompleteResults(this, CodeCompleter, | |||
4677 | Results.getCompletionContext(), | |||
4678 | Results.data(),Results.size()); | |||
4679 | } | |||
4680 | ||||
4681 | void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) { | |||
4682 | if (!CodeCompleter) | |||
4683 | return; | |||
4684 | ||||
4685 | // After "namespace", we expect to see a namespace or alias. | |||
4686 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4687 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4688 | CodeCompletionContext::CCC_Namespace, | |||
4689 | &ResultBuilder::IsNamespaceOrAlias); | |||
4690 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
4691 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | |||
4692 | CodeCompleter->includeGlobals()); | |||
4693 | HandleCodeCompleteResults(this, CodeCompleter, | |||
4694 | Results.getCompletionContext(), | |||
4695 | Results.data(),Results.size()); | |||
4696 | } | |||
4697 | ||||
4698 | void Sema::CodeCompleteOperatorName(Scope *S) { | |||
4699 | if (!CodeCompleter) | |||
4700 | return; | |||
4701 | ||||
4702 | typedef CodeCompletionResult Result; | |||
4703 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4704 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4705 | CodeCompletionContext::CCC_Type, | |||
4706 | &ResultBuilder::IsType); | |||
4707 | Results.EnterNewScope(); | |||
4708 | ||||
4709 | // Add the names of overloadable operators. | |||
4710 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ | |||
4711 | if (std::strcmp(Spelling, "?")) \ | |||
4712 | Results.AddResult(Result(Spelling)); | |||
4713 | #include "clang/Basic/OperatorKinds.def" | |||
4714 | ||||
4715 | // Add any type names visible from the current scope | |||
4716 | Results.allowNestedNameSpecifiers(); | |||
4717 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
4718 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | |||
4719 | CodeCompleter->includeGlobals()); | |||
4720 | ||||
4721 | // Add any type specifiers | |||
4722 | AddTypeSpecifierResults(getLangOpts(), Results); | |||
4723 | Results.ExitScope(); | |||
4724 | ||||
4725 | HandleCodeCompleteResults(this, CodeCompleter, | |||
4726 | CodeCompletionContext::CCC_Type, | |||
4727 | Results.data(),Results.size()); | |||
4728 | } | |||
4729 | ||||
4730 | void Sema::CodeCompleteConstructorInitializer( | |||
4731 | Decl *ConstructorD, | |||
4732 | ArrayRef <CXXCtorInitializer *> Initializers) { | |||
4733 | if (!ConstructorD) | |||
4734 | return; | |||
4735 | ||||
4736 | AdjustDeclIfTemplate(ConstructorD); | |||
4737 | ||||
4738 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ConstructorD); | |||
4739 | if (!Constructor) | |||
4740 | return; | |||
4741 | ||||
4742 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4743 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4744 | CodeCompletionContext::CCC_PotentiallyQualifiedName); | |||
4745 | Results.EnterNewScope(); | |||
4746 | ||||
4747 | // Fill in any already-initialized fields or base classes. | |||
4748 | llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields; | |||
4749 | llvm::SmallPtrSet<CanQualType, 4> InitializedBases; | |||
4750 | for (unsigned I = 0, E = Initializers.size(); I != E; ++I) { | |||
4751 | if (Initializers[I]->isBaseInitializer()) | |||
4752 | InitializedBases.insert( | |||
4753 | Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0))); | |||
4754 | else | |||
4755 | InitializedFields.insert(cast<FieldDecl>( | |||
4756 | Initializers[I]->getAnyMember())); | |||
4757 | } | |||
4758 | ||||
4759 | // Add completions for base classes. | |||
4760 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
4761 | Results.getCodeCompletionTUInfo()); | |||
4762 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); | |||
4763 | bool SawLastInitializer = Initializers.empty(); | |||
4764 | CXXRecordDecl *ClassDecl = Constructor->getParent(); | |||
4765 | for (const auto &Base : ClassDecl->bases()) { | |||
4766 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) | |||
4767 | .second) { | |||
4768 | SawLastInitializer | |||
4769 | = !Initializers.empty() && | |||
4770 | Initializers.back()->isBaseInitializer() && | |||
4771 | Context.hasSameUnqualifiedType(Base.getType(), | |||
4772 | QualType(Initializers.back()->getBaseClass(), 0)); | |||
4773 | continue; | |||
4774 | } | |||
4775 | ||||
4776 | Builder.AddTypedTextChunk( | |||
4777 | Results.getAllocator().CopyString( | |||
4778 | Base.getType().getAsString(Policy))); | |||
4779 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
4780 | Builder.AddPlaceholderChunk("args"); | |||
4781 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
4782 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), | |||
4783 | SawLastInitializer? CCP_NextInitializer | |||
4784 | : CCP_MemberDeclaration)); | |||
4785 | SawLastInitializer = false; | |||
4786 | } | |||
4787 | ||||
4788 | // Add completions for virtual base classes. | |||
4789 | for (const auto &Base : ClassDecl->vbases()) { | |||
4790 | if (!InitializedBases.insert(Context.getCanonicalType(Base.getType())) | |||
4791 | .second) { | |||
4792 | SawLastInitializer | |||
4793 | = !Initializers.empty() && | |||
4794 | Initializers.back()->isBaseInitializer() && | |||
4795 | Context.hasSameUnqualifiedType(Base.getType(), | |||
4796 | QualType(Initializers.back()->getBaseClass(), 0)); | |||
4797 | continue; | |||
4798 | } | |||
4799 | ||||
4800 | Builder.AddTypedTextChunk( | |||
4801 | Builder.getAllocator().CopyString( | |||
4802 | Base.getType().getAsString(Policy))); | |||
4803 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
4804 | Builder.AddPlaceholderChunk("args"); | |||
4805 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
4806 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), | |||
4807 | SawLastInitializer? CCP_NextInitializer | |||
4808 | : CCP_MemberDeclaration)); | |||
4809 | SawLastInitializer = false; | |||
4810 | } | |||
4811 | ||||
4812 | // Add completions for members. | |||
4813 | for (auto *Field : ClassDecl->fields()) { | |||
4814 | if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl())) | |||
4815 | .second) { | |||
4816 | SawLastInitializer | |||
4817 | = !Initializers.empty() && | |||
4818 | Initializers.back()->isAnyMemberInitializer() && | |||
4819 | Initializers.back()->getAnyMember() == Field; | |||
4820 | continue; | |||
4821 | } | |||
4822 | ||||
4823 | if (!Field->getDeclName()) | |||
4824 | continue; | |||
4825 | ||||
4826 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | |||
4827 | Field->getIdentifier()->getName())); | |||
4828 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
4829 | Builder.AddPlaceholderChunk("args"); | |||
4830 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
4831 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), | |||
4832 | SawLastInitializer? CCP_NextInitializer | |||
4833 | : CCP_MemberDeclaration, | |||
4834 | CXCursor_MemberRef, | |||
4835 | CXAvailability_Available, | |||
4836 | Field)); | |||
4837 | SawLastInitializer = false; | |||
4838 | } | |||
4839 | Results.ExitScope(); | |||
4840 | ||||
4841 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | |||
4842 | Results.data(), Results.size()); | |||
4843 | } | |||
4844 | ||||
4845 | /// \brief Determine whether this scope denotes a namespace. | |||
4846 | static bool isNamespaceScope(Scope *S) { | |||
4847 | DeclContext *DC = S->getEntity(); | |||
4848 | if (!DC) | |||
4849 | return false; | |||
4850 | ||||
4851 | return DC->isFileContext(); | |||
4852 | } | |||
4853 | ||||
4854 | void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro, | |||
4855 | bool AfterAmpersand) { | |||
4856 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4857 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4858 | CodeCompletionContext::CCC_Other); | |||
4859 | Results.EnterNewScope(); | |||
4860 | ||||
4861 | // Note what has already been captured. | |||
4862 | llvm::SmallPtrSet<IdentifierInfo *, 4> Known; | |||
4863 | bool IncludedThis = false; | |||
4864 | for (const auto &C : Intro.Captures) { | |||
4865 | if (C.Kind == LCK_This) { | |||
4866 | IncludedThis = true; | |||
4867 | continue; | |||
4868 | } | |||
4869 | ||||
4870 | Known.insert(C.Id); | |||
4871 | } | |||
4872 | ||||
4873 | // Look for other capturable variables. | |||
4874 | for (; S && !isNamespaceScope(S); S = S->getParent()) { | |||
4875 | for (const auto *D : S->decls()) { | |||
4876 | const auto *Var = dyn_cast<VarDecl>(D); | |||
4877 | if (!Var || | |||
4878 | !Var->hasLocalStorage() || | |||
4879 | Var->hasAttr<BlocksAttr>()) | |||
4880 | continue; | |||
4881 | ||||
4882 | if (Known.insert(Var->getIdentifier()).second) | |||
4883 | Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration), | |||
4884 | CurContext, nullptr, false); | |||
4885 | } | |||
4886 | } | |||
4887 | ||||
4888 | // Add 'this', if it would be valid. | |||
4889 | if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy) | |||
4890 | addThisCompletion(*this, Results); | |||
4891 | ||||
4892 | Results.ExitScope(); | |||
4893 | ||||
4894 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | |||
4895 | Results.data(), Results.size()); | |||
4896 | } | |||
4897 | ||||
4898 | /// Macro that optionally prepends an "@" to the string literal passed in via | |||
4899 | /// Keyword, depending on whether NeedAt is true or false. | |||
4900 | #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword)((NeedAt)? "@" Keyword : Keyword) ((NeedAt)? "@" Keyword : Keyword) | |||
4901 | ||||
4902 | static void AddObjCImplementationResults(const LangOptions &LangOpts, | |||
4903 | ResultBuilder &Results, | |||
4904 | bool NeedAt) { | |||
4905 | typedef CodeCompletionResult Result; | |||
4906 | // Since we have an implementation, we can end it. | |||
4907 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")((NeedAt)? "@" "end" : "end"))); | |||
4908 | ||||
4909 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
4910 | Results.getCodeCompletionTUInfo()); | |||
4911 | if (LangOpts.ObjC2) { | |||
4912 | // @dynamic | |||
4913 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic")((NeedAt)? "@" "dynamic" : "dynamic")); | |||
4914 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4915 | Builder.AddPlaceholderChunk("property"); | |||
4916 | Results.AddResult(Result(Builder.TakeString())); | |||
4917 | ||||
4918 | // @synthesize | |||
4919 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize")((NeedAt)? "@" "synthesize" : "synthesize")); | |||
4920 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4921 | Builder.AddPlaceholderChunk("property"); | |||
4922 | Results.AddResult(Result(Builder.TakeString())); | |||
4923 | } | |||
4924 | } | |||
4925 | ||||
4926 | static void AddObjCInterfaceResults(const LangOptions &LangOpts, | |||
4927 | ResultBuilder &Results, | |||
4928 | bool NeedAt) { | |||
4929 | typedef CodeCompletionResult Result; | |||
4930 | ||||
4931 | // Since we have an interface or protocol, we can end it. | |||
4932 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")((NeedAt)? "@" "end" : "end"))); | |||
4933 | ||||
4934 | if (LangOpts.ObjC2) { | |||
4935 | // @property | |||
4936 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property")((NeedAt)? "@" "property" : "property"))); | |||
4937 | ||||
4938 | // @required | |||
4939 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required")((NeedAt)? "@" "required" : "required"))); | |||
4940 | ||||
4941 | // @optional | |||
4942 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional")((NeedAt)? "@" "optional" : "optional"))); | |||
4943 | } | |||
4944 | } | |||
4945 | ||||
4946 | static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) { | |||
4947 | typedef CodeCompletionResult Result; | |||
4948 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
4949 | Results.getCodeCompletionTUInfo()); | |||
4950 | ||||
4951 | // @class name ; | |||
4952 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class")((NeedAt)? "@" "class" : "class")); | |||
4953 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4954 | Builder.AddPlaceholderChunk("name"); | |||
4955 | Results.AddResult(Result(Builder.TakeString())); | |||
4956 | ||||
4957 | if (Results.includeCodePatterns()) { | |||
4958 | // @interface name | |||
4959 | // FIXME: Could introduce the whole pattern, including superclasses and | |||
4960 | // such. | |||
4961 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface")((NeedAt)? "@" "interface" : "interface")); | |||
4962 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4963 | Builder.AddPlaceholderChunk("class"); | |||
4964 | Results.AddResult(Result(Builder.TakeString())); | |||
4965 | ||||
4966 | // @protocol name | |||
4967 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")((NeedAt)? "@" "protocol" : "protocol")); | |||
4968 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4969 | Builder.AddPlaceholderChunk("protocol"); | |||
4970 | Results.AddResult(Result(Builder.TakeString())); | |||
4971 | ||||
4972 | // @implementation name | |||
4973 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation")((NeedAt)? "@" "implementation" : "implementation")); | |||
4974 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4975 | Builder.AddPlaceholderChunk("class"); | |||
4976 | Results.AddResult(Result(Builder.TakeString())); | |||
4977 | } | |||
4978 | ||||
4979 | // @compatibility_alias name | |||
4980 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias")((NeedAt)? "@" "compatibility_alias" : "compatibility_alias")); | |||
4981 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4982 | Builder.AddPlaceholderChunk("alias"); | |||
4983 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4984 | Builder.AddPlaceholderChunk("class"); | |||
4985 | Results.AddResult(Result(Builder.TakeString())); | |||
4986 | ||||
4987 | if (Results.getSema().getLangOpts().Modules) { | |||
4988 | // @import name | |||
4989 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import")((NeedAt)? "@" "import" : "import")); | |||
4990 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
4991 | Builder.AddPlaceholderChunk("module"); | |||
4992 | Results.AddResult(Result(Builder.TakeString())); | |||
4993 | } | |||
4994 | } | |||
4995 | ||||
4996 | void Sema::CodeCompleteObjCAtDirective(Scope *S) { | |||
4997 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
4998 | CodeCompleter->getCodeCompletionTUInfo(), | |||
4999 | CodeCompletionContext::CCC_Other); | |||
5000 | Results.EnterNewScope(); | |||
5001 | if (isa<ObjCImplDecl>(CurContext)) | |||
5002 | AddObjCImplementationResults(getLangOpts(), Results, false); | |||
5003 | else if (CurContext->isObjCContainer()) | |||
5004 | AddObjCInterfaceResults(getLangOpts(), Results, false); | |||
5005 | else | |||
5006 | AddObjCTopLevelResults(Results, false); | |||
5007 | Results.ExitScope(); | |||
5008 | HandleCodeCompleteResults(this, CodeCompleter, | |||
5009 | CodeCompletionContext::CCC_Other, | |||
5010 | Results.data(),Results.size()); | |||
5011 | } | |||
5012 | ||||
5013 | static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) { | |||
5014 | typedef CodeCompletionResult Result; | |||
5015 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
5016 | Results.getCodeCompletionTUInfo()); | |||
5017 | ||||
5018 | // @encode ( type-name ) | |||
5019 | const char *EncodeType = "char[]"; | |||
5020 | if (Results.getSema().getLangOpts().CPlusPlus || | |||
5021 | Results.getSema().getLangOpts().ConstStrings) | |||
5022 | EncodeType = "const char[]"; | |||
5023 | Builder.AddResultTypeChunk(EncodeType); | |||
5024 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode")((NeedAt)? "@" "encode" : "encode")); | |||
5025 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
5026 | Builder.AddPlaceholderChunk("type-name"); | |||
5027 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
5028 | Results.AddResult(Result(Builder.TakeString())); | |||
5029 | ||||
5030 | // @protocol ( protocol-name ) | |||
5031 | Builder.AddResultTypeChunk("Protocol *"); | |||
5032 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol")((NeedAt)? "@" "protocol" : "protocol")); | |||
5033 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
5034 | Builder.AddPlaceholderChunk("protocol-name"); | |||
5035 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
5036 | Results.AddResult(Result(Builder.TakeString())); | |||
5037 | ||||
5038 | // @selector ( selector ) | |||
5039 | Builder.AddResultTypeChunk("SEL"); | |||
5040 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector")((NeedAt)? "@" "selector" : "selector")); | |||
5041 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
5042 | Builder.AddPlaceholderChunk("selector"); | |||
5043 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
5044 | Results.AddResult(Result(Builder.TakeString())); | |||
5045 | ||||
5046 | // @"string" | |||
5047 | Builder.AddResultTypeChunk("NSString *"); | |||
5048 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\"")((NeedAt)? "@" "\"" : "\"")); | |||
5049 | Builder.AddPlaceholderChunk("string"); | |||
5050 | Builder.AddTextChunk("\""); | |||
5051 | Results.AddResult(Result(Builder.TakeString())); | |||
5052 | ||||
5053 | // @[objects, ...] | |||
5054 | Builder.AddResultTypeChunk("NSArray *"); | |||
5055 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"[")((NeedAt)? "@" "[" : "[")); | |||
5056 | Builder.AddPlaceholderChunk("objects, ..."); | |||
5057 | Builder.AddChunk(CodeCompletionString::CK_RightBracket); | |||
5058 | Results.AddResult(Result(Builder.TakeString())); | |||
5059 | ||||
5060 | // @{key : object, ...} | |||
5061 | Builder.AddResultTypeChunk("NSDictionary *"); | |||
5062 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{")((NeedAt)? "@" "{" : "{")); | |||
5063 | Builder.AddPlaceholderChunk("key"); | |||
5064 | Builder.AddChunk(CodeCompletionString::CK_Colon); | |||
5065 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
5066 | Builder.AddPlaceholderChunk("object, ..."); | |||
5067 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
5068 | Results.AddResult(Result(Builder.TakeString())); | |||
5069 | ||||
5070 | // @(expression) | |||
5071 | Builder.AddResultTypeChunk("id"); | |||
5072 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "(")((NeedAt)? "@" "(" : "(")); | |||
5073 | Builder.AddPlaceholderChunk("expression"); | |||
5074 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
5075 | Results.AddResult(Result(Builder.TakeString())); | |||
5076 | } | |||
5077 | ||||
5078 | static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) { | |||
5079 | typedef CodeCompletionResult Result; | |||
5080 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
5081 | Results.getCodeCompletionTUInfo()); | |||
5082 | ||||
5083 | if (Results.includeCodePatterns()) { | |||
5084 | // @try { statements } @catch ( declaration ) { statements } @finally | |||
5085 | // { statements } | |||
5086 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try")((NeedAt)? "@" "try" : "try")); | |||
5087 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
5088 | Builder.AddPlaceholderChunk("statements"); | |||
5089 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
5090 | Builder.AddTextChunk("@catch"); | |||
5091 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
5092 | Builder.AddPlaceholderChunk("parameter"); | |||
5093 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
5094 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
5095 | Builder.AddPlaceholderChunk("statements"); | |||
5096 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
5097 | Builder.AddTextChunk("@finally"); | |||
5098 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
5099 | Builder.AddPlaceholderChunk("statements"); | |||
5100 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
5101 | Results.AddResult(Result(Builder.TakeString())); | |||
5102 | } | |||
5103 | ||||
5104 | // @throw | |||
5105 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw")((NeedAt)? "@" "throw" : "throw")); | |||
5106 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
5107 | Builder.AddPlaceholderChunk("expression"); | |||
5108 | Results.AddResult(Result(Builder.TakeString())); | |||
5109 | ||||
5110 | if (Results.includeCodePatterns()) { | |||
5111 | // @synchronized ( expression ) { statements } | |||
5112 | Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized")((NeedAt)? "@" "synchronized" : "synchronized")); | |||
5113 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
5114 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
5115 | Builder.AddPlaceholderChunk("expression"); | |||
5116 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
5117 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
5118 | Builder.AddPlaceholderChunk("statements"); | |||
5119 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
5120 | Results.AddResult(Result(Builder.TakeString())); | |||
5121 | } | |||
5122 | } | |||
5123 | ||||
5124 | static void AddObjCVisibilityResults(const LangOptions &LangOpts, | |||
5125 | ResultBuilder &Results, | |||
5126 | bool NeedAt) { | |||
5127 | typedef CodeCompletionResult Result; | |||
5128 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private")((NeedAt)? "@" "private" : "private"))); | |||
5129 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected")((NeedAt)? "@" "protected" : "protected"))); | |||
5130 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public")((NeedAt)? "@" "public" : "public"))); | |||
5131 | if (LangOpts.ObjC2) | |||
5132 | Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package")((NeedAt)? "@" "package" : "package"))); | |||
5133 | } | |||
5134 | ||||
5135 | void Sema::CodeCompleteObjCAtVisibility(Scope *S) { | |||
5136 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
5137 | CodeCompleter->getCodeCompletionTUInfo(), | |||
5138 | CodeCompletionContext::CCC_Other); | |||
5139 | Results.EnterNewScope(); | |||
5140 | AddObjCVisibilityResults(getLangOpts(), Results, false); | |||
5141 | Results.ExitScope(); | |||
5142 | HandleCodeCompleteResults(this, CodeCompleter, | |||
5143 | CodeCompletionContext::CCC_Other, | |||
5144 | Results.data(),Results.size()); | |||
5145 | } | |||
5146 | ||||
5147 | void Sema::CodeCompleteObjCAtStatement(Scope *S) { | |||
5148 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
5149 | CodeCompleter->getCodeCompletionTUInfo(), | |||
5150 | CodeCompletionContext::CCC_Other); | |||
5151 | Results.EnterNewScope(); | |||
5152 | AddObjCStatementResults(Results, false); | |||
5153 | AddObjCExpressionResults(Results, false); | |||
5154 | Results.ExitScope(); | |||
5155 | HandleCodeCompleteResults(this, CodeCompleter, | |||
5156 | CodeCompletionContext::CCC_Other, | |||
5157 | Results.data(),Results.size()); | |||
5158 | } | |||
5159 | ||||
5160 | void Sema::CodeCompleteObjCAtExpression(Scope *S) { | |||
5161 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
5162 | CodeCompleter->getCodeCompletionTUInfo(), | |||
5163 | CodeCompletionContext::CCC_Other); | |||
5164 | Results.EnterNewScope(); | |||
5165 | AddObjCExpressionResults(Results, false); | |||
5166 | Results.ExitScope(); | |||
5167 | HandleCodeCompleteResults(this, CodeCompleter, | |||
5168 | CodeCompletionContext::CCC_Other, | |||
5169 | Results.data(),Results.size()); | |||
5170 | } | |||
5171 | ||||
5172 | /// \brief Determine whether the addition of the given flag to an Objective-C | |||
5173 | /// property's attributes will cause a conflict. | |||
5174 | static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) { | |||
5175 | // Check if we've already added this flag. | |||
5176 | if (Attributes & NewFlag) | |||
5177 | return true; | |||
5178 | ||||
5179 | Attributes |= NewFlag; | |||
5180 | ||||
5181 | // Check for collisions with "readonly". | |||
5182 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && | |||
5183 | (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) | |||
5184 | return true; | |||
5185 | ||||
5186 | // Check for more than one of { assign, copy, retain, strong, weak }. | |||
5187 | unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign | | |||
5188 | ObjCDeclSpec::DQ_PR_unsafe_unretained | | |||
5189 | ObjCDeclSpec::DQ_PR_copy | | |||
5190 | ObjCDeclSpec::DQ_PR_retain | | |||
5191 | ObjCDeclSpec::DQ_PR_strong | | |||
5192 | ObjCDeclSpec::DQ_PR_weak); | |||
5193 | if (AssignCopyRetMask && | |||
5194 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign && | |||
5195 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained && | |||
5196 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy && | |||
5197 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain && | |||
5198 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong && | |||
5199 | AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak) | |||
5200 | return true; | |||
5201 | ||||
5202 | return false; | |||
5203 | } | |||
5204 | ||||
5205 | void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { | |||
5206 | if (!CodeCompleter) | |||
5207 | return; | |||
5208 | ||||
5209 | unsigned Attributes = ODS.getPropertyAttributes(); | |||
5210 | ||||
5211 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
5212 | CodeCompleter->getCodeCompletionTUInfo(), | |||
5213 | CodeCompletionContext::CCC_Other); | |||
5214 | Results.EnterNewScope(); | |||
5215 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly)) | |||
5216 | Results.AddResult(CodeCompletionResult("readonly")); | |||
5217 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign)) | |||
5218 | Results.AddResult(CodeCompletionResult("assign")); | |||
5219 | if (!ObjCPropertyFlagConflicts(Attributes, | |||
5220 | ObjCDeclSpec::DQ_PR_unsafe_unretained)) | |||
5221 | Results.AddResult(CodeCompletionResult("unsafe_unretained")); | |||
5222 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite)) | |||
5223 | Results.AddResult(CodeCompletionResult("readwrite")); | |||
5224 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain)) | |||
5225 | Results.AddResult(CodeCompletionResult("retain")); | |||
5226 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong)) | |||
5227 | Results.AddResult(CodeCompletionResult("strong")); | |||
5228 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy)) | |||
5229 | Results.AddResult(CodeCompletionResult("copy")); | |||
5230 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic)) | |||
5231 | Results.AddResult(CodeCompletionResult("nonatomic")); | |||
5232 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic)) | |||
5233 | Results.AddResult(CodeCompletionResult("atomic")); | |||
5234 | ||||
5235 | // Only suggest "weak" if we're compiling for ARC-with-weak-references or GC. | |||
5236 | if (getLangOpts().ObjCWeak || getLangOpts().getGC() != LangOptions::NonGC) | |||
5237 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak)) | |||
5238 | Results.AddResult(CodeCompletionResult("weak")); | |||
5239 | ||||
5240 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) { | |||
5241 | CodeCompletionBuilder Setter(Results.getAllocator(), | |||
5242 | Results.getCodeCompletionTUInfo()); | |||
5243 | Setter.AddTypedTextChunk("setter"); | |||
5244 | Setter.AddTextChunk("="); | |||
5245 | Setter.AddPlaceholderChunk("method"); | |||
5246 | Results.AddResult(CodeCompletionResult(Setter.TakeString())); | |||
5247 | } | |||
5248 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) { | |||
5249 | CodeCompletionBuilder Getter(Results.getAllocator(), | |||
5250 | Results.getCodeCompletionTUInfo()); | |||
5251 | Getter.AddTypedTextChunk("getter"); | |||
5252 | Getter.AddTextChunk("="); | |||
5253 | Getter.AddPlaceholderChunk("method"); | |||
5254 | Results.AddResult(CodeCompletionResult(Getter.TakeString())); | |||
5255 | } | |||
5256 | if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nullability)) { | |||
5257 | Results.AddResult(CodeCompletionResult("nonnull")); | |||
5258 | Results.AddResult(CodeCompletionResult("nullable")); | |||
5259 | Results.AddResult(CodeCompletionResult("null_unspecified")); | |||
5260 | Results.AddResult(CodeCompletionResult("null_resettable")); | |||
5261 | } | |||
5262 | Results.ExitScope(); | |||
5263 | HandleCodeCompleteResults(this, CodeCompleter, | |||
5264 | CodeCompletionContext::CCC_Other, | |||
5265 | Results.data(),Results.size()); | |||
5266 | } | |||
5267 | ||||
5268 | /// \brief Describes the kind of Objective-C method that we want to find | |||
5269 | /// via code completion. | |||
5270 | enum ObjCMethodKind { | |||
5271 | MK_Any, ///< Any kind of method, provided it means other specified criteria. | |||
5272 | MK_ZeroArgSelector, ///< Zero-argument (unary) selector. | |||
5273 | MK_OneArgSelector ///< One-argument selector. | |||
5274 | }; | |||
5275 | ||||
5276 | static bool isAcceptableObjCSelector(Selector Sel, | |||
5277 | ObjCMethodKind WantKind, | |||
5278 | ArrayRef<IdentifierInfo *> SelIdents, | |||
5279 | bool AllowSameLength = true) { | |||
5280 | unsigned NumSelIdents = SelIdents.size(); | |||
5281 | if (NumSelIdents > Sel.getNumArgs()) | |||
5282 | return false; | |||
5283 | ||||
5284 | switch (WantKind) { | |||
5285 | case MK_Any: break; | |||
5286 | case MK_ZeroArgSelector: return Sel.isUnarySelector(); | |||
5287 | case MK_OneArgSelector: return Sel.getNumArgs() == 1; | |||
5288 | } | |||
5289 | ||||
5290 | if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs()) | |||
5291 | return false; | |||
5292 | ||||
5293 | for (unsigned I = 0; I != NumSelIdents; ++I) | |||
5294 | if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I)) | |||
5295 | return false; | |||
5296 | ||||
5297 | return true; | |||
5298 | } | |||
5299 | ||||
5300 | static bool isAcceptableObjCMethod(ObjCMethodDecl *Method, | |||
5301 | ObjCMethodKind WantKind, | |||
5302 | ArrayRef<IdentifierInfo *> SelIdents, | |||
5303 | bool AllowSameLength = true) { | |||
5304 | return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents, | |||
5305 | AllowSameLength); | |||
5306 | } | |||
5307 | ||||
5308 | namespace { | |||
5309 | /// \brief A set of selectors, which is used to avoid introducing multiple | |||
5310 | /// completions with the same selector into the result set. | |||
5311 | typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet; | |||
5312 | } | |||
5313 | ||||
5314 | /// \brief Add all of the Objective-C methods in the given Objective-C | |||
5315 | /// container to the set of results. | |||
5316 | /// | |||
5317 | /// The container will be a class, protocol, category, or implementation of | |||
5318 | /// any of the above. This mether will recurse to include methods from | |||
5319 | /// the superclasses of classes along with their categories, protocols, and | |||
5320 | /// implementations. | |||
5321 | /// | |||
5322 | /// \param Container the container in which we'll look to find methods. | |||
5323 | /// | |||
5324 | /// \param WantInstanceMethods Whether to add instance methods (only); if | |||
5325 | /// false, this routine will add factory methods (only). | |||
5326 | /// | |||
5327 | /// \param CurContext the context in which we're performing the lookup that | |||
5328 | /// finds methods. | |||
5329 | /// | |||
5330 | /// \param AllowSameLength Whether we allow a method to be added to the list | |||
5331 | /// when it has the same number of parameters as we have selector identifiers. | |||
5332 | /// | |||
5333 | /// \param Results the structure into which we'll add results. | |||
5334 | static void AddObjCMethods(ObjCContainerDecl *Container, | |||
5335 | bool WantInstanceMethods, ObjCMethodKind WantKind, | |||
5336 | ArrayRef<IdentifierInfo *> SelIdents, | |||
5337 | DeclContext *CurContext, | |||
5338 | VisitedSelectorSet &Selectors, bool AllowSameLength, | |||
5339 | ResultBuilder &Results, bool InOriginalClass = true, | |||
5340 | bool IsRootClass = false) { | |||
5341 | typedef CodeCompletionResult Result; | |||
5342 | Container = getContainerDef(Container); | |||
5343 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container); | |||
5344 | IsRootClass = IsRootClass || (IFace && !IFace->getSuperClass()); | |||
5345 | for (auto *M : Container->methods()) { | |||
5346 | // The instance methods on the root class can be messaged via the | |||
5347 | // metaclass. | |||
5348 | if (M->isInstanceMethod() == WantInstanceMethods || | |||
5349 | (IsRootClass && !WantInstanceMethods)) { | |||
5350 | // Check whether the selector identifiers we've been given are a | |||
5351 | // subset of the identifiers for this particular method. | |||
5352 | if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength)) | |||
5353 | continue; | |||
5354 | ||||
5355 | if (!Selectors.insert(M->getSelector()).second) | |||
5356 | continue; | |||
5357 | ||||
5358 | Result R = Result(M, Results.getBasePriority(M), nullptr); | |||
5359 | R.StartParameter = SelIdents.size(); | |||
5360 | R.AllParametersAreInformative = (WantKind != MK_Any); | |||
5361 | if (!InOriginalClass) | |||
5362 | R.Priority += CCD_InBaseClass; | |||
5363 | Results.MaybeAddResult(R, CurContext); | |||
5364 | } | |||
5365 | } | |||
5366 | ||||
5367 | // Visit the protocols of protocols. | |||
5368 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { | |||
5369 | if (Protocol->hasDefinition()) { | |||
5370 | const ObjCList<ObjCProtocolDecl> &Protocols | |||
5371 | = Protocol->getReferencedProtocols(); | |||
5372 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | |||
5373 | E = Protocols.end(); | |||
5374 | I != E; ++I) | |||
5375 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, | |||
5376 | Selectors, AllowSameLength, Results, false, IsRootClass); | |||
5377 | } | |||
5378 | } | |||
5379 | ||||
5380 | if (!IFace || !IFace->hasDefinition()) | |||
5381 | return; | |||
5382 | ||||
5383 | // Add methods in protocols. | |||
5384 | for (auto *I : IFace->protocols()) | |||
5385 | AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents, CurContext, | |||
5386 | Selectors, AllowSameLength, Results, false, IsRootClass); | |||
5387 | ||||
5388 | // Add methods in categories. | |||
5389 | for (auto *CatDecl : IFace->known_categories()) { | |||
5390 | AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents, | |||
5391 | CurContext, Selectors, AllowSameLength, Results, | |||
5392 | InOriginalClass, IsRootClass); | |||
5393 | ||||
5394 | // Add a categories protocol methods. | |||
5395 | const ObjCList<ObjCProtocolDecl> &Protocols | |||
5396 | = CatDecl->getReferencedProtocols(); | |||
5397 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | |||
5398 | E = Protocols.end(); | |||
5399 | I != E; ++I) | |||
5400 | AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, CurContext, | |||
5401 | Selectors, AllowSameLength, Results, false, IsRootClass); | |||
5402 | ||||
5403 | // Add methods in category implementations. | |||
5404 | if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation()) | |||
5405 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, | |||
5406 | Selectors, AllowSameLength, Results, InOriginalClass, | |||
5407 | IsRootClass); | |||
5408 | } | |||
5409 | ||||
5410 | // Add methods in superclass. | |||
5411 | // Avoid passing in IsRootClass since root classes won't have super classes. | |||
5412 | if (IFace->getSuperClass()) | |||
5413 | AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind, | |||
5414 | SelIdents, CurContext, Selectors, AllowSameLength, Results, | |||
5415 | /*IsRootClass=*/false); | |||
5416 | ||||
5417 | // Add methods in our implementation, if any. | |||
5418 | if (ObjCImplementationDecl *Impl = IFace->getImplementation()) | |||
5419 | AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents, CurContext, | |||
5420 | Selectors, AllowSameLength, Results, InOriginalClass, | |||
5421 | IsRootClass); | |||
5422 | } | |||
5423 | ||||
5424 | ||||
5425 | void Sema::CodeCompleteObjCPropertyGetter(Scope *S) { | |||
5426 | // Try to find the interface where getters might live. | |||
5427 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); | |||
5428 | if (!Class) { | |||
5429 | if (ObjCCategoryDecl *Category | |||
5430 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) | |||
5431 | Class = Category->getClassInterface(); | |||
5432 | ||||
5433 | if (!Class) | |||
5434 | return; | |||
5435 | } | |||
5436 | ||||
5437 | // Find all of the potential getters. | |||
5438 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
5439 | CodeCompleter->getCodeCompletionTUInfo(), | |||
5440 | CodeCompletionContext::CCC_Other); | |||
5441 | Results.EnterNewScope(); | |||
5442 | ||||
5443 | VisitedSelectorSet Selectors; | |||
5444 | AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors, | |||
5445 | /*AllowSameLength=*/true, Results); | |||
5446 | Results.ExitScope(); | |||
5447 | HandleCodeCompleteResults(this, CodeCompleter, | |||
5448 | CodeCompletionContext::CCC_Other, | |||
5449 | Results.data(),Results.size()); | |||
5450 | } | |||
5451 | ||||
5452 | void Sema::CodeCompleteObjCPropertySetter(Scope *S) { | |||
5453 | // Try to find the interface where setters might live. | |||
5454 | ObjCInterfaceDecl *Class | |||
5455 | = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext); | |||
5456 | if (!Class) { | |||
5457 | if (ObjCCategoryDecl *Category | |||
5458 | = dyn_cast_or_null<ObjCCategoryDecl>(CurContext)) | |||
5459 | Class = Category->getClassInterface(); | |||
5460 | ||||
5461 | if (!Class) | |||
5462 | return; | |||
5463 | } | |||
5464 | ||||
5465 | // Find all of the potential getters. | |||
5466 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
5467 | CodeCompleter->getCodeCompletionTUInfo(), | |||
5468 | CodeCompletionContext::CCC_Other); | |||
5469 | Results.EnterNewScope(); | |||
5470 | ||||
5471 | VisitedSelectorSet Selectors; | |||
5472 | AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext, | |||
5473 | Selectors, /*AllowSameLength=*/true, Results); | |||
5474 | ||||
5475 | Results.ExitScope(); | |||
5476 | HandleCodeCompleteResults(this, CodeCompleter, | |||
5477 | CodeCompletionContext::CCC_Other, | |||
5478 | Results.data(),Results.size()); | |||
5479 | } | |||
5480 | ||||
5481 | void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS, | |||
5482 | bool IsParameter) { | |||
5483 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
5484 | CodeCompleter->getCodeCompletionTUInfo(), | |||
5485 | CodeCompletionContext::CCC_Type); | |||
5486 | Results.EnterNewScope(); | |||
5487 | ||||
5488 | // Add context-sensitive, Objective-C parameter-passing keywords. | |||
5489 | bool AddedInOut = false; | |||
5490 | if ((DS.getObjCDeclQualifier() & | |||
5491 | (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) { | |||
5492 | Results.AddResult("in"); | |||
5493 | Results.AddResult("inout"); | |||
5494 | AddedInOut = true; | |||
5495 | } | |||
5496 | if ((DS.getObjCDeclQualifier() & | |||
5497 | (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) { | |||
5498 | Results.AddResult("out"); | |||
5499 | if (!AddedInOut) | |||
5500 | Results.AddResult("inout"); | |||
5501 | } | |||
5502 | if ((DS.getObjCDeclQualifier() & | |||
5503 | (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref | | |||
5504 | ObjCDeclSpec::DQ_Oneway)) == 0) { | |||
5505 | Results.AddResult("bycopy"); | |||
5506 | Results.AddResult("byref"); | |||
5507 | Results.AddResult("oneway"); | |||
5508 | } | |||
5509 | if ((DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) == 0) { | |||
5510 | Results.AddResult("nonnull"); | |||
5511 | Results.AddResult("nullable"); | |||
5512 | Results.AddResult("null_unspecified"); | |||
5513 | } | |||
5514 | ||||
5515 | // If we're completing the return type of an Objective-C method and the | |||
5516 | // identifier IBAction refers to a macro, provide a completion item for | |||
5517 | // an action, e.g., | |||
5518 | // IBAction)<#selector#>:(id)sender | |||
5519 | if (DS.getObjCDeclQualifier() == 0 && !IsParameter && | |||
5520 | PP.isMacroDefined("IBAction")) { | |||
5521 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
5522 | Results.getCodeCompletionTUInfo(), | |||
5523 | CCP_CodePattern, CXAvailability_Available); | |||
5524 | Builder.AddTypedTextChunk("IBAction"); | |||
5525 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
5526 | Builder.AddPlaceholderChunk("selector"); | |||
5527 | Builder.AddChunk(CodeCompletionString::CK_Colon); | |||
5528 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
5529 | Builder.AddTextChunk("id"); | |||
5530 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
5531 | Builder.AddTextChunk("sender"); | |||
5532 | Results.AddResult(CodeCompletionResult(Builder.TakeString())); | |||
5533 | } | |||
5534 | ||||
5535 | // If we're completing the return type, provide 'instancetype'. | |||
5536 | if (!IsParameter) { | |||
5537 | Results.AddResult(CodeCompletionResult("instancetype")); | |||
5538 | } | |||
5539 | ||||
5540 | // Add various builtin type names and specifiers. | |||
5541 | AddOrdinaryNameResults(PCC_Type, S, *this, Results); | |||
5542 | Results.ExitScope(); | |||
5543 | ||||
5544 | // Add the various type names | |||
5545 | Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); | |||
5546 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
5547 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | |||
5548 | CodeCompleter->includeGlobals()); | |||
5549 | ||||
5550 | if (CodeCompleter->includeMacros()) | |||
5551 | AddMacroResults(PP, Results, false); | |||
5552 | ||||
5553 | HandleCodeCompleteResults(this, CodeCompleter, | |||
5554 | CodeCompletionContext::CCC_Type, | |||
5555 | Results.data(), Results.size()); | |||
5556 | } | |||
5557 | ||||
5558 | /// \brief When we have an expression with type "id", we may assume | |||
5559 | /// that it has some more-specific class type based on knowledge of | |||
5560 | /// common uses of Objective-C. This routine returns that class type, | |||
5561 | /// or NULL if no better result could be determined. | |||
5562 | static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) { | |||
5563 | ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E); | |||
5564 | if (!Msg) | |||
5565 | return nullptr; | |||
5566 | ||||
5567 | Selector Sel = Msg->getSelector(); | |||
5568 | if (Sel.isNull()) | |||
5569 | return nullptr; | |||
5570 | ||||
5571 | IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0); | |||
5572 | if (!Id) | |||
5573 | return nullptr; | |||
5574 | ||||
5575 | ObjCMethodDecl *Method = Msg->getMethodDecl(); | |||
5576 | if (!Method) | |||
5577 | return nullptr; | |||
5578 | ||||
5579 | // Determine the class that we're sending the message to. | |||
5580 | ObjCInterfaceDecl *IFace = nullptr; | |||
5581 | switch (Msg->getReceiverKind()) { | |||
5582 | case ObjCMessageExpr::Class: | |||
5583 | if (const ObjCObjectType *ObjType | |||
5584 | = Msg->getClassReceiver()->getAs<ObjCObjectType>()) | |||
5585 | IFace = ObjType->getInterface(); | |||
5586 | break; | |||
5587 | ||||
5588 | case ObjCMessageExpr::Instance: { | |||
5589 | QualType T = Msg->getInstanceReceiver()->getType(); | |||
5590 | if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) | |||
5591 | IFace = Ptr->getInterfaceDecl(); | |||
5592 | break; | |||
5593 | } | |||
5594 | ||||
5595 | case ObjCMessageExpr::SuperInstance: | |||
5596 | case ObjCMessageExpr::SuperClass: | |||
5597 | break; | |||
5598 | } | |||
5599 | ||||
5600 | if (!IFace) | |||
5601 | return nullptr; | |||
5602 | ||||
5603 | ObjCInterfaceDecl *Super = IFace->getSuperClass(); | |||
5604 | if (Method->isInstanceMethod()) | |||
5605 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) | |||
5606 | .Case("retain", IFace) | |||
5607 | .Case("strong", IFace) | |||
5608 | .Case("autorelease", IFace) | |||
5609 | .Case("copy", IFace) | |||
5610 | .Case("copyWithZone", IFace) | |||
5611 | .Case("mutableCopy", IFace) | |||
5612 | .Case("mutableCopyWithZone", IFace) | |||
5613 | .Case("awakeFromCoder", IFace) | |||
5614 | .Case("replacementObjectFromCoder", IFace) | |||
5615 | .Case("class", IFace) | |||
5616 | .Case("classForCoder", IFace) | |||
5617 | .Case("superclass", Super) | |||
5618 | .Default(nullptr); | |||
5619 | ||||
5620 | return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName()) | |||
5621 | .Case("new", IFace) | |||
5622 | .Case("alloc", IFace) | |||
5623 | .Case("allocWithZone", IFace) | |||
5624 | .Case("class", IFace) | |||
5625 | .Case("superclass", Super) | |||
5626 | .Default(nullptr); | |||
5627 | } | |||
5628 | ||||
5629 | // Add a special completion for a message send to "super", which fills in the | |||
5630 | // most likely case of forwarding all of our arguments to the superclass | |||
5631 | // function. | |||
5632 | /// | |||
5633 | /// \param S The semantic analysis object. | |||
5634 | /// | |||
5635 | /// \param NeedSuperKeyword Whether we need to prefix this completion with | |||
5636 | /// the "super" keyword. Otherwise, we just need to provide the arguments. | |||
5637 | /// | |||
5638 | /// \param SelIdents The identifiers in the selector that have already been | |||
5639 | /// provided as arguments for a send to "super". | |||
5640 | /// | |||
5641 | /// \param Results The set of results to augment. | |||
5642 | /// | |||
5643 | /// \returns the Objective-C method declaration that would be invoked by | |||
5644 | /// this "super" completion. If NULL, no completion was added. | |||
5645 | static ObjCMethodDecl *AddSuperSendCompletion( | |||
5646 | Sema &S, bool NeedSuperKeyword, | |||
5647 | ArrayRef<IdentifierInfo *> SelIdents, | |||
5648 | ResultBuilder &Results) { | |||
5649 | ObjCMethodDecl *CurMethod = S.getCurMethodDecl(); | |||
5650 | if (!CurMethod) | |||
5651 | return nullptr; | |||
5652 | ||||
5653 | ObjCInterfaceDecl *Class = CurMethod->getClassInterface(); | |||
5654 | if (!Class) | |||
5655 | return nullptr; | |||
5656 | ||||
5657 | // Try to find a superclass method with the same selector. | |||
5658 | ObjCMethodDecl *SuperMethod = nullptr; | |||
5659 | while ((Class = Class->getSuperClass()) && !SuperMethod) { | |||
5660 | // Check in the class | |||
5661 | SuperMethod = Class->getMethod(CurMethod->getSelector(), | |||
5662 | CurMethod->isInstanceMethod()); | |||
5663 | ||||
5664 | // Check in categories or class extensions. | |||
5665 | if (!SuperMethod) { | |||
5666 | for (const auto *Cat : Class->known_categories()) { | |||
5667 | if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(), | |||
5668 | CurMethod->isInstanceMethod()))) | |||
5669 | break; | |||
5670 | } | |||
5671 | } | |||
5672 | } | |||
5673 | ||||
5674 | if (!SuperMethod) | |||
5675 | return nullptr; | |||
5676 | ||||
5677 | // Check whether the superclass method has the same signature. | |||
5678 | if (CurMethod->param_size() != SuperMethod->param_size() || | |||
5679 | CurMethod->isVariadic() != SuperMethod->isVariadic()) | |||
5680 | return nullptr; | |||
5681 | ||||
5682 | for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(), | |||
5683 | CurPEnd = CurMethod->param_end(), | |||
5684 | SuperP = SuperMethod->param_begin(); | |||
5685 | CurP != CurPEnd; ++CurP, ++SuperP) { | |||
5686 | // Make sure the parameter types are compatible. | |||
5687 | if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(), | |||
5688 | (*SuperP)->getType())) | |||
5689 | return nullptr; | |||
5690 | ||||
5691 | // Make sure we have a parameter name to forward! | |||
5692 | if (!(*CurP)->getIdentifier()) | |||
5693 | return nullptr; | |||
5694 | } | |||
5695 | ||||
5696 | // We have a superclass method. Now, form the send-to-super completion. | |||
5697 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
5698 | Results.getCodeCompletionTUInfo()); | |||
5699 | ||||
5700 | // Give this completion a return type. | |||
5701 | AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod, | |||
5702 | Results.getCompletionContext().getBaseType(), | |||
5703 | Builder); | |||
5704 | ||||
5705 | // If we need the "super" keyword, add it (plus some spacing). | |||
5706 | if (NeedSuperKeyword) { | |||
5707 | Builder.AddTypedTextChunk("super"); | |||
5708 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
5709 | } | |||
5710 | ||||
5711 | Selector Sel = CurMethod->getSelector(); | |||
5712 | if (Sel.isUnarySelector()) { | |||
5713 | if (NeedSuperKeyword) | |||
5714 | Builder.AddTextChunk(Builder.getAllocator().CopyString( | |||
5715 | Sel.getNameForSlot(0))); | |||
5716 | else | |||
5717 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | |||
5718 | Sel.getNameForSlot(0))); | |||
5719 | } else { | |||
5720 | ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(); | |||
5721 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) { | |||
5722 | if (I > SelIdents.size()) | |||
5723 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
5724 | ||||
5725 | if (I < SelIdents.size()) | |||
5726 | Builder.AddInformativeChunk( | |||
5727 | Builder.getAllocator().CopyString( | |||
5728 | Sel.getNameForSlot(I) + ":")); | |||
5729 | else if (NeedSuperKeyword || I > SelIdents.size()) { | |||
5730 | Builder.AddTextChunk( | |||
5731 | Builder.getAllocator().CopyString( | |||
5732 | Sel.getNameForSlot(I) + ":")); | |||
5733 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( | |||
5734 | (*CurP)->getIdentifier()->getName())); | |||
5735 | } else { | |||
5736 | Builder.AddTypedTextChunk( | |||
5737 | Builder.getAllocator().CopyString( | |||
5738 | Sel.getNameForSlot(I) + ":")); | |||
5739 | Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString( | |||
5740 | (*CurP)->getIdentifier()->getName())); | |||
5741 | } | |||
5742 | } | |||
5743 | } | |||
5744 | ||||
5745 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod, | |||
5746 | CCP_SuperCompletion)); | |||
5747 | return SuperMethod; | |||
5748 | } | |||
5749 | ||||
5750 | void Sema::CodeCompleteObjCMessageReceiver(Scope *S) { | |||
5751 | typedef CodeCompletionResult Result; | |||
5752 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
5753 | CodeCompleter->getCodeCompletionTUInfo(), | |||
5754 | CodeCompletionContext::CCC_ObjCMessageReceiver, | |||
5755 | getLangOpts().CPlusPlus11 | |||
5756 | ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture | |||
5757 | : &ResultBuilder::IsObjCMessageReceiver); | |||
5758 | ||||
5759 | CodeCompletionDeclConsumer Consumer(Results, CurContext); | |||
5760 | Results.EnterNewScope(); | |||
5761 | LookupVisibleDecls(S, LookupOrdinaryName, Consumer, | |||
5762 | CodeCompleter->includeGlobals()); | |||
5763 | ||||
5764 | // If we are in an Objective-C method inside a class that has a superclass, | |||
5765 | // add "super" as an option. | |||
5766 | if (ObjCMethodDecl *Method = getCurMethodDecl()) | |||
5767 | if (ObjCInterfaceDecl *Iface = Method->getClassInterface()) | |||
5768 | if (Iface->getSuperClass()) { | |||
5769 | Results.AddResult(Result("super")); | |||
5770 | ||||
5771 | AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results); | |||
5772 | } | |||
5773 | ||||
5774 | if (getLangOpts().CPlusPlus11) | |||
5775 | addThisCompletion(*this, Results); | |||
5776 | ||||
5777 | Results.ExitScope(); | |||
5778 | ||||
5779 | if (CodeCompleter->includeMacros()) | |||
5780 | AddMacroResults(PP, Results, false); | |||
5781 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | |||
5782 | Results.data(), Results.size()); | |||
5783 | ||||
5784 | } | |||
5785 | ||||
5786 | void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc, | |||
5787 | ArrayRef<IdentifierInfo *> SelIdents, | |||
5788 | bool AtArgumentExpression) { | |||
5789 | ObjCInterfaceDecl *CDecl = nullptr; | |||
5790 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { | |||
5791 | // Figure out which interface we're in. | |||
5792 | CDecl = CurMethod->getClassInterface(); | |||
5793 | if (!CDecl) | |||
5794 | return; | |||
5795 | ||||
5796 | // Find the superclass of this class. | |||
5797 | CDecl = CDecl->getSuperClass(); | |||
5798 | if (!CDecl) | |||
5799 | return; | |||
5800 | ||||
5801 | if (CurMethod->isInstanceMethod()) { | |||
5802 | // We are inside an instance method, which means that the message | |||
5803 | // send [super ...] is actually calling an instance method on the | |||
5804 | // current object. | |||
5805 | return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents, | |||
5806 | AtArgumentExpression, | |||
5807 | CDecl); | |||
5808 | } | |||
5809 | ||||
5810 | // Fall through to send to the superclass in CDecl. | |||
5811 | } else { | |||
5812 | // "super" may be the name of a type or variable. Figure out which | |||
5813 | // it is. | |||
5814 | IdentifierInfo *Super = getSuperIdentifier(); | |||
5815 | NamedDecl *ND = LookupSingleName(S, Super, SuperLoc, | |||
5816 | LookupOrdinaryName); | |||
5817 | if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) { | |||
5818 | // "super" names an interface. Use it. | |||
5819 | } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) { | |||
5820 | if (const ObjCObjectType *Iface | |||
5821 | = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>()) | |||
5822 | CDecl = Iface->getInterface(); | |||
5823 | } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) { | |||
5824 | // "super" names an unresolved type; we can't be more specific. | |||
5825 | } else { | |||
5826 | // Assume that "super" names some kind of value and parse that way. | |||
5827 | CXXScopeSpec SS; | |||
5828 | SourceLocation TemplateKWLoc; | |||
5829 | UnqualifiedId id; | |||
5830 | id.setIdentifier(Super, SuperLoc); | |||
5831 | ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id, | |||
5832 | false, false); | |||
5833 | return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(), | |||
5834 | SelIdents, | |||
5835 | AtArgumentExpression); | |||
5836 | } | |||
5837 | ||||
5838 | // Fall through | |||
5839 | } | |||
5840 | ||||
5841 | ParsedType Receiver; | |||
5842 | if (CDecl) | |||
5843 | Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl)); | |||
5844 | return CodeCompleteObjCClassMessage(S, Receiver, SelIdents, | |||
5845 | AtArgumentExpression, | |||
5846 | /*IsSuper=*/true); | |||
5847 | } | |||
5848 | ||||
5849 | /// \brief Given a set of code-completion results for the argument of a message | |||
5850 | /// send, determine the preferred type (if any) for that argument expression. | |||
5851 | static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results, | |||
5852 | unsigned NumSelIdents) { | |||
5853 | typedef CodeCompletionResult Result; | |||
5854 | ASTContext &Context = Results.getSema().Context; | |||
5855 | ||||
5856 | QualType PreferredType; | |||
5857 | unsigned BestPriority = CCP_Unlikely * 2; | |||
5858 | Result *ResultsData = Results.data(); | |||
5859 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { | |||
5860 | Result &R = ResultsData[I]; | |||
5861 | if (R.Kind == Result::RK_Declaration && | |||
5862 | isa<ObjCMethodDecl>(R.Declaration)) { | |||
5863 | if (R.Priority <= BestPriority) { | |||
5864 | const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration); | |||
5865 | if (NumSelIdents <= Method->param_size()) { | |||
5866 | QualType MyPreferredType = Method->parameters()[NumSelIdents - 1] | |||
5867 | ->getType(); | |||
5868 | if (R.Priority < BestPriority || PreferredType.isNull()) { | |||
5869 | BestPriority = R.Priority; | |||
5870 | PreferredType = MyPreferredType; | |||
5871 | } else if (!Context.hasSameUnqualifiedType(PreferredType, | |||
5872 | MyPreferredType)) { | |||
5873 | PreferredType = QualType(); | |||
5874 | } | |||
5875 | } | |||
5876 | } | |||
5877 | } | |||
5878 | } | |||
5879 | ||||
5880 | return PreferredType; | |||
5881 | } | |||
5882 | ||||
5883 | static void AddClassMessageCompletions(Sema &SemaRef, Scope *S, | |||
5884 | ParsedType Receiver, | |||
5885 | ArrayRef<IdentifierInfo *> SelIdents, | |||
5886 | bool AtArgumentExpression, | |||
5887 | bool IsSuper, | |||
5888 | ResultBuilder &Results) { | |||
5889 | typedef CodeCompletionResult Result; | |||
5890 | ObjCInterfaceDecl *CDecl = nullptr; | |||
5891 | ||||
5892 | // If the given name refers to an interface type, retrieve the | |||
5893 | // corresponding declaration. | |||
5894 | if (Receiver) { | |||
5895 | QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr); | |||
5896 | if (!T.isNull()) | |||
5897 | if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>()) | |||
5898 | CDecl = Interface->getInterface(); | |||
5899 | } | |||
5900 | ||||
5901 | // Add all of the factory methods in this Objective-C class, its protocols, | |||
5902 | // superclasses, categories, implementation, etc. | |||
5903 | Results.EnterNewScope(); | |||
5904 | ||||
5905 | // If this is a send-to-super, try to add the special "super" send | |||
5906 | // completion. | |||
5907 | if (IsSuper) { | |||
5908 | if (ObjCMethodDecl *SuperMethod | |||
5909 | = AddSuperSendCompletion(SemaRef, false, SelIdents, Results)) | |||
5910 | Results.Ignore(SuperMethod); | |||
5911 | } | |||
5912 | ||||
5913 | // If we're inside an Objective-C method definition, prefer its selector to | |||
5914 | // others. | |||
5915 | if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl()) | |||
5916 | Results.setPreferredSelector(CurMethod->getSelector()); | |||
5917 | ||||
5918 | VisitedSelectorSet Selectors; | |||
5919 | if (CDecl) | |||
5920 | AddObjCMethods(CDecl, false, MK_Any, SelIdents, | |||
5921 | SemaRef.CurContext, Selectors, AtArgumentExpression, | |||
5922 | Results); | |||
5923 | else { | |||
5924 | // We're messaging "id" as a type; provide all class/factory methods. | |||
5925 | ||||
5926 | // If we have an external source, load the entire class method | |||
5927 | // pool from the AST file. | |||
5928 | if (SemaRef.getExternalSource()) { | |||
5929 | for (uint32_t I = 0, | |||
5930 | N = SemaRef.getExternalSource()->GetNumExternalSelectors(); | |||
5931 | I != N; ++I) { | |||
5932 | Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I); | |||
5933 | if (Sel.isNull() || SemaRef.MethodPool.count(Sel)) | |||
5934 | continue; | |||
5935 | ||||
5936 | SemaRef.ReadMethodPool(Sel); | |||
5937 | } | |||
5938 | } | |||
5939 | ||||
5940 | for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(), | |||
5941 | MEnd = SemaRef.MethodPool.end(); | |||
5942 | M != MEnd; ++M) { | |||
5943 | for (ObjCMethodList *MethList = &M->second.second; | |||
5944 | MethList && MethList->getMethod(); | |||
5945 | MethList = MethList->getNext()) { | |||
5946 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) | |||
5947 | continue; | |||
5948 | ||||
5949 | Result R(MethList->getMethod(), | |||
5950 | Results.getBasePriority(MethList->getMethod()), nullptr); | |||
5951 | R.StartParameter = SelIdents.size(); | |||
5952 | R.AllParametersAreInformative = false; | |||
5953 | Results.MaybeAddResult(R, SemaRef.CurContext); | |||
5954 | } | |||
5955 | } | |||
5956 | } | |||
5957 | ||||
5958 | Results.ExitScope(); | |||
5959 | } | |||
5960 | ||||
5961 | void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver, | |||
5962 | ArrayRef<IdentifierInfo *> SelIdents, | |||
5963 | bool AtArgumentExpression, | |||
5964 | bool IsSuper) { | |||
5965 | ||||
5966 | QualType T = this->GetTypeFromParser(Receiver); | |||
5967 | ||||
5968 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
5969 | CodeCompleter->getCodeCompletionTUInfo(), | |||
5970 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage, | |||
5971 | T, SelIdents)); | |||
5972 | ||||
5973 | AddClassMessageCompletions(*this, S, Receiver, SelIdents, | |||
5974 | AtArgumentExpression, IsSuper, Results); | |||
5975 | ||||
5976 | // If we're actually at the argument expression (rather than prior to the | |||
5977 | // selector), we're actually performing code completion for an expression. | |||
5978 | // Determine whether we have a single, best method. If so, we can | |||
5979 | // code-complete the expression using the corresponding parameter type as | |||
5980 | // our preferred type, improving completion results. | |||
5981 | if (AtArgumentExpression) { | |||
5982 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, | |||
5983 | SelIdents.size()); | |||
5984 | if (PreferredType.isNull()) | |||
5985 | CodeCompleteOrdinaryName(S, PCC_Expression); | |||
5986 | else | |||
5987 | CodeCompleteExpression(S, PreferredType); | |||
5988 | return; | |||
5989 | } | |||
5990 | ||||
5991 | HandleCodeCompleteResults(this, CodeCompleter, | |||
5992 | Results.getCompletionContext(), | |||
5993 | Results.data(), Results.size()); | |||
5994 | } | |||
5995 | ||||
5996 | void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver, | |||
5997 | ArrayRef<IdentifierInfo *> SelIdents, | |||
5998 | bool AtArgumentExpression, | |||
5999 | ObjCInterfaceDecl *Super) { | |||
6000 | typedef CodeCompletionResult Result; | |||
6001 | ||||
6002 | Expr *RecExpr = static_cast<Expr *>(Receiver); | |||
6003 | ||||
6004 | // If necessary, apply function/array conversion to the receiver. | |||
6005 | // C99 6.7.5.3p[7,8]. | |||
6006 | if (RecExpr) { | |||
6007 | ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr); | |||
6008 | if (Conv.isInvalid()) // conversion failed. bail. | |||
6009 | return; | |||
6010 | RecExpr = Conv.get(); | |||
6011 | } | |||
6012 | QualType ReceiverType = RecExpr? RecExpr->getType() | |||
6013 | : Super? Context.getObjCObjectPointerType( | |||
6014 | Context.getObjCInterfaceType(Super)) | |||
6015 | : Context.getObjCIdType(); | |||
6016 | ||||
6017 | // If we're messaging an expression with type "id" or "Class", check | |||
6018 | // whether we know something special about the receiver that allows | |||
6019 | // us to assume a more-specific receiver type. | |||
6020 | if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) { | |||
6021 | if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) { | |||
6022 | if (ReceiverType->isObjCClassType()) | |||
6023 | return CodeCompleteObjCClassMessage(S, | |||
6024 | ParsedType::make(Context.getObjCInterfaceType(IFace)), | |||
6025 | SelIdents, | |||
6026 | AtArgumentExpression, Super); | |||
6027 | ||||
6028 | ReceiverType = Context.getObjCObjectPointerType( | |||
6029 | Context.getObjCInterfaceType(IFace)); | |||
6030 | } | |||
6031 | } else if (RecExpr && getLangOpts().CPlusPlus) { | |||
6032 | ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr); | |||
6033 | if (Conv.isUsable()) { | |||
6034 | RecExpr = Conv.get(); | |||
6035 | ReceiverType = RecExpr->getType(); | |||
6036 | } | |||
6037 | } | |||
6038 | ||||
6039 | // Build the set of methods we can see. | |||
6040 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6041 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6042 | CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage, | |||
6043 | ReceiverType, SelIdents)); | |||
6044 | ||||
6045 | Results.EnterNewScope(); | |||
6046 | ||||
6047 | // If this is a send-to-super, try to add the special "super" send | |||
6048 | // completion. | |||
6049 | if (Super) { | |||
6050 | if (ObjCMethodDecl *SuperMethod | |||
6051 | = AddSuperSendCompletion(*this, false, SelIdents, Results)) | |||
6052 | Results.Ignore(SuperMethod); | |||
6053 | } | |||
6054 | ||||
6055 | // If we're inside an Objective-C method definition, prefer its selector to | |||
6056 | // others. | |||
6057 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) | |||
6058 | Results.setPreferredSelector(CurMethod->getSelector()); | |||
6059 | ||||
6060 | // Keep track of the selectors we've already added. | |||
6061 | VisitedSelectorSet Selectors; | |||
6062 | ||||
6063 | // Handle messages to Class. This really isn't a message to an instance | |||
6064 | // method, so we treat it the same way we would treat a message send to a | |||
6065 | // class method. | |||
6066 | if (ReceiverType->isObjCClassType() || | |||
6067 | ReceiverType->isObjCQualifiedClassType()) { | |||
6068 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) { | |||
6069 | if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface()) | |||
6070 | AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, | |||
6071 | CurContext, Selectors, AtArgumentExpression, Results); | |||
6072 | } | |||
6073 | } | |||
6074 | // Handle messages to a qualified ID ("id<foo>"). | |||
6075 | else if (const ObjCObjectPointerType *QualID | |||
6076 | = ReceiverType->getAsObjCQualifiedIdType()) { | |||
6077 | // Search protocols for instance methods. | |||
6078 | for (auto *I : QualID->quals()) | |||
6079 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, | |||
6080 | Selectors, AtArgumentExpression, Results); | |||
6081 | } | |||
6082 | // Handle messages to a pointer to interface type. | |||
6083 | else if (const ObjCObjectPointerType *IFacePtr | |||
6084 | = ReceiverType->getAsObjCInterfacePointerType()) { | |||
6085 | // Search the class, its superclasses, etc., for instance methods. | |||
6086 | AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents, | |||
6087 | CurContext, Selectors, AtArgumentExpression, | |||
6088 | Results); | |||
6089 | ||||
6090 | // Search protocols for instance methods. | |||
6091 | for (auto *I : IFacePtr->quals()) | |||
6092 | AddObjCMethods(I, true, MK_Any, SelIdents, CurContext, | |||
6093 | Selectors, AtArgumentExpression, Results); | |||
6094 | } | |||
6095 | // Handle messages to "id". | |||
6096 | else if (ReceiverType->isObjCIdType()) { | |||
6097 | // We're messaging "id", so provide all instance methods we know | |||
6098 | // about as code-completion results. | |||
6099 | ||||
6100 | // If we have an external source, load the entire class method | |||
6101 | // pool from the AST file. | |||
6102 | if (ExternalSource) { | |||
6103 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); | |||
6104 | I != N; ++I) { | |||
6105 | Selector Sel = ExternalSource->GetExternalSelector(I); | |||
6106 | if (Sel.isNull() || MethodPool.count(Sel)) | |||
6107 | continue; | |||
6108 | ||||
6109 | ReadMethodPool(Sel); | |||
6110 | } | |||
6111 | } | |||
6112 | ||||
6113 | for (GlobalMethodPool::iterator M = MethodPool.begin(), | |||
6114 | MEnd = MethodPool.end(); | |||
6115 | M != MEnd; ++M) { | |||
6116 | for (ObjCMethodList *MethList = &M->second.first; | |||
6117 | MethList && MethList->getMethod(); | |||
6118 | MethList = MethList->getNext()) { | |||
6119 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) | |||
6120 | continue; | |||
6121 | ||||
6122 | if (!Selectors.insert(MethList->getMethod()->getSelector()).second) | |||
6123 | continue; | |||
6124 | ||||
6125 | Result R(MethList->getMethod(), | |||
6126 | Results.getBasePriority(MethList->getMethod()), nullptr); | |||
6127 | R.StartParameter = SelIdents.size(); | |||
6128 | R.AllParametersAreInformative = false; | |||
6129 | Results.MaybeAddResult(R, CurContext); | |||
6130 | } | |||
6131 | } | |||
6132 | } | |||
6133 | Results.ExitScope(); | |||
6134 | ||||
6135 | ||||
6136 | // If we're actually at the argument expression (rather than prior to the | |||
6137 | // selector), we're actually performing code completion for an expression. | |||
6138 | // Determine whether we have a single, best method. If so, we can | |||
6139 | // code-complete the expression using the corresponding parameter type as | |||
6140 | // our preferred type, improving completion results. | |||
6141 | if (AtArgumentExpression) { | |||
6142 | QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results, | |||
6143 | SelIdents.size()); | |||
6144 | if (PreferredType.isNull()) | |||
6145 | CodeCompleteOrdinaryName(S, PCC_Expression); | |||
6146 | else | |||
6147 | CodeCompleteExpression(S, PreferredType); | |||
6148 | return; | |||
6149 | } | |||
6150 | ||||
6151 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6152 | Results.getCompletionContext(), | |||
6153 | Results.data(),Results.size()); | |||
6154 | } | |||
6155 | ||||
6156 | void Sema::CodeCompleteObjCForCollection(Scope *S, | |||
6157 | DeclGroupPtrTy IterationVar) { | |||
6158 | CodeCompleteExpressionData Data; | |||
6159 | Data.ObjCCollection = true; | |||
6160 | ||||
6161 | if (IterationVar.getAsOpaquePtr()) { | |||
6162 | DeclGroupRef DG = IterationVar.get(); | |||
6163 | for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) { | |||
6164 | if (*I) | |||
6165 | Data.IgnoreDecls.push_back(*I); | |||
6166 | } | |||
6167 | } | |||
6168 | ||||
6169 | CodeCompleteExpression(S, Data); | |||
6170 | } | |||
6171 | ||||
6172 | void Sema::CodeCompleteObjCSelector(Scope *S, | |||
6173 | ArrayRef<IdentifierInfo *> SelIdents) { | |||
6174 | // If we have an external source, load the entire class method | |||
6175 | // pool from the AST file. | |||
6176 | if (ExternalSource) { | |||
6177 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); | |||
6178 | I != N; ++I) { | |||
6179 | Selector Sel = ExternalSource->GetExternalSelector(I); | |||
6180 | if (Sel.isNull() || MethodPool.count(Sel)) | |||
6181 | continue; | |||
6182 | ||||
6183 | ReadMethodPool(Sel); | |||
6184 | } | |||
6185 | } | |||
6186 | ||||
6187 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6188 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6189 | CodeCompletionContext::CCC_SelectorName); | |||
6190 | Results.EnterNewScope(); | |||
6191 | for (GlobalMethodPool::iterator M = MethodPool.begin(), | |||
6192 | MEnd = MethodPool.end(); | |||
6193 | M != MEnd; ++M) { | |||
6194 | ||||
6195 | Selector Sel = M->first; | |||
6196 | if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents)) | |||
6197 | continue; | |||
6198 | ||||
6199 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
6200 | Results.getCodeCompletionTUInfo()); | |||
6201 | if (Sel.isUnarySelector()) { | |||
6202 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | |||
6203 | Sel.getNameForSlot(0))); | |||
6204 | Results.AddResult(Builder.TakeString()); | |||
6205 | continue; | |||
6206 | } | |||
6207 | ||||
6208 | std::string Accumulator; | |||
6209 | for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) { | |||
6210 | if (I == SelIdents.size()) { | |||
6211 | if (!Accumulator.empty()) { | |||
6212 | Builder.AddInformativeChunk(Builder.getAllocator().CopyString( | |||
6213 | Accumulator)); | |||
6214 | Accumulator.clear(); | |||
6215 | } | |||
6216 | } | |||
6217 | ||||
6218 | Accumulator += Sel.getNameForSlot(I); | |||
6219 | Accumulator += ':'; | |||
6220 | } | |||
6221 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator)); | |||
6222 | Results.AddResult(Builder.TakeString()); | |||
6223 | } | |||
6224 | Results.ExitScope(); | |||
6225 | ||||
6226 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6227 | CodeCompletionContext::CCC_SelectorName, | |||
6228 | Results.data(), Results.size()); | |||
6229 | } | |||
6230 | ||||
6231 | /// \brief Add all of the protocol declarations that we find in the given | |||
6232 | /// (translation unit) context. | |||
6233 | static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext, | |||
6234 | bool OnlyForwardDeclarations, | |||
6235 | ResultBuilder &Results) { | |||
6236 | typedef CodeCompletionResult Result; | |||
6237 | ||||
6238 | for (const auto *D : Ctx->decls()) { | |||
6239 | // Record any protocols we find. | |||
6240 | if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D)) | |||
6241 | if (!OnlyForwardDeclarations || !Proto->hasDefinition()) | |||
6242 | Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr), | |||
6243 | CurContext, nullptr, false); | |||
6244 | } | |||
6245 | } | |||
6246 | ||||
6247 | void Sema::CodeCompleteObjCProtocolReferences( | |||
6248 | ArrayRef<IdentifierLocPair> Protocols) { | |||
6249 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6250 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6251 | CodeCompletionContext::CCC_ObjCProtocolName); | |||
6252 | ||||
6253 | if (CodeCompleter->includeGlobals()) { | |||
6254 | Results.EnterNewScope(); | |||
6255 | ||||
6256 | // Tell the result set to ignore all of the protocols we have | |||
6257 | // already seen. | |||
6258 | // FIXME: This doesn't work when caching code-completion results. | |||
6259 | for (const IdentifierLocPair &Pair : Protocols) | |||
6260 | if (ObjCProtocolDecl *Protocol = LookupProtocol(Pair.first, | |||
6261 | Pair.second)) | |||
6262 | Results.Ignore(Protocol); | |||
6263 | ||||
6264 | // Add all protocols. | |||
6265 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false, | |||
6266 | Results); | |||
6267 | ||||
6268 | Results.ExitScope(); | |||
6269 | } | |||
6270 | ||||
6271 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6272 | CodeCompletionContext::CCC_ObjCProtocolName, | |||
6273 | Results.data(),Results.size()); | |||
6274 | } | |||
6275 | ||||
6276 | void Sema::CodeCompleteObjCProtocolDecl(Scope *) { | |||
6277 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6278 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6279 | CodeCompletionContext::CCC_ObjCProtocolName); | |||
6280 | ||||
6281 | if (CodeCompleter->includeGlobals()) { | |||
6282 | Results.EnterNewScope(); | |||
6283 | ||||
6284 | // Add all protocols. | |||
6285 | AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true, | |||
6286 | Results); | |||
6287 | ||||
6288 | Results.ExitScope(); | |||
6289 | } | |||
6290 | ||||
6291 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6292 | CodeCompletionContext::CCC_ObjCProtocolName, | |||
6293 | Results.data(),Results.size()); | |||
6294 | } | |||
6295 | ||||
6296 | /// \brief Add all of the Objective-C interface declarations that we find in | |||
6297 | /// the given (translation unit) context. | |||
6298 | static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext, | |||
6299 | bool OnlyForwardDeclarations, | |||
6300 | bool OnlyUnimplemented, | |||
6301 | ResultBuilder &Results) { | |||
6302 | typedef CodeCompletionResult Result; | |||
6303 | ||||
6304 | for (const auto *D : Ctx->decls()) { | |||
6305 | // Record any interfaces we find. | |||
6306 | if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D)) | |||
6307 | if ((!OnlyForwardDeclarations || !Class->hasDefinition()) && | |||
6308 | (!OnlyUnimplemented || !Class->getImplementation())) | |||
6309 | Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr), | |||
6310 | CurContext, nullptr, false); | |||
6311 | } | |||
6312 | } | |||
6313 | ||||
6314 | void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) { | |||
6315 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6316 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6317 | CodeCompletionContext::CCC_Other); | |||
6318 | Results.EnterNewScope(); | |||
6319 | ||||
6320 | if (CodeCompleter->includeGlobals()) { | |||
6321 | // Add all classes. | |||
6322 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, | |||
6323 | false, Results); | |||
6324 | } | |||
6325 | ||||
6326 | Results.ExitScope(); | |||
6327 | ||||
6328 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6329 | CodeCompletionContext::CCC_ObjCInterfaceName, | |||
6330 | Results.data(),Results.size()); | |||
6331 | } | |||
6332 | ||||
6333 | void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName, | |||
6334 | SourceLocation ClassNameLoc) { | |||
6335 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6336 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6337 | CodeCompletionContext::CCC_ObjCInterfaceName); | |||
6338 | Results.EnterNewScope(); | |||
6339 | ||||
6340 | // Make sure that we ignore the class we're currently defining. | |||
6341 | NamedDecl *CurClass | |||
6342 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); | |||
6343 | if (CurClass && isa<ObjCInterfaceDecl>(CurClass)) | |||
6344 | Results.Ignore(CurClass); | |||
6345 | ||||
6346 | if (CodeCompleter->includeGlobals()) { | |||
6347 | // Add all classes. | |||
6348 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, | |||
6349 | false, Results); | |||
6350 | } | |||
6351 | ||||
6352 | Results.ExitScope(); | |||
6353 | ||||
6354 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6355 | CodeCompletionContext::CCC_ObjCInterfaceName, | |||
6356 | Results.data(),Results.size()); | |||
6357 | } | |||
6358 | ||||
6359 | void Sema::CodeCompleteObjCImplementationDecl(Scope *S) { | |||
6360 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6361 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6362 | CodeCompletionContext::CCC_Other); | |||
6363 | Results.EnterNewScope(); | |||
6364 | ||||
6365 | if (CodeCompleter->includeGlobals()) { | |||
6366 | // Add all unimplemented classes. | |||
6367 | AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false, | |||
6368 | true, Results); | |||
6369 | } | |||
6370 | ||||
6371 | Results.ExitScope(); | |||
6372 | ||||
6373 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6374 | CodeCompletionContext::CCC_ObjCInterfaceName, | |||
6375 | Results.data(),Results.size()); | |||
6376 | } | |||
6377 | ||||
6378 | void Sema::CodeCompleteObjCInterfaceCategory(Scope *S, | |||
6379 | IdentifierInfo *ClassName, | |||
6380 | SourceLocation ClassNameLoc) { | |||
6381 | typedef CodeCompletionResult Result; | |||
6382 | ||||
6383 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6384 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6385 | CodeCompletionContext::CCC_ObjCCategoryName); | |||
6386 | ||||
6387 | // Ignore any categories we find that have already been implemented by this | |||
6388 | // interface. | |||
6389 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; | |||
6390 | NamedDecl *CurClass | |||
6391 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); | |||
6392 | if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){ | |||
6393 | for (const auto *Cat : Class->visible_categories()) | |||
6394 | CategoryNames.insert(Cat->getIdentifier()); | |||
6395 | } | |||
6396 | ||||
6397 | // Add all of the categories we know about. | |||
6398 | Results.EnterNewScope(); | |||
6399 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); | |||
6400 | for (const auto *D : TU->decls()) | |||
6401 | if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D)) | |||
6402 | if (CategoryNames.insert(Category->getIdentifier()).second) | |||
6403 | Results.AddResult(Result(Category, Results.getBasePriority(Category), | |||
6404 | nullptr), | |||
6405 | CurContext, nullptr, false); | |||
6406 | Results.ExitScope(); | |||
6407 | ||||
6408 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6409 | CodeCompletionContext::CCC_ObjCCategoryName, | |||
6410 | Results.data(),Results.size()); | |||
6411 | } | |||
6412 | ||||
6413 | void Sema::CodeCompleteObjCImplementationCategory(Scope *S, | |||
6414 | IdentifierInfo *ClassName, | |||
6415 | SourceLocation ClassNameLoc) { | |||
6416 | typedef CodeCompletionResult Result; | |||
6417 | ||||
6418 | // Find the corresponding interface. If we couldn't find the interface, the | |||
6419 | // program itself is ill-formed. However, we'll try to be helpful still by | |||
6420 | // providing the list of all of the categories we know about. | |||
6421 | NamedDecl *CurClass | |||
6422 | = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName); | |||
6423 | ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass); | |||
6424 | if (!Class) | |||
6425 | return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc); | |||
6426 | ||||
6427 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6428 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6429 | CodeCompletionContext::CCC_ObjCCategoryName); | |||
6430 | ||||
6431 | // Add all of the categories that have have corresponding interface | |||
6432 | // declarations in this class and any of its superclasses, except for | |||
6433 | // already-implemented categories in the class itself. | |||
6434 | llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames; | |||
6435 | Results.EnterNewScope(); | |||
6436 | bool IgnoreImplemented = true; | |||
6437 | while (Class) { | |||
6438 | for (const auto *Cat : Class->visible_categories()) { | |||
6439 | if ((!IgnoreImplemented || !Cat->getImplementation()) && | |||
6440 | CategoryNames.insert(Cat->getIdentifier()).second) | |||
6441 | Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr), | |||
6442 | CurContext, nullptr, false); | |||
6443 | } | |||
6444 | ||||
6445 | Class = Class->getSuperClass(); | |||
6446 | IgnoreImplemented = false; | |||
6447 | } | |||
6448 | Results.ExitScope(); | |||
6449 | ||||
6450 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6451 | CodeCompletionContext::CCC_ObjCCategoryName, | |||
6452 | Results.data(),Results.size()); | |||
6453 | } | |||
6454 | ||||
6455 | void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) { | |||
6456 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_Other); | |||
6457 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6458 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6459 | CCContext); | |||
6460 | ||||
6461 | // Figure out where this @synthesize lives. | |||
6462 | ObjCContainerDecl *Container | |||
6463 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); | |||
6464 | if (!Container || | |||
6465 | (!isa<ObjCImplementationDecl>(Container) && | |||
6466 | !isa<ObjCCategoryImplDecl>(Container))) | |||
6467 | return; | |||
6468 | ||||
6469 | // Ignore any properties that have already been implemented. | |||
6470 | Container = getContainerDef(Container); | |||
6471 | for (const auto *D : Container->decls()) | |||
6472 | if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D)) | |||
6473 | Results.Ignore(PropertyImpl->getPropertyDecl()); | |||
6474 | ||||
6475 | // Add any properties that we find. | |||
6476 | AddedPropertiesSet AddedProperties; | |||
6477 | Results.EnterNewScope(); | |||
6478 | if (ObjCImplementationDecl *ClassImpl | |||
6479 | = dyn_cast<ObjCImplementationDecl>(Container)) | |||
6480 | AddObjCProperties(CCContext, ClassImpl->getClassInterface(), false, | |||
6481 | /*AllowNullaryMethods=*/false, CurContext, | |||
6482 | AddedProperties, Results); | |||
6483 | else | |||
6484 | AddObjCProperties(CCContext, | |||
6485 | cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(), | |||
6486 | false, /*AllowNullaryMethods=*/false, CurContext, | |||
6487 | AddedProperties, Results); | |||
6488 | Results.ExitScope(); | |||
6489 | ||||
6490 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6491 | CodeCompletionContext::CCC_Other, | |||
6492 | Results.data(),Results.size()); | |||
6493 | } | |||
6494 | ||||
6495 | void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S, | |||
6496 | IdentifierInfo *PropertyName) { | |||
6497 | typedef CodeCompletionResult Result; | |||
6498 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
6499 | CodeCompleter->getCodeCompletionTUInfo(), | |||
6500 | CodeCompletionContext::CCC_Other); | |||
6501 | ||||
6502 | // Figure out where this @synthesize lives. | |||
6503 | ObjCContainerDecl *Container | |||
6504 | = dyn_cast_or_null<ObjCContainerDecl>(CurContext); | |||
6505 | if (!Container || | |||
| ||||
6506 | (!isa<ObjCImplementationDecl>(Container) && | |||
6507 | !isa<ObjCCategoryImplDecl>(Container))) | |||
6508 | return; | |||
6509 | ||||
6510 | // Figure out which interface we're looking into. | |||
6511 | ObjCInterfaceDecl *Class = nullptr; | |||
6512 | if (ObjCImplementationDecl *ClassImpl | |||
6513 | = dyn_cast<ObjCImplementationDecl>(Container)) | |||
6514 | Class = ClassImpl->getClassInterface(); | |||
6515 | else | |||
6516 | Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl() | |||
6517 | ->getClassInterface(); | |||
6518 | ||||
6519 | // Determine the type of the property we're synthesizing. | |||
6520 | QualType PropertyType = Context.getObjCIdType(); | |||
6521 | if (Class) { | |||
6522 | if (ObjCPropertyDecl *Property = Class->FindPropertyDeclaration( | |||
6523 | PropertyName, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { | |||
6524 | PropertyType | |||
6525 | = Property->getType().getNonReferenceType().getUnqualifiedType(); | |||
6526 | ||||
6527 | // Give preference to ivars | |||
6528 | Results.setPreferredType(PropertyType); | |||
6529 | } | |||
6530 | } | |||
6531 | ||||
6532 | // Add all of the instance variables in this class and its superclasses. | |||
6533 | Results.EnterNewScope(); | |||
6534 | bool SawSimilarlyNamedIvar = false; | |||
6535 | std::string NameWithPrefix; | |||
6536 | NameWithPrefix += '_'; | |||
6537 | NameWithPrefix += PropertyName->getName(); | |||
6538 | std::string NameWithSuffix = PropertyName->getName().str(); | |||
6539 | NameWithSuffix += '_'; | |||
6540 | for(; Class; Class = Class->getSuperClass()) { | |||
6541 | for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; | |||
6542 | Ivar = Ivar->getNextIvar()) { | |||
6543 | Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr), | |||
6544 | CurContext, nullptr, false); | |||
6545 | ||||
6546 | // Determine whether we've seen an ivar with a name similar to the | |||
6547 | // property. | |||
6548 | if ((PropertyName == Ivar->getIdentifier() || | |||
6549 | NameWithPrefix == Ivar->getName() || | |||
6550 | NameWithSuffix == Ivar->getName())) { | |||
6551 | SawSimilarlyNamedIvar = true; | |||
6552 | ||||
6553 | // Reduce the priority of this result by one, to give it a slight | |||
6554 | // advantage over other results whose names don't match so closely. | |||
6555 | if (Results.size() && | |||
6556 | Results.data()[Results.size() - 1].Kind | |||
| ||||
6557 | == CodeCompletionResult::RK_Declaration && | |||
6558 | Results.data()[Results.size() - 1].Declaration == Ivar) | |||
6559 | Results.data()[Results.size() - 1].Priority--; | |||
6560 | } | |||
6561 | } | |||
6562 | } | |||
6563 | ||||
6564 | if (!SawSimilarlyNamedIvar) { | |||
6565 | // Create ivar result _propName, that the user can use to synthesize | |||
6566 | // an ivar of the appropriate type. | |||
6567 | unsigned Priority = CCP_MemberDeclaration + 1; | |||
6568 | typedef CodeCompletionResult Result; | |||
6569 | CodeCompletionAllocator &Allocator = Results.getAllocator(); | |||
6570 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(), | |||
6571 | Priority,CXAvailability_Available); | |||
6572 | ||||
6573 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); | |||
6574 | Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context, | |||
6575 | Policy, Allocator)); | |||
6576 | Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix)); | |||
6577 | Results.AddResult(Result(Builder.TakeString(), Priority, | |||
6578 | CXCursor_ObjCIvarDecl)); | |||
6579 | } | |||
6580 | ||||
6581 | Results.ExitScope(); | |||
6582 | ||||
6583 | HandleCodeCompleteResults(this, CodeCompleter, | |||
6584 | CodeCompletionContext::CCC_Other, | |||
6585 | Results.data(),Results.size()); | |||
6586 | } | |||
6587 | ||||
6588 | // Mapping from selectors to the methods that implement that selector, along | |||
6589 | // with the "in original class" flag. | |||
6590 | typedef llvm::DenseMap< | |||
6591 | Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap; | |||
6592 | ||||
6593 | /// \brief Find all of the methods that reside in the given container | |||
6594 | /// (and its superclasses, protocols, etc.) that meet the given | |||
6595 | /// criteria. Insert those methods into the map of known methods, | |||
6596 | /// indexed by selector so they can be easily found. | |||
6597 | static void FindImplementableMethods(ASTContext &Context, | |||
6598 | ObjCContainerDecl *Container, | |||
6599 | bool WantInstanceMethods, | |||
6600 | QualType ReturnType, | |||
6601 | KnownMethodsMap &KnownMethods, | |||
6602 | bool InOriginalClass = true) { | |||
6603 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) { | |||
6604 | // Make sure we have a definition; that's what we'll walk. | |||
6605 | if (!IFace->hasDefinition()) | |||
6606 | return; | |||
6607 | ||||
6608 | IFace = IFace->getDefinition(); | |||
6609 | Container = IFace; | |||
6610 | ||||
6611 | const ObjCList<ObjCProtocolDecl> &Protocols | |||
6612 | = IFace->getReferencedProtocols(); | |||
6613 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | |||
6614 | E = Protocols.end(); | |||
6615 | I != E; ++I) | |||
6616 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, | |||
6617 | KnownMethods, InOriginalClass); | |||
6618 | ||||
6619 | // Add methods from any class extensions and categories. | |||
6620 | for (auto *Cat : IFace->visible_categories()) { | |||
6621 | FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType, | |||
6622 | KnownMethods, false); | |||
6623 | } | |||
6624 | ||||
6625 | // Visit the superclass. | |||
6626 | if (IFace->getSuperClass()) | |||
6627 | FindImplementableMethods(Context, IFace->getSuperClass(), | |||
6628 | WantInstanceMethods, ReturnType, | |||
6629 | KnownMethods, false); | |||
6630 | } | |||
6631 | ||||
6632 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { | |||
6633 | // Recurse into protocols. | |||
6634 | const ObjCList<ObjCProtocolDecl> &Protocols | |||
6635 | = Category->getReferencedProtocols(); | |||
6636 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | |||
6637 | E = Protocols.end(); | |||
6638 | I != E; ++I) | |||
6639 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, | |||
6640 | KnownMethods, InOriginalClass); | |||
6641 | ||||
6642 | // If this category is the original class, jump to the interface. | |||
6643 | if (InOriginalClass && Category->getClassInterface()) | |||
6644 | FindImplementableMethods(Context, Category->getClassInterface(), | |||
6645 | WantInstanceMethods, ReturnType, KnownMethods, | |||
6646 | false); | |||
6647 | } | |||
6648 | ||||
6649 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { | |||
6650 | // Make sure we have a definition; that's what we'll walk. | |||
6651 | if (!Protocol->hasDefinition()) | |||
6652 | return; | |||
6653 | Protocol = Protocol->getDefinition(); | |||
6654 | Container = Protocol; | |||
6655 | ||||
6656 | // Recurse into protocols. | |||
6657 | const ObjCList<ObjCProtocolDecl> &Protocols | |||
6658 | = Protocol->getReferencedProtocols(); | |||
6659 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), | |||
6660 | E = Protocols.end(); | |||
6661 | I != E; ++I) | |||
6662 | FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType, | |||
6663 | KnownMethods, false); | |||
6664 | } | |||
6665 | ||||
6666 | // Add methods in this container. This operation occurs last because | |||
6667 | // we want the methods from this container to override any methods | |||
6668 | // we've previously seen with the same selector. | |||
6669 | for (auto *M : Container->methods()) { | |||
6670 | if (M->isInstanceMethod() == WantInstanceMethods) { | |||
6671 | if (!ReturnType.isNull() && | |||
6672 | !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType())) | |||
6673 | continue; | |||
6674 | ||||
6675 | KnownMethods[M->getSelector()] = | |||
6676 | KnownMethodsMap::mapped_type(M, InOriginalClass); | |||
6677 | } | |||
6678 | } | |||
6679 | } | |||
6680 | ||||
6681 | /// \brief Add the parenthesized return or parameter type chunk to a code | |||
6682 | /// completion string. | |||
6683 | static void AddObjCPassingTypeChunk(QualType Type, | |||
6684 | unsigned ObjCDeclQuals, | |||
6685 | ASTContext &Context, | |||
6686 | const PrintingPolicy &Policy, | |||
6687 | CodeCompletionBuilder &Builder) { | |||
6688 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6689 | std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals, Type); | |||
6690 | if (!Quals.empty()) | |||
6691 | Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals)); | |||
6692 | Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy, | |||
6693 | Builder.getAllocator())); | |||
6694 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6695 | } | |||
6696 | ||||
6697 | /// \brief Determine whether the given class is or inherits from a class by | |||
6698 | /// the given name. | |||
6699 | static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class, | |||
6700 | StringRef Name) { | |||
6701 | if (!Class) | |||
6702 | return false; | |||
6703 | ||||
6704 | if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name) | |||
6705 | return true; | |||
6706 | ||||
6707 | return InheritsFromClassNamed(Class->getSuperClass(), Name); | |||
6708 | } | |||
6709 | ||||
6710 | /// \brief Add code completions for Objective-C Key-Value Coding (KVC) and | |||
6711 | /// Key-Value Observing (KVO). | |||
6712 | static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property, | |||
6713 | bool IsInstanceMethod, | |||
6714 | QualType ReturnType, | |||
6715 | ASTContext &Context, | |||
6716 | VisitedSelectorSet &KnownSelectors, | |||
6717 | ResultBuilder &Results) { | |||
6718 | IdentifierInfo *PropName = Property->getIdentifier(); | |||
6719 | if (!PropName || PropName->getLength() == 0) | |||
6720 | return; | |||
6721 | ||||
6722 | PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema()); | |||
6723 | ||||
6724 | // Builder that will create each code completion. | |||
6725 | typedef CodeCompletionResult Result; | |||
6726 | CodeCompletionAllocator &Allocator = Results.getAllocator(); | |||
6727 | CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo()); | |||
6728 | ||||
6729 | // The selector table. | |||
6730 | SelectorTable &Selectors = Context.Selectors; | |||
6731 | ||||
6732 | // The property name, copied into the code completion allocation region | |||
6733 | // on demand. | |||
6734 | struct KeyHolder { | |||
6735 | CodeCompletionAllocator &Allocator; | |||
6736 | StringRef Key; | |||
6737 | const char *CopiedKey; | |||
6738 | ||||
6739 | KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key) | |||
6740 | : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {} | |||
6741 | ||||
6742 | operator const char *() { | |||
6743 | if (CopiedKey) | |||
6744 | return CopiedKey; | |||
6745 | ||||
6746 | return CopiedKey = Allocator.CopyString(Key); | |||
6747 | } | |||
6748 | } Key(Allocator, PropName->getName()); | |||
6749 | ||||
6750 | // The uppercased name of the property name. | |||
6751 | std::string UpperKey = PropName->getName(); | |||
6752 | if (!UpperKey.empty()) | |||
6753 | UpperKey[0] = toUppercase(UpperKey[0]); | |||
6754 | ||||
6755 | bool ReturnTypeMatchesProperty = ReturnType.isNull() || | |||
6756 | Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(), | |||
6757 | Property->getType()); | |||
6758 | bool ReturnTypeMatchesVoid | |||
6759 | = ReturnType.isNull() || ReturnType->isVoidType(); | |||
6760 | ||||
6761 | // Add the normal accessor -(type)key. | |||
6762 | if (IsInstanceMethod && | |||
6763 | KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second && | |||
6764 | ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) { | |||
6765 | if (ReturnType.isNull()) | |||
6766 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, | |||
6767 | Context, Policy, Builder); | |||
6768 | ||||
6769 | Builder.AddTypedTextChunk(Key); | |||
6770 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, | |||
6771 | CXCursor_ObjCInstanceMethodDecl)); | |||
6772 | } | |||
6773 | ||||
6774 | // If we have an integral or boolean property (or the user has provided | |||
6775 | // an integral or boolean return type), add the accessor -(type)isKey. | |||
6776 | if (IsInstanceMethod && | |||
6777 | ((!ReturnType.isNull() && | |||
6778 | (ReturnType->isIntegerType() || ReturnType->isBooleanType())) || | |||
6779 | (ReturnType.isNull() && | |||
6780 | (Property->getType()->isIntegerType() || | |||
6781 | Property->getType()->isBooleanType())))) { | |||
6782 | std::string SelectorName = (Twine("is") + UpperKey).str(); | |||
6783 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
6784 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) | |||
6785 | .second) { | |||
6786 | if (ReturnType.isNull()) { | |||
6787 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6788 | Builder.AddTextChunk("BOOL"); | |||
6789 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6790 | } | |||
6791 | ||||
6792 | Builder.AddTypedTextChunk( | |||
6793 | Allocator.CopyString(SelectorId->getName())); | |||
6794 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, | |||
6795 | CXCursor_ObjCInstanceMethodDecl)); | |||
6796 | } | |||
6797 | } | |||
6798 | ||||
6799 | // Add the normal mutator. | |||
6800 | if (IsInstanceMethod && ReturnTypeMatchesVoid && | |||
6801 | !Property->getSetterMethodDecl()) { | |||
6802 | std::string SelectorName = (Twine("set") + UpperKey).str(); | |||
6803 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
6804 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
6805 | if (ReturnType.isNull()) { | |||
6806 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6807 | Builder.AddTextChunk("void"); | |||
6808 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6809 | } | |||
6810 | ||||
6811 | Builder.AddTypedTextChunk( | |||
6812 | Allocator.CopyString(SelectorId->getName())); | |||
6813 | Builder.AddTypedTextChunk(":"); | |||
6814 | AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0, | |||
6815 | Context, Policy, Builder); | |||
6816 | Builder.AddTextChunk(Key); | |||
6817 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, | |||
6818 | CXCursor_ObjCInstanceMethodDecl)); | |||
6819 | } | |||
6820 | } | |||
6821 | ||||
6822 | // Indexed and unordered accessors | |||
6823 | unsigned IndexedGetterPriority = CCP_CodePattern; | |||
6824 | unsigned IndexedSetterPriority = CCP_CodePattern; | |||
6825 | unsigned UnorderedGetterPriority = CCP_CodePattern; | |||
6826 | unsigned UnorderedSetterPriority = CCP_CodePattern; | |||
6827 | if (const ObjCObjectPointerType *ObjCPointer | |||
6828 | = Property->getType()->getAs<ObjCObjectPointerType>()) { | |||
6829 | if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) { | |||
6830 | // If this interface type is not provably derived from a known | |||
6831 | // collection, penalize the corresponding completions. | |||
6832 | if (!InheritsFromClassNamed(IFace, "NSMutableArray")) { | |||
6833 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; | |||
6834 | if (!InheritsFromClassNamed(IFace, "NSArray")) | |||
6835 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; | |||
6836 | } | |||
6837 | ||||
6838 | if (!InheritsFromClassNamed(IFace, "NSMutableSet")) { | |||
6839 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; | |||
6840 | if (!InheritsFromClassNamed(IFace, "NSSet")) | |||
6841 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; | |||
6842 | } | |||
6843 | } | |||
6844 | } else { | |||
6845 | IndexedGetterPriority += CCD_ProbablyNotObjCCollection; | |||
6846 | IndexedSetterPriority += CCD_ProbablyNotObjCCollection; | |||
6847 | UnorderedGetterPriority += CCD_ProbablyNotObjCCollection; | |||
6848 | UnorderedSetterPriority += CCD_ProbablyNotObjCCollection; | |||
6849 | } | |||
6850 | ||||
6851 | // Add -(NSUInteger)countOf<key> | |||
6852 | if (IsInstanceMethod && | |||
6853 | (ReturnType.isNull() || ReturnType->isIntegerType())) { | |||
6854 | std::string SelectorName = (Twine("countOf") + UpperKey).str(); | |||
6855 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
6856 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) | |||
6857 | .second) { | |||
6858 | if (ReturnType.isNull()) { | |||
6859 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6860 | Builder.AddTextChunk("NSUInteger"); | |||
6861 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6862 | } | |||
6863 | ||||
6864 | Builder.AddTypedTextChunk( | |||
6865 | Allocator.CopyString(SelectorId->getName())); | |||
6866 | Results.AddResult(Result(Builder.TakeString(), | |||
6867 | std::min(IndexedGetterPriority, | |||
6868 | UnorderedGetterPriority), | |||
6869 | CXCursor_ObjCInstanceMethodDecl)); | |||
6870 | } | |||
6871 | } | |||
6872 | ||||
6873 | // Indexed getters | |||
6874 | // Add -(id)objectInKeyAtIndex:(NSUInteger)index | |||
6875 | if (IsInstanceMethod && | |||
6876 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { | |||
6877 | std::string SelectorName | |||
6878 | = (Twine("objectIn") + UpperKey + "AtIndex").str(); | |||
6879 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
6880 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
6881 | if (ReturnType.isNull()) { | |||
6882 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6883 | Builder.AddTextChunk("id"); | |||
6884 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6885 | } | |||
6886 | ||||
6887 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
6888 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6889 | Builder.AddTextChunk("NSUInteger"); | |||
6890 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6891 | Builder.AddTextChunk("index"); | |||
6892 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, | |||
6893 | CXCursor_ObjCInstanceMethodDecl)); | |||
6894 | } | |||
6895 | } | |||
6896 | ||||
6897 | // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes | |||
6898 | if (IsInstanceMethod && | |||
6899 | (ReturnType.isNull() || | |||
6900 | (ReturnType->isObjCObjectPointerType() && | |||
6901 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && | |||
6902 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() | |||
6903 | ->getName() == "NSArray"))) { | |||
6904 | std::string SelectorName | |||
6905 | = (Twine(Property->getName()) + "AtIndexes").str(); | |||
6906 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
6907 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
6908 | if (ReturnType.isNull()) { | |||
6909 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6910 | Builder.AddTextChunk("NSArray *"); | |||
6911 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6912 | } | |||
6913 | ||||
6914 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
6915 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6916 | Builder.AddTextChunk("NSIndexSet *"); | |||
6917 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6918 | Builder.AddTextChunk("indexes"); | |||
6919 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, | |||
6920 | CXCursor_ObjCInstanceMethodDecl)); | |||
6921 | } | |||
6922 | } | |||
6923 | ||||
6924 | // Add -(void)getKey:(type **)buffer range:(NSRange)inRange | |||
6925 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
6926 | std::string SelectorName = (Twine("get") + UpperKey).str(); | |||
6927 | IdentifierInfo *SelectorIds[2] = { | |||
6928 | &Context.Idents.get(SelectorName), | |||
6929 | &Context.Idents.get("range") | |||
6930 | }; | |||
6931 | ||||
6932 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { | |||
6933 | if (ReturnType.isNull()) { | |||
6934 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6935 | Builder.AddTextChunk("void"); | |||
6936 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6937 | } | |||
6938 | ||||
6939 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
6940 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6941 | Builder.AddPlaceholderChunk("object-type"); | |||
6942 | Builder.AddTextChunk(" **"); | |||
6943 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6944 | Builder.AddTextChunk("buffer"); | |||
6945 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
6946 | Builder.AddTypedTextChunk("range:"); | |||
6947 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6948 | Builder.AddTextChunk("NSRange"); | |||
6949 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6950 | Builder.AddTextChunk("inRange"); | |||
6951 | Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority, | |||
6952 | CXCursor_ObjCInstanceMethodDecl)); | |||
6953 | } | |||
6954 | } | |||
6955 | ||||
6956 | // Mutable indexed accessors | |||
6957 | ||||
6958 | // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index | |||
6959 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
6960 | std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str(); | |||
6961 | IdentifierInfo *SelectorIds[2] = { | |||
6962 | &Context.Idents.get("insertObject"), | |||
6963 | &Context.Idents.get(SelectorName) | |||
6964 | }; | |||
6965 | ||||
6966 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { | |||
6967 | if (ReturnType.isNull()) { | |||
6968 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6969 | Builder.AddTextChunk("void"); | |||
6970 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6971 | } | |||
6972 | ||||
6973 | Builder.AddTypedTextChunk("insertObject:"); | |||
6974 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6975 | Builder.AddPlaceholderChunk("object-type"); | |||
6976 | Builder.AddTextChunk(" *"); | |||
6977 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6978 | Builder.AddTextChunk("object"); | |||
6979 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
6980 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
6981 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
6982 | Builder.AddPlaceholderChunk("NSUInteger"); | |||
6983 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
6984 | Builder.AddTextChunk("index"); | |||
6985 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | |||
6986 | CXCursor_ObjCInstanceMethodDecl)); | |||
6987 | } | |||
6988 | } | |||
6989 | ||||
6990 | // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes | |||
6991 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
6992 | std::string SelectorName = (Twine("insert") + UpperKey).str(); | |||
6993 | IdentifierInfo *SelectorIds[2] = { | |||
6994 | &Context.Idents.get(SelectorName), | |||
6995 | &Context.Idents.get("atIndexes") | |||
6996 | }; | |||
6997 | ||||
6998 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { | |||
6999 | if (ReturnType.isNull()) { | |||
7000 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7001 | Builder.AddTextChunk("void"); | |||
7002 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7003 | } | |||
7004 | ||||
7005 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
7006 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7007 | Builder.AddTextChunk("NSArray *"); | |||
7008 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7009 | Builder.AddTextChunk("array"); | |||
7010 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7011 | Builder.AddTypedTextChunk("atIndexes:"); | |||
7012 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7013 | Builder.AddPlaceholderChunk("NSIndexSet *"); | |||
7014 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7015 | Builder.AddTextChunk("indexes"); | |||
7016 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | |||
7017 | CXCursor_ObjCInstanceMethodDecl)); | |||
7018 | } | |||
7019 | } | |||
7020 | ||||
7021 | // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index | |||
7022 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
7023 | std::string SelectorName | |||
7024 | = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str(); | |||
7025 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7026 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
7027 | if (ReturnType.isNull()) { | |||
7028 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7029 | Builder.AddTextChunk("void"); | |||
7030 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7031 | } | |||
7032 | ||||
7033 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
7034 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7035 | Builder.AddTextChunk("NSUInteger"); | |||
7036 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7037 | Builder.AddTextChunk("index"); | |||
7038 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | |||
7039 | CXCursor_ObjCInstanceMethodDecl)); | |||
7040 | } | |||
7041 | } | |||
7042 | ||||
7043 | // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes | |||
7044 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
7045 | std::string SelectorName | |||
7046 | = (Twine("remove") + UpperKey + "AtIndexes").str(); | |||
7047 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7048 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
7049 | if (ReturnType.isNull()) { | |||
7050 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7051 | Builder.AddTextChunk("void"); | |||
7052 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7053 | } | |||
7054 | ||||
7055 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
7056 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7057 | Builder.AddTextChunk("NSIndexSet *"); | |||
7058 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7059 | Builder.AddTextChunk("indexes"); | |||
7060 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | |||
7061 | CXCursor_ObjCInstanceMethodDecl)); | |||
7062 | } | |||
7063 | } | |||
7064 | ||||
7065 | // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object | |||
7066 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
7067 | std::string SelectorName | |||
7068 | = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str(); | |||
7069 | IdentifierInfo *SelectorIds[2] = { | |||
7070 | &Context.Idents.get(SelectorName), | |||
7071 | &Context.Idents.get("withObject") | |||
7072 | }; | |||
7073 | ||||
7074 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { | |||
7075 | if (ReturnType.isNull()) { | |||
7076 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7077 | Builder.AddTextChunk("void"); | |||
7078 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7079 | } | |||
7080 | ||||
7081 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
7082 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7083 | Builder.AddPlaceholderChunk("NSUInteger"); | |||
7084 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7085 | Builder.AddTextChunk("index"); | |||
7086 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7087 | Builder.AddTypedTextChunk("withObject:"); | |||
7088 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7089 | Builder.AddTextChunk("id"); | |||
7090 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7091 | Builder.AddTextChunk("object"); | |||
7092 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | |||
7093 | CXCursor_ObjCInstanceMethodDecl)); | |||
7094 | } | |||
7095 | } | |||
7096 | ||||
7097 | // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array | |||
7098 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
7099 | std::string SelectorName1 | |||
7100 | = (Twine("replace") + UpperKey + "AtIndexes").str(); | |||
7101 | std::string SelectorName2 = (Twine("with") + UpperKey).str(); | |||
7102 | IdentifierInfo *SelectorIds[2] = { | |||
7103 | &Context.Idents.get(SelectorName1), | |||
7104 | &Context.Idents.get(SelectorName2) | |||
7105 | }; | |||
7106 | ||||
7107 | if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) { | |||
7108 | if (ReturnType.isNull()) { | |||
7109 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7110 | Builder.AddTextChunk("void"); | |||
7111 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7112 | } | |||
7113 | ||||
7114 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":")); | |||
7115 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7116 | Builder.AddPlaceholderChunk("NSIndexSet *"); | |||
7117 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7118 | Builder.AddTextChunk("indexes"); | |||
7119 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7120 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":")); | |||
7121 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7122 | Builder.AddTextChunk("NSArray *"); | |||
7123 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7124 | Builder.AddTextChunk("array"); | |||
7125 | Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority, | |||
7126 | CXCursor_ObjCInstanceMethodDecl)); | |||
7127 | } | |||
7128 | } | |||
7129 | ||||
7130 | // Unordered getters | |||
7131 | // - (NSEnumerator *)enumeratorOfKey | |||
7132 | if (IsInstanceMethod && | |||
7133 | (ReturnType.isNull() || | |||
7134 | (ReturnType->isObjCObjectPointerType() && | |||
7135 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && | |||
7136 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() | |||
7137 | ->getName() == "NSEnumerator"))) { | |||
7138 | std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str(); | |||
7139 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7140 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) | |||
7141 | .second) { | |||
7142 | if (ReturnType.isNull()) { | |||
7143 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7144 | Builder.AddTextChunk("NSEnumerator *"); | |||
7145 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7146 | } | |||
7147 | ||||
7148 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); | |||
7149 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, | |||
7150 | CXCursor_ObjCInstanceMethodDecl)); | |||
7151 | } | |||
7152 | } | |||
7153 | ||||
7154 | // - (type *)memberOfKey:(type *)object | |||
7155 | if (IsInstanceMethod && | |||
7156 | (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) { | |||
7157 | std::string SelectorName = (Twine("memberOf") + UpperKey).str(); | |||
7158 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7159 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
7160 | if (ReturnType.isNull()) { | |||
7161 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7162 | Builder.AddPlaceholderChunk("object-type"); | |||
7163 | Builder.AddTextChunk(" *"); | |||
7164 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7165 | } | |||
7166 | ||||
7167 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
7168 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7169 | if (ReturnType.isNull()) { | |||
7170 | Builder.AddPlaceholderChunk("object-type"); | |||
7171 | Builder.AddTextChunk(" *"); | |||
7172 | } else { | |||
7173 | Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context, | |||
7174 | Policy, | |||
7175 | Builder.getAllocator())); | |||
7176 | } | |||
7177 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7178 | Builder.AddTextChunk("object"); | |||
7179 | Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority, | |||
7180 | CXCursor_ObjCInstanceMethodDecl)); | |||
7181 | } | |||
7182 | } | |||
7183 | ||||
7184 | // Mutable unordered accessors | |||
7185 | // - (void)addKeyObject:(type *)object | |||
7186 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
7187 | std::string SelectorName | |||
7188 | = (Twine("add") + UpperKey + Twine("Object")).str(); | |||
7189 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7190 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
7191 | if (ReturnType.isNull()) { | |||
7192 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7193 | Builder.AddTextChunk("void"); | |||
7194 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7195 | } | |||
7196 | ||||
7197 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
7198 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7199 | Builder.AddPlaceholderChunk("object-type"); | |||
7200 | Builder.AddTextChunk(" *"); | |||
7201 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7202 | Builder.AddTextChunk("object"); | |||
7203 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, | |||
7204 | CXCursor_ObjCInstanceMethodDecl)); | |||
7205 | } | |||
7206 | } | |||
7207 | ||||
7208 | // - (void)addKey:(NSSet *)objects | |||
7209 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
7210 | std::string SelectorName = (Twine("add") + UpperKey).str(); | |||
7211 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7212 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
7213 | if (ReturnType.isNull()) { | |||
7214 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7215 | Builder.AddTextChunk("void"); | |||
7216 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7217 | } | |||
7218 | ||||
7219 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
7220 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7221 | Builder.AddTextChunk("NSSet *"); | |||
7222 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7223 | Builder.AddTextChunk("objects"); | |||
7224 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, | |||
7225 | CXCursor_ObjCInstanceMethodDecl)); | |||
7226 | } | |||
7227 | } | |||
7228 | ||||
7229 | // - (void)removeKeyObject:(type *)object | |||
7230 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
7231 | std::string SelectorName | |||
7232 | = (Twine("remove") + UpperKey + Twine("Object")).str(); | |||
7233 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7234 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
7235 | if (ReturnType.isNull()) { | |||
7236 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7237 | Builder.AddTextChunk("void"); | |||
7238 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7239 | } | |||
7240 | ||||
7241 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
7242 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7243 | Builder.AddPlaceholderChunk("object-type"); | |||
7244 | Builder.AddTextChunk(" *"); | |||
7245 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7246 | Builder.AddTextChunk("object"); | |||
7247 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, | |||
7248 | CXCursor_ObjCInstanceMethodDecl)); | |||
7249 | } | |||
7250 | } | |||
7251 | ||||
7252 | // - (void)removeKey:(NSSet *)objects | |||
7253 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
7254 | std::string SelectorName = (Twine("remove") + UpperKey).str(); | |||
7255 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7256 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
7257 | if (ReturnType.isNull()) { | |||
7258 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7259 | Builder.AddTextChunk("void"); | |||
7260 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7261 | } | |||
7262 | ||||
7263 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
7264 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7265 | Builder.AddTextChunk("NSSet *"); | |||
7266 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7267 | Builder.AddTextChunk("objects"); | |||
7268 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, | |||
7269 | CXCursor_ObjCInstanceMethodDecl)); | |||
7270 | } | |||
7271 | } | |||
7272 | ||||
7273 | // - (void)intersectKey:(NSSet *)objects | |||
7274 | if (IsInstanceMethod && ReturnTypeMatchesVoid) { | |||
7275 | std::string SelectorName = (Twine("intersect") + UpperKey).str(); | |||
7276 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7277 | if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) { | |||
7278 | if (ReturnType.isNull()) { | |||
7279 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7280 | Builder.AddTextChunk("void"); | |||
7281 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7282 | } | |||
7283 | ||||
7284 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":")); | |||
7285 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7286 | Builder.AddTextChunk("NSSet *"); | |||
7287 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7288 | Builder.AddTextChunk("objects"); | |||
7289 | Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority, | |||
7290 | CXCursor_ObjCInstanceMethodDecl)); | |||
7291 | } | |||
7292 | } | |||
7293 | ||||
7294 | // Key-Value Observing | |||
7295 | // + (NSSet *)keyPathsForValuesAffectingKey | |||
7296 | if (!IsInstanceMethod && | |||
7297 | (ReturnType.isNull() || | |||
7298 | (ReturnType->isObjCObjectPointerType() && | |||
7299 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() && | |||
7300 | ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() | |||
7301 | ->getName() == "NSSet"))) { | |||
7302 | std::string SelectorName | |||
7303 | = (Twine("keyPathsForValuesAffecting") + UpperKey).str(); | |||
7304 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7305 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) | |||
7306 | .second) { | |||
7307 | if (ReturnType.isNull()) { | |||
7308 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7309 | Builder.AddTextChunk("NSSet<NSString *> *"); | |||
7310 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7311 | } | |||
7312 | ||||
7313 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); | |||
7314 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, | |||
7315 | CXCursor_ObjCClassMethodDecl)); | |||
7316 | } | |||
7317 | } | |||
7318 | ||||
7319 | // + (BOOL)automaticallyNotifiesObserversForKey | |||
7320 | if (!IsInstanceMethod && | |||
7321 | (ReturnType.isNull() || | |||
7322 | ReturnType->isIntegerType() || | |||
7323 | ReturnType->isBooleanType())) { | |||
7324 | std::string SelectorName | |||
7325 | = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str(); | |||
7326 | IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName); | |||
7327 | if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId)) | |||
7328 | .second) { | |||
7329 | if (ReturnType.isNull()) { | |||
7330 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7331 | Builder.AddTextChunk("BOOL"); | |||
7332 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7333 | } | |||
7334 | ||||
7335 | Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName)); | |||
7336 | Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern, | |||
7337 | CXCursor_ObjCClassMethodDecl)); | |||
7338 | } | |||
7339 | } | |||
7340 | } | |||
7341 | ||||
7342 | void Sema::CodeCompleteObjCMethodDecl(Scope *S, | |||
7343 | bool IsInstanceMethod, | |||
7344 | ParsedType ReturnTy) { | |||
7345 | // Determine the return type of the method we're declaring, if | |||
7346 | // provided. | |||
7347 | QualType ReturnType = GetTypeFromParser(ReturnTy); | |||
7348 | Decl *IDecl = nullptr; | |||
7349 | if (CurContext->isObjCContainer()) { | |||
7350 | ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext); | |||
7351 | IDecl = cast<Decl>(OCD); | |||
7352 | } | |||
7353 | // Determine where we should start searching for methods. | |||
7354 | ObjCContainerDecl *SearchDecl = nullptr; | |||
7355 | bool IsInImplementation = false; | |||
7356 | if (Decl *D = IDecl) { | |||
7357 | if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) { | |||
7358 | SearchDecl = Impl->getClassInterface(); | |||
7359 | IsInImplementation = true; | |||
7360 | } else if (ObjCCategoryImplDecl *CatImpl | |||
7361 | = dyn_cast<ObjCCategoryImplDecl>(D)) { | |||
7362 | SearchDecl = CatImpl->getCategoryDecl(); | |||
7363 | IsInImplementation = true; | |||
7364 | } else | |||
7365 | SearchDecl = dyn_cast<ObjCContainerDecl>(D); | |||
7366 | } | |||
7367 | ||||
7368 | if (!SearchDecl && S) { | |||
7369 | if (DeclContext *DC = S->getEntity()) | |||
7370 | SearchDecl = dyn_cast<ObjCContainerDecl>(DC); | |||
7371 | } | |||
7372 | ||||
7373 | if (!SearchDecl) { | |||
7374 | HandleCodeCompleteResults(this, CodeCompleter, | |||
7375 | CodeCompletionContext::CCC_Other, | |||
7376 | nullptr, 0); | |||
7377 | return; | |||
7378 | } | |||
7379 | ||||
7380 | // Find all of the methods that we could declare/implement here. | |||
7381 | KnownMethodsMap KnownMethods; | |||
7382 | FindImplementableMethods(Context, SearchDecl, IsInstanceMethod, | |||
7383 | ReturnType, KnownMethods); | |||
7384 | ||||
7385 | // Add declarations or definitions for each of the known methods. | |||
7386 | typedef CodeCompletionResult Result; | |||
7387 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
7388 | CodeCompleter->getCodeCompletionTUInfo(), | |||
7389 | CodeCompletionContext::CCC_Other); | |||
7390 | Results.EnterNewScope(); | |||
7391 | PrintingPolicy Policy = getCompletionPrintingPolicy(*this); | |||
7392 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), | |||
7393 | MEnd = KnownMethods.end(); | |||
7394 | M != MEnd; ++M) { | |||
7395 | ObjCMethodDecl *Method = M->second.getPointer(); | |||
7396 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
7397 | Results.getCodeCompletionTUInfo()); | |||
7398 | ||||
7399 | // If the result type was not already provided, add it to the | |||
7400 | // pattern as (type). | |||
7401 | if (ReturnType.isNull()) { | |||
7402 | QualType ResTy = Method->getSendResultType().stripObjCKindOfType(Context); | |||
7403 | AttributedType::stripOuterNullability(ResTy); | |||
7404 | AddObjCPassingTypeChunk(ResTy, | |||
7405 | Method->getObjCDeclQualifier(), Context, Policy, | |||
7406 | Builder); | |||
7407 | } | |||
7408 | ||||
7409 | Selector Sel = Method->getSelector(); | |||
7410 | ||||
7411 | // Add the first part of the selector to the pattern. | |||
7412 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | |||
7413 | Sel.getNameForSlot(0))); | |||
7414 | ||||
7415 | // Add parameters to the pattern. | |||
7416 | unsigned I = 0; | |||
7417 | for (ObjCMethodDecl::param_iterator P = Method->param_begin(), | |||
7418 | PEnd = Method->param_end(); | |||
7419 | P != PEnd; (void)++P, ++I) { | |||
7420 | // Add the part of the selector name. | |||
7421 | if (I == 0) | |||
7422 | Builder.AddTypedTextChunk(":"); | |||
7423 | else if (I < Sel.getNumArgs()) { | |||
7424 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7425 | Builder.AddTypedTextChunk( | |||
7426 | Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":")); | |||
7427 | } else | |||
7428 | break; | |||
7429 | ||||
7430 | // Add the parameter type. | |||
7431 | QualType ParamType; | |||
7432 | if ((*P)->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) | |||
7433 | ParamType = (*P)->getType(); | |||
7434 | else | |||
7435 | ParamType = (*P)->getOriginalType(); | |||
7436 | ParamType = ParamType.substObjCTypeArgs(Context, {}, | |||
7437 | ObjCSubstitutionContext::Parameter); | |||
7438 | AttributedType::stripOuterNullability(ParamType); | |||
7439 | AddObjCPassingTypeChunk(ParamType, | |||
7440 | (*P)->getObjCDeclQualifier(), | |||
7441 | Context, Policy, | |||
7442 | Builder); | |||
7443 | ||||
7444 | if (IdentifierInfo *Id = (*P)->getIdentifier()) | |||
7445 | Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName())); | |||
7446 | } | |||
7447 | ||||
7448 | if (Method->isVariadic()) { | |||
7449 | if (Method->param_size() > 0) | |||
7450 | Builder.AddChunk(CodeCompletionString::CK_Comma); | |||
7451 | Builder.AddTextChunk("..."); | |||
7452 | } | |||
7453 | ||||
7454 | if (IsInImplementation && Results.includeCodePatterns()) { | |||
7455 | // We will be defining the method here, so add a compound statement. | |||
7456 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7457 | Builder.AddChunk(CodeCompletionString::CK_LeftBrace); | |||
7458 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
7459 | if (!Method->getReturnType()->isVoidType()) { | |||
7460 | // If the result type is not void, add a return clause. | |||
7461 | Builder.AddTextChunk("return"); | |||
7462 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7463 | Builder.AddPlaceholderChunk("expression"); | |||
7464 | Builder.AddChunk(CodeCompletionString::CK_SemiColon); | |||
7465 | } else | |||
7466 | Builder.AddPlaceholderChunk("statements"); | |||
7467 | ||||
7468 | Builder.AddChunk(CodeCompletionString::CK_VerticalSpace); | |||
7469 | Builder.AddChunk(CodeCompletionString::CK_RightBrace); | |||
7470 | } | |||
7471 | ||||
7472 | unsigned Priority = CCP_CodePattern; | |||
7473 | if (!M->second.getInt()) | |||
7474 | Priority += CCD_InBaseClass; | |||
7475 | ||||
7476 | Results.AddResult(Result(Builder.TakeString(), Method, Priority)); | |||
7477 | } | |||
7478 | ||||
7479 | // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of | |||
7480 | // the properties in this class and its categories. | |||
7481 | if (Context.getLangOpts().ObjC2) { | |||
7482 | SmallVector<ObjCContainerDecl *, 4> Containers; | |||
7483 | Containers.push_back(SearchDecl); | |||
7484 | ||||
7485 | VisitedSelectorSet KnownSelectors; | |||
7486 | for (KnownMethodsMap::iterator M = KnownMethods.begin(), | |||
7487 | MEnd = KnownMethods.end(); | |||
7488 | M != MEnd; ++M) | |||
7489 | KnownSelectors.insert(M->first); | |||
7490 | ||||
7491 | ||||
7492 | ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl); | |||
7493 | if (!IFace) | |||
7494 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl)) | |||
7495 | IFace = Category->getClassInterface(); | |||
7496 | ||||
7497 | if (IFace) | |||
7498 | for (auto *Cat : IFace->visible_categories()) | |||
7499 | Containers.push_back(Cat); | |||
7500 | ||||
7501 | for (unsigned I = 0, N = Containers.size(); I != N; ++I) | |||
7502 | for (auto *P : Containers[I]->instance_properties()) | |||
7503 | AddObjCKeyValueCompletions(P, IsInstanceMethod, ReturnType, Context, | |||
7504 | KnownSelectors, Results); | |||
7505 | } | |||
7506 | ||||
7507 | Results.ExitScope(); | |||
7508 | ||||
7509 | HandleCodeCompleteResults(this, CodeCompleter, | |||
7510 | CodeCompletionContext::CCC_Other, | |||
7511 | Results.data(),Results.size()); | |||
7512 | } | |||
7513 | ||||
7514 | void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S, | |||
7515 | bool IsInstanceMethod, | |||
7516 | bool AtParameterName, | |||
7517 | ParsedType ReturnTy, | |||
7518 | ArrayRef<IdentifierInfo *> SelIdents) { | |||
7519 | // If we have an external source, load the entire class method | |||
7520 | // pool from the AST file. | |||
7521 | if (ExternalSource) { | |||
7522 | for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors(); | |||
7523 | I != N; ++I) { | |||
7524 | Selector Sel = ExternalSource->GetExternalSelector(I); | |||
7525 | if (Sel.isNull() || MethodPool.count(Sel)) | |||
7526 | continue; | |||
7527 | ||||
7528 | ReadMethodPool(Sel); | |||
7529 | } | |||
7530 | } | |||
7531 | ||||
7532 | // Build the set of methods we can see. | |||
7533 | typedef CodeCompletionResult Result; | |||
7534 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
7535 | CodeCompleter->getCodeCompletionTUInfo(), | |||
7536 | CodeCompletionContext::CCC_Other); | |||
7537 | ||||
7538 | if (ReturnTy) | |||
7539 | Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType()); | |||
7540 | ||||
7541 | Results.EnterNewScope(); | |||
7542 | for (GlobalMethodPool::iterator M = MethodPool.begin(), | |||
7543 | MEnd = MethodPool.end(); | |||
7544 | M != MEnd; ++M) { | |||
7545 | for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first : | |||
7546 | &M->second.second; | |||
7547 | MethList && MethList->getMethod(); | |||
7548 | MethList = MethList->getNext()) { | |||
7549 | if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents)) | |||
7550 | continue; | |||
7551 | ||||
7552 | if (AtParameterName) { | |||
7553 | // Suggest parameter names we've seen before. | |||
7554 | unsigned NumSelIdents = SelIdents.size(); | |||
7555 | if (NumSelIdents && | |||
7556 | NumSelIdents <= MethList->getMethod()->param_size()) { | |||
7557 | ParmVarDecl *Param = | |||
7558 | MethList->getMethod()->parameters()[NumSelIdents - 1]; | |||
7559 | if (Param->getIdentifier()) { | |||
7560 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
7561 | Results.getCodeCompletionTUInfo()); | |||
7562 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | |||
7563 | Param->getIdentifier()->getName())); | |||
7564 | Results.AddResult(Builder.TakeString()); | |||
7565 | } | |||
7566 | } | |||
7567 | ||||
7568 | continue; | |||
7569 | } | |||
7570 | ||||
7571 | Result R(MethList->getMethod(), | |||
7572 | Results.getBasePriority(MethList->getMethod()), nullptr); | |||
7573 | R.StartParameter = SelIdents.size(); | |||
7574 | R.AllParametersAreInformative = false; | |||
7575 | R.DeclaringEntity = true; | |||
7576 | Results.MaybeAddResult(R, CurContext); | |||
7577 | } | |||
7578 | } | |||
7579 | ||||
7580 | Results.ExitScope(); | |||
7581 | ||||
7582 | if (!AtParameterName && !SelIdents.empty() && | |||
7583 | SelIdents.front()->getName().startswith("init")) { | |||
7584 | for (const auto &M : PP.macros()) { | |||
7585 | if (M.first->getName() != "NS_DESIGNATED_INITIALIZER") | |||
7586 | continue; | |||
7587 | Results.EnterNewScope(); | |||
7588 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
7589 | Results.getCodeCompletionTUInfo()); | |||
7590 | Builder.AddTypedTextChunk( | |||
7591 | Builder.getAllocator().CopyString(M.first->getName())); | |||
7592 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), CCP_Macro, | |||
7593 | CXCursor_MacroDefinition)); | |||
7594 | Results.ExitScope(); | |||
7595 | } | |||
7596 | } | |||
7597 | ||||
7598 | HandleCodeCompleteResults(this, CodeCompleter, | |||
7599 | CodeCompletionContext::CCC_Other, | |||
7600 | Results.data(),Results.size()); | |||
7601 | } | |||
7602 | ||||
7603 | void Sema::CodeCompletePreprocessorDirective(bool InConditional) { | |||
7604 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
7605 | CodeCompleter->getCodeCompletionTUInfo(), | |||
7606 | CodeCompletionContext::CCC_PreprocessorDirective); | |||
7607 | Results.EnterNewScope(); | |||
7608 | ||||
7609 | // #if <condition> | |||
7610 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
7611 | Results.getCodeCompletionTUInfo()); | |||
7612 | Builder.AddTypedTextChunk("if"); | |||
7613 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7614 | Builder.AddPlaceholderChunk("condition"); | |||
7615 | Results.AddResult(Builder.TakeString()); | |||
7616 | ||||
7617 | // #ifdef <macro> | |||
7618 | Builder.AddTypedTextChunk("ifdef"); | |||
7619 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7620 | Builder.AddPlaceholderChunk("macro"); | |||
7621 | Results.AddResult(Builder.TakeString()); | |||
7622 | ||||
7623 | // #ifndef <macro> | |||
7624 | Builder.AddTypedTextChunk("ifndef"); | |||
7625 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7626 | Builder.AddPlaceholderChunk("macro"); | |||
7627 | Results.AddResult(Builder.TakeString()); | |||
7628 | ||||
7629 | if (InConditional) { | |||
7630 | // #elif <condition> | |||
7631 | Builder.AddTypedTextChunk("elif"); | |||
7632 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7633 | Builder.AddPlaceholderChunk("condition"); | |||
7634 | Results.AddResult(Builder.TakeString()); | |||
7635 | ||||
7636 | // #else | |||
7637 | Builder.AddTypedTextChunk("else"); | |||
7638 | Results.AddResult(Builder.TakeString()); | |||
7639 | ||||
7640 | // #endif | |||
7641 | Builder.AddTypedTextChunk("endif"); | |||
7642 | Results.AddResult(Builder.TakeString()); | |||
7643 | } | |||
7644 | ||||
7645 | // #include "header" | |||
7646 | Builder.AddTypedTextChunk("include"); | |||
7647 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7648 | Builder.AddTextChunk("\""); | |||
7649 | Builder.AddPlaceholderChunk("header"); | |||
7650 | Builder.AddTextChunk("\""); | |||
7651 | Results.AddResult(Builder.TakeString()); | |||
7652 | ||||
7653 | // #include <header> | |||
7654 | Builder.AddTypedTextChunk("include"); | |||
7655 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7656 | Builder.AddTextChunk("<"); | |||
7657 | Builder.AddPlaceholderChunk("header"); | |||
7658 | Builder.AddTextChunk(">"); | |||
7659 | Results.AddResult(Builder.TakeString()); | |||
7660 | ||||
7661 | // #define <macro> | |||
7662 | Builder.AddTypedTextChunk("define"); | |||
7663 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7664 | Builder.AddPlaceholderChunk("macro"); | |||
7665 | Results.AddResult(Builder.TakeString()); | |||
7666 | ||||
7667 | // #define <macro>(<args>) | |||
7668 | Builder.AddTypedTextChunk("define"); | |||
7669 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7670 | Builder.AddPlaceholderChunk("macro"); | |||
7671 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7672 | Builder.AddPlaceholderChunk("args"); | |||
7673 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7674 | Results.AddResult(Builder.TakeString()); | |||
7675 | ||||
7676 | // #undef <macro> | |||
7677 | Builder.AddTypedTextChunk("undef"); | |||
7678 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7679 | Builder.AddPlaceholderChunk("macro"); | |||
7680 | Results.AddResult(Builder.TakeString()); | |||
7681 | ||||
7682 | // #line <number> | |||
7683 | Builder.AddTypedTextChunk("line"); | |||
7684 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7685 | Builder.AddPlaceholderChunk("number"); | |||
7686 | Results.AddResult(Builder.TakeString()); | |||
7687 | ||||
7688 | // #line <number> "filename" | |||
7689 | Builder.AddTypedTextChunk("line"); | |||
7690 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7691 | Builder.AddPlaceholderChunk("number"); | |||
7692 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7693 | Builder.AddTextChunk("\""); | |||
7694 | Builder.AddPlaceholderChunk("filename"); | |||
7695 | Builder.AddTextChunk("\""); | |||
7696 | Results.AddResult(Builder.TakeString()); | |||
7697 | ||||
7698 | // #error <message> | |||
7699 | Builder.AddTypedTextChunk("error"); | |||
7700 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7701 | Builder.AddPlaceholderChunk("message"); | |||
7702 | Results.AddResult(Builder.TakeString()); | |||
7703 | ||||
7704 | // #pragma <arguments> | |||
7705 | Builder.AddTypedTextChunk("pragma"); | |||
7706 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7707 | Builder.AddPlaceholderChunk("arguments"); | |||
7708 | Results.AddResult(Builder.TakeString()); | |||
7709 | ||||
7710 | if (getLangOpts().ObjC1) { | |||
7711 | // #import "header" | |||
7712 | Builder.AddTypedTextChunk("import"); | |||
7713 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7714 | Builder.AddTextChunk("\""); | |||
7715 | Builder.AddPlaceholderChunk("header"); | |||
7716 | Builder.AddTextChunk("\""); | |||
7717 | Results.AddResult(Builder.TakeString()); | |||
7718 | ||||
7719 | // #import <header> | |||
7720 | Builder.AddTypedTextChunk("import"); | |||
7721 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7722 | Builder.AddTextChunk("<"); | |||
7723 | Builder.AddPlaceholderChunk("header"); | |||
7724 | Builder.AddTextChunk(">"); | |||
7725 | Results.AddResult(Builder.TakeString()); | |||
7726 | } | |||
7727 | ||||
7728 | // #include_next "header" | |||
7729 | Builder.AddTypedTextChunk("include_next"); | |||
7730 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7731 | Builder.AddTextChunk("\""); | |||
7732 | Builder.AddPlaceholderChunk("header"); | |||
7733 | Builder.AddTextChunk("\""); | |||
7734 | Results.AddResult(Builder.TakeString()); | |||
7735 | ||||
7736 | // #include_next <header> | |||
7737 | Builder.AddTypedTextChunk("include_next"); | |||
7738 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7739 | Builder.AddTextChunk("<"); | |||
7740 | Builder.AddPlaceholderChunk("header"); | |||
7741 | Builder.AddTextChunk(">"); | |||
7742 | Results.AddResult(Builder.TakeString()); | |||
7743 | ||||
7744 | // #warning <message> | |||
7745 | Builder.AddTypedTextChunk("warning"); | |||
7746 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7747 | Builder.AddPlaceholderChunk("message"); | |||
7748 | Results.AddResult(Builder.TakeString()); | |||
7749 | ||||
7750 | // Note: #ident and #sccs are such crazy anachronisms that we don't provide | |||
7751 | // completions for them. And __include_macros is a Clang-internal extension | |||
7752 | // that we don't want to encourage anyone to use. | |||
7753 | ||||
7754 | // FIXME: we don't support #assert or #unassert, so don't suggest them. | |||
7755 | Results.ExitScope(); | |||
7756 | ||||
7757 | HandleCodeCompleteResults(this, CodeCompleter, | |||
7758 | CodeCompletionContext::CCC_PreprocessorDirective, | |||
7759 | Results.data(), Results.size()); | |||
7760 | } | |||
7761 | ||||
7762 | void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) { | |||
7763 | CodeCompleteOrdinaryName(S, | |||
7764 | S->getFnParent()? Sema::PCC_RecoveryInFunction | |||
7765 | : Sema::PCC_Namespace); | |||
7766 | } | |||
7767 | ||||
7768 | void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) { | |||
7769 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
7770 | CodeCompleter->getCodeCompletionTUInfo(), | |||
7771 | IsDefinition? CodeCompletionContext::CCC_MacroName | |||
7772 | : CodeCompletionContext::CCC_MacroNameUse); | |||
7773 | if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) { | |||
7774 | // Add just the names of macros, not their arguments. | |||
7775 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
7776 | Results.getCodeCompletionTUInfo()); | |||
7777 | Results.EnterNewScope(); | |||
7778 | for (Preprocessor::macro_iterator M = PP.macro_begin(), | |||
7779 | MEnd = PP.macro_end(); | |||
7780 | M != MEnd; ++M) { | |||
7781 | Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( | |||
7782 | M->first->getName())); | |||
7783 | Results.AddResult(CodeCompletionResult(Builder.TakeString(), | |||
7784 | CCP_CodePattern, | |||
7785 | CXCursor_MacroDefinition)); | |||
7786 | } | |||
7787 | Results.ExitScope(); | |||
7788 | } else if (IsDefinition) { | |||
7789 | // FIXME: Can we detect when the user just wrote an include guard above? | |||
7790 | } | |||
7791 | ||||
7792 | HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(), | |||
7793 | Results.data(), Results.size()); | |||
7794 | } | |||
7795 | ||||
7796 | void Sema::CodeCompletePreprocessorExpression() { | |||
7797 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
7798 | CodeCompleter->getCodeCompletionTUInfo(), | |||
7799 | CodeCompletionContext::CCC_PreprocessorExpression); | |||
7800 | ||||
7801 | if (!CodeCompleter || CodeCompleter->includeMacros()) | |||
7802 | AddMacroResults(PP, Results, true); | |||
7803 | ||||
7804 | // defined (<macro>) | |||
7805 | Results.EnterNewScope(); | |||
7806 | CodeCompletionBuilder Builder(Results.getAllocator(), | |||
7807 | Results.getCodeCompletionTUInfo()); | |||
7808 | Builder.AddTypedTextChunk("defined"); | |||
7809 | Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); | |||
7810 | Builder.AddChunk(CodeCompletionString::CK_LeftParen); | |||
7811 | Builder.AddPlaceholderChunk("macro"); | |||
7812 | Builder.AddChunk(CodeCompletionString::CK_RightParen); | |||
7813 | Results.AddResult(Builder.TakeString()); | |||
7814 | Results.ExitScope(); | |||
7815 | ||||
7816 | HandleCodeCompleteResults(this, CodeCompleter, | |||
7817 | CodeCompletionContext::CCC_PreprocessorExpression, | |||
7818 | Results.data(), Results.size()); | |||
7819 | } | |||
7820 | ||||
7821 | void Sema::CodeCompletePreprocessorMacroArgument(Scope *S, | |||
7822 | IdentifierInfo *Macro, | |||
7823 | MacroInfo *MacroInfo, | |||
7824 | unsigned Argument) { | |||
7825 | // FIXME: In the future, we could provide "overload" results, much like we | |||
7826 | // do for function calls. | |||
7827 | ||||
7828 | // Now just ignore this. There will be another code-completion callback | |||
7829 | // for the expanded tokens. | |||
7830 | } | |||
7831 | ||||
7832 | void Sema::CodeCompleteNaturalLanguage() { | |||
7833 | HandleCodeCompleteResults(this, CodeCompleter, | |||
7834 | CodeCompletionContext::CCC_NaturalLanguage, | |||
7835 | nullptr, 0); | |||
7836 | } | |||
7837 | ||||
7838 | void Sema::CodeCompleteAvailabilityPlatformName() { | |||
7839 | ResultBuilder Results(*this, CodeCompleter->getAllocator(), | |||
7840 | CodeCompleter->getCodeCompletionTUInfo(), | |||
7841 | CodeCompletionContext::CCC_Other); | |||
7842 | Results.EnterNewScope(); | |||
7843 | static const char *Platforms[] = {"macOS", "iOS", "watchOS", "tvOS"}; | |||
7844 | for (const char *Platform : llvm::makeArrayRef(Platforms)) { | |||
7845 | Results.AddResult(CodeCompletionResult(Platform)); | |||
7846 | Results.AddResult(CodeCompletionResult(Results.getAllocator().CopyString( | |||
7847 | Twine(Platform) + "ApplicationExtension"))); | |||
7848 | } | |||
7849 | Results.ExitScope(); | |||
7850 | HandleCodeCompleteResults(this, CodeCompleter, | |||
7851 | CodeCompletionContext::CCC_Other, Results.data(), | |||
7852 | Results.size()); | |||
7853 | } | |||
7854 | ||||
7855 | void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator, | |||
7856 | CodeCompletionTUInfo &CCTUInfo, | |||
7857 | SmallVectorImpl<CodeCompletionResult> &Results) { | |||
7858 | ResultBuilder Builder(*this, Allocator, CCTUInfo, | |||
7859 | CodeCompletionContext::CCC_Recovery); | |||
7860 | if (!CodeCompleter || CodeCompleter->includeGlobals()) { | |||
7861 | CodeCompletionDeclConsumer Consumer(Builder, | |||
7862 | Context.getTranslationUnitDecl()); | |||
7863 | LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName, | |||
7864 | Consumer); | |||
7865 | } | |||
7866 | ||||
7867 | if (!CodeCompleter || CodeCompleter->includeMacros()) | |||
7868 | AddMacroResults(PP, Builder, true); | |||
7869 | ||||
7870 | Results.clear(); | |||
7871 | Results.insert(Results.end(), | |||
7872 | Builder.data(), Builder.data() + Builder.size()); | |||
7873 | } |