1 //===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the code-completion semantic actions.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "Sema.h"
14 #include "Lookup.h"
15 #include "clang/Sema/CodeCompleteConsumer.h"
16 #include "clang/Sema/ExternalSemaSource.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "clang/AST/ExprObjC.h"
19 #include "clang/Lex/MacroInfo.h"
20 #include "clang/Lex/Preprocessor.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/StringSwitch.h"
24 #include <list>
25 #include <map>
26 #include <vector>
27 
28 using namespace clang;
29 
30 namespace {
31   /// \brief A container of code-completion results.
32   class ResultBuilder {
33   public:
34     /// \brief The type of a name-lookup filter, which can be provided to the
35     /// name-lookup routines to specify which declarations should be included in
36     /// the result set (when it returns true) and which declarations should be
37     /// filtered out (returns false).
38     typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const;
39 
40     typedef CodeCompleteConsumer::Result Result;
41 
42   private:
43     /// \brief The actual results we have found.
44     std::vector<Result> Results;
45 
46     /// \brief A record of all of the declarations we have found and placed
47     /// into the result set, used to ensure that no declaration ever gets into
48     /// the result set twice.
49     llvm::SmallPtrSet<Decl*, 16> AllDeclsFound;
50 
51     typedef std::pair<NamedDecl *, unsigned> DeclIndexPair;
52 
53     /// \brief An entry in the shadow map, which is optimized to store
54     /// a single (declaration, index) mapping (the common case) but
55     /// can also store a list of (declaration, index) mappings.
56     class ShadowMapEntry {
57       typedef llvm::SmallVector<DeclIndexPair, 4> DeclIndexPairVector;
58 
59       /// \brief Contains either the solitary NamedDecl * or a vector
60       /// of (declaration, index) pairs.
61       llvm::PointerUnion<NamedDecl *, DeclIndexPairVector*> DeclOrVector;
62 
63       /// \brief When the entry contains a single declaration, this is
64       /// the index associated with that entry.
65       unsigned SingleDeclIndex;
66 
67     public:
68       ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { }
69 
70       void Add(NamedDecl *ND, unsigned Index) {
71         if (DeclOrVector.isNull()) {
72           // 0 - > 1 elements: just set the single element information.
73           DeclOrVector = ND;
74           SingleDeclIndex = Index;
75           return;
76         }
77 
78         if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) {
79           // 1 -> 2 elements: create the vector of results and push in the
80           // existing declaration.
81           DeclIndexPairVector *Vec = new DeclIndexPairVector;
82           Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex));
83           DeclOrVector = Vec;
84         }
85 
86         // Add the new element to the end of the vector.
87         DeclOrVector.get<DeclIndexPairVector*>()->push_back(
88                                                     DeclIndexPair(ND, Index));
89       }
90 
91       void Destroy() {
92         if (DeclIndexPairVector *Vec
93               = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) {
94           delete Vec;
95           DeclOrVector = ((NamedDecl *)0);
96         }
97       }
98 
99       // Iteration.
100       class iterator;
101       iterator begin() const;
102       iterator end() const;
103     };
104 
105     /// \brief A mapping from declaration names to the declarations that have
106     /// this name within a particular scope and their index within the list of
107     /// results.
108     typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
109 
110     /// \brief The semantic analysis object for which results are being
111     /// produced.
112     Sema &SemaRef;
113 
114     /// \brief If non-NULL, a filter function used to remove any code-completion
115     /// results that are not desirable.
116     LookupFilter Filter;
117 
118     /// \brief Whether we should allow declarations as
119     /// nested-name-specifiers that would otherwise be filtered out.
120     bool AllowNestedNameSpecifiers;
121 
122     /// \brief A list of shadow maps, which is used to model name hiding at
123     /// different levels of, e.g., the inheritance hierarchy.
124     std::list<ShadowMap> ShadowMaps;
125 
126   public:
127     explicit ResultBuilder(Sema &SemaRef, LookupFilter Filter = 0)
128       : SemaRef(SemaRef), Filter(Filter), AllowNestedNameSpecifiers(false) { }
129 
130     /// \brief Set the filter used for code-completion results.
131     void setFilter(LookupFilter Filter) {
132       this->Filter = Filter;
133     }
134 
135     typedef std::vector<Result>::iterator iterator;
136     iterator begin() { return Results.begin(); }
137     iterator end() { return Results.end(); }
138 
139     Result *data() { return Results.empty()? 0 : &Results.front(); }
140     unsigned size() const { return Results.size(); }
141     bool empty() const { return Results.empty(); }
142 
143     /// \brief Specify whether nested-name-specifiers are allowed.
144     void allowNestedNameSpecifiers(bool Allow = true) {
145       AllowNestedNameSpecifiers = Allow;
146     }
147 
148     /// \brief Determine whether the given declaration is at all interesting
149     /// as a code-completion result.
150     ///
151     /// \param ND the declaration that we are inspecting.
152     ///
153     /// \param AsNestedNameSpecifier will be set true if this declaration is
154     /// only interesting when it is a nested-name-specifier.
155     bool isInterestingDecl(NamedDecl *ND, bool &AsNestedNameSpecifier) const;
156 
157     /// \brief Check whether the result is hidden by the Hiding declaration.
158     ///
159     /// \returns true if the result is hidden and cannot be found, false if
160     /// the hidden result could still be found. When false, \p R may be
161     /// modified to describe how the result can be found (e.g., via extra
162     /// qualification).
163     bool CheckHiddenResult(Result &R, DeclContext *CurContext,
164                            NamedDecl *Hiding);
165 
166     /// \brief Add a new result to this result set (if it isn't already in one
167     /// of the shadow maps), or replace an existing result (for, e.g., a
168     /// redeclaration).
169     ///
170     /// \param CurContext the result to add (if it is unique).
171     ///
172     /// \param R the context in which this result will be named.
173     void MaybeAddResult(Result R, DeclContext *CurContext = 0);
174 
175     /// \brief Add a new result to this result set, where we already know
176     /// the hiding declation (if any).
177     ///
178     /// \param R the result to add (if it is unique).
179     ///
180     /// \param CurContext the context in which this result will be named.
181     ///
182     /// \param Hiding the declaration that hides the result.
183     ///
184     /// \param InBaseClass whether the result was found in a base
185     /// class of the searched context.
186     void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding,
187                    bool InBaseClass);
188 
189     /// \brief Add a new non-declaration result to this result set.
190     void AddResult(Result R);
191 
192     /// \brief Enter into a new scope.
193     void EnterNewScope();
194 
195     /// \brief Exit from the current scope.
196     void ExitScope();
197 
198     /// \brief Ignore this declaration, if it is seen again.
199     void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); }
200 
201     /// \name Name lookup predicates
202     ///
203     /// These predicates can be passed to the name lookup functions to filter the
204     /// results of name lookup. All of the predicates have the same type, so that
205     ///
206     //@{
207     bool IsOrdinaryName(NamedDecl *ND) const;
208     bool IsOrdinaryNonValueName(NamedDecl *ND) const;
209     bool IsNestedNameSpecifier(NamedDecl *ND) const;
210     bool IsEnum(NamedDecl *ND) const;
211     bool IsClassOrStruct(NamedDecl *ND) const;
212     bool IsUnion(NamedDecl *ND) const;
213     bool IsNamespace(NamedDecl *ND) const;
214     bool IsNamespaceOrAlias(NamedDecl *ND) const;
215     bool IsType(NamedDecl *ND) const;
216     bool IsMember(NamedDecl *ND) const;
217     bool IsObjCIvar(NamedDecl *ND) const;
218     //@}
219   };
220 }
221 
222 class ResultBuilder::ShadowMapEntry::iterator {
223   llvm::PointerUnion<NamedDecl*, const DeclIndexPair*> DeclOrIterator;
224   unsigned SingleDeclIndex;
225 
226 public:
227   typedef DeclIndexPair value_type;
228   typedef value_type reference;
229   typedef std::ptrdiff_t difference_type;
230   typedef std::input_iterator_tag iterator_category;
231 
232   class pointer {
233     DeclIndexPair Value;
234 
235   public:
236     pointer(const DeclIndexPair &Value) : Value(Value) { }
237 
238     const DeclIndexPair *operator->() const {
239       return &Value;
240     }
241   };
242 
243   iterator() : DeclOrIterator((NamedDecl *)0), SingleDeclIndex(0) { }
244 
245   iterator(NamedDecl *SingleDecl, unsigned Index)
246     : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { }
247 
248   iterator(const DeclIndexPair *Iterator)
249     : DeclOrIterator(Iterator), SingleDeclIndex(0) { }
250 
251   iterator &operator++() {
252     if (DeclOrIterator.is<NamedDecl *>()) {
253       DeclOrIterator = (NamedDecl *)0;
254       SingleDeclIndex = 0;
255       return *this;
256     }
257 
258     const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>();
259     ++I;
260     DeclOrIterator = I;
261     return *this;
262   }
263 
264   iterator operator++(int) {
265     iterator tmp(*this);
266     ++(*this);
267     return tmp;
268   }
269 
270   reference operator*() const {
271     if (NamedDecl *ND = DeclOrIterator.dyn_cast<NamedDecl *>())
272       return reference(ND, SingleDeclIndex);
273 
274     return *DeclOrIterator.get<const DeclIndexPair*>();
275   }
276 
277   pointer operator->() const {
278     return pointer(**this);
279   }
280 
281   friend bool operator==(const iterator &X, const iterator &Y) {
282     return X.DeclOrIterator.getOpaqueValue()
283                                   == Y.DeclOrIterator.getOpaqueValue() &&
284       X.SingleDeclIndex == Y.SingleDeclIndex;
285   }
286 
287   friend bool operator!=(const iterator &X, const iterator &Y) {
288     return !(X == Y);
289   }
290 };
291 
292 ResultBuilder::ShadowMapEntry::iterator
293 ResultBuilder::ShadowMapEntry::begin() const {
294   if (DeclOrVector.isNull())
295     return iterator();
296 
297   if (NamedDecl *ND = DeclOrVector.dyn_cast<NamedDecl *>())
298     return iterator(ND, SingleDeclIndex);
299 
300   return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin());
301 }
302 
303 ResultBuilder::ShadowMapEntry::iterator
304 ResultBuilder::ShadowMapEntry::end() const {
305   if (DeclOrVector.is<NamedDecl *>() || DeclOrVector.isNull())
306     return iterator();
307 
308   return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end());
309 }
310 
311 /// \brief Compute the qualification required to get from the current context
312 /// (\p CurContext) to the target context (\p TargetContext).
313 ///
314 /// \param Context the AST context in which the qualification will be used.
315 ///
316 /// \param CurContext the context where an entity is being named, which is
317 /// typically based on the current scope.
318 ///
319 /// \param TargetContext the context in which the named entity actually
320 /// resides.
321 ///
322 /// \returns a nested name specifier that refers into the target context, or
323 /// NULL if no qualification is needed.
324 static NestedNameSpecifier *
325 getRequiredQualification(ASTContext &Context,
326                          DeclContext *CurContext,
327                          DeclContext *TargetContext) {
328   llvm::SmallVector<DeclContext *, 4> TargetParents;
329 
330   for (DeclContext *CommonAncestor = TargetContext;
331        CommonAncestor && !CommonAncestor->Encloses(CurContext);
332        CommonAncestor = CommonAncestor->getLookupParent()) {
333     if (CommonAncestor->isTransparentContext() ||
334         CommonAncestor->isFunctionOrMethod())
335       continue;
336 
337     TargetParents.push_back(CommonAncestor);
338   }
339 
340   NestedNameSpecifier *Result = 0;
341   while (!TargetParents.empty()) {
342     DeclContext *Parent = TargetParents.back();
343     TargetParents.pop_back();
344 
345     if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent))
346       Result = NestedNameSpecifier::Create(Context, Result, Namespace);
347     else if (TagDecl *TD = dyn_cast<TagDecl>(Parent))
348       Result = NestedNameSpecifier::Create(Context, Result,
349                                            false,
350                                      Context.getTypeDeclType(TD).getTypePtr());
351     else
352       assert(Parent->isTranslationUnit());
353   }
354   return Result;
355 }
356 
357 bool ResultBuilder::isInterestingDecl(NamedDecl *ND,
358                                       bool &AsNestedNameSpecifier) const {
359   AsNestedNameSpecifier = false;
360 
361   ND = ND->getUnderlyingDecl();
362   unsigned IDNS = ND->getIdentifierNamespace();
363 
364   // Skip unnamed entities.
365   if (!ND->getDeclName())
366     return false;
367 
368   // Friend declarations and declarations introduced due to friends are never
369   // added as results.
370   if (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend))
371     return false;
372 
373   // Class template (partial) specializations are never added as results.
374   if (isa<ClassTemplateSpecializationDecl>(ND) ||
375       isa<ClassTemplatePartialSpecializationDecl>(ND))
376     return false;
377 
378   // Using declarations themselves are never added as results.
379   if (isa<UsingDecl>(ND))
380     return false;
381 
382   // Some declarations have reserved names that we don't want to ever show.
383   if (const IdentifierInfo *Id = ND->getIdentifier()) {
384     // __va_list_tag is a freak of nature. Find it and skip it.
385     if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list"))
386       return false;
387 
388     // Filter out names reserved for the implementation (C99 7.1.3,
389     // C++ [lib.global.names]). Users don't need to see those.
390     //
391     // FIXME: Add predicate for this.
392     if (Id->getLength() >= 2) {
393       const char *Name = Id->getNameStart();
394       if (Name[0] == '_' &&
395           (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')))
396         return false;
397     }
398   }
399 
400   // C++ constructors are never found by name lookup.
401   if (isa<CXXConstructorDecl>(ND))
402     return false;
403 
404   // Filter out any unwanted results.
405   if (Filter && !(this->*Filter)(ND)) {
406     // Check whether it is interesting as a nested-name-specifier.
407     if (AllowNestedNameSpecifiers && SemaRef.getLangOptions().CPlusPlus &&
408         IsNestedNameSpecifier(ND) &&
409         (Filter != &ResultBuilder::IsMember ||
410          (isa<CXXRecordDecl>(ND) &&
411           cast<CXXRecordDecl>(ND)->isInjectedClassName()))) {
412       AsNestedNameSpecifier = true;
413       return true;
414     }
415 
416     return false;
417   }
418 
419   if (Filter == &ResultBuilder::IsNestedNameSpecifier)
420     AsNestedNameSpecifier = true;
421 
422   // ... then it must be interesting!
423   return true;
424 }
425 
426 bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext,
427                                       NamedDecl *Hiding) {
428   // In C, there is no way to refer to a hidden name.
429   // FIXME: This isn't true; we can find a tag name hidden by an ordinary
430   // name if we introduce the tag type.
431   if (!SemaRef.getLangOptions().CPlusPlus)
432     return true;
433 
434   DeclContext *HiddenCtx = R.Declaration->getDeclContext()->getLookupContext();
435 
436   // There is no way to qualify a name declared in a function or method.
437   if (HiddenCtx->isFunctionOrMethod())
438     return true;
439 
440   if (HiddenCtx == Hiding->getDeclContext()->getLookupContext())
441     return true;
442 
443   // We can refer to the result with the appropriate qualification. Do it.
444   R.Hidden = true;
445   R.QualifierIsInformative = false;
446 
447   if (!R.Qualifier)
448     R.Qualifier = getRequiredQualification(SemaRef.Context,
449                                            CurContext,
450                                            R.Declaration->getDeclContext());
451   return false;
452 }
453 
454 void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) {
455   assert(!ShadowMaps.empty() && "Must enter into a results scope");
456 
457   if (R.Kind != Result::RK_Declaration) {
458     // For non-declaration results, just add the result.
459     Results.push_back(R);
460     return;
461   }
462 
463   // Look through using declarations.
464   if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
465     MaybeAddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext);
466     return;
467   }
468 
469   Decl *CanonDecl = R.Declaration->getCanonicalDecl();
470   unsigned IDNS = CanonDecl->getIdentifierNamespace();
471 
472   bool AsNestedNameSpecifier = false;
473   if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
474     return;
475 
476   ShadowMap &SMap = ShadowMaps.back();
477   ShadowMapEntry::iterator I, IEnd;
478   ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName());
479   if (NamePos != SMap.end()) {
480     I = NamePos->second.begin();
481     IEnd = NamePos->second.end();
482   }
483 
484   for (; I != IEnd; ++I) {
485     NamedDecl *ND = I->first;
486     unsigned Index = I->second;
487     if (ND->getCanonicalDecl() == CanonDecl) {
488       // This is a redeclaration. Always pick the newer declaration.
489       Results[Index].Declaration = R.Declaration;
490 
491       // We're done.
492       return;
493     }
494   }
495 
496   // This is a new declaration in this scope. However, check whether this
497   // declaration name is hidden by a similarly-named declaration in an outer
498   // scope.
499   std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end();
500   --SMEnd;
501   for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) {
502     ShadowMapEntry::iterator I, IEnd;
503     ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName());
504     if (NamePos != SM->end()) {
505       I = NamePos->second.begin();
506       IEnd = NamePos->second.end();
507     }
508     for (; I != IEnd; ++I) {
509       // A tag declaration does not hide a non-tag declaration.
510       if (I->first->hasTagIdentifierNamespace() &&
511           (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary |
512                    Decl::IDNS_ObjCProtocol)))
513         continue;
514 
515       // Protocols are in distinct namespaces from everything else.
516       if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol)
517            || (IDNS & Decl::IDNS_ObjCProtocol)) &&
518           I->first->getIdentifierNamespace() != IDNS)
519         continue;
520 
521       // The newly-added result is hidden by an entry in the shadow map.
522       if (CheckHiddenResult(R, CurContext, I->first))
523         return;
524 
525       break;
526     }
527   }
528 
529   // Make sure that any given declaration only shows up in the result set once.
530   if (!AllDeclsFound.insert(CanonDecl))
531     return;
532 
533   // If the filter is for nested-name-specifiers, then this result starts a
534   // nested-name-specifier.
535   if (AsNestedNameSpecifier)
536     R.StartsNestedNameSpecifier = true;
537 
538   // If this result is supposed to have an informative qualifier, add one.
539   if (R.QualifierIsInformative && !R.Qualifier &&
540       !R.StartsNestedNameSpecifier) {
541     DeclContext *Ctx = R.Declaration->getDeclContext();
542     if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
543       R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
544     else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
545       R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
546                              SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
547     else
548       R.QualifierIsInformative = false;
549   }
550 
551   // Insert this result into the set of results and into the current shadow
552   // map.
553   SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size());
554   Results.push_back(R);
555 }
556 
557 void ResultBuilder::AddResult(Result R, DeclContext *CurContext,
558                               NamedDecl *Hiding, bool InBaseClass = false) {
559   if (R.Kind != Result::RK_Declaration) {
560     // For non-declaration results, just add the result.
561     Results.push_back(R);
562     return;
563   }
564 
565   // Look through using declarations.
566   if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
567     AddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext, Hiding);
568     return;
569   }
570 
571   bool AsNestedNameSpecifier = false;
572   if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
573     return;
574 
575   if (Hiding && CheckHiddenResult(R, CurContext, Hiding))
576     return;
577 
578   // Make sure that any given declaration only shows up in the result set once.
579   if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()))
580     return;
581 
582   // If the filter is for nested-name-specifiers, then this result starts a
583   // nested-name-specifier.
584   if (AsNestedNameSpecifier)
585     R.StartsNestedNameSpecifier = true;
586   else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass &&
587            isa<CXXRecordDecl>(R.Declaration->getDeclContext()
588                                                   ->getLookupContext()))
589     R.QualifierIsInformative = true;
590 
591   // If this result is supposed to have an informative qualifier, add one.
592   if (R.QualifierIsInformative && !R.Qualifier &&
593       !R.StartsNestedNameSpecifier) {
594     DeclContext *Ctx = R.Declaration->getDeclContext();
595     if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
596       R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
597     else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
598       R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
599                             SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
600     else
601       R.QualifierIsInformative = false;
602   }
603 
604   // Insert this result into the set of results.
605   Results.push_back(R);
606 }
607 
608 void ResultBuilder::AddResult(Result R) {
609   assert(R.Kind != Result::RK_Declaration &&
610           "Declaration results need more context");
611   Results.push_back(R);
612 }
613 
614 /// \brief Enter into a new scope.
615 void ResultBuilder::EnterNewScope() {
616   ShadowMaps.push_back(ShadowMap());
617 }
618 
619 /// \brief Exit from the current scope.
620 void ResultBuilder::ExitScope() {
621   for (ShadowMap::iterator E = ShadowMaps.back().begin(),
622                         EEnd = ShadowMaps.back().end();
623        E != EEnd;
624        ++E)
625     E->second.Destroy();
626 
627   ShadowMaps.pop_back();
628 }
629 
630 /// \brief Determines whether this given declaration will be found by
631 /// ordinary name lookup.
632 bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const {
633   unsigned IDNS = Decl::IDNS_Ordinary;
634   if (SemaRef.getLangOptions().CPlusPlus)
635     IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace;
636   else if (SemaRef.getLangOptions().ObjC1 && isa<ObjCIvarDecl>(ND))
637     return true;
638 
639   return ND->getIdentifierNamespace() & IDNS;
640 }
641 
642 /// \brief Determines whether this given declaration will be found by
643 /// ordinary name lookup.
644 bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const {
645   unsigned IDNS = Decl::IDNS_Ordinary;
646   if (SemaRef.getLangOptions().CPlusPlus)
647     IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace;
648 
649   return (ND->getIdentifierNamespace() & IDNS) &&
650     !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND);
651 }
652 
653 /// \brief Determines whether the given declaration is suitable as the
654 /// start of a C++ nested-name-specifier, e.g., a class or namespace.
655 bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const {
656   // Allow us to find class templates, too.
657   if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
658     ND = ClassTemplate->getTemplatedDecl();
659 
660   return SemaRef.isAcceptableNestedNameSpecifier(ND);
661 }
662 
663 /// \brief Determines whether the given declaration is an enumeration.
664 bool ResultBuilder::IsEnum(NamedDecl *ND) const {
665   return isa<EnumDecl>(ND);
666 }
667 
668 /// \brief Determines whether the given declaration is a class or struct.
669 bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const {
670   // Allow us to find class templates, too.
671   if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
672     ND = ClassTemplate->getTemplatedDecl();
673 
674   if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
675     return RD->getTagKind() == TagDecl::TK_class ||
676     RD->getTagKind() == TagDecl::TK_struct;
677 
678   return false;
679 }
680 
681 /// \brief Determines whether the given declaration is a union.
682 bool ResultBuilder::IsUnion(NamedDecl *ND) const {
683   // Allow us to find class templates, too.
684   if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
685     ND = ClassTemplate->getTemplatedDecl();
686 
687   if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
688     return RD->getTagKind() == TagDecl::TK_union;
689 
690   return false;
691 }
692 
693 /// \brief Determines whether the given declaration is a namespace.
694 bool ResultBuilder::IsNamespace(NamedDecl *ND) const {
695   return isa<NamespaceDecl>(ND);
696 }
697 
698 /// \brief Determines whether the given declaration is a namespace or
699 /// namespace alias.
700 bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const {
701   return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
702 }
703 
704 /// \brief Determines whether the given declaration is a type.
705 bool ResultBuilder::IsType(NamedDecl *ND) const {
706   return isa<TypeDecl>(ND);
707 }
708 
709 /// \brief Determines which members of a class should be visible via
710 /// "." or "->".  Only value declarations, nested name specifiers, and
711 /// using declarations thereof should show up.
712 bool ResultBuilder::IsMember(NamedDecl *ND) const {
713   if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND))
714     ND = Using->getTargetDecl();
715 
716   return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
717     isa<ObjCPropertyDecl>(ND);
718 }
719 
720 /// \rief Determines whether the given declaration is an Objective-C
721 /// instance variable.
722 bool ResultBuilder::IsObjCIvar(NamedDecl *ND) const {
723   return isa<ObjCIvarDecl>(ND);
724 }
725 
726 namespace {
727   /// \brief Visible declaration consumer that adds a code-completion result
728   /// for each visible declaration.
729   class CodeCompletionDeclConsumer : public VisibleDeclConsumer {
730     ResultBuilder &Results;
731     DeclContext *CurContext;
732 
733   public:
734     CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext)
735       : Results(Results), CurContext(CurContext) { }
736 
737     virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, bool InBaseClass) {
738       Results.AddResult(ND, CurContext, Hiding, InBaseClass);
739     }
740   };
741 }
742 
743 /// \brief Add type specifiers for the current language as keyword results.
744 static void AddTypeSpecifierResults(const LangOptions &LangOpts,
745                                     ResultBuilder &Results) {
746   typedef CodeCompleteConsumer::Result Result;
747   Results.AddResult(Result("short"));
748   Results.AddResult(Result("long"));
749   Results.AddResult(Result("signed"));
750   Results.AddResult(Result("unsigned"));
751   Results.AddResult(Result("void"));
752   Results.AddResult(Result("char"));
753   Results.AddResult(Result("int"));
754   Results.AddResult(Result("float"));
755   Results.AddResult(Result("double"));
756   Results.AddResult(Result("enum"));
757   Results.AddResult(Result("struct"));
758   Results.AddResult(Result("union"));
759   Results.AddResult(Result("const"));
760   Results.AddResult(Result("volatile"));
761 
762   if (LangOpts.C99) {
763     // C99-specific
764     Results.AddResult(Result("_Complex"));
765     Results.AddResult(Result("_Imaginary"));
766     Results.AddResult(Result("_Bool"));
767     Results.AddResult(Result("restrict"));
768   }
769 
770   if (LangOpts.CPlusPlus) {
771     // C++-specific
772     Results.AddResult(Result("bool"));
773     Results.AddResult(Result("class"));
774     Results.AddResult(Result("wchar_t"));
775 
776     // typename qualified-id
777     CodeCompletionString *Pattern = new CodeCompletionString;
778     Pattern->AddTypedTextChunk("typename");
779     Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
780     Pattern->AddPlaceholderChunk("qualified-id");
781     Results.AddResult(Result(Pattern));
782 
783     if (LangOpts.CPlusPlus0x) {
784       Results.AddResult(Result("auto"));
785       Results.AddResult(Result("char16_t"));
786       Results.AddResult(Result("char32_t"));
787       Results.AddResult(Result("decltype"));
788     }
789   }
790 
791   // GNU extensions
792   if (LangOpts.GNUMode) {
793     // FIXME: Enable when we actually support decimal floating point.
794     //    Results.AddResult(Result("_Decimal32"));
795     //    Results.AddResult(Result("_Decimal64"));
796     //    Results.AddResult(Result("_Decimal128"));
797 
798     CodeCompletionString *Pattern = new CodeCompletionString;
799     Pattern->AddTypedTextChunk("typeof");
800     Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
801     Pattern->AddPlaceholderChunk("expression-or-type");
802     Pattern->AddChunk(CodeCompletionString::CK_RightParen);
803     Results.AddResult(Result(Pattern));
804   }
805 }
806 
807 static void AddStorageSpecifiers(Action::CodeCompletionContext CCC,
808                                  const LangOptions &LangOpts,
809                                  ResultBuilder &Results) {
810   typedef CodeCompleteConsumer::Result Result;
811   // Note: we don't suggest either "auto" or "register", because both
812   // are pointless as storage specifiers. Elsewhere, we suggest "auto"
813   // in C++0x as a type specifier.
814   Results.AddResult(Result("extern"));
815   Results.AddResult(Result("static"));
816 }
817 
818 static void AddFunctionSpecifiers(Action::CodeCompletionContext CCC,
819                                   const LangOptions &LangOpts,
820                                   ResultBuilder &Results) {
821   typedef CodeCompleteConsumer::Result Result;
822   switch (CCC) {
823   case Action::CCC_Class:
824   case Action::CCC_MemberTemplate:
825     if (LangOpts.CPlusPlus) {
826       Results.AddResult(Result("explicit"));
827       Results.AddResult(Result("friend"));
828       Results.AddResult(Result("mutable"));
829       Results.AddResult(Result("virtual"));
830     }
831     // Fall through
832 
833   case Action::CCC_ObjCInterface:
834   case Action::CCC_ObjCImplementation:
835   case Action::CCC_Namespace:
836   case Action::CCC_Template:
837     if (LangOpts.CPlusPlus || LangOpts.C99)
838       Results.AddResult(Result("inline"));
839     break;
840 
841   case Action::CCC_ObjCInstanceVariableList:
842   case Action::CCC_Expression:
843   case Action::CCC_Statement:
844   case Action::CCC_ForInit:
845   case Action::CCC_Condition:
846     break;
847   }
848 }
849 
850 static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt);
851 static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt);
852 static void AddObjCVisibilityResults(const LangOptions &LangOpts,
853                                      ResultBuilder &Results,
854                                      bool NeedAt);
855 static void AddObjCImplementationResults(const LangOptions &LangOpts,
856                                          ResultBuilder &Results,
857                                          bool NeedAt);
858 static void AddObjCInterfaceResults(const LangOptions &LangOpts,
859                                     ResultBuilder &Results,
860                                     bool NeedAt);
861 static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt);
862 
863 /// \brief Add language constructs that show up for "ordinary" names.
864 static void AddOrdinaryNameResults(Action::CodeCompletionContext CCC,
865                                    Scope *S,
866                                    Sema &SemaRef,
867                                    ResultBuilder &Results) {
868   typedef CodeCompleteConsumer::Result Result;
869   switch (CCC) {
870   case Action::CCC_Namespace:
871     if (SemaRef.getLangOptions().CPlusPlus) {
872       // namespace <identifier> { }
873       CodeCompletionString *Pattern = new CodeCompletionString;
874       Pattern->AddTypedTextChunk("namespace");
875       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
876       Pattern->AddPlaceholderChunk("identifier");
877       Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
878       Pattern->AddPlaceholderChunk("declarations");
879       Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
880       Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
881       Results.AddResult(Result(Pattern));
882 
883       // namespace identifier = identifier ;
884       Pattern = new CodeCompletionString;
885       Pattern->AddTypedTextChunk("namespace");
886       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
887       Pattern->AddPlaceholderChunk("identifier");
888       Pattern->AddChunk(CodeCompletionString::CK_Equal);
889       Pattern->AddPlaceholderChunk("identifier");
890       Results.AddResult(Result(Pattern));
891 
892       // Using directives
893       Pattern = new CodeCompletionString;
894       Pattern->AddTypedTextChunk("using");
895       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
896       Pattern->AddTextChunk("namespace");
897       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
898       Pattern->AddPlaceholderChunk("identifier");
899       Results.AddResult(Result(Pattern));
900 
901       // asm(string-literal)
902       Pattern = new CodeCompletionString;
903       Pattern->AddTypedTextChunk("asm");
904       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
905       Pattern->AddPlaceholderChunk("string-literal");
906       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
907       Results.AddResult(Result(Pattern));
908 
909       // Explicit template instantiation
910       Pattern = new CodeCompletionString;
911       Pattern->AddTypedTextChunk("template");
912       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
913       Pattern->AddPlaceholderChunk("declaration");
914       Results.AddResult(Result(Pattern));
915     }
916 
917     if (SemaRef.getLangOptions().ObjC1)
918       AddObjCTopLevelResults(Results, true);
919 
920     // Fall through
921 
922   case Action::CCC_Class:
923     Results.AddResult(Result("typedef"));
924     if (SemaRef.getLangOptions().CPlusPlus) {
925       // Using declaration
926       CodeCompletionString *Pattern = new CodeCompletionString;
927       Pattern->AddTypedTextChunk("using");
928       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
929       Pattern->AddPlaceholderChunk("qualified-id");
930       Results.AddResult(Result(Pattern));
931 
932       // using typename qualified-id; (only in a dependent context)
933       if (SemaRef.CurContext->isDependentContext()) {
934         Pattern = new CodeCompletionString;
935         Pattern->AddTypedTextChunk("using");
936         Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
937         Pattern->AddTextChunk("typename");
938         Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
939         Pattern->AddPlaceholderChunk("qualified-id");
940         Results.AddResult(Result(Pattern));
941       }
942 
943       if (CCC == Action::CCC_Class) {
944         // public:
945         Pattern = new CodeCompletionString;
946         Pattern->AddTypedTextChunk("public");
947         Pattern->AddChunk(CodeCompletionString::CK_Colon);
948         Results.AddResult(Result(Pattern));
949 
950         // protected:
951         Pattern = new CodeCompletionString;
952         Pattern->AddTypedTextChunk("protected");
953         Pattern->AddChunk(CodeCompletionString::CK_Colon);
954         Results.AddResult(Result(Pattern));
955 
956         // private:
957         Pattern = new CodeCompletionString;
958         Pattern->AddTypedTextChunk("private");
959         Pattern->AddChunk(CodeCompletionString::CK_Colon);
960         Results.AddResult(Result(Pattern));
961       }
962     }
963     // Fall through
964 
965   case Action::CCC_Template:
966   case Action::CCC_MemberTemplate:
967     if (SemaRef.getLangOptions().CPlusPlus) {
968       // template < parameters >
969       CodeCompletionString *Pattern = new CodeCompletionString;
970       Pattern->AddTypedTextChunk("template");
971       Pattern->AddChunk(CodeCompletionString::CK_LeftAngle);
972       Pattern->AddPlaceholderChunk("parameters");
973       Pattern->AddChunk(CodeCompletionString::CK_RightAngle);
974       Results.AddResult(Result(Pattern));
975     }
976 
977     AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results);
978     AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results);
979     break;
980 
981   case Action::CCC_ObjCInterface:
982     AddObjCInterfaceResults(SemaRef.getLangOptions(), Results, true);
983     AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results);
984     AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results);
985     break;
986 
987   case Action::CCC_ObjCImplementation:
988     AddObjCImplementationResults(SemaRef.getLangOptions(), Results, true);
989     AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results);
990     AddFunctionSpecifiers(CCC, SemaRef.getLangOptions(), Results);
991     break;
992 
993   case Action::CCC_ObjCInstanceVariableList:
994     AddObjCVisibilityResults(SemaRef.getLangOptions(), Results, true);
995     break;
996 
997   case Action::CCC_Statement: {
998     Results.AddResult(Result("typedef"));
999 
1000     CodeCompletionString *Pattern = 0;
1001     if (SemaRef.getLangOptions().CPlusPlus) {
1002       Pattern = new CodeCompletionString;
1003       Pattern->AddTypedTextChunk("try");
1004       Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1005       Pattern->AddPlaceholderChunk("statements");
1006       Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1007       Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
1008       Pattern->AddTextChunk("catch");
1009       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1010       Pattern->AddPlaceholderChunk("declaration");
1011       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1012       Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1013       Pattern->AddPlaceholderChunk("statements");
1014       Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1015       Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
1016       Results.AddResult(Result(Pattern));
1017     }
1018     if (SemaRef.getLangOptions().ObjC1)
1019       AddObjCStatementResults(Results, true);
1020 
1021     // if (condition) { statements }
1022     Pattern = new CodeCompletionString;
1023     Pattern->AddTypedTextChunk("if");
1024     Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1025     if (SemaRef.getLangOptions().CPlusPlus)
1026       Pattern->AddPlaceholderChunk("condition");
1027     else
1028       Pattern->AddPlaceholderChunk("expression");
1029     Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1030     Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1031     Pattern->AddPlaceholderChunk("statements");
1032     Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1033     Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
1034     Results.AddResult(Result(Pattern));
1035 
1036     // switch (condition) { }
1037     Pattern = new CodeCompletionString;
1038     Pattern->AddTypedTextChunk("switch");
1039     Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1040     if (SemaRef.getLangOptions().CPlusPlus)
1041       Pattern->AddPlaceholderChunk("condition");
1042     else
1043       Pattern->AddPlaceholderChunk("expression");
1044     Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1045     Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1046     Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1047     Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
1048     Results.AddResult(Result(Pattern));
1049 
1050     // Switch-specific statements.
1051     if (!SemaRef.getSwitchStack().empty()) {
1052       // case expression:
1053       Pattern = new CodeCompletionString;
1054       Pattern->AddTypedTextChunk("case");
1055       Pattern->AddPlaceholderChunk("expression");
1056       Pattern->AddChunk(CodeCompletionString::CK_Colon);
1057       Results.AddResult(Result(Pattern));
1058 
1059       // default:
1060       Pattern = new CodeCompletionString;
1061       Pattern->AddTypedTextChunk("default");
1062       Pattern->AddChunk(CodeCompletionString::CK_Colon);
1063       Results.AddResult(Result(Pattern));
1064     }
1065 
1066     /// while (condition) { statements }
1067     Pattern = new CodeCompletionString;
1068     Pattern->AddTypedTextChunk("while");
1069     Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1070     if (SemaRef.getLangOptions().CPlusPlus)
1071       Pattern->AddPlaceholderChunk("condition");
1072     else
1073       Pattern->AddPlaceholderChunk("expression");
1074     Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1075     Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1076     Pattern->AddPlaceholderChunk("statements");
1077     Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1078     Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
1079     Results.AddResult(Result(Pattern));
1080 
1081     // do { statements } while ( expression );
1082     Pattern = new CodeCompletionString;
1083     Pattern->AddTypedTextChunk("do");
1084     Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1085     Pattern->AddPlaceholderChunk("statements");
1086     Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1087     Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
1088     Pattern->AddTextChunk("while");
1089     Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1090     Pattern->AddPlaceholderChunk("expression");
1091     Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1092     Results.AddResult(Result(Pattern));
1093 
1094     // for ( for-init-statement ; condition ; expression ) { statements }
1095     Pattern = new CodeCompletionString;
1096     Pattern->AddTypedTextChunk("for");
1097     Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1098     if (SemaRef.getLangOptions().CPlusPlus || SemaRef.getLangOptions().C99)
1099       Pattern->AddPlaceholderChunk("init-statement");
1100     else
1101       Pattern->AddPlaceholderChunk("init-expression");
1102     Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
1103     Pattern->AddPlaceholderChunk("condition");
1104     Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
1105     Pattern->AddPlaceholderChunk("inc-expression");
1106     Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1107     Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
1108     Pattern->AddPlaceholderChunk("statements");
1109     Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
1110     Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
1111     Results.AddResult(Result(Pattern));
1112 
1113     if (S->getContinueParent()) {
1114       // continue ;
1115       Pattern = new CodeCompletionString;
1116       Pattern->AddTypedTextChunk("continue");
1117       Results.AddResult(Result(Pattern));
1118     }
1119 
1120     if (S->getBreakParent()) {
1121       // break ;
1122       Pattern = new CodeCompletionString;
1123       Pattern->AddTypedTextChunk("break");
1124       Results.AddResult(Result(Pattern));
1125     }
1126 
1127     // "return expression ;" or "return ;", depending on whether we
1128     // know the function is void or not.
1129     bool isVoid = false;
1130     if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext))
1131       isVoid = Function->getResultType()->isVoidType();
1132     else if (ObjCMethodDecl *Method
1133                                  = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext))
1134       isVoid = Method->getResultType()->isVoidType();
1135     else if (SemaRef.getCurBlock() &&
1136              !SemaRef.getCurBlock()->ReturnType.isNull())
1137       isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType();
1138     Pattern = new CodeCompletionString;
1139     Pattern->AddTypedTextChunk("return");
1140     if (!isVoid) {
1141       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1142       Pattern->AddPlaceholderChunk("expression");
1143     }
1144     Results.AddResult(Result(Pattern));
1145 
1146     // goto identifier ;
1147     Pattern = new CodeCompletionString;
1148     Pattern->AddTypedTextChunk("goto");
1149     Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1150     Pattern->AddPlaceholderChunk("identifier");
1151     Results.AddResult(Result(Pattern));
1152 
1153     // Using directives
1154     Pattern = new CodeCompletionString;
1155     Pattern->AddTypedTextChunk("using");
1156     Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1157     Pattern->AddTextChunk("namespace");
1158     Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1159     Pattern->AddPlaceholderChunk("identifier");
1160     Results.AddResult(Result(Pattern));
1161   }
1162 
1163   // Fall through (for statement expressions).
1164   case Action::CCC_ForInit:
1165   case Action::CCC_Condition:
1166     AddStorageSpecifiers(CCC, SemaRef.getLangOptions(), Results);
1167     // Fall through: conditions and statements can have expressions.
1168 
1169   case Action::CCC_Expression: {
1170     CodeCompletionString *Pattern = 0;
1171     if (SemaRef.getLangOptions().CPlusPlus) {
1172       // 'this', if we're in a non-static member function.
1173       if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(SemaRef.CurContext))
1174         if (!Method->isStatic())
1175           Results.AddResult(Result("this"));
1176 
1177       // true, false
1178       Results.AddResult(Result("true"));
1179       Results.AddResult(Result("false"));
1180 
1181       // dynamic_cast < type-id > ( expression )
1182       Pattern = new CodeCompletionString;
1183       Pattern->AddTypedTextChunk("dynamic_cast");
1184       Pattern->AddChunk(CodeCompletionString::CK_LeftAngle);
1185       Pattern->AddPlaceholderChunk("type-id");
1186       Pattern->AddChunk(CodeCompletionString::CK_RightAngle);
1187       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1188       Pattern->AddPlaceholderChunk("expression");
1189       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1190       Results.AddResult(Result(Pattern));
1191 
1192       // static_cast < type-id > ( expression )
1193       Pattern = new CodeCompletionString;
1194       Pattern->AddTypedTextChunk("static_cast");
1195       Pattern->AddChunk(CodeCompletionString::CK_LeftAngle);
1196       Pattern->AddPlaceholderChunk("type-id");
1197       Pattern->AddChunk(CodeCompletionString::CK_RightAngle);
1198       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1199       Pattern->AddPlaceholderChunk("expression");
1200       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1201       Results.AddResult(Result(Pattern));
1202 
1203       // reinterpret_cast < type-id > ( expression )
1204       Pattern = new CodeCompletionString;
1205       Pattern->AddTypedTextChunk("reinterpret_cast");
1206       Pattern->AddChunk(CodeCompletionString::CK_LeftAngle);
1207       Pattern->AddPlaceholderChunk("type-id");
1208       Pattern->AddChunk(CodeCompletionString::CK_RightAngle);
1209       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1210       Pattern->AddPlaceholderChunk("expression");
1211       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1212       Results.AddResult(Result(Pattern));
1213 
1214       // const_cast < type-id > ( expression )
1215       Pattern = new CodeCompletionString;
1216       Pattern->AddTypedTextChunk("const_cast");
1217       Pattern->AddChunk(CodeCompletionString::CK_LeftAngle);
1218       Pattern->AddPlaceholderChunk("type-id");
1219       Pattern->AddChunk(CodeCompletionString::CK_RightAngle);
1220       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1221       Pattern->AddPlaceholderChunk("expression");
1222       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1223       Results.AddResult(Result(Pattern));
1224 
1225       // typeid ( expression-or-type )
1226       Pattern = new CodeCompletionString;
1227       Pattern->AddTypedTextChunk("typeid");
1228       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1229       Pattern->AddPlaceholderChunk("expression-or-type");
1230       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1231       Results.AddResult(Result(Pattern));
1232 
1233       // new T ( ... )
1234       Pattern = new CodeCompletionString;
1235       Pattern->AddTypedTextChunk("new");
1236       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1237       Pattern->AddPlaceholderChunk("type-id");
1238       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1239       Pattern->AddPlaceholderChunk("expressions");
1240       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1241       Results.AddResult(Result(Pattern));
1242 
1243       // new T [ ] ( ... )
1244       Pattern = new CodeCompletionString;
1245       Pattern->AddTypedTextChunk("new");
1246       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1247       Pattern->AddPlaceholderChunk("type-id");
1248       Pattern->AddChunk(CodeCompletionString::CK_LeftBracket);
1249       Pattern->AddPlaceholderChunk("size");
1250       Pattern->AddChunk(CodeCompletionString::CK_RightBracket);
1251       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1252       Pattern->AddPlaceholderChunk("expressions");
1253       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1254       Results.AddResult(Result(Pattern));
1255 
1256       // delete expression
1257       Pattern = new CodeCompletionString;
1258       Pattern->AddTypedTextChunk("delete");
1259       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1260       Pattern->AddPlaceholderChunk("expression");
1261       Results.AddResult(Result(Pattern));
1262 
1263       // delete [] expression
1264       Pattern = new CodeCompletionString;
1265       Pattern->AddTypedTextChunk("delete");
1266       Pattern->AddChunk(CodeCompletionString::CK_LeftBracket);
1267       Pattern->AddChunk(CodeCompletionString::CK_RightBracket);
1268       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1269       Pattern->AddPlaceholderChunk("expression");
1270       Results.AddResult(Result(Pattern));
1271 
1272       // throw expression
1273       Pattern = new CodeCompletionString;
1274       Pattern->AddTypedTextChunk("throw");
1275       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1276       Pattern->AddPlaceholderChunk("expression");
1277       Results.AddResult(Result(Pattern));
1278     }
1279 
1280     if (SemaRef.getLangOptions().ObjC1) {
1281       // Add "super", if we're in an Objective-C class with a superclass.
1282       if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl())
1283         if (Method->getClassInterface()->getSuperClass())
1284           Results.AddResult(Result("super"));
1285 
1286       AddObjCExpressionResults(Results, true);
1287     }
1288 
1289     // sizeof expression
1290     Pattern = new CodeCompletionString;
1291     Pattern->AddTypedTextChunk("sizeof");
1292     Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
1293     Pattern->AddPlaceholderChunk("expression-or-type");
1294     Pattern->AddChunk(CodeCompletionString::CK_RightParen);
1295     Results.AddResult(Result(Pattern));
1296     break;
1297   }
1298   }
1299 
1300   AddTypeSpecifierResults(SemaRef.getLangOptions(), Results);
1301 
1302   if (SemaRef.getLangOptions().CPlusPlus)
1303     Results.AddResult(Result("operator"));
1304 }
1305 
1306 /// \brief If the given declaration has an associated type, add it as a result
1307 /// type chunk.
1308 static void AddResultTypeChunk(ASTContext &Context,
1309                                NamedDecl *ND,
1310                                CodeCompletionString *Result) {
1311   if (!ND)
1312     return;
1313 
1314   // Determine the type of the declaration (if it has a type).
1315   QualType T;
1316   if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND))
1317     T = Function->getResultType();
1318   else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
1319     T = Method->getResultType();
1320   else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND))
1321     T = FunTmpl->getTemplatedDecl()->getResultType();
1322   else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND))
1323     T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext()));
1324   else if (isa<UnresolvedUsingValueDecl>(ND)) {
1325     /* Do nothing: ignore unresolved using declarations*/
1326   } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND))
1327     T = Value->getType();
1328   else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND))
1329     T = Property->getType();
1330 
1331   if (T.isNull() || Context.hasSameType(T, Context.DependentTy))
1332     return;
1333 
1334   PrintingPolicy Policy(Context.PrintingPolicy);
1335   Policy.AnonymousTagLocations = false;
1336 
1337   std::string TypeStr;
1338   T.getAsStringInternal(TypeStr, Policy);
1339   Result->AddResultTypeChunk(TypeStr);
1340 }
1341 
1342 /// \brief Add function parameter chunks to the given code completion string.
1343 static void AddFunctionParameterChunks(ASTContext &Context,
1344                                        FunctionDecl *Function,
1345                                        CodeCompletionString *Result) {
1346   typedef CodeCompletionString::Chunk Chunk;
1347 
1348   CodeCompletionString *CCStr = Result;
1349 
1350   for (unsigned P = 0, N = Function->getNumParams(); P != N; ++P) {
1351     ParmVarDecl *Param = Function->getParamDecl(P);
1352 
1353     if (Param->hasDefaultArg()) {
1354       // When we see an optional default argument, put that argument and
1355       // the remaining default arguments into a new, optional string.
1356       CodeCompletionString *Opt = new CodeCompletionString;
1357       CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt));
1358       CCStr = Opt;
1359     }
1360 
1361     if (P != 0)
1362       CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma));
1363 
1364     // Format the placeholder string.
1365     std::string PlaceholderStr;
1366     if (Param->getIdentifier())
1367       PlaceholderStr = Param->getIdentifier()->getName();
1368 
1369     Param->getType().getAsStringInternal(PlaceholderStr,
1370                                          Context.PrintingPolicy);
1371 
1372     // Add the placeholder string.
1373     CCStr->AddPlaceholderChunk(PlaceholderStr);
1374   }
1375 
1376   if (const FunctionProtoType *Proto
1377         = Function->getType()->getAs<FunctionProtoType>())
1378     if (Proto->isVariadic())
1379       CCStr->AddPlaceholderChunk(", ...");
1380 }
1381 
1382 /// \brief Add template parameter chunks to the given code completion string.
1383 static void AddTemplateParameterChunks(ASTContext &Context,
1384                                        TemplateDecl *Template,
1385                                        CodeCompletionString *Result,
1386                                        unsigned MaxParameters = 0) {
1387   typedef CodeCompletionString::Chunk Chunk;
1388 
1389   CodeCompletionString *CCStr = Result;
1390   bool FirstParameter = true;
1391 
1392   TemplateParameterList *Params = Template->getTemplateParameters();
1393   TemplateParameterList::iterator PEnd = Params->end();
1394   if (MaxParameters)
1395     PEnd = Params->begin() + MaxParameters;
1396   for (TemplateParameterList::iterator P = Params->begin(); P != PEnd; ++P) {
1397     bool HasDefaultArg = false;
1398     std::string PlaceholderStr;
1399     if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
1400       if (TTP->wasDeclaredWithTypename())
1401         PlaceholderStr = "typename";
1402       else
1403         PlaceholderStr = "class";
1404 
1405       if (TTP->getIdentifier()) {
1406         PlaceholderStr += ' ';
1407         PlaceholderStr += TTP->getIdentifier()->getName();
1408       }
1409 
1410       HasDefaultArg = TTP->hasDefaultArgument();
1411     } else if (NonTypeTemplateParmDecl *NTTP
1412                = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1413       if (NTTP->getIdentifier())
1414         PlaceholderStr = NTTP->getIdentifier()->getName();
1415       NTTP->getType().getAsStringInternal(PlaceholderStr,
1416                                           Context.PrintingPolicy);
1417       HasDefaultArg = NTTP->hasDefaultArgument();
1418     } else {
1419       assert(isa<TemplateTemplateParmDecl>(*P));
1420       TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
1421 
1422       // Since putting the template argument list into the placeholder would
1423       // be very, very long, we just use an abbreviation.
1424       PlaceholderStr = "template<...> class";
1425       if (TTP->getIdentifier()) {
1426         PlaceholderStr += ' ';
1427         PlaceholderStr += TTP->getIdentifier()->getName();
1428       }
1429 
1430       HasDefaultArg = TTP->hasDefaultArgument();
1431     }
1432 
1433     if (HasDefaultArg) {
1434       // When we see an optional default argument, put that argument and
1435       // the remaining default arguments into a new, optional string.
1436       CodeCompletionString *Opt = new CodeCompletionString;
1437       CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt));
1438       CCStr = Opt;
1439     }
1440 
1441     if (FirstParameter)
1442       FirstParameter = false;
1443     else
1444       CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma));
1445 
1446     // Add the placeholder string.
1447     CCStr->AddPlaceholderChunk(PlaceholderStr);
1448   }
1449 }
1450 
1451 /// \brief Add a qualifier to the given code-completion string, if the
1452 /// provided nested-name-specifier is non-NULL.
1453 static void
1454 AddQualifierToCompletionString(CodeCompletionString *Result,
1455                                NestedNameSpecifier *Qualifier,
1456                                bool QualifierIsInformative,
1457                                ASTContext &Context) {
1458   if (!Qualifier)
1459     return;
1460 
1461   std::string PrintedNNS;
1462   {
1463     llvm::raw_string_ostream OS(PrintedNNS);
1464     Qualifier->print(OS, Context.PrintingPolicy);
1465   }
1466   if (QualifierIsInformative)
1467     Result->AddInformativeChunk(PrintedNNS);
1468   else
1469     Result->AddTextChunk(PrintedNNS);
1470 }
1471 
1472 static void AddFunctionTypeQualsToCompletionString(CodeCompletionString *Result,
1473                                                    FunctionDecl *Function) {
1474   const FunctionProtoType *Proto
1475     = Function->getType()->getAs<FunctionProtoType>();
1476   if (!Proto || !Proto->getTypeQuals())
1477     return;
1478 
1479   std::string QualsStr;
1480   if (Proto->getTypeQuals() & Qualifiers::Const)
1481     QualsStr += " const";
1482   if (Proto->getTypeQuals() & Qualifiers::Volatile)
1483     QualsStr += " volatile";
1484   if (Proto->getTypeQuals() & Qualifiers::Restrict)
1485     QualsStr += " restrict";
1486   Result->AddInformativeChunk(QualsStr);
1487 }
1488 
1489 /// \brief If possible, create a new code completion string for the given
1490 /// result.
1491 ///
1492 /// \returns Either a new, heap-allocated code completion string describing
1493 /// how to use this result, or NULL to indicate that the string or name of the
1494 /// result is all that is needed.
1495 CodeCompletionString *
1496 CodeCompleteConsumer::Result::CreateCodeCompletionString(Sema &S) {
1497   typedef CodeCompletionString::Chunk Chunk;
1498 
1499   if (Kind == RK_Pattern)
1500     return Pattern->Clone();
1501 
1502   CodeCompletionString *Result = new CodeCompletionString;
1503 
1504   if (Kind == RK_Keyword) {
1505     Result->AddTypedTextChunk(Keyword);
1506     return Result;
1507   }
1508 
1509   if (Kind == RK_Macro) {
1510     MacroInfo *MI = S.PP.getMacroInfo(Macro);
1511     assert(MI && "Not a macro?");
1512 
1513     Result->AddTypedTextChunk(Macro->getName());
1514 
1515     if (!MI->isFunctionLike())
1516       return Result;
1517 
1518     // Format a function-like macro with placeholders for the arguments.
1519     Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
1520     for (MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end();
1521          A != AEnd; ++A) {
1522       if (A != MI->arg_begin())
1523         Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
1524 
1525       if (!MI->isVariadic() || A != AEnd - 1) {
1526         // Non-variadic argument.
1527         Result->AddPlaceholderChunk((*A)->getName());
1528         continue;
1529       }
1530 
1531       // Variadic argument; cope with the different between GNU and C99
1532       // variadic macros, providing a single placeholder for the rest of the
1533       // arguments.
1534       if ((*A)->isStr("__VA_ARGS__"))
1535         Result->AddPlaceholderChunk("...");
1536       else {
1537         std::string Arg = (*A)->getName();
1538         Arg += "...";
1539         Result->AddPlaceholderChunk(Arg);
1540       }
1541     }
1542     Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
1543     return Result;
1544   }
1545 
1546   assert(Kind == RK_Declaration && "Missed a macro kind?");
1547   NamedDecl *ND = Declaration;
1548 
1549   if (StartsNestedNameSpecifier) {
1550     Result->AddTypedTextChunk(ND->getNameAsString());
1551     Result->AddTextChunk("::");
1552     return Result;
1553   }
1554 
1555   AddResultTypeChunk(S.Context, ND, Result);
1556 
1557   if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
1558     AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
1559                                    S.Context);
1560     Result->AddTypedTextChunk(Function->getNameAsString());
1561     Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
1562     AddFunctionParameterChunks(S.Context, Function, Result);
1563     Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
1564     AddFunctionTypeQualsToCompletionString(Result, Function);
1565     return Result;
1566   }
1567 
1568   if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
1569     AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
1570                                    S.Context);
1571     FunctionDecl *Function = FunTmpl->getTemplatedDecl();
1572     Result->AddTypedTextChunk(Function->getNameAsString());
1573 
1574     // Figure out which template parameters are deduced (or have default
1575     // arguments).
1576     llvm::SmallVector<bool, 16> Deduced;
1577     S.MarkDeducedTemplateParameters(FunTmpl, Deduced);
1578     unsigned LastDeducibleArgument;
1579     for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0;
1580          --LastDeducibleArgument) {
1581       if (!Deduced[LastDeducibleArgument - 1]) {
1582         // C++0x: Figure out if the template argument has a default. If so,
1583         // the user doesn't need to type this argument.
1584         // FIXME: We need to abstract template parameters better!
1585         bool HasDefaultArg = false;
1586         NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam(
1587                                                                       LastDeducibleArgument - 1);
1588         if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
1589           HasDefaultArg = TTP->hasDefaultArgument();
1590         else if (NonTypeTemplateParmDecl *NTTP
1591                  = dyn_cast<NonTypeTemplateParmDecl>(Param))
1592           HasDefaultArg = NTTP->hasDefaultArgument();
1593         else {
1594           assert(isa<TemplateTemplateParmDecl>(Param));
1595           HasDefaultArg
1596             = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument();
1597         }
1598 
1599         if (!HasDefaultArg)
1600           break;
1601       }
1602     }
1603 
1604     if (LastDeducibleArgument) {
1605       // Some of the function template arguments cannot be deduced from a
1606       // function call, so we introduce an explicit template argument list
1607       // containing all of the arguments up to the first deducible argument.
1608       Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle));
1609       AddTemplateParameterChunks(S.Context, FunTmpl, Result,
1610                                  LastDeducibleArgument);
1611       Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle));
1612     }
1613 
1614     // Add the function parameters
1615     Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
1616     AddFunctionParameterChunks(S.Context, Function, Result);
1617     Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
1618     AddFunctionTypeQualsToCompletionString(Result, Function);
1619     return Result;
1620   }
1621 
1622   if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) {
1623     AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
1624                                    S.Context);
1625     Result->AddTypedTextChunk(Template->getNameAsString());
1626     Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle));
1627     AddTemplateParameterChunks(S.Context, Template, Result);
1628     Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle));
1629     return Result;
1630   }
1631 
1632   if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) {
1633     Selector Sel = Method->getSelector();
1634     if (Sel.isUnarySelector()) {
1635       Result->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName());
1636       return Result;
1637     }
1638 
1639     std::string SelName = Sel.getIdentifierInfoForSlot(0)->getName().str();
1640     SelName += ':';
1641     if (StartParameter == 0)
1642       Result->AddTypedTextChunk(SelName);
1643     else {
1644       Result->AddInformativeChunk(SelName);
1645 
1646       // If there is only one parameter, and we're past it, add an empty
1647       // typed-text chunk since there is nothing to type.
1648       if (Method->param_size() == 1)
1649         Result->AddTypedTextChunk("");
1650     }
1651     unsigned Idx = 0;
1652     for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
1653                                      PEnd = Method->param_end();
1654          P != PEnd; (void)++P, ++Idx) {
1655       if (Idx > 0) {
1656         std::string Keyword;
1657         if (Idx > StartParameter)
1658           Result->AddChunk(CodeCompletionString::CK_HorizontalSpace);
1659         if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx))
1660           Keyword += II->getName().str();
1661         Keyword += ":";
1662         if (Idx < StartParameter || AllParametersAreInformative) {
1663           Result->AddInformativeChunk(Keyword);
1664         } else if (Idx == StartParameter)
1665           Result->AddTypedTextChunk(Keyword);
1666         else
1667           Result->AddTextChunk(Keyword);
1668       }
1669 
1670       // If we're before the starting parameter, skip the placeholder.
1671       if (Idx < StartParameter)
1672         continue;
1673 
1674       std::string Arg;
1675       (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy);
1676       Arg = "(" + Arg + ")";
1677       if (IdentifierInfo *II = (*P)->getIdentifier())
1678         Arg += II->getName().str();
1679       if (AllParametersAreInformative)
1680         Result->AddInformativeChunk(Arg);
1681       else
1682         Result->AddPlaceholderChunk(Arg);
1683     }
1684 
1685     if (Method->isVariadic()) {
1686       if (AllParametersAreInformative)
1687         Result->AddInformativeChunk(", ...");
1688       else
1689         Result->AddPlaceholderChunk(", ...");
1690     }
1691 
1692     return Result;
1693   }
1694 
1695   if (Qualifier)
1696     AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
1697                                    S.Context);
1698 
1699   Result->AddTypedTextChunk(ND->getNameAsString());
1700   return Result;
1701 }
1702 
1703 CodeCompletionString *
1704 CodeCompleteConsumer::OverloadCandidate::CreateSignatureString(
1705                                                           unsigned CurrentArg,
1706                                                                Sema &S) const {
1707   typedef CodeCompletionString::Chunk Chunk;
1708 
1709   CodeCompletionString *Result = new CodeCompletionString;
1710   FunctionDecl *FDecl = getFunction();
1711   AddResultTypeChunk(S.Context, FDecl, Result);
1712   const FunctionProtoType *Proto
1713     = dyn_cast<FunctionProtoType>(getFunctionType());
1714   if (!FDecl && !Proto) {
1715     // Function without a prototype. Just give the return type and a
1716     // highlighted ellipsis.
1717     const FunctionType *FT = getFunctionType();
1718     Result->AddTextChunk(
1719             FT->getResultType().getAsString(S.Context.PrintingPolicy));
1720     Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
1721     Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "..."));
1722     Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
1723     return Result;
1724   }
1725 
1726   if (FDecl)
1727     Result->AddTextChunk(FDecl->getNameAsString());
1728   else
1729     Result->AddTextChunk(
1730          Proto->getResultType().getAsString(S.Context.PrintingPolicy));
1731 
1732   Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
1733   unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs();
1734   for (unsigned I = 0; I != NumParams; ++I) {
1735     if (I)
1736       Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
1737 
1738     std::string ArgString;
1739     QualType ArgType;
1740 
1741     if (FDecl) {
1742       ArgString = FDecl->getParamDecl(I)->getNameAsString();
1743       ArgType = FDecl->getParamDecl(I)->getOriginalType();
1744     } else {
1745       ArgType = Proto->getArgType(I);
1746     }
1747 
1748     ArgType.getAsStringInternal(ArgString, S.Context.PrintingPolicy);
1749 
1750     if (I == CurrentArg)
1751       Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter,
1752                              ArgString));
1753     else
1754       Result->AddTextChunk(ArgString);
1755   }
1756 
1757   if (Proto && Proto->isVariadic()) {
1758     Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
1759     if (CurrentArg < NumParams)
1760       Result->AddTextChunk("...");
1761     else
1762       Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "..."));
1763   }
1764   Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
1765 
1766   return Result;
1767 }
1768 
1769 namespace {
1770   struct SortCodeCompleteResult {
1771     typedef CodeCompleteConsumer::Result Result;
1772 
1773     bool isEarlierDeclarationName(DeclarationName X, DeclarationName Y) const {
1774       Selector XSel = X.getObjCSelector();
1775       Selector YSel = Y.getObjCSelector();
1776       if (!XSel.isNull() && !YSel.isNull()) {
1777         // We are comparing two selectors.
1778         unsigned N = std::min(XSel.getNumArgs(), YSel.getNumArgs());
1779         if (N == 0)
1780           ++N;
1781         for (unsigned I = 0; I != N; ++I) {
1782           IdentifierInfo *XId = XSel.getIdentifierInfoForSlot(I);
1783           IdentifierInfo *YId = YSel.getIdentifierInfoForSlot(I);
1784           if (!XId || !YId)
1785             return XId && !YId;
1786 
1787           switch (XId->getName().compare_lower(YId->getName())) {
1788           case -1: return true;
1789           case 1: return false;
1790           default: break;
1791           }
1792         }
1793 
1794         return XSel.getNumArgs() < YSel.getNumArgs();
1795       }
1796 
1797       // For non-selectors, order by kind.
1798       if (X.getNameKind() != Y.getNameKind())
1799         return X.getNameKind() < Y.getNameKind();
1800 
1801       // Order identifiers by comparison of their lowercased names.
1802       if (IdentifierInfo *XId = X.getAsIdentifierInfo())
1803         return XId->getName().compare_lower(
1804                                      Y.getAsIdentifierInfo()->getName()) < 0;
1805 
1806       // Order overloaded operators by the order in which they appear
1807       // in our list of operators.
1808       if (OverloadedOperatorKind XOp = X.getCXXOverloadedOperator())
1809         return XOp < Y.getCXXOverloadedOperator();
1810 
1811       // Order C++0x user-defined literal operators lexically by their
1812       // lowercased suffixes.
1813       if (IdentifierInfo *XLit = X.getCXXLiteralIdentifier())
1814         return XLit->getName().compare_lower(
1815                                   Y.getCXXLiteralIdentifier()->getName()) < 0;
1816 
1817       // The only stable ordering we have is to turn the name into a
1818       // string and then compare the lower-case strings. This is
1819       // inefficient, but thankfully does not happen too often.
1820       return llvm::StringRef(X.getAsString()).compare_lower(
1821                                                  Y.getAsString()) < 0;
1822     }
1823 
1824     /// \brief Retrieve the name that should be used to order a result.
1825     ///
1826     /// If the name needs to be constructed as a string, that string will be
1827     /// saved into Saved and the returned StringRef will refer to it.
1828     static llvm::StringRef getOrderedName(const Result &R,
1829                                           std::string &Saved) {
1830       switch (R.Kind) {
1831       case Result::RK_Keyword:
1832         return R.Keyword;
1833 
1834       case Result::RK_Pattern:
1835         return R.Pattern->getTypedText();
1836 
1837       case Result::RK_Macro:
1838         return R.Macro->getName();
1839 
1840       case Result::RK_Declaration:
1841         // Handle declarations below.
1842         break;
1843       }
1844 
1845       DeclarationName Name = R.Declaration->getDeclName();
1846 
1847       // If the name is a simple identifier (by far the common case), or a
1848       // zero-argument selector, just return a reference to that identifier.
1849       if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
1850         return Id->getName();
1851       if (Name.isObjCZeroArgSelector())
1852         if (IdentifierInfo *Id
1853                           = Name.getObjCSelector().getIdentifierInfoForSlot(0))
1854           return Id->getName();
1855 
1856       Saved = Name.getAsString();
1857       return Saved;
1858     }
1859 
1860     bool operator()(const Result &X, const Result &Y) const {
1861       std::string XSaved, YSaved;
1862       llvm::StringRef XStr = getOrderedName(X, XSaved);
1863       llvm::StringRef YStr = getOrderedName(Y, YSaved);
1864       int cmp = XStr.compare_lower(YStr);
1865       if (cmp)
1866         return cmp < 0;
1867 
1868       // Non-hidden names precede hidden names.
1869       if (X.Hidden != Y.Hidden)
1870         return !X.Hidden;
1871 
1872       // Non-nested-name-specifiers precede nested-name-specifiers.
1873       if (X.StartsNestedNameSpecifier != Y.StartsNestedNameSpecifier)
1874         return !X.StartsNestedNameSpecifier;
1875 
1876       return false;
1877     }
1878   };
1879 }
1880 
1881 static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results) {
1882   Results.EnterNewScope();
1883   for (Preprocessor::macro_iterator M = PP.macro_begin(),
1884                                  MEnd = PP.macro_end();
1885        M != MEnd; ++M)
1886     Results.AddResult(M->first);
1887   Results.ExitScope();
1888 }
1889 
1890 static void HandleCodeCompleteResults(Sema *S,
1891                                       CodeCompleteConsumer *CodeCompleter,
1892                                      CodeCompleteConsumer::Result *Results,
1893                                      unsigned NumResults) {
1894   std::stable_sort(Results, Results + NumResults, SortCodeCompleteResult());
1895 
1896   if (CodeCompleter)
1897     CodeCompleter->ProcessCodeCompleteResults(*S, Results, NumResults);
1898 
1899   for (unsigned I = 0; I != NumResults; ++I)
1900     Results[I].Destroy();
1901 }
1902 
1903 void Sema::CodeCompleteOrdinaryName(Scope *S,
1904                                     CodeCompletionContext CompletionContext) {
1905   typedef CodeCompleteConsumer::Result Result;
1906   ResultBuilder Results(*this);
1907 
1908   // Determine how to filter results, e.g., so that the names of
1909   // values (functions, enumerators, function templates, etc.) are
1910   // only allowed where we can have an expression.
1911   switch (CompletionContext) {
1912   case CCC_Namespace:
1913   case CCC_Class:
1914   case CCC_ObjCInterface:
1915   case CCC_ObjCImplementation:
1916   case CCC_ObjCInstanceVariableList:
1917   case CCC_Template:
1918   case CCC_MemberTemplate:
1919     Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
1920     break;
1921 
1922   case CCC_Expression:
1923   case CCC_Statement:
1924   case CCC_ForInit:
1925   case CCC_Condition:
1926     Results.setFilter(&ResultBuilder::IsOrdinaryName);
1927     break;
1928   }
1929 
1930   CodeCompletionDeclConsumer Consumer(Results, CurContext);
1931   LookupVisibleDecls(S, LookupOrdinaryName, Consumer);
1932 
1933   Results.EnterNewScope();
1934   AddOrdinaryNameResults(CompletionContext, S, *this, Results);
1935   Results.ExitScope();
1936 
1937   if (CodeCompleter->includeMacros())
1938     AddMacroResults(PP, Results);
1939   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
1940 }
1941 
1942 static void AddObjCProperties(ObjCContainerDecl *Container,
1943                               bool AllowCategories,
1944                               DeclContext *CurContext,
1945                               ResultBuilder &Results) {
1946   typedef CodeCompleteConsumer::Result Result;
1947 
1948   // Add properties in this container.
1949   for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(),
1950                                      PEnd = Container->prop_end();
1951        P != PEnd;
1952        ++P)
1953     Results.MaybeAddResult(Result(*P, 0), CurContext);
1954 
1955   // Add properties in referenced protocols.
1956   if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
1957     for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
1958                                           PEnd = Protocol->protocol_end();
1959          P != PEnd; ++P)
1960       AddObjCProperties(*P, AllowCategories, CurContext, Results);
1961   } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){
1962     if (AllowCategories) {
1963       // Look through categories.
1964       for (ObjCCategoryDecl *Category = IFace->getCategoryList();
1965            Category; Category = Category->getNextClassCategory())
1966         AddObjCProperties(Category, AllowCategories, CurContext, Results);
1967     }
1968 
1969     // Look through protocols.
1970     for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(),
1971                                               E = IFace->protocol_end();
1972          I != E; ++I)
1973       AddObjCProperties(*I, AllowCategories, CurContext, Results);
1974 
1975     // Look in the superclass.
1976     if (IFace->getSuperClass())
1977       AddObjCProperties(IFace->getSuperClass(), AllowCategories, CurContext,
1978                         Results);
1979   } else if (const ObjCCategoryDecl *Category
1980                                     = dyn_cast<ObjCCategoryDecl>(Container)) {
1981     // Look through protocols.
1982     for (ObjCInterfaceDecl::protocol_iterator P = Category->protocol_begin(),
1983                                            PEnd = Category->protocol_end();
1984          P != PEnd; ++P)
1985       AddObjCProperties(*P, AllowCategories, CurContext, Results);
1986   }
1987 }
1988 
1989 void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE,
1990                                            SourceLocation OpLoc,
1991                                            bool IsArrow) {
1992   if (!BaseE || !CodeCompleter)
1993     return;
1994 
1995   typedef CodeCompleteConsumer::Result Result;
1996 
1997   Expr *Base = static_cast<Expr *>(BaseE);
1998   QualType BaseType = Base->getType();
1999 
2000   if (IsArrow) {
2001     if (const PointerType *Ptr = BaseType->getAs<PointerType>())
2002       BaseType = Ptr->getPointeeType();
2003     else if (BaseType->isObjCObjectPointerType())
2004     /*Do nothing*/ ;
2005     else
2006       return;
2007   }
2008 
2009   ResultBuilder Results(*this, &ResultBuilder::IsMember);
2010   Results.EnterNewScope();
2011   if (const RecordType *Record = BaseType->getAs<RecordType>()) {
2012     // Access to a C/C++ class, struct, or union.
2013     Results.allowNestedNameSpecifiers();
2014     CodeCompletionDeclConsumer Consumer(Results, CurContext);
2015     LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer);
2016 
2017     if (getLangOptions().CPlusPlus) {
2018       if (!Results.empty()) {
2019         // The "template" keyword can follow "->" or "." in the grammar.
2020         // However, we only want to suggest the template keyword if something
2021         // is dependent.
2022         bool IsDependent = BaseType->isDependentType();
2023         if (!IsDependent) {
2024           for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent())
2025             if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) {
2026               IsDependent = Ctx->isDependentContext();
2027               break;
2028             }
2029         }
2030 
2031         if (IsDependent)
2032           Results.AddResult(Result("template"));
2033       }
2034     }
2035   } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) {
2036     // Objective-C property reference.
2037 
2038     // Add property results based on our interface.
2039     const ObjCObjectPointerType *ObjCPtr
2040       = BaseType->getAsObjCInterfacePointerType();
2041     assert(ObjCPtr && "Non-NULL pointer guaranteed above!");
2042     AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, CurContext, Results);
2043 
2044     // Add properties from the protocols in a qualified interface.
2045     for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(),
2046                                               E = ObjCPtr->qual_end();
2047          I != E; ++I)
2048       AddObjCProperties(*I, true, CurContext, Results);
2049   } else if ((IsArrow && BaseType->isObjCObjectPointerType()) ||
2050              (!IsArrow && BaseType->isObjCInterfaceType())) {
2051     // Objective-C instance variable access.
2052     ObjCInterfaceDecl *Class = 0;
2053     if (const ObjCObjectPointerType *ObjCPtr
2054                                     = BaseType->getAs<ObjCObjectPointerType>())
2055       Class = ObjCPtr->getInterfaceDecl();
2056     else
2057       Class = BaseType->getAs<ObjCInterfaceType>()->getDecl();
2058 
2059     // Add all ivars from this class and its superclasses.
2060     if (Class) {
2061       CodeCompletionDeclConsumer Consumer(Results, CurContext);
2062       Results.setFilter(&ResultBuilder::IsObjCIvar);
2063       LookupVisibleDecls(Class, LookupMemberName, Consumer);
2064     }
2065   }
2066 
2067   // FIXME: How do we cope with isa?
2068 
2069   Results.ExitScope();
2070 
2071   // Hand off the results found for code completion.
2072   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2073 }
2074 
2075 void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) {
2076   if (!CodeCompleter)
2077     return;
2078 
2079   typedef CodeCompleteConsumer::Result Result;
2080   ResultBuilder::LookupFilter Filter = 0;
2081   switch ((DeclSpec::TST)TagSpec) {
2082   case DeclSpec::TST_enum:
2083     Filter = &ResultBuilder::IsEnum;
2084     break;
2085 
2086   case DeclSpec::TST_union:
2087     Filter = &ResultBuilder::IsUnion;
2088     break;
2089 
2090   case DeclSpec::TST_struct:
2091   case DeclSpec::TST_class:
2092     Filter = &ResultBuilder::IsClassOrStruct;
2093     break;
2094 
2095   default:
2096     assert(false && "Unknown type specifier kind in CodeCompleteTag");
2097     return;
2098   }
2099 
2100   ResultBuilder Results(*this);
2101   CodeCompletionDeclConsumer Consumer(Results, CurContext);
2102 
2103   // First pass: look for tags.
2104   Results.setFilter(Filter);
2105   LookupVisibleDecls(S, LookupTagName, Consumer);
2106 
2107   // Second pass: look for nested name specifiers.
2108   Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
2109   LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer);
2110 
2111   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2112 }
2113 
2114 void Sema::CodeCompleteCase(Scope *S) {
2115   if (getSwitchStack().empty() || !CodeCompleter)
2116     return;
2117 
2118   SwitchStmt *Switch = getSwitchStack().back();
2119   if (!Switch->getCond()->getType()->isEnumeralType())
2120     return;
2121 
2122   // Code-complete the cases of a switch statement over an enumeration type
2123   // by providing the list of
2124   EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl();
2125 
2126   // Determine which enumerators we have already seen in the switch statement.
2127   // FIXME: Ideally, we would also be able to look *past* the code-completion
2128   // token, in case we are code-completing in the middle of the switch and not
2129   // at the end. However, we aren't able to do so at the moment.
2130   llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen;
2131   NestedNameSpecifier *Qualifier = 0;
2132   for (SwitchCase *SC = Switch->getSwitchCaseList(); SC;
2133        SC = SC->getNextSwitchCase()) {
2134     CaseStmt *Case = dyn_cast<CaseStmt>(SC);
2135     if (!Case)
2136       continue;
2137 
2138     Expr *CaseVal = Case->getLHS()->IgnoreParenCasts();
2139     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal))
2140       if (EnumConstantDecl *Enumerator
2141             = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
2142         // We look into the AST of the case statement to determine which
2143         // enumerator was named. Alternatively, we could compute the value of
2144         // the integral constant expression, then compare it against the
2145         // values of each enumerator. However, value-based approach would not
2146         // work as well with C++ templates where enumerators declared within a
2147         // template are type- and value-dependent.
2148         EnumeratorsSeen.insert(Enumerator);
2149 
2150         // If this is a qualified-id, keep track of the nested-name-specifier
2151         // so that we can reproduce it as part of code completion, e.g.,
2152         //
2153         //   switch (TagD.getKind()) {
2154         //     case TagDecl::TK_enum:
2155         //       break;
2156         //     case XXX
2157         //
2158         // At the XXX, our completions are TagDecl::TK_union,
2159         // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union,
2160         // TK_struct, and TK_class.
2161         Qualifier = DRE->getQualifier();
2162       }
2163   }
2164 
2165   if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) {
2166     // If there are no prior enumerators in C++, check whether we have to
2167     // qualify the names of the enumerators that we suggest, because they
2168     // may not be visible in this scope.
2169     Qualifier = getRequiredQualification(Context, CurContext,
2170                                          Enum->getDeclContext());
2171 
2172     // FIXME: Scoped enums need to start with "EnumDecl" as the context!
2173   }
2174 
2175   // Add any enumerators that have not yet been mentioned.
2176   ResultBuilder Results(*this);
2177   Results.EnterNewScope();
2178   for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(),
2179                                   EEnd = Enum->enumerator_end();
2180        E != EEnd; ++E) {
2181     if (EnumeratorsSeen.count(*E))
2182       continue;
2183 
2184     Results.AddResult(CodeCompleteConsumer::Result(*E, Qualifier),
2185                       CurContext, 0, false);
2186   }
2187   Results.ExitScope();
2188 
2189   if (CodeCompleter->includeMacros())
2190     AddMacroResults(PP, Results);
2191   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2192 }
2193 
2194 namespace {
2195   struct IsBetterOverloadCandidate {
2196     Sema &S;
2197     SourceLocation Loc;
2198 
2199   public:
2200     explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc)
2201       : S(S), Loc(Loc) { }
2202 
2203     bool
2204     operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const {
2205       return S.isBetterOverloadCandidate(X, Y, Loc);
2206     }
2207   };
2208 }
2209 
2210 void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn,
2211                             ExprTy **ArgsIn, unsigned NumArgs) {
2212   if (!CodeCompleter)
2213     return;
2214 
2215   // When we're code-completing for a call, we fall back to ordinary
2216   // name code-completion whenever we can't produce specific
2217   // results. We may want to revisit this strategy in the future,
2218   // e.g., by merging the two kinds of results.
2219 
2220   Expr *Fn = (Expr *)FnIn;
2221   Expr **Args = (Expr **)ArgsIn;
2222 
2223   // Ignore type-dependent call expressions entirely.
2224   if (Fn->isTypeDependent() ||
2225       Expr::hasAnyTypeDependentArguments(Args, NumArgs)) {
2226     CodeCompleteOrdinaryName(S, CCC_Expression);
2227     return;
2228   }
2229 
2230   // Build an overload candidate set based on the functions we find.
2231   SourceLocation Loc = Fn->getExprLoc();
2232   OverloadCandidateSet CandidateSet(Loc);
2233 
2234   // FIXME: What if we're calling something that isn't a function declaration?
2235   // FIXME: What if we're calling a pseudo-destructor?
2236   // FIXME: What if we're calling a member function?
2237 
2238   typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate;
2239   llvm::SmallVector<ResultCandidate, 8> Results;
2240 
2241   Expr *NakedFn = Fn->IgnoreParenCasts();
2242   if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn))
2243     AddOverloadedCallCandidates(ULE, Args, NumArgs, CandidateSet,
2244                                 /*PartialOverloading=*/ true);
2245   else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) {
2246     FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
2247     if (FDecl) {
2248       if (!FDecl->getType()->getAs<FunctionProtoType>())
2249         Results.push_back(ResultCandidate(FDecl));
2250       else
2251         // FIXME: access?
2252         AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none),
2253                              Args, NumArgs, CandidateSet,
2254                              false, /*PartialOverloading*/true);
2255     }
2256   }
2257 
2258   if (!CandidateSet.empty()) {
2259     // Sort the overload candidate set by placing the best overloads first.
2260     std::stable_sort(CandidateSet.begin(), CandidateSet.end(),
2261                      IsBetterOverloadCandidate(*this, Loc));
2262 
2263     // Add the remaining viable overload candidates as code-completion reslults.
2264     for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
2265                                      CandEnd = CandidateSet.end();
2266          Cand != CandEnd; ++Cand) {
2267       if (Cand->Viable)
2268         Results.push_back(ResultCandidate(Cand->Function));
2269     }
2270   }
2271 
2272   CodeCompleteOrdinaryName(S, CCC_Expression);
2273   if (!Results.empty())
2274     CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(),
2275                                              Results.size());
2276 }
2277 
2278 void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS,
2279                                    bool EnteringContext) {
2280   if (!SS.getScopeRep() || !CodeCompleter)
2281     return;
2282 
2283   DeclContext *Ctx = computeDeclContext(SS, EnteringContext);
2284   if (!Ctx)
2285     return;
2286 
2287   // Try to instantiate any non-dependent declaration contexts before
2288   // we look in them.
2289   if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx))
2290     return;
2291 
2292   ResultBuilder Results(*this);
2293   CodeCompletionDeclConsumer Consumer(Results, CurContext);
2294   LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer);
2295 
2296   // The "template" keyword can follow "::" in the grammar, but only
2297   // put it into the grammar if the nested-name-specifier is dependent.
2298   NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
2299   if (!Results.empty() && NNS->isDependent())
2300     Results.AddResult("template");
2301 
2302   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2303 }
2304 
2305 void Sema::CodeCompleteUsing(Scope *S) {
2306   if (!CodeCompleter)
2307     return;
2308 
2309   ResultBuilder Results(*this, &ResultBuilder::IsNestedNameSpecifier);
2310   Results.EnterNewScope();
2311 
2312   // If we aren't in class scope, we could see the "namespace" keyword.
2313   if (!S->isClassScope())
2314     Results.AddResult(CodeCompleteConsumer::Result("namespace"));
2315 
2316   // After "using", we can see anything that would start a
2317   // nested-name-specifier.
2318   CodeCompletionDeclConsumer Consumer(Results, CurContext);
2319   LookupVisibleDecls(S, LookupOrdinaryName, Consumer);
2320   Results.ExitScope();
2321 
2322   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2323 }
2324 
2325 void Sema::CodeCompleteUsingDirective(Scope *S) {
2326   if (!CodeCompleter)
2327     return;
2328 
2329   // After "using namespace", we expect to see a namespace name or namespace
2330   // alias.
2331   ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias);
2332   Results.EnterNewScope();
2333   CodeCompletionDeclConsumer Consumer(Results, CurContext);
2334   LookupVisibleDecls(S, LookupOrdinaryName, Consumer);
2335   Results.ExitScope();
2336   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2337 }
2338 
2339 void Sema::CodeCompleteNamespaceDecl(Scope *S)  {
2340   if (!CodeCompleter)
2341     return;
2342 
2343   ResultBuilder Results(*this, &ResultBuilder::IsNamespace);
2344   DeclContext *Ctx = (DeclContext *)S->getEntity();
2345   if (!S->getParent())
2346     Ctx = Context.getTranslationUnitDecl();
2347 
2348   if (Ctx && Ctx->isFileContext()) {
2349     // We only want to see those namespaces that have already been defined
2350     // within this scope, because its likely that the user is creating an
2351     // extended namespace declaration. Keep track of the most recent
2352     // definition of each namespace.
2353     std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest;
2354     for (DeclContext::specific_decl_iterator<NamespaceDecl>
2355          NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end());
2356          NS != NSEnd; ++NS)
2357       OrigToLatest[NS->getOriginalNamespace()] = *NS;
2358 
2359     // Add the most recent definition (or extended definition) of each
2360     // namespace to the list of results.
2361     Results.EnterNewScope();
2362     for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator
2363          NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end();
2364          NS != NSEnd; ++NS)
2365       Results.AddResult(CodeCompleteConsumer::Result(NS->second, 0),
2366                         CurContext, 0, false);
2367     Results.ExitScope();
2368   }
2369 
2370   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2371 }
2372 
2373 void Sema::CodeCompleteNamespaceAliasDecl(Scope *S)  {
2374   if (!CodeCompleter)
2375     return;
2376 
2377   // After "namespace", we expect to see a namespace or alias.
2378   ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias);
2379   CodeCompletionDeclConsumer Consumer(Results, CurContext);
2380   LookupVisibleDecls(S, LookupOrdinaryName, Consumer);
2381   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2382 }
2383 
2384 void Sema::CodeCompleteOperatorName(Scope *S) {
2385   if (!CodeCompleter)
2386     return;
2387 
2388   typedef CodeCompleteConsumer::Result Result;
2389   ResultBuilder Results(*this, &ResultBuilder::IsType);
2390   Results.EnterNewScope();
2391 
2392   // Add the names of overloadable operators.
2393 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly)      \
2394   if (std::strcmp(Spelling, "?"))                                                  \
2395     Results.AddResult(Result(Spelling));
2396 #include "clang/Basic/OperatorKinds.def"
2397 
2398   // Add any type names visible from the current scope
2399   Results.allowNestedNameSpecifiers();
2400   CodeCompletionDeclConsumer Consumer(Results, CurContext);
2401   LookupVisibleDecls(S, LookupOrdinaryName, Consumer);
2402 
2403   // Add any type specifiers
2404   AddTypeSpecifierResults(getLangOptions(), Results);
2405   Results.ExitScope();
2406 
2407   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2408 }
2409 
2410 // Macro that expands to @Keyword or Keyword, depending on whether NeedAt is
2411 // true or false.
2412 #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) NeedAt? "@" #Keyword : #Keyword
2413 static void AddObjCImplementationResults(const LangOptions &LangOpts,
2414                                          ResultBuilder &Results,
2415                                          bool NeedAt) {
2416   typedef CodeCompleteConsumer::Result Result;
2417   // Since we have an implementation, we can end it.
2418   Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end)));
2419 
2420   CodeCompletionString *Pattern = 0;
2421   if (LangOpts.ObjC2) {
2422     // @dynamic
2423     Pattern = new CodeCompletionString;
2424     Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,dynamic));
2425     Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2426     Pattern->AddPlaceholderChunk("property");
2427     Results.AddResult(Result(Pattern));
2428 
2429     // @synthesize
2430     Pattern = new CodeCompletionString;
2431     Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synthesize));
2432     Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2433     Pattern->AddPlaceholderChunk("property");
2434     Results.AddResult(Result(Pattern));
2435   }
2436 }
2437 
2438 static void AddObjCInterfaceResults(const LangOptions &LangOpts,
2439                                     ResultBuilder &Results,
2440                                     bool NeedAt) {
2441   typedef CodeCompleteConsumer::Result Result;
2442 
2443   // Since we have an interface or protocol, we can end it.
2444   Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,end)));
2445 
2446   if (LangOpts.ObjC2) {
2447     // @property
2448     Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,property)));
2449 
2450     // @required
2451     Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,required)));
2452 
2453     // @optional
2454     Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,optional)));
2455   }
2456 }
2457 
2458 static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) {
2459   typedef CodeCompleteConsumer::Result Result;
2460   CodeCompletionString *Pattern = 0;
2461 
2462   // @class name ;
2463   Pattern = new CodeCompletionString;
2464   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,class));
2465   Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2466   Pattern->AddPlaceholderChunk("identifier");
2467   Results.AddResult(Result(Pattern));
2468 
2469   // @interface name
2470   // FIXME: Could introduce the whole pattern, including superclasses and
2471   // such.
2472   Pattern = new CodeCompletionString;
2473   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,interface));
2474   Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2475   Pattern->AddPlaceholderChunk("class");
2476   Results.AddResult(Result(Pattern));
2477 
2478   // @protocol name
2479   Pattern = new CodeCompletionString;
2480   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol));
2481   Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2482   Pattern->AddPlaceholderChunk("protocol");
2483   Results.AddResult(Result(Pattern));
2484 
2485   // @implementation name
2486   Pattern = new CodeCompletionString;
2487   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,implementation));
2488   Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2489   Pattern->AddPlaceholderChunk("class");
2490   Results.AddResult(Result(Pattern));
2491 
2492   // @compatibility_alias name
2493   Pattern = new CodeCompletionString;
2494   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,compatibility_alias));
2495   Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2496   Pattern->AddPlaceholderChunk("alias");
2497   Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2498   Pattern->AddPlaceholderChunk("class");
2499   Results.AddResult(Result(Pattern));
2500 }
2501 
2502 void Sema::CodeCompleteObjCAtDirective(Scope *S, DeclPtrTy ObjCImpDecl,
2503                                        bool InInterface) {
2504   typedef CodeCompleteConsumer::Result Result;
2505   ResultBuilder Results(*this);
2506   Results.EnterNewScope();
2507   if (ObjCImpDecl)
2508     AddObjCImplementationResults(getLangOptions(), Results, false);
2509   else if (InInterface)
2510     AddObjCInterfaceResults(getLangOptions(), Results, false);
2511   else
2512     AddObjCTopLevelResults(Results, false);
2513   Results.ExitScope();
2514   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2515 }
2516 
2517 static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) {
2518   typedef CodeCompleteConsumer::Result Result;
2519   CodeCompletionString *Pattern = 0;
2520 
2521   // @encode ( type-name )
2522   Pattern = new CodeCompletionString;
2523   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,encode));
2524   Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
2525   Pattern->AddPlaceholderChunk("type-name");
2526   Pattern->AddChunk(CodeCompletionString::CK_RightParen);
2527   Results.AddResult(Result(Pattern));
2528 
2529   // @protocol ( protocol-name )
2530   Pattern = new CodeCompletionString;
2531   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,protocol));
2532   Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
2533   Pattern->AddPlaceholderChunk("protocol-name");
2534   Pattern->AddChunk(CodeCompletionString::CK_RightParen);
2535   Results.AddResult(Result(Pattern));
2536 
2537   // @selector ( selector )
2538   Pattern = new CodeCompletionString;
2539   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,selector));
2540   Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
2541   Pattern->AddPlaceholderChunk("selector");
2542   Pattern->AddChunk(CodeCompletionString::CK_RightParen);
2543   Results.AddResult(Result(Pattern));
2544 }
2545 
2546 static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) {
2547   typedef CodeCompleteConsumer::Result Result;
2548   CodeCompletionString *Pattern = 0;
2549 
2550   // @try { statements } @catch ( declaration ) { statements } @finally
2551   //   { statements }
2552   Pattern = new CodeCompletionString;
2553   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,try));
2554   Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
2555   Pattern->AddPlaceholderChunk("statements");
2556   Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
2557   Pattern->AddTextChunk("@catch");
2558   Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
2559   Pattern->AddPlaceholderChunk("parameter");
2560   Pattern->AddChunk(CodeCompletionString::CK_RightParen);
2561   Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
2562   Pattern->AddPlaceholderChunk("statements");
2563   Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
2564   Pattern->AddTextChunk("@finally");
2565   Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
2566   Pattern->AddPlaceholderChunk("statements");
2567   Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
2568   Results.AddResult(Result(Pattern));
2569 
2570   // @throw
2571   Pattern = new CodeCompletionString;
2572   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,throw));
2573   Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2574   Pattern->AddPlaceholderChunk("expression");
2575   Results.AddResult(Result(Pattern));
2576 
2577   // @synchronized ( expression ) { statements }
2578   Pattern = new CodeCompletionString;
2579   Pattern->AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,synchronized));
2580   Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
2581   Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
2582   Pattern->AddPlaceholderChunk("expression");
2583   Pattern->AddChunk(CodeCompletionString::CK_RightParen);
2584   Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
2585   Pattern->AddPlaceholderChunk("statements");
2586   Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
2587   Results.AddResult(Result(Pattern));
2588 }
2589 
2590 static void AddObjCVisibilityResults(const LangOptions &LangOpts,
2591                                      ResultBuilder &Results,
2592                                      bool NeedAt) {
2593   typedef CodeCompleteConsumer::Result Result;
2594   Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,private)));
2595   Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,protected)));
2596   Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,public)));
2597   if (LangOpts.ObjC2)
2598     Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,package)));
2599 }
2600 
2601 void Sema::CodeCompleteObjCAtVisibility(Scope *S) {
2602   ResultBuilder Results(*this);
2603   Results.EnterNewScope();
2604   AddObjCVisibilityResults(getLangOptions(), Results, false);
2605   Results.ExitScope();
2606   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2607 }
2608 
2609 void Sema::CodeCompleteObjCAtStatement(Scope *S) {
2610   ResultBuilder Results(*this);
2611   Results.EnterNewScope();
2612   AddObjCStatementResults(Results, false);
2613   AddObjCExpressionResults(Results, false);
2614   Results.ExitScope();
2615   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2616 }
2617 
2618 void Sema::CodeCompleteObjCAtExpression(Scope *S) {
2619   ResultBuilder Results(*this);
2620   Results.EnterNewScope();
2621   AddObjCExpressionResults(Results, false);
2622   Results.ExitScope();
2623   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2624 }
2625 
2626 /// \brief Determine whether the addition of the given flag to an Objective-C
2627 /// property's attributes will cause a conflict.
2628 static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
2629   // Check if we've already added this flag.
2630   if (Attributes & NewFlag)
2631     return true;
2632 
2633   Attributes |= NewFlag;
2634 
2635   // Check for collisions with "readonly".
2636   if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2637       (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
2638                      ObjCDeclSpec::DQ_PR_assign |
2639                      ObjCDeclSpec::DQ_PR_copy |
2640                      ObjCDeclSpec::DQ_PR_retain)))
2641     return true;
2642 
2643   // Check for more than one of { assign, copy, retain }.
2644   unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign |
2645                                              ObjCDeclSpec::DQ_PR_copy |
2646                                              ObjCDeclSpec::DQ_PR_retain);
2647   if (AssignCopyRetMask &&
2648       AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign &&
2649       AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy &&
2650       AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain)
2651     return true;
2652 
2653   return false;
2654 }
2655 
2656 void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
2657   if (!CodeCompleter)
2658     return;
2659 
2660   unsigned Attributes = ODS.getPropertyAttributes();
2661 
2662   typedef CodeCompleteConsumer::Result Result;
2663   ResultBuilder Results(*this);
2664   Results.EnterNewScope();
2665   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly))
2666     Results.AddResult(CodeCompleteConsumer::Result("readonly"));
2667   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign))
2668     Results.AddResult(CodeCompleteConsumer::Result("assign"));
2669   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite))
2670     Results.AddResult(CodeCompleteConsumer::Result("readwrite"));
2671   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain))
2672     Results.AddResult(CodeCompleteConsumer::Result("retain"));
2673   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy))
2674     Results.AddResult(CodeCompleteConsumer::Result("copy"));
2675   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic))
2676     Results.AddResult(CodeCompleteConsumer::Result("nonatomic"));
2677   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) {
2678     CodeCompletionString *Setter = new CodeCompletionString;
2679     Setter->AddTypedTextChunk("setter");
2680     Setter->AddTextChunk(" = ");
2681     Setter->AddPlaceholderChunk("method");
2682     Results.AddResult(CodeCompleteConsumer::Result(Setter));
2683   }
2684   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) {
2685     CodeCompletionString *Getter = new CodeCompletionString;
2686     Getter->AddTypedTextChunk("getter");
2687     Getter->AddTextChunk(" = ");
2688     Getter->AddPlaceholderChunk("method");
2689     Results.AddResult(CodeCompleteConsumer::Result(Getter));
2690   }
2691   Results.ExitScope();
2692   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
2693 }
2694 
2695 /// \brief Descripts the kind of Objective-C method that we want to find
2696 /// via code completion.
2697 enum ObjCMethodKind {
2698   MK_Any, //< Any kind of method, provided it means other specified criteria.
2699   MK_ZeroArgSelector, //< Zero-argument (unary) selector.
2700   MK_OneArgSelector //< One-argument selector.
2701 };
2702 
2703 static bool isAcceptableObjCMethod(ObjCMethodDecl *Method,
2704                                    ObjCMethodKind WantKind,
2705                                    IdentifierInfo **SelIdents,
2706                                    unsigned NumSelIdents) {
2707   Selector Sel = Method->getSelector();
2708   if (NumSelIdents > Sel.getNumArgs())
2709     return false;
2710 
2711   switch (WantKind) {
2712   case MK_Any:             break;
2713   case MK_ZeroArgSelector: return Sel.isUnarySelector();
2714   case MK_OneArgSelector:  return Sel.getNumArgs() == 1;
2715   }
2716 
2717   for (unsigned I = 0; I != NumSelIdents; ++I)
2718     if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
2719       return false;
2720 
2721   return true;
2722 }
2723 
2724 /// \brief Add all of the Objective-C methods in the given Objective-C
2725 /// container to the set of results.
2726 ///
2727 /// The container will be a class, protocol, category, or implementation of
2728 /// any of the above. This mether will recurse to include methods from
2729 /// the superclasses of classes along with their categories, protocols, and
2730 /// implementations.
2731 ///
2732 /// \param Container the container in which we'll look to find methods.
2733 ///
2734 /// \param WantInstance whether to add instance methods (only); if false, this
2735 /// routine will add factory methods (only).
2736 ///
2737 /// \param CurContext the context in which we're performing the lookup that
2738 /// finds methods.
2739 ///
2740 /// \param Results the structure into which we'll add results.
2741 static void AddObjCMethods(ObjCContainerDecl *Container,
2742                            bool WantInstanceMethods,
2743                            ObjCMethodKind WantKind,
2744                            IdentifierInfo **SelIdents,
2745                            unsigned NumSelIdents,
2746                            DeclContext *CurContext,
2747                            ResultBuilder &Results) {
2748   typedef CodeCompleteConsumer::Result Result;
2749   for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
2750                                        MEnd = Container->meth_end();
2751        M != MEnd; ++M) {
2752     if ((*M)->isInstanceMethod() == WantInstanceMethods) {
2753       // Check whether the selector identifiers we've been given are a
2754       // subset of the identifiers for this particular method.
2755       if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents))
2756         continue;
2757 
2758       Result R = Result(*M, 0);
2759       R.StartParameter = NumSelIdents;
2760       R.AllParametersAreInformative = (WantKind != MK_Any);
2761       Results.MaybeAddResult(R, CurContext);
2762     }
2763   }
2764 
2765   ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
2766   if (!IFace)
2767     return;
2768 
2769   // Add methods in protocols.
2770   const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols();
2771   for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
2772                                             E = Protocols.end();
2773        I != E; ++I)
2774     AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents,
2775                    CurContext, Results);
2776 
2777   // Add methods in categories.
2778   for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl;
2779        CatDecl = CatDecl->getNextClassCategory()) {
2780     AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents,
2781                    NumSelIdents, CurContext, Results);
2782 
2783     // Add a categories protocol methods.
2784     const ObjCList<ObjCProtocolDecl> &Protocols
2785       = CatDecl->getReferencedProtocols();
2786     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
2787                                               E = Protocols.end();
2788          I != E; ++I)
2789       AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
2790                      NumSelIdents, CurContext, Results);
2791 
2792     // Add methods in category implementations.
2793     if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
2794       AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
2795                      NumSelIdents, CurContext, Results);
2796   }
2797 
2798   // Add methods in superclass.
2799   if (IFace->getSuperClass())
2800     AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind,
2801                    SelIdents, NumSelIdents, CurContext, Results);
2802 
2803   // Add methods in our implementation, if any.
2804   if (ObjCImplementationDecl *Impl = IFace->getImplementation())
2805     AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
2806                    NumSelIdents, CurContext, Results);
2807 }
2808 
2809 
2810 void Sema::CodeCompleteObjCPropertyGetter(Scope *S, DeclPtrTy ClassDecl,
2811                                           DeclPtrTy *Methods,
2812                                           unsigned NumMethods) {
2813   typedef CodeCompleteConsumer::Result Result;
2814 
2815   // Try to find the interface where getters might live.
2816   ObjCInterfaceDecl *Class
2817     = dyn_cast_or_null<ObjCInterfaceDecl>(ClassDecl.getAs<Decl>());
2818   if (!Class) {
2819     if (ObjCCategoryDecl *Category
2820           = dyn_cast_or_null<ObjCCategoryDecl>(ClassDecl.getAs<Decl>()))
2821       Class = Category->getClassInterface();
2822 
2823     if (!Class)
2824       return;
2825   }
2826 
2827   // Find all of the potential getters.
2828   ResultBuilder Results(*this);
2829   Results.EnterNewScope();
2830 
2831   // FIXME: We need to do this because Objective-C methods don't get
2832   // pushed into DeclContexts early enough. Argh!
2833   for (unsigned I = 0; I != NumMethods; ++I) {
2834     if (ObjCMethodDecl *Method
2835             = dyn_cast_or_null<ObjCMethodDecl>(Methods[I].getAs<Decl>()))
2836       if (Method->isInstanceMethod() &&
2837           isAcceptableObjCMethod(Method, MK_ZeroArgSelector, 0, 0)) {
2838         Result R = Result(Method, 0);
2839         R.AllParametersAreInformative = true;
2840         Results.MaybeAddResult(R, CurContext);
2841       }
2842   }
2843 
2844   AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Results);
2845   Results.ExitScope();
2846   HandleCodeCompleteResults(this, CodeCompleter,Results.data(),Results.size());
2847 }
2848 
2849 void Sema::CodeCompleteObjCPropertySetter(Scope *S, DeclPtrTy ObjCImplDecl,
2850                                           DeclPtrTy *Methods,
2851                                           unsigned NumMethods) {
2852   typedef CodeCompleteConsumer::Result Result;
2853 
2854   // Try to find the interface where setters might live.
2855   ObjCInterfaceDecl *Class
2856     = dyn_cast_or_null<ObjCInterfaceDecl>(ObjCImplDecl.getAs<Decl>());
2857   if (!Class) {
2858     if (ObjCCategoryDecl *Category
2859           = dyn_cast_or_null<ObjCCategoryDecl>(ObjCImplDecl.getAs<Decl>()))
2860       Class = Category->getClassInterface();
2861 
2862     if (!Class)
2863       return;
2864   }
2865 
2866   // Find all of the potential getters.
2867   ResultBuilder Results(*this);
2868   Results.EnterNewScope();
2869 
2870   // FIXME: We need to do this because Objective-C methods don't get
2871   // pushed into DeclContexts early enough. Argh!
2872   for (unsigned I = 0; I != NumMethods; ++I) {
2873     if (ObjCMethodDecl *Method
2874             = dyn_cast_or_null<ObjCMethodDecl>(Methods[I].getAs<Decl>()))
2875       if (Method->isInstanceMethod() &&
2876           isAcceptableObjCMethod(Method, MK_OneArgSelector, 0, 0)) {
2877         Result R = Result(Method, 0);
2878         R.AllParametersAreInformative = true;
2879         Results.MaybeAddResult(R, CurContext);
2880       }
2881   }
2882 
2883   AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, Results);
2884 
2885   Results.ExitScope();
2886   HandleCodeCompleteResults(this, CodeCompleter,Results.data(),Results.size());
2887 }
2888 
2889 /// \brief When we have an expression with type "id", we may assume
2890 /// that it has some more-specific class type based on knowledge of
2891 /// common uses of Objective-C. This routine returns that class type,
2892 /// or NULL if no better result could be determined.
2893 static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) {
2894   ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E);
2895   if (!Msg)
2896     return 0;
2897 
2898   Selector Sel = Msg->getSelector();
2899   if (Sel.isNull())
2900     return 0;
2901 
2902   IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0);
2903   if (!Id)
2904     return 0;
2905 
2906   ObjCMethodDecl *Method = Msg->getMethodDecl();
2907   if (!Method)
2908     return 0;
2909 
2910   // Determine the class that we're sending the message to.
2911   ObjCInterfaceDecl *IFace = 0;
2912   switch (Msg->getReceiverKind()) {
2913   case ObjCMessageExpr::Class:
2914     if (const ObjCInterfaceType *IFaceType
2915                            = Msg->getClassReceiver()->getAs<ObjCInterfaceType>())
2916       IFace = IFaceType->getDecl();
2917     break;
2918 
2919   case ObjCMessageExpr::Instance: {
2920     QualType T = Msg->getInstanceReceiver()->getType();
2921     if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
2922       IFace = Ptr->getInterfaceDecl();
2923     break;
2924   }
2925 
2926   case ObjCMessageExpr::SuperInstance:
2927   case ObjCMessageExpr::SuperClass:
2928     break;
2929   }
2930 
2931   if (!IFace)
2932     return 0;
2933 
2934   ObjCInterfaceDecl *Super = IFace->getSuperClass();
2935   if (Method->isInstanceMethod())
2936     return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
2937       .Case("retain", IFace)
2938       .Case("autorelease", IFace)
2939       .Case("copy", IFace)
2940       .Case("copyWithZone", IFace)
2941       .Case("mutableCopy", IFace)
2942       .Case("mutableCopyWithZone", IFace)
2943       .Case("awakeFromCoder", IFace)
2944       .Case("replacementObjectFromCoder", IFace)
2945       .Case("class", IFace)
2946       .Case("classForCoder", IFace)
2947       .Case("superclass", Super)
2948       .Default(0);
2949 
2950   return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
2951     .Case("new", IFace)
2952     .Case("alloc", IFace)
2953     .Case("allocWithZone", IFace)
2954     .Case("class", IFace)
2955     .Case("superclass", Super)
2956     .Default(0);
2957 }
2958 
2959 void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc,
2960                                         IdentifierInfo **SelIdents,
2961                                         unsigned NumSelIdents) {
2962   ObjCInterfaceDecl *CDecl = 0;
2963   if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
2964     // Figure out which interface we're in.
2965     CDecl = CurMethod->getClassInterface();
2966     if (!CDecl)
2967       return;
2968 
2969     // Find the superclass of this class.
2970     CDecl = CDecl->getSuperClass();
2971     if (!CDecl)
2972       return;
2973 
2974     if (CurMethod->isInstanceMethod()) {
2975       // We are inside an instance method, which means that the message
2976       // send [super ...] is actually calling an instance method on the
2977       // current object. Build the super expression and handle this like
2978       // an instance method.
2979       QualType SuperTy = Context.getObjCInterfaceType(CDecl);
2980       SuperTy = Context.getObjCObjectPointerType(SuperTy);
2981       OwningExprResult Super
2982         = Owned(new (Context) ObjCSuperExpr(SuperLoc, SuperTy));
2983       return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(),
2984                                              SelIdents, NumSelIdents);
2985     }
2986 
2987     // Fall through to send to the superclass in CDecl.
2988   } else {
2989     // "super" may be the name of a type or variable. Figure out which
2990     // it is.
2991     IdentifierInfo *Super = &Context.Idents.get("super");
2992     NamedDecl *ND = LookupSingleName(S, Super, SuperLoc,
2993                                      LookupOrdinaryName);
2994     if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) {
2995       // "super" names an interface. Use it.
2996     } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) {
2997       if (const ObjCInterfaceType *Iface
2998             = Context.getTypeDeclType(TD)->getAs<ObjCInterfaceType>())
2999         CDecl = Iface->getDecl();
3000     } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) {
3001       // "super" names an unresolved type; we can't be more specific.
3002     } else {
3003       // Assume that "super" names some kind of value and parse that way.
3004       CXXScopeSpec SS;
3005       UnqualifiedId id;
3006       id.setIdentifier(Super, SuperLoc);
3007       OwningExprResult SuperExpr = ActOnIdExpression(S, SS, id, false, false);
3008       return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(),
3009                                              SelIdents, NumSelIdents);
3010     }
3011 
3012     // Fall through
3013   }
3014 
3015   TypeTy *Receiver = 0;
3016   if (CDecl)
3017     Receiver = Context.getObjCInterfaceType(CDecl).getAsOpaquePtr();
3018   return CodeCompleteObjCClassMessage(S, Receiver, SelIdents,
3019                                       NumSelIdents);
3020 }
3021 
3022 void Sema::CodeCompleteObjCClassMessage(Scope *S, TypeTy *Receiver,
3023                                         IdentifierInfo **SelIdents,
3024                                         unsigned NumSelIdents) {
3025   typedef CodeCompleteConsumer::Result Result;
3026   ObjCInterfaceDecl *CDecl = 0;
3027 
3028   // If the given name refers to an interface type, retrieve the
3029   // corresponding declaration.
3030   if (Receiver) {
3031     QualType T = GetTypeFromParser(Receiver, 0);
3032     if (!T.isNull())
3033       if (const ObjCInterfaceType *Interface = T->getAs<ObjCInterfaceType>())
3034         CDecl = Interface->getDecl();
3035   }
3036 
3037   // Add all of the factory methods in this Objective-C class, its protocols,
3038   // superclasses, categories, implementation, etc.
3039   ResultBuilder Results(*this);
3040   Results.EnterNewScope();
3041 
3042   if (CDecl)
3043     AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, CurContext,
3044                    Results);
3045   else {
3046     // We're messaging "id" as a type; provide all class/factory methods.
3047 
3048     // If we have an external source, load the entire class method
3049     // pool from the PCH file.
3050     if (ExternalSource) {
3051       for (uint32_t I = 0, N = ExternalSource->GetNumKnownSelectors(); I != N;
3052            ++I) {
3053         Selector Sel = ExternalSource->GetSelector(I);
3054         if (Sel.isNull() || FactoryMethodPool.count(Sel) ||
3055             InstanceMethodPool.count(Sel))
3056           continue;
3057 
3058         ReadMethodPool(Sel, /*isInstance=*/false);
3059       }
3060     }
3061 
3062     for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
3063            M = FactoryMethodPool.begin(),
3064            MEnd = FactoryMethodPool.end();
3065          M != MEnd;
3066          ++M) {
3067       for (ObjCMethodList *MethList = &M->second; MethList && MethList->Method;
3068            MethList = MethList->Next) {
3069         if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
3070                                     NumSelIdents))
3071           continue;
3072 
3073         Result R(MethList->Method, 0);
3074         R.StartParameter = NumSelIdents;
3075         R.AllParametersAreInformative = false;
3076         Results.MaybeAddResult(R, CurContext);
3077       }
3078     }
3079   }
3080 
3081   Results.ExitScope();
3082 
3083   // This also suppresses remaining diagnostics.
3084   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3085 }
3086 
3087 void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver,
3088                                            IdentifierInfo **SelIdents,
3089                                            unsigned NumSelIdents) {
3090   typedef CodeCompleteConsumer::Result Result;
3091 
3092   Expr *RecExpr = static_cast<Expr *>(Receiver);
3093 
3094   // If necessary, apply function/array conversion to the receiver.
3095   // C99 6.7.5.3p[7,8].
3096   DefaultFunctionArrayLvalueConversion(RecExpr);
3097   QualType ReceiverType = RecExpr->getType();
3098 
3099   // Build the set of methods we can see.
3100   ResultBuilder Results(*this);
3101   Results.EnterNewScope();
3102 
3103   // If we're messaging an expression with type "id" or "Class", check
3104   // whether we know something special about the receiver that allows
3105   // us to assume a more-specific receiver type.
3106   if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType())
3107     if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr))
3108       ReceiverType = Context.getObjCObjectPointerType(
3109                                           Context.getObjCInterfaceType(IFace));
3110 
3111   // Handle messages to Class. This really isn't a message to an instance
3112   // method, so we treat it the same way we would treat a message send to a
3113   // class method.
3114   if (ReceiverType->isObjCClassType() ||
3115       ReceiverType->isObjCQualifiedClassType()) {
3116     if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
3117       if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
3118         AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents,
3119                        CurContext, Results);
3120     }
3121   }
3122   // Handle messages to a qualified ID ("id<foo>").
3123   else if (const ObjCObjectPointerType *QualID
3124              = ReceiverType->getAsObjCQualifiedIdType()) {
3125     // Search protocols for instance methods.
3126     for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(),
3127                                               E = QualID->qual_end();
3128          I != E; ++I)
3129       AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
3130                      Results);
3131   }
3132   // Handle messages to a pointer to interface type.
3133   else if (const ObjCObjectPointerType *IFacePtr
3134                               = ReceiverType->getAsObjCInterfacePointerType()) {
3135     // Search the class, its superclasses, etc., for instance methods.
3136     AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents,
3137                    NumSelIdents, CurContext, Results);
3138 
3139     // Search protocols for instance methods.
3140     for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(),
3141          E = IFacePtr->qual_end();
3142          I != E; ++I)
3143       AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
3144                      Results);
3145   }
3146   // Handle messages to "id".
3147   else if (ReceiverType->isObjCIdType()) {
3148     // We're messaging "id", so provide all instance methods we know
3149     // about as code-completion results.
3150 
3151     // If we have an external source, load the entire class method
3152     // pool from the PCH file.
3153     if (ExternalSource) {
3154       for (uint32_t I = 0, N = ExternalSource->GetNumKnownSelectors(); I != N;
3155            ++I) {
3156         Selector Sel = ExternalSource->GetSelector(I);
3157         if (Sel.isNull() || InstanceMethodPool.count(Sel) ||
3158             FactoryMethodPool.count(Sel))
3159           continue;
3160 
3161         ReadMethodPool(Sel, /*isInstance=*/true);
3162       }
3163     }
3164 
3165     for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
3166            M = InstanceMethodPool.begin(),
3167            MEnd = InstanceMethodPool.end();
3168          M != MEnd;
3169          ++M) {
3170       for (ObjCMethodList *MethList = &M->second; MethList && MethList->Method;
3171            MethList = MethList->Next) {
3172         if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
3173                                     NumSelIdents))
3174           continue;
3175 
3176         Result R(MethList->Method, 0);
3177         R.StartParameter = NumSelIdents;
3178         R.AllParametersAreInformative = false;
3179         Results.MaybeAddResult(R, CurContext);
3180       }
3181     }
3182   }
3183 
3184   Results.ExitScope();
3185   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3186 }
3187 
3188 /// \brief Add all of the protocol declarations that we find in the given
3189 /// (translation unit) context.
3190 static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext,
3191                                bool OnlyForwardDeclarations,
3192                                ResultBuilder &Results) {
3193   typedef CodeCompleteConsumer::Result Result;
3194 
3195   for (DeclContext::decl_iterator D = Ctx->decls_begin(),
3196                                DEnd = Ctx->decls_end();
3197        D != DEnd; ++D) {
3198     // Record any protocols we find.
3199     if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D))
3200       if (!OnlyForwardDeclarations || Proto->isForwardDecl())
3201         Results.AddResult(Result(Proto, 0), CurContext, 0, false);
3202 
3203     // Record any forward-declared protocols we find.
3204     if (ObjCForwardProtocolDecl *Forward
3205           = dyn_cast<ObjCForwardProtocolDecl>(*D)) {
3206       for (ObjCForwardProtocolDecl::protocol_iterator
3207              P = Forward->protocol_begin(),
3208              PEnd = Forward->protocol_end();
3209            P != PEnd; ++P)
3210         if (!OnlyForwardDeclarations || (*P)->isForwardDecl())
3211           Results.AddResult(Result(*P, 0), CurContext, 0, false);
3212     }
3213   }
3214 }
3215 
3216 void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
3217                                               unsigned NumProtocols) {
3218   ResultBuilder Results(*this);
3219   Results.EnterNewScope();
3220 
3221   // Tell the result set to ignore all of the protocols we have
3222   // already seen.
3223   for (unsigned I = 0; I != NumProtocols; ++I)
3224     if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first,
3225                                                     Protocols[I].second))
3226       Results.Ignore(Protocol);
3227 
3228   // Add all protocols.
3229   AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false,
3230                      Results);
3231 
3232   Results.ExitScope();
3233   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3234 }
3235 
3236 void Sema::CodeCompleteObjCProtocolDecl(Scope *) {
3237   ResultBuilder Results(*this);
3238   Results.EnterNewScope();
3239 
3240   // Add all protocols.
3241   AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true,
3242                      Results);
3243 
3244   Results.ExitScope();
3245   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3246 }
3247 
3248 /// \brief Add all of the Objective-C interface declarations that we find in
3249 /// the given (translation unit) context.
3250 static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext,
3251                                 bool OnlyForwardDeclarations,
3252                                 bool OnlyUnimplemented,
3253                                 ResultBuilder &Results) {
3254   typedef CodeCompleteConsumer::Result Result;
3255 
3256   for (DeclContext::decl_iterator D = Ctx->decls_begin(),
3257                                DEnd = Ctx->decls_end();
3258        D != DEnd; ++D) {
3259     // Record any interfaces we find.
3260     if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D))
3261       if ((!OnlyForwardDeclarations || Class->isForwardDecl()) &&
3262           (!OnlyUnimplemented || !Class->getImplementation()))
3263         Results.AddResult(Result(Class, 0), CurContext, 0, false);
3264 
3265     // Record any forward-declared interfaces we find.
3266     if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) {
3267       for (ObjCClassDecl::iterator C = Forward->begin(), CEnd = Forward->end();
3268            C != CEnd; ++C)
3269         if ((!OnlyForwardDeclarations || C->getInterface()->isForwardDecl()) &&
3270             (!OnlyUnimplemented || !C->getInterface()->getImplementation()))
3271           Results.AddResult(Result(C->getInterface(), 0), CurContext,
3272                             0, false);
3273     }
3274   }
3275 }
3276 
3277 void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) {
3278   ResultBuilder Results(*this);
3279   Results.EnterNewScope();
3280 
3281   // Add all classes.
3282   AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, true,
3283                       false, Results);
3284 
3285   Results.ExitScope();
3286   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3287 }
3288 
3289 void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName,
3290                                       SourceLocation ClassNameLoc) {
3291   ResultBuilder Results(*this);
3292   Results.EnterNewScope();
3293 
3294   // Make sure that we ignore the class we're currently defining.
3295   NamedDecl *CurClass
3296     = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
3297   if (CurClass && isa<ObjCInterfaceDecl>(CurClass))
3298     Results.Ignore(CurClass);
3299 
3300   // Add all classes.
3301   AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
3302                       false, Results);
3303 
3304   Results.ExitScope();
3305   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3306 }
3307 
3308 void Sema::CodeCompleteObjCImplementationDecl(Scope *S) {
3309   ResultBuilder Results(*this);
3310   Results.EnterNewScope();
3311 
3312   // Add all unimplemented classes.
3313   AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
3314                       true, Results);
3315 
3316   Results.ExitScope();
3317   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3318 }
3319 
3320 void Sema::CodeCompleteObjCInterfaceCategory(Scope *S,
3321                                              IdentifierInfo *ClassName,
3322                                              SourceLocation ClassNameLoc) {
3323   typedef CodeCompleteConsumer::Result Result;
3324 
3325   ResultBuilder Results(*this);
3326 
3327   // Ignore any categories we find that have already been implemented by this
3328   // interface.
3329   llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
3330   NamedDecl *CurClass
3331     = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
3332   if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass))
3333     for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
3334          Category = Category->getNextClassCategory())
3335       CategoryNames.insert(Category->getIdentifier());
3336 
3337   // Add all of the categories we know about.
3338   Results.EnterNewScope();
3339   TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3340   for (DeclContext::decl_iterator D = TU->decls_begin(),
3341                                DEnd = TU->decls_end();
3342        D != DEnd; ++D)
3343     if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D))
3344       if (CategoryNames.insert(Category->getIdentifier()))
3345         Results.AddResult(Result(Category, 0), CurContext, 0, false);
3346   Results.ExitScope();
3347 
3348   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3349 }
3350 
3351 void Sema::CodeCompleteObjCImplementationCategory(Scope *S,
3352                                                   IdentifierInfo *ClassName,
3353                                                   SourceLocation ClassNameLoc) {
3354   typedef CodeCompleteConsumer::Result Result;
3355 
3356   // Find the corresponding interface. If we couldn't find the interface, the
3357   // program itself is ill-formed. However, we'll try to be helpful still by
3358   // providing the list of all of the categories we know about.
3359   NamedDecl *CurClass
3360     = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
3361   ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass);
3362   if (!Class)
3363     return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc);
3364 
3365   ResultBuilder Results(*this);
3366 
3367   // Add all of the categories that have have corresponding interface
3368   // declarations in this class and any of its superclasses, except for
3369   // already-implemented categories in the class itself.
3370   llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
3371   Results.EnterNewScope();
3372   bool IgnoreImplemented = true;
3373   while (Class) {
3374     for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
3375          Category = Category->getNextClassCategory())
3376       if ((!IgnoreImplemented || !Category->getImplementation()) &&
3377           CategoryNames.insert(Category->getIdentifier()))
3378         Results.AddResult(Result(Category, 0), CurContext, 0, false);
3379 
3380     Class = Class->getSuperClass();
3381     IgnoreImplemented = false;
3382   }
3383   Results.ExitScope();
3384 
3385   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3386 }
3387 
3388 void Sema::CodeCompleteObjCPropertyDefinition(Scope *S, DeclPtrTy ObjCImpDecl) {
3389   typedef CodeCompleteConsumer::Result Result;
3390   ResultBuilder Results(*this);
3391 
3392   // Figure out where this @synthesize lives.
3393   ObjCContainerDecl *Container
3394     = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl.getAs<Decl>());
3395   if (!Container ||
3396       (!isa<ObjCImplementationDecl>(Container) &&
3397        !isa<ObjCCategoryImplDecl>(Container)))
3398     return;
3399 
3400   // Ignore any properties that have already been implemented.
3401   for (DeclContext::decl_iterator D = Container->decls_begin(),
3402                                DEnd = Container->decls_end();
3403        D != DEnd; ++D)
3404     if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D))
3405       Results.Ignore(PropertyImpl->getPropertyDecl());
3406 
3407   // Add any properties that we find.
3408   Results.EnterNewScope();
3409   if (ObjCImplementationDecl *ClassImpl
3410         = dyn_cast<ObjCImplementationDecl>(Container))
3411     AddObjCProperties(ClassImpl->getClassInterface(), false, CurContext,
3412                       Results);
3413   else
3414     AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(),
3415                       false, CurContext, Results);
3416   Results.ExitScope();
3417 
3418   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3419 }
3420 
3421 void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
3422                                                   IdentifierInfo *PropertyName,
3423                                                   DeclPtrTy ObjCImpDecl) {
3424   typedef CodeCompleteConsumer::Result Result;
3425   ResultBuilder Results(*this);
3426 
3427   // Figure out where this @synthesize lives.
3428   ObjCContainerDecl *Container
3429     = dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl.getAs<Decl>());
3430   if (!Container ||
3431       (!isa<ObjCImplementationDecl>(Container) &&
3432        !isa<ObjCCategoryImplDecl>(Container)))
3433     return;
3434 
3435   // Figure out which interface we're looking into.
3436   ObjCInterfaceDecl *Class = 0;
3437   if (ObjCImplementationDecl *ClassImpl
3438                                  = dyn_cast<ObjCImplementationDecl>(Container))
3439     Class = ClassImpl->getClassInterface();
3440   else
3441     Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
3442                                                           ->getClassInterface();
3443 
3444   // Add all of the instance variables in this class and its superclasses.
3445   Results.EnterNewScope();
3446   for(; Class; Class = Class->getSuperClass()) {
3447     // FIXME: We could screen the type of each ivar for compatibility with
3448     // the property, but is that being too paternal?
3449     for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(),
3450                                        IVarEnd = Class->ivar_end();
3451          IVar != IVarEnd; ++IVar)
3452       Results.AddResult(Result(*IVar, 0), CurContext, 0, false);
3453   }
3454   Results.ExitScope();
3455 
3456   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3457 }
3458 
3459 typedef llvm::DenseMap<Selector, ObjCMethodDecl *> KnownMethodsMap;
3460 
3461 /// \brief Find all of the methods that reside in the given container
3462 /// (and its superclasses, protocols, etc.) that meet the given
3463 /// criteria. Insert those methods into the map of known methods,
3464 /// indexed by selector so they can be easily found.
3465 static void FindImplementableMethods(ASTContext &Context,
3466                                      ObjCContainerDecl *Container,
3467                                      bool WantInstanceMethods,
3468                                      QualType ReturnType,
3469                                      bool IsInImplementation,
3470                                      KnownMethodsMap &KnownMethods) {
3471   if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) {
3472     // Recurse into protocols.
3473     const ObjCList<ObjCProtocolDecl> &Protocols
3474       = IFace->getReferencedProtocols();
3475     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
3476            E = Protocols.end();
3477          I != E; ++I)
3478       FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
3479                                IsInImplementation, KnownMethods);
3480 
3481     // If we're not in the implementation of a class, also visit the
3482     // superclass.
3483     if (!IsInImplementation && IFace->getSuperClass())
3484       FindImplementableMethods(Context, IFace->getSuperClass(),
3485                                WantInstanceMethods, ReturnType,
3486                                IsInImplementation, KnownMethods);
3487 
3488     // Add methods from any class extensions (but not from categories;
3489     // those should go into category implementations).
3490     for (ObjCCategoryDecl *Cat = IFace->getCategoryList(); Cat;
3491          Cat = Cat->getNextClassCategory()) {
3492       if (!Cat->IsClassExtension())
3493         continue;
3494 
3495       FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType,
3496                                IsInImplementation, KnownMethods);
3497     }
3498   }
3499 
3500   if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
3501     // Recurse into protocols.
3502     const ObjCList<ObjCProtocolDecl> &Protocols
3503       = Category->getReferencedProtocols();
3504     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
3505            E = Protocols.end();
3506          I != E; ++I)
3507       FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
3508                                IsInImplementation, KnownMethods);
3509   }
3510 
3511   if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3512     // Recurse into protocols.
3513     const ObjCList<ObjCProtocolDecl> &Protocols
3514       = Protocol->getReferencedProtocols();
3515     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
3516            E = Protocols.end();
3517          I != E; ++I)
3518       FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
3519                                IsInImplementation, KnownMethods);
3520   }
3521 
3522   // Add methods in this container. This operation occurs last because
3523   // we want the methods from this container to override any methods
3524   // we've previously seen with the same selector.
3525   for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
3526                                        MEnd = Container->meth_end();
3527        M != MEnd; ++M) {
3528     if ((*M)->isInstanceMethod() == WantInstanceMethods) {
3529       if (!ReturnType.isNull() &&
3530           !Context.hasSameUnqualifiedType(ReturnType, (*M)->getResultType()))
3531         continue;
3532 
3533       KnownMethods[(*M)->getSelector()] = *M;
3534     }
3535   }
3536 }
3537 
3538 void Sema::CodeCompleteObjCMethodDecl(Scope *S,
3539                                       bool IsInstanceMethod,
3540                                       TypeTy *ReturnTy,
3541                                       DeclPtrTy IDecl) {
3542   // Determine the return type of the method we're declaring, if
3543   // provided.
3544   QualType ReturnType = GetTypeFromParser(ReturnTy);
3545 
3546   // Determine where we should start searching for methods, and where we
3547   ObjCContainerDecl *SearchDecl = 0, *CurrentDecl = 0;
3548   bool IsInImplementation = false;
3549   if (Decl *D = IDecl.getAs<Decl>()) {
3550     if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) {
3551       SearchDecl = Impl->getClassInterface();
3552       CurrentDecl = Impl;
3553       IsInImplementation = true;
3554     } else if (ObjCCategoryImplDecl *CatImpl
3555                                        = dyn_cast<ObjCCategoryImplDecl>(D)) {
3556       SearchDecl = CatImpl->getCategoryDecl();
3557       CurrentDecl = CatImpl;
3558       IsInImplementation = true;
3559     } else {
3560       SearchDecl = dyn_cast<ObjCContainerDecl>(D);
3561       CurrentDecl = SearchDecl;
3562     }
3563   }
3564 
3565   if (!SearchDecl && S) {
3566     if (DeclContext *DC = static_cast<DeclContext *>(S->getEntity())) {
3567       SearchDecl = dyn_cast<ObjCContainerDecl>(DC);
3568       CurrentDecl = SearchDecl;
3569     }
3570   }
3571 
3572   if (!SearchDecl || !CurrentDecl) {
3573     HandleCodeCompleteResults(this, CodeCompleter, 0, 0);
3574     return;
3575   }
3576 
3577   // Find all of the methods that we could declare/implement here.
3578   KnownMethodsMap KnownMethods;
3579   FindImplementableMethods(Context, SearchDecl, IsInstanceMethod,
3580                            ReturnType, IsInImplementation, KnownMethods);
3581 
3582   // Erase any methods that have already been declared or
3583   // implemented here.
3584   for (ObjCContainerDecl::method_iterator M = CurrentDecl->meth_begin(),
3585                                        MEnd = CurrentDecl->meth_end();
3586        M != MEnd; ++M) {
3587     if ((*M)->isInstanceMethod() != IsInstanceMethod)
3588       continue;
3589 
3590     KnownMethodsMap::iterator Pos = KnownMethods.find((*M)->getSelector());
3591     if (Pos != KnownMethods.end())
3592       KnownMethods.erase(Pos);
3593   }
3594 
3595   // Add declarations or definitions for each of the known methods.
3596   typedef CodeCompleteConsumer::Result Result;
3597   ResultBuilder Results(*this);
3598   Results.EnterNewScope();
3599   PrintingPolicy Policy(Context.PrintingPolicy);
3600   Policy.AnonymousTagLocations = false;
3601   for (KnownMethodsMap::iterator M = KnownMethods.begin(),
3602                               MEnd = KnownMethods.end();
3603        M != MEnd; ++M) {
3604     ObjCMethodDecl *Method = M->second;
3605     CodeCompletionString *Pattern = new CodeCompletionString;
3606 
3607     // If the result type was not already provided, add it to the
3608     // pattern as (type).
3609     if (ReturnType.isNull()) {
3610       std::string TypeStr;
3611       Method->getResultType().getAsStringInternal(TypeStr, Policy);
3612       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
3613       Pattern->AddTextChunk(TypeStr);
3614       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
3615     }
3616 
3617     Selector Sel = Method->getSelector();
3618 
3619     // Add the first part of the selector to the pattern.
3620     Pattern->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName());
3621 
3622     // Add parameters to the pattern.
3623     unsigned I = 0;
3624     for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
3625                                      PEnd = Method->param_end();
3626          P != PEnd; (void)++P, ++I) {
3627       // Add the part of the selector name.
3628       if (I == 0)
3629         Pattern->AddChunk(CodeCompletionString::CK_Colon);
3630       else if (I < Sel.getNumArgs()) {
3631         Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
3632         Pattern->AddTextChunk(Sel.getIdentifierInfoForSlot(1)->getName());
3633         Pattern->AddChunk(CodeCompletionString::CK_Colon);
3634       } else
3635         break;
3636 
3637       // Add the parameter type.
3638       std::string TypeStr;
3639       (*P)->getOriginalType().getAsStringInternal(TypeStr, Policy);
3640       Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
3641       Pattern->AddTextChunk(TypeStr);
3642       Pattern->AddChunk(CodeCompletionString::CK_RightParen);
3643 
3644       if (IdentifierInfo *Id = (*P)->getIdentifier())
3645         Pattern->AddTextChunk(Id->getName());
3646     }
3647 
3648     if (Method->isVariadic()) {
3649       if (Method->param_size() > 0)
3650         Pattern->AddChunk(CodeCompletionString::CK_Comma);
3651       Pattern->AddTextChunk("...");
3652     }
3653 
3654     if (IsInImplementation) {
3655       // We will be defining the method here, so add a compound statement.
3656       Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
3657       Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
3658       Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
3659       if (!Method->getResultType()->isVoidType()) {
3660         // If the result type is not void, add a return clause.
3661         Pattern->AddTextChunk("return");
3662         Pattern->AddChunk(CodeCompletionString::CK_HorizontalSpace);
3663         Pattern->AddPlaceholderChunk("expression");
3664         Pattern->AddChunk(CodeCompletionString::CK_SemiColon);
3665       } else
3666         Pattern->AddPlaceholderChunk("statements");
3667 
3668       Pattern->AddChunk(CodeCompletionString::CK_VerticalSpace);
3669       Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
3670     }
3671 
3672     Results.AddResult(Result(Pattern));
3673   }
3674 
3675   Results.ExitScope();
3676 
3677   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
3678 }
3679