Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1 | //===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===// |
Douglas Gregor | 2436e71 | 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 | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Scope.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Sema.h" |
Douglas Gregor | 56c2dbc | 2009-09-18 17:54:00 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 20 | #include "clang-c/Index.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include <algorithm> |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 26 | #include <cstring> |
| 27 | #include <functional> |
Douglas Gregor | ab6ccb5 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0212fd7 | 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 | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 37 | case CCC_Recovery: |
Douglas Gregor | 0212fd7 | 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 | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 49 | case CCC_DotMemberAccess: |
| 50 | case CCC_ArrowMemberAccess: |
| 51 | case CCC_ObjCPropertyAccess: |
Douglas Gregor | 0212fd7 | 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 | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 67 | case CCC_Other: |
Douglas Gregor | 3a69eaf | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 68 | case CCC_OtherWithMacros: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 69 | case CCC_ObjCInstanceMessage: |
| 70 | case CCC_ObjCClassMessage: |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 71 | case CCC_ObjCInterfaceName: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 72 | case CCC_ObjCCategoryName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 73 | return false; |
| 74 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 75 | |
| 76 | llvm_unreachable("Invalid CodeCompletionContext::Kind!"); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 80 | // Code completion string implementation |
| 81 | //===----------------------------------------------------------------------===// |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 82 | CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text) |
Daniel Dunbar | b0a1942 | 2009-11-12 18:40:12 +0000 | [diff] [blame] | 83 | : Kind(Kind), Text("") |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 84 | { |
Douglas Gregor | 9eb7701 | 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 | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 90 | case CK_ResultType: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 91 | case CK_CurrentParameter: |
| 92 | this->Text = Text; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 93 | break; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 94 | |
| 95 | case CK_Optional: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 96 | llvm_unreachable("Optional strings cannot be created from text"); |
Douglas Gregor | 9eb7701 | 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 | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 133 | |
| 134 | case CK_Colon: |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 135 | this->Text = ":"; |
Douglas Gregor | 504a6ae | 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 | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 153 | } |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 157 | CodeCompletionString::Chunk::CreateText(const char *Text) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 158 | return Chunk(CK_Text, Text); |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 162 | CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) { |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 163 | Chunk Result; |
| 164 | Result.Kind = CK_Optional; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 165 | Result.Optional = Optional; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 166 | return Result; |
| 167 | } |
| 168 | |
| 169 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 170 | CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 171 | return Chunk(CK_Placeholder, Placeholder); |
| 172 | } |
| 173 | |
| 174 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 175 | CodeCompletionString::Chunk::CreateInformative(const char *Informative) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 176 | return Chunk(CK_Informative, Informative); |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 179 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 180 | CodeCompletionString::Chunk::CreateResultType(const char *ResultType) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 181 | return Chunk(CK_ResultType, ResultType); |
| 182 | } |
| 183 | |
| 184 | CodeCompletionString::Chunk |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 185 | CodeCompletionString::Chunk::CreateCurrentParameter( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 186 | const char *CurrentParameter) { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 187 | return Chunk(CK_CurrentParameter, CurrentParameter); |
| 188 | } |
| 189 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 190 | CodeCompletionString::CodeCompletionString(const Chunk *Chunks, |
| 191 | unsigned NumChunks, |
| 192 | unsigned Priority, |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 193 | CXAvailabilityKind Availability, |
| 194 | const char **Annotations, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 195 | unsigned NumAnnotations, |
| 196 | CXCursorKind ParentKind, |
| 197 | StringRef ParentName) |
| 198 | : NumChunks(NumChunks), NumAnnotations(NumAnnotations), |
| 199 | Priority(Priority), Availability(Availability), ParentKind(ParentKind), |
| 200 | ParentName(ParentName) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 201 | { |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 202 | assert(NumChunks <= 0xffff); |
| 203 | assert(NumAnnotations <= 0xffff); |
| 204 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 205 | Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1); |
| 206 | for (unsigned I = 0; I != NumChunks; ++I) |
| 207 | StoredChunks[I] = Chunks[I]; |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 208 | |
| 209 | const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks); |
| 210 | for (unsigned I = 0; I != NumAnnotations; ++I) |
| 211 | StoredAnnotations[I] = Annotations[I]; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 214 | unsigned CodeCompletionString::getAnnotationCount() const { |
| 215 | return NumAnnotations; |
| 216 | } |
| 217 | |
| 218 | const 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 Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 226 | std::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 Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 232 | case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break; |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 233 | case CK_Placeholder: OS << "<#" << C->Text << "#>"; break; |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 234 | |
| 235 | case CK_Informative: |
| 236 | case CK_ResultType: |
| 237 | OS << "[#" << C->Text << "#]"; |
| 238 | break; |
| 239 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 240 | case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break; |
| 241 | default: OS << C->Text; break; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
Dan Gohman | 4888f1a | 2010-07-26 21:33:22 +0000 | [diff] [blame] | 244 | return OS.str(); |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 247 | const 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 Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 255 | const char *CodeCompletionAllocator::CopyString(StringRef String) { |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 256 | 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 Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 262 | const char *CodeCompletionAllocator::CopyString(Twine String) { |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 263 | // 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 Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 266 | SmallString<128> Data; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 267 | return CopyString(String.toStringRef(Data)); |
| 268 | } |
| 269 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 270 | CodeCompletionString *CodeCompletionBuilder::TakeString() { |
| 271 | void *Mem = Allocator.Allocate( |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 272 | sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() |
| 273 | + sizeof(const char *) * Annotations.size(), |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 274 | llvm::alignOf<CodeCompletionString>()); |
| 275 | CodeCompletionString *Result |
| 276 | = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(), |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 277 | Priority, Availability, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 278 | Annotations.data(), Annotations.size(), |
| 279 | ParentKind, ParentName); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 280 | Chunks.clear(); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 281 | return Result; |
| 282 | } |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 283 | |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 284 | void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) { |
| 285 | Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text)); |
| 286 | } |
| 287 | |
| 288 | void CodeCompletionBuilder::AddTextChunk(const char *Text) { |
| 289 | Chunks.push_back(Chunk::CreateText(Text)); |
| 290 | } |
| 291 | |
| 292 | void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) { |
| 293 | Chunks.push_back(Chunk::CreateOptional(Optional)); |
| 294 | } |
| 295 | |
| 296 | void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) { |
| 297 | Chunks.push_back(Chunk::CreatePlaceholder(Placeholder)); |
| 298 | } |
| 299 | |
| 300 | void CodeCompletionBuilder::AddInformativeChunk(const char *Text) { |
| 301 | Chunks.push_back(Chunk::CreateInformative(Text)); |
| 302 | } |
| 303 | |
| 304 | void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) { |
| 305 | Chunks.push_back(Chunk::CreateResultType(ResultType)); |
| 306 | } |
| 307 | |
| 308 | void |
| 309 | CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) { |
| 310 | Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter)); |
| 311 | } |
| 312 | |
| 313 | void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK, |
| 314 | const char *Text) { |
| 315 | Chunks.push_back(Chunk(CK, Text)); |
| 316 | } |
| 317 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 318 | void CodeCompletionBuilder::addParentContext(DeclContext *DC) { |
| 319 | if (DC->isTranslationUnit()) { |
| 320 | ParentKind = CXCursor_TranslationUnit; |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | if (DC->isFunctionOrMethod()) |
| 325 | return; |
| 326 | |
| 327 | NamedDecl *ND = dyn_cast<NamedDecl>(DC); |
| 328 | if (!ND) |
| 329 | return; |
| 330 | |
| 331 | ParentKind = getCursorKindForDecl(ND); |
| 332 | |
| 333 | // Check whether we've already cached the parent name. |
| 334 | StringRef &CachedParentName = Allocator.getParentNames()[DC]; |
| 335 | if (!CachedParentName.empty()) { |
| 336 | ParentName = CachedParentName; |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | // Find the interesting names. |
| 341 | llvm::SmallVector<DeclContext *, 2> Contexts; |
| 342 | while (DC && !DC->isFunctionOrMethod()) { |
| 343 | if (NamedDecl *ND = dyn_cast<NamedDecl>(DC)) { |
| 344 | if (ND->getIdentifier()) |
| 345 | Contexts.push_back(DC); |
| 346 | } |
| 347 | |
| 348 | DC = DC->getParent(); |
| 349 | } |
| 350 | |
| 351 | { |
| 352 | llvm::SmallString<128> S; |
| 353 | llvm::raw_svector_ostream OS(S); |
| 354 | bool First = true; |
| 355 | for (unsigned I = Contexts.size(); I != 0; --I) { |
| 356 | if (First) |
| 357 | First = false; |
| 358 | else { |
| 359 | OS << "::"; |
| 360 | } |
| 361 | |
| 362 | DeclContext *CurDC = Contexts[I-1]; |
| 363 | if (ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC)) |
| 364 | CurDC = CatImpl->getCategoryDecl(); |
| 365 | |
| 366 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) { |
| 367 | ObjCInterfaceDecl *Interface = Cat->getClassInterface(); |
| 368 | if (!Interface) |
| 369 | return; |
| 370 | |
| 371 | OS << Interface->getName() << '(' << Cat->getName() << ')'; |
| 372 | } else { |
| 373 | OS << cast<NamedDecl>(CurDC)->getName(); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | ParentName = Allocator.CopyString(OS.str()); |
| 378 | CachedParentName = ParentName; |
| 379 | } |
| 380 | } |
| 381 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 382 | unsigned CodeCompletionResult::getPriorityFromDecl(NamedDecl *ND) { |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 383 | if (!ND) |
| 384 | return CCP_Unlikely; |
| 385 | |
| 386 | // Context-based decisions. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 387 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Douglas Gregor | 521db40 | 2010-09-18 15:16:27 +0000 | [diff] [blame] | 388 | if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC)) { |
| 389 | // _cmd is relatively rare |
| 390 | if (ImplicitParamDecl *ImplicitParam = dyn_cast<ImplicitParamDecl>(ND)) |
| 391 | if (ImplicitParam->getIdentifier() && |
| 392 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 393 | return CCP_ObjC_cmd; |
| 394 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 395 | return CCP_LocalDeclaration; |
Douglas Gregor | 521db40 | 2010-09-18 15:16:27 +0000 | [diff] [blame] | 396 | } |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 397 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) |
| 398 | return CCP_MemberDeclaration; |
| 399 | |
| 400 | // Content-based decisions. |
| 401 | if (isa<EnumConstantDecl>(ND)) |
| 402 | return CCP_Constant; |
| 403 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 404 | return CCP_Type; |
Douglas Gregor | 521db40 | 2010-09-18 15:16:27 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 406 | return CCP_Declaration; |
| 407 | } |
| 408 | |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 409 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 410 | // Code completion overload candidate implementation |
| 411 | //===----------------------------------------------------------------------===// |
| 412 | FunctionDecl * |
| 413 | CodeCompleteConsumer::OverloadCandidate::getFunction() const { |
| 414 | if (getKind() == CK_Function) |
| 415 | return Function; |
| 416 | else if (getKind() == CK_FunctionTemplate) |
| 417 | return FunctionTemplate->getTemplatedDecl(); |
| 418 | else |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | const FunctionType * |
| 423 | CodeCompleteConsumer::OverloadCandidate::getFunctionType() const { |
| 424 | switch (Kind) { |
| 425 | case CK_Function: |
| 426 | return Function->getType()->getAs<FunctionType>(); |
| 427 | |
| 428 | case CK_FunctionTemplate: |
| 429 | return FunctionTemplate->getTemplatedDecl()->getType() |
| 430 | ->getAs<FunctionType>(); |
| 431 | |
| 432 | case CK_FunctionType: |
| 433 | return Type; |
| 434 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 435 | |
| 436 | llvm_unreachable("Invalid CandidateKind!"); |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 440 | // Code completion consumer implementation |
| 441 | //===----------------------------------------------------------------------===// |
| 442 | |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 443 | CodeCompleteConsumer::~CodeCompleteConsumer() { } |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 444 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 445 | void |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 446 | PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 447 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 448 | CodeCompletionResult *Results, |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 449 | unsigned NumResults) { |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 450 | std::stable_sort(Results, Results + NumResults); |
| 451 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 452 | // Print the results. |
| 453 | for (unsigned I = 0; I != NumResults; ++I) { |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 454 | OS << "COMPLETION: "; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 455 | switch (Results[I].Kind) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 456 | case CodeCompletionResult::RK_Declaration: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 457 | OS << *Results[I].Declaration; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 458 | if (Results[I].Hidden) |
| 459 | OS << " (Hidden)"; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 460 | if (CodeCompletionString *CCS |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 461 | = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) { |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 462 | OS << " : " << CCS->getAsString(); |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 465 | OS << '\n'; |
| 466 | break; |
| 467 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 468 | case CodeCompletionResult::RK_Keyword: |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 469 | OS << Results[I].Keyword << '\n'; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 470 | break; |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 471 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 472 | case CodeCompletionResult::RK_Macro: { |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 473 | OS << Results[I].Macro->getName(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 474 | if (CodeCompletionString *CCS |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 475 | = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) { |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 476 | OS << " : " << CCS->getAsString(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 477 | } |
| 478 | OS << '\n'; |
| 479 | break; |
| 480 | } |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 481 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 482 | case CodeCompletionResult::RK_Pattern: { |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 483 | OS << "Pattern : " |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 484 | << Results[I].Pattern->getAsString() << '\n'; |
| 485 | break; |
| 486 | } |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 489 | } |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 490 | |
| 491 | void |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 492 | PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef, |
| 493 | unsigned CurrentArg, |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 494 | OverloadCandidate *Candidates, |
| 495 | unsigned NumCandidates) { |
| 496 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 497 | if (CodeCompletionString *CCS |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 498 | = Candidates[I].CreateSignatureString(CurrentArg, SemaRef, |
| 499 | Allocator)) { |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 500 | OS << "OVERLOAD: " << CCS->getAsString() << "\n"; |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 503 | } |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 504 | |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 505 | /// \brief Retrieve the effective availability of the given declaration. |
| 506 | static AvailabilityResult getDeclAvailability(Decl *D) { |
| 507 | AvailabilityResult AR = D->getAvailability(); |
| 508 | if (isa<EnumConstantDecl>(D)) |
| 509 | AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability()); |
| 510 | return AR; |
| 511 | } |
| 512 | |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 513 | void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) { |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 514 | switch (Kind) { |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 515 | case RK_Pattern: |
| 516 | if (!Declaration) { |
| 517 | // Do nothing: Patterns can come with cursor kinds! |
| 518 | break; |
| 519 | } |
| 520 | // Fall through |
| 521 | |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 522 | case RK_Declaration: { |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 523 | // Set the availability based on attributes. |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 524 | switch (getDeclAvailability(Declaration)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 525 | case AR_Available: |
| 526 | case AR_NotYetIntroduced: |
| 527 | Availability = CXAvailability_Available; |
| 528 | break; |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 529 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 530 | case AR_Deprecated: |
| 531 | Availability = CXAvailability_Deprecated; |
| 532 | break; |
| 533 | |
| 534 | case AR_Unavailable: |
| 535 | Availability = CXAvailability_NotAvailable; |
| 536 | break; |
| 537 | } |
| 538 | |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 539 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration)) |
| 540 | if (Function->isDeleted()) |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 541 | Availability = CXAvailability_NotAvailable; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 542 | |
| 543 | CursorKind = getCursorKindForDecl(Declaration); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 544 | if (CursorKind == CXCursor_UnexposedDecl) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 545 | // FIXME: Forward declarations of Objective-C classes and protocols |
| 546 | // are not directly exposed, but we want code completion to treat them |
| 547 | // like a definition. |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 548 | if (isa<ObjCInterfaceDecl>(Declaration)) |
| 549 | CursorKind = CXCursor_ObjCInterfaceDecl; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 550 | else if (isa<ObjCProtocolDecl>(Declaration)) |
| 551 | CursorKind = CXCursor_ObjCProtocolDecl; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 552 | else |
| 553 | CursorKind = CXCursor_NotImplemented; |
| 554 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 555 | break; |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 556 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 557 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 558 | case RK_Macro: |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 559 | Availability = CXAvailability_Available; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 560 | CursorKind = CXCursor_MacroDefinition; |
| 561 | break; |
| 562 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 563 | case RK_Keyword: |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 564 | Availability = CXAvailability_Available; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 565 | CursorKind = CXCursor_NotImplemented; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 566 | break; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 567 | } |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 568 | |
| 569 | if (!Accessible) |
| 570 | Availability = CXAvailability_NotAccessible; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 571 | } |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 572 | |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 573 | /// \brief Retrieve the name that should be used to order a result. |
| 574 | /// |
| 575 | /// If the name needs to be constructed as a string, that string will be |
| 576 | /// saved into Saved and the returned StringRef will refer to it. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 577 | static StringRef getOrderedName(const CodeCompletionResult &R, |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 578 | std::string &Saved) { |
| 579 | switch (R.Kind) { |
| 580 | case CodeCompletionResult::RK_Keyword: |
| 581 | return R.Keyword; |
| 582 | |
| 583 | case CodeCompletionResult::RK_Pattern: |
| 584 | return R.Pattern->getTypedText(); |
| 585 | |
| 586 | case CodeCompletionResult::RK_Macro: |
| 587 | return R.Macro->getName(); |
| 588 | |
| 589 | case CodeCompletionResult::RK_Declaration: |
| 590 | // Handle declarations below. |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | DeclarationName Name = R.Declaration->getDeclName(); |
| 595 | |
| 596 | // If the name is a simple identifier (by far the common case), or a |
| 597 | // zero-argument selector, just return a reference to that identifier. |
| 598 | if (IdentifierInfo *Id = Name.getAsIdentifierInfo()) |
| 599 | return Id->getName(); |
| 600 | if (Name.isObjCZeroArgSelector()) |
| 601 | if (IdentifierInfo *Id |
| 602 | = Name.getObjCSelector().getIdentifierInfoForSlot(0)) |
| 603 | return Id->getName(); |
| 604 | |
| 605 | Saved = Name.getAsString(); |
| 606 | return Saved; |
| 607 | } |
| 608 | |
| 609 | bool clang::operator<(const CodeCompletionResult &X, |
| 610 | const CodeCompletionResult &Y) { |
| 611 | std::string XSaved, YSaved; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 612 | StringRef XStr = getOrderedName(X, XSaved); |
| 613 | StringRef YStr = getOrderedName(Y, YSaved); |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 614 | int cmp = XStr.compare_lower(YStr); |
| 615 | if (cmp) |
| 616 | return cmp < 0; |
| 617 | |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 618 | // If case-insensitive comparison fails, try case-sensitive comparison. |
| 619 | cmp = XStr.compare(YStr); |
| 620 | if (cmp) |
| 621 | return cmp < 0; |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 622 | |
| 623 | return false; |
| 624 | } |