blob: 303647bbc1f31ffc71ebd4e4be28b42c089f8ca6 [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"
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Sema/Scope.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000015#include "clang/Sema/Sema.h"
Douglas Gregor56c2dbc2009-09-18 17:54:00 +000016#include "clang/AST/DeclCXX.h"
John McCallde6836a2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
John McCall19c1bfd2010-08-25 05:32:35 +000018#include "clang/AST/DeclTemplate.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000019#include "clang/Lex/Preprocessor.h"
Douglas Gregorf09935f2009-12-01 05:55:20 +000020#include "clang-c/Index.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000021#include "llvm/ADT/STLExtras.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000022#include "llvm/Support/raw_ostream.h"
23#include <algorithm>
Douglas Gregorfedc3282009-09-18 22:15:54 +000024#include <cstring>
25#include <functional>
Douglas Gregorab6ccb52009-11-17 16:43:05 +000026
Douglas Gregor2436e712009-09-17 21:32:03 +000027using namespace clang;
Douglas Gregorab6ccb52009-11-17 16:43:05 +000028using llvm::StringRef;
Douglas Gregor2436e712009-09-17 21:32:03 +000029
Douglas Gregorfedc3282009-09-18 22:15:54 +000030//===----------------------------------------------------------------------===//
31// Code completion string implementation
32//===----------------------------------------------------------------------===//
Douglas Gregor9eb77012009-11-07 00:00:49 +000033CodeCompletionString::Chunk::Chunk(ChunkKind Kind, llvm::StringRef Text)
Daniel Dunbarb0a19422009-11-12 18:40:12 +000034 : Kind(Kind), Text("")
Douglas Gregor5bf52692009-09-22 23:15:58 +000035{
Douglas Gregor9eb77012009-11-07 00:00:49 +000036 switch (Kind) {
37 case CK_TypedText:
38 case CK_Text:
39 case CK_Placeholder:
40 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +000041 case CK_ResultType:
Douglas Gregor9eb77012009-11-07 00:00:49 +000042 case CK_CurrentParameter: {
43 char *New = new char [Text.size() + 1];
44 std::memcpy(New, Text.data(), Text.size());
45 New[Text.size()] = '\0';
46 this->Text = New;
47 break;
48 }
49
50 case CK_Optional:
Jeffrey Yasskin1615d452009-12-12 05:05:38 +000051 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor9eb77012009-11-07 00:00:49 +000052 break;
53
54 case CK_LeftParen:
55 this->Text = "(";
56 break;
57
58 case CK_RightParen:
59 this->Text = ")";
60 break;
61
62 case CK_LeftBracket:
63 this->Text = "[";
64 break;
65
66 case CK_RightBracket:
67 this->Text = "]";
68 break;
69
70 case CK_LeftBrace:
71 this->Text = "{";
72 break;
73
74 case CK_RightBrace:
75 this->Text = "}";
76 break;
77
78 case CK_LeftAngle:
79 this->Text = "<";
80 break;
81
82 case CK_RightAngle:
83 this->Text = ">";
84 break;
85
86 case CK_Comma:
87 this->Text = ", ";
88 break;
Douglas Gregor504a6ae2010-01-10 23:08:15 +000089
90 case CK_Colon:
Douglas Gregor636a61e2010-04-07 00:21:17 +000091 this->Text = ":";
Douglas Gregor504a6ae2010-01-10 23:08:15 +000092 break;
93
94 case CK_SemiColon:
95 this->Text = ";";
96 break;
97
98 case CK_Equal:
99 this->Text = " = ";
100 break;
101
102 case CK_HorizontalSpace:
103 this->Text = " ";
104 break;
105
106 case CK_VerticalSpace:
107 this->Text = "\n";
108 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000109 }
Douglas Gregor5bf52692009-09-22 23:15:58 +0000110}
111
112CodeCompletionString::Chunk
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000113CodeCompletionString::Chunk::CreateText(StringRef Text) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000114 return Chunk(CK_Text, Text);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000115}
116
117CodeCompletionString::Chunk
118CodeCompletionString::Chunk::CreateOptional(
119 std::auto_ptr<CodeCompletionString> Optional) {
120 Chunk Result;
121 Result.Kind = CK_Optional;
122 Result.Optional = Optional.release();
123 return Result;
124}
125
126CodeCompletionString::Chunk
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000127CodeCompletionString::Chunk::CreatePlaceholder(StringRef Placeholder) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000128 return Chunk(CK_Placeholder, Placeholder);
129}
130
131CodeCompletionString::Chunk
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000132CodeCompletionString::Chunk::CreateInformative(StringRef Informative) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000133 return Chunk(CK_Informative, Informative);
Douglas Gregorfedc3282009-09-18 22:15:54 +0000134}
135
Douglas Gregor9eb77012009-11-07 00:00:49 +0000136CodeCompletionString::Chunk
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000137CodeCompletionString::Chunk::CreateResultType(StringRef ResultType) {
138 return Chunk(CK_ResultType, ResultType);
139}
140
141CodeCompletionString::Chunk
Douglas Gregor9eb77012009-11-07 00:00:49 +0000142CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregorab6ccb52009-11-17 16:43:05 +0000143 StringRef CurrentParameter) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000144 return Chunk(CK_CurrentParameter, CurrentParameter);
145}
146
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000147CodeCompletionString::Chunk CodeCompletionString::Chunk::Clone() const {
148 switch (Kind) {
149 case CK_TypedText:
150 case CK_Text:
151 case CK_Placeholder:
152 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000153 case CK_ResultType:
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000154 case CK_CurrentParameter:
155 case CK_LeftParen:
156 case CK_RightParen:
157 case CK_LeftBracket:
158 case CK_RightBracket:
159 case CK_LeftBrace:
160 case CK_RightBrace:
161 case CK_LeftAngle:
162 case CK_RightAngle:
163 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000164 case CK_Colon:
165 case CK_SemiColon:
166 case CK_Equal:
167 case CK_HorizontalSpace:
168 case CK_VerticalSpace:
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000169 return Chunk(Kind, Text);
170
171 case CK_Optional: {
172 std::auto_ptr<CodeCompletionString> Opt(Optional->Clone());
173 return CreateOptional(Opt);
174 }
175 }
176
177 // Silence GCC warning.
178 return Chunk();
179}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000180
Douglas Gregorfedc3282009-09-18 22:15:54 +0000181void
182CodeCompletionString::Chunk::Destroy() {
183 switch (Kind) {
Douglas Gregor5bf52692009-09-22 23:15:58 +0000184 case CK_Optional:
185 delete Optional;
186 break;
187
Douglas Gregor9eb77012009-11-07 00:00:49 +0000188 case CK_TypedText:
Douglas Gregor5bf52692009-09-22 23:15:58 +0000189 case CK_Text:
190 case CK_Placeholder:
191 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000192 case CK_ResultType:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000193 case CK_CurrentParameter:
194 delete [] Text;
195 break;
196
197 case CK_LeftParen:
198 case CK_RightParen:
199 case CK_LeftBracket:
200 case CK_RightBracket:
201 case CK_LeftBrace:
202 case CK_RightBrace:
203 case CK_LeftAngle:
204 case CK_RightAngle:
205 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000206 case CK_Colon:
207 case CK_SemiColon:
208 case CK_Equal:
209 case CK_HorizontalSpace:
210 case CK_VerticalSpace:
Douglas Gregor5bf52692009-09-22 23:15:58 +0000211 break;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000212 }
213}
214
Douglas Gregora2db7932010-05-26 22:00:08 +0000215void CodeCompletionString::clear() {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000216 std::for_each(Chunks.begin(), Chunks.end(),
217 std::mem_fun_ref(&Chunk::Destroy));
Douglas Gregora2db7932010-05-26 22:00:08 +0000218 Chunks.clear();
Douglas Gregorfedc3282009-09-18 22:15:54 +0000219}
220
221std::string CodeCompletionString::getAsString() const {
222 std::string Result;
223 llvm::raw_string_ostream OS(Result);
224
225 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
226 switch (C->Kind) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000227 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor5bf52692009-09-22 23:15:58 +0000228 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000229
230 case CK_Informative:
231 case CK_ResultType:
232 OS << "[#" << C->Text << "#]";
233 break;
234
Douglas Gregor9eb77012009-11-07 00:00:49 +0000235 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
236 default: OS << C->Text; break;
Douglas Gregorfedc3282009-09-18 22:15:54 +0000237 }
238 }
Dan Gohman4888f1a2010-07-26 21:33:22 +0000239 return OS.str();
Douglas Gregorfedc3282009-09-18 22:15:54 +0000240}
241
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000242const char *CodeCompletionString::getTypedText() const {
243 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
244 if (C->Kind == CK_TypedText)
245 return C->Text;
246
247 return 0;
248}
249
Douglas Gregor8e984da2010-08-04 16:47:14 +0000250CodeCompletionString *
251CodeCompletionString::Clone(CodeCompletionString *Result) const {
252 if (!Result)
253 Result = new CodeCompletionString;
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000254 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
255 Result->AddChunk(C->Clone());
256 return Result;
257}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000258
Douglas Gregorf09935f2009-12-01 05:55:20 +0000259static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
260 OS.write((const char *)&Value, sizeof(unsigned));
261}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000262
Douglas Gregorf09935f2009-12-01 05:55:20 +0000263static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd,
264 unsigned &Value) {
265 if (Memory + sizeof(unsigned) > MemoryEnd)
266 return true;
267
268 memmove(&Value, Memory, sizeof(unsigned));
269 Memory += sizeof(unsigned);
270 return false;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000271}
272
273void CodeCompletionString::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000274 // Write the number of chunks.
275 WriteUnsigned(OS, size());
276
Douglas Gregor9eb77012009-11-07 00:00:49 +0000277 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000278 WriteUnsigned(OS, C->Kind);
279
Douglas Gregor9eb77012009-11-07 00:00:49 +0000280 switch (C->Kind) {
281 case CK_TypedText:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000282 case CK_Text:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000283 case CK_Placeholder:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000284 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000285 case CK_ResultType:
Douglas Gregorf09935f2009-12-01 05:55:20 +0000286 case CK_CurrentParameter: {
287 const char *Text = C->Text;
288 unsigned StrLen = strlen(Text);
289 WriteUnsigned(OS, StrLen);
290 OS.write(Text, StrLen);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000291 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000292 }
293
294 case CK_Optional:
295 C->Optional->Serialize(OS);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000296 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000297
Douglas Gregor9eb77012009-11-07 00:00:49 +0000298 case CK_LeftParen:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000299 case CK_RightParen:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000300 case CK_LeftBracket:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000301 case CK_RightBracket:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000302 case CK_LeftBrace:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000303 case CK_RightBrace:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000304 case CK_LeftAngle:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000305 case CK_RightAngle:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000306 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000307 case CK_Colon:
308 case CK_SemiColon:
309 case CK_Equal:
310 case CK_HorizontalSpace:
311 case CK_VerticalSpace:
Douglas Gregor9eb77012009-11-07 00:00:49 +0000312 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000313 }
Douglas Gregor9eb77012009-11-07 00:00:49 +0000314 }
315}
316
Douglas Gregora2db7932010-05-26 22:00:08 +0000317bool CodeCompletionString::Deserialize(const char *&Str, const char *StrEnd) {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000318 if (Str == StrEnd || *Str == 0)
Douglas Gregora2db7932010-05-26 22:00:08 +0000319 return false;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000320
Douglas Gregorf09935f2009-12-01 05:55:20 +0000321 unsigned NumBlocks;
322 if (ReadUnsigned(Str, StrEnd, NumBlocks))
Douglas Gregora2db7932010-05-26 22:00:08 +0000323 return false;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000324
Douglas Gregorf09935f2009-12-01 05:55:20 +0000325 for (unsigned I = 0; I != NumBlocks; ++I) {
326 if (Str + 1 >= StrEnd)
327 break;
328
329 // Parse the next kind.
330 unsigned KindValue;
331 if (ReadUnsigned(Str, StrEnd, KindValue))
Douglas Gregora2db7932010-05-26 22:00:08 +0000332 return false;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000333
334 switch (ChunkKind Kind = (ChunkKind)KindValue) {
335 case CK_TypedText:
336 case CK_Text:
337 case CK_Placeholder:
338 case CK_Informative:
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000339 case CK_ResultType:
Douglas Gregorf09935f2009-12-01 05:55:20 +0000340 case CK_CurrentParameter: {
341 unsigned StrLen;
342 if (ReadUnsigned(Str, StrEnd, StrLen) || (Str + StrLen > StrEnd))
Douglas Gregora2db7932010-05-26 22:00:08 +0000343 return false;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000344
Douglas Gregora2db7932010-05-26 22:00:08 +0000345 AddChunk(Chunk(Kind, StringRef(Str, StrLen)));
Douglas Gregorf09935f2009-12-01 05:55:20 +0000346 Str += StrLen;
347 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000348 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000349
350 case CK_Optional: {
Douglas Gregora2db7932010-05-26 22:00:08 +0000351 std::auto_ptr<CodeCompletionString> Optional(new CodeCompletionString());
352 if (Optional->Deserialize(Str, StrEnd))
353 AddOptionalChunk(Optional);
Douglas Gregorf09935f2009-12-01 05:55:20 +0000354 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000355 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000356
357 case CK_LeftParen:
358 case CK_RightParen:
359 case CK_LeftBracket:
360 case CK_RightBracket:
361 case CK_LeftBrace:
362 case CK_RightBrace:
363 case CK_LeftAngle:
364 case CK_RightAngle:
365 case CK_Comma:
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000366 case CK_Colon:
367 case CK_SemiColon:
368 case CK_Equal:
369 case CK_HorizontalSpace:
370 case CK_VerticalSpace:
Douglas Gregora2db7932010-05-26 22:00:08 +0000371 AddChunk(Chunk(Kind));
Douglas Gregorf09935f2009-12-01 05:55:20 +0000372 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000373 }
Douglas Gregorf09935f2009-12-01 05:55:20 +0000374 };
Douglas Gregor9eb77012009-11-07 00:00:49 +0000375
Douglas Gregora2db7932010-05-26 22:00:08 +0000376 return true;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000377}
378
John McCall276321a2010-08-25 06:19:51 +0000379void CodeCompletionResult::Destroy() {
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000380 if (Kind == RK_Pattern) {
381 delete Pattern;
382 Pattern = 0;
383 }
384}
385
John McCall276321a2010-08-25 06:19:51 +0000386unsigned CodeCompletionResult::getPriorityFromDecl(NamedDecl *ND) {
Douglas Gregora2db7932010-05-26 22:00:08 +0000387 if (!ND)
388 return CCP_Unlikely;
389
390 // Context-based decisions.
391 DeclContext *DC = ND->getDeclContext()->getLookupContext();
392 if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC))
393 return CCP_LocalDeclaration;
394 if (DC->isRecord() || isa<ObjCContainerDecl>(DC))
395 return CCP_MemberDeclaration;
396
397 // Content-based decisions.
398 if (isa<EnumConstantDecl>(ND))
399 return CCP_Constant;
400 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
401 return CCP_Type;
402 return CCP_Declaration;
403}
404
Douglas Gregorfedc3282009-09-18 22:15:54 +0000405//===----------------------------------------------------------------------===//
Douglas Gregor05f477c2009-09-23 00:16:58 +0000406// Code completion overload candidate implementation
407//===----------------------------------------------------------------------===//
408FunctionDecl *
409CodeCompleteConsumer::OverloadCandidate::getFunction() const {
410 if (getKind() == CK_Function)
411 return Function;
412 else if (getKind() == CK_FunctionTemplate)
413 return FunctionTemplate->getTemplatedDecl();
414 else
415 return 0;
416}
417
418const FunctionType *
419CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
420 switch (Kind) {
421 case CK_Function:
422 return Function->getType()->getAs<FunctionType>();
423
424 case CK_FunctionTemplate:
425 return FunctionTemplate->getTemplatedDecl()->getType()
426 ->getAs<FunctionType>();
427
428 case CK_FunctionType:
429 return Type;
430 }
431
432 return 0;
433}
434
435//===----------------------------------------------------------------------===//
Douglas Gregorfedc3282009-09-18 22:15:54 +0000436// Code completion consumer implementation
437//===----------------------------------------------------------------------===//
438
Douglas Gregor3545ff42009-09-21 16:56:56 +0000439CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregorfedc3282009-09-18 22:15:54 +0000440
Douglas Gregor2436e712009-09-17 21:32:03 +0000441void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000442PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000443 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +0000444 CodeCompletionResult *Results,
Douglas Gregor2436e712009-09-17 21:32:03 +0000445 unsigned NumResults) {
Douglas Gregor2436e712009-09-17 21:32:03 +0000446 // Print the results.
447 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregor58acf322009-10-09 22:16:47 +0000448 OS << "COMPLETION: ";
Douglas Gregor2436e712009-09-17 21:32:03 +0000449 switch (Results[I].Kind) {
John McCall276321a2010-08-25 06:19:51 +0000450 case CodeCompletionResult::RK_Declaration:
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000451 OS << Results[I].Declaration;
Douglas Gregor2436e712009-09-17 21:32:03 +0000452 if (Results[I].Hidden)
453 OS << " (Hidden)";
Douglas Gregor3545ff42009-09-21 16:56:56 +0000454 if (CodeCompletionString *CCS
455 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000456 OS << " : " << CCS->getAsString();
457 delete CCS;
458 }
459
Douglas Gregor2436e712009-09-17 21:32:03 +0000460 OS << '\n';
461 break;
462
John McCall276321a2010-08-25 06:19:51 +0000463 case CodeCompletionResult::RK_Keyword:
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000464 OS << Results[I].Keyword << '\n';
Douglas Gregor2436e712009-09-17 21:32:03 +0000465 break;
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000466
John McCall276321a2010-08-25 06:19:51 +0000467 case CodeCompletionResult::RK_Macro: {
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000468 OS << Results[I].Macro->getName();
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000469 if (CodeCompletionString *CCS
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000470 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregorf329c7c2009-10-30 16:50:04 +0000471 OS << " : " << CCS->getAsString();
472 delete CCS;
473 }
474 OS << '\n';
475 break;
476 }
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000477
John McCall276321a2010-08-25 06:19:51 +0000478 case CodeCompletionResult::RK_Pattern: {
Douglas Gregor52ce62f2010-01-13 23:24:38 +0000479 OS << "Pattern : "
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000480 << Results[I].Pattern->getAsString() << '\n';
481 break;
482 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000483 }
484 }
Douglas Gregor2436e712009-09-17 21:32:03 +0000485}
Douglas Gregor05f477c2009-09-23 00:16:58 +0000486
487void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000488PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
489 unsigned CurrentArg,
Douglas Gregor05f477c2009-09-23 00:16:58 +0000490 OverloadCandidate *Candidates,
491 unsigned NumCandidates) {
492 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregorf0f51982009-09-23 00:34:09 +0000493 if (CodeCompletionString *CCS
494 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef)) {
Douglas Gregor58acf322009-10-09 22:16:47 +0000495 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregorf0f51982009-09-23 00:34:09 +0000496 delete CCS;
Douglas Gregor05f477c2009-09-23 00:16:58 +0000497 }
498 }
Douglas Gregor05f477c2009-09-23 00:16:58 +0000499}
Douglas Gregor9eb77012009-11-07 00:00:49 +0000500
John McCall276321a2010-08-25 06:19:51 +0000501void CodeCompletionResult::computeCursorKindAndAvailability() {
Douglas Gregorb14904c2010-08-13 22:48:40 +0000502 switch (Kind) {
503 case RK_Declaration:
Douglas Gregorf757a122010-08-23 23:00:57 +0000504 // Set the availability based on attributes.
505 Availability = CXAvailability_Available;
506 if (Declaration->getAttr<UnavailableAttr>())
507 Availability = CXAvailability_NotAvailable;
508 else if (Declaration->getAttr<DeprecatedAttr>())
509 Availability = CXAvailability_Deprecated;
510
Douglas Gregorb14904c2010-08-13 22:48:40 +0000511 switch (Declaration->getKind()) {
512 case Decl::Record:
513 case Decl::CXXRecord:
514 case Decl::ClassTemplateSpecialization: {
515 RecordDecl *Record = cast<RecordDecl>(Declaration);
516 if (Record->isStruct())
517 CursorKind = CXCursor_StructDecl;
518 else if (Record->isUnion())
519 CursorKind = CXCursor_UnionDecl;
520 else
521 CursorKind = CXCursor_ClassDecl;
522 break;
523 }
524
525 case Decl::ObjCMethod: {
526 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(Declaration);
527 if (Method->isInstanceMethod())
528 CursorKind = CXCursor_ObjCInstanceMethodDecl;
529 else
530 CursorKind = CXCursor_ObjCClassMethodDecl;
531 break;
532 }
533
534 case Decl::Typedef:
535 CursorKind = CXCursor_TypedefDecl;
536 break;
Douglas Gregor45f83ee2009-11-19 00:01:57 +0000537
Douglas Gregorb14904c2010-08-13 22:48:40 +0000538 case Decl::Enum:
539 CursorKind = CXCursor_EnumDecl;
540 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000541
Douglas Gregorb14904c2010-08-13 22:48:40 +0000542 case Decl::Field:
543 CursorKind = CXCursor_FieldDecl;
544 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000545
Douglas Gregorb14904c2010-08-13 22:48:40 +0000546 case Decl::EnumConstant:
547 CursorKind = CXCursor_EnumConstantDecl;
548 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000549
Douglas Gregorb14904c2010-08-13 22:48:40 +0000550 case Decl::Function:
551 case Decl::CXXMethod:
552 case Decl::CXXConstructor:
553 case Decl::CXXDestructor:
554 case Decl::CXXConversion:
555 CursorKind = CXCursor_FunctionDecl;
Douglas Gregorf757a122010-08-23 23:00:57 +0000556 if (cast<FunctionDecl>(Declaration)->isDeleted())
557 Availability = CXAvailability_NotAvailable;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000558 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000559
Douglas Gregorb14904c2010-08-13 22:48:40 +0000560 case Decl::Var:
561 CursorKind = CXCursor_VarDecl;
562 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000563
Douglas Gregorb14904c2010-08-13 22:48:40 +0000564 case Decl::ParmVar:
565 CursorKind = CXCursor_ParmDecl;
566 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000567
Douglas Gregorb14904c2010-08-13 22:48:40 +0000568 case Decl::ObjCInterface:
569 CursorKind = CXCursor_ObjCInterfaceDecl;
570 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000571
Douglas Gregorb14904c2010-08-13 22:48:40 +0000572 case Decl::ObjCCategory:
573 CursorKind = CXCursor_ObjCCategoryDecl;
574 break;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000575
Douglas Gregorb14904c2010-08-13 22:48:40 +0000576 case Decl::ObjCProtocol:
577 CursorKind = CXCursor_ObjCProtocolDecl;
Douglas Gregorf09935f2009-12-01 05:55:20 +0000578 break;
Douglas Gregorf3df9852010-04-05 16:10:30 +0000579
Douglas Gregorb14904c2010-08-13 22:48:40 +0000580 case Decl::ObjCProperty:
581 CursorKind = CXCursor_ObjCPropertyDecl;
582 break;
583
584 case Decl::ObjCIvar:
585 CursorKind = CXCursor_ObjCIvarDecl;
586 break;
587
588 case Decl::ObjCImplementation:
589 CursorKind = CXCursor_ObjCImplementationDecl;
590 break;
591
592 case Decl::ObjCCategoryImpl:
593 CursorKind = CXCursor_ObjCCategoryImplDecl;
594 break;
595
596 default:
597 CursorKind = CXCursor_NotImplemented;
598 break;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000599 }
Douglas Gregorb14904c2010-08-13 22:48:40 +0000600 break;
601
John McCall276321a2010-08-25 06:19:51 +0000602 case RK_Macro:
Douglas Gregorf757a122010-08-23 23:00:57 +0000603 Availability = CXAvailability_Available;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000604 CursorKind = CXCursor_MacroDefinition;
605 break;
606
John McCall276321a2010-08-25 06:19:51 +0000607 case RK_Keyword:
Douglas Gregorf757a122010-08-23 23:00:57 +0000608 Availability = CXAvailability_Available;
Douglas Gregorb14904c2010-08-13 22:48:40 +0000609 CursorKind = CXCursor_NotImplemented;
610 break;
611
John McCall276321a2010-08-25 06:19:51 +0000612 case RK_Pattern:
Douglas Gregorb14904c2010-08-13 22:48:40 +0000613 // Do nothing: Patterns can come with cursor kinds!
614 break;
Douglas Gregor8e984da2010-08-04 16:47:14 +0000615 }
616}
Douglas Gregorf09935f2009-12-01 05:55:20 +0000617
Douglas Gregor8e984da2010-08-04 16:47:14 +0000618void
619CIndexCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000620 CodeCompletionContext Context,
John McCall276321a2010-08-25 06:19:51 +0000621 CodeCompletionResult *Results,
Douglas Gregor8e984da2010-08-04 16:47:14 +0000622 unsigned NumResults) {
623 // Print the results.
624 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorb14904c2010-08-13 22:48:40 +0000625 WriteUnsigned(OS, Results[I].CursorKind);
Douglas Gregora2db7932010-05-26 22:00:08 +0000626 WriteUnsigned(OS, Results[I].Priority);
Douglas Gregorf757a122010-08-23 23:00:57 +0000627 WriteUnsigned(OS, Results[I].Availability);
Douglas Gregorf09935f2009-12-01 05:55:20 +0000628 CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(SemaRef);
629 assert(CCS && "No code-completion string?");
630 CCS->Serialize(OS);
631 delete CCS;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000632 }
Douglas Gregor9eb77012009-11-07 00:00:49 +0000633}
634
635void
Daniel Dunbar242ea9a2009-11-13 08:58:20 +0000636CIndexCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
637 unsigned CurrentArg,
Douglas Gregor9eb77012009-11-07 00:00:49 +0000638 OverloadCandidate *Candidates,
639 unsigned NumCandidates) {
640 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregorf09935f2009-12-01 05:55:20 +0000641 WriteUnsigned(OS, CXCursor_NotImplemented);
Douglas Gregora2db7932010-05-26 22:00:08 +0000642 WriteUnsigned(OS, /*Priority=*/0);
Douglas Gregorf757a122010-08-23 23:00:57 +0000643 WriteUnsigned(OS, /*Availability=*/CXAvailability_Available);
Douglas Gregorf09935f2009-12-01 05:55:20 +0000644 CodeCompletionString *CCS
645 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef);
646 assert(CCS && "No code-completion string?");
647 CCS->Serialize(OS);
648 delete CCS;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000649 }
Douglas Gregor9eb77012009-11-07 00:00:49 +0000650}