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" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | #include <algorithm> |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 24 | #include <cstring> |
| 25 | #include <functional> |
Douglas Gregor | 92eff46 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 26 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 27 | using namespace clang; |
Douglas Gregor | 92eff46 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 28 | using llvm::StringRef; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 29 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Code completion string implementation |
| 32 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 33 | CodeCompletionString::Chunk::Chunk(ChunkKind Kind, llvm::StringRef Text) |
Daniel Dunbar | ad5757f | 2009-11-12 18:40:12 +0000 | [diff] [blame] | 34 | : Kind(Kind), Text("") |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 35 | { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 36 | switch (Kind) { |
| 37 | case CK_TypedText: |
| 38 | case CK_Text: |
| 39 | case CK_Placeholder: |
| 40 | case CK_Informative: |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 41 | case CK_ResultType: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 42 | 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 Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 51 | llvm_unreachable("Optional strings cannot be created from text"); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 52 | 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 Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 89 | |
| 90 | case CK_Colon: |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 91 | this->Text = ":"; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 92 | 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 Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 109 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | CodeCompletionString::Chunk |
Douglas Gregor | 92eff46 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 113 | CodeCompletionString::Chunk::CreateText(StringRef Text) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 114 | return Chunk(CK_Text, Text); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | CodeCompletionString::Chunk |
| 118 | CodeCompletionString::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 | |
| 126 | CodeCompletionString::Chunk |
Douglas Gregor | 92eff46 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 127 | CodeCompletionString::Chunk::CreatePlaceholder(StringRef Placeholder) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 128 | return Chunk(CK_Placeholder, Placeholder); |
| 129 | } |
| 130 | |
| 131 | CodeCompletionString::Chunk |
Douglas Gregor | 92eff46 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 132 | CodeCompletionString::Chunk::CreateInformative(StringRef Informative) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 133 | return Chunk(CK_Informative, Informative); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 136 | CodeCompletionString::Chunk |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 137 | CodeCompletionString::Chunk::CreateResultType(StringRef ResultType) { |
| 138 | return Chunk(CK_ResultType, ResultType); |
| 139 | } |
| 140 | |
| 141 | CodeCompletionString::Chunk |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 142 | CodeCompletionString::Chunk::CreateCurrentParameter( |
Douglas Gregor | 92eff46 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 143 | StringRef CurrentParameter) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 144 | return Chunk(CK_CurrentParameter, CurrentParameter); |
| 145 | } |
| 146 | |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 147 | CodeCompletionString::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 Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 153 | case CK_ResultType: |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 154 | 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 Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 164 | case CK_Colon: |
| 165 | case CK_SemiColon: |
| 166 | case CK_Equal: |
| 167 | case CK_HorizontalSpace: |
| 168 | case CK_VerticalSpace: |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 169 | 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 Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 180 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 181 | void |
| 182 | CodeCompletionString::Chunk::Destroy() { |
| 183 | switch (Kind) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 184 | case CK_Optional: |
| 185 | delete Optional; |
| 186 | break; |
| 187 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 188 | case CK_TypedText: |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 189 | case CK_Text: |
| 190 | case CK_Placeholder: |
| 191 | case CK_Informative: |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 192 | case CK_ResultType: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 193 | 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 Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 206 | case CK_Colon: |
| 207 | case CK_SemiColon: |
| 208 | case CK_Equal: |
| 209 | case CK_HorizontalSpace: |
| 210 | case CK_VerticalSpace: |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 211 | break; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 215 | void CodeCompletionString::clear() { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 216 | std::for_each(Chunks.begin(), Chunks.end(), |
| 217 | std::mem_fun_ref(&Chunk::Destroy)); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 218 | Chunks.clear(); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | std::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 Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 227 | case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break; |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 228 | case CK_Placeholder: OS << "<#" << C->Text << "#>"; break; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 229 | |
| 230 | case CK_Informative: |
| 231 | case CK_ResultType: |
| 232 | OS << "[#" << C->Text << "#]"; |
| 233 | break; |
| 234 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 235 | case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break; |
| 236 | default: OS << C->Text; break; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
Dan Gohman | 6bdeb40 | 2010-07-26 21:33:22 +0000 | [diff] [blame] | 239 | return OS.str(); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 242 | const 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 Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 250 | CodeCompletionString * |
| 251 | CodeCompletionString::Clone(CodeCompletionString *Result) const { |
| 252 | if (!Result) |
| 253 | Result = new CodeCompletionString; |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 254 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) |
| 255 | Result->AddChunk(C->Clone()); |
| 256 | return Result; |
| 257 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 258 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 259 | static void WriteUnsigned(llvm::raw_ostream &OS, unsigned Value) { |
| 260 | OS.write((const char *)&Value, sizeof(unsigned)); |
| 261 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 263 | static 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 Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void CodeCompletionString::Serialize(llvm::raw_ostream &OS) const { |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 274 | // Write the number of chunks. |
| 275 | WriteUnsigned(OS, size()); |
| 276 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 277 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) { |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 278 | WriteUnsigned(OS, C->Kind); |
| 279 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 280 | switch (C->Kind) { |
| 281 | case CK_TypedText: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 282 | case CK_Text: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 283 | case CK_Placeholder: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 284 | case CK_Informative: |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 285 | case CK_ResultType: |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 286 | case CK_CurrentParameter: { |
| 287 | const char *Text = C->Text; |
| 288 | unsigned StrLen = strlen(Text); |
| 289 | WriteUnsigned(OS, StrLen); |
| 290 | OS.write(Text, StrLen); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 291 | break; |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | case CK_Optional: |
| 295 | C->Optional->Serialize(OS); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 296 | break; |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 297 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 298 | case CK_LeftParen: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 299 | case CK_RightParen: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 300 | case CK_LeftBracket: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 301 | case CK_RightBracket: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 302 | case CK_LeftBrace: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 303 | case CK_RightBrace: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 304 | case CK_LeftAngle: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 305 | case CK_RightAngle: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 306 | case CK_Comma: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 307 | case CK_Colon: |
| 308 | case CK_SemiColon: |
| 309 | case CK_Equal: |
| 310 | case CK_HorizontalSpace: |
| 311 | case CK_VerticalSpace: |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 312 | break; |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 313 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 317 | bool CodeCompletionString::Deserialize(const char *&Str, const char *StrEnd) { |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 318 | if (Str == StrEnd || *Str == 0) |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 319 | return false; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 320 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 321 | unsigned NumBlocks; |
| 322 | if (ReadUnsigned(Str, StrEnd, NumBlocks)) |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 323 | return false; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 324 | |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 325 | 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 Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 332 | return false; |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 333 | |
| 334 | switch (ChunkKind Kind = (ChunkKind)KindValue) { |
| 335 | case CK_TypedText: |
| 336 | case CK_Text: |
| 337 | case CK_Placeholder: |
| 338 | case CK_Informative: |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 339 | case CK_ResultType: |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 340 | case CK_CurrentParameter: { |
| 341 | unsigned StrLen; |
| 342 | if (ReadUnsigned(Str, StrEnd, StrLen) || (Str + StrLen > StrEnd)) |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 343 | return false; |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 345 | AddChunk(Chunk(Kind, StringRef(Str, StrLen))); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 346 | Str += StrLen; |
| 347 | break; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 348 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 349 | |
| 350 | case CK_Optional: { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 351 | std::auto_ptr<CodeCompletionString> Optional(new CodeCompletionString()); |
| 352 | if (Optional->Deserialize(Str, StrEnd)) |
| 353 | AddOptionalChunk(Optional); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 354 | break; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 355 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 356 | |
| 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 Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 366 | case CK_Colon: |
| 367 | case CK_SemiColon: |
| 368 | case CK_Equal: |
| 369 | case CK_HorizontalSpace: |
| 370 | case CK_VerticalSpace: |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 371 | AddChunk(Chunk(Kind)); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 372 | break; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 373 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 374 | }; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 375 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 376 | return true; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 377 | } |
| 378 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 379 | void CodeCompletionResult::Destroy() { |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 380 | if (Kind == RK_Pattern) { |
| 381 | delete Pattern; |
| 382 | Pattern = 0; |
| 383 | } |
| 384 | } |
| 385 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 386 | unsigned CodeCompletionResult::getPriorityFromDecl(NamedDecl *ND) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 387 | if (!ND) |
| 388 | return CCP_Unlikely; |
| 389 | |
| 390 | // Context-based decisions. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 391 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 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 Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 405 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 406 | // Code completion overload candidate implementation |
| 407 | //===----------------------------------------------------------------------===// |
| 408 | FunctionDecl * |
| 409 | CodeCompleteConsumer::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 | |
| 418 | const FunctionType * |
| 419 | CodeCompleteConsumer::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 Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 436 | // Code completion consumer implementation |
| 437 | //===----------------------------------------------------------------------===// |
| 438 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 439 | CodeCompleteConsumer::~CodeCompleteConsumer() { } |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 440 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 441 | void |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 442 | PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 443 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 444 | CodeCompletionResult *Results, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 445 | unsigned NumResults) { |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 446 | std::stable_sort(Results, Results + NumResults); |
| 447 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 448 | // Print the results. |
| 449 | for (unsigned I = 0; I != NumResults; ++I) { |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 450 | OS << "COMPLETION: "; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 451 | switch (Results[I].Kind) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 452 | case CodeCompletionResult::RK_Declaration: |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 453 | OS << Results[I].Declaration; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 454 | if (Results[I].Hidden) |
| 455 | OS << " (Hidden)"; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 456 | if (CodeCompletionString *CCS |
| 457 | = Results[I].CreateCodeCompletionString(SemaRef)) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 458 | OS << " : " << CCS->getAsString(); |
| 459 | delete CCS; |
| 460 | } |
| 461 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 462 | OS << '\n'; |
| 463 | break; |
| 464 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 465 | case CodeCompletionResult::RK_Keyword: |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 466 | OS << Results[I].Keyword << '\n'; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 467 | break; |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 468 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 469 | case CodeCompletionResult::RK_Macro: { |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 470 | OS << Results[I].Macro->getName(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 471 | if (CodeCompletionString *CCS |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 472 | = Results[I].CreateCodeCompletionString(SemaRef)) { |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 473 | OS << " : " << CCS->getAsString(); |
| 474 | delete CCS; |
| 475 | } |
| 476 | OS << '\n'; |
| 477 | break; |
| 478 | } |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 479 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 480 | case CodeCompletionResult::RK_Pattern: { |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 481 | OS << "Pattern : " |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 482 | << Results[I].Pattern->getAsString() << '\n'; |
| 483 | break; |
| 484 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 487 | } |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 488 | |
| 489 | void |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 490 | PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef, |
| 491 | unsigned CurrentArg, |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 492 | OverloadCandidate *Candidates, |
| 493 | unsigned NumCandidates) { |
| 494 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 495 | if (CodeCompletionString *CCS |
| 496 | = Candidates[I].CreateSignatureString(CurrentArg, SemaRef)) { |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 497 | OS << "OVERLOAD: " << CCS->getAsString() << "\n"; |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 498 | delete CCS; |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 501 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 502 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 503 | void CodeCompletionResult::computeCursorKindAndAvailability() { |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 504 | switch (Kind) { |
| 505 | case RK_Declaration: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 506 | // Set the availability based on attributes. |
| 507 | Availability = CXAvailability_Available; |
| 508 | if (Declaration->getAttr<UnavailableAttr>()) |
| 509 | Availability = CXAvailability_NotAvailable; |
| 510 | else if (Declaration->getAttr<DeprecatedAttr>()) |
| 511 | Availability = CXAvailability_Deprecated; |
| 512 | |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 513 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration)) |
| 514 | if (Function->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 515 | Availability = CXAvailability_NotAvailable; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 516 | |
| 517 | CursorKind = getCursorKindForDecl(Declaration); |
| 518 | if (CursorKind == CXCursor_UnexposedDecl) |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 519 | CursorKind = CXCursor_NotImplemented; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 520 | break; |
| 521 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 522 | case RK_Macro: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 523 | Availability = CXAvailability_Available; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 524 | CursorKind = CXCursor_MacroDefinition; |
| 525 | break; |
| 526 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 527 | case RK_Keyword: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 528 | Availability = CXAvailability_Available; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 529 | CursorKind = CXCursor_NotImplemented; |
| 530 | break; |
| 531 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 532 | case RK_Pattern: |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 533 | // Do nothing: Patterns can come with cursor kinds! |
| 534 | break; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 537 | |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 538 | /// \brief Retrieve the name that should be used to order a result. |
| 539 | /// |
| 540 | /// If the name needs to be constructed as a string, that string will be |
| 541 | /// saved into Saved and the returned StringRef will refer to it. |
| 542 | static llvm::StringRef getOrderedName(const CodeCompletionResult &R, |
| 543 | std::string &Saved) { |
| 544 | switch (R.Kind) { |
| 545 | case CodeCompletionResult::RK_Keyword: |
| 546 | return R.Keyword; |
| 547 | |
| 548 | case CodeCompletionResult::RK_Pattern: |
| 549 | return R.Pattern->getTypedText(); |
| 550 | |
| 551 | case CodeCompletionResult::RK_Macro: |
| 552 | return R.Macro->getName(); |
| 553 | |
| 554 | case CodeCompletionResult::RK_Declaration: |
| 555 | // Handle declarations below. |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | DeclarationName Name = R.Declaration->getDeclName(); |
| 560 | |
| 561 | // If the name is a simple identifier (by far the common case), or a |
| 562 | // zero-argument selector, just return a reference to that identifier. |
| 563 | if (IdentifierInfo *Id = Name.getAsIdentifierInfo()) |
| 564 | return Id->getName(); |
| 565 | if (Name.isObjCZeroArgSelector()) |
| 566 | if (IdentifierInfo *Id |
| 567 | = Name.getObjCSelector().getIdentifierInfoForSlot(0)) |
| 568 | return Id->getName(); |
| 569 | |
| 570 | Saved = Name.getAsString(); |
| 571 | return Saved; |
| 572 | } |
| 573 | |
| 574 | bool clang::operator<(const CodeCompletionResult &X, |
| 575 | const CodeCompletionResult &Y) { |
| 576 | std::string XSaved, YSaved; |
| 577 | llvm::StringRef XStr = getOrderedName(X, XSaved); |
| 578 | llvm::StringRef YStr = getOrderedName(Y, YSaved); |
| 579 | int cmp = XStr.compare_lower(YStr); |
| 580 | if (cmp) |
| 581 | return cmp < 0; |
| 582 | |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 583 | // If case-insensitive comparison fails, try case-sensitive comparison. |
| 584 | cmp = XStr.compare(YStr); |
| 585 | if (cmp) |
| 586 | return cmp < 0; |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 587 | |
| 588 | return false; |
| 589 | } |
| 590 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 591 | void |
| 592 | CIndexCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 593 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 594 | CodeCompletionResult *Results, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 595 | unsigned NumResults) { |
| 596 | // Print the results. |
| 597 | for (unsigned I = 0; I != NumResults; ++I) { |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 598 | WriteUnsigned(OS, Results[I].CursorKind); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 599 | WriteUnsigned(OS, Results[I].Priority); |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 600 | WriteUnsigned(OS, Results[I].Availability); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 601 | CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(SemaRef); |
| 602 | assert(CCS && "No code-completion string?"); |
| 603 | CCS->Serialize(OS); |
| 604 | delete CCS; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 605 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | void |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 609 | CIndexCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef, |
| 610 | unsigned CurrentArg, |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 611 | OverloadCandidate *Candidates, |
| 612 | unsigned NumCandidates) { |
| 613 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 614 | WriteUnsigned(OS, CXCursor_NotImplemented); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 615 | WriteUnsigned(OS, /*Priority=*/I); |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 616 | WriteUnsigned(OS, /*Availability=*/CXAvailability_Available); |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 617 | CodeCompletionString *CCS |
| 618 | = Candidates[I].CreateSignatureString(CurrentArg, SemaRef); |
| 619 | assert(CCS && "No code-completion string?"); |
| 620 | CCS->Serialize(OS); |
| 621 | delete CCS; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 622 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 623 | } |