blob: f1a63927690c00da621c76e45c2fa3b57329fe88 [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 Gregor577cdfd2011-02-17 00:22:45 +000022#include "llvm/ADT/Twine.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000023#include "llvm/Support/raw_ostream.h"
24#include <algorithm>
Douglas Gregore6e03612009-09-18 22:15:54 +000025#include <cstring>
26#include <functional>
Douglas Gregor92eff462009-11-17 16:43:05 +000027
Douglas Gregor81b747b2009-09-17 21:32:03 +000028using namespace clang;
29
Douglas Gregore6e03612009-09-18 22:15:54 +000030//===----------------------------------------------------------------------===//
Douglas Gregor6f942b22010-09-21 16:06:22 +000031// Code completion context implementation
32//===----------------------------------------------------------------------===//
33
34bool CodeCompletionContext::wantConstructorResults() const {
35 switch (Kind) {
Douglas Gregor52779fb2010-09-23 23:01:17 +000036 case CCC_Recovery:
Douglas Gregor6f942b22010-09-21 16:06:22 +000037 case CCC_Statement:
38 case CCC_Expression:
39 case CCC_ObjCMessageReceiver:
40 case CCC_ParenthesizedExpression:
41 return true;
42
43 case CCC_TopLevel:
44 case CCC_ObjCInterface:
45 case CCC_ObjCImplementation:
46 case CCC_ObjCIvarList:
47 case CCC_ClassStructUnion:
Douglas Gregor3da626b2011-07-07 16:03:39 +000048 case CCC_DotMemberAccess:
49 case CCC_ArrowMemberAccess:
50 case CCC_ObjCPropertyAccess:
Douglas Gregor6f942b22010-09-21 16:06:22 +000051 case CCC_EnumTag:
52 case CCC_UnionTag:
53 case CCC_ClassOrStructTag:
54 case CCC_ObjCProtocolName:
55 case CCC_Namespace:
56 case CCC_Type:
57 case CCC_Name:
58 case CCC_PotentiallyQualifiedName:
59 case CCC_MacroName:
60 case CCC_MacroNameUse:
61 case CCC_PreprocessorExpression:
62 case CCC_PreprocessorDirective:
63 case CCC_NaturalLanguage:
64 case CCC_SelectorName:
65 case CCC_TypeQualifiers:
Douglas Gregor52779fb2010-09-23 23:01:17 +000066 case CCC_Other:
Douglas Gregor5c722c702011-02-18 23:30:37 +000067 case CCC_OtherWithMacros:
Douglas Gregor3da626b2011-07-07 16:03:39 +000068 case CCC_ObjCInstanceMessage:
69 case CCC_ObjCClassMessage:
Douglas Gregor0f91c8c2011-07-30 06:55:39 +000070 case CCC_ObjCInterfaceName:
Douglas Gregor3da626b2011-07-07 16:03:39 +000071 case CCC_ObjCCategoryName:
Douglas Gregor6f942b22010-09-21 16:06:22 +000072 return false;
73 }
David Blaikie7530c032012-01-17 06:56:22 +000074
75 llvm_unreachable("Invalid CodeCompletionContext::Kind!");
Douglas Gregor6f942b22010-09-21 16:06:22 +000076}
77
78//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +000079// Code completion string implementation
80//===----------------------------------------------------------------------===//
Douglas Gregor218937c2011-02-01 19:23:04 +000081CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
Daniel Dunbarad5757f2009-11-12 18:40:12 +000082 : Kind(Kind), Text("")
Douglas Gregor0563c262009-09-22 23:15:58 +000083{
Douglas Gregor0c8296d2009-11-07 00:00:49 +000084 switch (Kind) {
85 case CK_TypedText:
86 case CK_Text:
87 case CK_Placeholder:
88 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +000089 case CK_ResultType:
Douglas Gregor218937c2011-02-01 19:23:04 +000090 case CK_CurrentParameter:
91 this->Text = Text;
Douglas Gregor0c8296d2009-11-07 00:00:49 +000092 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +000093
94 case CK_Optional:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000095 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor0c8296d2009-11-07 00:00:49 +000096
97 case CK_LeftParen:
98 this->Text = "(";
99 break;
100
101 case CK_RightParen:
102 this->Text = ")";
103 break;
104
105 case CK_LeftBracket:
106 this->Text = "[";
107 break;
108
109 case CK_RightBracket:
110 this->Text = "]";
111 break;
112
113 case CK_LeftBrace:
114 this->Text = "{";
115 break;
116
117 case CK_RightBrace:
118 this->Text = "}";
119 break;
120
121 case CK_LeftAngle:
122 this->Text = "<";
123 break;
124
125 case CK_RightAngle:
126 this->Text = ">";
127 break;
128
129 case CK_Comma:
130 this->Text = ", ";
131 break;
Douglas Gregor01dfea02010-01-10 23:08:15 +0000132
133 case CK_Colon:
Douglas Gregore8f5a172010-04-07 00:21:17 +0000134 this->Text = ":";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000135 break;
136
137 case CK_SemiColon:
138 this->Text = ";";
139 break;
140
141 case CK_Equal:
142 this->Text = " = ";
143 break;
144
145 case CK_HorizontalSpace:
146 this->Text = " ";
147 break;
148
149 case CK_VerticalSpace:
150 this->Text = "\n";
151 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000152 }
Douglas Gregor0563c262009-09-22 23:15:58 +0000153}
154
155CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000156CodeCompletionString::Chunk::CreateText(const char *Text) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000157 return Chunk(CK_Text, Text);
Douglas Gregore6e03612009-09-18 22:15:54 +0000158}
159
160CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000161CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000162 Chunk Result;
163 Result.Kind = CK_Optional;
Douglas Gregor218937c2011-02-01 19:23:04 +0000164 Result.Optional = Optional;
Douglas Gregore6e03612009-09-18 22:15:54 +0000165 return Result;
166}
167
168CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000169CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000170 return Chunk(CK_Placeholder, Placeholder);
171}
172
173CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000174CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000175 return Chunk(CK_Informative, Informative);
Douglas Gregore6e03612009-09-18 22:15:54 +0000176}
177
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000178CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000179CodeCompletionString::Chunk::CreateResultType(const char *ResultType) {
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000180 return Chunk(CK_ResultType, ResultType);
181}
182
183CodeCompletionString::Chunk
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000184CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregor218937c2011-02-01 19:23:04 +0000185 const char *CurrentParameter) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000186 return Chunk(CK_CurrentParameter, CurrentParameter);
187}
188
Douglas Gregor218937c2011-02-01 19:23:04 +0000189CodeCompletionString::CodeCompletionString(const Chunk *Chunks,
190 unsigned NumChunks,
191 unsigned Priority,
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000192 CXAvailabilityKind Availability,
193 const char **Annotations,
194 unsigned NumAnnotations)
195 : NumChunks(NumChunks), NumAnnotations(NumAnnotations)
196 , Priority(Priority), Availability(Availability)
Douglas Gregor218937c2011-02-01 19:23:04 +0000197{
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000198 assert(NumChunks <= 0xffff);
199 assert(NumAnnotations <= 0xffff);
200
Douglas Gregor218937c2011-02-01 19:23:04 +0000201 Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
202 for (unsigned I = 0; I != NumChunks; ++I)
203 StoredChunks[I] = Chunks[I];
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000204
205 const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks);
206 for (unsigned I = 0; I != NumAnnotations; ++I)
207 StoredAnnotations[I] = Annotations[I];
Douglas Gregore6e03612009-09-18 22:15:54 +0000208}
209
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000210unsigned CodeCompletionString::getAnnotationCount() const {
211 return NumAnnotations;
212}
213
214const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const {
215 if (AnnotationNr < NumAnnotations)
216 return reinterpret_cast<const char * const*>(end())[AnnotationNr];
217 else
218 return 0;
219}
220
221
Douglas Gregore6e03612009-09-18 22:15:54 +0000222std::string CodeCompletionString::getAsString() const {
223 std::string Result;
224 llvm::raw_string_ostream OS(Result);
225
226 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
227 switch (C->Kind) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000228 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor0563c262009-09-22 23:15:58 +0000229 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000230
231 case CK_Informative:
232 case CK_ResultType:
233 OS << "[#" << C->Text << "#]";
234 break;
235
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000236 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
237 default: OS << C->Text; break;
Douglas Gregore6e03612009-09-18 22:15:54 +0000238 }
239 }
Dan Gohman6bdeb402010-07-26 21:33:22 +0000240 return OS.str();
Douglas Gregore6e03612009-09-18 22:15:54 +0000241}
242
Douglas Gregor54f01612009-11-19 00:01:57 +0000243const char *CodeCompletionString::getTypedText() const {
244 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
245 if (C->Kind == CK_TypedText)
246 return C->Text;
247
248 return 0;
249}
250
Chris Lattner5f9e2722011-07-23 10:55:15 +0000251const char *CodeCompletionAllocator::CopyString(StringRef String) {
Douglas Gregordae68752011-02-01 22:57:45 +0000252 char *Mem = (char *)Allocate(String.size() + 1, 1);
253 std::copy(String.begin(), String.end(), Mem);
254 Mem[String.size()] = 0;
255 return Mem;
256}
257
Chris Lattner5f9e2722011-07-23 10:55:15 +0000258const char *CodeCompletionAllocator::CopyString(Twine String) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +0000259 // FIXME: It would be more efficient to teach Twine to tell us its size and
260 // then add a routine there to fill in an allocated char* with the contents
261 // of the string.
262 llvm::SmallString<128> Data;
263 return CopyString(String.toStringRef(Data));
264}
265
Douglas Gregor218937c2011-02-01 19:23:04 +0000266CodeCompletionString *CodeCompletionBuilder::TakeString() {
267 void *Mem = Allocator.Allocate(
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000268 sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size()
269 + sizeof(const char *) * Annotations.size(),
Douglas Gregor218937c2011-02-01 19:23:04 +0000270 llvm::alignOf<CodeCompletionString>());
271 CodeCompletionString *Result
272 = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000273 Priority, Availability,
274 Annotations.data(), Annotations.size());
Douglas Gregor218937c2011-02-01 19:23:04 +0000275 Chunks.clear();
Douglas Gregor54f01612009-11-19 00:01:57 +0000276 return Result;
277}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000278
John McCall0a2c5e22010-08-25 06:19:51 +0000279unsigned CodeCompletionResult::getPriorityFromDecl(NamedDecl *ND) {
Douglas Gregor12e13132010-05-26 22:00:08 +0000280 if (!ND)
281 return CCP_Unlikely;
282
283 // Context-based decisions.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000284 DeclContext *DC = ND->getDeclContext()->getRedeclContext();
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000285 if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC)) {
286 // _cmd is relatively rare
287 if (ImplicitParamDecl *ImplicitParam = dyn_cast<ImplicitParamDecl>(ND))
288 if (ImplicitParam->getIdentifier() &&
289 ImplicitParam->getIdentifier()->isStr("_cmd"))
290 return CCP_ObjC_cmd;
291
Douglas Gregor12e13132010-05-26 22:00:08 +0000292 return CCP_LocalDeclaration;
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000293 }
Douglas Gregor12e13132010-05-26 22:00:08 +0000294 if (DC->isRecord() || isa<ObjCContainerDecl>(DC))
295 return CCP_MemberDeclaration;
296
297 // Content-based decisions.
298 if (isa<EnumConstantDecl>(ND))
299 return CCP_Constant;
300 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
301 return CCP_Type;
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000302
Douglas Gregor12e13132010-05-26 22:00:08 +0000303 return CCP_Declaration;
304}
305
Douglas Gregore6e03612009-09-18 22:15:54 +0000306//===----------------------------------------------------------------------===//
Douglas Gregor05944382009-09-23 00:16:58 +0000307// Code completion overload candidate implementation
308//===----------------------------------------------------------------------===//
309FunctionDecl *
310CodeCompleteConsumer::OverloadCandidate::getFunction() const {
311 if (getKind() == CK_Function)
312 return Function;
313 else if (getKind() == CK_FunctionTemplate)
314 return FunctionTemplate->getTemplatedDecl();
315 else
316 return 0;
317}
318
319const FunctionType *
320CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
321 switch (Kind) {
322 case CK_Function:
323 return Function->getType()->getAs<FunctionType>();
324
325 case CK_FunctionTemplate:
326 return FunctionTemplate->getTemplatedDecl()->getType()
327 ->getAs<FunctionType>();
328
329 case CK_FunctionType:
330 return Type;
331 }
David Blaikie7530c032012-01-17 06:56:22 +0000332
333 llvm_unreachable("Invalid CandidateKind!");
Douglas Gregor05944382009-09-23 00:16:58 +0000334}
335
336//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +0000337// Code completion consumer implementation
338//===----------------------------------------------------------------------===//
339
Douglas Gregor86d9a522009-09-21 16:56:56 +0000340CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregore6e03612009-09-18 22:15:54 +0000341
Douglas Gregor81b747b2009-09-17 21:32:03 +0000342void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000343PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000344 CodeCompletionContext Context,
John McCall0a2c5e22010-08-25 06:19:51 +0000345 CodeCompletionResult *Results,
Douglas Gregor81b747b2009-09-17 21:32:03 +0000346 unsigned NumResults) {
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000347 std::stable_sort(Results, Results + NumResults);
348
Douglas Gregor81b747b2009-09-17 21:32:03 +0000349 // Print the results.
350 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000351 OS << "COMPLETION: ";
Douglas Gregor81b747b2009-09-17 21:32:03 +0000352 switch (Results[I].Kind) {
John McCall0a2c5e22010-08-25 06:19:51 +0000353 case CodeCompletionResult::RK_Declaration:
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000354 OS << *Results[I].Declaration;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000355 if (Results[I].Hidden)
356 OS << " (Hidden)";
Douglas Gregor86d9a522009-09-21 16:56:56 +0000357 if (CodeCompletionString *CCS
Douglas Gregor218937c2011-02-01 19:23:04 +0000358 = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000359 OS << " : " << CCS->getAsString();
Douglas Gregore6e03612009-09-18 22:15:54 +0000360 }
361
Douglas Gregor81b747b2009-09-17 21:32:03 +0000362 OS << '\n';
363 break;
364
John McCall0a2c5e22010-08-25 06:19:51 +0000365 case CodeCompletionResult::RK_Keyword:
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000366 OS << Results[I].Keyword << '\n';
Douglas Gregor81b747b2009-09-17 21:32:03 +0000367 break;
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000368
John McCall0a2c5e22010-08-25 06:19:51 +0000369 case CodeCompletionResult::RK_Macro: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000370 OS << Results[I].Macro->getName();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000371 if (CodeCompletionString *CCS
Douglas Gregor218937c2011-02-01 19:23:04 +0000372 = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) {
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000373 OS << " : " << CCS->getAsString();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000374 }
375 OS << '\n';
376 break;
377 }
Douglas Gregor54f01612009-11-19 00:01:57 +0000378
John McCall0a2c5e22010-08-25 06:19:51 +0000379 case CodeCompletionResult::RK_Pattern: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000380 OS << "Pattern : "
Douglas Gregor54f01612009-11-19 00:01:57 +0000381 << Results[I].Pattern->getAsString() << '\n';
382 break;
383 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000384 }
385 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000386}
Douglas Gregor05944382009-09-23 00:16:58 +0000387
388void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000389PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
390 unsigned CurrentArg,
Douglas Gregor05944382009-09-23 00:16:58 +0000391 OverloadCandidate *Candidates,
392 unsigned NumCandidates) {
393 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor86d802e2009-09-23 00:34:09 +0000394 if (CodeCompletionString *CCS
Douglas Gregor218937c2011-02-01 19:23:04 +0000395 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef,
396 Allocator)) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000397 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregor05944382009-09-23 00:16:58 +0000398 }
399 }
Douglas Gregor05944382009-09-23 00:16:58 +0000400}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000401
Erik Verbruggend1205962011-10-06 07:27:49 +0000402void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) {
Douglas Gregor87c08a52010-08-13 22:48:40 +0000403 switch (Kind) {
404 case RK_Declaration:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000405 // Set the availability based on attributes.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000406 switch (Declaration->getAvailability()) {
407 case AR_Available:
408 case AR_NotYetIntroduced:
409 Availability = CXAvailability_Available;
410 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000411
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000412 case AR_Deprecated:
413 Availability = CXAvailability_Deprecated;
414 break;
415
416 case AR_Unavailable:
417 Availability = CXAvailability_NotAvailable;
418 break;
419 }
420
Douglas Gregore8d7beb2010-09-03 23:30:36 +0000421 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
422 if (Function->isDeleted())
Douglas Gregor58ddb602010-08-23 23:00:57 +0000423 Availability = CXAvailability_NotAvailable;
Douglas Gregore8d7beb2010-09-03 23:30:36 +0000424
425 CursorKind = getCursorKindForDecl(Declaration);
Douglas Gregor375bb142011-12-27 22:43:10 +0000426 if (CursorKind == CXCursor_UnexposedDecl) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000427 // FIXME: Forward declarations of Objective-C classes and protocols
428 // are not directly exposed, but we want code completion to treat them
429 // like a definition.
Douglas Gregor375bb142011-12-27 22:43:10 +0000430 if (isa<ObjCInterfaceDecl>(Declaration))
431 CursorKind = CXCursor_ObjCInterfaceDecl;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000432 else if (isa<ObjCProtocolDecl>(Declaration))
433 CursorKind = CXCursor_ObjCProtocolDecl;
Douglas Gregor375bb142011-12-27 22:43:10 +0000434 else
435 CursorKind = CXCursor_NotImplemented;
436 }
Douglas Gregor87c08a52010-08-13 22:48:40 +0000437 break;
438
John McCall0a2c5e22010-08-25 06:19:51 +0000439 case RK_Macro:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000440 Availability = CXAvailability_Available;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000441 CursorKind = CXCursor_MacroDefinition;
442 break;
443
John McCall0a2c5e22010-08-25 06:19:51 +0000444 case RK_Keyword:
Douglas Gregor58ddb602010-08-23 23:00:57 +0000445 Availability = CXAvailability_Available;
Douglas Gregor87c08a52010-08-13 22:48:40 +0000446 CursorKind = CXCursor_NotImplemented;
447 break;
448
John McCall0a2c5e22010-08-25 06:19:51 +0000449 case RK_Pattern:
Douglas Gregor87c08a52010-08-13 22:48:40 +0000450 // Do nothing: Patterns can come with cursor kinds!
451 break;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000452 }
Erik Verbruggend1205962011-10-06 07:27:49 +0000453
454 if (!Accessible)
455 Availability = CXAvailability_NotAccessible;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000456}
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000457
Douglas Gregor721f3592010-08-25 18:41:16 +0000458/// \brief Retrieve the name that should be used to order a result.
459///
460/// If the name needs to be constructed as a string, that string will be
461/// saved into Saved and the returned StringRef will refer to it.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000462static StringRef getOrderedName(const CodeCompletionResult &R,
Douglas Gregor721f3592010-08-25 18:41:16 +0000463 std::string &Saved) {
464 switch (R.Kind) {
465 case CodeCompletionResult::RK_Keyword:
466 return R.Keyword;
467
468 case CodeCompletionResult::RK_Pattern:
469 return R.Pattern->getTypedText();
470
471 case CodeCompletionResult::RK_Macro:
472 return R.Macro->getName();
473
474 case CodeCompletionResult::RK_Declaration:
475 // Handle declarations below.
476 break;
477 }
478
479 DeclarationName Name = R.Declaration->getDeclName();
480
481 // If the name is a simple identifier (by far the common case), or a
482 // zero-argument selector, just return a reference to that identifier.
483 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
484 return Id->getName();
485 if (Name.isObjCZeroArgSelector())
486 if (IdentifierInfo *Id
487 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
488 return Id->getName();
489
490 Saved = Name.getAsString();
491 return Saved;
492}
493
494bool clang::operator<(const CodeCompletionResult &X,
495 const CodeCompletionResult &Y) {
496 std::string XSaved, YSaved;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000497 StringRef XStr = getOrderedName(X, XSaved);
498 StringRef YStr = getOrderedName(Y, YSaved);
Douglas Gregor721f3592010-08-25 18:41:16 +0000499 int cmp = XStr.compare_lower(YStr);
500 if (cmp)
501 return cmp < 0;
502
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000503 // If case-insensitive comparison fails, try case-sensitive comparison.
504 cmp = XStr.compare(YStr);
505 if (cmp)
506 return cmp < 0;
Douglas Gregor721f3592010-08-25 18:41:16 +0000507
508 return false;
509}