| 1 |
/** |
| 2 |
* The demangle module converts mangled D symbols to a representation similar |
| 3 |
* to what would have existed in code. |
| 4 |
* |
| 5 |
* Copyright: Copyright Sean Kelly 2010 - 2014. |
| 6 |
* License: Distributed under the |
| 7 |
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). |
| 8 |
* (See accompanying file LICENSE) |
| 9 |
* Authors: Sean Kelly |
| 10 |
* Source: $(DRUNTIMESRC core/_demangle.d) |
| 11 |
*/ |
| 12 |
|
| 13 |
module core.demangle; |
| 14 |
|
| 15 |
version (OSX) |
| 16 |
version = Darwin; |
| 17 |
else version (iOS) |
| 18 |
version = Darwin; |
| 19 |
else version (TVOS) |
| 20 |
version = Darwin; |
| 21 |
else version (WatchOS) |
| 22 |
version = Darwin; |
| 23 |
|
| 24 |
debug (trace) debug = needPrintf; |
| 25 |
debug (info) debug = needPrintf; |
| 26 |
|
| 27 |
debug (needPrintf) |
| 28 |
private int printf(Args...)(scope const char* fmt, scope const Args args) |
| 29 |
=> __ctfe ? 0 : imported!"core.stdc.stdio".printf(fmt, args); |
| 30 |
|
| 31 |
extern (C) alias CXX_DEMANGLER = char* function (const char* mangled_name, |
| 32 |
char* output_buffer, |
| 33 |
size_t* length, |
| 34 |
int* status) nothrow pure @trusted; |
| 35 |
|
| 36 |
private struct NoHooks |
| 37 |
{ |
| 38 |
// supported hooks |
| 39 |
// static bool parseLName(ref Demangle); |
| 40 |
// static char[] parseType(ref Demangle, char[]) |
| 41 |
} |
| 42 |
|
| 43 |
private struct Demangle(Hooks = NoHooks) |
| 44 |
{ |
| 45 |
// NOTE: This implementation currently only works with mangled function |
| 46 |
// names as they exist in an object file. Type names mangled via |
| 47 |
// the .mangleof property are effectively incomplete as far as the |
| 48 |
// ABI is concerned and so are not considered to be mangled symbol |
| 49 |
// names. |
| 50 |
|
| 51 |
// NOTE: This implementation builds the demangled buffer in place by |
| 52 |
// writing data as it is decoded and then rearranging it later as |
| 53 |
// needed. In practice this results in very little data movement, |
| 54 |
// and the performance cost is more than offset by the gain from |
| 55 |
// not allocating dynamic memory to assemble the name piecemeal. |
| 56 |
// |
| 57 |
// If the destination buffer is too small, parsing will restart |
| 58 |
// with a larger buffer. Since this generally means only one |
| 59 |
// allocation during the course of a parsing run, this is still |
| 60 |
// faster than assembling the result piecemeal. |
| 61 |
|
| 62 |
pure @safe: |
| 63 |
enum AddType { no, yes } |
| 64 |
|
| 65 |
|
| 66 |
this( return scope const(char)[] buf_, return scope char[] dst_ = null ) |
| 67 |
{ |
| 68 |
this( buf_, AddType.yes, dst_ ); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
this( return scope const(char)[] buf_, AddType addType_, return scope char[] dst_ = null ) |
| 73 |
{ |
| 74 |
buf = buf_; |
| 75 |
addType = addType_; |
| 76 |
dst.dst = dst_; |
| 77 |
} |
| 78 |
|
| 79 |
const(char)[] buf = null; |
| 80 |
Buffer dst; |
| 81 |
size_t pos = 0; |
| 82 |
size_t brp = 0; // current back reference pos |
| 83 |
AddType addType = AddType.yes; |
| 84 |
bool mute = false; |
| 85 |
Hooks hooks; |
| 86 |
|
| 87 |
////////////////////////////////////////////////////////////////////////// |
| 88 |
// Type Testing and Conversion |
| 89 |
////////////////////////////////////////////////////////////////////////// |
| 90 |
|
| 91 |
|
| 92 |
static bool isAlpha( char val ) |
| 93 |
{ |
| 94 |
return ('a' <= val && 'z' >= val) || |
| 95 |
('A' <= val && 'Z' >= val) || |
| 96 |
(0x80 & val); // treat all unicode as alphabetic |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
static bool isDigit( char val ) nothrow |
| 101 |
{ |
| 102 |
return '0' <= val && '9' >= val; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
static bool isHexDigit( char val ) |
| 107 |
{ |
| 108 |
return ('0' <= val && '9' >= val) || |
| 109 |
('a' <= val && 'f' >= val) || |
| 110 |
('A' <= val && 'F' >= val); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
static ubyte ascii2hex( out bool errStatus, char val ) nothrow |
| 115 |
{ |
| 116 |
if (val >= 'a' && val <= 'f') |
| 117 |
return cast(ubyte)(val - 'a' + 10); |
| 118 |
if (val >= 'A' && val <= 'F') |
| 119 |
return cast(ubyte)(val - 'A' + 10); |
| 120 |
if (val >= '0' && val <= '9') |
| 121 |
return cast(ubyte)(val - '0'); |
| 122 |
|
| 123 |
errStatus = true; |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
BufSlice shift(scope const BufSlice val) return scope |
| 128 |
{ |
| 129 |
if (mute) |
| 130 |
return dst.bslice_empty; |
| 131 |
return dst.shift(val); |
| 132 |
} |
| 133 |
|
| 134 |
void putComma(size_t n) |
| 135 |
{ |
| 136 |
version (DigitalMars) pragma(inline, false); |
| 137 |
if (n) |
| 138 |
put(", "); |
| 139 |
} |
| 140 |
|
| 141 |
void put(char c) return scope |
| 142 |
{ |
| 143 |
char[1] val = c; |
| 144 |
put(val[]); |
| 145 |
} |
| 146 |
|
| 147 |
void put(scope BufSlice val) return scope |
| 148 |
{ |
| 149 |
put(val.getSlice); |
| 150 |
} |
| 151 |
|
| 152 |
void put(scope const(char)[] val) return scope nothrow |
| 153 |
{ |
| 154 |
if (mute) |
| 155 |
return; |
| 156 |
dst.append(val); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
void putAsHex( size_t val, int width = 0 ) |
| 161 |
{ |
| 162 |
import core.internal.string; |
| 163 |
|
| 164 |
UnsignedStringBuf buf = void; |
| 165 |
|
| 166 |
auto s = unsignedToTempString!16(val, buf); |
| 167 |
int slen = cast(int)s.length; |
| 168 |
if (slen < width) |
| 169 |
{ |
| 170 |
foreach (i; slen .. width) |
| 171 |
put('0'); |
| 172 |
} |
| 173 |
put(s); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
void pad( const(char)[] val ) |
| 178 |
{ |
| 179 |
if ( val.length ) |
| 180 |
{ |
| 181 |
put(" "); |
| 182 |
put( val ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
void silent( out bool err_status, void delegate(out bool err_status) pure @safe nothrow dg ) nothrow |
| 188 |
{ |
| 189 |
debug(trace) printf( "silent+\n" ); |
| 190 |
debug(trace) scope(success) printf( "silent-\n" ); |
| 191 |
auto n = dst.length; |
| 192 |
dg(err_status); |
| 193 |
if(!err_status) |
| 194 |
dst.len = n; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
////////////////////////////////////////////////////////////////////////// |
| 199 |
// Parsing Utility |
| 200 |
////////////////////////////////////////////////////////////////////////// |
| 201 |
|
| 202 |
@property bool empty() |
| 203 |
{ |
| 204 |
return pos >= buf.length; |
| 205 |
} |
| 206 |
|
| 207 |
@property char front() |
| 208 |
{ |
| 209 |
if ( pos < buf.length ) |
| 210 |
return buf[pos]; |
| 211 |
return char.init; |
| 212 |
} |
| 213 |
|
| 214 |
char peek( size_t n ) |
| 215 |
{ |
| 216 |
if ( pos + n < buf.length ) |
| 217 |
return buf[pos + n]; |
| 218 |
return char.init; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
bool test( char val ) nothrow |
| 223 |
{ |
| 224 |
return val == front; |
| 225 |
} |
| 226 |
|
| 227 |
void popFront() nothrow |
| 228 |
{ |
| 229 |
if ( pos++ >= buf.length ) |
| 230 |
assert(false); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
void popFront(int i) nothrow |
| 235 |
{ |
| 236 |
while (i--) |
| 237 |
popFront(); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
bool match( char val ) nothrow |
| 242 |
{ |
| 243 |
if (!test(val)) |
| 244 |
return false; |
| 245 |
else |
| 246 |
{ |
| 247 |
popFront(); |
| 248 |
return true; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
bool match( const(char)[] val ) nothrow |
| 253 |
{ |
| 254 |
foreach (char e; val ) |
| 255 |
if (!match( e )) |
| 256 |
return false; |
| 257 |
|
| 258 |
return true; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
void eat( char val ) |
| 263 |
{ |
| 264 |
if ( val == front ) |
| 265 |
popFront(); |
| 266 |
} |
| 267 |
|
| 268 |
bool isSymbolNameFront(out bool errStatus) nothrow |
| 269 |
{ |
| 270 |
char val = front; |
| 271 |
if ( isDigit( val ) || val == '_' ) |
| 272 |
return true; |
| 273 |
if ( val != 'Q' ) |
| 274 |
return false; |
| 275 |
|
| 276 |
// check the back reference encoding after 'Q' |
| 277 |
val = peekBackref(); |
| 278 |
if (val == 0) |
| 279 |
{ |
| 280 |
// invalid back reference |
| 281 |
errStatus = true; |
| 282 |
return false; |
| 283 |
} |
| 284 |
|
| 285 |
return isDigit( val ); // identifier ref |
| 286 |
} |
| 287 |
|
| 288 |
// return the first character at the back reference |
| 289 |
char peekBackref() nothrow |
| 290 |
{ |
| 291 |
assert( front == 'Q' ); |
| 292 |
auto n = decodeBackref!1(); |
| 293 |
if (!n || n > pos) |
| 294 |
return 0; // invalid back reference |
| 295 |
|
| 296 |
return buf[pos - n]; |
| 297 |
} |
| 298 |
|
| 299 |
size_t decodeBackref(size_t peekAt = 0)() nothrow |
| 300 |
{ |
| 301 |
enum base = 26; |
| 302 |
size_t n = 0; |
| 303 |
for (size_t p; ; p++) |
| 304 |
{ |
| 305 |
char t; |
| 306 |
static if (peekAt > 0) |
| 307 |
{ |
| 308 |
t = peek(peekAt + p); |
| 309 |
} |
| 310 |
else |
| 311 |
{ |
| 312 |
t = front; |
| 313 |
popFront(); |
| 314 |
} |
| 315 |
if (t < 'A' || t > 'Z') |
| 316 |
{ |
| 317 |
if (t < 'a' || t > 'z') |
| 318 |
return 0; // invalid back reference |
| 319 |
|
| 320 |
n = base * n + t - 'a'; |
| 321 |
return n; |
| 322 |
} |
| 323 |
n = base * n + t - 'A'; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
////////////////////////////////////////////////////////////////////////// |
| 328 |
// Parsing Implementation |
| 329 |
////////////////////////////////////////////////////////////////////////// |
| 330 |
|
| 331 |
|
| 332 |
/* |
| 333 |
Number: |
| 334 |
Digit |
| 335 |
Digit Number |
| 336 |
*/ |
| 337 |
const(char)[] sliceNumber() return scope |
| 338 |
{ |
| 339 |
debug(trace) printf( "sliceNumber+\n" ); |
| 340 |
debug(trace) scope(success) printf( "sliceNumber-\n" ); |
| 341 |
|
| 342 |
auto beg = pos; |
| 343 |
|
| 344 |
while ( true ) |
| 345 |
{ |
| 346 |
auto t = front; |
| 347 |
if (t >= '0' && t <= '9') |
| 348 |
popFront(); |
| 349 |
else |
| 350 |
return buf[beg .. pos]; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
size_t decodeNumber(out bool errStatus) scope nothrow |
| 356 |
{ |
| 357 |
debug(trace) printf( "decodeNumber+\n" ); |
| 358 |
debug(trace) scope(success) printf( "decodeNumber-\n" ); |
| 359 |
|
| 360 |
return decodeNumber( errStatus, sliceNumber() ); |
| 361 |
} |
| 362 |
|
| 363 |
size_t decodeNumber( out bool errStatus, scope const(char)[] num ) scope nothrow |
| 364 |
{ |
| 365 |
debug(trace) printf( "decodeNumber+\n" ); |
| 366 |
debug(trace) scope(success) printf( "decodeNumber-\n" ); |
| 367 |
|
| 368 |
size_t val = 0; |
| 369 |
|
| 370 |
foreach ( c; num ) |
| 371 |
{ |
| 372 |
import core.checkedint : mulu, addu; |
| 373 |
|
| 374 |
bool overflow = false; |
| 375 |
val = mulu(val, 10, overflow); |
| 376 |
val = addu(val, c - '0', overflow); |
| 377 |
if (overflow) |
| 378 |
{ |
| 379 |
errStatus = true; |
| 380 |
return 0; |
| 381 |
} |
| 382 |
} |
| 383 |
return val; |
| 384 |
} |
| 385 |
|
| 386 |
void parseReal(out bool errStatus) scope nothrow |
| 387 |
{ |
| 388 |
debug(trace) printf( "parseReal+\n" ); |
| 389 |
debug(trace) scope(success) printf( "parseReal-\n" ); |
| 390 |
|
| 391 |
char[64] tbuf = void; |
| 392 |
size_t tlen = 0; |
| 393 |
real val = void; |
| 394 |
|
| 395 |
void onError() |
| 396 |
{ |
| 397 |
errStatus = true; |
| 398 |
} |
| 399 |
|
| 400 |
if ( 'I' == front ) |
| 401 |
{ |
| 402 |
if (!match("INF")) |
| 403 |
return onError(); |
| 404 |
put( "real.infinity" ); |
| 405 |
return; |
| 406 |
} |
| 407 |
if ( 'N' == front ) |
| 408 |
{ |
| 409 |
popFront(); |
| 410 |
if ( 'I' == front ) |
| 411 |
{ |
| 412 |
if (!match("INF")) |
| 413 |
return onError(); |
| 414 |
put( "-real.infinity" ); |
| 415 |
return; |
| 416 |
} |
| 417 |
if ( 'A' == front ) |
| 418 |
{ |
| 419 |
if (!match("AN")) |
| 420 |
return onError(); |
| 421 |
put( "real.nan" ); |
| 422 |
return; |
| 423 |
} |
| 424 |
tbuf[tlen++] = '-'; |
| 425 |
} |
| 426 |
|
| 427 |
tbuf[tlen++] = '0'; |
| 428 |
tbuf[tlen++] = 'X'; |
| 429 |
errStatus = !isHexDigit( front ); |
| 430 |
if (errStatus) |
| 431 |
return; // Expected hex digit |
| 432 |
|
| 433 |
tbuf[tlen++] = front; |
| 434 |
tbuf[tlen++] = '.'; |
| 435 |
popFront(); |
| 436 |
|
| 437 |
while ( isHexDigit( front ) ) |
| 438 |
{ |
| 439 |
if (tlen >= tbuf.length) |
| 440 |
return onError(); // Too many hex float digits |
| 441 |
tbuf[tlen++] = front; |
| 442 |
popFront(); |
| 443 |
} |
| 444 |
if (!match('P')) |
| 445 |
return onError(); |
| 446 |
tbuf[tlen++] = 'p'; |
| 447 |
if ( 'N' == front ) |
| 448 |
{ |
| 449 |
tbuf[tlen++] = '-'; |
| 450 |
popFront(); |
| 451 |
} |
| 452 |
else |
| 453 |
{ |
| 454 |
tbuf[tlen++] = '+'; |
| 455 |
} |
| 456 |
while ( isDigit( front ) ) |
| 457 |
{ |
| 458 |
tbuf[tlen++] = front; |
| 459 |
popFront(); |
| 460 |
} |
| 461 |
|
| 462 |
tbuf[tlen] = 0; |
| 463 |
debug(info) printf( "got (%s)\n", tbuf.ptr ); |
| 464 |
pureReprintReal( tbuf[] ); |
| 465 |
debug(info) printf( "converted (%.*s)\n", cast(int) tlen, tbuf.ptr ); |
| 466 |
put( tbuf[0 .. tlen] ); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
/* |
| 471 |
LName: |
| 472 |
Number Name |
| 473 |
|
| 474 |
Name: |
| 475 |
Namestart |
| 476 |
Namestart Namechars |
| 477 |
|
| 478 |
Namestart: |
| 479 |
_ |
| 480 |
Alpha |
| 481 |
|
| 482 |
Namechar: |
| 483 |
Namestart |
| 484 |
Digit |
| 485 |
|
| 486 |
Namechars: |
| 487 |
Namechar |
| 488 |
Namechar Namechars |
| 489 |
*/ |
| 490 |
void parseLName(out string errMsg) scope nothrow |
| 491 |
{ |
| 492 |
debug(trace) printf( "parseLName+\n" ); |
| 493 |
debug(trace) scope(success) printf( "parseLName-\n" ); |
| 494 |
|
| 495 |
static if (__traits(hasMember, Hooks, "parseLName")) |
| 496 |
{ |
| 497 |
auto r = hooks.parseLName(errMsg, this); |
| 498 |
if (errMsg !is null) |
| 499 |
return; |
| 500 |
if (r) return; |
| 501 |
} |
| 502 |
|
| 503 |
void error(string msg) |
| 504 |
{ |
| 505 |
errMsg = msg; |
| 506 |
} |
| 507 |
|
| 508 |
if ( front == 'Q' ) |
| 509 |
{ |
| 510 |
// back reference to LName |
| 511 |
auto refPos = pos; |
| 512 |
popFront(); |
| 513 |
size_t n = decodeBackref(); |
| 514 |
if (!n || n > refPos) |
| 515 |
return error("Invalid LName back reference"); |
| 516 |
|
| 517 |
if ( !mute ) |
| 518 |
{ |
| 519 |
auto savePos = pos; |
| 520 |
scope(exit) pos = savePos; |
| 521 |
pos = refPos - n; |
| 522 |
parseLName(errMsg); |
| 523 |
} |
| 524 |
return; |
| 525 |
} |
| 526 |
|
| 527 |
bool err_flag; |
| 528 |
auto n = decodeNumber(err_flag); |
| 529 |
if (err_flag) |
| 530 |
return error("Number overflow"); |
| 531 |
|
| 532 |
if ( n == 0 ) |
| 533 |
{ |
| 534 |
put( "__anonymous" ); |
| 535 |
return; |
| 536 |
} |
| 537 |
if ( n > buf.length || n > buf.length - pos ) |
| 538 |
return error("LName must be at least 1 character"); |
| 539 |
|
| 540 |
if ( '_' != front && !isAlpha( front ) ) |
| 541 |
return error("Invalid character in LName"); |
| 542 |
|
| 543 |
foreach (char e; buf[pos + 1 .. pos + n] ) |
| 544 |
{ |
| 545 |
if ( '_' != e && !isAlpha( e ) && !isDigit( e ) ) |
| 546 |
return error("Invalid character in LName"); |
| 547 |
} |
| 548 |
|
| 549 |
put( buf[pos .. pos + n] ); |
| 550 |
pos += n; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
/* |
| 555 |
Type: |
| 556 |
Shared |
| 557 |
Const |
| 558 |
Immutable |
| 559 |
Wild |
| 560 |
TypeArray |
| 561 |
TypeVector |
| 562 |
TypeStaticArray |
| 563 |
TypeAssocArray |
| 564 |
TypePointer |
| 565 |
TypeFunction |
| 566 |
TypeIdent |
| 567 |
TypeClass |
| 568 |
TypeStruct |
| 569 |
TypeEnum |
| 570 |
TypeTypedef |
| 571 |
TypeDelegate |
| 572 |
TypeNone |
| 573 |
TypeVoid |
| 574 |
TypeNoreturn |
| 575 |
TypeByte |
| 576 |
TypeUbyte |
| 577 |
TypeShort |
| 578 |
TypeUshort |
| 579 |
TypeInt |
| 580 |
TypeUint |
| 581 |
TypeLong |
| 582 |
TypeUlong |
| 583 |
TypeCent |
| 584 |
TypeUcent |
| 585 |
TypeFloat |
| 586 |
TypeDouble |
| 587 |
TypeReal |
| 588 |
TypeIfloat |
| 589 |
TypeIdouble |
| 590 |
TypeIreal |
| 591 |
TypeCfloat |
| 592 |
TypeCdouble |
| 593 |
TypeCreal |
| 594 |
TypeBool |
| 595 |
TypeChar |
| 596 |
TypeWchar |
| 597 |
TypeDchar |
| 598 |
TypeTuple |
| 599 |
|
| 600 |
Shared: |
| 601 |
O Type |
| 602 |
|
| 603 |
Const: |
| 604 |
x Type |
| 605 |
|
| 606 |
Immutable: |
| 607 |
y Type |
| 608 |
|
| 609 |
Wild: |
| 610 |
Ng Type |
| 611 |
|
| 612 |
TypeArray: |
| 613 |
A Type |
| 614 |
|
| 615 |
TypeVector: |
| 616 |
Nh Type |
| 617 |
|
| 618 |
TypeStaticArray: |
| 619 |
G Number Type |
| 620 |
|
| 621 |
TypeAssocArray: |
| 622 |
H Type Type |
| 623 |
|
| 624 |
TypePointer: |
| 625 |
P Type |
| 626 |
|
| 627 |
TypeFunction: |
| 628 |
CallConvention FuncAttrs Arguments ArgClose Type |
| 629 |
|
| 630 |
TypeIdent: |
| 631 |
I LName |
| 632 |
|
| 633 |
TypeClass: |
| 634 |
C LName |
| 635 |
|
| 636 |
TypeStruct: |
| 637 |
S LName |
| 638 |
|
| 639 |
TypeEnum: |
| 640 |
E LName |
| 641 |
|
| 642 |
TypeTypedef: |
| 643 |
T LName |
| 644 |
|
| 645 |
TypeDelegate: |
| 646 |
D TypeFunction |
| 647 |
|
| 648 |
TypeNone: |
| 649 |
n |
| 650 |
|
| 651 |
TypeVoid: |
| 652 |
v |
| 653 |
|
| 654 |
TypeNoreturn |
| 655 |
Nn |
| 656 |
|
| 657 |
TypeByte: |
| 658 |
g |
| 659 |
|
| 660 |
TypeUbyte: |
| 661 |
h |
| 662 |
|
| 663 |
TypeShort: |
| 664 |
s |
| 665 |
|
| 666 |
TypeUshort: |
| 667 |
t |
| 668 |
|
| 669 |
TypeInt: |
| 670 |
i |
| 671 |
|
| 672 |
TypeUint: |
| 673 |
k |
| 674 |
|
| 675 |
TypeLong: |
| 676 |
l |
| 677 |
|
| 678 |
TypeUlong: |
| 679 |
m |
| 680 |
|
| 681 |
TypeCent |
| 682 |
zi |
| 683 |
|
| 684 |
TypeUcent |
| 685 |
zk |
| 686 |
|
| 687 |
TypeFloat: |
| 688 |
f |
| 689 |
|
| 690 |
TypeDouble: |
| 691 |
d |
| 692 |
|
| 693 |
TypeReal: |
| 694 |
e |
| 695 |
|
| 696 |
TypeIfloat: |
| 697 |
o |
| 698 |
|
| 699 |
TypeIdouble: |
| 700 |
p |
| 701 |
|
| 702 |
TypeIreal: |
| 703 |
j |
| 704 |
|
| 705 |
TypeCfloat: |
| 706 |
q |
| 707 |
|
| 708 |
TypeCdouble: |
| 709 |
r |
| 710 |
|
| 711 |
TypeCreal: |
| 712 |
c |
| 713 |
|
| 714 |
TypeBool: |
| 715 |
b |
| 716 |
|
| 717 |
TypeChar: |
| 718 |
a |
| 719 |
|
| 720 |
TypeWchar: |
| 721 |
u |
| 722 |
|
| 723 |
TypeDchar: |
| 724 |
w |
| 725 |
|
| 726 |
TypeTuple: |
| 727 |
B Number Arguments |
| 728 |
*/ |
| 729 |
BufSlice parseType(out bool errStatus) return scope nothrow |
| 730 |
{ |
| 731 |
static immutable string[23] primitives = [ |
| 732 |
"char", // a |
| 733 |
"bool", // b |
| 734 |
"creal", // c |
| 735 |
"double", // d |
| 736 |
"real", // e |
| 737 |
"float", // f |
| 738 |
"byte", // g |
| 739 |
"ubyte", // h |
| 740 |
"int", // i |
| 741 |
"ireal", // j |
| 742 |
"uint", // k |
| 743 |
"long", // l |
| 744 |
"ulong", // m |
| 745 |
null, // n |
| 746 |
"ifloat", // o |
| 747 |
"idouble", // p |
| 748 |
"cfloat", // q |
| 749 |
"cdouble", // r |
| 750 |
"short", // s |
| 751 |
"ushort", // t |
| 752 |
"wchar", // u |
| 753 |
"void", // v |
| 754 |
"dchar", // w |
| 755 |
]; |
| 756 |
|
| 757 |
static if (__traits(hasMember, Hooks, "parseType")) |
| 758 |
{ |
| 759 |
auto n = hooks.parseType(errStatus, this, null); |
| 760 |
if (errStatus) |
| 761 |
return dst.bslice_empty; |
| 762 |
else |
| 763 |
if (n !is null) |
| 764 |
return BufSlice(n, 0, n.length); |
| 765 |
} |
| 766 |
|
| 767 |
debug(trace) printf( "parseType+\n" ); |
| 768 |
debug(trace) scope(success) printf( "parseType-\n" ); |
| 769 |
auto beg = dst.length; |
| 770 |
auto t = front; |
| 771 |
|
| 772 |
BufSlice parseBackrefType(out string errStatus, scope BufSlice delegate(bool err_flag) pure @safe nothrow parseDg) pure @safe nothrow |
| 773 |
{ |
| 774 |
if (pos == brp) |
| 775 |
{ |
| 776 |
errStatus = "recursive back reference"; |
| 777 |
return dst.bslice_empty; |
| 778 |
} |
| 779 |
|
| 780 |
auto refPos = pos; |
| 781 |
popFront(); |
| 782 |
auto n = decodeBackref(); |
| 783 |
if (n == 0 || n > pos) |
| 784 |
{ |
| 785 |
errStatus = "invalid back reference"; |
| 786 |
return dst.bslice_empty; |
| 787 |
} |
| 788 |
|
| 789 |
if ( mute ) |
| 790 |
return dst.bslice_empty; |
| 791 |
auto savePos = pos; |
| 792 |
auto saveBrp = brp; |
| 793 |
scope(success) { pos = savePos; brp = saveBrp; } |
| 794 |
pos = refPos - n; |
| 795 |
brp = refPos; |
| 796 |
|
| 797 |
bool err_flag; |
| 798 |
auto ret = parseDg(err_flag); |
| 799 |
if (err_flag) |
| 800 |
{ |
| 801 |
errStatus = "parseDg error"; |
| 802 |
return dst.bslice_empty; |
| 803 |
} |
| 804 |
|
| 805 |
return ret; |
| 806 |
} |
| 807 |
|
| 808 |
// call parseType() and return error if occured |
| 809 |
enum parseTypeOrF = "parseType(errStatus); if (errStatus) return dst.bslice_empty;"; |
| 810 |
|
| 811 |
switch ( t ) |
| 812 |
{ |
| 813 |
case 'Q': // Type back reference |
| 814 |
string errMsg; |
| 815 |
auto r = parseBackrefType(errMsg, (e_flag) => parseType(e_flag)); |
| 816 |
if (errMsg !is null) |
| 817 |
return dst.bslice_empty; |
| 818 |
return r; |
| 819 |
case 'O': // Shared (O Type) |
| 820 |
popFront(); |
| 821 |
put( "shared(" ); |
| 822 |
mixin(parseTypeOrF); |
| 823 |
put( ')' ); |
| 824 |
return dst[beg .. $]; |
| 825 |
case 'x': // Const (x Type) |
| 826 |
popFront(); |
| 827 |
put( "const(" ); |
| 828 |
mixin(parseTypeOrF); |
| 829 |
put( ')' ); |
| 830 |
return dst[beg .. $]; |
| 831 |
case 'y': // Immutable (y Type) |
| 832 |
popFront(); |
| 833 |
put( "immutable(" ); |
| 834 |
mixin(parseTypeOrF); |
| 835 |
put( ')' ); |
| 836 |
return dst[beg .. $]; |
| 837 |
case 'N': |
| 838 |
popFront(); |
| 839 |
switch ( front ) |
| 840 |
{ |
| 841 |
case 'n': // Noreturn |
| 842 |
popFront(); |
| 843 |
put("noreturn"); |
| 844 |
return dst[beg .. $]; |
| 845 |
case 'g': // Wild (Ng Type) |
| 846 |
popFront(); |
| 847 |
// TODO: Anything needed here? |
| 848 |
put( "inout(" ); |
| 849 |
mixin(parseTypeOrF); |
| 850 |
put( ')' ); |
| 851 |
return dst[beg .. $]; |
| 852 |
case 'h': // TypeVector (Nh Type) |
| 853 |
popFront(); |
| 854 |
put( "__vector(" ); |
| 855 |
mixin(parseTypeOrF); |
| 856 |
put( ')' ); |
| 857 |
return dst[beg .. $]; |
| 858 |
default: |
| 859 |
errStatus = true; |
| 860 |
return dst.bslice_empty; |
| 861 |
} |
| 862 |
case 'A': // TypeArray (A Type) |
| 863 |
popFront(); |
| 864 |
mixin(parseTypeOrF); |
| 865 |
put( "[]" ); |
| 866 |
return dst[beg .. $]; |
| 867 |
case 'G': // TypeStaticArray (G Number Type) |
| 868 |
popFront(); |
| 869 |
auto num = sliceNumber(); |
| 870 |
mixin(parseTypeOrF); |
| 871 |
put( '[' ); |
| 872 |
put( num ); |
| 873 |
put( ']' ); |
| 874 |
return dst[beg .. $]; |
| 875 |
case 'H': // TypeAssocArray (H Type Type) |
| 876 |
popFront(); |
| 877 |
// skip t1 |
| 878 |
auto tx = parseType(errStatus); |
| 879 |
if (errStatus) |
| 880 |
return dst.bslice_empty; |
| 881 |
mixin(parseTypeOrF); |
| 882 |
put( '[' ); |
| 883 |
shift(tx); |
| 884 |
put( ']' ); |
| 885 |
return dst[beg .. $]; |
| 886 |
case 'P': // TypePointer (P Type) |
| 887 |
popFront(); |
| 888 |
mixin(parseTypeOrF); |
| 889 |
put( '*' ); |
| 890 |
return dst[beg .. $]; |
| 891 |
case 'F': case 'U': case 'W': case 'V': case 'R': // TypeFunction |
| 892 |
auto r = parseTypeFunction(errStatus); |
| 893 |
if (errStatus) |
| 894 |
return dst.bslice_empty; |
| 895 |
return r; |
| 896 |
case 'C': // TypeClass (C LName) |
| 897 |
case 'S': // TypeStruct (S LName) |
| 898 |
case 'E': // TypeEnum (E LName) |
| 899 |
case 'T': // TypeTypedef (T LName) |
| 900 |
popFront(); |
| 901 |
parseQualifiedName(errStatus); |
| 902 |
if (errStatus) |
| 903 |
return dst.bslice_empty; |
| 904 |
return dst[beg .. $]; |
| 905 |
case 'D': // TypeDelegate (D TypeFunction) |
| 906 |
popFront(); |
| 907 |
auto modifiers = parseModifier(); |
| 908 |
if ( front == 'Q' ) |
| 909 |
{ |
| 910 |
string errMsg; |
| 911 |
auto r = parseBackrefType(errMsg, (e_flag) => parseTypeFunction(e_flag, IsDelegate.yes)); |
| 912 |
if (errMsg !is null) |
| 913 |
return dst.bslice_empty; |
| 914 |
return r; |
| 915 |
} |
| 916 |
else |
| 917 |
{ |
| 918 |
parseTypeFunction(errStatus, IsDelegate.yes); |
| 919 |
if (errStatus) |
| 920 |
return dst.bslice_empty; |
| 921 |
} |
| 922 |
|
| 923 |
if (modifiers) |
| 924 |
{ |
| 925 |
// write modifiers behind the function arguments |
| 926 |
while (auto str = typeCtors.toStringConsume(modifiers)) |
| 927 |
{ |
| 928 |
put(' '); |
| 929 |
put(str); |
| 930 |
} |
| 931 |
} |
| 932 |
return dst[beg .. $]; |
| 933 |
case 'n': // TypeNone (n) |
| 934 |
popFront(); |
| 935 |
// TODO: Anything needed here? |
| 936 |
return dst[beg .. $]; |
| 937 |
case 'B': // TypeTuple (B Number Arguments) |
| 938 |
popFront(); |
| 939 |
// TODO: Handle this. |
| 940 |
return dst[beg .. $]; |
| 941 |
case 'Z': // Internal symbol |
| 942 |
// This 'type' is used for untyped internal symbols, i.e.: |
| 943 |
// __array |
| 944 |
// __init |
| 945 |
// __vtbl |
| 946 |
// __Class |
| 947 |
// __Interface |
| 948 |
// __ModuleInfo |
| 949 |
popFront(); |
| 950 |
return dst[beg .. $]; |
| 951 |
default: |
| 952 |
if (t >= 'a' && t <= 'w') |
| 953 |
{ |
| 954 |
popFront(); |
| 955 |
put( primitives[cast(size_t)(t - 'a')] ); |
| 956 |
return dst[beg .. $]; |
| 957 |
} |
| 958 |
else if (t == 'z') |
| 959 |
{ |
| 960 |
popFront(); |
| 961 |
switch ( front ) |
| 962 |
{ |
| 963 |
case 'i': |
| 964 |
popFront(); |
| 965 |
put( "cent" ); |
| 966 |
return dst[beg .. $]; |
| 967 |
case 'k': |
| 968 |
popFront(); |
| 969 |
put( "ucent" ); |
| 970 |
return dst[beg .. $]; |
| 971 |
default: |
| 972 |
errStatus = true; |
| 973 |
return dst.bslice_empty; |
| 974 |
} |
| 975 |
} |
| 976 |
errStatus = true; |
| 977 |
return dst.bslice_empty; |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
/* |
| 983 |
TypeFunction: |
| 984 |
CallConvention FuncAttrs Arguments ArgClose Type |
| 985 |
|
| 986 |
CallConvention: |
| 987 |
F // D |
| 988 |
U // C |
| 989 |
W // Windows |
| 990 |
R // C++ |
| 991 |
|
| 992 |
FuncAttrs: |
| 993 |
FuncAttr |
| 994 |
FuncAttr FuncAttrs |
| 995 |
|
| 996 |
FuncAttr: |
| 997 |
empty |
| 998 |
FuncAttrPure |
| 999 |
FuncAttrNothrow |
| 1000 |
FuncAttrProperty |
| 1001 |
FuncAttrRef |
| 1002 |
FuncAttrReturn |
| 1003 |
FuncAttrScope |
| 1004 |
FuncAttrTrusted |
| 1005 |
FuncAttrSafe |
| 1006 |
|
| 1007 |
FuncAttrPure: |
| 1008 |
Na |
| 1009 |
|
| 1010 |
FuncAttrNothrow: |
| 1011 |
Nb |
| 1012 |
|
| 1013 |
FuncAttrRef: |
| 1014 |
Nc |
| 1015 |
|
| 1016 |
FuncAttrProperty: |
| 1017 |
Nd |
| 1018 |
|
| 1019 |
FuncAttrTrusted: |
| 1020 |
Ne |
| 1021 |
|
| 1022 |
FuncAttrSafe: |
| 1023 |
Nf |
| 1024 |
|
| 1025 |
FuncAttrNogc: |
| 1026 |
Ni |
| 1027 |
|
| 1028 |
FuncAttrReturn: |
| 1029 |
Nj |
| 1030 |
|
| 1031 |
FuncAttrScope: |
| 1032 |
Nl |
| 1033 |
|
| 1034 |
Arguments: |
| 1035 |
Argument |
| 1036 |
Argument Arguments |
| 1037 |
|
| 1038 |
Argument: |
| 1039 |
Argument2 |
| 1040 |
M Argument2 // scope |
| 1041 |
|
| 1042 |
Argument2: |
| 1043 |
Type |
| 1044 |
J Type // out |
| 1045 |
K Type // ref |
| 1046 |
L Type // lazy |
| 1047 |
|
| 1048 |
ArgClose |
| 1049 |
X // variadic T t,...) style |
| 1050 |
Y // variadic T t...) style |
| 1051 |
Z // not variadic |
| 1052 |
*/ |
| 1053 |
void parseCallConvention(out bool errStatus) nothrow |
| 1054 |
{ |
| 1055 |
// CallConvention |
| 1056 |
switch ( front ) |
| 1057 |
{ |
| 1058 |
case 'F': // D |
| 1059 |
popFront(); |
| 1060 |
break; |
| 1061 |
case 'U': // C |
| 1062 |
popFront(); |
| 1063 |
put( "extern (C) " ); |
| 1064 |
break; |
| 1065 |
case 'W': // Windows |
| 1066 |
popFront(); |
| 1067 |
put( "extern (Windows) " ); |
| 1068 |
break; |
| 1069 |
case 'R': // C++ |
| 1070 |
popFront(); |
| 1071 |
put( "extern (C++) " ); |
| 1072 |
break; |
| 1073 |
default: |
| 1074 |
errStatus = true; |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
/// Returns: Flags of `TypeCtor` |
| 1079 |
ushort parseModifier() |
| 1080 |
{ |
| 1081 |
TypeCtor res = TypeCtor.None; |
| 1082 |
switch ( front ) |
| 1083 |
{ |
| 1084 |
case 'y': |
| 1085 |
popFront(); |
| 1086 |
return TypeCtor.Immutable; |
| 1087 |
case 'O': |
| 1088 |
popFront(); |
| 1089 |
res |= TypeCtor.Shared; |
| 1090 |
if (front == 'x') |
| 1091 |
goto case 'x'; |
| 1092 |
if (front == 'N') |
| 1093 |
goto case 'N'; |
| 1094 |
return TypeCtor.Shared; |
| 1095 |
case 'N': |
| 1096 |
if (peek( 1 ) != 'g') |
| 1097 |
return res; |
| 1098 |
popFront(); |
| 1099 |
popFront(); |
| 1100 |
res |= TypeCtor.InOut; |
| 1101 |
if ( front == 'x' ) |
| 1102 |
goto case 'x'; |
| 1103 |
return res; |
| 1104 |
case 'x': |
| 1105 |
popFront(); |
| 1106 |
res |= TypeCtor.Const; |
| 1107 |
return res; |
| 1108 |
default: return TypeCtor.None; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
ushort parseFuncAttr(out bool errStatus) nothrow |
| 1113 |
{ |
| 1114 |
// FuncAttrs |
| 1115 |
ushort result; |
| 1116 |
while ('N' == front) |
| 1117 |
{ |
| 1118 |
popFront(); |
| 1119 |
switch ( front ) |
| 1120 |
{ |
| 1121 |
case 'a': // FuncAttrPure |
| 1122 |
popFront(); |
| 1123 |
result |= FuncAttributes.Pure; |
| 1124 |
continue; |
| 1125 |
case 'b': // FuncAttrNoThrow |
| 1126 |
popFront(); |
| 1127 |
result |= FuncAttributes.Nothrow; |
| 1128 |
continue; |
| 1129 |
case 'c': // FuncAttrRef |
| 1130 |
popFront(); |
| 1131 |
result |= FuncAttributes.Ref; |
| 1132 |
continue; |
| 1133 |
case 'd': // FuncAttrProperty |
| 1134 |
popFront(); |
| 1135 |
result |= FuncAttributes.Property; |
| 1136 |
continue; |
| 1137 |
case 'e': // FuncAttrTrusted |
| 1138 |
popFront(); |
| 1139 |
result |= FuncAttributes.Trusted; |
| 1140 |
continue; |
| 1141 |
case 'f': // FuncAttrSafe |
| 1142 |
popFront(); |
| 1143 |
result |= FuncAttributes.Safe; |
| 1144 |
continue; |
| 1145 |
case 'g': |
| 1146 |
case 'h': |
| 1147 |
case 'k': |
| 1148 |
case 'n': |
| 1149 |
// NOTE: The inout parameter type is represented as "Ng". |
| 1150 |
// The vector parameter type is represented as "Nh". |
| 1151 |
// The return parameter type is represented as "Nk". |
| 1152 |
// The noreturn parameter type is represented as "Nn". |
| 1153 |
// These make it look like a FuncAttr, but infact |
| 1154 |
// if we see these, then we know we're really in |
| 1155 |
// the parameter list. Rewind and break. |
| 1156 |
pos--; |
| 1157 |
return result; |
| 1158 |
case 'i': // FuncAttrNogc |
| 1159 |
popFront(); |
| 1160 |
result |= FuncAttributes.NoGC; |
| 1161 |
continue; |
| 1162 |
case 'j': // FuncAttrReturn |
| 1163 |
popFront(); |
| 1164 |
if (this.peek(0) == 'N' && this.peek(1) == 'l') |
| 1165 |
{ |
| 1166 |
result |= FuncAttributes.ReturnScope; |
| 1167 |
popFront(); |
| 1168 |
popFront(); |
| 1169 |
} else { |
| 1170 |
result |= FuncAttributes.Return; |
| 1171 |
} |
| 1172 |
continue; |
| 1173 |
case 'l': // FuncAttrScope |
| 1174 |
popFront(); |
| 1175 |
if (this.peek(0) == 'N' && this.peek(1) == 'j') |
| 1176 |
{ |
| 1177 |
result |= FuncAttributes.ScopeReturn; |
| 1178 |
popFront(); |
| 1179 |
popFront(); |
| 1180 |
} else { |
| 1181 |
result |= FuncAttributes.Scope; |
| 1182 |
} |
| 1183 |
continue; |
| 1184 |
case 'm': // FuncAttrLive |
| 1185 |
popFront(); |
| 1186 |
result |= FuncAttributes.Live; |
| 1187 |
continue; |
| 1188 |
default: |
| 1189 |
errStatus = true; |
| 1190 |
return 0; |
| 1191 |
} |
| 1192 |
} |
| 1193 |
return result; |
| 1194 |
} |
| 1195 |
|
| 1196 |
void parseFuncArguments(out bool errStatus) scope nothrow |
| 1197 |
{ |
| 1198 |
// Arguments |
| 1199 |
for ( size_t n = 0; true; n++ ) |
| 1200 |
{ |
| 1201 |
debug(info) printf( "tok (%c)\n", front ); |
| 1202 |
switch ( front ) |
| 1203 |
{ |
| 1204 |
case 'X': // ArgClose (variadic T t...) style) |
| 1205 |
popFront(); |
| 1206 |
put( "..." ); |
| 1207 |
return; |
| 1208 |
case 'Y': // ArgClose (variadic T t,...) style) |
| 1209 |
popFront(); |
| 1210 |
put( ", ..." ); |
| 1211 |
return; |
| 1212 |
case 'Z': // ArgClose (not variadic) |
| 1213 |
popFront(); |
| 1214 |
return; |
| 1215 |
default: |
| 1216 |
break; |
| 1217 |
} |
| 1218 |
putComma(n); |
| 1219 |
|
| 1220 |
/* Do special return, scope, ref, out combinations |
| 1221 |
*/ |
| 1222 |
int npops; |
| 1223 |
if ( 'M' == front && peek(1) == 'N' && peek(2) == 'k') |
| 1224 |
{ |
| 1225 |
const c3 = peek(3); |
| 1226 |
if (c3 == 'J') |
| 1227 |
{ |
| 1228 |
put("scope return out "); // MNkJ |
| 1229 |
npops = 4; |
| 1230 |
} |
| 1231 |
else if (c3 == 'K') |
| 1232 |
{ |
| 1233 |
put("scope return ref "); // MNkK |
| 1234 |
npops = 4; |
| 1235 |
} |
| 1236 |
} |
| 1237 |
else if ('N' == front && peek(1) == 'k') |
| 1238 |
{ |
| 1239 |
const c2 = peek(2); |
| 1240 |
if (c2 == 'J') |
| 1241 |
{ |
| 1242 |
put("return out "); // NkJ |
| 1243 |
npops = 3; |
| 1244 |
} |
| 1245 |
else if (c2 == 'K') |
| 1246 |
{ |
| 1247 |
put("return ref "); // NkK |
| 1248 |
npops = 3; |
| 1249 |
} |
| 1250 |
else if (c2 == 'M') |
| 1251 |
{ |
| 1252 |
const c3 = peek(3); |
| 1253 |
if (c3 == 'J') |
| 1254 |
{ |
| 1255 |
put("return scope out "); // NkMJ |
| 1256 |
npops = 4; |
| 1257 |
} |
| 1258 |
else if (c3 == 'K') |
| 1259 |
{ |
| 1260 |
put("return scope ref "); // NkMK |
| 1261 |
npops = 4; |
| 1262 |
} |
| 1263 |
else |
| 1264 |
{ |
| 1265 |
put("return scope "); // NkM |
| 1266 |
npops = 3; |
| 1267 |
} |
| 1268 |
} |
| 1269 |
} |
| 1270 |
popFront(npops); |
| 1271 |
|
| 1272 |
if ( 'M' == front ) |
| 1273 |
{ |
| 1274 |
popFront(); |
| 1275 |
put( "scope " ); |
| 1276 |
} |
| 1277 |
if ( 'N' == front ) |
| 1278 |
{ |
| 1279 |
popFront(); |
| 1280 |
if ( 'k' == front ) // Return (Nk Parameter2) |
| 1281 |
{ |
| 1282 |
popFront(); |
| 1283 |
put( "return " ); |
| 1284 |
} |
| 1285 |
else |
| 1286 |
pos--; |
| 1287 |
} |
| 1288 |
|
| 1289 |
// call parseType() and return error if occured |
| 1290 |
enum parseTypeOrF = "parseType(errStatus); if (errStatus) return;"; |
| 1291 |
|
| 1292 |
switch ( front ) |
| 1293 |
{ |
| 1294 |
case 'I': // in (I Type) |
| 1295 |
popFront(); |
| 1296 |
put("in "); |
| 1297 |
if (front == 'K') |
| 1298 |
goto case; |
| 1299 |
mixin(parseTypeOrF); |
| 1300 |
continue; |
| 1301 |
case 'K': // ref (K Type) |
| 1302 |
popFront(); |
| 1303 |
put( "ref " ); |
| 1304 |
mixin(parseTypeOrF); |
| 1305 |
continue; |
| 1306 |
case 'J': // out (J Type) |
| 1307 |
popFront(); |
| 1308 |
put( "out " ); |
| 1309 |
mixin(parseTypeOrF); |
| 1310 |
continue; |
| 1311 |
case 'L': // lazy (L Type) |
| 1312 |
popFront(); |
| 1313 |
put( "lazy " ); |
| 1314 |
mixin(parseTypeOrF); |
| 1315 |
continue; |
| 1316 |
default: |
| 1317 |
mixin(parseTypeOrF); |
| 1318 |
} |
| 1319 |
} |
| 1320 |
} |
| 1321 |
|
| 1322 |
enum IsDelegate { no, yes } |
| 1323 |
|
| 1324 |
/* |
| 1325 |
TypeFunction: |
| 1326 |
CallConvention FuncAttrs Arguments ArgClose Type |
| 1327 |
*/ |
| 1328 |
BufSlice parseTypeFunction(out bool errStatus, IsDelegate isdg = IsDelegate.no) return scope nothrow |
| 1329 |
{ |
| 1330 |
debug(trace) printf( "parseTypeFunction+\n" ); |
| 1331 |
debug(trace) scope(success) printf( "parseTypeFunction-\n" ); |
| 1332 |
auto beg = dst.length; |
| 1333 |
|
| 1334 |
parseCallConvention(errStatus); |
| 1335 |
if (errStatus) |
| 1336 |
return dst.bslice_empty; |
| 1337 |
|
| 1338 |
auto attributes = parseFuncAttr(errStatus); |
| 1339 |
if (errStatus) |
| 1340 |
return dst.bslice_empty; |
| 1341 |
|
| 1342 |
auto argbeg = dst.length; |
| 1343 |
put(IsDelegate.yes == isdg ? "delegate" : "function"); |
| 1344 |
put( '(' ); |
| 1345 |
parseFuncArguments(errStatus); |
| 1346 |
if (errStatus) |
| 1347 |
return dst.bslice_empty; |
| 1348 |
put( ')' ); |
| 1349 |
if (attributes) |
| 1350 |
{ |
| 1351 |
// write function attributes behind arguments |
| 1352 |
while (auto str = funcAttrs.toStringConsume(attributes)) |
| 1353 |
{ |
| 1354 |
put(' '); |
| 1355 |
put(str); |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
// A function / delegate return type is located at the end of its mangling |
| 1360 |
// Write it in order, then shift it back to 'code order' |
| 1361 |
// e.g. `delegate(int) @safedouble ' => 'double delegate(int) @safe' |
| 1362 |
{ |
| 1363 |
auto retbeg = dst.length; |
| 1364 |
parseType(errStatus); |
| 1365 |
if (errStatus) |
| 1366 |
return dst.bslice_empty; |
| 1367 |
put(' '); |
| 1368 |
shift(dst[argbeg .. retbeg]); |
| 1369 |
} |
| 1370 |
|
| 1371 |
return dst[beg .. $]; |
| 1372 |
} |
| 1373 |
|
| 1374 |
static bool isCallConvention( char ch ) |
| 1375 |
{ |
| 1376 |
switch ( ch ) |
| 1377 |
{ |
| 1378 |
case 'F', 'U', 'V', 'W', 'R': |
| 1379 |
return true; |
| 1380 |
default: |
| 1381 |
return false; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
|
| 1385 |
/* |
| 1386 |
Value: |
| 1387 |
n |
| 1388 |
Number |
| 1389 |
i Number |
| 1390 |
N Number |
| 1391 |
e HexFloat |
| 1392 |
c HexFloat c HexFloat |
| 1393 |
A Number Value... |
| 1394 |
|
| 1395 |
HexFloat: |
| 1396 |
NAN |
| 1397 |
INF |
| 1398 |
NINF |
| 1399 |
N HexDigits P Exponent |
| 1400 |
HexDigits P Exponent |
| 1401 |
|
| 1402 |
Exponent: |
| 1403 |
N Number |
| 1404 |
Number |
| 1405 |
|
| 1406 |
HexDigits: |
| 1407 |
HexDigit |
| 1408 |
HexDigit HexDigits |
| 1409 |
|
| 1410 |
HexDigit: |
| 1411 |
Digit |
| 1412 |
A |
| 1413 |
B |
| 1414 |
C |
| 1415 |
D |
| 1416 |
E |
| 1417 |
F |
| 1418 |
*/ |
| 1419 |
|
| 1420 |
void parseValue(out bool errStatus) scope nothrow |
| 1421 |
{ |
| 1422 |
parseValue(errStatus, dst.bslice_empty); |
| 1423 |
} |
| 1424 |
|
| 1425 |
void parseValue(out bool errStatus, scope BufSlice name, char type = '\0' ) scope nothrow |
| 1426 |
{ |
| 1427 |
debug(trace) printf( "parseValue+\n" ); |
| 1428 |
debug(trace) scope(success) printf( "parseValue-\n" ); |
| 1429 |
|
| 1430 |
void onError() |
| 1431 |
{ |
| 1432 |
errStatus = true; |
| 1433 |
} |
| 1434 |
|
| 1435 |
// printf( "*** %c\n", front ); |
| 1436 |
switch ( front ) |
| 1437 |
{ |
| 1438 |
case 'n': |
| 1439 |
popFront(); |
| 1440 |
put( "null" ); |
| 1441 |
return; |
| 1442 |
case 'i': |
| 1443 |
popFront(); |
| 1444 |
if ('0' > front || '9' < front) |
| 1445 |
return onError(); // Number expected |
| 1446 |
goto case; |
| 1447 |
case '0': .. case '9': |
| 1448 |
parseIntegerValue( errStatus, name, type ); |
| 1449 |
return; |
| 1450 |
case 'N': |
| 1451 |
popFront(); |
| 1452 |
put( '-' ); |
| 1453 |
parseIntegerValue( errStatus, name, type ); |
| 1454 |
return; |
| 1455 |
case 'e': |
| 1456 |
popFront(); |
| 1457 |
parseReal(errStatus); |
| 1458 |
return; |
| 1459 |
case 'c': |
| 1460 |
popFront(); |
| 1461 |
parseReal(errStatus); |
| 1462 |
if (errStatus) |
| 1463 |
return; |
| 1464 |
put( '+' ); |
| 1465 |
if (!match('c')) |
| 1466 |
return onError(); |
| 1467 |
parseReal(errStatus); |
| 1468 |
if (errStatus) |
| 1469 |
return; |
| 1470 |
put( 'i' ); |
| 1471 |
return; |
| 1472 |
case 'a': case 'w': case 'd': |
| 1473 |
char t = front; |
| 1474 |
popFront(); |
| 1475 |
auto n = decodeNumber(errStatus); |
| 1476 |
if (errStatus) |
| 1477 |
return; |
| 1478 |
if (!match('_')) |
| 1479 |
return onError(); |
| 1480 |
put( '"' ); |
| 1481 |
foreach (i; 0..n) |
| 1482 |
{ |
| 1483 |
auto a = ascii2hex( errStatus, front ); |
| 1484 |
if (errStatus) |
| 1485 |
return; |
| 1486 |
popFront(); |
| 1487 |
|
| 1488 |
auto b = ascii2hex( errStatus, front ); |
| 1489 |
if (errStatus) |
| 1490 |
return; |
| 1491 |
popFront(); |
| 1492 |
|
| 1493 |
auto v = cast(char)((a << 4) | b); |
| 1494 |
if (' ' <= v && v <= '~') // ASCII printable |
| 1495 |
{ |
| 1496 |
put(v); |
| 1497 |
} |
| 1498 |
else |
| 1499 |
{ |
| 1500 |
put("\\x"); |
| 1501 |
putAsHex(v, 2); |
| 1502 |
} |
| 1503 |
} |
| 1504 |
put( '"' ); |
| 1505 |
if ( 'a' != t ) |
| 1506 |
put(t); |
| 1507 |
return; |
| 1508 |
case 'A': |
| 1509 |
// NOTE: This is kind of a hack. An associative array literal |
| 1510 |
// [1:2, 3:4] is represented as HiiA2i1i2i3i4, so the type |
| 1511 |
// is "Hii" and the value is "A2i1i2i3i4". Thus the only |
| 1512 |
// way to determine that this is an AA value rather than an |
| 1513 |
// array value is for the caller to supply the type char. |
| 1514 |
// Hopefully, this will change so that the value is |
| 1515 |
// "H2i1i2i3i4", rendering this unnecesary. |
| 1516 |
if ( 'H' == type ) |
| 1517 |
goto LassocArray; |
| 1518 |
// A Number Value... |
| 1519 |
// An array literal. Value is repeated Number times. |
| 1520 |
popFront(); |
| 1521 |
put( '[' ); |
| 1522 |
auto n = decodeNumber(errStatus); |
| 1523 |
if (errStatus) |
| 1524 |
return; |
| 1525 |
foreach ( i; 0 .. n ) |
| 1526 |
{ |
| 1527 |
putComma(i); |
| 1528 |
parseValue(errStatus); |
| 1529 |
if (errStatus) |
| 1530 |
return; |
| 1531 |
} |
| 1532 |
put( ']' ); |
| 1533 |
return; |
| 1534 |
case 'H': |
| 1535 |
LassocArray: |
| 1536 |
// H Number Value... |
| 1537 |
// An associative array literal. Value is repeated 2*Number times. |
| 1538 |
popFront(); |
| 1539 |
put( '[' ); |
| 1540 |
auto n = decodeNumber(errStatus); |
| 1541 |
if (errStatus) |
| 1542 |
return; |
| 1543 |
foreach ( i; 0 .. n ) |
| 1544 |
{ |
| 1545 |
putComma(i); |
| 1546 |
parseValue(errStatus); |
| 1547 |
if (errStatus) |
| 1548 |
return; |
| 1549 |
put(':'); |
| 1550 |
parseValue(errStatus); |
| 1551 |
if (errStatus) |
| 1552 |
return; |
| 1553 |
} |
| 1554 |
put( ']' ); |
| 1555 |
return; |
| 1556 |
case 'S': |
| 1557 |
// S Number Value... |
| 1558 |
// A struct literal. Value is repeated Number times. |
| 1559 |
popFront(); |
| 1560 |
if ( name.length ) |
| 1561 |
put( name ); |
| 1562 |
put( '(' ); |
| 1563 |
auto n = decodeNumber(errStatus); |
| 1564 |
if (errStatus) |
| 1565 |
return; |
| 1566 |
foreach ( i; 0 .. n ) |
| 1567 |
{ |
| 1568 |
putComma(i); |
| 1569 |
parseValue(errStatus); |
| 1570 |
if (errStatus) |
| 1571 |
return; |
| 1572 |
} |
| 1573 |
put( ')' ); |
| 1574 |
return; |
| 1575 |
case 'f': |
| 1576 |
// f MangledName |
| 1577 |
// A function literal symbol |
| 1578 |
popFront(); |
| 1579 |
parseMangledName(errStatus, false, 1); |
| 1580 |
return; |
| 1581 |
default: |
| 1582 |
errStatus = true; |
| 1583 |
} |
| 1584 |
} |
| 1585 |
|
| 1586 |
void parseIntegerValue( out bool errStatus, scope BufSlice name, char type = '\0' ) scope nothrow |
| 1587 |
{ |
| 1588 |
debug(trace) printf( "parseIntegerValue+\n" ); |
| 1589 |
debug(trace) scope(success) printf( "parseIntegerValue-\n" ); |
| 1590 |
|
| 1591 |
switch ( type ) |
| 1592 |
{ |
| 1593 |
case 'a': // char |
| 1594 |
case 'u': // wchar |
| 1595 |
case 'w': // dchar |
| 1596 |
{ |
| 1597 |
auto val = sliceNumber(); |
| 1598 |
auto num = decodeNumber( errStatus, val ); |
| 1599 |
if (errStatus) |
| 1600 |
return; |
| 1601 |
|
| 1602 |
switch ( num ) |
| 1603 |
{ |
| 1604 |
case '\'': |
| 1605 |
put( "'\\''" ); |
| 1606 |
return; |
| 1607 |
// \", \? |
| 1608 |
case '\\': |
| 1609 |
put( "'\\\\'" ); |
| 1610 |
return; |
| 1611 |
case '\a': |
| 1612 |
put( "'\\a'" ); |
| 1613 |
return; |
| 1614 |
case '\b': |
| 1615 |
put( "'\\b'" ); |
| 1616 |
return; |
| 1617 |
case '\f': |
| 1618 |
put( "'\\f'" ); |
| 1619 |
return; |
| 1620 |
case '\n': |
| 1621 |
put( "'\\n'" ); |
| 1622 |
return; |
| 1623 |
case '\r': |
| 1624 |
put( "'\\r'" ); |
| 1625 |
return; |
| 1626 |
case '\t': |
| 1627 |
put( "'\\t'" ); |
| 1628 |
return; |
| 1629 |
case '\v': |
| 1630 |
put( "'\\v'" ); |
| 1631 |
return; |
| 1632 |
default: |
| 1633 |
switch ( type ) |
| 1634 |
{ |
| 1635 |
case 'a': |
| 1636 |
if ( num >= 0x20 && num < 0x7F ) |
| 1637 |
{ |
| 1638 |
put( '\'' ); |
| 1639 |
put( cast(char)num ); |
| 1640 |
put( '\'' ); |
| 1641 |
return; |
| 1642 |
} |
| 1643 |
put( "\\x" ); |
| 1644 |
putAsHex( num, 2 ); |
| 1645 |
return; |
| 1646 |
case 'u': |
| 1647 |
put( "'\\u" ); |
| 1648 |
putAsHex( num, 4 ); |
| 1649 |
put( '\'' ); |
| 1650 |
return; |
| 1651 |
case 'w': |
| 1652 |
put( "'\\U" ); |
| 1653 |
putAsHex( num, 8 ); |
| 1654 |
put( '\'' ); |
| 1655 |
return; |
| 1656 |
default: |
| 1657 |
assert( 0 ); |
| 1658 |
} |
| 1659 |
} |
| 1660 |
} |
| 1661 |
case 'b': // bool |
| 1662 |
auto d = decodeNumber(errStatus); |
| 1663 |
if (errStatus) |
| 1664 |
return; |
| 1665 |
put( d ? "true" : "false" ); |
| 1666 |
return; |
| 1667 |
case 'h', 't', 'k': // ubyte, ushort, uint |
| 1668 |
put( sliceNumber() ); |
| 1669 |
put( 'u' ); |
| 1670 |
return; |
| 1671 |
case 'l': // long |
| 1672 |
put( sliceNumber() ); |
| 1673 |
put( 'L' ); |
| 1674 |
return; |
| 1675 |
case 'm': // ulong |
| 1676 |
put( sliceNumber() ); |
| 1677 |
put( "uL" ); |
| 1678 |
return; |
| 1679 |
default: |
| 1680 |
put( sliceNumber() ); |
| 1681 |
return; |
| 1682 |
} |
| 1683 |
} |
| 1684 |
|
| 1685 |
|
| 1686 |
/* |
| 1687 |
TemplateArgs: |
| 1688 |
TemplateArg |
| 1689 |
TemplateArg TemplateArgs |
| 1690 |
|
| 1691 |
TemplateArg: |
| 1692 |
TemplateArgX |
| 1693 |
H TemplateArgX |
| 1694 |
|
| 1695 |
TemplateArgX: |
| 1696 |
T Type |
| 1697 |
V Type Value |
| 1698 |
S Number_opt QualifiedName |
| 1699 |
X ExternallyMangledName |
| 1700 |
*/ |
| 1701 |
void parseTemplateArgs(out bool errStatus) scope nothrow |
| 1702 |
{ |
| 1703 |
debug(trace) printf( "parseTemplateArgs+\n" ); |
| 1704 |
debug(trace) scope(success) printf( "parseTemplateArgs-\n" ); |
| 1705 |
|
| 1706 |
L_nextArg: |
| 1707 |
for ( size_t n = 0; true; n++ ) |
| 1708 |
{ |
| 1709 |
if ( front == 'H' ) |
| 1710 |
popFront(); |
| 1711 |
|
| 1712 |
switch ( front ) |
| 1713 |
{ |
| 1714 |
case 'T': |
| 1715 |
popFront(); |
| 1716 |
putComma(n); |
| 1717 |
parseType(errStatus); |
| 1718 |
if (errStatus) |
| 1719 |
return; |
| 1720 |
continue; |
| 1721 |
case 'V': |
| 1722 |
popFront(); |
| 1723 |
putComma(n); |
| 1724 |
// NOTE: In the few instances where the type is actually |
| 1725 |
// desired in the output it should precede the value |
| 1726 |
// generated by parseValue, so it is safe to simply |
| 1727 |
// decrement len and let put/append do its thing. |
| 1728 |
char t = front; // peek at type for parseValue |
| 1729 |
if ( t == 'Q' ) |
| 1730 |
{ |
| 1731 |
t = peekBackref(); |
| 1732 |
if (t == 0) |
| 1733 |
{ |
| 1734 |
// invalid back reference |
| 1735 |
errStatus = true; |
| 1736 |
return; |
| 1737 |
} |
| 1738 |
} |
| 1739 |
BufSlice name = dst.bslice_empty; |
| 1740 |
silent( errStatus, delegate void(out bool e_flg) nothrow { name = parseType(e_flg); } ); |
| 1741 |
if (errStatus) |
| 1742 |
return; |
| 1743 |
parseValue( errStatus, name, t ); |
| 1744 |
if (errStatus) |
| 1745 |
return; |
| 1746 |
continue; |
| 1747 |
case 'S': |
| 1748 |
popFront(); |
| 1749 |
putComma(n); |
| 1750 |
|
| 1751 |
if ( mayBeMangledNameArg() ) |
| 1752 |
{ |
| 1753 |
auto l = dst.length; |
| 1754 |
auto p = pos; |
| 1755 |
auto b = brp; |
| 1756 |
|
| 1757 |
debug(trace) printf( "may be mangled name arg\n" ); |
| 1758 |
|
| 1759 |
if (parseMangledNameArg()) |
| 1760 |
continue; |
| 1761 |
dst.len = l; |
| 1762 |
pos = p; |
| 1763 |
brp = b; |
| 1764 |
debug(trace) printf( "not a mangled name arg\n" ); |
| 1765 |
} |
| 1766 |
if ( isDigit( front ) && isDigit( peek( 1 ) ) ) |
| 1767 |
{ |
| 1768 |
// ambiguity: length followed by qualified name (starting with number) |
| 1769 |
// try all possible pairs of numbers |
| 1770 |
auto qlen = decodeNumber(errStatus); |
| 1771 |
if (errStatus) |
| 1772 |
return; |
| 1773 |
|
| 1774 |
qlen /= 10; // last digit needed for QualifiedName |
| 1775 |
pos--; |
| 1776 |
auto l = dst.length; |
| 1777 |
auto p = pos; |
| 1778 |
auto b = brp; |
| 1779 |
while ( qlen > 0 ) |
| 1780 |
{ |
| 1781 |
errStatus = false; |
| 1782 |
parseQualifiedName(errStatus); |
| 1783 |
|
| 1784 |
if (!errStatus) |
| 1785 |
{ |
| 1786 |
if ( pos == p + qlen ) |
| 1787 |
continue L_nextArg; |
| 1788 |
} |
| 1789 |
|
| 1790 |
qlen /= 10; // retry with one digit less |
| 1791 |
pos = --p; |
| 1792 |
dst.len = l; |
| 1793 |
brp = b; |
| 1794 |
} |
| 1795 |
} |
| 1796 |
|
| 1797 |
parseQualifiedName(errStatus); |
| 1798 |
if (errStatus) |
| 1799 |
return; |
| 1800 |
continue; |
| 1801 |
case 'X': |
| 1802 |
popFront(); |
| 1803 |
putComma(n); |
| 1804 |
{ |
| 1805 |
string errMsg; |
| 1806 |
parseLName(errMsg); |
| 1807 |
if (errMsg) |
| 1808 |
return; |
| 1809 |
} |
| 1810 |
continue; |
| 1811 |
default: |
| 1812 |
return; |
| 1813 |
} |
| 1814 |
} |
| 1815 |
} |
| 1816 |
|
| 1817 |
|
| 1818 |
bool mayBeMangledNameArg() nothrow |
| 1819 |
{ |
| 1820 |
debug(trace) printf( "mayBeMangledNameArg+\n" ); |
| 1821 |
debug(trace) scope(success) printf( "mayBeMangledNameArg-\n" ); |
| 1822 |
|
| 1823 |
bool errStatus; |
| 1824 |
auto p = pos; |
| 1825 |
scope(exit) pos = p; |
| 1826 |
|
| 1827 |
if ( isDigit( buf[pos] ) ) |
| 1828 |
{ |
| 1829 |
auto n = decodeNumber(errStatus); |
| 1830 |
|
| 1831 |
return !errStatus && n >= 4 && |
| 1832 |
pos < buf.length && '_' == buf[pos++] && |
| 1833 |
pos < buf.length && 'D' == buf[pos++] && |
| 1834 |
isDigit( buf[pos] ); |
| 1835 |
} |
| 1836 |
else |
| 1837 |
{ |
| 1838 |
const isSNF = isSymbolNameFront(errStatus); |
| 1839 |
|
| 1840 |
return !errStatus && |
| 1841 |
pos < buf.length && '_' == buf[pos++] && |
| 1842 |
pos < buf.length && 'D' == buf[pos++] && |
| 1843 |
isSNF; |
| 1844 |
} |
| 1845 |
} |
| 1846 |
|
| 1847 |
bool parseMangledNameArg() nothrow |
| 1848 |
{ |
| 1849 |
debug(trace) printf( "parseMangledNameArg+\n" ); |
| 1850 |
debug(trace) scope(success) printf( "parseMangledNameArg-\n" ); |
| 1851 |
|
| 1852 |
bool errStatus; |
| 1853 |
|
| 1854 |
size_t n = 0; |
| 1855 |
if ( isDigit( front ) ) |
| 1856 |
{ |
| 1857 |
n = decodeNumber(errStatus); |
| 1858 |
|
| 1859 |
if (errStatus) |
| 1860 |
return false; |
| 1861 |
} |
| 1862 |
|
| 1863 |
parseMangledName(errStatus, false, n ); |
| 1864 |
|
| 1865 |
return !errStatus; |
| 1866 |
} |
| 1867 |
|
| 1868 |
/* |
| 1869 |
TemplateInstanceName: |
| 1870 |
Number __T LName TemplateArgs Z |
| 1871 |
*/ |
| 1872 |
void parseTemplateInstanceName(out bool errStatus, bool hasNumber) scope nothrow |
| 1873 |
{ |
| 1874 |
debug(trace) printf( "parseTemplateInstanceName+\n" ); |
| 1875 |
debug(trace) scope(success) printf( "parseTemplateInstanceName-\n" ); |
| 1876 |
|
| 1877 |
auto sav = pos; |
| 1878 |
auto saveBrp = brp; |
| 1879 |
|
| 1880 |
void onError() |
| 1881 |
{ |
| 1882 |
errStatus = true; |
| 1883 |
pos = sav; |
| 1884 |
brp = saveBrp; |
| 1885 |
} |
| 1886 |
|
| 1887 |
size_t n = 0; |
| 1888 |
if (hasNumber) |
| 1889 |
{ |
| 1890 |
n = decodeNumber(errStatus); |
| 1891 |
if (errStatus) |
| 1892 |
return onError(); |
| 1893 |
} |
| 1894 |
|
| 1895 |
auto beg = pos; |
| 1896 |
errStatus = !match( "__T" ); |
| 1897 |
if (errStatus) |
| 1898 |
return onError(); |
| 1899 |
|
| 1900 |
{ |
| 1901 |
string errMsg; |
| 1902 |
parseLName(errMsg); |
| 1903 |
if (errMsg !is null) |
| 1904 |
return onError(); |
| 1905 |
} |
| 1906 |
|
| 1907 |
put( "!(" ); |
| 1908 |
|
| 1909 |
parseTemplateArgs(errStatus); |
| 1910 |
if (errStatus) |
| 1911 |
return onError(); |
| 1912 |
|
| 1913 |
if (!match('Z')) |
| 1914 |
return onError(); |
| 1915 |
|
| 1916 |
if ( hasNumber && pos - beg != n ) |
| 1917 |
{ |
| 1918 |
// Template name length mismatch |
| 1919 |
return onError(); |
| 1920 |
} |
| 1921 |
|
| 1922 |
put( ')' ); |
| 1923 |
} |
| 1924 |
|
| 1925 |
|
| 1926 |
bool mayBeTemplateInstanceName() scope nothrow |
| 1927 |
{ |
| 1928 |
debug(trace) printf( "mayBeTemplateInstanceName+\n" ); |
| 1929 |
debug(trace) scope(success) printf( "mayBeTemplateInstanceName-\n" ); |
| 1930 |
|
| 1931 |
auto p = pos; |
| 1932 |
scope(exit) pos = p; |
| 1933 |
|
| 1934 |
bool errStatus; |
| 1935 |
auto n = decodeNumber(errStatus); |
| 1936 |
if (errStatus) |
| 1937 |
return false; |
| 1938 |
|
| 1939 |
return n >= 5 && |
| 1940 |
pos < buf.length && '_' == buf[pos++] && |
| 1941 |
pos < buf.length && '_' == buf[pos++] && |
| 1942 |
pos < buf.length && 'T' == buf[pos++]; |
| 1943 |
} |
| 1944 |
|
| 1945 |
|
| 1946 |
/* |
| 1947 |
SymbolName: |
| 1948 |
LName |
| 1949 |
TemplateInstanceName |
| 1950 |
*/ |
| 1951 |
void parseSymbolName(out bool errStatus) scope nothrow |
| 1952 |
{ |
| 1953 |
debug(trace) printf( "parseSymbolName+\n" ); |
| 1954 |
debug(trace) scope(success) printf( "parseSymbolName-\n" ); |
| 1955 |
|
| 1956 |
// LName -> Number |
| 1957 |
// TemplateInstanceName -> Number "__T" |
| 1958 |
switch ( front ) |
| 1959 |
{ |
| 1960 |
case '_': |
| 1961 |
// no length encoding for templates for new mangling |
| 1962 |
parseTemplateInstanceName(errStatus, false); |
| 1963 |
return; |
| 1964 |
|
| 1965 |
case '0': .. case '9': |
| 1966 |
if ( mayBeTemplateInstanceName() ) |
| 1967 |
{ |
| 1968 |
auto t = dst.length; |
| 1969 |
|
| 1970 |
debug(trace) printf( "may be template instance name\n" ); |
| 1971 |
parseTemplateInstanceName(errStatus, true); |
| 1972 |
if (!errStatus) |
| 1973 |
return; |
| 1974 |
else |
| 1975 |
{ |
| 1976 |
debug(trace) printf( "not a template instance name\n" ); |
| 1977 |
dst.len = t; |
| 1978 |
} |
| 1979 |
} |
| 1980 |
goto case; |
| 1981 |
case 'Q': |
| 1982 |
string errMsg; |
| 1983 |
parseLName(errMsg); |
| 1984 |
errStatus = errMsg !is null; |
| 1985 |
return; |
| 1986 |
default: |
| 1987 |
errStatus = true; |
| 1988 |
} |
| 1989 |
} |
| 1990 |
|
| 1991 |
// parse optional function arguments as part of a symbol name, i.e without return type |
| 1992 |
// if keepAttr, the calling convention and function attributes are not discarded, but returned |
| 1993 |
BufSlice parseFunctionTypeNoReturn( bool keepAttr = false ) return scope nothrow |
| 1994 |
{ |
| 1995 |
// try to demangle a function, in case we are pointing to some function local |
| 1996 |
auto prevpos = pos; |
| 1997 |
auto prevlen = dst.length; |
| 1998 |
auto prevbrp = brp; |
| 1999 |
|
| 2000 |
if ( 'M' == front ) |
| 2001 |
{ |
| 2002 |
// do not emit "needs this" |
| 2003 |
popFront(); |
| 2004 |
auto modifiers = parseModifier(); |
| 2005 |
while (auto str = typeCtors.toStringConsume(modifiers)) |
| 2006 |
{ |
| 2007 |
put(str); |
| 2008 |
put(' '); |
| 2009 |
} |
| 2010 |
} |
| 2011 |
if ( isCallConvention( front ) ) |
| 2012 |
{ |
| 2013 |
BufSlice attr = dst.bslice_empty; |
| 2014 |
// we don't want calling convention and attributes in the qualified name |
| 2015 |
bool errStatus; |
| 2016 |
parseCallConvention(errStatus); |
| 2017 |
if (!errStatus) |
| 2018 |
{ |
| 2019 |
auto attributes = parseFuncAttr(errStatus); |
| 2020 |
if (!errStatus) |
| 2021 |
{ |
| 2022 |
if (keepAttr) { |
| 2023 |
while (auto str = funcAttrs.toStringConsume(attributes)) |
| 2024 |
{ |
| 2025 |
put(str); |
| 2026 |
put(' '); |
| 2027 |
} |
| 2028 |
attr = dst[prevlen .. $]; |
| 2029 |
} |
| 2030 |
|
| 2031 |
put( '(' ); |
| 2032 |
parseFuncArguments(errStatus); |
| 2033 |
if (errStatus) |
| 2034 |
return dst.bslice_empty; |
| 2035 |
put( ')' ); |
| 2036 |
return attr; |
| 2037 |
} |
| 2038 |
} |
| 2039 |
|
| 2040 |
// not part of a qualified name, so back up |
| 2041 |
pos = prevpos; |
| 2042 |
dst.len = prevlen; |
| 2043 |
brp = prevbrp; |
| 2044 |
} |
| 2045 |
|
| 2046 |
return dst.bslice_empty; |
| 2047 |
} |
| 2048 |
|
| 2049 |
/* |
| 2050 |
QualifiedName: |
| 2051 |
SymbolName |
| 2052 |
SymbolName QualifiedName |
| 2053 |
*/ |
| 2054 |
void parseQualifiedName(out bool errStatus) return scope nothrow |
| 2055 |
{ |
| 2056 |
debug(trace) printf( "parseQualifiedName+\n" ); |
| 2057 |
debug(trace) scope(success) printf( "parseQualifiedName-\n" ); |
| 2058 |
|
| 2059 |
size_t n = 0; |
| 2060 |
bool is_sym_name_front; |
| 2061 |
|
| 2062 |
do |
| 2063 |
{ |
| 2064 |
if ( n++ ) |
| 2065 |
put( '.' ); |
| 2066 |
|
| 2067 |
parseSymbolName(errStatus); |
| 2068 |
if (errStatus) |
| 2069 |
return; |
| 2070 |
|
| 2071 |
parseFunctionTypeNoReturn(); |
| 2072 |
|
| 2073 |
is_sym_name_front = isSymbolNameFront(errStatus); |
| 2074 |
if (errStatus) |
| 2075 |
return; |
| 2076 |
|
| 2077 |
} while ( is_sym_name_front ); |
| 2078 |
} |
| 2079 |
|
| 2080 |
|
| 2081 |
/* |
| 2082 |
MangledName: |
| 2083 |
_D QualifiedName Type |
| 2084 |
_D QualifiedName M Type |
| 2085 |
*/ |
| 2086 |
void parseMangledName( out bool errStatus, bool displayType, size_t n = 0 ) scope nothrow |
| 2087 |
{ |
| 2088 |
debug(trace) printf( "parseMangledName+\n" ); |
| 2089 |
debug(trace) scope(success) printf( "parseMangledName-\n" ); |
| 2090 |
BufSlice name = dst.bslice_empty; |
| 2091 |
|
| 2092 |
auto end = pos + n; |
| 2093 |
|
| 2094 |
eat( '_' ); |
| 2095 |
errStatus = !match( 'D' ); |
| 2096 |
if (errStatus) |
| 2097 |
return; |
| 2098 |
|
| 2099 |
do |
| 2100 |
{ |
| 2101 |
size_t beg = dst.length; |
| 2102 |
size_t nameEnd = dst.length; |
| 2103 |
BufSlice attr = dst.bslice_empty; |
| 2104 |
bool is_sym_name_front; |
| 2105 |
|
| 2106 |
do |
| 2107 |
{ |
| 2108 |
if ( attr.length ) |
| 2109 |
dst.remove(attr); // dump attributes of parent symbols |
| 2110 |
if (beg != dst.length) |
| 2111 |
put( '.' ); |
| 2112 |
|
| 2113 |
parseSymbolName(errStatus); |
| 2114 |
if (errStatus) |
| 2115 |
return; |
| 2116 |
|
| 2117 |
nameEnd = dst.length; |
| 2118 |
attr = parseFunctionTypeNoReturn( displayType ); |
| 2119 |
|
| 2120 |
is_sym_name_front = isSymbolNameFront(errStatus); |
| 2121 |
if (errStatus) |
| 2122 |
return; |
| 2123 |
} while (is_sym_name_front); |
| 2124 |
|
| 2125 |
if ( displayType ) |
| 2126 |
{ |
| 2127 |
attr = shift( attr ); |
| 2128 |
nameEnd = dst.length - attr.length; // name includes function arguments |
| 2129 |
} |
| 2130 |
name = dst[beg .. nameEnd]; |
| 2131 |
|
| 2132 |
debug(info) printf( "name (%.*s)\n", cast(int) name.length, name.getSlice.ptr ); |
| 2133 |
if ( 'M' == front ) |
| 2134 |
popFront(); // has 'this' pointer |
| 2135 |
|
| 2136 |
auto lastlen = dst.length; |
| 2137 |
auto type = parseType(errStatus); |
| 2138 |
if (errStatus) |
| 2139 |
return; |
| 2140 |
|
| 2141 |
if ( displayType ) |
| 2142 |
{ |
| 2143 |
if ( type.length ) |
| 2144 |
put( ' ' ); |
| 2145 |
// sort (name,attr,type) -> (attr,type,name) |
| 2146 |
shift( name ); |
| 2147 |
} |
| 2148 |
else |
| 2149 |
{ |
| 2150 |
// remove type |
| 2151 |
assert( attr.length == 0 ); |
| 2152 |
dst.len = lastlen; |
| 2153 |
} |
| 2154 |
if ( pos >= buf.length || (n != 0 && pos >= end) ) |
| 2155 |
return; |
| 2156 |
|
| 2157 |
switch ( front ) |
| 2158 |
{ |
| 2159 |
case 'T': // terminators when used as template alias parameter |
| 2160 |
case 'V': |
| 2161 |
case 'S': |
| 2162 |
case 'Z': |
| 2163 |
return; |
| 2164 |
default: |
| 2165 |
} |
| 2166 |
put( '.' ); |
| 2167 |
|
| 2168 |
} while ( true ); |
| 2169 |
} |
| 2170 |
|
| 2171 |
void parseMangledName(out bool errStatus) nothrow |
| 2172 |
{ |
| 2173 |
parseMangledName(errStatus, AddType.yes == addType); |
| 2174 |
} |
| 2175 |
|
| 2176 |
char[] doDemangle(alias FUNC)() return scope nothrow |
| 2177 |
{ |
| 2178 |
while ( true ) |
| 2179 |
{ |
| 2180 |
debug(info) printf( "demangle(%.*s)\n", cast(int) buf.length, buf.ptr ); |
| 2181 |
|
| 2182 |
bool errStatus; |
| 2183 |
FUNC(errStatus); |
| 2184 |
if (!errStatus) |
| 2185 |
{ |
| 2186 |
return dst[0 .. $].getSlice; |
| 2187 |
} |
| 2188 |
else |
| 2189 |
{ |
| 2190 |
debug(info) printf( "error" ); |
| 2191 |
|
| 2192 |
return dst.copyInput(buf); |
| 2193 |
} |
| 2194 |
} |
| 2195 |
} |
| 2196 |
|
| 2197 |
char[] demangleName() nothrow |
| 2198 |
{ |
| 2199 |
return doDemangle!parseMangledName(); |
| 2200 |
} |
| 2201 |
|
| 2202 |
char[] demangleType() nothrow |
| 2203 |
{ |
| 2204 |
return doDemangle!parseType(); |
| 2205 |
} |
| 2206 |
} |
| 2207 |
|
| 2208 |
|
| 2209 |
/** |
| 2210 |
* Demangles D/C++ mangled names. If it is not a D/C++ mangled name, it |
| 2211 |
* returns its argument name. |
| 2212 |
* |
| 2213 |
* Params: |
| 2214 |
* buf = The string to demangle. |
| 2215 |
* dst = An optional destination buffer. |
| 2216 |
* __cxa_demangle = optional C++ demangler |
| 2217 |
* |
| 2218 |
* Returns: |
| 2219 |
* The demangled name or the original string if the name is not a mangled |
| 2220 |
* D/C++ name. |
| 2221 |
*/ |
| 2222 |
char[] demangle(return scope const(char)[] buf, return scope char[] dst = null, CXX_DEMANGLER __cxa_demangle = null) nothrow pure @safe |
| 2223 |
{ |
| 2224 |
if (__cxa_demangle && buf.length > 2 && buf[0..2] == "_Z") |
| 2225 |
return demangleCXX(buf, __cxa_demangle, dst); |
| 2226 |
auto d = Demangle!()(buf, dst); |
| 2227 |
// fast path (avoiding throwing & catching exception) for obvious |
| 2228 |
// non-D mangled names |
| 2229 |
if (buf.length < 2 || !(buf[0] == 'D' || buf[0..2] == "_D")) |
| 2230 |
return d.dst.copyInput(buf); |
| 2231 |
return d.demangleName(); |
| 2232 |
} |
| 2233 |
|
| 2234 |
|
| 2235 |
/** |
| 2236 |
* Demangles a D mangled type. |
| 2237 |
* |
| 2238 |
* Params: |
| 2239 |
* buf = The string to demangle. |
| 2240 |
* dst = An optional destination buffer. |
| 2241 |
* |
| 2242 |
* Returns: |
| 2243 |
* The demangled type name or the original string if the name is not a |
| 2244 |
* mangled D type. |
| 2245 |
*/ |
| 2246 |
char[] demangleType( const(char)[] buf, char[] dst = null ) nothrow pure @safe |
| 2247 |
{ |
| 2248 |
auto d = Demangle!()(buf, dst); |
| 2249 |
return d.demangleType(); |
| 2250 |
} |
| 2251 |
|
| 2252 |
/** |
| 2253 |
* reencode a mangled symbol name that might include duplicate occurrences |
| 2254 |
* of the same identifier by replacing all but the first occurence with |
| 2255 |
* a back reference. |
| 2256 |
* |
| 2257 |
* Params: |
| 2258 |
* mangled = The mangled string representing the type |
| 2259 |
* |
| 2260 |
* Returns: |
| 2261 |
* The mangled name with deduplicated identifiers |
| 2262 |
*/ |
| 2263 |
char[] reencodeMangled(return scope const(char)[] mangled) nothrow pure @safe |
| 2264 |
{ |
| 2265 |
static struct PrependHooks |
| 2266 |
{ |
| 2267 |
size_t lastpos; |
| 2268 |
char[] result; |
| 2269 |
size_t[const(char)[]] idpos; // identifier positions |
| 2270 |
|
| 2271 |
static struct Replacement |
| 2272 |
{ |
| 2273 |
size_t pos; // postion in original mangled string |
| 2274 |
size_t respos; // postion in result string |
| 2275 |
} |
| 2276 |
Replacement [] replacements; |
| 2277 |
|
| 2278 |
pure @safe: |
| 2279 |
size_t positionInResult(size_t pos) scope |
| 2280 |
{ |
| 2281 |
foreach_reverse (r; replacements) |
| 2282 |
if (pos >= r.pos) |
| 2283 |
return r.respos + pos - r.pos; |
| 2284 |
return pos; |
| 2285 |
} |
| 2286 |
|
| 2287 |
alias Remangle = Demangle!(PrependHooks); |
| 2288 |
|
| 2289 |
void flushPosition(ref Remangle d) scope |
| 2290 |
{ |
| 2291 |
if (lastpos < d.pos) |
| 2292 |
{ |
| 2293 |
result ~= d.buf[lastpos .. d.pos]; |
| 2294 |
} |
| 2295 |
else if (lastpos > d.pos) |
| 2296 |
{ |
| 2297 |
// roll back to earlier position |
| 2298 |
while (replacements.length > 0 && replacements[$-1].pos > d.pos) |
| 2299 |
replacements = replacements[0 .. $-1]; |
| 2300 |
|
| 2301 |
if (replacements.length > 0) |
| 2302 |
result.length = replacements[$-1].respos + d.pos - replacements[$-1].pos; |
| 2303 |
else |
| 2304 |
result.length = d.pos; |
| 2305 |
} |
| 2306 |
} |
| 2307 |
|
| 2308 |
bool parseLName(out string errMsg, scope ref Remangle d) scope @trusted nothrow |
| 2309 |
{ |
| 2310 |
bool error(string msg) |
| 2311 |
{ |
| 2312 |
errMsg = msg; |
| 2313 |
return false; |
| 2314 |
} |
| 2315 |
flushPosition(d); |
| 2316 |
|
| 2317 |
auto reslen = result.length; |
| 2318 |
auto refpos = d.pos; |
| 2319 |
if (d.front == 'Q') |
| 2320 |
{ |
| 2321 |
size_t npos; |
| 2322 |
{ |
| 2323 |
scope(exit) result.length = reslen; // remove all intermediate additions |
| 2324 |
// only support identifier back references |
| 2325 |
d.popFront(); |
| 2326 |
size_t n = d.decodeBackref(); |
| 2327 |
if (!n || n > refpos) |
| 2328 |
return error("invalid back reference"); |
| 2329 |
|
| 2330 |
auto savepos = d.pos; |
| 2331 |
scope(exit) d.pos = savepos; |
| 2332 |
size_t srcpos = refpos - n; |
| 2333 |
|
| 2334 |
bool errStatus; |
| 2335 |
auto idlen = d.decodeNumber(errStatus); |
| 2336 |
if (errStatus) |
| 2337 |
return error("invalid number"); |
| 2338 |
|
| 2339 |
if (d.pos + idlen > d.buf.length) |
| 2340 |
return error("invalid back reference"); |
| 2341 |
|
| 2342 |
auto id = d.buf[d.pos .. d.pos + idlen]; |
| 2343 |
auto pid = id in idpos; |
| 2344 |
if (!pid) |
| 2345 |
return error("invalid back reference"); |
| 2346 |
|
| 2347 |
npos = positionInResult(*pid); |
| 2348 |
} |
| 2349 |
encodeBackref(reslen - npos); |
| 2350 |
const pos = d.pos; // work around issues.dlang.org/show_bug.cgi?id=20675 |
| 2351 |
replacements ~= Replacement(pos, result.length); |
| 2352 |
} |
| 2353 |
else |
| 2354 |
{ |
| 2355 |
bool errStatus; |
| 2356 |
auto n = d.decodeNumber(errStatus); |
| 2357 |
if (errStatus) |
| 2358 |
return error("invalid number"); |
| 2359 |
|
| 2360 |
if (!n || n > d.buf.length || n > d.buf.length - d.pos) |
| 2361 |
return error("LName too short or too long"); |
| 2362 |
|
| 2363 |
auto id = d.buf[d.pos .. d.pos + n]; |
| 2364 |
d.pos += n; |
| 2365 |
if (auto pid = id in idpos) |
| 2366 |
{ |
| 2367 |
size_t npos = positionInResult(*pid); |
| 2368 |
result.length = reslen; |
| 2369 |
encodeBackref(reslen - npos); |
| 2370 |
const pos = d.pos; // work around issues.dlang.org/show_bug.cgi?id=20675 |
| 2371 |
replacements ~= Replacement(pos, result.length); |
| 2372 |
} |
| 2373 |
else |
| 2374 |
{ |
| 2375 |
idpos[id] = refpos; //! scope variable id used as AA key, makes this function @trusted |
| 2376 |
result ~= d.buf[refpos .. d.pos]; |
| 2377 |
} |
| 2378 |
} |
| 2379 |
lastpos = d.pos; |
| 2380 |
return true; |
| 2381 |
} |
| 2382 |
|
| 2383 |
char[] parseType( out bool errStatus, ref Remangle d, char[] name ) return scope nothrow |
| 2384 |
{ |
| 2385 |
if (d.front != 'Q') |
| 2386 |
return null; |
| 2387 |
|
| 2388 |
flushPosition(d); |
| 2389 |
|
| 2390 |
auto refPos = d.pos; |
| 2391 |
d.popFront(); |
| 2392 |
auto n = d.decodeBackref(); |
| 2393 |
if (n == 0 || n > refPos) |
| 2394 |
{ |
| 2395 |
// invalid back reference |
| 2396 |
errStatus = true; |
| 2397 |
return null; |
| 2398 |
} |
| 2399 |
|
| 2400 |
size_t npos = positionInResult(refPos - n); |
| 2401 |
size_t reslen = result.length; |
| 2402 |
encodeBackref(reslen - npos); |
| 2403 |
|
| 2404 |
lastpos = d.pos; |
| 2405 |
return result[reslen .. $]; // anything but null |
| 2406 |
} |
| 2407 |
|
| 2408 |
void encodeBackref(size_t relpos) scope |
| 2409 |
{ |
| 2410 |
result ~= 'Q'; |
| 2411 |
enum base = 26; |
| 2412 |
size_t div = 1; |
| 2413 |
while (relpos >= div * base) |
| 2414 |
div *= base; |
| 2415 |
while (div >= base) |
| 2416 |
{ |
| 2417 |
auto dig = (relpos / div); |
| 2418 |
result ~= cast(char)('A' + dig); |
| 2419 |
relpos -= dig * div; |
| 2420 |
div /= base; |
| 2421 |
} |
| 2422 |
result ~= cast(char)('a' + relpos); |
| 2423 |
} |
| 2424 |
} |
| 2425 |
|
| 2426 |
auto d = Demangle!(PrependHooks)(mangled, null); |
| 2427 |
d.hooks = PrependHooks(); |
| 2428 |
d.mute = true; // no demangled output |
| 2429 |
|
| 2430 |
bool errStatus; |
| 2431 |
d.parseMangledName(errStatus); |
| 2432 |
if (errStatus) |
| 2433 |
{ |
| 2434 |
// error cannot occur |
| 2435 |
return mangled.dup; |
| 2436 |
} |
| 2437 |
|
| 2438 |
if (d.hooks.lastpos < d.pos) |
| 2439 |
d.hooks.result ~= d.buf[d.hooks.lastpos .. d.pos]; |
| 2440 |
return d.hooks.result; |
| 2441 |
} |
| 2442 |
|
| 2443 |
/** |
| 2444 |
* Mangles a D symbol. |
| 2445 |
* |
| 2446 |
* Params: |
| 2447 |
* T = The type of the symbol. |
| 2448 |
* fqn = The fully qualified name of the symbol. |
| 2449 |
* dst = An optional destination buffer. |
| 2450 |
* |
| 2451 |
* Returns: |
| 2452 |
* The mangled name for a symbols of type T and the given fully |
| 2453 |
* qualified name. |
| 2454 |
*/ |
| 2455 |
char[] mangle(T)(return scope const(char)[] fqn, return scope char[] dst = null) @safe pure nothrow |
| 2456 |
{ |
| 2457 |
import core.internal.string : numDigits, unsignedToTempString; |
| 2458 |
|
| 2459 |
static struct DotSplitter |
| 2460 |
{ |
| 2461 |
@safe pure nothrow: |
| 2462 |
const(char)[] s; |
| 2463 |
|
| 2464 |
@property bool empty() const { return !s.length; } |
| 2465 |
|
| 2466 |
@property const(char)[] front() const return scope |
| 2467 |
{ |
| 2468 |
immutable i = indexOfDot(); |
| 2469 |
return i == -1 ? s[0 .. $] : s[0 .. i]; |
| 2470 |
} |
| 2471 |
|
| 2472 |
void popFront() scope |
| 2473 |
{ |
| 2474 |
immutable i = indexOfDot(); |
| 2475 |
s = i == -1 ? s[$ .. $] : s[i+1 .. $]; |
| 2476 |
} |
| 2477 |
|
| 2478 |
private ptrdiff_t indexOfDot() const scope |
| 2479 |
{ |
| 2480 |
foreach (i, c; s) if (c == '.') return i; |
| 2481 |
return -1; |
| 2482 |
} |
| 2483 |
} |
| 2484 |
|
| 2485 |
size_t len = "_D".length; |
| 2486 |
foreach (comp; DotSplitter(fqn)) |
| 2487 |
len += numDigits(comp.length) + comp.length; |
| 2488 |
len += T.mangleof.length; |
| 2489 |
if (dst.length < len) dst.length = len; |
| 2490 |
|
| 2491 |
size_t i = "_D".length; |
| 2492 |
dst[0 .. i] = "_D"; |
| 2493 |
foreach (comp; DotSplitter(fqn)) |
| 2494 |
{ |
| 2495 |
const ndigits = numDigits(comp.length); |
| 2496 |
unsignedToTempString(comp.length, dst[i .. i + ndigits]); |
| 2497 |
i += ndigits; |
| 2498 |
dst[i .. i + comp.length] = comp[]; |
| 2499 |
i += comp.length; |
| 2500 |
} |
| 2501 |
dst[i .. i + T.mangleof.length] = T.mangleof[]; |
| 2502 |
i += T.mangleof.length; |
| 2503 |
|
| 2504 |
static if (hasTypeBackRef) |
| 2505 |
return reencodeMangled(dst[0 .. i]); |
| 2506 |
else |
| 2507 |
return dst[0 .. i]; |
| 2508 |
} |
| 2509 |
|
| 2510 |
|
| 2511 |
/// |
| 2512 |
@safe pure nothrow unittest |
| 2513 |
{ |
| 2514 |
assert(mangle!int("a.b") == "_D1a1bi"); |
| 2515 |
assert(mangle!(char[])("test.foo") == "_D4test3fooAa"); |
| 2516 |
assert(mangle!(int function(int))("a.b") == "_D1a1bPFiZi"); |
| 2517 |
} |
| 2518 |
|
| 2519 |
@safe pure nothrow unittest |
| 2520 |
{ |
| 2521 |
static assert(mangle!int("a.b") == "_D1a1bi"); |
| 2522 |
|
| 2523 |
auto buf = new char[](10); |
| 2524 |
buf = mangle!int("a.b", buf); |
| 2525 |
assert(buf == "_D1a1bi"); |
| 2526 |
buf = mangle!(char[])("test.foo", buf); |
| 2527 |
assert(buf == "_D4test3fooAa"); |
| 2528 |
buf = mangle!(real delegate(int))("modµ.dg"); |
| 2529 |
assert(buf == "_D5modµ2dgDFiZe", buf); |
| 2530 |
} |
| 2531 |
|
| 2532 |
|
| 2533 |
/** |
| 2534 |
* Mangles a D function. |
| 2535 |
* |
| 2536 |
* Params: |
| 2537 |
* T = function pointer type. |
| 2538 |
* fqn = The fully qualified name of the symbol. |
| 2539 |
* dst = An optional destination buffer. |
| 2540 |
* |
| 2541 |
* Returns: |
| 2542 |
* The mangled name for a function with function pointer type T and |
| 2543 |
* the given fully qualified name. |
| 2544 |
*/ |
| 2545 |
char[] mangleFunc(T:FT*, FT)(return scope const(char)[] fqn, return scope char[] dst = null) @safe pure nothrow if (is(FT == function)) |
| 2546 |
{ |
| 2547 |
static if (isExternD!FT) |
| 2548 |
{ |
| 2549 |
return mangle!FT(fqn, dst); |
| 2550 |
} |
| 2551 |
else static if (hasPlainMangling!FT) |
| 2552 |
{ |
| 2553 |
dst.length = fqn.length; |
| 2554 |
dst[] = fqn[]; |
| 2555 |
return dst; |
| 2556 |
} |
| 2557 |
else static if (isExternCPP!FT) |
| 2558 |
{ |
| 2559 |
static assert(0, "Can't mangle extern(C++) functions."); |
| 2560 |
} |
| 2561 |
else |
| 2562 |
{ |
| 2563 |
static assert(0, "Can't mangle function with unknown linkage ("~FT.stringof~")."); |
| 2564 |
} |
| 2565 |
} |
| 2566 |
|
| 2567 |
private enum hasTypeBackRef = (int function(void**,void**)).mangleof[$-4 .. $] == "QdZi"; |
| 2568 |
|
| 2569 |
@safe pure nothrow unittest |
| 2570 |
{ |
| 2571 |
assert(mangleFunc!(int function(int))("a.b") == "_D1a1bFiZi"); |
| 2572 |
static if (hasTypeBackRef) |
| 2573 |
{ |
| 2574 |
assert(mangleFunc!(int function(Object))("object.Object.opEquals") == "_D6object6Object8opEqualsFCQsZi"); |
| 2575 |
assert(mangleFunc!(int function(Object, Object))("object.Object.opEquals") == "_D6object6Object8opEqualsFCQsQdZi"); |
| 2576 |
} |
| 2577 |
else |
| 2578 |
{ |
| 2579 |
auto mngl = mangleFunc!(int function(Object))("object.Object.opEquals"); |
| 2580 |
assert(mngl == "_D6object6Object8opEqualsFC6ObjectZi"); |
| 2581 |
auto remngl = reencodeMangled(mngl); |
| 2582 |
assert(remngl == "_D6object6Object8opEqualsFCQsZi"); |
| 2583 |
} |
| 2584 |
// trigger back tracking with ambiguity on '__T', template or identifier |
| 2585 |
assert(reencodeMangled("_D3std4conv4conv7__T3std4convi") == "_D3std4convQf7__T3stdQpi"); |
| 2586 |
} |
| 2587 |
|
| 2588 |
@safe pure nothrow unittest |
| 2589 |
{ |
| 2590 |
int function(lazy int[], ...) fp; |
| 2591 |
assert(mangle!(typeof(fp))("demangle.test") == "_D8demangle4testPFLAiYi"); |
| 2592 |
assert(mangle!(typeof(*fp))("demangle.test") == "_D8demangle4testFLAiYi"); |
| 2593 |
} |
| 2594 |
|
| 2595 |
private template isExternD(FT) if (is(FT == function)) |
| 2596 |
{ |
| 2597 |
enum isExternD = __traits(getLinkage, FT) == "D"; |
| 2598 |
} |
| 2599 |
|
| 2600 |
private template isExternCPP(FT) if (is(FT == function)) |
| 2601 |
{ |
| 2602 |
enum isExternCPP = __traits(getLinkage, FT) == "C++"; |
| 2603 |
} |
| 2604 |
|
| 2605 |
private template hasPlainMangling(FT) if (is(FT == function)) |
| 2606 |
{ |
| 2607 |
enum lnk = __traits(getLinkage, FT); |
| 2608 |
// C || Windows |
| 2609 |
enum hasPlainMangling = lnk == "C" || lnk == "Windows" || lnk == "System"; |
| 2610 |
} |
| 2611 |
|
| 2612 |
@safe pure nothrow unittest |
| 2613 |
{ |
| 2614 |
static extern(D) void fooD(); |
| 2615 |
static extern(C) void fooC(); |
| 2616 |
static extern(Windows) void fooW(); |
| 2617 |
static extern(C++) void fooCPP(); |
| 2618 |
|
| 2619 |
bool check(FT)(bool isD, bool isCPP, bool isPlain) |
| 2620 |
{ |
| 2621 |
return isExternD!FT == isD && isExternCPP!FT == isCPP && |
| 2622 |
hasPlainMangling!FT == isPlain; |
| 2623 |
} |
| 2624 |
static assert(check!(typeof(fooD))(true, false, false)); |
| 2625 |
static assert(check!(typeof(fooC))(false, false, true)); |
| 2626 |
static assert(check!(typeof(fooW))(false, false, true)); |
| 2627 |
static assert(check!(typeof(fooCPP))(false, true, false)); |
| 2628 |
|
| 2629 |
static assert(__traits(compiles, mangleFunc!(typeof(&fooD))(""))); |
| 2630 |
static assert(__traits(compiles, mangleFunc!(typeof(&fooC))(""))); |
| 2631 |
static assert(__traits(compiles, mangleFunc!(typeof(&fooW))(""))); |
| 2632 |
static assert(!__traits(compiles, mangleFunc!(typeof(&fooCPP))(""))); |
| 2633 |
} |
| 2634 |
|
| 2635 |
/*** |
| 2636 |
* C name mangling is done by adding a prefix on some platforms. |
| 2637 |
*/ |
| 2638 |
version (Win32) |
| 2639 |
enum string cPrefix = "_"; |
| 2640 |
else version (Darwin) |
| 2641 |
enum string cPrefix = "_"; |
| 2642 |
else |
| 2643 |
enum string cPrefix = ""; |
| 2644 |
|
| 2645 |
@safe pure nothrow unittest |
| 2646 |
{ |
| 2647 |
immutable string[2][] table = |
| 2648 |
[ |
| 2649 |
["printf", "printf"], |
| 2650 |
["_foo", "_foo"], |
| 2651 |
["_D88", "_D88"], |
| 2652 |
["_D3fooQeFIAyaZv", "void foo.foo(in immutable(char)[])" ], |
| 2653 |
["_D3barQeFIKAyaZv", "void bar.bar(in ref immutable(char)[])" ], |
| 2654 |
["_D4test3fooAa", "char[] test.foo"], |
| 2655 |
["_D8demangle8demangleFAaZAa", "char[] demangle.demangle(char[])"], |
| 2656 |
["_D6object6Object8opEqualsFC6ObjectZi", "int object.Object.opEquals(Object)"], |
| 2657 |
["_D4test2dgDFiYd", "double delegate(int, ...) test.dg"], |
| 2658 |
["_D4test2dgDxFNfiYd", "double delegate(int, ...) @safe const test.dg"], |
| 2659 |
//["_D4test58__T9factorialVde67666666666666860140VG5aa5_68656c6c6fVPvnZ9factorialf", ""], |
| 2660 |
//["_D4test101__T9factorialVde67666666666666860140Vrc9a999999999999d9014000000000000000c00040VG5aa5_68656c6c6fVPvnZ9factorialf", ""], |
| 2661 |
["_D4test34__T3barVG3uw3_616263VG3wd3_646566Z1xi", "int test.bar!(\"abc\"w, \"def\"d).x"], |
| 2662 |
["_D8demangle4testFLC6ObjectLDFLiZiZi", "int demangle.test(lazy Object, lazy int delegate(lazy int))"], |
| 2663 |
["_D8demangle4testFAiXi", "int demangle.test(int[]...)"], |
| 2664 |
["_D8demangle4testFAiYi", "int demangle.test(int[], ...)"], |
| 2665 |
["_D8demangle4testFLAiXi", "int demangle.test(lazy int[]...)"], |
| 2666 |
["_D8demangle4testFLAiYi", "int demangle.test(lazy int[], ...)"], |
| 2667 |
["_D6plugin8generateFiiZAya", "immutable(char)[] plugin.generate(int, int)"], |
| 2668 |
["_D6plugin8generateFiiZAxa", "const(char)[] plugin.generate(int, int)"], |
| 2669 |
["_D6plugin8generateFiiZAOa", "shared(char)[] plugin.generate(int, int)"], |
| 2670 |
["_D8demangle3fnAFZ3fnBMFZv", "void demangle.fnA().fnB()"], |
| 2671 |
["_D8demangle4mainFZ1S3fnCMFZv", "void demangle.main().S.fnC()"], |
| 2672 |
["_D8demangle4mainFZ1S3fnDMFZv", "void demangle.main().S.fnD()"], |
| 2673 |
["_D8demangle20__T2fnVAiA4i1i2i3i4Z2fnFZv", "void demangle.fn!([1, 2, 3, 4]).fn()"], |
| 2674 |
["_D8demangle10__T2fnVi1Z2fnFZv", "void demangle.fn!(1).fn()"], |
| 2675 |
["_D8demangle26__T2fnVS8demangle1SS2i1i2Z2fnFZv", "void demangle.fn!(demangle.S(1, 2)).fn()"], |
| 2676 |
["_D8demangle13__T2fnVeeNANZ2fnFZv", "void demangle.fn!(real.nan).fn()"], |
| 2677 |
["_D8demangle14__T2fnVeeNINFZ2fnFZv", "void demangle.fn!(-real.infinity).fn()"], |
| 2678 |
["_D8demangle13__T2fnVeeINFZ2fnFZv", "void demangle.fn!(real.infinity).fn()"], |
| 2679 |
["_D8demangle21__T2fnVHiiA2i1i2i3i4Z2fnFZv", "void demangle.fn!([1:2, 3:4]).fn()"], |
| 2680 |
["_D8demangle2fnFNgiZNgi", "inout(int) demangle.fn(inout(int))"], |
| 2681 |
["_D8demangle29__T2fnVa97Va9Va0Vu257Vw65537Z2fnFZv", "void demangle.fn!('a', '\\t', \\x00, '\\u0101', '\\U00010001').fn()"], |
| 2682 |
["_D2gc11gctemplates56__T8mkBitmapTS3std5range13__T4iotaTiTiZ4iotaFiiZ6ResultZ8mkBitmapFNbNiNfPmmZv", |
| 2683 |
"nothrow @nogc @safe void gc.gctemplates.mkBitmap!(std.range.iota!(int, int).iota(int, int).Result).mkBitmap(ulong*, ulong)"], |
| 2684 |
["_D8serenity9persister6Sqlite69__T15SqlitePersisterTS8serenity9persister6Sqlite11__unittest6FZ4TestZ15SqlitePersister12__T7opIndexZ7opIndexMFmZS8serenity9persister6Sqlite11__unittest6FZ4Test", |
| 2685 |
"serenity.persister.Sqlite.__unittest6().Test serenity.persister.Sqlite.SqlitePersister!(serenity.persister.Sqlite.__unittest6().Test).SqlitePersister.opIndex!().opIndex(ulong)"], |
| 2686 |
["_D8bug100274mainFZ5localMFZi","int bug10027.main().local()"], |
| 2687 |
["_D8demangle4testFNhG16gZv", "void demangle.test(__vector(byte[16]))"], |
| 2688 |
["_D8demangle4testFNhG8sZv", "void demangle.test(__vector(short[8]))"], |
| 2689 |
["_D8demangle4testFNhG4iZv", "void demangle.test(__vector(int[4]))"], |
| 2690 |
["_D8demangle4testFNhG2lZv", "void demangle.test(__vector(long[2]))"], |
| 2691 |
["_D8demangle4testFNhG4fZv", "void demangle.test(__vector(float[4]))"], |
| 2692 |
["_D8demangle4testFNhG2dZv", "void demangle.test(__vector(double[2]))"], |
| 2693 |
["_D8demangle4testFNhG4fNhG4fZv", "void demangle.test(__vector(float[4]), __vector(float[4]))"], |
| 2694 |
["_D8bug1119234__T3fooS23_D8bug111924mainFZ3bariZ3fooMFZv","void bug11192.foo!(bug11192.main().bar).foo()"], |
| 2695 |
["_D13libd_demangle12__ModuleInfoZ", "libd_demangle.__ModuleInfo"], |
| 2696 |
["_D15TypeInfo_Struct6__vtblZ", "TypeInfo_Struct.__vtbl"], |
| 2697 |
["_D3std5stdio12__ModuleInfoZ", "std.stdio.__ModuleInfo"], |
| 2698 |
["_D3std6traits15__T8DemangleTkZ8Demangle6__initZ", "std.traits.Demangle!(uint).Demangle.__init"], |
| 2699 |
["_D3foo3Bar7__ClassZ", "foo.Bar.__Class"], |
| 2700 |
["_D3foo3Bar6__vtblZ", "foo.Bar.__vtbl"], |
| 2701 |
["_D3foo3Bar11__interfaceZ", "foo.Bar.__interface"], |
| 2702 |
["_D3foo7__arrayZ", "foo.__array"], |
| 2703 |
["_D8link657428__T3fooVE8link65746Methodi0Z3fooFZi", "int link6574.foo!(0).foo()"], |
| 2704 |
["_D8link657429__T3fooHVE8link65746Methodi0Z3fooFZi", "int link6574.foo!(0).foo()"], |
| 2705 |
["_D4test22__T4funcVAyaa3_610a62Z4funcFNaNbNiNmNfZAya", `pure nothrow @nogc @live @safe immutable(char)[] test.func!("a\x0ab").func()`], |
| 2706 |
["_D3foo3barFzkZzi", "cent foo.bar(ucent)"], |
| 2707 |
["_D5bug145Class3fooMFNlZPv", "scope void* bug14.Class.foo()"], |
| 2708 |
["_D5bug145Class3barMFNjZPv", "return void* bug14.Class.bar()"], |
| 2709 |
["_D5bug143fooFMPvZPv", "void* bug14.foo(scope void*)"], |
| 2710 |
["_D5bug143barFMNkPvZPv", "void* bug14.bar(scope return void*)"], |
| 2711 |
["_D3std5range15__T4iotaTtTtTtZ4iotaFtttZ6Result7opIndexMNgFNaNbNiNfmZNgt", |
| 2712 |
"inout pure nothrow @nogc @safe inout(ushort) std.range.iota!(ushort, ushort, ushort).iota(ushort, ushort, ushort).Result.opIndex(ulong)"], |
| 2713 |
["_D3std6format77__T6getNthVAyaa13_696e7465676572207769647468S233std6traits10isIntegralTiTkTkZ6getNthFNaNfkkkZi", |
| 2714 |
"pure @safe int std.format.getNth!(\"integer width\", std.traits.isIntegral, int, uint, uint).getNth(uint, uint, uint)"], |
| 2715 |
["_D3std11parallelism42__T16RoundRobinBufferTDFKAaZvTDxFNaNdNeZbZ16RoundRobinBuffer5primeMFZv", |
| 2716 |
"void std.parallelism.RoundRobinBuffer!(void delegate(ref char[]), bool delegate() pure @property @trusted const).RoundRobinBuffer.prime()"], |
| 2717 |
["_D6mangle__T8fun21753VSQv6S21753S1f_DQBj10__lambda71MFNaNbNiNfZvZQCbQp", |
| 2718 |
"void function() pure nothrow @nogc @safe mangle.fun21753!(mangle.S21753(mangle.__lambda71())).fun21753"], |
| 2719 |
// Lname '0' |
| 2720 |
["_D3std9algorithm9iteration__T9MapResultSQBmQBlQBe005stripTAAyaZQBi7opSliceMFNaNbNiNfmmZSQDiQDhQDa__TQCtSQDyQDxQDq00QCmTQCjZQDq", |
| 2721 |
"pure nothrow @nogc @safe std.algorithm.iteration.MapResult!(std.algorithm.iteration.__anonymous.strip, " |
| 2722 |
~"immutable(char)[][]).MapResult std.algorithm.iteration.MapResult!(std.algorithm.iteration.strip, immutable(char)[][]).MapResult.opSlice(ulong, ulong)"], |
| 2723 |
|
| 2724 |
// back references |
| 2725 |
["_D4core4stdc5errnoQgFZi", "int core.stdc.errno.errno()"], // identifier back reference |
| 2726 |
["_D4testFS10structnameQnZb", "bool test(structname, structname)"], // type back reference |
| 2727 |
["_D3std11parallelism__T4TaskS8unittest3cmpTAyaTQeZQBb6__dtorMFNfZv", |
| 2728 |
"@safe void std.parallelism.Task!(unittest.cmp, immutable(char)[], immutable(char)[]).Task.__dtor()"], |
| 2729 |
// 1.s.s.foo from https://issues.dlang.org/show_bug.cgi?id=15831 |
| 2730 |
["_D13testexpansion44__T1sTS13testexpansion8__T1sTiZ1sFiZ6ResultZ1sFS13testexpansion8__T1sTiZ1sFiZ6ResultZ6Result3fooMFNaNfZv", |
| 2731 |
"pure @safe void testexpansion.s!(testexpansion.s!(int).s(int).Result).s(testexpansion.s!(int).s(int).Result).Result.foo()"], |
| 2732 |
["_D13testexpansion__T1sTSQw__TQjTiZQoFiZ6ResultZQBbFQBcZQq3fooMFNaNfZv", |
| 2733 |
"pure @safe void testexpansion.s!(testexpansion.s!(int).s(int).Result).s(testexpansion.s!(int).s(int).Result).Result.foo()"], |
| 2734 |
// formerly ambiguous on 'V', template value argument or pascal function |
| 2735 |
// pascal functions have now been removed (in v2.095.0) |
| 2736 |
["_D3std4conv__T7enumRepTyAaTEQBa12experimental9allocator15building_blocks15stats_collector7OptionsVQCti64ZQDnyQDh", |
| 2737 |
"immutable(char[]) std.conv.enumRep!(immutable(char[]), std.experimental.allocator.building_blocks.stats_collector.Options, 64).enumRep"], |
| 2738 |
// symbol back reference to location with symbol back reference |
| 2739 |
["_D3std12experimental9allocator6common__T10reallocateTSQCaQBzQBo15building_blocks17kernighan_ritchie__T8KRRegionTSQEhQEgQDvQCh14null_allocator13NullAllocatorZQCdZQErFNaNbNiKQEpKAvmZb", |
| 2740 |
"pure nothrow @nogc bool std.experimental.allocator.common.reallocate!(std.experimental.allocator.building_blocks.kernighan_ritchie.KRRegion!(" |
| 2741 |
~"std.experimental.allocator.building_blocks.null_allocator.NullAllocator).KRRegion).reallocate(ref " |
| 2742 |
~"std.experimental.allocator.building_blocks.kernighan_ritchie.KRRegion!(std.experimental.allocator.building_blocks.null_allocator.NullAllocator).KRRegion, ref void[], ulong)"], |
| 2743 |
["_D3std9exception__T11doesPointToTASQBh5regex8internal2ir10NamedGroupTQBkTvZQCeFNaNbNiNeKxASQDlQCeQCbQBvQBvKxQtZb", |
| 2744 |
"pure nothrow @nogc @trusted bool std.exception.doesPointTo!(std.regex.internal.ir.NamedGroup[], " |
| 2745 |
~"std.regex.internal.ir.NamedGroup[], void).doesPointTo(ref const(std.regex.internal.ir.NamedGroup[]), ref const(std.regex.internal.ir.NamedGroup[]))"], |
| 2746 |
["_D3std9algorithm9iteration__T14SplitterResultS_DQBu3uni7isWhiteFNaNbNiNfwZbTAyaZQBz9__xtoHashFNbNeKxSQDvQDuQDn__TQDgS_DQEnQCtQCsQCnTQCeZQEdZm", |
| 2747 |
"nothrow @trusted ulong std.algorithm.iteration.SplitterResult!(std.uni.isWhite(dchar), immutable(char)[]).SplitterResult." |
| 2748 |
~"__xtoHash(ref const(std.algorithm.iteration.SplitterResult!(std.uni.isWhite, immutable(char)[]).SplitterResult))"], |
| 2749 |
["_D3std8typecons__T7TypedefTCQBaQz19__unittestL6513_208FNfZ7MyClassVQBonVAyanZQCh6__ctorMFNaNbNcNiNfQCuZSQDyQDx__TQDrTQDmVQDqnVQCcnZQEj", |
| 2750 |
"pure nothrow ref @nogc @safe std.typecons.Typedef!(std.typecons.__unittestL6513_208().MyClass, null, null).Typedef " |
| 2751 |
~"std.typecons.Typedef!(std.typecons.__unittestL6513_208().MyClass, null, null).Typedef.__ctor(std.typecons.__unittestL6513_208().MyClass)"], |
| 2752 |
["_D3std6getopt__TQkTAyaTDFNaNbNiNfQoZvTQtTDQsZQBnFNfKAQBiQBlQBkQBrQyZSQCpQCo12GetoptResult", |
| 2753 |
"@safe std.getopt.GetoptResult std.getopt.getopt!(immutable(char)[], void delegate(immutable(char)[]) pure nothrow @nogc @safe, " |
| 2754 |
~"immutable(char)[], void delegate(immutable(char)[]) pure nothrow @nogc @safe)." |
| 2755 |
~"getopt(ref immutable(char)[][], immutable(char)[], void delegate(immutable(char)[]) pure nothrow @nogc @safe, " |
| 2756 |
~"immutable(char)[], void delegate(immutable(char)[]) pure nothrow @nogc @safe)"], |
| 2757 |
["_D3std5regex8internal9kickstart__T7ShiftOrTaZQl11ShiftThread__T3setS_DQCqQCpQCmQCg__TQBzTaZQCfQBv10setInvMaskMFNaNbNiNfkkZvZQCjMFNaNfwZv", |
| 2758 |
"pure @safe void std.regex.internal.kickstart.ShiftOr!(char).ShiftOr.ShiftThread.set!(std.regex.internal.kickstart.ShiftOr!(char).ShiftOr.ShiftThread.setInvMask(uint, uint)).set(dchar)"], |
| 2759 |
["_D3std5stdio4File__T8lockImplX10LockFileExTykZQBaMFmmykZi", // C function as template alias parameter |
| 2760 |
"int std.stdio.File.lockImpl!(LockFileEx, immutable(uint)).lockImpl(ulong, ulong, immutable(uint))"], |
| 2761 |
// back reference for type in template AA parameter value |
| 2762 |
["_D3std9algorithm9iteration__T12FilterResultSQBq8typecons__T5TupleTiVAyaa1_61TiVQla1_62TiVQva1_63ZQBm__T6renameVHiQBtA2i0a1_63i2a1_61ZQBeMFNcZ9__lambda1TAiZQEw9__xtoHashFNbNeKxSQGsQGrQGk__TQGdSQHiQFs__TQFmTiVQFja1_61TiVQFua1_62TiVQGfa1_63ZQGx__TQFlVQFhA2i0a1_63i2a1_61ZQGjMFNcZQFfTQEyZQJvZm", |
| 2763 |
`nothrow @trusted ulong std.algorithm.iteration.FilterResult!(std.typecons.Tuple!(int, "a", int, "b", int, "c").` |
| 2764 |
~`Tuple.rename!([0:"c", 2:"a"]).rename().__lambda1, int[]).FilterResult.__xtoHash(ref const(std.algorithm.iteration.` |
| 2765 |
~`FilterResult!(std.typecons.Tuple!(int, "a", int, "b", int, "c").Tuple.rename!([0:"c", 2:"a"]).rename().__lambda1, int[]).FilterResult))`], |
| 2766 |
|
| 2767 |
["_D4test4rrs1FKPiZv", "void test.rrs1(ref int*)"], |
| 2768 |
["_D4test4rrs1FMNkJPiZv", "void test.rrs1(scope return out int*)"], |
| 2769 |
["_D4test4rrs1FMNkKPiZv", "void test.rrs1(scope return ref int*)"], |
| 2770 |
["_D4test4rrs1FNkJPiZv", "void test.rrs1(return out int*)"], |
| 2771 |
["_D4test4rrs1FNkKPiZv", "void test.rrs1(return ref int*)"], |
| 2772 |
["_D4test4rrs1FNkMJPiZv", "void test.rrs1(return scope out int*)"], |
| 2773 |
["_D4test4rrs1FNkMKPiZv", "void test.rrs1(return scope ref int*)"], |
| 2774 |
["_D4test4rrs1FNkMPiZv", "void test.rrs1(return scope int*)"], |
| 2775 |
|
| 2776 |
// `scope` and `return` combinations |
| 2777 |
["_D3foo3Foo3barMNgFNjNlNfZNgPv", "inout return scope @safe inout(void*) foo.Foo.bar()"], |
| 2778 |
["_D3foo3FooQiMNgFNlNfZv", "inout scope @safe void foo.Foo.foo()"], |
| 2779 |
["_D3foo3Foo4foorMNgFNjNfZv", "inout return @safe void foo.Foo.foor()"], |
| 2780 |
["_D3foo3Foo3rabMNgFNlNjNfZv", "inout scope return @safe void foo.Foo.rab()"], |
| 2781 |
|
| 2782 |
// Hex float digit overflow |
| 2783 |
["_D3foo__T1fVdeFA3D0FBFB72A3C33FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "_D3foo__T1fVdeFA3D0FBFB72A3C33FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"], |
| 2784 |
]; |
| 2785 |
|
| 2786 |
|
| 2787 |
template staticIota(int x) |
| 2788 |
{ |
| 2789 |
template Seq(T...){ alias Seq = T; } |
| 2790 |
|
| 2791 |
static if (x == 0) |
| 2792 |
alias staticIota = Seq!(); |
| 2793 |
else |
| 2794 |
alias staticIota = Seq!(staticIota!(x - 1), x - 1); |
| 2795 |
} |
| 2796 |
foreach ( i, name; table ) |
| 2797 |
{ |
| 2798 |
auto r = demangle( name[0] ); |
| 2799 |
assert( r == name[1], |
| 2800 |
"demangled `" ~ name[0] ~ "` as `" ~ r ~ "` but expected `" ~ name[1] ~ "`"); |
| 2801 |
} |
| 2802 |
foreach ( i; staticIota!(table.length) ) |
| 2803 |
{ |
| 2804 |
enum r = demangle( table[i][0] ); |
| 2805 |
static assert( r == table[i][1], |
| 2806 |
"demangled `" ~ table[i][0] ~ "` as `" ~ r ~ "` but expected `" ~ table[i][1] ~ "`"); |
| 2807 |
} |
| 2808 |
|
| 2809 |
{ |
| 2810 |
// https://issues.dlang.org/show_bug.cgi?id=18531 |
| 2811 |
auto symbol = `_D3std3uni__T6toCaseS_DQvQt12toLowerIndexFNaNbNiNewZtVii1043S_DQCjQCi10toLowerTabFNaNbNiNemZwSQDo5ascii7toLowerTAyaZQDzFNaNeQmZ14__foreachbody2MFNaNeKmKwZ14__foreachbody3MFNaNeKwZi`; |
| 2812 |
auto demangled = `pure @trusted int std.uni.toCase!(std.uni.toLowerIndex(dchar), 1043, std.uni.toLowerTab(ulong), std.ascii.toLower, immutable(char)[]).toCase(immutable(char)[]).__foreachbody2(ref ulong, ref dchar).__foreachbody3(ref dchar)`; |
| 2813 |
auto dst = new char[200]; |
| 2814 |
auto ret = demangle( symbol, dst); |
| 2815 |
assert( ret == demangled ); |
| 2816 |
} |
| 2817 |
} |
| 2818 |
|
| 2819 |
unittest |
| 2820 |
{ |
| 2821 |
// https://issues.dlang.org/show_bug.cgi?id=18300 |
| 2822 |
string s = demangle.mangleof; |
| 2823 |
foreach (i; 1..77) |
| 2824 |
{ |
| 2825 |
char[] buf = new char[i]; |
| 2826 |
auto ds = demangle(s, buf); |
| 2827 |
assert(ds == "pure nothrow @safe char[] core.demangle.demangle(scope return const(char)[], scope return char[], extern (C) char* function(const(char*), char*, ulong*, int*) pure nothrow @trusted*)" || |
| 2828 |
ds == "pure nothrow @safe char[] core.demangle.demangle(return scope const(char)[], return scope char[], extern (C) char* function(const(char*), char*, ulong*, int*) pure nothrow @trusted*)" || |
| 2829 |
ds == "pure nothrow @safe char[] core.demangle.demangle(scope return const(char)[], scope return char[], extern (C) char* function(const(char*), char*, uint*, int*) pure nothrow @trusted*)" || |
| 2830 |
ds == "pure nothrow @safe char[] core.demangle.demangle(return scope const(char)[], return scope char[], extern (C) char* function(const(char*), char*, uint*, int*) pure nothrow @trusted*)", ds); |
| 2831 |
} |
| 2832 |
} |
| 2833 |
|
| 2834 |
unittest |
| 2835 |
{ |
| 2836 |
// https://issues.dlang.org/show_bug.cgi?id=18300 |
| 2837 |
string s = "_D1"; |
| 2838 |
string expected = "int "; |
| 2839 |
foreach (_; 0..10_000) |
| 2840 |
{ |
| 2841 |
s ~= "a1"; |
| 2842 |
expected ~= "a."; |
| 2843 |
} |
| 2844 |
s ~= "FiZi"; |
| 2845 |
expected ~= "F"; |
| 2846 |
assert(s.demangle == expected); |
| 2847 |
|
| 2848 |
// https://issues.dlang.org/show_bug.cgi?id=23562 |
| 2849 |
assert(demangle("_Zv") == "_Zv"); |
| 2850 |
} |
| 2851 |
|
| 2852 |
// https://issues.dlang.org/show_bug.cgi?id=22235 |
| 2853 |
unittest |
| 2854 |
{ |
| 2855 |
enum parent = __MODULE__ ~ '.' ~ __traits(identifier, __traits(parent, {})); |
| 2856 |
|
| 2857 |
static noreturn abort() { assert(false); } |
| 2858 |
assert(demangle(abort.mangleof) == "pure nothrow @nogc @safe noreturn " ~ parent ~ "().abort()"); |
| 2859 |
|
| 2860 |
static void accept(noreturn) {} |
| 2861 |
assert(demangle(accept.mangleof) == "pure nothrow @nogc @safe void " ~ parent ~ "().accept(noreturn)"); |
| 2862 |
|
| 2863 |
static void templ(T)(T, T) {} |
| 2864 |
assert(demangle(templ!noreturn.mangleof) == "pure nothrow @nogc @safe void " ~ parent ~ "().templ!(noreturn).templ(noreturn, noreturn)"); |
| 2865 |
|
| 2866 |
static struct S(T) {} |
| 2867 |
static void aggr(S!noreturn) { assert(0); } |
| 2868 |
assert(demangle(aggr.mangleof) == "pure nothrow @nogc @safe void " ~ parent ~ "().aggr(" ~ parent ~ "().S!(noreturn).S)"); |
| 2869 |
} |
| 2870 |
|
| 2871 |
// locally purified for internal use here only |
| 2872 |
extern (C) private |
| 2873 |
{ |
| 2874 |
pure @trusted @nogc nothrow pragma(mangle, "fakePureReprintReal") void pureReprintReal(char[] nptr); |
| 2875 |
|
| 2876 |
void fakePureReprintReal(char[] nptr) |
| 2877 |
{ |
| 2878 |
import core.stdc.stdlib : strtold; |
| 2879 |
import core.stdc.stdio : snprintf; |
| 2880 |
import core.stdc.errno : errno; |
| 2881 |
|
| 2882 |
const err = errno; |
| 2883 |
real val = strtold(nptr.ptr, null); |
| 2884 |
snprintf(nptr.ptr, nptr.length, "%#Lg", val); |
| 2885 |
errno = err; |
| 2886 |
} |
| 2887 |
} |
| 2888 |
|
| 2889 |
private struct ManglingFlagInfo |
| 2890 |
{ |
| 2891 |
/// The flag value to use |
| 2892 |
ushort flag; |
| 2893 |
|
| 2894 |
/// Human-readable representation |
| 2895 |
string value; |
| 2896 |
} |
| 2897 |
|
| 2898 |
private enum TypeCtor : ushort { |
| 2899 |
None = 0, |
| 2900 |
//// 'x' |
| 2901 |
Const = (1 << 1), |
| 2902 |
/// 'y' |
| 2903 |
Immutable = (1 << 2), |
| 2904 |
/// 'O' |
| 2905 |
Shared = (1 << 3), |
| 2906 |
/// |
| 2907 |
InOut = (1 << 4), |
| 2908 |
} |
| 2909 |
|
| 2910 |
private immutable ManglingFlagInfo[] typeCtors = [ |
| 2911 |
ManglingFlagInfo(TypeCtor.Immutable, "immutable"), |
| 2912 |
ManglingFlagInfo(TypeCtor.Shared, "shared"), |
| 2913 |
ManglingFlagInfo(TypeCtor.InOut, "inout"), |
| 2914 |
ManglingFlagInfo(TypeCtor.Const, "const"), |
| 2915 |
]; |
| 2916 |
|
| 2917 |
private enum FuncAttributes : ushort { |
| 2918 |
None = 0, |
| 2919 |
//// 'a' |
| 2920 |
Pure = (1 << 1), |
| 2921 |
//// 'b' |
| 2922 |
Nothrow = (1 << 2), |
| 2923 |
//// 'c' |
| 2924 |
Ref = (1 << 3), |
| 2925 |
//// 'd' |
| 2926 |
Property = (1 << 4), |
| 2927 |
//// 'e' |
| 2928 |
Trusted = (1 << 5), |
| 2929 |
//// 'f' |
| 2930 |
Safe = (1 << 6), |
| 2931 |
//// 'i' |
| 2932 |
NoGC = (1 << 7), |
| 2933 |
//// 'j' |
| 2934 |
Return = (1 << 8), |
| 2935 |
//// 'l' |
| 2936 |
Scope = (1 << 9), |
| 2937 |
//// 'm' |
| 2938 |
Live = (1 << 10), |
| 2939 |
|
| 2940 |
/// Their order matter |
| 2941 |
ReturnScope = (1 << 11), |
| 2942 |
ScopeReturn = (1 << 12), |
| 2943 |
} |
| 2944 |
|
| 2945 |
// The order in which we process is the same as in compiler/dmd/src/dmangle.d |
| 2946 |
private immutable ManglingFlagInfo[] funcAttrs = [ |
| 2947 |
ManglingFlagInfo(FuncAttributes.Pure, "pure"), |
| 2948 |
ManglingFlagInfo(FuncAttributes.Nothrow, "nothrow"), |
| 2949 |
ManglingFlagInfo(FuncAttributes.Ref, "ref"), |
| 2950 |
ManglingFlagInfo(FuncAttributes.Property, "@property"), |
| 2951 |
ManglingFlagInfo(FuncAttributes.NoGC, "@nogc"), |
| 2952 |
|
| 2953 |
ManglingFlagInfo(FuncAttributes.ReturnScope, "return scope"), |
| 2954 |
ManglingFlagInfo(FuncAttributes.ScopeReturn, "scope return"), |
| 2955 |
|
| 2956 |
ManglingFlagInfo(FuncAttributes.Return, "return"), |
| 2957 |
ManglingFlagInfo(FuncAttributes.Scope, "scope"), |
| 2958 |
|
| 2959 |
ManglingFlagInfo(FuncAttributes.Live, "@live"), |
| 2960 |
ManglingFlagInfo(FuncAttributes.Trusted, "@trusted"), |
| 2961 |
ManglingFlagInfo(FuncAttributes.Safe, "@safe"), |
| 2962 |
]; |
| 2963 |
|
| 2964 |
private string toStringConsume (immutable ManglingFlagInfo[] infos, ref ushort base) |
| 2965 |
@safe pure nothrow @nogc |
| 2966 |
{ |
| 2967 |
foreach (const ref info; infos) |
| 2968 |
{ |
| 2969 |
if ((base & info.flag) == info.flag) |
| 2970 |
{ |
| 2971 |
base &= ~info.flag; |
| 2972 |
return info.value; |
| 2973 |
} |
| 2974 |
} |
| 2975 |
return null; |
| 2976 |
} |
| 2977 |
|
| 2978 |
private shared CXX_DEMANGLER __cxa_demangle; |
| 2979 |
|
| 2980 |
/** |
| 2981 |
* Returns: |
| 2982 |
* a CXX_DEMANGLER if a C++ stdlib is loaded |
| 2983 |
*/ |
| 2984 |
|
| 2985 |
CXX_DEMANGLER getCXXDemangler() nothrow @trusted |
| 2986 |
{ |
| 2987 |
import core.atomic : atomicLoad, atomicStore; |
| 2988 |
if (__cxa_demangle is null) |
| 2989 |
version (Posix) |
| 2990 |
{ |
| 2991 |
import core.sys.posix.dlfcn : dlsym; |
| 2992 |
version (DragonFlyBSD) import core.sys.dragonflybsd.dlfcn : RTLD_DEFAULT; |
| 2993 |
version (FreeBSD) import core.sys.freebsd.dlfcn : RTLD_DEFAULT; |
| 2994 |
version (linux) import core.sys.linux.dlfcn : RTLD_DEFAULT; |
| 2995 |
version (NetBSD) import core.sys.netbsd.dlfcn : RTLD_DEFAULT; |
| 2996 |
version (OpenBSD) import core.sys.openbsd.dlfcn : RTLD_DEFAULT; |
| 2997 |
version (Darwin) import core.sys.darwin.dlfcn : RTLD_DEFAULT; |
| 2998 |
version (Solaris) import core.sys.solaris.dlfcn : RTLD_DEFAULT; |
| 2999 |
|
| 3000 |
if (auto found = cast(CXX_DEMANGLER) dlsym(RTLD_DEFAULT, "__cxa_demangle")) |
| 3001 |
atomicStore(__cxa_demangle, found); |
| 3002 |
} |
| 3003 |
|
| 3004 |
if (__cxa_demangle is null) |
| 3005 |
{ |
| 3006 |
static extern(C) char* _(const char* mangled_name, char* output_buffer, |
| 3007 |
size_t* length, int* status) nothrow pure @trusted |
| 3008 |
{ |
| 3009 |
*status = -1; |
| 3010 |
return null; |
| 3011 |
} |
| 3012 |
atomicStore(__cxa_demangle, &_); |
| 3013 |
} |
| 3014 |
|
| 3015 |
return atomicLoad(__cxa_demangle); |
| 3016 |
} |
| 3017 |
|
| 3018 |
/** |
| 3019 |
* Demangles C++ mangled names. If it is not a C++ mangled name, it |
| 3020 |
* returns its argument name. |
| 3021 |
* |
| 3022 |
* Params: |
| 3023 |
* buf = The string to demangle. |
| 3024 |
* __cxa_demangle = C++ demangler |
| 3025 |
* dst = An optional destination buffer. |
| 3026 |
* |
| 3027 |
* Returns: |
| 3028 |
* The demangled name or the original string if the name is not a mangled |
| 3029 |
* C++ name. |
| 3030 |
*/ |
| 3031 |
private char[] demangleCXX(return scope const(char)[] buf, CXX_DEMANGLER __cxa_demangle, return scope char[] dst = null,) nothrow pure @trusted |
| 3032 |
{ |
| 3033 |
char[] c_string = dst; // temporarily use dst buffer if possible |
| 3034 |
c_string.length = buf.length + 1; |
| 3035 |
c_string[0 .. buf.length] = buf[0 .. buf.length]; |
| 3036 |
c_string[buf.length] = '\0'; |
| 3037 |
|
| 3038 |
int status; |
| 3039 |
size_t demangled_length; |
| 3040 |
auto demangled = __cxa_demangle(&c_string[0], null, &demangled_length, &status); |
| 3041 |
scope (exit) { |
| 3042 |
import core.memory; |
| 3043 |
pureFree(cast(void*) demangled); |
| 3044 |
} |
| 3045 |
if (status == 0) |
| 3046 |
{ |
| 3047 |
dst.length = demangled_length; |
| 3048 |
dst[] = demangled[0 .. demangled_length]; |
| 3049 |
return dst; |
| 3050 |
} |
| 3051 |
|
| 3052 |
dst.length = buf.length; |
| 3053 |
dst[] = buf[]; |
| 3054 |
return dst; |
| 3055 |
} |
| 3056 |
|
| 3057 |
private struct Buffer |
| 3058 |
{ |
| 3059 |
enum size_t minSize = 4000; |
| 3060 |
|
| 3061 |
@safe pure: |
| 3062 |
|
| 3063 |
private char[] dst; |
| 3064 |
private size_t len; |
| 3065 |
|
| 3066 |
public alias opDollar = len; |
| 3067 |
|
| 3068 |
public size_t length () const scope @safe pure nothrow @nogc |
| 3069 |
{ |
| 3070 |
return this.len; |
| 3071 |
} |
| 3072 |
|
| 3073 |
public BufSlice opSlice (size_t from, size_t to) |
| 3074 |
return scope @safe pure nothrow @nogc |
| 3075 |
{ |
| 3076 |
return bslice(from, to); |
| 3077 |
} |
| 3078 |
|
| 3079 |
static bool contains(scope const(char)[] a, scope const BufSlice b) @safe nothrow |
| 3080 |
{ |
| 3081 |
return |
| 3082 |
b.from < a.length && |
| 3083 |
b.to <= a.length; |
| 3084 |
} |
| 3085 |
|
| 3086 |
char[] copyInput(scope const(char)[] buf) |
| 3087 |
return scope nothrow |
| 3088 |
{ |
| 3089 |
if (dst.length < buf.length) |
| 3090 |
dst.length = buf.length; |
| 3091 |
char[] r = dst[0 .. buf.length]; |
| 3092 |
r[] = buf[]; |
| 3093 |
return r; |
| 3094 |
} |
| 3095 |
|
| 3096 |
private void checkAndStretchBuf(size_t len_to_add) scope nothrow |
| 3097 |
{ |
| 3098 |
const required = len + len_to_add; |
| 3099 |
|
| 3100 |
if (required > dst.length) |
| 3101 |
dst.length = dst.length + len_to_add; |
| 3102 |
} |
| 3103 |
|
| 3104 |
// move val to the end of the dst buffer |
| 3105 |
BufSlice shift(scope const BufSlice val) return scope nothrow |
| 3106 |
{ |
| 3107 |
version (DigitalMars) pragma(inline, false); // tame dmd inliner |
| 3108 |
|
| 3109 |
if (val.length) |
| 3110 |
{ |
| 3111 |
const ptrdiff_t s = val.from; |
| 3112 |
const size_t f = len; |
| 3113 |
|
| 3114 |
assert(contains( dst[0 .. len], val ), |
| 3115 |
"\ndst=\""~dst[0 .. len]~"\"\n"~ |
| 3116 |
"val=\""~val.getSlice~"\"\n" |
| 3117 |
); |
| 3118 |
|
| 3119 |
checkAndStretchBuf(val.length); |
| 3120 |
|
| 3121 |
// store value temporary over len index |
| 3122 |
dst[len .. len + val.length] = val.getSlice(); |
| 3123 |
|
| 3124 |
// shift all chars including temporary saved above |
| 3125 |
// if buf was allocated above it will be leave for further usage |
| 3126 |
for (size_t p = s; p < f; p++) |
| 3127 |
dst[p] = dst[p + val.length]; |
| 3128 |
|
| 3129 |
return bslice(len - val.length, len); |
| 3130 |
} |
| 3131 |
|
| 3132 |
return bslice_empty; |
| 3133 |
} |
| 3134 |
|
| 3135 |
// remove val from dst buffer |
| 3136 |
void remove(scope BufSlice val) scope nothrow |
| 3137 |
{ |
| 3138 |
version (DigitalMars) pragma(inline, false); // tame dmd inliner |
| 3139 |
|
| 3140 |
if ( val.length ) |
| 3141 |
{ |
| 3142 |
assert( contains( dst[0 .. len], val ) ); |
| 3143 |
|
| 3144 |
assert( len >= val.length && len <= dst.length ); |
| 3145 |
len -= val.length; |
| 3146 |
for (size_t p = val.from; p < len; p++) |
| 3147 |
dst[p] = dst[p + val.length]; |
| 3148 |
} |
| 3149 |
} |
| 3150 |
|
| 3151 |
void append(scope const(char)[] val) scope nothrow |
| 3152 |
{ |
| 3153 |
version (DigitalMars) pragma(inline, false); // tame dmd inliner |
| 3154 |
|
| 3155 |
if (val.length) |
| 3156 |
{ |
| 3157 |
if ( !dst.length ) |
| 3158 |
dst.length = minSize; |
| 3159 |
|
| 3160 |
debug(info) printf( "appending (%.*s)\n", cast(int) val.length, val.ptr ); |
| 3161 |
|
| 3162 |
checkAndStretchBuf(val.length); |
| 3163 |
|
| 3164 |
// data is already not in place? |
| 3165 |
if ( &dst[len] != &val[0] ) |
| 3166 |
dst[len .. len + val.length] = val[]; |
| 3167 |
|
| 3168 |
len += val.length; |
| 3169 |
} |
| 3170 |
} |
| 3171 |
|
| 3172 |
@nogc: |
| 3173 |
|
| 3174 |
private scope bslice(size_t from, size_t to) nothrow |
| 3175 |
{ |
| 3176 |
return BufSlice(dst, from, to); |
| 3177 |
} |
| 3178 |
|
| 3179 |
private static scope bslice_empty() nothrow |
| 3180 |
{ |
| 3181 |
return BufSlice.init; |
| 3182 |
} |
| 3183 |
} |
| 3184 |
|
| 3185 |
private struct BufSlice |
| 3186 |
{ |
| 3187 |
char[] buf; |
| 3188 |
size_t from; |
| 3189 |
size_t to; |
| 3190 |
|
| 3191 |
@safe pure nothrow: |
| 3192 |
|
| 3193 |
@disable this(); |
| 3194 |
|
| 3195 |
this(return scope char[] buf) scope nothrow @nogc |
| 3196 |
{ |
| 3197 |
this(buf, 0, 0); |
| 3198 |
} |
| 3199 |
|
| 3200 |
this(return scope char[] buf, size_t from, size_t to, bool lastArgIsLen = false) scope nothrow @nogc |
| 3201 |
{ |
| 3202 |
this.buf = buf; |
| 3203 |
this.from = from; |
| 3204 |
|
| 3205 |
if (lastArgIsLen) |
| 3206 |
this.to = from + to; |
| 3207 |
else |
| 3208 |
this.to = to; |
| 3209 |
} |
| 3210 |
|
| 3211 |
invariant |
| 3212 |
{ |
| 3213 |
if (buf is null) |
| 3214 |
{ |
| 3215 |
assert(from == 0); |
| 3216 |
assert(to == 0); |
| 3217 |
} |
| 3218 |
|
| 3219 |
assert(from <= to); |
| 3220 |
} |
| 3221 |
|
| 3222 |
auto getSlice() inout nothrow scope { return buf[from .. to]; } |
| 3223 |
size_t length() const scope { return to - from; } |
| 3224 |
} |