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