blob: 7877534ff54c42b96f08ab4492f3d606b2a88dfe [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000014#include "clang-c/Index.h"
Douglas Gregor75b71282009-09-18 17:54:00 +000015#include "clang/AST/DeclCXX.h"
John McCall7cd088e2010-08-24 07:21:54 +000016#include "clang/AST/DeclObjC.h"
John McCall2a7fb272010-08-25 05:32:35 +000017#include "clang/AST/DeclTemplate.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000018#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Sema/Scope.h"
20#include "clang/Sema/Sema.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000021#include "llvm/ADT/STLExtras.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "llvm/ADT/SmallString.h"
Douglas Gregor577cdfd2011-02-17 00:22:45 +000023#include "llvm/ADT/Twine.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000024#include "llvm/Support/raw_ostream.h"
25#include <algorithm>
Douglas Gregore6e03612009-09-18 22:15:54 +000026#include <cstring>
27#include <functional>
Douglas Gregor92eff462009-11-17 16:43:05 +000028
Douglas Gregor81b747b2009-09-17 21:32:03 +000029using namespace clang;
30
Douglas Gregore6e03612009-09-18 22:15:54 +000031//===----------------------------------------------------------------------===//
Douglas Gregor6f942b22010-09-21 16:06:22 +000032// Code completion context implementation
33//===----------------------------------------------------------------------===//
34
35bool CodeCompletionContext::wantConstructorResults() const {
36 switch (Kind) {
Douglas Gregor52779fb2010-09-23 23:01:17 +000037 case CCC_Recovery:
Douglas Gregor6f942b22010-09-21 16:06:22 +000038 case CCC_Statement:
39 case CCC_Expression:
40 case CCC_ObjCMessageReceiver:
41 case CCC_ParenthesizedExpression:
42 return true;
43
44 case CCC_TopLevel:
45 case CCC_ObjCInterface:
46 case CCC_ObjCImplementation:
47 case CCC_ObjCIvarList:
48 case CCC_ClassStructUnion:
Douglas Gregor3da626b2011-07-07 16:03:39 +000049 case CCC_DotMemberAccess:
50 case CCC_ArrowMemberAccess:
51 case CCC_ObjCPropertyAccess:
Douglas Gregor6f942b22010-09-21 16:06:22 +000052 case CCC_EnumTag:
53 case CCC_UnionTag:
54 case CCC_ClassOrStructTag:
55 case CCC_ObjCProtocolName:
56 case CCC_Namespace:
57 case CCC_Type:
58 case CCC_Name:
59 case CCC_PotentiallyQualifiedName:
60 case CCC_MacroName:
61 case CCC_MacroNameUse:
62 case CCC_PreprocessorExpression:
63 case CCC_PreprocessorDirective:
64 case CCC_NaturalLanguage:
65 case CCC_SelectorName:
66 case CCC_TypeQualifiers:
Douglas Gregor52779fb2010-09-23 23:01:17 +000067 case CCC_Other:
Douglas Gregor5c722c702011-02-18 23:30:37 +000068 case CCC_OtherWithMacros:
Douglas Gregor3da626b2011-07-07 16:03:39 +000069 case CCC_ObjCInstanceMessage:
70 case CCC_ObjCClassMessage:
Douglas Gregor0f91c8c2011-07-30 06:55:39 +000071 case CCC_ObjCInterfaceName:
Douglas Gregor3da626b2011-07-07 16:03:39 +000072 case CCC_ObjCCategoryName:
Douglas Gregor6f942b22010-09-21 16:06:22 +000073 return false;
74 }
David Blaikie7530c032012-01-17 06:56:22 +000075
76 llvm_unreachable("Invalid CodeCompletionContext::Kind!");
Douglas Gregor6f942b22010-09-21 16:06:22 +000077}
78
79//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +000080// Code completion string implementation
81//===----------------------------------------------------------------------===//
Douglas Gregor218937c2011-02-01 19:23:04 +000082CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
Daniel Dunbarad5757f2009-11-12 18:40:12 +000083 : Kind(Kind), Text("")
Douglas Gregor0563c262009-09-22 23:15:58 +000084{
Douglas Gregor0c8296d2009-11-07 00:00:49 +000085 switch (Kind) {
86 case CK_TypedText:
87 case CK_Text:
88 case CK_Placeholder:
89 case CK_Informative:
Douglas Gregorff5ce6e2009-12-18 18:53:37 +000090 case CK_ResultType:
Douglas Gregor218937c2011-02-01 19:23:04 +000091 case CK_CurrentParameter:
92 this->Text = Text;
Douglas Gregor0c8296d2009-11-07 00:00:49 +000093 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +000094
95 case CK_Optional:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000096 llvm_unreachable("Optional strings cannot be created from text");
Douglas Gregor0c8296d2009-11-07 00:00:49 +000097
98 case CK_LeftParen:
99 this->Text = "(";
100 break;
101
102 case CK_RightParen:
103 this->Text = ")";
104 break;
105
106 case CK_LeftBracket:
107 this->Text = "[";
108 break;
109
110 case CK_RightBracket:
111 this->Text = "]";
112 break;
113
114 case CK_LeftBrace:
115 this->Text = "{";
116 break;
117
118 case CK_RightBrace:
119 this->Text = "}";
120 break;
121
122 case CK_LeftAngle:
123 this->Text = "<";
124 break;
125
126 case CK_RightAngle:
127 this->Text = ">";
128 break;
129
130 case CK_Comma:
131 this->Text = ", ";
132 break;
Douglas Gregor01dfea02010-01-10 23:08:15 +0000133
134 case CK_Colon:
Douglas Gregore8f5a172010-04-07 00:21:17 +0000135 this->Text = ":";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000136 break;
137
138 case CK_SemiColon:
139 this->Text = ";";
140 break;
141
142 case CK_Equal:
143 this->Text = " = ";
144 break;
145
146 case CK_HorizontalSpace:
147 this->Text = " ";
148 break;
149
150 case CK_VerticalSpace:
151 this->Text = "\n";
152 break;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000153 }
Douglas Gregor0563c262009-09-22 23:15:58 +0000154}
155
156CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000157CodeCompletionString::Chunk::CreateText(const char *Text) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000158 return Chunk(CK_Text, Text);
Douglas Gregore6e03612009-09-18 22:15:54 +0000159}
160
161CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000162CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000163 Chunk Result;
164 Result.Kind = CK_Optional;
Douglas Gregor218937c2011-02-01 19:23:04 +0000165 Result.Optional = Optional;
Douglas Gregore6e03612009-09-18 22:15:54 +0000166 return Result;
167}
168
169CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000170CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000171 return Chunk(CK_Placeholder, Placeholder);
172}
173
174CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000175CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
Douglas Gregor0563c262009-09-22 23:15:58 +0000176 return Chunk(CK_Informative, Informative);
Douglas Gregore6e03612009-09-18 22:15:54 +0000177}
178
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000179CodeCompletionString::Chunk
Douglas Gregor218937c2011-02-01 19:23:04 +0000180CodeCompletionString::Chunk::CreateResultType(const char *ResultType) {
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000181 return Chunk(CK_ResultType, ResultType);
182}
183
184CodeCompletionString::Chunk
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000185CodeCompletionString::Chunk::CreateCurrentParameter(
Douglas Gregor218937c2011-02-01 19:23:04 +0000186 const char *CurrentParameter) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000187 return Chunk(CK_CurrentParameter, CurrentParameter);
188}
189
Douglas Gregor218937c2011-02-01 19:23:04 +0000190CodeCompletionString::CodeCompletionString(const Chunk *Chunks,
191 unsigned NumChunks,
192 unsigned Priority,
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000193 CXAvailabilityKind Availability,
194 const char **Annotations,
Douglas Gregorba103062012-03-27 23:34:16 +0000195 unsigned NumAnnotations,
Dmitri Gribenkod99ef532012-07-02 17:35:10 +0000196 StringRef ParentName,
197 const char *BriefComment)
Douglas Gregorba103062012-03-27 23:34:16 +0000198 : NumChunks(NumChunks), NumAnnotations(NumAnnotations),
Argyrios Kyrtzidis526d2442012-09-26 16:39:56 +0000199 Priority(Priority), Availability(Availability),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +0000200 ParentName(ParentName), BriefComment(BriefComment)
Douglas Gregor218937c2011-02-01 19:23:04 +0000201{
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000202 assert(NumChunks <= 0xffff);
203 assert(NumAnnotations <= 0xffff);
204
Douglas Gregor218937c2011-02-01 19:23:04 +0000205 Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
206 for (unsigned I = 0; I != NumChunks; ++I)
207 StoredChunks[I] = Chunks[I];
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000208
209 const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks);
210 for (unsigned I = 0; I != NumAnnotations; ++I)
211 StoredAnnotations[I] = Annotations[I];
Douglas Gregore6e03612009-09-18 22:15:54 +0000212}
213
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000214unsigned CodeCompletionString::getAnnotationCount() const {
215 return NumAnnotations;
216}
217
218const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const {
219 if (AnnotationNr < NumAnnotations)
220 return reinterpret_cast<const char * const*>(end())[AnnotationNr];
221 else
222 return 0;
223}
224
225
Douglas Gregore6e03612009-09-18 22:15:54 +0000226std::string CodeCompletionString::getAsString() const {
227 std::string Result;
228 llvm::raw_string_ostream OS(Result);
229
230 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
231 switch (C->Kind) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000232 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor0563c262009-09-22 23:15:58 +0000233 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000234
235 case CK_Informative:
236 case CK_ResultType:
237 OS << "[#" << C->Text << "#]";
238 break;
239
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000240 case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
241 default: OS << C->Text; break;
Douglas Gregore6e03612009-09-18 22:15:54 +0000242 }
243 }
Dan Gohman6bdeb402010-07-26 21:33:22 +0000244 return OS.str();
Douglas Gregore6e03612009-09-18 22:15:54 +0000245}
246
Douglas Gregor54f01612009-11-19 00:01:57 +0000247const char *CodeCompletionString::getTypedText() const {
248 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
249 if (C->Kind == CK_TypedText)
250 return C->Text;
251
252 return 0;
253}
254
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255const char *CodeCompletionAllocator::CopyString(StringRef String) {
Douglas Gregordae68752011-02-01 22:57:45 +0000256 char *Mem = (char *)Allocate(String.size() + 1, 1);
257 std::copy(String.begin(), String.end(), Mem);
258 Mem[String.size()] = 0;
259 return Mem;
260}
261
Chris Lattner5f9e2722011-07-23 10:55:15 +0000262const char *CodeCompletionAllocator::CopyString(Twine String) {
Douglas Gregor577cdfd2011-02-17 00:22:45 +0000263 // FIXME: It would be more efficient to teach Twine to tell us its size and
264 // then add a routine there to fill in an allocated char* with the contents
265 // of the string.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000266 SmallString<128> Data;
Douglas Gregor577cdfd2011-02-17 00:22:45 +0000267 return CopyString(String.toStringRef(Data));
268}
269
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000270StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) {
271 const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000272 if (!ND)
273 return StringRef();
274
275 // Check whether we've already cached the parent name.
276 StringRef &CachedParentName = ParentNames[DC];
277 if (!CachedParentName.empty())
278 return CachedParentName;
279
280 // If we already processed this DeclContext and assigned empty to it, the
281 // data pointer will be non-null.
282 if (CachedParentName.data() != 0)
283 return StringRef();
284
285 // Find the interesting names.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000286 SmallVector<const DeclContext *, 2> Contexts;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000287 while (DC && !DC->isFunctionOrMethod()) {
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000288 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000289 if (ND->getIdentifier())
290 Contexts.push_back(DC);
291 }
292
293 DC = DC->getParent();
294 }
295
296 {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000297 SmallString<128> S;
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000298 llvm::raw_svector_ostream OS(S);
299 bool First = true;
300 for (unsigned I = Contexts.size(); I != 0; --I) {
301 if (First)
302 First = false;
303 else {
304 OS << "::";
305 }
306
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000307 const DeclContext *CurDC = Contexts[I-1];
308 if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000309 CurDC = CatImpl->getCategoryDecl();
310
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000311 if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
312 const ObjCInterfaceDecl *Interface = Cat->getClassInterface();
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000313 if (!Interface) {
314 // Assign an empty StringRef but with non-null data to distinguish
315 // between empty because we didn't process the DeclContext yet.
316 CachedParentName = StringRef((const char *)~0U, 0);
317 return StringRef();
318 }
319
320 OS << Interface->getName() << '(' << Cat->getName() << ')';
321 } else {
322 OS << cast<NamedDecl>(CurDC)->getName();
323 }
324 }
325
326 CachedParentName = AllocatorRef->CopyString(OS.str());
327 }
328
329 return CachedParentName;
330}
331
Douglas Gregor218937c2011-02-01 19:23:04 +0000332CodeCompletionString *CodeCompletionBuilder::TakeString() {
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000333 void *Mem = getAllocator().Allocate(
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000334 sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size()
335 + sizeof(const char *) * Annotations.size(),
Douglas Gregor218937c2011-02-01 19:23:04 +0000336 llvm::alignOf<CodeCompletionString>());
337 CodeCompletionString *Result
338 = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
Erik Verbruggen6164ea12011-10-14 15:31:08 +0000339 Priority, Availability,
Douglas Gregorba103062012-03-27 23:34:16 +0000340 Annotations.data(), Annotations.size(),
Argyrios Kyrtzidis526d2442012-09-26 16:39:56 +0000341 ParentName, BriefComment);
Douglas Gregor218937c2011-02-01 19:23:04 +0000342 Chunks.clear();
Douglas Gregor54f01612009-11-19 00:01:57 +0000343 return Result;
344}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000345
Benjamin Kramer1eb18af2012-03-26 16:57:36 +0000346void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) {
347 Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text));
348}
349
350void CodeCompletionBuilder::AddTextChunk(const char *Text) {
351 Chunks.push_back(Chunk::CreateText(Text));
352}
353
354void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) {
355 Chunks.push_back(Chunk::CreateOptional(Optional));
356}
357
358void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) {
359 Chunks.push_back(Chunk::CreatePlaceholder(Placeholder));
360}
361
362void CodeCompletionBuilder::AddInformativeChunk(const char *Text) {
363 Chunks.push_back(Chunk::CreateInformative(Text));
364}
365
366void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) {
367 Chunks.push_back(Chunk::CreateResultType(ResultType));
368}
369
370void
371CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) {
372 Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter));
373}
374
375void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK,
376 const char *Text) {
377 Chunks.push_back(Chunk(CK, Text));
378}
379
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000380void CodeCompletionBuilder::addParentContext(const DeclContext *DC) {
Douglas Gregorba103062012-03-27 23:34:16 +0000381 if (DC->isTranslationUnit()) {
Douglas Gregorba103062012-03-27 23:34:16 +0000382 return;
383 }
384
385 if (DC->isFunctionOrMethod())
386 return;
387
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000388 const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
Douglas Gregorba103062012-03-27 23:34:16 +0000389 if (!ND)
390 return;
391
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000392 ParentName = getCodeCompletionTUInfo().getParentName(DC);
Douglas Gregorba103062012-03-27 23:34:16 +0000393}
394
Dmitri Gribenkod99ef532012-07-02 17:35:10 +0000395void CodeCompletionBuilder::addBriefComment(StringRef Comment) {
396 BriefComment = Allocator.CopyString(Comment);
397}
398
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000399unsigned CodeCompletionResult::getPriorityFromDecl(const NamedDecl *ND) {
Douglas Gregor12e13132010-05-26 22:00:08 +0000400 if (!ND)
401 return CCP_Unlikely;
402
403 // Context-based decisions.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000404 const DeclContext *DC = ND->getDeclContext()->getRedeclContext();
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000405 if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC)) {
406 // _cmd is relatively rare
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000407 if (const ImplicitParamDecl *ImplicitParam =
408 dyn_cast<ImplicitParamDecl>(ND))
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000409 if (ImplicitParam->getIdentifier() &&
410 ImplicitParam->getIdentifier()->isStr("_cmd"))
411 return CCP_ObjC_cmd;
412
Douglas Gregor12e13132010-05-26 22:00:08 +0000413 return CCP_LocalDeclaration;
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000414 }
Douglas Gregor12e13132010-05-26 22:00:08 +0000415 if (DC->isRecord() || isa<ObjCContainerDecl>(DC))
416 return CCP_MemberDeclaration;
417
418 // Content-based decisions.
419 if (isa<EnumConstantDecl>(ND))
420 return CCP_Constant;
421 if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND))
422 return CCP_Type;
Douglas Gregor97fe61c2010-09-18 15:16:27 +0000423
Douglas Gregor12e13132010-05-26 22:00:08 +0000424 return CCP_Declaration;
425}
426
Douglas Gregore6e03612009-09-18 22:15:54 +0000427//===----------------------------------------------------------------------===//
Douglas Gregor05944382009-09-23 00:16:58 +0000428// Code completion overload candidate implementation
429//===----------------------------------------------------------------------===//
430FunctionDecl *
431CodeCompleteConsumer::OverloadCandidate::getFunction() const {
432 if (getKind() == CK_Function)
433 return Function;
434 else if (getKind() == CK_FunctionTemplate)
435 return FunctionTemplate->getTemplatedDecl();
436 else
437 return 0;
438}
439
440const FunctionType *
441CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
442 switch (Kind) {
443 case CK_Function:
444 return Function->getType()->getAs<FunctionType>();
445
446 case CK_FunctionTemplate:
447 return FunctionTemplate->getTemplatedDecl()->getType()
448 ->getAs<FunctionType>();
449
450 case CK_FunctionType:
451 return Type;
452 }
David Blaikie7530c032012-01-17 06:56:22 +0000453
454 llvm_unreachable("Invalid CandidateKind!");
Douglas Gregor05944382009-09-23 00:16:58 +0000455}
456
457//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +0000458// Code completion consumer implementation
459//===----------------------------------------------------------------------===//
460
Douglas Gregor86d9a522009-09-21 16:56:56 +0000461CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregore6e03612009-09-18 22:15:54 +0000462
Douglas Gregor81b747b2009-09-17 21:32:03 +0000463void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000464PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000465 CodeCompletionContext Context,
John McCall0a2c5e22010-08-25 06:19:51 +0000466 CodeCompletionResult *Results,
Douglas Gregor81b747b2009-09-17 21:32:03 +0000467 unsigned NumResults) {
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000468 std::stable_sort(Results, Results + NumResults);
469
Douglas Gregor81b747b2009-09-17 21:32:03 +0000470 // Print the results.
471 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000472 OS << "COMPLETION: ";
Douglas Gregor81b747b2009-09-17 21:32:03 +0000473 switch (Results[I].Kind) {
John McCall0a2c5e22010-08-25 06:19:51 +0000474 case CodeCompletionResult::RK_Declaration:
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000475 OS << *Results[I].Declaration;
Douglas Gregor81b747b2009-09-17 21:32:03 +0000476 if (Results[I].Hidden)
477 OS << " (Hidden)";
Douglas Gregor86d9a522009-09-21 16:56:56 +0000478 if (CodeCompletionString *CCS
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000479 = Results[I].CreateCodeCompletionString(SemaRef, getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +0000480 CCTUInfo,
481 includeBriefComments())) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000482 OS << " : " << CCS->getAsString();
Dmitri Gribenkod99ef532012-07-02 17:35:10 +0000483 if (const char *BriefComment = CCS->getBriefComment())
484 OS << " : " << BriefComment;
Douglas Gregore6e03612009-09-18 22:15:54 +0000485 }
486
Douglas Gregor81b747b2009-09-17 21:32:03 +0000487 OS << '\n';
488 break;
489
John McCall0a2c5e22010-08-25 06:19:51 +0000490 case CodeCompletionResult::RK_Keyword:
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000491 OS << Results[I].Keyword << '\n';
Douglas Gregor81b747b2009-09-17 21:32:03 +0000492 break;
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000493
John McCall0a2c5e22010-08-25 06:19:51 +0000494 case CodeCompletionResult::RK_Macro: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000495 OS << Results[I].Macro->getName();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000496 if (CodeCompletionString *CCS
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000497 = Results[I].CreateCodeCompletionString(SemaRef, getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +0000498 CCTUInfo,
499 includeBriefComments())) {
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000500 OS << " : " << CCS->getAsString();
Douglas Gregor3f7c7f42009-10-30 16:50:04 +0000501 }
502 OS << '\n';
503 break;
504 }
Douglas Gregor54f01612009-11-19 00:01:57 +0000505
John McCall0a2c5e22010-08-25 06:19:51 +0000506 case CodeCompletionResult::RK_Pattern: {
Douglas Gregorab0b4f12010-01-13 23:24:38 +0000507 OS << "Pattern : "
Douglas Gregor54f01612009-11-19 00:01:57 +0000508 << Results[I].Pattern->getAsString() << '\n';
509 break;
510 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000511 }
512 }
Douglas Gregor81b747b2009-09-17 21:32:03 +0000513}
Douglas Gregor05944382009-09-23 00:16:58 +0000514
515void
Daniel Dunbar3a2838d2009-11-13 08:58:20 +0000516PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
517 unsigned CurrentArg,
Douglas Gregor05944382009-09-23 00:16:58 +0000518 OverloadCandidate *Candidates,
519 unsigned NumCandidates) {
520 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor86d802e2009-09-23 00:34:09 +0000521 if (CodeCompletionString *CCS
Douglas Gregor218937c2011-02-01 19:23:04 +0000522 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef,
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +0000523 getAllocator(), CCTUInfo)) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000524 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregor05944382009-09-23 00:16:58 +0000525 }
526 }
Douglas Gregor05944382009-09-23 00:16:58 +0000527}
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000528
Douglas Gregor8e42f272012-03-17 06:39:06 +0000529/// \brief Retrieve the effective availability of the given declaration.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000530static AvailabilityResult getDeclAvailability(const Decl *D) {
Douglas Gregor8e42f272012-03-17 06:39:06 +0000531 AvailabilityResult AR = D->getAvailability();
532 if (isa<EnumConstantDecl>(D))
533 AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability());
534 return AR;
535}
536
Erik Verbruggend1205962011-10-06 07:27:49 +0000537void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) {
Douglas Gregor87c08a52010-08-13 22:48:40 +0000538 switch (Kind) {
Douglas Gregorba103062012-03-27 23:34:16 +0000539 case RK_Pattern:
540 if (!Declaration) {
541 // Do nothing: Patterns can come with cursor kinds!
542 break;
543 }
544 // Fall through
545
Douglas Gregor8e42f272012-03-17 06:39:06 +0000546 case RK_Declaration: {
Douglas Gregor58ddb602010-08-23 23:00:57 +0000547 // Set the availability based on attributes.
Douglas Gregor8e42f272012-03-17 06:39:06 +0000548 switch (getDeclAvailability(Declaration)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000549 case AR_Available:
550 case AR_NotYetIntroduced:
551 Availability = CXAvailability_Available;
552 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000553
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000554 case AR_Deprecated:
555 Availability = CXAvailability_Deprecated;
556 break;
557
558 case AR_Unavailable:
559 Availability = CXAvailability_NotAvailable;
560 break;
561 }
562
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000563 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
Douglas Gregore8d7beb2010-09-03 23:30:36 +0000564 if (Function->isDeleted())
Douglas Gregor58ddb602010-08-23 23:00:57 +0000565 Availability = CXAvailability_NotAvailable;
Douglas Gregore8d7beb2010-09-03 23:30:36 +0000566
567 CursorKind = getCursorKindForDecl(Declaration);
Douglas Gregor375bb142011-12-27 22:43:10 +0000568 if (CursorKind == CXCursor_UnexposedDecl) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000569 // FIXME: Forward declarations of Objective-C classes and protocols
570 // are not directly exposed, but we want code completion to treat them
571 // like a definition.
Douglas Gregor375bb142011-12-27 22:43:10 +0000572 if (isa<ObjCInterfaceDecl>(Declaration))
573 CursorKind = CXCursor_ObjCInterfaceDecl;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000574 else if (isa<ObjCProtocolDecl>(Declaration))
575 CursorKind = CXCursor_ObjCProtocolDecl;
Douglas Gregor375bb142011-12-27 22:43:10 +0000576 else
577 CursorKind = CXCursor_NotImplemented;
578 }
Douglas Gregor87c08a52010-08-13 22:48:40 +0000579 break;
Douglas Gregor8e42f272012-03-17 06:39:06 +0000580 }
Douglas Gregor87c08a52010-08-13 22:48:40 +0000581
John McCall0a2c5e22010-08-25 06:19:51 +0000582 case RK_Macro:
John McCall0a2c5e22010-08-25 06:19:51 +0000583 case RK_Keyword:
Benjamin Kramere5f83722012-05-20 14:19:46 +0000584 llvm_unreachable("Macro and keyword kinds are handled by the constructors");
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000585 }
Erik Verbruggend1205962011-10-06 07:27:49 +0000586
587 if (!Accessible)
588 Availability = CXAvailability_NotAccessible;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000589}
Douglas Gregor2b4074f2009-12-01 05:55:20 +0000590
Douglas Gregor721f3592010-08-25 18:41:16 +0000591/// \brief Retrieve the name that should be used to order a result.
592///
593/// If the name needs to be constructed as a string, that string will be
594/// saved into Saved and the returned StringRef will refer to it.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000595static StringRef getOrderedName(const CodeCompletionResult &R,
Douglas Gregor721f3592010-08-25 18:41:16 +0000596 std::string &Saved) {
597 switch (R.Kind) {
598 case CodeCompletionResult::RK_Keyword:
599 return R.Keyword;
600
601 case CodeCompletionResult::RK_Pattern:
602 return R.Pattern->getTypedText();
603
604 case CodeCompletionResult::RK_Macro:
605 return R.Macro->getName();
606
607 case CodeCompletionResult::RK_Declaration:
608 // Handle declarations below.
609 break;
610 }
611
612 DeclarationName Name = R.Declaration->getDeclName();
613
614 // If the name is a simple identifier (by far the common case), or a
615 // zero-argument selector, just return a reference to that identifier.
616 if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
617 return Id->getName();
618 if (Name.isObjCZeroArgSelector())
619 if (IdentifierInfo *Id
620 = Name.getObjCSelector().getIdentifierInfoForSlot(0))
621 return Id->getName();
622
623 Saved = Name.getAsString();
624 return Saved;
625}
626
627bool clang::operator<(const CodeCompletionResult &X,
628 const CodeCompletionResult &Y) {
629 std::string XSaved, YSaved;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000630 StringRef XStr = getOrderedName(X, XSaved);
631 StringRef YStr = getOrderedName(Y, YSaved);
Douglas Gregor721f3592010-08-25 18:41:16 +0000632 int cmp = XStr.compare_lower(YStr);
633 if (cmp)
634 return cmp < 0;
635
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000636 // If case-insensitive comparison fails, try case-sensitive comparison.
637 cmp = XStr.compare(YStr);
638 if (cmp)
639 return cmp < 0;
Douglas Gregor721f3592010-08-25 18:41:16 +0000640
641 return false;
642}