blob: 46b538520bb038a4837389c649fcd7aab8f12b09 [file] [log] [blame]
Douglas Gregor2436e712009-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//===----------------------------------------------------------------------===//
13#include "Sema.h"
Douglas Gregorc580c522010-01-14 01:09:38 +000014#include "Lookup.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000015#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregorf2510672009-09-21 19:57:38 +000016#include "clang/AST/ExprCXX.h"
Douglas Gregor8ce33212009-11-17 17:59:40 +000017#include "clang/AST/ExprObjC.h"
Douglas Gregorf329c7c2009-10-30 16:50:04 +000018#include "clang/Lex/MacroInfo.h"
19#include "clang/Lex/Preprocessor.h"
Douglas Gregor3545ff42009-09-21 16:56:56 +000020#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregore6688e62009-09-28 03:51:44 +000021#include "llvm/ADT/StringExtras.h"
Douglas Gregor3545ff42009-09-21 16:56:56 +000022#include <list>
23#include <map>
24#include <vector>
Douglas Gregor2436e712009-09-17 21:32:03 +000025
26using namespace clang;
27
Douglas Gregor3545ff42009-09-21 16:56:56 +000028namespace {
29 /// \brief A container of code-completion results.
30 class ResultBuilder {
31 public:
32 /// \brief The type of a name-lookup filter, which can be provided to the
33 /// name-lookup routines to specify which declarations should be included in
34 /// the result set (when it returns true) and which declarations should be
35 /// filtered out (returns false).
36 typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const;
37
38 typedef CodeCompleteConsumer::Result Result;
39
40 private:
41 /// \brief The actual results we have found.
42 std::vector<Result> Results;
43
44 /// \brief A record of all of the declarations we have found and placed
45 /// into the result set, used to ensure that no declaration ever gets into
46 /// the result set twice.
47 llvm::SmallPtrSet<Decl*, 16> AllDeclsFound;
48
Douglas Gregor05e7ca32009-12-06 20:23:50 +000049 typedef std::pair<NamedDecl *, unsigned> DeclIndexPair;
50
51 /// \brief An entry in the shadow map, which is optimized to store
52 /// a single (declaration, index) mapping (the common case) but
53 /// can also store a list of (declaration, index) mappings.
54 class ShadowMapEntry {
55 typedef llvm::SmallVector<DeclIndexPair, 4> DeclIndexPairVector;
56
57 /// \brief Contains either the solitary NamedDecl * or a vector
58 /// of (declaration, index) pairs.
59 llvm::PointerUnion<NamedDecl *, DeclIndexPairVector*> DeclOrVector;
60
61 /// \brief When the entry contains a single declaration, this is
62 /// the index associated with that entry.
63 unsigned SingleDeclIndex;
64
65 public:
66 ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { }
67
68 void Add(NamedDecl *ND, unsigned Index) {
69 if (DeclOrVector.isNull()) {
70 // 0 - > 1 elements: just set the single element information.
71 DeclOrVector = ND;
72 SingleDeclIndex = Index;
73 return;
74 }
75
76 if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) {
77 // 1 -> 2 elements: create the vector of results and push in the
78 // existing declaration.
79 DeclIndexPairVector *Vec = new DeclIndexPairVector;
80 Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex));
81 DeclOrVector = Vec;
82 }
83
84 // Add the new element to the end of the vector.
85 DeclOrVector.get<DeclIndexPairVector*>()->push_back(
86 DeclIndexPair(ND, Index));
87 }
88
89 void Destroy() {
90 if (DeclIndexPairVector *Vec
91 = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) {
92 delete Vec;
93 DeclOrVector = ((NamedDecl *)0);
94 }
95 }
96
97 // Iteration.
98 class iterator;
99 iterator begin() const;
100 iterator end() const;
101 };
102
Douglas Gregor3545ff42009-09-21 16:56:56 +0000103 /// \brief A mapping from declaration names to the declarations that have
104 /// this name within a particular scope and their index within the list of
105 /// results.
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000106 typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000107
108 /// \brief The semantic analysis object for which results are being
109 /// produced.
110 Sema &SemaRef;
111
112 /// \brief If non-NULL, a filter function used to remove any code-completion
113 /// results that are not desirable.
114 LookupFilter Filter;
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000115
116 /// \brief Whether we should allow declarations as
117 /// nested-name-specifiers that would otherwise be filtered out.
118 bool AllowNestedNameSpecifiers;
119
Douglas Gregor3545ff42009-09-21 16:56:56 +0000120 /// \brief A list of shadow maps, which is used to model name hiding at
121 /// different levels of, e.g., the inheritance hierarchy.
122 std::list<ShadowMap> ShadowMaps;
123
124 public:
125 explicit ResultBuilder(Sema &SemaRef, LookupFilter Filter = 0)
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000126 : SemaRef(SemaRef), Filter(Filter), AllowNestedNameSpecifiers(false) { }
Douglas Gregor3545ff42009-09-21 16:56:56 +0000127
128 /// \brief Set the filter used for code-completion results.
129 void setFilter(LookupFilter Filter) {
130 this->Filter = Filter;
131 }
132
133 typedef std::vector<Result>::iterator iterator;
134 iterator begin() { return Results.begin(); }
135 iterator end() { return Results.end(); }
136
137 Result *data() { return Results.empty()? 0 : &Results.front(); }
138 unsigned size() const { return Results.size(); }
139 bool empty() const { return Results.empty(); }
140
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000141 /// \brief Specify whether nested-name-specifiers are allowed.
142 void allowNestedNameSpecifiers(bool Allow = true) {
143 AllowNestedNameSpecifiers = Allow;
144 }
145
Douglas Gregor7c208612010-01-14 00:20:49 +0000146 /// \brief Determine whether the given declaration is at all interesting
147 /// as a code-completion result.
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000148 ///
149 /// \param ND the declaration that we are inspecting.
150 ///
151 /// \param AsNestedNameSpecifier will be set true if this declaration is
152 /// only interesting when it is a nested-name-specifier.
153 bool isInterestingDecl(NamedDecl *ND, bool &AsNestedNameSpecifier) const;
Douglas Gregore0717ab2010-01-14 00:41:07 +0000154
155 /// \brief Check whether the result is hidden by the Hiding declaration.
156 ///
157 /// \returns true if the result is hidden and cannot be found, false if
158 /// the hidden result could still be found. When false, \p R may be
159 /// modified to describe how the result can be found (e.g., via extra
160 /// qualification).
161 bool CheckHiddenResult(Result &R, DeclContext *CurContext,
162 NamedDecl *Hiding);
163
Douglas Gregor3545ff42009-09-21 16:56:56 +0000164 /// \brief Add a new result to this result set (if it isn't already in one
165 /// of the shadow maps), or replace an existing result (for, e.g., a
166 /// redeclaration).
Douglas Gregor2af2f672009-09-21 20:12:40 +0000167 ///
Douglas Gregorc580c522010-01-14 01:09:38 +0000168 /// \param CurContext the result to add (if it is unique).
Douglas Gregor2af2f672009-09-21 20:12:40 +0000169 ///
170 /// \param R the context in which this result will be named.
171 void MaybeAddResult(Result R, DeclContext *CurContext = 0);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000172
Douglas Gregorc580c522010-01-14 01:09:38 +0000173 /// \brief Add a new result to this result set, where we already know
174 /// the hiding declation (if any).
175 ///
176 /// \param R the result to add (if it is unique).
177 ///
178 /// \param CurContext the context in which this result will be named.
179 ///
180 /// \param Hiding the declaration that hides the result.
Douglas Gregor09bbc652010-01-14 15:47:35 +0000181 ///
182 /// \param InBaseClass whether the result was found in a base
183 /// class of the searched context.
184 void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding,
185 bool InBaseClass);
Douglas Gregorc580c522010-01-14 01:09:38 +0000186
Douglas Gregor78a21012010-01-14 16:01:26 +0000187 /// \brief Add a new non-declaration result to this result set.
188 void AddResult(Result R);
189
Douglas Gregor3545ff42009-09-21 16:56:56 +0000190 /// \brief Enter into a new scope.
191 void EnterNewScope();
192
193 /// \brief Exit from the current scope.
194 void ExitScope();
195
Douglas Gregorbaf69612009-11-18 04:19:12 +0000196 /// \brief Ignore this declaration, if it is seen again.
197 void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); }
198
Douglas Gregor3545ff42009-09-21 16:56:56 +0000199 /// \name Name lookup predicates
200 ///
201 /// These predicates can be passed to the name lookup functions to filter the
202 /// results of name lookup. All of the predicates have the same type, so that
203 ///
204 //@{
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000205 bool IsOrdinaryName(NamedDecl *ND) const;
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000206 bool IsOrdinaryNonValueName(NamedDecl *ND) const;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000207 bool IsNestedNameSpecifier(NamedDecl *ND) const;
208 bool IsEnum(NamedDecl *ND) const;
209 bool IsClassOrStruct(NamedDecl *ND) const;
210 bool IsUnion(NamedDecl *ND) const;
211 bool IsNamespace(NamedDecl *ND) const;
212 bool IsNamespaceOrAlias(NamedDecl *ND) const;
213 bool IsType(NamedDecl *ND) const;
Douglas Gregore412a5a2009-09-23 22:26:46 +0000214 bool IsMember(NamedDecl *ND) const;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000215 //@}
216 };
217}
218
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000219class ResultBuilder::ShadowMapEntry::iterator {
220 llvm::PointerUnion<NamedDecl*, const DeclIndexPair*> DeclOrIterator;
221 unsigned SingleDeclIndex;
222
223public:
224 typedef DeclIndexPair value_type;
225 typedef value_type reference;
226 typedef std::ptrdiff_t difference_type;
227 typedef std::input_iterator_tag iterator_category;
228
229 class pointer {
230 DeclIndexPair Value;
231
232 public:
233 pointer(const DeclIndexPair &Value) : Value(Value) { }
234
235 const DeclIndexPair *operator->() const {
236 return &Value;
237 }
238 };
239
240 iterator() : DeclOrIterator((NamedDecl *)0), SingleDeclIndex(0) { }
241
242 iterator(NamedDecl *SingleDecl, unsigned Index)
243 : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { }
244
245 iterator(const DeclIndexPair *Iterator)
246 : DeclOrIterator(Iterator), SingleDeclIndex(0) { }
247
248 iterator &operator++() {
249 if (DeclOrIterator.is<NamedDecl *>()) {
250 DeclOrIterator = (NamedDecl *)0;
251 SingleDeclIndex = 0;
252 return *this;
253 }
254
255 const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>();
256 ++I;
257 DeclOrIterator = I;
258 return *this;
259 }
260
261 iterator operator++(int) {
262 iterator tmp(*this);
263 ++(*this);
264 return tmp;
265 }
266
267 reference operator*() const {
268 if (NamedDecl *ND = DeclOrIterator.dyn_cast<NamedDecl *>())
269 return reference(ND, SingleDeclIndex);
270
Douglas Gregor94bb5e82009-12-06 21:27:58 +0000271 return *DeclOrIterator.get<const DeclIndexPair*>();
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000272 }
273
274 pointer operator->() const {
275 return pointer(**this);
276 }
277
278 friend bool operator==(const iterator &X, const iterator &Y) {
Douglas Gregor94bb5e82009-12-06 21:27:58 +0000279 return X.DeclOrIterator.getOpaqueValue()
280 == Y.DeclOrIterator.getOpaqueValue() &&
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000281 X.SingleDeclIndex == Y.SingleDeclIndex;
282 }
283
284 friend bool operator!=(const iterator &X, const iterator &Y) {
Douglas Gregor94bb5e82009-12-06 21:27:58 +0000285 return !(X == Y);
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000286 }
287};
288
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000289ResultBuilder::ShadowMapEntry::iterator
290ResultBuilder::ShadowMapEntry::begin() const {
291 if (DeclOrVector.isNull())
292 return iterator();
293
294 if (NamedDecl *ND = DeclOrVector.dyn_cast<NamedDecl *>())
295 return iterator(ND, SingleDeclIndex);
296
297 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin());
298}
299
300ResultBuilder::ShadowMapEntry::iterator
301ResultBuilder::ShadowMapEntry::end() const {
302 if (DeclOrVector.is<NamedDecl *>() || DeclOrVector.isNull())
303 return iterator();
304
305 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end());
306}
307
Douglas Gregor2af2f672009-09-21 20:12:40 +0000308/// \brief Compute the qualification required to get from the current context
309/// (\p CurContext) to the target context (\p TargetContext).
310///
311/// \param Context the AST context in which the qualification will be used.
312///
313/// \param CurContext the context where an entity is being named, which is
314/// typically based on the current scope.
315///
316/// \param TargetContext the context in which the named entity actually
317/// resides.
318///
319/// \returns a nested name specifier that refers into the target context, or
320/// NULL if no qualification is needed.
321static NestedNameSpecifier *
322getRequiredQualification(ASTContext &Context,
323 DeclContext *CurContext,
324 DeclContext *TargetContext) {
325 llvm::SmallVector<DeclContext *, 4> TargetParents;
326
327 for (DeclContext *CommonAncestor = TargetContext;
328 CommonAncestor && !CommonAncestor->Encloses(CurContext);
329 CommonAncestor = CommonAncestor->getLookupParent()) {
330 if (CommonAncestor->isTransparentContext() ||
331 CommonAncestor->isFunctionOrMethod())
332 continue;
333
334 TargetParents.push_back(CommonAncestor);
335 }
336
337 NestedNameSpecifier *Result = 0;
338 while (!TargetParents.empty()) {
339 DeclContext *Parent = TargetParents.back();
340 TargetParents.pop_back();
341
342 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent))
343 Result = NestedNameSpecifier::Create(Context, Result, Namespace);
344 else if (TagDecl *TD = dyn_cast<TagDecl>(Parent))
345 Result = NestedNameSpecifier::Create(Context, Result,
346 false,
347 Context.getTypeDeclType(TD).getTypePtr());
348 else
349 assert(Parent->isTranslationUnit());
Douglas Gregor9eb77012009-11-07 00:00:49 +0000350 }
Douglas Gregor2af2f672009-09-21 20:12:40 +0000351 return Result;
352}
353
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000354bool ResultBuilder::isInterestingDecl(NamedDecl *ND,
355 bool &AsNestedNameSpecifier) const {
356 AsNestedNameSpecifier = false;
357
Douglas Gregor7c208612010-01-14 00:20:49 +0000358 ND = ND->getUnderlyingDecl();
359 unsigned IDNS = ND->getIdentifierNamespace();
Douglas Gregor58acf322009-10-09 22:16:47 +0000360
361 // Skip unnamed entities.
Douglas Gregor7c208612010-01-14 00:20:49 +0000362 if (!ND->getDeclName())
363 return false;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000364
365 // Friend declarations and declarations introduced due to friends are never
366 // added as results.
Douglas Gregor7c208612010-01-14 00:20:49 +0000367 if (isa<FriendDecl>(ND) ||
Douglas Gregor3545ff42009-09-21 16:56:56 +0000368 (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend)))
Douglas Gregor7c208612010-01-14 00:20:49 +0000369 return false;
370
Douglas Gregor99fe2ad2009-12-11 17:31:05 +0000371 // Class template (partial) specializations are never added as results.
Douglas Gregor7c208612010-01-14 00:20:49 +0000372 if (isa<ClassTemplateSpecializationDecl>(ND) ||
373 isa<ClassTemplatePartialSpecializationDecl>(ND))
374 return false;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000375
Douglas Gregor99fe2ad2009-12-11 17:31:05 +0000376 // Using declarations themselves are never added as results.
Douglas Gregor7c208612010-01-14 00:20:49 +0000377 if (isa<UsingDecl>(ND))
378 return false;
379
380 // Some declarations have reserved names that we don't want to ever show.
381 if (const IdentifierInfo *Id = ND->getIdentifier()) {
Douglas Gregor3545ff42009-09-21 16:56:56 +0000382 // __va_list_tag is a freak of nature. Find it and skip it.
383 if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list"))
Douglas Gregor7c208612010-01-14 00:20:49 +0000384 return false;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000385
Douglas Gregor58acf322009-10-09 22:16:47 +0000386 // Filter out names reserved for the implementation (C99 7.1.3,
387 // C++ [lib.global.names]). Users don't need to see those.
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000388 //
389 // FIXME: Add predicate for this.
Douglas Gregor58acf322009-10-09 22:16:47 +0000390 if (Id->getLength() >= 2) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000391 const char *Name = Id->getNameStart();
Douglas Gregor58acf322009-10-09 22:16:47 +0000392 if (Name[0] == '_' &&
393 (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')))
Douglas Gregor7c208612010-01-14 00:20:49 +0000394 return false;
Douglas Gregor58acf322009-10-09 22:16:47 +0000395 }
Douglas Gregor3545ff42009-09-21 16:56:56 +0000396 }
Douglas Gregor7c208612010-01-14 00:20:49 +0000397
Douglas Gregor3545ff42009-09-21 16:56:56 +0000398 // C++ constructors are never found by name lookup.
Douglas Gregor7c208612010-01-14 00:20:49 +0000399 if (isa<CXXConstructorDecl>(ND))
400 return false;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000401
402 // Filter out any unwanted results.
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000403 if (Filter && !(this->*Filter)(ND)) {
404 // Check whether it is interesting as a nested-name-specifier.
405 if (AllowNestedNameSpecifiers && SemaRef.getLangOptions().CPlusPlus &&
406 IsNestedNameSpecifier(ND) &&
407 (Filter != &ResultBuilder::IsMember ||
408 (isa<CXXRecordDecl>(ND) &&
409 cast<CXXRecordDecl>(ND)->isInjectedClassName()))) {
410 AsNestedNameSpecifier = true;
411 return true;
412 }
413
Douglas Gregor7c208612010-01-14 00:20:49 +0000414 return false;
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000415 }
Douglas Gregor3545ff42009-09-21 16:56:56 +0000416
Douglas Gregor7c208612010-01-14 00:20:49 +0000417 // ... then it must be interesting!
418 return true;
419}
420
Douglas Gregore0717ab2010-01-14 00:41:07 +0000421bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext,
422 NamedDecl *Hiding) {
423 // In C, there is no way to refer to a hidden name.
424 // FIXME: This isn't true; we can find a tag name hidden by an ordinary
425 // name if we introduce the tag type.
426 if (!SemaRef.getLangOptions().CPlusPlus)
427 return true;
428
429 DeclContext *HiddenCtx = R.Declaration->getDeclContext()->getLookupContext();
430
431 // There is no way to qualify a name declared in a function or method.
432 if (HiddenCtx->isFunctionOrMethod())
433 return true;
434
435 if (HiddenCtx == Hiding->getDeclContext()->getLookupContext())
436 return true;
437
438 // We can refer to the result with the appropriate qualification. Do it.
439 R.Hidden = true;
440 R.QualifierIsInformative = false;
441
442 if (!R.Qualifier)
443 R.Qualifier = getRequiredQualification(SemaRef.Context,
444 CurContext,
445 R.Declaration->getDeclContext());
446 return false;
447}
448
Douglas Gregor7c208612010-01-14 00:20:49 +0000449void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) {
450 assert(!ShadowMaps.empty() && "Must enter into a results scope");
451
452 if (R.Kind != Result::RK_Declaration) {
453 // For non-declaration results, just add the result.
454 Results.push_back(R);
455 return;
456 }
457
458 // Look through using declarations.
459 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
460 MaybeAddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext);
461 return;
462 }
463
464 Decl *CanonDecl = R.Declaration->getCanonicalDecl();
465 unsigned IDNS = CanonDecl->getIdentifierNamespace();
466
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000467 bool AsNestedNameSpecifier = false;
468 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
Douglas Gregor7c208612010-01-14 00:20:49 +0000469 return;
470
Douglas Gregor3545ff42009-09-21 16:56:56 +0000471 ShadowMap &SMap = ShadowMaps.back();
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000472 ShadowMapEntry::iterator I, IEnd;
473 ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName());
474 if (NamePos != SMap.end()) {
475 I = NamePos->second.begin();
476 IEnd = NamePos->second.end();
477 }
478
479 for (; I != IEnd; ++I) {
480 NamedDecl *ND = I->first;
481 unsigned Index = I->second;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000482 if (ND->getCanonicalDecl() == CanonDecl) {
483 // This is a redeclaration. Always pick the newer declaration.
Douglas Gregor3545ff42009-09-21 16:56:56 +0000484 Results[Index].Declaration = R.Declaration;
485
Douglas Gregor3545ff42009-09-21 16:56:56 +0000486 // We're done.
487 return;
488 }
489 }
490
491 // This is a new declaration in this scope. However, check whether this
492 // declaration name is hidden by a similarly-named declaration in an outer
493 // scope.
494 std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end();
495 --SMEnd;
496 for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) {
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000497 ShadowMapEntry::iterator I, IEnd;
498 ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName());
499 if (NamePos != SM->end()) {
500 I = NamePos->second.begin();
501 IEnd = NamePos->second.end();
502 }
503 for (; I != IEnd; ++I) {
Douglas Gregor3545ff42009-09-21 16:56:56 +0000504 // A tag declaration does not hide a non-tag declaration.
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000505 if (I->first->getIdentifierNamespace() == Decl::IDNS_Tag &&
Douglas Gregor3545ff42009-09-21 16:56:56 +0000506 (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary |
507 Decl::IDNS_ObjCProtocol)))
508 continue;
509
510 // Protocols are in distinct namespaces from everything else.
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000511 if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol)
Douglas Gregor3545ff42009-09-21 16:56:56 +0000512 || (IDNS & Decl::IDNS_ObjCProtocol)) &&
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000513 I->first->getIdentifierNamespace() != IDNS)
Douglas Gregor3545ff42009-09-21 16:56:56 +0000514 continue;
515
516 // The newly-added result is hidden by an entry in the shadow map.
Douglas Gregore0717ab2010-01-14 00:41:07 +0000517 if (CheckHiddenResult(R, CurContext, I->first))
Douglas Gregor3545ff42009-09-21 16:56:56 +0000518 return;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000519
520 break;
521 }
522 }
523
524 // Make sure that any given declaration only shows up in the result set once.
525 if (!AllDeclsFound.insert(CanonDecl))
526 return;
527
Douglas Gregore412a5a2009-09-23 22:26:46 +0000528 // If the filter is for nested-name-specifiers, then this result starts a
529 // nested-name-specifier.
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000530 if (AsNestedNameSpecifier)
Douglas Gregore412a5a2009-09-23 22:26:46 +0000531 R.StartsNestedNameSpecifier = true;
532
Douglas Gregor5bf52692009-09-22 23:15:58 +0000533 // If this result is supposed to have an informative qualifier, add one.
Douglas Gregore412a5a2009-09-23 22:26:46 +0000534 if (R.QualifierIsInformative && !R.Qualifier &&
535 !R.StartsNestedNameSpecifier) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000536 DeclContext *Ctx = R.Declaration->getDeclContext();
537 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
538 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
539 else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
540 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
541 SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
542 else
543 R.QualifierIsInformative = false;
544 }
Douglas Gregore412a5a2009-09-23 22:26:46 +0000545
Douglas Gregor3545ff42009-09-21 16:56:56 +0000546 // Insert this result into the set of results and into the current shadow
547 // map.
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000548 SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size());
Douglas Gregor3545ff42009-09-21 16:56:56 +0000549 Results.push_back(R);
550}
551
Douglas Gregorc580c522010-01-14 01:09:38 +0000552void ResultBuilder::AddResult(Result R, DeclContext *CurContext,
Douglas Gregor09bbc652010-01-14 15:47:35 +0000553 NamedDecl *Hiding, bool InBaseClass = false) {
Douglas Gregor78a21012010-01-14 16:01:26 +0000554 if (R.Kind != Result::RK_Declaration) {
555 // For non-declaration results, just add the result.
556 Results.push_back(R);
557 return;
558 }
559
Douglas Gregorc580c522010-01-14 01:09:38 +0000560 // Look through using declarations.
561 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
562 AddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext, Hiding);
563 return;
564 }
565
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000566 bool AsNestedNameSpecifier = false;
567 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
Douglas Gregorc580c522010-01-14 01:09:38 +0000568 return;
569
570 if (Hiding && CheckHiddenResult(R, CurContext, Hiding))
571 return;
572
573 // Make sure that any given declaration only shows up in the result set once.
574 if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()))
575 return;
576
577 // If the filter is for nested-name-specifiers, then this result starts a
578 // nested-name-specifier.
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000579 if (AsNestedNameSpecifier)
Douglas Gregorc580c522010-01-14 01:09:38 +0000580 R.StartsNestedNameSpecifier = true;
Douglas Gregor09bbc652010-01-14 15:47:35 +0000581 else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass &&
582 isa<CXXRecordDecl>(R.Declaration->getDeclContext()
583 ->getLookupContext()))
584 R.QualifierIsInformative = true;
585
Douglas Gregorc580c522010-01-14 01:09:38 +0000586 // If this result is supposed to have an informative qualifier, add one.
587 if (R.QualifierIsInformative && !R.Qualifier &&
588 !R.StartsNestedNameSpecifier) {
589 DeclContext *Ctx = R.Declaration->getDeclContext();
590 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
591 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
592 else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
593 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000594 SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
Douglas Gregorc580c522010-01-14 01:09:38 +0000595 else
596 R.QualifierIsInformative = false;
597 }
598
599 // Insert this result into the set of results.
600 Results.push_back(R);
601}
602
Douglas Gregor78a21012010-01-14 16:01:26 +0000603void ResultBuilder::AddResult(Result R) {
604 assert(R.Kind != Result::RK_Declaration &&
605 "Declaration results need more context");
606 Results.push_back(R);
607}
608
Douglas Gregor3545ff42009-09-21 16:56:56 +0000609/// \brief Enter into a new scope.
610void ResultBuilder::EnterNewScope() {
611 ShadowMaps.push_back(ShadowMap());
612}
613
614/// \brief Exit from the current scope.
615void ResultBuilder::ExitScope() {
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000616 for (ShadowMap::iterator E = ShadowMaps.back().begin(),
617 EEnd = ShadowMaps.back().end();
618 E != EEnd;
619 ++E)
620 E->second.Destroy();
621
Douglas Gregor3545ff42009-09-21 16:56:56 +0000622 ShadowMaps.pop_back();
623}
624
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000625/// \brief Determines whether this given declaration will be found by
626/// ordinary name lookup.
627bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const {
628 unsigned IDNS = Decl::IDNS_Ordinary;
629 if (SemaRef.getLangOptions().CPlusPlus)
630 IDNS |= Decl::IDNS_Tag;
Douglas Gregorc580c522010-01-14 01:09:38 +0000631 else if (SemaRef.getLangOptions().ObjC1 && isa<ObjCIvarDecl>(ND))
632 return true;
633
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000634 return ND->getIdentifierNamespace() & IDNS;
635}
636
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000637/// \brief Determines whether this given declaration will be found by
638/// ordinary name lookup.
639bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const {
640 unsigned IDNS = Decl::IDNS_Ordinary;
641 if (SemaRef.getLangOptions().CPlusPlus)
642 IDNS |= Decl::IDNS_Tag;
643
644 return (ND->getIdentifierNamespace() & IDNS) &&
645 !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND);
646}
647
Douglas Gregor3545ff42009-09-21 16:56:56 +0000648/// \brief Determines whether the given declaration is suitable as the
649/// start of a C++ nested-name-specifier, e.g., a class or namespace.
650bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const {
651 // Allow us to find class templates, too.
652 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
653 ND = ClassTemplate->getTemplatedDecl();
654
655 return SemaRef.isAcceptableNestedNameSpecifier(ND);
656}
657
658/// \brief Determines whether the given declaration is an enumeration.
659bool ResultBuilder::IsEnum(NamedDecl *ND) const {
660 return isa<EnumDecl>(ND);
661}
662
663/// \brief Determines whether the given declaration is a class or struct.
664bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const {
665 // Allow us to find class templates, too.
666 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
667 ND = ClassTemplate->getTemplatedDecl();
668
669 if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
670 return RD->getTagKind() == TagDecl::TK_class ||
671 RD->getTagKind() == TagDecl::TK_struct;
672
673 return false;
674}
675
676/// \brief Determines whether the given declaration is a union.
677bool ResultBuilder::IsUnion(NamedDecl *ND) const {
678 // Allow us to find class templates, too.
679 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
680 ND = ClassTemplate->getTemplatedDecl();
681
682 if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
683 return RD->getTagKind() == TagDecl::TK_union;
684
685 return false;
686}
687
688/// \brief Determines whether the given declaration is a namespace.
689bool ResultBuilder::IsNamespace(NamedDecl *ND) const {
690 return isa<NamespaceDecl>(ND);
691}
692
693/// \brief Determines whether the given declaration is a namespace or
694/// namespace alias.
695bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const {
696 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
697}
698
Douglas Gregor99fe2ad2009-12-11 17:31:05 +0000699/// \brief Determines whether the given declaration is a type.
Douglas Gregor3545ff42009-09-21 16:56:56 +0000700bool ResultBuilder::IsType(NamedDecl *ND) const {
701 return isa<TypeDecl>(ND);
702}
703
Douglas Gregor99fe2ad2009-12-11 17:31:05 +0000704/// \brief Determines which members of a class should be visible via
705/// "." or "->". Only value declarations, nested name specifiers, and
706/// using declarations thereof should show up.
Douglas Gregore412a5a2009-09-23 22:26:46 +0000707bool ResultBuilder::IsMember(NamedDecl *ND) const {
Douglas Gregor99fe2ad2009-12-11 17:31:05 +0000708 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND))
709 ND = Using->getTargetDecl();
710
Douglas Gregor70788392009-12-11 18:14:22 +0000711 return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
712 isa<ObjCPropertyDecl>(ND);
Douglas Gregore412a5a2009-09-23 22:26:46 +0000713}
714
Douglas Gregorc580c522010-01-14 01:09:38 +0000715namespace {
716 /// \brief Visible declaration consumer that adds a code-completion result
717 /// for each visible declaration.
718 class CodeCompletionDeclConsumer : public VisibleDeclConsumer {
719 ResultBuilder &Results;
720 DeclContext *CurContext;
721
722 public:
723 CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext)
724 : Results(Results), CurContext(CurContext) { }
725
Douglas Gregor09bbc652010-01-14 15:47:35 +0000726 virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass) {
727 Results.AddResult(ND, CurContext, Hiding, InBaseClass);
Douglas Gregorc580c522010-01-14 01:09:38 +0000728 }
729 };
730}
731
Douglas Gregor3545ff42009-09-21 16:56:56 +0000732/// \brief Add type specifiers for the current language as keyword results.
Douglas Gregorf98e6a22010-01-13 23:51:12 +0000733static void AddTypeSpecifierResults(const LangOptions &LangOpts,
Douglas Gregor3545ff42009-09-21 16:56:56 +0000734 ResultBuilder &Results) {
735 typedef CodeCompleteConsumer::Result Result;
Douglas Gregor78a21012010-01-14 16:01:26 +0000736 Results.AddResult(Result("short"));
737 Results.AddResult(Result("long"));
738 Results.AddResult(Result("signed"));
739 Results.AddResult(Result("unsigned"));
740 Results.AddResult(Result("void"));
741 Results.AddResult(Result("char"));
742 Results.AddResult(Result("int"));
743 Results.AddResult(Result("float"));
744 Results.AddResult(Result("double"));
745 Results.AddResult(Result("enum"));
746 Results.AddResult(Result("struct"));
747 Results.AddResult(Result("union"));
748 Results.AddResult(Result("const"));
749 Results.AddResult(Result("volatile"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000750
Douglas Gregor3545ff42009-09-21 16:56:56 +0000751 if (LangOpts.C99) {
752 // C99-specific
Douglas Gregor78a21012010-01-14 16:01:26 +0000753 Results.AddResult(Result("_Complex"));
754 Results.AddResult(Result("_Imaginary"));
755 Results.AddResult(Result("_Bool"));
756 Results.AddResult(Result("restrict"));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000757 }
758
759 if (LangOpts.CPlusPlus) {
760 // C++-specific
Douglas Gregor78a21012010-01-14 16:01:26 +0000761 Results.AddResult(Result("bool"));
762 Results.AddResult(Result("class"));
763 Results.AddResult(Result("wchar_t"));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000764
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000765 // typename qualified-id
766 CodeCompletionString *Pattern = new CodeCompletionString;
767 Pattern->AddTypedTextChunk("typename");
768 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
769 Pattern->AddPlaceholderChunk("qualified-id");
Douglas Gregor78a21012010-01-14 16:01:26 +0000770 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000771
Douglas Gregor3545ff42009-09-21 16:56:56 +0000772 if (LangOpts.CPlusPlus0x) {
Douglas Gregor78a21012010-01-14 16:01:26 +0000773 Results.AddResult(Result("auto"));
774 Results.AddResult(Result("char16_t"));
775 Results.AddResult(Result("char32_t"));
776 Results.AddResult(Result("decltype"));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000777 }
778 }
779
780 // GNU extensions
781 if (LangOpts.GNUMode) {
782 // FIXME: Enable when we actually support decimal floating point.
Douglas Gregor78a21012010-01-14 16:01:26 +0000783 // Results.AddResult(Result("_Decimal32"));
784 // Results.AddResult(Result("_Decimal64"));
785 // Results.AddResult(Result("_Decimal128"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000786
787 CodeCompletionString *Pattern = new CodeCompletionString;
788 Pattern->AddTypedTextChunk("typeof");
789 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
790 Pattern->AddPlaceholderChunk("expression-or-type");
791 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +0000792 Results.AddResult(Result(Pattern));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000793 }
794}
795
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000796static void AddStorageSpecifiers(Action::CodeCompletionContext CCC,
797 const LangOptions &LangOpts,
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000798 ResultBuilder &Results) {
799 typedef CodeCompleteConsumer::Result Result;
800 // Note: we don't suggest either "auto" or "register", because both
801 // are pointless as storage specifiers. Elsewhere, we suggest "auto"
802 // in C++0x as a type specifier.
Douglas Gregor78a21012010-01-14 16:01:26 +0000803 Results.AddResult(Result("extern"));
804 Results.AddResult(Result("static"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000805}
806
807static void AddFunctionSpecifiers(Action::CodeCompletionContext CCC,
808 const LangOptions &LangOpts,
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000809 ResultBuilder &Results) {
810 typedef CodeCompleteConsumer::Result Result;
811 switch (CCC) {
812 case Action::CCC_Class:
813 case Action::CCC_MemberTemplate:
814 if (LangOpts.CPlusPlus) {
Douglas Gregor78a21012010-01-14 16:01:26 +0000815 Results.AddResult(Result("explicit"));
816 Results.AddResult(Result("friend"));
817 Results.AddResult(Result("mutable"));
818 Results.AddResult(Result("virtual"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000819 }
820 // Fall through
821
Douglas Gregorf1934162010-01-13 21:24:21 +0000822 case Action::CCC_ObjCInterface:
823 case Action::CCC_ObjCImplementation:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000824 case Action::CCC_Namespace:
825 case Action::CCC_Template:
826 if (LangOpts.CPlusPlus || LangOpts.C99)
Douglas Gregor78a21012010-01-14 16:01:26 +0000827 Results.AddResult(Result("inline"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000828 break;
829
Douglas Gregor48d46252010-01-13 21:54:15 +0000830 case Action::CCC_ObjCInstanceVariableList:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000831 case Action::CCC_Expression:
832 case Action::CCC_Statement:
833 case Action::CCC_ForInit:
834 case Action::CCC_Condition:
835 break;
836 }
837}
838
Douglas Gregorf98e6a22010-01-13 23:51:12 +0000839static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt);
840static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt);
841static void AddObjCVisibilityResults(const LangOptions &LangOpts,
Douglas Gregor48d46252010-01-13 21:54:15 +0000842 ResultBuilder &Results,
843 bool NeedAt);
Douglas Gregorf98e6a22010-01-13 23:51:12 +0000844static void AddObjCImplementationResults(const LangOptions &LangOpts,
Douglas Gregorf1934162010-01-13 21:24:21 +0000845 ResultBuilder &Results,
846 bool NeedAt);
Douglas Gregorf98e6a22010-01-13 23:51:12 +0000847static void AddObjCInterfaceResults(const LangOptions &LangOpts,
Douglas Gregorf1934162010-01-13 21:24:21 +0000848 ResultBuilder &Results,
849 bool NeedAt);
Douglas Gregorf98e6a22010-01-13 23:51:12 +0000850static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt);
Douglas Gregorf1934162010-01-13 21:24:21 +0000851
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000852/// \brief Add language constructs that show up for "ordinary" names.
853static void AddOrdinaryNameResults(Action::CodeCompletionContext CCC,
854 Scope *S,
855 Sema &SemaRef,
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000856 ResultBuilder &Results) {
857 typedef CodeCompleteConsumer::Result Result;
858 switch (CCC) {
859 case Action::CCC_Namespace:
860 if (SemaRef.getLangOptions().CPlusPlus) {
861 // namespace <identifier> { }
862 CodeCompletionString *Pattern = new CodeCompletionString;
863 Pattern->AddTypedTextChunk("namespace");
864 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
865 Pattern->AddPlaceholderChunk("identifier");
866 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
867 Pattern->AddPlaceholderChunk("declarations");
868 Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
869 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregor78a21012010-01-14 16:01:26 +0000870 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000871
872 // namespace identifier = identifier ;
873 Pattern = new CodeCompletionString;
874 Pattern->AddTypedTextChunk("namespace");
875 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
876 Pattern->AddPlaceholderChunk("identifier");
877 Pattern->AddChunk(CodeCompletionString::CK_Equal);
878 Pattern->AddPlaceholderChunk("identifier");
879 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +0000880 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000881
882 // Using directives
883 Pattern = new CodeCompletionString;
884 Pattern->AddTypedTextChunk("using");
885 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
886 Pattern->AddTextChunk("namespace");
887 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
888 Pattern->AddPlaceholderChunk("identifier");
889 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +0000890 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000891
892 // asm(string-literal)
893 Pattern = new CodeCompletionString;
894 Pattern->AddTypedTextChunk("asm");
895 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
896 Pattern->AddPlaceholderChunk("string-literal");
897 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
898 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +0000899 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000900
901 // Explicit template instantiation
902 Pattern = new CodeCompletionString;
903 Pattern->AddTypedTextChunk("template");
904 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
905 Pattern->AddPlaceholderChunk("declaration");
906 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +0000907 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000908 }
Douglas Gregorf1934162010-01-13 21:24:21 +0000909
910 if (SemaRef.getLangOptions().ObjC1)
Douglas Gregorf98e6a22010-01-13 23:51:12 +0000911 AddObjCTopLevelResults(Results, true);
Douglas Gregorf1934162010-01-13 21:24:21 +0000912
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000913 // Fall through
914
915 case Action::CCC_Class:
Douglas Gregor78a21012010-01-14 16:01:26 +0000916 Results.AddResult(Result("typedef"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000917 if (SemaRef.getLangOptions().CPlusPlus) {
918 // Using declaration
919 CodeCompletionString *Pattern = new CodeCompletionString;
920 Pattern->AddTypedTextChunk("using");
921 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
922 Pattern->AddPlaceholderChunk("qualified-id");
923 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +0000924 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000925
926 // using typename qualified-id; (only in a dependent context)
927 if (SemaRef.CurContext->isDependentContext()) {
928 Pattern = new CodeCompletionString;
929 Pattern->AddTypedTextChunk("using");
930 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
931 Pattern->AddTextChunk("typename");
932 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
933 Pattern->AddPlaceholderChunk("qualified-id");
934 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +0000935 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000936 }
937
938 if (CCC == Action::CCC_Class) {
939 // public:
940 Pattern = new CodeCompletionString;
941 Pattern->AddTypedTextChunk("public");
942 Pattern->AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor78a21012010-01-14 16:01:26 +0000943 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000944
945 // protected:
946 Pattern = new CodeCompletionString;
947 Pattern->AddTypedTextChunk("protected");
948 Pattern->AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor78a21012010-01-14 16:01:26 +0000949 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000950
951 // private:
952 Pattern = new CodeCompletionString;
953 Pattern->AddTypedTextChunk("private");
954 Pattern->AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor78a21012010-01-14 16:01:26 +0000955 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000956 }
957 }
958 // Fall through
959
960 case Action::CCC_Template:
961 case Action::CCC_MemberTemplate:
962 if (SemaRef.getLangOptions().CPlusPlus) {
963 // template < parameters >
964 CodeCompletionString *Pattern = new CodeCompletionString;
965 Pattern->AddTypedTextChunk("template");
966 Pattern->AddChunk(CodeCompletionString::CK_LeftAngle);
967 Pattern->AddPlaceholderChunk("parameters");
968 Pattern->AddChunk(CodeCompletionString::CK_RightAngle);
Douglas Gregor78a21012010-01-14 16:01:26 +0000969 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000970 }
971
Douglas Gregorf98e6a22010-01-13 23:51:12 +0000972 AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results);
973 AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000974 break;
975
Douglas Gregorf1934162010-01-13 21:24:21 +0000976 case Action::CCC_ObjCInterface:
Douglas Gregorf98e6a22010-01-13 23:51:12 +0000977 AddObjCInterfaceResults(SemaRef.getLangOptions(), Results, true);
978 AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results);
979 AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results);
Douglas Gregorf1934162010-01-13 21:24:21 +0000980 break;
981
982 case Action::CCC_ObjCImplementation:
Douglas Gregorf98e6a22010-01-13 23:51:12 +0000983 AddObjCImplementationResults(SemaRef.getLangOptions(), Results, true);
984 AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results);
985 AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results);
Douglas Gregorf1934162010-01-13 21:24:21 +0000986 break;
987
Douglas Gregor48d46252010-01-13 21:54:15 +0000988 case Action::CCC_ObjCInstanceVariableList:
Douglas Gregorf98e6a22010-01-13 23:51:12 +0000989 AddObjCVisibilityResults(SemaRef.getLangOptions(), Results, true);
Douglas Gregor48d46252010-01-13 21:54:15 +0000990 break;
991
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000992 case Action::CCC_Statement: {
Douglas Gregor78a21012010-01-14 16:01:26 +0000993 Results.AddResult(Result("typedef"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000994
995 CodeCompletionString *Pattern = 0;
996 if (SemaRef.getLangOptions().CPlusPlus) {
997 Pattern = new CodeCompletionString;
998 Pattern->AddTypedTextChunk("try");
999 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1000 Pattern->AddPlaceholderChunk("statements");
1001 Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1002 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
1003 Pattern->AddTextChunk("catch");
1004 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1005 Pattern->AddPlaceholderChunk("declaration");
1006 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1007 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1008 Pattern->AddPlaceholderChunk("statements");
1009 Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1010 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregor78a21012010-01-14 16:01:26 +00001011 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001012 }
Douglas Gregorf1934162010-01-13 21:24:21 +00001013 if (SemaRef.getLangOptions().ObjC1)
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001014 AddObjCStatementResults(Results, true);
Douglas Gregorf1934162010-01-13 21:24:21 +00001015
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001016 // if (condition) { statements }
1017 Pattern = new CodeCompletionString;
1018 Pattern->AddTypedTextChunk("if");
1019 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1020 if (SemaRef.getLangOptions().CPlusPlus)
1021 Pattern->AddPlaceholderChunk("condition");
1022 else
1023 Pattern->AddPlaceholderChunk("expression");
1024 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1025 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1026 Pattern->AddPlaceholderChunk("statements");
1027 Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1028 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregor78a21012010-01-14 16:01:26 +00001029 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001030
1031 // switch (condition) { }
1032 Pattern = new CodeCompletionString;
1033 Pattern->AddTypedTextChunk("switch");
1034 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1035 if (SemaRef.getLangOptions().CPlusPlus)
1036 Pattern->AddPlaceholderChunk("condition");
1037 else
1038 Pattern->AddPlaceholderChunk("expression");
1039 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1040 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1041 Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1042 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregor78a21012010-01-14 16:01:26 +00001043 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001044
1045 // Switch-specific statements.
1046 if (!SemaRef.getSwitchStack().empty()) {
1047 // case expression:
1048 Pattern = new CodeCompletionString;
1049 Pattern->AddTypedTextChunk("case");
1050 Pattern->AddPlaceholderChunk("expression");
1051 Pattern->AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor78a21012010-01-14 16:01:26 +00001052 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001053
1054 // default:
1055 Pattern = new CodeCompletionString;
1056 Pattern->AddTypedTextChunk("default");
1057 Pattern->AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregor78a21012010-01-14 16:01:26 +00001058 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001059 }
1060
1061 /// while (condition) { statements }
1062 Pattern = new CodeCompletionString;
1063 Pattern->AddTypedTextChunk("while");
1064 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1065 if (SemaRef.getLangOptions().CPlusPlus)
1066 Pattern->AddPlaceholderChunk("condition");
1067 else
1068 Pattern->AddPlaceholderChunk("expression");
1069 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1070 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1071 Pattern->AddPlaceholderChunk("statements");
1072 Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1073 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregor78a21012010-01-14 16:01:26 +00001074 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001075
1076 // do { statements } while ( expression );
1077 Pattern = new CodeCompletionString;
1078 Pattern->AddTypedTextChunk("do");
1079 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1080 Pattern->AddPlaceholderChunk("statements");
1081 Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1082 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
1083 Pattern->AddTextChunk("while");
1084 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1085 Pattern->AddPlaceholderChunk("expression");
1086 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1087 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +00001088 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001089
1090 // for ( for-init-statement ; condition ; expression ) { statements }
1091 Pattern = new CodeCompletionString;
1092 Pattern->AddTypedTextChunk("for");
1093 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1094 if (SemaRef.getLangOptions().CPlusPlus || SemaRef.getLangOptions().C99)
1095 Pattern->AddPlaceholderChunk("init-statement");
1096 else
1097 Pattern->AddPlaceholderChunk("init-expression");
1098 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
1099 Pattern->AddPlaceholderChunk("condition");
1100 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
1101 Pattern->AddPlaceholderChunk("inc-expression");
1102 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1103 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1104 Pattern->AddPlaceholderChunk("statements");
1105 Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1106 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregor78a21012010-01-14 16:01:26 +00001107 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001108
1109 if (S->getContinueParent()) {
1110 // continue ;
1111 Pattern = new CodeCompletionString;
1112 Pattern->AddTypedTextChunk("continue");
1113 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +00001114 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001115 }
1116
1117 if (S->getBreakParent()) {
1118 // break ;
1119 Pattern = new CodeCompletionString;
1120 Pattern->AddTypedTextChunk("break");
1121 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +00001122 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001123 }
1124
1125 // "return expression ;" or "return ;", depending on whether we
1126 // know the function is void or not.
1127 bool isVoid = false;
1128 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext))
1129 isVoid = Function->getResultType()->isVoidType();
1130 else if (ObjCMethodDecl *Method
1131 = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext))
1132 isVoid = Method->getResultType()->isVoidType();
1133 else if (SemaRef.CurBlock && !SemaRef.CurBlock->ReturnType.isNull())
1134 isVoid = SemaRef.CurBlock->ReturnType->isVoidType();
1135 Pattern = new CodeCompletionString;
1136 Pattern->AddTypedTextChunk("return");
1137 if (!isVoid)
1138 Pattern->AddPlaceholderChunk("expression");
1139 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +00001140 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001141
1142 // goto identifier ;
1143 Pattern = new CodeCompletionString;
1144 Pattern->AddTypedTextChunk("goto");
1145 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1146 Pattern->AddPlaceholderChunk("identifier");
1147 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +00001148 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001149
1150 // Using directives
1151 Pattern = new CodeCompletionString;
1152 Pattern->AddTypedTextChunk("using");
1153 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1154 Pattern->AddTextChunk("namespace");
1155 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1156 Pattern->AddPlaceholderChunk("identifier");
1157 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +00001158 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001159 }
1160
1161 // Fall through (for statement expressions).
1162 case Action::CCC_ForInit:
1163 case Action::CCC_Condition:
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001164 AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001165 // Fall through: conditions and statements can have expressions.
1166
1167 case Action::CCC_Expression: {
1168 CodeCompletionString *Pattern = 0;
1169 if (SemaRef.getLangOptions().CPlusPlus) {
1170 // 'this', if we're in a non-static member function.
1171 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(SemaRef.CurContext))
1172 if (!Method->isStatic())
Douglas Gregor78a21012010-01-14 16:01:26 +00001173 Results.AddResult(Result("this"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001174
1175 // true, false
Douglas Gregor78a21012010-01-14 16:01:26 +00001176 Results.AddResult(Result("true"));
1177 Results.AddResult(Result("false"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001178
1179 // dynamic_cast < type-id > ( expression )
1180 Pattern = new CodeCompletionString;
1181 Pattern->AddTypedTextChunk("dynamic_cast");
1182 Pattern->AddChunk(CodeCompletionString::CK_LeftAngle);
1183 Pattern->AddPlaceholderChunk("type-id");
1184 Pattern->AddChunk(CodeCompletionString::CK_RightAngle);
1185 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1186 Pattern->AddPlaceholderChunk("expression");
1187 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00001188 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001189
1190 // static_cast < type-id > ( expression )
1191 Pattern = new CodeCompletionString;
1192 Pattern->AddTypedTextChunk("static_cast");
1193 Pattern->AddChunk(CodeCompletionString::CK_LeftAngle);
1194 Pattern->AddPlaceholderChunk("type-id");
1195 Pattern->AddChunk(CodeCompletionString::CK_RightAngle);
1196 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1197 Pattern->AddPlaceholderChunk("expression");
1198 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00001199 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001200
1201 // reinterpret_cast < type-id > ( expression )
1202 Pattern = new CodeCompletionString;
1203 Pattern->AddTypedTextChunk("reinterpret_cast");
1204 Pattern->AddChunk(CodeCompletionString::CK_LeftAngle);
1205 Pattern->AddPlaceholderChunk("type-id");
1206 Pattern->AddChunk(CodeCompletionString::CK_RightAngle);
1207 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1208 Pattern->AddPlaceholderChunk("expression");
1209 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00001210 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001211
1212 // const_cast < type-id > ( expression )
1213 Pattern = new CodeCompletionString;
1214 Pattern->AddTypedTextChunk("const_cast");
1215 Pattern->AddChunk(CodeCompletionString::CK_LeftAngle);
1216 Pattern->AddPlaceholderChunk("type-id");
1217 Pattern->AddChunk(CodeCompletionString::CK_RightAngle);
1218 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1219 Pattern->AddPlaceholderChunk("expression");
1220 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00001221 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001222
1223 // typeid ( expression-or-type )
1224 Pattern = new CodeCompletionString;
1225 Pattern->AddTypedTextChunk("typeid");
1226 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1227 Pattern->AddPlaceholderChunk("expression-or-type");
1228 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00001229 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001230
1231 // new T ( ... )
1232 Pattern = new CodeCompletionString;
1233 Pattern->AddTypedTextChunk("new");
1234 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1235 Pattern->AddPlaceholderChunk("type-id");
1236 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1237 Pattern->AddPlaceholderChunk("expressions");
1238 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00001239 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001240
1241 // new T [ ] ( ... )
1242 Pattern = new CodeCompletionString;
1243 Pattern->AddTypedTextChunk("new");
1244 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1245 Pattern->AddPlaceholderChunk("type-id");
1246 Pattern->AddChunk(CodeCompletionString::CK_LeftBracket);
1247 Pattern->AddPlaceholderChunk("size");
1248 Pattern->AddChunk(CodeCompletionString::CK_RightBracket);
1249 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1250 Pattern->AddPlaceholderChunk("expressions");
1251 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00001252 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001253
1254 // delete expression
1255 Pattern = new CodeCompletionString;
1256 Pattern->AddTypedTextChunk("delete");
1257 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1258 Pattern->AddPlaceholderChunk("expression");
Douglas Gregor78a21012010-01-14 16:01:26 +00001259 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001260
1261 // delete [] expression
1262 Pattern = new CodeCompletionString;
1263 Pattern->AddTypedTextChunk("delete");
1264 Pattern->AddChunk(CodeCompletionString::CK_LeftBracket);
1265 Pattern->AddChunk(CodeCompletionString::CK_RightBracket);
1266 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1267 Pattern->AddPlaceholderChunk("expression");
Douglas Gregor78a21012010-01-14 16:01:26 +00001268 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001269
1270 // throw expression
1271 Pattern = new CodeCompletionString;
1272 Pattern->AddTypedTextChunk("throw");
1273 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1274 Pattern->AddPlaceholderChunk("expression");
Douglas Gregor78a21012010-01-14 16:01:26 +00001275 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001276 }
1277
1278 if (SemaRef.getLangOptions().ObjC1) {
1279 // Add "super", if we're in an Objective-C class with a superclass.
1280 if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl())
1281 if (Method->getClassInterface()->getSuperClass())
Douglas Gregor78a21012010-01-14 16:01:26 +00001282 Results.AddResult(Result("super"));
Douglas Gregorf1934162010-01-13 21:24:21 +00001283
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001284 AddObjCExpressionResults(Results, true);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001285 }
1286
1287 // sizeof expression
1288 Pattern = new CodeCompletionString;
1289 Pattern->AddTypedTextChunk("sizeof");
1290 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1291 Pattern->AddPlaceholderChunk("expression-or-type");
1292 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00001293 Results.AddResult(Result(Pattern));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001294 break;
1295 }
1296 }
1297
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001298 AddTypeSpecifierResults(SemaRef.getLangOptions(), Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001299
1300 if (SemaRef.getLangOptions().CPlusPlus)
Douglas Gregor78a21012010-01-14 16:01:26 +00001301 Results.AddResult(Result("operator"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001302}
1303
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001304/// \brief If the given declaration has an associated type, add it as a result
1305/// type chunk.
1306static void AddResultTypeChunk(ASTContext &Context,
1307 NamedDecl *ND,
1308 CodeCompletionString *Result) {
1309 if (!ND)
1310 return;
1311
1312 // Determine the type of the declaration (if it has a type).
1313 QualType T;
1314 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND))
1315 T = Function->getResultType();
1316 else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
1317 T = Method->getResultType();
1318 else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND))
1319 T = FunTmpl->getTemplatedDecl()->getResultType();
1320 else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND))
1321 T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext()));
1322 else if (isa<UnresolvedUsingValueDecl>(ND)) {
1323 /* Do nothing: ignore unresolved using declarations*/
1324 } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND))
1325 T = Value->getType();
1326 else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND))
1327 T = Property->getType();
1328
1329 if (T.isNull() || Context.hasSameType(T, Context.DependentTy))
1330 return;
1331
1332 std::string TypeStr;
1333 T.getAsStringInternal(TypeStr, Context.PrintingPolicy);
1334 Result->AddResultTypeChunk(TypeStr);
1335}
1336
Douglas Gregor3545ff42009-09-21 16:56:56 +00001337/// \brief Add function parameter chunks to the given code completion string.
1338static void AddFunctionParameterChunks(ASTContext &Context,
1339 FunctionDecl *Function,
1340 CodeCompletionString *Result) {
Douglas Gregor9eb77012009-11-07 00:00:49 +00001341 typedef CodeCompletionString::Chunk Chunk;
1342
Douglas Gregor3545ff42009-09-21 16:56:56 +00001343 CodeCompletionString *CCStr = Result;
1344
1345 for (unsigned P = 0, N = Function->getNumParams(); P != N; ++P) {
1346 ParmVarDecl *Param = Function->getParamDecl(P);
1347
1348 if (Param->hasDefaultArg()) {
1349 // When we see an optional default argument, put that argument and
1350 // the remaining default arguments into a new, optional string.
1351 CodeCompletionString *Opt = new CodeCompletionString;
1352 CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt));
1353 CCStr = Opt;
1354 }
1355
1356 if (P != 0)
Douglas Gregor9eb77012009-11-07 00:00:49 +00001357 CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001358
1359 // Format the placeholder string.
1360 std::string PlaceholderStr;
1361 if (Param->getIdentifier())
1362 PlaceholderStr = Param->getIdentifier()->getName();
1363
1364 Param->getType().getAsStringInternal(PlaceholderStr,
1365 Context.PrintingPolicy);
1366
1367 // Add the placeholder string.
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001368 CCStr->AddPlaceholderChunk(PlaceholderStr);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001369 }
Douglas Gregorba449032009-09-22 21:42:17 +00001370
1371 if (const FunctionProtoType *Proto
1372 = Function->getType()->getAs<FunctionProtoType>())
1373 if (Proto->isVariadic())
1374 CCStr->AddPlaceholderChunk(", ...");
Douglas Gregor3545ff42009-09-21 16:56:56 +00001375}
1376
1377/// \brief Add template parameter chunks to the given code completion string.
1378static void AddTemplateParameterChunks(ASTContext &Context,
1379 TemplateDecl *Template,
1380 CodeCompletionString *Result,
1381 unsigned MaxParameters = 0) {
Douglas Gregor9eb77012009-11-07 00:00:49 +00001382 typedef CodeCompletionString::Chunk Chunk;
1383
Douglas Gregor3545ff42009-09-21 16:56:56 +00001384 CodeCompletionString *CCStr = Result;
1385 bool FirstParameter = true;
1386
1387 TemplateParameterList *Params = Template->getTemplateParameters();
1388 TemplateParameterList::iterator PEnd = Params->end();
1389 if (MaxParameters)
1390 PEnd = Params->begin() + MaxParameters;
1391 for (TemplateParameterList::iterator P = Params->begin(); P != PEnd; ++P) {
1392 bool HasDefaultArg = false;
1393 std::string PlaceholderStr;
1394 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
1395 if (TTP->wasDeclaredWithTypename())
1396 PlaceholderStr = "typename";
1397 else
1398 PlaceholderStr = "class";
1399
1400 if (TTP->getIdentifier()) {
1401 PlaceholderStr += ' ';
1402 PlaceholderStr += TTP->getIdentifier()->getName();
1403 }
1404
1405 HasDefaultArg = TTP->hasDefaultArgument();
1406 } else if (NonTypeTemplateParmDecl *NTTP
1407 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1408 if (NTTP->getIdentifier())
1409 PlaceholderStr = NTTP->getIdentifier()->getName();
1410 NTTP->getType().getAsStringInternal(PlaceholderStr,
1411 Context.PrintingPolicy);
1412 HasDefaultArg = NTTP->hasDefaultArgument();
1413 } else {
1414 assert(isa<TemplateTemplateParmDecl>(*P));
1415 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
1416
1417 // Since putting the template argument list into the placeholder would
1418 // be very, very long, we just use an abbreviation.
1419 PlaceholderStr = "template<...> class";
1420 if (TTP->getIdentifier()) {
1421 PlaceholderStr += ' ';
1422 PlaceholderStr += TTP->getIdentifier()->getName();
1423 }
1424
1425 HasDefaultArg = TTP->hasDefaultArgument();
1426 }
1427
1428 if (HasDefaultArg) {
1429 // When we see an optional default argument, put that argument and
1430 // the remaining default arguments into a new, optional string.
1431 CodeCompletionString *Opt = new CodeCompletionString;
1432 CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt));
1433 CCStr = Opt;
1434 }
1435
1436 if (FirstParameter)
1437 FirstParameter = false;
1438 else
Douglas Gregor9eb77012009-11-07 00:00:49 +00001439 CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001440
1441 // Add the placeholder string.
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001442 CCStr->AddPlaceholderChunk(PlaceholderStr);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001443 }
1444}
1445
Douglas Gregorf2510672009-09-21 19:57:38 +00001446/// \brief Add a qualifier to the given code-completion string, if the
1447/// provided nested-name-specifier is non-NULL.
Douglas Gregor0f622362009-12-11 18:44:16 +00001448static void
1449AddQualifierToCompletionString(CodeCompletionString *Result,
1450 NestedNameSpecifier *Qualifier,
1451 bool QualifierIsInformative,
1452 ASTContext &Context) {
Douglas Gregorf2510672009-09-21 19:57:38 +00001453 if (!Qualifier)
1454 return;
1455
1456 std::string PrintedNNS;
1457 {
1458 llvm::raw_string_ostream OS(PrintedNNS);
1459 Qualifier->print(OS, Context.PrintingPolicy);
1460 }
Douglas Gregor5bf52692009-09-22 23:15:58 +00001461 if (QualifierIsInformative)
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001462 Result->AddInformativeChunk(PrintedNNS);
Douglas Gregor5bf52692009-09-22 23:15:58 +00001463 else
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001464 Result->AddTextChunk(PrintedNNS);
Douglas Gregorf2510672009-09-21 19:57:38 +00001465}
1466
Douglas Gregor0f622362009-12-11 18:44:16 +00001467static void AddFunctionTypeQualsToCompletionString(CodeCompletionString *Result,
1468 FunctionDecl *Function) {
1469 const FunctionProtoType *Proto
1470 = Function->getType()->getAs<FunctionProtoType>();
1471 if (!Proto || !Proto->getTypeQuals())
1472 return;
1473
1474 std::string QualsStr;
1475 if (Proto->getTypeQuals() & Qualifiers::Const)
1476 QualsStr += " const";
1477 if (Proto->getTypeQuals() & Qualifiers::Volatile)
1478 QualsStr += " volatile";
1479 if (Proto->getTypeQuals() & Qualifiers::Restrict)
1480 QualsStr += " restrict";
1481 Result->AddInformativeChunk(QualsStr);
1482}
1483
Douglas Gregor3545ff42009-09-21 16:56:56 +00001484/// \brief If possible, create a new code completion string for the given
1485/// result.
1486///
1487/// \returns Either a new, heap-allocated code completion string describing
1488/// how to use this result, or NULL to indicate that the string or name of the
1489/// result is all that is needed.
1490CodeCompletionString *
1491CodeCompleteConsumer::Result::CreateCodeCompletionString(Sema &S) {
Douglas Gregor9eb77012009-11-07 00:00:49 +00001492 typedef CodeCompletionString::Chunk Chunk;
1493
Douglas Gregorf09935f2009-12-01 05:55:20 +00001494 if (Kind == RK_Pattern)
1495 return Pattern->Clone();
1496
1497 CodeCompletionString *Result = new CodeCompletionString;
1498
1499 if (Kind == RK_Keyword) {
1500 Result->AddTypedTextChunk(Keyword);
1501 return Result;
1502 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001503
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001504 if (Kind == RK_Macro) {
1505 MacroInfo *MI = S.PP.getMacroInfo(Macro);
Douglas Gregorf09935f2009-12-01 05:55:20 +00001506 assert(MI && "Not a macro?");
1507
1508 Result->AddTypedTextChunk(Macro->getName());
1509
1510 if (!MI->isFunctionLike())
1511 return Result;
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001512
1513 // Format a function-like macro with placeholders for the arguments.
Douglas Gregor9eb77012009-11-07 00:00:49 +00001514 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001515 for (MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end();
1516 A != AEnd; ++A) {
1517 if (A != MI->arg_begin())
Douglas Gregor9eb77012009-11-07 00:00:49 +00001518 Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001519
1520 if (!MI->isVariadic() || A != AEnd - 1) {
1521 // Non-variadic argument.
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001522 Result->AddPlaceholderChunk((*A)->getName());
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001523 continue;
1524 }
1525
1526 // Variadic argument; cope with the different between GNU and C99
1527 // variadic macros, providing a single placeholder for the rest of the
1528 // arguments.
1529 if ((*A)->isStr("__VA_ARGS__"))
1530 Result->AddPlaceholderChunk("...");
1531 else {
1532 std::string Arg = (*A)->getName();
1533 Arg += "...";
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001534 Result->AddPlaceholderChunk(Arg);
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001535 }
1536 }
Douglas Gregor9eb77012009-11-07 00:00:49 +00001537 Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001538 return Result;
1539 }
1540
1541 assert(Kind == RK_Declaration && "Missed a macro kind?");
Douglas Gregor3545ff42009-09-21 16:56:56 +00001542 NamedDecl *ND = Declaration;
1543
Douglas Gregor9eb77012009-11-07 00:00:49 +00001544 if (StartsNestedNameSpecifier) {
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001545 Result->AddTypedTextChunk(ND->getNameAsString());
Douglas Gregor9eb77012009-11-07 00:00:49 +00001546 Result->AddTextChunk("::");
1547 return Result;
1548 }
1549
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001550 AddResultTypeChunk(S.Context, ND, Result);
1551
Douglas Gregor3545ff42009-09-21 16:56:56 +00001552 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
Douglas Gregor5bf52692009-09-22 23:15:58 +00001553 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
1554 S.Context);
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001555 Result->AddTypedTextChunk(Function->getNameAsString());
Douglas Gregor9eb77012009-11-07 00:00:49 +00001556 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001557 AddFunctionParameterChunks(S.Context, Function, Result);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001558 Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
Douglas Gregor0f622362009-12-11 18:44:16 +00001559 AddFunctionTypeQualsToCompletionString(Result, Function);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001560 return Result;
1561 }
1562
1563 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
Douglas Gregor5bf52692009-09-22 23:15:58 +00001564 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
1565 S.Context);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001566 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001567 Result->AddTypedTextChunk(Function->getNameAsString());
Douglas Gregor3545ff42009-09-21 16:56:56 +00001568
1569 // Figure out which template parameters are deduced (or have default
1570 // arguments).
1571 llvm::SmallVector<bool, 16> Deduced;
1572 S.MarkDeducedTemplateParameters(FunTmpl, Deduced);
1573 unsigned LastDeducibleArgument;
1574 for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0;
1575 --LastDeducibleArgument) {
1576 if (!Deduced[LastDeducibleArgument - 1]) {
1577 // C++0x: Figure out if the template argument has a default. If so,
1578 // the user doesn't need to type this argument.
1579 // FIXME: We need to abstract template parameters better!
1580 bool HasDefaultArg = false;
1581 NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam(
1582 LastDeducibleArgument - 1);
1583 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
1584 HasDefaultArg = TTP->hasDefaultArgument();
1585 else if (NonTypeTemplateParmDecl *NTTP
1586 = dyn_cast<NonTypeTemplateParmDecl>(Param))
1587 HasDefaultArg = NTTP->hasDefaultArgument();
1588 else {
1589 assert(isa<TemplateTemplateParmDecl>(Param));
1590 HasDefaultArg
Douglas Gregor9eb77012009-11-07 00:00:49 +00001591 = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument();
Douglas Gregor3545ff42009-09-21 16:56:56 +00001592 }
1593
1594 if (!HasDefaultArg)
1595 break;
1596 }
1597 }
1598
1599 if (LastDeducibleArgument) {
1600 // Some of the function template arguments cannot be deduced from a
1601 // function call, so we introduce an explicit template argument list
1602 // containing all of the arguments up to the first deducible argument.
Douglas Gregor9eb77012009-11-07 00:00:49 +00001603 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001604 AddTemplateParameterChunks(S.Context, FunTmpl, Result,
1605 LastDeducibleArgument);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001606 Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001607 }
1608
1609 // Add the function parameters
Douglas Gregor9eb77012009-11-07 00:00:49 +00001610 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001611 AddFunctionParameterChunks(S.Context, Function, Result);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001612 Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
Douglas Gregor0f622362009-12-11 18:44:16 +00001613 AddFunctionTypeQualsToCompletionString(Result, Function);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001614 return Result;
1615 }
1616
1617 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) {
Douglas Gregor5bf52692009-09-22 23:15:58 +00001618 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
1619 S.Context);
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001620 Result->AddTypedTextChunk(Template->getNameAsString());
Douglas Gregor9eb77012009-11-07 00:00:49 +00001621 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001622 AddTemplateParameterChunks(S.Context, Template, Result);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001623 Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001624 return Result;
1625 }
1626
Douglas Gregord3c5d792009-11-17 16:44:22 +00001627 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) {
Douglas Gregord3c5d792009-11-17 16:44:22 +00001628 Selector Sel = Method->getSelector();
1629 if (Sel.isUnarySelector()) {
1630 Result->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName());
1631 return Result;
1632 }
1633
Douglas Gregor1b605f72009-11-19 01:08:35 +00001634 std::string SelName = Sel.getIdentifierInfoForSlot(0)->getName().str();
1635 SelName += ':';
1636 if (StartParameter == 0)
1637 Result->AddTypedTextChunk(SelName);
1638 else {
1639 Result->AddInformativeChunk(SelName);
1640
1641 // If there is only one parameter, and we're past it, add an empty
1642 // typed-text chunk since there is nothing to type.
1643 if (Method->param_size() == 1)
1644 Result->AddTypedTextChunk("");
1645 }
Douglas Gregord3c5d792009-11-17 16:44:22 +00001646 unsigned Idx = 0;
1647 for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
1648 PEnd = Method->param_end();
1649 P != PEnd; (void)++P, ++Idx) {
1650 if (Idx > 0) {
Douglas Gregor1b605f72009-11-19 01:08:35 +00001651 std::string Keyword;
1652 if (Idx > StartParameter)
Douglas Gregor6a803932010-01-12 06:38:28 +00001653 Result->AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregord3c5d792009-11-17 16:44:22 +00001654 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx))
1655 Keyword += II->getName().str();
1656 Keyword += ":";
Douglas Gregorc8537c52009-11-19 07:41:15 +00001657 if (Idx < StartParameter || AllParametersAreInformative) {
Douglas Gregor1b605f72009-11-19 01:08:35 +00001658 Result->AddInformativeChunk(Keyword);
1659 } else if (Idx == StartParameter)
1660 Result->AddTypedTextChunk(Keyword);
1661 else
1662 Result->AddTextChunk(Keyword);
Douglas Gregord3c5d792009-11-17 16:44:22 +00001663 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00001664
1665 // If we're before the starting parameter, skip the placeholder.
1666 if (Idx < StartParameter)
1667 continue;
Douglas Gregord3c5d792009-11-17 16:44:22 +00001668
1669 std::string Arg;
1670 (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy);
1671 Arg = "(" + Arg + ")";
1672 if (IdentifierInfo *II = (*P)->getIdentifier())
1673 Arg += II->getName().str();
Douglas Gregorc8537c52009-11-19 07:41:15 +00001674 if (AllParametersAreInformative)
1675 Result->AddInformativeChunk(Arg);
1676 else
1677 Result->AddPlaceholderChunk(Arg);
Douglas Gregord3c5d792009-11-17 16:44:22 +00001678 }
1679
Douglas Gregor04c5f972009-12-23 00:21:46 +00001680 if (Method->isVariadic()) {
1681 if (AllParametersAreInformative)
1682 Result->AddInformativeChunk(", ...");
1683 else
1684 Result->AddPlaceholderChunk(", ...");
1685 }
1686
Douglas Gregord3c5d792009-11-17 16:44:22 +00001687 return Result;
1688 }
1689
Douglas Gregorf09935f2009-12-01 05:55:20 +00001690 if (Qualifier)
Douglas Gregor5bf52692009-09-22 23:15:58 +00001691 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
1692 S.Context);
Douglas Gregorf09935f2009-12-01 05:55:20 +00001693
1694 Result->AddTypedTextChunk(ND->getNameAsString());
1695 return Result;
Douglas Gregor3545ff42009-09-21 16:56:56 +00001696}
1697
Douglas Gregorf0f51982009-09-23 00:34:09 +00001698CodeCompletionString *
1699CodeCompleteConsumer::OverloadCandidate::CreateSignatureString(
1700 unsigned CurrentArg,
1701 Sema &S) const {
Douglas Gregor9eb77012009-11-07 00:00:49 +00001702 typedef CodeCompletionString::Chunk Chunk;
1703
Douglas Gregorf0f51982009-09-23 00:34:09 +00001704 CodeCompletionString *Result = new CodeCompletionString;
1705 FunctionDecl *FDecl = getFunction();
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001706 AddResultTypeChunk(S.Context, FDecl, Result);
Douglas Gregorf0f51982009-09-23 00:34:09 +00001707 const FunctionProtoType *Proto
1708 = dyn_cast<FunctionProtoType>(getFunctionType());
1709 if (!FDecl && !Proto) {
1710 // Function without a prototype. Just give the return type and a
1711 // highlighted ellipsis.
1712 const FunctionType *FT = getFunctionType();
1713 Result->AddTextChunk(
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001714 FT->getResultType().getAsString(S.Context.PrintingPolicy));
Douglas Gregor9eb77012009-11-07 00:00:49 +00001715 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
1716 Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "..."));
1717 Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001718 return Result;
1719 }
1720
1721 if (FDecl)
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001722 Result->AddTextChunk(FDecl->getNameAsString());
Douglas Gregorf0f51982009-09-23 00:34:09 +00001723 else
1724 Result->AddTextChunk(
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001725 Proto->getResultType().getAsString(S.Context.PrintingPolicy));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001726
Douglas Gregor9eb77012009-11-07 00:00:49 +00001727 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001728 unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs();
1729 for (unsigned I = 0; I != NumParams; ++I) {
1730 if (I)
Douglas Gregor9eb77012009-11-07 00:00:49 +00001731 Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001732
1733 std::string ArgString;
1734 QualType ArgType;
1735
1736 if (FDecl) {
1737 ArgString = FDecl->getParamDecl(I)->getNameAsString();
1738 ArgType = FDecl->getParamDecl(I)->getOriginalType();
1739 } else {
1740 ArgType = Proto->getArgType(I);
1741 }
1742
1743 ArgType.getAsStringInternal(ArgString, S.Context.PrintingPolicy);
1744
1745 if (I == CurrentArg)
Douglas Gregor9eb77012009-11-07 00:00:49 +00001746 Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter,
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001747 ArgString));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001748 else
Benjamin Kramerb33a97c2009-11-29 20:18:50 +00001749 Result->AddTextChunk(ArgString);
Douglas Gregorf0f51982009-09-23 00:34:09 +00001750 }
1751
1752 if (Proto && Proto->isVariadic()) {
Douglas Gregor9eb77012009-11-07 00:00:49 +00001753 Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001754 if (CurrentArg < NumParams)
1755 Result->AddTextChunk("...");
1756 else
Douglas Gregor9eb77012009-11-07 00:00:49 +00001757 Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "..."));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001758 }
Douglas Gregor9eb77012009-11-07 00:00:49 +00001759 Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001760
1761 return Result;
1762}
1763
Douglas Gregor3545ff42009-09-21 16:56:56 +00001764namespace {
1765 struct SortCodeCompleteResult {
1766 typedef CodeCompleteConsumer::Result Result;
1767
Douglas Gregore6688e62009-09-28 03:51:44 +00001768 bool isEarlierDeclarationName(DeclarationName X, DeclarationName Y) const {
Douglas Gregor249d6822009-12-05 09:08:56 +00001769 Selector XSel = X.getObjCSelector();
1770 Selector YSel = Y.getObjCSelector();
1771 if (!XSel.isNull() && !YSel.isNull()) {
1772 // We are comparing two selectors.
1773 unsigned N = std::min(XSel.getNumArgs(), YSel.getNumArgs());
1774 if (N == 0)
1775 ++N;
1776 for (unsigned I = 0; I != N; ++I) {
1777 IdentifierInfo *XId = XSel.getIdentifierInfoForSlot(I);
1778 IdentifierInfo *YId = YSel.getIdentifierInfoForSlot(I);
1779 if (!XId || !YId)
1780 return XId && !YId;
1781
1782 switch (XId->getName().compare_lower(YId->getName())) {
1783 case -1: return true;
1784 case 1: return false;
1785 default: break;
1786 }
1787 }
1788
1789 return XSel.getNumArgs() < YSel.getNumArgs();
1790 }
1791
1792 // For non-selectors, order by kind.
1793 if (X.getNameKind() != Y.getNameKind())
Douglas Gregore6688e62009-09-28 03:51:44 +00001794 return X.getNameKind() < Y.getNameKind();
1795
Douglas Gregor249d6822009-12-05 09:08:56 +00001796 // Order identifiers by comparison of their lowercased names.
1797 if (IdentifierInfo *XId = X.getAsIdentifierInfo())
1798 return XId->getName().compare_lower(
1799 Y.getAsIdentifierInfo()->getName()) < 0;
1800
1801 // Order overloaded operators by the order in which they appear
1802 // in our list of operators.
1803 if (OverloadedOperatorKind XOp = X.getCXXOverloadedOperator())
1804 return XOp < Y.getCXXOverloadedOperator();
1805
1806 // Order C++0x user-defined literal operators lexically by their
1807 // lowercased suffixes.
1808 if (IdentifierInfo *XLit = X.getCXXLiteralIdentifier())
1809 return XLit->getName().compare_lower(
1810 Y.getCXXLiteralIdentifier()->getName()) < 0;
1811
1812 // The only stable ordering we have is to turn the name into a
1813 // string and then compare the lower-case strings. This is
1814 // inefficient, but thankfully does not happen too often.
Benjamin Kramer4053e5d2009-12-05 10:22:15 +00001815 return llvm::StringRef(X.getAsString()).compare_lower(
1816 Y.getAsString()) < 0;
Douglas Gregore6688e62009-09-28 03:51:44 +00001817 }
1818
Douglas Gregor52ce62f2010-01-13 23:24:38 +00001819 /// \brief Retrieve the name that should be used to order a result.
1820 ///
1821 /// If the name needs to be constructed as a string, that string will be
1822 /// saved into Saved and the returned StringRef will refer to it.
1823 static llvm::StringRef getOrderedName(const Result &R,
1824 std::string &Saved) {
1825 switch (R.Kind) {
1826 case Result::RK_Keyword:
1827 return R.Keyword;
1828
1829 case Result::RK_Pattern:
1830 return R.Pattern->getTypedText();
1831
1832 case Result::RK_Macro:
1833 return R.Macro->getName();
1834
1835 case Result::RK_Declaration:
1836 // Handle declarations below.
1837 break;
Douglas Gregor45f83ee2009-11-19 00:01:57 +00001838 }
Douglas Gregor52ce62f2010-01-13 23:24:38 +00001839
1840 DeclarationName Name = R.Declaration->getDeclName();
Douglas Gregor45f83ee2009-11-19 00:01:57 +00001841
Douglas Gregor52ce62f2010-01-13 23:24:38 +00001842 // If the name is a simple identifier (by far the common case), or a
1843 // zero-argument selector, just return a reference to that identifier.
1844 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
1845 return Id->getName();
1846 if (Name.isObjCZeroArgSelector())
1847 if (IdentifierInfo *Id
1848 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
1849 return Id->getName();
1850
1851 Saved = Name.getAsString();
1852 return Saved;
1853 }
1854
1855 bool operator()(const Result &X, const Result &Y) const {
1856 std::string XSaved, YSaved;
1857 llvm::StringRef XStr = getOrderedName(X, XSaved);
1858 llvm::StringRef YStr = getOrderedName(Y, YSaved);
1859 int cmp = XStr.compare_lower(YStr);
1860 if (cmp)
1861 return cmp < 0;
Douglas Gregor3545ff42009-09-21 16:56:56 +00001862
1863 // Non-hidden names precede hidden names.
1864 if (X.Hidden != Y.Hidden)
1865 return !X.Hidden;
1866
Douglas Gregore412a5a2009-09-23 22:26:46 +00001867 // Non-nested-name-specifiers precede nested-name-specifiers.
1868 if (X.StartsNestedNameSpecifier != Y.StartsNestedNameSpecifier)
1869 return !X.StartsNestedNameSpecifier;
1870
Douglas Gregor3545ff42009-09-21 16:56:56 +00001871 return false;
1872 }
1873 };
1874}
1875
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001876static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results) {
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001877 Results.EnterNewScope();
Douglas Gregor9eb77012009-11-07 00:00:49 +00001878 for (Preprocessor::macro_iterator M = PP.macro_begin(),
1879 MEnd = PP.macro_end();
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001880 M != MEnd; ++M)
Douglas Gregor78a21012010-01-14 16:01:26 +00001881 Results.AddResult(M->first);
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001882 Results.ExitScope();
1883}
1884
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001885static void HandleCodeCompleteResults(Sema *S,
1886 CodeCompleteConsumer *CodeCompleter,
1887 CodeCompleteConsumer::Result *Results,
1888 unsigned NumResults) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00001889 std::stable_sort(Results, Results + NumResults, SortCodeCompleteResult());
1890
1891 if (CodeCompleter)
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001892 CodeCompleter->ProcessCodeCompleteResults(*S, Results, NumResults);
Douglas Gregor45f83ee2009-11-19 00:01:57 +00001893
1894 for (unsigned I = 0; I != NumResults; ++I)
1895 Results[I].Destroy();
Douglas Gregor3545ff42009-09-21 16:56:56 +00001896}
1897
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001898void Sema::CodeCompleteOrdinaryName(Scope *S,
1899 CodeCompletionContext CompletionContext) {
Douglas Gregor92253692009-12-07 09:54:55 +00001900 typedef CodeCompleteConsumer::Result Result;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001901 ResultBuilder Results(*this);
1902
1903 // Determine how to filter results, e.g., so that the names of
1904 // values (functions, enumerators, function templates, etc.) are
1905 // only allowed where we can have an expression.
1906 switch (CompletionContext) {
1907 case CCC_Namespace:
1908 case CCC_Class:
Douglas Gregorf1934162010-01-13 21:24:21 +00001909 case CCC_ObjCInterface:
1910 case CCC_ObjCImplementation:
Douglas Gregor48d46252010-01-13 21:54:15 +00001911 case CCC_ObjCInstanceVariableList:
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001912 case CCC_Template:
1913 case CCC_MemberTemplate:
1914 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
1915 break;
1916
1917 case CCC_Expression:
1918 case CCC_Statement:
1919 case CCC_ForInit:
1920 case CCC_Condition:
1921 Results.setFilter(&ResultBuilder::IsOrdinaryName);
1922 break;
1923 }
1924
Douglas Gregorc580c522010-01-14 01:09:38 +00001925 CodeCompletionDeclConsumer Consumer(Results, CurContext);
1926 LookupVisibleDecls(S, LookupOrdinaryName, Consumer);
Douglas Gregor92253692009-12-07 09:54:55 +00001927
1928 Results.EnterNewScope();
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001929 AddOrdinaryNameResults(CompletionContext, S, *this, Results);
Douglas Gregor92253692009-12-07 09:54:55 +00001930 Results.ExitScope();
1931
Douglas Gregor9eb77012009-11-07 00:00:49 +00001932 if (CodeCompleter->includeMacros())
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001933 AddMacroResults(PP, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001934 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001935}
1936
Douglas Gregor9291bad2009-11-18 01:29:26 +00001937static void AddObjCProperties(ObjCContainerDecl *Container,
Douglas Gregor5d649882009-11-18 22:32:06 +00001938 bool AllowCategories,
Douglas Gregor9291bad2009-11-18 01:29:26 +00001939 DeclContext *CurContext,
1940 ResultBuilder &Results) {
1941 typedef CodeCompleteConsumer::Result Result;
1942
1943 // Add properties in this container.
1944 for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(),
1945 PEnd = Container->prop_end();
1946 P != PEnd;
1947 ++P)
1948 Results.MaybeAddResult(Result(*P, 0), CurContext);
1949
1950 // Add properties in referenced protocols.
1951 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
1952 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
1953 PEnd = Protocol->protocol_end();
1954 P != PEnd; ++P)
Douglas Gregor5d649882009-11-18 22:32:06 +00001955 AddObjCProperties(*P, AllowCategories, CurContext, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00001956 } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){
Douglas Gregor5d649882009-11-18 22:32:06 +00001957 if (AllowCategories) {
1958 // Look through categories.
1959 for (ObjCCategoryDecl *Category = IFace->getCategoryList();
1960 Category; Category = Category->getNextClassCategory())
1961 AddObjCProperties(Category, AllowCategories, CurContext, Results);
1962 }
Douglas Gregor9291bad2009-11-18 01:29:26 +00001963
1964 // Look through protocols.
1965 for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(),
1966 E = IFace->protocol_end();
1967 I != E; ++I)
Douglas Gregor5d649882009-11-18 22:32:06 +00001968 AddObjCProperties(*I, AllowCategories, CurContext, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00001969
1970 // Look in the superclass.
1971 if (IFace->getSuperClass())
Douglas Gregor5d649882009-11-18 22:32:06 +00001972 AddObjCProperties(IFace->getSuperClass(), AllowCategories, CurContext,
1973 Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00001974 } else if (const ObjCCategoryDecl *Category
1975 = dyn_cast<ObjCCategoryDecl>(Container)) {
1976 // Look through protocols.
1977 for (ObjCInterfaceDecl::protocol_iterator P = Category->protocol_begin(),
1978 PEnd = Category->protocol_end();
1979 P != PEnd; ++P)
Douglas Gregor5d649882009-11-18 22:32:06 +00001980 AddObjCProperties(*P, AllowCategories, CurContext, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00001981 }
1982}
1983
Douglas Gregor2436e712009-09-17 21:32:03 +00001984void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE,
1985 SourceLocation OpLoc,
1986 bool IsArrow) {
1987 if (!BaseE || !CodeCompleter)
1988 return;
1989
Douglas Gregor3545ff42009-09-21 16:56:56 +00001990 typedef CodeCompleteConsumer::Result Result;
1991
Douglas Gregor2436e712009-09-17 21:32:03 +00001992 Expr *Base = static_cast<Expr *>(BaseE);
1993 QualType BaseType = Base->getType();
Douglas Gregor3545ff42009-09-21 16:56:56 +00001994
1995 if (IsArrow) {
1996 if (const PointerType *Ptr = BaseType->getAs<PointerType>())
1997 BaseType = Ptr->getPointeeType();
1998 else if (BaseType->isObjCObjectPointerType())
1999 /*Do nothing*/ ;
2000 else
2001 return;
2002 }
2003
Douglas Gregore412a5a2009-09-23 22:26:46 +00002004 ResultBuilder Results(*this, &ResultBuilder::IsMember);
Douglas Gregor9291bad2009-11-18 01:29:26 +00002005 Results.EnterNewScope();
2006 if (const RecordType *Record = BaseType->getAs<RecordType>()) {
2007 // Access to a C/C++ class, struct, or union.
Douglas Gregor6ae4c522010-01-14 03:21:49 +00002008 Results.allowNestedNameSpecifiers();
Douglas Gregor09bbc652010-01-14 15:47:35 +00002009 CodeCompletionDeclConsumer Consumer(Results, CurContext);
2010 LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002011
Douglas Gregor9291bad2009-11-18 01:29:26 +00002012 if (getLangOptions().CPlusPlus) {
2013 if (!Results.empty()) {
2014 // The "template" keyword can follow "->" or "." in the grammar.
2015 // However, we only want to suggest the template keyword if something
2016 // is dependent.
2017 bool IsDependent = BaseType->isDependentType();
2018 if (!IsDependent) {
2019 for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent())
2020 if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) {
2021 IsDependent = Ctx->isDependentContext();
2022 break;
2023 }
2024 }
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002025
Douglas Gregor9291bad2009-11-18 01:29:26 +00002026 if (IsDependent)
Douglas Gregor78a21012010-01-14 16:01:26 +00002027 Results.AddResult(Result("template"));
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002028 }
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002029 }
Douglas Gregor9291bad2009-11-18 01:29:26 +00002030 } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) {
2031 // Objective-C property reference.
2032
2033 // Add property results based on our interface.
2034 const ObjCObjectPointerType *ObjCPtr
2035 = BaseType->getAsObjCInterfacePointerType();
2036 assert(ObjCPtr && "Non-NULL pointer guaranteed above!");
Douglas Gregor5d649882009-11-18 22:32:06 +00002037 AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, CurContext, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00002038
2039 // Add properties from the protocols in a qualified interface.
2040 for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(),
2041 E = ObjCPtr->qual_end();
2042 I != E; ++I)
Douglas Gregor5d649882009-11-18 22:32:06 +00002043 AddObjCProperties(*I, true, CurContext, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00002044 } else if ((IsArrow && BaseType->isObjCObjectPointerType()) ||
2045 (!IsArrow && BaseType->isObjCInterfaceType())) {
2046 // Objective-C instance variable access.
2047 ObjCInterfaceDecl *Class = 0;
2048 if (const ObjCObjectPointerType *ObjCPtr
2049 = BaseType->getAs<ObjCObjectPointerType>())
2050 Class = ObjCPtr->getInterfaceDecl();
2051 else
2052 Class = BaseType->getAs<ObjCInterfaceType>()->getDecl();
2053
2054 // Add all ivars from this class and its superclasses.
2055 for (; Class; Class = Class->getSuperClass()) {
2056 for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(),
2057 IVarEnd = Class->ivar_end();
2058 IVar != IVarEnd; ++IVar)
2059 Results.MaybeAddResult(Result(*IVar, 0), CurContext);
2060 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00002061 }
Douglas Gregor9291bad2009-11-18 01:29:26 +00002062
2063 // FIXME: How do we cope with isa?
2064
2065 Results.ExitScope();
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002066
2067 // Add macros
2068 if (CodeCompleter->includeMacros())
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002069 AddMacroResults(PP, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002070
2071 // Hand off the results found for code completion.
2072 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor2436e712009-09-17 21:32:03 +00002073}
2074
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002075void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) {
2076 if (!CodeCompleter)
2077 return;
2078
Douglas Gregor3545ff42009-09-21 16:56:56 +00002079 typedef CodeCompleteConsumer::Result Result;
2080 ResultBuilder::LookupFilter Filter = 0;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002081 switch ((DeclSpec::TST)TagSpec) {
2082 case DeclSpec::TST_enum:
Douglas Gregor3545ff42009-09-21 16:56:56 +00002083 Filter = &ResultBuilder::IsEnum;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002084 break;
2085
2086 case DeclSpec::TST_union:
Douglas Gregor3545ff42009-09-21 16:56:56 +00002087 Filter = &ResultBuilder::IsUnion;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002088 break;
2089
2090 case DeclSpec::TST_struct:
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002091 case DeclSpec::TST_class:
Douglas Gregor3545ff42009-09-21 16:56:56 +00002092 Filter = &ResultBuilder::IsClassOrStruct;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002093 break;
2094
2095 default:
2096 assert(false && "Unknown type specifier kind in CodeCompleteTag");
2097 return;
2098 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00002099
2100 ResultBuilder Results(*this, Filter);
Douglas Gregor6ae4c522010-01-14 03:21:49 +00002101 Results.allowNestedNameSpecifiers();
Douglas Gregora6e2edc2010-01-14 03:27:13 +00002102 CodeCompletionDeclConsumer Consumer(Results, CurContext);
2103 LookupVisibleDecls(S, LookupTagName, Consumer);
Douglas Gregor3545ff42009-09-21 16:56:56 +00002104
Douglas Gregor9eb77012009-11-07 00:00:49 +00002105 if (CodeCompleter->includeMacros())
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002106 AddMacroResults(PP, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002107 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002108}
2109
Douglas Gregord328d572009-09-21 18:10:23 +00002110void Sema::CodeCompleteCase(Scope *S) {
2111 if (getSwitchStack().empty() || !CodeCompleter)
2112 return;
2113
2114 SwitchStmt *Switch = getSwitchStack().back();
2115 if (!Switch->getCond()->getType()->isEnumeralType())
2116 return;
2117
2118 // Code-complete the cases of a switch statement over an enumeration type
2119 // by providing the list of
2120 EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl();
2121
2122 // Determine which enumerators we have already seen in the switch statement.
2123 // FIXME: Ideally, we would also be able to look *past* the code-completion
2124 // token, in case we are code-completing in the middle of the switch and not
2125 // at the end. However, we aren't able to do so at the moment.
2126 llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen;
Douglas Gregorf2510672009-09-21 19:57:38 +00002127 NestedNameSpecifier *Qualifier = 0;
Douglas Gregord328d572009-09-21 18:10:23 +00002128 for (SwitchCase *SC = Switch->getSwitchCaseList(); SC;
2129 SC = SC->getNextSwitchCase()) {
2130 CaseStmt *Case = dyn_cast<CaseStmt>(SC);
2131 if (!Case)
2132 continue;
2133
2134 Expr *CaseVal = Case->getLHS()->IgnoreParenCasts();
2135 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal))
2136 if (EnumConstantDecl *Enumerator
2137 = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
2138 // We look into the AST of the case statement to determine which
2139 // enumerator was named. Alternatively, we could compute the value of
2140 // the integral constant expression, then compare it against the
2141 // values of each enumerator. However, value-based approach would not
2142 // work as well with C++ templates where enumerators declared within a
2143 // template are type- and value-dependent.
2144 EnumeratorsSeen.insert(Enumerator);
2145
Douglas Gregorf2510672009-09-21 19:57:38 +00002146 // If this is a qualified-id, keep track of the nested-name-specifier
2147 // so that we can reproduce it as part of code completion, e.g.,
Douglas Gregord328d572009-09-21 18:10:23 +00002148 //
2149 // switch (TagD.getKind()) {
2150 // case TagDecl::TK_enum:
2151 // break;
2152 // case XXX
2153 //
Douglas Gregorf2510672009-09-21 19:57:38 +00002154 // At the XXX, our completions are TagDecl::TK_union,
Douglas Gregord328d572009-09-21 18:10:23 +00002155 // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union,
2156 // TK_struct, and TK_class.
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002157 Qualifier = DRE->getQualifier();
Douglas Gregord328d572009-09-21 18:10:23 +00002158 }
2159 }
2160
Douglas Gregorf2510672009-09-21 19:57:38 +00002161 if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) {
2162 // If there are no prior enumerators in C++, check whether we have to
2163 // qualify the names of the enumerators that we suggest, because they
2164 // may not be visible in this scope.
2165 Qualifier = getRequiredQualification(Context, CurContext,
2166 Enum->getDeclContext());
2167
2168 // FIXME: Scoped enums need to start with "EnumDecl" as the context!
2169 }
2170
Douglas Gregord328d572009-09-21 18:10:23 +00002171 // Add any enumerators that have not yet been mentioned.
2172 ResultBuilder Results(*this);
2173 Results.EnterNewScope();
2174 for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(),
2175 EEnd = Enum->enumerator_end();
2176 E != EEnd; ++E) {
2177 if (EnumeratorsSeen.count(*E))
2178 continue;
2179
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002180 Results.MaybeAddResult(CodeCompleteConsumer::Result(*E, Qualifier));
Douglas Gregord328d572009-09-21 18:10:23 +00002181 }
2182 Results.ExitScope();
2183
Douglas Gregor9eb77012009-11-07 00:00:49 +00002184 if (CodeCompleter->includeMacros())
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002185 AddMacroResults(PP, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002186 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregord328d572009-09-21 18:10:23 +00002187}
2188
Douglas Gregorcabea402009-09-22 15:41:20 +00002189namespace {
2190 struct IsBetterOverloadCandidate {
2191 Sema &S;
2192
2193 public:
2194 explicit IsBetterOverloadCandidate(Sema &S) : S(S) { }
2195
2196 bool
2197 operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const {
2198 return S.isBetterOverloadCandidate(X, Y);
2199 }
2200 };
2201}
2202
2203void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn,
2204 ExprTy **ArgsIn, unsigned NumArgs) {
2205 if (!CodeCompleter)
2206 return;
Douglas Gregor3ef59522009-12-11 19:06:04 +00002207
2208 // When we're code-completing for a call, we fall back to ordinary
2209 // name code-completion whenever we can't produce specific
2210 // results. We may want to revisit this strategy in the future,
2211 // e.g., by merging the two kinds of results.
2212
Douglas Gregorcabea402009-09-22 15:41:20 +00002213 Expr *Fn = (Expr *)FnIn;
2214 Expr **Args = (Expr **)ArgsIn;
Douglas Gregor3ef59522009-12-11 19:06:04 +00002215
Douglas Gregorcabea402009-09-22 15:41:20 +00002216 // Ignore type-dependent call expressions entirely.
2217 if (Fn->isTypeDependent() ||
Douglas Gregor3ef59522009-12-11 19:06:04 +00002218 Expr::hasAnyTypeDependentArguments(Args, NumArgs)) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00002219 CodeCompleteOrdinaryName(S, CCC_Expression);
Douglas Gregorcabea402009-09-22 15:41:20 +00002220 return;
Douglas Gregor3ef59522009-12-11 19:06:04 +00002221 }
Douglas Gregorcabea402009-09-22 15:41:20 +00002222
John McCall57500772009-12-16 12:17:52 +00002223 // Build an overload candidate set based on the functions we find.
2224 OverloadCandidateSet CandidateSet;
2225
Douglas Gregorcabea402009-09-22 15:41:20 +00002226 // FIXME: What if we're calling something that isn't a function declaration?
2227 // FIXME: What if we're calling a pseudo-destructor?
2228 // FIXME: What if we're calling a member function?
2229
John McCall57500772009-12-16 12:17:52 +00002230 Expr *NakedFn = Fn->IgnoreParenCasts();
2231 if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn))
2232 AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet,
2233 /*PartialOverloading=*/ true);
2234 else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) {
2235 FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
2236 if (FDecl)
2237 AddOverloadCandidate(FDecl, Args, NumArgs, CandidateSet,
2238 false, false, /*PartialOverloading*/ true);
2239 }
Douglas Gregorcabea402009-09-22 15:41:20 +00002240
2241 // Sort the overload candidate set by placing the best overloads first.
2242 std::stable_sort(CandidateSet.begin(), CandidateSet.end(),
2243 IsBetterOverloadCandidate(*this));
2244
2245 // Add the remaining viable overload candidates as code-completion reslults.
Douglas Gregor05f477c2009-09-23 00:16:58 +00002246 typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate;
2247 llvm::SmallVector<ResultCandidate, 8> Results;
Anders Carlssone7ceb852009-09-22 17:29:51 +00002248
Douglas Gregorcabea402009-09-22 15:41:20 +00002249 for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
2250 CandEnd = CandidateSet.end();
2251 Cand != CandEnd; ++Cand) {
2252 if (Cand->Viable)
Douglas Gregor05f477c2009-09-23 00:16:58 +00002253 Results.push_back(ResultCandidate(Cand->Function));
Douglas Gregorcabea402009-09-22 15:41:20 +00002254 }
Douglas Gregor3ef59522009-12-11 19:06:04 +00002255
2256 if (Results.empty())
Douglas Gregor504a6ae2010-01-10 23:08:15 +00002257 CodeCompleteOrdinaryName(S, CCC_Expression);
Douglas Gregor3ef59522009-12-11 19:06:04 +00002258 else
2259 CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(),
2260 Results.size());
Douglas Gregorcabea402009-09-22 15:41:20 +00002261}
2262
Douglas Gregor2436e712009-09-17 21:32:03 +00002263void Sema::CodeCompleteQualifiedId(Scope *S, const CXXScopeSpec &SS,
2264 bool EnteringContext) {
2265 if (!SS.getScopeRep() || !CodeCompleter)
2266 return;
2267
Douglas Gregor3545ff42009-09-21 16:56:56 +00002268 DeclContext *Ctx = computeDeclContext(SS, EnteringContext);
2269 if (!Ctx)
2270 return;
Douglas Gregor800f2f02009-12-11 18:28:39 +00002271
2272 // Try to instantiate any non-dependent declaration contexts before
2273 // we look in them.
2274 if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS))
2275 return;
2276
Douglas Gregor3545ff42009-09-21 16:56:56 +00002277 ResultBuilder Results(*this);
Douglas Gregor200c99d2010-01-14 03:35:48 +00002278 CodeCompletionDeclConsumer Consumer(Results, CurContext);
2279 LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer);
Douglas Gregor3545ff42009-09-21 16:56:56 +00002280
2281 // The "template" keyword can follow "::" in the grammar, but only
2282 // put it into the grammar if the nested-name-specifier is dependent.
2283 NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
2284 if (!Results.empty() && NNS->isDependent())
Douglas Gregor78a21012010-01-14 16:01:26 +00002285 Results.AddResult("template");
Douglas Gregor3545ff42009-09-21 16:56:56 +00002286
Douglas Gregor9eb77012009-11-07 00:00:49 +00002287 if (CodeCompleter->includeMacros())
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002288 AddMacroResults(PP, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002289 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor2436e712009-09-17 21:32:03 +00002290}
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00002291
2292void Sema::CodeCompleteUsing(Scope *S) {
2293 if (!CodeCompleter)
2294 return;
2295
Douglas Gregor3545ff42009-09-21 16:56:56 +00002296 ResultBuilder Results(*this, &ResultBuilder::IsNestedNameSpecifier);
Douglas Gregor64b12b52009-09-22 23:31:26 +00002297 Results.EnterNewScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00002298
2299 // If we aren't in class scope, we could see the "namespace" keyword.
2300 if (!S->isClassScope())
Douglas Gregor78a21012010-01-14 16:01:26 +00002301 Results.AddResult(CodeCompleteConsumer::Result("namespace"));
Douglas Gregor3545ff42009-09-21 16:56:56 +00002302
2303 // After "using", we can see anything that would start a
2304 // nested-name-specifier.
Douglas Gregora6e2edc2010-01-14 03:27:13 +00002305 CodeCompletionDeclConsumer Consumer(Results, CurContext);
2306 LookupVisibleDecls(S, LookupOrdinaryName, Consumer);
Douglas Gregor64b12b52009-09-22 23:31:26 +00002307 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00002308
Douglas Gregor9eb77012009-11-07 00:00:49 +00002309 if (CodeCompleter->includeMacros())
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002310 AddMacroResults(PP, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002311 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00002312}
2313
2314void Sema::CodeCompleteUsingDirective(Scope *S) {
2315 if (!CodeCompleter)
2316 return;
2317
Douglas Gregor3545ff42009-09-21 16:56:56 +00002318 // After "using namespace", we expect to see a namespace name or namespace
2319 // alias.
2320 ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregor64b12b52009-09-22 23:31:26 +00002321 Results.EnterNewScope();
Douglas Gregora6e2edc2010-01-14 03:27:13 +00002322 CodeCompletionDeclConsumer Consumer(Results, CurContext);
2323 LookupVisibleDecls(S, LookupOrdinaryName, Consumer);
Douglas Gregor64b12b52009-09-22 23:31:26 +00002324 Results.ExitScope();
Douglas Gregor9eb77012009-11-07 00:00:49 +00002325 if (CodeCompleter->includeMacros())
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002326 AddMacroResults(PP, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002327 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00002328}
2329
2330void Sema::CodeCompleteNamespaceDecl(Scope *S) {
2331 if (!CodeCompleter)
2332 return;
2333
Douglas Gregor3545ff42009-09-21 16:56:56 +00002334 ResultBuilder Results(*this, &ResultBuilder::IsNamespace);
2335 DeclContext *Ctx = (DeclContext *)S->getEntity();
2336 if (!S->getParent())
2337 Ctx = Context.getTranslationUnitDecl();
2338
2339 if (Ctx && Ctx->isFileContext()) {
2340 // We only want to see those namespaces that have already been defined
2341 // within this scope, because its likely that the user is creating an
2342 // extended namespace declaration. Keep track of the most recent
2343 // definition of each namespace.
2344 std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest;
2345 for (DeclContext::specific_decl_iterator<NamespaceDecl>
2346 NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end());
2347 NS != NSEnd; ++NS)
2348 OrigToLatest[NS->getOriginalNamespace()] = *NS;
2349
2350 // Add the most recent definition (or extended definition) of each
2351 // namespace to the list of results.
Douglas Gregor64b12b52009-09-22 23:31:26 +00002352 Results.EnterNewScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00002353 for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator
2354 NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end();
2355 NS != NSEnd; ++NS)
Douglas Gregor2af2f672009-09-21 20:12:40 +00002356 Results.MaybeAddResult(CodeCompleteConsumer::Result(NS->second, 0),
2357 CurContext);
Douglas Gregor64b12b52009-09-22 23:31:26 +00002358 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00002359 }
2360
Douglas Gregor9eb77012009-11-07 00:00:49 +00002361 if (CodeCompleter->includeMacros())
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002362 AddMacroResults(PP, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002363 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00002364}
2365
2366void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) {
2367 if (!CodeCompleter)
2368 return;
2369
Douglas Gregor3545ff42009-09-21 16:56:56 +00002370 // After "namespace", we expect to see a namespace or alias.
2371 ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregora6e2edc2010-01-14 03:27:13 +00002372 CodeCompletionDeclConsumer Consumer(Results, CurContext);
2373 LookupVisibleDecls(S, LookupOrdinaryName, Consumer);
Douglas Gregor9eb77012009-11-07 00:00:49 +00002374 if (CodeCompleter->includeMacros())
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002375 AddMacroResults(PP, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002376 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00002377}
2378
Douglas Gregorc811ede2009-09-18 20:05:18 +00002379void Sema::CodeCompleteOperatorName(Scope *S) {
2380 if (!CodeCompleter)
2381 return;
Douglas Gregor3545ff42009-09-21 16:56:56 +00002382
2383 typedef CodeCompleteConsumer::Result Result;
2384 ResultBuilder Results(*this, &ResultBuilder::IsType);
Douglas Gregor64b12b52009-09-22 23:31:26 +00002385 Results.EnterNewScope();
Douglas Gregorc811ede2009-09-18 20:05:18 +00002386
Douglas Gregor3545ff42009-09-21 16:56:56 +00002387 // Add the names of overloadable operators.
2388#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2389 if (std::strcmp(Spelling, "?")) \
Douglas Gregor78a21012010-01-14 16:01:26 +00002390 Results.AddResult(Result(Spelling));
Douglas Gregor3545ff42009-09-21 16:56:56 +00002391#include "clang/Basic/OperatorKinds.def"
2392
2393 // Add any type names visible from the current scope
Douglas Gregor6ae4c522010-01-14 03:21:49 +00002394 Results.allowNestedNameSpecifiers();
Douglas Gregora6e2edc2010-01-14 03:27:13 +00002395 CodeCompletionDeclConsumer Consumer(Results, CurContext);
2396 LookupVisibleDecls(S, LookupOrdinaryName, Consumer);
Douglas Gregor3545ff42009-09-21 16:56:56 +00002397
2398 // Add any type specifiers
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002399 AddTypeSpecifierResults(getLangOptions(), Results);
Douglas Gregor64b12b52009-09-22 23:31:26 +00002400 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00002401
Douglas Gregor9eb77012009-11-07 00:00:49 +00002402 if (CodeCompleter->includeMacros())
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002403 AddMacroResults(PP, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002404 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregorc811ede2009-09-18 20:05:18 +00002405}
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00002406
Douglas Gregorf1934162010-01-13 21:24:21 +00002407// Macro that expands to @Keyword or Keyword, depending on whether NeedAt is
2408// true or false.
2409#define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) NeedAt? "@" #Keyword : #Keyword
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002410static void AddObjCImplementationResults(const LangOptions &LangOpts,
Douglas Gregorf1934162010-01-13 21:24:21 +00002411 ResultBuilder &Results,
2412 bool NeedAt) {
2413 typedef CodeCompleteConsumer::Result Result;
2414 // Since we have an implementation, we can end it.
Douglas Gregor78a21012010-01-14 16:01:26 +00002415 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end)));
Douglas Gregorf1934162010-01-13 21:24:21 +00002416
2417 CodeCompletionString *Pattern = 0;
2418 if (LangOpts.ObjC2) {
2419 // @dynamic
2420 Pattern = new CodeCompletionString;
2421 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,dynamic));
2422 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2423 Pattern->AddPlaceholderChunk("property");
Douglas Gregor78a21012010-01-14 16:01:26 +00002424 Results.AddResult(Result(Pattern));
Douglas Gregorf1934162010-01-13 21:24:21 +00002425
2426 // @synthesize
2427 Pattern = new CodeCompletionString;
2428 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synthesize));
2429 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2430 Pattern->AddPlaceholderChunk("property");
Douglas Gregor78a21012010-01-14 16:01:26 +00002431 Results.AddResult(Result(Pattern));
Douglas Gregorf1934162010-01-13 21:24:21 +00002432 }
2433}
2434
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002435static void AddObjCInterfaceResults(const LangOptions &LangOpts,
Douglas Gregorf1934162010-01-13 21:24:21 +00002436 ResultBuilder &Results,
2437 bool NeedAt) {
2438 typedef CodeCompleteConsumer::Result Result;
2439
2440 // Since we have an interface or protocol, we can end it.
Douglas Gregor78a21012010-01-14 16:01:26 +00002441 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end)));
Douglas Gregorf1934162010-01-13 21:24:21 +00002442
2443 if (LangOpts.ObjC2) {
2444 // @property
Douglas Gregor78a21012010-01-14 16:01:26 +00002445 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,property)));
Douglas Gregorf1934162010-01-13 21:24:21 +00002446
2447 // @required
Douglas Gregor78a21012010-01-14 16:01:26 +00002448 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,required)));
Douglas Gregorf1934162010-01-13 21:24:21 +00002449
2450 // @optional
Douglas Gregor78a21012010-01-14 16:01:26 +00002451 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,optional)));
Douglas Gregorf1934162010-01-13 21:24:21 +00002452 }
2453}
2454
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002455static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) {
Douglas Gregorf1934162010-01-13 21:24:21 +00002456 typedef CodeCompleteConsumer::Result Result;
2457 CodeCompletionString *Pattern = 0;
2458
2459 // @class name ;
2460 Pattern = new CodeCompletionString;
2461 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,class));
2462 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2463 Pattern->AddPlaceholderChunk("identifier");
2464 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +00002465 Results.AddResult(Result(Pattern));
Douglas Gregorf1934162010-01-13 21:24:21 +00002466
2467 // @interface name
2468 // FIXME: Could introduce the whole pattern, including superclasses and
2469 // such.
2470 Pattern = new CodeCompletionString;
2471 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,interface));
2472 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2473 Pattern->AddPlaceholderChunk("class");
Douglas Gregor78a21012010-01-14 16:01:26 +00002474 Results.AddResult(Result(Pattern));
Douglas Gregorf1934162010-01-13 21:24:21 +00002475
2476 // @protocol name
2477 Pattern = new CodeCompletionString;
2478 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol));
2479 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2480 Pattern->AddPlaceholderChunk("protocol");
Douglas Gregor78a21012010-01-14 16:01:26 +00002481 Results.AddResult(Result(Pattern));
Douglas Gregorf1934162010-01-13 21:24:21 +00002482
2483 // @implementation name
2484 Pattern = new CodeCompletionString;
2485 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,implementation));
2486 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2487 Pattern->AddPlaceholderChunk("class");
Douglas Gregor78a21012010-01-14 16:01:26 +00002488 Results.AddResult(Result(Pattern));
Douglas Gregorf1934162010-01-13 21:24:21 +00002489
2490 // @compatibility_alias name
2491 Pattern = new CodeCompletionString;
2492 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,compatibility_alias));
2493 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2494 Pattern->AddPlaceholderChunk("alias");
2495 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2496 Pattern->AddPlaceholderChunk("class");
Douglas Gregor78a21012010-01-14 16:01:26 +00002497 Results.AddResult(Result(Pattern));
Douglas Gregorf1934162010-01-13 21:24:21 +00002498}
2499
Douglas Gregorf48706c2009-12-07 09:27:33 +00002500void Sema::CodeCompleteObjCAtDirective(Scope *S, DeclPtrTy ObjCImpDecl,
2501 bool InInterface) {
2502 typedef CodeCompleteConsumer::Result Result;
2503 ResultBuilder Results(*this);
2504 Results.EnterNewScope();
Douglas Gregorf1934162010-01-13 21:24:21 +00002505 if (ObjCImpDecl)
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002506 AddObjCImplementationResults(getLangOptions(), Results, false);
Douglas Gregorf1934162010-01-13 21:24:21 +00002507 else if (InInterface)
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002508 AddObjCInterfaceResults(getLangOptions(), Results, false);
Douglas Gregorf1934162010-01-13 21:24:21 +00002509 else
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002510 AddObjCTopLevelResults(Results, false);
Douglas Gregorf48706c2009-12-07 09:27:33 +00002511 Results.ExitScope();
2512 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2513}
2514
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002515static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002516 typedef CodeCompleteConsumer::Result Result;
2517 CodeCompletionString *Pattern = 0;
2518
2519 // @encode ( type-name )
2520 Pattern = new CodeCompletionString;
Douglas Gregorf1934162010-01-13 21:24:21 +00002521 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,encode));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002522 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
2523 Pattern->AddPlaceholderChunk("type-name");
2524 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00002525 Results.AddResult(Result(Pattern));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002526
2527 // @protocol ( protocol-name )
2528 Pattern = new CodeCompletionString;
Douglas Gregorf1934162010-01-13 21:24:21 +00002529 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002530 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
2531 Pattern->AddPlaceholderChunk("protocol-name");
2532 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00002533 Results.AddResult(Result(Pattern));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002534
2535 // @selector ( selector )
2536 Pattern = new CodeCompletionString;
Douglas Gregorf1934162010-01-13 21:24:21 +00002537 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,selector));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002538 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
2539 Pattern->AddPlaceholderChunk("selector");
2540 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor78a21012010-01-14 16:01:26 +00002541 Results.AddResult(Result(Pattern));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002542}
2543
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002544static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002545 typedef CodeCompleteConsumer::Result Result;
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002546 CodeCompletionString *Pattern = 0;
Douglas Gregorf1934162010-01-13 21:24:21 +00002547
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002548 // @try { statements } @catch ( declaration ) { statements } @finally
2549 // { statements }
2550 Pattern = new CodeCompletionString;
Douglas Gregorf1934162010-01-13 21:24:21 +00002551 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,try));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002552 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
2553 Pattern->AddPlaceholderChunk("statements");
2554 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
2555 Pattern->AddTextChunk("@catch");
2556 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
2557 Pattern->AddPlaceholderChunk("parameter");
2558 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
2559 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
2560 Pattern->AddPlaceholderChunk("statements");
2561 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
2562 Pattern->AddTextChunk("@finally");
2563 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
2564 Pattern->AddPlaceholderChunk("statements");
2565 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregor78a21012010-01-14 16:01:26 +00002566 Results.AddResult(Result(Pattern));
Douglas Gregorf1934162010-01-13 21:24:21 +00002567
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002568 // @throw
2569 Pattern = new CodeCompletionString;
Douglas Gregorf1934162010-01-13 21:24:21 +00002570 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,throw));
Douglas Gregor6a803932010-01-12 06:38:28 +00002571 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002572 Pattern->AddPlaceholderChunk("expression");
Douglas Gregor6a803932010-01-12 06:38:28 +00002573 Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor78a21012010-01-14 16:01:26 +00002574 Results.AddResult(Result(Pattern));
Douglas Gregorf1934162010-01-13 21:24:21 +00002575
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002576 // @synchronized ( expression ) { statements }
2577 Pattern = new CodeCompletionString;
Douglas Gregorf1934162010-01-13 21:24:21 +00002578 Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synchronized));
Douglas Gregor6a803932010-01-12 06:38:28 +00002579 Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002580 Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
2581 Pattern->AddPlaceholderChunk("expression");
2582 Pattern->AddChunk(CodeCompletionString::CK_RightParen);
2583 Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
2584 Pattern->AddPlaceholderChunk("statements");
2585 Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregor78a21012010-01-14 16:01:26 +00002586 Results.AddResult(Result(Pattern));
Douglas Gregorf1934162010-01-13 21:24:21 +00002587}
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002588
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002589static void AddObjCVisibilityResults(const LangOptions &LangOpts,
Douglas Gregor48d46252010-01-13 21:54:15 +00002590 ResultBuilder &Results,
2591 bool NeedAt) {
Douglas Gregorf1934162010-01-13 21:24:21 +00002592 typedef CodeCompleteConsumer::Result Result;
Douglas Gregor78a21012010-01-14 16:01:26 +00002593 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,private)));
2594 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,protected)));
2595 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,public)));
Douglas Gregor48d46252010-01-13 21:54:15 +00002596 if (LangOpts.ObjC2)
Douglas Gregor78a21012010-01-14 16:01:26 +00002597 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,package)));
Douglas Gregor48d46252010-01-13 21:54:15 +00002598}
2599
2600void Sema::CodeCompleteObjCAtVisibility(Scope *S) {
2601 ResultBuilder Results(*this);
2602 Results.EnterNewScope();
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002603 AddObjCVisibilityResults(getLangOptions(), Results, false);
Douglas Gregor48d46252010-01-13 21:54:15 +00002604 Results.ExitScope();
2605 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2606}
2607
2608void Sema::CodeCompleteObjCAtStatement(Scope *S) {
Douglas Gregorf1934162010-01-13 21:24:21 +00002609 ResultBuilder Results(*this);
2610 Results.EnterNewScope();
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002611 AddObjCStatementResults(Results, false);
2612 AddObjCExpressionResults(Results, false);
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002613 Results.ExitScope();
2614 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2615}
2616
2617void Sema::CodeCompleteObjCAtExpression(Scope *S) {
2618 ResultBuilder Results(*this);
2619 Results.EnterNewScope();
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002620 AddObjCExpressionResults(Results, false);
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00002621 Results.ExitScope();
2622 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2623}
2624
Douglas Gregore6078da2009-11-19 00:14:45 +00002625/// \brief Determine whether the addition of the given flag to an Objective-C
2626/// property's attributes will cause a conflict.
2627static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
2628 // Check if we've already added this flag.
2629 if (Attributes & NewFlag)
2630 return true;
2631
2632 Attributes |= NewFlag;
2633
2634 // Check for collisions with "readonly".
2635 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2636 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
2637 ObjCDeclSpec::DQ_PR_assign |
2638 ObjCDeclSpec::DQ_PR_copy |
2639 ObjCDeclSpec::DQ_PR_retain)))
2640 return true;
2641
2642 // Check for more than one of { assign, copy, retain }.
2643 unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign |
2644 ObjCDeclSpec::DQ_PR_copy |
2645 ObjCDeclSpec::DQ_PR_retain);
2646 if (AssignCopyRetMask &&
2647 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign &&
2648 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy &&
2649 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain)
2650 return true;
2651
2652 return false;
2653}
2654
Douglas Gregor36029f42009-11-18 23:08:07 +00002655void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
Steve Naroff936354c2009-10-08 21:55:05 +00002656 if (!CodeCompleter)
2657 return;
Douglas Gregor1b605f72009-11-19 01:08:35 +00002658
Steve Naroff936354c2009-10-08 21:55:05 +00002659 unsigned Attributes = ODS.getPropertyAttributes();
2660
2661 typedef CodeCompleteConsumer::Result Result;
2662 ResultBuilder Results(*this);
2663 Results.EnterNewScope();
Douglas Gregore6078da2009-11-19 00:14:45 +00002664 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly))
Douglas Gregor78a21012010-01-14 16:01:26 +00002665 Results.AddResult(CodeCompleteConsumer::Result("readonly"));
Douglas Gregore6078da2009-11-19 00:14:45 +00002666 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign))
Douglas Gregor78a21012010-01-14 16:01:26 +00002667 Results.AddResult(CodeCompleteConsumer::Result("assign"));
Douglas Gregore6078da2009-11-19 00:14:45 +00002668 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite))
Douglas Gregor78a21012010-01-14 16:01:26 +00002669 Results.AddResult(CodeCompleteConsumer::Result("readwrite"));
Douglas Gregore6078da2009-11-19 00:14:45 +00002670 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain))
Douglas Gregor78a21012010-01-14 16:01:26 +00002671 Results.AddResult(CodeCompleteConsumer::Result("retain"));
Douglas Gregore6078da2009-11-19 00:14:45 +00002672 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy))
Douglas Gregor78a21012010-01-14 16:01:26 +00002673 Results.AddResult(CodeCompleteConsumer::Result("copy"));
Douglas Gregore6078da2009-11-19 00:14:45 +00002674 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic))
Douglas Gregor78a21012010-01-14 16:01:26 +00002675 Results.AddResult(CodeCompleteConsumer::Result("nonatomic"));
Douglas Gregore6078da2009-11-19 00:14:45 +00002676 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) {
Douglas Gregor45f83ee2009-11-19 00:01:57 +00002677 CodeCompletionString *Setter = new CodeCompletionString;
2678 Setter->AddTypedTextChunk("setter");
2679 Setter->AddTextChunk(" = ");
2680 Setter->AddPlaceholderChunk("method");
Douglas Gregor78a21012010-01-14 16:01:26 +00002681 Results.AddResult(CodeCompleteConsumer::Result(Setter));
Douglas Gregor45f83ee2009-11-19 00:01:57 +00002682 }
Douglas Gregore6078da2009-11-19 00:14:45 +00002683 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) {
Douglas Gregor45f83ee2009-11-19 00:01:57 +00002684 CodeCompletionString *Getter = new CodeCompletionString;
2685 Getter->AddTypedTextChunk("getter");
2686 Getter->AddTextChunk(" = ");
2687 Getter->AddPlaceholderChunk("method");
Douglas Gregor78a21012010-01-14 16:01:26 +00002688 Results.AddResult(CodeCompleteConsumer::Result(Getter));
Douglas Gregor45f83ee2009-11-19 00:01:57 +00002689 }
Steve Naroff936354c2009-10-08 21:55:05 +00002690 Results.ExitScope();
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002691 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Steve Naroff936354c2009-10-08 21:55:05 +00002692}
Steve Naroffeae65032009-11-07 02:08:14 +00002693
Douglas Gregorc8537c52009-11-19 07:41:15 +00002694/// \brief Descripts the kind of Objective-C method that we want to find
2695/// via code completion.
2696enum ObjCMethodKind {
2697 MK_Any, //< Any kind of method, provided it means other specified criteria.
2698 MK_ZeroArgSelector, //< Zero-argument (unary) selector.
2699 MK_OneArgSelector //< One-argument selector.
2700};
2701
2702static bool isAcceptableObjCMethod(ObjCMethodDecl *Method,
2703 ObjCMethodKind WantKind,
2704 IdentifierInfo **SelIdents,
2705 unsigned NumSelIdents) {
2706 Selector Sel = Method->getSelector();
2707 if (NumSelIdents > Sel.getNumArgs())
2708 return false;
2709
2710 switch (WantKind) {
2711 case MK_Any: break;
2712 case MK_ZeroArgSelector: return Sel.isUnarySelector();
2713 case MK_OneArgSelector: return Sel.getNumArgs() == 1;
2714 }
2715
2716 for (unsigned I = 0; I != NumSelIdents; ++I)
2717 if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
2718 return false;
2719
2720 return true;
2721}
2722
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002723/// \brief Add all of the Objective-C methods in the given Objective-C
2724/// container to the set of results.
2725///
2726/// The container will be a class, protocol, category, or implementation of
2727/// any of the above. This mether will recurse to include methods from
2728/// the superclasses of classes along with their categories, protocols, and
2729/// implementations.
2730///
2731/// \param Container the container in which we'll look to find methods.
2732///
2733/// \param WantInstance whether to add instance methods (only); if false, this
2734/// routine will add factory methods (only).
2735///
2736/// \param CurContext the context in which we're performing the lookup that
2737/// finds methods.
2738///
2739/// \param Results the structure into which we'll add results.
2740static void AddObjCMethods(ObjCContainerDecl *Container,
2741 bool WantInstanceMethods,
Douglas Gregorc8537c52009-11-19 07:41:15 +00002742 ObjCMethodKind WantKind,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002743 IdentifierInfo **SelIdents,
2744 unsigned NumSelIdents,
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002745 DeclContext *CurContext,
2746 ResultBuilder &Results) {
2747 typedef CodeCompleteConsumer::Result Result;
2748 for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
2749 MEnd = Container->meth_end();
2750 M != MEnd; ++M) {
Douglas Gregor1b605f72009-11-19 01:08:35 +00002751 if ((*M)->isInstanceMethod() == WantInstanceMethods) {
2752 // Check whether the selector identifiers we've been given are a
2753 // subset of the identifiers for this particular method.
Douglas Gregorc8537c52009-11-19 07:41:15 +00002754 if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents))
Douglas Gregor1b605f72009-11-19 01:08:35 +00002755 continue;
Douglas Gregorc8537c52009-11-19 07:41:15 +00002756
Douglas Gregor1b605f72009-11-19 01:08:35 +00002757 Result R = Result(*M, 0);
2758 R.StartParameter = NumSelIdents;
Douglas Gregorc8537c52009-11-19 07:41:15 +00002759 R.AllParametersAreInformative = (WantKind != MK_Any);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002760 Results.MaybeAddResult(R, CurContext);
2761 }
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002762 }
2763
2764 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
2765 if (!IFace)
2766 return;
2767
2768 // Add methods in protocols.
2769 const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols();
2770 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
2771 E = Protocols.end();
2772 I != E; ++I)
Douglas Gregorc8537c52009-11-19 07:41:15 +00002773 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002774 CurContext, Results);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002775
2776 // Add methods in categories.
2777 for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl;
2778 CatDecl = CatDecl->getNextClassCategory()) {
Douglas Gregorc8537c52009-11-19 07:41:15 +00002779 AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents,
2780 NumSelIdents, CurContext, Results);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002781
2782 // Add a categories protocol methods.
2783 const ObjCList<ObjCProtocolDecl> &Protocols
2784 = CatDecl->getReferencedProtocols();
2785 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
2786 E = Protocols.end();
2787 I != E; ++I)
Douglas Gregorc8537c52009-11-19 07:41:15 +00002788 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
2789 NumSelIdents, CurContext, Results);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002790
2791 // Add methods in category implementations.
2792 if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
Douglas Gregorc8537c52009-11-19 07:41:15 +00002793 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
2794 NumSelIdents, CurContext, Results);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002795 }
2796
2797 // Add methods in superclass.
2798 if (IFace->getSuperClass())
Douglas Gregorc8537c52009-11-19 07:41:15 +00002799 AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind,
2800 SelIdents, NumSelIdents, CurContext, Results);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002801
2802 // Add methods in our implementation, if any.
2803 if (ObjCImplementationDecl *Impl = IFace->getImplementation())
Douglas Gregorc8537c52009-11-19 07:41:15 +00002804 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
2805 NumSelIdents, CurContext, Results);
2806}
2807
2808
2809void Sema::CodeCompleteObjCPropertyGetter(Scope *S, DeclPtrTy ClassDecl,
2810 DeclPtrTy *Methods,
2811 unsigned NumMethods) {
2812 typedef CodeCompleteConsumer::Result Result;
2813
2814 // Try to find the interface where getters might live.
2815 ObjCInterfaceDecl *Class
2816 = dyn_cast_or_null<ObjCInterfaceDecl>(ClassDecl.getAs<Decl>());
2817 if (!Class) {
2818 if (ObjCCategoryDecl *Category
2819 = dyn_cast_or_null<ObjCCategoryDecl>(ClassDecl.getAs<Decl>()))
2820 Class = Category->getClassInterface();
2821
2822 if (!Class)
2823 return;
2824 }
2825
2826 // Find all of the potential getters.
2827 ResultBuilder Results(*this);
2828 Results.EnterNewScope();
2829
2830 // FIXME: We need to do this because Objective-C methods don't get
2831 // pushed into DeclContexts early enough. Argh!
2832 for (unsigned I = 0; I != NumMethods; ++I) {
2833 if (ObjCMethodDecl *Method
2834 = dyn_cast_or_null<ObjCMethodDecl>(Methods[I].getAs<Decl>()))
2835 if (Method->isInstanceMethod() &&
2836 isAcceptableObjCMethod(Method, MK_ZeroArgSelector, 0, 0)) {
2837 Result R = Result(Method, 0);
2838 R.AllParametersAreInformative = true;
2839 Results.MaybeAddResult(R, CurContext);
2840 }
2841 }
2842
2843 AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Results);
2844 Results.ExitScope();
2845 HandleCodeCompleteResults(this, CodeCompleter,Results.data(),Results.size());
2846}
2847
2848void Sema::CodeCompleteObjCPropertySetter(Scope *S, DeclPtrTy ObjCImplDecl,
2849 DeclPtrTy *Methods,
2850 unsigned NumMethods) {
2851 typedef CodeCompleteConsumer::Result Result;
2852
2853 // Try to find the interface where setters might live.
2854 ObjCInterfaceDecl *Class
2855 = dyn_cast_or_null<ObjCInterfaceDecl>(ObjCImplDecl.getAs<Decl>());
2856 if (!Class) {
2857 if (ObjCCategoryDecl *Category
2858 = dyn_cast_or_null<ObjCCategoryDecl>(ObjCImplDecl.getAs<Decl>()))
2859 Class = Category->getClassInterface();
2860
2861 if (!Class)
2862 return;
2863 }
2864
2865 // Find all of the potential getters.
2866 ResultBuilder Results(*this);
2867 Results.EnterNewScope();
2868
2869 // FIXME: We need to do this because Objective-C methods don't get
2870 // pushed into DeclContexts early enough. Argh!
2871 for (unsigned I = 0; I != NumMethods; ++I) {
2872 if (ObjCMethodDecl *Method
2873 = dyn_cast_or_null<ObjCMethodDecl>(Methods[I].getAs<Decl>()))
2874 if (Method->isInstanceMethod() &&
2875 isAcceptableObjCMethod(Method, MK_OneArgSelector, 0, 0)) {
2876 Result R = Result(Method, 0);
2877 R.AllParametersAreInformative = true;
2878 Results.MaybeAddResult(R, CurContext);
2879 }
2880 }
2881
2882 AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, Results);
2883
2884 Results.ExitScope();
2885 HandleCodeCompleteResults(this, CodeCompleter,Results.data(),Results.size());
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002886}
2887
Douglas Gregor090dd182009-11-17 23:31:36 +00002888void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002889 SourceLocation FNameLoc,
2890 IdentifierInfo **SelIdents,
2891 unsigned NumSelIdents) {
Steve Naroffeae65032009-11-07 02:08:14 +00002892 typedef CodeCompleteConsumer::Result Result;
Douglas Gregor8ce33212009-11-17 17:59:40 +00002893 ObjCInterfaceDecl *CDecl = 0;
2894
Douglas Gregor8ce33212009-11-17 17:59:40 +00002895 if (FName->isStr("super")) {
2896 // We're sending a message to "super".
2897 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
2898 // Figure out which interface we're in.
2899 CDecl = CurMethod->getClassInterface();
2900 if (!CDecl)
2901 return;
2902
2903 // Find the superclass of this class.
2904 CDecl = CDecl->getSuperClass();
2905 if (!CDecl)
2906 return;
2907
2908 if (CurMethod->isInstanceMethod()) {
2909 // We are inside an instance method, which means that the message
2910 // send [super ...] is actually calling an instance method on the
2911 // current object. Build the super expression and handle this like
2912 // an instance method.
2913 QualType SuperTy = Context.getObjCInterfaceType(CDecl);
2914 SuperTy = Context.getObjCObjectPointerType(SuperTy);
2915 OwningExprResult Super
Douglas Gregor090dd182009-11-17 23:31:36 +00002916 = Owned(new (Context) ObjCSuperExpr(FNameLoc, SuperTy));
Douglas Gregor1b605f72009-11-19 01:08:35 +00002917 return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(),
2918 SelIdents, NumSelIdents);
Douglas Gregor8ce33212009-11-17 17:59:40 +00002919 }
2920
2921 // Okay, we're calling a factory method in our superclass.
2922 }
2923 }
2924
2925 // If the given name refers to an interface type, retrieve the
2926 // corresponding declaration.
2927 if (!CDecl)
Douglas Gregor090dd182009-11-17 23:31:36 +00002928 if (TypeTy *Ty = getTypeName(*FName, FNameLoc, S, 0, false)) {
Douglas Gregor8ce33212009-11-17 17:59:40 +00002929 QualType T = GetTypeFromParser(Ty, 0);
2930 if (!T.isNull())
2931 if (const ObjCInterfaceType *Interface = T->getAs<ObjCInterfaceType>())
2932 CDecl = Interface->getDecl();
2933 }
2934
2935 if (!CDecl && FName->isStr("super")) {
2936 // "super" may be the name of a variable, in which case we are
2937 // probably calling an instance method.
John McCalle66edc12009-11-24 19:00:30 +00002938 CXXScopeSpec SS;
2939 UnqualifiedId id;
2940 id.setIdentifier(FName, FNameLoc);
2941 OwningExprResult Super = ActOnIdExpression(S, SS, id, false, false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002942 return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(),
2943 SelIdents, NumSelIdents);
Douglas Gregor8ce33212009-11-17 17:59:40 +00002944 }
2945
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002946 // Add all of the factory methods in this Objective-C class, its protocols,
2947 // superclasses, categories, implementation, etc.
Steve Naroffeae65032009-11-07 02:08:14 +00002948 ResultBuilder Results(*this);
2949 Results.EnterNewScope();
Douglas Gregorc8537c52009-11-19 07:41:15 +00002950 AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, CurContext,
2951 Results);
Steve Naroffeae65032009-11-07 02:08:14 +00002952 Results.ExitScope();
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002953
Steve Naroffeae65032009-11-07 02:08:14 +00002954 // This also suppresses remaining diagnostics.
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00002955 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Steve Naroffeae65032009-11-07 02:08:14 +00002956}
2957
Douglas Gregor1b605f72009-11-19 01:08:35 +00002958void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver,
2959 IdentifierInfo **SelIdents,
2960 unsigned NumSelIdents) {
Steve Naroffeae65032009-11-07 02:08:14 +00002961 typedef CodeCompleteConsumer::Result Result;
Steve Naroffeae65032009-11-07 02:08:14 +00002962
2963 Expr *RecExpr = static_cast<Expr *>(Receiver);
Steve Naroffeae65032009-11-07 02:08:14 +00002964
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002965 // If necessary, apply function/array conversion to the receiver.
2966 // C99 6.7.5.3p[7,8].
2967 DefaultFunctionArrayConversion(RecExpr);
2968 QualType ReceiverType = RecExpr->getType();
Steve Naroffeae65032009-11-07 02:08:14 +00002969
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002970 if (ReceiverType->isObjCIdType() || ReceiverType->isBlockPointerType()) {
2971 // FIXME: We're messaging 'id'. Do we actually want to look up every method
2972 // in the universe?
2973 return;
2974 }
2975
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002976 // Build the set of methods we can see.
2977 ResultBuilder Results(*this);
2978 Results.EnterNewScope();
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00002979
Douglas Gregora3329fa2009-11-18 00:06:18 +00002980 // Handle messages to Class. This really isn't a message to an instance
2981 // method, so we treat it the same way we would treat a message send to a
2982 // class method.
2983 if (ReceiverType->isObjCClassType() ||
2984 ReceiverType->isObjCQualifiedClassType()) {
2985 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
2986 if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
Douglas Gregorc8537c52009-11-19 07:41:15 +00002987 AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents,
2988 CurContext, Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00002989 }
2990 }
2991 // Handle messages to a qualified ID ("id<foo>").
2992 else if (const ObjCObjectPointerType *QualID
2993 = ReceiverType->getAsObjCQualifiedIdType()) {
2994 // Search protocols for instance methods.
2995 for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(),
2996 E = QualID->qual_end();
2997 I != E; ++I)
Douglas Gregorc8537c52009-11-19 07:41:15 +00002998 AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
2999 Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00003000 }
3001 // Handle messages to a pointer to interface type.
3002 else if (const ObjCObjectPointerType *IFacePtr
3003 = ReceiverType->getAsObjCInterfacePointerType()) {
3004 // Search the class, its superclasses, etc., for instance methods.
Douglas Gregorc8537c52009-11-19 07:41:15 +00003005 AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents,
3006 NumSelIdents, CurContext, Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00003007
3008 // Search protocols for instance methods.
3009 for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(),
3010 E = IFacePtr->qual_end();
3011 I != E; ++I)
Douglas Gregorc8537c52009-11-19 07:41:15 +00003012 AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
3013 Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00003014 }
3015
Steve Naroffeae65032009-11-07 02:08:14 +00003016 Results.ExitScope();
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003017 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Steve Naroffeae65032009-11-07 02:08:14 +00003018}
Douglas Gregorbaf69612009-11-18 04:19:12 +00003019
3020/// \brief Add all of the protocol declarations that we find in the given
3021/// (translation unit) context.
3022static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext,
Douglas Gregor5b4671c2009-11-18 04:49:41 +00003023 bool OnlyForwardDeclarations,
Douglas Gregorbaf69612009-11-18 04:19:12 +00003024 ResultBuilder &Results) {
3025 typedef CodeCompleteConsumer::Result Result;
3026
3027 for (DeclContext::decl_iterator D = Ctx->decls_begin(),
3028 DEnd = Ctx->decls_end();
3029 D != DEnd; ++D) {
3030 // Record any protocols we find.
3031 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D))
Douglas Gregor5b4671c2009-11-18 04:49:41 +00003032 if (!OnlyForwardDeclarations || Proto->isForwardDecl())
3033 Results.MaybeAddResult(Result(Proto, 0), CurContext);
Douglas Gregorbaf69612009-11-18 04:19:12 +00003034
3035 // Record any forward-declared protocols we find.
3036 if (ObjCForwardProtocolDecl *Forward
3037 = dyn_cast<ObjCForwardProtocolDecl>(*D)) {
3038 for (ObjCForwardProtocolDecl::protocol_iterator
3039 P = Forward->protocol_begin(),
3040 PEnd = Forward->protocol_end();
3041 P != PEnd; ++P)
Douglas Gregor5b4671c2009-11-18 04:49:41 +00003042 if (!OnlyForwardDeclarations || (*P)->isForwardDecl())
3043 Results.MaybeAddResult(Result(*P, 0), CurContext);
Douglas Gregorbaf69612009-11-18 04:19:12 +00003044 }
3045 }
3046}
3047
3048void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
3049 unsigned NumProtocols) {
3050 ResultBuilder Results(*this);
3051 Results.EnterNewScope();
3052
3053 // Tell the result set to ignore all of the protocols we have
3054 // already seen.
3055 for (unsigned I = 0; I != NumProtocols; ++I)
3056 if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first))
3057 Results.Ignore(Protocol);
3058
3059 // Add all protocols.
Douglas Gregor5b4671c2009-11-18 04:49:41 +00003060 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false,
3061 Results);
3062
3063 Results.ExitScope();
3064 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3065}
3066
3067void Sema::CodeCompleteObjCProtocolDecl(Scope *) {
3068 ResultBuilder Results(*this);
3069 Results.EnterNewScope();
3070
3071 // Add all protocols.
3072 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true,
3073 Results);
Douglas Gregorbaf69612009-11-18 04:19:12 +00003074
3075 Results.ExitScope();
3076 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3077}
Douglas Gregor49c22a72009-11-18 16:26:39 +00003078
3079/// \brief Add all of the Objective-C interface declarations that we find in
3080/// the given (translation unit) context.
3081static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext,
3082 bool OnlyForwardDeclarations,
3083 bool OnlyUnimplemented,
3084 ResultBuilder &Results) {
3085 typedef CodeCompleteConsumer::Result Result;
3086
3087 for (DeclContext::decl_iterator D = Ctx->decls_begin(),
3088 DEnd = Ctx->decls_end();
3089 D != DEnd; ++D) {
3090 // Record any interfaces we find.
3091 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D))
3092 if ((!OnlyForwardDeclarations || Class->isForwardDecl()) &&
3093 (!OnlyUnimplemented || !Class->getImplementation()))
3094 Results.MaybeAddResult(Result(Class, 0), CurContext);
3095
3096 // Record any forward-declared interfaces we find.
3097 if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) {
3098 for (ObjCClassDecl::iterator C = Forward->begin(), CEnd = Forward->end();
3099 C != CEnd; ++C)
3100 if ((!OnlyForwardDeclarations || C->getInterface()->isForwardDecl()) &&
3101 (!OnlyUnimplemented || !C->getInterface()->getImplementation()))
3102 Results.MaybeAddResult(Result(C->getInterface(), 0), CurContext);
3103 }
3104 }
3105}
3106
3107void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) {
3108 ResultBuilder Results(*this);
3109 Results.EnterNewScope();
3110
3111 // Add all classes.
3112 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, true,
3113 false, Results);
3114
3115 Results.ExitScope();
3116 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3117}
3118
3119void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName) {
3120 ResultBuilder Results(*this);
3121 Results.EnterNewScope();
3122
3123 // Make sure that we ignore the class we're currently defining.
3124 NamedDecl *CurClass
3125 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00003126 if (CurClass && isa<ObjCInterfaceDecl>(CurClass))
Douglas Gregor49c22a72009-11-18 16:26:39 +00003127 Results.Ignore(CurClass);
3128
3129 // Add all classes.
3130 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
3131 false, Results);
3132
3133 Results.ExitScope();
3134 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3135}
3136
3137void Sema::CodeCompleteObjCImplementationDecl(Scope *S) {
3138 ResultBuilder Results(*this);
3139 Results.EnterNewScope();
3140
3141 // Add all unimplemented classes.
3142 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
3143 true, Results);
3144
3145 Results.ExitScope();
3146 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3147}
Douglas Gregor5d34fd32009-11-18 19:08:43 +00003148
3149void Sema::CodeCompleteObjCInterfaceCategory(Scope *S,
3150 IdentifierInfo *ClassName) {
3151 typedef CodeCompleteConsumer::Result Result;
3152
3153 ResultBuilder Results(*this);
3154
3155 // Ignore any categories we find that have already been implemented by this
3156 // interface.
3157 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
3158 NamedDecl *CurClass
3159 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
3160 if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass))
3161 for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
3162 Category = Category->getNextClassCategory())
3163 CategoryNames.insert(Category->getIdentifier());
3164
3165 // Add all of the categories we know about.
3166 Results.EnterNewScope();
3167 TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3168 for (DeclContext::decl_iterator D = TU->decls_begin(),
3169 DEnd = TU->decls_end();
3170 D != DEnd; ++D)
3171 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D))
3172 if (CategoryNames.insert(Category->getIdentifier()))
3173 Results.MaybeAddResult(Result(Category, 0), CurContext);
3174 Results.ExitScope();
3175
3176 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3177}
3178
3179void Sema::CodeCompleteObjCImplementationCategory(Scope *S,
3180 IdentifierInfo *ClassName) {
3181 typedef CodeCompleteConsumer::Result Result;
3182
3183 // Find the corresponding interface. If we couldn't find the interface, the
3184 // program itself is ill-formed. However, we'll try to be helpful still by
3185 // providing the list of all of the categories we know about.
3186 NamedDecl *CurClass
3187 = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
3188 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass);
3189 if (!Class)
3190 return CodeCompleteObjCInterfaceCategory(S, ClassName);
3191
3192 ResultBuilder Results(*this);
3193
3194 // Add all of the categories that have have corresponding interface
3195 // declarations in this class and any of its superclasses, except for
3196 // already-implemented categories in the class itself.
3197 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
3198 Results.EnterNewScope();
3199 bool IgnoreImplemented = true;
3200 while (Class) {
3201 for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
3202 Category = Category->getNextClassCategory())
3203 if ((!IgnoreImplemented || !Category->getImplementation()) &&
3204 CategoryNames.insert(Category->getIdentifier()))
3205 Results.MaybeAddResult(Result(Category, 0), CurContext);
3206
3207 Class = Class->getSuperClass();
3208 IgnoreImplemented = false;
3209 }
3210 Results.ExitScope();
3211
3212 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3213}
Douglas Gregor5d649882009-11-18 22:32:06 +00003214
Douglas Gregor52e78bd2009-11-18 22:56:13 +00003215void Sema::CodeCompleteObjCPropertyDefinition(Scope *S, DeclPtrTy ObjCImpDecl) {
Douglas Gregor5d649882009-11-18 22:32:06 +00003216 typedef CodeCompleteConsumer::Result Result;
3217 ResultBuilder Results(*this);
3218
3219 // Figure out where this @synthesize lives.
3220 ObjCContainerDecl *Container
3221 = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl.getAs<Decl>());
3222 if (!Container ||
3223 (!isa<ObjCImplementationDecl>(Container) &&
3224 !isa<ObjCCategoryImplDecl>(Container)))
3225 return;
3226
3227 // Ignore any properties that have already been implemented.
3228 for (DeclContext::decl_iterator D = Container->decls_begin(),
3229 DEnd = Container->decls_end();
3230 D != DEnd; ++D)
3231 if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D))
3232 Results.Ignore(PropertyImpl->getPropertyDecl());
3233
3234 // Add any properties that we find.
3235 Results.EnterNewScope();
3236 if (ObjCImplementationDecl *ClassImpl
3237 = dyn_cast<ObjCImplementationDecl>(Container))
3238 AddObjCProperties(ClassImpl->getClassInterface(), false, CurContext,
3239 Results);
3240 else
3241 AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(),
3242 false, CurContext, Results);
3243 Results.ExitScope();
3244
3245 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3246}
3247
3248void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
3249 IdentifierInfo *PropertyName,
3250 DeclPtrTy ObjCImpDecl) {
3251 typedef CodeCompleteConsumer::Result Result;
3252 ResultBuilder Results(*this);
3253
3254 // Figure out where this @synthesize lives.
3255 ObjCContainerDecl *Container
3256 = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl.getAs<Decl>());
3257 if (!Container ||
3258 (!isa<ObjCImplementationDecl>(Container) &&
3259 !isa<ObjCCategoryImplDecl>(Container)))
3260 return;
3261
3262 // Figure out which interface we're looking into.
3263 ObjCInterfaceDecl *Class = 0;
3264 if (ObjCImplementationDecl *ClassImpl
3265 = dyn_cast<ObjCImplementationDecl>(Container))
3266 Class = ClassImpl->getClassInterface();
3267 else
3268 Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
3269 ->getClassInterface();
3270
3271 // Add all of the instance variables in this class and its superclasses.
3272 Results.EnterNewScope();
3273 for(; Class; Class = Class->getSuperClass()) {
3274 // FIXME: We could screen the type of each ivar for compatibility with
3275 // the property, but is that being too paternal?
3276 for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(),
3277 IVarEnd = Class->ivar_end();
3278 IVar != IVarEnd; ++IVar)
3279 Results.MaybeAddResult(Result(*IVar, 0), CurContext);
3280 }
3281 Results.ExitScope();
3282
3283 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3284}