Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1 | //===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===// |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 2 | // |
| 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 McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Scope.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Sema.h" |
Douglas Gregor | 75b7128 | 2009-09-18 17:54:00 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 20 | #include "clang-c/Index.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame^] | 21 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include <algorithm> |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 26 | #include <cstring> |
| 27 | #include <functional> |
Douglas Gregor | 92eff46 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 32 | // Code completion context implementation |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | bool CodeCompletionContext::wantConstructorResults() const { |
| 36 | switch (Kind) { |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 37 | case CCC_Recovery: |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 38 | 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 Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 49 | case CCC_DotMemberAccess: |
| 50 | case CCC_ArrowMemberAccess: |
| 51 | case CCC_ObjCPropertyAccess: |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 52 | 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 Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 67 | case CCC_Other: |
Douglas Gregor | 5c722c70 | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 68 | case CCC_OtherWithMacros: |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 69 | case CCC_ObjCInstanceMessage: |
| 70 | case CCC_ObjCClassMessage: |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 71 | case CCC_ObjCInterfaceName: |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 72 | case CCC_ObjCCategoryName: |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 73 | return false; |
| 74 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 75 | |
| 76 | llvm_unreachable("Invalid CodeCompletionContext::Kind!"); |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | //===----------------------------------------------------------------------===// |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 80 | // Code completion string implementation |
| 81 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 82 | CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text) |
Daniel Dunbar | ad5757f | 2009-11-12 18:40:12 +0000 | [diff] [blame] | 83 | : Kind(Kind), Text("") |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 84 | { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 85 | switch (Kind) { |
| 86 | case CK_TypedText: |
| 87 | case CK_Text: |
| 88 | case CK_Placeholder: |
| 89 | case CK_Informative: |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 90 | case CK_ResultType: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 91 | case CK_CurrentParameter: |
| 92 | this->Text = Text; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 93 | break; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 94 | |
| 95 | case CK_Optional: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 96 | llvm_unreachable("Optional strings cannot be created from text"); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 97 | |
| 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 Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 133 | |
| 134 | case CK_Colon: |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 135 | this->Text = ":"; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 136 | 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 Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 153 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 157 | CodeCompletionString::Chunk::CreateText(const char *Text) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 158 | return Chunk(CK_Text, Text); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 162 | CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 163 | Chunk Result; |
| 164 | Result.Kind = CK_Optional; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 165 | Result.Optional = Optional; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 166 | return Result; |
| 167 | } |
| 168 | |
| 169 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 170 | CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 171 | return Chunk(CK_Placeholder, Placeholder); |
| 172 | } |
| 173 | |
| 174 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 175 | CodeCompletionString::Chunk::CreateInformative(const char *Informative) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 176 | return Chunk(CK_Informative, Informative); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 179 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 180 | CodeCompletionString::Chunk::CreateResultType(const char *ResultType) { |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 181 | return Chunk(CK_ResultType, ResultType); |
| 182 | } |
| 183 | |
| 184 | CodeCompletionString::Chunk |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 185 | CodeCompletionString::Chunk::CreateCurrentParameter( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 186 | const char *CurrentParameter) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 187 | return Chunk(CK_CurrentParameter, CurrentParameter); |
| 188 | } |
| 189 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 190 | CodeCompletionString::CodeCompletionString(const Chunk *Chunks, |
| 191 | unsigned NumChunks, |
| 192 | unsigned Priority, |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 193 | CXAvailabilityKind Availability, |
| 194 | const char **Annotations, |
| 195 | unsigned NumAnnotations) |
| 196 | : NumChunks(NumChunks), NumAnnotations(NumAnnotations) |
| 197 | , Priority(Priority), Availability(Availability) |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 198 | { |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 199 | assert(NumChunks <= 0xffff); |
| 200 | assert(NumAnnotations <= 0xffff); |
| 201 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 202 | Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1); |
| 203 | for (unsigned I = 0; I != NumChunks; ++I) |
| 204 | StoredChunks[I] = Chunks[I]; |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 205 | |
| 206 | const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks); |
| 207 | for (unsigned I = 0; I != NumAnnotations; ++I) |
| 208 | StoredAnnotations[I] = Annotations[I]; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 211 | unsigned CodeCompletionString::getAnnotationCount() const { |
| 212 | return NumAnnotations; |
| 213 | } |
| 214 | |
| 215 | const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const { |
| 216 | if (AnnotationNr < NumAnnotations) |
| 217 | return reinterpret_cast<const char * const*>(end())[AnnotationNr]; |
| 218 | else |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 223 | std::string CodeCompletionString::getAsString() const { |
| 224 | std::string Result; |
| 225 | llvm::raw_string_ostream OS(Result); |
| 226 | |
| 227 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) { |
| 228 | switch (C->Kind) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 229 | case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break; |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 230 | case CK_Placeholder: OS << "<#" << C->Text << "#>"; break; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 231 | |
| 232 | case CK_Informative: |
| 233 | case CK_ResultType: |
| 234 | OS << "[#" << C->Text << "#]"; |
| 235 | break; |
| 236 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 237 | case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break; |
| 238 | default: OS << C->Text; break; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
Dan Gohman | 6bdeb40 | 2010-07-26 21:33:22 +0000 | [diff] [blame] | 241 | return OS.str(); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 244 | const char *CodeCompletionString::getTypedText() const { |
| 245 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) |
| 246 | if (C->Kind == CK_TypedText) |
| 247 | return C->Text; |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 252 | const char *CodeCompletionAllocator::CopyString(StringRef String) { |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 253 | char *Mem = (char *)Allocate(String.size() + 1, 1); |
| 254 | std::copy(String.begin(), String.end(), Mem); |
| 255 | Mem[String.size()] = 0; |
| 256 | return Mem; |
| 257 | } |
| 258 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 259 | const char *CodeCompletionAllocator::CopyString(Twine String) { |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 260 | // FIXME: It would be more efficient to teach Twine to tell us its size and |
| 261 | // then add a routine there to fill in an allocated char* with the contents |
| 262 | // of the string. |
| 263 | llvm::SmallString<128> Data; |
| 264 | return CopyString(String.toStringRef(Data)); |
| 265 | } |
| 266 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 267 | CodeCompletionString *CodeCompletionBuilder::TakeString() { |
| 268 | void *Mem = Allocator.Allocate( |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 269 | sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() |
| 270 | + sizeof(const char *) * Annotations.size(), |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 271 | llvm::alignOf<CodeCompletionString>()); |
| 272 | CodeCompletionString *Result |
| 273 | = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(), |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 274 | Priority, Availability, |
| 275 | Annotations.data(), Annotations.size()); |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 276 | Chunks.clear(); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 277 | return Result; |
| 278 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 279 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 280 | unsigned CodeCompletionResult::getPriorityFromDecl(NamedDecl *ND) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 281 | if (!ND) |
| 282 | return CCP_Unlikely; |
| 283 | |
| 284 | // Context-based decisions. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 285 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Douglas Gregor | 97fe61c | 2010-09-18 15:16:27 +0000 | [diff] [blame] | 286 | if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC)) { |
| 287 | // _cmd is relatively rare |
| 288 | if (ImplicitParamDecl *ImplicitParam = dyn_cast<ImplicitParamDecl>(ND)) |
| 289 | if (ImplicitParam->getIdentifier() && |
| 290 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 291 | return CCP_ObjC_cmd; |
| 292 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 293 | return CCP_LocalDeclaration; |
Douglas Gregor | 97fe61c | 2010-09-18 15:16:27 +0000 | [diff] [blame] | 294 | } |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 295 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) |
| 296 | return CCP_MemberDeclaration; |
| 297 | |
| 298 | // Content-based decisions. |
| 299 | if (isa<EnumConstantDecl>(ND)) |
| 300 | return CCP_Constant; |
| 301 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 302 | return CCP_Type; |
Douglas Gregor | 97fe61c | 2010-09-18 15:16:27 +0000 | [diff] [blame] | 303 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 304 | return CCP_Declaration; |
| 305 | } |
| 306 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 307 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 308 | // Code completion overload candidate implementation |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | FunctionDecl * |
| 311 | CodeCompleteConsumer::OverloadCandidate::getFunction() const { |
| 312 | if (getKind() == CK_Function) |
| 313 | return Function; |
| 314 | else if (getKind() == CK_FunctionTemplate) |
| 315 | return FunctionTemplate->getTemplatedDecl(); |
| 316 | else |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | const FunctionType * |
| 321 | CodeCompleteConsumer::OverloadCandidate::getFunctionType() const { |
| 322 | switch (Kind) { |
| 323 | case CK_Function: |
| 324 | return Function->getType()->getAs<FunctionType>(); |
| 325 | |
| 326 | case CK_FunctionTemplate: |
| 327 | return FunctionTemplate->getTemplatedDecl()->getType() |
| 328 | ->getAs<FunctionType>(); |
| 329 | |
| 330 | case CK_FunctionType: |
| 331 | return Type; |
| 332 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 333 | |
| 334 | llvm_unreachable("Invalid CandidateKind!"); |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | //===----------------------------------------------------------------------===// |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 338 | // Code completion consumer implementation |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 341 | CodeCompleteConsumer::~CodeCompleteConsumer() { } |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 342 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 343 | void |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 344 | PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 345 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 346 | CodeCompletionResult *Results, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 347 | unsigned NumResults) { |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 348 | std::stable_sort(Results, Results + NumResults); |
| 349 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 350 | // Print the results. |
| 351 | for (unsigned I = 0; I != NumResults; ++I) { |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 352 | OS << "COMPLETION: "; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 353 | switch (Results[I].Kind) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 354 | case CodeCompletionResult::RK_Declaration: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 355 | OS << *Results[I].Declaration; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 356 | if (Results[I].Hidden) |
| 357 | OS << " (Hidden)"; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 358 | if (CodeCompletionString *CCS |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 359 | = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 360 | OS << " : " << CCS->getAsString(); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 363 | OS << '\n'; |
| 364 | break; |
| 365 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 366 | case CodeCompletionResult::RK_Keyword: |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 367 | OS << Results[I].Keyword << '\n'; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 368 | break; |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 369 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 370 | case CodeCompletionResult::RK_Macro: { |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 371 | OS << Results[I].Macro->getName(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 372 | if (CodeCompletionString *CCS |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 373 | = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) { |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 374 | OS << " : " << CCS->getAsString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 375 | } |
| 376 | OS << '\n'; |
| 377 | break; |
| 378 | } |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 379 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 380 | case CodeCompletionResult::RK_Pattern: { |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 381 | OS << "Pattern : " |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 382 | << Results[I].Pattern->getAsString() << '\n'; |
| 383 | break; |
| 384 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 387 | } |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 388 | |
| 389 | void |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 390 | PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef, |
| 391 | unsigned CurrentArg, |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 392 | OverloadCandidate *Candidates, |
| 393 | unsigned NumCandidates) { |
| 394 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 395 | if (CodeCompletionString *CCS |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 396 | = Candidates[I].CreateSignatureString(CurrentArg, SemaRef, |
| 397 | Allocator)) { |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 398 | OS << "OVERLOAD: " << CCS->getAsString() << "\n"; |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 401 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 402 | |
Erik Verbruggen | d120596 | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 403 | void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) { |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 404 | switch (Kind) { |
| 405 | case RK_Declaration: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 406 | // Set the availability based on attributes. |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 407 | switch (Declaration->getAvailability()) { |
| 408 | case AR_Available: |
| 409 | case AR_NotYetIntroduced: |
| 410 | Availability = CXAvailability_Available; |
| 411 | break; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 412 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 413 | case AR_Deprecated: |
| 414 | Availability = CXAvailability_Deprecated; |
| 415 | break; |
| 416 | |
| 417 | case AR_Unavailable: |
| 418 | Availability = CXAvailability_NotAvailable; |
| 419 | break; |
| 420 | } |
| 421 | |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 422 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration)) |
| 423 | if (Function->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 424 | Availability = CXAvailability_NotAvailable; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 425 | |
| 426 | CursorKind = getCursorKindForDecl(Declaration); |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 427 | if (CursorKind == CXCursor_UnexposedDecl) { |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 428 | // FIXME: Forward declarations of Objective-C classes and protocols |
| 429 | // are not directly exposed, but we want code completion to treat them |
| 430 | // like a definition. |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 431 | if (isa<ObjCInterfaceDecl>(Declaration)) |
| 432 | CursorKind = CXCursor_ObjCInterfaceDecl; |
Douglas Gregor | bd9482d | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 433 | else if (isa<ObjCProtocolDecl>(Declaration)) |
| 434 | CursorKind = CXCursor_ObjCProtocolDecl; |
Douglas Gregor | 375bb14 | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 435 | else |
| 436 | CursorKind = CXCursor_NotImplemented; |
| 437 | } |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 438 | break; |
| 439 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 440 | case RK_Macro: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 441 | Availability = CXAvailability_Available; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 442 | CursorKind = CXCursor_MacroDefinition; |
| 443 | break; |
| 444 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 445 | case RK_Keyword: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 446 | Availability = CXAvailability_Available; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 447 | CursorKind = CXCursor_NotImplemented; |
| 448 | break; |
| 449 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 450 | case RK_Pattern: |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 451 | // Do nothing: Patterns can come with cursor kinds! |
| 452 | break; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 453 | } |
Erik Verbruggen | d120596 | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 454 | |
| 455 | if (!Accessible) |
| 456 | Availability = CXAvailability_NotAccessible; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 457 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 458 | |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 459 | /// \brief Retrieve the name that should be used to order a result. |
| 460 | /// |
| 461 | /// If the name needs to be constructed as a string, that string will be |
| 462 | /// saved into Saved and the returned StringRef will refer to it. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 463 | static StringRef getOrderedName(const CodeCompletionResult &R, |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 464 | std::string &Saved) { |
| 465 | switch (R.Kind) { |
| 466 | case CodeCompletionResult::RK_Keyword: |
| 467 | return R.Keyword; |
| 468 | |
| 469 | case CodeCompletionResult::RK_Pattern: |
| 470 | return R.Pattern->getTypedText(); |
| 471 | |
| 472 | case CodeCompletionResult::RK_Macro: |
| 473 | return R.Macro->getName(); |
| 474 | |
| 475 | case CodeCompletionResult::RK_Declaration: |
| 476 | // Handle declarations below. |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | DeclarationName Name = R.Declaration->getDeclName(); |
| 481 | |
| 482 | // If the name is a simple identifier (by far the common case), or a |
| 483 | // zero-argument selector, just return a reference to that identifier. |
| 484 | if (IdentifierInfo *Id = Name.getAsIdentifierInfo()) |
| 485 | return Id->getName(); |
| 486 | if (Name.isObjCZeroArgSelector()) |
| 487 | if (IdentifierInfo *Id |
| 488 | = Name.getObjCSelector().getIdentifierInfoForSlot(0)) |
| 489 | return Id->getName(); |
| 490 | |
| 491 | Saved = Name.getAsString(); |
| 492 | return Saved; |
| 493 | } |
| 494 | |
| 495 | bool clang::operator<(const CodeCompletionResult &X, |
| 496 | const CodeCompletionResult &Y) { |
| 497 | std::string XSaved, YSaved; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 498 | StringRef XStr = getOrderedName(X, XSaved); |
| 499 | StringRef YStr = getOrderedName(Y, YSaved); |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 500 | int cmp = XStr.compare_lower(YStr); |
| 501 | if (cmp) |
| 502 | return cmp < 0; |
| 503 | |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 504 | // If case-insensitive comparison fails, try case-sensitive comparison. |
| 505 | cmp = XStr.compare(YStr); |
| 506 | if (cmp) |
| 507 | return cmp < 0; |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 508 | |
| 509 | return false; |
| 510 | } |