blob: 6cefc61f6640f542dfa4f681aab57d95c8e1102f [file] [log] [blame]
Douglas Gregor86d9a522009-09-21 16:56:56 +00001//===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===//
Douglas Gregor81b747b2009-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"
Douglas Gregor75b71282009-09-18 17:54:00 +000014#include "clang/AST/DeclCXX.h"
Douglas Gregor374929f2009-09-18 15:37:17 +000015#include "clang/Parse/Scope.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000016#include "clang/Lex/Preprocessor.h"
Douglas Gregor2b4074f2009-12-01 05:55:20 +000017#include "clang-c/Index.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000018#include "Sema.h"
19#include "llvm/ADT/STLExtras.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000020#include "llvm/Support/raw_ostream.h"
21#include <algorithm>
Douglas Gregore6e03612009-09-18 22:15:54 +000022#include <cstring>
23#include <functional>
Douglas Gregor92eff462009-11-17 16:43:05 +000024
Douglas Gregor81b747b2009-09-17 21:32:03 +000025using namespace clang;
Douglas Gregor92eff462009-11-17 16:43:05 +000026using llvm::StringRef;
Douglas Gregor81b747b2009-09-17 21:32:03 +000027
Douglas Gregore6e03612009-09-18 22:15:54 +000028//===----------------------------------------------------------------------===//
29// Code completion string implementation
30//===----------------------------------------------------------------------===//
Douglas Gregor0c8296d2009-11-07 00:00:49 +000031CodeCompletionString::Chunk::Chunk(ChunkKind Kind, llvm::StringRef Text)
Daniel Dunbarad5757f2009-11-12 18:40:12 +000032 : Kind(Kind), Text("")
Douglas Gregor0563c262009-09-22 23:15:58 +000033{
Douglas Gregor0c8296d2009-11-07 00:00:49 +000034 switch (Kind) {
35 case CK_TypedText:
36 case CK_Text:
37 case CK_Placeholder:
38 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +000039 case CK_ResultType:
Douglas Gregor0c8296d2009-11-07 00:00:49 +000040 case CK_CurrentParameter: {
41 char *New = new char [Text.size() + 1];
42 std::memcpy(New, Text.data(), Text.size());
43 New[Text.size()] = '\0';
44 this->Text = New;
45 break;
46 }
47
48 case CK_Optional:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000049 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor0c8296d2009-11-07 00:00:49 +000050 break;
51
52 case CK_LeftParen:
53 this->Text = "(";
54 break;
55
56 case CK_RightParen:
57 this->Text = ")";
58 break;
59
60 case CK_LeftBracket:
61 this->Text = "[";
62 break;
63
64 case CK_RightBracket:
65 this->Text = "]";
66 break;
67
68 case CK_LeftBrace:
69 this->Text = "{";
70 break;
71
72 case CK_RightBrace:
73 this->Text = "}";
74 break;
75
76 case CK_LeftAngle:
77 this->Text = "<";
78 break;
79
80 case CK_RightAngle:
81 this->Text = ">";
82 break;
83
84 case CK_Comma:
85 this->Text = ", ";
86 break;
Douglas Gregor01dfea02010-01-10 23:08:15 +000087
88 case CK_Colon:
Douglas Gregore8f5a172010-04-07 00:21:17 +000089 this->Text = ":";
Douglas Gregor01dfea02010-01-10 23:08:15 +000090 break;
91
92 case CK_SemiColon:
93 this->Text = ";";
94 break;
95
96 case CK_Equal:
97 this->Text = " = ";
98 break;
99
100 case CK_HorizontalSpace:
101 this->Text = " ";
102 break;
103
104 case CK_VerticalSpace:
105 this->Text = "\n";
106 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000107 }
Douglas Gregor0563c262009-09-22 23:15:58 +0000108}
109
110CodeCompletionString::Chunk
Douglas Gregor92eff462009-11-17 16:43:05 +0000111CodeCompletionString::Chunk::CreateText(StringRef Text) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000112 return Chunk(CK_Text, Text);
Douglas Gregore6e03612009-09-18 22:15:54 +0000113}
114
115CodeCompletionString::Chunk
116CodeCompletionString::Chunk::CreateOptional(
117 std::auto_ptr<CodeCompletionString> Optional) {
118 Chunk Result;
119 Result.Kind = CK_Optional;
120 Result.Optional = Optional.release();
121 return Result;
122}
123
124CodeCompletionString::Chunk
Douglas Gregor92eff462009-11-17 16:43:05 +0000125CodeCompletionString::Chunk::CreatePlaceholder(StringRef Placeholder) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000126 return Chunk(CK_Placeholder, Placeholder);
127}
128
129CodeCompletionString::Chunk
Douglas Gregor92eff462009-11-17 16:43:05 +0000130CodeCompletionString::Chunk::CreateInformative(StringRef Informative) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000131 return Chunk(CK_Informative, Informative);
Douglas Gregore6e03612009-09-18 22:15:54 +0000132}
133
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000134CodeCompletionString::Chunk
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000135CodeCompletionString::Chunk::CreateResultType(StringRef ResultType) {
136 return Chunk(CK_ResultType, ResultType);
137}
138
139CodeCompletionString::Chunk
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000140CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregor92eff462009-11-17 16:43:05 +0000141 StringRef CurrentParameter) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000142 return Chunk(CK_CurrentParameter, CurrentParameter);
143}
144
Douglas Gregor54f01612009-11-19 00:01:57 +0000145CodeCompletionString::Chunk CodeCompletionString::Chunk::Clone() const {
146 switch (Kind) {
147 case CK_TypedText:
148 case CK_Text:
149 case CK_Placeholder:
150 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000151 case CK_ResultType:
Douglas Gregor54f01612009-11-19 00:01:57 +0000152 case CK_CurrentParameter:
153 case CK_LeftParen:
154 case CK_RightParen:
155 case CK_LeftBracket:
156 case CK_RightBracket:
157 case CK_LeftBrace:
158 case CK_RightBrace:
159 case CK_LeftAngle:
160 case CK_RightAngle:
161 case CK_Comma:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000162 case CK_Colon:
163 case CK_SemiColon:
164 case CK_Equal:
165 case CK_HorizontalSpace:
166 case CK_VerticalSpace:
Douglas Gregor54f01612009-11-19 00:01:57 +0000167 return Chunk(Kind, Text);
168
169 case CK_Optional: {
170 std::auto_ptr<CodeCompletionString> Opt(Optional->Clone());
171 return CreateOptional(Opt);
172 }
173 }
174
175 // Silence GCC warning.
176 return Chunk();
177}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000178
Douglas Gregore6e03612009-09-18 22:15:54 +0000179void
180CodeCompletionString::Chunk::Destroy() {
181 switch (Kind) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000182 case CK_Optional:
183 delete Optional;
184 break;
185
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000186 case CK_TypedText:
Douglas Gregor0563c262009-09-22 23:15:58 +0000187 case CK_Text:
188 case CK_Placeholder:
189 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000190 case CK_ResultType:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000191 case CK_CurrentParameter:
192 delete [] Text;
193 break;
194
195 case CK_LeftParen:
196 case CK_RightParen:
197 case CK_LeftBracket:
198 case CK_RightBracket:
199 case CK_LeftBrace:
200 case CK_RightBrace:
201 case CK_LeftAngle:
202 case CK_RightAngle:
203 case CK_Comma:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000204 case CK_Colon:
205 case CK_SemiColon:
206 case CK_Equal:
207 case CK_HorizontalSpace:
208 case CK_VerticalSpace:
Douglas Gregor0563c262009-09-22 23:15:58 +0000209 break;
Douglas Gregore6e03612009-09-18 22:15:54 +0000210 }
211}
212
Douglas Gregor12e13132010-05-26 22:00:08 +0000213void CodeCompletionString::clear() {
Douglas Gregore6e03612009-09-18 22:15:54 +0000214 std::for_each(Chunks.begin(), Chunks.end(),
215 std::mem_fun_ref(&Chunk::Destroy));
Douglas Gregor12e13132010-05-26 22:00:08 +0000216 Chunks.clear();
Douglas Gregore6e03612009-09-18 22:15:54 +0000217}
218
219std::string CodeCompletionString::getAsString() const {
220 std::string Result;
221 llvm::raw_string_ostream OS(Result);
222
223 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
224 switch (C->Kind) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000225 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor0563c262009-09-22 23:15:58 +0000226 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000227
228 case CK_Informative:
229 case CK_ResultType:
230 OS << "[#" << C->Text << "#]";
231 break;
232
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000233 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
234 default: OS << C->Text; break;
Douglas Gregore6e03612009-09-18 22:15:54 +0000235 }
236 }
Douglas Gregore2b7eea2009-09-29 15:13:39 +0000237 OS.flush();
Douglas Gregore6e03612009-09-18 22:15:54 +0000238 return Result;
239}
240
Douglas Gregor54f01612009-11-19 00:01:57 +0000241const char *CodeCompletionString::getTypedText() const {
242 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
243 if (C->Kind == CK_TypedText)
244 return C->Text;
245
246 return 0;
247}
248
249CodeCompletionString *CodeCompletionString::Clone() const {
250 CodeCompletionString *Result = new CodeCompletionString;
251 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
252 Result->AddChunk(C->Clone());
253 return Result;
254}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000255
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000256static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
257 OS.write((const char *)&Value, sizeof(unsigned));
258}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000259
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000260static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
261 unsigned &Value) {
262 if (Memory + sizeof(unsigned) > MemoryEnd)
263 return true;
264
265 memmove(&Value, Memory, sizeof(unsigned));
266 Memory += sizeof(unsigned);
267 return false;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000268}
269
270void CodeCompletionString::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000271 // Write the number of chunks.
272 WriteUnsigned(OS, size());
273
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000274 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000275 WriteUnsigned(OS, C->Kind);
276
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000277 switch (C->Kind) {
278 case CK_TypedText:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000279 case CK_Text:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000280 case CK_Placeholder:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000281 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000282 case CK_ResultType:
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000283 case CK_CurrentParameter: {
284 const char *Text = C->Text;
285 unsigned StrLen = strlen(Text);
286 WriteUnsigned(OS, StrLen);
287 OS.write(Text, StrLen);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000288 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000289 }
290
291 case CK_Optional:
292 C->Optional->Serialize(OS);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000293 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000294
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000295 case CK_LeftParen:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000296 case CK_RightParen:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000297 case CK_LeftBracket:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000298 case CK_RightBracket:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000299 case CK_LeftBrace:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000300 case CK_RightBrace:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000301 case CK_LeftAngle:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000302 case CK_RightAngle:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000303 case CK_Comma:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000304 case CK_Colon:
305 case CK_SemiColon:
306 case CK_Equal:
307 case CK_HorizontalSpace:
308 case CK_VerticalSpace:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000309 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000310 }
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000311 }
312}
313
Douglas Gregor12e13132010-05-26 22:00:08 +0000314bool CodeCompletionString::Deserialize(const char *&Str, const char *StrEnd) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000315 if (Str == StrEnd || *Str == 0)
Douglas Gregor12e13132010-05-26 22:00:08 +0000316 return false;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000317
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000318 unsigned NumBlocks;
319 if (ReadUnsigned(Str, StrEnd, NumBlocks))
Douglas Gregor12e13132010-05-26 22:00:08 +0000320 return false;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000321
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000322 for (unsigned I = 0; I != NumBlocks; ++I) {
323 if (Str + 1 >= StrEnd)
324 break;
325
326 // Parse the next kind.
327 unsigned KindValue;
328 if (ReadUnsigned(Str, StrEnd, KindValue))
Douglas Gregor12e13132010-05-26 22:00:08 +0000329 return false;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000330
331 switch (ChunkKind Kind = (ChunkKind)KindValue) {
332 case CK_TypedText:
333 case CK_Text:
334 case CK_Placeholder:
335 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000336 case CK_ResultType:
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000337 case CK_CurrentParameter: {
338 unsigned StrLen;
339 if (ReadUnsigned(Str, StrEnd, StrLen) || (Str + StrLen > StrEnd))
Douglas Gregor12e13132010-05-26 22:00:08 +0000340 return false;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000341
Douglas Gregor12e13132010-05-26 22:00:08 +0000342 AddChunk(Chunk(Kind, StringRef(Str, StrLen)));
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000343 Str += StrLen;
344 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000345 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000346
347 case CK_Optional: {
Douglas Gregor12e13132010-05-26 22:00:08 +0000348 std::auto_ptr<CodeCompletionString> Optional(new CodeCompletionString());
349 if (Optional->Deserialize(Str, StrEnd))
350 AddOptionalChunk(Optional);
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000351 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000352 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000353
354 case CK_LeftParen:
355 case CK_RightParen:
356 case CK_LeftBracket:
357 case CK_RightBracket:
358 case CK_LeftBrace:
359 case CK_RightBrace:
360 case CK_LeftAngle:
361 case CK_RightAngle:
362 case CK_Comma:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000363 case CK_Colon:
364 case CK_SemiColon:
365 case CK_Equal:
366 case CK_HorizontalSpace:
367 case CK_VerticalSpace:
Douglas Gregor12e13132010-05-26 22:00:08 +0000368 AddChunk(Chunk(Kind));
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000369 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000370 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000371 };
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000372
Douglas Gregor12e13132010-05-26 22:00:08 +0000373 return true;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000374}
375
Douglas Gregor54f01612009-11-19 00:01:57 +0000376void CodeCompleteConsumer::Result::Destroy() {
377 if (Kind == RK_Pattern) {
378 delete Pattern;
379 Pattern = 0;
380 }
381}
382
Douglas Gregor12e13132010-05-26 22:00:08 +0000383unsigned CodeCompleteConsumer::Result::getPriorityFromDecl(NamedDecl *ND) {
384 if (!ND)
385 return CCP_Unlikely;
386
387 // Context-based decisions.
388 DeclContext *DC = ND->getDeclContext()->getLookupContext();
389 if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC))
390 return CCP_LocalDeclaration;
391 if (DC->isRecord() || isa<ObjCContainerDecl>(DC))
392 return CCP_MemberDeclaration;
393
394 // Content-based decisions.
395 if (isa<EnumConstantDecl>(ND))
396 return CCP_Constant;
397 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
398 return CCP_Type;
399 return CCP_Declaration;
400}
401
Douglas Gregore6e03612009-09-18 22:15:54 +0000402//===----------------------------------------------------------------------===//
Douglas Gregor05944382009-09-23 00:16:58 +0000403// Code completion overload candidate implementation
404//===----------------------------------------------------------------------===//
405FunctionDecl *
406CodeCompleteConsumer::OverloadCandidate::getFunction() const {
407 if (getKind() == CK_Function)
408 return Function;
409 else if (getKind() == CK_FunctionTemplate)
410 return FunctionTemplate->getTemplatedDecl();
411 else
412 return 0;
413}
414
415const FunctionType *
416CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
417 switch (Kind) {
418 case CK_Function:
419 return Function->getType()->getAs<FunctionType>();
420
421 case CK_FunctionTemplate:
422 return FunctionTemplate->getTemplatedDecl()->getType()
423 ->getAs<FunctionType>();
424
425 case CK_FunctionType:
426 return Type;
427 }
428
429 return 0;
430}
431
432//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +0000433// Code completion consumer implementation
434//===----------------------------------------------------------------------===//
435
Douglas Gregor86d9a522009-09-21 16:56:56 +0000436CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregore6e03612009-09-18 22:15:54 +0000437
Douglas Gregor81b747b2009-09-17 21:32:03 +0000438void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000439PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
440 Result *Results,
Douglas Gregor81b747b2009-09-17 21:32:03 +0000441 unsigned NumResults) {
Douglas Gregor81b747b2009-09-17 21:32:03 +0000442 // Print the results.
443 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000444 OS << "COMPLETION: ";
Douglas Gregor81b747b2009-09-17 21:32:03 +0000445 switch (Results[I].Kind) {
446 case Result::RK_Declaration:
Benjamin Kramer900fc632010-04-17 09:33:03 +0000447 OS << Results[I].Declaration;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000448 if (Results[I].Hidden)
449 OS << " (Hidden)";
Douglas Gregor86d9a522009-09-21 16:56:56 +0000450 if (CodeCompletionString *CCS
451 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000452 OS << " : " << CCS->getAsString();
453 delete CCS;
454 }
455
Douglas Gregor81b747b2009-09-17 21:32:03 +0000456 OS << '\n';
457 break;
458
459 case Result::RK_Keyword:
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000460 OS << Results[I].Keyword << '\n';
Douglas Gregor81b747b2009-09-17 21:32:03 +0000461 break;
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000462
463 case Result::RK_Macro: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000464 OS << Results[I].Macro->getName();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000465 if (CodeCompletionString *CCS
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000466 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000467 OS << " : " << CCS->getAsString();
468 delete CCS;
469 }
470 OS << '\n';
471 break;
472 }
Douglas Gregor54f01612009-11-19 00:01:57 +0000473
474 case Result::RK_Pattern: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000475 OS << "Pattern : "
Douglas Gregor54f01612009-11-19 00:01:57 +0000476 << Results[I].Pattern->getAsString() << '\n';
477 break;
478 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000479 }
480 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000481}
Douglas Gregor05944382009-09-23 00:16:58 +0000482
483void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000484PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
485 unsigned CurrentArg,
Douglas Gregor05944382009-09-23 00:16:58 +0000486 OverloadCandidate *Candidates,
487 unsigned NumCandidates) {
488 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor86d802e2009-09-23 00:34:09 +0000489 if (CodeCompletionString *CCS
490 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef)) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000491 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregor86d802e2009-09-23 00:34:09 +0000492 delete CCS;
Douglas Gregor05944382009-09-23 00:16:58 +0000493 }
494 }
Douglas Gregor05944382009-09-23 00:16:58 +0000495}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000496
497void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000498CIndexCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
499 Result *Results,
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000500 unsigned NumResults) {
501 // Print the results.
502 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000503 CXCursorKind Kind = CXCursor_NotImplemented;
504
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000505 switch (Results[I].Kind) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000506 case Result::RK_Declaration:
507 switch (Results[I].Declaration->getKind()) {
508 case Decl::Record:
509 case Decl::CXXRecord:
510 case Decl::ClassTemplateSpecialization: {
511 RecordDecl *Record = cast<RecordDecl>(Results[I].Declaration);
512 if (Record->isStruct())
513 Kind = CXCursor_StructDecl;
514 else if (Record->isUnion())
515 Kind = CXCursor_UnionDecl;
516 else
517 Kind = CXCursor_ClassDecl;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000518 break;
519 }
Douglas Gregor54f01612009-11-19 00:01:57 +0000520
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000521 case Decl::ObjCMethod: {
522 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(Results[I].Declaration);
523 if (Method->isInstanceMethod())
524 Kind = CXCursor_ObjCInstanceMethodDecl;
525 else
526 Kind = CXCursor_ObjCClassMethodDecl;
Douglas Gregor54f01612009-11-19 00:01:57 +0000527 break;
528 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000529
530 case Decl::Typedef:
531 Kind = CXCursor_TypedefDecl;
532 break;
533
534 case Decl::Enum:
535 Kind = CXCursor_EnumDecl;
536 break;
537
538 case Decl::Field:
539 Kind = CXCursor_FieldDecl;
540 break;
541
542 case Decl::EnumConstant:
543 Kind = CXCursor_EnumConstantDecl;
544 break;
545
546 case Decl::Function:
547 case Decl::CXXMethod:
548 case Decl::CXXConstructor:
549 case Decl::CXXDestructor:
550 case Decl::CXXConversion:
551 Kind = CXCursor_FunctionDecl;
552 break;
553
554 case Decl::Var:
555 Kind = CXCursor_VarDecl;
556 break;
557
558 case Decl::ParmVar:
559 Kind = CXCursor_ParmDecl;
560 break;
561
562 case Decl::ObjCInterface:
563 Kind = CXCursor_ObjCInterfaceDecl;
564 break;
565
566 case Decl::ObjCCategory:
567 Kind = CXCursor_ObjCCategoryDecl;
568 break;
569
570 case Decl::ObjCProtocol:
571 Kind = CXCursor_ObjCProtocolDecl;
572 break;
573
574 case Decl::ObjCProperty:
575 Kind = CXCursor_ObjCPropertyDecl;
576 break;
577
578 case Decl::ObjCIvar:
579 Kind = CXCursor_ObjCIvarDecl;
580 break;
581
582 case Decl::ObjCImplementation:
Douglas Gregorb6998662010-01-19 19:34:47 +0000583 Kind = CXCursor_ObjCImplementationDecl;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000584 break;
585
586 case Decl::ObjCCategoryImpl:
Douglas Gregorb6998662010-01-19 19:34:47 +0000587 Kind = CXCursor_ObjCCategoryImplDecl;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000588 break;
589
590 default:
591 break;
592 }
593 break;
Douglas Gregor09d9fa12010-04-05 16:10:30 +0000594
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000595 case Result::RK_Macro:
Douglas Gregor09d9fa12010-04-05 16:10:30 +0000596 Kind = CXCursor_MacroDefinition;
597 break;
598
599 case Result::RK_Keyword:
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000600 case Result::RK_Pattern:
601 Kind = CXCursor_NotImplemented;
602 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000603 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000604
605 WriteUnsigned(OS, Kind);
Douglas Gregor12e13132010-05-26 22:00:08 +0000606 WriteUnsigned(OS, Results[I].Priority);
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000607 CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(SemaRef);
608 assert(CCS && "No code-completion string?");
609 CCS->Serialize(OS);
610 delete CCS;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000611 }
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000612}
613
614void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000615CIndexCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
616 unsigned CurrentArg,
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000617 OverloadCandidate *Candidates,
618 unsigned NumCandidates) {
619 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000620 WriteUnsigned(OS, CXCursor_NotImplemented);
Douglas Gregor12e13132010-05-26 22:00:08 +0000621 WriteUnsigned(OS, /*Priority=*/0);
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000622 CodeCompletionString *CCS
623 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef);
624 assert(CCS && "No code-completion string?");
625 CCS->Serialize(OS);
626 delete CCS;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000627 }
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000628}