blob: 04f6ba72fc2bfde0b1c3b6df3b54bd8b7dcacd87 [file] [log] [blame]
Douglas Gregor81b747b2009-09-17 21:32:03 +00001//===---------------- 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//===----------------------------------------------------------------------===//
John McCall2d887082010-08-25 22:03:47 +000013#include "clang/Sema/SemaInternal.h"
John McCall7cd088e2010-08-24 07:21:54 +000014#include "clang/AST/DeclObjC.h"
Douglas Gregorb9d0ef72009-09-21 19:57:38 +000015#include "clang/AST/ExprCXX.h"
Douglas Gregor24a069f2009-11-17 17:59:40 +000016#include "clang/AST/ExprObjC.h"
Douglas Gregorc5b2e582012-01-29 18:15:03 +000017#include "clang/Lex/HeaderSearch.h"
Douglas Gregor3f7c7f42009-10-30 16:50:04 +000018#include "clang/Lex/MacroInfo.h"
19#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/Sema/CodeCompleteConsumer.h"
21#include "clang/Sema/ExternalSemaSource.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"
Douglas Gregord36adf52010-09-16 16:06:31 +000026#include "llvm/ADT/DenseSet.h"
Benjamin Kramer013b3662012-01-30 16:17:39 +000027#include "llvm/ADT/SmallBitVector.h"
Douglas Gregor86d9a522009-09-21 16:56:56 +000028#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000029#include "llvm/ADT/SmallString.h"
Douglas Gregor6a684032009-09-28 03:51:44 +000030#include "llvm/ADT/StringExtras.h"
Douglas Gregor22f56992010-04-06 19:22:33 +000031#include "llvm/ADT/StringSwitch.h"
Douglas Gregor458433d2010-08-26 15:07:07 +000032#include "llvm/ADT/Twine.h"
Douglas Gregor86d9a522009-09-21 16:56:56 +000033#include <list>
34#include <map>
35#include <vector>
Douglas Gregor81b747b2009-09-17 21:32:03 +000036
37using namespace clang;
John McCall781472f2010-08-25 08:40:02 +000038using namespace sema;
Douglas Gregor81b747b2009-09-17 21:32:03 +000039
Douglas Gregor86d9a522009-09-21 16:56:56 +000040namespace {
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).
Dmitri Gribenko89cf4252013-01-23 17:21:11 +000048 typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const;
Douglas Gregor86d9a522009-09-21 16:56:56 +000049
John McCall0a2c5e22010-08-25 06:19:51 +000050 typedef CodeCompletionResult Result;
Douglas Gregor86d9a522009-09-21 16:56:56 +000051
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.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +000059 llvm::SmallPtrSet<const Decl*, 16> AllDeclsFound;
Douglas Gregor86d9a522009-09-21 16:56:56 +000060
Dmitri Gribenko89cf4252013-01-23 17:21:11 +000061 typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair;
Douglas Gregorfbcb5d62009-12-06 20:23:50 +000062
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 {
Chris Lattner5f9e2722011-07-23 10:55:15 +000067 typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector;
Douglas Gregorfbcb5d62009-12-06 20:23:50 +000068
69 /// \brief Contains either the solitary NamedDecl * or a vector
70 /// of (declaration, index) pairs.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +000071 llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector*> DeclOrVector;
Douglas Gregorfbcb5d62009-12-06 20:23:50 +000072
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
Dmitri Gribenko89cf4252013-01-23 17:21:11 +000080 void Add(const NamedDecl *ND, unsigned Index) {
Douglas Gregorfbcb5d62009-12-06 20:23:50 +000081 if (DeclOrVector.isNull()) {
82 // 0 - > 1 elements: just set the single element information.
83 DeclOrVector = ND;
84 SingleDeclIndex = Index;
85 return;
86 }
87
Dmitri Gribenko89cf4252013-01-23 17:21:11 +000088 if (const NamedDecl *PrevND =
89 DeclOrVector.dyn_cast<const NamedDecl *>()) {
Douglas Gregorfbcb5d62009-12-06 20:23:50 +000090 // 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 *)0);
107 }
108 }
109
110 // Iteration.
111 class iterator;
112 iterator begin() const;
113 iterator end() const;
114 };
115
Douglas Gregor86d9a522009-09-21 16:56:56 +0000116 /// \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.
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000119 typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000120
121 /// \brief The semantic analysis object for which results are being
122 /// produced.
123 Sema &SemaRef;
Douglas Gregor218937c2011-02-01 19:23:04 +0000124
125 /// \brief The allocator used to allocate new code-completion strings.
Douglas Gregordae68752011-02-01 22:57:45 +0000126 CodeCompletionAllocator &Allocator;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000127
128 CodeCompletionTUInfo &CCTUInfo;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000129
130 /// \brief If non-NULL, a filter function used to remove any code-completion
131 /// results that are not desirable.
132 LookupFilter Filter;
Douglas Gregor45bcd432010-01-14 03:21:49 +0000133
134 /// \brief Whether we should allow declarations as
135 /// nested-name-specifiers that would otherwise be filtered out.
136 bool AllowNestedNameSpecifiers;
137
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000138 /// \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
Douglas Gregor86d9a522009-09-21 16:56:56 +0000145 /// \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
Douglas Gregor3cdee122010-08-26 16:36:48 +0000149 /// \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
Douglas Gregor265f7492010-08-27 15:29:55 +0000156 /// \brief The selector that we prefer.
157 Selector PreferredSelector;
158
Douglas Gregorca45da02010-11-02 20:36:02 +0000159 /// \brief The completion context in which we are gathering results.
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000160 CodeCompletionContext CompletionContext;
161
James Dennetta40f7922012-06-14 03:11:41 +0000162 /// \brief If we are in an instance method definition, the \@implementation
Douglas Gregorca45da02010-11-02 20:36:02 +0000163 /// object.
164 ObjCImplementationDecl *ObjCImplementation;
Douglas Gregord1f09b42013-01-31 04:52:16 +0000165
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000166 void AdjustResultPriorityForDecl(Result &R);
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000167
Douglas Gregor6f942b22010-09-21 16:06:22 +0000168 void MaybeAddConstructorResults(Result R);
169
Douglas Gregor86d9a522009-09-21 16:56:56 +0000170 public:
Douglas Gregordae68752011-02-01 22:57:45 +0000171 explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000172 CodeCompletionTUInfo &CCTUInfo,
Douglas Gregor52779fb2010-09-23 23:01:17 +0000173 const CodeCompletionContext &CompletionContext,
174 LookupFilter Filter = 0)
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000175 : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo),
176 Filter(Filter),
Douglas Gregor218937c2011-02-01 19:23:04 +0000177 AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false),
Douglas Gregorca45da02010-11-02 20:36:02 +0000178 CompletionContext(CompletionContext),
179 ObjCImplementation(0)
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 }
Douglas Gregord1f09b42013-01-31 04:52:16 +0000199
200 /// \brief Determine the priority for a reference to the given declaration.
201 unsigned getBasePriority(const NamedDecl *D);
202
Douglas Gregord8e8a582010-05-25 21:41:55 +0000203 /// \brief Whether we should include code patterns in the completion
204 /// results.
205 bool includeCodePatterns() const {
206 return SemaRef.CodeCompleter &&
Douglas Gregorf6961522010-08-27 21:18:54 +0000207 SemaRef.CodeCompleter->includeCodePatterns();
Douglas Gregord8e8a582010-05-25 21:41:55 +0000208 }
209
Douglas Gregor86d9a522009-09-21 16:56:56 +0000210 /// \brief Set the filter used for code-completion results.
211 void setFilter(LookupFilter Filter) {
212 this->Filter = Filter;
213 }
214
Douglas Gregor86d9a522009-09-21 16:56:56 +0000215 Result *data() { return Results.empty()? 0 : &Results.front(); }
216 unsigned size() const { return Results.size(); }
217 bool empty() const { return Results.empty(); }
218
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000219 /// \brief Specify the preferred type.
220 void setPreferredType(QualType T) {
221 PreferredType = SemaRef.Context.getCanonicalType(T);
222 }
223
Douglas Gregor3cdee122010-08-26 16:36:48 +0000224 /// \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
Douglas Gregor265f7492010-08-27 15:29:55 +0000236 /// \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 }
Douglas Gregorca45da02010-11-02 20:36:02 +0000244
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000245 /// \brief Retrieve the code-completion context for which results are
246 /// being collected.
247 const CodeCompletionContext &getCompletionContext() const {
248 return CompletionContext;
249 }
250
Douglas Gregor45bcd432010-01-14 03:21:49 +0000251 /// \brief Specify whether nested-name-specifiers are allowed.
252 void allowNestedNameSpecifiers(bool Allow = true) {
253 AllowNestedNameSpecifiers = Allow;
254 }
255
Douglas Gregorb9d77572010-09-21 00:03:25 +0000256 /// \brief Return the semantic analysis object for which we are collecting
257 /// code completion results.
258 Sema &getSema() const { return SemaRef; }
259
Douglas Gregor218937c2011-02-01 19:23:04 +0000260 /// \brief Retrieve the allocator used to allocate code completion strings.
Douglas Gregordae68752011-02-01 22:57:45 +0000261 CodeCompletionAllocator &getAllocator() const { return Allocator; }
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000262
263 CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
Douglas Gregor218937c2011-02-01 19:23:04 +0000264
Douglas Gregore495b7f2010-01-14 00:20:49 +0000265 /// \brief Determine whether the given declaration is at all interesting
266 /// as a code-completion result.
Douglas Gregor45bcd432010-01-14 03:21:49 +0000267 ///
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.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000272 bool isInterestingDecl(const NamedDecl *ND,
273 bool &AsNestedNameSpecifier) const;
Douglas Gregor6660d842010-01-14 00:41:07 +0000274
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,
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000282 const NamedDecl *Hiding);
Douglas Gregor6660d842010-01-14 00:41:07 +0000283
Douglas Gregor86d9a522009-09-21 16:56:56 +0000284 /// \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).
Douglas Gregor456c4a12009-09-21 20:12:40 +0000287 ///
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000288 /// \param R the result to add (if it is unique).
Douglas Gregor456c4a12009-09-21 20:12:40 +0000289 ///
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000290 /// \param CurContext the context in which this result will be named.
Douglas Gregor456c4a12009-09-21 20:12:40 +0000291 void MaybeAddResult(Result R, DeclContext *CurContext = 0);
Douglas Gregor86d9a522009-09-21 16:56:56 +0000292
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000293 /// \brief Add a new result to this result set, where we already know
294 /// the hiding declation (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.
Douglas Gregor0cc84042010-01-14 15:47:35 +0000301 ///
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);
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000306
Douglas Gregora4477812010-01-14 16:01:26 +0000307 /// \brief Add a new non-declaration result to this result set.
308 void AddResult(Result R);
309
Douglas Gregor86d9a522009-09-21 16:56:56 +0000310 /// \brief Enter into a new scope.
311 void EnterNewScope();
312
313 /// \brief Exit from the current scope.
314 void ExitScope();
315
Douglas Gregor55385fe2009-11-18 04:19:12 +0000316 /// \brief Ignore this declaration, if it is seen again.
317 void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); }
318
Douglas Gregor86d9a522009-09-21 16:56:56 +0000319 /// \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 //@{
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000325 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;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000342 //@}
343 };
344}
345
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000346class ResultBuilder::ShadowMapEntry::iterator {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000347 llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator;
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000348 unsigned SingleDeclIndex;
349
350public:
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 *)0), SingleDeclIndex(0) { }
368
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000369 iterator(const NamedDecl *SingleDecl, unsigned Index)
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000370 : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { }
371
372 iterator(const DeclIndexPair *Iterator)
373 : DeclOrIterator(Iterator), SingleDeclIndex(0) { }
374
375 iterator &operator++() {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000376 if (DeclOrIterator.is<const NamedDecl *>()) {
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000377 DeclOrIterator = (NamedDecl *)0;
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
Chris Lattner66392d42010-09-04 18:12:20 +0000388 /*iterator operator++(int) {
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000389 iterator tmp(*this);
390 ++(*this);
391 return tmp;
Chris Lattner66392d42010-09-04 18:12:20 +0000392 }*/
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000393
394 reference operator*() const {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000395 if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>())
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000396 return reference(ND, SingleDeclIndex);
397
Douglas Gregord490f952009-12-06 21:27:58 +0000398 return *DeclOrIterator.get<const DeclIndexPair*>();
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000399 }
400
401 pointer operator->() const {
402 return pointer(**this);
403 }
404
405 friend bool operator==(const iterator &X, const iterator &Y) {
Douglas Gregord490f952009-12-06 21:27:58 +0000406 return X.DeclOrIterator.getOpaqueValue()
407 == Y.DeclOrIterator.getOpaqueValue() &&
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000408 X.SingleDeclIndex == Y.SingleDeclIndex;
409 }
410
411 friend bool operator!=(const iterator &X, const iterator &Y) {
Douglas Gregord490f952009-12-06 21:27:58 +0000412 return !(X == Y);
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000413 }
414};
415
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000416ResultBuilder::ShadowMapEntry::iterator
417ResultBuilder::ShadowMapEntry::begin() const {
418 if (DeclOrVector.isNull())
419 return iterator();
420
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000421 if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>())
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000422 return iterator(ND, SingleDeclIndex);
423
424 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin());
425}
426
427ResultBuilder::ShadowMapEntry::iterator
428ResultBuilder::ShadowMapEntry::end() const {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000429 if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull())
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000430 return iterator();
431
432 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end());
433}
434
Douglas Gregor456c4a12009-09-21 20:12:40 +0000435/// \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.
448static NestedNameSpecifier *
449getRequiredQualification(ASTContext &Context,
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000450 const DeclContext *CurContext,
451 const DeclContext *TargetContext) {
452 SmallVector<const DeclContext *, 4> TargetParents;
Douglas Gregor456c4a12009-09-21 20:12:40 +0000453
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000454 for (const DeclContext *CommonAncestor = TargetContext;
Douglas Gregor456c4a12009-09-21 20:12:40 +0000455 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 = 0;
465 while (!TargetParents.empty()) {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000466 const DeclContext *Parent = TargetParents.back();
Douglas Gregor456c4a12009-09-21 20:12:40 +0000467 TargetParents.pop_back();
468
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000469 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) {
Douglas Gregorfb629412010-08-23 21:17:50 +0000470 if (!Namespace->getIdentifier())
471 continue;
472
Douglas Gregor456c4a12009-09-21 20:12:40 +0000473 Result = NestedNameSpecifier::Create(Context, Result, Namespace);
Douglas Gregorfb629412010-08-23 21:17:50 +0000474 }
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000475 else if (const TagDecl *TD = dyn_cast<TagDecl>(Parent))
Douglas Gregor456c4a12009-09-21 20:12:40 +0000476 Result = NestedNameSpecifier::Create(Context, Result,
477 false,
478 Context.getTypeDeclType(TD).getTypePtr());
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000479 }
Douglas Gregor456c4a12009-09-21 20:12:40 +0000480 return Result;
481}
482
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000483bool ResultBuilder::isInterestingDecl(const NamedDecl *ND,
Douglas Gregor45bcd432010-01-14 03:21:49 +0000484 bool &AsNestedNameSpecifier) const {
485 AsNestedNameSpecifier = false;
486
Douglas Gregore495b7f2010-01-14 00:20:49 +0000487 ND = ND->getUnderlyingDecl();
488 unsigned IDNS = ND->getIdentifierNamespace();
Douglas Gregorf52cede2009-10-09 22:16:47 +0000489
490 // Skip unnamed entities.
Douglas Gregore495b7f2010-01-14 00:20:49 +0000491 if (!ND->getDeclName())
492 return false;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000493
494 // Friend declarations and declarations introduced due to friends are never
495 // added as results.
John McCall92b7f702010-03-11 07:50:04 +0000496 if (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend))
Douglas Gregore495b7f2010-01-14 00:20:49 +0000497 return false;
498
Douglas Gregor76282942009-12-11 17:31:05 +0000499 // Class template (partial) specializations are never added as results.
Douglas Gregore495b7f2010-01-14 00:20:49 +0000500 if (isa<ClassTemplateSpecializationDecl>(ND) ||
501 isa<ClassTemplatePartialSpecializationDecl>(ND))
502 return false;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000503
Douglas Gregor76282942009-12-11 17:31:05 +0000504 // Using declarations themselves are never added as results.
Douglas Gregore495b7f2010-01-14 00:20:49 +0000505 if (isa<UsingDecl>(ND))
506 return false;
507
508 // Some declarations have reserved names that we don't want to ever show.
509 if (const IdentifierInfo *Id = ND->getIdentifier()) {
Douglas Gregor86d9a522009-09-21 16:56:56 +0000510 // __va_list_tag is a freak of nature. Find it and skip it.
511 if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list"))
Douglas Gregore495b7f2010-01-14 00:20:49 +0000512 return false;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000513
Douglas Gregorf52cede2009-10-09 22:16:47 +0000514 // Filter out names reserved for the implementation (C99 7.1.3,
Douglas Gregor797efb52010-07-14 17:44:04 +0000515 // C++ [lib.global.names]) if they come from a system header.
Daniel Dunbare013d682009-10-18 20:26:12 +0000516 //
517 // FIXME: Add predicate for this.
Douglas Gregorf52cede2009-10-09 22:16:47 +0000518 if (Id->getLength() >= 2) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000519 const char *Name = Id->getNameStart();
Douglas Gregorf52cede2009-10-09 22:16:47 +0000520 if (Name[0] == '_' &&
Douglas Gregor797efb52010-07-14 17:44:04 +0000521 (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')) &&
522 (ND->getLocation().isInvalid() ||
523 SemaRef.SourceMgr.isInSystemHeader(
524 SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))))
Douglas Gregore495b7f2010-01-14 00:20:49 +0000525 return false;
Douglas Gregorf52cede2009-10-09 22:16:47 +0000526 }
Douglas Gregor86d9a522009-09-21 16:56:56 +0000527 }
Douglas Gregor9b0ba872010-11-09 03:59:40 +0000528
Douglas Gregora5fb7c32010-08-16 23:05:20 +0000529 if (Filter == &ResultBuilder::IsNestedNameSpecifier ||
530 ((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
531 Filter != &ResultBuilder::IsNamespace &&
Douglas Gregor52779fb2010-09-23 23:01:17 +0000532 Filter != &ResultBuilder::IsNamespaceOrAlias &&
533 Filter != 0))
Douglas Gregora5fb7c32010-08-16 23:05:20 +0000534 AsNestedNameSpecifier = true;
535
Douglas Gregor86d9a522009-09-21 16:56:56 +0000536 // Filter out any unwanted results.
Douglas Gregor45bcd432010-01-14 03:21:49 +0000537 if (Filter && !(this->*Filter)(ND)) {
538 // Check whether it is interesting as a nested-name-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +0000539 if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus &&
Douglas Gregor45bcd432010-01-14 03:21:49 +0000540 IsNestedNameSpecifier(ND) &&
541 (Filter != &ResultBuilder::IsMember ||
542 (isa<CXXRecordDecl>(ND) &&
543 cast<CXXRecordDecl>(ND)->isInjectedClassName()))) {
544 AsNestedNameSpecifier = true;
545 return true;
546 }
547
Douglas Gregore495b7f2010-01-14 00:20:49 +0000548 return false;
Douglas Gregora5fb7c32010-08-16 23:05:20 +0000549 }
Douglas Gregore495b7f2010-01-14 00:20:49 +0000550 // ... then it must be interesting!
551 return true;
552}
553
Douglas Gregor6660d842010-01-14 00:41:07 +0000554bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext,
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000555 const NamedDecl *Hiding) {
Douglas Gregor6660d842010-01-14 00:41:07 +0000556 // In C, there is no way to refer to a hidden name.
557 // FIXME: This isn't true; we can find a tag name hidden by an ordinary
558 // name if we introduce the tag type.
David Blaikie4e4d0842012-03-11 07:00:24 +0000559 if (!SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor6660d842010-01-14 00:41:07 +0000560 return true;
561
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000562 const DeclContext *HiddenCtx =
563 R.Declaration->getDeclContext()->getRedeclContext();
Douglas Gregor6660d842010-01-14 00:41:07 +0000564
565 // There is no way to qualify a name declared in a function or method.
566 if (HiddenCtx->isFunctionOrMethod())
567 return true;
568
Sebastian Redl7a126a42010-08-31 00:36:30 +0000569 if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext())
Douglas Gregor6660d842010-01-14 00:41:07 +0000570 return true;
571
572 // We can refer to the result with the appropriate qualification. Do it.
573 R.Hidden = true;
574 R.QualifierIsInformative = false;
575
576 if (!R.Qualifier)
577 R.Qualifier = getRequiredQualification(SemaRef.Context,
578 CurContext,
579 R.Declaration->getDeclContext());
580 return false;
581}
582
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000583/// \brief A simplified classification of types used to determine whether two
584/// types are "similar enough" when adjusting priorities.
Douglas Gregor1827e102010-08-16 16:18:59 +0000585SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000586 switch (T->getTypeClass()) {
587 case Type::Builtin:
588 switch (cast<BuiltinType>(T)->getKind()) {
589 case BuiltinType::Void:
590 return STC_Void;
591
592 case BuiltinType::NullPtr:
593 return STC_Pointer;
594
595 case BuiltinType::Overload:
596 case BuiltinType::Dependent:
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000597 return STC_Other;
598
599 case BuiltinType::ObjCId:
600 case BuiltinType::ObjCClass:
601 case BuiltinType::ObjCSel:
602 return STC_ObjectiveC;
603
604 default:
605 return STC_Arithmetic;
606 }
David Blaikie7530c032012-01-17 06:56:22 +0000607
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000608 case Type::Complex:
609 return STC_Arithmetic;
610
611 case Type::Pointer:
612 return STC_Pointer;
613
614 case Type::BlockPointer:
615 return STC_Block;
616
617 case Type::LValueReference:
618 case Type::RValueReference:
619 return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType());
620
621 case Type::ConstantArray:
622 case Type::IncompleteArray:
623 case Type::VariableArray:
624 case Type::DependentSizedArray:
625 return STC_Array;
626
627 case Type::DependentSizedExtVector:
628 case Type::Vector:
629 case Type::ExtVector:
630 return STC_Arithmetic;
631
632 case Type::FunctionProto:
633 case Type::FunctionNoProto:
634 return STC_Function;
635
636 case Type::Record:
637 return STC_Record;
638
639 case Type::Enum:
640 return STC_Arithmetic;
641
642 case Type::ObjCObject:
643 case Type::ObjCInterface:
644 case Type::ObjCObjectPointer:
645 return STC_ObjectiveC;
646
647 default:
648 return STC_Other;
649 }
650}
651
652/// \brief Get the type that a given expression will have if this declaration
653/// is used as an expression in its "typical" code-completion form.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000654QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000655 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
656
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000657 if (const TypeDecl *Type = dyn_cast<TypeDecl>(ND))
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000658 return C.getTypeDeclType(Type);
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000659 if (const ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND))
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000660 return C.getObjCInterfaceType(Iface);
661
662 QualType T;
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000663 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND))
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000664 T = Function->getCallResultType();
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000665 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000666 T = Method->getSendResultType();
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000667 else if (const FunctionTemplateDecl *FunTmpl =
668 dyn_cast<FunctionTemplateDecl>(ND))
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000669 T = FunTmpl->getTemplatedDecl()->getCallResultType();
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000670 else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND))
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000671 T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext()));
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000672 else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND))
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000673 T = Property->getType();
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000674 else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND))
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000675 T = Value->getType();
676 else
677 return QualType();
Douglas Gregor3e64d562011-04-14 20:33:34 +0000678
679 // Dig through references, function pointers, and block pointers to
680 // get down to the likely type of an expression when the entity is
681 // used.
682 do {
683 if (const ReferenceType *Ref = T->getAs<ReferenceType>()) {
684 T = Ref->getPointeeType();
685 continue;
686 }
687
688 if (const PointerType *Pointer = T->getAs<PointerType>()) {
689 if (Pointer->getPointeeType()->isFunctionType()) {
690 T = Pointer->getPointeeType();
691 continue;
692 }
693
694 break;
695 }
696
697 if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) {
698 T = Block->getPointeeType();
699 continue;
700 }
701
702 if (const FunctionType *Function = T->getAs<FunctionType>()) {
703 T = Function->getResultType();
704 continue;
705 }
706
707 break;
708 } while (true);
709
710 return T;
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000711}
712
Douglas Gregord1f09b42013-01-31 04:52:16 +0000713unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) {
714 if (!ND)
715 return CCP_Unlikely;
716
717 // Context-based decisions.
718 const DeclContext *DC = ND->getDeclContext()->getRedeclContext();
719 if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC)) {
720 // _cmd is relatively rare
721 if (const ImplicitParamDecl *ImplicitParam =
722 dyn_cast<ImplicitParamDecl>(ND))
723 if (ImplicitParam->getIdentifier() &&
724 ImplicitParam->getIdentifier()->isStr("_cmd"))
725 return CCP_ObjC_cmd;
726
727 return CCP_LocalDeclaration;
728 }
729 if (DC->isRecord() || isa<ObjCContainerDecl>(DC))
730 return CCP_MemberDeclaration;
731
732 // Content-based decisions.
733 if (isa<EnumConstantDecl>(ND))
734 return CCP_Constant;
735
Douglas Gregor626799b2013-01-31 05:03:46 +0000736 // Use CCP_Type for type declarations unless we're in a statement, Objective-C
737 // message receiver, or parenthesized expression context. There, it's as
738 // likely that the user will want to write a type as other declarations.
739 if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) &&
740 !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement ||
741 CompletionContext.getKind()
742 == CodeCompletionContext::CCC_ObjCMessageReceiver ||
743 CompletionContext.getKind()
744 == CodeCompletionContext::CCC_ParenthesizedExpression))
Douglas Gregord1f09b42013-01-31 04:52:16 +0000745 return CCP_Type;
746
747 return CCP_Declaration;
748}
749
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000750void ResultBuilder::AdjustResultPriorityForDecl(Result &R) {
751 // If this is an Objective-C method declaration whose selector matches our
752 // preferred selector, give it a priority boost.
753 if (!PreferredSelector.isNull())
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000754 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration))
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000755 if (PreferredSelector == Method->getSelector())
756 R.Priority += CCD_SelectorMatch;
Douglas Gregor08f43cd2010-09-20 23:11:55 +0000757
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000758 // If we have a preferred type, adjust the priority for results with exactly-
759 // matching or nearly-matching types.
760 if (!PreferredType.isNull()) {
761 QualType T = getDeclUsageType(SemaRef.Context, R.Declaration);
762 if (!T.isNull()) {
763 CanQualType TC = SemaRef.Context.getCanonicalType(T);
764 // Check for exactly-matching types (modulo qualifiers).
765 if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC))
766 R.Priority /= CCF_ExactTypeMatch;
767 // Check for nearly-matching types, based on classification of each.
768 else if ((getSimplifiedTypeClass(PreferredType)
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000769 == getSimplifiedTypeClass(TC)) &&
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000770 !(PreferredType->isEnumeralType() && TC->isEnumeralType()))
771 R.Priority /= CCF_SimilarTypeMatch;
772 }
773 }
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000774}
775
Douglas Gregor6f942b22010-09-21 16:06:22 +0000776void ResultBuilder::MaybeAddConstructorResults(Result R) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000777 if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration ||
Douglas Gregor6f942b22010-09-21 16:06:22 +0000778 !CompletionContext.wantConstructorResults())
779 return;
780
781 ASTContext &Context = SemaRef.Context;
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000782 const NamedDecl *D = R.Declaration;
783 const CXXRecordDecl *Record = 0;
784 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D))
Douglas Gregor6f942b22010-09-21 16:06:22 +0000785 Record = ClassTemplate->getTemplatedDecl();
786 else if ((Record = dyn_cast<CXXRecordDecl>(D))) {
787 // Skip specializations and partial specializations.
788 if (isa<ClassTemplateSpecializationDecl>(Record))
789 return;
790 } else {
791 // There are no constructors here.
792 return;
793 }
794
795 Record = Record->getDefinition();
796 if (!Record)
797 return;
798
799
800 QualType RecordTy = Context.getTypeDeclType(Record);
801 DeclarationName ConstructorName
802 = Context.DeclarationNames.getCXXConstructorName(
803 Context.getCanonicalType(RecordTy));
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000804 DeclContext::lookup_const_result Ctors = Record->lookup(ConstructorName);
805 for (DeclContext::lookup_const_iterator I = Ctors.begin(),
806 E = Ctors.end();
807 I != E; ++I) {
David Blaikie3bc93e32012-12-19 00:45:41 +0000808 R.Declaration = *I;
Douglas Gregor6f942b22010-09-21 16:06:22 +0000809 R.CursorKind = getCursorKindForDecl(R.Declaration);
810 Results.push_back(R);
811 }
812}
813
Douglas Gregore495b7f2010-01-14 00:20:49 +0000814void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) {
815 assert(!ShadowMaps.empty() && "Must enter into a results scope");
816
817 if (R.Kind != Result::RK_Declaration) {
818 // For non-declaration results, just add the result.
819 Results.push_back(R);
820 return;
821 }
822
823 // Look through using declarations.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000824 if (const UsingShadowDecl *Using =
825 dyn_cast<UsingShadowDecl>(R.Declaration)) {
Douglas Gregord1f09b42013-01-31 04:52:16 +0000826 MaybeAddResult(Result(Using->getTargetDecl(),
827 getBasePriority(Using->getTargetDecl()),
828 R.Qualifier),
829 CurContext);
Douglas Gregore495b7f2010-01-14 00:20:49 +0000830 return;
831 }
832
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000833 const Decl *CanonDecl = R.Declaration->getCanonicalDecl();
Douglas Gregore495b7f2010-01-14 00:20:49 +0000834 unsigned IDNS = CanonDecl->getIdentifierNamespace();
835
Douglas Gregor45bcd432010-01-14 03:21:49 +0000836 bool AsNestedNameSpecifier = false;
837 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
Douglas Gregore495b7f2010-01-14 00:20:49 +0000838 return;
839
Douglas Gregor6f942b22010-09-21 16:06:22 +0000840 // C++ constructors are never found by name lookup.
841 if (isa<CXXConstructorDecl>(R.Declaration))
842 return;
843
Douglas Gregor86d9a522009-09-21 16:56:56 +0000844 ShadowMap &SMap = ShadowMaps.back();
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000845 ShadowMapEntry::iterator I, IEnd;
846 ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName());
847 if (NamePos != SMap.end()) {
848 I = NamePos->second.begin();
849 IEnd = NamePos->second.end();
850 }
851
852 for (; I != IEnd; ++I) {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000853 const NamedDecl *ND = I->first;
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000854 unsigned Index = I->second;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000855 if (ND->getCanonicalDecl() == CanonDecl) {
856 // This is a redeclaration. Always pick the newer declaration.
Douglas Gregor86d9a522009-09-21 16:56:56 +0000857 Results[Index].Declaration = R.Declaration;
858
Douglas Gregor86d9a522009-09-21 16:56:56 +0000859 // We're done.
860 return;
861 }
862 }
863
864 // This is a new declaration in this scope. However, check whether this
865 // declaration name is hidden by a similarly-named declaration in an outer
866 // scope.
867 std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end();
868 --SMEnd;
869 for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) {
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000870 ShadowMapEntry::iterator I, IEnd;
871 ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName());
872 if (NamePos != SM->end()) {
873 I = NamePos->second.begin();
874 IEnd = NamePos->second.end();
875 }
876 for (; I != IEnd; ++I) {
Douglas Gregor86d9a522009-09-21 16:56:56 +0000877 // A tag declaration does not hide a non-tag declaration.
John McCall0d6b1642010-04-23 18:46:30 +0000878 if (I->first->hasTagIdentifierNamespace() &&
Douglas Gregor86d9a522009-09-21 16:56:56 +0000879 (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary |
880 Decl::IDNS_ObjCProtocol)))
881 continue;
882
883 // Protocols are in distinct namespaces from everything else.
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000884 if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol)
Douglas Gregor86d9a522009-09-21 16:56:56 +0000885 || (IDNS & Decl::IDNS_ObjCProtocol)) &&
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000886 I->first->getIdentifierNamespace() != IDNS)
Douglas Gregor86d9a522009-09-21 16:56:56 +0000887 continue;
888
889 // The newly-added result is hidden by an entry in the shadow map.
Douglas Gregor6660d842010-01-14 00:41:07 +0000890 if (CheckHiddenResult(R, CurContext, I->first))
Douglas Gregor86d9a522009-09-21 16:56:56 +0000891 return;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000892
893 break;
894 }
895 }
896
897 // Make sure that any given declaration only shows up in the result set once.
898 if (!AllDeclsFound.insert(CanonDecl))
899 return;
Douglas Gregor265f7492010-08-27 15:29:55 +0000900
Douglas Gregoreb5758b2009-09-23 22:26:46 +0000901 // If the filter is for nested-name-specifiers, then this result starts a
902 // nested-name-specifier.
Douglas Gregor12e13132010-05-26 22:00:08 +0000903 if (AsNestedNameSpecifier) {
Douglas Gregoreb5758b2009-09-23 22:26:46 +0000904 R.StartsNestedNameSpecifier = true;
Douglas Gregor12e13132010-05-26 22:00:08 +0000905 R.Priority = CCP_NestedNameSpecifier;
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000906 } else
907 AdjustResultPriorityForDecl(R);
Douglas Gregor265f7492010-08-27 15:29:55 +0000908
Douglas Gregor0563c262009-09-22 23:15:58 +0000909 // If this result is supposed to have an informative qualifier, add one.
Douglas Gregoreb5758b2009-09-23 22:26:46 +0000910 if (R.QualifierIsInformative && !R.Qualifier &&
911 !R.StartsNestedNameSpecifier) {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000912 const DeclContext *Ctx = R.Declaration->getDeclContext();
913 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
Douglas Gregor0563c262009-09-22 23:15:58 +0000914 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000915 else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
Douglas Gregor0563c262009-09-22 23:15:58 +0000916 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
917 SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
918 else
919 R.QualifierIsInformative = false;
920 }
Douglas Gregoreb5758b2009-09-23 22:26:46 +0000921
Douglas Gregor86d9a522009-09-21 16:56:56 +0000922 // Insert this result into the set of results and into the current shadow
923 // map.
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000924 SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size());
Douglas Gregor86d9a522009-09-21 16:56:56 +0000925 Results.push_back(R);
Douglas Gregor6f942b22010-09-21 16:06:22 +0000926
927 if (!AsNestedNameSpecifier)
928 MaybeAddConstructorResults(R);
Douglas Gregor86d9a522009-09-21 16:56:56 +0000929}
930
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000931void ResultBuilder::AddResult(Result R, DeclContext *CurContext,
Douglas Gregor0cc84042010-01-14 15:47:35 +0000932 NamedDecl *Hiding, bool InBaseClass = false) {
Douglas Gregora4477812010-01-14 16:01:26 +0000933 if (R.Kind != Result::RK_Declaration) {
934 // For non-declaration results, just add the result.
935 Results.push_back(R);
936 return;
937 }
938
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000939 // Look through using declarations.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000940 if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
Douglas Gregord1f09b42013-01-31 04:52:16 +0000941 AddResult(Result(Using->getTargetDecl(),
942 getBasePriority(Using->getTargetDecl()),
943 R.Qualifier),
944 CurContext, Hiding);
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000945 return;
946 }
947
Douglas Gregor45bcd432010-01-14 03:21:49 +0000948 bool AsNestedNameSpecifier = false;
949 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000950 return;
951
Douglas Gregor6f942b22010-09-21 16:06:22 +0000952 // C++ constructors are never found by name lookup.
953 if (isa<CXXConstructorDecl>(R.Declaration))
954 return;
955
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000956 if (Hiding && CheckHiddenResult(R, CurContext, Hiding))
957 return;
Nick Lewycky173a37a2012-04-03 21:44:08 +0000958
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000959 // Make sure that any given declaration only shows up in the result set once.
960 if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()))
961 return;
962
963 // If the filter is for nested-name-specifiers, then this result starts a
964 // nested-name-specifier.
Douglas Gregor12e13132010-05-26 22:00:08 +0000965 if (AsNestedNameSpecifier) {
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000966 R.StartsNestedNameSpecifier = true;
Douglas Gregor12e13132010-05-26 22:00:08 +0000967 R.Priority = CCP_NestedNameSpecifier;
968 }
Douglas Gregor0cc84042010-01-14 15:47:35 +0000969 else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass &&
970 isa<CXXRecordDecl>(R.Declaration->getDeclContext()
Sebastian Redl7a126a42010-08-31 00:36:30 +0000971 ->getRedeclContext()))
Douglas Gregor0cc84042010-01-14 15:47:35 +0000972 R.QualifierIsInformative = true;
973
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000974 // If this result is supposed to have an informative qualifier, add one.
975 if (R.QualifierIsInformative && !R.Qualifier &&
976 !R.StartsNestedNameSpecifier) {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000977 const DeclContext *Ctx = R.Declaration->getDeclContext();
978 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000979 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000980 else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000981 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
Douglas Gregor45bcd432010-01-14 03:21:49 +0000982 SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000983 else
984 R.QualifierIsInformative = false;
985 }
986
Douglas Gregor12e13132010-05-26 22:00:08 +0000987 // Adjust the priority if this result comes from a base class.
988 if (InBaseClass)
989 R.Priority += CCD_InBaseClass;
990
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000991 AdjustResultPriorityForDecl(R);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000992
Douglas Gregor3cdee122010-08-26 16:36:48 +0000993 if (HasObjectTypeQualifiers)
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000994 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration))
Douglas Gregor3cdee122010-08-26 16:36:48 +0000995 if (Method->isInstance()) {
996 Qualifiers MethodQuals
997 = Qualifiers::fromCVRMask(Method->getTypeQualifiers());
998 if (ObjectTypeQualifiers == MethodQuals)
999 R.Priority += CCD_ObjectQualifierMatch;
1000 else if (ObjectTypeQualifiers - MethodQuals) {
1001 // The method cannot be invoked, because doing so would drop
1002 // qualifiers.
1003 return;
1004 }
1005 }
1006
Douglas Gregor1ca6ae82010-01-14 01:09:38 +00001007 // Insert this result into the set of results.
1008 Results.push_back(R);
Douglas Gregor6f942b22010-09-21 16:06:22 +00001009
1010 if (!AsNestedNameSpecifier)
1011 MaybeAddConstructorResults(R);
Douglas Gregor1ca6ae82010-01-14 01:09:38 +00001012}
1013
Douglas Gregora4477812010-01-14 16:01:26 +00001014void ResultBuilder::AddResult(Result R) {
1015 assert(R.Kind != Result::RK_Declaration &&
1016 "Declaration results need more context");
1017 Results.push_back(R);
1018}
1019
Douglas Gregor86d9a522009-09-21 16:56:56 +00001020/// \brief Enter into a new scope.
1021void ResultBuilder::EnterNewScope() {
1022 ShadowMaps.push_back(ShadowMap());
1023}
1024
1025/// \brief Exit from the current scope.
1026void ResultBuilder::ExitScope() {
Douglas Gregorfbcb5d62009-12-06 20:23:50 +00001027 for (ShadowMap::iterator E = ShadowMaps.back().begin(),
1028 EEnd = ShadowMaps.back().end();
1029 E != EEnd;
1030 ++E)
1031 E->second.Destroy();
1032
Douglas Gregor86d9a522009-09-21 16:56:56 +00001033 ShadowMaps.pop_back();
1034}
1035
Douglas Gregor791215b2009-09-21 20:51:25 +00001036/// \brief Determines whether this given declaration will be found by
1037/// ordinary name lookup.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001038bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const {
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001039 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
1040
Douglas Gregor791215b2009-09-21 20:51:25 +00001041 unsigned IDNS = Decl::IDNS_Ordinary;
David Blaikie4e4d0842012-03-11 07:00:24 +00001042 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor9b30b262010-06-15 20:26:51 +00001043 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member;
David Blaikie4e4d0842012-03-11 07:00:24 +00001044 else if (SemaRef.getLangOpts().ObjC1) {
Douglas Gregorca45da02010-11-02 20:36:02 +00001045 if (isa<ObjCIvarDecl>(ND))
1046 return true;
Douglas Gregorca45da02010-11-02 20:36:02 +00001047 }
1048
Douglas Gregor791215b2009-09-21 20:51:25 +00001049 return ND->getIdentifierNamespace() & IDNS;
1050}
1051
Douglas Gregor01dfea02010-01-10 23:08:15 +00001052/// \brief Determines whether this given declaration will be found by
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001053/// ordinary name lookup but is not a type name.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001054bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const {
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001055 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
1056 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
1057 return false;
1058
1059 unsigned IDNS = Decl::IDNS_Ordinary;
David Blaikie4e4d0842012-03-11 07:00:24 +00001060 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor9b30b262010-06-15 20:26:51 +00001061 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member;
David Blaikie4e4d0842012-03-11 07:00:24 +00001062 else if (SemaRef.getLangOpts().ObjC1) {
Douglas Gregorca45da02010-11-02 20:36:02 +00001063 if (isa<ObjCIvarDecl>(ND))
1064 return true;
Douglas Gregorca45da02010-11-02 20:36:02 +00001065 }
1066
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001067 return ND->getIdentifierNamespace() & IDNS;
1068}
1069
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001070bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const {
Douglas Gregorf9578432010-07-28 21:50:18 +00001071 if (!IsOrdinaryNonTypeName(ND))
1072 return 0;
1073
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001074 if (const ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl()))
Douglas Gregorf9578432010-07-28 21:50:18 +00001075 if (VD->getType()->isIntegralOrEnumerationType())
1076 return true;
1077
1078 return false;
1079}
1080
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001081/// \brief Determines whether this given declaration will be found by
Douglas Gregor01dfea02010-01-10 23:08:15 +00001082/// ordinary name lookup.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001083bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const {
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001084 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
1085
Douglas Gregor01dfea02010-01-10 23:08:15 +00001086 unsigned IDNS = Decl::IDNS_Ordinary;
David Blaikie4e4d0842012-03-11 07:00:24 +00001087 if (SemaRef.getLangOpts().CPlusPlus)
John McCall0d6b1642010-04-23 18:46:30 +00001088 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001089
1090 return (ND->getIdentifierNamespace() & IDNS) &&
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001091 !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) &&
1092 !isa<ObjCPropertyDecl>(ND);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001093}
1094
Douglas Gregor86d9a522009-09-21 16:56:56 +00001095/// \brief Determines whether the given declaration is suitable as the
1096/// start of a C++ nested-name-specifier, e.g., a class or namespace.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001097bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const {
Douglas Gregor86d9a522009-09-21 16:56:56 +00001098 // Allow us to find class templates, too.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001099 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
Douglas Gregor86d9a522009-09-21 16:56:56 +00001100 ND = ClassTemplate->getTemplatedDecl();
1101
1102 return SemaRef.isAcceptableNestedNameSpecifier(ND);
1103}
1104
1105/// \brief Determines whether the given declaration is an enumeration.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001106bool ResultBuilder::IsEnum(const NamedDecl *ND) const {
Douglas Gregor86d9a522009-09-21 16:56:56 +00001107 return isa<EnumDecl>(ND);
1108}
1109
1110/// \brief Determines whether the given declaration is a class or struct.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001111bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const {
Douglas Gregor86d9a522009-09-21 16:56:56 +00001112 // Allow us to find class templates, too.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001113 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
Douglas Gregor86d9a522009-09-21 16:56:56 +00001114 ND = ClassTemplate->getTemplatedDecl();
Joao Matos6666ed42012-08-31 18:45:21 +00001115
1116 // For purposes of this check, interfaces match too.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001117 if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND))
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001118 return RD->getTagKind() == TTK_Class ||
Joao Matos6666ed42012-08-31 18:45:21 +00001119 RD->getTagKind() == TTK_Struct ||
1120 RD->getTagKind() == TTK_Interface;
Douglas Gregor86d9a522009-09-21 16:56:56 +00001121
1122 return false;
1123}
1124
1125/// \brief Determines whether the given declaration is a union.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001126bool ResultBuilder::IsUnion(const NamedDecl *ND) const {
Douglas Gregor86d9a522009-09-21 16:56:56 +00001127 // Allow us to find class templates, too.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001128 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
Douglas Gregor86d9a522009-09-21 16:56:56 +00001129 ND = ClassTemplate->getTemplatedDecl();
1130
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001131 if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND))
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001132 return RD->getTagKind() == TTK_Union;
Douglas Gregor86d9a522009-09-21 16:56:56 +00001133
1134 return false;
1135}
1136
1137/// \brief Determines whether the given declaration is a namespace.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001138bool ResultBuilder::IsNamespace(const NamedDecl *ND) const {
Douglas Gregor86d9a522009-09-21 16:56:56 +00001139 return isa<NamespaceDecl>(ND);
1140}
1141
1142/// \brief Determines whether the given declaration is a namespace or
1143/// namespace alias.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001144bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const {
Douglas Gregor86d9a522009-09-21 16:56:56 +00001145 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
1146}
1147
Douglas Gregor76282942009-12-11 17:31:05 +00001148/// \brief Determines whether the given declaration is a type.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001149bool ResultBuilder::IsType(const NamedDecl *ND) const {
1150 if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND))
Douglas Gregord32b0222010-08-24 01:06:58 +00001151 ND = Using->getTargetDecl();
1152
1153 return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND);
Douglas Gregor86d9a522009-09-21 16:56:56 +00001154}
1155
Douglas Gregor76282942009-12-11 17:31:05 +00001156/// \brief Determines which members of a class should be visible via
1157/// "." or "->". Only value declarations, nested name specifiers, and
1158/// using declarations thereof should show up.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001159bool ResultBuilder::IsMember(const NamedDecl *ND) const {
1160 if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND))
Douglas Gregor76282942009-12-11 17:31:05 +00001161 ND = Using->getTargetDecl();
1162
Douglas Gregorce821962009-12-11 18:14:22 +00001163 return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
1164 isa<ObjCPropertyDecl>(ND);
Douglas Gregoreb5758b2009-09-23 22:26:46 +00001165}
1166
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001167static bool isObjCReceiverType(ASTContext &C, QualType T) {
1168 T = C.getCanonicalType(T);
1169 switch (T->getTypeClass()) {
1170 case Type::ObjCObject:
1171 case Type::ObjCInterface:
1172 case Type::ObjCObjectPointer:
1173 return true;
1174
1175 case Type::Builtin:
1176 switch (cast<BuiltinType>(T)->getKind()) {
1177 case BuiltinType::ObjCId:
1178 case BuiltinType::ObjCClass:
1179 case BuiltinType::ObjCSel:
1180 return true;
1181
1182 default:
1183 break;
1184 }
1185 return false;
1186
1187 default:
1188 break;
1189 }
1190
David Blaikie4e4d0842012-03-11 07:00:24 +00001191 if (!C.getLangOpts().CPlusPlus)
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001192 return false;
1193
1194 // FIXME: We could perform more analysis here to determine whether a
1195 // particular class type has any conversions to Objective-C types. For now,
1196 // just accept all class types.
1197 return T->isDependentType() || T->isRecordType();
1198}
1199
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001200bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const {
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001201 QualType T = getDeclUsageType(SemaRef.Context, ND);
1202 if (T.isNull())
1203 return false;
1204
1205 T = SemaRef.Context.getBaseElementType(T);
1206 return isObjCReceiverType(SemaRef.Context, T);
1207}
1208
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001209bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const {
Douglas Gregor81f3bff2012-02-15 15:34:24 +00001210 if (IsObjCMessageReceiver(ND))
1211 return true;
1212
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001213 const VarDecl *Var = dyn_cast<VarDecl>(ND);
Douglas Gregor81f3bff2012-02-15 15:34:24 +00001214 if (!Var)
1215 return false;
1216
1217 return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>();
1218}
1219
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001220bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001221 if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) ||
1222 (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND)))
Douglas Gregorfb629412010-08-23 21:17:50 +00001223 return false;
1224
1225 QualType T = getDeclUsageType(SemaRef.Context, ND);
1226 if (T.isNull())
1227 return false;
1228
1229 T = SemaRef.Context.getBaseElementType(T);
1230 return T->isObjCObjectType() || T->isObjCObjectPointerType() ||
1231 T->isObjCIdType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001232 (SemaRef.getLangOpts().CPlusPlus && T->isRecordType());
Douglas Gregorfb629412010-08-23 21:17:50 +00001233}
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001234
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001235bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const {
Douglas Gregor52779fb2010-09-23 23:01:17 +00001236 return false;
1237}
1238
James Dennettde23c7e2012-06-17 05:33:25 +00001239/// \brief Determines whether the given declaration is an Objective-C
Douglas Gregor80f4f4c2010-01-14 16:08:12 +00001240/// instance variable.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00001241bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const {
Douglas Gregor80f4f4c2010-01-14 16:08:12 +00001242 return isa<ObjCIvarDecl>(ND);
1243}
1244
Douglas Gregor1ca6ae82010-01-14 01:09:38 +00001245namespace {
1246 /// \brief Visible declaration consumer that adds a code-completion result
1247 /// for each visible declaration.
1248 class CodeCompletionDeclConsumer : public VisibleDeclConsumer {
1249 ResultBuilder &Results;
1250 DeclContext *CurContext;
1251
1252 public:
1253 CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext)
1254 : Results(Results), CurContext(CurContext) { }
1255
Erik Verbruggend1205962011-10-06 07:27:49 +00001256 virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
1257 bool InBaseClass) {
1258 bool Accessible = true;
Douglas Gregor17015ef2011-11-03 16:51:37 +00001259 if (Ctx)
1260 Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx);
1261
Douglas Gregord1f09b42013-01-31 04:52:16 +00001262 ResultBuilder::Result Result(ND, Results.getBasePriority(ND), 0, false,
1263 Accessible);
Erik Verbruggend1205962011-10-06 07:27:49 +00001264 Results.AddResult(Result, CurContext, Hiding, InBaseClass);
Douglas Gregor1ca6ae82010-01-14 01:09:38 +00001265 }
1266 };
1267}
1268
Douglas Gregor86d9a522009-09-21 16:56:56 +00001269/// \brief Add type specifiers for the current language as keyword results.
Douglas Gregorbca403c2010-01-13 23:51:12 +00001270static void AddTypeSpecifierResults(const LangOptions &LangOpts,
Douglas Gregor86d9a522009-09-21 16:56:56 +00001271 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00001272 typedef CodeCompletionResult Result;
Douglas Gregor12e13132010-05-26 22:00:08 +00001273 Results.AddResult(Result("short", CCP_Type));
1274 Results.AddResult(Result("long", CCP_Type));
1275 Results.AddResult(Result("signed", CCP_Type));
1276 Results.AddResult(Result("unsigned", CCP_Type));
1277 Results.AddResult(Result("void", CCP_Type));
1278 Results.AddResult(Result("char", CCP_Type));
1279 Results.AddResult(Result("int", CCP_Type));
1280 Results.AddResult(Result("float", CCP_Type));
1281 Results.AddResult(Result("double", CCP_Type));
1282 Results.AddResult(Result("enum", CCP_Type));
1283 Results.AddResult(Result("struct", CCP_Type));
1284 Results.AddResult(Result("union", CCP_Type));
1285 Results.AddResult(Result("const", CCP_Type));
1286 Results.AddResult(Result("volatile", CCP_Type));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001287
Douglas Gregor86d9a522009-09-21 16:56:56 +00001288 if (LangOpts.C99) {
1289 // C99-specific
Douglas Gregor12e13132010-05-26 22:00:08 +00001290 Results.AddResult(Result("_Complex", CCP_Type));
1291 Results.AddResult(Result("_Imaginary", CCP_Type));
1292 Results.AddResult(Result("_Bool", CCP_Type));
1293 Results.AddResult(Result("restrict", CCP_Type));
Douglas Gregor86d9a522009-09-21 16:56:56 +00001294 }
1295
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001296 CodeCompletionBuilder Builder(Results.getAllocator(),
1297 Results.getCodeCompletionTUInfo());
Douglas Gregor86d9a522009-09-21 16:56:56 +00001298 if (LangOpts.CPlusPlus) {
1299 // C++-specific
Douglas Gregorb05496d2010-09-20 21:11:48 +00001300 Results.AddResult(Result("bool", CCP_Type +
1301 (LangOpts.ObjC1? CCD_bool_in_ObjC : 0)));
Douglas Gregor12e13132010-05-26 22:00:08 +00001302 Results.AddResult(Result("class", CCP_Type));
1303 Results.AddResult(Result("wchar_t", CCP_Type));
Douglas Gregor86d9a522009-09-21 16:56:56 +00001304
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001305 // typename qualified-id
Douglas Gregor218937c2011-02-01 19:23:04 +00001306 Builder.AddTypedTextChunk("typename");
1307 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1308 Builder.AddPlaceholderChunk("qualifier");
1309 Builder.AddTextChunk("::");
1310 Builder.AddPlaceholderChunk("name");
1311 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregord8e8a582010-05-25 21:41:55 +00001312
Richard Smith80ad52f2013-01-02 11:42:31 +00001313 if (LangOpts.CPlusPlus11) {
Douglas Gregor12e13132010-05-26 22:00:08 +00001314 Results.AddResult(Result("auto", CCP_Type));
1315 Results.AddResult(Result("char16_t", CCP_Type));
1316 Results.AddResult(Result("char32_t", CCP_Type));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001317
Douglas Gregor218937c2011-02-01 19:23:04 +00001318 Builder.AddTypedTextChunk("decltype");
1319 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1320 Builder.AddPlaceholderChunk("expression");
1321 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1322 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor86d9a522009-09-21 16:56:56 +00001323 }
1324 }
1325
1326 // GNU extensions
1327 if (LangOpts.GNUMode) {
1328 // FIXME: Enable when we actually support decimal floating point.
Douglas Gregora4477812010-01-14 16:01:26 +00001329 // Results.AddResult(Result("_Decimal32"));
1330 // Results.AddResult(Result("_Decimal64"));
1331 // Results.AddResult(Result("_Decimal128"));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001332
Douglas Gregor218937c2011-02-01 19:23:04 +00001333 Builder.AddTypedTextChunk("typeof");
1334 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1335 Builder.AddPlaceholderChunk("expression");
1336 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001337
Douglas Gregor218937c2011-02-01 19:23:04 +00001338 Builder.AddTypedTextChunk("typeof");
1339 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1340 Builder.AddPlaceholderChunk("type");
1341 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1342 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor86d9a522009-09-21 16:56:56 +00001343 }
1344}
1345
John McCallf312b1e2010-08-26 23:41:50 +00001346static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001347 const LangOptions &LangOpts,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001348 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00001349 typedef CodeCompletionResult Result;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001350 // Note: we don't suggest either "auto" or "register", because both
1351 // are pointless as storage specifiers. Elsewhere, we suggest "auto"
1352 // in C++0x as a type specifier.
Douglas Gregora4477812010-01-14 16:01:26 +00001353 Results.AddResult(Result("extern"));
1354 Results.AddResult(Result("static"));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001355}
1356
John McCallf312b1e2010-08-26 23:41:50 +00001357static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001358 const LangOptions &LangOpts,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001359 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00001360 typedef CodeCompletionResult Result;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001361 switch (CCC) {
John McCallf312b1e2010-08-26 23:41:50 +00001362 case Sema::PCC_Class:
1363 case Sema::PCC_MemberTemplate:
Douglas Gregor01dfea02010-01-10 23:08:15 +00001364 if (LangOpts.CPlusPlus) {
Douglas Gregora4477812010-01-14 16:01:26 +00001365 Results.AddResult(Result("explicit"));
1366 Results.AddResult(Result("friend"));
1367 Results.AddResult(Result("mutable"));
1368 Results.AddResult(Result("virtual"));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001369 }
1370 // Fall through
1371
John McCallf312b1e2010-08-26 23:41:50 +00001372 case Sema::PCC_ObjCInterface:
1373 case Sema::PCC_ObjCImplementation:
1374 case Sema::PCC_Namespace:
1375 case Sema::PCC_Template:
Douglas Gregor01dfea02010-01-10 23:08:15 +00001376 if (LangOpts.CPlusPlus || LangOpts.C99)
Douglas Gregora4477812010-01-14 16:01:26 +00001377 Results.AddResult(Result("inline"));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001378 break;
1379
John McCallf312b1e2010-08-26 23:41:50 +00001380 case Sema::PCC_ObjCInstanceVariableList:
1381 case Sema::PCC_Expression:
1382 case Sema::PCC_Statement:
1383 case Sema::PCC_ForInit:
1384 case Sema::PCC_Condition:
1385 case Sema::PCC_RecoveryInFunction:
1386 case Sema::PCC_Type:
Douglas Gregor02688102010-09-14 23:59:36 +00001387 case Sema::PCC_ParenthesizedExpression:
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001388 case Sema::PCC_LocalDeclarationSpecifiers:
Douglas Gregor01dfea02010-01-10 23:08:15 +00001389 break;
1390 }
1391}
1392
Douglas Gregorbca403c2010-01-13 23:51:12 +00001393static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt);
1394static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt);
1395static void AddObjCVisibilityResults(const LangOptions &LangOpts,
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001396 ResultBuilder &Results,
1397 bool NeedAt);
Douglas Gregorbca403c2010-01-13 23:51:12 +00001398static void AddObjCImplementationResults(const LangOptions &LangOpts,
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001399 ResultBuilder &Results,
1400 bool NeedAt);
Douglas Gregorbca403c2010-01-13 23:51:12 +00001401static void AddObjCInterfaceResults(const LangOptions &LangOpts,
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001402 ResultBuilder &Results,
1403 bool NeedAt);
Douglas Gregorbca403c2010-01-13 23:51:12 +00001404static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001405
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001406static void AddTypedefResult(ResultBuilder &Results) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001407 CodeCompletionBuilder Builder(Results.getAllocator(),
1408 Results.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00001409 Builder.AddTypedTextChunk("typedef");
1410 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1411 Builder.AddPlaceholderChunk("type");
1412 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1413 Builder.AddPlaceholderChunk("name");
1414 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001415}
1416
John McCallf312b1e2010-08-26 23:41:50 +00001417static bool WantTypesInContext(Sema::ParserCompletionContext CCC,
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001418 const LangOptions &LangOpts) {
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001419 switch (CCC) {
John McCallf312b1e2010-08-26 23:41:50 +00001420 case Sema::PCC_Namespace:
1421 case Sema::PCC_Class:
1422 case Sema::PCC_ObjCInstanceVariableList:
1423 case Sema::PCC_Template:
1424 case Sema::PCC_MemberTemplate:
1425 case Sema::PCC_Statement:
1426 case Sema::PCC_RecoveryInFunction:
1427 case Sema::PCC_Type:
Douglas Gregor02688102010-09-14 23:59:36 +00001428 case Sema::PCC_ParenthesizedExpression:
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001429 case Sema::PCC_LocalDeclarationSpecifiers:
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001430 return true;
1431
John McCallf312b1e2010-08-26 23:41:50 +00001432 case Sema::PCC_Expression:
1433 case Sema::PCC_Condition:
Douglas Gregor02688102010-09-14 23:59:36 +00001434 return LangOpts.CPlusPlus;
1435
1436 case Sema::PCC_ObjCInterface:
1437 case Sema::PCC_ObjCImplementation:
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001438 return false;
1439
John McCallf312b1e2010-08-26 23:41:50 +00001440 case Sema::PCC_ForInit:
Douglas Gregor02688102010-09-14 23:59:36 +00001441 return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99;
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001442 }
David Blaikie7530c032012-01-17 06:56:22 +00001443
1444 llvm_unreachable("Invalid ParserCompletionContext!");
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001445}
1446
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00001447static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context,
1448 const Preprocessor &PP) {
1449 PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP);
Douglas Gregor8ca72082011-10-18 21:20:17 +00001450 Policy.AnonymousTagLocations = false;
1451 Policy.SuppressStrongLifetime = true;
Douglas Gregor25270b62011-11-03 00:16:13 +00001452 Policy.SuppressUnwrittenScope = true;
Douglas Gregor8ca72082011-10-18 21:20:17 +00001453 return Policy;
1454}
1455
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00001456/// \brief Retrieve a printing policy suitable for code completion.
1457static PrintingPolicy getCompletionPrintingPolicy(Sema &S) {
1458 return getCompletionPrintingPolicy(S.Context, S.PP);
1459}
1460
Douglas Gregor8ca72082011-10-18 21:20:17 +00001461/// \brief Retrieve the string representation of the given type as a string
1462/// that has the appropriate lifetime for code completion.
1463///
1464/// This routine provides a fast path where we provide constant strings for
1465/// common type names.
1466static const char *GetCompletionTypeString(QualType T,
1467 ASTContext &Context,
1468 const PrintingPolicy &Policy,
1469 CodeCompletionAllocator &Allocator) {
1470 if (!T.getLocalQualifiers()) {
1471 // Built-in type names are constant strings.
1472 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T))
Argyrios Kyrtzidis27a00972012-05-05 04:20:28 +00001473 return BT->getNameAsCString(Policy);
Douglas Gregor8ca72082011-10-18 21:20:17 +00001474
1475 // Anonymous tag types are constant strings.
1476 if (const TagType *TagT = dyn_cast<TagType>(T))
1477 if (TagDecl *Tag = TagT->getDecl())
1478 if (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl()) {
1479 switch (Tag->getTagKind()) {
1480 case TTK_Struct: return "struct <anonymous>";
Joao Matos6666ed42012-08-31 18:45:21 +00001481 case TTK_Interface: return "__interface <anonymous>";
1482 case TTK_Class: return "class <anonymous>";
Douglas Gregor8ca72082011-10-18 21:20:17 +00001483 case TTK_Union: return "union <anonymous>";
1484 case TTK_Enum: return "enum <anonymous>";
1485 }
1486 }
1487 }
1488
1489 // Slow path: format the type as a string.
1490 std::string Result;
1491 T.getAsStringInternal(Result, Policy);
1492 return Allocator.CopyString(Result);
1493}
1494
Douglas Gregor81f3bff2012-02-15 15:34:24 +00001495/// \brief Add a completion for "this", if we're in a member function.
1496static void addThisCompletion(Sema &S, ResultBuilder &Results) {
1497 QualType ThisTy = S.getCurrentThisType();
1498 if (ThisTy.isNull())
1499 return;
1500
1501 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001502 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregor81f3bff2012-02-15 15:34:24 +00001503 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
1504 Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy,
1505 S.Context,
1506 Policy,
1507 Allocator));
1508 Builder.AddTypedTextChunk("this");
Joao Matos6666ed42012-08-31 18:45:21 +00001509 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
Douglas Gregor81f3bff2012-02-15 15:34:24 +00001510}
1511
Douglas Gregor01dfea02010-01-10 23:08:15 +00001512/// \brief Add language constructs that show up for "ordinary" names.
John McCallf312b1e2010-08-26 23:41:50 +00001513static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001514 Scope *S,
1515 Sema &SemaRef,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001516 ResultBuilder &Results) {
Douglas Gregor8ca72082011-10-18 21:20:17 +00001517 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001518 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregor8ca72082011-10-18 21:20:17 +00001519 PrintingPolicy Policy = getCompletionPrintingPolicy(SemaRef);
Douglas Gregor218937c2011-02-01 19:23:04 +00001520
John McCall0a2c5e22010-08-25 06:19:51 +00001521 typedef CodeCompletionResult Result;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001522 switch (CCC) {
John McCallf312b1e2010-08-26 23:41:50 +00001523 case Sema::PCC_Namespace:
David Blaikie4e4d0842012-03-11 07:00:24 +00001524 if (SemaRef.getLangOpts().CPlusPlus) {
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001525 if (Results.includeCodePatterns()) {
1526 // namespace <identifier> { declarations }
Douglas Gregor218937c2011-02-01 19:23:04 +00001527 Builder.AddTypedTextChunk("namespace");
1528 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1529 Builder.AddPlaceholderChunk("identifier");
1530 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1531 Builder.AddPlaceholderChunk("declarations");
1532 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1533 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1534 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001535 }
1536
Douglas Gregor01dfea02010-01-10 23:08:15 +00001537 // namespace identifier = identifier ;
Douglas Gregor218937c2011-02-01 19:23:04 +00001538 Builder.AddTypedTextChunk("namespace");
1539 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1540 Builder.AddPlaceholderChunk("name");
1541 Builder.AddChunk(CodeCompletionString::CK_Equal);
1542 Builder.AddPlaceholderChunk("namespace");
1543 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001544
1545 // Using directives
Douglas Gregor218937c2011-02-01 19:23:04 +00001546 Builder.AddTypedTextChunk("using");
1547 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1548 Builder.AddTextChunk("namespace");
1549 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1550 Builder.AddPlaceholderChunk("identifier");
1551 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001552
1553 // asm(string-literal)
Douglas Gregor218937c2011-02-01 19:23:04 +00001554 Builder.AddTypedTextChunk("asm");
1555 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1556 Builder.AddPlaceholderChunk("string-literal");
1557 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1558 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001559
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001560 if (Results.includeCodePatterns()) {
1561 // Explicit template instantiation
Douglas Gregor218937c2011-02-01 19:23:04 +00001562 Builder.AddTypedTextChunk("template");
1563 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1564 Builder.AddPlaceholderChunk("declaration");
1565 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001566 }
Douglas Gregor01dfea02010-01-10 23:08:15 +00001567 }
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001568
David Blaikie4e4d0842012-03-11 07:00:24 +00001569 if (SemaRef.getLangOpts().ObjC1)
Douglas Gregorbca403c2010-01-13 23:51:12 +00001570 AddObjCTopLevelResults(Results, true);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001571
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001572 AddTypedefResult(Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001573 // Fall through
1574
John McCallf312b1e2010-08-26 23:41:50 +00001575 case Sema::PCC_Class:
David Blaikie4e4d0842012-03-11 07:00:24 +00001576 if (SemaRef.getLangOpts().CPlusPlus) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001577 // Using declaration
Douglas Gregor218937c2011-02-01 19:23:04 +00001578 Builder.AddTypedTextChunk("using");
1579 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1580 Builder.AddPlaceholderChunk("qualifier");
1581 Builder.AddTextChunk("::");
1582 Builder.AddPlaceholderChunk("name");
1583 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001584
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001585 // using typename qualifier::name (only in a dependent context)
Douglas Gregor01dfea02010-01-10 23:08:15 +00001586 if (SemaRef.CurContext->isDependentContext()) {
Douglas Gregor218937c2011-02-01 19:23:04 +00001587 Builder.AddTypedTextChunk("using");
1588 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1589 Builder.AddTextChunk("typename");
1590 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1591 Builder.AddPlaceholderChunk("qualifier");
1592 Builder.AddTextChunk("::");
1593 Builder.AddPlaceholderChunk("name");
1594 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001595 }
1596
John McCallf312b1e2010-08-26 23:41:50 +00001597 if (CCC == Sema::PCC_Class) {
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001598 AddTypedefResult(Results);
1599
Douglas Gregor01dfea02010-01-10 23:08:15 +00001600 // public:
Douglas Gregor218937c2011-02-01 19:23:04 +00001601 Builder.AddTypedTextChunk("public");
Douglas Gregor10ccf122012-04-10 17:56:28 +00001602 if (Results.includeCodePatterns())
1603 Builder.AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor218937c2011-02-01 19:23:04 +00001604 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001605
1606 // protected:
Douglas Gregor218937c2011-02-01 19:23:04 +00001607 Builder.AddTypedTextChunk("protected");
Douglas Gregor10ccf122012-04-10 17:56:28 +00001608 if (Results.includeCodePatterns())
1609 Builder.AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor218937c2011-02-01 19:23:04 +00001610 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001611
1612 // private:
Douglas Gregor218937c2011-02-01 19:23:04 +00001613 Builder.AddTypedTextChunk("private");
Douglas Gregor10ccf122012-04-10 17:56:28 +00001614 if (Results.includeCodePatterns())
1615 Builder.AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor218937c2011-02-01 19:23:04 +00001616 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001617 }
1618 }
1619 // Fall through
1620
John McCallf312b1e2010-08-26 23:41:50 +00001621 case Sema::PCC_Template:
1622 case Sema::PCC_MemberTemplate:
David Blaikie4e4d0842012-03-11 07:00:24 +00001623 if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001624 // template < parameters >
Douglas Gregor218937c2011-02-01 19:23:04 +00001625 Builder.AddTypedTextChunk("template");
1626 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1627 Builder.AddPlaceholderChunk("parameters");
1628 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1629 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001630 }
1631
David Blaikie4e4d0842012-03-11 07:00:24 +00001632 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1633 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001634 break;
1635
John McCallf312b1e2010-08-26 23:41:50 +00001636 case Sema::PCC_ObjCInterface:
David Blaikie4e4d0842012-03-11 07:00:24 +00001637 AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true);
1638 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1639 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001640 break;
1641
John McCallf312b1e2010-08-26 23:41:50 +00001642 case Sema::PCC_ObjCImplementation:
David Blaikie4e4d0842012-03-11 07:00:24 +00001643 AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true);
1644 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1645 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001646 break;
1647
John McCallf312b1e2010-08-26 23:41:50 +00001648 case Sema::PCC_ObjCInstanceVariableList:
David Blaikie4e4d0842012-03-11 07:00:24 +00001649 AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true);
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001650 break;
1651
John McCallf312b1e2010-08-26 23:41:50 +00001652 case Sema::PCC_RecoveryInFunction:
1653 case Sema::PCC_Statement: {
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001654 AddTypedefResult(Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001655
David Blaikie4e4d0842012-03-11 07:00:24 +00001656 if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() &&
1657 SemaRef.getLangOpts().CXXExceptions) {
Douglas Gregor218937c2011-02-01 19:23:04 +00001658 Builder.AddTypedTextChunk("try");
1659 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1660 Builder.AddPlaceholderChunk("statements");
1661 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1662 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1663 Builder.AddTextChunk("catch");
1664 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1665 Builder.AddPlaceholderChunk("declaration");
1666 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1667 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1668 Builder.AddPlaceholderChunk("statements");
1669 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1670 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1671 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001672 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001673 if (SemaRef.getLangOpts().ObjC1)
Douglas Gregorbca403c2010-01-13 23:51:12 +00001674 AddObjCStatementResults(Results, true);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001675
Douglas Gregord8e8a582010-05-25 21:41:55 +00001676 if (Results.includeCodePatterns()) {
1677 // if (condition) { statements }
Douglas Gregor218937c2011-02-01 19:23:04 +00001678 Builder.AddTypedTextChunk("if");
1679 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikie4e4d0842012-03-11 07:00:24 +00001680 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor218937c2011-02-01 19:23:04 +00001681 Builder.AddPlaceholderChunk("condition");
Douglas Gregord8e8a582010-05-25 21:41:55 +00001682 else
Douglas Gregor218937c2011-02-01 19:23:04 +00001683 Builder.AddPlaceholderChunk("expression");
1684 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1685 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1686 Builder.AddPlaceholderChunk("statements");
1687 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1688 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1689 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001690
Douglas Gregord8e8a582010-05-25 21:41:55 +00001691 // switch (condition) { }
Douglas Gregor218937c2011-02-01 19:23:04 +00001692 Builder.AddTypedTextChunk("switch");
1693 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikie4e4d0842012-03-11 07:00:24 +00001694 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor218937c2011-02-01 19:23:04 +00001695 Builder.AddPlaceholderChunk("condition");
Douglas Gregord8e8a582010-05-25 21:41:55 +00001696 else
Douglas Gregor218937c2011-02-01 19:23:04 +00001697 Builder.AddPlaceholderChunk("expression");
1698 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1699 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1700 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1701 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1702 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregord8e8a582010-05-25 21:41:55 +00001703 }
1704
Douglas Gregor01dfea02010-01-10 23:08:15 +00001705 // Switch-specific statements.
John McCall781472f2010-08-25 08:40:02 +00001706 if (!SemaRef.getCurFunction()->SwitchStack.empty()) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001707 // case expression:
Douglas Gregor218937c2011-02-01 19:23:04 +00001708 Builder.AddTypedTextChunk("case");
1709 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1710 Builder.AddPlaceholderChunk("expression");
1711 Builder.AddChunk(CodeCompletionString::CK_Colon);
1712 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001713
1714 // default:
Douglas Gregor218937c2011-02-01 19:23:04 +00001715 Builder.AddTypedTextChunk("default");
1716 Builder.AddChunk(CodeCompletionString::CK_Colon);
1717 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001718 }
1719
Douglas Gregord8e8a582010-05-25 21:41:55 +00001720 if (Results.includeCodePatterns()) {
1721 /// while (condition) { statements }
Douglas Gregor218937c2011-02-01 19:23:04 +00001722 Builder.AddTypedTextChunk("while");
1723 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikie4e4d0842012-03-11 07:00:24 +00001724 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor218937c2011-02-01 19:23:04 +00001725 Builder.AddPlaceholderChunk("condition");
Douglas Gregord8e8a582010-05-25 21:41:55 +00001726 else
Douglas Gregor218937c2011-02-01 19:23:04 +00001727 Builder.AddPlaceholderChunk("expression");
1728 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1729 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1730 Builder.AddPlaceholderChunk("statements");
1731 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1732 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1733 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregord8e8a582010-05-25 21:41:55 +00001734
1735 // do { statements } while ( expression );
Douglas Gregor218937c2011-02-01 19:23:04 +00001736 Builder.AddTypedTextChunk("do");
1737 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1738 Builder.AddPlaceholderChunk("statements");
1739 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1740 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1741 Builder.AddTextChunk("while");
1742 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1743 Builder.AddPlaceholderChunk("expression");
1744 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1745 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001746
Douglas Gregord8e8a582010-05-25 21:41:55 +00001747 // for ( for-init-statement ; condition ; expression ) { statements }
Douglas Gregor218937c2011-02-01 19:23:04 +00001748 Builder.AddTypedTextChunk("for");
1749 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikie4e4d0842012-03-11 07:00:24 +00001750 if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99)
Douglas Gregor218937c2011-02-01 19:23:04 +00001751 Builder.AddPlaceholderChunk("init-statement");
Douglas Gregord8e8a582010-05-25 21:41:55 +00001752 else
Douglas Gregor218937c2011-02-01 19:23:04 +00001753 Builder.AddPlaceholderChunk("init-expression");
1754 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
1755 Builder.AddPlaceholderChunk("condition");
1756 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
1757 Builder.AddPlaceholderChunk("inc-expression");
1758 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1759 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1760 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1761 Builder.AddPlaceholderChunk("statements");
1762 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1763 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1764 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregord8e8a582010-05-25 21:41:55 +00001765 }
Douglas Gregor01dfea02010-01-10 23:08:15 +00001766
1767 if (S->getContinueParent()) {
1768 // continue ;
Douglas Gregor218937c2011-02-01 19:23:04 +00001769 Builder.AddTypedTextChunk("continue");
1770 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001771 }
1772
1773 if (S->getBreakParent()) {
1774 // break ;
Douglas Gregor218937c2011-02-01 19:23:04 +00001775 Builder.AddTypedTextChunk("break");
1776 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001777 }
1778
1779 // "return expression ;" or "return ;", depending on whether we
1780 // know the function is void or not.
1781 bool isVoid = false;
1782 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext))
1783 isVoid = Function->getResultType()->isVoidType();
1784 else if (ObjCMethodDecl *Method
1785 = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext))
1786 isVoid = Method->getResultType()->isVoidType();
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +00001787 else if (SemaRef.getCurBlock() &&
1788 !SemaRef.getCurBlock()->ReturnType.isNull())
1789 isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType();
Douglas Gregor218937c2011-02-01 19:23:04 +00001790 Builder.AddTypedTextChunk("return");
Douglas Gregor93298002010-02-18 04:06:48 +00001791 if (!isVoid) {
Douglas Gregor218937c2011-02-01 19:23:04 +00001792 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1793 Builder.AddPlaceholderChunk("expression");
Douglas Gregor93298002010-02-18 04:06:48 +00001794 }
Douglas Gregor218937c2011-02-01 19:23:04 +00001795 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001796
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001797 // goto identifier ;
Douglas Gregor218937c2011-02-01 19:23:04 +00001798 Builder.AddTypedTextChunk("goto");
1799 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1800 Builder.AddPlaceholderChunk("label");
1801 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001802
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001803 // Using directives
Douglas Gregor218937c2011-02-01 19:23:04 +00001804 Builder.AddTypedTextChunk("using");
1805 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1806 Builder.AddTextChunk("namespace");
1807 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1808 Builder.AddPlaceholderChunk("identifier");
1809 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001810 }
1811
1812 // Fall through (for statement expressions).
John McCallf312b1e2010-08-26 23:41:50 +00001813 case Sema::PCC_ForInit:
1814 case Sema::PCC_Condition:
David Blaikie4e4d0842012-03-11 07:00:24 +00001815 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001816 // Fall through: conditions and statements can have expressions.
1817
Douglas Gregor02688102010-09-14 23:59:36 +00001818 case Sema::PCC_ParenthesizedExpression:
David Blaikie4e4d0842012-03-11 07:00:24 +00001819 if (SemaRef.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001820 CCC == Sema::PCC_ParenthesizedExpression) {
1821 // (__bridge <type>)<expression>
1822 Builder.AddTypedTextChunk("__bridge");
1823 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1824 Builder.AddPlaceholderChunk("type");
1825 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1826 Builder.AddPlaceholderChunk("expression");
1827 Results.AddResult(Result(Builder.TakeString()));
1828
1829 // (__bridge_transfer <Objective-C type>)<expression>
1830 Builder.AddTypedTextChunk("__bridge_transfer");
1831 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1832 Builder.AddPlaceholderChunk("Objective-C type");
1833 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1834 Builder.AddPlaceholderChunk("expression");
1835 Results.AddResult(Result(Builder.TakeString()));
1836
1837 // (__bridge_retained <CF type>)<expression>
1838 Builder.AddTypedTextChunk("__bridge_retained");
1839 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1840 Builder.AddPlaceholderChunk("CF type");
1841 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1842 Builder.AddPlaceholderChunk("expression");
1843 Results.AddResult(Result(Builder.TakeString()));
1844 }
1845 // Fall through
1846
John McCallf312b1e2010-08-26 23:41:50 +00001847 case Sema::PCC_Expression: {
David Blaikie4e4d0842012-03-11 07:00:24 +00001848 if (SemaRef.getLangOpts().CPlusPlus) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001849 // 'this', if we're in a non-static member function.
Douglas Gregor81f3bff2012-02-15 15:34:24 +00001850 addThisCompletion(SemaRef, Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001851
Douglas Gregor8ca72082011-10-18 21:20:17 +00001852 // true
1853 Builder.AddResultTypeChunk("bool");
1854 Builder.AddTypedTextChunk("true");
1855 Results.AddResult(Result(Builder.TakeString()));
1856
1857 // false
1858 Builder.AddResultTypeChunk("bool");
1859 Builder.AddTypedTextChunk("false");
1860 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001861
David Blaikie4e4d0842012-03-11 07:00:24 +00001862 if (SemaRef.getLangOpts().RTTI) {
Douglas Gregorec3310a2011-04-12 02:47:21 +00001863 // dynamic_cast < type-id > ( expression )
1864 Builder.AddTypedTextChunk("dynamic_cast");
1865 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1866 Builder.AddPlaceholderChunk("type");
1867 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1868 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1869 Builder.AddPlaceholderChunk("expression");
1870 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1871 Results.AddResult(Result(Builder.TakeString()));
1872 }
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001873
1874 // static_cast < type-id > ( expression )
Douglas Gregor218937c2011-02-01 19:23:04 +00001875 Builder.AddTypedTextChunk("static_cast");
1876 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1877 Builder.AddPlaceholderChunk("type");
1878 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1879 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1880 Builder.AddPlaceholderChunk("expression");
1881 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1882 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001883
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001884 // reinterpret_cast < type-id > ( expression )
Douglas Gregor218937c2011-02-01 19:23:04 +00001885 Builder.AddTypedTextChunk("reinterpret_cast");
1886 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1887 Builder.AddPlaceholderChunk("type");
1888 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1889 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1890 Builder.AddPlaceholderChunk("expression");
1891 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1892 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001893
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001894 // const_cast < type-id > ( expression )
Douglas Gregor218937c2011-02-01 19:23:04 +00001895 Builder.AddTypedTextChunk("const_cast");
1896 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1897 Builder.AddPlaceholderChunk("type");
1898 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1899 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1900 Builder.AddPlaceholderChunk("expression");
1901 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1902 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001903
David Blaikie4e4d0842012-03-11 07:00:24 +00001904 if (SemaRef.getLangOpts().RTTI) {
Douglas Gregorec3310a2011-04-12 02:47:21 +00001905 // typeid ( expression-or-type )
Douglas Gregor8ca72082011-10-18 21:20:17 +00001906 Builder.AddResultTypeChunk("std::type_info");
Douglas Gregorec3310a2011-04-12 02:47:21 +00001907 Builder.AddTypedTextChunk("typeid");
1908 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1909 Builder.AddPlaceholderChunk("expression-or-type");
1910 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1911 Results.AddResult(Result(Builder.TakeString()));
1912 }
1913
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001914 // new T ( ... )
Douglas Gregor218937c2011-02-01 19:23:04 +00001915 Builder.AddTypedTextChunk("new");
1916 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1917 Builder.AddPlaceholderChunk("type");
1918 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1919 Builder.AddPlaceholderChunk("expressions");
1920 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1921 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001922
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001923 // new T [ ] ( ... )
Douglas Gregor218937c2011-02-01 19:23:04 +00001924 Builder.AddTypedTextChunk("new");
1925 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1926 Builder.AddPlaceholderChunk("type");
1927 Builder.AddChunk(CodeCompletionString::CK_LeftBracket);
1928 Builder.AddPlaceholderChunk("size");
1929 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
1930 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1931 Builder.AddPlaceholderChunk("expressions");
1932 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1933 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001934
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001935 // delete expression
Douglas Gregor8ca72082011-10-18 21:20:17 +00001936 Builder.AddResultTypeChunk("void");
Douglas Gregor218937c2011-02-01 19:23:04 +00001937 Builder.AddTypedTextChunk("delete");
1938 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1939 Builder.AddPlaceholderChunk("expression");
1940 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001941
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001942 // delete [] expression
Douglas Gregor8ca72082011-10-18 21:20:17 +00001943 Builder.AddResultTypeChunk("void");
Douglas Gregor218937c2011-02-01 19:23:04 +00001944 Builder.AddTypedTextChunk("delete");
1945 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1946 Builder.AddChunk(CodeCompletionString::CK_LeftBracket);
1947 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
1948 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1949 Builder.AddPlaceholderChunk("expression");
1950 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001951
David Blaikie4e4d0842012-03-11 07:00:24 +00001952 if (SemaRef.getLangOpts().CXXExceptions) {
Douglas Gregorec3310a2011-04-12 02:47:21 +00001953 // throw expression
Douglas Gregor8ca72082011-10-18 21:20:17 +00001954 Builder.AddResultTypeChunk("void");
Douglas Gregorec3310a2011-04-12 02:47:21 +00001955 Builder.AddTypedTextChunk("throw");
1956 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1957 Builder.AddPlaceholderChunk("expression");
1958 Results.AddResult(Result(Builder.TakeString()));
1959 }
Douglas Gregora50216c2011-10-18 16:29:03 +00001960
Douglas Gregor12e13132010-05-26 22:00:08 +00001961 // FIXME: Rethrow?
Douglas Gregora50216c2011-10-18 16:29:03 +00001962
Richard Smith80ad52f2013-01-02 11:42:31 +00001963 if (SemaRef.getLangOpts().CPlusPlus11) {
Douglas Gregora50216c2011-10-18 16:29:03 +00001964 // nullptr
Douglas Gregor8ca72082011-10-18 21:20:17 +00001965 Builder.AddResultTypeChunk("std::nullptr_t");
Douglas Gregora50216c2011-10-18 16:29:03 +00001966 Builder.AddTypedTextChunk("nullptr");
1967 Results.AddResult(Result(Builder.TakeString()));
1968
1969 // alignof
Douglas Gregor8ca72082011-10-18 21:20:17 +00001970 Builder.AddResultTypeChunk("size_t");
Douglas Gregora50216c2011-10-18 16:29:03 +00001971 Builder.AddTypedTextChunk("alignof");
1972 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1973 Builder.AddPlaceholderChunk("type");
1974 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1975 Results.AddResult(Result(Builder.TakeString()));
1976
1977 // noexcept
Douglas Gregor8ca72082011-10-18 21:20:17 +00001978 Builder.AddResultTypeChunk("bool");
Douglas Gregora50216c2011-10-18 16:29:03 +00001979 Builder.AddTypedTextChunk("noexcept");
1980 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1981 Builder.AddPlaceholderChunk("expression");
1982 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1983 Results.AddResult(Result(Builder.TakeString()));
1984
1985 // sizeof... expression
Douglas Gregor8ca72082011-10-18 21:20:17 +00001986 Builder.AddResultTypeChunk("size_t");
Douglas Gregora50216c2011-10-18 16:29:03 +00001987 Builder.AddTypedTextChunk("sizeof...");
1988 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1989 Builder.AddPlaceholderChunk("parameter-pack");
1990 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1991 Results.AddResult(Result(Builder.TakeString()));
1992 }
Douglas Gregor01dfea02010-01-10 23:08:15 +00001993 }
1994
David Blaikie4e4d0842012-03-11 07:00:24 +00001995 if (SemaRef.getLangOpts().ObjC1) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001996 // Add "super", if we're in an Objective-C class with a superclass.
Ted Kremenek681e2562010-05-31 21:43:10 +00001997 if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) {
1998 // The interface can be NULL.
1999 if (ObjCInterfaceDecl *ID = Method->getClassInterface())
Douglas Gregor8ca72082011-10-18 21:20:17 +00002000 if (ID->getSuperClass()) {
2001 std::string SuperType;
2002 SuperType = ID->getSuperClass()->getNameAsString();
2003 if (Method->isInstanceMethod())
2004 SuperType += " *";
2005
2006 Builder.AddResultTypeChunk(Allocator.CopyString(SuperType));
2007 Builder.AddTypedTextChunk("super");
2008 Results.AddResult(Result(Builder.TakeString()));
2009 }
Ted Kremenek681e2562010-05-31 21:43:10 +00002010 }
2011
Douglas Gregorbca403c2010-01-13 23:51:12 +00002012 AddObjCExpressionResults(Results, true);
Douglas Gregor01dfea02010-01-10 23:08:15 +00002013 }
2014
Jordan Rosef70a8862012-06-30 21:33:57 +00002015 if (SemaRef.getLangOpts().C11) {
2016 // _Alignof
2017 Builder.AddResultTypeChunk("size_t");
2018 if (SemaRef.getASTContext().Idents.get("alignof").hasMacroDefinition())
2019 Builder.AddTypedTextChunk("alignof");
2020 else
2021 Builder.AddTypedTextChunk("_Alignof");
2022 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
2023 Builder.AddPlaceholderChunk("type");
2024 Builder.AddChunk(CodeCompletionString::CK_RightParen);
2025 Results.AddResult(Result(Builder.TakeString()));
2026 }
2027
Douglas Gregorc8bddde2010-05-28 00:22:41 +00002028 // sizeof expression
Douglas Gregor8ca72082011-10-18 21:20:17 +00002029 Builder.AddResultTypeChunk("size_t");
Douglas Gregor218937c2011-02-01 19:23:04 +00002030 Builder.AddTypedTextChunk("sizeof");
2031 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
2032 Builder.AddPlaceholderChunk("expression-or-type");
2033 Builder.AddChunk(CodeCompletionString::CK_RightParen);
2034 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00002035 break;
2036 }
Douglas Gregord32b0222010-08-24 01:06:58 +00002037
John McCallf312b1e2010-08-26 23:41:50 +00002038 case Sema::PCC_Type:
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002039 case Sema::PCC_LocalDeclarationSpecifiers:
Douglas Gregord32b0222010-08-24 01:06:58 +00002040 break;
Douglas Gregor01dfea02010-01-10 23:08:15 +00002041 }
2042
David Blaikie4e4d0842012-03-11 07:00:24 +00002043 if (WantTypesInContext(CCC, SemaRef.getLangOpts()))
2044 AddTypeSpecifierResults(SemaRef.getLangOpts(), Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00002045
David Blaikie4e4d0842012-03-11 07:00:24 +00002046 if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type)
Douglas Gregora4477812010-01-14 16:01:26 +00002047 Results.AddResult(Result("operator"));
Douglas Gregor01dfea02010-01-10 23:08:15 +00002048}
2049
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002050/// \brief If the given declaration has an associated type, add it as a result
2051/// type chunk.
2052static void AddResultTypeChunk(ASTContext &Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00002053 const PrintingPolicy &Policy,
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002054 const NamedDecl *ND,
Douglas Gregor218937c2011-02-01 19:23:04 +00002055 CodeCompletionBuilder &Result) {
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002056 if (!ND)
2057 return;
Douglas Gregor6f942b22010-09-21 16:06:22 +00002058
2059 // Skip constructors and conversion functions, which have their return types
2060 // built into their names.
2061 if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND))
2062 return;
2063
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002064 // Determine the type of the declaration (if it has a type).
Douglas Gregor6f942b22010-09-21 16:06:22 +00002065 QualType T;
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002066 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND))
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002067 T = Function->getResultType();
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002068 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002069 T = Method->getResultType();
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002070 else if (const FunctionTemplateDecl *FunTmpl =
2071 dyn_cast<FunctionTemplateDecl>(ND))
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002072 T = FunTmpl->getTemplatedDecl()->getResultType();
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002073 else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND))
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002074 T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext()));
2075 else if (isa<UnresolvedUsingValueDecl>(ND)) {
2076 /* Do nothing: ignore unresolved using declarations*/
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002077 } else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) {
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002078 T = Value->getType();
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002079 } else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND))
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002080 T = Property->getType();
2081
2082 if (T.isNull() || Context.hasSameType(T, Context.DependentTy))
2083 return;
2084
Douglas Gregor8987b232011-09-27 23:30:47 +00002085 Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy,
Douglas Gregora63f6de2011-02-01 21:15:40 +00002086 Result.getAllocator()));
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002087}
2088
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002089static void MaybeAddSentinel(ASTContext &Context,
2090 const NamedDecl *FunctionOrMethod,
Douglas Gregor218937c2011-02-01 19:23:04 +00002091 CodeCompletionBuilder &Result) {
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002092 if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>())
2093 if (Sentinel->getSentinel() == 0) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002094 if (Context.getLangOpts().ObjC1 &&
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002095 Context.Idents.get("nil").hasMacroDefinition())
Douglas Gregor218937c2011-02-01 19:23:04 +00002096 Result.AddTextChunk(", nil");
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002097 else if (Context.Idents.get("NULL").hasMacroDefinition())
Douglas Gregor218937c2011-02-01 19:23:04 +00002098 Result.AddTextChunk(", NULL");
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002099 else
Douglas Gregor218937c2011-02-01 19:23:04 +00002100 Result.AddTextChunk(", (void*)0");
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002101 }
2102}
2103
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002104static std::string formatObjCParamQualifiers(unsigned ObjCQuals) {
2105 std::string Result;
2106 if (ObjCQuals & Decl::OBJC_TQ_In)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002107 Result += "in ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002108 else if (ObjCQuals & Decl::OBJC_TQ_Inout)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002109 Result += "inout ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002110 else if (ObjCQuals & Decl::OBJC_TQ_Out)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002111 Result += "out ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002112 if (ObjCQuals & Decl::OBJC_TQ_Bycopy)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002113 Result += "bycopy ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002114 else if (ObjCQuals & Decl::OBJC_TQ_Byref)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002115 Result += "byref ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002116 if (ObjCQuals & Decl::OBJC_TQ_Oneway)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002117 Result += "oneway ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002118 return Result;
2119}
2120
Douglas Gregor83482d12010-08-24 16:15:59 +00002121static std::string FormatFunctionParameter(ASTContext &Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00002122 const PrintingPolicy &Policy,
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002123 const ParmVarDecl *Param,
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002124 bool SuppressName = false,
2125 bool SuppressBlock = false) {
Douglas Gregor83482d12010-08-24 16:15:59 +00002126 bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext());
2127 if (Param->getType()->isDependentType() ||
2128 !Param->getType()->isBlockPointerType()) {
2129 // The argument for a dependent or non-block parameter is a placeholder
2130 // containing that parameter's type.
2131 std::string Result;
2132
Douglas Gregoraba48082010-08-29 19:47:46 +00002133 if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName)
Douglas Gregor83482d12010-08-24 16:15:59 +00002134 Result = Param->getIdentifier()->getName();
2135
John McCallf85e1932011-06-15 23:02:42 +00002136 Param->getType().getAsStringInternal(Result, Policy);
Douglas Gregor83482d12010-08-24 16:15:59 +00002137
2138 if (ObjCMethodParam) {
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002139 Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier())
2140 + Result + ")";
Douglas Gregoraba48082010-08-29 19:47:46 +00002141 if (Param->getIdentifier() && !SuppressName)
Douglas Gregor83482d12010-08-24 16:15:59 +00002142 Result += Param->getIdentifier()->getName();
2143 }
2144 return Result;
2145 }
2146
2147 // The argument for a block pointer parameter is a block literal with
2148 // the appropriate type.
Douglas Gregor830072c2011-02-15 22:37:09 +00002149 FunctionTypeLoc *Block = 0;
2150 FunctionProtoTypeLoc *BlockProto = 0;
Douglas Gregor83482d12010-08-24 16:15:59 +00002151 TypeLoc TL;
2152 if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) {
2153 TL = TSInfo->getTypeLoc().getUnqualifiedLoc();
2154 while (true) {
2155 // Look through typedefs.
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002156 if (!SuppressBlock) {
2157 if (TypedefTypeLoc *TypedefTL = dyn_cast<TypedefTypeLoc>(&TL)) {
2158 if (TypeSourceInfo *InnerTSInfo
2159 = TypedefTL->getTypedefNameDecl()->getTypeSourceInfo()) {
2160 TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc();
2161 continue;
2162 }
2163 }
2164
2165 // Look through qualified types
2166 if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) {
2167 TL = QualifiedTL->getUnqualifiedLoc();
Douglas Gregor83482d12010-08-24 16:15:59 +00002168 continue;
2169 }
2170 }
2171
Douglas Gregor83482d12010-08-24 16:15:59 +00002172 // Try to get the function prototype behind the block pointer type,
2173 // then we're done.
2174 if (BlockPointerTypeLoc *BlockPtr
2175 = dyn_cast<BlockPointerTypeLoc>(&TL)) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002176 TL = BlockPtr->getPointeeLoc().IgnoreParens();
Douglas Gregor830072c2011-02-15 22:37:09 +00002177 Block = dyn_cast<FunctionTypeLoc>(&TL);
2178 BlockProto = dyn_cast<FunctionProtoTypeLoc>(&TL);
Douglas Gregor83482d12010-08-24 16:15:59 +00002179 }
2180 break;
2181 }
2182 }
2183
2184 if (!Block) {
2185 // We were unable to find a FunctionProtoTypeLoc with parameter names
2186 // for the block; just use the parameter type as a placeholder.
2187 std::string Result;
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002188 if (!ObjCMethodParam && Param->getIdentifier())
2189 Result = Param->getIdentifier()->getName();
2190
John McCallf85e1932011-06-15 23:02:42 +00002191 Param->getType().getUnqualifiedType().getAsStringInternal(Result, Policy);
Douglas Gregor83482d12010-08-24 16:15:59 +00002192
2193 if (ObjCMethodParam) {
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002194 Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier())
2195 + Result + ")";
Douglas Gregor83482d12010-08-24 16:15:59 +00002196 if (Param->getIdentifier())
2197 Result += Param->getIdentifier()->getName();
2198 }
2199
2200 return Result;
2201 }
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002202
Douglas Gregor83482d12010-08-24 16:15:59 +00002203 // We have the function prototype behind the block pointer type, as it was
2204 // written in the source.
Douglas Gregor38276252010-09-08 22:47:51 +00002205 std::string Result;
2206 QualType ResultType = Block->getTypePtr()->getResultType();
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002207 if (!ResultType->isVoidType() || SuppressBlock)
John McCallf85e1932011-06-15 23:02:42 +00002208 ResultType.getAsStringInternal(Result, Policy);
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002209
2210 // Format the parameter list.
2211 std::string Params;
Douglas Gregor830072c2011-02-15 22:37:09 +00002212 if (!BlockProto || Block->getNumArgs() == 0) {
2213 if (BlockProto && BlockProto->getTypePtr()->isVariadic())
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002214 Params = "(...)";
Douglas Gregorc2760bc2010-10-02 23:49:58 +00002215 else
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002216 Params = "(void)";
Douglas Gregor38276252010-09-08 22:47:51 +00002217 } else {
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002218 Params += "(";
Douglas Gregor38276252010-09-08 22:47:51 +00002219 for (unsigned I = 0, N = Block->getNumArgs(); I != N; ++I) {
2220 if (I)
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002221 Params += ", ";
2222 Params += FormatFunctionParameter(Context, Policy, Block->getArg(I),
2223 /*SuppressName=*/false,
2224 /*SuppressBlock=*/true);
Douglas Gregor38276252010-09-08 22:47:51 +00002225
Douglas Gregor830072c2011-02-15 22:37:09 +00002226 if (I == N - 1 && BlockProto->getTypePtr()->isVariadic())
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002227 Params += ", ...";
Douglas Gregor38276252010-09-08 22:47:51 +00002228 }
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002229 Params += ")";
Douglas Gregore17794f2010-08-31 05:13:43 +00002230 }
Douglas Gregor38276252010-09-08 22:47:51 +00002231
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002232 if (SuppressBlock) {
2233 // Format as a parameter.
2234 Result = Result + " (^";
2235 if (Param->getIdentifier())
2236 Result += Param->getIdentifier()->getName();
2237 Result += ")";
2238 Result += Params;
2239 } else {
2240 // Format as a block literal argument.
2241 Result = '^' + Result;
2242 Result += Params;
2243
2244 if (Param->getIdentifier())
2245 Result += Param->getIdentifier()->getName();
2246 }
2247
Douglas Gregor83482d12010-08-24 16:15:59 +00002248 return Result;
2249}
2250
Douglas Gregor86d9a522009-09-21 16:56:56 +00002251/// \brief Add function parameter chunks to the given code completion string.
2252static void AddFunctionParameterChunks(ASTContext &Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00002253 const PrintingPolicy &Policy,
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002254 const FunctionDecl *Function,
Douglas Gregor218937c2011-02-01 19:23:04 +00002255 CodeCompletionBuilder &Result,
2256 unsigned Start = 0,
2257 bool InOptional = false) {
Douglas Gregor218937c2011-02-01 19:23:04 +00002258 bool FirstParameter = true;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002259
Douglas Gregor218937c2011-02-01 19:23:04 +00002260 for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002261 const ParmVarDecl *Param = Function->getParamDecl(P);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002262
Douglas Gregor218937c2011-02-01 19:23:04 +00002263 if (Param->hasDefaultArg() && !InOptional) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002264 // When we see an optional default argument, put that argument and
2265 // the remaining default arguments into a new, optional string.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002266 CodeCompletionBuilder Opt(Result.getAllocator(),
2267 Result.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00002268 if (!FirstParameter)
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002269 Opt.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor8987b232011-09-27 23:30:47 +00002270 AddFunctionParameterChunks(Context, Policy, Function, Opt, P, true);
Douglas Gregor218937c2011-02-01 19:23:04 +00002271 Result.AddOptionalChunk(Opt.TakeString());
2272 break;
Douglas Gregor86d9a522009-09-21 16:56:56 +00002273 }
2274
Douglas Gregor218937c2011-02-01 19:23:04 +00002275 if (FirstParameter)
2276 FirstParameter = false;
2277 else
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002278 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor218937c2011-02-01 19:23:04 +00002279
2280 InOptional = false;
Douglas Gregor86d9a522009-09-21 16:56:56 +00002281
2282 // Format the placeholder string.
Douglas Gregor8987b232011-09-27 23:30:47 +00002283 std::string PlaceholderStr = FormatFunctionParameter(Context, Policy,
2284 Param);
Douglas Gregor83482d12010-08-24 16:15:59 +00002285
Douglas Gregore17794f2010-08-31 05:13:43 +00002286 if (Function->isVariadic() && P == N - 1)
2287 PlaceholderStr += ", ...";
2288
Douglas Gregor86d9a522009-09-21 16:56:56 +00002289 // Add the placeholder string.
Douglas Gregordae68752011-02-01 22:57:45 +00002290 Result.AddPlaceholderChunk(
2291 Result.getAllocator().CopyString(PlaceholderStr));
Douglas Gregor86d9a522009-09-21 16:56:56 +00002292 }
Douglas Gregorb3d45252009-09-22 21:42:17 +00002293
2294 if (const FunctionProtoType *Proto
2295 = Function->getType()->getAs<FunctionProtoType>())
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002296 if (Proto->isVariadic()) {
Douglas Gregore17794f2010-08-31 05:13:43 +00002297 if (Proto->getNumArgs() == 0)
Douglas Gregor218937c2011-02-01 19:23:04 +00002298 Result.AddPlaceholderChunk("...");
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002299
Douglas Gregor218937c2011-02-01 19:23:04 +00002300 MaybeAddSentinel(Context, Function, Result);
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002301 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00002302}
2303
2304/// \brief Add template parameter chunks to the given code completion string.
2305static void AddTemplateParameterChunks(ASTContext &Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00002306 const PrintingPolicy &Policy,
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002307 const TemplateDecl *Template,
Douglas Gregor218937c2011-02-01 19:23:04 +00002308 CodeCompletionBuilder &Result,
2309 unsigned MaxParameters = 0,
2310 unsigned Start = 0,
2311 bool InDefaultArg = false) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002312 bool FirstParameter = true;
2313
2314 TemplateParameterList *Params = Template->getTemplateParameters();
2315 TemplateParameterList::iterator PEnd = Params->end();
2316 if (MaxParameters)
2317 PEnd = Params->begin() + MaxParameters;
Douglas Gregor218937c2011-02-01 19:23:04 +00002318 for (TemplateParameterList::iterator P = Params->begin() + Start;
2319 P != PEnd; ++P) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002320 bool HasDefaultArg = false;
2321 std::string PlaceholderStr;
2322 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
2323 if (TTP->wasDeclaredWithTypename())
2324 PlaceholderStr = "typename";
2325 else
2326 PlaceholderStr = "class";
2327
2328 if (TTP->getIdentifier()) {
2329 PlaceholderStr += ' ';
2330 PlaceholderStr += TTP->getIdentifier()->getName();
2331 }
2332
2333 HasDefaultArg = TTP->hasDefaultArgument();
2334 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor218937c2011-02-01 19:23:04 +00002335 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002336 if (NTTP->getIdentifier())
2337 PlaceholderStr = NTTP->getIdentifier()->getName();
John McCallf85e1932011-06-15 23:02:42 +00002338 NTTP->getType().getAsStringInternal(PlaceholderStr, Policy);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002339 HasDefaultArg = NTTP->hasDefaultArgument();
2340 } else {
2341 assert(isa<TemplateTemplateParmDecl>(*P));
2342 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
2343
2344 // Since putting the template argument list into the placeholder would
2345 // be very, very long, we just use an abbreviation.
2346 PlaceholderStr = "template<...> class";
2347 if (TTP->getIdentifier()) {
2348 PlaceholderStr += ' ';
2349 PlaceholderStr += TTP->getIdentifier()->getName();
2350 }
2351
2352 HasDefaultArg = TTP->hasDefaultArgument();
2353 }
2354
Douglas Gregor218937c2011-02-01 19:23:04 +00002355 if (HasDefaultArg && !InDefaultArg) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002356 // When we see an optional default argument, put that argument and
2357 // the remaining default arguments into a new, optional string.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002358 CodeCompletionBuilder Opt(Result.getAllocator(),
2359 Result.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00002360 if (!FirstParameter)
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002361 Opt.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor8987b232011-09-27 23:30:47 +00002362 AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters,
Douglas Gregor218937c2011-02-01 19:23:04 +00002363 P - Params->begin(), true);
2364 Result.AddOptionalChunk(Opt.TakeString());
2365 break;
Douglas Gregor86d9a522009-09-21 16:56:56 +00002366 }
2367
Douglas Gregor218937c2011-02-01 19:23:04 +00002368 InDefaultArg = false;
2369
Douglas Gregor86d9a522009-09-21 16:56:56 +00002370 if (FirstParameter)
2371 FirstParameter = false;
2372 else
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002373 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002374
2375 // Add the placeholder string.
Douglas Gregordae68752011-02-01 22:57:45 +00002376 Result.AddPlaceholderChunk(
2377 Result.getAllocator().CopyString(PlaceholderStr));
Douglas Gregor86d9a522009-09-21 16:56:56 +00002378 }
2379}
2380
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00002381/// \brief Add a qualifier to the given code-completion string, if the
2382/// provided nested-name-specifier is non-NULL.
Douglas Gregora61a8792009-12-11 18:44:16 +00002383static void
Douglas Gregor218937c2011-02-01 19:23:04 +00002384AddQualifierToCompletionString(CodeCompletionBuilder &Result,
Douglas Gregora61a8792009-12-11 18:44:16 +00002385 NestedNameSpecifier *Qualifier,
2386 bool QualifierIsInformative,
Douglas Gregor8987b232011-09-27 23:30:47 +00002387 ASTContext &Context,
2388 const PrintingPolicy &Policy) {
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00002389 if (!Qualifier)
2390 return;
2391
2392 std::string PrintedNNS;
2393 {
2394 llvm::raw_string_ostream OS(PrintedNNS);
Douglas Gregor8987b232011-09-27 23:30:47 +00002395 Qualifier->print(OS, Policy);
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00002396 }
Douglas Gregor0563c262009-09-22 23:15:58 +00002397 if (QualifierIsInformative)
Douglas Gregordae68752011-02-01 22:57:45 +00002398 Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS));
Douglas Gregor0563c262009-09-22 23:15:58 +00002399 else
Douglas Gregordae68752011-02-01 22:57:45 +00002400 Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS));
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00002401}
2402
Douglas Gregor218937c2011-02-01 19:23:04 +00002403static void
2404AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result,
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002405 const FunctionDecl *Function) {
Douglas Gregora61a8792009-12-11 18:44:16 +00002406 const FunctionProtoType *Proto
2407 = Function->getType()->getAs<FunctionProtoType>();
2408 if (!Proto || !Proto->getTypeQuals())
2409 return;
2410
Douglas Gregora63f6de2011-02-01 21:15:40 +00002411 // FIXME: Add ref-qualifier!
2412
2413 // Handle single qualifiers without copying
2414 if (Proto->getTypeQuals() == Qualifiers::Const) {
2415 Result.AddInformativeChunk(" const");
2416 return;
2417 }
2418
2419 if (Proto->getTypeQuals() == Qualifiers::Volatile) {
2420 Result.AddInformativeChunk(" volatile");
2421 return;
2422 }
2423
2424 if (Proto->getTypeQuals() == Qualifiers::Restrict) {
2425 Result.AddInformativeChunk(" restrict");
2426 return;
2427 }
2428
2429 // Handle multiple qualifiers.
Douglas Gregora61a8792009-12-11 18:44:16 +00002430 std::string QualsStr;
David Blaikie4ef832f2012-08-10 00:55:35 +00002431 if (Proto->isConst())
Douglas Gregora61a8792009-12-11 18:44:16 +00002432 QualsStr += " const";
David Blaikie4ef832f2012-08-10 00:55:35 +00002433 if (Proto->isVolatile())
Douglas Gregora61a8792009-12-11 18:44:16 +00002434 QualsStr += " volatile";
David Blaikie4ef832f2012-08-10 00:55:35 +00002435 if (Proto->isRestrict())
Douglas Gregora61a8792009-12-11 18:44:16 +00002436 QualsStr += " restrict";
Douglas Gregordae68752011-02-01 22:57:45 +00002437 Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr));
Douglas Gregora61a8792009-12-11 18:44:16 +00002438}
2439
Douglas Gregor6f942b22010-09-21 16:06:22 +00002440/// \brief Add the name of the given declaration
Douglas Gregor8987b232011-09-27 23:30:47 +00002441static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy,
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002442 const NamedDecl *ND,
2443 CodeCompletionBuilder &Result) {
Douglas Gregor6f942b22010-09-21 16:06:22 +00002444 DeclarationName Name = ND->getDeclName();
2445 if (!Name)
2446 return;
2447
2448 switch (Name.getNameKind()) {
Douglas Gregora63f6de2011-02-01 21:15:40 +00002449 case DeclarationName::CXXOperatorName: {
2450 const char *OperatorName = 0;
2451 switch (Name.getCXXOverloadedOperator()) {
2452 case OO_None:
2453 case OO_Conditional:
2454 case NUM_OVERLOADED_OPERATORS:
2455 OperatorName = "operator";
2456 break;
2457
2458#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2459 case OO_##Name: OperatorName = "operator" Spelling; break;
2460#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2461#include "clang/Basic/OperatorKinds.def"
2462
2463 case OO_New: OperatorName = "operator new"; break;
2464 case OO_Delete: OperatorName = "operator delete"; break;
2465 case OO_Array_New: OperatorName = "operator new[]"; break;
2466 case OO_Array_Delete: OperatorName = "operator delete[]"; break;
2467 case OO_Call: OperatorName = "operator()"; break;
2468 case OO_Subscript: OperatorName = "operator[]"; break;
2469 }
2470 Result.AddTypedTextChunk(OperatorName);
2471 break;
2472 }
2473
Douglas Gregor6f942b22010-09-21 16:06:22 +00002474 case DeclarationName::Identifier:
2475 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor6f942b22010-09-21 16:06:22 +00002476 case DeclarationName::CXXDestructorName:
2477 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregordae68752011-02-01 22:57:45 +00002478 Result.AddTypedTextChunk(
2479 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregor6f942b22010-09-21 16:06:22 +00002480 break;
2481
2482 case DeclarationName::CXXUsingDirective:
2483 case DeclarationName::ObjCZeroArgSelector:
2484 case DeclarationName::ObjCOneArgSelector:
2485 case DeclarationName::ObjCMultiArgSelector:
2486 break;
2487
2488 case DeclarationName::CXXConstructorName: {
2489 CXXRecordDecl *Record = 0;
2490 QualType Ty = Name.getCXXNameType();
2491 if (const RecordType *RecordTy = Ty->getAs<RecordType>())
2492 Record = cast<CXXRecordDecl>(RecordTy->getDecl());
2493 else if (const InjectedClassNameType *InjectedTy
2494 = Ty->getAs<InjectedClassNameType>())
2495 Record = InjectedTy->getDecl();
2496 else {
Douglas Gregordae68752011-02-01 22:57:45 +00002497 Result.AddTypedTextChunk(
2498 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregor6f942b22010-09-21 16:06:22 +00002499 break;
2500 }
2501
Douglas Gregordae68752011-02-01 22:57:45 +00002502 Result.AddTypedTextChunk(
2503 Result.getAllocator().CopyString(Record->getNameAsString()));
Douglas Gregor6f942b22010-09-21 16:06:22 +00002504 if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) {
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002505 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
Douglas Gregor8987b232011-09-27 23:30:47 +00002506 AddTemplateParameterChunks(Context, Policy, Template, Result);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002507 Result.AddChunk(CodeCompletionString::CK_RightAngle);
Douglas Gregor6f942b22010-09-21 16:06:22 +00002508 }
2509 break;
2510 }
2511 }
2512}
2513
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002514CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002515 CodeCompletionAllocator &Allocator,
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00002516 CodeCompletionTUInfo &CCTUInfo,
2517 bool IncludeBriefComments) {
2518 return CreateCodeCompletionString(S.Context, S.PP, Allocator, CCTUInfo,
2519 IncludeBriefComments);
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002520}
2521
Douglas Gregor86d9a522009-09-21 16:56:56 +00002522/// \brief If possible, create a new code completion string for the given
2523/// result.
2524///
2525/// \returns Either a new, heap-allocated code completion string describing
2526/// how to use this result, or NULL to indicate that the string or name of the
2527/// result is all that is needed.
2528CodeCompletionString *
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002529CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
2530 Preprocessor &PP,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002531 CodeCompletionAllocator &Allocator,
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00002532 CodeCompletionTUInfo &CCTUInfo,
2533 bool IncludeBriefComments) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002534 CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002535
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002536 PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP);
Douglas Gregor218937c2011-02-01 19:23:04 +00002537 if (Kind == RK_Pattern) {
2538 Pattern->Priority = Priority;
2539 Pattern->Availability = Availability;
Douglas Gregorba103062012-03-27 23:34:16 +00002540
2541 if (Declaration) {
2542 Result.addParentContext(Declaration->getDeclContext());
Douglas Gregorba103062012-03-27 23:34:16 +00002543 Pattern->ParentName = Result.getParentName();
2544 }
2545
Douglas Gregor218937c2011-02-01 19:23:04 +00002546 return Pattern;
2547 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002548
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002549 if (Kind == RK_Keyword) {
Douglas Gregor218937c2011-02-01 19:23:04 +00002550 Result.AddTypedTextChunk(Keyword);
2551 return Result.TakeString();
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002552 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00002553
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002554 if (Kind == RK_Macro) {
Douglas Gregor3644d972012-10-09 16:01:50 +00002555 MacroInfo *MI = PP.getMacroInfoHistory(Macro);
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002556 assert(MI && "Not a macro?");
2557
Douglas Gregordae68752011-02-01 22:57:45 +00002558 Result.AddTypedTextChunk(
2559 Result.getAllocator().CopyString(Macro->getName()));
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002560
2561 if (!MI->isFunctionLike())
Douglas Gregor218937c2011-02-01 19:23:04 +00002562 return Result.TakeString();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002563
2564 // Format a function-like macro with placeholders for the arguments.
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002565 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregore4244702011-07-30 08:17:44 +00002566 MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end();
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002567
2568 // C99 variadic macros add __VA_ARGS__ at the end. Skip it.
2569 if (MI->isC99Varargs()) {
2570 --AEnd;
2571
2572 if (A == AEnd) {
2573 Result.AddPlaceholderChunk("...");
2574 }
Douglas Gregore4244702011-07-30 08:17:44 +00002575 }
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002576
Douglas Gregore4244702011-07-30 08:17:44 +00002577 for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) {
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002578 if (A != MI->arg_begin())
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002579 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002580
2581 if (MI->isVariadic() && (A+1) == AEnd) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002582 SmallString<32> Arg = (*A)->getName();
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002583 if (MI->isC99Varargs())
2584 Arg += ", ...";
2585 else
2586 Arg += "...";
Douglas Gregordae68752011-02-01 22:57:45 +00002587 Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002588 break;
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002589 }
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002590
2591 // Non-variadic macros are simple.
2592 Result.AddPlaceholderChunk(
2593 Result.getAllocator().CopyString((*A)->getName()));
Douglas Gregore4244702011-07-30 08:17:44 +00002594 }
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002595 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor218937c2011-02-01 19:23:04 +00002596 return Result.TakeString();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002597 }
2598
Douglas Gregord8e8a582010-05-25 21:41:55 +00002599 assert(Kind == RK_Declaration && "Missed a result kind?");
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002600 const NamedDecl *ND = Declaration;
Douglas Gregorba103062012-03-27 23:34:16 +00002601 Result.addParentContext(ND->getDeclContext());
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00002602
2603 if (IncludeBriefComments) {
2604 // Add documentation comment, if it exists.
Dmitri Gribenkof50555e2012-08-11 00:51:43 +00002605 if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) {
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00002606 Result.addBriefComment(RC->getBriefText(Ctx));
2607 }
2608 }
2609
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002610 if (StartsNestedNameSpecifier) {
Douglas Gregordae68752011-02-01 22:57:45 +00002611 Result.AddTypedTextChunk(
2612 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregor218937c2011-02-01 19:23:04 +00002613 Result.AddTextChunk("::");
2614 return Result.TakeString();
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002615 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00002616
2617 for (Decl::attr_iterator i = ND->attr_begin(); i != ND->attr_end(); ++i) {
2618 if (AnnotateAttr *Attr = dyn_cast_or_null<AnnotateAttr>(*i)) {
2619 Result.AddAnnotation(Result.getAllocator().CopyString(Attr->getAnnotation()));
2620 }
2621 }
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002622
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002623 AddResultTypeChunk(Ctx, Policy, ND, Result);
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002624
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002625 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
Douglas Gregor0563c262009-09-22 23:15:58 +00002626 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002627 Ctx, Policy);
2628 AddTypedNameChunk(Ctx, Policy, ND, Result);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002629 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002630 AddFunctionParameterChunks(Ctx, Policy, Function, Result);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002631 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregora61a8792009-12-11 18:44:16 +00002632 AddFunctionTypeQualsToCompletionString(Result, Function);
Douglas Gregor218937c2011-02-01 19:23:04 +00002633 return Result.TakeString();
Douglas Gregor86d9a522009-09-21 16:56:56 +00002634 }
2635
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002636 if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
Douglas Gregor0563c262009-09-22 23:15:58 +00002637 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002638 Ctx, Policy);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002639 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002640 AddTypedNameChunk(Ctx, Policy, Function, Result);
Douglas Gregor6f942b22010-09-21 16:06:22 +00002641
Douglas Gregor86d9a522009-09-21 16:56:56 +00002642 // Figure out which template parameters are deduced (or have default
2643 // arguments).
Benjamin Kramer013b3662012-01-30 16:17:39 +00002644 llvm::SmallBitVector Deduced;
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002645 Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002646 unsigned LastDeducibleArgument;
2647 for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0;
2648 --LastDeducibleArgument) {
2649 if (!Deduced[LastDeducibleArgument - 1]) {
2650 // C++0x: Figure out if the template argument has a default. If so,
2651 // the user doesn't need to type this argument.
2652 // FIXME: We need to abstract template parameters better!
2653 bool HasDefaultArg = false;
2654 NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam(
Douglas Gregor218937c2011-02-01 19:23:04 +00002655 LastDeducibleArgument - 1);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002656 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
2657 HasDefaultArg = TTP->hasDefaultArgument();
2658 else if (NonTypeTemplateParmDecl *NTTP
2659 = dyn_cast<NonTypeTemplateParmDecl>(Param))
2660 HasDefaultArg = NTTP->hasDefaultArgument();
2661 else {
2662 assert(isa<TemplateTemplateParmDecl>(Param));
2663 HasDefaultArg
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002664 = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument();
Douglas Gregor86d9a522009-09-21 16:56:56 +00002665 }
2666
2667 if (!HasDefaultArg)
2668 break;
2669 }
2670 }
2671
2672 if (LastDeducibleArgument) {
2673 // Some of the function template arguments cannot be deduced from a
2674 // function call, so we introduce an explicit template argument list
2675 // containing all of the arguments up to the first deducible argument.
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002676 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002677 AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result,
Douglas Gregor86d9a522009-09-21 16:56:56 +00002678 LastDeducibleArgument);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002679 Result.AddChunk(CodeCompletionString::CK_RightAngle);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002680 }
2681
2682 // Add the function parameters
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002683 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002684 AddFunctionParameterChunks(Ctx, Policy, Function, Result);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002685 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregora61a8792009-12-11 18:44:16 +00002686 AddFunctionTypeQualsToCompletionString(Result, Function);
Douglas Gregor218937c2011-02-01 19:23:04 +00002687 return Result.TakeString();
Douglas Gregor86d9a522009-09-21 16:56:56 +00002688 }
2689
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002690 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) {
Douglas Gregor0563c262009-09-22 23:15:58 +00002691 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002692 Ctx, Policy);
Douglas Gregordae68752011-02-01 22:57:45 +00002693 Result.AddTypedTextChunk(
2694 Result.getAllocator().CopyString(Template->getNameAsString()));
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002695 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002696 AddTemplateParameterChunks(Ctx, Policy, Template, Result);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002697 Result.AddChunk(CodeCompletionString::CK_RightAngle);
Douglas Gregor218937c2011-02-01 19:23:04 +00002698 return Result.TakeString();
Douglas Gregor86d9a522009-09-21 16:56:56 +00002699 }
2700
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002701 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) {
Douglas Gregor9630eb62009-11-17 16:44:22 +00002702 Selector Sel = Method->getSelector();
2703 if (Sel.isUnarySelector()) {
Douglas Gregordae68752011-02-01 22:57:45 +00002704 Result.AddTypedTextChunk(Result.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00002705 Sel.getNameForSlot(0)));
Douglas Gregor218937c2011-02-01 19:23:04 +00002706 return Result.TakeString();
Douglas Gregor9630eb62009-11-17 16:44:22 +00002707 }
2708
Douglas Gregor813d8342011-02-18 22:29:55 +00002709 std::string SelName = Sel.getNameForSlot(0).str();
Douglas Gregord3c68542009-11-19 01:08:35 +00002710 SelName += ':';
2711 if (StartParameter == 0)
Douglas Gregordae68752011-02-01 22:57:45 +00002712 Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName));
Douglas Gregord3c68542009-11-19 01:08:35 +00002713 else {
Douglas Gregordae68752011-02-01 22:57:45 +00002714 Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName));
Douglas Gregord3c68542009-11-19 01:08:35 +00002715
2716 // If there is only one parameter, and we're past it, add an empty
2717 // typed-text chunk since there is nothing to type.
2718 if (Method->param_size() == 1)
Douglas Gregor218937c2011-02-01 19:23:04 +00002719 Result.AddTypedTextChunk("");
Douglas Gregord3c68542009-11-19 01:08:35 +00002720 }
Douglas Gregor9630eb62009-11-17 16:44:22 +00002721 unsigned Idx = 0;
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00002722 for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(),
2723 PEnd = Method->param_end();
Douglas Gregor9630eb62009-11-17 16:44:22 +00002724 P != PEnd; (void)++P, ++Idx) {
2725 if (Idx > 0) {
Douglas Gregord3c68542009-11-19 01:08:35 +00002726 std::string Keyword;
2727 if (Idx > StartParameter)
Douglas Gregor218937c2011-02-01 19:23:04 +00002728 Result.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregor9630eb62009-11-17 16:44:22 +00002729 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx))
Benjamin Kramera0651c52011-07-26 16:59:25 +00002730 Keyword += II->getName();
Douglas Gregor9630eb62009-11-17 16:44:22 +00002731 Keyword += ":";
Douglas Gregor1f5537a2010-07-08 23:20:03 +00002732 if (Idx < StartParameter || AllParametersAreInformative)
Douglas Gregordae68752011-02-01 22:57:45 +00002733 Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword));
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +00002734 else
Douglas Gregordae68752011-02-01 22:57:45 +00002735 Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword));
Douglas Gregor9630eb62009-11-17 16:44:22 +00002736 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002737
2738 // If we're before the starting parameter, skip the placeholder.
2739 if (Idx < StartParameter)
2740 continue;
Douglas Gregor9630eb62009-11-17 16:44:22 +00002741
2742 std::string Arg;
Douglas Gregor83482d12010-08-24 16:15:59 +00002743
2744 if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity)
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002745 Arg = FormatFunctionParameter(Ctx, Policy, *P, true);
Douglas Gregor83482d12010-08-24 16:15:59 +00002746 else {
John McCallf85e1932011-06-15 23:02:42 +00002747 (*P)->getType().getAsStringInternal(Arg, Policy);
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002748 Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier())
2749 + Arg + ")";
Douglas Gregor83482d12010-08-24 16:15:59 +00002750 if (IdentifierInfo *II = (*P)->getIdentifier())
Douglas Gregoraba48082010-08-29 19:47:46 +00002751 if (DeclaringEntity || AllParametersAreInformative)
Benjamin Kramera0651c52011-07-26 16:59:25 +00002752 Arg += II->getName();
Douglas Gregor83482d12010-08-24 16:15:59 +00002753 }
2754
Douglas Gregore17794f2010-08-31 05:13:43 +00002755 if (Method->isVariadic() && (P + 1) == PEnd)
2756 Arg += ", ...";
2757
Douglas Gregor1f5537a2010-07-08 23:20:03 +00002758 if (DeclaringEntity)
Douglas Gregordae68752011-02-01 22:57:45 +00002759 Result.AddTextChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregor1f5537a2010-07-08 23:20:03 +00002760 else if (AllParametersAreInformative)
Douglas Gregordae68752011-02-01 22:57:45 +00002761 Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregor4ad96852009-11-19 07:41:15 +00002762 else
Douglas Gregordae68752011-02-01 22:57:45 +00002763 Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregor9630eb62009-11-17 16:44:22 +00002764 }
2765
Douglas Gregor2a17af02009-12-23 00:21:46 +00002766 if (Method->isVariadic()) {
Douglas Gregore17794f2010-08-31 05:13:43 +00002767 if (Method->param_size() == 0) {
2768 if (DeclaringEntity)
Douglas Gregor218937c2011-02-01 19:23:04 +00002769 Result.AddTextChunk(", ...");
Douglas Gregore17794f2010-08-31 05:13:43 +00002770 else if (AllParametersAreInformative)
Douglas Gregor218937c2011-02-01 19:23:04 +00002771 Result.AddInformativeChunk(", ...");
Douglas Gregore17794f2010-08-31 05:13:43 +00002772 else
Douglas Gregor218937c2011-02-01 19:23:04 +00002773 Result.AddPlaceholderChunk(", ...");
Douglas Gregore17794f2010-08-31 05:13:43 +00002774 }
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002775
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002776 MaybeAddSentinel(Ctx, Method, Result);
Douglas Gregor2a17af02009-12-23 00:21:46 +00002777 }
2778
Douglas Gregor218937c2011-02-01 19:23:04 +00002779 return Result.TakeString();
Douglas Gregor9630eb62009-11-17 16:44:22 +00002780 }
2781
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002782 if (Qualifier)
Douglas Gregor0563c262009-09-22 23:15:58 +00002783 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002784 Ctx, Policy);
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002785
Douglas Gregordae68752011-02-01 22:57:45 +00002786 Result.AddTypedTextChunk(
2787 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregor218937c2011-02-01 19:23:04 +00002788 return Result.TakeString();
Douglas Gregor86d9a522009-09-21 16:56:56 +00002789}
2790
Douglas Gregor86d802e2009-09-23 00:34:09 +00002791CodeCompletionString *
2792CodeCompleteConsumer::OverloadCandidate::CreateSignatureString(
2793 unsigned CurrentArg,
Douglas Gregor32be4a52010-10-11 21:37:58 +00002794 Sema &S,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002795 CodeCompletionAllocator &Allocator,
2796 CodeCompletionTUInfo &CCTUInfo) const {
Douglas Gregor8987b232011-09-27 23:30:47 +00002797 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
John McCallf85e1932011-06-15 23:02:42 +00002798
Douglas Gregor218937c2011-02-01 19:23:04 +00002799 // FIXME: Set priority, availability appropriately.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002800 CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002801 FunctionDecl *FDecl = getFunction();
Douglas Gregor8987b232011-09-27 23:30:47 +00002802 AddResultTypeChunk(S.Context, Policy, FDecl, Result);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002803 const FunctionProtoType *Proto
2804 = dyn_cast<FunctionProtoType>(getFunctionType());
2805 if (!FDecl && !Proto) {
2806 // Function without a prototype. Just give the return type and a
2807 // highlighted ellipsis.
2808 const FunctionType *FT = getFunctionType();
Douglas Gregora63f6de2011-02-01 21:15:40 +00002809 Result.AddTextChunk(GetCompletionTypeString(FT->getResultType(),
Douglas Gregor8987b232011-09-27 23:30:47 +00002810 S.Context, Policy,
Douglas Gregora63f6de2011-02-01 21:15:40 +00002811 Result.getAllocator()));
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002812 Result.AddChunk(CodeCompletionString::CK_LeftParen);
2813 Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "...");
2814 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor218937c2011-02-01 19:23:04 +00002815 return Result.TakeString();
Douglas Gregor86d802e2009-09-23 00:34:09 +00002816 }
2817
2818 if (FDecl)
Douglas Gregordae68752011-02-01 22:57:45 +00002819 Result.AddTextChunk(
2820 Result.getAllocator().CopyString(FDecl->getNameAsString()));
Douglas Gregor86d802e2009-09-23 00:34:09 +00002821 else
Douglas Gregor218937c2011-02-01 19:23:04 +00002822 Result.AddTextChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00002823 Result.getAllocator().CopyString(
John McCallf85e1932011-06-15 23:02:42 +00002824 Proto->getResultType().getAsString(Policy)));
Douglas Gregor86d802e2009-09-23 00:34:09 +00002825
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002826 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002827 unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs();
2828 for (unsigned I = 0; I != NumParams; ++I) {
2829 if (I)
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002830 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002831
2832 std::string ArgString;
2833 QualType ArgType;
2834
2835 if (FDecl) {
2836 ArgString = FDecl->getParamDecl(I)->getNameAsString();
2837 ArgType = FDecl->getParamDecl(I)->getOriginalType();
2838 } else {
2839 ArgType = Proto->getArgType(I);
2840 }
2841
John McCallf85e1932011-06-15 23:02:42 +00002842 ArgType.getAsStringInternal(ArgString, Policy);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002843
2844 if (I == CurrentArg)
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002845 Result.AddChunk(CodeCompletionString::CK_CurrentParameter,
2846 Result.getAllocator().CopyString(ArgString));
Douglas Gregor86d802e2009-09-23 00:34:09 +00002847 else
Douglas Gregordae68752011-02-01 22:57:45 +00002848 Result.AddTextChunk(Result.getAllocator().CopyString(ArgString));
Douglas Gregor86d802e2009-09-23 00:34:09 +00002849 }
2850
2851 if (Proto && Proto->isVariadic()) {
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002852 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002853 if (CurrentArg < NumParams)
Douglas Gregor218937c2011-02-01 19:23:04 +00002854 Result.AddTextChunk("...");
Douglas Gregor86d802e2009-09-23 00:34:09 +00002855 else
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002856 Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "...");
Douglas Gregor86d802e2009-09-23 00:34:09 +00002857 }
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002858 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002859
Douglas Gregor218937c2011-02-01 19:23:04 +00002860 return Result.TakeString();
Douglas Gregor86d802e2009-09-23 00:34:09 +00002861}
2862
Chris Lattner5f9e2722011-07-23 10:55:15 +00002863unsigned clang::getMacroUsagePriority(StringRef MacroName,
Douglas Gregorb05496d2010-09-20 21:11:48 +00002864 const LangOptions &LangOpts,
Douglas Gregor1827e102010-08-16 16:18:59 +00002865 bool PreferredTypeIsPointer) {
2866 unsigned Priority = CCP_Macro;
2867
Douglas Gregorb05496d2010-09-20 21:11:48 +00002868 // Treat the "nil", "Nil" and "NULL" macros as null pointer constants.
2869 if (MacroName.equals("nil") || MacroName.equals("NULL") ||
2870 MacroName.equals("Nil")) {
Douglas Gregor1827e102010-08-16 16:18:59 +00002871 Priority = CCP_Constant;
2872 if (PreferredTypeIsPointer)
2873 Priority = Priority / CCF_SimilarTypeMatch;
Douglas Gregorb05496d2010-09-20 21:11:48 +00002874 }
2875 // Treat "YES", "NO", "true", and "false" as constants.
2876 else if (MacroName.equals("YES") || MacroName.equals("NO") ||
2877 MacroName.equals("true") || MacroName.equals("false"))
2878 Priority = CCP_Constant;
2879 // Treat "bool" as a type.
2880 else if (MacroName.equals("bool"))
2881 Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0);
2882
Douglas Gregor1827e102010-08-16 16:18:59 +00002883
2884 return Priority;
2885}
2886
Dmitri Gribenko06d8c602013-01-11 20:32:41 +00002887CXCursorKind clang::getCursorKindForDecl(const Decl *D) {
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002888 if (!D)
2889 return CXCursor_UnexposedDecl;
2890
2891 switch (D->getKind()) {
2892 case Decl::Enum: return CXCursor_EnumDecl;
2893 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
2894 case Decl::Field: return CXCursor_FieldDecl;
2895 case Decl::Function:
2896 return CXCursor_FunctionDecl;
2897 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
2898 case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002899 case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl;
Douglas Gregor375bb142011-12-27 22:43:10 +00002900
Argyrios Kyrtzidisc15707d2012-01-24 21:39:26 +00002901 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002902 case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl;
2903 case Decl::ObjCMethod:
2904 return cast<ObjCMethodDecl>(D)->isInstanceMethod()
2905 ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl;
2906 case Decl::CXXMethod: return CXCursor_CXXMethod;
2907 case Decl::CXXConstructor: return CXCursor_Constructor;
2908 case Decl::CXXDestructor: return CXCursor_Destructor;
2909 case Decl::CXXConversion: return CXCursor_ConversionFunction;
2910 case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl;
Argyrios Kyrtzidisc15707d2012-01-24 21:39:26 +00002911 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002912 case Decl::ParmVar: return CXCursor_ParmDecl;
2913 case Decl::Typedef: return CXCursor_TypedefDecl;
Richard Smith162e1c12011-04-15 14:24:37 +00002914 case Decl::TypeAlias: return CXCursor_TypeAliasDecl;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002915 case Decl::Var: return CXCursor_VarDecl;
2916 case Decl::Namespace: return CXCursor_Namespace;
2917 case Decl::NamespaceAlias: return CXCursor_NamespaceAlias;
2918 case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter;
2919 case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter;
2920 case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter;
2921 case Decl::FunctionTemplate: return CXCursor_FunctionTemplate;
2922 case Decl::ClassTemplate: return CXCursor_ClassTemplate;
Argyrios Kyrtzidis2dfdb942011-09-30 17:58:23 +00002923 case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002924 case Decl::ClassTemplatePartialSpecialization:
2925 return CXCursor_ClassTemplatePartialSpecialization;
2926 case Decl::UsingDirective: return CXCursor_UsingDirective;
Douglas Gregor8e5900c2012-04-30 23:41:16 +00002927 case Decl::TranslationUnit: return CXCursor_TranslationUnit;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002928
2929 case Decl::Using:
2930 case Decl::UnresolvedUsingValue:
2931 case Decl::UnresolvedUsingTypename:
2932 return CXCursor_UsingDeclaration;
2933
Douglas Gregor352697a2011-06-03 23:08:58 +00002934 case Decl::ObjCPropertyImpl:
2935 switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) {
2936 case ObjCPropertyImplDecl::Dynamic:
2937 return CXCursor_ObjCDynamicDecl;
2938
2939 case ObjCPropertyImplDecl::Synthesize:
2940 return CXCursor_ObjCSynthesizeDecl;
2941 }
Argyrios Kyrtzidis6a010122012-10-05 00:22:24 +00002942
2943 case Decl::Import:
2944 return CXCursor_ModuleImportDecl;
Douglas Gregor352697a2011-06-03 23:08:58 +00002945
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002946 default:
Dmitri Gribenko06d8c602013-01-11 20:32:41 +00002947 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002948 switch (TD->getTagKind()) {
Joao Matos6666ed42012-08-31 18:45:21 +00002949 case TTK_Interface: // fall through
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002950 case TTK_Struct: return CXCursor_StructDecl;
2951 case TTK_Class: return CXCursor_ClassDecl;
2952 case TTK_Union: return CXCursor_UnionDecl;
2953 case TTK_Enum: return CXCursor_EnumDecl;
2954 }
2955 }
2956 }
2957
2958 return CXCursor_UnexposedDecl;
2959}
2960
Douglas Gregor590c7d52010-07-08 20:55:51 +00002961static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results,
Douglas Gregor3644d972012-10-09 16:01:50 +00002962 bool IncludeUndefined,
Douglas Gregor590c7d52010-07-08 20:55:51 +00002963 bool TargetTypeIsPointer = false) {
John McCall0a2c5e22010-08-25 06:19:51 +00002964 typedef CodeCompletionResult Result;
Douglas Gregor590c7d52010-07-08 20:55:51 +00002965
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002966 Results.EnterNewScope();
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +00002967
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002968 for (Preprocessor::macro_iterator M = PP.macro_begin(),
2969 MEnd = PP.macro_end();
Douglas Gregor590c7d52010-07-08 20:55:51 +00002970 M != MEnd; ++M) {
Douglas Gregor3644d972012-10-09 16:01:50 +00002971 if (IncludeUndefined || M->first->hasMacroDefinition())
2972 Results.AddResult(Result(M->first,
Douglas Gregor1827e102010-08-16 16:18:59 +00002973 getMacroUsagePriority(M->first->getName(),
David Blaikie4e4d0842012-03-11 07:00:24 +00002974 PP.getLangOpts(),
Douglas Gregor1827e102010-08-16 16:18:59 +00002975 TargetTypeIsPointer)));
Douglas Gregor590c7d52010-07-08 20:55:51 +00002976 }
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +00002977
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002978 Results.ExitScope();
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +00002979
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002980}
2981
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00002982static void AddPrettyFunctionResults(const LangOptions &LangOpts,
2983 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00002984 typedef CodeCompletionResult Result;
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00002985
2986 Results.EnterNewScope();
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +00002987
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00002988 Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant));
2989 Results.AddResult(Result("__FUNCTION__", CCP_Constant));
Richard Smith80ad52f2013-01-02 11:42:31 +00002990 if (LangOpts.C99 || LangOpts.CPlusPlus11)
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00002991 Results.AddResult(Result("__func__", CCP_Constant));
2992 Results.ExitScope();
2993}
2994
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00002995static void HandleCodeCompleteResults(Sema *S,
2996 CodeCompleteConsumer *CodeCompleter,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002997 CodeCompletionContext Context,
John McCall0a2c5e22010-08-25 06:19:51 +00002998 CodeCompletionResult *Results,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002999 unsigned NumResults) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00003000 if (CodeCompleter)
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003001 CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults);
Douglas Gregor86d9a522009-09-21 16:56:56 +00003002}
3003
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003004static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S,
3005 Sema::ParserCompletionContext PCC) {
3006 switch (PCC) {
John McCallf312b1e2010-08-26 23:41:50 +00003007 case Sema::PCC_Namespace:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003008 return CodeCompletionContext::CCC_TopLevel;
3009
John McCallf312b1e2010-08-26 23:41:50 +00003010 case Sema::PCC_Class:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003011 return CodeCompletionContext::CCC_ClassStructUnion;
3012
John McCallf312b1e2010-08-26 23:41:50 +00003013 case Sema::PCC_ObjCInterface:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003014 return CodeCompletionContext::CCC_ObjCInterface;
3015
John McCallf312b1e2010-08-26 23:41:50 +00003016 case Sema::PCC_ObjCImplementation:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003017 return CodeCompletionContext::CCC_ObjCImplementation;
3018
John McCallf312b1e2010-08-26 23:41:50 +00003019 case Sema::PCC_ObjCInstanceVariableList:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003020 return CodeCompletionContext::CCC_ObjCIvarList;
3021
John McCallf312b1e2010-08-26 23:41:50 +00003022 case Sema::PCC_Template:
3023 case Sema::PCC_MemberTemplate:
Douglas Gregor52779fb2010-09-23 23:01:17 +00003024 if (S.CurContext->isFileContext())
3025 return CodeCompletionContext::CCC_TopLevel;
David Blaikie7530c032012-01-17 06:56:22 +00003026 if (S.CurContext->isRecord())
Douglas Gregor52779fb2010-09-23 23:01:17 +00003027 return CodeCompletionContext::CCC_ClassStructUnion;
David Blaikie7530c032012-01-17 06:56:22 +00003028 return CodeCompletionContext::CCC_Other;
Douglas Gregor52779fb2010-09-23 23:01:17 +00003029
John McCallf312b1e2010-08-26 23:41:50 +00003030 case Sema::PCC_RecoveryInFunction:
Douglas Gregor52779fb2010-09-23 23:01:17 +00003031 return CodeCompletionContext::CCC_Recovery;
Douglas Gregora5450a02010-10-18 22:01:46 +00003032
John McCallf312b1e2010-08-26 23:41:50 +00003033 case Sema::PCC_ForInit:
David Blaikie4e4d0842012-03-11 07:00:24 +00003034 if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 ||
3035 S.getLangOpts().ObjC1)
Douglas Gregora5450a02010-10-18 22:01:46 +00003036 return CodeCompletionContext::CCC_ParenthesizedExpression;
3037 else
3038 return CodeCompletionContext::CCC_Expression;
3039
3040 case Sema::PCC_Expression:
John McCallf312b1e2010-08-26 23:41:50 +00003041 case Sema::PCC_Condition:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003042 return CodeCompletionContext::CCC_Expression;
3043
John McCallf312b1e2010-08-26 23:41:50 +00003044 case Sema::PCC_Statement:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003045 return CodeCompletionContext::CCC_Statement;
Douglas Gregor72db1082010-08-24 01:11:00 +00003046
John McCallf312b1e2010-08-26 23:41:50 +00003047 case Sema::PCC_Type:
Douglas Gregor72db1082010-08-24 01:11:00 +00003048 return CodeCompletionContext::CCC_Type;
Douglas Gregor02688102010-09-14 23:59:36 +00003049
3050 case Sema::PCC_ParenthesizedExpression:
3051 return CodeCompletionContext::CCC_ParenthesizedExpression;
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00003052
3053 case Sema::PCC_LocalDeclarationSpecifiers:
3054 return CodeCompletionContext::CCC_Type;
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003055 }
David Blaikie7530c032012-01-17 06:56:22 +00003056
3057 llvm_unreachable("Invalid ParserCompletionContext!");
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003058}
3059
Douglas Gregorf6961522010-08-27 21:18:54 +00003060/// \brief If we're in a C++ virtual member function, add completion results
3061/// that invoke the functions we override, since it's common to invoke the
3062/// overridden function as well as adding new functionality.
3063///
3064/// \param S The semantic analysis object for which we are generating results.
3065///
3066/// \param InContext This context in which the nested-name-specifier preceding
3067/// the code-completion point
3068static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext,
3069 ResultBuilder &Results) {
3070 // Look through blocks.
3071 DeclContext *CurContext = S.CurContext;
3072 while (isa<BlockDecl>(CurContext))
3073 CurContext = CurContext->getParent();
3074
3075
3076 CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext);
3077 if (!Method || !Method->isVirtual())
3078 return;
3079
3080 // We need to have names for all of the parameters, if we're going to
3081 // generate a forwarding call.
3082 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
3083 PEnd = Method->param_end();
3084 P != PEnd;
3085 ++P) {
3086 if (!(*P)->getDeclName())
3087 return;
3088 }
3089
Douglas Gregor8987b232011-09-27 23:30:47 +00003090 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
Douglas Gregorf6961522010-08-27 21:18:54 +00003091 for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(),
3092 MEnd = Method->end_overridden_methods();
3093 M != MEnd; ++M) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003094 CodeCompletionBuilder Builder(Results.getAllocator(),
3095 Results.getCodeCompletionTUInfo());
Douglas Gregorf6961522010-08-27 21:18:54 +00003096 CXXMethodDecl *Overridden = const_cast<CXXMethodDecl *>(*M);
3097 if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl())
3098 continue;
3099
3100 // If we need a nested-name-specifier, add one now.
3101 if (!InContext) {
3102 NestedNameSpecifier *NNS
3103 = getRequiredQualification(S.Context, CurContext,
3104 Overridden->getDeclContext());
3105 if (NNS) {
3106 std::string Str;
3107 llvm::raw_string_ostream OS(Str);
Douglas Gregor8987b232011-09-27 23:30:47 +00003108 NNS->print(OS, Policy);
Douglas Gregordae68752011-02-01 22:57:45 +00003109 Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str()));
Douglas Gregorf6961522010-08-27 21:18:54 +00003110 }
3111 } else if (!InContext->Equals(Overridden->getDeclContext()))
3112 continue;
3113
Douglas Gregordae68752011-02-01 22:57:45 +00003114 Builder.AddTypedTextChunk(Results.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00003115 Overridden->getNameAsString()));
3116 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregorf6961522010-08-27 21:18:54 +00003117 bool FirstParam = true;
3118 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
3119 PEnd = Method->param_end();
3120 P != PEnd; ++P) {
3121 if (FirstParam)
3122 FirstParam = false;
3123 else
Douglas Gregor218937c2011-02-01 19:23:04 +00003124 Builder.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregorf6961522010-08-27 21:18:54 +00003125
Douglas Gregordae68752011-02-01 22:57:45 +00003126 Builder.AddPlaceholderChunk(Results.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00003127 (*P)->getIdentifier()->getName()));
Douglas Gregorf6961522010-08-27 21:18:54 +00003128 }
Douglas Gregor218937c2011-02-01 19:23:04 +00003129 Builder.AddChunk(CodeCompletionString::CK_RightParen);
3130 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregorf6961522010-08-27 21:18:54 +00003131 CCP_SuperCompletion,
Douglas Gregorba103062012-03-27 23:34:16 +00003132 CXCursor_CXXMethod,
3133 CXAvailability_Available,
3134 Overridden));
Douglas Gregorf6961522010-08-27 21:18:54 +00003135 Results.Ignore(Overridden);
3136 }
3137}
3138
Douglas Gregorc5b2e582012-01-29 18:15:03 +00003139void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc,
3140 ModuleIdPath Path) {
3141 typedef CodeCompletionResult Result;
3142 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003143 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorc5b2e582012-01-29 18:15:03 +00003144 CodeCompletionContext::CCC_Other);
3145 Results.EnterNewScope();
3146
3147 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003148 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregorc5b2e582012-01-29 18:15:03 +00003149 typedef CodeCompletionResult Result;
3150 if (Path.empty()) {
3151 // Enumerate all top-level modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003152 SmallVector<Module *, 8> Modules;
Douglas Gregorc5b2e582012-01-29 18:15:03 +00003153 PP.getHeaderSearchInfo().collectAllModules(Modules);
3154 for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
3155 Builder.AddTypedTextChunk(
3156 Builder.getAllocator().CopyString(Modules[I]->Name));
3157 Results.AddResult(Result(Builder.TakeString(),
3158 CCP_Declaration,
3159 CXCursor_NotImplemented,
3160 Modules[I]->isAvailable()
3161 ? CXAvailability_Available
3162 : CXAvailability_NotAvailable));
3163 }
3164 } else {
3165 // Load the named module.
3166 Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path,
3167 Module::AllVisible,
3168 /*IsInclusionDirective=*/false);
3169 // Enumerate submodules.
3170 if (Mod) {
3171 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
3172 SubEnd = Mod->submodule_end();
3173 Sub != SubEnd; ++Sub) {
3174
3175 Builder.AddTypedTextChunk(
3176 Builder.getAllocator().CopyString((*Sub)->Name));
3177 Results.AddResult(Result(Builder.TakeString(),
3178 CCP_Declaration,
3179 CXCursor_NotImplemented,
3180 (*Sub)->isAvailable()
3181 ? CXAvailability_Available
3182 : CXAvailability_NotAvailable));
3183 }
3184 }
3185 }
3186 Results.ExitScope();
3187 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3188 Results.data(),Results.size());
3189}
3190
Douglas Gregor01dfea02010-01-10 23:08:15 +00003191void Sema::CodeCompleteOrdinaryName(Scope *S,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003192 ParserCompletionContext CompletionContext) {
Douglas Gregor218937c2011-02-01 19:23:04 +00003193 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003194 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00003195 mapCodeCompletionContext(*this, CompletionContext));
Douglas Gregorf6961522010-08-27 21:18:54 +00003196 Results.EnterNewScope();
Douglas Gregorcee9ff12010-09-20 22:39:41 +00003197
Douglas Gregor01dfea02010-01-10 23:08:15 +00003198 // Determine how to filter results, e.g., so that the names of
3199 // values (functions, enumerators, function templates, etc.) are
3200 // only allowed where we can have an expression.
3201 switch (CompletionContext) {
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003202 case PCC_Namespace:
3203 case PCC_Class:
3204 case PCC_ObjCInterface:
3205 case PCC_ObjCImplementation:
3206 case PCC_ObjCInstanceVariableList:
3207 case PCC_Template:
3208 case PCC_MemberTemplate:
Douglas Gregor72db1082010-08-24 01:11:00 +00003209 case PCC_Type:
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00003210 case PCC_LocalDeclarationSpecifiers:
Douglas Gregor01dfea02010-01-10 23:08:15 +00003211 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
3212 break;
3213
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003214 case PCC_Statement:
Douglas Gregor02688102010-09-14 23:59:36 +00003215 case PCC_ParenthesizedExpression:
Douglas Gregoreb0d0142010-08-24 23:58:17 +00003216 case PCC_Expression:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003217 case PCC_ForInit:
3218 case PCC_Condition:
David Blaikie4e4d0842012-03-11 07:00:24 +00003219 if (WantTypesInContext(CompletionContext, getLangOpts()))
Douglas Gregor4710e5b2010-05-28 00:49:12 +00003220 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3221 else
3222 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
Douglas Gregorf6961522010-08-27 21:18:54 +00003223
David Blaikie4e4d0842012-03-11 07:00:24 +00003224 if (getLangOpts().CPlusPlus)
Douglas Gregorf6961522010-08-27 21:18:54 +00003225 MaybeAddOverrideCalls(*this, /*InContext=*/0, Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00003226 break;
Douglas Gregordc845342010-05-25 05:58:43 +00003227
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003228 case PCC_RecoveryInFunction:
Douglas Gregordc845342010-05-25 05:58:43 +00003229 // Unfiltered
3230 break;
Douglas Gregor01dfea02010-01-10 23:08:15 +00003231 }
3232
Douglas Gregor3cdee122010-08-26 16:36:48 +00003233 // If we are in a C++ non-static member function, check the qualifiers on
3234 // the member function to filter/prioritize the results list.
3235 if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext))
3236 if (CurMethod->isInstance())
3237 Results.setObjectTypeQualifiers(
3238 Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers()));
3239
Douglas Gregor1ca6ae82010-01-14 01:09:38 +00003240 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00003241 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3242 CodeCompleter->includeGlobals());
Douglas Gregor2a7925c2009-12-07 09:54:55 +00003243
Douglas Gregorbca403c2010-01-13 23:51:12 +00003244 AddOrdinaryNameResults(CompletionContext, S, *this, Results);
Douglas Gregor2a7925c2009-12-07 09:54:55 +00003245 Results.ExitScope();
3246
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00003247 switch (CompletionContext) {
Douglas Gregor02688102010-09-14 23:59:36 +00003248 case PCC_ParenthesizedExpression:
Douglas Gregor72db1082010-08-24 01:11:00 +00003249 case PCC_Expression:
3250 case PCC_Statement:
3251 case PCC_RecoveryInFunction:
3252 if (S->getFnParent())
David Blaikie4e4d0842012-03-11 07:00:24 +00003253 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregor72db1082010-08-24 01:11:00 +00003254 break;
3255
3256 case PCC_Namespace:
3257 case PCC_Class:
3258 case PCC_ObjCInterface:
3259 case PCC_ObjCImplementation:
3260 case PCC_ObjCInstanceVariableList:
3261 case PCC_Template:
3262 case PCC_MemberTemplate:
3263 case PCC_ForInit:
3264 case PCC_Condition:
3265 case PCC_Type:
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00003266 case PCC_LocalDeclarationSpecifiers:
Douglas Gregor72db1082010-08-24 01:11:00 +00003267 break;
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00003268 }
3269
Douglas Gregor0c8296d2009-11-07 00:00:49 +00003270 if (CodeCompleter->includeMacros())
Douglas Gregor3644d972012-10-09 16:01:50 +00003271 AddMacroResults(PP, Results, false);
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00003272
Douglas Gregorcee9ff12010-09-20 22:39:41 +00003273 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003274 Results.data(),Results.size());
Douglas Gregor791215b2009-09-21 20:51:25 +00003275}
3276
Douglas Gregorc7b6d882010-09-16 15:14:18 +00003277static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
3278 ParsedType Receiver,
3279 IdentifierInfo **SelIdents,
3280 unsigned NumSelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00003281 bool AtArgumentExpression,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00003282 bool IsSuper,
3283 ResultBuilder &Results);
3284
3285void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS,
3286 bool AllowNonIdentifiers,
3287 bool AllowNestedNameSpecifiers) {
John McCall0a2c5e22010-08-25 06:19:51 +00003288 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00003289 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003290 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00003291 AllowNestedNameSpecifiers
3292 ? CodeCompletionContext::CCC_PotentiallyQualifiedName
3293 : CodeCompletionContext::CCC_Name);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003294 Results.EnterNewScope();
3295
3296 // Type qualifiers can come after names.
3297 Results.AddResult(Result("const"));
3298 Results.AddResult(Result("volatile"));
David Blaikie4e4d0842012-03-11 07:00:24 +00003299 if (getLangOpts().C99)
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003300 Results.AddResult(Result("restrict"));
3301
David Blaikie4e4d0842012-03-11 07:00:24 +00003302 if (getLangOpts().CPlusPlus) {
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003303 if (AllowNonIdentifiers) {
3304 Results.AddResult(Result("operator"));
3305 }
3306
3307 // Add nested-name-specifiers.
3308 if (AllowNestedNameSpecifiers) {
3309 Results.allowNestedNameSpecifiers();
Douglas Gregor52779fb2010-09-23 23:01:17 +00003310 Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003311 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3312 LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer,
3313 CodeCompleter->includeGlobals());
Douglas Gregor52779fb2010-09-23 23:01:17 +00003314 Results.setFilter(0);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003315 }
3316 }
3317 Results.ExitScope();
3318
Douglas Gregorc7b6d882010-09-16 15:14:18 +00003319 // If we're in a context where we might have an expression (rather than a
3320 // declaration), and what we've seen so far is an Objective-C type that could
3321 // be a receiver of a class message, this may be a class message send with
3322 // the initial opening bracket '[' missing. Add appropriate completions.
3323 if (AllowNonIdentifiers && !AllowNestedNameSpecifiers &&
3324 DS.getTypeSpecType() == DeclSpec::TST_typename &&
3325 DS.getStorageClassSpecAsWritten() == DeclSpec::SCS_unspecified &&
3326 !DS.isThreadSpecified() && !DS.isExternInLinkageSpec() &&
3327 DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified &&
3328 DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
3329 DS.getTypeQualifiers() == 0 &&
3330 S &&
3331 (S->getFlags() & Scope::DeclScope) != 0 &&
3332 (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope |
3333 Scope::FunctionPrototypeScope |
3334 Scope::AtCatchScope)) == 0) {
3335 ParsedType T = DS.getRepAsType();
3336 if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00003337 AddClassMessageCompletions(*this, S, T, 0, 0, false, false, Results);
Douglas Gregorc7b6d882010-09-16 15:14:18 +00003338 }
3339
Douglas Gregor4497dd42010-08-24 04:59:56 +00003340 // Note that we intentionally suppress macro results here, since we do not
3341 // encourage using macros to produce the names of entities.
3342
Douglas Gregor52779fb2010-09-23 23:01:17 +00003343 HandleCodeCompleteResults(this, CodeCompleter,
3344 Results.getCompletionContext(),
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003345 Results.data(), Results.size());
3346}
3347
Douglas Gregorfb629412010-08-23 21:17:50 +00003348struct Sema::CodeCompleteExpressionData {
3349 CodeCompleteExpressionData(QualType PreferredType = QualType())
3350 : PreferredType(PreferredType), IntegralConstantExpression(false),
3351 ObjCCollection(false) { }
3352
3353 QualType PreferredType;
3354 bool IntegralConstantExpression;
3355 bool ObjCCollection;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003356 SmallVector<Decl *, 4> IgnoreDecls;
Douglas Gregorfb629412010-08-23 21:17:50 +00003357};
3358
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003359/// \brief Perform code-completion in an expression context when we know what
3360/// type we're looking for.
Douglas Gregorfb629412010-08-23 21:17:50 +00003361void Sema::CodeCompleteExpression(Scope *S,
3362 const CodeCompleteExpressionData &Data) {
Douglas Gregor218937c2011-02-01 19:23:04 +00003363 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003364 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00003365 CodeCompletionContext::CCC_Expression);
Douglas Gregorfb629412010-08-23 21:17:50 +00003366 if (Data.ObjCCollection)
3367 Results.setFilter(&ResultBuilder::IsObjCCollection);
3368 else if (Data.IntegralConstantExpression)
Douglas Gregorf9578432010-07-28 21:50:18 +00003369 Results.setFilter(&ResultBuilder::IsIntegralConstantValue);
David Blaikie4e4d0842012-03-11 07:00:24 +00003370 else if (WantTypesInContext(PCC_Expression, getLangOpts()))
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003371 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3372 else
3373 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
Douglas Gregorfb629412010-08-23 21:17:50 +00003374
3375 if (!Data.PreferredType.isNull())
3376 Results.setPreferredType(Data.PreferredType.getNonReferenceType());
3377
3378 // Ignore any declarations that we were told that we don't care about.
3379 for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I)
3380 Results.Ignore(Data.IgnoreDecls[I]);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003381
3382 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00003383 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3384 CodeCompleter->includeGlobals());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003385
3386 Results.EnterNewScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003387 AddOrdinaryNameResults(PCC_Expression, S, *this, Results);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003388 Results.ExitScope();
3389
Douglas Gregor590c7d52010-07-08 20:55:51 +00003390 bool PreferredTypeIsPointer = false;
Douglas Gregorfb629412010-08-23 21:17:50 +00003391 if (!Data.PreferredType.isNull())
3392 PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType()
3393 || Data.PreferredType->isMemberPointerType()
3394 || Data.PreferredType->isBlockPointerType();
Douglas Gregor590c7d52010-07-08 20:55:51 +00003395
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00003396 if (S->getFnParent() &&
3397 !Data.ObjCCollection &&
3398 !Data.IntegralConstantExpression)
David Blaikie4e4d0842012-03-11 07:00:24 +00003399 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00003400
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003401 if (CodeCompleter->includeMacros())
Douglas Gregor3644d972012-10-09 16:01:50 +00003402 AddMacroResults(PP, Results, false, PreferredTypeIsPointer);
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003403 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregorfb629412010-08-23 21:17:50 +00003404 CodeCompletionContext(CodeCompletionContext::CCC_Expression,
3405 Data.PreferredType),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003406 Results.data(),Results.size());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003407}
3408
Douglas Gregorac5fd842010-09-18 01:28:11 +00003409void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) {
3410 if (E.isInvalid())
3411 CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction);
David Blaikie4e4d0842012-03-11 07:00:24 +00003412 else if (getLangOpts().ObjC1)
Douglas Gregorac5fd842010-09-18 01:28:11 +00003413 CodeCompleteObjCInstanceMessage(S, E.take(), 0, 0, false);
Douglas Gregor78edf512010-09-15 16:23:04 +00003414}
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003415
Douglas Gregor73449212010-12-09 23:01:55 +00003416/// \brief The set of properties that have already been added, referenced by
3417/// property name.
3418typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet;
3419
Douglas Gregorb92a4082012-06-12 13:44:08 +00003420/// \brief Retrieve the container definition, if any?
3421static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) {
3422 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
3423 if (Interface->hasDefinition())
3424 return Interface->getDefinition();
3425
3426 return Interface;
3427 }
3428
3429 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3430 if (Protocol->hasDefinition())
3431 return Protocol->getDefinition();
3432
3433 return Protocol;
3434 }
3435 return Container;
3436}
3437
3438static void AddObjCProperties(ObjCContainerDecl *Container,
Douglas Gregor322328b2009-11-18 22:32:06 +00003439 bool AllowCategories,
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003440 bool AllowNullaryMethods,
Douglas Gregor95ac6552009-11-18 01:29:26 +00003441 DeclContext *CurContext,
Douglas Gregor73449212010-12-09 23:01:55 +00003442 AddedPropertiesSet &AddedProperties,
Douglas Gregor95ac6552009-11-18 01:29:26 +00003443 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00003444 typedef CodeCompletionResult Result;
Douglas Gregor95ac6552009-11-18 01:29:26 +00003445
Douglas Gregorb92a4082012-06-12 13:44:08 +00003446 // Retrieve the definition.
3447 Container = getContainerDef(Container);
3448
Douglas Gregor95ac6552009-11-18 01:29:26 +00003449 // Add properties in this container.
3450 for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(),
3451 PEnd = Container->prop_end();
3452 P != PEnd;
Douglas Gregor73449212010-12-09 23:01:55 +00003453 ++P) {
3454 if (AddedProperties.insert(P->getIdentifier()))
Douglas Gregord1f09b42013-01-31 04:52:16 +00003455 Results.MaybeAddResult(Result(*P, Results.getBasePriority(*P), 0),
3456 CurContext);
Douglas Gregor73449212010-12-09 23:01:55 +00003457 }
Douglas Gregor95ac6552009-11-18 01:29:26 +00003458
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003459 // Add nullary methods
3460 if (AllowNullaryMethods) {
3461 ASTContext &Context = Container->getASTContext();
Douglas Gregor8987b232011-09-27 23:30:47 +00003462 PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003463 for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
3464 MEnd = Container->meth_end();
3465 M != MEnd; ++M) {
3466 if (M->getSelector().isUnarySelector())
3467 if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0))
3468 if (AddedProperties.insert(Name)) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003469 CodeCompletionBuilder Builder(Results.getAllocator(),
3470 Results.getCodeCompletionTUInfo());
David Blaikie581deb32012-06-06 20:45:41 +00003471 AddResultTypeChunk(Context, Policy, *M, Builder);
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003472 Builder.AddTypedTextChunk(
3473 Results.getAllocator().CopyString(Name->getName()));
3474
David Blaikie581deb32012-06-06 20:45:41 +00003475 Results.MaybeAddResult(Result(Builder.TakeString(), *M,
Douglas Gregorba103062012-03-27 23:34:16 +00003476 CCP_MemberDeclaration + CCD_MethodAsProperty),
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003477 CurContext);
3478 }
3479 }
3480 }
3481
3482
Douglas Gregor95ac6552009-11-18 01:29:26 +00003483 // Add properties in referenced protocols.
3484 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3485 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
3486 PEnd = Protocol->protocol_end();
3487 P != PEnd; ++P)
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003488 AddObjCProperties(*P, AllowCategories, AllowNullaryMethods, CurContext,
3489 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003490 } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){
Douglas Gregor322328b2009-11-18 22:32:06 +00003491 if (AllowCategories) {
3492 // Look through categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003493 for (ObjCInterfaceDecl::known_categories_iterator
3494 Cat = IFace->known_categories_begin(),
3495 CatEnd = IFace->known_categories_end();
3496 Cat != CatEnd; ++Cat)
3497 AddObjCProperties(*Cat, AllowCategories, AllowNullaryMethods,
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003498 CurContext, AddedProperties, Results);
Douglas Gregor322328b2009-11-18 22:32:06 +00003499 }
Douglas Gregor95ac6552009-11-18 01:29:26 +00003500
3501 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00003502 for (ObjCInterfaceDecl::all_protocol_iterator
3503 I = IFace->all_referenced_protocol_begin(),
3504 E = IFace->all_referenced_protocol_end(); I != E; ++I)
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003505 AddObjCProperties(*I, AllowCategories, AllowNullaryMethods, CurContext,
3506 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003507
3508 // Look in the superclass.
3509 if (IFace->getSuperClass())
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003510 AddObjCProperties(IFace->getSuperClass(), AllowCategories,
3511 AllowNullaryMethods, CurContext,
Douglas Gregor73449212010-12-09 23:01:55 +00003512 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003513 } else if (const ObjCCategoryDecl *Category
3514 = dyn_cast<ObjCCategoryDecl>(Container)) {
3515 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00003516 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
3517 PEnd = Category->protocol_end();
Douglas Gregor95ac6552009-11-18 01:29:26 +00003518 P != PEnd; ++P)
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003519 AddObjCProperties(*P, AllowCategories, AllowNullaryMethods, CurContext,
3520 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003521 }
3522}
3523
Douglas Gregorf5cd27d2012-01-23 15:59:30 +00003524void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base,
Douglas Gregor81b747b2009-09-17 21:32:03 +00003525 SourceLocation OpLoc,
3526 bool IsArrow) {
Douglas Gregorf5cd27d2012-01-23 15:59:30 +00003527 if (!Base || !CodeCompleter)
Douglas Gregor81b747b2009-09-17 21:32:03 +00003528 return;
3529
Douglas Gregorf5cd27d2012-01-23 15:59:30 +00003530 ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow);
3531 if (ConvertedBase.isInvalid())
3532 return;
3533 Base = ConvertedBase.get();
3534
John McCall0a2c5e22010-08-25 06:19:51 +00003535 typedef CodeCompletionResult Result;
Douglas Gregor86d9a522009-09-21 16:56:56 +00003536
Douglas Gregor81b747b2009-09-17 21:32:03 +00003537 QualType BaseType = Base->getType();
Douglas Gregor86d9a522009-09-21 16:56:56 +00003538
3539 if (IsArrow) {
3540 if (const PointerType *Ptr = BaseType->getAs<PointerType>())
3541 BaseType = Ptr->getPointeeType();
3542 else if (BaseType->isObjCObjectPointerType())
Douglas Gregor3cdee122010-08-26 16:36:48 +00003543 /*Do nothing*/ ;
Douglas Gregor86d9a522009-09-21 16:56:56 +00003544 else
3545 return;
3546 }
3547
Douglas Gregor3da626b2011-07-07 16:03:39 +00003548 enum CodeCompletionContext::Kind contextKind;
3549
3550 if (IsArrow) {
3551 contextKind = CodeCompletionContext::CCC_ArrowMemberAccess;
3552 }
3553 else {
3554 if (BaseType->isObjCObjectPointerType() ||
3555 BaseType->isObjCObjectOrInterfaceType()) {
3556 contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess;
3557 }
3558 else {
3559 contextKind = CodeCompletionContext::CCC_DotMemberAccess;
3560 }
3561 }
3562
Douglas Gregor218937c2011-02-01 19:23:04 +00003563 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003564 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor3da626b2011-07-07 16:03:39 +00003565 CodeCompletionContext(contextKind,
Douglas Gregor52779fb2010-09-23 23:01:17 +00003566 BaseType),
3567 &ResultBuilder::IsMember);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003568 Results.EnterNewScope();
3569 if (const RecordType *Record = BaseType->getAs<RecordType>()) {
Douglas Gregor3cdee122010-08-26 16:36:48 +00003570 // Indicate that we are performing a member access, and the cv-qualifiers
3571 // for the base object type.
3572 Results.setObjectTypeQualifiers(BaseType.getQualifiers());
3573
Douglas Gregor95ac6552009-11-18 01:29:26 +00003574 // Access to a C/C++ class, struct, or union.
Douglas Gregor45bcd432010-01-14 03:21:49 +00003575 Results.allowNestedNameSpecifiers();
Douglas Gregor0cc84042010-01-14 15:47:35 +00003576 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00003577 LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer,
3578 CodeCompleter->includeGlobals());
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003579
David Blaikie4e4d0842012-03-11 07:00:24 +00003580 if (getLangOpts().CPlusPlus) {
Douglas Gregor95ac6552009-11-18 01:29:26 +00003581 if (!Results.empty()) {
3582 // The "template" keyword can follow "->" or "." in the grammar.
3583 // However, we only want to suggest the template keyword if something
3584 // is dependent.
3585 bool IsDependent = BaseType->isDependentType();
3586 if (!IsDependent) {
3587 for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent())
3588 if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) {
3589 IsDependent = Ctx->isDependentContext();
3590 break;
3591 }
3592 }
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003593
Douglas Gregor95ac6552009-11-18 01:29:26 +00003594 if (IsDependent)
Douglas Gregora4477812010-01-14 16:01:26 +00003595 Results.AddResult(Result("template"));
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003596 }
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003597 }
Douglas Gregor95ac6552009-11-18 01:29:26 +00003598 } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) {
3599 // Objective-C property reference.
Douglas Gregor73449212010-12-09 23:01:55 +00003600 AddedPropertiesSet AddedProperties;
Douglas Gregor95ac6552009-11-18 01:29:26 +00003601
3602 // Add property results based on our interface.
3603 const ObjCObjectPointerType *ObjCPtr
3604 = BaseType->getAsObjCInterfacePointerType();
3605 assert(ObjCPtr && "Non-NULL pointer guaranteed above!");
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003606 AddObjCProperties(ObjCPtr->getInterfaceDecl(), true,
3607 /*AllowNullaryMethods=*/true, CurContext,
Douglas Gregor73449212010-12-09 23:01:55 +00003608 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003609
3610 // Add properties from the protocols in a qualified interface.
3611 for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(),
3612 E = ObjCPtr->qual_end();
3613 I != E; ++I)
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003614 AddObjCProperties(*I, true, /*AllowNullaryMethods=*/true, CurContext,
3615 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003616 } else if ((IsArrow && BaseType->isObjCObjectPointerType()) ||
John McCallc12c5bb2010-05-15 11:32:37 +00003617 (!IsArrow && BaseType->isObjCObjectType())) {
Douglas Gregor95ac6552009-11-18 01:29:26 +00003618 // Objective-C instance variable access.
3619 ObjCInterfaceDecl *Class = 0;
3620 if (const ObjCObjectPointerType *ObjCPtr
3621 = BaseType->getAs<ObjCObjectPointerType>())
3622 Class = ObjCPtr->getInterfaceDecl();
3623 else
John McCallc12c5bb2010-05-15 11:32:37 +00003624 Class = BaseType->getAs<ObjCObjectType>()->getInterface();
Douglas Gregor95ac6552009-11-18 01:29:26 +00003625
3626 // Add all ivars from this class and its superclasses.
Douglas Gregor80f4f4c2010-01-14 16:08:12 +00003627 if (Class) {
3628 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3629 Results.setFilter(&ResultBuilder::IsObjCIvar);
Douglas Gregor8071e422010-08-15 06:18:01 +00003630 LookupVisibleDecls(Class, LookupMemberName, Consumer,
3631 CodeCompleter->includeGlobals());
Douglas Gregor95ac6552009-11-18 01:29:26 +00003632 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00003633 }
Douglas Gregor95ac6552009-11-18 01:29:26 +00003634
3635 // FIXME: How do we cope with isa?
3636
3637 Results.ExitScope();
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003638
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003639 // Hand off the results found for code completion.
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003640 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor52779fb2010-09-23 23:01:17 +00003641 Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003642 Results.data(),Results.size());
Douglas Gregor81b747b2009-09-17 21:32:03 +00003643}
3644
Douglas Gregor374929f2009-09-18 15:37:17 +00003645void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) {
3646 if (!CodeCompleter)
3647 return;
3648
Douglas Gregor86d9a522009-09-21 16:56:56 +00003649 ResultBuilder::LookupFilter Filter = 0;
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003650 enum CodeCompletionContext::Kind ContextKind
3651 = CodeCompletionContext::CCC_Other;
Douglas Gregor374929f2009-09-18 15:37:17 +00003652 switch ((DeclSpec::TST)TagSpec) {
3653 case DeclSpec::TST_enum:
Douglas Gregor86d9a522009-09-21 16:56:56 +00003654 Filter = &ResultBuilder::IsEnum;
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003655 ContextKind = CodeCompletionContext::CCC_EnumTag;
Douglas Gregor374929f2009-09-18 15:37:17 +00003656 break;
3657
3658 case DeclSpec::TST_union:
Douglas Gregor86d9a522009-09-21 16:56:56 +00003659 Filter = &ResultBuilder::IsUnion;
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003660 ContextKind = CodeCompletionContext::CCC_UnionTag;
Douglas Gregor374929f2009-09-18 15:37:17 +00003661 break;
3662
3663 case DeclSpec::TST_struct:
Douglas Gregor374929f2009-09-18 15:37:17 +00003664 case DeclSpec::TST_class:
Joao Matos6666ed42012-08-31 18:45:21 +00003665 case DeclSpec::TST_interface:
Douglas Gregor86d9a522009-09-21 16:56:56 +00003666 Filter = &ResultBuilder::IsClassOrStruct;
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003667 ContextKind = CodeCompletionContext::CCC_ClassOrStructTag;
Douglas Gregor374929f2009-09-18 15:37:17 +00003668 break;
3669
3670 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003671 llvm_unreachable("Unknown type specifier kind in CodeCompleteTag");
Douglas Gregor374929f2009-09-18 15:37:17 +00003672 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00003673
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003674 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3675 CodeCompleter->getCodeCompletionTUInfo(), ContextKind);
Douglas Gregor5d2fc402010-01-14 03:27:13 +00003676 CodeCompletionDeclConsumer Consumer(Results, CurContext);
John McCall0d6b1642010-04-23 18:46:30 +00003677
3678 // First pass: look for tags.
3679 Results.setFilter(Filter);
Douglas Gregor8071e422010-08-15 06:18:01 +00003680 LookupVisibleDecls(S, LookupTagName, Consumer,
3681 CodeCompleter->includeGlobals());
John McCall0d6b1642010-04-23 18:46:30 +00003682
Douglas Gregor8071e422010-08-15 06:18:01 +00003683 if (CodeCompleter->includeGlobals()) {
3684 // Second pass: look for nested name specifiers.
3685 Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
3686 LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer);
3687 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00003688
Douglas Gregor52779fb2010-09-23 23:01:17 +00003689 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003690 Results.data(),Results.size());
Douglas Gregor374929f2009-09-18 15:37:17 +00003691}
3692
Douglas Gregor1a480c42010-08-27 17:35:51 +00003693void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) {
Douglas Gregor218937c2011-02-01 19:23:04 +00003694 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003695 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00003696 CodeCompletionContext::CCC_TypeQualifiers);
Douglas Gregor1a480c42010-08-27 17:35:51 +00003697 Results.EnterNewScope();
3698 if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const))
3699 Results.AddResult("const");
3700 if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile))
3701 Results.AddResult("volatile");
David Blaikie4e4d0842012-03-11 07:00:24 +00003702 if (getLangOpts().C99 &&
Douglas Gregor1a480c42010-08-27 17:35:51 +00003703 !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict))
3704 Results.AddResult("restrict");
3705 Results.ExitScope();
3706 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor52779fb2010-09-23 23:01:17 +00003707 Results.getCompletionContext(),
Douglas Gregor1a480c42010-08-27 17:35:51 +00003708 Results.data(), Results.size());
3709}
3710
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003711void Sema::CodeCompleteCase(Scope *S) {
John McCall781472f2010-08-25 08:40:02 +00003712 if (getCurFunction()->SwitchStack.empty() || !CodeCompleter)
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003713 return;
John McCalla8e0cd82011-08-06 07:30:58 +00003714
John McCall781472f2010-08-25 08:40:02 +00003715 SwitchStmt *Switch = getCurFunction()->SwitchStack.back();
John McCalla8e0cd82011-08-06 07:30:58 +00003716 QualType type = Switch->getCond()->IgnoreImplicit()->getType();
3717 if (!type->isEnumeralType()) {
3718 CodeCompleteExpressionData Data(type);
Douglas Gregorfb629412010-08-23 21:17:50 +00003719 Data.IntegralConstantExpression = true;
3720 CodeCompleteExpression(S, Data);
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003721 return;
Douglas Gregorf9578432010-07-28 21:50:18 +00003722 }
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003723
3724 // Code-complete the cases of a switch statement over an enumeration type
3725 // by providing the list of
John McCalla8e0cd82011-08-06 07:30:58 +00003726 EnumDecl *Enum = type->castAs<EnumType>()->getDecl();
Douglas Gregorb92a4082012-06-12 13:44:08 +00003727 if (EnumDecl *Def = Enum->getDefinition())
3728 Enum = Def;
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003729
3730 // Determine which enumerators we have already seen in the switch statement.
3731 // FIXME: Ideally, we would also be able to look *past* the code-completion
3732 // token, in case we are code-completing in the middle of the switch and not
3733 // at the end. However, we aren't able to do so at the moment.
3734 llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen;
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00003735 NestedNameSpecifier *Qualifier = 0;
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003736 for (SwitchCase *SC = Switch->getSwitchCaseList(); SC;
3737 SC = SC->getNextSwitchCase()) {
3738 CaseStmt *Case = dyn_cast<CaseStmt>(SC);
3739 if (!Case)
3740 continue;
3741
3742 Expr *CaseVal = Case->getLHS()->IgnoreParenCasts();
3743 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal))
3744 if (EnumConstantDecl *Enumerator
3745 = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
3746 // We look into the AST of the case statement to determine which
3747 // enumerator was named. Alternatively, we could compute the value of
3748 // the integral constant expression, then compare it against the
3749 // values of each enumerator. However, value-based approach would not
3750 // work as well with C++ templates where enumerators declared within a
3751 // template are type- and value-dependent.
3752 EnumeratorsSeen.insert(Enumerator);
3753
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00003754 // If this is a qualified-id, keep track of the nested-name-specifier
3755 // so that we can reproduce it as part of code completion, e.g.,
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003756 //
3757 // switch (TagD.getKind()) {
3758 // case TagDecl::TK_enum:
3759 // break;
3760 // case XXX
3761 //
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00003762 // At the XXX, our completions are TagDecl::TK_union,
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003763 // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union,
3764 // TK_struct, and TK_class.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003765 Qualifier = DRE->getQualifier();
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003766 }
3767 }
3768
David Blaikie4e4d0842012-03-11 07:00:24 +00003769 if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) {
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00003770 // If there are no prior enumerators in C++, check whether we have to
3771 // qualify the names of the enumerators that we suggest, because they
3772 // may not be visible in this scope.
Douglas Gregorb223d8c2012-02-01 05:02:47 +00003773 Qualifier = getRequiredQualification(Context, CurContext, Enum);
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00003774 }
3775
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003776 // Add any enumerators that have not yet been mentioned.
Douglas Gregor218937c2011-02-01 19:23:04 +00003777 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003778 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00003779 CodeCompletionContext::CCC_Expression);
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003780 Results.EnterNewScope();
3781 for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(),
3782 EEnd = Enum->enumerator_end();
3783 E != EEnd; ++E) {
David Blaikie581deb32012-06-06 20:45:41 +00003784 if (EnumeratorsSeen.count(*E))
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003785 continue;
3786
Douglas Gregord1f09b42013-01-31 04:52:16 +00003787 CodeCompletionResult R(*E, CCP_EnumInCase, Qualifier);
Douglas Gregor5c722c702011-02-18 23:30:37 +00003788 Results.AddResult(R, CurContext, 0, false);
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003789 }
3790 Results.ExitScope();
Douglas Gregor2f880e42010-04-06 20:02:15 +00003791
Douglas Gregor3da626b2011-07-07 16:03:39 +00003792 //We need to make sure we're setting the right context,
3793 //so only say we include macros if the code completer says we do
3794 enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other;
3795 if (CodeCompleter->includeMacros()) {
Douglas Gregor3644d972012-10-09 16:01:50 +00003796 AddMacroResults(PP, Results, false);
Douglas Gregor3da626b2011-07-07 16:03:39 +00003797 kind = CodeCompletionContext::CCC_OtherWithMacros;
3798 }
3799
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003800 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor3da626b2011-07-07 16:03:39 +00003801 kind,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003802 Results.data(),Results.size());
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003803}
3804
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003805namespace {
3806 struct IsBetterOverloadCandidate {
3807 Sema &S;
John McCall5769d612010-02-08 23:07:23 +00003808 SourceLocation Loc;
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003809
3810 public:
John McCall5769d612010-02-08 23:07:23 +00003811 explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc)
3812 : S(S), Loc(Loc) { }
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003813
3814 bool
3815 operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const {
John McCall120d63c2010-08-24 20:38:10 +00003816 return isBetterOverloadCandidate(S, X, Y, Loc);
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003817 }
3818 };
3819}
3820
Ahmed Charles13a140c2012-02-25 11:00:22 +00003821static bool anyNullArguments(llvm::ArrayRef<Expr*> Args) {
3822 if (Args.size() && !Args.data())
Douglas Gregord28dcd72010-05-30 06:10:08 +00003823 return true;
Ahmed Charles13a140c2012-02-25 11:00:22 +00003824
3825 for (unsigned I = 0; I != Args.size(); ++I)
Douglas Gregord28dcd72010-05-30 06:10:08 +00003826 if (!Args[I])
3827 return true;
Ahmed Charles13a140c2012-02-25 11:00:22 +00003828
Douglas Gregord28dcd72010-05-30 06:10:08 +00003829 return false;
3830}
3831
Richard Trieuf81e5a92011-09-09 02:00:50 +00003832void Sema::CodeCompleteCall(Scope *S, Expr *FnIn,
Ahmed Charles13a140c2012-02-25 11:00:22 +00003833 llvm::ArrayRef<Expr *> Args) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003834 if (!CodeCompleter)
3835 return;
Douglas Gregoref96eac2009-12-11 19:06:04 +00003836
3837 // When we're code-completing for a call, we fall back to ordinary
3838 // name code-completion whenever we can't produce specific
3839 // results. We may want to revisit this strategy in the future,
3840 // e.g., by merging the two kinds of results.
3841
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003842 Expr *Fn = (Expr *)FnIn;
Douglas Gregoref96eac2009-12-11 19:06:04 +00003843
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003844 // Ignore type-dependent call expressions entirely.
Ahmed Charles13a140c2012-02-25 11:00:22 +00003845 if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) ||
3846 Expr::hasAnyTypeDependentArguments(Args)) {
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003847 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003848 return;
Douglas Gregoref96eac2009-12-11 19:06:04 +00003849 }
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003850
John McCall3b4294e2009-12-16 12:17:52 +00003851 // Build an overload candidate set based on the functions we find.
John McCall5769d612010-02-08 23:07:23 +00003852 SourceLocation Loc = Fn->getExprLoc();
3853 OverloadCandidateSet CandidateSet(Loc);
John McCall3b4294e2009-12-16 12:17:52 +00003854
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003855 // FIXME: What if we're calling something that isn't a function declaration?
3856 // FIXME: What if we're calling a pseudo-destructor?
3857 // FIXME: What if we're calling a member function?
3858
Douglas Gregorc0265402010-01-21 15:46:19 +00003859 typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003860 SmallVector<ResultCandidate, 8> Results;
Douglas Gregorc0265402010-01-21 15:46:19 +00003861
John McCall3b4294e2009-12-16 12:17:52 +00003862 Expr *NakedFn = Fn->IgnoreParenCasts();
3863 if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn))
Ahmed Charles13a140c2012-02-25 11:00:22 +00003864 AddOverloadedCallCandidates(ULE, Args, CandidateSet,
John McCall3b4294e2009-12-16 12:17:52 +00003865 /*PartialOverloading=*/ true);
3866 else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) {
3867 FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
Douglas Gregorc0265402010-01-21 15:46:19 +00003868 if (FDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003869 if (!getLangOpts().CPlusPlus ||
Douglas Gregord28dcd72010-05-30 06:10:08 +00003870 !FDecl->getType()->getAs<FunctionProtoType>())
Douglas Gregorc0265402010-01-21 15:46:19 +00003871 Results.push_back(ResultCandidate(FDecl));
3872 else
John McCall86820f52010-01-26 01:37:31 +00003873 // FIXME: access?
Ahmed Charles13a140c2012-02-25 11:00:22 +00003874 AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), Args,
3875 CandidateSet, false, /*PartialOverloading*/true);
Douglas Gregorc0265402010-01-21 15:46:19 +00003876 }
John McCall3b4294e2009-12-16 12:17:52 +00003877 }
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003878
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003879 QualType ParamType;
3880
Douglas Gregorc0265402010-01-21 15:46:19 +00003881 if (!CandidateSet.empty()) {
3882 // Sort the overload candidate set by placing the best overloads first.
3883 std::stable_sort(CandidateSet.begin(), CandidateSet.end(),
John McCall5769d612010-02-08 23:07:23 +00003884 IsBetterOverloadCandidate(*this, Loc));
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003885
Douglas Gregorc0265402010-01-21 15:46:19 +00003886 // Add the remaining viable overload candidates as code-completion reslults.
3887 for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
3888 CandEnd = CandidateSet.end();
3889 Cand != CandEnd; ++Cand) {
3890 if (Cand->Viable)
3891 Results.push_back(ResultCandidate(Cand->Function));
3892 }
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003893
3894 // From the viable candidates, try to determine the type of this parameter.
3895 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
3896 if (const FunctionType *FType = Results[I].getFunctionType())
3897 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FType))
Ahmed Charles13a140c2012-02-25 11:00:22 +00003898 if (Args.size() < Proto->getNumArgs()) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003899 if (ParamType.isNull())
Ahmed Charles13a140c2012-02-25 11:00:22 +00003900 ParamType = Proto->getArgType(Args.size());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003901 else if (!Context.hasSameUnqualifiedType(
3902 ParamType.getNonReferenceType(),
Ahmed Charles13a140c2012-02-25 11:00:22 +00003903 Proto->getArgType(Args.size()).getNonReferenceType())) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003904 ParamType = QualType();
3905 break;
3906 }
3907 }
3908 }
3909 } else {
3910 // Try to determine the parameter type from the type of the expression
3911 // being called.
3912 QualType FunctionType = Fn->getType();
3913 if (const PointerType *Ptr = FunctionType->getAs<PointerType>())
3914 FunctionType = Ptr->getPointeeType();
3915 else if (const BlockPointerType *BlockPtr
3916 = FunctionType->getAs<BlockPointerType>())
3917 FunctionType = BlockPtr->getPointeeType();
3918 else if (const MemberPointerType *MemPtr
3919 = FunctionType->getAs<MemberPointerType>())
3920 FunctionType = MemPtr->getPointeeType();
3921
3922 if (const FunctionProtoType *Proto
3923 = FunctionType->getAs<FunctionProtoType>()) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00003924 if (Args.size() < Proto->getNumArgs())
3925 ParamType = Proto->getArgType(Args.size());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003926 }
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003927 }
Douglas Gregoref96eac2009-12-11 19:06:04 +00003928
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003929 if (ParamType.isNull())
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003930 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003931 else
3932 CodeCompleteExpression(S, ParamType);
3933
Douglas Gregor2e4c7a52010-04-06 20:19:47 +00003934 if (!Results.empty())
Ahmed Charles13a140c2012-02-25 11:00:22 +00003935 CodeCompleter->ProcessOverloadCandidates(*this, Args.size(), Results.data(),
Douglas Gregoref96eac2009-12-11 19:06:04 +00003936 Results.size());
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003937}
3938
John McCalld226f652010-08-21 09:40:31 +00003939void Sema::CodeCompleteInitializer(Scope *S, Decl *D) {
3940 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003941 if (!VD) {
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003942 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003943 return;
3944 }
3945
3946 CodeCompleteExpression(S, VD->getType());
3947}
3948
3949void Sema::CodeCompleteReturn(Scope *S) {
3950 QualType ResultType;
3951 if (isa<BlockDecl>(CurContext)) {
3952 if (BlockScopeInfo *BSI = getCurBlock())
3953 ResultType = BSI->ReturnType;
3954 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext))
3955 ResultType = Function->getResultType();
3956 else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext))
3957 ResultType = Method->getResultType();
3958
3959 if (ResultType.isNull())
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003960 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003961 else
3962 CodeCompleteExpression(S, ResultType);
3963}
3964
Douglas Gregord2d8be62011-07-30 08:36:53 +00003965void Sema::CodeCompleteAfterIf(Scope *S) {
Douglas Gregord2d8be62011-07-30 08:36:53 +00003966 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003967 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregord2d8be62011-07-30 08:36:53 +00003968 mapCodeCompletionContext(*this, PCC_Statement));
3969 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3970 Results.EnterNewScope();
3971
3972 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3973 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3974 CodeCompleter->includeGlobals());
3975
3976 AddOrdinaryNameResults(PCC_Statement, S, *this, Results);
3977
3978 // "else" block
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003979 CodeCompletionBuilder Builder(Results.getAllocator(),
3980 Results.getCodeCompletionTUInfo());
Douglas Gregord2d8be62011-07-30 08:36:53 +00003981 Builder.AddTypedTextChunk("else");
Douglas Gregorf11641a2012-02-16 17:49:04 +00003982 if (Results.includeCodePatterns()) {
3983 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3984 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
3985 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3986 Builder.AddPlaceholderChunk("statements");
3987 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3988 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
3989 }
Douglas Gregord2d8be62011-07-30 08:36:53 +00003990 Results.AddResult(Builder.TakeString());
3991
3992 // "else if" block
3993 Builder.AddTypedTextChunk("else");
3994 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3995 Builder.AddTextChunk("if");
3996 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3997 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikie4e4d0842012-03-11 07:00:24 +00003998 if (getLangOpts().CPlusPlus)
Douglas Gregord2d8be62011-07-30 08:36:53 +00003999 Builder.AddPlaceholderChunk("condition");
4000 else
4001 Builder.AddPlaceholderChunk("expression");
4002 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorf11641a2012-02-16 17:49:04 +00004003 if (Results.includeCodePatterns()) {
4004 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4005 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4006 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
4007 Builder.AddPlaceholderChunk("statements");
4008 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
4009 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4010 }
Douglas Gregord2d8be62011-07-30 08:36:53 +00004011 Results.AddResult(Builder.TakeString());
4012
4013 Results.ExitScope();
4014
4015 if (S->getFnParent())
David Blaikie4e4d0842012-03-11 07:00:24 +00004016 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregord2d8be62011-07-30 08:36:53 +00004017
4018 if (CodeCompleter->includeMacros())
Douglas Gregor3644d972012-10-09 16:01:50 +00004019 AddMacroResults(PP, Results, false);
Douglas Gregord2d8be62011-07-30 08:36:53 +00004020
4021 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4022 Results.data(),Results.size());
4023}
4024
Richard Trieuf81e5a92011-09-09 02:00:50 +00004025void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00004026 if (LHS)
4027 CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType());
4028 else
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004029 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00004030}
4031
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004032void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS,
Douglas Gregor81b747b2009-09-17 21:32:03 +00004033 bool EnteringContext) {
4034 if (!SS.getScopeRep() || !CodeCompleter)
4035 return;
4036
Douglas Gregor86d9a522009-09-21 16:56:56 +00004037 DeclContext *Ctx = computeDeclContext(SS, EnteringContext);
4038 if (!Ctx)
4039 return;
Douglas Gregord1cd31a2009-12-11 18:28:39 +00004040
4041 // Try to instantiate any non-dependent declaration contexts before
4042 // we look in them.
John McCall77bb1aa2010-05-01 00:40:08 +00004043 if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx))
Douglas Gregord1cd31a2009-12-11 18:28:39 +00004044 return;
4045
Douglas Gregor218937c2011-02-01 19:23:04 +00004046 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004047 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004048 CodeCompletionContext::CCC_Name);
Douglas Gregorf6961522010-08-27 21:18:54 +00004049 Results.EnterNewScope();
Douglas Gregor52779fb2010-09-23 23:01:17 +00004050
Douglas Gregor86d9a522009-09-21 16:56:56 +00004051 // The "template" keyword can follow "::" in the grammar, but only
4052 // put it into the grammar if the nested-name-specifier is dependent.
4053 NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
4054 if (!Results.empty() && NNS->isDependent())
Douglas Gregora4477812010-01-14 16:01:26 +00004055 Results.AddResult("template");
Douglas Gregorf6961522010-08-27 21:18:54 +00004056
4057 // Add calls to overridden virtual functions, if there are any.
4058 //
4059 // FIXME: This isn't wonderful, because we don't know whether we're actually
4060 // in a context that permits expressions. This is a general issue with
4061 // qualified-id completions.
4062 if (!EnteringContext)
4063 MaybeAddOverrideCalls(*this, Ctx, Results);
4064 Results.ExitScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004065
Douglas Gregorf6961522010-08-27 21:18:54 +00004066 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4067 LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer);
4068
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004069 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor430d7a12011-07-25 17:48:11 +00004070 Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004071 Results.data(),Results.size());
Douglas Gregor81b747b2009-09-17 21:32:03 +00004072}
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004073
4074void Sema::CodeCompleteUsing(Scope *S) {
4075 if (!CodeCompleter)
4076 return;
4077
Douglas Gregor218937c2011-02-01 19:23:04 +00004078 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004079 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00004080 CodeCompletionContext::CCC_PotentiallyQualifiedName,
4081 &ResultBuilder::IsNestedNameSpecifier);
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004082 Results.EnterNewScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004083
4084 // If we aren't in class scope, we could see the "namespace" keyword.
4085 if (!S->isClassScope())
John McCall0a2c5e22010-08-25 06:19:51 +00004086 Results.AddResult(CodeCompletionResult("namespace"));
Douglas Gregor86d9a522009-09-21 16:56:56 +00004087
4088 // After "using", we can see anything that would start a
4089 // nested-name-specifier.
Douglas Gregor5d2fc402010-01-14 03:27:13 +00004090 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00004091 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4092 CodeCompleter->includeGlobals());
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004093 Results.ExitScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004094
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004095 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004096 CodeCompletionContext::CCC_PotentiallyQualifiedName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004097 Results.data(),Results.size());
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004098}
4099
4100void Sema::CodeCompleteUsingDirective(Scope *S) {
4101 if (!CodeCompleter)
4102 return;
4103
Douglas Gregor86d9a522009-09-21 16:56:56 +00004104 // After "using namespace", we expect to see a namespace name or namespace
4105 // alias.
Douglas Gregor218937c2011-02-01 19:23:04 +00004106 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004107 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004108 CodeCompletionContext::CCC_Namespace,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004109 &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004110 Results.EnterNewScope();
Douglas Gregor5d2fc402010-01-14 03:27:13 +00004111 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00004112 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4113 CodeCompleter->includeGlobals());
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004114 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004115 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor8071e422010-08-15 06:18:01 +00004116 CodeCompletionContext::CCC_Namespace,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004117 Results.data(),Results.size());
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004118}
4119
4120void Sema::CodeCompleteNamespaceDecl(Scope *S) {
4121 if (!CodeCompleter)
4122 return;
4123
Douglas Gregor86d9a522009-09-21 16:56:56 +00004124 DeclContext *Ctx = (DeclContext *)S->getEntity();
4125 if (!S->getParent())
4126 Ctx = Context.getTranslationUnitDecl();
4127
Douglas Gregor52779fb2010-09-23 23:01:17 +00004128 bool SuppressedGlobalResults
4129 = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx);
4130
Douglas Gregor218937c2011-02-01 19:23:04 +00004131 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004132 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00004133 SuppressedGlobalResults
4134 ? CodeCompletionContext::CCC_Namespace
4135 : CodeCompletionContext::CCC_Other,
4136 &ResultBuilder::IsNamespace);
4137
4138 if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00004139 // We only want to see those namespaces that have already been defined
4140 // within this scope, because its likely that the user is creating an
4141 // extended namespace declaration. Keep track of the most recent
4142 // definition of each namespace.
4143 std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest;
4144 for (DeclContext::specific_decl_iterator<NamespaceDecl>
4145 NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end());
4146 NS != NSEnd; ++NS)
David Blaikie581deb32012-06-06 20:45:41 +00004147 OrigToLatest[NS->getOriginalNamespace()] = *NS;
Douglas Gregor86d9a522009-09-21 16:56:56 +00004148
4149 // Add the most recent definition (or extended definition) of each
4150 // namespace to the list of results.
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004151 Results.EnterNewScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004152 for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator
Douglas Gregorba103062012-03-27 23:34:16 +00004153 NS = OrigToLatest.begin(),
4154 NSEnd = OrigToLatest.end();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004155 NS != NSEnd; ++NS)
Douglas Gregord1f09b42013-01-31 04:52:16 +00004156 Results.AddResult(CodeCompletionResult(
4157 NS->second, Results.getBasePriority(NS->second), 0),
Douglas Gregor608300b2010-01-14 16:14:35 +00004158 CurContext, 0, false);
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004159 Results.ExitScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004160 }
4161
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004162 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004163 Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004164 Results.data(),Results.size());
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004165}
4166
4167void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) {
4168 if (!CodeCompleter)
4169 return;
4170
Douglas Gregor86d9a522009-09-21 16:56:56 +00004171 // After "namespace", we expect to see a namespace or alias.
Douglas Gregor218937c2011-02-01 19:23:04 +00004172 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004173 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004174 CodeCompletionContext::CCC_Namespace,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004175 &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregor5d2fc402010-01-14 03:27:13 +00004176 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00004177 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4178 CodeCompleter->includeGlobals());
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004179 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004180 Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004181 Results.data(),Results.size());
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004182}
4183
Douglas Gregored8d3222009-09-18 20:05:18 +00004184void Sema::CodeCompleteOperatorName(Scope *S) {
4185 if (!CodeCompleter)
4186 return;
Douglas Gregor86d9a522009-09-21 16:56:56 +00004187
John McCall0a2c5e22010-08-25 06:19:51 +00004188 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00004189 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004190 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004191 CodeCompletionContext::CCC_Type,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004192 &ResultBuilder::IsType);
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004193 Results.EnterNewScope();
Douglas Gregored8d3222009-09-18 20:05:18 +00004194
Douglas Gregor86d9a522009-09-21 16:56:56 +00004195 // Add the names of overloadable operators.
4196#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
4197 if (std::strcmp(Spelling, "?")) \
Douglas Gregora4477812010-01-14 16:01:26 +00004198 Results.AddResult(Result(Spelling));
Douglas Gregor86d9a522009-09-21 16:56:56 +00004199#include "clang/Basic/OperatorKinds.def"
4200
4201 // Add any type names visible from the current scope
Douglas Gregor45bcd432010-01-14 03:21:49 +00004202 Results.allowNestedNameSpecifiers();
Douglas Gregor5d2fc402010-01-14 03:27:13 +00004203 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00004204 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4205 CodeCompleter->includeGlobals());
Douglas Gregor86d9a522009-09-21 16:56:56 +00004206
4207 // Add any type specifiers
David Blaikie4e4d0842012-03-11 07:00:24 +00004208 AddTypeSpecifierResults(getLangOpts(), Results);
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004209 Results.ExitScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004210
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004211 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor8071e422010-08-15 06:18:01 +00004212 CodeCompletionContext::CCC_Type,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004213 Results.data(),Results.size());
Douglas Gregored8d3222009-09-18 20:05:18 +00004214}
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004215
Douglas Gregor0133f522010-08-28 00:00:50 +00004216void Sema::CodeCompleteConstructorInitializer(Decl *ConstructorD,
Sean Huntcbb67482011-01-08 20:30:50 +00004217 CXXCtorInitializer** Initializers,
Douglas Gregor0133f522010-08-28 00:00:50 +00004218 unsigned NumInitializers) {
Douglas Gregor8987b232011-09-27 23:30:47 +00004219 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregor0133f522010-08-28 00:00:50 +00004220 CXXConstructorDecl *Constructor
4221 = static_cast<CXXConstructorDecl *>(ConstructorD);
4222 if (!Constructor)
4223 return;
4224
Douglas Gregor218937c2011-02-01 19:23:04 +00004225 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004226 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00004227 CodeCompletionContext::CCC_PotentiallyQualifiedName);
Douglas Gregor0133f522010-08-28 00:00:50 +00004228 Results.EnterNewScope();
4229
4230 // Fill in any already-initialized fields or base classes.
4231 llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields;
4232 llvm::SmallPtrSet<CanQualType, 4> InitializedBases;
4233 for (unsigned I = 0; I != NumInitializers; ++I) {
4234 if (Initializers[I]->isBaseInitializer())
4235 InitializedBases.insert(
4236 Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0)));
4237 else
Francois Pichet00eb3f92010-12-04 09:14:42 +00004238 InitializedFields.insert(cast<FieldDecl>(
4239 Initializers[I]->getAnyMember()));
Douglas Gregor0133f522010-08-28 00:00:50 +00004240 }
4241
4242 // Add completions for base classes.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004243 CodeCompletionBuilder Builder(Results.getAllocator(),
4244 Results.getCodeCompletionTUInfo());
Douglas Gregor0c431c82010-08-29 19:27:27 +00004245 bool SawLastInitializer = (NumInitializers == 0);
Douglas Gregor0133f522010-08-28 00:00:50 +00004246 CXXRecordDecl *ClassDecl = Constructor->getParent();
4247 for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(),
4248 BaseEnd = ClassDecl->bases_end();
4249 Base != BaseEnd; ++Base) {
Douglas Gregor0c431c82010-08-29 19:27:27 +00004250 if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) {
4251 SawLastInitializer
4252 = NumInitializers > 0 &&
4253 Initializers[NumInitializers - 1]->isBaseInitializer() &&
4254 Context.hasSameUnqualifiedType(Base->getType(),
4255 QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0));
Douglas Gregor0133f522010-08-28 00:00:50 +00004256 continue;
Douglas Gregor0c431c82010-08-29 19:27:27 +00004257 }
Douglas Gregor0133f522010-08-28 00:00:50 +00004258
Douglas Gregor218937c2011-02-01 19:23:04 +00004259 Builder.AddTypedTextChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00004260 Results.getAllocator().CopyString(
John McCallf85e1932011-06-15 23:02:42 +00004261 Base->getType().getAsString(Policy)));
Douglas Gregor218937c2011-02-01 19:23:04 +00004262 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4263 Builder.AddPlaceholderChunk("args");
4264 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4265 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor0c431c82010-08-29 19:27:27 +00004266 SawLastInitializer? CCP_NextInitializer
4267 : CCP_MemberDeclaration));
4268 SawLastInitializer = false;
Douglas Gregor0133f522010-08-28 00:00:50 +00004269 }
4270
4271 // Add completions for virtual base classes.
4272 for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(),
4273 BaseEnd = ClassDecl->vbases_end();
4274 Base != BaseEnd; ++Base) {
Douglas Gregor0c431c82010-08-29 19:27:27 +00004275 if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) {
4276 SawLastInitializer
4277 = NumInitializers > 0 &&
4278 Initializers[NumInitializers - 1]->isBaseInitializer() &&
4279 Context.hasSameUnqualifiedType(Base->getType(),
4280 QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0));
Douglas Gregor0133f522010-08-28 00:00:50 +00004281 continue;
Douglas Gregor0c431c82010-08-29 19:27:27 +00004282 }
Douglas Gregor0133f522010-08-28 00:00:50 +00004283
Douglas Gregor218937c2011-02-01 19:23:04 +00004284 Builder.AddTypedTextChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00004285 Builder.getAllocator().CopyString(
John McCallf85e1932011-06-15 23:02:42 +00004286 Base->getType().getAsString(Policy)));
Douglas Gregor218937c2011-02-01 19:23:04 +00004287 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4288 Builder.AddPlaceholderChunk("args");
4289 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4290 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor0c431c82010-08-29 19:27:27 +00004291 SawLastInitializer? CCP_NextInitializer
4292 : CCP_MemberDeclaration));
4293 SawLastInitializer = false;
Douglas Gregor0133f522010-08-28 00:00:50 +00004294 }
4295
4296 // Add completions for members.
4297 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
4298 FieldEnd = ClassDecl->field_end();
4299 Field != FieldEnd; ++Field) {
Douglas Gregor0c431c82010-08-29 19:27:27 +00004300 if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))) {
4301 SawLastInitializer
4302 = NumInitializers > 0 &&
Francois Pichet00eb3f92010-12-04 09:14:42 +00004303 Initializers[NumInitializers - 1]->isAnyMemberInitializer() &&
David Blaikie581deb32012-06-06 20:45:41 +00004304 Initializers[NumInitializers - 1]->getAnyMember() == *Field;
Douglas Gregor0133f522010-08-28 00:00:50 +00004305 continue;
Douglas Gregor0c431c82010-08-29 19:27:27 +00004306 }
Douglas Gregor0133f522010-08-28 00:00:50 +00004307
4308 if (!Field->getDeclName())
4309 continue;
4310
Douglas Gregordae68752011-02-01 22:57:45 +00004311 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00004312 Field->getIdentifier()->getName()));
4313 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4314 Builder.AddPlaceholderChunk("args");
4315 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4316 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor0c431c82010-08-29 19:27:27 +00004317 SawLastInitializer? CCP_NextInitializer
Douglas Gregora67e03f2010-09-09 21:42:20 +00004318 : CCP_MemberDeclaration,
Douglas Gregorba103062012-03-27 23:34:16 +00004319 CXCursor_MemberRef,
4320 CXAvailability_Available,
David Blaikie581deb32012-06-06 20:45:41 +00004321 *Field));
Douglas Gregor0c431c82010-08-29 19:27:27 +00004322 SawLastInitializer = false;
Douglas Gregor0133f522010-08-28 00:00:50 +00004323 }
4324 Results.ExitScope();
4325
Douglas Gregor52779fb2010-09-23 23:01:17 +00004326 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor0133f522010-08-28 00:00:50 +00004327 Results.data(), Results.size());
4328}
4329
Douglas Gregor81f3bff2012-02-15 15:34:24 +00004330/// \brief Determine whether this scope denotes a namespace.
4331static bool isNamespaceScope(Scope *S) {
4332 DeclContext *DC = static_cast<DeclContext *>(S->getEntity());
4333 if (!DC)
4334 return false;
4335
4336 return DC->isFileContext();
4337}
4338
4339void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro,
4340 bool AfterAmpersand) {
4341 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004342 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor81f3bff2012-02-15 15:34:24 +00004343 CodeCompletionContext::CCC_Other);
4344 Results.EnterNewScope();
4345
4346 // Note what has already been captured.
4347 llvm::SmallPtrSet<IdentifierInfo *, 4> Known;
4348 bool IncludedThis = false;
4349 for (SmallVectorImpl<LambdaCapture>::iterator C = Intro.Captures.begin(),
4350 CEnd = Intro.Captures.end();
4351 C != CEnd; ++C) {
4352 if (C->Kind == LCK_This) {
4353 IncludedThis = true;
4354 continue;
4355 }
4356
4357 Known.insert(C->Id);
4358 }
4359
4360 // Look for other capturable variables.
4361 for (; S && !isNamespaceScope(S); S = S->getParent()) {
4362 for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
4363 D != DEnd; ++D) {
4364 VarDecl *Var = dyn_cast<VarDecl>(*D);
4365 if (!Var ||
4366 !Var->hasLocalStorage() ||
4367 Var->hasAttr<BlocksAttr>())
4368 continue;
4369
4370 if (Known.insert(Var->getIdentifier()))
Douglas Gregord1f09b42013-01-31 04:52:16 +00004371 Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration),
4372 CurContext, 0, false);
Douglas Gregor81f3bff2012-02-15 15:34:24 +00004373 }
4374 }
4375
4376 // Add 'this', if it would be valid.
4377 if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy)
4378 addThisCompletion(*this, Results);
4379
4380 Results.ExitScope();
4381
4382 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4383 Results.data(), Results.size());
4384}
4385
James Dennetta40f7922012-06-14 03:11:41 +00004386/// Macro that optionally prepends an "@" to the string literal passed in via
4387/// Keyword, depending on whether NeedAt is true or false.
4388#define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword)
4389
Douglas Gregorbca403c2010-01-13 23:51:12 +00004390static void AddObjCImplementationResults(const LangOptions &LangOpts,
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004391 ResultBuilder &Results,
4392 bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004393 typedef CodeCompletionResult Result;
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004394 // Since we have an implementation, we can end it.
James Dennetta40f7922012-06-14 03:11:41 +00004395 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004396
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004397 CodeCompletionBuilder Builder(Results.getAllocator(),
4398 Results.getCodeCompletionTUInfo());
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004399 if (LangOpts.ObjC2) {
4400 // @dynamic
James Dennetta40f7922012-06-14 03:11:41 +00004401 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004402 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4403 Builder.AddPlaceholderChunk("property");
4404 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004405
4406 // @synthesize
James Dennetta40f7922012-06-14 03:11:41 +00004407 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004408 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4409 Builder.AddPlaceholderChunk("property");
4410 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004411 }
4412}
4413
Douglas Gregorbca403c2010-01-13 23:51:12 +00004414static void AddObjCInterfaceResults(const LangOptions &LangOpts,
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004415 ResultBuilder &Results,
4416 bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004417 typedef CodeCompletionResult Result;
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004418
4419 // Since we have an interface or protocol, we can end it.
James Dennetta40f7922012-06-14 03:11:41 +00004420 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004421
4422 if (LangOpts.ObjC2) {
4423 // @property
James Dennetta40f7922012-06-14 03:11:41 +00004424 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property")));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004425
4426 // @required
James Dennetta40f7922012-06-14 03:11:41 +00004427 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required")));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004428
4429 // @optional
James Dennetta40f7922012-06-14 03:11:41 +00004430 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional")));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004431 }
4432}
4433
Douglas Gregorbca403c2010-01-13 23:51:12 +00004434static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004435 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004436 CodeCompletionBuilder Builder(Results.getAllocator(),
4437 Results.getCodeCompletionTUInfo());
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004438
4439 // @class name ;
James Dennetta40f7922012-06-14 03:11:41 +00004440 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004441 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4442 Builder.AddPlaceholderChunk("name");
4443 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004444
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004445 if (Results.includeCodePatterns()) {
4446 // @interface name
4447 // FIXME: Could introduce the whole pattern, including superclasses and
4448 // such.
James Dennetta40f7922012-06-14 03:11:41 +00004449 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004450 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4451 Builder.AddPlaceholderChunk("class");
4452 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004453
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004454 // @protocol name
James Dennetta40f7922012-06-14 03:11:41 +00004455 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004456 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4457 Builder.AddPlaceholderChunk("protocol");
4458 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004459
4460 // @implementation name
James Dennetta40f7922012-06-14 03:11:41 +00004461 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004462 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4463 Builder.AddPlaceholderChunk("class");
4464 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004465 }
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004466
4467 // @compatibility_alias name
James Dennetta40f7922012-06-14 03:11:41 +00004468 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004469 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4470 Builder.AddPlaceholderChunk("alias");
4471 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4472 Builder.AddPlaceholderChunk("class");
4473 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004474}
4475
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004476void Sema::CodeCompleteObjCAtDirective(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00004477 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004478 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004479 CodeCompletionContext::CCC_Other);
Douglas Gregorc464ae82009-12-07 09:27:33 +00004480 Results.EnterNewScope();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004481 if (isa<ObjCImplDecl>(CurContext))
David Blaikie4e4d0842012-03-11 07:00:24 +00004482 AddObjCImplementationResults(getLangOpts(), Results, false);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004483 else if (CurContext->isObjCContainer())
David Blaikie4e4d0842012-03-11 07:00:24 +00004484 AddObjCInterfaceResults(getLangOpts(), Results, false);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004485 else
Douglas Gregorbca403c2010-01-13 23:51:12 +00004486 AddObjCTopLevelResults(Results, false);
Douglas Gregorc464ae82009-12-07 09:27:33 +00004487 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004488 HandleCodeCompleteResults(this, CodeCompleter,
4489 CodeCompletionContext::CCC_Other,
4490 Results.data(),Results.size());
Douglas Gregorc464ae82009-12-07 09:27:33 +00004491}
4492
Douglas Gregorbca403c2010-01-13 23:51:12 +00004493static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004494 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004495 CodeCompletionBuilder Builder(Results.getAllocator(),
4496 Results.getCodeCompletionTUInfo());
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004497
4498 // @encode ( type-name )
Douglas Gregor8ca72082011-10-18 21:20:17 +00004499 const char *EncodeType = "char[]";
David Blaikie4e4d0842012-03-11 07:00:24 +00004500 if (Results.getSema().getLangOpts().CPlusPlus ||
4501 Results.getSema().getLangOpts().ConstStrings)
Jordan Rose1f6e22d2012-06-15 18:19:56 +00004502 EncodeType = "const char[]";
Douglas Gregor8ca72082011-10-18 21:20:17 +00004503 Builder.AddResultTypeChunk(EncodeType);
James Dennetta40f7922012-06-14 03:11:41 +00004504 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004505 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4506 Builder.AddPlaceholderChunk("type-name");
4507 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4508 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004509
4510 // @protocol ( protocol-name )
Douglas Gregor8ca72082011-10-18 21:20:17 +00004511 Builder.AddResultTypeChunk("Protocol *");
James Dennetta40f7922012-06-14 03:11:41 +00004512 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004513 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4514 Builder.AddPlaceholderChunk("protocol-name");
4515 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4516 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004517
4518 // @selector ( selector )
Douglas Gregor8ca72082011-10-18 21:20:17 +00004519 Builder.AddResultTypeChunk("SEL");
James Dennetta40f7922012-06-14 03:11:41 +00004520 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004521 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4522 Builder.AddPlaceholderChunk("selector");
4523 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4524 Results.AddResult(Result(Builder.TakeString()));
Jordan Rose1f6e22d2012-06-15 18:19:56 +00004525
4526 // @"string"
4527 Builder.AddResultTypeChunk("NSString *");
4528 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\""));
4529 Builder.AddPlaceholderChunk("string");
4530 Builder.AddTextChunk("\"");
4531 Results.AddResult(Result(Builder.TakeString()));
4532
Douglas Gregor79615892012-07-17 23:24:47 +00004533 // @[objects, ...]
Jordan Rose1f6e22d2012-06-15 18:19:56 +00004534 Builder.AddResultTypeChunk("NSArray *");
James Dennetta40f7922012-06-14 03:11:41 +00004535 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"["));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004536 Builder.AddPlaceholderChunk("objects, ...");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004537 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
4538 Results.AddResult(Result(Builder.TakeString()));
4539
Douglas Gregor79615892012-07-17 23:24:47 +00004540 // @{key : object, ...}
Jordan Rose1f6e22d2012-06-15 18:19:56 +00004541 Builder.AddResultTypeChunk("NSDictionary *");
James Dennetta40f7922012-06-14 03:11:41 +00004542 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{"));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004543 Builder.AddPlaceholderChunk("key");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004544 Builder.AddChunk(CodeCompletionString::CK_Colon);
4545 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4546 Builder.AddPlaceholderChunk("object, ...");
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004547 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4548 Results.AddResult(Result(Builder.TakeString()));
Jordan Rose1f6e22d2012-06-15 18:19:56 +00004549
Douglas Gregor79615892012-07-17 23:24:47 +00004550 // @(expression)
Jordan Rose1f6e22d2012-06-15 18:19:56 +00004551 Builder.AddResultTypeChunk("id");
4552 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "("));
Jordan Rose1f6e22d2012-06-15 18:19:56 +00004553 Builder.AddPlaceholderChunk("expression");
Jordan Rose1f6e22d2012-06-15 18:19:56 +00004554 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4555 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004556}
4557
Douglas Gregorbca403c2010-01-13 23:51:12 +00004558static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004559 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004560 CodeCompletionBuilder Builder(Results.getAllocator(),
4561 Results.getCodeCompletionTUInfo());
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004562
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004563 if (Results.includeCodePatterns()) {
4564 // @try { statements } @catch ( declaration ) { statements } @finally
4565 // { statements }
James Dennetta40f7922012-06-14 03:11:41 +00004566 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004567 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4568 Builder.AddPlaceholderChunk("statements");
4569 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4570 Builder.AddTextChunk("@catch");
4571 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4572 Builder.AddPlaceholderChunk("parameter");
4573 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4574 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4575 Builder.AddPlaceholderChunk("statements");
4576 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4577 Builder.AddTextChunk("@finally");
4578 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4579 Builder.AddPlaceholderChunk("statements");
4580 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4581 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004582 }
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004583
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004584 // @throw
James Dennetta40f7922012-06-14 03:11:41 +00004585 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004586 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4587 Builder.AddPlaceholderChunk("expression");
4588 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004589
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004590 if (Results.includeCodePatterns()) {
4591 // @synchronized ( expression ) { statements }
James Dennetta40f7922012-06-14 03:11:41 +00004592 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004593 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4594 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4595 Builder.AddPlaceholderChunk("expression");
4596 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4597 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4598 Builder.AddPlaceholderChunk("statements");
4599 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4600 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004601 }
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004602}
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004603
Douglas Gregorbca403c2010-01-13 23:51:12 +00004604static void AddObjCVisibilityResults(const LangOptions &LangOpts,
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004605 ResultBuilder &Results,
4606 bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004607 typedef CodeCompletionResult Result;
James Dennetta40f7922012-06-14 03:11:41 +00004608 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private")));
4609 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected")));
4610 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public")));
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004611 if (LangOpts.ObjC2)
James Dennetta40f7922012-06-14 03:11:41 +00004612 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package")));
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004613}
4614
4615void Sema::CodeCompleteObjCAtVisibility(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00004616 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004617 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004618 CodeCompletionContext::CCC_Other);
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004619 Results.EnterNewScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00004620 AddObjCVisibilityResults(getLangOpts(), Results, false);
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004621 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004622 HandleCodeCompleteResults(this, CodeCompleter,
4623 CodeCompletionContext::CCC_Other,
4624 Results.data(),Results.size());
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004625}
4626
4627void Sema::CodeCompleteObjCAtStatement(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00004628 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004629 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004630 CodeCompletionContext::CCC_Other);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004631 Results.EnterNewScope();
Douglas Gregorbca403c2010-01-13 23:51:12 +00004632 AddObjCStatementResults(Results, false);
4633 AddObjCExpressionResults(Results, false);
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004634 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004635 HandleCodeCompleteResults(this, CodeCompleter,
4636 CodeCompletionContext::CCC_Other,
4637 Results.data(),Results.size());
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004638}
4639
4640void Sema::CodeCompleteObjCAtExpression(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00004641 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004642 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004643 CodeCompletionContext::CCC_Other);
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004644 Results.EnterNewScope();
Douglas Gregorbca403c2010-01-13 23:51:12 +00004645 AddObjCExpressionResults(Results, false);
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004646 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004647 HandleCodeCompleteResults(this, CodeCompleter,
4648 CodeCompletionContext::CCC_Other,
4649 Results.data(),Results.size());
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004650}
4651
Douglas Gregor988358f2009-11-19 00:14:45 +00004652/// \brief Determine whether the addition of the given flag to an Objective-C
4653/// property's attributes will cause a conflict.
Bill Wendlingad017fa2012-12-20 19:22:21 +00004654static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
Douglas Gregor988358f2009-11-19 00:14:45 +00004655 // Check if we've already added this flag.
Bill Wendlingad017fa2012-12-20 19:22:21 +00004656 if (Attributes & NewFlag)
Douglas Gregor988358f2009-11-19 00:14:45 +00004657 return true;
4658
Bill Wendlingad017fa2012-12-20 19:22:21 +00004659 Attributes |= NewFlag;
Douglas Gregor988358f2009-11-19 00:14:45 +00004660
4661 // Check for collisions with "readonly".
Bill Wendlingad017fa2012-12-20 19:22:21 +00004662 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
4663 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Douglas Gregor988358f2009-11-19 00:14:45 +00004664 return true;
4665
Jordan Rosed7403a72012-08-20 20:01:13 +00004666 // Check for more than one of { assign, copy, retain, strong, weak }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00004667 unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00004668 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Douglas Gregor988358f2009-11-19 00:14:45 +00004669 ObjCDeclSpec::DQ_PR_copy |
Jordan Rosed7403a72012-08-20 20:01:13 +00004670 ObjCDeclSpec::DQ_PR_retain |
4671 ObjCDeclSpec::DQ_PR_strong |
4672 ObjCDeclSpec::DQ_PR_weak);
Douglas Gregor988358f2009-11-19 00:14:45 +00004673 if (AssignCopyRetMask &&
4674 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign &&
John McCallf85e1932011-06-15 23:02:42 +00004675 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained &&
Douglas Gregor988358f2009-11-19 00:14:45 +00004676 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy &&
John McCallf85e1932011-06-15 23:02:42 +00004677 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain &&
Jordan Rosed7403a72012-08-20 20:01:13 +00004678 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong &&
4679 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak)
Douglas Gregor988358f2009-11-19 00:14:45 +00004680 return true;
4681
4682 return false;
4683}
4684
Douglas Gregora93b1082009-11-18 23:08:07 +00004685void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
Steve Naroffece8e712009-10-08 21:55:05 +00004686 if (!CodeCompleter)
4687 return;
Douglas Gregord3c68542009-11-19 01:08:35 +00004688
Bill Wendlingad017fa2012-12-20 19:22:21 +00004689 unsigned Attributes = ODS.getPropertyAttributes();
Steve Naroffece8e712009-10-08 21:55:05 +00004690
Douglas Gregor218937c2011-02-01 19:23:04 +00004691 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004692 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004693 CodeCompletionContext::CCC_Other);
Steve Naroffece8e712009-10-08 21:55:05 +00004694 Results.EnterNewScope();
Bill Wendlingad017fa2012-12-20 19:22:21 +00004695 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly))
John McCall0a2c5e22010-08-25 06:19:51 +00004696 Results.AddResult(CodeCompletionResult("readonly"));
Bill Wendlingad017fa2012-12-20 19:22:21 +00004697 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign))
John McCall0a2c5e22010-08-25 06:19:51 +00004698 Results.AddResult(CodeCompletionResult("assign"));
Bill Wendlingad017fa2012-12-20 19:22:21 +00004699 if (!ObjCPropertyFlagConflicts(Attributes,
John McCallf85e1932011-06-15 23:02:42 +00004700 ObjCDeclSpec::DQ_PR_unsafe_unretained))
4701 Results.AddResult(CodeCompletionResult("unsafe_unretained"));
Bill Wendlingad017fa2012-12-20 19:22:21 +00004702 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite))
John McCall0a2c5e22010-08-25 06:19:51 +00004703 Results.AddResult(CodeCompletionResult("readwrite"));
Bill Wendlingad017fa2012-12-20 19:22:21 +00004704 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain))
John McCall0a2c5e22010-08-25 06:19:51 +00004705 Results.AddResult(CodeCompletionResult("retain"));
Bill Wendlingad017fa2012-12-20 19:22:21 +00004706 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong))
John McCallf85e1932011-06-15 23:02:42 +00004707 Results.AddResult(CodeCompletionResult("strong"));
Bill Wendlingad017fa2012-12-20 19:22:21 +00004708 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy))
John McCall0a2c5e22010-08-25 06:19:51 +00004709 Results.AddResult(CodeCompletionResult("copy"));
Bill Wendlingad017fa2012-12-20 19:22:21 +00004710 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic))
John McCall0a2c5e22010-08-25 06:19:51 +00004711 Results.AddResult(CodeCompletionResult("nonatomic"));
Bill Wendlingad017fa2012-12-20 19:22:21 +00004712 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic))
Fariborz Jahanian27f45232011-06-11 17:14:27 +00004713 Results.AddResult(CodeCompletionResult("atomic"));
Jordan Rosed7403a72012-08-20 20:01:13 +00004714
4715 // Only suggest "weak" if we're compiling for ARC-with-weak-references or GC.
John McCall0a7dd782012-08-21 02:47:43 +00004716 if (getLangOpts().ObjCARCWeak || getLangOpts().getGC() != LangOptions::NonGC)
Bill Wendlingad017fa2012-12-20 19:22:21 +00004717 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak))
Jordan Rosed7403a72012-08-20 20:01:13 +00004718 Results.AddResult(CodeCompletionResult("weak"));
4719
Bill Wendlingad017fa2012-12-20 19:22:21 +00004720 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004721 CodeCompletionBuilder Setter(Results.getAllocator(),
4722 Results.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00004723 Setter.AddTypedTextChunk("setter");
4724 Setter.AddTextChunk(" = ");
4725 Setter.AddPlaceholderChunk("method");
4726 Results.AddResult(CodeCompletionResult(Setter.TakeString()));
Douglas Gregor54f01612009-11-19 00:01:57 +00004727 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00004728 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004729 CodeCompletionBuilder Getter(Results.getAllocator(),
4730 Results.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00004731 Getter.AddTypedTextChunk("getter");
4732 Getter.AddTextChunk(" = ");
4733 Getter.AddPlaceholderChunk("method");
4734 Results.AddResult(CodeCompletionResult(Getter.TakeString()));
Douglas Gregor54f01612009-11-19 00:01:57 +00004735 }
Steve Naroffece8e712009-10-08 21:55:05 +00004736 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004737 HandleCodeCompleteResults(this, CodeCompleter,
4738 CodeCompletionContext::CCC_Other,
4739 Results.data(),Results.size());
Steve Naroffece8e712009-10-08 21:55:05 +00004740}
Steve Naroffc4df6d22009-11-07 02:08:14 +00004741
James Dennettde23c7e2012-06-17 05:33:25 +00004742/// \brief Describes the kind of Objective-C method that we want to find
Douglas Gregor4ad96852009-11-19 07:41:15 +00004743/// via code completion.
4744enum ObjCMethodKind {
Dmitri Gribenko49fdccb2012-06-08 23:13:42 +00004745 MK_Any, ///< Any kind of method, provided it means other specified criteria.
4746 MK_ZeroArgSelector, ///< Zero-argument (unary) selector.
4747 MK_OneArgSelector ///< One-argument selector.
Douglas Gregor4ad96852009-11-19 07:41:15 +00004748};
4749
Douglas Gregor458433d2010-08-26 15:07:07 +00004750static bool isAcceptableObjCSelector(Selector Sel,
4751 ObjCMethodKind WantKind,
4752 IdentifierInfo **SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004753 unsigned NumSelIdents,
4754 bool AllowSameLength = true) {
Douglas Gregor458433d2010-08-26 15:07:07 +00004755 if (NumSelIdents > Sel.getNumArgs())
4756 return false;
4757
4758 switch (WantKind) {
4759 case MK_Any: break;
4760 case MK_ZeroArgSelector: return Sel.isUnarySelector();
4761 case MK_OneArgSelector: return Sel.getNumArgs() == 1;
4762 }
4763
Douglas Gregorcf544262010-11-17 21:36:08 +00004764 if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs())
4765 return false;
4766
Douglas Gregor458433d2010-08-26 15:07:07 +00004767 for (unsigned I = 0; I != NumSelIdents; ++I)
4768 if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
4769 return false;
4770
4771 return true;
4772}
4773
Douglas Gregor4ad96852009-11-19 07:41:15 +00004774static bool isAcceptableObjCMethod(ObjCMethodDecl *Method,
4775 ObjCMethodKind WantKind,
4776 IdentifierInfo **SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004777 unsigned NumSelIdents,
4778 bool AllowSameLength = true) {
Douglas Gregor458433d2010-08-26 15:07:07 +00004779 return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004780 NumSelIdents, AllowSameLength);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004781}
Douglas Gregord36adf52010-09-16 16:06:31 +00004782
4783namespace {
4784 /// \brief A set of selectors, which is used to avoid introducing multiple
4785 /// completions with the same selector into the result set.
4786 typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet;
4787}
4788
Douglas Gregor36ecb042009-11-17 23:22:23 +00004789/// \brief Add all of the Objective-C methods in the given Objective-C
4790/// container to the set of results.
4791///
4792/// The container will be a class, protocol, category, or implementation of
4793/// any of the above. This mether will recurse to include methods from
4794/// the superclasses of classes along with their categories, protocols, and
4795/// implementations.
4796///
4797/// \param Container the container in which we'll look to find methods.
4798///
James Dennetta40f7922012-06-14 03:11:41 +00004799/// \param WantInstanceMethods Whether to add instance methods (only); if
4800/// false, this routine will add factory methods (only).
Douglas Gregor36ecb042009-11-17 23:22:23 +00004801///
4802/// \param CurContext the context in which we're performing the lookup that
4803/// finds methods.
4804///
Douglas Gregorcf544262010-11-17 21:36:08 +00004805/// \param AllowSameLength Whether we allow a method to be added to the list
4806/// when it has the same number of parameters as we have selector identifiers.
4807///
Douglas Gregor36ecb042009-11-17 23:22:23 +00004808/// \param Results the structure into which we'll add results.
4809static void AddObjCMethods(ObjCContainerDecl *Container,
4810 bool WantInstanceMethods,
Douglas Gregor4ad96852009-11-19 07:41:15 +00004811 ObjCMethodKind WantKind,
Douglas Gregord3c68542009-11-19 01:08:35 +00004812 IdentifierInfo **SelIdents,
4813 unsigned NumSelIdents,
Douglas Gregor36ecb042009-11-17 23:22:23 +00004814 DeclContext *CurContext,
Douglas Gregord36adf52010-09-16 16:06:31 +00004815 VisitedSelectorSet &Selectors,
Douglas Gregorcf544262010-11-17 21:36:08 +00004816 bool AllowSameLength,
Douglas Gregor408be5a2010-08-25 01:08:01 +00004817 ResultBuilder &Results,
4818 bool InOriginalClass = true) {
John McCall0a2c5e22010-08-25 06:19:51 +00004819 typedef CodeCompletionResult Result;
Douglas Gregorb92a4082012-06-12 13:44:08 +00004820 Container = getContainerDef(Container);
Douglas Gregor5824b802013-01-30 06:58:39 +00004821 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
4822 bool isRootClass = IFace && !IFace->getSuperClass();
Douglas Gregor36ecb042009-11-17 23:22:23 +00004823 for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
4824 MEnd = Container->meth_end();
4825 M != MEnd; ++M) {
Douglas Gregor5824b802013-01-30 06:58:39 +00004826 // The instance methods on the root class can be messaged via the
4827 // metaclass.
4828 if (M->isInstanceMethod() == WantInstanceMethods ||
4829 (isRootClass && !WantInstanceMethods)) {
Douglas Gregord3c68542009-11-19 01:08:35 +00004830 // Check whether the selector identifiers we've been given are a
4831 // subset of the identifiers for this particular method.
David Blaikie581deb32012-06-06 20:45:41 +00004832 if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004833 AllowSameLength))
Douglas Gregord3c68542009-11-19 01:08:35 +00004834 continue;
Douglas Gregor4ad96852009-11-19 07:41:15 +00004835
David Blaikie262bc182012-04-30 02:36:29 +00004836 if (!Selectors.insert(M->getSelector()))
Douglas Gregord36adf52010-09-16 16:06:31 +00004837 continue;
4838
Douglas Gregord1f09b42013-01-31 04:52:16 +00004839 Result R = Result(*M, Results.getBasePriority(*M), 0);
Douglas Gregord3c68542009-11-19 01:08:35 +00004840 R.StartParameter = NumSelIdents;
Douglas Gregor4ad96852009-11-19 07:41:15 +00004841 R.AllParametersAreInformative = (WantKind != MK_Any);
Douglas Gregor408be5a2010-08-25 01:08:01 +00004842 if (!InOriginalClass)
4843 R.Priority += CCD_InBaseClass;
Douglas Gregord3c68542009-11-19 01:08:35 +00004844 Results.MaybeAddResult(R, CurContext);
4845 }
Douglas Gregor36ecb042009-11-17 23:22:23 +00004846 }
4847
Douglas Gregore396c7b2010-09-16 15:34:59 +00004848 // Visit the protocols of protocols.
4849 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00004850 if (Protocol->hasDefinition()) {
4851 const ObjCList<ObjCProtocolDecl> &Protocols
4852 = Protocol->getReferencedProtocols();
4853 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
4854 E = Protocols.end();
4855 I != E; ++I)
4856 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
4857 NumSelIdents, CurContext, Selectors, AllowSameLength,
4858 Results, false);
4859 }
Douglas Gregore396c7b2010-09-16 15:34:59 +00004860 }
4861
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00004862 if (!IFace || !IFace->hasDefinition())
Douglas Gregor36ecb042009-11-17 23:22:23 +00004863 return;
4864
4865 // Add methods in protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00004866 for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(),
4867 E = IFace->protocol_end();
Douglas Gregor36ecb042009-11-17 23:22:23 +00004868 I != E; ++I)
Douglas Gregor4ad96852009-11-19 07:41:15 +00004869 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004870 CurContext, Selectors, AllowSameLength, Results, false);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004871
4872 // Add methods in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00004873 for (ObjCInterfaceDecl::known_categories_iterator
4874 Cat = IFace->known_categories_begin(),
4875 CatEnd = IFace->known_categories_end();
4876 Cat != CatEnd; ++Cat) {
4877 ObjCCategoryDecl *CatDecl = *Cat;
4878
4879 AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004880 NumSelIdents, CurContext, Selectors, AllowSameLength,
4881 Results, InOriginalClass);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004882
4883 // Add a categories protocol methods.
4884 const ObjCList<ObjCProtocolDecl> &Protocols
4885 = CatDecl->getReferencedProtocols();
4886 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
4887 E = Protocols.end();
4888 I != E; ++I)
Douglas Gregor4ad96852009-11-19 07:41:15 +00004889 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004890 NumSelIdents, CurContext, Selectors, AllowSameLength,
4891 Results, false);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004892
4893 // Add methods in category implementations.
4894 if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
Douglas Gregor4ad96852009-11-19 07:41:15 +00004895 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004896 NumSelIdents, CurContext, Selectors, AllowSameLength,
4897 Results, InOriginalClass);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004898 }
4899
4900 // Add methods in superclass.
4901 if (IFace->getSuperClass())
Douglas Gregor4ad96852009-11-19 07:41:15 +00004902 AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind,
Douglas Gregorcf544262010-11-17 21:36:08 +00004903 SelIdents, NumSelIdents, CurContext, Selectors,
4904 AllowSameLength, Results, false);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004905
4906 // Add methods in our implementation, if any.
4907 if (ObjCImplementationDecl *Impl = IFace->getImplementation())
Douglas Gregor4ad96852009-11-19 07:41:15 +00004908 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004909 NumSelIdents, CurContext, Selectors, AllowSameLength,
4910 Results, InOriginalClass);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004911}
4912
4913
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004914void Sema::CodeCompleteObjCPropertyGetter(Scope *S) {
Douglas Gregor4ad96852009-11-19 07:41:15 +00004915 // Try to find the interface where getters might live.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004916 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004917 if (!Class) {
4918 if (ObjCCategoryDecl *Category
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004919 = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
Douglas Gregor4ad96852009-11-19 07:41:15 +00004920 Class = Category->getClassInterface();
4921
4922 if (!Class)
4923 return;
4924 }
4925
4926 // Find all of the potential getters.
Douglas Gregor218937c2011-02-01 19:23:04 +00004927 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004928 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004929 CodeCompletionContext::CCC_Other);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004930 Results.EnterNewScope();
4931
Douglas Gregord36adf52010-09-16 16:06:31 +00004932 VisitedSelectorSet Selectors;
4933 AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Selectors,
Douglas Gregorcf544262010-11-17 21:36:08 +00004934 /*AllowSameLength=*/true, Results);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004935 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004936 HandleCodeCompleteResults(this, CodeCompleter,
4937 CodeCompletionContext::CCC_Other,
4938 Results.data(),Results.size());
Douglas Gregor4ad96852009-11-19 07:41:15 +00004939}
4940
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004941void Sema::CodeCompleteObjCPropertySetter(Scope *S) {
Douglas Gregor4ad96852009-11-19 07:41:15 +00004942 // Try to find the interface where setters might live.
4943 ObjCInterfaceDecl *Class
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004944 = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004945 if (!Class) {
4946 if (ObjCCategoryDecl *Category
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004947 = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
Douglas Gregor4ad96852009-11-19 07:41:15 +00004948 Class = Category->getClassInterface();
4949
4950 if (!Class)
4951 return;
4952 }
4953
4954 // Find all of the potential getters.
Douglas Gregor218937c2011-02-01 19:23:04 +00004955 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004956 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004957 CodeCompletionContext::CCC_Other);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004958 Results.EnterNewScope();
4959
Douglas Gregord36adf52010-09-16 16:06:31 +00004960 VisitedSelectorSet Selectors;
4961 AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext,
Douglas Gregorcf544262010-11-17 21:36:08 +00004962 Selectors, /*AllowSameLength=*/true, Results);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004963
4964 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004965 HandleCodeCompleteResults(this, CodeCompleter,
4966 CodeCompletionContext::CCC_Other,
4967 Results.data(),Results.size());
Douglas Gregor36ecb042009-11-17 23:22:23 +00004968}
4969
Douglas Gregorafc45782011-02-15 22:19:42 +00004970void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS,
4971 bool IsParameter) {
Douglas Gregor218937c2011-02-01 19:23:04 +00004972 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004973 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004974 CodeCompletionContext::CCC_Type);
Douglas Gregord32b0222010-08-24 01:06:58 +00004975 Results.EnterNewScope();
4976
4977 // Add context-sensitive, Objective-C parameter-passing keywords.
4978 bool AddedInOut = false;
4979 if ((DS.getObjCDeclQualifier() &
4980 (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) {
4981 Results.AddResult("in");
4982 Results.AddResult("inout");
4983 AddedInOut = true;
4984 }
4985 if ((DS.getObjCDeclQualifier() &
4986 (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) {
4987 Results.AddResult("out");
4988 if (!AddedInOut)
4989 Results.AddResult("inout");
4990 }
4991 if ((DS.getObjCDeclQualifier() &
4992 (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref |
4993 ObjCDeclSpec::DQ_Oneway)) == 0) {
4994 Results.AddResult("bycopy");
4995 Results.AddResult("byref");
4996 Results.AddResult("oneway");
4997 }
4998
Douglas Gregorafc45782011-02-15 22:19:42 +00004999 // If we're completing the return type of an Objective-C method and the
5000 // identifier IBAction refers to a macro, provide a completion item for
5001 // an action, e.g.,
5002 // IBAction)<#selector#>:(id)sender
5003 if (DS.getObjCDeclQualifier() == 0 && !IsParameter &&
5004 Context.Idents.get("IBAction").hasMacroDefinition()) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005005 CodeCompletionBuilder Builder(Results.getAllocator(),
5006 Results.getCodeCompletionTUInfo(),
5007 CCP_CodePattern, CXAvailability_Available);
Douglas Gregorafc45782011-02-15 22:19:42 +00005008 Builder.AddTypedTextChunk("IBAction");
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00005009 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorafc45782011-02-15 22:19:42 +00005010 Builder.AddPlaceholderChunk("selector");
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00005011 Builder.AddChunk(CodeCompletionString::CK_Colon);
5012 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregorafc45782011-02-15 22:19:42 +00005013 Builder.AddTextChunk("id");
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00005014 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorafc45782011-02-15 22:19:42 +00005015 Builder.AddTextChunk("sender");
5016 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
5017 }
Douglas Gregor31aa5772013-01-30 07:11:43 +00005018
5019 // If we're completing the return type, provide 'instancetype'.
5020 if (!IsParameter) {
5021 Results.AddResult(CodeCompletionResult("instancetype"));
5022 }
Douglas Gregorafc45782011-02-15 22:19:42 +00005023
Douglas Gregord32b0222010-08-24 01:06:58 +00005024 // Add various builtin type names and specifiers.
5025 AddOrdinaryNameResults(PCC_Type, S, *this, Results);
5026 Results.ExitScope();
5027
5028 // Add the various type names
5029 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
5030 CodeCompletionDeclConsumer Consumer(Results, CurContext);
5031 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5032 CodeCompleter->includeGlobals());
5033
5034 if (CodeCompleter->includeMacros())
Douglas Gregor3644d972012-10-09 16:01:50 +00005035 AddMacroResults(PP, Results, false);
Douglas Gregord32b0222010-08-24 01:06:58 +00005036
5037 HandleCodeCompleteResults(this, CodeCompleter,
5038 CodeCompletionContext::CCC_Type,
5039 Results.data(), Results.size());
5040}
5041
Douglas Gregor22f56992010-04-06 19:22:33 +00005042/// \brief When we have an expression with type "id", we may assume
5043/// that it has some more-specific class type based on knowledge of
5044/// common uses of Objective-C. This routine returns that class type,
5045/// or NULL if no better result could be determined.
5046static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) {
Douglas Gregor78edf512010-09-15 16:23:04 +00005047 ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E);
Douglas Gregor22f56992010-04-06 19:22:33 +00005048 if (!Msg)
5049 return 0;
5050
5051 Selector Sel = Msg->getSelector();
5052 if (Sel.isNull())
5053 return 0;
5054
5055 IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0);
5056 if (!Id)
5057 return 0;
5058
5059 ObjCMethodDecl *Method = Msg->getMethodDecl();
5060 if (!Method)
5061 return 0;
5062
5063 // Determine the class that we're sending the message to.
Douglas Gregor04badcf2010-04-21 00:45:42 +00005064 ObjCInterfaceDecl *IFace = 0;
5065 switch (Msg->getReceiverKind()) {
5066 case ObjCMessageExpr::Class:
John McCallc12c5bb2010-05-15 11:32:37 +00005067 if (const ObjCObjectType *ObjType
5068 = Msg->getClassReceiver()->getAs<ObjCObjectType>())
5069 IFace = ObjType->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00005070 break;
5071
5072 case ObjCMessageExpr::Instance: {
5073 QualType T = Msg->getInstanceReceiver()->getType();
5074 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
5075 IFace = Ptr->getInterfaceDecl();
5076 break;
5077 }
5078
5079 case ObjCMessageExpr::SuperInstance:
5080 case ObjCMessageExpr::SuperClass:
5081 break;
Douglas Gregor22f56992010-04-06 19:22:33 +00005082 }
5083
5084 if (!IFace)
5085 return 0;
5086
5087 ObjCInterfaceDecl *Super = IFace->getSuperClass();
5088 if (Method->isInstanceMethod())
5089 return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5090 .Case("retain", IFace)
John McCallf85e1932011-06-15 23:02:42 +00005091 .Case("strong", IFace)
Douglas Gregor22f56992010-04-06 19:22:33 +00005092 .Case("autorelease", IFace)
5093 .Case("copy", IFace)
5094 .Case("copyWithZone", IFace)
5095 .Case("mutableCopy", IFace)
5096 .Case("mutableCopyWithZone", IFace)
5097 .Case("awakeFromCoder", IFace)
5098 .Case("replacementObjectFromCoder", IFace)
5099 .Case("class", IFace)
5100 .Case("classForCoder", IFace)
5101 .Case("superclass", Super)
5102 .Default(0);
5103
5104 return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5105 .Case("new", IFace)
5106 .Case("alloc", IFace)
5107 .Case("allocWithZone", IFace)
5108 .Case("class", IFace)
5109 .Case("superclass", Super)
5110 .Default(0);
5111}
5112
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005113// Add a special completion for a message send to "super", which fills in the
5114// most likely case of forwarding all of our arguments to the superclass
5115// function.
5116///
5117/// \param S The semantic analysis object.
5118///
Dmitri Gribenko70517ca2012-08-23 17:58:28 +00005119/// \param NeedSuperKeyword Whether we need to prefix this completion with
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005120/// the "super" keyword. Otherwise, we just need to provide the arguments.
5121///
5122/// \param SelIdents The identifiers in the selector that have already been
5123/// provided as arguments for a send to "super".
5124///
5125/// \param NumSelIdents The number of identifiers in \p SelIdents.
5126///
5127/// \param Results The set of results to augment.
5128///
5129/// \returns the Objective-C method declaration that would be invoked by
5130/// this "super" completion. If NULL, no completion was added.
5131static ObjCMethodDecl *AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword,
5132 IdentifierInfo **SelIdents,
5133 unsigned NumSelIdents,
5134 ResultBuilder &Results) {
5135 ObjCMethodDecl *CurMethod = S.getCurMethodDecl();
5136 if (!CurMethod)
5137 return 0;
5138
5139 ObjCInterfaceDecl *Class = CurMethod->getClassInterface();
5140 if (!Class)
5141 return 0;
5142
5143 // Try to find a superclass method with the same selector.
5144 ObjCMethodDecl *SuperMethod = 0;
Douglas Gregor78bcd912011-02-16 00:51:18 +00005145 while ((Class = Class->getSuperClass()) && !SuperMethod) {
5146 // Check in the class
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005147 SuperMethod = Class->getMethod(CurMethod->getSelector(),
5148 CurMethod->isInstanceMethod());
5149
Douglas Gregor78bcd912011-02-16 00:51:18 +00005150 // Check in categories or class extensions.
5151 if (!SuperMethod) {
Douglas Gregord3297242013-01-16 23:00:23 +00005152 for (ObjCInterfaceDecl::known_categories_iterator
5153 Cat = Class->known_categories_begin(),
5154 CatEnd = Class->known_categories_end();
5155 Cat != CatEnd; ++Cat) {
5156 if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(),
Douglas Gregor78bcd912011-02-16 00:51:18 +00005157 CurMethod->isInstanceMethod())))
5158 break;
Douglas Gregord3297242013-01-16 23:00:23 +00005159 }
Douglas Gregor78bcd912011-02-16 00:51:18 +00005160 }
5161 }
5162
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005163 if (!SuperMethod)
5164 return 0;
5165
5166 // Check whether the superclass method has the same signature.
5167 if (CurMethod->param_size() != SuperMethod->param_size() ||
5168 CurMethod->isVariadic() != SuperMethod->isVariadic())
5169 return 0;
5170
5171 for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(),
5172 CurPEnd = CurMethod->param_end(),
5173 SuperP = SuperMethod->param_begin();
5174 CurP != CurPEnd; ++CurP, ++SuperP) {
5175 // Make sure the parameter types are compatible.
5176 if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(),
5177 (*SuperP)->getType()))
5178 return 0;
5179
5180 // Make sure we have a parameter name to forward!
5181 if (!(*CurP)->getIdentifier())
5182 return 0;
5183 }
5184
5185 // We have a superclass method. Now, form the send-to-super completion.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005186 CodeCompletionBuilder Builder(Results.getAllocator(),
5187 Results.getCodeCompletionTUInfo());
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005188
5189 // Give this completion a return type.
Douglas Gregor8987b232011-09-27 23:30:47 +00005190 AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod,
5191 Builder);
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005192
5193 // If we need the "super" keyword, add it (plus some spacing).
5194 if (NeedSuperKeyword) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005195 Builder.AddTypedTextChunk("super");
5196 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005197 }
5198
5199 Selector Sel = CurMethod->getSelector();
5200 if (Sel.isUnarySelector()) {
5201 if (NeedSuperKeyword)
Douglas Gregordae68752011-02-01 22:57:45 +00005202 Builder.AddTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005203 Sel.getNameForSlot(0)));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005204 else
Douglas Gregordae68752011-02-01 22:57:45 +00005205 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005206 Sel.getNameForSlot(0)));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005207 } else {
5208 ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin();
5209 for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) {
5210 if (I > NumSelIdents)
Douglas Gregor218937c2011-02-01 19:23:04 +00005211 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005212
5213 if (I < NumSelIdents)
Douglas Gregor218937c2011-02-01 19:23:04 +00005214 Builder.AddInformativeChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00005215 Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005216 Sel.getNameForSlot(I) + ":"));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005217 else if (NeedSuperKeyword || I > NumSelIdents) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005218 Builder.AddTextChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00005219 Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005220 Sel.getNameForSlot(I) + ":"));
Douglas Gregordae68752011-02-01 22:57:45 +00005221 Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00005222 (*CurP)->getIdentifier()->getName()));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005223 } else {
Douglas Gregor218937c2011-02-01 19:23:04 +00005224 Builder.AddTypedTextChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00005225 Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005226 Sel.getNameForSlot(I) + ":"));
Douglas Gregordae68752011-02-01 22:57:45 +00005227 Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00005228 (*CurP)->getIdentifier()->getName()));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005229 }
5230 }
5231 }
5232
Douglas Gregorba103062012-03-27 23:34:16 +00005233 Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod,
5234 CCP_SuperCompletion));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005235 return SuperMethod;
5236}
5237
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005238void Sema::CodeCompleteObjCMessageReceiver(Scope *S) {
John McCall0a2c5e22010-08-25 06:19:51 +00005239 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00005240 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005241 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005242 CodeCompletionContext::CCC_ObjCMessageReceiver,
Richard Smith80ad52f2013-01-02 11:42:31 +00005243 getLangOpts().CPlusPlus11
Douglas Gregor81f3bff2012-02-15 15:34:24 +00005244 ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture
5245 : &ResultBuilder::IsObjCMessageReceiver);
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005246
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005247 CodeCompletionDeclConsumer Consumer(Results, CurContext);
5248 Results.EnterNewScope();
Douglas Gregor8071e422010-08-15 06:18:01 +00005249 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5250 CodeCompleter->includeGlobals());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005251
5252 // If we are in an Objective-C method inside a class that has a superclass,
5253 // add "super" as an option.
5254 if (ObjCMethodDecl *Method = getCurMethodDecl())
5255 if (ObjCInterfaceDecl *Iface = Method->getClassInterface())
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005256 if (Iface->getSuperClass()) {
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005257 Results.AddResult(Result("super"));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005258
5259 AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, 0, 0, Results);
5260 }
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005261
Richard Smith80ad52f2013-01-02 11:42:31 +00005262 if (getLangOpts().CPlusPlus11)
Douglas Gregor81f3bff2012-02-15 15:34:24 +00005263 addThisCompletion(*this, Results);
5264
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005265 Results.ExitScope();
5266
5267 if (CodeCompleter->includeMacros())
Douglas Gregor3644d972012-10-09 16:01:50 +00005268 AddMacroResults(PP, Results, false);
Douglas Gregorcee9ff12010-09-20 22:39:41 +00005269 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005270 Results.data(), Results.size());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005271
5272}
5273
Douglas Gregor2725ca82010-04-21 19:57:20 +00005274void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc,
5275 IdentifierInfo **SelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005276 unsigned NumSelIdents,
5277 bool AtArgumentExpression) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00005278 ObjCInterfaceDecl *CDecl = 0;
5279 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5280 // Figure out which interface we're in.
5281 CDecl = CurMethod->getClassInterface();
5282 if (!CDecl)
5283 return;
5284
5285 // Find the superclass of this class.
5286 CDecl = CDecl->getSuperClass();
5287 if (!CDecl)
5288 return;
5289
5290 if (CurMethod->isInstanceMethod()) {
5291 // We are inside an instance method, which means that the message
5292 // send [super ...] is actually calling an instance method on the
Douglas Gregor6b0656a2010-10-13 21:24:53 +00005293 // current object.
5294 return CodeCompleteObjCInstanceMessage(S, 0,
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005295 SelIdents, NumSelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005296 AtArgumentExpression,
Douglas Gregor6b0656a2010-10-13 21:24:53 +00005297 CDecl);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005298 }
5299
5300 // Fall through to send to the superclass in CDecl.
5301 } else {
5302 // "super" may be the name of a type or variable. Figure out which
5303 // it is.
5304 IdentifierInfo *Super = &Context.Idents.get("super");
5305 NamedDecl *ND = LookupSingleName(S, Super, SuperLoc,
5306 LookupOrdinaryName);
5307 if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) {
5308 // "super" names an interface. Use it.
5309 } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) {
John McCallc12c5bb2010-05-15 11:32:37 +00005310 if (const ObjCObjectType *Iface
5311 = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>())
5312 CDecl = Iface->getInterface();
Douglas Gregor2725ca82010-04-21 19:57:20 +00005313 } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) {
5314 // "super" names an unresolved type; we can't be more specific.
5315 } else {
5316 // Assume that "super" names some kind of value and parse that way.
5317 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00005318 SourceLocation TemplateKWLoc;
Douglas Gregor2725ca82010-04-21 19:57:20 +00005319 UnqualifiedId id;
5320 id.setIdentifier(Super, SuperLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00005321 ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id,
5322 false, false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005323 return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005324 SelIdents, NumSelIdents,
5325 AtArgumentExpression);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005326 }
5327
5328 // Fall through
5329 }
5330
John McCallb3d87482010-08-24 05:47:05 +00005331 ParsedType Receiver;
Douglas Gregor2725ca82010-04-21 19:57:20 +00005332 if (CDecl)
John McCallb3d87482010-08-24 05:47:05 +00005333 Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl));
Douglas Gregor2725ca82010-04-21 19:57:20 +00005334 return CodeCompleteObjCClassMessage(S, Receiver, SelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005335 NumSelIdents, AtArgumentExpression,
5336 /*IsSuper=*/true);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005337}
5338
Douglas Gregorb9d77572010-09-21 00:03:25 +00005339/// \brief Given a set of code-completion results for the argument of a message
5340/// send, determine the preferred type (if any) for that argument expression.
5341static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results,
5342 unsigned NumSelIdents) {
5343 typedef CodeCompletionResult Result;
5344 ASTContext &Context = Results.getSema().Context;
5345
5346 QualType PreferredType;
5347 unsigned BestPriority = CCP_Unlikely * 2;
5348 Result *ResultsData = Results.data();
5349 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
5350 Result &R = ResultsData[I];
5351 if (R.Kind == Result::RK_Declaration &&
5352 isa<ObjCMethodDecl>(R.Declaration)) {
5353 if (R.Priority <= BestPriority) {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +00005354 const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration);
Douglas Gregorb9d77572010-09-21 00:03:25 +00005355 if (NumSelIdents <= Method->param_size()) {
5356 QualType MyPreferredType = Method->param_begin()[NumSelIdents - 1]
5357 ->getType();
5358 if (R.Priority < BestPriority || PreferredType.isNull()) {
5359 BestPriority = R.Priority;
5360 PreferredType = MyPreferredType;
5361 } else if (!Context.hasSameUnqualifiedType(PreferredType,
5362 MyPreferredType)) {
5363 PreferredType = QualType();
5364 }
5365 }
5366 }
5367 }
5368 }
5369
5370 return PreferredType;
5371}
5372
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005373static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
5374 ParsedType Receiver,
5375 IdentifierInfo **SelIdents,
5376 unsigned NumSelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005377 bool AtArgumentExpression,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005378 bool IsSuper,
5379 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00005380 typedef CodeCompletionResult Result;
Douglas Gregor24a069f2009-11-17 17:59:40 +00005381 ObjCInterfaceDecl *CDecl = 0;
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005382
Douglas Gregor24a069f2009-11-17 17:59:40 +00005383 // If the given name refers to an interface type, retrieve the
5384 // corresponding declaration.
Douglas Gregor2725ca82010-04-21 19:57:20 +00005385 if (Receiver) {
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005386 QualType T = SemaRef.GetTypeFromParser(Receiver, 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005387 if (!T.isNull())
John McCallc12c5bb2010-05-15 11:32:37 +00005388 if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>())
5389 CDecl = Interface->getInterface();
Douglas Gregor24a069f2009-11-17 17:59:40 +00005390 }
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005391
Douglas Gregor36ecb042009-11-17 23:22:23 +00005392 // Add all of the factory methods in this Objective-C class, its protocols,
5393 // superclasses, categories, implementation, etc.
Steve Naroffc4df6d22009-11-07 02:08:14 +00005394 Results.EnterNewScope();
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005395
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005396 // If this is a send-to-super, try to add the special "super" send
5397 // completion.
5398 if (IsSuper) {
5399 if (ObjCMethodDecl *SuperMethod
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005400 = AddSuperSendCompletion(SemaRef, false, SelIdents, NumSelIdents,
5401 Results))
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005402 Results.Ignore(SuperMethod);
5403 }
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005404
Douglas Gregor265f7492010-08-27 15:29:55 +00005405 // If we're inside an Objective-C method definition, prefer its selector to
5406 // others.
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005407 if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl())
Douglas Gregor265f7492010-08-27 15:29:55 +00005408 Results.setPreferredSelector(CurMethod->getSelector());
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005409
Douglas Gregord36adf52010-09-16 16:06:31 +00005410 VisitedSelectorSet Selectors;
Douglas Gregor13438f92010-04-06 16:40:00 +00005411 if (CDecl)
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005412 AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00005413 SemaRef.CurContext, Selectors, AtArgumentExpression,
5414 Results);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005415 else {
Douglas Gregor13438f92010-04-06 16:40:00 +00005416 // We're messaging "id" as a type; provide all class/factory methods.
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005417
Douglas Gregor719770d2010-04-06 17:30:22 +00005418 // If we have an external source, load the entire class method
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005419 // pool from the AST file.
Axel Naumann0ec56b72012-10-18 19:05:02 +00005420 if (SemaRef.getExternalSource()) {
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005421 for (uint32_t I = 0,
Axel Naumann0ec56b72012-10-18 19:05:02 +00005422 N = SemaRef.getExternalSource()->GetNumExternalSelectors();
John McCall76bd1f32010-06-01 09:23:16 +00005423 I != N; ++I) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00005424 Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I);
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005425 if (Sel.isNull() || SemaRef.MethodPool.count(Sel))
Douglas Gregor719770d2010-04-06 17:30:22 +00005426 continue;
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005427
5428 SemaRef.ReadMethodPool(Sel);
Douglas Gregor719770d2010-04-06 17:30:22 +00005429 }
5430 }
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005431
5432 for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(),
5433 MEnd = SemaRef.MethodPool.end();
Sebastian Redldb9d2142010-08-02 23:18:59 +00005434 M != MEnd; ++M) {
5435 for (ObjCMethodList *MethList = &M->second.second;
5436 MethList && MethList->Method;
Douglas Gregor13438f92010-04-06 16:40:00 +00005437 MethList = MethList->Next) {
5438 if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
5439 NumSelIdents))
5440 continue;
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005441
Douglas Gregord1f09b42013-01-31 04:52:16 +00005442 Result R(MethList->Method, Results.getBasePriority(MethList->Method),0);
Douglas Gregor13438f92010-04-06 16:40:00 +00005443 R.StartParameter = NumSelIdents;
5444 R.AllParametersAreInformative = false;
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005445 Results.MaybeAddResult(R, SemaRef.CurContext);
Douglas Gregor13438f92010-04-06 16:40:00 +00005446 }
5447 }
5448 }
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005449
5450 Results.ExitScope();
5451}
Douglas Gregor13438f92010-04-06 16:40:00 +00005452
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005453void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver,
5454 IdentifierInfo **SelIdents,
5455 unsigned NumSelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005456 bool AtArgumentExpression,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005457 bool IsSuper) {
Douglas Gregore081a612011-07-21 01:05:26 +00005458
5459 QualType T = this->GetTypeFromParser(Receiver);
5460
Douglas Gregor218937c2011-02-01 19:23:04 +00005461 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005462 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregore081a612011-07-21 01:05:26 +00005463 CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage,
Douglas Gregor0a47d692011-07-26 15:24:30 +00005464 T, SelIdents, NumSelIdents));
Douglas Gregore081a612011-07-21 01:05:26 +00005465
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005466 AddClassMessageCompletions(*this, S, Receiver, SelIdents, NumSelIdents,
5467 AtArgumentExpression, IsSuper, Results);
Douglas Gregorb9d77572010-09-21 00:03:25 +00005468
5469 // If we're actually at the argument expression (rather than prior to the
5470 // selector), we're actually performing code completion for an expression.
5471 // Determine whether we have a single, best method. If so, we can
5472 // code-complete the expression using the corresponding parameter type as
5473 // our preferred type, improving completion results.
5474 if (AtArgumentExpression) {
5475 QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
Douglas Gregore081a612011-07-21 01:05:26 +00005476 NumSelIdents);
Douglas Gregorb9d77572010-09-21 00:03:25 +00005477 if (PreferredType.isNull())
5478 CodeCompleteOrdinaryName(S, PCC_Expression);
5479 else
5480 CodeCompleteExpression(S, PreferredType);
5481 return;
5482 }
5483
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005484 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregore081a612011-07-21 01:05:26 +00005485 Results.getCompletionContext(),
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005486 Results.data(), Results.size());
Steve Naroffc4df6d22009-11-07 02:08:14 +00005487}
5488
Richard Trieuf81e5a92011-09-09 02:00:50 +00005489void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver,
Douglas Gregord3c68542009-11-19 01:08:35 +00005490 IdentifierInfo **SelIdents,
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005491 unsigned NumSelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005492 bool AtArgumentExpression,
Douglas Gregor6b0656a2010-10-13 21:24:53 +00005493 ObjCInterfaceDecl *Super) {
John McCall0a2c5e22010-08-25 06:19:51 +00005494 typedef CodeCompletionResult Result;
Steve Naroffc4df6d22009-11-07 02:08:14 +00005495
5496 Expr *RecExpr = static_cast<Expr *>(Receiver);
Steve Naroffc4df6d22009-11-07 02:08:14 +00005497
Douglas Gregor36ecb042009-11-17 23:22:23 +00005498 // If necessary, apply function/array conversion to the receiver.
5499 // C99 6.7.5.3p[7,8].
John Wiegley429bb272011-04-08 18:41:53 +00005500 if (RecExpr) {
5501 ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr);
5502 if (Conv.isInvalid()) // conversion failed. bail.
5503 return;
5504 RecExpr = Conv.take();
5505 }
Douglas Gregor6b0656a2010-10-13 21:24:53 +00005506 QualType ReceiverType = RecExpr? RecExpr->getType()
5507 : Super? Context.getObjCObjectPointerType(
5508 Context.getObjCInterfaceType(Super))
5509 : Context.getObjCIdType();
Steve Naroffc4df6d22009-11-07 02:08:14 +00005510
Douglas Gregorda892642010-11-08 21:12:30 +00005511 // If we're messaging an expression with type "id" or "Class", check
5512 // whether we know something special about the receiver that allows
5513 // us to assume a more-specific receiver type.
5514 if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType())
5515 if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) {
5516 if (ReceiverType->isObjCClassType())
5517 return CodeCompleteObjCClassMessage(S,
5518 ParsedType::make(Context.getObjCInterfaceType(IFace)),
5519 SelIdents, NumSelIdents,
5520 AtArgumentExpression, Super);
5521
5522 ReceiverType = Context.getObjCObjectPointerType(
5523 Context.getObjCInterfaceType(IFace));
5524 }
5525
Douglas Gregor36ecb042009-11-17 23:22:23 +00005526 // Build the set of methods we can see.
Douglas Gregor218937c2011-02-01 19:23:04 +00005527 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005528 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregore081a612011-07-21 01:05:26 +00005529 CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage,
Douglas Gregor0a47d692011-07-26 15:24:30 +00005530 ReceiverType, SelIdents, NumSelIdents));
Douglas Gregore081a612011-07-21 01:05:26 +00005531
Douglas Gregor36ecb042009-11-17 23:22:23 +00005532 Results.EnterNewScope();
Douglas Gregor22f56992010-04-06 19:22:33 +00005533
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005534 // If this is a send-to-super, try to add the special "super" send
5535 // completion.
Douglas Gregor6b0656a2010-10-13 21:24:53 +00005536 if (Super) {
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005537 if (ObjCMethodDecl *SuperMethod
5538 = AddSuperSendCompletion(*this, false, SelIdents, NumSelIdents,
5539 Results))
5540 Results.Ignore(SuperMethod);
5541 }
5542
Douglas Gregor265f7492010-08-27 15:29:55 +00005543 // If we're inside an Objective-C method definition, prefer its selector to
5544 // others.
5545 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
5546 Results.setPreferredSelector(CurMethod->getSelector());
Douglas Gregor36ecb042009-11-17 23:22:23 +00005547
Douglas Gregord36adf52010-09-16 16:06:31 +00005548 // Keep track of the selectors we've already added.
5549 VisitedSelectorSet Selectors;
5550
Douglas Gregorf74a4192009-11-18 00:06:18 +00005551 // Handle messages to Class. This really isn't a message to an instance
5552 // method, so we treat it the same way we would treat a message send to a
5553 // class method.
5554 if (ReceiverType->isObjCClassType() ||
5555 ReceiverType->isObjCQualifiedClassType()) {
5556 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5557 if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
Douglas Gregor4ad96852009-11-19 07:41:15 +00005558 AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00005559 CurContext, Selectors, AtArgumentExpression, Results);
Douglas Gregorf74a4192009-11-18 00:06:18 +00005560 }
5561 }
5562 // Handle messages to a qualified ID ("id<foo>").
5563 else if (const ObjCObjectPointerType *QualID
5564 = ReceiverType->getAsObjCQualifiedIdType()) {
5565 // Search protocols for instance methods.
5566 for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(),
5567 E = QualID->qual_end();
5568 I != E; ++I)
Douglas Gregor4ad96852009-11-19 07:41:15 +00005569 AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
Douglas Gregorcf544262010-11-17 21:36:08 +00005570 Selectors, AtArgumentExpression, Results);
Douglas Gregorf74a4192009-11-18 00:06:18 +00005571 }
5572 // Handle messages to a pointer to interface type.
5573 else if (const ObjCObjectPointerType *IFacePtr
5574 = ReceiverType->getAsObjCInterfacePointerType()) {
5575 // Search the class, its superclasses, etc., for instance methods.
Douglas Gregor4ad96852009-11-19 07:41:15 +00005576 AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00005577 NumSelIdents, CurContext, Selectors, AtArgumentExpression,
5578 Results);
Douglas Gregorf74a4192009-11-18 00:06:18 +00005579
5580 // Search protocols for instance methods.
5581 for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(),
5582 E = IFacePtr->qual_end();
5583 I != E; ++I)
Douglas Gregor4ad96852009-11-19 07:41:15 +00005584 AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
Douglas Gregorcf544262010-11-17 21:36:08 +00005585 Selectors, AtArgumentExpression, Results);
Douglas Gregorf74a4192009-11-18 00:06:18 +00005586 }
Douglas Gregor13438f92010-04-06 16:40:00 +00005587 // Handle messages to "id".
5588 else if (ReceiverType->isObjCIdType()) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005589 // We're messaging "id", so provide all instance methods we know
5590 // about as code-completion results.
5591
5592 // If we have an external source, load the entire class method
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005593 // pool from the AST file.
Douglas Gregor719770d2010-04-06 17:30:22 +00005594 if (ExternalSource) {
John McCall76bd1f32010-06-01 09:23:16 +00005595 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5596 I != N; ++I) {
5597 Selector Sel = ExternalSource->GetExternalSelector(I);
Sebastian Redldb9d2142010-08-02 23:18:59 +00005598 if (Sel.isNull() || MethodPool.count(Sel))
Douglas Gregor719770d2010-04-06 17:30:22 +00005599 continue;
5600
Sebastian Redldb9d2142010-08-02 23:18:59 +00005601 ReadMethodPool(Sel);
Douglas Gregor719770d2010-04-06 17:30:22 +00005602 }
5603 }
5604
Sebastian Redldb9d2142010-08-02 23:18:59 +00005605 for (GlobalMethodPool::iterator M = MethodPool.begin(),
5606 MEnd = MethodPool.end();
5607 M != MEnd; ++M) {
5608 for (ObjCMethodList *MethList = &M->second.first;
5609 MethList && MethList->Method;
Douglas Gregor13438f92010-04-06 16:40:00 +00005610 MethList = MethList->Next) {
5611 if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
5612 NumSelIdents))
5613 continue;
Douglas Gregord36adf52010-09-16 16:06:31 +00005614
5615 if (!Selectors.insert(MethList->Method->getSelector()))
5616 continue;
5617
Douglas Gregord1f09b42013-01-31 04:52:16 +00005618 Result R(MethList->Method, Results.getBasePriority(MethList->Method),0);
Douglas Gregor13438f92010-04-06 16:40:00 +00005619 R.StartParameter = NumSelIdents;
5620 R.AllParametersAreInformative = false;
5621 Results.MaybeAddResult(R, CurContext);
5622 }
5623 }
5624 }
Steve Naroffc4df6d22009-11-07 02:08:14 +00005625 Results.ExitScope();
Douglas Gregorb9d77572010-09-21 00:03:25 +00005626
5627
5628 // If we're actually at the argument expression (rather than prior to the
5629 // selector), we're actually performing code completion for an expression.
5630 // Determine whether we have a single, best method. If so, we can
5631 // code-complete the expression using the corresponding parameter type as
5632 // our preferred type, improving completion results.
5633 if (AtArgumentExpression) {
5634 QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
5635 NumSelIdents);
5636 if (PreferredType.isNull())
5637 CodeCompleteOrdinaryName(S, PCC_Expression);
5638 else
5639 CodeCompleteExpression(S, PreferredType);
5640 return;
5641 }
5642
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005643 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregore081a612011-07-21 01:05:26 +00005644 Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005645 Results.data(),Results.size());
Steve Naroffc4df6d22009-11-07 02:08:14 +00005646}
Douglas Gregor55385fe2009-11-18 04:19:12 +00005647
Douglas Gregorfb629412010-08-23 21:17:50 +00005648void Sema::CodeCompleteObjCForCollection(Scope *S,
5649 DeclGroupPtrTy IterationVar) {
5650 CodeCompleteExpressionData Data;
5651 Data.ObjCCollection = true;
5652
5653 if (IterationVar.getAsOpaquePtr()) {
5654 DeclGroupRef DG = IterationVar.getAsVal<DeclGroupRef>();
5655 for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) {
5656 if (*I)
5657 Data.IgnoreDecls.push_back(*I);
5658 }
5659 }
5660
5661 CodeCompleteExpression(S, Data);
5662}
5663
Douglas Gregor458433d2010-08-26 15:07:07 +00005664void Sema::CodeCompleteObjCSelector(Scope *S, IdentifierInfo **SelIdents,
5665 unsigned NumSelIdents) {
5666 // If we have an external source, load the entire class method
5667 // pool from the AST file.
5668 if (ExternalSource) {
5669 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5670 I != N; ++I) {
5671 Selector Sel = ExternalSource->GetExternalSelector(I);
5672 if (Sel.isNull() || MethodPool.count(Sel))
5673 continue;
5674
5675 ReadMethodPool(Sel);
5676 }
5677 }
5678
Douglas Gregor218937c2011-02-01 19:23:04 +00005679 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005680 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005681 CodeCompletionContext::CCC_SelectorName);
Douglas Gregor458433d2010-08-26 15:07:07 +00005682 Results.EnterNewScope();
5683 for (GlobalMethodPool::iterator M = MethodPool.begin(),
5684 MEnd = MethodPool.end();
5685 M != MEnd; ++M) {
5686
5687 Selector Sel = M->first;
5688 if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents, NumSelIdents))
5689 continue;
5690
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005691 CodeCompletionBuilder Builder(Results.getAllocator(),
5692 Results.getCodeCompletionTUInfo());
Douglas Gregor458433d2010-08-26 15:07:07 +00005693 if (Sel.isUnarySelector()) {
Douglas Gregordae68752011-02-01 22:57:45 +00005694 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005695 Sel.getNameForSlot(0)));
Douglas Gregor218937c2011-02-01 19:23:04 +00005696 Results.AddResult(Builder.TakeString());
Douglas Gregor458433d2010-08-26 15:07:07 +00005697 continue;
5698 }
5699
Douglas Gregor2d9e21f2010-08-26 16:46:39 +00005700 std::string Accumulator;
Douglas Gregor458433d2010-08-26 15:07:07 +00005701 for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) {
Douglas Gregor2d9e21f2010-08-26 16:46:39 +00005702 if (I == NumSelIdents) {
5703 if (!Accumulator.empty()) {
Douglas Gregordae68752011-02-01 22:57:45 +00005704 Builder.AddInformativeChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00005705 Accumulator));
Douglas Gregor2d9e21f2010-08-26 16:46:39 +00005706 Accumulator.clear();
5707 }
5708 }
5709
Benjamin Kramera0651c52011-07-26 16:59:25 +00005710 Accumulator += Sel.getNameForSlot(I);
Douglas Gregor2d9e21f2010-08-26 16:46:39 +00005711 Accumulator += ':';
Douglas Gregor458433d2010-08-26 15:07:07 +00005712 }
Douglas Gregordae68752011-02-01 22:57:45 +00005713 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator));
Douglas Gregor218937c2011-02-01 19:23:04 +00005714 Results.AddResult(Builder.TakeString());
Douglas Gregor458433d2010-08-26 15:07:07 +00005715 }
5716 Results.ExitScope();
5717
5718 HandleCodeCompleteResults(this, CodeCompleter,
5719 CodeCompletionContext::CCC_SelectorName,
5720 Results.data(), Results.size());
5721}
5722
Douglas Gregor55385fe2009-11-18 04:19:12 +00005723/// \brief Add all of the protocol declarations that we find in the given
5724/// (translation unit) context.
5725static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext,
Douglas Gregor083128f2009-11-18 04:49:41 +00005726 bool OnlyForwardDeclarations,
Douglas Gregor55385fe2009-11-18 04:19:12 +00005727 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00005728 typedef CodeCompletionResult Result;
Douglas Gregor55385fe2009-11-18 04:19:12 +00005729
5730 for (DeclContext::decl_iterator D = Ctx->decls_begin(),
5731 DEnd = Ctx->decls_end();
5732 D != DEnd; ++D) {
5733 // Record any protocols we find.
5734 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D))
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00005735 if (!OnlyForwardDeclarations || !Proto->hasDefinition())
Douglas Gregord1f09b42013-01-31 04:52:16 +00005736 Results.AddResult(Result(Proto, Results.getBasePriority(Proto), 0),
5737 CurContext, 0, false);
Douglas Gregor55385fe2009-11-18 04:19:12 +00005738 }
5739}
5740
5741void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
5742 unsigned NumProtocols) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005743 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005744 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005745 CodeCompletionContext::CCC_ObjCProtocolName);
Douglas Gregor55385fe2009-11-18 04:19:12 +00005746
Douglas Gregor70c23352010-12-09 21:44:02 +00005747 if (CodeCompleter && CodeCompleter->includeGlobals()) {
5748 Results.EnterNewScope();
5749
5750 // Tell the result set to ignore all of the protocols we have
5751 // already seen.
5752 // FIXME: This doesn't work when caching code-completion results.
5753 for (unsigned I = 0; I != NumProtocols; ++I)
5754 if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first,
5755 Protocols[I].second))
5756 Results.Ignore(Protocol);
Douglas Gregor55385fe2009-11-18 04:19:12 +00005757
Douglas Gregor70c23352010-12-09 21:44:02 +00005758 // Add all protocols.
5759 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false,
5760 Results);
Douglas Gregor083128f2009-11-18 04:49:41 +00005761
Douglas Gregor70c23352010-12-09 21:44:02 +00005762 Results.ExitScope();
5763 }
5764
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005765 HandleCodeCompleteResults(this, CodeCompleter,
5766 CodeCompletionContext::CCC_ObjCProtocolName,
5767 Results.data(),Results.size());
Douglas Gregor083128f2009-11-18 04:49:41 +00005768}
5769
5770void Sema::CodeCompleteObjCProtocolDecl(Scope *) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005771 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005772 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005773 CodeCompletionContext::CCC_ObjCProtocolName);
Douglas Gregor083128f2009-11-18 04:49:41 +00005774
Douglas Gregor70c23352010-12-09 21:44:02 +00005775 if (CodeCompleter && CodeCompleter->includeGlobals()) {
5776 Results.EnterNewScope();
5777
5778 // Add all protocols.
5779 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true,
5780 Results);
Douglas Gregor55385fe2009-11-18 04:19:12 +00005781
Douglas Gregor70c23352010-12-09 21:44:02 +00005782 Results.ExitScope();
5783 }
5784
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005785 HandleCodeCompleteResults(this, CodeCompleter,
5786 CodeCompletionContext::CCC_ObjCProtocolName,
5787 Results.data(),Results.size());
Douglas Gregor55385fe2009-11-18 04:19:12 +00005788}
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005789
5790/// \brief Add all of the Objective-C interface declarations that we find in
5791/// the given (translation unit) context.
5792static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext,
5793 bool OnlyForwardDeclarations,
5794 bool OnlyUnimplemented,
5795 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00005796 typedef CodeCompletionResult Result;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005797
5798 for (DeclContext::decl_iterator D = Ctx->decls_begin(),
5799 DEnd = Ctx->decls_end();
5800 D != DEnd; ++D) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +00005801 // Record any interfaces we find.
5802 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D))
Douglas Gregor7723fec2011-12-15 20:29:51 +00005803 if ((!OnlyForwardDeclarations || !Class->hasDefinition()) &&
Douglas Gregordeacbdc2010-08-11 12:19:30 +00005804 (!OnlyUnimplemented || !Class->getImplementation()))
Douglas Gregord1f09b42013-01-31 04:52:16 +00005805 Results.AddResult(Result(Class, Results.getBasePriority(Class), 0),
5806 CurContext, 0, false);
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005807 }
5808}
5809
5810void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005811 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005812 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005813 CodeCompletionContext::CCC_Other);
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005814 Results.EnterNewScope();
5815
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005816 if (CodeCompleter->includeGlobals()) {
5817 // Add all classes.
5818 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5819 false, Results);
5820 }
5821
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005822 Results.ExitScope();
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005823
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005824 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005825 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005826 Results.data(),Results.size());
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005827}
5828
Douglas Gregorc83c6872010-04-15 22:33:43 +00005829void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName,
5830 SourceLocation ClassNameLoc) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005831 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005832 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005833 CodeCompletionContext::CCC_ObjCInterfaceName);
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005834 Results.EnterNewScope();
5835
5836 // Make sure that we ignore the class we're currently defining.
5837 NamedDecl *CurClass
Douglas Gregorc83c6872010-04-15 22:33:43 +00005838 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005839 if (CurClass && isa<ObjCInterfaceDecl>(CurClass))
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005840 Results.Ignore(CurClass);
5841
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005842 if (CodeCompleter->includeGlobals()) {
5843 // Add all classes.
5844 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5845 false, Results);
5846 }
5847
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005848 Results.ExitScope();
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005849
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005850 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005851 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005852 Results.data(),Results.size());
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005853}
5854
5855void Sema::CodeCompleteObjCImplementationDecl(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005856 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005857 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005858 CodeCompletionContext::CCC_Other);
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005859 Results.EnterNewScope();
5860
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005861 if (CodeCompleter->includeGlobals()) {
5862 // Add all unimplemented classes.
5863 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5864 true, Results);
5865 }
5866
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005867 Results.ExitScope();
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005868
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005869 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005870 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005871 Results.data(),Results.size());
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005872}
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005873
5874void Sema::CodeCompleteObjCInterfaceCategory(Scope *S,
Douglas Gregorc83c6872010-04-15 22:33:43 +00005875 IdentifierInfo *ClassName,
5876 SourceLocation ClassNameLoc) {
John McCall0a2c5e22010-08-25 06:19:51 +00005877 typedef CodeCompletionResult Result;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005878
Douglas Gregor218937c2011-02-01 19:23:04 +00005879 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005880 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor3da626b2011-07-07 16:03:39 +00005881 CodeCompletionContext::CCC_ObjCCategoryName);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005882
5883 // Ignore any categories we find that have already been implemented by this
5884 // interface.
5885 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
5886 NamedDecl *CurClass
Douglas Gregorc83c6872010-04-15 22:33:43 +00005887 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregord3297242013-01-16 23:00:23 +00005888 if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){
5889 for (ObjCInterfaceDecl::visible_categories_iterator
5890 Cat = Class->visible_categories_begin(),
5891 CatEnd = Class->visible_categories_end();
5892 Cat != CatEnd; ++Cat) {
5893 CategoryNames.insert(Cat->getIdentifier());
5894 }
5895 }
5896
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005897 // Add all of the categories we know about.
5898 Results.EnterNewScope();
5899 TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5900 for (DeclContext::decl_iterator D = TU->decls_begin(),
5901 DEnd = TU->decls_end();
5902 D != DEnd; ++D)
5903 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D))
5904 if (CategoryNames.insert(Category->getIdentifier()))
Douglas Gregord1f09b42013-01-31 04:52:16 +00005905 Results.AddResult(Result(Category, Results.getBasePriority(Category),0),
5906 CurContext, 0, false);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005907 Results.ExitScope();
5908
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005909 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor3da626b2011-07-07 16:03:39 +00005910 CodeCompletionContext::CCC_ObjCCategoryName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005911 Results.data(),Results.size());
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005912}
5913
5914void Sema::CodeCompleteObjCImplementationCategory(Scope *S,
Douglas Gregorc83c6872010-04-15 22:33:43 +00005915 IdentifierInfo *ClassName,
5916 SourceLocation ClassNameLoc) {
John McCall0a2c5e22010-08-25 06:19:51 +00005917 typedef CodeCompletionResult Result;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005918
5919 // Find the corresponding interface. If we couldn't find the interface, the
5920 // program itself is ill-formed. However, we'll try to be helpful still by
5921 // providing the list of all of the categories we know about.
5922 NamedDecl *CurClass
Douglas Gregorc83c6872010-04-15 22:33:43 +00005923 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005924 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass);
5925 if (!Class)
Douglas Gregorc83c6872010-04-15 22:33:43 +00005926 return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005927
Douglas Gregor218937c2011-02-01 19:23:04 +00005928 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005929 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor3da626b2011-07-07 16:03:39 +00005930 CodeCompletionContext::CCC_ObjCCategoryName);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005931
5932 // Add all of the categories that have have corresponding interface
5933 // declarations in this class and any of its superclasses, except for
5934 // already-implemented categories in the class itself.
5935 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
5936 Results.EnterNewScope();
5937 bool IgnoreImplemented = true;
5938 while (Class) {
Douglas Gregord3297242013-01-16 23:00:23 +00005939 for (ObjCInterfaceDecl::visible_categories_iterator
5940 Cat = Class->visible_categories_begin(),
5941 CatEnd = Class->visible_categories_end();
5942 Cat != CatEnd; ++Cat) {
5943 if ((!IgnoreImplemented || !Cat->getImplementation()) &&
5944 CategoryNames.insert(Cat->getIdentifier()))
Douglas Gregord1f09b42013-01-31 04:52:16 +00005945 Results.AddResult(Result(*Cat, Results.getBasePriority(*Cat), 0),
5946 CurContext, 0, false);
Douglas Gregord3297242013-01-16 23:00:23 +00005947 }
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005948
5949 Class = Class->getSuperClass();
5950 IgnoreImplemented = false;
5951 }
5952 Results.ExitScope();
5953
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005954 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor3da626b2011-07-07 16:03:39 +00005955 CodeCompletionContext::CCC_ObjCCategoryName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005956 Results.data(),Results.size());
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005957}
Douglas Gregor322328b2009-11-18 22:32:06 +00005958
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00005959void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005960 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005961 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005962 CodeCompletionContext::CCC_Other);
Douglas Gregor322328b2009-11-18 22:32:06 +00005963
5964 // Figure out where this @synthesize lives.
5965 ObjCContainerDecl *Container
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00005966 = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
Douglas Gregor322328b2009-11-18 22:32:06 +00005967 if (!Container ||
5968 (!isa<ObjCImplementationDecl>(Container) &&
5969 !isa<ObjCCategoryImplDecl>(Container)))
5970 return;
5971
5972 // Ignore any properties that have already been implemented.
Douglas Gregorb92a4082012-06-12 13:44:08 +00005973 Container = getContainerDef(Container);
5974 for (DeclContext::decl_iterator D = Container->decls_begin(),
Douglas Gregor322328b2009-11-18 22:32:06 +00005975 DEnd = Container->decls_end();
5976 D != DEnd; ++D)
5977 if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D))
5978 Results.Ignore(PropertyImpl->getPropertyDecl());
5979
5980 // Add any properties that we find.
Douglas Gregor73449212010-12-09 23:01:55 +00005981 AddedPropertiesSet AddedProperties;
Douglas Gregor322328b2009-11-18 22:32:06 +00005982 Results.EnterNewScope();
5983 if (ObjCImplementationDecl *ClassImpl
5984 = dyn_cast<ObjCImplementationDecl>(Container))
Douglas Gregor4b81cde2011-05-05 15:50:42 +00005985 AddObjCProperties(ClassImpl->getClassInterface(), false,
5986 /*AllowNullaryMethods=*/false, CurContext,
Douglas Gregor73449212010-12-09 23:01:55 +00005987 AddedProperties, Results);
Douglas Gregor322328b2009-11-18 22:32:06 +00005988 else
5989 AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(),
Douglas Gregor4b81cde2011-05-05 15:50:42 +00005990 false, /*AllowNullaryMethods=*/false, CurContext,
5991 AddedProperties, Results);
Douglas Gregor322328b2009-11-18 22:32:06 +00005992 Results.ExitScope();
5993
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005994 HandleCodeCompleteResults(this, CodeCompleter,
5995 CodeCompletionContext::CCC_Other,
5996 Results.data(),Results.size());
Douglas Gregor322328b2009-11-18 22:32:06 +00005997}
5998
5999void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00006000 IdentifierInfo *PropertyName) {
John McCall0a2c5e22010-08-25 06:19:51 +00006001 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00006002 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006003 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00006004 CodeCompletionContext::CCC_Other);
Douglas Gregor322328b2009-11-18 22:32:06 +00006005
6006 // Figure out where this @synthesize lives.
6007 ObjCContainerDecl *Container
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00006008 = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
Douglas Gregor322328b2009-11-18 22:32:06 +00006009 if (!Container ||
6010 (!isa<ObjCImplementationDecl>(Container) &&
6011 !isa<ObjCCategoryImplDecl>(Container)))
6012 return;
6013
6014 // Figure out which interface we're looking into.
6015 ObjCInterfaceDecl *Class = 0;
6016 if (ObjCImplementationDecl *ClassImpl
6017 = dyn_cast<ObjCImplementationDecl>(Container))
6018 Class = ClassImpl->getClassInterface();
6019 else
6020 Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
6021 ->getClassInterface();
6022
Douglas Gregore8426052011-04-18 14:40:46 +00006023 // Determine the type of the property we're synthesizing.
6024 QualType PropertyType = Context.getObjCIdType();
6025 if (Class) {
6026 if (ObjCPropertyDecl *Property
6027 = Class->FindPropertyDeclaration(PropertyName)) {
6028 PropertyType
6029 = Property->getType().getNonReferenceType().getUnqualifiedType();
6030
6031 // Give preference to ivars
6032 Results.setPreferredType(PropertyType);
6033 }
6034 }
6035
Douglas Gregor322328b2009-11-18 22:32:06 +00006036 // Add all of the instance variables in this class and its superclasses.
6037 Results.EnterNewScope();
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006038 bool SawSimilarlyNamedIvar = false;
6039 std::string NameWithPrefix;
6040 NameWithPrefix += '_';
Benjamin Kramera0651c52011-07-26 16:59:25 +00006041 NameWithPrefix += PropertyName->getName();
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006042 std::string NameWithSuffix = PropertyName->getName().str();
6043 NameWithSuffix += '_';
Douglas Gregor322328b2009-11-18 22:32:06 +00006044 for(; Class; Class = Class->getSuperClass()) {
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006045 for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar;
6046 Ivar = Ivar->getNextIvar()) {
Douglas Gregord1f09b42013-01-31 04:52:16 +00006047 Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), 0),
6048 CurContext, 0, false);
Douglas Gregore8426052011-04-18 14:40:46 +00006049
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006050 // Determine whether we've seen an ivar with a name similar to the
6051 // property.
Douglas Gregore8426052011-04-18 14:40:46 +00006052 if ((PropertyName == Ivar->getIdentifier() ||
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006053 NameWithPrefix == Ivar->getName() ||
Douglas Gregore8426052011-04-18 14:40:46 +00006054 NameWithSuffix == Ivar->getName())) {
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006055 SawSimilarlyNamedIvar = true;
Douglas Gregore8426052011-04-18 14:40:46 +00006056
6057 // Reduce the priority of this result by one, to give it a slight
6058 // advantage over other results whose names don't match so closely.
6059 if (Results.size() &&
6060 Results.data()[Results.size() - 1].Kind
6061 == CodeCompletionResult::RK_Declaration &&
6062 Results.data()[Results.size() - 1].Declaration == Ivar)
6063 Results.data()[Results.size() - 1].Priority--;
6064 }
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006065 }
Douglas Gregor322328b2009-11-18 22:32:06 +00006066 }
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006067
6068 if (!SawSimilarlyNamedIvar) {
6069 // Create ivar result _propName, that the user can use to synthesize
Douglas Gregore8426052011-04-18 14:40:46 +00006070 // an ivar of the appropriate type.
6071 unsigned Priority = CCP_MemberDeclaration + 1;
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006072 typedef CodeCompletionResult Result;
6073 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006074 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(),
6075 Priority,CXAvailability_Available);
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006076
Douglas Gregor8987b232011-09-27 23:30:47 +00006077 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregore8426052011-04-18 14:40:46 +00006078 Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00006079 Policy, Allocator));
Douglas Gregoraa490cb2011-04-18 14:13:53 +00006080 Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix));
6081 Results.AddResult(Result(Builder.TakeString(), Priority,
6082 CXCursor_ObjCIvarDecl));
6083 }
6084
Douglas Gregor322328b2009-11-18 22:32:06 +00006085 Results.ExitScope();
6086
Douglas Gregore6b1bb62010-08-11 21:23:17 +00006087 HandleCodeCompleteResults(this, CodeCompleter,
6088 CodeCompletionContext::CCC_Other,
6089 Results.data(),Results.size());
Douglas Gregor322328b2009-11-18 22:32:06 +00006090}
Douglas Gregore8f5a172010-04-07 00:21:17 +00006091
Douglas Gregor408be5a2010-08-25 01:08:01 +00006092// Mapping from selectors to the methods that implement that selector, along
6093// with the "in original class" flag.
6094typedef llvm::DenseMap<Selector, std::pair<ObjCMethodDecl *, bool> >
6095 KnownMethodsMap;
Douglas Gregore8f5a172010-04-07 00:21:17 +00006096
6097/// \brief Find all of the methods that reside in the given container
6098/// (and its superclasses, protocols, etc.) that meet the given
6099/// criteria. Insert those methods into the map of known methods,
6100/// indexed by selector so they can be easily found.
6101static void FindImplementableMethods(ASTContext &Context,
6102 ObjCContainerDecl *Container,
6103 bool WantInstanceMethods,
6104 QualType ReturnType,
Douglas Gregor408be5a2010-08-25 01:08:01 +00006105 KnownMethodsMap &KnownMethods,
6106 bool InOriginalClass = true) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006107 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) {
Douglas Gregorb92a4082012-06-12 13:44:08 +00006108 // Make sure we have a definition; that's what we'll walk.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00006109 if (!IFace->hasDefinition())
6110 return;
Douglas Gregorb92a4082012-06-12 13:44:08 +00006111
6112 IFace = IFace->getDefinition();
6113 Container = IFace;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00006114
Douglas Gregore8f5a172010-04-07 00:21:17 +00006115 const ObjCList<ObjCProtocolDecl> &Protocols
6116 = IFace->getReferencedProtocols();
6117 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
Douglas Gregorea766182010-10-18 18:21:28 +00006118 E = Protocols.end();
Douglas Gregore8f5a172010-04-07 00:21:17 +00006119 I != E; ++I)
6120 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
Douglas Gregorea766182010-10-18 18:21:28 +00006121 KnownMethods, InOriginalClass);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006122
Douglas Gregorea766182010-10-18 18:21:28 +00006123 // Add methods from any class extensions and categories.
Douglas Gregord3297242013-01-16 23:00:23 +00006124 for (ObjCInterfaceDecl::visible_categories_iterator
6125 Cat = IFace->visible_categories_begin(),
6126 CatEnd = IFace->visible_categories_end();
6127 Cat != CatEnd; ++Cat) {
6128 FindImplementableMethods(Context, *Cat, WantInstanceMethods, ReturnType,
Douglas Gregorea766182010-10-18 18:21:28 +00006129 KnownMethods, false);
Douglas Gregord3297242013-01-16 23:00:23 +00006130 }
6131
Douglas Gregorea766182010-10-18 18:21:28 +00006132 // Visit the superclass.
6133 if (IFace->getSuperClass())
6134 FindImplementableMethods(Context, IFace->getSuperClass(),
6135 WantInstanceMethods, ReturnType,
6136 KnownMethods, false);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006137 }
6138
6139 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
6140 // Recurse into protocols.
6141 const ObjCList<ObjCProtocolDecl> &Protocols
6142 = Category->getReferencedProtocols();
6143 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
Douglas Gregorea766182010-10-18 18:21:28 +00006144 E = Protocols.end();
Douglas Gregore8f5a172010-04-07 00:21:17 +00006145 I != E; ++I)
6146 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
Douglas Gregorea766182010-10-18 18:21:28 +00006147 KnownMethods, InOriginalClass);
6148
6149 // If this category is the original class, jump to the interface.
6150 if (InOriginalClass && Category->getClassInterface())
6151 FindImplementableMethods(Context, Category->getClassInterface(),
6152 WantInstanceMethods, ReturnType, KnownMethods,
6153 false);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006154 }
6155
6156 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
Douglas Gregorb92a4082012-06-12 13:44:08 +00006157 // Make sure we have a definition; that's what we'll walk.
6158 if (!Protocol->hasDefinition())
6159 return;
6160 Protocol = Protocol->getDefinition();
6161 Container = Protocol;
6162
6163 // Recurse into protocols.
6164 const ObjCList<ObjCProtocolDecl> &Protocols
6165 = Protocol->getReferencedProtocols();
6166 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6167 E = Protocols.end();
6168 I != E; ++I)
6169 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6170 KnownMethods, false);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006171 }
6172
6173 // Add methods in this container. This operation occurs last because
6174 // we want the methods from this container to override any methods
6175 // we've previously seen with the same selector.
6176 for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
6177 MEnd = Container->meth_end();
6178 M != MEnd; ++M) {
David Blaikie262bc182012-04-30 02:36:29 +00006179 if (M->isInstanceMethod() == WantInstanceMethods) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006180 if (!ReturnType.isNull() &&
David Blaikie262bc182012-04-30 02:36:29 +00006181 !Context.hasSameUnqualifiedType(ReturnType, M->getResultType()))
Douglas Gregore8f5a172010-04-07 00:21:17 +00006182 continue;
6183
David Blaikie581deb32012-06-06 20:45:41 +00006184 KnownMethods[M->getSelector()] = std::make_pair(*M, InOriginalClass);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006185 }
6186 }
6187}
6188
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006189/// \brief Add the parenthesized return or parameter type chunk to a code
6190/// completion string.
6191static void AddObjCPassingTypeChunk(QualType Type,
Douglas Gregor90f5f472012-04-10 18:35:07 +00006192 unsigned ObjCDeclQuals,
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006193 ASTContext &Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00006194 const PrintingPolicy &Policy,
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006195 CodeCompletionBuilder &Builder) {
6196 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregor90f5f472012-04-10 18:35:07 +00006197 std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals);
6198 if (!Quals.empty())
6199 Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals));
Douglas Gregor8987b232011-09-27 23:30:47 +00006200 Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy,
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006201 Builder.getAllocator()));
6202 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6203}
6204
6205/// \brief Determine whether the given class is or inherits from a class by
6206/// the given name.
6207static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class,
Chris Lattner5f9e2722011-07-23 10:55:15 +00006208 StringRef Name) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006209 if (!Class)
6210 return false;
6211
6212 if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name)
6213 return true;
6214
6215 return InheritsFromClassNamed(Class->getSuperClass(), Name);
6216}
6217
6218/// \brief Add code completions for Objective-C Key-Value Coding (KVC) and
6219/// Key-Value Observing (KVO).
6220static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property,
6221 bool IsInstanceMethod,
6222 QualType ReturnType,
6223 ASTContext &Context,
Douglas Gregore74c25c2011-05-04 23:50:46 +00006224 VisitedSelectorSet &KnownSelectors,
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006225 ResultBuilder &Results) {
6226 IdentifierInfo *PropName = Property->getIdentifier();
6227 if (!PropName || PropName->getLength() == 0)
6228 return;
6229
Douglas Gregor8987b232011-09-27 23:30:47 +00006230 PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
6231
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006232 // Builder that will create each code completion.
6233 typedef CodeCompletionResult Result;
6234 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006235 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006236
6237 // The selector table.
6238 SelectorTable &Selectors = Context.Selectors;
6239
6240 // The property name, copied into the code completion allocation region
6241 // on demand.
6242 struct KeyHolder {
6243 CodeCompletionAllocator &Allocator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006244 StringRef Key;
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006245 const char *CopiedKey;
6246
Chris Lattner5f9e2722011-07-23 10:55:15 +00006247 KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key)
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006248 : Allocator(Allocator), Key(Key), CopiedKey(0) { }
6249
6250 operator const char *() {
6251 if (CopiedKey)
6252 return CopiedKey;
6253
6254 return CopiedKey = Allocator.CopyString(Key);
6255 }
6256 } Key(Allocator, PropName->getName());
6257
6258 // The uppercased name of the property name.
6259 std::string UpperKey = PropName->getName();
6260 if (!UpperKey.empty())
6261 UpperKey[0] = toupper(UpperKey[0]);
6262
6263 bool ReturnTypeMatchesProperty = ReturnType.isNull() ||
6264 Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(),
6265 Property->getType());
6266 bool ReturnTypeMatchesVoid
6267 = ReturnType.isNull() || ReturnType->isVoidType();
6268
6269 // Add the normal accessor -(type)key.
6270 if (IsInstanceMethod &&
Douglas Gregore74c25c2011-05-04 23:50:46 +00006271 KnownSelectors.insert(Selectors.getNullarySelector(PropName)) &&
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006272 ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) {
6273 if (ReturnType.isNull())
Douglas Gregor90f5f472012-04-10 18:35:07 +00006274 AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6275 Context, Policy, Builder);
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006276
6277 Builder.AddTypedTextChunk(Key);
6278 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6279 CXCursor_ObjCInstanceMethodDecl));
6280 }
6281
6282 // If we have an integral or boolean property (or the user has provided
6283 // an integral or boolean return type), add the accessor -(type)isKey.
6284 if (IsInstanceMethod &&
6285 ((!ReturnType.isNull() &&
6286 (ReturnType->isIntegerType() || ReturnType->isBooleanType())) ||
6287 (ReturnType.isNull() &&
6288 (Property->getType()->isIntegerType() ||
6289 Property->getType()->isBooleanType())))) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006290 std::string SelectorName = (Twine("is") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006291 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006292 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006293 if (ReturnType.isNull()) {
6294 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6295 Builder.AddTextChunk("BOOL");
6296 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6297 }
6298
6299 Builder.AddTypedTextChunk(
6300 Allocator.CopyString(SelectorId->getName()));
6301 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6302 CXCursor_ObjCInstanceMethodDecl));
6303 }
6304 }
6305
6306 // Add the normal mutator.
6307 if (IsInstanceMethod && ReturnTypeMatchesVoid &&
6308 !Property->getSetterMethodDecl()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006309 std::string SelectorName = (Twine("set") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006310 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006311 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006312 if (ReturnType.isNull()) {
6313 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6314 Builder.AddTextChunk("void");
6315 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6316 }
6317
6318 Builder.AddTypedTextChunk(
6319 Allocator.CopyString(SelectorId->getName()));
6320 Builder.AddTypedTextChunk(":");
Douglas Gregor90f5f472012-04-10 18:35:07 +00006321 AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6322 Context, Policy, Builder);
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006323 Builder.AddTextChunk(Key);
6324 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6325 CXCursor_ObjCInstanceMethodDecl));
6326 }
6327 }
6328
6329 // Indexed and unordered accessors
6330 unsigned IndexedGetterPriority = CCP_CodePattern;
6331 unsigned IndexedSetterPriority = CCP_CodePattern;
6332 unsigned UnorderedGetterPriority = CCP_CodePattern;
6333 unsigned UnorderedSetterPriority = CCP_CodePattern;
6334 if (const ObjCObjectPointerType *ObjCPointer
6335 = Property->getType()->getAs<ObjCObjectPointerType>()) {
6336 if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) {
6337 // If this interface type is not provably derived from a known
6338 // collection, penalize the corresponding completions.
6339 if (!InheritsFromClassNamed(IFace, "NSMutableArray")) {
6340 IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6341 if (!InheritsFromClassNamed(IFace, "NSArray"))
6342 IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6343 }
6344
6345 if (!InheritsFromClassNamed(IFace, "NSMutableSet")) {
6346 UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6347 if (!InheritsFromClassNamed(IFace, "NSSet"))
6348 UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6349 }
6350 }
6351 } else {
6352 IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6353 IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6354 UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6355 UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6356 }
6357
6358 // Add -(NSUInteger)countOf<key>
6359 if (IsInstanceMethod &&
6360 (ReturnType.isNull() || ReturnType->isIntegerType())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006361 std::string SelectorName = (Twine("countOf") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006362 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006363 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006364 if (ReturnType.isNull()) {
6365 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6366 Builder.AddTextChunk("NSUInteger");
6367 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6368 }
6369
6370 Builder.AddTypedTextChunk(
6371 Allocator.CopyString(SelectorId->getName()));
6372 Results.AddResult(Result(Builder.TakeString(),
6373 std::min(IndexedGetterPriority,
6374 UnorderedGetterPriority),
6375 CXCursor_ObjCInstanceMethodDecl));
6376 }
6377 }
6378
6379 // Indexed getters
6380 // Add -(id)objectInKeyAtIndex:(NSUInteger)index
6381 if (IsInstanceMethod &&
6382 (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
Douglas Gregor62041592011-02-17 03:19:26 +00006383 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006384 = (Twine("objectIn") + UpperKey + "AtIndex").str();
Douglas Gregor62041592011-02-17 03:19:26 +00006385 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006386 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006387 if (ReturnType.isNull()) {
6388 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6389 Builder.AddTextChunk("id");
6390 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6391 }
6392
6393 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6394 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6395 Builder.AddTextChunk("NSUInteger");
6396 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6397 Builder.AddTextChunk("index");
6398 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6399 CXCursor_ObjCInstanceMethodDecl));
6400 }
6401 }
6402
6403 // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes
6404 if (IsInstanceMethod &&
6405 (ReturnType.isNull() ||
6406 (ReturnType->isObjCObjectPointerType() &&
6407 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6408 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6409 ->getName() == "NSArray"))) {
Douglas Gregor62041592011-02-17 03:19:26 +00006410 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006411 = (Twine(Property->getName()) + "AtIndexes").str();
Douglas Gregor62041592011-02-17 03:19:26 +00006412 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006413 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006414 if (ReturnType.isNull()) {
6415 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6416 Builder.AddTextChunk("NSArray *");
6417 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6418 }
6419
6420 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6421 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6422 Builder.AddTextChunk("NSIndexSet *");
6423 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6424 Builder.AddTextChunk("indexes");
6425 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6426 CXCursor_ObjCInstanceMethodDecl));
6427 }
6428 }
6429
6430 // Add -(void)getKey:(type **)buffer range:(NSRange)inRange
6431 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006432 std::string SelectorName = (Twine("get") + UpperKey).str();
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006433 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor62041592011-02-17 03:19:26 +00006434 &Context.Idents.get(SelectorName),
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006435 &Context.Idents.get("range")
6436 };
6437
Douglas Gregore74c25c2011-05-04 23:50:46 +00006438 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006439 if (ReturnType.isNull()) {
6440 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6441 Builder.AddTextChunk("void");
6442 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6443 }
6444
6445 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6446 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6447 Builder.AddPlaceholderChunk("object-type");
6448 Builder.AddTextChunk(" **");
6449 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6450 Builder.AddTextChunk("buffer");
6451 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6452 Builder.AddTypedTextChunk("range:");
6453 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6454 Builder.AddTextChunk("NSRange");
6455 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6456 Builder.AddTextChunk("inRange");
6457 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6458 CXCursor_ObjCInstanceMethodDecl));
6459 }
6460 }
6461
6462 // Mutable indexed accessors
6463
6464 // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index
6465 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006466 std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str();
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006467 IdentifierInfo *SelectorIds[2] = {
6468 &Context.Idents.get("insertObject"),
Douglas Gregor62041592011-02-17 03:19:26 +00006469 &Context.Idents.get(SelectorName)
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006470 };
6471
Douglas Gregore74c25c2011-05-04 23:50:46 +00006472 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006473 if (ReturnType.isNull()) {
6474 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6475 Builder.AddTextChunk("void");
6476 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6477 }
6478
6479 Builder.AddTypedTextChunk("insertObject:");
6480 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6481 Builder.AddPlaceholderChunk("object-type");
6482 Builder.AddTextChunk(" *");
6483 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6484 Builder.AddTextChunk("object");
6485 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6486 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6487 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6488 Builder.AddPlaceholderChunk("NSUInteger");
6489 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6490 Builder.AddTextChunk("index");
6491 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6492 CXCursor_ObjCInstanceMethodDecl));
6493 }
6494 }
6495
6496 // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes
6497 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006498 std::string SelectorName = (Twine("insert") + UpperKey).str();
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006499 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor62041592011-02-17 03:19:26 +00006500 &Context.Idents.get(SelectorName),
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006501 &Context.Idents.get("atIndexes")
6502 };
6503
Douglas Gregore74c25c2011-05-04 23:50:46 +00006504 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006505 if (ReturnType.isNull()) {
6506 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6507 Builder.AddTextChunk("void");
6508 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6509 }
6510
6511 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6512 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6513 Builder.AddTextChunk("NSArray *");
6514 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6515 Builder.AddTextChunk("array");
6516 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6517 Builder.AddTypedTextChunk("atIndexes:");
6518 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6519 Builder.AddPlaceholderChunk("NSIndexSet *");
6520 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6521 Builder.AddTextChunk("indexes");
6522 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6523 CXCursor_ObjCInstanceMethodDecl));
6524 }
6525 }
6526
6527 // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index
6528 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006529 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006530 = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str();
Douglas Gregor62041592011-02-17 03:19:26 +00006531 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006532 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006533 if (ReturnType.isNull()) {
6534 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6535 Builder.AddTextChunk("void");
6536 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6537 }
6538
6539 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6540 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6541 Builder.AddTextChunk("NSUInteger");
6542 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6543 Builder.AddTextChunk("index");
6544 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6545 CXCursor_ObjCInstanceMethodDecl));
6546 }
6547 }
6548
6549 // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes
6550 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006551 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006552 = (Twine("remove") + UpperKey + "AtIndexes").str();
Douglas Gregor62041592011-02-17 03:19:26 +00006553 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006554 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006555 if (ReturnType.isNull()) {
6556 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6557 Builder.AddTextChunk("void");
6558 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6559 }
6560
6561 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6562 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6563 Builder.AddTextChunk("NSIndexSet *");
6564 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6565 Builder.AddTextChunk("indexes");
6566 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6567 CXCursor_ObjCInstanceMethodDecl));
6568 }
6569 }
6570
6571 // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object
6572 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006573 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006574 = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str();
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006575 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor62041592011-02-17 03:19:26 +00006576 &Context.Idents.get(SelectorName),
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006577 &Context.Idents.get("withObject")
6578 };
6579
Douglas Gregore74c25c2011-05-04 23:50:46 +00006580 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006581 if (ReturnType.isNull()) {
6582 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6583 Builder.AddTextChunk("void");
6584 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6585 }
6586
6587 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6588 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6589 Builder.AddPlaceholderChunk("NSUInteger");
6590 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6591 Builder.AddTextChunk("index");
6592 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6593 Builder.AddTypedTextChunk("withObject:");
6594 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6595 Builder.AddTextChunk("id");
6596 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6597 Builder.AddTextChunk("object");
6598 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6599 CXCursor_ObjCInstanceMethodDecl));
6600 }
6601 }
6602
6603 // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array
6604 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006605 std::string SelectorName1
Chris Lattner5f9e2722011-07-23 10:55:15 +00006606 = (Twine("replace") + UpperKey + "AtIndexes").str();
6607 std::string SelectorName2 = (Twine("with") + UpperKey).str();
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006608 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor62041592011-02-17 03:19:26 +00006609 &Context.Idents.get(SelectorName1),
6610 &Context.Idents.get(SelectorName2)
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006611 };
6612
Douglas Gregore74c25c2011-05-04 23:50:46 +00006613 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006614 if (ReturnType.isNull()) {
6615 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6616 Builder.AddTextChunk("void");
6617 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6618 }
6619
6620 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":"));
6621 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6622 Builder.AddPlaceholderChunk("NSIndexSet *");
6623 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6624 Builder.AddTextChunk("indexes");
6625 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6626 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":"));
6627 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6628 Builder.AddTextChunk("NSArray *");
6629 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6630 Builder.AddTextChunk("array");
6631 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6632 CXCursor_ObjCInstanceMethodDecl));
6633 }
6634 }
6635
6636 // Unordered getters
6637 // - (NSEnumerator *)enumeratorOfKey
6638 if (IsInstanceMethod &&
6639 (ReturnType.isNull() ||
6640 (ReturnType->isObjCObjectPointerType() &&
6641 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6642 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6643 ->getName() == "NSEnumerator"))) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006644 std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006645 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006646 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006647 if (ReturnType.isNull()) {
6648 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6649 Builder.AddTextChunk("NSEnumerator *");
6650 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6651 }
6652
6653 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6654 Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6655 CXCursor_ObjCInstanceMethodDecl));
6656 }
6657 }
6658
6659 // - (type *)memberOfKey:(type *)object
6660 if (IsInstanceMethod &&
6661 (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006662 std::string SelectorName = (Twine("memberOf") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006663 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006664 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006665 if (ReturnType.isNull()) {
6666 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6667 Builder.AddPlaceholderChunk("object-type");
6668 Builder.AddTextChunk(" *");
6669 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6670 }
6671
6672 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6673 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6674 if (ReturnType.isNull()) {
6675 Builder.AddPlaceholderChunk("object-type");
6676 Builder.AddTextChunk(" *");
6677 } else {
6678 Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00006679 Policy,
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006680 Builder.getAllocator()));
6681 }
6682 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6683 Builder.AddTextChunk("object");
6684 Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6685 CXCursor_ObjCInstanceMethodDecl));
6686 }
6687 }
6688
6689 // Mutable unordered accessors
6690 // - (void)addKeyObject:(type *)object
6691 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006692 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006693 = (Twine("add") + UpperKey + Twine("Object")).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006694 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006695 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006696 if (ReturnType.isNull()) {
6697 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6698 Builder.AddTextChunk("void");
6699 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6700 }
6701
6702 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6703 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6704 Builder.AddPlaceholderChunk("object-type");
6705 Builder.AddTextChunk(" *");
6706 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6707 Builder.AddTextChunk("object");
6708 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6709 CXCursor_ObjCInstanceMethodDecl));
6710 }
6711 }
6712
6713 // - (void)addKey:(NSSet *)objects
6714 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006715 std::string SelectorName = (Twine("add") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006716 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006717 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006718 if (ReturnType.isNull()) {
6719 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6720 Builder.AddTextChunk("void");
6721 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6722 }
6723
6724 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6725 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6726 Builder.AddTextChunk("NSSet *");
6727 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6728 Builder.AddTextChunk("objects");
6729 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6730 CXCursor_ObjCInstanceMethodDecl));
6731 }
6732 }
6733
6734 // - (void)removeKeyObject:(type *)object
6735 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006736 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006737 = (Twine("remove") + UpperKey + Twine("Object")).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006738 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006739 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006740 if (ReturnType.isNull()) {
6741 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6742 Builder.AddTextChunk("void");
6743 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6744 }
6745
6746 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6747 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6748 Builder.AddPlaceholderChunk("object-type");
6749 Builder.AddTextChunk(" *");
6750 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6751 Builder.AddTextChunk("object");
6752 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6753 CXCursor_ObjCInstanceMethodDecl));
6754 }
6755 }
6756
6757 // - (void)removeKey:(NSSet *)objects
6758 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006759 std::string SelectorName = (Twine("remove") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006760 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006761 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006762 if (ReturnType.isNull()) {
6763 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6764 Builder.AddTextChunk("void");
6765 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6766 }
6767
6768 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6769 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6770 Builder.AddTextChunk("NSSet *");
6771 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6772 Builder.AddTextChunk("objects");
6773 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6774 CXCursor_ObjCInstanceMethodDecl));
6775 }
6776 }
6777
6778 // - (void)intersectKey:(NSSet *)objects
6779 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006780 std::string SelectorName = (Twine("intersect") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006781 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006782 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006783 if (ReturnType.isNull()) {
6784 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6785 Builder.AddTextChunk("void");
6786 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6787 }
6788
6789 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6790 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6791 Builder.AddTextChunk("NSSet *");
6792 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6793 Builder.AddTextChunk("objects");
6794 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6795 CXCursor_ObjCInstanceMethodDecl));
6796 }
6797 }
6798
6799 // Key-Value Observing
6800 // + (NSSet *)keyPathsForValuesAffectingKey
6801 if (!IsInstanceMethod &&
6802 (ReturnType.isNull() ||
6803 (ReturnType->isObjCObjectPointerType() &&
6804 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6805 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6806 ->getName() == "NSSet"))) {
Douglas Gregor62041592011-02-17 03:19:26 +00006807 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006808 = (Twine("keyPathsForValuesAffecting") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006809 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006810 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006811 if (ReturnType.isNull()) {
6812 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6813 Builder.AddTextChunk("NSSet *");
6814 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6815 }
6816
6817 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6818 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
Douglas Gregor3f828d12011-06-02 04:02:27 +00006819 CXCursor_ObjCClassMethodDecl));
6820 }
6821 }
6822
6823 // + (BOOL)automaticallyNotifiesObserversForKey
6824 if (!IsInstanceMethod &&
6825 (ReturnType.isNull() ||
6826 ReturnType->isIntegerType() ||
6827 ReturnType->isBooleanType())) {
6828 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006829 = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str();
Douglas Gregor3f828d12011-06-02 04:02:27 +00006830 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6831 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
6832 if (ReturnType.isNull()) {
6833 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6834 Builder.AddTextChunk("BOOL");
6835 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6836 }
6837
6838 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6839 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6840 CXCursor_ObjCClassMethodDecl));
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006841 }
6842 }
6843}
6844
Douglas Gregore8f5a172010-04-07 00:21:17 +00006845void Sema::CodeCompleteObjCMethodDecl(Scope *S,
6846 bool IsInstanceMethod,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00006847 ParsedType ReturnTy) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006848 // Determine the return type of the method we're declaring, if
6849 // provided.
6850 QualType ReturnType = GetTypeFromParser(ReturnTy);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00006851 Decl *IDecl = 0;
6852 if (CurContext->isObjCContainer()) {
6853 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
6854 IDecl = cast<Decl>(OCD);
6855 }
Douglas Gregorea766182010-10-18 18:21:28 +00006856 // Determine where we should start searching for methods.
6857 ObjCContainerDecl *SearchDecl = 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +00006858 bool IsInImplementation = false;
John McCalld226f652010-08-21 09:40:31 +00006859 if (Decl *D = IDecl) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006860 if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) {
6861 SearchDecl = Impl->getClassInterface();
Douglas Gregore8f5a172010-04-07 00:21:17 +00006862 IsInImplementation = true;
6863 } else if (ObjCCategoryImplDecl *CatImpl
Douglas Gregorea766182010-10-18 18:21:28 +00006864 = dyn_cast<ObjCCategoryImplDecl>(D)) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006865 SearchDecl = CatImpl->getCategoryDecl();
Douglas Gregore8f5a172010-04-07 00:21:17 +00006866 IsInImplementation = true;
Douglas Gregorea766182010-10-18 18:21:28 +00006867 } else
Douglas Gregore8f5a172010-04-07 00:21:17 +00006868 SearchDecl = dyn_cast<ObjCContainerDecl>(D);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006869 }
6870
6871 if (!SearchDecl && S) {
Douglas Gregorea766182010-10-18 18:21:28 +00006872 if (DeclContext *DC = static_cast<DeclContext *>(S->getEntity()))
Douglas Gregore8f5a172010-04-07 00:21:17 +00006873 SearchDecl = dyn_cast<ObjCContainerDecl>(DC);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006874 }
6875
Douglas Gregorea766182010-10-18 18:21:28 +00006876 if (!SearchDecl) {
Douglas Gregore6b1bb62010-08-11 21:23:17 +00006877 HandleCodeCompleteResults(this, CodeCompleter,
6878 CodeCompletionContext::CCC_Other,
6879 0, 0);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006880 return;
6881 }
6882
6883 // Find all of the methods that we could declare/implement here.
6884 KnownMethodsMap KnownMethods;
6885 FindImplementableMethods(Context, SearchDecl, IsInstanceMethod,
Douglas Gregorea766182010-10-18 18:21:28 +00006886 ReturnType, KnownMethods);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006887
Douglas Gregore8f5a172010-04-07 00:21:17 +00006888 // Add declarations or definitions for each of the known methods.
John McCall0a2c5e22010-08-25 06:19:51 +00006889 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00006890 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006891 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00006892 CodeCompletionContext::CCC_Other);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006893 Results.EnterNewScope();
Douglas Gregor8987b232011-09-27 23:30:47 +00006894 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006895 for (KnownMethodsMap::iterator M = KnownMethods.begin(),
6896 MEnd = KnownMethods.end();
6897 M != MEnd; ++M) {
Douglas Gregor408be5a2010-08-25 01:08:01 +00006898 ObjCMethodDecl *Method = M->second.first;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006899 CodeCompletionBuilder Builder(Results.getAllocator(),
6900 Results.getCodeCompletionTUInfo());
Douglas Gregore8f5a172010-04-07 00:21:17 +00006901
6902 // If the result type was not already provided, add it to the
6903 // pattern as (type).
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006904 if (ReturnType.isNull())
Douglas Gregor90f5f472012-04-10 18:35:07 +00006905 AddObjCPassingTypeChunk(Method->getResultType(),
6906 Method->getObjCDeclQualifier(),
6907 Context, Policy,
6908 Builder);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006909
6910 Selector Sel = Method->getSelector();
6911
6912 // Add the first part of the selector to the pattern.
Douglas Gregordae68752011-02-01 22:57:45 +00006913 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00006914 Sel.getNameForSlot(0)));
Douglas Gregore8f5a172010-04-07 00:21:17 +00006915
6916 // Add parameters to the pattern.
6917 unsigned I = 0;
6918 for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
6919 PEnd = Method->param_end();
6920 P != PEnd; (void)++P, ++I) {
6921 // Add the part of the selector name.
6922 if (I == 0)
Douglas Gregor218937c2011-02-01 19:23:04 +00006923 Builder.AddTypedTextChunk(":");
Douglas Gregore8f5a172010-04-07 00:21:17 +00006924 else if (I < Sel.getNumArgs()) {
Douglas Gregor218937c2011-02-01 19:23:04 +00006925 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6926 Builder.AddTypedTextChunk(
Douglas Gregor813d8342011-02-18 22:29:55 +00006927 Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":"));
Douglas Gregore8f5a172010-04-07 00:21:17 +00006928 } else
6929 break;
6930
6931 // Add the parameter type.
Douglas Gregor90f5f472012-04-10 18:35:07 +00006932 AddObjCPassingTypeChunk((*P)->getOriginalType(),
6933 (*P)->getObjCDeclQualifier(),
6934 Context, Policy,
Douglas Gregor8987b232011-09-27 23:30:47 +00006935 Builder);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006936
6937 if (IdentifierInfo *Id = (*P)->getIdentifier())
Douglas Gregordae68752011-02-01 22:57:45 +00006938 Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName()));
Douglas Gregore8f5a172010-04-07 00:21:17 +00006939 }
6940
6941 if (Method->isVariadic()) {
6942 if (Method->param_size() > 0)
Douglas Gregor218937c2011-02-01 19:23:04 +00006943 Builder.AddChunk(CodeCompletionString::CK_Comma);
6944 Builder.AddTextChunk("...");
Douglas Gregore17794f2010-08-31 05:13:43 +00006945 }
Douglas Gregore8f5a172010-04-07 00:21:17 +00006946
Douglas Gregor447107d2010-05-28 00:57:46 +00006947 if (IsInImplementation && Results.includeCodePatterns()) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006948 // We will be defining the method here, so add a compound statement.
Douglas Gregor218937c2011-02-01 19:23:04 +00006949 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6950 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
6951 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006952 if (!Method->getResultType()->isVoidType()) {
6953 // If the result type is not void, add a return clause.
Douglas Gregor218937c2011-02-01 19:23:04 +00006954 Builder.AddTextChunk("return");
6955 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6956 Builder.AddPlaceholderChunk("expression");
6957 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006958 } else
Douglas Gregor218937c2011-02-01 19:23:04 +00006959 Builder.AddPlaceholderChunk("statements");
Douglas Gregore8f5a172010-04-07 00:21:17 +00006960
Douglas Gregor218937c2011-02-01 19:23:04 +00006961 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
6962 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006963 }
6964
Douglas Gregor408be5a2010-08-25 01:08:01 +00006965 unsigned Priority = CCP_CodePattern;
6966 if (!M->second.second)
6967 Priority += CCD_InBaseClass;
6968
Douglas Gregorba103062012-03-27 23:34:16 +00006969 Results.AddResult(Result(Builder.TakeString(), Method, Priority));
Douglas Gregore8f5a172010-04-07 00:21:17 +00006970 }
6971
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006972 // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of
6973 // the properties in this class and its categories.
David Blaikie4e4d0842012-03-11 07:00:24 +00006974 if (Context.getLangOpts().ObjC2) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006975 SmallVector<ObjCContainerDecl *, 4> Containers;
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006976 Containers.push_back(SearchDecl);
6977
Douglas Gregore74c25c2011-05-04 23:50:46 +00006978 VisitedSelectorSet KnownSelectors;
6979 for (KnownMethodsMap::iterator M = KnownMethods.begin(),
6980 MEnd = KnownMethods.end();
6981 M != MEnd; ++M)
6982 KnownSelectors.insert(M->first);
6983
6984
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006985 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl);
6986 if (!IFace)
6987 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl))
6988 IFace = Category->getClassInterface();
6989
6990 if (IFace) {
Douglas Gregord3297242013-01-16 23:00:23 +00006991 for (ObjCInterfaceDecl::visible_categories_iterator
6992 Cat = IFace->visible_categories_begin(),
6993 CatEnd = IFace->visible_categories_end();
6994 Cat != CatEnd; ++Cat) {
6995 Containers.push_back(*Cat);
6996 }
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006997 }
6998
6999 for (unsigned I = 0, N = Containers.size(); I != N; ++I) {
7000 for (ObjCContainerDecl::prop_iterator P = Containers[I]->prop_begin(),
7001 PEnd = Containers[I]->prop_end();
7002 P != PEnd; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00007003 AddObjCKeyValueCompletions(*P, IsInstanceMethod, ReturnType, Context,
Douglas Gregore74c25c2011-05-04 23:50:46 +00007004 KnownSelectors, Results);
Douglas Gregor577cdfd2011-02-17 00:22:45 +00007005 }
7006 }
7007 }
7008
Douglas Gregore8f5a172010-04-07 00:21:17 +00007009 Results.ExitScope();
7010
Douglas Gregore6b1bb62010-08-11 21:23:17 +00007011 HandleCodeCompleteResults(this, CodeCompleter,
7012 CodeCompletionContext::CCC_Other,
7013 Results.data(),Results.size());
Douglas Gregore8f5a172010-04-07 00:21:17 +00007014}
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007015
7016void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S,
7017 bool IsInstanceMethod,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00007018 bool AtParameterName,
John McCallb3d87482010-08-24 05:47:05 +00007019 ParsedType ReturnTy,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007020 IdentifierInfo **SelIdents,
7021 unsigned NumSelIdents) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007022 // If we have an external source, load the entire class method
Sebastian Redl3c7f4132010-08-18 23:57:06 +00007023 // pool from the AST file.
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007024 if (ExternalSource) {
7025 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
7026 I != N; ++I) {
7027 Selector Sel = ExternalSource->GetExternalSelector(I);
Sebastian Redldb9d2142010-08-02 23:18:59 +00007028 if (Sel.isNull() || MethodPool.count(Sel))
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007029 continue;
Sebastian Redldb9d2142010-08-02 23:18:59 +00007030
7031 ReadMethodPool(Sel);
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007032 }
7033 }
7034
7035 // Build the set of methods we can see.
John McCall0a2c5e22010-08-25 06:19:51 +00007036 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00007037 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007038 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00007039 CodeCompletionContext::CCC_Other);
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007040
7041 if (ReturnTy)
7042 Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType());
Sebastian Redldb9d2142010-08-02 23:18:59 +00007043
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007044 Results.EnterNewScope();
Sebastian Redldb9d2142010-08-02 23:18:59 +00007045 for (GlobalMethodPool::iterator M = MethodPool.begin(),
7046 MEnd = MethodPool.end();
7047 M != MEnd; ++M) {
7048 for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first :
7049 &M->second.second;
7050 MethList && MethList->Method;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007051 MethList = MethList->Next) {
7052 if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
7053 NumSelIdents))
7054 continue;
7055
Douglas Gregor40ed9a12010-07-08 23:37:41 +00007056 if (AtParameterName) {
7057 // Suggest parameter names we've seen before.
7058 if (NumSelIdents && NumSelIdents <= MethList->Method->param_size()) {
7059 ParmVarDecl *Param = MethList->Method->param_begin()[NumSelIdents-1];
7060 if (Param->getIdentifier()) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007061 CodeCompletionBuilder Builder(Results.getAllocator(),
7062 Results.getCodeCompletionTUInfo());
Douglas Gregordae68752011-02-01 22:57:45 +00007063 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00007064 Param->getIdentifier()->getName()));
7065 Results.AddResult(Builder.TakeString());
Douglas Gregor40ed9a12010-07-08 23:37:41 +00007066 }
7067 }
7068
7069 continue;
7070 }
7071
Douglas Gregord1f09b42013-01-31 04:52:16 +00007072 Result R(MethList->Method, Results.getBasePriority(MethList->Method), 0);
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007073 R.StartParameter = NumSelIdents;
7074 R.AllParametersAreInformative = false;
7075 R.DeclaringEntity = true;
7076 Results.MaybeAddResult(R, CurContext);
7077 }
7078 }
7079
7080 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00007081 HandleCodeCompleteResults(this, CodeCompleter,
7082 CodeCompletionContext::CCC_Other,
7083 Results.data(),Results.size());
Douglas Gregor1f5537a2010-07-08 23:20:03 +00007084}
Douglas Gregor87c08a52010-08-13 22:48:40 +00007085
Douglas Gregorf29c5232010-08-24 22:20:20 +00007086void Sema::CodeCompletePreprocessorDirective(bool InConditional) {
Douglas Gregor218937c2011-02-01 19:23:04 +00007087 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007088 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00007089 CodeCompletionContext::CCC_PreprocessorDirective);
Douglas Gregorf44e8542010-08-24 19:08:16 +00007090 Results.EnterNewScope();
7091
7092 // #if <condition>
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007093 CodeCompletionBuilder Builder(Results.getAllocator(),
7094 Results.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00007095 Builder.AddTypedTextChunk("if");
7096 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7097 Builder.AddPlaceholderChunk("condition");
7098 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007099
7100 // #ifdef <macro>
Douglas Gregor218937c2011-02-01 19:23:04 +00007101 Builder.AddTypedTextChunk("ifdef");
7102 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7103 Builder.AddPlaceholderChunk("macro");
7104 Results.AddResult(Builder.TakeString());
7105
Douglas Gregorf44e8542010-08-24 19:08:16 +00007106 // #ifndef <macro>
Douglas Gregor218937c2011-02-01 19:23:04 +00007107 Builder.AddTypedTextChunk("ifndef");
7108 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7109 Builder.AddPlaceholderChunk("macro");
7110 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007111
7112 if (InConditional) {
7113 // #elif <condition>
Douglas Gregor218937c2011-02-01 19:23:04 +00007114 Builder.AddTypedTextChunk("elif");
7115 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7116 Builder.AddPlaceholderChunk("condition");
7117 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007118
7119 // #else
Douglas Gregor218937c2011-02-01 19:23:04 +00007120 Builder.AddTypedTextChunk("else");
7121 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007122
7123 // #endif
Douglas Gregor218937c2011-02-01 19:23:04 +00007124 Builder.AddTypedTextChunk("endif");
7125 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007126 }
7127
7128 // #include "header"
Douglas Gregor218937c2011-02-01 19:23:04 +00007129 Builder.AddTypedTextChunk("include");
7130 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7131 Builder.AddTextChunk("\"");
7132 Builder.AddPlaceholderChunk("header");
7133 Builder.AddTextChunk("\"");
7134 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007135
7136 // #include <header>
Douglas Gregor218937c2011-02-01 19:23:04 +00007137 Builder.AddTypedTextChunk("include");
7138 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7139 Builder.AddTextChunk("<");
7140 Builder.AddPlaceholderChunk("header");
7141 Builder.AddTextChunk(">");
7142 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007143
7144 // #define <macro>
Douglas Gregor218937c2011-02-01 19:23:04 +00007145 Builder.AddTypedTextChunk("define");
7146 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7147 Builder.AddPlaceholderChunk("macro");
7148 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007149
7150 // #define <macro>(<args>)
Douglas Gregor218937c2011-02-01 19:23:04 +00007151 Builder.AddTypedTextChunk("define");
7152 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7153 Builder.AddPlaceholderChunk("macro");
7154 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7155 Builder.AddPlaceholderChunk("args");
7156 Builder.AddChunk(CodeCompletionString::CK_RightParen);
7157 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007158
7159 // #undef <macro>
Douglas Gregor218937c2011-02-01 19:23:04 +00007160 Builder.AddTypedTextChunk("undef");
7161 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7162 Builder.AddPlaceholderChunk("macro");
7163 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007164
7165 // #line <number>
Douglas Gregor218937c2011-02-01 19:23:04 +00007166 Builder.AddTypedTextChunk("line");
7167 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7168 Builder.AddPlaceholderChunk("number");
7169 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007170
7171 // #line <number> "filename"
Douglas Gregor218937c2011-02-01 19:23:04 +00007172 Builder.AddTypedTextChunk("line");
7173 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7174 Builder.AddPlaceholderChunk("number");
7175 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7176 Builder.AddTextChunk("\"");
7177 Builder.AddPlaceholderChunk("filename");
7178 Builder.AddTextChunk("\"");
7179 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007180
7181 // #error <message>
Douglas Gregor218937c2011-02-01 19:23:04 +00007182 Builder.AddTypedTextChunk("error");
7183 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7184 Builder.AddPlaceholderChunk("message");
7185 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007186
7187 // #pragma <arguments>
Douglas Gregor218937c2011-02-01 19:23:04 +00007188 Builder.AddTypedTextChunk("pragma");
7189 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7190 Builder.AddPlaceholderChunk("arguments");
7191 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007192
David Blaikie4e4d0842012-03-11 07:00:24 +00007193 if (getLangOpts().ObjC1) {
Douglas Gregorf44e8542010-08-24 19:08:16 +00007194 // #import "header"
Douglas Gregor218937c2011-02-01 19:23:04 +00007195 Builder.AddTypedTextChunk("import");
7196 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7197 Builder.AddTextChunk("\"");
7198 Builder.AddPlaceholderChunk("header");
7199 Builder.AddTextChunk("\"");
7200 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007201
7202 // #import <header>
Douglas Gregor218937c2011-02-01 19:23:04 +00007203 Builder.AddTypedTextChunk("import");
7204 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7205 Builder.AddTextChunk("<");
7206 Builder.AddPlaceholderChunk("header");
7207 Builder.AddTextChunk(">");
7208 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007209 }
7210
7211 // #include_next "header"
Douglas Gregor218937c2011-02-01 19:23:04 +00007212 Builder.AddTypedTextChunk("include_next");
7213 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7214 Builder.AddTextChunk("\"");
7215 Builder.AddPlaceholderChunk("header");
7216 Builder.AddTextChunk("\"");
7217 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007218
7219 // #include_next <header>
Douglas Gregor218937c2011-02-01 19:23:04 +00007220 Builder.AddTypedTextChunk("include_next");
7221 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7222 Builder.AddTextChunk("<");
7223 Builder.AddPlaceholderChunk("header");
7224 Builder.AddTextChunk(">");
7225 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007226
7227 // #warning <message>
Douglas Gregor218937c2011-02-01 19:23:04 +00007228 Builder.AddTypedTextChunk("warning");
7229 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7230 Builder.AddPlaceholderChunk("message");
7231 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007232
7233 // Note: #ident and #sccs are such crazy anachronisms that we don't provide
7234 // completions for them. And __include_macros is a Clang-internal extension
7235 // that we don't want to encourage anyone to use.
7236
7237 // FIXME: we don't support #assert or #unassert, so don't suggest them.
7238 Results.ExitScope();
7239
Douglas Gregorf44e8542010-08-24 19:08:16 +00007240 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor721f3592010-08-25 18:41:16 +00007241 CodeCompletionContext::CCC_PreprocessorDirective,
Douglas Gregorf44e8542010-08-24 19:08:16 +00007242 Results.data(), Results.size());
7243}
7244
7245void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) {
Douglas Gregorf29c5232010-08-24 22:20:20 +00007246 CodeCompleteOrdinaryName(S,
John McCallf312b1e2010-08-26 23:41:50 +00007247 S->getFnParent()? Sema::PCC_RecoveryInFunction
7248 : Sema::PCC_Namespace);
Douglas Gregorf44e8542010-08-24 19:08:16 +00007249}
7250
Douglas Gregorf29c5232010-08-24 22:20:20 +00007251void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) {
Douglas Gregor218937c2011-02-01 19:23:04 +00007252 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007253 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00007254 IsDefinition? CodeCompletionContext::CCC_MacroName
7255 : CodeCompletionContext::CCC_MacroNameUse);
Douglas Gregor1fbb4472010-08-24 20:21:13 +00007256 if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) {
7257 // Add just the names of macros, not their arguments.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007258 CodeCompletionBuilder Builder(Results.getAllocator(),
7259 Results.getCodeCompletionTUInfo());
Douglas Gregor1fbb4472010-08-24 20:21:13 +00007260 Results.EnterNewScope();
7261 for (Preprocessor::macro_iterator M = PP.macro_begin(),
7262 MEnd = PP.macro_end();
7263 M != MEnd; ++M) {
Douglas Gregordae68752011-02-01 22:57:45 +00007264 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00007265 M->first->getName()));
Argyrios Kyrtzidisc04bb922012-09-27 00:24:09 +00007266 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
7267 CCP_CodePattern,
7268 CXCursor_MacroDefinition));
Douglas Gregor1fbb4472010-08-24 20:21:13 +00007269 }
7270 Results.ExitScope();
7271 } else if (IsDefinition) {
7272 // FIXME: Can we detect when the user just wrote an include guard above?
7273 }
7274
Douglas Gregor52779fb2010-09-23 23:01:17 +00007275 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor1fbb4472010-08-24 20:21:13 +00007276 Results.data(), Results.size());
7277}
7278
Douglas Gregorf29c5232010-08-24 22:20:20 +00007279void Sema::CodeCompletePreprocessorExpression() {
Douglas Gregor218937c2011-02-01 19:23:04 +00007280 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007281 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00007282 CodeCompletionContext::CCC_PreprocessorExpression);
Douglas Gregorf29c5232010-08-24 22:20:20 +00007283
7284 if (!CodeCompleter || CodeCompleter->includeMacros())
Douglas Gregor3644d972012-10-09 16:01:50 +00007285 AddMacroResults(PP, Results, true);
Douglas Gregorf29c5232010-08-24 22:20:20 +00007286
7287 // defined (<macro>)
7288 Results.EnterNewScope();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007289 CodeCompletionBuilder Builder(Results.getAllocator(),
7290 Results.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00007291 Builder.AddTypedTextChunk("defined");
7292 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7293 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7294 Builder.AddPlaceholderChunk("macro");
7295 Builder.AddChunk(CodeCompletionString::CK_RightParen);
7296 Results.AddResult(Builder.TakeString());
Douglas Gregorf29c5232010-08-24 22:20:20 +00007297 Results.ExitScope();
7298
7299 HandleCodeCompleteResults(this, CodeCompleter,
7300 CodeCompletionContext::CCC_PreprocessorExpression,
7301 Results.data(), Results.size());
7302}
7303
7304void Sema::CodeCompletePreprocessorMacroArgument(Scope *S,
7305 IdentifierInfo *Macro,
7306 MacroInfo *MacroInfo,
7307 unsigned Argument) {
7308 // FIXME: In the future, we could provide "overload" results, much like we
7309 // do for function calls.
7310
Argyrios Kyrtzidis5c5f03e2011-08-18 19:41:28 +00007311 // Now just ignore this. There will be another code-completion callback
7312 // for the expanded tokens.
Douglas Gregorf29c5232010-08-24 22:20:20 +00007313}
7314
Douglas Gregor55817af2010-08-25 17:04:25 +00007315void Sema::CodeCompleteNaturalLanguage() {
Douglas Gregor55817af2010-08-25 17:04:25 +00007316 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregoraf1c6b52010-08-25 17:10:00 +00007317 CodeCompletionContext::CCC_NaturalLanguage,
Douglas Gregor55817af2010-08-25 17:04:25 +00007318 0, 0);
7319}
7320
Douglas Gregordae68752011-02-01 22:57:45 +00007321void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007322 CodeCompletionTUInfo &CCTUInfo,
Chris Lattner5f9e2722011-07-23 10:55:15 +00007323 SmallVectorImpl<CodeCompletionResult> &Results) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007324 ResultBuilder Builder(*this, Allocator, CCTUInfo,
7325 CodeCompletionContext::CCC_Recovery);
Douglas Gregor8071e422010-08-15 06:18:01 +00007326 if (!CodeCompleter || CodeCompleter->includeGlobals()) {
7327 CodeCompletionDeclConsumer Consumer(Builder,
7328 Context.getTranslationUnitDecl());
7329 LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName,
7330 Consumer);
7331 }
Douglas Gregor87c08a52010-08-13 22:48:40 +00007332
7333 if (!CodeCompleter || CodeCompleter->includeMacros())
Douglas Gregor3644d972012-10-09 16:01:50 +00007334 AddMacroResults(PP, Builder, true);
Douglas Gregor87c08a52010-08-13 22:48:40 +00007335
7336 Results.clear();
7337 Results.insert(Results.end(),
7338 Builder.data(), Builder.data() + Builder.size());
7339}