blob: 43451235589e1579269dea12fa1656ec752d49e3 [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"
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Sema/Scope.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Sema.h"
Douglas Gregor75b71282009-09-18 17:54:00 +000016#include "clang/AST/DeclCXX.h"
John McCall7cd088e2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
John McCall2a7fb272010-08-25 05:32:35 +000018#include "clang/AST/DeclTemplate.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000019#include "clang/Lex/Preprocessor.h"
Douglas Gregor2b4074f2009-12-01 05:55:20 +000020#include "clang-c/Index.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000021#include "llvm/ADT/STLExtras.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000022#include "llvm/Support/raw_ostream.h"
23#include <algorithm>
Douglas Gregore6e03612009-09-18 22:15:54 +000024#include <cstring>
25#include <functional>
Douglas Gregor92eff462009-11-17 16:43:05 +000026
Douglas Gregor81b747b2009-09-17 21:32:03 +000027using namespace clang;
Douglas Gregor92eff462009-11-17 16:43:05 +000028using llvm::StringRef;
Douglas Gregor81b747b2009-09-17 21:32:03 +000029
Douglas Gregore6e03612009-09-18 22:15:54 +000030//===----------------------------------------------------------------------===//
31// Code completion string implementation
32//===----------------------------------------------------------------------===//
Douglas Gregor0c8296d2009-11-07 00:00:49 +000033CodeCompletionString::Chunk::Chunk(ChunkKind Kind, llvm::StringRef Text)
Daniel Dunbarad5757f2009-11-12 18:40:12 +000034 : Kind(Kind), Text("")
Douglas Gregor0563c262009-09-22 23:15:58 +000035{
Douglas Gregor0c8296d2009-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 Gregorff5ce6e2009-12-18 18:53:37 +000041 case CK_ResultType:
Douglas Gregor0c8296d2009-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 Yasskin9f61aa92009-12-12 05:05:38 +000051 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor0c8296d2009-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 Gregor01dfea02010-01-10 23:08:15 +000089
90 case CK_Colon:
Douglas Gregore8f5a172010-04-07 00:21:17 +000091 this->Text = ":";
Douglas Gregor01dfea02010-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 Gregor0c8296d2009-11-07 00:00:49 +0000109 }
Douglas Gregor0563c262009-09-22 23:15:58 +0000110}
111
112CodeCompletionString::Chunk
Douglas Gregor92eff462009-11-17 16:43:05 +0000113CodeCompletionString::Chunk::CreateText(StringRef Text) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000114 return Chunk(CK_Text, Text);
Douglas Gregore6e03612009-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 Gregor92eff462009-11-17 16:43:05 +0000127CodeCompletionString::Chunk::CreatePlaceholder(StringRef Placeholder) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000128 return Chunk(CK_Placeholder, Placeholder);
129}
130
131CodeCompletionString::Chunk
Douglas Gregor92eff462009-11-17 16:43:05 +0000132CodeCompletionString::Chunk::CreateInformative(StringRef Informative) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000133 return Chunk(CK_Informative, Informative);
Douglas Gregore6e03612009-09-18 22:15:54 +0000134}
135
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000136CodeCompletionString::Chunk
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000137CodeCompletionString::Chunk::CreateResultType(StringRef ResultType) {
138 return Chunk(CK_ResultType, ResultType);
139}
140
141CodeCompletionString::Chunk
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000142CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregor92eff462009-11-17 16:43:05 +0000143 StringRef CurrentParameter) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000144 return Chunk(CK_CurrentParameter, CurrentParameter);
145}
146
Douglas Gregor54f01612009-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 Gregorff5ce6e2009-12-18 18:53:37 +0000153 case CK_ResultType:
Douglas Gregor54f01612009-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 Gregor01dfea02010-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 Gregor54f01612009-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 Gregor0c8296d2009-11-07 00:00:49 +0000180
Douglas Gregore6e03612009-09-18 22:15:54 +0000181void
182CodeCompletionString::Chunk::Destroy() {
183 switch (Kind) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000184 case CK_Optional:
185 delete Optional;
186 break;
187
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000188 case CK_TypedText:
Douglas Gregor0563c262009-09-22 23:15:58 +0000189 case CK_Text:
190 case CK_Placeholder:
191 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000192 case CK_ResultType:
Douglas Gregor0c8296d2009-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 Gregor01dfea02010-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 Gregor0563c262009-09-22 23:15:58 +0000211 break;
Douglas Gregore6e03612009-09-18 22:15:54 +0000212 }
213}
214
Douglas Gregor12e13132010-05-26 22:00:08 +0000215void CodeCompletionString::clear() {
Douglas Gregore6e03612009-09-18 22:15:54 +0000216 std::for_each(Chunks.begin(), Chunks.end(),
217 std::mem_fun_ref(&Chunk::Destroy));
Douglas Gregor12e13132010-05-26 22:00:08 +0000218 Chunks.clear();
Douglas Gregore6e03612009-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 Gregore6e03612009-09-18 22:15:54 +0000227 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor0563c262009-09-22 23:15:58 +0000228 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000229
230 case CK_Informative:
231 case CK_ResultType:
232 OS << "[#" << C->Text << "#]";
233 break;
234
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000235 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
236 default: OS << C->Text; break;
Douglas Gregore6e03612009-09-18 22:15:54 +0000237 }
238 }
Dan Gohman6bdeb402010-07-26 21:33:22 +0000239 return OS.str();
Douglas Gregore6e03612009-09-18 22:15:54 +0000240}
241
Douglas Gregor54f01612009-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 Gregor1abc6bc2010-08-04 16:47:14 +0000250CodeCompletionString *
251CodeCompletionString::Clone(CodeCompletionString *Result) const {
252 if (!Result)
253 Result = new CodeCompletionString;
Douglas Gregor54f01612009-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 Gregor0c8296d2009-11-07 00:00:49 +0000258
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000259static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) {
260 OS.write((const char *)&Value, sizeof(unsigned));
261}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000262
Douglas Gregor2b4074f2009-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 Gregor0c8296d2009-11-07 00:00:49 +0000271}
272
273void CodeCompletionString::Serialize(llvm::raw_ostream &OS) const {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000274 // Write the number of chunks.
275 WriteUnsigned(OS, size());
276
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000277 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000278 WriteUnsigned(OS, C->Kind);
279
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000280 switch (C->Kind) {
281 case CK_TypedText:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000282 case CK_Text:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000283 case CK_Placeholder:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000284 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000285 case CK_ResultType:
Douglas Gregor2b4074f2009-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 Gregor0c8296d2009-11-07 00:00:49 +0000291 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000292 }
293
294 case CK_Optional:
295 C->Optional->Serialize(OS);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000296 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000297
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000298 case CK_LeftParen:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000299 case CK_RightParen:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000300 case CK_LeftBracket:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000301 case CK_RightBracket:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000302 case CK_LeftBrace:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000303 case CK_RightBrace:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000304 case CK_LeftAngle:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000305 case CK_RightAngle:
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000306 case CK_Comma:
Douglas Gregor01dfea02010-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 Gregor0c8296d2009-11-07 00:00:49 +0000312 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000313 }
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000314 }
315}
316
Douglas Gregor12e13132010-05-26 22:00:08 +0000317bool CodeCompletionString::Deserialize(const char *&Str, const char *StrEnd) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000318 if (Str == StrEnd || *Str == 0)
Douglas Gregor12e13132010-05-26 22:00:08 +0000319 return false;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000320
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000321 unsigned NumBlocks;
322 if (ReadUnsigned(Str, StrEnd, NumBlocks))
Douglas Gregor12e13132010-05-26 22:00:08 +0000323 return false;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000324
Douglas Gregor2b4074f2009-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 Gregor12e13132010-05-26 22:00:08 +0000332 return false;
Douglas Gregor2b4074f2009-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 Gregorff5ce6e2009-12-18 18:53:37 +0000339 case CK_ResultType:
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000340 case CK_CurrentParameter: {
341 unsigned StrLen;
342 if (ReadUnsigned(Str, StrEnd, StrLen) || (Str + StrLen > StrEnd))
Douglas Gregor12e13132010-05-26 22:00:08 +0000343 return false;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000344
Douglas Gregor12e13132010-05-26 22:00:08 +0000345 AddChunk(Chunk(Kind, StringRef(Str, StrLen)));
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000346 Str += StrLen;
347 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000348 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000349
350 case CK_Optional: {
Douglas Gregor12e13132010-05-26 22:00:08 +0000351 std::auto_ptr<CodeCompletionString> Optional(new CodeCompletionString());
352 if (Optional->Deserialize(Str, StrEnd))
353 AddOptionalChunk(Optional);
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000354 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000355 }
Douglas Gregor2b4074f2009-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 Gregor01dfea02010-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 Gregor12e13132010-05-26 22:00:08 +0000371 AddChunk(Chunk(Kind));
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000372 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000373 }
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000374 };
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000375
Douglas Gregor12e13132010-05-26 22:00:08 +0000376 return true;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000377}
378
John McCall0a2c5e22010-08-25 06:19:51 +0000379void CodeCompletionResult::Destroy() {
Douglas Gregor54f01612009-11-19 00:01:57 +0000380 if (Kind == RK_Pattern) {
381 delete Pattern;
382 Pattern = 0;
383 }
384}
385
John McCall0a2c5e22010-08-25 06:19:51 +0000386unsigned CodeCompletionResult::getPriorityFromDecl(NamedDecl *ND) {
Douglas Gregor12e13132010-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 Gregore6e03612009-09-18 22:15:54 +0000405//===----------------------------------------------------------------------===//
Douglas Gregor05944382009-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 Gregore6e03612009-09-18 22:15:54 +0000436// Code completion consumer implementation
437//===----------------------------------------------------------------------===//
438
Douglas Gregor86d9a522009-09-21 16:56:56 +0000439CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregore6e03612009-09-18 22:15:54 +0000440
Douglas Gregor81b747b2009-09-17 21:32:03 +0000441void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000442PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000443 CodeCompletionContext Context,
John McCall0a2c5e22010-08-25 06:19:51 +0000444 CodeCompletionResult *Results,
Douglas Gregor81b747b2009-09-17 21:32:03 +0000445 unsigned NumResults) {
Douglas Gregor81b747b2009-09-17 21:32:03 +0000446 // Print the results.
447 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000448 OS << "COMPLETION: ";
Douglas Gregor81b747b2009-09-17 21:32:03 +0000449 switch (Results[I].Kind) {
John McCall0a2c5e22010-08-25 06:19:51 +0000450 case CodeCompletionResult::RK_Declaration:
Benjamin Kramer900fc632010-04-17 09:33:03 +0000451 OS << Results[I].Declaration;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000452 if (Results[I].Hidden)
453 OS << " (Hidden)";
Douglas Gregor86d9a522009-09-21 16:56:56 +0000454 if (CodeCompletionString *CCS
455 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000456 OS << " : " << CCS->getAsString();
457 delete CCS;
458 }
459
Douglas Gregor81b747b2009-09-17 21:32:03 +0000460 OS << '\n';
461 break;
462
John McCall0a2c5e22010-08-25 06:19:51 +0000463 case CodeCompletionResult::RK_Keyword:
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000464 OS << Results[I].Keyword << '\n';
Douglas Gregor81b747b2009-09-17 21:32:03 +0000465 break;
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000466
John McCall0a2c5e22010-08-25 06:19:51 +0000467 case CodeCompletionResult::RK_Macro: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000468 OS << Results[I].Macro->getName();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000469 if (CodeCompletionString *CCS
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000470 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000471 OS << " : " << CCS->getAsString();
472 delete CCS;
473 }
474 OS << '\n';
475 break;
476 }
Douglas Gregor54f01612009-11-19 00:01:57 +0000477
John McCall0a2c5e22010-08-25 06:19:51 +0000478 case CodeCompletionResult::RK_Pattern: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000479 OS << "Pattern : "
Douglas Gregor54f01612009-11-19 00:01:57 +0000480 << Results[I].Pattern->getAsString() << '\n';
481 break;
482 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000483 }
484 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000485}
Douglas Gregor05944382009-09-23 00:16:58 +0000486
487void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000488PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
489 unsigned CurrentArg,
Douglas Gregor05944382009-09-23 00:16:58 +0000490 OverloadCandidate *Candidates,
491 unsigned NumCandidates) {
492 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor86d802e2009-09-23 00:34:09 +0000493 if (CodeCompletionString *CCS
494 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef)) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000495 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregor86d802e2009-09-23 00:34:09 +0000496 delete CCS;
Douglas Gregor05944382009-09-23 00:16:58 +0000497 }
498 }
Douglas Gregor05944382009-09-23 00:16:58 +0000499}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000500
John McCall0a2c5e22010-08-25 06:19:51 +0000501void CodeCompletionResult::computeCursorKindAndAvailability() {
Douglas Gregor87c08a52010-08-13 22:48:40 +0000502 switch (Kind) {
503 case RK_Declaration:
Douglas Gregor58ddb602010-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 Gregor87c08a52010-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 Gregor54f01612009-11-19 00:01:57 +0000537
Douglas Gregor87c08a52010-08-13 22:48:40 +0000538 case Decl::Enum:
539 CursorKind = CXCursor_EnumDecl;
540 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000541
Douglas Gregor87c08a52010-08-13 22:48:40 +0000542 case Decl::Field:
543 CursorKind = CXCursor_FieldDecl;
544 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000545
Douglas Gregor87c08a52010-08-13 22:48:40 +0000546 case Decl::EnumConstant:
547 CursorKind = CXCursor_EnumConstantDecl;
548 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000549
Douglas Gregor87c08a52010-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 Gregor58ddb602010-08-23 23:00:57 +0000556 if (cast<FunctionDecl>(Declaration)->isDeleted())
557 Availability = CXAvailability_NotAvailable;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000558 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000559
Douglas Gregor87c08a52010-08-13 22:48:40 +0000560 case Decl::Var:
561 CursorKind = CXCursor_VarDecl;
562 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000563
Douglas Gregor87c08a52010-08-13 22:48:40 +0000564 case Decl::ParmVar:
565 CursorKind = CXCursor_ParmDecl;
566 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000567
Douglas Gregor87c08a52010-08-13 22:48:40 +0000568 case Decl::ObjCInterface:
569 CursorKind = CXCursor_ObjCInterfaceDecl;
570 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000571
Douglas Gregor87c08a52010-08-13 22:48:40 +0000572 case Decl::ObjCCategory:
573 CursorKind = CXCursor_ObjCCategoryDecl;
574 break;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000575
Douglas Gregor87c08a52010-08-13 22:48:40 +0000576 case Decl::ObjCProtocol:
577 CursorKind = CXCursor_ObjCProtocolDecl;
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000578 break;
Douglas Gregor09d9fa12010-04-05 16:10:30 +0000579
Douglas Gregor87c08a52010-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 Gregor0c8296d2009-11-07 00:00:49 +0000599 }
Douglas Gregor87c08a52010-08-13 22:48:40 +0000600 break;
601
John McCall0a2c5e22010-08-25 06:19:51 +0000602 case RK_Macro:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000603 Availability = CXAvailability_Available;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000604 CursorKind = CXCursor_MacroDefinition;
605 break;
606
John McCall0a2c5e22010-08-25 06:19:51 +0000607 case RK_Keyword:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000608 Availability = CXAvailability_Available;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000609 CursorKind = CXCursor_NotImplemented;
610 break;
611
John McCall0a2c5e22010-08-25 06:19:51 +0000612 case RK_Pattern:
Douglas Gregor87c08a52010-08-13 22:48:40 +0000613 // Do nothing: Patterns can come with cursor kinds!
614 break;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000615 }
616}
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000617
Douglas Gregor721f3592010-08-25 18:41:16 +0000618/// \brief Retrieve the name that should be used to order a result.
619///
620/// If the name needs to be constructed as a string, that string will be
621/// saved into Saved and the returned StringRef will refer to it.
622static llvm::StringRef getOrderedName(const CodeCompletionResult &R,
623 std::string &Saved) {
624 switch (R.Kind) {
625 case CodeCompletionResult::RK_Keyword:
626 return R.Keyword;
627
628 case CodeCompletionResult::RK_Pattern:
629 return R.Pattern->getTypedText();
630
631 case CodeCompletionResult::RK_Macro:
632 return R.Macro->getName();
633
634 case CodeCompletionResult::RK_Declaration:
635 // Handle declarations below.
636 break;
637 }
638
639 DeclarationName Name = R.Declaration->getDeclName();
640
641 // If the name is a simple identifier (by far the common case), or a
642 // zero-argument selector, just return a reference to that identifier.
643 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
644 return Id->getName();
645 if (Name.isObjCZeroArgSelector())
646 if (IdentifierInfo *Id
647 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
648 return Id->getName();
649
650 Saved = Name.getAsString();
651 return Saved;
652}
653
654bool clang::operator<(const CodeCompletionResult &X,
655 const CodeCompletionResult &Y) {
656 std::string XSaved, YSaved;
657 llvm::StringRef XStr = getOrderedName(X, XSaved);
658 llvm::StringRef YStr = getOrderedName(Y, YSaved);
659 int cmp = XStr.compare_lower(YStr);
660 if (cmp)
661 return cmp < 0;
662
663 // Non-hidden names precede hidden names.
664 if (X.Hidden != Y.Hidden)
665 return !X.Hidden;
666
667 // Non-nested-name-specifiers precede nested-name-specifiers.
668 if (X.StartsNestedNameSpecifier != Y.StartsNestedNameSpecifier)
669 return !X.StartsNestedNameSpecifier;
670
671 return false;
672}
673
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000674void
675CIndexCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000676 CodeCompletionContext Context,
John McCall0a2c5e22010-08-25 06:19:51 +0000677 CodeCompletionResult *Results,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000678 unsigned NumResults) {
679 // Print the results.
680 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregor87c08a52010-08-13 22:48:40 +0000681 WriteUnsigned(OS, Results[I].CursorKind);
Douglas Gregor12e13132010-05-26 22:00:08 +0000682 WriteUnsigned(OS, Results[I].Priority);
Douglas Gregor58ddb602010-08-23 23:00:57 +0000683 WriteUnsigned(OS, Results[I].Availability);
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000684 CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(SemaRef);
685 assert(CCS && "No code-completion string?");
686 CCS->Serialize(OS);
687 delete CCS;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000688 }
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000689}
690
691void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000692CIndexCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
693 unsigned CurrentArg,
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000694 OverloadCandidate *Candidates,
695 unsigned NumCandidates) {
696 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000697 WriteUnsigned(OS, CXCursor_NotImplemented);
Daniel Dunbar1cb237f2010-08-26 03:53:50 +0000698 WriteUnsigned(OS, /*Priority=*/0);
Douglas Gregor58ddb602010-08-23 23:00:57 +0000699 WriteUnsigned(OS, /*Availability=*/CXAvailability_Available);
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000700 CodeCompletionString *CCS
701 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef);
702 assert(CCS && "No code-completion string?");
703 CCS->Serialize(OS);
704 delete CCS;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000705 }
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000706}