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 "clang/Sema/SemaInternal.h"
14#include "clang/Sema/Lookup.h"
15#include "clang/Sema/Overload.h"
16#include "clang/Sema/CodeCompleteConsumer.h"
17#include "clang/Sema/ExternalSemaSource.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ScopeInfo.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/AST/ExprObjC.h"
23#include "clang/Lex/HeaderSearch.h"
24#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Preprocessor.h"
26#include "llvm/ADT/DenseSet.h"
27#include "llvm/ADT/SmallBitVector.h"
28#include "llvm/ADT/SmallPtrSet.h"
29#include "llvm/ADT/SmallString.h"
30#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/StringSwitch.h"
32#include "llvm/ADT/Twine.h"
33#include <list>
34#include <map>
35#include <vector>
36
37using namespace clang;
38using namespace sema;
39
40namespace {
41  /// \brief A container of code-completion results.
42  class ResultBuilder {
43  public:
44    /// \brief The type of a name-lookup filter, which can be provided to the
45    /// name-lookup routines to specify which declarations should be included in
46    /// the result set (when it returns true) and which declarations should be
47    /// filtered out (returns false).
48    typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const;
49
50    typedef CodeCompletionResult Result;
51
52  private:
53    /// \brief The actual results we have found.
54    std::vector<Result> Results;
55
56    /// \brief A record of all of the declarations we have found and placed
57    /// into the result set, used to ensure that no declaration ever gets into
58    /// the result set twice.
59    llvm::SmallPtrSet<Decl*, 16> AllDeclsFound;
60
61    typedef std::pair<NamedDecl *, unsigned> DeclIndexPair;
62
63    /// \brief An entry in the shadow map, which is optimized to store
64    /// a single (declaration, index) mapping (the common case) but
65    /// can also store a list of (declaration, index) mappings.
66    class ShadowMapEntry {
67      typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector;
68
69      /// \brief Contains either the solitary NamedDecl * or a vector
70      /// of (declaration, index) pairs.
71      llvm::PointerUnion<NamedDecl *, DeclIndexPairVector*> DeclOrVector;
72
73      /// \brief When the entry contains a single declaration, this is
74      /// the index associated with that entry.
75      unsigned SingleDeclIndex;
76
77    public:
78      ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { }
79
80      void Add(NamedDecl *ND, unsigned Index) {
81        if (DeclOrVector.isNull()) {
82          // 0 - > 1 elements: just set the single element information.
83          DeclOrVector = ND;
84          SingleDeclIndex = Index;
85          return;
86        }
87
88        if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) {
89          // 1 -> 2 elements: create the vector of results and push in the
90          // existing declaration.
91          DeclIndexPairVector *Vec = new DeclIndexPairVector;
92          Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex));
93          DeclOrVector = Vec;
94        }
95
96        // Add the new element to the end of the vector.
97        DeclOrVector.get<DeclIndexPairVector*>()->push_back(
98                                                    DeclIndexPair(ND, Index));
99      }
100
101      void Destroy() {
102        if (DeclIndexPairVector *Vec
103              = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) {
104          delete Vec;
105          DeclOrVector = ((NamedDecl *)0);
106        }
107      }
108
109      // Iteration.
110      class iterator;
111      iterator begin() const;
112      iterator end() const;
113    };
114
115    /// \brief A mapping from declaration names to the declarations that have
116    /// this name within a particular scope and their index within the list of
117    /// results.
118    typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
119
120    /// \brief The semantic analysis object for which results are being
121    /// produced.
122    Sema &SemaRef;
123
124    /// \brief The allocator used to allocate new code-completion strings.
125    CodeCompletionAllocator &Allocator;
126
127    CodeCompletionTUInfo &CCTUInfo;
128
129    /// \brief If non-NULL, a filter function used to remove any code-completion
130    /// results that are not desirable.
131    LookupFilter Filter;
132
133    /// \brief Whether we should allow declarations as
134    /// nested-name-specifiers that would otherwise be filtered out.
135    bool AllowNestedNameSpecifiers;
136
137    /// \brief If set, the type that we would prefer our resulting value
138    /// declarations to have.
139    ///
140    /// Closely matching the preferred type gives a boost to a result's
141    /// priority.
142    CanQualType PreferredType;
143
144    /// \brief A list of shadow maps, which is used to model name hiding at
145    /// different levels of, e.g., the inheritance hierarchy.
146    std::list<ShadowMap> ShadowMaps;
147
148    /// \brief If we're potentially referring to a C++ member function, the set
149    /// of qualifiers applied to the object type.
150    Qualifiers ObjectTypeQualifiers;
151
152    /// \brief Whether the \p ObjectTypeQualifiers field is active.
153    bool HasObjectTypeQualifiers;
154
155    /// \brief The selector that we prefer.
156    Selector PreferredSelector;
157
158    /// \brief The completion context in which we are gathering results.
159    CodeCompletionContext CompletionContext;
160
161    /// \brief If we are in an instance method definition, the \@implementation
162    /// object.
163    ObjCImplementationDecl *ObjCImplementation;
164
165    void AdjustResultPriorityForDecl(Result &R);
166
167    void MaybeAddConstructorResults(Result R);
168
169  public:
170    explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator,
171                           CodeCompletionTUInfo &CCTUInfo,
172                           const CodeCompletionContext &CompletionContext,
173                           LookupFilter Filter = 0)
174      : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo),
175        Filter(Filter),
176        AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false),
177        CompletionContext(CompletionContext),
178        ObjCImplementation(0)
179    {
180      // If this is an Objective-C instance method definition, dig out the
181      // corresponding implementation.
182      switch (CompletionContext.getKind()) {
183      case CodeCompletionContext::CCC_Expression:
184      case CodeCompletionContext::CCC_ObjCMessageReceiver:
185      case CodeCompletionContext::CCC_ParenthesizedExpression:
186      case CodeCompletionContext::CCC_Statement:
187      case CodeCompletionContext::CCC_Recovery:
188        if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl())
189          if (Method->isInstanceMethod())
190            if (ObjCInterfaceDecl *Interface = Method->getClassInterface())
191              ObjCImplementation = Interface->getImplementation();
192        break;
193
194      default:
195        break;
196      }
197    }
198
199    /// \brief Whether we should include code patterns in the completion
200    /// results.
201    bool includeCodePatterns() const {
202      return SemaRef.CodeCompleter &&
203             SemaRef.CodeCompleter->includeCodePatterns();
204    }
205
206    /// \brief Set the filter used for code-completion results.
207    void setFilter(LookupFilter Filter) {
208      this->Filter = Filter;
209    }
210
211    Result *data() { return Results.empty()? 0 : &Results.front(); }
212    unsigned size() const { return Results.size(); }
213    bool empty() const { return Results.empty(); }
214
215    /// \brief Specify the preferred type.
216    void setPreferredType(QualType T) {
217      PreferredType = SemaRef.Context.getCanonicalType(T);
218    }
219
220    /// \brief Set the cv-qualifiers on the object type, for us in filtering
221    /// calls to member functions.
222    ///
223    /// When there are qualifiers in this set, they will be used to filter
224    /// out member functions that aren't available (because there will be a
225    /// cv-qualifier mismatch) or prefer functions with an exact qualifier
226    /// match.
227    void setObjectTypeQualifiers(Qualifiers Quals) {
228      ObjectTypeQualifiers = Quals;
229      HasObjectTypeQualifiers = true;
230    }
231
232    /// \brief Set the preferred selector.
233    ///
234    /// When an Objective-C method declaration result is added, and that
235    /// method's selector matches this preferred selector, we give that method
236    /// a slight priority boost.
237    void setPreferredSelector(Selector Sel) {
238      PreferredSelector = Sel;
239    }
240
241    /// \brief Retrieve the code-completion context for which results are
242    /// being collected.
243    const CodeCompletionContext &getCompletionContext() const {
244      return CompletionContext;
245    }
246
247    /// \brief Specify whether nested-name-specifiers are allowed.
248    void allowNestedNameSpecifiers(bool Allow = true) {
249      AllowNestedNameSpecifiers = Allow;
250    }
251
252    /// \brief Return the semantic analysis object for which we are collecting
253    /// code completion results.
254    Sema &getSema() const { return SemaRef; }
255
256    /// \brief Retrieve the allocator used to allocate code completion strings.
257    CodeCompletionAllocator &getAllocator() const { return Allocator; }
258
259    CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
260
261    /// \brief Determine whether the given declaration is at all interesting
262    /// as a code-completion result.
263    ///
264    /// \param ND the declaration that we are inspecting.
265    ///
266    /// \param AsNestedNameSpecifier will be set true if this declaration is
267    /// only interesting when it is a nested-name-specifier.
268    bool isInterestingDecl(NamedDecl *ND, bool &AsNestedNameSpecifier) const;
269
270    /// \brief Check whether the result is hidden by the Hiding declaration.
271    ///
272    /// \returns true if the result is hidden and cannot be found, false if
273    /// the hidden result could still be found. When false, \p R may be
274    /// modified to describe how the result can be found (e.g., via extra
275    /// qualification).
276    bool CheckHiddenResult(Result &R, DeclContext *CurContext,
277                           NamedDecl *Hiding);
278
279    /// \brief Add a new result to this result set (if it isn't already in one
280    /// of the shadow maps), or replace an existing result (for, e.g., a
281    /// redeclaration).
282    ///
283    /// \param R the result to add (if it is unique).
284    ///
285    /// \param CurContext the context in which this result will be named.
286    void MaybeAddResult(Result R, DeclContext *CurContext = 0);
287
288    /// \brief Add a new result to this result set, where we already know
289    /// the hiding declation (if any).
290    ///
291    /// \param R the result to add (if it is unique).
292    ///
293    /// \param CurContext the context in which this result will be named.
294    ///
295    /// \param Hiding the declaration that hides the result.
296    ///
297    /// \param InBaseClass whether the result was found in a base
298    /// class of the searched context.
299    void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding,
300                   bool InBaseClass);
301
302    /// \brief Add a new non-declaration result to this result set.
303    void AddResult(Result R);
304
305    /// \brief Enter into a new scope.
306    void EnterNewScope();
307
308    /// \brief Exit from the current scope.
309    void ExitScope();
310
311    /// \brief Ignore this declaration, if it is seen again.
312    void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); }
313
314    /// \name Name lookup predicates
315    ///
316    /// These predicates can be passed to the name lookup functions to filter the
317    /// results of name lookup. All of the predicates have the same type, so that
318    ///
319    //@{
320    bool IsOrdinaryName(NamedDecl *ND) const;
321    bool IsOrdinaryNonTypeName(NamedDecl *ND) const;
322    bool IsIntegralConstantValue(NamedDecl *ND) const;
323    bool IsOrdinaryNonValueName(NamedDecl *ND) const;
324    bool IsNestedNameSpecifier(NamedDecl *ND) const;
325    bool IsEnum(NamedDecl *ND) const;
326    bool IsClassOrStruct(NamedDecl *ND) const;
327    bool IsUnion(NamedDecl *ND) const;
328    bool IsNamespace(NamedDecl *ND) const;
329    bool IsNamespaceOrAlias(NamedDecl *ND) const;
330    bool IsType(NamedDecl *ND) const;
331    bool IsMember(NamedDecl *ND) const;
332    bool IsObjCIvar(NamedDecl *ND) const;
333    bool IsObjCMessageReceiver(NamedDecl *ND) const;
334    bool IsObjCMessageReceiverOrLambdaCapture(NamedDecl *ND) const;
335    bool IsObjCCollection(NamedDecl *ND) const;
336    bool IsImpossibleToSatisfy(NamedDecl *ND) const;
337    //@}
338  };
339}
340
341class ResultBuilder::ShadowMapEntry::iterator {
342  llvm::PointerUnion<NamedDecl*, const DeclIndexPair*> DeclOrIterator;
343  unsigned SingleDeclIndex;
344
345public:
346  typedef DeclIndexPair value_type;
347  typedef value_type reference;
348  typedef std::ptrdiff_t difference_type;
349  typedef std::input_iterator_tag iterator_category;
350
351  class pointer {
352    DeclIndexPair Value;
353
354  public:
355    pointer(const DeclIndexPair &Value) : Value(Value) { }
356
357    const DeclIndexPair *operator->() const {
358      return &Value;
359    }
360  };
361
362  iterator() : DeclOrIterator((NamedDecl *)0), SingleDeclIndex(0) { }
363
364  iterator(NamedDecl *SingleDecl, unsigned Index)
365    : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { }
366
367  iterator(const DeclIndexPair *Iterator)
368    : DeclOrIterator(Iterator), SingleDeclIndex(0) { }
369
370  iterator &operator++() {
371    if (DeclOrIterator.is<NamedDecl *>()) {
372      DeclOrIterator = (NamedDecl *)0;
373      SingleDeclIndex = 0;
374      return *this;
375    }
376
377    const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>();
378    ++I;
379    DeclOrIterator = I;
380    return *this;
381  }
382
383  /*iterator operator++(int) {
384    iterator tmp(*this);
385    ++(*this);
386    return tmp;
387  }*/
388
389  reference operator*() const {
390    if (NamedDecl *ND = DeclOrIterator.dyn_cast<NamedDecl *>())
391      return reference(ND, SingleDeclIndex);
392
393    return *DeclOrIterator.get<const DeclIndexPair*>();
394  }
395
396  pointer operator->() const {
397    return pointer(**this);
398  }
399
400  friend bool operator==(const iterator &X, const iterator &Y) {
401    return X.DeclOrIterator.getOpaqueValue()
402                                  == Y.DeclOrIterator.getOpaqueValue() &&
403      X.SingleDeclIndex == Y.SingleDeclIndex;
404  }
405
406  friend bool operator!=(const iterator &X, const iterator &Y) {
407    return !(X == Y);
408  }
409};
410
411ResultBuilder::ShadowMapEntry::iterator
412ResultBuilder::ShadowMapEntry::begin() const {
413  if (DeclOrVector.isNull())
414    return iterator();
415
416  if (NamedDecl *ND = DeclOrVector.dyn_cast<NamedDecl *>())
417    return iterator(ND, SingleDeclIndex);
418
419  return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin());
420}
421
422ResultBuilder::ShadowMapEntry::iterator
423ResultBuilder::ShadowMapEntry::end() const {
424  if (DeclOrVector.is<NamedDecl *>() || DeclOrVector.isNull())
425    return iterator();
426
427  return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end());
428}
429
430/// \brief Compute the qualification required to get from the current context
431/// (\p CurContext) to the target context (\p TargetContext).
432///
433/// \param Context the AST context in which the qualification will be used.
434///
435/// \param CurContext the context where an entity is being named, which is
436/// typically based on the current scope.
437///
438/// \param TargetContext the context in which the named entity actually
439/// resides.
440///
441/// \returns a nested name specifier that refers into the target context, or
442/// NULL if no qualification is needed.
443static NestedNameSpecifier *
444getRequiredQualification(ASTContext &Context,
445                         DeclContext *CurContext,
446                         DeclContext *TargetContext) {
447  SmallVector<DeclContext *, 4> TargetParents;
448
449  for (DeclContext *CommonAncestor = TargetContext;
450       CommonAncestor && !CommonAncestor->Encloses(CurContext);
451       CommonAncestor = CommonAncestor->getLookupParent()) {
452    if (CommonAncestor->isTransparentContext() ||
453        CommonAncestor->isFunctionOrMethod())
454      continue;
455
456    TargetParents.push_back(CommonAncestor);
457  }
458
459  NestedNameSpecifier *Result = 0;
460  while (!TargetParents.empty()) {
461    DeclContext *Parent = TargetParents.back();
462    TargetParents.pop_back();
463
464    if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) {
465      if (!Namespace->getIdentifier())
466        continue;
467
468      Result = NestedNameSpecifier::Create(Context, Result, Namespace);
469    }
470    else if (TagDecl *TD = dyn_cast<TagDecl>(Parent))
471      Result = NestedNameSpecifier::Create(Context, Result,
472                                           false,
473                                     Context.getTypeDeclType(TD).getTypePtr());
474  }
475  return Result;
476}
477
478bool ResultBuilder::isInterestingDecl(NamedDecl *ND,
479                                      bool &AsNestedNameSpecifier) const {
480  AsNestedNameSpecifier = false;
481
482  ND = ND->getUnderlyingDecl();
483  unsigned IDNS = ND->getIdentifierNamespace();
484
485  // Skip unnamed entities.
486  if (!ND->getDeclName())
487    return false;
488
489  // Friend declarations and declarations introduced due to friends are never
490  // added as results.
491  if (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend))
492    return false;
493
494  // Class template (partial) specializations are never added as results.
495  if (isa<ClassTemplateSpecializationDecl>(ND) ||
496      isa<ClassTemplatePartialSpecializationDecl>(ND))
497    return false;
498
499  // Using declarations themselves are never added as results.
500  if (isa<UsingDecl>(ND))
501    return false;
502
503  // Some declarations have reserved names that we don't want to ever show.
504  if (const IdentifierInfo *Id = ND->getIdentifier()) {
505    // __va_list_tag is a freak of nature. Find it and skip it.
506    if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list"))
507      return false;
508
509    // Filter out names reserved for the implementation (C99 7.1.3,
510    // C++ [lib.global.names]) if they come from a system header.
511    //
512    // FIXME: Add predicate for this.
513    if (Id->getLength() >= 2) {
514      const char *Name = Id->getNameStart();
515      if (Name[0] == '_' &&
516          (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')) &&
517          (ND->getLocation().isInvalid() ||
518           SemaRef.SourceMgr.isInSystemHeader(
519                          SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))))
520        return false;
521    }
522  }
523
524  if (Filter == &ResultBuilder::IsNestedNameSpecifier ||
525      ((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
526       Filter != &ResultBuilder::IsNamespace &&
527       Filter != &ResultBuilder::IsNamespaceOrAlias &&
528       Filter != 0))
529    AsNestedNameSpecifier = true;
530
531  // Filter out any unwanted results.
532  if (Filter && !(this->*Filter)(ND)) {
533    // Check whether it is interesting as a nested-name-specifier.
534    if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus &&
535        IsNestedNameSpecifier(ND) &&
536        (Filter != &ResultBuilder::IsMember ||
537         (isa<CXXRecordDecl>(ND) &&
538          cast<CXXRecordDecl>(ND)->isInjectedClassName()))) {
539      AsNestedNameSpecifier = true;
540      return true;
541    }
542
543    return false;
544  }
545  // ... then it must be interesting!
546  return true;
547}
548
549bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext,
550                                      NamedDecl *Hiding) {
551  // In C, there is no way to refer to a hidden name.
552  // FIXME: This isn't true; we can find a tag name hidden by an ordinary
553  // name if we introduce the tag type.
554  if (!SemaRef.getLangOpts().CPlusPlus)
555    return true;
556
557  DeclContext *HiddenCtx = R.Declaration->getDeclContext()->getRedeclContext();
558
559  // There is no way to qualify a name declared in a function or method.
560  if (HiddenCtx->isFunctionOrMethod())
561    return true;
562
563  if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext())
564    return true;
565
566  // We can refer to the result with the appropriate qualification. Do it.
567  R.Hidden = true;
568  R.QualifierIsInformative = false;
569
570  if (!R.Qualifier)
571    R.Qualifier = getRequiredQualification(SemaRef.Context,
572                                           CurContext,
573                                           R.Declaration->getDeclContext());
574  return false;
575}
576
577/// \brief A simplified classification of types used to determine whether two
578/// types are "similar enough" when adjusting priorities.
579SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) {
580  switch (T->getTypeClass()) {
581  case Type::Builtin:
582    switch (cast<BuiltinType>(T)->getKind()) {
583      case BuiltinType::Void:
584        return STC_Void;
585
586      case BuiltinType::NullPtr:
587        return STC_Pointer;
588
589      case BuiltinType::Overload:
590      case BuiltinType::Dependent:
591        return STC_Other;
592
593      case BuiltinType::ObjCId:
594      case BuiltinType::ObjCClass:
595      case BuiltinType::ObjCSel:
596        return STC_ObjectiveC;
597
598      default:
599        return STC_Arithmetic;
600    }
601
602  case Type::Complex:
603    return STC_Arithmetic;
604
605  case Type::Pointer:
606    return STC_Pointer;
607
608  case Type::BlockPointer:
609    return STC_Block;
610
611  case Type::LValueReference:
612  case Type::RValueReference:
613    return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType());
614
615  case Type::ConstantArray:
616  case Type::IncompleteArray:
617  case Type::VariableArray:
618  case Type::DependentSizedArray:
619    return STC_Array;
620
621  case Type::DependentSizedExtVector:
622  case Type::Vector:
623  case Type::ExtVector:
624    return STC_Arithmetic;
625
626  case Type::FunctionProto:
627  case Type::FunctionNoProto:
628    return STC_Function;
629
630  case Type::Record:
631    return STC_Record;
632
633  case Type::Enum:
634    return STC_Arithmetic;
635
636  case Type::ObjCObject:
637  case Type::ObjCInterface:
638  case Type::ObjCObjectPointer:
639    return STC_ObjectiveC;
640
641  default:
642    return STC_Other;
643  }
644}
645
646/// \brief Get the type that a given expression will have if this declaration
647/// is used as an expression in its "typical" code-completion form.
648QualType clang::getDeclUsageType(ASTContext &C, NamedDecl *ND) {
649  ND = cast<NamedDecl>(ND->getUnderlyingDecl());
650
651  if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
652    return C.getTypeDeclType(Type);
653  if (ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND))
654    return C.getObjCInterfaceType(Iface);
655
656  QualType T;
657  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND))
658    T = Function->getCallResultType();
659  else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
660    T = Method->getSendResultType();
661  else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND))
662    T = FunTmpl->getTemplatedDecl()->getCallResultType();
663  else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND))
664    T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext()));
665  else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND))
666    T = Property->getType();
667  else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND))
668    T = Value->getType();
669  else
670    return QualType();
671
672  // Dig through references, function pointers, and block pointers to
673  // get down to the likely type of an expression when the entity is
674  // used.
675  do {
676    if (const ReferenceType *Ref = T->getAs<ReferenceType>()) {
677      T = Ref->getPointeeType();
678      continue;
679    }
680
681    if (const PointerType *Pointer = T->getAs<PointerType>()) {
682      if (Pointer->getPointeeType()->isFunctionType()) {
683        T = Pointer->getPointeeType();
684        continue;
685      }
686
687      break;
688    }
689
690    if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) {
691      T = Block->getPointeeType();
692      continue;
693    }
694
695    if (const FunctionType *Function = T->getAs<FunctionType>()) {
696      T = Function->getResultType();
697      continue;
698    }
699
700    break;
701  } while (true);
702
703  return T;
704}
705
706void ResultBuilder::AdjustResultPriorityForDecl(Result &R) {
707  // If this is an Objective-C method declaration whose selector matches our
708  // preferred selector, give it a priority boost.
709  if (!PreferredSelector.isNull())
710    if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration))
711      if (PreferredSelector == Method->getSelector())
712        R.Priority += CCD_SelectorMatch;
713
714  // If we have a preferred type, adjust the priority for results with exactly-
715  // matching or nearly-matching types.
716  if (!PreferredType.isNull()) {
717    QualType T = getDeclUsageType(SemaRef.Context, R.Declaration);
718    if (!T.isNull()) {
719      CanQualType TC = SemaRef.Context.getCanonicalType(T);
720      // Check for exactly-matching types (modulo qualifiers).
721      if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC))
722        R.Priority /= CCF_ExactTypeMatch;
723      // Check for nearly-matching types, based on classification of each.
724      else if ((getSimplifiedTypeClass(PreferredType)
725                                               == getSimplifiedTypeClass(TC)) &&
726               !(PreferredType->isEnumeralType() && TC->isEnumeralType()))
727        R.Priority /= CCF_SimilarTypeMatch;
728    }
729  }
730}
731
732void ResultBuilder::MaybeAddConstructorResults(Result R) {
733  if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration ||
734      !CompletionContext.wantConstructorResults())
735    return;
736
737  ASTContext &Context = SemaRef.Context;
738  NamedDecl *D = R.Declaration;
739  CXXRecordDecl *Record = 0;
740  if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D))
741    Record = ClassTemplate->getTemplatedDecl();
742  else if ((Record = dyn_cast<CXXRecordDecl>(D))) {
743    // Skip specializations and partial specializations.
744    if (isa<ClassTemplateSpecializationDecl>(Record))
745      return;
746  } else {
747    // There are no constructors here.
748    return;
749  }
750
751  Record = Record->getDefinition();
752  if (!Record)
753    return;
754
755
756  QualType RecordTy = Context.getTypeDeclType(Record);
757  DeclarationName ConstructorName
758    = Context.DeclarationNames.getCXXConstructorName(
759                                           Context.getCanonicalType(RecordTy));
760  for (DeclContext::lookup_result Ctors = Record->lookup(ConstructorName);
761       Ctors.first != Ctors.second; ++Ctors.first) {
762    R.Declaration = *Ctors.first;
763    R.CursorKind = getCursorKindForDecl(R.Declaration);
764    Results.push_back(R);
765  }
766}
767
768void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) {
769  assert(!ShadowMaps.empty() && "Must enter into a results scope");
770
771  if (R.Kind != Result::RK_Declaration) {
772    // For non-declaration results, just add the result.
773    Results.push_back(R);
774    return;
775  }
776
777  // Look through using declarations.
778  if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
779    MaybeAddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext);
780    return;
781  }
782
783  Decl *CanonDecl = R.Declaration->getCanonicalDecl();
784  unsigned IDNS = CanonDecl->getIdentifierNamespace();
785
786  bool AsNestedNameSpecifier = false;
787  if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
788    return;
789
790  // C++ constructors are never found by name lookup.
791  if (isa<CXXConstructorDecl>(R.Declaration))
792    return;
793
794  ShadowMap &SMap = ShadowMaps.back();
795  ShadowMapEntry::iterator I, IEnd;
796  ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName());
797  if (NamePos != SMap.end()) {
798    I = NamePos->second.begin();
799    IEnd = NamePos->second.end();
800  }
801
802  for (; I != IEnd; ++I) {
803    NamedDecl *ND = I->first;
804    unsigned Index = I->second;
805    if (ND->getCanonicalDecl() == CanonDecl) {
806      // This is a redeclaration. Always pick the newer declaration.
807      Results[Index].Declaration = R.Declaration;
808
809      // We're done.
810      return;
811    }
812  }
813
814  // This is a new declaration in this scope. However, check whether this
815  // declaration name is hidden by a similarly-named declaration in an outer
816  // scope.
817  std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end();
818  --SMEnd;
819  for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) {
820    ShadowMapEntry::iterator I, IEnd;
821    ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName());
822    if (NamePos != SM->end()) {
823      I = NamePos->second.begin();
824      IEnd = NamePos->second.end();
825    }
826    for (; I != IEnd; ++I) {
827      // A tag declaration does not hide a non-tag declaration.
828      if (I->first->hasTagIdentifierNamespace() &&
829          (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary |
830                   Decl::IDNS_ObjCProtocol)))
831        continue;
832
833      // Protocols are in distinct namespaces from everything else.
834      if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol)
835           || (IDNS & Decl::IDNS_ObjCProtocol)) &&
836          I->first->getIdentifierNamespace() != IDNS)
837        continue;
838
839      // The newly-added result is hidden by an entry in the shadow map.
840      if (CheckHiddenResult(R, CurContext, I->first))
841        return;
842
843      break;
844    }
845  }
846
847  // Make sure that any given declaration only shows up in the result set once.
848  if (!AllDeclsFound.insert(CanonDecl))
849    return;
850
851  // If the filter is for nested-name-specifiers, then this result starts a
852  // nested-name-specifier.
853  if (AsNestedNameSpecifier) {
854    R.StartsNestedNameSpecifier = true;
855    R.Priority = CCP_NestedNameSpecifier;
856  } else
857      AdjustResultPriorityForDecl(R);
858
859  // If this result is supposed to have an informative qualifier, add one.
860  if (R.QualifierIsInformative && !R.Qualifier &&
861      !R.StartsNestedNameSpecifier) {
862    DeclContext *Ctx = R.Declaration->getDeclContext();
863    if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
864      R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
865    else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
866      R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
867                             SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
868    else
869      R.QualifierIsInformative = false;
870  }
871
872  // Insert this result into the set of results and into the current shadow
873  // map.
874  SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size());
875  Results.push_back(R);
876
877  if (!AsNestedNameSpecifier)
878    MaybeAddConstructorResults(R);
879}
880
881void ResultBuilder::AddResult(Result R, DeclContext *CurContext,
882                              NamedDecl *Hiding, bool InBaseClass = false) {
883  if (R.Kind != Result::RK_Declaration) {
884    // For non-declaration results, just add the result.
885    Results.push_back(R);
886    return;
887  }
888
889  // Look through using declarations.
890  if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
891    AddResult(Result(Using->getTargetDecl(), R.Qualifier), CurContext, Hiding);
892    return;
893  }
894
895  bool AsNestedNameSpecifier = false;
896  if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
897    return;
898
899  // C++ constructors are never found by name lookup.
900  if (isa<CXXConstructorDecl>(R.Declaration))
901    return;
902
903  if (Hiding && CheckHiddenResult(R, CurContext, Hiding))
904    return;
905
906  // Make sure that any given declaration only shows up in the result set once.
907  if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()))
908    return;
909
910  // If the filter is for nested-name-specifiers, then this result starts a
911  // nested-name-specifier.
912  if (AsNestedNameSpecifier) {
913    R.StartsNestedNameSpecifier = true;
914    R.Priority = CCP_NestedNameSpecifier;
915  }
916  else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass &&
917           isa<CXXRecordDecl>(R.Declaration->getDeclContext()
918                                                  ->getRedeclContext()))
919    R.QualifierIsInformative = true;
920
921  // If this result is supposed to have an informative qualifier, add one.
922  if (R.QualifierIsInformative && !R.Qualifier &&
923      !R.StartsNestedNameSpecifier) {
924    DeclContext *Ctx = R.Declaration->getDeclContext();
925    if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
926      R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
927    else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
928      R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
929                            SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
930    else
931      R.QualifierIsInformative = false;
932  }
933
934  // Adjust the priority if this result comes from a base class.
935  if (InBaseClass)
936    R.Priority += CCD_InBaseClass;
937
938  AdjustResultPriorityForDecl(R);
939
940  if (HasObjectTypeQualifiers)
941    if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration))
942      if (Method->isInstance()) {
943        Qualifiers MethodQuals
944                        = Qualifiers::fromCVRMask(Method->getTypeQualifiers());
945        if (ObjectTypeQualifiers == MethodQuals)
946          R.Priority += CCD_ObjectQualifierMatch;
947        else if (ObjectTypeQualifiers - MethodQuals) {
948          // The method cannot be invoked, because doing so would drop
949          // qualifiers.
950          return;
951        }
952      }
953
954  // Insert this result into the set of results.
955  Results.push_back(R);
956
957  if (!AsNestedNameSpecifier)
958    MaybeAddConstructorResults(R);
959}
960
961void ResultBuilder::AddResult(Result R) {
962  assert(R.Kind != Result::RK_Declaration &&
963          "Declaration results need more context");
964  Results.push_back(R);
965}
966
967/// \brief Enter into a new scope.
968void ResultBuilder::EnterNewScope() {
969  ShadowMaps.push_back(ShadowMap());
970}
971
972/// \brief Exit from the current scope.
973void ResultBuilder::ExitScope() {
974  for (ShadowMap::iterator E = ShadowMaps.back().begin(),
975                        EEnd = ShadowMaps.back().end();
976       E != EEnd;
977       ++E)
978    E->second.Destroy();
979
980  ShadowMaps.pop_back();
981}
982
983/// \brief Determines whether this given declaration will be found by
984/// ordinary name lookup.
985bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const {
986  ND = cast<NamedDecl>(ND->getUnderlyingDecl());
987
988  unsigned IDNS = Decl::IDNS_Ordinary;
989  if (SemaRef.getLangOpts().CPlusPlus)
990    IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member;
991  else if (SemaRef.getLangOpts().ObjC1) {
992    if (isa<ObjCIvarDecl>(ND))
993      return true;
994  }
995
996  return ND->getIdentifierNamespace() & IDNS;
997}
998
999/// \brief Determines whether this given declaration will be found by
1000/// ordinary name lookup but is not a type name.
1001bool ResultBuilder::IsOrdinaryNonTypeName(NamedDecl *ND) const {
1002  ND = cast<NamedDecl>(ND->getUnderlyingDecl());
1003  if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
1004    return false;
1005
1006  unsigned IDNS = Decl::IDNS_Ordinary;
1007  if (SemaRef.getLangOpts().CPlusPlus)
1008    IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member;
1009  else if (SemaRef.getLangOpts().ObjC1) {
1010    if (isa<ObjCIvarDecl>(ND))
1011      return true;
1012  }
1013
1014  return ND->getIdentifierNamespace() & IDNS;
1015}
1016
1017bool ResultBuilder::IsIntegralConstantValue(NamedDecl *ND) const {
1018  if (!IsOrdinaryNonTypeName(ND))
1019    return 0;
1020
1021  if (ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl()))
1022    if (VD->getType()->isIntegralOrEnumerationType())
1023      return true;
1024
1025  return false;
1026}
1027
1028/// \brief Determines whether this given declaration will be found by
1029/// ordinary name lookup.
1030bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const {
1031  ND = cast<NamedDecl>(ND->getUnderlyingDecl());
1032
1033  unsigned IDNS = Decl::IDNS_Ordinary;
1034  if (SemaRef.getLangOpts().CPlusPlus)
1035    IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace;
1036
1037  return (ND->getIdentifierNamespace() & IDNS) &&
1038    !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) &&
1039    !isa<ObjCPropertyDecl>(ND);
1040}
1041
1042/// \brief Determines whether the given declaration is suitable as the
1043/// start of a C++ nested-name-specifier, e.g., a class or namespace.
1044bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const {
1045  // Allow us to find class templates, too.
1046  if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
1047    ND = ClassTemplate->getTemplatedDecl();
1048
1049  return SemaRef.isAcceptableNestedNameSpecifier(ND);
1050}
1051
1052/// \brief Determines whether the given declaration is an enumeration.
1053bool ResultBuilder::IsEnum(NamedDecl *ND) const {
1054  return isa<EnumDecl>(ND);
1055}
1056
1057/// \brief Determines whether the given declaration is a class or struct.
1058bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const {
1059  // Allow us to find class templates, too.
1060  if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
1061    ND = ClassTemplate->getTemplatedDecl();
1062
1063  // For purposes of this check, interfaces match too.
1064  if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
1065    return RD->getTagKind() == TTK_Class ||
1066    RD->getTagKind() == TTK_Struct ||
1067    RD->getTagKind() == TTK_Interface;
1068
1069  return false;
1070}
1071
1072/// \brief Determines whether the given declaration is a union.
1073bool ResultBuilder::IsUnion(NamedDecl *ND) const {
1074  // Allow us to find class templates, too.
1075  if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
1076    ND = ClassTemplate->getTemplatedDecl();
1077
1078  if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
1079    return RD->getTagKind() == TTK_Union;
1080
1081  return false;
1082}
1083
1084/// \brief Determines whether the given declaration is a namespace.
1085bool ResultBuilder::IsNamespace(NamedDecl *ND) const {
1086  return isa<NamespaceDecl>(ND);
1087}
1088
1089/// \brief Determines whether the given declaration is a namespace or
1090/// namespace alias.
1091bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const {
1092  return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
1093}
1094
1095/// \brief Determines whether the given declaration is a type.
1096bool ResultBuilder::IsType(NamedDecl *ND) const {
1097  if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND))
1098    ND = Using->getTargetDecl();
1099
1100  return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND);
1101}
1102
1103/// \brief Determines which members of a class should be visible via
1104/// "." or "->".  Only value declarations, nested name specifiers, and
1105/// using declarations thereof should show up.
1106bool ResultBuilder::IsMember(NamedDecl *ND) const {
1107  if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND))
1108    ND = Using->getTargetDecl();
1109
1110  return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
1111    isa<ObjCPropertyDecl>(ND);
1112}
1113
1114static bool isObjCReceiverType(ASTContext &C, QualType T) {
1115  T = C.getCanonicalType(T);
1116  switch (T->getTypeClass()) {
1117  case Type::ObjCObject:
1118  case Type::ObjCInterface:
1119  case Type::ObjCObjectPointer:
1120    return true;
1121
1122  case Type::Builtin:
1123    switch (cast<BuiltinType>(T)->getKind()) {
1124    case BuiltinType::ObjCId:
1125    case BuiltinType::ObjCClass:
1126    case BuiltinType::ObjCSel:
1127      return true;
1128
1129    default:
1130      break;
1131    }
1132    return false;
1133
1134  default:
1135    break;
1136  }
1137
1138  if (!C.getLangOpts().CPlusPlus)
1139    return false;
1140
1141  // FIXME: We could perform more analysis here to determine whether a
1142  // particular class type has any conversions to Objective-C types. For now,
1143  // just accept all class types.
1144  return T->isDependentType() || T->isRecordType();
1145}
1146
1147bool ResultBuilder::IsObjCMessageReceiver(NamedDecl *ND) const {
1148  QualType T = getDeclUsageType(SemaRef.Context, ND);
1149  if (T.isNull())
1150    return false;
1151
1152  T = SemaRef.Context.getBaseElementType(T);
1153  return isObjCReceiverType(SemaRef.Context, T);
1154}
1155
1156bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(NamedDecl *ND) const {
1157  if (IsObjCMessageReceiver(ND))
1158    return true;
1159
1160  VarDecl *Var = dyn_cast<VarDecl>(ND);
1161  if (!Var)
1162    return false;
1163
1164  return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>();
1165}
1166
1167bool ResultBuilder::IsObjCCollection(NamedDecl *ND) const {
1168  if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) ||
1169      (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND)))
1170    return false;
1171
1172  QualType T = getDeclUsageType(SemaRef.Context, ND);
1173  if (T.isNull())
1174    return false;
1175
1176  T = SemaRef.Context.getBaseElementType(T);
1177  return T->isObjCObjectType() || T->isObjCObjectPointerType() ||
1178         T->isObjCIdType() ||
1179         (SemaRef.getLangOpts().CPlusPlus && T->isRecordType());
1180}
1181
1182bool ResultBuilder::IsImpossibleToSatisfy(NamedDecl *ND) const {
1183  return false;
1184}
1185
1186/// \brief Determines whether the given declaration is an Objective-C
1187/// instance variable.
1188bool ResultBuilder::IsObjCIvar(NamedDecl *ND) const {
1189  return isa<ObjCIvarDecl>(ND);
1190}
1191
1192namespace {
1193  /// \brief Visible declaration consumer that adds a code-completion result
1194  /// for each visible declaration.
1195  class CodeCompletionDeclConsumer : public VisibleDeclConsumer {
1196    ResultBuilder &Results;
1197    DeclContext *CurContext;
1198
1199  public:
1200    CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext)
1201      : Results(Results), CurContext(CurContext) { }
1202
1203    virtual void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
1204                           bool InBaseClass) {
1205      bool Accessible = true;
1206      if (Ctx)
1207        Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx);
1208
1209      ResultBuilder::Result Result(ND, 0, false, Accessible);
1210      Results.AddResult(Result, CurContext, Hiding, InBaseClass);
1211    }
1212  };
1213}
1214
1215/// \brief Add type specifiers for the current language as keyword results.
1216static void AddTypeSpecifierResults(const LangOptions &LangOpts,
1217                                    ResultBuilder &Results) {
1218  typedef CodeCompletionResult Result;
1219  Results.AddResult(Result("short", CCP_Type));
1220  Results.AddResult(Result("long", CCP_Type));
1221  Results.AddResult(Result("signed", CCP_Type));
1222  Results.AddResult(Result("unsigned", CCP_Type));
1223  Results.AddResult(Result("void", CCP_Type));
1224  Results.AddResult(Result("char", CCP_Type));
1225  Results.AddResult(Result("int", CCP_Type));
1226  Results.AddResult(Result("float", CCP_Type));
1227  Results.AddResult(Result("double", CCP_Type));
1228  Results.AddResult(Result("enum", CCP_Type));
1229  Results.AddResult(Result("struct", CCP_Type));
1230  Results.AddResult(Result("union", CCP_Type));
1231  Results.AddResult(Result("const", CCP_Type));
1232  Results.AddResult(Result("volatile", CCP_Type));
1233
1234  if (LangOpts.C99) {
1235    // C99-specific
1236    Results.AddResult(Result("_Complex", CCP_Type));
1237    Results.AddResult(Result("_Imaginary", CCP_Type));
1238    Results.AddResult(Result("_Bool", CCP_Type));
1239    Results.AddResult(Result("restrict", CCP_Type));
1240  }
1241
1242  CodeCompletionBuilder Builder(Results.getAllocator(),
1243                                Results.getCodeCompletionTUInfo());
1244  if (LangOpts.CPlusPlus) {
1245    // C++-specific
1246    Results.AddResult(Result("bool", CCP_Type +
1247                             (LangOpts.ObjC1? CCD_bool_in_ObjC : 0)));
1248    Results.AddResult(Result("class", CCP_Type));
1249    Results.AddResult(Result("wchar_t", CCP_Type));
1250
1251    // typename qualified-id
1252    Builder.AddTypedTextChunk("typename");
1253    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1254    Builder.AddPlaceholderChunk("qualifier");
1255    Builder.AddTextChunk("::");
1256    Builder.AddPlaceholderChunk("name");
1257    Results.AddResult(Result(Builder.TakeString()));
1258
1259    if (LangOpts.CPlusPlus0x) {
1260      Results.AddResult(Result("auto", CCP_Type));
1261      Results.AddResult(Result("char16_t", CCP_Type));
1262      Results.AddResult(Result("char32_t", CCP_Type));
1263
1264      Builder.AddTypedTextChunk("decltype");
1265      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1266      Builder.AddPlaceholderChunk("expression");
1267      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1268      Results.AddResult(Result(Builder.TakeString()));
1269    }
1270  }
1271
1272  // GNU extensions
1273  if (LangOpts.GNUMode) {
1274    // FIXME: Enable when we actually support decimal floating point.
1275    //    Results.AddResult(Result("_Decimal32"));
1276    //    Results.AddResult(Result("_Decimal64"));
1277    //    Results.AddResult(Result("_Decimal128"));
1278
1279    Builder.AddTypedTextChunk("typeof");
1280    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1281    Builder.AddPlaceholderChunk("expression");
1282    Results.AddResult(Result(Builder.TakeString()));
1283
1284    Builder.AddTypedTextChunk("typeof");
1285    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1286    Builder.AddPlaceholderChunk("type");
1287    Builder.AddChunk(CodeCompletionString::CK_RightParen);
1288    Results.AddResult(Result(Builder.TakeString()));
1289  }
1290}
1291
1292static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC,
1293                                 const LangOptions &LangOpts,
1294                                 ResultBuilder &Results) {
1295  typedef CodeCompletionResult Result;
1296  // Note: we don't suggest either "auto" or "register", because both
1297  // are pointless as storage specifiers. Elsewhere, we suggest "auto"
1298  // in C++0x as a type specifier.
1299  Results.AddResult(Result("extern"));
1300  Results.AddResult(Result("static"));
1301}
1302
1303static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC,
1304                                  const LangOptions &LangOpts,
1305                                  ResultBuilder &Results) {
1306  typedef CodeCompletionResult Result;
1307  switch (CCC) {
1308  case Sema::PCC_Class:
1309  case Sema::PCC_MemberTemplate:
1310    if (LangOpts.CPlusPlus) {
1311      Results.AddResult(Result("explicit"));
1312      Results.AddResult(Result("friend"));
1313      Results.AddResult(Result("mutable"));
1314      Results.AddResult(Result("virtual"));
1315    }
1316    // Fall through
1317
1318  case Sema::PCC_ObjCInterface:
1319  case Sema::PCC_ObjCImplementation:
1320  case Sema::PCC_Namespace:
1321  case Sema::PCC_Template:
1322    if (LangOpts.CPlusPlus || LangOpts.C99)
1323      Results.AddResult(Result("inline"));
1324    break;
1325
1326  case Sema::PCC_ObjCInstanceVariableList:
1327  case Sema::PCC_Expression:
1328  case Sema::PCC_Statement:
1329  case Sema::PCC_ForInit:
1330  case Sema::PCC_Condition:
1331  case Sema::PCC_RecoveryInFunction:
1332  case Sema::PCC_Type:
1333  case Sema::PCC_ParenthesizedExpression:
1334  case Sema::PCC_LocalDeclarationSpecifiers:
1335    break;
1336  }
1337}
1338
1339static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt);
1340static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt);
1341static void AddObjCVisibilityResults(const LangOptions &LangOpts,
1342                                     ResultBuilder &Results,
1343                                     bool NeedAt);
1344static void AddObjCImplementationResults(const LangOptions &LangOpts,
1345                                         ResultBuilder &Results,
1346                                         bool NeedAt);
1347static void AddObjCInterfaceResults(const LangOptions &LangOpts,
1348                                    ResultBuilder &Results,
1349                                    bool NeedAt);
1350static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt);
1351
1352static void AddTypedefResult(ResultBuilder &Results) {
1353  CodeCompletionBuilder Builder(Results.getAllocator(),
1354                                Results.getCodeCompletionTUInfo());
1355  Builder.AddTypedTextChunk("typedef");
1356  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1357  Builder.AddPlaceholderChunk("type");
1358  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1359  Builder.AddPlaceholderChunk("name");
1360  Results.AddResult(CodeCompletionResult(Builder.TakeString()));
1361}
1362
1363static bool WantTypesInContext(Sema::ParserCompletionContext CCC,
1364                               const LangOptions &LangOpts) {
1365  switch (CCC) {
1366  case Sema::PCC_Namespace:
1367  case Sema::PCC_Class:
1368  case Sema::PCC_ObjCInstanceVariableList:
1369  case Sema::PCC_Template:
1370  case Sema::PCC_MemberTemplate:
1371  case Sema::PCC_Statement:
1372  case Sema::PCC_RecoveryInFunction:
1373  case Sema::PCC_Type:
1374  case Sema::PCC_ParenthesizedExpression:
1375  case Sema::PCC_LocalDeclarationSpecifiers:
1376    return true;
1377
1378  case Sema::PCC_Expression:
1379  case Sema::PCC_Condition:
1380    return LangOpts.CPlusPlus;
1381
1382  case Sema::PCC_ObjCInterface:
1383  case Sema::PCC_ObjCImplementation:
1384    return false;
1385
1386  case Sema::PCC_ForInit:
1387    return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99;
1388  }
1389
1390  llvm_unreachable("Invalid ParserCompletionContext!");
1391}
1392
1393static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context,
1394                                                  const Preprocessor &PP) {
1395  PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP);
1396  Policy.AnonymousTagLocations = false;
1397  Policy.SuppressStrongLifetime = true;
1398  Policy.SuppressUnwrittenScope = true;
1399  return Policy;
1400}
1401
1402/// \brief Retrieve a printing policy suitable for code completion.
1403static PrintingPolicy getCompletionPrintingPolicy(Sema &S) {
1404  return getCompletionPrintingPolicy(S.Context, S.PP);
1405}
1406
1407/// \brief Retrieve the string representation of the given type as a string
1408/// that has the appropriate lifetime for code completion.
1409///
1410/// This routine provides a fast path where we provide constant strings for
1411/// common type names.
1412static const char *GetCompletionTypeString(QualType T,
1413                                           ASTContext &Context,
1414                                           const PrintingPolicy &Policy,
1415                                           CodeCompletionAllocator &Allocator) {
1416  if (!T.getLocalQualifiers()) {
1417    // Built-in type names are constant strings.
1418    if (const BuiltinType *BT = dyn_cast<BuiltinType>(T))
1419      return BT->getNameAsCString(Policy);
1420
1421    // Anonymous tag types are constant strings.
1422    if (const TagType *TagT = dyn_cast<TagType>(T))
1423      if (TagDecl *Tag = TagT->getDecl())
1424        if (!Tag->getIdentifier() && !Tag->getTypedefNameForAnonDecl()) {
1425          switch (Tag->getTagKind()) {
1426          case TTK_Struct: return "struct <anonymous>";
1427          case TTK_Interface: return "__interface <anonymous>";
1428          case TTK_Class:  return "class <anonymous>";
1429          case TTK_Union:  return "union <anonymous>";
1430          case TTK_Enum:   return "enum <anonymous>";
1431          }
1432        }
1433  }
1434
1435  // Slow path: format the type as a string.
1436  std::string Result;
1437  T.getAsStringInternal(Result, Policy);
1438  return Allocator.CopyString(Result);
1439}
1440
1441/// \brief Add a completion for "this", if we're in a member function.
1442static void addThisCompletion(Sema &S, ResultBuilder &Results) {
1443  QualType ThisTy = S.getCurrentThisType();
1444  if (ThisTy.isNull())
1445    return;
1446
1447  CodeCompletionAllocator &Allocator = Results.getAllocator();
1448  CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
1449  PrintingPolicy Policy = getCompletionPrintingPolicy(S);
1450  Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy,
1451                                                     S.Context,
1452                                                     Policy,
1453                                                     Allocator));
1454  Builder.AddTypedTextChunk("this");
1455  Results.AddResult(CodeCompletionResult(Builder.TakeString()));
1456}
1457
1458/// \brief Add language constructs that show up for "ordinary" names.
1459static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC,
1460                                   Scope *S,
1461                                   Sema &SemaRef,
1462                                   ResultBuilder &Results) {
1463  CodeCompletionAllocator &Allocator = Results.getAllocator();
1464  CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
1465  PrintingPolicy Policy = getCompletionPrintingPolicy(SemaRef);
1466
1467  typedef CodeCompletionResult Result;
1468  switch (CCC) {
1469  case Sema::PCC_Namespace:
1470    if (SemaRef.getLangOpts().CPlusPlus) {
1471      if (Results.includeCodePatterns()) {
1472        // namespace <identifier> { declarations }
1473        Builder.AddTypedTextChunk("namespace");
1474        Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1475        Builder.AddPlaceholderChunk("identifier");
1476        Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1477        Builder.AddPlaceholderChunk("declarations");
1478        Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1479        Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1480        Results.AddResult(Result(Builder.TakeString()));
1481      }
1482
1483      // namespace identifier = identifier ;
1484      Builder.AddTypedTextChunk("namespace");
1485      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1486      Builder.AddPlaceholderChunk("name");
1487      Builder.AddChunk(CodeCompletionString::CK_Equal);
1488      Builder.AddPlaceholderChunk("namespace");
1489      Results.AddResult(Result(Builder.TakeString()));
1490
1491      // Using directives
1492      Builder.AddTypedTextChunk("using");
1493      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1494      Builder.AddTextChunk("namespace");
1495      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1496      Builder.AddPlaceholderChunk("identifier");
1497      Results.AddResult(Result(Builder.TakeString()));
1498
1499      // asm(string-literal)
1500      Builder.AddTypedTextChunk("asm");
1501      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1502      Builder.AddPlaceholderChunk("string-literal");
1503      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1504      Results.AddResult(Result(Builder.TakeString()));
1505
1506      if (Results.includeCodePatterns()) {
1507        // Explicit template instantiation
1508        Builder.AddTypedTextChunk("template");
1509        Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1510        Builder.AddPlaceholderChunk("declaration");
1511        Results.AddResult(Result(Builder.TakeString()));
1512      }
1513    }
1514
1515    if (SemaRef.getLangOpts().ObjC1)
1516      AddObjCTopLevelResults(Results, true);
1517
1518    AddTypedefResult(Results);
1519    // Fall through
1520
1521  case Sema::PCC_Class:
1522    if (SemaRef.getLangOpts().CPlusPlus) {
1523      // Using declaration
1524      Builder.AddTypedTextChunk("using");
1525      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1526      Builder.AddPlaceholderChunk("qualifier");
1527      Builder.AddTextChunk("::");
1528      Builder.AddPlaceholderChunk("name");
1529      Results.AddResult(Result(Builder.TakeString()));
1530
1531      // using typename qualifier::name (only in a dependent context)
1532      if (SemaRef.CurContext->isDependentContext()) {
1533        Builder.AddTypedTextChunk("using");
1534        Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1535        Builder.AddTextChunk("typename");
1536        Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1537        Builder.AddPlaceholderChunk("qualifier");
1538        Builder.AddTextChunk("::");
1539        Builder.AddPlaceholderChunk("name");
1540        Results.AddResult(Result(Builder.TakeString()));
1541      }
1542
1543      if (CCC == Sema::PCC_Class) {
1544        AddTypedefResult(Results);
1545
1546        // public:
1547        Builder.AddTypedTextChunk("public");
1548        if (Results.includeCodePatterns())
1549          Builder.AddChunk(CodeCompletionString::CK_Colon);
1550        Results.AddResult(Result(Builder.TakeString()));
1551
1552        // protected:
1553        Builder.AddTypedTextChunk("protected");
1554        if (Results.includeCodePatterns())
1555          Builder.AddChunk(CodeCompletionString::CK_Colon);
1556        Results.AddResult(Result(Builder.TakeString()));
1557
1558        // private:
1559        Builder.AddTypedTextChunk("private");
1560        if (Results.includeCodePatterns())
1561          Builder.AddChunk(CodeCompletionString::CK_Colon);
1562        Results.AddResult(Result(Builder.TakeString()));
1563      }
1564    }
1565    // Fall through
1566
1567  case Sema::PCC_Template:
1568  case Sema::PCC_MemberTemplate:
1569    if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) {
1570      // template < parameters >
1571      Builder.AddTypedTextChunk("template");
1572      Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1573      Builder.AddPlaceholderChunk("parameters");
1574      Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1575      Results.AddResult(Result(Builder.TakeString()));
1576    }
1577
1578    AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1579    AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1580    break;
1581
1582  case Sema::PCC_ObjCInterface:
1583    AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true);
1584    AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1585    AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1586    break;
1587
1588  case Sema::PCC_ObjCImplementation:
1589    AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true);
1590    AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1591    AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1592    break;
1593
1594  case Sema::PCC_ObjCInstanceVariableList:
1595    AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true);
1596    break;
1597
1598  case Sema::PCC_RecoveryInFunction:
1599  case Sema::PCC_Statement: {
1600    AddTypedefResult(Results);
1601
1602    if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() &&
1603        SemaRef.getLangOpts().CXXExceptions) {
1604      Builder.AddTypedTextChunk("try");
1605      Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1606      Builder.AddPlaceholderChunk("statements");
1607      Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1608      Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1609      Builder.AddTextChunk("catch");
1610      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1611      Builder.AddPlaceholderChunk("declaration");
1612      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1613      Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1614      Builder.AddPlaceholderChunk("statements");
1615      Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1616      Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1617      Results.AddResult(Result(Builder.TakeString()));
1618    }
1619    if (SemaRef.getLangOpts().ObjC1)
1620      AddObjCStatementResults(Results, true);
1621
1622    if (Results.includeCodePatterns()) {
1623      // if (condition) { statements }
1624      Builder.AddTypedTextChunk("if");
1625      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1626      if (SemaRef.getLangOpts().CPlusPlus)
1627        Builder.AddPlaceholderChunk("condition");
1628      else
1629        Builder.AddPlaceholderChunk("expression");
1630      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1631      Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1632      Builder.AddPlaceholderChunk("statements");
1633      Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1634      Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1635      Results.AddResult(Result(Builder.TakeString()));
1636
1637      // switch (condition) { }
1638      Builder.AddTypedTextChunk("switch");
1639      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1640      if (SemaRef.getLangOpts().CPlusPlus)
1641        Builder.AddPlaceholderChunk("condition");
1642      else
1643        Builder.AddPlaceholderChunk("expression");
1644      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1645      Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1646      Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1647      Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1648      Results.AddResult(Result(Builder.TakeString()));
1649    }
1650
1651    // Switch-specific statements.
1652    if (!SemaRef.getCurFunction()->SwitchStack.empty()) {
1653      // case expression:
1654      Builder.AddTypedTextChunk("case");
1655      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1656      Builder.AddPlaceholderChunk("expression");
1657      Builder.AddChunk(CodeCompletionString::CK_Colon);
1658      Results.AddResult(Result(Builder.TakeString()));
1659
1660      // default:
1661      Builder.AddTypedTextChunk("default");
1662      Builder.AddChunk(CodeCompletionString::CK_Colon);
1663      Results.AddResult(Result(Builder.TakeString()));
1664    }
1665
1666    if (Results.includeCodePatterns()) {
1667      /// while (condition) { statements }
1668      Builder.AddTypedTextChunk("while");
1669      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1670      if (SemaRef.getLangOpts().CPlusPlus)
1671        Builder.AddPlaceholderChunk("condition");
1672      else
1673        Builder.AddPlaceholderChunk("expression");
1674      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1675      Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1676      Builder.AddPlaceholderChunk("statements");
1677      Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1678      Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1679      Results.AddResult(Result(Builder.TakeString()));
1680
1681      // do { statements } while ( expression );
1682      Builder.AddTypedTextChunk("do");
1683      Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1684      Builder.AddPlaceholderChunk("statements");
1685      Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1686      Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1687      Builder.AddTextChunk("while");
1688      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1689      Builder.AddPlaceholderChunk("expression");
1690      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1691      Results.AddResult(Result(Builder.TakeString()));
1692
1693      // for ( for-init-statement ; condition ; expression ) { statements }
1694      Builder.AddTypedTextChunk("for");
1695      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1696      if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99)
1697        Builder.AddPlaceholderChunk("init-statement");
1698      else
1699        Builder.AddPlaceholderChunk("init-expression");
1700      Builder.AddChunk(CodeCompletionString::CK_SemiColon);
1701      Builder.AddPlaceholderChunk("condition");
1702      Builder.AddChunk(CodeCompletionString::CK_SemiColon);
1703      Builder.AddPlaceholderChunk("inc-expression");
1704      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1705      Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1706      Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1707      Builder.AddPlaceholderChunk("statements");
1708      Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1709      Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1710      Results.AddResult(Result(Builder.TakeString()));
1711    }
1712
1713    if (S->getContinueParent()) {
1714      // continue ;
1715      Builder.AddTypedTextChunk("continue");
1716      Results.AddResult(Result(Builder.TakeString()));
1717    }
1718
1719    if (S->getBreakParent()) {
1720      // break ;
1721      Builder.AddTypedTextChunk("break");
1722      Results.AddResult(Result(Builder.TakeString()));
1723    }
1724
1725    // "return expression ;" or "return ;", depending on whether we
1726    // know the function is void or not.
1727    bool isVoid = false;
1728    if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext))
1729      isVoid = Function->getResultType()->isVoidType();
1730    else if (ObjCMethodDecl *Method
1731                                 = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext))
1732      isVoid = Method->getResultType()->isVoidType();
1733    else if (SemaRef.getCurBlock() &&
1734             !SemaRef.getCurBlock()->ReturnType.isNull())
1735      isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType();
1736    Builder.AddTypedTextChunk("return");
1737    if (!isVoid) {
1738      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1739      Builder.AddPlaceholderChunk("expression");
1740    }
1741    Results.AddResult(Result(Builder.TakeString()));
1742
1743    // goto identifier ;
1744    Builder.AddTypedTextChunk("goto");
1745    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1746    Builder.AddPlaceholderChunk("label");
1747    Results.AddResult(Result(Builder.TakeString()));
1748
1749    // Using directives
1750    Builder.AddTypedTextChunk("using");
1751    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1752    Builder.AddTextChunk("namespace");
1753    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1754    Builder.AddPlaceholderChunk("identifier");
1755    Results.AddResult(Result(Builder.TakeString()));
1756  }
1757
1758  // Fall through (for statement expressions).
1759  case Sema::PCC_ForInit:
1760  case Sema::PCC_Condition:
1761    AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1762    // Fall through: conditions and statements can have expressions.
1763
1764  case Sema::PCC_ParenthesizedExpression:
1765    if (SemaRef.getLangOpts().ObjCAutoRefCount &&
1766        CCC == Sema::PCC_ParenthesizedExpression) {
1767      // (__bridge <type>)<expression>
1768      Builder.AddTypedTextChunk("__bridge");
1769      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1770      Builder.AddPlaceholderChunk("type");
1771      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1772      Builder.AddPlaceholderChunk("expression");
1773      Results.AddResult(Result(Builder.TakeString()));
1774
1775      // (__bridge_transfer <Objective-C type>)<expression>
1776      Builder.AddTypedTextChunk("__bridge_transfer");
1777      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1778      Builder.AddPlaceholderChunk("Objective-C type");
1779      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1780      Builder.AddPlaceholderChunk("expression");
1781      Results.AddResult(Result(Builder.TakeString()));
1782
1783      // (__bridge_retained <CF type>)<expression>
1784      Builder.AddTypedTextChunk("__bridge_retained");
1785      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1786      Builder.AddPlaceholderChunk("CF type");
1787      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1788      Builder.AddPlaceholderChunk("expression");
1789      Results.AddResult(Result(Builder.TakeString()));
1790    }
1791    // Fall through
1792
1793  case Sema::PCC_Expression: {
1794    if (SemaRef.getLangOpts().CPlusPlus) {
1795      // 'this', if we're in a non-static member function.
1796      addThisCompletion(SemaRef, Results);
1797
1798      // true
1799      Builder.AddResultTypeChunk("bool");
1800      Builder.AddTypedTextChunk("true");
1801      Results.AddResult(Result(Builder.TakeString()));
1802
1803      // false
1804      Builder.AddResultTypeChunk("bool");
1805      Builder.AddTypedTextChunk("false");
1806      Results.AddResult(Result(Builder.TakeString()));
1807
1808      if (SemaRef.getLangOpts().RTTI) {
1809        // dynamic_cast < type-id > ( expression )
1810        Builder.AddTypedTextChunk("dynamic_cast");
1811        Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1812        Builder.AddPlaceholderChunk("type");
1813        Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1814        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1815        Builder.AddPlaceholderChunk("expression");
1816        Builder.AddChunk(CodeCompletionString::CK_RightParen);
1817        Results.AddResult(Result(Builder.TakeString()));
1818      }
1819
1820      // static_cast < type-id > ( expression )
1821      Builder.AddTypedTextChunk("static_cast");
1822      Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1823      Builder.AddPlaceholderChunk("type");
1824      Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1825      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1826      Builder.AddPlaceholderChunk("expression");
1827      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1828      Results.AddResult(Result(Builder.TakeString()));
1829
1830      // reinterpret_cast < type-id > ( expression )
1831      Builder.AddTypedTextChunk("reinterpret_cast");
1832      Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1833      Builder.AddPlaceholderChunk("type");
1834      Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1835      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1836      Builder.AddPlaceholderChunk("expression");
1837      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1838      Results.AddResult(Result(Builder.TakeString()));
1839
1840      // const_cast < type-id > ( expression )
1841      Builder.AddTypedTextChunk("const_cast");
1842      Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1843      Builder.AddPlaceholderChunk("type");
1844      Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1845      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1846      Builder.AddPlaceholderChunk("expression");
1847      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1848      Results.AddResult(Result(Builder.TakeString()));
1849
1850      if (SemaRef.getLangOpts().RTTI) {
1851        // typeid ( expression-or-type )
1852        Builder.AddResultTypeChunk("std::type_info");
1853        Builder.AddTypedTextChunk("typeid");
1854        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1855        Builder.AddPlaceholderChunk("expression-or-type");
1856        Builder.AddChunk(CodeCompletionString::CK_RightParen);
1857        Results.AddResult(Result(Builder.TakeString()));
1858      }
1859
1860      // new T ( ... )
1861      Builder.AddTypedTextChunk("new");
1862      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1863      Builder.AddPlaceholderChunk("type");
1864      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1865      Builder.AddPlaceholderChunk("expressions");
1866      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1867      Results.AddResult(Result(Builder.TakeString()));
1868
1869      // new T [ ] ( ... )
1870      Builder.AddTypedTextChunk("new");
1871      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1872      Builder.AddPlaceholderChunk("type");
1873      Builder.AddChunk(CodeCompletionString::CK_LeftBracket);
1874      Builder.AddPlaceholderChunk("size");
1875      Builder.AddChunk(CodeCompletionString::CK_RightBracket);
1876      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1877      Builder.AddPlaceholderChunk("expressions");
1878      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1879      Results.AddResult(Result(Builder.TakeString()));
1880
1881      // delete expression
1882      Builder.AddResultTypeChunk("void");
1883      Builder.AddTypedTextChunk("delete");
1884      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1885      Builder.AddPlaceholderChunk("expression");
1886      Results.AddResult(Result(Builder.TakeString()));
1887
1888      // delete [] expression
1889      Builder.AddResultTypeChunk("void");
1890      Builder.AddTypedTextChunk("delete");
1891      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1892      Builder.AddChunk(CodeCompletionString::CK_LeftBracket);
1893      Builder.AddChunk(CodeCompletionString::CK_RightBracket);
1894      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1895      Builder.AddPlaceholderChunk("expression");
1896      Results.AddResult(Result(Builder.TakeString()));
1897
1898      if (SemaRef.getLangOpts().CXXExceptions) {
1899        // throw expression
1900        Builder.AddResultTypeChunk("void");
1901        Builder.AddTypedTextChunk("throw");
1902        Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1903        Builder.AddPlaceholderChunk("expression");
1904        Results.AddResult(Result(Builder.TakeString()));
1905      }
1906
1907      // FIXME: Rethrow?
1908
1909      if (SemaRef.getLangOpts().CPlusPlus0x) {
1910        // nullptr
1911        Builder.AddResultTypeChunk("std::nullptr_t");
1912        Builder.AddTypedTextChunk("nullptr");
1913        Results.AddResult(Result(Builder.TakeString()));
1914
1915        // alignof
1916        Builder.AddResultTypeChunk("size_t");
1917        Builder.AddTypedTextChunk("alignof");
1918        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1919        Builder.AddPlaceholderChunk("type");
1920        Builder.AddChunk(CodeCompletionString::CK_RightParen);
1921        Results.AddResult(Result(Builder.TakeString()));
1922
1923        // noexcept
1924        Builder.AddResultTypeChunk("bool");
1925        Builder.AddTypedTextChunk("noexcept");
1926        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1927        Builder.AddPlaceholderChunk("expression");
1928        Builder.AddChunk(CodeCompletionString::CK_RightParen);
1929        Results.AddResult(Result(Builder.TakeString()));
1930
1931        // sizeof... expression
1932        Builder.AddResultTypeChunk("size_t");
1933        Builder.AddTypedTextChunk("sizeof...");
1934        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1935        Builder.AddPlaceholderChunk("parameter-pack");
1936        Builder.AddChunk(CodeCompletionString::CK_RightParen);
1937        Results.AddResult(Result(Builder.TakeString()));
1938      }
1939    }
1940
1941    if (SemaRef.getLangOpts().ObjC1) {
1942      // Add "super", if we're in an Objective-C class with a superclass.
1943      if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) {
1944        // The interface can be NULL.
1945        if (ObjCInterfaceDecl *ID = Method->getClassInterface())
1946          if (ID->getSuperClass()) {
1947            std::string SuperType;
1948            SuperType = ID->getSuperClass()->getNameAsString();
1949            if (Method->isInstanceMethod())
1950              SuperType += " *";
1951
1952            Builder.AddResultTypeChunk(Allocator.CopyString(SuperType));
1953            Builder.AddTypedTextChunk("super");
1954            Results.AddResult(Result(Builder.TakeString()));
1955          }
1956      }
1957
1958      AddObjCExpressionResults(Results, true);
1959    }
1960
1961    if (SemaRef.getLangOpts().C11) {
1962      // _Alignof
1963      Builder.AddResultTypeChunk("size_t");
1964      if (SemaRef.getASTContext().Idents.get("alignof").hasMacroDefinition())
1965        Builder.AddTypedTextChunk("alignof");
1966      else
1967        Builder.AddTypedTextChunk("_Alignof");
1968      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1969      Builder.AddPlaceholderChunk("type");
1970      Builder.AddChunk(CodeCompletionString::CK_RightParen);
1971      Results.AddResult(Result(Builder.TakeString()));
1972    }
1973
1974    // sizeof expression
1975    Builder.AddResultTypeChunk("size_t");
1976    Builder.AddTypedTextChunk("sizeof");
1977    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1978    Builder.AddPlaceholderChunk("expression-or-type");
1979    Builder.AddChunk(CodeCompletionString::CK_RightParen);
1980    Results.AddResult(Result(Builder.TakeString()));
1981    break;
1982  }
1983
1984  case Sema::PCC_Type:
1985  case Sema::PCC_LocalDeclarationSpecifiers:
1986    break;
1987  }
1988
1989  if (WantTypesInContext(CCC, SemaRef.getLangOpts()))
1990    AddTypeSpecifierResults(SemaRef.getLangOpts(), Results);
1991
1992  if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type)
1993    Results.AddResult(Result("operator"));
1994}
1995
1996/// \brief If the given declaration has an associated type, add it as a result
1997/// type chunk.
1998static void AddResultTypeChunk(ASTContext &Context,
1999                               const PrintingPolicy &Policy,
2000                               NamedDecl *ND,
2001                               CodeCompletionBuilder &Result) {
2002  if (!ND)
2003    return;
2004
2005  // Skip constructors and conversion functions, which have their return types
2006  // built into their names.
2007  if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND))
2008    return;
2009
2010  // Determine the type of the declaration (if it has a type).
2011  QualType T;
2012  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND))
2013    T = Function->getResultType();
2014  else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
2015    T = Method->getResultType();
2016  else if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND))
2017    T = FunTmpl->getTemplatedDecl()->getResultType();
2018  else if (EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND))
2019    T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext()));
2020  else if (isa<UnresolvedUsingValueDecl>(ND)) {
2021    /* Do nothing: ignore unresolved using declarations*/
2022  } else if (ValueDecl *Value = dyn_cast<ValueDecl>(ND)) {
2023    T = Value->getType();
2024  } else if (ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND))
2025    T = Property->getType();
2026
2027  if (T.isNull() || Context.hasSameType(T, Context.DependentTy))
2028    return;
2029
2030  Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy,
2031                                                    Result.getAllocator()));
2032}
2033
2034static void MaybeAddSentinel(ASTContext &Context, NamedDecl *FunctionOrMethod,
2035                             CodeCompletionBuilder &Result) {
2036  if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>())
2037    if (Sentinel->getSentinel() == 0) {
2038      if (Context.getLangOpts().ObjC1 &&
2039          Context.Idents.get("nil").hasMacroDefinition())
2040        Result.AddTextChunk(", nil");
2041      else if (Context.Idents.get("NULL").hasMacroDefinition())
2042        Result.AddTextChunk(", NULL");
2043      else
2044        Result.AddTextChunk(", (void*)0");
2045    }
2046}
2047
2048static std::string formatObjCParamQualifiers(unsigned ObjCQuals) {
2049  std::string Result;
2050  if (ObjCQuals & Decl::OBJC_TQ_In)
2051    Result += "in ";
2052  else if (ObjCQuals & Decl::OBJC_TQ_Inout)
2053    Result += "inout ";
2054  else if (ObjCQuals & Decl::OBJC_TQ_Out)
2055    Result += "out ";
2056  if (ObjCQuals & Decl::OBJC_TQ_Bycopy)
2057    Result += "bycopy ";
2058  else if (ObjCQuals & Decl::OBJC_TQ_Byref)
2059    Result += "byref ";
2060  if (ObjCQuals & Decl::OBJC_TQ_Oneway)
2061    Result += "oneway ";
2062  return Result;
2063}
2064
2065static std::string FormatFunctionParameter(ASTContext &Context,
2066                                           const PrintingPolicy &Policy,
2067                                           ParmVarDecl *Param,
2068                                           bool SuppressName = false,
2069                                           bool SuppressBlock = false) {
2070  bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext());
2071  if (Param->getType()->isDependentType() ||
2072      !Param->getType()->isBlockPointerType()) {
2073    // The argument for a dependent or non-block parameter is a placeholder
2074    // containing that parameter's type.
2075    std::string Result;
2076
2077    if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName)
2078      Result = Param->getIdentifier()->getName();
2079
2080    Param->getType().getAsStringInternal(Result, Policy);
2081
2082    if (ObjCMethodParam) {
2083      Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier())
2084             + Result + ")";
2085      if (Param->getIdentifier() && !SuppressName)
2086        Result += Param->getIdentifier()->getName();
2087    }
2088    return Result;
2089  }
2090
2091  // The argument for a block pointer parameter is a block literal with
2092  // the appropriate type.
2093  FunctionTypeLoc *Block = 0;
2094  FunctionProtoTypeLoc *BlockProto = 0;
2095  TypeLoc TL;
2096  if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) {
2097    TL = TSInfo->getTypeLoc().getUnqualifiedLoc();
2098    while (true) {
2099      // Look through typedefs.
2100      if (!SuppressBlock) {
2101        if (TypedefTypeLoc *TypedefTL = dyn_cast<TypedefTypeLoc>(&TL)) {
2102          if (TypeSourceInfo *InnerTSInfo
2103              = TypedefTL->getTypedefNameDecl()->getTypeSourceInfo()) {
2104            TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc();
2105            continue;
2106          }
2107        }
2108
2109        // Look through qualified types
2110        if (QualifiedTypeLoc *QualifiedTL = dyn_cast<QualifiedTypeLoc>(&TL)) {
2111          TL = QualifiedTL->getUnqualifiedLoc();
2112          continue;
2113        }
2114      }
2115
2116      // Try to get the function prototype behind the block pointer type,
2117      // then we're done.
2118      if (BlockPointerTypeLoc *BlockPtr
2119          = dyn_cast<BlockPointerTypeLoc>(&TL)) {
2120        TL = BlockPtr->getPointeeLoc().IgnoreParens();
2121        Block = dyn_cast<FunctionTypeLoc>(&TL);
2122        BlockProto = dyn_cast<FunctionProtoTypeLoc>(&TL);
2123      }
2124      break;
2125    }
2126  }
2127
2128  if (!Block) {
2129    // We were unable to find a FunctionProtoTypeLoc with parameter names
2130    // for the block; just use the parameter type as a placeholder.
2131    std::string Result;
2132    if (!ObjCMethodParam && Param->getIdentifier())
2133      Result = Param->getIdentifier()->getName();
2134
2135    Param->getType().getUnqualifiedType().getAsStringInternal(Result, Policy);
2136
2137    if (ObjCMethodParam) {
2138      Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier())
2139             + Result + ")";
2140      if (Param->getIdentifier())
2141        Result += Param->getIdentifier()->getName();
2142    }
2143
2144    return Result;
2145  }
2146
2147  // We have the function prototype behind the block pointer type, as it was
2148  // written in the source.
2149  std::string Result;
2150  QualType ResultType = Block->getTypePtr()->getResultType();
2151  if (!ResultType->isVoidType() || SuppressBlock)
2152    ResultType.getAsStringInternal(Result, Policy);
2153
2154  // Format the parameter list.
2155  std::string Params;
2156  if (!BlockProto || Block->getNumArgs() == 0) {
2157    if (BlockProto && BlockProto->getTypePtr()->isVariadic())
2158      Params = "(...)";
2159    else
2160      Params = "(void)";
2161  } else {
2162    Params += "(";
2163    for (unsigned I = 0, N = Block->getNumArgs(); I != N; ++I) {
2164      if (I)
2165        Params += ", ";
2166      Params += FormatFunctionParameter(Context, Policy, Block->getArg(I),
2167                                        /*SuppressName=*/false,
2168                                        /*SuppressBlock=*/true);
2169
2170      if (I == N - 1 && BlockProto->getTypePtr()->isVariadic())
2171        Params += ", ...";
2172    }
2173    Params += ")";
2174  }
2175
2176  if (SuppressBlock) {
2177    // Format as a parameter.
2178    Result = Result + " (^";
2179    if (Param->getIdentifier())
2180      Result += Param->getIdentifier()->getName();
2181    Result += ")";
2182    Result += Params;
2183  } else {
2184    // Format as a block literal argument.
2185    Result = '^' + Result;
2186    Result += Params;
2187
2188    if (Param->getIdentifier())
2189      Result += Param->getIdentifier()->getName();
2190  }
2191
2192  return Result;
2193}
2194
2195/// \brief Add function parameter chunks to the given code completion string.
2196static void AddFunctionParameterChunks(ASTContext &Context,
2197                                       const PrintingPolicy &Policy,
2198                                       FunctionDecl *Function,
2199                                       CodeCompletionBuilder &Result,
2200                                       unsigned Start = 0,
2201                                       bool InOptional = false) {
2202  bool FirstParameter = true;
2203
2204  for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) {
2205    ParmVarDecl *Param = Function->getParamDecl(P);
2206
2207    if (Param->hasDefaultArg() && !InOptional) {
2208      // When we see an optional default argument, put that argument and
2209      // the remaining default arguments into a new, optional string.
2210      CodeCompletionBuilder Opt(Result.getAllocator(),
2211                                Result.getCodeCompletionTUInfo());
2212      if (!FirstParameter)
2213        Opt.AddChunk(CodeCompletionString::CK_Comma);
2214      AddFunctionParameterChunks(Context, Policy, Function, Opt, P, true);
2215      Result.AddOptionalChunk(Opt.TakeString());
2216      break;
2217    }
2218
2219    if (FirstParameter)
2220      FirstParameter = false;
2221    else
2222      Result.AddChunk(CodeCompletionString::CK_Comma);
2223
2224    InOptional = false;
2225
2226    // Format the placeholder string.
2227    std::string PlaceholderStr = FormatFunctionParameter(Context, Policy,
2228                                                         Param);
2229
2230    if (Function->isVariadic() && P == N - 1)
2231      PlaceholderStr += ", ...";
2232
2233    // Add the placeholder string.
2234    Result.AddPlaceholderChunk(
2235                             Result.getAllocator().CopyString(PlaceholderStr));
2236  }
2237
2238  if (const FunctionProtoType *Proto
2239        = Function->getType()->getAs<FunctionProtoType>())
2240    if (Proto->isVariadic()) {
2241      if (Proto->getNumArgs() == 0)
2242        Result.AddPlaceholderChunk("...");
2243
2244      MaybeAddSentinel(Context, Function, Result);
2245    }
2246}
2247
2248/// \brief Add template parameter chunks to the given code completion string.
2249static void AddTemplateParameterChunks(ASTContext &Context,
2250                                       const PrintingPolicy &Policy,
2251                                       TemplateDecl *Template,
2252                                       CodeCompletionBuilder &Result,
2253                                       unsigned MaxParameters = 0,
2254                                       unsigned Start = 0,
2255                                       bool InDefaultArg = false) {
2256  bool FirstParameter = true;
2257
2258  TemplateParameterList *Params = Template->getTemplateParameters();
2259  TemplateParameterList::iterator PEnd = Params->end();
2260  if (MaxParameters)
2261    PEnd = Params->begin() + MaxParameters;
2262  for (TemplateParameterList::iterator P = Params->begin() + Start;
2263       P != PEnd; ++P) {
2264    bool HasDefaultArg = false;
2265    std::string PlaceholderStr;
2266    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
2267      if (TTP->wasDeclaredWithTypename())
2268        PlaceholderStr = "typename";
2269      else
2270        PlaceholderStr = "class";
2271
2272      if (TTP->getIdentifier()) {
2273        PlaceholderStr += ' ';
2274        PlaceholderStr += TTP->getIdentifier()->getName();
2275      }
2276
2277      HasDefaultArg = TTP->hasDefaultArgument();
2278    } else if (NonTypeTemplateParmDecl *NTTP
2279                                    = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
2280      if (NTTP->getIdentifier())
2281        PlaceholderStr = NTTP->getIdentifier()->getName();
2282      NTTP->getType().getAsStringInternal(PlaceholderStr, Policy);
2283      HasDefaultArg = NTTP->hasDefaultArgument();
2284    } else {
2285      assert(isa<TemplateTemplateParmDecl>(*P));
2286      TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
2287
2288      // Since putting the template argument list into the placeholder would
2289      // be very, very long, we just use an abbreviation.
2290      PlaceholderStr = "template<...> class";
2291      if (TTP->getIdentifier()) {
2292        PlaceholderStr += ' ';
2293        PlaceholderStr += TTP->getIdentifier()->getName();
2294      }
2295
2296      HasDefaultArg = TTP->hasDefaultArgument();
2297    }
2298
2299    if (HasDefaultArg && !InDefaultArg) {
2300      // When we see an optional default argument, put that argument and
2301      // the remaining default arguments into a new, optional string.
2302      CodeCompletionBuilder Opt(Result.getAllocator(),
2303                                Result.getCodeCompletionTUInfo());
2304      if (!FirstParameter)
2305        Opt.AddChunk(CodeCompletionString::CK_Comma);
2306      AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters,
2307                                 P - Params->begin(), true);
2308      Result.AddOptionalChunk(Opt.TakeString());
2309      break;
2310    }
2311
2312    InDefaultArg = false;
2313
2314    if (FirstParameter)
2315      FirstParameter = false;
2316    else
2317      Result.AddChunk(CodeCompletionString::CK_Comma);
2318
2319    // Add the placeholder string.
2320    Result.AddPlaceholderChunk(
2321                              Result.getAllocator().CopyString(PlaceholderStr));
2322  }
2323}
2324
2325/// \brief Add a qualifier to the given code-completion string, if the
2326/// provided nested-name-specifier is non-NULL.
2327static void
2328AddQualifierToCompletionString(CodeCompletionBuilder &Result,
2329                               NestedNameSpecifier *Qualifier,
2330                               bool QualifierIsInformative,
2331                               ASTContext &Context,
2332                               const PrintingPolicy &Policy) {
2333  if (!Qualifier)
2334    return;
2335
2336  std::string PrintedNNS;
2337  {
2338    llvm::raw_string_ostream OS(PrintedNNS);
2339    Qualifier->print(OS, Policy);
2340  }
2341  if (QualifierIsInformative)
2342    Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS));
2343  else
2344    Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS));
2345}
2346
2347static void
2348AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result,
2349                                       FunctionDecl *Function) {
2350  const FunctionProtoType *Proto
2351    = Function->getType()->getAs<FunctionProtoType>();
2352  if (!Proto || !Proto->getTypeQuals())
2353    return;
2354
2355  // FIXME: Add ref-qualifier!
2356
2357  // Handle single qualifiers without copying
2358  if (Proto->getTypeQuals() == Qualifiers::Const) {
2359    Result.AddInformativeChunk(" const");
2360    return;
2361  }
2362
2363  if (Proto->getTypeQuals() == Qualifiers::Volatile) {
2364    Result.AddInformativeChunk(" volatile");
2365    return;
2366  }
2367
2368  if (Proto->getTypeQuals() == Qualifiers::Restrict) {
2369    Result.AddInformativeChunk(" restrict");
2370    return;
2371  }
2372
2373  // Handle multiple qualifiers.
2374  std::string QualsStr;
2375  if (Proto->isConst())
2376    QualsStr += " const";
2377  if (Proto->isVolatile())
2378    QualsStr += " volatile";
2379  if (Proto->isRestrict())
2380    QualsStr += " restrict";
2381  Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr));
2382}
2383
2384/// \brief Add the name of the given declaration
2385static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy,
2386                              NamedDecl *ND, CodeCompletionBuilder &Result) {
2387  DeclarationName Name = ND->getDeclName();
2388  if (!Name)
2389    return;
2390
2391  switch (Name.getNameKind()) {
2392    case DeclarationName::CXXOperatorName: {
2393      const char *OperatorName = 0;
2394      switch (Name.getCXXOverloadedOperator()) {
2395      case OO_None:
2396      case OO_Conditional:
2397      case NUM_OVERLOADED_OPERATORS:
2398        OperatorName = "operator";
2399        break;
2400
2401#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2402      case OO_##Name: OperatorName = "operator" Spelling; break;
2403#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2404#include "clang/Basic/OperatorKinds.def"
2405
2406      case OO_New:          OperatorName = "operator new"; break;
2407      case OO_Delete:       OperatorName = "operator delete"; break;
2408      case OO_Array_New:    OperatorName = "operator new[]"; break;
2409      case OO_Array_Delete: OperatorName = "operator delete[]"; break;
2410      case OO_Call:         OperatorName = "operator()"; break;
2411      case OO_Subscript:    OperatorName = "operator[]"; break;
2412      }
2413      Result.AddTypedTextChunk(OperatorName);
2414      break;
2415    }
2416
2417  case DeclarationName::Identifier:
2418  case DeclarationName::CXXConversionFunctionName:
2419  case DeclarationName::CXXDestructorName:
2420  case DeclarationName::CXXLiteralOperatorName:
2421    Result.AddTypedTextChunk(
2422                      Result.getAllocator().CopyString(ND->getNameAsString()));
2423    break;
2424
2425  case DeclarationName::CXXUsingDirective:
2426  case DeclarationName::ObjCZeroArgSelector:
2427  case DeclarationName::ObjCOneArgSelector:
2428  case DeclarationName::ObjCMultiArgSelector:
2429    break;
2430
2431  case DeclarationName::CXXConstructorName: {
2432    CXXRecordDecl *Record = 0;
2433    QualType Ty = Name.getCXXNameType();
2434    if (const RecordType *RecordTy = Ty->getAs<RecordType>())
2435      Record = cast<CXXRecordDecl>(RecordTy->getDecl());
2436    else if (const InjectedClassNameType *InjectedTy
2437                                        = Ty->getAs<InjectedClassNameType>())
2438      Record = InjectedTy->getDecl();
2439    else {
2440      Result.AddTypedTextChunk(
2441                      Result.getAllocator().CopyString(ND->getNameAsString()));
2442      break;
2443    }
2444
2445    Result.AddTypedTextChunk(
2446                  Result.getAllocator().CopyString(Record->getNameAsString()));
2447    if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) {
2448      Result.AddChunk(CodeCompletionString::CK_LeftAngle);
2449      AddTemplateParameterChunks(Context, Policy, Template, Result);
2450      Result.AddChunk(CodeCompletionString::CK_RightAngle);
2451    }
2452    break;
2453  }
2454  }
2455}
2456
2457CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S,
2458                                         CodeCompletionAllocator &Allocator,
2459                                         CodeCompletionTUInfo &CCTUInfo,
2460                                         bool IncludeBriefComments) {
2461  return CreateCodeCompletionString(S.Context, S.PP, Allocator, CCTUInfo,
2462                                    IncludeBriefComments);
2463}
2464
2465/// \brief If possible, create a new code completion string for the given
2466/// result.
2467///
2468/// \returns Either a new, heap-allocated code completion string describing
2469/// how to use this result, or NULL to indicate that the string or name of the
2470/// result is all that is needed.
2471CodeCompletionString *
2472CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
2473                                                 Preprocessor &PP,
2474                                           CodeCompletionAllocator &Allocator,
2475                                           CodeCompletionTUInfo &CCTUInfo,
2476                                           bool IncludeBriefComments) {
2477  CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability);
2478
2479  PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP);
2480  if (Kind == RK_Pattern) {
2481    Pattern->Priority = Priority;
2482    Pattern->Availability = Availability;
2483
2484    if (Declaration) {
2485      Result.addParentContext(Declaration->getDeclContext());
2486      Pattern->ParentKind = Result.getParentKind();
2487      Pattern->ParentName = Result.getParentName();
2488    }
2489
2490    return Pattern;
2491  }
2492
2493  if (Kind == RK_Keyword) {
2494    Result.AddTypedTextChunk(Keyword);
2495    return Result.TakeString();
2496  }
2497
2498  if (Kind == RK_Macro) {
2499    MacroInfo *MI = PP.getMacroInfo(Macro);
2500    assert(MI && "Not a macro?");
2501
2502    Result.AddTypedTextChunk(
2503                            Result.getAllocator().CopyString(Macro->getName()));
2504
2505    if (!MI->isFunctionLike())
2506      return Result.TakeString();
2507
2508    // Format a function-like macro with placeholders for the arguments.
2509    Result.AddChunk(CodeCompletionString::CK_LeftParen);
2510    MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end();
2511
2512    // C99 variadic macros add __VA_ARGS__ at the end. Skip it.
2513    if (MI->isC99Varargs()) {
2514      --AEnd;
2515
2516      if (A == AEnd) {
2517        Result.AddPlaceholderChunk("...");
2518      }
2519    }
2520
2521    for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) {
2522      if (A != MI->arg_begin())
2523        Result.AddChunk(CodeCompletionString::CK_Comma);
2524
2525      if (MI->isVariadic() && (A+1) == AEnd) {
2526        SmallString<32> Arg = (*A)->getName();
2527        if (MI->isC99Varargs())
2528          Arg += ", ...";
2529        else
2530          Arg += "...";
2531        Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
2532        break;
2533      }
2534
2535      // Non-variadic macros are simple.
2536      Result.AddPlaceholderChunk(
2537                          Result.getAllocator().CopyString((*A)->getName()));
2538    }
2539    Result.AddChunk(CodeCompletionString::CK_RightParen);
2540    return Result.TakeString();
2541  }
2542
2543  assert(Kind == RK_Declaration && "Missed a result kind?");
2544  NamedDecl *ND = Declaration;
2545  Result.addParentContext(ND->getDeclContext());
2546
2547  if (IncludeBriefComments) {
2548    // Add documentation comment, if it exists.
2549    if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) {
2550      Result.addBriefComment(RC->getBriefText(Ctx));
2551    }
2552  }
2553
2554  if (StartsNestedNameSpecifier) {
2555    Result.AddTypedTextChunk(
2556                      Result.getAllocator().CopyString(ND->getNameAsString()));
2557    Result.AddTextChunk("::");
2558    return Result.TakeString();
2559  }
2560
2561  for (Decl::attr_iterator i = ND->attr_begin(); i != ND->attr_end(); ++i) {
2562    if (AnnotateAttr *Attr = dyn_cast_or_null<AnnotateAttr>(*i)) {
2563      Result.AddAnnotation(Result.getAllocator().CopyString(Attr->getAnnotation()));
2564    }
2565  }
2566
2567  AddResultTypeChunk(Ctx, Policy, ND, Result);
2568
2569  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
2570    AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
2571                                   Ctx, Policy);
2572    AddTypedNameChunk(Ctx, Policy, ND, Result);
2573    Result.AddChunk(CodeCompletionString::CK_LeftParen);
2574    AddFunctionParameterChunks(Ctx, Policy, Function, Result);
2575    Result.AddChunk(CodeCompletionString::CK_RightParen);
2576    AddFunctionTypeQualsToCompletionString(Result, Function);
2577    return Result.TakeString();
2578  }
2579
2580  if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
2581    AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
2582                                   Ctx, Policy);
2583    FunctionDecl *Function = FunTmpl->getTemplatedDecl();
2584    AddTypedNameChunk(Ctx, Policy, Function, Result);
2585
2586    // Figure out which template parameters are deduced (or have default
2587    // arguments).
2588    llvm::SmallBitVector Deduced;
2589    Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced);
2590    unsigned LastDeducibleArgument;
2591    for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0;
2592         --LastDeducibleArgument) {
2593      if (!Deduced[LastDeducibleArgument - 1]) {
2594        // C++0x: Figure out if the template argument has a default. If so,
2595        // the user doesn't need to type this argument.
2596        // FIXME: We need to abstract template parameters better!
2597        bool HasDefaultArg = false;
2598        NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam(
2599                                                    LastDeducibleArgument - 1);
2600        if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
2601          HasDefaultArg = TTP->hasDefaultArgument();
2602        else if (NonTypeTemplateParmDecl *NTTP
2603                 = dyn_cast<NonTypeTemplateParmDecl>(Param))
2604          HasDefaultArg = NTTP->hasDefaultArgument();
2605        else {
2606          assert(isa<TemplateTemplateParmDecl>(Param));
2607          HasDefaultArg
2608            = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument();
2609        }
2610
2611        if (!HasDefaultArg)
2612          break;
2613      }
2614    }
2615
2616    if (LastDeducibleArgument) {
2617      // Some of the function template arguments cannot be deduced from a
2618      // function call, so we introduce an explicit template argument list
2619      // containing all of the arguments up to the first deducible argument.
2620      Result.AddChunk(CodeCompletionString::CK_LeftAngle);
2621      AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result,
2622                                 LastDeducibleArgument);
2623      Result.AddChunk(CodeCompletionString::CK_RightAngle);
2624    }
2625
2626    // Add the function parameters
2627    Result.AddChunk(CodeCompletionString::CK_LeftParen);
2628    AddFunctionParameterChunks(Ctx, Policy, Function, Result);
2629    Result.AddChunk(CodeCompletionString::CK_RightParen);
2630    AddFunctionTypeQualsToCompletionString(Result, Function);
2631    return Result.TakeString();
2632  }
2633
2634  if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) {
2635    AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
2636                                   Ctx, Policy);
2637    Result.AddTypedTextChunk(
2638                Result.getAllocator().CopyString(Template->getNameAsString()));
2639    Result.AddChunk(CodeCompletionString::CK_LeftAngle);
2640    AddTemplateParameterChunks(Ctx, Policy, Template, Result);
2641    Result.AddChunk(CodeCompletionString::CK_RightAngle);
2642    return Result.TakeString();
2643  }
2644
2645  if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) {
2646    Selector Sel = Method->getSelector();
2647    if (Sel.isUnarySelector()) {
2648      Result.AddTypedTextChunk(Result.getAllocator().CopyString(
2649                                  Sel.getNameForSlot(0)));
2650      return Result.TakeString();
2651    }
2652
2653    std::string SelName = Sel.getNameForSlot(0).str();
2654    SelName += ':';
2655    if (StartParameter == 0)
2656      Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName));
2657    else {
2658      Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName));
2659
2660      // If there is only one parameter, and we're past it, add an empty
2661      // typed-text chunk since there is nothing to type.
2662      if (Method->param_size() == 1)
2663        Result.AddTypedTextChunk("");
2664    }
2665    unsigned Idx = 0;
2666    for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
2667                                     PEnd = Method->param_end();
2668         P != PEnd; (void)++P, ++Idx) {
2669      if (Idx > 0) {
2670        std::string Keyword;
2671        if (Idx > StartParameter)
2672          Result.AddChunk(CodeCompletionString::CK_HorizontalSpace);
2673        if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx))
2674          Keyword += II->getName();
2675        Keyword += ":";
2676        if (Idx < StartParameter || AllParametersAreInformative)
2677          Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword));
2678        else
2679          Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword));
2680      }
2681
2682      // If we're before the starting parameter, skip the placeholder.
2683      if (Idx < StartParameter)
2684        continue;
2685
2686      std::string Arg;
2687
2688      if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity)
2689        Arg = FormatFunctionParameter(Ctx, Policy, *P, true);
2690      else {
2691        (*P)->getType().getAsStringInternal(Arg, Policy);
2692        Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier())
2693            + Arg + ")";
2694        if (IdentifierInfo *II = (*P)->getIdentifier())
2695          if (DeclaringEntity || AllParametersAreInformative)
2696            Arg += II->getName();
2697      }
2698
2699      if (Method->isVariadic() && (P + 1) == PEnd)
2700        Arg += ", ...";
2701
2702      if (DeclaringEntity)
2703        Result.AddTextChunk(Result.getAllocator().CopyString(Arg));
2704      else if (AllParametersAreInformative)
2705        Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg));
2706      else
2707        Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
2708    }
2709
2710    if (Method->isVariadic()) {
2711      if (Method->param_size() == 0) {
2712        if (DeclaringEntity)
2713          Result.AddTextChunk(", ...");
2714        else if (AllParametersAreInformative)
2715          Result.AddInformativeChunk(", ...");
2716        else
2717          Result.AddPlaceholderChunk(", ...");
2718      }
2719
2720      MaybeAddSentinel(Ctx, Method, Result);
2721    }
2722
2723    return Result.TakeString();
2724  }
2725
2726  if (Qualifier)
2727    AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
2728                                   Ctx, Policy);
2729
2730  Result.AddTypedTextChunk(
2731                       Result.getAllocator().CopyString(ND->getNameAsString()));
2732  return Result.TakeString();
2733}
2734
2735CodeCompletionString *
2736CodeCompleteConsumer::OverloadCandidate::CreateSignatureString(
2737                                                          unsigned CurrentArg,
2738                                                               Sema &S,
2739                                     CodeCompletionAllocator &Allocator,
2740                                     CodeCompletionTUInfo &CCTUInfo) const {
2741  PrintingPolicy Policy = getCompletionPrintingPolicy(S);
2742
2743  // FIXME: Set priority, availability appropriately.
2744  CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available);
2745  FunctionDecl *FDecl = getFunction();
2746  AddResultTypeChunk(S.Context, Policy, FDecl, Result);
2747  const FunctionProtoType *Proto
2748    = dyn_cast<FunctionProtoType>(getFunctionType());
2749  if (!FDecl && !Proto) {
2750    // Function without a prototype. Just give the return type and a
2751    // highlighted ellipsis.
2752    const FunctionType *FT = getFunctionType();
2753    Result.AddTextChunk(GetCompletionTypeString(FT->getResultType(),
2754                                                S.Context, Policy,
2755                                                Result.getAllocator()));
2756    Result.AddChunk(CodeCompletionString::CK_LeftParen);
2757    Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "...");
2758    Result.AddChunk(CodeCompletionString::CK_RightParen);
2759    return Result.TakeString();
2760  }
2761
2762  if (FDecl)
2763    Result.AddTextChunk(
2764                    Result.getAllocator().CopyString(FDecl->getNameAsString()));
2765  else
2766    Result.AddTextChunk(
2767         Result.getAllocator().CopyString(
2768                                  Proto->getResultType().getAsString(Policy)));
2769
2770  Result.AddChunk(CodeCompletionString::CK_LeftParen);
2771  unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs();
2772  for (unsigned I = 0; I != NumParams; ++I) {
2773    if (I)
2774      Result.AddChunk(CodeCompletionString::CK_Comma);
2775
2776    std::string ArgString;
2777    QualType ArgType;
2778
2779    if (FDecl) {
2780      ArgString = FDecl->getParamDecl(I)->getNameAsString();
2781      ArgType = FDecl->getParamDecl(I)->getOriginalType();
2782    } else {
2783      ArgType = Proto->getArgType(I);
2784    }
2785
2786    ArgType.getAsStringInternal(ArgString, Policy);
2787
2788    if (I == CurrentArg)
2789      Result.AddChunk(CodeCompletionString::CK_CurrentParameter,
2790                      Result.getAllocator().CopyString(ArgString));
2791    else
2792      Result.AddTextChunk(Result.getAllocator().CopyString(ArgString));
2793  }
2794
2795  if (Proto && Proto->isVariadic()) {
2796    Result.AddChunk(CodeCompletionString::CK_Comma);
2797    if (CurrentArg < NumParams)
2798      Result.AddTextChunk("...");
2799    else
2800      Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "...");
2801  }
2802  Result.AddChunk(CodeCompletionString::CK_RightParen);
2803
2804  return Result.TakeString();
2805}
2806
2807unsigned clang::getMacroUsagePriority(StringRef MacroName,
2808                                      const LangOptions &LangOpts,
2809                                      bool PreferredTypeIsPointer) {
2810  unsigned Priority = CCP_Macro;
2811
2812  // Treat the "nil", "Nil" and "NULL" macros as null pointer constants.
2813  if (MacroName.equals("nil") || MacroName.equals("NULL") ||
2814      MacroName.equals("Nil")) {
2815    Priority = CCP_Constant;
2816    if (PreferredTypeIsPointer)
2817      Priority = Priority / CCF_SimilarTypeMatch;
2818  }
2819  // Treat "YES", "NO", "true", and "false" as constants.
2820  else if (MacroName.equals("YES") || MacroName.equals("NO") ||
2821           MacroName.equals("true") || MacroName.equals("false"))
2822    Priority = CCP_Constant;
2823  // Treat "bool" as a type.
2824  else if (MacroName.equals("bool"))
2825    Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0);
2826
2827
2828  return Priority;
2829}
2830
2831CXCursorKind clang::getCursorKindForDecl(Decl *D) {
2832  if (!D)
2833    return CXCursor_UnexposedDecl;
2834
2835  switch (D->getKind()) {
2836    case Decl::Enum:               return CXCursor_EnumDecl;
2837    case Decl::EnumConstant:       return CXCursor_EnumConstantDecl;
2838    case Decl::Field:              return CXCursor_FieldDecl;
2839    case Decl::Function:
2840      return CXCursor_FunctionDecl;
2841    case Decl::ObjCCategory:       return CXCursor_ObjCCategoryDecl;
2842    case Decl::ObjCCategoryImpl:   return CXCursor_ObjCCategoryImplDecl;
2843    case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl;
2844
2845    case Decl::ObjCInterface:      return CXCursor_ObjCInterfaceDecl;
2846    case Decl::ObjCIvar:           return CXCursor_ObjCIvarDecl;
2847    case Decl::ObjCMethod:
2848      return cast<ObjCMethodDecl>(D)->isInstanceMethod()
2849      ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl;
2850    case Decl::CXXMethod:          return CXCursor_CXXMethod;
2851    case Decl::CXXConstructor:     return CXCursor_Constructor;
2852    case Decl::CXXDestructor:      return CXCursor_Destructor;
2853    case Decl::CXXConversion:      return CXCursor_ConversionFunction;
2854    case Decl::ObjCProperty:       return CXCursor_ObjCPropertyDecl;
2855    case Decl::ObjCProtocol:       return CXCursor_ObjCProtocolDecl;
2856    case Decl::ParmVar:            return CXCursor_ParmDecl;
2857    case Decl::Typedef:            return CXCursor_TypedefDecl;
2858    case Decl::TypeAlias:          return CXCursor_TypeAliasDecl;
2859    case Decl::Var:                return CXCursor_VarDecl;
2860    case Decl::Namespace:          return CXCursor_Namespace;
2861    case Decl::NamespaceAlias:     return CXCursor_NamespaceAlias;
2862    case Decl::TemplateTypeParm:   return CXCursor_TemplateTypeParameter;
2863    case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter;
2864    case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter;
2865    case Decl::FunctionTemplate:   return CXCursor_FunctionTemplate;
2866    case Decl::ClassTemplate:      return CXCursor_ClassTemplate;
2867    case Decl::AccessSpec:         return CXCursor_CXXAccessSpecifier;
2868    case Decl::ClassTemplatePartialSpecialization:
2869      return CXCursor_ClassTemplatePartialSpecialization;
2870    case Decl::UsingDirective:     return CXCursor_UsingDirective;
2871    case Decl::TranslationUnit:    return CXCursor_TranslationUnit;
2872
2873    case Decl::Using:
2874    case Decl::UnresolvedUsingValue:
2875    case Decl::UnresolvedUsingTypename:
2876      return CXCursor_UsingDeclaration;
2877
2878    case Decl::ObjCPropertyImpl:
2879      switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) {
2880      case ObjCPropertyImplDecl::Dynamic:
2881        return CXCursor_ObjCDynamicDecl;
2882
2883      case ObjCPropertyImplDecl::Synthesize:
2884        return CXCursor_ObjCSynthesizeDecl;
2885      }
2886
2887    default:
2888      if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
2889        switch (TD->getTagKind()) {
2890          case TTK_Interface:  // fall through
2891          case TTK_Struct: return CXCursor_StructDecl;
2892          case TTK_Class:  return CXCursor_ClassDecl;
2893          case TTK_Union:  return CXCursor_UnionDecl;
2894          case TTK_Enum:   return CXCursor_EnumDecl;
2895        }
2896      }
2897  }
2898
2899  return CXCursor_UnexposedDecl;
2900}
2901
2902static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results,
2903                            bool TargetTypeIsPointer = false) {
2904  typedef CodeCompletionResult Result;
2905
2906  Results.EnterNewScope();
2907
2908  for (Preprocessor::macro_iterator M = PP.macro_begin(),
2909                                 MEnd = PP.macro_end();
2910       M != MEnd; ++M) {
2911    // FIXME: Eventually, we'd want to be able to look back to the macro
2912    // definition that was actually active at the point of code completion (even
2913    // if that macro has since been #undef'd).
2914    if (M->first->hasMacroDefinition())
2915      Results.AddResult(Result(M->first,
2916                             getMacroUsagePriority(M->first->getName(),
2917                                                   PP.getLangOpts(),
2918                                                   TargetTypeIsPointer)));
2919  }
2920
2921  Results.ExitScope();
2922
2923}
2924
2925static void AddPrettyFunctionResults(const LangOptions &LangOpts,
2926                                     ResultBuilder &Results) {
2927  typedef CodeCompletionResult Result;
2928
2929  Results.EnterNewScope();
2930
2931  Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant));
2932  Results.AddResult(Result("__FUNCTION__", CCP_Constant));
2933  if (LangOpts.C99 || LangOpts.CPlusPlus0x)
2934    Results.AddResult(Result("__func__", CCP_Constant));
2935  Results.ExitScope();
2936}
2937
2938static void HandleCodeCompleteResults(Sema *S,
2939                                      CodeCompleteConsumer *CodeCompleter,
2940                                      CodeCompletionContext Context,
2941                                      CodeCompletionResult *Results,
2942                                      unsigned NumResults) {
2943  if (CodeCompleter)
2944    CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults);
2945}
2946
2947static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S,
2948                                            Sema::ParserCompletionContext PCC) {
2949  switch (PCC) {
2950  case Sema::PCC_Namespace:
2951    return CodeCompletionContext::CCC_TopLevel;
2952
2953  case Sema::PCC_Class:
2954    return CodeCompletionContext::CCC_ClassStructUnion;
2955
2956  case Sema::PCC_ObjCInterface:
2957    return CodeCompletionContext::CCC_ObjCInterface;
2958
2959  case Sema::PCC_ObjCImplementation:
2960    return CodeCompletionContext::CCC_ObjCImplementation;
2961
2962  case Sema::PCC_ObjCInstanceVariableList:
2963    return CodeCompletionContext::CCC_ObjCIvarList;
2964
2965  case Sema::PCC_Template:
2966  case Sema::PCC_MemberTemplate:
2967    if (S.CurContext->isFileContext())
2968      return CodeCompletionContext::CCC_TopLevel;
2969    if (S.CurContext->isRecord())
2970      return CodeCompletionContext::CCC_ClassStructUnion;
2971    return CodeCompletionContext::CCC_Other;
2972
2973  case Sema::PCC_RecoveryInFunction:
2974    return CodeCompletionContext::CCC_Recovery;
2975
2976  case Sema::PCC_ForInit:
2977    if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 ||
2978        S.getLangOpts().ObjC1)
2979      return CodeCompletionContext::CCC_ParenthesizedExpression;
2980    else
2981      return CodeCompletionContext::CCC_Expression;
2982
2983  case Sema::PCC_Expression:
2984  case Sema::PCC_Condition:
2985    return CodeCompletionContext::CCC_Expression;
2986
2987  case Sema::PCC_Statement:
2988    return CodeCompletionContext::CCC_Statement;
2989
2990  case Sema::PCC_Type:
2991    return CodeCompletionContext::CCC_Type;
2992
2993  case Sema::PCC_ParenthesizedExpression:
2994    return CodeCompletionContext::CCC_ParenthesizedExpression;
2995
2996  case Sema::PCC_LocalDeclarationSpecifiers:
2997    return CodeCompletionContext::CCC_Type;
2998  }
2999
3000  llvm_unreachable("Invalid ParserCompletionContext!");
3001}
3002
3003/// \brief If we're in a C++ virtual member function, add completion results
3004/// that invoke the functions we override, since it's common to invoke the
3005/// overridden function as well as adding new functionality.
3006///
3007/// \param S The semantic analysis object for which we are generating results.
3008///
3009/// \param InContext This context in which the nested-name-specifier preceding
3010/// the code-completion point
3011static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext,
3012                                  ResultBuilder &Results) {
3013  // Look through blocks.
3014  DeclContext *CurContext = S.CurContext;
3015  while (isa<BlockDecl>(CurContext))
3016    CurContext = CurContext->getParent();
3017
3018
3019  CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext);
3020  if (!Method || !Method->isVirtual())
3021    return;
3022
3023  // We need to have names for all of the parameters, if we're going to
3024  // generate a forwarding call.
3025  for (CXXMethodDecl::param_iterator P = Method->param_begin(),
3026                                  PEnd = Method->param_end();
3027       P != PEnd;
3028       ++P) {
3029    if (!(*P)->getDeclName())
3030      return;
3031  }
3032
3033  PrintingPolicy Policy = getCompletionPrintingPolicy(S);
3034  for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(),
3035                                   MEnd = Method->end_overridden_methods();
3036       M != MEnd; ++M) {
3037    CodeCompletionBuilder Builder(Results.getAllocator(),
3038                                  Results.getCodeCompletionTUInfo());
3039    CXXMethodDecl *Overridden = const_cast<CXXMethodDecl *>(*M);
3040    if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl())
3041      continue;
3042
3043    // If we need a nested-name-specifier, add one now.
3044    if (!InContext) {
3045      NestedNameSpecifier *NNS
3046        = getRequiredQualification(S.Context, CurContext,
3047                                   Overridden->getDeclContext());
3048      if (NNS) {
3049        std::string Str;
3050        llvm::raw_string_ostream OS(Str);
3051        NNS->print(OS, Policy);
3052        Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str()));
3053      }
3054    } else if (!InContext->Equals(Overridden->getDeclContext()))
3055      continue;
3056
3057    Builder.AddTypedTextChunk(Results.getAllocator().CopyString(
3058                                         Overridden->getNameAsString()));
3059    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
3060    bool FirstParam = true;
3061    for (CXXMethodDecl::param_iterator P = Method->param_begin(),
3062                                    PEnd = Method->param_end();
3063         P != PEnd; ++P) {
3064      if (FirstParam)
3065        FirstParam = false;
3066      else
3067        Builder.AddChunk(CodeCompletionString::CK_Comma);
3068
3069      Builder.AddPlaceholderChunk(Results.getAllocator().CopyString(
3070                                        (*P)->getIdentifier()->getName()));
3071    }
3072    Builder.AddChunk(CodeCompletionString::CK_RightParen);
3073    Results.AddResult(CodeCompletionResult(Builder.TakeString(),
3074                                           CCP_SuperCompletion,
3075                                           CXCursor_CXXMethod,
3076                                           CXAvailability_Available,
3077                                           Overridden));
3078    Results.Ignore(Overridden);
3079  }
3080}
3081
3082void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc,
3083                                    ModuleIdPath Path) {
3084  typedef CodeCompletionResult Result;
3085  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3086                        CodeCompleter->getCodeCompletionTUInfo(),
3087                        CodeCompletionContext::CCC_Other);
3088  Results.EnterNewScope();
3089
3090  CodeCompletionAllocator &Allocator = Results.getAllocator();
3091  CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
3092  typedef CodeCompletionResult Result;
3093  if (Path.empty()) {
3094    // Enumerate all top-level modules.
3095    llvm::SmallVector<Module *, 8> Modules;
3096    PP.getHeaderSearchInfo().collectAllModules(Modules);
3097    for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
3098      Builder.AddTypedTextChunk(
3099        Builder.getAllocator().CopyString(Modules[I]->Name));
3100      Results.AddResult(Result(Builder.TakeString(),
3101                               CCP_Declaration,
3102                               CXCursor_NotImplemented,
3103                               Modules[I]->isAvailable()
3104                                 ? CXAvailability_Available
3105                                  : CXAvailability_NotAvailable));
3106    }
3107  } else {
3108    // Load the named module.
3109    Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path,
3110                                                  Module::AllVisible,
3111                                                /*IsInclusionDirective=*/false);
3112    // Enumerate submodules.
3113    if (Mod) {
3114      for (Module::submodule_iterator Sub = Mod->submodule_begin(),
3115                                   SubEnd = Mod->submodule_end();
3116           Sub != SubEnd; ++Sub) {
3117
3118        Builder.AddTypedTextChunk(
3119          Builder.getAllocator().CopyString((*Sub)->Name));
3120        Results.AddResult(Result(Builder.TakeString(),
3121                                 CCP_Declaration,
3122                                 CXCursor_NotImplemented,
3123                                 (*Sub)->isAvailable()
3124                                   ? CXAvailability_Available
3125                                   : CXAvailability_NotAvailable));
3126      }
3127    }
3128  }
3129  Results.ExitScope();
3130  HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3131                            Results.data(),Results.size());
3132}
3133
3134void Sema::CodeCompleteOrdinaryName(Scope *S,
3135                                    ParserCompletionContext CompletionContext) {
3136  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3137                        CodeCompleter->getCodeCompletionTUInfo(),
3138                        mapCodeCompletionContext(*this, CompletionContext));
3139  Results.EnterNewScope();
3140
3141  // Determine how to filter results, e.g., so that the names of
3142  // values (functions, enumerators, function templates, etc.) are
3143  // only allowed where we can have an expression.
3144  switch (CompletionContext) {
3145  case PCC_Namespace:
3146  case PCC_Class:
3147  case PCC_ObjCInterface:
3148  case PCC_ObjCImplementation:
3149  case PCC_ObjCInstanceVariableList:
3150  case PCC_Template:
3151  case PCC_MemberTemplate:
3152  case PCC_Type:
3153  case PCC_LocalDeclarationSpecifiers:
3154    Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
3155    break;
3156
3157  case PCC_Statement:
3158  case PCC_ParenthesizedExpression:
3159  case PCC_Expression:
3160  case PCC_ForInit:
3161  case PCC_Condition:
3162    if (WantTypesInContext(CompletionContext, getLangOpts()))
3163      Results.setFilter(&ResultBuilder::IsOrdinaryName);
3164    else
3165      Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
3166
3167    if (getLangOpts().CPlusPlus)
3168      MaybeAddOverrideCalls(*this, /*InContext=*/0, Results);
3169    break;
3170
3171  case PCC_RecoveryInFunction:
3172    // Unfiltered
3173    break;
3174  }
3175
3176  // If we are in a C++ non-static member function, check the qualifiers on
3177  // the member function to filter/prioritize the results list.
3178  if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext))
3179    if (CurMethod->isInstance())
3180      Results.setObjectTypeQualifiers(
3181                      Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers()));
3182
3183  CodeCompletionDeclConsumer Consumer(Results, CurContext);
3184  LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3185                     CodeCompleter->includeGlobals());
3186
3187  AddOrdinaryNameResults(CompletionContext, S, *this, Results);
3188  Results.ExitScope();
3189
3190  switch (CompletionContext) {
3191  case PCC_ParenthesizedExpression:
3192  case PCC_Expression:
3193  case PCC_Statement:
3194  case PCC_RecoveryInFunction:
3195    if (S->getFnParent())
3196      AddPrettyFunctionResults(PP.getLangOpts(), Results);
3197    break;
3198
3199  case PCC_Namespace:
3200  case PCC_Class:
3201  case PCC_ObjCInterface:
3202  case PCC_ObjCImplementation:
3203  case PCC_ObjCInstanceVariableList:
3204  case PCC_Template:
3205  case PCC_MemberTemplate:
3206  case PCC_ForInit:
3207  case PCC_Condition:
3208  case PCC_Type:
3209  case PCC_LocalDeclarationSpecifiers:
3210    break;
3211  }
3212
3213  if (CodeCompleter->includeMacros())
3214    AddMacroResults(PP, Results);
3215
3216  HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3217                            Results.data(),Results.size());
3218}
3219
3220static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
3221                                       ParsedType Receiver,
3222                                       IdentifierInfo **SelIdents,
3223                                       unsigned NumSelIdents,
3224                                       bool AtArgumentExpression,
3225                                       bool IsSuper,
3226                                       ResultBuilder &Results);
3227
3228void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS,
3229                                bool AllowNonIdentifiers,
3230                                bool AllowNestedNameSpecifiers) {
3231  typedef CodeCompletionResult Result;
3232  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3233                        CodeCompleter->getCodeCompletionTUInfo(),
3234                        AllowNestedNameSpecifiers
3235                          ? CodeCompletionContext::CCC_PotentiallyQualifiedName
3236                          : CodeCompletionContext::CCC_Name);
3237  Results.EnterNewScope();
3238
3239  // Type qualifiers can come after names.
3240  Results.AddResult(Result("const"));
3241  Results.AddResult(Result("volatile"));
3242  if (getLangOpts().C99)
3243    Results.AddResult(Result("restrict"));
3244
3245  if (getLangOpts().CPlusPlus) {
3246    if (AllowNonIdentifiers) {
3247      Results.AddResult(Result("operator"));
3248    }
3249
3250    // Add nested-name-specifiers.
3251    if (AllowNestedNameSpecifiers) {
3252      Results.allowNestedNameSpecifiers();
3253      Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy);
3254      CodeCompletionDeclConsumer Consumer(Results, CurContext);
3255      LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer,
3256                         CodeCompleter->includeGlobals());
3257      Results.setFilter(0);
3258    }
3259  }
3260  Results.ExitScope();
3261
3262  // If we're in a context where we might have an expression (rather than a
3263  // declaration), and what we've seen so far is an Objective-C type that could
3264  // be a receiver of a class message, this may be a class message send with
3265  // the initial opening bracket '[' missing. Add appropriate completions.
3266  if (AllowNonIdentifiers && !AllowNestedNameSpecifiers &&
3267      DS.getTypeSpecType() == DeclSpec::TST_typename &&
3268      DS.getStorageClassSpecAsWritten() == DeclSpec::SCS_unspecified &&
3269      !DS.isThreadSpecified() && !DS.isExternInLinkageSpec() &&
3270      DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified &&
3271      DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
3272      DS.getTypeQualifiers() == 0 &&
3273      S &&
3274      (S->getFlags() & Scope::DeclScope) != 0 &&
3275      (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope |
3276                        Scope::FunctionPrototypeScope |
3277                        Scope::AtCatchScope)) == 0) {
3278    ParsedType T = DS.getRepAsType();
3279    if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType())
3280      AddClassMessageCompletions(*this, S, T, 0, 0, false, false, Results);
3281  }
3282
3283  // Note that we intentionally suppress macro results here, since we do not
3284  // encourage using macros to produce the names of entities.
3285
3286  HandleCodeCompleteResults(this, CodeCompleter,
3287                            Results.getCompletionContext(),
3288                            Results.data(), Results.size());
3289}
3290
3291struct Sema::CodeCompleteExpressionData {
3292  CodeCompleteExpressionData(QualType PreferredType = QualType())
3293    : PreferredType(PreferredType), IntegralConstantExpression(false),
3294      ObjCCollection(false) { }
3295
3296  QualType PreferredType;
3297  bool IntegralConstantExpression;
3298  bool ObjCCollection;
3299  SmallVector<Decl *, 4> IgnoreDecls;
3300};
3301
3302/// \brief Perform code-completion in an expression context when we know what
3303/// type we're looking for.
3304void Sema::CodeCompleteExpression(Scope *S,
3305                                  const CodeCompleteExpressionData &Data) {
3306  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3307                        CodeCompleter->getCodeCompletionTUInfo(),
3308                        CodeCompletionContext::CCC_Expression);
3309  if (Data.ObjCCollection)
3310    Results.setFilter(&ResultBuilder::IsObjCCollection);
3311  else if (Data.IntegralConstantExpression)
3312    Results.setFilter(&ResultBuilder::IsIntegralConstantValue);
3313  else if (WantTypesInContext(PCC_Expression, getLangOpts()))
3314    Results.setFilter(&ResultBuilder::IsOrdinaryName);
3315  else
3316    Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
3317
3318  if (!Data.PreferredType.isNull())
3319    Results.setPreferredType(Data.PreferredType.getNonReferenceType());
3320
3321  // Ignore any declarations that we were told that we don't care about.
3322  for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I)
3323    Results.Ignore(Data.IgnoreDecls[I]);
3324
3325  CodeCompletionDeclConsumer Consumer(Results, CurContext);
3326  LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3327                     CodeCompleter->includeGlobals());
3328
3329  Results.EnterNewScope();
3330  AddOrdinaryNameResults(PCC_Expression, S, *this, Results);
3331  Results.ExitScope();
3332
3333  bool PreferredTypeIsPointer = false;
3334  if (!Data.PreferredType.isNull())
3335    PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType()
3336      || Data.PreferredType->isMemberPointerType()
3337      || Data.PreferredType->isBlockPointerType();
3338
3339  if (S->getFnParent() &&
3340      !Data.ObjCCollection &&
3341      !Data.IntegralConstantExpression)
3342    AddPrettyFunctionResults(PP.getLangOpts(), Results);
3343
3344  if (CodeCompleter->includeMacros())
3345    AddMacroResults(PP, Results, PreferredTypeIsPointer);
3346  HandleCodeCompleteResults(this, CodeCompleter,
3347                CodeCompletionContext(CodeCompletionContext::CCC_Expression,
3348                                      Data.PreferredType),
3349                            Results.data(),Results.size());
3350}
3351
3352void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) {
3353  if (E.isInvalid())
3354    CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction);
3355  else if (getLangOpts().ObjC1)
3356    CodeCompleteObjCInstanceMessage(S, E.take(), 0, 0, false);
3357}
3358
3359/// \brief The set of properties that have already been added, referenced by
3360/// property name.
3361typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet;
3362
3363/// \brief Retrieve the container definition, if any?
3364static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) {
3365  if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
3366    if (Interface->hasDefinition())
3367      return Interface->getDefinition();
3368
3369    return Interface;
3370  }
3371
3372  if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3373    if (Protocol->hasDefinition())
3374      return Protocol->getDefinition();
3375
3376    return Protocol;
3377  }
3378  return Container;
3379}
3380
3381static void AddObjCProperties(ObjCContainerDecl *Container,
3382                              bool AllowCategories,
3383                              bool AllowNullaryMethods,
3384                              DeclContext *CurContext,
3385                              AddedPropertiesSet &AddedProperties,
3386                              ResultBuilder &Results) {
3387  typedef CodeCompletionResult Result;
3388
3389  // Retrieve the definition.
3390  Container = getContainerDef(Container);
3391
3392  // Add properties in this container.
3393  for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(),
3394                                     PEnd = Container->prop_end();
3395       P != PEnd;
3396       ++P) {
3397    if (AddedProperties.insert(P->getIdentifier()))
3398      Results.MaybeAddResult(Result(*P, 0), CurContext);
3399  }
3400
3401  // Add nullary methods
3402  if (AllowNullaryMethods) {
3403    ASTContext &Context = Container->getASTContext();
3404    PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
3405    for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
3406                                         MEnd = Container->meth_end();
3407         M != MEnd; ++M) {
3408      if (M->getSelector().isUnarySelector())
3409        if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0))
3410          if (AddedProperties.insert(Name)) {
3411            CodeCompletionBuilder Builder(Results.getAllocator(),
3412                                          Results.getCodeCompletionTUInfo());
3413            AddResultTypeChunk(Context, Policy, *M, Builder);
3414            Builder.AddTypedTextChunk(
3415                            Results.getAllocator().CopyString(Name->getName()));
3416
3417            Results.MaybeAddResult(Result(Builder.TakeString(), *M,
3418                                  CCP_MemberDeclaration + CCD_MethodAsProperty),
3419                                          CurContext);
3420          }
3421    }
3422  }
3423
3424
3425  // Add properties in referenced protocols.
3426  if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3427    for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
3428                                          PEnd = Protocol->protocol_end();
3429         P != PEnd; ++P)
3430      AddObjCProperties(*P, AllowCategories, AllowNullaryMethods, CurContext,
3431                        AddedProperties, Results);
3432  } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){
3433    if (AllowCategories) {
3434      // Look through categories.
3435      for (ObjCCategoryDecl *Category = IFace->getCategoryList();
3436           Category; Category = Category->getNextClassCategory())
3437        AddObjCProperties(Category, AllowCategories, AllowNullaryMethods,
3438                          CurContext, AddedProperties, Results);
3439    }
3440
3441    // Look through protocols.
3442    for (ObjCInterfaceDecl::all_protocol_iterator
3443         I = IFace->all_referenced_protocol_begin(),
3444         E = IFace->all_referenced_protocol_end(); I != E; ++I)
3445      AddObjCProperties(*I, AllowCategories, AllowNullaryMethods, CurContext,
3446                        AddedProperties, Results);
3447
3448    // Look in the superclass.
3449    if (IFace->getSuperClass())
3450      AddObjCProperties(IFace->getSuperClass(), AllowCategories,
3451                        AllowNullaryMethods, CurContext,
3452                        AddedProperties, Results);
3453  } else if (const ObjCCategoryDecl *Category
3454                                    = dyn_cast<ObjCCategoryDecl>(Container)) {
3455    // Look through protocols.
3456    for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
3457                                          PEnd = Category->protocol_end();
3458         P != PEnd; ++P)
3459      AddObjCProperties(*P, AllowCategories, AllowNullaryMethods, CurContext,
3460                        AddedProperties, Results);
3461  }
3462}
3463
3464void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base,
3465                                           SourceLocation OpLoc,
3466                                           bool IsArrow) {
3467  if (!Base || !CodeCompleter)
3468    return;
3469
3470  ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow);
3471  if (ConvertedBase.isInvalid())
3472    return;
3473  Base = ConvertedBase.get();
3474
3475  typedef CodeCompletionResult Result;
3476
3477  QualType BaseType = Base->getType();
3478
3479  if (IsArrow) {
3480    if (const PointerType *Ptr = BaseType->getAs<PointerType>())
3481      BaseType = Ptr->getPointeeType();
3482    else if (BaseType->isObjCObjectPointerType())
3483      /*Do nothing*/ ;
3484    else
3485      return;
3486  }
3487
3488  enum CodeCompletionContext::Kind contextKind;
3489
3490  if (IsArrow) {
3491    contextKind = CodeCompletionContext::CCC_ArrowMemberAccess;
3492  }
3493  else {
3494    if (BaseType->isObjCObjectPointerType() ||
3495        BaseType->isObjCObjectOrInterfaceType()) {
3496      contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess;
3497    }
3498    else {
3499      contextKind = CodeCompletionContext::CCC_DotMemberAccess;
3500    }
3501  }
3502
3503  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3504                        CodeCompleter->getCodeCompletionTUInfo(),
3505                  CodeCompletionContext(contextKind,
3506                                        BaseType),
3507                        &ResultBuilder::IsMember);
3508  Results.EnterNewScope();
3509  if (const RecordType *Record = BaseType->getAs<RecordType>()) {
3510    // Indicate that we are performing a member access, and the cv-qualifiers
3511    // for the base object type.
3512    Results.setObjectTypeQualifiers(BaseType.getQualifiers());
3513
3514    // Access to a C/C++ class, struct, or union.
3515    Results.allowNestedNameSpecifiers();
3516    CodeCompletionDeclConsumer Consumer(Results, CurContext);
3517    LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer,
3518                       CodeCompleter->includeGlobals());
3519
3520    if (getLangOpts().CPlusPlus) {
3521      if (!Results.empty()) {
3522        // The "template" keyword can follow "->" or "." in the grammar.
3523        // However, we only want to suggest the template keyword if something
3524        // is dependent.
3525        bool IsDependent = BaseType->isDependentType();
3526        if (!IsDependent) {
3527          for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent())
3528            if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) {
3529              IsDependent = Ctx->isDependentContext();
3530              break;
3531            }
3532        }
3533
3534        if (IsDependent)
3535          Results.AddResult(Result("template"));
3536      }
3537    }
3538  } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) {
3539    // Objective-C property reference.
3540    AddedPropertiesSet AddedProperties;
3541
3542    // Add property results based on our interface.
3543    const ObjCObjectPointerType *ObjCPtr
3544      = BaseType->getAsObjCInterfacePointerType();
3545    assert(ObjCPtr && "Non-NULL pointer guaranteed above!");
3546    AddObjCProperties(ObjCPtr->getInterfaceDecl(), true,
3547                      /*AllowNullaryMethods=*/true, CurContext,
3548                      AddedProperties, Results);
3549
3550    // Add properties from the protocols in a qualified interface.
3551    for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(),
3552                                              E = ObjCPtr->qual_end();
3553         I != E; ++I)
3554      AddObjCProperties(*I, true, /*AllowNullaryMethods=*/true, CurContext,
3555                        AddedProperties, Results);
3556  } else if ((IsArrow && BaseType->isObjCObjectPointerType()) ||
3557             (!IsArrow && BaseType->isObjCObjectType())) {
3558    // Objective-C instance variable access.
3559    ObjCInterfaceDecl *Class = 0;
3560    if (const ObjCObjectPointerType *ObjCPtr
3561                                    = BaseType->getAs<ObjCObjectPointerType>())
3562      Class = ObjCPtr->getInterfaceDecl();
3563    else
3564      Class = BaseType->getAs<ObjCObjectType>()->getInterface();
3565
3566    // Add all ivars from this class and its superclasses.
3567    if (Class) {
3568      CodeCompletionDeclConsumer Consumer(Results, CurContext);
3569      Results.setFilter(&ResultBuilder::IsObjCIvar);
3570      LookupVisibleDecls(Class, LookupMemberName, Consumer,
3571                         CodeCompleter->includeGlobals());
3572    }
3573  }
3574
3575  // FIXME: How do we cope with isa?
3576
3577  Results.ExitScope();
3578
3579  // Hand off the results found for code completion.
3580  HandleCodeCompleteResults(this, CodeCompleter,
3581                            Results.getCompletionContext(),
3582                            Results.data(),Results.size());
3583}
3584
3585void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) {
3586  if (!CodeCompleter)
3587    return;
3588
3589  ResultBuilder::LookupFilter Filter = 0;
3590  enum CodeCompletionContext::Kind ContextKind
3591    = CodeCompletionContext::CCC_Other;
3592  switch ((DeclSpec::TST)TagSpec) {
3593  case DeclSpec::TST_enum:
3594    Filter = &ResultBuilder::IsEnum;
3595    ContextKind = CodeCompletionContext::CCC_EnumTag;
3596    break;
3597
3598  case DeclSpec::TST_union:
3599    Filter = &ResultBuilder::IsUnion;
3600    ContextKind = CodeCompletionContext::CCC_UnionTag;
3601    break;
3602
3603  case DeclSpec::TST_struct:
3604  case DeclSpec::TST_class:
3605  case DeclSpec::TST_interface:
3606    Filter = &ResultBuilder::IsClassOrStruct;
3607    ContextKind = CodeCompletionContext::CCC_ClassOrStructTag;
3608    break;
3609
3610  default:
3611    llvm_unreachable("Unknown type specifier kind in CodeCompleteTag");
3612  }
3613
3614  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3615                        CodeCompleter->getCodeCompletionTUInfo(), ContextKind);
3616  CodeCompletionDeclConsumer Consumer(Results, CurContext);
3617
3618  // First pass: look for tags.
3619  Results.setFilter(Filter);
3620  LookupVisibleDecls(S, LookupTagName, Consumer,
3621                     CodeCompleter->includeGlobals());
3622
3623  if (CodeCompleter->includeGlobals()) {
3624    // Second pass: look for nested name specifiers.
3625    Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
3626    LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer);
3627  }
3628
3629  HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3630                            Results.data(),Results.size());
3631}
3632
3633void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) {
3634  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3635                        CodeCompleter->getCodeCompletionTUInfo(),
3636                        CodeCompletionContext::CCC_TypeQualifiers);
3637  Results.EnterNewScope();
3638  if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const))
3639    Results.AddResult("const");
3640  if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile))
3641    Results.AddResult("volatile");
3642  if (getLangOpts().C99 &&
3643      !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict))
3644    Results.AddResult("restrict");
3645  Results.ExitScope();
3646  HandleCodeCompleteResults(this, CodeCompleter,
3647                            Results.getCompletionContext(),
3648                            Results.data(), Results.size());
3649}
3650
3651void Sema::CodeCompleteCase(Scope *S) {
3652  if (getCurFunction()->SwitchStack.empty() || !CodeCompleter)
3653    return;
3654
3655  SwitchStmt *Switch = getCurFunction()->SwitchStack.back();
3656  QualType type = Switch->getCond()->IgnoreImplicit()->getType();
3657  if (!type->isEnumeralType()) {
3658    CodeCompleteExpressionData Data(type);
3659    Data.IntegralConstantExpression = true;
3660    CodeCompleteExpression(S, Data);
3661    return;
3662  }
3663
3664  // Code-complete the cases of a switch statement over an enumeration type
3665  // by providing the list of
3666  EnumDecl *Enum = type->castAs<EnumType>()->getDecl();
3667  if (EnumDecl *Def = Enum->getDefinition())
3668    Enum = Def;
3669
3670  // Determine which enumerators we have already seen in the switch statement.
3671  // FIXME: Ideally, we would also be able to look *past* the code-completion
3672  // token, in case we are code-completing in the middle of the switch and not
3673  // at the end. However, we aren't able to do so at the moment.
3674  llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen;
3675  NestedNameSpecifier *Qualifier = 0;
3676  for (SwitchCase *SC = Switch->getSwitchCaseList(); SC;
3677       SC = SC->getNextSwitchCase()) {
3678    CaseStmt *Case = dyn_cast<CaseStmt>(SC);
3679    if (!Case)
3680      continue;
3681
3682    Expr *CaseVal = Case->getLHS()->IgnoreParenCasts();
3683    if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal))
3684      if (EnumConstantDecl *Enumerator
3685            = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
3686        // We look into the AST of the case statement to determine which
3687        // enumerator was named. Alternatively, we could compute the value of
3688        // the integral constant expression, then compare it against the
3689        // values of each enumerator. However, value-based approach would not
3690        // work as well with C++ templates where enumerators declared within a
3691        // template are type- and value-dependent.
3692        EnumeratorsSeen.insert(Enumerator);
3693
3694        // If this is a qualified-id, keep track of the nested-name-specifier
3695        // so that we can reproduce it as part of code completion, e.g.,
3696        //
3697        //   switch (TagD.getKind()) {
3698        //     case TagDecl::TK_enum:
3699        //       break;
3700        //     case XXX
3701        //
3702        // At the XXX, our completions are TagDecl::TK_union,
3703        // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union,
3704        // TK_struct, and TK_class.
3705        Qualifier = DRE->getQualifier();
3706      }
3707  }
3708
3709  if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) {
3710    // If there are no prior enumerators in C++, check whether we have to
3711    // qualify the names of the enumerators that we suggest, because they
3712    // may not be visible in this scope.
3713    Qualifier = getRequiredQualification(Context, CurContext, Enum);
3714  }
3715
3716  // Add any enumerators that have not yet been mentioned.
3717  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3718                        CodeCompleter->getCodeCompletionTUInfo(),
3719                        CodeCompletionContext::CCC_Expression);
3720  Results.EnterNewScope();
3721  for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(),
3722                                  EEnd = Enum->enumerator_end();
3723       E != EEnd; ++E) {
3724    if (EnumeratorsSeen.count(*E))
3725      continue;
3726
3727    CodeCompletionResult R(*E, Qualifier);
3728    R.Priority = CCP_EnumInCase;
3729    Results.AddResult(R, CurContext, 0, false);
3730  }
3731  Results.ExitScope();
3732
3733  //We need to make sure we're setting the right context,
3734  //so only say we include macros if the code completer says we do
3735  enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other;
3736  if (CodeCompleter->includeMacros()) {
3737    AddMacroResults(PP, Results);
3738    kind = CodeCompletionContext::CCC_OtherWithMacros;
3739  }
3740
3741  HandleCodeCompleteResults(this, CodeCompleter,
3742                            kind,
3743                            Results.data(),Results.size());
3744}
3745
3746namespace {
3747  struct IsBetterOverloadCandidate {
3748    Sema &S;
3749    SourceLocation Loc;
3750
3751  public:
3752    explicit IsBetterOverloadCandidate(Sema &S, SourceLocation Loc)
3753      : S(S), Loc(Loc) { }
3754
3755    bool
3756    operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const {
3757      return isBetterOverloadCandidate(S, X, Y, Loc);
3758    }
3759  };
3760}
3761
3762static bool anyNullArguments(llvm::ArrayRef<Expr*> Args) {
3763  if (Args.size() && !Args.data())
3764    return true;
3765
3766  for (unsigned I = 0; I != Args.size(); ++I)
3767    if (!Args[I])
3768      return true;
3769
3770  return false;
3771}
3772
3773void Sema::CodeCompleteCall(Scope *S, Expr *FnIn,
3774                            llvm::ArrayRef<Expr *> Args) {
3775  if (!CodeCompleter)
3776    return;
3777
3778  // When we're code-completing for a call, we fall back to ordinary
3779  // name code-completion whenever we can't produce specific
3780  // results. We may want to revisit this strategy in the future,
3781  // e.g., by merging the two kinds of results.
3782
3783  Expr *Fn = (Expr *)FnIn;
3784
3785  // Ignore type-dependent call expressions entirely.
3786  if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) ||
3787      Expr::hasAnyTypeDependentArguments(Args)) {
3788    CodeCompleteOrdinaryName(S, PCC_Expression);
3789    return;
3790  }
3791
3792  // Build an overload candidate set based on the functions we find.
3793  SourceLocation Loc = Fn->getExprLoc();
3794  OverloadCandidateSet CandidateSet(Loc);
3795
3796  // FIXME: What if we're calling something that isn't a function declaration?
3797  // FIXME: What if we're calling a pseudo-destructor?
3798  // FIXME: What if we're calling a member function?
3799
3800  typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate;
3801  SmallVector<ResultCandidate, 8> Results;
3802
3803  Expr *NakedFn = Fn->IgnoreParenCasts();
3804  if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn))
3805    AddOverloadedCallCandidates(ULE, Args, CandidateSet,
3806                                /*PartialOverloading=*/ true);
3807  else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) {
3808    FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
3809    if (FDecl) {
3810      if (!getLangOpts().CPlusPlus ||
3811          !FDecl->getType()->getAs<FunctionProtoType>())
3812        Results.push_back(ResultCandidate(FDecl));
3813      else
3814        // FIXME: access?
3815        AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), Args,
3816                             CandidateSet, false, /*PartialOverloading*/true);
3817    }
3818  }
3819
3820  QualType ParamType;
3821
3822  if (!CandidateSet.empty()) {
3823    // Sort the overload candidate set by placing the best overloads first.
3824    std::stable_sort(CandidateSet.begin(), CandidateSet.end(),
3825                     IsBetterOverloadCandidate(*this, Loc));
3826
3827    // Add the remaining viable overload candidates as code-completion reslults.
3828    for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
3829                                     CandEnd = CandidateSet.end();
3830         Cand != CandEnd; ++Cand) {
3831      if (Cand->Viable)
3832        Results.push_back(ResultCandidate(Cand->Function));
3833    }
3834
3835    // From the viable candidates, try to determine the type of this parameter.
3836    for (unsigned I = 0, N = Results.size(); I != N; ++I) {
3837      if (const FunctionType *FType = Results[I].getFunctionType())
3838        if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FType))
3839          if (Args.size() < Proto->getNumArgs()) {
3840            if (ParamType.isNull())
3841              ParamType = Proto->getArgType(Args.size());
3842            else if (!Context.hasSameUnqualifiedType(
3843                                            ParamType.getNonReferenceType(),
3844                       Proto->getArgType(Args.size()).getNonReferenceType())) {
3845              ParamType = QualType();
3846              break;
3847            }
3848          }
3849    }
3850  } else {
3851    // Try to determine the parameter type from the type of the expression
3852    // being called.
3853    QualType FunctionType = Fn->getType();
3854    if (const PointerType *Ptr = FunctionType->getAs<PointerType>())
3855      FunctionType = Ptr->getPointeeType();
3856    else if (const BlockPointerType *BlockPtr
3857                                    = FunctionType->getAs<BlockPointerType>())
3858      FunctionType = BlockPtr->getPointeeType();
3859    else if (const MemberPointerType *MemPtr
3860                                    = FunctionType->getAs<MemberPointerType>())
3861      FunctionType = MemPtr->getPointeeType();
3862
3863    if (const FunctionProtoType *Proto
3864                                  = FunctionType->getAs<FunctionProtoType>()) {
3865      if (Args.size() < Proto->getNumArgs())
3866        ParamType = Proto->getArgType(Args.size());
3867    }
3868  }
3869
3870  if (ParamType.isNull())
3871    CodeCompleteOrdinaryName(S, PCC_Expression);
3872  else
3873    CodeCompleteExpression(S, ParamType);
3874
3875  if (!Results.empty())
3876    CodeCompleter->ProcessOverloadCandidates(*this, Args.size(), Results.data(),
3877                                             Results.size());
3878}
3879
3880void Sema::CodeCompleteInitializer(Scope *S, Decl *D) {
3881  ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D);
3882  if (!VD) {
3883    CodeCompleteOrdinaryName(S, PCC_Expression);
3884    return;
3885  }
3886
3887  CodeCompleteExpression(S, VD->getType());
3888}
3889
3890void Sema::CodeCompleteReturn(Scope *S) {
3891  QualType ResultType;
3892  if (isa<BlockDecl>(CurContext)) {
3893    if (BlockScopeInfo *BSI = getCurBlock())
3894      ResultType = BSI->ReturnType;
3895  } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext))
3896    ResultType = Function->getResultType();
3897  else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext))
3898    ResultType = Method->getResultType();
3899
3900  if (ResultType.isNull())
3901    CodeCompleteOrdinaryName(S, PCC_Expression);
3902  else
3903    CodeCompleteExpression(S, ResultType);
3904}
3905
3906void Sema::CodeCompleteAfterIf(Scope *S) {
3907  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3908                        CodeCompleter->getCodeCompletionTUInfo(),
3909                        mapCodeCompletionContext(*this, PCC_Statement));
3910  Results.setFilter(&ResultBuilder::IsOrdinaryName);
3911  Results.EnterNewScope();
3912
3913  CodeCompletionDeclConsumer Consumer(Results, CurContext);
3914  LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3915                     CodeCompleter->includeGlobals());
3916
3917  AddOrdinaryNameResults(PCC_Statement, S, *this, Results);
3918
3919  // "else" block
3920  CodeCompletionBuilder Builder(Results.getAllocator(),
3921                                Results.getCodeCompletionTUInfo());
3922  Builder.AddTypedTextChunk("else");
3923  if (Results.includeCodePatterns()) {
3924    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3925    Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
3926    Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3927    Builder.AddPlaceholderChunk("statements");
3928    Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3929    Builder.AddChunk(CodeCompletionString::CK_RightBrace);
3930  }
3931  Results.AddResult(Builder.TakeString());
3932
3933  // "else if" block
3934  Builder.AddTypedTextChunk("else");
3935  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3936  Builder.AddTextChunk("if");
3937  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3938  Builder.AddChunk(CodeCompletionString::CK_LeftParen);
3939  if (getLangOpts().CPlusPlus)
3940    Builder.AddPlaceholderChunk("condition");
3941  else
3942    Builder.AddPlaceholderChunk("expression");
3943  Builder.AddChunk(CodeCompletionString::CK_RightParen);
3944  if (Results.includeCodePatterns()) {
3945    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3946    Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
3947    Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3948    Builder.AddPlaceholderChunk("statements");
3949    Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3950    Builder.AddChunk(CodeCompletionString::CK_RightBrace);
3951  }
3952  Results.AddResult(Builder.TakeString());
3953
3954  Results.ExitScope();
3955
3956  if (S->getFnParent())
3957    AddPrettyFunctionResults(PP.getLangOpts(), Results);
3958
3959  if (CodeCompleter->includeMacros())
3960    AddMacroResults(PP, Results);
3961
3962  HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3963                            Results.data(),Results.size());
3964}
3965
3966void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) {
3967  if (LHS)
3968    CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType());
3969  else
3970    CodeCompleteOrdinaryName(S, PCC_Expression);
3971}
3972
3973void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS,
3974                                   bool EnteringContext) {
3975  if (!SS.getScopeRep() || !CodeCompleter)
3976    return;
3977
3978  DeclContext *Ctx = computeDeclContext(SS, EnteringContext);
3979  if (!Ctx)
3980    return;
3981
3982  // Try to instantiate any non-dependent declaration contexts before
3983  // we look in them.
3984  if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx))
3985    return;
3986
3987  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3988                        CodeCompleter->getCodeCompletionTUInfo(),
3989                        CodeCompletionContext::CCC_Name);
3990  Results.EnterNewScope();
3991
3992  // The "template" keyword can follow "::" in the grammar, but only
3993  // put it into the grammar if the nested-name-specifier is dependent.
3994  NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
3995  if (!Results.empty() && NNS->isDependent())
3996    Results.AddResult("template");
3997
3998  // Add calls to overridden virtual functions, if there are any.
3999  //
4000  // FIXME: This isn't wonderful, because we don't know whether we're actually
4001  // in a context that permits expressions. This is a general issue with
4002  // qualified-id completions.
4003  if (!EnteringContext)
4004    MaybeAddOverrideCalls(*this, Ctx, Results);
4005  Results.ExitScope();
4006
4007  CodeCompletionDeclConsumer Consumer(Results, CurContext);
4008  LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer);
4009
4010  HandleCodeCompleteResults(this, CodeCompleter,
4011                            Results.getCompletionContext(),
4012                            Results.data(),Results.size());
4013}
4014
4015void Sema::CodeCompleteUsing(Scope *S) {
4016  if (!CodeCompleter)
4017    return;
4018
4019  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4020                        CodeCompleter->getCodeCompletionTUInfo(),
4021                        CodeCompletionContext::CCC_PotentiallyQualifiedName,
4022                        &ResultBuilder::IsNestedNameSpecifier);
4023  Results.EnterNewScope();
4024
4025  // If we aren't in class scope, we could see the "namespace" keyword.
4026  if (!S->isClassScope())
4027    Results.AddResult(CodeCompletionResult("namespace"));
4028
4029  // After "using", we can see anything that would start a
4030  // nested-name-specifier.
4031  CodeCompletionDeclConsumer Consumer(Results, CurContext);
4032  LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4033                     CodeCompleter->includeGlobals());
4034  Results.ExitScope();
4035
4036  HandleCodeCompleteResults(this, CodeCompleter,
4037                            CodeCompletionContext::CCC_PotentiallyQualifiedName,
4038                            Results.data(),Results.size());
4039}
4040
4041void Sema::CodeCompleteUsingDirective(Scope *S) {
4042  if (!CodeCompleter)
4043    return;
4044
4045  // After "using namespace", we expect to see a namespace name or namespace
4046  // alias.
4047  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4048                        CodeCompleter->getCodeCompletionTUInfo(),
4049                        CodeCompletionContext::CCC_Namespace,
4050                        &ResultBuilder::IsNamespaceOrAlias);
4051  Results.EnterNewScope();
4052  CodeCompletionDeclConsumer Consumer(Results, CurContext);
4053  LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4054                     CodeCompleter->includeGlobals());
4055  Results.ExitScope();
4056  HandleCodeCompleteResults(this, CodeCompleter,
4057                            CodeCompletionContext::CCC_Namespace,
4058                            Results.data(),Results.size());
4059}
4060
4061void Sema::CodeCompleteNamespaceDecl(Scope *S)  {
4062  if (!CodeCompleter)
4063    return;
4064
4065  DeclContext *Ctx = (DeclContext *)S->getEntity();
4066  if (!S->getParent())
4067    Ctx = Context.getTranslationUnitDecl();
4068
4069  bool SuppressedGlobalResults
4070    = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx);
4071
4072  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4073                        CodeCompleter->getCodeCompletionTUInfo(),
4074                        SuppressedGlobalResults
4075                          ? CodeCompletionContext::CCC_Namespace
4076                          : CodeCompletionContext::CCC_Other,
4077                        &ResultBuilder::IsNamespace);
4078
4079  if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) {
4080    // We only want to see those namespaces that have already been defined
4081    // within this scope, because its likely that the user is creating an
4082    // extended namespace declaration. Keep track of the most recent
4083    // definition of each namespace.
4084    std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest;
4085    for (DeclContext::specific_decl_iterator<NamespaceDecl>
4086         NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end());
4087         NS != NSEnd; ++NS)
4088      OrigToLatest[NS->getOriginalNamespace()] = *NS;
4089
4090    // Add the most recent definition (or extended definition) of each
4091    // namespace to the list of results.
4092    Results.EnterNewScope();
4093    for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator
4094              NS = OrigToLatest.begin(),
4095           NSEnd = OrigToLatest.end();
4096         NS != NSEnd; ++NS)
4097      Results.AddResult(CodeCompletionResult(NS->second, 0),
4098                        CurContext, 0, false);
4099    Results.ExitScope();
4100  }
4101
4102  HandleCodeCompleteResults(this, CodeCompleter,
4103                            Results.getCompletionContext(),
4104                            Results.data(),Results.size());
4105}
4106
4107void Sema::CodeCompleteNamespaceAliasDecl(Scope *S)  {
4108  if (!CodeCompleter)
4109    return;
4110
4111  // After "namespace", we expect to see a namespace or alias.
4112  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4113                        CodeCompleter->getCodeCompletionTUInfo(),
4114                        CodeCompletionContext::CCC_Namespace,
4115                        &ResultBuilder::IsNamespaceOrAlias);
4116  CodeCompletionDeclConsumer Consumer(Results, CurContext);
4117  LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4118                     CodeCompleter->includeGlobals());
4119  HandleCodeCompleteResults(this, CodeCompleter,
4120                            Results.getCompletionContext(),
4121                            Results.data(),Results.size());
4122}
4123
4124void Sema::CodeCompleteOperatorName(Scope *S) {
4125  if (!CodeCompleter)
4126    return;
4127
4128  typedef CodeCompletionResult Result;
4129  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4130                        CodeCompleter->getCodeCompletionTUInfo(),
4131                        CodeCompletionContext::CCC_Type,
4132                        &ResultBuilder::IsType);
4133  Results.EnterNewScope();
4134
4135  // Add the names of overloadable operators.
4136#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly)      \
4137  if (std::strcmp(Spelling, "?"))                                                  \
4138    Results.AddResult(Result(Spelling));
4139#include "clang/Basic/OperatorKinds.def"
4140
4141  // Add any type names visible from the current scope
4142  Results.allowNestedNameSpecifiers();
4143  CodeCompletionDeclConsumer Consumer(Results, CurContext);
4144  LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4145                     CodeCompleter->includeGlobals());
4146
4147  // Add any type specifiers
4148  AddTypeSpecifierResults(getLangOpts(), Results);
4149  Results.ExitScope();
4150
4151  HandleCodeCompleteResults(this, CodeCompleter,
4152                            CodeCompletionContext::CCC_Type,
4153                            Results.data(),Results.size());
4154}
4155
4156void Sema::CodeCompleteConstructorInitializer(Decl *ConstructorD,
4157                                              CXXCtorInitializer** Initializers,
4158                                              unsigned NumInitializers) {
4159  PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
4160  CXXConstructorDecl *Constructor
4161    = static_cast<CXXConstructorDecl *>(ConstructorD);
4162  if (!Constructor)
4163    return;
4164
4165  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4166                        CodeCompleter->getCodeCompletionTUInfo(),
4167                        CodeCompletionContext::CCC_PotentiallyQualifiedName);
4168  Results.EnterNewScope();
4169
4170  // Fill in any already-initialized fields or base classes.
4171  llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields;
4172  llvm::SmallPtrSet<CanQualType, 4> InitializedBases;
4173  for (unsigned I = 0; I != NumInitializers; ++I) {
4174    if (Initializers[I]->isBaseInitializer())
4175      InitializedBases.insert(
4176        Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0)));
4177    else
4178      InitializedFields.insert(cast<FieldDecl>(
4179                               Initializers[I]->getAnyMember()));
4180  }
4181
4182  // Add completions for base classes.
4183  CodeCompletionBuilder Builder(Results.getAllocator(),
4184                                Results.getCodeCompletionTUInfo());
4185  bool SawLastInitializer = (NumInitializers == 0);
4186  CXXRecordDecl *ClassDecl = Constructor->getParent();
4187  for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(),
4188                                       BaseEnd = ClassDecl->bases_end();
4189       Base != BaseEnd; ++Base) {
4190    if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) {
4191      SawLastInitializer
4192        = NumInitializers > 0 &&
4193          Initializers[NumInitializers - 1]->isBaseInitializer() &&
4194          Context.hasSameUnqualifiedType(Base->getType(),
4195               QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0));
4196      continue;
4197    }
4198
4199    Builder.AddTypedTextChunk(
4200               Results.getAllocator().CopyString(
4201                          Base->getType().getAsString(Policy)));
4202    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4203    Builder.AddPlaceholderChunk("args");
4204    Builder.AddChunk(CodeCompletionString::CK_RightParen);
4205    Results.AddResult(CodeCompletionResult(Builder.TakeString(),
4206                                   SawLastInitializer? CCP_NextInitializer
4207                                                     : CCP_MemberDeclaration));
4208    SawLastInitializer = false;
4209  }
4210
4211  // Add completions for virtual base classes.
4212  for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(),
4213                                       BaseEnd = ClassDecl->vbases_end();
4214       Base != BaseEnd; ++Base) {
4215    if (!InitializedBases.insert(Context.getCanonicalType(Base->getType()))) {
4216      SawLastInitializer
4217        = NumInitializers > 0 &&
4218          Initializers[NumInitializers - 1]->isBaseInitializer() &&
4219          Context.hasSameUnqualifiedType(Base->getType(),
4220               QualType(Initializers[NumInitializers - 1]->getBaseClass(), 0));
4221      continue;
4222    }
4223
4224    Builder.AddTypedTextChunk(
4225               Builder.getAllocator().CopyString(
4226                          Base->getType().getAsString(Policy)));
4227    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4228    Builder.AddPlaceholderChunk("args");
4229    Builder.AddChunk(CodeCompletionString::CK_RightParen);
4230    Results.AddResult(CodeCompletionResult(Builder.TakeString(),
4231                                   SawLastInitializer? CCP_NextInitializer
4232                                                     : CCP_MemberDeclaration));
4233    SawLastInitializer = false;
4234  }
4235
4236  // Add completions for members.
4237  for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
4238                                  FieldEnd = ClassDecl->field_end();
4239       Field != FieldEnd; ++Field) {
4240    if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))) {
4241      SawLastInitializer
4242        = NumInitializers > 0 &&
4243          Initializers[NumInitializers - 1]->isAnyMemberInitializer() &&
4244          Initializers[NumInitializers - 1]->getAnyMember() == *Field;
4245      continue;
4246    }
4247
4248    if (!Field->getDeclName())
4249      continue;
4250
4251    Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
4252                                         Field->getIdentifier()->getName()));
4253    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4254    Builder.AddPlaceholderChunk("args");
4255    Builder.AddChunk(CodeCompletionString::CK_RightParen);
4256    Results.AddResult(CodeCompletionResult(Builder.TakeString(),
4257                                   SawLastInitializer? CCP_NextInitializer
4258                                                     : CCP_MemberDeclaration,
4259                                           CXCursor_MemberRef,
4260                                           CXAvailability_Available,
4261                                           *Field));
4262    SawLastInitializer = false;
4263  }
4264  Results.ExitScope();
4265
4266  HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4267                            Results.data(), Results.size());
4268}
4269
4270/// \brief Determine whether this scope denotes a namespace.
4271static bool isNamespaceScope(Scope *S) {
4272  DeclContext *DC = static_cast<DeclContext *>(S->getEntity());
4273  if (!DC)
4274    return false;
4275
4276  return DC->isFileContext();
4277}
4278
4279void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro,
4280                                        bool AfterAmpersand) {
4281  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4282                        CodeCompleter->getCodeCompletionTUInfo(),
4283                        CodeCompletionContext::CCC_Other);
4284  Results.EnterNewScope();
4285
4286  // Note what has already been captured.
4287  llvm::SmallPtrSet<IdentifierInfo *, 4> Known;
4288  bool IncludedThis = false;
4289  for (SmallVectorImpl<LambdaCapture>::iterator C = Intro.Captures.begin(),
4290                                             CEnd = Intro.Captures.end();
4291       C != CEnd; ++C) {
4292    if (C->Kind == LCK_This) {
4293      IncludedThis = true;
4294      continue;
4295    }
4296
4297    Known.insert(C->Id);
4298  }
4299
4300  // Look for other capturable variables.
4301  for (; S && !isNamespaceScope(S); S = S->getParent()) {
4302    for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
4303         D != DEnd; ++D) {
4304      VarDecl *Var = dyn_cast<VarDecl>(*D);
4305      if (!Var ||
4306          !Var->hasLocalStorage() ||
4307          Var->hasAttr<BlocksAttr>())
4308        continue;
4309
4310      if (Known.insert(Var->getIdentifier()))
4311        Results.AddResult(CodeCompletionResult(Var), CurContext, 0, false);
4312    }
4313  }
4314
4315  // Add 'this', if it would be valid.
4316  if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy)
4317    addThisCompletion(*this, Results);
4318
4319  Results.ExitScope();
4320
4321  HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4322                            Results.data(), Results.size());
4323}
4324
4325/// Macro that optionally prepends an "@" to the string literal passed in via
4326/// Keyword, depending on whether NeedAt is true or false.
4327#define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword)
4328
4329static void AddObjCImplementationResults(const LangOptions &LangOpts,
4330                                         ResultBuilder &Results,
4331                                         bool NeedAt) {
4332  typedef CodeCompletionResult Result;
4333  // Since we have an implementation, we can end it.
4334  Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
4335
4336  CodeCompletionBuilder Builder(Results.getAllocator(),
4337                                Results.getCodeCompletionTUInfo());
4338  if (LangOpts.ObjC2) {
4339    // @dynamic
4340    Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic"));
4341    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4342    Builder.AddPlaceholderChunk("property");
4343    Results.AddResult(Result(Builder.TakeString()));
4344
4345    // @synthesize
4346    Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize"));
4347    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4348    Builder.AddPlaceholderChunk("property");
4349    Results.AddResult(Result(Builder.TakeString()));
4350  }
4351}
4352
4353static void AddObjCInterfaceResults(const LangOptions &LangOpts,
4354                                    ResultBuilder &Results,
4355                                    bool NeedAt) {
4356  typedef CodeCompletionResult Result;
4357
4358  // Since we have an interface or protocol, we can end it.
4359  Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
4360
4361  if (LangOpts.ObjC2) {
4362    // @property
4363    Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property")));
4364
4365    // @required
4366    Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required")));
4367
4368    // @optional
4369    Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional")));
4370  }
4371}
4372
4373static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) {
4374  typedef CodeCompletionResult Result;
4375  CodeCompletionBuilder Builder(Results.getAllocator(),
4376                                Results.getCodeCompletionTUInfo());
4377
4378  // @class name ;
4379  Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class"));
4380  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4381  Builder.AddPlaceholderChunk("name");
4382  Results.AddResult(Result(Builder.TakeString()));
4383
4384  if (Results.includeCodePatterns()) {
4385    // @interface name
4386    // FIXME: Could introduce the whole pattern, including superclasses and
4387    // such.
4388    Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface"));
4389    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4390    Builder.AddPlaceholderChunk("class");
4391    Results.AddResult(Result(Builder.TakeString()));
4392
4393    // @protocol name
4394    Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
4395    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4396    Builder.AddPlaceholderChunk("protocol");
4397    Results.AddResult(Result(Builder.TakeString()));
4398
4399    // @implementation name
4400    Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation"));
4401    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4402    Builder.AddPlaceholderChunk("class");
4403    Results.AddResult(Result(Builder.TakeString()));
4404  }
4405
4406  // @compatibility_alias name
4407  Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias"));
4408  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4409  Builder.AddPlaceholderChunk("alias");
4410  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4411  Builder.AddPlaceholderChunk("class");
4412  Results.AddResult(Result(Builder.TakeString()));
4413}
4414
4415void Sema::CodeCompleteObjCAtDirective(Scope *S) {
4416  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4417                        CodeCompleter->getCodeCompletionTUInfo(),
4418                        CodeCompletionContext::CCC_Other);
4419  Results.EnterNewScope();
4420  if (isa<ObjCImplDecl>(CurContext))
4421    AddObjCImplementationResults(getLangOpts(), Results, false);
4422  else if (CurContext->isObjCContainer())
4423    AddObjCInterfaceResults(getLangOpts(), Results, false);
4424  else
4425    AddObjCTopLevelResults(Results, false);
4426  Results.ExitScope();
4427  HandleCodeCompleteResults(this, CodeCompleter,
4428                            CodeCompletionContext::CCC_Other,
4429                            Results.data(),Results.size());
4430}
4431
4432static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) {
4433  typedef CodeCompletionResult Result;
4434  CodeCompletionBuilder Builder(Results.getAllocator(),
4435                                Results.getCodeCompletionTUInfo());
4436
4437  // @encode ( type-name )
4438  const char *EncodeType = "char[]";
4439  if (Results.getSema().getLangOpts().CPlusPlus ||
4440      Results.getSema().getLangOpts().ConstStrings)
4441    EncodeType = "const char[]";
4442  Builder.AddResultTypeChunk(EncodeType);
4443  Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode"));
4444  Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4445  Builder.AddPlaceholderChunk("type-name");
4446  Builder.AddChunk(CodeCompletionString::CK_RightParen);
4447  Results.AddResult(Result(Builder.TakeString()));
4448
4449  // @protocol ( protocol-name )
4450  Builder.AddResultTypeChunk("Protocol *");
4451  Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
4452  Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4453  Builder.AddPlaceholderChunk("protocol-name");
4454  Builder.AddChunk(CodeCompletionString::CK_RightParen);
4455  Results.AddResult(Result(Builder.TakeString()));
4456
4457  // @selector ( selector )
4458  Builder.AddResultTypeChunk("SEL");
4459  Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector"));
4460  Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4461  Builder.AddPlaceholderChunk("selector");
4462  Builder.AddChunk(CodeCompletionString::CK_RightParen);
4463  Results.AddResult(Result(Builder.TakeString()));
4464
4465  // @"string"
4466  Builder.AddResultTypeChunk("NSString *");
4467  Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\""));
4468  Builder.AddPlaceholderChunk("string");
4469  Builder.AddTextChunk("\"");
4470  Results.AddResult(Result(Builder.TakeString()));
4471
4472  // @[objects, ...]
4473  Builder.AddResultTypeChunk("NSArray *");
4474  Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"["));
4475  Builder.AddPlaceholderChunk("objects, ...");
4476  Builder.AddChunk(CodeCompletionString::CK_RightBracket);
4477  Results.AddResult(Result(Builder.TakeString()));
4478
4479  // @{key : object, ...}
4480  Builder.AddResultTypeChunk("NSDictionary *");
4481  Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{"));
4482  Builder.AddPlaceholderChunk("key");
4483  Builder.AddChunk(CodeCompletionString::CK_Colon);
4484  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4485  Builder.AddPlaceholderChunk("object, ...");
4486  Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4487  Results.AddResult(Result(Builder.TakeString()));
4488
4489  // @(expression)
4490  Builder.AddResultTypeChunk("id");
4491  Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "("));
4492  Builder.AddPlaceholderChunk("expression");
4493  Builder.AddChunk(CodeCompletionString::CK_RightParen);
4494  Results.AddResult(Result(Builder.TakeString()));
4495}
4496
4497static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) {
4498  typedef CodeCompletionResult Result;
4499  CodeCompletionBuilder Builder(Results.getAllocator(),
4500                                Results.getCodeCompletionTUInfo());
4501
4502  if (Results.includeCodePatterns()) {
4503    // @try { statements } @catch ( declaration ) { statements } @finally
4504    //   { statements }
4505    Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try"));
4506    Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4507    Builder.AddPlaceholderChunk("statements");
4508    Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4509    Builder.AddTextChunk("@catch");
4510    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4511    Builder.AddPlaceholderChunk("parameter");
4512    Builder.AddChunk(CodeCompletionString::CK_RightParen);
4513    Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4514    Builder.AddPlaceholderChunk("statements");
4515    Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4516    Builder.AddTextChunk("@finally");
4517    Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4518    Builder.AddPlaceholderChunk("statements");
4519    Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4520    Results.AddResult(Result(Builder.TakeString()));
4521  }
4522
4523  // @throw
4524  Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw"));
4525  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4526  Builder.AddPlaceholderChunk("expression");
4527  Results.AddResult(Result(Builder.TakeString()));
4528
4529  if (Results.includeCodePatterns()) {
4530    // @synchronized ( expression ) { statements }
4531    Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized"));
4532    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4533    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4534    Builder.AddPlaceholderChunk("expression");
4535    Builder.AddChunk(CodeCompletionString::CK_RightParen);
4536    Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4537    Builder.AddPlaceholderChunk("statements");
4538    Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4539    Results.AddResult(Result(Builder.TakeString()));
4540  }
4541}
4542
4543static void AddObjCVisibilityResults(const LangOptions &LangOpts,
4544                                     ResultBuilder &Results,
4545                                     bool NeedAt) {
4546  typedef CodeCompletionResult Result;
4547  Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private")));
4548  Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected")));
4549  Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public")));
4550  if (LangOpts.ObjC2)
4551    Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package")));
4552}
4553
4554void Sema::CodeCompleteObjCAtVisibility(Scope *S) {
4555  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4556                        CodeCompleter->getCodeCompletionTUInfo(),
4557                        CodeCompletionContext::CCC_Other);
4558  Results.EnterNewScope();
4559  AddObjCVisibilityResults(getLangOpts(), Results, false);
4560  Results.ExitScope();
4561  HandleCodeCompleteResults(this, CodeCompleter,
4562                            CodeCompletionContext::CCC_Other,
4563                            Results.data(),Results.size());
4564}
4565
4566void Sema::CodeCompleteObjCAtStatement(Scope *S) {
4567  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4568                        CodeCompleter->getCodeCompletionTUInfo(),
4569                        CodeCompletionContext::CCC_Other);
4570  Results.EnterNewScope();
4571  AddObjCStatementResults(Results, false);
4572  AddObjCExpressionResults(Results, false);
4573  Results.ExitScope();
4574  HandleCodeCompleteResults(this, CodeCompleter,
4575                            CodeCompletionContext::CCC_Other,
4576                            Results.data(),Results.size());
4577}
4578
4579void Sema::CodeCompleteObjCAtExpression(Scope *S) {
4580  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4581                        CodeCompleter->getCodeCompletionTUInfo(),
4582                        CodeCompletionContext::CCC_Other);
4583  Results.EnterNewScope();
4584  AddObjCExpressionResults(Results, false);
4585  Results.ExitScope();
4586  HandleCodeCompleteResults(this, CodeCompleter,
4587                            CodeCompletionContext::CCC_Other,
4588                            Results.data(),Results.size());
4589}
4590
4591/// \brief Determine whether the addition of the given flag to an Objective-C
4592/// property's attributes will cause a conflict.
4593static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
4594  // Check if we've already added this flag.
4595  if (Attributes & NewFlag)
4596    return true;
4597
4598  Attributes |= NewFlag;
4599
4600  // Check for collisions with "readonly".
4601  if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
4602      (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
4603    return true;
4604
4605  // Check for more than one of { assign, copy, retain, strong, weak }.
4606  unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign |
4607                                         ObjCDeclSpec::DQ_PR_unsafe_unretained |
4608                                             ObjCDeclSpec::DQ_PR_copy |
4609                                             ObjCDeclSpec::DQ_PR_retain |
4610                                             ObjCDeclSpec::DQ_PR_strong |
4611                                             ObjCDeclSpec::DQ_PR_weak);
4612  if (AssignCopyRetMask &&
4613      AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign &&
4614      AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained &&
4615      AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy &&
4616      AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain &&
4617      AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong &&
4618      AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak)
4619    return true;
4620
4621  return false;
4622}
4623
4624void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
4625  if (!CodeCompleter)
4626    return;
4627
4628  unsigned Attributes = ODS.getPropertyAttributes();
4629
4630  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4631                        CodeCompleter->getCodeCompletionTUInfo(),
4632                        CodeCompletionContext::CCC_Other);
4633  Results.EnterNewScope();
4634  if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly))
4635    Results.AddResult(CodeCompletionResult("readonly"));
4636  if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign))
4637    Results.AddResult(CodeCompletionResult("assign"));
4638  if (!ObjCPropertyFlagConflicts(Attributes,
4639                                 ObjCDeclSpec::DQ_PR_unsafe_unretained))
4640    Results.AddResult(CodeCompletionResult("unsafe_unretained"));
4641  if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite))
4642    Results.AddResult(CodeCompletionResult("readwrite"));
4643  if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain))
4644    Results.AddResult(CodeCompletionResult("retain"));
4645  if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong))
4646    Results.AddResult(CodeCompletionResult("strong"));
4647  if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy))
4648    Results.AddResult(CodeCompletionResult("copy"));
4649  if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic))
4650    Results.AddResult(CodeCompletionResult("nonatomic"));
4651  if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic))
4652    Results.AddResult(CodeCompletionResult("atomic"));
4653
4654  // Only suggest "weak" if we're compiling for ARC-with-weak-references or GC.
4655  if (getLangOpts().ObjCARCWeak || getLangOpts().getGC() != LangOptions::NonGC)
4656    if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak))
4657      Results.AddResult(CodeCompletionResult("weak"));
4658
4659  if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) {
4660    CodeCompletionBuilder Setter(Results.getAllocator(),
4661                                 Results.getCodeCompletionTUInfo());
4662    Setter.AddTypedTextChunk("setter");
4663    Setter.AddTextChunk(" = ");
4664    Setter.AddPlaceholderChunk("method");
4665    Results.AddResult(CodeCompletionResult(Setter.TakeString()));
4666  }
4667  if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) {
4668    CodeCompletionBuilder Getter(Results.getAllocator(),
4669                                 Results.getCodeCompletionTUInfo());
4670    Getter.AddTypedTextChunk("getter");
4671    Getter.AddTextChunk(" = ");
4672    Getter.AddPlaceholderChunk("method");
4673    Results.AddResult(CodeCompletionResult(Getter.TakeString()));
4674  }
4675  Results.ExitScope();
4676  HandleCodeCompleteResults(this, CodeCompleter,
4677                            CodeCompletionContext::CCC_Other,
4678                            Results.data(),Results.size());
4679}
4680
4681/// \brief Describes the kind of Objective-C method that we want to find
4682/// via code completion.
4683enum ObjCMethodKind {
4684  MK_Any, ///< Any kind of method, provided it means other specified criteria.
4685  MK_ZeroArgSelector, ///< Zero-argument (unary) selector.
4686  MK_OneArgSelector ///< One-argument selector.
4687};
4688
4689static bool isAcceptableObjCSelector(Selector Sel,
4690                                     ObjCMethodKind WantKind,
4691                                     IdentifierInfo **SelIdents,
4692                                     unsigned NumSelIdents,
4693                                     bool AllowSameLength = true) {
4694  if (NumSelIdents > Sel.getNumArgs())
4695    return false;
4696
4697  switch (WantKind) {
4698    case MK_Any:             break;
4699    case MK_ZeroArgSelector: return Sel.isUnarySelector();
4700    case MK_OneArgSelector:  return Sel.getNumArgs() == 1;
4701  }
4702
4703  if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs())
4704    return false;
4705
4706  for (unsigned I = 0; I != NumSelIdents; ++I)
4707    if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
4708      return false;
4709
4710  return true;
4711}
4712
4713static bool isAcceptableObjCMethod(ObjCMethodDecl *Method,
4714                                   ObjCMethodKind WantKind,
4715                                   IdentifierInfo **SelIdents,
4716                                   unsigned NumSelIdents,
4717                                   bool AllowSameLength = true) {
4718  return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents,
4719                                  NumSelIdents, AllowSameLength);
4720}
4721
4722namespace {
4723  /// \brief A set of selectors, which is used to avoid introducing multiple
4724  /// completions with the same selector into the result set.
4725  typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet;
4726}
4727
4728/// \brief Add all of the Objective-C methods in the given Objective-C
4729/// container to the set of results.
4730///
4731/// The container will be a class, protocol, category, or implementation of
4732/// any of the above. This mether will recurse to include methods from
4733/// the superclasses of classes along with their categories, protocols, and
4734/// implementations.
4735///
4736/// \param Container the container in which we'll look to find methods.
4737///
4738/// \param WantInstanceMethods Whether to add instance methods (only); if
4739/// false, this routine will add factory methods (only).
4740///
4741/// \param CurContext the context in which we're performing the lookup that
4742/// finds methods.
4743///
4744/// \param AllowSameLength Whether we allow a method to be added to the list
4745/// when it has the same number of parameters as we have selector identifiers.
4746///
4747/// \param Results the structure into which we'll add results.
4748static void AddObjCMethods(ObjCContainerDecl *Container,
4749                           bool WantInstanceMethods,
4750                           ObjCMethodKind WantKind,
4751                           IdentifierInfo **SelIdents,
4752                           unsigned NumSelIdents,
4753                           DeclContext *CurContext,
4754                           VisitedSelectorSet &Selectors,
4755                           bool AllowSameLength,
4756                           ResultBuilder &Results,
4757                           bool InOriginalClass = true) {
4758  typedef CodeCompletionResult Result;
4759  Container = getContainerDef(Container);
4760  for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
4761                                       MEnd = Container->meth_end();
4762       M != MEnd; ++M) {
4763    if (M->isInstanceMethod() == WantInstanceMethods) {
4764      // Check whether the selector identifiers we've been given are a
4765      // subset of the identifiers for this particular method.
4766      if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents,
4767                                  AllowSameLength))
4768        continue;
4769
4770      if (!Selectors.insert(M->getSelector()))
4771        continue;
4772
4773      Result R = Result(*M, 0);
4774      R.StartParameter = NumSelIdents;
4775      R.AllParametersAreInformative = (WantKind != MK_Any);
4776      if (!InOriginalClass)
4777        R.Priority += CCD_InBaseClass;
4778      Results.MaybeAddResult(R, CurContext);
4779    }
4780  }
4781
4782  // Visit the protocols of protocols.
4783  if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
4784    if (Protocol->hasDefinition()) {
4785      const ObjCList<ObjCProtocolDecl> &Protocols
4786        = Protocol->getReferencedProtocols();
4787      for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
4788                                                E = Protocols.end();
4789           I != E; ++I)
4790        AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
4791                       NumSelIdents, CurContext, Selectors, AllowSameLength,
4792                       Results, false);
4793    }
4794  }
4795
4796  ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
4797  if (!IFace || !IFace->hasDefinition())
4798    return;
4799
4800  // Add methods in protocols.
4801  for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(),
4802                                            E = IFace->protocol_end();
4803       I != E; ++I)
4804    AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents,
4805                   CurContext, Selectors, AllowSameLength, Results, false);
4806
4807  // Add methods in categories.
4808  for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl;
4809       CatDecl = CatDecl->getNextClassCategory()) {
4810    AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents,
4811                   NumSelIdents, CurContext, Selectors, AllowSameLength,
4812                   Results, InOriginalClass);
4813
4814    // Add a categories protocol methods.
4815    const ObjCList<ObjCProtocolDecl> &Protocols
4816      = CatDecl->getReferencedProtocols();
4817    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
4818                                              E = Protocols.end();
4819         I != E; ++I)
4820      AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
4821                     NumSelIdents, CurContext, Selectors, AllowSameLength,
4822                     Results, false);
4823
4824    // Add methods in category implementations.
4825    if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
4826      AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
4827                     NumSelIdents, CurContext, Selectors, AllowSameLength,
4828                     Results, InOriginalClass);
4829  }
4830
4831  // Add methods in superclass.
4832  if (IFace->getSuperClass())
4833    AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind,
4834                   SelIdents, NumSelIdents, CurContext, Selectors,
4835                   AllowSameLength, Results, false);
4836
4837  // Add methods in our implementation, if any.
4838  if (ObjCImplementationDecl *Impl = IFace->getImplementation())
4839    AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
4840                   NumSelIdents, CurContext, Selectors, AllowSameLength,
4841                   Results, InOriginalClass);
4842}
4843
4844
4845void Sema::CodeCompleteObjCPropertyGetter(Scope *S) {
4846  // Try to find the interface where getters might live.
4847  ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
4848  if (!Class) {
4849    if (ObjCCategoryDecl *Category
4850          = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
4851      Class = Category->getClassInterface();
4852
4853    if (!Class)
4854      return;
4855  }
4856
4857  // Find all of the potential getters.
4858  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4859                        CodeCompleter->getCodeCompletionTUInfo(),
4860                        CodeCompletionContext::CCC_Other);
4861  Results.EnterNewScope();
4862
4863  VisitedSelectorSet Selectors;
4864  AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Selectors,
4865                 /*AllowSameLength=*/true, Results);
4866  Results.ExitScope();
4867  HandleCodeCompleteResults(this, CodeCompleter,
4868                            CodeCompletionContext::CCC_Other,
4869                            Results.data(),Results.size());
4870}
4871
4872void Sema::CodeCompleteObjCPropertySetter(Scope *S) {
4873  // Try to find the interface where setters might live.
4874  ObjCInterfaceDecl *Class
4875    = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
4876  if (!Class) {
4877    if (ObjCCategoryDecl *Category
4878          = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
4879      Class = Category->getClassInterface();
4880
4881    if (!Class)
4882      return;
4883  }
4884
4885  // Find all of the potential getters.
4886  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4887                        CodeCompleter->getCodeCompletionTUInfo(),
4888                        CodeCompletionContext::CCC_Other);
4889  Results.EnterNewScope();
4890
4891  VisitedSelectorSet Selectors;
4892  AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext,
4893                 Selectors, /*AllowSameLength=*/true, Results);
4894
4895  Results.ExitScope();
4896  HandleCodeCompleteResults(this, CodeCompleter,
4897                            CodeCompletionContext::CCC_Other,
4898                            Results.data(),Results.size());
4899}
4900
4901void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS,
4902                                       bool IsParameter) {
4903  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4904                        CodeCompleter->getCodeCompletionTUInfo(),
4905                        CodeCompletionContext::CCC_Type);
4906  Results.EnterNewScope();
4907
4908  // Add context-sensitive, Objective-C parameter-passing keywords.
4909  bool AddedInOut = false;
4910  if ((DS.getObjCDeclQualifier() &
4911       (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) {
4912    Results.AddResult("in");
4913    Results.AddResult("inout");
4914    AddedInOut = true;
4915  }
4916  if ((DS.getObjCDeclQualifier() &
4917       (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) {
4918    Results.AddResult("out");
4919    if (!AddedInOut)
4920      Results.AddResult("inout");
4921  }
4922  if ((DS.getObjCDeclQualifier() &
4923       (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref |
4924        ObjCDeclSpec::DQ_Oneway)) == 0) {
4925     Results.AddResult("bycopy");
4926     Results.AddResult("byref");
4927     Results.AddResult("oneway");
4928  }
4929
4930  // If we're completing the return type of an Objective-C method and the
4931  // identifier IBAction refers to a macro, provide a completion item for
4932  // an action, e.g.,
4933  //   IBAction)<#selector#>:(id)sender
4934  if (DS.getObjCDeclQualifier() == 0 && !IsParameter &&
4935      Context.Idents.get("IBAction").hasMacroDefinition()) {
4936    CodeCompletionBuilder Builder(Results.getAllocator(),
4937                                  Results.getCodeCompletionTUInfo(),
4938                                  CCP_CodePattern, CXAvailability_Available);
4939    Builder.AddTypedTextChunk("IBAction");
4940    Builder.AddChunk(CodeCompletionString::CK_RightParen);
4941    Builder.AddPlaceholderChunk("selector");
4942    Builder.AddChunk(CodeCompletionString::CK_Colon);
4943    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4944    Builder.AddTextChunk("id");
4945    Builder.AddChunk(CodeCompletionString::CK_RightParen);
4946    Builder.AddTextChunk("sender");
4947    Results.AddResult(CodeCompletionResult(Builder.TakeString()));
4948  }
4949
4950  // Add various builtin type names and specifiers.
4951  AddOrdinaryNameResults(PCC_Type, S, *this, Results);
4952  Results.ExitScope();
4953
4954  // Add the various type names
4955  Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
4956  CodeCompletionDeclConsumer Consumer(Results, CurContext);
4957  LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4958                     CodeCompleter->includeGlobals());
4959
4960  if (CodeCompleter->includeMacros())
4961    AddMacroResults(PP, Results);
4962
4963  HandleCodeCompleteResults(this, CodeCompleter,
4964                            CodeCompletionContext::CCC_Type,
4965                            Results.data(), Results.size());
4966}
4967
4968/// \brief When we have an expression with type "id", we may assume
4969/// that it has some more-specific class type based on knowledge of
4970/// common uses of Objective-C. This routine returns that class type,
4971/// or NULL if no better result could be determined.
4972static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) {
4973  ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E);
4974  if (!Msg)
4975    return 0;
4976
4977  Selector Sel = Msg->getSelector();
4978  if (Sel.isNull())
4979    return 0;
4980
4981  IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0);
4982  if (!Id)
4983    return 0;
4984
4985  ObjCMethodDecl *Method = Msg->getMethodDecl();
4986  if (!Method)
4987    return 0;
4988
4989  // Determine the class that we're sending the message to.
4990  ObjCInterfaceDecl *IFace = 0;
4991  switch (Msg->getReceiverKind()) {
4992  case ObjCMessageExpr::Class:
4993    if (const ObjCObjectType *ObjType
4994                           = Msg->getClassReceiver()->getAs<ObjCObjectType>())
4995      IFace = ObjType->getInterface();
4996    break;
4997
4998  case ObjCMessageExpr::Instance: {
4999    QualType T = Msg->getInstanceReceiver()->getType();
5000    if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
5001      IFace = Ptr->getInterfaceDecl();
5002    break;
5003  }
5004
5005  case ObjCMessageExpr::SuperInstance:
5006  case ObjCMessageExpr::SuperClass:
5007    break;
5008  }
5009
5010  if (!IFace)
5011    return 0;
5012
5013  ObjCInterfaceDecl *Super = IFace->getSuperClass();
5014  if (Method->isInstanceMethod())
5015    return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5016      .Case("retain", IFace)
5017      .Case("strong", IFace)
5018      .Case("autorelease", IFace)
5019      .Case("copy", IFace)
5020      .Case("copyWithZone", IFace)
5021      .Case("mutableCopy", IFace)
5022      .Case("mutableCopyWithZone", IFace)
5023      .Case("awakeFromCoder", IFace)
5024      .Case("replacementObjectFromCoder", IFace)
5025      .Case("class", IFace)
5026      .Case("classForCoder", IFace)
5027      .Case("superclass", Super)
5028      .Default(0);
5029
5030  return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5031    .Case("new", IFace)
5032    .Case("alloc", IFace)
5033    .Case("allocWithZone", IFace)
5034    .Case("class", IFace)
5035    .Case("superclass", Super)
5036    .Default(0);
5037}
5038
5039// Add a special completion for a message send to "super", which fills in the
5040// most likely case of forwarding all of our arguments to the superclass
5041// function.
5042///
5043/// \param S The semantic analysis object.
5044///
5045/// \param NeedSuperKeyword Whether we need to prefix this completion with
5046/// the "super" keyword. Otherwise, we just need to provide the arguments.
5047///
5048/// \param SelIdents The identifiers in the selector that have already been
5049/// provided as arguments for a send to "super".
5050///
5051/// \param NumSelIdents The number of identifiers in \p SelIdents.
5052///
5053/// \param Results The set of results to augment.
5054///
5055/// \returns the Objective-C method declaration that would be invoked by
5056/// this "super" completion. If NULL, no completion was added.
5057static ObjCMethodDecl *AddSuperSendCompletion(Sema &S, bool NeedSuperKeyword,
5058                                              IdentifierInfo **SelIdents,
5059                                              unsigned NumSelIdents,
5060                                              ResultBuilder &Results) {
5061  ObjCMethodDecl *CurMethod = S.getCurMethodDecl();
5062  if (!CurMethod)
5063    return 0;
5064
5065  ObjCInterfaceDecl *Class = CurMethod->getClassInterface();
5066  if (!Class)
5067    return 0;
5068
5069  // Try to find a superclass method with the same selector.
5070  ObjCMethodDecl *SuperMethod = 0;
5071  while ((Class = Class->getSuperClass()) && !SuperMethod) {
5072    // Check in the class
5073    SuperMethod = Class->getMethod(CurMethod->getSelector(),
5074                                   CurMethod->isInstanceMethod());
5075
5076    // Check in categories or class extensions.
5077    if (!SuperMethod) {
5078      for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
5079           Category = Category->getNextClassCategory())
5080        if ((SuperMethod = Category->getMethod(CurMethod->getSelector(),
5081                                               CurMethod->isInstanceMethod())))
5082          break;
5083    }
5084  }
5085
5086  if (!SuperMethod)
5087    return 0;
5088
5089  // Check whether the superclass method has the same signature.
5090  if (CurMethod->param_size() != SuperMethod->param_size() ||
5091      CurMethod->isVariadic() != SuperMethod->isVariadic())
5092    return 0;
5093
5094  for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(),
5095                                   CurPEnd = CurMethod->param_end(),
5096                                    SuperP = SuperMethod->param_begin();
5097       CurP != CurPEnd; ++CurP, ++SuperP) {
5098    // Make sure the parameter types are compatible.
5099    if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(),
5100                                          (*SuperP)->getType()))
5101      return 0;
5102
5103    // Make sure we have a parameter name to forward!
5104    if (!(*CurP)->getIdentifier())
5105      return 0;
5106  }
5107
5108  // We have a superclass method. Now, form the send-to-super completion.
5109  CodeCompletionBuilder Builder(Results.getAllocator(),
5110                                Results.getCodeCompletionTUInfo());
5111
5112  // Give this completion a return type.
5113  AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod,
5114                     Builder);
5115
5116  // If we need the "super" keyword, add it (plus some spacing).
5117  if (NeedSuperKeyword) {
5118    Builder.AddTypedTextChunk("super");
5119    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
5120  }
5121
5122  Selector Sel = CurMethod->getSelector();
5123  if (Sel.isUnarySelector()) {
5124    if (NeedSuperKeyword)
5125      Builder.AddTextChunk(Builder.getAllocator().CopyString(
5126                                  Sel.getNameForSlot(0)));
5127    else
5128      Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
5129                                   Sel.getNameForSlot(0)));
5130  } else {
5131    ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin();
5132    for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) {
5133      if (I > NumSelIdents)
5134        Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
5135
5136      if (I < NumSelIdents)
5137        Builder.AddInformativeChunk(
5138                   Builder.getAllocator().CopyString(
5139                                                 Sel.getNameForSlot(I) + ":"));
5140      else if (NeedSuperKeyword || I > NumSelIdents) {
5141        Builder.AddTextChunk(
5142                 Builder.getAllocator().CopyString(
5143                                                  Sel.getNameForSlot(I) + ":"));
5144        Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
5145                                         (*CurP)->getIdentifier()->getName()));
5146      } else {
5147        Builder.AddTypedTextChunk(
5148                  Builder.getAllocator().CopyString(
5149                                                  Sel.getNameForSlot(I) + ":"));
5150        Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
5151                                         (*CurP)->getIdentifier()->getName()));
5152      }
5153    }
5154  }
5155
5156  Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod,
5157                                         CCP_SuperCompletion));
5158  return SuperMethod;
5159}
5160
5161void Sema::CodeCompleteObjCMessageReceiver(Scope *S) {
5162  typedef CodeCompletionResult Result;
5163  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5164                        CodeCompleter->getCodeCompletionTUInfo(),
5165                        CodeCompletionContext::CCC_ObjCMessageReceiver,
5166                        getLangOpts().CPlusPlus0x
5167                          ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture
5168                          : &ResultBuilder::IsObjCMessageReceiver);
5169
5170  CodeCompletionDeclConsumer Consumer(Results, CurContext);
5171  Results.EnterNewScope();
5172  LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5173                     CodeCompleter->includeGlobals());
5174
5175  // If we are in an Objective-C method inside a class that has a superclass,
5176  // add "super" as an option.
5177  if (ObjCMethodDecl *Method = getCurMethodDecl())
5178    if (ObjCInterfaceDecl *Iface = Method->getClassInterface())
5179      if (Iface->getSuperClass()) {
5180        Results.AddResult(Result("super"));
5181
5182        AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, 0, 0, Results);
5183      }
5184
5185  if (getLangOpts().CPlusPlus0x)
5186    addThisCompletion(*this, Results);
5187
5188  Results.ExitScope();
5189
5190  if (CodeCompleter->includeMacros())
5191    AddMacroResults(PP, Results);
5192  HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
5193                            Results.data(), Results.size());
5194
5195}
5196
5197void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc,
5198                                        IdentifierInfo **SelIdents,
5199                                        unsigned NumSelIdents,
5200                                        bool AtArgumentExpression) {
5201  ObjCInterfaceDecl *CDecl = 0;
5202  if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5203    // Figure out which interface we're in.
5204    CDecl = CurMethod->getClassInterface();
5205    if (!CDecl)
5206      return;
5207
5208    // Find the superclass of this class.
5209    CDecl = CDecl->getSuperClass();
5210    if (!CDecl)
5211      return;
5212
5213    if (CurMethod->isInstanceMethod()) {
5214      // We are inside an instance method, which means that the message
5215      // send [super ...] is actually calling an instance method on the
5216      // current object.
5217      return CodeCompleteObjCInstanceMessage(S, 0,
5218                                             SelIdents, NumSelIdents,
5219                                             AtArgumentExpression,
5220                                             CDecl);
5221    }
5222
5223    // Fall through to send to the superclass in CDecl.
5224  } else {
5225    // "super" may be the name of a type or variable. Figure out which
5226    // it is.
5227    IdentifierInfo *Super = &Context.Idents.get("super");
5228    NamedDecl *ND = LookupSingleName(S, Super, SuperLoc,
5229                                     LookupOrdinaryName);
5230    if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) {
5231      // "super" names an interface. Use it.
5232    } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) {
5233      if (const ObjCObjectType *Iface
5234            = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>())
5235        CDecl = Iface->getInterface();
5236    } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) {
5237      // "super" names an unresolved type; we can't be more specific.
5238    } else {
5239      // Assume that "super" names some kind of value and parse that way.
5240      CXXScopeSpec SS;
5241      SourceLocation TemplateKWLoc;
5242      UnqualifiedId id;
5243      id.setIdentifier(Super, SuperLoc);
5244      ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id,
5245                                               false, false);
5246      return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(),
5247                                             SelIdents, NumSelIdents,
5248                                             AtArgumentExpression);
5249    }
5250
5251    // Fall through
5252  }
5253
5254  ParsedType Receiver;
5255  if (CDecl)
5256    Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl));
5257  return CodeCompleteObjCClassMessage(S, Receiver, SelIdents,
5258                                      NumSelIdents, AtArgumentExpression,
5259                                      /*IsSuper=*/true);
5260}
5261
5262/// \brief Given a set of code-completion results for the argument of a message
5263/// send, determine the preferred type (if any) for that argument expression.
5264static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results,
5265                                                       unsigned NumSelIdents) {
5266  typedef CodeCompletionResult Result;
5267  ASTContext &Context = Results.getSema().Context;
5268
5269  QualType PreferredType;
5270  unsigned BestPriority = CCP_Unlikely * 2;
5271  Result *ResultsData = Results.data();
5272  for (unsigned I = 0, N = Results.size(); I != N; ++I) {
5273    Result &R = ResultsData[I];
5274    if (R.Kind == Result::RK_Declaration &&
5275        isa<ObjCMethodDecl>(R.Declaration)) {
5276      if (R.Priority <= BestPriority) {
5277        ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration);
5278        if (NumSelIdents <= Method->param_size()) {
5279          QualType MyPreferredType = Method->param_begin()[NumSelIdents - 1]
5280                                       ->getType();
5281          if (R.Priority < BestPriority || PreferredType.isNull()) {
5282            BestPriority = R.Priority;
5283            PreferredType = MyPreferredType;
5284          } else if (!Context.hasSameUnqualifiedType(PreferredType,
5285                                                     MyPreferredType)) {
5286            PreferredType = QualType();
5287          }
5288        }
5289      }
5290    }
5291  }
5292
5293  return PreferredType;
5294}
5295
5296static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
5297                                       ParsedType Receiver,
5298                                       IdentifierInfo **SelIdents,
5299                                       unsigned NumSelIdents,
5300                                       bool AtArgumentExpression,
5301                                       bool IsSuper,
5302                                       ResultBuilder &Results) {
5303  typedef CodeCompletionResult Result;
5304  ObjCInterfaceDecl *CDecl = 0;
5305
5306  // If the given name refers to an interface type, retrieve the
5307  // corresponding declaration.
5308  if (Receiver) {
5309    QualType T = SemaRef.GetTypeFromParser(Receiver, 0);
5310    if (!T.isNull())
5311      if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>())
5312        CDecl = Interface->getInterface();
5313  }
5314
5315  // Add all of the factory methods in this Objective-C class, its protocols,
5316  // superclasses, categories, implementation, etc.
5317  Results.EnterNewScope();
5318
5319  // If this is a send-to-super, try to add the special "super" send
5320  // completion.
5321  if (IsSuper) {
5322    if (ObjCMethodDecl *SuperMethod
5323        = AddSuperSendCompletion(SemaRef, false, SelIdents, NumSelIdents,
5324                                 Results))
5325      Results.Ignore(SuperMethod);
5326  }
5327
5328  // If we're inside an Objective-C method definition, prefer its selector to
5329  // others.
5330  if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl())
5331    Results.setPreferredSelector(CurMethod->getSelector());
5332
5333  VisitedSelectorSet Selectors;
5334  if (CDecl)
5335    AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents,
5336                   SemaRef.CurContext, Selectors, AtArgumentExpression,
5337                   Results);
5338  else {
5339    // We're messaging "id" as a type; provide all class/factory methods.
5340
5341    // If we have an external source, load the entire class method
5342    // pool from the AST file.
5343    if (SemaRef.ExternalSource) {
5344      for (uint32_t I = 0,
5345                    N = SemaRef.ExternalSource->GetNumExternalSelectors();
5346           I != N; ++I) {
5347        Selector Sel = SemaRef.ExternalSource->GetExternalSelector(I);
5348        if (Sel.isNull() || SemaRef.MethodPool.count(Sel))
5349          continue;
5350
5351        SemaRef.ReadMethodPool(Sel);
5352      }
5353    }
5354
5355    for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(),
5356                                       MEnd = SemaRef.MethodPool.end();
5357         M != MEnd; ++M) {
5358      for (ObjCMethodList *MethList = &M->second.second;
5359           MethList && MethList->Method;
5360           MethList = MethList->Next) {
5361        if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
5362                                    NumSelIdents))
5363          continue;
5364
5365        Result R(MethList->Method, 0);
5366        R.StartParameter = NumSelIdents;
5367        R.AllParametersAreInformative = false;
5368        Results.MaybeAddResult(R, SemaRef.CurContext);
5369      }
5370    }
5371  }
5372
5373  Results.ExitScope();
5374}
5375
5376void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver,
5377                                        IdentifierInfo **SelIdents,
5378                                        unsigned NumSelIdents,
5379                                        bool AtArgumentExpression,
5380                                        bool IsSuper) {
5381
5382  QualType T = this->GetTypeFromParser(Receiver);
5383
5384  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5385                        CodeCompleter->getCodeCompletionTUInfo(),
5386              CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage,
5387                                    T, SelIdents, NumSelIdents));
5388
5389  AddClassMessageCompletions(*this, S, Receiver, SelIdents, NumSelIdents,
5390                             AtArgumentExpression, IsSuper, Results);
5391
5392  // If we're actually at the argument expression (rather than prior to the
5393  // selector), we're actually performing code completion for an expression.
5394  // Determine whether we have a single, best method. If so, we can
5395  // code-complete the expression using the corresponding parameter type as
5396  // our preferred type, improving completion results.
5397  if (AtArgumentExpression) {
5398    QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
5399                                                                  NumSelIdents);
5400    if (PreferredType.isNull())
5401      CodeCompleteOrdinaryName(S, PCC_Expression);
5402    else
5403      CodeCompleteExpression(S, PreferredType);
5404    return;
5405  }
5406
5407  HandleCodeCompleteResults(this, CodeCompleter,
5408                            Results.getCompletionContext(),
5409                            Results.data(), Results.size());
5410}
5411
5412void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver,
5413                                           IdentifierInfo **SelIdents,
5414                                           unsigned NumSelIdents,
5415                                           bool AtArgumentExpression,
5416                                           ObjCInterfaceDecl *Super) {
5417  typedef CodeCompletionResult Result;
5418
5419  Expr *RecExpr = static_cast<Expr *>(Receiver);
5420
5421  // If necessary, apply function/array conversion to the receiver.
5422  // C99 6.7.5.3p[7,8].
5423  if (RecExpr) {
5424    ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr);
5425    if (Conv.isInvalid()) // conversion failed. bail.
5426      return;
5427    RecExpr = Conv.take();
5428  }
5429  QualType ReceiverType = RecExpr? RecExpr->getType()
5430                          : Super? Context.getObjCObjectPointerType(
5431                                            Context.getObjCInterfaceType(Super))
5432                                 : Context.getObjCIdType();
5433
5434  // If we're messaging an expression with type "id" or "Class", check
5435  // whether we know something special about the receiver that allows
5436  // us to assume a more-specific receiver type.
5437  if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType())
5438    if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) {
5439      if (ReceiverType->isObjCClassType())
5440        return CodeCompleteObjCClassMessage(S,
5441                       ParsedType::make(Context.getObjCInterfaceType(IFace)),
5442                                            SelIdents, NumSelIdents,
5443                                            AtArgumentExpression, Super);
5444
5445      ReceiverType = Context.getObjCObjectPointerType(
5446                                          Context.getObjCInterfaceType(IFace));
5447    }
5448
5449  // Build the set of methods we can see.
5450  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5451                        CodeCompleter->getCodeCompletionTUInfo(),
5452           CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage,
5453                                 ReceiverType, SelIdents, NumSelIdents));
5454
5455  Results.EnterNewScope();
5456
5457  // If this is a send-to-super, try to add the special "super" send
5458  // completion.
5459  if (Super) {
5460    if (ObjCMethodDecl *SuperMethod
5461          = AddSuperSendCompletion(*this, false, SelIdents, NumSelIdents,
5462                                   Results))
5463      Results.Ignore(SuperMethod);
5464  }
5465
5466  // If we're inside an Objective-C method definition, prefer its selector to
5467  // others.
5468  if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
5469    Results.setPreferredSelector(CurMethod->getSelector());
5470
5471  // Keep track of the selectors we've already added.
5472  VisitedSelectorSet Selectors;
5473
5474  // Handle messages to Class. This really isn't a message to an instance
5475  // method, so we treat it the same way we would treat a message send to a
5476  // class method.
5477  if (ReceiverType->isObjCClassType() ||
5478      ReceiverType->isObjCQualifiedClassType()) {
5479    if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5480      if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
5481        AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents,
5482                       CurContext, Selectors, AtArgumentExpression, Results);
5483    }
5484  }
5485  // Handle messages to a qualified ID ("id<foo>").
5486  else if (const ObjCObjectPointerType *QualID
5487             = ReceiverType->getAsObjCQualifiedIdType()) {
5488    // Search protocols for instance methods.
5489    for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(),
5490                                              E = QualID->qual_end();
5491         I != E; ++I)
5492      AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
5493                     Selectors, AtArgumentExpression, Results);
5494  }
5495  // Handle messages to a pointer to interface type.
5496  else if (const ObjCObjectPointerType *IFacePtr
5497                              = ReceiverType->getAsObjCInterfacePointerType()) {
5498    // Search the class, its superclasses, etc., for instance methods.
5499    AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents,
5500                   NumSelIdents, CurContext, Selectors, AtArgumentExpression,
5501                   Results);
5502
5503    // Search protocols for instance methods.
5504    for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(),
5505         E = IFacePtr->qual_end();
5506         I != E; ++I)
5507      AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
5508                     Selectors, AtArgumentExpression, Results);
5509  }
5510  // Handle messages to "id".
5511  else if (ReceiverType->isObjCIdType()) {
5512    // We're messaging "id", so provide all instance methods we know
5513    // about as code-completion results.
5514
5515    // If we have an external source, load the entire class method
5516    // pool from the AST file.
5517    if (ExternalSource) {
5518      for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5519           I != N; ++I) {
5520        Selector Sel = ExternalSource->GetExternalSelector(I);
5521        if (Sel.isNull() || MethodPool.count(Sel))
5522          continue;
5523
5524        ReadMethodPool(Sel);
5525      }
5526    }
5527
5528    for (GlobalMethodPool::iterator M = MethodPool.begin(),
5529                                    MEnd = MethodPool.end();
5530         M != MEnd; ++M) {
5531      for (ObjCMethodList *MethList = &M->second.first;
5532           MethList && MethList->Method;
5533           MethList = MethList->Next) {
5534        if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
5535                                    NumSelIdents))
5536          continue;
5537
5538        if (!Selectors.insert(MethList->Method->getSelector()))
5539          continue;
5540
5541        Result R(MethList->Method, 0);
5542        R.StartParameter = NumSelIdents;
5543        R.AllParametersAreInformative = false;
5544        Results.MaybeAddResult(R, CurContext);
5545      }
5546    }
5547  }
5548  Results.ExitScope();
5549
5550
5551  // If we're actually at the argument expression (rather than prior to the
5552  // selector), we're actually performing code completion for an expression.
5553  // Determine whether we have a single, best method. If so, we can
5554  // code-complete the expression using the corresponding parameter type as
5555  // our preferred type, improving completion results.
5556  if (AtArgumentExpression) {
5557    QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
5558                                                                  NumSelIdents);
5559    if (PreferredType.isNull())
5560      CodeCompleteOrdinaryName(S, PCC_Expression);
5561    else
5562      CodeCompleteExpression(S, PreferredType);
5563    return;
5564  }
5565
5566  HandleCodeCompleteResults(this, CodeCompleter,
5567                            Results.getCompletionContext(),
5568                            Results.data(),Results.size());
5569}
5570
5571void Sema::CodeCompleteObjCForCollection(Scope *S,
5572                                         DeclGroupPtrTy IterationVar) {
5573  CodeCompleteExpressionData Data;
5574  Data.ObjCCollection = true;
5575
5576  if (IterationVar.getAsOpaquePtr()) {
5577    DeclGroupRef DG = IterationVar.getAsVal<DeclGroupRef>();
5578    for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) {
5579      if (*I)
5580        Data.IgnoreDecls.push_back(*I);
5581    }
5582  }
5583
5584  CodeCompleteExpression(S, Data);
5585}
5586
5587void Sema::CodeCompleteObjCSelector(Scope *S, IdentifierInfo **SelIdents,
5588                                    unsigned NumSelIdents) {
5589  // If we have an external source, load the entire class method
5590  // pool from the AST file.
5591  if (ExternalSource) {
5592    for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5593         I != N; ++I) {
5594      Selector Sel = ExternalSource->GetExternalSelector(I);
5595      if (Sel.isNull() || MethodPool.count(Sel))
5596        continue;
5597
5598      ReadMethodPool(Sel);
5599    }
5600  }
5601
5602  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5603                        CodeCompleter->getCodeCompletionTUInfo(),
5604                        CodeCompletionContext::CCC_SelectorName);
5605  Results.EnterNewScope();
5606  for (GlobalMethodPool::iterator M = MethodPool.begin(),
5607                               MEnd = MethodPool.end();
5608       M != MEnd; ++M) {
5609
5610    Selector Sel = M->first;
5611    if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents, NumSelIdents))
5612      continue;
5613
5614    CodeCompletionBuilder Builder(Results.getAllocator(),
5615                                  Results.getCodeCompletionTUInfo());
5616    if (Sel.isUnarySelector()) {
5617      Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
5618                                                       Sel.getNameForSlot(0)));
5619      Results.AddResult(Builder.TakeString());
5620      continue;
5621    }
5622
5623    std::string Accumulator;
5624    for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) {
5625      if (I == NumSelIdents) {
5626        if (!Accumulator.empty()) {
5627          Builder.AddInformativeChunk(Builder.getAllocator().CopyString(
5628                                                 Accumulator));
5629          Accumulator.clear();
5630        }
5631      }
5632
5633      Accumulator += Sel.getNameForSlot(I);
5634      Accumulator += ':';
5635    }
5636    Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator));
5637    Results.AddResult(Builder.TakeString());
5638  }
5639  Results.ExitScope();
5640
5641  HandleCodeCompleteResults(this, CodeCompleter,
5642                            CodeCompletionContext::CCC_SelectorName,
5643                            Results.data(), Results.size());
5644}
5645
5646/// \brief Add all of the protocol declarations that we find in the given
5647/// (translation unit) context.
5648static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext,
5649                               bool OnlyForwardDeclarations,
5650                               ResultBuilder &Results) {
5651  typedef CodeCompletionResult Result;
5652
5653  for (DeclContext::decl_iterator D = Ctx->decls_begin(),
5654                               DEnd = Ctx->decls_end();
5655       D != DEnd; ++D) {
5656    // Record any protocols we find.
5657    if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D))
5658      if (!OnlyForwardDeclarations || !Proto->hasDefinition())
5659        Results.AddResult(Result(Proto, 0), CurContext, 0, false);
5660  }
5661}
5662
5663void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
5664                                              unsigned NumProtocols) {
5665  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5666                        CodeCompleter->getCodeCompletionTUInfo(),
5667                        CodeCompletionContext::CCC_ObjCProtocolName);
5668
5669  if (CodeCompleter && CodeCompleter->includeGlobals()) {
5670    Results.EnterNewScope();
5671
5672    // Tell the result set to ignore all of the protocols we have
5673    // already seen.
5674    // FIXME: This doesn't work when caching code-completion results.
5675    for (unsigned I = 0; I != NumProtocols; ++I)
5676      if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first,
5677                                                      Protocols[I].second))
5678        Results.Ignore(Protocol);
5679
5680    // Add all protocols.
5681    AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false,
5682                       Results);
5683
5684    Results.ExitScope();
5685  }
5686
5687  HandleCodeCompleteResults(this, CodeCompleter,
5688                            CodeCompletionContext::CCC_ObjCProtocolName,
5689                            Results.data(),Results.size());
5690}
5691
5692void Sema::CodeCompleteObjCProtocolDecl(Scope *) {
5693  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5694                        CodeCompleter->getCodeCompletionTUInfo(),
5695                        CodeCompletionContext::CCC_ObjCProtocolName);
5696
5697  if (CodeCompleter && CodeCompleter->includeGlobals()) {
5698    Results.EnterNewScope();
5699
5700    // Add all protocols.
5701    AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true,
5702                       Results);
5703
5704    Results.ExitScope();
5705  }
5706
5707  HandleCodeCompleteResults(this, CodeCompleter,
5708                            CodeCompletionContext::CCC_ObjCProtocolName,
5709                            Results.data(),Results.size());
5710}
5711
5712/// \brief Add all of the Objective-C interface declarations that we find in
5713/// the given (translation unit) context.
5714static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext,
5715                                bool OnlyForwardDeclarations,
5716                                bool OnlyUnimplemented,
5717                                ResultBuilder &Results) {
5718  typedef CodeCompletionResult Result;
5719
5720  for (DeclContext::decl_iterator D = Ctx->decls_begin(),
5721                               DEnd = Ctx->decls_end();
5722       D != DEnd; ++D) {
5723    // Record any interfaces we find.
5724    if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D))
5725      if ((!OnlyForwardDeclarations || !Class->hasDefinition()) &&
5726          (!OnlyUnimplemented || !Class->getImplementation()))
5727        Results.AddResult(Result(Class, 0), CurContext, 0, false);
5728  }
5729}
5730
5731void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) {
5732  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5733                        CodeCompleter->getCodeCompletionTUInfo(),
5734                        CodeCompletionContext::CCC_Other);
5735  Results.EnterNewScope();
5736
5737  if (CodeCompleter->includeGlobals()) {
5738    // Add all classes.
5739    AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5740                        false, Results);
5741  }
5742
5743  Results.ExitScope();
5744
5745  HandleCodeCompleteResults(this, CodeCompleter,
5746                            CodeCompletionContext::CCC_ObjCInterfaceName,
5747                            Results.data(),Results.size());
5748}
5749
5750void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName,
5751                                      SourceLocation ClassNameLoc) {
5752  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5753                        CodeCompleter->getCodeCompletionTUInfo(),
5754                        CodeCompletionContext::CCC_ObjCInterfaceName);
5755  Results.EnterNewScope();
5756
5757  // Make sure that we ignore the class we're currently defining.
5758  NamedDecl *CurClass
5759    = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
5760  if (CurClass && isa<ObjCInterfaceDecl>(CurClass))
5761    Results.Ignore(CurClass);
5762
5763  if (CodeCompleter->includeGlobals()) {
5764    // Add all classes.
5765    AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5766                        false, Results);
5767  }
5768
5769  Results.ExitScope();
5770
5771  HandleCodeCompleteResults(this, CodeCompleter,
5772                            CodeCompletionContext::CCC_ObjCInterfaceName,
5773                            Results.data(),Results.size());
5774}
5775
5776void Sema::CodeCompleteObjCImplementationDecl(Scope *S) {
5777  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5778                        CodeCompleter->getCodeCompletionTUInfo(),
5779                        CodeCompletionContext::CCC_Other);
5780  Results.EnterNewScope();
5781
5782  if (CodeCompleter->includeGlobals()) {
5783    // Add all unimplemented classes.
5784    AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5785                        true, Results);
5786  }
5787
5788  Results.ExitScope();
5789
5790  HandleCodeCompleteResults(this, CodeCompleter,
5791                            CodeCompletionContext::CCC_ObjCInterfaceName,
5792                            Results.data(),Results.size());
5793}
5794
5795void Sema::CodeCompleteObjCInterfaceCategory(Scope *S,
5796                                             IdentifierInfo *ClassName,
5797                                             SourceLocation ClassNameLoc) {
5798  typedef CodeCompletionResult Result;
5799
5800  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5801                        CodeCompleter->getCodeCompletionTUInfo(),
5802                        CodeCompletionContext::CCC_ObjCCategoryName);
5803
5804  // Ignore any categories we find that have already been implemented by this
5805  // interface.
5806  llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
5807  NamedDecl *CurClass
5808    = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
5809  if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass))
5810    for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
5811         Category = Category->getNextClassCategory())
5812      CategoryNames.insert(Category->getIdentifier());
5813
5814  // Add all of the categories we know about.
5815  Results.EnterNewScope();
5816  TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5817  for (DeclContext::decl_iterator D = TU->decls_begin(),
5818                               DEnd = TU->decls_end();
5819       D != DEnd; ++D)
5820    if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D))
5821      if (CategoryNames.insert(Category->getIdentifier()))
5822        Results.AddResult(Result(Category, 0), CurContext, 0, false);
5823  Results.ExitScope();
5824
5825  HandleCodeCompleteResults(this, CodeCompleter,
5826                            CodeCompletionContext::CCC_ObjCCategoryName,
5827                            Results.data(),Results.size());
5828}
5829
5830void Sema::CodeCompleteObjCImplementationCategory(Scope *S,
5831                                                  IdentifierInfo *ClassName,
5832                                                  SourceLocation ClassNameLoc) {
5833  typedef CodeCompletionResult Result;
5834
5835  // Find the corresponding interface. If we couldn't find the interface, the
5836  // program itself is ill-formed. However, we'll try to be helpful still by
5837  // providing the list of all of the categories we know about.
5838  NamedDecl *CurClass
5839    = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
5840  ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass);
5841  if (!Class)
5842    return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc);
5843
5844  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5845                        CodeCompleter->getCodeCompletionTUInfo(),
5846                        CodeCompletionContext::CCC_ObjCCategoryName);
5847
5848  // Add all of the categories that have have corresponding interface
5849  // declarations in this class and any of its superclasses, except for
5850  // already-implemented categories in the class itself.
5851  llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
5852  Results.EnterNewScope();
5853  bool IgnoreImplemented = true;
5854  while (Class) {
5855    for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
5856         Category = Category->getNextClassCategory())
5857      if ((!IgnoreImplemented || !Category->getImplementation()) &&
5858          CategoryNames.insert(Category->getIdentifier()))
5859        Results.AddResult(Result(Category, 0), CurContext, 0, false);
5860
5861    Class = Class->getSuperClass();
5862    IgnoreImplemented = false;
5863  }
5864  Results.ExitScope();
5865
5866  HandleCodeCompleteResults(this, CodeCompleter,
5867                            CodeCompletionContext::CCC_ObjCCategoryName,
5868                            Results.data(),Results.size());
5869}
5870
5871void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) {
5872  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5873                        CodeCompleter->getCodeCompletionTUInfo(),
5874                        CodeCompletionContext::CCC_Other);
5875
5876  // Figure out where this @synthesize lives.
5877  ObjCContainerDecl *Container
5878    = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
5879  if (!Container ||
5880      (!isa<ObjCImplementationDecl>(Container) &&
5881       !isa<ObjCCategoryImplDecl>(Container)))
5882    return;
5883
5884  // Ignore any properties that have already been implemented.
5885  Container = getContainerDef(Container);
5886  for (DeclContext::decl_iterator D = Container->decls_begin(),
5887                               DEnd = Container->decls_end();
5888       D != DEnd; ++D)
5889    if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D))
5890      Results.Ignore(PropertyImpl->getPropertyDecl());
5891
5892  // Add any properties that we find.
5893  AddedPropertiesSet AddedProperties;
5894  Results.EnterNewScope();
5895  if (ObjCImplementationDecl *ClassImpl
5896        = dyn_cast<ObjCImplementationDecl>(Container))
5897    AddObjCProperties(ClassImpl->getClassInterface(), false,
5898                      /*AllowNullaryMethods=*/false, CurContext,
5899                      AddedProperties, Results);
5900  else
5901    AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(),
5902                      false, /*AllowNullaryMethods=*/false, CurContext,
5903                      AddedProperties, Results);
5904  Results.ExitScope();
5905
5906  HandleCodeCompleteResults(this, CodeCompleter,
5907                            CodeCompletionContext::CCC_Other,
5908                            Results.data(),Results.size());
5909}
5910
5911void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
5912                                                  IdentifierInfo *PropertyName) {
5913  typedef CodeCompletionResult Result;
5914  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5915                        CodeCompleter->getCodeCompletionTUInfo(),
5916                        CodeCompletionContext::CCC_Other);
5917
5918  // Figure out where this @synthesize lives.
5919  ObjCContainerDecl *Container
5920    = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
5921  if (!Container ||
5922      (!isa<ObjCImplementationDecl>(Container) &&
5923       !isa<ObjCCategoryImplDecl>(Container)))
5924    return;
5925
5926  // Figure out which interface we're looking into.
5927  ObjCInterfaceDecl *Class = 0;
5928  if (ObjCImplementationDecl *ClassImpl
5929                                 = dyn_cast<ObjCImplementationDecl>(Container))
5930    Class = ClassImpl->getClassInterface();
5931  else
5932    Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
5933                                                          ->getClassInterface();
5934
5935  // Determine the type of the property we're synthesizing.
5936  QualType PropertyType = Context.getObjCIdType();
5937  if (Class) {
5938    if (ObjCPropertyDecl *Property
5939                              = Class->FindPropertyDeclaration(PropertyName)) {
5940      PropertyType
5941        = Property->getType().getNonReferenceType().getUnqualifiedType();
5942
5943      // Give preference to ivars
5944      Results.setPreferredType(PropertyType);
5945    }
5946  }
5947
5948  // Add all of the instance variables in this class and its superclasses.
5949  Results.EnterNewScope();
5950  bool SawSimilarlyNamedIvar = false;
5951  std::string NameWithPrefix;
5952  NameWithPrefix += '_';
5953  NameWithPrefix += PropertyName->getName();
5954  std::string NameWithSuffix = PropertyName->getName().str();
5955  NameWithSuffix += '_';
5956  for(; Class; Class = Class->getSuperClass()) {
5957    for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar;
5958         Ivar = Ivar->getNextIvar()) {
5959      Results.AddResult(Result(Ivar, 0), CurContext, 0, false);
5960
5961      // Determine whether we've seen an ivar with a name similar to the
5962      // property.
5963      if ((PropertyName == Ivar->getIdentifier() ||
5964           NameWithPrefix == Ivar->getName() ||
5965           NameWithSuffix == Ivar->getName())) {
5966        SawSimilarlyNamedIvar = true;
5967
5968        // Reduce the priority of this result by one, to give it a slight
5969        // advantage over other results whose names don't match so closely.
5970        if (Results.size() &&
5971            Results.data()[Results.size() - 1].Kind
5972                                      == CodeCompletionResult::RK_Declaration &&
5973            Results.data()[Results.size() - 1].Declaration == Ivar)
5974          Results.data()[Results.size() - 1].Priority--;
5975      }
5976    }
5977  }
5978
5979  if (!SawSimilarlyNamedIvar) {
5980    // Create ivar result _propName, that the user can use to synthesize
5981    // an ivar of the appropriate type.
5982    unsigned Priority = CCP_MemberDeclaration + 1;
5983    typedef CodeCompletionResult Result;
5984    CodeCompletionAllocator &Allocator = Results.getAllocator();
5985    CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(),
5986                                  Priority,CXAvailability_Available);
5987
5988    PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
5989    Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context,
5990                                                       Policy, Allocator));
5991    Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix));
5992    Results.AddResult(Result(Builder.TakeString(), Priority,
5993                             CXCursor_ObjCIvarDecl));
5994  }
5995
5996  Results.ExitScope();
5997
5998  HandleCodeCompleteResults(this, CodeCompleter,
5999                            CodeCompletionContext::CCC_Other,
6000                            Results.data(),Results.size());
6001}
6002
6003// Mapping from selectors to the methods that implement that selector, along
6004// with the "in original class" flag.
6005typedef llvm::DenseMap<Selector, std::pair<ObjCMethodDecl *, bool> >
6006  KnownMethodsMap;
6007
6008/// \brief Find all of the methods that reside in the given container
6009/// (and its superclasses, protocols, etc.) that meet the given
6010/// criteria. Insert those methods into the map of known methods,
6011/// indexed by selector so they can be easily found.
6012static void FindImplementableMethods(ASTContext &Context,
6013                                     ObjCContainerDecl *Container,
6014                                     bool WantInstanceMethods,
6015                                     QualType ReturnType,
6016                                     KnownMethodsMap &KnownMethods,
6017                                     bool InOriginalClass = true) {
6018  if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) {
6019    // Make sure we have a definition; that's what we'll walk.
6020    if (!IFace->hasDefinition())
6021      return;
6022
6023    IFace = IFace->getDefinition();
6024    Container = IFace;
6025
6026    const ObjCList<ObjCProtocolDecl> &Protocols
6027      = IFace->getReferencedProtocols();
6028    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6029                                              E = Protocols.end();
6030         I != E; ++I)
6031      FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6032                               KnownMethods, InOriginalClass);
6033
6034    // Add methods from any class extensions and categories.
6035    for (const ObjCCategoryDecl *Cat = IFace->getCategoryList(); Cat;
6036         Cat = Cat->getNextClassCategory())
6037      FindImplementableMethods(Context, const_cast<ObjCCategoryDecl*>(Cat),
6038                               WantInstanceMethods, ReturnType,
6039                               KnownMethods, false);
6040
6041    // Visit the superclass.
6042    if (IFace->getSuperClass())
6043      FindImplementableMethods(Context, IFace->getSuperClass(),
6044                               WantInstanceMethods, ReturnType,
6045                               KnownMethods, false);
6046  }
6047
6048  if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
6049    // Recurse into protocols.
6050    const ObjCList<ObjCProtocolDecl> &Protocols
6051      = Category->getReferencedProtocols();
6052    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6053                                              E = Protocols.end();
6054         I != E; ++I)
6055      FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6056                               KnownMethods, InOriginalClass);
6057
6058    // If this category is the original class, jump to the interface.
6059    if (InOriginalClass && Category->getClassInterface())
6060      FindImplementableMethods(Context, Category->getClassInterface(),
6061                               WantInstanceMethods, ReturnType, KnownMethods,
6062                               false);
6063  }
6064
6065  if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
6066    // Make sure we have a definition; that's what we'll walk.
6067    if (!Protocol->hasDefinition())
6068      return;
6069    Protocol = Protocol->getDefinition();
6070    Container = Protocol;
6071
6072    // Recurse into protocols.
6073    const ObjCList<ObjCProtocolDecl> &Protocols
6074      = Protocol->getReferencedProtocols();
6075    for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6076           E = Protocols.end();
6077         I != E; ++I)
6078      FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6079                               KnownMethods, false);
6080  }
6081
6082  // Add methods in this container. This operation occurs last because
6083  // we want the methods from this container to override any methods
6084  // we've previously seen with the same selector.
6085  for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
6086                                       MEnd = Container->meth_end();
6087       M != MEnd; ++M) {
6088    if (M->isInstanceMethod() == WantInstanceMethods) {
6089      if (!ReturnType.isNull() &&
6090          !Context.hasSameUnqualifiedType(ReturnType, M->getResultType()))
6091        continue;
6092
6093      KnownMethods[M->getSelector()] = std::make_pair(*M, InOriginalClass);
6094    }
6095  }
6096}
6097
6098/// \brief Add the parenthesized return or parameter type chunk to a code
6099/// completion string.
6100static void AddObjCPassingTypeChunk(QualType Type,
6101                                    unsigned ObjCDeclQuals,
6102                                    ASTContext &Context,
6103                                    const PrintingPolicy &Policy,
6104                                    CodeCompletionBuilder &Builder) {
6105  Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6106  std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals);
6107  if (!Quals.empty())
6108    Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals));
6109  Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy,
6110                                               Builder.getAllocator()));
6111  Builder.AddChunk(CodeCompletionString::CK_RightParen);
6112}
6113
6114/// \brief Determine whether the given class is or inherits from a class by
6115/// the given name.
6116static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class,
6117                                   StringRef Name) {
6118  if (!Class)
6119    return false;
6120
6121  if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name)
6122    return true;
6123
6124  return InheritsFromClassNamed(Class->getSuperClass(), Name);
6125}
6126
6127/// \brief Add code completions for Objective-C Key-Value Coding (KVC) and
6128/// Key-Value Observing (KVO).
6129static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property,
6130                                       bool IsInstanceMethod,
6131                                       QualType ReturnType,
6132                                       ASTContext &Context,
6133                                       VisitedSelectorSet &KnownSelectors,
6134                                       ResultBuilder &Results) {
6135  IdentifierInfo *PropName = Property->getIdentifier();
6136  if (!PropName || PropName->getLength() == 0)
6137    return;
6138
6139  PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
6140
6141  // Builder that will create each code completion.
6142  typedef CodeCompletionResult Result;
6143  CodeCompletionAllocator &Allocator = Results.getAllocator();
6144  CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
6145
6146  // The selector table.
6147  SelectorTable &Selectors = Context.Selectors;
6148
6149  // The property name, copied into the code completion allocation region
6150  // on demand.
6151  struct KeyHolder {
6152    CodeCompletionAllocator &Allocator;
6153    StringRef Key;
6154    const char *CopiedKey;
6155
6156    KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key)
6157    : Allocator(Allocator), Key(Key), CopiedKey(0) { }
6158
6159    operator const char *() {
6160      if (CopiedKey)
6161        return CopiedKey;
6162
6163      return CopiedKey = Allocator.CopyString(Key);
6164    }
6165  } Key(Allocator, PropName->getName());
6166
6167  // The uppercased name of the property name.
6168  std::string UpperKey = PropName->getName();
6169  if (!UpperKey.empty())
6170    UpperKey[0] = toupper(UpperKey[0]);
6171
6172  bool ReturnTypeMatchesProperty = ReturnType.isNull() ||
6173    Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(),
6174                                   Property->getType());
6175  bool ReturnTypeMatchesVoid
6176    = ReturnType.isNull() || ReturnType->isVoidType();
6177
6178  // Add the normal accessor -(type)key.
6179  if (IsInstanceMethod &&
6180      KnownSelectors.insert(Selectors.getNullarySelector(PropName)) &&
6181      ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) {
6182    if (ReturnType.isNull())
6183      AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6184                              Context, Policy, Builder);
6185
6186    Builder.AddTypedTextChunk(Key);
6187    Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6188                             CXCursor_ObjCInstanceMethodDecl));
6189  }
6190
6191  // If we have an integral or boolean property (or the user has provided
6192  // an integral or boolean return type), add the accessor -(type)isKey.
6193  if (IsInstanceMethod &&
6194      ((!ReturnType.isNull() &&
6195        (ReturnType->isIntegerType() || ReturnType->isBooleanType())) ||
6196       (ReturnType.isNull() &&
6197        (Property->getType()->isIntegerType() ||
6198         Property->getType()->isBooleanType())))) {
6199    std::string SelectorName = (Twine("is") + UpperKey).str();
6200    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6201    if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
6202      if (ReturnType.isNull()) {
6203        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6204        Builder.AddTextChunk("BOOL");
6205        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6206      }
6207
6208      Builder.AddTypedTextChunk(
6209                                Allocator.CopyString(SelectorId->getName()));
6210      Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6211                               CXCursor_ObjCInstanceMethodDecl));
6212    }
6213  }
6214
6215  // Add the normal mutator.
6216  if (IsInstanceMethod && ReturnTypeMatchesVoid &&
6217      !Property->getSetterMethodDecl()) {
6218    std::string SelectorName = (Twine("set") + UpperKey).str();
6219    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6220    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6221      if (ReturnType.isNull()) {
6222        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6223        Builder.AddTextChunk("void");
6224        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6225      }
6226
6227      Builder.AddTypedTextChunk(
6228                                Allocator.CopyString(SelectorId->getName()));
6229      Builder.AddTypedTextChunk(":");
6230      AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6231                              Context, Policy, Builder);
6232      Builder.AddTextChunk(Key);
6233      Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6234                               CXCursor_ObjCInstanceMethodDecl));
6235    }
6236  }
6237
6238  // Indexed and unordered accessors
6239  unsigned IndexedGetterPriority = CCP_CodePattern;
6240  unsigned IndexedSetterPriority = CCP_CodePattern;
6241  unsigned UnorderedGetterPriority = CCP_CodePattern;
6242  unsigned UnorderedSetterPriority = CCP_CodePattern;
6243  if (const ObjCObjectPointerType *ObjCPointer
6244                    = Property->getType()->getAs<ObjCObjectPointerType>()) {
6245    if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) {
6246      // If this interface type is not provably derived from a known
6247      // collection, penalize the corresponding completions.
6248      if (!InheritsFromClassNamed(IFace, "NSMutableArray")) {
6249        IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6250        if (!InheritsFromClassNamed(IFace, "NSArray"))
6251          IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6252      }
6253
6254      if (!InheritsFromClassNamed(IFace, "NSMutableSet")) {
6255        UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6256        if (!InheritsFromClassNamed(IFace, "NSSet"))
6257          UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6258      }
6259    }
6260  } else {
6261    IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6262    IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6263    UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6264    UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6265  }
6266
6267  // Add -(NSUInteger)countOf<key>
6268  if (IsInstanceMethod &&
6269      (ReturnType.isNull() || ReturnType->isIntegerType())) {
6270    std::string SelectorName = (Twine("countOf") + UpperKey).str();
6271    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6272    if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
6273      if (ReturnType.isNull()) {
6274        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6275        Builder.AddTextChunk("NSUInteger");
6276        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6277      }
6278
6279      Builder.AddTypedTextChunk(
6280                                Allocator.CopyString(SelectorId->getName()));
6281      Results.AddResult(Result(Builder.TakeString(),
6282                               std::min(IndexedGetterPriority,
6283                                        UnorderedGetterPriority),
6284                               CXCursor_ObjCInstanceMethodDecl));
6285    }
6286  }
6287
6288  // Indexed getters
6289  // Add -(id)objectInKeyAtIndex:(NSUInteger)index
6290  if (IsInstanceMethod &&
6291      (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
6292    std::string SelectorName
6293      = (Twine("objectIn") + UpperKey + "AtIndex").str();
6294    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6295    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6296      if (ReturnType.isNull()) {
6297        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6298        Builder.AddTextChunk("id");
6299        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6300      }
6301
6302      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6303      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6304      Builder.AddTextChunk("NSUInteger");
6305      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6306      Builder.AddTextChunk("index");
6307      Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6308                               CXCursor_ObjCInstanceMethodDecl));
6309    }
6310  }
6311
6312  // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes
6313  if (IsInstanceMethod &&
6314      (ReturnType.isNull() ||
6315       (ReturnType->isObjCObjectPointerType() &&
6316        ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6317        ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6318                                                ->getName() == "NSArray"))) {
6319    std::string SelectorName
6320      = (Twine(Property->getName()) + "AtIndexes").str();
6321    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6322    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6323      if (ReturnType.isNull()) {
6324        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6325        Builder.AddTextChunk("NSArray *");
6326        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6327      }
6328
6329      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6330      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6331      Builder.AddTextChunk("NSIndexSet *");
6332      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6333      Builder.AddTextChunk("indexes");
6334      Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6335                               CXCursor_ObjCInstanceMethodDecl));
6336    }
6337  }
6338
6339  // Add -(void)getKey:(type **)buffer range:(NSRange)inRange
6340  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6341    std::string SelectorName = (Twine("get") + UpperKey).str();
6342    IdentifierInfo *SelectorIds[2] = {
6343      &Context.Idents.get(SelectorName),
6344      &Context.Idents.get("range")
6345    };
6346
6347    if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
6348      if (ReturnType.isNull()) {
6349        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6350        Builder.AddTextChunk("void");
6351        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6352      }
6353
6354      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6355      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6356      Builder.AddPlaceholderChunk("object-type");
6357      Builder.AddTextChunk(" **");
6358      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6359      Builder.AddTextChunk("buffer");
6360      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6361      Builder.AddTypedTextChunk("range:");
6362      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6363      Builder.AddTextChunk("NSRange");
6364      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6365      Builder.AddTextChunk("inRange");
6366      Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6367                               CXCursor_ObjCInstanceMethodDecl));
6368    }
6369  }
6370
6371  // Mutable indexed accessors
6372
6373  // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index
6374  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6375    std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str();
6376    IdentifierInfo *SelectorIds[2] = {
6377      &Context.Idents.get("insertObject"),
6378      &Context.Idents.get(SelectorName)
6379    };
6380
6381    if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
6382      if (ReturnType.isNull()) {
6383        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6384        Builder.AddTextChunk("void");
6385        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6386      }
6387
6388      Builder.AddTypedTextChunk("insertObject:");
6389      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6390      Builder.AddPlaceholderChunk("object-type");
6391      Builder.AddTextChunk(" *");
6392      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6393      Builder.AddTextChunk("object");
6394      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6395      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6396      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6397      Builder.AddPlaceholderChunk("NSUInteger");
6398      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6399      Builder.AddTextChunk("index");
6400      Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6401                               CXCursor_ObjCInstanceMethodDecl));
6402    }
6403  }
6404
6405  // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes
6406  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6407    std::string SelectorName = (Twine("insert") + UpperKey).str();
6408    IdentifierInfo *SelectorIds[2] = {
6409      &Context.Idents.get(SelectorName),
6410      &Context.Idents.get("atIndexes")
6411    };
6412
6413    if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
6414      if (ReturnType.isNull()) {
6415        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6416        Builder.AddTextChunk("void");
6417        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6418      }
6419
6420      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6421      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6422      Builder.AddTextChunk("NSArray *");
6423      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6424      Builder.AddTextChunk("array");
6425      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6426      Builder.AddTypedTextChunk("atIndexes:");
6427      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6428      Builder.AddPlaceholderChunk("NSIndexSet *");
6429      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6430      Builder.AddTextChunk("indexes");
6431      Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6432                               CXCursor_ObjCInstanceMethodDecl));
6433    }
6434  }
6435
6436  // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index
6437  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6438    std::string SelectorName
6439      = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str();
6440    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6441    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6442      if (ReturnType.isNull()) {
6443        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6444        Builder.AddTextChunk("void");
6445        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6446      }
6447
6448      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6449      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6450      Builder.AddTextChunk("NSUInteger");
6451      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6452      Builder.AddTextChunk("index");
6453      Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6454                               CXCursor_ObjCInstanceMethodDecl));
6455    }
6456  }
6457
6458  // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes
6459  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6460    std::string SelectorName
6461      = (Twine("remove") + UpperKey + "AtIndexes").str();
6462    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6463    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6464      if (ReturnType.isNull()) {
6465        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6466        Builder.AddTextChunk("void");
6467        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6468      }
6469
6470      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6471      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6472      Builder.AddTextChunk("NSIndexSet *");
6473      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6474      Builder.AddTextChunk("indexes");
6475      Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6476                               CXCursor_ObjCInstanceMethodDecl));
6477    }
6478  }
6479
6480  // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object
6481  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6482    std::string SelectorName
6483      = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str();
6484    IdentifierInfo *SelectorIds[2] = {
6485      &Context.Idents.get(SelectorName),
6486      &Context.Idents.get("withObject")
6487    };
6488
6489    if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
6490      if (ReturnType.isNull()) {
6491        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6492        Builder.AddTextChunk("void");
6493        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6494      }
6495
6496      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6497      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6498      Builder.AddPlaceholderChunk("NSUInteger");
6499      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6500      Builder.AddTextChunk("index");
6501      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6502      Builder.AddTypedTextChunk("withObject:");
6503      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6504      Builder.AddTextChunk("id");
6505      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6506      Builder.AddTextChunk("object");
6507      Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6508                               CXCursor_ObjCInstanceMethodDecl));
6509    }
6510  }
6511
6512  // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array
6513  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6514    std::string SelectorName1
6515      = (Twine("replace") + UpperKey + "AtIndexes").str();
6516    std::string SelectorName2 = (Twine("with") + UpperKey).str();
6517    IdentifierInfo *SelectorIds[2] = {
6518      &Context.Idents.get(SelectorName1),
6519      &Context.Idents.get(SelectorName2)
6520    };
6521
6522    if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds))) {
6523      if (ReturnType.isNull()) {
6524        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6525        Builder.AddTextChunk("void");
6526        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6527      }
6528
6529      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":"));
6530      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6531      Builder.AddPlaceholderChunk("NSIndexSet *");
6532      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6533      Builder.AddTextChunk("indexes");
6534      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6535      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":"));
6536      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6537      Builder.AddTextChunk("NSArray *");
6538      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6539      Builder.AddTextChunk("array");
6540      Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6541                               CXCursor_ObjCInstanceMethodDecl));
6542    }
6543  }
6544
6545  // Unordered getters
6546  // - (NSEnumerator *)enumeratorOfKey
6547  if (IsInstanceMethod &&
6548      (ReturnType.isNull() ||
6549       (ReturnType->isObjCObjectPointerType() &&
6550        ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6551        ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6552          ->getName() == "NSEnumerator"))) {
6553    std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str();
6554    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6555    if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
6556      if (ReturnType.isNull()) {
6557        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6558        Builder.AddTextChunk("NSEnumerator *");
6559        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6560      }
6561
6562      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6563      Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6564                              CXCursor_ObjCInstanceMethodDecl));
6565    }
6566  }
6567
6568  // - (type *)memberOfKey:(type *)object
6569  if (IsInstanceMethod &&
6570      (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
6571    std::string SelectorName = (Twine("memberOf") + UpperKey).str();
6572    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6573    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6574      if (ReturnType.isNull()) {
6575        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6576        Builder.AddPlaceholderChunk("object-type");
6577        Builder.AddTextChunk(" *");
6578        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6579      }
6580
6581      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6582      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6583      if (ReturnType.isNull()) {
6584        Builder.AddPlaceholderChunk("object-type");
6585        Builder.AddTextChunk(" *");
6586      } else {
6587        Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context,
6588                                                     Policy,
6589                                                     Builder.getAllocator()));
6590      }
6591      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6592      Builder.AddTextChunk("object");
6593      Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6594                               CXCursor_ObjCInstanceMethodDecl));
6595    }
6596  }
6597
6598  // Mutable unordered accessors
6599  // - (void)addKeyObject:(type *)object
6600  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6601    std::string SelectorName
6602      = (Twine("add") + UpperKey + Twine("Object")).str();
6603    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6604    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6605      if (ReturnType.isNull()) {
6606        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6607        Builder.AddTextChunk("void");
6608        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6609      }
6610
6611      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6612      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6613      Builder.AddPlaceholderChunk("object-type");
6614      Builder.AddTextChunk(" *");
6615      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6616      Builder.AddTextChunk("object");
6617      Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6618                               CXCursor_ObjCInstanceMethodDecl));
6619    }
6620  }
6621
6622  // - (void)addKey:(NSSet *)objects
6623  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6624    std::string SelectorName = (Twine("add") + UpperKey).str();
6625    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6626    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6627      if (ReturnType.isNull()) {
6628        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6629        Builder.AddTextChunk("void");
6630        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6631      }
6632
6633      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6634      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6635      Builder.AddTextChunk("NSSet *");
6636      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6637      Builder.AddTextChunk("objects");
6638      Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6639                               CXCursor_ObjCInstanceMethodDecl));
6640    }
6641  }
6642
6643  // - (void)removeKeyObject:(type *)object
6644  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6645    std::string SelectorName
6646      = (Twine("remove") + UpperKey + Twine("Object")).str();
6647    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6648    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6649      if (ReturnType.isNull()) {
6650        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6651        Builder.AddTextChunk("void");
6652        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6653      }
6654
6655      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6656      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6657      Builder.AddPlaceholderChunk("object-type");
6658      Builder.AddTextChunk(" *");
6659      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6660      Builder.AddTextChunk("object");
6661      Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6662                               CXCursor_ObjCInstanceMethodDecl));
6663    }
6664  }
6665
6666  // - (void)removeKey:(NSSet *)objects
6667  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6668    std::string SelectorName = (Twine("remove") + UpperKey).str();
6669    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6670    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6671      if (ReturnType.isNull()) {
6672        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6673        Builder.AddTextChunk("void");
6674        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6675      }
6676
6677      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6678      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6679      Builder.AddTextChunk("NSSet *");
6680      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6681      Builder.AddTextChunk("objects");
6682      Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6683                               CXCursor_ObjCInstanceMethodDecl));
6684    }
6685  }
6686
6687  // - (void)intersectKey:(NSSet *)objects
6688  if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6689    std::string SelectorName = (Twine("intersect") + UpperKey).str();
6690    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6691    if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId))) {
6692      if (ReturnType.isNull()) {
6693        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6694        Builder.AddTextChunk("void");
6695        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6696      }
6697
6698      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6699      Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6700      Builder.AddTextChunk("NSSet *");
6701      Builder.AddChunk(CodeCompletionString::CK_RightParen);
6702      Builder.AddTextChunk("objects");
6703      Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6704                               CXCursor_ObjCInstanceMethodDecl));
6705    }
6706  }
6707
6708  // Key-Value Observing
6709  // + (NSSet *)keyPathsForValuesAffectingKey
6710  if (!IsInstanceMethod &&
6711      (ReturnType.isNull() ||
6712       (ReturnType->isObjCObjectPointerType() &&
6713        ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6714        ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6715                                                    ->getName() == "NSSet"))) {
6716    std::string SelectorName
6717      = (Twine("keyPathsForValuesAffecting") + UpperKey).str();
6718    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6719    if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
6720      if (ReturnType.isNull()) {
6721        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6722        Builder.AddTextChunk("NSSet *");
6723        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6724      }
6725
6726      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6727      Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6728                              CXCursor_ObjCClassMethodDecl));
6729    }
6730  }
6731
6732  // + (BOOL)automaticallyNotifiesObserversForKey
6733  if (!IsInstanceMethod &&
6734      (ReturnType.isNull() ||
6735       ReturnType->isIntegerType() ||
6736       ReturnType->isBooleanType())) {
6737    std::string SelectorName
6738      = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str();
6739    IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6740    if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))) {
6741      if (ReturnType.isNull()) {
6742        Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6743        Builder.AddTextChunk("BOOL");
6744        Builder.AddChunk(CodeCompletionString::CK_RightParen);
6745      }
6746
6747      Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6748      Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6749                              CXCursor_ObjCClassMethodDecl));
6750    }
6751  }
6752}
6753
6754void Sema::CodeCompleteObjCMethodDecl(Scope *S,
6755                                      bool IsInstanceMethod,
6756                                      ParsedType ReturnTy) {
6757  // Determine the return type of the method we're declaring, if
6758  // provided.
6759  QualType ReturnType = GetTypeFromParser(ReturnTy);
6760  Decl *IDecl = 0;
6761  if (CurContext->isObjCContainer()) {
6762      ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
6763      IDecl = cast<Decl>(OCD);
6764  }
6765  // Determine where we should start searching for methods.
6766  ObjCContainerDecl *SearchDecl = 0;
6767  bool IsInImplementation = false;
6768  if (Decl *D = IDecl) {
6769    if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) {
6770      SearchDecl = Impl->getClassInterface();
6771      IsInImplementation = true;
6772    } else if (ObjCCategoryImplDecl *CatImpl
6773                                         = dyn_cast<ObjCCategoryImplDecl>(D)) {
6774      SearchDecl = CatImpl->getCategoryDecl();
6775      IsInImplementation = true;
6776    } else
6777      SearchDecl = dyn_cast<ObjCContainerDecl>(D);
6778  }
6779
6780  if (!SearchDecl && S) {
6781    if (DeclContext *DC = static_cast<DeclContext *>(S->getEntity()))
6782      SearchDecl = dyn_cast<ObjCContainerDecl>(DC);
6783  }
6784
6785  if (!SearchDecl) {
6786    HandleCodeCompleteResults(this, CodeCompleter,
6787                              CodeCompletionContext::CCC_Other,
6788                              0, 0);
6789    return;
6790  }
6791
6792  // Find all of the methods that we could declare/implement here.
6793  KnownMethodsMap KnownMethods;
6794  FindImplementableMethods(Context, SearchDecl, IsInstanceMethod,
6795                           ReturnType, KnownMethods);
6796
6797  // Add declarations or definitions for each of the known methods.
6798  typedef CodeCompletionResult Result;
6799  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
6800                        CodeCompleter->getCodeCompletionTUInfo(),
6801                        CodeCompletionContext::CCC_Other);
6802  Results.EnterNewScope();
6803  PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
6804  for (KnownMethodsMap::iterator M = KnownMethods.begin(),
6805                              MEnd = KnownMethods.end();
6806       M != MEnd; ++M) {
6807    ObjCMethodDecl *Method = M->second.first;
6808    CodeCompletionBuilder Builder(Results.getAllocator(),
6809                                  Results.getCodeCompletionTUInfo());
6810
6811    // If the result type was not already provided, add it to the
6812    // pattern as (type).
6813    if (ReturnType.isNull())
6814      AddObjCPassingTypeChunk(Method->getResultType(),
6815                              Method->getObjCDeclQualifier(),
6816                              Context, Policy,
6817                              Builder);
6818
6819    Selector Sel = Method->getSelector();
6820
6821    // Add the first part of the selector to the pattern.
6822    Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
6823                                                       Sel.getNameForSlot(0)));
6824
6825    // Add parameters to the pattern.
6826    unsigned I = 0;
6827    for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
6828                                     PEnd = Method->param_end();
6829         P != PEnd; (void)++P, ++I) {
6830      // Add the part of the selector name.
6831      if (I == 0)
6832        Builder.AddTypedTextChunk(":");
6833      else if (I < Sel.getNumArgs()) {
6834        Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6835        Builder.AddTypedTextChunk(
6836                Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":"));
6837      } else
6838        break;
6839
6840      // Add the parameter type.
6841      AddObjCPassingTypeChunk((*P)->getOriginalType(),
6842                              (*P)->getObjCDeclQualifier(),
6843                              Context, Policy,
6844                              Builder);
6845
6846      if (IdentifierInfo *Id = (*P)->getIdentifier())
6847        Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName()));
6848    }
6849
6850    if (Method->isVariadic()) {
6851      if (Method->param_size() > 0)
6852        Builder.AddChunk(CodeCompletionString::CK_Comma);
6853      Builder.AddTextChunk("...");
6854    }
6855
6856    if (IsInImplementation && Results.includeCodePatterns()) {
6857      // We will be defining the method here, so add a compound statement.
6858      Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6859      Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
6860      Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
6861      if (!Method->getResultType()->isVoidType()) {
6862        // If the result type is not void, add a return clause.
6863        Builder.AddTextChunk("return");
6864        Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6865        Builder.AddPlaceholderChunk("expression");
6866        Builder.AddChunk(CodeCompletionString::CK_SemiColon);
6867      } else
6868        Builder.AddPlaceholderChunk("statements");
6869
6870      Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
6871      Builder.AddChunk(CodeCompletionString::CK_RightBrace);
6872    }
6873
6874    unsigned Priority = CCP_CodePattern;
6875    if (!M->second.second)
6876      Priority += CCD_InBaseClass;
6877
6878    Results.AddResult(Result(Builder.TakeString(), Method, Priority));
6879  }
6880
6881  // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of
6882  // the properties in this class and its categories.
6883  if (Context.getLangOpts().ObjC2) {
6884    SmallVector<ObjCContainerDecl *, 4> Containers;
6885    Containers.push_back(SearchDecl);
6886
6887    VisitedSelectorSet KnownSelectors;
6888    for (KnownMethodsMap::iterator M = KnownMethods.begin(),
6889                                MEnd = KnownMethods.end();
6890         M != MEnd; ++M)
6891      KnownSelectors.insert(M->first);
6892
6893
6894    ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl);
6895    if (!IFace)
6896      if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl))
6897        IFace = Category->getClassInterface();
6898
6899    if (IFace) {
6900      for (ObjCCategoryDecl *Category = IFace->getCategoryList(); Category;
6901           Category = Category->getNextClassCategory())
6902        Containers.push_back(Category);
6903    }
6904
6905    for (unsigned I = 0, N = Containers.size(); I != N; ++I) {
6906      for (ObjCContainerDecl::prop_iterator P = Containers[I]->prop_begin(),
6907                                         PEnd = Containers[I]->prop_end();
6908           P != PEnd; ++P) {
6909        AddObjCKeyValueCompletions(*P, IsInstanceMethod, ReturnType, Context,
6910                                   KnownSelectors, Results);
6911      }
6912    }
6913  }
6914
6915  Results.ExitScope();
6916
6917  HandleCodeCompleteResults(this, CodeCompleter,
6918                            CodeCompletionContext::CCC_Other,
6919                            Results.data(),Results.size());
6920}
6921
6922void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S,
6923                                              bool IsInstanceMethod,
6924                                              bool AtParameterName,
6925                                              ParsedType ReturnTy,
6926                                              IdentifierInfo **SelIdents,
6927                                              unsigned NumSelIdents) {
6928  // If we have an external source, load the entire class method
6929  // pool from the AST file.
6930  if (ExternalSource) {
6931    for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
6932         I != N; ++I) {
6933      Selector Sel = ExternalSource->GetExternalSelector(I);
6934      if (Sel.isNull() || MethodPool.count(Sel))
6935        continue;
6936
6937      ReadMethodPool(Sel);
6938    }
6939  }
6940
6941  // Build the set of methods we can see.
6942  typedef CodeCompletionResult Result;
6943  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
6944                        CodeCompleter->getCodeCompletionTUInfo(),
6945                        CodeCompletionContext::CCC_Other);
6946
6947  if (ReturnTy)
6948    Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType());
6949
6950  Results.EnterNewScope();
6951  for (GlobalMethodPool::iterator M = MethodPool.begin(),
6952                                  MEnd = MethodPool.end();
6953       M != MEnd; ++M) {
6954    for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first :
6955                                                       &M->second.second;
6956         MethList && MethList->Method;
6957         MethList = MethList->Next) {
6958      if (!isAcceptableObjCMethod(MethList->Method, MK_Any, SelIdents,
6959                                  NumSelIdents))
6960        continue;
6961
6962      if (AtParameterName) {
6963        // Suggest parameter names we've seen before.
6964        if (NumSelIdents && NumSelIdents <= MethList->Method->param_size()) {
6965          ParmVarDecl *Param = MethList->Method->param_begin()[NumSelIdents-1];
6966          if (Param->getIdentifier()) {
6967            CodeCompletionBuilder Builder(Results.getAllocator(),
6968                                          Results.getCodeCompletionTUInfo());
6969            Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
6970                                           Param->getIdentifier()->getName()));
6971            Results.AddResult(Builder.TakeString());
6972          }
6973        }
6974
6975        continue;
6976      }
6977
6978      Result R(MethList->Method, 0);
6979      R.StartParameter = NumSelIdents;
6980      R.AllParametersAreInformative = false;
6981      R.DeclaringEntity = true;
6982      Results.MaybeAddResult(R, CurContext);
6983    }
6984  }
6985
6986  Results.ExitScope();
6987  HandleCodeCompleteResults(this, CodeCompleter,
6988                            CodeCompletionContext::CCC_Other,
6989                            Results.data(),Results.size());
6990}
6991
6992void Sema::CodeCompletePreprocessorDirective(bool InConditional) {
6993  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
6994                        CodeCompleter->getCodeCompletionTUInfo(),
6995                        CodeCompletionContext::CCC_PreprocessorDirective);
6996  Results.EnterNewScope();
6997
6998  // #if <condition>
6999  CodeCompletionBuilder Builder(Results.getAllocator(),
7000                                Results.getCodeCompletionTUInfo());
7001  Builder.AddTypedTextChunk("if");
7002  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7003  Builder.AddPlaceholderChunk("condition");
7004  Results.AddResult(Builder.TakeString());
7005
7006  // #ifdef <macro>
7007  Builder.AddTypedTextChunk("ifdef");
7008  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7009  Builder.AddPlaceholderChunk("macro");
7010  Results.AddResult(Builder.TakeString());
7011
7012  // #ifndef <macro>
7013  Builder.AddTypedTextChunk("ifndef");
7014  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7015  Builder.AddPlaceholderChunk("macro");
7016  Results.AddResult(Builder.TakeString());
7017
7018  if (InConditional) {
7019    // #elif <condition>
7020    Builder.AddTypedTextChunk("elif");
7021    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7022    Builder.AddPlaceholderChunk("condition");
7023    Results.AddResult(Builder.TakeString());
7024
7025    // #else
7026    Builder.AddTypedTextChunk("else");
7027    Results.AddResult(Builder.TakeString());
7028
7029    // #endif
7030    Builder.AddTypedTextChunk("endif");
7031    Results.AddResult(Builder.TakeString());
7032  }
7033
7034  // #include "header"
7035  Builder.AddTypedTextChunk("include");
7036  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7037  Builder.AddTextChunk("\"");
7038  Builder.AddPlaceholderChunk("header");
7039  Builder.AddTextChunk("\"");
7040  Results.AddResult(Builder.TakeString());
7041
7042  // #include <header>
7043  Builder.AddTypedTextChunk("include");
7044  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7045  Builder.AddTextChunk("<");
7046  Builder.AddPlaceholderChunk("header");
7047  Builder.AddTextChunk(">");
7048  Results.AddResult(Builder.TakeString());
7049
7050  // #define <macro>
7051  Builder.AddTypedTextChunk("define");
7052  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7053  Builder.AddPlaceholderChunk("macro");
7054  Results.AddResult(Builder.TakeString());
7055
7056  // #define <macro>(<args>)
7057  Builder.AddTypedTextChunk("define");
7058  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7059  Builder.AddPlaceholderChunk("macro");
7060  Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7061  Builder.AddPlaceholderChunk("args");
7062  Builder.AddChunk(CodeCompletionString::CK_RightParen);
7063  Results.AddResult(Builder.TakeString());
7064
7065  // #undef <macro>
7066  Builder.AddTypedTextChunk("undef");
7067  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7068  Builder.AddPlaceholderChunk("macro");
7069  Results.AddResult(Builder.TakeString());
7070
7071  // #line <number>
7072  Builder.AddTypedTextChunk("line");
7073  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7074  Builder.AddPlaceholderChunk("number");
7075  Results.AddResult(Builder.TakeString());
7076
7077  // #line <number> "filename"
7078  Builder.AddTypedTextChunk("line");
7079  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7080  Builder.AddPlaceholderChunk("number");
7081  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7082  Builder.AddTextChunk("\"");
7083  Builder.AddPlaceholderChunk("filename");
7084  Builder.AddTextChunk("\"");
7085  Results.AddResult(Builder.TakeString());
7086
7087  // #error <message>
7088  Builder.AddTypedTextChunk("error");
7089  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7090  Builder.AddPlaceholderChunk("message");
7091  Results.AddResult(Builder.TakeString());
7092
7093  // #pragma <arguments>
7094  Builder.AddTypedTextChunk("pragma");
7095  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7096  Builder.AddPlaceholderChunk("arguments");
7097  Results.AddResult(Builder.TakeString());
7098
7099  if (getLangOpts().ObjC1) {
7100    // #import "header"
7101    Builder.AddTypedTextChunk("import");
7102    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7103    Builder.AddTextChunk("\"");
7104    Builder.AddPlaceholderChunk("header");
7105    Builder.AddTextChunk("\"");
7106    Results.AddResult(Builder.TakeString());
7107
7108    // #import <header>
7109    Builder.AddTypedTextChunk("import");
7110    Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7111    Builder.AddTextChunk("<");
7112    Builder.AddPlaceholderChunk("header");
7113    Builder.AddTextChunk(">");
7114    Results.AddResult(Builder.TakeString());
7115  }
7116
7117  // #include_next "header"
7118  Builder.AddTypedTextChunk("include_next");
7119  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7120  Builder.AddTextChunk("\"");
7121  Builder.AddPlaceholderChunk("header");
7122  Builder.AddTextChunk("\"");
7123  Results.AddResult(Builder.TakeString());
7124
7125  // #include_next <header>
7126  Builder.AddTypedTextChunk("include_next");
7127  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7128  Builder.AddTextChunk("<");
7129  Builder.AddPlaceholderChunk("header");
7130  Builder.AddTextChunk(">");
7131  Results.AddResult(Builder.TakeString());
7132
7133  // #warning <message>
7134  Builder.AddTypedTextChunk("warning");
7135  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7136  Builder.AddPlaceholderChunk("message");
7137  Results.AddResult(Builder.TakeString());
7138
7139  // Note: #ident and #sccs are such crazy anachronisms that we don't provide
7140  // completions for them. And __include_macros is a Clang-internal extension
7141  // that we don't want to encourage anyone to use.
7142
7143  // FIXME: we don't support #assert or #unassert, so don't suggest them.
7144  Results.ExitScope();
7145
7146  HandleCodeCompleteResults(this, CodeCompleter,
7147                            CodeCompletionContext::CCC_PreprocessorDirective,
7148                            Results.data(), Results.size());
7149}
7150
7151void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) {
7152  CodeCompleteOrdinaryName(S,
7153                           S->getFnParent()? Sema::PCC_RecoveryInFunction
7154                                           : Sema::PCC_Namespace);
7155}
7156
7157void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) {
7158  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
7159                        CodeCompleter->getCodeCompletionTUInfo(),
7160                        IsDefinition? CodeCompletionContext::CCC_MacroName
7161                                    : CodeCompletionContext::CCC_MacroNameUse);
7162  if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) {
7163    // Add just the names of macros, not their arguments.
7164    CodeCompletionBuilder Builder(Results.getAllocator(),
7165                                  Results.getCodeCompletionTUInfo());
7166    Results.EnterNewScope();
7167    for (Preprocessor::macro_iterator M = PP.macro_begin(),
7168                                   MEnd = PP.macro_end();
7169         M != MEnd; ++M) {
7170      Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
7171                                           M->first->getName()));
7172      Results.AddResult(Builder.TakeString());
7173    }
7174    Results.ExitScope();
7175  } else if (IsDefinition) {
7176    // FIXME: Can we detect when the user just wrote an include guard above?
7177  }
7178
7179  HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
7180                            Results.data(), Results.size());
7181}
7182
7183void Sema::CodeCompletePreprocessorExpression() {
7184  ResultBuilder Results(*this, CodeCompleter->getAllocator(),
7185                        CodeCompleter->getCodeCompletionTUInfo(),
7186                        CodeCompletionContext::CCC_PreprocessorExpression);
7187
7188  if (!CodeCompleter || CodeCompleter->includeMacros())
7189    AddMacroResults(PP, Results);
7190
7191    // defined (<macro>)
7192  Results.EnterNewScope();
7193  CodeCompletionBuilder Builder(Results.getAllocator(),
7194                                Results.getCodeCompletionTUInfo());
7195  Builder.AddTypedTextChunk("defined");
7196  Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7197  Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7198  Builder.AddPlaceholderChunk("macro");
7199  Builder.AddChunk(CodeCompletionString::CK_RightParen);
7200  Results.AddResult(Builder.TakeString());
7201  Results.ExitScope();
7202
7203  HandleCodeCompleteResults(this, CodeCompleter,
7204                            CodeCompletionContext::CCC_PreprocessorExpression,
7205                            Results.data(), Results.size());
7206}
7207
7208void Sema::CodeCompletePreprocessorMacroArgument(Scope *S,
7209                                                 IdentifierInfo *Macro,
7210                                                 MacroInfo *MacroInfo,
7211                                                 unsigned Argument) {
7212  // FIXME: In the future, we could provide "overload" results, much like we
7213  // do for function calls.
7214
7215  // Now just ignore this. There will be another code-completion callback
7216  // for the expanded tokens.
7217}
7218
7219void Sema::CodeCompleteNaturalLanguage() {
7220  HandleCodeCompleteResults(this, CodeCompleter,
7221                            CodeCompletionContext::CCC_NaturalLanguage,
7222                            0, 0);
7223}
7224
7225void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator,
7226                                       CodeCompletionTUInfo &CCTUInfo,
7227                 SmallVectorImpl<CodeCompletionResult> &Results) {
7228  ResultBuilder Builder(*this, Allocator, CCTUInfo,
7229                        CodeCompletionContext::CCC_Recovery);
7230  if (!CodeCompleter || CodeCompleter->includeGlobals()) {
7231    CodeCompletionDeclConsumer Consumer(Builder,
7232                                        Context.getTranslationUnitDecl());
7233    LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName,
7234                       Consumer);
7235  }
7236
7237  if (!CodeCompleter || CodeCompleter->includeMacros())
7238    AddMacroResults(PP, Builder);
7239
7240  Results.clear();
7241  Results.insert(Results.end(),
7242                 Builder.data(), Builder.data() + Builder.size());
7243}
7244