blob: ebb6bbcd3454be0e19a386b536be0e00a939bb33 [file] [log] [blame]
Douglas Gregor2436e712009-09-17 21:32:03 +00001//===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the code-completion semantic actions.
11//
12//===----------------------------------------------------------------------===//
John McCall83024632010-08-25 22:03:47 +000013#include "clang/Sema/SemaInternal.h"
John McCallde6836a2010-08-24 07:21:54 +000014#include "clang/AST/DeclObjC.h"
Douglas Gregorf2510672009-09-21 19:57:38 +000015#include "clang/AST/ExprCXX.h"
Douglas Gregor8ce33212009-11-17 17:59:40 +000016#include "clang/AST/ExprObjC.h"
Jordan Rose4938f272013-02-09 10:09:43 +000017#include "clang/Basic/CharInfo.h"
Douglas Gregor07f43572012-01-29 18:15:03 +000018#include "clang/Lex/HeaderSearch.h"
Douglas Gregorf329c7c2009-10-30 16:50:04 +000019#include "clang/Lex/MacroInfo.h"
20#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#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"
Douglas Gregor1154e272010-09-16 16:06:31 +000027#include "llvm/ADT/DenseSet.h"
Benjamin Kramere0513cb2012-01-30 16:17:39 +000028#include "llvm/ADT/SmallBitVector.h"
Douglas Gregor3545ff42009-09-21 16:56:56 +000029#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000030#include "llvm/ADT/SmallString.h"
Douglas Gregore6688e62009-09-28 03:51:44 +000031#include "llvm/ADT/StringExtras.h"
Douglas Gregor9d2ddb22010-04-06 19:22:33 +000032#include "llvm/ADT/StringSwitch.h"
Douglas Gregor67c692c2010-08-26 15:07:07 +000033#include "llvm/ADT/Twine.h"
Douglas Gregor3545ff42009-09-21 16:56:56 +000034#include <list>
35#include <map>
36#include <vector>
Douglas Gregor2436e712009-09-17 21:32:03 +000037
38using namespace clang;
John McCallaab3e412010-08-25 08:40:02 +000039using namespace sema;
Douglas Gregor2436e712009-09-17 21:32:03 +000040
Douglas Gregor3545ff42009-09-21 16:56:56 +000041namespace {
42 /// \brief A container of code-completion results.
43 class ResultBuilder {
44 public:
45 /// \brief The type of a name-lookup filter, which can be provided to the
46 /// name-lookup routines to specify which declarations should be included in
47 /// the result set (when it returns true) and which declarations should be
48 /// filtered out (returns false).
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +000049 typedef bool (ResultBuilder::*LookupFilter)(const NamedDecl *) const;
Douglas Gregor3545ff42009-09-21 16:56:56 +000050
John McCall276321a2010-08-25 06:19:51 +000051 typedef CodeCompletionResult Result;
Douglas Gregor3545ff42009-09-21 16:56:56 +000052
53 private:
54 /// \brief The actual results we have found.
55 std::vector<Result> Results;
56
57 /// \brief A record of all of the declarations we have found and placed
58 /// into the result set, used to ensure that no declaration ever gets into
59 /// the result set twice.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +000060 llvm::SmallPtrSet<const Decl*, 16> AllDeclsFound;
Douglas Gregor3545ff42009-09-21 16:56:56 +000061
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +000062 typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair;
Douglas Gregor05e7ca32009-12-06 20:23:50 +000063
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 {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000068 typedef SmallVector<DeclIndexPair, 4> DeclIndexPairVector;
Douglas Gregor05e7ca32009-12-06 20:23:50 +000069
70 /// \brief Contains either the solitary NamedDecl * or a vector
71 /// of (declaration, index) pairs.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +000072 llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector*> DeclOrVector;
Douglas Gregor05e7ca32009-12-06 20:23:50 +000073
74 /// \brief When the entry contains a single declaration, this is
75 /// the index associated with that entry.
76 unsigned SingleDeclIndex;
77
78 public:
79 ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { }
80
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +000081 void Add(const NamedDecl *ND, unsigned Index) {
Douglas Gregor05e7ca32009-12-06 20:23:50 +000082 if (DeclOrVector.isNull()) {
83 // 0 - > 1 elements: just set the single element information.
84 DeclOrVector = ND;
85 SingleDeclIndex = Index;
86 return;
87 }
88
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +000089 if (const NamedDecl *PrevND =
90 DeclOrVector.dyn_cast<const NamedDecl *>()) {
Douglas Gregor05e7ca32009-12-06 20:23:50 +000091 // 1 -> 2 elements: create the vector of results and push in the
92 // existing declaration.
93 DeclIndexPairVector *Vec = new DeclIndexPairVector;
94 Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex));
95 DeclOrVector = Vec;
96 }
97
98 // Add the new element to the end of the vector.
99 DeclOrVector.get<DeclIndexPairVector*>()->push_back(
100 DeclIndexPair(ND, Index));
101 }
102
103 void Destroy() {
104 if (DeclIndexPairVector *Vec
105 = DeclOrVector.dyn_cast<DeclIndexPairVector *>()) {
106 delete Vec;
Craig Topperc3ec1492014-05-26 06:22:03 +0000107 DeclOrVector = ((NamedDecl *)nullptr);
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000108 }
109 }
110
111 // Iteration.
112 class iterator;
113 iterator begin() const;
114 iterator end() const;
115 };
116
Douglas Gregor3545ff42009-09-21 16:56:56 +0000117 /// \brief A mapping from declaration names to the declarations that have
118 /// this name within a particular scope and their index within the list of
119 /// results.
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000120 typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000121
122 /// \brief The semantic analysis object for which results are being
123 /// produced.
124 Sema &SemaRef;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000125
126 /// \brief The allocator used to allocate new code-completion strings.
Douglas Gregorbcbf46c2011-02-01 22:57:45 +0000127 CodeCompletionAllocator &Allocator;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000128
129 CodeCompletionTUInfo &CCTUInfo;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000130
131 /// \brief If non-NULL, a filter function used to remove any code-completion
132 /// results that are not desirable.
133 LookupFilter Filter;
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000134
135 /// \brief Whether we should allow declarations as
136 /// nested-name-specifiers that would otherwise be filtered out.
137 bool AllowNestedNameSpecifiers;
138
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000139 /// \brief If set, the type that we would prefer our resulting value
140 /// declarations to have.
141 ///
142 /// Closely matching the preferred type gives a boost to a result's
143 /// priority.
144 CanQualType PreferredType;
145
Douglas Gregor3545ff42009-09-21 16:56:56 +0000146 /// \brief A list of shadow maps, which is used to model name hiding at
147 /// different levels of, e.g., the inheritance hierarchy.
148 std::list<ShadowMap> ShadowMaps;
149
Douglas Gregor9be0ed42010-08-26 16:36:48 +0000150 /// \brief If we're potentially referring to a C++ member function, the set
151 /// of qualifiers applied to the object type.
152 Qualifiers ObjectTypeQualifiers;
153
154 /// \brief Whether the \p ObjectTypeQualifiers field is active.
155 bool HasObjectTypeQualifiers;
156
Douglas Gregorc2cb2e22010-08-27 15:29:55 +0000157 /// \brief The selector that we prefer.
158 Selector PreferredSelector;
159
Douglas Gregor05fcf842010-11-02 20:36:02 +0000160 /// \brief The completion context in which we are gathering results.
Douglas Gregor50832e02010-09-20 22:39:41 +0000161 CodeCompletionContext CompletionContext;
162
James Dennett596e4752012-06-14 03:11:41 +0000163 /// \brief If we are in an instance method definition, the \@implementation
Douglas Gregor05fcf842010-11-02 20:36:02 +0000164 /// object.
165 ObjCImplementationDecl *ObjCImplementation;
Douglas Gregor0a0e2b32013-01-31 04:52:16 +0000166
Douglas Gregor50832e02010-09-20 22:39:41 +0000167 void AdjustResultPriorityForDecl(Result &R);
Douglas Gregor95887f92010-07-08 23:20:03 +0000168
Douglas Gregor0212fd72010-09-21 16:06:22 +0000169 void MaybeAddConstructorResults(Result R);
170
Douglas Gregor3545ff42009-09-21 16:56:56 +0000171 public:
Douglas Gregorbcbf46c2011-02-01 22:57:45 +0000172 explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator,
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000173 CodeCompletionTUInfo &CCTUInfo,
Douglas Gregor0ac41382010-09-23 23:01:17 +0000174 const CodeCompletionContext &CompletionContext,
Craig Topperc3ec1492014-05-26 06:22:03 +0000175 LookupFilter Filter = nullptr)
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000176 : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo),
177 Filter(Filter),
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000178 AllowNestedNameSpecifiers(false), HasObjectTypeQualifiers(false),
Douglas Gregor05fcf842010-11-02 20:36:02 +0000179 CompletionContext(CompletionContext),
Craig Topperc3ec1492014-05-26 06:22:03 +0000180 ObjCImplementation(nullptr)
Douglas Gregor05fcf842010-11-02 20:36:02 +0000181 {
182 // If this is an Objective-C instance method definition, dig out the
183 // corresponding implementation.
184 switch (CompletionContext.getKind()) {
185 case CodeCompletionContext::CCC_Expression:
186 case CodeCompletionContext::CCC_ObjCMessageReceiver:
187 case CodeCompletionContext::CCC_ParenthesizedExpression:
188 case CodeCompletionContext::CCC_Statement:
189 case CodeCompletionContext::CCC_Recovery:
190 if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl())
191 if (Method->isInstanceMethod())
192 if (ObjCInterfaceDecl *Interface = Method->getClassInterface())
193 ObjCImplementation = Interface->getImplementation();
194 break;
195
196 default:
197 break;
198 }
199 }
Douglas Gregor0a0e2b32013-01-31 04:52:16 +0000200
201 /// \brief Determine the priority for a reference to the given declaration.
202 unsigned getBasePriority(const NamedDecl *D);
203
Douglas Gregorf64acca2010-05-25 21:41:55 +0000204 /// \brief Whether we should include code patterns in the completion
205 /// results.
206 bool includeCodePatterns() const {
207 return SemaRef.CodeCompleter &&
Douglas Gregorac322ec2010-08-27 21:18:54 +0000208 SemaRef.CodeCompleter->includeCodePatterns();
Douglas Gregorf64acca2010-05-25 21:41:55 +0000209 }
210
Douglas Gregor3545ff42009-09-21 16:56:56 +0000211 /// \brief Set the filter used for code-completion results.
212 void setFilter(LookupFilter Filter) {
213 this->Filter = Filter;
214 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000215
216 Result *data() { return Results.empty()? nullptr : &Results.front(); }
Douglas Gregor3545ff42009-09-21 16:56:56 +0000217 unsigned size() const { return Results.size(); }
218 bool empty() const { return Results.empty(); }
219
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000220 /// \brief Specify the preferred type.
221 void setPreferredType(QualType T) {
222 PreferredType = SemaRef.Context.getCanonicalType(T);
223 }
224
Douglas Gregor9be0ed42010-08-26 16:36:48 +0000225 /// \brief Set the cv-qualifiers on the object type, for us in filtering
226 /// calls to member functions.
227 ///
228 /// When there are qualifiers in this set, they will be used to filter
229 /// out member functions that aren't available (because there will be a
230 /// cv-qualifier mismatch) or prefer functions with an exact qualifier
231 /// match.
232 void setObjectTypeQualifiers(Qualifiers Quals) {
233 ObjectTypeQualifiers = Quals;
234 HasObjectTypeQualifiers = true;
235 }
236
Douglas Gregorc2cb2e22010-08-27 15:29:55 +0000237 /// \brief Set the preferred selector.
238 ///
239 /// When an Objective-C method declaration result is added, and that
240 /// method's selector matches this preferred selector, we give that method
241 /// a slight priority boost.
242 void setPreferredSelector(Selector Sel) {
243 PreferredSelector = Sel;
244 }
Douglas Gregor05fcf842010-11-02 20:36:02 +0000245
Douglas Gregor50832e02010-09-20 22:39:41 +0000246 /// \brief Retrieve the code-completion context for which results are
247 /// being collected.
248 const CodeCompletionContext &getCompletionContext() const {
249 return CompletionContext;
250 }
251
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000252 /// \brief Specify whether nested-name-specifiers are allowed.
253 void allowNestedNameSpecifiers(bool Allow = true) {
254 AllowNestedNameSpecifiers = Allow;
255 }
256
Douglas Gregor74661272010-09-21 00:03:25 +0000257 /// \brief Return the semantic analysis object for which we are collecting
258 /// code completion results.
259 Sema &getSema() const { return SemaRef; }
260
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000261 /// \brief Retrieve the allocator used to allocate code completion strings.
Douglas Gregorbcbf46c2011-02-01 22:57:45 +0000262 CodeCompletionAllocator &getAllocator() const { return Allocator; }
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000263
264 CodeCompletionTUInfo &getCodeCompletionTUInfo() const { return CCTUInfo; }
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000265
Douglas Gregor7c208612010-01-14 00:20:49 +0000266 /// \brief Determine whether the given declaration is at all interesting
267 /// as a code-completion result.
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000268 ///
269 /// \param ND the declaration that we are inspecting.
270 ///
271 /// \param AsNestedNameSpecifier will be set true if this declaration is
272 /// only interesting when it is a nested-name-specifier.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000273 bool isInterestingDecl(const NamedDecl *ND,
274 bool &AsNestedNameSpecifier) const;
Douglas Gregore0717ab2010-01-14 00:41:07 +0000275
276 /// \brief Check whether the result is hidden by the Hiding declaration.
277 ///
278 /// \returns true if the result is hidden and cannot be found, false if
279 /// the hidden result could still be found. When false, \p R may be
280 /// modified to describe how the result can be found (e.g., via extra
281 /// qualification).
282 bool CheckHiddenResult(Result &R, DeclContext *CurContext,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000283 const NamedDecl *Hiding);
Douglas Gregore0717ab2010-01-14 00:41:07 +0000284
Douglas Gregor3545ff42009-09-21 16:56:56 +0000285 /// \brief Add a new result to this result set (if it isn't already in one
286 /// of the shadow maps), or replace an existing result (for, e.g., a
287 /// redeclaration).
Douglas Gregor2af2f672009-09-21 20:12:40 +0000288 ///
Douglas Gregord8c61782012-02-15 15:34:24 +0000289 /// \param R the result to add (if it is unique).
Douglas Gregor2af2f672009-09-21 20:12:40 +0000290 ///
Douglas Gregord8c61782012-02-15 15:34:24 +0000291 /// \param CurContext the context in which this result will be named.
Craig Topperc3ec1492014-05-26 06:22:03 +0000292 void MaybeAddResult(Result R, DeclContext *CurContext = nullptr);
293
Douglas Gregorc580c522010-01-14 01:09:38 +0000294 /// \brief Add a new result to this result set, where we already know
Yaron Keren8fbe43982014-11-14 18:33:42 +0000295 /// the hiding declaration (if any).
Douglas Gregorc580c522010-01-14 01:09:38 +0000296 ///
297 /// \param R the result to add (if it is unique).
298 ///
299 /// \param CurContext the context in which this result will be named.
300 ///
301 /// \param Hiding the declaration that hides the result.
Douglas Gregor09bbc652010-01-14 15:47:35 +0000302 ///
303 /// \param InBaseClass whether the result was found in a base
304 /// class of the searched context.
305 void AddResult(Result R, DeclContext *CurContext, NamedDecl *Hiding,
306 bool InBaseClass);
Douglas Gregorc580c522010-01-14 01:09:38 +0000307
Douglas Gregor78a21012010-01-14 16:01:26 +0000308 /// \brief Add a new non-declaration result to this result set.
309 void AddResult(Result R);
310
Douglas Gregor3545ff42009-09-21 16:56:56 +0000311 /// \brief Enter into a new scope.
312 void EnterNewScope();
313
314 /// \brief Exit from the current scope.
315 void ExitScope();
316
Douglas Gregorbaf69612009-11-18 04:19:12 +0000317 /// \brief Ignore this declaration, if it is seen again.
Dmitri Gribenko6cfb1532013-02-14 13:53:30 +0000318 void Ignore(const Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); }
Douglas Gregorbaf69612009-11-18 04:19:12 +0000319
Douglas Gregor3545ff42009-09-21 16:56:56 +0000320 /// \name Name lookup predicates
321 ///
322 /// These predicates can be passed to the name lookup functions to filter the
323 /// results of name lookup. All of the predicates have the same type, so that
324 ///
325 //@{
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000326 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;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000343 //@}
344 };
345}
346
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000347class ResultBuilder::ShadowMapEntry::iterator {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000348 llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator;
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000349 unsigned SingleDeclIndex;
350
351public:
352 typedef DeclIndexPair value_type;
353 typedef value_type reference;
354 typedef std::ptrdiff_t difference_type;
355 typedef std::input_iterator_tag iterator_category;
356
357 class pointer {
358 DeclIndexPair Value;
359
360 public:
361 pointer(const DeclIndexPair &Value) : Value(Value) { }
362
363 const DeclIndexPair *operator->() const {
364 return &Value;
365 }
366 };
Craig Topperc3ec1492014-05-26 06:22:03 +0000367
368 iterator() : DeclOrIterator((NamedDecl *)nullptr), SingleDeclIndex(0) {}
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000369
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000370 iterator(const NamedDecl *SingleDecl, unsigned Index)
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000371 : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { }
372
373 iterator(const DeclIndexPair *Iterator)
374 : DeclOrIterator(Iterator), SingleDeclIndex(0) { }
375
376 iterator &operator++() {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000377 if (DeclOrIterator.is<const NamedDecl *>()) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000378 DeclOrIterator = (NamedDecl *)nullptr;
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000379 SingleDeclIndex = 0;
380 return *this;
381 }
382
383 const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>();
384 ++I;
385 DeclOrIterator = I;
386 return *this;
387 }
388
Chris Lattner9795b392010-09-04 18:12:20 +0000389 /*iterator operator++(int) {
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000390 iterator tmp(*this);
391 ++(*this);
392 return tmp;
Chris Lattner9795b392010-09-04 18:12:20 +0000393 }*/
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000394
395 reference operator*() const {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000396 if (const NamedDecl *ND = DeclOrIterator.dyn_cast<const NamedDecl *>())
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000397 return reference(ND, SingleDeclIndex);
398
Douglas Gregor94bb5e82009-12-06 21:27:58 +0000399 return *DeclOrIterator.get<const DeclIndexPair*>();
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000400 }
401
402 pointer operator->() const {
403 return pointer(**this);
404 }
405
406 friend bool operator==(const iterator &X, const iterator &Y) {
Douglas Gregor94bb5e82009-12-06 21:27:58 +0000407 return X.DeclOrIterator.getOpaqueValue()
408 == Y.DeclOrIterator.getOpaqueValue() &&
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000409 X.SingleDeclIndex == Y.SingleDeclIndex;
410 }
411
412 friend bool operator!=(const iterator &X, const iterator &Y) {
Douglas Gregor94bb5e82009-12-06 21:27:58 +0000413 return !(X == Y);
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000414 }
415};
416
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000417ResultBuilder::ShadowMapEntry::iterator
418ResultBuilder::ShadowMapEntry::begin() const {
419 if (DeclOrVector.isNull())
420 return iterator();
421
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000422 if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>())
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000423 return iterator(ND, SingleDeclIndex);
424
425 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin());
426}
427
428ResultBuilder::ShadowMapEntry::iterator
429ResultBuilder::ShadowMapEntry::end() const {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000430 if (DeclOrVector.is<const NamedDecl *>() || DeclOrVector.isNull())
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000431 return iterator();
432
433 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end());
434}
435
Douglas Gregor2af2f672009-09-21 20:12:40 +0000436/// \brief Compute the qualification required to get from the current context
437/// (\p CurContext) to the target context (\p TargetContext).
438///
439/// \param Context the AST context in which the qualification will be used.
440///
441/// \param CurContext the context where an entity is being named, which is
442/// typically based on the current scope.
443///
444/// \param TargetContext the context in which the named entity actually
445/// resides.
446///
447/// \returns a nested name specifier that refers into the target context, or
448/// NULL if no qualification is needed.
449static NestedNameSpecifier *
450getRequiredQualification(ASTContext &Context,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000451 const DeclContext *CurContext,
452 const DeclContext *TargetContext) {
453 SmallVector<const DeclContext *, 4> TargetParents;
Douglas Gregor2af2f672009-09-21 20:12:40 +0000454
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000455 for (const DeclContext *CommonAncestor = TargetContext;
Douglas Gregor2af2f672009-09-21 20:12:40 +0000456 CommonAncestor && !CommonAncestor->Encloses(CurContext);
457 CommonAncestor = CommonAncestor->getLookupParent()) {
458 if (CommonAncestor->isTransparentContext() ||
459 CommonAncestor->isFunctionOrMethod())
460 continue;
461
462 TargetParents.push_back(CommonAncestor);
463 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000464
465 NestedNameSpecifier *Result = nullptr;
Douglas Gregor2af2f672009-09-21 20:12:40 +0000466 while (!TargetParents.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000467 const DeclContext *Parent = TargetParents.pop_back_val();
468
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000469 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent)) {
Douglas Gregor68762e72010-08-23 21:17:50 +0000470 if (!Namespace->getIdentifier())
471 continue;
472
Douglas Gregor2af2f672009-09-21 20:12:40 +0000473 Result = NestedNameSpecifier::Create(Context, Result, Namespace);
Douglas Gregor68762e72010-08-23 21:17:50 +0000474 }
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000475 else if (const TagDecl *TD = dyn_cast<TagDecl>(Parent))
Douglas Gregor2af2f672009-09-21 20:12:40 +0000476 Result = NestedNameSpecifier::Create(Context, Result,
477 false,
478 Context.getTypeDeclType(TD).getTypePtr());
Douglas Gregor9eb77012009-11-07 00:00:49 +0000479 }
Douglas Gregor2af2f672009-09-21 20:12:40 +0000480 return Result;
481}
482
Alp Toker034bbd52014-06-30 01:33:53 +0000483/// Determine whether \p Id is a name reserved for the implementation (C99
484/// 7.1.3, C++ [lib.global.names]).
485static bool isReservedName(const IdentifierInfo *Id) {
486 if (Id->getLength() < 2)
487 return false;
488 const char *Name = Id->getNameStart();
489 return Name[0] == '_' &&
490 (Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z'));
491}
492
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000493bool ResultBuilder::isInterestingDecl(const NamedDecl *ND,
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000494 bool &AsNestedNameSpecifier) const {
495 AsNestedNameSpecifier = false;
496
Douglas Gregor7c208612010-01-14 00:20:49 +0000497 ND = ND->getUnderlyingDecl();
Douglas Gregor58acf322009-10-09 22:16:47 +0000498
499 // Skip unnamed entities.
Douglas Gregor7c208612010-01-14 00:20:49 +0000500 if (!ND->getDeclName())
501 return false;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000502
503 // Friend declarations and declarations introduced due to friends are never
504 // added as results.
Richard Smith6eece292015-01-15 02:27:20 +0000505 if (ND->getFriendObjectKind() == Decl::FOK_Undeclared)
Douglas Gregor7c208612010-01-14 00:20:49 +0000506 return false;
507
Douglas Gregor99fe2ad2009-12-11 17:31:05 +0000508 // Class template (partial) specializations are never added as results.
Douglas Gregor7c208612010-01-14 00:20:49 +0000509 if (isa<ClassTemplateSpecializationDecl>(ND) ||
510 isa<ClassTemplatePartialSpecializationDecl>(ND))
511 return false;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000512
Douglas Gregor99fe2ad2009-12-11 17:31:05 +0000513 // Using declarations themselves are never added as results.
Douglas Gregor7c208612010-01-14 00:20:49 +0000514 if (isa<UsingDecl>(ND))
515 return false;
516
517 // Some declarations have reserved names that we don't want to ever show.
Alp Toker034bbd52014-06-30 01:33:53 +0000518 // Filter out names reserved for the implementation if they come from a
519 // system header.
520 // TODO: Add a predicate for this.
521 if (const IdentifierInfo *Id = ND->getIdentifier())
522 if (isReservedName(Id) &&
523 (ND->getLocation().isInvalid() ||
524 SemaRef.SourceMgr.isInSystemHeader(
525 SemaRef.SourceMgr.getSpellingLoc(ND->getLocation()))))
Douglas Gregor7c208612010-01-14 00:20:49 +0000526 return false;
Douglas Gregor2927c0c2010-11-09 03:59:40 +0000527
Douglas Gregor59cab552010-08-16 23:05:20 +0000528 if (Filter == &ResultBuilder::IsNestedNameSpecifier ||
529 ((isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) &&
530 Filter != &ResultBuilder::IsNamespace &&
Douglas Gregor0ac41382010-09-23 23:01:17 +0000531 Filter != &ResultBuilder::IsNamespaceOrAlias &&
Craig Topperc3ec1492014-05-26 06:22:03 +0000532 Filter != nullptr))
Douglas Gregor59cab552010-08-16 23:05:20 +0000533 AsNestedNameSpecifier = true;
534
Douglas Gregor3545ff42009-09-21 16:56:56 +0000535 // Filter out any unwanted results.
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000536 if (Filter && !(this->*Filter)(ND)) {
537 // Check whether it is interesting as a nested-name-specifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000538 if (AllowNestedNameSpecifiers && SemaRef.getLangOpts().CPlusPlus &&
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000539 IsNestedNameSpecifier(ND) &&
540 (Filter != &ResultBuilder::IsMember ||
541 (isa<CXXRecordDecl>(ND) &&
542 cast<CXXRecordDecl>(ND)->isInjectedClassName()))) {
543 AsNestedNameSpecifier = true;
544 return true;
545 }
546
Douglas Gregor7c208612010-01-14 00:20:49 +0000547 return false;
Douglas Gregor59cab552010-08-16 23:05:20 +0000548 }
Douglas Gregor7c208612010-01-14 00:20:49 +0000549 // ... then it must be interesting!
550 return true;
551}
552
Douglas Gregore0717ab2010-01-14 00:41:07 +0000553bool ResultBuilder::CheckHiddenResult(Result &R, DeclContext *CurContext,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000554 const NamedDecl *Hiding) {
Douglas Gregore0717ab2010-01-14 00:41:07 +0000555 // In C, there is no way to refer to a hidden name.
556 // FIXME: This isn't true; we can find a tag name hidden by an ordinary
557 // name if we introduce the tag type.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000558 if (!SemaRef.getLangOpts().CPlusPlus)
Douglas Gregore0717ab2010-01-14 00:41:07 +0000559 return true;
560
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000561 const DeclContext *HiddenCtx =
562 R.Declaration->getDeclContext()->getRedeclContext();
Douglas Gregore0717ab2010-01-14 00:41:07 +0000563
564 // There is no way to qualify a name declared in a function or method.
565 if (HiddenCtx->isFunctionOrMethod())
566 return true;
567
Sebastian Redl50c68252010-08-31 00:36:30 +0000568 if (HiddenCtx == Hiding->getDeclContext()->getRedeclContext())
Douglas Gregore0717ab2010-01-14 00:41:07 +0000569 return true;
570
571 // We can refer to the result with the appropriate qualification. Do it.
572 R.Hidden = true;
573 R.QualifierIsInformative = false;
574
575 if (!R.Qualifier)
576 R.Qualifier = getRequiredQualification(SemaRef.Context,
577 CurContext,
578 R.Declaration->getDeclContext());
579 return false;
580}
581
Douglas Gregor95887f92010-07-08 23:20:03 +0000582/// \brief A simplified classification of types used to determine whether two
583/// types are "similar enough" when adjusting priorities.
Douglas Gregor6e240332010-08-16 16:18:59 +0000584SimplifiedTypeClass clang::getSimplifiedTypeClass(CanQualType T) {
Douglas Gregor95887f92010-07-08 23:20:03 +0000585 switch (T->getTypeClass()) {
586 case Type::Builtin:
587 switch (cast<BuiltinType>(T)->getKind()) {
588 case BuiltinType::Void:
589 return STC_Void;
590
591 case BuiltinType::NullPtr:
592 return STC_Pointer;
593
594 case BuiltinType::Overload:
595 case BuiltinType::Dependent:
Douglas Gregor95887f92010-07-08 23:20:03 +0000596 return STC_Other;
597
598 case BuiltinType::ObjCId:
599 case BuiltinType::ObjCClass:
600 case BuiltinType::ObjCSel:
601 return STC_ObjectiveC;
602
603 default:
604 return STC_Arithmetic;
605 }
David Blaikie8a40f702012-01-17 06:56:22 +0000606
Douglas Gregor95887f92010-07-08 23:20:03 +0000607 case Type::Complex:
608 return STC_Arithmetic;
609
610 case Type::Pointer:
611 return STC_Pointer;
612
613 case Type::BlockPointer:
614 return STC_Block;
615
616 case Type::LValueReference:
617 case Type::RValueReference:
618 return getSimplifiedTypeClass(T->getAs<ReferenceType>()->getPointeeType());
619
620 case Type::ConstantArray:
621 case Type::IncompleteArray:
622 case Type::VariableArray:
623 case Type::DependentSizedArray:
624 return STC_Array;
625
626 case Type::DependentSizedExtVector:
627 case Type::Vector:
628 case Type::ExtVector:
629 return STC_Arithmetic;
630
631 case Type::FunctionProto:
632 case Type::FunctionNoProto:
633 return STC_Function;
634
635 case Type::Record:
636 return STC_Record;
637
638 case Type::Enum:
639 return STC_Arithmetic;
640
641 case Type::ObjCObject:
642 case Type::ObjCInterface:
643 case Type::ObjCObjectPointer:
644 return STC_ObjectiveC;
645
646 default:
647 return STC_Other;
648 }
649}
650
651/// \brief Get the type that a given expression will have if this declaration
652/// is used as an expression in its "typical" code-completion form.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000653QualType clang::getDeclUsageType(ASTContext &C, const NamedDecl *ND) {
Douglas Gregor95887f92010-07-08 23:20:03 +0000654 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
655
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000656 if (const TypeDecl *Type = dyn_cast<TypeDecl>(ND))
Douglas Gregor95887f92010-07-08 23:20:03 +0000657 return C.getTypeDeclType(Type);
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000658 if (const ObjCInterfaceDecl *Iface = dyn_cast<ObjCInterfaceDecl>(ND))
Douglas Gregor95887f92010-07-08 23:20:03 +0000659 return C.getObjCInterfaceType(Iface);
660
661 QualType T;
Alp Tokera2794f92014-01-22 07:29:52 +0000662 if (const FunctionDecl *Function = ND->getAsFunction())
Douglas Gregor603d81b2010-07-13 08:18:22 +0000663 T = Function->getCallResultType();
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000664 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
Douglas Gregor603d81b2010-07-13 08:18:22 +0000665 T = Method->getSendResultType();
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000666 else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND))
Douglas Gregor95887f92010-07-08 23:20:03 +0000667 T = C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext()));
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000668 else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND))
Douglas Gregor95887f92010-07-08 23:20:03 +0000669 T = Property->getType();
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000670 else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND))
Douglas Gregor95887f92010-07-08 23:20:03 +0000671 T = Value->getType();
672 else
673 return QualType();
Douglas Gregoraf670a82011-04-14 20:33:34 +0000674
675 // Dig through references, function pointers, and block pointers to
676 // get down to the likely type of an expression when the entity is
677 // used.
678 do {
679 if (const ReferenceType *Ref = T->getAs<ReferenceType>()) {
680 T = Ref->getPointeeType();
681 continue;
682 }
683
684 if (const PointerType *Pointer = T->getAs<PointerType>()) {
685 if (Pointer->getPointeeType()->isFunctionType()) {
686 T = Pointer->getPointeeType();
687 continue;
688 }
689
690 break;
691 }
692
693 if (const BlockPointerType *Block = T->getAs<BlockPointerType>()) {
694 T = Block->getPointeeType();
695 continue;
696 }
697
698 if (const FunctionType *Function = T->getAs<FunctionType>()) {
Alp Toker314cc812014-01-25 16:55:45 +0000699 T = Function->getReturnType();
Douglas Gregoraf670a82011-04-14 20:33:34 +0000700 continue;
701 }
702
703 break;
704 } while (true);
705
706 return T;
Douglas Gregor95887f92010-07-08 23:20:03 +0000707}
708
Douglas Gregor0a0e2b32013-01-31 04:52:16 +0000709unsigned ResultBuilder::getBasePriority(const NamedDecl *ND) {
710 if (!ND)
711 return CCP_Unlikely;
712
713 // Context-based decisions.
Richard Smith541b38b2013-09-20 01:15:31 +0000714 const DeclContext *LexicalDC = ND->getLexicalDeclContext();
715 if (LexicalDC->isFunctionOrMethod()) {
Douglas Gregor0a0e2b32013-01-31 04:52:16 +0000716 // _cmd is relatively rare
717 if (const ImplicitParamDecl *ImplicitParam =
718 dyn_cast<ImplicitParamDecl>(ND))
719 if (ImplicitParam->getIdentifier() &&
720 ImplicitParam->getIdentifier()->isStr("_cmd"))
721 return CCP_ObjC_cmd;
722
723 return CCP_LocalDeclaration;
724 }
Richard Smith541b38b2013-09-20 01:15:31 +0000725
726 const DeclContext *DC = ND->getDeclContext()->getRedeclContext();
Douglas Gregor0a0e2b32013-01-31 04:52:16 +0000727 if (DC->isRecord() || isa<ObjCContainerDecl>(DC))
728 return CCP_MemberDeclaration;
729
730 // Content-based decisions.
731 if (isa<EnumConstantDecl>(ND))
732 return CCP_Constant;
733
Douglas Gregor52e0de42013-01-31 05:03:46 +0000734 // Use CCP_Type for type declarations unless we're in a statement, Objective-C
735 // message receiver, or parenthesized expression context. There, it's as
736 // likely that the user will want to write a type as other declarations.
737 if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) &&
738 !(CompletionContext.getKind() == CodeCompletionContext::CCC_Statement ||
739 CompletionContext.getKind()
740 == CodeCompletionContext::CCC_ObjCMessageReceiver ||
741 CompletionContext.getKind()
742 == CodeCompletionContext::CCC_ParenthesizedExpression))
Douglas Gregor0a0e2b32013-01-31 04:52:16 +0000743 return CCP_Type;
744
745 return CCP_Declaration;
746}
747
Douglas Gregor50832e02010-09-20 22:39:41 +0000748void ResultBuilder::AdjustResultPriorityForDecl(Result &R) {
749 // If this is an Objective-C method declaration whose selector matches our
750 // preferred selector, give it a priority boost.
751 if (!PreferredSelector.isNull())
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000752 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(R.Declaration))
Douglas Gregor50832e02010-09-20 22:39:41 +0000753 if (PreferredSelector == Method->getSelector())
754 R.Priority += CCD_SelectorMatch;
Douglas Gregor5fb901d2010-09-20 23:11:55 +0000755
Douglas Gregor50832e02010-09-20 22:39:41 +0000756 // If we have a preferred type, adjust the priority for results with exactly-
757 // matching or nearly-matching types.
758 if (!PreferredType.isNull()) {
759 QualType T = getDeclUsageType(SemaRef.Context, R.Declaration);
760 if (!T.isNull()) {
761 CanQualType TC = SemaRef.Context.getCanonicalType(T);
762 // Check for exactly-matching types (modulo qualifiers).
763 if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC))
764 R.Priority /= CCF_ExactTypeMatch;
765 // Check for nearly-matching types, based on classification of each.
766 else if ((getSimplifiedTypeClass(PreferredType)
Douglas Gregor95887f92010-07-08 23:20:03 +0000767 == getSimplifiedTypeClass(TC)) &&
Douglas Gregor50832e02010-09-20 22:39:41 +0000768 !(PreferredType->isEnumeralType() && TC->isEnumeralType()))
769 R.Priority /= CCF_SimilarTypeMatch;
770 }
771 }
Douglas Gregor95887f92010-07-08 23:20:03 +0000772}
773
Douglas Gregor0212fd72010-09-21 16:06:22 +0000774void ResultBuilder::MaybeAddConstructorResults(Result R) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000775 if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration ||
Douglas Gregor0212fd72010-09-21 16:06:22 +0000776 !CompletionContext.wantConstructorResults())
777 return;
778
779 ASTContext &Context = SemaRef.Context;
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000780 const NamedDecl *D = R.Declaration;
Craig Topperc3ec1492014-05-26 06:22:03 +0000781 const CXXRecordDecl *Record = nullptr;
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000782 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D))
Douglas Gregor0212fd72010-09-21 16:06:22 +0000783 Record = ClassTemplate->getTemplatedDecl();
784 else if ((Record = dyn_cast<CXXRecordDecl>(D))) {
785 // Skip specializations and partial specializations.
786 if (isa<ClassTemplateSpecializationDecl>(Record))
787 return;
788 } else {
789 // There are no constructors here.
790 return;
791 }
792
793 Record = Record->getDefinition();
794 if (!Record)
795 return;
796
797
798 QualType RecordTy = Context.getTypeDeclType(Record);
799 DeclarationName ConstructorName
800 = Context.DeclarationNames.getCXXConstructorName(
801 Context.getCanonicalType(RecordTy));
Richard Smithcf4bdde2015-02-21 02:45:19 +0000802 DeclContext::lookup_result Ctors = Record->lookup(ConstructorName);
803 for (DeclContext::lookup_iterator I = Ctors.begin(),
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000804 E = Ctors.end();
805 I != E; ++I) {
David Blaikieff7d47a2012-12-19 00:45:41 +0000806 R.Declaration = *I;
Douglas Gregor0212fd72010-09-21 16:06:22 +0000807 R.CursorKind = getCursorKindForDecl(R.Declaration);
808 Results.push_back(R);
809 }
810}
811
Douglas Gregor7c208612010-01-14 00:20:49 +0000812void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) {
813 assert(!ShadowMaps.empty() && "Must enter into a results scope");
814
815 if (R.Kind != Result::RK_Declaration) {
816 // For non-declaration results, just add the result.
817 Results.push_back(R);
818 return;
819 }
820
821 // Look through using declarations.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000822 if (const UsingShadowDecl *Using =
823 dyn_cast<UsingShadowDecl>(R.Declaration)) {
Douglas Gregor0a0e2b32013-01-31 04:52:16 +0000824 MaybeAddResult(Result(Using->getTargetDecl(),
825 getBasePriority(Using->getTargetDecl()),
826 R.Qualifier),
827 CurContext);
Douglas Gregor7c208612010-01-14 00:20:49 +0000828 return;
829 }
830
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000831 const Decl *CanonDecl = R.Declaration->getCanonicalDecl();
Douglas Gregor7c208612010-01-14 00:20:49 +0000832 unsigned IDNS = CanonDecl->getIdentifierNamespace();
833
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000834 bool AsNestedNameSpecifier = false;
835 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
Douglas Gregor7c208612010-01-14 00:20:49 +0000836 return;
837
Douglas Gregor0212fd72010-09-21 16:06:22 +0000838 // C++ constructors are never found by name lookup.
839 if (isa<CXXConstructorDecl>(R.Declaration))
840 return;
841
Douglas Gregor3545ff42009-09-21 16:56:56 +0000842 ShadowMap &SMap = ShadowMaps.back();
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000843 ShadowMapEntry::iterator I, IEnd;
844 ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName());
845 if (NamePos != SMap.end()) {
846 I = NamePos->second.begin();
847 IEnd = NamePos->second.end();
848 }
849
850 for (; I != IEnd; ++I) {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000851 const NamedDecl *ND = I->first;
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000852 unsigned Index = I->second;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000853 if (ND->getCanonicalDecl() == CanonDecl) {
854 // This is a redeclaration. Always pick the newer declaration.
Douglas Gregor3545ff42009-09-21 16:56:56 +0000855 Results[Index].Declaration = R.Declaration;
856
Douglas Gregor3545ff42009-09-21 16:56:56 +0000857 // We're done.
858 return;
859 }
860 }
861
862 // This is a new declaration in this scope. However, check whether this
863 // declaration name is hidden by a similarly-named declaration in an outer
864 // scope.
865 std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end();
866 --SMEnd;
867 for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) {
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000868 ShadowMapEntry::iterator I, IEnd;
869 ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName());
870 if (NamePos != SM->end()) {
871 I = NamePos->second.begin();
872 IEnd = NamePos->second.end();
873 }
874 for (; I != IEnd; ++I) {
Douglas Gregor3545ff42009-09-21 16:56:56 +0000875 // A tag declaration does not hide a non-tag declaration.
John McCalle87beb22010-04-23 18:46:30 +0000876 if (I->first->hasTagIdentifierNamespace() &&
Richard Smith541b38b2013-09-20 01:15:31 +0000877 (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary |
878 Decl::IDNS_LocalExtern | Decl::IDNS_ObjCProtocol)))
Douglas Gregor3545ff42009-09-21 16:56:56 +0000879 continue;
880
881 // Protocols are in distinct namespaces from everything else.
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000882 if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol)
Douglas Gregor3545ff42009-09-21 16:56:56 +0000883 || (IDNS & Decl::IDNS_ObjCProtocol)) &&
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000884 I->first->getIdentifierNamespace() != IDNS)
Douglas Gregor3545ff42009-09-21 16:56:56 +0000885 continue;
886
887 // The newly-added result is hidden by an entry in the shadow map.
Douglas Gregore0717ab2010-01-14 00:41:07 +0000888 if (CheckHiddenResult(R, CurContext, I->first))
Douglas Gregor3545ff42009-09-21 16:56:56 +0000889 return;
Douglas Gregor3545ff42009-09-21 16:56:56 +0000890
891 break;
892 }
893 }
894
895 // Make sure that any given declaration only shows up in the result set once.
David Blaikie82e95a32014-11-19 07:49:47 +0000896 if (!AllDeclsFound.insert(CanonDecl).second)
Douglas Gregor3545ff42009-09-21 16:56:56 +0000897 return;
Douglas Gregorc2cb2e22010-08-27 15:29:55 +0000898
Douglas Gregore412a5a2009-09-23 22:26:46 +0000899 // If the filter is for nested-name-specifiers, then this result starts a
900 // nested-name-specifier.
Douglas Gregora2db7932010-05-26 22:00:08 +0000901 if (AsNestedNameSpecifier) {
Douglas Gregore412a5a2009-09-23 22:26:46 +0000902 R.StartsNestedNameSpecifier = true;
Douglas Gregora2db7932010-05-26 22:00:08 +0000903 R.Priority = CCP_NestedNameSpecifier;
Douglas Gregor50832e02010-09-20 22:39:41 +0000904 } else
905 AdjustResultPriorityForDecl(R);
Douglas Gregorc2cb2e22010-08-27 15:29:55 +0000906
Douglas Gregor5bf52692009-09-22 23:15:58 +0000907 // If this result is supposed to have an informative qualifier, add one.
Douglas Gregore412a5a2009-09-23 22:26:46 +0000908 if (R.QualifierIsInformative && !R.Qualifier &&
909 !R.StartsNestedNameSpecifier) {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000910 const DeclContext *Ctx = R.Declaration->getDeclContext();
911 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
Craig Topperc3ec1492014-05-26 06:22:03 +0000912 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr,
913 Namespace);
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000914 else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
Craig Topperc3ec1492014-05-26 06:22:03 +0000915 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr,
916 false, SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
Douglas Gregor5bf52692009-09-22 23:15:58 +0000917 else
918 R.QualifierIsInformative = false;
919 }
Douglas Gregore412a5a2009-09-23 22:26:46 +0000920
Douglas Gregor3545ff42009-09-21 16:56:56 +0000921 // Insert this result into the set of results and into the current shadow
922 // map.
Douglas Gregor05e7ca32009-12-06 20:23:50 +0000923 SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size());
Douglas Gregor3545ff42009-09-21 16:56:56 +0000924 Results.push_back(R);
Douglas Gregor0212fd72010-09-21 16:06:22 +0000925
926 if (!AsNestedNameSpecifier)
927 MaybeAddConstructorResults(R);
Douglas Gregor3545ff42009-09-21 16:56:56 +0000928}
929
Douglas Gregorc580c522010-01-14 01:09:38 +0000930void ResultBuilder::AddResult(Result R, DeclContext *CurContext,
Douglas Gregor09bbc652010-01-14 15:47:35 +0000931 NamedDecl *Hiding, bool InBaseClass = false) {
Douglas Gregor78a21012010-01-14 16:01:26 +0000932 if (R.Kind != Result::RK_Declaration) {
933 // For non-declaration results, just add the result.
934 Results.push_back(R);
935 return;
936 }
937
Douglas Gregorc580c522010-01-14 01:09:38 +0000938 // Look through using declarations.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000939 if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
Douglas Gregor0a0e2b32013-01-31 04:52:16 +0000940 AddResult(Result(Using->getTargetDecl(),
941 getBasePriority(Using->getTargetDecl()),
942 R.Qualifier),
943 CurContext, Hiding);
Douglas Gregorc580c522010-01-14 01:09:38 +0000944 return;
945 }
946
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000947 bool AsNestedNameSpecifier = false;
948 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
Douglas Gregorc580c522010-01-14 01:09:38 +0000949 return;
950
Douglas Gregor0212fd72010-09-21 16:06:22 +0000951 // C++ constructors are never found by name lookup.
952 if (isa<CXXConstructorDecl>(R.Declaration))
953 return;
954
Douglas Gregorc580c522010-01-14 01:09:38 +0000955 if (Hiding && CheckHiddenResult(R, CurContext, Hiding))
956 return;
Nick Lewyckyc3921482012-04-03 21:44:08 +0000957
Douglas Gregorc580c522010-01-14 01:09:38 +0000958 // Make sure that any given declaration only shows up in the result set once.
David Blaikie82e95a32014-11-19 07:49:47 +0000959 if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second)
Douglas Gregorc580c522010-01-14 01:09:38 +0000960 return;
961
962 // If the filter is for nested-name-specifiers, then this result starts a
963 // nested-name-specifier.
Douglas Gregora2db7932010-05-26 22:00:08 +0000964 if (AsNestedNameSpecifier) {
Douglas Gregorc580c522010-01-14 01:09:38 +0000965 R.StartsNestedNameSpecifier = true;
Douglas Gregora2db7932010-05-26 22:00:08 +0000966 R.Priority = CCP_NestedNameSpecifier;
967 }
Douglas Gregor09bbc652010-01-14 15:47:35 +0000968 else if (Filter == &ResultBuilder::IsMember && !R.Qualifier && InBaseClass &&
969 isa<CXXRecordDecl>(R.Declaration->getDeclContext()
Sebastian Redl50c68252010-08-31 00:36:30 +0000970 ->getRedeclContext()))
Douglas Gregor09bbc652010-01-14 15:47:35 +0000971 R.QualifierIsInformative = true;
972
Douglas Gregorc580c522010-01-14 01:09:38 +0000973 // If this result is supposed to have an informative qualifier, add one.
974 if (R.QualifierIsInformative && !R.Qualifier &&
975 !R.StartsNestedNameSpecifier) {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000976 const DeclContext *Ctx = R.Declaration->getDeclContext();
977 if (const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
Craig Topperc3ec1492014-05-26 06:22:03 +0000978 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr,
979 Namespace);
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000980 else if (const TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
Craig Topperc3ec1492014-05-26 06:22:03 +0000981 R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, nullptr, false,
Douglas Gregor6ae4c522010-01-14 03:21:49 +0000982 SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
Douglas Gregorc580c522010-01-14 01:09:38 +0000983 else
984 R.QualifierIsInformative = false;
985 }
986
Douglas Gregora2db7932010-05-26 22:00:08 +0000987 // Adjust the priority if this result comes from a base class.
988 if (InBaseClass)
989 R.Priority += CCD_InBaseClass;
990
Douglas Gregor50832e02010-09-20 22:39:41 +0000991 AdjustResultPriorityForDecl(R);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000992
Douglas Gregor9be0ed42010-08-26 16:36:48 +0000993 if (HasObjectTypeQualifiers)
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000994 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(R.Declaration))
Douglas Gregor9be0ed42010-08-26 16:36:48 +0000995 if (Method->isInstance()) {
996 Qualifiers MethodQuals
997 = Qualifiers::fromCVRMask(Method->getTypeQualifiers());
998 if (ObjectTypeQualifiers == MethodQuals)
999 R.Priority += CCD_ObjectQualifierMatch;
1000 else if (ObjectTypeQualifiers - MethodQuals) {
1001 // The method cannot be invoked, because doing so would drop
1002 // qualifiers.
1003 return;
1004 }
1005 }
1006
Douglas Gregorc580c522010-01-14 01:09:38 +00001007 // Insert this result into the set of results.
1008 Results.push_back(R);
Douglas Gregor0212fd72010-09-21 16:06:22 +00001009
1010 if (!AsNestedNameSpecifier)
1011 MaybeAddConstructorResults(R);
Douglas Gregorc580c522010-01-14 01:09:38 +00001012}
1013
Douglas Gregor78a21012010-01-14 16:01:26 +00001014void ResultBuilder::AddResult(Result R) {
1015 assert(R.Kind != Result::RK_Declaration &&
1016 "Declaration results need more context");
1017 Results.push_back(R);
1018}
1019
Douglas Gregor3545ff42009-09-21 16:56:56 +00001020/// \brief Enter into a new scope.
Benjamin Kramer3204b152015-05-29 19:42:19 +00001021void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001022
1023/// \brief Exit from the current scope.
1024void ResultBuilder::ExitScope() {
Douglas Gregor05e7ca32009-12-06 20:23:50 +00001025 for (ShadowMap::iterator E = ShadowMaps.back().begin(),
1026 EEnd = ShadowMaps.back().end();
1027 E != EEnd;
1028 ++E)
1029 E->second.Destroy();
1030
Douglas Gregor3545ff42009-09-21 16:56:56 +00001031 ShadowMaps.pop_back();
1032}
1033
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001034/// \brief Determines whether this given declaration will be found by
1035/// ordinary name lookup.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001036bool ResultBuilder::IsOrdinaryName(const NamedDecl *ND) const {
Douglas Gregor70febae2010-05-28 00:49:12 +00001037 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
1038
Richard Smith541b38b2013-09-20 01:15:31 +00001039 // If name lookup finds a local extern declaration, then we are in a
1040 // context where it behaves like an ordinary name.
1041 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001042 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor9858ed52010-06-15 20:26:51 +00001043 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001044 else if (SemaRef.getLangOpts().ObjC1) {
Douglas Gregor05fcf842010-11-02 20:36:02 +00001045 if (isa<ObjCIvarDecl>(ND))
1046 return true;
Douglas Gregor05fcf842010-11-02 20:36:02 +00001047 }
1048
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001049 return ND->getIdentifierNamespace() & IDNS;
1050}
1051
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001052/// \brief Determines whether this given declaration will be found by
Douglas Gregor70febae2010-05-28 00:49:12 +00001053/// ordinary name lookup but is not a type name.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001054bool ResultBuilder::IsOrdinaryNonTypeName(const NamedDecl *ND) const {
Douglas Gregor70febae2010-05-28 00:49:12 +00001055 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
1056 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
1057 return false;
1058
Richard Smith541b38b2013-09-20 01:15:31 +00001059 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001060 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregor9858ed52010-06-15 20:26:51 +00001061 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace | Decl::IDNS_Member;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001062 else if (SemaRef.getLangOpts().ObjC1) {
Douglas Gregor05fcf842010-11-02 20:36:02 +00001063 if (isa<ObjCIvarDecl>(ND))
1064 return true;
Douglas Gregor05fcf842010-11-02 20:36:02 +00001065 }
1066
Douglas Gregor70febae2010-05-28 00:49:12 +00001067 return ND->getIdentifierNamespace() & IDNS;
1068}
1069
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001070bool ResultBuilder::IsIntegralConstantValue(const NamedDecl *ND) const {
Douglas Gregor85b50632010-07-28 21:50:18 +00001071 if (!IsOrdinaryNonTypeName(ND))
1072 return 0;
1073
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001074 if (const ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl()))
Douglas Gregor85b50632010-07-28 21:50:18 +00001075 if (VD->getType()->isIntegralOrEnumerationType())
1076 return true;
1077
1078 return false;
1079}
1080
Douglas Gregor70febae2010-05-28 00:49:12 +00001081/// \brief Determines whether this given declaration will be found by
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001082/// ordinary name lookup.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001083bool ResultBuilder::IsOrdinaryNonValueName(const NamedDecl *ND) const {
Douglas Gregor70febae2010-05-28 00:49:12 +00001084 ND = cast<NamedDecl>(ND->getUnderlyingDecl());
1085
Richard Smith541b38b2013-09-20 01:15:31 +00001086 unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_LocalExtern;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001087 if (SemaRef.getLangOpts().CPlusPlus)
John McCalle87beb22010-04-23 18:46:30 +00001088 IDNS |= Decl::IDNS_Tag | Decl::IDNS_Namespace;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001089
1090 return (ND->getIdentifierNamespace() & IDNS) &&
Douglas Gregor70febae2010-05-28 00:49:12 +00001091 !isa<ValueDecl>(ND) && !isa<FunctionTemplateDecl>(ND) &&
1092 !isa<ObjCPropertyDecl>(ND);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001093}
1094
Douglas Gregor3545ff42009-09-21 16:56:56 +00001095/// \brief Determines whether the given declaration is suitable as the
1096/// start of a C++ nested-name-specifier, e.g., a class or namespace.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001097bool ResultBuilder::IsNestedNameSpecifier(const NamedDecl *ND) const {
Douglas Gregor3545ff42009-09-21 16:56:56 +00001098 // Allow us to find class templates, too.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001099 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
Douglas Gregor3545ff42009-09-21 16:56:56 +00001100 ND = ClassTemplate->getTemplatedDecl();
1101
1102 return SemaRef.isAcceptableNestedNameSpecifier(ND);
1103}
1104
1105/// \brief Determines whether the given declaration is an enumeration.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001106bool ResultBuilder::IsEnum(const NamedDecl *ND) const {
Douglas Gregor3545ff42009-09-21 16:56:56 +00001107 return isa<EnumDecl>(ND);
1108}
1109
1110/// \brief Determines whether the given declaration is a class or struct.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001111bool ResultBuilder::IsClassOrStruct(const NamedDecl *ND) const {
Douglas Gregor3545ff42009-09-21 16:56:56 +00001112 // Allow us to find class templates, too.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001113 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
Douglas Gregor3545ff42009-09-21 16:56:56 +00001114 ND = ClassTemplate->getTemplatedDecl();
Joao Matosdc86f942012-08-31 18:45:21 +00001115
1116 // For purposes of this check, interfaces match too.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001117 if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND))
Abramo Bagnara6150c882010-05-11 21:36:43 +00001118 return RD->getTagKind() == TTK_Class ||
Joao Matosdc86f942012-08-31 18:45:21 +00001119 RD->getTagKind() == TTK_Struct ||
1120 RD->getTagKind() == TTK_Interface;
Douglas Gregor3545ff42009-09-21 16:56:56 +00001121
1122 return false;
1123}
1124
1125/// \brief Determines whether the given declaration is a union.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001126bool ResultBuilder::IsUnion(const NamedDecl *ND) const {
Douglas Gregor3545ff42009-09-21 16:56:56 +00001127 // Allow us to find class templates, too.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001128 if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
Douglas Gregor3545ff42009-09-21 16:56:56 +00001129 ND = ClassTemplate->getTemplatedDecl();
1130
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001131 if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND))
Abramo Bagnara6150c882010-05-11 21:36:43 +00001132 return RD->getTagKind() == TTK_Union;
Douglas Gregor3545ff42009-09-21 16:56:56 +00001133
1134 return false;
1135}
1136
1137/// \brief Determines whether the given declaration is a namespace.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001138bool ResultBuilder::IsNamespace(const NamedDecl *ND) const {
Douglas Gregor3545ff42009-09-21 16:56:56 +00001139 return isa<NamespaceDecl>(ND);
1140}
1141
1142/// \brief Determines whether the given declaration is a namespace or
1143/// namespace alias.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001144bool ResultBuilder::IsNamespaceOrAlias(const NamedDecl *ND) const {
Douglas Gregor3545ff42009-09-21 16:56:56 +00001145 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
1146}
1147
Douglas Gregor99fe2ad2009-12-11 17:31:05 +00001148/// \brief Determines whether the given declaration is a type.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001149bool ResultBuilder::IsType(const NamedDecl *ND) const {
1150 if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND))
Douglas Gregor99fa2642010-08-24 01:06:58 +00001151 ND = Using->getTargetDecl();
1152
1153 return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001154}
1155
Douglas Gregor99fe2ad2009-12-11 17:31:05 +00001156/// \brief Determines which members of a class should be visible via
1157/// "." or "->". Only value declarations, nested name specifiers, and
1158/// using declarations thereof should show up.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001159bool ResultBuilder::IsMember(const NamedDecl *ND) const {
1160 if (const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(ND))
Douglas Gregor99fe2ad2009-12-11 17:31:05 +00001161 ND = Using->getTargetDecl();
1162
Douglas Gregor70788392009-12-11 18:14:22 +00001163 return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
1164 isa<ObjCPropertyDecl>(ND);
Douglas Gregore412a5a2009-09-23 22:26:46 +00001165}
1166
Douglas Gregora817a192010-05-27 23:06:34 +00001167static bool isObjCReceiverType(ASTContext &C, QualType T) {
1168 T = C.getCanonicalType(T);
1169 switch (T->getTypeClass()) {
1170 case Type::ObjCObject:
1171 case Type::ObjCInterface:
1172 case Type::ObjCObjectPointer:
1173 return true;
1174
1175 case Type::Builtin:
1176 switch (cast<BuiltinType>(T)->getKind()) {
1177 case BuiltinType::ObjCId:
1178 case BuiltinType::ObjCClass:
1179 case BuiltinType::ObjCSel:
1180 return true;
1181
1182 default:
1183 break;
1184 }
1185 return false;
1186
1187 default:
1188 break;
1189 }
1190
David Blaikiebbafb8a2012-03-11 07:00:24 +00001191 if (!C.getLangOpts().CPlusPlus)
Douglas Gregora817a192010-05-27 23:06:34 +00001192 return false;
1193
1194 // FIXME: We could perform more analysis here to determine whether a
1195 // particular class type has any conversions to Objective-C types. For now,
1196 // just accept all class types.
1197 return T->isDependentType() || T->isRecordType();
1198}
1199
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001200bool ResultBuilder::IsObjCMessageReceiver(const NamedDecl *ND) const {
Douglas Gregora817a192010-05-27 23:06:34 +00001201 QualType T = getDeclUsageType(SemaRef.Context, ND);
1202 if (T.isNull())
1203 return false;
1204
1205 T = SemaRef.Context.getBaseElementType(T);
1206 return isObjCReceiverType(SemaRef.Context, T);
1207}
1208
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001209bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(const NamedDecl *ND) const {
Douglas Gregord8c61782012-02-15 15:34:24 +00001210 if (IsObjCMessageReceiver(ND))
1211 return true;
1212
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001213 const VarDecl *Var = dyn_cast<VarDecl>(ND);
Douglas Gregord8c61782012-02-15 15:34:24 +00001214 if (!Var)
1215 return false;
1216
1217 return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>();
1218}
1219
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001220bool ResultBuilder::IsObjCCollection(const NamedDecl *ND) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001221 if ((SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) ||
1222 (!SemaRef.getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND)))
Douglas Gregor68762e72010-08-23 21:17:50 +00001223 return false;
1224
1225 QualType T = getDeclUsageType(SemaRef.Context, ND);
1226 if (T.isNull())
1227 return false;
1228
1229 T = SemaRef.Context.getBaseElementType(T);
1230 return T->isObjCObjectType() || T->isObjCObjectPointerType() ||
1231 T->isObjCIdType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001232 (SemaRef.getLangOpts().CPlusPlus && T->isRecordType());
Douglas Gregor68762e72010-08-23 21:17:50 +00001233}
Douglas Gregora817a192010-05-27 23:06:34 +00001234
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001235bool ResultBuilder::IsImpossibleToSatisfy(const NamedDecl *ND) const {
Douglas Gregor0ac41382010-09-23 23:01:17 +00001236 return false;
1237}
1238
James Dennettf1243872012-06-17 05:33:25 +00001239/// \brief Determines whether the given declaration is an Objective-C
Douglas Gregor2b8162b2010-01-14 16:08:12 +00001240/// instance variable.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00001241bool ResultBuilder::IsObjCIvar(const NamedDecl *ND) const {
Douglas Gregor2b8162b2010-01-14 16:08:12 +00001242 return isa<ObjCIvarDecl>(ND);
1243}
1244
Douglas Gregorc580c522010-01-14 01:09:38 +00001245namespace {
1246 /// \brief Visible declaration consumer that adds a code-completion result
1247 /// for each visible declaration.
1248 class CodeCompletionDeclConsumer : public VisibleDeclConsumer {
1249 ResultBuilder &Results;
1250 DeclContext *CurContext;
1251
1252 public:
1253 CodeCompletionDeclConsumer(ResultBuilder &Results, DeclContext *CurContext)
1254 : Results(Results), CurContext(CurContext) { }
Craig Toppere14c0f82014-03-12 04:55:44 +00001255
1256 void FoundDecl(NamedDecl *ND, NamedDecl *Hiding, DeclContext *Ctx,
1257 bool InBaseClass) override {
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001258 bool Accessible = true;
Douglas Gregor03ba1882011-11-03 16:51:37 +00001259 if (Ctx)
1260 Accessible = Results.getSema().IsSimplyAccessible(ND, Ctx);
Craig Topperc3ec1492014-05-26 06:22:03 +00001261
1262 ResultBuilder::Result Result(ND, Results.getBasePriority(ND), nullptr,
1263 false, Accessible);
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001264 Results.AddResult(Result, CurContext, Hiding, InBaseClass);
Douglas Gregorc580c522010-01-14 01:09:38 +00001265 }
1266 };
1267}
1268
Douglas Gregor3545ff42009-09-21 16:56:56 +00001269/// \brief Add type specifiers for the current language as keyword results.
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001270static void AddTypeSpecifierResults(const LangOptions &LangOpts,
Douglas Gregor3545ff42009-09-21 16:56:56 +00001271 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00001272 typedef CodeCompletionResult Result;
Douglas Gregora2db7932010-05-26 22:00:08 +00001273 Results.AddResult(Result("short", CCP_Type));
1274 Results.AddResult(Result("long", CCP_Type));
1275 Results.AddResult(Result("signed", CCP_Type));
1276 Results.AddResult(Result("unsigned", CCP_Type));
1277 Results.AddResult(Result("void", CCP_Type));
1278 Results.AddResult(Result("char", CCP_Type));
1279 Results.AddResult(Result("int", CCP_Type));
1280 Results.AddResult(Result("float", CCP_Type));
1281 Results.AddResult(Result("double", CCP_Type));
1282 Results.AddResult(Result("enum", CCP_Type));
1283 Results.AddResult(Result("struct", CCP_Type));
1284 Results.AddResult(Result("union", CCP_Type));
1285 Results.AddResult(Result("const", CCP_Type));
1286 Results.AddResult(Result("volatile", CCP_Type));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001287
Douglas Gregor3545ff42009-09-21 16:56:56 +00001288 if (LangOpts.C99) {
1289 // C99-specific
Douglas Gregora2db7932010-05-26 22:00:08 +00001290 Results.AddResult(Result("_Complex", CCP_Type));
1291 Results.AddResult(Result("_Imaginary", CCP_Type));
1292 Results.AddResult(Result("_Bool", CCP_Type));
1293 Results.AddResult(Result("restrict", CCP_Type));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001294 }
1295
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001296 CodeCompletionBuilder Builder(Results.getAllocator(),
1297 Results.getCodeCompletionTUInfo());
Douglas Gregor3545ff42009-09-21 16:56:56 +00001298 if (LangOpts.CPlusPlus) {
1299 // C++-specific
Douglas Gregor9dcf58a2010-09-20 21:11:48 +00001300 Results.AddResult(Result("bool", CCP_Type +
1301 (LangOpts.ObjC1? CCD_bool_in_ObjC : 0)));
Douglas Gregora2db7932010-05-26 22:00:08 +00001302 Results.AddResult(Result("class", CCP_Type));
1303 Results.AddResult(Result("wchar_t", CCP_Type));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001304
Douglas Gregorf4c33342010-05-28 00:22:41 +00001305 // typename qualified-id
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001306 Builder.AddTypedTextChunk("typename");
1307 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1308 Builder.AddPlaceholderChunk("qualifier");
1309 Builder.AddTextChunk("::");
1310 Builder.AddPlaceholderChunk("name");
1311 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf64acca2010-05-25 21:41:55 +00001312
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001313 if (LangOpts.CPlusPlus11) {
Douglas Gregora2db7932010-05-26 22:00:08 +00001314 Results.AddResult(Result("auto", CCP_Type));
1315 Results.AddResult(Result("char16_t", CCP_Type));
1316 Results.AddResult(Result("char32_t", CCP_Type));
Douglas Gregorf4c33342010-05-28 00:22:41 +00001317
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001318 Builder.AddTypedTextChunk("decltype");
1319 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1320 Builder.AddPlaceholderChunk("expression");
1321 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1322 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001323 }
1324 }
1325
1326 // GNU extensions
1327 if (LangOpts.GNUMode) {
1328 // FIXME: Enable when we actually support decimal floating point.
Douglas Gregor78a21012010-01-14 16:01:26 +00001329 // Results.AddResult(Result("_Decimal32"));
1330 // Results.AddResult(Result("_Decimal64"));
1331 // Results.AddResult(Result("_Decimal128"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001332
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001333 Builder.AddTypedTextChunk("typeof");
1334 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1335 Builder.AddPlaceholderChunk("expression");
1336 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00001337
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001338 Builder.AddTypedTextChunk("typeof");
1339 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1340 Builder.AddPlaceholderChunk("type");
1341 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1342 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001343 }
Douglas Gregor86b42682015-06-19 18:27:52 +00001344
1345 // Nullability
1346 Results.AddResult(Result("__nonnull", CCP_Type));
1347 Results.AddResult(Result("__null_unspecified", CCP_Type));
1348 Results.AddResult(Result("__nullable", CCP_Type));
Douglas Gregor3545ff42009-09-21 16:56:56 +00001349}
1350
John McCallfaf5fb42010-08-26 23:41:50 +00001351static void AddStorageSpecifiers(Sema::ParserCompletionContext CCC,
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001352 const LangOptions &LangOpts,
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001353 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00001354 typedef CodeCompletionResult Result;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001355 // Note: we don't suggest either "auto" or "register", because both
1356 // are pointless as storage specifiers. Elsewhere, we suggest "auto"
1357 // in C++0x as a type specifier.
Douglas Gregor78a21012010-01-14 16:01:26 +00001358 Results.AddResult(Result("extern"));
1359 Results.AddResult(Result("static"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001360}
1361
John McCallfaf5fb42010-08-26 23:41:50 +00001362static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC,
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001363 const LangOptions &LangOpts,
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001364 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00001365 typedef CodeCompletionResult Result;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001366 switch (CCC) {
John McCallfaf5fb42010-08-26 23:41:50 +00001367 case Sema::PCC_Class:
1368 case Sema::PCC_MemberTemplate:
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001369 if (LangOpts.CPlusPlus) {
Douglas Gregor78a21012010-01-14 16:01:26 +00001370 Results.AddResult(Result("explicit"));
1371 Results.AddResult(Result("friend"));
1372 Results.AddResult(Result("mutable"));
1373 Results.AddResult(Result("virtual"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001374 }
1375 // Fall through
1376
John McCallfaf5fb42010-08-26 23:41:50 +00001377 case Sema::PCC_ObjCInterface:
1378 case Sema::PCC_ObjCImplementation:
1379 case Sema::PCC_Namespace:
1380 case Sema::PCC_Template:
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001381 if (LangOpts.CPlusPlus || LangOpts.C99)
Douglas Gregor78a21012010-01-14 16:01:26 +00001382 Results.AddResult(Result("inline"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001383 break;
1384
John McCallfaf5fb42010-08-26 23:41:50 +00001385 case Sema::PCC_ObjCInstanceVariableList:
1386 case Sema::PCC_Expression:
1387 case Sema::PCC_Statement:
1388 case Sema::PCC_ForInit:
1389 case Sema::PCC_Condition:
1390 case Sema::PCC_RecoveryInFunction:
1391 case Sema::PCC_Type:
Douglas Gregor5e35d592010-09-14 23:59:36 +00001392 case Sema::PCC_ParenthesizedExpression:
Douglas Gregor80039242011-02-15 20:33:25 +00001393 case Sema::PCC_LocalDeclarationSpecifiers:
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001394 break;
1395 }
1396}
1397
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001398static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt);
1399static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt);
1400static void AddObjCVisibilityResults(const LangOptions &LangOpts,
Douglas Gregor48d46252010-01-13 21:54:15 +00001401 ResultBuilder &Results,
1402 bool NeedAt);
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001403static void AddObjCImplementationResults(const LangOptions &LangOpts,
Douglas Gregorf1934162010-01-13 21:24:21 +00001404 ResultBuilder &Results,
1405 bool NeedAt);
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001406static void AddObjCInterfaceResults(const LangOptions &LangOpts,
Douglas Gregorf1934162010-01-13 21:24:21 +00001407 ResultBuilder &Results,
1408 bool NeedAt);
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001409static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt);
Douglas Gregorf1934162010-01-13 21:24:21 +00001410
Douglas Gregorf4c33342010-05-28 00:22:41 +00001411static void AddTypedefResult(ResultBuilder &Results) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001412 CodeCompletionBuilder Builder(Results.getAllocator(),
1413 Results.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001414 Builder.AddTypedTextChunk("typedef");
1415 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1416 Builder.AddPlaceholderChunk("type");
1417 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1418 Builder.AddPlaceholderChunk("name");
1419 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00001420}
1421
John McCallfaf5fb42010-08-26 23:41:50 +00001422static bool WantTypesInContext(Sema::ParserCompletionContext CCC,
Douglas Gregor70febae2010-05-28 00:49:12 +00001423 const LangOptions &LangOpts) {
Douglas Gregor70febae2010-05-28 00:49:12 +00001424 switch (CCC) {
John McCallfaf5fb42010-08-26 23:41:50 +00001425 case Sema::PCC_Namespace:
1426 case Sema::PCC_Class:
1427 case Sema::PCC_ObjCInstanceVariableList:
1428 case Sema::PCC_Template:
1429 case Sema::PCC_MemberTemplate:
1430 case Sema::PCC_Statement:
1431 case Sema::PCC_RecoveryInFunction:
1432 case Sema::PCC_Type:
Douglas Gregor5e35d592010-09-14 23:59:36 +00001433 case Sema::PCC_ParenthesizedExpression:
Douglas Gregor80039242011-02-15 20:33:25 +00001434 case Sema::PCC_LocalDeclarationSpecifiers:
Douglas Gregor70febae2010-05-28 00:49:12 +00001435 return true;
1436
John McCallfaf5fb42010-08-26 23:41:50 +00001437 case Sema::PCC_Expression:
1438 case Sema::PCC_Condition:
Douglas Gregor5e35d592010-09-14 23:59:36 +00001439 return LangOpts.CPlusPlus;
1440
1441 case Sema::PCC_ObjCInterface:
1442 case Sema::PCC_ObjCImplementation:
Douglas Gregor70febae2010-05-28 00:49:12 +00001443 return false;
1444
John McCallfaf5fb42010-08-26 23:41:50 +00001445 case Sema::PCC_ForInit:
Douglas Gregor5e35d592010-09-14 23:59:36 +00001446 return LangOpts.CPlusPlus || LangOpts.ObjC1 || LangOpts.C99;
Douglas Gregor70febae2010-05-28 00:49:12 +00001447 }
David Blaikie8a40f702012-01-17 06:56:22 +00001448
1449 llvm_unreachable("Invalid ParserCompletionContext!");
Douglas Gregor70febae2010-05-28 00:49:12 +00001450}
1451
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00001452static PrintingPolicy getCompletionPrintingPolicy(const ASTContext &Context,
1453 const Preprocessor &PP) {
1454 PrintingPolicy Policy = Sema::getPrintingPolicy(Context, PP);
Douglas Gregore5c79d52011-10-18 21:20:17 +00001455 Policy.AnonymousTagLocations = false;
1456 Policy.SuppressStrongLifetime = true;
Douglas Gregor2e10cf92011-11-03 00:16:13 +00001457 Policy.SuppressUnwrittenScope = true;
Douglas Gregore5c79d52011-10-18 21:20:17 +00001458 return Policy;
1459}
1460
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00001461/// \brief Retrieve a printing policy suitable for code completion.
1462static PrintingPolicy getCompletionPrintingPolicy(Sema &S) {
1463 return getCompletionPrintingPolicy(S.Context, S.PP);
1464}
1465
Douglas Gregore5c79d52011-10-18 21:20:17 +00001466/// \brief Retrieve the string representation of the given type as a string
1467/// that has the appropriate lifetime for code completion.
1468///
1469/// This routine provides a fast path where we provide constant strings for
1470/// common type names.
1471static const char *GetCompletionTypeString(QualType T,
1472 ASTContext &Context,
1473 const PrintingPolicy &Policy,
1474 CodeCompletionAllocator &Allocator) {
1475 if (!T.getLocalQualifiers()) {
1476 // Built-in type names are constant strings.
1477 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T))
Argyrios Kyrtzidisbbff3da2012-05-05 04:20:28 +00001478 return BT->getNameAsCString(Policy);
Douglas Gregore5c79d52011-10-18 21:20:17 +00001479
1480 // Anonymous tag types are constant strings.
1481 if (const TagType *TagT = dyn_cast<TagType>(T))
1482 if (TagDecl *Tag = TagT->getDecl())
John McCall5ea95772013-03-09 00:54:27 +00001483 if (!Tag->hasNameForLinkage()) {
Douglas Gregore5c79d52011-10-18 21:20:17 +00001484 switch (Tag->getTagKind()) {
1485 case TTK_Struct: return "struct <anonymous>";
Joao Matosdc86f942012-08-31 18:45:21 +00001486 case TTK_Interface: return "__interface <anonymous>";
1487 case TTK_Class: return "class <anonymous>";
Douglas Gregore5c79d52011-10-18 21:20:17 +00001488 case TTK_Union: return "union <anonymous>";
1489 case TTK_Enum: return "enum <anonymous>";
1490 }
1491 }
1492 }
1493
1494 // Slow path: format the type as a string.
1495 std::string Result;
1496 T.getAsStringInternal(Result, Policy);
1497 return Allocator.CopyString(Result);
1498}
1499
Douglas Gregord8c61782012-02-15 15:34:24 +00001500/// \brief Add a completion for "this", if we're in a member function.
1501static void addThisCompletion(Sema &S, ResultBuilder &Results) {
1502 QualType ThisTy = S.getCurrentThisType();
1503 if (ThisTy.isNull())
1504 return;
1505
1506 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001507 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregord8c61782012-02-15 15:34:24 +00001508 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
1509 Builder.AddResultTypeChunk(GetCompletionTypeString(ThisTy,
1510 S.Context,
1511 Policy,
1512 Allocator));
1513 Builder.AddTypedTextChunk("this");
Joao Matosdc86f942012-08-31 18:45:21 +00001514 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
Douglas Gregord8c61782012-02-15 15:34:24 +00001515}
1516
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001517/// \brief Add language constructs that show up for "ordinary" names.
John McCallfaf5fb42010-08-26 23:41:50 +00001518static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC,
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001519 Scope *S,
1520 Sema &SemaRef,
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001521 ResultBuilder &Results) {
Douglas Gregore5c79d52011-10-18 21:20:17 +00001522 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001523 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregore5c79d52011-10-18 21:20:17 +00001524 PrintingPolicy Policy = getCompletionPrintingPolicy(SemaRef);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001525
John McCall276321a2010-08-25 06:19:51 +00001526 typedef CodeCompletionResult Result;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001527 switch (CCC) {
John McCallfaf5fb42010-08-26 23:41:50 +00001528 case Sema::PCC_Namespace:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001529 if (SemaRef.getLangOpts().CPlusPlus) {
Douglas Gregorf4c33342010-05-28 00:22:41 +00001530 if (Results.includeCodePatterns()) {
1531 // namespace <identifier> { declarations }
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001532 Builder.AddTypedTextChunk("namespace");
1533 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1534 Builder.AddPlaceholderChunk("identifier");
1535 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1536 Builder.AddPlaceholderChunk("declarations");
1537 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1538 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1539 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00001540 }
1541
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001542 // namespace identifier = identifier ;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001543 Builder.AddTypedTextChunk("namespace");
1544 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1545 Builder.AddPlaceholderChunk("name");
1546 Builder.AddChunk(CodeCompletionString::CK_Equal);
1547 Builder.AddPlaceholderChunk("namespace");
1548 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001549
1550 // Using directives
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001551 Builder.AddTypedTextChunk("using");
1552 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1553 Builder.AddTextChunk("namespace");
1554 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1555 Builder.AddPlaceholderChunk("identifier");
1556 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001557
1558 // asm(string-literal)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001559 Builder.AddTypedTextChunk("asm");
1560 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1561 Builder.AddPlaceholderChunk("string-literal");
1562 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1563 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001564
Douglas Gregorf4c33342010-05-28 00:22:41 +00001565 if (Results.includeCodePatterns()) {
1566 // Explicit template instantiation
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001567 Builder.AddTypedTextChunk("template");
1568 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1569 Builder.AddPlaceholderChunk("declaration");
1570 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00001571 }
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001572 }
Douglas Gregorf1934162010-01-13 21:24:21 +00001573
David Blaikiebbafb8a2012-03-11 07:00:24 +00001574 if (SemaRef.getLangOpts().ObjC1)
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001575 AddObjCTopLevelResults(Results, true);
Douglas Gregorf1934162010-01-13 21:24:21 +00001576
Douglas Gregorf4c33342010-05-28 00:22:41 +00001577 AddTypedefResult(Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001578 // Fall through
1579
John McCallfaf5fb42010-08-26 23:41:50 +00001580 case Sema::PCC_Class:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001581 if (SemaRef.getLangOpts().CPlusPlus) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001582 // Using declaration
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001583 Builder.AddTypedTextChunk("using");
1584 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1585 Builder.AddPlaceholderChunk("qualifier");
1586 Builder.AddTextChunk("::");
1587 Builder.AddPlaceholderChunk("name");
1588 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001589
Douglas Gregorf4c33342010-05-28 00:22:41 +00001590 // using typename qualifier::name (only in a dependent context)
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001591 if (SemaRef.CurContext->isDependentContext()) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001592 Builder.AddTypedTextChunk("using");
1593 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1594 Builder.AddTextChunk("typename");
1595 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1596 Builder.AddPlaceholderChunk("qualifier");
1597 Builder.AddTextChunk("::");
1598 Builder.AddPlaceholderChunk("name");
1599 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001600 }
1601
John McCallfaf5fb42010-08-26 23:41:50 +00001602 if (CCC == Sema::PCC_Class) {
Douglas Gregorf4c33342010-05-28 00:22:41 +00001603 AddTypedefResult(Results);
1604
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001605 // public:
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001606 Builder.AddTypedTextChunk("public");
Douglas Gregor9489cdf2012-04-10 17:56:28 +00001607 if (Results.includeCodePatterns())
1608 Builder.AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001609 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001610
1611 // protected:
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001612 Builder.AddTypedTextChunk("protected");
Douglas Gregor9489cdf2012-04-10 17:56:28 +00001613 if (Results.includeCodePatterns())
1614 Builder.AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001615 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001616
1617 // private:
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001618 Builder.AddTypedTextChunk("private");
Douglas Gregor9489cdf2012-04-10 17:56:28 +00001619 if (Results.includeCodePatterns())
1620 Builder.AddChunk(CodeCompletionString::CK_Colon);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001621 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001622 }
1623 }
1624 // Fall through
1625
John McCallfaf5fb42010-08-26 23:41:50 +00001626 case Sema::PCC_Template:
1627 case Sema::PCC_MemberTemplate:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001628 if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns()) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001629 // template < parameters >
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001630 Builder.AddTypedTextChunk("template");
1631 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1632 Builder.AddPlaceholderChunk("parameters");
1633 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1634 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001635 }
1636
David Blaikiebbafb8a2012-03-11 07:00:24 +00001637 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1638 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001639 break;
1640
John McCallfaf5fb42010-08-26 23:41:50 +00001641 case Sema::PCC_ObjCInterface:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001642 AddObjCInterfaceResults(SemaRef.getLangOpts(), Results, true);
1643 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1644 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregorf1934162010-01-13 21:24:21 +00001645 break;
1646
John McCallfaf5fb42010-08-26 23:41:50 +00001647 case Sema::PCC_ObjCImplementation:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001648 AddObjCImplementationResults(SemaRef.getLangOpts(), Results, true);
1649 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
1650 AddFunctionSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregorf1934162010-01-13 21:24:21 +00001651 break;
1652
John McCallfaf5fb42010-08-26 23:41:50 +00001653 case Sema::PCC_ObjCInstanceVariableList:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001654 AddObjCVisibilityResults(SemaRef.getLangOpts(), Results, true);
Douglas Gregor48d46252010-01-13 21:54:15 +00001655 break;
1656
John McCallfaf5fb42010-08-26 23:41:50 +00001657 case Sema::PCC_RecoveryInFunction:
1658 case Sema::PCC_Statement: {
Douglas Gregorf4c33342010-05-28 00:22:41 +00001659 AddTypedefResult(Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001660
David Blaikiebbafb8a2012-03-11 07:00:24 +00001661 if (SemaRef.getLangOpts().CPlusPlus && Results.includeCodePatterns() &&
1662 SemaRef.getLangOpts().CXXExceptions) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001663 Builder.AddTypedTextChunk("try");
1664 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1665 Builder.AddPlaceholderChunk("statements");
1666 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1667 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1668 Builder.AddTextChunk("catch");
1669 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1670 Builder.AddPlaceholderChunk("declaration");
1671 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1672 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1673 Builder.AddPlaceholderChunk("statements");
1674 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1675 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1676 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001677 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001678 if (SemaRef.getLangOpts().ObjC1)
Douglas Gregorf98e6a22010-01-13 23:51:12 +00001679 AddObjCStatementResults(Results, true);
Douglas Gregorf1934162010-01-13 21:24:21 +00001680
Douglas Gregorf64acca2010-05-25 21:41:55 +00001681 if (Results.includeCodePatterns()) {
1682 // if (condition) { statements }
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001683 Builder.AddTypedTextChunk("if");
1684 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001685 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001686 Builder.AddPlaceholderChunk("condition");
Douglas Gregorf64acca2010-05-25 21:41:55 +00001687 else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001688 Builder.AddPlaceholderChunk("expression");
1689 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1690 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1691 Builder.AddPlaceholderChunk("statements");
1692 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1693 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1694 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001695
Douglas Gregorf64acca2010-05-25 21:41:55 +00001696 // switch (condition) { }
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001697 Builder.AddTypedTextChunk("switch");
1698 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001699 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001700 Builder.AddPlaceholderChunk("condition");
Douglas Gregorf64acca2010-05-25 21:41:55 +00001701 else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001702 Builder.AddPlaceholderChunk("expression");
1703 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1704 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1705 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1706 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1707 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf64acca2010-05-25 21:41:55 +00001708 }
1709
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001710 // Switch-specific statements.
John McCallaab3e412010-08-25 08:40:02 +00001711 if (!SemaRef.getCurFunction()->SwitchStack.empty()) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001712 // case expression:
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001713 Builder.AddTypedTextChunk("case");
1714 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1715 Builder.AddPlaceholderChunk("expression");
1716 Builder.AddChunk(CodeCompletionString::CK_Colon);
1717 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001718
1719 // default:
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001720 Builder.AddTypedTextChunk("default");
1721 Builder.AddChunk(CodeCompletionString::CK_Colon);
1722 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001723 }
1724
Douglas Gregorf64acca2010-05-25 21:41:55 +00001725 if (Results.includeCodePatterns()) {
1726 /// while (condition) { statements }
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001727 Builder.AddTypedTextChunk("while");
1728 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001729 if (SemaRef.getLangOpts().CPlusPlus)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001730 Builder.AddPlaceholderChunk("condition");
Douglas Gregorf64acca2010-05-25 21:41:55 +00001731 else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001732 Builder.AddPlaceholderChunk("expression");
1733 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1734 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1735 Builder.AddPlaceholderChunk("statements");
1736 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1737 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1738 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf64acca2010-05-25 21:41:55 +00001739
1740 // do { statements } while ( expression );
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001741 Builder.AddTypedTextChunk("do");
1742 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1743 Builder.AddPlaceholderChunk("statements");
1744 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1745 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1746 Builder.AddTextChunk("while");
1747 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1748 Builder.AddPlaceholderChunk("expression");
1749 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1750 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001751
Douglas Gregorf64acca2010-05-25 21:41:55 +00001752 // for ( for-init-statement ; condition ; expression ) { statements }
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001753 Builder.AddTypedTextChunk("for");
1754 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001755 if (SemaRef.getLangOpts().CPlusPlus || SemaRef.getLangOpts().C99)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001756 Builder.AddPlaceholderChunk("init-statement");
Douglas Gregorf64acca2010-05-25 21:41:55 +00001757 else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001758 Builder.AddPlaceholderChunk("init-expression");
1759 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
1760 Builder.AddPlaceholderChunk("condition");
1761 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
1762 Builder.AddPlaceholderChunk("inc-expression");
1763 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1764 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
1765 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1766 Builder.AddPlaceholderChunk("statements");
1767 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
1768 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
1769 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf64acca2010-05-25 21:41:55 +00001770 }
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001771
1772 if (S->getContinueParent()) {
1773 // continue ;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001774 Builder.AddTypedTextChunk("continue");
1775 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001776 }
1777
1778 if (S->getBreakParent()) {
1779 // break ;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001780 Builder.AddTypedTextChunk("break");
1781 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001782 }
1783
1784 // "return expression ;" or "return ;", depending on whether we
1785 // know the function is void or not.
1786 bool isVoid = false;
1787 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext))
Alp Toker314cc812014-01-25 16:55:45 +00001788 isVoid = Function->getReturnType()->isVoidType();
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001789 else if (ObjCMethodDecl *Method
1790 = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext))
Alp Toker314cc812014-01-25 16:55:45 +00001791 isVoid = Method->getReturnType()->isVoidType();
Douglas Gregor9a28e842010-03-01 23:15:13 +00001792 else if (SemaRef.getCurBlock() &&
1793 !SemaRef.getCurBlock()->ReturnType.isNull())
1794 isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType();
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001795 Builder.AddTypedTextChunk("return");
Douglas Gregor44272ca2010-02-18 04:06:48 +00001796 if (!isVoid) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001797 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1798 Builder.AddPlaceholderChunk("expression");
Douglas Gregor44272ca2010-02-18 04:06:48 +00001799 }
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001800 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001801
Douglas Gregorf4c33342010-05-28 00:22:41 +00001802 // goto identifier ;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001803 Builder.AddTypedTextChunk("goto");
1804 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1805 Builder.AddPlaceholderChunk("label");
1806 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001807
Douglas Gregorf4c33342010-05-28 00:22:41 +00001808 // Using directives
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001809 Builder.AddTypedTextChunk("using");
1810 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1811 Builder.AddTextChunk("namespace");
1812 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1813 Builder.AddPlaceholderChunk("identifier");
1814 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001815 }
1816
1817 // Fall through (for statement expressions).
John McCallfaf5fb42010-08-26 23:41:50 +00001818 case Sema::PCC_ForInit:
1819 case Sema::PCC_Condition:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001820 AddStorageSpecifiers(CCC, SemaRef.getLangOpts(), Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001821 // Fall through: conditions and statements can have expressions.
1822
Douglas Gregor5e35d592010-09-14 23:59:36 +00001823 case Sema::PCC_ParenthesizedExpression:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001824 if (SemaRef.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00001825 CCC == Sema::PCC_ParenthesizedExpression) {
1826 // (__bridge <type>)<expression>
1827 Builder.AddTypedTextChunk("__bridge");
1828 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1829 Builder.AddPlaceholderChunk("type");
1830 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1831 Builder.AddPlaceholderChunk("expression");
1832 Results.AddResult(Result(Builder.TakeString()));
1833
1834 // (__bridge_transfer <Objective-C type>)<expression>
1835 Builder.AddTypedTextChunk("__bridge_transfer");
1836 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1837 Builder.AddPlaceholderChunk("Objective-C type");
1838 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1839 Builder.AddPlaceholderChunk("expression");
1840 Results.AddResult(Result(Builder.TakeString()));
1841
1842 // (__bridge_retained <CF type>)<expression>
1843 Builder.AddTypedTextChunk("__bridge_retained");
1844 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1845 Builder.AddPlaceholderChunk("CF type");
1846 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1847 Builder.AddPlaceholderChunk("expression");
1848 Results.AddResult(Result(Builder.TakeString()));
1849 }
1850 // Fall through
1851
John McCallfaf5fb42010-08-26 23:41:50 +00001852 case Sema::PCC_Expression: {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001853 if (SemaRef.getLangOpts().CPlusPlus) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001854 // 'this', if we're in a non-static member function.
Douglas Gregord8c61782012-02-15 15:34:24 +00001855 addThisCompletion(SemaRef, Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001856
Douglas Gregore5c79d52011-10-18 21:20:17 +00001857 // true
1858 Builder.AddResultTypeChunk("bool");
1859 Builder.AddTypedTextChunk("true");
1860 Results.AddResult(Result(Builder.TakeString()));
1861
1862 // false
1863 Builder.AddResultTypeChunk("bool");
1864 Builder.AddTypedTextChunk("false");
1865 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001866
David Blaikiebbafb8a2012-03-11 07:00:24 +00001867 if (SemaRef.getLangOpts().RTTI) {
Douglas Gregorc05f6572011-04-12 02:47:21 +00001868 // dynamic_cast < type-id > ( expression )
1869 Builder.AddTypedTextChunk("dynamic_cast");
1870 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1871 Builder.AddPlaceholderChunk("type");
1872 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1873 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1874 Builder.AddPlaceholderChunk("expression");
1875 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1876 Results.AddResult(Result(Builder.TakeString()));
1877 }
Douglas Gregorf4c33342010-05-28 00:22:41 +00001878
1879 // static_cast < type-id > ( expression )
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001880 Builder.AddTypedTextChunk("static_cast");
1881 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1882 Builder.AddPlaceholderChunk("type");
1883 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1884 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1885 Builder.AddPlaceholderChunk("expression");
1886 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1887 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001888
Douglas Gregorf4c33342010-05-28 00:22:41 +00001889 // reinterpret_cast < type-id > ( expression )
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001890 Builder.AddTypedTextChunk("reinterpret_cast");
1891 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1892 Builder.AddPlaceholderChunk("type");
1893 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1894 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1895 Builder.AddPlaceholderChunk("expression");
1896 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1897 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001898
Douglas Gregorf4c33342010-05-28 00:22:41 +00001899 // const_cast < type-id > ( expression )
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001900 Builder.AddTypedTextChunk("const_cast");
1901 Builder.AddChunk(CodeCompletionString::CK_LeftAngle);
1902 Builder.AddPlaceholderChunk("type");
1903 Builder.AddChunk(CodeCompletionString::CK_RightAngle);
1904 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1905 Builder.AddPlaceholderChunk("expression");
1906 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1907 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001908
David Blaikiebbafb8a2012-03-11 07:00:24 +00001909 if (SemaRef.getLangOpts().RTTI) {
Douglas Gregorc05f6572011-04-12 02:47:21 +00001910 // typeid ( expression-or-type )
Douglas Gregore5c79d52011-10-18 21:20:17 +00001911 Builder.AddResultTypeChunk("std::type_info");
Douglas Gregorc05f6572011-04-12 02:47:21 +00001912 Builder.AddTypedTextChunk("typeid");
1913 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1914 Builder.AddPlaceholderChunk("expression-or-type");
1915 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1916 Results.AddResult(Result(Builder.TakeString()));
1917 }
1918
Douglas Gregorf4c33342010-05-28 00:22:41 +00001919 // new T ( ... )
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001920 Builder.AddTypedTextChunk("new");
1921 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1922 Builder.AddPlaceholderChunk("type");
1923 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1924 Builder.AddPlaceholderChunk("expressions");
1925 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1926 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001927
Douglas Gregorf4c33342010-05-28 00:22:41 +00001928 // new T [ ] ( ... )
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001929 Builder.AddTypedTextChunk("new");
1930 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1931 Builder.AddPlaceholderChunk("type");
1932 Builder.AddChunk(CodeCompletionString::CK_LeftBracket);
1933 Builder.AddPlaceholderChunk("size");
1934 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
1935 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1936 Builder.AddPlaceholderChunk("expressions");
1937 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1938 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001939
Douglas Gregorf4c33342010-05-28 00:22:41 +00001940 // delete expression
Douglas Gregore5c79d52011-10-18 21:20:17 +00001941 Builder.AddResultTypeChunk("void");
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001942 Builder.AddTypedTextChunk("delete");
1943 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1944 Builder.AddPlaceholderChunk("expression");
1945 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001946
Douglas Gregorf4c33342010-05-28 00:22:41 +00001947 // delete [] expression
Douglas Gregore5c79d52011-10-18 21:20:17 +00001948 Builder.AddResultTypeChunk("void");
Douglas Gregorb278aaf2011-02-01 19:23:04 +00001949 Builder.AddTypedTextChunk("delete");
1950 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1951 Builder.AddChunk(CodeCompletionString::CK_LeftBracket);
1952 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
1953 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1954 Builder.AddPlaceholderChunk("expression");
1955 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001956
David Blaikiebbafb8a2012-03-11 07:00:24 +00001957 if (SemaRef.getLangOpts().CXXExceptions) {
Douglas Gregorc05f6572011-04-12 02:47:21 +00001958 // throw expression
Douglas Gregore5c79d52011-10-18 21:20:17 +00001959 Builder.AddResultTypeChunk("void");
Douglas Gregorc05f6572011-04-12 02:47:21 +00001960 Builder.AddTypedTextChunk("throw");
1961 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
1962 Builder.AddPlaceholderChunk("expression");
1963 Results.AddResult(Result(Builder.TakeString()));
1964 }
Douglas Gregor4205fef2011-10-18 16:29:03 +00001965
Douglas Gregora2db7932010-05-26 22:00:08 +00001966 // FIXME: Rethrow?
Douglas Gregor4205fef2011-10-18 16:29:03 +00001967
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001968 if (SemaRef.getLangOpts().CPlusPlus11) {
Douglas Gregor4205fef2011-10-18 16:29:03 +00001969 // nullptr
Douglas Gregore5c79d52011-10-18 21:20:17 +00001970 Builder.AddResultTypeChunk("std::nullptr_t");
Douglas Gregor4205fef2011-10-18 16:29:03 +00001971 Builder.AddTypedTextChunk("nullptr");
1972 Results.AddResult(Result(Builder.TakeString()));
1973
1974 // alignof
Douglas Gregore5c79d52011-10-18 21:20:17 +00001975 Builder.AddResultTypeChunk("size_t");
Douglas Gregor4205fef2011-10-18 16:29:03 +00001976 Builder.AddTypedTextChunk("alignof");
1977 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1978 Builder.AddPlaceholderChunk("type");
1979 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1980 Results.AddResult(Result(Builder.TakeString()));
1981
1982 // noexcept
Douglas Gregore5c79d52011-10-18 21:20:17 +00001983 Builder.AddResultTypeChunk("bool");
Douglas Gregor4205fef2011-10-18 16:29:03 +00001984 Builder.AddTypedTextChunk("noexcept");
1985 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1986 Builder.AddPlaceholderChunk("expression");
1987 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1988 Results.AddResult(Result(Builder.TakeString()));
1989
1990 // sizeof... expression
Douglas Gregore5c79d52011-10-18 21:20:17 +00001991 Builder.AddResultTypeChunk("size_t");
Douglas Gregor4205fef2011-10-18 16:29:03 +00001992 Builder.AddTypedTextChunk("sizeof...");
1993 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
1994 Builder.AddPlaceholderChunk("parameter-pack");
1995 Builder.AddChunk(CodeCompletionString::CK_RightParen);
1996 Results.AddResult(Result(Builder.TakeString()));
1997 }
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001998 }
1999
David Blaikiebbafb8a2012-03-11 07:00:24 +00002000 if (SemaRef.getLangOpts().ObjC1) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +00002001 // Add "super", if we're in an Objective-C class with a superclass.
Ted Kremenek305a0a72010-05-31 21:43:10 +00002002 if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) {
2003 // The interface can be NULL.
2004 if (ObjCInterfaceDecl *ID = Method->getClassInterface())
Douglas Gregore5c79d52011-10-18 21:20:17 +00002005 if (ID->getSuperClass()) {
2006 std::string SuperType;
2007 SuperType = ID->getSuperClass()->getNameAsString();
2008 if (Method->isInstanceMethod())
2009 SuperType += " *";
2010
2011 Builder.AddResultTypeChunk(Allocator.CopyString(SuperType));
2012 Builder.AddTypedTextChunk("super");
2013 Results.AddResult(Result(Builder.TakeString()));
2014 }
Ted Kremenek305a0a72010-05-31 21:43:10 +00002015 }
2016
Douglas Gregorf98e6a22010-01-13 23:51:12 +00002017 AddObjCExpressionResults(Results, true);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00002018 }
2019
Jordan Rose58d54722012-06-30 21:33:57 +00002020 if (SemaRef.getLangOpts().C11) {
2021 // _Alignof
2022 Builder.AddResultTypeChunk("size_t");
Richard Smith20e883e2015-04-29 23:20:19 +00002023 if (SemaRef.PP.isMacroDefined("alignof"))
Jordan Rose58d54722012-06-30 21:33:57 +00002024 Builder.AddTypedTextChunk("alignof");
2025 else
2026 Builder.AddTypedTextChunk("_Alignof");
2027 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
2028 Builder.AddPlaceholderChunk("type");
2029 Builder.AddChunk(CodeCompletionString::CK_RightParen);
2030 Results.AddResult(Result(Builder.TakeString()));
2031 }
2032
Douglas Gregorf4c33342010-05-28 00:22:41 +00002033 // sizeof expression
Douglas Gregore5c79d52011-10-18 21:20:17 +00002034 Builder.AddResultTypeChunk("size_t");
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002035 Builder.AddTypedTextChunk("sizeof");
2036 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
2037 Builder.AddPlaceholderChunk("expression-or-type");
2038 Builder.AddChunk(CodeCompletionString::CK_RightParen);
2039 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00002040 break;
2041 }
Douglas Gregor99fa2642010-08-24 01:06:58 +00002042
John McCallfaf5fb42010-08-26 23:41:50 +00002043 case Sema::PCC_Type:
Douglas Gregor80039242011-02-15 20:33:25 +00002044 case Sema::PCC_LocalDeclarationSpecifiers:
Douglas Gregor99fa2642010-08-24 01:06:58 +00002045 break;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00002046 }
2047
David Blaikiebbafb8a2012-03-11 07:00:24 +00002048 if (WantTypesInContext(CCC, SemaRef.getLangOpts()))
2049 AddTypeSpecifierResults(SemaRef.getLangOpts(), Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00002050
David Blaikiebbafb8a2012-03-11 07:00:24 +00002051 if (SemaRef.getLangOpts().CPlusPlus && CCC != Sema::PCC_Type)
Douglas Gregor78a21012010-01-14 16:01:26 +00002052 Results.AddResult(Result("operator"));
Douglas Gregor504a6ae2010-01-10 23:08:15 +00002053}
2054
Douglas Gregorb3fa9192009-12-18 18:53:37 +00002055/// \brief If the given declaration has an associated type, add it as a result
2056/// type chunk.
2057static void AddResultTypeChunk(ASTContext &Context,
Douglas Gregor75acd922011-09-27 23:30:47 +00002058 const PrintingPolicy &Policy,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002059 const NamedDecl *ND,
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002060 CodeCompletionBuilder &Result) {
Douglas Gregorb3fa9192009-12-18 18:53:37 +00002061 if (!ND)
2062 return;
Douglas Gregor0212fd72010-09-21 16:06:22 +00002063
2064 // Skip constructors and conversion functions, which have their return types
2065 // built into their names.
2066 if (isa<CXXConstructorDecl>(ND) || isa<CXXConversionDecl>(ND))
2067 return;
2068
Douglas Gregorb3fa9192009-12-18 18:53:37 +00002069 // Determine the type of the declaration (if it has a type).
Alp Tokera2794f92014-01-22 07:29:52 +00002070 QualType T;
2071 if (const FunctionDecl *Function = ND->getAsFunction())
Alp Toker314cc812014-01-25 16:55:45 +00002072 T = Function->getReturnType();
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002073 else if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND))
Alp Toker314cc812014-01-25 16:55:45 +00002074 T = Method->getReturnType();
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002075 else if (const EnumConstantDecl *Enumerator = dyn_cast<EnumConstantDecl>(ND))
Douglas Gregorb3fa9192009-12-18 18:53:37 +00002076 T = Context.getTypeDeclType(cast<TypeDecl>(Enumerator->getDeclContext()));
2077 else if (isa<UnresolvedUsingValueDecl>(ND)) {
2078 /* Do nothing: ignore unresolved using declarations*/
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002079 } else if (const ValueDecl *Value = dyn_cast<ValueDecl>(ND)) {
Douglas Gregorb3fa9192009-12-18 18:53:37 +00002080 T = Value->getType();
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002081 } else if (const ObjCPropertyDecl *Property = dyn_cast<ObjCPropertyDecl>(ND))
Douglas Gregorb3fa9192009-12-18 18:53:37 +00002082 T = Property->getType();
2083
2084 if (T.isNull() || Context.hasSameType(T, Context.DependentTy))
2085 return;
2086
Douglas Gregor75acd922011-09-27 23:30:47 +00002087 Result.AddResultTypeChunk(GetCompletionTypeString(T, Context, Policy,
Douglas Gregor304f9b02011-02-01 21:15:40 +00002088 Result.getAllocator()));
Douglas Gregorb3fa9192009-12-18 18:53:37 +00002089}
2090
Richard Smith20e883e2015-04-29 23:20:19 +00002091static void MaybeAddSentinel(Preprocessor &PP,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002092 const NamedDecl *FunctionOrMethod,
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002093 CodeCompletionBuilder &Result) {
Douglas Gregordbb71db2010-08-23 23:51:41 +00002094 if (SentinelAttr *Sentinel = FunctionOrMethod->getAttr<SentinelAttr>())
2095 if (Sentinel->getSentinel() == 0) {
Richard Smith20e883e2015-04-29 23:20:19 +00002096 if (PP.getLangOpts().ObjC1 && PP.isMacroDefined("nil"))
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002097 Result.AddTextChunk(", nil");
Richard Smith20e883e2015-04-29 23:20:19 +00002098 else if (PP.isMacroDefined("NULL"))
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002099 Result.AddTextChunk(", NULL");
Douglas Gregordbb71db2010-08-23 23:51:41 +00002100 else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002101 Result.AddTextChunk(", (void*)0");
Douglas Gregordbb71db2010-08-23 23:51:41 +00002102 }
2103}
2104
Douglas Gregor86b42682015-06-19 18:27:52 +00002105static std::string formatObjCParamQualifiers(unsigned ObjCQuals,
2106 QualType &Type) {
Douglas Gregor8f08d742011-07-30 07:55:26 +00002107 std::string Result;
2108 if (ObjCQuals & Decl::OBJC_TQ_In)
Douglas Gregor407d1f92011-11-09 02:13:45 +00002109 Result += "in ";
Douglas Gregor8f08d742011-07-30 07:55:26 +00002110 else if (ObjCQuals & Decl::OBJC_TQ_Inout)
Douglas Gregor407d1f92011-11-09 02:13:45 +00002111 Result += "inout ";
Douglas Gregor8f08d742011-07-30 07:55:26 +00002112 else if (ObjCQuals & Decl::OBJC_TQ_Out)
Douglas Gregor407d1f92011-11-09 02:13:45 +00002113 Result += "out ";
Douglas Gregor8f08d742011-07-30 07:55:26 +00002114 if (ObjCQuals & Decl::OBJC_TQ_Bycopy)
Douglas Gregor407d1f92011-11-09 02:13:45 +00002115 Result += "bycopy ";
Douglas Gregor8f08d742011-07-30 07:55:26 +00002116 else if (ObjCQuals & Decl::OBJC_TQ_Byref)
Douglas Gregor407d1f92011-11-09 02:13:45 +00002117 Result += "byref ";
Douglas Gregor8f08d742011-07-30 07:55:26 +00002118 if (ObjCQuals & Decl::OBJC_TQ_Oneway)
Douglas Gregor407d1f92011-11-09 02:13:45 +00002119 Result += "oneway ";
Douglas Gregor86b42682015-06-19 18:27:52 +00002120 if (ObjCQuals & Decl::OBJC_TQ_CSNullability) {
2121 if (auto nullability = AttributedType::stripOuterNullability(Type)) {
2122 switch (*nullability) {
2123 case NullabilityKind::NonNull:
2124 Result += "nonnull ";
2125 break;
2126
2127 case NullabilityKind::Nullable:
2128 Result += "nullable ";
2129 break;
2130
2131 case NullabilityKind::Unspecified:
2132 Result += "null_unspecified ";
2133 break;
2134 }
2135 }
2136 }
Douglas Gregor8f08d742011-07-30 07:55:26 +00002137 return Result;
2138}
2139
Richard Smith20e883e2015-04-29 23:20:19 +00002140static std::string FormatFunctionParameter(const PrintingPolicy &Policy,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002141 const ParmVarDecl *Param,
Douglas Gregord793e7c2011-10-18 04:23:19 +00002142 bool SuppressName = false,
2143 bool SuppressBlock = false) {
Douglas Gregore90dd002010-08-24 16:15:59 +00002144 bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->getDeclContext());
2145 if (Param->getType()->isDependentType() ||
2146 !Param->getType()->isBlockPointerType()) {
2147 // The argument for a dependent or non-block parameter is a placeholder
2148 // containing that parameter's type.
2149 std::string Result;
2150
Douglas Gregor981a0c42010-08-29 19:47:46 +00002151 if (Param->getIdentifier() && !ObjCMethodParam && !SuppressName)
Douglas Gregore90dd002010-08-24 16:15:59 +00002152 Result = Param->getIdentifier()->getName();
2153
Douglas Gregor86b42682015-06-19 18:27:52 +00002154 QualType Type = Param->getType();
Douglas Gregore90dd002010-08-24 16:15:59 +00002155 if (ObjCMethodParam) {
Douglas Gregor86b42682015-06-19 18:27:52 +00002156 Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(),
2157 Type);
2158 Result += Type.getAsString(Policy) + ")";
Douglas Gregor981a0c42010-08-29 19:47:46 +00002159 if (Param->getIdentifier() && !SuppressName)
Douglas Gregore90dd002010-08-24 16:15:59 +00002160 Result += Param->getIdentifier()->getName();
Douglas Gregor86b42682015-06-19 18:27:52 +00002161 } else {
2162 Type.getAsStringInternal(Result, Policy);
Douglas Gregore90dd002010-08-24 16:15:59 +00002163 }
2164 return Result;
2165 }
2166
2167 // The argument for a block pointer parameter is a block literal with
2168 // the appropriate type.
David Blaikie6adc78e2013-02-18 22:06:02 +00002169 FunctionTypeLoc Block;
2170 FunctionProtoTypeLoc BlockProto;
Douglas Gregore90dd002010-08-24 16:15:59 +00002171 TypeLoc TL;
2172 if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) {
2173 TL = TSInfo->getTypeLoc().getUnqualifiedLoc();
2174 while (true) {
2175 // Look through typedefs.
Douglas Gregord793e7c2011-10-18 04:23:19 +00002176 if (!SuppressBlock) {
David Blaikie6adc78e2013-02-18 22:06:02 +00002177 if (TypedefTypeLoc TypedefTL = TL.getAs<TypedefTypeLoc>()) {
2178 if (TypeSourceInfo *InnerTSInfo =
2179 TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) {
Douglas Gregord793e7c2011-10-18 04:23:19 +00002180 TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc();
2181 continue;
2182 }
2183 }
2184
2185 // Look through qualified types
David Blaikie6adc78e2013-02-18 22:06:02 +00002186 if (QualifiedTypeLoc QualifiedTL = TL.getAs<QualifiedTypeLoc>()) {
2187 TL = QualifiedTL.getUnqualifiedLoc();
Douglas Gregore90dd002010-08-24 16:15:59 +00002188 continue;
2189 }
2190 }
2191
Douglas Gregore90dd002010-08-24 16:15:59 +00002192 // Try to get the function prototype behind the block pointer type,
2193 // then we're done.
David Blaikie6adc78e2013-02-18 22:06:02 +00002194 if (BlockPointerTypeLoc BlockPtr = TL.getAs<BlockPointerTypeLoc>()) {
2195 TL = BlockPtr.getPointeeLoc().IgnoreParens();
2196 Block = TL.getAs<FunctionTypeLoc>();
2197 BlockProto = TL.getAs<FunctionProtoTypeLoc>();
Douglas Gregore90dd002010-08-24 16:15:59 +00002198 }
2199 break;
2200 }
2201 }
2202
2203 if (!Block) {
2204 // We were unable to find a FunctionProtoTypeLoc with parameter names
2205 // for the block; just use the parameter type as a placeholder.
2206 std::string Result;
Douglas Gregord793e7c2011-10-18 04:23:19 +00002207 if (!ObjCMethodParam && Param->getIdentifier())
2208 Result = Param->getIdentifier()->getName();
2209
Douglas Gregor86b42682015-06-19 18:27:52 +00002210 QualType Type = Param->getType().getUnqualifiedType();
Douglas Gregore90dd002010-08-24 16:15:59 +00002211
2212 if (ObjCMethodParam) {
Douglas Gregor86b42682015-06-19 18:27:52 +00002213 Result = "(" + formatObjCParamQualifiers(Param->getObjCDeclQualifier(),
2214 Type);
2215 Result += Type.getAsString(Policy) + Result + ")";
Douglas Gregore90dd002010-08-24 16:15:59 +00002216 if (Param->getIdentifier())
2217 Result += Param->getIdentifier()->getName();
Douglas Gregor86b42682015-06-19 18:27:52 +00002218 } else {
2219 Type.getAsStringInternal(Result, Policy);
Douglas Gregore90dd002010-08-24 16:15:59 +00002220 }
2221
2222 return Result;
2223 }
Douglas Gregord793e7c2011-10-18 04:23:19 +00002224
Douglas Gregore90dd002010-08-24 16:15:59 +00002225 // We have the function prototype behind the block pointer type, as it was
2226 // written in the source.
Douglas Gregor67da50e2010-09-08 22:47:51 +00002227 std::string Result;
Alp Toker314cc812014-01-25 16:55:45 +00002228 QualType ResultType = Block.getTypePtr()->getReturnType();
Douglas Gregord793e7c2011-10-18 04:23:19 +00002229 if (!ResultType->isVoidType() || SuppressBlock)
John McCall31168b02011-06-15 23:02:42 +00002230 ResultType.getAsStringInternal(Result, Policy);
Douglas Gregord793e7c2011-10-18 04:23:19 +00002231
2232 // Format the parameter list.
2233 std::string Params;
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00002234 if (!BlockProto || Block.getNumParams() == 0) {
David Blaikie6adc78e2013-02-18 22:06:02 +00002235 if (BlockProto && BlockProto.getTypePtr()->isVariadic())
Douglas Gregord793e7c2011-10-18 04:23:19 +00002236 Params = "(...)";
Douglas Gregoraf25cfa2010-10-02 23:49:58 +00002237 else
Douglas Gregord793e7c2011-10-18 04:23:19 +00002238 Params = "(void)";
Douglas Gregor67da50e2010-09-08 22:47:51 +00002239 } else {
Douglas Gregord793e7c2011-10-18 04:23:19 +00002240 Params += "(";
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00002241 for (unsigned I = 0, N = Block.getNumParams(); I != N; ++I) {
Douglas Gregor67da50e2010-09-08 22:47:51 +00002242 if (I)
Douglas Gregord793e7c2011-10-18 04:23:19 +00002243 Params += ", ";
Richard Smith20e883e2015-04-29 23:20:19 +00002244 Params += FormatFunctionParameter(Policy, Block.getParam(I),
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00002245 /*SuppressName=*/false,
Douglas Gregord793e7c2011-10-18 04:23:19 +00002246 /*SuppressBlock=*/true);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00002247
David Blaikie6adc78e2013-02-18 22:06:02 +00002248 if (I == N - 1 && BlockProto.getTypePtr()->isVariadic())
Douglas Gregord793e7c2011-10-18 04:23:19 +00002249 Params += ", ...";
Douglas Gregor67da50e2010-09-08 22:47:51 +00002250 }
Douglas Gregord793e7c2011-10-18 04:23:19 +00002251 Params += ")";
Douglas Gregor400f5972010-08-31 05:13:43 +00002252 }
Douglas Gregor67da50e2010-09-08 22:47:51 +00002253
Douglas Gregord793e7c2011-10-18 04:23:19 +00002254 if (SuppressBlock) {
2255 // Format as a parameter.
2256 Result = Result + " (^";
2257 if (Param->getIdentifier())
2258 Result += Param->getIdentifier()->getName();
2259 Result += ")";
2260 Result += Params;
2261 } else {
2262 // Format as a block literal argument.
2263 Result = '^' + Result;
2264 Result += Params;
2265
2266 if (Param->getIdentifier())
2267 Result += Param->getIdentifier()->getName();
2268 }
2269
Douglas Gregore90dd002010-08-24 16:15:59 +00002270 return Result;
2271}
2272
Douglas Gregor3545ff42009-09-21 16:56:56 +00002273/// \brief Add function parameter chunks to the given code completion string.
Richard Smith20e883e2015-04-29 23:20:19 +00002274static void AddFunctionParameterChunks(Preprocessor &PP,
Douglas Gregor75acd922011-09-27 23:30:47 +00002275 const PrintingPolicy &Policy,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002276 const FunctionDecl *Function,
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002277 CodeCompletionBuilder &Result,
2278 unsigned Start = 0,
2279 bool InOptional = false) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002280 bool FirstParameter = true;
Douglas Gregor9eb77012009-11-07 00:00:49 +00002281
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002282 for (unsigned P = Start, N = Function->getNumParams(); P != N; ++P) {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002283 const ParmVarDecl *Param = Function->getParamDecl(P);
Douglas Gregor3545ff42009-09-21 16:56:56 +00002284
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002285 if (Param->hasDefaultArg() && !InOptional) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00002286 // When we see an optional default argument, put that argument and
2287 // the remaining default arguments into a new, optional string.
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00002288 CodeCompletionBuilder Opt(Result.getAllocator(),
2289 Result.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002290 if (!FirstParameter)
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002291 Opt.AddChunk(CodeCompletionString::CK_Comma);
Richard Smith20e883e2015-04-29 23:20:19 +00002292 AddFunctionParameterChunks(PP, Policy, Function, Opt, P, true);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002293 Result.AddOptionalChunk(Opt.TakeString());
2294 break;
Douglas Gregor3545ff42009-09-21 16:56:56 +00002295 }
2296
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002297 if (FirstParameter)
2298 FirstParameter = false;
2299 else
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002300 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002301
2302 InOptional = false;
Douglas Gregor3545ff42009-09-21 16:56:56 +00002303
2304 // Format the placeholder string.
Richard Smith20e883e2015-04-29 23:20:19 +00002305 std::string PlaceholderStr = FormatFunctionParameter(Policy, Param);
2306
Douglas Gregor400f5972010-08-31 05:13:43 +00002307 if (Function->isVariadic() && P == N - 1)
2308 PlaceholderStr += ", ...";
2309
Douglas Gregor3545ff42009-09-21 16:56:56 +00002310 // Add the placeholder string.
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002311 Result.AddPlaceholderChunk(
2312 Result.getAllocator().CopyString(PlaceholderStr));
Douglas Gregor3545ff42009-09-21 16:56:56 +00002313 }
Douglas Gregorba449032009-09-22 21:42:17 +00002314
2315 if (const FunctionProtoType *Proto
2316 = Function->getType()->getAs<FunctionProtoType>())
Douglas Gregordbb71db2010-08-23 23:51:41 +00002317 if (Proto->isVariadic()) {
Alp Toker9cacbab2014-01-20 20:26:09 +00002318 if (Proto->getNumParams() == 0)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002319 Result.AddPlaceholderChunk("...");
Douglas Gregordbb71db2010-08-23 23:51:41 +00002320
Richard Smith20e883e2015-04-29 23:20:19 +00002321 MaybeAddSentinel(PP, Function, Result);
Douglas Gregordbb71db2010-08-23 23:51:41 +00002322 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00002323}
2324
2325/// \brief Add template parameter chunks to the given code completion string.
2326static void AddTemplateParameterChunks(ASTContext &Context,
Douglas Gregor75acd922011-09-27 23:30:47 +00002327 const PrintingPolicy &Policy,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002328 const TemplateDecl *Template,
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002329 CodeCompletionBuilder &Result,
2330 unsigned MaxParameters = 0,
2331 unsigned Start = 0,
2332 bool InDefaultArg = false) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00002333 bool FirstParameter = true;
Richard Smith6eece292015-01-15 02:27:20 +00002334
2335 // Prefer to take the template parameter names from the first declaration of
2336 // the template.
2337 Template = cast<TemplateDecl>(Template->getCanonicalDecl());
2338
Douglas Gregor3545ff42009-09-21 16:56:56 +00002339 TemplateParameterList *Params = Template->getTemplateParameters();
2340 TemplateParameterList::iterator PEnd = Params->end();
2341 if (MaxParameters)
2342 PEnd = Params->begin() + MaxParameters;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002343 for (TemplateParameterList::iterator P = Params->begin() + Start;
2344 P != PEnd; ++P) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00002345 bool HasDefaultArg = false;
2346 std::string PlaceholderStr;
2347 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
2348 if (TTP->wasDeclaredWithTypename())
2349 PlaceholderStr = "typename";
2350 else
2351 PlaceholderStr = "class";
2352
2353 if (TTP->getIdentifier()) {
2354 PlaceholderStr += ' ';
2355 PlaceholderStr += TTP->getIdentifier()->getName();
2356 }
2357
2358 HasDefaultArg = TTP->hasDefaultArgument();
2359 } else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002360 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00002361 if (NTTP->getIdentifier())
2362 PlaceholderStr = NTTP->getIdentifier()->getName();
John McCall31168b02011-06-15 23:02:42 +00002363 NTTP->getType().getAsStringInternal(PlaceholderStr, Policy);
Douglas Gregor3545ff42009-09-21 16:56:56 +00002364 HasDefaultArg = NTTP->hasDefaultArgument();
2365 } else {
2366 assert(isa<TemplateTemplateParmDecl>(*P));
2367 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
2368
2369 // Since putting the template argument list into the placeholder would
2370 // be very, very long, we just use an abbreviation.
2371 PlaceholderStr = "template<...> class";
2372 if (TTP->getIdentifier()) {
2373 PlaceholderStr += ' ';
2374 PlaceholderStr += TTP->getIdentifier()->getName();
2375 }
2376
2377 HasDefaultArg = TTP->hasDefaultArgument();
2378 }
2379
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002380 if (HasDefaultArg && !InDefaultArg) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00002381 // When we see an optional default argument, put that argument and
2382 // the remaining default arguments into a new, optional string.
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00002383 CodeCompletionBuilder Opt(Result.getAllocator(),
2384 Result.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002385 if (!FirstParameter)
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002386 Opt.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor75acd922011-09-27 23:30:47 +00002387 AddTemplateParameterChunks(Context, Policy, Template, Opt, MaxParameters,
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002388 P - Params->begin(), true);
2389 Result.AddOptionalChunk(Opt.TakeString());
2390 break;
Douglas Gregor3545ff42009-09-21 16:56:56 +00002391 }
2392
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002393 InDefaultArg = false;
2394
Douglas Gregor3545ff42009-09-21 16:56:56 +00002395 if (FirstParameter)
2396 FirstParameter = false;
2397 else
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002398 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor3545ff42009-09-21 16:56:56 +00002399
2400 // Add the placeholder string.
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002401 Result.AddPlaceholderChunk(
2402 Result.getAllocator().CopyString(PlaceholderStr));
Douglas Gregor3545ff42009-09-21 16:56:56 +00002403 }
2404}
2405
Douglas Gregorf2510672009-09-21 19:57:38 +00002406/// \brief Add a qualifier to the given code-completion string, if the
2407/// provided nested-name-specifier is non-NULL.
Douglas Gregor0f622362009-12-11 18:44:16 +00002408static void
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002409AddQualifierToCompletionString(CodeCompletionBuilder &Result,
Douglas Gregor0f622362009-12-11 18:44:16 +00002410 NestedNameSpecifier *Qualifier,
2411 bool QualifierIsInformative,
Douglas Gregor75acd922011-09-27 23:30:47 +00002412 ASTContext &Context,
2413 const PrintingPolicy &Policy) {
Douglas Gregorf2510672009-09-21 19:57:38 +00002414 if (!Qualifier)
2415 return;
2416
2417 std::string PrintedNNS;
2418 {
2419 llvm::raw_string_ostream OS(PrintedNNS);
Douglas Gregor75acd922011-09-27 23:30:47 +00002420 Qualifier->print(OS, Policy);
Douglas Gregorf2510672009-09-21 19:57:38 +00002421 }
Douglas Gregor5bf52692009-09-22 23:15:58 +00002422 if (QualifierIsInformative)
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002423 Result.AddInformativeChunk(Result.getAllocator().CopyString(PrintedNNS));
Douglas Gregor5bf52692009-09-22 23:15:58 +00002424 else
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002425 Result.AddTextChunk(Result.getAllocator().CopyString(PrintedNNS));
Douglas Gregorf2510672009-09-21 19:57:38 +00002426}
2427
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002428static void
2429AddFunctionTypeQualsToCompletionString(CodeCompletionBuilder &Result,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002430 const FunctionDecl *Function) {
Douglas Gregor0f622362009-12-11 18:44:16 +00002431 const FunctionProtoType *Proto
2432 = Function->getType()->getAs<FunctionProtoType>();
2433 if (!Proto || !Proto->getTypeQuals())
2434 return;
2435
Douglas Gregor304f9b02011-02-01 21:15:40 +00002436 // FIXME: Add ref-qualifier!
2437
2438 // Handle single qualifiers without copying
2439 if (Proto->getTypeQuals() == Qualifiers::Const) {
2440 Result.AddInformativeChunk(" const");
2441 return;
2442 }
2443
2444 if (Proto->getTypeQuals() == Qualifiers::Volatile) {
2445 Result.AddInformativeChunk(" volatile");
2446 return;
2447 }
2448
2449 if (Proto->getTypeQuals() == Qualifiers::Restrict) {
2450 Result.AddInformativeChunk(" restrict");
2451 return;
2452 }
2453
2454 // Handle multiple qualifiers.
Douglas Gregor0f622362009-12-11 18:44:16 +00002455 std::string QualsStr;
David Blaikief5697e52012-08-10 00:55:35 +00002456 if (Proto->isConst())
Douglas Gregor0f622362009-12-11 18:44:16 +00002457 QualsStr += " const";
David Blaikief5697e52012-08-10 00:55:35 +00002458 if (Proto->isVolatile())
Douglas Gregor0f622362009-12-11 18:44:16 +00002459 QualsStr += " volatile";
David Blaikief5697e52012-08-10 00:55:35 +00002460 if (Proto->isRestrict())
Douglas Gregor0f622362009-12-11 18:44:16 +00002461 QualsStr += " restrict";
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002462 Result.AddInformativeChunk(Result.getAllocator().CopyString(QualsStr));
Douglas Gregor0f622362009-12-11 18:44:16 +00002463}
2464
Douglas Gregor0212fd72010-09-21 16:06:22 +00002465/// \brief Add the name of the given declaration
Douglas Gregor75acd922011-09-27 23:30:47 +00002466static void AddTypedNameChunk(ASTContext &Context, const PrintingPolicy &Policy,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002467 const NamedDecl *ND,
2468 CodeCompletionBuilder &Result) {
Douglas Gregor0212fd72010-09-21 16:06:22 +00002469 DeclarationName Name = ND->getDeclName();
2470 if (!Name)
2471 return;
2472
2473 switch (Name.getNameKind()) {
Douglas Gregor304f9b02011-02-01 21:15:40 +00002474 case DeclarationName::CXXOperatorName: {
Craig Topperc3ec1492014-05-26 06:22:03 +00002475 const char *OperatorName = nullptr;
Douglas Gregor304f9b02011-02-01 21:15:40 +00002476 switch (Name.getCXXOverloadedOperator()) {
2477 case OO_None:
2478 case OO_Conditional:
2479 case NUM_OVERLOADED_OPERATORS:
2480 OperatorName = "operator";
2481 break;
2482
2483#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
2484 case OO_##Name: OperatorName = "operator" Spelling; break;
2485#define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly)
2486#include "clang/Basic/OperatorKinds.def"
2487
2488 case OO_New: OperatorName = "operator new"; break;
2489 case OO_Delete: OperatorName = "operator delete"; break;
2490 case OO_Array_New: OperatorName = "operator new[]"; break;
2491 case OO_Array_Delete: OperatorName = "operator delete[]"; break;
2492 case OO_Call: OperatorName = "operator()"; break;
2493 case OO_Subscript: OperatorName = "operator[]"; break;
2494 }
2495 Result.AddTypedTextChunk(OperatorName);
2496 break;
2497 }
2498
Douglas Gregor0212fd72010-09-21 16:06:22 +00002499 case DeclarationName::Identifier:
2500 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor0212fd72010-09-21 16:06:22 +00002501 case DeclarationName::CXXDestructorName:
2502 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002503 Result.AddTypedTextChunk(
2504 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregor0212fd72010-09-21 16:06:22 +00002505 break;
2506
2507 case DeclarationName::CXXUsingDirective:
2508 case DeclarationName::ObjCZeroArgSelector:
2509 case DeclarationName::ObjCOneArgSelector:
2510 case DeclarationName::ObjCMultiArgSelector:
2511 break;
2512
2513 case DeclarationName::CXXConstructorName: {
Craig Topperc3ec1492014-05-26 06:22:03 +00002514 CXXRecordDecl *Record = nullptr;
Douglas Gregor0212fd72010-09-21 16:06:22 +00002515 QualType Ty = Name.getCXXNameType();
2516 if (const RecordType *RecordTy = Ty->getAs<RecordType>())
2517 Record = cast<CXXRecordDecl>(RecordTy->getDecl());
2518 else if (const InjectedClassNameType *InjectedTy
2519 = Ty->getAs<InjectedClassNameType>())
2520 Record = InjectedTy->getDecl();
2521 else {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002522 Result.AddTypedTextChunk(
2523 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregor0212fd72010-09-21 16:06:22 +00002524 break;
2525 }
2526
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002527 Result.AddTypedTextChunk(
2528 Result.getAllocator().CopyString(Record->getNameAsString()));
Douglas Gregor0212fd72010-09-21 16:06:22 +00002529 if (ClassTemplateDecl *Template = Record->getDescribedClassTemplate()) {
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002530 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
Douglas Gregor75acd922011-09-27 23:30:47 +00002531 AddTemplateParameterChunks(Context, Policy, Template, Result);
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002532 Result.AddChunk(CodeCompletionString::CK_RightAngle);
Douglas Gregor0212fd72010-09-21 16:06:22 +00002533 }
2534 break;
2535 }
2536 }
2537}
2538
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002539CodeCompletionString *CodeCompletionResult::CreateCodeCompletionString(Sema &S,
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00002540 CodeCompletionAllocator &Allocator,
Dmitri Gribenko3292d062012-07-02 17:35:10 +00002541 CodeCompletionTUInfo &CCTUInfo,
2542 bool IncludeBriefComments) {
2543 return CreateCodeCompletionString(S.Context, S.PP, Allocator, CCTUInfo,
2544 IncludeBriefComments);
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002545}
2546
Douglas Gregor3545ff42009-09-21 16:56:56 +00002547/// \brief If possible, create a new code completion string for the given
2548/// result.
2549///
2550/// \returns Either a new, heap-allocated code completion string describing
2551/// how to use this result, or NULL to indicate that the string or name of the
2552/// result is all that is needed.
2553CodeCompletionString *
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002554CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
2555 Preprocessor &PP,
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00002556 CodeCompletionAllocator &Allocator,
Dmitri Gribenko3292d062012-07-02 17:35:10 +00002557 CodeCompletionTUInfo &CCTUInfo,
2558 bool IncludeBriefComments) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00002559 CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability);
Douglas Gregor9eb77012009-11-07 00:00:49 +00002560
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002561 PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002562 if (Kind == RK_Pattern) {
2563 Pattern->Priority = Priority;
2564 Pattern->Availability = Availability;
Douglas Gregor78254c82012-03-27 23:34:16 +00002565
2566 if (Declaration) {
2567 Result.addParentContext(Declaration->getDeclContext());
Douglas Gregor78254c82012-03-27 23:34:16 +00002568 Pattern->ParentName = Result.getParentName();
Fariborz Jahanian1fcf4922013-03-22 17:55:27 +00002569 // Provide code completion comment for self.GetterName where
2570 // GetterName is the getter method for a property with name
2571 // different from the property name (declared via a property
2572 // getter attribute.
2573 const NamedDecl *ND = Declaration;
2574 if (const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(ND))
2575 if (M->isPropertyAccessor())
2576 if (const ObjCPropertyDecl *PDecl = M->findPropertyDecl())
2577 if (PDecl->getGetterName() == M->getSelector() &&
Fariborz Jahanianbe8bc672013-03-23 01:10:45 +00002578 PDecl->getIdentifier() != M->getIdentifier()) {
2579 if (const RawComment *RC =
2580 Ctx.getRawCommentForAnyRedecl(M)) {
Fariborz Jahanian1fcf4922013-03-22 17:55:27 +00002581 Result.addBriefComment(RC->getBriefText(Ctx));
2582 Pattern->BriefComment = Result.getBriefComment();
2583 }
Fariborz Jahanianbe8bc672013-03-23 01:10:45 +00002584 else if (const RawComment *RC =
2585 Ctx.getRawCommentForAnyRedecl(PDecl)) {
2586 Result.addBriefComment(RC->getBriefText(Ctx));
2587 Pattern->BriefComment = Result.getBriefComment();
2588 }
2589 }
Douglas Gregor78254c82012-03-27 23:34:16 +00002590 }
2591
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002592 return Pattern;
2593 }
Douglas Gregorf09935f2009-12-01 05:55:20 +00002594
Douglas Gregorf09935f2009-12-01 05:55:20 +00002595 if (Kind == RK_Keyword) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002596 Result.AddTypedTextChunk(Keyword);
2597 return Result.TakeString();
Douglas Gregorf09935f2009-12-01 05:55:20 +00002598 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00002599
Douglas Gregorf329c7c2009-10-30 16:50:04 +00002600 if (Kind == RK_Macro) {
Richard Smith20e883e2015-04-29 23:20:19 +00002601 const MacroInfo *MI = PP.getMacroInfo(Macro);
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002602 Result.AddTypedTextChunk(
2603 Result.getAllocator().CopyString(Macro->getName()));
Douglas Gregorf09935f2009-12-01 05:55:20 +00002604
Ben Langmuirc28ce3a2014-09-30 20:00:18 +00002605 if (!MI || !MI->isFunctionLike())
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002606 return Result.TakeString();
Douglas Gregorf329c7c2009-10-30 16:50:04 +00002607
2608 // Format a function-like macro with placeholders for the arguments.
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002609 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregor0c505312011-07-30 08:17:44 +00002610 MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end();
Douglas Gregor3aa55262012-01-21 00:43:38 +00002611
2612 // C99 variadic macros add __VA_ARGS__ at the end. Skip it.
2613 if (MI->isC99Varargs()) {
2614 --AEnd;
2615
2616 if (A == AEnd) {
2617 Result.AddPlaceholderChunk("...");
2618 }
Douglas Gregor0c505312011-07-30 08:17:44 +00002619 }
Douglas Gregor3aa55262012-01-21 00:43:38 +00002620
Douglas Gregor0c505312011-07-30 08:17:44 +00002621 for (MacroInfo::arg_iterator A = MI->arg_begin(); A != AEnd; ++A) {
Douglas Gregorf329c7c2009-10-30 16:50:04 +00002622 if (A != MI->arg_begin())
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002623 Result.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregor3aa55262012-01-21 00:43:38 +00002624
2625 if (MI->isVariadic() && (A+1) == AEnd) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002626 SmallString<32> Arg = (*A)->getName();
Douglas Gregor3aa55262012-01-21 00:43:38 +00002627 if (MI->isC99Varargs())
2628 Arg += ", ...";
2629 else
2630 Arg += "...";
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002631 Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregor3aa55262012-01-21 00:43:38 +00002632 break;
Douglas Gregorf329c7c2009-10-30 16:50:04 +00002633 }
Douglas Gregor3aa55262012-01-21 00:43:38 +00002634
2635 // Non-variadic macros are simple.
2636 Result.AddPlaceholderChunk(
2637 Result.getAllocator().CopyString((*A)->getName()));
Douglas Gregor0c505312011-07-30 08:17:44 +00002638 }
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002639 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002640 return Result.TakeString();
Douglas Gregorf329c7c2009-10-30 16:50:04 +00002641 }
2642
Douglas Gregorf64acca2010-05-25 21:41:55 +00002643 assert(Kind == RK_Declaration && "Missed a result kind?");
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002644 const NamedDecl *ND = Declaration;
Douglas Gregor78254c82012-03-27 23:34:16 +00002645 Result.addParentContext(ND->getDeclContext());
Dmitri Gribenko3292d062012-07-02 17:35:10 +00002646
2647 if (IncludeBriefComments) {
2648 // Add documentation comment, if it exists.
Dmitri Gribenkoa43ec182012-08-11 00:51:43 +00002649 if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(ND)) {
Dmitri Gribenko3292d062012-07-02 17:35:10 +00002650 Result.addBriefComment(RC->getBriefText(Ctx));
Fariborz Jahanian15a0b552013-02-28 17:47:14 +00002651 }
2652 else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
2653 if (OMD->isPropertyAccessor())
2654 if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl())
2655 if (const RawComment *RC = Ctx.getRawCommentForAnyRedecl(PDecl))
2656 Result.addBriefComment(RC->getBriefText(Ctx));
Dmitri Gribenko3292d062012-07-02 17:35:10 +00002657 }
2658
Douglas Gregor9eb77012009-11-07 00:00:49 +00002659 if (StartsNestedNameSpecifier) {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002660 Result.AddTypedTextChunk(
2661 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002662 Result.AddTextChunk("::");
2663 return Result.TakeString();
Douglas Gregor9eb77012009-11-07 00:00:49 +00002664 }
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00002665
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +00002666 for (const auto *I : ND->specific_attrs<AnnotateAttr>())
2667 Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation()));
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002668
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002669 AddResultTypeChunk(Ctx, Policy, ND, Result);
Douglas Gregorb3fa9192009-12-18 18:53:37 +00002670
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002671 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
Douglas Gregor5bf52692009-09-22 23:15:58 +00002672 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002673 Ctx, Policy);
2674 AddTypedNameChunk(Ctx, Policy, ND, Result);
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002675 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Richard Smith20e883e2015-04-29 23:20:19 +00002676 AddFunctionParameterChunks(PP, Policy, Function, Result);
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002677 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor0f622362009-12-11 18:44:16 +00002678 AddFunctionTypeQualsToCompletionString(Result, Function);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002679 return Result.TakeString();
Douglas Gregor3545ff42009-09-21 16:56:56 +00002680 }
2681
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002682 if (const FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
Douglas Gregor5bf52692009-09-22 23:15:58 +00002683 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002684 Ctx, Policy);
Douglas Gregor3545ff42009-09-21 16:56:56 +00002685 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002686 AddTypedNameChunk(Ctx, Policy, Function, Result);
Douglas Gregor0212fd72010-09-21 16:06:22 +00002687
Douglas Gregor3545ff42009-09-21 16:56:56 +00002688 // Figure out which template parameters are deduced (or have default
2689 // arguments).
Benjamin Kramere0513cb2012-01-30 16:17:39 +00002690 llvm::SmallBitVector Deduced;
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002691 Sema::MarkDeducedTemplateParameters(Ctx, FunTmpl, Deduced);
Douglas Gregor3545ff42009-09-21 16:56:56 +00002692 unsigned LastDeducibleArgument;
2693 for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0;
2694 --LastDeducibleArgument) {
2695 if (!Deduced[LastDeducibleArgument - 1]) {
2696 // C++0x: Figure out if the template argument has a default. If so,
2697 // the user doesn't need to type this argument.
2698 // FIXME: We need to abstract template parameters better!
2699 bool HasDefaultArg = false;
2700 NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002701 LastDeducibleArgument - 1);
Douglas Gregor3545ff42009-09-21 16:56:56 +00002702 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
2703 HasDefaultArg = TTP->hasDefaultArgument();
2704 else if (NonTypeTemplateParmDecl *NTTP
2705 = dyn_cast<NonTypeTemplateParmDecl>(Param))
2706 HasDefaultArg = NTTP->hasDefaultArgument();
2707 else {
2708 assert(isa<TemplateTemplateParmDecl>(Param));
2709 HasDefaultArg
Douglas Gregor9eb77012009-11-07 00:00:49 +00002710 = cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument();
Douglas Gregor3545ff42009-09-21 16:56:56 +00002711 }
2712
2713 if (!HasDefaultArg)
2714 break;
2715 }
2716 }
2717
2718 if (LastDeducibleArgument) {
2719 // Some of the function template arguments cannot be deduced from a
2720 // function call, so we introduce an explicit template argument list
2721 // containing all of the arguments up to the first deducible argument.
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002722 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002723 AddTemplateParameterChunks(Ctx, Policy, FunTmpl, Result,
Douglas Gregor3545ff42009-09-21 16:56:56 +00002724 LastDeducibleArgument);
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002725 Result.AddChunk(CodeCompletionString::CK_RightAngle);
Douglas Gregor3545ff42009-09-21 16:56:56 +00002726 }
2727
2728 // Add the function parameters
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002729 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Richard Smith20e883e2015-04-29 23:20:19 +00002730 AddFunctionParameterChunks(PP, Policy, Function, Result);
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002731 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor0f622362009-12-11 18:44:16 +00002732 AddFunctionTypeQualsToCompletionString(Result, Function);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002733 return Result.TakeString();
Douglas Gregor3545ff42009-09-21 16:56:56 +00002734 }
2735
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002736 if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) {
Douglas Gregor5bf52692009-09-22 23:15:58 +00002737 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002738 Ctx, Policy);
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002739 Result.AddTypedTextChunk(
2740 Result.getAllocator().CopyString(Template->getNameAsString()));
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002741 Result.AddChunk(CodeCompletionString::CK_LeftAngle);
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002742 AddTemplateParameterChunks(Ctx, Policy, Template, Result);
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002743 Result.AddChunk(CodeCompletionString::CK_RightAngle);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002744 return Result.TakeString();
Douglas Gregor3545ff42009-09-21 16:56:56 +00002745 }
2746
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002747 if (const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) {
Douglas Gregord3c5d792009-11-17 16:44:22 +00002748 Selector Sel = Method->getSelector();
2749 if (Sel.isUnarySelector()) {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002750 Result.AddTypedTextChunk(Result.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00002751 Sel.getNameForSlot(0)));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002752 return Result.TakeString();
Douglas Gregord3c5d792009-11-17 16:44:22 +00002753 }
2754
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00002755 std::string SelName = Sel.getNameForSlot(0).str();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002756 SelName += ':';
2757 if (StartParameter == 0)
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002758 Result.AddTypedTextChunk(Result.getAllocator().CopyString(SelName));
Douglas Gregor1b605f72009-11-19 01:08:35 +00002759 else {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002760 Result.AddInformativeChunk(Result.getAllocator().CopyString(SelName));
Douglas Gregor1b605f72009-11-19 01:08:35 +00002761
2762 // If there is only one parameter, and we're past it, add an empty
2763 // typed-text chunk since there is nothing to type.
2764 if (Method->param_size() == 1)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002765 Result.AddTypedTextChunk("");
Douglas Gregor1b605f72009-11-19 01:08:35 +00002766 }
Douglas Gregord3c5d792009-11-17 16:44:22 +00002767 unsigned Idx = 0;
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00002768 for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(),
2769 PEnd = Method->param_end();
Douglas Gregord3c5d792009-11-17 16:44:22 +00002770 P != PEnd; (void)++P, ++Idx) {
2771 if (Idx > 0) {
Douglas Gregor1b605f72009-11-19 01:08:35 +00002772 std::string Keyword;
2773 if (Idx > StartParameter)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002774 Result.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregord3c5d792009-11-17 16:44:22 +00002775 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx))
Benjamin Kramer632500c2011-07-26 16:59:25 +00002776 Keyword += II->getName();
Douglas Gregord3c5d792009-11-17 16:44:22 +00002777 Keyword += ":";
Douglas Gregor95887f92010-07-08 23:20:03 +00002778 if (Idx < StartParameter || AllParametersAreInformative)
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002779 Result.AddInformativeChunk(Result.getAllocator().CopyString(Keyword));
Douglas Gregor8e3e8742010-10-18 21:05:04 +00002780 else
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002781 Result.AddTypedTextChunk(Result.getAllocator().CopyString(Keyword));
Douglas Gregord3c5d792009-11-17 16:44:22 +00002782 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002783
2784 // If we're before the starting parameter, skip the placeholder.
2785 if (Idx < StartParameter)
2786 continue;
Douglas Gregord3c5d792009-11-17 16:44:22 +00002787
2788 std::string Arg;
Douglas Gregore90dd002010-08-24 16:15:59 +00002789
2790 if ((*P)->getType()->isBlockPointerType() && !DeclaringEntity)
Richard Smith20e883e2015-04-29 23:20:19 +00002791 Arg = FormatFunctionParameter(Policy, *P, true);
Douglas Gregore90dd002010-08-24 16:15:59 +00002792 else {
Douglas Gregor86b42682015-06-19 18:27:52 +00002793 QualType Type = (*P)->getType();
2794 Arg = "(" + formatObjCParamQualifiers((*P)->getObjCDeclQualifier(),
2795 Type);
2796 Arg += Type.getAsString(Policy) + ")";
Douglas Gregore90dd002010-08-24 16:15:59 +00002797 if (IdentifierInfo *II = (*P)->getIdentifier())
Douglas Gregor981a0c42010-08-29 19:47:46 +00002798 if (DeclaringEntity || AllParametersAreInformative)
Benjamin Kramer632500c2011-07-26 16:59:25 +00002799 Arg += II->getName();
Douglas Gregore90dd002010-08-24 16:15:59 +00002800 }
2801
Douglas Gregor400f5972010-08-31 05:13:43 +00002802 if (Method->isVariadic() && (P + 1) == PEnd)
2803 Arg += ", ...";
2804
Douglas Gregor95887f92010-07-08 23:20:03 +00002805 if (DeclaringEntity)
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002806 Result.AddTextChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregor95887f92010-07-08 23:20:03 +00002807 else if (AllParametersAreInformative)
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002808 Result.AddInformativeChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregorc8537c52009-11-19 07:41:15 +00002809 else
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002810 Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
Douglas Gregord3c5d792009-11-17 16:44:22 +00002811 }
2812
Douglas Gregor04c5f972009-12-23 00:21:46 +00002813 if (Method->isVariadic()) {
Douglas Gregor400f5972010-08-31 05:13:43 +00002814 if (Method->param_size() == 0) {
2815 if (DeclaringEntity)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002816 Result.AddTextChunk(", ...");
Douglas Gregor400f5972010-08-31 05:13:43 +00002817 else if (AllParametersAreInformative)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002818 Result.AddInformativeChunk(", ...");
Douglas Gregor400f5972010-08-31 05:13:43 +00002819 else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002820 Result.AddPlaceholderChunk(", ...");
Douglas Gregor400f5972010-08-31 05:13:43 +00002821 }
Douglas Gregordbb71db2010-08-23 23:51:41 +00002822
Richard Smith20e883e2015-04-29 23:20:19 +00002823 MaybeAddSentinel(PP, Method, Result);
Douglas Gregor04c5f972009-12-23 00:21:46 +00002824 }
2825
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002826 return Result.TakeString();
Douglas Gregord3c5d792009-11-17 16:44:22 +00002827 }
2828
Douglas Gregorf09935f2009-12-01 05:55:20 +00002829 if (Qualifier)
Douglas Gregor5bf52692009-09-22 23:15:58 +00002830 AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
Argyrios Kyrtzidis8d05ca72012-01-17 02:15:51 +00002831 Ctx, Policy);
Douglas Gregorf09935f2009-12-01 05:55:20 +00002832
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002833 Result.AddTypedTextChunk(
2834 Result.getAllocator().CopyString(ND->getNameAsString()));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002835 return Result.TakeString();
Douglas Gregor3545ff42009-09-21 16:56:56 +00002836}
2837
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00002838/// \brief Add function overload parameter chunks to the given code completion
2839/// string.
2840static void AddOverloadParameterChunks(ASTContext &Context,
2841 const PrintingPolicy &Policy,
2842 const FunctionDecl *Function,
2843 const FunctionProtoType *Prototype,
2844 CodeCompletionBuilder &Result,
2845 unsigned CurrentArg,
2846 unsigned Start = 0,
2847 bool InOptional = false) {
2848 bool FirstParameter = true;
2849 unsigned NumParams = Function ? Function->getNumParams()
2850 : Prototype->getNumParams();
2851
2852 for (unsigned P = Start; P != NumParams; ++P) {
2853 if (Function && Function->getParamDecl(P)->hasDefaultArg() && !InOptional) {
2854 // When we see an optional default argument, put that argument and
2855 // the remaining default arguments into a new, optional string.
2856 CodeCompletionBuilder Opt(Result.getAllocator(),
2857 Result.getCodeCompletionTUInfo());
2858 if (!FirstParameter)
2859 Opt.AddChunk(CodeCompletionString::CK_Comma);
2860 // Optional sections are nested.
2861 AddOverloadParameterChunks(Context, Policy, Function, Prototype, Opt,
2862 CurrentArg, P, /*InOptional=*/true);
2863 Result.AddOptionalChunk(Opt.TakeString());
2864 return;
2865 }
2866
2867 if (FirstParameter)
2868 FirstParameter = false;
2869 else
2870 Result.AddChunk(CodeCompletionString::CK_Comma);
2871
2872 InOptional = false;
2873
2874 // Format the placeholder string.
2875 std::string Placeholder;
2876 if (Function)
Richard Smith20e883e2015-04-29 23:20:19 +00002877 Placeholder = FormatFunctionParameter(Policy, Function->getParamDecl(P));
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00002878 else
2879 Placeholder = Prototype->getParamType(P).getAsString(Policy);
2880
2881 if (P == CurrentArg)
2882 Result.AddCurrentParameterChunk(
2883 Result.getAllocator().CopyString(Placeholder));
2884 else
2885 Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Placeholder));
2886 }
2887
2888 if (Prototype && Prototype->isVariadic()) {
2889 CodeCompletionBuilder Opt(Result.getAllocator(),
2890 Result.getCodeCompletionTUInfo());
2891 if (!FirstParameter)
2892 Opt.AddChunk(CodeCompletionString::CK_Comma);
2893
2894 if (CurrentArg < NumParams)
2895 Opt.AddPlaceholderChunk("...");
2896 else
2897 Opt.AddCurrentParameterChunk("...");
2898
2899 Result.AddOptionalChunk(Opt.TakeString());
2900 }
2901}
2902
Douglas Gregorf0f51982009-09-23 00:34:09 +00002903CodeCompletionString *
2904CodeCompleteConsumer::OverloadCandidate::CreateSignatureString(
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00002905 unsigned CurrentArg, Sema &S,
2906 CodeCompletionAllocator &Allocator,
2907 CodeCompletionTUInfo &CCTUInfo,
2908 bool IncludeBriefComments) const {
Douglas Gregor75acd922011-09-27 23:30:47 +00002909 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
John McCall31168b02011-06-15 23:02:42 +00002910
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002911 // FIXME: Set priority, availability appropriately.
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00002912 CodeCompletionBuilder Result(Allocator,CCTUInfo, 1, CXAvailability_Available);
Douglas Gregorf0f51982009-09-23 00:34:09 +00002913 FunctionDecl *FDecl = getFunction();
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00002914 const FunctionProtoType *Proto
Douglas Gregorf0f51982009-09-23 00:34:09 +00002915 = dyn_cast<FunctionProtoType>(getFunctionType());
2916 if (!FDecl && !Proto) {
2917 // Function without a prototype. Just give the return type and a
2918 // highlighted ellipsis.
2919 const FunctionType *FT = getFunctionType();
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00002920 Result.AddResultTypeChunk(Result.getAllocator().CopyString(
2921 FT->getReturnType().getAsString(Policy)));
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002922 Result.AddChunk(CodeCompletionString::CK_LeftParen);
2923 Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "...");
2924 Result.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002925 return Result.TakeString();
Douglas Gregorf0f51982009-09-23 00:34:09 +00002926 }
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00002927
2928 if (FDecl) {
2929 if (IncludeBriefComments && CurrentArg < FDecl->getNumParams())
2930 if (auto RC = S.getASTContext().getRawCommentForAnyRedecl(
2931 FDecl->getParamDecl(CurrentArg)))
2932 Result.addBriefComment(RC->getBriefText(S.getASTContext()));
2933 AddResultTypeChunk(S.Context, Policy, FDecl, Result);
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00002934 Result.AddTextChunk(
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00002935 Result.getAllocator().CopyString(FDecl->getNameAsString()));
2936 } else {
2937 Result.AddResultTypeChunk(
2938 Result.getAllocator().CopyString(
Alp Toker314cc812014-01-25 16:55:45 +00002939 Proto->getReturnType().getAsString(Policy)));
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00002940 }
Alp Toker314cc812014-01-25 16:55:45 +00002941
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002942 Result.AddChunk(CodeCompletionString::CK_LeftParen);
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00002943 AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result,
2944 CurrentArg);
Benjamin Kramerdb534a42012-03-26 16:57:36 +00002945 Result.AddChunk(CodeCompletionString::CK_RightParen);
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00002946
Douglas Gregorb278aaf2011-02-01 19:23:04 +00002947 return Result.TakeString();
Douglas Gregorf0f51982009-09-23 00:34:09 +00002948}
2949
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002950unsigned clang::getMacroUsagePriority(StringRef MacroName,
Douglas Gregor9dcf58a2010-09-20 21:11:48 +00002951 const LangOptions &LangOpts,
Douglas Gregor6e240332010-08-16 16:18:59 +00002952 bool PreferredTypeIsPointer) {
2953 unsigned Priority = CCP_Macro;
2954
Douglas Gregor9dcf58a2010-09-20 21:11:48 +00002955 // Treat the "nil", "Nil" and "NULL" macros as null pointer constants.
2956 if (MacroName.equals("nil") || MacroName.equals("NULL") ||
2957 MacroName.equals("Nil")) {
Douglas Gregor6e240332010-08-16 16:18:59 +00002958 Priority = CCP_Constant;
2959 if (PreferredTypeIsPointer)
2960 Priority = Priority / CCF_SimilarTypeMatch;
Douglas Gregor9dcf58a2010-09-20 21:11:48 +00002961 }
2962 // Treat "YES", "NO", "true", and "false" as constants.
2963 else if (MacroName.equals("YES") || MacroName.equals("NO") ||
2964 MacroName.equals("true") || MacroName.equals("false"))
2965 Priority = CCP_Constant;
2966 // Treat "bool" as a type.
2967 else if (MacroName.equals("bool"))
2968 Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0);
2969
Douglas Gregor6e240332010-08-16 16:18:59 +00002970
2971 return Priority;
2972}
2973
Dmitri Gribenkobdc80de2013-01-11 20:32:41 +00002974CXCursorKind clang::getCursorKindForDecl(const Decl *D) {
Douglas Gregor09c0eb12010-09-03 23:30:36 +00002975 if (!D)
2976 return CXCursor_UnexposedDecl;
2977
2978 switch (D->getKind()) {
2979 case Decl::Enum: return CXCursor_EnumDecl;
2980 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
2981 case Decl::Field: return CXCursor_FieldDecl;
2982 case Decl::Function:
2983 return CXCursor_FunctionDecl;
2984 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
2985 case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl;
Douglas Gregor09c0eb12010-09-03 23:30:36 +00002986 case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl;
Douglas Gregordeafd0b2011-12-27 22:43:10 +00002987
Argyrios Kyrtzidis3698cef2012-01-24 21:39:26 +00002988 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Douglas Gregor09c0eb12010-09-03 23:30:36 +00002989 case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl;
2990 case Decl::ObjCMethod:
2991 return cast<ObjCMethodDecl>(D)->isInstanceMethod()
2992 ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl;
2993 case Decl::CXXMethod: return CXCursor_CXXMethod;
2994 case Decl::CXXConstructor: return CXCursor_Constructor;
2995 case Decl::CXXDestructor: return CXCursor_Destructor;
2996 case Decl::CXXConversion: return CXCursor_ConversionFunction;
2997 case Decl::ObjCProperty: return CXCursor_ObjCPropertyDecl;
Argyrios Kyrtzidis3698cef2012-01-24 21:39:26 +00002998 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Douglas Gregor09c0eb12010-09-03 23:30:36 +00002999 case Decl::ParmVar: return CXCursor_ParmDecl;
3000 case Decl::Typedef: return CXCursor_TypedefDecl;
Richard Smithdda56e42011-04-15 14:24:37 +00003001 case Decl::TypeAlias: return CXCursor_TypeAliasDecl;
Douglas Gregor09c0eb12010-09-03 23:30:36 +00003002 case Decl::Var: return CXCursor_VarDecl;
3003 case Decl::Namespace: return CXCursor_Namespace;
3004 case Decl::NamespaceAlias: return CXCursor_NamespaceAlias;
3005 case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter;
3006 case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter;
3007 case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter;
3008 case Decl::FunctionTemplate: return CXCursor_FunctionTemplate;
3009 case Decl::ClassTemplate: return CXCursor_ClassTemplate;
Argyrios Kyrtzidis12afd702011-09-30 17:58:23 +00003010 case Decl::AccessSpec: return CXCursor_CXXAccessSpecifier;
Douglas Gregor09c0eb12010-09-03 23:30:36 +00003011 case Decl::ClassTemplatePartialSpecialization:
3012 return CXCursor_ClassTemplatePartialSpecialization;
3013 case Decl::UsingDirective: return CXCursor_UsingDirective;
Douglas Gregor3e653b32012-04-30 23:41:16 +00003014 case Decl::TranslationUnit: return CXCursor_TranslationUnit;
Douglas Gregor09c0eb12010-09-03 23:30:36 +00003015
3016 case Decl::Using:
3017 case Decl::UnresolvedUsingValue:
3018 case Decl::UnresolvedUsingTypename:
3019 return CXCursor_UsingDeclaration;
3020
Douglas Gregor4cd65962011-06-03 23:08:58 +00003021 case Decl::ObjCPropertyImpl:
3022 switch (cast<ObjCPropertyImplDecl>(D)->getPropertyImplementation()) {
3023 case ObjCPropertyImplDecl::Dynamic:
3024 return CXCursor_ObjCDynamicDecl;
3025
3026 case ObjCPropertyImplDecl::Synthesize:
3027 return CXCursor_ObjCSynthesizeDecl;
3028 }
Argyrios Kyrtzidis50e5b1d2012-10-05 00:22:24 +00003029
3030 case Decl::Import:
3031 return CXCursor_ModuleImportDecl;
Douglas Gregor4cd65962011-06-03 23:08:58 +00003032
Douglas Gregor09c0eb12010-09-03 23:30:36 +00003033 default:
Dmitri Gribenkobdc80de2013-01-11 20:32:41 +00003034 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor09c0eb12010-09-03 23:30:36 +00003035 switch (TD->getTagKind()) {
Joao Matosdc86f942012-08-31 18:45:21 +00003036 case TTK_Interface: // fall through
Douglas Gregor09c0eb12010-09-03 23:30:36 +00003037 case TTK_Struct: return CXCursor_StructDecl;
3038 case TTK_Class: return CXCursor_ClassDecl;
3039 case TTK_Union: return CXCursor_UnionDecl;
3040 case TTK_Enum: return CXCursor_EnumDecl;
3041 }
3042 }
3043 }
3044
3045 return CXCursor_UnexposedDecl;
3046}
3047
Douglas Gregor55b037b2010-07-08 20:55:51 +00003048static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results,
Douglas Gregor8cb17462012-10-09 16:01:50 +00003049 bool IncludeUndefined,
Douglas Gregor55b037b2010-07-08 20:55:51 +00003050 bool TargetTypeIsPointer = false) {
John McCall276321a2010-08-25 06:19:51 +00003051 typedef CodeCompletionResult Result;
Douglas Gregor55b037b2010-07-08 20:55:51 +00003052
Douglas Gregorf329c7c2009-10-30 16:50:04 +00003053 Results.EnterNewScope();
Douglas Gregor8e3e8742010-10-18 21:05:04 +00003054
Douglas Gregor9eb77012009-11-07 00:00:49 +00003055 for (Preprocessor::macro_iterator M = PP.macro_begin(),
3056 MEnd = PP.macro_end();
Douglas Gregor55b037b2010-07-08 20:55:51 +00003057 M != MEnd; ++M) {
Richard Smith20e883e2015-04-29 23:20:19 +00003058 auto MD = PP.getMacroDefinition(M->first);
3059 if (IncludeUndefined || MD) {
3060 if (MacroInfo *MI = MD.getMacroInfo())
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00003061 if (MI->isUsedForHeaderGuard())
3062 continue;
3063
Douglas Gregor8cb17462012-10-09 16:01:50 +00003064 Results.AddResult(Result(M->first,
Douglas Gregor6e240332010-08-16 16:18:59 +00003065 getMacroUsagePriority(M->first->getName(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00003066 PP.getLangOpts(),
Douglas Gregor6e240332010-08-16 16:18:59 +00003067 TargetTypeIsPointer)));
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00003068 }
Douglas Gregor55b037b2010-07-08 20:55:51 +00003069 }
Douglas Gregor8e3e8742010-10-18 21:05:04 +00003070
Douglas Gregorf329c7c2009-10-30 16:50:04 +00003071 Results.ExitScope();
Douglas Gregor8e3e8742010-10-18 21:05:04 +00003072
Douglas Gregorf329c7c2009-10-30 16:50:04 +00003073}
3074
Douglas Gregorce0e8562010-08-23 21:54:33 +00003075static void AddPrettyFunctionResults(const LangOptions &LangOpts,
3076 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00003077 typedef CodeCompletionResult Result;
Douglas Gregorce0e8562010-08-23 21:54:33 +00003078
3079 Results.EnterNewScope();
Douglas Gregor8e3e8742010-10-18 21:05:04 +00003080
Douglas Gregorce0e8562010-08-23 21:54:33 +00003081 Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant));
3082 Results.AddResult(Result("__FUNCTION__", CCP_Constant));
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003083 if (LangOpts.C99 || LangOpts.CPlusPlus11)
Douglas Gregorce0e8562010-08-23 21:54:33 +00003084 Results.AddResult(Result("__func__", CCP_Constant));
3085 Results.ExitScope();
3086}
3087
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003088static void HandleCodeCompleteResults(Sema *S,
3089 CodeCompleteConsumer *CodeCompleter,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003090 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +00003091 CodeCompletionResult *Results,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003092 unsigned NumResults) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00003093 if (CodeCompleter)
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003094 CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults);
Douglas Gregor3545ff42009-09-21 16:56:56 +00003095}
3096
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003097static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S,
3098 Sema::ParserCompletionContext PCC) {
3099 switch (PCC) {
John McCallfaf5fb42010-08-26 23:41:50 +00003100 case Sema::PCC_Namespace:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003101 return CodeCompletionContext::CCC_TopLevel;
3102
John McCallfaf5fb42010-08-26 23:41:50 +00003103 case Sema::PCC_Class:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003104 return CodeCompletionContext::CCC_ClassStructUnion;
3105
John McCallfaf5fb42010-08-26 23:41:50 +00003106 case Sema::PCC_ObjCInterface:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003107 return CodeCompletionContext::CCC_ObjCInterface;
3108
John McCallfaf5fb42010-08-26 23:41:50 +00003109 case Sema::PCC_ObjCImplementation:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003110 return CodeCompletionContext::CCC_ObjCImplementation;
3111
John McCallfaf5fb42010-08-26 23:41:50 +00003112 case Sema::PCC_ObjCInstanceVariableList:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003113 return CodeCompletionContext::CCC_ObjCIvarList;
3114
John McCallfaf5fb42010-08-26 23:41:50 +00003115 case Sema::PCC_Template:
3116 case Sema::PCC_MemberTemplate:
Douglas Gregor0ac41382010-09-23 23:01:17 +00003117 if (S.CurContext->isFileContext())
3118 return CodeCompletionContext::CCC_TopLevel;
David Blaikie8a40f702012-01-17 06:56:22 +00003119 if (S.CurContext->isRecord())
Douglas Gregor0ac41382010-09-23 23:01:17 +00003120 return CodeCompletionContext::CCC_ClassStructUnion;
David Blaikie8a40f702012-01-17 06:56:22 +00003121 return CodeCompletionContext::CCC_Other;
Douglas Gregor0ac41382010-09-23 23:01:17 +00003122
John McCallfaf5fb42010-08-26 23:41:50 +00003123 case Sema::PCC_RecoveryInFunction:
Douglas Gregor0ac41382010-09-23 23:01:17 +00003124 return CodeCompletionContext::CCC_Recovery;
Douglas Gregorc769d6e2010-10-18 22:01:46 +00003125
John McCallfaf5fb42010-08-26 23:41:50 +00003126 case Sema::PCC_ForInit:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003127 if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 ||
3128 S.getLangOpts().ObjC1)
Douglas Gregorc769d6e2010-10-18 22:01:46 +00003129 return CodeCompletionContext::CCC_ParenthesizedExpression;
3130 else
3131 return CodeCompletionContext::CCC_Expression;
3132
3133 case Sema::PCC_Expression:
John McCallfaf5fb42010-08-26 23:41:50 +00003134 case Sema::PCC_Condition:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003135 return CodeCompletionContext::CCC_Expression;
3136
John McCallfaf5fb42010-08-26 23:41:50 +00003137 case Sema::PCC_Statement:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003138 return CodeCompletionContext::CCC_Statement;
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003139
John McCallfaf5fb42010-08-26 23:41:50 +00003140 case Sema::PCC_Type:
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003141 return CodeCompletionContext::CCC_Type;
Douglas Gregor5e35d592010-09-14 23:59:36 +00003142
3143 case Sema::PCC_ParenthesizedExpression:
3144 return CodeCompletionContext::CCC_ParenthesizedExpression;
Douglas Gregor80039242011-02-15 20:33:25 +00003145
3146 case Sema::PCC_LocalDeclarationSpecifiers:
3147 return CodeCompletionContext::CCC_Type;
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003148 }
David Blaikie8a40f702012-01-17 06:56:22 +00003149
3150 llvm_unreachable("Invalid ParserCompletionContext!");
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003151}
3152
Douglas Gregorac322ec2010-08-27 21:18:54 +00003153/// \brief If we're in a C++ virtual member function, add completion results
3154/// that invoke the functions we override, since it's common to invoke the
3155/// overridden function as well as adding new functionality.
3156///
3157/// \param S The semantic analysis object for which we are generating results.
3158///
3159/// \param InContext This context in which the nested-name-specifier preceding
3160/// the code-completion point
3161static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext,
3162 ResultBuilder &Results) {
3163 // Look through blocks.
3164 DeclContext *CurContext = S.CurContext;
3165 while (isa<BlockDecl>(CurContext))
3166 CurContext = CurContext->getParent();
3167
3168
3169 CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext);
3170 if (!Method || !Method->isVirtual())
3171 return;
3172
3173 // We need to have names for all of the parameters, if we're going to
3174 // generate a forwarding call.
Aaron Ballman43b68be2014-03-07 17:50:17 +00003175 for (auto P : Method->params())
3176 if (!P->getDeclName())
Douglas Gregorac322ec2010-08-27 21:18:54 +00003177 return;
Douglas Gregorac322ec2010-08-27 21:18:54 +00003178
Douglas Gregor75acd922011-09-27 23:30:47 +00003179 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
Douglas Gregorac322ec2010-08-27 21:18:54 +00003180 for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(),
3181 MEnd = Method->end_overridden_methods();
3182 M != MEnd; ++M) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003183 CodeCompletionBuilder Builder(Results.getAllocator(),
3184 Results.getCodeCompletionTUInfo());
Dmitri Gribenko6cfb1532013-02-14 13:53:30 +00003185 const CXXMethodDecl *Overridden = *M;
Douglas Gregorac322ec2010-08-27 21:18:54 +00003186 if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl())
3187 continue;
3188
3189 // If we need a nested-name-specifier, add one now.
3190 if (!InContext) {
3191 NestedNameSpecifier *NNS
3192 = getRequiredQualification(S.Context, CurContext,
3193 Overridden->getDeclContext());
3194 if (NNS) {
3195 std::string Str;
3196 llvm::raw_string_ostream OS(Str);
Douglas Gregor75acd922011-09-27 23:30:47 +00003197 NNS->print(OS, Policy);
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00003198 Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str()));
Douglas Gregorac322ec2010-08-27 21:18:54 +00003199 }
3200 } else if (!InContext->Equals(Overridden->getDeclContext()))
3201 continue;
3202
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00003203 Builder.AddTypedTextChunk(Results.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003204 Overridden->getNameAsString()));
3205 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregorac322ec2010-08-27 21:18:54 +00003206 bool FirstParam = true;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003207 for (auto P : Method->params()) {
Douglas Gregorac322ec2010-08-27 21:18:54 +00003208 if (FirstParam)
3209 FirstParam = false;
3210 else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003211 Builder.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregorac322ec2010-08-27 21:18:54 +00003212
Aaron Ballman43b68be2014-03-07 17:50:17 +00003213 Builder.AddPlaceholderChunk(
3214 Results.getAllocator().CopyString(P->getIdentifier()->getName()));
Douglas Gregorac322ec2010-08-27 21:18:54 +00003215 }
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003216 Builder.AddChunk(CodeCompletionString::CK_RightParen);
3217 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregorac322ec2010-08-27 21:18:54 +00003218 CCP_SuperCompletion,
Douglas Gregor78254c82012-03-27 23:34:16 +00003219 CXCursor_CXXMethod,
3220 CXAvailability_Available,
3221 Overridden));
Douglas Gregorac322ec2010-08-27 21:18:54 +00003222 Results.Ignore(Overridden);
3223 }
3224}
3225
Douglas Gregor07f43572012-01-29 18:15:03 +00003226void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc,
3227 ModuleIdPath Path) {
3228 typedef CodeCompletionResult Result;
3229 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003230 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor07f43572012-01-29 18:15:03 +00003231 CodeCompletionContext::CCC_Other);
3232 Results.EnterNewScope();
3233
3234 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003235 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregor07f43572012-01-29 18:15:03 +00003236 typedef CodeCompletionResult Result;
3237 if (Path.empty()) {
3238 // Enumerate all top-level modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003239 SmallVector<Module *, 8> Modules;
Douglas Gregor07f43572012-01-29 18:15:03 +00003240 PP.getHeaderSearchInfo().collectAllModules(Modules);
3241 for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
3242 Builder.AddTypedTextChunk(
3243 Builder.getAllocator().CopyString(Modules[I]->Name));
3244 Results.AddResult(Result(Builder.TakeString(),
3245 CCP_Declaration,
Argyrios Kyrtzidis345d05f2013-05-29 18:50:15 +00003246 CXCursor_ModuleImportDecl,
Douglas Gregor07f43572012-01-29 18:15:03 +00003247 Modules[I]->isAvailable()
3248 ? CXAvailability_Available
3249 : CXAvailability_NotAvailable));
3250 }
Daniel Jasper07e6c402013-08-05 20:26:17 +00003251 } else if (getLangOpts().Modules) {
Douglas Gregor07f43572012-01-29 18:15:03 +00003252 // Load the named module.
3253 Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path,
3254 Module::AllVisible,
3255 /*IsInclusionDirective=*/false);
3256 // Enumerate submodules.
3257 if (Mod) {
3258 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
3259 SubEnd = Mod->submodule_end();
3260 Sub != SubEnd; ++Sub) {
3261
3262 Builder.AddTypedTextChunk(
3263 Builder.getAllocator().CopyString((*Sub)->Name));
3264 Results.AddResult(Result(Builder.TakeString(),
3265 CCP_Declaration,
Argyrios Kyrtzidis345d05f2013-05-29 18:50:15 +00003266 CXCursor_ModuleImportDecl,
Douglas Gregor07f43572012-01-29 18:15:03 +00003267 (*Sub)->isAvailable()
3268 ? CXAvailability_Available
3269 : CXAvailability_NotAvailable));
3270 }
3271 }
3272 }
3273 Results.ExitScope();
3274 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3275 Results.data(),Results.size());
3276}
3277
Douglas Gregor504a6ae2010-01-10 23:08:15 +00003278void Sema::CodeCompleteOrdinaryName(Scope *S,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003279 ParserCompletionContext CompletionContext) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003280 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003281 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00003282 mapCodeCompletionContext(*this, CompletionContext));
Douglas Gregorac322ec2010-08-27 21:18:54 +00003283 Results.EnterNewScope();
Douglas Gregor50832e02010-09-20 22:39:41 +00003284
Douglas Gregor504a6ae2010-01-10 23:08:15 +00003285 // Determine how to filter results, e.g., so that the names of
3286 // values (functions, enumerators, function templates, etc.) are
3287 // only allowed where we can have an expression.
3288 switch (CompletionContext) {
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003289 case PCC_Namespace:
3290 case PCC_Class:
3291 case PCC_ObjCInterface:
3292 case PCC_ObjCImplementation:
3293 case PCC_ObjCInstanceVariableList:
3294 case PCC_Template:
3295 case PCC_MemberTemplate:
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003296 case PCC_Type:
Douglas Gregor80039242011-02-15 20:33:25 +00003297 case PCC_LocalDeclarationSpecifiers:
Douglas Gregor504a6ae2010-01-10 23:08:15 +00003298 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
3299 break;
3300
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003301 case PCC_Statement:
Douglas Gregor5e35d592010-09-14 23:59:36 +00003302 case PCC_ParenthesizedExpression:
Douglas Gregor4d755e82010-08-24 23:58:17 +00003303 case PCC_Expression:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003304 case PCC_ForInit:
3305 case PCC_Condition:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003306 if (WantTypesInContext(CompletionContext, getLangOpts()))
Douglas Gregor70febae2010-05-28 00:49:12 +00003307 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3308 else
3309 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
Douglas Gregorac322ec2010-08-27 21:18:54 +00003310
David Blaikiebbafb8a2012-03-11 07:00:24 +00003311 if (getLangOpts().CPlusPlus)
Craig Topperc3ec1492014-05-26 06:22:03 +00003312 MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00003313 break;
Douglas Gregor6da3db42010-05-25 05:58:43 +00003314
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003315 case PCC_RecoveryInFunction:
Douglas Gregor6da3db42010-05-25 05:58:43 +00003316 // Unfiltered
3317 break;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00003318 }
3319
Douglas Gregor9be0ed42010-08-26 16:36:48 +00003320 // If we are in a C++ non-static member function, check the qualifiers on
3321 // the member function to filter/prioritize the results list.
3322 if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext))
3323 if (CurMethod->isInstance())
3324 Results.setObjectTypeQualifiers(
3325 Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers()));
3326
Douglas Gregorc580c522010-01-14 01:09:38 +00003327 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00003328 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3329 CodeCompleter->includeGlobals());
Douglas Gregor92253692009-12-07 09:54:55 +00003330
Douglas Gregorf98e6a22010-01-13 23:51:12 +00003331 AddOrdinaryNameResults(CompletionContext, S, *this, Results);
Douglas Gregor92253692009-12-07 09:54:55 +00003332 Results.ExitScope();
3333
Douglas Gregorce0e8562010-08-23 21:54:33 +00003334 switch (CompletionContext) {
Douglas Gregor5e35d592010-09-14 23:59:36 +00003335 case PCC_ParenthesizedExpression:
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003336 case PCC_Expression:
3337 case PCC_Statement:
3338 case PCC_RecoveryInFunction:
3339 if (S->getFnParent())
David Blaikiebbafb8a2012-03-11 07:00:24 +00003340 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003341 break;
3342
3343 case PCC_Namespace:
3344 case PCC_Class:
3345 case PCC_ObjCInterface:
3346 case PCC_ObjCImplementation:
3347 case PCC_ObjCInstanceVariableList:
3348 case PCC_Template:
3349 case PCC_MemberTemplate:
3350 case PCC_ForInit:
3351 case PCC_Condition:
3352 case PCC_Type:
Douglas Gregor80039242011-02-15 20:33:25 +00003353 case PCC_LocalDeclarationSpecifiers:
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003354 break;
Douglas Gregorce0e8562010-08-23 21:54:33 +00003355 }
3356
Douglas Gregor9eb77012009-11-07 00:00:49 +00003357 if (CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00003358 AddMacroResults(PP, Results, false);
Douglas Gregorce0e8562010-08-23 21:54:33 +00003359
Douglas Gregor50832e02010-09-20 22:39:41 +00003360 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003361 Results.data(),Results.size());
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00003362}
3363
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003364static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
3365 ParsedType Receiver,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003366 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003367 bool AtArgumentExpression,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003368 bool IsSuper,
3369 ResultBuilder &Results);
3370
3371void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS,
3372 bool AllowNonIdentifiers,
3373 bool AllowNestedNameSpecifiers) {
John McCall276321a2010-08-25 06:19:51 +00003374 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003375 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003376 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00003377 AllowNestedNameSpecifiers
3378 ? CodeCompletionContext::CCC_PotentiallyQualifiedName
3379 : CodeCompletionContext::CCC_Name);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003380 Results.EnterNewScope();
3381
3382 // Type qualifiers can come after names.
3383 Results.AddResult(Result("const"));
3384 Results.AddResult(Result("volatile"));
David Blaikiebbafb8a2012-03-11 07:00:24 +00003385 if (getLangOpts().C99)
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003386 Results.AddResult(Result("restrict"));
3387
David Blaikiebbafb8a2012-03-11 07:00:24 +00003388 if (getLangOpts().CPlusPlus) {
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003389 if (AllowNonIdentifiers) {
3390 Results.AddResult(Result("operator"));
3391 }
3392
3393 // Add nested-name-specifiers.
3394 if (AllowNestedNameSpecifiers) {
3395 Results.allowNestedNameSpecifiers();
Douglas Gregor0ac41382010-09-23 23:01:17 +00003396 Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003397 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3398 LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer,
3399 CodeCompleter->includeGlobals());
Craig Topperc3ec1492014-05-26 06:22:03 +00003400 Results.setFilter(nullptr);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003401 }
3402 }
3403 Results.ExitScope();
3404
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003405 // If we're in a context where we might have an expression (rather than a
3406 // declaration), and what we've seen so far is an Objective-C type that could
3407 // be a receiver of a class message, this may be a class message send with
3408 // the initial opening bracket '[' missing. Add appropriate completions.
3409 if (AllowNonIdentifiers && !AllowNestedNameSpecifiers &&
Richard Smithb4a9e862013-04-12 22:46:28 +00003410 DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier &&
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003411 DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003412 DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified &&
3413 DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
Richard Smithb4a9e862013-04-12 22:46:28 +00003414 !DS.isTypeAltiVecVector() &&
3415 S &&
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003416 (S->getFlags() & Scope::DeclScope) != 0 &&
3417 (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope |
3418 Scope::FunctionPrototypeScope |
3419 Scope::AtCatchScope)) == 0) {
3420 ParsedType T = DS.getRepAsType();
3421 if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003422 AddClassMessageCompletions(*this, S, T, None, false, false, Results);
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003423 }
3424
Douglas Gregor56ccce02010-08-24 04:59:56 +00003425 // Note that we intentionally suppress macro results here, since we do not
3426 // encourage using macros to produce the names of entities.
3427
Douglas Gregor0ac41382010-09-23 23:01:17 +00003428 HandleCodeCompleteResults(this, CodeCompleter,
3429 Results.getCompletionContext(),
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003430 Results.data(), Results.size());
3431}
3432
Douglas Gregor68762e72010-08-23 21:17:50 +00003433struct Sema::CodeCompleteExpressionData {
3434 CodeCompleteExpressionData(QualType PreferredType = QualType())
3435 : PreferredType(PreferredType), IntegralConstantExpression(false),
3436 ObjCCollection(false) { }
3437
3438 QualType PreferredType;
3439 bool IntegralConstantExpression;
3440 bool ObjCCollection;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003441 SmallVector<Decl *, 4> IgnoreDecls;
Douglas Gregor68762e72010-08-23 21:17:50 +00003442};
3443
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003444/// \brief Perform code-completion in an expression context when we know what
3445/// type we're looking for.
Douglas Gregor68762e72010-08-23 21:17:50 +00003446void Sema::CodeCompleteExpression(Scope *S,
3447 const CodeCompleteExpressionData &Data) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003448 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003449 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003450 CodeCompletionContext::CCC_Expression);
Douglas Gregor68762e72010-08-23 21:17:50 +00003451 if (Data.ObjCCollection)
3452 Results.setFilter(&ResultBuilder::IsObjCCollection);
3453 else if (Data.IntegralConstantExpression)
Douglas Gregor85b50632010-07-28 21:50:18 +00003454 Results.setFilter(&ResultBuilder::IsIntegralConstantValue);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003455 else if (WantTypesInContext(PCC_Expression, getLangOpts()))
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003456 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3457 else
3458 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
Douglas Gregor68762e72010-08-23 21:17:50 +00003459
3460 if (!Data.PreferredType.isNull())
3461 Results.setPreferredType(Data.PreferredType.getNonReferenceType());
3462
3463 // Ignore any declarations that we were told that we don't care about.
3464 for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I)
3465 Results.Ignore(Data.IgnoreDecls[I]);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003466
3467 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00003468 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3469 CodeCompleter->includeGlobals());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003470
3471 Results.EnterNewScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003472 AddOrdinaryNameResults(PCC_Expression, S, *this, Results);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003473 Results.ExitScope();
3474
Douglas Gregor55b037b2010-07-08 20:55:51 +00003475 bool PreferredTypeIsPointer = false;
Douglas Gregor68762e72010-08-23 21:17:50 +00003476 if (!Data.PreferredType.isNull())
3477 PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType()
3478 || Data.PreferredType->isMemberPointerType()
3479 || Data.PreferredType->isBlockPointerType();
Douglas Gregor55b037b2010-07-08 20:55:51 +00003480
Douglas Gregorce0e8562010-08-23 21:54:33 +00003481 if (S->getFnParent() &&
3482 !Data.ObjCCollection &&
3483 !Data.IntegralConstantExpression)
David Blaikiebbafb8a2012-03-11 07:00:24 +00003484 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregorce0e8562010-08-23 21:54:33 +00003485
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003486 if (CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00003487 AddMacroResults(PP, Results, false, PreferredTypeIsPointer);
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003488 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor68762e72010-08-23 21:17:50 +00003489 CodeCompletionContext(CodeCompletionContext::CCC_Expression,
3490 Data.PreferredType),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003491 Results.data(),Results.size());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003492}
3493
Douglas Gregoreda7e542010-09-18 01:28:11 +00003494void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) {
3495 if (E.isInvalid())
3496 CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003497 else if (getLangOpts().ObjC1)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003498 CodeCompleteObjCInstanceMessage(S, E.get(), None, false);
Douglas Gregored0b69d2010-09-15 16:23:04 +00003499}
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003500
Douglas Gregorb888acf2010-12-09 23:01:55 +00003501/// \brief The set of properties that have already been added, referenced by
3502/// property name.
3503typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet;
3504
Douglas Gregor9b4f3702012-06-12 13:44:08 +00003505/// \brief Retrieve the container definition, if any?
3506static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) {
3507 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
3508 if (Interface->hasDefinition())
3509 return Interface->getDefinition();
3510
3511 return Interface;
3512 }
3513
3514 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3515 if (Protocol->hasDefinition())
3516 return Protocol->getDefinition();
3517
3518 return Protocol;
3519 }
3520 return Container;
3521}
3522
3523static void AddObjCProperties(ObjCContainerDecl *Container,
Douglas Gregor5d649882009-11-18 22:32:06 +00003524 bool AllowCategories,
Douglas Gregor95147142011-05-05 15:50:42 +00003525 bool AllowNullaryMethods,
Douglas Gregor9291bad2009-11-18 01:29:26 +00003526 DeclContext *CurContext,
Douglas Gregorb888acf2010-12-09 23:01:55 +00003527 AddedPropertiesSet &AddedProperties,
Douglas Gregor9291bad2009-11-18 01:29:26 +00003528 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00003529 typedef CodeCompletionResult Result;
Douglas Gregor9291bad2009-11-18 01:29:26 +00003530
Douglas Gregor9b4f3702012-06-12 13:44:08 +00003531 // Retrieve the definition.
3532 Container = getContainerDef(Container);
3533
Douglas Gregor9291bad2009-11-18 01:29:26 +00003534 // Add properties in this container.
Aaron Ballmand174edf2014-03-13 19:11:50 +00003535 for (const auto *P : Container->properties())
David Blaikie82e95a32014-11-19 07:49:47 +00003536 if (AddedProperties.insert(P->getIdentifier()).second)
Craig Topperc3ec1492014-05-26 06:22:03 +00003537 Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr),
Douglas Gregor0a0e2b32013-01-31 04:52:16 +00003538 CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +00003539
Douglas Gregor95147142011-05-05 15:50:42 +00003540 // Add nullary methods
3541 if (AllowNullaryMethods) {
3542 ASTContext &Context = Container->getASTContext();
Douglas Gregor75acd922011-09-27 23:30:47 +00003543 PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
Aaron Ballmanaff18c02014-03-13 19:03:34 +00003544 for (auto *M : Container->methods()) {
Douglas Gregor95147142011-05-05 15:50:42 +00003545 if (M->getSelector().isUnarySelector())
3546 if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0))
David Blaikie82e95a32014-11-19 07:49:47 +00003547 if (AddedProperties.insert(Name).second) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003548 CodeCompletionBuilder Builder(Results.getAllocator(),
3549 Results.getCodeCompletionTUInfo());
Aaron Ballmanaff18c02014-03-13 19:03:34 +00003550 AddResultTypeChunk(Context, Policy, M, Builder);
Douglas Gregor95147142011-05-05 15:50:42 +00003551 Builder.AddTypedTextChunk(
3552 Results.getAllocator().CopyString(Name->getName()));
3553
Aaron Ballmanaff18c02014-03-13 19:03:34 +00003554 Results.MaybeAddResult(Result(Builder.TakeString(), M,
Douglas Gregor78254c82012-03-27 23:34:16 +00003555 CCP_MemberDeclaration + CCD_MethodAsProperty),
Douglas Gregor95147142011-05-05 15:50:42 +00003556 CurContext);
3557 }
3558 }
3559 }
3560
3561
Douglas Gregor9291bad2009-11-18 01:29:26 +00003562 // Add properties in referenced protocols.
3563 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00003564 for (auto *P : Protocol->protocols())
3565 AddObjCProperties(P, AllowCategories, AllowNullaryMethods, CurContext,
Douglas Gregor95147142011-05-05 15:50:42 +00003566 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003567 } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){
Douglas Gregor5d649882009-11-18 22:32:06 +00003568 if (AllowCategories) {
3569 // Look through categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003570 for (auto *Cat : IFace->known_categories())
3571 AddObjCProperties(Cat, AllowCategories, AllowNullaryMethods, CurContext,
3572 AddedProperties, Results);
Douglas Gregor5d649882009-11-18 22:32:06 +00003573 }
Aaron Ballman15063e12014-03-13 21:35:02 +00003574
Douglas Gregor9291bad2009-11-18 01:29:26 +00003575 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00003576 for (auto *I : IFace->all_referenced_protocols())
3577 AddObjCProperties(I, AllowCategories, AllowNullaryMethods, CurContext,
Douglas Gregor95147142011-05-05 15:50:42 +00003578 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003579
3580 // Look in the superclass.
3581 if (IFace->getSuperClass())
Douglas Gregor95147142011-05-05 15:50:42 +00003582 AddObjCProperties(IFace->getSuperClass(), AllowCategories,
3583 AllowNullaryMethods, CurContext,
Douglas Gregorb888acf2010-12-09 23:01:55 +00003584 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003585 } else if (const ObjCCategoryDecl *Category
3586 = dyn_cast<ObjCCategoryDecl>(Container)) {
3587 // Look through protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00003588 for (auto *P : Category->protocols())
3589 AddObjCProperties(P, AllowCategories, AllowNullaryMethods, CurContext,
Douglas Gregor95147142011-05-05 15:50:42 +00003590 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003591 }
3592}
3593
Douglas Gregor1cc88a92012-01-23 15:59:30 +00003594void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base,
Douglas Gregor2436e712009-09-17 21:32:03 +00003595 SourceLocation OpLoc,
3596 bool IsArrow) {
Douglas Gregor1cc88a92012-01-23 15:59:30 +00003597 if (!Base || !CodeCompleter)
Douglas Gregor2436e712009-09-17 21:32:03 +00003598 return;
3599
Douglas Gregor1cc88a92012-01-23 15:59:30 +00003600 ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow);
3601 if (ConvertedBase.isInvalid())
3602 return;
3603 Base = ConvertedBase.get();
3604
John McCall276321a2010-08-25 06:19:51 +00003605 typedef CodeCompletionResult Result;
Douglas Gregor3545ff42009-09-21 16:56:56 +00003606
Douglas Gregor2436e712009-09-17 21:32:03 +00003607 QualType BaseType = Base->getType();
Douglas Gregor3545ff42009-09-21 16:56:56 +00003608
3609 if (IsArrow) {
3610 if (const PointerType *Ptr = BaseType->getAs<PointerType>())
3611 BaseType = Ptr->getPointeeType();
3612 else if (BaseType->isObjCObjectPointerType())
Douglas Gregor9be0ed42010-08-26 16:36:48 +00003613 /*Do nothing*/ ;
Douglas Gregor3545ff42009-09-21 16:56:56 +00003614 else
3615 return;
3616 }
3617
Douglas Gregor21325842011-07-07 16:03:39 +00003618 enum CodeCompletionContext::Kind contextKind;
3619
3620 if (IsArrow) {
3621 contextKind = CodeCompletionContext::CCC_ArrowMemberAccess;
3622 }
3623 else {
3624 if (BaseType->isObjCObjectPointerType() ||
3625 BaseType->isObjCObjectOrInterfaceType()) {
3626 contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess;
3627 }
3628 else {
3629 contextKind = CodeCompletionContext::CCC_DotMemberAccess;
3630 }
3631 }
3632
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003633 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003634 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor21325842011-07-07 16:03:39 +00003635 CodeCompletionContext(contextKind,
Douglas Gregor0ac41382010-09-23 23:01:17 +00003636 BaseType),
3637 &ResultBuilder::IsMember);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003638 Results.EnterNewScope();
3639 if (const RecordType *Record = BaseType->getAs<RecordType>()) {
Douglas Gregor9be0ed42010-08-26 16:36:48 +00003640 // Indicate that we are performing a member access, and the cv-qualifiers
3641 // for the base object type.
3642 Results.setObjectTypeQualifiers(BaseType.getQualifiers());
3643
Douglas Gregor9291bad2009-11-18 01:29:26 +00003644 // Access to a C/C++ class, struct, or union.
Douglas Gregor6ae4c522010-01-14 03:21:49 +00003645 Results.allowNestedNameSpecifiers();
Douglas Gregor09bbc652010-01-14 15:47:35 +00003646 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00003647 LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer,
3648 CodeCompleter->includeGlobals());
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003649
David Blaikiebbafb8a2012-03-11 07:00:24 +00003650 if (getLangOpts().CPlusPlus) {
Douglas Gregor9291bad2009-11-18 01:29:26 +00003651 if (!Results.empty()) {
3652 // The "template" keyword can follow "->" or "." in the grammar.
3653 // However, we only want to suggest the template keyword if something
3654 // is dependent.
3655 bool IsDependent = BaseType->isDependentType();
3656 if (!IsDependent) {
3657 for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent())
Ted Kremenekc37877d2013-10-08 17:08:03 +00003658 if (DeclContext *Ctx = DepScope->getEntity()) {
Douglas Gregor9291bad2009-11-18 01:29:26 +00003659 IsDependent = Ctx->isDependentContext();
3660 break;
3661 }
3662 }
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003663
Douglas Gregor9291bad2009-11-18 01:29:26 +00003664 if (IsDependent)
Douglas Gregor78a21012010-01-14 16:01:26 +00003665 Results.AddResult(Result("template"));
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003666 }
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003667 }
Douglas Gregor9291bad2009-11-18 01:29:26 +00003668 } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) {
3669 // Objective-C property reference.
Douglas Gregorb888acf2010-12-09 23:01:55 +00003670 AddedPropertiesSet AddedProperties;
Douglas Gregor9291bad2009-11-18 01:29:26 +00003671
3672 // Add property results based on our interface.
3673 const ObjCObjectPointerType *ObjCPtr
3674 = BaseType->getAsObjCInterfacePointerType();
3675 assert(ObjCPtr && "Non-NULL pointer guaranteed above!");
Douglas Gregor95147142011-05-05 15:50:42 +00003676 AddObjCProperties(ObjCPtr->getInterfaceDecl(), true,
3677 /*AllowNullaryMethods=*/true, CurContext,
Douglas Gregorb888acf2010-12-09 23:01:55 +00003678 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003679
3680 // Add properties from the protocols in a qualified interface.
Aaron Ballman83731462014-03-17 16:14:00 +00003681 for (auto *I : ObjCPtr->quals())
3682 AddObjCProperties(I, true, /*AllowNullaryMethods=*/true, CurContext,
Douglas Gregor95147142011-05-05 15:50:42 +00003683 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003684 } else if ((IsArrow && BaseType->isObjCObjectPointerType()) ||
John McCall8b07ec22010-05-15 11:32:37 +00003685 (!IsArrow && BaseType->isObjCObjectType())) {
Douglas Gregor9291bad2009-11-18 01:29:26 +00003686 // Objective-C instance variable access.
Craig Topperc3ec1492014-05-26 06:22:03 +00003687 ObjCInterfaceDecl *Class = nullptr;
Douglas Gregor9291bad2009-11-18 01:29:26 +00003688 if (const ObjCObjectPointerType *ObjCPtr
3689 = BaseType->getAs<ObjCObjectPointerType>())
3690 Class = ObjCPtr->getInterfaceDecl();
3691 else
John McCall8b07ec22010-05-15 11:32:37 +00003692 Class = BaseType->getAs<ObjCObjectType>()->getInterface();
Douglas Gregor9291bad2009-11-18 01:29:26 +00003693
3694 // Add all ivars from this class and its superclasses.
Douglas Gregor2b8162b2010-01-14 16:08:12 +00003695 if (Class) {
3696 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3697 Results.setFilter(&ResultBuilder::IsObjCIvar);
Douglas Gregor39982192010-08-15 06:18:01 +00003698 LookupVisibleDecls(Class, LookupMemberName, Consumer,
3699 CodeCompleter->includeGlobals());
Douglas Gregor9291bad2009-11-18 01:29:26 +00003700 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00003701 }
Douglas Gregor9291bad2009-11-18 01:29:26 +00003702
3703 // FIXME: How do we cope with isa?
3704
3705 Results.ExitScope();
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003706
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003707 // Hand off the results found for code completion.
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003708 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0ac41382010-09-23 23:01:17 +00003709 Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003710 Results.data(),Results.size());
Douglas Gregor2436e712009-09-17 21:32:03 +00003711}
3712
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003713void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) {
3714 if (!CodeCompleter)
3715 return;
Craig Topperc3ec1492014-05-26 06:22:03 +00003716
3717 ResultBuilder::LookupFilter Filter = nullptr;
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003718 enum CodeCompletionContext::Kind ContextKind
3719 = CodeCompletionContext::CCC_Other;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003720 switch ((DeclSpec::TST)TagSpec) {
3721 case DeclSpec::TST_enum:
Douglas Gregor3545ff42009-09-21 16:56:56 +00003722 Filter = &ResultBuilder::IsEnum;
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003723 ContextKind = CodeCompletionContext::CCC_EnumTag;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003724 break;
3725
3726 case DeclSpec::TST_union:
Douglas Gregor3545ff42009-09-21 16:56:56 +00003727 Filter = &ResultBuilder::IsUnion;
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003728 ContextKind = CodeCompletionContext::CCC_UnionTag;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003729 break;
3730
3731 case DeclSpec::TST_struct:
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003732 case DeclSpec::TST_class:
Joao Matosdc86f942012-08-31 18:45:21 +00003733 case DeclSpec::TST_interface:
Douglas Gregor3545ff42009-09-21 16:56:56 +00003734 Filter = &ResultBuilder::IsClassOrStruct;
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003735 ContextKind = CodeCompletionContext::CCC_ClassOrStructTag;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003736 break;
3737
3738 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003739 llvm_unreachable("Unknown type specifier kind in CodeCompleteTag");
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003740 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00003741
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003742 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3743 CodeCompleter->getCodeCompletionTUInfo(), ContextKind);
Douglas Gregora6e2edc2010-01-14 03:27:13 +00003744 CodeCompletionDeclConsumer Consumer(Results, CurContext);
John McCalle87beb22010-04-23 18:46:30 +00003745
3746 // First pass: look for tags.
3747 Results.setFilter(Filter);
Douglas Gregor39982192010-08-15 06:18:01 +00003748 LookupVisibleDecls(S, LookupTagName, Consumer,
3749 CodeCompleter->includeGlobals());
John McCalle87beb22010-04-23 18:46:30 +00003750
Douglas Gregor39982192010-08-15 06:18:01 +00003751 if (CodeCompleter->includeGlobals()) {
3752 // Second pass: look for nested name specifiers.
3753 Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
3754 LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer);
3755 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00003756
Douglas Gregor0ac41382010-09-23 23:01:17 +00003757 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003758 Results.data(),Results.size());
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003759}
3760
Douglas Gregor28c78432010-08-27 17:35:51 +00003761void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003762 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003763 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003764 CodeCompletionContext::CCC_TypeQualifiers);
Douglas Gregor28c78432010-08-27 17:35:51 +00003765 Results.EnterNewScope();
3766 if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const))
3767 Results.AddResult("const");
3768 if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile))
3769 Results.AddResult("volatile");
David Blaikiebbafb8a2012-03-11 07:00:24 +00003770 if (getLangOpts().C99 &&
Douglas Gregor28c78432010-08-27 17:35:51 +00003771 !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict))
3772 Results.AddResult("restrict");
Richard Smith8e1ac332013-03-28 01:55:44 +00003773 if (getLangOpts().C11 &&
3774 !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic))
3775 Results.AddResult("_Atomic");
Douglas Gregor28c78432010-08-27 17:35:51 +00003776 Results.ExitScope();
3777 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0ac41382010-09-23 23:01:17 +00003778 Results.getCompletionContext(),
Douglas Gregor28c78432010-08-27 17:35:51 +00003779 Results.data(), Results.size());
3780}
3781
Douglas Gregord328d572009-09-21 18:10:23 +00003782void Sema::CodeCompleteCase(Scope *S) {
John McCallaab3e412010-08-25 08:40:02 +00003783 if (getCurFunction()->SwitchStack.empty() || !CodeCompleter)
Douglas Gregord328d572009-09-21 18:10:23 +00003784 return;
John McCall5939b162011-08-06 07:30:58 +00003785
John McCallaab3e412010-08-25 08:40:02 +00003786 SwitchStmt *Switch = getCurFunction()->SwitchStack.back();
John McCall5939b162011-08-06 07:30:58 +00003787 QualType type = Switch->getCond()->IgnoreImplicit()->getType();
3788 if (!type->isEnumeralType()) {
3789 CodeCompleteExpressionData Data(type);
Douglas Gregor68762e72010-08-23 21:17:50 +00003790 Data.IntegralConstantExpression = true;
3791 CodeCompleteExpression(S, Data);
Douglas Gregord328d572009-09-21 18:10:23 +00003792 return;
Douglas Gregor85b50632010-07-28 21:50:18 +00003793 }
Douglas Gregord328d572009-09-21 18:10:23 +00003794
3795 // Code-complete the cases of a switch statement over an enumeration type
3796 // by providing the list of
John McCall5939b162011-08-06 07:30:58 +00003797 EnumDecl *Enum = type->castAs<EnumType>()->getDecl();
Douglas Gregor9b4f3702012-06-12 13:44:08 +00003798 if (EnumDecl *Def = Enum->getDefinition())
3799 Enum = Def;
Douglas Gregord328d572009-09-21 18:10:23 +00003800
3801 // Determine which enumerators we have already seen in the switch statement.
3802 // FIXME: Ideally, we would also be able to look *past* the code-completion
3803 // token, in case we are code-completing in the middle of the switch and not
3804 // at the end. However, we aren't able to do so at the moment.
3805 llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen;
Craig Topperc3ec1492014-05-26 06:22:03 +00003806 NestedNameSpecifier *Qualifier = nullptr;
Douglas Gregord328d572009-09-21 18:10:23 +00003807 for (SwitchCase *SC = Switch->getSwitchCaseList(); SC;
3808 SC = SC->getNextSwitchCase()) {
3809 CaseStmt *Case = dyn_cast<CaseStmt>(SC);
3810 if (!Case)
3811 continue;
3812
3813 Expr *CaseVal = Case->getLHS()->IgnoreParenCasts();
3814 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal))
3815 if (EnumConstantDecl *Enumerator
3816 = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
3817 // We look into the AST of the case statement to determine which
3818 // enumerator was named. Alternatively, we could compute the value of
3819 // the integral constant expression, then compare it against the
3820 // values of each enumerator. However, value-based approach would not
3821 // work as well with C++ templates where enumerators declared within a
3822 // template are type- and value-dependent.
3823 EnumeratorsSeen.insert(Enumerator);
3824
Douglas Gregorf2510672009-09-21 19:57:38 +00003825 // If this is a qualified-id, keep track of the nested-name-specifier
3826 // so that we can reproduce it as part of code completion, e.g.,
Douglas Gregord328d572009-09-21 18:10:23 +00003827 //
3828 // switch (TagD.getKind()) {
3829 // case TagDecl::TK_enum:
3830 // break;
3831 // case XXX
3832 //
Douglas Gregorf2510672009-09-21 19:57:38 +00003833 // At the XXX, our completions are TagDecl::TK_union,
Douglas Gregord328d572009-09-21 18:10:23 +00003834 // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union,
3835 // TK_struct, and TK_class.
Douglas Gregor4bd90e52009-10-23 18:54:35 +00003836 Qualifier = DRE->getQualifier();
Douglas Gregord328d572009-09-21 18:10:23 +00003837 }
3838 }
3839
David Blaikiebbafb8a2012-03-11 07:00:24 +00003840 if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) {
Douglas Gregorf2510672009-09-21 19:57:38 +00003841 // If there are no prior enumerators in C++, check whether we have to
3842 // qualify the names of the enumerators that we suggest, because they
3843 // may not be visible in this scope.
Douglas Gregord3cebdb2012-02-01 05:02:47 +00003844 Qualifier = getRequiredQualification(Context, CurContext, Enum);
Douglas Gregorf2510672009-09-21 19:57:38 +00003845 }
3846
Douglas Gregord328d572009-09-21 18:10:23 +00003847 // Add any enumerators that have not yet been mentioned.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003848 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003849 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003850 CodeCompletionContext::CCC_Expression);
Douglas Gregord328d572009-09-21 18:10:23 +00003851 Results.EnterNewScope();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00003852 for (auto *E : Enum->enumerators()) {
3853 if (EnumeratorsSeen.count(E))
Douglas Gregord328d572009-09-21 18:10:23 +00003854 continue;
3855
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00003856 CodeCompletionResult R(E, CCP_EnumInCase, Qualifier);
Craig Topperc3ec1492014-05-26 06:22:03 +00003857 Results.AddResult(R, CurContext, nullptr, false);
Douglas Gregord328d572009-09-21 18:10:23 +00003858 }
3859 Results.ExitScope();
Douglas Gregor285560922010-04-06 20:02:15 +00003860
Douglas Gregor21325842011-07-07 16:03:39 +00003861 //We need to make sure we're setting the right context,
3862 //so only say we include macros if the code completer says we do
3863 enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other;
3864 if (CodeCompleter->includeMacros()) {
Douglas Gregor8cb17462012-10-09 16:01:50 +00003865 AddMacroResults(PP, Results, false);
Douglas Gregor21325842011-07-07 16:03:39 +00003866 kind = CodeCompletionContext::CCC_OtherWithMacros;
3867 }
3868
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003869 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor21325842011-07-07 16:03:39 +00003870 kind,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003871 Results.data(),Results.size());
Douglas Gregord328d572009-09-21 18:10:23 +00003872}
3873
Robert Wilhelm16e94b92013-08-09 18:02:13 +00003874static bool anyNullArguments(ArrayRef<Expr *> Args) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003875 if (Args.size() && !Args.data())
Douglas Gregor6ed3eb82010-05-30 06:10:08 +00003876 return true;
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003877
3878 for (unsigned I = 0; I != Args.size(); ++I)
Douglas Gregor6ed3eb82010-05-30 06:10:08 +00003879 if (!Args[I])
3880 return true;
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003881
Douglas Gregor6ed3eb82010-05-30 06:10:08 +00003882 return false;
3883}
3884
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003885typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate;
3886
Francisco Lopes da Silva8cafefa2015-01-29 05:54:59 +00003887static void mergeCandidatesWithResults(Sema &SemaRef,
3888 SmallVectorImpl<ResultCandidate> &Results,
3889 OverloadCandidateSet &CandidateSet,
3890 SourceLocation Loc) {
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003891 if (!CandidateSet.empty()) {
3892 // Sort the overload candidate set by placing the best overloads first.
3893 std::stable_sort(
3894 CandidateSet.begin(), CandidateSet.end(),
3895 [&](const OverloadCandidate &X, const OverloadCandidate &Y) {
3896 return isBetterOverloadCandidate(SemaRef, X, Y, Loc);
3897 });
3898
3899 // Add the remaining viable overload candidates as code-completion results.
3900 for (auto &Candidate : CandidateSet)
3901 if (Candidate.Viable)
3902 Results.push_back(ResultCandidate(Candidate.Function));
3903 }
3904}
3905
3906/// \brief Get the type of the Nth parameter from a given set of overload
3907/// candidates.
Francisco Lopes da Silva8cafefa2015-01-29 05:54:59 +00003908static QualType getParamType(Sema &SemaRef,
3909 ArrayRef<ResultCandidate> Candidates,
3910 unsigned N) {
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003911
3912 // Given the overloads 'Candidates' for a function call matching all arguments
3913 // up to N, return the type of the Nth parameter if it is the same for all
3914 // overload candidates.
3915 QualType ParamType;
3916 for (auto &Candidate : Candidates) {
3917 if (auto FType = Candidate.getFunctionType())
3918 if (auto Proto = dyn_cast<FunctionProtoType>(FType))
3919 if (N < Proto->getNumParams()) {
3920 if (ParamType.isNull())
3921 ParamType = Proto->getParamType(N);
3922 else if (!SemaRef.Context.hasSameUnqualifiedType(
3923 ParamType.getNonReferenceType(),
3924 Proto->getParamType(N).getNonReferenceType()))
3925 // Otherwise return a default-constructed QualType.
3926 return QualType();
3927 }
3928 }
3929
3930 return ParamType;
3931}
3932
Francisco Lopes da Silva8cafefa2015-01-29 05:54:59 +00003933static void CodeCompleteOverloadResults(Sema &SemaRef, Scope *S,
3934 MutableArrayRef<ResultCandidate> Candidates,
3935 unsigned CurrentArg,
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003936 bool CompleteExpressionWithCurrentArg = true) {
3937 QualType ParamType;
3938 if (CompleteExpressionWithCurrentArg)
3939 ParamType = getParamType(SemaRef, Candidates, CurrentArg);
3940
3941 if (ParamType.isNull())
3942 SemaRef.CodeCompleteOrdinaryName(S, Sema::PCC_Expression);
3943 else
3944 SemaRef.CodeCompleteExpression(S, ParamType);
3945
3946 if (!Candidates.empty())
3947 SemaRef.CodeCompleter->ProcessOverloadCandidates(SemaRef, CurrentArg,
3948 Candidates.data(),
3949 Candidates.size());
3950}
3951
3952void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args) {
Douglas Gregorcabea402009-09-22 15:41:20 +00003953 if (!CodeCompleter)
3954 return;
Douglas Gregor3ef59522009-12-11 19:06:04 +00003955
3956 // When we're code-completing for a call, we fall back to ordinary
3957 // name code-completion whenever we can't produce specific
3958 // results. We may want to revisit this strategy in the future,
3959 // e.g., by merging the two kinds of results.
3960
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003961 // FIXME: Provide support for variadic template functions.
Douglas Gregor3ef59522009-12-11 19:06:04 +00003962
Douglas Gregorcabea402009-09-22 15:41:20 +00003963 // Ignore type-dependent call expressions entirely.
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003964 if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) ||
3965 Expr::hasAnyTypeDependentArguments(Args)) {
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003966 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregorcabea402009-09-22 15:41:20 +00003967 return;
Douglas Gregor3ef59522009-12-11 19:06:04 +00003968 }
Douglas Gregorcabea402009-09-22 15:41:20 +00003969
John McCall57500772009-12-16 12:17:52 +00003970 // Build an overload candidate set based on the functions we find.
John McCallbc077cf2010-02-08 23:07:23 +00003971 SourceLocation Loc = Fn->getExprLoc();
Richard Smith100b24a2014-04-17 01:52:14 +00003972 OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal);
John McCall57500772009-12-16 12:17:52 +00003973
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003974 SmallVector<ResultCandidate, 8> Results;
Douglas Gregorff59f672010-01-21 15:46:19 +00003975
John McCall57500772009-12-16 12:17:52 +00003976 Expr *NakedFn = Fn->IgnoreParenCasts();
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003977 if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn))
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003978 AddOverloadedCallCandidates(ULE, Args, CandidateSet,
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003979 /*PartialOverloading=*/true);
3980 else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) {
3981 TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr;
3982 if (UME->hasExplicitTemplateArgs()) {
3983 UME->copyTemplateArgumentsInto(TemplateArgsBuffer);
3984 TemplateArgs = &TemplateArgsBuffer;
Douglas Gregorff59f672010-01-21 15:46:19 +00003985 }
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003986 SmallVector<Expr *, 12> ArgExprs(1, UME->getBase());
3987 ArgExprs.append(Args.begin(), Args.end());
3988 UnresolvedSet<8> Decls;
3989 Decls.append(UME->decls_begin(), UME->decls_end());
3990 AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs,
3991 /*SuppressUsedConversions=*/false,
3992 /*PartialOverloading=*/true);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003993 } else {
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003994 FunctionDecl *FD = nullptr;
3995 if (auto MCE = dyn_cast<MemberExpr>(NakedFn))
3996 FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl());
3997 else if (auto DRE = dyn_cast<DeclRefExpr>(NakedFn))
3998 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
Francisco Lopes da Silvac6ccc4f2015-01-22 21:14:08 +00003999 if (FD) { // We check whether it's a resolved function declaration.
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00004000 if (!getLangOpts().CPlusPlus ||
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00004001 !FD->getType()->getAs<FunctionProtoType>())
4002 Results.push_back(ResultCandidate(FD));
4003 else
4004 AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()),
4005 Args, CandidateSet,
4006 /*SuppressUsedConversions=*/false,
4007 /*PartialOverloading=*/true);
Francisco Lopes da Silvac6ccc4f2015-01-22 21:14:08 +00004008
4009 } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) {
4010 // If expression's type is CXXRecordDecl, it may overload the function
4011 // call operator, so we check if it does and add them as candidates.
Francisco Lopes da Silvaa349a8a2015-01-25 08:47:59 +00004012 // A complete type is needed to lookup for member function call operators.
Francisco Lopes da Silva1a4f8552015-01-25 17:00:47 +00004013 if (!RequireCompleteType(Loc, NakedFn->getType(), 0)) {
Francisco Lopes da Silvaa349a8a2015-01-25 08:47:59 +00004014 DeclarationName OpName = Context.DeclarationNames
4015 .getCXXOperatorName(OO_Call);
4016 LookupResult R(*this, OpName, Loc, LookupOrdinaryName);
4017 LookupQualifiedName(R, DC);
4018 R.suppressDiagnostics();
4019 SmallVector<Expr *, 12> ArgExprs(1, NakedFn);
4020 ArgExprs.append(Args.begin(), Args.end());
4021 AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet,
4022 /*ExplicitArgs=*/nullptr,
4023 /*SuppressUsedConversions=*/false,
4024 /*PartialOverloading=*/true);
4025 }
Francisco Lopes da Silvac6ccc4f2015-01-22 21:14:08 +00004026 } else {
4027 // Lastly we check whether expression's type is function pointer or
4028 // function.
4029 QualType T = NakedFn->getType();
4030 if (!T->getPointeeType().isNull())
4031 T = T->getPointeeType();
4032
4033 if (auto FP = T->getAs<FunctionProtoType>()) {
4034 if (!TooManyArguments(FP->getNumParams(), Args.size(),
Francisco Lopes da Silva62a9a4f2015-01-23 13:17:51 +00004035 /*PartialOverloading=*/true) ||
4036 FP->isVariadic())
Francisco Lopes da Silvac6ccc4f2015-01-22 21:14:08 +00004037 Results.push_back(ResultCandidate(FP));
4038 } else if (auto FT = T->getAs<FunctionType>())
Francisco Lopes da Silva62a9a4f2015-01-23 13:17:51 +00004039 // No prototype and declaration, it may be a K & R style function.
Francisco Lopes da Silvac6ccc4f2015-01-22 21:14:08 +00004040 Results.push_back(ResultCandidate(FT));
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004041 }
Douglas Gregorcabea402009-09-22 15:41:20 +00004042 }
Douglas Gregor3ef59522009-12-11 19:06:04 +00004043
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00004044 mergeCandidatesWithResults(*this, Results, CandidateSet, Loc);
4045 CodeCompleteOverloadResults(*this, S, Results, Args.size(),
4046 !CandidateSet.empty());
4047}
4048
4049void Sema::CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc,
4050 ArrayRef<Expr *> Args) {
4051 if (!CodeCompleter)
4052 return;
4053
4054 // A complete type is needed to lookup for constructors.
4055 if (RequireCompleteType(Loc, Type, 0))
4056 return;
4057
Argyrios Kyrtzidisee1d76f2015-03-13 07:39:30 +00004058 CXXRecordDecl *RD = Type->getAsCXXRecordDecl();
4059 if (!RD) {
4060 CodeCompleteExpression(S, Type);
4061 return;
4062 }
4063
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00004064 // FIXME: Provide support for member initializers.
4065 // FIXME: Provide support for variadic template constructors.
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00004066
4067 OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal);
4068
Argyrios Kyrtzidisee1d76f2015-03-13 07:39:30 +00004069 for (auto C : LookupConstructors(RD)) {
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00004070 if (auto FD = dyn_cast<FunctionDecl>(C)) {
4071 AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()),
4072 Args, CandidateSet,
4073 /*SuppressUsedConversions=*/false,
4074 /*PartialOverloading=*/true);
4075 } else if (auto FTD = dyn_cast<FunctionTemplateDecl>(C)) {
4076 AddTemplateOverloadCandidate(FTD,
4077 DeclAccessPair::make(FTD, C->getAccess()),
4078 /*ExplicitTemplateArgs=*/nullptr,
4079 Args, CandidateSet,
4080 /*SuppressUsedConversions=*/false,
4081 /*PartialOverloading=*/true);
4082 }
4083 }
4084
4085 SmallVector<ResultCandidate, 8> Results;
4086 mergeCandidatesWithResults(*this, Results, CandidateSet, Loc);
4087 CodeCompleteOverloadResults(*this, S, Results, Args.size());
Douglas Gregorcabea402009-09-22 15:41:20 +00004088}
4089
John McCall48871652010-08-21 09:40:31 +00004090void Sema::CodeCompleteInitializer(Scope *S, Decl *D) {
4091 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004092 if (!VD) {
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004093 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004094 return;
4095 }
4096
4097 CodeCompleteExpression(S, VD->getType());
4098}
4099
4100void Sema::CodeCompleteReturn(Scope *S) {
4101 QualType ResultType;
4102 if (isa<BlockDecl>(CurContext)) {
4103 if (BlockScopeInfo *BSI = getCurBlock())
4104 ResultType = BSI->ReturnType;
4105 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext))
Alp Toker314cc812014-01-25 16:55:45 +00004106 ResultType = Function->getReturnType();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004107 else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext))
Alp Toker314cc812014-01-25 16:55:45 +00004108 ResultType = Method->getReturnType();
4109
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004110 if (ResultType.isNull())
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004111 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004112 else
4113 CodeCompleteExpression(S, ResultType);
4114}
4115
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004116void Sema::CodeCompleteAfterIf(Scope *S) {
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004117 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004118 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004119 mapCodeCompletionContext(*this, PCC_Statement));
4120 Results.setFilter(&ResultBuilder::IsOrdinaryName);
4121 Results.EnterNewScope();
4122
4123 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4124 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4125 CodeCompleter->includeGlobals());
4126
4127 AddOrdinaryNameResults(PCC_Statement, S, *this, Results);
4128
4129 // "else" block
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004130 CodeCompletionBuilder Builder(Results.getAllocator(),
4131 Results.getCodeCompletionTUInfo());
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004132 Builder.AddTypedTextChunk("else");
Douglas Gregor3a5d6c22012-02-16 17:49:04 +00004133 if (Results.includeCodePatterns()) {
4134 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4135 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4136 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
4137 Builder.AddPlaceholderChunk("statements");
4138 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
4139 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4140 }
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004141 Results.AddResult(Builder.TakeString());
4142
4143 // "else if" block
4144 Builder.AddTypedTextChunk("else");
4145 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4146 Builder.AddTextChunk("if");
4147 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4148 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikiebbafb8a2012-03-11 07:00:24 +00004149 if (getLangOpts().CPlusPlus)
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004150 Builder.AddPlaceholderChunk("condition");
4151 else
4152 Builder.AddPlaceholderChunk("expression");
4153 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor3a5d6c22012-02-16 17:49:04 +00004154 if (Results.includeCodePatterns()) {
4155 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4156 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4157 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
4158 Builder.AddPlaceholderChunk("statements");
4159 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
4160 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4161 }
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004162 Results.AddResult(Builder.TakeString());
4163
4164 Results.ExitScope();
4165
4166 if (S->getFnParent())
David Blaikiebbafb8a2012-03-11 07:00:24 +00004167 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004168
4169 if (CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00004170 AddMacroResults(PP, Results, false);
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004171
4172 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4173 Results.data(),Results.size());
4174}
4175
Richard Trieu2bd04012011-09-09 02:00:50 +00004176void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004177 if (LHS)
4178 CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType());
4179 else
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004180 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004181}
4182
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004183void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS,
Douglas Gregor2436e712009-09-17 21:32:03 +00004184 bool EnteringContext) {
4185 if (!SS.getScopeRep() || !CodeCompleter)
4186 return;
4187
Douglas Gregor3545ff42009-09-21 16:56:56 +00004188 DeclContext *Ctx = computeDeclContext(SS, EnteringContext);
4189 if (!Ctx)
4190 return;
Douglas Gregor800f2f02009-12-11 18:28:39 +00004191
4192 // Try to instantiate any non-dependent declaration contexts before
4193 // we look in them.
John McCall0b66eb32010-05-01 00:40:08 +00004194 if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx))
Douglas Gregor800f2f02009-12-11 18:28:39 +00004195 return;
4196
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004197 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004198 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004199 CodeCompletionContext::CCC_Name);
Douglas Gregorac322ec2010-08-27 21:18:54 +00004200 Results.EnterNewScope();
Douglas Gregor0ac41382010-09-23 23:01:17 +00004201
Douglas Gregor3545ff42009-09-21 16:56:56 +00004202 // The "template" keyword can follow "::" in the grammar, but only
4203 // put it into the grammar if the nested-name-specifier is dependent.
Aaron Ballman4a979672014-01-03 13:56:08 +00004204 NestedNameSpecifier *NNS = SS.getScopeRep();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004205 if (!Results.empty() && NNS->isDependent())
Douglas Gregor78a21012010-01-14 16:01:26 +00004206 Results.AddResult("template");
Douglas Gregorac322ec2010-08-27 21:18:54 +00004207
4208 // Add calls to overridden virtual functions, if there are any.
4209 //
4210 // FIXME: This isn't wonderful, because we don't know whether we're actually
4211 // in a context that permits expressions. This is a general issue with
4212 // qualified-id completions.
4213 if (!EnteringContext)
4214 MaybeAddOverrideCalls(*this, Ctx, Results);
4215 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004216
Douglas Gregorac322ec2010-08-27 21:18:54 +00004217 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4218 LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer);
4219
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004220 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregorc1679ec2011-07-25 17:48:11 +00004221 Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004222 Results.data(),Results.size());
Douglas Gregor2436e712009-09-17 21:32:03 +00004223}
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004224
4225void Sema::CodeCompleteUsing(Scope *S) {
4226 if (!CodeCompleter)
4227 return;
4228
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004229 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004230 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00004231 CodeCompletionContext::CCC_PotentiallyQualifiedName,
4232 &ResultBuilder::IsNestedNameSpecifier);
Douglas Gregor64b12b52009-09-22 23:31:26 +00004233 Results.EnterNewScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004234
4235 // If we aren't in class scope, we could see the "namespace" keyword.
4236 if (!S->isClassScope())
John McCall276321a2010-08-25 06:19:51 +00004237 Results.AddResult(CodeCompletionResult("namespace"));
Douglas Gregor3545ff42009-09-21 16:56:56 +00004238
4239 // After "using", we can see anything that would start a
4240 // nested-name-specifier.
Douglas Gregora6e2edc2010-01-14 03:27:13 +00004241 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00004242 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4243 CodeCompleter->includeGlobals());
Douglas Gregor64b12b52009-09-22 23:31:26 +00004244 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004245
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004246 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004247 CodeCompletionContext::CCC_PotentiallyQualifiedName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004248 Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004249}
4250
4251void Sema::CodeCompleteUsingDirective(Scope *S) {
4252 if (!CodeCompleter)
4253 return;
4254
Douglas Gregor3545ff42009-09-21 16:56:56 +00004255 // After "using namespace", we expect to see a namespace name or namespace
4256 // alias.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004257 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004258 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004259 CodeCompletionContext::CCC_Namespace,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004260 &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregor64b12b52009-09-22 23:31:26 +00004261 Results.EnterNewScope();
Douglas Gregora6e2edc2010-01-14 03:27:13 +00004262 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00004263 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4264 CodeCompleter->includeGlobals());
Douglas Gregor64b12b52009-09-22 23:31:26 +00004265 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004266 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor39982192010-08-15 06:18:01 +00004267 CodeCompletionContext::CCC_Namespace,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004268 Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004269}
4270
4271void Sema::CodeCompleteNamespaceDecl(Scope *S) {
4272 if (!CodeCompleter)
4273 return;
4274
Ted Kremenekc37877d2013-10-08 17:08:03 +00004275 DeclContext *Ctx = S->getEntity();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004276 if (!S->getParent())
4277 Ctx = Context.getTranslationUnitDecl();
4278
Douglas Gregor0ac41382010-09-23 23:01:17 +00004279 bool SuppressedGlobalResults
4280 = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx);
4281
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004282 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004283 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00004284 SuppressedGlobalResults
4285 ? CodeCompletionContext::CCC_Namespace
4286 : CodeCompletionContext::CCC_Other,
4287 &ResultBuilder::IsNamespace);
4288
4289 if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00004290 // We only want to see those namespaces that have already been defined
4291 // within this scope, because its likely that the user is creating an
4292 // extended namespace declaration. Keep track of the most recent
4293 // definition of each namespace.
4294 std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest;
4295 for (DeclContext::specific_decl_iterator<NamespaceDecl>
4296 NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end());
4297 NS != NSEnd; ++NS)
David Blaikie40ed2972012-06-06 20:45:41 +00004298 OrigToLatest[NS->getOriginalNamespace()] = *NS;
Douglas Gregor3545ff42009-09-21 16:56:56 +00004299
4300 // Add the most recent definition (or extended definition) of each
4301 // namespace to the list of results.
Douglas Gregor64b12b52009-09-22 23:31:26 +00004302 Results.EnterNewScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004303 for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator
Douglas Gregor78254c82012-03-27 23:34:16 +00004304 NS = OrigToLatest.begin(),
4305 NSEnd = OrigToLatest.end();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004306 NS != NSEnd; ++NS)
Douglas Gregor0a0e2b32013-01-31 04:52:16 +00004307 Results.AddResult(CodeCompletionResult(
Craig Topperc3ec1492014-05-26 06:22:03 +00004308 NS->second, Results.getBasePriority(NS->second),
4309 nullptr),
4310 CurContext, nullptr, false);
Douglas Gregor64b12b52009-09-22 23:31:26 +00004311 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004312 }
4313
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004314 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004315 Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004316 Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004317}
4318
4319void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) {
4320 if (!CodeCompleter)
4321 return;
4322
Douglas Gregor3545ff42009-09-21 16:56:56 +00004323 // After "namespace", we expect to see a namespace or alias.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004324 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004325 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004326 CodeCompletionContext::CCC_Namespace,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004327 &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregora6e2edc2010-01-14 03:27:13 +00004328 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00004329 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4330 CodeCompleter->includeGlobals());
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004331 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004332 Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004333 Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004334}
4335
Douglas Gregorc811ede2009-09-18 20:05:18 +00004336void Sema::CodeCompleteOperatorName(Scope *S) {
4337 if (!CodeCompleter)
4338 return;
Douglas Gregor3545ff42009-09-21 16:56:56 +00004339
John McCall276321a2010-08-25 06:19:51 +00004340 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004341 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004342 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004343 CodeCompletionContext::CCC_Type,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004344 &ResultBuilder::IsType);
Douglas Gregor64b12b52009-09-22 23:31:26 +00004345 Results.EnterNewScope();
Douglas Gregorc811ede2009-09-18 20:05:18 +00004346
Douglas Gregor3545ff42009-09-21 16:56:56 +00004347 // Add the names of overloadable operators.
4348#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
4349 if (std::strcmp(Spelling, "?")) \
Douglas Gregor78a21012010-01-14 16:01:26 +00004350 Results.AddResult(Result(Spelling));
Douglas Gregor3545ff42009-09-21 16:56:56 +00004351#include "clang/Basic/OperatorKinds.def"
4352
4353 // Add any type names visible from the current scope
Douglas Gregor6ae4c522010-01-14 03:21:49 +00004354 Results.allowNestedNameSpecifiers();
Douglas Gregora6e2edc2010-01-14 03:27:13 +00004355 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00004356 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4357 CodeCompleter->includeGlobals());
Douglas Gregor3545ff42009-09-21 16:56:56 +00004358
4359 // Add any type specifiers
David Blaikiebbafb8a2012-03-11 07:00:24 +00004360 AddTypeSpecifierResults(getLangOpts(), Results);
Douglas Gregor64b12b52009-09-22 23:31:26 +00004361 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004362
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004363 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor39982192010-08-15 06:18:01 +00004364 CodeCompletionContext::CCC_Type,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004365 Results.data(),Results.size());
Douglas Gregorc811ede2009-09-18 20:05:18 +00004366}
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004367
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004368void Sema::CodeCompleteConstructorInitializer(
4369 Decl *ConstructorD,
4370 ArrayRef <CXXCtorInitializer *> Initializers) {
Douglas Gregor75acd922011-09-27 23:30:47 +00004371 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004372 CXXConstructorDecl *Constructor
4373 = static_cast<CXXConstructorDecl *>(ConstructorD);
4374 if (!Constructor)
4375 return;
4376
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004377 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004378 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00004379 CodeCompletionContext::CCC_PotentiallyQualifiedName);
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004380 Results.EnterNewScope();
4381
4382 // Fill in any already-initialized fields or base classes.
4383 llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields;
4384 llvm::SmallPtrSet<CanQualType, 4> InitializedBases;
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004385 for (unsigned I = 0, E = Initializers.size(); I != E; ++I) {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004386 if (Initializers[I]->isBaseInitializer())
4387 InitializedBases.insert(
4388 Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0)));
4389 else
Francois Pichetd583da02010-12-04 09:14:42 +00004390 InitializedFields.insert(cast<FieldDecl>(
4391 Initializers[I]->getAnyMember()));
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004392 }
4393
4394 // Add completions for base classes.
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004395 CodeCompletionBuilder Builder(Results.getAllocator(),
4396 Results.getCodeCompletionTUInfo());
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004397 bool SawLastInitializer = Initializers.empty();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004398 CXXRecordDecl *ClassDecl = Constructor->getParent();
Aaron Ballman574705e2014-03-13 15:41:46 +00004399 for (const auto &Base : ClassDecl->bases()) {
David Blaikie82e95a32014-11-19 07:49:47 +00004400 if (!InitializedBases.insert(Context.getCanonicalType(Base.getType()))
4401 .second) {
Douglas Gregor99129ef2010-08-29 19:27:27 +00004402 SawLastInitializer
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004403 = !Initializers.empty() &&
4404 Initializers.back()->isBaseInitializer() &&
Aaron Ballman574705e2014-03-13 15:41:46 +00004405 Context.hasSameUnqualifiedType(Base.getType(),
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004406 QualType(Initializers.back()->getBaseClass(), 0));
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004407 continue;
Douglas Gregor99129ef2010-08-29 19:27:27 +00004408 }
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004409
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004410 Builder.AddTypedTextChunk(
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00004411 Results.getAllocator().CopyString(
Aaron Ballman574705e2014-03-13 15:41:46 +00004412 Base.getType().getAsString(Policy)));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004413 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4414 Builder.AddPlaceholderChunk("args");
4415 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4416 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor99129ef2010-08-29 19:27:27 +00004417 SawLastInitializer? CCP_NextInitializer
4418 : CCP_MemberDeclaration));
4419 SawLastInitializer = false;
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004420 }
4421
4422 // Add completions for virtual base classes.
Aaron Ballman445a9392014-03-13 16:15:17 +00004423 for (const auto &Base : ClassDecl->vbases()) {
David Blaikie82e95a32014-11-19 07:49:47 +00004424 if (!InitializedBases.insert(Context.getCanonicalType(Base.getType()))
4425 .second) {
Douglas Gregor99129ef2010-08-29 19:27:27 +00004426 SawLastInitializer
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004427 = !Initializers.empty() &&
4428 Initializers.back()->isBaseInitializer() &&
Aaron Ballman445a9392014-03-13 16:15:17 +00004429 Context.hasSameUnqualifiedType(Base.getType(),
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004430 QualType(Initializers.back()->getBaseClass(), 0));
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004431 continue;
Douglas Gregor99129ef2010-08-29 19:27:27 +00004432 }
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004433
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004434 Builder.AddTypedTextChunk(
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00004435 Builder.getAllocator().CopyString(
Aaron Ballman445a9392014-03-13 16:15:17 +00004436 Base.getType().getAsString(Policy)));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004437 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4438 Builder.AddPlaceholderChunk("args");
4439 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4440 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor99129ef2010-08-29 19:27:27 +00004441 SawLastInitializer? CCP_NextInitializer
4442 : CCP_MemberDeclaration));
4443 SawLastInitializer = false;
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004444 }
4445
4446 // Add completions for members.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00004447 for (auto *Field : ClassDecl->fields()) {
David Blaikie82e95a32014-11-19 07:49:47 +00004448 if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))
4449 .second) {
Douglas Gregor99129ef2010-08-29 19:27:27 +00004450 SawLastInitializer
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004451 = !Initializers.empty() &&
4452 Initializers.back()->isAnyMemberInitializer() &&
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00004453 Initializers.back()->getAnyMember() == Field;
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004454 continue;
Douglas Gregor99129ef2010-08-29 19:27:27 +00004455 }
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004456
4457 if (!Field->getDeclName())
4458 continue;
4459
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00004460 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004461 Field->getIdentifier()->getName()));
4462 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4463 Builder.AddPlaceholderChunk("args");
4464 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4465 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor99129ef2010-08-29 19:27:27 +00004466 SawLastInitializer? CCP_NextInitializer
Douglas Gregorf3af3112010-09-09 21:42:20 +00004467 : CCP_MemberDeclaration,
Douglas Gregor78254c82012-03-27 23:34:16 +00004468 CXCursor_MemberRef,
4469 CXAvailability_Available,
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00004470 Field));
Douglas Gregor99129ef2010-08-29 19:27:27 +00004471 SawLastInitializer = false;
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004472 }
4473 Results.ExitScope();
4474
Douglas Gregor0ac41382010-09-23 23:01:17 +00004475 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004476 Results.data(), Results.size());
4477}
4478
Douglas Gregord8c61782012-02-15 15:34:24 +00004479/// \brief Determine whether this scope denotes a namespace.
4480static bool isNamespaceScope(Scope *S) {
Ted Kremenekc37877d2013-10-08 17:08:03 +00004481 DeclContext *DC = S->getEntity();
Douglas Gregord8c61782012-02-15 15:34:24 +00004482 if (!DC)
4483 return false;
4484
4485 return DC->isFileContext();
4486}
4487
4488void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro,
4489 bool AfterAmpersand) {
4490 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004491 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregord8c61782012-02-15 15:34:24 +00004492 CodeCompletionContext::CCC_Other);
4493 Results.EnterNewScope();
4494
4495 // Note what has already been captured.
4496 llvm::SmallPtrSet<IdentifierInfo *, 4> Known;
4497 bool IncludedThis = false;
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00004498 for (const auto &C : Intro.Captures) {
4499 if (C.Kind == LCK_This) {
Douglas Gregord8c61782012-02-15 15:34:24 +00004500 IncludedThis = true;
4501 continue;
4502 }
4503
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00004504 Known.insert(C.Id);
Douglas Gregord8c61782012-02-15 15:34:24 +00004505 }
4506
4507 // Look for other capturable variables.
4508 for (; S && !isNamespaceScope(S); S = S->getParent()) {
Aaron Ballman35c54952014-03-17 16:55:25 +00004509 for (const auto *D : S->decls()) {
4510 const auto *Var = dyn_cast<VarDecl>(D);
Douglas Gregord8c61782012-02-15 15:34:24 +00004511 if (!Var ||
4512 !Var->hasLocalStorage() ||
4513 Var->hasAttr<BlocksAttr>())
4514 continue;
4515
David Blaikie82e95a32014-11-19 07:49:47 +00004516 if (Known.insert(Var->getIdentifier()).second)
Douglas Gregor0a0e2b32013-01-31 04:52:16 +00004517 Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration),
Craig Topperc3ec1492014-05-26 06:22:03 +00004518 CurContext, nullptr, false);
Douglas Gregord8c61782012-02-15 15:34:24 +00004519 }
4520 }
4521
4522 // Add 'this', if it would be valid.
4523 if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy)
4524 addThisCompletion(*this, Results);
4525
4526 Results.ExitScope();
4527
4528 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4529 Results.data(), Results.size());
4530}
4531
James Dennett596e4752012-06-14 03:11:41 +00004532/// Macro that optionally prepends an "@" to the string literal passed in via
4533/// Keyword, depending on whether NeedAt is true or false.
4534#define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword)
4535
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004536static void AddObjCImplementationResults(const LangOptions &LangOpts,
Douglas Gregorf1934162010-01-13 21:24:21 +00004537 ResultBuilder &Results,
4538 bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004539 typedef CodeCompletionResult Result;
Douglas Gregorf1934162010-01-13 21:24:21 +00004540 // Since we have an implementation, we can end it.
James Dennett596e4752012-06-14 03:11:41 +00004541 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
Douglas Gregorf1934162010-01-13 21:24:21 +00004542
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004543 CodeCompletionBuilder Builder(Results.getAllocator(),
4544 Results.getCodeCompletionTUInfo());
Douglas Gregorf1934162010-01-13 21:24:21 +00004545 if (LangOpts.ObjC2) {
4546 // @dynamic
James Dennett596e4752012-06-14 03:11:41 +00004547 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004548 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4549 Builder.AddPlaceholderChunk("property");
4550 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf1934162010-01-13 21:24:21 +00004551
4552 // @synthesize
James Dennett596e4752012-06-14 03:11:41 +00004553 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004554 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4555 Builder.AddPlaceholderChunk("property");
4556 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf1934162010-01-13 21:24:21 +00004557 }
4558}
4559
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004560static void AddObjCInterfaceResults(const LangOptions &LangOpts,
Douglas Gregorf1934162010-01-13 21:24:21 +00004561 ResultBuilder &Results,
4562 bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004563 typedef CodeCompletionResult Result;
Douglas Gregorf1934162010-01-13 21:24:21 +00004564
4565 // Since we have an interface or protocol, we can end it.
James Dennett596e4752012-06-14 03:11:41 +00004566 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
Douglas Gregorf1934162010-01-13 21:24:21 +00004567
4568 if (LangOpts.ObjC2) {
4569 // @property
James Dennett596e4752012-06-14 03:11:41 +00004570 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property")));
Douglas Gregorf1934162010-01-13 21:24:21 +00004571
4572 // @required
James Dennett596e4752012-06-14 03:11:41 +00004573 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required")));
Douglas Gregorf1934162010-01-13 21:24:21 +00004574
4575 // @optional
James Dennett596e4752012-06-14 03:11:41 +00004576 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional")));
Douglas Gregorf1934162010-01-13 21:24:21 +00004577 }
4578}
4579
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004580static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004581 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004582 CodeCompletionBuilder Builder(Results.getAllocator(),
4583 Results.getCodeCompletionTUInfo());
Douglas Gregorf1934162010-01-13 21:24:21 +00004584
4585 // @class name ;
James Dennett596e4752012-06-14 03:11:41 +00004586 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004587 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4588 Builder.AddPlaceholderChunk("name");
4589 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf1934162010-01-13 21:24:21 +00004590
Douglas Gregorf4c33342010-05-28 00:22:41 +00004591 if (Results.includeCodePatterns()) {
4592 // @interface name
4593 // FIXME: Could introduce the whole pattern, including superclasses and
4594 // such.
James Dennett596e4752012-06-14 03:11:41 +00004595 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004596 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4597 Builder.AddPlaceholderChunk("class");
4598 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf1934162010-01-13 21:24:21 +00004599
Douglas Gregorf4c33342010-05-28 00:22:41 +00004600 // @protocol name
James Dennett596e4752012-06-14 03:11:41 +00004601 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004602 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4603 Builder.AddPlaceholderChunk("protocol");
4604 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00004605
4606 // @implementation name
James Dennett596e4752012-06-14 03:11:41 +00004607 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004608 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4609 Builder.AddPlaceholderChunk("class");
4610 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00004611 }
Douglas Gregorf1934162010-01-13 21:24:21 +00004612
4613 // @compatibility_alias name
James Dennett596e4752012-06-14 03:11:41 +00004614 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004615 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4616 Builder.AddPlaceholderChunk("alias");
4617 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4618 Builder.AddPlaceholderChunk("class");
4619 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor61e36812013-03-07 23:26:24 +00004620
4621 if (Results.getSema().getLangOpts().Modules) {
4622 // @import name
4623 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import"));
4624 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4625 Builder.AddPlaceholderChunk("module");
4626 Results.AddResult(Result(Builder.TakeString()));
4627 }
Douglas Gregorf1934162010-01-13 21:24:21 +00004628}
4629
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00004630void Sema::CodeCompleteObjCAtDirective(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004631 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004632 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004633 CodeCompletionContext::CCC_Other);
Douglas Gregorf48706c2009-12-07 09:27:33 +00004634 Results.EnterNewScope();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00004635 if (isa<ObjCImplDecl>(CurContext))
David Blaikiebbafb8a2012-03-11 07:00:24 +00004636 AddObjCImplementationResults(getLangOpts(), Results, false);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00004637 else if (CurContext->isObjCContainer())
David Blaikiebbafb8a2012-03-11 07:00:24 +00004638 AddObjCInterfaceResults(getLangOpts(), Results, false);
Douglas Gregorf1934162010-01-13 21:24:21 +00004639 else
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004640 AddObjCTopLevelResults(Results, false);
Douglas Gregorf48706c2009-12-07 09:27:33 +00004641 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004642 HandleCodeCompleteResults(this, CodeCompleter,
4643 CodeCompletionContext::CCC_Other,
4644 Results.data(),Results.size());
Douglas Gregorf48706c2009-12-07 09:27:33 +00004645}
4646
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004647static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004648 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004649 CodeCompletionBuilder Builder(Results.getAllocator(),
4650 Results.getCodeCompletionTUInfo());
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004651
4652 // @encode ( type-name )
Douglas Gregore5c79d52011-10-18 21:20:17 +00004653 const char *EncodeType = "char[]";
David Blaikiebbafb8a2012-03-11 07:00:24 +00004654 if (Results.getSema().getLangOpts().CPlusPlus ||
4655 Results.getSema().getLangOpts().ConstStrings)
Jordan Rose9da05852012-06-15 18:19:56 +00004656 EncodeType = "const char[]";
Douglas Gregore5c79d52011-10-18 21:20:17 +00004657 Builder.AddResultTypeChunk(EncodeType);
James Dennett596e4752012-06-14 03:11:41 +00004658 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004659 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4660 Builder.AddPlaceholderChunk("type-name");
4661 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4662 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004663
4664 // @protocol ( protocol-name )
Douglas Gregore5c79d52011-10-18 21:20:17 +00004665 Builder.AddResultTypeChunk("Protocol *");
James Dennett596e4752012-06-14 03:11:41 +00004666 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004667 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4668 Builder.AddPlaceholderChunk("protocol-name");
4669 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4670 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004671
4672 // @selector ( selector )
Douglas Gregore5c79d52011-10-18 21:20:17 +00004673 Builder.AddResultTypeChunk("SEL");
James Dennett596e4752012-06-14 03:11:41 +00004674 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004675 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4676 Builder.AddPlaceholderChunk("selector");
4677 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4678 Results.AddResult(Result(Builder.TakeString()));
Jordan Rose9da05852012-06-15 18:19:56 +00004679
4680 // @"string"
4681 Builder.AddResultTypeChunk("NSString *");
4682 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\""));
4683 Builder.AddPlaceholderChunk("string");
4684 Builder.AddTextChunk("\"");
4685 Results.AddResult(Result(Builder.TakeString()));
4686
Douglas Gregor951de302012-07-17 23:24:47 +00004687 // @[objects, ...]
Jordan Rose9da05852012-06-15 18:19:56 +00004688 Builder.AddResultTypeChunk("NSArray *");
James Dennett596e4752012-06-14 03:11:41 +00004689 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"["));
Ted Kremeneke65b0862012-03-06 20:05:56 +00004690 Builder.AddPlaceholderChunk("objects, ...");
Ted Kremeneke65b0862012-03-06 20:05:56 +00004691 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
4692 Results.AddResult(Result(Builder.TakeString()));
4693
Douglas Gregor951de302012-07-17 23:24:47 +00004694 // @{key : object, ...}
Jordan Rose9da05852012-06-15 18:19:56 +00004695 Builder.AddResultTypeChunk("NSDictionary *");
James Dennett596e4752012-06-14 03:11:41 +00004696 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{"));
Ted Kremeneke65b0862012-03-06 20:05:56 +00004697 Builder.AddPlaceholderChunk("key");
Ted Kremeneke65b0862012-03-06 20:05:56 +00004698 Builder.AddChunk(CodeCompletionString::CK_Colon);
4699 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4700 Builder.AddPlaceholderChunk("object, ...");
Ted Kremeneke65b0862012-03-06 20:05:56 +00004701 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4702 Results.AddResult(Result(Builder.TakeString()));
Jordan Rose9da05852012-06-15 18:19:56 +00004703
Douglas Gregor951de302012-07-17 23:24:47 +00004704 // @(expression)
Jordan Rose9da05852012-06-15 18:19:56 +00004705 Builder.AddResultTypeChunk("id");
4706 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "("));
Jordan Rose9da05852012-06-15 18:19:56 +00004707 Builder.AddPlaceholderChunk("expression");
Jordan Rose9da05852012-06-15 18:19:56 +00004708 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4709 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004710}
4711
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004712static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004713 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004714 CodeCompletionBuilder Builder(Results.getAllocator(),
4715 Results.getCodeCompletionTUInfo());
Douglas Gregorf1934162010-01-13 21:24:21 +00004716
Douglas Gregorf4c33342010-05-28 00:22:41 +00004717 if (Results.includeCodePatterns()) {
4718 // @try { statements } @catch ( declaration ) { statements } @finally
4719 // { statements }
James Dennett596e4752012-06-14 03:11:41 +00004720 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004721 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4722 Builder.AddPlaceholderChunk("statements");
4723 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4724 Builder.AddTextChunk("@catch");
4725 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4726 Builder.AddPlaceholderChunk("parameter");
4727 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4728 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4729 Builder.AddPlaceholderChunk("statements");
4730 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4731 Builder.AddTextChunk("@finally");
4732 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4733 Builder.AddPlaceholderChunk("statements");
4734 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4735 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00004736 }
Douglas Gregorf1934162010-01-13 21:24:21 +00004737
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004738 // @throw
James Dennett596e4752012-06-14 03:11:41 +00004739 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004740 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4741 Builder.AddPlaceholderChunk("expression");
4742 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf1934162010-01-13 21:24:21 +00004743
Douglas Gregorf4c33342010-05-28 00:22:41 +00004744 if (Results.includeCodePatterns()) {
4745 // @synchronized ( expression ) { statements }
James Dennett596e4752012-06-14 03:11:41 +00004746 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004747 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4748 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4749 Builder.AddPlaceholderChunk("expression");
4750 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4751 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4752 Builder.AddPlaceholderChunk("statements");
4753 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4754 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00004755 }
Douglas Gregorf1934162010-01-13 21:24:21 +00004756}
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004757
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004758static void AddObjCVisibilityResults(const LangOptions &LangOpts,
Douglas Gregor48d46252010-01-13 21:54:15 +00004759 ResultBuilder &Results,
4760 bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004761 typedef CodeCompletionResult Result;
James Dennett596e4752012-06-14 03:11:41 +00004762 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private")));
4763 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected")));
4764 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public")));
Douglas Gregor48d46252010-01-13 21:54:15 +00004765 if (LangOpts.ObjC2)
James Dennett596e4752012-06-14 03:11:41 +00004766 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package")));
Douglas Gregor48d46252010-01-13 21:54:15 +00004767}
4768
4769void Sema::CodeCompleteObjCAtVisibility(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004770 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004771 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004772 CodeCompletionContext::CCC_Other);
Douglas Gregor48d46252010-01-13 21:54:15 +00004773 Results.EnterNewScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004774 AddObjCVisibilityResults(getLangOpts(), Results, false);
Douglas Gregor48d46252010-01-13 21:54:15 +00004775 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004776 HandleCodeCompleteResults(this, CodeCompleter,
4777 CodeCompletionContext::CCC_Other,
4778 Results.data(),Results.size());
Douglas Gregor48d46252010-01-13 21:54:15 +00004779}
4780
4781void Sema::CodeCompleteObjCAtStatement(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004782 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004783 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004784 CodeCompletionContext::CCC_Other);
Douglas Gregorf1934162010-01-13 21:24:21 +00004785 Results.EnterNewScope();
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004786 AddObjCStatementResults(Results, false);
4787 AddObjCExpressionResults(Results, false);
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004788 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004789 HandleCodeCompleteResults(this, CodeCompleter,
4790 CodeCompletionContext::CCC_Other,
4791 Results.data(),Results.size());
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004792}
4793
4794void Sema::CodeCompleteObjCAtExpression(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004795 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004796 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004797 CodeCompletionContext::CCC_Other);
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004798 Results.EnterNewScope();
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004799 AddObjCExpressionResults(Results, false);
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004800 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004801 HandleCodeCompleteResults(this, CodeCompleter,
4802 CodeCompletionContext::CCC_Other,
4803 Results.data(),Results.size());
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004804}
4805
Douglas Gregore6078da2009-11-19 00:14:45 +00004806/// \brief Determine whether the addition of the given flag to an Objective-C
4807/// property's attributes will cause a conflict.
Bill Wendling44426052012-12-20 19:22:21 +00004808static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
Douglas Gregore6078da2009-11-19 00:14:45 +00004809 // Check if we've already added this flag.
Bill Wendling44426052012-12-20 19:22:21 +00004810 if (Attributes & NewFlag)
Douglas Gregore6078da2009-11-19 00:14:45 +00004811 return true;
4812
Bill Wendling44426052012-12-20 19:22:21 +00004813 Attributes |= NewFlag;
Douglas Gregore6078da2009-11-19 00:14:45 +00004814
4815 // Check for collisions with "readonly".
Bill Wendling44426052012-12-20 19:22:21 +00004816 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
4817 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Douglas Gregore6078da2009-11-19 00:14:45 +00004818 return true;
4819
Jordan Rose53cb2f32012-08-20 20:01:13 +00004820 // Check for more than one of { assign, copy, retain, strong, weak }.
Bill Wendling44426052012-12-20 19:22:21 +00004821 unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign |
John McCall31168b02011-06-15 23:02:42 +00004822 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Douglas Gregore6078da2009-11-19 00:14:45 +00004823 ObjCDeclSpec::DQ_PR_copy |
Jordan Rose53cb2f32012-08-20 20:01:13 +00004824 ObjCDeclSpec::DQ_PR_retain |
4825 ObjCDeclSpec::DQ_PR_strong |
4826 ObjCDeclSpec::DQ_PR_weak);
Douglas Gregore6078da2009-11-19 00:14:45 +00004827 if (AssignCopyRetMask &&
4828 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign &&
John McCall31168b02011-06-15 23:02:42 +00004829 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained &&
Douglas Gregore6078da2009-11-19 00:14:45 +00004830 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy &&
John McCall31168b02011-06-15 23:02:42 +00004831 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain &&
Jordan Rose53cb2f32012-08-20 20:01:13 +00004832 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong &&
4833 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak)
Douglas Gregore6078da2009-11-19 00:14:45 +00004834 return true;
4835
4836 return false;
4837}
4838
Douglas Gregor36029f42009-11-18 23:08:07 +00004839void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
Steve Naroff936354c2009-10-08 21:55:05 +00004840 if (!CodeCompleter)
4841 return;
Douglas Gregor1b605f72009-11-19 01:08:35 +00004842
Bill Wendling44426052012-12-20 19:22:21 +00004843 unsigned Attributes = ODS.getPropertyAttributes();
Steve Naroff936354c2009-10-08 21:55:05 +00004844
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004845 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004846 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004847 CodeCompletionContext::CCC_Other);
Steve Naroff936354c2009-10-08 21:55:05 +00004848 Results.EnterNewScope();
Bill Wendling44426052012-12-20 19:22:21 +00004849 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly))
John McCall276321a2010-08-25 06:19:51 +00004850 Results.AddResult(CodeCompletionResult("readonly"));
Bill Wendling44426052012-12-20 19:22:21 +00004851 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign))
John McCall276321a2010-08-25 06:19:51 +00004852 Results.AddResult(CodeCompletionResult("assign"));
Bill Wendling44426052012-12-20 19:22:21 +00004853 if (!ObjCPropertyFlagConflicts(Attributes,
John McCall31168b02011-06-15 23:02:42 +00004854 ObjCDeclSpec::DQ_PR_unsafe_unretained))
4855 Results.AddResult(CodeCompletionResult("unsafe_unretained"));
Bill Wendling44426052012-12-20 19:22:21 +00004856 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite))
John McCall276321a2010-08-25 06:19:51 +00004857 Results.AddResult(CodeCompletionResult("readwrite"));
Bill Wendling44426052012-12-20 19:22:21 +00004858 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain))
John McCall276321a2010-08-25 06:19:51 +00004859 Results.AddResult(CodeCompletionResult("retain"));
Bill Wendling44426052012-12-20 19:22:21 +00004860 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong))
John McCall31168b02011-06-15 23:02:42 +00004861 Results.AddResult(CodeCompletionResult("strong"));
Bill Wendling44426052012-12-20 19:22:21 +00004862 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy))
John McCall276321a2010-08-25 06:19:51 +00004863 Results.AddResult(CodeCompletionResult("copy"));
Bill Wendling44426052012-12-20 19:22:21 +00004864 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic))
John McCall276321a2010-08-25 06:19:51 +00004865 Results.AddResult(CodeCompletionResult("nonatomic"));
Bill Wendling44426052012-12-20 19:22:21 +00004866 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic))
Fariborz Jahanian1c2d29e2011-06-11 17:14:27 +00004867 Results.AddResult(CodeCompletionResult("atomic"));
Jordan Rose53cb2f32012-08-20 20:01:13 +00004868
4869 // Only suggest "weak" if we're compiling for ARC-with-weak-references or GC.
John McCall3deb1ad2012-08-21 02:47:43 +00004870 if (getLangOpts().ObjCARCWeak || getLangOpts().getGC() != LangOptions::NonGC)
Bill Wendling44426052012-12-20 19:22:21 +00004871 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak))
Jordan Rose53cb2f32012-08-20 20:01:13 +00004872 Results.AddResult(CodeCompletionResult("weak"));
4873
Bill Wendling44426052012-12-20 19:22:21 +00004874 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004875 CodeCompletionBuilder Setter(Results.getAllocator(),
4876 Results.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004877 Setter.AddTypedTextChunk("setter");
Argyrios Kyrtzidis7bbb8812014-02-20 07:55:15 +00004878 Setter.AddTextChunk("=");
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004879 Setter.AddPlaceholderChunk("method");
4880 Results.AddResult(CodeCompletionResult(Setter.TakeString()));
Douglas Gregor45f83ee2009-11-19 00:01:57 +00004881 }
Bill Wendling44426052012-12-20 19:22:21 +00004882 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004883 CodeCompletionBuilder Getter(Results.getAllocator(),
4884 Results.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004885 Getter.AddTypedTextChunk("getter");
Argyrios Kyrtzidis7bbb8812014-02-20 07:55:15 +00004886 Getter.AddTextChunk("=");
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004887 Getter.AddPlaceholderChunk("method");
4888 Results.AddResult(CodeCompletionResult(Getter.TakeString()));
Douglas Gregor45f83ee2009-11-19 00:01:57 +00004889 }
Douglas Gregor86b42682015-06-19 18:27:52 +00004890 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nullability)) {
4891 Results.AddResult(CodeCompletionResult("nonnull"));
4892 Results.AddResult(CodeCompletionResult("nullable"));
4893 Results.AddResult(CodeCompletionResult("null_unspecified"));
4894 Results.AddResult(CodeCompletionResult("null_resettable"));
4895 }
Steve Naroff936354c2009-10-08 21:55:05 +00004896 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004897 HandleCodeCompleteResults(this, CodeCompleter,
4898 CodeCompletionContext::CCC_Other,
4899 Results.data(),Results.size());
Steve Naroff936354c2009-10-08 21:55:05 +00004900}
Steve Naroffeae65032009-11-07 02:08:14 +00004901
James Dennettf1243872012-06-17 05:33:25 +00004902/// \brief Describes the kind of Objective-C method that we want to find
Douglas Gregorc8537c52009-11-19 07:41:15 +00004903/// via code completion.
4904enum ObjCMethodKind {
Dmitri Gribenko4280e5c2012-06-08 23:13:42 +00004905 MK_Any, ///< Any kind of method, provided it means other specified criteria.
4906 MK_ZeroArgSelector, ///< Zero-argument (unary) selector.
4907 MK_OneArgSelector ///< One-argument selector.
Douglas Gregorc8537c52009-11-19 07:41:15 +00004908};
4909
Douglas Gregor67c692c2010-08-26 15:07:07 +00004910static bool isAcceptableObjCSelector(Selector Sel,
4911 ObjCMethodKind WantKind,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004912 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00004913 bool AllowSameLength = true) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004914 unsigned NumSelIdents = SelIdents.size();
Douglas Gregor67c692c2010-08-26 15:07:07 +00004915 if (NumSelIdents > Sel.getNumArgs())
4916 return false;
4917
4918 switch (WantKind) {
4919 case MK_Any: break;
4920 case MK_ZeroArgSelector: return Sel.isUnarySelector();
4921 case MK_OneArgSelector: return Sel.getNumArgs() == 1;
4922 }
4923
Douglas Gregorb4a7c032010-11-17 21:36:08 +00004924 if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs())
4925 return false;
4926
Douglas Gregor67c692c2010-08-26 15:07:07 +00004927 for (unsigned I = 0; I != NumSelIdents; ++I)
4928 if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
4929 return false;
4930
4931 return true;
4932}
4933
Douglas Gregorc8537c52009-11-19 07:41:15 +00004934static bool isAcceptableObjCMethod(ObjCMethodDecl *Method,
4935 ObjCMethodKind WantKind,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004936 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00004937 bool AllowSameLength = true) {
Douglas Gregor67c692c2010-08-26 15:07:07 +00004938 return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004939 AllowSameLength);
Douglas Gregorc8537c52009-11-19 07:41:15 +00004940}
Douglas Gregor1154e272010-09-16 16:06:31 +00004941
4942namespace {
4943 /// \brief A set of selectors, which is used to avoid introducing multiple
4944 /// completions with the same selector into the result set.
4945 typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet;
4946}
4947
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00004948/// \brief Add all of the Objective-C methods in the given Objective-C
4949/// container to the set of results.
4950///
4951/// The container will be a class, protocol, category, or implementation of
4952/// any of the above. This mether will recurse to include methods from
4953/// the superclasses of classes along with their categories, protocols, and
4954/// implementations.
4955///
4956/// \param Container the container in which we'll look to find methods.
4957///
James Dennett596e4752012-06-14 03:11:41 +00004958/// \param WantInstanceMethods Whether to add instance methods (only); if
4959/// false, this routine will add factory methods (only).
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00004960///
4961/// \param CurContext the context in which we're performing the lookup that
4962/// finds methods.
4963///
Douglas Gregorb4a7c032010-11-17 21:36:08 +00004964/// \param AllowSameLength Whether we allow a method to be added to the list
4965/// when it has the same number of parameters as we have selector identifiers.
4966///
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00004967/// \param Results the structure into which we'll add results.
4968static void AddObjCMethods(ObjCContainerDecl *Container,
4969 bool WantInstanceMethods,
Douglas Gregorc8537c52009-11-19 07:41:15 +00004970 ObjCMethodKind WantKind,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004971 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00004972 DeclContext *CurContext,
Douglas Gregor1154e272010-09-16 16:06:31 +00004973 VisitedSelectorSet &Selectors,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00004974 bool AllowSameLength,
Douglas Gregor416b5752010-08-25 01:08:01 +00004975 ResultBuilder &Results,
4976 bool InOriginalClass = true) {
John McCall276321a2010-08-25 06:19:51 +00004977 typedef CodeCompletionResult Result;
Douglas Gregor9b4f3702012-06-12 13:44:08 +00004978 Container = getContainerDef(Container);
Douglas Gregor41778c32013-01-30 06:58:39 +00004979 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
4980 bool isRootClass = IFace && !IFace->getSuperClass();
Aaron Ballmanaff18c02014-03-13 19:03:34 +00004981 for (auto *M : Container->methods()) {
Douglas Gregor41778c32013-01-30 06:58:39 +00004982 // The instance methods on the root class can be messaged via the
4983 // metaclass.
4984 if (M->isInstanceMethod() == WantInstanceMethods ||
4985 (isRootClass && !WantInstanceMethods)) {
Douglas Gregor1b605f72009-11-19 01:08:35 +00004986 // Check whether the selector identifiers we've been given are a
4987 // subset of the identifiers for this particular method.
Aaron Ballmanaff18c02014-03-13 19:03:34 +00004988 if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength))
Douglas Gregor1b605f72009-11-19 01:08:35 +00004989 continue;
Douglas Gregorc8537c52009-11-19 07:41:15 +00004990
David Blaikie82e95a32014-11-19 07:49:47 +00004991 if (!Selectors.insert(M->getSelector()).second)
Douglas Gregor1154e272010-09-16 16:06:31 +00004992 continue;
Craig Topperc3ec1492014-05-26 06:22:03 +00004993
4994 Result R = Result(M, Results.getBasePriority(M), nullptr);
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004995 R.StartParameter = SelIdents.size();
Douglas Gregorc8537c52009-11-19 07:41:15 +00004996 R.AllParametersAreInformative = (WantKind != MK_Any);
Douglas Gregor416b5752010-08-25 01:08:01 +00004997 if (!InOriginalClass)
4998 R.Priority += CCD_InBaseClass;
Douglas Gregor1b605f72009-11-19 01:08:35 +00004999 Results.MaybeAddResult(R, CurContext);
5000 }
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005001 }
5002
Douglas Gregorf37c9492010-09-16 15:34:59 +00005003 // Visit the protocols of protocols.
5004 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00005005 if (Protocol->hasDefinition()) {
5006 const ObjCList<ObjCProtocolDecl> &Protocols
5007 = Protocol->getReferencedProtocols();
5008 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
5009 E = Protocols.end();
5010 I != E; ++I)
5011 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005012 CurContext, Selectors, AllowSameLength, Results, false);
Douglas Gregore6e48b12012-01-01 19:29:29 +00005013 }
Douglas Gregorf37c9492010-09-16 15:34:59 +00005014 }
5015
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00005016 if (!IFace || !IFace->hasDefinition())
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005017 return;
5018
5019 // Add methods in protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +00005020 for (auto *I : IFace->protocols())
5021 AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005022 CurContext, Selectors, AllowSameLength, Results, false);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005023
5024 // Add methods in categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00005025 for (auto *CatDecl : IFace->known_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00005026 AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005027 CurContext, Selectors, AllowSameLength,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005028 Results, InOriginalClass);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005029
5030 // Add a categories protocol methods.
5031 const ObjCList<ObjCProtocolDecl> &Protocols
5032 = CatDecl->getReferencedProtocols();
5033 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
5034 E = Protocols.end();
5035 I != E; ++I)
Douglas Gregorc8537c52009-11-19 07:41:15 +00005036 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005037 CurContext, Selectors, AllowSameLength,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005038 Results, false);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005039
5040 // Add methods in category implementations.
5041 if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
Douglas Gregorc8537c52009-11-19 07:41:15 +00005042 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005043 CurContext, Selectors, AllowSameLength,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005044 Results, InOriginalClass);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005045 }
5046
5047 // Add methods in superclass.
5048 if (IFace->getSuperClass())
Douglas Gregorc8537c52009-11-19 07:41:15 +00005049 AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005050 SelIdents, CurContext, Selectors,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005051 AllowSameLength, Results, false);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005052
5053 // Add methods in our implementation, if any.
5054 if (ObjCImplementationDecl *Impl = IFace->getImplementation())
Douglas Gregorc8537c52009-11-19 07:41:15 +00005055 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005056 CurContext, Selectors, AllowSameLength,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005057 Results, InOriginalClass);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005058}
5059
5060
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005061void Sema::CodeCompleteObjCPropertyGetter(Scope *S) {
Douglas Gregorc8537c52009-11-19 07:41:15 +00005062 // Try to find the interface where getters might live.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005063 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005064 if (!Class) {
5065 if (ObjCCategoryDecl *Category
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005066 = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
Douglas Gregorc8537c52009-11-19 07:41:15 +00005067 Class = Category->getClassInterface();
5068
5069 if (!Class)
5070 return;
5071 }
5072
5073 // Find all of the potential getters.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005074 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005075 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005076 CodeCompletionContext::CCC_Other);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005077 Results.EnterNewScope();
5078
Douglas Gregor1154e272010-09-16 16:06:31 +00005079 VisitedSelectorSet Selectors;
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005080 AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005081 /*AllowSameLength=*/true, Results);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005082 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005083 HandleCodeCompleteResults(this, CodeCompleter,
5084 CodeCompletionContext::CCC_Other,
5085 Results.data(),Results.size());
Douglas Gregorc8537c52009-11-19 07:41:15 +00005086}
5087
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005088void Sema::CodeCompleteObjCPropertySetter(Scope *S) {
Douglas Gregorc8537c52009-11-19 07:41:15 +00005089 // Try to find the interface where setters might live.
5090 ObjCInterfaceDecl *Class
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005091 = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005092 if (!Class) {
5093 if (ObjCCategoryDecl *Category
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005094 = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
Douglas Gregorc8537c52009-11-19 07:41:15 +00005095 Class = Category->getClassInterface();
5096
5097 if (!Class)
5098 return;
5099 }
5100
5101 // Find all of the potential getters.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005102 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005103 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005104 CodeCompletionContext::CCC_Other);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005105 Results.EnterNewScope();
5106
Douglas Gregor1154e272010-09-16 16:06:31 +00005107 VisitedSelectorSet Selectors;
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005108 AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005109 Selectors, /*AllowSameLength=*/true, Results);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005110
5111 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005112 HandleCodeCompleteResults(this, CodeCompleter,
5113 CodeCompletionContext::CCC_Other,
5114 Results.data(),Results.size());
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005115}
5116
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005117void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS,
5118 bool IsParameter) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005119 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005120 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005121 CodeCompletionContext::CCC_Type);
Douglas Gregor99fa2642010-08-24 01:06:58 +00005122 Results.EnterNewScope();
5123
5124 // Add context-sensitive, Objective-C parameter-passing keywords.
5125 bool AddedInOut = false;
5126 if ((DS.getObjCDeclQualifier() &
5127 (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) {
5128 Results.AddResult("in");
5129 Results.AddResult("inout");
5130 AddedInOut = true;
5131 }
5132 if ((DS.getObjCDeclQualifier() &
5133 (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) {
5134 Results.AddResult("out");
5135 if (!AddedInOut)
5136 Results.AddResult("inout");
5137 }
5138 if ((DS.getObjCDeclQualifier() &
5139 (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref |
5140 ObjCDeclSpec::DQ_Oneway)) == 0) {
5141 Results.AddResult("bycopy");
5142 Results.AddResult("byref");
5143 Results.AddResult("oneway");
5144 }
Douglas Gregor86b42682015-06-19 18:27:52 +00005145 if ((DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) == 0) {
5146 Results.AddResult("nonnull");
5147 Results.AddResult("nullable");
5148 Results.AddResult("null_unspecified");
5149 }
Douglas Gregor99fa2642010-08-24 01:06:58 +00005150
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005151 // If we're completing the return type of an Objective-C method and the
5152 // identifier IBAction refers to a macro, provide a completion item for
5153 // an action, e.g.,
5154 // IBAction)<#selector#>:(id)sender
5155 if (DS.getObjCDeclQualifier() == 0 && !IsParameter &&
Richard Smith20e883e2015-04-29 23:20:19 +00005156 PP.isMacroDefined("IBAction")) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005157 CodeCompletionBuilder Builder(Results.getAllocator(),
5158 Results.getCodeCompletionTUInfo(),
5159 CCP_CodePattern, CXAvailability_Available);
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005160 Builder.AddTypedTextChunk("IBAction");
Benjamin Kramerdb534a42012-03-26 16:57:36 +00005161 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005162 Builder.AddPlaceholderChunk("selector");
Benjamin Kramerdb534a42012-03-26 16:57:36 +00005163 Builder.AddChunk(CodeCompletionString::CK_Colon);
5164 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005165 Builder.AddTextChunk("id");
Benjamin Kramerdb534a42012-03-26 16:57:36 +00005166 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005167 Builder.AddTextChunk("sender");
5168 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
5169 }
Douglas Gregored1f5972013-01-30 07:11:43 +00005170
5171 // If we're completing the return type, provide 'instancetype'.
5172 if (!IsParameter) {
5173 Results.AddResult(CodeCompletionResult("instancetype"));
5174 }
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005175
Douglas Gregor99fa2642010-08-24 01:06:58 +00005176 // Add various builtin type names and specifiers.
5177 AddOrdinaryNameResults(PCC_Type, S, *this, Results);
5178 Results.ExitScope();
5179
5180 // Add the various type names
5181 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
5182 CodeCompletionDeclConsumer Consumer(Results, CurContext);
5183 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5184 CodeCompleter->includeGlobals());
5185
5186 if (CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00005187 AddMacroResults(PP, Results, false);
Douglas Gregor99fa2642010-08-24 01:06:58 +00005188
5189 HandleCodeCompleteResults(this, CodeCompleter,
5190 CodeCompletionContext::CCC_Type,
5191 Results.data(), Results.size());
5192}
5193
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005194/// \brief When we have an expression with type "id", we may assume
5195/// that it has some more-specific class type based on knowledge of
5196/// common uses of Objective-C. This routine returns that class type,
5197/// or NULL if no better result could be determined.
5198static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) {
Douglas Gregored0b69d2010-09-15 16:23:04 +00005199 ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E);
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005200 if (!Msg)
Craig Topperc3ec1492014-05-26 06:22:03 +00005201 return nullptr;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005202
5203 Selector Sel = Msg->getSelector();
5204 if (Sel.isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +00005205 return nullptr;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005206
5207 IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0);
5208 if (!Id)
Craig Topperc3ec1492014-05-26 06:22:03 +00005209 return nullptr;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005210
5211 ObjCMethodDecl *Method = Msg->getMethodDecl();
5212 if (!Method)
Craig Topperc3ec1492014-05-26 06:22:03 +00005213 return nullptr;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005214
5215 // Determine the class that we're sending the message to.
Craig Topperc3ec1492014-05-26 06:22:03 +00005216 ObjCInterfaceDecl *IFace = nullptr;
Douglas Gregor9a129192010-04-21 00:45:42 +00005217 switch (Msg->getReceiverKind()) {
5218 case ObjCMessageExpr::Class:
John McCall8b07ec22010-05-15 11:32:37 +00005219 if (const ObjCObjectType *ObjType
5220 = Msg->getClassReceiver()->getAs<ObjCObjectType>())
5221 IFace = ObjType->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00005222 break;
5223
5224 case ObjCMessageExpr::Instance: {
5225 QualType T = Msg->getInstanceReceiver()->getType();
5226 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
5227 IFace = Ptr->getInterfaceDecl();
5228 break;
5229 }
5230
5231 case ObjCMessageExpr::SuperInstance:
5232 case ObjCMessageExpr::SuperClass:
5233 break;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005234 }
5235
5236 if (!IFace)
Craig Topperc3ec1492014-05-26 06:22:03 +00005237 return nullptr;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005238
5239 ObjCInterfaceDecl *Super = IFace->getSuperClass();
5240 if (Method->isInstanceMethod())
5241 return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5242 .Case("retain", IFace)
John McCall31168b02011-06-15 23:02:42 +00005243 .Case("strong", IFace)
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005244 .Case("autorelease", IFace)
5245 .Case("copy", IFace)
5246 .Case("copyWithZone", IFace)
5247 .Case("mutableCopy", IFace)
5248 .Case("mutableCopyWithZone", IFace)
5249 .Case("awakeFromCoder", IFace)
5250 .Case("replacementObjectFromCoder", IFace)
5251 .Case("class", IFace)
5252 .Case("classForCoder", IFace)
5253 .Case("superclass", Super)
Craig Topperc3ec1492014-05-26 06:22:03 +00005254 .Default(nullptr);
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005255
5256 return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5257 .Case("new", IFace)
5258 .Case("alloc", IFace)
5259 .Case("allocWithZone", IFace)
5260 .Case("class", IFace)
5261 .Case("superclass", Super)
Craig Topperc3ec1492014-05-26 06:22:03 +00005262 .Default(nullptr);
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005263}
5264
Douglas Gregor6fc04132010-08-27 15:10:57 +00005265// Add a special completion for a message send to "super", which fills in the
5266// most likely case of forwarding all of our arguments to the superclass
5267// function.
5268///
5269/// \param S The semantic analysis object.
5270///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +00005271/// \param NeedSuperKeyword Whether we need to prefix this completion with
Douglas Gregor6fc04132010-08-27 15:10:57 +00005272/// the "super" keyword. Otherwise, we just need to provide the arguments.
5273///
5274/// \param SelIdents The identifiers in the selector that have already been
5275/// provided as arguments for a send to "super".
5276///
Douglas Gregor6fc04132010-08-27 15:10:57 +00005277/// \param Results The set of results to augment.
5278///
5279/// \returns the Objective-C method declaration that would be invoked by
5280/// this "super" completion. If NULL, no completion was added.
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005281static ObjCMethodDecl *AddSuperSendCompletion(
5282 Sema &S, bool NeedSuperKeyword,
5283 ArrayRef<IdentifierInfo *> SelIdents,
5284 ResultBuilder &Results) {
Douglas Gregor6fc04132010-08-27 15:10:57 +00005285 ObjCMethodDecl *CurMethod = S.getCurMethodDecl();
5286 if (!CurMethod)
Craig Topperc3ec1492014-05-26 06:22:03 +00005287 return nullptr;
5288
Douglas Gregor6fc04132010-08-27 15:10:57 +00005289 ObjCInterfaceDecl *Class = CurMethod->getClassInterface();
5290 if (!Class)
Craig Topperc3ec1492014-05-26 06:22:03 +00005291 return nullptr;
5292
Douglas Gregor6fc04132010-08-27 15:10:57 +00005293 // Try to find a superclass method with the same selector.
Craig Topperc3ec1492014-05-26 06:22:03 +00005294 ObjCMethodDecl *SuperMethod = nullptr;
Douglas Gregorb5f1e462011-02-16 00:51:18 +00005295 while ((Class = Class->getSuperClass()) && !SuperMethod) {
5296 // Check in the class
Douglas Gregor6fc04132010-08-27 15:10:57 +00005297 SuperMethod = Class->getMethod(CurMethod->getSelector(),
5298 CurMethod->isInstanceMethod());
5299
Douglas Gregorb5f1e462011-02-16 00:51:18 +00005300 // Check in categories or class extensions.
5301 if (!SuperMethod) {
Aaron Ballman15063e12014-03-13 21:35:02 +00005302 for (const auto *Cat : Class->known_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00005303 if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(),
Douglas Gregorb5f1e462011-02-16 00:51:18 +00005304 CurMethod->isInstanceMethod())))
5305 break;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00005306 }
Douglas Gregorb5f1e462011-02-16 00:51:18 +00005307 }
5308 }
5309
Douglas Gregor6fc04132010-08-27 15:10:57 +00005310 if (!SuperMethod)
Craig Topperc3ec1492014-05-26 06:22:03 +00005311 return nullptr;
5312
Douglas Gregor6fc04132010-08-27 15:10:57 +00005313 // Check whether the superclass method has the same signature.
5314 if (CurMethod->param_size() != SuperMethod->param_size() ||
5315 CurMethod->isVariadic() != SuperMethod->isVariadic())
Craig Topperc3ec1492014-05-26 06:22:03 +00005316 return nullptr;
5317
Douglas Gregor6fc04132010-08-27 15:10:57 +00005318 for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(),
5319 CurPEnd = CurMethod->param_end(),
5320 SuperP = SuperMethod->param_begin();
5321 CurP != CurPEnd; ++CurP, ++SuperP) {
5322 // Make sure the parameter types are compatible.
5323 if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(),
5324 (*SuperP)->getType()))
Craig Topperc3ec1492014-05-26 06:22:03 +00005325 return nullptr;
5326
Douglas Gregor6fc04132010-08-27 15:10:57 +00005327 // Make sure we have a parameter name to forward!
5328 if (!(*CurP)->getIdentifier())
Craig Topperc3ec1492014-05-26 06:22:03 +00005329 return nullptr;
Douglas Gregor6fc04132010-08-27 15:10:57 +00005330 }
5331
5332 // We have a superclass method. Now, form the send-to-super completion.
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005333 CodeCompletionBuilder Builder(Results.getAllocator(),
5334 Results.getCodeCompletionTUInfo());
Douglas Gregor6fc04132010-08-27 15:10:57 +00005335
5336 // Give this completion a return type.
Douglas Gregor75acd922011-09-27 23:30:47 +00005337 AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod,
5338 Builder);
Douglas Gregor6fc04132010-08-27 15:10:57 +00005339
5340 // If we need the "super" keyword, add it (plus some spacing).
5341 if (NeedSuperKeyword) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005342 Builder.AddTypedTextChunk("super");
5343 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregor6fc04132010-08-27 15:10:57 +00005344 }
5345
5346 Selector Sel = CurMethod->getSelector();
5347 if (Sel.isUnarySelector()) {
5348 if (NeedSuperKeyword)
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005349 Builder.AddTextChunk(Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005350 Sel.getNameForSlot(0)));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005351 else
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005352 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005353 Sel.getNameForSlot(0)));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005354 } else {
5355 ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin();
5356 for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005357 if (I > SelIdents.size())
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005358 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregor6fc04132010-08-27 15:10:57 +00005359
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005360 if (I < SelIdents.size())
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005361 Builder.AddInformativeChunk(
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005362 Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005363 Sel.getNameForSlot(I) + ":"));
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005364 else if (NeedSuperKeyword || I > SelIdents.size()) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005365 Builder.AddTextChunk(
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005366 Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005367 Sel.getNameForSlot(I) + ":"));
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005368 Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005369 (*CurP)->getIdentifier()->getName()));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005370 } else {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005371 Builder.AddTypedTextChunk(
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005372 Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005373 Sel.getNameForSlot(I) + ":"));
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005374 Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005375 (*CurP)->getIdentifier()->getName()));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005376 }
5377 }
5378 }
5379
Douglas Gregor78254c82012-03-27 23:34:16 +00005380 Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod,
5381 CCP_SuperCompletion));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005382 return SuperMethod;
5383}
5384
Douglas Gregora817a192010-05-27 23:06:34 +00005385void Sema::CodeCompleteObjCMessageReceiver(Scope *S) {
John McCall276321a2010-08-25 06:19:51 +00005386 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005387 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005388 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005389 CodeCompletionContext::CCC_ObjCMessageReceiver,
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005390 getLangOpts().CPlusPlus11
Douglas Gregord8c61782012-02-15 15:34:24 +00005391 ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture
5392 : &ResultBuilder::IsObjCMessageReceiver);
Douglas Gregora817a192010-05-27 23:06:34 +00005393
Douglas Gregora817a192010-05-27 23:06:34 +00005394 CodeCompletionDeclConsumer Consumer(Results, CurContext);
5395 Results.EnterNewScope();
Douglas Gregor39982192010-08-15 06:18:01 +00005396 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5397 CodeCompleter->includeGlobals());
Douglas Gregora817a192010-05-27 23:06:34 +00005398
5399 // If we are in an Objective-C method inside a class that has a superclass,
5400 // add "super" as an option.
5401 if (ObjCMethodDecl *Method = getCurMethodDecl())
5402 if (ObjCInterfaceDecl *Iface = Method->getClassInterface())
Douglas Gregor6fc04132010-08-27 15:10:57 +00005403 if (Iface->getSuperClass()) {
Douglas Gregora817a192010-05-27 23:06:34 +00005404 Results.AddResult(Result("super"));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005405
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005406 AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results);
Douglas Gregor6fc04132010-08-27 15:10:57 +00005407 }
Douglas Gregora817a192010-05-27 23:06:34 +00005408
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005409 if (getLangOpts().CPlusPlus11)
Douglas Gregord8c61782012-02-15 15:34:24 +00005410 addThisCompletion(*this, Results);
5411
Douglas Gregora817a192010-05-27 23:06:34 +00005412 Results.ExitScope();
5413
5414 if (CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00005415 AddMacroResults(PP, Results, false);
Douglas Gregor50832e02010-09-20 22:39:41 +00005416 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005417 Results.data(), Results.size());
Douglas Gregora817a192010-05-27 23:06:34 +00005418
5419}
5420
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005421void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005422 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005423 bool AtArgumentExpression) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005424 ObjCInterfaceDecl *CDecl = nullptr;
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005425 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5426 // Figure out which interface we're in.
5427 CDecl = CurMethod->getClassInterface();
5428 if (!CDecl)
5429 return;
5430
5431 // Find the superclass of this class.
5432 CDecl = CDecl->getSuperClass();
5433 if (!CDecl)
5434 return;
5435
5436 if (CurMethod->isInstanceMethod()) {
5437 // We are inside an instance method, which means that the message
5438 // send [super ...] is actually calling an instance method on the
Douglas Gregor392a84b2010-10-13 21:24:53 +00005439 // current object.
Craig Topperc3ec1492014-05-26 06:22:03 +00005440 return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005441 AtArgumentExpression,
Douglas Gregor392a84b2010-10-13 21:24:53 +00005442 CDecl);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005443 }
5444
5445 // Fall through to send to the superclass in CDecl.
5446 } else {
5447 // "super" may be the name of a type or variable. Figure out which
5448 // it is.
Argyrios Kyrtzidis3e56dd42013-03-14 22:56:43 +00005449 IdentifierInfo *Super = getSuperIdentifier();
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005450 NamedDecl *ND = LookupSingleName(S, Super, SuperLoc,
5451 LookupOrdinaryName);
5452 if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) {
5453 // "super" names an interface. Use it.
5454 } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) {
John McCall8b07ec22010-05-15 11:32:37 +00005455 if (const ObjCObjectType *Iface
5456 = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>())
5457 CDecl = Iface->getInterface();
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005458 } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) {
5459 // "super" names an unresolved type; we can't be more specific.
5460 } else {
5461 // Assume that "super" names some kind of value and parse that way.
5462 CXXScopeSpec SS;
Abramo Bagnara7945c982012-01-27 09:46:47 +00005463 SourceLocation TemplateKWLoc;
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005464 UnqualifiedId id;
5465 id.setIdentifier(Super, SuperLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00005466 ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id,
5467 false, false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005468 return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(),
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005469 SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005470 AtArgumentExpression);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005471 }
5472
5473 // Fall through
5474 }
5475
John McCallba7bf592010-08-24 05:47:05 +00005476 ParsedType Receiver;
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005477 if (CDecl)
John McCallba7bf592010-08-24 05:47:05 +00005478 Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005479 return CodeCompleteObjCClassMessage(S, Receiver, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005480 AtArgumentExpression,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005481 /*IsSuper=*/true);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005482}
5483
Douglas Gregor74661272010-09-21 00:03:25 +00005484/// \brief Given a set of code-completion results for the argument of a message
5485/// send, determine the preferred type (if any) for that argument expression.
5486static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results,
5487 unsigned NumSelIdents) {
5488 typedef CodeCompletionResult Result;
5489 ASTContext &Context = Results.getSema().Context;
5490
5491 QualType PreferredType;
5492 unsigned BestPriority = CCP_Unlikely * 2;
5493 Result *ResultsData = Results.data();
5494 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
5495 Result &R = ResultsData[I];
5496 if (R.Kind == Result::RK_Declaration &&
5497 isa<ObjCMethodDecl>(R.Declaration)) {
5498 if (R.Priority <= BestPriority) {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00005499 const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration);
Douglas Gregor74661272010-09-21 00:03:25 +00005500 if (NumSelIdents <= Method->param_size()) {
Alp Toker03376dc2014-07-07 09:02:20 +00005501 QualType MyPreferredType = Method->parameters()[NumSelIdents - 1]
Douglas Gregor74661272010-09-21 00:03:25 +00005502 ->getType();
5503 if (R.Priority < BestPriority || PreferredType.isNull()) {
5504 BestPriority = R.Priority;
5505 PreferredType = MyPreferredType;
5506 } else if (!Context.hasSameUnqualifiedType(PreferredType,
5507 MyPreferredType)) {
5508 PreferredType = QualType();
5509 }
5510 }
5511 }
5512 }
5513 }
5514
5515 return PreferredType;
5516}
5517
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005518static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
5519 ParsedType Receiver,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005520 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005521 bool AtArgumentExpression,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005522 bool IsSuper,
5523 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00005524 typedef CodeCompletionResult Result;
Craig Topperc3ec1492014-05-26 06:22:03 +00005525 ObjCInterfaceDecl *CDecl = nullptr;
5526
Douglas Gregor8ce33212009-11-17 17:59:40 +00005527 // If the given name refers to an interface type, retrieve the
5528 // corresponding declaration.
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005529 if (Receiver) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005530 QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005531 if (!T.isNull())
John McCall8b07ec22010-05-15 11:32:37 +00005532 if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>())
5533 CDecl = Interface->getInterface();
Douglas Gregor8ce33212009-11-17 17:59:40 +00005534 }
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005535
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005536 // Add all of the factory methods in this Objective-C class, its protocols,
5537 // superclasses, categories, implementation, etc.
Steve Naroffeae65032009-11-07 02:08:14 +00005538 Results.EnterNewScope();
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005539
Douglas Gregor6fc04132010-08-27 15:10:57 +00005540 // If this is a send-to-super, try to add the special "super" send
5541 // completion.
5542 if (IsSuper) {
5543 if (ObjCMethodDecl *SuperMethod
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005544 = AddSuperSendCompletion(SemaRef, false, SelIdents, Results))
Douglas Gregor6fc04132010-08-27 15:10:57 +00005545 Results.Ignore(SuperMethod);
5546 }
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005547
Douglas Gregorc2cb2e22010-08-27 15:29:55 +00005548 // If we're inside an Objective-C method definition, prefer its selector to
5549 // others.
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005550 if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl())
Douglas Gregorc2cb2e22010-08-27 15:29:55 +00005551 Results.setPreferredSelector(CurMethod->getSelector());
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005552
Douglas Gregor1154e272010-09-16 16:06:31 +00005553 VisitedSelectorSet Selectors;
Douglas Gregor6285f752010-04-06 16:40:00 +00005554 if (CDecl)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005555 AddObjCMethods(CDecl, false, MK_Any, SelIdents,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005556 SemaRef.CurContext, Selectors, AtArgumentExpression,
5557 Results);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005558 else {
Douglas Gregor6285f752010-04-06 16:40:00 +00005559 // We're messaging "id" as a type; provide all class/factory methods.
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005560
Douglas Gregord720daf2010-04-06 17:30:22 +00005561 // If we have an external source, load the entire class method
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005562 // pool from the AST file.
Axel Naumanndd433f02012-10-18 19:05:02 +00005563 if (SemaRef.getExternalSource()) {
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005564 for (uint32_t I = 0,
Axel Naumanndd433f02012-10-18 19:05:02 +00005565 N = SemaRef.getExternalSource()->GetNumExternalSelectors();
John McCall75b960e2010-06-01 09:23:16 +00005566 I != N; ++I) {
Axel Naumanndd433f02012-10-18 19:05:02 +00005567 Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I);
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005568 if (Sel.isNull() || SemaRef.MethodPool.count(Sel))
Douglas Gregord720daf2010-04-06 17:30:22 +00005569 continue;
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005570
5571 SemaRef.ReadMethodPool(Sel);
Douglas Gregord720daf2010-04-06 17:30:22 +00005572 }
5573 }
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005574
5575 for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(),
5576 MEnd = SemaRef.MethodPool.end();
Sebastian Redl75d8a322010-08-02 23:18:59 +00005577 M != MEnd; ++M) {
5578 for (ObjCMethodList *MethList = &M->second.second;
Nico Weber2e0c8f72014-12-27 03:58:08 +00005579 MethList && MethList->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00005580 MethList = MethList->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00005581 if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
Douglas Gregor6285f752010-04-06 16:40:00 +00005582 continue;
Craig Topperc3ec1492014-05-26 06:22:03 +00005583
Nico Weber2e0c8f72014-12-27 03:58:08 +00005584 Result R(MethList->getMethod(),
5585 Results.getBasePriority(MethList->getMethod()), nullptr);
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005586 R.StartParameter = SelIdents.size();
Douglas Gregor6285f752010-04-06 16:40:00 +00005587 R.AllParametersAreInformative = false;
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005588 Results.MaybeAddResult(R, SemaRef.CurContext);
Douglas Gregor6285f752010-04-06 16:40:00 +00005589 }
5590 }
5591 }
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005592
5593 Results.ExitScope();
5594}
Douglas Gregor6285f752010-04-06 16:40:00 +00005595
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005596void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005597 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005598 bool AtArgumentExpression,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005599 bool IsSuper) {
Douglas Gregor63745d52011-07-21 01:05:26 +00005600
5601 QualType T = this->GetTypeFromParser(Receiver);
5602
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005603 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005604 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor63745d52011-07-21 01:05:26 +00005605 CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005606 T, SelIdents));
Douglas Gregor63745d52011-07-21 01:05:26 +00005607
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005608 AddClassMessageCompletions(*this, S, Receiver, SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005609 AtArgumentExpression, IsSuper, Results);
Douglas Gregor74661272010-09-21 00:03:25 +00005610
5611 // If we're actually at the argument expression (rather than prior to the
5612 // selector), we're actually performing code completion for an expression.
5613 // Determine whether we have a single, best method. If so, we can
5614 // code-complete the expression using the corresponding parameter type as
5615 // our preferred type, improving completion results.
5616 if (AtArgumentExpression) {
5617 QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005618 SelIdents.size());
Douglas Gregor74661272010-09-21 00:03:25 +00005619 if (PreferredType.isNull())
5620 CodeCompleteOrdinaryName(S, PCC_Expression);
5621 else
5622 CodeCompleteExpression(S, PreferredType);
5623 return;
5624 }
5625
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005626 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor63745d52011-07-21 01:05:26 +00005627 Results.getCompletionContext(),
Douglas Gregor6fc04132010-08-27 15:10:57 +00005628 Results.data(), Results.size());
Steve Naroffeae65032009-11-07 02:08:14 +00005629}
5630
Richard Trieu2bd04012011-09-09 02:00:50 +00005631void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005632 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005633 bool AtArgumentExpression,
Douglas Gregor392a84b2010-10-13 21:24:53 +00005634 ObjCInterfaceDecl *Super) {
John McCall276321a2010-08-25 06:19:51 +00005635 typedef CodeCompletionResult Result;
Steve Naroffeae65032009-11-07 02:08:14 +00005636
5637 Expr *RecExpr = static_cast<Expr *>(Receiver);
Steve Naroffeae65032009-11-07 02:08:14 +00005638
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005639 // If necessary, apply function/array conversion to the receiver.
5640 // C99 6.7.5.3p[7,8].
John Wiegley01296292011-04-08 18:41:53 +00005641 if (RecExpr) {
5642 ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr);
5643 if (Conv.isInvalid()) // conversion failed. bail.
5644 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005645 RecExpr = Conv.get();
John Wiegley01296292011-04-08 18:41:53 +00005646 }
Douglas Gregor392a84b2010-10-13 21:24:53 +00005647 QualType ReceiverType = RecExpr? RecExpr->getType()
5648 : Super? Context.getObjCObjectPointerType(
5649 Context.getObjCInterfaceType(Super))
5650 : Context.getObjCIdType();
Steve Naroffeae65032009-11-07 02:08:14 +00005651
Douglas Gregordc520b02010-11-08 21:12:30 +00005652 // If we're messaging an expression with type "id" or "Class", check
5653 // whether we know something special about the receiver that allows
5654 // us to assume a more-specific receiver type.
Anders Carlsson382ba412014-02-28 19:07:22 +00005655 if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) {
Douglas Gregordc520b02010-11-08 21:12:30 +00005656 if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) {
5657 if (ReceiverType->isObjCClassType())
5658 return CodeCompleteObjCClassMessage(S,
5659 ParsedType::make(Context.getObjCInterfaceType(IFace)),
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005660 SelIdents,
Douglas Gregordc520b02010-11-08 21:12:30 +00005661 AtArgumentExpression, Super);
5662
5663 ReceiverType = Context.getObjCObjectPointerType(
5664 Context.getObjCInterfaceType(IFace));
5665 }
Anders Carlsson382ba412014-02-28 19:07:22 +00005666 } else if (RecExpr && getLangOpts().CPlusPlus) {
5667 ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr);
5668 if (Conv.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005669 RecExpr = Conv.get();
Anders Carlsson382ba412014-02-28 19:07:22 +00005670 ReceiverType = RecExpr->getType();
5671 }
5672 }
Douglas Gregordc520b02010-11-08 21:12:30 +00005673
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005674 // Build the set of methods we can see.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005675 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005676 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor63745d52011-07-21 01:05:26 +00005677 CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005678 ReceiverType, SelIdents));
Douglas Gregor63745d52011-07-21 01:05:26 +00005679
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005680 Results.EnterNewScope();
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005681
Douglas Gregor6fc04132010-08-27 15:10:57 +00005682 // If this is a send-to-super, try to add the special "super" send
5683 // completion.
Douglas Gregor392a84b2010-10-13 21:24:53 +00005684 if (Super) {
Douglas Gregor6fc04132010-08-27 15:10:57 +00005685 if (ObjCMethodDecl *SuperMethod
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005686 = AddSuperSendCompletion(*this, false, SelIdents, Results))
Douglas Gregor6fc04132010-08-27 15:10:57 +00005687 Results.Ignore(SuperMethod);
5688 }
5689
Douglas Gregorc2cb2e22010-08-27 15:29:55 +00005690 // If we're inside an Objective-C method definition, prefer its selector to
5691 // others.
5692 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
5693 Results.setPreferredSelector(CurMethod->getSelector());
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005694
Douglas Gregor1154e272010-09-16 16:06:31 +00005695 // Keep track of the selectors we've already added.
5696 VisitedSelectorSet Selectors;
5697
Douglas Gregora3329fa2009-11-18 00:06:18 +00005698 // Handle messages to Class. This really isn't a message to an instance
5699 // method, so we treat it the same way we would treat a message send to a
5700 // class method.
5701 if (ReceiverType->isObjCClassType() ||
5702 ReceiverType->isObjCQualifiedClassType()) {
5703 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5704 if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005705 AddObjCMethods(ClassDecl, false, MK_Any, SelIdents,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005706 CurContext, Selectors, AtArgumentExpression, Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00005707 }
5708 }
5709 // Handle messages to a qualified ID ("id<foo>").
5710 else if (const ObjCObjectPointerType *QualID
5711 = ReceiverType->getAsObjCQualifiedIdType()) {
5712 // Search protocols for instance methods.
Aaron Ballman83731462014-03-17 16:14:00 +00005713 for (auto *I : QualID->quals())
5714 AddObjCMethods(I, true, MK_Any, SelIdents, CurContext,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005715 Selectors, AtArgumentExpression, Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00005716 }
5717 // Handle messages to a pointer to interface type.
5718 else if (const ObjCObjectPointerType *IFacePtr
5719 = ReceiverType->getAsObjCInterfacePointerType()) {
5720 // Search the class, its superclasses, etc., for instance methods.
Douglas Gregorc8537c52009-11-19 07:41:15 +00005721 AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005722 CurContext, Selectors, AtArgumentExpression,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005723 Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00005724
5725 // Search protocols for instance methods.
Aaron Ballman83731462014-03-17 16:14:00 +00005726 for (auto *I : IFacePtr->quals())
5727 AddObjCMethods(I, true, MK_Any, SelIdents, CurContext,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005728 Selectors, AtArgumentExpression, Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00005729 }
Douglas Gregor6285f752010-04-06 16:40:00 +00005730 // Handle messages to "id".
5731 else if (ReceiverType->isObjCIdType()) {
Douglas Gregord720daf2010-04-06 17:30:22 +00005732 // We're messaging "id", so provide all instance methods we know
5733 // about as code-completion results.
5734
5735 // If we have an external source, load the entire class method
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005736 // pool from the AST file.
Douglas Gregord720daf2010-04-06 17:30:22 +00005737 if (ExternalSource) {
John McCall75b960e2010-06-01 09:23:16 +00005738 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5739 I != N; ++I) {
5740 Selector Sel = ExternalSource->GetExternalSelector(I);
Sebastian Redl75d8a322010-08-02 23:18:59 +00005741 if (Sel.isNull() || MethodPool.count(Sel))
Douglas Gregord720daf2010-04-06 17:30:22 +00005742 continue;
5743
Sebastian Redl75d8a322010-08-02 23:18:59 +00005744 ReadMethodPool(Sel);
Douglas Gregord720daf2010-04-06 17:30:22 +00005745 }
5746 }
5747
Sebastian Redl75d8a322010-08-02 23:18:59 +00005748 for (GlobalMethodPool::iterator M = MethodPool.begin(),
5749 MEnd = MethodPool.end();
5750 M != MEnd; ++M) {
5751 for (ObjCMethodList *MethList = &M->second.first;
Nico Weber2e0c8f72014-12-27 03:58:08 +00005752 MethList && MethList->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00005753 MethList = MethList->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00005754 if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
Douglas Gregor6285f752010-04-06 16:40:00 +00005755 continue;
Douglas Gregor1154e272010-09-16 16:06:31 +00005756
Nico Weber2e0c8f72014-12-27 03:58:08 +00005757 if (!Selectors.insert(MethList->getMethod()->getSelector()).second)
Douglas Gregor1154e272010-09-16 16:06:31 +00005758 continue;
Craig Topperc3ec1492014-05-26 06:22:03 +00005759
Nico Weber2e0c8f72014-12-27 03:58:08 +00005760 Result R(MethList->getMethod(),
5761 Results.getBasePriority(MethList->getMethod()), nullptr);
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005762 R.StartParameter = SelIdents.size();
Douglas Gregor6285f752010-04-06 16:40:00 +00005763 R.AllParametersAreInformative = false;
5764 Results.MaybeAddResult(R, CurContext);
5765 }
5766 }
5767 }
Steve Naroffeae65032009-11-07 02:08:14 +00005768 Results.ExitScope();
Douglas Gregor74661272010-09-21 00:03:25 +00005769
5770
5771 // If we're actually at the argument expression (rather than prior to the
5772 // selector), we're actually performing code completion for an expression.
5773 // Determine whether we have a single, best method. If so, we can
5774 // code-complete the expression using the corresponding parameter type as
5775 // our preferred type, improving completion results.
5776 if (AtArgumentExpression) {
5777 QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005778 SelIdents.size());
Douglas Gregor74661272010-09-21 00:03:25 +00005779 if (PreferredType.isNull())
5780 CodeCompleteOrdinaryName(S, PCC_Expression);
5781 else
5782 CodeCompleteExpression(S, PreferredType);
5783 return;
5784 }
5785
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005786 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor63745d52011-07-21 01:05:26 +00005787 Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005788 Results.data(),Results.size());
Steve Naroffeae65032009-11-07 02:08:14 +00005789}
Douglas Gregorbaf69612009-11-18 04:19:12 +00005790
Douglas Gregor68762e72010-08-23 21:17:50 +00005791void Sema::CodeCompleteObjCForCollection(Scope *S,
5792 DeclGroupPtrTy IterationVar) {
5793 CodeCompleteExpressionData Data;
5794 Data.ObjCCollection = true;
5795
5796 if (IterationVar.getAsOpaquePtr()) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00005797 DeclGroupRef DG = IterationVar.get();
Douglas Gregor68762e72010-08-23 21:17:50 +00005798 for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) {
5799 if (*I)
5800 Data.IgnoreDecls.push_back(*I);
5801 }
5802 }
5803
5804 CodeCompleteExpression(S, Data);
5805}
5806
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005807void Sema::CodeCompleteObjCSelector(Scope *S,
5808 ArrayRef<IdentifierInfo *> SelIdents) {
Douglas Gregor67c692c2010-08-26 15:07:07 +00005809 // If we have an external source, load the entire class method
5810 // pool from the AST file.
5811 if (ExternalSource) {
5812 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5813 I != N; ++I) {
5814 Selector Sel = ExternalSource->GetExternalSelector(I);
5815 if (Sel.isNull() || MethodPool.count(Sel))
5816 continue;
5817
5818 ReadMethodPool(Sel);
5819 }
5820 }
5821
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005822 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005823 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005824 CodeCompletionContext::CCC_SelectorName);
Douglas Gregor67c692c2010-08-26 15:07:07 +00005825 Results.EnterNewScope();
5826 for (GlobalMethodPool::iterator M = MethodPool.begin(),
5827 MEnd = MethodPool.end();
5828 M != MEnd; ++M) {
5829
5830 Selector Sel = M->first;
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005831 if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents))
Douglas Gregor67c692c2010-08-26 15:07:07 +00005832 continue;
5833
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005834 CodeCompletionBuilder Builder(Results.getAllocator(),
5835 Results.getCodeCompletionTUInfo());
Douglas Gregor67c692c2010-08-26 15:07:07 +00005836 if (Sel.isUnarySelector()) {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005837 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005838 Sel.getNameForSlot(0)));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005839 Results.AddResult(Builder.TakeString());
Douglas Gregor67c692c2010-08-26 15:07:07 +00005840 continue;
5841 }
5842
Douglas Gregor9ac1ad12010-08-26 16:46:39 +00005843 std::string Accumulator;
Douglas Gregor67c692c2010-08-26 15:07:07 +00005844 for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005845 if (I == SelIdents.size()) {
Douglas Gregor9ac1ad12010-08-26 16:46:39 +00005846 if (!Accumulator.empty()) {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005847 Builder.AddInformativeChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005848 Accumulator));
Douglas Gregor9ac1ad12010-08-26 16:46:39 +00005849 Accumulator.clear();
5850 }
5851 }
5852
Benjamin Kramer632500c2011-07-26 16:59:25 +00005853 Accumulator += Sel.getNameForSlot(I);
Douglas Gregor9ac1ad12010-08-26 16:46:39 +00005854 Accumulator += ':';
Douglas Gregor67c692c2010-08-26 15:07:07 +00005855 }
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005856 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005857 Results.AddResult(Builder.TakeString());
Douglas Gregor67c692c2010-08-26 15:07:07 +00005858 }
5859 Results.ExitScope();
5860
5861 HandleCodeCompleteResults(this, CodeCompleter,
5862 CodeCompletionContext::CCC_SelectorName,
5863 Results.data(), Results.size());
5864}
5865
Douglas Gregorbaf69612009-11-18 04:19:12 +00005866/// \brief Add all of the protocol declarations that we find in the given
5867/// (translation unit) context.
5868static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext,
Douglas Gregor5b4671c2009-11-18 04:49:41 +00005869 bool OnlyForwardDeclarations,
Douglas Gregorbaf69612009-11-18 04:19:12 +00005870 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00005871 typedef CodeCompletionResult Result;
Douglas Gregorbaf69612009-11-18 04:19:12 +00005872
Aaron Ballman629afae2014-03-07 19:56:05 +00005873 for (const auto *D : Ctx->decls()) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00005874 // Record any protocols we find.
Aaron Ballman629afae2014-03-07 19:56:05 +00005875 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D))
Douglas Gregore6e48b12012-01-01 19:29:29 +00005876 if (!OnlyForwardDeclarations || !Proto->hasDefinition())
Craig Topperc3ec1492014-05-26 06:22:03 +00005877 Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr),
5878 CurContext, nullptr, false);
Douglas Gregorbaf69612009-11-18 04:19:12 +00005879 }
5880}
5881
5882void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
5883 unsigned NumProtocols) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005884 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005885 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005886 CodeCompletionContext::CCC_ObjCProtocolName);
Douglas Gregorbaf69612009-11-18 04:19:12 +00005887
Douglas Gregora3b23b02010-12-09 21:44:02 +00005888 if (CodeCompleter && CodeCompleter->includeGlobals()) {
5889 Results.EnterNewScope();
5890
5891 // Tell the result set to ignore all of the protocols we have
5892 // already seen.
5893 // FIXME: This doesn't work when caching code-completion results.
5894 for (unsigned I = 0; I != NumProtocols; ++I)
5895 if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first,
5896 Protocols[I].second))
5897 Results.Ignore(Protocol);
Douglas Gregorbaf69612009-11-18 04:19:12 +00005898
Douglas Gregora3b23b02010-12-09 21:44:02 +00005899 // Add all protocols.
5900 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false,
5901 Results);
Douglas Gregor5b4671c2009-11-18 04:49:41 +00005902
Douglas Gregora3b23b02010-12-09 21:44:02 +00005903 Results.ExitScope();
5904 }
5905
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005906 HandleCodeCompleteResults(this, CodeCompleter,
5907 CodeCompletionContext::CCC_ObjCProtocolName,
5908 Results.data(),Results.size());
Douglas Gregor5b4671c2009-11-18 04:49:41 +00005909}
5910
5911void Sema::CodeCompleteObjCProtocolDecl(Scope *) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005912 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005913 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005914 CodeCompletionContext::CCC_ObjCProtocolName);
Douglas Gregor5b4671c2009-11-18 04:49:41 +00005915
Douglas Gregora3b23b02010-12-09 21:44:02 +00005916 if (CodeCompleter && CodeCompleter->includeGlobals()) {
5917 Results.EnterNewScope();
5918
5919 // Add all protocols.
5920 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true,
5921 Results);
Douglas Gregorbaf69612009-11-18 04:19:12 +00005922
Douglas Gregora3b23b02010-12-09 21:44:02 +00005923 Results.ExitScope();
5924 }
5925
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005926 HandleCodeCompleteResults(this, CodeCompleter,
5927 CodeCompletionContext::CCC_ObjCProtocolName,
5928 Results.data(),Results.size());
Douglas Gregorbaf69612009-11-18 04:19:12 +00005929}
Douglas Gregor49c22a72009-11-18 16:26:39 +00005930
5931/// \brief Add all of the Objective-C interface declarations that we find in
5932/// the given (translation unit) context.
5933static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext,
5934 bool OnlyForwardDeclarations,
5935 bool OnlyUnimplemented,
5936 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00005937 typedef CodeCompletionResult Result;
Douglas Gregor49c22a72009-11-18 16:26:39 +00005938
Aaron Ballman629afae2014-03-07 19:56:05 +00005939 for (const auto *D : Ctx->decls()) {
Douglas Gregor1c283312010-08-11 12:19:30 +00005940 // Record any interfaces we find.
Aaron Ballman629afae2014-03-07 19:56:05 +00005941 if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Douglas Gregordc9166c2011-12-15 20:29:51 +00005942 if ((!OnlyForwardDeclarations || !Class->hasDefinition()) &&
Douglas Gregor1c283312010-08-11 12:19:30 +00005943 (!OnlyUnimplemented || !Class->getImplementation()))
Craig Topperc3ec1492014-05-26 06:22:03 +00005944 Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr),
5945 CurContext, nullptr, false);
Douglas Gregor49c22a72009-11-18 16:26:39 +00005946 }
5947}
5948
5949void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005950 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005951 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005952 CodeCompletionContext::CCC_Other);
Douglas Gregor49c22a72009-11-18 16:26:39 +00005953 Results.EnterNewScope();
5954
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005955 if (CodeCompleter->includeGlobals()) {
5956 // Add all classes.
5957 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5958 false, Results);
5959 }
5960
Douglas Gregor49c22a72009-11-18 16:26:39 +00005961 Results.ExitScope();
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005962
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005963 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005964 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005965 Results.data(),Results.size());
Douglas Gregor49c22a72009-11-18 16:26:39 +00005966}
5967
Douglas Gregorb2ccf012010-04-15 22:33:43 +00005968void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName,
5969 SourceLocation ClassNameLoc) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005970 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005971 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005972 CodeCompletionContext::CCC_ObjCInterfaceName);
Douglas Gregor49c22a72009-11-18 16:26:39 +00005973 Results.EnterNewScope();
5974
5975 // Make sure that we ignore the class we're currently defining.
5976 NamedDecl *CurClass
Douglas Gregorb2ccf012010-04-15 22:33:43 +00005977 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00005978 if (CurClass && isa<ObjCInterfaceDecl>(CurClass))
Douglas Gregor49c22a72009-11-18 16:26:39 +00005979 Results.Ignore(CurClass);
5980
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005981 if (CodeCompleter->includeGlobals()) {
5982 // Add all classes.
5983 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5984 false, Results);
5985 }
5986
Douglas Gregor49c22a72009-11-18 16:26:39 +00005987 Results.ExitScope();
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005988
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005989 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005990 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005991 Results.data(),Results.size());
Douglas Gregor49c22a72009-11-18 16:26:39 +00005992}
5993
5994void Sema::CodeCompleteObjCImplementationDecl(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005995 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005996 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005997 CodeCompletionContext::CCC_Other);
Douglas Gregor49c22a72009-11-18 16:26:39 +00005998 Results.EnterNewScope();
5999
Douglas Gregor2c595ad2011-07-30 06:55:39 +00006000 if (CodeCompleter->includeGlobals()) {
6001 // Add all unimplemented classes.
6002 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
6003 true, Results);
6004 }
6005
Douglas Gregor49c22a72009-11-18 16:26:39 +00006006 Results.ExitScope();
Douglas Gregor2c595ad2011-07-30 06:55:39 +00006007
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006008 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor2c595ad2011-07-30 06:55:39 +00006009 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006010 Results.data(),Results.size());
Douglas Gregor49c22a72009-11-18 16:26:39 +00006011}
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006012
6013void Sema::CodeCompleteObjCInterfaceCategory(Scope *S,
Douglas Gregorb2ccf012010-04-15 22:33:43 +00006014 IdentifierInfo *ClassName,
6015 SourceLocation ClassNameLoc) {
John McCall276321a2010-08-25 06:19:51 +00006016 typedef CodeCompletionResult Result;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006017
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006018 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006019 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor21325842011-07-07 16:03:39 +00006020 CodeCompletionContext::CCC_ObjCCategoryName);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006021
6022 // Ignore any categories we find that have already been implemented by this
6023 // interface.
6024 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
6025 NamedDecl *CurClass
Douglas Gregorb2ccf012010-04-15 22:33:43 +00006026 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006027 if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){
Aaron Ballman3fe486a2014-03-13 21:23:55 +00006028 for (const auto *Cat : Class->visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006029 CategoryNames.insert(Cat->getIdentifier());
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006030 }
6031
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006032 // Add all of the categories we know about.
6033 Results.EnterNewScope();
6034 TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Aaron Ballman629afae2014-03-07 19:56:05 +00006035 for (const auto *D : TU->decls())
6036 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D))
David Blaikie82e95a32014-11-19 07:49:47 +00006037 if (CategoryNames.insert(Category->getIdentifier()).second)
Craig Topperc3ec1492014-05-26 06:22:03 +00006038 Results.AddResult(Result(Category, Results.getBasePriority(Category),
6039 nullptr),
6040 CurContext, nullptr, false);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006041 Results.ExitScope();
6042
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006043 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor21325842011-07-07 16:03:39 +00006044 CodeCompletionContext::CCC_ObjCCategoryName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006045 Results.data(),Results.size());
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006046}
6047
6048void Sema::CodeCompleteObjCImplementationCategory(Scope *S,
Douglas Gregorb2ccf012010-04-15 22:33:43 +00006049 IdentifierInfo *ClassName,
6050 SourceLocation ClassNameLoc) {
John McCall276321a2010-08-25 06:19:51 +00006051 typedef CodeCompletionResult Result;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006052
6053 // Find the corresponding interface. If we couldn't find the interface, the
6054 // program itself is ill-formed. However, we'll try to be helpful still by
6055 // providing the list of all of the categories we know about.
6056 NamedDecl *CurClass
Douglas Gregorb2ccf012010-04-15 22:33:43 +00006057 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006058 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass);
6059 if (!Class)
Douglas Gregorb2ccf012010-04-15 22:33:43 +00006060 return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006061
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006062 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006063 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor21325842011-07-07 16:03:39 +00006064 CodeCompletionContext::CCC_ObjCCategoryName);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006065
6066 // Add all of the categories that have have corresponding interface
6067 // declarations in this class and any of its superclasses, except for
6068 // already-implemented categories in the class itself.
6069 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
6070 Results.EnterNewScope();
6071 bool IgnoreImplemented = true;
6072 while (Class) {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00006073 for (const auto *Cat : Class->visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006074 if ((!IgnoreImplemented || !Cat->getImplementation()) &&
David Blaikie82e95a32014-11-19 07:49:47 +00006075 CategoryNames.insert(Cat->getIdentifier()).second)
Craig Topperc3ec1492014-05-26 06:22:03 +00006076 Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr),
6077 CurContext, nullptr, false);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006078 }
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006079
6080 Class = Class->getSuperClass();
6081 IgnoreImplemented = false;
6082 }
6083 Results.ExitScope();
6084
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006085 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor21325842011-07-07 16:03:39 +00006086 CodeCompletionContext::CCC_ObjCCategoryName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006087 Results.data(),Results.size());
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006088}
Douglas Gregor5d649882009-11-18 22:32:06 +00006089
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006090void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006091 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006092 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006093 CodeCompletionContext::CCC_Other);
Douglas Gregor5d649882009-11-18 22:32:06 +00006094
6095 // Figure out where this @synthesize lives.
6096 ObjCContainerDecl *Container
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006097 = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
Douglas Gregor5d649882009-11-18 22:32:06 +00006098 if (!Container ||
6099 (!isa<ObjCImplementationDecl>(Container) &&
6100 !isa<ObjCCategoryImplDecl>(Container)))
6101 return;
6102
6103 // Ignore any properties that have already been implemented.
Douglas Gregor9b4f3702012-06-12 13:44:08 +00006104 Container = getContainerDef(Container);
Aaron Ballman629afae2014-03-07 19:56:05 +00006105 for (const auto *D : Container->decls())
6106 if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D))
Douglas Gregor5d649882009-11-18 22:32:06 +00006107 Results.Ignore(PropertyImpl->getPropertyDecl());
6108
6109 // Add any properties that we find.
Douglas Gregorb888acf2010-12-09 23:01:55 +00006110 AddedPropertiesSet AddedProperties;
Douglas Gregor5d649882009-11-18 22:32:06 +00006111 Results.EnterNewScope();
6112 if (ObjCImplementationDecl *ClassImpl
6113 = dyn_cast<ObjCImplementationDecl>(Container))
Douglas Gregor95147142011-05-05 15:50:42 +00006114 AddObjCProperties(ClassImpl->getClassInterface(), false,
6115 /*AllowNullaryMethods=*/false, CurContext,
Douglas Gregorb888acf2010-12-09 23:01:55 +00006116 AddedProperties, Results);
Douglas Gregor5d649882009-11-18 22:32:06 +00006117 else
6118 AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(),
Douglas Gregor95147142011-05-05 15:50:42 +00006119 false, /*AllowNullaryMethods=*/false, CurContext,
6120 AddedProperties, Results);
Douglas Gregor5d649882009-11-18 22:32:06 +00006121 Results.ExitScope();
6122
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006123 HandleCodeCompleteResults(this, CodeCompleter,
6124 CodeCompletionContext::CCC_Other,
6125 Results.data(),Results.size());
Douglas Gregor5d649882009-11-18 22:32:06 +00006126}
6127
6128void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006129 IdentifierInfo *PropertyName) {
John McCall276321a2010-08-25 06:19:51 +00006130 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006131 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006132 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006133 CodeCompletionContext::CCC_Other);
Douglas Gregor5d649882009-11-18 22:32:06 +00006134
6135 // Figure out where this @synthesize lives.
6136 ObjCContainerDecl *Container
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006137 = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
Douglas Gregor5d649882009-11-18 22:32:06 +00006138 if (!Container ||
6139 (!isa<ObjCImplementationDecl>(Container) &&
6140 !isa<ObjCCategoryImplDecl>(Container)))
6141 return;
6142
6143 // Figure out which interface we're looking into.
Craig Topperc3ec1492014-05-26 06:22:03 +00006144 ObjCInterfaceDecl *Class = nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00006145 if (ObjCImplementationDecl *ClassImpl
6146 = dyn_cast<ObjCImplementationDecl>(Container))
6147 Class = ClassImpl->getClassInterface();
6148 else
6149 Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
6150 ->getClassInterface();
6151
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006152 // Determine the type of the property we're synthesizing.
6153 QualType PropertyType = Context.getObjCIdType();
6154 if (Class) {
6155 if (ObjCPropertyDecl *Property
6156 = Class->FindPropertyDeclaration(PropertyName)) {
6157 PropertyType
6158 = Property->getType().getNonReferenceType().getUnqualifiedType();
6159
6160 // Give preference to ivars
6161 Results.setPreferredType(PropertyType);
6162 }
6163 }
6164
Douglas Gregor5d649882009-11-18 22:32:06 +00006165 // Add all of the instance variables in this class and its superclasses.
6166 Results.EnterNewScope();
Douglas Gregor331faa02011-04-18 14:13:53 +00006167 bool SawSimilarlyNamedIvar = false;
6168 std::string NameWithPrefix;
6169 NameWithPrefix += '_';
Benjamin Kramer632500c2011-07-26 16:59:25 +00006170 NameWithPrefix += PropertyName->getName();
Douglas Gregor331faa02011-04-18 14:13:53 +00006171 std::string NameWithSuffix = PropertyName->getName().str();
6172 NameWithSuffix += '_';
Douglas Gregor5d649882009-11-18 22:32:06 +00006173 for(; Class; Class = Class->getSuperClass()) {
Douglas Gregor331faa02011-04-18 14:13:53 +00006174 for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar;
6175 Ivar = Ivar->getNextIvar()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006176 Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr),
6177 CurContext, nullptr, false);
6178
Douglas Gregor331faa02011-04-18 14:13:53 +00006179 // Determine whether we've seen an ivar with a name similar to the
6180 // property.
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006181 if ((PropertyName == Ivar->getIdentifier() ||
Douglas Gregor331faa02011-04-18 14:13:53 +00006182 NameWithPrefix == Ivar->getName() ||
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006183 NameWithSuffix == Ivar->getName())) {
Douglas Gregor331faa02011-04-18 14:13:53 +00006184 SawSimilarlyNamedIvar = true;
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006185
6186 // Reduce the priority of this result by one, to give it a slight
6187 // advantage over other results whose names don't match so closely.
6188 if (Results.size() &&
6189 Results.data()[Results.size() - 1].Kind
6190 == CodeCompletionResult::RK_Declaration &&
6191 Results.data()[Results.size() - 1].Declaration == Ivar)
6192 Results.data()[Results.size() - 1].Priority--;
6193 }
Douglas Gregor331faa02011-04-18 14:13:53 +00006194 }
Douglas Gregor5d649882009-11-18 22:32:06 +00006195 }
Douglas Gregor331faa02011-04-18 14:13:53 +00006196
6197 if (!SawSimilarlyNamedIvar) {
6198 // Create ivar result _propName, that the user can use to synthesize
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006199 // an ivar of the appropriate type.
6200 unsigned Priority = CCP_MemberDeclaration + 1;
Douglas Gregor331faa02011-04-18 14:13:53 +00006201 typedef CodeCompletionResult Result;
6202 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006203 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(),
6204 Priority,CXAvailability_Available);
Douglas Gregor331faa02011-04-18 14:13:53 +00006205
Douglas Gregor75acd922011-09-27 23:30:47 +00006206 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006207 Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context,
Douglas Gregor75acd922011-09-27 23:30:47 +00006208 Policy, Allocator));
Douglas Gregor331faa02011-04-18 14:13:53 +00006209 Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix));
6210 Results.AddResult(Result(Builder.TakeString(), Priority,
6211 CXCursor_ObjCIvarDecl));
6212 }
6213
Douglas Gregor5d649882009-11-18 22:32:06 +00006214 Results.ExitScope();
6215
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006216 HandleCodeCompleteResults(this, CodeCompleter,
6217 CodeCompletionContext::CCC_Other,
6218 Results.data(),Results.size());
Douglas Gregor5d649882009-11-18 22:32:06 +00006219}
Douglas Gregor636a61e2010-04-07 00:21:17 +00006220
Douglas Gregor416b5752010-08-25 01:08:01 +00006221// Mapping from selectors to the methods that implement that selector, along
6222// with the "in original class" flag.
Benjamin Kramereb8c4462013-06-29 17:52:13 +00006223typedef llvm::DenseMap<
6224 Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap;
Douglas Gregor636a61e2010-04-07 00:21:17 +00006225
6226/// \brief Find all of the methods that reside in the given container
6227/// (and its superclasses, protocols, etc.) that meet the given
6228/// criteria. Insert those methods into the map of known methods,
6229/// indexed by selector so they can be easily found.
6230static void FindImplementableMethods(ASTContext &Context,
6231 ObjCContainerDecl *Container,
6232 bool WantInstanceMethods,
6233 QualType ReturnType,
Douglas Gregor416b5752010-08-25 01:08:01 +00006234 KnownMethodsMap &KnownMethods,
6235 bool InOriginalClass = true) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00006236 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) {
Douglas Gregor9b4f3702012-06-12 13:44:08 +00006237 // Make sure we have a definition; that's what we'll walk.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00006238 if (!IFace->hasDefinition())
6239 return;
Douglas Gregor9b4f3702012-06-12 13:44:08 +00006240
6241 IFace = IFace->getDefinition();
6242 Container = IFace;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00006243
Douglas Gregor636a61e2010-04-07 00:21:17 +00006244 const ObjCList<ObjCProtocolDecl> &Protocols
6245 = IFace->getReferencedProtocols();
6246 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006247 E = Protocols.end();
Douglas Gregor636a61e2010-04-07 00:21:17 +00006248 I != E; ++I)
6249 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006250 KnownMethods, InOriginalClass);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006251
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006252 // Add methods from any class extensions and categories.
Aaron Ballman3fe486a2014-03-13 21:23:55 +00006253 for (auto *Cat : IFace->visible_categories()) {
6254 FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType,
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006255 KnownMethods, false);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006256 }
6257
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006258 // Visit the superclass.
6259 if (IFace->getSuperClass())
6260 FindImplementableMethods(Context, IFace->getSuperClass(),
6261 WantInstanceMethods, ReturnType,
6262 KnownMethods, false);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006263 }
6264
6265 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
6266 // Recurse into protocols.
6267 const ObjCList<ObjCProtocolDecl> &Protocols
6268 = Category->getReferencedProtocols();
6269 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006270 E = Protocols.end();
Douglas Gregor636a61e2010-04-07 00:21:17 +00006271 I != E; ++I)
6272 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006273 KnownMethods, InOriginalClass);
6274
6275 // If this category is the original class, jump to the interface.
6276 if (InOriginalClass && Category->getClassInterface())
6277 FindImplementableMethods(Context, Category->getClassInterface(),
6278 WantInstanceMethods, ReturnType, KnownMethods,
6279 false);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006280 }
6281
6282 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
Douglas Gregor9b4f3702012-06-12 13:44:08 +00006283 // Make sure we have a definition; that's what we'll walk.
6284 if (!Protocol->hasDefinition())
6285 return;
6286 Protocol = Protocol->getDefinition();
6287 Container = Protocol;
6288
6289 // Recurse into protocols.
6290 const ObjCList<ObjCProtocolDecl> &Protocols
6291 = Protocol->getReferencedProtocols();
6292 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6293 E = Protocols.end();
6294 I != E; ++I)
6295 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6296 KnownMethods, false);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006297 }
6298
6299 // Add methods in this container. This operation occurs last because
6300 // we want the methods from this container to override any methods
6301 // we've previously seen with the same selector.
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006302 for (auto *M : Container->methods()) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00006303 if (M->isInstanceMethod() == WantInstanceMethods) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00006304 if (!ReturnType.isNull() &&
Alp Toker314cc812014-01-25 16:55:45 +00006305 !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType()))
Douglas Gregor636a61e2010-04-07 00:21:17 +00006306 continue;
6307
Benjamin Kramereb8c4462013-06-29 17:52:13 +00006308 KnownMethods[M->getSelector()] =
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006309 KnownMethodsMap::mapped_type(M, InOriginalClass);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006310 }
6311 }
6312}
6313
Douglas Gregor669a25a2011-02-17 00:22:45 +00006314/// \brief Add the parenthesized return or parameter type chunk to a code
6315/// completion string.
6316static void AddObjCPassingTypeChunk(QualType Type,
Douglas Gregor29979142012-04-10 18:35:07 +00006317 unsigned ObjCDeclQuals,
Douglas Gregor669a25a2011-02-17 00:22:45 +00006318 ASTContext &Context,
Douglas Gregor75acd922011-09-27 23:30:47 +00006319 const PrintingPolicy &Policy,
Douglas Gregor669a25a2011-02-17 00:22:45 +00006320 CodeCompletionBuilder &Builder) {
6321 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregor86b42682015-06-19 18:27:52 +00006322 std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals, Type);
Douglas Gregor29979142012-04-10 18:35:07 +00006323 if (!Quals.empty())
6324 Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals));
Douglas Gregor75acd922011-09-27 23:30:47 +00006325 Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy,
Douglas Gregor669a25a2011-02-17 00:22:45 +00006326 Builder.getAllocator()));
6327 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6328}
6329
6330/// \brief Determine whether the given class is or inherits from a class by
6331/// the given name.
6332static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006333 StringRef Name) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006334 if (!Class)
6335 return false;
6336
6337 if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name)
6338 return true;
6339
6340 return InheritsFromClassNamed(Class->getSuperClass(), Name);
6341}
6342
6343/// \brief Add code completions for Objective-C Key-Value Coding (KVC) and
6344/// Key-Value Observing (KVO).
6345static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property,
6346 bool IsInstanceMethod,
6347 QualType ReturnType,
6348 ASTContext &Context,
Douglas Gregord4a8ced2011-05-04 23:50:46 +00006349 VisitedSelectorSet &KnownSelectors,
Douglas Gregor669a25a2011-02-17 00:22:45 +00006350 ResultBuilder &Results) {
6351 IdentifierInfo *PropName = Property->getIdentifier();
6352 if (!PropName || PropName->getLength() == 0)
6353 return;
6354
Douglas Gregor75acd922011-09-27 23:30:47 +00006355 PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
6356
Douglas Gregor669a25a2011-02-17 00:22:45 +00006357 // Builder that will create each code completion.
6358 typedef CodeCompletionResult Result;
6359 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006360 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregor669a25a2011-02-17 00:22:45 +00006361
6362 // The selector table.
6363 SelectorTable &Selectors = Context.Selectors;
6364
6365 // The property name, copied into the code completion allocation region
6366 // on demand.
6367 struct KeyHolder {
6368 CodeCompletionAllocator &Allocator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006369 StringRef Key;
Douglas Gregor669a25a2011-02-17 00:22:45 +00006370 const char *CopiedKey;
Craig Topperc3ec1492014-05-26 06:22:03 +00006371
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006372 KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key)
Craig Topperc3ec1492014-05-26 06:22:03 +00006373 : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {}
6374
Douglas Gregor669a25a2011-02-17 00:22:45 +00006375 operator const char *() {
6376 if (CopiedKey)
6377 return CopiedKey;
6378
6379 return CopiedKey = Allocator.CopyString(Key);
6380 }
6381 } Key(Allocator, PropName->getName());
6382
6383 // The uppercased name of the property name.
6384 std::string UpperKey = PropName->getName();
6385 if (!UpperKey.empty())
Jordan Rose4938f272013-02-09 10:09:43 +00006386 UpperKey[0] = toUppercase(UpperKey[0]);
Douglas Gregor669a25a2011-02-17 00:22:45 +00006387
6388 bool ReturnTypeMatchesProperty = ReturnType.isNull() ||
6389 Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(),
6390 Property->getType());
6391 bool ReturnTypeMatchesVoid
6392 = ReturnType.isNull() || ReturnType->isVoidType();
6393
6394 // Add the normal accessor -(type)key.
6395 if (IsInstanceMethod &&
David Blaikie82e95a32014-11-19 07:49:47 +00006396 KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second &&
Douglas Gregor669a25a2011-02-17 00:22:45 +00006397 ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) {
6398 if (ReturnType.isNull())
Douglas Gregor29979142012-04-10 18:35:07 +00006399 AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6400 Context, Policy, Builder);
Douglas Gregor669a25a2011-02-17 00:22:45 +00006401
6402 Builder.AddTypedTextChunk(Key);
6403 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6404 CXCursor_ObjCInstanceMethodDecl));
6405 }
6406
6407 // If we have an integral or boolean property (or the user has provided
6408 // an integral or boolean return type), add the accessor -(type)isKey.
6409 if (IsInstanceMethod &&
6410 ((!ReturnType.isNull() &&
6411 (ReturnType->isIntegerType() || ReturnType->isBooleanType())) ||
6412 (ReturnType.isNull() &&
6413 (Property->getType()->isIntegerType() ||
6414 Property->getType()->isBooleanType())))) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006415 std::string SelectorName = (Twine("is") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006416 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006417 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6418 .second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006419 if (ReturnType.isNull()) {
6420 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6421 Builder.AddTextChunk("BOOL");
6422 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6423 }
6424
6425 Builder.AddTypedTextChunk(
6426 Allocator.CopyString(SelectorId->getName()));
6427 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6428 CXCursor_ObjCInstanceMethodDecl));
6429 }
6430 }
6431
6432 // Add the normal mutator.
6433 if (IsInstanceMethod && ReturnTypeMatchesVoid &&
6434 !Property->getSetterMethodDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006435 std::string SelectorName = (Twine("set") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006436 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006437 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006438 if (ReturnType.isNull()) {
6439 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6440 Builder.AddTextChunk("void");
6441 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6442 }
6443
6444 Builder.AddTypedTextChunk(
6445 Allocator.CopyString(SelectorId->getName()));
6446 Builder.AddTypedTextChunk(":");
Douglas Gregor29979142012-04-10 18:35:07 +00006447 AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6448 Context, Policy, Builder);
Douglas Gregor669a25a2011-02-17 00:22:45 +00006449 Builder.AddTextChunk(Key);
6450 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6451 CXCursor_ObjCInstanceMethodDecl));
6452 }
6453 }
6454
6455 // Indexed and unordered accessors
6456 unsigned IndexedGetterPriority = CCP_CodePattern;
6457 unsigned IndexedSetterPriority = CCP_CodePattern;
6458 unsigned UnorderedGetterPriority = CCP_CodePattern;
6459 unsigned UnorderedSetterPriority = CCP_CodePattern;
6460 if (const ObjCObjectPointerType *ObjCPointer
6461 = Property->getType()->getAs<ObjCObjectPointerType>()) {
6462 if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) {
6463 // If this interface type is not provably derived from a known
6464 // collection, penalize the corresponding completions.
6465 if (!InheritsFromClassNamed(IFace, "NSMutableArray")) {
6466 IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6467 if (!InheritsFromClassNamed(IFace, "NSArray"))
6468 IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6469 }
6470
6471 if (!InheritsFromClassNamed(IFace, "NSMutableSet")) {
6472 UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6473 if (!InheritsFromClassNamed(IFace, "NSSet"))
6474 UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6475 }
6476 }
6477 } else {
6478 IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6479 IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6480 UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6481 UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6482 }
6483
6484 // Add -(NSUInteger)countOf<key>
6485 if (IsInstanceMethod &&
6486 (ReturnType.isNull() || ReturnType->isIntegerType())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006487 std::string SelectorName = (Twine("countOf") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006488 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006489 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6490 .second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006491 if (ReturnType.isNull()) {
6492 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6493 Builder.AddTextChunk("NSUInteger");
6494 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6495 }
6496
6497 Builder.AddTypedTextChunk(
6498 Allocator.CopyString(SelectorId->getName()));
6499 Results.AddResult(Result(Builder.TakeString(),
6500 std::min(IndexedGetterPriority,
6501 UnorderedGetterPriority),
6502 CXCursor_ObjCInstanceMethodDecl));
6503 }
6504 }
6505
6506 // Indexed getters
6507 // Add -(id)objectInKeyAtIndex:(NSUInteger)index
6508 if (IsInstanceMethod &&
6509 (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006510 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006511 = (Twine("objectIn") + UpperKey + "AtIndex").str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006512 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006513 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006514 if (ReturnType.isNull()) {
6515 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6516 Builder.AddTextChunk("id");
6517 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6518 }
6519
6520 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6521 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6522 Builder.AddTextChunk("NSUInteger");
6523 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6524 Builder.AddTextChunk("index");
6525 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6526 CXCursor_ObjCInstanceMethodDecl));
6527 }
6528 }
6529
6530 // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes
6531 if (IsInstanceMethod &&
6532 (ReturnType.isNull() ||
6533 (ReturnType->isObjCObjectPointerType() &&
6534 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6535 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6536 ->getName() == "NSArray"))) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006537 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006538 = (Twine(Property->getName()) + "AtIndexes").str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006539 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006540 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006541 if (ReturnType.isNull()) {
6542 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6543 Builder.AddTextChunk("NSArray *");
6544 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6545 }
6546
6547 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6548 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6549 Builder.AddTextChunk("NSIndexSet *");
6550 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6551 Builder.AddTextChunk("indexes");
6552 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6553 CXCursor_ObjCInstanceMethodDecl));
6554 }
6555 }
6556
6557 // Add -(void)getKey:(type **)buffer range:(NSRange)inRange
6558 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006559 std::string SelectorName = (Twine("get") + UpperKey).str();
Douglas Gregor669a25a2011-02-17 00:22:45 +00006560 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006561 &Context.Idents.get(SelectorName),
Douglas Gregor669a25a2011-02-17 00:22:45 +00006562 &Context.Idents.get("range")
6563 };
6564
David Blaikie82e95a32014-11-19 07:49:47 +00006565 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006566 if (ReturnType.isNull()) {
6567 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6568 Builder.AddTextChunk("void");
6569 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6570 }
6571
6572 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6573 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6574 Builder.AddPlaceholderChunk("object-type");
6575 Builder.AddTextChunk(" **");
6576 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6577 Builder.AddTextChunk("buffer");
6578 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6579 Builder.AddTypedTextChunk("range:");
6580 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6581 Builder.AddTextChunk("NSRange");
6582 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6583 Builder.AddTextChunk("inRange");
6584 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6585 CXCursor_ObjCInstanceMethodDecl));
6586 }
6587 }
6588
6589 // Mutable indexed accessors
6590
6591 // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index
6592 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006593 std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str();
Douglas Gregor669a25a2011-02-17 00:22:45 +00006594 IdentifierInfo *SelectorIds[2] = {
6595 &Context.Idents.get("insertObject"),
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006596 &Context.Idents.get(SelectorName)
Douglas Gregor669a25a2011-02-17 00:22:45 +00006597 };
6598
David Blaikie82e95a32014-11-19 07:49:47 +00006599 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006600 if (ReturnType.isNull()) {
6601 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6602 Builder.AddTextChunk("void");
6603 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6604 }
6605
6606 Builder.AddTypedTextChunk("insertObject:");
6607 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6608 Builder.AddPlaceholderChunk("object-type");
6609 Builder.AddTextChunk(" *");
6610 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6611 Builder.AddTextChunk("object");
6612 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6613 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6614 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6615 Builder.AddPlaceholderChunk("NSUInteger");
6616 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6617 Builder.AddTextChunk("index");
6618 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6619 CXCursor_ObjCInstanceMethodDecl));
6620 }
6621 }
6622
6623 // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes
6624 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006625 std::string SelectorName = (Twine("insert") + UpperKey).str();
Douglas Gregor669a25a2011-02-17 00:22:45 +00006626 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006627 &Context.Idents.get(SelectorName),
Douglas Gregor669a25a2011-02-17 00:22:45 +00006628 &Context.Idents.get("atIndexes")
6629 };
6630
David Blaikie82e95a32014-11-19 07:49:47 +00006631 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006632 if (ReturnType.isNull()) {
6633 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6634 Builder.AddTextChunk("void");
6635 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6636 }
6637
6638 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6639 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6640 Builder.AddTextChunk("NSArray *");
6641 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6642 Builder.AddTextChunk("array");
6643 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6644 Builder.AddTypedTextChunk("atIndexes:");
6645 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6646 Builder.AddPlaceholderChunk("NSIndexSet *");
6647 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6648 Builder.AddTextChunk("indexes");
6649 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6650 CXCursor_ObjCInstanceMethodDecl));
6651 }
6652 }
6653
6654 // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index
6655 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006656 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006657 = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006658 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006659 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006660 if (ReturnType.isNull()) {
6661 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6662 Builder.AddTextChunk("void");
6663 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6664 }
6665
6666 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6667 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6668 Builder.AddTextChunk("NSUInteger");
6669 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6670 Builder.AddTextChunk("index");
6671 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6672 CXCursor_ObjCInstanceMethodDecl));
6673 }
6674 }
6675
6676 // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes
6677 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006678 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006679 = (Twine("remove") + UpperKey + "AtIndexes").str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006680 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006681 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006682 if (ReturnType.isNull()) {
6683 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6684 Builder.AddTextChunk("void");
6685 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6686 }
6687
6688 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6689 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6690 Builder.AddTextChunk("NSIndexSet *");
6691 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6692 Builder.AddTextChunk("indexes");
6693 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6694 CXCursor_ObjCInstanceMethodDecl));
6695 }
6696 }
6697
6698 // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object
6699 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006700 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006701 = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str();
Douglas Gregor669a25a2011-02-17 00:22:45 +00006702 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006703 &Context.Idents.get(SelectorName),
Douglas Gregor669a25a2011-02-17 00:22:45 +00006704 &Context.Idents.get("withObject")
6705 };
6706
David Blaikie82e95a32014-11-19 07:49:47 +00006707 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006708 if (ReturnType.isNull()) {
6709 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6710 Builder.AddTextChunk("void");
6711 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6712 }
6713
6714 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6715 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6716 Builder.AddPlaceholderChunk("NSUInteger");
6717 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6718 Builder.AddTextChunk("index");
6719 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6720 Builder.AddTypedTextChunk("withObject:");
6721 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6722 Builder.AddTextChunk("id");
6723 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6724 Builder.AddTextChunk("object");
6725 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6726 CXCursor_ObjCInstanceMethodDecl));
6727 }
6728 }
6729
6730 // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array
6731 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006732 std::string SelectorName1
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006733 = (Twine("replace") + UpperKey + "AtIndexes").str();
6734 std::string SelectorName2 = (Twine("with") + UpperKey).str();
Douglas Gregor669a25a2011-02-17 00:22:45 +00006735 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006736 &Context.Idents.get(SelectorName1),
6737 &Context.Idents.get(SelectorName2)
Douglas Gregor669a25a2011-02-17 00:22:45 +00006738 };
6739
David Blaikie82e95a32014-11-19 07:49:47 +00006740 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006741 if (ReturnType.isNull()) {
6742 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6743 Builder.AddTextChunk("void");
6744 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6745 }
6746
6747 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":"));
6748 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6749 Builder.AddPlaceholderChunk("NSIndexSet *");
6750 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6751 Builder.AddTextChunk("indexes");
6752 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6753 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":"));
6754 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6755 Builder.AddTextChunk("NSArray *");
6756 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6757 Builder.AddTextChunk("array");
6758 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6759 CXCursor_ObjCInstanceMethodDecl));
6760 }
6761 }
6762
6763 // Unordered getters
6764 // - (NSEnumerator *)enumeratorOfKey
6765 if (IsInstanceMethod &&
6766 (ReturnType.isNull() ||
6767 (ReturnType->isObjCObjectPointerType() &&
6768 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6769 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6770 ->getName() == "NSEnumerator"))) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006771 std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006772 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006773 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6774 .second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006775 if (ReturnType.isNull()) {
6776 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6777 Builder.AddTextChunk("NSEnumerator *");
6778 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6779 }
6780
6781 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6782 Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6783 CXCursor_ObjCInstanceMethodDecl));
6784 }
6785 }
6786
6787 // - (type *)memberOfKey:(type *)object
6788 if (IsInstanceMethod &&
6789 (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006790 std::string SelectorName = (Twine("memberOf") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006791 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006792 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006793 if (ReturnType.isNull()) {
6794 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6795 Builder.AddPlaceholderChunk("object-type");
6796 Builder.AddTextChunk(" *");
6797 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6798 }
6799
6800 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6801 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6802 if (ReturnType.isNull()) {
6803 Builder.AddPlaceholderChunk("object-type");
6804 Builder.AddTextChunk(" *");
6805 } else {
6806 Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context,
Douglas Gregor75acd922011-09-27 23:30:47 +00006807 Policy,
Douglas Gregor669a25a2011-02-17 00:22:45 +00006808 Builder.getAllocator()));
6809 }
6810 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6811 Builder.AddTextChunk("object");
6812 Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6813 CXCursor_ObjCInstanceMethodDecl));
6814 }
6815 }
6816
6817 // Mutable unordered accessors
6818 // - (void)addKeyObject:(type *)object
6819 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006820 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006821 = (Twine("add") + UpperKey + Twine("Object")).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006822 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006823 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006824 if (ReturnType.isNull()) {
6825 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6826 Builder.AddTextChunk("void");
6827 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6828 }
6829
6830 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6831 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6832 Builder.AddPlaceholderChunk("object-type");
6833 Builder.AddTextChunk(" *");
6834 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6835 Builder.AddTextChunk("object");
6836 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6837 CXCursor_ObjCInstanceMethodDecl));
6838 }
6839 }
6840
6841 // - (void)addKey:(NSSet *)objects
6842 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006843 std::string SelectorName = (Twine("add") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006844 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006845 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006846 if (ReturnType.isNull()) {
6847 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6848 Builder.AddTextChunk("void");
6849 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6850 }
6851
6852 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6853 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6854 Builder.AddTextChunk("NSSet *");
6855 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6856 Builder.AddTextChunk("objects");
6857 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6858 CXCursor_ObjCInstanceMethodDecl));
6859 }
6860 }
6861
6862 // - (void)removeKeyObject:(type *)object
6863 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006864 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006865 = (Twine("remove") + UpperKey + Twine("Object")).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006866 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006867 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006868 if (ReturnType.isNull()) {
6869 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6870 Builder.AddTextChunk("void");
6871 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6872 }
6873
6874 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6875 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6876 Builder.AddPlaceholderChunk("object-type");
6877 Builder.AddTextChunk(" *");
6878 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6879 Builder.AddTextChunk("object");
6880 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6881 CXCursor_ObjCInstanceMethodDecl));
6882 }
6883 }
6884
6885 // - (void)removeKey:(NSSet *)objects
6886 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006887 std::string SelectorName = (Twine("remove") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006888 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006889 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006890 if (ReturnType.isNull()) {
6891 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6892 Builder.AddTextChunk("void");
6893 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6894 }
6895
6896 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6897 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6898 Builder.AddTextChunk("NSSet *");
6899 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6900 Builder.AddTextChunk("objects");
6901 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6902 CXCursor_ObjCInstanceMethodDecl));
6903 }
6904 }
6905
6906 // - (void)intersectKey:(NSSet *)objects
6907 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006908 std::string SelectorName = (Twine("intersect") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006909 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006910 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006911 if (ReturnType.isNull()) {
6912 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6913 Builder.AddTextChunk("void");
6914 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6915 }
6916
6917 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6918 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6919 Builder.AddTextChunk("NSSet *");
6920 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6921 Builder.AddTextChunk("objects");
6922 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6923 CXCursor_ObjCInstanceMethodDecl));
6924 }
6925 }
6926
6927 // Key-Value Observing
6928 // + (NSSet *)keyPathsForValuesAffectingKey
6929 if (!IsInstanceMethod &&
6930 (ReturnType.isNull() ||
6931 (ReturnType->isObjCObjectPointerType() &&
6932 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6933 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6934 ->getName() == "NSSet"))) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006935 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006936 = (Twine("keyPathsForValuesAffecting") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006937 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006938 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6939 .second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006940 if (ReturnType.isNull()) {
6941 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6942 Builder.AddTextChunk("NSSet *");
6943 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6944 }
6945
6946 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6947 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
Douglas Gregor857bcda2011-06-02 04:02:27 +00006948 CXCursor_ObjCClassMethodDecl));
6949 }
6950 }
6951
6952 // + (BOOL)automaticallyNotifiesObserversForKey
6953 if (!IsInstanceMethod &&
6954 (ReturnType.isNull() ||
6955 ReturnType->isIntegerType() ||
6956 ReturnType->isBooleanType())) {
6957 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006958 = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str();
Douglas Gregor857bcda2011-06-02 04:02:27 +00006959 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006960 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6961 .second) {
Douglas Gregor857bcda2011-06-02 04:02:27 +00006962 if (ReturnType.isNull()) {
6963 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6964 Builder.AddTextChunk("BOOL");
6965 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6966 }
6967
6968 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6969 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6970 CXCursor_ObjCClassMethodDecl));
Douglas Gregor669a25a2011-02-17 00:22:45 +00006971 }
6972 }
6973}
6974
Douglas Gregor636a61e2010-04-07 00:21:17 +00006975void Sema::CodeCompleteObjCMethodDecl(Scope *S,
6976 bool IsInstanceMethod,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006977 ParsedType ReturnTy) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00006978 // Determine the return type of the method we're declaring, if
6979 // provided.
6980 QualType ReturnType = GetTypeFromParser(ReturnTy);
Craig Topperc3ec1492014-05-26 06:22:03 +00006981 Decl *IDecl = nullptr;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006982 if (CurContext->isObjCContainer()) {
6983 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
6984 IDecl = cast<Decl>(OCD);
6985 }
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006986 // Determine where we should start searching for methods.
Craig Topperc3ec1492014-05-26 06:22:03 +00006987 ObjCContainerDecl *SearchDecl = nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00006988 bool IsInImplementation = false;
John McCall48871652010-08-21 09:40:31 +00006989 if (Decl *D = IDecl) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00006990 if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) {
6991 SearchDecl = Impl->getClassInterface();
Douglas Gregor636a61e2010-04-07 00:21:17 +00006992 IsInImplementation = true;
6993 } else if (ObjCCategoryImplDecl *CatImpl
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006994 = dyn_cast<ObjCCategoryImplDecl>(D)) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00006995 SearchDecl = CatImpl->getCategoryDecl();
Douglas Gregor636a61e2010-04-07 00:21:17 +00006996 IsInImplementation = true;
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006997 } else
Douglas Gregor636a61e2010-04-07 00:21:17 +00006998 SearchDecl = dyn_cast<ObjCContainerDecl>(D);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006999 }
7000
7001 if (!SearchDecl && S) {
Ted Kremenekc37877d2013-10-08 17:08:03 +00007002 if (DeclContext *DC = S->getEntity())
Douglas Gregor636a61e2010-04-07 00:21:17 +00007003 SearchDecl = dyn_cast<ObjCContainerDecl>(DC);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007004 }
7005
Douglas Gregor1b035bb2010-10-18 18:21:28 +00007006 if (!SearchDecl) {
Douglas Gregor00c37ef2010-08-11 21:23:17 +00007007 HandleCodeCompleteResults(this, CodeCompleter,
7008 CodeCompletionContext::CCC_Other,
Craig Topperc3ec1492014-05-26 06:22:03 +00007009 nullptr, 0);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007010 return;
7011 }
7012
7013 // Find all of the methods that we could declare/implement here.
7014 KnownMethodsMap KnownMethods;
7015 FindImplementableMethods(Context, SearchDecl, IsInstanceMethod,
Douglas Gregor1b035bb2010-10-18 18:21:28 +00007016 ReturnType, KnownMethods);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007017
Douglas Gregor636a61e2010-04-07 00:21:17 +00007018 // Add declarations or definitions for each of the known methods.
John McCall276321a2010-08-25 06:19:51 +00007019 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007020 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007021 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007022 CodeCompletionContext::CCC_Other);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007023 Results.EnterNewScope();
Douglas Gregor75acd922011-09-27 23:30:47 +00007024 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007025 for (KnownMethodsMap::iterator M = KnownMethods.begin(),
7026 MEnd = KnownMethods.end();
7027 M != MEnd; ++M) {
Benjamin Kramereb8c4462013-06-29 17:52:13 +00007028 ObjCMethodDecl *Method = M->second.getPointer();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007029 CodeCompletionBuilder Builder(Results.getAllocator(),
7030 Results.getCodeCompletionTUInfo());
Douglas Gregor636a61e2010-04-07 00:21:17 +00007031
7032 // If the result type was not already provided, add it to the
7033 // pattern as (type).
Douglas Gregor669a25a2011-02-17 00:22:45 +00007034 if (ReturnType.isNull())
Alp Toker314cc812014-01-25 16:55:45 +00007035 AddObjCPassingTypeChunk(Method->getReturnType(),
7036 Method->getObjCDeclQualifier(), Context, Policy,
7037 Builder);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007038
7039 Selector Sel = Method->getSelector();
7040
7041 // Add the first part of the selector to the pattern.
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00007042 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00007043 Sel.getNameForSlot(0)));
Douglas Gregor636a61e2010-04-07 00:21:17 +00007044
7045 // Add parameters to the pattern.
7046 unsigned I = 0;
7047 for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
7048 PEnd = Method->param_end();
7049 P != PEnd; (void)++P, ++I) {
7050 // Add the part of the selector name.
7051 if (I == 0)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007052 Builder.AddTypedTextChunk(":");
Douglas Gregor636a61e2010-04-07 00:21:17 +00007053 else if (I < Sel.getNumArgs()) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007054 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7055 Builder.AddTypedTextChunk(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00007056 Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":"));
Douglas Gregor636a61e2010-04-07 00:21:17 +00007057 } else
7058 break;
7059
7060 // Add the parameter type.
Douglas Gregor86b42682015-06-19 18:27:52 +00007061 QualType ParamType;
7062 if ((*P)->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability)
7063 ParamType = (*P)->getType();
7064 else
7065 ParamType = (*P)->getOriginalType();
7066 AddObjCPassingTypeChunk(ParamType,
Douglas Gregor29979142012-04-10 18:35:07 +00007067 (*P)->getObjCDeclQualifier(),
7068 Context, Policy,
Douglas Gregor75acd922011-09-27 23:30:47 +00007069 Builder);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007070
7071 if (IdentifierInfo *Id = (*P)->getIdentifier())
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00007072 Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName()));
Douglas Gregor636a61e2010-04-07 00:21:17 +00007073 }
7074
7075 if (Method->isVariadic()) {
7076 if (Method->param_size() > 0)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007077 Builder.AddChunk(CodeCompletionString::CK_Comma);
7078 Builder.AddTextChunk("...");
Douglas Gregor400f5972010-08-31 05:13:43 +00007079 }
Douglas Gregor636a61e2010-04-07 00:21:17 +00007080
Douglas Gregord37c59d2010-05-28 00:57:46 +00007081 if (IsInImplementation && Results.includeCodePatterns()) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00007082 // We will be defining the method here, so add a compound statement.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007083 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7084 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
7085 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
Alp Toker314cc812014-01-25 16:55:45 +00007086 if (!Method->getReturnType()->isVoidType()) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00007087 // If the result type is not void, add a return clause.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007088 Builder.AddTextChunk("return");
7089 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7090 Builder.AddPlaceholderChunk("expression");
7091 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007092 } else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007093 Builder.AddPlaceholderChunk("statements");
Douglas Gregor636a61e2010-04-07 00:21:17 +00007094
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007095 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
7096 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007097 }
7098
Douglas Gregor416b5752010-08-25 01:08:01 +00007099 unsigned Priority = CCP_CodePattern;
Benjamin Kramereb8c4462013-06-29 17:52:13 +00007100 if (!M->second.getInt())
Douglas Gregor416b5752010-08-25 01:08:01 +00007101 Priority += CCD_InBaseClass;
7102
Douglas Gregor78254c82012-03-27 23:34:16 +00007103 Results.AddResult(Result(Builder.TakeString(), Method, Priority));
Douglas Gregor636a61e2010-04-07 00:21:17 +00007104 }
7105
Douglas Gregor669a25a2011-02-17 00:22:45 +00007106 // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of
7107 // the properties in this class and its categories.
David Blaikiebbafb8a2012-03-11 07:00:24 +00007108 if (Context.getLangOpts().ObjC2) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007109 SmallVector<ObjCContainerDecl *, 4> Containers;
Douglas Gregor669a25a2011-02-17 00:22:45 +00007110 Containers.push_back(SearchDecl);
7111
Douglas Gregord4a8ced2011-05-04 23:50:46 +00007112 VisitedSelectorSet KnownSelectors;
7113 for (KnownMethodsMap::iterator M = KnownMethods.begin(),
7114 MEnd = KnownMethods.end();
7115 M != MEnd; ++M)
7116 KnownSelectors.insert(M->first);
7117
7118
Douglas Gregor669a25a2011-02-17 00:22:45 +00007119 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl);
7120 if (!IFace)
7121 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl))
7122 IFace = Category->getClassInterface();
7123
Aaron Ballman3fe486a2014-03-13 21:23:55 +00007124 if (IFace)
7125 for (auto *Cat : IFace->visible_categories())
7126 Containers.push_back(Cat);
Douglas Gregor669a25a2011-02-17 00:22:45 +00007127
Aaron Ballmandc4bea42014-03-13 18:47:37 +00007128 for (unsigned I = 0, N = Containers.size(); I != N; ++I)
Aaron Ballmand174edf2014-03-13 19:11:50 +00007129 for (auto *P : Containers[I]->properties())
Aaron Ballmandc4bea42014-03-13 18:47:37 +00007130 AddObjCKeyValueCompletions(P, IsInstanceMethod, ReturnType, Context,
Douglas Gregord4a8ced2011-05-04 23:50:46 +00007131 KnownSelectors, Results);
Douglas Gregor669a25a2011-02-17 00:22:45 +00007132 }
7133
Douglas Gregor636a61e2010-04-07 00:21:17 +00007134 Results.ExitScope();
7135
Douglas Gregor00c37ef2010-08-11 21:23:17 +00007136 HandleCodeCompleteResults(this, CodeCompleter,
7137 CodeCompletionContext::CCC_Other,
7138 Results.data(),Results.size());
Douglas Gregor636a61e2010-04-07 00:21:17 +00007139}
Douglas Gregor95887f92010-07-08 23:20:03 +00007140
7141void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S,
7142 bool IsInstanceMethod,
Douglas Gregor45879692010-07-08 23:37:41 +00007143 bool AtParameterName,
John McCallba7bf592010-08-24 05:47:05 +00007144 ParsedType ReturnTy,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00007145 ArrayRef<IdentifierInfo *> SelIdents) {
Douglas Gregor95887f92010-07-08 23:20:03 +00007146 // If we have an external source, load the entire class method
Sebastian Redld44cd6a2010-08-18 23:57:06 +00007147 // pool from the AST file.
Douglas Gregor95887f92010-07-08 23:20:03 +00007148 if (ExternalSource) {
7149 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
7150 I != N; ++I) {
7151 Selector Sel = ExternalSource->GetExternalSelector(I);
Sebastian Redl75d8a322010-08-02 23:18:59 +00007152 if (Sel.isNull() || MethodPool.count(Sel))
Douglas Gregor95887f92010-07-08 23:20:03 +00007153 continue;
Sebastian Redl75d8a322010-08-02 23:18:59 +00007154
7155 ReadMethodPool(Sel);
Douglas Gregor95887f92010-07-08 23:20:03 +00007156 }
7157 }
7158
7159 // Build the set of methods we can see.
John McCall276321a2010-08-25 06:19:51 +00007160 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007161 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007162 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007163 CodeCompletionContext::CCC_Other);
Douglas Gregor95887f92010-07-08 23:20:03 +00007164
7165 if (ReturnTy)
7166 Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType());
Sebastian Redl75d8a322010-08-02 23:18:59 +00007167
Douglas Gregor95887f92010-07-08 23:20:03 +00007168 Results.EnterNewScope();
Sebastian Redl75d8a322010-08-02 23:18:59 +00007169 for (GlobalMethodPool::iterator M = MethodPool.begin(),
7170 MEnd = MethodPool.end();
7171 M != MEnd; ++M) {
7172 for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first :
7173 &M->second.second;
Nico Weber2e0c8f72014-12-27 03:58:08 +00007174 MethList && MethList->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007175 MethList = MethList->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00007176 if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
Douglas Gregor95887f92010-07-08 23:20:03 +00007177 continue;
7178
Douglas Gregor45879692010-07-08 23:37:41 +00007179 if (AtParameterName) {
7180 // Suggest parameter names we've seen before.
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00007181 unsigned NumSelIdents = SelIdents.size();
Nico Weber2e0c8f72014-12-27 03:58:08 +00007182 if (NumSelIdents &&
7183 NumSelIdents <= MethList->getMethod()->param_size()) {
7184 ParmVarDecl *Param =
7185 MethList->getMethod()->parameters()[NumSelIdents - 1];
Douglas Gregor45879692010-07-08 23:37:41 +00007186 if (Param->getIdentifier()) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007187 CodeCompletionBuilder Builder(Results.getAllocator(),
7188 Results.getCodeCompletionTUInfo());
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00007189 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007190 Param->getIdentifier()->getName()));
7191 Results.AddResult(Builder.TakeString());
Douglas Gregor45879692010-07-08 23:37:41 +00007192 }
7193 }
7194
7195 continue;
7196 }
Craig Topperc3ec1492014-05-26 06:22:03 +00007197
Nico Weber2e0c8f72014-12-27 03:58:08 +00007198 Result R(MethList->getMethod(),
7199 Results.getBasePriority(MethList->getMethod()), nullptr);
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00007200 R.StartParameter = SelIdents.size();
Douglas Gregor95887f92010-07-08 23:20:03 +00007201 R.AllParametersAreInformative = false;
7202 R.DeclaringEntity = true;
7203 Results.MaybeAddResult(R, CurContext);
7204 }
7205 }
7206
7207 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00007208 HandleCodeCompleteResults(this, CodeCompleter,
7209 CodeCompletionContext::CCC_Other,
7210 Results.data(),Results.size());
Douglas Gregor95887f92010-07-08 23:20:03 +00007211}
Douglas Gregorb14904c2010-08-13 22:48:40 +00007212
Douglas Gregorec00a262010-08-24 22:20:20 +00007213void Sema::CodeCompletePreprocessorDirective(bool InConditional) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007214 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007215 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00007216 CodeCompletionContext::CCC_PreprocessorDirective);
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007217 Results.EnterNewScope();
7218
7219 // #if <condition>
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007220 CodeCompletionBuilder Builder(Results.getAllocator(),
7221 Results.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007222 Builder.AddTypedTextChunk("if");
7223 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7224 Builder.AddPlaceholderChunk("condition");
7225 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007226
7227 // #ifdef <macro>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007228 Builder.AddTypedTextChunk("ifdef");
7229 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7230 Builder.AddPlaceholderChunk("macro");
7231 Results.AddResult(Builder.TakeString());
7232
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007233 // #ifndef <macro>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007234 Builder.AddTypedTextChunk("ifndef");
7235 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7236 Builder.AddPlaceholderChunk("macro");
7237 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007238
7239 if (InConditional) {
7240 // #elif <condition>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007241 Builder.AddTypedTextChunk("elif");
7242 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7243 Builder.AddPlaceholderChunk("condition");
7244 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007245
7246 // #else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007247 Builder.AddTypedTextChunk("else");
7248 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007249
7250 // #endif
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007251 Builder.AddTypedTextChunk("endif");
7252 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007253 }
7254
7255 // #include "header"
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007256 Builder.AddTypedTextChunk("include");
7257 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7258 Builder.AddTextChunk("\"");
7259 Builder.AddPlaceholderChunk("header");
7260 Builder.AddTextChunk("\"");
7261 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007262
7263 // #include <header>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007264 Builder.AddTypedTextChunk("include");
7265 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7266 Builder.AddTextChunk("<");
7267 Builder.AddPlaceholderChunk("header");
7268 Builder.AddTextChunk(">");
7269 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007270
7271 // #define <macro>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007272 Builder.AddTypedTextChunk("define");
7273 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7274 Builder.AddPlaceholderChunk("macro");
7275 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007276
7277 // #define <macro>(<args>)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007278 Builder.AddTypedTextChunk("define");
7279 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7280 Builder.AddPlaceholderChunk("macro");
7281 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7282 Builder.AddPlaceholderChunk("args");
7283 Builder.AddChunk(CodeCompletionString::CK_RightParen);
7284 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007285
7286 // #undef <macro>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007287 Builder.AddTypedTextChunk("undef");
7288 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7289 Builder.AddPlaceholderChunk("macro");
7290 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007291
7292 // #line <number>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007293 Builder.AddTypedTextChunk("line");
7294 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7295 Builder.AddPlaceholderChunk("number");
7296 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007297
7298 // #line <number> "filename"
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007299 Builder.AddTypedTextChunk("line");
7300 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7301 Builder.AddPlaceholderChunk("number");
7302 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7303 Builder.AddTextChunk("\"");
7304 Builder.AddPlaceholderChunk("filename");
7305 Builder.AddTextChunk("\"");
7306 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007307
7308 // #error <message>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007309 Builder.AddTypedTextChunk("error");
7310 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7311 Builder.AddPlaceholderChunk("message");
7312 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007313
7314 // #pragma <arguments>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007315 Builder.AddTypedTextChunk("pragma");
7316 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7317 Builder.AddPlaceholderChunk("arguments");
7318 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007319
David Blaikiebbafb8a2012-03-11 07:00:24 +00007320 if (getLangOpts().ObjC1) {
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007321 // #import "header"
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007322 Builder.AddTypedTextChunk("import");
7323 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7324 Builder.AddTextChunk("\"");
7325 Builder.AddPlaceholderChunk("header");
7326 Builder.AddTextChunk("\"");
7327 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007328
7329 // #import <header>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007330 Builder.AddTypedTextChunk("import");
7331 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7332 Builder.AddTextChunk("<");
7333 Builder.AddPlaceholderChunk("header");
7334 Builder.AddTextChunk(">");
7335 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007336 }
7337
7338 // #include_next "header"
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007339 Builder.AddTypedTextChunk("include_next");
7340 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7341 Builder.AddTextChunk("\"");
7342 Builder.AddPlaceholderChunk("header");
7343 Builder.AddTextChunk("\"");
7344 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007345
7346 // #include_next <header>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007347 Builder.AddTypedTextChunk("include_next");
7348 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7349 Builder.AddTextChunk("<");
7350 Builder.AddPlaceholderChunk("header");
7351 Builder.AddTextChunk(">");
7352 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007353
7354 // #warning <message>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007355 Builder.AddTypedTextChunk("warning");
7356 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7357 Builder.AddPlaceholderChunk("message");
7358 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007359
7360 // Note: #ident and #sccs are such crazy anachronisms that we don't provide
7361 // completions for them. And __include_macros is a Clang-internal extension
7362 // that we don't want to encourage anyone to use.
7363
7364 // FIXME: we don't support #assert or #unassert, so don't suggest them.
7365 Results.ExitScope();
7366
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007367 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0de55ce2010-08-25 18:41:16 +00007368 CodeCompletionContext::CCC_PreprocessorDirective,
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007369 Results.data(), Results.size());
7370}
7371
7372void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) {
Douglas Gregorec00a262010-08-24 22:20:20 +00007373 CodeCompleteOrdinaryName(S,
John McCallfaf5fb42010-08-26 23:41:50 +00007374 S->getFnParent()? Sema::PCC_RecoveryInFunction
7375 : Sema::PCC_Namespace);
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007376}
7377
Douglas Gregorec00a262010-08-24 22:20:20 +00007378void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007379 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007380 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00007381 IsDefinition? CodeCompletionContext::CCC_MacroName
7382 : CodeCompletionContext::CCC_MacroNameUse);
Douglas Gregor12785102010-08-24 20:21:13 +00007383 if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) {
7384 // Add just the names of macros, not their arguments.
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007385 CodeCompletionBuilder Builder(Results.getAllocator(),
7386 Results.getCodeCompletionTUInfo());
Douglas Gregor12785102010-08-24 20:21:13 +00007387 Results.EnterNewScope();
7388 for (Preprocessor::macro_iterator M = PP.macro_begin(),
7389 MEnd = PP.macro_end();
7390 M != MEnd; ++M) {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00007391 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007392 M->first->getName()));
Argyrios Kyrtzidis5c8b1cd2012-09-27 00:24:09 +00007393 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
7394 CCP_CodePattern,
7395 CXCursor_MacroDefinition));
Douglas Gregor12785102010-08-24 20:21:13 +00007396 }
7397 Results.ExitScope();
7398 } else if (IsDefinition) {
7399 // FIXME: Can we detect when the user just wrote an include guard above?
7400 }
7401
Douglas Gregor0ac41382010-09-23 23:01:17 +00007402 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor12785102010-08-24 20:21:13 +00007403 Results.data(), Results.size());
7404}
7405
Douglas Gregorec00a262010-08-24 22:20:20 +00007406void Sema::CodeCompletePreprocessorExpression() {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007407 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007408 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00007409 CodeCompletionContext::CCC_PreprocessorExpression);
Douglas Gregorec00a262010-08-24 22:20:20 +00007410
7411 if (!CodeCompleter || CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00007412 AddMacroResults(PP, Results, true);
Douglas Gregorec00a262010-08-24 22:20:20 +00007413
7414 // defined (<macro>)
7415 Results.EnterNewScope();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007416 CodeCompletionBuilder Builder(Results.getAllocator(),
7417 Results.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007418 Builder.AddTypedTextChunk("defined");
7419 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7420 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7421 Builder.AddPlaceholderChunk("macro");
7422 Builder.AddChunk(CodeCompletionString::CK_RightParen);
7423 Results.AddResult(Builder.TakeString());
Douglas Gregorec00a262010-08-24 22:20:20 +00007424 Results.ExitScope();
7425
7426 HandleCodeCompleteResults(this, CodeCompleter,
7427 CodeCompletionContext::CCC_PreprocessorExpression,
7428 Results.data(), Results.size());
7429}
7430
7431void Sema::CodeCompletePreprocessorMacroArgument(Scope *S,
7432 IdentifierInfo *Macro,
7433 MacroInfo *MacroInfo,
7434 unsigned Argument) {
7435 // FIXME: In the future, we could provide "overload" results, much like we
7436 // do for function calls.
7437
Argyrios Kyrtzidis75f6cd22011-08-18 19:41:28 +00007438 // Now just ignore this. There will be another code-completion callback
7439 // for the expanded tokens.
Douglas Gregorec00a262010-08-24 22:20:20 +00007440}
7441
Douglas Gregor11583702010-08-25 17:04:25 +00007442void Sema::CodeCompleteNaturalLanguage() {
Douglas Gregor11583702010-08-25 17:04:25 +00007443 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregorea736372010-08-25 17:10:00 +00007444 CodeCompletionContext::CCC_NaturalLanguage,
Craig Topperc3ec1492014-05-26 06:22:03 +00007445 nullptr, 0);
Douglas Gregor11583702010-08-25 17:04:25 +00007446}
7447
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00007448void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator,
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007449 CodeCompletionTUInfo &CCTUInfo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007450 SmallVectorImpl<CodeCompletionResult> &Results) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007451 ResultBuilder Builder(*this, Allocator, CCTUInfo,
7452 CodeCompletionContext::CCC_Recovery);
Douglas Gregor39982192010-08-15 06:18:01 +00007453 if (!CodeCompleter || CodeCompleter->includeGlobals()) {
7454 CodeCompletionDeclConsumer Consumer(Builder,
7455 Context.getTranslationUnitDecl());
7456 LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName,
7457 Consumer);
7458 }
Douglas Gregorb14904c2010-08-13 22:48:40 +00007459
7460 if (!CodeCompleter || CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00007461 AddMacroResults(PP, Builder, true);
Douglas Gregorb14904c2010-08-13 22:48:40 +00007462
7463 Results.clear();
7464 Results.insert(Results.end(),
7465 Builder.data(), Builder.data() + Builder.size());
7466}