1 //===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the code-completion semantic actions.
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"
38 using namespace clang;
42 /// \brief A container of code-completion results.
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;
51 typedef CodeCompletionResult Result;
54 /// \brief The actual results we have found.
55 std::vector<Result> Results;
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;
62 typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair;
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;
70 /// \brief Contains either the solitary NamedDecl * or a vector
71 /// of (declaration, index) pairs.
72 llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector*> DeclOrVector;
74 /// \brief When the entry contains a single declaration, this is
75 /// the index associated with that entry.
76 unsigned SingleDeclIndex;
79 ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { }
81 void Add(const NamedDecl *ND, unsigned Index) {
82 if (DeclOrVector.isNull()) {
83 // 0 - > 1 elements: just set the single element information.
85 SingleDeclIndex = Index;
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));
98 // Add the new element to the end of the vector.
99 DeclOrVector.get<DeclIndexPairVector*>()->push_back(
100 DeclIndexPair(ND, Index));
104 if (DeclIndexPairVector *Vec
105 = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) {
107 DeclOrVector = ((NamedDecl *)nullptr);
113 iterator begin() const;
114 iterator end() const;
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
120 typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
122 /// \brief The semantic analysis object for which results are being
126 /// \brief The allocator used to allocate new code-completion strings.
127 CodeCompletionAllocator &Allocator;
129 CodeCompletionTUInfo &CCTUInfo;
131 /// \brief If non-NULL, a filter function used to remove any code-completion
132 /// results that are not desirable.
135 /// \brief Whether we should allow declarations as
136 /// nested-name-specifiers that would otherwise be filtered out.
137 bool AllowNestedNameSpecifiers;
139 /// \brief If set, the type that we would prefer our resulting value
140 /// declarations to have.
142 /// Closely matching the preferred type gives a boost to a result's
144 CanQualType PreferredType;
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;
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;
154 /// \brief Whether the \p ObjectTypeQualifiers field is active.
155 bool HasObjectTypeQualifiers;
157 /// \brief The selector that we prefer.
158 Selector PreferredSelector;
160 /// \brief The completion context in which we are gathering results.
161 CodeCompletionContext CompletionContext;
163 /// \brief If we are in an instance method definition, the \@implementation
165 ObjCImplementationDecl *ObjCImplementation;
167 void AdjustResultPriorityForDecl(Result &R);
169 void MaybeAddConstructorResults(Result R);
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),
178 AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false),
179 CompletionContext(CompletionContext),
180 ObjCImplementation(nullptr)
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();
201 /// \brief Determine the priority for a reference to the given declaration.
202 unsigned getBasePriority(const NamedDecl *D);
204 /// \brief Whether we should include code patterns in the completion
206 bool includeCodePatterns() const {
207 return SemaRef.CodeCompleter &&
208 SemaRef.CodeCompleter->includeCodePatterns();
211 /// \brief Set the filter used for code-completion results.
212 void setFilter(LookupFilter Filter) {
213 this->Filter = Filter;
216 Result *data() { return Results.empty()? nullptr : &Results.front(); }
217 unsigned size() const { return Results.size(); }
218 bool empty() const { return Results.empty(); }
220 /// \brief Specify the preferred type.
221 void setPreferredType(QualType T) {
222 PreferredType = SemaRef.Context.getCanonicalType(T);
225 /// \brief Set the cv-qualifiers on the object type, for us in filtering
226 /// calls to member functions.
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
232 void setObjectTypeQualifiers(Qualifiers Quals) {
233 ObjectTypeQualifiers = Quals;
234 HasObjectTypeQualifiers = true;
237 /// \brief Set the preferred selector.
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;
246 /// \brief Retrieve the code-completion context for which results are
248 const CodeCompletionContext &getCompletionContext() const {
249 return CompletionContext;
252 /// \brief Specify whether nested-name-specifiers are allowed.
253 void allowNestedNameSpecifiers(bool Allow = true) {
254 AllowNestedNameSpecifiers = Allow;
257 /// \brief Return the semantic analysis object for which we are collecting
258 /// code completion results.
259 Sema &getSema() const { return SemaRef; }
261 /// \brief Retrieve the allocator used to allocate code completion strings.
262 CodeCompletionAllocator &getAllocator() const { return Allocator; }
264 CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
266 /// \brief Determine whether the given declaration is at all interesting
267 /// as a code-completion result.
269 /// \param ND the declaration that we are inspecting.
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;
276 /// \brief Check whether the result is hidden by the Hiding declaration.
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
282 bool CheckHiddenResult(Result &R, DeclContext *CurContext,
283 const NamedDecl *Hiding);
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
289 /// \param R the result to add (if it is unique).
291 /// \param CurContext the context in which this result will be named.
292 void MaybeAddResult(Result R, DeclContext *CurContext = nullptr);
294 /// \brief Add a new result to this result set, where we already know
295 /// the hiding declaration (if any).
297 /// \param R the result to add (if it is unique).
299 /// \param CurContext the context in which this result will be named.
301 /// \param Hiding the declaration that hides the result.
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,
308 /// \brief Add a new non-declaration result to this result set.
309 void AddResult(Result R);
311 /// \brief Enter into a new scope.
312 void EnterNewScope();
314 /// \brief Exit from the current scope.
317 /// \brief Ignore this declaration, if it is seen again.
318 void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); }
320 /// \name Name lookup predicates
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
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;
347 class ResultBuilder::ShadowMapEntry::iterator {
348 llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator;
349 unsigned SingleDeclIndex;
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;
361 pointer(const DeclIndexPair &Value) : Value(Value) { }
363 const DeclIndexPair *operator->() const {
368 iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {}
370 iterator(const NamedDecl *SingleDecl, unsigned Index)
371 : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { }
373 iterator(const DeclIndexPair *Iterator)
374 : DeclOrIterator(Iterator), SingleDeclIndex(0) { }
376 iterator &operator++() {
377 if (DeclOrIterator.is<const NamedDecl *>()) {
378 DeclOrIterator = (NamedDecl *)nullptr;
383 const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>();
389 /*iterator operator++(int) {
395 reference operator*() const {
396 if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>())
397 return reference(ND, SingleDeclIndex);
399 return *DeclOrIterator.get<const DeclIndexPair*>();
402 pointer operator->() const {
403 return pointer(**this);
406 friend bool operator==(const iterator &X, const iterator &Y) {
407 return X.DeclOrIterator.getOpaqueValue()
408 == Y.DeclOrIterator.getOpaqueValue() &&
409 X.SingleDeclIndex == Y.SingleDeclIndex;
412 friend bool operator!=(const iterator &X, const iterator &Y) {
417 ResultBuilder::ShadowMapEntry::iterator
418 ResultBuilder::ShadowMapEntry::begin() const {
419 if (DeclOrVector.isNull())
422 if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>())
423 return iterator(ND, SingleDeclIndex);
425 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin());
428 ResultBuilder::ShadowMapEntry::iterator
429 ResultBuilder::ShadowMapEntry::end() const {
430 if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull())
433 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end());
436 /// \brief Compute the qualification required to get from the current context
437 /// (\p CurContext) to the target context (\p TargetContext).
439 /// \param Context the AST context in which the qualification will be used.
441 /// \param CurContext the context where an entity is being named, which is
442 /// typically based on the current scope.
444 /// \param TargetContext the context in which the named entity actually
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;
455 for (const DeclContext *CommonAncestor = TargetContext;
456 CommonAncestor && !CommonAncestor->Encloses(CurContext);
457 CommonAncestor = CommonAncestor->getLookupParent()) {
458 if (CommonAncestor->isTransparentContext() ||
459 CommonAncestor->isFunctionOrMethod())
462 TargetParents.push_back(CommonAncestor);
465 NestedNameSpecifier *Result = nullptr;
466 while (!TargetParents.empty()) {
467 const DeclContext *Parent = TargetParents.pop_back_val();
469 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) {
470 if (!Namespace->getIdentifier())
473 Result = NestedNameSpecifier::Create(Context, Result, Namespace);
475 else if (const TagDecl *TD = dyn_cast<TagDecl>(Parent))
476 Result = NestedNameSpecifier::Create(Context, Result,
478 Context.getTypeDeclType(TD).getTypePtr());
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)
488 const char *Name = Id->getNameStart();
489 return Name[0] == '_' &&
490 (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z'));
493 bool ResultBuilder::isInterestingDecl(const NamedDecl *ND,
494 bool &AsNestedNameSpecifier) const {
495 AsNestedNameSpecifier = false;
497 ND = ND->getUnderlyingDecl();
498 unsigned IDNS = ND->getIdentifierNamespace();
500 // Skip unnamed entities.
501 if (!ND->getDeclName())
504 // Friend declarations and declarations introduced due to friends are never
506 if (IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend))
509 // Class template (partial) specializations are never added as results.
510 if (isa<ClassTemplateSpecializationDecl>(ND) ||
511 isa<ClassTemplatePartialSpecializationDecl>(ND))
514 // Using declarations themselves are never added as results.
515 if (isa<UsingDecl>(ND))
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
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()))))
529 if (Filter == &ResultBuilder::IsNestedNameSpecifier ||
530 ((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
531 Filter != &ResultBuilder::IsNamespace &&
532 Filter != &ResultBuilder::IsNamespaceOrAlias &&
534 AsNestedNameSpecifier = true;
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;
550 // ... then it must be interesting!
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)
562 const DeclContext *HiddenCtx =
563 R.Declaration->getDeclContext()->getRedeclContext();
565 // There is no way to qualify a name declared in a function or method.
566 if (HiddenCtx->isFunctionOrMethod())
569 if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext())
572 // We can refer to the result with the appropriate qualification. Do it.
574 R.QualifierIsInformative = false;
577 R.Qualifier = getRequiredQualification(SemaRef.Context,
579 R.Declaration->getDeclContext());
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()) {
588 switch (cast<BuiltinType>(T)->getKind()) {
589 case BuiltinType::Void:
592 case BuiltinType::NullPtr:
595 case BuiltinType::Overload:
596 case BuiltinType::Dependent:
599 case BuiltinType::ObjCId:
600 case BuiltinType::ObjCClass:
601 case BuiltinType::ObjCSel:
602 return STC_ObjectiveC;
605 return STC_Arithmetic;
609 return STC_Arithmetic;
614 case Type::BlockPointer:
617 case Type::LValueReference:
618 case Type::RValueReference:
619 return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType());
621 case Type::ConstantArray:
622 case Type::IncompleteArray:
623 case Type::VariableArray:
624 case Type::DependentSizedArray:
627 case Type::DependentSizedExtVector:
629 case Type::ExtVector:
630 return STC_Arithmetic;
632 case Type::FunctionProto:
633 case Type::FunctionNoProto:
640 return STC_Arithmetic;
642 case Type::ObjCObject:
643 case Type::ObjCInterface:
644 case Type::ObjCObjectPointer:
645 return STC_ObjectiveC;
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());
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);
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();
676 // Dig through references, function pointers, and block pointers to
677 // get down to the likely type of an expression when the entity is
680 if (const ReferenceType *Ref = T->getAs<ReferenceType>()) {
681 T = Ref->getPointeeType();
685 if (const PointerType *Pointer = T->getAs<PointerType>()) {
686 if (Pointer->getPointeeType()->isFunctionType()) {
687 T = Pointer->getPointeeType();
694 if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) {
695 T = Block->getPointeeType();
699 if (const FunctionType *Function = T->getAs<FunctionType>()) {
700 T = Function->getReturnType();
710 unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) {
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"))
724 return CCP_LocalDeclaration;
727 const DeclContext *DC = ND->getDeclContext()->getRedeclContext();
728 if (DC->isRecord() || isa<ObjCContainerDecl>(DC))
729 return CCP_MemberDeclaration;
731 // Content-based decisions.
732 if (isa<EnumConstantDecl>(ND))
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))
746 return CCP_Declaration;
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;
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);
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;
775 void ResultBuilder::MaybeAddConstructorResults(Result R) {
776 if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration ||
777 !CompletionContext.wantConstructorResults())
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))
790 // There are no constructors here.
794 Record = Record->getDefinition();
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(),
808 R.CursorKind = getCursorKindForDecl(R.Declaration);
809 Results.push_back(R);
813 void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) {
814 assert(!ShadowMaps.empty() && "Must enter into a results scope");
816 if (R.Kind != Result::RK_Declaration) {
817 // For non-declaration results, just add the result.
818 Results.push_back(R);
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()),
832 const Decl *CanonDecl = R.Declaration->getCanonicalDecl();
833 unsigned IDNS = CanonDecl->getIdentifierNamespace();
835 bool AsNestedNameSpecifier = false;
836 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
839 // C++ constructors are never found by name lookup.
840 if (isa<CXXConstructorDecl>(R.Declaration))
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();
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;
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
866 std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end();
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();
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)))
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)
888 // The newly-added result is hidden by an entry in the shadow map.
889 if (CheckHiddenResult(R, CurContext, I->first))
896 // Make sure that any given declaration only shows up in the result set once.
897 if (!AllDeclsFound.insert(CanonDecl).second)
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;
906 AdjustResultPriorityForDecl(R);
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,
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());
919 R.QualifierIsInformative = false;
922 // Insert this result into the set of results and into the current shadow
924 SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size());
925 Results.push_back(R);
927 if (!AsNestedNameSpecifier)
928 MaybeAddConstructorResults(R);
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);
939 // Look through using declarations.
940 if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
941 AddResult(Result(Using->getTargetDecl(),
942 getBasePriority(Using->getTargetDecl()),
948 bool AsNestedNameSpecifier = false;
949 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
952 // C++ constructors are never found by name lookup.
953 if (isa<CXXConstructorDecl>(R.Declaration))
956 if (Hiding && CheckHiddenResult(R, CurContext, Hiding))
959 // Make sure that any given declaration only shows up in the result set once.
960 if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second)
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;
969 else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass &&
970 isa<CXXRecordDecl>(R.Declaration->getDeclContext()
971 ->getRedeclContext()))
972 R.QualifierIsInformative = true;
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,
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());
985 R.QualifierIsInformative = false;
988 // Adjust the priority if this result comes from a base class.
990 R.Priority += CCD_InBaseClass;
992 AdjustResultPriorityForDecl(R);
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
1008 // Insert this result into the set of results.
1009 Results.push_back(R);
1011 if (!AsNestedNameSpecifier)
1012 MaybeAddConstructorResults(R);
1015 void ResultBuilder::AddResult(Result R) {
1016 assert(R.Kind != Result::RK_Declaration &&
1017 "Declaration results need more context");
1018 Results.push_back(R);
1021 /// \brief Enter into a new scope.
1022 void ResultBuilder::EnterNewScope() {
1023 ShadowMaps.push_back(ShadowMap());
1026 /// \brief Exit from the current scope.
1027 void ResultBuilder::ExitScope() {
1028 for (ShadowMap::iterator E = ShadowMaps.back().begin(),
1029 EEnd = ShadowMaps.back().end();
1032 E->second.Destroy();
1034 ShadowMaps.pop_back();
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());
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))
1052 return ND->getIdentifierNamespace() & IDNS;
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))
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))
1070 return ND->getIdentifierNamespace() & IDNS;
1073 bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const {
1074 if (!IsOrdinaryNonTypeName(ND))
1077 if (const ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl()))
1078 if (VD->getType()->isIntegralOrEnumerationType())
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());
1089 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern;
1090 if (SemaRef.getLangOpts().CPlusPlus)
1091 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace;
1093 return (ND->getIdentifierNamespace() & IDNS) &&
1094 !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) &&
1095 !isa<ObjCPropertyDecl>(ND);
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();
1105 return SemaRef.isAcceptableNestedNameSpecifier(ND);
1108 /// \brief Determines whether the given declaration is an enumeration.
1109 bool ResultBuilder::IsEnum(const NamedDecl *ND) const {
1110 return isa<EnumDecl>(ND);
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();
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;
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();
1134 if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND))
1135 return RD->getTagKind() == TTK_Union;
1140 /// \brief Determines whether the given declaration is a namespace.
1141 bool ResultBuilder::IsNamespace(const NamedDecl *ND) const {
1142 return isa<NamespaceDecl>(ND);
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);
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();
1156 return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND);
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();
1166 return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
1167 isa<ObjCPropertyDecl>(ND);
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:
1179 switch (cast<BuiltinType>(T)->getKind()) {
1180 case BuiltinType::ObjCId:
1181 case BuiltinType::ObjCClass:
1182 case BuiltinType::ObjCSel:
1194 if (!C.getLangOpts().CPlusPlus)
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();
1203 bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const {
1204 QualType T = getDeclUsageType(SemaRef.Context, ND);
1208 T = SemaRef.Context.getBaseElementType(T);
1209 return isObjCReceiverType(SemaRef.Context, T);
1212 bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const {
1213 if (IsObjCMessageReceiver(ND))
1216 const VarDecl *Var = dyn_cast<VarDecl>(ND);
1220 return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>();
1223 bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const {
1224 if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) ||
1225 (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND)))
1228 QualType T = getDeclUsageType(SemaRef.Context, ND);
1232 T = SemaRef.Context.getBaseElementType(T);
1233 return T->isObjCObjectType() || T->isObjCObjectPointerType() ||
1234 T->isObjCIdType() ||
1235 (SemaRef.getLangOpts().CPlusPlus && T->isRecordType());
1238 bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const {
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);
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;
1256 CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext)
1257 : Results(Results), CurContext(CurContext) { }
1259 void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
1260 bool InBaseClass) override {
1261 bool Accessible = true;
1263 Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx);
1265 ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr,
1267 Results.AddResult(Result, CurContext, Hiding, InBaseClass);
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));
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));
1299 CodeCompletionBuilder Builder(Results.getAllocator(),
1300 Results.getCodeCompletionTUInfo());
1301 if (LangOpts.CPlusPlus) {
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));
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()));
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));
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()));
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"));
1336 Builder.AddTypedTextChunk("typeof");
1337 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1338 Builder.AddPlaceholderChunk("expression");
1339 Results.AddResult(Result(Builder.TakeString()));
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()));
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"));
1360 static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC,
1361 const LangOptions &LangOpts,
1362 ResultBuilder &Results) {
1363 typedef CodeCompletionResult Result;
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"));
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"));
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:
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,
1401 static void AddObjCImplementationResults(const LangOptions &LangOpts,
1402 ResultBuilder &Results,
1404 static void AddObjCInterfaceResults(const LangOptions &LangOpts,
1405 ResultBuilder &Results,
1407 static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt);
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()));
1420 static bool WantTypesInContext(Sema::ParserCompletionContext CCC,
1421 const LangOptions &LangOpts) {
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:
1435 case Sema::PCC_Expression:
1436 case Sema::PCC_Condition:
1437 return LangOpts.CPlusPlus;
1439 case Sema::PCC_ObjCInterface:
1440 case Sema::PCC_ObjCImplementation:
1443 case Sema::PCC_ForInit:
1444 return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99;
1447 llvm_unreachable("Invalid ParserCompletionContext!");
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;
1459 /// \brief Retrieve a printing policy suitable for code completion.
1460 static PrintingPolicy getCompletionPrintingPolicy(Sema &S) {
1461 return getCompletionPrintingPolicy(S.Context, S.PP);
1464 /// \brief Retrieve the string representation of the given type as a string
1465 /// that has the appropriate lifetime for code completion.
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);
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>";
1492 // Slow path: format the type as a string.
1494 T.getAsStringInternal(Result, Policy);
1495 return Allocator.CopyString(Result);
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())
1504 CodeCompletionAllocator &Allocator = Results.getAllocator();
1505 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
1506 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
1507 Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy,
1511 Builder.AddTypedTextChunk("this");
1512 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
1515 /// \brief Add language constructs that show up for "ordinary" names.
1516 static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC,
1519 ResultBuilder &Results) {
1520 CodeCompletionAllocator &Allocator = Results.getAllocator();
1521 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
1522 PrintingPolicy Policy = getCompletionPrintingPolicy(SemaRef);
1524 typedef CodeCompletionResult Result;
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()));
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()));
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()));
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()));
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()));
1572 if (SemaRef.getLangOpts().ObjC1)
1573 AddObjCTopLevelResults(Results, true);
1575 AddTypedefResult(Results);
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()));
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()));
1600 if (CCC == Sema::PCC_Class) {
1601 AddTypedefResult(Results);
1604 Builder.AddTypedTextChunk("public");
1605 if (Results.includeCodePatterns())
1606 Builder.AddChunk(CodeCompletionString::CK_Colon);
1607 Results.AddResult(Result(Builder.TakeString()));
1610 Builder.AddTypedTextChunk("protected");
1611 if (Results.includeCodePatterns())
1612 Builder.AddChunk(CodeCompletionString::CK_Colon);
1613 Results.AddResult(Result(Builder.TakeString()));
1616 Builder.AddTypedTextChunk("private");
1617 if (Results.includeCodePatterns())
1618 Builder.AddChunk(CodeCompletionString::CK_Colon);
1619 Results.AddResult(Result(Builder.TakeString()));
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()));
1635 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1636 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1639 case Sema::PCC_ObjCInterface:
1640 AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true);
1641 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1642 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1645 case Sema::PCC_ObjCImplementation:
1646 AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true);
1647 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1648 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1651 case Sema::PCC_ObjCInstanceVariableList:
1652 AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true);
1655 case Sema::PCC_RecoveryInFunction:
1656 case Sema::PCC_Statement: {
1657 AddTypedefResult(Results);
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()));
1676 if (SemaRef.getLangOpts().ObjC1)
1677 AddObjCStatementResults(Results, true);
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");
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()));
1694 // switch (condition) { }
1695 Builder.AddTypedTextChunk("switch");
1696 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1697 if (SemaRef.getLangOpts().CPlusPlus)
1698 Builder.AddPlaceholderChunk("condition");
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()));
1708 // Switch-specific statements.
1709 if (!SemaRef.getCurFunction()->SwitchStack.empty()) {
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()));
1718 Builder.AddTypedTextChunk("default");
1719 Builder.AddChunk(CodeCompletionString::CK_Colon);
1720 Results.AddResult(Result(Builder.TakeString()));
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");
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()));
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()));
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");
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()));
1770 if (S->getContinueParent()) {
1772 Builder.AddTypedTextChunk("continue");
1773 Results.AddResult(Result(Builder.TakeString()));
1776 if (S->getBreakParent()) {
1778 Builder.AddTypedTextChunk("break");
1779 Results.AddResult(Result(Builder.TakeString()));
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");
1795 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1796 Builder.AddPlaceholderChunk("expression");
1798 Results.AddResult(Result(Builder.TakeString()));
1800 // goto identifier ;
1801 Builder.AddTypedTextChunk("goto");
1802 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1803 Builder.AddPlaceholderChunk("label");
1804 Results.AddResult(Result(Builder.TakeString()));
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()));
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.
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()));
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()));
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()));
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);
1856 Builder.AddResultTypeChunk("bool");
1857 Builder.AddTypedTextChunk("true");
1858 Results.AddResult(Result(Builder.TakeString()));
1861 Builder.AddResultTypeChunk("bool");
1862 Builder.AddTypedTextChunk("false");
1863 Results.AddResult(Result(Builder.TakeString()));
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()));
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()));
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()));
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()));
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()));
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()));
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()));
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()));
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()));
1955 if (SemaRef.getLangOpts().CXXExceptions) {
1957 Builder.AddResultTypeChunk("void");
1958 Builder.AddTypedTextChunk("throw");
1959 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1960 Builder.AddPlaceholderChunk("expression");
1961 Results.AddResult(Result(Builder.TakeString()));
1966 if (SemaRef.getLangOpts().CPlusPlus11) {
1968 Builder.AddResultTypeChunk("std::nullptr_t");
1969 Builder.AddTypedTextChunk("nullptr");
1970 Results.AddResult(Result(Builder.TakeString()));
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()));
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()));
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()));
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())
2009 Builder.AddResultTypeChunk(Allocator.CopyString(SuperType));
2010 Builder.AddTypedTextChunk("super");
2011 Results.AddResult(Result(Builder.TakeString()));
2015 AddObjCExpressionResults(Results, true);
2018 if (SemaRef.getLangOpts().C11) {
2020 Builder.AddResultTypeChunk("size_t");
2021 if (SemaRef.getASTContext().Idents.get("alignof").hasMacroDefinition())
2022 Builder.AddTypedTextChunk("alignof");
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()));
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()));
2041 case Sema::PCC_Type:
2042 case Sema::PCC_LocalDeclarationSpecifiers:
2046 if (WantTypesInContext(CCC, SemaRef.getLangOpts()))
2047 AddTypeSpecifierResults(SemaRef.getLangOpts(), Results);
2049 if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type)
2050 Results.AddResult(Result("operator"));
2053 /// \brief If the given declaration has an associated type, add it as a result
2055 static void AddResultTypeChunk(ASTContext &Context,
2056 const PrintingPolicy &Policy,
2057 const NamedDecl *ND,
2058 CodeCompletionBuilder &Result) {
2062 // Skip constructors and conversion functions, which have their return types
2063 // built into their names.
2064 if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND))
2067 // Determine the type of the declaration (if it has a type).
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();
2082 if (T.isNull() || Context.hasSameType(T, Context.DependentTy))
2085 Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy,
2086 Result.getAllocator()));
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");
2100 Result.AddTextChunk(", (void*)0");
2104 static std::string formatObjCParamQualifiers(unsigned ObjCQuals) {
2106 if (ObjCQuals & Decl::OBJC_TQ_In)
2108 else if (ObjCQuals & Decl::OBJC_TQ_Inout)
2110 else if (ObjCQuals & Decl::OBJC_TQ_Out)
2112 if (ObjCQuals & Decl::OBJC_TQ_Bycopy)
2113 Result += "bycopy ";
2114 else if (ObjCQuals & Decl::OBJC_TQ_Byref)
2116 if (ObjCQuals & Decl::OBJC_TQ_Oneway)
2117 Result += "oneway ";
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.
2133 if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName)
2134 Result = Param->getIdentifier()->getName();
2136 Param->getType().getAsStringInternal(Result, Policy);
2138 if (ObjCMethodParam) {
2139 Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier())
2141 if (Param->getIdentifier() && !SuppressName)
2142 Result += Param->getIdentifier()->getName();
2147 // The argument for a block pointer parameter is a block literal with
2148 // the appropriate type.
2149 FunctionTypeLoc Block;
2150 FunctionProtoTypeLoc BlockProto;
2152 if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) {
2153 TL = TSInfo->getTypeLoc().getUnqualifiedLoc();
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();
2165 // Look through qualified types
2166 if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) {
2167 TL = QualifiedTL.getUnqualifiedLoc();
2172 // Try to get the function prototype behind the block pointer type,
2174 if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) {
2175 TL = BlockPtr.getPointeeLoc().IgnoreParens();
2176 Block = TL.getAs<FunctionTypeLoc>();
2177 BlockProto = TL.getAs<FunctionProtoTypeLoc>();
2184 // We were unable to find a FunctionProtoTypeLoc with parameter names
2185 // for the block; just use the parameter type as a placeholder.
2187 if (!ObjCMethodParam && Param->getIdentifier())
2188 Result = Param->getIdentifier()->getName();
2190 Param->getType().getUnqualifiedType().getAsStringInternal(Result, Policy);
2192 if (ObjCMethodParam) {
2193 Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier())
2195 if (Param->getIdentifier())
2196 Result += Param->getIdentifier()->getName();
2202 // We have the function prototype behind the block pointer type, as it was
2203 // written in the source.
2205 QualType ResultType = Block.getTypePtr()->getReturnType();
2206 if (!ResultType->isVoidType() || SuppressBlock)
2207 ResultType.getAsStringInternal(Result, Policy);
2209 // Format the parameter list.
2211 if (!BlockProto || Block.getNumParams() == 0) {
2212 if (BlockProto && BlockProto.getTypePtr()->isVariadic())
2218 for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) {
2221 Params += FormatFunctionParameter(Context, Policy, Block.getParam(I),
2222 /*SuppressName=*/false,
2223 /*SuppressBlock=*/true);
2225 if (I == N - 1 && BlockProto.getTypePtr()->isVariadic())
2231 if (SuppressBlock) {
2232 // Format as a parameter.
2233 Result = Result + " (^";
2234 if (Param->getIdentifier())
2235 Result += Param->getIdentifier()->getName();
2239 // Format as a block literal argument.
2240 Result = '^' + Result;
2243 if (Param->getIdentifier())
2244 Result += Param->getIdentifier()->getName();
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,
2256 bool InOptional = false) {
2257 bool FirstParameter = true;
2259 for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) {
2260 const ParmVarDecl *Param = Function->getParamDecl(P);
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());
2275 FirstParameter = false;
2277 Result.AddChunk(CodeCompletionString::CK_Comma);
2281 // Format the placeholder string.
2282 std::string PlaceholderStr = FormatFunctionParameter(Context, Policy,
2285 if (Function->isVariadic() && P == N - 1)
2286 PlaceholderStr += ", ...";
2288 // Add the placeholder string.
2289 Result.AddPlaceholderChunk(
2290 Result.getAllocator().CopyString(PlaceholderStr));
2293 if (const FunctionProtoType *Proto
2294 = Function->getType()->getAs<FunctionProtoType>())
2295 if (Proto->isVariadic()) {
2296 if (Proto->getNumParams() == 0)
2297 Result.AddPlaceholderChunk("...");
2299 MaybeAddSentinel(Context, Function, Result);
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,
2310 bool InDefaultArg = false) {
2311 bool FirstParameter = true;
2313 TemplateParameterList *Params = Template->getTemplateParameters();
2314 TemplateParameterList::iterator PEnd = Params->end();
2316 PEnd = Params->begin() + MaxParameters;
2317 for (TemplateParameterList::iterator P = Params->begin() + Start;
2319 bool HasDefaultArg = false;
2320 std::string PlaceholderStr;
2321 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
2322 if (TTP->wasDeclaredWithTypename())
2323 PlaceholderStr = "typename";
2325 PlaceholderStr = "class";
2327 if (TTP->getIdentifier()) {
2328 PlaceholderStr += ' ';
2329 PlaceholderStr += TTP->getIdentifier()->getName();
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();
2340 assert(isa<TemplateTemplateParmDecl>(*P));
2341 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
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();
2351 HasDefaultArg = TTP->hasDefaultArgument();
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());
2367 InDefaultArg = false;
2370 FirstParameter = false;
2372 Result.AddChunk(CodeCompletionString::CK_Comma);
2374 // Add the placeholder string.
2375 Result.AddPlaceholderChunk(
2376 Result.getAllocator().CopyString(PlaceholderStr));
2380 /// \brief Add a qualifier to the given code-completion string, if the
2381 /// provided nested-name-specifier is non-NULL.
2383 AddQualifierToCompletionString(CodeCompletionBuilder &Result,
2384 NestedNameSpecifier *Qualifier,
2385 bool QualifierIsInformative,
2386 ASTContext &Context,
2387 const PrintingPolicy &Policy) {
2391 std::string PrintedNNS;
2393 llvm::raw_string_ostream OS(PrintedNNS);
2394 Qualifier->print(OS, Policy);
2396 if (QualifierIsInformative)
2397 Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS));
2399 Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS));
2403 AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result,
2404 const FunctionDecl *Function) {
2405 const FunctionProtoType *Proto
2406 = Function->getType()->getAs<FunctionProtoType>();
2407 if (!Proto || !Proto->getTypeQuals())
2410 // FIXME: Add ref-qualifier!
2412 // Handle single qualifiers without copying
2413 if (Proto->getTypeQuals() == Qualifiers::Const) {
2414 Result.AddInformativeChunk(" const");
2418 if (Proto->getTypeQuals() == Qualifiers::Volatile) {
2419 Result.AddInformativeChunk(" volatile");
2423 if (Proto->getTypeQuals() == Qualifiers::Restrict) {
2424 Result.AddInformativeChunk(" restrict");
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));
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();
2447 switch (Name.getNameKind()) {
2448 case DeclarationName::CXXOperatorName: {
2449 const char *OperatorName = nullptr;
2450 switch (Name.getCXXOverloadedOperator()) {
2452 case OO_Conditional:
2453 case NUM_OVERLOADED_OPERATORS:
2454 OperatorName = "operator";
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"
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;
2469 Result.AddTypedTextChunk(OperatorName);
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()));
2481 case DeclarationName::CXXUsingDirective:
2482 case DeclarationName::ObjCZeroArgSelector:
2483 case DeclarationName::ObjCOneArgSelector:
2484 case DeclarationName::ObjCMultiArgSelector:
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();
2496 Result.AddTypedTextChunk(
2497 Result.getAllocator().CopyString(ND->getNameAsString()));
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);
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);
2521 /// \brief If possible, create a new code completion string for the given
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,
2530 CodeCompletionAllocator &Allocator,
2531 CodeCompletionTUInfo &CCTUInfo,
2532 bool IncludeBriefComments) {
2533 CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability);
2535 PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP);
2536 if (Kind == RK_Pattern) {
2537 Pattern->Priority = Priority;
2538 Pattern->Availability = Availability;
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();
2558 else if (const RawComment *RC =
2559 Ctx.getRawCommentForAnyRedecl(PDecl)) {
2560 Result.addBriefComment(RC->getBriefText(Ctx));
2561 Pattern->BriefComment = Result.getBriefComment();
2569 if (Kind == RK_Keyword) {
2570 Result.AddTypedTextChunk(Keyword);
2571 return Result.TakeString();
2574 if (Kind == RK_Macro) {
2575 const MacroDirective *MD = PP.getMacroDirectiveHistory(Macro);
2576 assert(MD && "Not a macro?");
2577 const MacroInfo *MI = MD->getMacroInfo();
2578 assert((!MD->isDefined() || MI) && "missing MacroInfo for define");
2580 Result.AddTypedTextChunk(
2581 Result.getAllocator().CopyString(Macro->getName()));
2583 if (!MI || !MI->isFunctionLike())
2584 return Result.TakeString();
2586 // Format a function-like macro with placeholders for the arguments.
2587 Result.AddChunk(CodeCompletionString::CK_LeftParen);
2588 MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end();
2590 // C99 variadic macros add __VA_ARGS__ at the end. Skip it.
2591 if (MI->isC99Varargs()) {
2595 Result.AddPlaceholderChunk("...");
2599 for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) {
2600 if (A != MI->arg_begin())
2601 Result.AddChunk(CodeCompletionString::CK_Comma);
2603 if (MI->isVariadic() && (A+1) == AEnd) {
2604 SmallString<32> Arg = (*A)->getName();
2605 if (MI->isC99Varargs())
2609 Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
2613 // Non-variadic macros are simple.
2614 Result.AddPlaceholderChunk(
2615 Result.getAllocator().CopyString((*A)->getName()));
2617 Result.AddChunk(CodeCompletionString::CK_RightParen);
2618 return Result.TakeString();
2621 assert(Kind == RK_Declaration && "Missed a result kind?");
2622 const NamedDecl *ND = Declaration;
2623 Result.addParentContext(ND->getDeclContext());
2625 if (IncludeBriefComments) {
2626 // Add documentation comment, if it exists.
2627 if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) {
2628 Result.addBriefComment(RC->getBriefText(Ctx));
2630 else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
2631 if (OMD->isPropertyAccessor())
2632 if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl())
2633 if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(PDecl))
2634 Result.addBriefComment(RC->getBriefText(Ctx));
2637 if (StartsNestedNameSpecifier) {
2638 Result.AddTypedTextChunk(
2639 Result.getAllocator().CopyString(ND->getNameAsString()));
2640 Result.AddTextChunk("::");
2641 return Result.TakeString();
2644 for (const auto *I : ND->specific_attrs<AnnotateAttr>())
2645 Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation()));
2647 AddResultTypeChunk(Ctx, Policy, ND, Result);
2649 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
2650 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
2652 AddTypedNameChunk(Ctx, Policy, ND, Result);
2653 Result.AddChunk(CodeCompletionString::CK_LeftParen);
2654 AddFunctionParameterChunks(Ctx, Policy, Function, Result);
2655 Result.AddChunk(CodeCompletionString::CK_RightParen);
2656 AddFunctionTypeQualsToCompletionString(Result, Function);
2657 return Result.TakeString();
2660 if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
2661 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
2663 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
2664 AddTypedNameChunk(Ctx, Policy, Function, Result);
2666 // Figure out which template parameters are deduced (or have default
2668 llvm::SmallBitVector Deduced;
2669 Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced);
2670 unsigned LastDeducibleArgument;
2671 for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0;
2672 --LastDeducibleArgument) {
2673 if (!Deduced[LastDeducibleArgument - 1]) {
2674 // C++0x: Figure out if the template argument has a default. If so,
2675 // the user doesn't need to type this argument.
2676 // FIXME: We need to abstract template parameters better!
2677 bool HasDefaultArg = false;
2678 NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam(
2679 LastDeducibleArgument - 1);
2680 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
2681 HasDefaultArg = TTP->hasDefaultArgument();
2682 else if (NonTypeTemplateParmDecl *NTTP
2683 = dyn_cast<NonTypeTemplateParmDecl>(Param))
2684 HasDefaultArg = NTTP->hasDefaultArgument();
2686 assert(isa<TemplateTemplateParmDecl>(Param));
2688 = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument();
2696 if (LastDeducibleArgument) {
2697 // Some of the function template arguments cannot be deduced from a
2698 // function call, so we introduce an explicit template argument list
2699 // containing all of the arguments up to the first deducible argument.
2700 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
2701 AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result,
2702 LastDeducibleArgument);
2703 Result.AddChunk(CodeCompletionString::CK_RightAngle);
2706 // Add the function parameters
2707 Result.AddChunk(CodeCompletionString::CK_LeftParen);
2708 AddFunctionParameterChunks(Ctx, Policy, Function, Result);
2709 Result.AddChunk(CodeCompletionString::CK_RightParen);
2710 AddFunctionTypeQualsToCompletionString(Result, Function);
2711 return Result.TakeString();
2714 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) {
2715 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
2717 Result.AddTypedTextChunk(
2718 Result.getAllocator().CopyString(Template->getNameAsString()));
2719 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
2720 AddTemplateParameterChunks(Ctx, Policy, Template, Result);
2721 Result.AddChunk(CodeCompletionString::CK_RightAngle);
2722 return Result.TakeString();
2725 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) {
2726 Selector Sel = Method->getSelector();
2727 if (Sel.isUnarySelector()) {
2728 Result.AddTypedTextChunk(Result.getAllocator().CopyString(
2729 Sel.getNameForSlot(0)));
2730 return Result.TakeString();
2733 std::string SelName = Sel.getNameForSlot(0).str();
2735 if (StartParameter == 0)
2736 Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName));
2738 Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName));
2740 // If there is only one parameter, and we're past it, add an empty
2741 // typed-text chunk since there is nothing to type.
2742 if (Method->param_size() == 1)
2743 Result.AddTypedTextChunk("");
2746 for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(),
2747 PEnd = Method->param_end();
2748 P != PEnd; (void)++P, ++Idx) {
2750 std::string Keyword;
2751 if (Idx > StartParameter)
2752 Result.AddChunk(CodeCompletionString::CK_HorizontalSpace);
2753 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx))
2754 Keyword += II->getName();
2756 if (Idx < StartParameter || AllParametersAreInformative)
2757 Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword));
2759 Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword));
2762 // If we're before the starting parameter, skip the placeholder.
2763 if (Idx < StartParameter)
2768 if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity)
2769 Arg = FormatFunctionParameter(Ctx, Policy, *P, true);
2771 (*P)->getType().getAsStringInternal(Arg, Policy);
2772 Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier())
2774 if (IdentifierInfo *II = (*P)->getIdentifier())
2775 if (DeclaringEntity || AllParametersAreInformative)
2776 Arg += II->getName();
2779 if (Method->isVariadic() && (P + 1) == PEnd)
2782 if (DeclaringEntity)
2783 Result.AddTextChunk(Result.getAllocator().CopyString(Arg));
2784 else if (AllParametersAreInformative)
2785 Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg));
2787 Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
2790 if (Method->isVariadic()) {
2791 if (Method->param_size() == 0) {
2792 if (DeclaringEntity)
2793 Result.AddTextChunk(", ...");
2794 else if (AllParametersAreInformative)
2795 Result.AddInformativeChunk(", ...");
2797 Result.AddPlaceholderChunk(", ...");
2800 MaybeAddSentinel(Ctx, Method, Result);
2803 return Result.TakeString();
2807 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
2810 Result.AddTypedTextChunk(
2811 Result.getAllocator().CopyString(ND->getNameAsString()));
2812 return Result.TakeString();
2815 CodeCompletionString *
2816 CodeCompleteConsumer::OverloadCandidate::CreateSignatureString(
2817 unsigned CurrentArg,
2819 CodeCompletionAllocator &Allocator,
2820 CodeCompletionTUInfo &CCTUInfo) const {
2821 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
2823 // FIXME: Set priority, availability appropriately.
2824 CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available);
2825 FunctionDecl *FDecl = getFunction();
2826 AddResultTypeChunk(S.Context, Policy, FDecl, Result);
2827 const FunctionProtoType *Proto
2828 = dyn_cast<FunctionProtoType>(getFunctionType());
2829 if (!FDecl && !Proto) {
2830 // Function without a prototype. Just give the return type and a
2831 // highlighted ellipsis.
2832 const FunctionType *FT = getFunctionType();
2833 Result.AddTextChunk(GetCompletionTypeString(FT->getReturnType(), S.Context,
2834 Policy, Result.getAllocator()));
2835 Result.AddChunk(CodeCompletionString::CK_LeftParen);
2836 Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "...");
2837 Result.AddChunk(CodeCompletionString::CK_RightParen);
2838 return Result.TakeString();
2842 Result.AddTextChunk(
2843 Result.getAllocator().CopyString(FDecl->getNameAsString()));
2845 Result.AddTextChunk(Result.getAllocator().CopyString(
2846 Proto->getReturnType().getAsString(Policy)));
2848 Result.AddChunk(CodeCompletionString::CK_LeftParen);
2849 unsigned NumParams = FDecl ? FDecl->getNumParams() : Proto->getNumParams();
2850 for (unsigned I = 0; I != NumParams; ++I) {
2852 Result.AddChunk(CodeCompletionString::CK_Comma);
2854 std::string ArgString;
2858 ArgString = FDecl->getParamDecl(I)->getNameAsString();
2859 ArgType = FDecl->getParamDecl(I)->getOriginalType();
2861 ArgType = Proto->getParamType(I);
2864 ArgType.getAsStringInternal(ArgString, Policy);
2866 if (I == CurrentArg)
2867 Result.AddChunk(CodeCompletionString::CK_CurrentParameter,
2868 Result.getAllocator().CopyString(ArgString));
2870 Result.AddTextChunk(Result.getAllocator().CopyString(ArgString));
2873 if (Proto && Proto->isVariadic()) {
2874 Result.AddChunk(CodeCompletionString::CK_Comma);
2875 if (CurrentArg < NumParams)
2876 Result.AddTextChunk("...");
2878 Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "...");
2880 Result.AddChunk(CodeCompletionString::CK_RightParen);
2882 return Result.TakeString();
2885 unsigned clang::getMacroUsagePriority(StringRef MacroName,
2886 const LangOptions &LangOpts,
2887 bool PreferredTypeIsPointer) {
2888 unsigned Priority = CCP_Macro;
2890 // Treat the "nil", "Nil" and "NULL" macros as null pointer constants.
2891 if (MacroName.equals("nil") || MacroName.equals("NULL") ||
2892 MacroName.equals("Nil")) {
2893 Priority = CCP_Constant;
2894 if (PreferredTypeIsPointer)
2895 Priority = Priority / CCF_SimilarTypeMatch;
2897 // Treat "YES", "NO", "true", and "false" as constants.
2898 else if (MacroName.equals("YES") || MacroName.equals("NO") ||
2899 MacroName.equals("true") || MacroName.equals("false"))
2900 Priority = CCP_Constant;
2901 // Treat "bool" as a type.
2902 else if (MacroName.equals("bool"))
2903 Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0);
2909 CXCursorKind clang::getCursorKindForDecl(const Decl *D) {
2911 return CXCursor_UnexposedDecl;
2913 switch (D->getKind()) {
2914 case Decl::Enum: return CXCursor_EnumDecl;
2915 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
2916 case Decl::Field: return CXCursor_FieldDecl;
2917 case Decl::Function:
2918 return CXCursor_FunctionDecl;
2919 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
2920 case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl;
2921 case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl;
2923 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
2924 case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl;
2925 case Decl::ObjCMethod:
2926 return cast<ObjCMethodDecl>(D)->isInstanceMethod()
2927 ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl;
2928 case Decl::CXXMethod: return CXCursor_CXXMethod;
2929 case Decl::CXXConstructor: return CXCursor_Constructor;
2930 case Decl::CXXDestructor: return CXCursor_Destructor;
2931 case Decl::CXXConversion: return CXCursor_ConversionFunction;
2932 case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl;
2933 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
2934 case Decl::ParmVar: return CXCursor_ParmDecl;
2935 case Decl::Typedef: return CXCursor_TypedefDecl;
2936 case Decl::TypeAlias: return CXCursor_TypeAliasDecl;
2937 case Decl::Var: return CXCursor_VarDecl;
2938 case Decl::Namespace: return CXCursor_Namespace;
2939 case Decl::NamespaceAlias: return CXCursor_NamespaceAlias;
2940 case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter;
2941 case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter;
2942 case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter;
2943 case Decl::FunctionTemplate: return CXCursor_FunctionTemplate;
2944 case Decl::ClassTemplate: return CXCursor_ClassTemplate;
2945 case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier;
2946 case Decl::ClassTemplatePartialSpecialization:
2947 return CXCursor_ClassTemplatePartialSpecialization;
2948 case Decl::UsingDirective: return CXCursor_UsingDirective;
2949 case Decl::TranslationUnit: return CXCursor_TranslationUnit;
2952 case Decl::UnresolvedUsingValue:
2953 case Decl::UnresolvedUsingTypename:
2954 return CXCursor_UsingDeclaration;
2956 case Decl::ObjCPropertyImpl:
2957 switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) {
2958 case ObjCPropertyImplDecl::Dynamic:
2959 return CXCursor_ObjCDynamicDecl;
2961 case ObjCPropertyImplDecl::Synthesize:
2962 return CXCursor_ObjCSynthesizeDecl;
2966 return CXCursor_ModuleImportDecl;
2969 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
2970 switch (TD->getTagKind()) {
2971 case TTK_Interface: // fall through
2972 case TTK_Struct: return CXCursor_StructDecl;
2973 case TTK_Class: return CXCursor_ClassDecl;
2974 case TTK_Union: return CXCursor_UnionDecl;
2975 case TTK_Enum: return CXCursor_EnumDecl;
2980 return CXCursor_UnexposedDecl;
2983 static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results,
2984 bool IncludeUndefined,
2985 bool TargetTypeIsPointer = false) {
2986 typedef CodeCompletionResult Result;
2988 Results.EnterNewScope();
2990 for (Preprocessor::macro_iterator M = PP.macro_begin(),
2991 MEnd = PP.macro_end();
2993 if (IncludeUndefined || M->first->hasMacroDefinition()) {
2994 if (MacroInfo *MI = M->second->getMacroInfo())
2995 if (MI->isUsedForHeaderGuard())
2998 Results.AddResult(Result(M->first,
2999 getMacroUsagePriority(M->first->getName(),
3001 TargetTypeIsPointer)));
3005 Results.ExitScope();
3009 static void AddPrettyFunctionResults(const LangOptions &LangOpts,
3010 ResultBuilder &Results) {
3011 typedef CodeCompletionResult Result;
3013 Results.EnterNewScope();
3015 Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant));
3016 Results.AddResult(Result("__FUNCTION__", CCP_Constant));
3017 if (LangOpts.C99 || LangOpts.CPlusPlus11)
3018 Results.AddResult(Result("__func__", CCP_Constant));
3019 Results.ExitScope();
3022 static void HandleCodeCompleteResults(Sema *S,
3023 CodeCompleteConsumer *CodeCompleter,
3024 CodeCompletionContext Context,
3025 CodeCompletionResult *Results,
3026 unsigned NumResults) {
3028 CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults);
3031 static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S,
3032 Sema::ParserCompletionContext PCC) {
3034 case Sema::PCC_Namespace:
3035 return CodeCompletionContext::CCC_TopLevel;
3037 case Sema::PCC_Class:
3038 return CodeCompletionContext::CCC_ClassStructUnion;
3040 case Sema::PCC_ObjCInterface:
3041 return CodeCompletionContext::CCC_ObjCInterface;
3043 case Sema::PCC_ObjCImplementation:
3044 return CodeCompletionContext::CCC_ObjCImplementation;
3046 case Sema::PCC_ObjCInstanceVariableList:
3047 return CodeCompletionContext::CCC_ObjCIvarList;
3049 case Sema::PCC_Template:
3050 case Sema::PCC_MemberTemplate:
3051 if (S.CurContext->isFileContext())
3052 return CodeCompletionContext::CCC_TopLevel;
3053 if (S.CurContext->isRecord())
3054 return CodeCompletionContext::CCC_ClassStructUnion;
3055 return CodeCompletionContext::CCC_Other;
3057 case Sema::PCC_RecoveryInFunction:
3058 return CodeCompletionContext::CCC_Recovery;
3060 case Sema::PCC_ForInit:
3061 if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 ||
3062 S.getLangOpts().ObjC1)
3063 return CodeCompletionContext::CCC_ParenthesizedExpression;
3065 return CodeCompletionContext::CCC_Expression;
3067 case Sema::PCC_Expression:
3068 case Sema::PCC_Condition:
3069 return CodeCompletionContext::CCC_Expression;
3071 case Sema::PCC_Statement:
3072 return CodeCompletionContext::CCC_Statement;
3074 case Sema::PCC_Type:
3075 return CodeCompletionContext::CCC_Type;
3077 case Sema::PCC_ParenthesizedExpression:
3078 return CodeCompletionContext::CCC_ParenthesizedExpression;
3080 case Sema::PCC_LocalDeclarationSpecifiers:
3081 return CodeCompletionContext::CCC_Type;
3084 llvm_unreachable("Invalid ParserCompletionContext!");
3087 /// \brief If we're in a C++ virtual member function, add completion results
3088 /// that invoke the functions we override, since it's common to invoke the
3089 /// overridden function as well as adding new functionality.
3091 /// \param S The semantic analysis object for which we are generating results.
3093 /// \param InContext This context in which the nested-name-specifier preceding
3094 /// the code-completion point
3095 static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext,
3096 ResultBuilder &Results) {
3097 // Look through blocks.
3098 DeclContext *CurContext = S.CurContext;
3099 while (isa<BlockDecl>(CurContext))
3100 CurContext = CurContext->getParent();
3103 CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext);
3104 if (!Method || !Method->isVirtual())
3107 // We need to have names for all of the parameters, if we're going to
3108 // generate a forwarding call.
3109 for (auto P : Method->params())
3110 if (!P->getDeclName())
3113 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
3114 for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(),
3115 MEnd = Method->end_overridden_methods();
3117 CodeCompletionBuilder Builder(Results.getAllocator(),
3118 Results.getCodeCompletionTUInfo());
3119 const CXXMethodDecl *Overridden = *M;
3120 if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl())
3123 // If we need a nested-name-specifier, add one now.
3125 NestedNameSpecifier *NNS
3126 = getRequiredQualification(S.Context, CurContext,
3127 Overridden->getDeclContext());
3130 llvm::raw_string_ostream OS(Str);
3131 NNS->print(OS, Policy);
3132 Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str()));
3134 } else if (!InContext->Equals(Overridden->getDeclContext()))
3137 Builder.AddTypedTextChunk(Results.getAllocator().CopyString(
3138 Overridden->getNameAsString()));
3139 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
3140 bool FirstParam = true;
3141 for (auto P : Method->params()) {
3145 Builder.AddChunk(CodeCompletionString::CK_Comma);
3147 Builder.AddPlaceholderChunk(
3148 Results.getAllocator().CopyString(P->getIdentifier()->getName()));
3150 Builder.AddChunk(CodeCompletionString::CK_RightParen);
3151 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
3152 CCP_SuperCompletion,
3154 CXAvailability_Available,
3156 Results.Ignore(Overridden);
3160 void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc,
3161 ModuleIdPath Path) {
3162 typedef CodeCompletionResult Result;
3163 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3164 CodeCompleter->getCodeCompletionTUInfo(),
3165 CodeCompletionContext::CCC_Other);
3166 Results.EnterNewScope();
3168 CodeCompletionAllocator &Allocator = Results.getAllocator();
3169 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
3170 typedef CodeCompletionResult Result;
3172 // Enumerate all top-level modules.
3173 SmallVector<Module *, 8> Modules;
3174 PP.getHeaderSearchInfo().collectAllModules(Modules);
3175 for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
3176 Builder.AddTypedTextChunk(
3177 Builder.getAllocator().CopyString(Modules[I]->Name));
3178 Results.AddResult(Result(Builder.TakeString(),
3180 CXCursor_ModuleImportDecl,
3181 Modules[I]->isAvailable()
3182 ? CXAvailability_Available
3183 : CXAvailability_NotAvailable));
3185 } else if (getLangOpts().Modules) {
3186 // Load the named module.
3187 Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path,
3189 /*IsInclusionDirective=*/false);
3190 // Enumerate submodules.
3192 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
3193 SubEnd = Mod->submodule_end();
3194 Sub != SubEnd; ++Sub) {
3196 Builder.AddTypedTextChunk(
3197 Builder.getAllocator().CopyString((*Sub)->Name));
3198 Results.AddResult(Result(Builder.TakeString(),
3200 CXCursor_ModuleImportDecl,
3201 (*Sub)->isAvailable()
3202 ? CXAvailability_Available
3203 : CXAvailability_NotAvailable));
3207 Results.ExitScope();
3208 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3209 Results.data(),Results.size());
3212 void Sema::CodeCompleteOrdinaryName(Scope *S,
3213 ParserCompletionContext CompletionContext) {
3214 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3215 CodeCompleter->getCodeCompletionTUInfo(),
3216 mapCodeCompletionContext(*this, CompletionContext));
3217 Results.EnterNewScope();
3219 // Determine how to filter results, e.g., so that the names of
3220 // values (functions, enumerators, function templates, etc.) are
3221 // only allowed where we can have an expression.
3222 switch (CompletionContext) {
3225 case PCC_ObjCInterface:
3226 case PCC_ObjCImplementation:
3227 case PCC_ObjCInstanceVariableList:
3229 case PCC_MemberTemplate:
3231 case PCC_LocalDeclarationSpecifiers:
3232 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
3236 case PCC_ParenthesizedExpression:
3237 case PCC_Expression:
3240 if (WantTypesInContext(CompletionContext, getLangOpts()))
3241 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3243 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
3245 if (getLangOpts().CPlusPlus)
3246 MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results);
3249 case PCC_RecoveryInFunction:
3254 // If we are in a C++ non-static member function, check the qualifiers on
3255 // the member function to filter/prioritize the results list.
3256 if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext))
3257 if (CurMethod->isInstance())
3258 Results.setObjectTypeQualifiers(
3259 Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers()));
3261 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3262 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3263 CodeCompleter->includeGlobals());
3265 AddOrdinaryNameResults(CompletionContext, S, *this, Results);
3266 Results.ExitScope();
3268 switch (CompletionContext) {
3269 case PCC_ParenthesizedExpression:
3270 case PCC_Expression:
3272 case PCC_RecoveryInFunction:
3273 if (S->getFnParent())
3274 AddPrettyFunctionResults(PP.getLangOpts(), Results);
3279 case PCC_ObjCInterface:
3280 case PCC_ObjCImplementation:
3281 case PCC_ObjCInstanceVariableList:
3283 case PCC_MemberTemplate:
3287 case PCC_LocalDeclarationSpecifiers:
3291 if (CodeCompleter->includeMacros())
3292 AddMacroResults(PP, Results, false);
3294 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3295 Results.data(),Results.size());
3298 static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
3299 ParsedType Receiver,
3300 ArrayRef<IdentifierInfo *> SelIdents,
3301 bool AtArgumentExpression,
3303 ResultBuilder &Results);
3305 void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS,
3306 bool AllowNonIdentifiers,
3307 bool AllowNestedNameSpecifiers) {
3308 typedef CodeCompletionResult Result;
3309 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3310 CodeCompleter->getCodeCompletionTUInfo(),
3311 AllowNestedNameSpecifiers
3312 ? CodeCompletionContext::CCC_PotentiallyQualifiedName
3313 : CodeCompletionContext::CCC_Name);
3314 Results.EnterNewScope();
3316 // Type qualifiers can come after names.
3317 Results.AddResult(Result("const"));
3318 Results.AddResult(Result("volatile"));
3319 if (getLangOpts().C99)
3320 Results.AddResult(Result("restrict"));
3322 if (getLangOpts().CPlusPlus) {
3323 if (AllowNonIdentifiers) {
3324 Results.AddResult(Result("operator"));
3327 // Add nested-name-specifiers.
3328 if (AllowNestedNameSpecifiers) {
3329 Results.allowNestedNameSpecifiers();
3330 Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy);
3331 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3332 LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer,
3333 CodeCompleter->includeGlobals());
3334 Results.setFilter(nullptr);
3337 Results.ExitScope();
3339 // If we're in a context where we might have an expression (rather than a
3340 // declaration), and what we've seen so far is an Objective-C type that could
3341 // be a receiver of a class message, this may be a class message send with
3342 // the initial opening bracket '[' missing. Add appropriate completions.
3343 if (AllowNonIdentifiers && !AllowNestedNameSpecifiers &&
3344 DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier &&
3345 DS.getTypeSpecType() == DeclSpec::TST_typename &&
3346 DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified &&
3347 DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
3348 !DS.isTypeAltiVecVector() &&
3350 (S->getFlags() & Scope::DeclScope) != 0 &&
3351 (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope |
3352 Scope::FunctionPrototypeScope |
3353 Scope::AtCatchScope)) == 0) {
3354 ParsedType T = DS.getRepAsType();
3355 if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType())
3356 AddClassMessageCompletions(*this, S, T, None, false, false, Results);
3359 // Note that we intentionally suppress macro results here, since we do not
3360 // encourage using macros to produce the names of entities.
3362 HandleCodeCompleteResults(this, CodeCompleter,
3363 Results.getCompletionContext(),
3364 Results.data(), Results.size());
3367 struct Sema::CodeCompleteExpressionData {
3368 CodeCompleteExpressionData(QualType PreferredType = QualType())
3369 : PreferredType(PreferredType), IntegralConstantExpression(false),
3370 ObjCCollection(false) { }
3372 QualType PreferredType;
3373 bool IntegralConstantExpression;
3374 bool ObjCCollection;
3375 SmallVector<Decl *, 4> IgnoreDecls;
3378 /// \brief Perform code-completion in an expression context when we know what
3379 /// type we're looking for.
3380 void Sema::CodeCompleteExpression(Scope *S,
3381 const CodeCompleteExpressionData &Data) {
3382 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3383 CodeCompleter->getCodeCompletionTUInfo(),
3384 CodeCompletionContext::CCC_Expression);
3385 if (Data.ObjCCollection)
3386 Results.setFilter(&ResultBuilder::IsObjCCollection);
3387 else if (Data.IntegralConstantExpression)
3388 Results.setFilter(&ResultBuilder::IsIntegralConstantValue);
3389 else if (WantTypesInContext(PCC_Expression, getLangOpts()))
3390 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3392 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
3394 if (!Data.PreferredType.isNull())
3395 Results.setPreferredType(Data.PreferredType.getNonReferenceType());
3397 // Ignore any declarations that we were told that we don't care about.
3398 for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I)
3399 Results.Ignore(Data.IgnoreDecls[I]);
3401 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3402 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3403 CodeCompleter->includeGlobals());
3405 Results.EnterNewScope();
3406 AddOrdinaryNameResults(PCC_Expression, S, *this, Results);
3407 Results.ExitScope();
3409 bool PreferredTypeIsPointer = false;
3410 if (!Data.PreferredType.isNull())
3411 PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType()
3412 || Data.PreferredType->isMemberPointerType()
3413 || Data.PreferredType->isBlockPointerType();
3415 if (S->getFnParent() &&
3416 !Data.ObjCCollection &&
3417 !Data.IntegralConstantExpression)
3418 AddPrettyFunctionResults(PP.getLangOpts(), Results);
3420 if (CodeCompleter->includeMacros())
3421 AddMacroResults(PP, Results, false, PreferredTypeIsPointer);
3422 HandleCodeCompleteResults(this, CodeCompleter,
3423 CodeCompletionContext(CodeCompletionContext::CCC_Expression,
3424 Data.PreferredType),
3425 Results.data(),Results.size());
3428 void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) {
3430 CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction);
3431 else if (getLangOpts().ObjC1)
3432 CodeCompleteObjCInstanceMessage(S, E.get(), None, false);
3435 /// \brief The set of properties that have already been added, referenced by
3437 typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet;
3439 /// \brief Retrieve the container definition, if any?
3440 static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) {
3441 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
3442 if (Interface->hasDefinition())
3443 return Interface->getDefinition();
3448 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3449 if (Protocol->hasDefinition())
3450 return Protocol->getDefinition();
3457 static void AddObjCProperties(ObjCContainerDecl *Container,
3458 bool AllowCategories,
3459 bool AllowNullaryMethods,
3460 DeclContext *CurContext,
3461 AddedPropertiesSet &AddedProperties,
3462 ResultBuilder &Results) {
3463 typedef CodeCompletionResult Result;
3465 // Retrieve the definition.
3466 Container = getContainerDef(Container);
3468 // Add properties in this container.
3469 for (const auto *P : Container->properties())
3470 if (AddedProperties.insert(P->getIdentifier()).second)
3471 Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr),
3474 // Add nullary methods
3475 if (AllowNullaryMethods) {
3476 ASTContext &Context = Container->getASTContext();
3477 PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
3478 for (auto *M : Container->methods()) {
3479 if (M->getSelector().isUnarySelector())
3480 if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0))
3481 if (AddedProperties.insert(Name).second) {
3482 CodeCompletionBuilder Builder(Results.getAllocator(),
3483 Results.getCodeCompletionTUInfo());
3484 AddResultTypeChunk(Context, Policy, M, Builder);
3485 Builder.AddTypedTextChunk(
3486 Results.getAllocator().CopyString(Name->getName()));
3488 Results.MaybeAddResult(Result(Builder.TakeString(), M,
3489 CCP_MemberDeclaration + CCD_MethodAsProperty),
3496 // Add properties in referenced protocols.
3497 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3498 for (auto *P : Protocol->protocols())
3499 AddObjCProperties(P, AllowCategories, AllowNullaryMethods, CurContext,
3500 AddedProperties, Results);
3501 } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){
3502 if (AllowCategories) {
3503 // Look through categories.
3504 for (auto *Cat : IFace->known_categories())
3505 AddObjCProperties(Cat, AllowCategories, AllowNullaryMethods, CurContext,
3506 AddedProperties, Results);
3509 // Look through protocols.
3510 for (auto *I : IFace->all_referenced_protocols())
3511 AddObjCProperties(I, AllowCategories, AllowNullaryMethods, CurContext,
3512 AddedProperties, Results);
3514 // Look in the superclass.
3515 if (IFace->getSuperClass())
3516 AddObjCProperties(IFace->getSuperClass(), AllowCategories,
3517 AllowNullaryMethods, CurContext,
3518 AddedProperties, Results);
3519 } else if (const ObjCCategoryDecl *Category
3520 = dyn_cast<ObjCCategoryDecl>(Container)) {
3521 // Look through protocols.
3522 for (auto *P : Category->protocols())
3523 AddObjCProperties(P, AllowCategories, AllowNullaryMethods, CurContext,
3524 AddedProperties, Results);
3528 void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base,
3529 SourceLocation OpLoc,
3531 if (!Base || !CodeCompleter)
3534 ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow);
3535 if (ConvertedBase.isInvalid())
3537 Base = ConvertedBase.get();
3539 typedef CodeCompletionResult Result;
3541 QualType BaseType = Base->getType();
3544 if (const PointerType *Ptr = BaseType->getAs<PointerType>())
3545 BaseType = Ptr->getPointeeType();
3546 else if (BaseType->isObjCObjectPointerType())
3552 enum CodeCompletionContext::Kind contextKind;
3555 contextKind = CodeCompletionContext::CCC_ArrowMemberAccess;
3558 if (BaseType->isObjCObjectPointerType() ||
3559 BaseType->isObjCObjectOrInterfaceType()) {
3560 contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess;
3563 contextKind = CodeCompletionContext::CCC_DotMemberAccess;
3567 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3568 CodeCompleter->getCodeCompletionTUInfo(),
3569 CodeCompletionContext(contextKind,
3571 &ResultBuilder::IsMember);
3572 Results.EnterNewScope();
3573 if (const RecordType *Record = BaseType->getAs<RecordType>()) {
3574 // Indicate that we are performing a member access, and the cv-qualifiers
3575 // for the base object type.
3576 Results.setObjectTypeQualifiers(BaseType.getQualifiers());
3578 // Access to a C/C++ class, struct, or union.
3579 Results.allowNestedNameSpecifiers();
3580 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3581 LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer,
3582 CodeCompleter->includeGlobals());
3584 if (getLangOpts().CPlusPlus) {
3585 if (!Results.empty()) {
3586 // The "template" keyword can follow "->" or "." in the grammar.
3587 // However, we only want to suggest the template keyword if something
3589 bool IsDependent = BaseType->isDependentType();
3591 for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent())
3592 if (DeclContext *Ctx = DepScope->getEntity()) {
3593 IsDependent = Ctx->isDependentContext();
3599 Results.AddResult(Result("template"));
3602 } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) {
3603 // Objective-C property reference.
3604 AddedPropertiesSet AddedProperties;
3606 // Add property results based on our interface.
3607 const ObjCObjectPointerType *ObjCPtr
3608 = BaseType->getAsObjCInterfacePointerType();
3609 assert(ObjCPtr && "Non-NULL pointer guaranteed above!");
3610 AddObjCProperties(ObjCPtr->getInterfaceDecl(), true,
3611 /*AllowNullaryMethods=*/true, CurContext,
3612 AddedProperties, Results);
3614 // Add properties from the protocols in a qualified interface.
3615 for (auto *I : ObjCPtr->quals())
3616 AddObjCProperties(I, true, /*AllowNullaryMethods=*/true, CurContext,
3617 AddedProperties, Results);
3618 } else if ((IsArrow && BaseType->isObjCObjectPointerType()) ||
3619 (!IsArrow && BaseType->isObjCObjectType())) {
3620 // Objective-C instance variable access.
3621 ObjCInterfaceDecl *Class = nullptr;
3622 if (const ObjCObjectPointerType *ObjCPtr
3623 = BaseType->getAs<ObjCObjectPointerType>())
3624 Class = ObjCPtr->getInterfaceDecl();
3626 Class = BaseType->getAs<ObjCObjectType>()->getInterface();
3628 // Add all ivars from this class and its superclasses.
3630 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3631 Results.setFilter(&ResultBuilder::IsObjCIvar);
3632 LookupVisibleDecls(Class, LookupMemberName, Consumer,
3633 CodeCompleter->includeGlobals());
3637 // FIXME: How do we cope with isa?
3639 Results.ExitScope();
3641 // Hand off the results found for code completion.
3642 HandleCodeCompleteResults(this, CodeCompleter,
3643 Results.getCompletionContext(),
3644 Results.data(),Results.size());
3647 void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) {
3651 ResultBuilder::LookupFilter Filter = nullptr;
3652 enum CodeCompletionContext::Kind ContextKind
3653 = CodeCompletionContext::CCC_Other;
3654 switch ((DeclSpec::TST)TagSpec) {
3655 case DeclSpec::TST_enum:
3656 Filter = &ResultBuilder::IsEnum;
3657 ContextKind = CodeCompletionContext::CCC_EnumTag;
3660 case DeclSpec::TST_union:
3661 Filter = &ResultBuilder::IsUnion;
3662 ContextKind = CodeCompletionContext::CCC_UnionTag;
3665 case DeclSpec::TST_struct:
3666 case DeclSpec::TST_class:
3667 case DeclSpec::TST_interface:
3668 Filter = &ResultBuilder::IsClassOrStruct;
3669 ContextKind = CodeCompletionContext::CCC_ClassOrStructTag;
3673 llvm_unreachable("Unknown type specifier kind in CodeCompleteTag");
3676 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3677 CodeCompleter->getCodeCompletionTUInfo(), ContextKind);
3678 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3680 // First pass: look for tags.
3681 Results.setFilter(Filter);
3682 LookupVisibleDecls(S, LookupTagName, Consumer,
3683 CodeCompleter->includeGlobals());
3685 if (CodeCompleter->includeGlobals()) {
3686 // Second pass: look for nested name specifiers.
3687 Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
3688 LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer);
3691 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3692 Results.data(),Results.size());
3695 void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) {
3696 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3697 CodeCompleter->getCodeCompletionTUInfo(),
3698 CodeCompletionContext::CCC_TypeQualifiers);
3699 Results.EnterNewScope();
3700 if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const))
3701 Results.AddResult("const");
3702 if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile))
3703 Results.AddResult("volatile");
3704 if (getLangOpts().C99 &&
3705 !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict))
3706 Results.AddResult("restrict");
3707 if (getLangOpts().C11 &&
3708 !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic))
3709 Results.AddResult("_Atomic");
3710 Results.ExitScope();
3711 HandleCodeCompleteResults(this, CodeCompleter,
3712 Results.getCompletionContext(),
3713 Results.data(), Results.size());
3716 void Sema::CodeCompleteCase(Scope *S) {
3717 if (getCurFunction()->SwitchStack.empty() || !CodeCompleter)
3720 SwitchStmt *Switch = getCurFunction()->SwitchStack.back();
3721 QualType type = Switch->getCond()->IgnoreImplicit()->getType();
3722 if (!type->isEnumeralType()) {
3723 CodeCompleteExpressionData Data(type);
3724 Data.IntegralConstantExpression = true;
3725 CodeCompleteExpression(S, Data);
3729 // Code-complete the cases of a switch statement over an enumeration type
3730 // by providing the list of
3731 EnumDecl *Enum = type->castAs<EnumType>()->getDecl();
3732 if (EnumDecl *Def = Enum->getDefinition())
3735 // Determine which enumerators we have already seen in the switch statement.
3736 // FIXME: Ideally, we would also be able to look *past* the code-completion
3737 // token, in case we are code-completing in the middle of the switch and not
3738 // at the end. However, we aren't able to do so at the moment.
3739 llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen;
3740 NestedNameSpecifier *Qualifier = nullptr;
3741 for (SwitchCase *SC = Switch->getSwitchCaseList(); SC;
3742 SC = SC->getNextSwitchCase()) {
3743 CaseStmt *Case = dyn_cast<CaseStmt>(SC);
3747 Expr *CaseVal = Case->getLHS()->IgnoreParenCasts();
3748 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal))
3749 if (EnumConstantDecl *Enumerator
3750 = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
3751 // We look into the AST of the case statement to determine which
3752 // enumerator was named. Alternatively, we could compute the value of
3753 // the integral constant expression, then compare it against the
3754 // values of each enumerator. However, value-based approach would not
3755 // work as well with C++ templates where enumerators declared within a
3756 // template are type- and value-dependent.
3757 EnumeratorsSeen.insert(Enumerator);
3759 // If this is a qualified-id, keep track of the nested-name-specifier
3760 // so that we can reproduce it as part of code completion, e.g.,
3762 // switch (TagD.getKind()) {
3763 // case TagDecl::TK_enum:
3767 // At the XXX, our completions are TagDecl::TK_union,
3768 // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union,
3769 // TK_struct, and TK_class.
3770 Qualifier = DRE->getQualifier();
3774 if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) {
3775 // If there are no prior enumerators in C++, check whether we have to
3776 // qualify the names of the enumerators that we suggest, because they
3777 // may not be visible in this scope.
3778 Qualifier = getRequiredQualification(Context, CurContext, Enum);
3781 // Add any enumerators that have not yet been mentioned.
3782 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3783 CodeCompleter->getCodeCompletionTUInfo(),
3784 CodeCompletionContext::CCC_Expression);
3785 Results.EnterNewScope();
3786 for (auto *E : Enum->enumerators()) {
3787 if (EnumeratorsSeen.count(E))
3790 CodeCompletionResult R(E, CCP_EnumInCase, Qualifier);
3791 Results.AddResult(R, CurContext, nullptr, false);
3793 Results.ExitScope();
3795 //We need to make sure we're setting the right context,
3796 //so only say we include macros if the code completer says we do
3797 enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other;
3798 if (CodeCompleter->includeMacros()) {
3799 AddMacroResults(PP, Results, false);
3800 kind = CodeCompletionContext::CCC_OtherWithMacros;
3803 HandleCodeCompleteResults(this, CodeCompleter,
3805 Results.data(),Results.size());
3808 static bool anyNullArguments(ArrayRef<Expr *> Args) {
3809 if (Args.size() && !Args.data())
3812 for (unsigned I = 0; I != Args.size(); ++I)
3819 void Sema::CodeCompleteCall(Scope *S, Expr *FnIn, ArrayRef<Expr *> Args) {
3823 // When we're code-completing for a call, we fall back to ordinary
3824 // name code-completion whenever we can't produce specific
3825 // results. We may want to revisit this strategy in the future,
3826 // e.g., by merging the two kinds of results.
3828 Expr *Fn = (Expr *)FnIn;
3830 // Ignore type-dependent call expressions entirely.
3831 if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) ||
3832 Expr::hasAnyTypeDependentArguments(Args)) {
3833 CodeCompleteOrdinaryName(S, PCC_Expression);
3837 // Build an overload candidate set based on the functions we find.
3838 SourceLocation Loc = Fn->getExprLoc();
3839 OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal);
3841 // FIXME: What if we're calling something that isn't a function declaration?
3842 // FIXME: What if we're calling a pseudo-destructor?
3843 // FIXME: What if we're calling a member function?
3845 typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate;
3846 SmallVector<ResultCandidate, 8> Results;
3848 Expr *NakedFn = Fn->IgnoreParenCasts();
3849 if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn))
3850 AddOverloadedCallCandidates(ULE, Args, CandidateSet,
3851 /*PartialOverloading=*/ true);
3852 else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(NakedFn)) {
3853 FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
3855 if (!getLangOpts().CPlusPlus ||
3856 !FDecl->getType()->getAs<FunctionProtoType>())
3857 Results.push_back(ResultCandidate(FDecl));
3860 AddOverloadCandidate(FDecl, DeclAccessPair::make(FDecl, AS_none), Args,
3861 CandidateSet, false, /*PartialOverloading*/true);
3867 if (!CandidateSet.empty()) {
3868 // Sort the overload candidate set by placing the best overloads first.
3870 CandidateSet.begin(), CandidateSet.end(),
3871 [&](const OverloadCandidate &X, const OverloadCandidate &Y) {
3872 return isBetterOverloadCandidate(*this, X, Y, Loc);
3875 // Add the remaining viable overload candidates as code-completion reslults.
3876 for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
3877 CandEnd = CandidateSet.end();
3878 Cand != CandEnd; ++Cand) {
3880 Results.push_back(ResultCandidate(Cand->Function));
3883 // From the viable candidates, try to determine the type of this parameter.
3884 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
3885 if (const FunctionType *FType = Results[I].getFunctionType())
3886 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FType))
3887 if (Args.size() < Proto->getNumParams()) {
3888 if (ParamType.isNull())
3889 ParamType = Proto->getParamType(Args.size());
3890 else if (!Context.hasSameUnqualifiedType(
3891 ParamType.getNonReferenceType(),
3892 Proto->getParamType(Args.size())
3893 .getNonReferenceType())) {
3894 ParamType = QualType();
3900 // Try to determine the parameter type from the type of the expression
3902 QualType FunctionType = Fn->getType();
3903 if (const PointerType *Ptr = FunctionType->getAs<PointerType>())
3904 FunctionType = Ptr->getPointeeType();
3905 else if (const BlockPointerType *BlockPtr
3906 = FunctionType->getAs<BlockPointerType>())
3907 FunctionType = BlockPtr->getPointeeType();
3908 else if (const MemberPointerType *MemPtr
3909 = FunctionType->getAs<MemberPointerType>())
3910 FunctionType = MemPtr->getPointeeType();
3912 if (const FunctionProtoType *Proto
3913 = FunctionType->getAs<FunctionProtoType>()) {
3914 if (Args.size() < Proto->getNumParams())
3915 ParamType = Proto->getParamType(Args.size());
3919 if (ParamType.isNull())
3920 CodeCompleteOrdinaryName(S, PCC_Expression);
3922 CodeCompleteExpression(S, ParamType);
3924 if (!Results.empty())
3925 CodeCompleter->ProcessOverloadCandidates(*this, Args.size(), Results.data(),
3929 void Sema::CodeCompleteInitializer(Scope *S, Decl *D) {
3930 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D);
3932 CodeCompleteOrdinaryName(S, PCC_Expression);
3936 CodeCompleteExpression(S, VD->getType());
3939 void Sema::CodeCompleteReturn(Scope *S) {
3940 QualType ResultType;
3941 if (isa<BlockDecl>(CurContext)) {
3942 if (BlockScopeInfo *BSI = getCurBlock())
3943 ResultType = BSI->ReturnType;
3944 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext))
3945 ResultType = Function->getReturnType();
3946 else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext))
3947 ResultType = Method->getReturnType();
3949 if (ResultType.isNull())
3950 CodeCompleteOrdinaryName(S, PCC_Expression);
3952 CodeCompleteExpression(S, ResultType);
3955 void Sema::CodeCompleteAfterIf(Scope *S) {
3956 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3957 CodeCompleter->getCodeCompletionTUInfo(),
3958 mapCodeCompletionContext(*this, PCC_Statement));
3959 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3960 Results.EnterNewScope();
3962 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3963 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3964 CodeCompleter->includeGlobals());
3966 AddOrdinaryNameResults(PCC_Statement, S, *this, Results);
3969 CodeCompletionBuilder Builder(Results.getAllocator(),
3970 Results.getCodeCompletionTUInfo());
3971 Builder.AddTypedTextChunk("else");
3972 if (Results.includeCodePatterns()) {
3973 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3974 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
3975 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3976 Builder.AddPlaceholderChunk("statements");
3977 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3978 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
3980 Results.AddResult(Builder.TakeString());
3983 Builder.AddTypedTextChunk("else");
3984 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3985 Builder.AddTextChunk("if");
3986 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3987 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
3988 if (getLangOpts().CPlusPlus)
3989 Builder.AddPlaceholderChunk("condition");
3991 Builder.AddPlaceholderChunk("expression");
3992 Builder.AddChunk(CodeCompletionString::CK_RightParen);
3993 if (Results.includeCodePatterns()) {
3994 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
3995 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
3996 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3997 Builder.AddPlaceholderChunk("statements");
3998 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
3999 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4001 Results.AddResult(Builder.TakeString());
4003 Results.ExitScope();
4005 if (S->getFnParent())
4006 AddPrettyFunctionResults(PP.getLangOpts(), Results);
4008 if (CodeCompleter->includeMacros())
4009 AddMacroResults(PP, Results, false);
4011 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4012 Results.data(),Results.size());
4015 void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) {
4017 CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType());
4019 CodeCompleteOrdinaryName(S, PCC_Expression);
4022 void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS,
4023 bool EnteringContext) {
4024 if (!SS.getScopeRep() || !CodeCompleter)
4027 DeclContext *Ctx = computeDeclContext(SS, EnteringContext);
4031 // Try to instantiate any non-dependent declaration contexts before
4033 if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx))
4036 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4037 CodeCompleter->getCodeCompletionTUInfo(),
4038 CodeCompletionContext::CCC_Name);
4039 Results.EnterNewScope();
4041 // The "template" keyword can follow "::" in the grammar, but only
4042 // put it into the grammar if the nested-name-specifier is dependent.
4043 NestedNameSpecifier *NNS = SS.getScopeRep();
4044 if (!Results.empty() && NNS->isDependent())
4045 Results.AddResult("template");
4047 // Add calls to overridden virtual functions, if there are any.
4049 // FIXME: This isn't wonderful, because we don't know whether we're actually
4050 // in a context that permits expressions. This is a general issue with
4051 // qualified-id completions.
4052 if (!EnteringContext)
4053 MaybeAddOverrideCalls(*this, Ctx, Results);
4054 Results.ExitScope();
4056 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4057 LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer);
4059 HandleCodeCompleteResults(this, CodeCompleter,
4060 Results.getCompletionContext(),
4061 Results.data(),Results.size());
4064 void Sema::CodeCompleteUsing(Scope *S) {
4068 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4069 CodeCompleter->getCodeCompletionTUInfo(),
4070 CodeCompletionContext::CCC_PotentiallyQualifiedName,
4071 &ResultBuilder::IsNestedNameSpecifier);
4072 Results.EnterNewScope();
4074 // If we aren't in class scope, we could see the "namespace" keyword.
4075 if (!S->isClassScope())
4076 Results.AddResult(CodeCompletionResult("namespace"));
4078 // After "using", we can see anything that would start a
4079 // nested-name-specifier.
4080 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4081 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4082 CodeCompleter->includeGlobals());
4083 Results.ExitScope();
4085 HandleCodeCompleteResults(this, CodeCompleter,
4086 CodeCompletionContext::CCC_PotentiallyQualifiedName,
4087 Results.data(),Results.size());
4090 void Sema::CodeCompleteUsingDirective(Scope *S) {
4094 // After "using namespace", we expect to see a namespace name or namespace
4096 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4097 CodeCompleter->getCodeCompletionTUInfo(),
4098 CodeCompletionContext::CCC_Namespace,
4099 &ResultBuilder::IsNamespaceOrAlias);
4100 Results.EnterNewScope();
4101 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4102 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4103 CodeCompleter->includeGlobals());
4104 Results.ExitScope();
4105 HandleCodeCompleteResults(this, CodeCompleter,
4106 CodeCompletionContext::CCC_Namespace,
4107 Results.data(),Results.size());
4110 void Sema::CodeCompleteNamespaceDecl(Scope *S) {
4114 DeclContext *Ctx = S->getEntity();
4115 if (!S->getParent())
4116 Ctx = Context.getTranslationUnitDecl();
4118 bool SuppressedGlobalResults
4119 = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx);
4121 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4122 CodeCompleter->getCodeCompletionTUInfo(),
4123 SuppressedGlobalResults
4124 ? CodeCompletionContext::CCC_Namespace
4125 : CodeCompletionContext::CCC_Other,
4126 &ResultBuilder::IsNamespace);
4128 if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) {
4129 // We only want to see those namespaces that have already been defined
4130 // within this scope, because its likely that the user is creating an
4131 // extended namespace declaration. Keep track of the most recent
4132 // definition of each namespace.
4133 std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest;
4134 for (DeclContext::specific_decl_iterator<NamespaceDecl>
4135 NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end());
4137 OrigToLatest[NS->getOriginalNamespace()] = *NS;
4139 // Add the most recent definition (or extended definition) of each
4140 // namespace to the list of results.
4141 Results.EnterNewScope();
4142 for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator
4143 NS = OrigToLatest.begin(),
4144 NSEnd = OrigToLatest.end();
4146 Results.AddResult(CodeCompletionResult(
4147 NS->second, Results.getBasePriority(NS->second),
4149 CurContext, nullptr, false);
4150 Results.ExitScope();
4153 HandleCodeCompleteResults(this, CodeCompleter,
4154 Results.getCompletionContext(),
4155 Results.data(),Results.size());
4158 void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) {
4162 // After "namespace", we expect to see a namespace or alias.
4163 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4164 CodeCompleter->getCodeCompletionTUInfo(),
4165 CodeCompletionContext::CCC_Namespace,
4166 &ResultBuilder::IsNamespaceOrAlias);
4167 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4168 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4169 CodeCompleter->includeGlobals());
4170 HandleCodeCompleteResults(this, CodeCompleter,
4171 Results.getCompletionContext(),
4172 Results.data(),Results.size());
4175 void Sema::CodeCompleteOperatorName(Scope *S) {
4179 typedef CodeCompletionResult Result;
4180 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4181 CodeCompleter->getCodeCompletionTUInfo(),
4182 CodeCompletionContext::CCC_Type,
4183 &ResultBuilder::IsType);
4184 Results.EnterNewScope();
4186 // Add the names of overloadable operators.
4187 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
4188 if (std::strcmp(Spelling, "?")) \
4189 Results.AddResult(Result(Spelling));
4190 #include "clang/Basic/OperatorKinds.def"
4192 // Add any type names visible from the current scope
4193 Results.allowNestedNameSpecifiers();
4194 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4195 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4196 CodeCompleter->includeGlobals());
4198 // Add any type specifiers
4199 AddTypeSpecifierResults(getLangOpts(), Results);
4200 Results.ExitScope();
4202 HandleCodeCompleteResults(this, CodeCompleter,
4203 CodeCompletionContext::CCC_Type,
4204 Results.data(),Results.size());
4207 void Sema::CodeCompleteConstructorInitializer(
4209 ArrayRef <CXXCtorInitializer *> Initializers) {
4210 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
4211 CXXConstructorDecl *Constructor
4212 = static_cast<CXXConstructorDecl *>(ConstructorD);
4216 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4217 CodeCompleter->getCodeCompletionTUInfo(),
4218 CodeCompletionContext::CCC_PotentiallyQualifiedName);
4219 Results.EnterNewScope();
4221 // Fill in any already-initialized fields or base classes.
4222 llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields;
4223 llvm::SmallPtrSet<CanQualType, 4> InitializedBases;
4224 for (unsigned I = 0, E = Initializers.size(); I != E; ++I) {
4225 if (Initializers[I]->isBaseInitializer())
4226 InitializedBases.insert(
4227 Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0)));
4229 InitializedFields.insert(cast<FieldDecl>(
4230 Initializers[I]->getAnyMember()));
4233 // Add completions for base classes.
4234 CodeCompletionBuilder Builder(Results.getAllocator(),
4235 Results.getCodeCompletionTUInfo());
4236 bool SawLastInitializer = Initializers.empty();
4237 CXXRecordDecl *ClassDecl = Constructor->getParent();
4238 for (const auto &Base : ClassDecl->bases()) {
4239 if (!InitializedBases.insert(Context.getCanonicalType(Base.getType()))
4242 = !Initializers.empty() &&
4243 Initializers.back()->isBaseInitializer() &&
4244 Context.hasSameUnqualifiedType(Base.getType(),
4245 QualType(Initializers.back()->getBaseClass(), 0));
4249 Builder.AddTypedTextChunk(
4250 Results.getAllocator().CopyString(
4251 Base.getType().getAsString(Policy)));
4252 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4253 Builder.AddPlaceholderChunk("args");
4254 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4255 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
4256 SawLastInitializer? CCP_NextInitializer
4257 : CCP_MemberDeclaration));
4258 SawLastInitializer = false;
4261 // Add completions for virtual base classes.
4262 for (const auto &Base : ClassDecl->vbases()) {
4263 if (!InitializedBases.insert(Context.getCanonicalType(Base.getType()))
4266 = !Initializers.empty() &&
4267 Initializers.back()->isBaseInitializer() &&
4268 Context.hasSameUnqualifiedType(Base.getType(),
4269 QualType(Initializers.back()->getBaseClass(), 0));
4273 Builder.AddTypedTextChunk(
4274 Builder.getAllocator().CopyString(
4275 Base.getType().getAsString(Policy)));
4276 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4277 Builder.AddPlaceholderChunk("args");
4278 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4279 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
4280 SawLastInitializer? CCP_NextInitializer
4281 : CCP_MemberDeclaration));
4282 SawLastInitializer = false;
4285 // Add completions for members.
4286 for (auto *Field : ClassDecl->fields()) {
4287 if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))
4290 = !Initializers.empty() &&
4291 Initializers.back()->isAnyMemberInitializer() &&
4292 Initializers.back()->getAnyMember() == Field;
4296 if (!Field->getDeclName())
4299 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
4300 Field->getIdentifier()->getName()));
4301 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4302 Builder.AddPlaceholderChunk("args");
4303 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4304 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
4305 SawLastInitializer? CCP_NextInitializer
4306 : CCP_MemberDeclaration,
4308 CXAvailability_Available,
4310 SawLastInitializer = false;
4312 Results.ExitScope();
4314 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4315 Results.data(), Results.size());
4318 /// \brief Determine whether this scope denotes a namespace.
4319 static bool isNamespaceScope(Scope *S) {
4320 DeclContext *DC = S->getEntity();
4324 return DC->isFileContext();
4327 void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro,
4328 bool AfterAmpersand) {
4329 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4330 CodeCompleter->getCodeCompletionTUInfo(),
4331 CodeCompletionContext::CCC_Other);
4332 Results.EnterNewScope();
4334 // Note what has already been captured.
4335 llvm::SmallPtrSet<IdentifierInfo *, 4> Known;
4336 bool IncludedThis = false;
4337 for (const auto &C : Intro.Captures) {
4338 if (C.Kind == LCK_This) {
4339 IncludedThis = true;
4346 // Look for other capturable variables.
4347 for (; S && !isNamespaceScope(S); S = S->getParent()) {
4348 for (const auto *D : S->decls()) {
4349 const auto *Var = dyn_cast<VarDecl>(D);
4351 !Var->hasLocalStorage() ||
4352 Var->hasAttr<BlocksAttr>())
4355 if (Known.insert(Var->getIdentifier()).second)
4356 Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration),
4357 CurContext, nullptr, false);
4361 // Add 'this', if it would be valid.
4362 if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy)
4363 addThisCompletion(*this, Results);
4365 Results.ExitScope();
4367 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4368 Results.data(), Results.size());
4371 /// Macro that optionally prepends an "@" to the string literal passed in via
4372 /// Keyword, depending on whether NeedAt is true or false.
4373 #define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword)
4375 static void AddObjCImplementationResults(const LangOptions &LangOpts,
4376 ResultBuilder &Results,
4378 typedef CodeCompletionResult Result;
4379 // Since we have an implementation, we can end it.
4380 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
4382 CodeCompletionBuilder Builder(Results.getAllocator(),
4383 Results.getCodeCompletionTUInfo());
4384 if (LangOpts.ObjC2) {
4386 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic"));
4387 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4388 Builder.AddPlaceholderChunk("property");
4389 Results.AddResult(Result(Builder.TakeString()));
4392 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize"));
4393 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4394 Builder.AddPlaceholderChunk("property");
4395 Results.AddResult(Result(Builder.TakeString()));
4399 static void AddObjCInterfaceResults(const LangOptions &LangOpts,
4400 ResultBuilder &Results,
4402 typedef CodeCompletionResult Result;
4404 // Since we have an interface or protocol, we can end it.
4405 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
4407 if (LangOpts.ObjC2) {
4409 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property")));
4412 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required")));
4415 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional")));
4419 static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) {
4420 typedef CodeCompletionResult Result;
4421 CodeCompletionBuilder Builder(Results.getAllocator(),
4422 Results.getCodeCompletionTUInfo());
4425 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class"));
4426 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4427 Builder.AddPlaceholderChunk("name");
4428 Results.AddResult(Result(Builder.TakeString()));
4430 if (Results.includeCodePatterns()) {
4432 // FIXME: Could introduce the whole pattern, including superclasses and
4434 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface"));
4435 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4436 Builder.AddPlaceholderChunk("class");
4437 Results.AddResult(Result(Builder.TakeString()));
4440 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
4441 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4442 Builder.AddPlaceholderChunk("protocol");
4443 Results.AddResult(Result(Builder.TakeString()));
4445 // @implementation name
4446 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation"));
4447 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4448 Builder.AddPlaceholderChunk("class");
4449 Results.AddResult(Result(Builder.TakeString()));
4452 // @compatibility_alias name
4453 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias"));
4454 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4455 Builder.AddPlaceholderChunk("alias");
4456 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4457 Builder.AddPlaceholderChunk("class");
4458 Results.AddResult(Result(Builder.TakeString()));
4460 if (Results.getSema().getLangOpts().Modules) {
4462 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import"));
4463 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4464 Builder.AddPlaceholderChunk("module");
4465 Results.AddResult(Result(Builder.TakeString()));
4469 void Sema::CodeCompleteObjCAtDirective(Scope *S) {
4470 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4471 CodeCompleter->getCodeCompletionTUInfo(),
4472 CodeCompletionContext::CCC_Other);
4473 Results.EnterNewScope();
4474 if (isa<ObjCImplDecl>(CurContext))
4475 AddObjCImplementationResults(getLangOpts(), Results, false);
4476 else if (CurContext->isObjCContainer())
4477 AddObjCInterfaceResults(getLangOpts(), Results, false);
4479 AddObjCTopLevelResults(Results, false);
4480 Results.ExitScope();
4481 HandleCodeCompleteResults(this, CodeCompleter,
4482 CodeCompletionContext::CCC_Other,
4483 Results.data(),Results.size());
4486 static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) {
4487 typedef CodeCompletionResult Result;
4488 CodeCompletionBuilder Builder(Results.getAllocator(),
4489 Results.getCodeCompletionTUInfo());
4491 // @encode ( type-name )
4492 const char *EncodeType = "char[]";
4493 if (Results.getSema().getLangOpts().CPlusPlus ||
4494 Results.getSema().getLangOpts().ConstStrings)
4495 EncodeType = "const char[]";
4496 Builder.AddResultTypeChunk(EncodeType);
4497 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode"));
4498 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4499 Builder.AddPlaceholderChunk("type-name");
4500 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4501 Results.AddResult(Result(Builder.TakeString()));
4503 // @protocol ( protocol-name )
4504 Builder.AddResultTypeChunk("Protocol *");
4505 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
4506 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4507 Builder.AddPlaceholderChunk("protocol-name");
4508 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4509 Results.AddResult(Result(Builder.TakeString()));
4511 // @selector ( selector )
4512 Builder.AddResultTypeChunk("SEL");
4513 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector"));
4514 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4515 Builder.AddPlaceholderChunk("selector");
4516 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4517 Results.AddResult(Result(Builder.TakeString()));
4520 Builder.AddResultTypeChunk("NSString *");
4521 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\""));
4522 Builder.AddPlaceholderChunk("string");
4523 Builder.AddTextChunk("\"");
4524 Results.AddResult(Result(Builder.TakeString()));
4527 Builder.AddResultTypeChunk("NSArray *");
4528 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"["));
4529 Builder.AddPlaceholderChunk("objects, ...");
4530 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
4531 Results.AddResult(Result(Builder.TakeString()));
4533 // @{key : object, ...}
4534 Builder.AddResultTypeChunk("NSDictionary *");
4535 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{"));
4536 Builder.AddPlaceholderChunk("key");
4537 Builder.AddChunk(CodeCompletionString::CK_Colon);
4538 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4539 Builder.AddPlaceholderChunk("object, ...");
4540 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4541 Results.AddResult(Result(Builder.TakeString()));
4544 Builder.AddResultTypeChunk("id");
4545 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "("));
4546 Builder.AddPlaceholderChunk("expression");
4547 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4548 Results.AddResult(Result(Builder.TakeString()));
4551 static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) {
4552 typedef CodeCompletionResult Result;
4553 CodeCompletionBuilder Builder(Results.getAllocator(),
4554 Results.getCodeCompletionTUInfo());
4556 if (Results.includeCodePatterns()) {
4557 // @try { statements } @catch ( declaration ) { statements } @finally
4559 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try"));
4560 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4561 Builder.AddPlaceholderChunk("statements");
4562 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4563 Builder.AddTextChunk("@catch");
4564 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4565 Builder.AddPlaceholderChunk("parameter");
4566 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4567 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4568 Builder.AddPlaceholderChunk("statements");
4569 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4570 Builder.AddTextChunk("@finally");
4571 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4572 Builder.AddPlaceholderChunk("statements");
4573 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4574 Results.AddResult(Result(Builder.TakeString()));
4578 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw"));
4579 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4580 Builder.AddPlaceholderChunk("expression");
4581 Results.AddResult(Result(Builder.TakeString()));
4583 if (Results.includeCodePatterns()) {
4584 // @synchronized ( expression ) { statements }
4585 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized"));
4586 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4587 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4588 Builder.AddPlaceholderChunk("expression");
4589 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4590 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4591 Builder.AddPlaceholderChunk("statements");
4592 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4593 Results.AddResult(Result(Builder.TakeString()));
4597 static void AddObjCVisibilityResults(const LangOptions &LangOpts,
4598 ResultBuilder &Results,
4600 typedef CodeCompletionResult Result;
4601 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private")));
4602 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected")));
4603 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public")));
4605 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package")));
4608 void Sema::CodeCompleteObjCAtVisibility(Scope *S) {
4609 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4610 CodeCompleter->getCodeCompletionTUInfo(),
4611 CodeCompletionContext::CCC_Other);
4612 Results.EnterNewScope();
4613 AddObjCVisibilityResults(getLangOpts(), Results, false);
4614 Results.ExitScope();
4615 HandleCodeCompleteResults(this, CodeCompleter,
4616 CodeCompletionContext::CCC_Other,
4617 Results.data(),Results.size());
4620 void Sema::CodeCompleteObjCAtStatement(Scope *S) {
4621 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4622 CodeCompleter->getCodeCompletionTUInfo(),
4623 CodeCompletionContext::CCC_Other);
4624 Results.EnterNewScope();
4625 AddObjCStatementResults(Results, false);
4626 AddObjCExpressionResults(Results, false);
4627 Results.ExitScope();
4628 HandleCodeCompleteResults(this, CodeCompleter,
4629 CodeCompletionContext::CCC_Other,
4630 Results.data(),Results.size());
4633 void Sema::CodeCompleteObjCAtExpression(Scope *S) {
4634 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4635 CodeCompleter->getCodeCompletionTUInfo(),
4636 CodeCompletionContext::CCC_Other);
4637 Results.EnterNewScope();
4638 AddObjCExpressionResults(Results, false);
4639 Results.ExitScope();
4640 HandleCodeCompleteResults(this, CodeCompleter,
4641 CodeCompletionContext::CCC_Other,
4642 Results.data(),Results.size());
4645 /// \brief Determine whether the addition of the given flag to an Objective-C
4646 /// property's attributes will cause a conflict.
4647 static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
4648 // Check if we've already added this flag.
4649 if (Attributes & NewFlag)
4652 Attributes |= NewFlag;
4654 // Check for collisions with "readonly".
4655 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
4656 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
4659 // Check for more than one of { assign, copy, retain, strong, weak }.
4660 unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign |
4661 ObjCDeclSpec::DQ_PR_unsafe_unretained |
4662 ObjCDeclSpec::DQ_PR_copy |
4663 ObjCDeclSpec::DQ_PR_retain |
4664 ObjCDeclSpec::DQ_PR_strong |
4665 ObjCDeclSpec::DQ_PR_weak);
4666 if (AssignCopyRetMask &&
4667 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign &&
4668 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained &&
4669 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy &&
4670 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain &&
4671 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong &&
4672 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak)
4678 void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
4682 unsigned Attributes = ODS.getPropertyAttributes();
4684 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4685 CodeCompleter->getCodeCompletionTUInfo(),
4686 CodeCompletionContext::CCC_Other);
4687 Results.EnterNewScope();
4688 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly))
4689 Results.AddResult(CodeCompletionResult("readonly"));
4690 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign))
4691 Results.AddResult(CodeCompletionResult("assign"));
4692 if (!ObjCPropertyFlagConflicts(Attributes,
4693 ObjCDeclSpec::DQ_PR_unsafe_unretained))
4694 Results.AddResult(CodeCompletionResult("unsafe_unretained"));
4695 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite))
4696 Results.AddResult(CodeCompletionResult("readwrite"));
4697 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain))
4698 Results.AddResult(CodeCompletionResult("retain"));
4699 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong))
4700 Results.AddResult(CodeCompletionResult("strong"));
4701 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy))
4702 Results.AddResult(CodeCompletionResult("copy"));
4703 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic))
4704 Results.AddResult(CodeCompletionResult("nonatomic"));
4705 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic))
4706 Results.AddResult(CodeCompletionResult("atomic"));
4708 // Only suggest "weak" if we're compiling for ARC-with-weak-references or GC.
4709 if (getLangOpts().ObjCARCWeak || getLangOpts().getGC() != LangOptions::NonGC)
4710 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak))
4711 Results.AddResult(CodeCompletionResult("weak"));
4713 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) {
4714 CodeCompletionBuilder Setter(Results.getAllocator(),
4715 Results.getCodeCompletionTUInfo());
4716 Setter.AddTypedTextChunk("setter");
4717 Setter.AddTextChunk("=");
4718 Setter.AddPlaceholderChunk("method");
4719 Results.AddResult(CodeCompletionResult(Setter.TakeString()));
4721 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) {
4722 CodeCompletionBuilder Getter(Results.getAllocator(),
4723 Results.getCodeCompletionTUInfo());
4724 Getter.AddTypedTextChunk("getter");
4725 Getter.AddTextChunk("=");
4726 Getter.AddPlaceholderChunk("method");
4727 Results.AddResult(CodeCompletionResult(Getter.TakeString()));
4729 Results.ExitScope();
4730 HandleCodeCompleteResults(this, CodeCompleter,
4731 CodeCompletionContext::CCC_Other,
4732 Results.data(),Results.size());
4735 /// \brief Describes the kind of Objective-C method that we want to find
4736 /// via code completion.
4737 enum ObjCMethodKind {
4738 MK_Any, ///< Any kind of method, provided it means other specified criteria.
4739 MK_ZeroArgSelector, ///< Zero-argument (unary) selector.
4740 MK_OneArgSelector ///< One-argument selector.
4743 static bool isAcceptableObjCSelector(Selector Sel,
4744 ObjCMethodKind WantKind,
4745 ArrayRef<IdentifierInfo *> SelIdents,
4746 bool AllowSameLength = true) {
4747 unsigned NumSelIdents = SelIdents.size();
4748 if (NumSelIdents > Sel.getNumArgs())
4753 case MK_ZeroArgSelector: return Sel.isUnarySelector();
4754 case MK_OneArgSelector: return Sel.getNumArgs() == 1;
4757 if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs())
4760 for (unsigned I = 0; I != NumSelIdents; ++I)
4761 if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
4767 static bool isAcceptableObjCMethod(ObjCMethodDecl *Method,
4768 ObjCMethodKind WantKind,
4769 ArrayRef<IdentifierInfo *> SelIdents,
4770 bool AllowSameLength = true) {
4771 return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents,
4776 /// \brief A set of selectors, which is used to avoid introducing multiple
4777 /// completions with the same selector into the result set.
4778 typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet;
4781 /// \brief Add all of the Objective-C methods in the given Objective-C
4782 /// container to the set of results.
4784 /// The container will be a class, protocol, category, or implementation of
4785 /// any of the above. This mether will recurse to include methods from
4786 /// the superclasses of classes along with their categories, protocols, and
4787 /// implementations.
4789 /// \param Container the container in which we'll look to find methods.
4791 /// \param WantInstanceMethods Whether to add instance methods (only); if
4792 /// false, this routine will add factory methods (only).
4794 /// \param CurContext the context in which we're performing the lookup that
4797 /// \param AllowSameLength Whether we allow a method to be added to the list
4798 /// when it has the same number of parameters as we have selector identifiers.
4800 /// \param Results the structure into which we'll add results.
4801 static void AddObjCMethods(ObjCContainerDecl *Container,
4802 bool WantInstanceMethods,
4803 ObjCMethodKind WantKind,
4804 ArrayRef<IdentifierInfo *> SelIdents,
4805 DeclContext *CurContext,
4806 VisitedSelectorSet &Selectors,
4807 bool AllowSameLength,
4808 ResultBuilder &Results,
4809 bool InOriginalClass = true) {
4810 typedef CodeCompletionResult Result;
4811 Container = getContainerDef(Container);
4812 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
4813 bool isRootClass = IFace && !IFace->getSuperClass();
4814 for (auto *M : Container->methods()) {
4815 // The instance methods on the root class can be messaged via the
4817 if (M->isInstanceMethod() == WantInstanceMethods ||
4818 (isRootClass && !WantInstanceMethods)) {
4819 // Check whether the selector identifiers we've been given are a
4820 // subset of the identifiers for this particular method.
4821 if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength))
4824 if (!Selectors.insert(M->getSelector()).second)
4827 Result R = Result(M, Results.getBasePriority(M), nullptr);
4828 R.StartParameter = SelIdents.size();
4829 R.AllParametersAreInformative = (WantKind != MK_Any);
4830 if (!InOriginalClass)
4831 R.Priority += CCD_InBaseClass;
4832 Results.MaybeAddResult(R, CurContext);
4836 // Visit the protocols of protocols.
4837 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
4838 if (Protocol->hasDefinition()) {
4839 const ObjCList<ObjCProtocolDecl> &Protocols
4840 = Protocol->getReferencedProtocols();
4841 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
4842 E = Protocols.end();
4844 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
4845 CurContext, Selectors, AllowSameLength, Results, false);
4849 if (!IFace || !IFace->hasDefinition())
4852 // Add methods in protocols.
4853 for (auto *I : IFace->protocols())
4854 AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents,
4855 CurContext, Selectors, AllowSameLength, Results, false);
4857 // Add methods in categories.
4858 for (auto *CatDecl : IFace->known_categories()) {
4859 AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents,
4860 CurContext, Selectors, AllowSameLength,
4861 Results, InOriginalClass);
4863 // Add a categories protocol methods.
4864 const ObjCList<ObjCProtocolDecl> &Protocols
4865 = CatDecl->getReferencedProtocols();
4866 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
4867 E = Protocols.end();
4869 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
4870 CurContext, Selectors, AllowSameLength,
4873 // Add methods in category implementations.
4874 if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
4875 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
4876 CurContext, Selectors, AllowSameLength,
4877 Results, InOriginalClass);
4880 // Add methods in superclass.
4881 if (IFace->getSuperClass())
4882 AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind,
4883 SelIdents, CurContext, Selectors,
4884 AllowSameLength, Results, false);
4886 // Add methods in our implementation, if any.
4887 if (ObjCImplementationDecl *Impl = IFace->getImplementation())
4888 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
4889 CurContext, Selectors, AllowSameLength,
4890 Results, InOriginalClass);
4894 void Sema::CodeCompleteObjCPropertyGetter(Scope *S) {
4895 // Try to find the interface where getters might live.
4896 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
4898 if (ObjCCategoryDecl *Category
4899 = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
4900 Class = Category->getClassInterface();
4906 // Find all of the potential getters.
4907 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4908 CodeCompleter->getCodeCompletionTUInfo(),
4909 CodeCompletionContext::CCC_Other);
4910 Results.EnterNewScope();
4912 VisitedSelectorSet Selectors;
4913 AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors,
4914 /*AllowSameLength=*/true, Results);
4915 Results.ExitScope();
4916 HandleCodeCompleteResults(this, CodeCompleter,
4917 CodeCompletionContext::CCC_Other,
4918 Results.data(),Results.size());
4921 void Sema::CodeCompleteObjCPropertySetter(Scope *S) {
4922 // Try to find the interface where setters might live.
4923 ObjCInterfaceDecl *Class
4924 = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
4926 if (ObjCCategoryDecl *Category
4927 = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
4928 Class = Category->getClassInterface();
4934 // Find all of the potential getters.
4935 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4936 CodeCompleter->getCodeCompletionTUInfo(),
4937 CodeCompletionContext::CCC_Other);
4938 Results.EnterNewScope();
4940 VisitedSelectorSet Selectors;
4941 AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext,
4942 Selectors, /*AllowSameLength=*/true, Results);
4944 Results.ExitScope();
4945 HandleCodeCompleteResults(this, CodeCompleter,
4946 CodeCompletionContext::CCC_Other,
4947 Results.data(),Results.size());
4950 void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS,
4952 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
4953 CodeCompleter->getCodeCompletionTUInfo(),
4954 CodeCompletionContext::CCC_Type);
4955 Results.EnterNewScope();
4957 // Add context-sensitive, Objective-C parameter-passing keywords.
4958 bool AddedInOut = false;
4959 if ((DS.getObjCDeclQualifier() &
4960 (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) {
4961 Results.AddResult("in");
4962 Results.AddResult("inout");
4965 if ((DS.getObjCDeclQualifier() &
4966 (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) {
4967 Results.AddResult("out");
4969 Results.AddResult("inout");
4971 if ((DS.getObjCDeclQualifier() &
4972 (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref |
4973 ObjCDeclSpec::DQ_Oneway)) == 0) {
4974 Results.AddResult("bycopy");
4975 Results.AddResult("byref");
4976 Results.AddResult("oneway");
4979 // If we're completing the return type of an Objective-C method and the
4980 // identifier IBAction refers to a macro, provide a completion item for
4982 // IBAction)<#selector#>:(id)sender
4983 if (DS.getObjCDeclQualifier() == 0 && !IsParameter &&
4984 Context.Idents.get("IBAction").hasMacroDefinition()) {
4985 CodeCompletionBuilder Builder(Results.getAllocator(),
4986 Results.getCodeCompletionTUInfo(),
4987 CCP_CodePattern, CXAvailability_Available);
4988 Builder.AddTypedTextChunk("IBAction");
4989 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4990 Builder.AddPlaceholderChunk("selector");
4991 Builder.AddChunk(CodeCompletionString::CK_Colon);
4992 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4993 Builder.AddTextChunk("id");
4994 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4995 Builder.AddTextChunk("sender");
4996 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
4999 // If we're completing the return type, provide 'instancetype'.
5001 Results.AddResult(CodeCompletionResult("instancetype"));
5004 // Add various builtin type names and specifiers.
5005 AddOrdinaryNameResults(PCC_Type, S, *this, Results);
5006 Results.ExitScope();
5008 // Add the various type names
5009 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
5010 CodeCompletionDeclConsumer Consumer(Results, CurContext);
5011 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5012 CodeCompleter->includeGlobals());
5014 if (CodeCompleter->includeMacros())
5015 AddMacroResults(PP, Results, false);
5017 HandleCodeCompleteResults(this, CodeCompleter,
5018 CodeCompletionContext::CCC_Type,
5019 Results.data(), Results.size());
5022 /// \brief When we have an expression with type "id", we may assume
5023 /// that it has some more-specific class type based on knowledge of
5024 /// common uses of Objective-C. This routine returns that class type,
5025 /// or NULL if no better result could be determined.
5026 static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) {
5027 ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E);
5031 Selector Sel = Msg->getSelector();
5035 IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0);
5039 ObjCMethodDecl *Method = Msg->getMethodDecl();
5043 // Determine the class that we're sending the message to.
5044 ObjCInterfaceDecl *IFace = nullptr;
5045 switch (Msg->getReceiverKind()) {
5046 case ObjCMessageExpr::Class:
5047 if (const ObjCObjectType *ObjType
5048 = Msg->getClassReceiver()->getAs<ObjCObjectType>())
5049 IFace = ObjType->getInterface();
5052 case ObjCMessageExpr::Instance: {
5053 QualType T = Msg->getInstanceReceiver()->getType();
5054 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
5055 IFace = Ptr->getInterfaceDecl();
5059 case ObjCMessageExpr::SuperInstance:
5060 case ObjCMessageExpr::SuperClass:
5067 ObjCInterfaceDecl *Super = IFace->getSuperClass();
5068 if (Method->isInstanceMethod())
5069 return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5070 .Case("retain", IFace)
5071 .Case("strong", IFace)
5072 .Case("autorelease", IFace)
5073 .Case("copy", IFace)
5074 .Case("copyWithZone", IFace)
5075 .Case("mutableCopy", IFace)
5076 .Case("mutableCopyWithZone", IFace)
5077 .Case("awakeFromCoder", IFace)
5078 .Case("replacementObjectFromCoder", IFace)
5079 .Case("class", IFace)
5080 .Case("classForCoder", IFace)
5081 .Case("superclass", Super)
5084 return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5086 .Case("alloc", IFace)
5087 .Case("allocWithZone", IFace)
5088 .Case("class", IFace)
5089 .Case("superclass", Super)
5093 // Add a special completion for a message send to "super", which fills in the
5094 // most likely case of forwarding all of our arguments to the superclass
5097 /// \param S The semantic analysis object.
5099 /// \param NeedSuperKeyword Whether we need to prefix this completion with
5100 /// the "super" keyword. Otherwise, we just need to provide the arguments.
5102 /// \param SelIdents The identifiers in the selector that have already been
5103 /// provided as arguments for a send to "super".
5105 /// \param Results The set of results to augment.
5107 /// \returns the Objective-C method declaration that would be invoked by
5108 /// this "super" completion. If NULL, no completion was added.
5109 static ObjCMethodDecl *AddSuperSendCompletion(
5110 Sema &S, bool NeedSuperKeyword,
5111 ArrayRef<IdentifierInfo *> SelIdents,
5112 ResultBuilder &Results) {
5113 ObjCMethodDecl *CurMethod = S.getCurMethodDecl();
5117 ObjCInterfaceDecl *Class = CurMethod->getClassInterface();
5121 // Try to find a superclass method with the same selector.
5122 ObjCMethodDecl *SuperMethod = nullptr;
5123 while ((Class = Class->getSuperClass()) && !SuperMethod) {
5124 // Check in the class
5125 SuperMethod = Class->getMethod(CurMethod->getSelector(),
5126 CurMethod->isInstanceMethod());
5128 // Check in categories or class extensions.
5130 for (const auto *Cat : Class->known_categories()) {
5131 if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(),
5132 CurMethod->isInstanceMethod())))
5141 // Check whether the superclass method has the same signature.
5142 if (CurMethod->param_size() != SuperMethod->param_size() ||
5143 CurMethod->isVariadic() != SuperMethod->isVariadic())
5146 for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(),
5147 CurPEnd = CurMethod->param_end(),
5148 SuperP = SuperMethod->param_begin();
5149 CurP != CurPEnd; ++CurP, ++SuperP) {
5150 // Make sure the parameter types are compatible.
5151 if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(),
5152 (*SuperP)->getType()))
5155 // Make sure we have a parameter name to forward!
5156 if (!(*CurP)->getIdentifier())
5160 // We have a superclass method. Now, form the send-to-super completion.
5161 CodeCompletionBuilder Builder(Results.getAllocator(),
5162 Results.getCodeCompletionTUInfo());
5164 // Give this completion a return type.
5165 AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod,
5168 // If we need the "super" keyword, add it (plus some spacing).
5169 if (NeedSuperKeyword) {
5170 Builder.AddTypedTextChunk("super");
5171 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
5174 Selector Sel = CurMethod->getSelector();
5175 if (Sel.isUnarySelector()) {
5176 if (NeedSuperKeyword)
5177 Builder.AddTextChunk(Builder.getAllocator().CopyString(
5178 Sel.getNameForSlot(0)));
5180 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
5181 Sel.getNameForSlot(0)));
5183 ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin();
5184 for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) {
5185 if (I > SelIdents.size())
5186 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
5188 if (I < SelIdents.size())
5189 Builder.AddInformativeChunk(
5190 Builder.getAllocator().CopyString(
5191 Sel.getNameForSlot(I) + ":"));
5192 else if (NeedSuperKeyword || I > SelIdents.size()) {
5193 Builder.AddTextChunk(
5194 Builder.getAllocator().CopyString(
5195 Sel.getNameForSlot(I) + ":"));
5196 Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
5197 (*CurP)->getIdentifier()->getName()));
5199 Builder.AddTypedTextChunk(
5200 Builder.getAllocator().CopyString(
5201 Sel.getNameForSlot(I) + ":"));
5202 Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
5203 (*CurP)->getIdentifier()->getName()));
5208 Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod,
5209 CCP_SuperCompletion));
5213 void Sema::CodeCompleteObjCMessageReceiver(Scope *S) {
5214 typedef CodeCompletionResult Result;
5215 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5216 CodeCompleter->getCodeCompletionTUInfo(),
5217 CodeCompletionContext::CCC_ObjCMessageReceiver,
5218 getLangOpts().CPlusPlus11
5219 ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture
5220 : &ResultBuilder::IsObjCMessageReceiver);
5222 CodeCompletionDeclConsumer Consumer(Results, CurContext);
5223 Results.EnterNewScope();
5224 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5225 CodeCompleter->includeGlobals());
5227 // If we are in an Objective-C method inside a class that has a superclass,
5228 // add "super" as an option.
5229 if (ObjCMethodDecl *Method = getCurMethodDecl())
5230 if (ObjCInterfaceDecl *Iface = Method->getClassInterface())
5231 if (Iface->getSuperClass()) {
5232 Results.AddResult(Result("super"));
5234 AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results);
5237 if (getLangOpts().CPlusPlus11)
5238 addThisCompletion(*this, Results);
5240 Results.ExitScope();
5242 if (CodeCompleter->includeMacros())
5243 AddMacroResults(PP, Results, false);
5244 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
5245 Results.data(), Results.size());
5249 void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc,
5250 ArrayRef<IdentifierInfo *> SelIdents,
5251 bool AtArgumentExpression) {
5252 ObjCInterfaceDecl *CDecl = nullptr;
5253 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5254 // Figure out which interface we're in.
5255 CDecl = CurMethod->getClassInterface();
5259 // Find the superclass of this class.
5260 CDecl = CDecl->getSuperClass();
5264 if (CurMethod->isInstanceMethod()) {
5265 // We are inside an instance method, which means that the message
5266 // send [super ...] is actually calling an instance method on the
5268 return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents,
5269 AtArgumentExpression,
5273 // Fall through to send to the superclass in CDecl.
5275 // "super" may be the name of a type or variable. Figure out which
5277 IdentifierInfo *Super = getSuperIdentifier();
5278 NamedDecl *ND = LookupSingleName(S, Super, SuperLoc,
5279 LookupOrdinaryName);
5280 if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) {
5281 // "super" names an interface. Use it.
5282 } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) {
5283 if (const ObjCObjectType *Iface
5284 = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>())
5285 CDecl = Iface->getInterface();
5286 } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) {
5287 // "super" names an unresolved type; we can't be more specific.
5289 // Assume that "super" names some kind of value and parse that way.
5291 SourceLocation TemplateKWLoc;
5293 id.setIdentifier(Super, SuperLoc);
5294 ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id,
5296 return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(),
5298 AtArgumentExpression);
5304 ParsedType Receiver;
5306 Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl));
5307 return CodeCompleteObjCClassMessage(S, Receiver, SelIdents,
5308 AtArgumentExpression,
5312 /// \brief Given a set of code-completion results for the argument of a message
5313 /// send, determine the preferred type (if any) for that argument expression.
5314 static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results,
5315 unsigned NumSelIdents) {
5316 typedef CodeCompletionResult Result;
5317 ASTContext &Context = Results.getSema().Context;
5319 QualType PreferredType;
5320 unsigned BestPriority = CCP_Unlikely * 2;
5321 Result *ResultsData = Results.data();
5322 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
5323 Result &R = ResultsData[I];
5324 if (R.Kind == Result::RK_Declaration &&
5325 isa<ObjCMethodDecl>(R.Declaration)) {
5326 if (R.Priority <= BestPriority) {
5327 const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration);
5328 if (NumSelIdents <= Method->param_size()) {
5329 QualType MyPreferredType = Method->parameters()[NumSelIdents - 1]
5331 if (R.Priority < BestPriority || PreferredType.isNull()) {
5332 BestPriority = R.Priority;
5333 PreferredType = MyPreferredType;
5334 } else if (!Context.hasSameUnqualifiedType(PreferredType,
5336 PreferredType = QualType();
5343 return PreferredType;
5346 static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
5347 ParsedType Receiver,
5348 ArrayRef<IdentifierInfo *> SelIdents,
5349 bool AtArgumentExpression,
5351 ResultBuilder &Results) {
5352 typedef CodeCompletionResult Result;
5353 ObjCInterfaceDecl *CDecl = nullptr;
5355 // If the given name refers to an interface type, retrieve the
5356 // corresponding declaration.
5358 QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr);
5360 if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>())
5361 CDecl = Interface->getInterface();
5364 // Add all of the factory methods in this Objective-C class, its protocols,
5365 // superclasses, categories, implementation, etc.
5366 Results.EnterNewScope();
5368 // If this is a send-to-super, try to add the special "super" send
5371 if (ObjCMethodDecl *SuperMethod
5372 = AddSuperSendCompletion(SemaRef, false, SelIdents, Results))
5373 Results.Ignore(SuperMethod);
5376 // If we're inside an Objective-C method definition, prefer its selector to
5378 if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl())
5379 Results.setPreferredSelector(CurMethod->getSelector());
5381 VisitedSelectorSet Selectors;
5383 AddObjCMethods(CDecl, false, MK_Any, SelIdents,
5384 SemaRef.CurContext, Selectors, AtArgumentExpression,
5387 // We're messaging "id" as a type; provide all class/factory methods.
5389 // If we have an external source, load the entire class method
5390 // pool from the AST file.
5391 if (SemaRef.getExternalSource()) {
5392 for (uint32_t I = 0,
5393 N = SemaRef.getExternalSource()->GetNumExternalSelectors();
5395 Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I);
5396 if (Sel.isNull() || SemaRef.MethodPool.count(Sel))
5399 SemaRef.ReadMethodPool(Sel);
5403 for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(),
5404 MEnd = SemaRef.MethodPool.end();
5406 for (ObjCMethodList *MethList = &M->second.second;
5407 MethList && MethList->getMethod();
5408 MethList = MethList->getNext()) {
5409 if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
5412 Result R(MethList->getMethod(),
5413 Results.getBasePriority(MethList->getMethod()), nullptr);
5414 R.StartParameter = SelIdents.size();
5415 R.AllParametersAreInformative = false;
5416 Results.MaybeAddResult(R, SemaRef.CurContext);
5421 Results.ExitScope();
5424 void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver,
5425 ArrayRef<IdentifierInfo *> SelIdents,
5426 bool AtArgumentExpression,
5429 QualType T = this->GetTypeFromParser(Receiver);
5431 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5432 CodeCompleter->getCodeCompletionTUInfo(),
5433 CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage,
5436 AddClassMessageCompletions(*this, S, Receiver, SelIdents,
5437 AtArgumentExpression, IsSuper, Results);
5439 // If we're actually at the argument expression (rather than prior to the
5440 // selector), we're actually performing code completion for an expression.
5441 // Determine whether we have a single, best method. If so, we can
5442 // code-complete the expression using the corresponding parameter type as
5443 // our preferred type, improving completion results.
5444 if (AtArgumentExpression) {
5445 QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
5447 if (PreferredType.isNull())
5448 CodeCompleteOrdinaryName(S, PCC_Expression);
5450 CodeCompleteExpression(S, PreferredType);
5454 HandleCodeCompleteResults(this, CodeCompleter,
5455 Results.getCompletionContext(),
5456 Results.data(), Results.size());
5459 void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver,
5460 ArrayRef<IdentifierInfo *> SelIdents,
5461 bool AtArgumentExpression,
5462 ObjCInterfaceDecl *Super) {
5463 typedef CodeCompletionResult Result;
5465 Expr *RecExpr = static_cast<Expr *>(Receiver);
5467 // If necessary, apply function/array conversion to the receiver.
5468 // C99 6.7.5.3p[7,8].
5470 ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr);
5471 if (Conv.isInvalid()) // conversion failed. bail.
5473 RecExpr = Conv.get();
5475 QualType ReceiverType = RecExpr? RecExpr->getType()
5476 : Super? Context.getObjCObjectPointerType(
5477 Context.getObjCInterfaceType(Super))
5478 : Context.getObjCIdType();
5480 // If we're messaging an expression with type "id" or "Class", check
5481 // whether we know something special about the receiver that allows
5482 // us to assume a more-specific receiver type.
5483 if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) {
5484 if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) {
5485 if (ReceiverType->isObjCClassType())
5486 return CodeCompleteObjCClassMessage(S,
5487 ParsedType::make(Context.getObjCInterfaceType(IFace)),
5489 AtArgumentExpression, Super);
5491 ReceiverType = Context.getObjCObjectPointerType(
5492 Context.getObjCInterfaceType(IFace));
5494 } else if (RecExpr && getLangOpts().CPlusPlus) {
5495 ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr);
5496 if (Conv.isUsable()) {
5497 RecExpr = Conv.get();
5498 ReceiverType = RecExpr->getType();
5502 // Build the set of methods we can see.
5503 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5504 CodeCompleter->getCodeCompletionTUInfo(),
5505 CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage,
5506 ReceiverType, SelIdents));
5508 Results.EnterNewScope();
5510 // If this is a send-to-super, try to add the special "super" send
5513 if (ObjCMethodDecl *SuperMethod
5514 = AddSuperSendCompletion(*this, false, SelIdents, Results))
5515 Results.Ignore(SuperMethod);
5518 // If we're inside an Objective-C method definition, prefer its selector to
5520 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
5521 Results.setPreferredSelector(CurMethod->getSelector());
5523 // Keep track of the selectors we've already added.
5524 VisitedSelectorSet Selectors;
5526 // Handle messages to Class. This really isn't a message to an instance
5527 // method, so we treat it the same way we would treat a message send to a
5529 if (ReceiverType->isObjCClassType() ||
5530 ReceiverType->isObjCQualifiedClassType()) {
5531 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5532 if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
5533 AddObjCMethods(ClassDecl, false, MK_Any, SelIdents,
5534 CurContext, Selectors, AtArgumentExpression, Results);
5537 // Handle messages to a qualified ID ("id<foo>").
5538 else if (const ObjCObjectPointerType *QualID
5539 = ReceiverType->getAsObjCQualifiedIdType()) {
5540 // Search protocols for instance methods.
5541 for (auto *I : QualID->quals())
5542 AddObjCMethods(I, true, MK_Any, SelIdents, CurContext,
5543 Selectors, AtArgumentExpression, Results);
5545 // Handle messages to a pointer to interface type.
5546 else if (const ObjCObjectPointerType *IFacePtr
5547 = ReceiverType->getAsObjCInterfacePointerType()) {
5548 // Search the class, its superclasses, etc., for instance methods.
5549 AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents,
5550 CurContext, Selectors, AtArgumentExpression,
5553 // Search protocols for instance methods.
5554 for (auto *I : IFacePtr->quals())
5555 AddObjCMethods(I, true, MK_Any, SelIdents, CurContext,
5556 Selectors, AtArgumentExpression, Results);
5558 // Handle messages to "id".
5559 else if (ReceiverType->isObjCIdType()) {
5560 // We're messaging "id", so provide all instance methods we know
5561 // about as code-completion results.
5563 // If we have an external source, load the entire class method
5564 // pool from the AST file.
5565 if (ExternalSource) {
5566 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5568 Selector Sel = ExternalSource->GetExternalSelector(I);
5569 if (Sel.isNull() || MethodPool.count(Sel))
5572 ReadMethodPool(Sel);
5576 for (GlobalMethodPool::iterator M = MethodPool.begin(),
5577 MEnd = MethodPool.end();
5579 for (ObjCMethodList *MethList = &M->second.first;
5580 MethList && MethList->getMethod();
5581 MethList = MethList->getNext()) {
5582 if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
5585 if (!Selectors.insert(MethList->getMethod()->getSelector()).second)
5588 Result R(MethList->getMethod(),
5589 Results.getBasePriority(MethList->getMethod()), nullptr);
5590 R.StartParameter = SelIdents.size();
5591 R.AllParametersAreInformative = false;
5592 Results.MaybeAddResult(R, CurContext);
5596 Results.ExitScope();
5599 // If we're actually at the argument expression (rather than prior to the
5600 // selector), we're actually performing code completion for an expression.
5601 // Determine whether we have a single, best method. If so, we can
5602 // code-complete the expression using the corresponding parameter type as
5603 // our preferred type, improving completion results.
5604 if (AtArgumentExpression) {
5605 QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
5607 if (PreferredType.isNull())
5608 CodeCompleteOrdinaryName(S, PCC_Expression);
5610 CodeCompleteExpression(S, PreferredType);
5614 HandleCodeCompleteResults(this, CodeCompleter,
5615 Results.getCompletionContext(),
5616 Results.data(),Results.size());
5619 void Sema::CodeCompleteObjCForCollection(Scope *S,
5620 DeclGroupPtrTy IterationVar) {
5621 CodeCompleteExpressionData Data;
5622 Data.ObjCCollection = true;
5624 if (IterationVar.getAsOpaquePtr()) {
5625 DeclGroupRef DG = IterationVar.get();
5626 for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) {
5628 Data.IgnoreDecls.push_back(*I);
5632 CodeCompleteExpression(S, Data);
5635 void Sema::CodeCompleteObjCSelector(Scope *S,
5636 ArrayRef<IdentifierInfo *> SelIdents) {
5637 // If we have an external source, load the entire class method
5638 // pool from the AST file.
5639 if (ExternalSource) {
5640 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5642 Selector Sel = ExternalSource->GetExternalSelector(I);
5643 if (Sel.isNull() || MethodPool.count(Sel))
5646 ReadMethodPool(Sel);
5650 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5651 CodeCompleter->getCodeCompletionTUInfo(),
5652 CodeCompletionContext::CCC_SelectorName);
5653 Results.EnterNewScope();
5654 for (GlobalMethodPool::iterator M = MethodPool.begin(),
5655 MEnd = MethodPool.end();
5658 Selector Sel = M->first;
5659 if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents))
5662 CodeCompletionBuilder Builder(Results.getAllocator(),
5663 Results.getCodeCompletionTUInfo());
5664 if (Sel.isUnarySelector()) {
5665 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
5666 Sel.getNameForSlot(0)));
5667 Results.AddResult(Builder.TakeString());
5671 std::string Accumulator;
5672 for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) {
5673 if (I == SelIdents.size()) {
5674 if (!Accumulator.empty()) {
5675 Builder.AddInformativeChunk(Builder.getAllocator().CopyString(
5677 Accumulator.clear();
5681 Accumulator += Sel.getNameForSlot(I);
5684 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator));
5685 Results.AddResult(Builder.TakeString());
5687 Results.ExitScope();
5689 HandleCodeCompleteResults(this, CodeCompleter,
5690 CodeCompletionContext::CCC_SelectorName,
5691 Results.data(), Results.size());
5694 /// \brief Add all of the protocol declarations that we find in the given
5695 /// (translation unit) context.
5696 static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext,
5697 bool OnlyForwardDeclarations,
5698 ResultBuilder &Results) {
5699 typedef CodeCompletionResult Result;
5701 for (const auto *D : Ctx->decls()) {
5702 // Record any protocols we find.
5703 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D))
5704 if (!OnlyForwardDeclarations || !Proto->hasDefinition())
5705 Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr),
5706 CurContext, nullptr, false);
5710 void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
5711 unsigned NumProtocols) {
5712 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5713 CodeCompleter->getCodeCompletionTUInfo(),
5714 CodeCompletionContext::CCC_ObjCProtocolName);
5716 if (CodeCompleter && CodeCompleter->includeGlobals()) {
5717 Results.EnterNewScope();
5719 // Tell the result set to ignore all of the protocols we have
5721 // FIXME: This doesn't work when caching code-completion results.
5722 for (unsigned I = 0; I != NumProtocols; ++I)
5723 if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first,
5724 Protocols[I].second))
5725 Results.Ignore(Protocol);
5727 // Add all protocols.
5728 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false,
5731 Results.ExitScope();
5734 HandleCodeCompleteResults(this, CodeCompleter,
5735 CodeCompletionContext::CCC_ObjCProtocolName,
5736 Results.data(),Results.size());
5739 void Sema::CodeCompleteObjCProtocolDecl(Scope *) {
5740 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5741 CodeCompleter->getCodeCompletionTUInfo(),
5742 CodeCompletionContext::CCC_ObjCProtocolName);
5744 if (CodeCompleter && CodeCompleter->includeGlobals()) {
5745 Results.EnterNewScope();
5747 // Add all protocols.
5748 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true,
5751 Results.ExitScope();
5754 HandleCodeCompleteResults(this, CodeCompleter,
5755 CodeCompletionContext::CCC_ObjCProtocolName,
5756 Results.data(),Results.size());
5759 /// \brief Add all of the Objective-C interface declarations that we find in
5760 /// the given (translation unit) context.
5761 static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext,
5762 bool OnlyForwardDeclarations,
5763 bool OnlyUnimplemented,
5764 ResultBuilder &Results) {
5765 typedef CodeCompletionResult Result;
5767 for (const auto *D : Ctx->decls()) {
5768 // Record any interfaces we find.
5769 if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
5770 if ((!OnlyForwardDeclarations || !Class->hasDefinition()) &&
5771 (!OnlyUnimplemented || !Class->getImplementation()))
5772 Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr),
5773 CurContext, nullptr, false);
5777 void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) {
5778 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5779 CodeCompleter->getCodeCompletionTUInfo(),
5780 CodeCompletionContext::CCC_Other);
5781 Results.EnterNewScope();
5783 if (CodeCompleter->includeGlobals()) {
5785 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5789 Results.ExitScope();
5791 HandleCodeCompleteResults(this, CodeCompleter,
5792 CodeCompletionContext::CCC_ObjCInterfaceName,
5793 Results.data(),Results.size());
5796 void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName,
5797 SourceLocation ClassNameLoc) {
5798 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5799 CodeCompleter->getCodeCompletionTUInfo(),
5800 CodeCompletionContext::CCC_ObjCInterfaceName);
5801 Results.EnterNewScope();
5803 // Make sure that we ignore the class we're currently defining.
5805 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
5806 if (CurClass && isa<ObjCInterfaceDecl>(CurClass))
5807 Results.Ignore(CurClass);
5809 if (CodeCompleter->includeGlobals()) {
5811 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5815 Results.ExitScope();
5817 HandleCodeCompleteResults(this, CodeCompleter,
5818 CodeCompletionContext::CCC_ObjCInterfaceName,
5819 Results.data(),Results.size());
5822 void Sema::CodeCompleteObjCImplementationDecl(Scope *S) {
5823 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5824 CodeCompleter->getCodeCompletionTUInfo(),
5825 CodeCompletionContext::CCC_Other);
5826 Results.EnterNewScope();
5828 if (CodeCompleter->includeGlobals()) {
5829 // Add all unimplemented classes.
5830 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5834 Results.ExitScope();
5836 HandleCodeCompleteResults(this, CodeCompleter,
5837 CodeCompletionContext::CCC_ObjCInterfaceName,
5838 Results.data(),Results.size());
5841 void Sema::CodeCompleteObjCInterfaceCategory(Scope *S,
5842 IdentifierInfo *ClassName,
5843 SourceLocation ClassNameLoc) {
5844 typedef CodeCompletionResult Result;
5846 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5847 CodeCompleter->getCodeCompletionTUInfo(),
5848 CodeCompletionContext::CCC_ObjCCategoryName);
5850 // Ignore any categories we find that have already been implemented by this
5852 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
5854 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
5855 if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){
5856 for (const auto *Cat : Class->visible_categories())
5857 CategoryNames.insert(Cat->getIdentifier());
5860 // Add all of the categories we know about.
5861 Results.EnterNewScope();
5862 TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5863 for (const auto *D : TU->decls())
5864 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D))
5865 if (CategoryNames.insert(Category->getIdentifier()).second)
5866 Results.AddResult(Result(Category, Results.getBasePriority(Category),
5868 CurContext, nullptr, false);
5869 Results.ExitScope();
5871 HandleCodeCompleteResults(this, CodeCompleter,
5872 CodeCompletionContext::CCC_ObjCCategoryName,
5873 Results.data(),Results.size());
5876 void Sema::CodeCompleteObjCImplementationCategory(Scope *S,
5877 IdentifierInfo *ClassName,
5878 SourceLocation ClassNameLoc) {
5879 typedef CodeCompletionResult Result;
5881 // Find the corresponding interface. If we couldn't find the interface, the
5882 // program itself is ill-formed. However, we'll try to be helpful still by
5883 // providing the list of all of the categories we know about.
5885 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
5886 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass);
5888 return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc);
5890 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5891 CodeCompleter->getCodeCompletionTUInfo(),
5892 CodeCompletionContext::CCC_ObjCCategoryName);
5894 // Add all of the categories that have have corresponding interface
5895 // declarations in this class and any of its superclasses, except for
5896 // already-implemented categories in the class itself.
5897 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
5898 Results.EnterNewScope();
5899 bool IgnoreImplemented = true;
5901 for (const auto *Cat : Class->visible_categories()) {
5902 if ((!IgnoreImplemented || !Cat->getImplementation()) &&
5903 CategoryNames.insert(Cat->getIdentifier()).second)
5904 Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr),
5905 CurContext, nullptr, false);
5908 Class = Class->getSuperClass();
5909 IgnoreImplemented = false;
5911 Results.ExitScope();
5913 HandleCodeCompleteResults(this, CodeCompleter,
5914 CodeCompletionContext::CCC_ObjCCategoryName,
5915 Results.data(),Results.size());
5918 void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) {
5919 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5920 CodeCompleter->getCodeCompletionTUInfo(),
5921 CodeCompletionContext::CCC_Other);
5923 // Figure out where this @synthesize lives.
5924 ObjCContainerDecl *Container
5925 = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
5927 (!isa<ObjCImplementationDecl>(Container) &&
5928 !isa<ObjCCategoryImplDecl>(Container)))
5931 // Ignore any properties that have already been implemented.
5932 Container = getContainerDef(Container);
5933 for (const auto *D : Container->decls())
5934 if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D))
5935 Results.Ignore(PropertyImpl->getPropertyDecl());
5937 // Add any properties that we find.
5938 AddedPropertiesSet AddedProperties;
5939 Results.EnterNewScope();
5940 if (ObjCImplementationDecl *ClassImpl
5941 = dyn_cast<ObjCImplementationDecl>(Container))
5942 AddObjCProperties(ClassImpl->getClassInterface(), false,
5943 /*AllowNullaryMethods=*/false, CurContext,
5944 AddedProperties, Results);
5946 AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(),
5947 false, /*AllowNullaryMethods=*/false, CurContext,
5948 AddedProperties, Results);
5949 Results.ExitScope();
5951 HandleCodeCompleteResults(this, CodeCompleter,
5952 CodeCompletionContext::CCC_Other,
5953 Results.data(),Results.size());
5956 void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
5957 IdentifierInfo *PropertyName) {
5958 typedef CodeCompletionResult Result;
5959 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
5960 CodeCompleter->getCodeCompletionTUInfo(),
5961 CodeCompletionContext::CCC_Other);
5963 // Figure out where this @synthesize lives.
5964 ObjCContainerDecl *Container
5965 = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
5967 (!isa<ObjCImplementationDecl>(Container) &&
5968 !isa<ObjCCategoryImplDecl>(Container)))
5971 // Figure out which interface we're looking into.
5972 ObjCInterfaceDecl *Class = nullptr;
5973 if (ObjCImplementationDecl *ClassImpl
5974 = dyn_cast<ObjCImplementationDecl>(Container))
5975 Class = ClassImpl->getClassInterface();
5977 Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
5978 ->getClassInterface();
5980 // Determine the type of the property we're synthesizing.
5981 QualType PropertyType = Context.getObjCIdType();
5983 if (ObjCPropertyDecl *Property
5984 = Class->FindPropertyDeclaration(PropertyName)) {
5986 = Property->getType().getNonReferenceType().getUnqualifiedType();
5988 // Give preference to ivars
5989 Results.setPreferredType(PropertyType);
5993 // Add all of the instance variables in this class and its superclasses.
5994 Results.EnterNewScope();
5995 bool SawSimilarlyNamedIvar = false;
5996 std::string NameWithPrefix;
5997 NameWithPrefix += '_';
5998 NameWithPrefix += PropertyName->getName();
5999 std::string NameWithSuffix = PropertyName->getName().str();
6000 NameWithSuffix += '_';
6001 for(; Class; Class = Class->getSuperClass()) {
6002 for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar;
6003 Ivar = Ivar->getNextIvar()) {
6004 Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr),
6005 CurContext, nullptr, false);
6007 // Determine whether we've seen an ivar with a name similar to the
6009 if ((PropertyName == Ivar->getIdentifier() ||
6010 NameWithPrefix == Ivar->getName() ||
6011 NameWithSuffix == Ivar->getName())) {
6012 SawSimilarlyNamedIvar = true;
6014 // Reduce the priority of this result by one, to give it a slight
6015 // advantage over other results whose names don't match so closely.
6016 if (Results.size() &&
6017 Results.data()[Results.size() - 1].Kind
6018 == CodeCompletionResult::RK_Declaration &&
6019 Results.data()[Results.size() - 1].Declaration == Ivar)
6020 Results.data()[Results.size() - 1].Priority--;
6025 if (!SawSimilarlyNamedIvar) {
6026 // Create ivar result _propName, that the user can use to synthesize
6027 // an ivar of the appropriate type.
6028 unsigned Priority = CCP_MemberDeclaration + 1;
6029 typedef CodeCompletionResult Result;
6030 CodeCompletionAllocator &Allocator = Results.getAllocator();
6031 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(),
6032 Priority,CXAvailability_Available);
6034 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
6035 Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context,
6036 Policy, Allocator));
6037 Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix));
6038 Results.AddResult(Result(Builder.TakeString(), Priority,
6039 CXCursor_ObjCIvarDecl));
6042 Results.ExitScope();
6044 HandleCodeCompleteResults(this, CodeCompleter,
6045 CodeCompletionContext::CCC_Other,
6046 Results.data(),Results.size());
6049 // Mapping from selectors to the methods that implement that selector, along
6050 // with the "in original class" flag.
6051 typedef llvm::DenseMap<
6052 Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap;
6054 /// \brief Find all of the methods that reside in the given container
6055 /// (and its superclasses, protocols, etc.) that meet the given
6056 /// criteria. Insert those methods into the map of known methods,
6057 /// indexed by selector so they can be easily found.
6058 static void FindImplementableMethods(ASTContext &Context,
6059 ObjCContainerDecl *Container,
6060 bool WantInstanceMethods,
6061 QualType ReturnType,
6062 KnownMethodsMap &KnownMethods,
6063 bool InOriginalClass = true) {
6064 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) {
6065 // Make sure we have a definition; that's what we'll walk.
6066 if (!IFace->hasDefinition())
6069 IFace = IFace->getDefinition();
6072 const ObjCList<ObjCProtocolDecl> &Protocols
6073 = IFace->getReferencedProtocols();
6074 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6075 E = Protocols.end();
6077 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6078 KnownMethods, InOriginalClass);
6080 // Add methods from any class extensions and categories.
6081 for (auto *Cat : IFace->visible_categories()) {
6082 FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType,
6083 KnownMethods, false);
6086 // Visit the superclass.
6087 if (IFace->getSuperClass())
6088 FindImplementableMethods(Context, IFace->getSuperClass(),
6089 WantInstanceMethods, ReturnType,
6090 KnownMethods, false);
6093 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
6094 // Recurse into protocols.
6095 const ObjCList<ObjCProtocolDecl> &Protocols
6096 = Category->getReferencedProtocols();
6097 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6098 E = Protocols.end();
6100 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6101 KnownMethods, InOriginalClass);
6103 // If this category is the original class, jump to the interface.
6104 if (InOriginalClass && Category->getClassInterface())
6105 FindImplementableMethods(Context, Category->getClassInterface(),
6106 WantInstanceMethods, ReturnType, KnownMethods,
6110 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
6111 // Make sure we have a definition; that's what we'll walk.
6112 if (!Protocol->hasDefinition())
6114 Protocol = Protocol->getDefinition();
6115 Container = Protocol;
6117 // Recurse into protocols.
6118 const ObjCList<ObjCProtocolDecl> &Protocols
6119 = Protocol->getReferencedProtocols();
6120 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6121 E = Protocols.end();
6123 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6124 KnownMethods, false);
6127 // Add methods in this container. This operation occurs last because
6128 // we want the methods from this container to override any methods
6129 // we've previously seen with the same selector.
6130 for (auto *M : Container->methods()) {
6131 if (M->isInstanceMethod() == WantInstanceMethods) {
6132 if (!ReturnType.isNull() &&
6133 !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType()))
6136 KnownMethods[M->getSelector()] =
6137 KnownMethodsMap::mapped_type(M, InOriginalClass);
6142 /// \brief Add the parenthesized return or parameter type chunk to a code
6143 /// completion string.
6144 static void AddObjCPassingTypeChunk(QualType Type,
6145 unsigned ObjCDeclQuals,
6146 ASTContext &Context,
6147 const PrintingPolicy &Policy,
6148 CodeCompletionBuilder &Builder) {
6149 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6150 std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals);
6152 Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals));
6153 Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy,
6154 Builder.getAllocator()));
6155 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6158 /// \brief Determine whether the given class is or inherits from a class by
6160 static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class,
6165 if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name)
6168 return InheritsFromClassNamed(Class->getSuperClass(), Name);
6171 /// \brief Add code completions for Objective-C Key-Value Coding (KVC) and
6172 /// Key-Value Observing (KVO).
6173 static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property,
6174 bool IsInstanceMethod,
6175 QualType ReturnType,
6176 ASTContext &Context,
6177 VisitedSelectorSet &KnownSelectors,
6178 ResultBuilder &Results) {
6179 IdentifierInfo *PropName = Property->getIdentifier();
6180 if (!PropName || PropName->getLength() == 0)
6183 PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
6185 // Builder that will create each code completion.
6186 typedef CodeCompletionResult Result;
6187 CodeCompletionAllocator &Allocator = Results.getAllocator();
6188 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
6190 // The selector table.
6191 SelectorTable &Selectors = Context.Selectors;
6193 // The property name, copied into the code completion allocation region
6196 CodeCompletionAllocator &Allocator;
6198 const char *CopiedKey;
6200 KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key)
6201 : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {}
6203 operator const char *() {
6207 return CopiedKey = Allocator.CopyString(Key);
6209 } Key(Allocator, PropName->getName());
6211 // The uppercased name of the property name.
6212 std::string UpperKey = PropName->getName();
6213 if (!UpperKey.empty())
6214 UpperKey[0] = toUppercase(UpperKey[0]);
6216 bool ReturnTypeMatchesProperty = ReturnType.isNull() ||
6217 Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(),
6218 Property->getType());
6219 bool ReturnTypeMatchesVoid
6220 = ReturnType.isNull() || ReturnType->isVoidType();
6222 // Add the normal accessor -(type)key.
6223 if (IsInstanceMethod &&
6224 KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second &&
6225 ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) {
6226 if (ReturnType.isNull())
6227 AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6228 Context, Policy, Builder);
6230 Builder.AddTypedTextChunk(Key);
6231 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6232 CXCursor_ObjCInstanceMethodDecl));
6235 // If we have an integral or boolean property (or the user has provided
6236 // an integral or boolean return type), add the accessor -(type)isKey.
6237 if (IsInstanceMethod &&
6238 ((!ReturnType.isNull() &&
6239 (ReturnType->isIntegerType() || ReturnType->isBooleanType())) ||
6240 (ReturnType.isNull() &&
6241 (Property->getType()->isIntegerType() ||
6242 Property->getType()->isBooleanType())))) {
6243 std::string SelectorName = (Twine("is") + UpperKey).str();
6244 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6245 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6247 if (ReturnType.isNull()) {
6248 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6249 Builder.AddTextChunk("BOOL");
6250 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6253 Builder.AddTypedTextChunk(
6254 Allocator.CopyString(SelectorId->getName()));
6255 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6256 CXCursor_ObjCInstanceMethodDecl));
6260 // Add the normal mutator.
6261 if (IsInstanceMethod && ReturnTypeMatchesVoid &&
6262 !Property->getSetterMethodDecl()) {
6263 std::string SelectorName = (Twine("set") + UpperKey).str();
6264 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6265 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6266 if (ReturnType.isNull()) {
6267 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6268 Builder.AddTextChunk("void");
6269 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6272 Builder.AddTypedTextChunk(
6273 Allocator.CopyString(SelectorId->getName()));
6274 Builder.AddTypedTextChunk(":");
6275 AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6276 Context, Policy, Builder);
6277 Builder.AddTextChunk(Key);
6278 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6279 CXCursor_ObjCInstanceMethodDecl));
6283 // Indexed and unordered accessors
6284 unsigned IndexedGetterPriority = CCP_CodePattern;
6285 unsigned IndexedSetterPriority = CCP_CodePattern;
6286 unsigned UnorderedGetterPriority = CCP_CodePattern;
6287 unsigned UnorderedSetterPriority = CCP_CodePattern;
6288 if (const ObjCObjectPointerType *ObjCPointer
6289 = Property->getType()->getAs<ObjCObjectPointerType>()) {
6290 if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) {
6291 // If this interface type is not provably derived from a known
6292 // collection, penalize the corresponding completions.
6293 if (!InheritsFromClassNamed(IFace, "NSMutableArray")) {
6294 IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6295 if (!InheritsFromClassNamed(IFace, "NSArray"))
6296 IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6299 if (!InheritsFromClassNamed(IFace, "NSMutableSet")) {
6300 UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6301 if (!InheritsFromClassNamed(IFace, "NSSet"))
6302 UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6306 IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6307 IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6308 UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6309 UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6312 // Add -(NSUInteger)countOf<key>
6313 if (IsInstanceMethod &&
6314 (ReturnType.isNull() || ReturnType->isIntegerType())) {
6315 std::string SelectorName = (Twine("countOf") + UpperKey).str();
6316 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6317 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6319 if (ReturnType.isNull()) {
6320 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6321 Builder.AddTextChunk("NSUInteger");
6322 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6325 Builder.AddTypedTextChunk(
6326 Allocator.CopyString(SelectorId->getName()));
6327 Results.AddResult(Result(Builder.TakeString(),
6328 std::min(IndexedGetterPriority,
6329 UnorderedGetterPriority),
6330 CXCursor_ObjCInstanceMethodDecl));
6335 // Add -(id)objectInKeyAtIndex:(NSUInteger)index
6336 if (IsInstanceMethod &&
6337 (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
6338 std::string SelectorName
6339 = (Twine("objectIn") + UpperKey + "AtIndex").str();
6340 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6341 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6342 if (ReturnType.isNull()) {
6343 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6344 Builder.AddTextChunk("id");
6345 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6348 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6349 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6350 Builder.AddTextChunk("NSUInteger");
6351 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6352 Builder.AddTextChunk("index");
6353 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6354 CXCursor_ObjCInstanceMethodDecl));
6358 // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes
6359 if (IsInstanceMethod &&
6360 (ReturnType.isNull() ||
6361 (ReturnType->isObjCObjectPointerType() &&
6362 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6363 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6364 ->getName() == "NSArray"))) {
6365 std::string SelectorName
6366 = (Twine(Property->getName()) + "AtIndexes").str();
6367 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6368 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6369 if (ReturnType.isNull()) {
6370 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6371 Builder.AddTextChunk("NSArray *");
6372 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6375 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6376 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6377 Builder.AddTextChunk("NSIndexSet *");
6378 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6379 Builder.AddTextChunk("indexes");
6380 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6381 CXCursor_ObjCInstanceMethodDecl));
6385 // Add -(void)getKey:(type **)buffer range:(NSRange)inRange
6386 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6387 std::string SelectorName = (Twine("get") + UpperKey).str();
6388 IdentifierInfo *SelectorIds[2] = {
6389 &Context.Idents.get(SelectorName),
6390 &Context.Idents.get("range")
6393 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
6394 if (ReturnType.isNull()) {
6395 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6396 Builder.AddTextChunk("void");
6397 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6400 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6401 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6402 Builder.AddPlaceholderChunk("object-type");
6403 Builder.AddTextChunk(" **");
6404 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6405 Builder.AddTextChunk("buffer");
6406 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6407 Builder.AddTypedTextChunk("range:");
6408 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6409 Builder.AddTextChunk("NSRange");
6410 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6411 Builder.AddTextChunk("inRange");
6412 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6413 CXCursor_ObjCInstanceMethodDecl));
6417 // Mutable indexed accessors
6419 // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index
6420 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6421 std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str();
6422 IdentifierInfo *SelectorIds[2] = {
6423 &Context.Idents.get("insertObject"),
6424 &Context.Idents.get(SelectorName)
6427 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
6428 if (ReturnType.isNull()) {
6429 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6430 Builder.AddTextChunk("void");
6431 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6434 Builder.AddTypedTextChunk("insertObject:");
6435 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6436 Builder.AddPlaceholderChunk("object-type");
6437 Builder.AddTextChunk(" *");
6438 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6439 Builder.AddTextChunk("object");
6440 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6441 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6442 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6443 Builder.AddPlaceholderChunk("NSUInteger");
6444 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6445 Builder.AddTextChunk("index");
6446 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6447 CXCursor_ObjCInstanceMethodDecl));
6451 // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes
6452 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6453 std::string SelectorName = (Twine("insert") + UpperKey).str();
6454 IdentifierInfo *SelectorIds[2] = {
6455 &Context.Idents.get(SelectorName),
6456 &Context.Idents.get("atIndexes")
6459 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
6460 if (ReturnType.isNull()) {
6461 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6462 Builder.AddTextChunk("void");
6463 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6466 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6467 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6468 Builder.AddTextChunk("NSArray *");
6469 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6470 Builder.AddTextChunk("array");
6471 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6472 Builder.AddTypedTextChunk("atIndexes:");
6473 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6474 Builder.AddPlaceholderChunk("NSIndexSet *");
6475 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6476 Builder.AddTextChunk("indexes");
6477 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6478 CXCursor_ObjCInstanceMethodDecl));
6482 // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index
6483 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6484 std::string SelectorName
6485 = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str();
6486 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6487 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6488 if (ReturnType.isNull()) {
6489 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6490 Builder.AddTextChunk("void");
6491 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6494 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6495 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6496 Builder.AddTextChunk("NSUInteger");
6497 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6498 Builder.AddTextChunk("index");
6499 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6500 CXCursor_ObjCInstanceMethodDecl));
6504 // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes
6505 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6506 std::string SelectorName
6507 = (Twine("remove") + UpperKey + "AtIndexes").str();
6508 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6509 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6510 if (ReturnType.isNull()) {
6511 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6512 Builder.AddTextChunk("void");
6513 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6516 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6517 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6518 Builder.AddTextChunk("NSIndexSet *");
6519 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6520 Builder.AddTextChunk("indexes");
6521 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6522 CXCursor_ObjCInstanceMethodDecl));
6526 // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object
6527 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6528 std::string SelectorName
6529 = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str();
6530 IdentifierInfo *SelectorIds[2] = {
6531 &Context.Idents.get(SelectorName),
6532 &Context.Idents.get("withObject")
6535 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
6536 if (ReturnType.isNull()) {
6537 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6538 Builder.AddTextChunk("void");
6539 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6542 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6543 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6544 Builder.AddPlaceholderChunk("NSUInteger");
6545 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6546 Builder.AddTextChunk("index");
6547 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6548 Builder.AddTypedTextChunk("withObject:");
6549 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6550 Builder.AddTextChunk("id");
6551 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6552 Builder.AddTextChunk("object");
6553 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6554 CXCursor_ObjCInstanceMethodDecl));
6558 // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array
6559 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6560 std::string SelectorName1
6561 = (Twine("replace") + UpperKey + "AtIndexes").str();
6562 std::string SelectorName2 = (Twine("with") + UpperKey).str();
6563 IdentifierInfo *SelectorIds[2] = {
6564 &Context.Idents.get(SelectorName1),
6565 &Context.Idents.get(SelectorName2)
6568 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
6569 if (ReturnType.isNull()) {
6570 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6571 Builder.AddTextChunk("void");
6572 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6575 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":"));
6576 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6577 Builder.AddPlaceholderChunk("NSIndexSet *");
6578 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6579 Builder.AddTextChunk("indexes");
6580 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6581 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":"));
6582 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6583 Builder.AddTextChunk("NSArray *");
6584 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6585 Builder.AddTextChunk("array");
6586 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6587 CXCursor_ObjCInstanceMethodDecl));
6591 // Unordered getters
6592 // - (NSEnumerator *)enumeratorOfKey
6593 if (IsInstanceMethod &&
6594 (ReturnType.isNull() ||
6595 (ReturnType->isObjCObjectPointerType() &&
6596 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6597 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6598 ->getName() == "NSEnumerator"))) {
6599 std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str();
6600 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6601 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6603 if (ReturnType.isNull()) {
6604 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6605 Builder.AddTextChunk("NSEnumerator *");
6606 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6609 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6610 Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6611 CXCursor_ObjCInstanceMethodDecl));
6615 // - (type *)memberOfKey:(type *)object
6616 if (IsInstanceMethod &&
6617 (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
6618 std::string SelectorName = (Twine("memberOf") + UpperKey).str();
6619 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6620 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6621 if (ReturnType.isNull()) {
6622 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6623 Builder.AddPlaceholderChunk("object-type");
6624 Builder.AddTextChunk(" *");
6625 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6628 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6629 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6630 if (ReturnType.isNull()) {
6631 Builder.AddPlaceholderChunk("object-type");
6632 Builder.AddTextChunk(" *");
6634 Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context,
6636 Builder.getAllocator()));
6638 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6639 Builder.AddTextChunk("object");
6640 Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6641 CXCursor_ObjCInstanceMethodDecl));
6645 // Mutable unordered accessors
6646 // - (void)addKeyObject:(type *)object
6647 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6648 std::string SelectorName
6649 = (Twine("add") + UpperKey + Twine("Object")).str();
6650 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6651 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6652 if (ReturnType.isNull()) {
6653 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6654 Builder.AddTextChunk("void");
6655 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6658 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6659 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6660 Builder.AddPlaceholderChunk("object-type");
6661 Builder.AddTextChunk(" *");
6662 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6663 Builder.AddTextChunk("object");
6664 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6665 CXCursor_ObjCInstanceMethodDecl));
6669 // - (void)addKey:(NSSet *)objects
6670 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6671 std::string SelectorName = (Twine("add") + UpperKey).str();
6672 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6673 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6674 if (ReturnType.isNull()) {
6675 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6676 Builder.AddTextChunk("void");
6677 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6680 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6681 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6682 Builder.AddTextChunk("NSSet *");
6683 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6684 Builder.AddTextChunk("objects");
6685 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6686 CXCursor_ObjCInstanceMethodDecl));
6690 // - (void)removeKeyObject:(type *)object
6691 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6692 std::string SelectorName
6693 = (Twine("remove") + UpperKey + Twine("Object")).str();
6694 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6695 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6696 if (ReturnType.isNull()) {
6697 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6698 Builder.AddTextChunk("void");
6699 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6702 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6703 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6704 Builder.AddPlaceholderChunk("object-type");
6705 Builder.AddTextChunk(" *");
6706 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6707 Builder.AddTextChunk("object");
6708 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6709 CXCursor_ObjCInstanceMethodDecl));
6713 // - (void)removeKey:(NSSet *)objects
6714 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6715 std::string SelectorName = (Twine("remove") + UpperKey).str();
6716 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6717 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6718 if (ReturnType.isNull()) {
6719 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6720 Builder.AddTextChunk("void");
6721 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6724 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6725 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6726 Builder.AddTextChunk("NSSet *");
6727 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6728 Builder.AddTextChunk("objects");
6729 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6730 CXCursor_ObjCInstanceMethodDecl));
6734 // - (void)intersectKey:(NSSet *)objects
6735 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
6736 std::string SelectorName = (Twine("intersect") + UpperKey).str();
6737 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6738 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
6739 if (ReturnType.isNull()) {
6740 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6741 Builder.AddTextChunk("void");
6742 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6745 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6746 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6747 Builder.AddTextChunk("NSSet *");
6748 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6749 Builder.AddTextChunk("objects");
6750 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6751 CXCursor_ObjCInstanceMethodDecl));
6755 // Key-Value Observing
6756 // + (NSSet *)keyPathsForValuesAffectingKey
6757 if (!IsInstanceMethod &&
6758 (ReturnType.isNull() ||
6759 (ReturnType->isObjCObjectPointerType() &&
6760 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6761 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6762 ->getName() == "NSSet"))) {
6763 std::string SelectorName
6764 = (Twine("keyPathsForValuesAffecting") + UpperKey).str();
6765 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6766 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6768 if (ReturnType.isNull()) {
6769 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6770 Builder.AddTextChunk("NSSet *");
6771 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6774 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6775 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6776 CXCursor_ObjCClassMethodDecl));
6780 // + (BOOL)automaticallyNotifiesObserversForKey
6781 if (!IsInstanceMethod &&
6782 (ReturnType.isNull() ||
6783 ReturnType->isIntegerType() ||
6784 ReturnType->isBooleanType())) {
6785 std::string SelectorName
6786 = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str();
6787 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
6788 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6790 if (ReturnType.isNull()) {
6791 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6792 Builder.AddTextChunk("BOOL");
6793 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6796 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6797 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6798 CXCursor_ObjCClassMethodDecl));
6803 void Sema::CodeCompleteObjCMethodDecl(Scope *S,
6804 bool IsInstanceMethod,
6805 ParsedType ReturnTy) {
6806 // Determine the return type of the method we're declaring, if
6808 QualType ReturnType = GetTypeFromParser(ReturnTy);
6809 Decl *IDecl = nullptr;
6810 if (CurContext->isObjCContainer()) {
6811 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
6812 IDecl = cast<Decl>(OCD);
6814 // Determine where we should start searching for methods.
6815 ObjCContainerDecl *SearchDecl = nullptr;
6816 bool IsInImplementation = false;
6817 if (Decl *D = IDecl) {
6818 if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) {
6819 SearchDecl = Impl->getClassInterface();
6820 IsInImplementation = true;
6821 } else if (ObjCCategoryImplDecl *CatImpl
6822 = dyn_cast<ObjCCategoryImplDecl>(D)) {
6823 SearchDecl = CatImpl->getCategoryDecl();
6824 IsInImplementation = true;
6826 SearchDecl = dyn_cast<ObjCContainerDecl>(D);
6829 if (!SearchDecl && S) {
6830 if (DeclContext *DC = S->getEntity())
6831 SearchDecl = dyn_cast<ObjCContainerDecl>(DC);
6835 HandleCodeCompleteResults(this, CodeCompleter,
6836 CodeCompletionContext::CCC_Other,
6841 // Find all of the methods that we could declare/implement here.
6842 KnownMethodsMap KnownMethods;
6843 FindImplementableMethods(Context, SearchDecl, IsInstanceMethod,
6844 ReturnType, KnownMethods);
6846 // Add declarations or definitions for each of the known methods.
6847 typedef CodeCompletionResult Result;
6848 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
6849 CodeCompleter->getCodeCompletionTUInfo(),
6850 CodeCompletionContext::CCC_Other);
6851 Results.EnterNewScope();
6852 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
6853 for (KnownMethodsMap::iterator M = KnownMethods.begin(),
6854 MEnd = KnownMethods.end();
6856 ObjCMethodDecl *Method = M->second.getPointer();
6857 CodeCompletionBuilder Builder(Results.getAllocator(),
6858 Results.getCodeCompletionTUInfo());
6860 // If the result type was not already provided, add it to the
6861 // pattern as (type).
6862 if (ReturnType.isNull())
6863 AddObjCPassingTypeChunk(Method->getReturnType(),
6864 Method->getObjCDeclQualifier(), Context, Policy,
6867 Selector Sel = Method->getSelector();
6869 // Add the first part of the selector to the pattern.
6870 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
6871 Sel.getNameForSlot(0)));
6873 // Add parameters to the pattern.
6875 for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
6876 PEnd = Method->param_end();
6877 P != PEnd; (void)++P, ++I) {
6878 // Add the part of the selector name.
6880 Builder.AddTypedTextChunk(":");
6881 else if (I < Sel.getNumArgs()) {
6882 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6883 Builder.AddTypedTextChunk(
6884 Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":"));
6888 // Add the parameter type.
6889 AddObjCPassingTypeChunk((*P)->getOriginalType(),
6890 (*P)->getObjCDeclQualifier(),
6894 if (IdentifierInfo *Id = (*P)->getIdentifier())
6895 Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName()));
6898 if (Method->isVariadic()) {
6899 if (Method->param_size() > 0)
6900 Builder.AddChunk(CodeCompletionString::CK_Comma);
6901 Builder.AddTextChunk("...");
6904 if (IsInImplementation && Results.includeCodePatterns()) {
6905 // We will be defining the method here, so add a compound statement.
6906 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6907 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
6908 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
6909 if (!Method->getReturnType()->isVoidType()) {
6910 // If the result type is not void, add a return clause.
6911 Builder.AddTextChunk("return");
6912 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6913 Builder.AddPlaceholderChunk("expression");
6914 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
6916 Builder.AddPlaceholderChunk("statements");
6918 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
6919 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
6922 unsigned Priority = CCP_CodePattern;
6923 if (!M->second.getInt())
6924 Priority += CCD_InBaseClass;
6926 Results.AddResult(Result(Builder.TakeString(), Method, Priority));
6929 // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of
6930 // the properties in this class and its categories.
6931 if (Context.getLangOpts().ObjC2) {
6932 SmallVector<ObjCContainerDecl *, 4> Containers;
6933 Containers.push_back(SearchDecl);
6935 VisitedSelectorSet KnownSelectors;
6936 for (KnownMethodsMap::iterator M = KnownMethods.begin(),
6937 MEnd = KnownMethods.end();
6939 KnownSelectors.insert(M->first);
6942 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl);
6944 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl))
6945 IFace = Category->getClassInterface();
6948 for (auto *Cat : IFace->visible_categories())
6949 Containers.push_back(Cat);
6951 for (unsigned I = 0, N = Containers.size(); I != N; ++I)
6952 for (auto *P : Containers[I]->properties())
6953 AddObjCKeyValueCompletions(P, IsInstanceMethod, ReturnType, Context,
6954 KnownSelectors, Results);
6957 Results.ExitScope();
6959 HandleCodeCompleteResults(this, CodeCompleter,
6960 CodeCompletionContext::CCC_Other,
6961 Results.data(),Results.size());
6964 void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S,
6965 bool IsInstanceMethod,
6966 bool AtParameterName,
6967 ParsedType ReturnTy,
6968 ArrayRef<IdentifierInfo *> SelIdents) {
6969 // If we have an external source, load the entire class method
6970 // pool from the AST file.
6971 if (ExternalSource) {
6972 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
6974 Selector Sel = ExternalSource->GetExternalSelector(I);
6975 if (Sel.isNull() || MethodPool.count(Sel))
6978 ReadMethodPool(Sel);
6982 // Build the set of methods we can see.
6983 typedef CodeCompletionResult Result;
6984 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
6985 CodeCompleter->getCodeCompletionTUInfo(),
6986 CodeCompletionContext::CCC_Other);
6989 Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType());
6991 Results.EnterNewScope();
6992 for (GlobalMethodPool::iterator M = MethodPool.begin(),
6993 MEnd = MethodPool.end();
6995 for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first :
6997 MethList && MethList->getMethod();
6998 MethList = MethList->getNext()) {
6999 if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
7002 if (AtParameterName) {
7003 // Suggest parameter names we've seen before.
7004 unsigned NumSelIdents = SelIdents.size();
7006 NumSelIdents <= MethList->getMethod()->param_size()) {
7007 ParmVarDecl *Param =
7008 MethList->getMethod()->parameters()[NumSelIdents - 1];
7009 if (Param->getIdentifier()) {
7010 CodeCompletionBuilder Builder(Results.getAllocator(),
7011 Results.getCodeCompletionTUInfo());
7012 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
7013 Param->getIdentifier()->getName()));
7014 Results.AddResult(Builder.TakeString());
7021 Result R(MethList->getMethod(),
7022 Results.getBasePriority(MethList->getMethod()), nullptr);
7023 R.StartParameter = SelIdents.size();
7024 R.AllParametersAreInformative = false;
7025 R.DeclaringEntity = true;
7026 Results.MaybeAddResult(R, CurContext);
7030 Results.ExitScope();
7031 HandleCodeCompleteResults(this, CodeCompleter,
7032 CodeCompletionContext::CCC_Other,
7033 Results.data(),Results.size());
7036 void Sema::CodeCompletePreprocessorDirective(bool InConditional) {
7037 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
7038 CodeCompleter->getCodeCompletionTUInfo(),
7039 CodeCompletionContext::CCC_PreprocessorDirective);
7040 Results.EnterNewScope();
7043 CodeCompletionBuilder Builder(Results.getAllocator(),
7044 Results.getCodeCompletionTUInfo());
7045 Builder.AddTypedTextChunk("if");
7046 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7047 Builder.AddPlaceholderChunk("condition");
7048 Results.AddResult(Builder.TakeString());
7051 Builder.AddTypedTextChunk("ifdef");
7052 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7053 Builder.AddPlaceholderChunk("macro");
7054 Results.AddResult(Builder.TakeString());
7057 Builder.AddTypedTextChunk("ifndef");
7058 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7059 Builder.AddPlaceholderChunk("macro");
7060 Results.AddResult(Builder.TakeString());
7062 if (InConditional) {
7063 // #elif <condition>
7064 Builder.AddTypedTextChunk("elif");
7065 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7066 Builder.AddPlaceholderChunk("condition");
7067 Results.AddResult(Builder.TakeString());
7070 Builder.AddTypedTextChunk("else");
7071 Results.AddResult(Builder.TakeString());
7074 Builder.AddTypedTextChunk("endif");
7075 Results.AddResult(Builder.TakeString());
7078 // #include "header"
7079 Builder.AddTypedTextChunk("include");
7080 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7081 Builder.AddTextChunk("\"");
7082 Builder.AddPlaceholderChunk("header");
7083 Builder.AddTextChunk("\"");
7084 Results.AddResult(Builder.TakeString());
7086 // #include <header>
7087 Builder.AddTypedTextChunk("include");
7088 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7089 Builder.AddTextChunk("<");
7090 Builder.AddPlaceholderChunk("header");
7091 Builder.AddTextChunk(">");
7092 Results.AddResult(Builder.TakeString());
7095 Builder.AddTypedTextChunk("define");
7096 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7097 Builder.AddPlaceholderChunk("macro");
7098 Results.AddResult(Builder.TakeString());
7100 // #define <macro>(<args>)
7101 Builder.AddTypedTextChunk("define");
7102 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7103 Builder.AddPlaceholderChunk("macro");
7104 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7105 Builder.AddPlaceholderChunk("args");
7106 Builder.AddChunk(CodeCompletionString::CK_RightParen);
7107 Results.AddResult(Builder.TakeString());
7110 Builder.AddTypedTextChunk("undef");
7111 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7112 Builder.AddPlaceholderChunk("macro");
7113 Results.AddResult(Builder.TakeString());
7116 Builder.AddTypedTextChunk("line");
7117 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7118 Builder.AddPlaceholderChunk("number");
7119 Results.AddResult(Builder.TakeString());
7121 // #line <number> "filename"
7122 Builder.AddTypedTextChunk("line");
7123 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7124 Builder.AddPlaceholderChunk("number");
7125 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7126 Builder.AddTextChunk("\"");
7127 Builder.AddPlaceholderChunk("filename");
7128 Builder.AddTextChunk("\"");
7129 Results.AddResult(Builder.TakeString());
7132 Builder.AddTypedTextChunk("error");
7133 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7134 Builder.AddPlaceholderChunk("message");
7135 Results.AddResult(Builder.TakeString());
7137 // #pragma <arguments>
7138 Builder.AddTypedTextChunk("pragma");
7139 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7140 Builder.AddPlaceholderChunk("arguments");
7141 Results.AddResult(Builder.TakeString());
7143 if (getLangOpts().ObjC1) {
7145 Builder.AddTypedTextChunk("import");
7146 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7147 Builder.AddTextChunk("\"");
7148 Builder.AddPlaceholderChunk("header");
7149 Builder.AddTextChunk("\"");
7150 Results.AddResult(Builder.TakeString());
7153 Builder.AddTypedTextChunk("import");
7154 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7155 Builder.AddTextChunk("<");
7156 Builder.AddPlaceholderChunk("header");
7157 Builder.AddTextChunk(">");
7158 Results.AddResult(Builder.TakeString());
7161 // #include_next "header"
7162 Builder.AddTypedTextChunk("include_next");
7163 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7164 Builder.AddTextChunk("\"");
7165 Builder.AddPlaceholderChunk("header");
7166 Builder.AddTextChunk("\"");
7167 Results.AddResult(Builder.TakeString());
7169 // #include_next <header>
7170 Builder.AddTypedTextChunk("include_next");
7171 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7172 Builder.AddTextChunk("<");
7173 Builder.AddPlaceholderChunk("header");
7174 Builder.AddTextChunk(">");
7175 Results.AddResult(Builder.TakeString());
7177 // #warning <message>
7178 Builder.AddTypedTextChunk("warning");
7179 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7180 Builder.AddPlaceholderChunk("message");
7181 Results.AddResult(Builder.TakeString());
7183 // Note: #ident and #sccs are such crazy anachronisms that we don't provide
7184 // completions for them. And __include_macros is a Clang-internal extension
7185 // that we don't want to encourage anyone to use.
7187 // FIXME: we don't support #assert or #unassert, so don't suggest them.
7188 Results.ExitScope();
7190 HandleCodeCompleteResults(this, CodeCompleter,
7191 CodeCompletionContext::CCC_PreprocessorDirective,
7192 Results.data(), Results.size());
7195 void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) {
7196 CodeCompleteOrdinaryName(S,
7197 S->getFnParent()? Sema::PCC_RecoveryInFunction
7198 : Sema::PCC_Namespace);
7201 void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) {
7202 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
7203 CodeCompleter->getCodeCompletionTUInfo(),
7204 IsDefinition? CodeCompletionContext::CCC_MacroName
7205 : CodeCompletionContext::CCC_MacroNameUse);
7206 if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) {
7207 // Add just the names of macros, not their arguments.
7208 CodeCompletionBuilder Builder(Results.getAllocator(),
7209 Results.getCodeCompletionTUInfo());
7210 Results.EnterNewScope();
7211 for (Preprocessor::macro_iterator M = PP.macro_begin(),
7212 MEnd = PP.macro_end();
7214 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
7215 M->first->getName()));
7216 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
7218 CXCursor_MacroDefinition));
7220 Results.ExitScope();
7221 } else if (IsDefinition) {
7222 // FIXME: Can we detect when the user just wrote an include guard above?
7225 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
7226 Results.data(), Results.size());
7229 void Sema::CodeCompletePreprocessorExpression() {
7230 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
7231 CodeCompleter->getCodeCompletionTUInfo(),
7232 CodeCompletionContext::CCC_PreprocessorExpression);
7234 if (!CodeCompleter || CodeCompleter->includeMacros())
7235 AddMacroResults(PP, Results, true);
7237 // defined (<macro>)
7238 Results.EnterNewScope();
7239 CodeCompletionBuilder Builder(Results.getAllocator(),
7240 Results.getCodeCompletionTUInfo());
7241 Builder.AddTypedTextChunk("defined");
7242 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7243 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7244 Builder.AddPlaceholderChunk("macro");
7245 Builder.AddChunk(CodeCompletionString::CK_RightParen);
7246 Results.AddResult(Builder.TakeString());
7247 Results.ExitScope();
7249 HandleCodeCompleteResults(this, CodeCompleter,
7250 CodeCompletionContext::CCC_PreprocessorExpression,
7251 Results.data(), Results.size());
7254 void Sema::CodeCompletePreprocessorMacroArgument(Scope *S,
7255 IdentifierInfo *Macro,
7256 MacroInfo *MacroInfo,
7257 unsigned Argument) {
7258 // FIXME: In the future, we could provide "overload" results, much like we
7259 // do for function calls.
7261 // Now just ignore this. There will be another code-completion callback
7262 // for the expanded tokens.
7265 void Sema::CodeCompleteNaturalLanguage() {
7266 HandleCodeCompleteResults(this, CodeCompleter,
7267 CodeCompletionContext::CCC_NaturalLanguage,
7271 void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator,
7272 CodeCompletionTUInfo &CCTUInfo,
7273 SmallVectorImpl<CodeCompletionResult> &Results) {
7274 ResultBuilder Builder(*this, Allocator, CCTUInfo,
7275 CodeCompletionContext::CCC_Recovery);
7276 if (!CodeCompleter || CodeCompleter->includeGlobals()) {
7277 CodeCompletionDeclConsumer Consumer(Builder,
7278 Context.getTranslationUnitDecl());
7279 LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName,
7283 if (!CodeCompleter || CodeCompleter->includeMacros())
7284 AddMacroResults(PP, Builder, true);
7287 Results.insert(Results.end(),
7288 Builder.data(), Builder.data() + Builder.size());