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