blob: e4e99ab9932428724b1b2142e8e8430b3a8babf6 [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 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000345}
Douglas Gregor3545ff42009-09-21 16:56:56 +0000346
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 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001267}
Douglas Gregorc580c522010-01-14 01:09:38 +00001268
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
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001346 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 Gregor85f3f952015-07-07 03:57:15 +00003032
3033 case Decl::ObjCTypeParam: return CXCursor_TemplateTypeParameter;
3034
Douglas Gregor09c0eb12010-09-03 23:30:36 +00003035 default:
Dmitri Gribenkobdc80de2013-01-11 20:32:41 +00003036 if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor09c0eb12010-09-03 23:30:36 +00003037 switch (TD->getTagKind()) {
Joao Matosdc86f942012-08-31 18:45:21 +00003038 case TTK_Interface: // fall through
Douglas Gregor09c0eb12010-09-03 23:30:36 +00003039 case TTK_Struct: return CXCursor_StructDecl;
3040 case TTK_Class: return CXCursor_ClassDecl;
3041 case TTK_Union: return CXCursor_UnionDecl;
3042 case TTK_Enum: return CXCursor_EnumDecl;
3043 }
3044 }
3045 }
3046
3047 return CXCursor_UnexposedDecl;
3048}
3049
Douglas Gregor55b037b2010-07-08 20:55:51 +00003050static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results,
Douglas Gregor8cb17462012-10-09 16:01:50 +00003051 bool IncludeUndefined,
Douglas Gregor55b037b2010-07-08 20:55:51 +00003052 bool TargetTypeIsPointer = false) {
John McCall276321a2010-08-25 06:19:51 +00003053 typedef CodeCompletionResult Result;
Douglas Gregor55b037b2010-07-08 20:55:51 +00003054
Douglas Gregorf329c7c2009-10-30 16:50:04 +00003055 Results.EnterNewScope();
Douglas Gregor8e3e8742010-10-18 21:05:04 +00003056
Douglas Gregor9eb77012009-11-07 00:00:49 +00003057 for (Preprocessor::macro_iterator M = PP.macro_begin(),
3058 MEnd = PP.macro_end();
Douglas Gregor55b037b2010-07-08 20:55:51 +00003059 M != MEnd; ++M) {
Richard Smith20e883e2015-04-29 23:20:19 +00003060 auto MD = PP.getMacroDefinition(M->first);
3061 if (IncludeUndefined || MD) {
3062 if (MacroInfo *MI = MD.getMacroInfo())
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00003063 if (MI->isUsedForHeaderGuard())
3064 continue;
3065
Douglas Gregor8cb17462012-10-09 16:01:50 +00003066 Results.AddResult(Result(M->first,
Douglas Gregor6e240332010-08-16 16:18:59 +00003067 getMacroUsagePriority(M->first->getName(),
David Blaikiebbafb8a2012-03-11 07:00:24 +00003068 PP.getLangOpts(),
Douglas Gregor6e240332010-08-16 16:18:59 +00003069 TargetTypeIsPointer)));
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00003070 }
Douglas Gregor55b037b2010-07-08 20:55:51 +00003071 }
Douglas Gregor8e3e8742010-10-18 21:05:04 +00003072
Douglas Gregorf329c7c2009-10-30 16:50:04 +00003073 Results.ExitScope();
Douglas Gregor8e3e8742010-10-18 21:05:04 +00003074
Douglas Gregorf329c7c2009-10-30 16:50:04 +00003075}
3076
Douglas Gregorce0e8562010-08-23 21:54:33 +00003077static void AddPrettyFunctionResults(const LangOptions &LangOpts,
3078 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00003079 typedef CodeCompletionResult Result;
Douglas Gregorce0e8562010-08-23 21:54:33 +00003080
3081 Results.EnterNewScope();
Douglas Gregor8e3e8742010-10-18 21:05:04 +00003082
Douglas Gregorce0e8562010-08-23 21:54:33 +00003083 Results.AddResult(Result("__PRETTY_FUNCTION__", CCP_Constant));
3084 Results.AddResult(Result("__FUNCTION__", CCP_Constant));
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003085 if (LangOpts.C99 || LangOpts.CPlusPlus11)
Douglas Gregorce0e8562010-08-23 21:54:33 +00003086 Results.AddResult(Result("__func__", CCP_Constant));
3087 Results.ExitScope();
3088}
3089
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003090static void HandleCodeCompleteResults(Sema *S,
3091 CodeCompleteConsumer *CodeCompleter,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003092 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +00003093 CodeCompletionResult *Results,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003094 unsigned NumResults) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00003095 if (CodeCompleter)
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003096 CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults);
Douglas Gregor3545ff42009-09-21 16:56:56 +00003097}
3098
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003099static enum CodeCompletionContext::Kind mapCodeCompletionContext(Sema &S,
3100 Sema::ParserCompletionContext PCC) {
3101 switch (PCC) {
John McCallfaf5fb42010-08-26 23:41:50 +00003102 case Sema::PCC_Namespace:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003103 return CodeCompletionContext::CCC_TopLevel;
3104
John McCallfaf5fb42010-08-26 23:41:50 +00003105 case Sema::PCC_Class:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003106 return CodeCompletionContext::CCC_ClassStructUnion;
3107
John McCallfaf5fb42010-08-26 23:41:50 +00003108 case Sema::PCC_ObjCInterface:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003109 return CodeCompletionContext::CCC_ObjCInterface;
3110
John McCallfaf5fb42010-08-26 23:41:50 +00003111 case Sema::PCC_ObjCImplementation:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003112 return CodeCompletionContext::CCC_ObjCImplementation;
3113
John McCallfaf5fb42010-08-26 23:41:50 +00003114 case Sema::PCC_ObjCInstanceVariableList:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003115 return CodeCompletionContext::CCC_ObjCIvarList;
3116
John McCallfaf5fb42010-08-26 23:41:50 +00003117 case Sema::PCC_Template:
3118 case Sema::PCC_MemberTemplate:
Douglas Gregor0ac41382010-09-23 23:01:17 +00003119 if (S.CurContext->isFileContext())
3120 return CodeCompletionContext::CCC_TopLevel;
David Blaikie8a40f702012-01-17 06:56:22 +00003121 if (S.CurContext->isRecord())
Douglas Gregor0ac41382010-09-23 23:01:17 +00003122 return CodeCompletionContext::CCC_ClassStructUnion;
David Blaikie8a40f702012-01-17 06:56:22 +00003123 return CodeCompletionContext::CCC_Other;
Douglas Gregor0ac41382010-09-23 23:01:17 +00003124
John McCallfaf5fb42010-08-26 23:41:50 +00003125 case Sema::PCC_RecoveryInFunction:
Douglas Gregor0ac41382010-09-23 23:01:17 +00003126 return CodeCompletionContext::CCC_Recovery;
Douglas Gregorc769d6e2010-10-18 22:01:46 +00003127
John McCallfaf5fb42010-08-26 23:41:50 +00003128 case Sema::PCC_ForInit:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003129 if (S.getLangOpts().CPlusPlus || S.getLangOpts().C99 ||
3130 S.getLangOpts().ObjC1)
Douglas Gregorc769d6e2010-10-18 22:01:46 +00003131 return CodeCompletionContext::CCC_ParenthesizedExpression;
3132 else
3133 return CodeCompletionContext::CCC_Expression;
3134
3135 case Sema::PCC_Expression:
John McCallfaf5fb42010-08-26 23:41:50 +00003136 case Sema::PCC_Condition:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003137 return CodeCompletionContext::CCC_Expression;
3138
John McCallfaf5fb42010-08-26 23:41:50 +00003139 case Sema::PCC_Statement:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003140 return CodeCompletionContext::CCC_Statement;
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003141
John McCallfaf5fb42010-08-26 23:41:50 +00003142 case Sema::PCC_Type:
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003143 return CodeCompletionContext::CCC_Type;
Douglas Gregor5e35d592010-09-14 23:59:36 +00003144
3145 case Sema::PCC_ParenthesizedExpression:
3146 return CodeCompletionContext::CCC_ParenthesizedExpression;
Douglas Gregor80039242011-02-15 20:33:25 +00003147
3148 case Sema::PCC_LocalDeclarationSpecifiers:
3149 return CodeCompletionContext::CCC_Type;
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003150 }
David Blaikie8a40f702012-01-17 06:56:22 +00003151
3152 llvm_unreachable("Invalid ParserCompletionContext!");
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003153}
3154
Douglas Gregorac322ec2010-08-27 21:18:54 +00003155/// \brief If we're in a C++ virtual member function, add completion results
3156/// that invoke the functions we override, since it's common to invoke the
3157/// overridden function as well as adding new functionality.
3158///
3159/// \param S The semantic analysis object for which we are generating results.
3160///
3161/// \param InContext This context in which the nested-name-specifier preceding
3162/// the code-completion point
3163static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext,
3164 ResultBuilder &Results) {
3165 // Look through blocks.
3166 DeclContext *CurContext = S.CurContext;
3167 while (isa<BlockDecl>(CurContext))
3168 CurContext = CurContext->getParent();
3169
3170
3171 CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext);
3172 if (!Method || !Method->isVirtual())
3173 return;
3174
3175 // We need to have names for all of the parameters, if we're going to
3176 // generate a forwarding call.
Aaron Ballman43b68be2014-03-07 17:50:17 +00003177 for (auto P : Method->params())
3178 if (!P->getDeclName())
Douglas Gregorac322ec2010-08-27 21:18:54 +00003179 return;
Douglas Gregorac322ec2010-08-27 21:18:54 +00003180
Douglas Gregor75acd922011-09-27 23:30:47 +00003181 PrintingPolicy Policy = getCompletionPrintingPolicy(S);
Douglas Gregorac322ec2010-08-27 21:18:54 +00003182 for (CXXMethodDecl::method_iterator M = Method->begin_overridden_methods(),
3183 MEnd = Method->end_overridden_methods();
3184 M != MEnd; ++M) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003185 CodeCompletionBuilder Builder(Results.getAllocator(),
3186 Results.getCodeCompletionTUInfo());
Dmitri Gribenko6cfb1532013-02-14 13:53:30 +00003187 const CXXMethodDecl *Overridden = *M;
Douglas Gregorac322ec2010-08-27 21:18:54 +00003188 if (Overridden->getCanonicalDecl() == Method->getCanonicalDecl())
3189 continue;
3190
3191 // If we need a nested-name-specifier, add one now.
3192 if (!InContext) {
3193 NestedNameSpecifier *NNS
3194 = getRequiredQualification(S.Context, CurContext,
3195 Overridden->getDeclContext());
3196 if (NNS) {
3197 std::string Str;
3198 llvm::raw_string_ostream OS(Str);
Douglas Gregor75acd922011-09-27 23:30:47 +00003199 NNS->print(OS, Policy);
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00003200 Builder.AddTextChunk(Results.getAllocator().CopyString(OS.str()));
Douglas Gregorac322ec2010-08-27 21:18:54 +00003201 }
3202 } else if (!InContext->Equals(Overridden->getDeclContext()))
3203 continue;
3204
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00003205 Builder.AddTypedTextChunk(Results.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003206 Overridden->getNameAsString()));
3207 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregorac322ec2010-08-27 21:18:54 +00003208 bool FirstParam = true;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003209 for (auto P : Method->params()) {
Douglas Gregorac322ec2010-08-27 21:18:54 +00003210 if (FirstParam)
3211 FirstParam = false;
3212 else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003213 Builder.AddChunk(CodeCompletionString::CK_Comma);
Douglas Gregorac322ec2010-08-27 21:18:54 +00003214
Aaron Ballman43b68be2014-03-07 17:50:17 +00003215 Builder.AddPlaceholderChunk(
3216 Results.getAllocator().CopyString(P->getIdentifier()->getName()));
Douglas Gregorac322ec2010-08-27 21:18:54 +00003217 }
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003218 Builder.AddChunk(CodeCompletionString::CK_RightParen);
3219 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregorac322ec2010-08-27 21:18:54 +00003220 CCP_SuperCompletion,
Douglas Gregor78254c82012-03-27 23:34:16 +00003221 CXCursor_CXXMethod,
3222 CXAvailability_Available,
3223 Overridden));
Douglas Gregorac322ec2010-08-27 21:18:54 +00003224 Results.Ignore(Overridden);
3225 }
3226}
3227
Douglas Gregor07f43572012-01-29 18:15:03 +00003228void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc,
3229 ModuleIdPath Path) {
3230 typedef CodeCompletionResult Result;
3231 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003232 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor07f43572012-01-29 18:15:03 +00003233 CodeCompletionContext::CCC_Other);
3234 Results.EnterNewScope();
3235
3236 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003237 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregor07f43572012-01-29 18:15:03 +00003238 typedef CodeCompletionResult Result;
3239 if (Path.empty()) {
3240 // Enumerate all top-level modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003241 SmallVector<Module *, 8> Modules;
Douglas Gregor07f43572012-01-29 18:15:03 +00003242 PP.getHeaderSearchInfo().collectAllModules(Modules);
3243 for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
3244 Builder.AddTypedTextChunk(
3245 Builder.getAllocator().CopyString(Modules[I]->Name));
3246 Results.AddResult(Result(Builder.TakeString(),
3247 CCP_Declaration,
Argyrios Kyrtzidis345d05f2013-05-29 18:50:15 +00003248 CXCursor_ModuleImportDecl,
Douglas Gregor07f43572012-01-29 18:15:03 +00003249 Modules[I]->isAvailable()
3250 ? CXAvailability_Available
3251 : CXAvailability_NotAvailable));
3252 }
Daniel Jasper07e6c402013-08-05 20:26:17 +00003253 } else if (getLangOpts().Modules) {
Douglas Gregor07f43572012-01-29 18:15:03 +00003254 // Load the named module.
3255 Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path,
3256 Module::AllVisible,
3257 /*IsInclusionDirective=*/false);
3258 // Enumerate submodules.
3259 if (Mod) {
3260 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
3261 SubEnd = Mod->submodule_end();
3262 Sub != SubEnd; ++Sub) {
3263
3264 Builder.AddTypedTextChunk(
3265 Builder.getAllocator().CopyString((*Sub)->Name));
3266 Results.AddResult(Result(Builder.TakeString(),
3267 CCP_Declaration,
Argyrios Kyrtzidis345d05f2013-05-29 18:50:15 +00003268 CXCursor_ModuleImportDecl,
Douglas Gregor07f43572012-01-29 18:15:03 +00003269 (*Sub)->isAvailable()
3270 ? CXAvailability_Available
3271 : CXAvailability_NotAvailable));
3272 }
3273 }
3274 }
3275 Results.ExitScope();
3276 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
3277 Results.data(),Results.size());
3278}
3279
Douglas Gregor504a6ae2010-01-10 23:08:15 +00003280void Sema::CodeCompleteOrdinaryName(Scope *S,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003281 ParserCompletionContext CompletionContext) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003282 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003283 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00003284 mapCodeCompletionContext(*this, CompletionContext));
Douglas Gregorac322ec2010-08-27 21:18:54 +00003285 Results.EnterNewScope();
Douglas Gregor50832e02010-09-20 22:39:41 +00003286
Douglas Gregor504a6ae2010-01-10 23:08:15 +00003287 // Determine how to filter results, e.g., so that the names of
3288 // values (functions, enumerators, function templates, etc.) are
3289 // only allowed where we can have an expression.
3290 switch (CompletionContext) {
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003291 case PCC_Namespace:
3292 case PCC_Class:
3293 case PCC_ObjCInterface:
3294 case PCC_ObjCImplementation:
3295 case PCC_ObjCInstanceVariableList:
3296 case PCC_Template:
3297 case PCC_MemberTemplate:
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003298 case PCC_Type:
Douglas Gregor80039242011-02-15 20:33:25 +00003299 case PCC_LocalDeclarationSpecifiers:
Douglas Gregor504a6ae2010-01-10 23:08:15 +00003300 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
3301 break;
3302
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003303 case PCC_Statement:
Douglas Gregor5e35d592010-09-14 23:59:36 +00003304 case PCC_ParenthesizedExpression:
Douglas Gregor4d755e82010-08-24 23:58:17 +00003305 case PCC_Expression:
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003306 case PCC_ForInit:
3307 case PCC_Condition:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003308 if (WantTypesInContext(CompletionContext, getLangOpts()))
Douglas Gregor70febae2010-05-28 00:49:12 +00003309 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3310 else
3311 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
Douglas Gregorac322ec2010-08-27 21:18:54 +00003312
David Blaikiebbafb8a2012-03-11 07:00:24 +00003313 if (getLangOpts().CPlusPlus)
Craig Topperc3ec1492014-05-26 06:22:03 +00003314 MaybeAddOverrideCalls(*this, /*InContext=*/nullptr, Results);
Douglas Gregor504a6ae2010-01-10 23:08:15 +00003315 break;
Douglas Gregor6da3db42010-05-25 05:58:43 +00003316
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003317 case PCC_RecoveryInFunction:
Douglas Gregor6da3db42010-05-25 05:58:43 +00003318 // Unfiltered
3319 break;
Douglas Gregor504a6ae2010-01-10 23:08:15 +00003320 }
3321
Douglas Gregor9be0ed42010-08-26 16:36:48 +00003322 // If we are in a C++ non-static member function, check the qualifiers on
3323 // the member function to filter/prioritize the results list.
3324 if (CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext))
3325 if (CurMethod->isInstance())
3326 Results.setObjectTypeQualifiers(
3327 Qualifiers::fromCVRMask(CurMethod->getTypeQualifiers()));
3328
Douglas Gregorc580c522010-01-14 01:09:38 +00003329 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00003330 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3331 CodeCompleter->includeGlobals());
Douglas Gregor92253692009-12-07 09:54:55 +00003332
Douglas Gregorf98e6a22010-01-13 23:51:12 +00003333 AddOrdinaryNameResults(CompletionContext, S, *this, Results);
Douglas Gregor92253692009-12-07 09:54:55 +00003334 Results.ExitScope();
3335
Douglas Gregorce0e8562010-08-23 21:54:33 +00003336 switch (CompletionContext) {
Douglas Gregor5e35d592010-09-14 23:59:36 +00003337 case PCC_ParenthesizedExpression:
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003338 case PCC_Expression:
3339 case PCC_Statement:
3340 case PCC_RecoveryInFunction:
3341 if (S->getFnParent())
David Blaikiebbafb8a2012-03-11 07:00:24 +00003342 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003343 break;
3344
3345 case PCC_Namespace:
3346 case PCC_Class:
3347 case PCC_ObjCInterface:
3348 case PCC_ObjCImplementation:
3349 case PCC_ObjCInstanceVariableList:
3350 case PCC_Template:
3351 case PCC_MemberTemplate:
3352 case PCC_ForInit:
3353 case PCC_Condition:
3354 case PCC_Type:
Douglas Gregor80039242011-02-15 20:33:25 +00003355 case PCC_LocalDeclarationSpecifiers:
Douglas Gregorf02e5f32010-08-24 01:11:00 +00003356 break;
Douglas Gregorce0e8562010-08-23 21:54:33 +00003357 }
3358
Douglas Gregor9eb77012009-11-07 00:00:49 +00003359 if (CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00003360 AddMacroResults(PP, Results, false);
Douglas Gregorce0e8562010-08-23 21:54:33 +00003361
Douglas Gregor50832e02010-09-20 22:39:41 +00003362 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003363 Results.data(),Results.size());
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00003364}
3365
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003366static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
3367 ParsedType Receiver,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003368 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00003369 bool AtArgumentExpression,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003370 bool IsSuper,
3371 ResultBuilder &Results);
3372
3373void Sema::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS,
3374 bool AllowNonIdentifiers,
3375 bool AllowNestedNameSpecifiers) {
John McCall276321a2010-08-25 06:19:51 +00003376 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003377 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003378 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00003379 AllowNestedNameSpecifiers
3380 ? CodeCompletionContext::CCC_PotentiallyQualifiedName
3381 : CodeCompletionContext::CCC_Name);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003382 Results.EnterNewScope();
3383
3384 // Type qualifiers can come after names.
3385 Results.AddResult(Result("const"));
3386 Results.AddResult(Result("volatile"));
David Blaikiebbafb8a2012-03-11 07:00:24 +00003387 if (getLangOpts().C99)
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003388 Results.AddResult(Result("restrict"));
3389
David Blaikiebbafb8a2012-03-11 07:00:24 +00003390 if (getLangOpts().CPlusPlus) {
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003391 if (AllowNonIdentifiers) {
3392 Results.AddResult(Result("operator"));
3393 }
3394
3395 // Add nested-name-specifiers.
3396 if (AllowNestedNameSpecifiers) {
3397 Results.allowNestedNameSpecifiers();
Douglas Gregor0ac41382010-09-23 23:01:17 +00003398 Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003399 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3400 LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer,
3401 CodeCompleter->includeGlobals());
Craig Topperc3ec1492014-05-26 06:22:03 +00003402 Results.setFilter(nullptr);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003403 }
3404 }
3405 Results.ExitScope();
3406
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003407 // If we're in a context where we might have an expression (rather than a
3408 // declaration), and what we've seen so far is an Objective-C type that could
3409 // be a receiver of a class message, this may be a class message send with
3410 // the initial opening bracket '[' missing. Add appropriate completions.
3411 if (AllowNonIdentifiers && !AllowNestedNameSpecifiers &&
Richard Smithb4a9e862013-04-12 22:46:28 +00003412 DS.getParsedSpecifiers() == DeclSpec::PQ_TypeSpecifier &&
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003413 DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003414 DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified &&
3415 DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
Richard Smithb4a9e862013-04-12 22:46:28 +00003416 !DS.isTypeAltiVecVector() &&
3417 S &&
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003418 (S->getFlags() & Scope::DeclScope) != 0 &&
3419 (S->getFlags() & (Scope::ClassScope | Scope::TemplateParamScope |
3420 Scope::FunctionPrototypeScope |
3421 Scope::AtCatchScope)) == 0) {
3422 ParsedType T = DS.getRepAsType();
3423 if (!T.get().isNull() && T.get()->isObjCObjectOrInterfaceType())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00003424 AddClassMessageCompletions(*this, S, T, None, false, false, Results);
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003425 }
3426
Douglas Gregor56ccce02010-08-24 04:59:56 +00003427 // Note that we intentionally suppress macro results here, since we do not
3428 // encourage using macros to produce the names of entities.
3429
Douglas Gregor0ac41382010-09-23 23:01:17 +00003430 HandleCodeCompleteResults(this, CodeCompleter,
3431 Results.getCompletionContext(),
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003432 Results.data(), Results.size());
3433}
3434
Douglas Gregor68762e72010-08-23 21:17:50 +00003435struct Sema::CodeCompleteExpressionData {
3436 CodeCompleteExpressionData(QualType PreferredType = QualType())
3437 : PreferredType(PreferredType), IntegralConstantExpression(false),
3438 ObjCCollection(false) { }
3439
3440 QualType PreferredType;
3441 bool IntegralConstantExpression;
3442 bool ObjCCollection;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003443 SmallVector<Decl *, 4> IgnoreDecls;
Douglas Gregor68762e72010-08-23 21:17:50 +00003444};
3445
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003446/// \brief Perform code-completion in an expression context when we know what
3447/// type we're looking for.
Douglas Gregor68762e72010-08-23 21:17:50 +00003448void Sema::CodeCompleteExpression(Scope *S,
3449 const CodeCompleteExpressionData &Data) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003450 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003451 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003452 CodeCompletionContext::CCC_Expression);
Douglas Gregor68762e72010-08-23 21:17:50 +00003453 if (Data.ObjCCollection)
3454 Results.setFilter(&ResultBuilder::IsObjCCollection);
3455 else if (Data.IntegralConstantExpression)
Douglas Gregor85b50632010-07-28 21:50:18 +00003456 Results.setFilter(&ResultBuilder::IsIntegralConstantValue);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003457 else if (WantTypesInContext(PCC_Expression, getLangOpts()))
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003458 Results.setFilter(&ResultBuilder::IsOrdinaryName);
3459 else
3460 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
Douglas Gregor68762e72010-08-23 21:17:50 +00003461
3462 if (!Data.PreferredType.isNull())
3463 Results.setPreferredType(Data.PreferredType.getNonReferenceType());
3464
3465 // Ignore any declarations that we were told that we don't care about.
3466 for (unsigned I = 0, N = Data.IgnoreDecls.size(); I != N; ++I)
3467 Results.Ignore(Data.IgnoreDecls[I]);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003468
3469 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00003470 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
3471 CodeCompleter->includeGlobals());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003472
3473 Results.EnterNewScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003474 AddOrdinaryNameResults(PCC_Expression, S, *this, Results);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003475 Results.ExitScope();
3476
Douglas Gregor55b037b2010-07-08 20:55:51 +00003477 bool PreferredTypeIsPointer = false;
Douglas Gregor68762e72010-08-23 21:17:50 +00003478 if (!Data.PreferredType.isNull())
3479 PreferredTypeIsPointer = Data.PreferredType->isAnyPointerType()
3480 || Data.PreferredType->isMemberPointerType()
3481 || Data.PreferredType->isBlockPointerType();
Douglas Gregor55b037b2010-07-08 20:55:51 +00003482
Douglas Gregorce0e8562010-08-23 21:54:33 +00003483 if (S->getFnParent() &&
3484 !Data.ObjCCollection &&
3485 !Data.IntegralConstantExpression)
David Blaikiebbafb8a2012-03-11 07:00:24 +00003486 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregorce0e8562010-08-23 21:54:33 +00003487
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003488 if (CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00003489 AddMacroResults(PP, Results, false, PreferredTypeIsPointer);
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003490 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor68762e72010-08-23 21:17:50 +00003491 CodeCompletionContext(CodeCompletionContext::CCC_Expression,
3492 Data.PreferredType),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003493 Results.data(),Results.size());
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003494}
3495
Douglas Gregoreda7e542010-09-18 01:28:11 +00003496void Sema::CodeCompletePostfixExpression(Scope *S, ExprResult E) {
3497 if (E.isInvalid())
3498 CodeCompleteOrdinaryName(S, PCC_RecoveryInFunction);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003499 else if (getLangOpts().ObjC1)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003500 CodeCompleteObjCInstanceMessage(S, E.get(), None, false);
Douglas Gregored0b69d2010-09-15 16:23:04 +00003501}
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003502
Douglas Gregorb888acf2010-12-09 23:01:55 +00003503/// \brief The set of properties that have already been added, referenced by
3504/// property name.
3505typedef llvm::SmallPtrSet<IdentifierInfo*, 16> AddedPropertiesSet;
3506
Douglas Gregor9b4f3702012-06-12 13:44:08 +00003507/// \brief Retrieve the container definition, if any?
3508static ObjCContainerDecl *getContainerDef(ObjCContainerDecl *Container) {
3509 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
3510 if (Interface->hasDefinition())
3511 return Interface->getDefinition();
3512
3513 return Interface;
3514 }
3515
3516 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
3517 if (Protocol->hasDefinition())
3518 return Protocol->getDefinition();
3519
3520 return Protocol;
3521 }
3522 return Container;
3523}
3524
3525static void AddObjCProperties(ObjCContainerDecl *Container,
Douglas Gregor5d649882009-11-18 22:32:06 +00003526 bool AllowCategories,
Douglas Gregor95147142011-05-05 15:50:42 +00003527 bool AllowNullaryMethods,
Douglas Gregor9291bad2009-11-18 01:29:26 +00003528 DeclContext *CurContext,
Douglas Gregorb888acf2010-12-09 23:01:55 +00003529 AddedPropertiesSet &AddedProperties,
Douglas Gregor9291bad2009-11-18 01:29:26 +00003530 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00003531 typedef CodeCompletionResult Result;
Douglas Gregor9291bad2009-11-18 01:29:26 +00003532
Douglas Gregor9b4f3702012-06-12 13:44:08 +00003533 // Retrieve the definition.
3534 Container = getContainerDef(Container);
3535
Douglas Gregor9291bad2009-11-18 01:29:26 +00003536 // Add properties in this container.
Aaron Ballmand174edf2014-03-13 19:11:50 +00003537 for (const auto *P : Container->properties())
David Blaikie82e95a32014-11-19 07:49:47 +00003538 if (AddedProperties.insert(P->getIdentifier()).second)
Craig Topperc3ec1492014-05-26 06:22:03 +00003539 Results.MaybeAddResult(Result(P, Results.getBasePriority(P), nullptr),
Douglas Gregor0a0e2b32013-01-31 04:52:16 +00003540 CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +00003541
Douglas Gregor95147142011-05-05 15:50:42 +00003542 // Add nullary methods
3543 if (AllowNullaryMethods) {
3544 ASTContext &Context = Container->getASTContext();
Douglas Gregor75acd922011-09-27 23:30:47 +00003545 PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
Aaron Ballmanaff18c02014-03-13 19:03:34 +00003546 for (auto *M : Container->methods()) {
Douglas Gregor95147142011-05-05 15:50:42 +00003547 if (M->getSelector().isUnarySelector())
3548 if (IdentifierInfo *Name = M->getSelector().getIdentifierInfoForSlot(0))
David Blaikie82e95a32014-11-19 07:49:47 +00003549 if (AddedProperties.insert(Name).second) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003550 CodeCompletionBuilder Builder(Results.getAllocator(),
3551 Results.getCodeCompletionTUInfo());
Aaron Ballmanaff18c02014-03-13 19:03:34 +00003552 AddResultTypeChunk(Context, Policy, M, Builder);
Douglas Gregor95147142011-05-05 15:50:42 +00003553 Builder.AddTypedTextChunk(
3554 Results.getAllocator().CopyString(Name->getName()));
3555
Aaron Ballmanaff18c02014-03-13 19:03:34 +00003556 Results.MaybeAddResult(Result(Builder.TakeString(), M,
Douglas Gregor78254c82012-03-27 23:34:16 +00003557 CCP_MemberDeclaration + CCD_MethodAsProperty),
Douglas Gregor95147142011-05-05 15:50:42 +00003558 CurContext);
3559 }
3560 }
3561 }
3562
3563
Douglas Gregor9291bad2009-11-18 01:29:26 +00003564 // Add properties in referenced protocols.
3565 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00003566 for (auto *P : Protocol->protocols())
3567 AddObjCProperties(P, AllowCategories, AllowNullaryMethods, CurContext,
Douglas Gregor95147142011-05-05 15:50:42 +00003568 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003569 } else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){
Douglas Gregor5d649882009-11-18 22:32:06 +00003570 if (AllowCategories) {
3571 // Look through categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00003572 for (auto *Cat : IFace->known_categories())
3573 AddObjCProperties(Cat, AllowCategories, AllowNullaryMethods, CurContext,
3574 AddedProperties, Results);
Douglas Gregor5d649882009-11-18 22:32:06 +00003575 }
Aaron Ballman15063e12014-03-13 21:35:02 +00003576
Douglas Gregor9291bad2009-11-18 01:29:26 +00003577 // Look through protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00003578 for (auto *I : IFace->all_referenced_protocols())
3579 AddObjCProperties(I, AllowCategories, AllowNullaryMethods, CurContext,
Douglas Gregor95147142011-05-05 15:50:42 +00003580 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003581
3582 // Look in the superclass.
3583 if (IFace->getSuperClass())
Douglas Gregor95147142011-05-05 15:50:42 +00003584 AddObjCProperties(IFace->getSuperClass(), AllowCategories,
3585 AllowNullaryMethods, CurContext,
Douglas Gregorb888acf2010-12-09 23:01:55 +00003586 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003587 } else if (const ObjCCategoryDecl *Category
3588 = dyn_cast<ObjCCategoryDecl>(Container)) {
3589 // Look through protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00003590 for (auto *P : Category->protocols())
3591 AddObjCProperties(P, AllowCategories, AllowNullaryMethods, CurContext,
Douglas Gregor95147142011-05-05 15:50:42 +00003592 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003593 }
3594}
3595
Douglas Gregor1cc88a92012-01-23 15:59:30 +00003596void Sema::CodeCompleteMemberReferenceExpr(Scope *S, Expr *Base,
Douglas Gregor2436e712009-09-17 21:32:03 +00003597 SourceLocation OpLoc,
3598 bool IsArrow) {
Douglas Gregor1cc88a92012-01-23 15:59:30 +00003599 if (!Base || !CodeCompleter)
Douglas Gregor2436e712009-09-17 21:32:03 +00003600 return;
3601
Douglas Gregor1cc88a92012-01-23 15:59:30 +00003602 ExprResult ConvertedBase = PerformMemberExprBaseConversion(Base, IsArrow);
3603 if (ConvertedBase.isInvalid())
3604 return;
3605 Base = ConvertedBase.get();
3606
John McCall276321a2010-08-25 06:19:51 +00003607 typedef CodeCompletionResult Result;
Douglas Gregor3545ff42009-09-21 16:56:56 +00003608
Douglas Gregor2436e712009-09-17 21:32:03 +00003609 QualType BaseType = Base->getType();
Douglas Gregor3545ff42009-09-21 16:56:56 +00003610
3611 if (IsArrow) {
3612 if (const PointerType *Ptr = BaseType->getAs<PointerType>())
3613 BaseType = Ptr->getPointeeType();
3614 else if (BaseType->isObjCObjectPointerType())
Douglas Gregor9be0ed42010-08-26 16:36:48 +00003615 /*Do nothing*/ ;
Douglas Gregor3545ff42009-09-21 16:56:56 +00003616 else
3617 return;
3618 }
3619
Douglas Gregor21325842011-07-07 16:03:39 +00003620 enum CodeCompletionContext::Kind contextKind;
3621
3622 if (IsArrow) {
3623 contextKind = CodeCompletionContext::CCC_ArrowMemberAccess;
3624 }
3625 else {
3626 if (BaseType->isObjCObjectPointerType() ||
3627 BaseType->isObjCObjectOrInterfaceType()) {
3628 contextKind = CodeCompletionContext::CCC_ObjCPropertyAccess;
3629 }
3630 else {
3631 contextKind = CodeCompletionContext::CCC_DotMemberAccess;
3632 }
3633 }
3634
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003635 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003636 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor21325842011-07-07 16:03:39 +00003637 CodeCompletionContext(contextKind,
Douglas Gregor0ac41382010-09-23 23:01:17 +00003638 BaseType),
3639 &ResultBuilder::IsMember);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003640 Results.EnterNewScope();
3641 if (const RecordType *Record = BaseType->getAs<RecordType>()) {
Douglas Gregor9be0ed42010-08-26 16:36:48 +00003642 // Indicate that we are performing a member access, and the cv-qualifiers
3643 // for the base object type.
3644 Results.setObjectTypeQualifiers(BaseType.getQualifiers());
3645
Douglas Gregor9291bad2009-11-18 01:29:26 +00003646 // Access to a C/C++ class, struct, or union.
Douglas Gregor6ae4c522010-01-14 03:21:49 +00003647 Results.allowNestedNameSpecifiers();
Douglas Gregor09bbc652010-01-14 15:47:35 +00003648 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00003649 LookupVisibleDecls(Record->getDecl(), LookupMemberName, Consumer,
3650 CodeCompleter->includeGlobals());
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003651
David Blaikiebbafb8a2012-03-11 07:00:24 +00003652 if (getLangOpts().CPlusPlus) {
Douglas Gregor9291bad2009-11-18 01:29:26 +00003653 if (!Results.empty()) {
3654 // The "template" keyword can follow "->" or "." in the grammar.
3655 // However, we only want to suggest the template keyword if something
3656 // is dependent.
3657 bool IsDependent = BaseType->isDependentType();
3658 if (!IsDependent) {
3659 for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent())
Ted Kremenekc37877d2013-10-08 17:08:03 +00003660 if (DeclContext *Ctx = DepScope->getEntity()) {
Douglas Gregor9291bad2009-11-18 01:29:26 +00003661 IsDependent = Ctx->isDependentContext();
3662 break;
3663 }
3664 }
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003665
Douglas Gregor9291bad2009-11-18 01:29:26 +00003666 if (IsDependent)
Douglas Gregor78a21012010-01-14 16:01:26 +00003667 Results.AddResult(Result("template"));
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003668 }
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003669 }
Douglas Gregor9291bad2009-11-18 01:29:26 +00003670 } else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) {
3671 // Objective-C property reference.
Douglas Gregorb888acf2010-12-09 23:01:55 +00003672 AddedPropertiesSet AddedProperties;
Douglas Gregor9291bad2009-11-18 01:29:26 +00003673
3674 // Add property results based on our interface.
3675 const ObjCObjectPointerType *ObjCPtr
3676 = BaseType->getAsObjCInterfacePointerType();
3677 assert(ObjCPtr && "Non-NULL pointer guaranteed above!");
Douglas Gregor95147142011-05-05 15:50:42 +00003678 AddObjCProperties(ObjCPtr->getInterfaceDecl(), true,
3679 /*AllowNullaryMethods=*/true, CurContext,
Douglas Gregorb888acf2010-12-09 23:01:55 +00003680 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003681
3682 // Add properties from the protocols in a qualified interface.
Aaron Ballman83731462014-03-17 16:14:00 +00003683 for (auto *I : ObjCPtr->quals())
3684 AddObjCProperties(I, true, /*AllowNullaryMethods=*/true, CurContext,
Douglas Gregor95147142011-05-05 15:50:42 +00003685 AddedProperties, Results);
Douglas Gregor9291bad2009-11-18 01:29:26 +00003686 } else if ((IsArrow && BaseType->isObjCObjectPointerType()) ||
John McCall8b07ec22010-05-15 11:32:37 +00003687 (!IsArrow && BaseType->isObjCObjectType())) {
Douglas Gregor9291bad2009-11-18 01:29:26 +00003688 // Objective-C instance variable access.
Craig Topperc3ec1492014-05-26 06:22:03 +00003689 ObjCInterfaceDecl *Class = nullptr;
Douglas Gregor9291bad2009-11-18 01:29:26 +00003690 if (const ObjCObjectPointerType *ObjCPtr
3691 = BaseType->getAs<ObjCObjectPointerType>())
3692 Class = ObjCPtr->getInterfaceDecl();
3693 else
John McCall8b07ec22010-05-15 11:32:37 +00003694 Class = BaseType->getAs<ObjCObjectType>()->getInterface();
Douglas Gregor9291bad2009-11-18 01:29:26 +00003695
3696 // Add all ivars from this class and its superclasses.
Douglas Gregor2b8162b2010-01-14 16:08:12 +00003697 if (Class) {
3698 CodeCompletionDeclConsumer Consumer(Results, CurContext);
3699 Results.setFilter(&ResultBuilder::IsObjCIvar);
Douglas Gregor39982192010-08-15 06:18:01 +00003700 LookupVisibleDecls(Class, LookupMemberName, Consumer,
3701 CodeCompleter->includeGlobals());
Douglas Gregor9291bad2009-11-18 01:29:26 +00003702 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00003703 }
Douglas Gregor9291bad2009-11-18 01:29:26 +00003704
3705 // FIXME: How do we cope with isa?
3706
3707 Results.ExitScope();
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003708
Daniel Dunbar242ea9a2009-11-13 08:58:20 +00003709 // Hand off the results found for code completion.
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003710 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0ac41382010-09-23 23:01:17 +00003711 Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003712 Results.data(),Results.size());
Douglas Gregor2436e712009-09-17 21:32:03 +00003713}
3714
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003715void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) {
3716 if (!CodeCompleter)
3717 return;
Craig Topperc3ec1492014-05-26 06:22:03 +00003718
3719 ResultBuilder::LookupFilter Filter = nullptr;
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003720 enum CodeCompletionContext::Kind ContextKind
3721 = CodeCompletionContext::CCC_Other;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003722 switch ((DeclSpec::TST)TagSpec) {
3723 case DeclSpec::TST_enum:
Douglas Gregor3545ff42009-09-21 16:56:56 +00003724 Filter = &ResultBuilder::IsEnum;
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003725 ContextKind = CodeCompletionContext::CCC_EnumTag;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003726 break;
3727
3728 case DeclSpec::TST_union:
Douglas Gregor3545ff42009-09-21 16:56:56 +00003729 Filter = &ResultBuilder::IsUnion;
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003730 ContextKind = CodeCompletionContext::CCC_UnionTag;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003731 break;
3732
3733 case DeclSpec::TST_struct:
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003734 case DeclSpec::TST_class:
Joao Matosdc86f942012-08-31 18:45:21 +00003735 case DeclSpec::TST_interface:
Douglas Gregor3545ff42009-09-21 16:56:56 +00003736 Filter = &ResultBuilder::IsClassOrStruct;
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003737 ContextKind = CodeCompletionContext::CCC_ClassOrStructTag;
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003738 break;
3739
3740 default:
David Blaikie83d382b2011-09-23 05:06:16 +00003741 llvm_unreachable("Unknown type specifier kind in CodeCompleteTag");
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003742 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00003743
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003744 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
3745 CodeCompleter->getCodeCompletionTUInfo(), ContextKind);
Douglas Gregora6e2edc2010-01-14 03:27:13 +00003746 CodeCompletionDeclConsumer Consumer(Results, CurContext);
John McCalle87beb22010-04-23 18:46:30 +00003747
3748 // First pass: look for tags.
3749 Results.setFilter(Filter);
Douglas Gregor39982192010-08-15 06:18:01 +00003750 LookupVisibleDecls(S, LookupTagName, Consumer,
3751 CodeCompleter->includeGlobals());
John McCalle87beb22010-04-23 18:46:30 +00003752
Douglas Gregor39982192010-08-15 06:18:01 +00003753 if (CodeCompleter->includeGlobals()) {
3754 // Second pass: look for nested name specifiers.
3755 Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
3756 LookupVisibleDecls(S, LookupNestedNameSpecifierName, Consumer);
3757 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00003758
Douglas Gregor0ac41382010-09-23 23:01:17 +00003759 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003760 Results.data(),Results.size());
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003761}
3762
Douglas Gregor28c78432010-08-27 17:35:51 +00003763void Sema::CodeCompleteTypeQualifiers(DeclSpec &DS) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003764 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003765 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003766 CodeCompletionContext::CCC_TypeQualifiers);
Douglas Gregor28c78432010-08-27 17:35:51 +00003767 Results.EnterNewScope();
3768 if (!(DS.getTypeQualifiers() & DeclSpec::TQ_const))
3769 Results.AddResult("const");
3770 if (!(DS.getTypeQualifiers() & DeclSpec::TQ_volatile))
3771 Results.AddResult("volatile");
David Blaikiebbafb8a2012-03-11 07:00:24 +00003772 if (getLangOpts().C99 &&
Douglas Gregor28c78432010-08-27 17:35:51 +00003773 !(DS.getTypeQualifiers() & DeclSpec::TQ_restrict))
3774 Results.AddResult("restrict");
Richard Smith8e1ac332013-03-28 01:55:44 +00003775 if (getLangOpts().C11 &&
3776 !(DS.getTypeQualifiers() & DeclSpec::TQ_atomic))
3777 Results.AddResult("_Atomic");
Douglas Gregor28c78432010-08-27 17:35:51 +00003778 Results.ExitScope();
3779 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0ac41382010-09-23 23:01:17 +00003780 Results.getCompletionContext(),
Douglas Gregor28c78432010-08-27 17:35:51 +00003781 Results.data(), Results.size());
3782}
3783
Douglas Gregord328d572009-09-21 18:10:23 +00003784void Sema::CodeCompleteCase(Scope *S) {
John McCallaab3e412010-08-25 08:40:02 +00003785 if (getCurFunction()->SwitchStack.empty() || !CodeCompleter)
Douglas Gregord328d572009-09-21 18:10:23 +00003786 return;
John McCall5939b162011-08-06 07:30:58 +00003787
John McCallaab3e412010-08-25 08:40:02 +00003788 SwitchStmt *Switch = getCurFunction()->SwitchStack.back();
John McCall5939b162011-08-06 07:30:58 +00003789 QualType type = Switch->getCond()->IgnoreImplicit()->getType();
3790 if (!type->isEnumeralType()) {
3791 CodeCompleteExpressionData Data(type);
Douglas Gregor68762e72010-08-23 21:17:50 +00003792 Data.IntegralConstantExpression = true;
3793 CodeCompleteExpression(S, Data);
Douglas Gregord328d572009-09-21 18:10:23 +00003794 return;
Douglas Gregor85b50632010-07-28 21:50:18 +00003795 }
Douglas Gregord328d572009-09-21 18:10:23 +00003796
3797 // Code-complete the cases of a switch statement over an enumeration type
3798 // by providing the list of
John McCall5939b162011-08-06 07:30:58 +00003799 EnumDecl *Enum = type->castAs<EnumType>()->getDecl();
Douglas Gregor9b4f3702012-06-12 13:44:08 +00003800 if (EnumDecl *Def = Enum->getDefinition())
3801 Enum = Def;
Douglas Gregord328d572009-09-21 18:10:23 +00003802
3803 // Determine which enumerators we have already seen in the switch statement.
3804 // FIXME: Ideally, we would also be able to look *past* the code-completion
3805 // token, in case we are code-completing in the middle of the switch and not
3806 // at the end. However, we aren't able to do so at the moment.
3807 llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen;
Craig Topperc3ec1492014-05-26 06:22:03 +00003808 NestedNameSpecifier *Qualifier = nullptr;
Douglas Gregord328d572009-09-21 18:10:23 +00003809 for (SwitchCase *SC = Switch->getSwitchCaseList(); SC;
3810 SC = SC->getNextSwitchCase()) {
3811 CaseStmt *Case = dyn_cast<CaseStmt>(SC);
3812 if (!Case)
3813 continue;
3814
3815 Expr *CaseVal = Case->getLHS()->IgnoreParenCasts();
3816 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal))
3817 if (EnumConstantDecl *Enumerator
3818 = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
3819 // We look into the AST of the case statement to determine which
3820 // enumerator was named. Alternatively, we could compute the value of
3821 // the integral constant expression, then compare it against the
3822 // values of each enumerator. However, value-based approach would not
3823 // work as well with C++ templates where enumerators declared within a
3824 // template are type- and value-dependent.
3825 EnumeratorsSeen.insert(Enumerator);
3826
Douglas Gregorf2510672009-09-21 19:57:38 +00003827 // If this is a qualified-id, keep track of the nested-name-specifier
3828 // so that we can reproduce it as part of code completion, e.g.,
Douglas Gregord328d572009-09-21 18:10:23 +00003829 //
3830 // switch (TagD.getKind()) {
3831 // case TagDecl::TK_enum:
3832 // break;
3833 // case XXX
3834 //
Douglas Gregorf2510672009-09-21 19:57:38 +00003835 // At the XXX, our completions are TagDecl::TK_union,
Douglas Gregord328d572009-09-21 18:10:23 +00003836 // TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union,
3837 // TK_struct, and TK_class.
Douglas Gregor4bd90e52009-10-23 18:54:35 +00003838 Qualifier = DRE->getQualifier();
Douglas Gregord328d572009-09-21 18:10:23 +00003839 }
3840 }
3841
David Blaikiebbafb8a2012-03-11 07:00:24 +00003842 if (getLangOpts().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) {
Douglas Gregorf2510672009-09-21 19:57:38 +00003843 // If there are no prior enumerators in C++, check whether we have to
3844 // qualify the names of the enumerators that we suggest, because they
3845 // may not be visible in this scope.
Douglas Gregord3cebdb2012-02-01 05:02:47 +00003846 Qualifier = getRequiredQualification(Context, CurContext, Enum);
Douglas Gregorf2510672009-09-21 19:57:38 +00003847 }
3848
Douglas Gregord328d572009-09-21 18:10:23 +00003849 // Add any enumerators that have not yet been mentioned.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003850 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00003851 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00003852 CodeCompletionContext::CCC_Expression);
Douglas Gregord328d572009-09-21 18:10:23 +00003853 Results.EnterNewScope();
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00003854 for (auto *E : Enum->enumerators()) {
3855 if (EnumeratorsSeen.count(E))
Douglas Gregord328d572009-09-21 18:10:23 +00003856 continue;
3857
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00003858 CodeCompletionResult R(E, CCP_EnumInCase, Qualifier);
Craig Topperc3ec1492014-05-26 06:22:03 +00003859 Results.AddResult(R, CurContext, nullptr, false);
Douglas Gregord328d572009-09-21 18:10:23 +00003860 }
3861 Results.ExitScope();
Douglas Gregor285560922010-04-06 20:02:15 +00003862
Douglas Gregor21325842011-07-07 16:03:39 +00003863 //We need to make sure we're setting the right context,
3864 //so only say we include macros if the code completer says we do
3865 enum CodeCompletionContext::Kind kind = CodeCompletionContext::CCC_Other;
3866 if (CodeCompleter->includeMacros()) {
Douglas Gregor8cb17462012-10-09 16:01:50 +00003867 AddMacroResults(PP, Results, false);
Douglas Gregor21325842011-07-07 16:03:39 +00003868 kind = CodeCompletionContext::CCC_OtherWithMacros;
3869 }
3870
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003871 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor21325842011-07-07 16:03:39 +00003872 kind,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003873 Results.data(),Results.size());
Douglas Gregord328d572009-09-21 18:10:23 +00003874}
3875
Robert Wilhelm16e94b92013-08-09 18:02:13 +00003876static bool anyNullArguments(ArrayRef<Expr *> Args) {
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003877 if (Args.size() && !Args.data())
Douglas Gregor6ed3eb82010-05-30 06:10:08 +00003878 return true;
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003879
3880 for (unsigned I = 0; I != Args.size(); ++I)
Douglas Gregor6ed3eb82010-05-30 06:10:08 +00003881 if (!Args[I])
3882 return true;
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003883
Douglas Gregor6ed3eb82010-05-30 06:10:08 +00003884 return false;
3885}
3886
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003887typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate;
3888
Francisco Lopes da Silva8cafefa2015-01-29 05:54:59 +00003889static void mergeCandidatesWithResults(Sema &SemaRef,
3890 SmallVectorImpl<ResultCandidate> &Results,
3891 OverloadCandidateSet &CandidateSet,
3892 SourceLocation Loc) {
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003893 if (!CandidateSet.empty()) {
3894 // Sort the overload candidate set by placing the best overloads first.
3895 std::stable_sort(
3896 CandidateSet.begin(), CandidateSet.end(),
3897 [&](const OverloadCandidate &X, const OverloadCandidate &Y) {
3898 return isBetterOverloadCandidate(SemaRef, X, Y, Loc);
3899 });
3900
3901 // Add the remaining viable overload candidates as code-completion results.
3902 for (auto &Candidate : CandidateSet)
3903 if (Candidate.Viable)
3904 Results.push_back(ResultCandidate(Candidate.Function));
3905 }
3906}
3907
3908/// \brief Get the type of the Nth parameter from a given set of overload
3909/// candidates.
Francisco Lopes da Silva8cafefa2015-01-29 05:54:59 +00003910static QualType getParamType(Sema &SemaRef,
3911 ArrayRef<ResultCandidate> Candidates,
3912 unsigned N) {
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003913
3914 // Given the overloads 'Candidates' for a function call matching all arguments
3915 // up to N, return the type of the Nth parameter if it is the same for all
3916 // overload candidates.
3917 QualType ParamType;
3918 for (auto &Candidate : Candidates) {
3919 if (auto FType = Candidate.getFunctionType())
3920 if (auto Proto = dyn_cast<FunctionProtoType>(FType))
3921 if (N < Proto->getNumParams()) {
3922 if (ParamType.isNull())
3923 ParamType = Proto->getParamType(N);
3924 else if (!SemaRef.Context.hasSameUnqualifiedType(
3925 ParamType.getNonReferenceType(),
3926 Proto->getParamType(N).getNonReferenceType()))
3927 // Otherwise return a default-constructed QualType.
3928 return QualType();
3929 }
3930 }
3931
3932 return ParamType;
3933}
3934
Francisco Lopes da Silva8cafefa2015-01-29 05:54:59 +00003935static void CodeCompleteOverloadResults(Sema &SemaRef, Scope *S,
3936 MutableArrayRef<ResultCandidate> Candidates,
3937 unsigned CurrentArg,
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003938 bool CompleteExpressionWithCurrentArg = true) {
3939 QualType ParamType;
3940 if (CompleteExpressionWithCurrentArg)
3941 ParamType = getParamType(SemaRef, Candidates, CurrentArg);
3942
3943 if (ParamType.isNull())
3944 SemaRef.CodeCompleteOrdinaryName(S, Sema::PCC_Expression);
3945 else
3946 SemaRef.CodeCompleteExpression(S, ParamType);
3947
3948 if (!Candidates.empty())
3949 SemaRef.CodeCompleter->ProcessOverloadCandidates(SemaRef, CurrentArg,
3950 Candidates.data(),
3951 Candidates.size());
3952}
3953
3954void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args) {
Douglas Gregorcabea402009-09-22 15:41:20 +00003955 if (!CodeCompleter)
3956 return;
Douglas Gregor3ef59522009-12-11 19:06:04 +00003957
3958 // When we're code-completing for a call, we fall back to ordinary
3959 // name code-completion whenever we can't produce specific
3960 // results. We may want to revisit this strategy in the future,
3961 // e.g., by merging the two kinds of results.
3962
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003963 // FIXME: Provide support for variadic template functions.
Douglas Gregor3ef59522009-12-11 19:06:04 +00003964
Douglas Gregorcabea402009-09-22 15:41:20 +00003965 // Ignore type-dependent call expressions entirely.
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003966 if (!Fn || Fn->isTypeDependent() || anyNullArguments(Args) ||
3967 Expr::hasAnyTypeDependentArguments(Args)) {
Douglas Gregor00c37ef2010-08-11 21:23:17 +00003968 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregorcabea402009-09-22 15:41:20 +00003969 return;
Douglas Gregor3ef59522009-12-11 19:06:04 +00003970 }
Douglas Gregorcabea402009-09-22 15:41:20 +00003971
John McCall57500772009-12-16 12:17:52 +00003972 // Build an overload candidate set based on the functions we find.
John McCallbc077cf2010-02-08 23:07:23 +00003973 SourceLocation Loc = Fn->getExprLoc();
Richard Smith100b24a2014-04-17 01:52:14 +00003974 OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal);
John McCall57500772009-12-16 12:17:52 +00003975
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003976 SmallVector<ResultCandidate, 8> Results;
Douglas Gregorff59f672010-01-21 15:46:19 +00003977
John McCall57500772009-12-16 12:17:52 +00003978 Expr *NakedFn = Fn->IgnoreParenCasts();
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003979 if (auto ULE = dyn_cast<UnresolvedLookupExpr>(NakedFn))
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003980 AddOverloadedCallCandidates(ULE, Args, CandidateSet,
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003981 /*PartialOverloading=*/true);
3982 else if (auto UME = dyn_cast<UnresolvedMemberExpr>(NakedFn)) {
3983 TemplateArgumentListInfo TemplateArgsBuffer, *TemplateArgs = nullptr;
3984 if (UME->hasExplicitTemplateArgs()) {
3985 UME->copyTemplateArgumentsInto(TemplateArgsBuffer);
3986 TemplateArgs = &TemplateArgsBuffer;
Douglas Gregorff59f672010-01-21 15:46:19 +00003987 }
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003988 SmallVector<Expr *, 12> ArgExprs(1, UME->getBase());
3989 ArgExprs.append(Args.begin(), Args.end());
3990 UnresolvedSet<8> Decls;
3991 Decls.append(UME->decls_begin(), UME->decls_end());
3992 AddFunctionCandidates(Decls, ArgExprs, CandidateSet, TemplateArgs,
3993 /*SuppressUsedConversions=*/false,
3994 /*PartialOverloading=*/true);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00003995 } else {
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00003996 FunctionDecl *FD = nullptr;
3997 if (auto MCE = dyn_cast<MemberExpr>(NakedFn))
3998 FD = dyn_cast<FunctionDecl>(MCE->getMemberDecl());
3999 else if (auto DRE = dyn_cast<DeclRefExpr>(NakedFn))
4000 FD = dyn_cast<FunctionDecl>(DRE->getDecl());
Francisco Lopes da Silvac6ccc4f2015-01-22 21:14:08 +00004001 if (FD) { // We check whether it's a resolved function declaration.
Francisco Lopes da Silva0c010cd2015-01-28 14:17:22 +00004002 if (!getLangOpts().CPlusPlus ||
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00004003 !FD->getType()->getAs<FunctionProtoType>())
4004 Results.push_back(ResultCandidate(FD));
4005 else
4006 AddOverloadCandidate(FD, DeclAccessPair::make(FD, FD->getAccess()),
4007 Args, CandidateSet,
4008 /*SuppressUsedConversions=*/false,
4009 /*PartialOverloading=*/true);
Francisco Lopes da Silvac6ccc4f2015-01-22 21:14:08 +00004010
4011 } else if (auto DC = NakedFn->getType()->getAsCXXRecordDecl()) {
4012 // If expression's type is CXXRecordDecl, it may overload the function
4013 // call operator, so we check if it does and add them as candidates.
Francisco Lopes da Silvaa349a8a2015-01-25 08:47:59 +00004014 // A complete type is needed to lookup for member function call operators.
Francisco Lopes da Silva1a4f8552015-01-25 17:00:47 +00004015 if (!RequireCompleteType(Loc, NakedFn->getType(), 0)) {
Francisco Lopes da Silvaa349a8a2015-01-25 08:47:59 +00004016 DeclarationName OpName = Context.DeclarationNames
4017 .getCXXOperatorName(OO_Call);
4018 LookupResult R(*this, OpName, Loc, LookupOrdinaryName);
4019 LookupQualifiedName(R, DC);
4020 R.suppressDiagnostics();
4021 SmallVector<Expr *, 12> ArgExprs(1, NakedFn);
4022 ArgExprs.append(Args.begin(), Args.end());
4023 AddFunctionCandidates(R.asUnresolvedSet(), ArgExprs, CandidateSet,
4024 /*ExplicitArgs=*/nullptr,
4025 /*SuppressUsedConversions=*/false,
4026 /*PartialOverloading=*/true);
4027 }
Francisco Lopes da Silvac6ccc4f2015-01-22 21:14:08 +00004028 } else {
4029 // Lastly we check whether expression's type is function pointer or
4030 // function.
4031 QualType T = NakedFn->getType();
4032 if (!T->getPointeeType().isNull())
4033 T = T->getPointeeType();
4034
4035 if (auto FP = T->getAs<FunctionProtoType>()) {
4036 if (!TooManyArguments(FP->getNumParams(), Args.size(),
Francisco Lopes da Silva62a9a4f2015-01-23 13:17:51 +00004037 /*PartialOverloading=*/true) ||
4038 FP->isVariadic())
Francisco Lopes da Silvac6ccc4f2015-01-22 21:14:08 +00004039 Results.push_back(ResultCandidate(FP));
4040 } else if (auto FT = T->getAs<FunctionType>())
Francisco Lopes da Silva62a9a4f2015-01-23 13:17:51 +00004041 // No prototype and declaration, it may be a K & R style function.
Francisco Lopes da Silvac6ccc4f2015-01-22 21:14:08 +00004042 Results.push_back(ResultCandidate(FT));
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004043 }
Douglas Gregorcabea402009-09-22 15:41:20 +00004044 }
Douglas Gregor3ef59522009-12-11 19:06:04 +00004045
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00004046 mergeCandidatesWithResults(*this, Results, CandidateSet, Loc);
4047 CodeCompleteOverloadResults(*this, S, Results, Args.size(),
4048 !CandidateSet.empty());
4049}
4050
4051void Sema::CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc,
4052 ArrayRef<Expr *> Args) {
4053 if (!CodeCompleter)
4054 return;
4055
4056 // A complete type is needed to lookup for constructors.
4057 if (RequireCompleteType(Loc, Type, 0))
4058 return;
4059
Argyrios Kyrtzidisee1d76f2015-03-13 07:39:30 +00004060 CXXRecordDecl *RD = Type->getAsCXXRecordDecl();
4061 if (!RD) {
4062 CodeCompleteExpression(S, Type);
4063 return;
4064 }
4065
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00004066 // FIXME: Provide support for member initializers.
4067 // FIXME: Provide support for variadic template constructors.
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00004068
4069 OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal);
4070
Argyrios Kyrtzidisee1d76f2015-03-13 07:39:30 +00004071 for (auto C : LookupConstructors(RD)) {
Francisco Lopes da Silva975a9f62015-01-21 16:24:11 +00004072 if (auto FD = dyn_cast<FunctionDecl>(C)) {
4073 AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()),
4074 Args, CandidateSet,
4075 /*SuppressUsedConversions=*/false,
4076 /*PartialOverloading=*/true);
4077 } else if (auto FTD = dyn_cast<FunctionTemplateDecl>(C)) {
4078 AddTemplateOverloadCandidate(FTD,
4079 DeclAccessPair::make(FTD, C->getAccess()),
4080 /*ExplicitTemplateArgs=*/nullptr,
4081 Args, CandidateSet,
4082 /*SuppressUsedConversions=*/false,
4083 /*PartialOverloading=*/true);
4084 }
4085 }
4086
4087 SmallVector<ResultCandidate, 8> Results;
4088 mergeCandidatesWithResults(*this, Results, CandidateSet, Loc);
4089 CodeCompleteOverloadResults(*this, S, Results, Args.size());
Douglas Gregorcabea402009-09-22 15:41:20 +00004090}
4091
John McCall48871652010-08-21 09:40:31 +00004092void Sema::CodeCompleteInitializer(Scope *S, Decl *D) {
4093 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004094 if (!VD) {
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004095 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004096 return;
4097 }
4098
4099 CodeCompleteExpression(S, VD->getType());
4100}
4101
4102void Sema::CodeCompleteReturn(Scope *S) {
4103 QualType ResultType;
4104 if (isa<BlockDecl>(CurContext)) {
4105 if (BlockScopeInfo *BSI = getCurBlock())
4106 ResultType = BSI->ReturnType;
4107 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(CurContext))
Alp Toker314cc812014-01-25 16:55:45 +00004108 ResultType = Function->getReturnType();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004109 else if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(CurContext))
Alp Toker314cc812014-01-25 16:55:45 +00004110 ResultType = Method->getReturnType();
4111
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004112 if (ResultType.isNull())
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004113 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004114 else
4115 CodeCompleteExpression(S, ResultType);
4116}
4117
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004118void Sema::CodeCompleteAfterIf(Scope *S) {
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004119 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004120 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004121 mapCodeCompletionContext(*this, PCC_Statement));
4122 Results.setFilter(&ResultBuilder::IsOrdinaryName);
4123 Results.EnterNewScope();
4124
4125 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4126 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4127 CodeCompleter->includeGlobals());
4128
4129 AddOrdinaryNameResults(PCC_Statement, S, *this, Results);
4130
4131 // "else" block
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004132 CodeCompletionBuilder Builder(Results.getAllocator(),
4133 Results.getCodeCompletionTUInfo());
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004134 Builder.AddTypedTextChunk("else");
Douglas Gregor3a5d6c22012-02-16 17:49:04 +00004135 if (Results.includeCodePatterns()) {
4136 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4137 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4138 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
4139 Builder.AddPlaceholderChunk("statements");
4140 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
4141 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4142 }
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004143 Results.AddResult(Builder.TakeString());
4144
4145 // "else if" block
4146 Builder.AddTypedTextChunk("else");
4147 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4148 Builder.AddTextChunk("if");
4149 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4150 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
David Blaikiebbafb8a2012-03-11 07:00:24 +00004151 if (getLangOpts().CPlusPlus)
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004152 Builder.AddPlaceholderChunk("condition");
4153 else
4154 Builder.AddPlaceholderChunk("expression");
4155 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregor3a5d6c22012-02-16 17:49:04 +00004156 if (Results.includeCodePatterns()) {
4157 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4158 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4159 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
4160 Builder.AddPlaceholderChunk("statements");
4161 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
4162 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4163 }
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004164 Results.AddResult(Builder.TakeString());
4165
4166 Results.ExitScope();
4167
4168 if (S->getFnParent())
David Blaikiebbafb8a2012-03-11 07:00:24 +00004169 AddPrettyFunctionResults(PP.getLangOpts(), Results);
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004170
4171 if (CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00004172 AddMacroResults(PP, Results, false);
Douglas Gregor4ecb7202011-07-30 08:36:53 +00004173
4174 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4175 Results.data(),Results.size());
4176}
4177
Richard Trieu2bd04012011-09-09 02:00:50 +00004178void Sema::CodeCompleteAssignmentRHS(Scope *S, Expr *LHS) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004179 if (LHS)
4180 CodeCompleteExpression(S, static_cast<Expr *>(LHS)->getType());
4181 else
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004182 CodeCompleteOrdinaryName(S, PCC_Expression);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00004183}
4184
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004185void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS,
Douglas Gregor2436e712009-09-17 21:32:03 +00004186 bool EnteringContext) {
4187 if (!SS.getScopeRep() || !CodeCompleter)
4188 return;
4189
Douglas Gregor3545ff42009-09-21 16:56:56 +00004190 DeclContext *Ctx = computeDeclContext(SS, EnteringContext);
4191 if (!Ctx)
4192 return;
Douglas Gregor800f2f02009-12-11 18:28:39 +00004193
4194 // Try to instantiate any non-dependent declaration contexts before
4195 // we look in them.
John McCall0b66eb32010-05-01 00:40:08 +00004196 if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx))
Douglas Gregor800f2f02009-12-11 18:28:39 +00004197 return;
4198
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004199 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004200 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004201 CodeCompletionContext::CCC_Name);
Douglas Gregorac322ec2010-08-27 21:18:54 +00004202 Results.EnterNewScope();
Douglas Gregor0ac41382010-09-23 23:01:17 +00004203
Douglas Gregor3545ff42009-09-21 16:56:56 +00004204 // The "template" keyword can follow "::" in the grammar, but only
4205 // put it into the grammar if the nested-name-specifier is dependent.
Aaron Ballman4a979672014-01-03 13:56:08 +00004206 NestedNameSpecifier *NNS = SS.getScopeRep();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004207 if (!Results.empty() && NNS->isDependent())
Douglas Gregor78a21012010-01-14 16:01:26 +00004208 Results.AddResult("template");
Douglas Gregorac322ec2010-08-27 21:18:54 +00004209
4210 // Add calls to overridden virtual functions, if there are any.
4211 //
4212 // FIXME: This isn't wonderful, because we don't know whether we're actually
4213 // in a context that permits expressions. This is a general issue with
4214 // qualified-id completions.
4215 if (!EnteringContext)
4216 MaybeAddOverrideCalls(*this, Ctx, Results);
4217 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004218
Douglas Gregorac322ec2010-08-27 21:18:54 +00004219 CodeCompletionDeclConsumer Consumer(Results, CurContext);
4220 LookupVisibleDecls(Ctx, LookupOrdinaryName, Consumer);
4221
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004222 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregorc1679ec2011-07-25 17:48:11 +00004223 Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004224 Results.data(),Results.size());
Douglas Gregor2436e712009-09-17 21:32:03 +00004225}
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004226
4227void Sema::CodeCompleteUsing(Scope *S) {
4228 if (!CodeCompleter)
4229 return;
4230
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004231 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004232 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00004233 CodeCompletionContext::CCC_PotentiallyQualifiedName,
4234 &ResultBuilder::IsNestedNameSpecifier);
Douglas Gregor64b12b52009-09-22 23:31:26 +00004235 Results.EnterNewScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004236
4237 // If we aren't in class scope, we could see the "namespace" keyword.
4238 if (!S->isClassScope())
John McCall276321a2010-08-25 06:19:51 +00004239 Results.AddResult(CodeCompletionResult("namespace"));
Douglas Gregor3545ff42009-09-21 16:56:56 +00004240
4241 // After "using", we can see anything that would start a
4242 // nested-name-specifier.
Douglas Gregora6e2edc2010-01-14 03:27:13 +00004243 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00004244 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4245 CodeCompleter->includeGlobals());
Douglas Gregor64b12b52009-09-22 23:31:26 +00004246 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004247
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004248 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004249 CodeCompletionContext::CCC_PotentiallyQualifiedName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004250 Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004251}
4252
4253void Sema::CodeCompleteUsingDirective(Scope *S) {
4254 if (!CodeCompleter)
4255 return;
4256
Douglas Gregor3545ff42009-09-21 16:56:56 +00004257 // After "using namespace", we expect to see a namespace name or namespace
4258 // alias.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004259 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004260 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004261 CodeCompletionContext::CCC_Namespace,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004262 &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregor64b12b52009-09-22 23:31:26 +00004263 Results.EnterNewScope();
Douglas Gregora6e2edc2010-01-14 03:27:13 +00004264 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00004265 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4266 CodeCompleter->includeGlobals());
Douglas Gregor64b12b52009-09-22 23:31:26 +00004267 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004268 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor39982192010-08-15 06:18:01 +00004269 CodeCompletionContext::CCC_Namespace,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004270 Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004271}
4272
4273void Sema::CodeCompleteNamespaceDecl(Scope *S) {
4274 if (!CodeCompleter)
4275 return;
4276
Ted Kremenekc37877d2013-10-08 17:08:03 +00004277 DeclContext *Ctx = S->getEntity();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004278 if (!S->getParent())
4279 Ctx = Context.getTranslationUnitDecl();
4280
Douglas Gregor0ac41382010-09-23 23:01:17 +00004281 bool SuppressedGlobalResults
4282 = Ctx && !CodeCompleter->includeGlobals() && isa<TranslationUnitDecl>(Ctx);
4283
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004284 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004285 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00004286 SuppressedGlobalResults
4287 ? CodeCompletionContext::CCC_Namespace
4288 : CodeCompletionContext::CCC_Other,
4289 &ResultBuilder::IsNamespace);
4290
4291 if (Ctx && Ctx->isFileContext() && !SuppressedGlobalResults) {
Douglas Gregor3545ff42009-09-21 16:56:56 +00004292 // We only want to see those namespaces that have already been defined
4293 // within this scope, because its likely that the user is creating an
4294 // extended namespace declaration. Keep track of the most recent
4295 // definition of each namespace.
4296 std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest;
4297 for (DeclContext::specific_decl_iterator<NamespaceDecl>
4298 NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end());
4299 NS != NSEnd; ++NS)
David Blaikie40ed2972012-06-06 20:45:41 +00004300 OrigToLatest[NS->getOriginalNamespace()] = *NS;
Douglas Gregor3545ff42009-09-21 16:56:56 +00004301
4302 // Add the most recent definition (or extended definition) of each
4303 // namespace to the list of results.
Douglas Gregor64b12b52009-09-22 23:31:26 +00004304 Results.EnterNewScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004305 for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator
Douglas Gregor78254c82012-03-27 23:34:16 +00004306 NS = OrigToLatest.begin(),
4307 NSEnd = OrigToLatest.end();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004308 NS != NSEnd; ++NS)
Douglas Gregor0a0e2b32013-01-31 04:52:16 +00004309 Results.AddResult(CodeCompletionResult(
Craig Topperc3ec1492014-05-26 06:22:03 +00004310 NS->second, Results.getBasePriority(NS->second),
4311 nullptr),
4312 CurContext, nullptr, false);
Douglas Gregor64b12b52009-09-22 23:31:26 +00004313 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004314 }
4315
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004316 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004317 Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004318 Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004319}
4320
4321void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) {
4322 if (!CodeCompleter)
4323 return;
4324
Douglas Gregor3545ff42009-09-21 16:56:56 +00004325 // After "namespace", we expect to see a namespace or alias.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004326 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004327 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004328 CodeCompletionContext::CCC_Namespace,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004329 &ResultBuilder::IsNamespaceOrAlias);
Douglas Gregora6e2edc2010-01-14 03:27:13 +00004330 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00004331 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4332 CodeCompleter->includeGlobals());
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004333 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004334 Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004335 Results.data(),Results.size());
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004336}
4337
Douglas Gregorc811ede2009-09-18 20:05:18 +00004338void Sema::CodeCompleteOperatorName(Scope *S) {
4339 if (!CodeCompleter)
4340 return;
Douglas Gregor3545ff42009-09-21 16:56:56 +00004341
John McCall276321a2010-08-25 06:19:51 +00004342 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004343 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004344 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004345 CodeCompletionContext::CCC_Type,
Douglas Gregor0ac41382010-09-23 23:01:17 +00004346 &ResultBuilder::IsType);
Douglas Gregor64b12b52009-09-22 23:31:26 +00004347 Results.EnterNewScope();
Douglas Gregorc811ede2009-09-18 20:05:18 +00004348
Douglas Gregor3545ff42009-09-21 16:56:56 +00004349 // Add the names of overloadable operators.
4350#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
4351 if (std::strcmp(Spelling, "?")) \
Douglas Gregor78a21012010-01-14 16:01:26 +00004352 Results.AddResult(Result(Spelling));
Douglas Gregor3545ff42009-09-21 16:56:56 +00004353#include "clang/Basic/OperatorKinds.def"
4354
4355 // Add any type names visible from the current scope
Douglas Gregor6ae4c522010-01-14 03:21:49 +00004356 Results.allowNestedNameSpecifiers();
Douglas Gregora6e2edc2010-01-14 03:27:13 +00004357 CodeCompletionDeclConsumer Consumer(Results, CurContext);
Douglas Gregor39982192010-08-15 06:18:01 +00004358 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
4359 CodeCompleter->includeGlobals());
Douglas Gregor3545ff42009-09-21 16:56:56 +00004360
4361 // Add any type specifiers
David Blaikiebbafb8a2012-03-11 07:00:24 +00004362 AddTypeSpecifierResults(getLangOpts(), Results);
Douglas Gregor64b12b52009-09-22 23:31:26 +00004363 Results.ExitScope();
Douglas Gregor3545ff42009-09-21 16:56:56 +00004364
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004365 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor39982192010-08-15 06:18:01 +00004366 CodeCompletionContext::CCC_Type,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004367 Results.data(),Results.size());
Douglas Gregorc811ede2009-09-18 20:05:18 +00004368}
Douglas Gregor7e90c6d2009-09-18 19:03:04 +00004369
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004370void Sema::CodeCompleteConstructorInitializer(
4371 Decl *ConstructorD,
4372 ArrayRef <CXXCtorInitializer *> Initializers) {
Douglas Gregor75acd922011-09-27 23:30:47 +00004373 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004374 CXXConstructorDecl *Constructor
4375 = static_cast<CXXConstructorDecl *>(ConstructorD);
4376 if (!Constructor)
4377 return;
4378
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004379 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004380 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00004381 CodeCompletionContext::CCC_PotentiallyQualifiedName);
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004382 Results.EnterNewScope();
4383
4384 // Fill in any already-initialized fields or base classes.
4385 llvm::SmallPtrSet<FieldDecl *, 4> InitializedFields;
4386 llvm::SmallPtrSet<CanQualType, 4> InitializedBases;
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004387 for (unsigned I = 0, E = Initializers.size(); I != E; ++I) {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004388 if (Initializers[I]->isBaseInitializer())
4389 InitializedBases.insert(
4390 Context.getCanonicalType(QualType(Initializers[I]->getBaseClass(), 0)));
4391 else
Francois Pichetd583da02010-12-04 09:14:42 +00004392 InitializedFields.insert(cast<FieldDecl>(
4393 Initializers[I]->getAnyMember()));
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004394 }
4395
4396 // Add completions for base classes.
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004397 CodeCompletionBuilder Builder(Results.getAllocator(),
4398 Results.getCodeCompletionTUInfo());
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004399 bool SawLastInitializer = Initializers.empty();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004400 CXXRecordDecl *ClassDecl = Constructor->getParent();
Aaron Ballman574705e2014-03-13 15:41:46 +00004401 for (const auto &Base : ClassDecl->bases()) {
David Blaikie82e95a32014-11-19 07:49:47 +00004402 if (!InitializedBases.insert(Context.getCanonicalType(Base.getType()))
4403 .second) {
Douglas Gregor99129ef2010-08-29 19:27:27 +00004404 SawLastInitializer
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004405 = !Initializers.empty() &&
4406 Initializers.back()->isBaseInitializer() &&
Aaron Ballman574705e2014-03-13 15:41:46 +00004407 Context.hasSameUnqualifiedType(Base.getType(),
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004408 QualType(Initializers.back()->getBaseClass(), 0));
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004409 continue;
Douglas Gregor99129ef2010-08-29 19:27:27 +00004410 }
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004411
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004412 Builder.AddTypedTextChunk(
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00004413 Results.getAllocator().CopyString(
Aaron Ballman574705e2014-03-13 15:41:46 +00004414 Base.getType().getAsString(Policy)));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004415 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4416 Builder.AddPlaceholderChunk("args");
4417 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4418 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor99129ef2010-08-29 19:27:27 +00004419 SawLastInitializer? CCP_NextInitializer
4420 : CCP_MemberDeclaration));
4421 SawLastInitializer = false;
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004422 }
4423
4424 // Add completions for virtual base classes.
Aaron Ballman445a9392014-03-13 16:15:17 +00004425 for (const auto &Base : ClassDecl->vbases()) {
David Blaikie82e95a32014-11-19 07:49:47 +00004426 if (!InitializedBases.insert(Context.getCanonicalType(Base.getType()))
4427 .second) {
Douglas Gregor99129ef2010-08-29 19:27:27 +00004428 SawLastInitializer
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004429 = !Initializers.empty() &&
4430 Initializers.back()->isBaseInitializer() &&
Aaron Ballman445a9392014-03-13 16:15:17 +00004431 Context.hasSameUnqualifiedType(Base.getType(),
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004432 QualType(Initializers.back()->getBaseClass(), 0));
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004433 continue;
Douglas Gregor99129ef2010-08-29 19:27:27 +00004434 }
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004435
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004436 Builder.AddTypedTextChunk(
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00004437 Builder.getAllocator().CopyString(
Aaron Ballman445a9392014-03-13 16:15:17 +00004438 Base.getType().getAsString(Policy)));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004439 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4440 Builder.AddPlaceholderChunk("args");
4441 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4442 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor99129ef2010-08-29 19:27:27 +00004443 SawLastInitializer? CCP_NextInitializer
4444 : CCP_MemberDeclaration));
4445 SawLastInitializer = false;
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004446 }
4447
4448 // Add completions for members.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00004449 for (auto *Field : ClassDecl->fields()) {
David Blaikie82e95a32014-11-19 07:49:47 +00004450 if (!InitializedFields.insert(cast<FieldDecl>(Field->getCanonicalDecl()))
4451 .second) {
Douglas Gregor99129ef2010-08-29 19:27:27 +00004452 SawLastInitializer
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00004453 = !Initializers.empty() &&
4454 Initializers.back()->isAnyMemberInitializer() &&
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00004455 Initializers.back()->getAnyMember() == Field;
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004456 continue;
Douglas Gregor99129ef2010-08-29 19:27:27 +00004457 }
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004458
4459 if (!Field->getDeclName())
4460 continue;
4461
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00004462 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004463 Field->getIdentifier()->getName()));
4464 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4465 Builder.AddPlaceholderChunk("args");
4466 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4467 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
Douglas Gregor99129ef2010-08-29 19:27:27 +00004468 SawLastInitializer? CCP_NextInitializer
Douglas Gregorf3af3112010-09-09 21:42:20 +00004469 : CCP_MemberDeclaration,
Douglas Gregor78254c82012-03-27 23:34:16 +00004470 CXCursor_MemberRef,
4471 CXAvailability_Available,
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00004472 Field));
Douglas Gregor99129ef2010-08-29 19:27:27 +00004473 SawLastInitializer = false;
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004474 }
4475 Results.ExitScope();
4476
Douglas Gregor0ac41382010-09-23 23:01:17 +00004477 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregoreaeeca92010-08-28 00:00:50 +00004478 Results.data(), Results.size());
4479}
4480
Douglas Gregord8c61782012-02-15 15:34:24 +00004481/// \brief Determine whether this scope denotes a namespace.
4482static bool isNamespaceScope(Scope *S) {
Ted Kremenekc37877d2013-10-08 17:08:03 +00004483 DeclContext *DC = S->getEntity();
Douglas Gregord8c61782012-02-15 15:34:24 +00004484 if (!DC)
4485 return false;
4486
4487 return DC->isFileContext();
4488}
4489
4490void Sema::CodeCompleteLambdaIntroducer(Scope *S, LambdaIntroducer &Intro,
4491 bool AfterAmpersand) {
4492 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004493 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregord8c61782012-02-15 15:34:24 +00004494 CodeCompletionContext::CCC_Other);
4495 Results.EnterNewScope();
4496
4497 // Note what has already been captured.
4498 llvm::SmallPtrSet<IdentifierInfo *, 4> Known;
4499 bool IncludedThis = false;
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00004500 for (const auto &C : Intro.Captures) {
4501 if (C.Kind == LCK_This) {
Douglas Gregord8c61782012-02-15 15:34:24 +00004502 IncludedThis = true;
4503 continue;
4504 }
4505
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00004506 Known.insert(C.Id);
Douglas Gregord8c61782012-02-15 15:34:24 +00004507 }
4508
4509 // Look for other capturable variables.
4510 for (; S && !isNamespaceScope(S); S = S->getParent()) {
Aaron Ballman35c54952014-03-17 16:55:25 +00004511 for (const auto *D : S->decls()) {
4512 const auto *Var = dyn_cast<VarDecl>(D);
Douglas Gregord8c61782012-02-15 15:34:24 +00004513 if (!Var ||
4514 !Var->hasLocalStorage() ||
4515 Var->hasAttr<BlocksAttr>())
4516 continue;
4517
David Blaikie82e95a32014-11-19 07:49:47 +00004518 if (Known.insert(Var->getIdentifier()).second)
Douglas Gregor0a0e2b32013-01-31 04:52:16 +00004519 Results.AddResult(CodeCompletionResult(Var, CCP_LocalDeclaration),
Craig Topperc3ec1492014-05-26 06:22:03 +00004520 CurContext, nullptr, false);
Douglas Gregord8c61782012-02-15 15:34:24 +00004521 }
4522 }
4523
4524 // Add 'this', if it would be valid.
4525 if (!IncludedThis && !AfterAmpersand && Intro.Default != LCD_ByCopy)
4526 addThisCompletion(*this, Results);
4527
4528 Results.ExitScope();
4529
4530 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
4531 Results.data(), Results.size());
4532}
4533
James Dennett596e4752012-06-14 03:11:41 +00004534/// Macro that optionally prepends an "@" to the string literal passed in via
4535/// Keyword, depending on whether NeedAt is true or false.
4536#define OBJC_AT_KEYWORD_NAME(NeedAt,Keyword) ((NeedAt)? "@" Keyword : Keyword)
4537
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004538static void AddObjCImplementationResults(const LangOptions &LangOpts,
Douglas Gregorf1934162010-01-13 21:24:21 +00004539 ResultBuilder &Results,
4540 bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004541 typedef CodeCompletionResult Result;
Douglas Gregorf1934162010-01-13 21:24:21 +00004542 // Since we have an implementation, we can end it.
James Dennett596e4752012-06-14 03:11:41 +00004543 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
Douglas Gregorf1934162010-01-13 21:24:21 +00004544
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004545 CodeCompletionBuilder Builder(Results.getAllocator(),
4546 Results.getCodeCompletionTUInfo());
Douglas Gregorf1934162010-01-13 21:24:21 +00004547 if (LangOpts.ObjC2) {
4548 // @dynamic
James Dennett596e4752012-06-14 03:11:41 +00004549 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"dynamic"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004550 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4551 Builder.AddPlaceholderChunk("property");
4552 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf1934162010-01-13 21:24:21 +00004553
4554 // @synthesize
James Dennett596e4752012-06-14 03:11:41 +00004555 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synthesize"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004556 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4557 Builder.AddPlaceholderChunk("property");
4558 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf1934162010-01-13 21:24:21 +00004559 }
4560}
4561
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004562static void AddObjCInterfaceResults(const LangOptions &LangOpts,
Douglas Gregorf1934162010-01-13 21:24:21 +00004563 ResultBuilder &Results,
4564 bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004565 typedef CodeCompletionResult Result;
Douglas Gregorf1934162010-01-13 21:24:21 +00004566
4567 // Since we have an interface or protocol, we can end it.
James Dennett596e4752012-06-14 03:11:41 +00004568 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"end")));
Douglas Gregorf1934162010-01-13 21:24:21 +00004569
4570 if (LangOpts.ObjC2) {
4571 // @property
James Dennett596e4752012-06-14 03:11:41 +00004572 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"property")));
Douglas Gregorf1934162010-01-13 21:24:21 +00004573
4574 // @required
James Dennett596e4752012-06-14 03:11:41 +00004575 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"required")));
Douglas Gregorf1934162010-01-13 21:24:21 +00004576
4577 // @optional
James Dennett596e4752012-06-14 03:11:41 +00004578 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"optional")));
Douglas Gregorf1934162010-01-13 21:24:21 +00004579 }
4580}
4581
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004582static void AddObjCTopLevelResults(ResultBuilder &Results, bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004583 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004584 CodeCompletionBuilder Builder(Results.getAllocator(),
4585 Results.getCodeCompletionTUInfo());
Douglas Gregorf1934162010-01-13 21:24:21 +00004586
4587 // @class name ;
James Dennett596e4752012-06-14 03:11:41 +00004588 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"class"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004589 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4590 Builder.AddPlaceholderChunk("name");
4591 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf1934162010-01-13 21:24:21 +00004592
Douglas Gregorf4c33342010-05-28 00:22:41 +00004593 if (Results.includeCodePatterns()) {
4594 // @interface name
4595 // FIXME: Could introduce the whole pattern, including superclasses and
4596 // such.
James Dennett596e4752012-06-14 03:11:41 +00004597 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"interface"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004598 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4599 Builder.AddPlaceholderChunk("class");
4600 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf1934162010-01-13 21:24:21 +00004601
Douglas Gregorf4c33342010-05-28 00:22:41 +00004602 // @protocol name
James Dennett596e4752012-06-14 03:11:41 +00004603 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004604 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4605 Builder.AddPlaceholderChunk("protocol");
4606 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00004607
4608 // @implementation name
James Dennett596e4752012-06-14 03:11:41 +00004609 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"implementation"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004610 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4611 Builder.AddPlaceholderChunk("class");
4612 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00004613 }
Douglas Gregorf1934162010-01-13 21:24:21 +00004614
4615 // @compatibility_alias name
James Dennett596e4752012-06-14 03:11:41 +00004616 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"compatibility_alias"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004617 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4618 Builder.AddPlaceholderChunk("alias");
4619 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4620 Builder.AddPlaceholderChunk("class");
4621 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregor61e36812013-03-07 23:26:24 +00004622
4623 if (Results.getSema().getLangOpts().Modules) {
4624 // @import name
4625 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "import"));
4626 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4627 Builder.AddPlaceholderChunk("module");
4628 Results.AddResult(Result(Builder.TakeString()));
4629 }
Douglas Gregorf1934162010-01-13 21:24:21 +00004630}
4631
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00004632void Sema::CodeCompleteObjCAtDirective(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004633 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004634 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004635 CodeCompletionContext::CCC_Other);
Douglas Gregorf48706c2009-12-07 09:27:33 +00004636 Results.EnterNewScope();
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00004637 if (isa<ObjCImplDecl>(CurContext))
David Blaikiebbafb8a2012-03-11 07:00:24 +00004638 AddObjCImplementationResults(getLangOpts(), Results, false);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00004639 else if (CurContext->isObjCContainer())
David Blaikiebbafb8a2012-03-11 07:00:24 +00004640 AddObjCInterfaceResults(getLangOpts(), Results, false);
Douglas Gregorf1934162010-01-13 21:24:21 +00004641 else
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004642 AddObjCTopLevelResults(Results, false);
Douglas Gregorf48706c2009-12-07 09:27:33 +00004643 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004644 HandleCodeCompleteResults(this, CodeCompleter,
4645 CodeCompletionContext::CCC_Other,
4646 Results.data(),Results.size());
Douglas Gregorf48706c2009-12-07 09:27:33 +00004647}
4648
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004649static void AddObjCExpressionResults(ResultBuilder &Results, bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004650 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004651 CodeCompletionBuilder Builder(Results.getAllocator(),
4652 Results.getCodeCompletionTUInfo());
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004653
4654 // @encode ( type-name )
Douglas Gregore5c79d52011-10-18 21:20:17 +00004655 const char *EncodeType = "char[]";
David Blaikiebbafb8a2012-03-11 07:00:24 +00004656 if (Results.getSema().getLangOpts().CPlusPlus ||
4657 Results.getSema().getLangOpts().ConstStrings)
Jordan Rose9da05852012-06-15 18:19:56 +00004658 EncodeType = "const char[]";
Douglas Gregore5c79d52011-10-18 21:20:17 +00004659 Builder.AddResultTypeChunk(EncodeType);
James Dennett596e4752012-06-14 03:11:41 +00004660 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"encode"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004661 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4662 Builder.AddPlaceholderChunk("type-name");
4663 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4664 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004665
4666 // @protocol ( protocol-name )
Douglas Gregore5c79d52011-10-18 21:20:17 +00004667 Builder.AddResultTypeChunk("Protocol *");
James Dennett596e4752012-06-14 03:11:41 +00004668 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"protocol"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004669 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4670 Builder.AddPlaceholderChunk("protocol-name");
4671 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4672 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004673
4674 // @selector ( selector )
Douglas Gregore5c79d52011-10-18 21:20:17 +00004675 Builder.AddResultTypeChunk("SEL");
James Dennett596e4752012-06-14 03:11:41 +00004676 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"selector"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004677 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4678 Builder.AddPlaceholderChunk("selector");
4679 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4680 Results.AddResult(Result(Builder.TakeString()));
Jordan Rose9da05852012-06-15 18:19:56 +00004681
4682 // @"string"
4683 Builder.AddResultTypeChunk("NSString *");
4684 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"\""));
4685 Builder.AddPlaceholderChunk("string");
4686 Builder.AddTextChunk("\"");
4687 Results.AddResult(Result(Builder.TakeString()));
4688
Douglas Gregor951de302012-07-17 23:24:47 +00004689 // @[objects, ...]
Jordan Rose9da05852012-06-15 18:19:56 +00004690 Builder.AddResultTypeChunk("NSArray *");
James Dennett596e4752012-06-14 03:11:41 +00004691 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"["));
Ted Kremeneke65b0862012-03-06 20:05:56 +00004692 Builder.AddPlaceholderChunk("objects, ...");
Ted Kremeneke65b0862012-03-06 20:05:56 +00004693 Builder.AddChunk(CodeCompletionString::CK_RightBracket);
4694 Results.AddResult(Result(Builder.TakeString()));
4695
Douglas Gregor951de302012-07-17 23:24:47 +00004696 // @{key : object, ...}
Jordan Rose9da05852012-06-15 18:19:56 +00004697 Builder.AddResultTypeChunk("NSDictionary *");
James Dennett596e4752012-06-14 03:11:41 +00004698 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"{"));
Ted Kremeneke65b0862012-03-06 20:05:56 +00004699 Builder.AddPlaceholderChunk("key");
Ted Kremeneke65b0862012-03-06 20:05:56 +00004700 Builder.AddChunk(CodeCompletionString::CK_Colon);
4701 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4702 Builder.AddPlaceholderChunk("object, ...");
Ted Kremeneke65b0862012-03-06 20:05:56 +00004703 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4704 Results.AddResult(Result(Builder.TakeString()));
Jordan Rose9da05852012-06-15 18:19:56 +00004705
Douglas Gregor951de302012-07-17 23:24:47 +00004706 // @(expression)
Jordan Rose9da05852012-06-15 18:19:56 +00004707 Builder.AddResultTypeChunk("id");
4708 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt, "("));
Jordan Rose9da05852012-06-15 18:19:56 +00004709 Builder.AddPlaceholderChunk("expression");
Jordan Rose9da05852012-06-15 18:19:56 +00004710 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4711 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004712}
4713
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004714static void AddObjCStatementResults(ResultBuilder &Results, bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004715 typedef CodeCompletionResult Result;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004716 CodeCompletionBuilder Builder(Results.getAllocator(),
4717 Results.getCodeCompletionTUInfo());
Douglas Gregorf1934162010-01-13 21:24:21 +00004718
Douglas Gregorf4c33342010-05-28 00:22:41 +00004719 if (Results.includeCodePatterns()) {
4720 // @try { statements } @catch ( declaration ) { statements } @finally
4721 // { statements }
James Dennett596e4752012-06-14 03:11:41 +00004722 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"try"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004723 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4724 Builder.AddPlaceholderChunk("statements");
4725 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4726 Builder.AddTextChunk("@catch");
4727 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4728 Builder.AddPlaceholderChunk("parameter");
4729 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4730 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4731 Builder.AddPlaceholderChunk("statements");
4732 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4733 Builder.AddTextChunk("@finally");
4734 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4735 Builder.AddPlaceholderChunk("statements");
4736 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4737 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00004738 }
Douglas Gregorf1934162010-01-13 21:24:21 +00004739
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004740 // @throw
James Dennett596e4752012-06-14 03:11:41 +00004741 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"throw"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004742 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4743 Builder.AddPlaceholderChunk("expression");
4744 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf1934162010-01-13 21:24:21 +00004745
Douglas Gregorf4c33342010-05-28 00:22:41 +00004746 if (Results.includeCodePatterns()) {
4747 // @synchronized ( expression ) { statements }
James Dennett596e4752012-06-14 03:11:41 +00004748 Builder.AddTypedTextChunk(OBJC_AT_KEYWORD_NAME(NeedAt,"synchronized"));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004749 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4750 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
4751 Builder.AddPlaceholderChunk("expression");
4752 Builder.AddChunk(CodeCompletionString::CK_RightParen);
4753 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
4754 Builder.AddPlaceholderChunk("statements");
4755 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
4756 Results.AddResult(Result(Builder.TakeString()));
Douglas Gregorf4c33342010-05-28 00:22:41 +00004757 }
Douglas Gregorf1934162010-01-13 21:24:21 +00004758}
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004759
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004760static void AddObjCVisibilityResults(const LangOptions &LangOpts,
Douglas Gregor48d46252010-01-13 21:54:15 +00004761 ResultBuilder &Results,
4762 bool NeedAt) {
John McCall276321a2010-08-25 06:19:51 +00004763 typedef CodeCompletionResult Result;
James Dennett596e4752012-06-14 03:11:41 +00004764 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"private")));
4765 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"protected")));
4766 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"public")));
Douglas Gregor48d46252010-01-13 21:54:15 +00004767 if (LangOpts.ObjC2)
James Dennett596e4752012-06-14 03:11:41 +00004768 Results.AddResult(Result(OBJC_AT_KEYWORD_NAME(NeedAt,"package")));
Douglas Gregor48d46252010-01-13 21:54:15 +00004769}
4770
4771void Sema::CodeCompleteObjCAtVisibility(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004772 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004773 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004774 CodeCompletionContext::CCC_Other);
Douglas Gregor48d46252010-01-13 21:54:15 +00004775 Results.EnterNewScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004776 AddObjCVisibilityResults(getLangOpts(), Results, false);
Douglas Gregor48d46252010-01-13 21:54:15 +00004777 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004778 HandleCodeCompleteResults(this, CodeCompleter,
4779 CodeCompletionContext::CCC_Other,
4780 Results.data(),Results.size());
Douglas Gregor48d46252010-01-13 21:54:15 +00004781}
4782
4783void Sema::CodeCompleteObjCAtStatement(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004784 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004785 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004786 CodeCompletionContext::CCC_Other);
Douglas Gregorf1934162010-01-13 21:24:21 +00004787 Results.EnterNewScope();
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004788 AddObjCStatementResults(Results, false);
4789 AddObjCExpressionResults(Results, false);
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004790 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004791 HandleCodeCompleteResults(this, CodeCompleter,
4792 CodeCompletionContext::CCC_Other,
4793 Results.data(),Results.size());
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004794}
4795
4796void Sema::CodeCompleteObjCAtExpression(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004797 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004798 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004799 CodeCompletionContext::CCC_Other);
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004800 Results.EnterNewScope();
Douglas Gregorf98e6a22010-01-13 23:51:12 +00004801 AddObjCExpressionResults(Results, false);
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004802 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004803 HandleCodeCompleteResults(this, CodeCompleter,
4804 CodeCompletionContext::CCC_Other,
4805 Results.data(),Results.size());
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00004806}
4807
Douglas Gregore6078da2009-11-19 00:14:45 +00004808/// \brief Determine whether the addition of the given flag to an Objective-C
4809/// property's attributes will cause a conflict.
Bill Wendling44426052012-12-20 19:22:21 +00004810static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
Douglas Gregore6078da2009-11-19 00:14:45 +00004811 // Check if we've already added this flag.
Bill Wendling44426052012-12-20 19:22:21 +00004812 if (Attributes & NewFlag)
Douglas Gregore6078da2009-11-19 00:14:45 +00004813 return true;
4814
Bill Wendling44426052012-12-20 19:22:21 +00004815 Attributes |= NewFlag;
Douglas Gregore6078da2009-11-19 00:14:45 +00004816
4817 // Check for collisions with "readonly".
Bill Wendling44426052012-12-20 19:22:21 +00004818 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
4819 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Douglas Gregore6078da2009-11-19 00:14:45 +00004820 return true;
4821
Jordan Rose53cb2f32012-08-20 20:01:13 +00004822 // Check for more than one of { assign, copy, retain, strong, weak }.
Bill Wendling44426052012-12-20 19:22:21 +00004823 unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign |
John McCall31168b02011-06-15 23:02:42 +00004824 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Douglas Gregore6078da2009-11-19 00:14:45 +00004825 ObjCDeclSpec::DQ_PR_copy |
Jordan Rose53cb2f32012-08-20 20:01:13 +00004826 ObjCDeclSpec::DQ_PR_retain |
4827 ObjCDeclSpec::DQ_PR_strong |
4828 ObjCDeclSpec::DQ_PR_weak);
Douglas Gregore6078da2009-11-19 00:14:45 +00004829 if (AssignCopyRetMask &&
4830 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign &&
John McCall31168b02011-06-15 23:02:42 +00004831 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_unsafe_unretained &&
Douglas Gregore6078da2009-11-19 00:14:45 +00004832 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy &&
John McCall31168b02011-06-15 23:02:42 +00004833 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain &&
Jordan Rose53cb2f32012-08-20 20:01:13 +00004834 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_strong &&
4835 AssignCopyRetMask != ObjCDeclSpec::DQ_PR_weak)
Douglas Gregore6078da2009-11-19 00:14:45 +00004836 return true;
4837
4838 return false;
4839}
4840
Douglas Gregor36029f42009-11-18 23:08:07 +00004841void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
Steve Naroff936354c2009-10-08 21:55:05 +00004842 if (!CodeCompleter)
4843 return;
Douglas Gregor1b605f72009-11-19 01:08:35 +00004844
Bill Wendling44426052012-12-20 19:22:21 +00004845 unsigned Attributes = ODS.getPropertyAttributes();
Steve Naroff936354c2009-10-08 21:55:05 +00004846
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004847 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004848 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004849 CodeCompletionContext::CCC_Other);
Steve Naroff936354c2009-10-08 21:55:05 +00004850 Results.EnterNewScope();
Bill Wendling44426052012-12-20 19:22:21 +00004851 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly))
John McCall276321a2010-08-25 06:19:51 +00004852 Results.AddResult(CodeCompletionResult("readonly"));
Bill Wendling44426052012-12-20 19:22:21 +00004853 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign))
John McCall276321a2010-08-25 06:19:51 +00004854 Results.AddResult(CodeCompletionResult("assign"));
Bill Wendling44426052012-12-20 19:22:21 +00004855 if (!ObjCPropertyFlagConflicts(Attributes,
John McCall31168b02011-06-15 23:02:42 +00004856 ObjCDeclSpec::DQ_PR_unsafe_unretained))
4857 Results.AddResult(CodeCompletionResult("unsafe_unretained"));
Bill Wendling44426052012-12-20 19:22:21 +00004858 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite))
John McCall276321a2010-08-25 06:19:51 +00004859 Results.AddResult(CodeCompletionResult("readwrite"));
Bill Wendling44426052012-12-20 19:22:21 +00004860 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain))
John McCall276321a2010-08-25 06:19:51 +00004861 Results.AddResult(CodeCompletionResult("retain"));
Bill Wendling44426052012-12-20 19:22:21 +00004862 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_strong))
John McCall31168b02011-06-15 23:02:42 +00004863 Results.AddResult(CodeCompletionResult("strong"));
Bill Wendling44426052012-12-20 19:22:21 +00004864 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy))
John McCall276321a2010-08-25 06:19:51 +00004865 Results.AddResult(CodeCompletionResult("copy"));
Bill Wendling44426052012-12-20 19:22:21 +00004866 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic))
John McCall276321a2010-08-25 06:19:51 +00004867 Results.AddResult(CodeCompletionResult("nonatomic"));
Bill Wendling44426052012-12-20 19:22:21 +00004868 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_atomic))
Fariborz Jahanian1c2d29e2011-06-11 17:14:27 +00004869 Results.AddResult(CodeCompletionResult("atomic"));
Jordan Rose53cb2f32012-08-20 20:01:13 +00004870
4871 // Only suggest "weak" if we're compiling for ARC-with-weak-references or GC.
John McCall3deb1ad2012-08-21 02:47:43 +00004872 if (getLangOpts().ObjCARCWeak || getLangOpts().getGC() != LangOptions::NonGC)
Bill Wendling44426052012-12-20 19:22:21 +00004873 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_weak))
Jordan Rose53cb2f32012-08-20 20:01:13 +00004874 Results.AddResult(CodeCompletionResult("weak"));
4875
Bill Wendling44426052012-12-20 19:22:21 +00004876 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004877 CodeCompletionBuilder Setter(Results.getAllocator(),
4878 Results.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004879 Setter.AddTypedTextChunk("setter");
Argyrios Kyrtzidis7bbb8812014-02-20 07:55:15 +00004880 Setter.AddTextChunk("=");
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004881 Setter.AddPlaceholderChunk("method");
4882 Results.AddResult(CodeCompletionResult(Setter.TakeString()));
Douglas Gregor45f83ee2009-11-19 00:01:57 +00004883 }
Bill Wendling44426052012-12-20 19:22:21 +00004884 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00004885 CodeCompletionBuilder Getter(Results.getAllocator(),
4886 Results.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004887 Getter.AddTypedTextChunk("getter");
Argyrios Kyrtzidis7bbb8812014-02-20 07:55:15 +00004888 Getter.AddTextChunk("=");
Douglas Gregorb278aaf2011-02-01 19:23:04 +00004889 Getter.AddPlaceholderChunk("method");
4890 Results.AddResult(CodeCompletionResult(Getter.TakeString()));
Douglas Gregor45f83ee2009-11-19 00:01:57 +00004891 }
Douglas Gregor86b42682015-06-19 18:27:52 +00004892 if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nullability)) {
4893 Results.AddResult(CodeCompletionResult("nonnull"));
4894 Results.AddResult(CodeCompletionResult("nullable"));
4895 Results.AddResult(CodeCompletionResult("null_unspecified"));
4896 Results.AddResult(CodeCompletionResult("null_resettable"));
4897 }
Steve Naroff936354c2009-10-08 21:55:05 +00004898 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00004899 HandleCodeCompleteResults(this, CodeCompleter,
4900 CodeCompletionContext::CCC_Other,
4901 Results.data(),Results.size());
Steve Naroff936354c2009-10-08 21:55:05 +00004902}
Steve Naroffeae65032009-11-07 02:08:14 +00004903
James Dennettf1243872012-06-17 05:33:25 +00004904/// \brief Describes the kind of Objective-C method that we want to find
Douglas Gregorc8537c52009-11-19 07:41:15 +00004905/// via code completion.
4906enum ObjCMethodKind {
Dmitri Gribenko4280e5c2012-06-08 23:13:42 +00004907 MK_Any, ///< Any kind of method, provided it means other specified criteria.
4908 MK_ZeroArgSelector, ///< Zero-argument (unary) selector.
4909 MK_OneArgSelector ///< One-argument selector.
Douglas Gregorc8537c52009-11-19 07:41:15 +00004910};
4911
Douglas Gregor67c692c2010-08-26 15:07:07 +00004912static bool isAcceptableObjCSelector(Selector Sel,
4913 ObjCMethodKind WantKind,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004914 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00004915 bool AllowSameLength = true) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004916 unsigned NumSelIdents = SelIdents.size();
Douglas Gregor67c692c2010-08-26 15:07:07 +00004917 if (NumSelIdents > Sel.getNumArgs())
4918 return false;
4919
4920 switch (WantKind) {
4921 case MK_Any: break;
4922 case MK_ZeroArgSelector: return Sel.isUnarySelector();
4923 case MK_OneArgSelector: return Sel.getNumArgs() == 1;
4924 }
4925
Douglas Gregorb4a7c032010-11-17 21:36:08 +00004926 if (!AllowSameLength && NumSelIdents && NumSelIdents == Sel.getNumArgs())
4927 return false;
4928
Douglas Gregor67c692c2010-08-26 15:07:07 +00004929 for (unsigned I = 0; I != NumSelIdents; ++I)
4930 if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
4931 return false;
4932
4933 return true;
4934}
4935
Douglas Gregorc8537c52009-11-19 07:41:15 +00004936static bool isAcceptableObjCMethod(ObjCMethodDecl *Method,
4937 ObjCMethodKind WantKind,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004938 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00004939 bool AllowSameLength = true) {
Douglas Gregor67c692c2010-08-26 15:07:07 +00004940 return isAcceptableObjCSelector(Method->getSelector(), WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004941 AllowSameLength);
Douglas Gregorc8537c52009-11-19 07:41:15 +00004942}
Douglas Gregor1154e272010-09-16 16:06:31 +00004943
4944namespace {
4945 /// \brief A set of selectors, which is used to avoid introducing multiple
4946 /// completions with the same selector into the result set.
4947 typedef llvm::SmallPtrSet<Selector, 16> VisitedSelectorSet;
4948}
4949
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00004950/// \brief Add all of the Objective-C methods in the given Objective-C
4951/// container to the set of results.
4952///
4953/// The container will be a class, protocol, category, or implementation of
4954/// any of the above. This mether will recurse to include methods from
4955/// the superclasses of classes along with their categories, protocols, and
4956/// implementations.
4957///
4958/// \param Container the container in which we'll look to find methods.
4959///
James Dennett596e4752012-06-14 03:11:41 +00004960/// \param WantInstanceMethods Whether to add instance methods (only); if
4961/// false, this routine will add factory methods (only).
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00004962///
4963/// \param CurContext the context in which we're performing the lookup that
4964/// finds methods.
4965///
Douglas Gregorb4a7c032010-11-17 21:36:08 +00004966/// \param AllowSameLength Whether we allow a method to be added to the list
4967/// when it has the same number of parameters as we have selector identifiers.
4968///
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00004969/// \param Results the structure into which we'll add results.
4970static void AddObjCMethods(ObjCContainerDecl *Container,
4971 bool WantInstanceMethods,
Douglas Gregorc8537c52009-11-19 07:41:15 +00004972 ObjCMethodKind WantKind,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004973 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00004974 DeclContext *CurContext,
Douglas Gregor1154e272010-09-16 16:06:31 +00004975 VisitedSelectorSet &Selectors,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00004976 bool AllowSameLength,
Douglas Gregor416b5752010-08-25 01:08:01 +00004977 ResultBuilder &Results,
4978 bool InOriginalClass = true) {
John McCall276321a2010-08-25 06:19:51 +00004979 typedef CodeCompletionResult Result;
Douglas Gregor9b4f3702012-06-12 13:44:08 +00004980 Container = getContainerDef(Container);
Douglas Gregor41778c32013-01-30 06:58:39 +00004981 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
4982 bool isRootClass = IFace && !IFace->getSuperClass();
Aaron Ballmanaff18c02014-03-13 19:03:34 +00004983 for (auto *M : Container->methods()) {
Douglas Gregor41778c32013-01-30 06:58:39 +00004984 // The instance methods on the root class can be messaged via the
4985 // metaclass.
4986 if (M->isInstanceMethod() == WantInstanceMethods ||
4987 (isRootClass && !WantInstanceMethods)) {
Douglas Gregor1b605f72009-11-19 01:08:35 +00004988 // Check whether the selector identifiers we've been given are a
4989 // subset of the identifiers for this particular method.
Aaron Ballmanaff18c02014-03-13 19:03:34 +00004990 if (!isAcceptableObjCMethod(M, WantKind, SelIdents, AllowSameLength))
Douglas Gregor1b605f72009-11-19 01:08:35 +00004991 continue;
Douglas Gregorc8537c52009-11-19 07:41:15 +00004992
David Blaikie82e95a32014-11-19 07:49:47 +00004993 if (!Selectors.insert(M->getSelector()).second)
Douglas Gregor1154e272010-09-16 16:06:31 +00004994 continue;
Craig Topperc3ec1492014-05-26 06:22:03 +00004995
4996 Result R = Result(M, Results.getBasePriority(M), nullptr);
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00004997 R.StartParameter = SelIdents.size();
Douglas Gregorc8537c52009-11-19 07:41:15 +00004998 R.AllParametersAreInformative = (WantKind != MK_Any);
Douglas Gregor416b5752010-08-25 01:08:01 +00004999 if (!InOriginalClass)
5000 R.Priority += CCD_InBaseClass;
Douglas Gregor1b605f72009-11-19 01:08:35 +00005001 Results.MaybeAddResult(R, CurContext);
5002 }
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005003 }
5004
Douglas Gregorf37c9492010-09-16 15:34:59 +00005005 // Visit the protocols of protocols.
5006 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00005007 if (Protocol->hasDefinition()) {
5008 const ObjCList<ObjCProtocolDecl> &Protocols
5009 = Protocol->getReferencedProtocols();
5010 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
5011 E = Protocols.end();
5012 I != E; ++I)
5013 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005014 CurContext, Selectors, AllowSameLength, Results, false);
Douglas Gregore6e48b12012-01-01 19:29:29 +00005015 }
Douglas Gregorf37c9492010-09-16 15:34:59 +00005016 }
5017
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00005018 if (!IFace || !IFace->hasDefinition())
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005019 return;
5020
5021 // Add methods in protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +00005022 for (auto *I : IFace->protocols())
5023 AddObjCMethods(I, WantInstanceMethods, WantKind, SelIdents,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005024 CurContext, Selectors, AllowSameLength, Results, false);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005025
5026 // Add methods in categories.
Aaron Ballman15063e12014-03-13 21:35:02 +00005027 for (auto *CatDecl : IFace->known_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00005028 AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005029 CurContext, Selectors, AllowSameLength,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005030 Results, InOriginalClass);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005031
5032 // Add a categories protocol methods.
5033 const ObjCList<ObjCProtocolDecl> &Protocols
5034 = CatDecl->getReferencedProtocols();
5035 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
5036 E = Protocols.end();
5037 I != E; ++I)
Douglas Gregorc8537c52009-11-19 07:41:15 +00005038 AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005039 CurContext, Selectors, AllowSameLength,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005040 Results, false);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005041
5042 // Add methods in category implementations.
5043 if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
Douglas Gregorc8537c52009-11-19 07:41:15 +00005044 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005045 CurContext, Selectors, AllowSameLength,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005046 Results, InOriginalClass);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005047 }
5048
5049 // Add methods in superclass.
5050 if (IFace->getSuperClass())
Douglas Gregorc8537c52009-11-19 07:41:15 +00005051 AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005052 SelIdents, CurContext, Selectors,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005053 AllowSameLength, Results, false);
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005054
5055 // Add methods in our implementation, if any.
5056 if (ObjCImplementationDecl *Impl = IFace->getImplementation())
Douglas Gregorc8537c52009-11-19 07:41:15 +00005057 AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005058 CurContext, Selectors, AllowSameLength,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005059 Results, InOriginalClass);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005060}
5061
5062
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005063void Sema::CodeCompleteObjCPropertyGetter(Scope *S) {
Douglas Gregorc8537c52009-11-19 07:41:15 +00005064 // Try to find the interface where getters might live.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005065 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005066 if (!Class) {
5067 if (ObjCCategoryDecl *Category
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005068 = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
Douglas Gregorc8537c52009-11-19 07:41:15 +00005069 Class = Category->getClassInterface();
5070
5071 if (!Class)
5072 return;
5073 }
5074
5075 // Find all of the potential getters.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005076 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005077 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005078 CodeCompletionContext::CCC_Other);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005079 Results.EnterNewScope();
5080
Douglas Gregor1154e272010-09-16 16:06:31 +00005081 VisitedSelectorSet Selectors;
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005082 AddObjCMethods(Class, true, MK_ZeroArgSelector, None, CurContext, Selectors,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005083 /*AllowSameLength=*/true, Results);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005084 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005085 HandleCodeCompleteResults(this, CodeCompleter,
5086 CodeCompletionContext::CCC_Other,
5087 Results.data(),Results.size());
Douglas Gregorc8537c52009-11-19 07:41:15 +00005088}
5089
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005090void Sema::CodeCompleteObjCPropertySetter(Scope *S) {
Douglas Gregorc8537c52009-11-19 07:41:15 +00005091 // Try to find the interface where setters might live.
5092 ObjCInterfaceDecl *Class
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005093 = dyn_cast_or_null<ObjCInterfaceDecl>(CurContext);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005094 if (!Class) {
5095 if (ObjCCategoryDecl *Category
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00005096 = dyn_cast_or_null<ObjCCategoryDecl>(CurContext))
Douglas Gregorc8537c52009-11-19 07:41:15 +00005097 Class = Category->getClassInterface();
5098
5099 if (!Class)
5100 return;
5101 }
5102
5103 // Find all of the potential getters.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005104 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005105 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005106 CodeCompletionContext::CCC_Other);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005107 Results.EnterNewScope();
5108
Douglas Gregor1154e272010-09-16 16:06:31 +00005109 VisitedSelectorSet Selectors;
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005110 AddObjCMethods(Class, true, MK_OneArgSelector, None, CurContext,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005111 Selectors, /*AllowSameLength=*/true, Results);
Douglas Gregorc8537c52009-11-19 07:41:15 +00005112
5113 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005114 HandleCodeCompleteResults(this, CodeCompleter,
5115 CodeCompletionContext::CCC_Other,
5116 Results.data(),Results.size());
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005117}
5118
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005119void Sema::CodeCompleteObjCPassingType(Scope *S, ObjCDeclSpec &DS,
5120 bool IsParameter) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005121 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005122 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005123 CodeCompletionContext::CCC_Type);
Douglas Gregor99fa2642010-08-24 01:06:58 +00005124 Results.EnterNewScope();
5125
5126 // Add context-sensitive, Objective-C parameter-passing keywords.
5127 bool AddedInOut = false;
5128 if ((DS.getObjCDeclQualifier() &
5129 (ObjCDeclSpec::DQ_In | ObjCDeclSpec::DQ_Inout)) == 0) {
5130 Results.AddResult("in");
5131 Results.AddResult("inout");
5132 AddedInOut = true;
5133 }
5134 if ((DS.getObjCDeclQualifier() &
5135 (ObjCDeclSpec::DQ_Out | ObjCDeclSpec::DQ_Inout)) == 0) {
5136 Results.AddResult("out");
5137 if (!AddedInOut)
5138 Results.AddResult("inout");
5139 }
5140 if ((DS.getObjCDeclQualifier() &
5141 (ObjCDeclSpec::DQ_Bycopy | ObjCDeclSpec::DQ_Byref |
5142 ObjCDeclSpec::DQ_Oneway)) == 0) {
5143 Results.AddResult("bycopy");
5144 Results.AddResult("byref");
5145 Results.AddResult("oneway");
5146 }
Douglas Gregor86b42682015-06-19 18:27:52 +00005147 if ((DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability) == 0) {
5148 Results.AddResult("nonnull");
5149 Results.AddResult("nullable");
5150 Results.AddResult("null_unspecified");
5151 }
Douglas Gregor99fa2642010-08-24 01:06:58 +00005152
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005153 // If we're completing the return type of an Objective-C method and the
5154 // identifier IBAction refers to a macro, provide a completion item for
5155 // an action, e.g.,
5156 // IBAction)<#selector#>:(id)sender
5157 if (DS.getObjCDeclQualifier() == 0 && !IsParameter &&
Richard Smith20e883e2015-04-29 23:20:19 +00005158 PP.isMacroDefined("IBAction")) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005159 CodeCompletionBuilder Builder(Results.getAllocator(),
5160 Results.getCodeCompletionTUInfo(),
5161 CCP_CodePattern, CXAvailability_Available);
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005162 Builder.AddTypedTextChunk("IBAction");
Benjamin Kramerdb534a42012-03-26 16:57:36 +00005163 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005164 Builder.AddPlaceholderChunk("selector");
Benjamin Kramerdb534a42012-03-26 16:57:36 +00005165 Builder.AddChunk(CodeCompletionString::CK_Colon);
5166 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005167 Builder.AddTextChunk("id");
Benjamin Kramerdb534a42012-03-26 16:57:36 +00005168 Builder.AddChunk(CodeCompletionString::CK_RightParen);
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005169 Builder.AddTextChunk("sender");
5170 Results.AddResult(CodeCompletionResult(Builder.TakeString()));
5171 }
Douglas Gregored1f5972013-01-30 07:11:43 +00005172
5173 // If we're completing the return type, provide 'instancetype'.
5174 if (!IsParameter) {
5175 Results.AddResult(CodeCompletionResult("instancetype"));
5176 }
Douglas Gregorf34a6f02011-02-15 22:19:42 +00005177
Douglas Gregor99fa2642010-08-24 01:06:58 +00005178 // Add various builtin type names and specifiers.
5179 AddOrdinaryNameResults(PCC_Type, S, *this, Results);
5180 Results.ExitScope();
5181
5182 // Add the various type names
5183 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
5184 CodeCompletionDeclConsumer Consumer(Results, CurContext);
5185 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5186 CodeCompleter->includeGlobals());
5187
5188 if (CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00005189 AddMacroResults(PP, Results, false);
Douglas Gregor99fa2642010-08-24 01:06:58 +00005190
5191 HandleCodeCompleteResults(this, CodeCompleter,
5192 CodeCompletionContext::CCC_Type,
5193 Results.data(), Results.size());
5194}
5195
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005196/// \brief When we have an expression with type "id", we may assume
5197/// that it has some more-specific class type based on knowledge of
5198/// common uses of Objective-C. This routine returns that class type,
5199/// or NULL if no better result could be determined.
5200static ObjCInterfaceDecl *GetAssumedMessageSendExprType(Expr *E) {
Douglas Gregored0b69d2010-09-15 16:23:04 +00005201 ObjCMessageExpr *Msg = dyn_cast_or_null<ObjCMessageExpr>(E);
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005202 if (!Msg)
Craig Topperc3ec1492014-05-26 06:22:03 +00005203 return nullptr;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005204
5205 Selector Sel = Msg->getSelector();
5206 if (Sel.isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +00005207 return nullptr;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005208
5209 IdentifierInfo *Id = Sel.getIdentifierInfoForSlot(0);
5210 if (!Id)
Craig Topperc3ec1492014-05-26 06:22:03 +00005211 return nullptr;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005212
5213 ObjCMethodDecl *Method = Msg->getMethodDecl();
5214 if (!Method)
Craig Topperc3ec1492014-05-26 06:22:03 +00005215 return nullptr;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005216
5217 // Determine the class that we're sending the message to.
Craig Topperc3ec1492014-05-26 06:22:03 +00005218 ObjCInterfaceDecl *IFace = nullptr;
Douglas Gregor9a129192010-04-21 00:45:42 +00005219 switch (Msg->getReceiverKind()) {
5220 case ObjCMessageExpr::Class:
John McCall8b07ec22010-05-15 11:32:37 +00005221 if (const ObjCObjectType *ObjType
5222 = Msg->getClassReceiver()->getAs<ObjCObjectType>())
5223 IFace = ObjType->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00005224 break;
5225
5226 case ObjCMessageExpr::Instance: {
5227 QualType T = Msg->getInstanceReceiver()->getType();
5228 if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
5229 IFace = Ptr->getInterfaceDecl();
5230 break;
5231 }
5232
5233 case ObjCMessageExpr::SuperInstance:
5234 case ObjCMessageExpr::SuperClass:
5235 break;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005236 }
5237
5238 if (!IFace)
Craig Topperc3ec1492014-05-26 06:22:03 +00005239 return nullptr;
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005240
5241 ObjCInterfaceDecl *Super = IFace->getSuperClass();
5242 if (Method->isInstanceMethod())
5243 return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5244 .Case("retain", IFace)
John McCall31168b02011-06-15 23:02:42 +00005245 .Case("strong", IFace)
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005246 .Case("autorelease", IFace)
5247 .Case("copy", IFace)
5248 .Case("copyWithZone", IFace)
5249 .Case("mutableCopy", IFace)
5250 .Case("mutableCopyWithZone", IFace)
5251 .Case("awakeFromCoder", IFace)
5252 .Case("replacementObjectFromCoder", IFace)
5253 .Case("class", IFace)
5254 .Case("classForCoder", IFace)
5255 .Case("superclass", Super)
Craig Topperc3ec1492014-05-26 06:22:03 +00005256 .Default(nullptr);
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005257
5258 return llvm::StringSwitch<ObjCInterfaceDecl *>(Id->getName())
5259 .Case("new", IFace)
5260 .Case("alloc", IFace)
5261 .Case("allocWithZone", IFace)
5262 .Case("class", IFace)
5263 .Case("superclass", Super)
Craig Topperc3ec1492014-05-26 06:22:03 +00005264 .Default(nullptr);
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005265}
5266
Douglas Gregor6fc04132010-08-27 15:10:57 +00005267// Add a special completion for a message send to "super", which fills in the
5268// most likely case of forwarding all of our arguments to the superclass
5269// function.
5270///
5271/// \param S The semantic analysis object.
5272///
Dmitri Gribenkoadba9be2012-08-23 17:58:28 +00005273/// \param NeedSuperKeyword Whether we need to prefix this completion with
Douglas Gregor6fc04132010-08-27 15:10:57 +00005274/// the "super" keyword. Otherwise, we just need to provide the arguments.
5275///
5276/// \param SelIdents The identifiers in the selector that have already been
5277/// provided as arguments for a send to "super".
5278///
Douglas Gregor6fc04132010-08-27 15:10:57 +00005279/// \param Results The set of results to augment.
5280///
5281/// \returns the Objective-C method declaration that would be invoked by
5282/// this "super" completion. If NULL, no completion was added.
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005283static ObjCMethodDecl *AddSuperSendCompletion(
5284 Sema &S, bool NeedSuperKeyword,
5285 ArrayRef<IdentifierInfo *> SelIdents,
5286 ResultBuilder &Results) {
Douglas Gregor6fc04132010-08-27 15:10:57 +00005287 ObjCMethodDecl *CurMethod = S.getCurMethodDecl();
5288 if (!CurMethod)
Craig Topperc3ec1492014-05-26 06:22:03 +00005289 return nullptr;
5290
Douglas Gregor6fc04132010-08-27 15:10:57 +00005291 ObjCInterfaceDecl *Class = CurMethod->getClassInterface();
5292 if (!Class)
Craig Topperc3ec1492014-05-26 06:22:03 +00005293 return nullptr;
5294
Douglas Gregor6fc04132010-08-27 15:10:57 +00005295 // Try to find a superclass method with the same selector.
Craig Topperc3ec1492014-05-26 06:22:03 +00005296 ObjCMethodDecl *SuperMethod = nullptr;
Douglas Gregorb5f1e462011-02-16 00:51:18 +00005297 while ((Class = Class->getSuperClass()) && !SuperMethod) {
5298 // Check in the class
Douglas Gregor6fc04132010-08-27 15:10:57 +00005299 SuperMethod = Class->getMethod(CurMethod->getSelector(),
5300 CurMethod->isInstanceMethod());
5301
Douglas Gregorb5f1e462011-02-16 00:51:18 +00005302 // Check in categories or class extensions.
5303 if (!SuperMethod) {
Aaron Ballman15063e12014-03-13 21:35:02 +00005304 for (const auto *Cat : Class->known_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00005305 if ((SuperMethod = Cat->getMethod(CurMethod->getSelector(),
Douglas Gregorb5f1e462011-02-16 00:51:18 +00005306 CurMethod->isInstanceMethod())))
5307 break;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00005308 }
Douglas Gregorb5f1e462011-02-16 00:51:18 +00005309 }
5310 }
5311
Douglas Gregor6fc04132010-08-27 15:10:57 +00005312 if (!SuperMethod)
Craig Topperc3ec1492014-05-26 06:22:03 +00005313 return nullptr;
5314
Douglas Gregor6fc04132010-08-27 15:10:57 +00005315 // Check whether the superclass method has the same signature.
5316 if (CurMethod->param_size() != SuperMethod->param_size() ||
5317 CurMethod->isVariadic() != SuperMethod->isVariadic())
Craig Topperc3ec1492014-05-26 06:22:03 +00005318 return nullptr;
5319
Douglas Gregor6fc04132010-08-27 15:10:57 +00005320 for (ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin(),
5321 CurPEnd = CurMethod->param_end(),
5322 SuperP = SuperMethod->param_begin();
5323 CurP != CurPEnd; ++CurP, ++SuperP) {
5324 // Make sure the parameter types are compatible.
5325 if (!S.Context.hasSameUnqualifiedType((*CurP)->getType(),
5326 (*SuperP)->getType()))
Craig Topperc3ec1492014-05-26 06:22:03 +00005327 return nullptr;
5328
Douglas Gregor6fc04132010-08-27 15:10:57 +00005329 // Make sure we have a parameter name to forward!
5330 if (!(*CurP)->getIdentifier())
Craig Topperc3ec1492014-05-26 06:22:03 +00005331 return nullptr;
Douglas Gregor6fc04132010-08-27 15:10:57 +00005332 }
5333
5334 // We have a superclass method. Now, form the send-to-super completion.
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005335 CodeCompletionBuilder Builder(Results.getAllocator(),
5336 Results.getCodeCompletionTUInfo());
Douglas Gregor6fc04132010-08-27 15:10:57 +00005337
5338 // Give this completion a return type.
Douglas Gregor75acd922011-09-27 23:30:47 +00005339 AddResultTypeChunk(S.Context, getCompletionPrintingPolicy(S), SuperMethod,
5340 Builder);
Douglas Gregor6fc04132010-08-27 15:10:57 +00005341
5342 // If we need the "super" keyword, add it (plus some spacing).
5343 if (NeedSuperKeyword) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005344 Builder.AddTypedTextChunk("super");
5345 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregor6fc04132010-08-27 15:10:57 +00005346 }
5347
5348 Selector Sel = CurMethod->getSelector();
5349 if (Sel.isUnarySelector()) {
5350 if (NeedSuperKeyword)
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005351 Builder.AddTextChunk(Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005352 Sel.getNameForSlot(0)));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005353 else
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005354 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005355 Sel.getNameForSlot(0)));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005356 } else {
5357 ObjCMethodDecl::param_iterator CurP = CurMethod->param_begin();
5358 for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I, ++CurP) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005359 if (I > SelIdents.size())
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005360 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
Douglas Gregor6fc04132010-08-27 15:10:57 +00005361
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005362 if (I < SelIdents.size())
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005363 Builder.AddInformativeChunk(
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005364 Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005365 Sel.getNameForSlot(I) + ":"));
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005366 else if (NeedSuperKeyword || I > SelIdents.size()) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005367 Builder.AddTextChunk(
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005368 Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005369 Sel.getNameForSlot(I) + ":"));
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005370 Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005371 (*CurP)->getIdentifier()->getName()));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005372 } else {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005373 Builder.AddTypedTextChunk(
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005374 Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005375 Sel.getNameForSlot(I) + ":"));
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005376 Builder.AddPlaceholderChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005377 (*CurP)->getIdentifier()->getName()));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005378 }
5379 }
5380 }
5381
Douglas Gregor78254c82012-03-27 23:34:16 +00005382 Results.AddResult(CodeCompletionResult(Builder.TakeString(), SuperMethod,
5383 CCP_SuperCompletion));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005384 return SuperMethod;
5385}
5386
Douglas Gregora817a192010-05-27 23:06:34 +00005387void Sema::CodeCompleteObjCMessageReceiver(Scope *S) {
John McCall276321a2010-08-25 06:19:51 +00005388 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005389 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005390 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005391 CodeCompletionContext::CCC_ObjCMessageReceiver,
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005392 getLangOpts().CPlusPlus11
Douglas Gregord8c61782012-02-15 15:34:24 +00005393 ? &ResultBuilder::IsObjCMessageReceiverOrLambdaCapture
5394 : &ResultBuilder::IsObjCMessageReceiver);
Douglas Gregora817a192010-05-27 23:06:34 +00005395
Douglas Gregora817a192010-05-27 23:06:34 +00005396 CodeCompletionDeclConsumer Consumer(Results, CurContext);
5397 Results.EnterNewScope();
Douglas Gregor39982192010-08-15 06:18:01 +00005398 LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
5399 CodeCompleter->includeGlobals());
Douglas Gregora817a192010-05-27 23:06:34 +00005400
5401 // If we are in an Objective-C method inside a class that has a superclass,
5402 // add "super" as an option.
5403 if (ObjCMethodDecl *Method = getCurMethodDecl())
5404 if (ObjCInterfaceDecl *Iface = Method->getClassInterface())
Douglas Gregor6fc04132010-08-27 15:10:57 +00005405 if (Iface->getSuperClass()) {
Douglas Gregora817a192010-05-27 23:06:34 +00005406 Results.AddResult(Result("super"));
Douglas Gregor6fc04132010-08-27 15:10:57 +00005407
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005408 AddSuperSendCompletion(*this, /*NeedSuperKeyword=*/true, None, Results);
Douglas Gregor6fc04132010-08-27 15:10:57 +00005409 }
Douglas Gregora817a192010-05-27 23:06:34 +00005410
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005411 if (getLangOpts().CPlusPlus11)
Douglas Gregord8c61782012-02-15 15:34:24 +00005412 addThisCompletion(*this, Results);
5413
Douglas Gregora817a192010-05-27 23:06:34 +00005414 Results.ExitScope();
5415
5416 if (CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00005417 AddMacroResults(PP, Results, false);
Douglas Gregor50832e02010-09-20 22:39:41 +00005418 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005419 Results.data(), Results.size());
Douglas Gregora817a192010-05-27 23:06:34 +00005420
5421}
5422
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005423void Sema::CodeCompleteObjCSuperMessage(Scope *S, SourceLocation SuperLoc,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005424 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005425 bool AtArgumentExpression) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005426 ObjCInterfaceDecl *CDecl = nullptr;
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005427 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5428 // Figure out which interface we're in.
5429 CDecl = CurMethod->getClassInterface();
5430 if (!CDecl)
5431 return;
5432
5433 // Find the superclass of this class.
5434 CDecl = CDecl->getSuperClass();
5435 if (!CDecl)
5436 return;
5437
5438 if (CurMethod->isInstanceMethod()) {
5439 // We are inside an instance method, which means that the message
5440 // send [super ...] is actually calling an instance method on the
Douglas Gregor392a84b2010-10-13 21:24:53 +00005441 // current object.
Craig Topperc3ec1492014-05-26 06:22:03 +00005442 return CodeCompleteObjCInstanceMessage(S, nullptr, SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005443 AtArgumentExpression,
Douglas Gregor392a84b2010-10-13 21:24:53 +00005444 CDecl);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005445 }
5446
5447 // Fall through to send to the superclass in CDecl.
5448 } else {
5449 // "super" may be the name of a type or variable. Figure out which
5450 // it is.
Argyrios Kyrtzidis3e56dd42013-03-14 22:56:43 +00005451 IdentifierInfo *Super = getSuperIdentifier();
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005452 NamedDecl *ND = LookupSingleName(S, Super, SuperLoc,
5453 LookupOrdinaryName);
5454 if ((CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(ND))) {
5455 // "super" names an interface. Use it.
5456 } else if (TypeDecl *TD = dyn_cast_or_null<TypeDecl>(ND)) {
John McCall8b07ec22010-05-15 11:32:37 +00005457 if (const ObjCObjectType *Iface
5458 = Context.getTypeDeclType(TD)->getAs<ObjCObjectType>())
5459 CDecl = Iface->getInterface();
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005460 } else if (ND && isa<UnresolvedUsingTypenameDecl>(ND)) {
5461 // "super" names an unresolved type; we can't be more specific.
5462 } else {
5463 // Assume that "super" names some kind of value and parse that way.
5464 CXXScopeSpec SS;
Abramo Bagnara7945c982012-01-27 09:46:47 +00005465 SourceLocation TemplateKWLoc;
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005466 UnqualifiedId id;
5467 id.setIdentifier(Super, SuperLoc);
Abramo Bagnara7945c982012-01-27 09:46:47 +00005468 ExprResult SuperExpr = ActOnIdExpression(S, SS, TemplateKWLoc, id,
5469 false, false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005470 return CodeCompleteObjCInstanceMessage(S, (Expr *)SuperExpr.get(),
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005471 SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005472 AtArgumentExpression);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005473 }
5474
5475 // Fall through
5476 }
5477
John McCallba7bf592010-08-24 05:47:05 +00005478 ParsedType Receiver;
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005479 if (CDecl)
John McCallba7bf592010-08-24 05:47:05 +00005480 Receiver = ParsedType::make(Context.getObjCInterfaceType(CDecl));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005481 return CodeCompleteObjCClassMessage(S, Receiver, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005482 AtArgumentExpression,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005483 /*IsSuper=*/true);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005484}
5485
Douglas Gregor74661272010-09-21 00:03:25 +00005486/// \brief Given a set of code-completion results for the argument of a message
5487/// send, determine the preferred type (if any) for that argument expression.
5488static QualType getPreferredArgumentTypeForMessageSend(ResultBuilder &Results,
5489 unsigned NumSelIdents) {
5490 typedef CodeCompletionResult Result;
5491 ASTContext &Context = Results.getSema().Context;
5492
5493 QualType PreferredType;
5494 unsigned BestPriority = CCP_Unlikely * 2;
5495 Result *ResultsData = Results.data();
5496 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
5497 Result &R = ResultsData[I];
5498 if (R.Kind == Result::RK_Declaration &&
5499 isa<ObjCMethodDecl>(R.Declaration)) {
5500 if (R.Priority <= BestPriority) {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00005501 const ObjCMethodDecl *Method = cast<ObjCMethodDecl>(R.Declaration);
Douglas Gregor74661272010-09-21 00:03:25 +00005502 if (NumSelIdents <= Method->param_size()) {
Alp Toker03376dc2014-07-07 09:02:20 +00005503 QualType MyPreferredType = Method->parameters()[NumSelIdents - 1]
Douglas Gregor74661272010-09-21 00:03:25 +00005504 ->getType();
5505 if (R.Priority < BestPriority || PreferredType.isNull()) {
5506 BestPriority = R.Priority;
5507 PreferredType = MyPreferredType;
5508 } else if (!Context.hasSameUnqualifiedType(PreferredType,
5509 MyPreferredType)) {
5510 PreferredType = QualType();
5511 }
5512 }
5513 }
5514 }
5515 }
5516
5517 return PreferredType;
5518}
5519
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005520static void AddClassMessageCompletions(Sema &SemaRef, Scope *S,
5521 ParsedType Receiver,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005522 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005523 bool AtArgumentExpression,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005524 bool IsSuper,
5525 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00005526 typedef CodeCompletionResult Result;
Craig Topperc3ec1492014-05-26 06:22:03 +00005527 ObjCInterfaceDecl *CDecl = nullptr;
5528
Douglas Gregor8ce33212009-11-17 17:59:40 +00005529 // If the given name refers to an interface type, retrieve the
5530 // corresponding declaration.
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005531 if (Receiver) {
Craig Topperc3ec1492014-05-26 06:22:03 +00005532 QualType T = SemaRef.GetTypeFromParser(Receiver, nullptr);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005533 if (!T.isNull())
John McCall8b07ec22010-05-15 11:32:37 +00005534 if (const ObjCObjectType *Interface = T->getAs<ObjCObjectType>())
5535 CDecl = Interface->getInterface();
Douglas Gregor8ce33212009-11-17 17:59:40 +00005536 }
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005537
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005538 // Add all of the factory methods in this Objective-C class, its protocols,
5539 // superclasses, categories, implementation, etc.
Steve Naroffeae65032009-11-07 02:08:14 +00005540 Results.EnterNewScope();
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005541
Douglas Gregor6fc04132010-08-27 15:10:57 +00005542 // If this is a send-to-super, try to add the special "super" send
5543 // completion.
5544 if (IsSuper) {
5545 if (ObjCMethodDecl *SuperMethod
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005546 = AddSuperSendCompletion(SemaRef, false, SelIdents, Results))
Douglas Gregor6fc04132010-08-27 15:10:57 +00005547 Results.Ignore(SuperMethod);
5548 }
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005549
Douglas Gregorc2cb2e22010-08-27 15:29:55 +00005550 // If we're inside an Objective-C method definition, prefer its selector to
5551 // others.
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005552 if (ObjCMethodDecl *CurMethod = SemaRef.getCurMethodDecl())
Douglas Gregorc2cb2e22010-08-27 15:29:55 +00005553 Results.setPreferredSelector(CurMethod->getSelector());
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005554
Douglas Gregor1154e272010-09-16 16:06:31 +00005555 VisitedSelectorSet Selectors;
Douglas Gregor6285f752010-04-06 16:40:00 +00005556 if (CDecl)
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005557 AddObjCMethods(CDecl, false, MK_Any, SelIdents,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005558 SemaRef.CurContext, Selectors, AtArgumentExpression,
5559 Results);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00005560 else {
Douglas Gregor6285f752010-04-06 16:40:00 +00005561 // We're messaging "id" as a type; provide all class/factory methods.
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005562
Douglas Gregord720daf2010-04-06 17:30:22 +00005563 // If we have an external source, load the entire class method
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005564 // pool from the AST file.
Axel Naumanndd433f02012-10-18 19:05:02 +00005565 if (SemaRef.getExternalSource()) {
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005566 for (uint32_t I = 0,
Axel Naumanndd433f02012-10-18 19:05:02 +00005567 N = SemaRef.getExternalSource()->GetNumExternalSelectors();
John McCall75b960e2010-06-01 09:23:16 +00005568 I != N; ++I) {
Axel Naumanndd433f02012-10-18 19:05:02 +00005569 Selector Sel = SemaRef.getExternalSource()->GetExternalSelector(I);
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005570 if (Sel.isNull() || SemaRef.MethodPool.count(Sel))
Douglas Gregord720daf2010-04-06 17:30:22 +00005571 continue;
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005572
5573 SemaRef.ReadMethodPool(Sel);
Douglas Gregord720daf2010-04-06 17:30:22 +00005574 }
5575 }
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005576
5577 for (Sema::GlobalMethodPool::iterator M = SemaRef.MethodPool.begin(),
5578 MEnd = SemaRef.MethodPool.end();
Sebastian Redl75d8a322010-08-02 23:18:59 +00005579 M != MEnd; ++M) {
5580 for (ObjCMethodList *MethList = &M->second.second;
Nico Weber2e0c8f72014-12-27 03:58:08 +00005581 MethList && MethList->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00005582 MethList = MethList->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00005583 if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
Douglas Gregor6285f752010-04-06 16:40:00 +00005584 continue;
Craig Topperc3ec1492014-05-26 06:22:03 +00005585
Nico Weber2e0c8f72014-12-27 03:58:08 +00005586 Result R(MethList->getMethod(),
5587 Results.getBasePriority(MethList->getMethod()), nullptr);
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005588 R.StartParameter = SelIdents.size();
Douglas Gregor6285f752010-04-06 16:40:00 +00005589 R.AllParametersAreInformative = false;
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005590 Results.MaybeAddResult(R, SemaRef.CurContext);
Douglas Gregor6285f752010-04-06 16:40:00 +00005591 }
5592 }
5593 }
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005594
5595 Results.ExitScope();
5596}
Douglas Gregor6285f752010-04-06 16:40:00 +00005597
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005598void Sema::CodeCompleteObjCClassMessage(Scope *S, ParsedType Receiver,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005599 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005600 bool AtArgumentExpression,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00005601 bool IsSuper) {
Douglas Gregor63745d52011-07-21 01:05:26 +00005602
5603 QualType T = this->GetTypeFromParser(Receiver);
5604
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005605 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005606 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor63745d52011-07-21 01:05:26 +00005607 CodeCompletionContext(CodeCompletionContext::CCC_ObjCClassMessage,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005608 T, SelIdents));
Douglas Gregor63745d52011-07-21 01:05:26 +00005609
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005610 AddClassMessageCompletions(*this, S, Receiver, SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005611 AtArgumentExpression, IsSuper, Results);
Douglas Gregor74661272010-09-21 00:03:25 +00005612
5613 // If we're actually at the argument expression (rather than prior to the
5614 // selector), we're actually performing code completion for an expression.
5615 // Determine whether we have a single, best method. If so, we can
5616 // code-complete the expression using the corresponding parameter type as
5617 // our preferred type, improving completion results.
5618 if (AtArgumentExpression) {
5619 QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005620 SelIdents.size());
Douglas Gregor74661272010-09-21 00:03:25 +00005621 if (PreferredType.isNull())
5622 CodeCompleteOrdinaryName(S, PCC_Expression);
5623 else
5624 CodeCompleteExpression(S, PreferredType);
5625 return;
5626 }
5627
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005628 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor63745d52011-07-21 01:05:26 +00005629 Results.getCompletionContext(),
Douglas Gregor6fc04132010-08-27 15:10:57 +00005630 Results.data(), Results.size());
Steve Naroffeae65032009-11-07 02:08:14 +00005631}
5632
Richard Trieu2bd04012011-09-09 02:00:50 +00005633void Sema::CodeCompleteObjCInstanceMessage(Scope *S, Expr *Receiver,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005634 ArrayRef<IdentifierInfo *> SelIdents,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00005635 bool AtArgumentExpression,
Douglas Gregor392a84b2010-10-13 21:24:53 +00005636 ObjCInterfaceDecl *Super) {
John McCall276321a2010-08-25 06:19:51 +00005637 typedef CodeCompletionResult Result;
Steve Naroffeae65032009-11-07 02:08:14 +00005638
5639 Expr *RecExpr = static_cast<Expr *>(Receiver);
Steve Naroffeae65032009-11-07 02:08:14 +00005640
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005641 // If necessary, apply function/array conversion to the receiver.
5642 // C99 6.7.5.3p[7,8].
John Wiegley01296292011-04-08 18:41:53 +00005643 if (RecExpr) {
5644 ExprResult Conv = DefaultFunctionArrayLvalueConversion(RecExpr);
5645 if (Conv.isInvalid()) // conversion failed. bail.
5646 return;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005647 RecExpr = Conv.get();
John Wiegley01296292011-04-08 18:41:53 +00005648 }
Douglas Gregor392a84b2010-10-13 21:24:53 +00005649 QualType ReceiverType = RecExpr? RecExpr->getType()
5650 : Super? Context.getObjCObjectPointerType(
5651 Context.getObjCInterfaceType(Super))
5652 : Context.getObjCIdType();
Steve Naroffeae65032009-11-07 02:08:14 +00005653
Douglas Gregordc520b02010-11-08 21:12:30 +00005654 // If we're messaging an expression with type "id" or "Class", check
5655 // whether we know something special about the receiver that allows
5656 // us to assume a more-specific receiver type.
Anders Carlsson382ba412014-02-28 19:07:22 +00005657 if (ReceiverType->isObjCIdType() || ReceiverType->isObjCClassType()) {
Douglas Gregordc520b02010-11-08 21:12:30 +00005658 if (ObjCInterfaceDecl *IFace = GetAssumedMessageSendExprType(RecExpr)) {
5659 if (ReceiverType->isObjCClassType())
5660 return CodeCompleteObjCClassMessage(S,
5661 ParsedType::make(Context.getObjCInterfaceType(IFace)),
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005662 SelIdents,
Douglas Gregordc520b02010-11-08 21:12:30 +00005663 AtArgumentExpression, Super);
5664
5665 ReceiverType = Context.getObjCObjectPointerType(
5666 Context.getObjCInterfaceType(IFace));
5667 }
Anders Carlsson382ba412014-02-28 19:07:22 +00005668 } else if (RecExpr && getLangOpts().CPlusPlus) {
5669 ExprResult Conv = PerformContextuallyConvertToObjCPointer(RecExpr);
5670 if (Conv.isUsable()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005671 RecExpr = Conv.get();
Anders Carlsson382ba412014-02-28 19:07:22 +00005672 ReceiverType = RecExpr->getType();
5673 }
5674 }
Douglas Gregordc520b02010-11-08 21:12:30 +00005675
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005676 // Build the set of methods we can see.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005677 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005678 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor63745d52011-07-21 01:05:26 +00005679 CodeCompletionContext(CodeCompletionContext::CCC_ObjCInstanceMessage,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005680 ReceiverType, SelIdents));
Douglas Gregor63745d52011-07-21 01:05:26 +00005681
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005682 Results.EnterNewScope();
Douglas Gregor9d2ddb22010-04-06 19:22:33 +00005683
Douglas Gregor6fc04132010-08-27 15:10:57 +00005684 // If this is a send-to-super, try to add the special "super" send
5685 // completion.
Douglas Gregor392a84b2010-10-13 21:24:53 +00005686 if (Super) {
Douglas Gregor6fc04132010-08-27 15:10:57 +00005687 if (ObjCMethodDecl *SuperMethod
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005688 = AddSuperSendCompletion(*this, false, SelIdents, Results))
Douglas Gregor6fc04132010-08-27 15:10:57 +00005689 Results.Ignore(SuperMethod);
5690 }
5691
Douglas Gregorc2cb2e22010-08-27 15:29:55 +00005692 // If we're inside an Objective-C method definition, prefer its selector to
5693 // others.
5694 if (ObjCMethodDecl *CurMethod = getCurMethodDecl())
5695 Results.setPreferredSelector(CurMethod->getSelector());
Douglas Gregorbab2b3c2009-11-17 23:22:23 +00005696
Douglas Gregor1154e272010-09-16 16:06:31 +00005697 // Keep track of the selectors we've already added.
5698 VisitedSelectorSet Selectors;
5699
Douglas Gregora3329fa2009-11-18 00:06:18 +00005700 // Handle messages to Class. This really isn't a message to an instance
5701 // method, so we treat it the same way we would treat a message send to a
5702 // class method.
5703 if (ReceiverType->isObjCClassType() ||
5704 ReceiverType->isObjCQualifiedClassType()) {
5705 if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
5706 if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005707 AddObjCMethods(ClassDecl, false, MK_Any, SelIdents,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005708 CurContext, Selectors, AtArgumentExpression, Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00005709 }
5710 }
5711 // Handle messages to a qualified ID ("id<foo>").
5712 else if (const ObjCObjectPointerType *QualID
5713 = ReceiverType->getAsObjCQualifiedIdType()) {
5714 // Search protocols for instance methods.
Aaron Ballman83731462014-03-17 16:14:00 +00005715 for (auto *I : QualID->quals())
5716 AddObjCMethods(I, true, MK_Any, SelIdents, CurContext,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005717 Selectors, AtArgumentExpression, Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00005718 }
5719 // Handle messages to a pointer to interface type.
5720 else if (const ObjCObjectPointerType *IFacePtr
5721 = ReceiverType->getAsObjCInterfacePointerType()) {
5722 // Search the class, its superclasses, etc., for instance methods.
Douglas Gregorc8537c52009-11-19 07:41:15 +00005723 AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005724 CurContext, Selectors, AtArgumentExpression,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005725 Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00005726
5727 // Search protocols for instance methods.
Aaron Ballman83731462014-03-17 16:14:00 +00005728 for (auto *I : IFacePtr->quals())
5729 AddObjCMethods(I, true, MK_Any, SelIdents, CurContext,
Douglas Gregorb4a7c032010-11-17 21:36:08 +00005730 Selectors, AtArgumentExpression, Results);
Douglas Gregora3329fa2009-11-18 00:06:18 +00005731 }
Douglas Gregor6285f752010-04-06 16:40:00 +00005732 // Handle messages to "id".
5733 else if (ReceiverType->isObjCIdType()) {
Douglas Gregord720daf2010-04-06 17:30:22 +00005734 // We're messaging "id", so provide all instance methods we know
5735 // about as code-completion results.
5736
5737 // If we have an external source, load the entire class method
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005738 // pool from the AST file.
Douglas Gregord720daf2010-04-06 17:30:22 +00005739 if (ExternalSource) {
John McCall75b960e2010-06-01 09:23:16 +00005740 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5741 I != N; ++I) {
5742 Selector Sel = ExternalSource->GetExternalSelector(I);
Sebastian Redl75d8a322010-08-02 23:18:59 +00005743 if (Sel.isNull() || MethodPool.count(Sel))
Douglas Gregord720daf2010-04-06 17:30:22 +00005744 continue;
5745
Sebastian Redl75d8a322010-08-02 23:18:59 +00005746 ReadMethodPool(Sel);
Douglas Gregord720daf2010-04-06 17:30:22 +00005747 }
5748 }
5749
Sebastian Redl75d8a322010-08-02 23:18:59 +00005750 for (GlobalMethodPool::iterator M = MethodPool.begin(),
5751 MEnd = MethodPool.end();
5752 M != MEnd; ++M) {
5753 for (ObjCMethodList *MethList = &M->second.first;
Nico Weber2e0c8f72014-12-27 03:58:08 +00005754 MethList && MethList->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00005755 MethList = MethList->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00005756 if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
Douglas Gregor6285f752010-04-06 16:40:00 +00005757 continue;
Douglas Gregor1154e272010-09-16 16:06:31 +00005758
Nico Weber2e0c8f72014-12-27 03:58:08 +00005759 if (!Selectors.insert(MethList->getMethod()->getSelector()).second)
Douglas Gregor1154e272010-09-16 16:06:31 +00005760 continue;
Craig Topperc3ec1492014-05-26 06:22:03 +00005761
Nico Weber2e0c8f72014-12-27 03:58:08 +00005762 Result R(MethList->getMethod(),
5763 Results.getBasePriority(MethList->getMethod()), nullptr);
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005764 R.StartParameter = SelIdents.size();
Douglas Gregor6285f752010-04-06 16:40:00 +00005765 R.AllParametersAreInformative = false;
5766 Results.MaybeAddResult(R, CurContext);
5767 }
5768 }
5769 }
Steve Naroffeae65032009-11-07 02:08:14 +00005770 Results.ExitScope();
Douglas Gregor74661272010-09-21 00:03:25 +00005771
5772
5773 // If we're actually at the argument expression (rather than prior to the
5774 // selector), we're actually performing code completion for an expression.
5775 // Determine whether we have a single, best method. If so, we can
5776 // code-complete the expression using the corresponding parameter type as
5777 // our preferred type, improving completion results.
5778 if (AtArgumentExpression) {
5779 QualType PreferredType = getPreferredArgumentTypeForMessageSend(Results,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005780 SelIdents.size());
Douglas Gregor74661272010-09-21 00:03:25 +00005781 if (PreferredType.isNull())
5782 CodeCompleteOrdinaryName(S, PCC_Expression);
5783 else
5784 CodeCompleteExpression(S, PreferredType);
5785 return;
5786 }
5787
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005788 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor63745d52011-07-21 01:05:26 +00005789 Results.getCompletionContext(),
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005790 Results.data(),Results.size());
Steve Naroffeae65032009-11-07 02:08:14 +00005791}
Douglas Gregorbaf69612009-11-18 04:19:12 +00005792
Douglas Gregor68762e72010-08-23 21:17:50 +00005793void Sema::CodeCompleteObjCForCollection(Scope *S,
5794 DeclGroupPtrTy IterationVar) {
5795 CodeCompleteExpressionData Data;
5796 Data.ObjCCollection = true;
5797
5798 if (IterationVar.getAsOpaquePtr()) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00005799 DeclGroupRef DG = IterationVar.get();
Douglas Gregor68762e72010-08-23 21:17:50 +00005800 for (DeclGroupRef::iterator I = DG.begin(), End = DG.end(); I != End; ++I) {
5801 if (*I)
5802 Data.IgnoreDecls.push_back(*I);
5803 }
5804 }
5805
5806 CodeCompleteExpression(S, Data);
5807}
5808
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005809void Sema::CodeCompleteObjCSelector(Scope *S,
5810 ArrayRef<IdentifierInfo *> SelIdents) {
Douglas Gregor67c692c2010-08-26 15:07:07 +00005811 // If we have an external source, load the entire class method
5812 // pool from the AST file.
5813 if (ExternalSource) {
5814 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
5815 I != N; ++I) {
5816 Selector Sel = ExternalSource->GetExternalSelector(I);
5817 if (Sel.isNull() || MethodPool.count(Sel))
5818 continue;
5819
5820 ReadMethodPool(Sel);
5821 }
5822 }
5823
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005824 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005825 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005826 CodeCompletionContext::CCC_SelectorName);
Douglas Gregor67c692c2010-08-26 15:07:07 +00005827 Results.EnterNewScope();
5828 for (GlobalMethodPool::iterator M = MethodPool.begin(),
5829 MEnd = MethodPool.end();
5830 M != MEnd; ++M) {
5831
5832 Selector Sel = M->first;
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005833 if (!isAcceptableObjCSelector(Sel, MK_Any, SelIdents))
Douglas Gregor67c692c2010-08-26 15:07:07 +00005834 continue;
5835
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005836 CodeCompletionBuilder Builder(Results.getAllocator(),
5837 Results.getCodeCompletionTUInfo());
Douglas Gregor67c692c2010-08-26 15:07:07 +00005838 if (Sel.isUnarySelector()) {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005839 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00005840 Sel.getNameForSlot(0)));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005841 Results.AddResult(Builder.TakeString());
Douglas Gregor67c692c2010-08-26 15:07:07 +00005842 continue;
5843 }
5844
Douglas Gregor9ac1ad12010-08-26 16:46:39 +00005845 std::string Accumulator;
Douglas Gregor67c692c2010-08-26 15:07:07 +00005846 for (unsigned I = 0, N = Sel.getNumArgs(); I != N; ++I) {
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00005847 if (I == SelIdents.size()) {
Douglas Gregor9ac1ad12010-08-26 16:46:39 +00005848 if (!Accumulator.empty()) {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005849 Builder.AddInformativeChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005850 Accumulator));
Douglas Gregor9ac1ad12010-08-26 16:46:39 +00005851 Accumulator.clear();
5852 }
5853 }
5854
Benjamin Kramer632500c2011-07-26 16:59:25 +00005855 Accumulator += Sel.getNameForSlot(I);
Douglas Gregor9ac1ad12010-08-26 16:46:39 +00005856 Accumulator += ':';
Douglas Gregor67c692c2010-08-26 15:07:07 +00005857 }
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00005858 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString( Accumulator));
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005859 Results.AddResult(Builder.TakeString());
Douglas Gregor67c692c2010-08-26 15:07:07 +00005860 }
5861 Results.ExitScope();
5862
5863 HandleCodeCompleteResults(this, CodeCompleter,
5864 CodeCompletionContext::CCC_SelectorName,
5865 Results.data(), Results.size());
5866}
5867
Douglas Gregorbaf69612009-11-18 04:19:12 +00005868/// \brief Add all of the protocol declarations that we find in the given
5869/// (translation unit) context.
5870static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext,
Douglas Gregor5b4671c2009-11-18 04:49:41 +00005871 bool OnlyForwardDeclarations,
Douglas Gregorbaf69612009-11-18 04:19:12 +00005872 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00005873 typedef CodeCompletionResult Result;
Douglas Gregorbaf69612009-11-18 04:19:12 +00005874
Aaron Ballman629afae2014-03-07 19:56:05 +00005875 for (const auto *D : Ctx->decls()) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00005876 // Record any protocols we find.
Aaron Ballman629afae2014-03-07 19:56:05 +00005877 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(D))
Douglas Gregore6e48b12012-01-01 19:29:29 +00005878 if (!OnlyForwardDeclarations || !Proto->hasDefinition())
Craig Topperc3ec1492014-05-26 06:22:03 +00005879 Results.AddResult(Result(Proto, Results.getBasePriority(Proto),nullptr),
5880 CurContext, nullptr, false);
Douglas Gregorbaf69612009-11-18 04:19:12 +00005881 }
5882}
5883
5884void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
5885 unsigned NumProtocols) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005886 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005887 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005888 CodeCompletionContext::CCC_ObjCProtocolName);
Douglas Gregorbaf69612009-11-18 04:19:12 +00005889
Douglas Gregora3b23b02010-12-09 21:44:02 +00005890 if (CodeCompleter && CodeCompleter->includeGlobals()) {
5891 Results.EnterNewScope();
5892
5893 // Tell the result set to ignore all of the protocols we have
5894 // already seen.
5895 // FIXME: This doesn't work when caching code-completion results.
5896 for (unsigned I = 0; I != NumProtocols; ++I)
5897 if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first,
5898 Protocols[I].second))
5899 Results.Ignore(Protocol);
Douglas Gregorbaf69612009-11-18 04:19:12 +00005900
Douglas Gregora3b23b02010-12-09 21:44:02 +00005901 // Add all protocols.
5902 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false,
5903 Results);
Douglas Gregor5b4671c2009-11-18 04:49:41 +00005904
Douglas Gregora3b23b02010-12-09 21:44:02 +00005905 Results.ExitScope();
5906 }
5907
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005908 HandleCodeCompleteResults(this, CodeCompleter,
5909 CodeCompletionContext::CCC_ObjCProtocolName,
5910 Results.data(),Results.size());
Douglas Gregor5b4671c2009-11-18 04:49:41 +00005911}
5912
5913void Sema::CodeCompleteObjCProtocolDecl(Scope *) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005914 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005915 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005916 CodeCompletionContext::CCC_ObjCProtocolName);
Douglas Gregor5b4671c2009-11-18 04:49:41 +00005917
Douglas Gregora3b23b02010-12-09 21:44:02 +00005918 if (CodeCompleter && CodeCompleter->includeGlobals()) {
5919 Results.EnterNewScope();
5920
5921 // Add all protocols.
5922 AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true,
5923 Results);
Douglas Gregorbaf69612009-11-18 04:19:12 +00005924
Douglas Gregora3b23b02010-12-09 21:44:02 +00005925 Results.ExitScope();
5926 }
5927
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005928 HandleCodeCompleteResults(this, CodeCompleter,
5929 CodeCompletionContext::CCC_ObjCProtocolName,
5930 Results.data(),Results.size());
Douglas Gregorbaf69612009-11-18 04:19:12 +00005931}
Douglas Gregor49c22a72009-11-18 16:26:39 +00005932
5933/// \brief Add all of the Objective-C interface declarations that we find in
5934/// the given (translation unit) context.
5935static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext,
5936 bool OnlyForwardDeclarations,
5937 bool OnlyUnimplemented,
5938 ResultBuilder &Results) {
John McCall276321a2010-08-25 06:19:51 +00005939 typedef CodeCompletionResult Result;
Douglas Gregor49c22a72009-11-18 16:26:39 +00005940
Aaron Ballman629afae2014-03-07 19:56:05 +00005941 for (const auto *D : Ctx->decls()) {
Douglas Gregor1c283312010-08-11 12:19:30 +00005942 // Record any interfaces we find.
Aaron Ballman629afae2014-03-07 19:56:05 +00005943 if (const auto *Class = dyn_cast<ObjCInterfaceDecl>(D))
Douglas Gregordc9166c2011-12-15 20:29:51 +00005944 if ((!OnlyForwardDeclarations || !Class->hasDefinition()) &&
Douglas Gregor1c283312010-08-11 12:19:30 +00005945 (!OnlyUnimplemented || !Class->getImplementation()))
Craig Topperc3ec1492014-05-26 06:22:03 +00005946 Results.AddResult(Result(Class, Results.getBasePriority(Class),nullptr),
5947 CurContext, nullptr, false);
Douglas Gregor49c22a72009-11-18 16:26:39 +00005948 }
5949}
5950
5951void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005952 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005953 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005954 CodeCompletionContext::CCC_Other);
Douglas Gregor49c22a72009-11-18 16:26:39 +00005955 Results.EnterNewScope();
5956
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005957 if (CodeCompleter->includeGlobals()) {
5958 // Add all classes.
5959 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5960 false, Results);
5961 }
5962
Douglas Gregor49c22a72009-11-18 16:26:39 +00005963 Results.ExitScope();
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005964
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005965 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005966 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005967 Results.data(),Results.size());
Douglas Gregor49c22a72009-11-18 16:26:39 +00005968}
5969
Douglas Gregorb2ccf012010-04-15 22:33:43 +00005970void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName,
5971 SourceLocation ClassNameLoc) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005972 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005973 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005974 CodeCompletionContext::CCC_ObjCInterfaceName);
Douglas Gregor49c22a72009-11-18 16:26:39 +00005975 Results.EnterNewScope();
5976
5977 // Make sure that we ignore the class we're currently defining.
5978 NamedDecl *CurClass
Douglas Gregorb2ccf012010-04-15 22:33:43 +00005979 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00005980 if (CurClass && isa<ObjCInterfaceDecl>(CurClass))
Douglas Gregor49c22a72009-11-18 16:26:39 +00005981 Results.Ignore(CurClass);
5982
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005983 if (CodeCompleter->includeGlobals()) {
5984 // Add all classes.
5985 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
5986 false, Results);
5987 }
5988
Douglas Gregor49c22a72009-11-18 16:26:39 +00005989 Results.ExitScope();
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005990
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005991 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor2c595ad2011-07-30 06:55:39 +00005992 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00005993 Results.data(),Results.size());
Douglas Gregor49c22a72009-11-18 16:26:39 +00005994}
5995
5996void Sema::CodeCompleteObjCImplementationDecl(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005997 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00005998 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00005999 CodeCompletionContext::CCC_Other);
Douglas Gregor49c22a72009-11-18 16:26:39 +00006000 Results.EnterNewScope();
6001
Douglas Gregor2c595ad2011-07-30 06:55:39 +00006002 if (CodeCompleter->includeGlobals()) {
6003 // Add all unimplemented classes.
6004 AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
6005 true, Results);
6006 }
6007
Douglas Gregor49c22a72009-11-18 16:26:39 +00006008 Results.ExitScope();
Douglas Gregor2c595ad2011-07-30 06:55:39 +00006009
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006010 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor2c595ad2011-07-30 06:55:39 +00006011 CodeCompletionContext::CCC_ObjCInterfaceName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006012 Results.data(),Results.size());
Douglas Gregor49c22a72009-11-18 16:26:39 +00006013}
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006014
6015void Sema::CodeCompleteObjCInterfaceCategory(Scope *S,
Douglas Gregorb2ccf012010-04-15 22:33:43 +00006016 IdentifierInfo *ClassName,
6017 SourceLocation ClassNameLoc) {
John McCall276321a2010-08-25 06:19:51 +00006018 typedef CodeCompletionResult Result;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006019
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006020 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006021 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor21325842011-07-07 16:03:39 +00006022 CodeCompletionContext::CCC_ObjCCategoryName);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006023
6024 // Ignore any categories we find that have already been implemented by this
6025 // interface.
6026 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
6027 NamedDecl *CurClass
Douglas Gregorb2ccf012010-04-15 22:33:43 +00006028 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006029 if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass)){
Aaron Ballman3fe486a2014-03-13 21:23:55 +00006030 for (const auto *Cat : Class->visible_categories())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006031 CategoryNames.insert(Cat->getIdentifier());
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006032 }
6033
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006034 // Add all of the categories we know about.
6035 Results.EnterNewScope();
6036 TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Aaron Ballman629afae2014-03-07 19:56:05 +00006037 for (const auto *D : TU->decls())
6038 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(D))
David Blaikie82e95a32014-11-19 07:49:47 +00006039 if (CategoryNames.insert(Category->getIdentifier()).second)
Craig Topperc3ec1492014-05-26 06:22:03 +00006040 Results.AddResult(Result(Category, Results.getBasePriority(Category),
6041 nullptr),
6042 CurContext, nullptr, false);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006043 Results.ExitScope();
6044
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006045 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor21325842011-07-07 16:03:39 +00006046 CodeCompletionContext::CCC_ObjCCategoryName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006047 Results.data(),Results.size());
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006048}
6049
6050void Sema::CodeCompleteObjCImplementationCategory(Scope *S,
Douglas Gregorb2ccf012010-04-15 22:33:43 +00006051 IdentifierInfo *ClassName,
6052 SourceLocation ClassNameLoc) {
John McCall276321a2010-08-25 06:19:51 +00006053 typedef CodeCompletionResult Result;
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006054
6055 // Find the corresponding interface. If we couldn't find the interface, the
6056 // program itself is ill-formed. However, we'll try to be helpful still by
6057 // providing the list of all of the categories we know about.
6058 NamedDecl *CurClass
Douglas Gregorb2ccf012010-04-15 22:33:43 +00006059 = LookupSingleName(TUScope, ClassName, ClassNameLoc, LookupOrdinaryName);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006060 ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass);
6061 if (!Class)
Douglas Gregorb2ccf012010-04-15 22:33:43 +00006062 return CodeCompleteObjCInterfaceCategory(S, ClassName, ClassNameLoc);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006063
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006064 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006065 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor21325842011-07-07 16:03:39 +00006066 CodeCompletionContext::CCC_ObjCCategoryName);
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006067
6068 // Add all of the categories that have have corresponding interface
6069 // declarations in this class and any of its superclasses, except for
6070 // already-implemented categories in the class itself.
6071 llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
6072 Results.EnterNewScope();
6073 bool IgnoreImplemented = true;
6074 while (Class) {
Aaron Ballman3fe486a2014-03-13 21:23:55 +00006075 for (const auto *Cat : Class->visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006076 if ((!IgnoreImplemented || !Cat->getImplementation()) &&
David Blaikie82e95a32014-11-19 07:49:47 +00006077 CategoryNames.insert(Cat->getIdentifier()).second)
Craig Topperc3ec1492014-05-26 06:22:03 +00006078 Results.AddResult(Result(Cat, Results.getBasePriority(Cat), nullptr),
6079 CurContext, nullptr, false);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006080 }
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006081
6082 Class = Class->getSuperClass();
6083 IgnoreImplemented = false;
6084 }
6085 Results.ExitScope();
6086
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006087 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor21325842011-07-07 16:03:39 +00006088 CodeCompletionContext::CCC_ObjCCategoryName,
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006089 Results.data(),Results.size());
Douglas Gregor5d34fd32009-11-18 19:08:43 +00006090}
Douglas Gregor5d649882009-11-18 22:32:06 +00006091
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006092void Sema::CodeCompleteObjCPropertyDefinition(Scope *S) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006093 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006094 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006095 CodeCompletionContext::CCC_Other);
Douglas Gregor5d649882009-11-18 22:32:06 +00006096
6097 // Figure out where this @synthesize lives.
6098 ObjCContainerDecl *Container
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006099 = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
Douglas Gregor5d649882009-11-18 22:32:06 +00006100 if (!Container ||
6101 (!isa<ObjCImplementationDecl>(Container) &&
6102 !isa<ObjCCategoryImplDecl>(Container)))
6103 return;
6104
6105 // Ignore any properties that have already been implemented.
Douglas Gregor9b4f3702012-06-12 13:44:08 +00006106 Container = getContainerDef(Container);
Aaron Ballman629afae2014-03-07 19:56:05 +00006107 for (const auto *D : Container->decls())
6108 if (const auto *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(D))
Douglas Gregor5d649882009-11-18 22:32:06 +00006109 Results.Ignore(PropertyImpl->getPropertyDecl());
6110
6111 // Add any properties that we find.
Douglas Gregorb888acf2010-12-09 23:01:55 +00006112 AddedPropertiesSet AddedProperties;
Douglas Gregor5d649882009-11-18 22:32:06 +00006113 Results.EnterNewScope();
6114 if (ObjCImplementationDecl *ClassImpl
6115 = dyn_cast<ObjCImplementationDecl>(Container))
Douglas Gregor95147142011-05-05 15:50:42 +00006116 AddObjCProperties(ClassImpl->getClassInterface(), false,
6117 /*AllowNullaryMethods=*/false, CurContext,
Douglas Gregorb888acf2010-12-09 23:01:55 +00006118 AddedProperties, Results);
Douglas Gregor5d649882009-11-18 22:32:06 +00006119 else
6120 AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(),
Douglas Gregor95147142011-05-05 15:50:42 +00006121 false, /*AllowNullaryMethods=*/false, CurContext,
6122 AddedProperties, Results);
Douglas Gregor5d649882009-11-18 22:32:06 +00006123 Results.ExitScope();
6124
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006125 HandleCodeCompleteResults(this, CodeCompleter,
6126 CodeCompletionContext::CCC_Other,
6127 Results.data(),Results.size());
Douglas Gregor5d649882009-11-18 22:32:06 +00006128}
6129
6130void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006131 IdentifierInfo *PropertyName) {
John McCall276321a2010-08-25 06:19:51 +00006132 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006133 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006134 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00006135 CodeCompletionContext::CCC_Other);
Douglas Gregor5d649882009-11-18 22:32:06 +00006136
6137 // Figure out where this @synthesize lives.
6138 ObjCContainerDecl *Container
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006139 = dyn_cast_or_null<ObjCContainerDecl>(CurContext);
Douglas Gregor5d649882009-11-18 22:32:06 +00006140 if (!Container ||
6141 (!isa<ObjCImplementationDecl>(Container) &&
6142 !isa<ObjCCategoryImplDecl>(Container)))
6143 return;
6144
6145 // Figure out which interface we're looking into.
Craig Topperc3ec1492014-05-26 06:22:03 +00006146 ObjCInterfaceDecl *Class = nullptr;
Douglas Gregor5d649882009-11-18 22:32:06 +00006147 if (ObjCImplementationDecl *ClassImpl
6148 = dyn_cast<ObjCImplementationDecl>(Container))
6149 Class = ClassImpl->getClassInterface();
6150 else
6151 Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
6152 ->getClassInterface();
6153
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006154 // Determine the type of the property we're synthesizing.
6155 QualType PropertyType = Context.getObjCIdType();
6156 if (Class) {
6157 if (ObjCPropertyDecl *Property
6158 = Class->FindPropertyDeclaration(PropertyName)) {
6159 PropertyType
6160 = Property->getType().getNonReferenceType().getUnqualifiedType();
6161
6162 // Give preference to ivars
6163 Results.setPreferredType(PropertyType);
6164 }
6165 }
6166
Douglas Gregor5d649882009-11-18 22:32:06 +00006167 // Add all of the instance variables in this class and its superclasses.
6168 Results.EnterNewScope();
Douglas Gregor331faa02011-04-18 14:13:53 +00006169 bool SawSimilarlyNamedIvar = false;
6170 std::string NameWithPrefix;
6171 NameWithPrefix += '_';
Benjamin Kramer632500c2011-07-26 16:59:25 +00006172 NameWithPrefix += PropertyName->getName();
Douglas Gregor331faa02011-04-18 14:13:53 +00006173 std::string NameWithSuffix = PropertyName->getName().str();
6174 NameWithSuffix += '_';
Douglas Gregor5d649882009-11-18 22:32:06 +00006175 for(; Class; Class = Class->getSuperClass()) {
Douglas Gregor331faa02011-04-18 14:13:53 +00006176 for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar;
6177 Ivar = Ivar->getNextIvar()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00006178 Results.AddResult(Result(Ivar, Results.getBasePriority(Ivar), nullptr),
6179 CurContext, nullptr, false);
6180
Douglas Gregor331faa02011-04-18 14:13:53 +00006181 // Determine whether we've seen an ivar with a name similar to the
6182 // property.
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006183 if ((PropertyName == Ivar->getIdentifier() ||
Douglas Gregor331faa02011-04-18 14:13:53 +00006184 NameWithPrefix == Ivar->getName() ||
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006185 NameWithSuffix == Ivar->getName())) {
Douglas Gregor331faa02011-04-18 14:13:53 +00006186 SawSimilarlyNamedIvar = true;
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006187
6188 // Reduce the priority of this result by one, to give it a slight
6189 // advantage over other results whose names don't match so closely.
6190 if (Results.size() &&
6191 Results.data()[Results.size() - 1].Kind
6192 == CodeCompletionResult::RK_Declaration &&
6193 Results.data()[Results.size() - 1].Declaration == Ivar)
6194 Results.data()[Results.size() - 1].Priority--;
6195 }
Douglas Gregor331faa02011-04-18 14:13:53 +00006196 }
Douglas Gregor5d649882009-11-18 22:32:06 +00006197 }
Douglas Gregor331faa02011-04-18 14:13:53 +00006198
6199 if (!SawSimilarlyNamedIvar) {
6200 // Create ivar result _propName, that the user can use to synthesize
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006201 // an ivar of the appropriate type.
6202 unsigned Priority = CCP_MemberDeclaration + 1;
Douglas Gregor331faa02011-04-18 14:13:53 +00006203 typedef CodeCompletionResult Result;
6204 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006205 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo(),
6206 Priority,CXAvailability_Available);
Douglas Gregor331faa02011-04-18 14:13:53 +00006207
Douglas Gregor75acd922011-09-27 23:30:47 +00006208 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregor6c7a9ee2011-04-18 14:40:46 +00006209 Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context,
Douglas Gregor75acd922011-09-27 23:30:47 +00006210 Policy, Allocator));
Douglas Gregor331faa02011-04-18 14:13:53 +00006211 Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix));
6212 Results.AddResult(Result(Builder.TakeString(), Priority,
6213 CXCursor_ObjCIvarDecl));
6214 }
6215
Douglas Gregor5d649882009-11-18 22:32:06 +00006216 Results.ExitScope();
6217
Douglas Gregor00c37ef2010-08-11 21:23:17 +00006218 HandleCodeCompleteResults(this, CodeCompleter,
6219 CodeCompletionContext::CCC_Other,
6220 Results.data(),Results.size());
Douglas Gregor5d649882009-11-18 22:32:06 +00006221}
Douglas Gregor636a61e2010-04-07 00:21:17 +00006222
Douglas Gregor416b5752010-08-25 01:08:01 +00006223// Mapping from selectors to the methods that implement that selector, along
6224// with the "in original class" flag.
Benjamin Kramereb8c4462013-06-29 17:52:13 +00006225typedef llvm::DenseMap<
6226 Selector, llvm::PointerIntPair<ObjCMethodDecl *, 1, bool> > KnownMethodsMap;
Douglas Gregor636a61e2010-04-07 00:21:17 +00006227
6228/// \brief Find all of the methods that reside in the given container
6229/// (and its superclasses, protocols, etc.) that meet the given
6230/// criteria. Insert those methods into the map of known methods,
6231/// indexed by selector so they can be easily found.
6232static void FindImplementableMethods(ASTContext &Context,
6233 ObjCContainerDecl *Container,
6234 bool WantInstanceMethods,
6235 QualType ReturnType,
Douglas Gregor416b5752010-08-25 01:08:01 +00006236 KnownMethodsMap &KnownMethods,
6237 bool InOriginalClass = true) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00006238 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)) {
Douglas Gregor9b4f3702012-06-12 13:44:08 +00006239 // Make sure we have a definition; that's what we'll walk.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00006240 if (!IFace->hasDefinition())
6241 return;
Douglas Gregor9b4f3702012-06-12 13:44:08 +00006242
6243 IFace = IFace->getDefinition();
6244 Container = IFace;
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00006245
Douglas Gregor636a61e2010-04-07 00:21:17 +00006246 const ObjCList<ObjCProtocolDecl> &Protocols
6247 = IFace->getReferencedProtocols();
6248 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006249 E = Protocols.end();
Douglas Gregor636a61e2010-04-07 00:21:17 +00006250 I != E; ++I)
6251 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006252 KnownMethods, InOriginalClass);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006253
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006254 // Add methods from any class extensions and categories.
Aaron Ballman3fe486a2014-03-13 21:23:55 +00006255 for (auto *Cat : IFace->visible_categories()) {
6256 FindImplementableMethods(Context, Cat, WantInstanceMethods, ReturnType,
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006257 KnownMethods, false);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00006258 }
6259
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006260 // Visit the superclass.
6261 if (IFace->getSuperClass())
6262 FindImplementableMethods(Context, IFace->getSuperClass(),
6263 WantInstanceMethods, ReturnType,
6264 KnownMethods, false);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006265 }
6266
6267 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
6268 // Recurse into protocols.
6269 const ObjCList<ObjCProtocolDecl> &Protocols
6270 = Category->getReferencedProtocols();
6271 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006272 E = Protocols.end();
Douglas Gregor636a61e2010-04-07 00:21:17 +00006273 I != E; ++I)
6274 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006275 KnownMethods, InOriginalClass);
6276
6277 // If this category is the original class, jump to the interface.
6278 if (InOriginalClass && Category->getClassInterface())
6279 FindImplementableMethods(Context, Category->getClassInterface(),
6280 WantInstanceMethods, ReturnType, KnownMethods,
6281 false);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006282 }
6283
6284 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
Douglas Gregor9b4f3702012-06-12 13:44:08 +00006285 // Make sure we have a definition; that's what we'll walk.
6286 if (!Protocol->hasDefinition())
6287 return;
6288 Protocol = Protocol->getDefinition();
6289 Container = Protocol;
6290
6291 // Recurse into protocols.
6292 const ObjCList<ObjCProtocolDecl> &Protocols
6293 = Protocol->getReferencedProtocols();
6294 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
6295 E = Protocols.end();
6296 I != E; ++I)
6297 FindImplementableMethods(Context, *I, WantInstanceMethods, ReturnType,
6298 KnownMethods, false);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006299 }
6300
6301 // Add methods in this container. This operation occurs last because
6302 // we want the methods from this container to override any methods
6303 // we've previously seen with the same selector.
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006304 for (auto *M : Container->methods()) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00006305 if (M->isInstanceMethod() == WantInstanceMethods) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00006306 if (!ReturnType.isNull() &&
Alp Toker314cc812014-01-25 16:55:45 +00006307 !Context.hasSameUnqualifiedType(ReturnType, M->getReturnType()))
Douglas Gregor636a61e2010-04-07 00:21:17 +00006308 continue;
6309
Benjamin Kramereb8c4462013-06-29 17:52:13 +00006310 KnownMethods[M->getSelector()] =
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006311 KnownMethodsMap::mapped_type(M, InOriginalClass);
Douglas Gregor636a61e2010-04-07 00:21:17 +00006312 }
6313 }
6314}
6315
Douglas Gregor669a25a2011-02-17 00:22:45 +00006316/// \brief Add the parenthesized return or parameter type chunk to a code
6317/// completion string.
6318static void AddObjCPassingTypeChunk(QualType Type,
Douglas Gregor29979142012-04-10 18:35:07 +00006319 unsigned ObjCDeclQuals,
Douglas Gregor669a25a2011-02-17 00:22:45 +00006320 ASTContext &Context,
Douglas Gregor75acd922011-09-27 23:30:47 +00006321 const PrintingPolicy &Policy,
Douglas Gregor669a25a2011-02-17 00:22:45 +00006322 CodeCompletionBuilder &Builder) {
6323 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
Douglas Gregor86b42682015-06-19 18:27:52 +00006324 std::string Quals = formatObjCParamQualifiers(ObjCDeclQuals, Type);
Douglas Gregor29979142012-04-10 18:35:07 +00006325 if (!Quals.empty())
6326 Builder.AddTextChunk(Builder.getAllocator().CopyString(Quals));
Douglas Gregor75acd922011-09-27 23:30:47 +00006327 Builder.AddTextChunk(GetCompletionTypeString(Type, Context, Policy,
Douglas Gregor669a25a2011-02-17 00:22:45 +00006328 Builder.getAllocator()));
6329 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6330}
6331
6332/// \brief Determine whether the given class is or inherits from a class by
6333/// the given name.
6334static bool InheritsFromClassNamed(ObjCInterfaceDecl *Class,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006335 StringRef Name) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006336 if (!Class)
6337 return false;
6338
6339 if (Class->getIdentifier() && Class->getIdentifier()->getName() == Name)
6340 return true;
6341
6342 return InheritsFromClassNamed(Class->getSuperClass(), Name);
6343}
6344
6345/// \brief Add code completions for Objective-C Key-Value Coding (KVC) and
6346/// Key-Value Observing (KVO).
6347static void AddObjCKeyValueCompletions(ObjCPropertyDecl *Property,
6348 bool IsInstanceMethod,
6349 QualType ReturnType,
6350 ASTContext &Context,
Douglas Gregord4a8ced2011-05-04 23:50:46 +00006351 VisitedSelectorSet &KnownSelectors,
Douglas Gregor669a25a2011-02-17 00:22:45 +00006352 ResultBuilder &Results) {
6353 IdentifierInfo *PropName = Property->getIdentifier();
6354 if (!PropName || PropName->getLength() == 0)
6355 return;
6356
Douglas Gregor75acd922011-09-27 23:30:47 +00006357 PrintingPolicy Policy = getCompletionPrintingPolicy(Results.getSema());
6358
Douglas Gregor669a25a2011-02-17 00:22:45 +00006359 // Builder that will create each code completion.
6360 typedef CodeCompletionResult Result;
6361 CodeCompletionAllocator &Allocator = Results.getAllocator();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00006362 CodeCompletionBuilder Builder(Allocator, Results.getCodeCompletionTUInfo());
Douglas Gregor669a25a2011-02-17 00:22:45 +00006363
6364 // The selector table.
6365 SelectorTable &Selectors = Context.Selectors;
6366
6367 // The property name, copied into the code completion allocation region
6368 // on demand.
6369 struct KeyHolder {
6370 CodeCompletionAllocator &Allocator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006371 StringRef Key;
Douglas Gregor669a25a2011-02-17 00:22:45 +00006372 const char *CopiedKey;
Craig Topperc3ec1492014-05-26 06:22:03 +00006373
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006374 KeyHolder(CodeCompletionAllocator &Allocator, StringRef Key)
Craig Topperc3ec1492014-05-26 06:22:03 +00006375 : Allocator(Allocator), Key(Key), CopiedKey(nullptr) {}
6376
Douglas Gregor669a25a2011-02-17 00:22:45 +00006377 operator const char *() {
6378 if (CopiedKey)
6379 return CopiedKey;
6380
6381 return CopiedKey = Allocator.CopyString(Key);
6382 }
6383 } Key(Allocator, PropName->getName());
6384
6385 // The uppercased name of the property name.
6386 std::string UpperKey = PropName->getName();
6387 if (!UpperKey.empty())
Jordan Rose4938f272013-02-09 10:09:43 +00006388 UpperKey[0] = toUppercase(UpperKey[0]);
Douglas Gregor669a25a2011-02-17 00:22:45 +00006389
6390 bool ReturnTypeMatchesProperty = ReturnType.isNull() ||
6391 Context.hasSameUnqualifiedType(ReturnType.getNonReferenceType(),
6392 Property->getType());
6393 bool ReturnTypeMatchesVoid
6394 = ReturnType.isNull() || ReturnType->isVoidType();
6395
6396 // Add the normal accessor -(type)key.
6397 if (IsInstanceMethod &&
David Blaikie82e95a32014-11-19 07:49:47 +00006398 KnownSelectors.insert(Selectors.getNullarySelector(PropName)).second &&
Douglas Gregor669a25a2011-02-17 00:22:45 +00006399 ReturnTypeMatchesProperty && !Property->getGetterMethodDecl()) {
6400 if (ReturnType.isNull())
Douglas Gregor29979142012-04-10 18:35:07 +00006401 AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6402 Context, Policy, Builder);
Douglas Gregor669a25a2011-02-17 00:22:45 +00006403
6404 Builder.AddTypedTextChunk(Key);
6405 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6406 CXCursor_ObjCInstanceMethodDecl));
6407 }
6408
6409 // If we have an integral or boolean property (or the user has provided
6410 // an integral or boolean return type), add the accessor -(type)isKey.
6411 if (IsInstanceMethod &&
6412 ((!ReturnType.isNull() &&
6413 (ReturnType->isIntegerType() || ReturnType->isBooleanType())) ||
6414 (ReturnType.isNull() &&
6415 (Property->getType()->isIntegerType() ||
6416 Property->getType()->isBooleanType())))) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006417 std::string SelectorName = (Twine("is") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006418 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006419 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6420 .second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006421 if (ReturnType.isNull()) {
6422 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6423 Builder.AddTextChunk("BOOL");
6424 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6425 }
6426
6427 Builder.AddTypedTextChunk(
6428 Allocator.CopyString(SelectorId->getName()));
6429 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6430 CXCursor_ObjCInstanceMethodDecl));
6431 }
6432 }
6433
6434 // Add the normal mutator.
6435 if (IsInstanceMethod && ReturnTypeMatchesVoid &&
6436 !Property->getSetterMethodDecl()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006437 std::string SelectorName = (Twine("set") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006438 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006439 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006440 if (ReturnType.isNull()) {
6441 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6442 Builder.AddTextChunk("void");
6443 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6444 }
6445
6446 Builder.AddTypedTextChunk(
6447 Allocator.CopyString(SelectorId->getName()));
6448 Builder.AddTypedTextChunk(":");
Douglas Gregor29979142012-04-10 18:35:07 +00006449 AddObjCPassingTypeChunk(Property->getType(), /*Quals=*/0,
6450 Context, Policy, Builder);
Douglas Gregor669a25a2011-02-17 00:22:45 +00006451 Builder.AddTextChunk(Key);
6452 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6453 CXCursor_ObjCInstanceMethodDecl));
6454 }
6455 }
6456
6457 // Indexed and unordered accessors
6458 unsigned IndexedGetterPriority = CCP_CodePattern;
6459 unsigned IndexedSetterPriority = CCP_CodePattern;
6460 unsigned UnorderedGetterPriority = CCP_CodePattern;
6461 unsigned UnorderedSetterPriority = CCP_CodePattern;
6462 if (const ObjCObjectPointerType *ObjCPointer
6463 = Property->getType()->getAs<ObjCObjectPointerType>()) {
6464 if (ObjCInterfaceDecl *IFace = ObjCPointer->getInterfaceDecl()) {
6465 // If this interface type is not provably derived from a known
6466 // collection, penalize the corresponding completions.
6467 if (!InheritsFromClassNamed(IFace, "NSMutableArray")) {
6468 IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6469 if (!InheritsFromClassNamed(IFace, "NSArray"))
6470 IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6471 }
6472
6473 if (!InheritsFromClassNamed(IFace, "NSMutableSet")) {
6474 UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6475 if (!InheritsFromClassNamed(IFace, "NSSet"))
6476 UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6477 }
6478 }
6479 } else {
6480 IndexedGetterPriority += CCD_ProbablyNotObjCCollection;
6481 IndexedSetterPriority += CCD_ProbablyNotObjCCollection;
6482 UnorderedGetterPriority += CCD_ProbablyNotObjCCollection;
6483 UnorderedSetterPriority += CCD_ProbablyNotObjCCollection;
6484 }
6485
6486 // Add -(NSUInteger)countOf<key>
6487 if (IsInstanceMethod &&
6488 (ReturnType.isNull() || ReturnType->isIntegerType())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006489 std::string SelectorName = (Twine("countOf") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006490 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006491 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6492 .second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006493 if (ReturnType.isNull()) {
6494 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6495 Builder.AddTextChunk("NSUInteger");
6496 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6497 }
6498
6499 Builder.AddTypedTextChunk(
6500 Allocator.CopyString(SelectorId->getName()));
6501 Results.AddResult(Result(Builder.TakeString(),
6502 std::min(IndexedGetterPriority,
6503 UnorderedGetterPriority),
6504 CXCursor_ObjCInstanceMethodDecl));
6505 }
6506 }
6507
6508 // Indexed getters
6509 // Add -(id)objectInKeyAtIndex:(NSUInteger)index
6510 if (IsInstanceMethod &&
6511 (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006512 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006513 = (Twine("objectIn") + UpperKey + "AtIndex").str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006514 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006515 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006516 if (ReturnType.isNull()) {
6517 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6518 Builder.AddTextChunk("id");
6519 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6520 }
6521
6522 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6523 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6524 Builder.AddTextChunk("NSUInteger");
6525 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6526 Builder.AddTextChunk("index");
6527 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6528 CXCursor_ObjCInstanceMethodDecl));
6529 }
6530 }
6531
6532 // Add -(NSArray *)keyAtIndexes:(NSIndexSet *)indexes
6533 if (IsInstanceMethod &&
6534 (ReturnType.isNull() ||
6535 (ReturnType->isObjCObjectPointerType() &&
6536 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6537 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6538 ->getName() == "NSArray"))) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006539 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006540 = (Twine(Property->getName()) + "AtIndexes").str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006541 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006542 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006543 if (ReturnType.isNull()) {
6544 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6545 Builder.AddTextChunk("NSArray *");
6546 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6547 }
6548
6549 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6550 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6551 Builder.AddTextChunk("NSIndexSet *");
6552 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6553 Builder.AddTextChunk("indexes");
6554 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6555 CXCursor_ObjCInstanceMethodDecl));
6556 }
6557 }
6558
6559 // Add -(void)getKey:(type **)buffer range:(NSRange)inRange
6560 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006561 std::string SelectorName = (Twine("get") + UpperKey).str();
Douglas Gregor669a25a2011-02-17 00:22:45 +00006562 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006563 &Context.Idents.get(SelectorName),
Douglas Gregor669a25a2011-02-17 00:22:45 +00006564 &Context.Idents.get("range")
6565 };
6566
David Blaikie82e95a32014-11-19 07:49:47 +00006567 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006568 if (ReturnType.isNull()) {
6569 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6570 Builder.AddTextChunk("void");
6571 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6572 }
6573
6574 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6575 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6576 Builder.AddPlaceholderChunk("object-type");
6577 Builder.AddTextChunk(" **");
6578 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6579 Builder.AddTextChunk("buffer");
6580 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6581 Builder.AddTypedTextChunk("range:");
6582 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6583 Builder.AddTextChunk("NSRange");
6584 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6585 Builder.AddTextChunk("inRange");
6586 Results.AddResult(Result(Builder.TakeString(), IndexedGetterPriority,
6587 CXCursor_ObjCInstanceMethodDecl));
6588 }
6589 }
6590
6591 // Mutable indexed accessors
6592
6593 // - (void)insertObject:(type *)object inKeyAtIndex:(NSUInteger)index
6594 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006595 std::string SelectorName = (Twine("in") + UpperKey + "AtIndex").str();
Douglas Gregor669a25a2011-02-17 00:22:45 +00006596 IdentifierInfo *SelectorIds[2] = {
6597 &Context.Idents.get("insertObject"),
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006598 &Context.Idents.get(SelectorName)
Douglas Gregor669a25a2011-02-17 00:22:45 +00006599 };
6600
David Blaikie82e95a32014-11-19 07:49:47 +00006601 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006602 if (ReturnType.isNull()) {
6603 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6604 Builder.AddTextChunk("void");
6605 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6606 }
6607
6608 Builder.AddTypedTextChunk("insertObject:");
6609 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6610 Builder.AddPlaceholderChunk("object-type");
6611 Builder.AddTextChunk(" *");
6612 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6613 Builder.AddTextChunk("object");
6614 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6615 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6616 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6617 Builder.AddPlaceholderChunk("NSUInteger");
6618 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6619 Builder.AddTextChunk("index");
6620 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6621 CXCursor_ObjCInstanceMethodDecl));
6622 }
6623 }
6624
6625 // - (void)insertKey:(NSArray *)array atIndexes:(NSIndexSet *)indexes
6626 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006627 std::string SelectorName = (Twine("insert") + UpperKey).str();
Douglas Gregor669a25a2011-02-17 00:22:45 +00006628 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006629 &Context.Idents.get(SelectorName),
Douglas Gregor669a25a2011-02-17 00:22:45 +00006630 &Context.Idents.get("atIndexes")
6631 };
6632
David Blaikie82e95a32014-11-19 07:49:47 +00006633 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006634 if (ReturnType.isNull()) {
6635 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6636 Builder.AddTextChunk("void");
6637 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6638 }
6639
6640 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6641 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6642 Builder.AddTextChunk("NSArray *");
6643 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6644 Builder.AddTextChunk("array");
6645 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6646 Builder.AddTypedTextChunk("atIndexes:");
6647 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6648 Builder.AddPlaceholderChunk("NSIndexSet *");
6649 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6650 Builder.AddTextChunk("indexes");
6651 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6652 CXCursor_ObjCInstanceMethodDecl));
6653 }
6654 }
6655
6656 // -(void)removeObjectFromKeyAtIndex:(NSUInteger)index
6657 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006658 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006659 = (Twine("removeObjectFrom") + UpperKey + "AtIndex").str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006660 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006661 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006662 if (ReturnType.isNull()) {
6663 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6664 Builder.AddTextChunk("void");
6665 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6666 }
6667
6668 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6669 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6670 Builder.AddTextChunk("NSUInteger");
6671 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6672 Builder.AddTextChunk("index");
6673 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6674 CXCursor_ObjCInstanceMethodDecl));
6675 }
6676 }
6677
6678 // -(void)removeKeyAtIndexes:(NSIndexSet *)indexes
6679 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006680 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006681 = (Twine("remove") + UpperKey + "AtIndexes").str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006682 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006683 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006684 if (ReturnType.isNull()) {
6685 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6686 Builder.AddTextChunk("void");
6687 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6688 }
6689
6690 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6691 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6692 Builder.AddTextChunk("NSIndexSet *");
6693 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6694 Builder.AddTextChunk("indexes");
6695 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6696 CXCursor_ObjCInstanceMethodDecl));
6697 }
6698 }
6699
6700 // - (void)replaceObjectInKeyAtIndex:(NSUInteger)index withObject:(id)object
6701 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006702 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006703 = (Twine("replaceObjectIn") + UpperKey + "AtIndex").str();
Douglas Gregor669a25a2011-02-17 00:22:45 +00006704 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006705 &Context.Idents.get(SelectorName),
Douglas Gregor669a25a2011-02-17 00:22:45 +00006706 &Context.Idents.get("withObject")
6707 };
6708
David Blaikie82e95a32014-11-19 07:49:47 +00006709 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006710 if (ReturnType.isNull()) {
6711 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6712 Builder.AddTextChunk("void");
6713 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6714 }
6715
6716 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6717 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6718 Builder.AddPlaceholderChunk("NSUInteger");
6719 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6720 Builder.AddTextChunk("index");
6721 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6722 Builder.AddTypedTextChunk("withObject:");
6723 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6724 Builder.AddTextChunk("id");
6725 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6726 Builder.AddTextChunk("object");
6727 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6728 CXCursor_ObjCInstanceMethodDecl));
6729 }
6730 }
6731
6732 // - (void)replaceKeyAtIndexes:(NSIndexSet *)indexes withKey:(NSArray *)array
6733 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006734 std::string SelectorName1
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006735 = (Twine("replace") + UpperKey + "AtIndexes").str();
6736 std::string SelectorName2 = (Twine("with") + UpperKey).str();
Douglas Gregor669a25a2011-02-17 00:22:45 +00006737 IdentifierInfo *SelectorIds[2] = {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006738 &Context.Idents.get(SelectorName1),
6739 &Context.Idents.get(SelectorName2)
Douglas Gregor669a25a2011-02-17 00:22:45 +00006740 };
6741
David Blaikie82e95a32014-11-19 07:49:47 +00006742 if (KnownSelectors.insert(Selectors.getSelector(2, SelectorIds)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006743 if (ReturnType.isNull()) {
6744 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6745 Builder.AddTextChunk("void");
6746 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6747 }
6748
6749 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName1 + ":"));
6750 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6751 Builder.AddPlaceholderChunk("NSIndexSet *");
6752 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6753 Builder.AddTextChunk("indexes");
6754 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
6755 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName2 + ":"));
6756 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6757 Builder.AddTextChunk("NSArray *");
6758 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6759 Builder.AddTextChunk("array");
6760 Results.AddResult(Result(Builder.TakeString(), IndexedSetterPriority,
6761 CXCursor_ObjCInstanceMethodDecl));
6762 }
6763 }
6764
6765 // Unordered getters
6766 // - (NSEnumerator *)enumeratorOfKey
6767 if (IsInstanceMethod &&
6768 (ReturnType.isNull() ||
6769 (ReturnType->isObjCObjectPointerType() &&
6770 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6771 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6772 ->getName() == "NSEnumerator"))) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006773 std::string SelectorName = (Twine("enumeratorOf") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006774 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006775 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6776 .second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006777 if (ReturnType.isNull()) {
6778 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6779 Builder.AddTextChunk("NSEnumerator *");
6780 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6781 }
6782
6783 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6784 Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6785 CXCursor_ObjCInstanceMethodDecl));
6786 }
6787 }
6788
6789 // - (type *)memberOfKey:(type *)object
6790 if (IsInstanceMethod &&
6791 (ReturnType.isNull() || ReturnType->isObjCObjectPointerType())) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006792 std::string SelectorName = (Twine("memberOf") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006793 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006794 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006795 if (ReturnType.isNull()) {
6796 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6797 Builder.AddPlaceholderChunk("object-type");
6798 Builder.AddTextChunk(" *");
6799 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6800 }
6801
6802 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6803 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6804 if (ReturnType.isNull()) {
6805 Builder.AddPlaceholderChunk("object-type");
6806 Builder.AddTextChunk(" *");
6807 } else {
6808 Builder.AddTextChunk(GetCompletionTypeString(ReturnType, Context,
Douglas Gregor75acd922011-09-27 23:30:47 +00006809 Policy,
Douglas Gregor669a25a2011-02-17 00:22:45 +00006810 Builder.getAllocator()));
6811 }
6812 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6813 Builder.AddTextChunk("object");
6814 Results.AddResult(Result(Builder.TakeString(), UnorderedGetterPriority,
6815 CXCursor_ObjCInstanceMethodDecl));
6816 }
6817 }
6818
6819 // Mutable unordered accessors
6820 // - (void)addKeyObject:(type *)object
6821 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006822 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006823 = (Twine("add") + UpperKey + Twine("Object")).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006824 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006825 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006826 if (ReturnType.isNull()) {
6827 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6828 Builder.AddTextChunk("void");
6829 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6830 }
6831
6832 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6833 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6834 Builder.AddPlaceholderChunk("object-type");
6835 Builder.AddTextChunk(" *");
6836 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6837 Builder.AddTextChunk("object");
6838 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6839 CXCursor_ObjCInstanceMethodDecl));
6840 }
6841 }
6842
6843 // - (void)addKey:(NSSet *)objects
6844 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006845 std::string SelectorName = (Twine("add") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006846 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006847 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006848 if (ReturnType.isNull()) {
6849 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6850 Builder.AddTextChunk("void");
6851 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6852 }
6853
6854 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6855 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6856 Builder.AddTextChunk("NSSet *");
6857 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6858 Builder.AddTextChunk("objects");
6859 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6860 CXCursor_ObjCInstanceMethodDecl));
6861 }
6862 }
6863
6864 // - (void)removeKeyObject:(type *)object
6865 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006866 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006867 = (Twine("remove") + UpperKey + Twine("Object")).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006868 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006869 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006870 if (ReturnType.isNull()) {
6871 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6872 Builder.AddTextChunk("void");
6873 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6874 }
6875
6876 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6877 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6878 Builder.AddPlaceholderChunk("object-type");
6879 Builder.AddTextChunk(" *");
6880 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6881 Builder.AddTextChunk("object");
6882 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6883 CXCursor_ObjCInstanceMethodDecl));
6884 }
6885 }
6886
6887 // - (void)removeKey:(NSSet *)objects
6888 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006889 std::string SelectorName = (Twine("remove") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006890 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006891 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006892 if (ReturnType.isNull()) {
6893 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6894 Builder.AddTextChunk("void");
6895 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6896 }
6897
6898 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6899 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6900 Builder.AddTextChunk("NSSet *");
6901 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6902 Builder.AddTextChunk("objects");
6903 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6904 CXCursor_ObjCInstanceMethodDecl));
6905 }
6906 }
6907
6908 // - (void)intersectKey:(NSSet *)objects
6909 if (IsInstanceMethod && ReturnTypeMatchesVoid) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006910 std::string SelectorName = (Twine("intersect") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006911 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006912 if (KnownSelectors.insert(Selectors.getUnarySelector(SelectorId)).second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006913 if (ReturnType.isNull()) {
6914 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6915 Builder.AddTextChunk("void");
6916 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6917 }
6918
6919 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName + ":"));
6920 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6921 Builder.AddTextChunk("NSSet *");
6922 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6923 Builder.AddTextChunk("objects");
6924 Results.AddResult(Result(Builder.TakeString(), UnorderedSetterPriority,
6925 CXCursor_ObjCInstanceMethodDecl));
6926 }
6927 }
6928
6929 // Key-Value Observing
6930 // + (NSSet *)keyPathsForValuesAffectingKey
6931 if (!IsInstanceMethod &&
6932 (ReturnType.isNull() ||
6933 (ReturnType->isObjCObjectPointerType() &&
6934 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl() &&
6935 ReturnType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()
6936 ->getName() == "NSSet"))) {
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006937 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006938 = (Twine("keyPathsForValuesAffecting") + UpperKey).str();
Douglas Gregor0e5d72f2011-02-17 03:19:26 +00006939 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006940 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6941 .second) {
Douglas Gregor669a25a2011-02-17 00:22:45 +00006942 if (ReturnType.isNull()) {
6943 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6944 Builder.AddTextChunk("NSSet *");
6945 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6946 }
6947
6948 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6949 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
Douglas Gregor857bcda2011-06-02 04:02:27 +00006950 CXCursor_ObjCClassMethodDecl));
6951 }
6952 }
6953
6954 // + (BOOL)automaticallyNotifiesObserversForKey
6955 if (!IsInstanceMethod &&
6956 (ReturnType.isNull() ||
6957 ReturnType->isIntegerType() ||
6958 ReturnType->isBooleanType())) {
6959 std::string SelectorName
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006960 = (Twine("automaticallyNotifiesObserversOf") + UpperKey).str();
Douglas Gregor857bcda2011-06-02 04:02:27 +00006961 IdentifierInfo *SelectorId = &Context.Idents.get(SelectorName);
David Blaikie82e95a32014-11-19 07:49:47 +00006962 if (KnownSelectors.insert(Selectors.getNullarySelector(SelectorId))
6963 .second) {
Douglas Gregor857bcda2011-06-02 04:02:27 +00006964 if (ReturnType.isNull()) {
6965 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
6966 Builder.AddTextChunk("BOOL");
6967 Builder.AddChunk(CodeCompletionString::CK_RightParen);
6968 }
6969
6970 Builder.AddTypedTextChunk(Allocator.CopyString(SelectorName));
6971 Results.AddResult(Result(Builder.TakeString(), CCP_CodePattern,
6972 CXCursor_ObjCClassMethodDecl));
Douglas Gregor669a25a2011-02-17 00:22:45 +00006973 }
6974 }
6975}
6976
Douglas Gregor636a61e2010-04-07 00:21:17 +00006977void Sema::CodeCompleteObjCMethodDecl(Scope *S,
6978 bool IsInstanceMethod,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006979 ParsedType ReturnTy) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00006980 // Determine the return type of the method we're declaring, if
6981 // provided.
6982 QualType ReturnType = GetTypeFromParser(ReturnTy);
Craig Topperc3ec1492014-05-26 06:22:03 +00006983 Decl *IDecl = nullptr;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00006984 if (CurContext->isObjCContainer()) {
6985 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
6986 IDecl = cast<Decl>(OCD);
6987 }
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006988 // Determine where we should start searching for methods.
Craig Topperc3ec1492014-05-26 06:22:03 +00006989 ObjCContainerDecl *SearchDecl = nullptr;
Douglas Gregor636a61e2010-04-07 00:21:17 +00006990 bool IsInImplementation = false;
John McCall48871652010-08-21 09:40:31 +00006991 if (Decl *D = IDecl) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00006992 if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) {
6993 SearchDecl = Impl->getClassInterface();
Douglas Gregor636a61e2010-04-07 00:21:17 +00006994 IsInImplementation = true;
6995 } else if (ObjCCategoryImplDecl *CatImpl
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006996 = dyn_cast<ObjCCategoryImplDecl>(D)) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00006997 SearchDecl = CatImpl->getCategoryDecl();
Douglas Gregor636a61e2010-04-07 00:21:17 +00006998 IsInImplementation = true;
Douglas Gregor1b035bb2010-10-18 18:21:28 +00006999 } else
Douglas Gregor636a61e2010-04-07 00:21:17 +00007000 SearchDecl = dyn_cast<ObjCContainerDecl>(D);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007001 }
7002
7003 if (!SearchDecl && S) {
Ted Kremenekc37877d2013-10-08 17:08:03 +00007004 if (DeclContext *DC = S->getEntity())
Douglas Gregor636a61e2010-04-07 00:21:17 +00007005 SearchDecl = dyn_cast<ObjCContainerDecl>(DC);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007006 }
7007
Douglas Gregor1b035bb2010-10-18 18:21:28 +00007008 if (!SearchDecl) {
Douglas Gregor00c37ef2010-08-11 21:23:17 +00007009 HandleCodeCompleteResults(this, CodeCompleter,
7010 CodeCompletionContext::CCC_Other,
Craig Topperc3ec1492014-05-26 06:22:03 +00007011 nullptr, 0);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007012 return;
7013 }
7014
7015 // Find all of the methods that we could declare/implement here.
7016 KnownMethodsMap KnownMethods;
7017 FindImplementableMethods(Context, SearchDecl, IsInstanceMethod,
Douglas Gregor1b035bb2010-10-18 18:21:28 +00007018 ReturnType, KnownMethods);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007019
Douglas Gregor636a61e2010-04-07 00:21:17 +00007020 // Add declarations or definitions for each of the known methods.
John McCall276321a2010-08-25 06:19:51 +00007021 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007022 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007023 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007024 CodeCompletionContext::CCC_Other);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007025 Results.EnterNewScope();
Douglas Gregor75acd922011-09-27 23:30:47 +00007026 PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007027 for (KnownMethodsMap::iterator M = KnownMethods.begin(),
7028 MEnd = KnownMethods.end();
7029 M != MEnd; ++M) {
Benjamin Kramereb8c4462013-06-29 17:52:13 +00007030 ObjCMethodDecl *Method = M->second.getPointer();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007031 CodeCompletionBuilder Builder(Results.getAllocator(),
7032 Results.getCodeCompletionTUInfo());
Douglas Gregor636a61e2010-04-07 00:21:17 +00007033
7034 // If the result type was not already provided, add it to the
7035 // pattern as (type).
Douglas Gregor669a25a2011-02-17 00:22:45 +00007036 if (ReturnType.isNull())
Alp Toker314cc812014-01-25 16:55:45 +00007037 AddObjCPassingTypeChunk(Method->getReturnType(),
7038 Method->getObjCDeclQualifier(), Context, Policy,
7039 Builder);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007040
7041 Selector Sel = Method->getSelector();
7042
7043 // Add the first part of the selector to the pattern.
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00007044 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00007045 Sel.getNameForSlot(0)));
Douglas Gregor636a61e2010-04-07 00:21:17 +00007046
7047 // Add parameters to the pattern.
7048 unsigned I = 0;
7049 for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
7050 PEnd = Method->param_end();
7051 P != PEnd; (void)++P, ++I) {
7052 // Add the part of the selector name.
7053 if (I == 0)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007054 Builder.AddTypedTextChunk(":");
Douglas Gregor636a61e2010-04-07 00:21:17 +00007055 else if (I < Sel.getNumArgs()) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007056 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7057 Builder.AddTypedTextChunk(
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +00007058 Builder.getAllocator().CopyString(Sel.getNameForSlot(I) + ":"));
Douglas Gregor636a61e2010-04-07 00:21:17 +00007059 } else
7060 break;
7061
7062 // Add the parameter type.
Douglas Gregor86b42682015-06-19 18:27:52 +00007063 QualType ParamType;
7064 if ((*P)->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability)
7065 ParamType = (*P)->getType();
7066 else
7067 ParamType = (*P)->getOriginalType();
7068 AddObjCPassingTypeChunk(ParamType,
Douglas Gregor29979142012-04-10 18:35:07 +00007069 (*P)->getObjCDeclQualifier(),
7070 Context, Policy,
Douglas Gregor75acd922011-09-27 23:30:47 +00007071 Builder);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007072
7073 if (IdentifierInfo *Id = (*P)->getIdentifier())
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00007074 Builder.AddTextChunk(Builder.getAllocator().CopyString( Id->getName()));
Douglas Gregor636a61e2010-04-07 00:21:17 +00007075 }
7076
7077 if (Method->isVariadic()) {
7078 if (Method->param_size() > 0)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007079 Builder.AddChunk(CodeCompletionString::CK_Comma);
7080 Builder.AddTextChunk("...");
Douglas Gregor400f5972010-08-31 05:13:43 +00007081 }
Douglas Gregor636a61e2010-04-07 00:21:17 +00007082
Douglas Gregord37c59d2010-05-28 00:57:46 +00007083 if (IsInImplementation && Results.includeCodePatterns()) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00007084 // We will be defining the method here, so add a compound statement.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007085 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7086 Builder.AddChunk(CodeCompletionString::CK_LeftBrace);
7087 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
Alp Toker314cc812014-01-25 16:55:45 +00007088 if (!Method->getReturnType()->isVoidType()) {
Douglas Gregor636a61e2010-04-07 00:21:17 +00007089 // If the result type is not void, add a return clause.
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007090 Builder.AddTextChunk("return");
7091 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7092 Builder.AddPlaceholderChunk("expression");
7093 Builder.AddChunk(CodeCompletionString::CK_SemiColon);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007094 } else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007095 Builder.AddPlaceholderChunk("statements");
Douglas Gregor636a61e2010-04-07 00:21:17 +00007096
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007097 Builder.AddChunk(CodeCompletionString::CK_VerticalSpace);
7098 Builder.AddChunk(CodeCompletionString::CK_RightBrace);
Douglas Gregor636a61e2010-04-07 00:21:17 +00007099 }
7100
Douglas Gregor416b5752010-08-25 01:08:01 +00007101 unsigned Priority = CCP_CodePattern;
Benjamin Kramereb8c4462013-06-29 17:52:13 +00007102 if (!M->second.getInt())
Douglas Gregor416b5752010-08-25 01:08:01 +00007103 Priority += CCD_InBaseClass;
7104
Douglas Gregor78254c82012-03-27 23:34:16 +00007105 Results.AddResult(Result(Builder.TakeString(), Method, Priority));
Douglas Gregor636a61e2010-04-07 00:21:17 +00007106 }
7107
Douglas Gregor669a25a2011-02-17 00:22:45 +00007108 // Add Key-Value-Coding and Key-Value-Observing accessor methods for all of
7109 // the properties in this class and its categories.
David Blaikiebbafb8a2012-03-11 07:00:24 +00007110 if (Context.getLangOpts().ObjC2) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007111 SmallVector<ObjCContainerDecl *, 4> Containers;
Douglas Gregor669a25a2011-02-17 00:22:45 +00007112 Containers.push_back(SearchDecl);
7113
Douglas Gregord4a8ced2011-05-04 23:50:46 +00007114 VisitedSelectorSet KnownSelectors;
7115 for (KnownMethodsMap::iterator M = KnownMethods.begin(),
7116 MEnd = KnownMethods.end();
7117 M != MEnd; ++M)
7118 KnownSelectors.insert(M->first);
7119
7120
Douglas Gregor669a25a2011-02-17 00:22:45 +00007121 ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(SearchDecl);
7122 if (!IFace)
7123 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(SearchDecl))
7124 IFace = Category->getClassInterface();
7125
Aaron Ballman3fe486a2014-03-13 21:23:55 +00007126 if (IFace)
7127 for (auto *Cat : IFace->visible_categories())
7128 Containers.push_back(Cat);
Douglas Gregor669a25a2011-02-17 00:22:45 +00007129
Aaron Ballmandc4bea42014-03-13 18:47:37 +00007130 for (unsigned I = 0, N = Containers.size(); I != N; ++I)
Aaron Ballmand174edf2014-03-13 19:11:50 +00007131 for (auto *P : Containers[I]->properties())
Aaron Ballmandc4bea42014-03-13 18:47:37 +00007132 AddObjCKeyValueCompletions(P, IsInstanceMethod, ReturnType, Context,
Douglas Gregord4a8ced2011-05-04 23:50:46 +00007133 KnownSelectors, Results);
Douglas Gregor669a25a2011-02-17 00:22:45 +00007134 }
7135
Douglas Gregor636a61e2010-04-07 00:21:17 +00007136 Results.ExitScope();
7137
Douglas Gregor00c37ef2010-08-11 21:23:17 +00007138 HandleCodeCompleteResults(this, CodeCompleter,
7139 CodeCompletionContext::CCC_Other,
7140 Results.data(),Results.size());
Douglas Gregor636a61e2010-04-07 00:21:17 +00007141}
Douglas Gregor95887f92010-07-08 23:20:03 +00007142
7143void Sema::CodeCompleteObjCMethodDeclSelector(Scope *S,
7144 bool IsInstanceMethod,
Douglas Gregor45879692010-07-08 23:37:41 +00007145 bool AtParameterName,
John McCallba7bf592010-08-24 05:47:05 +00007146 ParsedType ReturnTy,
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00007147 ArrayRef<IdentifierInfo *> SelIdents) {
Douglas Gregor95887f92010-07-08 23:20:03 +00007148 // If we have an external source, load the entire class method
Sebastian Redld44cd6a2010-08-18 23:57:06 +00007149 // pool from the AST file.
Douglas Gregor95887f92010-07-08 23:20:03 +00007150 if (ExternalSource) {
7151 for (uint32_t I = 0, N = ExternalSource->GetNumExternalSelectors();
7152 I != N; ++I) {
7153 Selector Sel = ExternalSource->GetExternalSelector(I);
Sebastian Redl75d8a322010-08-02 23:18:59 +00007154 if (Sel.isNull() || MethodPool.count(Sel))
Douglas Gregor95887f92010-07-08 23:20:03 +00007155 continue;
Sebastian Redl75d8a322010-08-02 23:18:59 +00007156
7157 ReadMethodPool(Sel);
Douglas Gregor95887f92010-07-08 23:20:03 +00007158 }
7159 }
7160
7161 // Build the set of methods we can see.
John McCall276321a2010-08-25 06:19:51 +00007162 typedef CodeCompletionResult Result;
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007163 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007164 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007165 CodeCompletionContext::CCC_Other);
Douglas Gregor95887f92010-07-08 23:20:03 +00007166
7167 if (ReturnTy)
7168 Results.setPreferredType(GetTypeFromParser(ReturnTy).getNonReferenceType());
Sebastian Redl75d8a322010-08-02 23:18:59 +00007169
Douglas Gregor95887f92010-07-08 23:20:03 +00007170 Results.EnterNewScope();
Sebastian Redl75d8a322010-08-02 23:18:59 +00007171 for (GlobalMethodPool::iterator M = MethodPool.begin(),
7172 MEnd = MethodPool.end();
7173 M != MEnd; ++M) {
7174 for (ObjCMethodList *MethList = IsInstanceMethod ? &M->second.first :
7175 &M->second.second;
Nico Weber2e0c8f72014-12-27 03:58:08 +00007176 MethList && MethList->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007177 MethList = MethList->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00007178 if (!isAcceptableObjCMethod(MethList->getMethod(), MK_Any, SelIdents))
Douglas Gregor95887f92010-07-08 23:20:03 +00007179 continue;
7180
Douglas Gregor45879692010-07-08 23:37:41 +00007181 if (AtParameterName) {
7182 // Suggest parameter names we've seen before.
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00007183 unsigned NumSelIdents = SelIdents.size();
Nico Weber2e0c8f72014-12-27 03:58:08 +00007184 if (NumSelIdents &&
7185 NumSelIdents <= MethList->getMethod()->param_size()) {
7186 ParmVarDecl *Param =
7187 MethList->getMethod()->parameters()[NumSelIdents - 1];
Douglas Gregor45879692010-07-08 23:37:41 +00007188 if (Param->getIdentifier()) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007189 CodeCompletionBuilder Builder(Results.getAllocator(),
7190 Results.getCodeCompletionTUInfo());
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00007191 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007192 Param->getIdentifier()->getName()));
7193 Results.AddResult(Builder.TakeString());
Douglas Gregor45879692010-07-08 23:37:41 +00007194 }
7195 }
7196
7197 continue;
7198 }
Craig Topperc3ec1492014-05-26 06:22:03 +00007199
Nico Weber2e0c8f72014-12-27 03:58:08 +00007200 Result R(MethList->getMethod(),
7201 Results.getBasePriority(MethList->getMethod()), nullptr);
Dmitri Gribenko070a10e2013-06-16 03:47:57 +00007202 R.StartParameter = SelIdents.size();
Douglas Gregor95887f92010-07-08 23:20:03 +00007203 R.AllParametersAreInformative = false;
7204 R.DeclaringEntity = true;
7205 Results.MaybeAddResult(R, CurContext);
7206 }
7207 }
7208
7209 Results.ExitScope();
Douglas Gregor00c37ef2010-08-11 21:23:17 +00007210 HandleCodeCompleteResults(this, CodeCompleter,
7211 CodeCompletionContext::CCC_Other,
7212 Results.data(),Results.size());
Douglas Gregor95887f92010-07-08 23:20:03 +00007213}
Douglas Gregorb14904c2010-08-13 22:48:40 +00007214
Douglas Gregorec00a262010-08-24 22:20:20 +00007215void Sema::CodeCompletePreprocessorDirective(bool InConditional) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007216 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007217 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00007218 CodeCompletionContext::CCC_PreprocessorDirective);
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007219 Results.EnterNewScope();
7220
7221 // #if <condition>
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007222 CodeCompletionBuilder Builder(Results.getAllocator(),
7223 Results.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007224 Builder.AddTypedTextChunk("if");
7225 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7226 Builder.AddPlaceholderChunk("condition");
7227 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007228
7229 // #ifdef <macro>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007230 Builder.AddTypedTextChunk("ifdef");
7231 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7232 Builder.AddPlaceholderChunk("macro");
7233 Results.AddResult(Builder.TakeString());
7234
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007235 // #ifndef <macro>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007236 Builder.AddTypedTextChunk("ifndef");
7237 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7238 Builder.AddPlaceholderChunk("macro");
7239 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007240
7241 if (InConditional) {
7242 // #elif <condition>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007243 Builder.AddTypedTextChunk("elif");
7244 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7245 Builder.AddPlaceholderChunk("condition");
7246 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007247
7248 // #else
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007249 Builder.AddTypedTextChunk("else");
7250 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007251
7252 // #endif
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007253 Builder.AddTypedTextChunk("endif");
7254 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007255 }
7256
7257 // #include "header"
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007258 Builder.AddTypedTextChunk("include");
7259 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7260 Builder.AddTextChunk("\"");
7261 Builder.AddPlaceholderChunk("header");
7262 Builder.AddTextChunk("\"");
7263 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007264
7265 // #include <header>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007266 Builder.AddTypedTextChunk("include");
7267 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7268 Builder.AddTextChunk("<");
7269 Builder.AddPlaceholderChunk("header");
7270 Builder.AddTextChunk(">");
7271 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007272
7273 // #define <macro>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007274 Builder.AddTypedTextChunk("define");
7275 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7276 Builder.AddPlaceholderChunk("macro");
7277 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007278
7279 // #define <macro>(<args>)
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007280 Builder.AddTypedTextChunk("define");
7281 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7282 Builder.AddPlaceholderChunk("macro");
7283 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7284 Builder.AddPlaceholderChunk("args");
7285 Builder.AddChunk(CodeCompletionString::CK_RightParen);
7286 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007287
7288 // #undef <macro>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007289 Builder.AddTypedTextChunk("undef");
7290 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7291 Builder.AddPlaceholderChunk("macro");
7292 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007293
7294 // #line <number>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007295 Builder.AddTypedTextChunk("line");
7296 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7297 Builder.AddPlaceholderChunk("number");
7298 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007299
7300 // #line <number> "filename"
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007301 Builder.AddTypedTextChunk("line");
7302 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7303 Builder.AddPlaceholderChunk("number");
7304 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7305 Builder.AddTextChunk("\"");
7306 Builder.AddPlaceholderChunk("filename");
7307 Builder.AddTextChunk("\"");
7308 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007309
7310 // #error <message>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007311 Builder.AddTypedTextChunk("error");
7312 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7313 Builder.AddPlaceholderChunk("message");
7314 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007315
7316 // #pragma <arguments>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007317 Builder.AddTypedTextChunk("pragma");
7318 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7319 Builder.AddPlaceholderChunk("arguments");
7320 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007321
David Blaikiebbafb8a2012-03-11 07:00:24 +00007322 if (getLangOpts().ObjC1) {
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007323 // #import "header"
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007324 Builder.AddTypedTextChunk("import");
7325 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7326 Builder.AddTextChunk("\"");
7327 Builder.AddPlaceholderChunk("header");
7328 Builder.AddTextChunk("\"");
7329 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007330
7331 // #import <header>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007332 Builder.AddTypedTextChunk("import");
7333 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7334 Builder.AddTextChunk("<");
7335 Builder.AddPlaceholderChunk("header");
7336 Builder.AddTextChunk(">");
7337 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007338 }
7339
7340 // #include_next "header"
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007341 Builder.AddTypedTextChunk("include_next");
7342 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7343 Builder.AddTextChunk("\"");
7344 Builder.AddPlaceholderChunk("header");
7345 Builder.AddTextChunk("\"");
7346 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007347
7348 // #include_next <header>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007349 Builder.AddTypedTextChunk("include_next");
7350 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7351 Builder.AddTextChunk("<");
7352 Builder.AddPlaceholderChunk("header");
7353 Builder.AddTextChunk(">");
7354 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007355
7356 // #warning <message>
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007357 Builder.AddTypedTextChunk("warning");
7358 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7359 Builder.AddPlaceholderChunk("message");
7360 Results.AddResult(Builder.TakeString());
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007361
7362 // Note: #ident and #sccs are such crazy anachronisms that we don't provide
7363 // completions for them. And __include_macros is a Clang-internal extension
7364 // that we don't want to encourage anyone to use.
7365
7366 // FIXME: we don't support #assert or #unassert, so don't suggest them.
7367 Results.ExitScope();
7368
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007369 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregor0de55ce2010-08-25 18:41:16 +00007370 CodeCompletionContext::CCC_PreprocessorDirective,
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007371 Results.data(), Results.size());
7372}
7373
7374void Sema::CodeCompleteInPreprocessorConditionalExclusion(Scope *S) {
Douglas Gregorec00a262010-08-24 22:20:20 +00007375 CodeCompleteOrdinaryName(S,
John McCallfaf5fb42010-08-26 23:41:50 +00007376 S->getFnParent()? Sema::PCC_RecoveryInFunction
7377 : Sema::PCC_Namespace);
Douglas Gregor3a7ad252010-08-24 19:08:16 +00007378}
7379
Douglas Gregorec00a262010-08-24 22:20:20 +00007380void Sema::CodeCompletePreprocessorMacroName(bool IsDefinition) {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007381 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007382 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00007383 IsDefinition? CodeCompletionContext::CCC_MacroName
7384 : CodeCompletionContext::CCC_MacroNameUse);
Douglas Gregor12785102010-08-24 20:21:13 +00007385 if (!IsDefinition && (!CodeCompleter || CodeCompleter->includeMacros())) {
7386 // Add just the names of macros, not their arguments.
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007387 CodeCompletionBuilder Builder(Results.getAllocator(),
7388 Results.getCodeCompletionTUInfo());
Douglas Gregor12785102010-08-24 20:21:13 +00007389 Results.EnterNewScope();
7390 for (Preprocessor::macro_iterator M = PP.macro_begin(),
7391 MEnd = PP.macro_end();
7392 M != MEnd; ++M) {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00007393 Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007394 M->first->getName()));
Argyrios Kyrtzidis5c8b1cd2012-09-27 00:24:09 +00007395 Results.AddResult(CodeCompletionResult(Builder.TakeString(),
7396 CCP_CodePattern,
7397 CXCursor_MacroDefinition));
Douglas Gregor12785102010-08-24 20:21:13 +00007398 }
7399 Results.ExitScope();
7400 } else if (IsDefinition) {
7401 // FIXME: Can we detect when the user just wrote an include guard above?
7402 }
7403
Douglas Gregor0ac41382010-09-23 23:01:17 +00007404 HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
Douglas Gregor12785102010-08-24 20:21:13 +00007405 Results.data(), Results.size());
7406}
7407
Douglas Gregorec00a262010-08-24 22:20:20 +00007408void Sema::CodeCompletePreprocessorExpression() {
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007409 ResultBuilder Results(*this, CodeCompleter->getAllocator(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007410 CodeCompleter->getCodeCompletionTUInfo(),
Douglas Gregor0ac41382010-09-23 23:01:17 +00007411 CodeCompletionContext::CCC_PreprocessorExpression);
Douglas Gregorec00a262010-08-24 22:20:20 +00007412
7413 if (!CodeCompleter || CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00007414 AddMacroResults(PP, Results, true);
Douglas Gregorec00a262010-08-24 22:20:20 +00007415
7416 // defined (<macro>)
7417 Results.EnterNewScope();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007418 CodeCompletionBuilder Builder(Results.getAllocator(),
7419 Results.getCodeCompletionTUInfo());
Douglas Gregorb278aaf2011-02-01 19:23:04 +00007420 Builder.AddTypedTextChunk("defined");
7421 Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
7422 Builder.AddChunk(CodeCompletionString::CK_LeftParen);
7423 Builder.AddPlaceholderChunk("macro");
7424 Builder.AddChunk(CodeCompletionString::CK_RightParen);
7425 Results.AddResult(Builder.TakeString());
Douglas Gregorec00a262010-08-24 22:20:20 +00007426 Results.ExitScope();
7427
7428 HandleCodeCompleteResults(this, CodeCompleter,
7429 CodeCompletionContext::CCC_PreprocessorExpression,
7430 Results.data(), Results.size());
7431}
7432
7433void Sema::CodeCompletePreprocessorMacroArgument(Scope *S,
7434 IdentifierInfo *Macro,
7435 MacroInfo *MacroInfo,
7436 unsigned Argument) {
7437 // FIXME: In the future, we could provide "overload" results, much like we
7438 // do for function calls.
7439
Argyrios Kyrtzidis75f6cd22011-08-18 19:41:28 +00007440 // Now just ignore this. There will be another code-completion callback
7441 // for the expanded tokens.
Douglas Gregorec00a262010-08-24 22:20:20 +00007442}
7443
Douglas Gregor11583702010-08-25 17:04:25 +00007444void Sema::CodeCompleteNaturalLanguage() {
Douglas Gregor11583702010-08-25 17:04:25 +00007445 HandleCodeCompleteResults(this, CodeCompleter,
Douglas Gregorea736372010-08-25 17:10:00 +00007446 CodeCompletionContext::CCC_NaturalLanguage,
Craig Topperc3ec1492014-05-26 06:22:03 +00007447 nullptr, 0);
Douglas Gregor11583702010-08-25 17:04:25 +00007448}
7449
Douglas Gregorbcbf46c2011-02-01 22:57:45 +00007450void Sema::GatherGlobalCodeCompletions(CodeCompletionAllocator &Allocator,
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007451 CodeCompletionTUInfo &CCTUInfo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007452 SmallVectorImpl<CodeCompletionResult> &Results) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00007453 ResultBuilder Builder(*this, Allocator, CCTUInfo,
7454 CodeCompletionContext::CCC_Recovery);
Douglas Gregor39982192010-08-15 06:18:01 +00007455 if (!CodeCompleter || CodeCompleter->includeGlobals()) {
7456 CodeCompletionDeclConsumer Consumer(Builder,
7457 Context.getTranslationUnitDecl());
7458 LookupVisibleDecls(Context.getTranslationUnitDecl(), LookupAnyName,
7459 Consumer);
7460 }
Douglas Gregorb14904c2010-08-13 22:48:40 +00007461
7462 if (!CodeCompleter || CodeCompleter->includeMacros())
Douglas Gregor8cb17462012-10-09 16:01:50 +00007463 AddMacroResults(PP, Builder, true);
Douglas Gregorb14904c2010-08-13 22:48:40 +00007464
7465 Results.clear();
7466 Results.insert(Results.end(),
7467 Builder.data(), Builder.data() + Builder.size());
7468}