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