blob: ca5ee3b226c6a6625f37d7c68cc66e96fb2000a9 [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"
Douglas Gregore737f502010-08-12 20:07:10 +000014#include "clang/Sema/Lookup.h"
John McCall120d63c2010-08-24 20:38:10 +000015#include "clang/Sema/Overload.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000016#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregor719770d2010-04-06 17:30:22 +000017#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000018#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000019#include "clang/Sema/ScopeInfo.h"
John McCall7cd088e2010-08-24 07:21:54 +000020#include "clang/AST/DeclObjC.h"
Douglas Gregorb9d0ef72009-09-21 19:57:38 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregor24a069f2009-11-17 17:59:40 +000022#include "clang/AST/ExprObjC.h"
Douglas Gregorc5b2e582012-01-29 18:15:03 +000023#include "clang/Lex/HeaderSearch.h"
Douglas Gregor3f7c7f42009-10-30 16:50:04 +000024#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.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).
48 typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const;
49
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.
59 llvm::SmallPtrSet<Decl*, 16> AllDeclsFound;
60
Douglas Gregorfbcb5d62009-12-06 20:23:50 +000061 typedef std::pair<NamedDecl *, unsigned> DeclIndexPair;
62
63 /// \brief An entry in the shadow map, which is optimized to store
64 /// a single (declaration, index) mapping (the common case) but
65 /// can also store a list of (declaration, index) mappings.
66 class ShadowMapEntry {
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.
71 llvm::PointerUnion<NamedDecl *, DeclIndexPairVector*> DeclOrVector;
72
73 /// \brief When the entry contains a single declaration, this is
74 /// the index associated with that entry.
75 unsigned SingleDeclIndex;
76
77 public:
78 ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { }
79
80 void Add(NamedDecl *ND, unsigned Index) {
81 if (DeclOrVector.isNull()) {
82 // 0 - > 1 elements: just set the single element information.
83 DeclOrVector = ND;
84 SingleDeclIndex = Index;
85 return;
86 }
87
88 if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) {
89 // 1 -> 2 elements: create the vector of results and push in the
90 // existing declaration.
91 DeclIndexPairVector *Vec = new DeclIndexPairVector;
92 Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex));
93 DeclOrVector = Vec;
94 }
95
96 // Add the new element to the end of the vector.
97 DeclOrVector.get<DeclIndexPairVector*>()->push_back(
98 DeclIndexPair(ND, Index));
99 }
100
101 void Destroy() {
102 if (DeclIndexPairVector *Vec
103 = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) {
104 delete Vec;
105 DeclOrVector = ((NamedDecl *)0);
106 }
107 }
108
109 // Iteration.
110 class iterator;
111 iterator begin() const;
112 iterator end() const;
113 };
114
Douglas Gregor86d9a522009-09-21 16:56:56 +0000115 /// \brief A mapping from declaration names to the declarations that have
116 /// this name within a particular scope and their index within the list of
117 /// results.
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000118 typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000119
120 /// \brief The semantic analysis object for which results are being
121 /// produced.
122 Sema &SemaRef;
Douglas Gregor218937c2011-02-01 19:23:04 +0000123
124 /// \brief The allocator used to allocate new code-completion strings.
Douglas Gregordae68752011-02-01 22:57:45 +0000125 CodeCompletionAllocator &Allocator;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000126
127 CodeCompletionTUInfo &CCTUInfo;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000128
129 /// \brief If non-NULL, a filter function used to remove any code-completion
130 /// results that are not desirable.
131 LookupFilter Filter;
Douglas Gregor45bcd432010-01-14 03:21:49 +0000132
133 /// \brief Whether we should allow declarations as
134 /// nested-name-specifiers that would otherwise be filtered out.
135 bool AllowNestedNameSpecifiers;
136
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000137 /// \brief If set, the type that we would prefer our resulting value
138 /// declarations to have.
139 ///
140 /// Closely matching the preferred type gives a boost to a result's
141 /// priority.
142 CanQualType PreferredType;
143
Douglas Gregor86d9a522009-09-21 16:56:56 +0000144 /// \brief A list of shadow maps, which is used to model name hiding at
145 /// different levels of, e.g., the inheritance hierarchy.
146 std::list<ShadowMap> ShadowMaps;
147
Douglas Gregor3cdee122010-08-26 16:36:48 +0000148 /// \brief If we're potentially referring to a C++ member function, the set
149 /// of qualifiers applied to the object type.
150 Qualifiers ObjectTypeQualifiers;
151
152 /// \brief Whether the \p ObjectTypeQualifiers field is active.
153 bool HasObjectTypeQualifiers;
154
Douglas Gregor265f7492010-08-27 15:29:55 +0000155 /// \brief The selector that we prefer.
156 Selector PreferredSelector;
157
Douglas Gregorca45da02010-11-02 20:36:02 +0000158 /// \brief The completion context in which we are gathering results.
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000159 CodeCompletionContext CompletionContext;
160
James Dennetta40f7922012-06-14 03:11:41 +0000161 /// \brief If we are in an instance method definition, the \@implementation
Douglas Gregorca45da02010-11-02 20:36:02 +0000162 /// object.
163 ObjCImplementationDecl *ObjCImplementation;
164
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000165 void AdjustResultPriorityForDecl(Result &R);
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000166
Douglas Gregor6f942b22010-09-21 16:06:22 +0000167 void MaybeAddConstructorResults(Result R);
168
Douglas Gregor86d9a522009-09-21 16:56:56 +0000169 public:
Douglas Gregordae68752011-02-01 22:57:45 +0000170 explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000171 CodeCompletionTUInfo &CCTUInfo,
Douglas Gregor52779fb2010-09-23 23:01:17 +0000172 const CodeCompletionContext &CompletionContext,
173 LookupFilter Filter = 0)
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000174 : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo),
175 Filter(Filter),
Douglas Gregor218937c2011-02-01 19:23:04 +0000176 AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false),
Douglas Gregorca45da02010-11-02 20:36:02 +0000177 CompletionContext(CompletionContext),
178 ObjCImplementation(0)
179 {
180 // If this is an Objective-C instance method definition, dig out the
181 // corresponding implementation.
182 switch (CompletionContext.getKind()) {
183 case CodeCompletionContext::CCC_Expression:
184 case CodeCompletionContext::CCC_ObjCMessageReceiver:
185 case CodeCompletionContext::CCC_ParenthesizedExpression:
186 case CodeCompletionContext::CCC_Statement:
187 case CodeCompletionContext::CCC_Recovery:
188 if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl())
189 if (Method->isInstanceMethod())
190 if (ObjCInterfaceDecl *Interface = Method->getClassInterface())
191 ObjCImplementation = Interface->getImplementation();
192 break;
193
194 default:
195 break;
196 }
197 }
Douglas Gregor86d9a522009-09-21 16:56:56 +0000198
Douglas Gregord8e8a582010-05-25 21:41:55 +0000199 /// \brief Whether we should include code patterns in the completion
200 /// results.
201 bool includeCodePatterns() const {
202 return SemaRef.CodeCompleter &&
Douglas Gregorf6961522010-08-27 21:18:54 +0000203 SemaRef.CodeCompleter->includeCodePatterns();
Douglas Gregord8e8a582010-05-25 21:41:55 +0000204 }
205
Douglas Gregor86d9a522009-09-21 16:56:56 +0000206 /// \brief Set the filter used for code-completion results.
207 void setFilter(LookupFilter Filter) {
208 this->Filter = Filter;
209 }
210
Douglas Gregor86d9a522009-09-21 16:56:56 +0000211 Result *data() { return Results.empty()? 0 : &Results.front(); }
212 unsigned size() const { return Results.size(); }
213 bool empty() const { return Results.empty(); }
214
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000215 /// \brief Specify the preferred type.
216 void setPreferredType(QualType T) {
217 PreferredType = SemaRef.Context.getCanonicalType(T);
218 }
219
Douglas Gregor3cdee122010-08-26 16:36:48 +0000220 /// \brief Set the cv-qualifiers on the object type, for us in filtering
221 /// calls to member functions.
222 ///
223 /// When there are qualifiers in this set, they will be used to filter
224 /// out member functions that aren't available (because there will be a
225 /// cv-qualifier mismatch) or prefer functions with an exact qualifier
226 /// match.
227 void setObjectTypeQualifiers(Qualifiers Quals) {
228 ObjectTypeQualifiers = Quals;
229 HasObjectTypeQualifiers = true;
230 }
231
Douglas Gregor265f7492010-08-27 15:29:55 +0000232 /// \brief Set the preferred selector.
233 ///
234 /// When an Objective-C method declaration result is added, and that
235 /// method's selector matches this preferred selector, we give that method
236 /// a slight priority boost.
237 void setPreferredSelector(Selector Sel) {
238 PreferredSelector = Sel;
239 }
Douglas Gregorca45da02010-11-02 20:36:02 +0000240
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000241 /// \brief Retrieve the code-completion context for which results are
242 /// being collected.
243 const CodeCompletionContext &getCompletionContext() const {
244 return CompletionContext;
245 }
246
Douglas Gregor45bcd432010-01-14 03:21:49 +0000247 /// \brief Specify whether nested-name-specifiers are allowed.
248 void allowNestedNameSpecifiers(bool Allow = true) {
249 AllowNestedNameSpecifiers = Allow;
250 }
251
Douglas Gregorb9d77572010-09-21 00:03:25 +0000252 /// \brief Return the semantic analysis object for which we are collecting
253 /// code completion results.
254 Sema &getSema() const { return SemaRef; }
255
Douglas Gregor218937c2011-02-01 19:23:04 +0000256 /// \brief Retrieve the allocator used to allocate code completion strings.
Douglas Gregordae68752011-02-01 22:57:45 +0000257 CodeCompletionAllocator &getAllocator() const { return Allocator; }
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000258
259 CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
Douglas Gregor218937c2011-02-01 19:23:04 +0000260
Douglas Gregore495b7f2010-01-14 00:20:49 +0000261 /// \brief Determine whether the given declaration is at all interesting
262 /// as a code-completion result.
Douglas Gregor45bcd432010-01-14 03:21:49 +0000263 ///
264 /// \param ND the declaration that we are inspecting.
265 ///
266 /// \param AsNestedNameSpecifier will be set true if this declaration is
267 /// only interesting when it is a nested-name-specifier.
268 bool isInterestingDecl(NamedDecl *ND, bool &AsNestedNameSpecifier) const;
Douglas Gregor6660d842010-01-14 00:41:07 +0000269
270 /// \brief Check whether the result is hidden by the Hiding declaration.
271 ///
272 /// \returns true if the result is hidden and cannot be found, false if
273 /// the hidden result could still be found. When false, \p R may be
274 /// modified to describe how the result can be found (e.g., via extra
275 /// qualification).
276 bool CheckHiddenResult(Result &R, DeclContext *CurContext,
277 NamedDecl *Hiding);
278
Douglas Gregor86d9a522009-09-21 16:56:56 +0000279 /// \brief Add a new result to this result set (if it isn't already in one
280 /// of the shadow maps), or replace an existing result (for, e.g., a
281 /// redeclaration).
Douglas Gregor456c4a12009-09-21 20:12:40 +0000282 ///
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000283 /// \param R the result to add (if it is unique).
Douglas Gregor456c4a12009-09-21 20:12:40 +0000284 ///
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000285 /// \param CurContext the context in which this result will be named.
Douglas Gregor456c4a12009-09-21 20:12:40 +0000286 void MaybeAddResult(Result R, DeclContext *CurContext = 0);
Douglas Gregor86d9a522009-09-21 16:56:56 +0000287
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000288 /// \brief Add a new result to this result set, where we already know
289 /// the hiding declation (if any).
290 ///
291 /// \param R the result to add (if it is unique).
292 ///
293 /// \param CurContext the context in which this result will be named.
294 ///
295 /// \param Hiding the declaration that hides the result.
Douglas Gregor0cc84042010-01-14 15:47:35 +0000296 ///
297 /// \param InBaseClass whether the result was found in a base
298 /// class of the searched context.
299 void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding,
300 bool InBaseClass);
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000301
Douglas Gregora4477812010-01-14 16:01:26 +0000302 /// \brief Add a new non-declaration result to this result set.
303 void AddResult(Result R);
304
Douglas Gregor86d9a522009-09-21 16:56:56 +0000305 /// \brief Enter into a new scope.
306 void EnterNewScope();
307
308 /// \brief Exit from the current scope.
309 void ExitScope();
310
Douglas Gregor55385fe2009-11-18 04:19:12 +0000311 /// \brief Ignore this declaration, if it is seen again.
312 void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); }
313
Douglas Gregor86d9a522009-09-21 16:56:56 +0000314 /// \name Name lookup predicates
315 ///
316 /// These predicates can be passed to the name lookup functions to filter the
317 /// results of name lookup. All of the predicates have the same type, so that
318 ///
319 //@{
Douglas Gregor791215b2009-09-21 20:51:25 +0000320 bool IsOrdinaryName(NamedDecl *ND) const;
Douglas Gregor4710e5b2010-05-28 00:49:12 +0000321 bool IsOrdinaryNonTypeName(NamedDecl *ND) const;
Douglas Gregorf9578432010-07-28 21:50:18 +0000322 bool IsIntegralConstantValue(NamedDecl *ND) const;
Douglas Gregor01dfea02010-01-10 23:08:15 +0000323 bool IsOrdinaryNonValueName(NamedDecl *ND) const;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000324 bool IsNestedNameSpecifier(NamedDecl *ND) const;
325 bool IsEnum(NamedDecl *ND) const;
326 bool IsClassOrStruct(NamedDecl *ND) const;
327 bool IsUnion(NamedDecl *ND) const;
328 bool IsNamespace(NamedDecl *ND) const;
329 bool IsNamespaceOrAlias(NamedDecl *ND) const;
330 bool IsType(NamedDecl *ND) const;
Douglas Gregoreb5758b2009-09-23 22:26:46 +0000331 bool IsMember(NamedDecl *ND) const;
Douglas Gregor80f4f4c2010-01-14 16:08:12 +0000332 bool IsObjCIvar(NamedDecl *ND) const;
Douglas Gregor8e254cf2010-05-27 23:06:34 +0000333 bool IsObjCMessageReceiver(NamedDecl *ND) const;
Douglas Gregor81f3bff2012-02-15 15:34:24 +0000334 bool IsObjCMessageReceiverOrLambdaCapture(NamedDecl *ND) const;
Douglas Gregorfb629412010-08-23 21:17:50 +0000335 bool IsObjCCollection(NamedDecl *ND) const;
Douglas Gregor52779fb2010-09-23 23:01:17 +0000336 bool IsImpossibleToSatisfy(NamedDecl *ND) const;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000337 //@}
338 };
339}
340
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000341class ResultBuilder::ShadowMapEntry::iterator {
342 llvm::PointerUnion<NamedDecl*, const DeclIndexPair*> DeclOrIterator;
343 unsigned SingleDeclIndex;
344
345public:
346 typedef DeclIndexPair value_type;
347 typedef value_type reference;
348 typedef std::ptrdiff_t difference_type;
349 typedef std::input_iterator_tag iterator_category;
350
351 class pointer {
352 DeclIndexPair Value;
353
354 public:
355 pointer(const DeclIndexPair &Value) : Value(Value) { }
356
357 const DeclIndexPair *operator->() const {
358 return &Value;
359 }
360 };
361
362 iterator() : DeclOrIterator((NamedDecl *)0), SingleDeclIndex(0) { }
363
364 iterator(NamedDecl *SingleDecl, unsigned Index)
365 : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { }
366
367 iterator(const DeclIndexPair *Iterator)
368 : DeclOrIterator(Iterator), SingleDeclIndex(0) { }
369
370 iterator &operator++() {
371 if (DeclOrIterator.is<NamedDecl *>()) {
372 DeclOrIterator = (NamedDecl *)0;
373 SingleDeclIndex = 0;
374 return *this;
375 }
376
377 const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>();
378 ++I;
379 DeclOrIterator = I;
380 return *this;
381 }
382
Chris Lattner66392d42010-09-04 18:12:20 +0000383 /*iterator operator++(int) {
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000384 iterator tmp(*this);
385 ++(*this);
386 return tmp;
Chris Lattner66392d42010-09-04 18:12:20 +0000387 }*/
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000388
389 reference operator*() const {
390 if (NamedDecl *ND = DeclOrIterator.dyn_cast<NamedDecl *>())
391 return reference(ND, SingleDeclIndex);
392
Douglas Gregord490f952009-12-06 21:27:58 +0000393 return *DeclOrIterator.get<const DeclIndexPair*>();
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000394 }
395
396 pointer operator->() const {
397 return pointer(**this);
398 }
399
400 friend bool operator==(const iterator &X, const iterator &Y) {
Douglas Gregord490f952009-12-06 21:27:58 +0000401 return X.DeclOrIterator.getOpaqueValue()
402 == Y.DeclOrIterator.getOpaqueValue() &&
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000403 X.SingleDeclIndex == Y.SingleDeclIndex;
404 }
405
406 friend bool operator!=(const iterator &X, const iterator &Y) {
Douglas Gregord490f952009-12-06 21:27:58 +0000407 return !(X == Y);
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000408 }
409};
410
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000411ResultBuilder::ShadowMapEntry::iterator
412ResultBuilder::ShadowMapEntry::begin() const {
413 if (DeclOrVector.isNull())
414 return iterator();
415
416 if (NamedDecl *ND = DeclOrVector.dyn_cast<NamedDecl *>())
417 return iterator(ND, SingleDeclIndex);
418
419 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin());
420}
421
422ResultBuilder::ShadowMapEntry::iterator
423ResultBuilder::ShadowMapEntry::end() const {
424 if (DeclOrVector.is<NamedDecl *>() || DeclOrVector.isNull())
425 return iterator();
426
427 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end());
428}
429
Douglas Gregor456c4a12009-09-21 20:12:40 +0000430/// \brief Compute the qualification required to get from the current context
431/// (\p CurContext) to the target context (\p TargetContext).
432///
433/// \param Context the AST context in which the qualification will be used.
434///
435/// \param CurContext the context where an entity is being named, which is
436/// typically based on the current scope.
437///
438/// \param TargetContext the context in which the named entity actually
439/// resides.
440///
441/// \returns a nested name specifier that refers into the target context, or
442/// NULL if no qualification is needed.
443static NestedNameSpecifier *
444getRequiredQualification(ASTContext &Context,
445 DeclContext *CurContext,
446 DeclContext *TargetContext) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000447 SmallVector<DeclContext *, 4> TargetParents;
Douglas Gregor456c4a12009-09-21 20:12:40 +0000448
449 for (DeclContext *CommonAncestor = TargetContext;
450 CommonAncestor && !CommonAncestor->Encloses(CurContext);
451 CommonAncestor = CommonAncestor->getLookupParent()) {
452 if (CommonAncestor->isTransparentContext() ||
453 CommonAncestor->isFunctionOrMethod())
454 continue;
455
456 TargetParents.push_back(CommonAncestor);
457 }
458
459 NestedNameSpecifier *Result = 0;
460 while (!TargetParents.empty()) {
461 DeclContext *Parent = TargetParents.back();
462 TargetParents.pop_back();
463
Douglas Gregorfb629412010-08-23 21:17:50 +0000464 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) {
465 if (!Namespace->getIdentifier())
466 continue;
467
Douglas Gregor456c4a12009-09-21 20:12:40 +0000468 Result = NestedNameSpecifier::Create(Context, Result, Namespace);
Douglas Gregorfb629412010-08-23 21:17:50 +0000469 }
Douglas Gregor456c4a12009-09-21 20:12:40 +0000470 else if (TagDecl *TD = dyn_cast<TagDecl>(Parent))
471 Result = NestedNameSpecifier::Create(Context, Result,
472 false,
473 Context.getTypeDeclType(TD).getTypePtr());
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000474 }
Douglas Gregor456c4a12009-09-21 20:12:40 +0000475 return Result;
476}
477
Douglas Gregor45bcd432010-01-14 03:21:49 +0000478bool ResultBuilder::isInterestingDecl(NamedDecl *ND,
479 bool &AsNestedNameSpecifier) const {
480 AsNestedNameSpecifier = false;
481
Douglas Gregore495b7f2010-01-14 00:20:49 +0000482 ND = ND->getUnderlyingDecl();
483 unsigned IDNS = ND->getIdentifierNamespace();
Douglas Gregorf52cede2009-10-09 22:16:47 +0000484
485 // Skip unnamed entities.
Douglas Gregore495b7f2010-01-14 00:20:49 +0000486 if (!ND->getDeclName())
487 return false;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000488
489 // Friend declarations and declarations introduced due to friends are never
490 // added as results.
John McCall92b7f702010-03-11 07:50:04 +0000491 if (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend))
Douglas Gregore495b7f2010-01-14 00:20:49 +0000492 return false;
493
Douglas Gregor76282942009-12-11 17:31:05 +0000494 // Class template (partial) specializations are never added as results.
Douglas Gregore495b7f2010-01-14 00:20:49 +0000495 if (isa<ClassTemplateSpecializationDecl>(ND) ||
496 isa<ClassTemplatePartialSpecializationDecl>(ND))
497 return false;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000498
Douglas Gregor76282942009-12-11 17:31:05 +0000499 // Using declarations themselves are never added as results.
Douglas Gregore495b7f2010-01-14 00:20:49 +0000500 if (isa<UsingDecl>(ND))
501 return false;
502
503 // Some declarations have reserved names that we don't want to ever show.
504 if (const IdentifierInfo *Id = ND->getIdentifier()) {
Douglas Gregor86d9a522009-09-21 16:56:56 +0000505 // __va_list_tag is a freak of nature. Find it and skip it.
506 if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list"))
Douglas Gregore495b7f2010-01-14 00:20:49 +0000507 return false;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000508
Douglas Gregorf52cede2009-10-09 22:16:47 +0000509 // Filter out names reserved for the implementation (C99 7.1.3,
Douglas Gregor797efb52010-07-14 17:44:04 +0000510 // C++ [lib.global.names]) if they come from a system header.
Daniel Dunbare013d682009-10-18 20:26:12 +0000511 //
512 // FIXME: Add predicate for this.
Douglas Gregorf52cede2009-10-09 22:16:47 +0000513 if (Id->getLength() >= 2) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000514 const char *Name = Id->getNameStart();
Douglas Gregorf52cede2009-10-09 22:16:47 +0000515 if (Name[0] == '_' &&
Douglas Gregor797efb52010-07-14 17:44:04 +0000516 (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')) &&
517 (ND->getLocation().isInvalid() ||
518 SemaRef.SourceMgr.isInSystemHeader(
519 SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))))
Douglas Gregore495b7f2010-01-14 00:20:49 +0000520 return false;
Douglas Gregorf52cede2009-10-09 22:16:47 +0000521 }
Douglas Gregor86d9a522009-09-21 16:56:56 +0000522 }
Douglas Gregor9b0ba872010-11-09 03:59:40 +0000523
Douglas Gregora5fb7c32010-08-16 23:05:20 +0000524 if (Filter == &ResultBuilder::IsNestedNameSpecifier ||
525 ((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
526 Filter != &ResultBuilder::IsNamespace &&
Douglas Gregor52779fb2010-09-23 23:01:17 +0000527 Filter != &ResultBuilder::IsNamespaceOrAlias &&
528 Filter != 0))
Douglas Gregora5fb7c32010-08-16 23:05:20 +0000529 AsNestedNameSpecifier = true;
530
Douglas Gregor86d9a522009-09-21 16:56:56 +0000531 // Filter out any unwanted results.
Douglas Gregor45bcd432010-01-14 03:21:49 +0000532 if (Filter && !(this->*Filter)(ND)) {
533 // Check whether it is interesting as a nested-name-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +0000534 if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus &&
Douglas Gregor45bcd432010-01-14 03:21:49 +0000535 IsNestedNameSpecifier(ND) &&
536 (Filter != &ResultBuilder::IsMember ||
537 (isa<CXXRecordDecl>(ND) &&
538 cast<CXXRecordDecl>(ND)->isInjectedClassName()))) {
539 AsNestedNameSpecifier = true;
540 return true;
541 }
542
Douglas Gregore495b7f2010-01-14 00:20:49 +0000543 return false;
Douglas Gregora5fb7c32010-08-16 23:05:20 +0000544 }
Douglas Gregore495b7f2010-01-14 00:20:49 +0000545 // ... then it must be interesting!
546 return true;
547}
548
Douglas Gregor6660d842010-01-14 00:41:07 +0000549bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext,
550 NamedDecl *Hiding) {
551 // In C, there is no way to refer to a hidden name.
552 // FIXME: This isn't true; we can find a tag name hidden by an ordinary
553 // name if we introduce the tag type.
David Blaikie4e4d0842012-03-11 07:00:24 +0000554 if (!SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor6660d842010-01-14 00:41:07 +0000555 return true;
556
Sebastian Redl7a126a42010-08-31 00:36:30 +0000557 DeclContext *HiddenCtx = R.Declaration->getDeclContext()->getRedeclContext();
Douglas Gregor6660d842010-01-14 00:41:07 +0000558
559 // There is no way to qualify a name declared in a function or method.
560 if (HiddenCtx->isFunctionOrMethod())
561 return true;
562
Sebastian Redl7a126a42010-08-31 00:36:30 +0000563 if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext())
Douglas Gregor6660d842010-01-14 00:41:07 +0000564 return true;
565
566 // We can refer to the result with the appropriate qualification. Do it.
567 R.Hidden = true;
568 R.QualifierIsInformative = false;
569
570 if (!R.Qualifier)
571 R.Qualifier = getRequiredQualification(SemaRef.Context,
572 CurContext,
573 R.Declaration->getDeclContext());
574 return false;
575}
576
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000577/// \brief A simplified classification of types used to determine whether two
578/// types are "similar enough" when adjusting priorities.
Douglas Gregor1827e102010-08-16 16:18:59 +0000579SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000580 switch (T->getTypeClass()) {
581 case Type::Builtin:
582 switch (cast<BuiltinType>(T)->getKind()) {
583 case BuiltinType::Void:
584 return STC_Void;
585
586 case BuiltinType::NullPtr:
587 return STC_Pointer;
588
589 case BuiltinType::Overload:
590 case BuiltinType::Dependent:
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000591 return STC_Other;
592
593 case BuiltinType::ObjCId:
594 case BuiltinType::ObjCClass:
595 case BuiltinType::ObjCSel:
596 return STC_ObjectiveC;
597
598 default:
599 return STC_Arithmetic;
600 }
David Blaikie7530c032012-01-17 06:56:22 +0000601
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000602 case Type::Complex:
603 return STC_Arithmetic;
604
605 case Type::Pointer:
606 return STC_Pointer;
607
608 case Type::BlockPointer:
609 return STC_Block;
610
611 case Type::LValueReference:
612 case Type::RValueReference:
613 return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType());
614
615 case Type::ConstantArray:
616 case Type::IncompleteArray:
617 case Type::VariableArray:
618 case Type::DependentSizedArray:
619 return STC_Array;
620
621 case Type::DependentSizedExtVector:
622 case Type::Vector:
623 case Type::ExtVector:
624 return STC_Arithmetic;
625
626 case Type::FunctionProto:
627 case Type::FunctionNoProto:
628 return STC_Function;
629
630 case Type::Record:
631 return STC_Record;
632
633 case Type::Enum:
634 return STC_Arithmetic;
635
636 case Type::ObjCObject:
637 case Type::ObjCInterface:
638 case Type::ObjCObjectPointer:
639 return STC_ObjectiveC;
640
641 default:
642 return STC_Other;
643 }
644}
645
646/// \brief Get the type that a given expression will have if this declaration
647/// is used as an expression in its "typical" code-completion form.
Douglas Gregor1827e102010-08-16 16:18:59 +0000648QualType clang::getDeclUsageType(ASTContext &C, NamedDecl *ND) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000649 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
650
651 if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
652 return C.getTypeDeclType(Type);
653 if (ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND))
654 return C.getObjCInterfaceType(Iface);
655
656 QualType T;
657 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND))
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000658 T = Function->getCallResultType();
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000659 else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000660 T = Method->getSendResultType();
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000661 else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND))
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000662 T = FunTmpl->getTemplatedDecl()->getCallResultType();
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000663 else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND))
664 T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext()));
665 else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND))
666 T = Property->getType();
667 else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND))
668 T = Value->getType();
669 else
670 return QualType();
Douglas Gregor3e64d562011-04-14 20:33:34 +0000671
672 // Dig through references, function pointers, and block pointers to
673 // get down to the likely type of an expression when the entity is
674 // used.
675 do {
676 if (const ReferenceType *Ref = T->getAs<ReferenceType>()) {
677 T = Ref->getPointeeType();
678 continue;
679 }
680
681 if (const PointerType *Pointer = T->getAs<PointerType>()) {
682 if (Pointer->getPointeeType()->isFunctionType()) {
683 T = Pointer->getPointeeType();
684 continue;
685 }
686
687 break;
688 }
689
690 if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) {
691 T = Block->getPointeeType();
692 continue;
693 }
694
695 if (const FunctionType *Function = T->getAs<FunctionType>()) {
696 T = Function->getResultType();
697 continue;
698 }
699
700 break;
701 } while (true);
702
703 return T;
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000704}
705
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000706void ResultBuilder::AdjustResultPriorityForDecl(Result &R) {
707 // If this is an Objective-C method declaration whose selector matches our
708 // preferred selector, give it a priority boost.
709 if (!PreferredSelector.isNull())
710 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration))
711 if (PreferredSelector == Method->getSelector())
712 R.Priority += CCD_SelectorMatch;
Douglas Gregor08f43cd2010-09-20 23:11:55 +0000713
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000714 // If we have a preferred type, adjust the priority for results with exactly-
715 // matching or nearly-matching types.
716 if (!PreferredType.isNull()) {
717 QualType T = getDeclUsageType(SemaRef.Context, R.Declaration);
718 if (!T.isNull()) {
719 CanQualType TC = SemaRef.Context.getCanonicalType(T);
720 // Check for exactly-matching types (modulo qualifiers).
721 if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC))
722 R.Priority /= CCF_ExactTypeMatch;
723 // Check for nearly-matching types, based on classification of each.
724 else if ((getSimplifiedTypeClass(PreferredType)
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000725 == getSimplifiedTypeClass(TC)) &&
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000726 !(PreferredType->isEnumeralType() && TC->isEnumeralType()))
727 R.Priority /= CCF_SimilarTypeMatch;
728 }
729 }
Douglas Gregor1f5537a2010-07-08 23:20:03 +0000730}
731
Douglas Gregor6f942b22010-09-21 16:06:22 +0000732void ResultBuilder::MaybeAddConstructorResults(Result R) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000733 if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration ||
Douglas Gregor6f942b22010-09-21 16:06:22 +0000734 !CompletionContext.wantConstructorResults())
735 return;
736
737 ASTContext &Context = SemaRef.Context;
738 NamedDecl *D = R.Declaration;
739 CXXRecordDecl *Record = 0;
740 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D))
741 Record = ClassTemplate->getTemplatedDecl();
742 else if ((Record = dyn_cast<CXXRecordDecl>(D))) {
743 // Skip specializations and partial specializations.
744 if (isa<ClassTemplateSpecializationDecl>(Record))
745 return;
746 } else {
747 // There are no constructors here.
748 return;
749 }
750
751 Record = Record->getDefinition();
752 if (!Record)
753 return;
754
755
756 QualType RecordTy = Context.getTypeDeclType(Record);
757 DeclarationName ConstructorName
758 = Context.DeclarationNames.getCXXConstructorName(
759 Context.getCanonicalType(RecordTy));
760 for (DeclContext::lookup_result Ctors = Record->lookup(ConstructorName);
761 Ctors.first != Ctors.second; ++Ctors.first) {
762 R.Declaration = *Ctors.first;
763 R.CursorKind = getCursorKindForDecl(R.Declaration);
764 Results.push_back(R);
765 }
766}
767
Douglas Gregore495b7f2010-01-14 00:20:49 +0000768void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) {
769 assert(!ShadowMaps.empty() && "Must enter into a results scope");
770
771 if (R.Kind != Result::RK_Declaration) {
772 // For non-declaration results, just add the result.
773 Results.push_back(R);
774 return;
775 }
776
777 // Look through using declarations.
778 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
779 MaybeAddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext);
780 return;
781 }
782
783 Decl *CanonDecl = R.Declaration->getCanonicalDecl();
784 unsigned IDNS = CanonDecl->getIdentifierNamespace();
785
Douglas Gregor45bcd432010-01-14 03:21:49 +0000786 bool AsNestedNameSpecifier = false;
787 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
Douglas Gregore495b7f2010-01-14 00:20:49 +0000788 return;
789
Douglas Gregor6f942b22010-09-21 16:06:22 +0000790 // C++ constructors are never found by name lookup.
791 if (isa<CXXConstructorDecl>(R.Declaration))
792 return;
793
Douglas Gregor86d9a522009-09-21 16:56:56 +0000794 ShadowMap &SMap = ShadowMaps.back();
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000795 ShadowMapEntry::iterator I, IEnd;
796 ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName());
797 if (NamePos != SMap.end()) {
798 I = NamePos->second.begin();
799 IEnd = NamePos->second.end();
800 }
801
802 for (; I != IEnd; ++I) {
803 NamedDecl *ND = I->first;
804 unsigned Index = I->second;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000805 if (ND->getCanonicalDecl() == CanonDecl) {
806 // This is a redeclaration. Always pick the newer declaration.
Douglas Gregor86d9a522009-09-21 16:56:56 +0000807 Results[Index].Declaration = R.Declaration;
808
Douglas Gregor86d9a522009-09-21 16:56:56 +0000809 // We're done.
810 return;
811 }
812 }
813
814 // This is a new declaration in this scope. However, check whether this
815 // declaration name is hidden by a similarly-named declaration in an outer
816 // scope.
817 std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end();
818 --SMEnd;
819 for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) {
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000820 ShadowMapEntry::iterator I, IEnd;
821 ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName());
822 if (NamePos != SM->end()) {
823 I = NamePos->second.begin();
824 IEnd = NamePos->second.end();
825 }
826 for (; I != IEnd; ++I) {
Douglas Gregor86d9a522009-09-21 16:56:56 +0000827 // A tag declaration does not hide a non-tag declaration.
John McCall0d6b1642010-04-23 18:46:30 +0000828 if (I->first->hasTagIdentifierNamespace() &&
Douglas Gregor86d9a522009-09-21 16:56:56 +0000829 (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary |
830 Decl::IDNS_ObjCProtocol)))
831 continue;
832
833 // Protocols are in distinct namespaces from everything else.
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000834 if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol)
Douglas Gregor86d9a522009-09-21 16:56:56 +0000835 || (IDNS & Decl::IDNS_ObjCProtocol)) &&
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000836 I->first->getIdentifierNamespace() != IDNS)
Douglas Gregor86d9a522009-09-21 16:56:56 +0000837 continue;
838
839 // The newly-added result is hidden by an entry in the shadow map.
Douglas Gregor6660d842010-01-14 00:41:07 +0000840 if (CheckHiddenResult(R, CurContext, I->first))
Douglas Gregor86d9a522009-09-21 16:56:56 +0000841 return;
Douglas Gregor86d9a522009-09-21 16:56:56 +0000842
843 break;
844 }
845 }
846
847 // Make sure that any given declaration only shows up in the result set once.
848 if (!AllDeclsFound.insert(CanonDecl))
849 return;
Douglas Gregor265f7492010-08-27 15:29:55 +0000850
Douglas Gregoreb5758b2009-09-23 22:26:46 +0000851 // If the filter is for nested-name-specifiers, then this result starts a
852 // nested-name-specifier.
Douglas Gregor12e13132010-05-26 22:00:08 +0000853 if (AsNestedNameSpecifier) {
Douglas Gregoreb5758b2009-09-23 22:26:46 +0000854 R.StartsNestedNameSpecifier = true;
Douglas Gregor12e13132010-05-26 22:00:08 +0000855 R.Priority = CCP_NestedNameSpecifier;
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000856 } else
857 AdjustResultPriorityForDecl(R);
Douglas Gregor265f7492010-08-27 15:29:55 +0000858
Douglas Gregor0563c262009-09-22 23:15:58 +0000859 // If this result is supposed to have an informative qualifier, add one.
Douglas Gregoreb5758b2009-09-23 22:26:46 +0000860 if (R.QualifierIsInformative && !R.Qualifier &&
861 !R.StartsNestedNameSpecifier) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000862 DeclContext *Ctx = R.Declaration->getDeclContext();
863 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
864 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
865 else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
866 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
867 SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
868 else
869 R.QualifierIsInformative = false;
870 }
Douglas Gregoreb5758b2009-09-23 22:26:46 +0000871
Douglas Gregor86d9a522009-09-21 16:56:56 +0000872 // Insert this result into the set of results and into the current shadow
873 // map.
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000874 SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size());
Douglas Gregor86d9a522009-09-21 16:56:56 +0000875 Results.push_back(R);
Douglas Gregor6f942b22010-09-21 16:06:22 +0000876
877 if (!AsNestedNameSpecifier)
878 MaybeAddConstructorResults(R);
Douglas Gregor86d9a522009-09-21 16:56:56 +0000879}
880
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000881void ResultBuilder::AddResult(Result R, DeclContext *CurContext,
Douglas Gregor0cc84042010-01-14 15:47:35 +0000882 NamedDecl *Hiding, bool InBaseClass = false) {
Douglas Gregora4477812010-01-14 16:01:26 +0000883 if (R.Kind != Result::RK_Declaration) {
884 // For non-declaration results, just add the result.
885 Results.push_back(R);
886 return;
887 }
888
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000889 // Look through using declarations.
890 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
891 AddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext, Hiding);
892 return;
893 }
894
Douglas Gregor45bcd432010-01-14 03:21:49 +0000895 bool AsNestedNameSpecifier = false;
896 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000897 return;
898
Douglas Gregor6f942b22010-09-21 16:06:22 +0000899 // C++ constructors are never found by name lookup.
900 if (isa<CXXConstructorDecl>(R.Declaration))
901 return;
902
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000903 if (Hiding && CheckHiddenResult(R, CurContext, Hiding))
904 return;
Nick Lewycky173a37a2012-04-03 21:44:08 +0000905
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000906 // Make sure that any given declaration only shows up in the result set once.
907 if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()))
908 return;
909
910 // If the filter is for nested-name-specifiers, then this result starts a
911 // nested-name-specifier.
Douglas Gregor12e13132010-05-26 22:00:08 +0000912 if (AsNestedNameSpecifier) {
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000913 R.StartsNestedNameSpecifier = true;
Douglas Gregor12e13132010-05-26 22:00:08 +0000914 R.Priority = CCP_NestedNameSpecifier;
915 }
Douglas Gregor0cc84042010-01-14 15:47:35 +0000916 else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass &&
917 isa<CXXRecordDecl>(R.Declaration->getDeclContext()
Sebastian Redl7a126a42010-08-31 00:36:30 +0000918 ->getRedeclContext()))
Douglas Gregor0cc84042010-01-14 15:47:35 +0000919 R.QualifierIsInformative = true;
920
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000921 // If this result is supposed to have an informative qualifier, add one.
922 if (R.QualifierIsInformative && !R.Qualifier &&
923 !R.StartsNestedNameSpecifier) {
924 DeclContext *Ctx = R.Declaration->getDeclContext();
925 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
926 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
927 else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
928 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
Douglas Gregor45bcd432010-01-14 03:21:49 +0000929 SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000930 else
931 R.QualifierIsInformative = false;
932 }
933
Douglas Gregor12e13132010-05-26 22:00:08 +0000934 // Adjust the priority if this result comes from a base class.
935 if (InBaseClass)
936 R.Priority += CCD_InBaseClass;
937
Douglas Gregorcee9ff12010-09-20 22:39:41 +0000938 AdjustResultPriorityForDecl(R);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000939
Douglas Gregor3cdee122010-08-26 16:36:48 +0000940 if (HasObjectTypeQualifiers)
941 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration))
942 if (Method->isInstance()) {
943 Qualifiers MethodQuals
944 = Qualifiers::fromCVRMask(Method->getTypeQualifiers());
945 if (ObjectTypeQualifiers == MethodQuals)
946 R.Priority += CCD_ObjectQualifierMatch;
947 else if (ObjectTypeQualifiers - MethodQuals) {
948 // The method cannot be invoked, because doing so would drop
949 // qualifiers.
950 return;
951 }
952 }
953
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000954 // Insert this result into the set of results.
955 Results.push_back(R);
Douglas Gregor6f942b22010-09-21 16:06:22 +0000956
957 if (!AsNestedNameSpecifier)
958 MaybeAddConstructorResults(R);
Douglas Gregor1ca6ae82010-01-14 01:09:38 +0000959}
960
Douglas Gregora4477812010-01-14 16:01:26 +0000961void ResultBuilder::AddResult(Result R) {
962 assert(R.Kind != Result::RK_Declaration &&
963 "Declaration results need more context");
964 Results.push_back(R);
965}
966
Douglas Gregor86d9a522009-09-21 16:56:56 +0000967/// \brief Enter into a new scope.
968void ResultBuilder::EnterNewScope() {
969 ShadowMaps.push_back(ShadowMap());
970}
971
972/// \brief Exit from the current scope.
973void ResultBuilder::ExitScope() {
Douglas Gregorfbcb5d62009-12-06 20:23:50 +0000974 for (ShadowMap::iterator E = ShadowMaps.back().begin(),
975 EEnd = ShadowMaps.back().end();
976 E != EEnd;
977 ++E)
978 E->second.Destroy();
979
Douglas Gregor86d9a522009-09-21 16:56:56 +0000980 ShadowMaps.pop_back();
981}
982
Douglas Gregor791215b2009-09-21 20:51:25 +0000983/// \brief Determines whether this given declaration will be found by
984/// ordinary name lookup.
985bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const {
Douglas Gregor4710e5b2010-05-28 00:49:12 +0000986 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
987
Douglas Gregor791215b2009-09-21 20:51:25 +0000988 unsigned IDNS = Decl::IDNS_Ordinary;
David Blaikie4e4d0842012-03-11 07:00:24 +0000989 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor9b30b262010-06-15 20:26:51 +0000990 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member;
David Blaikie4e4d0842012-03-11 07:00:24 +0000991 else if (SemaRef.getLangOpts().ObjC1) {
Douglas Gregorca45da02010-11-02 20:36:02 +0000992 if (isa<ObjCIvarDecl>(ND))
993 return true;
Douglas Gregorca45da02010-11-02 20:36:02 +0000994 }
995
Douglas Gregor791215b2009-09-21 20:51:25 +0000996 return ND->getIdentifierNamespace() & IDNS;
997}
998
Douglas Gregor01dfea02010-01-10 23:08:15 +0000999/// \brief Determines whether this given declaration will be found by
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001000/// ordinary name lookup but is not a type name.
1001bool ResultBuilder::IsOrdinaryNonTypeName(NamedDecl *ND) const {
1002 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
1003 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
1004 return false;
1005
1006 unsigned IDNS = Decl::IDNS_Ordinary;
David Blaikie4e4d0842012-03-11 07:00:24 +00001007 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor9b30b262010-06-15 20:26:51 +00001008 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member;
David Blaikie4e4d0842012-03-11 07:00:24 +00001009 else if (SemaRef.getLangOpts().ObjC1) {
Douglas Gregorca45da02010-11-02 20:36:02 +00001010 if (isa<ObjCIvarDecl>(ND))
1011 return true;
Douglas Gregorca45da02010-11-02 20:36:02 +00001012 }
1013
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001014 return ND->getIdentifierNamespace() & IDNS;
1015}
1016
Douglas Gregorf9578432010-07-28 21:50:18 +00001017bool ResultBuilder::IsIntegralConstantValue(NamedDecl *ND) const {
1018 if (!IsOrdinaryNonTypeName(ND))
1019 return 0;
1020
1021 if (ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl()))
1022 if (VD->getType()->isIntegralOrEnumerationType())
1023 return true;
1024
1025 return false;
1026}
1027
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001028/// \brief Determines whether this given declaration will be found by
Douglas Gregor01dfea02010-01-10 23:08:15 +00001029/// ordinary name lookup.
1030bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const {
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001031 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
1032
Douglas Gregor01dfea02010-01-10 23:08:15 +00001033 unsigned IDNS = Decl::IDNS_Ordinary;
David Blaikie4e4d0842012-03-11 07:00:24 +00001034 if (SemaRef.getLangOpts().CPlusPlus)
John McCall0d6b1642010-04-23 18:46:30 +00001035 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001036
1037 return (ND->getIdentifierNamespace() & IDNS) &&
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001038 !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) &&
1039 !isa<ObjCPropertyDecl>(ND);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001040}
1041
Douglas Gregor86d9a522009-09-21 16:56:56 +00001042/// \brief Determines whether the given declaration is suitable as the
1043/// start of a C++ nested-name-specifier, e.g., a class or namespace.
1044bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const {
1045 // Allow us to find class templates, too.
1046 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
1047 ND = ClassTemplate->getTemplatedDecl();
1048
1049 return SemaRef.isAcceptableNestedNameSpecifier(ND);
1050}
1051
1052/// \brief Determines whether the given declaration is an enumeration.
1053bool ResultBuilder::IsEnum(NamedDecl *ND) const {
1054 return isa<EnumDecl>(ND);
1055}
1056
1057/// \brief Determines whether the given declaration is a class or struct.
1058bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const {
1059 // Allow us to find class templates, too.
1060 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
1061 ND = ClassTemplate->getTemplatedDecl();
1062
1063 if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001064 return RD->getTagKind() == TTK_Class ||
1065 RD->getTagKind() == TTK_Struct;
Douglas Gregor86d9a522009-09-21 16:56:56 +00001066
1067 return false;
1068}
1069
1070/// \brief Determines whether the given declaration is a union.
1071bool ResultBuilder::IsUnion(NamedDecl *ND) const {
1072 // Allow us to find class templates, too.
1073 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
1074 ND = ClassTemplate->getTemplatedDecl();
1075
1076 if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001077 return RD->getTagKind() == TTK_Union;
Douglas Gregor86d9a522009-09-21 16:56:56 +00001078
1079 return false;
1080}
1081
1082/// \brief Determines whether the given declaration is a namespace.
1083bool ResultBuilder::IsNamespace(NamedDecl *ND) const {
1084 return isa<NamespaceDecl>(ND);
1085}
1086
1087/// \brief Determines whether the given declaration is a namespace or
1088/// namespace alias.
1089bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const {
1090 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
1091}
1092
Douglas Gregor76282942009-12-11 17:31:05 +00001093/// \brief Determines whether the given declaration is a type.
Douglas Gregor86d9a522009-09-21 16:56:56 +00001094bool ResultBuilder::IsType(NamedDecl *ND) const {
Douglas Gregord32b0222010-08-24 01:06:58 +00001095 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND))
1096 ND = Using->getTargetDecl();
1097
1098 return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND);
Douglas Gregor86d9a522009-09-21 16:56:56 +00001099}
1100
Douglas Gregor76282942009-12-11 17:31:05 +00001101/// \brief Determines which members of a class should be visible via
1102/// "." or "->". Only value declarations, nested name specifiers, and
1103/// using declarations thereof should show up.
Douglas Gregoreb5758b2009-09-23 22:26:46 +00001104bool ResultBuilder::IsMember(NamedDecl *ND) const {
Douglas Gregor76282942009-12-11 17:31:05 +00001105 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND))
1106 ND = Using->getTargetDecl();
1107
Douglas Gregorce821962009-12-11 18:14:22 +00001108 return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
1109 isa<ObjCPropertyDecl>(ND);
Douglas Gregoreb5758b2009-09-23 22:26:46 +00001110}
1111
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001112static bool isObjCReceiverType(ASTContext &C, QualType T) {
1113 T = C.getCanonicalType(T);
1114 switch (T->getTypeClass()) {
1115 case Type::ObjCObject:
1116 case Type::ObjCInterface:
1117 case Type::ObjCObjectPointer:
1118 return true;
1119
1120 case Type::Builtin:
1121 switch (cast<BuiltinType>(T)->getKind()) {
1122 case BuiltinType::ObjCId:
1123 case BuiltinType::ObjCClass:
1124 case BuiltinType::ObjCSel:
1125 return true;
1126
1127 default:
1128 break;
1129 }
1130 return false;
1131
1132 default:
1133 break;
1134 }
1135
David Blaikie4e4d0842012-03-11 07:00:24 +00001136 if (!C.getLangOpts().CPlusPlus)
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001137 return false;
1138
1139 // FIXME: We could perform more analysis here to determine whether a
1140 // particular class type has any conversions to Objective-C types. For now,
1141 // just accept all class types.
1142 return T->isDependentType() || T->isRecordType();
1143}
1144
1145bool ResultBuilder::IsObjCMessageReceiver(NamedDecl *ND) const {
1146 QualType T = getDeclUsageType(SemaRef.Context, ND);
1147 if (T.isNull())
1148 return false;
1149
1150 T = SemaRef.Context.getBaseElementType(T);
1151 return isObjCReceiverType(SemaRef.Context, T);
1152}
1153
Douglas Gregor81f3bff2012-02-15 15:34:24 +00001154bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(NamedDecl *ND) const {
1155 if (IsObjCMessageReceiver(ND))
1156 return true;
1157
1158 VarDecl *Var = dyn_cast<VarDecl>(ND);
1159 if (!Var)
1160 return false;
1161
1162 return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>();
1163}
1164
Douglas Gregorfb629412010-08-23 21:17:50 +00001165bool ResultBuilder::IsObjCCollection(NamedDecl *ND) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001166 if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) ||
1167 (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND)))
Douglas Gregorfb629412010-08-23 21:17:50 +00001168 return false;
1169
1170 QualType T = getDeclUsageType(SemaRef.Context, ND);
1171 if (T.isNull())
1172 return false;
1173
1174 T = SemaRef.Context.getBaseElementType(T);
1175 return T->isObjCObjectType() || T->isObjCObjectPointerType() ||
1176 T->isObjCIdType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001177 (SemaRef.getLangOpts().CPlusPlus && T->isRecordType());
Douglas Gregorfb629412010-08-23 21:17:50 +00001178}
Douglas Gregor8e254cf2010-05-27 23:06:34 +00001179
Douglas Gregor52779fb2010-09-23 23:01:17 +00001180bool ResultBuilder::IsImpossibleToSatisfy(NamedDecl *ND) const {
1181 return false;
1182}
1183
Douglas Gregor80f4f4c2010-01-14 16:08:12 +00001184/// \rief Determines whether the given declaration is an Objective-C
1185/// instance variable.
1186bool ResultBuilder::IsObjCIvar(NamedDecl *ND) const {
1187 return isa<ObjCIvarDecl>(ND);
1188}
1189
Douglas Gregor1ca6ae82010-01-14 01:09:38 +00001190namespace {
1191 /// \brief Visible declaration consumer that adds a code-completion result
1192 /// for each visible declaration.
1193 class CodeCompletionDeclConsumer : public VisibleDeclConsumer {
1194 ResultBuilder &Results;
1195 DeclContext *CurContext;
1196
1197 public:
1198 CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext)
1199 : Results(Results), CurContext(CurContext) { }
1200
Erik Verbruggend1205962011-10-06 07:27:49 +00001201 virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
1202 bool InBaseClass) {
1203 bool Accessible = true;
Douglas Gregor17015ef2011-11-03 16:51:37 +00001204 if (Ctx)
1205 Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx);
1206
Erik Verbruggend1205962011-10-06 07:27:49 +00001207 ResultBuilder::Result Result(ND, 0, false, Accessible);
1208 Results.AddResult(Result, CurContext, Hiding, InBaseClass);
Douglas Gregor1ca6ae82010-01-14 01:09:38 +00001209 }
1210 };
1211}
1212
Douglas Gregor86d9a522009-09-21 16:56:56 +00001213/// \brief Add type specifiers for the current language as keyword results.
Douglas Gregorbca403c2010-01-13 23:51:12 +00001214static void AddTypeSpecifierResults(const LangOptions &LangOpts,
Douglas Gregor86d9a522009-09-21 16:56:56 +00001215 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00001216 typedef CodeCompletionResult Result;
Douglas Gregor12e13132010-05-26 22:00:08 +00001217 Results.AddResult(Result("short", CCP_Type));
1218 Results.AddResult(Result("long", CCP_Type));
1219 Results.AddResult(Result("signed", CCP_Type));
1220 Results.AddResult(Result("unsigned", CCP_Type));
1221 Results.AddResult(Result("void", CCP_Type));
1222 Results.AddResult(Result("char", CCP_Type));
1223 Results.AddResult(Result("int", CCP_Type));
1224 Results.AddResult(Result("float", CCP_Type));
1225 Results.AddResult(Result("double", CCP_Type));
1226 Results.AddResult(Result("enum", CCP_Type));
1227 Results.AddResult(Result("struct", CCP_Type));
1228 Results.AddResult(Result("union", CCP_Type));
1229 Results.AddResult(Result("const", CCP_Type));
1230 Results.AddResult(Result("volatile", CCP_Type));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001231
Douglas Gregor86d9a522009-09-21 16:56:56 +00001232 if (LangOpts.C99) {
1233 // C99-specific
Douglas Gregor12e13132010-05-26 22:00:08 +00001234 Results.AddResult(Result("_Complex", CCP_Type));
1235 Results.AddResult(Result("_Imaginary", CCP_Type));
1236 Results.AddResult(Result("_Bool", CCP_Type));
1237 Results.AddResult(Result("restrict", CCP_Type));
Douglas Gregor86d9a522009-09-21 16:56:56 +00001238 }
1239
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001240 CodeCompletionBuilder Builder(Results.getAllocator(),
1241 Results.getCodeCompletionTUInfo());
Douglas Gregor86d9a522009-09-21 16:56:56 +00001242 if (LangOpts.CPlusPlus) {
1243 // C++-specific
Douglas Gregorb05496d2010-09-20 21:11:48 +00001244 Results.AddResult(Result("bool", CCP_Type +
1245 (LangOpts.ObjC1? CCD_bool_in_ObjC : 0)));
Douglas Gregor12e13132010-05-26 22:00:08 +00001246 Results.AddResult(Result("class", CCP_Type));
1247 Results.AddResult(Result("wchar_t", CCP_Type));
Douglas Gregor86d9a522009-09-21 16:56:56 +00001248
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001249 // typename qualified-id
Douglas Gregor218937c2011-02-01 19:23:04 +00001250 Builder.AddTypedTextChunk("typename");
1251 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1252 Builder.AddPlaceholderChunk("qualifier");
1253 Builder.AddTextChunk("::");
1254 Builder.AddPlaceholderChunk("name");
1255 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregord8e8a582010-05-25 21:41:55 +00001256
Douglas Gregor86d9a522009-09-21 16:56:56 +00001257 if (LangOpts.CPlusPlus0x) {
Douglas Gregor12e13132010-05-26 22:00:08 +00001258 Results.AddResult(Result("auto", CCP_Type));
1259 Results.AddResult(Result("char16_t", CCP_Type));
1260 Results.AddResult(Result("char32_t", CCP_Type));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001261
Douglas Gregor218937c2011-02-01 19:23:04 +00001262 Builder.AddTypedTextChunk("decltype");
1263 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1264 Builder.AddPlaceholderChunk("expression");
1265 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1266 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor86d9a522009-09-21 16:56:56 +00001267 }
1268 }
1269
1270 // GNU extensions
1271 if (LangOpts.GNUMode) {
1272 // FIXME: Enable when we actually support decimal floating point.
Douglas Gregora4477812010-01-14 16:01:26 +00001273 // Results.AddResult(Result("_Decimal32"));
1274 // Results.AddResult(Result("_Decimal64"));
1275 // Results.AddResult(Result("_Decimal128"));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001276
Douglas Gregor218937c2011-02-01 19:23:04 +00001277 Builder.AddTypedTextChunk("typeof");
1278 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1279 Builder.AddPlaceholderChunk("expression");
1280 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001281
Douglas Gregor218937c2011-02-01 19:23:04 +00001282 Builder.AddTypedTextChunk("typeof");
1283 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1284 Builder.AddPlaceholderChunk("type");
1285 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1286 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor86d9a522009-09-21 16:56:56 +00001287 }
1288}
1289
John McCallf312b1e2010-08-26 23:41:50 +00001290static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001291 const LangOptions &LangOpts,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001292 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00001293 typedef CodeCompletionResult Result;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001294 // Note: we don't suggest either "auto" or "register", because both
1295 // are pointless as storage specifiers. Elsewhere, we suggest "auto"
1296 // in C++0x as a type specifier.
Douglas Gregora4477812010-01-14 16:01:26 +00001297 Results.AddResult(Result("extern"));
1298 Results.AddResult(Result("static"));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001299}
1300
John McCallf312b1e2010-08-26 23:41:50 +00001301static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001302 const LangOptions &LangOpts,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001303 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00001304 typedef CodeCompletionResult Result;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001305 switch (CCC) {
John McCallf312b1e2010-08-26 23:41:50 +00001306 case Sema::PCC_Class:
1307 case Sema::PCC_MemberTemplate:
Douglas Gregor01dfea02010-01-10 23:08:15 +00001308 if (LangOpts.CPlusPlus) {
Douglas Gregora4477812010-01-14 16:01:26 +00001309 Results.AddResult(Result("explicit"));
1310 Results.AddResult(Result("friend"));
1311 Results.AddResult(Result("mutable"));
1312 Results.AddResult(Result("virtual"));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001313 }
1314 // Fall through
1315
John McCallf312b1e2010-08-26 23:41:50 +00001316 case Sema::PCC_ObjCInterface:
1317 case Sema::PCC_ObjCImplementation:
1318 case Sema::PCC_Namespace:
1319 case Sema::PCC_Template:
Douglas Gregor01dfea02010-01-10 23:08:15 +00001320 if (LangOpts.CPlusPlus || LangOpts.C99)
Douglas Gregora4477812010-01-14 16:01:26 +00001321 Results.AddResult(Result("inline"));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001322 break;
1323
John McCallf312b1e2010-08-26 23:41:50 +00001324 case Sema::PCC_ObjCInstanceVariableList:
1325 case Sema::PCC_Expression:
1326 case Sema::PCC_Statement:
1327 case Sema::PCC_ForInit:
1328 case Sema::PCC_Condition:
1329 case Sema::PCC_RecoveryInFunction:
1330 case Sema::PCC_Type:
Douglas Gregor02688102010-09-14 23:59:36 +00001331 case Sema::PCC_ParenthesizedExpression:
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001332 case Sema::PCC_LocalDeclarationSpecifiers:
Douglas Gregor01dfea02010-01-10 23:08:15 +00001333 break;
1334 }
1335}
1336
Douglas Gregorbca403c2010-01-13 23:51:12 +00001337static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt);
1338static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt);
1339static void AddObjCVisibilityResults(const LangOptions &LangOpts,
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001340 ResultBuilder &Results,
1341 bool NeedAt);
Douglas Gregorbca403c2010-01-13 23:51:12 +00001342static void AddObjCImplementationResults(const LangOptions &LangOpts,
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001343 ResultBuilder &Results,
1344 bool NeedAt);
Douglas Gregorbca403c2010-01-13 23:51:12 +00001345static void AddObjCInterfaceResults(const LangOptions &LangOpts,
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001346 ResultBuilder &Results,
1347 bool NeedAt);
Douglas Gregorbca403c2010-01-13 23:51:12 +00001348static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001349
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001350static void AddTypedefResult(ResultBuilder &Results) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001351 CodeCompletionBuilder Builder(Results.getAllocator(),
1352 Results.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00001353 Builder.AddTypedTextChunk("typedef");
1354 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1355 Builder.AddPlaceholderChunk("type");
1356 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1357 Builder.AddPlaceholderChunk("name");
1358 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001359}
1360
John McCallf312b1e2010-08-26 23:41:50 +00001361static bool WantTypesInContext(Sema::ParserCompletionContext CCC,
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001362 const LangOptions &LangOpts) {
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001363 switch (CCC) {
John McCallf312b1e2010-08-26 23:41:50 +00001364 case Sema::PCC_Namespace:
1365 case Sema::PCC_Class:
1366 case Sema::PCC_ObjCInstanceVariableList:
1367 case Sema::PCC_Template:
1368 case Sema::PCC_MemberTemplate:
1369 case Sema::PCC_Statement:
1370 case Sema::PCC_RecoveryInFunction:
1371 case Sema::PCC_Type:
Douglas Gregor02688102010-09-14 23:59:36 +00001372 case Sema::PCC_ParenthesizedExpression:
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001373 case Sema::PCC_LocalDeclarationSpecifiers:
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001374 return true;
1375
John McCallf312b1e2010-08-26 23:41:50 +00001376 case Sema::PCC_Expression:
1377 case Sema::PCC_Condition:
Douglas Gregor02688102010-09-14 23:59:36 +00001378 return LangOpts.CPlusPlus;
1379
1380 case Sema::PCC_ObjCInterface:
1381 case Sema::PCC_ObjCImplementation:
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001382 return false;
1383
John McCallf312b1e2010-08-26 23:41:50 +00001384 case Sema::PCC_ForInit:
Douglas Gregor02688102010-09-14 23:59:36 +00001385 return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99;
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001386 }
David Blaikie7530c032012-01-17 06:56:22 +00001387
1388 llvm_unreachable("Invalid ParserCompletionContext!");
Douglas Gregor4710e5b2010-05-28 00:49:12 +00001389}
1390
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00001391static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context,
1392 const Preprocessor &PP) {
1393 PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP);
Douglas Gregor8ca72082011-10-18 21:20:17 +00001394 Policy.AnonymousTagLocations = false;
1395 Policy.SuppressStrongLifetime = true;
Douglas Gregor25270b62011-11-03 00:16:13 +00001396 Policy.SuppressUnwrittenScope = true;
Douglas Gregor8ca72082011-10-18 21:20:17 +00001397 return Policy;
1398}
1399
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00001400/// \brief Retrieve a printing policy suitable for code completion.
1401static PrintingPolicy getCompletionPrintingPolicy(Sema &S) {
1402 return getCompletionPrintingPolicy(S.Context, S.PP);
1403}
1404
Douglas Gregor8ca72082011-10-18 21:20:17 +00001405/// \brief Retrieve the string representation of the given type as a string
1406/// that has the appropriate lifetime for code completion.
1407///
1408/// This routine provides a fast path where we provide constant strings for
1409/// common type names.
1410static const char *GetCompletionTypeString(QualType T,
1411 ASTContext &Context,
1412 const PrintingPolicy &Policy,
1413 CodeCompletionAllocator &Allocator) {
1414 if (!T.getLocalQualifiers()) {
1415 // Built-in type names are constant strings.
1416 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T))
Argyrios Kyrtzidis27a00972012-05-05 04:20:28 +00001417 return BT->getNameAsCString(Policy);
Douglas Gregor8ca72082011-10-18 21:20:17 +00001418
1419 // Anonymous tag types are constant strings.
1420 if (const TagType *TagT = dyn_cast<TagType>(T))
1421 if (TagDecl *Tag = TagT->getDecl())
1422 if (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl()) {
1423 switch (Tag->getTagKind()) {
1424 case TTK_Struct: return "struct <anonymous>";
1425 case TTK_Class: return "class <anonymous>";
1426 case TTK_Union: return "union <anonymous>";
1427 case TTK_Enum: return "enum <anonymous>";
1428 }
1429 }
1430 }
1431
1432 // Slow path: format the type as a string.
1433 std::string Result;
1434 T.getAsStringInternal(Result, Policy);
1435 return Allocator.CopyString(Result);
1436}
1437
Douglas Gregor81f3bff2012-02-15 15:34:24 +00001438/// \brief Add a completion for "this", if we're in a member function.
1439static void addThisCompletion(Sema &S, ResultBuilder &Results) {
1440 QualType ThisTy = S.getCurrentThisType();
1441 if (ThisTy.isNull())
1442 return;
1443
1444 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001445 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregor81f3bff2012-02-15 15:34:24 +00001446 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
1447 Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy,
1448 S.Context,
1449 Policy,
1450 Allocator));
1451 Builder.AddTypedTextChunk("this");
1452 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
1453}
1454
Douglas Gregor01dfea02010-01-10 23:08:15 +00001455/// \brief Add language constructs that show up for "ordinary" names.
John McCallf312b1e2010-08-26 23:41:50 +00001456static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001457 Scope *S,
1458 Sema &SemaRef,
Douglas Gregor01dfea02010-01-10 23:08:15 +00001459 ResultBuilder &Results) {
Douglas Gregor8ca72082011-10-18 21:20:17 +00001460 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001461 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregor8ca72082011-10-18 21:20:17 +00001462 PrintingPolicy Policy = getCompletionPrintingPolicy(SemaRef);
Douglas Gregor218937c2011-02-01 19:23:04 +00001463
John McCall0a2c5e22010-08-25 06:19:51 +00001464 typedef CodeCompletionResult Result;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001465 switch (CCC) {
John McCallf312b1e2010-08-26 23:41:50 +00001466 case Sema::PCC_Namespace:
David Blaikie4e4d0842012-03-11 07:00:24 +00001467 if (SemaRef.getLangOpts().CPlusPlus) {
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001468 if (Results.includeCodePatterns()) {
1469 // namespace <identifier> { declarations }
Douglas Gregor218937c2011-02-01 19:23:04 +00001470 Builder.AddTypedTextChunk("namespace");
1471 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1472 Builder.AddPlaceholderChunk("identifier");
1473 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1474 Builder.AddPlaceholderChunk("declarations");
1475 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1476 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1477 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001478 }
1479
Douglas Gregor01dfea02010-01-10 23:08:15 +00001480 // namespace identifier = identifier ;
Douglas Gregor218937c2011-02-01 19:23:04 +00001481 Builder.AddTypedTextChunk("namespace");
1482 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1483 Builder.AddPlaceholderChunk("name");
1484 Builder.AddChunk(CodeCompletionString::CK_Equal);
1485 Builder.AddPlaceholderChunk("namespace");
1486 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001487
1488 // Using directives
Douglas Gregor218937c2011-02-01 19:23:04 +00001489 Builder.AddTypedTextChunk("using");
1490 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1491 Builder.AddTextChunk("namespace");
1492 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1493 Builder.AddPlaceholderChunk("identifier");
1494 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001495
1496 // asm(string-literal)
Douglas Gregor218937c2011-02-01 19:23:04 +00001497 Builder.AddTypedTextChunk("asm");
1498 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1499 Builder.AddPlaceholderChunk("string-literal");
1500 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1501 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001502
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001503 if (Results.includeCodePatterns()) {
1504 // Explicit template instantiation
Douglas Gregor218937c2011-02-01 19:23:04 +00001505 Builder.AddTypedTextChunk("template");
1506 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1507 Builder.AddPlaceholderChunk("declaration");
1508 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001509 }
Douglas Gregor01dfea02010-01-10 23:08:15 +00001510 }
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001511
David Blaikie4e4d0842012-03-11 07:00:24 +00001512 if (SemaRef.getLangOpts().ObjC1)
Douglas Gregorbca403c2010-01-13 23:51:12 +00001513 AddObjCTopLevelResults(Results, true);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001514
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001515 AddTypedefResult(Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001516 // Fall through
1517
John McCallf312b1e2010-08-26 23:41:50 +00001518 case Sema::PCC_Class:
David Blaikie4e4d0842012-03-11 07:00:24 +00001519 if (SemaRef.getLangOpts().CPlusPlus) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001520 // Using declaration
Douglas Gregor218937c2011-02-01 19:23:04 +00001521 Builder.AddTypedTextChunk("using");
1522 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1523 Builder.AddPlaceholderChunk("qualifier");
1524 Builder.AddTextChunk("::");
1525 Builder.AddPlaceholderChunk("name");
1526 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001527
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001528 // using typename qualifier::name (only in a dependent context)
Douglas Gregor01dfea02010-01-10 23:08:15 +00001529 if (SemaRef.CurContext->isDependentContext()) {
Douglas Gregor218937c2011-02-01 19:23:04 +00001530 Builder.AddTypedTextChunk("using");
1531 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1532 Builder.AddTextChunk("typename");
1533 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1534 Builder.AddPlaceholderChunk("qualifier");
1535 Builder.AddTextChunk("::");
1536 Builder.AddPlaceholderChunk("name");
1537 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001538 }
1539
John McCallf312b1e2010-08-26 23:41:50 +00001540 if (CCC == Sema::PCC_Class) {
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001541 AddTypedefResult(Results);
1542
Douglas Gregor01dfea02010-01-10 23:08:15 +00001543 // public:
Douglas Gregor218937c2011-02-01 19:23:04 +00001544 Builder.AddTypedTextChunk("public");
Douglas Gregor10ccf122012-04-10 17:56:28 +00001545 if (Results.includeCodePatterns())
1546 Builder.AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor218937c2011-02-01 19:23:04 +00001547 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001548
1549 // protected:
Douglas Gregor218937c2011-02-01 19:23:04 +00001550 Builder.AddTypedTextChunk("protected");
Douglas Gregor10ccf122012-04-10 17:56:28 +00001551 if (Results.includeCodePatterns())
1552 Builder.AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor218937c2011-02-01 19:23:04 +00001553 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001554
1555 // private:
Douglas Gregor218937c2011-02-01 19:23:04 +00001556 Builder.AddTypedTextChunk("private");
Douglas Gregor10ccf122012-04-10 17:56:28 +00001557 if (Results.includeCodePatterns())
1558 Builder.AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor218937c2011-02-01 19:23:04 +00001559 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001560 }
1561 }
1562 // Fall through
1563
John McCallf312b1e2010-08-26 23:41:50 +00001564 case Sema::PCC_Template:
1565 case Sema::PCC_MemberTemplate:
David Blaikie4e4d0842012-03-11 07:00:24 +00001566 if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001567 // template < parameters >
Douglas Gregor218937c2011-02-01 19:23:04 +00001568 Builder.AddTypedTextChunk("template");
1569 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1570 Builder.AddPlaceholderChunk("parameters");
1571 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1572 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001573 }
1574
David Blaikie4e4d0842012-03-11 07:00:24 +00001575 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1576 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001577 break;
1578
John McCallf312b1e2010-08-26 23:41:50 +00001579 case Sema::PCC_ObjCInterface:
David Blaikie4e4d0842012-03-11 07:00:24 +00001580 AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true);
1581 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1582 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001583 break;
1584
John McCallf312b1e2010-08-26 23:41:50 +00001585 case Sema::PCC_ObjCImplementation:
David Blaikie4e4d0842012-03-11 07:00:24 +00001586 AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true);
1587 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1588 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001589 break;
1590
John McCallf312b1e2010-08-26 23:41:50 +00001591 case Sema::PCC_ObjCInstanceVariableList:
David Blaikie4e4d0842012-03-11 07:00:24 +00001592 AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true);
Douglas Gregorc38c3e12010-01-13 21:54:15 +00001593 break;
1594
John McCallf312b1e2010-08-26 23:41:50 +00001595 case Sema::PCC_RecoveryInFunction:
1596 case Sema::PCC_Statement: {
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001597 AddTypedefResult(Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001598
David Blaikie4e4d0842012-03-11 07:00:24 +00001599 if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() &&
1600 SemaRef.getLangOpts().CXXExceptions) {
Douglas Gregor218937c2011-02-01 19:23:04 +00001601 Builder.AddTypedTextChunk("try");
1602 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1603 Builder.AddPlaceholderChunk("statements");
1604 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1605 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1606 Builder.AddTextChunk("catch");
1607 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1608 Builder.AddPlaceholderChunk("declaration");
1609 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1610 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1611 Builder.AddPlaceholderChunk("statements");
1612 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1613 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1614 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001615 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001616 if (SemaRef.getLangOpts().ObjC1)
Douglas Gregorbca403c2010-01-13 23:51:12 +00001617 AddObjCStatementResults(Results, true);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00001618
Douglas Gregord8e8a582010-05-25 21:41:55 +00001619 if (Results.includeCodePatterns()) {
1620 // if (condition) { statements }
Douglas Gregor218937c2011-02-01 19:23:04 +00001621 Builder.AddTypedTextChunk("if");
1622 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikie4e4d0842012-03-11 07:00:24 +00001623 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor218937c2011-02-01 19:23:04 +00001624 Builder.AddPlaceholderChunk("condition");
Douglas Gregord8e8a582010-05-25 21:41:55 +00001625 else
Douglas Gregor218937c2011-02-01 19:23:04 +00001626 Builder.AddPlaceholderChunk("expression");
1627 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1628 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1629 Builder.AddPlaceholderChunk("statements");
1630 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1631 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1632 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001633
Douglas Gregord8e8a582010-05-25 21:41:55 +00001634 // switch (condition) { }
Douglas Gregor218937c2011-02-01 19:23:04 +00001635 Builder.AddTypedTextChunk("switch");
1636 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikie4e4d0842012-03-11 07:00:24 +00001637 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor218937c2011-02-01 19:23:04 +00001638 Builder.AddPlaceholderChunk("condition");
Douglas Gregord8e8a582010-05-25 21:41:55 +00001639 else
Douglas Gregor218937c2011-02-01 19:23:04 +00001640 Builder.AddPlaceholderChunk("expression");
1641 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1642 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1643 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1644 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1645 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregord8e8a582010-05-25 21:41:55 +00001646 }
1647
Douglas Gregor01dfea02010-01-10 23:08:15 +00001648 // Switch-specific statements.
John McCall781472f2010-08-25 08:40:02 +00001649 if (!SemaRef.getCurFunction()->SwitchStack.empty()) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001650 // case expression:
Douglas Gregor218937c2011-02-01 19:23:04 +00001651 Builder.AddTypedTextChunk("case");
1652 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1653 Builder.AddPlaceholderChunk("expression");
1654 Builder.AddChunk(CodeCompletionString::CK_Colon);
1655 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001656
1657 // default:
Douglas Gregor218937c2011-02-01 19:23:04 +00001658 Builder.AddTypedTextChunk("default");
1659 Builder.AddChunk(CodeCompletionString::CK_Colon);
1660 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001661 }
1662
Douglas Gregord8e8a582010-05-25 21:41:55 +00001663 if (Results.includeCodePatterns()) {
1664 /// while (condition) { statements }
Douglas Gregor218937c2011-02-01 19:23:04 +00001665 Builder.AddTypedTextChunk("while");
1666 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikie4e4d0842012-03-11 07:00:24 +00001667 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor218937c2011-02-01 19:23:04 +00001668 Builder.AddPlaceholderChunk("condition");
Douglas Gregord8e8a582010-05-25 21:41:55 +00001669 else
Douglas Gregor218937c2011-02-01 19:23:04 +00001670 Builder.AddPlaceholderChunk("expression");
1671 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1672 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1673 Builder.AddPlaceholderChunk("statements");
1674 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1675 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1676 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregord8e8a582010-05-25 21:41:55 +00001677
1678 // do { statements } while ( expression );
Douglas Gregor218937c2011-02-01 19:23:04 +00001679 Builder.AddTypedTextChunk("do");
1680 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1681 Builder.AddPlaceholderChunk("statements");
1682 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1683 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1684 Builder.AddTextChunk("while");
1685 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1686 Builder.AddPlaceholderChunk("expression");
1687 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1688 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001689
Douglas Gregord8e8a582010-05-25 21:41:55 +00001690 // for ( for-init-statement ; condition ; expression ) { statements }
Douglas Gregor218937c2011-02-01 19:23:04 +00001691 Builder.AddTypedTextChunk("for");
1692 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikie4e4d0842012-03-11 07:00:24 +00001693 if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99)
Douglas Gregor218937c2011-02-01 19:23:04 +00001694 Builder.AddPlaceholderChunk("init-statement");
Douglas Gregord8e8a582010-05-25 21:41:55 +00001695 else
Douglas Gregor218937c2011-02-01 19:23:04 +00001696 Builder.AddPlaceholderChunk("init-expression");
1697 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
1698 Builder.AddPlaceholderChunk("condition");
1699 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
1700 Builder.AddPlaceholderChunk("inc-expression");
1701 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1702 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1703 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1704 Builder.AddPlaceholderChunk("statements");
1705 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1706 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1707 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregord8e8a582010-05-25 21:41:55 +00001708 }
Douglas Gregor01dfea02010-01-10 23:08:15 +00001709
1710 if (S->getContinueParent()) {
1711 // continue ;
Douglas Gregor218937c2011-02-01 19:23:04 +00001712 Builder.AddTypedTextChunk("continue");
1713 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001714 }
1715
1716 if (S->getBreakParent()) {
1717 // break ;
Douglas Gregor218937c2011-02-01 19:23:04 +00001718 Builder.AddTypedTextChunk("break");
1719 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001720 }
1721
1722 // "return expression ;" or "return ;", depending on whether we
1723 // know the function is void or not.
1724 bool isVoid = false;
1725 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext))
1726 isVoid = Function->getResultType()->isVoidType();
1727 else if (ObjCMethodDecl *Method
1728 = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext))
1729 isVoid = Method->getResultType()->isVoidType();
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +00001730 else if (SemaRef.getCurBlock() &&
1731 !SemaRef.getCurBlock()->ReturnType.isNull())
1732 isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType();
Douglas Gregor218937c2011-02-01 19:23:04 +00001733 Builder.AddTypedTextChunk("return");
Douglas Gregor93298002010-02-18 04:06:48 +00001734 if (!isVoid) {
Douglas Gregor218937c2011-02-01 19:23:04 +00001735 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1736 Builder.AddPlaceholderChunk("expression");
Douglas Gregor93298002010-02-18 04:06:48 +00001737 }
Douglas Gregor218937c2011-02-01 19:23:04 +00001738 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001739
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001740 // goto identifier ;
Douglas Gregor218937c2011-02-01 19:23:04 +00001741 Builder.AddTypedTextChunk("goto");
1742 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1743 Builder.AddPlaceholderChunk("label");
1744 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001745
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001746 // Using directives
Douglas Gregor218937c2011-02-01 19:23:04 +00001747 Builder.AddTypedTextChunk("using");
1748 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1749 Builder.AddTextChunk("namespace");
1750 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1751 Builder.AddPlaceholderChunk("identifier");
1752 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001753 }
1754
1755 // Fall through (for statement expressions).
John McCallf312b1e2010-08-26 23:41:50 +00001756 case Sema::PCC_ForInit:
1757 case Sema::PCC_Condition:
David Blaikie4e4d0842012-03-11 07:00:24 +00001758 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001759 // Fall through: conditions and statements can have expressions.
1760
Douglas Gregor02688102010-09-14 23:59:36 +00001761 case Sema::PCC_ParenthesizedExpression:
David Blaikie4e4d0842012-03-11 07:00:24 +00001762 if (SemaRef.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001763 CCC == Sema::PCC_ParenthesizedExpression) {
1764 // (__bridge <type>)<expression>
1765 Builder.AddTypedTextChunk("__bridge");
1766 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1767 Builder.AddPlaceholderChunk("type");
1768 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1769 Builder.AddPlaceholderChunk("expression");
1770 Results.AddResult(Result(Builder.TakeString()));
1771
1772 // (__bridge_transfer <Objective-C type>)<expression>
1773 Builder.AddTypedTextChunk("__bridge_transfer");
1774 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1775 Builder.AddPlaceholderChunk("Objective-C type");
1776 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1777 Builder.AddPlaceholderChunk("expression");
1778 Results.AddResult(Result(Builder.TakeString()));
1779
1780 // (__bridge_retained <CF type>)<expression>
1781 Builder.AddTypedTextChunk("__bridge_retained");
1782 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1783 Builder.AddPlaceholderChunk("CF type");
1784 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1785 Builder.AddPlaceholderChunk("expression");
1786 Results.AddResult(Result(Builder.TakeString()));
1787 }
1788 // Fall through
1789
John McCallf312b1e2010-08-26 23:41:50 +00001790 case Sema::PCC_Expression: {
David Blaikie4e4d0842012-03-11 07:00:24 +00001791 if (SemaRef.getLangOpts().CPlusPlus) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001792 // 'this', if we're in a non-static member function.
Douglas Gregor81f3bff2012-02-15 15:34:24 +00001793 addThisCompletion(SemaRef, Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001794
Douglas Gregor8ca72082011-10-18 21:20:17 +00001795 // true
1796 Builder.AddResultTypeChunk("bool");
1797 Builder.AddTypedTextChunk("true");
1798 Results.AddResult(Result(Builder.TakeString()));
1799
1800 // false
1801 Builder.AddResultTypeChunk("bool");
1802 Builder.AddTypedTextChunk("false");
1803 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001804
David Blaikie4e4d0842012-03-11 07:00:24 +00001805 if (SemaRef.getLangOpts().RTTI) {
Douglas Gregorec3310a2011-04-12 02:47:21 +00001806 // dynamic_cast < type-id > ( expression )
1807 Builder.AddTypedTextChunk("dynamic_cast");
1808 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1809 Builder.AddPlaceholderChunk("type");
1810 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1811 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1812 Builder.AddPlaceholderChunk("expression");
1813 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1814 Results.AddResult(Result(Builder.TakeString()));
1815 }
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001816
1817 // static_cast < type-id > ( expression )
Douglas Gregor218937c2011-02-01 19:23:04 +00001818 Builder.AddTypedTextChunk("static_cast");
1819 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1820 Builder.AddPlaceholderChunk("type");
1821 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1822 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1823 Builder.AddPlaceholderChunk("expression");
1824 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1825 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001826
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001827 // reinterpret_cast < type-id > ( expression )
Douglas Gregor218937c2011-02-01 19:23:04 +00001828 Builder.AddTypedTextChunk("reinterpret_cast");
1829 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1830 Builder.AddPlaceholderChunk("type");
1831 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1832 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1833 Builder.AddPlaceholderChunk("expression");
1834 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1835 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001836
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001837 // const_cast < type-id > ( expression )
Douglas Gregor218937c2011-02-01 19:23:04 +00001838 Builder.AddTypedTextChunk("const_cast");
1839 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1840 Builder.AddPlaceholderChunk("type");
1841 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1842 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1843 Builder.AddPlaceholderChunk("expression");
1844 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1845 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001846
David Blaikie4e4d0842012-03-11 07:00:24 +00001847 if (SemaRef.getLangOpts().RTTI) {
Douglas Gregorec3310a2011-04-12 02:47:21 +00001848 // typeid ( expression-or-type )
Douglas Gregor8ca72082011-10-18 21:20:17 +00001849 Builder.AddResultTypeChunk("std::type_info");
Douglas Gregorec3310a2011-04-12 02:47:21 +00001850 Builder.AddTypedTextChunk("typeid");
1851 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1852 Builder.AddPlaceholderChunk("expression-or-type");
1853 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1854 Results.AddResult(Result(Builder.TakeString()));
1855 }
1856
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001857 // new T ( ... )
Douglas Gregor218937c2011-02-01 19:23:04 +00001858 Builder.AddTypedTextChunk("new");
1859 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1860 Builder.AddPlaceholderChunk("type");
1861 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1862 Builder.AddPlaceholderChunk("expressions");
1863 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1864 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001865
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001866 // new T [ ] ( ... )
Douglas Gregor218937c2011-02-01 19:23:04 +00001867 Builder.AddTypedTextChunk("new");
1868 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1869 Builder.AddPlaceholderChunk("type");
1870 Builder.AddChunk(CodeCompletionString::CK_LeftBracket);
1871 Builder.AddPlaceholderChunk("size");
1872 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
1873 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1874 Builder.AddPlaceholderChunk("expressions");
1875 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1876 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001877
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001878 // delete expression
Douglas Gregor8ca72082011-10-18 21:20:17 +00001879 Builder.AddResultTypeChunk("void");
Douglas Gregor218937c2011-02-01 19:23:04 +00001880 Builder.AddTypedTextChunk("delete");
1881 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1882 Builder.AddPlaceholderChunk("expression");
1883 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001884
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001885 // delete [] expression
Douglas Gregor8ca72082011-10-18 21:20:17 +00001886 Builder.AddResultTypeChunk("void");
Douglas Gregor218937c2011-02-01 19:23:04 +00001887 Builder.AddTypedTextChunk("delete");
1888 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1889 Builder.AddChunk(CodeCompletionString::CK_LeftBracket);
1890 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
1891 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1892 Builder.AddPlaceholderChunk("expression");
1893 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001894
David Blaikie4e4d0842012-03-11 07:00:24 +00001895 if (SemaRef.getLangOpts().CXXExceptions) {
Douglas Gregorec3310a2011-04-12 02:47:21 +00001896 // throw expression
Douglas Gregor8ca72082011-10-18 21:20:17 +00001897 Builder.AddResultTypeChunk("void");
Douglas Gregorec3310a2011-04-12 02:47:21 +00001898 Builder.AddTypedTextChunk("throw");
1899 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1900 Builder.AddPlaceholderChunk("expression");
1901 Results.AddResult(Result(Builder.TakeString()));
1902 }
Douglas Gregora50216c2011-10-18 16:29:03 +00001903
Douglas Gregor12e13132010-05-26 22:00:08 +00001904 // FIXME: Rethrow?
Douglas Gregora50216c2011-10-18 16:29:03 +00001905
David Blaikie4e4d0842012-03-11 07:00:24 +00001906 if (SemaRef.getLangOpts().CPlusPlus0x) {
Douglas Gregora50216c2011-10-18 16:29:03 +00001907 // nullptr
Douglas Gregor8ca72082011-10-18 21:20:17 +00001908 Builder.AddResultTypeChunk("std::nullptr_t");
Douglas Gregora50216c2011-10-18 16:29:03 +00001909 Builder.AddTypedTextChunk("nullptr");
1910 Results.AddResult(Result(Builder.TakeString()));
1911
1912 // alignof
Douglas Gregor8ca72082011-10-18 21:20:17 +00001913 Builder.AddResultTypeChunk("size_t");
Douglas Gregora50216c2011-10-18 16:29:03 +00001914 Builder.AddTypedTextChunk("alignof");
1915 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1916 Builder.AddPlaceholderChunk("type");
1917 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1918 Results.AddResult(Result(Builder.TakeString()));
1919
1920 // noexcept
Douglas Gregor8ca72082011-10-18 21:20:17 +00001921 Builder.AddResultTypeChunk("bool");
Douglas Gregora50216c2011-10-18 16:29:03 +00001922 Builder.AddTypedTextChunk("noexcept");
1923 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1924 Builder.AddPlaceholderChunk("expression");
1925 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1926 Results.AddResult(Result(Builder.TakeString()));
1927
1928 // sizeof... expression
Douglas Gregor8ca72082011-10-18 21:20:17 +00001929 Builder.AddResultTypeChunk("size_t");
Douglas Gregora50216c2011-10-18 16:29:03 +00001930 Builder.AddTypedTextChunk("sizeof...");
1931 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1932 Builder.AddPlaceholderChunk("parameter-pack");
1933 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1934 Results.AddResult(Result(Builder.TakeString()));
1935 }
Douglas Gregor01dfea02010-01-10 23:08:15 +00001936 }
1937
David Blaikie4e4d0842012-03-11 07:00:24 +00001938 if (SemaRef.getLangOpts().ObjC1) {
Douglas Gregor01dfea02010-01-10 23:08:15 +00001939 // Add "super", if we're in an Objective-C class with a superclass.
Ted Kremenek681e2562010-05-31 21:43:10 +00001940 if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) {
1941 // The interface can be NULL.
1942 if (ObjCInterfaceDecl *ID = Method->getClassInterface())
Douglas Gregor8ca72082011-10-18 21:20:17 +00001943 if (ID->getSuperClass()) {
1944 std::string SuperType;
1945 SuperType = ID->getSuperClass()->getNameAsString();
1946 if (Method->isInstanceMethod())
1947 SuperType += " *";
1948
1949 Builder.AddResultTypeChunk(Allocator.CopyString(SuperType));
1950 Builder.AddTypedTextChunk("super");
1951 Results.AddResult(Result(Builder.TakeString()));
1952 }
Ted Kremenek681e2562010-05-31 21:43:10 +00001953 }
1954
Douglas Gregorbca403c2010-01-13 23:51:12 +00001955 AddObjCExpressionResults(Results, true);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001956 }
1957
Douglas Gregorc8bddde2010-05-28 00:22:41 +00001958 // sizeof expression
Douglas Gregor8ca72082011-10-18 21:20:17 +00001959 Builder.AddResultTypeChunk("size_t");
Douglas Gregor218937c2011-02-01 19:23:04 +00001960 Builder.AddTypedTextChunk("sizeof");
1961 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1962 Builder.AddPlaceholderChunk("expression-or-type");
1963 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1964 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001965 break;
1966 }
Douglas Gregord32b0222010-08-24 01:06:58 +00001967
John McCallf312b1e2010-08-26 23:41:50 +00001968 case Sema::PCC_Type:
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001969 case Sema::PCC_LocalDeclarationSpecifiers:
Douglas Gregord32b0222010-08-24 01:06:58 +00001970 break;
Douglas Gregor01dfea02010-01-10 23:08:15 +00001971 }
1972
David Blaikie4e4d0842012-03-11 07:00:24 +00001973 if (WantTypesInContext(CCC, SemaRef.getLangOpts()))
1974 AddTypeSpecifierResults(SemaRef.getLangOpts(), Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00001975
David Blaikie4e4d0842012-03-11 07:00:24 +00001976 if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type)
Douglas Gregora4477812010-01-14 16:01:26 +00001977 Results.AddResult(Result("operator"));
Douglas Gregor01dfea02010-01-10 23:08:15 +00001978}
1979
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001980/// \brief If the given declaration has an associated type, add it as a result
1981/// type chunk.
1982static void AddResultTypeChunk(ASTContext &Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00001983 const PrintingPolicy &Policy,
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001984 NamedDecl *ND,
Douglas Gregor218937c2011-02-01 19:23:04 +00001985 CodeCompletionBuilder &Result) {
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001986 if (!ND)
1987 return;
Douglas Gregor6f942b22010-09-21 16:06:22 +00001988
1989 // Skip constructors and conversion functions, which have their return types
1990 // built into their names.
1991 if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND))
1992 return;
1993
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001994 // Determine the type of the declaration (if it has a type).
Douglas Gregor6f942b22010-09-21 16:06:22 +00001995 QualType T;
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001996 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND))
1997 T = Function->getResultType();
1998 else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
1999 T = Method->getResultType();
2000 else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND))
2001 T = FunTmpl->getTemplatedDecl()->getResultType();
2002 else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND))
2003 T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext()));
2004 else if (isa<UnresolvedUsingValueDecl>(ND)) {
2005 /* Do nothing: ignore unresolved using declarations*/
John McCallf85e1932011-06-15 23:02:42 +00002006 } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) {
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002007 T = Value->getType();
John McCallf85e1932011-06-15 23:02:42 +00002008 } else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND))
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002009 T = Property->getType();
2010
2011 if (T.isNull() || Context.hasSameType(T, Context.DependentTy))
2012 return;
2013
Douglas Gregor8987b232011-09-27 23:30:47 +00002014 Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy,
Douglas Gregora63f6de2011-02-01 21:15:40 +00002015 Result.getAllocator()));
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002016}
2017
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002018static void MaybeAddSentinel(ASTContext &Context, NamedDecl *FunctionOrMethod,
Douglas Gregor218937c2011-02-01 19:23:04 +00002019 CodeCompletionBuilder &Result) {
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002020 if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>())
2021 if (Sentinel->getSentinel() == 0) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002022 if (Context.getLangOpts().ObjC1 &&
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002023 Context.Idents.get("nil").hasMacroDefinition())
Douglas Gregor218937c2011-02-01 19:23:04 +00002024 Result.AddTextChunk(", nil");
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002025 else if (Context.Idents.get("NULL").hasMacroDefinition())
Douglas Gregor218937c2011-02-01 19:23:04 +00002026 Result.AddTextChunk(", NULL");
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002027 else
Douglas Gregor218937c2011-02-01 19:23:04 +00002028 Result.AddTextChunk(", (void*)0");
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002029 }
2030}
2031
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002032static std::string formatObjCParamQualifiers(unsigned ObjCQuals) {
2033 std::string Result;
2034 if (ObjCQuals & Decl::OBJC_TQ_In)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002035 Result += "in ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002036 else if (ObjCQuals & Decl::OBJC_TQ_Inout)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002037 Result += "inout ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002038 else if (ObjCQuals & Decl::OBJC_TQ_Out)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002039 Result += "out ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002040 if (ObjCQuals & Decl::OBJC_TQ_Bycopy)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002041 Result += "bycopy ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002042 else if (ObjCQuals & Decl::OBJC_TQ_Byref)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002043 Result += "byref ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002044 if (ObjCQuals & Decl::OBJC_TQ_Oneway)
Douglas Gregor6ef92092011-11-09 02:13:45 +00002045 Result += "oneway ";
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002046 return Result;
2047}
2048
Douglas Gregor83482d12010-08-24 16:15:59 +00002049static std::string FormatFunctionParameter(ASTContext &Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00002050 const PrintingPolicy &Policy,
Douglas Gregoraba48082010-08-29 19:47:46 +00002051 ParmVarDecl *Param,
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002052 bool SuppressName = false,
2053 bool SuppressBlock = false) {
Douglas Gregor83482d12010-08-24 16:15:59 +00002054 bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext());
2055 if (Param->getType()->isDependentType() ||
2056 !Param->getType()->isBlockPointerType()) {
2057 // The argument for a dependent or non-block parameter is a placeholder
2058 // containing that parameter's type.
2059 std::string Result;
2060
Douglas Gregoraba48082010-08-29 19:47:46 +00002061 if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName)
Douglas Gregor83482d12010-08-24 16:15:59 +00002062 Result = Param->getIdentifier()->getName();
2063
John McCallf85e1932011-06-15 23:02:42 +00002064 Param->getType().getAsStringInternal(Result, Policy);
Douglas Gregor83482d12010-08-24 16:15:59 +00002065
2066 if (ObjCMethodParam) {
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002067 Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier())
2068 + Result + ")";
Douglas Gregoraba48082010-08-29 19:47:46 +00002069 if (Param->getIdentifier() && !SuppressName)
Douglas Gregor83482d12010-08-24 16:15:59 +00002070 Result += Param->getIdentifier()->getName();
2071 }
2072 return Result;
2073 }
2074
2075 // The argument for a block pointer parameter is a block literal with
2076 // the appropriate type.
Douglas Gregor830072c2011-02-15 22:37:09 +00002077 FunctionTypeLoc *Block = 0;
2078 FunctionProtoTypeLoc *BlockProto = 0;
Douglas Gregor83482d12010-08-24 16:15:59 +00002079 TypeLoc TL;
2080 if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) {
2081 TL = TSInfo->getTypeLoc().getUnqualifiedLoc();
2082 while (true) {
2083 // Look through typedefs.
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002084 if (!SuppressBlock) {
2085 if (TypedefTypeLoc *TypedefTL = dyn_cast<TypedefTypeLoc>(&TL)) {
2086 if (TypeSourceInfo *InnerTSInfo
2087 = TypedefTL->getTypedefNameDecl()->getTypeSourceInfo()) {
2088 TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc();
2089 continue;
2090 }
2091 }
2092
2093 // Look through qualified types
2094 if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) {
2095 TL = QualifiedTL->getUnqualifiedLoc();
Douglas Gregor83482d12010-08-24 16:15:59 +00002096 continue;
2097 }
2098 }
2099
Douglas Gregor83482d12010-08-24 16:15:59 +00002100 // Try to get the function prototype behind the block pointer type,
2101 // then we're done.
2102 if (BlockPointerTypeLoc *BlockPtr
2103 = dyn_cast<BlockPointerTypeLoc>(&TL)) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002104 TL = BlockPtr->getPointeeLoc().IgnoreParens();
Douglas Gregor830072c2011-02-15 22:37:09 +00002105 Block = dyn_cast<FunctionTypeLoc>(&TL);
2106 BlockProto = dyn_cast<FunctionProtoTypeLoc>(&TL);
Douglas Gregor83482d12010-08-24 16:15:59 +00002107 }
2108 break;
2109 }
2110 }
2111
2112 if (!Block) {
2113 // We were unable to find a FunctionProtoTypeLoc with parameter names
2114 // for the block; just use the parameter type as a placeholder.
2115 std::string Result;
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002116 if (!ObjCMethodParam && Param->getIdentifier())
2117 Result = Param->getIdentifier()->getName();
2118
John McCallf85e1932011-06-15 23:02:42 +00002119 Param->getType().getUnqualifiedType().getAsStringInternal(Result, Policy);
Douglas Gregor83482d12010-08-24 16:15:59 +00002120
2121 if (ObjCMethodParam) {
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002122 Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier())
2123 + Result + ")";
Douglas Gregor83482d12010-08-24 16:15:59 +00002124 if (Param->getIdentifier())
2125 Result += Param->getIdentifier()->getName();
2126 }
2127
2128 return Result;
2129 }
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002130
Douglas Gregor83482d12010-08-24 16:15:59 +00002131 // We have the function prototype behind the block pointer type, as it was
2132 // written in the source.
Douglas Gregor38276252010-09-08 22:47:51 +00002133 std::string Result;
2134 QualType ResultType = Block->getTypePtr()->getResultType();
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002135 if (!ResultType->isVoidType() || SuppressBlock)
John McCallf85e1932011-06-15 23:02:42 +00002136 ResultType.getAsStringInternal(Result, Policy);
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002137
2138 // Format the parameter list.
2139 std::string Params;
Douglas Gregor830072c2011-02-15 22:37:09 +00002140 if (!BlockProto || Block->getNumArgs() == 0) {
2141 if (BlockProto && BlockProto->getTypePtr()->isVariadic())
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002142 Params = "(...)";
Douglas Gregorc2760bc2010-10-02 23:49:58 +00002143 else
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002144 Params = "(void)";
Douglas Gregor38276252010-09-08 22:47:51 +00002145 } else {
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002146 Params += "(";
Douglas Gregor38276252010-09-08 22:47:51 +00002147 for (unsigned I = 0, N = Block->getNumArgs(); I != N; ++I) {
2148 if (I)
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002149 Params += ", ";
2150 Params += FormatFunctionParameter(Context, Policy, Block->getArg(I),
2151 /*SuppressName=*/false,
2152 /*SuppressBlock=*/true);
Douglas Gregor38276252010-09-08 22:47:51 +00002153
Douglas Gregor830072c2011-02-15 22:37:09 +00002154 if (I == N - 1 && BlockProto->getTypePtr()->isVariadic())
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002155 Params += ", ...";
Douglas Gregor38276252010-09-08 22:47:51 +00002156 }
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002157 Params += ")";
Douglas Gregore17794f2010-08-31 05:13:43 +00002158 }
Douglas Gregor38276252010-09-08 22:47:51 +00002159
Douglas Gregoree1c68a2011-10-18 04:23:19 +00002160 if (SuppressBlock) {
2161 // Format as a parameter.
2162 Result = Result + " (^";
2163 if (Param->getIdentifier())
2164 Result += Param->getIdentifier()->getName();
2165 Result += ")";
2166 Result += Params;
2167 } else {
2168 // Format as a block literal argument.
2169 Result = '^' + Result;
2170 Result += Params;
2171
2172 if (Param->getIdentifier())
2173 Result += Param->getIdentifier()->getName();
2174 }
2175
Douglas Gregor83482d12010-08-24 16:15:59 +00002176 return Result;
2177}
2178
Douglas Gregor86d9a522009-09-21 16:56:56 +00002179/// \brief Add function parameter chunks to the given code completion string.
2180static void AddFunctionParameterChunks(ASTContext &Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00002181 const PrintingPolicy &Policy,
Douglas Gregor86d9a522009-09-21 16:56:56 +00002182 FunctionDecl *Function,
Douglas Gregor218937c2011-02-01 19:23:04 +00002183 CodeCompletionBuilder &Result,
2184 unsigned Start = 0,
2185 bool InOptional = false) {
Douglas Gregor218937c2011-02-01 19:23:04 +00002186 bool FirstParameter = true;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002187
Douglas Gregor218937c2011-02-01 19:23:04 +00002188 for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002189 ParmVarDecl *Param = Function->getParamDecl(P);
2190
Douglas Gregor218937c2011-02-01 19:23:04 +00002191 if (Param->hasDefaultArg() && !InOptional) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002192 // When we see an optional default argument, put that argument and
2193 // the remaining default arguments into a new, optional string.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002194 CodeCompletionBuilder Opt(Result.getAllocator(),
2195 Result.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00002196 if (!FirstParameter)
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002197 Opt.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor8987b232011-09-27 23:30:47 +00002198 AddFunctionParameterChunks(Context, Policy, Function, Opt, P, true);
Douglas Gregor218937c2011-02-01 19:23:04 +00002199 Result.AddOptionalChunk(Opt.TakeString());
2200 break;
Douglas Gregor86d9a522009-09-21 16:56:56 +00002201 }
2202
Douglas Gregor218937c2011-02-01 19:23:04 +00002203 if (FirstParameter)
2204 FirstParameter = false;
2205 else
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002206 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor218937c2011-02-01 19:23:04 +00002207
2208 InOptional = false;
Douglas Gregor86d9a522009-09-21 16:56:56 +00002209
2210 // Format the placeholder string.
Douglas Gregor8987b232011-09-27 23:30:47 +00002211 std::string PlaceholderStr = FormatFunctionParameter(Context, Policy,
2212 Param);
Douglas Gregor83482d12010-08-24 16:15:59 +00002213
Douglas Gregore17794f2010-08-31 05:13:43 +00002214 if (Function->isVariadic() && P == N - 1)
2215 PlaceholderStr += ", ...";
2216
Douglas Gregor86d9a522009-09-21 16:56:56 +00002217 // Add the placeholder string.
Douglas Gregordae68752011-02-01 22:57:45 +00002218 Result.AddPlaceholderChunk(
2219 Result.getAllocator().CopyString(PlaceholderStr));
Douglas Gregor86d9a522009-09-21 16:56:56 +00002220 }
Douglas Gregorb3d45252009-09-22 21:42:17 +00002221
2222 if (const FunctionProtoType *Proto
2223 = Function->getType()->getAs<FunctionProtoType>())
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002224 if (Proto->isVariadic()) {
Douglas Gregore17794f2010-08-31 05:13:43 +00002225 if (Proto->getNumArgs() == 0)
Douglas Gregor218937c2011-02-01 19:23:04 +00002226 Result.AddPlaceholderChunk("...");
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002227
Douglas Gregor218937c2011-02-01 19:23:04 +00002228 MaybeAddSentinel(Context, Function, Result);
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002229 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00002230}
2231
2232/// \brief Add template parameter chunks to the given code completion string.
2233static void AddTemplateParameterChunks(ASTContext &Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00002234 const PrintingPolicy &Policy,
Douglas Gregor86d9a522009-09-21 16:56:56 +00002235 TemplateDecl *Template,
Douglas Gregor218937c2011-02-01 19:23:04 +00002236 CodeCompletionBuilder &Result,
2237 unsigned MaxParameters = 0,
2238 unsigned Start = 0,
2239 bool InDefaultArg = false) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002240 bool FirstParameter = true;
2241
2242 TemplateParameterList *Params = Template->getTemplateParameters();
2243 TemplateParameterList::iterator PEnd = Params->end();
2244 if (MaxParameters)
2245 PEnd = Params->begin() + MaxParameters;
Douglas Gregor218937c2011-02-01 19:23:04 +00002246 for (TemplateParameterList::iterator P = Params->begin() + Start;
2247 P != PEnd; ++P) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002248 bool HasDefaultArg = false;
2249 std::string PlaceholderStr;
2250 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
2251 if (TTP->wasDeclaredWithTypename())
2252 PlaceholderStr = "typename";
2253 else
2254 PlaceholderStr = "class";
2255
2256 if (TTP->getIdentifier()) {
2257 PlaceholderStr += ' ';
2258 PlaceholderStr += TTP->getIdentifier()->getName();
2259 }
2260
2261 HasDefaultArg = TTP->hasDefaultArgument();
2262 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor218937c2011-02-01 19:23:04 +00002263 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002264 if (NTTP->getIdentifier())
2265 PlaceholderStr = NTTP->getIdentifier()->getName();
John McCallf85e1932011-06-15 23:02:42 +00002266 NTTP->getType().getAsStringInternal(PlaceholderStr, Policy);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002267 HasDefaultArg = NTTP->hasDefaultArgument();
2268 } else {
2269 assert(isa<TemplateTemplateParmDecl>(*P));
2270 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
2271
2272 // Since putting the template argument list into the placeholder would
2273 // be very, very long, we just use an abbreviation.
2274 PlaceholderStr = "template<...> class";
2275 if (TTP->getIdentifier()) {
2276 PlaceholderStr += ' ';
2277 PlaceholderStr += TTP->getIdentifier()->getName();
2278 }
2279
2280 HasDefaultArg = TTP->hasDefaultArgument();
2281 }
2282
Douglas Gregor218937c2011-02-01 19:23:04 +00002283 if (HasDefaultArg && !InDefaultArg) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002284 // When we see an optional default argument, put that argument and
2285 // the remaining default arguments into a new, optional string.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002286 CodeCompletionBuilder Opt(Result.getAllocator(),
2287 Result.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00002288 if (!FirstParameter)
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002289 Opt.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor8987b232011-09-27 23:30:47 +00002290 AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters,
Douglas Gregor218937c2011-02-01 19:23:04 +00002291 P - Params->begin(), true);
2292 Result.AddOptionalChunk(Opt.TakeString());
2293 break;
Douglas Gregor86d9a522009-09-21 16:56:56 +00002294 }
2295
Douglas Gregor218937c2011-02-01 19:23:04 +00002296 InDefaultArg = false;
2297
Douglas Gregor86d9a522009-09-21 16:56:56 +00002298 if (FirstParameter)
2299 FirstParameter = false;
2300 else
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002301 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002302
2303 // Add the placeholder string.
Douglas Gregordae68752011-02-01 22:57:45 +00002304 Result.AddPlaceholderChunk(
2305 Result.getAllocator().CopyString(PlaceholderStr));
Douglas Gregor86d9a522009-09-21 16:56:56 +00002306 }
2307}
2308
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00002309/// \brief Add a qualifier to the given code-completion string, if the
2310/// provided nested-name-specifier is non-NULL.
Douglas Gregora61a8792009-12-11 18:44:16 +00002311static void
Douglas Gregor218937c2011-02-01 19:23:04 +00002312AddQualifierToCompletionString(CodeCompletionBuilder &Result,
Douglas Gregora61a8792009-12-11 18:44:16 +00002313 NestedNameSpecifier *Qualifier,
2314 bool QualifierIsInformative,
Douglas Gregor8987b232011-09-27 23:30:47 +00002315 ASTContext &Context,
2316 const PrintingPolicy &Policy) {
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00002317 if (!Qualifier)
2318 return;
2319
2320 std::string PrintedNNS;
2321 {
2322 llvm::raw_string_ostream OS(PrintedNNS);
Douglas Gregor8987b232011-09-27 23:30:47 +00002323 Qualifier->print(OS, Policy);
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00002324 }
Douglas Gregor0563c262009-09-22 23:15:58 +00002325 if (QualifierIsInformative)
Douglas Gregordae68752011-02-01 22:57:45 +00002326 Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS));
Douglas Gregor0563c262009-09-22 23:15:58 +00002327 else
Douglas Gregordae68752011-02-01 22:57:45 +00002328 Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS));
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00002329}
2330
Douglas Gregor218937c2011-02-01 19:23:04 +00002331static void
2332AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result,
2333 FunctionDecl *Function) {
Douglas Gregora61a8792009-12-11 18:44:16 +00002334 const FunctionProtoType *Proto
2335 = Function->getType()->getAs<FunctionProtoType>();
2336 if (!Proto || !Proto->getTypeQuals())
2337 return;
2338
Douglas Gregora63f6de2011-02-01 21:15:40 +00002339 // FIXME: Add ref-qualifier!
2340
2341 // Handle single qualifiers without copying
2342 if (Proto->getTypeQuals() == Qualifiers::Const) {
2343 Result.AddInformativeChunk(" const");
2344 return;
2345 }
2346
2347 if (Proto->getTypeQuals() == Qualifiers::Volatile) {
2348 Result.AddInformativeChunk(" volatile");
2349 return;
2350 }
2351
2352 if (Proto->getTypeQuals() == Qualifiers::Restrict) {
2353 Result.AddInformativeChunk(" restrict");
2354 return;
2355 }
2356
2357 // Handle multiple qualifiers.
Douglas Gregora61a8792009-12-11 18:44:16 +00002358 std::string QualsStr;
2359 if (Proto->getTypeQuals() & Qualifiers::Const)
2360 QualsStr += " const";
2361 if (Proto->getTypeQuals() & Qualifiers::Volatile)
2362 QualsStr += " volatile";
2363 if (Proto->getTypeQuals() & Qualifiers::Restrict)
2364 QualsStr += " restrict";
Douglas Gregordae68752011-02-01 22:57:45 +00002365 Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr));
Douglas Gregora61a8792009-12-11 18:44:16 +00002366}
2367
Douglas Gregor6f942b22010-09-21 16:06:22 +00002368/// \brief Add the name of the given declaration
Douglas Gregor8987b232011-09-27 23:30:47 +00002369static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy,
2370 NamedDecl *ND, CodeCompletionBuilder &Result) {
Douglas Gregor6f942b22010-09-21 16:06:22 +00002371 DeclarationName Name = ND->getDeclName();
2372 if (!Name)
2373 return;
2374
2375 switch (Name.getNameKind()) {
Douglas Gregora63f6de2011-02-01 21:15:40 +00002376 case DeclarationName::CXXOperatorName: {
2377 const char *OperatorName = 0;
2378 switch (Name.getCXXOverloadedOperator()) {
2379 case OO_None:
2380 case OO_Conditional:
2381 case NUM_OVERLOADED_OPERATORS:
2382 OperatorName = "operator";
2383 break;
2384
2385#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2386 case OO_##Name: OperatorName = "operator" Spelling; break;
2387#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2388#include "clang/Basic/OperatorKinds.def"
2389
2390 case OO_New: OperatorName = "operator new"; break;
2391 case OO_Delete: OperatorName = "operator delete"; break;
2392 case OO_Array_New: OperatorName = "operator new[]"; break;
2393 case OO_Array_Delete: OperatorName = "operator delete[]"; break;
2394 case OO_Call: OperatorName = "operator()"; break;
2395 case OO_Subscript: OperatorName = "operator[]"; break;
2396 }
2397 Result.AddTypedTextChunk(OperatorName);
2398 break;
2399 }
2400
Douglas Gregor6f942b22010-09-21 16:06:22 +00002401 case DeclarationName::Identifier:
2402 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor6f942b22010-09-21 16:06:22 +00002403 case DeclarationName::CXXDestructorName:
2404 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregordae68752011-02-01 22:57:45 +00002405 Result.AddTypedTextChunk(
2406 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregor6f942b22010-09-21 16:06:22 +00002407 break;
2408
2409 case DeclarationName::CXXUsingDirective:
2410 case DeclarationName::ObjCZeroArgSelector:
2411 case DeclarationName::ObjCOneArgSelector:
2412 case DeclarationName::ObjCMultiArgSelector:
2413 break;
2414
2415 case DeclarationName::CXXConstructorName: {
2416 CXXRecordDecl *Record = 0;
2417 QualType Ty = Name.getCXXNameType();
2418 if (const RecordType *RecordTy = Ty->getAs<RecordType>())
2419 Record = cast<CXXRecordDecl>(RecordTy->getDecl());
2420 else if (const InjectedClassNameType *InjectedTy
2421 = Ty->getAs<InjectedClassNameType>())
2422 Record = InjectedTy->getDecl();
2423 else {
Douglas Gregordae68752011-02-01 22:57:45 +00002424 Result.AddTypedTextChunk(
2425 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregor6f942b22010-09-21 16:06:22 +00002426 break;
2427 }
2428
Douglas Gregordae68752011-02-01 22:57:45 +00002429 Result.AddTypedTextChunk(
2430 Result.getAllocator().CopyString(Record->getNameAsString()));
Douglas Gregor6f942b22010-09-21 16:06:22 +00002431 if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) {
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002432 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
Douglas Gregor8987b232011-09-27 23:30:47 +00002433 AddTemplateParameterChunks(Context, Policy, Template, Result);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002434 Result.AddChunk(CodeCompletionString::CK_RightAngle);
Douglas Gregor6f942b22010-09-21 16:06:22 +00002435 }
2436 break;
2437 }
2438 }
2439}
2440
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002441CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002442 CodeCompletionAllocator &Allocator,
2443 CodeCompletionTUInfo &CCTUInfo) {
2444 return CreateCodeCompletionString(S.Context, S.PP, Allocator, CCTUInfo);
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002445}
2446
Douglas Gregor86d9a522009-09-21 16:56:56 +00002447/// \brief If possible, create a new code completion string for the given
2448/// result.
2449///
2450/// \returns Either a new, heap-allocated code completion string describing
2451/// how to use this result, or NULL to indicate that the string or name of the
2452/// result is all that is needed.
2453CodeCompletionString *
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002454CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
2455 Preprocessor &PP,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002456 CodeCompletionAllocator &Allocator,
2457 CodeCompletionTUInfo &CCTUInfo) {
2458 CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002459
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002460 PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP);
Douglas Gregor218937c2011-02-01 19:23:04 +00002461 if (Kind == RK_Pattern) {
2462 Pattern->Priority = Priority;
2463 Pattern->Availability = Availability;
Douglas Gregorba103062012-03-27 23:34:16 +00002464
2465 if (Declaration) {
2466 Result.addParentContext(Declaration->getDeclContext());
2467 Pattern->ParentKind = Result.getParentKind();
2468 Pattern->ParentName = Result.getParentName();
2469 }
2470
Douglas Gregor218937c2011-02-01 19:23:04 +00002471 return Pattern;
2472 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002473
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002474 if (Kind == RK_Keyword) {
Douglas Gregor218937c2011-02-01 19:23:04 +00002475 Result.AddTypedTextChunk(Keyword);
2476 return Result.TakeString();
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002477 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00002478
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002479 if (Kind == RK_Macro) {
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002480 MacroInfo *MI = PP.getMacroInfo(Macro);
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002481 assert(MI && "Not a macro?");
2482
Douglas Gregordae68752011-02-01 22:57:45 +00002483 Result.AddTypedTextChunk(
2484 Result.getAllocator().CopyString(Macro->getName()));
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002485
2486 if (!MI->isFunctionLike())
Douglas Gregor218937c2011-02-01 19:23:04 +00002487 return Result.TakeString();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002488
2489 // Format a function-like macro with placeholders for the arguments.
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002490 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregore4244702011-07-30 08:17:44 +00002491 MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end();
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002492
2493 // C99 variadic macros add __VA_ARGS__ at the end. Skip it.
2494 if (MI->isC99Varargs()) {
2495 --AEnd;
2496
2497 if (A == AEnd) {
2498 Result.AddPlaceholderChunk("...");
2499 }
Douglas Gregore4244702011-07-30 08:17:44 +00002500 }
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002501
Douglas Gregore4244702011-07-30 08:17:44 +00002502 for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) {
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002503 if (A != MI->arg_begin())
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002504 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002505
2506 if (MI->isVariadic() && (A+1) == AEnd) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002507 SmallString<32> Arg = (*A)->getName();
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002508 if (MI->isC99Varargs())
2509 Arg += ", ...";
2510 else
2511 Arg += "...";
Douglas Gregordae68752011-02-01 22:57:45 +00002512 Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002513 break;
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002514 }
Douglas Gregorc8dc1352012-01-21 00:43:38 +00002515
2516 // Non-variadic macros are simple.
2517 Result.AddPlaceholderChunk(
2518 Result.getAllocator().CopyString((*A)->getName()));
Douglas Gregore4244702011-07-30 08:17:44 +00002519 }
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002520 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor218937c2011-02-01 19:23:04 +00002521 return Result.TakeString();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002522 }
2523
Douglas Gregord8e8a582010-05-25 21:41:55 +00002524 assert(Kind == RK_Declaration && "Missed a result kind?");
Douglas Gregor86d9a522009-09-21 16:56:56 +00002525 NamedDecl *ND = Declaration;
Douglas Gregorba103062012-03-27 23:34:16 +00002526 Result.addParentContext(ND->getDeclContext());
2527
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002528 if (StartsNestedNameSpecifier) {
Douglas Gregordae68752011-02-01 22:57:45 +00002529 Result.AddTypedTextChunk(
2530 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregor218937c2011-02-01 19:23:04 +00002531 Result.AddTextChunk("::");
2532 return Result.TakeString();
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002533 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00002534
2535 for (Decl::attr_iterator i = ND->attr_begin(); i != ND->attr_end(); ++i) {
2536 if (AnnotateAttr *Attr = dyn_cast_or_null<AnnotateAttr>(*i)) {
2537 Result.AddAnnotation(Result.getAllocator().CopyString(Attr->getAnnotation()));
2538 }
2539 }
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002540
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002541 AddResultTypeChunk(Ctx, Policy, ND, Result);
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00002542
Douglas Gregor86d9a522009-09-21 16:56:56 +00002543 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
Douglas Gregor0563c262009-09-22 23:15:58 +00002544 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002545 Ctx, Policy);
2546 AddTypedNameChunk(Ctx, Policy, ND, Result);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002547 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002548 AddFunctionParameterChunks(Ctx, Policy, Function, Result);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002549 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregora61a8792009-12-11 18:44:16 +00002550 AddFunctionTypeQualsToCompletionString(Result, Function);
Douglas Gregor218937c2011-02-01 19:23:04 +00002551 return Result.TakeString();
Douglas Gregor86d9a522009-09-21 16:56:56 +00002552 }
2553
2554 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
Douglas Gregor0563c262009-09-22 23:15:58 +00002555 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002556 Ctx, Policy);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002557 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002558 AddTypedNameChunk(Ctx, Policy, Function, Result);
Douglas Gregor6f942b22010-09-21 16:06:22 +00002559
Douglas Gregor86d9a522009-09-21 16:56:56 +00002560 // Figure out which template parameters are deduced (or have default
2561 // arguments).
Benjamin Kramer013b3662012-01-30 16:17:39 +00002562 llvm::SmallBitVector Deduced;
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002563 Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002564 unsigned LastDeducibleArgument;
2565 for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0;
2566 --LastDeducibleArgument) {
2567 if (!Deduced[LastDeducibleArgument - 1]) {
2568 // C++0x: Figure out if the template argument has a default. If so,
2569 // the user doesn't need to type this argument.
2570 // FIXME: We need to abstract template parameters better!
2571 bool HasDefaultArg = false;
2572 NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam(
Douglas Gregor218937c2011-02-01 19:23:04 +00002573 LastDeducibleArgument - 1);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002574 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
2575 HasDefaultArg = TTP->hasDefaultArgument();
2576 else if (NonTypeTemplateParmDecl *NTTP
2577 = dyn_cast<NonTypeTemplateParmDecl>(Param))
2578 HasDefaultArg = NTTP->hasDefaultArgument();
2579 else {
2580 assert(isa<TemplateTemplateParmDecl>(Param));
2581 HasDefaultArg
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002582 = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument();
Douglas Gregor86d9a522009-09-21 16:56:56 +00002583 }
2584
2585 if (!HasDefaultArg)
2586 break;
2587 }
2588 }
2589
2590 if (LastDeducibleArgument) {
2591 // Some of the function template arguments cannot be deduced from a
2592 // function call, so we introduce an explicit template argument list
2593 // containing all of the arguments up to the first deducible argument.
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002594 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002595 AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result,
Douglas Gregor86d9a522009-09-21 16:56:56 +00002596 LastDeducibleArgument);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002597 Result.AddChunk(CodeCompletionString::CK_RightAngle);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002598 }
2599
2600 // Add the function parameters
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002601 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002602 AddFunctionParameterChunks(Ctx, Policy, Function, Result);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002603 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregora61a8792009-12-11 18:44:16 +00002604 AddFunctionTypeQualsToCompletionString(Result, Function);
Douglas Gregor218937c2011-02-01 19:23:04 +00002605 return Result.TakeString();
Douglas Gregor86d9a522009-09-21 16:56:56 +00002606 }
2607
2608 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) {
Douglas Gregor0563c262009-09-22 23:15:58 +00002609 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002610 Ctx, Policy);
Douglas Gregordae68752011-02-01 22:57:45 +00002611 Result.AddTypedTextChunk(
2612 Result.getAllocator().CopyString(Template->getNameAsString()));
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002613 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002614 AddTemplateParameterChunks(Ctx, Policy, Template, Result);
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002615 Result.AddChunk(CodeCompletionString::CK_RightAngle);
Douglas Gregor218937c2011-02-01 19:23:04 +00002616 return Result.TakeString();
Douglas Gregor86d9a522009-09-21 16:56:56 +00002617 }
2618
Douglas Gregor9630eb62009-11-17 16:44:22 +00002619 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) {
Douglas Gregor9630eb62009-11-17 16:44:22 +00002620 Selector Sel = Method->getSelector();
2621 if (Sel.isUnarySelector()) {
Douglas Gregordae68752011-02-01 22:57:45 +00002622 Result.AddTypedTextChunk(Result.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00002623 Sel.getNameForSlot(0)));
Douglas Gregor218937c2011-02-01 19:23:04 +00002624 return Result.TakeString();
Douglas Gregor9630eb62009-11-17 16:44:22 +00002625 }
2626
Douglas Gregor813d8342011-02-18 22:29:55 +00002627 std::string SelName = Sel.getNameForSlot(0).str();
Douglas Gregord3c68542009-11-19 01:08:35 +00002628 SelName += ':';
2629 if (StartParameter == 0)
Douglas Gregordae68752011-02-01 22:57:45 +00002630 Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName));
Douglas Gregord3c68542009-11-19 01:08:35 +00002631 else {
Douglas Gregordae68752011-02-01 22:57:45 +00002632 Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName));
Douglas Gregord3c68542009-11-19 01:08:35 +00002633
2634 // If there is only one parameter, and we're past it, add an empty
2635 // typed-text chunk since there is nothing to type.
2636 if (Method->param_size() == 1)
Douglas Gregor218937c2011-02-01 19:23:04 +00002637 Result.AddTypedTextChunk("");
Douglas Gregord3c68542009-11-19 01:08:35 +00002638 }
Douglas Gregor9630eb62009-11-17 16:44:22 +00002639 unsigned Idx = 0;
2640 for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
2641 PEnd = Method->param_end();
2642 P != PEnd; (void)++P, ++Idx) {
2643 if (Idx > 0) {
Douglas Gregord3c68542009-11-19 01:08:35 +00002644 std::string Keyword;
2645 if (Idx > StartParameter)
Douglas Gregor218937c2011-02-01 19:23:04 +00002646 Result.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregor9630eb62009-11-17 16:44:22 +00002647 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx))
Benjamin Kramera0651c52011-07-26 16:59:25 +00002648 Keyword += II->getName();
Douglas Gregor9630eb62009-11-17 16:44:22 +00002649 Keyword += ":";
Douglas Gregor1f5537a2010-07-08 23:20:03 +00002650 if (Idx < StartParameter || AllParametersAreInformative)
Douglas Gregordae68752011-02-01 22:57:45 +00002651 Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword));
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +00002652 else
Douglas Gregordae68752011-02-01 22:57:45 +00002653 Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword));
Douglas Gregor9630eb62009-11-17 16:44:22 +00002654 }
Douglas Gregord3c68542009-11-19 01:08:35 +00002655
2656 // If we're before the starting parameter, skip the placeholder.
2657 if (Idx < StartParameter)
2658 continue;
Douglas Gregor9630eb62009-11-17 16:44:22 +00002659
2660 std::string Arg;
Douglas Gregor83482d12010-08-24 16:15:59 +00002661
2662 if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity)
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002663 Arg = FormatFunctionParameter(Ctx, Policy, *P, true);
Douglas Gregor83482d12010-08-24 16:15:59 +00002664 else {
John McCallf85e1932011-06-15 23:02:42 +00002665 (*P)->getType().getAsStringInternal(Arg, Policy);
Douglas Gregor6fa14dd2011-07-30 07:55:26 +00002666 Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier())
2667 + Arg + ")";
Douglas Gregor83482d12010-08-24 16:15:59 +00002668 if (IdentifierInfo *II = (*P)->getIdentifier())
Douglas Gregoraba48082010-08-29 19:47:46 +00002669 if (DeclaringEntity || AllParametersAreInformative)
Benjamin Kramera0651c52011-07-26 16:59:25 +00002670 Arg += II->getName();
Douglas Gregor83482d12010-08-24 16:15:59 +00002671 }
2672
Douglas Gregore17794f2010-08-31 05:13:43 +00002673 if (Method->isVariadic() && (P + 1) == PEnd)
2674 Arg += ", ...";
2675
Douglas Gregor1f5537a2010-07-08 23:20:03 +00002676 if (DeclaringEntity)
Douglas Gregordae68752011-02-01 22:57:45 +00002677 Result.AddTextChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregor1f5537a2010-07-08 23:20:03 +00002678 else if (AllParametersAreInformative)
Douglas Gregordae68752011-02-01 22:57:45 +00002679 Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregor4ad96852009-11-19 07:41:15 +00002680 else
Douglas Gregordae68752011-02-01 22:57:45 +00002681 Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregor9630eb62009-11-17 16:44:22 +00002682 }
2683
Douglas Gregor2a17af02009-12-23 00:21:46 +00002684 if (Method->isVariadic()) {
Douglas Gregore17794f2010-08-31 05:13:43 +00002685 if (Method->param_size() == 0) {
2686 if (DeclaringEntity)
Douglas Gregor218937c2011-02-01 19:23:04 +00002687 Result.AddTextChunk(", ...");
Douglas Gregore17794f2010-08-31 05:13:43 +00002688 else if (AllParametersAreInformative)
Douglas Gregor218937c2011-02-01 19:23:04 +00002689 Result.AddInformativeChunk(", ...");
Douglas Gregore17794f2010-08-31 05:13:43 +00002690 else
Douglas Gregor218937c2011-02-01 19:23:04 +00002691 Result.AddPlaceholderChunk(", ...");
Douglas Gregore17794f2010-08-31 05:13:43 +00002692 }
Douglas Gregoraaa107a2010-08-23 23:51:41 +00002693
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002694 MaybeAddSentinel(Ctx, Method, Result);
Douglas Gregor2a17af02009-12-23 00:21:46 +00002695 }
2696
Douglas Gregor218937c2011-02-01 19:23:04 +00002697 return Result.TakeString();
Douglas Gregor9630eb62009-11-17 16:44:22 +00002698 }
2699
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002700 if (Qualifier)
Douglas Gregor0563c262009-09-22 23:15:58 +00002701 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidisea8c59a2012-01-17 02:15:51 +00002702 Ctx, Policy);
Douglas Gregor2b4074f2009-12-01 05:55:20 +00002703
Douglas Gregordae68752011-02-01 22:57:45 +00002704 Result.AddTypedTextChunk(
2705 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregor218937c2011-02-01 19:23:04 +00002706 return Result.TakeString();
Douglas Gregor86d9a522009-09-21 16:56:56 +00002707}
2708
Douglas Gregor86d802e2009-09-23 00:34:09 +00002709CodeCompletionString *
2710CodeCompleteConsumer::OverloadCandidate::CreateSignatureString(
2711 unsigned CurrentArg,
Douglas Gregor32be4a52010-10-11 21:37:58 +00002712 Sema &S,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002713 CodeCompletionAllocator &Allocator,
2714 CodeCompletionTUInfo &CCTUInfo) const {
Douglas Gregor8987b232011-09-27 23:30:47 +00002715 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
John McCallf85e1932011-06-15 23:02:42 +00002716
Douglas Gregor218937c2011-02-01 19:23:04 +00002717 // FIXME: Set priority, availability appropriately.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00002718 CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002719 FunctionDecl *FDecl = getFunction();
Douglas Gregor8987b232011-09-27 23:30:47 +00002720 AddResultTypeChunk(S.Context, Policy, FDecl, Result);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002721 const FunctionProtoType *Proto
2722 = dyn_cast<FunctionProtoType>(getFunctionType());
2723 if (!FDecl && !Proto) {
2724 // Function without a prototype. Just give the return type and a
2725 // highlighted ellipsis.
2726 const FunctionType *FT = getFunctionType();
Douglas Gregora63f6de2011-02-01 21:15:40 +00002727 Result.AddTextChunk(GetCompletionTypeString(FT->getResultType(),
Douglas Gregor8987b232011-09-27 23:30:47 +00002728 S.Context, Policy,
Douglas Gregora63f6de2011-02-01 21:15:40 +00002729 Result.getAllocator()));
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002730 Result.AddChunk(CodeCompletionString::CK_LeftParen);
2731 Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "...");
2732 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor218937c2011-02-01 19:23:04 +00002733 return Result.TakeString();
Douglas Gregor86d802e2009-09-23 00:34:09 +00002734 }
2735
2736 if (FDecl)
Douglas Gregordae68752011-02-01 22:57:45 +00002737 Result.AddTextChunk(
2738 Result.getAllocator().CopyString(FDecl->getNameAsString()));
Douglas Gregor86d802e2009-09-23 00:34:09 +00002739 else
Douglas Gregor218937c2011-02-01 19:23:04 +00002740 Result.AddTextChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00002741 Result.getAllocator().CopyString(
John McCallf85e1932011-06-15 23:02:42 +00002742 Proto->getResultType().getAsString(Policy)));
Douglas Gregor86d802e2009-09-23 00:34:09 +00002743
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002744 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002745 unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs();
2746 for (unsigned I = 0; I != NumParams; ++I) {
2747 if (I)
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002748 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002749
2750 std::string ArgString;
2751 QualType ArgType;
2752
2753 if (FDecl) {
2754 ArgString = FDecl->getParamDecl(I)->getNameAsString();
2755 ArgType = FDecl->getParamDecl(I)->getOriginalType();
2756 } else {
2757 ArgType = Proto->getArgType(I);
2758 }
2759
John McCallf85e1932011-06-15 23:02:42 +00002760 ArgType.getAsStringInternal(ArgString, Policy);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002761
2762 if (I == CurrentArg)
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002763 Result.AddChunk(CodeCompletionString::CK_CurrentParameter,
2764 Result.getAllocator().CopyString(ArgString));
Douglas Gregor86d802e2009-09-23 00:34:09 +00002765 else
Douglas Gregordae68752011-02-01 22:57:45 +00002766 Result.AddTextChunk(Result.getAllocator().CopyString(ArgString));
Douglas Gregor86d802e2009-09-23 00:34:09 +00002767 }
2768
2769 if (Proto && Proto->isVariadic()) {
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002770 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002771 if (CurrentArg < NumParams)
Douglas Gregor218937c2011-02-01 19:23:04 +00002772 Result.AddTextChunk("...");
Douglas Gregor86d802e2009-09-23 00:34:09 +00002773 else
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002774 Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "...");
Douglas Gregor86d802e2009-09-23 00:34:09 +00002775 }
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00002776 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor86d802e2009-09-23 00:34:09 +00002777
Douglas Gregor218937c2011-02-01 19:23:04 +00002778 return Result.TakeString();
Douglas Gregor86d802e2009-09-23 00:34:09 +00002779}
2780
Chris Lattner5f9e2722011-07-23 10:55:15 +00002781unsigned clang::getMacroUsagePriority(StringRef MacroName,
Douglas Gregorb05496d2010-09-20 21:11:48 +00002782 const LangOptions &LangOpts,
Douglas Gregor1827e102010-08-16 16:18:59 +00002783 bool PreferredTypeIsPointer) {
2784 unsigned Priority = CCP_Macro;
2785
Douglas Gregorb05496d2010-09-20 21:11:48 +00002786 // Treat the "nil", "Nil" and "NULL" macros as null pointer constants.
2787 if (MacroName.equals("nil") || MacroName.equals("NULL") ||
2788 MacroName.equals("Nil")) {
Douglas Gregor1827e102010-08-16 16:18:59 +00002789 Priority = CCP_Constant;
2790 if (PreferredTypeIsPointer)
2791 Priority = Priority / CCF_SimilarTypeMatch;
Douglas Gregorb05496d2010-09-20 21:11:48 +00002792 }
2793 // Treat "YES", "NO", "true", and "false" as constants.
2794 else if (MacroName.equals("YES") || MacroName.equals("NO") ||
2795 MacroName.equals("true") || MacroName.equals("false"))
2796 Priority = CCP_Constant;
2797 // Treat "bool" as a type.
2798 else if (MacroName.equals("bool"))
2799 Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0);
2800
Douglas Gregor1827e102010-08-16 16:18:59 +00002801
2802 return Priority;
2803}
2804
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002805CXCursorKind clang::getCursorKindForDecl(Decl *D) {
2806 if (!D)
2807 return CXCursor_UnexposedDecl;
2808
2809 switch (D->getKind()) {
2810 case Decl::Enum: return CXCursor_EnumDecl;
2811 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
2812 case Decl::Field: return CXCursor_FieldDecl;
2813 case Decl::Function:
2814 return CXCursor_FunctionDecl;
2815 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
2816 case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002817 case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl;
Douglas Gregor375bb142011-12-27 22:43:10 +00002818
Argyrios Kyrtzidisc15707d2012-01-24 21:39:26 +00002819 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002820 case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl;
2821 case Decl::ObjCMethod:
2822 return cast<ObjCMethodDecl>(D)->isInstanceMethod()
2823 ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl;
2824 case Decl::CXXMethod: return CXCursor_CXXMethod;
2825 case Decl::CXXConstructor: return CXCursor_Constructor;
2826 case Decl::CXXDestructor: return CXCursor_Destructor;
2827 case Decl::CXXConversion: return CXCursor_ConversionFunction;
2828 case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl;
Argyrios Kyrtzidisc15707d2012-01-24 21:39:26 +00002829 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002830 case Decl::ParmVar: return CXCursor_ParmDecl;
2831 case Decl::Typedef: return CXCursor_TypedefDecl;
Richard Smith162e1c12011-04-15 14:24:37 +00002832 case Decl::TypeAlias: return CXCursor_TypeAliasDecl;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002833 case Decl::Var: return CXCursor_VarDecl;
2834 case Decl::Namespace: return CXCursor_Namespace;
2835 case Decl::NamespaceAlias: return CXCursor_NamespaceAlias;
2836 case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter;
2837 case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter;
2838 case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter;
2839 case Decl::FunctionTemplate: return CXCursor_FunctionTemplate;
2840 case Decl::ClassTemplate: return CXCursor_ClassTemplate;
Argyrios Kyrtzidis2dfdb942011-09-30 17:58:23 +00002841 case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002842 case Decl::ClassTemplatePartialSpecialization:
2843 return CXCursor_ClassTemplatePartialSpecialization;
2844 case Decl::UsingDirective: return CXCursor_UsingDirective;
Douglas Gregor8e5900c2012-04-30 23:41:16 +00002845 case Decl::TranslationUnit: return CXCursor_TranslationUnit;
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002846
2847 case Decl::Using:
2848 case Decl::UnresolvedUsingValue:
2849 case Decl::UnresolvedUsingTypename:
2850 return CXCursor_UsingDeclaration;
2851
Douglas Gregor352697a2011-06-03 23:08:58 +00002852 case Decl::ObjCPropertyImpl:
2853 switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) {
2854 case ObjCPropertyImplDecl::Dynamic:
2855 return CXCursor_ObjCDynamicDecl;
2856
2857 case ObjCPropertyImplDecl::Synthesize:
2858 return CXCursor_ObjCSynthesizeDecl;
2859 }
Douglas Gregor352697a2011-06-03 23:08:58 +00002860
Douglas Gregore8d7beb2010-09-03 23:30:36 +00002861 default:
2862 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
2863 switch (TD->getTagKind()) {
2864 case TTK_Struct: return CXCursor_StructDecl;
2865 case TTK_Class: return CXCursor_ClassDecl;
2866 case TTK_Union: return CXCursor_UnionDecl;
2867 case TTK_Enum: return CXCursor_EnumDecl;
2868 }
2869 }
2870 }
2871
2872 return CXCursor_UnexposedDecl;
2873}
2874
Douglas Gregor590c7d52010-07-08 20:55:51 +00002875static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results,
2876 bool TargetTypeIsPointer = false) {
John McCall0a2c5e22010-08-25 06:19:51 +00002877 typedef CodeCompletionResult Result;
Douglas Gregor590c7d52010-07-08 20:55:51 +00002878
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002879 Results.EnterNewScope();
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +00002880
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002881 for (Preprocessor::macro_iterator M = PP.macro_begin(),
2882 MEnd = PP.macro_end();
Douglas Gregor590c7d52010-07-08 20:55:51 +00002883 M != MEnd; ++M) {
Douglas Gregor1827e102010-08-16 16:18:59 +00002884 Results.AddResult(Result(M->first,
2885 getMacroUsagePriority(M->first->getName(),
David Blaikie4e4d0842012-03-11 07:00:24 +00002886 PP.getLangOpts(),
Douglas Gregor1827e102010-08-16 16:18:59 +00002887 TargetTypeIsPointer)));
Douglas Gregor590c7d52010-07-08 20:55:51 +00002888 }
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +00002889
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002890 Results.ExitScope();
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +00002891
Douglas Gregor3f7c7f42009-10-30 16:50:04 +00002892}
2893
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00002894static void AddPrettyFunctionResults(const LangOptions &LangOpts,
2895 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00002896 typedef CodeCompletionResult Result;
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00002897
2898 Results.EnterNewScope();
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +00002899
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00002900 Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant));
2901 Results.AddResult(Result("__FUNCTION__", CCP_Constant));
2902 if (LangOpts.C99 || LangOpts.CPlusPlus0x)
2903 Results.AddResult(Result("__func__", CCP_Constant));
2904 Results.ExitScope();
2905}
2906
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00002907static void HandleCodeCompleteResults(Sema *S,
2908 CodeCompleteConsumer *CodeCompleter,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002909 CodeCompletionContext Context,
John McCall0a2c5e22010-08-25 06:19:51 +00002910 CodeCompletionResult *Results,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002911 unsigned NumResults) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00002912 if (CodeCompleter)
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002913 CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults);
Douglas Gregor86d9a522009-09-21 16:56:56 +00002914}
2915
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002916static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S,
2917 Sema::ParserCompletionContext PCC) {
2918 switch (PCC) {
John McCallf312b1e2010-08-26 23:41:50 +00002919 case Sema::PCC_Namespace:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002920 return CodeCompletionContext::CCC_TopLevel;
2921
John McCallf312b1e2010-08-26 23:41:50 +00002922 case Sema::PCC_Class:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002923 return CodeCompletionContext::CCC_ClassStructUnion;
2924
John McCallf312b1e2010-08-26 23:41:50 +00002925 case Sema::PCC_ObjCInterface:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002926 return CodeCompletionContext::CCC_ObjCInterface;
2927
John McCallf312b1e2010-08-26 23:41:50 +00002928 case Sema::PCC_ObjCImplementation:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002929 return CodeCompletionContext::CCC_ObjCImplementation;
2930
John McCallf312b1e2010-08-26 23:41:50 +00002931 case Sema::PCC_ObjCInstanceVariableList:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002932 return CodeCompletionContext::CCC_ObjCIvarList;
2933
John McCallf312b1e2010-08-26 23:41:50 +00002934 case Sema::PCC_Template:
2935 case Sema::PCC_MemberTemplate:
Douglas Gregor52779fb2010-09-23 23:01:17 +00002936 if (S.CurContext->isFileContext())
2937 return CodeCompletionContext::CCC_TopLevel;
David Blaikie7530c032012-01-17 06:56:22 +00002938 if (S.CurContext->isRecord())
Douglas Gregor52779fb2010-09-23 23:01:17 +00002939 return CodeCompletionContext::CCC_ClassStructUnion;
David Blaikie7530c032012-01-17 06:56:22 +00002940 return CodeCompletionContext::CCC_Other;
Douglas Gregor52779fb2010-09-23 23:01:17 +00002941
John McCallf312b1e2010-08-26 23:41:50 +00002942 case Sema::PCC_RecoveryInFunction:
Douglas Gregor52779fb2010-09-23 23:01:17 +00002943 return CodeCompletionContext::CCC_Recovery;
Douglas Gregora5450a02010-10-18 22:01:46 +00002944
John McCallf312b1e2010-08-26 23:41:50 +00002945 case Sema::PCC_ForInit:
David Blaikie4e4d0842012-03-11 07:00:24 +00002946 if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 ||
2947 S.getLangOpts().ObjC1)
Douglas Gregora5450a02010-10-18 22:01:46 +00002948 return CodeCompletionContext::CCC_ParenthesizedExpression;
2949 else
2950 return CodeCompletionContext::CCC_Expression;
2951
2952 case Sema::PCC_Expression:
John McCallf312b1e2010-08-26 23:41:50 +00002953 case Sema::PCC_Condition:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002954 return CodeCompletionContext::CCC_Expression;
2955
John McCallf312b1e2010-08-26 23:41:50 +00002956 case Sema::PCC_Statement:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002957 return CodeCompletionContext::CCC_Statement;
Douglas Gregor72db1082010-08-24 01:11:00 +00002958
John McCallf312b1e2010-08-26 23:41:50 +00002959 case Sema::PCC_Type:
Douglas Gregor72db1082010-08-24 01:11:00 +00002960 return CodeCompletionContext::CCC_Type;
Douglas Gregor02688102010-09-14 23:59:36 +00002961
2962 case Sema::PCC_ParenthesizedExpression:
2963 return CodeCompletionContext::CCC_ParenthesizedExpression;
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002964
2965 case Sema::PCC_LocalDeclarationSpecifiers:
2966 return CodeCompletionContext::CCC_Type;
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002967 }
David Blaikie7530c032012-01-17 06:56:22 +00002968
2969 llvm_unreachable("Invalid ParserCompletionContext!");
Douglas Gregore6b1bb62010-08-11 21:23:17 +00002970}
2971
Douglas Gregorf6961522010-08-27 21:18:54 +00002972/// \brief If we're in a C++ virtual member function, add completion results
2973/// that invoke the functions we override, since it's common to invoke the
2974/// overridden function as well as adding new functionality.
2975///
2976/// \param S The semantic analysis object for which we are generating results.
2977///
2978/// \param InContext This context in which the nested-name-specifier preceding
2979/// the code-completion point
2980static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext,
2981 ResultBuilder &Results) {
2982 // Look through blocks.
2983 DeclContext *CurContext = S.CurContext;
2984 while (isa<BlockDecl>(CurContext))
2985 CurContext = CurContext->getParent();
2986
2987
2988 CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext);
2989 if (!Method || !Method->isVirtual())
2990 return;
2991
2992 // We need to have names for all of the parameters, if we're going to
2993 // generate a forwarding call.
2994 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
2995 PEnd = Method->param_end();
2996 P != PEnd;
2997 ++P) {
2998 if (!(*P)->getDeclName())
2999 return;
3000 }
3001
Douglas Gregor8987b232011-09-27 23:30:47 +00003002 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
Douglas Gregorf6961522010-08-27 21:18:54 +00003003 for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(),
3004 MEnd = Method->end_overridden_methods();
3005 M != MEnd; ++M) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003006 CodeCompletionBuilder Builder(Results.getAllocator(),
3007 Results.getCodeCompletionTUInfo());
Douglas Gregorf6961522010-08-27 21:18:54 +00003008 CXXMethodDecl *Overridden = const_cast<CXXMethodDecl *>(*M);
3009 if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl())
3010 continue;
3011
3012 // If we need a nested-name-specifier, add one now.
3013 if (!InContext) {
3014 NestedNameSpecifier *NNS
3015 = getRequiredQualification(S.Context, CurContext,
3016 Overridden->getDeclContext());
3017 if (NNS) {
3018 std::string Str;
3019 llvm::raw_string_ostream OS(Str);
Douglas Gregor8987b232011-09-27 23:30:47 +00003020 NNS->print(OS, Policy);
Douglas Gregordae68752011-02-01 22:57:45 +00003021 Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str()));
Douglas Gregorf6961522010-08-27 21:18:54 +00003022 }
3023 } else if (!InContext->Equals(Overridden->getDeclContext()))
3024 continue;
3025
Douglas Gregordae68752011-02-01 22:57:45 +00003026 Builder.AddTypedTextChunk(Results.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00003027 Overridden->getNameAsString()));
3028 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregorf6961522010-08-27 21:18:54 +00003029 bool FirstParam = true;
3030 for (CXXMethodDecl::param_iterator P = Method->param_begin(),
3031 PEnd = Method->param_end();
3032 P != PEnd; ++P) {
3033 if (FirstParam)
3034 FirstParam = false;
3035 else
Douglas Gregor218937c2011-02-01 19:23:04 +00003036 Builder.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregorf6961522010-08-27 21:18:54 +00003037
Douglas Gregordae68752011-02-01 22:57:45 +00003038 Builder.AddPlaceholderChunk(Results.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00003039 (*P)->getIdentifier()->getName()));
Douglas Gregorf6961522010-08-27 21:18:54 +00003040 }
Douglas Gregor218937c2011-02-01 19:23:04 +00003041 Builder.AddChunk(CodeCompletionString::CK_RightParen);
3042 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregorf6961522010-08-27 21:18:54 +00003043 CCP_SuperCompletion,
Douglas Gregorba103062012-03-27 23:34:16 +00003044 CXCursor_CXXMethod,
3045 CXAvailability_Available,
3046 Overridden));
Douglas Gregorf6961522010-08-27 21:18:54 +00003047 Results.Ignore(Overridden);
3048 }
3049}
3050
Douglas Gregorc5b2e582012-01-29 18:15:03 +00003051void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc,
3052 ModuleIdPath Path) {
3053 typedef CodeCompletionResult Result;
3054 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003055 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorc5b2e582012-01-29 18:15:03 +00003056 CodeCompletionContext::CCC_Other);
3057 Results.EnterNewScope();
3058
3059 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003060 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregorc5b2e582012-01-29 18:15:03 +00003061 typedef CodeCompletionResult Result;
3062 if (Path.empty()) {
3063 // Enumerate all top-level modules.
3064 llvm::SmallVector<Module *, 8> Modules;
3065 PP.getHeaderSearchInfo().collectAllModules(Modules);
3066 for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
3067 Builder.AddTypedTextChunk(
3068 Builder.getAllocator().CopyString(Modules[I]->Name));
3069 Results.AddResult(Result(Builder.TakeString(),
3070 CCP_Declaration,
3071 CXCursor_NotImplemented,
3072 Modules[I]->isAvailable()
3073 ? CXAvailability_Available
3074 : CXAvailability_NotAvailable));
3075 }
3076 } else {
3077 // Load the named module.
3078 Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path,
3079 Module::AllVisible,
3080 /*IsInclusionDirective=*/false);
3081 // Enumerate submodules.
3082 if (Mod) {
3083 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
3084 SubEnd = Mod->submodule_end();
3085 Sub != SubEnd; ++Sub) {
3086
3087 Builder.AddTypedTextChunk(
3088 Builder.getAllocator().CopyString((*Sub)->Name));
3089 Results.AddResult(Result(Builder.TakeString(),
3090 CCP_Declaration,
3091 CXCursor_NotImplemented,
3092 (*Sub)->isAvailable()
3093 ? CXAvailability_Available
3094 : CXAvailability_NotAvailable));
3095 }
3096 }
3097 }
3098 Results.ExitScope();
3099 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3100 Results.data(),Results.size());
3101}
3102
Douglas Gregor01dfea02010-01-10 23:08:15 +00003103void Sema::CodeCompleteOrdinaryName(Scope *S,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003104 ParserCompletionContext CompletionContext) {
John McCall0a2c5e22010-08-25 06:19:51 +00003105 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00003106 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003107 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00003108 mapCodeCompletionContext(*this, CompletionContext));
Douglas Gregorf6961522010-08-27 21:18:54 +00003109 Results.EnterNewScope();
Douglas Gregorcee9ff12010-09-20 22:39:41 +00003110
Douglas Gregor01dfea02010-01-10 23:08:15 +00003111 // Determine how to filter results, e.g., so that the names of
3112 // values (functions, enumerators, function templates, etc.) are
3113 // only allowed where we can have an expression.
3114 switch (CompletionContext) {
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003115 case PCC_Namespace:
3116 case PCC_Class:
3117 case PCC_ObjCInterface:
3118 case PCC_ObjCImplementation:
3119 case PCC_ObjCInstanceVariableList:
3120 case PCC_Template:
3121 case PCC_MemberTemplate:
Douglas Gregor72db1082010-08-24 01:11:00 +00003122 case PCC_Type:
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00003123 case PCC_LocalDeclarationSpecifiers:
Douglas Gregor01dfea02010-01-10 23:08:15 +00003124 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
3125 break;
3126
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003127 case PCC_Statement:
Douglas Gregor02688102010-09-14 23:59:36 +00003128 case PCC_ParenthesizedExpression:
Douglas Gregoreb0d0142010-08-24 23:58:17 +00003129 case PCC_Expression:
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003130 case PCC_ForInit:
3131 case PCC_Condition:
David Blaikie4e4d0842012-03-11 07:00:24 +00003132 if (WantTypesInContext(CompletionContext, getLangOpts()))
Douglas Gregor4710e5b2010-05-28 00:49:12 +00003133 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3134 else
3135 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
Douglas Gregorf6961522010-08-27 21:18:54 +00003136
David Blaikie4e4d0842012-03-11 07:00:24 +00003137 if (getLangOpts().CPlusPlus)
Douglas Gregorf6961522010-08-27 21:18:54 +00003138 MaybeAddOverrideCalls(*this, /*InContext=*/0, Results);
Douglas Gregor01dfea02010-01-10 23:08:15 +00003139 break;
Douglas Gregordc845342010-05-25 05:58:43 +00003140
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003141 case PCC_RecoveryInFunction:
Douglas Gregordc845342010-05-25 05:58:43 +00003142 // Unfiltered
3143 break;
Douglas Gregor01dfea02010-01-10 23:08:15 +00003144 }
3145
Douglas Gregor3cdee122010-08-26 16:36:48 +00003146 // If we are in a C++ non-static member function, check the qualifiers on
3147 // the member function to filter/prioritize the results list.
3148 if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext))
3149 if (CurMethod->isInstance())
3150 Results.setObjectTypeQualifiers(
3151 Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers()));
3152
Douglas Gregor1ca6ae82010-01-14 01:09:38 +00003153 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00003154 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3155 CodeCompleter->includeGlobals());
Douglas Gregor2a7925c2009-12-07 09:54:55 +00003156
Douglas Gregorbca403c2010-01-13 23:51:12 +00003157 AddOrdinaryNameResults(CompletionContext, S, *this, Results);
Douglas Gregor2a7925c2009-12-07 09:54:55 +00003158 Results.ExitScope();
3159
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00003160 switch (CompletionContext) {
Douglas Gregor02688102010-09-14 23:59:36 +00003161 case PCC_ParenthesizedExpression:
Douglas Gregor72db1082010-08-24 01:11:00 +00003162 case PCC_Expression:
3163 case PCC_Statement:
3164 case PCC_RecoveryInFunction:
3165 if (S->getFnParent())
David Blaikie4e4d0842012-03-11 07:00:24 +00003166 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregor72db1082010-08-24 01:11:00 +00003167 break;
3168
3169 case PCC_Namespace:
3170 case PCC_Class:
3171 case PCC_ObjCInterface:
3172 case PCC_ObjCImplementation:
3173 case PCC_ObjCInstanceVariableList:
3174 case PCC_Template:
3175 case PCC_MemberTemplate:
3176 case PCC_ForInit:
3177 case PCC_Condition:
3178 case PCC_Type:
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00003179 case PCC_LocalDeclarationSpecifiers:
Douglas Gregor72db1082010-08-24 01:11:00 +00003180 break;
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00003181 }
3182
Douglas Gregor0c8296d2009-11-07 00:00:49 +00003183 if (CodeCompleter->includeMacros())
Douglas Gregorbca403c2010-01-13 23:51:12 +00003184 AddMacroResults(PP, Results);
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00003185
Douglas Gregorcee9ff12010-09-20 22:39:41 +00003186 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003187 Results.data(),Results.size());
Douglas Gregor791215b2009-09-21 20:51:25 +00003188}
3189
Douglas Gregorc7b6d882010-09-16 15:14:18 +00003190static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
3191 ParsedType Receiver,
3192 IdentifierInfo **SelIdents,
3193 unsigned NumSelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00003194 bool AtArgumentExpression,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00003195 bool IsSuper,
3196 ResultBuilder &Results);
3197
3198void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS,
3199 bool AllowNonIdentifiers,
3200 bool AllowNestedNameSpecifiers) {
John McCall0a2c5e22010-08-25 06:19:51 +00003201 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00003202 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003203 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00003204 AllowNestedNameSpecifiers
3205 ? CodeCompletionContext::CCC_PotentiallyQualifiedName
3206 : CodeCompletionContext::CCC_Name);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003207 Results.EnterNewScope();
3208
3209 // Type qualifiers can come after names.
3210 Results.AddResult(Result("const"));
3211 Results.AddResult(Result("volatile"));
David Blaikie4e4d0842012-03-11 07:00:24 +00003212 if (getLangOpts().C99)
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003213 Results.AddResult(Result("restrict"));
3214
David Blaikie4e4d0842012-03-11 07:00:24 +00003215 if (getLangOpts().CPlusPlus) {
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003216 if (AllowNonIdentifiers) {
3217 Results.AddResult(Result("operator"));
3218 }
3219
3220 // Add nested-name-specifiers.
3221 if (AllowNestedNameSpecifiers) {
3222 Results.allowNestedNameSpecifiers();
Douglas Gregor52779fb2010-09-23 23:01:17 +00003223 Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003224 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3225 LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer,
3226 CodeCompleter->includeGlobals());
Douglas Gregor52779fb2010-09-23 23:01:17 +00003227 Results.setFilter(0);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003228 }
3229 }
3230 Results.ExitScope();
3231
Douglas Gregorc7b6d882010-09-16 15:14:18 +00003232 // If we're in a context where we might have an expression (rather than a
3233 // declaration), and what we've seen so far is an Objective-C type that could
3234 // be a receiver of a class message, this may be a class message send with
3235 // the initial opening bracket '[' missing. Add appropriate completions.
3236 if (AllowNonIdentifiers && !AllowNestedNameSpecifiers &&
3237 DS.getTypeSpecType() == DeclSpec::TST_typename &&
3238 DS.getStorageClassSpecAsWritten() == DeclSpec::SCS_unspecified &&
3239 !DS.isThreadSpecified() && !DS.isExternInLinkageSpec() &&
3240 DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified &&
3241 DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
3242 DS.getTypeQualifiers() == 0 &&
3243 S &&
3244 (S->getFlags() & Scope::DeclScope) != 0 &&
3245 (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope |
3246 Scope::FunctionPrototypeScope |
3247 Scope::AtCatchScope)) == 0) {
3248 ParsedType T = DS.getRepAsType();
3249 if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType())
Douglas Gregor70c5ac72010-09-20 23:34:21 +00003250 AddClassMessageCompletions(*this, S, T, 0, 0, false, false, Results);
Douglas Gregorc7b6d882010-09-16 15:14:18 +00003251 }
3252
Douglas Gregor4497dd42010-08-24 04:59:56 +00003253 // Note that we intentionally suppress macro results here, since we do not
3254 // encourage using macros to produce the names of entities.
3255
Douglas Gregor52779fb2010-09-23 23:01:17 +00003256 HandleCodeCompleteResults(this, CodeCompleter,
3257 Results.getCompletionContext(),
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003258 Results.data(), Results.size());
3259}
3260
Douglas Gregorfb629412010-08-23 21:17:50 +00003261struct Sema::CodeCompleteExpressionData {
3262 CodeCompleteExpressionData(QualType PreferredType = QualType())
3263 : PreferredType(PreferredType), IntegralConstantExpression(false),
3264 ObjCCollection(false) { }
3265
3266 QualType PreferredType;
3267 bool IntegralConstantExpression;
3268 bool ObjCCollection;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003269 SmallVector<Decl *, 4> IgnoreDecls;
Douglas Gregorfb629412010-08-23 21:17:50 +00003270};
3271
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003272/// \brief Perform code-completion in an expression context when we know what
3273/// type we're looking for.
Douglas Gregorfb629412010-08-23 21:17:50 +00003274void Sema::CodeCompleteExpression(Scope *S,
3275 const CodeCompleteExpressionData &Data) {
John McCall0a2c5e22010-08-25 06:19:51 +00003276 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00003277 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003278 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00003279 CodeCompletionContext::CCC_Expression);
Douglas Gregorfb629412010-08-23 21:17:50 +00003280 if (Data.ObjCCollection)
3281 Results.setFilter(&ResultBuilder::IsObjCCollection);
3282 else if (Data.IntegralConstantExpression)
Douglas Gregorf9578432010-07-28 21:50:18 +00003283 Results.setFilter(&ResultBuilder::IsIntegralConstantValue);
David Blaikie4e4d0842012-03-11 07:00:24 +00003284 else if (WantTypesInContext(PCC_Expression, getLangOpts()))
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003285 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3286 else
3287 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
Douglas Gregorfb629412010-08-23 21:17:50 +00003288
3289 if (!Data.PreferredType.isNull())
3290 Results.setPreferredType(Data.PreferredType.getNonReferenceType());
3291
3292 // Ignore any declarations that we were told that we don't care about.
3293 for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I)
3294 Results.Ignore(Data.IgnoreDecls[I]);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003295
3296 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00003297 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3298 CodeCompleter->includeGlobals());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003299
3300 Results.EnterNewScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003301 AddOrdinaryNameResults(PCC_Expression, S, *this, Results);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003302 Results.ExitScope();
3303
Douglas Gregor590c7d52010-07-08 20:55:51 +00003304 bool PreferredTypeIsPointer = false;
Douglas Gregorfb629412010-08-23 21:17:50 +00003305 if (!Data.PreferredType.isNull())
3306 PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType()
3307 || Data.PreferredType->isMemberPointerType()
3308 || Data.PreferredType->isBlockPointerType();
Douglas Gregor590c7d52010-07-08 20:55:51 +00003309
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00003310 if (S->getFnParent() &&
3311 !Data.ObjCCollection &&
3312 !Data.IntegralConstantExpression)
David Blaikie4e4d0842012-03-11 07:00:24 +00003313 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregoraa5f77b2010-08-23 21:54:33 +00003314
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003315 if (CodeCompleter->includeMacros())
Douglas Gregor590c7d52010-07-08 20:55:51 +00003316 AddMacroResults(PP, Results, PreferredTypeIsPointer);
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003317 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregorfb629412010-08-23 21:17:50 +00003318 CodeCompletionContext(CodeCompletionContext::CCC_Expression,
3319 Data.PreferredType),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003320 Results.data(),Results.size());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003321}
3322
Douglas Gregorac5fd842010-09-18 01:28:11 +00003323void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) {
3324 if (E.isInvalid())
3325 CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction);
David Blaikie4e4d0842012-03-11 07:00:24 +00003326 else if (getLangOpts().ObjC1)
Douglas Gregorac5fd842010-09-18 01:28:11 +00003327 CodeCompleteObjCInstanceMessage(S, E.take(), 0, 0, false);
Douglas Gregor78edf512010-09-15 16:23:04 +00003328}
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003329
Douglas Gregor73449212010-12-09 23:01:55 +00003330/// \brief The set of properties that have already been added, referenced by
3331/// property name.
3332typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet;
3333
Douglas Gregorb92a4082012-06-12 13:44:08 +00003334/// \brief Retrieve the container definition, if any?
3335static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) {
3336 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
3337 if (Interface->hasDefinition())
3338 return Interface->getDefinition();
3339
3340 return Interface;
3341 }
3342
3343 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3344 if (Protocol->hasDefinition())
3345 return Protocol->getDefinition();
3346
3347 return Protocol;
3348 }
3349 return Container;
3350}
3351
3352static void AddObjCProperties(ObjCContainerDecl *Container,
Douglas Gregor322328b2009-11-18 22:32:06 +00003353 bool AllowCategories,
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003354 bool AllowNullaryMethods,
Douglas Gregor95ac6552009-11-18 01:29:26 +00003355 DeclContext *CurContext,
Douglas Gregor73449212010-12-09 23:01:55 +00003356 AddedPropertiesSet &AddedProperties,
Douglas Gregor95ac6552009-11-18 01:29:26 +00003357 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00003358 typedef CodeCompletionResult Result;
Douglas Gregor95ac6552009-11-18 01:29:26 +00003359
Douglas Gregorb92a4082012-06-12 13:44:08 +00003360 // Retrieve the definition.
3361 Container = getContainerDef(Container);
3362
Douglas Gregor95ac6552009-11-18 01:29:26 +00003363 // Add properties in this container.
3364 for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(),
3365 PEnd = Container->prop_end();
3366 P != PEnd;
Douglas Gregor73449212010-12-09 23:01:55 +00003367 ++P) {
3368 if (AddedProperties.insert(P->getIdentifier()))
David Blaikie581deb32012-06-06 20:45:41 +00003369 Results.MaybeAddResult(Result(*P, 0), CurContext);
Douglas Gregor73449212010-12-09 23:01:55 +00003370 }
Douglas Gregor95ac6552009-11-18 01:29:26 +00003371
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003372 // Add nullary methods
3373 if (AllowNullaryMethods) {
3374 ASTContext &Context = Container->getASTContext();
Douglas Gregor8987b232011-09-27 23:30:47 +00003375 PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003376 for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
3377 MEnd = Container->meth_end();
3378 M != MEnd; ++M) {
3379 if (M->getSelector().isUnarySelector())
3380 if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0))
3381 if (AddedProperties.insert(Name)) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003382 CodeCompletionBuilder Builder(Results.getAllocator(),
3383 Results.getCodeCompletionTUInfo());
David Blaikie581deb32012-06-06 20:45:41 +00003384 AddResultTypeChunk(Context, Policy, *M, Builder);
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003385 Builder.AddTypedTextChunk(
3386 Results.getAllocator().CopyString(Name->getName()));
3387
David Blaikie581deb32012-06-06 20:45:41 +00003388 Results.MaybeAddResult(Result(Builder.TakeString(), *M,
Douglas Gregorba103062012-03-27 23:34:16 +00003389 CCP_MemberDeclaration + CCD_MethodAsProperty),
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003390 CurContext);
3391 }
3392 }
3393 }
3394
3395
Douglas Gregor95ac6552009-11-18 01:29:26 +00003396 // Add properties in referenced protocols.
3397 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3398 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
3399 PEnd = Protocol->protocol_end();
3400 P != PEnd; ++P)
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003401 AddObjCProperties(*P, AllowCategories, AllowNullaryMethods, CurContext,
3402 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003403 } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){
Douglas Gregor322328b2009-11-18 22:32:06 +00003404 if (AllowCategories) {
3405 // Look through categories.
3406 for (ObjCCategoryDecl *Category = IFace->getCategoryList();
3407 Category; Category = Category->getNextClassCategory())
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003408 AddObjCProperties(Category, AllowCategories, AllowNullaryMethods,
3409 CurContext, AddedProperties, Results);
Douglas Gregor322328b2009-11-18 22:32:06 +00003410 }
Douglas Gregor95ac6552009-11-18 01:29:26 +00003411
3412 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00003413 for (ObjCInterfaceDecl::all_protocol_iterator
3414 I = IFace->all_referenced_protocol_begin(),
3415 E = IFace->all_referenced_protocol_end(); I != E; ++I)
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003416 AddObjCProperties(*I, AllowCategories, AllowNullaryMethods, CurContext,
3417 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003418
3419 // Look in the superclass.
3420 if (IFace->getSuperClass())
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003421 AddObjCProperties(IFace->getSuperClass(), AllowCategories,
3422 AllowNullaryMethods, CurContext,
Douglas Gregor73449212010-12-09 23:01:55 +00003423 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003424 } else if (const ObjCCategoryDecl *Category
3425 = dyn_cast<ObjCCategoryDecl>(Container)) {
3426 // Look through protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00003427 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
3428 PEnd = Category->protocol_end();
Douglas Gregor95ac6552009-11-18 01:29:26 +00003429 P != PEnd; ++P)
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003430 AddObjCProperties(*P, AllowCategories, AllowNullaryMethods, CurContext,
3431 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003432 }
3433}
3434
Douglas Gregorf5cd27d2012-01-23 15:59:30 +00003435void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base,
Douglas Gregor81b747b2009-09-17 21:32:03 +00003436 SourceLocation OpLoc,
3437 bool IsArrow) {
Douglas Gregorf5cd27d2012-01-23 15:59:30 +00003438 if (!Base || !CodeCompleter)
Douglas Gregor81b747b2009-09-17 21:32:03 +00003439 return;
3440
Douglas Gregorf5cd27d2012-01-23 15:59:30 +00003441 ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow);
3442 if (ConvertedBase.isInvalid())
3443 return;
3444 Base = ConvertedBase.get();
3445
John McCall0a2c5e22010-08-25 06:19:51 +00003446 typedef CodeCompletionResult Result;
Douglas Gregor86d9a522009-09-21 16:56:56 +00003447
Douglas Gregor81b747b2009-09-17 21:32:03 +00003448 QualType BaseType = Base->getType();
Douglas Gregor86d9a522009-09-21 16:56:56 +00003449
3450 if (IsArrow) {
3451 if (const PointerType *Ptr = BaseType->getAs<PointerType>())
3452 BaseType = Ptr->getPointeeType();
3453 else if (BaseType->isObjCObjectPointerType())
Douglas Gregor3cdee122010-08-26 16:36:48 +00003454 /*Do nothing*/ ;
Douglas Gregor86d9a522009-09-21 16:56:56 +00003455 else
3456 return;
3457 }
3458
Douglas Gregor3da626b2011-07-07 16:03:39 +00003459 enum CodeCompletionContext::Kind contextKind;
3460
3461 if (IsArrow) {
3462 contextKind = CodeCompletionContext::CCC_ArrowMemberAccess;
3463 }
3464 else {
3465 if (BaseType->isObjCObjectPointerType() ||
3466 BaseType->isObjCObjectOrInterfaceType()) {
3467 contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess;
3468 }
3469 else {
3470 contextKind = CodeCompletionContext::CCC_DotMemberAccess;
3471 }
3472 }
3473
Douglas Gregor218937c2011-02-01 19:23:04 +00003474 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003475 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor3da626b2011-07-07 16:03:39 +00003476 CodeCompletionContext(contextKind,
Douglas Gregor52779fb2010-09-23 23:01:17 +00003477 BaseType),
3478 &ResultBuilder::IsMember);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003479 Results.EnterNewScope();
3480 if (const RecordType *Record = BaseType->getAs<RecordType>()) {
Douglas Gregor3cdee122010-08-26 16:36:48 +00003481 // Indicate that we are performing a member access, and the cv-qualifiers
3482 // for the base object type.
3483 Results.setObjectTypeQualifiers(BaseType.getQualifiers());
3484
Douglas Gregor95ac6552009-11-18 01:29:26 +00003485 // Access to a C/C++ class, struct, or union.
Douglas Gregor45bcd432010-01-14 03:21:49 +00003486 Results.allowNestedNameSpecifiers();
Douglas Gregor0cc84042010-01-14 15:47:35 +00003487 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00003488 LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer,
3489 CodeCompleter->includeGlobals());
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003490
David Blaikie4e4d0842012-03-11 07:00:24 +00003491 if (getLangOpts().CPlusPlus) {
Douglas Gregor95ac6552009-11-18 01:29:26 +00003492 if (!Results.empty()) {
3493 // The "template" keyword can follow "->" or "." in the grammar.
3494 // However, we only want to suggest the template keyword if something
3495 // is dependent.
3496 bool IsDependent = BaseType->isDependentType();
3497 if (!IsDependent) {
3498 for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent())
3499 if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) {
3500 IsDependent = Ctx->isDependentContext();
3501 break;
3502 }
3503 }
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003504
Douglas Gregor95ac6552009-11-18 01:29:26 +00003505 if (IsDependent)
Douglas Gregora4477812010-01-14 16:01:26 +00003506 Results.AddResult(Result("template"));
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003507 }
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003508 }
Douglas Gregor95ac6552009-11-18 01:29:26 +00003509 } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) {
3510 // Objective-C property reference.
Douglas Gregor73449212010-12-09 23:01:55 +00003511 AddedPropertiesSet AddedProperties;
Douglas Gregor95ac6552009-11-18 01:29:26 +00003512
3513 // Add property results based on our interface.
3514 const ObjCObjectPointerType *ObjCPtr
3515 = BaseType->getAsObjCInterfacePointerType();
3516 assert(ObjCPtr && "Non-NULL pointer guaranteed above!");
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003517 AddObjCProperties(ObjCPtr->getInterfaceDecl(), true,
3518 /*AllowNullaryMethods=*/true, CurContext,
Douglas Gregor73449212010-12-09 23:01:55 +00003519 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003520
3521 // Add properties from the protocols in a qualified interface.
3522 for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(),
3523 E = ObjCPtr->qual_end();
3524 I != E; ++I)
Douglas Gregor4b81cde2011-05-05 15:50:42 +00003525 AddObjCProperties(*I, true, /*AllowNullaryMethods=*/true, CurContext,
3526 AddedProperties, Results);
Douglas Gregor95ac6552009-11-18 01:29:26 +00003527 } else if ((IsArrow && BaseType->isObjCObjectPointerType()) ||
John McCallc12c5bb2010-05-15 11:32:37 +00003528 (!IsArrow && BaseType->isObjCObjectType())) {
Douglas Gregor95ac6552009-11-18 01:29:26 +00003529 // Objective-C instance variable access.
3530 ObjCInterfaceDecl *Class = 0;
3531 if (const ObjCObjectPointerType *ObjCPtr
3532 = BaseType->getAs<ObjCObjectPointerType>())
3533 Class = ObjCPtr->getInterfaceDecl();
3534 else
John McCallc12c5bb2010-05-15 11:32:37 +00003535 Class = BaseType->getAs<ObjCObjectType>()->getInterface();
Douglas Gregor95ac6552009-11-18 01:29:26 +00003536
3537 // Add all ivars from this class and its superclasses.
Douglas Gregor80f4f4c2010-01-14 16:08:12 +00003538 if (Class) {
3539 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3540 Results.setFilter(&ResultBuilder::IsObjCIvar);
Douglas Gregor8071e422010-08-15 06:18:01 +00003541 LookupVisibleDecls(Class, LookupMemberName, Consumer,
3542 CodeCompleter->includeGlobals());
Douglas Gregor95ac6552009-11-18 01:29:26 +00003543 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00003544 }
Douglas Gregor95ac6552009-11-18 01:29:26 +00003545
3546 // FIXME: How do we cope with isa?
3547
3548 Results.ExitScope();
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003549
Daniel Dunbar3a2838d2009-11-13 08:58:20 +00003550 // Hand off the results found for code completion.
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003551 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor52779fb2010-09-23 23:01:17 +00003552 Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003553 Results.data(),Results.size());
Douglas Gregor81b747b2009-09-17 21:32:03 +00003554}
3555
Douglas Gregor374929f2009-09-18 15:37:17 +00003556void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) {
3557 if (!CodeCompleter)
3558 return;
3559
John McCall0a2c5e22010-08-25 06:19:51 +00003560 typedef CodeCompletionResult Result;
Douglas Gregor86d9a522009-09-21 16:56:56 +00003561 ResultBuilder::LookupFilter Filter = 0;
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003562 enum CodeCompletionContext::Kind ContextKind
3563 = CodeCompletionContext::CCC_Other;
Douglas Gregor374929f2009-09-18 15:37:17 +00003564 switch ((DeclSpec::TST)TagSpec) {
3565 case DeclSpec::TST_enum:
Douglas Gregor86d9a522009-09-21 16:56:56 +00003566 Filter = &ResultBuilder::IsEnum;
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003567 ContextKind = CodeCompletionContext::CCC_EnumTag;
Douglas Gregor374929f2009-09-18 15:37:17 +00003568 break;
3569
3570 case DeclSpec::TST_union:
Douglas Gregor86d9a522009-09-21 16:56:56 +00003571 Filter = &ResultBuilder::IsUnion;
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003572 ContextKind = CodeCompletionContext::CCC_UnionTag;
Douglas Gregor374929f2009-09-18 15:37:17 +00003573 break;
3574
3575 case DeclSpec::TST_struct:
Douglas Gregor374929f2009-09-18 15:37:17 +00003576 case DeclSpec::TST_class:
Douglas Gregor86d9a522009-09-21 16:56:56 +00003577 Filter = &ResultBuilder::IsClassOrStruct;
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003578 ContextKind = CodeCompletionContext::CCC_ClassOrStructTag;
Douglas Gregor374929f2009-09-18 15:37:17 +00003579 break;
3580
3581 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003582 llvm_unreachable("Unknown type specifier kind in CodeCompleteTag");
Douglas Gregor374929f2009-09-18 15:37:17 +00003583 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00003584
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003585 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3586 CodeCompleter->getCodeCompletionTUInfo(), ContextKind);
Douglas Gregor5d2fc402010-01-14 03:27:13 +00003587 CodeCompletionDeclConsumer Consumer(Results, CurContext);
John McCall0d6b1642010-04-23 18:46:30 +00003588
3589 // First pass: look for tags.
3590 Results.setFilter(Filter);
Douglas Gregor8071e422010-08-15 06:18:01 +00003591 LookupVisibleDecls(S, LookupTagName, Consumer,
3592 CodeCompleter->includeGlobals());
John McCall0d6b1642010-04-23 18:46:30 +00003593
Douglas Gregor8071e422010-08-15 06:18:01 +00003594 if (CodeCompleter->includeGlobals()) {
3595 // Second pass: look for nested name specifiers.
3596 Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
3597 LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer);
3598 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00003599
Douglas Gregor52779fb2010-09-23 23:01:17 +00003600 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003601 Results.data(),Results.size());
Douglas Gregor374929f2009-09-18 15:37:17 +00003602}
3603
Douglas Gregor1a480c42010-08-27 17:35:51 +00003604void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) {
Douglas Gregor218937c2011-02-01 19:23:04 +00003605 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003606 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00003607 CodeCompletionContext::CCC_TypeQualifiers);
Douglas Gregor1a480c42010-08-27 17:35:51 +00003608 Results.EnterNewScope();
3609 if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const))
3610 Results.AddResult("const");
3611 if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile))
3612 Results.AddResult("volatile");
David Blaikie4e4d0842012-03-11 07:00:24 +00003613 if (getLangOpts().C99 &&
Douglas Gregor1a480c42010-08-27 17:35:51 +00003614 !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict))
3615 Results.AddResult("restrict");
3616 Results.ExitScope();
3617 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor52779fb2010-09-23 23:01:17 +00003618 Results.getCompletionContext(),
Douglas Gregor1a480c42010-08-27 17:35:51 +00003619 Results.data(), Results.size());
3620}
3621
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003622void Sema::CodeCompleteCase(Scope *S) {
John McCall781472f2010-08-25 08:40:02 +00003623 if (getCurFunction()->SwitchStack.empty() || !CodeCompleter)
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003624 return;
John McCalla8e0cd82011-08-06 07:30:58 +00003625
John McCall781472f2010-08-25 08:40:02 +00003626 SwitchStmt *Switch = getCurFunction()->SwitchStack.back();
John McCalla8e0cd82011-08-06 07:30:58 +00003627 QualType type = Switch->getCond()->IgnoreImplicit()->getType();
3628 if (!type->isEnumeralType()) {
3629 CodeCompleteExpressionData Data(type);
Douglas Gregorfb629412010-08-23 21:17:50 +00003630 Data.IntegralConstantExpression = true;
3631 CodeCompleteExpression(S, Data);
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003632 return;
Douglas Gregorf9578432010-07-28 21:50:18 +00003633 }
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003634
3635 // Code-complete the cases of a switch statement over an enumeration type
3636 // by providing the list of
John McCalla8e0cd82011-08-06 07:30:58 +00003637 EnumDecl *Enum = type->castAs<EnumType>()->getDecl();
Douglas Gregorb92a4082012-06-12 13:44:08 +00003638 if (EnumDecl *Def = Enum->getDefinition())
3639 Enum = Def;
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003640
3641 // Determine which enumerators we have already seen in the switch statement.
3642 // FIXME: Ideally, we would also be able to look *past* the code-completion
3643 // token, in case we are code-completing in the middle of the switch and not
3644 // at the end. However, we aren't able to do so at the moment.
3645 llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen;
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00003646 NestedNameSpecifier *Qualifier = 0;
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003647 for (SwitchCase *SC = Switch->getSwitchCaseList(); SC;
3648 SC = SC->getNextSwitchCase()) {
3649 CaseStmt *Case = dyn_cast<CaseStmt>(SC);
3650 if (!Case)
3651 continue;
3652
3653 Expr *CaseVal = Case->getLHS()->IgnoreParenCasts();
3654 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal))
3655 if (EnumConstantDecl *Enumerator
3656 = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
3657 // We look into the AST of the case statement to determine which
3658 // enumerator was named. Alternatively, we could compute the value of
3659 // the integral constant expression, then compare it against the
3660 // values of each enumerator. However, value-based approach would not
3661 // work as well with C++ templates where enumerators declared within a
3662 // template are type- and value-dependent.
3663 EnumeratorsSeen.insert(Enumerator);
3664
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00003665 // If this is a qualified-id, keep track of the nested-name-specifier
3666 // so that we can reproduce it as part of code completion, e.g.,
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003667 //
3668 // switch (TagD.getKind()) {
3669 // case TagDecl::TK_enum:
3670 // break;
3671 // case XXX
3672 //
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00003673 // At the XXX, our completions are TagDecl::TK_union,
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003674 // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union,
3675 // TK_struct, and TK_class.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003676 Qualifier = DRE->getQualifier();
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003677 }
3678 }
3679
David Blaikie4e4d0842012-03-11 07:00:24 +00003680 if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) {
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00003681 // If there are no prior enumerators in C++, check whether we have to
3682 // qualify the names of the enumerators that we suggest, because they
3683 // may not be visible in this scope.
Douglas Gregorb223d8c2012-02-01 05:02:47 +00003684 Qualifier = getRequiredQualification(Context, CurContext, Enum);
Douglas Gregorb9d0ef72009-09-21 19:57:38 +00003685 }
3686
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003687 // Add any enumerators that have not yet been mentioned.
Douglas Gregor218937c2011-02-01 19:23:04 +00003688 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003689 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00003690 CodeCompletionContext::CCC_Expression);
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003691 Results.EnterNewScope();
3692 for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(),
3693 EEnd = Enum->enumerator_end();
3694 E != EEnd; ++E) {
David Blaikie581deb32012-06-06 20:45:41 +00003695 if (EnumeratorsSeen.count(*E))
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003696 continue;
3697
David Blaikie581deb32012-06-06 20:45:41 +00003698 CodeCompletionResult R(*E, Qualifier);
Douglas Gregor5c722c702011-02-18 23:30:37 +00003699 R.Priority = CCP_EnumInCase;
3700 Results.AddResult(R, CurContext, 0, false);
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003701 }
3702 Results.ExitScope();
Douglas Gregor2f880e42010-04-06 20:02:15 +00003703
Douglas Gregor3da626b2011-07-07 16:03:39 +00003704 //We need to make sure we're setting the right context,
3705 //so only say we include macros if the code completer says we do
3706 enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other;
3707 if (CodeCompleter->includeMacros()) {
Douglas Gregorbca403c2010-01-13 23:51:12 +00003708 AddMacroResults(PP, Results);
Douglas Gregor3da626b2011-07-07 16:03:39 +00003709 kind = CodeCompletionContext::CCC_OtherWithMacros;
3710 }
3711
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003712 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor3da626b2011-07-07 16:03:39 +00003713 kind,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003714 Results.data(),Results.size());
Douglas Gregor3e1005f2009-09-21 18:10:23 +00003715}
3716
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003717namespace {
3718 struct IsBetterOverloadCandidate {
3719 Sema &S;
John McCall5769d612010-02-08 23:07:23 +00003720 SourceLocation Loc;
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003721
3722 public:
John McCall5769d612010-02-08 23:07:23 +00003723 explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc)
3724 : S(S), Loc(Loc) { }
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003725
3726 bool
3727 operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const {
John McCall120d63c2010-08-24 20:38:10 +00003728 return isBetterOverloadCandidate(S, X, Y, Loc);
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003729 }
3730 };
3731}
3732
Ahmed Charles13a140c2012-02-25 11:00:22 +00003733static bool anyNullArguments(llvm::ArrayRef<Expr*> Args) {
3734 if (Args.size() && !Args.data())
Douglas Gregord28dcd72010-05-30 06:10:08 +00003735 return true;
Ahmed Charles13a140c2012-02-25 11:00:22 +00003736
3737 for (unsigned I = 0; I != Args.size(); ++I)
Douglas Gregord28dcd72010-05-30 06:10:08 +00003738 if (!Args[I])
3739 return true;
Ahmed Charles13a140c2012-02-25 11:00:22 +00003740
Douglas Gregord28dcd72010-05-30 06:10:08 +00003741 return false;
3742}
3743
Richard Trieuf81e5a92011-09-09 02:00:50 +00003744void Sema::CodeCompleteCall(Scope *S, Expr *FnIn,
Ahmed Charles13a140c2012-02-25 11:00:22 +00003745 llvm::ArrayRef<Expr *> Args) {
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003746 if (!CodeCompleter)
3747 return;
Douglas Gregoref96eac2009-12-11 19:06:04 +00003748
3749 // When we're code-completing for a call, we fall back to ordinary
3750 // name code-completion whenever we can't produce specific
3751 // results. We may want to revisit this strategy in the future,
3752 // e.g., by merging the two kinds of results.
3753
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003754 Expr *Fn = (Expr *)FnIn;
Douglas Gregoref96eac2009-12-11 19:06:04 +00003755
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003756 // Ignore type-dependent call expressions entirely.
Ahmed Charles13a140c2012-02-25 11:00:22 +00003757 if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) ||
3758 Expr::hasAnyTypeDependentArguments(Args)) {
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003759 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003760 return;
Douglas Gregoref96eac2009-12-11 19:06:04 +00003761 }
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003762
John McCall3b4294e2009-12-16 12:17:52 +00003763 // Build an overload candidate set based on the functions we find.
John McCall5769d612010-02-08 23:07:23 +00003764 SourceLocation Loc = Fn->getExprLoc();
3765 OverloadCandidateSet CandidateSet(Loc);
John McCall3b4294e2009-12-16 12:17:52 +00003766
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003767 // FIXME: What if we're calling something that isn't a function declaration?
3768 // FIXME: What if we're calling a pseudo-destructor?
3769 // FIXME: What if we're calling a member function?
3770
Douglas Gregorc0265402010-01-21 15:46:19 +00003771 typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003772 SmallVector<ResultCandidate, 8> Results;
Douglas Gregorc0265402010-01-21 15:46:19 +00003773
John McCall3b4294e2009-12-16 12:17:52 +00003774 Expr *NakedFn = Fn->IgnoreParenCasts();
3775 if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn))
Ahmed Charles13a140c2012-02-25 11:00:22 +00003776 AddOverloadedCallCandidates(ULE, Args, CandidateSet,
John McCall3b4294e2009-12-16 12:17:52 +00003777 /*PartialOverloading=*/ true);
3778 else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) {
3779 FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
Douglas Gregorc0265402010-01-21 15:46:19 +00003780 if (FDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003781 if (!getLangOpts().CPlusPlus ||
Douglas Gregord28dcd72010-05-30 06:10:08 +00003782 !FDecl->getType()->getAs<FunctionProtoType>())
Douglas Gregorc0265402010-01-21 15:46:19 +00003783 Results.push_back(ResultCandidate(FDecl));
3784 else
John McCall86820f52010-01-26 01:37:31 +00003785 // FIXME: access?
Ahmed Charles13a140c2012-02-25 11:00:22 +00003786 AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), Args,
3787 CandidateSet, false, /*PartialOverloading*/true);
Douglas Gregorc0265402010-01-21 15:46:19 +00003788 }
John McCall3b4294e2009-12-16 12:17:52 +00003789 }
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003790
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003791 QualType ParamType;
3792
Douglas Gregorc0265402010-01-21 15:46:19 +00003793 if (!CandidateSet.empty()) {
3794 // Sort the overload candidate set by placing the best overloads first.
3795 std::stable_sort(CandidateSet.begin(), CandidateSet.end(),
John McCall5769d612010-02-08 23:07:23 +00003796 IsBetterOverloadCandidate(*this, Loc));
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003797
Douglas Gregorc0265402010-01-21 15:46:19 +00003798 // Add the remaining viable overload candidates as code-completion reslults.
3799 for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
3800 CandEnd = CandidateSet.end();
3801 Cand != CandEnd; ++Cand) {
3802 if (Cand->Viable)
3803 Results.push_back(ResultCandidate(Cand->Function));
3804 }
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003805
3806 // From the viable candidates, try to determine the type of this parameter.
3807 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
3808 if (const FunctionType *FType = Results[I].getFunctionType())
3809 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FType))
Ahmed Charles13a140c2012-02-25 11:00:22 +00003810 if (Args.size() < Proto->getNumArgs()) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003811 if (ParamType.isNull())
Ahmed Charles13a140c2012-02-25 11:00:22 +00003812 ParamType = Proto->getArgType(Args.size());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003813 else if (!Context.hasSameUnqualifiedType(
3814 ParamType.getNonReferenceType(),
Ahmed Charles13a140c2012-02-25 11:00:22 +00003815 Proto->getArgType(Args.size()).getNonReferenceType())) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003816 ParamType = QualType();
3817 break;
3818 }
3819 }
3820 }
3821 } else {
3822 // Try to determine the parameter type from the type of the expression
3823 // being called.
3824 QualType FunctionType = Fn->getType();
3825 if (const PointerType *Ptr = FunctionType->getAs<PointerType>())
3826 FunctionType = Ptr->getPointeeType();
3827 else if (const BlockPointerType *BlockPtr
3828 = FunctionType->getAs<BlockPointerType>())
3829 FunctionType = BlockPtr->getPointeeType();
3830 else if (const MemberPointerType *MemPtr
3831 = FunctionType->getAs<MemberPointerType>())
3832 FunctionType = MemPtr->getPointeeType();
3833
3834 if (const FunctionProtoType *Proto
3835 = FunctionType->getAs<FunctionProtoType>()) {
Ahmed Charles13a140c2012-02-25 11:00:22 +00003836 if (Args.size() < Proto->getNumArgs())
3837 ParamType = Proto->getArgType(Args.size());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003838 }
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003839 }
Douglas Gregoref96eac2009-12-11 19:06:04 +00003840
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003841 if (ParamType.isNull())
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003842 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003843 else
3844 CodeCompleteExpression(S, ParamType);
3845
Douglas Gregor2e4c7a52010-04-06 20:19:47 +00003846 if (!Results.empty())
Ahmed Charles13a140c2012-02-25 11:00:22 +00003847 CodeCompleter->ProcessOverloadCandidates(*this, Args.size(), Results.data(),
Douglas Gregoref96eac2009-12-11 19:06:04 +00003848 Results.size());
Douglas Gregor9c6a0e92009-09-22 15:41:20 +00003849}
3850
John McCalld226f652010-08-21 09:40:31 +00003851void Sema::CodeCompleteInitializer(Scope *S, Decl *D) {
3852 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003853 if (!VD) {
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003854 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003855 return;
3856 }
3857
3858 CodeCompleteExpression(S, VD->getType());
3859}
3860
3861void Sema::CodeCompleteReturn(Scope *S) {
3862 QualType ResultType;
3863 if (isa<BlockDecl>(CurContext)) {
3864 if (BlockScopeInfo *BSI = getCurBlock())
3865 ResultType = BSI->ReturnType;
3866 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext))
3867 ResultType = Function->getResultType();
3868 else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext))
3869 ResultType = Method->getResultType();
3870
3871 if (ResultType.isNull())
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003872 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003873 else
3874 CodeCompleteExpression(S, ResultType);
3875}
3876
Douglas Gregord2d8be62011-07-30 08:36:53 +00003877void Sema::CodeCompleteAfterIf(Scope *S) {
3878 typedef CodeCompletionResult Result;
3879 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003880 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregord2d8be62011-07-30 08:36:53 +00003881 mapCodeCompletionContext(*this, PCC_Statement));
3882 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3883 Results.EnterNewScope();
3884
3885 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3886 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3887 CodeCompleter->includeGlobals());
3888
3889 AddOrdinaryNameResults(PCC_Statement, S, *this, Results);
3890
3891 // "else" block
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003892 CodeCompletionBuilder Builder(Results.getAllocator(),
3893 Results.getCodeCompletionTUInfo());
Douglas Gregord2d8be62011-07-30 08:36:53 +00003894 Builder.AddTypedTextChunk("else");
Douglas Gregorf11641a2012-02-16 17:49:04 +00003895 if (Results.includeCodePatterns()) {
3896 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3897 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
3898 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3899 Builder.AddPlaceholderChunk("statements");
3900 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3901 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
3902 }
Douglas Gregord2d8be62011-07-30 08:36:53 +00003903 Results.AddResult(Builder.TakeString());
3904
3905 // "else if" block
3906 Builder.AddTypedTextChunk("else");
3907 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3908 Builder.AddTextChunk("if");
3909 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3910 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikie4e4d0842012-03-11 07:00:24 +00003911 if (getLangOpts().CPlusPlus)
Douglas Gregord2d8be62011-07-30 08:36:53 +00003912 Builder.AddPlaceholderChunk("condition");
3913 else
3914 Builder.AddPlaceholderChunk("expression");
3915 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorf11641a2012-02-16 17:49:04 +00003916 if (Results.includeCodePatterns()) {
3917 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3918 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
3919 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3920 Builder.AddPlaceholderChunk("statements");
3921 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3922 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
3923 }
Douglas Gregord2d8be62011-07-30 08:36:53 +00003924 Results.AddResult(Builder.TakeString());
3925
3926 Results.ExitScope();
3927
3928 if (S->getFnParent())
David Blaikie4e4d0842012-03-11 07:00:24 +00003929 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregord2d8be62011-07-30 08:36:53 +00003930
3931 if (CodeCompleter->includeMacros())
3932 AddMacroResults(PP, Results);
3933
3934 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3935 Results.data(),Results.size());
3936}
3937
Richard Trieuf81e5a92011-09-09 02:00:50 +00003938void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003939 if (LHS)
3940 CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType());
3941 else
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003942 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00003943}
3944
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003945void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS,
Douglas Gregor81b747b2009-09-17 21:32:03 +00003946 bool EnteringContext) {
3947 if (!SS.getScopeRep() || !CodeCompleter)
3948 return;
3949
Douglas Gregor86d9a522009-09-21 16:56:56 +00003950 DeclContext *Ctx = computeDeclContext(SS, EnteringContext);
3951 if (!Ctx)
3952 return;
Douglas Gregord1cd31a2009-12-11 18:28:39 +00003953
3954 // Try to instantiate any non-dependent declaration contexts before
3955 // we look in them.
John McCall77bb1aa2010-05-01 00:40:08 +00003956 if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx))
Douglas Gregord1cd31a2009-12-11 18:28:39 +00003957 return;
3958
Douglas Gregor218937c2011-02-01 19:23:04 +00003959 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003960 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00003961 CodeCompletionContext::CCC_Name);
Douglas Gregorf6961522010-08-27 21:18:54 +00003962 Results.EnterNewScope();
Douglas Gregor52779fb2010-09-23 23:01:17 +00003963
Douglas Gregor86d9a522009-09-21 16:56:56 +00003964 // The "template" keyword can follow "::" in the grammar, but only
3965 // put it into the grammar if the nested-name-specifier is dependent.
3966 NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
3967 if (!Results.empty() && NNS->isDependent())
Douglas Gregora4477812010-01-14 16:01:26 +00003968 Results.AddResult("template");
Douglas Gregorf6961522010-08-27 21:18:54 +00003969
3970 // Add calls to overridden virtual functions, if there are any.
3971 //
3972 // FIXME: This isn't wonderful, because we don't know whether we're actually
3973 // in a context that permits expressions. This is a general issue with
3974 // qualified-id completions.
3975 if (!EnteringContext)
3976 MaybeAddOverrideCalls(*this, Ctx, Results);
3977 Results.ExitScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00003978
Douglas Gregorf6961522010-08-27 21:18:54 +00003979 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3980 LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer);
3981
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003982 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor430d7a12011-07-25 17:48:11 +00003983 Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00003984 Results.data(),Results.size());
Douglas Gregor81b747b2009-09-17 21:32:03 +00003985}
Douglas Gregor49f40bd2009-09-18 19:03:04 +00003986
3987void Sema::CodeCompleteUsing(Scope *S) {
3988 if (!CodeCompleter)
3989 return;
3990
Douglas Gregor218937c2011-02-01 19:23:04 +00003991 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00003992 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00003993 CodeCompletionContext::CCC_PotentiallyQualifiedName,
3994 &ResultBuilder::IsNestedNameSpecifier);
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00003995 Results.EnterNewScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00003996
3997 // If we aren't in class scope, we could see the "namespace" keyword.
3998 if (!S->isClassScope())
John McCall0a2c5e22010-08-25 06:19:51 +00003999 Results.AddResult(CodeCompletionResult("namespace"));
Douglas Gregor86d9a522009-09-21 16:56:56 +00004000
4001 // After "using", we can see anything that would start a
4002 // nested-name-specifier.
Douglas Gregor5d2fc402010-01-14 03:27:13 +00004003 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00004004 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4005 CodeCompleter->includeGlobals());
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004006 Results.ExitScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004007
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004008 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004009 CodeCompletionContext::CCC_PotentiallyQualifiedName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004010 Results.data(),Results.size());
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004011}
4012
4013void Sema::CodeCompleteUsingDirective(Scope *S) {
4014 if (!CodeCompleter)
4015 return;
4016
Douglas Gregor86d9a522009-09-21 16:56:56 +00004017 // After "using namespace", we expect to see a namespace name or namespace
4018 // alias.
Douglas Gregor218937c2011-02-01 19:23:04 +00004019 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004020 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004021 CodeCompletionContext::CCC_Namespace,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004022 &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004023 Results.EnterNewScope();
Douglas Gregor5d2fc402010-01-14 03:27:13 +00004024 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00004025 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4026 CodeCompleter->includeGlobals());
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004027 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004028 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor8071e422010-08-15 06:18:01 +00004029 CodeCompletionContext::CCC_Namespace,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004030 Results.data(),Results.size());
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004031}
4032
4033void Sema::CodeCompleteNamespaceDecl(Scope *S) {
4034 if (!CodeCompleter)
4035 return;
4036
Douglas Gregor86d9a522009-09-21 16:56:56 +00004037 DeclContext *Ctx = (DeclContext *)S->getEntity();
4038 if (!S->getParent())
4039 Ctx = Context.getTranslationUnitDecl();
4040
Douglas Gregor52779fb2010-09-23 23:01:17 +00004041 bool SuppressedGlobalResults
4042 = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx);
4043
Douglas Gregor218937c2011-02-01 19:23:04 +00004044 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004045 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00004046 SuppressedGlobalResults
4047 ? CodeCompletionContext::CCC_Namespace
4048 : CodeCompletionContext::CCC_Other,
4049 &ResultBuilder::IsNamespace);
4050
4051 if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) {
Douglas Gregor86d9a522009-09-21 16:56:56 +00004052 // We only want to see those namespaces that have already been defined
4053 // within this scope, because its likely that the user is creating an
4054 // extended namespace declaration. Keep track of the most recent
4055 // definition of each namespace.
4056 std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest;
4057 for (DeclContext::specific_decl_iterator<NamespaceDecl>
4058 NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end());
4059 NS != NSEnd; ++NS)
David Blaikie581deb32012-06-06 20:45:41 +00004060 OrigToLatest[NS->getOriginalNamespace()] = *NS;
Douglas Gregor86d9a522009-09-21 16:56:56 +00004061
4062 // Add the most recent definition (or extended definition) of each
4063 // namespace to the list of results.
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004064 Results.EnterNewScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004065 for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator
Douglas Gregorba103062012-03-27 23:34:16 +00004066 NS = OrigToLatest.begin(),
4067 NSEnd = OrigToLatest.end();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004068 NS != NSEnd; ++NS)
John McCall0a2c5e22010-08-25 06:19:51 +00004069 Results.AddResult(CodeCompletionResult(NS->second, 0),
Douglas Gregor608300b2010-01-14 16:14:35 +00004070 CurContext, 0, false);
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004071 Results.ExitScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004072 }
4073
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004074 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004075 Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004076 Results.data(),Results.size());
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004077}
4078
4079void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) {
4080 if (!CodeCompleter)
4081 return;
4082
Douglas Gregor86d9a522009-09-21 16:56:56 +00004083 // After "namespace", we expect to see a namespace or alias.
Douglas Gregor218937c2011-02-01 19:23:04 +00004084 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004085 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004086 CodeCompletionContext::CCC_Namespace,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004087 &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregor5d2fc402010-01-14 03:27:13 +00004088 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00004089 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4090 CodeCompleter->includeGlobals());
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004091 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004092 Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004093 Results.data(),Results.size());
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004094}
4095
Douglas Gregored8d3222009-09-18 20:05:18 +00004096void Sema::CodeCompleteOperatorName(Scope *S) {
4097 if (!CodeCompleter)
4098 return;
Douglas Gregor86d9a522009-09-21 16:56:56 +00004099
John McCall0a2c5e22010-08-25 06:19:51 +00004100 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00004101 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004102 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004103 CodeCompletionContext::CCC_Type,
Douglas Gregor52779fb2010-09-23 23:01:17 +00004104 &ResultBuilder::IsType);
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004105 Results.EnterNewScope();
Douglas Gregored8d3222009-09-18 20:05:18 +00004106
Douglas Gregor86d9a522009-09-21 16:56:56 +00004107 // Add the names of overloadable operators.
4108#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
4109 if (std::strcmp(Spelling, "?")) \
Douglas Gregora4477812010-01-14 16:01:26 +00004110 Results.AddResult(Result(Spelling));
Douglas Gregor86d9a522009-09-21 16:56:56 +00004111#include "clang/Basic/OperatorKinds.def"
4112
4113 // Add any type names visible from the current scope
Douglas Gregor45bcd432010-01-14 03:21:49 +00004114 Results.allowNestedNameSpecifiers();
Douglas Gregor5d2fc402010-01-14 03:27:13 +00004115 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor8071e422010-08-15 06:18:01 +00004116 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4117 CodeCompleter->includeGlobals());
Douglas Gregor86d9a522009-09-21 16:56:56 +00004118
4119 // Add any type specifiers
David Blaikie4e4d0842012-03-11 07:00:24 +00004120 AddTypeSpecifierResults(getLangOpts(), Results);
Douglas Gregor8e0a0e42009-09-22 23:31:26 +00004121 Results.ExitScope();
Douglas Gregor86d9a522009-09-21 16:56:56 +00004122
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004123 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor8071e422010-08-15 06:18:01 +00004124 CodeCompletionContext::CCC_Type,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004125 Results.data(),Results.size());
Douglas Gregored8d3222009-09-18 20:05:18 +00004126}
Douglas Gregor49f40bd2009-09-18 19:03:04 +00004127
Douglas Gregor0133f522010-08-28 00:00:50 +00004128void Sema::CodeCompleteConstructorInitializer(Decl *ConstructorD,
Sean Huntcbb67482011-01-08 20:30:50 +00004129 CXXCtorInitializer** Initializers,
Douglas Gregor0133f522010-08-28 00:00:50 +00004130 unsigned NumInitializers) {
Douglas Gregor8987b232011-09-27 23:30:47 +00004131 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregor0133f522010-08-28 00:00:50 +00004132 CXXConstructorDecl *Constructor
4133 = static_cast<CXXConstructorDecl *>(ConstructorD);
4134 if (!Constructor)
4135 return;
4136
Douglas Gregor218937c2011-02-01 19:23:04 +00004137 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004138 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00004139 CodeCompletionContext::CCC_PotentiallyQualifiedName);
Douglas Gregor0133f522010-08-28 00:00:50 +00004140 Results.EnterNewScope();
4141
4142 // Fill in any already-initialized fields or base classes.
4143 llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields;
4144 llvm::SmallPtrSet<CanQualType, 4> InitializedBases;
4145 for (unsigned I = 0; I != NumInitializers; ++I) {
4146 if (Initializers[I]->isBaseInitializer())
4147 InitializedBases.insert(
4148 Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0)));
4149 else
Francois Pichet00eb3f92010-12-04 09:14:42 +00004150 InitializedFields.insert(cast<FieldDecl>(
4151 Initializers[I]->getAnyMember()));
Douglas Gregor0133f522010-08-28 00:00:50 +00004152 }
4153
4154 // Add completions for base classes.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004155 CodeCompletionBuilder Builder(Results.getAllocator(),
4156 Results.getCodeCompletionTUInfo());
Douglas Gregor0c431c82010-08-29 19:27:27 +00004157 bool SawLastInitializer = (NumInitializers == 0);
Douglas Gregor0133f522010-08-28 00:00:50 +00004158 CXXRecordDecl *ClassDecl = Constructor->getParent();
4159 for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(),
4160 BaseEnd = ClassDecl->bases_end();
4161 Base != BaseEnd; ++Base) {
Douglas Gregor0c431c82010-08-29 19:27:27 +00004162 if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) {
4163 SawLastInitializer
4164 = NumInitializers > 0 &&
4165 Initializers[NumInitializers - 1]->isBaseInitializer() &&
4166 Context.hasSameUnqualifiedType(Base->getType(),
4167 QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0));
Douglas Gregor0133f522010-08-28 00:00:50 +00004168 continue;
Douglas Gregor0c431c82010-08-29 19:27:27 +00004169 }
Douglas Gregor0133f522010-08-28 00:00:50 +00004170
Douglas Gregor218937c2011-02-01 19:23:04 +00004171 Builder.AddTypedTextChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00004172 Results.getAllocator().CopyString(
John McCallf85e1932011-06-15 23:02:42 +00004173 Base->getType().getAsString(Policy)));
Douglas Gregor218937c2011-02-01 19:23:04 +00004174 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4175 Builder.AddPlaceholderChunk("args");
4176 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4177 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor0c431c82010-08-29 19:27:27 +00004178 SawLastInitializer? CCP_NextInitializer
4179 : CCP_MemberDeclaration));
4180 SawLastInitializer = false;
Douglas Gregor0133f522010-08-28 00:00:50 +00004181 }
4182
4183 // Add completions for virtual base classes.
4184 for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(),
4185 BaseEnd = ClassDecl->vbases_end();
4186 Base != BaseEnd; ++Base) {
Douglas Gregor0c431c82010-08-29 19:27:27 +00004187 if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) {
4188 SawLastInitializer
4189 = NumInitializers > 0 &&
4190 Initializers[NumInitializers - 1]->isBaseInitializer() &&
4191 Context.hasSameUnqualifiedType(Base->getType(),
4192 QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0));
Douglas Gregor0133f522010-08-28 00:00:50 +00004193 continue;
Douglas Gregor0c431c82010-08-29 19:27:27 +00004194 }
Douglas Gregor0133f522010-08-28 00:00:50 +00004195
Douglas Gregor218937c2011-02-01 19:23:04 +00004196 Builder.AddTypedTextChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00004197 Builder.getAllocator().CopyString(
John McCallf85e1932011-06-15 23:02:42 +00004198 Base->getType().getAsString(Policy)));
Douglas Gregor218937c2011-02-01 19:23:04 +00004199 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4200 Builder.AddPlaceholderChunk("args");
4201 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4202 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor0c431c82010-08-29 19:27:27 +00004203 SawLastInitializer? CCP_NextInitializer
4204 : CCP_MemberDeclaration));
4205 SawLastInitializer = false;
Douglas Gregor0133f522010-08-28 00:00:50 +00004206 }
4207
4208 // Add completions for members.
4209 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
4210 FieldEnd = ClassDecl->field_end();
4211 Field != FieldEnd; ++Field) {
Douglas Gregor0c431c82010-08-29 19:27:27 +00004212 if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))) {
4213 SawLastInitializer
4214 = NumInitializers > 0 &&
Francois Pichet00eb3f92010-12-04 09:14:42 +00004215 Initializers[NumInitializers - 1]->isAnyMemberInitializer() &&
David Blaikie581deb32012-06-06 20:45:41 +00004216 Initializers[NumInitializers - 1]->getAnyMember() == *Field;
Douglas Gregor0133f522010-08-28 00:00:50 +00004217 continue;
Douglas Gregor0c431c82010-08-29 19:27:27 +00004218 }
Douglas Gregor0133f522010-08-28 00:00:50 +00004219
4220 if (!Field->getDeclName())
4221 continue;
4222
Douglas Gregordae68752011-02-01 22:57:45 +00004223 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00004224 Field->getIdentifier()->getName()));
4225 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4226 Builder.AddPlaceholderChunk("args");
4227 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4228 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor0c431c82010-08-29 19:27:27 +00004229 SawLastInitializer? CCP_NextInitializer
Douglas Gregora67e03f2010-09-09 21:42:20 +00004230 : CCP_MemberDeclaration,
Douglas Gregorba103062012-03-27 23:34:16 +00004231 CXCursor_MemberRef,
4232 CXAvailability_Available,
David Blaikie581deb32012-06-06 20:45:41 +00004233 *Field));
Douglas Gregor0c431c82010-08-29 19:27:27 +00004234 SawLastInitializer = false;
Douglas Gregor0133f522010-08-28 00:00:50 +00004235 }
4236 Results.ExitScope();
4237
Douglas Gregor52779fb2010-09-23 23:01:17 +00004238 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor0133f522010-08-28 00:00:50 +00004239 Results.data(), Results.size());
4240}
4241
Douglas Gregor81f3bff2012-02-15 15:34:24 +00004242/// \brief Determine whether this scope denotes a namespace.
4243static bool isNamespaceScope(Scope *S) {
4244 DeclContext *DC = static_cast<DeclContext *>(S->getEntity());
4245 if (!DC)
4246 return false;
4247
4248 return DC->isFileContext();
4249}
4250
4251void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro,
4252 bool AfterAmpersand) {
4253 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004254 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor81f3bff2012-02-15 15:34:24 +00004255 CodeCompletionContext::CCC_Other);
4256 Results.EnterNewScope();
4257
4258 // Note what has already been captured.
4259 llvm::SmallPtrSet<IdentifierInfo *, 4> Known;
4260 bool IncludedThis = false;
4261 for (SmallVectorImpl<LambdaCapture>::iterator C = Intro.Captures.begin(),
4262 CEnd = Intro.Captures.end();
4263 C != CEnd; ++C) {
4264 if (C->Kind == LCK_This) {
4265 IncludedThis = true;
4266 continue;
4267 }
4268
4269 Known.insert(C->Id);
4270 }
4271
4272 // Look for other capturable variables.
4273 for (; S && !isNamespaceScope(S); S = S->getParent()) {
4274 for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
4275 D != DEnd; ++D) {
4276 VarDecl *Var = dyn_cast<VarDecl>(*D);
4277 if (!Var ||
4278 !Var->hasLocalStorage() ||
4279 Var->hasAttr<BlocksAttr>())
4280 continue;
4281
4282 if (Known.insert(Var->getIdentifier()))
4283 Results.AddResult(CodeCompletionResult(Var), CurContext, 0, false);
4284 }
4285 }
4286
4287 // Add 'this', if it would be valid.
4288 if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy)
4289 addThisCompletion(*this, Results);
4290
4291 Results.ExitScope();
4292
4293 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4294 Results.data(), Results.size());
4295}
4296
James Dennetta40f7922012-06-14 03:11:41 +00004297/// Macro that optionally prepends an "@" to the string literal passed in via
4298/// Keyword, depending on whether NeedAt is true or false.
4299#define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword)
4300
Douglas Gregorbca403c2010-01-13 23:51:12 +00004301static void AddObjCImplementationResults(const LangOptions &LangOpts,
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004302 ResultBuilder &Results,
4303 bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004304 typedef CodeCompletionResult Result;
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004305 // Since we have an implementation, we can end it.
James Dennetta40f7922012-06-14 03:11:41 +00004306 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004307
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004308 CodeCompletionBuilder Builder(Results.getAllocator(),
4309 Results.getCodeCompletionTUInfo());
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004310 if (LangOpts.ObjC2) {
4311 // @dynamic
James Dennetta40f7922012-06-14 03:11:41 +00004312 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004313 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4314 Builder.AddPlaceholderChunk("property");
4315 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004316
4317 // @synthesize
James Dennetta40f7922012-06-14 03:11:41 +00004318 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004319 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4320 Builder.AddPlaceholderChunk("property");
4321 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004322 }
4323}
4324
Douglas Gregorbca403c2010-01-13 23:51:12 +00004325static void AddObjCInterfaceResults(const LangOptions &LangOpts,
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004326 ResultBuilder &Results,
4327 bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004328 typedef CodeCompletionResult Result;
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004329
4330 // Since we have an interface or protocol, we can end it.
James Dennetta40f7922012-06-14 03:11:41 +00004331 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004332
4333 if (LangOpts.ObjC2) {
4334 // @property
James Dennetta40f7922012-06-14 03:11:41 +00004335 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property")));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004336
4337 // @required
James Dennetta40f7922012-06-14 03:11:41 +00004338 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required")));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004339
4340 // @optional
James Dennetta40f7922012-06-14 03:11:41 +00004341 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional")));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004342 }
4343}
4344
Douglas Gregorbca403c2010-01-13 23:51:12 +00004345static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004346 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004347 CodeCompletionBuilder Builder(Results.getAllocator(),
4348 Results.getCodeCompletionTUInfo());
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004349
4350 // @class name ;
James Dennetta40f7922012-06-14 03:11:41 +00004351 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004352 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4353 Builder.AddPlaceholderChunk("name");
4354 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004355
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004356 if (Results.includeCodePatterns()) {
4357 // @interface name
4358 // FIXME: Could introduce the whole pattern, including superclasses and
4359 // such.
James Dennetta40f7922012-06-14 03:11:41 +00004360 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004361 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4362 Builder.AddPlaceholderChunk("class");
4363 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004364
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004365 // @protocol name
James Dennetta40f7922012-06-14 03:11:41 +00004366 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004367 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4368 Builder.AddPlaceholderChunk("protocol");
4369 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004370
4371 // @implementation name
James Dennetta40f7922012-06-14 03:11:41 +00004372 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004373 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4374 Builder.AddPlaceholderChunk("class");
4375 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004376 }
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004377
4378 // @compatibility_alias name
James Dennetta40f7922012-06-14 03:11:41 +00004379 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004380 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4381 Builder.AddPlaceholderChunk("alias");
4382 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4383 Builder.AddPlaceholderChunk("class");
4384 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004385}
4386
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004387void Sema::CodeCompleteObjCAtDirective(Scope *S) {
John McCall0a2c5e22010-08-25 06:19:51 +00004388 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00004389 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004390 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004391 CodeCompletionContext::CCC_Other);
Douglas Gregorc464ae82009-12-07 09:27:33 +00004392 Results.EnterNewScope();
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004393 if (isa<ObjCImplDecl>(CurContext))
David Blaikie4e4d0842012-03-11 07:00:24 +00004394 AddObjCImplementationResults(getLangOpts(), Results, false);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004395 else if (CurContext->isObjCContainer())
David Blaikie4e4d0842012-03-11 07:00:24 +00004396 AddObjCInterfaceResults(getLangOpts(), Results, false);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004397 else
Douglas Gregorbca403c2010-01-13 23:51:12 +00004398 AddObjCTopLevelResults(Results, false);
Douglas Gregorc464ae82009-12-07 09:27:33 +00004399 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004400 HandleCodeCompleteResults(this, CodeCompleter,
4401 CodeCompletionContext::CCC_Other,
4402 Results.data(),Results.size());
Douglas Gregorc464ae82009-12-07 09:27:33 +00004403}
4404
Douglas Gregorbca403c2010-01-13 23:51:12 +00004405static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004406 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004407 CodeCompletionBuilder Builder(Results.getAllocator(),
4408 Results.getCodeCompletionTUInfo());
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004409
4410 // @encode ( type-name )
Douglas Gregor8ca72082011-10-18 21:20:17 +00004411 const char *EncodeType = "char[]";
David Blaikie4e4d0842012-03-11 07:00:24 +00004412 if (Results.getSema().getLangOpts().CPlusPlus ||
4413 Results.getSema().getLangOpts().ConstStrings)
Douglas Gregor8ca72082011-10-18 21:20:17 +00004414 EncodeType = " const char[]";
4415 Builder.AddResultTypeChunk(EncodeType);
James Dennetta40f7922012-06-14 03:11:41 +00004416 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004417 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4418 Builder.AddPlaceholderChunk("type-name");
4419 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4420 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004421
4422 // @protocol ( protocol-name )
Douglas Gregor8ca72082011-10-18 21:20:17 +00004423 Builder.AddResultTypeChunk("Protocol *");
James Dennetta40f7922012-06-14 03:11:41 +00004424 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004425 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4426 Builder.AddPlaceholderChunk("protocol-name");
4427 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4428 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004429
4430 // @selector ( selector )
Douglas Gregor8ca72082011-10-18 21:20:17 +00004431 Builder.AddResultTypeChunk("SEL");
James Dennetta40f7922012-06-14 03:11:41 +00004432 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004433 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4434 Builder.AddPlaceholderChunk("selector");
4435 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4436 Results.AddResult(Result(Builder.TakeString()));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004437
4438 // @[ objects, ... ]
James Dennetta40f7922012-06-14 03:11:41 +00004439 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"["));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004440 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4441 Builder.AddPlaceholderChunk("objects, ...");
4442 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4443 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
4444 Results.AddResult(Result(Builder.TakeString()));
4445
4446 // @{ key : object, ... }
James Dennetta40f7922012-06-14 03:11:41 +00004447 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{"));
Ted Kremenekebcb57a2012-03-06 20:05:56 +00004448 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4449 Builder.AddPlaceholderChunk("key");
4450 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4451 Builder.AddChunk(CodeCompletionString::CK_Colon);
4452 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4453 Builder.AddPlaceholderChunk("object, ...");
4454 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4455 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4456 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004457}
4458
Douglas Gregorbca403c2010-01-13 23:51:12 +00004459static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004460 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004461 CodeCompletionBuilder Builder(Results.getAllocator(),
4462 Results.getCodeCompletionTUInfo());
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004463
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004464 if (Results.includeCodePatterns()) {
4465 // @try { statements } @catch ( declaration ) { statements } @finally
4466 // { statements }
James Dennetta40f7922012-06-14 03:11:41 +00004467 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004468 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4469 Builder.AddPlaceholderChunk("statements");
4470 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4471 Builder.AddTextChunk("@catch");
4472 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4473 Builder.AddPlaceholderChunk("parameter");
4474 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4475 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4476 Builder.AddPlaceholderChunk("statements");
4477 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4478 Builder.AddTextChunk("@finally");
4479 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4480 Builder.AddPlaceholderChunk("statements");
4481 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4482 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004483 }
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004484
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004485 // @throw
James Dennetta40f7922012-06-14 03:11:41 +00004486 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004487 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4488 Builder.AddPlaceholderChunk("expression");
4489 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004490
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004491 if (Results.includeCodePatterns()) {
4492 // @synchronized ( expression ) { statements }
James Dennetta40f7922012-06-14 03:11:41 +00004493 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized"));
Douglas Gregor218937c2011-02-01 19:23:04 +00004494 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4495 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4496 Builder.AddPlaceholderChunk("expression");
4497 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4498 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4499 Builder.AddPlaceholderChunk("statements");
4500 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4501 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorc8bddde2010-05-28 00:22:41 +00004502 }
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004503}
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004504
Douglas Gregorbca403c2010-01-13 23:51:12 +00004505static void AddObjCVisibilityResults(const LangOptions &LangOpts,
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004506 ResultBuilder &Results,
4507 bool NeedAt) {
John McCall0a2c5e22010-08-25 06:19:51 +00004508 typedef CodeCompletionResult Result;
James Dennetta40f7922012-06-14 03:11:41 +00004509 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private")));
4510 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected")));
4511 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public")));
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004512 if (LangOpts.ObjC2)
James Dennetta40f7922012-06-14 03:11:41 +00004513 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package")));
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004514}
4515
4516void Sema::CodeCompleteObjCAtVisibility(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00004517 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004518 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004519 CodeCompletionContext::CCC_Other);
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004520 Results.EnterNewScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00004521 AddObjCVisibilityResults(getLangOpts(), Results, false);
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004522 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004523 HandleCodeCompleteResults(this, CodeCompleter,
4524 CodeCompletionContext::CCC_Other,
4525 Results.data(),Results.size());
Douglas Gregorc38c3e12010-01-13 21:54:15 +00004526}
4527
4528void Sema::CodeCompleteObjCAtStatement(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00004529 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004530 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004531 CodeCompletionContext::CCC_Other);
Douglas Gregorb6ac2452010-01-13 21:24:21 +00004532 Results.EnterNewScope();
Douglas Gregorbca403c2010-01-13 23:51:12 +00004533 AddObjCStatementResults(Results, false);
4534 AddObjCExpressionResults(Results, false);
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004535 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004536 HandleCodeCompleteResults(this, CodeCompleter,
4537 CodeCompletionContext::CCC_Other,
4538 Results.data(),Results.size());
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004539}
4540
4541void Sema::CodeCompleteObjCAtExpression(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00004542 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004543 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004544 CodeCompletionContext::CCC_Other);
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004545 Results.EnterNewScope();
Douglas Gregorbca403c2010-01-13 23:51:12 +00004546 AddObjCExpressionResults(Results, false);
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004547 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004548 HandleCodeCompleteResults(this, CodeCompleter,
4549 CodeCompletionContext::CCC_Other,
4550 Results.data(),Results.size());
Douglas Gregor9a0c85e2009-12-07 09:51:25 +00004551}
4552
Douglas Gregor988358f2009-11-19 00:14:45 +00004553/// \brief Determine whether the addition of the given flag to an Objective-C
4554/// property's attributes will cause a conflict.
4555static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
4556 // Check if we've already added this flag.
4557 if (Attributes & NewFlag)
4558 return true;
4559
4560 Attributes |= NewFlag;
4561
4562 // Check for collisions with "readonly".
4563 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
4564 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
4565 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00004566 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Douglas Gregor988358f2009-11-19 00:14:45 +00004567 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00004568 ObjCDeclSpec::DQ_PR_retain |
4569 ObjCDeclSpec::DQ_PR_strong)))
Douglas Gregor988358f2009-11-19 00:14:45 +00004570 return true;
4571
John McCallf85e1932011-06-15 23:02:42 +00004572 // Check for more than one of { assign, copy, retain, strong }.
Douglas Gregor988358f2009-11-19 00:14:45 +00004573 unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00004574 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Douglas Gregor988358f2009-11-19 00:14:45 +00004575 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00004576 ObjCDeclSpec::DQ_PR_retain|
4577 ObjCDeclSpec::DQ_PR_strong);
Douglas Gregor988358f2009-11-19 00:14:45 +00004578 if (AssignCopyRetMask &&
4579 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign &&
John McCallf85e1932011-06-15 23:02:42 +00004580 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained &&
Douglas Gregor988358f2009-11-19 00:14:45 +00004581 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy &&
John McCallf85e1932011-06-15 23:02:42 +00004582 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain &&
4583 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong)
Douglas Gregor988358f2009-11-19 00:14:45 +00004584 return true;
4585
4586 return false;
4587}
4588
Douglas Gregora93b1082009-11-18 23:08:07 +00004589void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
Steve Naroffece8e712009-10-08 21:55:05 +00004590 if (!CodeCompleter)
4591 return;
Douglas Gregord3c68542009-11-19 01:08:35 +00004592
Steve Naroffece8e712009-10-08 21:55:05 +00004593 unsigned Attributes = ODS.getPropertyAttributes();
4594
John McCall0a2c5e22010-08-25 06:19:51 +00004595 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00004596 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004597 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004598 CodeCompletionContext::CCC_Other);
Steve Naroffece8e712009-10-08 21:55:05 +00004599 Results.EnterNewScope();
Douglas Gregor988358f2009-11-19 00:14:45 +00004600 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly))
John McCall0a2c5e22010-08-25 06:19:51 +00004601 Results.AddResult(CodeCompletionResult("readonly"));
Douglas Gregor988358f2009-11-19 00:14:45 +00004602 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign))
John McCall0a2c5e22010-08-25 06:19:51 +00004603 Results.AddResult(CodeCompletionResult("assign"));
John McCallf85e1932011-06-15 23:02:42 +00004604 if (!ObjCPropertyFlagConflicts(Attributes,
4605 ObjCDeclSpec::DQ_PR_unsafe_unretained))
4606 Results.AddResult(CodeCompletionResult("unsafe_unretained"));
Douglas Gregor988358f2009-11-19 00:14:45 +00004607 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite))
John McCall0a2c5e22010-08-25 06:19:51 +00004608 Results.AddResult(CodeCompletionResult("readwrite"));
Douglas Gregor988358f2009-11-19 00:14:45 +00004609 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain))
John McCall0a2c5e22010-08-25 06:19:51 +00004610 Results.AddResult(CodeCompletionResult("retain"));
John McCallf85e1932011-06-15 23:02:42 +00004611 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong))
4612 Results.AddResult(CodeCompletionResult("strong"));
Douglas Gregor988358f2009-11-19 00:14:45 +00004613 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy))
John McCall0a2c5e22010-08-25 06:19:51 +00004614 Results.AddResult(CodeCompletionResult("copy"));
Douglas Gregor988358f2009-11-19 00:14:45 +00004615 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic))
John McCall0a2c5e22010-08-25 06:19:51 +00004616 Results.AddResult(CodeCompletionResult("nonatomic"));
Fariborz Jahanian27f45232011-06-11 17:14:27 +00004617 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic))
4618 Results.AddResult(CodeCompletionResult("atomic"));
Douglas Gregor988358f2009-11-19 00:14:45 +00004619 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004620 CodeCompletionBuilder Setter(Results.getAllocator(),
4621 Results.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00004622 Setter.AddTypedTextChunk("setter");
4623 Setter.AddTextChunk(" = ");
4624 Setter.AddPlaceholderChunk("method");
4625 Results.AddResult(CodeCompletionResult(Setter.TakeString()));
Douglas Gregor54f01612009-11-19 00:01:57 +00004626 }
Douglas Gregor988358f2009-11-19 00:14:45 +00004627 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004628 CodeCompletionBuilder Getter(Results.getAllocator(),
4629 Results.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00004630 Getter.AddTypedTextChunk("getter");
4631 Getter.AddTextChunk(" = ");
4632 Getter.AddPlaceholderChunk("method");
4633 Results.AddResult(CodeCompletionResult(Getter.TakeString()));
Douglas Gregor54f01612009-11-19 00:01:57 +00004634 }
Steve Naroffece8e712009-10-08 21:55:05 +00004635 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004636 HandleCodeCompleteResults(this, CodeCompleter,
4637 CodeCompletionContext::CCC_Other,
4638 Results.data(),Results.size());
Steve Naroffece8e712009-10-08 21:55:05 +00004639}
Steve Naroffc4df6d22009-11-07 02:08:14 +00004640
Douglas Gregor4ad96852009-11-19 07:41:15 +00004641/// \brief Descripts the kind of Objective-C method that we want to find
4642/// via code completion.
4643enum ObjCMethodKind {
Dmitri Gribenko49fdccb2012-06-08 23:13:42 +00004644 MK_Any, ///< Any kind of method, provided it means other specified criteria.
4645 MK_ZeroArgSelector, ///< Zero-argument (unary) selector.
4646 MK_OneArgSelector ///< One-argument selector.
Douglas Gregor4ad96852009-11-19 07:41:15 +00004647};
4648
Douglas Gregor458433d2010-08-26 15:07:07 +00004649static bool isAcceptableObjCSelector(Selector Sel,
4650 ObjCMethodKind WantKind,
4651 IdentifierInfo **SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004652 unsigned NumSelIdents,
4653 bool AllowSameLength = true) {
Douglas Gregor458433d2010-08-26 15:07:07 +00004654 if (NumSelIdents > Sel.getNumArgs())
4655 return false;
4656
4657 switch (WantKind) {
4658 case MK_Any: break;
4659 case MK_ZeroArgSelector: return Sel.isUnarySelector();
4660 case MK_OneArgSelector: return Sel.getNumArgs() == 1;
4661 }
4662
Douglas Gregorcf544262010-11-17 21:36:08 +00004663 if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs())
4664 return false;
4665
Douglas Gregor458433d2010-08-26 15:07:07 +00004666 for (unsigned I = 0; I != NumSelIdents; ++I)
4667 if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
4668 return false;
4669
4670 return true;
4671}
4672
Douglas Gregor4ad96852009-11-19 07:41:15 +00004673static bool isAcceptableObjCMethod(ObjCMethodDecl *Method,
4674 ObjCMethodKind WantKind,
4675 IdentifierInfo **SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004676 unsigned NumSelIdents,
4677 bool AllowSameLength = true) {
Douglas Gregor458433d2010-08-26 15:07:07 +00004678 return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004679 NumSelIdents, AllowSameLength);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004680}
Douglas Gregord36adf52010-09-16 16:06:31 +00004681
4682namespace {
4683 /// \brief A set of selectors, which is used to avoid introducing multiple
4684 /// completions with the same selector into the result set.
4685 typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet;
4686}
4687
Douglas Gregor36ecb042009-11-17 23:22:23 +00004688/// \brief Add all of the Objective-C methods in the given Objective-C
4689/// container to the set of results.
4690///
4691/// The container will be a class, protocol, category, or implementation of
4692/// any of the above. This mether will recurse to include methods from
4693/// the superclasses of classes along with their categories, protocols, and
4694/// implementations.
4695///
4696/// \param Container the container in which we'll look to find methods.
4697///
James Dennetta40f7922012-06-14 03:11:41 +00004698/// \param WantInstanceMethods Whether to add instance methods (only); if
4699/// false, this routine will add factory methods (only).
Douglas Gregor36ecb042009-11-17 23:22:23 +00004700///
4701/// \param CurContext the context in which we're performing the lookup that
4702/// finds methods.
4703///
Douglas Gregorcf544262010-11-17 21:36:08 +00004704/// \param AllowSameLength Whether we allow a method to be added to the list
4705/// when it has the same number of parameters as we have selector identifiers.
4706///
Douglas Gregor36ecb042009-11-17 23:22:23 +00004707/// \param Results the structure into which we'll add results.
4708static void AddObjCMethods(ObjCContainerDecl *Container,
4709 bool WantInstanceMethods,
Douglas Gregor4ad96852009-11-19 07:41:15 +00004710 ObjCMethodKind WantKind,
Douglas Gregord3c68542009-11-19 01:08:35 +00004711 IdentifierInfo **SelIdents,
4712 unsigned NumSelIdents,
Douglas Gregor36ecb042009-11-17 23:22:23 +00004713 DeclContext *CurContext,
Douglas Gregord36adf52010-09-16 16:06:31 +00004714 VisitedSelectorSet &Selectors,
Douglas Gregorcf544262010-11-17 21:36:08 +00004715 bool AllowSameLength,
Douglas Gregor408be5a2010-08-25 01:08:01 +00004716 ResultBuilder &Results,
4717 bool InOriginalClass = true) {
John McCall0a2c5e22010-08-25 06:19:51 +00004718 typedef CodeCompletionResult Result;
Douglas Gregorb92a4082012-06-12 13:44:08 +00004719 Container = getContainerDef(Container);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004720 for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
4721 MEnd = Container->meth_end();
4722 M != MEnd; ++M) {
David Blaikie262bc182012-04-30 02:36:29 +00004723 if (M->isInstanceMethod() == WantInstanceMethods) {
Douglas Gregord3c68542009-11-19 01:08:35 +00004724 // Check whether the selector identifiers we've been given are a
4725 // subset of the identifiers for this particular method.
David Blaikie581deb32012-06-06 20:45:41 +00004726 if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004727 AllowSameLength))
Douglas Gregord3c68542009-11-19 01:08:35 +00004728 continue;
Douglas Gregor4ad96852009-11-19 07:41:15 +00004729
David Blaikie262bc182012-04-30 02:36:29 +00004730 if (!Selectors.insert(M->getSelector()))
Douglas Gregord36adf52010-09-16 16:06:31 +00004731 continue;
4732
David Blaikie581deb32012-06-06 20:45:41 +00004733 Result R = Result(*M, 0);
Douglas Gregord3c68542009-11-19 01:08:35 +00004734 R.StartParameter = NumSelIdents;
Douglas Gregor4ad96852009-11-19 07:41:15 +00004735 R.AllParametersAreInformative = (WantKind != MK_Any);
Douglas Gregor408be5a2010-08-25 01:08:01 +00004736 if (!InOriginalClass)
4737 R.Priority += CCD_InBaseClass;
Douglas Gregord3c68542009-11-19 01:08:35 +00004738 Results.MaybeAddResult(R, CurContext);
4739 }
Douglas Gregor36ecb042009-11-17 23:22:23 +00004740 }
4741
Douglas Gregore396c7b2010-09-16 15:34:59 +00004742 // Visit the protocols of protocols.
4743 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00004744 if (Protocol->hasDefinition()) {
4745 const ObjCList<ObjCProtocolDecl> &Protocols
4746 = Protocol->getReferencedProtocols();
4747 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
4748 E = Protocols.end();
4749 I != E; ++I)
4750 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
4751 NumSelIdents, CurContext, Selectors, AllowSameLength,
4752 Results, false);
4753 }
Douglas Gregore396c7b2010-09-16 15:34:59 +00004754 }
4755
Douglas Gregor36ecb042009-11-17 23:22:23 +00004756 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00004757 if (!IFace || !IFace->hasDefinition())
Douglas Gregor36ecb042009-11-17 23:22:23 +00004758 return;
4759
4760 // Add methods in protocols.
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00004761 for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(),
4762 E = IFace->protocol_end();
Douglas Gregor36ecb042009-11-17 23:22:23 +00004763 I != E; ++I)
Douglas Gregor4ad96852009-11-19 07:41:15 +00004764 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004765 CurContext, Selectors, AllowSameLength, Results, false);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004766
4767 // Add methods in categories.
4768 for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl;
4769 CatDecl = CatDecl->getNextClassCategory()) {
Douglas Gregor4ad96852009-11-19 07:41:15 +00004770 AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004771 NumSelIdents, CurContext, Selectors, AllowSameLength,
4772 Results, InOriginalClass);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004773
4774 // Add a categories protocol methods.
4775 const ObjCList<ObjCProtocolDecl> &Protocols
4776 = CatDecl->getReferencedProtocols();
4777 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
4778 E = Protocols.end();
4779 I != E; ++I)
Douglas Gregor4ad96852009-11-19 07:41:15 +00004780 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004781 NumSelIdents, CurContext, Selectors, AllowSameLength,
4782 Results, false);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004783
4784 // Add methods in category implementations.
4785 if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
Douglas Gregor4ad96852009-11-19 07:41:15 +00004786 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004787 NumSelIdents, CurContext, Selectors, AllowSameLength,
4788 Results, InOriginalClass);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004789 }
4790
4791 // Add methods in superclass.
4792 if (IFace->getSuperClass())
Douglas Gregor4ad96852009-11-19 07:41:15 +00004793 AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind,
Douglas Gregorcf544262010-11-17 21:36:08 +00004794 SelIdents, NumSelIdents, CurContext, Selectors,
4795 AllowSameLength, Results, false);
Douglas Gregor36ecb042009-11-17 23:22:23 +00004796
4797 // Add methods in our implementation, if any.
4798 if (ObjCImplementationDecl *Impl = IFace->getImplementation())
Douglas Gregor4ad96852009-11-19 07:41:15 +00004799 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00004800 NumSelIdents, CurContext, Selectors, AllowSameLength,
4801 Results, InOriginalClass);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004802}
4803
4804
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004805void Sema::CodeCompleteObjCPropertyGetter(Scope *S) {
John McCall0a2c5e22010-08-25 06:19:51 +00004806 typedef CodeCompletionResult Result;
Douglas Gregor4ad96852009-11-19 07:41:15 +00004807
4808 // Try to find the interface where getters might live.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004809 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004810 if (!Class) {
4811 if (ObjCCategoryDecl *Category
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004812 = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
Douglas Gregor4ad96852009-11-19 07:41:15 +00004813 Class = Category->getClassInterface();
4814
4815 if (!Class)
4816 return;
4817 }
4818
4819 // Find all of the potential getters.
Douglas Gregor218937c2011-02-01 19:23:04 +00004820 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004821 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004822 CodeCompletionContext::CCC_Other);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004823 Results.EnterNewScope();
4824
Douglas Gregord36adf52010-09-16 16:06:31 +00004825 VisitedSelectorSet Selectors;
4826 AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Selectors,
Douglas Gregorcf544262010-11-17 21:36:08 +00004827 /*AllowSameLength=*/true, Results);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004828 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004829 HandleCodeCompleteResults(this, CodeCompleter,
4830 CodeCompletionContext::CCC_Other,
4831 Results.data(),Results.size());
Douglas Gregor4ad96852009-11-19 07:41:15 +00004832}
4833
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004834void Sema::CodeCompleteObjCPropertySetter(Scope *S) {
John McCall0a2c5e22010-08-25 06:19:51 +00004835 typedef CodeCompletionResult Result;
Douglas Gregor4ad96852009-11-19 07:41:15 +00004836
4837 // Try to find the interface where setters might live.
4838 ObjCInterfaceDecl *Class
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004839 = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004840 if (!Class) {
4841 if (ObjCCategoryDecl *Category
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00004842 = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
Douglas Gregor4ad96852009-11-19 07:41:15 +00004843 Class = Category->getClassInterface();
4844
4845 if (!Class)
4846 return;
4847 }
4848
4849 // Find all of the potential getters.
Douglas Gregor218937c2011-02-01 19:23:04 +00004850 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004851 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004852 CodeCompletionContext::CCC_Other);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004853 Results.EnterNewScope();
4854
Douglas Gregord36adf52010-09-16 16:06:31 +00004855 VisitedSelectorSet Selectors;
4856 AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext,
Douglas Gregorcf544262010-11-17 21:36:08 +00004857 Selectors, /*AllowSameLength=*/true, Results);
Douglas Gregor4ad96852009-11-19 07:41:15 +00004858
4859 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00004860 HandleCodeCompleteResults(this, CodeCompleter,
4861 CodeCompletionContext::CCC_Other,
4862 Results.data(),Results.size());
Douglas Gregor36ecb042009-11-17 23:22:23 +00004863}
4864
Douglas Gregorafc45782011-02-15 22:19:42 +00004865void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS,
4866 bool IsParameter) {
John McCall0a2c5e22010-08-25 06:19:51 +00004867 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00004868 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004869 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00004870 CodeCompletionContext::CCC_Type);
Douglas Gregord32b0222010-08-24 01:06:58 +00004871 Results.EnterNewScope();
4872
4873 // Add context-sensitive, Objective-C parameter-passing keywords.
4874 bool AddedInOut = false;
4875 if ((DS.getObjCDeclQualifier() &
4876 (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) {
4877 Results.AddResult("in");
4878 Results.AddResult("inout");
4879 AddedInOut = true;
4880 }
4881 if ((DS.getObjCDeclQualifier() &
4882 (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) {
4883 Results.AddResult("out");
4884 if (!AddedInOut)
4885 Results.AddResult("inout");
4886 }
4887 if ((DS.getObjCDeclQualifier() &
4888 (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref |
4889 ObjCDeclSpec::DQ_Oneway)) == 0) {
4890 Results.AddResult("bycopy");
4891 Results.AddResult("byref");
4892 Results.AddResult("oneway");
4893 }
4894
Douglas Gregorafc45782011-02-15 22:19:42 +00004895 // If we're completing the return type of an Objective-C method and the
4896 // identifier IBAction refers to a macro, provide a completion item for
4897 // an action, e.g.,
4898 // IBAction)<#selector#>:(id)sender
4899 if (DS.getObjCDeclQualifier() == 0 && !IsParameter &&
4900 Context.Idents.get("IBAction").hasMacroDefinition()) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00004901 CodeCompletionBuilder Builder(Results.getAllocator(),
4902 Results.getCodeCompletionTUInfo(),
4903 CCP_CodePattern, CXAvailability_Available);
Douglas Gregorafc45782011-02-15 22:19:42 +00004904 Builder.AddTypedTextChunk("IBAction");
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00004905 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorafc45782011-02-15 22:19:42 +00004906 Builder.AddPlaceholderChunk("selector");
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00004907 Builder.AddChunk(CodeCompletionString::CK_Colon);
4908 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregorafc45782011-02-15 22:19:42 +00004909 Builder.AddTextChunk("id");
Benjamin Kramer1eb18af2012-03-26 16:57:36 +00004910 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorafc45782011-02-15 22:19:42 +00004911 Builder.AddTextChunk("sender");
4912 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
4913 }
4914
Douglas Gregord32b0222010-08-24 01:06:58 +00004915 // Add various builtin type names and specifiers.
4916 AddOrdinaryNameResults(PCC_Type, S, *this, Results);
4917 Results.ExitScope();
4918
4919 // Add the various type names
4920 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
4921 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4922 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4923 CodeCompleter->includeGlobals());
4924
4925 if (CodeCompleter->includeMacros())
4926 AddMacroResults(PP, Results);
4927
4928 HandleCodeCompleteResults(this, CodeCompleter,
4929 CodeCompletionContext::CCC_Type,
4930 Results.data(), Results.size());
4931}
4932
Douglas Gregor22f56992010-04-06 19:22:33 +00004933/// \brief When we have an expression with type "id", we may assume
4934/// that it has some more-specific class type based on knowledge of
4935/// common uses of Objective-C. This routine returns that class type,
4936/// or NULL if no better result could be determined.
4937static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) {
Douglas Gregor78edf512010-09-15 16:23:04 +00004938 ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E);
Douglas Gregor22f56992010-04-06 19:22:33 +00004939 if (!Msg)
4940 return 0;
4941
4942 Selector Sel = Msg->getSelector();
4943 if (Sel.isNull())
4944 return 0;
4945
4946 IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0);
4947 if (!Id)
4948 return 0;
4949
4950 ObjCMethodDecl *Method = Msg->getMethodDecl();
4951 if (!Method)
4952 return 0;
4953
4954 // Determine the class that we're sending the message to.
Douglas Gregor04badcf2010-04-21 00:45:42 +00004955 ObjCInterfaceDecl *IFace = 0;
4956 switch (Msg->getReceiverKind()) {
4957 case ObjCMessageExpr::Class:
John McCallc12c5bb2010-05-15 11:32:37 +00004958 if (const ObjCObjectType *ObjType
4959 = Msg->getClassReceiver()->getAs<ObjCObjectType>())
4960 IFace = ObjType->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00004961 break;
4962
4963 case ObjCMessageExpr::Instance: {
4964 QualType T = Msg->getInstanceReceiver()->getType();
4965 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
4966 IFace = Ptr->getInterfaceDecl();
4967 break;
4968 }
4969
4970 case ObjCMessageExpr::SuperInstance:
4971 case ObjCMessageExpr::SuperClass:
4972 break;
Douglas Gregor22f56992010-04-06 19:22:33 +00004973 }
4974
4975 if (!IFace)
4976 return 0;
4977
4978 ObjCInterfaceDecl *Super = IFace->getSuperClass();
4979 if (Method->isInstanceMethod())
4980 return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
4981 .Case("retain", IFace)
John McCallf85e1932011-06-15 23:02:42 +00004982 .Case("strong", IFace)
Douglas Gregor22f56992010-04-06 19:22:33 +00004983 .Case("autorelease", IFace)
4984 .Case("copy", IFace)
4985 .Case("copyWithZone", IFace)
4986 .Case("mutableCopy", IFace)
4987 .Case("mutableCopyWithZone", IFace)
4988 .Case("awakeFromCoder", IFace)
4989 .Case("replacementObjectFromCoder", IFace)
4990 .Case("class", IFace)
4991 .Case("classForCoder", IFace)
4992 .Case("superclass", Super)
4993 .Default(0);
4994
4995 return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
4996 .Case("new", IFace)
4997 .Case("alloc", IFace)
4998 .Case("allocWithZone", IFace)
4999 .Case("class", IFace)
5000 .Case("superclass", Super)
5001 .Default(0);
5002}
5003
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005004// Add a special completion for a message send to "super", which fills in the
5005// most likely case of forwarding all of our arguments to the superclass
5006// function.
5007///
5008/// \param S The semantic analysis object.
5009///
5010/// \param S NeedSuperKeyword Whether we need to prefix this completion with
5011/// the "super" keyword. Otherwise, we just need to provide the arguments.
5012///
5013/// \param SelIdents The identifiers in the selector that have already been
5014/// provided as arguments for a send to "super".
5015///
5016/// \param NumSelIdents The number of identifiers in \p SelIdents.
5017///
5018/// \param Results The set of results to augment.
5019///
5020/// \returns the Objective-C method declaration that would be invoked by
5021/// this "super" completion. If NULL, no completion was added.
5022static ObjCMethodDecl *AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword,
5023 IdentifierInfo **SelIdents,
5024 unsigned NumSelIdents,
5025 ResultBuilder &Results) {
5026 ObjCMethodDecl *CurMethod = S.getCurMethodDecl();
5027 if (!CurMethod)
5028 return 0;
5029
5030 ObjCInterfaceDecl *Class = CurMethod->getClassInterface();
5031 if (!Class)
5032 return 0;
5033
5034 // Try to find a superclass method with the same selector.
5035 ObjCMethodDecl *SuperMethod = 0;
Douglas Gregor78bcd912011-02-16 00:51:18 +00005036 while ((Class = Class->getSuperClass()) && !SuperMethod) {
5037 // Check in the class
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005038 SuperMethod = Class->getMethod(CurMethod->getSelector(),
5039 CurMethod->isInstanceMethod());
5040
Douglas Gregor78bcd912011-02-16 00:51:18 +00005041 // Check in categories or class extensions.
5042 if (!SuperMethod) {
5043 for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
5044 Category = Category->getNextClassCategory())
5045 if ((SuperMethod = Category->getMethod(CurMethod->getSelector(),
5046 CurMethod->isInstanceMethod())))
5047 break;
5048 }
5049 }
5050
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005051 if (!SuperMethod)
5052 return 0;
5053
5054 // Check whether the superclass method has the same signature.
5055 if (CurMethod->param_size() != SuperMethod->param_size() ||
5056 CurMethod->isVariadic() != SuperMethod->isVariadic())
5057 return 0;
5058
5059 for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(),
5060 CurPEnd = CurMethod->param_end(),
5061 SuperP = SuperMethod->param_begin();
5062 CurP != CurPEnd; ++CurP, ++SuperP) {
5063 // Make sure the parameter types are compatible.
5064 if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(),
5065 (*SuperP)->getType()))
5066 return 0;
5067
5068 // Make sure we have a parameter name to forward!
5069 if (!(*CurP)->getIdentifier())
5070 return 0;
5071 }
5072
5073 // We have a superclass method. Now, form the send-to-super completion.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005074 CodeCompletionBuilder Builder(Results.getAllocator(),
5075 Results.getCodeCompletionTUInfo());
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005076
5077 // Give this completion a return type.
Douglas Gregor8987b232011-09-27 23:30:47 +00005078 AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod,
5079 Builder);
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005080
5081 // If we need the "super" keyword, add it (plus some spacing).
5082 if (NeedSuperKeyword) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005083 Builder.AddTypedTextChunk("super");
5084 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005085 }
5086
5087 Selector Sel = CurMethod->getSelector();
5088 if (Sel.isUnarySelector()) {
5089 if (NeedSuperKeyword)
Douglas Gregordae68752011-02-01 22:57:45 +00005090 Builder.AddTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005091 Sel.getNameForSlot(0)));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005092 else
Douglas Gregordae68752011-02-01 22:57:45 +00005093 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005094 Sel.getNameForSlot(0)));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005095 } else {
5096 ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin();
5097 for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) {
5098 if (I > NumSelIdents)
Douglas Gregor218937c2011-02-01 19:23:04 +00005099 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005100
5101 if (I < NumSelIdents)
Douglas Gregor218937c2011-02-01 19:23:04 +00005102 Builder.AddInformativeChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00005103 Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005104 Sel.getNameForSlot(I) + ":"));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005105 else if (NeedSuperKeyword || I > NumSelIdents) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005106 Builder.AddTextChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00005107 Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005108 Sel.getNameForSlot(I) + ":"));
Douglas Gregordae68752011-02-01 22:57:45 +00005109 Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00005110 (*CurP)->getIdentifier()->getName()));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005111 } else {
Douglas Gregor218937c2011-02-01 19:23:04 +00005112 Builder.AddTypedTextChunk(
Douglas Gregordae68752011-02-01 22:57:45 +00005113 Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005114 Sel.getNameForSlot(I) + ":"));
Douglas Gregordae68752011-02-01 22:57:45 +00005115 Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00005116 (*CurP)->getIdentifier()->getName()));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005117 }
5118 }
5119 }
5120
Douglas Gregorba103062012-03-27 23:34:16 +00005121 Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod,
5122 CCP_SuperCompletion));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005123 return SuperMethod;
5124}
5125
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005126void Sema::CodeCompleteObjCMessageReceiver(Scope *S) {
John McCall0a2c5e22010-08-25 06:19:51 +00005127 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00005128 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005129 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005130 CodeCompletionContext::CCC_ObjCMessageReceiver,
David Blaikie4e4d0842012-03-11 07:00:24 +00005131 getLangOpts().CPlusPlus0x
Douglas Gregor81f3bff2012-02-15 15:34:24 +00005132 ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture
5133 : &ResultBuilder::IsObjCMessageReceiver);
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005134
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005135 CodeCompletionDeclConsumer Consumer(Results, CurContext);
5136 Results.EnterNewScope();
Douglas Gregor8071e422010-08-15 06:18:01 +00005137 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5138 CodeCompleter->includeGlobals());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005139
5140 // If we are in an Objective-C method inside a class that has a superclass,
5141 // add "super" as an option.
5142 if (ObjCMethodDecl *Method = getCurMethodDecl())
5143 if (ObjCInterfaceDecl *Iface = Method->getClassInterface())
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005144 if (Iface->getSuperClass()) {
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005145 Results.AddResult(Result("super"));
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005146
5147 AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, 0, 0, Results);
5148 }
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005149
David Blaikie4e4d0842012-03-11 07:00:24 +00005150 if (getLangOpts().CPlusPlus0x)
Douglas Gregor81f3bff2012-02-15 15:34:24 +00005151 addThisCompletion(*this, Results);
5152
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005153 Results.ExitScope();
5154
5155 if (CodeCompleter->includeMacros())
5156 AddMacroResults(PP, Results);
Douglas Gregorcee9ff12010-09-20 22:39:41 +00005157 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005158 Results.data(), Results.size());
Douglas Gregor8e254cf2010-05-27 23:06:34 +00005159
5160}
5161
Douglas Gregor2725ca82010-04-21 19:57:20 +00005162void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc,
5163 IdentifierInfo **SelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005164 unsigned NumSelIdents,
5165 bool AtArgumentExpression) {
Douglas Gregor2725ca82010-04-21 19:57:20 +00005166 ObjCInterfaceDecl *CDecl = 0;
5167 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5168 // Figure out which interface we're in.
5169 CDecl = CurMethod->getClassInterface();
5170 if (!CDecl)
5171 return;
5172
5173 // Find the superclass of this class.
5174 CDecl = CDecl->getSuperClass();
5175 if (!CDecl)
5176 return;
5177
5178 if (CurMethod->isInstanceMethod()) {
5179 // We are inside an instance method, which means that the message
5180 // send [super ...] is actually calling an instance method on the
Douglas Gregor6b0656a2010-10-13 21:24:53 +00005181 // current object.
5182 return CodeCompleteObjCInstanceMessage(S, 0,
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005183 SelIdents, NumSelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005184 AtArgumentExpression,
Douglas Gregor6b0656a2010-10-13 21:24:53 +00005185 CDecl);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005186 }
5187
5188 // Fall through to send to the superclass in CDecl.
5189 } else {
5190 // "super" may be the name of a type or variable. Figure out which
5191 // it is.
5192 IdentifierInfo *Super = &Context.Idents.get("super");
5193 NamedDecl *ND = LookupSingleName(S, Super, SuperLoc,
5194 LookupOrdinaryName);
5195 if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) {
5196 // "super" names an interface. Use it.
5197 } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) {
John McCallc12c5bb2010-05-15 11:32:37 +00005198 if (const ObjCObjectType *Iface
5199 = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>())
5200 CDecl = Iface->getInterface();
Douglas Gregor2725ca82010-04-21 19:57:20 +00005201 } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) {
5202 // "super" names an unresolved type; we can't be more specific.
5203 } else {
5204 // Assume that "super" names some kind of value and parse that way.
5205 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00005206 SourceLocation TemplateKWLoc;
Douglas Gregor2725ca82010-04-21 19:57:20 +00005207 UnqualifiedId id;
5208 id.setIdentifier(Super, SuperLoc);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00005209 ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id,
5210 false, false);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005211 return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(),
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005212 SelIdents, NumSelIdents,
5213 AtArgumentExpression);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005214 }
5215
5216 // Fall through
5217 }
5218
John McCallb3d87482010-08-24 05:47:05 +00005219 ParsedType Receiver;
Douglas Gregor2725ca82010-04-21 19:57:20 +00005220 if (CDecl)
John McCallb3d87482010-08-24 05:47:05 +00005221 Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl));
Douglas Gregor2725ca82010-04-21 19:57:20 +00005222 return CodeCompleteObjCClassMessage(S, Receiver, SelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005223 NumSelIdents, AtArgumentExpression,
5224 /*IsSuper=*/true);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005225}
5226
Douglas Gregorb9d77572010-09-21 00:03:25 +00005227/// \brief Given a set of code-completion results for the argument of a message
5228/// send, determine the preferred type (if any) for that argument expression.
5229static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results,
5230 unsigned NumSelIdents) {
5231 typedef CodeCompletionResult Result;
5232 ASTContext &Context = Results.getSema().Context;
5233
5234 QualType PreferredType;
5235 unsigned BestPriority = CCP_Unlikely * 2;
5236 Result *ResultsData = Results.data();
5237 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
5238 Result &R = ResultsData[I];
5239 if (R.Kind == Result::RK_Declaration &&
5240 isa<ObjCMethodDecl>(R.Declaration)) {
5241 if (R.Priority <= BestPriority) {
5242 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration);
5243 if (NumSelIdents <= Method->param_size()) {
5244 QualType MyPreferredType = Method->param_begin()[NumSelIdents - 1]
5245 ->getType();
5246 if (R.Priority < BestPriority || PreferredType.isNull()) {
5247 BestPriority = R.Priority;
5248 PreferredType = MyPreferredType;
5249 } else if (!Context.hasSameUnqualifiedType(PreferredType,
5250 MyPreferredType)) {
5251 PreferredType = QualType();
5252 }
5253 }
5254 }
5255 }
5256 }
5257
5258 return PreferredType;
5259}
5260
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005261static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
5262 ParsedType Receiver,
5263 IdentifierInfo **SelIdents,
5264 unsigned NumSelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005265 bool AtArgumentExpression,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005266 bool IsSuper,
5267 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00005268 typedef CodeCompletionResult Result;
Douglas Gregor24a069f2009-11-17 17:59:40 +00005269 ObjCInterfaceDecl *CDecl = 0;
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005270
Douglas Gregor24a069f2009-11-17 17:59:40 +00005271 // If the given name refers to an interface type, retrieve the
5272 // corresponding declaration.
Douglas Gregor2725ca82010-04-21 19:57:20 +00005273 if (Receiver) {
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005274 QualType T = SemaRef.GetTypeFromParser(Receiver, 0);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005275 if (!T.isNull())
John McCallc12c5bb2010-05-15 11:32:37 +00005276 if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>())
5277 CDecl = Interface->getInterface();
Douglas Gregor24a069f2009-11-17 17:59:40 +00005278 }
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005279
Douglas Gregor36ecb042009-11-17 23:22:23 +00005280 // Add all of the factory methods in this Objective-C class, its protocols,
5281 // superclasses, categories, implementation, etc.
Steve Naroffc4df6d22009-11-07 02:08:14 +00005282 Results.EnterNewScope();
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005283
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005284 // If this is a send-to-super, try to add the special "super" send
5285 // completion.
5286 if (IsSuper) {
5287 if (ObjCMethodDecl *SuperMethod
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005288 = AddSuperSendCompletion(SemaRef, false, SelIdents, NumSelIdents,
5289 Results))
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005290 Results.Ignore(SuperMethod);
5291 }
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005292
Douglas Gregor265f7492010-08-27 15:29:55 +00005293 // If we're inside an Objective-C method definition, prefer its selector to
5294 // others.
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005295 if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl())
Douglas Gregor265f7492010-08-27 15:29:55 +00005296 Results.setPreferredSelector(CurMethod->getSelector());
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005297
Douglas Gregord36adf52010-09-16 16:06:31 +00005298 VisitedSelectorSet Selectors;
Douglas Gregor13438f92010-04-06 16:40:00 +00005299 if (CDecl)
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005300 AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00005301 SemaRef.CurContext, Selectors, AtArgumentExpression,
5302 Results);
Douglas Gregor2725ca82010-04-21 19:57:20 +00005303 else {
Douglas Gregor13438f92010-04-06 16:40:00 +00005304 // We're messaging "id" as a type; provide all class/factory methods.
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005305
Douglas Gregor719770d2010-04-06 17:30:22 +00005306 // If we have an external source, load the entire class method
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005307 // pool from the AST file.
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005308 if (SemaRef.ExternalSource) {
5309 for (uint32_t I = 0,
5310 N = SemaRef.ExternalSource->GetNumExternalSelectors();
John McCall76bd1f32010-06-01 09:23:16 +00005311 I != N; ++I) {
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005312 Selector Sel = SemaRef.ExternalSource->GetExternalSelector(I);
5313 if (Sel.isNull() || SemaRef.MethodPool.count(Sel))
Douglas Gregor719770d2010-04-06 17:30:22 +00005314 continue;
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005315
5316 SemaRef.ReadMethodPool(Sel);
Douglas Gregor719770d2010-04-06 17:30:22 +00005317 }
5318 }
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005319
5320 for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(),
5321 MEnd = SemaRef.MethodPool.end();
Sebastian Redldb9d2142010-08-02 23:18:59 +00005322 M != MEnd; ++M) {
5323 for (ObjCMethodList *MethList = &M->second.second;
5324 MethList && MethList->Method;
Douglas Gregor13438f92010-04-06 16:40:00 +00005325 MethList = MethList->Next) {
5326 if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
5327 NumSelIdents))
5328 continue;
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005329
Douglas Gregor13438f92010-04-06 16:40:00 +00005330 Result R(MethList->Method, 0);
5331 R.StartParameter = NumSelIdents;
5332 R.AllParametersAreInformative = false;
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005333 Results.MaybeAddResult(R, SemaRef.CurContext);
Douglas Gregor13438f92010-04-06 16:40:00 +00005334 }
5335 }
5336 }
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005337
5338 Results.ExitScope();
5339}
Douglas Gregor13438f92010-04-06 16:40:00 +00005340
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005341void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver,
5342 IdentifierInfo **SelIdents,
5343 unsigned NumSelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005344 bool AtArgumentExpression,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00005345 bool IsSuper) {
Douglas Gregore081a612011-07-21 01:05:26 +00005346
5347 QualType T = this->GetTypeFromParser(Receiver);
5348
Douglas Gregor218937c2011-02-01 19:23:04 +00005349 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005350 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregore081a612011-07-21 01:05:26 +00005351 CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage,
Douglas Gregor0a47d692011-07-26 15:24:30 +00005352 T, SelIdents, NumSelIdents));
Douglas Gregore081a612011-07-21 01:05:26 +00005353
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005354 AddClassMessageCompletions(*this, S, Receiver, SelIdents, NumSelIdents,
5355 AtArgumentExpression, IsSuper, Results);
Douglas Gregorb9d77572010-09-21 00:03:25 +00005356
5357 // If we're actually at the argument expression (rather than prior to the
5358 // selector), we're actually performing code completion for an expression.
5359 // Determine whether we have a single, best method. If so, we can
5360 // code-complete the expression using the corresponding parameter type as
5361 // our preferred type, improving completion results.
5362 if (AtArgumentExpression) {
5363 QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
Douglas Gregore081a612011-07-21 01:05:26 +00005364 NumSelIdents);
Douglas Gregorb9d77572010-09-21 00:03:25 +00005365 if (PreferredType.isNull())
5366 CodeCompleteOrdinaryName(S, PCC_Expression);
5367 else
5368 CodeCompleteExpression(S, PreferredType);
5369 return;
5370 }
5371
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005372 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregore081a612011-07-21 01:05:26 +00005373 Results.getCompletionContext(),
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005374 Results.data(), Results.size());
Steve Naroffc4df6d22009-11-07 02:08:14 +00005375}
5376
Richard Trieuf81e5a92011-09-09 02:00:50 +00005377void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver,
Douglas Gregord3c68542009-11-19 01:08:35 +00005378 IdentifierInfo **SelIdents,
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005379 unsigned NumSelIdents,
Douglas Gregor70c5ac72010-09-20 23:34:21 +00005380 bool AtArgumentExpression,
Douglas Gregor6b0656a2010-10-13 21:24:53 +00005381 ObjCInterfaceDecl *Super) {
John McCall0a2c5e22010-08-25 06:19:51 +00005382 typedef CodeCompletionResult Result;
Steve Naroffc4df6d22009-11-07 02:08:14 +00005383
5384 Expr *RecExpr = static_cast<Expr *>(Receiver);
Steve Naroffc4df6d22009-11-07 02:08:14 +00005385
Douglas Gregor36ecb042009-11-17 23:22:23 +00005386 // If necessary, apply function/array conversion to the receiver.
5387 // C99 6.7.5.3p[7,8].
John Wiegley429bb272011-04-08 18:41:53 +00005388 if (RecExpr) {
5389 ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr);
5390 if (Conv.isInvalid()) // conversion failed. bail.
5391 return;
5392 RecExpr = Conv.take();
5393 }
Douglas Gregor6b0656a2010-10-13 21:24:53 +00005394 QualType ReceiverType = RecExpr? RecExpr->getType()
5395 : Super? Context.getObjCObjectPointerType(
5396 Context.getObjCInterfaceType(Super))
5397 : Context.getObjCIdType();
Steve Naroffc4df6d22009-11-07 02:08:14 +00005398
Douglas Gregorda892642010-11-08 21:12:30 +00005399 // If we're messaging an expression with type "id" or "Class", check
5400 // whether we know something special about the receiver that allows
5401 // us to assume a more-specific receiver type.
5402 if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType())
5403 if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) {
5404 if (ReceiverType->isObjCClassType())
5405 return CodeCompleteObjCClassMessage(S,
5406 ParsedType::make(Context.getObjCInterfaceType(IFace)),
5407 SelIdents, NumSelIdents,
5408 AtArgumentExpression, Super);
5409
5410 ReceiverType = Context.getObjCObjectPointerType(
5411 Context.getObjCInterfaceType(IFace));
5412 }
5413
Douglas Gregor36ecb042009-11-17 23:22:23 +00005414 // Build the set of methods we can see.
Douglas Gregor218937c2011-02-01 19:23:04 +00005415 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005416 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregore081a612011-07-21 01:05:26 +00005417 CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage,
Douglas Gregor0a47d692011-07-26 15:24:30 +00005418 ReceiverType, SelIdents, NumSelIdents));
Douglas Gregore081a612011-07-21 01:05:26 +00005419
Douglas Gregor36ecb042009-11-17 23:22:23 +00005420 Results.EnterNewScope();
Douglas Gregor22f56992010-04-06 19:22:33 +00005421
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005422 // If this is a send-to-super, try to add the special "super" send
5423 // completion.
Douglas Gregor6b0656a2010-10-13 21:24:53 +00005424 if (Super) {
Douglas Gregor03d8aec2010-08-27 15:10:57 +00005425 if (ObjCMethodDecl *SuperMethod
5426 = AddSuperSendCompletion(*this, false, SelIdents, NumSelIdents,
5427 Results))
5428 Results.Ignore(SuperMethod);
5429 }
5430
Douglas Gregor265f7492010-08-27 15:29:55 +00005431 // If we're inside an Objective-C method definition, prefer its selector to
5432 // others.
5433 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
5434 Results.setPreferredSelector(CurMethod->getSelector());
Douglas Gregor36ecb042009-11-17 23:22:23 +00005435
Douglas Gregord36adf52010-09-16 16:06:31 +00005436 // Keep track of the selectors we've already added.
5437 VisitedSelectorSet Selectors;
5438
Douglas Gregorf74a4192009-11-18 00:06:18 +00005439 // Handle messages to Class. This really isn't a message to an instance
5440 // method, so we treat it the same way we would treat a message send to a
5441 // class method.
5442 if (ReceiverType->isObjCClassType() ||
5443 ReceiverType->isObjCQualifiedClassType()) {
5444 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5445 if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
Douglas Gregor4ad96852009-11-19 07:41:15 +00005446 AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00005447 CurContext, Selectors, AtArgumentExpression, Results);
Douglas Gregorf74a4192009-11-18 00:06:18 +00005448 }
5449 }
5450 // Handle messages to a qualified ID ("id<foo>").
5451 else if (const ObjCObjectPointerType *QualID
5452 = ReceiverType->getAsObjCQualifiedIdType()) {
5453 // Search protocols for instance methods.
5454 for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(),
5455 E = QualID->qual_end();
5456 I != E; ++I)
Douglas Gregor4ad96852009-11-19 07:41:15 +00005457 AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
Douglas Gregorcf544262010-11-17 21:36:08 +00005458 Selectors, AtArgumentExpression, Results);
Douglas Gregorf74a4192009-11-18 00:06:18 +00005459 }
5460 // Handle messages to a pointer to interface type.
5461 else if (const ObjCObjectPointerType *IFacePtr
5462 = ReceiverType->getAsObjCInterfacePointerType()) {
5463 // Search the class, its superclasses, etc., for instance methods.
Douglas Gregor4ad96852009-11-19 07:41:15 +00005464 AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents,
Douglas Gregorcf544262010-11-17 21:36:08 +00005465 NumSelIdents, CurContext, Selectors, AtArgumentExpression,
5466 Results);
Douglas Gregorf74a4192009-11-18 00:06:18 +00005467
5468 // Search protocols for instance methods.
5469 for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(),
5470 E = IFacePtr->qual_end();
5471 I != E; ++I)
Douglas Gregor4ad96852009-11-19 07:41:15 +00005472 AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
Douglas Gregorcf544262010-11-17 21:36:08 +00005473 Selectors, AtArgumentExpression, Results);
Douglas Gregorf74a4192009-11-18 00:06:18 +00005474 }
Douglas Gregor13438f92010-04-06 16:40:00 +00005475 // Handle messages to "id".
5476 else if (ReceiverType->isObjCIdType()) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005477 // We're messaging "id", so provide all instance methods we know
5478 // about as code-completion results.
5479
5480 // If we have an external source, load the entire class method
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005481 // pool from the AST file.
Douglas Gregor719770d2010-04-06 17:30:22 +00005482 if (ExternalSource) {
John McCall76bd1f32010-06-01 09:23:16 +00005483 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5484 I != N; ++I) {
5485 Selector Sel = ExternalSource->GetExternalSelector(I);
Sebastian Redldb9d2142010-08-02 23:18:59 +00005486 if (Sel.isNull() || MethodPool.count(Sel))
Douglas Gregor719770d2010-04-06 17:30:22 +00005487 continue;
5488
Sebastian Redldb9d2142010-08-02 23:18:59 +00005489 ReadMethodPool(Sel);
Douglas Gregor719770d2010-04-06 17:30:22 +00005490 }
5491 }
5492
Sebastian Redldb9d2142010-08-02 23:18:59 +00005493 for (GlobalMethodPool::iterator M = MethodPool.begin(),
5494 MEnd = MethodPool.end();
5495 M != MEnd; ++M) {
5496 for (ObjCMethodList *MethList = &M->second.first;
5497 MethList && MethList->Method;
Douglas Gregor13438f92010-04-06 16:40:00 +00005498 MethList = MethList->Next) {
5499 if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
5500 NumSelIdents))
5501 continue;
Douglas Gregord36adf52010-09-16 16:06:31 +00005502
5503 if (!Selectors.insert(MethList->Method->getSelector()))
5504 continue;
5505
Douglas Gregor13438f92010-04-06 16:40:00 +00005506 Result R(MethList->Method, 0);
5507 R.StartParameter = NumSelIdents;
5508 R.AllParametersAreInformative = false;
5509 Results.MaybeAddResult(R, CurContext);
5510 }
5511 }
5512 }
Steve Naroffc4df6d22009-11-07 02:08:14 +00005513 Results.ExitScope();
Douglas Gregorb9d77572010-09-21 00:03:25 +00005514
5515
5516 // If we're actually at the argument expression (rather than prior to the
5517 // selector), we're actually performing code completion for an expression.
5518 // Determine whether we have a single, best method. If so, we can
5519 // code-complete the expression using the corresponding parameter type as
5520 // our preferred type, improving completion results.
5521 if (AtArgumentExpression) {
5522 QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
5523 NumSelIdents);
5524 if (PreferredType.isNull())
5525 CodeCompleteOrdinaryName(S, PCC_Expression);
5526 else
5527 CodeCompleteExpression(S, PreferredType);
5528 return;
5529 }
5530
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005531 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregore081a612011-07-21 01:05:26 +00005532 Results.getCompletionContext(),
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005533 Results.data(),Results.size());
Steve Naroffc4df6d22009-11-07 02:08:14 +00005534}
Douglas Gregor55385fe2009-11-18 04:19:12 +00005535
Douglas Gregorfb629412010-08-23 21:17:50 +00005536void Sema::CodeCompleteObjCForCollection(Scope *S,
5537 DeclGroupPtrTy IterationVar) {
5538 CodeCompleteExpressionData Data;
5539 Data.ObjCCollection = true;
5540
5541 if (IterationVar.getAsOpaquePtr()) {
5542 DeclGroupRef DG = IterationVar.getAsVal<DeclGroupRef>();
5543 for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) {
5544 if (*I)
5545 Data.IgnoreDecls.push_back(*I);
5546 }
5547 }
5548
5549 CodeCompleteExpression(S, Data);
5550}
5551
Douglas Gregor458433d2010-08-26 15:07:07 +00005552void Sema::CodeCompleteObjCSelector(Scope *S, IdentifierInfo **SelIdents,
5553 unsigned NumSelIdents) {
5554 // If we have an external source, load the entire class method
5555 // pool from the AST file.
5556 if (ExternalSource) {
5557 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5558 I != N; ++I) {
5559 Selector Sel = ExternalSource->GetExternalSelector(I);
5560 if (Sel.isNull() || MethodPool.count(Sel))
5561 continue;
5562
5563 ReadMethodPool(Sel);
5564 }
5565 }
5566
Douglas Gregor218937c2011-02-01 19:23:04 +00005567 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005568 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005569 CodeCompletionContext::CCC_SelectorName);
Douglas Gregor458433d2010-08-26 15:07:07 +00005570 Results.EnterNewScope();
5571 for (GlobalMethodPool::iterator M = MethodPool.begin(),
5572 MEnd = MethodPool.end();
5573 M != MEnd; ++M) {
5574
5575 Selector Sel = M->first;
5576 if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents, NumSelIdents))
5577 continue;
5578
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005579 CodeCompletionBuilder Builder(Results.getAllocator(),
5580 Results.getCodeCompletionTUInfo());
Douglas Gregor458433d2010-08-26 15:07:07 +00005581 if (Sel.isUnarySelector()) {
Douglas Gregordae68752011-02-01 22:57:45 +00005582 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00005583 Sel.getNameForSlot(0)));
Douglas Gregor218937c2011-02-01 19:23:04 +00005584 Results.AddResult(Builder.TakeString());
Douglas Gregor458433d2010-08-26 15:07:07 +00005585 continue;
5586 }
5587
Douglas Gregor2d9e21f2010-08-26 16:46:39 +00005588 std::string Accumulator;
Douglas Gregor458433d2010-08-26 15:07:07 +00005589 for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) {
Douglas Gregor2d9e21f2010-08-26 16:46:39 +00005590 if (I == NumSelIdents) {
5591 if (!Accumulator.empty()) {
Douglas Gregordae68752011-02-01 22:57:45 +00005592 Builder.AddInformativeChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00005593 Accumulator));
Douglas Gregor2d9e21f2010-08-26 16:46:39 +00005594 Accumulator.clear();
5595 }
5596 }
5597
Benjamin Kramera0651c52011-07-26 16:59:25 +00005598 Accumulator += Sel.getNameForSlot(I);
Douglas Gregor2d9e21f2010-08-26 16:46:39 +00005599 Accumulator += ':';
Douglas Gregor458433d2010-08-26 15:07:07 +00005600 }
Douglas Gregordae68752011-02-01 22:57:45 +00005601 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator));
Douglas Gregor218937c2011-02-01 19:23:04 +00005602 Results.AddResult(Builder.TakeString());
Douglas Gregor458433d2010-08-26 15:07:07 +00005603 }
5604 Results.ExitScope();
5605
5606 HandleCodeCompleteResults(this, CodeCompleter,
5607 CodeCompletionContext::CCC_SelectorName,
5608 Results.data(), Results.size());
5609}
5610
Douglas Gregor55385fe2009-11-18 04:19:12 +00005611/// \brief Add all of the protocol declarations that we find in the given
5612/// (translation unit) context.
5613static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext,
Douglas Gregor083128f2009-11-18 04:49:41 +00005614 bool OnlyForwardDeclarations,
Douglas Gregor55385fe2009-11-18 04:19:12 +00005615 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00005616 typedef CodeCompletionResult Result;
Douglas Gregor55385fe2009-11-18 04:19:12 +00005617
5618 for (DeclContext::decl_iterator D = Ctx->decls_begin(),
5619 DEnd = Ctx->decls_end();
5620 D != DEnd; ++D) {
5621 // Record any protocols we find.
5622 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D))
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00005623 if (!OnlyForwardDeclarations || !Proto->hasDefinition())
Douglas Gregor608300b2010-01-14 16:14:35 +00005624 Results.AddResult(Result(Proto, 0), CurContext, 0, false);
Douglas Gregor55385fe2009-11-18 04:19:12 +00005625 }
5626}
5627
5628void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
5629 unsigned NumProtocols) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005630 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005631 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005632 CodeCompletionContext::CCC_ObjCProtocolName);
Douglas Gregor55385fe2009-11-18 04:19:12 +00005633
Douglas Gregor70c23352010-12-09 21:44:02 +00005634 if (CodeCompleter && CodeCompleter->includeGlobals()) {
5635 Results.EnterNewScope();
5636
5637 // Tell the result set to ignore all of the protocols we have
5638 // already seen.
5639 // FIXME: This doesn't work when caching code-completion results.
5640 for (unsigned I = 0; I != NumProtocols; ++I)
5641 if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first,
5642 Protocols[I].second))
5643 Results.Ignore(Protocol);
Douglas Gregor55385fe2009-11-18 04:19:12 +00005644
Douglas Gregor70c23352010-12-09 21:44:02 +00005645 // Add all protocols.
5646 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false,
5647 Results);
Douglas Gregor083128f2009-11-18 04:49:41 +00005648
Douglas Gregor70c23352010-12-09 21:44:02 +00005649 Results.ExitScope();
5650 }
5651
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005652 HandleCodeCompleteResults(this, CodeCompleter,
5653 CodeCompletionContext::CCC_ObjCProtocolName,
5654 Results.data(),Results.size());
Douglas Gregor083128f2009-11-18 04:49:41 +00005655}
5656
5657void Sema::CodeCompleteObjCProtocolDecl(Scope *) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005658 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005659 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005660 CodeCompletionContext::CCC_ObjCProtocolName);
Douglas Gregor083128f2009-11-18 04:49:41 +00005661
Douglas Gregor70c23352010-12-09 21:44:02 +00005662 if (CodeCompleter && CodeCompleter->includeGlobals()) {
5663 Results.EnterNewScope();
5664
5665 // Add all protocols.
5666 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true,
5667 Results);
Douglas Gregor55385fe2009-11-18 04:19:12 +00005668
Douglas Gregor70c23352010-12-09 21:44:02 +00005669 Results.ExitScope();
5670 }
5671
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005672 HandleCodeCompleteResults(this, CodeCompleter,
5673 CodeCompletionContext::CCC_ObjCProtocolName,
5674 Results.data(),Results.size());
Douglas Gregor55385fe2009-11-18 04:19:12 +00005675}
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005676
5677/// \brief Add all of the Objective-C interface declarations that we find in
5678/// the given (translation unit) context.
5679static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext,
5680 bool OnlyForwardDeclarations,
5681 bool OnlyUnimplemented,
5682 ResultBuilder &Results) {
John McCall0a2c5e22010-08-25 06:19:51 +00005683 typedef CodeCompletionResult Result;
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005684
5685 for (DeclContext::decl_iterator D = Ctx->decls_begin(),
5686 DEnd = Ctx->decls_end();
5687 D != DEnd; ++D) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +00005688 // Record any interfaces we find.
5689 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D))
Douglas Gregor7723fec2011-12-15 20:29:51 +00005690 if ((!OnlyForwardDeclarations || !Class->hasDefinition()) &&
Douglas Gregordeacbdc2010-08-11 12:19:30 +00005691 (!OnlyUnimplemented || !Class->getImplementation()))
5692 Results.AddResult(Result(Class, 0), CurContext, 0, false);
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005693 }
5694}
5695
5696void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005697 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005698 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005699 CodeCompletionContext::CCC_Other);
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005700 Results.EnterNewScope();
5701
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005702 if (CodeCompleter->includeGlobals()) {
5703 // Add all classes.
5704 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5705 false, Results);
5706 }
5707
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005708 Results.ExitScope();
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005709
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005710 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005711 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005712 Results.data(),Results.size());
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005713}
5714
Douglas Gregorc83c6872010-04-15 22:33:43 +00005715void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName,
5716 SourceLocation ClassNameLoc) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005717 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005718 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005719 CodeCompletionContext::CCC_ObjCInterfaceName);
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005720 Results.EnterNewScope();
5721
5722 // Make sure that we ignore the class we're currently defining.
5723 NamedDecl *CurClass
Douglas Gregorc83c6872010-04-15 22:33:43 +00005724 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005725 if (CurClass && isa<ObjCInterfaceDecl>(CurClass))
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005726 Results.Ignore(CurClass);
5727
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005728 if (CodeCompleter->includeGlobals()) {
5729 // Add all classes.
5730 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5731 false, Results);
5732 }
5733
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005734 Results.ExitScope();
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005735
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005736 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005737 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005738 Results.data(),Results.size());
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005739}
5740
5741void Sema::CodeCompleteObjCImplementationDecl(Scope *S) {
Douglas Gregor218937c2011-02-01 19:23:04 +00005742 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005743 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005744 CodeCompletionContext::CCC_Other);
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005745 Results.EnterNewScope();
5746
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005747 if (CodeCompleter->includeGlobals()) {
5748 // Add all unimplemented classes.
5749 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5750 true, Results);
5751 }
5752
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005753 Results.ExitScope();
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005754
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005755 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0f91c8c2011-07-30 06:55:39 +00005756 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005757 Results.data(),Results.size());
Douglas Gregor3b49aca2009-11-18 16:26:39 +00005758}
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005759
5760void Sema::CodeCompleteObjCInterfaceCategory(Scope *S,
Douglas Gregorc83c6872010-04-15 22:33:43 +00005761 IdentifierInfo *ClassName,
5762 SourceLocation ClassNameLoc) {
John McCall0a2c5e22010-08-25 06:19:51 +00005763 typedef CodeCompletionResult Result;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005764
Douglas Gregor218937c2011-02-01 19:23:04 +00005765 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005766 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor3da626b2011-07-07 16:03:39 +00005767 CodeCompletionContext::CCC_ObjCCategoryName);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005768
5769 // Ignore any categories we find that have already been implemented by this
5770 // interface.
5771 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
5772 NamedDecl *CurClass
Douglas Gregorc83c6872010-04-15 22:33:43 +00005773 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005774 if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass))
5775 for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
5776 Category = Category->getNextClassCategory())
5777 CategoryNames.insert(Category->getIdentifier());
5778
5779 // Add all of the categories we know about.
5780 Results.EnterNewScope();
5781 TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5782 for (DeclContext::decl_iterator D = TU->decls_begin(),
5783 DEnd = TU->decls_end();
5784 D != DEnd; ++D)
5785 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D))
5786 if (CategoryNames.insert(Category->getIdentifier()))
Douglas Gregor608300b2010-01-14 16:14:35 +00005787 Results.AddResult(Result(Category, 0), CurContext, 0, false);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005788 Results.ExitScope();
5789
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005790 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor3da626b2011-07-07 16:03:39 +00005791 CodeCompletionContext::CCC_ObjCCategoryName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005792 Results.data(),Results.size());
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005793}
5794
5795void Sema::CodeCompleteObjCImplementationCategory(Scope *S,
Douglas Gregorc83c6872010-04-15 22:33:43 +00005796 IdentifierInfo *ClassName,
5797 SourceLocation ClassNameLoc) {
John McCall0a2c5e22010-08-25 06:19:51 +00005798 typedef CodeCompletionResult Result;
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005799
5800 // Find the corresponding interface. If we couldn't find the interface, the
5801 // program itself is ill-formed. However, we'll try to be helpful still by
5802 // providing the list of all of the categories we know about.
5803 NamedDecl *CurClass
Douglas Gregorc83c6872010-04-15 22:33:43 +00005804 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005805 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass);
5806 if (!Class)
Douglas Gregorc83c6872010-04-15 22:33:43 +00005807 return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005808
Douglas Gregor218937c2011-02-01 19:23:04 +00005809 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005810 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor3da626b2011-07-07 16:03:39 +00005811 CodeCompletionContext::CCC_ObjCCategoryName);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005812
5813 // Add all of the categories that have have corresponding interface
5814 // declarations in this class and any of its superclasses, except for
5815 // already-implemented categories in the class itself.
5816 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
5817 Results.EnterNewScope();
5818 bool IgnoreImplemented = true;
5819 while (Class) {
5820 for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
5821 Category = Category->getNextClassCategory())
5822 if ((!IgnoreImplemented || !Category->getImplementation()) &&
5823 CategoryNames.insert(Category->getIdentifier()))
Douglas Gregor608300b2010-01-14 16:14:35 +00005824 Results.AddResult(Result(Category, 0), CurContext, 0, false);
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005825
5826 Class = Class->getSuperClass();
5827 IgnoreImplemented = false;
5828 }
5829 Results.ExitScope();
5830
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005831 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor3da626b2011-07-07 16:03:39 +00005832 CodeCompletionContext::CCC_ObjCCategoryName,
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005833 Results.data(),Results.size());
Douglas Gregor33ced0b2009-11-18 19:08:43 +00005834}
Douglas Gregor322328b2009-11-18 22:32:06 +00005835
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00005836void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) {
John McCall0a2c5e22010-08-25 06:19:51 +00005837 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00005838 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005839 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005840 CodeCompletionContext::CCC_Other);
Douglas Gregor322328b2009-11-18 22:32:06 +00005841
5842 // Figure out where this @synthesize lives.
5843 ObjCContainerDecl *Container
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00005844 = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
Douglas Gregor322328b2009-11-18 22:32:06 +00005845 if (!Container ||
5846 (!isa<ObjCImplementationDecl>(Container) &&
5847 !isa<ObjCCategoryImplDecl>(Container)))
5848 return;
5849
5850 // Ignore any properties that have already been implemented.
Douglas Gregorb92a4082012-06-12 13:44:08 +00005851 Container = getContainerDef(Container);
5852 for (DeclContext::decl_iterator D = Container->decls_begin(),
Douglas Gregor322328b2009-11-18 22:32:06 +00005853 DEnd = Container->decls_end();
5854 D != DEnd; ++D)
5855 if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D))
5856 Results.Ignore(PropertyImpl->getPropertyDecl());
5857
5858 // Add any properties that we find.
Douglas Gregor73449212010-12-09 23:01:55 +00005859 AddedPropertiesSet AddedProperties;
Douglas Gregor322328b2009-11-18 22:32:06 +00005860 Results.EnterNewScope();
5861 if (ObjCImplementationDecl *ClassImpl
5862 = dyn_cast<ObjCImplementationDecl>(Container))
Douglas Gregor4b81cde2011-05-05 15:50:42 +00005863 AddObjCProperties(ClassImpl->getClassInterface(), false,
5864 /*AllowNullaryMethods=*/false, CurContext,
Douglas Gregor73449212010-12-09 23:01:55 +00005865 AddedProperties, Results);
Douglas Gregor322328b2009-11-18 22:32:06 +00005866 else
5867 AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(),
Douglas Gregor4b81cde2011-05-05 15:50:42 +00005868 false, /*AllowNullaryMethods=*/false, CurContext,
5869 AddedProperties, Results);
Douglas Gregor322328b2009-11-18 22:32:06 +00005870 Results.ExitScope();
5871
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005872 HandleCodeCompleteResults(this, CodeCompleter,
5873 CodeCompletionContext::CCC_Other,
5874 Results.data(),Results.size());
Douglas Gregor322328b2009-11-18 22:32:06 +00005875}
5876
5877void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00005878 IdentifierInfo *PropertyName) {
John McCall0a2c5e22010-08-25 06:19:51 +00005879 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00005880 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005881 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00005882 CodeCompletionContext::CCC_Other);
Douglas Gregor322328b2009-11-18 22:32:06 +00005883
5884 // Figure out where this @synthesize lives.
5885 ObjCContainerDecl *Container
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00005886 = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
Douglas Gregor322328b2009-11-18 22:32:06 +00005887 if (!Container ||
5888 (!isa<ObjCImplementationDecl>(Container) &&
5889 !isa<ObjCCategoryImplDecl>(Container)))
5890 return;
5891
5892 // Figure out which interface we're looking into.
5893 ObjCInterfaceDecl *Class = 0;
5894 if (ObjCImplementationDecl *ClassImpl
5895 = dyn_cast<ObjCImplementationDecl>(Container))
5896 Class = ClassImpl->getClassInterface();
5897 else
5898 Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
5899 ->getClassInterface();
5900
Douglas Gregore8426052011-04-18 14:40:46 +00005901 // Determine the type of the property we're synthesizing.
5902 QualType PropertyType = Context.getObjCIdType();
5903 if (Class) {
5904 if (ObjCPropertyDecl *Property
5905 = Class->FindPropertyDeclaration(PropertyName)) {
5906 PropertyType
5907 = Property->getType().getNonReferenceType().getUnqualifiedType();
5908
5909 // Give preference to ivars
5910 Results.setPreferredType(PropertyType);
5911 }
5912 }
5913
Douglas Gregor322328b2009-11-18 22:32:06 +00005914 // Add all of the instance variables in this class and its superclasses.
5915 Results.EnterNewScope();
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005916 bool SawSimilarlyNamedIvar = false;
5917 std::string NameWithPrefix;
5918 NameWithPrefix += '_';
Benjamin Kramera0651c52011-07-26 16:59:25 +00005919 NameWithPrefix += PropertyName->getName();
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005920 std::string NameWithSuffix = PropertyName->getName().str();
5921 NameWithSuffix += '_';
Douglas Gregor322328b2009-11-18 22:32:06 +00005922 for(; Class; Class = Class->getSuperClass()) {
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005923 for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar;
5924 Ivar = Ivar->getNextIvar()) {
Douglas Gregore8426052011-04-18 14:40:46 +00005925 Results.AddResult(Result(Ivar, 0), CurContext, 0, false);
5926
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005927 // Determine whether we've seen an ivar with a name similar to the
5928 // property.
Douglas Gregore8426052011-04-18 14:40:46 +00005929 if ((PropertyName == Ivar->getIdentifier() ||
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005930 NameWithPrefix == Ivar->getName() ||
Douglas Gregore8426052011-04-18 14:40:46 +00005931 NameWithSuffix == Ivar->getName())) {
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005932 SawSimilarlyNamedIvar = true;
Douglas Gregore8426052011-04-18 14:40:46 +00005933
5934 // Reduce the priority of this result by one, to give it a slight
5935 // advantage over other results whose names don't match so closely.
5936 if (Results.size() &&
5937 Results.data()[Results.size() - 1].Kind
5938 == CodeCompletionResult::RK_Declaration &&
5939 Results.data()[Results.size() - 1].Declaration == Ivar)
5940 Results.data()[Results.size() - 1].Priority--;
5941 }
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005942 }
Douglas Gregor322328b2009-11-18 22:32:06 +00005943 }
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005944
5945 if (!SawSimilarlyNamedIvar) {
5946 // Create ivar result _propName, that the user can use to synthesize
Douglas Gregore8426052011-04-18 14:40:46 +00005947 // an ivar of the appropriate type.
5948 unsigned Priority = CCP_MemberDeclaration + 1;
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005949 typedef CodeCompletionResult Result;
5950 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00005951 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(),
5952 Priority,CXAvailability_Available);
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005953
Douglas Gregor8987b232011-09-27 23:30:47 +00005954 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregore8426052011-04-18 14:40:46 +00005955 Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00005956 Policy, Allocator));
Douglas Gregoraa490cb2011-04-18 14:13:53 +00005957 Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix));
5958 Results.AddResult(Result(Builder.TakeString(), Priority,
5959 CXCursor_ObjCIvarDecl));
5960 }
5961
Douglas Gregor322328b2009-11-18 22:32:06 +00005962 Results.ExitScope();
5963
Douglas Gregore6b1bb62010-08-11 21:23:17 +00005964 HandleCodeCompleteResults(this, CodeCompleter,
5965 CodeCompletionContext::CCC_Other,
5966 Results.data(),Results.size());
Douglas Gregor322328b2009-11-18 22:32:06 +00005967}
Douglas Gregore8f5a172010-04-07 00:21:17 +00005968
Douglas Gregor408be5a2010-08-25 01:08:01 +00005969// Mapping from selectors to the methods that implement that selector, along
5970// with the "in original class" flag.
5971typedef llvm::DenseMap<Selector, std::pair<ObjCMethodDecl *, bool> >
5972 KnownMethodsMap;
Douglas Gregore8f5a172010-04-07 00:21:17 +00005973
5974/// \brief Find all of the methods that reside in the given container
5975/// (and its superclasses, protocols, etc.) that meet the given
5976/// criteria. Insert those methods into the map of known methods,
5977/// indexed by selector so they can be easily found.
5978static void FindImplementableMethods(ASTContext &Context,
5979 ObjCContainerDecl *Container,
5980 bool WantInstanceMethods,
5981 QualType ReturnType,
Douglas Gregor408be5a2010-08-25 01:08:01 +00005982 KnownMethodsMap &KnownMethods,
5983 bool InOriginalClass = true) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00005984 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) {
Douglas Gregorb92a4082012-06-12 13:44:08 +00005985 // Make sure we have a definition; that's what we'll walk.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00005986 if (!IFace->hasDefinition())
5987 return;
Douglas Gregorb92a4082012-06-12 13:44:08 +00005988
5989 IFace = IFace->getDefinition();
5990 Container = IFace;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00005991
Douglas Gregore8f5a172010-04-07 00:21:17 +00005992 const ObjCList<ObjCProtocolDecl> &Protocols
5993 = IFace->getReferencedProtocols();
5994 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
Douglas Gregorea766182010-10-18 18:21:28 +00005995 E = Protocols.end();
Douglas Gregore8f5a172010-04-07 00:21:17 +00005996 I != E; ++I)
5997 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
Douglas Gregorea766182010-10-18 18:21:28 +00005998 KnownMethods, InOriginalClass);
Douglas Gregore8f5a172010-04-07 00:21:17 +00005999
Douglas Gregorea766182010-10-18 18:21:28 +00006000 // Add methods from any class extensions and categories.
6001 for (const ObjCCategoryDecl *Cat = IFace->getCategoryList(); Cat;
6002 Cat = Cat->getNextClassCategory())
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00006003 FindImplementableMethods(Context, const_cast<ObjCCategoryDecl*>(Cat),
6004 WantInstanceMethods, ReturnType,
Douglas Gregorea766182010-10-18 18:21:28 +00006005 KnownMethods, false);
6006
6007 // Visit the superclass.
6008 if (IFace->getSuperClass())
6009 FindImplementableMethods(Context, IFace->getSuperClass(),
6010 WantInstanceMethods, ReturnType,
6011 KnownMethods, false);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006012 }
6013
6014 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
6015 // Recurse into protocols.
6016 const ObjCList<ObjCProtocolDecl> &Protocols
6017 = Category->getReferencedProtocols();
6018 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
Douglas Gregorea766182010-10-18 18:21:28 +00006019 E = Protocols.end();
Douglas Gregore8f5a172010-04-07 00:21:17 +00006020 I != E; ++I)
6021 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
Douglas Gregorea766182010-10-18 18:21:28 +00006022 KnownMethods, InOriginalClass);
6023
6024 // If this category is the original class, jump to the interface.
6025 if (InOriginalClass && Category->getClassInterface())
6026 FindImplementableMethods(Context, Category->getClassInterface(),
6027 WantInstanceMethods, ReturnType, KnownMethods,
6028 false);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006029 }
6030
6031 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
Douglas Gregorb92a4082012-06-12 13:44:08 +00006032 // Make sure we have a definition; that's what we'll walk.
6033 if (!Protocol->hasDefinition())
6034 return;
6035 Protocol = Protocol->getDefinition();
6036 Container = Protocol;
6037
6038 // Recurse into protocols.
6039 const ObjCList<ObjCProtocolDecl> &Protocols
6040 = Protocol->getReferencedProtocols();
6041 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6042 E = Protocols.end();
6043 I != E; ++I)
6044 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6045 KnownMethods, false);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006046 }
6047
6048 // Add methods in this container. This operation occurs last because
6049 // we want the methods from this container to override any methods
6050 // we've previously seen with the same selector.
6051 for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
6052 MEnd = Container->meth_end();
6053 M != MEnd; ++M) {
David Blaikie262bc182012-04-30 02:36:29 +00006054 if (M->isInstanceMethod() == WantInstanceMethods) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006055 if (!ReturnType.isNull() &&
David Blaikie262bc182012-04-30 02:36:29 +00006056 !Context.hasSameUnqualifiedType(ReturnType, M->getResultType()))
Douglas Gregore8f5a172010-04-07 00:21:17 +00006057 continue;
6058
David Blaikie581deb32012-06-06 20:45:41 +00006059 KnownMethods[M->getSelector()] = std::make_pair(*M, InOriginalClass);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006060 }
6061 }
6062}
6063
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006064/// \brief Add the parenthesized return or parameter type chunk to a code
6065/// completion string.
6066static void AddObjCPassingTypeChunk(QualType Type,
Douglas Gregor90f5f472012-04-10 18:35:07 +00006067 unsigned ObjCDeclQuals,
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006068 ASTContext &Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00006069 const PrintingPolicy &Policy,
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006070 CodeCompletionBuilder &Builder) {
6071 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregor90f5f472012-04-10 18:35:07 +00006072 std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals);
6073 if (!Quals.empty())
6074 Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals));
Douglas Gregor8987b232011-09-27 23:30:47 +00006075 Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy,
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006076 Builder.getAllocator()));
6077 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6078}
6079
6080/// \brief Determine whether the given class is or inherits from a class by
6081/// the given name.
6082static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class,
Chris Lattner5f9e2722011-07-23 10:55:15 +00006083 StringRef Name) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006084 if (!Class)
6085 return false;
6086
6087 if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name)
6088 return true;
6089
6090 return InheritsFromClassNamed(Class->getSuperClass(), Name);
6091}
6092
6093/// \brief Add code completions for Objective-C Key-Value Coding (KVC) and
6094/// Key-Value Observing (KVO).
6095static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property,
6096 bool IsInstanceMethod,
6097 QualType ReturnType,
6098 ASTContext &Context,
Douglas Gregore74c25c2011-05-04 23:50:46 +00006099 VisitedSelectorSet &KnownSelectors,
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006100 ResultBuilder &Results) {
6101 IdentifierInfo *PropName = Property->getIdentifier();
6102 if (!PropName || PropName->getLength() == 0)
6103 return;
6104
Douglas Gregor8987b232011-09-27 23:30:47 +00006105 PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
6106
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006107 // Builder that will create each code completion.
6108 typedef CodeCompletionResult Result;
6109 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006110 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006111
6112 // The selector table.
6113 SelectorTable &Selectors = Context.Selectors;
6114
6115 // The property name, copied into the code completion allocation region
6116 // on demand.
6117 struct KeyHolder {
6118 CodeCompletionAllocator &Allocator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006119 StringRef Key;
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006120 const char *CopiedKey;
6121
Chris Lattner5f9e2722011-07-23 10:55:15 +00006122 KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key)
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006123 : Allocator(Allocator), Key(Key), CopiedKey(0) { }
6124
6125 operator const char *() {
6126 if (CopiedKey)
6127 return CopiedKey;
6128
6129 return CopiedKey = Allocator.CopyString(Key);
6130 }
6131 } Key(Allocator, PropName->getName());
6132
6133 // The uppercased name of the property name.
6134 std::string UpperKey = PropName->getName();
6135 if (!UpperKey.empty())
6136 UpperKey[0] = toupper(UpperKey[0]);
6137
6138 bool ReturnTypeMatchesProperty = ReturnType.isNull() ||
6139 Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(),
6140 Property->getType());
6141 bool ReturnTypeMatchesVoid
6142 = ReturnType.isNull() || ReturnType->isVoidType();
6143
6144 // Add the normal accessor -(type)key.
6145 if (IsInstanceMethod &&
Douglas Gregore74c25c2011-05-04 23:50:46 +00006146 KnownSelectors.insert(Selectors.getNullarySelector(PropName)) &&
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006147 ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) {
6148 if (ReturnType.isNull())
Douglas Gregor90f5f472012-04-10 18:35:07 +00006149 AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6150 Context, Policy, Builder);
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006151
6152 Builder.AddTypedTextChunk(Key);
6153 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6154 CXCursor_ObjCInstanceMethodDecl));
6155 }
6156
6157 // If we have an integral or boolean property (or the user has provided
6158 // an integral or boolean return type), add the accessor -(type)isKey.
6159 if (IsInstanceMethod &&
6160 ((!ReturnType.isNull() &&
6161 (ReturnType->isIntegerType() || ReturnType->isBooleanType())) ||
6162 (ReturnType.isNull() &&
6163 (Property->getType()->isIntegerType() ||
6164 Property->getType()->isBooleanType())))) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006165 std::string SelectorName = (Twine("is") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006166 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006167 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006168 if (ReturnType.isNull()) {
6169 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6170 Builder.AddTextChunk("BOOL");
6171 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6172 }
6173
6174 Builder.AddTypedTextChunk(
6175 Allocator.CopyString(SelectorId->getName()));
6176 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6177 CXCursor_ObjCInstanceMethodDecl));
6178 }
6179 }
6180
6181 // Add the normal mutator.
6182 if (IsInstanceMethod && ReturnTypeMatchesVoid &&
6183 !Property->getSetterMethodDecl()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006184 std::string SelectorName = (Twine("set") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006185 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006186 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006187 if (ReturnType.isNull()) {
6188 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6189 Builder.AddTextChunk("void");
6190 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6191 }
6192
6193 Builder.AddTypedTextChunk(
6194 Allocator.CopyString(SelectorId->getName()));
6195 Builder.AddTypedTextChunk(":");
Douglas Gregor90f5f472012-04-10 18:35:07 +00006196 AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6197 Context, Policy, Builder);
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006198 Builder.AddTextChunk(Key);
6199 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6200 CXCursor_ObjCInstanceMethodDecl));
6201 }
6202 }
6203
6204 // Indexed and unordered accessors
6205 unsigned IndexedGetterPriority = CCP_CodePattern;
6206 unsigned IndexedSetterPriority = CCP_CodePattern;
6207 unsigned UnorderedGetterPriority = CCP_CodePattern;
6208 unsigned UnorderedSetterPriority = CCP_CodePattern;
6209 if (const ObjCObjectPointerType *ObjCPointer
6210 = Property->getType()->getAs<ObjCObjectPointerType>()) {
6211 if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) {
6212 // If this interface type is not provably derived from a known
6213 // collection, penalize the corresponding completions.
6214 if (!InheritsFromClassNamed(IFace, "NSMutableArray")) {
6215 IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6216 if (!InheritsFromClassNamed(IFace, "NSArray"))
6217 IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6218 }
6219
6220 if (!InheritsFromClassNamed(IFace, "NSMutableSet")) {
6221 UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6222 if (!InheritsFromClassNamed(IFace, "NSSet"))
6223 UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6224 }
6225 }
6226 } else {
6227 IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6228 IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6229 UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6230 UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6231 }
6232
6233 // Add -(NSUInteger)countOf<key>
6234 if (IsInstanceMethod &&
6235 (ReturnType.isNull() || ReturnType->isIntegerType())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006236 std::string SelectorName = (Twine("countOf") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006237 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006238 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006239 if (ReturnType.isNull()) {
6240 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6241 Builder.AddTextChunk("NSUInteger");
6242 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6243 }
6244
6245 Builder.AddTypedTextChunk(
6246 Allocator.CopyString(SelectorId->getName()));
6247 Results.AddResult(Result(Builder.TakeString(),
6248 std::min(IndexedGetterPriority,
6249 UnorderedGetterPriority),
6250 CXCursor_ObjCInstanceMethodDecl));
6251 }
6252 }
6253
6254 // Indexed getters
6255 // Add -(id)objectInKeyAtIndex:(NSUInteger)index
6256 if (IsInstanceMethod &&
6257 (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
Douglas Gregor62041592011-02-17 03:19:26 +00006258 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006259 = (Twine("objectIn") + UpperKey + "AtIndex").str();
Douglas Gregor62041592011-02-17 03:19:26 +00006260 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006261 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006262 if (ReturnType.isNull()) {
6263 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6264 Builder.AddTextChunk("id");
6265 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6266 }
6267
6268 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6269 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6270 Builder.AddTextChunk("NSUInteger");
6271 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6272 Builder.AddTextChunk("index");
6273 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6274 CXCursor_ObjCInstanceMethodDecl));
6275 }
6276 }
6277
6278 // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes
6279 if (IsInstanceMethod &&
6280 (ReturnType.isNull() ||
6281 (ReturnType->isObjCObjectPointerType() &&
6282 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6283 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6284 ->getName() == "NSArray"))) {
Douglas Gregor62041592011-02-17 03:19:26 +00006285 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006286 = (Twine(Property->getName()) + "AtIndexes").str();
Douglas Gregor62041592011-02-17 03:19:26 +00006287 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006288 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006289 if (ReturnType.isNull()) {
6290 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6291 Builder.AddTextChunk("NSArray *");
6292 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6293 }
6294
6295 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6296 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6297 Builder.AddTextChunk("NSIndexSet *");
6298 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6299 Builder.AddTextChunk("indexes");
6300 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6301 CXCursor_ObjCInstanceMethodDecl));
6302 }
6303 }
6304
6305 // Add -(void)getKey:(type **)buffer range:(NSRange)inRange
6306 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006307 std::string SelectorName = (Twine("get") + UpperKey).str();
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006308 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor62041592011-02-17 03:19:26 +00006309 &Context.Idents.get(SelectorName),
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006310 &Context.Idents.get("range")
6311 };
6312
Douglas Gregore74c25c2011-05-04 23:50:46 +00006313 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006314 if (ReturnType.isNull()) {
6315 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6316 Builder.AddTextChunk("void");
6317 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6318 }
6319
6320 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6321 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6322 Builder.AddPlaceholderChunk("object-type");
6323 Builder.AddTextChunk(" **");
6324 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6325 Builder.AddTextChunk("buffer");
6326 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6327 Builder.AddTypedTextChunk("range:");
6328 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6329 Builder.AddTextChunk("NSRange");
6330 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6331 Builder.AddTextChunk("inRange");
6332 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6333 CXCursor_ObjCInstanceMethodDecl));
6334 }
6335 }
6336
6337 // Mutable indexed accessors
6338
6339 // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index
6340 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006341 std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str();
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006342 IdentifierInfo *SelectorIds[2] = {
6343 &Context.Idents.get("insertObject"),
Douglas Gregor62041592011-02-17 03:19:26 +00006344 &Context.Idents.get(SelectorName)
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006345 };
6346
Douglas Gregore74c25c2011-05-04 23:50:46 +00006347 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006348 if (ReturnType.isNull()) {
6349 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6350 Builder.AddTextChunk("void");
6351 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6352 }
6353
6354 Builder.AddTypedTextChunk("insertObject:");
6355 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6356 Builder.AddPlaceholderChunk("object-type");
6357 Builder.AddTextChunk(" *");
6358 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6359 Builder.AddTextChunk("object");
6360 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6361 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6362 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6363 Builder.AddPlaceholderChunk("NSUInteger");
6364 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6365 Builder.AddTextChunk("index");
6366 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6367 CXCursor_ObjCInstanceMethodDecl));
6368 }
6369 }
6370
6371 // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes
6372 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006373 std::string SelectorName = (Twine("insert") + UpperKey).str();
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006374 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor62041592011-02-17 03:19:26 +00006375 &Context.Idents.get(SelectorName),
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006376 &Context.Idents.get("atIndexes")
6377 };
6378
Douglas Gregore74c25c2011-05-04 23:50:46 +00006379 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006380 if (ReturnType.isNull()) {
6381 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6382 Builder.AddTextChunk("void");
6383 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6384 }
6385
6386 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6387 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6388 Builder.AddTextChunk("NSArray *");
6389 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6390 Builder.AddTextChunk("array");
6391 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6392 Builder.AddTypedTextChunk("atIndexes:");
6393 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6394 Builder.AddPlaceholderChunk("NSIndexSet *");
6395 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6396 Builder.AddTextChunk("indexes");
6397 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6398 CXCursor_ObjCInstanceMethodDecl));
6399 }
6400 }
6401
6402 // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index
6403 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006404 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006405 = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str();
Douglas Gregor62041592011-02-17 03:19:26 +00006406 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006407 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006408 if (ReturnType.isNull()) {
6409 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6410 Builder.AddTextChunk("void");
6411 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6412 }
6413
6414 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6415 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6416 Builder.AddTextChunk("NSUInteger");
6417 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6418 Builder.AddTextChunk("index");
6419 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6420 CXCursor_ObjCInstanceMethodDecl));
6421 }
6422 }
6423
6424 // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes
6425 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006426 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006427 = (Twine("remove") + UpperKey + "AtIndexes").str();
Douglas Gregor62041592011-02-17 03:19:26 +00006428 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006429 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006430 if (ReturnType.isNull()) {
6431 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6432 Builder.AddTextChunk("void");
6433 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6434 }
6435
6436 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6437 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6438 Builder.AddTextChunk("NSIndexSet *");
6439 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6440 Builder.AddTextChunk("indexes");
6441 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6442 CXCursor_ObjCInstanceMethodDecl));
6443 }
6444 }
6445
6446 // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object
6447 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006448 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006449 = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str();
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006450 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor62041592011-02-17 03:19:26 +00006451 &Context.Idents.get(SelectorName),
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006452 &Context.Idents.get("withObject")
6453 };
6454
Douglas Gregore74c25c2011-05-04 23:50:46 +00006455 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006456 if (ReturnType.isNull()) {
6457 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6458 Builder.AddTextChunk("void");
6459 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6460 }
6461
6462 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6463 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6464 Builder.AddPlaceholderChunk("NSUInteger");
6465 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6466 Builder.AddTextChunk("index");
6467 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6468 Builder.AddTypedTextChunk("withObject:");
6469 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6470 Builder.AddTextChunk("id");
6471 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6472 Builder.AddTextChunk("object");
6473 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6474 CXCursor_ObjCInstanceMethodDecl));
6475 }
6476 }
6477
6478 // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array
6479 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006480 std::string SelectorName1
Chris Lattner5f9e2722011-07-23 10:55:15 +00006481 = (Twine("replace") + UpperKey + "AtIndexes").str();
6482 std::string SelectorName2 = (Twine("with") + UpperKey).str();
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006483 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor62041592011-02-17 03:19:26 +00006484 &Context.Idents.get(SelectorName1),
6485 &Context.Idents.get(SelectorName2)
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006486 };
6487
Douglas Gregore74c25c2011-05-04 23:50:46 +00006488 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006489 if (ReturnType.isNull()) {
6490 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6491 Builder.AddTextChunk("void");
6492 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6493 }
6494
6495 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":"));
6496 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6497 Builder.AddPlaceholderChunk("NSIndexSet *");
6498 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6499 Builder.AddTextChunk("indexes");
6500 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6501 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":"));
6502 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6503 Builder.AddTextChunk("NSArray *");
6504 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6505 Builder.AddTextChunk("array");
6506 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6507 CXCursor_ObjCInstanceMethodDecl));
6508 }
6509 }
6510
6511 // Unordered getters
6512 // - (NSEnumerator *)enumeratorOfKey
6513 if (IsInstanceMethod &&
6514 (ReturnType.isNull() ||
6515 (ReturnType->isObjCObjectPointerType() &&
6516 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6517 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6518 ->getName() == "NSEnumerator"))) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006519 std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006520 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006521 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006522 if (ReturnType.isNull()) {
6523 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6524 Builder.AddTextChunk("NSEnumerator *");
6525 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6526 }
6527
6528 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6529 Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6530 CXCursor_ObjCInstanceMethodDecl));
6531 }
6532 }
6533
6534 // - (type *)memberOfKey:(type *)object
6535 if (IsInstanceMethod &&
6536 (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006537 std::string SelectorName = (Twine("memberOf") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006538 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006539 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006540 if (ReturnType.isNull()) {
6541 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6542 Builder.AddPlaceholderChunk("object-type");
6543 Builder.AddTextChunk(" *");
6544 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6545 }
6546
6547 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6548 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6549 if (ReturnType.isNull()) {
6550 Builder.AddPlaceholderChunk("object-type");
6551 Builder.AddTextChunk(" *");
6552 } else {
6553 Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context,
Douglas Gregor8987b232011-09-27 23:30:47 +00006554 Policy,
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006555 Builder.getAllocator()));
6556 }
6557 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6558 Builder.AddTextChunk("object");
6559 Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6560 CXCursor_ObjCInstanceMethodDecl));
6561 }
6562 }
6563
6564 // Mutable unordered accessors
6565 // - (void)addKeyObject:(type *)object
6566 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006567 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006568 = (Twine("add") + UpperKey + Twine("Object")).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006569 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006570 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006571 if (ReturnType.isNull()) {
6572 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6573 Builder.AddTextChunk("void");
6574 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6575 }
6576
6577 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6578 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6579 Builder.AddPlaceholderChunk("object-type");
6580 Builder.AddTextChunk(" *");
6581 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6582 Builder.AddTextChunk("object");
6583 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6584 CXCursor_ObjCInstanceMethodDecl));
6585 }
6586 }
6587
6588 // - (void)addKey:(NSSet *)objects
6589 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006590 std::string SelectorName = (Twine("add") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006591 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006592 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006593 if (ReturnType.isNull()) {
6594 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6595 Builder.AddTextChunk("void");
6596 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6597 }
6598
6599 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6600 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6601 Builder.AddTextChunk("NSSet *");
6602 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6603 Builder.AddTextChunk("objects");
6604 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6605 CXCursor_ObjCInstanceMethodDecl));
6606 }
6607 }
6608
6609 // - (void)removeKeyObject:(type *)object
6610 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor62041592011-02-17 03:19:26 +00006611 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006612 = (Twine("remove") + UpperKey + Twine("Object")).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006613 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006614 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006615 if (ReturnType.isNull()) {
6616 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6617 Builder.AddTextChunk("void");
6618 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6619 }
6620
6621 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6622 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6623 Builder.AddPlaceholderChunk("object-type");
6624 Builder.AddTextChunk(" *");
6625 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6626 Builder.AddTextChunk("object");
6627 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6628 CXCursor_ObjCInstanceMethodDecl));
6629 }
6630 }
6631
6632 // - (void)removeKey:(NSSet *)objects
6633 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006634 std::string SelectorName = (Twine("remove") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006635 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006636 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006637 if (ReturnType.isNull()) {
6638 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6639 Builder.AddTextChunk("void");
6640 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6641 }
6642
6643 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6644 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6645 Builder.AddTextChunk("NSSet *");
6646 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6647 Builder.AddTextChunk("objects");
6648 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6649 CXCursor_ObjCInstanceMethodDecl));
6650 }
6651 }
6652
6653 // - (void)intersectKey:(NSSet *)objects
6654 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006655 std::string SelectorName = (Twine("intersect") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006656 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006657 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006658 if (ReturnType.isNull()) {
6659 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6660 Builder.AddTextChunk("void");
6661 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6662 }
6663
6664 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6665 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6666 Builder.AddTextChunk("NSSet *");
6667 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6668 Builder.AddTextChunk("objects");
6669 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6670 CXCursor_ObjCInstanceMethodDecl));
6671 }
6672 }
6673
6674 // Key-Value Observing
6675 // + (NSSet *)keyPathsForValuesAffectingKey
6676 if (!IsInstanceMethod &&
6677 (ReturnType.isNull() ||
6678 (ReturnType->isObjCObjectPointerType() &&
6679 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6680 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6681 ->getName() == "NSSet"))) {
Douglas Gregor62041592011-02-17 03:19:26 +00006682 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006683 = (Twine("keyPathsForValuesAffecting") + UpperKey).str();
Douglas Gregor62041592011-02-17 03:19:26 +00006684 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
Douglas Gregore74c25c2011-05-04 23:50:46 +00006685 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006686 if (ReturnType.isNull()) {
6687 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6688 Builder.AddTextChunk("NSSet *");
6689 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6690 }
6691
6692 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6693 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
Douglas Gregor3f828d12011-06-02 04:02:27 +00006694 CXCursor_ObjCClassMethodDecl));
6695 }
6696 }
6697
6698 // + (BOOL)automaticallyNotifiesObserversForKey
6699 if (!IsInstanceMethod &&
6700 (ReturnType.isNull() ||
6701 ReturnType->isIntegerType() ||
6702 ReturnType->isBooleanType())) {
6703 std::string SelectorName
Chris Lattner5f9e2722011-07-23 10:55:15 +00006704 = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str();
Douglas Gregor3f828d12011-06-02 04:02:27 +00006705 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6706 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
6707 if (ReturnType.isNull()) {
6708 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6709 Builder.AddTextChunk("BOOL");
6710 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6711 }
6712
6713 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6714 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6715 CXCursor_ObjCClassMethodDecl));
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006716 }
6717 }
6718}
6719
Douglas Gregore8f5a172010-04-07 00:21:17 +00006720void Sema::CodeCompleteObjCMethodDecl(Scope *S,
6721 bool IsInstanceMethod,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00006722 ParsedType ReturnTy) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006723 // Determine the return type of the method we're declaring, if
6724 // provided.
6725 QualType ReturnType = GetTypeFromParser(ReturnTy);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00006726 Decl *IDecl = 0;
6727 if (CurContext->isObjCContainer()) {
6728 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
6729 IDecl = cast<Decl>(OCD);
6730 }
Douglas Gregorea766182010-10-18 18:21:28 +00006731 // Determine where we should start searching for methods.
6732 ObjCContainerDecl *SearchDecl = 0;
Douglas Gregore8f5a172010-04-07 00:21:17 +00006733 bool IsInImplementation = false;
John McCalld226f652010-08-21 09:40:31 +00006734 if (Decl *D = IDecl) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006735 if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) {
6736 SearchDecl = Impl->getClassInterface();
Douglas Gregore8f5a172010-04-07 00:21:17 +00006737 IsInImplementation = true;
6738 } else if (ObjCCategoryImplDecl *CatImpl
Douglas Gregorea766182010-10-18 18:21:28 +00006739 = dyn_cast<ObjCCategoryImplDecl>(D)) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006740 SearchDecl = CatImpl->getCategoryDecl();
Douglas Gregore8f5a172010-04-07 00:21:17 +00006741 IsInImplementation = true;
Douglas Gregorea766182010-10-18 18:21:28 +00006742 } else
Douglas Gregore8f5a172010-04-07 00:21:17 +00006743 SearchDecl = dyn_cast<ObjCContainerDecl>(D);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006744 }
6745
6746 if (!SearchDecl && S) {
Douglas Gregorea766182010-10-18 18:21:28 +00006747 if (DeclContext *DC = static_cast<DeclContext *>(S->getEntity()))
Douglas Gregore8f5a172010-04-07 00:21:17 +00006748 SearchDecl = dyn_cast<ObjCContainerDecl>(DC);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006749 }
6750
Douglas Gregorea766182010-10-18 18:21:28 +00006751 if (!SearchDecl) {
Douglas Gregore6b1bb62010-08-11 21:23:17 +00006752 HandleCodeCompleteResults(this, CodeCompleter,
6753 CodeCompletionContext::CCC_Other,
6754 0, 0);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006755 return;
6756 }
6757
6758 // Find all of the methods that we could declare/implement here.
6759 KnownMethodsMap KnownMethods;
6760 FindImplementableMethods(Context, SearchDecl, IsInstanceMethod,
Douglas Gregorea766182010-10-18 18:21:28 +00006761 ReturnType, KnownMethods);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006762
Douglas Gregore8f5a172010-04-07 00:21:17 +00006763 // Add declarations or definitions for each of the known methods.
John McCall0a2c5e22010-08-25 06:19:51 +00006764 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00006765 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006766 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00006767 CodeCompletionContext::CCC_Other);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006768 Results.EnterNewScope();
Douglas Gregor8987b232011-09-27 23:30:47 +00006769 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006770 for (KnownMethodsMap::iterator M = KnownMethods.begin(),
6771 MEnd = KnownMethods.end();
6772 M != MEnd; ++M) {
Douglas Gregor408be5a2010-08-25 01:08:01 +00006773 ObjCMethodDecl *Method = M->second.first;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006774 CodeCompletionBuilder Builder(Results.getAllocator(),
6775 Results.getCodeCompletionTUInfo());
Douglas Gregore8f5a172010-04-07 00:21:17 +00006776
6777 // If the result type was not already provided, add it to the
6778 // pattern as (type).
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006779 if (ReturnType.isNull())
Douglas Gregor90f5f472012-04-10 18:35:07 +00006780 AddObjCPassingTypeChunk(Method->getResultType(),
6781 Method->getObjCDeclQualifier(),
6782 Context, Policy,
6783 Builder);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006784
6785 Selector Sel = Method->getSelector();
6786
6787 // Add the first part of the selector to the pattern.
Douglas Gregordae68752011-02-01 22:57:45 +00006788 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor813d8342011-02-18 22:29:55 +00006789 Sel.getNameForSlot(0)));
Douglas Gregore8f5a172010-04-07 00:21:17 +00006790
6791 // Add parameters to the pattern.
6792 unsigned I = 0;
6793 for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
6794 PEnd = Method->param_end();
6795 P != PEnd; (void)++P, ++I) {
6796 // Add the part of the selector name.
6797 if (I == 0)
Douglas Gregor218937c2011-02-01 19:23:04 +00006798 Builder.AddTypedTextChunk(":");
Douglas Gregore8f5a172010-04-07 00:21:17 +00006799 else if (I < Sel.getNumArgs()) {
Douglas Gregor218937c2011-02-01 19:23:04 +00006800 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6801 Builder.AddTypedTextChunk(
Douglas Gregor813d8342011-02-18 22:29:55 +00006802 Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":"));
Douglas Gregore8f5a172010-04-07 00:21:17 +00006803 } else
6804 break;
6805
6806 // Add the parameter type.
Douglas Gregor90f5f472012-04-10 18:35:07 +00006807 AddObjCPassingTypeChunk((*P)->getOriginalType(),
6808 (*P)->getObjCDeclQualifier(),
6809 Context, Policy,
Douglas Gregor8987b232011-09-27 23:30:47 +00006810 Builder);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006811
6812 if (IdentifierInfo *Id = (*P)->getIdentifier())
Douglas Gregordae68752011-02-01 22:57:45 +00006813 Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName()));
Douglas Gregore8f5a172010-04-07 00:21:17 +00006814 }
6815
6816 if (Method->isVariadic()) {
6817 if (Method->param_size() > 0)
Douglas Gregor218937c2011-02-01 19:23:04 +00006818 Builder.AddChunk(CodeCompletionString::CK_Comma);
6819 Builder.AddTextChunk("...");
Douglas Gregore17794f2010-08-31 05:13:43 +00006820 }
Douglas Gregore8f5a172010-04-07 00:21:17 +00006821
Douglas Gregor447107d2010-05-28 00:57:46 +00006822 if (IsInImplementation && Results.includeCodePatterns()) {
Douglas Gregore8f5a172010-04-07 00:21:17 +00006823 // We will be defining the method here, so add a compound statement.
Douglas Gregor218937c2011-02-01 19:23:04 +00006824 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6825 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
6826 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006827 if (!Method->getResultType()->isVoidType()) {
6828 // If the result type is not void, add a return clause.
Douglas Gregor218937c2011-02-01 19:23:04 +00006829 Builder.AddTextChunk("return");
6830 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6831 Builder.AddPlaceholderChunk("expression");
6832 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006833 } else
Douglas Gregor218937c2011-02-01 19:23:04 +00006834 Builder.AddPlaceholderChunk("statements");
Douglas Gregore8f5a172010-04-07 00:21:17 +00006835
Douglas Gregor218937c2011-02-01 19:23:04 +00006836 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
6837 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregore8f5a172010-04-07 00:21:17 +00006838 }
6839
Douglas Gregor408be5a2010-08-25 01:08:01 +00006840 unsigned Priority = CCP_CodePattern;
6841 if (!M->second.second)
6842 Priority += CCD_InBaseClass;
6843
Douglas Gregorba103062012-03-27 23:34:16 +00006844 Results.AddResult(Result(Builder.TakeString(), Method, Priority));
Douglas Gregore8f5a172010-04-07 00:21:17 +00006845 }
6846
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006847 // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of
6848 // the properties in this class and its categories.
David Blaikie4e4d0842012-03-11 07:00:24 +00006849 if (Context.getLangOpts().ObjC2) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00006850 SmallVector<ObjCContainerDecl *, 4> Containers;
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006851 Containers.push_back(SearchDecl);
6852
Douglas Gregore74c25c2011-05-04 23:50:46 +00006853 VisitedSelectorSet KnownSelectors;
6854 for (KnownMethodsMap::iterator M = KnownMethods.begin(),
6855 MEnd = KnownMethods.end();
6856 M != MEnd; ++M)
6857 KnownSelectors.insert(M->first);
6858
6859
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006860 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl);
6861 if (!IFace)
6862 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl))
6863 IFace = Category->getClassInterface();
6864
6865 if (IFace) {
6866 for (ObjCCategoryDecl *Category = IFace->getCategoryList(); Category;
6867 Category = Category->getNextClassCategory())
6868 Containers.push_back(Category);
6869 }
6870
6871 for (unsigned I = 0, N = Containers.size(); I != N; ++I) {
6872 for (ObjCContainerDecl::prop_iterator P = Containers[I]->prop_begin(),
6873 PEnd = Containers[I]->prop_end();
6874 P != PEnd; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00006875 AddObjCKeyValueCompletions(*P, IsInstanceMethod, ReturnType, Context,
Douglas Gregore74c25c2011-05-04 23:50:46 +00006876 KnownSelectors, Results);
Douglas Gregor577cdfd2011-02-17 00:22:45 +00006877 }
6878 }
6879 }
6880
Douglas Gregore8f5a172010-04-07 00:21:17 +00006881 Results.ExitScope();
6882
Douglas Gregore6b1bb62010-08-11 21:23:17 +00006883 HandleCodeCompleteResults(this, CodeCompleter,
6884 CodeCompletionContext::CCC_Other,
6885 Results.data(),Results.size());
Douglas Gregore8f5a172010-04-07 00:21:17 +00006886}
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006887
6888void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S,
6889 bool IsInstanceMethod,
Douglas Gregor40ed9a12010-07-08 23:37:41 +00006890 bool AtParameterName,
John McCallb3d87482010-08-24 05:47:05 +00006891 ParsedType ReturnTy,
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006892 IdentifierInfo **SelIdents,
6893 unsigned NumSelIdents) {
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006894 // If we have an external source, load the entire class method
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006895 // pool from the AST file.
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006896 if (ExternalSource) {
6897 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
6898 I != N; ++I) {
6899 Selector Sel = ExternalSource->GetExternalSelector(I);
Sebastian Redldb9d2142010-08-02 23:18:59 +00006900 if (Sel.isNull() || MethodPool.count(Sel))
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006901 continue;
Sebastian Redldb9d2142010-08-02 23:18:59 +00006902
6903 ReadMethodPool(Sel);
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006904 }
6905 }
6906
6907 // Build the set of methods we can see.
John McCall0a2c5e22010-08-25 06:19:51 +00006908 typedef CodeCompletionResult Result;
Douglas Gregor218937c2011-02-01 19:23:04 +00006909 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006910 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor218937c2011-02-01 19:23:04 +00006911 CodeCompletionContext::CCC_Other);
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006912
6913 if (ReturnTy)
6914 Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType());
Sebastian Redldb9d2142010-08-02 23:18:59 +00006915
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006916 Results.EnterNewScope();
Sebastian Redldb9d2142010-08-02 23:18:59 +00006917 for (GlobalMethodPool::iterator M = MethodPool.begin(),
6918 MEnd = MethodPool.end();
6919 M != MEnd; ++M) {
6920 for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first :
6921 &M->second.second;
6922 MethList && MethList->Method;
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006923 MethList = MethList->Next) {
6924 if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
6925 NumSelIdents))
6926 continue;
6927
Douglas Gregor40ed9a12010-07-08 23:37:41 +00006928 if (AtParameterName) {
6929 // Suggest parameter names we've seen before.
6930 if (NumSelIdents && NumSelIdents <= MethList->Method->param_size()) {
6931 ParmVarDecl *Param = MethList->Method->param_begin()[NumSelIdents-1];
6932 if (Param->getIdentifier()) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006933 CodeCompletionBuilder Builder(Results.getAllocator(),
6934 Results.getCodeCompletionTUInfo());
Douglas Gregordae68752011-02-01 22:57:45 +00006935 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00006936 Param->getIdentifier()->getName()));
6937 Results.AddResult(Builder.TakeString());
Douglas Gregor40ed9a12010-07-08 23:37:41 +00006938 }
6939 }
6940
6941 continue;
6942 }
6943
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006944 Result R(MethList->Method, 0);
6945 R.StartParameter = NumSelIdents;
6946 R.AllParametersAreInformative = false;
6947 R.DeclaringEntity = true;
6948 Results.MaybeAddResult(R, CurContext);
6949 }
6950 }
6951
6952 Results.ExitScope();
Douglas Gregore6b1bb62010-08-11 21:23:17 +00006953 HandleCodeCompleteResults(this, CodeCompleter,
6954 CodeCompletionContext::CCC_Other,
6955 Results.data(),Results.size());
Douglas Gregor1f5537a2010-07-08 23:20:03 +00006956}
Douglas Gregor87c08a52010-08-13 22:48:40 +00006957
Douglas Gregorf29c5232010-08-24 22:20:20 +00006958void Sema::CodeCompletePreprocessorDirective(bool InConditional) {
Douglas Gregor218937c2011-02-01 19:23:04 +00006959 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006960 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00006961 CodeCompletionContext::CCC_PreprocessorDirective);
Douglas Gregorf44e8542010-08-24 19:08:16 +00006962 Results.EnterNewScope();
6963
6964 // #if <condition>
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00006965 CodeCompletionBuilder Builder(Results.getAllocator(),
6966 Results.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00006967 Builder.AddTypedTextChunk("if");
6968 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6969 Builder.AddPlaceholderChunk("condition");
6970 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00006971
6972 // #ifdef <macro>
Douglas Gregor218937c2011-02-01 19:23:04 +00006973 Builder.AddTypedTextChunk("ifdef");
6974 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6975 Builder.AddPlaceholderChunk("macro");
6976 Results.AddResult(Builder.TakeString());
6977
Douglas Gregorf44e8542010-08-24 19:08:16 +00006978 // #ifndef <macro>
Douglas Gregor218937c2011-02-01 19:23:04 +00006979 Builder.AddTypedTextChunk("ifndef");
6980 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6981 Builder.AddPlaceholderChunk("macro");
6982 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00006983
6984 if (InConditional) {
6985 // #elif <condition>
Douglas Gregor218937c2011-02-01 19:23:04 +00006986 Builder.AddTypedTextChunk("elif");
6987 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6988 Builder.AddPlaceholderChunk("condition");
6989 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00006990
6991 // #else
Douglas Gregor218937c2011-02-01 19:23:04 +00006992 Builder.AddTypedTextChunk("else");
6993 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00006994
6995 // #endif
Douglas Gregor218937c2011-02-01 19:23:04 +00006996 Builder.AddTypedTextChunk("endif");
6997 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00006998 }
6999
7000 // #include "header"
Douglas Gregor218937c2011-02-01 19:23:04 +00007001 Builder.AddTypedTextChunk("include");
7002 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7003 Builder.AddTextChunk("\"");
7004 Builder.AddPlaceholderChunk("header");
7005 Builder.AddTextChunk("\"");
7006 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007007
7008 // #include <header>
Douglas Gregor218937c2011-02-01 19:23:04 +00007009 Builder.AddTypedTextChunk("include");
7010 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7011 Builder.AddTextChunk("<");
7012 Builder.AddPlaceholderChunk("header");
7013 Builder.AddTextChunk(">");
7014 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007015
7016 // #define <macro>
Douglas Gregor218937c2011-02-01 19:23:04 +00007017 Builder.AddTypedTextChunk("define");
7018 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7019 Builder.AddPlaceholderChunk("macro");
7020 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007021
7022 // #define <macro>(<args>)
Douglas Gregor218937c2011-02-01 19:23:04 +00007023 Builder.AddTypedTextChunk("define");
7024 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7025 Builder.AddPlaceholderChunk("macro");
7026 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7027 Builder.AddPlaceholderChunk("args");
7028 Builder.AddChunk(CodeCompletionString::CK_RightParen);
7029 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007030
7031 // #undef <macro>
Douglas Gregor218937c2011-02-01 19:23:04 +00007032 Builder.AddTypedTextChunk("undef");
7033 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7034 Builder.AddPlaceholderChunk("macro");
7035 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007036
7037 // #line <number>
Douglas Gregor218937c2011-02-01 19:23:04 +00007038 Builder.AddTypedTextChunk("line");
7039 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7040 Builder.AddPlaceholderChunk("number");
7041 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007042
7043 // #line <number> "filename"
Douglas Gregor218937c2011-02-01 19:23:04 +00007044 Builder.AddTypedTextChunk("line");
7045 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7046 Builder.AddPlaceholderChunk("number");
7047 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7048 Builder.AddTextChunk("\"");
7049 Builder.AddPlaceholderChunk("filename");
7050 Builder.AddTextChunk("\"");
7051 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007052
7053 // #error <message>
Douglas Gregor218937c2011-02-01 19:23:04 +00007054 Builder.AddTypedTextChunk("error");
7055 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7056 Builder.AddPlaceholderChunk("message");
7057 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007058
7059 // #pragma <arguments>
Douglas Gregor218937c2011-02-01 19:23:04 +00007060 Builder.AddTypedTextChunk("pragma");
7061 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7062 Builder.AddPlaceholderChunk("arguments");
7063 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007064
David Blaikie4e4d0842012-03-11 07:00:24 +00007065 if (getLangOpts().ObjC1) {
Douglas Gregorf44e8542010-08-24 19:08:16 +00007066 // #import "header"
Douglas Gregor218937c2011-02-01 19:23:04 +00007067 Builder.AddTypedTextChunk("import");
7068 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7069 Builder.AddTextChunk("\"");
7070 Builder.AddPlaceholderChunk("header");
7071 Builder.AddTextChunk("\"");
7072 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007073
7074 // #import <header>
Douglas Gregor218937c2011-02-01 19:23:04 +00007075 Builder.AddTypedTextChunk("import");
7076 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7077 Builder.AddTextChunk("<");
7078 Builder.AddPlaceholderChunk("header");
7079 Builder.AddTextChunk(">");
7080 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007081 }
7082
7083 // #include_next "header"
Douglas Gregor218937c2011-02-01 19:23:04 +00007084 Builder.AddTypedTextChunk("include_next");
7085 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7086 Builder.AddTextChunk("\"");
7087 Builder.AddPlaceholderChunk("header");
7088 Builder.AddTextChunk("\"");
7089 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007090
7091 // #include_next <header>
Douglas Gregor218937c2011-02-01 19:23:04 +00007092 Builder.AddTypedTextChunk("include_next");
7093 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7094 Builder.AddTextChunk("<");
7095 Builder.AddPlaceholderChunk("header");
7096 Builder.AddTextChunk(">");
7097 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007098
7099 // #warning <message>
Douglas Gregor218937c2011-02-01 19:23:04 +00007100 Builder.AddTypedTextChunk("warning");
7101 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7102 Builder.AddPlaceholderChunk("message");
7103 Results.AddResult(Builder.TakeString());
Douglas Gregorf44e8542010-08-24 19:08:16 +00007104
7105 // Note: #ident and #sccs are such crazy anachronisms that we don't provide
7106 // completions for them. And __include_macros is a Clang-internal extension
7107 // that we don't want to encourage anyone to use.
7108
7109 // FIXME: we don't support #assert or #unassert, so don't suggest them.
7110 Results.ExitScope();
7111
Douglas Gregorf44e8542010-08-24 19:08:16 +00007112 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor721f3592010-08-25 18:41:16 +00007113 CodeCompletionContext::CCC_PreprocessorDirective,
Douglas Gregorf44e8542010-08-24 19:08:16 +00007114 Results.data(), Results.size());
7115}
7116
7117void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) {
Douglas Gregorf29c5232010-08-24 22:20:20 +00007118 CodeCompleteOrdinaryName(S,
John McCallf312b1e2010-08-26 23:41:50 +00007119 S->getFnParent()? Sema::PCC_RecoveryInFunction
7120 : Sema::PCC_Namespace);
Douglas Gregorf44e8542010-08-24 19:08:16 +00007121}
7122
Douglas Gregorf29c5232010-08-24 22:20:20 +00007123void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) {
Douglas Gregor218937c2011-02-01 19:23:04 +00007124 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007125 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00007126 IsDefinition? CodeCompletionContext::CCC_MacroName
7127 : CodeCompletionContext::CCC_MacroNameUse);
Douglas Gregor1fbb4472010-08-24 20:21:13 +00007128 if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) {
7129 // Add just the names of macros, not their arguments.
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007130 CodeCompletionBuilder Builder(Results.getAllocator(),
7131 Results.getCodeCompletionTUInfo());
Douglas Gregor1fbb4472010-08-24 20:21:13 +00007132 Results.EnterNewScope();
7133 for (Preprocessor::macro_iterator M = PP.macro_begin(),
7134 MEnd = PP.macro_end();
7135 M != MEnd; ++M) {
Douglas Gregordae68752011-02-01 22:57:45 +00007136 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregor218937c2011-02-01 19:23:04 +00007137 M->first->getName()));
7138 Results.AddResult(Builder.TakeString());
Douglas Gregor1fbb4472010-08-24 20:21:13 +00007139 }
7140 Results.ExitScope();
7141 } else if (IsDefinition) {
7142 // FIXME: Can we detect when the user just wrote an include guard above?
7143 }
7144
Douglas Gregor52779fb2010-09-23 23:01:17 +00007145 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor1fbb4472010-08-24 20:21:13 +00007146 Results.data(), Results.size());
7147}
7148
Douglas Gregorf29c5232010-08-24 22:20:20 +00007149void Sema::CodeCompletePreprocessorExpression() {
Douglas Gregor218937c2011-02-01 19:23:04 +00007150 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007151 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor52779fb2010-09-23 23:01:17 +00007152 CodeCompletionContext::CCC_PreprocessorExpression);
Douglas Gregorf29c5232010-08-24 22:20:20 +00007153
7154 if (!CodeCompleter || CodeCompleter->includeMacros())
7155 AddMacroResults(PP, Results);
7156
7157 // defined (<macro>)
7158 Results.EnterNewScope();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007159 CodeCompletionBuilder Builder(Results.getAllocator(),
7160 Results.getCodeCompletionTUInfo());
Douglas Gregor218937c2011-02-01 19:23:04 +00007161 Builder.AddTypedTextChunk("defined");
7162 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7163 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7164 Builder.AddPlaceholderChunk("macro");
7165 Builder.AddChunk(CodeCompletionString::CK_RightParen);
7166 Results.AddResult(Builder.TakeString());
Douglas Gregorf29c5232010-08-24 22:20:20 +00007167 Results.ExitScope();
7168
7169 HandleCodeCompleteResults(this, CodeCompleter,
7170 CodeCompletionContext::CCC_PreprocessorExpression,
7171 Results.data(), Results.size());
7172}
7173
7174void Sema::CodeCompletePreprocessorMacroArgument(Scope *S,
7175 IdentifierInfo *Macro,
7176 MacroInfo *MacroInfo,
7177 unsigned Argument) {
7178 // FIXME: In the future, we could provide "overload" results, much like we
7179 // do for function calls.
7180
Argyrios Kyrtzidis5c5f03e2011-08-18 19:41:28 +00007181 // Now just ignore this. There will be another code-completion callback
7182 // for the expanded tokens.
Douglas Gregorf29c5232010-08-24 22:20:20 +00007183}
7184
Douglas Gregor55817af2010-08-25 17:04:25 +00007185void Sema::CodeCompleteNaturalLanguage() {
Douglas Gregor55817af2010-08-25 17:04:25 +00007186 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregoraf1c6b52010-08-25 17:10:00 +00007187 CodeCompletionContext::CCC_NaturalLanguage,
Douglas Gregor55817af2010-08-25 17:04:25 +00007188 0, 0);
7189}
7190
Douglas Gregordae68752011-02-01 22:57:45 +00007191void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007192 CodeCompletionTUInfo &CCTUInfo,
Chris Lattner5f9e2722011-07-23 10:55:15 +00007193 SmallVectorImpl<CodeCompletionResult> &Results) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00007194 ResultBuilder Builder(*this, Allocator, CCTUInfo,
7195 CodeCompletionContext::CCC_Recovery);
Douglas Gregor8071e422010-08-15 06:18:01 +00007196 if (!CodeCompleter || CodeCompleter->includeGlobals()) {
7197 CodeCompletionDeclConsumer Consumer(Builder,
7198 Context.getTranslationUnitDecl());
7199 LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName,
7200 Consumer);
7201 }
Douglas Gregor87c08a52010-08-13 22:48:40 +00007202
7203 if (!CodeCompleter || CodeCompleter->includeMacros())
7204 AddMacroResults(PP, Builder);
7205
7206 Results.clear();
7207 Results.insert(Results.end(),
7208 Builder.data(), Builder.data() + Builder.size());
7209}