blob: ebd860b6926f61e0dceb717f6896c66fc9c01029 [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"
14#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregorf2510672009-09-21 19:57:38 +000015#include "clang/AST/ExprCXX.h"
Douglas Gregor8ce33212009-11-17 17:59:40 +000016#include "clang/AST/ExprObjC.h"
Douglas Gregorf329c7c2009-10-30 16:50:04 +000017#include "clang/Lex/MacroInfo.h"
18#include "clang/Lex/Preprocessor.h"
Douglas Gregor3545ff42009-09-21 16:56:56 +000019#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregore6688e62009-09-28 03:51:44 +000020#include "llvm/ADT/StringExtras.h"
Douglas Gregor3545ff42009-09-21 16:56:56 +000021#include <list>
22#include <map>
23#include <vector>
Douglas Gregor2436e712009-09-17 21:32:03 +000024
25using namespace clang;
26
Douglas Gregor3545ff42009-09-21 16:56:56 +000027namespace {
28 /// \brief A container of code-completion results.
29 class ResultBuilder {
30 public:
31 /// \brief The type of a name-lookup filter, which can be provided to the
32 /// name-lookup routines to specify which declarations should be included in
33 /// the result set (when it returns true) and which declarations should be
34 /// filtered out (returns false).
35 typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const;
36
37 typedef CodeCompleteConsumer::Result Result;
38
39 private:
40 /// \brief The actual results we have found.
41 std::vector<Result> Results;
42
43 /// \brief A record of all of the declarations we have found and placed
44 /// into the result set, used to ensure that no declaration ever gets into
45 /// the result set twice.
46 llvm::SmallPtrSet<Decl*, 16> AllDeclsFound;
47
48 /// \brief A mapping from declaration names to the declarations that have
49 /// this name within a particular scope and their index within the list of
50 /// results.
51 typedef std::multimap<DeclarationName,
52 std::pair<NamedDecl *, unsigned> > ShadowMap;
53
54 /// \brief The semantic analysis object for which results are being
55 /// produced.
56 Sema &SemaRef;
57
58 /// \brief If non-NULL, a filter function used to remove any code-completion
59 /// results that are not desirable.
60 LookupFilter Filter;
61
62 /// \brief A list of shadow maps, which is used to model name hiding at
63 /// different levels of, e.g., the inheritance hierarchy.
64 std::list<ShadowMap> ShadowMaps;
65
66 public:
67 explicit ResultBuilder(Sema &SemaRef, LookupFilter Filter = 0)
68 : SemaRef(SemaRef), Filter(Filter) { }
69
70 /// \brief Set the filter used for code-completion results.
71 void setFilter(LookupFilter Filter) {
72 this->Filter = Filter;
73 }
74
75 typedef std::vector<Result>::iterator iterator;
76 iterator begin() { return Results.begin(); }
77 iterator end() { return Results.end(); }
78
79 Result *data() { return Results.empty()? 0 : &Results.front(); }
80 unsigned size() const { return Results.size(); }
81 bool empty() const { return Results.empty(); }
82
83 /// \brief Add a new result to this result set (if it isn't already in one
84 /// of the shadow maps), or replace an existing result (for, e.g., a
85 /// redeclaration).
Douglas Gregor2af2f672009-09-21 20:12:40 +000086 ///
87 /// \param R the result to add (if it is unique).
88 ///
89 /// \param R the context in which this result will be named.
90 void MaybeAddResult(Result R, DeclContext *CurContext = 0);
Douglas Gregor3545ff42009-09-21 16:56:56 +000091
92 /// \brief Enter into a new scope.
93 void EnterNewScope();
94
95 /// \brief Exit from the current scope.
96 void ExitScope();
97
98 /// \name Name lookup predicates
99 ///
100 /// These predicates can be passed to the name lookup functions to filter the
101 /// results of name lookup. All of the predicates have the same type, so that
102 ///
103 //@{
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000104 bool IsOrdinaryName(NamedDecl *ND) const;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000105 bool IsNestedNameSpecifier(NamedDecl *ND) const;
106 bool IsEnum(NamedDecl *ND) const;
107 bool IsClassOrStruct(NamedDecl *ND) const;
108 bool IsUnion(NamedDecl *ND) const;
109 bool IsNamespace(NamedDecl *ND) const;
110 bool IsNamespaceOrAlias(NamedDecl *ND) const;
111 bool IsType(NamedDecl *ND) const;
Douglas Gregore412a5a2009-09-23 22:26:46 +0000112 bool IsMember(NamedDecl *ND) const;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000113 //@}
114 };
115}
116
117/// \brief Determines whether the given hidden result could be found with
118/// some extra work, e.g., by qualifying the name.
119///
120/// \param Hidden the declaration that is hidden by the currenly \p Visible
121/// declaration.
122///
123/// \param Visible the declaration with the same name that is already visible.
124///
125/// \returns true if the hidden result can be found by some mechanism,
126/// false otherwise.
127static bool canHiddenResultBeFound(const LangOptions &LangOpts,
128 NamedDecl *Hidden, NamedDecl *Visible) {
129 // In C, there is no way to refer to a hidden name.
130 if (!LangOpts.CPlusPlus)
131 return false;
132
133 DeclContext *HiddenCtx = Hidden->getDeclContext()->getLookupContext();
134
135 // There is no way to qualify a name declared in a function or method.
136 if (HiddenCtx->isFunctionOrMethod())
137 return false;
138
Douglas Gregor3545ff42009-09-21 16:56:56 +0000139 return HiddenCtx != Visible->getDeclContext()->getLookupContext();
140}
141
Douglas Gregor2af2f672009-09-21 20:12:40 +0000142/// \brief Compute the qualification required to get from the current context
143/// (\p CurContext) to the target context (\p TargetContext).
144///
145/// \param Context the AST context in which the qualification will be used.
146///
147/// \param CurContext the context where an entity is being named, which is
148/// typically based on the current scope.
149///
150/// \param TargetContext the context in which the named entity actually
151/// resides.
152///
153/// \returns a nested name specifier that refers into the target context, or
154/// NULL if no qualification is needed.
155static NestedNameSpecifier *
156getRequiredQualification(ASTContext &Context,
157 DeclContext *CurContext,
158 DeclContext *TargetContext) {
159 llvm::SmallVector<DeclContext *, 4> TargetParents;
160
161 for (DeclContext *CommonAncestor = TargetContext;
162 CommonAncestor && !CommonAncestor->Encloses(CurContext);
163 CommonAncestor = CommonAncestor->getLookupParent()) {
164 if (CommonAncestor->isTransparentContext() ||
165 CommonAncestor->isFunctionOrMethod())
166 continue;
167
168 TargetParents.push_back(CommonAncestor);
169 }
170
171 NestedNameSpecifier *Result = 0;
172 while (!TargetParents.empty()) {
173 DeclContext *Parent = TargetParents.back();
174 TargetParents.pop_back();
175
176 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent))
177 Result = NestedNameSpecifier::Create(Context, Result, Namespace);
178 else if (TagDecl *TD = dyn_cast<TagDecl>(Parent))
179 Result = NestedNameSpecifier::Create(Context, Result,
180 false,
181 Context.getTypeDeclType(TD).getTypePtr());
182 else
183 assert(Parent->isTranslationUnit());
Douglas Gregor9eb77012009-11-07 00:00:49 +0000184 }
Douglas Gregor2af2f672009-09-21 20:12:40 +0000185 return Result;
186}
187
188void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) {
Douglas Gregor64b12b52009-09-22 23:31:26 +0000189 assert(!ShadowMaps.empty() && "Must enter into a results scope");
190
Douglas Gregor3545ff42009-09-21 16:56:56 +0000191 if (R.Kind != Result::RK_Declaration) {
192 // For non-declaration results, just add the result.
193 Results.push_back(R);
194 return;
195 }
Douglas Gregor58acf322009-10-09 22:16:47 +0000196
197 // Skip unnamed entities.
198 if (!R.Declaration->getDeclName())
199 return;
200
Douglas Gregor3545ff42009-09-21 16:56:56 +0000201 // Look through using declarations.
John McCall3f746822009-11-17 05:59:44 +0000202 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration))
Douglas Gregor5bf52692009-09-22 23:15:58 +0000203 MaybeAddResult(Result(Using->getTargetDecl(), R.Rank, R.Qualifier),
204 CurContext);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000205
206 // Handle each declaration in an overload set separately.
207 if (OverloadedFunctionDecl *Ovl
208 = dyn_cast<OverloadedFunctionDecl>(R.Declaration)) {
209 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
210 FEnd = Ovl->function_end();
211 F != FEnd; ++F)
Douglas Gregor2af2f672009-09-21 20:12:40 +0000212 MaybeAddResult(Result(*F, R.Rank, R.Qualifier), CurContext);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000213
214 return;
215 }
216
217 Decl *CanonDecl = R.Declaration->getCanonicalDecl();
218 unsigned IDNS = CanonDecl->getIdentifierNamespace();
219
220 // Friend declarations and declarations introduced due to friends are never
221 // added as results.
222 if (isa<FriendDecl>(CanonDecl) ||
223 (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend)))
224 return;
225
226 if (const IdentifierInfo *Id = R.Declaration->getIdentifier()) {
227 // __va_list_tag is a freak of nature. Find it and skip it.
228 if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list"))
229 return;
230
Douglas Gregor58acf322009-10-09 22:16:47 +0000231 // Filter out names reserved for the implementation (C99 7.1.3,
232 // C++ [lib.global.names]). Users don't need to see those.
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000233 //
234 // FIXME: Add predicate for this.
Douglas Gregor58acf322009-10-09 22:16:47 +0000235 if (Id->getLength() >= 2) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000236 const char *Name = Id->getNameStart();
Douglas Gregor58acf322009-10-09 22:16:47 +0000237 if (Name[0] == '_' &&
238 (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')))
239 return;
240 }
Douglas Gregor3545ff42009-09-21 16:56:56 +0000241 }
242
243 // C++ constructors are never found by name lookup.
244 if (isa<CXXConstructorDecl>(CanonDecl))
245 return;
246
247 // Filter out any unwanted results.
248 if (Filter && !(this->*Filter)(R.Declaration))
249 return;
250
251 ShadowMap &SMap = ShadowMaps.back();
252 ShadowMap::iterator I, IEnd;
253 for (llvm::tie(I, IEnd) = SMap.equal_range(R.Declaration->getDeclName());
254 I != IEnd; ++I) {
255 NamedDecl *ND = I->second.first;
256 unsigned Index = I->second.second;
257 if (ND->getCanonicalDecl() == CanonDecl) {
258 // This is a redeclaration. Always pick the newer declaration.
259 I->second.first = R.Declaration;
260 Results[Index].Declaration = R.Declaration;
261
262 // Pick the best rank of the two.
263 Results[Index].Rank = std::min(Results[Index].Rank, R.Rank);
264
265 // We're done.
266 return;
267 }
268 }
269
270 // This is a new declaration in this scope. However, check whether this
271 // declaration name is hidden by a similarly-named declaration in an outer
272 // scope.
273 std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end();
274 --SMEnd;
275 for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) {
276 for (llvm::tie(I, IEnd) = SM->equal_range(R.Declaration->getDeclName());
277 I != IEnd; ++I) {
278 // A tag declaration does not hide a non-tag declaration.
279 if (I->second.first->getIdentifierNamespace() == Decl::IDNS_Tag &&
280 (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary |
281 Decl::IDNS_ObjCProtocol)))
282 continue;
283
284 // Protocols are in distinct namespaces from everything else.
285 if (((I->second.first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol)
286 || (IDNS & Decl::IDNS_ObjCProtocol)) &&
287 I->second.first->getIdentifierNamespace() != IDNS)
288 continue;
289
290 // The newly-added result is hidden by an entry in the shadow map.
291 if (canHiddenResultBeFound(SemaRef.getLangOptions(), R.Declaration,
292 I->second.first)) {
293 // Note that this result was hidden.
294 R.Hidden = true;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000295 R.QualifierIsInformative = false;
Douglas Gregor2af2f672009-09-21 20:12:40 +0000296
297 if (!R.Qualifier)
298 R.Qualifier = getRequiredQualification(SemaRef.Context,
299 CurContext,
300 R.Declaration->getDeclContext());
Douglas Gregor3545ff42009-09-21 16:56:56 +0000301 } else {
302 // This result was hidden and cannot be found; don't bother adding
303 // it.
304 return;
305 }
306
307 break;
308 }
309 }
310
311 // Make sure that any given declaration only shows up in the result set once.
312 if (!AllDeclsFound.insert(CanonDecl))
313 return;
314
Douglas Gregore412a5a2009-09-23 22:26:46 +0000315 // If the filter is for nested-name-specifiers, then this result starts a
316 // nested-name-specifier.
317 if ((Filter == &ResultBuilder::IsNestedNameSpecifier) ||
318 (Filter == &ResultBuilder::IsMember &&
319 isa<CXXRecordDecl>(R.Declaration) &&
320 cast<CXXRecordDecl>(R.Declaration)->isInjectedClassName()))
321 R.StartsNestedNameSpecifier = true;
322
Douglas Gregor5bf52692009-09-22 23:15:58 +0000323 // If this result is supposed to have an informative qualifier, add one.
Douglas Gregore412a5a2009-09-23 22:26:46 +0000324 if (R.QualifierIsInformative && !R.Qualifier &&
325 !R.StartsNestedNameSpecifier) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000326 DeclContext *Ctx = R.Declaration->getDeclContext();
327 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
328 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
329 else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
330 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
331 SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
332 else
333 R.QualifierIsInformative = false;
334 }
Douglas Gregore412a5a2009-09-23 22:26:46 +0000335
Douglas Gregor3545ff42009-09-21 16:56:56 +0000336 // Insert this result into the set of results and into the current shadow
337 // map.
338 SMap.insert(std::make_pair(R.Declaration->getDeclName(),
339 std::make_pair(R.Declaration, Results.size())));
340 Results.push_back(R);
341}
342
343/// \brief Enter into a new scope.
344void ResultBuilder::EnterNewScope() {
345 ShadowMaps.push_back(ShadowMap());
346}
347
348/// \brief Exit from the current scope.
349void ResultBuilder::ExitScope() {
350 ShadowMaps.pop_back();
351}
352
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000353/// \brief Determines whether this given declaration will be found by
354/// ordinary name lookup.
355bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const {
356 unsigned IDNS = Decl::IDNS_Ordinary;
357 if (SemaRef.getLangOptions().CPlusPlus)
358 IDNS |= Decl::IDNS_Tag;
359
360 return ND->getIdentifierNamespace() & IDNS;
361}
362
Douglas Gregor3545ff42009-09-21 16:56:56 +0000363/// \brief Determines whether the given declaration is suitable as the
364/// start of a C++ nested-name-specifier, e.g., a class or namespace.
365bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const {
366 // Allow us to find class templates, too.
367 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
368 ND = ClassTemplate->getTemplatedDecl();
369
370 return SemaRef.isAcceptableNestedNameSpecifier(ND);
371}
372
373/// \brief Determines whether the given declaration is an enumeration.
374bool ResultBuilder::IsEnum(NamedDecl *ND) const {
375 return isa<EnumDecl>(ND);
376}
377
378/// \brief Determines whether the given declaration is a class or struct.
379bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const {
380 // Allow us to find class templates, too.
381 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
382 ND = ClassTemplate->getTemplatedDecl();
383
384 if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
385 return RD->getTagKind() == TagDecl::TK_class ||
386 RD->getTagKind() == TagDecl::TK_struct;
387
388 return false;
389}
390
391/// \brief Determines whether the given declaration is a union.
392bool ResultBuilder::IsUnion(NamedDecl *ND) const {
393 // Allow us to find class templates, too.
394 if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
395 ND = ClassTemplate->getTemplatedDecl();
396
397 if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
398 return RD->getTagKind() == TagDecl::TK_union;
399
400 return false;
401}
402
403/// \brief Determines whether the given declaration is a namespace.
404bool ResultBuilder::IsNamespace(NamedDecl *ND) const {
405 return isa<NamespaceDecl>(ND);
406}
407
408/// \brief Determines whether the given declaration is a namespace or
409/// namespace alias.
410bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const {
411 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
412}
413
414/// \brief Brief determines whether the given declaration is a namespace or
415/// namespace alias.
416bool ResultBuilder::IsType(NamedDecl *ND) const {
417 return isa<TypeDecl>(ND);
418}
419
Douglas Gregore412a5a2009-09-23 22:26:46 +0000420/// \brief Since every declaration found within a class is a member that we
421/// care about, always returns true. This predicate exists mostly to
422/// communicate to the result builder that we are performing a lookup for
423/// member access.
424bool ResultBuilder::IsMember(NamedDecl *ND) const {
425 return true;
426}
427
Douglas Gregor3545ff42009-09-21 16:56:56 +0000428// Find the next outer declaration context corresponding to this scope.
429static DeclContext *findOuterContext(Scope *S) {
430 for (S = S->getParent(); S; S = S->getParent())
431 if (S->getEntity())
432 return static_cast<DeclContext *>(S->getEntity())->getPrimaryContext();
433
434 return 0;
435}
436
437/// \brief Collect the results of searching for members within the given
438/// declaration context.
439///
440/// \param Ctx the declaration context from which we will gather results.
441///
Douglas Gregor5bf52692009-09-22 23:15:58 +0000442/// \param Rank the rank given to results in this declaration context.
Douglas Gregor3545ff42009-09-21 16:56:56 +0000443///
444/// \param Visited the set of declaration contexts that have already been
445/// visited. Declaration contexts will only be visited once.
446///
447/// \param Results the result set that will be extended with any results
448/// found within this declaration context (and, for a C++ class, its bases).
449///
Douglas Gregor5bf52692009-09-22 23:15:58 +0000450/// \param InBaseClass whether we are in a base class.
451///
Douglas Gregor3545ff42009-09-21 16:56:56 +0000452/// \returns the next higher rank value, after considering all of the
453/// names within this declaration context.
454static unsigned CollectMemberLookupResults(DeclContext *Ctx,
Douglas Gregor5bf52692009-09-22 23:15:58 +0000455 unsigned Rank,
Douglas Gregor2af2f672009-09-21 20:12:40 +0000456 DeclContext *CurContext,
457 llvm::SmallPtrSet<DeclContext *, 16> &Visited,
Douglas Gregor5bf52692009-09-22 23:15:58 +0000458 ResultBuilder &Results,
459 bool InBaseClass = false) {
Douglas Gregor3545ff42009-09-21 16:56:56 +0000460 // Make sure we don't visit the same context twice.
461 if (!Visited.insert(Ctx->getPrimaryContext()))
Douglas Gregor5bf52692009-09-22 23:15:58 +0000462 return Rank;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000463
464 // Enumerate all of the results in this context.
Douglas Gregor5bf52692009-09-22 23:15:58 +0000465 typedef CodeCompleteConsumer::Result Result;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000466 Results.EnterNewScope();
467 for (DeclContext *CurCtx = Ctx->getPrimaryContext(); CurCtx;
468 CurCtx = CurCtx->getNextContext()) {
469 for (DeclContext::decl_iterator D = CurCtx->decls_begin(),
Douglas Gregor8caea942009-11-09 21:35:27 +0000470 DEnd = CurCtx->decls_end();
Douglas Gregor3545ff42009-09-21 16:56:56 +0000471 D != DEnd; ++D) {
472 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor5bf52692009-09-22 23:15:58 +0000473 Results.MaybeAddResult(Result(ND, Rank, 0, InBaseClass), CurContext);
Douglas Gregor8caea942009-11-09 21:35:27 +0000474
475 // Visit transparent contexts inside this context.
476 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) {
477 if (InnerCtx->isTransparentContext())
478 CollectMemberLookupResults(InnerCtx, Rank, CurContext, Visited,
479 Results, InBaseClass);
480 }
Douglas Gregor3545ff42009-09-21 16:56:56 +0000481 }
482 }
483
484 // Traverse the contexts of inherited classes.
Douglas Gregor3545ff42009-09-21 16:56:56 +0000485 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) {
486 for (CXXRecordDecl::base_class_iterator B = Record->bases_begin(),
Douglas Gregor8caea942009-11-09 21:35:27 +0000487 BEnd = Record->bases_end();
Douglas Gregor3545ff42009-09-21 16:56:56 +0000488 B != BEnd; ++B) {
489 QualType BaseType = B->getType();
490
491 // Don't look into dependent bases, because name lookup can't look
492 // there anyway.
493 if (BaseType->isDependentType())
494 continue;
495
496 const RecordType *Record = BaseType->getAs<RecordType>();
497 if (!Record)
498 continue;
499
500 // FIXME: It would be nice to be able to determine whether referencing
501 // a particular member would be ambiguous. For example, given
502 //
503 // struct A { int member; };
504 // struct B { int member; };
505 // struct C : A, B { };
506 //
507 // void f(C *c) { c->### }
508 // accessing 'member' would result in an ambiguity. However, code
509 // completion could be smart enough to qualify the member with the
510 // base class, e.g.,
511 //
512 // c->B::member
513 //
514 // or
515 //
516 // c->A::member
517
518 // Collect results from this base class (and its bases).
Douglas Gregor5bf52692009-09-22 23:15:58 +0000519 CollectMemberLookupResults(Record->getDecl(), Rank, CurContext, Visited,
520 Results, /*InBaseClass=*/true);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000521 }
522 }
523
524 // FIXME: Look into base classes in Objective-C!
525
526 Results.ExitScope();
Douglas Gregor5bf52692009-09-22 23:15:58 +0000527 return Rank + 1;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000528}
529
530/// \brief Collect the results of searching for members within the given
531/// declaration context.
532///
533/// \param Ctx the declaration context from which we will gather results.
534///
535/// \param InitialRank the initial rank given to results in this declaration
536/// context. Larger rank values will be used for, e.g., members found in
537/// base classes.
538///
539/// \param Results the result set that will be extended with any results
540/// found within this declaration context (and, for a C++ class, its bases).
541///
542/// \returns the next higher rank value, after considering all of the
543/// names within this declaration context.
544static unsigned CollectMemberLookupResults(DeclContext *Ctx,
545 unsigned InitialRank,
Douglas Gregor2af2f672009-09-21 20:12:40 +0000546 DeclContext *CurContext,
Douglas Gregor3545ff42009-09-21 16:56:56 +0000547 ResultBuilder &Results) {
548 llvm::SmallPtrSet<DeclContext *, 16> Visited;
Douglas Gregor2af2f672009-09-21 20:12:40 +0000549 return CollectMemberLookupResults(Ctx, InitialRank, CurContext, Visited,
550 Results);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000551}
552
553/// \brief Collect the results of searching for declarations within the given
554/// scope and its parent scopes.
555///
556/// \param S the scope in which we will start looking for declarations.
557///
558/// \param InitialRank the initial rank given to results in this scope.
559/// Larger rank values will be used for results found in parent scopes.
560///
Douglas Gregor2af2f672009-09-21 20:12:40 +0000561/// \param CurContext the context from which lookup results will be found.
562///
Douglas Gregor3545ff42009-09-21 16:56:56 +0000563/// \param Results the builder object that will receive each result.
564static unsigned CollectLookupResults(Scope *S,
565 TranslationUnitDecl *TranslationUnit,
566 unsigned InitialRank,
Douglas Gregor2af2f672009-09-21 20:12:40 +0000567 DeclContext *CurContext,
Douglas Gregor3545ff42009-09-21 16:56:56 +0000568 ResultBuilder &Results) {
569 if (!S)
570 return InitialRank;
571
572 // FIXME: Using directives!
573
574 unsigned NextRank = InitialRank;
575 Results.EnterNewScope();
576 if (S->getEntity() &&
577 !((DeclContext *)S->getEntity())->isFunctionOrMethod()) {
578 // Look into this scope's declaration context, along with any of its
579 // parent lookup contexts (e.g., enclosing classes), up to the point
580 // where we hit the context stored in the next outer scope.
581 DeclContext *Ctx = (DeclContext *)S->getEntity();
582 DeclContext *OuterCtx = findOuterContext(S);
583
584 for (; Ctx && Ctx->getPrimaryContext() != OuterCtx;
585 Ctx = Ctx->getLookupParent()) {
586 if (Ctx->isFunctionOrMethod())
587 continue;
588
Douglas Gregor2af2f672009-09-21 20:12:40 +0000589 NextRank = CollectMemberLookupResults(Ctx, NextRank + 1, CurContext,
590 Results);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000591 }
592 } else if (!S->getParent()) {
593 // Look into the translation unit scope. We walk through the translation
594 // unit's declaration context, because the Scope itself won't have all of
595 // the declarations if we loaded a precompiled header.
596 // FIXME: We would like the translation unit's Scope object to point to the
597 // translation unit, so we don't need this special "if" branch. However,
598 // doing so would force the normal C++ name-lookup code to look into the
599 // translation unit decl when the IdentifierInfo chains would suffice.
600 // Once we fix that problem (which is part of a more general "don't look
601 // in DeclContexts unless we have to" optimization), we can eliminate the
602 // TranslationUnit parameter entirely.
603 NextRank = CollectMemberLookupResults(TranslationUnit, NextRank + 1,
Douglas Gregor2af2f672009-09-21 20:12:40 +0000604 CurContext, Results);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000605 } else {
606 // Walk through the declarations in this Scope.
607 for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
608 D != DEnd; ++D) {
609 if (NamedDecl *ND = dyn_cast<NamedDecl>((Decl *)((*D).get())))
Douglas Gregor2af2f672009-09-21 20:12:40 +0000610 Results.MaybeAddResult(CodeCompleteConsumer::Result(ND, NextRank),
611 CurContext);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000612 }
613
614 NextRank = NextRank + 1;
615 }
616
617 // Lookup names in the parent scope.
618 NextRank = CollectLookupResults(S->getParent(), TranslationUnit, NextRank,
Douglas Gregor2af2f672009-09-21 20:12:40 +0000619 CurContext, Results);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000620 Results.ExitScope();
621
622 return NextRank;
623}
624
625/// \brief Add type specifiers for the current language as keyword results.
626static void AddTypeSpecifierResults(const LangOptions &LangOpts, unsigned Rank,
627 ResultBuilder &Results) {
628 typedef CodeCompleteConsumer::Result Result;
629 Results.MaybeAddResult(Result("short", Rank));
630 Results.MaybeAddResult(Result("long", Rank));
631 Results.MaybeAddResult(Result("signed", Rank));
632 Results.MaybeAddResult(Result("unsigned", Rank));
633 Results.MaybeAddResult(Result("void", Rank));
634 Results.MaybeAddResult(Result("char", Rank));
635 Results.MaybeAddResult(Result("int", Rank));
636 Results.MaybeAddResult(Result("float", Rank));
637 Results.MaybeAddResult(Result("double", Rank));
638 Results.MaybeAddResult(Result("enum", Rank));
639 Results.MaybeAddResult(Result("struct", Rank));
640 Results.MaybeAddResult(Result("union", Rank));
641
642 if (LangOpts.C99) {
643 // C99-specific
644 Results.MaybeAddResult(Result("_Complex", Rank));
645 Results.MaybeAddResult(Result("_Imaginary", Rank));
646 Results.MaybeAddResult(Result("_Bool", Rank));
647 }
648
649 if (LangOpts.CPlusPlus) {
650 // C++-specific
651 Results.MaybeAddResult(Result("bool", Rank));
652 Results.MaybeAddResult(Result("class", Rank));
653 Results.MaybeAddResult(Result("typename", Rank));
654 Results.MaybeAddResult(Result("wchar_t", Rank));
655
656 if (LangOpts.CPlusPlus0x) {
657 Results.MaybeAddResult(Result("char16_t", Rank));
658 Results.MaybeAddResult(Result("char32_t", Rank));
659 Results.MaybeAddResult(Result("decltype", Rank));
660 }
661 }
662
663 // GNU extensions
664 if (LangOpts.GNUMode) {
665 // FIXME: Enable when we actually support decimal floating point.
666 // Results.MaybeAddResult(Result("_Decimal32", Rank));
667 // Results.MaybeAddResult(Result("_Decimal64", Rank));
668 // Results.MaybeAddResult(Result("_Decimal128", Rank));
669 Results.MaybeAddResult(Result("typeof", Rank));
670 }
671}
672
673/// \brief Add function parameter chunks to the given code completion string.
674static void AddFunctionParameterChunks(ASTContext &Context,
675 FunctionDecl *Function,
676 CodeCompletionString *Result) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000677 typedef CodeCompletionString::Chunk Chunk;
678
Douglas Gregor3545ff42009-09-21 16:56:56 +0000679 CodeCompletionString *CCStr = Result;
680
681 for (unsigned P = 0, N = Function->getNumParams(); P != N; ++P) {
682 ParmVarDecl *Param = Function->getParamDecl(P);
683
684 if (Param->hasDefaultArg()) {
685 // When we see an optional default argument, put that argument and
686 // the remaining default arguments into a new, optional string.
687 CodeCompletionString *Opt = new CodeCompletionString;
688 CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt));
689 CCStr = Opt;
690 }
691
692 if (P != 0)
Douglas Gregor9eb77012009-11-07 00:00:49 +0000693 CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000694
695 // Format the placeholder string.
696 std::string PlaceholderStr;
697 if (Param->getIdentifier())
698 PlaceholderStr = Param->getIdentifier()->getName();
699
700 Param->getType().getAsStringInternal(PlaceholderStr,
701 Context.PrintingPolicy);
702
703 // Add the placeholder string.
704 CCStr->AddPlaceholderChunk(PlaceholderStr.c_str());
705 }
Douglas Gregorba449032009-09-22 21:42:17 +0000706
707 if (const FunctionProtoType *Proto
708 = Function->getType()->getAs<FunctionProtoType>())
709 if (Proto->isVariadic())
710 CCStr->AddPlaceholderChunk(", ...");
Douglas Gregor3545ff42009-09-21 16:56:56 +0000711}
712
713/// \brief Add template parameter chunks to the given code completion string.
714static void AddTemplateParameterChunks(ASTContext &Context,
715 TemplateDecl *Template,
716 CodeCompletionString *Result,
717 unsigned MaxParameters = 0) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000718 typedef CodeCompletionString::Chunk Chunk;
719
Douglas Gregor3545ff42009-09-21 16:56:56 +0000720 CodeCompletionString *CCStr = Result;
721 bool FirstParameter = true;
722
723 TemplateParameterList *Params = Template->getTemplateParameters();
724 TemplateParameterList::iterator PEnd = Params->end();
725 if (MaxParameters)
726 PEnd = Params->begin() + MaxParameters;
727 for (TemplateParameterList::iterator P = Params->begin(); P != PEnd; ++P) {
728 bool HasDefaultArg = false;
729 std::string PlaceholderStr;
730 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
731 if (TTP->wasDeclaredWithTypename())
732 PlaceholderStr = "typename";
733 else
734 PlaceholderStr = "class";
735
736 if (TTP->getIdentifier()) {
737 PlaceholderStr += ' ';
738 PlaceholderStr += TTP->getIdentifier()->getName();
739 }
740
741 HasDefaultArg = TTP->hasDefaultArgument();
742 } else if (NonTypeTemplateParmDecl *NTTP
743 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
744 if (NTTP->getIdentifier())
745 PlaceholderStr = NTTP->getIdentifier()->getName();
746 NTTP->getType().getAsStringInternal(PlaceholderStr,
747 Context.PrintingPolicy);
748 HasDefaultArg = NTTP->hasDefaultArgument();
749 } else {
750 assert(isa<TemplateTemplateParmDecl>(*P));
751 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
752
753 // Since putting the template argument list into the placeholder would
754 // be very, very long, we just use an abbreviation.
755 PlaceholderStr = "template<...> class";
756 if (TTP->getIdentifier()) {
757 PlaceholderStr += ' ';
758 PlaceholderStr += TTP->getIdentifier()->getName();
759 }
760
761 HasDefaultArg = TTP->hasDefaultArgument();
762 }
763
764 if (HasDefaultArg) {
765 // When we see an optional default argument, put that argument and
766 // the remaining default arguments into a new, optional string.
767 CodeCompletionString *Opt = new CodeCompletionString;
768 CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt));
769 CCStr = Opt;
770 }
771
772 if (FirstParameter)
773 FirstParameter = false;
774 else
Douglas Gregor9eb77012009-11-07 00:00:49 +0000775 CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000776
777 // Add the placeholder string.
778 CCStr->AddPlaceholderChunk(PlaceholderStr.c_str());
779 }
780}
781
Douglas Gregorf2510672009-09-21 19:57:38 +0000782/// \brief Add a qualifier to the given code-completion string, if the
783/// provided nested-name-specifier is non-NULL.
784void AddQualifierToCompletionString(CodeCompletionString *Result,
785 NestedNameSpecifier *Qualifier,
Douglas Gregor5bf52692009-09-22 23:15:58 +0000786 bool QualifierIsInformative,
Douglas Gregorf2510672009-09-21 19:57:38 +0000787 ASTContext &Context) {
788 if (!Qualifier)
789 return;
790
791 std::string PrintedNNS;
792 {
793 llvm::raw_string_ostream OS(PrintedNNS);
794 Qualifier->print(OS, Context.PrintingPolicy);
795 }
Douglas Gregor5bf52692009-09-22 23:15:58 +0000796 if (QualifierIsInformative)
797 Result->AddInformativeChunk(PrintedNNS.c_str());
798 else
799 Result->AddTextChunk(PrintedNNS.c_str());
Douglas Gregorf2510672009-09-21 19:57:38 +0000800}
801
Douglas Gregor3545ff42009-09-21 16:56:56 +0000802/// \brief If possible, create a new code completion string for the given
803/// result.
804///
805/// \returns Either a new, heap-allocated code completion string describing
806/// how to use this result, or NULL to indicate that the string or name of the
807/// result is all that is needed.
808CodeCompletionString *
809CodeCompleteConsumer::Result::CreateCodeCompletionString(Sema &S) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000810 typedef CodeCompletionString::Chunk Chunk;
811
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000812 if (Kind == RK_Keyword)
Douglas Gregor3545ff42009-09-21 16:56:56 +0000813 return 0;
814
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000815 if (Kind == RK_Macro) {
816 MacroInfo *MI = S.PP.getMacroInfo(Macro);
817 if (!MI || !MI->isFunctionLike())
818 return 0;
819
820 // Format a function-like macro with placeholders for the arguments.
821 CodeCompletionString *Result = new CodeCompletionString;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000822 Result->AddTypedTextChunk(Macro->getName().str().c_str());
823 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000824 for (MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end();
825 A != AEnd; ++A) {
826 if (A != MI->arg_begin())
Douglas Gregor9eb77012009-11-07 00:00:49 +0000827 Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000828
829 if (!MI->isVariadic() || A != AEnd - 1) {
830 // Non-variadic argument.
831 Result->AddPlaceholderChunk((*A)->getName().str().c_str());
832 continue;
833 }
834
835 // Variadic argument; cope with the different between GNU and C99
836 // variadic macros, providing a single placeholder for the rest of the
837 // arguments.
838 if ((*A)->isStr("__VA_ARGS__"))
839 Result->AddPlaceholderChunk("...");
840 else {
841 std::string Arg = (*A)->getName();
842 Arg += "...";
843 Result->AddPlaceholderChunk(Arg.c_str());
844 }
845 }
Douglas Gregor9eb77012009-11-07 00:00:49 +0000846 Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000847 return Result;
848 }
849
850 assert(Kind == RK_Declaration && "Missed a macro kind?");
Douglas Gregor3545ff42009-09-21 16:56:56 +0000851 NamedDecl *ND = Declaration;
852
Douglas Gregor9eb77012009-11-07 00:00:49 +0000853 if (StartsNestedNameSpecifier) {
854 CodeCompletionString *Result = new CodeCompletionString;
855 Result->AddTypedTextChunk(ND->getNameAsString().c_str());
856 Result->AddTextChunk("::");
857 return Result;
858 }
859
Douglas Gregor3545ff42009-09-21 16:56:56 +0000860 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
861 CodeCompletionString *Result = new CodeCompletionString;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000862 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
863 S.Context);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000864 Result->AddTypedTextChunk(Function->getNameAsString().c_str());
865 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000866 AddFunctionParameterChunks(S.Context, Function, Result);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000867 Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000868 return Result;
869 }
870
871 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
872 CodeCompletionString *Result = new CodeCompletionString;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000873 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
874 S.Context);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000875 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
Douglas Gregor9eb77012009-11-07 00:00:49 +0000876 Result->AddTypedTextChunk(Function->getNameAsString().c_str());
Douglas Gregor3545ff42009-09-21 16:56:56 +0000877
878 // Figure out which template parameters are deduced (or have default
879 // arguments).
880 llvm::SmallVector<bool, 16> Deduced;
881 S.MarkDeducedTemplateParameters(FunTmpl, Deduced);
882 unsigned LastDeducibleArgument;
883 for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0;
884 --LastDeducibleArgument) {
885 if (!Deduced[LastDeducibleArgument - 1]) {
886 // C++0x: Figure out if the template argument has a default. If so,
887 // the user doesn't need to type this argument.
888 // FIXME: We need to abstract template parameters better!
889 bool HasDefaultArg = false;
890 NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam(
891 LastDeducibleArgument - 1);
892 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
893 HasDefaultArg = TTP->hasDefaultArgument();
894 else if (NonTypeTemplateParmDecl *NTTP
895 = dyn_cast<NonTypeTemplateParmDecl>(Param))
896 HasDefaultArg = NTTP->hasDefaultArgument();
897 else {
898 assert(isa<TemplateTemplateParmDecl>(Param));
899 HasDefaultArg
Douglas Gregor9eb77012009-11-07 00:00:49 +0000900 = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument();
Douglas Gregor3545ff42009-09-21 16:56:56 +0000901 }
902
903 if (!HasDefaultArg)
904 break;
905 }
906 }
907
908 if (LastDeducibleArgument) {
909 // Some of the function template arguments cannot be deduced from a
910 // function call, so we introduce an explicit template argument list
911 // containing all of the arguments up to the first deducible argument.
Douglas Gregor9eb77012009-11-07 00:00:49 +0000912 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000913 AddTemplateParameterChunks(S.Context, FunTmpl, Result,
914 LastDeducibleArgument);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000915 Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000916 }
917
918 // Add the function parameters
Douglas Gregor9eb77012009-11-07 00:00:49 +0000919 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000920 AddFunctionParameterChunks(S.Context, Function, Result);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000921 Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000922 return Result;
923 }
924
925 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) {
926 CodeCompletionString *Result = new CodeCompletionString;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000927 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
928 S.Context);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000929 Result->AddTypedTextChunk(Template->getNameAsString().c_str());
930 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000931 AddTemplateParameterChunks(S.Context, Template, Result);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000932 Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle));
Douglas Gregor3545ff42009-09-21 16:56:56 +0000933 return Result;
934 }
935
Douglas Gregord3c5d792009-11-17 16:44:22 +0000936 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) {
937 CodeCompletionString *Result = new CodeCompletionString;
938 Selector Sel = Method->getSelector();
939 if (Sel.isUnarySelector()) {
940 Result->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName());
941 return Result;
942 }
943
944 Result->AddTypedTextChunk(
945 Sel.getIdentifierInfoForSlot(0)->getName().str() + std::string(":"));
946 unsigned Idx = 0;
947 for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
948 PEnd = Method->param_end();
949 P != PEnd; (void)++P, ++Idx) {
950 if (Idx > 0) {
951 std::string Keyword = " ";
952 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx))
953 Keyword += II->getName().str();
954 Keyword += ":";
955 Result->AddTextChunk(Keyword);
956 }
957
958 std::string Arg;
959 (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy);
960 Arg = "(" + Arg + ")";
961 if (IdentifierInfo *II = (*P)->getIdentifier())
962 Arg += II->getName().str();
963 Result->AddPlaceholderChunk(Arg);
964 }
965
966 return Result;
967 }
968
Douglas Gregor9eb77012009-11-07 00:00:49 +0000969 if (Qualifier) {
Douglas Gregorf2510672009-09-21 19:57:38 +0000970 CodeCompletionString *Result = new CodeCompletionString;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000971 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
972 S.Context);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000973 Result->AddTypedTextChunk(ND->getNameAsString().c_str());
Douglas Gregorf2510672009-09-21 19:57:38 +0000974 return Result;
975 }
976
Douglas Gregor3545ff42009-09-21 16:56:56 +0000977 return 0;
978}
979
Douglas Gregorf0f51982009-09-23 00:34:09 +0000980CodeCompletionString *
981CodeCompleteConsumer::OverloadCandidate::CreateSignatureString(
982 unsigned CurrentArg,
983 Sema &S) const {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000984 typedef CodeCompletionString::Chunk Chunk;
985
Douglas Gregorf0f51982009-09-23 00:34:09 +0000986 CodeCompletionString *Result = new CodeCompletionString;
987 FunctionDecl *FDecl = getFunction();
988 const FunctionProtoType *Proto
989 = dyn_cast<FunctionProtoType>(getFunctionType());
990 if (!FDecl && !Proto) {
991 // Function without a prototype. Just give the return type and a
992 // highlighted ellipsis.
993 const FunctionType *FT = getFunctionType();
994 Result->AddTextChunk(
995 FT->getResultType().getAsString(S.Context.PrintingPolicy).c_str());
Douglas Gregor9eb77012009-11-07 00:00:49 +0000996 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
997 Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "..."));
998 Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
Douglas Gregorf0f51982009-09-23 00:34:09 +0000999 return Result;
1000 }
1001
1002 if (FDecl)
1003 Result->AddTextChunk(FDecl->getNameAsString().c_str());
1004 else
1005 Result->AddTextChunk(
1006 Proto->getResultType().getAsString(S.Context.PrintingPolicy).c_str());
1007
Douglas Gregor9eb77012009-11-07 00:00:49 +00001008 Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001009 unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs();
1010 for (unsigned I = 0; I != NumParams; ++I) {
1011 if (I)
Douglas Gregor9eb77012009-11-07 00:00:49 +00001012 Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001013
1014 std::string ArgString;
1015 QualType ArgType;
1016
1017 if (FDecl) {
1018 ArgString = FDecl->getParamDecl(I)->getNameAsString();
1019 ArgType = FDecl->getParamDecl(I)->getOriginalType();
1020 } else {
1021 ArgType = Proto->getArgType(I);
1022 }
1023
1024 ArgType.getAsStringInternal(ArgString, S.Context.PrintingPolicy);
1025
1026 if (I == CurrentArg)
Douglas Gregor9eb77012009-11-07 00:00:49 +00001027 Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter,
1028 ArgString.c_str()));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001029 else
1030 Result->AddTextChunk(ArgString.c_str());
1031 }
1032
1033 if (Proto && Proto->isVariadic()) {
Douglas Gregor9eb77012009-11-07 00:00:49 +00001034 Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001035 if (CurrentArg < NumParams)
1036 Result->AddTextChunk("...");
1037 else
Douglas Gregor9eb77012009-11-07 00:00:49 +00001038 Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "..."));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001039 }
Douglas Gregor9eb77012009-11-07 00:00:49 +00001040 Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
Douglas Gregorf0f51982009-09-23 00:34:09 +00001041
1042 return Result;
1043}
1044
Douglas Gregor3545ff42009-09-21 16:56:56 +00001045namespace {
1046 struct SortCodeCompleteResult {
1047 typedef CodeCompleteConsumer::Result Result;
1048
Douglas Gregore6688e62009-09-28 03:51:44 +00001049 bool isEarlierDeclarationName(DeclarationName X, DeclarationName Y) const {
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00001050 if (!X.getObjCSelector().isNull() && !Y.getObjCSelector().isNull()) {
1051 // Consider all selector kinds to be equivalent.
1052 } else if (X.getNameKind() != Y.getNameKind())
Douglas Gregore6688e62009-09-28 03:51:44 +00001053 return X.getNameKind() < Y.getNameKind();
1054
1055 return llvm::LowercaseString(X.getAsString())
1056 < llvm::LowercaseString(Y.getAsString());
1057 }
1058
Douglas Gregor3545ff42009-09-21 16:56:56 +00001059 bool operator()(const Result &X, const Result &Y) const {
1060 // Sort first by rank.
1061 if (X.Rank < Y.Rank)
1062 return true;
1063 else if (X.Rank > Y.Rank)
1064 return false;
1065
1066 // Result kinds are ordered by decreasing importance.
1067 if (X.Kind < Y.Kind)
1068 return true;
1069 else if (X.Kind > Y.Kind)
1070 return false;
1071
1072 // Non-hidden names precede hidden names.
1073 if (X.Hidden != Y.Hidden)
1074 return !X.Hidden;
1075
Douglas Gregore412a5a2009-09-23 22:26:46 +00001076 // Non-nested-name-specifiers precede nested-name-specifiers.
1077 if (X.StartsNestedNameSpecifier != Y.StartsNestedNameSpecifier)
1078 return !X.StartsNestedNameSpecifier;
1079
Douglas Gregor3545ff42009-09-21 16:56:56 +00001080 // Ordering depends on the kind of result.
1081 switch (X.Kind) {
1082 case Result::RK_Declaration:
1083 // Order based on the declaration names.
Douglas Gregore6688e62009-09-28 03:51:44 +00001084 return isEarlierDeclarationName(X.Declaration->getDeclName(),
1085 Y.Declaration->getDeclName());
Douglas Gregor3545ff42009-09-21 16:56:56 +00001086
1087 case Result::RK_Keyword:
Steve Naroff3b086302009-10-08 23:45:10 +00001088 return strcmp(X.Keyword, Y.Keyword) < 0;
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001089
1090 case Result::RK_Macro:
1091 return llvm::LowercaseString(X.Macro->getName()) <
1092 llvm::LowercaseString(Y.Macro->getName());
Douglas Gregor3545ff42009-09-21 16:56:56 +00001093 }
1094
1095 // Silence GCC warning.
1096 return false;
1097 }
1098 };
1099}
1100
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001101static void AddMacroResults(Preprocessor &PP, unsigned Rank,
1102 ResultBuilder &Results) {
1103 Results.EnterNewScope();
Douglas Gregor9eb77012009-11-07 00:00:49 +00001104 for (Preprocessor::macro_iterator M = PP.macro_begin(),
1105 MEnd = PP.macro_end();
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001106 M != MEnd; ++M)
1107 Results.MaybeAddResult(CodeCompleteConsumer::Result(M->first, Rank));
1108 Results.ExitScope();
1109}
1110
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001111static void HandleCodeCompleteResults(Sema *S,
1112 CodeCompleteConsumer *CodeCompleter,
1113 CodeCompleteConsumer::Result *Results,
1114 unsigned NumResults) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00001115 // Sort the results by rank/kind/etc.
1116 std::stable_sort(Results, Results + NumResults, SortCodeCompleteResult());
1117
1118 if (CodeCompleter)
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001119 CodeCompleter->ProcessCodeCompleteResults(*S, Results, NumResults);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001120}
1121
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001122void Sema::CodeCompleteOrdinaryName(Scope *S) {
1123 ResultBuilder Results(*this, &ResultBuilder::IsOrdinaryName);
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001124 unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
1125 0, CurContext, Results);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001126 if (CodeCompleter->includeMacros())
1127 AddMacroResults(PP, NextRank, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001128 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001129}
1130
Douglas Gregor9291bad2009-11-18 01:29:26 +00001131static void AddObjCProperties(ObjCContainerDecl *Container,
1132 DeclContext *CurContext,
1133 ResultBuilder &Results) {
1134 typedef CodeCompleteConsumer::Result Result;
1135
1136 // Add properties in this container.
1137 for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(),
1138 PEnd = Container->prop_end();
1139 P != PEnd;
1140 ++P)
1141 Results.MaybeAddResult(Result(*P, 0), CurContext);
1142
1143 // Add properties in referenced protocols.
1144 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
1145 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
1146 PEnd = Protocol->protocol_end();
1147 P != PEnd; ++P)
1148 AddObjCProperties(*P, CurContext, Results);
1149 } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){
1150 // Look through categories.
1151 for (ObjCCategoryDecl *Category = IFace->getCategoryList();
1152 Category; Category = Category->getNextClassCategory())
1153 AddObjCProperties(Category, CurContext, Results);
1154
1155 // Look through protocols.
1156 for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(),
1157 E = IFace->protocol_end();
1158 I != E; ++I)
1159 AddObjCProperties(*I, CurContext, Results);
1160
1161 // Look in the superclass.
1162 if (IFace->getSuperClass())
1163 AddObjCProperties(IFace->getSuperClass(), CurContext, Results);
1164 } else if (const ObjCCategoryDecl *Category
1165 = dyn_cast<ObjCCategoryDecl>(Container)) {
1166 // Look through protocols.
1167 for (ObjCInterfaceDecl::protocol_iterator P = Category->protocol_begin(),
1168 PEnd = Category->protocol_end();
1169 P != PEnd; ++P)
1170 AddObjCProperties(*P, CurContext, Results);
1171 }
1172}
1173
Douglas Gregor2436e712009-09-17 21:32:03 +00001174void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE,
1175 SourceLocation OpLoc,
1176 bool IsArrow) {
1177 if (!BaseE || !CodeCompleter)
1178 return;
1179
Douglas Gregor3545ff42009-09-21 16:56:56 +00001180 typedef CodeCompleteConsumer::Result Result;
1181
Douglas Gregor2436e712009-09-17 21:32:03 +00001182 Expr *Base = static_cast<Expr *>(BaseE);
1183 QualType BaseType = Base->getType();
Douglas Gregor3545ff42009-09-21 16:56:56 +00001184
1185 if (IsArrow) {
1186 if (const PointerType *Ptr = BaseType->getAs<PointerType>())
1187 BaseType = Ptr->getPointeeType();
1188 else if (BaseType->isObjCObjectPointerType())
1189 /*Do nothing*/ ;
1190 else
1191 return;
1192 }
1193
Douglas Gregore412a5a2009-09-23 22:26:46 +00001194 ResultBuilder Results(*this, &ResultBuilder::IsMember);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001195 unsigned NextRank = 0;
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001196
Douglas Gregor9291bad2009-11-18 01:29:26 +00001197 Results.EnterNewScope();
1198 if (const RecordType *Record = BaseType->getAs<RecordType>()) {
1199 // Access to a C/C++ class, struct, or union.
1200 NextRank = CollectMemberLookupResults(Record->getDecl(), NextRank,
1201 Record->getDecl(), Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001202
Douglas Gregor9291bad2009-11-18 01:29:26 +00001203 if (getLangOptions().CPlusPlus) {
1204 if (!Results.empty()) {
1205 // The "template" keyword can follow "->" or "." in the grammar.
1206 // However, we only want to suggest the template keyword if something
1207 // is dependent.
1208 bool IsDependent = BaseType->isDependentType();
1209 if (!IsDependent) {
1210 for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent())
1211 if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) {
1212 IsDependent = Ctx->isDependentContext();
1213 break;
1214 }
1215 }
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001216
Douglas Gregor9291bad2009-11-18 01:29:26 +00001217 if (IsDependent)
1218 Results.MaybeAddResult(Result("template", NextRank++));
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001219 }
1220
Douglas Gregor9291bad2009-11-18 01:29:26 +00001221 // We could have the start of a nested-name-specifier. Add those
1222 // results as well.
1223 Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
1224 CollectLookupResults(S, Context.getTranslationUnitDecl(), NextRank,
1225 CurContext, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001226 }
Douglas Gregor9291bad2009-11-18 01:29:26 +00001227 } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) {
1228 // Objective-C property reference.
1229
1230 // Add property results based on our interface.
1231 const ObjCObjectPointerType *ObjCPtr
1232 = BaseType->getAsObjCInterfacePointerType();
1233 assert(ObjCPtr && "Non-NULL pointer guaranteed above!");
1234 AddObjCProperties(ObjCPtr->getInterfaceDecl(), CurContext, Results);
1235
1236 // Add properties from the protocols in a qualified interface.
1237 for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(),
1238 E = ObjCPtr->qual_end();
1239 I != E; ++I)
1240 AddObjCProperties(*I, CurContext, Results);
1241
1242 // FIXME: We could (should?) also look for "implicit" properties, identified
1243 // only by the presence of nullary and unary selectors.
1244 } else if ((IsArrow && BaseType->isObjCObjectPointerType()) ||
1245 (!IsArrow && BaseType->isObjCInterfaceType())) {
1246 // Objective-C instance variable access.
1247 ObjCInterfaceDecl *Class = 0;
1248 if (const ObjCObjectPointerType *ObjCPtr
1249 = BaseType->getAs<ObjCObjectPointerType>())
1250 Class = ObjCPtr->getInterfaceDecl();
1251 else
1252 Class = BaseType->getAs<ObjCInterfaceType>()->getDecl();
1253
1254 // Add all ivars from this class and its superclasses.
1255 for (; Class; Class = Class->getSuperClass()) {
1256 for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(),
1257 IVarEnd = Class->ivar_end();
1258 IVar != IVarEnd; ++IVar)
1259 Results.MaybeAddResult(Result(*IVar, 0), CurContext);
1260 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001261 }
Douglas Gregor9291bad2009-11-18 01:29:26 +00001262
1263 // FIXME: How do we cope with isa?
1264
1265 Results.ExitScope();
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001266
1267 // Add macros
1268 if (CodeCompleter->includeMacros())
1269 AddMacroResults(PP, NextRank, Results);
1270
1271 // Hand off the results found for code completion.
1272 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor2436e712009-09-17 21:32:03 +00001273}
1274
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001275void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) {
1276 if (!CodeCompleter)
1277 return;
1278
Douglas Gregor3545ff42009-09-21 16:56:56 +00001279 typedef CodeCompleteConsumer::Result Result;
1280 ResultBuilder::LookupFilter Filter = 0;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001281 switch ((DeclSpec::TST)TagSpec) {
1282 case DeclSpec::TST_enum:
Douglas Gregor3545ff42009-09-21 16:56:56 +00001283 Filter = &ResultBuilder::IsEnum;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001284 break;
1285
1286 case DeclSpec::TST_union:
Douglas Gregor3545ff42009-09-21 16:56:56 +00001287 Filter = &ResultBuilder::IsUnion;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001288 break;
1289
1290 case DeclSpec::TST_struct:
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001291 case DeclSpec::TST_class:
Douglas Gregor3545ff42009-09-21 16:56:56 +00001292 Filter = &ResultBuilder::IsClassOrStruct;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001293 break;
1294
1295 default:
1296 assert(false && "Unknown type specifier kind in CodeCompleteTag");
1297 return;
1298 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001299
1300 ResultBuilder Results(*this, Filter);
1301 unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
Douglas Gregor2af2f672009-09-21 20:12:40 +00001302 0, CurContext, Results);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001303
1304 if (getLangOptions().CPlusPlus) {
1305 // We could have the start of a nested-name-specifier. Add those
1306 // results as well.
1307 Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001308 NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
1309 NextRank, CurContext, Results);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001310 }
1311
Douglas Gregor9eb77012009-11-07 00:00:49 +00001312 if (CodeCompleter->includeMacros())
1313 AddMacroResults(PP, NextRank, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001314 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001315}
1316
Douglas Gregord328d572009-09-21 18:10:23 +00001317void Sema::CodeCompleteCase(Scope *S) {
1318 if (getSwitchStack().empty() || !CodeCompleter)
1319 return;
1320
1321 SwitchStmt *Switch = getSwitchStack().back();
1322 if (!Switch->getCond()->getType()->isEnumeralType())
1323 return;
1324
1325 // Code-complete the cases of a switch statement over an enumeration type
1326 // by providing the list of
1327 EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl();
1328
1329 // Determine which enumerators we have already seen in the switch statement.
1330 // FIXME: Ideally, we would also be able to look *past* the code-completion
1331 // token, in case we are code-completing in the middle of the switch and not
1332 // at the end. However, we aren't able to do so at the moment.
1333 llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen;
Douglas Gregorf2510672009-09-21 19:57:38 +00001334 NestedNameSpecifier *Qualifier = 0;
Douglas Gregord328d572009-09-21 18:10:23 +00001335 for (SwitchCase *SC = Switch->getSwitchCaseList(); SC;
1336 SC = SC->getNextSwitchCase()) {
1337 CaseStmt *Case = dyn_cast<CaseStmt>(SC);
1338 if (!Case)
1339 continue;
1340
1341 Expr *CaseVal = Case->getLHS()->IgnoreParenCasts();
1342 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal))
1343 if (EnumConstantDecl *Enumerator
1344 = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
1345 // We look into the AST of the case statement to determine which
1346 // enumerator was named. Alternatively, we could compute the value of
1347 // the integral constant expression, then compare it against the
1348 // values of each enumerator. However, value-based approach would not
1349 // work as well with C++ templates where enumerators declared within a
1350 // template are type- and value-dependent.
1351 EnumeratorsSeen.insert(Enumerator);
1352
Douglas Gregorf2510672009-09-21 19:57:38 +00001353 // If this is a qualified-id, keep track of the nested-name-specifier
1354 // so that we can reproduce it as part of code completion, e.g.,
Douglas Gregord328d572009-09-21 18:10:23 +00001355 //
1356 // switch (TagD.getKind()) {
1357 // case TagDecl::TK_enum:
1358 // break;
1359 // case XXX
1360 //
Douglas Gregorf2510672009-09-21 19:57:38 +00001361 // At the XXX, our completions are TagDecl::TK_union,
Douglas Gregord328d572009-09-21 18:10:23 +00001362 // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union,
1363 // TK_struct, and TK_class.
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001364 Qualifier = DRE->getQualifier();
Douglas Gregord328d572009-09-21 18:10:23 +00001365 }
1366 }
1367
Douglas Gregorf2510672009-09-21 19:57:38 +00001368 if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) {
1369 // If there are no prior enumerators in C++, check whether we have to
1370 // qualify the names of the enumerators that we suggest, because they
1371 // may not be visible in this scope.
1372 Qualifier = getRequiredQualification(Context, CurContext,
1373 Enum->getDeclContext());
1374
1375 // FIXME: Scoped enums need to start with "EnumDecl" as the context!
1376 }
1377
Douglas Gregord328d572009-09-21 18:10:23 +00001378 // Add any enumerators that have not yet been mentioned.
1379 ResultBuilder Results(*this);
1380 Results.EnterNewScope();
1381 for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(),
1382 EEnd = Enum->enumerator_end();
1383 E != EEnd; ++E) {
1384 if (EnumeratorsSeen.count(*E))
1385 continue;
1386
Douglas Gregorf2510672009-09-21 19:57:38 +00001387 Results.MaybeAddResult(CodeCompleteConsumer::Result(*E, 0, Qualifier));
Douglas Gregord328d572009-09-21 18:10:23 +00001388 }
1389 Results.ExitScope();
1390
Douglas Gregor9eb77012009-11-07 00:00:49 +00001391 if (CodeCompleter->includeMacros())
1392 AddMacroResults(PP, 1, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001393 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregord328d572009-09-21 18:10:23 +00001394}
1395
Douglas Gregorcabea402009-09-22 15:41:20 +00001396namespace {
1397 struct IsBetterOverloadCandidate {
1398 Sema &S;
1399
1400 public:
1401 explicit IsBetterOverloadCandidate(Sema &S) : S(S) { }
1402
1403 bool
1404 operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const {
1405 return S.isBetterOverloadCandidate(X, Y);
1406 }
1407 };
1408}
1409
1410void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn,
1411 ExprTy **ArgsIn, unsigned NumArgs) {
1412 if (!CodeCompleter)
1413 return;
1414
1415 Expr *Fn = (Expr *)FnIn;
1416 Expr **Args = (Expr **)ArgsIn;
1417
1418 // Ignore type-dependent call expressions entirely.
1419 if (Fn->isTypeDependent() ||
1420 Expr::hasAnyTypeDependentArguments(Args, NumArgs))
1421 return;
1422
1423 NamedDecl *Function;
1424 DeclarationName UnqualifiedName;
1425 NestedNameSpecifier *Qualifier;
1426 SourceRange QualifierRange;
1427 bool ArgumentDependentLookup;
1428 bool HasExplicitTemplateArgs;
John McCall0ad16662009-10-29 08:12:44 +00001429 const TemplateArgumentLoc *ExplicitTemplateArgs;
Douglas Gregorcabea402009-09-22 15:41:20 +00001430 unsigned NumExplicitTemplateArgs;
1431
1432 DeconstructCallFunction(Fn,
1433 Function, UnqualifiedName, Qualifier, QualifierRange,
1434 ArgumentDependentLookup, HasExplicitTemplateArgs,
1435 ExplicitTemplateArgs, NumExplicitTemplateArgs);
1436
1437
1438 // FIXME: What if we're calling something that isn't a function declaration?
1439 // FIXME: What if we're calling a pseudo-destructor?
1440 // FIXME: What if we're calling a member function?
1441
1442 // Build an overload candidate set based on the functions we find.
1443 OverloadCandidateSet CandidateSet;
1444 AddOverloadedCallCandidates(Function, UnqualifiedName,
1445 ArgumentDependentLookup, HasExplicitTemplateArgs,
1446 ExplicitTemplateArgs, NumExplicitTemplateArgs,
1447 Args, NumArgs,
1448 CandidateSet,
1449 /*PartialOverloading=*/true);
1450
1451 // Sort the overload candidate set by placing the best overloads first.
1452 std::stable_sort(CandidateSet.begin(), CandidateSet.end(),
1453 IsBetterOverloadCandidate(*this));
1454
1455 // Add the remaining viable overload candidates as code-completion reslults.
Douglas Gregor05f477c2009-09-23 00:16:58 +00001456 typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate;
1457 llvm::SmallVector<ResultCandidate, 8> Results;
Anders Carlssone7ceb852009-09-22 17:29:51 +00001458
Douglas Gregorcabea402009-09-22 15:41:20 +00001459 for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
1460 CandEnd = CandidateSet.end();
1461 Cand != CandEnd; ++Cand) {
1462 if (Cand->Viable)
Douglas Gregor05f477c2009-09-23 00:16:58 +00001463 Results.push_back(ResultCandidate(Cand->Function));
Douglas Gregorcabea402009-09-22 15:41:20 +00001464 }
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001465 CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(),
Douglas Gregor05f477c2009-09-23 00:16:58 +00001466 Results.size());
Douglas Gregorcabea402009-09-22 15:41:20 +00001467}
1468
Douglas Gregor2436e712009-09-17 21:32:03 +00001469void Sema::CodeCompleteQualifiedId(Scope *S, const CXXScopeSpec &SS,
1470 bool EnteringContext) {
1471 if (!SS.getScopeRep() || !CodeCompleter)
1472 return;
1473
Douglas Gregor3545ff42009-09-21 16:56:56 +00001474 DeclContext *Ctx = computeDeclContext(SS, EnteringContext);
1475 if (!Ctx)
1476 return;
1477
1478 ResultBuilder Results(*this);
Douglas Gregor2af2f672009-09-21 20:12:40 +00001479 unsigned NextRank = CollectMemberLookupResults(Ctx, 0, Ctx, Results);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001480
1481 // The "template" keyword can follow "::" in the grammar, but only
1482 // put it into the grammar if the nested-name-specifier is dependent.
1483 NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1484 if (!Results.empty() && NNS->isDependent())
1485 Results.MaybeAddResult(CodeCompleteConsumer::Result("template", NextRank));
1486
Douglas Gregor9eb77012009-11-07 00:00:49 +00001487 if (CodeCompleter->includeMacros())
1488 AddMacroResults(PP, NextRank + 1, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001489 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor2436e712009-09-17 21:32:03 +00001490}
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00001491
1492void Sema::CodeCompleteUsing(Scope *S) {
1493 if (!CodeCompleter)
1494 return;
1495
Douglas Gregor3545ff42009-09-21 16:56:56 +00001496 ResultBuilder Results(*this, &ResultBuilder::IsNestedNameSpecifier);
Douglas Gregor64b12b52009-09-22 23:31:26 +00001497 Results.EnterNewScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00001498
1499 // If we aren't in class scope, we could see the "namespace" keyword.
1500 if (!S->isClassScope())
1501 Results.MaybeAddResult(CodeCompleteConsumer::Result("namespace", 0));
1502
1503 // After "using", we can see anything that would start a
1504 // nested-name-specifier.
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001505 unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
1506 0, CurContext, Results);
Douglas Gregor64b12b52009-09-22 23:31:26 +00001507 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00001508
Douglas Gregor9eb77012009-11-07 00:00:49 +00001509 if (CodeCompleter->includeMacros())
1510 AddMacroResults(PP, NextRank, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001511 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00001512}
1513
1514void Sema::CodeCompleteUsingDirective(Scope *S) {
1515 if (!CodeCompleter)
1516 return;
1517
Douglas Gregor3545ff42009-09-21 16:56:56 +00001518 // After "using namespace", we expect to see a namespace name or namespace
1519 // alias.
1520 ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregor64b12b52009-09-22 23:31:26 +00001521 Results.EnterNewScope();
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001522 unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
1523 0, CurContext, Results);
Douglas Gregor64b12b52009-09-22 23:31:26 +00001524 Results.ExitScope();
Douglas Gregor9eb77012009-11-07 00:00:49 +00001525 if (CodeCompleter->includeMacros())
1526 AddMacroResults(PP, NextRank, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001527 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00001528}
1529
1530void Sema::CodeCompleteNamespaceDecl(Scope *S) {
1531 if (!CodeCompleter)
1532 return;
1533
Douglas Gregor3545ff42009-09-21 16:56:56 +00001534 ResultBuilder Results(*this, &ResultBuilder::IsNamespace);
1535 DeclContext *Ctx = (DeclContext *)S->getEntity();
1536 if (!S->getParent())
1537 Ctx = Context.getTranslationUnitDecl();
1538
1539 if (Ctx && Ctx->isFileContext()) {
1540 // We only want to see those namespaces that have already been defined
1541 // within this scope, because its likely that the user is creating an
1542 // extended namespace declaration. Keep track of the most recent
1543 // definition of each namespace.
1544 std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest;
1545 for (DeclContext::specific_decl_iterator<NamespaceDecl>
1546 NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end());
1547 NS != NSEnd; ++NS)
1548 OrigToLatest[NS->getOriginalNamespace()] = *NS;
1549
1550 // Add the most recent definition (or extended definition) of each
1551 // namespace to the list of results.
Douglas Gregor64b12b52009-09-22 23:31:26 +00001552 Results.EnterNewScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00001553 for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator
1554 NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end();
1555 NS != NSEnd; ++NS)
Douglas Gregor2af2f672009-09-21 20:12:40 +00001556 Results.MaybeAddResult(CodeCompleteConsumer::Result(NS->second, 0),
1557 CurContext);
Douglas Gregor64b12b52009-09-22 23:31:26 +00001558 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00001559 }
1560
Douglas Gregor9eb77012009-11-07 00:00:49 +00001561 if (CodeCompleter->includeMacros())
1562 AddMacroResults(PP, 1, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001563 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00001564}
1565
1566void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) {
1567 if (!CodeCompleter)
1568 return;
1569
Douglas Gregor3545ff42009-09-21 16:56:56 +00001570 // After "namespace", we expect to see a namespace or alias.
1571 ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001572 unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
1573 0, CurContext, Results);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001574 if (CodeCompleter->includeMacros())
1575 AddMacroResults(PP, NextRank, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001576 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00001577}
1578
Douglas Gregorc811ede2009-09-18 20:05:18 +00001579void Sema::CodeCompleteOperatorName(Scope *S) {
1580 if (!CodeCompleter)
1581 return;
Douglas Gregor3545ff42009-09-21 16:56:56 +00001582
1583 typedef CodeCompleteConsumer::Result Result;
1584 ResultBuilder Results(*this, &ResultBuilder::IsType);
Douglas Gregor64b12b52009-09-22 23:31:26 +00001585 Results.EnterNewScope();
Douglas Gregorc811ede2009-09-18 20:05:18 +00001586
Douglas Gregor3545ff42009-09-21 16:56:56 +00001587 // Add the names of overloadable operators.
1588#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
1589 if (std::strcmp(Spelling, "?")) \
1590 Results.MaybeAddResult(Result(Spelling, 0));
1591#include "clang/Basic/OperatorKinds.def"
1592
1593 // Add any type names visible from the current scope
1594 unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
Douglas Gregor2af2f672009-09-21 20:12:40 +00001595 0, CurContext, Results);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001596
1597 // Add any type specifiers
1598 AddTypeSpecifierResults(getLangOptions(), 0, Results);
1599
1600 // Add any nested-name-specifiers
1601 Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
Douglas Gregorf329c7c2009-10-30 16:50:04 +00001602 NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
1603 NextRank + 1, CurContext, Results);
Douglas Gregor64b12b52009-09-22 23:31:26 +00001604 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00001605
Douglas Gregor9eb77012009-11-07 00:00:49 +00001606 if (CodeCompleter->includeMacros())
1607 AddMacroResults(PP, NextRank, Results);
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001608 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Douglas Gregorc811ede2009-09-18 20:05:18 +00001609}
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00001610
Steve Naroff936354c2009-10-08 21:55:05 +00001611void Sema::CodeCompleteObjCProperty(Scope *S, ObjCDeclSpec &ODS) {
1612 if (!CodeCompleter)
1613 return;
1614 unsigned Attributes = ODS.getPropertyAttributes();
1615
1616 typedef CodeCompleteConsumer::Result Result;
1617 ResultBuilder Results(*this);
1618 Results.EnterNewScope();
1619 if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly))
1620 Results.MaybeAddResult(CodeCompleteConsumer::Result("readonly", 0));
1621 if (!(Attributes & ObjCDeclSpec::DQ_PR_assign))
1622 Results.MaybeAddResult(CodeCompleteConsumer::Result("assign", 0));
1623 if (!(Attributes & ObjCDeclSpec::DQ_PR_readwrite))
1624 Results.MaybeAddResult(CodeCompleteConsumer::Result("readwrite", 0));
1625 if (!(Attributes & ObjCDeclSpec::DQ_PR_retain))
1626 Results.MaybeAddResult(CodeCompleteConsumer::Result("retain", 0));
1627 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy))
1628 Results.MaybeAddResult(CodeCompleteConsumer::Result("copy", 0));
1629 if (!(Attributes & ObjCDeclSpec::DQ_PR_nonatomic))
1630 Results.MaybeAddResult(CodeCompleteConsumer::Result("nonatomic", 0));
1631 if (!(Attributes & ObjCDeclSpec::DQ_PR_setter))
1632 Results.MaybeAddResult(CodeCompleteConsumer::Result("setter", 0));
1633 if (!(Attributes & ObjCDeclSpec::DQ_PR_getter))
1634 Results.MaybeAddResult(CodeCompleteConsumer::Result("getter", 0));
1635 Results.ExitScope();
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001636 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Steve Naroff936354c2009-10-08 21:55:05 +00001637}
Steve Naroffeae65032009-11-07 02:08:14 +00001638
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00001639/// \brief Add all of the Objective-C methods in the given Objective-C
1640/// container to the set of results.
1641///
1642/// The container will be a class, protocol, category, or implementation of
1643/// any of the above. This mether will recurse to include methods from
1644/// the superclasses of classes along with their categories, protocols, and
1645/// implementations.
1646///
1647/// \param Container the container in which we'll look to find methods.
1648///
1649/// \param WantInstance whether to add instance methods (only); if false, this
1650/// routine will add factory methods (only).
1651///
1652/// \param CurContext the context in which we're performing the lookup that
1653/// finds methods.
1654///
1655/// \param Results the structure into which we'll add results.
1656static void AddObjCMethods(ObjCContainerDecl *Container,
1657 bool WantInstanceMethods,
1658 DeclContext *CurContext,
1659 ResultBuilder &Results) {
1660 typedef CodeCompleteConsumer::Result Result;
1661 for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
1662 MEnd = Container->meth_end();
1663 M != MEnd; ++M) {
1664 if ((*M)->isInstanceMethod() == WantInstanceMethods)
1665 Results.MaybeAddResult(Result(*M, 0), CurContext);
1666 }
1667
1668 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
1669 if (!IFace)
1670 return;
1671
1672 // Add methods in protocols.
1673 const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols();
1674 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1675 E = Protocols.end();
1676 I != E; ++I)
1677 AddObjCMethods(*I, WantInstanceMethods, CurContext, Results);
1678
1679 // Add methods in categories.
1680 for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl;
1681 CatDecl = CatDecl->getNextClassCategory()) {
1682 AddObjCMethods(CatDecl, WantInstanceMethods, CurContext, Results);
1683
1684 // Add a categories protocol methods.
1685 const ObjCList<ObjCProtocolDecl> &Protocols
1686 = CatDecl->getReferencedProtocols();
1687 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
1688 E = Protocols.end();
1689 I != E; ++I)
1690 AddObjCMethods(*I, WantInstanceMethods, CurContext, Results);
1691
1692 // Add methods in category implementations.
1693 if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
1694 AddObjCMethods(Impl, WantInstanceMethods, CurContext, Results);
1695 }
1696
1697 // Add methods in superclass.
1698 if (IFace->getSuperClass())
1699 AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, CurContext,
1700 Results);
1701
1702 // Add methods in our implementation, if any.
1703 if (ObjCImplementationDecl *Impl = IFace->getImplementation())
1704 AddObjCMethods(Impl, WantInstanceMethods, CurContext, Results);
1705}
1706
Douglas Gregor090dd182009-11-17 23:31:36 +00001707void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName,
1708 SourceLocation FNameLoc) {
Steve Naroffeae65032009-11-07 02:08:14 +00001709 typedef CodeCompleteConsumer::Result Result;
Douglas Gregor8ce33212009-11-17 17:59:40 +00001710 ObjCInterfaceDecl *CDecl = 0;
1711
Douglas Gregor8ce33212009-11-17 17:59:40 +00001712 if (FName->isStr("super")) {
1713 // We're sending a message to "super".
1714 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
1715 // Figure out which interface we're in.
1716 CDecl = CurMethod->getClassInterface();
1717 if (!CDecl)
1718 return;
1719
1720 // Find the superclass of this class.
1721 CDecl = CDecl->getSuperClass();
1722 if (!CDecl)
1723 return;
1724
1725 if (CurMethod->isInstanceMethod()) {
1726 // We are inside an instance method, which means that the message
1727 // send [super ...] is actually calling an instance method on the
1728 // current object. Build the super expression and handle this like
1729 // an instance method.
1730 QualType SuperTy = Context.getObjCInterfaceType(CDecl);
1731 SuperTy = Context.getObjCObjectPointerType(SuperTy);
1732 OwningExprResult Super
Douglas Gregor090dd182009-11-17 23:31:36 +00001733 = Owned(new (Context) ObjCSuperExpr(FNameLoc, SuperTy));
1734 return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get());
Douglas Gregor8ce33212009-11-17 17:59:40 +00001735 }
1736
1737 // Okay, we're calling a factory method in our superclass.
1738 }
1739 }
1740
1741 // If the given name refers to an interface type, retrieve the
1742 // corresponding declaration.
1743 if (!CDecl)
Douglas Gregor090dd182009-11-17 23:31:36 +00001744 if (TypeTy *Ty = getTypeName(*FName, FNameLoc, S, 0, false)) {
Douglas Gregor8ce33212009-11-17 17:59:40 +00001745 QualType T = GetTypeFromParser(Ty, 0);
1746 if (!T.isNull())
1747 if (const ObjCInterfaceType *Interface = T->getAs<ObjCInterfaceType>())
1748 CDecl = Interface->getDecl();
1749 }
1750
1751 if (!CDecl && FName->isStr("super")) {
1752 // "super" may be the name of a variable, in which case we are
1753 // probably calling an instance method.
Douglas Gregor090dd182009-11-17 23:31:36 +00001754 OwningExprResult Super = ActOnDeclarationNameExpr(S, FNameLoc, FName,
Douglas Gregor8ce33212009-11-17 17:59:40 +00001755 false, 0, false);
Douglas Gregor090dd182009-11-17 23:31:36 +00001756 return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get());
Douglas Gregor8ce33212009-11-17 17:59:40 +00001757 }
1758
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00001759 // Add all of the factory methods in this Objective-C class, its protocols,
1760 // superclasses, categories, implementation, etc.
Steve Naroffeae65032009-11-07 02:08:14 +00001761 ResultBuilder Results(*this);
1762 Results.EnterNewScope();
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00001763 AddObjCMethods(CDecl, false, CurContext, Results);
Steve Naroffeae65032009-11-07 02:08:14 +00001764 Results.ExitScope();
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00001765
Steve Naroffeae65032009-11-07 02:08:14 +00001766 // This also suppresses remaining diagnostics.
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001767 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Steve Naroffeae65032009-11-07 02:08:14 +00001768}
1769
Douglas Gregor090dd182009-11-17 23:31:36 +00001770void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver) {
Steve Naroffeae65032009-11-07 02:08:14 +00001771 typedef CodeCompleteConsumer::Result Result;
Steve Naroffeae65032009-11-07 02:08:14 +00001772
1773 Expr *RecExpr = static_cast<Expr *>(Receiver);
1774 QualType RecType = RecExpr->getType();
1775
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00001776 // If necessary, apply function/array conversion to the receiver.
1777 // C99 6.7.5.3p[7,8].
1778 DefaultFunctionArrayConversion(RecExpr);
1779 QualType ReceiverType = RecExpr->getType();
Steve Naroffeae65032009-11-07 02:08:14 +00001780
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00001781 if (ReceiverType->isObjCIdType() || ReceiverType->isBlockPointerType()) {
1782 // FIXME: We're messaging 'id'. Do we actually want to look up every method
1783 // in the universe?
1784 return;
1785 }
1786
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00001787 // Build the set of methods we can see.
1788 ResultBuilder Results(*this);
1789 Results.EnterNewScope();
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00001790
Douglas Gregora3329fa2009-11-18 00:06:18 +00001791 // Handle messages to Class. This really isn't a message to an instance
1792 // method, so we treat it the same way we would treat a message send to a
1793 // class method.
1794 if (ReceiverType->isObjCClassType() ||
1795 ReceiverType->isObjCQualifiedClassType()) {
1796 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
1797 if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
1798 AddObjCMethods(ClassDecl, false, CurContext, Results);
1799 }
1800 }
1801 // Handle messages to a qualified ID ("id<foo>").
1802 else if (const ObjCObjectPointerType *QualID
1803 = ReceiverType->getAsObjCQualifiedIdType()) {
1804 // Search protocols for instance methods.
1805 for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(),
1806 E = QualID->qual_end();
1807 I != E; ++I)
1808 AddObjCMethods(*I, true, CurContext, Results);
1809 }
1810 // Handle messages to a pointer to interface type.
1811 else if (const ObjCObjectPointerType *IFacePtr
1812 = ReceiverType->getAsObjCInterfacePointerType()) {
1813 // Search the class, its superclasses, etc., for instance methods.
1814 AddObjCMethods(IFacePtr->getInterfaceDecl(), true, CurContext, Results);
1815
1816 // Search protocols for instance methods.
1817 for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(),
1818 E = IFacePtr->qual_end();
1819 I != E; ++I)
1820 AddObjCMethods(*I, true, CurContext, Results);
1821 }
1822
Steve Naroffeae65032009-11-07 02:08:14 +00001823 Results.ExitScope();
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00001824 HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
Steve Naroffeae65032009-11-07 02:08:14 +00001825}