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:
ShadowMapEntry()79       ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { }
80 
Add(const NamedDecl * ND,unsigned Index)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 
Destroy()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:
ResultBuilder(Sema & SemaRef,CodeCompletionAllocator & Allocator,CodeCompletionTUInfo & CCTUInfo,const CodeCompletionContext & CompletionContext,LookupFilter Filter=nullptr)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.
includeCodePatterns() const206     bool includeCodePatterns() const {
207       return SemaRef.CodeCompleter &&
208              SemaRef.CodeCompleter->includeCodePatterns();
209     }
210 
211     /// \brief Set the filter used for code-completion results.
setFilter(LookupFilter Filter)212     void setFilter(LookupFilter Filter) {
213       this->Filter = Filter;
214     }
215 
data()216     Result *data() { return Results.empty()? nullptr : &Results.front(); }
size() const217     unsigned size() const { return Results.size(); }
empty() const218     bool empty() const { return Results.empty(); }
219 
220     /// \brief Specify the preferred type.
setPreferredType(QualType T)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.
setObjectTypeQualifiers(Qualifiers Quals)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.
setPreferredSelector(Selector Sel)242     void setPreferredSelector(Selector Sel) {
243       PreferredSelector = Sel;
244     }
245 
246     /// \brief Retrieve the code-completion context for which results are
247     /// being collected.
getCompletionContext() const248     const CodeCompletionContext &getCompletionContext() const {
249       return CompletionContext;
250     }
251 
252     /// \brief Specify whether nested-name-specifiers are allowed.
allowNestedNameSpecifiers(bool Allow=true)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.
getSema() const259     Sema &getSema() const { return SemaRef; }
260 
261     /// \brief Retrieve the allocator used to allocate code completion strings.
getAllocator() const262     CodeCompletionAllocator &getAllocator() const { return Allocator; }
263 
getCodeCompletionTUInfo() const264     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 declaration (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.
Ignore(const Decl * D)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:
pointer(const DeclIndexPair & Value)361     pointer(const DeclIndexPair &Value) : Value(Value) { }
362 
operator ->() const363     const DeclIndexPair *operator->() const {
364       return &Value;
365     }
366   };
367 
iterator()368   iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {}
369 
iterator(const NamedDecl * SingleDecl,unsigned Index)370   iterator(const NamedDecl *SingleDecl, unsigned Index)
371     : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { }
372 
iterator(const DeclIndexPair * Iterator)373   iterator(const DeclIndexPair *Iterator)
374     : DeclOrIterator(Iterator), SingleDeclIndex(0) { }
375 
operator ++()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 
operator *() const395   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 
operator ->() const402   pointer operator->() const {
403     return pointer(**this);
404   }
405 
operator ==(const iterator & X,const iterator & Y)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 
operator !=(const iterator & X,const iterator & Y)412   friend bool operator!=(const iterator &X, const iterator &Y) {
413     return !(X == Y);
414   }
415 };
416 
417 ResultBuilder::ShadowMapEntry::iterator
begin() const418 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
end() const429 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 *
getRequiredQualification(ASTContext & Context,const DeclContext * CurContext,const DeclContext * TargetContext)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]).
isReservedName(const IdentifierInfo * Id)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 
isInterestingDecl(const NamedDecl * ND,bool & AsNestedNameSpecifier) const493 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 
CheckHiddenResult(Result & R,DeclContext * CurContext,const NamedDecl * Hiding)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.
getSimplifiedTypeClass(CanQualType T)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.
getDeclUsageType(ASTContext & C,const NamedDecl * ND)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 
getBasePriority(const NamedDecl * ND)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 
AdjustResultPriorityForDecl(Result & R)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 
MaybeAddConstructorResults(Result R)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 
MaybeAddResult(Result R,DeclContext * CurContext)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).second)
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 
AddResult(Result R,DeclContext * CurContext,NamedDecl * Hiding,bool InBaseClass=false)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()).second)
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 
AddResult(Result R)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.
EnterNewScope()1022 void ResultBuilder::EnterNewScope() {
1023   ShadowMaps.push_back(ShadowMap());
1024 }
1025 
1026 /// \brief Exit from the current scope.
ExitScope()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.
IsOrdinaryName(const NamedDecl * ND) const1039 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.
IsOrdinaryNonTypeName(const NamedDecl * ND) const1057 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 
IsIntegralConstantValue(const NamedDecl * ND) const1073 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.
IsOrdinaryNonValueName(const NamedDecl * ND) const1086 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.
IsNestedNameSpecifier(const NamedDecl * ND) const1100 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.
IsEnum(const NamedDecl * ND) const1109 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.
IsClassOrStruct(const NamedDecl * ND) const1114 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.
IsUnion(const NamedDecl * ND) const1129 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.
IsNamespace(const NamedDecl * ND) const1141 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.
IsNamespaceOrAlias(const NamedDecl * ND) const1147 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.
IsType(const NamedDecl * ND) const1152 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.
IsMember(const NamedDecl * ND) const1162 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 
isObjCReceiverType(ASTContext & C,QualType T)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 
IsObjCMessageReceiver(const NamedDecl * ND) const1203 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 
IsObjCMessageReceiverOrLambdaCapture(const NamedDecl * ND) const1212 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 
IsObjCCollection(const NamedDecl * ND) const1223 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 
IsImpossibleToSatisfy(const NamedDecl * ND) const1238 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.
IsObjCIvar(const NamedDecl * ND) const1244 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:
CodeCompletionDeclConsumer(ResultBuilder & Results,DeclContext * CurContext)1256     CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext)
1257       : Results(Results), CurContext(CurContext) { }
1258 
FoundDecl(NamedDecl * ND,NamedDecl * Hiding,DeclContext * Ctx,bool InBaseClass)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.
AddTypeSpecifierResults(const LangOptions & LangOpts,ResultBuilder & 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 
AddStorageSpecifiers(Sema::ParserCompletionContext CCC,const LangOptions & LangOpts,ResultBuilder & Results)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 
AddFunctionSpecifiers(Sema::ParserCompletionContext CCC,const LangOptions & LangOpts,ResultBuilder & Results)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 
AddTypedefResult(ResultBuilder & Results)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 
WantTypesInContext(Sema::ParserCompletionContext CCC,const LangOptions & LangOpts)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 
getCompletionPrintingPolicy(const ASTContext & Context,const Preprocessor & PP)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.
getCompletionPrintingPolicy(Sema & S)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.
GetCompletionTypeString(QualType T,ASTContext & Context,const PrintingPolicy & Policy,CodeCompletionAllocator & Allocator)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.
addThisCompletion(Sema & S,ResultBuilder & Results)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.
AddOrdinaryNameResults(Sema::ParserCompletionContext CCC,Scope * S,Sema & SemaRef,ResultBuilder & Results)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.
AddResultTypeChunk(ASTContext & Context,const PrintingPolicy & Policy,const NamedDecl * ND,CodeCompletionBuilder & Result)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 
MaybeAddSentinel(ASTContext & Context,const NamedDecl * FunctionOrMethod,CodeCompletionBuilder & Result)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 
formatObjCParamQualifiers(unsigned ObjCQuals)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 
FormatFunctionParameter(ASTContext & Context,const PrintingPolicy & Policy,const ParmVarDecl * Param,bool SuppressName=false,bool SuppressBlock=false)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.
AddFunctionParameterChunks(ASTContext & Context,const PrintingPolicy & Policy,const FunctionDecl * Function,CodeCompletionBuilder & Result,unsigned Start=0,bool InOptional=false)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.
AddTemplateParameterChunks(ASTContext & Context,const PrintingPolicy & Policy,const TemplateDecl * Template,CodeCompletionBuilder & Result,unsigned MaxParameters=0,unsigned Start=0,bool InDefaultArg=false)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
AddQualifierToCompletionString(CodeCompletionBuilder & Result,NestedNameSpecifier * Qualifier,bool QualifierIsInformative,ASTContext & Context,const PrintingPolicy & Policy)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
AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder & Result,const FunctionDecl * Function)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
AddTypedNameChunk(ASTContext & Context,const PrintingPolicy & Policy,const NamedDecl * ND,CodeCompletionBuilder & Result)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 
CreateCodeCompletionString(Sema & S,CodeCompletionAllocator & Allocator,CodeCompletionTUInfo & CCTUInfo,bool IncludeBriefComments)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 *
CreateCodeCompletionString(ASTContext & Ctx,Preprocessor & PP,CodeCompletionAllocator & Allocator,CodeCompletionTUInfo & CCTUInfo,bool IncludeBriefComments)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 *
CreateSignatureString(unsigned CurrentArg,Sema & S,CodeCompletionAllocator & Allocator,CodeCompletionTUInfo & CCTUInfo) const2816 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 
getMacroUsagePriority(StringRef MacroName,const LangOptions & LangOpts,bool PreferredTypeIsPointer)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 
getCursorKindForDecl(const Decl * D)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 
AddMacroResults(Preprocessor & PP,ResultBuilder & Results,bool IncludeUndefined,bool TargetTypeIsPointer=false)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 
AddPrettyFunctionResults(const LangOptions & LangOpts,ResultBuilder & Results)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 
HandleCodeCompleteResults(Sema * S,CodeCompleteConsumer * CodeCompleter,CodeCompletionContext Context,CodeCompletionResult * Results,unsigned NumResults)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 
mapCodeCompletionContext(Sema & S,Sema::ParserCompletionContext PCC)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
MaybeAddOverrideCalls(Sema & S,DeclContext * InContext,ResultBuilder & Results)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 
CodeCompleteModuleImport(SourceLocation ImportLoc,ModuleIdPath Path)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 
CodeCompleteOrdinaryName(Scope * S,ParserCompletionContext CompletionContext)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 
CodeCompleteDeclSpec(Scope * S,DeclSpec & DS,bool AllowNonIdentifiers,bool AllowNestedNameSpecifiers)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 {
CodeCompleteExpressionDataSema::CodeCompleteExpressionData3368   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.
CodeCompleteExpression(Scope * S,const CodeCompleteExpressionData & Data)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 
CodeCompletePostfixExpression(Scope * S,ExprResult E)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?
getContainerDef(ObjCContainerDecl * Container)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 
AddObjCProperties(ObjCContainerDecl * Container,bool AllowCategories,bool AllowNullaryMethods,DeclContext * CurContext,AddedPropertiesSet & AddedProperties,ResultBuilder & Results)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()).second)
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).second) {
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 
CodeCompleteMemberReferenceExpr(Scope * S,Expr * Base,SourceLocation OpLoc,bool IsArrow)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 
CodeCompleteTag(Scope * S,unsigned TagSpec)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 
CodeCompleteTypeQualifiers(DeclSpec & DS)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 
CodeCompleteCase(Scope * S)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 
anyNullArguments(ArrayRef<Expr * > Args)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 
CodeCompleteCall(Scope * S,Expr * FnIn,ArrayRef<Expr * > Args)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 
CodeCompleteInitializer(Scope * S,Decl * D)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 
CodeCompleteReturn(Scope * S)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 
CodeCompleteAfterIf(Scope * S)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 
CodeCompleteAssignmentRHS(Scope * S,Expr * LHS)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 
CodeCompleteQualifiedId(Scope * S,CXXScopeSpec & SS,bool EnteringContext)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 
CodeCompleteUsing(Scope * S)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 
CodeCompleteUsingDirective(Scope * S)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 
CodeCompleteNamespaceDecl(Scope * S)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 
CodeCompleteNamespaceAliasDecl(Scope * S)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 
CodeCompleteOperatorName(Scope * S)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 
CodeCompleteConstructorInitializer(Decl * ConstructorD,ArrayRef<CXXCtorInitializer * > Initializers)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              .second) {
4241       SawLastInitializer
4242         = !Initializers.empty() &&
4243           Initializers.back()->isBaseInitializer() &&
4244           Context.hasSameUnqualifiedType(Base.getType(),
4245                QualType(Initializers.back()->getBaseClass(), 0));
4246       continue;
4247     }
4248 
4249     Builder.AddTypedTextChunk(
4250                Results.getAllocator().CopyString(
4251                           Base.getType().getAsString(Policy)));
4252     Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4253     Builder.AddPlaceholderChunk("args");
4254     Builder.AddChunk(CodeCompletionString::CK_RightParen);
4255     Results.AddResult(CodeCompletionResult(Builder.TakeString(),
4256                                    SawLastInitializer? CCP_NextInitializer
4257                                                      : CCP_MemberDeclaration));
4258     SawLastInitializer = false;
4259   }
4260 
4261   // Add completions for virtual base classes.
4262   for (const auto &Base : ClassDecl->vbases()) {
4263     if (!InitializedBases.insert(Context.getCanonicalType(Base.getType()))
4264              .second) {
4265       SawLastInitializer
4266         = !Initializers.empty() &&
4267           Initializers.back()->isBaseInitializer() &&
4268           Context.hasSameUnqualifiedType(Base.getType(),
4269                QualType(Initializers.back()->getBaseClass(), 0));
4270       continue;
4271     }
4272 
4273     Builder.AddTypedTextChunk(
4274                Builder.getAllocator().CopyString(
4275                           Base.getType().getAsString(Policy)));
4276     Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4277     Builder.AddPlaceholderChunk("args");
4278     Builder.AddChunk(CodeCompletionString::CK_RightParen);
4279     Results.AddResult(CodeCompletionResult(Builder.TakeString(),
4280                                    SawLastInitializer? CCP_NextInitializer
4281                                                      : CCP_MemberDeclaration));
4282     SawLastInitializer = false;
4283   }
4284 
4285   // Add completions for members.
4286   for (auto *Field : ClassDecl->fields()) {
4287     if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))
4288              .second) {
4289       SawLastInitializer
4290         = !Initializers.empty() &&
4291           Initializers.back()->isAnyMemberInitializer() &&
4292           Initializers.back()->getAnyMember() == Field;
4293       continue;
4294     }
4295 
4296     if (!Field->getDeclName())
4297       continue;
4298 
4299     Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
4300                                          Field->getIdentifier()->getName()));
4301     Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4302     Builder.AddPlaceholderChunk("args");
4303     Builder.AddChunk(CodeCompletionString::CK_RightParen);
4304     Results.AddResult(CodeCompletionResult(Builder.TakeString(),
4305                                    SawLastInitializer? CCP_NextInitializer
4306                                                      : CCP_MemberDeclaration,
4307                                            CXCursor_MemberRef,
4308                                            CXAvailability_Available,
4309                                            Field));
4310     SawLastInitializer = false;
4311   }
4312   Results.ExitScope();
4313 
4314   HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4315                             Results.data(), Results.size());
4316 }
4317 
4318 /// \brief Determine whether this scope denotes a namespace.
isNamespaceScope(Scope * S)4319 static bool isNamespaceScope(Scope *S) {
4320   DeclContext *DC = S->getEntity();
4321   if (!DC)
4322     return false;
4323 
4324   return DC->isFileContext();
4325 }
4326 
CodeCompleteLambdaIntroducer(Scope * S,LambdaIntroducer & Intro,bool AfterAmpersand)4327 void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro,
4328                                         bool AfterAmpersand) {
4329   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4330                         CodeCompleter->getCodeCompletionTUInfo(),
4331                         CodeCompletionContext::CCC_Other);
4332   Results.EnterNewScope();
4333 
4334   // Note what has already been captured.
4335   llvm::SmallPtrSet<IdentifierInfo *, 4> Known;
4336   bool IncludedThis = false;
4337   for (const auto &C : Intro.Captures) {
4338     if (C.Kind == LCK_This) {
4339       IncludedThis = true;
4340       continue;
4341     }
4342 
4343     Known.insert(C.Id);
4344   }
4345 
4346   // Look for other capturable variables.
4347   for (; S && !isNamespaceScope(S); S = S->getParent()) {
4348     for (const auto *D : S->decls()) {
4349       const auto *Var = dyn_cast<VarDecl>(D);
4350       if (!Var ||
4351           !Var->hasLocalStorage() ||
4352           Var->hasAttr<BlocksAttr>())
4353         continue;
4354 
4355       if (Known.insert(Var->getIdentifier()).second)
4356         Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration),
4357                           CurContext, nullptr, false);
4358     }
4359   }
4360 
4361   // Add 'this', if it would be valid.
4362   if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy)
4363     addThisCompletion(*this, Results);
4364 
4365   Results.ExitScope();
4366 
4367   HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4368                             Results.data(), Results.size());
4369 }
4370 
4371 /// Macro that optionally prepends an "@" to the string literal passed in via
4372 /// Keyword, depending on whether NeedAt is true or false.
4373 #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword)
4374 
AddObjCImplementationResults(const LangOptions & LangOpts,ResultBuilder & Results,bool NeedAt)4375 static void AddObjCImplementationResults(const LangOptions &LangOpts,
4376                                          ResultBuilder &Results,
4377                                          bool NeedAt) {
4378   typedef CodeCompletionResult Result;
4379   // Since we have an implementation, we can end it.
4380   Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
4381 
4382   CodeCompletionBuilder Builder(Results.getAllocator(),
4383                                 Results.getCodeCompletionTUInfo());
4384   if (LangOpts.ObjC2) {
4385     // @dynamic
4386     Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic"));
4387     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4388     Builder.AddPlaceholderChunk("property");
4389     Results.AddResult(Result(Builder.TakeString()));
4390 
4391     // @synthesize
4392     Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize"));
4393     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4394     Builder.AddPlaceholderChunk("property");
4395     Results.AddResult(Result(Builder.TakeString()));
4396   }
4397 }
4398 
AddObjCInterfaceResults(const LangOptions & LangOpts,ResultBuilder & Results,bool NeedAt)4399 static void AddObjCInterfaceResults(const LangOptions &LangOpts,
4400                                     ResultBuilder &Results,
4401                                     bool NeedAt) {
4402   typedef CodeCompletionResult Result;
4403 
4404   // Since we have an interface or protocol, we can end it.
4405   Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
4406 
4407   if (LangOpts.ObjC2) {
4408     // @property
4409     Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property")));
4410 
4411     // @required
4412     Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required")));
4413 
4414     // @optional
4415     Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional")));
4416   }
4417 }
4418 
AddObjCTopLevelResults(ResultBuilder & Results,bool NeedAt)4419 static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) {
4420   typedef CodeCompletionResult Result;
4421   CodeCompletionBuilder Builder(Results.getAllocator(),
4422                                 Results.getCodeCompletionTUInfo());
4423 
4424   // @class name ;
4425   Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class"));
4426   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4427   Builder.AddPlaceholderChunk("name");
4428   Results.AddResult(Result(Builder.TakeString()));
4429 
4430   if (Results.includeCodePatterns()) {
4431     // @interface name
4432     // FIXME: Could introduce the whole pattern, including superclasses and
4433     // such.
4434     Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface"));
4435     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4436     Builder.AddPlaceholderChunk("class");
4437     Results.AddResult(Result(Builder.TakeString()));
4438 
4439     // @protocol name
4440     Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
4441     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4442     Builder.AddPlaceholderChunk("protocol");
4443     Results.AddResult(Result(Builder.TakeString()));
4444 
4445     // @implementation name
4446     Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation"));
4447     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4448     Builder.AddPlaceholderChunk("class");
4449     Results.AddResult(Result(Builder.TakeString()));
4450   }
4451 
4452   // @compatibility_alias name
4453   Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias"));
4454   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4455   Builder.AddPlaceholderChunk("alias");
4456   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4457   Builder.AddPlaceholderChunk("class");
4458   Results.AddResult(Result(Builder.TakeString()));
4459 
4460   if (Results.getSema().getLangOpts().Modules) {
4461     // @import name
4462     Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import"));
4463     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4464     Builder.AddPlaceholderChunk("module");
4465     Results.AddResult(Result(Builder.TakeString()));
4466   }
4467 }
4468 
CodeCompleteObjCAtDirective(Scope * S)4469 void Sema::CodeCompleteObjCAtDirective(Scope *S) {
4470   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4471                         CodeCompleter->getCodeCompletionTUInfo(),
4472                         CodeCompletionContext::CCC_Other);
4473   Results.EnterNewScope();
4474   if (isa<ObjCImplDecl>(CurContext))
4475     AddObjCImplementationResults(getLangOpts(), Results, false);
4476   else if (CurContext->isObjCContainer())
4477     AddObjCInterfaceResults(getLangOpts(), Results, false);
4478   else
4479     AddObjCTopLevelResults(Results, false);
4480   Results.ExitScope();
4481   HandleCodeCompleteResults(this, CodeCompleter,
4482                             CodeCompletionContext::CCC_Other,
4483                             Results.data(),Results.size());
4484 }
4485 
AddObjCExpressionResults(ResultBuilder & Results,bool NeedAt)4486 static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) {
4487   typedef CodeCompletionResult Result;
4488   CodeCompletionBuilder Builder(Results.getAllocator(),
4489                                 Results.getCodeCompletionTUInfo());
4490 
4491   // @encode ( type-name )
4492   const char *EncodeType = "char[]";
4493   if (Results.getSema().getLangOpts().CPlusPlus ||
4494       Results.getSema().getLangOpts().ConstStrings)
4495     EncodeType = "const char[]";
4496   Builder.AddResultTypeChunk(EncodeType);
4497   Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode"));
4498   Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4499   Builder.AddPlaceholderChunk("type-name");
4500   Builder.AddChunk(CodeCompletionString::CK_RightParen);
4501   Results.AddResult(Result(Builder.TakeString()));
4502 
4503   // @protocol ( protocol-name )
4504   Builder.AddResultTypeChunk("Protocol *");
4505   Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
4506   Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4507   Builder.AddPlaceholderChunk("protocol-name");
4508   Builder.AddChunk(CodeCompletionString::CK_RightParen);
4509   Results.AddResult(Result(Builder.TakeString()));
4510 
4511   // @selector ( selector )
4512   Builder.AddResultTypeChunk("SEL");
4513   Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector"));
4514   Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4515   Builder.AddPlaceholderChunk("selector");
4516   Builder.AddChunk(CodeCompletionString::CK_RightParen);
4517   Results.AddResult(Result(Builder.TakeString()));
4518 
4519   // @"string"
4520   Builder.AddResultTypeChunk("NSString *");
4521   Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\""));
4522   Builder.AddPlaceholderChunk("string");
4523   Builder.AddTextChunk("\"");
4524   Results.AddResult(Result(Builder.TakeString()));
4525 
4526   // @[objects, ...]
4527   Builder.AddResultTypeChunk("NSArray *");
4528   Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"["));
4529   Builder.AddPlaceholderChunk("objects, ...");
4530   Builder.AddChunk(CodeCompletionString::CK_RightBracket);
4531   Results.AddResult(Result(Builder.TakeString()));
4532 
4533   // @{key : object, ...}
4534   Builder.AddResultTypeChunk("NSDictionary *");
4535   Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{"));
4536   Builder.AddPlaceholderChunk("key");
4537   Builder.AddChunk(CodeCompletionString::CK_Colon);
4538   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4539   Builder.AddPlaceholderChunk("object, ...");
4540   Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4541   Results.AddResult(Result(Builder.TakeString()));
4542 
4543   // @(expression)
4544   Builder.AddResultTypeChunk("id");
4545   Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "("));
4546   Builder.AddPlaceholderChunk("expression");
4547   Builder.AddChunk(CodeCompletionString::CK_RightParen);
4548   Results.AddResult(Result(Builder.TakeString()));
4549 }
4550 
AddObjCStatementResults(ResultBuilder & Results,bool NeedAt)4551 static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) {
4552   typedef CodeCompletionResult Result;
4553   CodeCompletionBuilder Builder(Results.getAllocator(),
4554                                 Results.getCodeCompletionTUInfo());
4555 
4556   if (Results.includeCodePatterns()) {
4557     // @try { statements } @catch ( declaration ) { statements } @finally
4558     //   { statements }
4559     Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try"));
4560     Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4561     Builder.AddPlaceholderChunk("statements");
4562     Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4563     Builder.AddTextChunk("@catch");
4564     Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4565     Builder.AddPlaceholderChunk("parameter");
4566     Builder.AddChunk(CodeCompletionString::CK_RightParen);
4567     Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4568     Builder.AddPlaceholderChunk("statements");
4569     Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4570     Builder.AddTextChunk("@finally");
4571     Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4572     Builder.AddPlaceholderChunk("statements");
4573     Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4574     Results.AddResult(Result(Builder.TakeString()));
4575   }
4576 
4577   // @throw
4578   Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw"));
4579   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4580   Builder.AddPlaceholderChunk("expression");
4581   Results.AddResult(Result(Builder.TakeString()));
4582 
4583   if (Results.includeCodePatterns()) {
4584     // @synchronized ( expression ) { statements }
4585     Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized"));
4586     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4587     Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4588     Builder.AddPlaceholderChunk("expression");
4589     Builder.AddChunk(CodeCompletionString::CK_RightParen);
4590     Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4591     Builder.AddPlaceholderChunk("statements");
4592     Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4593     Results.AddResult(Result(Builder.TakeString()));
4594   }
4595 }
4596 
AddObjCVisibilityResults(const LangOptions & LangOpts,ResultBuilder & Results,bool NeedAt)4597 static void AddObjCVisibilityResults(const LangOptions &LangOpts,
4598                                      ResultBuilder &Results,
4599                                      bool NeedAt) {
4600   typedef CodeCompletionResult Result;
4601   Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private")));
4602   Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected")));
4603   Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public")));
4604   if (LangOpts.ObjC2)
4605     Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package")));
4606 }
4607 
CodeCompleteObjCAtVisibility(Scope * S)4608 void Sema::CodeCompleteObjCAtVisibility(Scope *S) {
4609   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4610                         CodeCompleter->getCodeCompletionTUInfo(),
4611                         CodeCompletionContext::CCC_Other);
4612   Results.EnterNewScope();
4613   AddObjCVisibilityResults(getLangOpts(), Results, false);
4614   Results.ExitScope();
4615   HandleCodeCompleteResults(this, CodeCompleter,
4616                             CodeCompletionContext::CCC_Other,
4617                             Results.data(),Results.size());
4618 }
4619 
CodeCompleteObjCAtStatement(Scope * S)4620 void Sema::CodeCompleteObjCAtStatement(Scope *S) {
4621   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4622                         CodeCompleter->getCodeCompletionTUInfo(),
4623                         CodeCompletionContext::CCC_Other);
4624   Results.EnterNewScope();
4625   AddObjCStatementResults(Results, false);
4626   AddObjCExpressionResults(Results, false);
4627   Results.ExitScope();
4628   HandleCodeCompleteResults(this, CodeCompleter,
4629                             CodeCompletionContext::CCC_Other,
4630                             Results.data(),Results.size());
4631 }
4632 
CodeCompleteObjCAtExpression(Scope * S)4633 void Sema::CodeCompleteObjCAtExpression(Scope *S) {
4634   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4635                         CodeCompleter->getCodeCompletionTUInfo(),
4636                         CodeCompletionContext::CCC_Other);
4637   Results.EnterNewScope();
4638   AddObjCExpressionResults(Results, false);
4639   Results.ExitScope();
4640   HandleCodeCompleteResults(this, CodeCompleter,
4641                             CodeCompletionContext::CCC_Other,
4642                             Results.data(),Results.size());
4643 }
4644 
4645 /// \brief Determine whether the addition of the given flag to an Objective-C
4646 /// property's attributes will cause a conflict.
ObjCPropertyFlagConflicts(unsigned Attributes,unsigned NewFlag)4647 static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
4648   // Check if we've already added this flag.
4649   if (Attributes & NewFlag)
4650     return true;
4651 
4652   Attributes |= NewFlag;
4653 
4654   // Check for collisions with "readonly".
4655   if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
4656       (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
4657     return true;
4658 
4659   // Check for more than one of { assign, copy, retain, strong, weak }.
4660   unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign |
4661                                          ObjCDeclSpec::DQ_PR_unsafe_unretained |
4662                                              ObjCDeclSpec::DQ_PR_copy |
4663                                              ObjCDeclSpec::DQ_PR_retain |
4664                                              ObjCDeclSpec::DQ_PR_strong |
4665                                              ObjCDeclSpec::DQ_PR_weak);
4666   if (AssignCopyRetMask &&
4667       AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign &&
4668       AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained &&
4669       AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy &&
4670       AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain &&
4671       AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong &&
4672       AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak)
4673     return true;
4674 
4675   return false;
4676 }
4677 
CodeCompleteObjCPropertyFlags(Scope * S,ObjCDeclSpec & ODS)4678 void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
4679   if (!CodeCompleter)
4680     return;
4681 
4682   unsigned Attributes = ODS.getPropertyAttributes();
4683 
4684   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4685                         CodeCompleter->getCodeCompletionTUInfo(),
4686                         CodeCompletionContext::CCC_Other);
4687   Results.EnterNewScope();
4688   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly))
4689     Results.AddResult(CodeCompletionResult("readonly"));
4690   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign))
4691     Results.AddResult(CodeCompletionResult("assign"));
4692   if (!ObjCPropertyFlagConflicts(Attributes,
4693                                  ObjCDeclSpec::DQ_PR_unsafe_unretained))
4694     Results.AddResult(CodeCompletionResult("unsafe_unretained"));
4695   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite))
4696     Results.AddResult(CodeCompletionResult("readwrite"));
4697   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain))
4698     Results.AddResult(CodeCompletionResult("retain"));
4699   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong))
4700     Results.AddResult(CodeCompletionResult("strong"));
4701   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy))
4702     Results.AddResult(CodeCompletionResult("copy"));
4703   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic))
4704     Results.AddResult(CodeCompletionResult("nonatomic"));
4705   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic))
4706     Results.AddResult(CodeCompletionResult("atomic"));
4707 
4708   // Only suggest "weak" if we're compiling for ARC-with-weak-references or GC.
4709   if (getLangOpts().ObjCARCWeak || getLangOpts().getGC() != LangOptions::NonGC)
4710     if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak))
4711       Results.AddResult(CodeCompletionResult("weak"));
4712 
4713   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) {
4714     CodeCompletionBuilder Setter(Results.getAllocator(),
4715                                  Results.getCodeCompletionTUInfo());
4716     Setter.AddTypedTextChunk("setter");
4717     Setter.AddTextChunk("=");
4718     Setter.AddPlaceholderChunk("method");
4719     Results.AddResult(CodeCompletionResult(Setter.TakeString()));
4720   }
4721   if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) {
4722     CodeCompletionBuilder Getter(Results.getAllocator(),
4723                                  Results.getCodeCompletionTUInfo());
4724     Getter.AddTypedTextChunk("getter");
4725     Getter.AddTextChunk("=");
4726     Getter.AddPlaceholderChunk("method");
4727     Results.AddResult(CodeCompletionResult(Getter.TakeString()));
4728   }
4729   Results.ExitScope();
4730   HandleCodeCompleteResults(this, CodeCompleter,
4731                             CodeCompletionContext::CCC_Other,
4732                             Results.data(),Results.size());
4733 }
4734 
4735 /// \brief Describes the kind of Objective-C method that we want to find
4736 /// via code completion.
4737 enum ObjCMethodKind {
4738   MK_Any, ///< Any kind of method, provided it means other specified criteria.
4739   MK_ZeroArgSelector, ///< Zero-argument (unary) selector.
4740   MK_OneArgSelector ///< One-argument selector.
4741 };
4742 
isAcceptableObjCSelector(Selector Sel,ObjCMethodKind WantKind,ArrayRef<IdentifierInfo * > SelIdents,bool AllowSameLength=true)4743 static bool isAcceptableObjCSelector(Selector Sel,
4744                                      ObjCMethodKind WantKind,
4745                                      ArrayRef<IdentifierInfo *> SelIdents,
4746                                      bool AllowSameLength = true) {
4747   unsigned NumSelIdents = SelIdents.size();
4748   if (NumSelIdents > Sel.getNumArgs())
4749     return false;
4750 
4751   switch (WantKind) {
4752     case MK_Any:             break;
4753     case MK_ZeroArgSelector: return Sel.isUnarySelector();
4754     case MK_OneArgSelector:  return Sel.getNumArgs() == 1;
4755   }
4756 
4757   if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs())
4758     return false;
4759 
4760   for (unsigned I = 0; I != NumSelIdents; ++I)
4761     if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
4762       return false;
4763 
4764   return true;
4765 }
4766 
isAcceptableObjCMethod(ObjCMethodDecl * Method,ObjCMethodKind WantKind,ArrayRef<IdentifierInfo * > SelIdents,bool AllowSameLength=true)4767 static bool isAcceptableObjCMethod(ObjCMethodDecl *Method,
4768                                    ObjCMethodKind WantKind,
4769                                    ArrayRef<IdentifierInfo *> SelIdents,
4770                                    bool AllowSameLength = true) {
4771   return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents,
4772                                   AllowSameLength);
4773 }
4774 
4775 namespace {
4776   /// \brief A set of selectors, which is used to avoid introducing multiple
4777   /// completions with the same selector into the result set.
4778   typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet;
4779 }
4780 
4781 /// \brief Add all of the Objective-C methods in the given Objective-C
4782 /// container to the set of results.
4783 ///
4784 /// The container will be a class, protocol, category, or implementation of
4785 /// any of the above. This mether will recurse to include methods from
4786 /// the superclasses of classes along with their categories, protocols, and
4787 /// implementations.
4788 ///
4789 /// \param Container the container in which we'll look to find methods.
4790 ///
4791 /// \param WantInstanceMethods Whether to add instance methods (only); if
4792 /// false, this routine will add factory methods (only).
4793 ///
4794 /// \param CurContext the context in which we're performing the lookup that
4795 /// finds methods.
4796 ///
4797 /// \param AllowSameLength Whether we allow a method to be added to the list
4798 /// when it has the same number of parameters as we have selector identifiers.
4799 ///
4800 /// \param Results the structure into which we'll add results.
AddObjCMethods(ObjCContainerDecl * Container,bool WantInstanceMethods,ObjCMethodKind WantKind,ArrayRef<IdentifierInfo * > SelIdents,DeclContext * CurContext,VisitedSelectorSet & Selectors,bool AllowSameLength,ResultBuilder & Results,bool InOriginalClass=true)4801 static void AddObjCMethods(ObjCContainerDecl *Container,
4802                            bool WantInstanceMethods,
4803                            ObjCMethodKind WantKind,
4804                            ArrayRef<IdentifierInfo *> SelIdents,
4805                            DeclContext *CurContext,
4806                            VisitedSelectorSet &Selectors,
4807                            bool AllowSameLength,
4808                            ResultBuilder &Results,
4809                            bool InOriginalClass = true) {
4810   typedef CodeCompletionResult Result;
4811   Container = getContainerDef(Container);
4812   ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
4813   bool isRootClass = IFace && !IFace->getSuperClass();
4814   for (auto *M : Container->methods()) {
4815     // The instance methods on the root class can be messaged via the
4816     // metaclass.
4817     if (M->isInstanceMethod() == WantInstanceMethods ||
4818         (isRootClass && !WantInstanceMethods)) {
4819       // Check whether the selector identifiers we've been given are a
4820       // subset of the identifiers for this particular method.
4821       if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength))
4822         continue;
4823 
4824       if (!Selectors.insert(M->getSelector()).second)
4825         continue;
4826 
4827       Result R = Result(M, Results.getBasePriority(M), nullptr);
4828       R.StartParameter = SelIdents.size();
4829       R.AllParametersAreInformative = (WantKind != MK_Any);
4830       if (!InOriginalClass)
4831         R.Priority += CCD_InBaseClass;
4832       Results.MaybeAddResult(R, CurContext);
4833     }
4834   }
4835 
4836   // Visit the protocols of protocols.
4837   if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
4838     if (Protocol->hasDefinition()) {
4839       const ObjCList<ObjCProtocolDecl> &Protocols
4840         = Protocol->getReferencedProtocols();
4841       for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
4842                                                 E = Protocols.end();
4843            I != E; ++I)
4844         AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
4845                        CurContext, Selectors, AllowSameLength, Results, false);
4846     }
4847   }
4848 
4849   if (!IFace || !IFace->hasDefinition())
4850     return;
4851 
4852   // Add methods in protocols.
4853   for (auto *I : IFace->protocols())
4854     AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents,
4855                    CurContext, Selectors, AllowSameLength, Results, false);
4856 
4857   // Add methods in categories.
4858   for (auto *CatDecl : IFace->known_categories()) {
4859     AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents,
4860                    CurContext, Selectors, AllowSameLength,
4861                    Results, InOriginalClass);
4862 
4863     // Add a categories protocol methods.
4864     const ObjCList<ObjCProtocolDecl> &Protocols
4865       = CatDecl->getReferencedProtocols();
4866     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
4867                                               E = Protocols.end();
4868          I != E; ++I)
4869       AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
4870                      CurContext, Selectors, AllowSameLength,
4871                      Results, false);
4872 
4873     // Add methods in category implementations.
4874     if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
4875       AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
4876                      CurContext, Selectors, AllowSameLength,
4877                      Results, InOriginalClass);
4878   }
4879 
4880   // Add methods in superclass.
4881   if (IFace->getSuperClass())
4882     AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind,
4883                    SelIdents, CurContext, Selectors,
4884                    AllowSameLength, Results, false);
4885 
4886   // Add methods in our implementation, if any.
4887   if (ObjCImplementationDecl *Impl = IFace->getImplementation())
4888     AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
4889                    CurContext, Selectors, AllowSameLength,
4890                    Results, InOriginalClass);
4891 }
4892 
4893 
CodeCompleteObjCPropertyGetter(Scope * S)4894 void Sema::CodeCompleteObjCPropertyGetter(Scope *S) {
4895   // Try to find the interface where getters might live.
4896   ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
4897   if (!Class) {
4898     if (ObjCCategoryDecl *Category
4899           = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
4900       Class = Category->getClassInterface();
4901 
4902     if (!Class)
4903       return;
4904   }
4905 
4906   // Find all of the potential getters.
4907   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4908                         CodeCompleter->getCodeCompletionTUInfo(),
4909                         CodeCompletionContext::CCC_Other);
4910   Results.EnterNewScope();
4911 
4912   VisitedSelectorSet Selectors;
4913   AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors,
4914                  /*AllowSameLength=*/true, Results);
4915   Results.ExitScope();
4916   HandleCodeCompleteResults(this, CodeCompleter,
4917                             CodeCompletionContext::CCC_Other,
4918                             Results.data(),Results.size());
4919 }
4920 
CodeCompleteObjCPropertySetter(Scope * S)4921 void Sema::CodeCompleteObjCPropertySetter(Scope *S) {
4922   // Try to find the interface where setters might live.
4923   ObjCInterfaceDecl *Class
4924     = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
4925   if (!Class) {
4926     if (ObjCCategoryDecl *Category
4927           = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
4928       Class = Category->getClassInterface();
4929 
4930     if (!Class)
4931       return;
4932   }
4933 
4934   // Find all of the potential getters.
4935   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4936                         CodeCompleter->getCodeCompletionTUInfo(),
4937                         CodeCompletionContext::CCC_Other);
4938   Results.EnterNewScope();
4939 
4940   VisitedSelectorSet Selectors;
4941   AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext,
4942                  Selectors, /*AllowSameLength=*/true, Results);
4943 
4944   Results.ExitScope();
4945   HandleCodeCompleteResults(this, CodeCompleter,
4946                             CodeCompletionContext::CCC_Other,
4947                             Results.data(),Results.size());
4948 }
4949 
CodeCompleteObjCPassingType(Scope * S,ObjCDeclSpec & DS,bool IsParameter)4950 void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS,
4951                                        bool IsParameter) {
4952   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4953                         CodeCompleter->getCodeCompletionTUInfo(),
4954                         CodeCompletionContext::CCC_Type);
4955   Results.EnterNewScope();
4956 
4957   // Add context-sensitive, Objective-C parameter-passing keywords.
4958   bool AddedInOut = false;
4959   if ((DS.getObjCDeclQualifier() &
4960        (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) {
4961     Results.AddResult("in");
4962     Results.AddResult("inout");
4963     AddedInOut = true;
4964   }
4965   if ((DS.getObjCDeclQualifier() &
4966        (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) {
4967     Results.AddResult("out");
4968     if (!AddedInOut)
4969       Results.AddResult("inout");
4970   }
4971   if ((DS.getObjCDeclQualifier() &
4972        (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref |
4973         ObjCDeclSpec::DQ_Oneway)) == 0) {
4974      Results.AddResult("bycopy");
4975      Results.AddResult("byref");
4976      Results.AddResult("oneway");
4977   }
4978 
4979   // If we're completing the return type of an Objective-C method and the
4980   // identifier IBAction refers to a macro, provide a completion item for
4981   // an action, e.g.,
4982   //   IBAction)<#selector#>:(id)sender
4983   if (DS.getObjCDeclQualifier() == 0 && !IsParameter &&
4984       Context.Idents.get("IBAction").hasMacroDefinition()) {
4985     CodeCompletionBuilder Builder(Results.getAllocator(),
4986                                   Results.getCodeCompletionTUInfo(),
4987                                   CCP_CodePattern, CXAvailability_Available);
4988     Builder.AddTypedTextChunk("IBAction");
4989     Builder.AddChunk(CodeCompletionString::CK_RightParen);
4990     Builder.AddPlaceholderChunk("selector");
4991     Builder.AddChunk(CodeCompletionString::CK_Colon);
4992     Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4993     Builder.AddTextChunk("id");
4994     Builder.AddChunk(CodeCompletionString::CK_RightParen);
4995     Builder.AddTextChunk("sender");
4996     Results.AddResult(CodeCompletionResult(Builder.TakeString()));
4997   }
4998 
4999   // If we're completing the return type, provide 'instancetype'.
5000   if (!IsParameter) {
5001     Results.AddResult(CodeCompletionResult("instancetype"));
5002   }
5003 
5004   // Add various builtin type names and specifiers.
5005   AddOrdinaryNameResults(PCC_Type, S, *this, Results);
5006   Results.ExitScope();
5007 
5008   // Add the various type names
5009   Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
5010   CodeCompletionDeclConsumer Consumer(Results, CurContext);
5011   LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5012                      CodeCompleter->includeGlobals());
5013 
5014   if (CodeCompleter->includeMacros())
5015     AddMacroResults(PP, Results, false);
5016 
5017   HandleCodeCompleteResults(this, CodeCompleter,
5018                             CodeCompletionContext::CCC_Type,
5019                             Results.data(), Results.size());
5020 }
5021 
5022 /// \brief When we have an expression with type "id", we may assume
5023 /// that it has some more-specific class type based on knowledge of
5024 /// common uses of Objective-C. This routine returns that class type,
5025 /// or NULL if no better result could be determined.
GetAssumedMessageSendExprType(Expr * E)5026 static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) {
5027   ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E);
5028   if (!Msg)
5029     return nullptr;
5030 
5031   Selector Sel = Msg->getSelector();
5032   if (Sel.isNull())
5033     return nullptr;
5034 
5035   IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0);
5036   if (!Id)
5037     return nullptr;
5038 
5039   ObjCMethodDecl *Method = Msg->getMethodDecl();
5040   if (!Method)
5041     return nullptr;
5042 
5043   // Determine the class that we're sending the message to.
5044   ObjCInterfaceDecl *IFace = nullptr;
5045   switch (Msg->getReceiverKind()) {
5046   case ObjCMessageExpr::Class:
5047     if (const ObjCObjectType *ObjType
5048                            = Msg->getClassReceiver()->getAs<ObjCObjectType>())
5049       IFace = ObjType->getInterface();
5050     break;
5051 
5052   case ObjCMessageExpr::Instance: {
5053     QualType T = Msg->getInstanceReceiver()->getType();
5054     if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
5055       IFace = Ptr->getInterfaceDecl();
5056     break;
5057   }
5058 
5059   case ObjCMessageExpr::SuperInstance:
5060   case ObjCMessageExpr::SuperClass:
5061     break;
5062   }
5063 
5064   if (!IFace)
5065     return nullptr;
5066 
5067   ObjCInterfaceDecl *Super = IFace->getSuperClass();
5068   if (Method->isInstanceMethod())
5069     return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5070       .Case("retain", IFace)
5071       .Case("strong", IFace)
5072       .Case("autorelease", IFace)
5073       .Case("copy", IFace)
5074       .Case("copyWithZone", IFace)
5075       .Case("mutableCopy", IFace)
5076       .Case("mutableCopyWithZone", IFace)
5077       .Case("awakeFromCoder", IFace)
5078       .Case("replacementObjectFromCoder", IFace)
5079       .Case("class", IFace)
5080       .Case("classForCoder", IFace)
5081       .Case("superclass", Super)
5082       .Default(nullptr);
5083 
5084   return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5085     .Case("new", IFace)
5086     .Case("alloc", IFace)
5087     .Case("allocWithZone", IFace)
5088     .Case("class", IFace)
5089     .Case("superclass", Super)
5090     .Default(nullptr);
5091 }
5092 
5093 // Add a special completion for a message send to "super", which fills in the
5094 // most likely case of forwarding all of our arguments to the superclass
5095 // function.
5096 ///
5097 /// \param S The semantic analysis object.
5098 ///
5099 /// \param NeedSuperKeyword Whether we need to prefix this completion with
5100 /// the "super" keyword. Otherwise, we just need to provide the arguments.
5101 ///
5102 /// \param SelIdents The identifiers in the selector that have already been
5103 /// provided as arguments for a send to "super".
5104 ///
5105 /// \param Results The set of results to augment.
5106 ///
5107 /// \returns the Objective-C method declaration that would be invoked by
5108 /// this "super" completion. If NULL, no completion was added.
AddSuperSendCompletion(Sema & S,bool NeedSuperKeyword,ArrayRef<IdentifierInfo * > SelIdents,ResultBuilder & Results)5109 static ObjCMethodDecl *AddSuperSendCompletion(
5110                                           Sema &S, bool NeedSuperKeyword,
5111                                           ArrayRef<IdentifierInfo *> SelIdents,
5112                                           ResultBuilder &Results) {
5113   ObjCMethodDecl *CurMethod = S.getCurMethodDecl();
5114   if (!CurMethod)
5115     return nullptr;
5116 
5117   ObjCInterfaceDecl *Class = CurMethod->getClassInterface();
5118   if (!Class)
5119     return nullptr;
5120 
5121   // Try to find a superclass method with the same selector.
5122   ObjCMethodDecl *SuperMethod = nullptr;
5123   while ((Class = Class->getSuperClass()) && !SuperMethod) {
5124     // Check in the class
5125     SuperMethod = Class->getMethod(CurMethod->getSelector(),
5126                                    CurMethod->isInstanceMethod());
5127 
5128     // Check in categories or class extensions.
5129     if (!SuperMethod) {
5130       for (const auto *Cat : Class->known_categories()) {
5131         if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(),
5132                                                CurMethod->isInstanceMethod())))
5133           break;
5134       }
5135     }
5136   }
5137 
5138   if (!SuperMethod)
5139     return nullptr;
5140 
5141   // Check whether the superclass method has the same signature.
5142   if (CurMethod->param_size() != SuperMethod->param_size() ||
5143       CurMethod->isVariadic() != SuperMethod->isVariadic())
5144     return nullptr;
5145 
5146   for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(),
5147                                    CurPEnd = CurMethod->param_end(),
5148                                     SuperP = SuperMethod->param_begin();
5149        CurP != CurPEnd; ++CurP, ++SuperP) {
5150     // Make sure the parameter types are compatible.
5151     if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(),
5152                                           (*SuperP)->getType()))
5153       return nullptr;
5154 
5155     // Make sure we have a parameter name to forward!
5156     if (!(*CurP)->getIdentifier())
5157       return nullptr;
5158   }
5159 
5160   // We have a superclass method. Now, form the send-to-super completion.
5161   CodeCompletionBuilder Builder(Results.getAllocator(),
5162                                 Results.getCodeCompletionTUInfo());
5163 
5164   // Give this completion a return type.
5165   AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod,
5166                      Builder);
5167 
5168   // If we need the "super" keyword, add it (plus some spacing).
5169   if (NeedSuperKeyword) {
5170     Builder.AddTypedTextChunk("super");
5171     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
5172   }
5173 
5174   Selector Sel = CurMethod->getSelector();
5175   if (Sel.isUnarySelector()) {
5176     if (NeedSuperKeyword)
5177       Builder.AddTextChunk(Builder.getAllocator().CopyString(
5178                                   Sel.getNameForSlot(0)));
5179     else
5180       Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
5181                                    Sel.getNameForSlot(0)));
5182   } else {
5183     ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin();
5184     for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) {
5185       if (I > SelIdents.size())
5186         Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
5187 
5188       if (I < SelIdents.size())
5189         Builder.AddInformativeChunk(
5190                    Builder.getAllocator().CopyString(
5191                                                  Sel.getNameForSlot(I) + ":"));
5192       else if (NeedSuperKeyword || I > SelIdents.size()) {
5193         Builder.AddTextChunk(
5194                  Builder.getAllocator().CopyString(
5195                                                   Sel.getNameForSlot(I) + ":"));
5196         Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
5197                                          (*CurP)->getIdentifier()->getName()));
5198       } else {
5199         Builder.AddTypedTextChunk(
5200                   Builder.getAllocator().CopyString(
5201                                                   Sel.getNameForSlot(I) + ":"));
5202         Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
5203                                          (*CurP)->getIdentifier()->getName()));
5204       }
5205     }
5206   }
5207 
5208   Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod,
5209                                          CCP_SuperCompletion));
5210   return SuperMethod;
5211 }
5212 
CodeCompleteObjCMessageReceiver(Scope * S)5213 void Sema::CodeCompleteObjCMessageReceiver(Scope *S) {
5214   typedef CodeCompletionResult Result;
5215   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5216                         CodeCompleter->getCodeCompletionTUInfo(),
5217                         CodeCompletionContext::CCC_ObjCMessageReceiver,
5218                         getLangOpts().CPlusPlus11
5219                           ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture
5220                           : &ResultBuilder::IsObjCMessageReceiver);
5221 
5222   CodeCompletionDeclConsumer Consumer(Results, CurContext);
5223   Results.EnterNewScope();
5224   LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5225                      CodeCompleter->includeGlobals());
5226 
5227   // If we are in an Objective-C method inside a class that has a superclass,
5228   // add "super" as an option.
5229   if (ObjCMethodDecl *Method = getCurMethodDecl())
5230     if (ObjCInterfaceDecl *Iface = Method->getClassInterface())
5231       if (Iface->getSuperClass()) {
5232         Results.AddResult(Result("super"));
5233 
5234         AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results);
5235       }
5236 
5237   if (getLangOpts().CPlusPlus11)
5238     addThisCompletion(*this, Results);
5239 
5240   Results.ExitScope();
5241 
5242   if (CodeCompleter->includeMacros())
5243     AddMacroResults(PP, Results, false);
5244   HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
5245                             Results.data(), Results.size());
5246 
5247 }
5248 
CodeCompleteObjCSuperMessage(Scope * S,SourceLocation SuperLoc,ArrayRef<IdentifierInfo * > SelIdents,bool AtArgumentExpression)5249 void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc,
5250                                         ArrayRef<IdentifierInfo *> SelIdents,
5251                                         bool AtArgumentExpression) {
5252   ObjCInterfaceDecl *CDecl = nullptr;
5253   if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5254     // Figure out which interface we're in.
5255     CDecl = CurMethod->getClassInterface();
5256     if (!CDecl)
5257       return;
5258 
5259     // Find the superclass of this class.
5260     CDecl = CDecl->getSuperClass();
5261     if (!CDecl)
5262       return;
5263 
5264     if (CurMethod->isInstanceMethod()) {
5265       // We are inside an instance method, which means that the message
5266       // send [super ...] is actually calling an instance method on the
5267       // current object.
5268       return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents,
5269                                              AtArgumentExpression,
5270                                              CDecl);
5271     }
5272 
5273     // Fall through to send to the superclass in CDecl.
5274   } else {
5275     // "super" may be the name of a type or variable. Figure out which
5276     // it is.
5277     IdentifierInfo *Super = getSuperIdentifier();
5278     NamedDecl *ND = LookupSingleName(S, Super, SuperLoc,
5279                                      LookupOrdinaryName);
5280     if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) {
5281       // "super" names an interface. Use it.
5282     } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) {
5283       if (const ObjCObjectType *Iface
5284             = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>())
5285         CDecl = Iface->getInterface();
5286     } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) {
5287       // "super" names an unresolved type; we can't be more specific.
5288     } else {
5289       // Assume that "super" names some kind of value and parse that way.
5290       CXXScopeSpec SS;
5291       SourceLocation TemplateKWLoc;
5292       UnqualifiedId id;
5293       id.setIdentifier(Super, SuperLoc);
5294       ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id,
5295                                                false, false);
5296       return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(),
5297                                              SelIdents,
5298                                              AtArgumentExpression);
5299     }
5300 
5301     // Fall through
5302   }
5303 
5304   ParsedType Receiver;
5305   if (CDecl)
5306     Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl));
5307   return CodeCompleteObjCClassMessage(S, Receiver, SelIdents,
5308                                       AtArgumentExpression,
5309                                       /*IsSuper=*/true);
5310 }
5311 
5312 /// \brief Given a set of code-completion results for the argument of a message
5313 /// send, determine the preferred type (if any) for that argument expression.
getPreferredArgumentTypeForMessageSend(ResultBuilder & Results,unsigned NumSelIdents)5314 static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results,
5315                                                        unsigned NumSelIdents) {
5316   typedef CodeCompletionResult Result;
5317   ASTContext &Context = Results.getSema().Context;
5318 
5319   QualType PreferredType;
5320   unsigned BestPriority = CCP_Unlikely * 2;
5321   Result *ResultsData = Results.data();
5322   for (unsigned I = 0, N = Results.size(); I != N; ++I) {
5323     Result &R = ResultsData[I];
5324     if (R.Kind == Result::RK_Declaration &&
5325         isa<ObjCMethodDecl>(R.Declaration)) {
5326       if (R.Priority <= BestPriority) {
5327         const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration);
5328         if (NumSelIdents <= Method->param_size()) {
5329           QualType MyPreferredType = Method->parameters()[NumSelIdents - 1]
5330                                        ->getType();
5331           if (R.Priority < BestPriority || PreferredType.isNull()) {
5332             BestPriority = R.Priority;
5333             PreferredType = MyPreferredType;
5334           } else if (!Context.hasSameUnqualifiedType(PreferredType,
5335                                                      MyPreferredType)) {
5336             PreferredType = QualType();
5337           }
5338         }
5339       }
5340     }
5341   }
5342 
5343   return PreferredType;
5344 }
5345 
AddClassMessageCompletions(Sema & SemaRef,Scope * S,ParsedType Receiver,ArrayRef<IdentifierInfo * > SelIdents,bool AtArgumentExpression,bool IsSuper,ResultBuilder & Results)5346 static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
5347                                        ParsedType Receiver,
5348                                        ArrayRef<IdentifierInfo *> SelIdents,
5349                                        bool AtArgumentExpression,
5350                                        bool IsSuper,
5351                                        ResultBuilder &Results) {
5352   typedef CodeCompletionResult Result;
5353   ObjCInterfaceDecl *CDecl = nullptr;
5354 
5355   // If the given name refers to an interface type, retrieve the
5356   // corresponding declaration.
5357   if (Receiver) {
5358     QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr);
5359     if (!T.isNull())
5360       if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>())
5361         CDecl = Interface->getInterface();
5362   }
5363 
5364   // Add all of the factory methods in this Objective-C class, its protocols,
5365   // superclasses, categories, implementation, etc.
5366   Results.EnterNewScope();
5367 
5368   // If this is a send-to-super, try to add the special "super" send
5369   // completion.
5370   if (IsSuper) {
5371     if (ObjCMethodDecl *SuperMethod
5372         = AddSuperSendCompletion(SemaRef, false, SelIdents, Results))
5373       Results.Ignore(SuperMethod);
5374   }
5375 
5376   // If we're inside an Objective-C method definition, prefer its selector to
5377   // others.
5378   if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl())
5379     Results.setPreferredSelector(CurMethod->getSelector());
5380 
5381   VisitedSelectorSet Selectors;
5382   if (CDecl)
5383     AddObjCMethods(CDecl, false, MK_Any, SelIdents,
5384                    SemaRef.CurContext, Selectors, AtArgumentExpression,
5385                    Results);
5386   else {
5387     // We're messaging "id" as a type; provide all class/factory methods.
5388 
5389     // If we have an external source, load the entire class method
5390     // pool from the AST file.
5391     if (SemaRef.getExternalSource()) {
5392       for (uint32_t I = 0,
5393                     N = SemaRef.getExternalSource()->GetNumExternalSelectors();
5394            I != N; ++I) {
5395         Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I);
5396         if (Sel.isNull() || SemaRef.MethodPool.count(Sel))
5397           continue;
5398 
5399         SemaRef.ReadMethodPool(Sel);
5400       }
5401     }
5402 
5403     for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(),
5404                                        MEnd = SemaRef.MethodPool.end();
5405          M != MEnd; ++M) {
5406       for (ObjCMethodList *MethList = &M->second.second;
5407            MethList && MethList->getMethod();
5408            MethList = MethList->getNext()) {
5409         if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
5410           continue;
5411 
5412         Result R(MethList->getMethod(),
5413                  Results.getBasePriority(MethList->getMethod()), nullptr);
5414         R.StartParameter = SelIdents.size();
5415         R.AllParametersAreInformative = false;
5416         Results.MaybeAddResult(R, SemaRef.CurContext);
5417       }
5418     }
5419   }
5420 
5421   Results.ExitScope();
5422 }
5423 
CodeCompleteObjCClassMessage(Scope * S,ParsedType Receiver,ArrayRef<IdentifierInfo * > SelIdents,bool AtArgumentExpression,bool IsSuper)5424 void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver,
5425                                         ArrayRef<IdentifierInfo *> SelIdents,
5426                                         bool AtArgumentExpression,
5427                                         bool IsSuper) {
5428 
5429   QualType T = this->GetTypeFromParser(Receiver);
5430 
5431   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5432                         CodeCompleter->getCodeCompletionTUInfo(),
5433               CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage,
5434                                     T, SelIdents));
5435 
5436   AddClassMessageCompletions(*this, S, Receiver, SelIdents,
5437                              AtArgumentExpression, IsSuper, Results);
5438 
5439   // If we're actually at the argument expression (rather than prior to the
5440   // selector), we're actually performing code completion for an expression.
5441   // Determine whether we have a single, best method. If so, we can
5442   // code-complete the expression using the corresponding parameter type as
5443   // our preferred type, improving completion results.
5444   if (AtArgumentExpression) {
5445     QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
5446                                                               SelIdents.size());
5447     if (PreferredType.isNull())
5448       CodeCompleteOrdinaryName(S, PCC_Expression);
5449     else
5450       CodeCompleteExpression(S, PreferredType);
5451     return;
5452   }
5453 
5454   HandleCodeCompleteResults(this, CodeCompleter,
5455                             Results.getCompletionContext(),
5456                             Results.data(), Results.size());
5457 }
5458 
CodeCompleteObjCInstanceMessage(Scope * S,Expr * Receiver,ArrayRef<IdentifierInfo * > SelIdents,bool AtArgumentExpression,ObjCInterfaceDecl * Super)5459 void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver,
5460                                            ArrayRef<IdentifierInfo *> SelIdents,
5461                                            bool AtArgumentExpression,
5462                                            ObjCInterfaceDecl *Super) {
5463   typedef CodeCompletionResult Result;
5464 
5465   Expr *RecExpr = static_cast<Expr *>(Receiver);
5466 
5467   // If necessary, apply function/array conversion to the receiver.
5468   // C99 6.7.5.3p[7,8].
5469   if (RecExpr) {
5470     ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr);
5471     if (Conv.isInvalid()) // conversion failed. bail.
5472       return;
5473     RecExpr = Conv.get();
5474   }
5475   QualType ReceiverType = RecExpr? RecExpr->getType()
5476                           : Super? Context.getObjCObjectPointerType(
5477                                             Context.getObjCInterfaceType(Super))
5478                                  : Context.getObjCIdType();
5479 
5480   // If we're messaging an expression with type "id" or "Class", check
5481   // whether we know something special about the receiver that allows
5482   // us to assume a more-specific receiver type.
5483   if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) {
5484     if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) {
5485       if (ReceiverType->isObjCClassType())
5486         return CodeCompleteObjCClassMessage(S,
5487                        ParsedType::make(Context.getObjCInterfaceType(IFace)),
5488                                             SelIdents,
5489                                             AtArgumentExpression, Super);
5490 
5491       ReceiverType = Context.getObjCObjectPointerType(
5492                                           Context.getObjCInterfaceType(IFace));
5493     }
5494   } else if (RecExpr && getLangOpts().CPlusPlus) {
5495     ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr);
5496     if (Conv.isUsable()) {
5497       RecExpr = Conv.get();
5498       ReceiverType = RecExpr->getType();
5499     }
5500   }
5501 
5502   // Build the set of methods we can see.
5503   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5504                         CodeCompleter->getCodeCompletionTUInfo(),
5505            CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage,
5506                                  ReceiverType, SelIdents));
5507 
5508   Results.EnterNewScope();
5509 
5510   // If this is a send-to-super, try to add the special "super" send
5511   // completion.
5512   if (Super) {
5513     if (ObjCMethodDecl *SuperMethod
5514           = AddSuperSendCompletion(*this, false, SelIdents, Results))
5515       Results.Ignore(SuperMethod);
5516   }
5517 
5518   // If we're inside an Objective-C method definition, prefer its selector to
5519   // others.
5520   if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
5521     Results.setPreferredSelector(CurMethod->getSelector());
5522 
5523   // Keep track of the selectors we've already added.
5524   VisitedSelectorSet Selectors;
5525 
5526   // Handle messages to Class. This really isn't a message to an instance
5527   // method, so we treat it the same way we would treat a message send to a
5528   // class method.
5529   if (ReceiverType->isObjCClassType() ||
5530       ReceiverType->isObjCQualifiedClassType()) {
5531     if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5532       if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
5533         AddObjCMethods(ClassDecl, false, MK_Any, SelIdents,
5534                        CurContext, Selectors, AtArgumentExpression, Results);
5535     }
5536   }
5537   // Handle messages to a qualified ID ("id<foo>").
5538   else if (const ObjCObjectPointerType *QualID
5539              = ReceiverType->getAsObjCQualifiedIdType()) {
5540     // Search protocols for instance methods.
5541     for (auto *I : QualID->quals())
5542       AddObjCMethods(I, true, MK_Any, SelIdents, CurContext,
5543                      Selectors, AtArgumentExpression, Results);
5544   }
5545   // Handle messages to a pointer to interface type.
5546   else if (const ObjCObjectPointerType *IFacePtr
5547                               = ReceiverType->getAsObjCInterfacePointerType()) {
5548     // Search the class, its superclasses, etc., for instance methods.
5549     AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents,
5550                    CurContext, Selectors, AtArgumentExpression,
5551                    Results);
5552 
5553     // Search protocols for instance methods.
5554     for (auto *I : IFacePtr->quals())
5555       AddObjCMethods(I, true, MK_Any, SelIdents, CurContext,
5556                      Selectors, AtArgumentExpression, Results);
5557   }
5558   // Handle messages to "id".
5559   else if (ReceiverType->isObjCIdType()) {
5560     // We're messaging "id", so provide all instance methods we know
5561     // about as code-completion results.
5562 
5563     // If we have an external source, load the entire class method
5564     // pool from the AST file.
5565     if (ExternalSource) {
5566       for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5567            I != N; ++I) {
5568         Selector Sel = ExternalSource->GetExternalSelector(I);
5569         if (Sel.isNull() || MethodPool.count(Sel))
5570           continue;
5571 
5572         ReadMethodPool(Sel);
5573       }
5574     }
5575 
5576     for (GlobalMethodPool::iterator M = MethodPool.begin(),
5577                                     MEnd = MethodPool.end();
5578          M != MEnd; ++M) {
5579       for (ObjCMethodList *MethList = &M->second.first;
5580            MethList && MethList->getMethod();
5581            MethList = MethList->getNext()) {
5582         if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
5583           continue;
5584 
5585         if (!Selectors.insert(MethList->getMethod()->getSelector()).second)
5586           continue;
5587 
5588         Result R(MethList->getMethod(),
5589                  Results.getBasePriority(MethList->getMethod()), nullptr);
5590         R.StartParameter = SelIdents.size();
5591         R.AllParametersAreInformative = false;
5592         Results.MaybeAddResult(R, CurContext);
5593       }
5594     }
5595   }
5596   Results.ExitScope();
5597 
5598 
5599   // If we're actually at the argument expression (rather than prior to the
5600   // selector), we're actually performing code completion for an expression.
5601   // Determine whether we have a single, best method. If so, we can
5602   // code-complete the expression using the corresponding parameter type as
5603   // our preferred type, improving completion results.
5604   if (AtArgumentExpression) {
5605     QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
5606                                                               SelIdents.size());
5607     if (PreferredType.isNull())
5608       CodeCompleteOrdinaryName(S, PCC_Expression);
5609     else
5610       CodeCompleteExpression(S, PreferredType);
5611     return;
5612   }
5613 
5614   HandleCodeCompleteResults(this, CodeCompleter,
5615                             Results.getCompletionContext(),
5616                             Results.data(),Results.size());
5617 }
5618 
CodeCompleteObjCForCollection(Scope * S,DeclGroupPtrTy IterationVar)5619 void Sema::CodeCompleteObjCForCollection(Scope *S,
5620                                          DeclGroupPtrTy IterationVar) {
5621   CodeCompleteExpressionData Data;
5622   Data.ObjCCollection = true;
5623 
5624   if (IterationVar.getAsOpaquePtr()) {
5625     DeclGroupRef DG = IterationVar.get();
5626     for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) {
5627       if (*I)
5628         Data.IgnoreDecls.push_back(*I);
5629     }
5630   }
5631 
5632   CodeCompleteExpression(S, Data);
5633 }
5634 
CodeCompleteObjCSelector(Scope * S,ArrayRef<IdentifierInfo * > SelIdents)5635 void Sema::CodeCompleteObjCSelector(Scope *S,
5636                                     ArrayRef<IdentifierInfo *> SelIdents) {
5637   // If we have an external source, load the entire class method
5638   // pool from the AST file.
5639   if (ExternalSource) {
5640     for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5641          I != N; ++I) {
5642       Selector Sel = ExternalSource->GetExternalSelector(I);
5643       if (Sel.isNull() || MethodPool.count(Sel))
5644         continue;
5645 
5646       ReadMethodPool(Sel);
5647     }
5648   }
5649 
5650   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5651                         CodeCompleter->getCodeCompletionTUInfo(),
5652                         CodeCompletionContext::CCC_SelectorName);
5653   Results.EnterNewScope();
5654   for (GlobalMethodPool::iterator M = MethodPool.begin(),
5655                                MEnd = MethodPool.end();
5656        M != MEnd; ++M) {
5657 
5658     Selector Sel = M->first;
5659     if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents))
5660       continue;
5661 
5662     CodeCompletionBuilder Builder(Results.getAllocator(),
5663                                   Results.getCodeCompletionTUInfo());
5664     if (Sel.isUnarySelector()) {
5665       Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
5666                                                        Sel.getNameForSlot(0)));
5667       Results.AddResult(Builder.TakeString());
5668       continue;
5669     }
5670 
5671     std::string Accumulator;
5672     for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) {
5673       if (I == SelIdents.size()) {
5674         if (!Accumulator.empty()) {
5675           Builder.AddInformativeChunk(Builder.getAllocator().CopyString(
5676                                                  Accumulator));
5677           Accumulator.clear();
5678         }
5679       }
5680 
5681       Accumulator += Sel.getNameForSlot(I);
5682       Accumulator += ':';
5683     }
5684     Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator));
5685     Results.AddResult(Builder.TakeString());
5686   }
5687   Results.ExitScope();
5688 
5689   HandleCodeCompleteResults(this, CodeCompleter,
5690                             CodeCompletionContext::CCC_SelectorName,
5691                             Results.data(), Results.size());
5692 }
5693 
5694 /// \brief Add all of the protocol declarations that we find in the given
5695 /// (translation unit) context.
AddProtocolResults(DeclContext * Ctx,DeclContext * CurContext,bool OnlyForwardDeclarations,ResultBuilder & Results)5696 static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext,
5697                                bool OnlyForwardDeclarations,
5698                                ResultBuilder &Results) {
5699   typedef CodeCompletionResult Result;
5700 
5701   for (const auto *D : Ctx->decls()) {
5702     // Record any protocols we find.
5703     if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D))
5704       if (!OnlyForwardDeclarations || !Proto->hasDefinition())
5705         Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr),
5706                           CurContext, nullptr, false);
5707   }
5708 }
5709 
CodeCompleteObjCProtocolReferences(IdentifierLocPair * Protocols,unsigned NumProtocols)5710 void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
5711                                               unsigned NumProtocols) {
5712   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5713                         CodeCompleter->getCodeCompletionTUInfo(),
5714                         CodeCompletionContext::CCC_ObjCProtocolName);
5715 
5716   if (CodeCompleter && CodeCompleter->includeGlobals()) {
5717     Results.EnterNewScope();
5718 
5719     // Tell the result set to ignore all of the protocols we have
5720     // already seen.
5721     // FIXME: This doesn't work when caching code-completion results.
5722     for (unsigned I = 0; I != NumProtocols; ++I)
5723       if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first,
5724                                                       Protocols[I].second))
5725         Results.Ignore(Protocol);
5726 
5727     // Add all protocols.
5728     AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false,
5729                        Results);
5730 
5731     Results.ExitScope();
5732   }
5733 
5734   HandleCodeCompleteResults(this, CodeCompleter,
5735                             CodeCompletionContext::CCC_ObjCProtocolName,
5736                             Results.data(),Results.size());
5737 }
5738 
CodeCompleteObjCProtocolDecl(Scope *)5739 void Sema::CodeCompleteObjCProtocolDecl(Scope *) {
5740   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5741                         CodeCompleter->getCodeCompletionTUInfo(),
5742                         CodeCompletionContext::CCC_ObjCProtocolName);
5743 
5744   if (CodeCompleter && CodeCompleter->includeGlobals()) {
5745     Results.EnterNewScope();
5746 
5747     // Add all protocols.
5748     AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true,
5749                        Results);
5750 
5751     Results.ExitScope();
5752   }
5753 
5754   HandleCodeCompleteResults(this, CodeCompleter,
5755                             CodeCompletionContext::CCC_ObjCProtocolName,
5756                             Results.data(),Results.size());
5757 }
5758 
5759 /// \brief Add all of the Objective-C interface declarations that we find in
5760 /// the given (translation unit) context.
AddInterfaceResults(DeclContext * Ctx,DeclContext * CurContext,bool OnlyForwardDeclarations,bool OnlyUnimplemented,ResultBuilder & Results)5761 static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext,
5762                                 bool OnlyForwardDeclarations,
5763                                 bool OnlyUnimplemented,
5764                                 ResultBuilder &Results) {
5765   typedef CodeCompletionResult Result;
5766 
5767   for (const auto *D : Ctx->decls()) {
5768     // Record any interfaces we find.
5769     if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
5770       if ((!OnlyForwardDeclarations || !Class->hasDefinition()) &&
5771           (!OnlyUnimplemented || !Class->getImplementation()))
5772         Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr),
5773                           CurContext, nullptr, false);
5774   }
5775 }
5776 
CodeCompleteObjCInterfaceDecl(Scope * S)5777 void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) {
5778   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5779                         CodeCompleter->getCodeCompletionTUInfo(),
5780                         CodeCompletionContext::CCC_Other);
5781   Results.EnterNewScope();
5782 
5783   if (CodeCompleter->includeGlobals()) {
5784     // Add all classes.
5785     AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5786                         false, Results);
5787   }
5788 
5789   Results.ExitScope();
5790 
5791   HandleCodeCompleteResults(this, CodeCompleter,
5792                             CodeCompletionContext::CCC_ObjCInterfaceName,
5793                             Results.data(),Results.size());
5794 }
5795 
CodeCompleteObjCSuperclass(Scope * S,IdentifierInfo * ClassName,SourceLocation ClassNameLoc)5796 void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName,
5797                                       SourceLocation ClassNameLoc) {
5798   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5799                         CodeCompleter->getCodeCompletionTUInfo(),
5800                         CodeCompletionContext::CCC_ObjCInterfaceName);
5801   Results.EnterNewScope();
5802 
5803   // Make sure that we ignore the class we're currently defining.
5804   NamedDecl *CurClass
5805     = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
5806   if (CurClass && isa<ObjCInterfaceDecl>(CurClass))
5807     Results.Ignore(CurClass);
5808 
5809   if (CodeCompleter->includeGlobals()) {
5810     // Add all classes.
5811     AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5812                         false, Results);
5813   }
5814 
5815   Results.ExitScope();
5816 
5817   HandleCodeCompleteResults(this, CodeCompleter,
5818                             CodeCompletionContext::CCC_ObjCInterfaceName,
5819                             Results.data(),Results.size());
5820 }
5821 
CodeCompleteObjCImplementationDecl(Scope * S)5822 void Sema::CodeCompleteObjCImplementationDecl(Scope *S) {
5823   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5824                         CodeCompleter->getCodeCompletionTUInfo(),
5825                         CodeCompletionContext::CCC_Other);
5826   Results.EnterNewScope();
5827 
5828   if (CodeCompleter->includeGlobals()) {
5829     // Add all unimplemented classes.
5830     AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5831                         true, Results);
5832   }
5833 
5834   Results.ExitScope();
5835 
5836   HandleCodeCompleteResults(this, CodeCompleter,
5837                             CodeCompletionContext::CCC_ObjCInterfaceName,
5838                             Results.data(),Results.size());
5839 }
5840 
CodeCompleteObjCInterfaceCategory(Scope * S,IdentifierInfo * ClassName,SourceLocation ClassNameLoc)5841 void Sema::CodeCompleteObjCInterfaceCategory(Scope *S,
5842                                              IdentifierInfo *ClassName,
5843                                              SourceLocation ClassNameLoc) {
5844   typedef CodeCompletionResult Result;
5845 
5846   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5847                         CodeCompleter->getCodeCompletionTUInfo(),
5848                         CodeCompletionContext::CCC_ObjCCategoryName);
5849 
5850   // Ignore any categories we find that have already been implemented by this
5851   // interface.
5852   llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
5853   NamedDecl *CurClass
5854     = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
5855   if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){
5856     for (const auto *Cat : Class->visible_categories())
5857       CategoryNames.insert(Cat->getIdentifier());
5858   }
5859 
5860   // Add all of the categories we know about.
5861   Results.EnterNewScope();
5862   TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5863   for (const auto *D : TU->decls())
5864     if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D))
5865       if (CategoryNames.insert(Category->getIdentifier()).second)
5866         Results.AddResult(Result(Category, Results.getBasePriority(Category),
5867                                  nullptr),
5868                           CurContext, nullptr, false);
5869   Results.ExitScope();
5870 
5871   HandleCodeCompleteResults(this, CodeCompleter,
5872                             CodeCompletionContext::CCC_ObjCCategoryName,
5873                             Results.data(),Results.size());
5874 }
5875 
CodeCompleteObjCImplementationCategory(Scope * S,IdentifierInfo * ClassName,SourceLocation ClassNameLoc)5876 void Sema::CodeCompleteObjCImplementationCategory(Scope *S,
5877                                                   IdentifierInfo *ClassName,
5878                                                   SourceLocation ClassNameLoc) {
5879   typedef CodeCompletionResult Result;
5880 
5881   // Find the corresponding interface. If we couldn't find the interface, the
5882   // program itself is ill-formed. However, we'll try to be helpful still by
5883   // providing the list of all of the categories we know about.
5884   NamedDecl *CurClass
5885     = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
5886   ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass);
5887   if (!Class)
5888     return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc);
5889 
5890   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5891                         CodeCompleter->getCodeCompletionTUInfo(),
5892                         CodeCompletionContext::CCC_ObjCCategoryName);
5893 
5894   // Add all of the categories that have have corresponding interface
5895   // declarations in this class and any of its superclasses, except for
5896   // already-implemented categories in the class itself.
5897   llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
5898   Results.EnterNewScope();
5899   bool IgnoreImplemented = true;
5900   while (Class) {
5901     for (const auto *Cat : Class->visible_categories()) {
5902       if ((!IgnoreImplemented || !Cat->getImplementation()) &&
5903           CategoryNames.insert(Cat->getIdentifier()).second)
5904         Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr),
5905                           CurContext, nullptr, false);
5906     }
5907 
5908     Class = Class->getSuperClass();
5909     IgnoreImplemented = false;
5910   }
5911   Results.ExitScope();
5912 
5913   HandleCodeCompleteResults(this, CodeCompleter,
5914                             CodeCompletionContext::CCC_ObjCCategoryName,
5915                             Results.data(),Results.size());
5916 }
5917 
CodeCompleteObjCPropertyDefinition(Scope * S)5918 void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) {
5919   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5920                         CodeCompleter->getCodeCompletionTUInfo(),
5921                         CodeCompletionContext::CCC_Other);
5922 
5923   // Figure out where this @synthesize lives.
5924   ObjCContainerDecl *Container
5925     = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
5926   if (!Container ||
5927       (!isa<ObjCImplementationDecl>(Container) &&
5928        !isa<ObjCCategoryImplDecl>(Container)))
5929     return;
5930 
5931   // Ignore any properties that have already been implemented.
5932   Container = getContainerDef(Container);
5933   for (const auto *D : Container->decls())
5934     if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D))
5935       Results.Ignore(PropertyImpl->getPropertyDecl());
5936 
5937   // Add any properties that we find.
5938   AddedPropertiesSet AddedProperties;
5939   Results.EnterNewScope();
5940   if (ObjCImplementationDecl *ClassImpl
5941         = dyn_cast<ObjCImplementationDecl>(Container))
5942     AddObjCProperties(ClassImpl->getClassInterface(), false,
5943                       /*AllowNullaryMethods=*/false, CurContext,
5944                       AddedProperties, Results);
5945   else
5946     AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(),
5947                       false, /*AllowNullaryMethods=*/false, CurContext,
5948                       AddedProperties, Results);
5949   Results.ExitScope();
5950 
5951   HandleCodeCompleteResults(this, CodeCompleter,
5952                             CodeCompletionContext::CCC_Other,
5953                             Results.data(),Results.size());
5954 }
5955 
CodeCompleteObjCPropertySynthesizeIvar(Scope * S,IdentifierInfo * PropertyName)5956 void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
5957                                                   IdentifierInfo *PropertyName) {
5958   typedef CodeCompletionResult Result;
5959   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5960                         CodeCompleter->getCodeCompletionTUInfo(),
5961                         CodeCompletionContext::CCC_Other);
5962 
5963   // Figure out where this @synthesize lives.
5964   ObjCContainerDecl *Container
5965     = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
5966   if (!Container ||
5967       (!isa<ObjCImplementationDecl>(Container) &&
5968        !isa<ObjCCategoryImplDecl>(Container)))
5969     return;
5970 
5971   // Figure out which interface we're looking into.
5972   ObjCInterfaceDecl *Class = nullptr;
5973   if (ObjCImplementationDecl *ClassImpl
5974                                  = dyn_cast<ObjCImplementationDecl>(Container))
5975     Class = ClassImpl->getClassInterface();
5976   else
5977     Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
5978                                                           ->getClassInterface();
5979 
5980   // Determine the type of the property we're synthesizing.
5981   QualType PropertyType = Context.getObjCIdType();
5982   if (Class) {
5983     if (ObjCPropertyDecl *Property
5984                               = Class->FindPropertyDeclaration(PropertyName)) {
5985       PropertyType
5986         = Property->getType().getNonReferenceType().getUnqualifiedType();
5987 
5988       // Give preference to ivars
5989       Results.setPreferredType(PropertyType);
5990     }
5991   }
5992 
5993   // Add all of the instance variables in this class and its superclasses.
5994   Results.EnterNewScope();
5995   bool SawSimilarlyNamedIvar = false;
5996   std::string NameWithPrefix;
5997   NameWithPrefix += '_';
5998   NameWithPrefix += PropertyName->getName();
5999   std::string NameWithSuffix = PropertyName->getName().str();
6000   NameWithSuffix += '_';
6001   for(; Class; Class = Class->getSuperClass()) {
6002     for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar;
6003          Ivar = Ivar->getNextIvar()) {
6004       Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr),
6005                         CurContext, nullptr, false);
6006 
6007       // Determine whether we've seen an ivar with a name similar to the
6008       // property.
6009       if ((PropertyName == Ivar->getIdentifier() ||
6010            NameWithPrefix == Ivar->getName() ||
6011            NameWithSuffix == Ivar->getName())) {
6012         SawSimilarlyNamedIvar = true;
6013 
6014         // Reduce the priority of this result by one, to give it a slight
6015         // advantage over other results whose names don't match so closely.
6016         if (Results.size() &&
6017             Results.data()[Results.size() - 1].Kind
6018                                       == CodeCompletionResult::RK_Declaration &&
6019             Results.data()[Results.size() - 1].Declaration == Ivar)
6020           Results.data()[Results.size() - 1].Priority--;
6021       }
6022     }
6023   }
6024 
6025   if (!SawSimilarlyNamedIvar) {
6026     // Create ivar result _propName, that the user can use to synthesize
6027     // an ivar of the appropriate type.
6028     unsigned Priority = CCP_MemberDeclaration + 1;
6029     typedef CodeCompletionResult Result;
6030     CodeCompletionAllocator &Allocator = Results.getAllocator();
6031     CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(),
6032                                   Priority,CXAvailability_Available);
6033 
6034     PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
6035     Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context,
6036                                                        Policy, Allocator));
6037     Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix));
6038     Results.AddResult(Result(Builder.TakeString(), Priority,
6039                              CXCursor_ObjCIvarDecl));
6040   }
6041 
6042   Results.ExitScope();
6043 
6044   HandleCodeCompleteResults(this, CodeCompleter,
6045                             CodeCompletionContext::CCC_Other,
6046                             Results.data(),Results.size());
6047 }
6048 
6049 // Mapping from selectors to the methods that implement that selector, along
6050 // with the "in original class" flag.
6051 typedef llvm::DenseMap<
6052     Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap;
6053 
6054 /// \brief Find all of the methods that reside in the given container
6055 /// (and its superclasses, protocols, etc.) that meet the given
6056 /// criteria. Insert those methods into the map of known methods,
6057 /// indexed by selector so they can be easily found.
FindImplementableMethods(ASTContext & Context,ObjCContainerDecl * Container,bool WantInstanceMethods,QualType ReturnType,KnownMethodsMap & KnownMethods,bool InOriginalClass=true)6058 static void FindImplementableMethods(ASTContext &Context,
6059                                      ObjCContainerDecl *Container,
6060                                      bool WantInstanceMethods,
6061                                      QualType ReturnType,
6062                                      KnownMethodsMap &KnownMethods,
6063                                      bool InOriginalClass = true) {
6064   if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) {
6065     // Make sure we have a definition; that's what we'll walk.
6066     if (!IFace->hasDefinition())
6067       return;
6068 
6069     IFace = IFace->getDefinition();
6070     Container = IFace;
6071 
6072     const ObjCList<ObjCProtocolDecl> &Protocols
6073       = IFace->getReferencedProtocols();
6074     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6075                                               E = Protocols.end();
6076          I != E; ++I)
6077       FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6078                                KnownMethods, InOriginalClass);
6079 
6080     // Add methods from any class extensions and categories.
6081     for (auto *Cat : IFace->visible_categories()) {
6082       FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType,
6083                                KnownMethods, false);
6084     }
6085 
6086     // Visit the superclass.
6087     if (IFace->getSuperClass())
6088       FindImplementableMethods(Context, IFace->getSuperClass(),
6089                                WantInstanceMethods, ReturnType,
6090                                KnownMethods, false);
6091   }
6092 
6093   if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
6094     // Recurse into protocols.
6095     const ObjCList<ObjCProtocolDecl> &Protocols
6096       = Category->getReferencedProtocols();
6097     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6098                                               E = Protocols.end();
6099          I != E; ++I)
6100       FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6101                                KnownMethods, InOriginalClass);
6102 
6103     // If this category is the original class, jump to the interface.
6104     if (InOriginalClass && Category->getClassInterface())
6105       FindImplementableMethods(Context, Category->getClassInterface(),
6106                                WantInstanceMethods, ReturnType, KnownMethods,
6107                                false);
6108   }
6109 
6110   if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
6111     // Make sure we have a definition; that's what we'll walk.
6112     if (!Protocol->hasDefinition())
6113       return;
6114     Protocol = Protocol->getDefinition();
6115     Container = Protocol;
6116 
6117     // Recurse into protocols.
6118     const ObjCList<ObjCProtocolDecl> &Protocols
6119       = Protocol->getReferencedProtocols();
6120     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6121            E = Protocols.end();
6122          I != E; ++I)
6123       FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6124                                KnownMethods, false);
6125   }
6126 
6127   // Add methods in this container. This operation occurs last because
6128   // we want the methods from this container to override any methods
6129   // we've previously seen with the same selector.
6130   for (auto *M : Container->methods()) {
6131     if (M->isInstanceMethod() == WantInstanceMethods) {
6132       if (!ReturnType.isNull() &&
6133           !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType()))
6134         continue;
6135 
6136       KnownMethods[M->getSelector()] =
6137           KnownMethodsMap::mapped_type(M, InOriginalClass);
6138     }
6139   }
6140 }
6141 
6142 /// \brief Add the parenthesized return or parameter type chunk to a code
6143 /// completion string.
AddObjCPassingTypeChunk(QualType Type,unsigned ObjCDeclQuals,ASTContext & Context,const PrintingPolicy & Policy,CodeCompletionBuilder & Builder)6144 static void AddObjCPassingTypeChunk(QualType Type,
6145                                     unsigned ObjCDeclQuals,
6146                                     ASTContext &Context,
6147                                     const PrintingPolicy &Policy,
6148                                     CodeCompletionBuilder &Builder) {
6149   Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6150   std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals);
6151   if (!Quals.empty())
6152     Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals));
6153   Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy,
6154                                                Builder.getAllocator()));
6155   Builder.AddChunk(CodeCompletionString::CK_RightParen);
6156 }
6157 
6158 /// \brief Determine whether the given class is or inherits from a class by
6159 /// the given name.
InheritsFromClassNamed(ObjCInterfaceDecl * Class,StringRef Name)6160 static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class,
6161                                    StringRef Name) {
6162   if (!Class)
6163     return false;
6164 
6165   if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name)
6166     return true;
6167 
6168   return InheritsFromClassNamed(Class->getSuperClass(), Name);
6169 }
6170 
6171 /// \brief Add code completions for Objective-C Key-Value Coding (KVC) and
6172 /// Key-Value Observing (KVO).
AddObjCKeyValueCompletions(ObjCPropertyDecl * Property,bool IsInstanceMethod,QualType ReturnType,ASTContext & Context,VisitedSelectorSet & KnownSelectors,ResultBuilder & Results)6173 static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property,
6174                                        bool IsInstanceMethod,
6175                                        QualType ReturnType,
6176                                        ASTContext &Context,
6177                                        VisitedSelectorSet &KnownSelectors,
6178                                        ResultBuilder &Results) {
6179   IdentifierInfo *PropName = Property->getIdentifier();
6180   if (!PropName || PropName->getLength() == 0)
6181     return;
6182 
6183   PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
6184 
6185   // Builder that will create each code completion.
6186   typedef CodeCompletionResult Result;
6187   CodeCompletionAllocator &Allocator = Results.getAllocator();
6188   CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
6189 
6190   // The selector table.
6191   SelectorTable &Selectors = Context.Selectors;
6192 
6193   // The property name, copied into the code completion allocation region
6194   // on demand.
6195   struct KeyHolder {
6196     CodeCompletionAllocator &Allocator;
6197     StringRef Key;
6198     const char *CopiedKey;
6199 
6200     KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key)
6201     : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {}
6202 
6203     operator const char *() {
6204       if (CopiedKey)
6205         return CopiedKey;
6206 
6207       return CopiedKey = Allocator.CopyString(Key);
6208     }
6209   } Key(Allocator, PropName->getName());
6210 
6211   // The uppercased name of the property name.
6212   std::string UpperKey = PropName->getName();
6213   if (!UpperKey.empty())
6214     UpperKey[0] = toUppercase(UpperKey[0]);
6215 
6216   bool ReturnTypeMatchesProperty = ReturnType.isNull() ||
6217     Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(),
6218                                    Property->getType());
6219   bool ReturnTypeMatchesVoid
6220     = ReturnType.isNull() || ReturnType->isVoidType();
6221 
6222   // Add the normal accessor -(type)key.
6223   if (IsInstanceMethod &&
6224       KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second &&
6225       ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) {
6226     if (ReturnType.isNull())
6227       AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6228                               Context, Policy, Builder);
6229 
6230     Builder.AddTypedTextChunk(Key);
6231     Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6232                              CXCursor_ObjCInstanceMethodDecl));
6233   }
6234 
6235   // If we have an integral or boolean property (or the user has provided
6236   // an integral or boolean return type), add the accessor -(type)isKey.
6237   if (IsInstanceMethod &&
6238       ((!ReturnType.isNull() &&
6239         (ReturnType->isIntegerType() || ReturnType->isBooleanType())) ||
6240        (ReturnType.isNull() &&
6241         (Property->getType()->isIntegerType() ||
6242          Property->getType()->isBooleanType())))) {
6243     std::string SelectorName = (Twine("is") + UpperKey).str();
6244     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6245     if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6246             .second) {
6247       if (ReturnType.isNull()) {
6248         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6249         Builder.AddTextChunk("BOOL");
6250         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6251       }
6252 
6253       Builder.AddTypedTextChunk(
6254                                 Allocator.CopyString(SelectorId->getName()));
6255       Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6256                                CXCursor_ObjCInstanceMethodDecl));
6257     }
6258   }
6259 
6260   // Add the normal mutator.
6261   if (IsInstanceMethod && ReturnTypeMatchesVoid &&
6262       !Property->getSetterMethodDecl()) {
6263     std::string SelectorName = (Twine("set") + UpperKey).str();
6264     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6265     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6266       if (ReturnType.isNull()) {
6267         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6268         Builder.AddTextChunk("void");
6269         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6270       }
6271 
6272       Builder.AddTypedTextChunk(
6273                                 Allocator.CopyString(SelectorId->getName()));
6274       Builder.AddTypedTextChunk(":");
6275       AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6276                               Context, Policy, Builder);
6277       Builder.AddTextChunk(Key);
6278       Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6279                                CXCursor_ObjCInstanceMethodDecl));
6280     }
6281   }
6282 
6283   // Indexed and unordered accessors
6284   unsigned IndexedGetterPriority = CCP_CodePattern;
6285   unsigned IndexedSetterPriority = CCP_CodePattern;
6286   unsigned UnorderedGetterPriority = CCP_CodePattern;
6287   unsigned UnorderedSetterPriority = CCP_CodePattern;
6288   if (const ObjCObjectPointerType *ObjCPointer
6289                     = Property->getType()->getAs<ObjCObjectPointerType>()) {
6290     if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) {
6291       // If this interface type is not provably derived from a known
6292       // collection, penalize the corresponding completions.
6293       if (!InheritsFromClassNamed(IFace, "NSMutableArray")) {
6294         IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6295         if (!InheritsFromClassNamed(IFace, "NSArray"))
6296           IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6297       }
6298 
6299       if (!InheritsFromClassNamed(IFace, "NSMutableSet")) {
6300         UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6301         if (!InheritsFromClassNamed(IFace, "NSSet"))
6302           UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6303       }
6304     }
6305   } else {
6306     IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6307     IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6308     UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6309     UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6310   }
6311 
6312   // Add -(NSUInteger)countOf<key>
6313   if (IsInstanceMethod &&
6314       (ReturnType.isNull() || ReturnType->isIntegerType())) {
6315     std::string SelectorName = (Twine("countOf") + UpperKey).str();
6316     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6317     if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6318             .second) {
6319       if (ReturnType.isNull()) {
6320         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6321         Builder.AddTextChunk("NSUInteger");
6322         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6323       }
6324 
6325       Builder.AddTypedTextChunk(
6326                                 Allocator.CopyString(SelectorId->getName()));
6327       Results.AddResult(Result(Builder.TakeString(),
6328                                std::min(IndexedGetterPriority,
6329                                         UnorderedGetterPriority),
6330                                CXCursor_ObjCInstanceMethodDecl));
6331     }
6332   }
6333 
6334   // Indexed getters
6335   // Add -(id)objectInKeyAtIndex:(NSUInteger)index
6336   if (IsInstanceMethod &&
6337       (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
6338     std::string SelectorName
6339       = (Twine("objectIn") + UpperKey + "AtIndex").str();
6340     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6341     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6342       if (ReturnType.isNull()) {
6343         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6344         Builder.AddTextChunk("id");
6345         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6346       }
6347 
6348       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6349       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6350       Builder.AddTextChunk("NSUInteger");
6351       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6352       Builder.AddTextChunk("index");
6353       Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6354                                CXCursor_ObjCInstanceMethodDecl));
6355     }
6356   }
6357 
6358   // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes
6359   if (IsInstanceMethod &&
6360       (ReturnType.isNull() ||
6361        (ReturnType->isObjCObjectPointerType() &&
6362         ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6363         ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6364                                                 ->getName() == "NSArray"))) {
6365     std::string SelectorName
6366       = (Twine(Property->getName()) + "AtIndexes").str();
6367     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6368     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6369       if (ReturnType.isNull()) {
6370         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6371         Builder.AddTextChunk("NSArray *");
6372         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6373       }
6374 
6375       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6376       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6377       Builder.AddTextChunk("NSIndexSet *");
6378       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6379       Builder.AddTextChunk("indexes");
6380       Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6381                                CXCursor_ObjCInstanceMethodDecl));
6382     }
6383   }
6384 
6385   // Add -(void)getKey:(type **)buffer range:(NSRange)inRange
6386   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6387     std::string SelectorName = (Twine("get") + UpperKey).str();
6388     IdentifierInfo *SelectorIds[2] = {
6389       &Context.Idents.get(SelectorName),
6390       &Context.Idents.get("range")
6391     };
6392 
6393     if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
6394       if (ReturnType.isNull()) {
6395         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6396         Builder.AddTextChunk("void");
6397         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6398       }
6399 
6400       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6401       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6402       Builder.AddPlaceholderChunk("object-type");
6403       Builder.AddTextChunk(" **");
6404       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6405       Builder.AddTextChunk("buffer");
6406       Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6407       Builder.AddTypedTextChunk("range:");
6408       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6409       Builder.AddTextChunk("NSRange");
6410       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6411       Builder.AddTextChunk("inRange");
6412       Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6413                                CXCursor_ObjCInstanceMethodDecl));
6414     }
6415   }
6416 
6417   // Mutable indexed accessors
6418 
6419   // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index
6420   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6421     std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str();
6422     IdentifierInfo *SelectorIds[2] = {
6423       &Context.Idents.get("insertObject"),
6424       &Context.Idents.get(SelectorName)
6425     };
6426 
6427     if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
6428       if (ReturnType.isNull()) {
6429         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6430         Builder.AddTextChunk("void");
6431         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6432       }
6433 
6434       Builder.AddTypedTextChunk("insertObject:");
6435       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6436       Builder.AddPlaceholderChunk("object-type");
6437       Builder.AddTextChunk(" *");
6438       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6439       Builder.AddTextChunk("object");
6440       Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6441       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6442       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6443       Builder.AddPlaceholderChunk("NSUInteger");
6444       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6445       Builder.AddTextChunk("index");
6446       Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6447                                CXCursor_ObjCInstanceMethodDecl));
6448     }
6449   }
6450 
6451   // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes
6452   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6453     std::string SelectorName = (Twine("insert") + UpperKey).str();
6454     IdentifierInfo *SelectorIds[2] = {
6455       &Context.Idents.get(SelectorName),
6456       &Context.Idents.get("atIndexes")
6457     };
6458 
6459     if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
6460       if (ReturnType.isNull()) {
6461         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6462         Builder.AddTextChunk("void");
6463         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6464       }
6465 
6466       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6467       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6468       Builder.AddTextChunk("NSArray *");
6469       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6470       Builder.AddTextChunk("array");
6471       Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6472       Builder.AddTypedTextChunk("atIndexes:");
6473       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6474       Builder.AddPlaceholderChunk("NSIndexSet *");
6475       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6476       Builder.AddTextChunk("indexes");
6477       Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6478                                CXCursor_ObjCInstanceMethodDecl));
6479     }
6480   }
6481 
6482   // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index
6483   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6484     std::string SelectorName
6485       = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str();
6486     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6487     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6488       if (ReturnType.isNull()) {
6489         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6490         Builder.AddTextChunk("void");
6491         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6492       }
6493 
6494       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6495       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6496       Builder.AddTextChunk("NSUInteger");
6497       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6498       Builder.AddTextChunk("index");
6499       Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6500                                CXCursor_ObjCInstanceMethodDecl));
6501     }
6502   }
6503 
6504   // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes
6505   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6506     std::string SelectorName
6507       = (Twine("remove") + UpperKey + "AtIndexes").str();
6508     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6509     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6510       if (ReturnType.isNull()) {
6511         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6512         Builder.AddTextChunk("void");
6513         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6514       }
6515 
6516       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6517       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6518       Builder.AddTextChunk("NSIndexSet *");
6519       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6520       Builder.AddTextChunk("indexes");
6521       Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6522                                CXCursor_ObjCInstanceMethodDecl));
6523     }
6524   }
6525 
6526   // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object
6527   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6528     std::string SelectorName
6529       = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str();
6530     IdentifierInfo *SelectorIds[2] = {
6531       &Context.Idents.get(SelectorName),
6532       &Context.Idents.get("withObject")
6533     };
6534 
6535     if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
6536       if (ReturnType.isNull()) {
6537         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6538         Builder.AddTextChunk("void");
6539         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6540       }
6541 
6542       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6543       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6544       Builder.AddPlaceholderChunk("NSUInteger");
6545       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6546       Builder.AddTextChunk("index");
6547       Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6548       Builder.AddTypedTextChunk("withObject:");
6549       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6550       Builder.AddTextChunk("id");
6551       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6552       Builder.AddTextChunk("object");
6553       Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6554                                CXCursor_ObjCInstanceMethodDecl));
6555     }
6556   }
6557 
6558   // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array
6559   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6560     std::string SelectorName1
6561       = (Twine("replace") + UpperKey + "AtIndexes").str();
6562     std::string SelectorName2 = (Twine("with") + UpperKey).str();
6563     IdentifierInfo *SelectorIds[2] = {
6564       &Context.Idents.get(SelectorName1),
6565       &Context.Idents.get(SelectorName2)
6566     };
6567 
6568     if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
6569       if (ReturnType.isNull()) {
6570         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6571         Builder.AddTextChunk("void");
6572         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6573       }
6574 
6575       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":"));
6576       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6577       Builder.AddPlaceholderChunk("NSIndexSet *");
6578       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6579       Builder.AddTextChunk("indexes");
6580       Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6581       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":"));
6582       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6583       Builder.AddTextChunk("NSArray *");
6584       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6585       Builder.AddTextChunk("array");
6586       Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6587                                CXCursor_ObjCInstanceMethodDecl));
6588     }
6589   }
6590 
6591   // Unordered getters
6592   // - (NSEnumerator *)enumeratorOfKey
6593   if (IsInstanceMethod &&
6594       (ReturnType.isNull() ||
6595        (ReturnType->isObjCObjectPointerType() &&
6596         ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6597         ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6598           ->getName() == "NSEnumerator"))) {
6599     std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str();
6600     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6601     if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6602             .second) {
6603       if (ReturnType.isNull()) {
6604         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6605         Builder.AddTextChunk("NSEnumerator *");
6606         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6607       }
6608 
6609       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6610       Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6611                               CXCursor_ObjCInstanceMethodDecl));
6612     }
6613   }
6614 
6615   // - (type *)memberOfKey:(type *)object
6616   if (IsInstanceMethod &&
6617       (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
6618     std::string SelectorName = (Twine("memberOf") + UpperKey).str();
6619     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6620     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6621       if (ReturnType.isNull()) {
6622         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6623         Builder.AddPlaceholderChunk("object-type");
6624         Builder.AddTextChunk(" *");
6625         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6626       }
6627 
6628       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6629       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6630       if (ReturnType.isNull()) {
6631         Builder.AddPlaceholderChunk("object-type");
6632         Builder.AddTextChunk(" *");
6633       } else {
6634         Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context,
6635                                                      Policy,
6636                                                      Builder.getAllocator()));
6637       }
6638       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6639       Builder.AddTextChunk("object");
6640       Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6641                                CXCursor_ObjCInstanceMethodDecl));
6642     }
6643   }
6644 
6645   // Mutable unordered accessors
6646   // - (void)addKeyObject:(type *)object
6647   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6648     std::string SelectorName
6649       = (Twine("add") + UpperKey + Twine("Object")).str();
6650     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6651     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6652       if (ReturnType.isNull()) {
6653         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6654         Builder.AddTextChunk("void");
6655         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6656       }
6657 
6658       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6659       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6660       Builder.AddPlaceholderChunk("object-type");
6661       Builder.AddTextChunk(" *");
6662       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6663       Builder.AddTextChunk("object");
6664       Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6665                                CXCursor_ObjCInstanceMethodDecl));
6666     }
6667   }
6668 
6669   // - (void)addKey:(NSSet *)objects
6670   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6671     std::string SelectorName = (Twine("add") + UpperKey).str();
6672     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6673     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6674       if (ReturnType.isNull()) {
6675         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6676         Builder.AddTextChunk("void");
6677         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6678       }
6679 
6680       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6681       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6682       Builder.AddTextChunk("NSSet *");
6683       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6684       Builder.AddTextChunk("objects");
6685       Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6686                                CXCursor_ObjCInstanceMethodDecl));
6687     }
6688   }
6689 
6690   // - (void)removeKeyObject:(type *)object
6691   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6692     std::string SelectorName
6693       = (Twine("remove") + UpperKey + Twine("Object")).str();
6694     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6695     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6696       if (ReturnType.isNull()) {
6697         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6698         Builder.AddTextChunk("void");
6699         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6700       }
6701 
6702       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6703       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6704       Builder.AddPlaceholderChunk("object-type");
6705       Builder.AddTextChunk(" *");
6706       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6707       Builder.AddTextChunk("object");
6708       Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6709                                CXCursor_ObjCInstanceMethodDecl));
6710     }
6711   }
6712 
6713   // - (void)removeKey:(NSSet *)objects
6714   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6715     std::string SelectorName = (Twine("remove") + UpperKey).str();
6716     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6717     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6718       if (ReturnType.isNull()) {
6719         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6720         Builder.AddTextChunk("void");
6721         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6722       }
6723 
6724       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6725       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6726       Builder.AddTextChunk("NSSet *");
6727       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6728       Builder.AddTextChunk("objects");
6729       Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6730                                CXCursor_ObjCInstanceMethodDecl));
6731     }
6732   }
6733 
6734   // - (void)intersectKey:(NSSet *)objects
6735   if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6736     std::string SelectorName = (Twine("intersect") + UpperKey).str();
6737     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6738     if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6739       if (ReturnType.isNull()) {
6740         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6741         Builder.AddTextChunk("void");
6742         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6743       }
6744 
6745       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6746       Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6747       Builder.AddTextChunk("NSSet *");
6748       Builder.AddChunk(CodeCompletionString::CK_RightParen);
6749       Builder.AddTextChunk("objects");
6750       Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6751                                CXCursor_ObjCInstanceMethodDecl));
6752     }
6753   }
6754 
6755   // Key-Value Observing
6756   // + (NSSet *)keyPathsForValuesAffectingKey
6757   if (!IsInstanceMethod &&
6758       (ReturnType.isNull() ||
6759        (ReturnType->isObjCObjectPointerType() &&
6760         ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6761         ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6762                                                     ->getName() == "NSSet"))) {
6763     std::string SelectorName
6764       = (Twine("keyPathsForValuesAffecting") + UpperKey).str();
6765     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6766     if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6767             .second) {
6768       if (ReturnType.isNull()) {
6769         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6770         Builder.AddTextChunk("NSSet *");
6771         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6772       }
6773 
6774       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6775       Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6776                               CXCursor_ObjCClassMethodDecl));
6777     }
6778   }
6779 
6780   // + (BOOL)automaticallyNotifiesObserversForKey
6781   if (!IsInstanceMethod &&
6782       (ReturnType.isNull() ||
6783        ReturnType->isIntegerType() ||
6784        ReturnType->isBooleanType())) {
6785     std::string SelectorName
6786       = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str();
6787     IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6788     if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6789             .second) {
6790       if (ReturnType.isNull()) {
6791         Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6792         Builder.AddTextChunk("BOOL");
6793         Builder.AddChunk(CodeCompletionString::CK_RightParen);
6794       }
6795 
6796       Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6797       Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6798                               CXCursor_ObjCClassMethodDecl));
6799     }
6800   }
6801 }
6802 
CodeCompleteObjCMethodDecl(Scope * S,bool IsInstanceMethod,ParsedType ReturnTy)6803 void Sema::CodeCompleteObjCMethodDecl(Scope *S,
6804                                       bool IsInstanceMethod,
6805                                       ParsedType ReturnTy) {
6806   // Determine the return type of the method we're declaring, if
6807   // provided.
6808   QualType ReturnType = GetTypeFromParser(ReturnTy);
6809   Decl *IDecl = nullptr;
6810   if (CurContext->isObjCContainer()) {
6811       ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
6812       IDecl = cast<Decl>(OCD);
6813   }
6814   // Determine where we should start searching for methods.
6815   ObjCContainerDecl *SearchDecl = nullptr;
6816   bool IsInImplementation = false;
6817   if (Decl *D = IDecl) {
6818     if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) {
6819       SearchDecl = Impl->getClassInterface();
6820       IsInImplementation = true;
6821     } else if (ObjCCategoryImplDecl *CatImpl
6822                                          = dyn_cast<ObjCCategoryImplDecl>(D)) {
6823       SearchDecl = CatImpl->getCategoryDecl();
6824       IsInImplementation = true;
6825     } else
6826       SearchDecl = dyn_cast<ObjCContainerDecl>(D);
6827   }
6828 
6829   if (!SearchDecl && S) {
6830     if (DeclContext *DC = S->getEntity())
6831       SearchDecl = dyn_cast<ObjCContainerDecl>(DC);
6832   }
6833 
6834   if (!SearchDecl) {
6835     HandleCodeCompleteResults(this, CodeCompleter,
6836                               CodeCompletionContext::CCC_Other,
6837                               nullptr, 0);
6838     return;
6839   }
6840 
6841   // Find all of the methods that we could declare/implement here.
6842   KnownMethodsMap KnownMethods;
6843   FindImplementableMethods(Context, SearchDecl, IsInstanceMethod,
6844                            ReturnType, KnownMethods);
6845 
6846   // Add declarations or definitions for each of the known methods.
6847   typedef CodeCompletionResult Result;
6848   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
6849                         CodeCompleter->getCodeCompletionTUInfo(),
6850                         CodeCompletionContext::CCC_Other);
6851   Results.EnterNewScope();
6852   PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
6853   for (KnownMethodsMap::iterator M = KnownMethods.begin(),
6854                               MEnd = KnownMethods.end();
6855        M != MEnd; ++M) {
6856     ObjCMethodDecl *Method = M->second.getPointer();
6857     CodeCompletionBuilder Builder(Results.getAllocator(),
6858                                   Results.getCodeCompletionTUInfo());
6859 
6860     // If the result type was not already provided, add it to the
6861     // pattern as (type).
6862     if (ReturnType.isNull())
6863       AddObjCPassingTypeChunk(Method->getReturnType(),
6864                               Method->getObjCDeclQualifier(), Context, Policy,
6865                               Builder);
6866 
6867     Selector Sel = Method->getSelector();
6868 
6869     // Add the first part of the selector to the pattern.
6870     Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
6871                                                        Sel.getNameForSlot(0)));
6872 
6873     // Add parameters to the pattern.
6874     unsigned I = 0;
6875     for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
6876                                      PEnd = Method->param_end();
6877          P != PEnd; (void)++P, ++I) {
6878       // Add the part of the selector name.
6879       if (I == 0)
6880         Builder.AddTypedTextChunk(":");
6881       else if (I < Sel.getNumArgs()) {
6882         Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6883         Builder.AddTypedTextChunk(
6884                 Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":"));
6885       } else
6886         break;
6887 
6888       // Add the parameter type.
6889       AddObjCPassingTypeChunk((*P)->getOriginalType(),
6890                               (*P)->getObjCDeclQualifier(),
6891                               Context, Policy,
6892                               Builder);
6893 
6894       if (IdentifierInfo *Id = (*P)->getIdentifier())
6895         Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName()));
6896     }
6897 
6898     if (Method->isVariadic()) {
6899       if (Method->param_size() > 0)
6900         Builder.AddChunk(CodeCompletionString::CK_Comma);
6901       Builder.AddTextChunk("...");
6902     }
6903 
6904     if (IsInImplementation && Results.includeCodePatterns()) {
6905       // We will be defining the method here, so add a compound statement.
6906       Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6907       Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
6908       Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
6909       if (!Method->getReturnType()->isVoidType()) {
6910         // If the result type is not void, add a return clause.
6911         Builder.AddTextChunk("return");
6912         Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6913         Builder.AddPlaceholderChunk("expression");
6914         Builder.AddChunk(CodeCompletionString::CK_SemiColon);
6915       } else
6916         Builder.AddPlaceholderChunk("statements");
6917 
6918       Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
6919       Builder.AddChunk(CodeCompletionString::CK_RightBrace);
6920     }
6921 
6922     unsigned Priority = CCP_CodePattern;
6923     if (!M->second.getInt())
6924       Priority += CCD_InBaseClass;
6925 
6926     Results.AddResult(Result(Builder.TakeString(), Method, Priority));
6927   }
6928 
6929   // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of
6930   // the properties in this class and its categories.
6931   if (Context.getLangOpts().ObjC2) {
6932     SmallVector<ObjCContainerDecl *, 4> Containers;
6933     Containers.push_back(SearchDecl);
6934 
6935     VisitedSelectorSet KnownSelectors;
6936     for (KnownMethodsMap::iterator M = KnownMethods.begin(),
6937                                 MEnd = KnownMethods.end();
6938          M != MEnd; ++M)
6939       KnownSelectors.insert(M->first);
6940 
6941 
6942     ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl);
6943     if (!IFace)
6944       if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl))
6945         IFace = Category->getClassInterface();
6946 
6947     if (IFace)
6948       for (auto *Cat : IFace->visible_categories())
6949         Containers.push_back(Cat);
6950 
6951     for (unsigned I = 0, N = Containers.size(); I != N; ++I)
6952       for (auto *P : Containers[I]->properties())
6953         AddObjCKeyValueCompletions(P, IsInstanceMethod, ReturnType, Context,
6954                                    KnownSelectors, Results);
6955   }
6956 
6957   Results.ExitScope();
6958 
6959   HandleCodeCompleteResults(this, CodeCompleter,
6960                             CodeCompletionContext::CCC_Other,
6961                             Results.data(),Results.size());
6962 }
6963 
CodeCompleteObjCMethodDeclSelector(Scope * S,bool IsInstanceMethod,bool AtParameterName,ParsedType ReturnTy,ArrayRef<IdentifierInfo * > SelIdents)6964 void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S,
6965                                               bool IsInstanceMethod,
6966                                               bool AtParameterName,
6967                                               ParsedType ReturnTy,
6968                                          ArrayRef<IdentifierInfo *> SelIdents) {
6969   // If we have an external source, load the entire class method
6970   // pool from the AST file.
6971   if (ExternalSource) {
6972     for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
6973          I != N; ++I) {
6974       Selector Sel = ExternalSource->GetExternalSelector(I);
6975       if (Sel.isNull() || MethodPool.count(Sel))
6976         continue;
6977 
6978       ReadMethodPool(Sel);
6979     }
6980   }
6981 
6982   // Build the set of methods we can see.
6983   typedef CodeCompletionResult Result;
6984   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
6985                         CodeCompleter->getCodeCompletionTUInfo(),
6986                         CodeCompletionContext::CCC_Other);
6987 
6988   if (ReturnTy)
6989     Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType());
6990 
6991   Results.EnterNewScope();
6992   for (GlobalMethodPool::iterator M = MethodPool.begin(),
6993                                   MEnd = MethodPool.end();
6994        M != MEnd; ++M) {
6995     for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first :
6996                                                        &M->second.second;
6997          MethList && MethList->getMethod();
6998          MethList = MethList->getNext()) {
6999       if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
7000         continue;
7001 
7002       if (AtParameterName) {
7003         // Suggest parameter names we've seen before.
7004         unsigned NumSelIdents = SelIdents.size();
7005         if (NumSelIdents &&
7006             NumSelIdents <= MethList->getMethod()->param_size()) {
7007           ParmVarDecl *Param =
7008               MethList->getMethod()->parameters()[NumSelIdents - 1];
7009           if (Param->getIdentifier()) {
7010             CodeCompletionBuilder Builder(Results.getAllocator(),
7011                                           Results.getCodeCompletionTUInfo());
7012             Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
7013                                            Param->getIdentifier()->getName()));
7014             Results.AddResult(Builder.TakeString());
7015           }
7016         }
7017 
7018         continue;
7019       }
7020 
7021       Result R(MethList->getMethod(),
7022                Results.getBasePriority(MethList->getMethod()), nullptr);
7023       R.StartParameter = SelIdents.size();
7024       R.AllParametersAreInformative = false;
7025       R.DeclaringEntity = true;
7026       Results.MaybeAddResult(R, CurContext);
7027     }
7028   }
7029 
7030   Results.ExitScope();
7031   HandleCodeCompleteResults(this, CodeCompleter,
7032                             CodeCompletionContext::CCC_Other,
7033                             Results.data(),Results.size());
7034 }
7035 
CodeCompletePreprocessorDirective(bool InConditional)7036 void Sema::CodeCompletePreprocessorDirective(bool InConditional) {
7037   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
7038                         CodeCompleter->getCodeCompletionTUInfo(),
7039                         CodeCompletionContext::CCC_PreprocessorDirective);
7040   Results.EnterNewScope();
7041 
7042   // #if <condition>
7043   CodeCompletionBuilder Builder(Results.getAllocator(),
7044                                 Results.getCodeCompletionTUInfo());
7045   Builder.AddTypedTextChunk("if");
7046   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7047   Builder.AddPlaceholderChunk("condition");
7048   Results.AddResult(Builder.TakeString());
7049 
7050   // #ifdef <macro>
7051   Builder.AddTypedTextChunk("ifdef");
7052   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7053   Builder.AddPlaceholderChunk("macro");
7054   Results.AddResult(Builder.TakeString());
7055 
7056   // #ifndef <macro>
7057   Builder.AddTypedTextChunk("ifndef");
7058   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7059   Builder.AddPlaceholderChunk("macro");
7060   Results.AddResult(Builder.TakeString());
7061 
7062   if (InConditional) {
7063     // #elif <condition>
7064     Builder.AddTypedTextChunk("elif");
7065     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7066     Builder.AddPlaceholderChunk("condition");
7067     Results.AddResult(Builder.TakeString());
7068 
7069     // #else
7070     Builder.AddTypedTextChunk("else");
7071     Results.AddResult(Builder.TakeString());
7072 
7073     // #endif
7074     Builder.AddTypedTextChunk("endif");
7075     Results.AddResult(Builder.TakeString());
7076   }
7077 
7078   // #include "header"
7079   Builder.AddTypedTextChunk("include");
7080   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7081   Builder.AddTextChunk("\"");
7082   Builder.AddPlaceholderChunk("header");
7083   Builder.AddTextChunk("\"");
7084   Results.AddResult(Builder.TakeString());
7085 
7086   // #include <header>
7087   Builder.AddTypedTextChunk("include");
7088   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7089   Builder.AddTextChunk("<");
7090   Builder.AddPlaceholderChunk("header");
7091   Builder.AddTextChunk(">");
7092   Results.AddResult(Builder.TakeString());
7093 
7094   // #define <macro>
7095   Builder.AddTypedTextChunk("define");
7096   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7097   Builder.AddPlaceholderChunk("macro");
7098   Results.AddResult(Builder.TakeString());
7099 
7100   // #define <macro>(<args>)
7101   Builder.AddTypedTextChunk("define");
7102   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7103   Builder.AddPlaceholderChunk("macro");
7104   Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7105   Builder.AddPlaceholderChunk("args");
7106   Builder.AddChunk(CodeCompletionString::CK_RightParen);
7107   Results.AddResult(Builder.TakeString());
7108 
7109   // #undef <macro>
7110   Builder.AddTypedTextChunk("undef");
7111   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7112   Builder.AddPlaceholderChunk("macro");
7113   Results.AddResult(Builder.TakeString());
7114 
7115   // #line <number>
7116   Builder.AddTypedTextChunk("line");
7117   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7118   Builder.AddPlaceholderChunk("number");
7119   Results.AddResult(Builder.TakeString());
7120 
7121   // #line <number> "filename"
7122   Builder.AddTypedTextChunk("line");
7123   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7124   Builder.AddPlaceholderChunk("number");
7125   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7126   Builder.AddTextChunk("\"");
7127   Builder.AddPlaceholderChunk("filename");
7128   Builder.AddTextChunk("\"");
7129   Results.AddResult(Builder.TakeString());
7130 
7131   // #error <message>
7132   Builder.AddTypedTextChunk("error");
7133   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7134   Builder.AddPlaceholderChunk("message");
7135   Results.AddResult(Builder.TakeString());
7136 
7137   // #pragma <arguments>
7138   Builder.AddTypedTextChunk("pragma");
7139   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7140   Builder.AddPlaceholderChunk("arguments");
7141   Results.AddResult(Builder.TakeString());
7142 
7143   if (getLangOpts().ObjC1) {
7144     // #import "header"
7145     Builder.AddTypedTextChunk("import");
7146     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7147     Builder.AddTextChunk("\"");
7148     Builder.AddPlaceholderChunk("header");
7149     Builder.AddTextChunk("\"");
7150     Results.AddResult(Builder.TakeString());
7151 
7152     // #import <header>
7153     Builder.AddTypedTextChunk("import");
7154     Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7155     Builder.AddTextChunk("<");
7156     Builder.AddPlaceholderChunk("header");
7157     Builder.AddTextChunk(">");
7158     Results.AddResult(Builder.TakeString());
7159   }
7160 
7161   // #include_next "header"
7162   Builder.AddTypedTextChunk("include_next");
7163   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7164   Builder.AddTextChunk("\"");
7165   Builder.AddPlaceholderChunk("header");
7166   Builder.AddTextChunk("\"");
7167   Results.AddResult(Builder.TakeString());
7168 
7169   // #include_next <header>
7170   Builder.AddTypedTextChunk("include_next");
7171   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7172   Builder.AddTextChunk("<");
7173   Builder.AddPlaceholderChunk("header");
7174   Builder.AddTextChunk(">");
7175   Results.AddResult(Builder.TakeString());
7176 
7177   // #warning <message>
7178   Builder.AddTypedTextChunk("warning");
7179   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7180   Builder.AddPlaceholderChunk("message");
7181   Results.AddResult(Builder.TakeString());
7182 
7183   // Note: #ident and #sccs are such crazy anachronisms that we don't provide
7184   // completions for them. And __include_macros is a Clang-internal extension
7185   // that we don't want to encourage anyone to use.
7186 
7187   // FIXME: we don't support #assert or #unassert, so don't suggest them.
7188   Results.ExitScope();
7189 
7190   HandleCodeCompleteResults(this, CodeCompleter,
7191                             CodeCompletionContext::CCC_PreprocessorDirective,
7192                             Results.data(), Results.size());
7193 }
7194 
CodeCompleteInPreprocessorConditionalExclusion(Scope * S)7195 void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) {
7196   CodeCompleteOrdinaryName(S,
7197                            S->getFnParent()? Sema::PCC_RecoveryInFunction
7198                                            : Sema::PCC_Namespace);
7199 }
7200 
CodeCompletePreprocessorMacroName(bool IsDefinition)7201 void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) {
7202   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
7203                         CodeCompleter->getCodeCompletionTUInfo(),
7204                         IsDefinition? CodeCompletionContext::CCC_MacroName
7205                                     : CodeCompletionContext::CCC_MacroNameUse);
7206   if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) {
7207     // Add just the names of macros, not their arguments.
7208     CodeCompletionBuilder Builder(Results.getAllocator(),
7209                                   Results.getCodeCompletionTUInfo());
7210     Results.EnterNewScope();
7211     for (Preprocessor::macro_iterator M = PP.macro_begin(),
7212                                    MEnd = PP.macro_end();
7213          M != MEnd; ++M) {
7214       Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
7215                                            M->first->getName()));
7216       Results.AddResult(CodeCompletionResult(Builder.TakeString(),
7217                                              CCP_CodePattern,
7218                                              CXCursor_MacroDefinition));
7219     }
7220     Results.ExitScope();
7221   } else if (IsDefinition) {
7222     // FIXME: Can we detect when the user just wrote an include guard above?
7223   }
7224 
7225   HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
7226                             Results.data(), Results.size());
7227 }
7228 
CodeCompletePreprocessorExpression()7229 void Sema::CodeCompletePreprocessorExpression() {
7230   ResultBuilder Results(*this, CodeCompleter->getAllocator(),
7231                         CodeCompleter->getCodeCompletionTUInfo(),
7232                         CodeCompletionContext::CCC_PreprocessorExpression);
7233 
7234   if (!CodeCompleter || CodeCompleter->includeMacros())
7235     AddMacroResults(PP, Results, true);
7236 
7237     // defined (<macro>)
7238   Results.EnterNewScope();
7239   CodeCompletionBuilder Builder(Results.getAllocator(),
7240                                 Results.getCodeCompletionTUInfo());
7241   Builder.AddTypedTextChunk("defined");
7242   Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7243   Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7244   Builder.AddPlaceholderChunk("macro");
7245   Builder.AddChunk(CodeCompletionString::CK_RightParen);
7246   Results.AddResult(Builder.TakeString());
7247   Results.ExitScope();
7248 
7249   HandleCodeCompleteResults(this, CodeCompleter,
7250                             CodeCompletionContext::CCC_PreprocessorExpression,
7251                             Results.data(), Results.size());
7252 }
7253 
CodeCompletePreprocessorMacroArgument(Scope * S,IdentifierInfo * Macro,MacroInfo * MacroInfo,unsigned Argument)7254 void Sema::CodeCompletePreprocessorMacroArgument(Scope *S,
7255                                                  IdentifierInfo *Macro,
7256                                                  MacroInfo *MacroInfo,
7257                                                  unsigned Argument) {
7258   // FIXME: In the future, we could provide "overload" results, much like we
7259   // do for function calls.
7260 
7261   // Now just ignore this. There will be another code-completion callback
7262   // for the expanded tokens.
7263 }
7264 
CodeCompleteNaturalLanguage()7265 void Sema::CodeCompleteNaturalLanguage() {
7266   HandleCodeCompleteResults(this, CodeCompleter,
7267                             CodeCompletionContext::CCC_NaturalLanguage,
7268                             nullptr, 0);
7269 }
7270 
GatherGlobalCodeCompletions(CodeCompletionAllocator & Allocator,CodeCompletionTUInfo & CCTUInfo,SmallVectorImpl<CodeCompletionResult> & Results)7271 void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator,
7272                                        CodeCompletionTUInfo &CCTUInfo,
7273                  SmallVectorImpl<CodeCompletionResult> &Results) {
7274   ResultBuilder Builder(*this, Allocator, CCTUInfo,
7275                         CodeCompletionContext::CCC_Recovery);
7276   if (!CodeCompleter || CodeCompleter->includeGlobals()) {
7277     CodeCompletionDeclConsumer Consumer(Builder,
7278                                         Context.getTranslationUnitDecl());
7279     LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName,
7280                        Consumer);
7281   }
7282 
7283   if (!CodeCompleter || CodeCompleter->includeMacros())
7284     AddMacroResults(PP, Builder, true);
7285 
7286   Results.clear();
7287   Results.insert(Results.end(),
7288                  Builder.data(), Builder.data() + Builder.size());
7289 }
7290