blob: b2dc2d79616bb8726057a40b515063fac7b0dec8 [file] [log] [blame]
Douglas Gregor3545ff42009-09-21 16:56:56 +00001//===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===//
Douglas Gregor2436e712009-09-17 21:32:03 +00002//
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 implements the CodeCompleteConsumer class.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Sema/CodeCompleteConsumer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang-c/Index.h"
Douglas Gregor56c2dbc2009-09-18 17:54:00 +000015#include "clang/AST/DeclCXX.h"
John McCallde6836a2010-08-24 07:21:54 +000016#include "clang/AST/DeclObjC.h"
John McCall19c1bfd2010-08-25 05:32:35 +000017#include "clang/AST/DeclTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/Sema.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000020#include "llvm/ADT/STLExtras.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "llvm/ADT/SmallString.h"
Douglas Gregor669a25a2011-02-17 00:22:45 +000022#include "llvm/ADT/Twine.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000023#include "llvm/Support/raw_ostream.h"
24#include <algorithm>
Douglas Gregorfedc3282009-09-18 22:15:54 +000025#include <cstring>
26#include <functional>
Douglas Gregorab6ccb52009-11-17 16:43:05 +000027
Douglas Gregor2436e712009-09-17 21:32:03 +000028using namespace clang;
29
Douglas Gregorfedc3282009-09-18 22:15:54 +000030//===----------------------------------------------------------------------===//
Douglas Gregor0212fd72010-09-21 16:06:22 +000031// Code completion context implementation
32//===----------------------------------------------------------------------===//
33
34bool CodeCompletionContext::wantConstructorResults() const {
35 switch (Kind) {
Douglas Gregor0ac41382010-09-23 23:01:17 +000036 case CCC_Recovery:
Douglas Gregor0212fd72010-09-21 16:06:22 +000037 case CCC_Statement:
38 case CCC_Expression:
39 case CCC_ObjCMessageReceiver:
40 case CCC_ParenthesizedExpression:
41 return true;
42
43 case CCC_TopLevel:
44 case CCC_ObjCInterface:
45 case CCC_ObjCImplementation:
46 case CCC_ObjCIvarList:
47 case CCC_ClassStructUnion:
Douglas Gregor21325842011-07-07 16:03:39 +000048 case CCC_DotMemberAccess:
49 case CCC_ArrowMemberAccess:
50 case CCC_ObjCPropertyAccess:
Douglas Gregor0212fd72010-09-21 16:06:22 +000051 case CCC_EnumTag:
52 case CCC_UnionTag:
53 case CCC_ClassOrStructTag:
54 case CCC_ObjCProtocolName:
55 case CCC_Namespace:
56 case CCC_Type:
57 case CCC_Name:
58 case CCC_PotentiallyQualifiedName:
59 case CCC_MacroName:
60 case CCC_MacroNameUse:
61 case CCC_PreprocessorExpression:
62 case CCC_PreprocessorDirective:
63 case CCC_NaturalLanguage:
64 case CCC_SelectorName:
65 case CCC_TypeQualifiers:
Douglas Gregor0ac41382010-09-23 23:01:17 +000066 case CCC_Other:
Douglas Gregor3a69eaf2011-02-18 23:30:37 +000067 case CCC_OtherWithMacros:
Douglas Gregor21325842011-07-07 16:03:39 +000068 case CCC_ObjCInstanceMessage:
69 case CCC_ObjCClassMessage:
Douglas Gregor2c595ad2011-07-30 06:55:39 +000070 case CCC_ObjCInterfaceName:
Douglas Gregor21325842011-07-07 16:03:39 +000071 case CCC_ObjCCategoryName:
Douglas Gregor0212fd72010-09-21 16:06:22 +000072 return false;
73 }
David Blaikie8a40f702012-01-17 06:56:22 +000074
75 llvm_unreachable("Invalid CodeCompletionContext::Kind!");
Douglas Gregor0212fd72010-09-21 16:06:22 +000076}
77
78//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +000079// Code completion string implementation
80//===----------------------------------------------------------------------===//
Douglas Gregorb278aaf2011-02-01 19:23:04 +000081CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
Daniel Dunbarb0a19422009-11-12 18:40:12 +000082 : Kind(Kind), Text("")
Douglas Gregor5bf52692009-09-22 23:15:58 +000083{
Douglas Gregor9eb77012009-11-07 00:00:49 +000084 switch (Kind) {
85 case CK_TypedText:
86 case CK_Text:
87 case CK_Placeholder:
88 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +000089 case CK_ResultType:
Douglas Gregorb278aaf2011-02-01 19:23:04 +000090 case CK_CurrentParameter:
91 this->Text = Text;
Douglas Gregor9eb77012009-11-07 00:00:49 +000092 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +000093
94 case CK_Optional:
Jeffrey Yasskin1615d452009-12-12 05:05:38 +000095 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor9eb77012009-11-07 00:00:49 +000096
97 case CK_LeftParen:
98 this->Text = "(";
99 break;
100
101 case CK_RightParen:
102 this->Text = ")";
103 break;
104
105 case CK_LeftBracket:
106 this->Text = "[";
107 break;
108
109 case CK_RightBracket:
110 this->Text = "]";
111 break;
112
113 case CK_LeftBrace:
114 this->Text = "{";
115 break;
116
117 case CK_RightBrace:
118 this->Text = "}";
119 break;
120
121 case CK_LeftAngle:
122 this->Text = "<";
123 break;
124
125 case CK_RightAngle:
126 this->Text = ">";
127 break;
128
129 case CK_Comma:
130 this->Text = ", ";
131 break;
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000132
133 case CK_Colon:
Douglas Gregor636a61e2010-04-07 00:21:17 +0000134 this->Text = ":";
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000135 break;
136
137 case CK_SemiColon:
138 this->Text = ";";
139 break;
140
141 case CK_Equal:
142 this->Text = " = ";
143 break;
144
145 case CK_HorizontalSpace:
146 this->Text = " ";
147 break;
148
149 case CK_VerticalSpace:
150 this->Text = "\n";
151 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000152 }
Douglas Gregor5bf52692009-09-22 23:15:58 +0000153}
154
155CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000156CodeCompletionString::Chunk::CreateText(const char *Text) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000157 return Chunk(CK_Text, Text);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000158}
159
160CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000161CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000162 Chunk Result;
163 Result.Kind = CK_Optional;
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000164 Result.Optional = Optional;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000165 return Result;
166}
167
168CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000169CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000170 return Chunk(CK_Placeholder, Placeholder);
171}
172
173CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000174CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000175 return Chunk(CK_Informative, Informative);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000176}
177
Douglas Gregor9eb77012009-11-07 00:00:49 +0000178CodeCompletionString::Chunk
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000179CodeCompletionString::Chunk::CreateResultType(const char *ResultType) {
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000180 return Chunk(CK_ResultType, ResultType);
181}
182
183CodeCompletionString::Chunk
Douglas Gregor9eb77012009-11-07 00:00:49 +0000184CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000185 const char *CurrentParameter) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000186 return Chunk(CK_CurrentParameter, CurrentParameter);
187}
188
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000189CodeCompletionString::CodeCompletionString(const Chunk *Chunks,
190 unsigned NumChunks,
191 unsigned Priority,
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000192 CXAvailabilityKind Availability,
193 const char **Annotations,
Douglas Gregor78254c82012-03-27 23:34:16 +0000194 unsigned NumAnnotations,
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000195 StringRef ParentName,
196 const char *BriefComment)
Douglas Gregor78254c82012-03-27 23:34:16 +0000197 : NumChunks(NumChunks), NumAnnotations(NumAnnotations),
Argyrios Kyrtzidis9ae39562012-09-26 16:39:56 +0000198 Priority(Priority), Availability(Availability),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000199 ParentName(ParentName), BriefComment(BriefComment)
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000200{
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000201 assert(NumChunks <= 0xffff);
202 assert(NumAnnotations <= 0xffff);
203
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000204 Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
205 for (unsigned I = 0; I != NumChunks; ++I)
206 StoredChunks[I] = Chunks[I];
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000207
208 const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks);
209 for (unsigned I = 0; I != NumAnnotations; ++I)
210 StoredAnnotations[I] = Annotations[I];
Douglas Gregorfedc3282009-09-18 22:15:54 +0000211}
212
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000213unsigned CodeCompletionString::getAnnotationCount() const {
214 return NumAnnotations;
215}
216
217const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const {
218 if (AnnotationNr < NumAnnotations)
219 return reinterpret_cast<const char * const*>(end())[AnnotationNr];
220 else
Craig Topperc3ec1492014-05-26 06:22:03 +0000221 return nullptr;
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000222}
223
224
Douglas Gregorfedc3282009-09-18 22:15:54 +0000225std::string CodeCompletionString::getAsString() const {
226 std::string Result;
227 llvm::raw_string_ostream OS(Result);
228
229 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
230 switch (C->Kind) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000231 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000232 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000233
234 case CK_Informative:
235 case CK_ResultType:
236 OS << "[#" << C->Text << "#]";
237 break;
238
Douglas Gregor9eb77012009-11-07 00:00:49 +0000239 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
240 default: OS << C->Text; break;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000241 }
242 }
Dan Gohman4888f1a2010-07-26 21:33:22 +0000243 return OS.str();
Douglas Gregorfedc3282009-09-18 22:15:54 +0000244}
245
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000246const char *CodeCompletionString::getTypedText() const {
247 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
248 if (C->Kind == CK_TypedText)
249 return C->Text;
Craig Topperc3ec1492014-05-26 06:22:03 +0000250
251 return nullptr;
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000252}
253
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000254const char *CodeCompletionAllocator::CopyString(StringRef String) {
Douglas Gregorbcbf46c2011-02-01 22:57:45 +0000255 char *Mem = (char *)Allocate(String.size() + 1, 1);
256 std::copy(String.begin(), String.end(), Mem);
257 Mem[String.size()] = 0;
258 return Mem;
259}
260
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000261const char *CodeCompletionAllocator::CopyString(Twine String) {
Douglas Gregor669a25a2011-02-17 00:22:45 +0000262 // FIXME: It would be more efficient to teach Twine to tell us its size and
263 // then add a routine there to fill in an allocated char* with the contents
264 // of the string.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000265 SmallString<128> Data;
Douglas Gregor669a25a2011-02-17 00:22:45 +0000266 return CopyString(String.toStringRef(Data));
267}
268
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000269StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) {
270 const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000271 if (!ND)
272 return StringRef();
273
274 // Check whether we've already cached the parent name.
275 StringRef &CachedParentName = ParentNames[DC];
276 if (!CachedParentName.empty())
277 return CachedParentName;
278
279 // If we already processed this DeclContext and assigned empty to it, the
280 // data pointer will be non-null.
Craig Topperc3ec1492014-05-26 06:22:03 +0000281 if (CachedParentName.data() != nullptr)
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000282 return StringRef();
283
284 // Find the interesting names.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000285 SmallVector<const DeclContext *, 2> Contexts;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000286 while (DC && !DC->isFunctionOrMethod()) {
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000287 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000288 if (ND->getIdentifier())
289 Contexts.push_back(DC);
290 }
291
292 DC = DC->getParent();
293 }
294
295 {
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000296 SmallString<128> S;
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000297 llvm::raw_svector_ostream OS(S);
298 bool First = true;
299 for (unsigned I = Contexts.size(); I != 0; --I) {
300 if (First)
301 First = false;
302 else {
303 OS << "::";
304 }
305
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000306 const DeclContext *CurDC = Contexts[I-1];
307 if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000308 CurDC = CatImpl->getCategoryDecl();
309
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000310 if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
311 const ObjCInterfaceDecl *Interface = Cat->getClassInterface();
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000312 if (!Interface) {
313 // Assign an empty StringRef but with non-null data to distinguish
314 // between empty because we didn't process the DeclContext yet.
315 CachedParentName = StringRef((const char *)~0U, 0);
316 return StringRef();
317 }
318
319 OS << Interface->getName() << '(' << Cat->getName() << ')';
320 } else {
321 OS << cast<NamedDecl>(CurDC)->getName();
322 }
323 }
324
325 CachedParentName = AllocatorRef->CopyString(OS.str());
326 }
327
328 return CachedParentName;
329}
330
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000331CodeCompletionString *CodeCompletionBuilder::TakeString() {
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000332 void *Mem = getAllocator().Allocate(
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000333 sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size()
334 + sizeof(const char *) * Annotations.size(),
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000335 llvm::alignOf<CodeCompletionString>());
336 CodeCompletionString *Result
337 = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
Erik Verbruggen98ea7f62011-10-14 15:31:08 +0000338 Priority, Availability,
Douglas Gregor78254c82012-03-27 23:34:16 +0000339 Annotations.data(), Annotations.size(),
Argyrios Kyrtzidis9ae39562012-09-26 16:39:56 +0000340 ParentName, BriefComment);
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000341 Chunks.clear();
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000342 return Result;
343}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000344
Benjamin Kramerdb534a42012-03-26 16:57:36 +0000345void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) {
346 Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text));
347}
348
349void CodeCompletionBuilder::AddTextChunk(const char *Text) {
350 Chunks.push_back(Chunk::CreateText(Text));
351}
352
353void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) {
354 Chunks.push_back(Chunk::CreateOptional(Optional));
355}
356
357void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) {
358 Chunks.push_back(Chunk::CreatePlaceholder(Placeholder));
359}
360
361void CodeCompletionBuilder::AddInformativeChunk(const char *Text) {
362 Chunks.push_back(Chunk::CreateInformative(Text));
363}
364
365void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) {
366 Chunks.push_back(Chunk::CreateResultType(ResultType));
367}
368
369void
370CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) {
371 Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter));
372}
373
374void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK,
375 const char *Text) {
376 Chunks.push_back(Chunk(CK, Text));
377}
378
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000379void CodeCompletionBuilder::addParentContext(const DeclContext *DC) {
Douglas Gregor78254c82012-03-27 23:34:16 +0000380 if (DC->isTranslationUnit()) {
Douglas Gregor78254c82012-03-27 23:34:16 +0000381 return;
382 }
383
384 if (DC->isFunctionOrMethod())
385 return;
386
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000387 const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
Douglas Gregor78254c82012-03-27 23:34:16 +0000388 if (!ND)
389 return;
390
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000391 ParentName = getCodeCompletionTUInfo().getParentName(DC);
Douglas Gregor78254c82012-03-27 23:34:16 +0000392}
393
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000394void CodeCompletionBuilder::addBriefComment(StringRef Comment) {
395 BriefComment = Allocator.CopyString(Comment);
396}
397
Douglas Gregorfedc3282009-09-18 22:15:54 +0000398//===----------------------------------------------------------------------===//
Douglas Gregor05f477c2009-09-23 00:16:58 +0000399// Code completion overload candidate implementation
400//===----------------------------------------------------------------------===//
401FunctionDecl *
402CodeCompleteConsumer::OverloadCandidate::getFunction() const {
403 if (getKind() == CK_Function)
404 return Function;
405 else if (getKind() == CK_FunctionTemplate)
406 return FunctionTemplate->getTemplatedDecl();
407 else
Craig Topperc3ec1492014-05-26 06:22:03 +0000408 return nullptr;
Douglas Gregor05f477c2009-09-23 00:16:58 +0000409}
410
411const FunctionType *
412CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
413 switch (Kind) {
414 case CK_Function:
415 return Function->getType()->getAs<FunctionType>();
416
417 case CK_FunctionTemplate:
418 return FunctionTemplate->getTemplatedDecl()->getType()
419 ->getAs<FunctionType>();
420
421 case CK_FunctionType:
422 return Type;
423 }
David Blaikie8a40f702012-01-17 06:56:22 +0000424
425 llvm_unreachable("Invalid CandidateKind!");
Douglas Gregor05f477c2009-09-23 00:16:58 +0000426}
427
428//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +0000429// Code completion consumer implementation
430//===----------------------------------------------------------------------===//
431
Douglas Gregor3545ff42009-09-21 16:56:56 +0000432CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregorfedc3282009-09-18 22:15:54 +0000433
Douglas Gregor2436e712009-09-17 21:32:03 +0000434void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000435PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000436 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +0000437 CodeCompletionResult *Results,
Douglas Gregor2436e712009-09-17 21:32:03 +0000438 unsigned NumResults) {
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000439 std::stable_sort(Results, Results + NumResults);
440
Douglas Gregor2436e712009-09-17 21:32:03 +0000441 // Print the results.
442 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregor58acf322009-10-09 22:16:47 +0000443 OS << "COMPLETION: ";
Douglas Gregor2436e712009-09-17 21:32:03 +0000444 switch (Results[I].Kind) {
John McCall276321a2010-08-25 06:19:51 +0000445 case CodeCompletionResult::RK_Declaration:
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000446 OS << *Results[I].Declaration;
Douglas Gregor2436e712009-09-17 21:32:03 +0000447 if (Results[I].Hidden)
448 OS << " (Hidden)";
Douglas Gregor3545ff42009-09-21 16:56:56 +0000449 if (CodeCompletionString *CCS
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000450 = Results[I].CreateCodeCompletionString(SemaRef, getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000451 CCTUInfo,
452 includeBriefComments())) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000453 OS << " : " << CCS->getAsString();
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000454 if (const char *BriefComment = CCS->getBriefComment())
455 OS << " : " << BriefComment;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000456 }
457
Douglas Gregor2436e712009-09-17 21:32:03 +0000458 OS << '\n';
459 break;
460
John McCall276321a2010-08-25 06:19:51 +0000461 case CodeCompletionResult::RK_Keyword:
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000462 OS << Results[I].Keyword << '\n';
Douglas Gregor2436e712009-09-17 21:32:03 +0000463 break;
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000464
John McCall276321a2010-08-25 06:19:51 +0000465 case CodeCompletionResult::RK_Macro: {
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000466 OS << Results[I].Macro->getName();
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000467 if (CodeCompletionString *CCS
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000468 = Results[I].CreateCodeCompletionString(SemaRef, getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +0000469 CCTUInfo,
470 includeBriefComments())) {
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000471 OS << " : " << CCS->getAsString();
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000472 }
473 OS << '\n';
474 break;
475 }
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000476
John McCall276321a2010-08-25 06:19:51 +0000477 case CodeCompletionResult::RK_Pattern: {
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000478 OS << "Pattern : "
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000479 << Results[I].Pattern->getAsString() << '\n';
480 break;
481 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000482 }
483 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000484}
Douglas Gregor05f477c2009-09-23 00:16:58 +0000485
486void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000487PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
488 unsigned CurrentArg,
Douglas Gregor05f477c2009-09-23 00:16:58 +0000489 OverloadCandidate *Candidates,
490 unsigned NumCandidates) {
491 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregorf0f51982009-09-23 00:34:09 +0000492 if (CodeCompletionString *CCS
Douglas Gregorb278aaf2011-02-01 19:23:04 +0000493 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef,
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +0000494 getAllocator(), CCTUInfo)) {
Douglas Gregor58acf322009-10-09 22:16:47 +0000495 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregor05f477c2009-09-23 00:16:58 +0000496 }
497 }
Douglas Gregor05f477c2009-09-23 00:16:58 +0000498}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000499
Douglas Gregor7b316822012-03-17 06:39:06 +0000500/// \brief Retrieve the effective availability of the given declaration.
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000501static AvailabilityResult getDeclAvailability(const Decl *D) {
Douglas Gregor7b316822012-03-17 06:39:06 +0000502 AvailabilityResult AR = D->getAvailability();
503 if (isa<EnumConstantDecl>(D))
504 AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability());
505 return AR;
506}
507
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000508void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) {
Douglas Gregorb14904c2010-08-13 22:48:40 +0000509 switch (Kind) {
Douglas Gregor78254c82012-03-27 23:34:16 +0000510 case RK_Pattern:
511 if (!Declaration) {
512 // Do nothing: Patterns can come with cursor kinds!
513 break;
514 }
515 // Fall through
516
Douglas Gregor7b316822012-03-17 06:39:06 +0000517 case RK_Declaration: {
Douglas Gregorf757a122010-08-23 23:00:57 +0000518 // Set the availability based on attributes.
Douglas Gregor7b316822012-03-17 06:39:06 +0000519 switch (getDeclAvailability(Declaration)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000520 case AR_Available:
521 case AR_NotYetIntroduced:
522 Availability = CXAvailability_Available;
523 break;
Douglas Gregorf757a122010-08-23 23:00:57 +0000524
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000525 case AR_Deprecated:
526 Availability = CXAvailability_Deprecated;
527 break;
528
529 case AR_Unavailable:
530 Availability = CXAvailability_NotAvailable;
531 break;
532 }
533
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +0000534 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
Douglas Gregor09c0eb12010-09-03 23:30:36 +0000535 if (Function->isDeleted())
Douglas Gregorf757a122010-08-23 23:00:57 +0000536 Availability = CXAvailability_NotAvailable;
Douglas Gregor09c0eb12010-09-03 23:30:36 +0000537
538 CursorKind = getCursorKindForDecl(Declaration);
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000539 if (CursorKind == CXCursor_UnexposedDecl) {
Douglas Gregorf6102672012-01-01 21:23:57 +0000540 // FIXME: Forward declarations of Objective-C classes and protocols
541 // are not directly exposed, but we want code completion to treat them
542 // like a definition.
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000543 if (isa<ObjCInterfaceDecl>(Declaration))
544 CursorKind = CXCursor_ObjCInterfaceDecl;
Douglas Gregorf6102672012-01-01 21:23:57 +0000545 else if (isa<ObjCProtocolDecl>(Declaration))
546 CursorKind = CXCursor_ObjCProtocolDecl;
Douglas Gregordeafd0b2011-12-27 22:43:10 +0000547 else
548 CursorKind = CXCursor_NotImplemented;
549 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000550 break;
Douglas Gregor7b316822012-03-17 06:39:06 +0000551 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000552
John McCall276321a2010-08-25 06:19:51 +0000553 case RK_Macro:
John McCall276321a2010-08-25 06:19:51 +0000554 case RK_Keyword:
Benjamin Kramerc25c0b92012-05-20 14:19:46 +0000555 llvm_unreachable("Macro and keyword kinds are handled by the constructors");
Douglas Gregor8e984da2010-08-04 16:47:14 +0000556 }
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000557
558 if (!Accessible)
559 Availability = CXAvailability_NotAccessible;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000560}
Douglas Gregorf09935f2009-12-01 05:55:20 +0000561
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000562/// \brief Retrieve the name that should be used to order a result.
563///
564/// If the name needs to be constructed as a string, that string will be
565/// saved into Saved and the returned StringRef will refer to it.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000566static StringRef getOrderedName(const CodeCompletionResult &R,
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000567 std::string &Saved) {
568 switch (R.Kind) {
569 case CodeCompletionResult::RK_Keyword:
570 return R.Keyword;
571
572 case CodeCompletionResult::RK_Pattern:
573 return R.Pattern->getTypedText();
574
575 case CodeCompletionResult::RK_Macro:
576 return R.Macro->getName();
577
578 case CodeCompletionResult::RK_Declaration:
579 // Handle declarations below.
580 break;
581 }
582
583 DeclarationName Name = R.Declaration->getDeclName();
584
585 // If the name is a simple identifier (by far the common case), or a
586 // zero-argument selector, just return a reference to that identifier.
587 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
588 return Id->getName();
589 if (Name.isObjCZeroArgSelector())
590 if (IdentifierInfo *Id
591 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
592 return Id->getName();
593
594 Saved = Name.getAsString();
595 return Saved;
596}
597
598bool clang::operator<(const CodeCompletionResult &X,
599 const CodeCompletionResult &Y) {
600 std::string XSaved, YSaved;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000601 StringRef XStr = getOrderedName(X, XSaved);
602 StringRef YStr = getOrderedName(Y, YSaved);
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000603 int cmp = XStr.compare_lower(YStr);
604 if (cmp)
605 return cmp < 0;
606
Douglas Gregor49f67ce2010-08-26 13:48:20 +0000607 // If case-insensitive comparison fails, try case-sensitive comparison.
608 cmp = XStr.compare(YStr);
609 if (cmp)
610 return cmp < 0;
Douglas Gregor0de55ce2010-08-25 18:41:16 +0000611
612 return false;
613}