blob: 0a00b4226d8118287c8928e848e5e75559cfa589 [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"
Douglas Gregor56c2dbc2009-09-18 17:54:00 +000014#include "clang/AST/DeclCXX.h"
Douglas Gregorf45b0cf2009-09-18 15:37:17 +000015#include "clang/Parse/Scope.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000016#include "clang/Lex/Preprocessor.h"
Douglas Gregorf09935f2009-12-01 05:55:20 +000017#include "clang-c/Index.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000018#include "Sema.h"
19#include "llvm/ADT/STLExtras.h"
Douglas Gregor9eb77012009-11-07 00:00:49 +000020#include "llvm/ADT/StringSwitch.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000021#include "llvm/Support/raw_ostream.h"
22#include <algorithm>
Douglas Gregorfedc3282009-09-18 22:15:54 +000023#include <cstring>
24#include <functional>
Douglas Gregorab6ccb52009-11-17 16:43:05 +000025
Douglas Gregor2436e712009-09-17 21:32:03 +000026using namespace clang;
Douglas Gregorab6ccb52009-11-17 16:43:05 +000027using llvm::StringRef;
Douglas Gregor2436e712009-09-17 21:32:03 +000028
Douglas Gregorfedc3282009-09-18 22:15:54 +000029//===----------------------------------------------------------------------===//
30// Code completion string implementation
31//===----------------------------------------------------------------------===//
Douglas Gregor9eb77012009-11-07 00:00:49 +000032CodeCompletionString::Chunk::Chunk(ChunkKind Kind, llvm::StringRef Text)
Daniel Dunbarb0a19422009-11-12 18:40:12 +000033 : Kind(Kind), Text("")
Douglas Gregor5bf52692009-09-22 23:15:58 +000034{
Douglas Gregor9eb77012009-11-07 00:00:49 +000035 switch (Kind) {
36 case CK_TypedText:
37 case CK_Text:
38 case CK_Placeholder:
39 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +000040 case CK_ResultType:
Douglas Gregor9eb77012009-11-07 00:00:49 +000041 case CK_CurrentParameter: {
42 char *New = new char [Text.size() + 1];
43 std::memcpy(New, Text.data(), Text.size());
44 New[Text.size()] = '\0';
45 this->Text = New;
46 break;
47 }
48
49 case CK_Optional:
Jeffrey Yasskin1615d452009-12-12 05:05:38 +000050 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor9eb77012009-11-07 00:00:49 +000051 break;
52
53 case CK_LeftParen:
54 this->Text = "(";
55 break;
56
57 case CK_RightParen:
58 this->Text = ")";
59 break;
60
61 case CK_LeftBracket:
62 this->Text = "[";
63 break;
64
65 case CK_RightBracket:
66 this->Text = "]";
67 break;
68
69 case CK_LeftBrace:
70 this->Text = "{";
71 break;
72
73 case CK_RightBrace:
74 this->Text = "}";
75 break;
76
77 case CK_LeftAngle:
78 this->Text = "<";
79 break;
80
81 case CK_RightAngle:
82 this->Text = ">";
83 break;
84
85 case CK_Comma:
86 this->Text = ", ";
87 break;
Douglas Gregor504a6ae2010-01-10 23:08:15 +000088
89 case CK_Colon:
90 this->Text = ": ";
91 break;
92
93 case CK_SemiColon:
94 this->Text = ";";
95 break;
96
97 case CK_Equal:
98 this->Text = " = ";
99 break;
100
101 case CK_HorizontalSpace:
102 this->Text = " ";
103 break;
104
105 case CK_VerticalSpace:
106 this->Text = "\n";
107 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000108 }
Douglas Gregor5bf52692009-09-22 23:15:58 +0000109}
110
111CodeCompletionString::Chunk
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000112CodeCompletionString::Chunk::CreateText(StringRef Text) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000113 return Chunk(CK_Text, Text);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000114}
115
116CodeCompletionString::Chunk
117CodeCompletionString::Chunk::CreateOptional(
118 std::auto_ptr<CodeCompletionString> Optional) {
119 Chunk Result;
120 Result.Kind = CK_Optional;
121 Result.Optional = Optional.release();
122 return Result;
123}
124
125CodeCompletionString::Chunk
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000126CodeCompletionString::Chunk::CreatePlaceholder(StringRef Placeholder) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000127 return Chunk(CK_Placeholder, Placeholder);
128}
129
130CodeCompletionString::Chunk
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000131CodeCompletionString::Chunk::CreateInformative(StringRef Informative) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000132 return Chunk(CK_Informative, Informative);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000133}
134
Douglas Gregor9eb77012009-11-07 00:00:49 +0000135CodeCompletionString::Chunk
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000136CodeCompletionString::Chunk::CreateResultType(StringRef ResultType) {
137 return Chunk(CK_ResultType, ResultType);
138}
139
140CodeCompletionString::Chunk
Douglas Gregor9eb77012009-11-07 00:00:49 +0000141CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000142 StringRef CurrentParameter) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000143 return Chunk(CK_CurrentParameter, CurrentParameter);
144}
145
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000146CodeCompletionString::Chunk CodeCompletionString::Chunk::Clone() const {
147 switch (Kind) {
148 case CK_TypedText:
149 case CK_Text:
150 case CK_Placeholder:
151 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000152 case CK_ResultType:
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000153 case CK_CurrentParameter:
154 case CK_LeftParen:
155 case CK_RightParen:
156 case CK_LeftBracket:
157 case CK_RightBracket:
158 case CK_LeftBrace:
159 case CK_RightBrace:
160 case CK_LeftAngle:
161 case CK_RightAngle:
162 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000163 case CK_Colon:
164 case CK_SemiColon:
165 case CK_Equal:
166 case CK_HorizontalSpace:
167 case CK_VerticalSpace:
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000168 return Chunk(Kind, Text);
169
170 case CK_Optional: {
171 std::auto_ptr<CodeCompletionString> Opt(Optional->Clone());
172 return CreateOptional(Opt);
173 }
174 }
175
176 // Silence GCC warning.
177 return Chunk();
178}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000179
Douglas Gregorfedc3282009-09-18 22:15:54 +0000180void
181CodeCompletionString::Chunk::Destroy() {
182 switch (Kind) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000183 case CK_Optional:
184 delete Optional;
185 break;
186
Douglas Gregor9eb77012009-11-07 00:00:49 +0000187 case CK_TypedText:
Douglas Gregor5bf52692009-09-22 23:15:58 +0000188 case CK_Text:
189 case CK_Placeholder:
190 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000191 case CK_ResultType:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000192 case CK_CurrentParameter:
193 delete [] Text;
194 break;
195
196 case CK_LeftParen:
197 case CK_RightParen:
198 case CK_LeftBracket:
199 case CK_RightBracket:
200 case CK_LeftBrace:
201 case CK_RightBrace:
202 case CK_LeftAngle:
203 case CK_RightAngle:
204 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000205 case CK_Colon:
206 case CK_SemiColon:
207 case CK_Equal:
208 case CK_HorizontalSpace:
209 case CK_VerticalSpace:
Douglas Gregor5bf52692009-09-22 23:15:58 +0000210 break;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000211 }
212}
213
214CodeCompletionString::~CodeCompletionString() {
215 std::for_each(Chunks.begin(), Chunks.end(),
216 std::mem_fun_ref(&Chunk::Destroy));
217}
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 Gregorfedc3282009-09-18 22:15:54 +0000225 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000226 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000227
228 case CK_Informative:
229 case CK_ResultType:
230 OS << "[#" << C->Text << "#]";
231 break;
232
Douglas Gregor9eb77012009-11-07 00:00:49 +0000233 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
234 default: OS << C->Text; break;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000235 }
236 }
Douglas Gregor78f0fa52009-09-29 15:13:39 +0000237 OS.flush();
Douglas Gregorfedc3282009-09-18 22:15:54 +0000238 return Result;
239}
240
Douglas Gregor45f83ee2009-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 Gregor9eb77012009-11-07 00:00:49 +0000255
Douglas Gregorf09935f2009-12-01 05:55:20 +0000256static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
257 OS.write((const char *)&Value, sizeof(unsigned));
258}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000259
Douglas Gregorf09935f2009-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 Gregor9eb77012009-11-07 00:00:49 +0000268}
269
270void CodeCompletionString::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000271 // Write the number of chunks.
272 WriteUnsigned(OS, size());
273
Douglas Gregor9eb77012009-11-07 00:00:49 +0000274 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000275 WriteUnsigned(OS, C->Kind);
276
Douglas Gregor9eb77012009-11-07 00:00:49 +0000277 switch (C->Kind) {
278 case CK_TypedText:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000279 case CK_Text:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000280 case CK_Placeholder:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000281 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000282 case CK_ResultType:
Douglas Gregorf09935f2009-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 Gregor9eb77012009-11-07 00:00:49 +0000288 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000289 }
290
291 case CK_Optional:
292 C->Optional->Serialize(OS);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000293 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000294
Douglas Gregor9eb77012009-11-07 00:00:49 +0000295 case CK_LeftParen:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000296 case CK_RightParen:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000297 case CK_LeftBracket:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000298 case CK_RightBracket:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000299 case CK_LeftBrace:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000300 case CK_RightBrace:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000301 case CK_LeftAngle:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000302 case CK_RightAngle:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000303 case CK_Comma:
Douglas Gregor504a6ae2010-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 Gregor9eb77012009-11-07 00:00:49 +0000309 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000310 }
Douglas Gregor9eb77012009-11-07 00:00:49 +0000311 }
312}
313
Douglas Gregorf09935f2009-12-01 05:55:20 +0000314CodeCompletionString *CodeCompletionString::Deserialize(const char *&Str,
315 const char *StrEnd) {
316 if (Str == StrEnd || *Str == 0)
317 return 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000318
Douglas Gregor9eb77012009-11-07 00:00:49 +0000319 CodeCompletionString *Result = new CodeCompletionString;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000320 unsigned NumBlocks;
321 if (ReadUnsigned(Str, StrEnd, NumBlocks))
322 return Result;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000323
Douglas Gregorf09935f2009-12-01 05:55:20 +0000324 for (unsigned I = 0; I != NumBlocks; ++I) {
325 if (Str + 1 >= StrEnd)
326 break;
327
328 // Parse the next kind.
329 unsigned KindValue;
330 if (ReadUnsigned(Str, StrEnd, KindValue))
331 return Result;
332
333 switch (ChunkKind Kind = (ChunkKind)KindValue) {
334 case CK_TypedText:
335 case CK_Text:
336 case CK_Placeholder:
337 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000338 case CK_ResultType:
Douglas Gregorf09935f2009-12-01 05:55:20 +0000339 case CK_CurrentParameter: {
340 unsigned StrLen;
341 if (ReadUnsigned(Str, StrEnd, StrLen) || (Str + StrLen > StrEnd))
342 return Result;
343
344 Result->AddChunk(Chunk(Kind, StringRef(Str, StrLen)));
345 Str += StrLen;
346 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000347 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000348
349 case CK_Optional: {
350 std::auto_ptr<CodeCompletionString> Optional(Deserialize(Str, StrEnd));
Douglas Gregor9eb77012009-11-07 00:00:49 +0000351 Result->AddOptionalChunk(Optional);
Douglas Gregorf09935f2009-12-01 05:55:20 +0000352 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000353 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000354
355 case CK_LeftParen:
356 case CK_RightParen:
357 case CK_LeftBracket:
358 case CK_RightBracket:
359 case CK_LeftBrace:
360 case CK_RightBrace:
361 case CK_LeftAngle:
362 case CK_RightAngle:
363 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000364 case CK_Colon:
365 case CK_SemiColon:
366 case CK_Equal:
367 case CK_HorizontalSpace:
368 case CK_VerticalSpace:
Douglas Gregorf09935f2009-12-01 05:55:20 +0000369 Result->AddChunk(Chunk(Kind));
370 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000371 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000372 };
Douglas Gregor9eb77012009-11-07 00:00:49 +0000373
374 return Result;
375}
376
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000377void CodeCompleteConsumer::Result::Destroy() {
378 if (Kind == RK_Pattern) {
379 delete Pattern;
380 Pattern = 0;
381 }
382}
383
Douglas Gregorfedc3282009-09-18 22:15:54 +0000384//===----------------------------------------------------------------------===//
Douglas Gregor05f477c2009-09-23 00:16:58 +0000385// Code completion overload candidate implementation
386//===----------------------------------------------------------------------===//
387FunctionDecl *
388CodeCompleteConsumer::OverloadCandidate::getFunction() const {
389 if (getKind() == CK_Function)
390 return Function;
391 else if (getKind() == CK_FunctionTemplate)
392 return FunctionTemplate->getTemplatedDecl();
393 else
394 return 0;
395}
396
397const FunctionType *
398CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
399 switch (Kind) {
400 case CK_Function:
401 return Function->getType()->getAs<FunctionType>();
402
403 case CK_FunctionTemplate:
404 return FunctionTemplate->getTemplatedDecl()->getType()
405 ->getAs<FunctionType>();
406
407 case CK_FunctionType:
408 return Type;
409 }
410
411 return 0;
412}
413
414//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +0000415// Code completion consumer implementation
416//===----------------------------------------------------------------------===//
417
Douglas Gregor3545ff42009-09-21 16:56:56 +0000418CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregorfedc3282009-09-18 22:15:54 +0000419
Douglas Gregor2436e712009-09-17 21:32:03 +0000420void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000421PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
422 Result *Results,
Douglas Gregor2436e712009-09-17 21:32:03 +0000423 unsigned NumResults) {
Douglas Gregor2436e712009-09-17 21:32:03 +0000424 // Print the results.
425 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregor58acf322009-10-09 22:16:47 +0000426 OS << "COMPLETION: ";
Douglas Gregor2436e712009-09-17 21:32:03 +0000427 switch (Results[I].Kind) {
428 case Result::RK_Declaration:
429 OS << Results[I].Declaration->getNameAsString() << " : "
430 << Results[I].Rank;
431 if (Results[I].Hidden)
432 OS << " (Hidden)";
Douglas Gregor3545ff42009-09-21 16:56:56 +0000433 if (CodeCompletionString *CCS
434 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000435 OS << " : " << CCS->getAsString();
436 delete CCS;
437 }
438
Douglas Gregor2436e712009-09-17 21:32:03 +0000439 OS << '\n';
440 break;
441
442 case Result::RK_Keyword:
443 OS << Results[I].Keyword << " : " << Results[I].Rank << '\n';
444 break;
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000445
446 case Result::RK_Macro: {
447 OS << Results[I].Macro->getName() << " : " << Results[I].Rank;
448 if (CodeCompletionString *CCS
449 = Results[I].CreateCodeCompletionString(SemaRef)) {
450 OS << " : " << CCS->getAsString();
451 delete CCS;
452 }
453 OS << '\n';
454 break;
455 }
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000456
457 case Result::RK_Pattern: {
458 OS << "Pattern : " << Results[I].Rank << " : "
459 << Results[I].Pattern->getAsString() << '\n';
460 break;
461 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000462 }
463 }
464
465 // Once we've printed the code-completion results, suppress remaining
466 // diagnostics.
467 // FIXME: Move this somewhere else!
Douglas Gregor3545ff42009-09-21 16:56:56 +0000468 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
Douglas Gregor2436e712009-09-17 21:32:03 +0000469}
Douglas Gregor05f477c2009-09-23 00:16:58 +0000470
471void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000472PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
473 unsigned CurrentArg,
Douglas Gregor05f477c2009-09-23 00:16:58 +0000474 OverloadCandidate *Candidates,
475 unsigned NumCandidates) {
476 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregorf0f51982009-09-23 00:34:09 +0000477 if (CodeCompletionString *CCS
478 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef)) {
Douglas Gregor58acf322009-10-09 22:16:47 +0000479 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregorf0f51982009-09-23 00:34:09 +0000480 delete CCS;
Douglas Gregor05f477c2009-09-23 00:16:58 +0000481 }
482 }
483
484 // Once we've printed the code-completion results, suppress remaining
485 // diagnostics.
486 // FIXME: Move this somewhere else!
487 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
488}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000489
490void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000491CIndexCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
492 Result *Results,
Douglas Gregor9eb77012009-11-07 00:00:49 +0000493 unsigned NumResults) {
494 // Print the results.
495 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000496 CXCursorKind Kind = CXCursor_NotImplemented;
497
Douglas Gregor9eb77012009-11-07 00:00:49 +0000498 switch (Results[I].Kind) {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000499 case Result::RK_Declaration:
500 switch (Results[I].Declaration->getKind()) {
501 case Decl::Record:
502 case Decl::CXXRecord:
503 case Decl::ClassTemplateSpecialization: {
504 RecordDecl *Record = cast<RecordDecl>(Results[I].Declaration);
505 if (Record->isStruct())
506 Kind = CXCursor_StructDecl;
507 else if (Record->isUnion())
508 Kind = CXCursor_UnionDecl;
509 else
510 Kind = CXCursor_ClassDecl;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000511 break;
512 }
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000513
Douglas Gregorf09935f2009-12-01 05:55:20 +0000514 case Decl::ObjCMethod: {
515 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(Results[I].Declaration);
516 if (Method->isInstanceMethod())
517 Kind = CXCursor_ObjCInstanceMethodDecl;
518 else
519 Kind = CXCursor_ObjCClassMethodDecl;
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000520 break;
521 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000522
523 case Decl::Typedef:
524 Kind = CXCursor_TypedefDecl;
525 break;
526
527 case Decl::Enum:
528 Kind = CXCursor_EnumDecl;
529 break;
530
531 case Decl::Field:
532 Kind = CXCursor_FieldDecl;
533 break;
534
535 case Decl::EnumConstant:
536 Kind = CXCursor_EnumConstantDecl;
537 break;
538
539 case Decl::Function:
540 case Decl::CXXMethod:
541 case Decl::CXXConstructor:
542 case Decl::CXXDestructor:
543 case Decl::CXXConversion:
544 Kind = CXCursor_FunctionDecl;
545 break;
546
547 case Decl::Var:
548 Kind = CXCursor_VarDecl;
549 break;
550
551 case Decl::ParmVar:
552 Kind = CXCursor_ParmDecl;
553 break;
554
555 case Decl::ObjCInterface:
556 Kind = CXCursor_ObjCInterfaceDecl;
557 break;
558
559 case Decl::ObjCCategory:
560 Kind = CXCursor_ObjCCategoryDecl;
561 break;
562
563 case Decl::ObjCProtocol:
564 Kind = CXCursor_ObjCProtocolDecl;
565 break;
566
567 case Decl::ObjCProperty:
568 Kind = CXCursor_ObjCPropertyDecl;
569 break;
570
571 case Decl::ObjCIvar:
572 Kind = CXCursor_ObjCIvarDecl;
573 break;
574
575 case Decl::ObjCImplementation:
576 Kind = CXCursor_ObjCClassDefn;
577 break;
578
579 case Decl::ObjCCategoryImpl:
580 Kind = CXCursor_ObjCCategoryDefn;
581 break;
582
583 default:
584 break;
585 }
586 break;
587
588 case Result::RK_Keyword:
589 case Result::RK_Macro:
590 case Result::RK_Pattern:
591 Kind = CXCursor_NotImplemented;
592 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000593 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000594
595 WriteUnsigned(OS, Kind);
596 CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(SemaRef);
597 assert(CCS && "No code-completion string?");
598 CCS->Serialize(OS);
599 delete CCS;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000600 }
601
602 // Once we've printed the code-completion results, suppress remaining
603 // diagnostics.
604 // FIXME: Move this somewhere else!
605 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
606}
607
608void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000609CIndexCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
610 unsigned CurrentArg,
Douglas Gregor9eb77012009-11-07 00:00:49 +0000611 OverloadCandidate *Candidates,
612 unsigned NumCandidates) {
613 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000614 WriteUnsigned(OS, CXCursor_NotImplemented);
615 CodeCompletionString *CCS
616 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef);
617 assert(CCS && "No code-completion string?");
618 CCS->Serialize(OS);
619 delete CCS;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000620 }
621
622 // Once we've printed the code-completion results, suppress remaining
623 // diagnostics.
624 // FIXME: Move this somewhere else!
625 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
626}