Kate Stone | e2b2186 | 2014-07-22 17:03:38 +0000 | [diff] [blame^] | 1 | //===-- FastDemangle.cpp ----------------------------------------*- C++ -*-===// |
| 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 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | //#define DEBUG_FAILURES 1 |
| 15 | //#define DEBUG_SUBSTITUTIONS 1 |
| 16 | //#define DEBUG_TEMPLATE_ARGS 1 |
| 17 | //#define DEBUG_HIGHWATER 1 |
| 18 | //#define DEBUG_REORDERING 1 |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | /// @brief Represents the collection of qualifiers on a type |
| 23 | |
| 24 | enum Qualifiers |
| 25 | { |
| 26 | QualifierNone = 0, |
| 27 | QualifierConst = 1, |
| 28 | QualifierRestrict = 2, |
| 29 | QualifierVolatile = 4, |
| 30 | QualifierReference = 8, |
| 31 | QualifierRValueReference = 16, |
| 32 | QualifierPointer = 32 |
| 33 | }; |
| 34 | |
| 35 | /// @brief Categorizes the recognized operators |
| 36 | |
| 37 | enum class OperatorKind |
| 38 | { |
| 39 | Unary, |
| 40 | Postfix, |
| 41 | Binary, |
| 42 | Ternary, |
| 43 | Other, |
| 44 | ConversionOperator, |
| 45 | Vendor, |
| 46 | NoMatch |
| 47 | }; |
| 48 | |
| 49 | /// @brief Represents one of the recognized two-character operator |
| 50 | /// abbreviations used when parsing operators as names and expressions |
| 51 | |
| 52 | struct Operator |
| 53 | { |
| 54 | const char * name; |
| 55 | OperatorKind kind; |
| 56 | }; |
| 57 | |
| 58 | /// @brief Represents a range of characters in the output buffer, typically for |
| 59 | /// use with RewriteRange() |
| 60 | |
| 61 | struct BufferRange |
| 62 | { |
| 63 | int offset; |
| 64 | int length; |
| 65 | }; |
| 66 | |
| 67 | /// @brief Transient state required while parsing a name |
| 68 | |
| 69 | struct NameState |
| 70 | { |
| 71 | bool parse_function_params; |
| 72 | bool is_last_generic; |
| 73 | bool has_no_return_type; |
| 74 | BufferRange last_name_range; |
| 75 | }; |
| 76 | |
| 77 | /// @brief LLDB's fast C++ demangler |
| 78 | /// |
| 79 | /// This is an incomplete implementation designed to speed up the demangling |
| 80 | /// process that is often a bottleneck when LLDB stops a process for the first |
| 81 | /// time. Where the implementation doesn't know how to demangle a symbol it |
| 82 | /// fails gracefully to allow the caller to fall back to the existing demangler. |
| 83 | /// |
| 84 | /// Over time the full mangling spec should be supported without compromising |
| 85 | /// performance for the most common cases. |
| 86 | |
| 87 | class SymbolDemangler |
| 88 | { |
| 89 | public: |
| 90 | |
| 91 | //---------------------------------------------------- |
| 92 | // Public API |
| 93 | //---------------------------------------------------- |
| 94 | |
| 95 | /// @brief Create a SymbolDemangler |
| 96 | /// |
| 97 | /// The newly created demangler allocates and owns scratch memory sufficient |
| 98 | /// for demangling typical symbols. Additional memory will be allocated if |
| 99 | /// needed and managed by the demangler instance. |
| 100 | |
| 101 | SymbolDemangler() |
| 102 | { |
| 103 | buffer = (char *) malloc(8192); |
| 104 | buffer_end = buffer + 8192; |
| 105 | owns_buffer = true; |
| 106 | |
| 107 | rewrite_ranges = (BufferRange *) malloc(128 * sizeof(BufferRange)); |
| 108 | rewrite_ranges_size = 128; |
| 109 | owns_rewrite_ranges = true; |
| 110 | } |
| 111 | |
| 112 | /// @brief Create a SymbolDemangler that uses provided scratch memory |
| 113 | /// |
| 114 | /// The provided memory is not owned by the demangler. It will be |
| 115 | /// overwritten during calls to GetDemangledCopy() but can be used for |
| 116 | /// other purposes between calls. The provided memory will not be freed |
| 117 | /// when this instance is destroyed. |
| 118 | /// |
| 119 | /// If demangling a symbol requires additional space it will be allocated |
| 120 | /// and managed by the demangler instance. |
| 121 | /// |
| 122 | /// @param storage_ptr Valid pointer to at least storage_size bytes of |
| 123 | /// space that the SymbolDemangler can use during demangling |
| 124 | /// |
| 125 | /// @param storage_size Number of bytes of space available scratch memory |
| 126 | /// referenced by storage_ptr |
| 127 | |
| 128 | SymbolDemangler(void * storage_ptr, int storage_size) |
| 129 | { |
| 130 | // Use up to 1/8th of the provided space for rewrite ranges |
| 131 | rewrite_ranges_size = (storage_size >> 3) / sizeof(BufferRange); |
| 132 | rewrite_ranges = (BufferRange *) storage_ptr; |
| 133 | owns_rewrite_ranges = false; |
| 134 | |
| 135 | // Use the rest for the character buffer |
| 136 | buffer = (char *) storage_ptr + rewrite_ranges_size * sizeof(BufferRange); |
| 137 | buffer_end = (const char *)storage_ptr + storage_size; |
| 138 | owns_buffer = false; |
| 139 | } |
| 140 | |
| 141 | /// @brief Destroys the SymbolDemangler and deallocates any scratch |
| 142 | /// memory that it owns |
| 143 | |
| 144 | ~SymbolDemangler() |
| 145 | { |
| 146 | if (owns_buffer) free(buffer); |
| 147 | if (owns_rewrite_ranges) free(rewrite_ranges); |
| 148 | } |
| 149 | |
| 150 | #ifdef DEBUG_HIGHWATER |
| 151 | int highwater_store = 0; |
| 152 | int highwater_buffer = 0; |
| 153 | #endif |
| 154 | |
| 155 | /// @brief Parses the provided mangled name and returns a newly allocated |
| 156 | /// demangling |
| 157 | /// |
| 158 | /// @param mangled_name Valid null-terminated C++ mangled name following |
| 159 | /// the Itanium C++ ABI mangling specification as implemented by Clang |
| 160 | /// |
| 161 | /// @result Newly allocated null-terminated demangled name when demangling |
| 162 | /// is succesful, and nullptr when demangling fails. The caller is |
| 163 | /// responsible for freeing the allocated memory. |
| 164 | |
| 165 | char * GetDemangledCopy(const char * mangled_name, |
| 166 | long mangled_name_length = 0) |
| 167 | { |
| 168 | if (!ParseMangling(mangled_name, mangled_name_length)) return nullptr; |
| 169 | |
| 170 | #ifdef DEBUG_HIGHWATER |
| 171 | int rewrite_count = next_substitute_index + |
| 172 | (rewrite_ranges_size - 1 - next_template_arg_index); |
| 173 | int buffer_size = (int)(write_ptr - buffer); |
| 174 | if (rewrite_count > highwater_store) highwater_store = rewrite_count; |
| 175 | if (buffer_size > highwater_buffer) highwater_buffer = buffer_size; |
| 176 | #endif |
| 177 | |
| 178 | int length = (int)(write_ptr - buffer); |
| 179 | char * copy = (char *)malloc(length + 1); |
| 180 | memcpy(copy, buffer, length); |
| 181 | copy[length] = '\0'; |
| 182 | return copy; |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | |
| 187 | //---------------------------------------------------- |
| 188 | // Grow methods |
| 189 | // |
| 190 | // Manage the storage used during demangling |
| 191 | //---------------------------------------------------- |
| 192 | |
| 193 | void GrowBuffer(long min_growth = 0) |
| 194 | { |
| 195 | // By default, double the size of the buffer |
| 196 | long growth = buffer_end - buffer; |
| 197 | |
| 198 | // Avoid growing by more than 1MB at a time |
| 199 | if (growth > 1 << 20) growth = 1 << 20; |
| 200 | |
| 201 | // ... but never grow by less than requested, |
| 202 | // or 1K, whichever is greater |
| 203 | if (min_growth < 1024) min_growth = 1024; |
| 204 | if (growth < min_growth) growth = min_growth; |
| 205 | |
| 206 | // Allocate the new buffer and migrate content |
| 207 | long new_size = (buffer_end - buffer) + growth; |
| 208 | char * new_buffer = (char *)malloc(new_size); |
| 209 | memcpy(new_buffer, buffer, write_ptr - buffer); |
| 210 | if (owns_buffer) free(buffer); |
| 211 | owns_buffer = true; |
| 212 | |
| 213 | // Update references to the new buffer |
| 214 | write_ptr = new_buffer + (write_ptr - buffer); |
| 215 | buffer = new_buffer; |
| 216 | buffer_end = buffer + new_size; |
| 217 | } |
| 218 | |
| 219 | void GrowRewriteRanges() |
| 220 | { |
| 221 | // By default, double the size of the array |
| 222 | int growth = rewrite_ranges_size; |
| 223 | |
| 224 | // Apply reasonable minimum and maximum sizes for growth |
| 225 | if (growth > 128) growth = 128; |
| 226 | if (growth < 16) growth = 16; |
| 227 | |
| 228 | // Allocate the new array and migrate content |
| 229 | int bytes = (rewrite_ranges_size + growth) * sizeof(BufferRange); |
| 230 | BufferRange * new_ranges = (BufferRange *) malloc(bytes); |
| 231 | for (int index = 0; index < next_substitute_index; index++) |
| 232 | { |
| 233 | new_ranges[index] = rewrite_ranges[index]; |
| 234 | } |
| 235 | for (int index = rewrite_ranges_size - 1; |
| 236 | index > next_template_arg_index; index--) |
| 237 | { |
| 238 | new_ranges[index + growth] = rewrite_ranges[index]; |
| 239 | } |
| 240 | if (owns_rewrite_ranges) free(rewrite_ranges); |
| 241 | owns_rewrite_ranges = true; |
| 242 | |
| 243 | // Update references to the new array |
| 244 | rewrite_ranges = new_ranges; |
| 245 | rewrite_ranges_size += growth; |
| 246 | next_template_arg_index += growth; |
| 247 | } |
| 248 | |
| 249 | //---------------------------------------------------- |
| 250 | // Range and state management |
| 251 | //---------------------------------------------------- |
| 252 | |
| 253 | int GetStartCookie() |
| 254 | { |
| 255 | return (int)(write_ptr - buffer); |
| 256 | } |
| 257 | |
| 258 | BufferRange EndRange(int start_cookie) |
| 259 | { |
| 260 | return { start_cookie, (int)(write_ptr - (buffer + start_cookie)) }; |
| 261 | } |
| 262 | |
| 263 | void ReorderRange(BufferRange source_range, int insertion_point_cookie) |
| 264 | { |
| 265 | // Ensure there's room the preserve the source range |
| 266 | if (write_ptr + source_range.length > buffer_end) |
| 267 | { |
| 268 | GrowBuffer(write_ptr + source_range.length - buffer_end); |
| 269 | } |
| 270 | |
| 271 | // Reorder the content |
| 272 | memcpy(write_ptr, buffer + source_range.offset, source_range.length); |
| 273 | memmove(buffer + insertion_point_cookie + source_range.length, |
| 274 | buffer + insertion_point_cookie, |
| 275 | source_range.offset - insertion_point_cookie); |
| 276 | memcpy(buffer + insertion_point_cookie, write_ptr, source_range.length); |
| 277 | |
| 278 | // Fix up rewritable ranges, covering both substitutions and templates |
| 279 | int index = 0; |
| 280 | while (true) |
| 281 | { |
| 282 | if (index == next_substitute_index) index = next_template_arg_index + 1; |
| 283 | if (index == rewrite_ranges_size) break; |
| 284 | |
| 285 | // Affected ranges are either shuffled forward when after the |
| 286 | // insertion but before the source, or backward when inside the |
| 287 | // source |
| 288 | int candidate_offset = rewrite_ranges[index].offset; |
| 289 | if (candidate_offset >= insertion_point_cookie) |
| 290 | { |
| 291 | if (candidate_offset < source_range.offset) |
| 292 | { |
| 293 | rewrite_ranges[index].offset += source_range.length; |
| 294 | } |
| 295 | else if (candidate_offset >= source_range.offset) |
| 296 | { |
| 297 | rewrite_ranges[index].offset -= |
| 298 | (source_range.offset - insertion_point_cookie); |
| 299 | } |
| 300 | } |
| 301 | ++index; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void EndSubstitution(int start_cookie) |
| 306 | { |
| 307 | if (next_substitute_index == next_template_arg_index) GrowRewriteRanges(); |
| 308 | |
| 309 | int index = next_substitute_index++; |
| 310 | rewrite_ranges[index] = EndRange(start_cookie); |
| 311 | #ifdef DEBUG_SUBSTITUTIONS |
| 312 | printf("Saved substitution # %d = %.*s\n", index, |
| 313 | rewrite_ranges[index].length, buffer + start_cookie); |
| 314 | #endif |
| 315 | } |
| 316 | |
| 317 | void EndTemplateArg(int start_cookie) |
| 318 | { |
| 319 | if (next_substitute_index == next_template_arg_index) GrowRewriteRanges(); |
| 320 | |
| 321 | int index = next_template_arg_index--; |
| 322 | rewrite_ranges[index] = EndRange(start_cookie); |
| 323 | #ifdef DEBUG_TEMPLATE_ARGS |
| 324 | printf("Saved template arg # %d = %.*s\n", |
| 325 | rewrite_ranges_size - index - 1, |
| 326 | rewrite_ranges[index].length, buffer + start_cookie); |
| 327 | #endif |
| 328 | } |
| 329 | |
| 330 | void ResetTemplateArgs() |
| 331 | { |
| 332 | //TODO: this works, but is it the right thing to do? |
| 333 | // Should we push/pop somehow at the call sites? |
| 334 | next_template_arg_index = rewrite_ranges_size - 1; |
| 335 | } |
| 336 | |
| 337 | //---------------------------------------------------- |
| 338 | // Write methods |
| 339 | // |
| 340 | // Appends content to the existing output buffer |
| 341 | //---------------------------------------------------- |
| 342 | |
| 343 | void Write(char character) |
| 344 | { |
| 345 | if (write_ptr == buffer_end) GrowBuffer(); |
| 346 | *write_ptr++ = character; |
| 347 | } |
| 348 | |
| 349 | void Write(const char * content) |
| 350 | { |
| 351 | Write(content, strlen(content)); |
| 352 | } |
| 353 | |
| 354 | void Write(const char * content, long content_length) |
| 355 | { |
| 356 | char * end_write_ptr = write_ptr + content_length; |
| 357 | if (end_write_ptr > buffer_end) |
| 358 | { |
| 359 | GrowBuffer(end_write_ptr - buffer_end); |
| 360 | end_write_ptr = write_ptr + content_length; |
| 361 | } |
| 362 | memcpy(write_ptr, content, content_length); |
| 363 | write_ptr = end_write_ptr; |
| 364 | } |
| 365 | #define WRITE(x) Write(x, sizeof(x) - 1) |
| 366 | |
| 367 | void WriteTemplateStart() |
| 368 | { |
| 369 | Write('<'); |
| 370 | } |
| 371 | |
| 372 | void WriteTemplateEnd() |
| 373 | { |
| 374 | // Put a space between terminal > characters when nesting templates |
| 375 | if (write_ptr != buffer && *(write_ptr - 1) == '>') WRITE(" >"); |
| 376 | else Write('>'); |
| 377 | } |
| 378 | |
| 379 | void WriteCommaSpace() |
| 380 | { |
| 381 | WRITE(", "); |
| 382 | } |
| 383 | |
| 384 | void WriteNamespaceSeparator() |
| 385 | { |
| 386 | WRITE("::"); |
| 387 | } |
| 388 | |
| 389 | void WriteStdPrefix() |
| 390 | { |
| 391 | WRITE("std::"); |
| 392 | } |
| 393 | |
| 394 | void WriteQualifiers(int qualifiers, bool space_before_reference = true) |
| 395 | { |
| 396 | if (qualifiers & QualifierPointer) Write('*'); |
| 397 | if (qualifiers & QualifierConst) WRITE(" const"); |
| 398 | if (qualifiers & QualifierVolatile) WRITE(" volatile"); |
| 399 | if (qualifiers & QualifierRestrict) WRITE(" restrict"); |
| 400 | if (qualifiers & QualifierReference) |
| 401 | { |
| 402 | if (space_before_reference) WRITE(" &"); |
| 403 | else Write('&'); |
| 404 | } |
| 405 | if (qualifiers & QualifierRValueReference) |
| 406 | { |
| 407 | if (space_before_reference) WRITE(" &&"); |
| 408 | else WRITE("&&"); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | //---------------------------------------------------- |
| 413 | // Rewrite methods |
| 414 | // |
| 415 | // Write another copy of content already present |
| 416 | // earlier in the output buffer |
| 417 | //---------------------------------------------------- |
| 418 | |
| 419 | void RewriteRange(BufferRange range) |
| 420 | { |
| 421 | Write(buffer + range.offset, range.length); |
| 422 | } |
| 423 | |
| 424 | bool RewriteSubstitution(int index) |
| 425 | { |
| 426 | if (index < 0 || index >= next_substitute_index) |
| 427 | { |
| 428 | #ifdef DEBUG_FAILURES |
| 429 | printf("*** Invalid substitution #%d\n", index); |
| 430 | #endif |
| 431 | return false; |
| 432 | } |
| 433 | RewriteRange(rewrite_ranges[index]); |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | bool RewriteTemplateArg(int template_index) |
| 438 | { |
| 439 | int index = rewrite_ranges_size - 1 - template_index; |
| 440 | if (template_index < 0 || index <= next_template_arg_index) |
| 441 | { |
| 442 | #ifdef DEBUG_FAILURES |
| 443 | printf("*** Invalid template arg reference #%d\n", template_index); |
| 444 | #endif |
| 445 | return false; |
| 446 | } |
| 447 | RewriteRange(rewrite_ranges[index]); |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | //---------------------------------------------------- |
| 452 | // TryParse methods |
| 453 | // |
| 454 | // Provide information with return values instead of |
| 455 | // writing to the output buffer |
| 456 | // |
| 457 | // Values indicating failure guarantee that the pre- |
| 458 | // call read_ptr is unchanged |
| 459 | //---------------------------------------------------- |
| 460 | |
| 461 | int TryParseNumber() |
| 462 | { |
| 463 | unsigned char digit = *read_ptr - '0'; |
| 464 | if (digit > 9) return -1; |
| 465 | |
| 466 | int count = digit; |
| 467 | while (true) |
| 468 | { |
| 469 | digit = *++read_ptr - '0'; |
| 470 | if (digit > 9) break; |
| 471 | |
| 472 | count = count * 10 + digit; |
| 473 | } |
| 474 | return count; |
| 475 | } |
| 476 | |
| 477 | int TryParseBase36Number() |
| 478 | { |
| 479 | char digit = *read_ptr; |
| 480 | int count; |
| 481 | if (digit >= '0' && digit <= '9') count = digit -= '0'; |
| 482 | else if (digit >= 'A' && digit <= 'Z') count = digit -= ('A' - 10); |
| 483 | else return -1; |
| 484 | |
| 485 | while (true) |
| 486 | { |
| 487 | digit = *++read_ptr; |
| 488 | if (digit >= '0' && digit <= '9') digit -= '0'; |
| 489 | else if (digit >= 'A' && digit <= 'Z') digit -= ('A' - 10); |
| 490 | else break; |
| 491 | |
| 492 | count = count * 36 + digit; |
| 493 | } |
| 494 | return count; |
| 495 | } |
| 496 | |
| 497 | // <builtin-type> ::= v # void |
| 498 | // ::= w # wchar_t |
| 499 | // ::= b # bool |
| 500 | // ::= c # char |
| 501 | // ::= a # signed char |
| 502 | // ::= h # unsigned char |
| 503 | // ::= s # short |
| 504 | // ::= t # unsigned short |
| 505 | // ::= i # int |
| 506 | // ::= j # unsigned int |
| 507 | // ::= l # long |
| 508 | // ::= m # unsigned long |
| 509 | // ::= x # long long, __int64 |
| 510 | // ::= y # unsigned long long, __int64 |
| 511 | // ::= n # __int128 |
| 512 | // ::= o # unsigned __int128 |
| 513 | // ::= f # float |
| 514 | // ::= d # double |
| 515 | // ::= e # long double, __float80 |
| 516 | // ::= g # __float128 |
| 517 | // ::= z # ellipsis |
| 518 | // ::= Dd # IEEE 754r decimal floating point (64 bits) |
| 519 | // ::= De # IEEE 754r decimal floating point (128 bits) |
| 520 | // ::= Df # IEEE 754r decimal floating point (32 bits) |
| 521 | // ::= Dh # IEEE 754r half-precision floating point (16 bits) |
| 522 | // ::= Di # char32_t |
| 523 | // ::= Ds # char16_t |
| 524 | // ::= Da # auto (in dependent new-expressions) |
| 525 | // ::= Dn # std::nullptr_t (i.e., decltype(nullptr)) |
| 526 | // ::= u <source-name> # vendor extended type |
| 527 | |
| 528 | const char * TryParseBuiltinType() |
| 529 | { |
| 530 | switch (*read_ptr++) |
| 531 | { |
| 532 | case 'v': return "void"; |
| 533 | case 'w': return "wchar_t"; |
| 534 | case 'b': return "bool"; |
| 535 | case 'c': return "char"; |
| 536 | case 'a': return "signed char"; |
| 537 | case 'h': return "unsigned char"; |
| 538 | case 's': return "short"; |
| 539 | case 't': return "unsigned short"; |
| 540 | case 'i': return "int"; |
| 541 | case 'j': return "unsigned int"; |
| 542 | case 'l': return "long"; |
| 543 | case 'm': return "unsigned long"; |
| 544 | case 'x': return "long long"; |
| 545 | case 'y': return "unsigned long long"; |
| 546 | case 'n': return "__int128"; |
| 547 | case 'o': return "unsigned __int128"; |
| 548 | case 'f': return "float"; |
| 549 | case 'd': return "double"; |
| 550 | case 'e': return "long double"; |
| 551 | case 'g': return "__float128"; |
| 552 | case 'z': return "..."; |
| 553 | case 'D': |
| 554 | { |
| 555 | switch (*read_ptr++) |
| 556 | { |
| 557 | case 'd': return "decimal64"; |
| 558 | case 'e': return "decimal128"; |
| 559 | case 'f': return "decimal32"; |
| 560 | case 'h': return "decimal16"; |
| 561 | case 'i': return "char32_t"; |
| 562 | case 's': return "char16_t"; |
| 563 | case 'a': return "auto"; |
| 564 | case 'c': return "decltype(auto)"; |
| 565 | case 'n': return "std::nullptr_t"; |
| 566 | default: |
| 567 | --read_ptr; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | --read_ptr; |
| 572 | return nullptr; |
| 573 | } |
| 574 | |
| 575 | // <operator-name> |
| 576 | // ::= aa # && |
| 577 | // ::= ad # & (unary) |
| 578 | // ::= an # & |
| 579 | // ::= aN # &= |
| 580 | // ::= aS # = |
| 581 | // ::= cl # () |
| 582 | // ::= cm # , |
| 583 | // ::= co # ~ |
| 584 | // ::= da # delete[] |
| 585 | // ::= de # * (unary) |
| 586 | // ::= dl # delete |
| 587 | // ::= dv # / |
| 588 | // ::= dV # /= |
| 589 | // ::= eo # ^ |
| 590 | // ::= eO # ^= |
| 591 | // ::= eq # == |
| 592 | // ::= ge # >= |
| 593 | // ::= gt # > |
| 594 | // ::= ix # [] |
| 595 | // ::= le # <= |
| 596 | // ::= ls # << |
| 597 | // ::= lS # <<= |
| 598 | // ::= lt # < |
| 599 | // ::= mi # - |
| 600 | // ::= mI # -= |
| 601 | // ::= ml # * |
| 602 | // ::= mL # *= |
| 603 | // ::= mm # -- (postfix in <expression> context) |
| 604 | // ::= na # new[] |
| 605 | // ::= ne # != |
| 606 | // ::= ng # - (unary) |
| 607 | // ::= nt # ! |
| 608 | // ::= nw # new |
| 609 | // ::= oo # || |
| 610 | // ::= or # | |
| 611 | // ::= oR # |= |
| 612 | // ::= pm # ->* |
| 613 | // ::= pl # + |
| 614 | // ::= pL # += |
| 615 | // ::= pp # ++ (postfix in <expression> context) |
| 616 | // ::= ps # + (unary) |
| 617 | // ::= pt # -> |
| 618 | // ::= qu # ? |
| 619 | // ::= rm # % |
| 620 | // ::= rM # %= |
| 621 | // ::= rs # >> |
| 622 | // ::= rS # >>= |
| 623 | // ::= cv <type> # (cast) |
| 624 | // ::= v <digit> <source-name> # vendor extended operator |
| 625 | |
| 626 | Operator TryParseOperator() |
| 627 | { |
| 628 | switch (*read_ptr++) |
| 629 | { |
| 630 | case 'a': |
| 631 | switch (*read_ptr++) |
| 632 | { |
| 633 | case 'a': return { "&&", OperatorKind::Binary }; |
| 634 | case 'd': return { "&", OperatorKind::Unary }; |
| 635 | case 'n': return { "&", OperatorKind::Binary }; |
| 636 | case 'N': return { "&=", OperatorKind::Binary }; |
| 637 | case 'S': return { "=", OperatorKind::Binary }; |
| 638 | } |
| 639 | --read_ptr; |
| 640 | break; |
| 641 | case 'c': |
| 642 | switch (*read_ptr++) |
| 643 | { |
| 644 | case 'l': return { "()", OperatorKind::Other }; |
| 645 | case 'm': return { ",", OperatorKind::Other }; |
| 646 | case 'o': return { "~", OperatorKind::Unary }; |
| 647 | case 'v': return { nullptr, OperatorKind::ConversionOperator }; |
| 648 | } |
| 649 | --read_ptr; |
| 650 | break; |
| 651 | case 'd': |
| 652 | switch (*read_ptr++) |
| 653 | { |
| 654 | case 'a': return { " delete[]", OperatorKind::Other }; |
| 655 | case 'e': return { "*", OperatorKind::Unary }; |
| 656 | case 'l': return { " delete", OperatorKind::Other }; |
| 657 | case 'v': return { "/", OperatorKind::Binary }; |
| 658 | case 'V': return { "/=", OperatorKind::Binary }; |
| 659 | } |
| 660 | --read_ptr; |
| 661 | break; |
| 662 | case 'e': |
| 663 | switch (*read_ptr++) |
| 664 | { |
| 665 | case 'o': return { "^", OperatorKind::Binary }; |
| 666 | case 'O': return { "^=", OperatorKind::Binary }; |
| 667 | case 'q': return { "==", OperatorKind::Binary }; |
| 668 | } |
| 669 | --read_ptr; |
| 670 | break; |
| 671 | case 'g': |
| 672 | switch (*read_ptr++) |
| 673 | { |
| 674 | case 'e': return { ">=", OperatorKind::Binary }; |
| 675 | case 't': return { ">", OperatorKind::Binary }; |
| 676 | } |
| 677 | --read_ptr; |
| 678 | break; |
| 679 | case 'i': |
| 680 | switch (*read_ptr++) |
| 681 | { |
| 682 | case 'x': return { "[]", OperatorKind::Other }; |
| 683 | } |
| 684 | --read_ptr; |
| 685 | break; |
| 686 | case 'l': |
| 687 | switch (*read_ptr++) |
| 688 | { |
| 689 | case 'e': return { "<=", OperatorKind::Binary }; |
| 690 | case 's': return { "<<", OperatorKind::Binary }; |
| 691 | case 'S': return { "<<=", OperatorKind::Binary }; |
| 692 | case 't': return { "<", OperatorKind::Binary }; |
| 693 | // case 'i': return { "?", OperatorKind::Binary }; |
| 694 | } |
| 695 | --read_ptr; |
| 696 | break; |
| 697 | case 'm': |
| 698 | switch (*read_ptr++) |
| 699 | { |
| 700 | case 'i': return { "-", OperatorKind::Binary }; |
| 701 | case 'I': return { "-=", OperatorKind::Binary }; |
| 702 | case 'l': return { "*", OperatorKind::Binary }; |
| 703 | case 'L': return { "*=", OperatorKind::Binary }; |
| 704 | case 'm': return { "--", OperatorKind::Postfix }; |
| 705 | } |
| 706 | --read_ptr; |
| 707 | break; |
| 708 | case 'n': |
| 709 | switch (*read_ptr++) |
| 710 | { |
| 711 | case 'a': return { " new[]", OperatorKind::Other }; |
| 712 | case 'e': return { "!=", OperatorKind::Binary }; |
| 713 | case 'g': return { "-", OperatorKind::Unary }; |
| 714 | case 't': return { "!", OperatorKind::Unary }; |
| 715 | case 'w': return { " new", OperatorKind::Other }; |
| 716 | } |
| 717 | --read_ptr; |
| 718 | break; |
| 719 | case 'o': |
| 720 | switch (*read_ptr++) |
| 721 | { |
| 722 | case 'o': return { "||", OperatorKind::Binary }; |
| 723 | case 'r': return { "|", OperatorKind::Binary }; |
| 724 | case 'R': return { "|=", OperatorKind::Binary }; |
| 725 | } |
| 726 | --read_ptr; |
| 727 | break; |
| 728 | case 'p': |
| 729 | switch (*read_ptr++) |
| 730 | { |
| 731 | case 'm': return { "->*", OperatorKind::Binary }; |
| 732 | case 's': return { "+", OperatorKind::Unary }; |
| 733 | case 'l': return { "+", OperatorKind::Binary }; |
| 734 | case 'L': return { "+=", OperatorKind::Binary }; |
| 735 | case 'p': return { "++", OperatorKind::Postfix }; |
| 736 | case 't': return { "->", OperatorKind::Binary }; |
| 737 | } |
| 738 | --read_ptr; |
| 739 | break; |
| 740 | case 'q': |
| 741 | switch (*read_ptr++) |
| 742 | { |
| 743 | case 'u': return { "?", OperatorKind::Ternary }; |
| 744 | } |
| 745 | --read_ptr; |
| 746 | break; |
| 747 | case 'r': |
| 748 | switch (*read_ptr++) |
| 749 | { |
| 750 | case 'm': return { "%", OperatorKind::Binary }; |
| 751 | case 'M': return { "%=", OperatorKind::Binary }; |
| 752 | case 's': return { ">>", OperatorKind::Binary }; |
| 753 | case 'S': return { ">=", OperatorKind::Binary }; |
| 754 | } |
| 755 | --read_ptr; |
| 756 | break; |
| 757 | case 'v': |
| 758 | char digit = *read_ptr; |
| 759 | if (digit >= '0' && digit <= '9') |
| 760 | { |
| 761 | read_ptr++; |
| 762 | return { nullptr, OperatorKind::Vendor }; |
| 763 | } |
| 764 | --read_ptr; |
| 765 | break; |
| 766 | } |
| 767 | --read_ptr; |
| 768 | return { nullptr, OperatorKind::NoMatch }; |
| 769 | } |
| 770 | |
| 771 | // <CV-qualifiers> ::= [r] [V] [K] |
| 772 | // <ref-qualifier> ::= R # & ref-qualifier |
| 773 | // <ref-qualifier> ::= O # && ref-qualifier |
| 774 | |
| 775 | int TryParseQualifiers(bool allow_cv, bool allow_ro) |
| 776 | { |
| 777 | int qualifiers = QualifierNone; |
| 778 | char next = *read_ptr; |
| 779 | if (allow_cv) |
| 780 | { |
| 781 | if (next == 'r') // restrict |
| 782 | { |
| 783 | qualifiers |= QualifierRestrict; |
| 784 | next = *++read_ptr; |
| 785 | } |
| 786 | if (next == 'V') // volatile |
| 787 | { |
| 788 | qualifiers |= QualifierVolatile; |
| 789 | next = *++read_ptr; |
| 790 | } |
| 791 | if (next == 'K') // const |
| 792 | { |
| 793 | qualifiers |= QualifierConst; |
| 794 | next = *++read_ptr; |
| 795 | } |
| 796 | } |
| 797 | if (allow_ro) |
| 798 | { |
| 799 | if (next == 'R') |
| 800 | { |
| 801 | ++read_ptr; |
| 802 | qualifiers |= QualifierReference; |
| 803 | } |
| 804 | else if (next =='O') |
| 805 | { |
| 806 | ++read_ptr; |
| 807 | qualifiers |= QualifierRValueReference; |
| 808 | } |
| 809 | } |
| 810 | return qualifiers; |
| 811 | } |
| 812 | |
| 813 | // <discriminator> := _ <non-negative number> # when number < 10 |
| 814 | // := __ <non-negative number> _ # when number >= 10 |
| 815 | // extension := decimal-digit+ |
| 816 | |
| 817 | int TryParseDiscriminator() |
| 818 | { |
| 819 | const char * discriminator_start = read_ptr; |
| 820 | |
| 821 | // Test the extension first, since it's what Clang uses |
| 822 | int discriminator_value = TryParseNumber(); |
| 823 | if (discriminator_value != -1) return discriminator_value; |
| 824 | |
| 825 | char next = *read_ptr; |
| 826 | if (next == '_') |
| 827 | { |
| 828 | next = *++read_ptr; |
| 829 | if (next == '_') |
| 830 | { |
| 831 | ++read_ptr; |
| 832 | discriminator_value = TryParseNumber(); |
| 833 | if (discriminator_value != -1 && *read_ptr++ != '_') |
| 834 | { |
| 835 | return discriminator_value; |
| 836 | } |
| 837 | } |
| 838 | else if (next >= '0' && next <= '9') |
| 839 | { |
| 840 | ++read_ptr; |
| 841 | return next - '0'; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | // Not a valid discriminator |
| 846 | read_ptr = discriminator_start; |
| 847 | return -1; |
| 848 | } |
| 849 | |
| 850 | //---------------------------------------------------- |
| 851 | // Parse methods |
| 852 | // |
| 853 | // Consume input starting from read_ptr and produce |
| 854 | // buffered output at write_ptr |
| 855 | // |
| 856 | // Failures return false and may leave read_ptr in an |
| 857 | // indeterminate state |
| 858 | //---------------------------------------------------- |
| 859 | |
| 860 | bool Parse(char character) |
| 861 | { |
| 862 | if (*read_ptr++ == character) return true; |
| 863 | #ifdef DEBUG_FAILURES |
| 864 | printf("*** Expected '%c'\n", character); |
| 865 | #endif |
| 866 | return false; |
| 867 | } |
| 868 | |
| 869 | // <number> ::= [n] <non-negative decimal integer> |
| 870 | |
| 871 | bool ParseNumber(bool allow_negative = false) |
| 872 | { |
| 873 | if (allow_negative && *read_ptr == 'n') |
| 874 | { |
| 875 | Write('-'); |
| 876 | ++read_ptr; |
| 877 | } |
| 878 | const char * before_digits = read_ptr; |
| 879 | while (true) |
| 880 | { |
| 881 | unsigned char digit = *read_ptr - '0'; |
| 882 | if (digit > 9) break; |
| 883 | ++read_ptr; |
| 884 | } |
| 885 | if (int digit_count = (int)(read_ptr - before_digits)) |
| 886 | { |
| 887 | Write(before_digits, digit_count); |
| 888 | return true; |
| 889 | } |
| 890 | #ifdef DEBUG_FAILURES |
| 891 | printf("*** Expected number\n"); |
| 892 | #endif |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | // <substitution> ::= S <seq-id> _ |
| 897 | // ::= S_ |
| 898 | // <substitution> ::= Sa # ::std::allocator |
| 899 | // <substitution> ::= Sb # ::std::basic_string |
| 900 | // <substitution> ::= Ss # ::std::basic_string < char, |
| 901 | // ::std::char_traits<char>, |
| 902 | // ::std::allocator<char> > |
| 903 | // <substitution> ::= Si # ::std::basic_istream<char, std::char_traits<char> > |
| 904 | // <substitution> ::= So # ::std::basic_ostream<char, std::char_traits<char> > |
| 905 | // <substitution> ::= Sd # ::std::basic_iostream<char, std::char_traits<char> > |
| 906 | |
| 907 | bool ParseSubstitution() |
| 908 | { |
| 909 | const char * substitution; |
| 910 | switch (*read_ptr) |
| 911 | { |
| 912 | case 'a': substitution = "std::allocator"; break; |
| 913 | case 'b': substitution = "std::basic_string"; break; |
| 914 | case 's': substitution = "std::string"; break; |
| 915 | case 'i': substitution = "std::istream"; break; |
| 916 | case 'o': substitution = "std::ostream"; break; |
| 917 | case 'd': substitution = "std::iostream"; break; |
| 918 | default: |
| 919 | // A failed attempt to parse a number will return -1 which turns out to be |
| 920 | // perfect here as S_ is the first substitution, S0_ the next and so forth |
| 921 | int substitution_index = TryParseBase36Number(); |
| 922 | if (*read_ptr++ != '_') |
| 923 | { |
| 924 | #ifdef DEBUG_FAILURES |
| 925 | printf("*** Expected terminal _ in substitution\n"); |
| 926 | #endif |
| 927 | return false; |
| 928 | } |
| 929 | return RewriteSubstitution(substitution_index + 1); |
| 930 | } |
| 931 | Write(substitution); |
| 932 | ++read_ptr; |
| 933 | return true; |
| 934 | } |
| 935 | |
| 936 | // <function-type> ::= F [Y] <bare-function-type> [<ref-qualifier>] E |
| 937 | // |
| 938 | // <bare-function-type> ::= <signature type>+ # types are possible return type, then parameter types |
| 939 | |
| 940 | bool ParseFunctionType(int inner_qualifiers = QualifierNone) |
| 941 | { |
| 942 | #ifdef DEBUG_FAILURES |
| 943 | printf("*** Function types not supported\n"); |
| 944 | #endif |
| 945 | //TODO: first steps toward an implementation follow, but they're far |
| 946 | // from complete. Function types tend to bracket other types eg: |
| 947 | // int (*)() when used as the type for "name" becomes int (*name)(). |
| 948 | // This makes substitution et al ... interesting. |
| 949 | return false; |
| 950 | |
| 951 | if (*read_ptr == 'Y') ++read_ptr;; |
| 952 | |
| 953 | int return_type_start_cookie = GetStartCookie(); |
| 954 | if (!ParseType()) return false; |
| 955 | Write(' '); |
| 956 | |
| 957 | int insert_cookie = GetStartCookie(); |
| 958 | Write('('); |
| 959 | bool first_param = true; |
| 960 | int qualifiers = QualifierNone; |
| 961 | while (true) |
| 962 | { |
| 963 | switch (*read_ptr) |
| 964 | { |
| 965 | case 'E': |
| 966 | ++read_ptr; |
| 967 | Write(')'); |
| 968 | break; |
| 969 | case 'v': |
| 970 | ++read_ptr; |
| 971 | continue; |
| 972 | case 'R': |
| 973 | case 'O': |
| 974 | if (*(read_ptr + 1) == 'E') |
| 975 | { |
| 976 | qualifiers = TryParseQualifiers(false, true); |
| 977 | Parse('E'); |
| 978 | break; |
| 979 | } |
| 980 | // fallthrough |
| 981 | default: |
| 982 | { |
| 983 | if (first_param) first_param = false; |
| 984 | else WriteCommaSpace(); |
| 985 | |
| 986 | if (!ParseType()) return false; |
| 987 | continue; |
| 988 | } |
| 989 | } |
| 990 | break; |
| 991 | } |
| 992 | |
| 993 | if (qualifiers) |
| 994 | { |
| 995 | WriteQualifiers(qualifiers); |
| 996 | EndSubstitution(return_type_start_cookie); |
| 997 | } |
| 998 | |
| 999 | if (inner_qualifiers) |
| 1000 | { |
| 1001 | int qualifier_start_cookie = GetStartCookie(); |
| 1002 | Write('('); |
| 1003 | WriteQualifiers(inner_qualifiers); |
| 1004 | Write(')'); |
| 1005 | ReorderRange(EndRange(qualifier_start_cookie), insert_cookie); |
| 1006 | } |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
| 1010 | // <array-type> ::= A <positive dimension number> _ <element type> |
| 1011 | // ::= A [<dimension expression>] _ <element type> |
| 1012 | |
| 1013 | bool ParseArrayType(int qualifiers = QualifierNone) |
| 1014 | { |
| 1015 | #ifdef DEBUG_FAILURES |
| 1016 | printf("*** Array type unsupported\n"); |
| 1017 | #endif |
| 1018 | //TODO: We fail horribly when recalling these as substitutions or |
| 1019 | // templates and trying to constify them eg: |
| 1020 | // _ZN4llvm2cl5applyIA28_cNS0_3optIbLb0ENS0_6parserIbEEEEEEvRKT_PT0_ |
| 1021 | // |
| 1022 | //TODO: Chances are we don't do any better with references and pointers |
| 1023 | // that should be type (&) [] instead of type & [] |
| 1024 | |
| 1025 | return false; |
| 1026 | |
| 1027 | if (*read_ptr == '_') |
| 1028 | { |
| 1029 | ++read_ptr; |
| 1030 | if (!ParseType()) return false; |
| 1031 | if (qualifiers) WriteQualifiers(qualifiers); |
| 1032 | WRITE(" []"); |
| 1033 | return true; |
| 1034 | } |
| 1035 | else |
| 1036 | { |
| 1037 | const char * before_digits = read_ptr; |
| 1038 | if (TryParseNumber() != -1) |
| 1039 | { |
| 1040 | const char * after_digits = read_ptr; |
| 1041 | if (!Parse('_')) return false; |
| 1042 | if (!ParseType()) return false; |
| 1043 | if (qualifiers) WriteQualifiers(qualifiers); |
| 1044 | Write(' '); |
| 1045 | Write('['); |
| 1046 | Write(before_digits, after_digits - before_digits); |
| 1047 | } |
| 1048 | else |
| 1049 | { |
| 1050 | int type_insertion_cookie = GetStartCookie(); |
| 1051 | if (!ParseExpression()) return false; |
| 1052 | if (!Parse('_')) return false; |
| 1053 | |
| 1054 | int type_start_cookie = GetStartCookie(); |
| 1055 | if (!ParseType()) return false; |
| 1056 | if (qualifiers) WriteQualifiers(qualifiers); |
| 1057 | Write(' '); |
| 1058 | Write('['); |
| 1059 | ReorderRange(EndRange(type_start_cookie), type_insertion_cookie); |
| 1060 | } |
| 1061 | Write(']'); |
| 1062 | return true; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | // <pointer-to-member-type> ::= M <class type> <member type> |
| 1067 | |
| 1068 | //TODO: Determine how to handle pointers to function members correctly, |
| 1069 | // currently not an issue because we don't have function types at all... |
| 1070 | bool ParsePointerToMemberType() |
| 1071 | { |
| 1072 | int insertion_cookie = GetStartCookie(); |
| 1073 | Write(' '); |
| 1074 | if (!ParseType()) return false; |
| 1075 | WRITE("::*"); |
| 1076 | |
| 1077 | int type_cookie = GetStartCookie(); |
| 1078 | if (!ParseType()) return false; |
| 1079 | ReorderRange(EndRange(type_cookie), insertion_cookie); |
| 1080 | return true; |
| 1081 | } |
| 1082 | |
| 1083 | // <template-param> ::= T_ # first template parameter |
| 1084 | // ::= T <parameter-2 non-negative number> _ |
| 1085 | |
| 1086 | bool ParseTemplateParam() |
| 1087 | { |
| 1088 | int count = TryParseNumber(); |
| 1089 | if (!Parse('_')) return false; |
| 1090 | |
| 1091 | // When no number is present we get -1, which is convenient since |
| 1092 | // T_ is the zeroth element T0_ is element 1, and so on |
| 1093 | return RewriteTemplateArg(count + 1); |
| 1094 | } |
| 1095 | |
| 1096 | // <type> ::= <builtin-type> |
| 1097 | // ::= <function-type> |
| 1098 | // ::= <class-enum-type> |
| 1099 | // ::= <array-type> |
| 1100 | // ::= <pointer-to-member-type> |
| 1101 | // ::= <template-param> |
| 1102 | // ::= <template-template-param> <template-args> |
| 1103 | // ::= <decltype> |
| 1104 | // ::= <substitution> |
| 1105 | // ::= <CV-qualifiers> <type> |
| 1106 | // ::= P <type> # pointer-to |
| 1107 | // ::= R <type> # reference-to |
| 1108 | // ::= O <type> # rvalue reference-to (C++0x) |
| 1109 | // ::= C <type> # complex pair (C 2000) |
| 1110 | // ::= G <type> # imaginary (C 2000) |
| 1111 | // ::= Dp <type> # pack expansion (C++0x) |
| 1112 | // ::= U <source-name> <type> # vendor extended type qualifier |
| 1113 | // extension := U <objc-name> <objc-type> # objc-type<identifier> |
| 1114 | // extension := <vector-type> # <vector-type> starts with Dv |
| 1115 | |
| 1116 | // <objc-name> ::= <k0 number> objcproto <k1 number> <identifier> # k0 = 9 + <number of digits in k1> + k1 |
| 1117 | // <objc-type> := <source-name> # PU<11+>objcproto 11objc_object<source-name> 11objc_object -> id<source-name> |
| 1118 | |
| 1119 | bool ParseType() |
| 1120 | { |
| 1121 | #ifdef DEBUG_FAILURES |
| 1122 | const char * failed_type = read_ptr; |
| 1123 | #endif |
| 1124 | int type_start_cookie = GetStartCookie(); |
| 1125 | bool suppress_substitution = false; |
| 1126 | |
| 1127 | int qualifiers = TryParseQualifiers(true, false); |
| 1128 | switch (*read_ptr) |
| 1129 | { |
| 1130 | case 'D': |
| 1131 | ++read_ptr; |
| 1132 | switch (*read_ptr++) |
| 1133 | { |
| 1134 | case 'p': |
| 1135 | if (!ParseType()) return false; |
| 1136 | break; |
| 1137 | case 'T': |
| 1138 | case 't': |
| 1139 | case 'v': |
| 1140 | default: |
| 1141 | #ifdef DEBUG_FAILURES |
| 1142 | printf("*** Unsupported type: %.3s\n", failed_type); |
| 1143 | #endif |
| 1144 | return false; |
| 1145 | } |
| 1146 | break; |
| 1147 | case 'T': |
| 1148 | ++read_ptr; |
| 1149 | if (!ParseTemplateParam()) return false; |
| 1150 | break; |
| 1151 | case 'M': |
| 1152 | ++read_ptr; |
| 1153 | if (!ParsePointerToMemberType()) return false; |
| 1154 | break; |
| 1155 | case 'A': |
| 1156 | ++read_ptr; |
| 1157 | if (!ParseArrayType()) return false; |
| 1158 | break; |
| 1159 | case 'F': |
| 1160 | ++read_ptr; |
| 1161 | if (!ParseFunctionType()) return false; |
| 1162 | break; |
| 1163 | case 'S': |
| 1164 | if (*++read_ptr == 't') |
| 1165 | { |
| 1166 | ++read_ptr; |
| 1167 | WriteStdPrefix(); |
| 1168 | if (!ParseName()) return false; |
| 1169 | } |
| 1170 | else |
| 1171 | { |
| 1172 | suppress_substitution = true; |
| 1173 | if (!ParseSubstitution()) return false; |
| 1174 | } |
| 1175 | break; |
| 1176 | case 'P': |
| 1177 | { |
| 1178 | switch (*++read_ptr) |
| 1179 | { |
| 1180 | case 'F': |
| 1181 | ++read_ptr; |
| 1182 | if (!ParseFunctionType(QualifierPointer)) return false; |
| 1183 | break; |
| 1184 | default: |
| 1185 | if (!ParseType()) return false; |
| 1186 | Write('*'); |
| 1187 | break; |
| 1188 | } |
| 1189 | break; |
| 1190 | } |
| 1191 | case 'R': |
| 1192 | { |
| 1193 | ++read_ptr; |
| 1194 | if (!ParseType()) return false; |
| 1195 | Write('&'); |
| 1196 | break; |
| 1197 | } |
| 1198 | case 'O': |
| 1199 | { |
| 1200 | ++read_ptr; |
| 1201 | if (!ParseType()) return false; |
| 1202 | Write('&'); |
| 1203 | Write('&'); |
| 1204 | break; |
| 1205 | } |
| 1206 | case 'C': |
| 1207 | case 'G': |
| 1208 | case 'U': |
| 1209 | #ifdef DEBUG_FAILURES |
| 1210 | printf("*** Unsupported type: %.3s\n", failed_type); |
| 1211 | #endif |
| 1212 | return false; |
| 1213 | // Test for common cases to avoid TryParseBuiltinType() overhead |
| 1214 | case 'N': |
| 1215 | case 'Z': |
| 1216 | case 'L': |
| 1217 | if (!ParseName()) return false; |
| 1218 | break; |
| 1219 | default: |
| 1220 | if (const char * builtin = TryParseBuiltinType()) |
| 1221 | { |
| 1222 | Write(builtin); |
| 1223 | suppress_substitution = true; |
| 1224 | } |
| 1225 | else |
| 1226 | { |
| 1227 | if (!ParseName()) return false; |
| 1228 | } |
| 1229 | break; |
| 1230 | } |
| 1231 | |
| 1232 | // Allow base substitutions to be suppressed, but always record |
| 1233 | // substitutions for the qualified variant |
| 1234 | if (!suppress_substitution) EndSubstitution(type_start_cookie); |
| 1235 | if (qualifiers) |
| 1236 | { |
| 1237 | WriteQualifiers(qualifiers, false); |
| 1238 | EndSubstitution(type_start_cookie); |
| 1239 | } |
| 1240 | return true; |
| 1241 | } |
| 1242 | |
| 1243 | // <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ |
| 1244 | // ::= <closure-type-name> |
| 1245 | // |
| 1246 | // <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ |
| 1247 | // |
| 1248 | // <lambda-sig> ::= <parameter type>+ # Parameter types or "v" if the lambda has no parameters |
| 1249 | |
| 1250 | bool ParseUnnamedTypeName(NameState & name_state) |
| 1251 | { |
| 1252 | switch (*read_ptr++) |
| 1253 | { |
| 1254 | case 't': |
| 1255 | { |
| 1256 | int cookie = GetStartCookie(); |
| 1257 | WRITE("'unnamed"); |
| 1258 | const char * before_digits = read_ptr; |
| 1259 | if (TryParseNumber() != -1) Write(before_digits, |
| 1260 | read_ptr - before_digits); |
| 1261 | if (!Parse('_')) return false; |
| 1262 | Write('\''); |
| 1263 | name_state.last_name_range = EndRange(cookie); |
| 1264 | return true; |
| 1265 | } |
| 1266 | case 'b': |
| 1267 | { |
| 1268 | int cookie = GetStartCookie(); |
| 1269 | WRITE("'block"); |
| 1270 | const char * before_digits = read_ptr; |
| 1271 | if (TryParseNumber() != -1) Write(before_digits, |
| 1272 | read_ptr - before_digits); |
| 1273 | if (!Parse('_')) return false; |
| 1274 | Write('\''); |
| 1275 | name_state.last_name_range = EndRange(cookie); |
| 1276 | return true; |
| 1277 | } |
| 1278 | case 'l': |
| 1279 | #ifdef DEBUG_FAILURES |
| 1280 | printf("*** Lambda type names unsupported\n"); |
| 1281 | #endif |
| 1282 | return false; |
| 1283 | } |
| 1284 | #ifdef DEBUG_FAILURES |
| 1285 | printf("*** Unknown unnamed type %.3s\n", read_ptr - 2); |
| 1286 | #endif |
| 1287 | return false; |
| 1288 | } |
| 1289 | |
| 1290 | // <ctor-dtor-name> ::= C1 # complete object constructor |
| 1291 | // ::= C2 # base object constructor |
| 1292 | // ::= C3 # complete object allocating constructor |
| 1293 | |
| 1294 | bool ParseCtor(NameState & name_state) |
| 1295 | { |
| 1296 | char next = *read_ptr; |
| 1297 | if (next == '1' || next == '2' || next == '3' || next == '5') |
| 1298 | { |
| 1299 | RewriteRange(name_state.last_name_range); |
| 1300 | name_state.has_no_return_type = true; |
| 1301 | ++read_ptr; |
| 1302 | return true; |
| 1303 | } |
| 1304 | #ifdef DEBUG_FAILURES |
| 1305 | printf("*** Broken constructor\n"); |
| 1306 | #endif |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
| 1310 | // <ctor-dtor-name> ::= D0 # deleting destructor |
| 1311 | // ::= D1 # complete object destructor |
| 1312 | // ::= D2 # base object destructor |
| 1313 | |
| 1314 | bool ParseDtor(NameState & name_state) |
| 1315 | { |
| 1316 | char next = *read_ptr; |
| 1317 | if (next == '0' || next == '1' || next == '2' || next == '5') |
| 1318 | { |
| 1319 | Write('~'); |
| 1320 | RewriteRange(name_state.last_name_range); |
| 1321 | name_state.has_no_return_type = true; |
| 1322 | ++read_ptr; |
| 1323 | return true; |
| 1324 | } |
| 1325 | #ifdef DEBUG_FAILURES |
| 1326 | printf("*** Broken destructor\n"); |
| 1327 | #endif |
| 1328 | return false; |
| 1329 | } |
| 1330 | |
| 1331 | // See TryParseOperator() |
| 1332 | |
| 1333 | bool ParseOperatorName(NameState & name_state) |
| 1334 | { |
| 1335 | #ifdef DEBUG_FAILURES |
| 1336 | const char * operator_ptr = read_ptr; |
| 1337 | #endif |
| 1338 | Operator parsed_operator = TryParseOperator(); |
| 1339 | if (parsed_operator.name) |
| 1340 | { |
| 1341 | WRITE("operator"); |
| 1342 | Write(parsed_operator.name); |
| 1343 | return true; |
| 1344 | } |
| 1345 | |
| 1346 | // Handle special operators |
| 1347 | switch (parsed_operator.kind) |
| 1348 | { |
| 1349 | case OperatorKind::Vendor: |
| 1350 | WRITE("operator "); |
| 1351 | return ParseSourceName(); |
| 1352 | case OperatorKind::ConversionOperator: |
| 1353 | ResetTemplateArgs(); |
| 1354 | name_state.has_no_return_type = true; |
| 1355 | WRITE("operator "); |
| 1356 | return ParseType(); |
| 1357 | default: |
| 1358 | #ifdef DEBUG_FAILURES |
| 1359 | printf("*** Unknown operator: %.2s\n", operator_ptr); |
| 1360 | #endif |
| 1361 | return false; |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | // <source-name> ::= <positive length number> <identifier> |
| 1366 | |
| 1367 | bool ParseSourceName() |
| 1368 | { |
| 1369 | int count = TryParseNumber(); |
| 1370 | if (count == -1) |
| 1371 | { |
| 1372 | #ifdef DEBUG_FAILURES |
| 1373 | printf("*** Malformed source name, missing length count\n"); |
| 1374 | #endif |
| 1375 | return false; |
| 1376 | } |
| 1377 | |
| 1378 | const char * next_read_ptr = read_ptr + count; |
| 1379 | if (next_read_ptr > read_end) |
| 1380 | { |
| 1381 | #ifdef DEBUG_FAILURES |
| 1382 | printf("*** Malformed source name, premature termination\n"); |
| 1383 | #endif |
| 1384 | return false; |
| 1385 | } |
| 1386 | |
| 1387 | if (count >= 10 && strncmp(read_ptr, "_GLOBAL__N", 10) == 0) WRITE("(anonymous namespace)"); |
| 1388 | else Write(read_ptr, count); |
| 1389 | |
| 1390 | read_ptr = next_read_ptr; |
| 1391 | return true; |
| 1392 | } |
| 1393 | |
| 1394 | // <unqualified-name> ::= <operator-name> |
| 1395 | // ::= <ctor-dtor-name> |
| 1396 | // ::= <source-name> |
| 1397 | // ::= <unnamed-type-name> |
| 1398 | |
| 1399 | bool ParseUnqualifiedName(NameState & name_state) |
| 1400 | { |
| 1401 | // Note that these are detected directly in ParseNestedName for |
| 1402 | // performance rather than switching on the same options twice |
| 1403 | char next = *read_ptr; |
| 1404 | switch (next) |
| 1405 | { |
| 1406 | case 'C': |
| 1407 | ++read_ptr; |
| 1408 | return ParseCtor(name_state); |
| 1409 | case 'D': |
| 1410 | ++read_ptr; |
| 1411 | return ParseDtor(name_state); |
| 1412 | case 'U': |
| 1413 | ++read_ptr; |
| 1414 | return ParseUnnamedTypeName(name_state); |
| 1415 | case '0': |
| 1416 | case '1': |
| 1417 | case '2': |
| 1418 | case '3': |
| 1419 | case '4': |
| 1420 | case '5': |
| 1421 | case '6': |
| 1422 | case '7': |
| 1423 | case '8': |
| 1424 | case '9': |
| 1425 | { |
| 1426 | int name_start_cookie = GetStartCookie(); |
| 1427 | if (!ParseSourceName()) return false; |
| 1428 | name_state.last_name_range = EndRange(name_start_cookie); |
| 1429 | return true; |
| 1430 | } |
| 1431 | default: |
| 1432 | return ParseOperatorName(name_state); |
| 1433 | }; |
| 1434 | } |
| 1435 | |
| 1436 | // <unscoped-name> ::= <unqualified-name> |
| 1437 | // ::= St <unqualified-name> # ::std:: |
| 1438 | // extension ::= StL<unqualified-name> |
| 1439 | |
| 1440 | bool ParseUnscopedName(NameState & name_state) |
| 1441 | { |
| 1442 | if (*read_ptr == 'S' && *(read_ptr + 1) == 't') |
| 1443 | { |
| 1444 | WriteStdPrefix(); |
| 1445 | if (*(read_ptr += 2) == 'L') ++read_ptr; |
| 1446 | } |
| 1447 | return ParseUnqualifiedName(name_state); |
| 1448 | } |
| 1449 | |
| 1450 | bool ParseIntegerLiteral(const char * prefix, const char * suffix, |
| 1451 | bool allow_negative) |
| 1452 | { |
| 1453 | if (prefix) Write(prefix); |
| 1454 | if (!ParseNumber(allow_negative)) return false; |
| 1455 | if (suffix) Write(suffix); |
| 1456 | return Parse('E'); |
| 1457 | } |
| 1458 | |
| 1459 | bool ParseBooleanLiteral() |
| 1460 | { |
| 1461 | switch (*read_ptr++) |
| 1462 | { |
| 1463 | case '0': WRITE("false"); break; |
| 1464 | case '1': WRITE("true"); break; |
| 1465 | default: |
| 1466 | #ifdef DEBUG_FAILURES |
| 1467 | printf("*** Boolean literal not 0 or 1\n"); |
| 1468 | #endif |
| 1469 | return false; |
| 1470 | } |
| 1471 | return Parse('E'); |
| 1472 | } |
| 1473 | |
| 1474 | // <expr-primary> ::= L <type> <value number> E # integer literal |
| 1475 | // ::= L <type> <value float> E # floating literal |
| 1476 | // ::= L <string type> E # string literal |
| 1477 | // ::= L <nullptr type> E # nullptr literal (i.e., "LDnE") |
| 1478 | // ::= L <type> <real-part float> _ <imag-part float> E # complex floating point literal (C 2000) |
| 1479 | // ::= L <mangled-name> E # external name |
| 1480 | |
| 1481 | bool ParseExpressionPrimary() |
| 1482 | { |
| 1483 | switch (*read_ptr++) |
| 1484 | { |
| 1485 | case 'b': return ParseBooleanLiteral(); |
| 1486 | case 'x': return ParseIntegerLiteral(nullptr, "ll", true); |
| 1487 | case 'l': return ParseIntegerLiteral(nullptr, "l", true); |
| 1488 | case 'i': return ParseIntegerLiteral(nullptr, nullptr, true); |
| 1489 | case 'n': return ParseIntegerLiteral("(__int128)", nullptr, true); |
| 1490 | case 'j': return ParseIntegerLiteral(nullptr, "u", false); |
| 1491 | case 'm': return ParseIntegerLiteral(nullptr, "ul", false); |
| 1492 | case 'y': return ParseIntegerLiteral(nullptr, "ull", false); |
| 1493 | case 'o': return ParseIntegerLiteral("(unsigned __int128)", |
| 1494 | nullptr, false); |
| 1495 | case '_': |
| 1496 | if (*read_ptr++ == 'Z') |
| 1497 | { |
| 1498 | if (!ParseEncoding()) return false; |
| 1499 | return Parse('E'); |
| 1500 | } |
| 1501 | --read_ptr; |
| 1502 | // fallthrough |
| 1503 | case 'w': |
| 1504 | case 'c': |
| 1505 | case 'a': |
| 1506 | case 'h': |
| 1507 | case 's': |
| 1508 | case 't': |
| 1509 | case 'f': |
| 1510 | case 'd': |
| 1511 | case 'e': |
| 1512 | #ifdef DEBUG_FAILURES |
| 1513 | printf("*** Unsupported primary expression %.5s\n", read_ptr - 1); |
| 1514 | #endif |
| 1515 | return false; |
| 1516 | case 'T': |
| 1517 | // Invalid mangled name per |
| 1518 | // http://sourcerytools.com/pipermail/cxx-abi-dev/2011-August/002422.html |
| 1519 | #ifdef DEBUG_FAILURES |
| 1520 | printf("*** Invalid primary expr encoding\n"); |
| 1521 | #endif |
| 1522 | return false; |
| 1523 | default: |
| 1524 | --read_ptr; |
| 1525 | Write('('); |
| 1526 | if (!ParseType()) return false; |
| 1527 | Write(')'); |
| 1528 | if (!ParseNumber()) return false; |
| 1529 | return Parse('E'); |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | // <unresolved-type> ::= <template-param> |
| 1534 | // ::= <decltype> |
| 1535 | // ::= <substitution> |
| 1536 | |
| 1537 | bool ParseUnresolvedType() |
| 1538 | { |
| 1539 | int type_start_cookie = GetStartCookie(); |
| 1540 | switch (*read_ptr++) |
| 1541 | { |
| 1542 | case 'T': |
| 1543 | if (!ParseTemplateParam()) return false; |
| 1544 | EndSubstitution(type_start_cookie); |
| 1545 | return true; |
| 1546 | case 'S': |
| 1547 | { |
| 1548 | if (*read_ptr != 't') return ParseSubstitution(); |
| 1549 | |
| 1550 | ++read_ptr; |
| 1551 | WriteStdPrefix(); |
| 1552 | NameState type_name = {}; |
| 1553 | if (!ParseUnqualifiedName(type_name)) return false; |
| 1554 | EndSubstitution(type_start_cookie); |
| 1555 | return true; |
| 1556 | |
| 1557 | } |
| 1558 | case 'D': |
| 1559 | default: |
| 1560 | #ifdef DEBUG_FAILURES |
| 1561 | printf("*** Unsupported unqualified type: %3s\n", read_ptr - 1); |
| 1562 | #endif |
| 1563 | return false; |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | // <base-unresolved-name> ::= <simple-id> # unresolved name |
| 1568 | // extension ::= <operator-name> # unresolved operator-function-id |
| 1569 | // extension ::= <operator-name> <template-args> # unresolved operator template-id |
| 1570 | // ::= on <operator-name> # unresolved operator-function-id |
| 1571 | // ::= on <operator-name> <template-args> # unresolved operator template-id |
| 1572 | // ::= dn <destructor-name> # destructor or pseudo-destructor; |
| 1573 | // # e.g. ~X or ~X<N-1> |
| 1574 | |
| 1575 | bool ParseBaseUnresolvedName() |
| 1576 | { |
| 1577 | #ifdef DEBUG_FAILURES |
| 1578 | printf("*** Base unresolved name unsupported\n"); |
| 1579 | #endif |
| 1580 | return false; |
| 1581 | } |
| 1582 | |
| 1583 | // <unresolved-name> |
| 1584 | // extension ::= srN <unresolved-type> [<template-args>] <unresolved-qualifier-level>* E <base-unresolved-name> |
| 1585 | // ::= [gs] <base-unresolved-name> # x or (with "gs") ::x |
| 1586 | // ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name> |
| 1587 | // # A::x, N::y, A<T>::z; "gs" means leading "::" |
| 1588 | // ::= sr <unresolved-type> <base-unresolved-name> # T::x / decltype(p)::x |
| 1589 | // extension ::= sr <unresolved-type> <template-args> <base-unresolved-name> |
| 1590 | // # T::N::x /decltype(p)::N::x |
| 1591 | // (ignored) ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name> |
| 1592 | |
| 1593 | bool ParseUnresolvedName() |
| 1594 | { |
| 1595 | #ifdef DEBUG_FAILURES |
| 1596 | printf("*** Unresolved names not supported\n"); |
| 1597 | #endif |
| 1598 | //TODO: grammar for all of this seems unclear... |
| 1599 | return false; |
| 1600 | |
| 1601 | if (*read_ptr == 'g' && *(read_ptr + 1) == 's') |
| 1602 | { |
| 1603 | read_ptr += 2; |
| 1604 | WriteNamespaceSeparator(); |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | // <expression> ::= <unary operator-name> <expression> |
| 1609 | // ::= <binary operator-name> <expression> <expression> |
| 1610 | // ::= <ternary operator-name> <expression> <expression> <expression> |
| 1611 | // ::= cl <expression>+ E # call |
| 1612 | // ::= cv <type> <expression> # conversion with one argument |
| 1613 | // ::= cv <type> _ <expression>* E # conversion with a different number of arguments |
| 1614 | // ::= [gs] nw <expression>* _ <type> E # new (expr-list) type |
| 1615 | // ::= [gs] nw <expression>* _ <type> <initializer> # new (expr-list) type (init) |
| 1616 | // ::= [gs] na <expression>* _ <type> E # new[] (expr-list) type |
| 1617 | // ::= [gs] na <expression>* _ <type> <initializer> # new[] (expr-list) type (init) |
| 1618 | // ::= [gs] dl <expression> # delete expression |
| 1619 | // ::= [gs] da <expression> # delete[] expression |
| 1620 | // ::= pp_ <expression> # prefix ++ |
| 1621 | // ::= mm_ <expression> # prefix -- |
| 1622 | // ::= ti <type> # typeid (type) |
| 1623 | // ::= te <expression> # typeid (expression) |
| 1624 | // ::= dc <type> <expression> # dynamic_cast<type> (expression) |
| 1625 | // ::= sc <type> <expression> # static_cast<type> (expression) |
| 1626 | // ::= cc <type> <expression> # const_cast<type> (expression) |
| 1627 | // ::= rc <type> <expression> # reinterpret_cast<type> (expression) |
| 1628 | // ::= st <type> # sizeof (a type) |
| 1629 | // ::= sz <expression> # sizeof (an expression) |
| 1630 | // ::= at <type> # alignof (a type) |
| 1631 | // ::= az <expression> # alignof (an expression) |
| 1632 | // ::= nx <expression> # noexcept (expression) |
| 1633 | // ::= <template-param> |
| 1634 | // ::= <function-param> |
| 1635 | // ::= dt <expression> <unresolved-name> # expr.name |
| 1636 | // ::= pt <expression> <unresolved-name> # expr->name |
| 1637 | // ::= ds <expression> <expression> # expr.*expr |
| 1638 | // ::= sZ <template-param> # size of a parameter pack |
| 1639 | // ::= sZ <function-param> # size of a function parameter pack |
| 1640 | // ::= sp <expression> # pack expansion |
| 1641 | // ::= tw <expression> # throw expression |
| 1642 | // ::= tr # throw with no operand (rethrow) |
| 1643 | // ::= <unresolved-name> # f(p), N::f(p), ::f(p), |
| 1644 | // # freestanding dependent name (e.g., T::x), |
| 1645 | // # objectless nonstatic member reference |
| 1646 | // ::= <expr-primary> |
| 1647 | |
| 1648 | bool ParseExpression() |
| 1649 | { |
| 1650 | Operator expression_operator = TryParseOperator(); |
| 1651 | switch (expression_operator.kind) |
| 1652 | { |
| 1653 | case OperatorKind::Unary: |
| 1654 | Write(expression_operator.name); |
| 1655 | Write('('); |
| 1656 | if (!ParseExpression()) return false; |
| 1657 | Write(')'); |
| 1658 | return true; |
| 1659 | case OperatorKind::Binary: |
| 1660 | if (!ParseExpression()) return false; |
| 1661 | Write(expression_operator.name); |
| 1662 | return ParseExpression(); |
| 1663 | case OperatorKind::Ternary: |
| 1664 | if (!ParseExpression()) return false; |
| 1665 | Write('?'); |
| 1666 | if (!ParseExpression()) return false; |
| 1667 | Write(':'); |
| 1668 | return ParseExpression(); |
| 1669 | case OperatorKind::NoMatch: |
| 1670 | break; |
| 1671 | case OperatorKind::Other: |
| 1672 | default: |
| 1673 | #ifdef DEBUG_FAILURES |
| 1674 | printf("*** Unsupported operator: %s\n", expression_operator.name); |
| 1675 | #endif |
| 1676 | return false; |
| 1677 | } |
| 1678 | |
| 1679 | switch (*read_ptr++) |
| 1680 | { |
| 1681 | case 'T': return ParseTemplateParam(); |
| 1682 | case 'L': return ParseExpressionPrimary(); |
| 1683 | case 's': |
| 1684 | if (*read_ptr++ == 'r') return ParseUnresolvedName(); |
| 1685 | --read_ptr; |
| 1686 | // fallthrough |
| 1687 | default: |
| 1688 | return ParseExpressionPrimary(); |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | // <template-arg> ::= <type> # type or template |
| 1693 | // ::= X <expression> E # expression |
| 1694 | // ::= <expr-primary> # simple expressions |
| 1695 | // ::= J <template-arg>* E # argument pack |
| 1696 | // ::= LZ <encoding> E # extension |
| 1697 | |
| 1698 | bool ParseTemplateArg() |
| 1699 | { |
| 1700 | switch (*read_ptr) { |
| 1701 | case 'J': |
| 1702 | #ifdef DEBUG_FAILURES |
| 1703 | printf("*** Template argument packs unsupported\n"); |
| 1704 | #endif |
| 1705 | return false; |
| 1706 | case 'X': |
| 1707 | ++read_ptr; |
| 1708 | if (!ParseExpression()) return false; |
| 1709 | return Parse('E'); |
| 1710 | case 'L': |
| 1711 | ++read_ptr; |
| 1712 | return ParseExpressionPrimary(); |
| 1713 | default: |
| 1714 | return ParseType(); |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | // <template-args> ::= I <template-arg>* E |
| 1719 | // extension, the abi says <template-arg>+ |
| 1720 | |
| 1721 | bool ParseTemplateArgs(bool record_template_args = false) |
| 1722 | { |
| 1723 | if (record_template_args) ResetTemplateArgs(); |
| 1724 | |
| 1725 | bool first_arg = true; |
| 1726 | while (*read_ptr != 'E') |
| 1727 | { |
| 1728 | if (first_arg) first_arg = false; |
| 1729 | else WriteCommaSpace(); |
| 1730 | |
| 1731 | int template_start_cookie = GetStartCookie(); |
| 1732 | if (!ParseTemplateArg()) return false; |
| 1733 | if (record_template_args) EndTemplateArg(template_start_cookie); |
| 1734 | } |
| 1735 | ++read_ptr; |
| 1736 | return true; |
| 1737 | } |
| 1738 | |
| 1739 | // <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> <unqualified-name> E |
| 1740 | // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> <template-args> E |
| 1741 | // |
| 1742 | // <prefix> ::= <prefix> <unqualified-name> |
| 1743 | // ::= <template-prefix> <template-args> |
| 1744 | // ::= <template-param> |
| 1745 | // ::= <decltype> |
| 1746 | // ::= # empty |
| 1747 | // ::= <substitution> |
| 1748 | // ::= <prefix> <data-member-prefix> |
| 1749 | // extension ::= L |
| 1750 | // |
| 1751 | // <template-prefix> ::= <prefix> <template unqualified-name> |
| 1752 | // ::= <template-param> |
| 1753 | // ::= <substitution> |
| 1754 | // |
| 1755 | // <unqualified-name> ::= <operator-name> |
| 1756 | // ::= <ctor-dtor-name> |
| 1757 | // ::= <source-name> |
| 1758 | // ::= <unnamed-type-name> |
| 1759 | |
| 1760 | bool ParseNestedName(NameState & name_state, bool parse_discriminator = false) |
| 1761 | { |
| 1762 | int qualifiers = TryParseQualifiers(true, true); |
| 1763 | bool first_part = true; |
| 1764 | bool suppress_substitution = true; |
| 1765 | int name_start_cookie = GetStartCookie(); |
| 1766 | while (true) |
| 1767 | { |
| 1768 | char next = *read_ptr; |
| 1769 | if (next == 'E') |
| 1770 | { |
| 1771 | ++read_ptr; |
| 1772 | break; |
| 1773 | } |
| 1774 | |
| 1775 | // Record a substitution candidate for all prefixes, but not the full name |
| 1776 | if (suppress_substitution) suppress_substitution = false; |
| 1777 | else EndSubstitution(name_start_cookie); |
| 1778 | |
| 1779 | if (next == 'I') |
| 1780 | { |
| 1781 | ++read_ptr; |
| 1782 | name_state.is_last_generic = true; |
| 1783 | WriteTemplateStart(); |
| 1784 | if (!ParseTemplateArgs(name_state.parse_function_params)) return false; |
| 1785 | WriteTemplateEnd(); |
| 1786 | continue; |
| 1787 | } |
| 1788 | |
| 1789 | if (first_part) first_part = false; |
| 1790 | else WriteNamespaceSeparator(); |
| 1791 | |
| 1792 | name_state.is_last_generic = false; |
| 1793 | switch (next) |
| 1794 | { |
| 1795 | case '0': |
| 1796 | case '1': |
| 1797 | case '2': |
| 1798 | case '3': |
| 1799 | case '4': |
| 1800 | case '5': |
| 1801 | case '6': |
| 1802 | case '7': |
| 1803 | case '8': |
| 1804 | case '9': |
| 1805 | { |
| 1806 | int name_start_cookie = GetStartCookie(); |
| 1807 | if (!ParseSourceName()) return false; |
| 1808 | name_state.last_name_range = EndRange(name_start_cookie); |
| 1809 | continue; |
| 1810 | } |
| 1811 | case 'S': |
| 1812 | if (*++read_ptr == 't') |
| 1813 | { |
| 1814 | WriteStdPrefix(); |
| 1815 | ++read_ptr; |
| 1816 | if (!ParseUnqualifiedName(name_state)) return false; |
| 1817 | } |
| 1818 | else |
| 1819 | { |
| 1820 | if (!ParseSubstitution()) return false; |
| 1821 | suppress_substitution = true; |
| 1822 | } |
| 1823 | continue; |
| 1824 | case 'T': |
| 1825 | ++read_ptr; |
| 1826 | if (!ParseTemplateParam()) return false; |
| 1827 | continue; |
| 1828 | case 'C': |
| 1829 | ++read_ptr; |
| 1830 | if (!ParseCtor(name_state)) return false; |
| 1831 | continue; |
| 1832 | case 'D': |
| 1833 | { |
| 1834 | switch (*(read_ptr + 1)) |
| 1835 | { |
| 1836 | case 't': |
| 1837 | case 'T': |
| 1838 | #ifdef DEBUG_FAILURES |
| 1839 | printf("*** Decltype unsupported\n"); |
| 1840 | #endif |
| 1841 | return false; |
| 1842 | } |
| 1843 | ++read_ptr; |
| 1844 | if (!ParseDtor(name_state)) return false; |
| 1845 | continue; |
| 1846 | } |
| 1847 | case 'U': |
| 1848 | ++read_ptr; |
| 1849 | if (!ParseUnnamedTypeName(name_state)) return false; |
| 1850 | continue; |
| 1851 | case 'L': |
| 1852 | ++read_ptr; |
| 1853 | if (!ParseUnqualifiedName(name_state)) return false; |
| 1854 | continue; |
| 1855 | default: |
| 1856 | if (!ParseOperatorName(name_state)) return false; |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | if (parse_discriminator) TryParseDiscriminator(); |
| 1861 | if (name_state.parse_function_params && |
| 1862 | !ParseFunctionArgs(name_state, name_start_cookie)) return false; |
| 1863 | if (qualifiers) WriteQualifiers(qualifiers); |
| 1864 | return true; |
| 1865 | } |
| 1866 | |
| 1867 | // <local-name> := Z <function encoding> E <entity name> [<discriminator>] |
| 1868 | // := Z <function encoding> E s [<discriminator>] |
| 1869 | // := Z <function encoding> Ed [ <parameter number> ] _ <entity name> |
| 1870 | |
| 1871 | bool ParseLocalName(bool parse_function_params) |
| 1872 | { |
| 1873 | if (!ParseEncoding()) return false; |
| 1874 | if (!Parse('E')) return false; |
| 1875 | |
| 1876 | switch (*read_ptr) |
| 1877 | { |
| 1878 | case 's': |
| 1879 | TryParseDiscriminator(); // Optional and ignored |
| 1880 | WRITE("::string literal"); |
| 1881 | break; |
| 1882 | case 'd': |
| 1883 | TryParseNumber(); // Optional and ignored |
| 1884 | WriteNamespaceSeparator(); |
| 1885 | if (!ParseName()) return false; |
| 1886 | break; |
| 1887 | default: |
| 1888 | WriteNamespaceSeparator(); |
| 1889 | if (!ParseName(parse_function_params, true)) return false; |
| 1890 | TryParseDiscriminator(); // Optional and ignored |
| 1891 | } |
| 1892 | return true; |
| 1893 | } |
| 1894 | |
| 1895 | // <name> ::= <nested-name> |
| 1896 | // ::= <local-name> |
| 1897 | // ::= <unscoped-template-name> <template-args> |
| 1898 | // ::= <unscoped-name> |
| 1899 | |
| 1900 | // <unscoped-template-name> ::= <unscoped-name> |
| 1901 | // ::= <substitution> |
| 1902 | |
| 1903 | bool ParseName(bool parse_function_params = false, |
| 1904 | bool parse_discriminator = false) |
| 1905 | { |
| 1906 | NameState name_state = { parse_function_params }; |
| 1907 | int name_start_cookie = GetStartCookie(); |
| 1908 | |
| 1909 | switch (*read_ptr) |
| 1910 | { |
| 1911 | case 'N': |
| 1912 | ++read_ptr; |
| 1913 | return ParseNestedName(name_state, parse_discriminator); |
| 1914 | case 'Z': |
| 1915 | { |
| 1916 | ++read_ptr; |
| 1917 | if (!ParseLocalName(parse_function_params)) return false; |
| 1918 | break; |
| 1919 | } |
| 1920 | case 'L': |
| 1921 | ++read_ptr; |
| 1922 | // fallthrough |
| 1923 | default: |
| 1924 | { |
| 1925 | if (!ParseUnscopedName(name_state)) return false; |
| 1926 | |
| 1927 | if (*read_ptr == 'I') |
| 1928 | { |
| 1929 | EndSubstitution(name_start_cookie); |
| 1930 | |
| 1931 | ++read_ptr; |
| 1932 | name_state.is_last_generic = true; |
| 1933 | WriteTemplateStart(); |
| 1934 | if (!ParseTemplateArgs(parse_function_params)) return false; |
| 1935 | WriteTemplateEnd(); |
| 1936 | } |
| 1937 | break; |
| 1938 | } |
| 1939 | } |
| 1940 | if (parse_discriminator) TryParseDiscriminator(); |
| 1941 | if (parse_function_params && |
| 1942 | !ParseFunctionArgs(name_state, name_start_cookie)) return false; |
| 1943 | return true; |
| 1944 | } |
| 1945 | |
| 1946 | // <call-offset> ::= h <nv-offset> _ |
| 1947 | // ::= v <v-offset> _ |
| 1948 | // |
| 1949 | // <nv-offset> ::= <offset number> |
| 1950 | // # non-virtual base override |
| 1951 | // |
| 1952 | // <v-offset> ::= <offset number> _ <virtual offset number> |
| 1953 | // # virtual base override, with vcall offset |
| 1954 | |
| 1955 | bool ParseCallOffset() |
| 1956 | { |
| 1957 | switch (*read_ptr++) |
| 1958 | { |
| 1959 | case 'h': |
| 1960 | if (*read_ptr == 'n') ++read_ptr; |
| 1961 | if (TryParseNumber() == -1 || *read_ptr++ != '_') break; |
| 1962 | return true; |
| 1963 | case 'v': |
| 1964 | if (*read_ptr == 'n') ++read_ptr; |
| 1965 | if (TryParseNumber() == -1 || *read_ptr++ != '_') break; |
| 1966 | if (*read_ptr == 'n') ++read_ptr; |
| 1967 | if (TryParseNumber() == -1 || *read_ptr++ != '_') break; |
| 1968 | return true; |
| 1969 | } |
| 1970 | #ifdef DEBUG_FAILURES |
| 1971 | printf("*** Malformed call offset\n"); |
| 1972 | #endif |
| 1973 | return false; |
| 1974 | } |
| 1975 | |
| 1976 | // <special-name> ::= TV <type> # virtual table |
| 1977 | // ::= TT <type> # VTT structure (construction vtable index) |
| 1978 | // ::= TI <type> # typeinfo structure |
| 1979 | // ::= TS <type> # typeinfo name (null-terminated byte string) |
| 1980 | // ::= Tc <call-offset> <call-offset> <base encoding> |
| 1981 | // # base is the nominal target function of thunk |
| 1982 | // # first call-offset is 'this' adjustment |
| 1983 | // # second call-offset is result adjustment |
| 1984 | // ::= T <call-offset> <base encoding> |
| 1985 | // # base is the nominal target function of thunk |
| 1986 | // extension ::= TC <first type> <number> _ <second type> # construction vtable for second-in-first |
| 1987 | |
| 1988 | bool ParseSpecialNameT() |
| 1989 | { |
| 1990 | switch (*read_ptr++) |
| 1991 | { |
| 1992 | case 'V': |
| 1993 | WRITE("vtable for "); |
| 1994 | return ParseType(); |
| 1995 | case 'T': |
| 1996 | WRITE("VTT for "); |
| 1997 | return ParseType(); |
| 1998 | case 'I': |
| 1999 | WRITE("typeinfo for "); |
| 2000 | return ParseType(); |
| 2001 | case 'S': |
| 2002 | WRITE("typeinfo name for "); |
| 2003 | return ParseType(); |
| 2004 | case 'c': |
| 2005 | case 'C': |
| 2006 | #ifdef DEBUG_FAILURES |
| 2007 | printf("*** Unsupported thunk or construction vtable name: %.3s\n", read_ptr - 1); |
| 2008 | #endif |
| 2009 | return false; |
| 2010 | default: |
| 2011 | if (*--read_ptr == 'v') |
| 2012 | { |
| 2013 | WRITE("virtual thunk to "); |
| 2014 | } |
| 2015 | else |
| 2016 | { |
| 2017 | WRITE("non-virtual thunk to "); |
| 2018 | } |
| 2019 | if (!ParseCallOffset()) return false; |
| 2020 | return ParseEncoding(); |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | // <special-name> ::= GV <object name> # Guard variable for one-time initialization |
| 2025 | // # No <type> |
| 2026 | // extension ::= GR <object name> # reference temporary for object |
| 2027 | |
| 2028 | bool ParseSpecialNameG() |
| 2029 | { |
| 2030 | switch (*read_ptr++) |
| 2031 | { |
| 2032 | case 'V': |
| 2033 | WRITE("guard variable for "); |
| 2034 | if (!ParseName(true)) return false; |
| 2035 | break; |
| 2036 | case 'R': |
| 2037 | WRITE("reference temporary for "); |
| 2038 | if (!ParseName(true)) return false; |
| 2039 | break; |
| 2040 | default: |
| 2041 | #ifdef DEBUG_FAILURES |
| 2042 | printf("*** Unknown G encoding\n"); |
| 2043 | #endif |
| 2044 | return false; |
| 2045 | } |
| 2046 | return true; |
| 2047 | } |
| 2048 | |
| 2049 | // <bare-function-type> ::= <signature type>+ # types are possible return type, then parameter types |
| 2050 | |
| 2051 | bool ParseFunctionArgs(NameState & name_state, int return_insert_cookie) |
| 2052 | { |
| 2053 | char next = *read_ptr; |
| 2054 | if (next == 'E' || next == '\0' || next == '.') return true; |
| 2055 | |
| 2056 | // Clang has a bad habit of making unique manglings by just sticking numbers on the end of a symbol, |
| 2057 | // which is ambiguous with malformed source name manglings |
| 2058 | const char * before_clang_uniquing_test = read_ptr; |
| 2059 | if (TryParseNumber()) |
| 2060 | { |
| 2061 | if (*read_ptr == '\0') return true; |
| 2062 | read_ptr = before_clang_uniquing_test; |
| 2063 | } |
| 2064 | |
| 2065 | if (name_state.is_last_generic && !name_state.has_no_return_type) |
| 2066 | { |
| 2067 | int return_type_start_cookie = GetStartCookie(); |
| 2068 | if (!ParseType()) return false; |
| 2069 | Write(' '); |
| 2070 | ReorderRange(EndRange(return_type_start_cookie), |
| 2071 | return_insert_cookie); |
| 2072 | } |
| 2073 | |
| 2074 | Write('('); |
| 2075 | bool first_param = true; |
| 2076 | while (true) |
| 2077 | { |
| 2078 | switch (*read_ptr) |
| 2079 | { |
| 2080 | case '\0': |
| 2081 | case 'E': |
| 2082 | case '.': |
| 2083 | break; |
| 2084 | case 'v': |
| 2085 | ++read_ptr; |
| 2086 | continue; |
| 2087 | case '_': |
| 2088 | // Not a formal part of the mangling specification, but clang emits suffixes starting with _block_invoke |
| 2089 | if (strncmp(read_ptr, "_block_invoke", 13) == 0) |
| 2090 | { |
| 2091 | read_ptr += strlen(read_ptr); |
| 2092 | break; |
| 2093 | } |
| 2094 | // fallthrough |
| 2095 | default: |
| 2096 | if (first_param) first_param = false; |
| 2097 | else WriteCommaSpace(); |
| 2098 | |
| 2099 | if (!ParseType()) return false; |
| 2100 | continue; |
| 2101 | } |
| 2102 | break; |
| 2103 | } |
| 2104 | Write(')'); |
| 2105 | return true; |
| 2106 | } |
| 2107 | |
| 2108 | // <encoding> ::= <function name> <bare-function-type> |
| 2109 | // ::= <data name> |
| 2110 | // ::= <special-name> |
| 2111 | |
| 2112 | bool ParseEncoding() |
| 2113 | { |
| 2114 | switch (*read_ptr) |
| 2115 | { |
| 2116 | case 'T': |
| 2117 | ++read_ptr; |
| 2118 | if (!ParseSpecialNameT()) return false; |
| 2119 | break; |
| 2120 | case 'G': |
| 2121 | ++read_ptr; |
| 2122 | if (!ParseSpecialNameG()) return false; |
| 2123 | break; |
| 2124 | default: |
| 2125 | if (!ParseName(true)) return false; |
| 2126 | break; |
| 2127 | } |
| 2128 | return true; |
| 2129 | } |
| 2130 | |
| 2131 | bool ParseMangling(const char * mangled_name, long mangled_name_length = 0) |
| 2132 | { |
| 2133 | if (!mangled_name_length) mangled_name_length = strlen(mangled_name); |
| 2134 | read_end = mangled_name + mangled_name_length; |
| 2135 | read_ptr = mangled_name; |
| 2136 | write_ptr = buffer; |
| 2137 | next_substitute_index = 0; |
| 2138 | next_template_arg_index = rewrite_ranges_size - 1; |
| 2139 | |
| 2140 | if (*read_ptr++ != '_' || *read_ptr++ != 'Z') |
| 2141 | { |
| 2142 | #ifdef DEBUG_FAILURES |
| 2143 | printf("*** Missing _Z prefix\n"); |
| 2144 | #endif |
| 2145 | return false; |
| 2146 | } |
| 2147 | if (!ParseEncoding()) return false; |
| 2148 | switch (*read_ptr) |
| 2149 | { |
| 2150 | case '.': |
| 2151 | Write(' '); |
| 2152 | Write('('); |
| 2153 | Write(read_ptr, read_end - read_ptr); |
| 2154 | Write(')'); |
| 2155 | case '\0': |
| 2156 | return true; |
| 2157 | default: |
| 2158 | #ifdef DEBUG_FAILURES |
| 2159 | printf("*** Unparsed mangled content\n"); |
| 2160 | #endif |
| 2161 | return false; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | private: |
| 2166 | |
| 2167 | // External scratch storage used during demanglings |
| 2168 | |
| 2169 | char * buffer; |
| 2170 | const char * buffer_end; |
| 2171 | BufferRange * rewrite_ranges; |
| 2172 | int rewrite_ranges_size; |
| 2173 | bool owns_buffer; |
| 2174 | bool owns_rewrite_ranges; |
| 2175 | |
| 2176 | // Internal state used during demangling |
| 2177 | |
| 2178 | const char * read_ptr; |
| 2179 | const char * read_end; |
| 2180 | char * write_ptr; |
| 2181 | int next_template_arg_index; |
| 2182 | int next_substitute_index; |
| 2183 | }; |
| 2184 | |
| 2185 | } // Anonymous namespace |
| 2186 | |
| 2187 | // Public entry points referenced from Mangled.cpp |
| 2188 | namespace lldb_private |
| 2189 | { |
| 2190 | char * FastDemangle(const char* mangled_name) |
| 2191 | { |
| 2192 | char buffer[16384]; |
| 2193 | SymbolDemangler demangler(buffer, sizeof(buffer)); |
| 2194 | return demangler.GetDemangledCopy(mangled_name); |
| 2195 | } |
| 2196 | |
| 2197 | char * FastDemangle(const char* mangled_name, long mangled_name_length) |
| 2198 | { |
| 2199 | char buffer[16384]; |
| 2200 | SymbolDemangler demangler(buffer, sizeof(buffer)); |
| 2201 | return demangler.GetDemangledCopy(mangled_name, mangled_name_length); |
| 2202 | } |
| 2203 | } // lldb_private namespace |