| 1 |
/** |
| 2 |
* $(SCRIPT inhibitQuickIndex = 1;) |
| 3 |
* $(DIVC quickindex, |
| 4 |
* $(BOOKTABLE, |
| 5 |
* $(TR $(TH Category) $(TH Symbols)) |
| 6 |
* $(TR $(TD Arrays) $(TD |
| 7 |
* $(MYREF assumeSafeAppend) |
| 8 |
* $(MYREF capacity) |
| 9 |
* $(A #.dup.2, $(TT dup)) |
| 10 |
* $(MYREF idup) |
| 11 |
* $(MYREF reserve) |
| 12 |
* )) |
| 13 |
* $(TR $(TD Associative arrays) $(TD |
| 14 |
* $(MYREF byKey) |
| 15 |
* $(MYREF byKeyValue) |
| 16 |
* $(MYREF byValue) |
| 17 |
* $(MYREF clear) |
| 18 |
* $(MYREF dup) |
| 19 |
* $(MYREF get) |
| 20 |
* $(MYREF keys) |
| 21 |
* $(MYREF rehash) |
| 22 |
* $(MYREF require) |
| 23 |
* $(MYREF update) |
| 24 |
* $(MYREF values) |
| 25 |
* )) |
| 26 |
* $(TR $(TD General) $(TD |
| 27 |
* $(MYREF destroy) |
| 28 |
* $(MYREF hashOf) |
| 29 |
* $(MYREF imported) |
| 30 |
* $(MYREF noreturn) |
| 31 |
* )) |
| 32 |
* $(TR $(TD Classes) $(TD |
| 33 |
* $(MYREF Error) |
| 34 |
* $(MYREF Exception) |
| 35 |
* $(MYREF Object) |
| 36 |
* $(MYREF opEquals) |
| 37 |
* $(MYREF Throwable) |
| 38 |
* )) |
| 39 |
* $(TR $(TD Type info) $(TD |
| 40 |
* $(MYREF Interface) |
| 41 |
* $(MYREF ModuleInfo) |
| 42 |
* $(MYREF OffsetTypeInfo) |
| 43 |
* $(MYREF RTInfoImpl) |
| 44 |
* $(MYREF rtinfoNoPointers) |
| 45 |
* $(MYREF TypeInfo) |
| 46 |
* $(MYREF TypeInfo_Class) |
| 47 |
* )) |
| 48 |
* )) |
| 49 |
* |
| 50 |
* Forms the symbols available to all D programs. Includes Object, which is |
| 51 |
* the root of the class object hierarchy. This module is implicitly |
| 52 |
* imported. |
| 53 |
* |
| 54 |
* Copyright: Copyright Digital Mars 2000 - 2011. |
| 55 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 56 |
* Authors: Walter Bright, Sean Kelly |
| 57 |
* Source: $(DRUNTIMESRC object.d) |
| 58 |
*/ |
| 59 |
|
| 60 |
module object; |
| 61 |
|
| 62 |
alias size_t = typeof(int.sizeof); |
| 63 |
alias ptrdiff_t = typeof(cast(void*)0 - cast(void*)0); |
| 64 |
|
| 65 |
alias sizediff_t = ptrdiff_t; // For backwards compatibility only. |
| 66 |
/** |
| 67 |
* Bottom type. |
| 68 |
* See $(DDSUBLINK spec/type, noreturn, `noreturn`). |
| 69 |
*/ |
| 70 |
alias noreturn = typeof(*null); |
| 71 |
|
| 72 |
alias hash_t = size_t; // For backwards compatibility only. |
| 73 |
alias equals_t = bool; // For backwards compatibility only. |
| 74 |
|
| 75 |
alias string = immutable(char)[]; |
| 76 |
alias wstring = immutable(wchar)[]; |
| 77 |
alias dstring = immutable(dchar)[]; |
| 78 |
|
| 79 |
version (D_ObjectiveC) |
| 80 |
{ |
| 81 |
deprecated("explicitly import `selector` instead using: `import core.attribute : selector;`") |
| 82 |
public import core.attribute : selector; |
| 83 |
} |
| 84 |
version (Posix) public import core.attribute : gnuAbiTag; |
| 85 |
|
| 86 |
// Some ABIs use a complex varargs implementation requiring TypeInfo.argTypes(). |
| 87 |
version (GNU) |
| 88 |
{ |
| 89 |
// No TypeInfo-based core.vararg.va_arg(). |
| 90 |
} |
| 91 |
else version (X86_64) |
| 92 |
{ |
| 93 |
version (DigitalMars) version = WithArgTypes; |
| 94 |
else version (Windows) { /* no need for Win64 ABI */ } |
| 95 |
else version = WithArgTypes; |
| 96 |
} |
| 97 |
else version (AArch64) |
| 98 |
{ |
| 99 |
// Apple uses a trivial varargs implementation |
| 100 |
version (OSX) {} |
| 101 |
else version (iOS) {} |
| 102 |
else version (TVOS) {} |
| 103 |
else version (WatchOS) {} |
| 104 |
else version = WithArgTypes; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* All D class objects inherit from Object. |
| 109 |
*/ |
| 110 |
class Object |
| 111 |
{ |
| 112 |
/** |
| 113 |
* Convert Object to a human readable string. |
| 114 |
*/ |
| 115 |
string toString() |
| 116 |
{ |
| 117 |
return typeid(this).name; |
| 118 |
} |
| 119 |
|
| 120 |
@system unittest |
| 121 |
{ |
| 122 |
enum unittest_sym_name = __traits(identifier, __traits(parent, (){})); |
| 123 |
enum fqn_unittest = "object.Object." ~ unittest_sym_name; // object.__unittest_LX_CY |
| 124 |
|
| 125 |
class C {} |
| 126 |
|
| 127 |
Object obj = new Object; |
| 128 |
C c = new C; |
| 129 |
|
| 130 |
assert(obj.toString() == "object.Object"); |
| 131 |
assert(c.toString() == fqn_unittest ~ ".C"); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Compute hash function for Object. |
| 136 |
*/ |
| 137 |
size_t toHash() @trusted nothrow |
| 138 |
{ |
| 139 |
// BUG: this prevents a compacting GC from working, needs to be fixed |
| 140 |
size_t addr = cast(size_t) cast(void*) this; |
| 141 |
// The bottom log2((void*).alignof) bits of the address will always |
| 142 |
// be 0. Moreover it is likely that each Object is allocated with a |
| 143 |
// separate call to malloc. The alignment of malloc differs from |
| 144 |
// platform to platform, but rather than having special cases for |
| 145 |
// each platform it is safe to use a shift of 4. To minimize |
| 146 |
// collisions in the low bits it is more important for the shift to |
| 147 |
// not be too small than for the shift to not be too big. |
| 148 |
return addr ^ (addr >>> 4); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Compare with another Object obj. |
| 153 |
* Returns: |
| 154 |
* $(TABLE |
| 155 |
* $(TR $(TD this < obj) $(TD < 0)) |
| 156 |
* $(TR $(TD this == obj) $(TD 0)) |
| 157 |
* $(TR $(TD this > obj) $(TD > 0)) |
| 158 |
* ) |
| 159 |
*/ |
| 160 |
int opCmp(Object o) |
| 161 |
{ |
| 162 |
// BUG: this prevents a compacting GC from working, needs to be fixed |
| 163 |
//return cast(int)cast(void*)this - cast(int)cast(void*)o; |
| 164 |
|
| 165 |
throw new Exception("need opCmp for class " ~ typeid(this).name); |
| 166 |
//return this !is o; |
| 167 |
} |
| 168 |
|
| 169 |
@system unittest |
| 170 |
{ |
| 171 |
Object obj = new Object; |
| 172 |
|
| 173 |
bool gotCaught; |
| 174 |
try |
| 175 |
{ |
| 176 |
obj.opCmp(new Object); |
| 177 |
} |
| 178 |
catch (Exception e) |
| 179 |
{ |
| 180 |
gotCaught = true; |
| 181 |
assert(e.msg == "need opCmp for class object.Object"); |
| 182 |
} |
| 183 |
assert(gotCaught); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Test whether $(D this) is equal to $(D o). |
| 188 |
* The default implementation only compares by identity (using the $(D is) operator). |
| 189 |
* Generally, overrides and overloads for $(D opEquals) should attempt to compare objects by their contents. |
| 190 |
* A class will most likely want to add an overload that takes your specific type as the argument |
| 191 |
* and does the content comparison. Then you can override this and forward it to your specific |
| 192 |
* typed overload with a cast. Remember to check for `null` on the typed overload. |
| 193 |
* |
| 194 |
* Examples: |
| 195 |
* --- |
| 196 |
* class Child { |
| 197 |
* int contents; |
| 198 |
* // the typed overload first. It can use all the attribute you want |
| 199 |
* bool opEquals(const Child c) const @safe pure nothrow @nogc |
| 200 |
* { |
| 201 |
* if (c is null) |
| 202 |
* return false; |
| 203 |
* return this.contents == c.contents; |
| 204 |
* } |
| 205 |
* |
| 206 |
* // and now the generic override forwards with a cast |
| 207 |
* override bool opEquals(Object o) |
| 208 |
* { |
| 209 |
* return this.opEquals(cast(Child) o); |
| 210 |
* } |
| 211 |
* } |
| 212 |
* --- |
| 213 |
*/ |
| 214 |
bool opEquals(Object o) |
| 215 |
{ |
| 216 |
return this is o; |
| 217 |
} |
| 218 |
|
| 219 |
interface Monitor |
| 220 |
{ |
| 221 |
void lock(); |
| 222 |
void unlock(); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Create instance of class specified by the fully qualified name |
| 227 |
* classname. |
| 228 |
* The class must either have no constructors or have |
| 229 |
* a default constructor. |
| 230 |
* Returns: |
| 231 |
* null if failed |
| 232 |
* Example: |
| 233 |
* --- |
| 234 |
* module foo.bar; |
| 235 |
* |
| 236 |
* class C |
| 237 |
* { |
| 238 |
* this() { x = 10; } |
| 239 |
* int x; |
| 240 |
* } |
| 241 |
* |
| 242 |
* void main() |
| 243 |
* { |
| 244 |
* auto c = cast(C)Object.factory("foo.bar.C"); |
| 245 |
* assert(c !is null && c.x == 10); |
| 246 |
* } |
| 247 |
* --- |
| 248 |
*/ |
| 249 |
static Object factory(string classname) |
| 250 |
{ |
| 251 |
auto ci = TypeInfo_Class.find(classname); |
| 252 |
if (ci) |
| 253 |
{ |
| 254 |
return ci.create(); |
| 255 |
} |
| 256 |
return null; |
| 257 |
} |
| 258 |
|
| 259 |
@system unittest |
| 260 |
{ |
| 261 |
Object valid_obj = Object.factory("object.Object"); |
| 262 |
Object invalid_obj = Object.factory("object.__this_class_doesnt_exist__"); |
| 263 |
|
| 264 |
assert(valid_obj !is null); |
| 265 |
assert(invalid_obj is null); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/++ |
| 270 |
Implementation for class opEquals override. Calls the class-defined methods after a null check. |
| 271 |
Please note this is not nogc right now, even if your implementation is, because of |
| 272 |
the typeinfo name string compare. This is because of dmd's dll implementation. However, |
| 273 |
it can infer to @safe if your class' opEquals is. |
| 274 |
+/ |
| 275 |
bool opEquals(LHS, RHS)(LHS lhs, RHS rhs) |
| 276 |
if ((is(LHS : const Object) || is(LHS : const shared Object)) && |
| 277 |
(is(RHS : const Object) || is(RHS : const shared Object))) |
| 278 |
{ |
| 279 |
static if (__traits(compiles, lhs.opEquals(rhs)) && __traits(compiles, rhs.opEquals(lhs))) |
| 280 |
{ |
| 281 |
// If aliased to the same object or both null => equal |
| 282 |
if (lhs is rhs) return true; |
| 283 |
|
| 284 |
// If either is null => non-equal |
| 285 |
if (lhs is null || rhs is null) return false; |
| 286 |
|
| 287 |
if (!lhs.opEquals(rhs)) return false; |
| 288 |
|
| 289 |
// If same exact type => one call to method opEquals |
| 290 |
if (typeid(lhs) is typeid(rhs) || |
| 291 |
!__ctfe && typeid(lhs).opEquals(typeid(rhs))) |
| 292 |
/* CTFE doesn't like typeid much. 'is' works, but opEquals doesn't: |
| 293 |
https://issues.dlang.org/show_bug.cgi?id=7147 |
| 294 |
But CTFE also guarantees that equal TypeInfos are |
| 295 |
always identical. So, no opEquals needed during CTFE. */ |
| 296 |
{ |
| 297 |
return true; |
| 298 |
} |
| 299 |
|
| 300 |
// General case => symmetric calls to method opEquals |
| 301 |
return rhs.opEquals(lhs); |
| 302 |
} |
| 303 |
else |
| 304 |
{ |
| 305 |
// this is a compatibility hack for the old const cast behavior |
| 306 |
// if none of the new overloads compile, we'll go back plain Object, |
| 307 |
// including casting away const. It does this through the pointer |
| 308 |
// to bypass any opCast that may be present on the original class. |
| 309 |
return .opEquals!(Object, Object)(*cast(Object*) &lhs, *cast(Object*) &rhs); |
| 310 |
|
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
/// If aliased to the same object or both null => equal |
| 315 |
@system unittest // this one is not @safe because it goes through the Object base method |
| 316 |
{ |
| 317 |
class F { int flag; this(int flag) { this.flag = flag; } } |
| 318 |
|
| 319 |
F f; |
| 320 |
assert(f == f); // both null |
| 321 |
f = new F(1); |
| 322 |
assert(f == f); // both aliased to the same object |
| 323 |
} |
| 324 |
|
| 325 |
/// If either is null => non-equal |
| 326 |
@system unittest |
| 327 |
{ |
| 328 |
class F { int flag; this(int flag) { this.flag = flag; } } |
| 329 |
F f; |
| 330 |
assert(!(new F(0) == f)); |
| 331 |
assert(!(f == new F(0))); |
| 332 |
} |
| 333 |
|
| 334 |
/// If same exact type => one call to method opEquals |
| 335 |
/// This test passes `@safe` because it defines a new opEquals with `@safe` |
| 336 |
@safe unittest |
| 337 |
{ |
| 338 |
class F |
| 339 |
{ |
| 340 |
int flag; |
| 341 |
|
| 342 |
this(int flag) |
| 343 |
{ |
| 344 |
this.flag = flag; |
| 345 |
} |
| 346 |
|
| 347 |
bool opEquals(const F o) const @safe nothrow pure |
| 348 |
{ |
| 349 |
return flag == o.flag; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
F f; |
| 354 |
assert(new F(0) == new F(0)); |
| 355 |
assert(!(new F(0) == new F(1))); |
| 356 |
} |
| 357 |
|
| 358 |
/// General case => symmetric calls to method opEquals |
| 359 |
@safe unittest |
| 360 |
{ |
| 361 |
int fEquals, gEquals; |
| 362 |
|
| 363 |
class Base |
| 364 |
{ |
| 365 |
int flag; |
| 366 |
this(int flag) |
| 367 |
{ |
| 368 |
this.flag = flag; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
class F : Base |
| 373 |
{ |
| 374 |
this(int flag) { super(flag); } |
| 375 |
|
| 376 |
bool opEquals(const Base o) @safe |
| 377 |
{ |
| 378 |
fEquals++; |
| 379 |
return flag == o.flag; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
class G : Base |
| 384 |
{ |
| 385 |
this(int flag) { super(flag); } |
| 386 |
|
| 387 |
bool opEquals(const Base o) @safe |
| 388 |
{ |
| 389 |
gEquals++; |
| 390 |
return flag == o.flag; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
assert(new F(1) == new G(1)); |
| 395 |
assert(fEquals == 1); |
| 396 |
assert(gEquals == 1); |
| 397 |
} |
| 398 |
|
| 399 |
/++ |
| 400 |
This test shows an example for a comprehensive inheritance equality chain too. |
| 401 |
+/ |
| 402 |
unittest |
| 403 |
{ |
| 404 |
static class Base |
| 405 |
{ |
| 406 |
int member; |
| 407 |
|
| 408 |
this(int member) pure @safe nothrow @nogc |
| 409 |
{ |
| 410 |
this.member = member; |
| 411 |
} |
| 412 |
|
| 413 |
override bool opEquals(Object rhs) const |
| 414 |
{ |
| 415 |
return this.opEquals(cast(Base) rhs); |
| 416 |
} |
| 417 |
|
| 418 |
bool opEquals(const Base rhs) const @nogc pure nothrow @safe |
| 419 |
{ |
| 420 |
if (rhs is null) |
| 421 |
return false; |
| 422 |
return this.member == rhs.member; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
// works through the direct class with attributes enabled, except for pure and nogc in the current TypeInfo implementation |
| 427 |
bool testThroughBase() nothrow @safe |
| 428 |
{ |
| 429 |
Base b1 = new Base(0); |
| 430 |
Base b2 = new Base(0); |
| 431 |
assert(b1 == b2); |
| 432 |
Base b3 = new Base(1); |
| 433 |
assert(b1 != b3); |
| 434 |
return true; |
| 435 |
} |
| 436 |
|
| 437 |
static assert(testThroughBase()); |
| 438 |
|
| 439 |
// also works through the base class interface thanks to the override, but no more attributes |
| 440 |
bool testThroughObject() |
| 441 |
{ |
| 442 |
Object o1 = new Base(0); |
| 443 |
Object o2 = new Base(0); |
| 444 |
assert(o1 == o2); |
| 445 |
Object o3 = new Base(1); |
| 446 |
assert(o1 != o3); |
| 447 |
return true; |
| 448 |
} |
| 449 |
|
| 450 |
static assert(testThroughObject()); |
| 451 |
|
| 452 |
// Each time you make a child, you want to override all old opEquals |
| 453 |
// and add a new overload for the new child. |
| 454 |
static class Child : Base |
| 455 |
{ |
| 456 |
int member2; |
| 457 |
|
| 458 |
this(int member, int member2) pure @safe nothrow @nogc |
| 459 |
{ |
| 460 |
super(member); |
| 461 |
this.member2 = member2; |
| 462 |
} |
| 463 |
|
| 464 |
// override the whole chain so it works consistently though any base |
| 465 |
override bool opEquals(Object rhs) const |
| 466 |
{ |
| 467 |
return this.opEquals(cast(Child) rhs); |
| 468 |
} |
| 469 |
override bool opEquals(const Base rhs) const |
| 470 |
{ |
| 471 |
return this.opEquals(cast(const Child) rhs); |
| 472 |
} |
| 473 |
// and then add the new overload, if necessary, to handle new members |
| 474 |
bool opEquals(const Child rhs) const @nogc pure nothrow @safe |
| 475 |
{ |
| 476 |
if (rhs is null) |
| 477 |
return false; |
| 478 |
// can call back to the devirtualized base test with implicit conversion |
| 479 |
// then compare the new member too. or we could have just compared the base |
| 480 |
// member directly here as well. |
| 481 |
return Base.opEquals(rhs) && this.member2 == rhs.member2; |
| 482 |
} |
| 483 |
|
| 484 |
// a mixin template, of course, could automate this. |
| 485 |
} |
| 486 |
|
| 487 |
bool testThroughChild() |
| 488 |
{ |
| 489 |
Child a = new Child(0, 0); |
| 490 |
Child b = new Child(0, 1); |
| 491 |
assert(a != b); |
| 492 |
|
| 493 |
Base ba = a; |
| 494 |
Base bb = b; |
| 495 |
assert(ba != bb); |
| 496 |
|
| 497 |
Object oa = a; |
| 498 |
Object ob = b; |
| 499 |
assert(oa != ob); |
| 500 |
|
| 501 |
return true; |
| 502 |
} |
| 503 |
|
| 504 |
static assert(testThroughChild()); |
| 505 |
} |
| 506 |
|
| 507 |
// To cover const Object opEquals |
| 508 |
@system unittest |
| 509 |
{ |
| 510 |
const Object obj1 = new Object; |
| 511 |
const Object obj2 = new Object; |
| 512 |
|
| 513 |
assert(obj1 == obj1); |
| 514 |
assert(obj1 != obj2); |
| 515 |
} |
| 516 |
|
| 517 |
// https://issues.dlang.org/show_bug.cgi?id=23291 |
| 518 |
@system unittest |
| 519 |
{ |
| 520 |
static shared class C { bool opEquals(const(shared(C)) rhs) const shared { return true;}} |
| 521 |
const(C) c = new C(); |
| 522 |
const(C)[] a = [c]; |
| 523 |
const(C)[] b = [c]; |
| 524 |
assert(a[0] == b[0]); |
| 525 |
} |
| 526 |
|
| 527 |
private extern(C) void _d_setSameMutex(shared Object ownee, shared Object owner) nothrow; |
| 528 |
|
| 529 |
/** Makes ownee use owner's mutex. |
| 530 |
* This will initialize owner's mutex if it hasn't been set yet. |
| 531 |
* Params: |
| 532 |
* ownee = object to change |
| 533 |
* owner = source object |
| 534 |
*/ |
| 535 |
void setSameMutex(shared Object ownee, shared Object owner) |
| 536 |
{ |
| 537 |
import core.atomic : atomicLoad; |
| 538 |
_d_setSameMutex(atomicLoad(ownee), atomicLoad(owner)); |
| 539 |
} |
| 540 |
|
| 541 |
@system unittest |
| 542 |
{ |
| 543 |
shared Object obj1 = new Object; |
| 544 |
synchronized class C |
| 545 |
{ |
| 546 |
void bar() {} |
| 547 |
} |
| 548 |
shared C obj2 = new shared(C); |
| 549 |
obj2.bar(); |
| 550 |
|
| 551 |
assert(obj1.__monitor != obj2.__monitor); |
| 552 |
assert(obj1.__monitor is null); |
| 553 |
|
| 554 |
setSameMutex(obj1, obj2); |
| 555 |
assert(obj1.__monitor == obj2.__monitor); |
| 556 |
assert(obj1.__monitor !is null); |
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
* Information about an interface. |
| 561 |
* When an object is accessed via an interface, an Interface* appears as the |
| 562 |
* first entry in its vtbl. |
| 563 |
*/ |
| 564 |
struct Interface |
| 565 |
{ |
| 566 |
/// Class info returned by `typeid` for this interface (not for containing class) |
| 567 |
TypeInfo_Class classinfo; |
| 568 |
void*[] vtbl; |
| 569 |
size_t offset; /// offset to Interface 'this' from Object 'this' |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Array of pairs giving the offset and type information for each |
| 574 |
* member in an aggregate. |
| 575 |
*/ |
| 576 |
struct OffsetTypeInfo |
| 577 |
{ |
| 578 |
size_t offset; /// Offset of member from start of object |
| 579 |
TypeInfo ti; /// TypeInfo for this member |
| 580 |
} |
| 581 |
|
| 582 |
/** |
| 583 |
* Runtime type information about a type. |
| 584 |
* Can be retrieved for any type using a |
| 585 |
* $(GLINK2 expression,TypeidExpression, TypeidExpression). |
| 586 |
*/ |
| 587 |
class TypeInfo |
| 588 |
{ |
| 589 |
override string toString() const @safe nothrow |
| 590 |
{ |
| 591 |
return typeid(this).name; |
| 592 |
} |
| 593 |
|
| 594 |
override size_t toHash() @trusted const nothrow |
| 595 |
{ |
| 596 |
return hashOf(this.toString()); |
| 597 |
} |
| 598 |
|
| 599 |
override int opCmp(Object rhs) |
| 600 |
{ |
| 601 |
if (this is rhs) |
| 602 |
return 0; |
| 603 |
auto ti = cast(TypeInfo) rhs; |
| 604 |
if (ti is null) |
| 605 |
return 1; |
| 606 |
return __cmp(this.toString(), ti.toString()); |
| 607 |
} |
| 608 |
|
| 609 |
@system unittest |
| 610 |
{ |
| 611 |
assert(typeid(void) <= typeid(void)); |
| 612 |
assert(typeid(void).opCmp(null)); |
| 613 |
assert(!typeid(void).opCmp(typeid(void))); |
| 614 |
} |
| 615 |
|
| 616 |
override bool opEquals(Object o) |
| 617 |
{ |
| 618 |
return opEquals(cast(TypeInfo) o); |
| 619 |
} |
| 620 |
|
| 621 |
bool opEquals(const TypeInfo ti) @safe nothrow const |
| 622 |
{ |
| 623 |
/* TypeInfo instances are singletons, but duplicates can exist |
| 624 |
* across DLL's. Therefore, comparing for a name match is |
| 625 |
* sufficient. |
| 626 |
*/ |
| 627 |
if (this is ti) |
| 628 |
return true; |
| 629 |
return ti && this.toString() == ti.toString(); |
| 630 |
} |
| 631 |
|
| 632 |
@system unittest |
| 633 |
{ |
| 634 |
auto anotherObj = new Object(); |
| 635 |
|
| 636 |
assert(typeid(void).opEquals(typeid(void))); |
| 637 |
assert(typeid(void) != anotherObj); // calling .opEquals here directly is a type mismatch |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* Computes a hash of the instance of a type. |
| 642 |
* Params: |
| 643 |
* p = pointer to start of instance of the type |
| 644 |
* Returns: |
| 645 |
* the hash |
| 646 |
* Bugs: |
| 647 |
* fix https://issues.dlang.org/show_bug.cgi?id=12516 e.g. by changing this to a truly safe interface. |
| 648 |
*/ |
| 649 |
size_t getHash(scope const void* p) @trusted nothrow const |
| 650 |
{ |
| 651 |
// by default, do not assume anything about the type |
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
/// Compares two instances for equality. |
| 656 |
bool equals(in void* p1, in void* p2) const { return p1 == p2; } |
| 657 |
|
| 658 |
/// Compares two instances for <, ==, or >. |
| 659 |
int compare(in void* p1, in void* p2) const { return _xopCmp(p1, p2); } |
| 660 |
|
| 661 |
/// Returns size of the type. |
| 662 |
@property size_t tsize() nothrow pure const @safe @nogc { return 0; } |
| 663 |
|
| 664 |
/// Swaps two instances of the type. |
| 665 |
void swap(void* p1, void* p2) const |
| 666 |
{ |
| 667 |
size_t remaining = tsize; |
| 668 |
// If the type might contain pointers perform the swap in pointer-sized |
| 669 |
// chunks in case a garbage collection pass interrupts this function. |
| 670 |
if ((cast(size_t) p1 | cast(size_t) p2) % (void*).alignof == 0) |
| 671 |
{ |
| 672 |
while (remaining >= (void*).sizeof) |
| 673 |
{ |
| 674 |
void* tmp = *cast(void**) p1; |
| 675 |
*cast(void**) p1 = *cast(void**) p2; |
| 676 |
*cast(void**) p2 = tmp; |
| 677 |
p1 += (void*).sizeof; |
| 678 |
p2 += (void*).sizeof; |
| 679 |
remaining -= (void*).sizeof; |
| 680 |
} |
| 681 |
} |
| 682 |
for (size_t i = 0; i < remaining; i++) |
| 683 |
{ |
| 684 |
byte t = (cast(byte *)p1)[i]; |
| 685 |
(cast(byte*)p1)[i] = (cast(byte*)p2)[i]; |
| 686 |
(cast(byte*)p2)[i] = t; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
@system unittest |
| 691 |
{ |
| 692 |
class _TypeInfo_Dummy : TypeInfo |
| 693 |
{ |
| 694 |
override const(void)[] initializer() const { return []; } |
| 695 |
@property override size_t tsize() nothrow pure const @safe @nogc { return tsize_val; } |
| 696 |
|
| 697 |
size_t tsize_val; |
| 698 |
} |
| 699 |
auto dummy = new _TypeInfo_Dummy(); |
| 700 |
cast(void)dummy.initializer(); // For coverage completeness |
| 701 |
|
| 702 |
int a = 2, b = -2; |
| 703 |
dummy.swap(&a, &b); |
| 704 |
// does nothing because tsize is 0 |
| 705 |
assert(a == 2); |
| 706 |
assert(b == -2); |
| 707 |
|
| 708 |
dummy.tsize_val = int.sizeof; |
| 709 |
dummy.swap(&a, &b); |
| 710 |
assert(a == -2); |
| 711 |
assert(b == 2); |
| 712 |
|
| 713 |
void* ptr_a = null, ptr_b = cast(void*)1; |
| 714 |
dummy.tsize_val = (void*).sizeof; |
| 715 |
dummy.swap(&ptr_a, &ptr_b); |
| 716 |
assert(ptr_a is cast(void*)1); |
| 717 |
assert(ptr_b is null); |
| 718 |
} |
| 719 |
|
| 720 |
/** Get TypeInfo for 'next' type, as defined by what kind of type this is, |
| 721 |
null if none. */ |
| 722 |
@property inout(TypeInfo) next() nothrow pure inout @nogc { return null; } |
| 723 |
|
| 724 |
/** |
| 725 |
* Return default initializer. If the type should be initialized to all |
| 726 |
* zeros, an array with a null ptr and a length equal to the type size will |
| 727 |
* be returned. For static arrays, this returns the default initializer for |
| 728 |
* a single element of the array, use `tsize` to get the correct size. |
| 729 |
*/ |
| 730 |
abstract const(void)[] initializer() nothrow pure const @safe @nogc; |
| 731 |
|
| 732 |
/** Get flags for type: 1 means GC should scan for pointers, |
| 733 |
2 means arg of this type is passed in SIMD register(s) if available */ |
| 734 |
@property uint flags() nothrow pure const @safe @nogc { return 0; } |
| 735 |
|
| 736 |
/// Get type information on the contents of the type; null if not available |
| 737 |
const(OffsetTypeInfo)[] offTi() const { return null; } |
| 738 |
/// Run the destructor on the object and all its sub-objects |
| 739 |
void destroy(void* p) const {} |
| 740 |
/// Run the postblit on the object and all its sub-objects |
| 741 |
void postblit(void* p) const {} |
| 742 |
|
| 743 |
|
| 744 |
/// Return alignment of type |
| 745 |
@property size_t talign() nothrow pure const @safe @nogc { return tsize; } |
| 746 |
|
| 747 |
/** Return internal info on arguments fitting into 8byte. |
| 748 |
* See X86-64 ABI 3.2.3 |
| 749 |
*/ |
| 750 |
version (WithArgTypes) int argTypes(out TypeInfo arg1, out TypeInfo arg2) @safe nothrow |
| 751 |
{ |
| 752 |
arg1 = this; |
| 753 |
return 0; |
| 754 |
} |
| 755 |
|
| 756 |
/** Return info used by the garbage collector to do precise collection. |
| 757 |
*/ |
| 758 |
@property immutable(void)* rtInfo() nothrow pure const @trusted @nogc { return rtinfoHasPointers; } // better safe than sorry |
| 759 |
} |
| 760 |
|
| 761 |
@system unittest |
| 762 |
{ |
| 763 |
class _TypeInfo_Dummy : TypeInfo |
| 764 |
{ |
| 765 |
override const(void)[] initializer() const { return []; } |
| 766 |
} |
| 767 |
auto dummy = new _TypeInfo_Dummy(); |
| 768 |
cast(void)dummy.initializer(); // For coverage completeness |
| 769 |
|
| 770 |
assert(dummy.rtInfo() is rtinfoHasPointers); |
| 771 |
assert(typeid(void).rtInfo() is rtinfoNoPointers); |
| 772 |
|
| 773 |
assert(dummy.tsize() == 0); |
| 774 |
|
| 775 |
bool gotCaught; |
| 776 |
try |
| 777 |
{ |
| 778 |
dummy.compare(null, null); |
| 779 |
} catch (Error e) |
| 780 |
{ |
| 781 |
gotCaught = true; |
| 782 |
assert(e.msg == "TypeInfo.compare is not implemented"); |
| 783 |
} |
| 784 |
assert(gotCaught); |
| 785 |
|
| 786 |
assert(dummy.equals(null, null)); |
| 787 |
assert(!dummy.equals(cast(void*)1, null)); |
| 788 |
} |
| 789 |
|
| 790 |
@system unittest |
| 791 |
{ |
| 792 |
assert(typeid(void).next() is null); |
| 793 |
assert(typeid(void).offTi() is null); |
| 794 |
assert(typeid(void).tsize() == 1); |
| 795 |
|
| 796 |
version (WithArgTypes) |
| 797 |
{ |
| 798 |
TypeInfo ti1; |
| 799 |
TypeInfo ti2; |
| 800 |
assert(typeid(void).argTypes(ti1, ti2) == 0); |
| 801 |
assert(typeid(void) is ti1); |
| 802 |
|
| 803 |
assert(ti1 !is null); |
| 804 |
assert(ti2 is null); |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
@system unittest |
| 809 |
{ |
| 810 |
class _ZypeInfo_Dummy : TypeInfo |
| 811 |
{ |
| 812 |
override const(void)[] initializer() const { return []; } |
| 813 |
} |
| 814 |
auto dummy2 = new _ZypeInfo_Dummy(); |
| 815 |
cast(void)dummy2.initializer(); // For coverage completeness |
| 816 |
|
| 817 |
assert(typeid(void) > dummy2); |
| 818 |
assert(dummy2 < typeid(void)); |
| 819 |
} |
| 820 |
|
| 821 |
@safe unittest |
| 822 |
{ |
| 823 |
enum unittest_sym_name = __traits(identifier, __traits(parent, (){})); |
| 824 |
enum fqn_unittest = "object." ~ unittest_sym_name; // object.__unittest_LX_CY |
| 825 |
|
| 826 |
class _TypeInfo_Dummy : TypeInfo |
| 827 |
{ |
| 828 |
override const(void)[] initializer() const { return []; } |
| 829 |
} |
| 830 |
|
| 831 |
auto dummy = new _TypeInfo_Dummy(); |
| 832 |
cast(void)dummy.initializer(); // For coverage completeness |
| 833 |
|
| 834 |
assert(dummy.toString() == fqn_unittest ~ "._TypeInfo_Dummy"); |
| 835 |
assert(dummy.toHash() == hashOf(dummy.toString())); |
| 836 |
assert(dummy.getHash(null) == 0); |
| 837 |
} |
| 838 |
|
| 839 |
class TypeInfo_Enum : TypeInfo |
| 840 |
{ |
| 841 |
override string toString() const pure { return name; } |
| 842 |
|
| 843 |
override bool opEquals(Object o) |
| 844 |
{ |
| 845 |
if (this is o) |
| 846 |
return true; |
| 847 |
auto c = cast(const TypeInfo_Enum)o; |
| 848 |
return c && this.name == c.name && |
| 849 |
this.base == c.base; |
| 850 |
} |
| 851 |
|
| 852 |
@system unittest |
| 853 |
{ |
| 854 |
enum E { A, B, C } |
| 855 |
enum EE { A, B, C } |
| 856 |
|
| 857 |
assert(typeid(E).opEquals(typeid(E))); |
| 858 |
assert(!typeid(E).opEquals(typeid(EE))); |
| 859 |
} |
| 860 |
|
| 861 |
override size_t getHash(scope const void* p) const { return base.getHash(p); } |
| 862 |
|
| 863 |
@system unittest |
| 864 |
{ |
| 865 |
enum E { A, B, C } |
| 866 |
E e1 = E.A; |
| 867 |
E e2 = E.B; |
| 868 |
|
| 869 |
assert(typeid(E).getHash(&e1) == hashOf(E.A)); |
| 870 |
assert(typeid(E).getHash(&e2) == hashOf(E.B)); |
| 871 |
|
| 872 |
enum ES : string { A = "foo", B = "bar" } |
| 873 |
ES es1 = ES.A; |
| 874 |
ES es2 = ES.B; |
| 875 |
|
| 876 |
assert(typeid(ES).getHash(&es1) == hashOf("foo")); |
| 877 |
assert(typeid(ES).getHash(&es2) == hashOf("bar")); |
| 878 |
} |
| 879 |
|
| 880 |
override bool equals(in void* p1, in void* p2) const { return base.equals(p1, p2); } |
| 881 |
|
| 882 |
@system unittest |
| 883 |
{ |
| 884 |
enum E { A, B, C } |
| 885 |
|
| 886 |
E e1 = E.A; |
| 887 |
E e2 = E.B; |
| 888 |
|
| 889 |
assert(typeid(E).equals(&e1, &e1)); |
| 890 |
assert(!typeid(E).equals(&e1, &e2)); |
| 891 |
} |
| 892 |
|
| 893 |
override int compare(in void* p1, in void* p2) const { return base.compare(p1, p2); } |
| 894 |
|
| 895 |
@system unittest |
| 896 |
{ |
| 897 |
enum E { A, B, C } |
| 898 |
|
| 899 |
E e1 = E.A; |
| 900 |
E e2 = E.B; |
| 901 |
|
| 902 |
assert(typeid(E).compare(&e1, &e1) == 0); |
| 903 |
assert(typeid(E).compare(&e1, &e2) < 0); |
| 904 |
assert(typeid(E).compare(&e2, &e1) > 0); |
| 905 |
} |
| 906 |
|
| 907 |
override @property size_t tsize() nothrow pure const { return base.tsize; } |
| 908 |
|
| 909 |
@safe unittest |
| 910 |
{ |
| 911 |
enum E { A, B, C } |
| 912 |
enum ES : string { A = "a", B = "b", C = "c"} |
| 913 |
|
| 914 |
assert(typeid(E).tsize == E.sizeof); |
| 915 |
assert(typeid(ES).tsize == ES.sizeof); |
| 916 |
assert(typeid(E).tsize != ES.sizeof); |
| 917 |
} |
| 918 |
|
| 919 |
override void swap(void* p1, void* p2) const { return base.swap(p1, p2); } |
| 920 |
|
| 921 |
@system unittest |
| 922 |
{ |
| 923 |
enum E { A, B, C } |
| 924 |
|
| 925 |
E e1 = E.A; |
| 926 |
E e2 = E.B; |
| 927 |
|
| 928 |
typeid(E).swap(&e1, &e2); |
| 929 |
assert(e1 == E.B); |
| 930 |
assert(e2 == E.A); |
| 931 |
} |
| 932 |
|
| 933 |
override @property inout(TypeInfo) next() nothrow pure inout { return base.next; } |
| 934 |
|
| 935 |
@system unittest |
| 936 |
{ |
| 937 |
enum E { A, B, C } |
| 938 |
|
| 939 |
assert(typeid(E).next is null); |
| 940 |
} |
| 941 |
|
| 942 |
override @property uint flags() nothrow pure const { return base.flags; } |
| 943 |
|
| 944 |
@safe unittest |
| 945 |
{ |
| 946 |
enum E { A, B, C } |
| 947 |
|
| 948 |
assert(typeid(E).flags == 0); |
| 949 |
} |
| 950 |
|
| 951 |
override const(OffsetTypeInfo)[] offTi() const { return base.offTi; } |
| 952 |
|
| 953 |
@system unittest |
| 954 |
{ |
| 955 |
enum E { A, B, C } |
| 956 |
|
| 957 |
assert(typeid(E).offTi is null); |
| 958 |
} |
| 959 |
|
| 960 |
override void destroy(void* p) const { return base.destroy(p); } |
| 961 |
override void postblit(void* p) const { return base.postblit(p); } |
| 962 |
|
| 963 |
override const(void)[] initializer() const |
| 964 |
{ |
| 965 |
return m_init.length ? m_init : base.initializer(); |
| 966 |
} |
| 967 |
|
| 968 |
override @property size_t talign() nothrow pure const { return base.talign; } |
| 969 |
|
| 970 |
version (WithArgTypes) override int argTypes(out TypeInfo arg1, out TypeInfo arg2) |
| 971 |
{ |
| 972 |
return base.argTypes(arg1, arg2); |
| 973 |
} |
| 974 |
|
| 975 |
override @property immutable(void)* rtInfo() const { return base.rtInfo; } |
| 976 |
|
| 977 |
TypeInfo base; |
| 978 |
string name; |
| 979 |
void[] m_init; |
| 980 |
} |
| 981 |
|
| 982 |
@safe unittest |
| 983 |
{ |
| 984 |
enum unittest_sym_name = __traits(identifier, __traits(parent, (){})); |
| 985 |
enum fqn_unittest = "object." ~ unittest_sym_name; // object.__unittest_LX_CY |
| 986 |
|
| 987 |
enum E { A, B, C } |
| 988 |
enum EE { A, B, C } |
| 989 |
|
| 990 |
assert(typeid(E).toString() == fqn_unittest ~ ".E"); |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
@safe unittest // https://issues.dlang.org/show_bug.cgi?id=12233 |
| 995 |
{ |
| 996 |
static assert(is(typeof(TypeInfo.init) == TypeInfo)); |
| 997 |
assert(TypeInfo.init is null); |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
// Please make sure to keep this in sync with TypeInfo_P (src/rt/typeinfo/ti_ptr.d) |
| 1002 |
class TypeInfo_Pointer : TypeInfo |
| 1003 |
{ |
| 1004 |
override string toString() const { return m_next.toString() ~ "*"; } |
| 1005 |
|
| 1006 |
override bool opEquals(Object o) |
| 1007 |
{ |
| 1008 |
if (this is o) |
| 1009 |
return true; |
| 1010 |
auto c = cast(const TypeInfo_Pointer)o; |
| 1011 |
return c && this.m_next == c.m_next; |
| 1012 |
} |
| 1013 |
|
| 1014 |
override size_t getHash(scope const void* p) @trusted const |
| 1015 |
{ |
| 1016 |
size_t addr = cast(size_t) *cast(const void**)p; |
| 1017 |
return addr ^ (addr >> 4); |
| 1018 |
} |
| 1019 |
|
| 1020 |
override bool equals(in void* p1, in void* p2) const |
| 1021 |
{ |
| 1022 |
return *cast(void**)p1 == *cast(void**)p2; |
| 1023 |
} |
| 1024 |
|
| 1025 |
override int compare(in void* p1, in void* p2) const |
| 1026 |
{ |
| 1027 |
const v1 = *cast(void**) p1, v2 = *cast(void**) p2; |
| 1028 |
return (v1 > v2) - (v1 < v2); |
| 1029 |
} |
| 1030 |
|
| 1031 |
override @property size_t tsize() nothrow pure const |
| 1032 |
{ |
| 1033 |
return (void*).sizeof; |
| 1034 |
} |
| 1035 |
|
| 1036 |
override const(void)[] initializer() const @trusted |
| 1037 |
{ |
| 1038 |
return (cast(void *)null)[0 .. (void*).sizeof]; |
| 1039 |
} |
| 1040 |
|
| 1041 |
override void swap(void* p1, void* p2) const |
| 1042 |
{ |
| 1043 |
void* tmp = *cast(void**)p1; |
| 1044 |
*cast(void**)p1 = *cast(void**)p2; |
| 1045 |
*cast(void**)p2 = tmp; |
| 1046 |
} |
| 1047 |
|
| 1048 |
override @property inout(TypeInfo) next() nothrow pure inout { return m_next; } |
| 1049 |
override @property uint flags() nothrow pure const { return 1; } |
| 1050 |
|
| 1051 |
TypeInfo m_next; |
| 1052 |
} |
| 1053 |
|
| 1054 |
class TypeInfo_Array : TypeInfo |
| 1055 |
{ |
| 1056 |
override string toString() const { return value.toString() ~ "[]"; } |
| 1057 |
|
| 1058 |
override bool opEquals(Object o) |
| 1059 |
{ |
| 1060 |
if (this is o) |
| 1061 |
return true; |
| 1062 |
auto c = cast(const TypeInfo_Array)o; |
| 1063 |
return c && this.value == c.value; |
| 1064 |
} |
| 1065 |
|
| 1066 |
override size_t getHash(scope const void* p) @trusted const |
| 1067 |
{ |
| 1068 |
void[] a = *cast(void[]*)p; |
| 1069 |
return getArrayHash(value, a.ptr, a.length); |
| 1070 |
} |
| 1071 |
|
| 1072 |
override bool equals(in void* p1, in void* p2) const |
| 1073 |
{ |
| 1074 |
void[] a1 = *cast(void[]*)p1; |
| 1075 |
void[] a2 = *cast(void[]*)p2; |
| 1076 |
if (a1.length != a2.length) |
| 1077 |
return false; |
| 1078 |
size_t sz = value.tsize; |
| 1079 |
for (size_t i = 0; i < a1.length; i++) |
| 1080 |
{ |
| 1081 |
if (!value.equals(a1.ptr + i * sz, a2.ptr + i * sz)) |
| 1082 |
return false; |
| 1083 |
} |
| 1084 |
return true; |
| 1085 |
} |
| 1086 |
|
| 1087 |
override int compare(in void* p1, in void* p2) const |
| 1088 |
{ |
| 1089 |
void[] a1 = *cast(void[]*)p1; |
| 1090 |
void[] a2 = *cast(void[]*)p2; |
| 1091 |
size_t sz = value.tsize; |
| 1092 |
size_t len = a1.length; |
| 1093 |
|
| 1094 |
if (a2.length < len) |
| 1095 |
len = a2.length; |
| 1096 |
for (size_t u = 0; u < len; u++) |
| 1097 |
{ |
| 1098 |
immutable int result = value.compare(a1.ptr + u * sz, a2.ptr + u * sz); |
| 1099 |
if (result) |
| 1100 |
return result; |
| 1101 |
} |
| 1102 |
return (a1.length > a2.length) - (a1.length < a2.length); |
| 1103 |
} |
| 1104 |
|
| 1105 |
override @property size_t tsize() nothrow pure const |
| 1106 |
{ |
| 1107 |
return (void[]).sizeof; |
| 1108 |
} |
| 1109 |
|
| 1110 |
override const(void)[] initializer() const @trusted |
| 1111 |
{ |
| 1112 |
return (cast(void *)null)[0 .. (void[]).sizeof]; |
| 1113 |
} |
| 1114 |
|
| 1115 |
override void swap(void* p1, void* p2) const |
| 1116 |
{ |
| 1117 |
void[] tmp = *cast(void[]*)p1; |
| 1118 |
*cast(void[]*)p1 = *cast(void[]*)p2; |
| 1119 |
*cast(void[]*)p2 = tmp; |
| 1120 |
} |
| 1121 |
|
| 1122 |
TypeInfo value; |
| 1123 |
|
| 1124 |
override @property inout(TypeInfo) next() nothrow pure inout |
| 1125 |
{ |
| 1126 |
return value; |
| 1127 |
} |
| 1128 |
|
| 1129 |
override @property uint flags() nothrow pure const { return 1; } |
| 1130 |
|
| 1131 |
override @property size_t talign() nothrow pure const |
| 1132 |
{ |
| 1133 |
return (void[]).alignof; |
| 1134 |
} |
| 1135 |
|
| 1136 |
version (WithArgTypes) override int argTypes(out TypeInfo arg1, out TypeInfo arg2) |
| 1137 |
{ |
| 1138 |
arg1 = typeid(size_t); |
| 1139 |
arg2 = typeid(void*); |
| 1140 |
return 0; |
| 1141 |
} |
| 1142 |
|
| 1143 |
override @property immutable(void)* rtInfo() nothrow pure const @safe { return RTInfo!(void[]); } |
| 1144 |
} |
| 1145 |
|
| 1146 |
class TypeInfo_StaticArray : TypeInfo |
| 1147 |
{ |
| 1148 |
override string toString() const |
| 1149 |
{ |
| 1150 |
import core.internal.string : unsignedToTempString; |
| 1151 |
|
| 1152 |
char[20] tmpBuff = void; |
| 1153 |
const lenString = unsignedToTempString(len, tmpBuff); |
| 1154 |
|
| 1155 |
return (() @trusted => cast(string) (value.toString() ~ "[" ~ lenString ~ "]"))(); |
| 1156 |
} |
| 1157 |
|
| 1158 |
override bool opEquals(Object o) |
| 1159 |
{ |
| 1160 |
if (this is o) |
| 1161 |
return true; |
| 1162 |
auto c = cast(const TypeInfo_StaticArray)o; |
| 1163 |
return c && this.len == c.len && |
| 1164 |
this.value == c.value; |
| 1165 |
} |
| 1166 |
|
| 1167 |
override size_t getHash(scope const void* p) @trusted const |
| 1168 |
{ |
| 1169 |
return getArrayHash(value, p, len); |
| 1170 |
} |
| 1171 |
|
| 1172 |
override bool equals(in void* p1, in void* p2) const |
| 1173 |
{ |
| 1174 |
size_t sz = value.tsize; |
| 1175 |
|
| 1176 |
for (size_t u = 0; u < len; u++) |
| 1177 |
{ |
| 1178 |
if (!value.equals(p1 + u * sz, p2 + u * sz)) |
| 1179 |
return false; |
| 1180 |
} |
| 1181 |
return true; |
| 1182 |
} |
| 1183 |
|
| 1184 |
override int compare(in void* p1, in void* p2) const |
| 1185 |
{ |
| 1186 |
size_t sz = value.tsize; |
| 1187 |
|
| 1188 |
for (size_t u = 0; u < len; u++) |
| 1189 |
{ |
| 1190 |
immutable int result = value.compare(p1 + u * sz, p2 + u * sz); |
| 1191 |
if (result) |
| 1192 |
return result; |
| 1193 |
} |
| 1194 |
return 0; |
| 1195 |
} |
| 1196 |
|
| 1197 |
override @property size_t tsize() nothrow pure const |
| 1198 |
{ |
| 1199 |
return len * value.tsize; |
| 1200 |
} |
| 1201 |
|
| 1202 |
override void swap(void* p1, void* p2) const |
| 1203 |
{ |
| 1204 |
import core.stdc.string : memcpy; |
| 1205 |
|
| 1206 |
size_t remaining = value.tsize * len; |
| 1207 |
void[size_t.sizeof * 4] buffer = void; |
| 1208 |
while (remaining > buffer.length) |
| 1209 |
{ |
| 1210 |
memcpy(buffer.ptr, p1, buffer.length); |
| 1211 |
memcpy(p1, p2, buffer.length); |
| 1212 |
memcpy(p2, buffer.ptr, buffer.length); |
| 1213 |
p1 += buffer.length; |
| 1214 |
p2 += buffer.length; |
| 1215 |
remaining -= buffer.length; |
| 1216 |
} |
| 1217 |
memcpy(buffer.ptr, p1, remaining); |
| 1218 |
memcpy(p1, p2, remaining); |
| 1219 |
memcpy(p2, buffer.ptr, remaining); |
| 1220 |
} |
| 1221 |
|
| 1222 |
override const(void)[] initializer() nothrow pure const |
| 1223 |
{ |
| 1224 |
return value.initializer(); |
| 1225 |
} |
| 1226 |
|
| 1227 |
override @property inout(TypeInfo) next() nothrow pure inout { return value; } |
| 1228 |
override @property uint flags() nothrow pure const { return value.flags; } |
| 1229 |
|
| 1230 |
override void destroy(void* p) const |
| 1231 |
{ |
| 1232 |
immutable sz = value.tsize; |
| 1233 |
p += sz * len; |
| 1234 |
foreach (i; 0 .. len) |
| 1235 |
{ |
| 1236 |
p -= sz; |
| 1237 |
value.destroy(p); |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
override void postblit(void* p) const |
| 1242 |
{ |
| 1243 |
immutable sz = value.tsize; |
| 1244 |
foreach (i; 0 .. len) |
| 1245 |
{ |
| 1246 |
value.postblit(p); |
| 1247 |
p += sz; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
TypeInfo value; |
| 1252 |
size_t len; |
| 1253 |
|
| 1254 |
override @property size_t talign() nothrow pure const |
| 1255 |
{ |
| 1256 |
return value.talign; |
| 1257 |
} |
| 1258 |
|
| 1259 |
version (WithArgTypes) override int argTypes(out TypeInfo arg1, out TypeInfo arg2) |
| 1260 |
{ |
| 1261 |
arg1 = typeid(void*); |
| 1262 |
return 0; |
| 1263 |
} |
| 1264 |
|
| 1265 |
// just return the rtInfo of the element, we have no generic type T to run RTInfo!T on |
| 1266 |
override @property immutable(void)* rtInfo() nothrow pure const @safe { return value.rtInfo(); } |
| 1267 |
} |
| 1268 |
|
| 1269 |
// https://issues.dlang.org/show_bug.cgi?id=21315 |
| 1270 |
@system unittest |
| 1271 |
{ |
| 1272 |
int[16] a, b; |
| 1273 |
foreach (int i; 0 .. 16) |
| 1274 |
{ |
| 1275 |
a[i] = i; |
| 1276 |
b[i] = ~i; |
| 1277 |
} |
| 1278 |
typeid(int[16]).swap(&a, &b); |
| 1279 |
foreach (int i; 0 .. 16) |
| 1280 |
{ |
| 1281 |
assert(a[i] == ~i); |
| 1282 |
assert(b[i] == i); |
| 1283 |
} |
| 1284 |
} |
| 1285 |
|
| 1286 |
class TypeInfo_AssociativeArray : TypeInfo |
| 1287 |
{ |
| 1288 |
override string toString() const |
| 1289 |
{ |
| 1290 |
return value.toString() ~ "[" ~ key.toString() ~ "]"; |
| 1291 |
} |
| 1292 |
|
| 1293 |
override bool opEquals(Object o) |
| 1294 |
{ |
| 1295 |
if (this is o) |
| 1296 |
return true; |
| 1297 |
auto c = cast(const TypeInfo_AssociativeArray)o; |
| 1298 |
return c && this.key == c.key && |
| 1299 |
this.value == c.value; |
| 1300 |
} |
| 1301 |
|
| 1302 |
override bool equals(in void* p1, in void* p2) @trusted const |
| 1303 |
{ |
| 1304 |
return !!_aaEqual(this, *cast(const AA*) p1, *cast(const AA*) p2); |
| 1305 |
} |
| 1306 |
|
| 1307 |
override hash_t getHash(scope const void* p) nothrow @trusted const |
| 1308 |
{ |
| 1309 |
return _aaGetHash(cast(AA*)p, this); |
| 1310 |
} |
| 1311 |
|
| 1312 |
// BUG: need to add the rest of the functions |
| 1313 |
|
| 1314 |
override @property size_t tsize() nothrow pure const |
| 1315 |
{ |
| 1316 |
return (char[int]).sizeof; |
| 1317 |
} |
| 1318 |
|
| 1319 |
override const(void)[] initializer() const @trusted |
| 1320 |
{ |
| 1321 |
return (cast(void *)null)[0 .. (char[int]).sizeof]; |
| 1322 |
} |
| 1323 |
|
| 1324 |
override @property inout(TypeInfo) next() nothrow pure inout { return value; } |
| 1325 |
override @property uint flags() nothrow pure const { return 1; } |
| 1326 |
|
| 1327 |
// TypeInfo entry is generated from the type of this template to help rt/aaA.d |
| 1328 |
static struct Entry(K, V) |
| 1329 |
{ |
| 1330 |
K key; |
| 1331 |
V value; |
| 1332 |
} |
| 1333 |
|
| 1334 |
TypeInfo value; |
| 1335 |
TypeInfo key; |
| 1336 |
TypeInfo entry; |
| 1337 |
|
| 1338 |
override @property size_t talign() nothrow pure const |
| 1339 |
{ |
| 1340 |
return (char[int]).alignof; |
| 1341 |
} |
| 1342 |
|
| 1343 |
version (WithArgTypes) override int argTypes(out TypeInfo arg1, out TypeInfo arg2) |
| 1344 |
{ |
| 1345 |
arg1 = typeid(void*); |
| 1346 |
return 0; |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
class TypeInfo_Vector : TypeInfo |
| 1351 |
{ |
| 1352 |
override string toString() const { return "__vector(" ~ base.toString() ~ ")"; } |
| 1353 |
|
| 1354 |
override bool opEquals(Object o) |
| 1355 |
{ |
| 1356 |
if (this is o) |
| 1357 |
return true; |
| 1358 |
auto c = cast(const TypeInfo_Vector)o; |
| 1359 |
return c && this.base == c.base; |
| 1360 |
} |
| 1361 |
|
| 1362 |
override size_t getHash(scope const void* p) const { return base.getHash(p); } |
| 1363 |
override bool equals(in void* p1, in void* p2) const { return base.equals(p1, p2); } |
| 1364 |
override int compare(in void* p1, in void* p2) const { return base.compare(p1, p2); } |
| 1365 |
override @property size_t tsize() nothrow pure const { return base.tsize; } |
| 1366 |
override void swap(void* p1, void* p2) const { return base.swap(p1, p2); } |
| 1367 |
|
| 1368 |
override @property inout(TypeInfo) next() nothrow pure inout { return base.next; } |
| 1369 |
override @property uint flags() nothrow pure const { return 2; /* passed in SIMD register */ } |
| 1370 |
|
| 1371 |
override const(void)[] initializer() nothrow pure const |
| 1372 |
{ |
| 1373 |
return base.initializer(); |
| 1374 |
} |
| 1375 |
|
| 1376 |
override @property size_t talign() nothrow pure const { return 16; } |
| 1377 |
|
| 1378 |
version (WithArgTypes) override int argTypes(out TypeInfo arg1, out TypeInfo arg2) |
| 1379 |
{ |
| 1380 |
return base.argTypes(arg1, arg2); |
| 1381 |
} |
| 1382 |
|
| 1383 |
TypeInfo base; |
| 1384 |
} |
| 1385 |
|
| 1386 |
class TypeInfo_Function : TypeInfo |
| 1387 |
{ |
| 1388 |
override string toString() const pure @trusted |
| 1389 |
{ |
| 1390 |
import core.demangle : demangleType; |
| 1391 |
|
| 1392 |
alias SafeDemangleFunctionType = char[] function (const(char)[] buf, char[] dst = null) @safe nothrow pure; |
| 1393 |
SafeDemangleFunctionType demangle = cast(SafeDemangleFunctionType) &demangleType; |
| 1394 |
|
| 1395 |
return cast(string) demangle(deco); |
| 1396 |
} |
| 1397 |
|
| 1398 |
override bool opEquals(Object o) |
| 1399 |
{ |
| 1400 |
if (this is o) |
| 1401 |
return true; |
| 1402 |
auto c = cast(const TypeInfo_Function)o; |
| 1403 |
return c && this.deco == c.deco; |
| 1404 |
} |
| 1405 |
|
| 1406 |
// BUG: need to add the rest of the functions |
| 1407 |
|
| 1408 |
override @property size_t tsize() nothrow pure const |
| 1409 |
{ |
| 1410 |
return 0; // no size for functions |
| 1411 |
} |
| 1412 |
|
| 1413 |
override const(void)[] initializer() const @safe |
| 1414 |
{ |
| 1415 |
return null; |
| 1416 |
} |
| 1417 |
|
| 1418 |
override @property immutable(void)* rtInfo() nothrow pure const @safe { return rtinfoNoPointers; } |
| 1419 |
|
| 1420 |
TypeInfo next; |
| 1421 |
|
| 1422 |
/** |
| 1423 |
* Mangled function type string |
| 1424 |
*/ |
| 1425 |
string deco; |
| 1426 |
} |
| 1427 |
|
| 1428 |
@safe unittest |
| 1429 |
{ |
| 1430 |
abstract class C |
| 1431 |
{ |
| 1432 |
void func(); |
| 1433 |
void func(int a); |
| 1434 |
int func(int a, int b); |
| 1435 |
} |
| 1436 |
|
| 1437 |
alias functionTypes = typeof(__traits(getVirtualMethods, C, "func")); |
| 1438 |
assert(typeid(functionTypes[0]).toString() == "void function()"); |
| 1439 |
assert(typeid(functionTypes[1]).toString() == "void function(int)"); |
| 1440 |
assert(typeid(functionTypes[2]).toString() == "int function(int, int)"); |
| 1441 |
} |
| 1442 |
|
| 1443 |
@system unittest |
| 1444 |
{ |
| 1445 |
abstract class C |
| 1446 |
{ |
| 1447 |
void func(); |
| 1448 |
void func(int a); |
| 1449 |
} |
| 1450 |
|
| 1451 |
alias functionTypes = typeof(__traits(getVirtualMethods, C, "func")); |
| 1452 |
|
| 1453 |
Object obj = typeid(functionTypes[0]); |
| 1454 |
assert(obj.opEquals(typeid(functionTypes[0]))); |
| 1455 |
assert(typeid(functionTypes[0]) == typeid(functionTypes[0])); |
| 1456 |
assert(typeid(functionTypes[0]) != typeid(functionTypes[1])); |
| 1457 |
|
| 1458 |
assert(typeid(functionTypes[0]).tsize() == 0); |
| 1459 |
assert(typeid(functionTypes[0]).initializer() is null); |
| 1460 |
assert(typeid(functionTypes[0]).rtInfo() is null); |
| 1461 |
} |
| 1462 |
|
| 1463 |
class TypeInfo_Delegate : TypeInfo |
| 1464 |
{ |
| 1465 |
override string toString() const pure @trusted |
| 1466 |
{ |
| 1467 |
import core.demangle : demangleType; |
| 1468 |
|
| 1469 |
alias SafeDemangleFunctionType = char[] function (const(char)[] buf, char[] dst = null) @safe nothrow pure; |
| 1470 |
SafeDemangleFunctionType demangle = cast(SafeDemangleFunctionType) &demangleType; |
| 1471 |
|
| 1472 |
return cast(string) demangle(deco); |
| 1473 |
} |
| 1474 |
|
| 1475 |
@safe unittest |
| 1476 |
{ |
| 1477 |
double sqr(double x) { return x * x; } |
| 1478 |
sqr(double.init); // for coverage completeness |
| 1479 |
|
| 1480 |
auto delegate_str = "double delegate(double) pure nothrow @nogc @safe"; |
| 1481 |
|
| 1482 |
assert(typeid(typeof(&sqr)).toString() == delegate_str); |
| 1483 |
assert(delegate_str.hashOf() == typeid(typeof(&sqr)).hashOf()); |
| 1484 |
assert(typeid(typeof(&sqr)).toHash() == typeid(typeof(&sqr)).hashOf()); |
| 1485 |
|
| 1486 |
int g; |
| 1487 |
|
| 1488 |
alias delegate_type = typeof((int a, int b) => a + b + g); |
| 1489 |
delegate_str = "int delegate(int, int) pure nothrow @nogc @safe"; |
| 1490 |
|
| 1491 |
assert(typeid(delegate_type).toString() == delegate_str); |
| 1492 |
assert(delegate_str.hashOf() == typeid(delegate_type).hashOf()); |
| 1493 |
assert(typeid(delegate_type).toHash() == typeid(delegate_type).hashOf()); |
| 1494 |
} |
| 1495 |
|
| 1496 |
override bool opEquals(Object o) |
| 1497 |
{ |
| 1498 |
if (this is o) |
| 1499 |
return true; |
| 1500 |
auto c = cast(const TypeInfo_Delegate)o; |
| 1501 |
return c && this.deco == c.deco; |
| 1502 |
} |
| 1503 |
|
| 1504 |
@system unittest |
| 1505 |
{ |
| 1506 |
double sqr(double x) { return x * x; } |
| 1507 |
int dbl(int x) { return x + x; } |
| 1508 |
sqr(double.init); // for coverage completeness |
| 1509 |
dbl(int.init); // for coverage completeness |
| 1510 |
|
| 1511 |
Object obj = typeid(typeof(&sqr)); |
| 1512 |
assert(obj.opEquals(typeid(typeof(&sqr)))); |
| 1513 |
assert(typeid(typeof(&sqr)) == typeid(typeof(&sqr))); |
| 1514 |
assert(typeid(typeof(&dbl)) != typeid(typeof(&sqr))); |
| 1515 |
} |
| 1516 |
|
| 1517 |
override size_t getHash(scope const void* p) @trusted const |
| 1518 |
{ |
| 1519 |
return hashOf(*cast(const void delegate() *)p); |
| 1520 |
} |
| 1521 |
|
| 1522 |
override bool equals(in void* p1, in void* p2) const |
| 1523 |
{ |
| 1524 |
auto dg1 = *cast(void delegate()*)p1; |
| 1525 |
auto dg2 = *cast(void delegate()*)p2; |
| 1526 |
return dg1 == dg2; |
| 1527 |
} |
| 1528 |
|
| 1529 |
override int compare(in void* p1, in void* p2) const |
| 1530 |
{ |
| 1531 |
auto dg1 = *cast(void delegate()*)p1; |
| 1532 |
auto dg2 = *cast(void delegate()*)p2; |
| 1533 |
|
| 1534 |
if (dg1 < dg2) |
| 1535 |
return -1; |
| 1536 |
else if (dg1 > dg2) |
| 1537 |
return 1; |
| 1538 |
else |
| 1539 |
return 0; |
| 1540 |
} |
| 1541 |
|
| 1542 |
override @property size_t tsize() nothrow pure const |
| 1543 |
{ |
| 1544 |
alias dg = int delegate(); |
| 1545 |
return dg.sizeof; |
| 1546 |
} |
| 1547 |
|
| 1548 |
override const(void)[] initializer() const @trusted |
| 1549 |
{ |
| 1550 |
return (cast(void *)null)[0 .. (int delegate()).sizeof]; |
| 1551 |
} |
| 1552 |
|
| 1553 |
override @property uint flags() nothrow pure const { return 1; } |
| 1554 |
|
| 1555 |
TypeInfo next; |
| 1556 |
string deco; |
| 1557 |
|
| 1558 |
override @property size_t talign() nothrow pure const |
| 1559 |
{ |
| 1560 |
alias dg = int delegate(); |
| 1561 |
return dg.alignof; |
| 1562 |
} |
| 1563 |
|
| 1564 |
version (WithArgTypes) override int argTypes(out TypeInfo arg1, out TypeInfo arg2) |
| 1565 |
{ |
| 1566 |
arg1 = typeid(void*); |
| 1567 |
arg2 = typeid(void*); |
| 1568 |
return 0; |
| 1569 |
} |
| 1570 |
|
| 1571 |
override @property immutable(void)* rtInfo() nothrow pure const @safe { return RTInfo!(int delegate()); } |
| 1572 |
} |
| 1573 |
|
| 1574 |
private extern (C) Object _d_newclass(const TypeInfo_Class ci); |
| 1575 |
private extern (C) int _d_isbaseof(scope TypeInfo_Class child, |
| 1576 |
scope const TypeInfo_Class parent) @nogc nothrow pure @safe; // rt.cast_ |
| 1577 |
|
| 1578 |
/** |
| 1579 |
* Runtime type information about a class. |
| 1580 |
* Can be retrieved from an object instance by using the |
| 1581 |
* $(DDSUBLINK spec/expression,typeid_expressions,typeid expression). |
| 1582 |
*/ |
| 1583 |
class TypeInfo_Class : TypeInfo |
| 1584 |
{ |
| 1585 |
override string toString() const pure { return name; } |
| 1586 |
|
| 1587 |
override bool opEquals(const TypeInfo o) const |
| 1588 |
{ |
| 1589 |
if (this is o) |
| 1590 |
return true; |
| 1591 |
auto c = cast(const TypeInfo_Class)o; |
| 1592 |
return c && this.name == c.name; |
| 1593 |
} |
| 1594 |
|
| 1595 |
override size_t getHash(scope const void* p) @trusted const |
| 1596 |
{ |
| 1597 |
auto o = *cast(Object*)p; |
| 1598 |
return o ? o.toHash() : 0; |
| 1599 |
} |
| 1600 |
|
| 1601 |
override bool equals(in void* p1, in void* p2) const |
| 1602 |
{ |
| 1603 |
Object o1 = *cast(Object*)p1; |
| 1604 |
Object o2 = *cast(Object*)p2; |
| 1605 |
|
| 1606 |
return (o1 is o2) || (o1 && o1.opEquals(o2)); |
| 1607 |
} |
| 1608 |
|
| 1609 |
override int compare(in void* p1, in void* p2) const |
| 1610 |
{ |
| 1611 |
Object o1 = *cast(Object*)p1; |
| 1612 |
Object o2 = *cast(Object*)p2; |
| 1613 |
int c = 0; |
| 1614 |
|
| 1615 |
// Regard null references as always being "less than" |
| 1616 |
if (o1 !is o2) |
| 1617 |
{ |
| 1618 |
if (o1) |
| 1619 |
{ |
| 1620 |
if (!o2) |
| 1621 |
c = 1; |
| 1622 |
else |
| 1623 |
c = o1.opCmp(o2); |
| 1624 |
} |
| 1625 |
else |
| 1626 |
c = -1; |
| 1627 |
} |
| 1628 |
return c; |
| 1629 |
} |
| 1630 |
|
| 1631 |
override @property size_t tsize() nothrow pure const |
| 1632 |
{ |
| 1633 |
return Object.sizeof; |
| 1634 |
} |
| 1635 |
|
| 1636 |
override const(void)[] initializer() nothrow pure const @safe |
| 1637 |
{ |
| 1638 |
return m_init; |
| 1639 |
} |
| 1640 |
|
| 1641 |
override @property uint flags() nothrow pure const { return 1; } |
| 1642 |
|
| 1643 |
override @property const(OffsetTypeInfo)[] offTi() nothrow pure const |
| 1644 |
{ |
| 1645 |
return m_offTi; |
| 1646 |
} |
| 1647 |
|
| 1648 |
final @property auto info() @safe @nogc nothrow pure const return { return this; } |
| 1649 |
final @property auto typeinfo() @safe @nogc nothrow pure const return { return this; } |
| 1650 |
|
| 1651 |
byte[] m_init; /** class static initializer |
| 1652 |
* (init.length gives size in bytes of class) |
| 1653 |
*/ |
| 1654 |
string name; /// class name |
| 1655 |
void*[] vtbl; /// virtual function pointer table |
| 1656 |
Interface[] interfaces; /// interfaces this class implements |
| 1657 |
TypeInfo_Class base; /// base class |
| 1658 |
void* destructor; |
| 1659 |
void function(Object) classInvariant; |
| 1660 |
enum ClassFlags : ushort |
| 1661 |
{ |
| 1662 |
isCOMclass = 0x1, |
| 1663 |
noPointers = 0x2, |
| 1664 |
hasOffTi = 0x4, |
| 1665 |
hasCtor = 0x8, |
| 1666 |
hasGetMembers = 0x10, |
| 1667 |
hasTypeInfo = 0x20, |
| 1668 |
isAbstract = 0x40, |
| 1669 |
isCPPclass = 0x80, |
| 1670 |
hasDtor = 0x100, |
| 1671 |
hasNameSig = 0x200, |
| 1672 |
} |
| 1673 |
ClassFlags m_flags; |
| 1674 |
ushort depth; /// inheritance distance from Object |
| 1675 |
void* deallocator; |
| 1676 |
OffsetTypeInfo[] m_offTi; |
| 1677 |
void function(Object) defaultConstructor; // default Constructor |
| 1678 |
|
| 1679 |
immutable(void)* m_RTInfo; // data for precise GC |
| 1680 |
override @property immutable(void)* rtInfo() const { return m_RTInfo; } |
| 1681 |
|
| 1682 |
uint[4] nameSig; /// unique signature for `name` |
| 1683 |
|
| 1684 |
/** |
| 1685 |
* Search all modules for TypeInfo_Class corresponding to classname. |
| 1686 |
* Returns: null if not found |
| 1687 |
*/ |
| 1688 |
static const(TypeInfo_Class) find(const scope char[] classname) |
| 1689 |
{ |
| 1690 |
foreach (m; ModuleInfo) |
| 1691 |
{ |
| 1692 |
if (m) |
| 1693 |
{ |
| 1694 |
//writefln("module %s, %d", m.name, m.localClasses.length); |
| 1695 |
foreach (c; m.localClasses) |
| 1696 |
{ |
| 1697 |
if (c is null) |
| 1698 |
continue; |
| 1699 |
//writefln("\tclass %s", c.name); |
| 1700 |
if (c.name == classname) |
| 1701 |
return c; |
| 1702 |
} |
| 1703 |
} |
| 1704 |
} |
| 1705 |
return null; |
| 1706 |
} |
| 1707 |
|
| 1708 |
/** |
| 1709 |
* Create instance of Object represented by 'this'. |
| 1710 |
*/ |
| 1711 |
Object create() const |
| 1712 |
{ |
| 1713 |
if (m_flags & 8 && !defaultConstructor) |
| 1714 |
return null; |
| 1715 |
if (m_flags & 64) // abstract |
| 1716 |
return null; |
| 1717 |
Object o = _d_newclass(this); |
| 1718 |
if (m_flags & 8 && defaultConstructor) |
| 1719 |
{ |
| 1720 |
defaultConstructor(o); |
| 1721 |
} |
| 1722 |
return o; |
| 1723 |
} |
| 1724 |
|
| 1725 |
/** |
| 1726 |
* Returns true if the class described by `child` derives from or is |
| 1727 |
* the class described by this `TypeInfo_Class`. Always returns false |
| 1728 |
* if the argument is null. |
| 1729 |
* |
| 1730 |
* Params: |
| 1731 |
* child = TypeInfo for some class |
| 1732 |
* Returns: |
| 1733 |
* true if the class described by `child` derives from or is the |
| 1734 |
* class described by this `TypeInfo_Class`. |
| 1735 |
*/ |
| 1736 |
final bool isBaseOf(scope const TypeInfo_Class child) const @nogc nothrow pure @trusted |
| 1737 |
{ |
| 1738 |
if (m_init.length) |
| 1739 |
{ |
| 1740 |
// If this TypeInfo_Class represents an actual class we only need |
| 1741 |
// to check the child and its direct ancestors. |
| 1742 |
for (auto ti = cast() child; ti !is null; ti = ti.base) |
| 1743 |
if (ti is this) |
| 1744 |
return true; |
| 1745 |
return false; |
| 1746 |
} |
| 1747 |
else |
| 1748 |
{ |
| 1749 |
// If this TypeInfo_Class is the .info field of a TypeInfo_Interface |
| 1750 |
// we also need to recursively check the child's interfaces. |
| 1751 |
return child !is null && _d_isbaseof(cast() child, this); |
| 1752 |
} |
| 1753 |
} |
| 1754 |
} |
| 1755 |
|
| 1756 |
alias ClassInfo = TypeInfo_Class; |
| 1757 |
|
| 1758 |
@safe unittest |
| 1759 |
{ |
| 1760 |
// Bugzilla 14401 |
| 1761 |
static class X |
| 1762 |
{ |
| 1763 |
int a; |
| 1764 |
} |
| 1765 |
|
| 1766 |
assert(typeid(X).initializer is typeid(X).m_init); |
| 1767 |
assert(typeid(X).initializer.length == typeid(const(X)).initializer.length); |
| 1768 |
assert(typeid(X).initializer.length == typeid(shared(X)).initializer.length); |
| 1769 |
assert(typeid(X).initializer.length == typeid(immutable(X)).initializer.length); |
| 1770 |
} |
| 1771 |
|
| 1772 |
class TypeInfo_Interface : TypeInfo |
| 1773 |
{ |
| 1774 |
override string toString() const pure { return info.name; } |
| 1775 |
|
| 1776 |
override bool opEquals(Object o) |
| 1777 |
{ |
| 1778 |
if (this is o) |
| 1779 |
return true; |
| 1780 |
auto c = cast(const TypeInfo_Interface)o; |
| 1781 |
return c && this.info.name == typeid(c).name; |
| 1782 |
} |
| 1783 |
|
| 1784 |
override size_t getHash(scope const void* p) @trusted const |
| 1785 |
{ |
| 1786 |
if (!*cast(void**)p) |
| 1787 |
{ |
| 1788 |
return 0; |
| 1789 |
} |
| 1790 |
Interface* pi = **cast(Interface ***)*cast(void**)p; |
| 1791 |
Object o = cast(Object)(*cast(void**)p - pi.offset); |
| 1792 |
assert(o); |
| 1793 |
return o.toHash(); |
| 1794 |
} |
| 1795 |
|
| 1796 |
override bool equals(in void* p1, in void* p2) const |
| 1797 |
{ |
| 1798 |
Interface* pi = **cast(Interface ***)*cast(void**)p1; |
| 1799 |
Object o1 = cast(Object)(*cast(void**)p1 - pi.offset); |
| 1800 |
pi = **cast(Interface ***)*cast(void**)p2; |
| 1801 |
Object o2 = cast(Object)(*cast(void**)p2 - pi.offset); |
| 1802 |
|
| 1803 |
return o1 == o2 || (o1 && o1.opCmp(o2) == 0); |
| 1804 |
} |
| 1805 |
|
| 1806 |
override int compare(in void* p1, in void* p2) const |
| 1807 |
{ |
| 1808 |
Interface* pi = **cast(Interface ***)*cast(void**)p1; |
| 1809 |
Object o1 = cast(Object)(*cast(void**)p1 - pi.offset); |
| 1810 |
pi = **cast(Interface ***)*cast(void**)p2; |
| 1811 |
Object o2 = cast(Object)(*cast(void**)p2 - pi.offset); |
| 1812 |
int c = 0; |
| 1813 |
|
| 1814 |
// Regard null references as always being "less than" |
| 1815 |
if (o1 != o2) |
| 1816 |
{ |
| 1817 |
if (o1) |
| 1818 |
{ |
| 1819 |
if (!o2) |
| 1820 |
c = 1; |
| 1821 |
else |
| 1822 |
c = o1.opCmp(o2); |
| 1823 |
} |
| 1824 |
else |
| 1825 |
c = -1; |
| 1826 |
} |
| 1827 |
return c; |
| 1828 |
} |
| 1829 |
|
| 1830 |
override @property size_t tsize() nothrow pure const |
| 1831 |
{ |
| 1832 |
return Object.sizeof; |
| 1833 |
} |
| 1834 |
|
| 1835 |
override const(void)[] initializer() const @trusted |
| 1836 |
{ |
| 1837 |
return (cast(void *)null)[0 .. Object.sizeof]; |
| 1838 |
} |
| 1839 |
|
| 1840 |
override @property uint flags() nothrow pure const { return 1; } |
| 1841 |
|
| 1842 |
TypeInfo_Class info; |
| 1843 |
|
| 1844 |
/** |
| 1845 |
* Returns true if the class described by `child` derives from the |
| 1846 |
* interface described by this `TypeInfo_Interface`. Always returns |
| 1847 |
* false if the argument is null. |
| 1848 |
* |
| 1849 |
* Params: |
| 1850 |
* child = TypeInfo for some class |
| 1851 |
* Returns: |
| 1852 |
* true if the class described by `child` derives from the |
| 1853 |
* interface described by this `TypeInfo_Interface`. |
| 1854 |
*/ |
| 1855 |
final bool isBaseOf(scope const TypeInfo_Class child) const @nogc nothrow pure @trusted |
| 1856 |
{ |
| 1857 |
return child !is null && _d_isbaseof(cast() child, this.info); |
| 1858 |
} |
| 1859 |
|
| 1860 |
/** |
| 1861 |
* Returns true if the interface described by `child` derives from |
| 1862 |
* or is the interface described by this `TypeInfo_Interface`. |
| 1863 |
* Always returns false if the argument is null. |
| 1864 |
* |
| 1865 |
* Params: |
| 1866 |
* child = TypeInfo for some interface |
| 1867 |
* Returns: |
| 1868 |
* true if the interface described by `child` derives from or is |
| 1869 |
* the interface described by this `TypeInfo_Interface`. |
| 1870 |
*/ |
| 1871 |
final bool isBaseOf(scope const TypeInfo_Interface child) const @nogc nothrow pure @trusted |
| 1872 |
{ |
| 1873 |
return child !is null && _d_isbaseof(cast() child.info, this.info); |
| 1874 |
} |
| 1875 |
} |
| 1876 |
|
| 1877 |
@safe unittest |
| 1878 |
{ |
| 1879 |
enum unittest_sym_name = __traits(identifier, __traits(parent, (){})); |
| 1880 |
enum fqn_unittest = "object." ~ unittest_sym_name; // object.__unittest_LX_CY |
| 1881 |
|
| 1882 |
interface I {} |
| 1883 |
|
| 1884 |
assert(fqn_unittest ~ ".I" == typeid(I).info.name); |
| 1885 |
assert((fqn_unittest ~ ".I").hashOf() == typeid(I).hashOf()); |
| 1886 |
assert(typeid(I).toHash() == typeid(I).hashOf()); |
| 1887 |
} |
| 1888 |
|
| 1889 |
class TypeInfo_Struct : TypeInfo |
| 1890 |
{ |
| 1891 |
override string toString() const { return name; } |
| 1892 |
|
| 1893 |
override size_t toHash() const |
| 1894 |
{ |
| 1895 |
return hashOf(this.mangledName); |
| 1896 |
} |
| 1897 |
|
| 1898 |
override bool opEquals(Object o) |
| 1899 |
{ |
| 1900 |
if (this is o) |
| 1901 |
return true; |
| 1902 |
auto s = cast(const TypeInfo_Struct)o; |
| 1903 |
return s && this.mangledName == s.mangledName; |
| 1904 |
} |
| 1905 |
|
| 1906 |
override size_t getHash(scope const void* p) @trusted pure nothrow const |
| 1907 |
{ |
| 1908 |
assert(p); |
| 1909 |
if (xtoHash) |
| 1910 |
{ |
| 1911 |
return (*xtoHash)(p); |
| 1912 |
} |
| 1913 |
else |
| 1914 |
{ |
| 1915 |
return hashOf(p[0 .. initializer().length]); |
| 1916 |
} |
| 1917 |
} |
| 1918 |
|
| 1919 |
override bool equals(in void* p1, in void* p2) @trusted pure nothrow const |
| 1920 |
{ |
| 1921 |
import core.stdc.string : memcmp; |
| 1922 |
|
| 1923 |
if (!p1 || !p2) |
| 1924 |
return false; |
| 1925 |
else if (xopEquals) |
| 1926 |
{ |
| 1927 |
const dg = _memberFunc(p1, xopEquals); |
| 1928 |
return dg.xopEquals(p2); |
| 1929 |
} |
| 1930 |
else if (p1 == p2) |
| 1931 |
return true; |
| 1932 |
else |
| 1933 |
// BUG: relies on the GC not moving objects |
| 1934 |
return memcmp(p1, p2, initializer().length) == 0; |
| 1935 |
} |
| 1936 |
|
| 1937 |
override int compare(in void* p1, in void* p2) @trusted pure nothrow const |
| 1938 |
{ |
| 1939 |
import core.stdc.string : memcmp; |
| 1940 |
|
| 1941 |
// Regard null references as always being "less than" |
| 1942 |
if (p1 != p2) |
| 1943 |
{ |
| 1944 |
if (p1) |
| 1945 |
{ |
| 1946 |
if (!p2) |
| 1947 |
return true; |
| 1948 |
else if (xopCmp) |
| 1949 |
{ |
| 1950 |
const dg = _memberFunc(p1, xopCmp); |
| 1951 |
return dg.xopCmp(p2); |
| 1952 |
} |
| 1953 |
else |
| 1954 |
// BUG: relies on the GC not moving objects |
| 1955 |
return memcmp(p1, p2, initializer().length); |
| 1956 |
} |
| 1957 |
else |
| 1958 |
return -1; |
| 1959 |
} |
| 1960 |
return 0; |
| 1961 |
} |
| 1962 |
|
| 1963 |
override @property size_t tsize() nothrow pure const |
| 1964 |
{ |
| 1965 |
return initializer().length; |
| 1966 |
} |
| 1967 |
|
| 1968 |
override const(void)[] initializer() nothrow pure const @safe |
| 1969 |
{ |
| 1970 |
return m_init; |
| 1971 |
} |
| 1972 |
|
| 1973 |
override @property uint flags() nothrow pure const { return m_flags; } |
| 1974 |
|
| 1975 |
override @property size_t talign() nothrow pure const { return m_align; } |
| 1976 |
|
| 1977 |
final override void destroy(void* p) const |
| 1978 |
{ |
| 1979 |
if (xdtor) |
| 1980 |
{ |
| 1981 |
if (m_flags & StructFlags.isDynamicType) |
| 1982 |
(*xdtorti)(p, this); |
| 1983 |
else |
| 1984 |
(*xdtor)(p); |
| 1985 |
} |
| 1986 |
} |
| 1987 |
|
| 1988 |
override void postblit(void* p) const |
| 1989 |
{ |
| 1990 |
if (xpostblit) |
| 1991 |
(*xpostblit)(p); |
| 1992 |
} |
| 1993 |
|
| 1994 |
string mangledName; |
| 1995 |
|
| 1996 |
final @property string name() nothrow const @trusted |
| 1997 |
{ |
| 1998 |
import core.demangle : demangleType; |
| 1999 |
|
| 2000 |
if (mangledName is null) // e.g., opaque structs |
| 2001 |
return null; |
| 2002 |
|
| 2003 |
const key = cast(const void*) this; // faster lookup than TypeInfo_Struct, at the cost of potential duplicates per binary |
| 2004 |
static string[typeof(key)] demangledNamesCache; // per thread |
| 2005 |
|
| 2006 |
// not nothrow: |
| 2007 |
//return demangledNamesCache.require(key, cast(string) demangleType(mangledName)); |
| 2008 |
|
| 2009 |
if (auto pDemangled = key in demangledNamesCache) |
| 2010 |
return *pDemangled; |
| 2011 |
|
| 2012 |
const demangled = cast(string) demangleType(mangledName); |
| 2013 |
demangledNamesCache[key] = demangled; |
| 2014 |
return demangled; |
| 2015 |
} |
| 2016 |
|
| 2017 |
void[] m_init; // initializer; m_init.ptr == null if 0 initialize |
| 2018 |
|
| 2019 |
@safe pure nothrow |
| 2020 |
{ |
| 2021 |
size_t function(in void*) xtoHash; |
| 2022 |
bool function(in void*, in void*) xopEquals; |
| 2023 |
int function(in void*, in void*) xopCmp; |
| 2024 |
string function(in void*) xtoString; |
| 2025 |
|
| 2026 |
enum StructFlags : uint |
| 2027 |
{ |
| 2028 |
hasPointers = 0x1, |
| 2029 |
isDynamicType = 0x2, // built at runtime, needs type info in xdtor |
| 2030 |
} |
| 2031 |
StructFlags m_flags; |
| 2032 |
} |
| 2033 |
union |
| 2034 |
{ |
| 2035 |
void function(void*) xdtor; |
| 2036 |
void function(void*, const TypeInfo_Struct ti) xdtorti; |
| 2037 |
} |
| 2038 |
void function(void*) xpostblit; |
| 2039 |
|
| 2040 |
uint m_align; |
| 2041 |
|
| 2042 |
override @property immutable(void)* rtInfo() nothrow pure const @safe { return m_RTInfo; } |
| 2043 |
|
| 2044 |
version (WithArgTypes) |
| 2045 |
{ |
| 2046 |
override int argTypes(out TypeInfo arg1, out TypeInfo arg2) |
| 2047 |
{ |
| 2048 |
arg1 = m_arg1; |
| 2049 |
arg2 = m_arg2; |
| 2050 |
return 0; |
| 2051 |
} |
| 2052 |
TypeInfo m_arg1; |
| 2053 |
TypeInfo m_arg2; |
| 2054 |
} |
| 2055 |
immutable(void)* m_RTInfo; // data for precise GC |
| 2056 |
|
| 2057 |
// The xopEquals and xopCmp members are function pointers to member |
| 2058 |
// functions, which is not guaranteed to share the same ABI, as it is not |
| 2059 |
// known whether the `this` parameter is the first or second argument. |
| 2060 |
// This wrapper is to convert it to a delegate which will always pass the |
| 2061 |
// `this` parameter in the correct way. |
| 2062 |
private struct _memberFunc |
| 2063 |
{ |
| 2064 |
union |
| 2065 |
{ |
| 2066 |
struct // delegate |
| 2067 |
{ |
| 2068 |
const void* ptr; |
| 2069 |
const void* funcptr; |
| 2070 |
} |
| 2071 |
@safe pure nothrow |
| 2072 |
{ |
| 2073 |
bool delegate(in void*) xopEquals; |
| 2074 |
int delegate(in void*) xopCmp; |
| 2075 |
} |
| 2076 |
} |
| 2077 |
} |
| 2078 |
} |
| 2079 |
|
| 2080 |
@system unittest |
| 2081 |
{ |
| 2082 |
struct S |
| 2083 |
{ |
| 2084 |
bool opEquals(ref const S rhs) const |
| 2085 |
{ |
| 2086 |
return false; |
| 2087 |
} |
| 2088 |
} |
| 2089 |
S s; |
| 2090 |
assert(!typeid(S).equals(&s, &s)); |
| 2091 |
} |
| 2092 |
|
| 2093 |
class TypeInfo_Tuple : TypeInfo |
| 2094 |
{ |
| 2095 |
TypeInfo[] elements; |
| 2096 |
|
| 2097 |
override string toString() const |
| 2098 |
{ |
| 2099 |
string s = "("; |
| 2100 |
foreach (i, element; elements) |
| 2101 |
{ |
| 2102 |
if (i) |
| 2103 |
s ~= ','; |
| 2104 |
s ~= element.toString(); |
| 2105 |
} |
| 2106 |
s ~= ")"; |
| 2107 |
return s; |
| 2108 |
} |
| 2109 |
|
| 2110 |
override bool opEquals(Object o) |
| 2111 |
{ |
| 2112 |
if (this is o) |
| 2113 |
return true; |
| 2114 |
|
| 2115 |
auto t = cast(const TypeInfo_Tuple)o; |
| 2116 |
if (t && elements.length == t.elements.length) |
| 2117 |
{ |
| 2118 |
for (size_t i = 0; i < elements.length; i++) |
| 2119 |
{ |
| 2120 |
if (elements[i] != t.elements[i]) |
| 2121 |
return false; |
| 2122 |
} |
| 2123 |
return true; |
| 2124 |
} |
| 2125 |
return false; |
| 2126 |
} |
| 2127 |
|
| 2128 |
override size_t getHash(scope const void* p) const |
| 2129 |
{ |
| 2130 |
assert(0); |
| 2131 |
} |
| 2132 |
|
| 2133 |
override bool equals(in void* p1, in void* p2) const |
| 2134 |
{ |
| 2135 |
assert(0); |
| 2136 |
} |
| 2137 |
|
| 2138 |
override int compare(in void* p1, in void* p2) const |
| 2139 |
{ |
| 2140 |
assert(0); |
| 2141 |
} |
| 2142 |
|
| 2143 |
override @property size_t tsize() nothrow pure const |
| 2144 |
{ |
| 2145 |
assert(0); |
| 2146 |
} |
| 2147 |
|
| 2148 |
override const(void)[] initializer() const @trusted |
| 2149 |
{ |
| 2150 |
assert(0); |
| 2151 |
} |
| 2152 |
|
| 2153 |
override void swap(void* p1, void* p2) const |
| 2154 |
{ |
| 2155 |
assert(0); |
| 2156 |
} |
| 2157 |
|
| 2158 |
override void destroy(void* p) const |
| 2159 |
{ |
| 2160 |
assert(0); |
| 2161 |
} |
| 2162 |
|
| 2163 |
override void postblit(void* p) const |
| 2164 |
{ |
| 2165 |
assert(0); |
| 2166 |
} |
| 2167 |
|
| 2168 |
override @property size_t talign() nothrow pure const |
| 2169 |
{ |
| 2170 |
assert(0); |
| 2171 |
} |
| 2172 |
|
| 2173 |
version (WithArgTypes) override int argTypes(out TypeInfo arg1, out TypeInfo arg2) |
| 2174 |
{ |
| 2175 |
assert(0); |
| 2176 |
} |
| 2177 |
} |
| 2178 |
|
| 2179 |
class TypeInfo_Const : TypeInfo |
| 2180 |
{ |
| 2181 |
override string toString() const |
| 2182 |
{ |
| 2183 |
return cast(string) ("const(" ~ base.toString() ~ ")"); |
| 2184 |
} |
| 2185 |
|
| 2186 |
//override bool opEquals(Object o) { return base.opEquals(o); } |
| 2187 |
override bool opEquals(Object o) |
| 2188 |
{ |
| 2189 |
if (this is o) |
| 2190 |
return true; |
| 2191 |
|
| 2192 |
if (typeid(this) != typeid(o)) |
| 2193 |
return false; |
| 2194 |
|
| 2195 |
auto t = cast(TypeInfo_Const)o; |
| 2196 |
return base.opEquals(t.base); |
| 2197 |
} |
| 2198 |
|
| 2199 |
override size_t getHash(scope const void *p) const { return base.getHash(p); } |
| 2200 |
override bool equals(in void *p1, in void *p2) const { return base.equals(p1, p2); } |
| 2201 |
override int compare(in void *p1, in void *p2) const { return base.compare(p1, p2); } |
| 2202 |
override @property size_t tsize() nothrow pure const { return base.tsize; } |
| 2203 |
override void swap(void *p1, void *p2) const { return base.swap(p1, p2); } |
| 2204 |
|
| 2205 |
override @property inout(TypeInfo) next() nothrow pure inout { return base.next; } |
| 2206 |
override @property uint flags() nothrow pure const { return base.flags; } |
| 2207 |
|
| 2208 |
override const(void)[] initializer() nothrow pure const |
| 2209 |
{ |
| 2210 |
return base.initializer(); |
| 2211 |
} |
| 2212 |
|
| 2213 |
override @property size_t talign() nothrow pure const { return base.talign; } |
| 2214 |
|
| 2215 |
version (WithArgTypes) override int argTypes(out TypeInfo arg1, out TypeInfo arg2) |
| 2216 |
{ |
| 2217 |
return base.argTypes(arg1, arg2); |
| 2218 |
} |
| 2219 |
|
| 2220 |
TypeInfo base; |
| 2221 |
} |
| 2222 |
|
| 2223 |
class TypeInfo_Invariant : TypeInfo_Const |
| 2224 |
{ |
| 2225 |
override string toString() const |
| 2226 |
{ |
| 2227 |
return cast(string) ("immutable(" ~ base.toString() ~ ")"); |
| 2228 |
} |
| 2229 |
} |
| 2230 |
|
| 2231 |
class TypeInfo_Shared : TypeInfo_Const |
| 2232 |
{ |
| 2233 |
override string toString() const |
| 2234 |
{ |
| 2235 |
return cast(string) ("shared(" ~ base.toString() ~ ")"); |
| 2236 |
} |
| 2237 |
} |
| 2238 |
|
| 2239 |
class TypeInfo_Inout : TypeInfo_Const |
| 2240 |
{ |
| 2241 |
override string toString() const |
| 2242 |
{ |
| 2243 |
return cast(string) ("inout(" ~ base.toString() ~ ")"); |
| 2244 |
} |
| 2245 |
} |
| 2246 |
|
| 2247 |
// Contents of Moduleinfo._flags |
| 2248 |
enum |
| 2249 |
{ |
| 2250 |
MIctorstart = 0x1, // we've started constructing it |
| 2251 |
MIctordone = 0x2, // finished construction |
| 2252 |
MIstandalone = 0x4, // module ctor does not depend on other module |
| 2253 |
// ctors being done first |
| 2254 |
MItlsctor = 8, |
| 2255 |
MItlsdtor = 0x10, |
| 2256 |
MIctor = 0x20, |
| 2257 |
MIdtor = 0x40, |
| 2258 |
MIxgetMembers = 0x80, |
| 2259 |
MIictor = 0x100, |
| 2260 |
MIunitTest = 0x200, |
| 2261 |
MIimportedModules = 0x400, |
| 2262 |
MIlocalClasses = 0x800, |
| 2263 |
MIname = 0x1000, |
| 2264 |
} |
| 2265 |
|
| 2266 |
/***************************************** |
| 2267 |
* An instance of ModuleInfo is generated into the object file for each compiled module. |
| 2268 |
* |
| 2269 |
* It provides access to various aspects of the module. |
| 2270 |
* It is not generated for betterC. |
| 2271 |
*/ |
| 2272 |
struct ModuleInfo |
| 2273 |
{ |
| 2274 |
uint _flags; // MIxxxx |
| 2275 |
uint _index; // index into _moduleinfo_array[] |
| 2276 |
|
| 2277 |
version (all) |
| 2278 |
{ |
| 2279 |
deprecated("ModuleInfo cannot be copy-assigned because it is a variable-sized struct.") |
| 2280 |
void opAssign(const scope ModuleInfo m) { _flags = m._flags; _index = m._index; } |
| 2281 |
} |
| 2282 |
else |
| 2283 |
{ |
| 2284 |
@disable this(); |
| 2285 |
} |
| 2286 |
|
| 2287 |
const: |
| 2288 |
private void* addrOf(int flag) return nothrow pure @nogc |
| 2289 |
in |
| 2290 |
{ |
| 2291 |
assert(flag >= MItlsctor && flag <= MIname); |
| 2292 |
assert(!(flag & (flag - 1)) && !(flag & ~(flag - 1) << 1)); |
| 2293 |
} |
| 2294 |
do |
| 2295 |
{ |
| 2296 |
import core.stdc.string : strlen; |
| 2297 |
|
| 2298 |
void* p = cast(void*)&this + ModuleInfo.sizeof; |
| 2299 |
|
| 2300 |
if (flags & MItlsctor) |
| 2301 |
{ |
| 2302 |
if (flag == MItlsctor) return p; |
| 2303 |
p += typeof(tlsctor).sizeof; |
| 2304 |
} |
| 2305 |
if (flags & MItlsdtor) |
| 2306 |
{ |
| 2307 |
if (flag == MItlsdtor) return p; |
| 2308 |
p += typeof(tlsdtor).sizeof; |
| 2309 |
} |
| 2310 |
if (flags & MIctor) |
| 2311 |
{ |
| 2312 |
if (flag == MIctor) return p; |
| 2313 |
p += typeof(ctor).sizeof; |
| 2314 |
} |
| 2315 |
if (flags & MIdtor) |
| 2316 |
{ |
| 2317 |
if (flag == MIdtor) return p; |
| 2318 |
p += typeof(dtor).sizeof; |
| 2319 |
} |
| 2320 |
if (flags & MIxgetMembers) |
| 2321 |
{ |
| 2322 |
if (flag == MIxgetMembers) return p; |
| 2323 |
p += typeof(xgetMembers).sizeof; |
| 2324 |
} |
| 2325 |
if (flags & MIictor) |
| 2326 |
{ |
| 2327 |
if (flag == MIictor) return p; |
| 2328 |
p += typeof(ictor).sizeof; |
| 2329 |
} |
| 2330 |
if (flags & MIunitTest) |
| 2331 |
{ |
| 2332 |
if (flag == MIunitTest) return p; |
| 2333 |
p += typeof(unitTest).sizeof; |
| 2334 |
} |
| 2335 |
if (flags & MIimportedModules) |
| 2336 |
{ |
| 2337 |
if (flag == MIimportedModules) return p; |
| 2338 |
p += size_t.sizeof + *cast(size_t*)p * typeof(importedModules[0]).sizeof; |
| 2339 |
} |
| 2340 |
if (flags & MIlocalClasses) |
| 2341 |
{ |
| 2342 |
if (flag == MIlocalClasses) return p; |
| 2343 |
p += size_t.sizeof + *cast(size_t*)p * typeof(localClasses[0]).sizeof; |
| 2344 |
} |
| 2345 |
if (true || flags & MIname) // always available for now |
| 2346 |
{ |
| 2347 |
if (flag == MIname) return p; |
| 2348 |
p += strlen(cast(immutable char*)p); |
| 2349 |
} |
| 2350 |
assert(0); |
| 2351 |
} |
| 2352 |
|
| 2353 |
@property uint index() nothrow pure @nogc { return _index; } |
| 2354 |
|
| 2355 |
@property uint flags() nothrow pure @nogc { return _flags; } |
| 2356 |
|
| 2357 |
/************************ |
| 2358 |
* Returns: |
| 2359 |
* module constructor for thread locals, `null` if there isn't one |
| 2360 |
*/ |
| 2361 |
@property void function() tlsctor() nothrow pure @nogc |
| 2362 |
{ |
| 2363 |
return flags & MItlsctor ? *cast(typeof(return)*)addrOf(MItlsctor) : null; |
| 2364 |
} |
| 2365 |
|
| 2366 |
/************************ |
| 2367 |
* Returns: |
| 2368 |
* module destructor for thread locals, `null` if there isn't one |
| 2369 |
*/ |
| 2370 |
@property void function() tlsdtor() nothrow pure @nogc |
| 2371 |
{ |
| 2372 |
return flags & MItlsdtor ? *cast(typeof(return)*)addrOf(MItlsdtor) : null; |
| 2373 |
} |
| 2374 |
|
| 2375 |
/***************************** |
| 2376 |
* Returns: |
| 2377 |
* address of a module's `const(MemberInfo)[] getMembers(string)` function, `null` if there isn't one |
| 2378 |
*/ |
| 2379 |
@property void* xgetMembers() nothrow pure @nogc |
| 2380 |
{ |
| 2381 |
return flags & MIxgetMembers ? *cast(typeof(return)*)addrOf(MIxgetMembers) : null; |
| 2382 |
} |
| 2383 |
|
| 2384 |
/************************ |
| 2385 |
* Returns: |
| 2386 |
* module constructor, `null` if there isn't one |
| 2387 |
*/ |
| 2388 |
@property void function() ctor() nothrow pure @nogc |
| 2389 |
{ |
| 2390 |
return flags & MIctor ? *cast(typeof(return)*)addrOf(MIctor) : null; |
| 2391 |
} |
| 2392 |
|
| 2393 |
/************************ |
| 2394 |
* Returns: |
| 2395 |
* module destructor, `null` if there isn't one |
| 2396 |
*/ |
| 2397 |
@property void function() dtor() nothrow pure @nogc |
| 2398 |
{ |
| 2399 |
return flags & MIdtor ? *cast(typeof(return)*)addrOf(MIdtor) : null; |
| 2400 |
} |
| 2401 |
|
| 2402 |
/************************ |
| 2403 |
* Returns: |
| 2404 |
* module order independent constructor, `null` if there isn't one |
| 2405 |
*/ |
| 2406 |
@property void function() ictor() nothrow pure @nogc |
| 2407 |
{ |
| 2408 |
return flags & MIictor ? *cast(typeof(return)*)addrOf(MIictor) : null; |
| 2409 |
} |
| 2410 |
|
| 2411 |
/************* |
| 2412 |
* Returns: |
| 2413 |
* address of function that runs the module's unittests, `null` if there isn't one |
| 2414 |
*/ |
| 2415 |
@property void function() unitTest() nothrow pure @nogc |
| 2416 |
{ |
| 2417 |
return flags & MIunitTest ? *cast(typeof(return)*)addrOf(MIunitTest) : null; |
| 2418 |
} |
| 2419 |
|
| 2420 |
/**************** |
| 2421 |
* Returns: |
| 2422 |
* array of pointers to the ModuleInfo's of modules imported by this one |
| 2423 |
*/ |
| 2424 |
@property immutable(ModuleInfo*)[] importedModules() return nothrow pure @nogc |
| 2425 |
{ |
| 2426 |
if (flags & MIimportedModules) |
| 2427 |
{ |
| 2428 |
auto p = cast(size_t*)addrOf(MIimportedModules); |
| 2429 |
return (cast(immutable(ModuleInfo*)*)(p + 1))[0 .. *p]; |
| 2430 |
} |
| 2431 |
return null; |
| 2432 |
} |
| 2433 |
|
| 2434 |
/**************** |
| 2435 |
* Returns: |
| 2436 |
* array of TypeInfo_Class references for classes defined in this module |
| 2437 |
*/ |
| 2438 |
@property TypeInfo_Class[] localClasses() return nothrow pure @nogc |
| 2439 |
{ |
| 2440 |
if (flags & MIlocalClasses) |
| 2441 |
{ |
| 2442 |
auto p = cast(size_t*)addrOf(MIlocalClasses); |
| 2443 |
return (cast(TypeInfo_Class*)(p + 1))[0 .. *p]; |
| 2444 |
} |
| 2445 |
return null; |
| 2446 |
} |
| 2447 |
|
| 2448 |
/******************** |
| 2449 |
* Returns: |
| 2450 |
* name of module, `null` if no name |
| 2451 |
*/ |
| 2452 |
@property string name() return nothrow pure @nogc |
| 2453 |
{ |
| 2454 |
import core.stdc.string : strlen; |
| 2455 |
|
| 2456 |
auto p = cast(immutable char*) addrOf(MIname); |
| 2457 |
return p[0 .. strlen(p)]; |
| 2458 |
} |
| 2459 |
|
| 2460 |
static int opApply(scope int delegate(ModuleInfo*) dg) |
| 2461 |
{ |
| 2462 |
import core.internal.traits : externDFunc; |
| 2463 |
alias moduleinfos_apply = externDFunc!("rt.minfo.moduleinfos_apply", |
| 2464 |
int function(scope int delegate(immutable(ModuleInfo*)))); |
| 2465 |
// Bugzilla 13084 - enforcing immutable ModuleInfo would break client code |
| 2466 |
return moduleinfos_apply( |
| 2467 |
(immutable(ModuleInfo*)m) => dg(cast(ModuleInfo*)m)); |
| 2468 |
} |
| 2469 |
} |
| 2470 |
|
| 2471 |
@system unittest |
| 2472 |
{ |
| 2473 |
ModuleInfo* m1; |
| 2474 |
foreach (m; ModuleInfo) |
| 2475 |
{ |
| 2476 |
m1 = m; |
| 2477 |
} |
| 2478 |
} |
| 2479 |
|
| 2480 |
/////////////////////////////////////////////////////////////////////////////// |
| 2481 |
// Throwable |
| 2482 |
/////////////////////////////////////////////////////////////////////////////// |
| 2483 |
|
| 2484 |
|
| 2485 |
/** |
| 2486 |
* The base class of all thrown objects. |
| 2487 |
* |
| 2488 |
* All thrown objects must inherit from Throwable. Class $(D Exception), which |
| 2489 |
* derives from this class, represents the category of thrown objects that are |
| 2490 |
* safe to catch and handle. In principle, one should not catch Throwable |
| 2491 |
* objects that are not derived from $(D Exception), as they represent |
| 2492 |
* unrecoverable runtime errors. Certain runtime guarantees may fail to hold |
| 2493 |
* when these errors are thrown, making it unsafe to continue execution after |
| 2494 |
* catching them. |
| 2495 |
*/ |
| 2496 |
class Throwable : Object |
| 2497 |
{ |
| 2498 |
interface TraceInfo |
| 2499 |
{ |
| 2500 |
int opApply(scope int delegate(ref const(char[]))) const; |
| 2501 |
int opApply(scope int delegate(ref size_t, ref const(char[]))) const; |
| 2502 |
string toString() const; |
| 2503 |
} |
| 2504 |
|
| 2505 |
alias TraceDeallocator = void function(TraceInfo) nothrow; |
| 2506 |
|
| 2507 |
string msg; /// A message describing the error. |
| 2508 |
|
| 2509 |
/** |
| 2510 |
* The _file name of the D source code corresponding with |
| 2511 |
* where the error was thrown from. |
| 2512 |
*/ |
| 2513 |
string file; |
| 2514 |
/** |
| 2515 |
* The _line number of the D source code corresponding with |
| 2516 |
* where the error was thrown from. |
| 2517 |
*/ |
| 2518 |
size_t line; |
| 2519 |
|
| 2520 |
/** |
| 2521 |
* The stack trace of where the error happened. This is an opaque object |
| 2522 |
* that can either be converted to $(D string), or iterated over with $(D |
| 2523 |
* foreach) to extract the items in the stack trace (as strings). |
| 2524 |
*/ |
| 2525 |
TraceInfo info; |
| 2526 |
|
| 2527 |
/** |
| 2528 |
* If set, this is used to deallocate the TraceInfo on destruction. |
| 2529 |
*/ |
| 2530 |
TraceDeallocator infoDeallocator; |
| 2531 |
|
| 2532 |
|
| 2533 |
/** |
| 2534 |
* A reference to the _next error in the list. This is used when a new |
| 2535 |
* $(D Throwable) is thrown from inside a $(D catch) block. The originally |
| 2536 |
* caught $(D Exception) will be chained to the new $(D Throwable) via this |
| 2537 |
* field. |
| 2538 |
*/ |
| 2539 |
private Throwable nextInChain; |
| 2540 |
|
| 2541 |
private uint _refcount; // 0 : allocated by GC |
| 2542 |
// 1 : allocated by _d_newThrowable() |
| 2543 |
// 2.. : reference count + 1 |
| 2544 |
|
| 2545 |
/** |
| 2546 |
* Returns: |
| 2547 |
* A reference to the _next error in the list. This is used when a new |
| 2548 |
* $(D Throwable) is thrown from inside a $(D catch) block. The originally |
| 2549 |
* caught $(D Exception) will be chained to the new $(D Throwable) via this |
| 2550 |
* field. |
| 2551 |
*/ |
| 2552 |
@property inout(Throwable) next() @safe inout return scope pure nothrow @nogc { return nextInChain; } |
| 2553 |
|
| 2554 |
/** |
| 2555 |
* Replace next in chain with `tail`. |
| 2556 |
* Use `chainTogether` instead if at all possible. |
| 2557 |
*/ |
| 2558 |
@property void next(Throwable tail) @safe scope pure nothrow @nogc |
| 2559 |
{ |
| 2560 |
if (tail && tail._refcount) |
| 2561 |
++tail._refcount; // increment the replacement *first* |
| 2562 |
|
| 2563 |
auto n = nextInChain; |
| 2564 |
nextInChain = null; // sever the tail before deleting it |
| 2565 |
|
| 2566 |
if (n && n._refcount) |
| 2567 |
_d_delThrowable(n); // now delete the old tail |
| 2568 |
|
| 2569 |
nextInChain = tail; // and set the new tail |
| 2570 |
} |
| 2571 |
|
| 2572 |
/** |
| 2573 |
* Returns: |
| 2574 |
* mutable reference to the reference count, which is |
| 2575 |
* 0 - allocated by the GC, 1 - allocated by _d_newThrowable(), |
| 2576 |
* and >=2 which is the reference count + 1 |
| 2577 |
* Note: |
| 2578 |
* Marked as `@system` to discourage casual use of it. |
| 2579 |
*/ |
| 2580 |
@system @nogc final pure nothrow ref uint refcount() return { return _refcount; } |
| 2581 |
|
| 2582 |
/** |
| 2583 |
* Loop over the chain of Throwables. |
| 2584 |
*/ |
| 2585 |
int opApply(scope int delegate(Throwable) dg) |
| 2586 |
{ |
| 2587 |
int result = 0; |
| 2588 |
for (Throwable t = this; t; t = t.nextInChain) |
| 2589 |
{ |
| 2590 |
result = dg(t); |
| 2591 |
if (result) |
| 2592 |
break; |
| 2593 |
} |
| 2594 |
return result; |
| 2595 |
} |
| 2596 |
|
| 2597 |
/** |
| 2598 |
* Append `e2` to chain of exceptions that starts with `e1`. |
| 2599 |
* Params: |
| 2600 |
* e1 = start of chain (can be null) |
| 2601 |
* e2 = second part of chain (can be null) |
| 2602 |
* Returns: |
| 2603 |
* Throwable that is at the start of the chain; null if both `e1` and `e2` are null |
| 2604 |
*/ |
| 2605 |
static @__future @system @nogc pure nothrow Throwable chainTogether(return scope Throwable e1, return scope Throwable e2) |
| 2606 |
{ |
| 2607 |
if (!e1) |
| 2608 |
return e2; |
| 2609 |
if (!e2) |
| 2610 |
return e1; |
| 2611 |
if (e2.refcount()) |
| 2612 |
++e2.refcount(); |
| 2613 |
|
| 2614 |
for (auto e = e1; 1; e = e.nextInChain) |
| 2615 |
{ |
| 2616 |
if (!e.nextInChain) |
| 2617 |
{ |
| 2618 |
e.nextInChain = e2; |
| 2619 |
break; |
| 2620 |
} |
| 2621 |
} |
| 2622 |
return e1; |
| 2623 |
} |
| 2624 |
|
| 2625 |
@nogc @safe pure nothrow this(string msg, Throwable nextInChain = null) |
| 2626 |
{ |
| 2627 |
this.msg = msg; |
| 2628 |
this.nextInChain = nextInChain; |
| 2629 |
if (nextInChain && nextInChain._refcount) |
| 2630 |
++nextInChain._refcount; |
| 2631 |
//this.info = _d_traceContext(); |
| 2632 |
} |
| 2633 |
|
| 2634 |
@nogc @safe pure nothrow this(string msg, string file, size_t line, Throwable nextInChain = null) |
| 2635 |
{ |
| 2636 |
this(msg, nextInChain); |
| 2637 |
this.file = file; |
| 2638 |
this.line = line; |
| 2639 |
//this.info = _d_traceContext(); |
| 2640 |
} |
| 2641 |
|
| 2642 |
@trusted nothrow ~this() |
| 2643 |
{ |
| 2644 |
if (nextInChain && nextInChain._refcount) |
| 2645 |
_d_delThrowable(nextInChain); |
| 2646 |
// handle owned traceinfo |
| 2647 |
if (infoDeallocator !is null) |
| 2648 |
{ |
| 2649 |
infoDeallocator(info); |
| 2650 |
info = null; // avoid any kind of dangling pointers if we can help |
| 2651 |
// it. |
| 2652 |
} |
| 2653 |
} |
| 2654 |
|
| 2655 |
/** |
| 2656 |
* Overrides $(D Object.toString) and returns the error message. |
| 2657 |
* Internally this forwards to the $(D toString) overload that |
| 2658 |
* takes a $(D_PARAM sink) delegate. |
| 2659 |
*/ |
| 2660 |
override string toString() |
| 2661 |
{ |
| 2662 |
string s; |
| 2663 |
toString((in buf) { s ~= buf; }); |
| 2664 |
return s; |
| 2665 |
} |
| 2666 |
|
| 2667 |
/** |
| 2668 |
* The Throwable hierarchy uses a toString overload that takes a |
| 2669 |
* $(D_PARAM _sink) delegate to avoid GC allocations, which cannot be |
| 2670 |
* performed in certain error situations. Override this $(D |
| 2671 |
* toString) method to customize the error message. |
| 2672 |
*/ |
| 2673 |
void toString(scope void delegate(in char[]) sink) const |
| 2674 |
{ |
| 2675 |
import core.internal.string : unsignedToTempString; |
| 2676 |
|
| 2677 |
char[20] tmpBuff = void; |
| 2678 |
|
| 2679 |
sink(typeid(this).name); |
| 2680 |
sink("@"); sink(file); |
| 2681 |
sink("("); sink(unsignedToTempString(line, tmpBuff)); sink(")"); |
| 2682 |
|
| 2683 |
if (msg.length) |
| 2684 |
{ |
| 2685 |
sink(": "); sink(msg); |
| 2686 |
} |
| 2687 |
if (info) |
| 2688 |
{ |
| 2689 |
try |
| 2690 |
{ |
| 2691 |
sink("\n----------------"); |
| 2692 |
foreach (t; info) |
| 2693 |
{ |
| 2694 |
sink("\n"); sink(t); |
| 2695 |
} |
| 2696 |
} |
| 2697 |
catch (Throwable) |
| 2698 |
{ |
| 2699 |
// ignore more errors |
| 2700 |
} |
| 2701 |
} |
| 2702 |
} |
| 2703 |
|
| 2704 |
/** |
| 2705 |
* Get the message describing the error. |
| 2706 |
* |
| 2707 |
* This getter is an alternative way to access the Exception's message, |
| 2708 |
* with the added advantage of being override-able in subclasses. |
| 2709 |
* Subclasses are hence free to do their own memory managements without |
| 2710 |
* being tied to the requirement of providing a `string` in a field. |
| 2711 |
* |
| 2712 |
* The default behavior is to return the `Throwable.msg` field. |
| 2713 |
* |
| 2714 |
* Returns: |
| 2715 |
* A message representing the cause of the `Throwable` |
| 2716 |
*/ |
| 2717 |
@__future const(char)[] message() const @safe nothrow |
| 2718 |
{ |
| 2719 |
return this.msg; |
| 2720 |
} |
| 2721 |
} |
| 2722 |
|
| 2723 |
|
| 2724 |
/** |
| 2725 |
* The base class of all errors that are safe to catch and handle. |
| 2726 |
* |
| 2727 |
* In principle, only thrown objects derived from this class are safe to catch |
| 2728 |
* inside a $(D catch) block. Thrown objects not derived from Exception |
| 2729 |
* represent runtime errors that should not be caught, as certain runtime |
| 2730 |
* guarantees may not hold, making it unsafe to continue program execution. |
| 2731 |
*/ |
| 2732 |
class Exception : Throwable |
| 2733 |
{ |
| 2734 |
|
| 2735 |
/** |
| 2736 |
* Creates a new instance of Exception. The nextInChain parameter is used |
| 2737 |
* internally and should always be $(D null) when passed by user code. |
| 2738 |
* This constructor does not automatically throw the newly-created |
| 2739 |
* Exception; the $(D throw) expression should be used for that purpose. |
| 2740 |
*/ |
| 2741 |
@nogc @safe pure nothrow this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null) |
| 2742 |
{ |
| 2743 |
super(msg, file, line, nextInChain); |
| 2744 |
} |
| 2745 |
|
| 2746 |
@nogc @safe pure nothrow this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__) |
| 2747 |
{ |
| 2748 |
super(msg, file, line, nextInChain); |
| 2749 |
} |
| 2750 |
} |
| 2751 |
|
| 2752 |
/// |
| 2753 |
@safe unittest |
| 2754 |
{ |
| 2755 |
bool gotCaught; |
| 2756 |
try |
| 2757 |
{ |
| 2758 |
throw new Exception("msg"); |
| 2759 |
} |
| 2760 |
catch (Exception e) |
| 2761 |
{ |
| 2762 |
gotCaught = true; |
| 2763 |
assert(e.msg == "msg"); |
| 2764 |
} |
| 2765 |
assert(gotCaught); |
| 2766 |
} |
| 2767 |
|
| 2768 |
@system unittest |
| 2769 |
{ |
| 2770 |
{ |
| 2771 |
auto e = new Exception("msg"); |
| 2772 |
assert(e.file == __FILE__); |
| 2773 |
assert(e.line == __LINE__ - 2); |
| 2774 |
assert(e.nextInChain is null); |
| 2775 |
assert(e.msg == "msg"); |
| 2776 |
} |
| 2777 |
|
| 2778 |
{ |
| 2779 |
auto e = new Exception("msg", new Exception("It's an Exception!"), "hello", 42); |
| 2780 |
assert(e.file == "hello"); |
| 2781 |
assert(e.line == 42); |
| 2782 |
assert(e.nextInChain !is null); |
| 2783 |
assert(e.msg == "msg"); |
| 2784 |
} |
| 2785 |
|
| 2786 |
{ |
| 2787 |
auto e = new Exception("msg", "hello", 42, new Exception("It's an Exception!")); |
| 2788 |
assert(e.file == "hello"); |
| 2789 |
assert(e.line == 42); |
| 2790 |
assert(e.nextInChain !is null); |
| 2791 |
assert(e.msg == "msg"); |
| 2792 |
} |
| 2793 |
|
| 2794 |
{ |
| 2795 |
auto e = new Exception("message"); |
| 2796 |
assert(e.message == "message"); |
| 2797 |
} |
| 2798 |
} |
| 2799 |
|
| 2800 |
|
| 2801 |
/** |
| 2802 |
* The base class of all unrecoverable runtime errors. |
| 2803 |
* |
| 2804 |
* This represents the category of $(D Throwable) objects that are $(B not) |
| 2805 |
* safe to catch and handle. In principle, one should not catch Error |
| 2806 |
* objects, as they represent unrecoverable runtime errors. |
| 2807 |
* Certain runtime guarantees may fail to hold when these errors are |
| 2808 |
* thrown, making it unsafe to continue execution after catching them. |
| 2809 |
*/ |
| 2810 |
class Error : Throwable |
| 2811 |
{ |
| 2812 |
/** |
| 2813 |
* Creates a new instance of Error. The nextInChain parameter is used |
| 2814 |
* internally and should always be $(D null) when passed by user code. |
| 2815 |
* This constructor does not automatically throw the newly-created |
| 2816 |
* Error; the $(D throw) statement should be used for that purpose. |
| 2817 |
*/ |
| 2818 |
@nogc @safe pure nothrow this(string msg, Throwable nextInChain = null) |
| 2819 |
{ |
| 2820 |
super(msg, nextInChain); |
| 2821 |
bypassedException = null; |
| 2822 |
} |
| 2823 |
|
| 2824 |
@nogc @safe pure nothrow this(string msg, string file, size_t line, Throwable nextInChain = null) |
| 2825 |
{ |
| 2826 |
super(msg, file, line, nextInChain); |
| 2827 |
bypassedException = null; |
| 2828 |
} |
| 2829 |
|
| 2830 |
/** The first $(D Exception) which was bypassed when this Error was thrown, |
| 2831 |
or $(D null) if no $(D Exception)s were pending. */ |
| 2832 |
Throwable bypassedException; |
| 2833 |
} |
| 2834 |
|
| 2835 |
/// |
| 2836 |
@system unittest |
| 2837 |
{ |
| 2838 |
bool gotCaught; |
| 2839 |
try |
| 2840 |
{ |
| 2841 |
throw new Error("msg"); |
| 2842 |
} |
| 2843 |
catch (Error e) |
| 2844 |
{ |
| 2845 |
gotCaught = true; |
| 2846 |
assert(e.msg == "msg"); |
| 2847 |
} |
| 2848 |
assert(gotCaught); |
| 2849 |
} |
| 2850 |
|
| 2851 |
@safe unittest |
| 2852 |
{ |
| 2853 |
{ |
| 2854 |
auto e = new Error("msg"); |
| 2855 |
assert(e.file is null); |
| 2856 |
assert(e.line == 0); |
| 2857 |
assert(e.nextInChain is null); |
| 2858 |
assert(e.msg == "msg"); |
| 2859 |
assert(e.bypassedException is null); |
| 2860 |
} |
| 2861 |
|
| 2862 |
{ |
| 2863 |
auto e = new Error("msg", new Exception("It's an Exception!")); |
| 2864 |
assert(e.file is null); |
| 2865 |
assert(e.line == 0); |
| 2866 |
assert(e.nextInChain !is null); |
| 2867 |
assert(e.msg == "msg"); |
| 2868 |
assert(e.bypassedException is null); |
| 2869 |
} |
| 2870 |
|
| 2871 |
{ |
| 2872 |
auto e = new Error("msg", "hello", 42, new Exception("It's an Exception!")); |
| 2873 |
assert(e.file == "hello"); |
| 2874 |
assert(e.line == 42); |
| 2875 |
assert(e.nextInChain !is null); |
| 2876 |
assert(e.msg == "msg"); |
| 2877 |
assert(e.bypassedException is null); |
| 2878 |
} |
| 2879 |
} |
| 2880 |
|
| 2881 |
extern (C) |
| 2882 |
{ |
| 2883 |
// from druntime/src/rt/aaA.d |
| 2884 |
|
| 2885 |
private struct AA { void* impl; } |
| 2886 |
// size_t _aaLen(in AA aa) pure nothrow @nogc; |
| 2887 |
private void* _aaGetY(scope AA* paa, const TypeInfo_AssociativeArray ti, const size_t valsz, const scope void* pkey) pure nothrow; |
| 2888 |
private void* _aaGetX(scope AA* paa, const TypeInfo_AssociativeArray ti, const size_t valsz, const scope void* pkey, out bool found) pure nothrow; |
| 2889 |
// inout(void)* _aaGetRvalueX(inout AA aa, in TypeInfo keyti, in size_t valsz, in void* pkey); |
| 2890 |
inout(void[]) _aaValues(inout AA aa, const size_t keysz, const size_t valsz, const TypeInfo tiValueArray) pure nothrow; |
| 2891 |
inout(void[]) _aaKeys(inout AA aa, const size_t keysz, const TypeInfo tiKeyArray) pure nothrow; |
| 2892 |
void* _aaRehash(AA* paa, const scope TypeInfo keyti) pure nothrow; |
| 2893 |
void _aaClear(AA aa) pure nothrow; |
| 2894 |
|
| 2895 |
// alias _dg_t = extern(D) int delegate(void*); |
| 2896 |
// int _aaApply(AA aa, size_t keysize, _dg_t dg); |
| 2897 |
|
| 2898 |
// alias _dg2_t = extern(D) int delegate(void*, void*); |
| 2899 |
// int _aaApply2(AA aa, size_t keysize, _dg2_t dg); |
| 2900 |
|
| 2901 |
private struct AARange { AA impl; size_t idx; } |
| 2902 |
AARange _aaRange(AA aa) pure nothrow @nogc @safe; |
| 2903 |
bool _aaRangeEmpty(AARange r) pure nothrow @nogc @safe; |
| 2904 |
void* _aaRangeFrontKey(AARange r) pure nothrow @nogc @safe; |
| 2905 |
void* _aaRangeFrontValue(AARange r) pure nothrow @nogc @safe; |
| 2906 |
void _aaRangePopFront(ref AARange r) pure nothrow @nogc @safe; |
| 2907 |
|
| 2908 |
int _aaEqual(scope const TypeInfo tiRaw, scope const AA aa1, scope const AA aa2); |
| 2909 |
hash_t _aaGetHash(scope const AA* aa, scope const TypeInfo tiRaw) nothrow; |
| 2910 |
|
| 2911 |
/* |
| 2912 |
_d_assocarrayliteralTX marked as pure, because aaLiteral can be called from pure code. |
| 2913 |
This is a typesystem hole, however this is existing hole. |
| 2914 |
Early compiler didn't check purity of toHash or postblit functions, if key is a UDT thus |
| 2915 |
copiler allowed to create AA literal with keys, which have impure unsafe toHash methods. |
| 2916 |
*/ |
| 2917 |
void* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, void[] values) pure; |
| 2918 |
} |
| 2919 |
|
| 2920 |
void* aaLiteral(Key, Value)(Key[] keys, Value[] values) @trusted pure |
| 2921 |
{ |
| 2922 |
return _d_assocarrayliteralTX(typeid(Value[Key]), *cast(void[]*)&keys, *cast(void[]*)&values); |
| 2923 |
} |
| 2924 |
|
| 2925 |
// Lower an Associative Array to a newaa struct for static initialization. |
| 2926 |
auto _aaAsStruct(K, V)(V[K] aa) @safe |
| 2927 |
{ |
| 2928 |
import core.internal.newaa : makeAA; |
| 2929 |
assert(__ctfe); |
| 2930 |
return makeAA!(K, V)(aa); |
| 2931 |
} |
| 2932 |
|
| 2933 |
alias AssociativeArray(Key, Value) = Value[Key]; |
| 2934 |
|
| 2935 |
/*********************************** |
| 2936 |
* Removes all remaining keys and values from an associative array. |
| 2937 |
* Params: |
| 2938 |
* aa = The associative array. |
| 2939 |
*/ |
| 2940 |
void clear(Value, Key)(Value[Key] aa) @trusted |
| 2941 |
{ |
| 2942 |
_aaClear(*cast(AA *) &aa); |
| 2943 |
} |
| 2944 |
|
| 2945 |
/** ditto */ |
| 2946 |
void clear(Value, Key)(Value[Key]* aa) @trusted |
| 2947 |
{ |
| 2948 |
_aaClear(*cast(AA *) aa); |
| 2949 |
} |
| 2950 |
|
| 2951 |
/// |
| 2952 |
@safe unittest |
| 2953 |
{ |
| 2954 |
auto aa = ["k1": 2]; |
| 2955 |
aa.clear; |
| 2956 |
assert("k1" !in aa); |
| 2957 |
} |
| 2958 |
|
| 2959 |
// Issue 20559 |
| 2960 |
@system unittest |
| 2961 |
{ |
| 2962 |
static class Foo |
| 2963 |
{ |
| 2964 |
int[string] aa; |
| 2965 |
alias aa this; |
| 2966 |
} |
| 2967 |
|
| 2968 |
auto v = new Foo(); |
| 2969 |
v["Hello World"] = 42; |
| 2970 |
v.clear; |
| 2971 |
assert("Hello World" !in v); |
| 2972 |
|
| 2973 |
// Test for T* |
| 2974 |
static assert(!__traits(compiles, (&v).clear)); |
| 2975 |
static assert( __traits(compiles, (*(&v)).clear)); |
| 2976 |
} |
| 2977 |
|
| 2978 |
/*********************************** |
| 2979 |
* Reorganizes the associative array in place so that lookups are more |
| 2980 |
* efficient. |
| 2981 |
* Params: |
| 2982 |
* aa = The associative array. |
| 2983 |
* Returns: |
| 2984 |
* The rehashed associative array. |
| 2985 |
*/ |
| 2986 |
T rehash(T : Value[Key], Value, Key)(T aa) |
| 2987 |
{ |
| 2988 |
_aaRehash(cast(AA*)&aa, typeid(Value[Key])); |
| 2989 |
return aa; |
| 2990 |
} |
| 2991 |
|
| 2992 |
/** ditto */ |
| 2993 |
T rehash(T : Value[Key], Value, Key)(T* aa) |
| 2994 |
{ |
| 2995 |
_aaRehash(cast(AA*)aa, typeid(Value[Key])); |
| 2996 |
return *aa; |
| 2997 |
} |
| 2998 |
|
| 2999 |
/** ditto */ |
| 3000 |
T rehash(T : shared Value[Key], Value, Key)(T aa) |
| 3001 |
{ |
| 3002 |
_aaRehash(cast(AA*)&aa, typeid(Value[Key])); |
| 3003 |
return aa; |
| 3004 |
} |
| 3005 |
|
| 3006 |
/** ditto */ |
| 3007 |
T rehash(T : shared Value[Key], Value, Key)(T* aa) |
| 3008 |
{ |
| 3009 |
_aaRehash(cast(AA*)aa, typeid(Value[Key])); |
| 3010 |
return *aa; |
| 3011 |
} |
| 3012 |
|
| 3013 |
/*********************************** |
| 3014 |
* Creates a new associative array of the same size and copies the contents of |
| 3015 |
* the associative array into it. |
| 3016 |
* Params: |
| 3017 |
* aa = The associative array. |
| 3018 |
*/ |
| 3019 |
V[K] dup(T : V[K], K, V)(T aa) |
| 3020 |
{ |
| 3021 |
//pragma(msg, "K = ", K, ", V = ", V); |
| 3022 |
|
| 3023 |
// Bug10720 - check whether V is copyable |
| 3024 |
static assert(is(typeof({ V v = aa[K.init]; })), |
| 3025 |
"cannot call " ~ T.stringof ~ ".dup because " ~ V.stringof ~ " is not copyable"); |
| 3026 |
|
| 3027 |
V[K] result; |
| 3028 |
|
| 3029 |
//foreach (k, ref v; aa) |
| 3030 |
// result[k] = v; // Bug13701 - won't work if V is not mutable |
| 3031 |
|
| 3032 |
ref V duplicateElem(ref K k, ref const V v) @trusted pure nothrow |
| 3033 |
{ |
| 3034 |
import core.stdc.string : memcpy; |
| 3035 |
|
| 3036 |
void* pv = _aaGetY(cast(AA*)&result, typeid(V[K]), V.sizeof, &k); |
| 3037 |
memcpy(pv, &v, V.sizeof); |
| 3038 |
return *cast(V*)pv; |
| 3039 |
} |
| 3040 |
|
| 3041 |
foreach (k, ref v; aa) |
| 3042 |
{ |
| 3043 |
static if (!__traits(hasPostblit, V)) |
| 3044 |
duplicateElem(k, v); |
| 3045 |
else static if (__traits(isStaticArray, V)) |
| 3046 |
_doPostblit(duplicateElem(k, v)[]); |
| 3047 |
else static if (!is(typeof(v.__xpostblit())) && is(immutable V == immutable UV, UV)) |
| 3048 |
(() @trusted => *cast(UV*) &duplicateElem(k, v))().__xpostblit(); |
| 3049 |
else |
| 3050 |
duplicateElem(k, v).__xpostblit(); |
| 3051 |
} |
| 3052 |
|
| 3053 |
return result; |
| 3054 |
} |
| 3055 |
|
| 3056 |
/** ditto */ |
| 3057 |
V[K] dup(T : V[K], K, V)(T* aa) |
| 3058 |
{ |
| 3059 |
return (*aa).dup; |
| 3060 |
} |
| 3061 |
|
| 3062 |
/// |
| 3063 |
@safe unittest |
| 3064 |
{ |
| 3065 |
auto aa = ["k1": 2]; |
| 3066 |
auto a2 = aa.dup; |
| 3067 |
aa["k2"] = 3; |
| 3068 |
assert("k2" !in a2); |
| 3069 |
} |
| 3070 |
|
| 3071 |
// this should never be made public. |
| 3072 |
private AARange _aaToRange(T: V[K], K, V)(ref T aa) pure nothrow @nogc @safe |
| 3073 |
{ |
| 3074 |
// ensure we are dealing with a genuine AA. |
| 3075 |
static if (is(const(V[K]) == const(T))) |
| 3076 |
alias realAA = aa; |
| 3077 |
else |
| 3078 |
const(V[K]) realAA = aa; |
| 3079 |
return _aaRange(() @trusted { return *cast(AA*)&realAA; } ()); |
| 3080 |
} |
| 3081 |
|
| 3082 |
/*********************************** |
| 3083 |
* Returns a $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) |
| 3084 |
* which will iterate over the keys of the associative array. The keys are |
| 3085 |
* returned by reference. |
| 3086 |
* |
| 3087 |
* If structural changes are made to the array (removing or adding keys), all |
| 3088 |
* ranges previously obtained through this function are invalidated. The |
| 3089 |
* following example program will dereference a null pointer: |
| 3090 |
* |
| 3091 |
*--- |
| 3092 |
* import std.stdio : writeln; |
| 3093 |
* |
| 3094 |
* auto dict = ["k1": 1, "k2": 2]; |
| 3095 |
* auto keyRange = dict.byKey; |
| 3096 |
* dict.clear; |
| 3097 |
* writeln(keyRange.front); // Segmentation fault |
| 3098 |
*--- |
| 3099 |
* |
| 3100 |
* Params: |
| 3101 |
* aa = The associative array. |
| 3102 |
* Returns: |
| 3103 |
* A forward range referencing the keys of the associative array. |
| 3104 |
*/ |
| 3105 |
auto byKey(T : V[K], K, V)(T aa) pure nothrow @nogc @safe |
| 3106 |
{ |
| 3107 |
import core.internal.traits : substInout; |
| 3108 |
|
| 3109 |
static struct Result |
| 3110 |
{ |
| 3111 |
AARange r; |
| 3112 |
|
| 3113 |
pure nothrow @nogc: |
| 3114 |
@property bool empty() @safe { return _aaRangeEmpty(r); } |
| 3115 |
@property ref front() @trusted |
| 3116 |
{ |
| 3117 |
return *cast(substInout!K*) _aaRangeFrontKey(r); |
| 3118 |
} |
| 3119 |
void popFront() @safe { _aaRangePopFront(r); } |
| 3120 |
@property Result save() { return this; } |
| 3121 |
} |
| 3122 |
|
| 3123 |
return Result(_aaToRange(aa)); |
| 3124 |
} |
| 3125 |
|
| 3126 |
/** ditto */ |
| 3127 |
auto byKey(T : V[K], K, V)(T* aa) pure nothrow @nogc |
| 3128 |
{ |
| 3129 |
return (*aa).byKey(); |
| 3130 |
} |
| 3131 |
|
| 3132 |
/// |
| 3133 |
@safe unittest |
| 3134 |
{ |
| 3135 |
auto dict = [1: "v1", 2: "v2"]; |
| 3136 |
int sum; |
| 3137 |
foreach (v; dict.byKey) |
| 3138 |
sum += v; |
| 3139 |
|
| 3140 |
assert(sum == 3); |
| 3141 |
} |
| 3142 |
|
| 3143 |
/*********************************** |
| 3144 |
* Returns a $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) |
| 3145 |
* which will iterate over the values of the associative array. The values are |
| 3146 |
* returned by reference. |
| 3147 |
* |
| 3148 |
* If structural changes are made to the array (removing or adding keys), all |
| 3149 |
* ranges previously obtained through this function are invalidated. The |
| 3150 |
* following example program will dereference a null pointer: |
| 3151 |
* |
| 3152 |
*--- |
| 3153 |
* import std.stdio : writeln; |
| 3154 |
* |
| 3155 |
* auto dict = ["k1": 1, "k2": 2]; |
| 3156 |
* auto valueRange = dict.byValue; |
| 3157 |
* dict.clear; |
| 3158 |
* writeln(valueRange.front); // Segmentation fault |
| 3159 |
*--- |
| 3160 |
* |
| 3161 |
* Params: |
| 3162 |
* aa = The associative array. |
| 3163 |
* Returns: |
| 3164 |
* A forward range referencing the values of the associative array. |
| 3165 |
*/ |
| 3166 |
auto byValue(T : V[K], K, V)(T aa) pure nothrow @nogc @safe |
| 3167 |
{ |
| 3168 |
import core.internal.traits : substInout; |
| 3169 |
|
| 3170 |
static struct Result |
| 3171 |
{ |
| 3172 |
AARange r; |
| 3173 |
|
| 3174 |
pure nothrow @nogc: |
| 3175 |
@property bool empty() @safe { return _aaRangeEmpty(r); } |
| 3176 |
@property ref front() @trusted |
| 3177 |
{ |
| 3178 |
return *cast(substInout!V*) _aaRangeFrontValue(r); |
| 3179 |
} |
| 3180 |
void popFront() @safe { _aaRangePopFront(r); } |
| 3181 |
@property Result save() { return this; } |
| 3182 |
} |
| 3183 |
|
| 3184 |
return Result(_aaToRange(aa)); |
| 3185 |
} |
| 3186 |
|
| 3187 |
/** ditto */ |
| 3188 |
auto byValue(T : V[K], K, V)(T* aa) pure nothrow @nogc |
| 3189 |
{ |
| 3190 |
return (*aa).byValue(); |
| 3191 |
} |
| 3192 |
|
| 3193 |
/// |
| 3194 |
@safe unittest |
| 3195 |
{ |
| 3196 |
auto dict = ["k1": 1, "k2": 2]; |
| 3197 |
int sum; |
| 3198 |
foreach (v; dict.byValue) |
| 3199 |
sum += v; |
| 3200 |
|
| 3201 |
assert(sum == 3); |
| 3202 |
|
| 3203 |
foreach (ref v; dict.byValue) |
| 3204 |
v++; |
| 3205 |
assert(dict == ["k1": 2, "k2": 3]); |
| 3206 |
} |
| 3207 |
|
| 3208 |
/*********************************** |
| 3209 |
* Returns a $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) |
| 3210 |
* which will iterate over the key-value pairs of the associative array. The |
| 3211 |
* returned pairs are represented by an opaque type with `.key` and `.value` |
| 3212 |
* properties for accessing references to the key and value of the pair, |
| 3213 |
* respectively. |
| 3214 |
* |
| 3215 |
* If structural changes are made to the array (removing or adding keys), all |
| 3216 |
* ranges previously obtained through this function are invalidated. The |
| 3217 |
* following example program will dereference a null pointer: |
| 3218 |
* |
| 3219 |
*--- |
| 3220 |
* import std.stdio : writeln; |
| 3221 |
* |
| 3222 |
* auto dict = ["k1": 1, "k2": 2]; |
| 3223 |
* auto kvRange = dict.byKeyValue; |
| 3224 |
* dict.clear; |
| 3225 |
* writeln(kvRange.front.key, ": ", kvRange.front.value); // Segmentation fault |
| 3226 |
*--- |
| 3227 |
* |
| 3228 |
* Note that this is a low-level interface to iterating over the associative |
| 3229 |
* array and is not compatible with the |
| 3230 |
* $(LINK2 $(ROOT_DIR)phobos/std_typecons.html#.Tuple,`Tuple`) type in Phobos. |
| 3231 |
* For compatibility with `Tuple`, use |
| 3232 |
* $(LINK2 $(ROOT_DIR)phobos/std_array.html#.byPair,std.array.byPair) instead. |
| 3233 |
* |
| 3234 |
* Params: |
| 3235 |
* aa = The associative array. |
| 3236 |
* Returns: |
| 3237 |
* A forward range referencing the pairs of the associative array. |
| 3238 |
*/ |
| 3239 |
auto byKeyValue(T : V[K], K, V)(T aa) pure nothrow @nogc @safe |
| 3240 |
{ |
| 3241 |
import core.internal.traits : substInout; |
| 3242 |
|
| 3243 |
static struct Result |
| 3244 |
{ |
| 3245 |
AARange r; |
| 3246 |
|
| 3247 |
pure nothrow @nogc: |
| 3248 |
@property bool empty() @safe { return _aaRangeEmpty(r); } |
| 3249 |
@property auto front() |
| 3250 |
{ |
| 3251 |
static struct Pair |
| 3252 |
{ |
| 3253 |
// We save the pointers here so that the Pair we return |
| 3254 |
// won't mutate when Result.popFront is called afterwards. |
| 3255 |
private void* keyp; |
| 3256 |
private void* valp; |
| 3257 |
|
| 3258 |
@property ref key() inout @trusted |
| 3259 |
{ |
| 3260 |
return *cast(substInout!K*) keyp; |
| 3261 |
} |
| 3262 |
@property ref value() inout @trusted |
| 3263 |
{ |
| 3264 |
return *cast(substInout!V*) valp; |
| 3265 |
} |
| 3266 |
} |
| 3267 |
return Pair(_aaRangeFrontKey(r), |
| 3268 |
_aaRangeFrontValue(r)); |
| 3269 |
} |
| 3270 |
void popFront() @safe { return _aaRangePopFront(r); } |
| 3271 |
@property Result save() { return this; } |
| 3272 |
} |
| 3273 |
|
| 3274 |
return Result(_aaToRange(aa)); |
| 3275 |
} |
| 3276 |
|
| 3277 |
/** ditto */ |
| 3278 |
auto byKeyValue(T : V[K], K, V)(T* aa) pure nothrow @nogc |
| 3279 |
{ |
| 3280 |
return (*aa).byKeyValue(); |
| 3281 |
} |
| 3282 |
|
| 3283 |
/// |
| 3284 |
@safe unittest |
| 3285 |
{ |
| 3286 |
auto dict = ["k1": 1, "k2": 2]; |
| 3287 |
int sum; |
| 3288 |
foreach (e; dict.byKeyValue) |
| 3289 |
{ |
| 3290 |
assert(e.key[1] == e.value + '0'); |
| 3291 |
sum += e.value; |
| 3292 |
} |
| 3293 |
assert(sum == 3); |
| 3294 |
|
| 3295 |
foreach (e; dict.byKeyValue) |
| 3296 |
e.value++; |
| 3297 |
assert(dict == ["k1": 2, "k2": 3]); |
| 3298 |
} |
| 3299 |
|
| 3300 |
/*********************************** |
| 3301 |
* Returns a newly allocated dynamic array containing a copy of the keys from |
| 3302 |
* the associative array. |
| 3303 |
* Params: |
| 3304 |
* aa = The associative array. |
| 3305 |
* Returns: |
| 3306 |
* A dynamic array containing a copy of the keys. |
| 3307 |
*/ |
| 3308 |
Key[] keys(T : Value[Key], Value, Key)(T aa) @property |
| 3309 |
{ |
| 3310 |
// ensure we are dealing with a genuine AA. |
| 3311 |
static if (is(const(Value[Key]) == const(T))) |
| 3312 |
alias realAA = aa; |
| 3313 |
else |
| 3314 |
const(Value[Key]) realAA = aa; |
| 3315 |
auto res = () @trusted { |
| 3316 |
auto a = cast(void[])_aaKeys(*cast(inout(AA)*)&realAA, Key.sizeof, typeid(Key[])); |
| 3317 |
return *cast(Key[]*)&a; |
| 3318 |
}(); |
| 3319 |
static if (__traits(hasPostblit, Key)) |
| 3320 |
_doPostblit(res); |
| 3321 |
return res; |
| 3322 |
} |
| 3323 |
|
| 3324 |
/** ditto */ |
| 3325 |
Key[] keys(T : Value[Key], Value, Key)(T *aa) @property |
| 3326 |
{ |
| 3327 |
return (*aa).keys; |
| 3328 |
} |
| 3329 |
|
| 3330 |
/// |
| 3331 |
@safe unittest |
| 3332 |
{ |
| 3333 |
auto aa = [1: "v1", 2: "v2"]; |
| 3334 |
int sum; |
| 3335 |
foreach (k; aa.keys) |
| 3336 |
sum += k; |
| 3337 |
|
| 3338 |
assert(sum == 3); |
| 3339 |
} |
| 3340 |
|
| 3341 |
@safe unittest |
| 3342 |
{ |
| 3343 |
static struct S |
| 3344 |
{ |
| 3345 |
string str; |
| 3346 |
void[][string] dict; |
| 3347 |
alias dict this; |
| 3348 |
} |
| 3349 |
|
| 3350 |
auto s = S("a"); |
| 3351 |
assert(s.keys.length == 0); |
| 3352 |
} |
| 3353 |
|
| 3354 |
@safe unittest |
| 3355 |
{ |
| 3356 |
@safe static struct Key |
| 3357 |
{ |
| 3358 |
string str; |
| 3359 |
this(this) @safe {} |
| 3360 |
} |
| 3361 |
string[Key] aa; |
| 3362 |
static assert(__traits(compiles, { |
| 3363 |
void test() @safe { |
| 3364 |
const _ = aa.keys; |
| 3365 |
} |
| 3366 |
})); |
| 3367 |
} |
| 3368 |
|
| 3369 |
@safe unittest |
| 3370 |
{ |
| 3371 |
static struct Key |
| 3372 |
{ |
| 3373 |
string str; |
| 3374 |
this(this) @system {} |
| 3375 |
} |
| 3376 |
string[Key] aa; |
| 3377 |
static assert(!__traits(compiles, { |
| 3378 |
void test() @safe { |
| 3379 |
const _ = aa.keys; |
| 3380 |
} |
| 3381 |
})); |
| 3382 |
} |
| 3383 |
|
| 3384 |
/*********************************** |
| 3385 |
* Returns a newly allocated dynamic array containing a copy of the values from |
| 3386 |
* the associative array. |
| 3387 |
* Params: |
| 3388 |
* aa = The associative array. |
| 3389 |
* Returns: |
| 3390 |
* A dynamic array containing a copy of the values. |
| 3391 |
*/ |
| 3392 |
Value[] values(T : Value[Key], Value, Key)(T aa) @property |
| 3393 |
{ |
| 3394 |
// ensure we are dealing with a genuine AA. |
| 3395 |
static if (is(const(Value[Key]) == const(T))) |
| 3396 |
alias realAA = aa; |
| 3397 |
else |
| 3398 |
const(Value[Key]) realAA = aa; |
| 3399 |
auto res = () @trusted { |
| 3400 |
auto a = cast(void[])_aaValues(*cast(inout(AA)*)&realAA, Key.sizeof, Value.sizeof, typeid(Value[])); |
| 3401 |
return *cast(Value[]*)&a; |
| 3402 |
}(); |
| 3403 |
static if (__traits(hasPostblit, Value)) |
| 3404 |
_doPostblit(res); |
| 3405 |
return res; |
| 3406 |
} |
| 3407 |
|
| 3408 |
/** ditto */ |
| 3409 |
Value[] values(T : Value[Key], Value, Key)(T *aa) @property |
| 3410 |
{ |
| 3411 |
return (*aa).values; |
| 3412 |
} |
| 3413 |
|
| 3414 |
/// |
| 3415 |
@safe unittest |
| 3416 |
{ |
| 3417 |
auto aa = ["k1": 1, "k2": 2]; |
| 3418 |
int sum; |
| 3419 |
foreach (e; aa.values) |
| 3420 |
sum += e; |
| 3421 |
|
| 3422 |
assert(sum == 3); |
| 3423 |
} |
| 3424 |
|
| 3425 |
@safe unittest |
| 3426 |
{ |
| 3427 |
static struct S |
| 3428 |
{ |
| 3429 |
string str; |
| 3430 |
void[][string] dict; |
| 3431 |
alias dict this; |
| 3432 |
} |
| 3433 |
|
| 3434 |
auto s = S("a"); |
| 3435 |
assert(s.values.length == 0); |
| 3436 |
} |
| 3437 |
|
| 3438 |
@safe unittest |
| 3439 |
{ |
| 3440 |
@safe static struct Value |
| 3441 |
{ |
| 3442 |
string str; |
| 3443 |
this(this) @safe {} |
| 3444 |
} |
| 3445 |
Value[string] aa; |
| 3446 |
static assert(__traits(compiles, { |
| 3447 |
void test() @safe { |
| 3448 |
const _ = aa.values; |
| 3449 |
} |
| 3450 |
})); |
| 3451 |
} |
| 3452 |
|
| 3453 |
@safe unittest |
| 3454 |
{ |
| 3455 |
static struct Value |
| 3456 |
{ |
| 3457 |
string str; |
| 3458 |
this(this) @system {} |
| 3459 |
} |
| 3460 |
Value[string] aa; |
| 3461 |
static assert(!__traits(compiles, { |
| 3462 |
void test() @safe { |
| 3463 |
const _ = aa.values; |
| 3464 |
} |
| 3465 |
})); |
| 3466 |
} |
| 3467 |
|
| 3468 |
/*********************************** |
| 3469 |
* If `key` is in `aa`, returns corresponding value; otherwise it evaluates and |
| 3470 |
* returns `defaultValue`. |
| 3471 |
* Params: |
| 3472 |
* aa = The associative array. |
| 3473 |
* key = The key. |
| 3474 |
* defaultValue = The default value. |
| 3475 |
* Returns: |
| 3476 |
* The value. |
| 3477 |
*/ |
| 3478 |
inout(V) get(K, V)(inout(V[K]) aa, K key, lazy inout(V) defaultValue) |
| 3479 |
{ |
| 3480 |
auto p = key in aa; |
| 3481 |
return p ? *p : defaultValue; |
| 3482 |
} |
| 3483 |
|
| 3484 |
/** ditto */ |
| 3485 |
inout(V) get(K, V)(inout(V[K])* aa, K key, lazy inout(V) defaultValue) |
| 3486 |
{ |
| 3487 |
return (*aa).get(key, defaultValue); |
| 3488 |
} |
| 3489 |
|
| 3490 |
/// |
| 3491 |
@safe unittest |
| 3492 |
{ |
| 3493 |
auto aa = ["k1": 1]; |
| 3494 |
assert(aa.get("k1", 0) == 1); |
| 3495 |
assert(aa.get("k2", 0) == 0); |
| 3496 |
} |
| 3497 |
|
| 3498 |
/*********************************** |
| 3499 |
* If `key` is in `aa`, returns corresponding value; otherwise it evaluates |
| 3500 |
* `value`, adds it to the associative array and returns it. |
| 3501 |
* Params: |
| 3502 |
* aa = The associative array. |
| 3503 |
* key = The key. |
| 3504 |
* value = The required value. |
| 3505 |
* Returns: |
| 3506 |
* The value. |
| 3507 |
*/ |
| 3508 |
ref V require(K, V)(ref V[K] aa, K key, lazy V value = V.init) |
| 3509 |
{ |
| 3510 |
bool found; |
| 3511 |
// if key is @safe-ly copyable, `require` can infer @safe |
| 3512 |
static if (isSafeCopyable!K) |
| 3513 |
{ |
| 3514 |
auto p = () @trusted |
| 3515 |
{ |
| 3516 |
return cast(V*) _aaGetX(cast(AA*) &aa, typeid(V[K]), V.sizeof, &key, found); |
| 3517 |
} (); |
| 3518 |
} |
| 3519 |
else |
| 3520 |
{ |
| 3521 |
auto p = cast(V*) _aaGetX(cast(AA*) &aa, typeid(V[K]), V.sizeof, &key, found); |
| 3522 |
} |
| 3523 |
if (found) |
| 3524 |
return *p; |
| 3525 |
else |
| 3526 |
{ |
| 3527 |
*p = value; // Not `return (*p = value)` since if `=` is overloaded |
| 3528 |
return *p; // this might not return a ref to the left-hand side. |
| 3529 |
} |
| 3530 |
} |
| 3531 |
|
| 3532 |
/// |
| 3533 |
@safe unittest |
| 3534 |
{ |
| 3535 |
auto aa = ["k1": 1]; |
| 3536 |
assert(aa.require("k1", 0) == 1); |
| 3537 |
assert(aa.require("k2", 0) == 0); |
| 3538 |
assert(aa["k2"] == 0); |
| 3539 |
} |
| 3540 |
|
| 3541 |
// Tests whether T can be @safe-ly copied. Use a union to exclude destructor from the test. |
| 3542 |
private enum bool isSafeCopyable(T) = is(typeof(() @safe { union U { T x; } T *x; auto u = U(*x); })); |
| 3543 |
|
| 3544 |
/*********************************** |
| 3545 |
* Calls `create` if `key` doesn't exist in the associative array, |
| 3546 |
* otherwise calls `update`. |
| 3547 |
* `create` returns a corresponding value for `key`. |
| 3548 |
* `update` accepts a key parameter. If it returns a value, the value is |
| 3549 |
* set for `key`. |
| 3550 |
* Params: |
| 3551 |
* aa = The associative array. |
| 3552 |
* key = The key. |
| 3553 |
* create = The callable to create a value for `key`. |
| 3554 |
* Must return V. |
| 3555 |
* update = The callable to call if `key` exists. |
| 3556 |
* Takes a K argument, returns a V or void. |
| 3557 |
*/ |
| 3558 |
void update(K, V, C, U)(ref V[K] aa, K key, scope C create, scope U update) |
| 3559 |
if (is(typeof(create()) : V) && (is(typeof(update(aa[K.init])) : V) || is(typeof(update(aa[K.init])) == void))) |
| 3560 |
{ |
| 3561 |
bool found; |
| 3562 |
// if key is @safe-ly copyable, `update` may infer @safe |
| 3563 |
static if (isSafeCopyable!K) |
| 3564 |
{ |
| 3565 |
auto p = () @trusted |
| 3566 |
{ |
| 3567 |
return cast(V*) _aaGetX(cast(AA*) &aa, typeid(V[K]), V.sizeof, &key, found); |
| 3568 |
} (); |
| 3569 |
} |
| 3570 |
else |
| 3571 |
{ |
| 3572 |
auto p = cast(V*) _aaGetX(cast(AA*) &aa, typeid(V[K]), V.sizeof, &key, found); |
| 3573 |
} |
| 3574 |
if (!found) |
| 3575 |
*p = create(); |
| 3576 |
else |
| 3577 |
{ |
| 3578 |
static if (is(typeof(update(*p)) == void)) |
| 3579 |
update(*p); |
| 3580 |
else |
| 3581 |
*p = update(*p); |
| 3582 |
} |
| 3583 |
} |
| 3584 |
|
| 3585 |
/// |
| 3586 |
@safe unittest |
| 3587 |
{ |
| 3588 |
int[string] aa; |
| 3589 |
|
| 3590 |
// create |
| 3591 |
aa.update("key", |
| 3592 |
() => 1, |
| 3593 |
(int) {} // not executed |
| 3594 |
); |
| 3595 |
assert(aa["key"] == 1); |
| 3596 |
|
| 3597 |
// update value by ref |
| 3598 |
aa.update("key", |
| 3599 |
() => 0, // not executed |
| 3600 |
(ref int v) { |
| 3601 |
v += 1; |
| 3602 |
}); |
| 3603 |
assert(aa["key"] == 2); |
| 3604 |
|
| 3605 |
// update from return value |
| 3606 |
aa.update("key", |
| 3607 |
() => 0, // not executed |
| 3608 |
(int v) => v * 2 |
| 3609 |
); |
| 3610 |
assert(aa["key"] == 4); |
| 3611 |
|
| 3612 |
// 'update' without changing value |
| 3613 |
aa.update("key", |
| 3614 |
() => 0, // not executed |
| 3615 |
(int) { |
| 3616 |
// do something else |
| 3617 |
}); |
| 3618 |
assert(aa["key"] == 4); |
| 3619 |
} |
| 3620 |
|
| 3621 |
@safe unittest |
| 3622 |
{ |
| 3623 |
static struct S |
| 3624 |
{ |
| 3625 |
int x; |
| 3626 |
@nogc nothrow pure: |
| 3627 |
this(this) @system {} |
| 3628 |
|
| 3629 |
@safe const: |
| 3630 |
// stubs |
| 3631 |
bool opEquals(S rhs) { assert(0); } |
| 3632 |
size_t toHash() const { assert(0); } |
| 3633 |
} |
| 3634 |
|
| 3635 |
int[string] aai; |
| 3636 |
static assert(is(typeof(() @safe { aai.require("a", 1234); }))); |
| 3637 |
static assert(is(typeof(() @safe { aai.update("a", { return 1234; }, (ref int x) { x++; return x; }); }))); |
| 3638 |
|
| 3639 |
S[string] aas; |
| 3640 |
static assert(is(typeof(() { aas.require("a", S(1234)); }))); |
| 3641 |
static assert(is(typeof(() { aas.update("a", { return S(1234); }, (ref S s) { s.x++; return s; }); }))); |
| 3642 |
static assert(!is(typeof(() @safe { aas.update("a", { return S(1234); }, (ref S s) { s.x++; return s; }); }))); |
| 3643 |
|
| 3644 |
int[S] aais; |
| 3645 |
static assert(is(typeof(() { aais.require(S(1234), 1234); }))); |
| 3646 |
static assert(is(typeof(() { aais.update(S(1234), { return 1234; }, (ref int x) { x++; return x; }); }))); |
| 3647 |
static assert(!is(typeof(() @safe { aais.require(S(1234), 1234); }))); |
| 3648 |
static assert(!is(typeof(() @safe { aais.update(S(1234), { return 1234; }, (ref int x) { x++; return x; }); }))); |
| 3649 |
} |
| 3650 |
|
| 3651 |
@safe unittest |
| 3652 |
{ |
| 3653 |
struct S0 |
| 3654 |
{ |
| 3655 |
int opCall(ref int v) |
| 3656 |
{ |
| 3657 |
return v + 1; |
| 3658 |
} |
| 3659 |
} |
| 3660 |
|
| 3661 |
struct S1 |
| 3662 |
{ |
| 3663 |
int opCall()() |
| 3664 |
{ |
| 3665 |
return -2; |
| 3666 |
} |
| 3667 |
|
| 3668 |
T opCall(T)(ref T v) |
| 3669 |
{ |
| 3670 |
return v + 1; |
| 3671 |
} |
| 3672 |
} |
| 3673 |
|
| 3674 |
int[string] a = ["2" : 1]; |
| 3675 |
a.update("2", () => -1, S0.init); |
| 3676 |
assert(a["2"] == 2); |
| 3677 |
a.update("0", () => -1, S0.init); |
| 3678 |
assert(a["0"] == -1); |
| 3679 |
a.update("2", S1.init, S1.init); |
| 3680 |
assert(a["2"] == 3); |
| 3681 |
a.update("1", S1.init, S1.init); |
| 3682 |
assert(a["1"] == -2); |
| 3683 |
} |
| 3684 |
|
| 3685 |
@system unittest |
| 3686 |
{ |
| 3687 |
int[string] aa; |
| 3688 |
|
| 3689 |
foreach (n; 0 .. 2) |
| 3690 |
aa.update("k1", { |
| 3691 |
return 7; |
| 3692 |
}, (ref int v) { |
| 3693 |
return v + 3; |
| 3694 |
}); |
| 3695 |
assert(aa["k1"] == 10); |
| 3696 |
} |
| 3697 |
|
| 3698 |
version (CoreDdoc) |
| 3699 |
{ |
| 3700 |
// This lets DDoc produce better documentation. |
| 3701 |
|
| 3702 |
/** |
| 3703 |
Calculates the hash value of `arg` with an optional `seed` initial value. |
| 3704 |
The result might not be equal to `typeid(T).getHash(&arg)`. |
| 3705 |
|
| 3706 |
Params: |
| 3707 |
arg = argument to calculate the hash value of |
| 3708 |
seed = optional `seed` value (may be used for hash chaining) |
| 3709 |
|
| 3710 |
Return: calculated hash value of `arg` |
| 3711 |
*/ |
| 3712 |
size_t hashOf(T)(auto ref T arg, size_t seed) |
| 3713 |
{ |
| 3714 |
static import core.internal.hash; |
| 3715 |
return core.internal.hash.hashOf(arg, seed); |
| 3716 |
} |
| 3717 |
/// ditto |
| 3718 |
size_t hashOf(T)(auto ref T arg) |
| 3719 |
{ |
| 3720 |
static import core.internal.hash; |
| 3721 |
return core.internal.hash.hashOf(arg); |
| 3722 |
} |
| 3723 |
|
| 3724 |
@safe unittest |
| 3725 |
{ |
| 3726 |
auto h1 = "my.string".hashOf; |
| 3727 |
assert(h1 == "my.string".hashOf); |
| 3728 |
} |
| 3729 |
} |
| 3730 |
else |
| 3731 |
{ |
| 3732 |
public import core.internal.hash : hashOf; |
| 3733 |
} |
| 3734 |
|
| 3735 |
/// |
| 3736 |
@system unittest |
| 3737 |
{ |
| 3738 |
class MyObject |
| 3739 |
{ |
| 3740 |
size_t myMegaHash() const @safe pure nothrow |
| 3741 |
{ |
| 3742 |
return 42; |
| 3743 |
} |
| 3744 |
} |
| 3745 |
struct Test |
| 3746 |
{ |
| 3747 |
int a; |
| 3748 |
string b; |
| 3749 |
MyObject c; |
| 3750 |
size_t toHash() const pure nothrow |
| 3751 |
{ |
| 3752 |
size_t hash = a.hashOf(); |
| 3753 |
hash = b.hashOf(hash); |
| 3754 |
size_t h1 = c.myMegaHash(); |
| 3755 |
hash = h1.hashOf(hash); //Mix two hash values |
| 3756 |
return hash; |
| 3757 |
} |
| 3758 |
} |
| 3759 |
} |
| 3760 |
|
| 3761 |
bool _xopEquals(in void*, in void*) |
| 3762 |
{ |
| 3763 |
throw new Error("TypeInfo.equals is not implemented"); |
| 3764 |
} |
| 3765 |
|
| 3766 |
bool _xopCmp(in void*, in void*) |
| 3767 |
{ |
| 3768 |
throw new Error("TypeInfo.compare is not implemented"); |
| 3769 |
} |
| 3770 |
|
| 3771 |
/****************************************** |
| 3772 |
* Create RTInfo for type T |
| 3773 |
*/ |
| 3774 |
|
| 3775 |
template RTInfoImpl(size_t[] pointerBitmap) |
| 3776 |
{ |
| 3777 |
immutable size_t[pointerBitmap.length] RTInfoImpl = pointerBitmap[]; |
| 3778 |
} |
| 3779 |
|
| 3780 |
template RTInfo(T) |
| 3781 |
{ |
| 3782 |
enum pointerBitmap = __traits(getPointerBitmap, T); |
| 3783 |
static if (pointerBitmap[1 .. $] == size_t[pointerBitmap.length - 1].init) |
| 3784 |
enum RTInfo = rtinfoNoPointers; |
| 3785 |
else |
| 3786 |
enum RTInfo = RTInfoImpl!(pointerBitmap).ptr; |
| 3787 |
} |
| 3788 |
|
| 3789 |
/** |
| 3790 |
* shortcuts for the precise GC, also generated by the compiler |
| 3791 |
* used instead of the actual pointer bitmap |
| 3792 |
*/ |
| 3793 |
enum immutable(void)* rtinfoNoPointers = null; |
| 3794 |
enum immutable(void)* rtinfoHasPointers = cast(void*)1; |
| 3795 |
|
| 3796 |
// Helper functions |
| 3797 |
|
| 3798 |
private inout(TypeInfo) getElement(return scope inout TypeInfo value) @trusted pure nothrow |
| 3799 |
{ |
| 3800 |
TypeInfo element = cast() value; |
| 3801 |
for (;;) |
| 3802 |
{ |
| 3803 |
if (auto qualified = cast(TypeInfo_Const) element) |
| 3804 |
element = qualified.base; |
| 3805 |
else if (auto redefined = cast(TypeInfo_Enum) element) |
| 3806 |
element = redefined.base; |
| 3807 |
else if (auto staticArray = cast(TypeInfo_StaticArray) element) |
| 3808 |
element = staticArray.value; |
| 3809 |
else if (auto vector = cast(TypeInfo_Vector) element) |
| 3810 |
element = vector.base; |
| 3811 |
else |
| 3812 |
break; |
| 3813 |
} |
| 3814 |
return cast(inout) element; |
| 3815 |
} |
| 3816 |
|
| 3817 |
private size_t getArrayHash(const scope TypeInfo element, const scope void* ptr, const size_t count) @trusted nothrow |
| 3818 |
{ |
| 3819 |
if (!count) |
| 3820 |
return 0; |
| 3821 |
|
| 3822 |
const size_t elementSize = element.tsize; |
| 3823 |
if (!elementSize) |
| 3824 |
return 0; |
| 3825 |
|
| 3826 |
static bool hasCustomToHash(const scope TypeInfo value) @trusted pure nothrow |
| 3827 |
{ |
| 3828 |
const element = getElement(value); |
| 3829 |
|
| 3830 |
if (const struct_ = cast(const TypeInfo_Struct) element) |
| 3831 |
return !!struct_.xtoHash; |
| 3832 |
|
| 3833 |
return cast(const TypeInfo_Array) element |
| 3834 |
|| cast(const TypeInfo_AssociativeArray) element |
| 3835 |
|| cast(const ClassInfo) element |
| 3836 |
|| cast(const TypeInfo_Interface) element; |
| 3837 |
} |
| 3838 |
|
| 3839 |
if (!hasCustomToHash(element)) |
| 3840 |
return hashOf(ptr[0 .. elementSize * count]); |
| 3841 |
|
| 3842 |
size_t hash = 0; |
| 3843 |
foreach (size_t i; 0 .. count) |
| 3844 |
hash = hashOf(element.getHash(ptr + i * elementSize), hash); |
| 3845 |
return hash; |
| 3846 |
} |
| 3847 |
|
| 3848 |
/// Provide the .dup array property. |
| 3849 |
@property auto dup(T)(T[] a) |
| 3850 |
if (!is(const(T) : T)) |
| 3851 |
{ |
| 3852 |
import core.internal.traits : Unconst; |
| 3853 |
import core.internal.array.duplication : _dup; |
| 3854 |
static assert(is(T : Unconst!T), "Cannot implicitly convert type "~T.stringof~ |
| 3855 |
" to "~Unconst!T.stringof~" in dup."); |
| 3856 |
|
| 3857 |
return _dup!(T, Unconst!T)(a); |
| 3858 |
} |
| 3859 |
|
| 3860 |
/// |
| 3861 |
@safe unittest |
| 3862 |
{ |
| 3863 |
auto arr = [1, 2]; |
| 3864 |
auto arr2 = arr.dup; |
| 3865 |
arr[0] = 0; |
| 3866 |
assert(arr == [0, 2]); |
| 3867 |
assert(arr2 == [1, 2]); |
| 3868 |
} |
| 3869 |
|
| 3870 |
/// ditto |
| 3871 |
// const overload to support implicit conversion to immutable (unique result, see DIP29) |
| 3872 |
@property T[] dup(T)(const(T)[] a) |
| 3873 |
if (is(const(T) : T)) |
| 3874 |
{ |
| 3875 |
import core.internal.array.duplication : _dup; |
| 3876 |
return _dup!(const(T), T)(a); |
| 3877 |
} |
| 3878 |
|
| 3879 |
|
| 3880 |
/// Provide the .idup array property. |
| 3881 |
@property immutable(T)[] idup(T)(T[] a) |
| 3882 |
{ |
| 3883 |
import core.internal.array.duplication : _dup; |
| 3884 |
static assert(is(T : immutable(T)), "Cannot implicitly convert type "~T.stringof~ |
| 3885 |
" to immutable in idup."); |
| 3886 |
return _dup!(T, immutable(T))(a); |
| 3887 |
} |
| 3888 |
|
| 3889 |
/// ditto |
| 3890 |
@property immutable(T)[] idup(T:void)(const(T)[] a) |
| 3891 |
{ |
| 3892 |
return a.dup; |
| 3893 |
} |
| 3894 |
|
| 3895 |
/// |
| 3896 |
@safe unittest |
| 3897 |
{ |
| 3898 |
char[] arr = ['a', 'b', 'c']; |
| 3899 |
string s = arr.idup; |
| 3900 |
arr[0] = '.'; |
| 3901 |
assert(s == "abc"); |
| 3902 |
} |
| 3903 |
|
| 3904 |
// HACK: This is a lie. `_d_arraysetcapacity` is neither `nothrow` nor `pure`, but this lie is |
| 3905 |
// necessary for now to prevent breaking code. |
| 3906 |
private extern (C) size_t _d_arraysetcapacity(const TypeInfo ti, size_t newcapacity, void[]* arrptr) pure nothrow; |
| 3907 |
|
| 3908 |
/** |
| 3909 |
(Property) Gets the current _capacity of a slice. The _capacity is the size |
| 3910 |
that the slice can grow to before the underlying array must be |
| 3911 |
reallocated or extended. |
| 3912 |
|
| 3913 |
If an append must reallocate a slice with no possibility of extension, then |
| 3914 |
`0` is returned. This happens when the slice references a static array, or |
| 3915 |
if another slice references elements past the end of the current slice. |
| 3916 |
|
| 3917 |
Note: The _capacity of a slice may be impacted by operations on other slices. |
| 3918 |
*/ |
| 3919 |
@property size_t capacity(T)(T[] arr) pure nothrow @trusted |
| 3920 |
{ |
| 3921 |
return _d_arraysetcapacity(typeid(T[]), 0, cast(void[]*)&arr); |
| 3922 |
} |
| 3923 |
|
| 3924 |
/// |
| 3925 |
@safe unittest |
| 3926 |
{ |
| 3927 |
//Static array slice: no capacity |
| 3928 |
int[4] sarray = [1, 2, 3, 4]; |
| 3929 |
int[] slice = sarray[]; |
| 3930 |
assert(sarray.capacity == 0); |
| 3931 |
//Appending to slice will reallocate to a new array |
| 3932 |
slice ~= 5; |
| 3933 |
assert(slice.capacity >= 5); |
| 3934 |
|
| 3935 |
//Dynamic array slices |
| 3936 |
int[] a = [1, 2, 3, 4]; |
| 3937 |
int[] b = a[1 .. $]; |
| 3938 |
int[] c = a[1 .. $ - 1]; |
| 3939 |
debug(SENTINEL) {} else // non-zero capacity very much depends on the array and GC implementation |
| 3940 |
{ |
| 3941 |
assert(a.capacity != 0); |
| 3942 |
assert(a.capacity == b.capacity + 1); //both a and b share the same tail |
| 3943 |
} |
| 3944 |
assert(c.capacity == 0); //an append to c must relocate c. |
| 3945 |
} |
| 3946 |
|
| 3947 |
/** |
| 3948 |
Reserves capacity for a slice. The capacity is the size |
| 3949 |
that the slice can grow to before the underlying array must be |
| 3950 |
reallocated or extended. |
| 3951 |
|
| 3952 |
Returns: The new capacity of the array (which may be larger than |
| 3953 |
the requested capacity). |
| 3954 |
*/ |
| 3955 |
size_t reserve(T)(ref T[] arr, size_t newcapacity) pure nothrow @trusted |
| 3956 |
{ |
| 3957 |
if (__ctfe) |
| 3958 |
return newcapacity; |
| 3959 |
else |
| 3960 |
return _d_arraysetcapacity(typeid(T[]), newcapacity, cast(void[]*)&arr); |
| 3961 |
} |
| 3962 |
|
| 3963 |
/// |
| 3964 |
@safe unittest |
| 3965 |
{ |
| 3966 |
//Static array slice: no capacity. Reserve relocates. |
| 3967 |
int[4] sarray = [1, 2, 3, 4]; |
| 3968 |
int[] slice = sarray[]; |
| 3969 |
auto u = slice.reserve(8); |
| 3970 |
assert(u >= 8); |
| 3971 |
assert(&sarray[0] !is &slice[0]); |
| 3972 |
assert(slice.capacity == u); |
| 3973 |
|
| 3974 |
//Dynamic array slices |
| 3975 |
int[] a = [1, 2, 3, 4]; |
| 3976 |
a.reserve(8); //prepare a for appending 4 more items |
| 3977 |
auto p = &a[0]; |
| 3978 |
u = a.capacity; |
| 3979 |
a ~= [5, 6, 7, 8]; |
| 3980 |
assert(p == &a[0]); //a should not have been reallocated |
| 3981 |
assert(u == a.capacity); //a should not have been extended |
| 3982 |
} |
| 3983 |
|
| 3984 |
// https://issues.dlang.org/show_bug.cgi?id=12330, reserve() at CTFE time |
| 3985 |
@safe unittest |
| 3986 |
{ |
| 3987 |
int[] foo() { |
| 3988 |
int[] result; |
| 3989 |
auto a = result.reserve = 5; |
| 3990 |
assert(a == 5); |
| 3991 |
return result; |
| 3992 |
} |
| 3993 |
enum r = foo(); |
| 3994 |
} |
| 3995 |
|
| 3996 |
// Issue 6646: should be possible to use array.reserve from SafeD. |
| 3997 |
@safe unittest |
| 3998 |
{ |
| 3999 |
int[] a; |
| 4000 |
a.reserve(10); |
| 4001 |
} |
| 4002 |
|
| 4003 |
// HACK: This is a lie. `_d_arrayshrinkfit` is not `nothrow`, but this lie is necessary |
| 4004 |
// for now to prevent breaking code. |
| 4005 |
private extern (C) void _d_arrayshrinkfit(const TypeInfo ti, void[] arr) nothrow; |
| 4006 |
|
| 4007 |
/** |
| 4008 |
Assume that it is safe to append to this array. Appends made to this array |
| 4009 |
after calling this function may append in place, even if the array was a |
| 4010 |
slice of a larger array to begin with. |
| 4011 |
|
| 4012 |
Use this only when it is certain there are no elements in use beyond the |
| 4013 |
array in the memory block. If there are, those elements will be |
| 4014 |
overwritten by appending to this array. |
| 4015 |
|
| 4016 |
Warning: Calling this function, and then using references to data located after the |
| 4017 |
given array results in undefined behavior. |
| 4018 |
|
| 4019 |
Returns: |
| 4020 |
The input is returned. |
| 4021 |
*/ |
| 4022 |
auto ref inout(T[]) assumeSafeAppend(T)(auto ref inout(T[]) arr) nothrow @system |
| 4023 |
{ |
| 4024 |
_d_arrayshrinkfit(typeid(T[]), *(cast(void[]*)&arr)); |
| 4025 |
return arr; |
| 4026 |
} |
| 4027 |
|
| 4028 |
/// |
| 4029 |
@system unittest |
| 4030 |
{ |
| 4031 |
int[] a = [1, 2, 3, 4]; |
| 4032 |
|
| 4033 |
// Without assumeSafeAppend. Appending relocates. |
| 4034 |
int[] b = a [0 .. 3]; |
| 4035 |
b ~= 5; |
| 4036 |
assert(a.ptr != b.ptr); |
| 4037 |
|
| 4038 |
debug(SENTINEL) {} else |
| 4039 |
{ |
| 4040 |
// With assumeSafeAppend. Appending overwrites. |
| 4041 |
int[] c = a [0 .. 3]; |
| 4042 |
c.assumeSafeAppend() ~= 5; |
| 4043 |
assert(a.ptr == c.ptr); |
| 4044 |
} |
| 4045 |
} |
| 4046 |
|
| 4047 |
@system unittest |
| 4048 |
{ |
| 4049 |
int[] arr; |
| 4050 |
auto newcap = arr.reserve(2000); |
| 4051 |
assert(newcap >= 2000); |
| 4052 |
assert(newcap == arr.capacity); |
| 4053 |
auto ptr = arr.ptr; |
| 4054 |
foreach (i; 0..2000) |
| 4055 |
arr ~= i; |
| 4056 |
assert(ptr == arr.ptr); |
| 4057 |
arr = arr[0..1]; |
| 4058 |
arr.assumeSafeAppend(); |
| 4059 |
arr ~= 5; |
| 4060 |
assert(ptr == arr.ptr); |
| 4061 |
} |
| 4062 |
|
| 4063 |
@system unittest |
| 4064 |
{ |
| 4065 |
int[] arr = [1, 2, 3]; |
| 4066 |
void foo(ref int[] i) |
| 4067 |
{ |
| 4068 |
i ~= 5; |
| 4069 |
} |
| 4070 |
arr = arr[0 .. 2]; |
| 4071 |
foo(assumeSafeAppend(arr)); //pass by ref |
| 4072 |
assert(arr[]==[1, 2, 5]); |
| 4073 |
arr = arr[0 .. 1].assumeSafeAppend(); //pass by value |
| 4074 |
} |
| 4075 |
|
| 4076 |
// https://issues.dlang.org/show_bug.cgi?id=10574 |
| 4077 |
@system unittest |
| 4078 |
{ |
| 4079 |
int[] a; |
| 4080 |
immutable(int[]) b; |
| 4081 |
auto a2 = &assumeSafeAppend(a); |
| 4082 |
auto b2 = &assumeSafeAppend(b); |
| 4083 |
auto a3 = assumeSafeAppend(a[]); |
| 4084 |
auto b3 = assumeSafeAppend(b[]); |
| 4085 |
assert(is(typeof(*a2) == int[])); |
| 4086 |
assert(is(typeof(*b2) == immutable(int[]))); |
| 4087 |
assert(is(typeof(a3) == int[])); |
| 4088 |
assert(is(typeof(b3) == immutable(int[]))); |
| 4089 |
} |
| 4090 |
|
| 4091 |
private void _doPostblit(T)(T[] arr) |
| 4092 |
{ |
| 4093 |
// infer static postblit type, run postblit if any |
| 4094 |
static if (__traits(hasPostblit, T)) |
| 4095 |
{ |
| 4096 |
static if (__traits(isStaticArray, T) && is(T : E[], E)) |
| 4097 |
_doPostblit(cast(E[]) arr); |
| 4098 |
else static if (!is(typeof(arr[0].__xpostblit())) && is(immutable T == immutable U, U)) |
| 4099 |
foreach (ref elem; (() @trusted => cast(U[]) arr)()) |
| 4100 |
elem.__xpostblit(); |
| 4101 |
else |
| 4102 |
foreach (ref elem; arr) |
| 4103 |
elem.__xpostblit(); |
| 4104 |
} |
| 4105 |
} |
| 4106 |
|
| 4107 |
/** |
| 4108 |
Destroys the given object and optionally resets to initial state. It's used to |
| 4109 |
_destroy an object, calling its destructor or finalizer so it no longer |
| 4110 |
references any other objects. It does $(I not) initiate a GC cycle or free |
| 4111 |
any GC memory. |
| 4112 |
If `initialize` is supplied `false`, the object is considered invalid after |
| 4113 |
destruction, and should not be referenced. |
| 4114 |
*/ |
| 4115 |
void destroy(bool initialize = true, T)(ref T obj) if (is(T == struct)) |
| 4116 |
{ |
| 4117 |
import core.internal.destruction : destructRecurse; |
| 4118 |
|
| 4119 |
destructRecurse(obj); |
| 4120 |
|
| 4121 |
static if (initialize) |
| 4122 |
{ |
| 4123 |
import core.internal.lifetime : emplaceInitializer; |
| 4124 |
emplaceInitializer(obj); // emplace T.init |
| 4125 |
} |
| 4126 |
} |
| 4127 |
|
| 4128 |
@safe unittest |
| 4129 |
{ |
| 4130 |
struct A { string s = "A"; } |
| 4131 |
A a = {s: "B"}; |
| 4132 |
assert(a.s == "B"); |
| 4133 |
a.destroy; |
| 4134 |
assert(a.s == "A"); |
| 4135 |
} |
| 4136 |
|
| 4137 |
nothrow @safe @nogc unittest |
| 4138 |
{ |
| 4139 |
{ |
| 4140 |
struct A { string s = "A"; } |
| 4141 |
A a; |
| 4142 |
a.s = "asd"; |
| 4143 |
destroy!false(a); |
| 4144 |
assert(a.s == "asd"); |
| 4145 |
destroy(a); |
| 4146 |
assert(a.s == "A"); |
| 4147 |
} |
| 4148 |
{ |
| 4149 |
static int destroyed = 0; |
| 4150 |
struct C |
| 4151 |
{ |
| 4152 |
string s = "C"; |
| 4153 |
~this() nothrow @safe @nogc |
| 4154 |
{ |
| 4155 |
destroyed ++; |
| 4156 |
} |
| 4157 |
} |
| 4158 |
|
| 4159 |
struct B |
| 4160 |
{ |
| 4161 |
C c; |
| 4162 |
string s = "B"; |
| 4163 |
~this() nothrow @safe @nogc |
| 4164 |
{ |
| 4165 |
destroyed ++; |
| 4166 |
} |
| 4167 |
} |
| 4168 |
B a; |
| 4169 |
a.s = "asd"; |
| 4170 |
a.c.s = "jkl"; |
| 4171 |
destroy!false(a); |
| 4172 |
assert(destroyed == 2); |
| 4173 |
assert(a.s == "asd"); |
| 4174 |
assert(a.c.s == "jkl" ); |
| 4175 |
destroy(a); |
| 4176 |
assert(destroyed == 4); |
| 4177 |
assert(a.s == "B"); |
| 4178 |
assert(a.c.s == "C" ); |
| 4179 |
} |
| 4180 |
} |
| 4181 |
|
| 4182 |
private extern (C) void rt_finalize2(void* p, bool det = true, bool resetMemory = true) nothrow; |
| 4183 |
|
| 4184 |
/// ditto |
| 4185 |
void destroy(bool initialize = true, T)(T obj) if (is(T == class)) |
| 4186 |
{ |
| 4187 |
static if (__traits(getLinkage, T) == "C++") |
| 4188 |
{ |
| 4189 |
static if (__traits(hasMember, T, "__xdtor")) |
| 4190 |
obj.__xdtor(); |
| 4191 |
|
| 4192 |
static if (initialize) |
| 4193 |
{ |
| 4194 |
const initializer = __traits(initSymbol, T); |
| 4195 |
(cast(void*)obj)[0 .. initializer.length] = initializer[]; |
| 4196 |
} |
| 4197 |
} |
| 4198 |
else |
| 4199 |
{ |
| 4200 |
// Bypass overloaded opCast |
| 4201 |
auto ptr = (() @trusted => *cast(void**) &obj)(); |
| 4202 |
rt_finalize2(ptr, true, initialize); |
| 4203 |
} |
| 4204 |
} |
| 4205 |
|
| 4206 |
/// ditto |
| 4207 |
void destroy(bool initialize = true, T)(T obj) if (is(T == interface)) |
| 4208 |
{ |
| 4209 |
static assert(__traits(getLinkage, T) == "D", "Invalid call to destroy() on extern(" ~ __traits(getLinkage, T) ~ ") interface"); |
| 4210 |
|
| 4211 |
destroy!initialize(cast(Object)obj); |
| 4212 |
} |
| 4213 |
|
| 4214 |
/// Reference type demonstration |
| 4215 |
@system unittest |
| 4216 |
{ |
| 4217 |
class C |
| 4218 |
{ |
| 4219 |
struct Agg |
| 4220 |
{ |
| 4221 |
static int dtorCount; |
| 4222 |
|
| 4223 |
int x = 10; |
| 4224 |
~this() { dtorCount++; } |
| 4225 |
} |
| 4226 |
|
| 4227 |
static int dtorCount; |
| 4228 |
|
| 4229 |
string s = "S"; |
| 4230 |
Agg a; |
| 4231 |
~this() { dtorCount++; } |
| 4232 |
} |
| 4233 |
|
| 4234 |
C c = new C(); |
| 4235 |
assert(c.dtorCount == 0); // destructor not yet called |
| 4236 |
assert(c.s == "S"); // initial state `c.s` is `"S"` |
| 4237 |
assert(c.a.dtorCount == 0); // destructor not yet called |
| 4238 |
assert(c.a.x == 10); // initial state `c.a.x` is `10` |
| 4239 |
c.s = "T"; |
| 4240 |
c.a.x = 30; |
| 4241 |
assert(c.s == "T"); // `c.s` is `"T"` |
| 4242 |
destroy(c); |
| 4243 |
assert(c.dtorCount == 1); // `c`'s destructor was called |
| 4244 |
assert(c.s == "S"); // `c.s` is back to its inital state, `"S"` |
| 4245 |
assert(c.a.dtorCount == 1); // `c.a`'s destructor was called |
| 4246 |
assert(c.a.x == 10); // `c.a.x` is back to its inital state, `10` |
| 4247 |
} |
| 4248 |
|
| 4249 |
/// C++ classes work too |
| 4250 |
@system unittest |
| 4251 |
{ |
| 4252 |
extern (C++) class CPP |
| 4253 |
{ |
| 4254 |
struct Agg |
| 4255 |
{ |
| 4256 |
__gshared int dtorCount; |
| 4257 |
|
| 4258 |
int x = 10; |
| 4259 |
~this() { dtorCount++; } |
| 4260 |
} |
| 4261 |
|
| 4262 |
__gshared int dtorCount; |
| 4263 |
|
| 4264 |
string s = "S"; |
| 4265 |
Agg a; |
| 4266 |
~this() { dtorCount++; } |
| 4267 |
} |
| 4268 |
|
| 4269 |
CPP cpp = new CPP(); |
| 4270 |
assert(cpp.dtorCount == 0); // destructor not yet called |
| 4271 |
assert(cpp.s == "S"); // initial state `cpp.s` is `"S"` |
| 4272 |
assert(cpp.a.dtorCount == 0); // destructor not yet called |
| 4273 |
assert(cpp.a.x == 10); // initial state `cpp.a.x` is `10` |
| 4274 |
cpp.s = "T"; |
| 4275 |
cpp.a.x = 30; |
| 4276 |
assert(cpp.s == "T"); // `cpp.s` is `"T"` |
| 4277 |
destroy!false(cpp); // destroy without initialization |
| 4278 |
assert(cpp.dtorCount == 1); // `cpp`'s destructor was called |
| 4279 |
assert(cpp.s == "T"); // `cpp.s` is not initialized |
| 4280 |
assert(cpp.a.dtorCount == 1); // `cpp.a`'s destructor was called |
| 4281 |
assert(cpp.a.x == 30); // `cpp.a.x` is not initialized |
| 4282 |
destroy(cpp); |
| 4283 |
assert(cpp.dtorCount == 2); // `cpp`'s destructor was called again |
| 4284 |
assert(cpp.s == "S"); // `cpp.s` is back to its inital state, `"S"` |
| 4285 |
assert(cpp.a.dtorCount == 2); // `cpp.a`'s destructor was called again |
| 4286 |
assert(cpp.a.x == 10); // `cpp.a.x` is back to its inital state, `10` |
| 4287 |
} |
| 4288 |
|
| 4289 |
/// Value type demonstration |
| 4290 |
@safe unittest |
| 4291 |
{ |
| 4292 |
int i; |
| 4293 |
assert(i == 0); // `i`'s initial state is `0` |
| 4294 |
i = 1; |
| 4295 |
assert(i == 1); // `i` changed to `1` |
| 4296 |
destroy!false(i); |
| 4297 |
assert(i == 1); // `i` was not initialized |
| 4298 |
destroy(i); |
| 4299 |
assert(i == 0); // `i` is back to its initial state `0` |
| 4300 |
} |
| 4301 |
|
| 4302 |
/// Nested struct type |
| 4303 |
@system unittest |
| 4304 |
{ |
| 4305 |
int dtorCount; |
| 4306 |
struct A |
| 4307 |
{ |
| 4308 |
int i; |
| 4309 |
~this() |
| 4310 |
{ |
| 4311 |
dtorCount++; // capture local variable |
| 4312 |
} |
| 4313 |
} |
| 4314 |
A a = A(5); |
| 4315 |
destroy!false(a); |
| 4316 |
assert(dtorCount == 1); |
| 4317 |
assert(a.i == 5); |
| 4318 |
|
| 4319 |
destroy(a); |
| 4320 |
assert(dtorCount == 2); |
| 4321 |
assert(a.i == 0); |
| 4322 |
|
| 4323 |
// the context pointer is now null |
| 4324 |
// restore it so the dtor can run |
| 4325 |
import core.lifetime : emplace; |
| 4326 |
emplace(&a, A(0)); |
| 4327 |
// dtor also called here |
| 4328 |
} |
| 4329 |
|
| 4330 |
@system unittest |
| 4331 |
{ |
| 4332 |
extern(C++) |
| 4333 |
static class C |
| 4334 |
{ |
| 4335 |
void* ptr; |
| 4336 |
this() {} |
| 4337 |
} |
| 4338 |
|
| 4339 |
destroy!false(new C()); |
| 4340 |
destroy!true(new C()); |
| 4341 |
} |
| 4342 |
|
| 4343 |
@system unittest |
| 4344 |
{ |
| 4345 |
// class with an `alias this` |
| 4346 |
class A |
| 4347 |
{ |
| 4348 |
static int dtorCount; |
| 4349 |
~this() |
| 4350 |
{ |
| 4351 |
dtorCount++; |
| 4352 |
} |
| 4353 |
} |
| 4354 |
|
| 4355 |
class B |
| 4356 |
{ |
| 4357 |
A a; |
| 4358 |
alias a this; |
| 4359 |
this() |
| 4360 |
{ |
| 4361 |
a = new A; |
| 4362 |
} |
| 4363 |
static int dtorCount; |
| 4364 |
~this() |
| 4365 |
{ |
| 4366 |
dtorCount++; |
| 4367 |
} |
| 4368 |
} |
| 4369 |
auto b = new B; |
| 4370 |
assert(A.dtorCount == 0); |
| 4371 |
assert(B.dtorCount == 0); |
| 4372 |
destroy(b); |
| 4373 |
assert(A.dtorCount == 0); |
| 4374 |
assert(B.dtorCount == 1); |
| 4375 |
|
| 4376 |
auto a = new A; |
| 4377 |
destroy(a); |
| 4378 |
assert(A.dtorCount == 1); |
| 4379 |
} |
| 4380 |
|
| 4381 |
@system unittest |
| 4382 |
{ |
| 4383 |
interface I { } |
| 4384 |
{ |
| 4385 |
class A: I { string s = "A"; this() {} } |
| 4386 |
auto a = new A, b = new A; |
| 4387 |
a.s = b.s = "asd"; |
| 4388 |
destroy(a); |
| 4389 |
assert(a.s == "A"); |
| 4390 |
|
| 4391 |
I i = b; |
| 4392 |
destroy(i); |
| 4393 |
assert(b.s == "A"); |
| 4394 |
} |
| 4395 |
{ |
| 4396 |
static bool destroyed = false; |
| 4397 |
class B: I |
| 4398 |
{ |
| 4399 |
string s = "B"; |
| 4400 |
this() {} |
| 4401 |
~this() |
| 4402 |
{ |
| 4403 |
destroyed = true; |
| 4404 |
} |
| 4405 |
} |
| 4406 |
auto a = new B, b = new B; |
| 4407 |
a.s = b.s = "asd"; |
| 4408 |
destroy(a); |
| 4409 |
assert(destroyed); |
| 4410 |
assert(a.s == "B"); |
| 4411 |
|
| 4412 |
destroyed = false; |
| 4413 |
I i = b; |
| 4414 |
destroy(i); |
| 4415 |
assert(destroyed); |
| 4416 |
assert(b.s == "B"); |
| 4417 |
} |
| 4418 |
// this test is invalid now that the default ctor is not run after clearing |
| 4419 |
version (none) |
| 4420 |
{ |
| 4421 |
class C |
| 4422 |
{ |
| 4423 |
string s; |
| 4424 |
this() |
| 4425 |
{ |
| 4426 |
s = "C"; |
| 4427 |
} |
| 4428 |
} |
| 4429 |
auto a = new C; |
| 4430 |
a.s = "asd"; |
| 4431 |
destroy(a); |
| 4432 |
assert(a.s == "C"); |
| 4433 |
} |
| 4434 |
} |
| 4435 |
|
| 4436 |
nothrow @safe @nogc unittest |
| 4437 |
{ |
| 4438 |
{ |
| 4439 |
struct A { string s = "A"; } |
| 4440 |
A a; |
| 4441 |
a.s = "asd"; |
| 4442 |
destroy!false(a); |
| 4443 |
assert(a.s == "asd"); |
| 4444 |
destroy(a); |
| 4445 |
assert(a.s == "A"); |
| 4446 |
} |
| 4447 |
{ |
| 4448 |
static int destroyed = 0; |
| 4449 |
struct C |
| 4450 |
{ |
| 4451 |
string s = "C"; |
| 4452 |
~this() nothrow @safe @nogc |
| 4453 |
{ |
| 4454 |
destroyed ++; |
| 4455 |
} |
| 4456 |
} |
| 4457 |
|
| 4458 |
struct B |
| 4459 |
{ |
| 4460 |
C c; |
| 4461 |
string s = "B"; |
| 4462 |
~this() nothrow @safe @nogc |
| 4463 |
{ |
| 4464 |
destroyed ++; |
| 4465 |
} |
| 4466 |
} |
| 4467 |
B a; |
| 4468 |
a.s = "asd"; |
| 4469 |
a.c.s = "jkl"; |
| 4470 |
destroy!false(a); |
| 4471 |
assert(destroyed == 2); |
| 4472 |
assert(a.s == "asd"); |
| 4473 |
assert(a.c.s == "jkl" ); |
| 4474 |
destroy(a); |
| 4475 |
assert(destroyed == 4); |
| 4476 |
assert(a.s == "B"); |
| 4477 |
assert(a.c.s == "C" ); |
| 4478 |
} |
| 4479 |
} |
| 4480 |
|
| 4481 |
nothrow unittest |
| 4482 |
{ |
| 4483 |
// Bugzilla 20049: Test to ensure proper behavior of `nothrow` destructors |
| 4484 |
class C |
| 4485 |
{ |
| 4486 |
static int dtorCount = 0; |
| 4487 |
this() nothrow {} |
| 4488 |
~this() nothrow { dtorCount++; } |
| 4489 |
} |
| 4490 |
|
| 4491 |
auto c = new C; |
| 4492 |
destroy(c); |
| 4493 |
assert(C.dtorCount == 1); |
| 4494 |
} |
| 4495 |
|
| 4496 |
// https://issues.dlang.org/show_bug.cgi?id=22832 |
| 4497 |
nothrow unittest |
| 4498 |
{ |
| 4499 |
static struct A {} |
| 4500 |
static class B |
| 4501 |
{ |
| 4502 |
A opCast(T : A)() { return A(); } |
| 4503 |
} |
| 4504 |
|
| 4505 |
destroy(B.init); |
| 4506 |
} |
| 4507 |
|
| 4508 |
// make sure destroy!false skips re-initialization |
| 4509 |
unittest |
| 4510 |
{ |
| 4511 |
static struct S { int x; } |
| 4512 |
static class C { int x; } |
| 4513 |
static extern(C++) class Cpp { int x; } |
| 4514 |
|
| 4515 |
static void test(T)(T inst) |
| 4516 |
{ |
| 4517 |
inst.x = 123; |
| 4518 |
destroy!false(inst); |
| 4519 |
assert(inst.x == 123, T.stringof); |
| 4520 |
} |
| 4521 |
|
| 4522 |
test(S()); |
| 4523 |
test(new C()); |
| 4524 |
test(new Cpp()); |
| 4525 |
} |
| 4526 |
|
| 4527 |
/// ditto |
| 4528 |
void destroy(bool initialize = true, T)(ref T obj) |
| 4529 |
if (__traits(isStaticArray, T)) |
| 4530 |
{ |
| 4531 |
foreach_reverse (ref e; obj[]) |
| 4532 |
destroy!initialize(e); |
| 4533 |
} |
| 4534 |
|
| 4535 |
@safe unittest |
| 4536 |
{ |
| 4537 |
int[2] a; |
| 4538 |
a[0] = 1; |
| 4539 |
a[1] = 2; |
| 4540 |
destroy!false(a); |
| 4541 |
assert(a == [ 1, 2 ]); |
| 4542 |
destroy(a); |
| 4543 |
assert(a == [ 0, 0 ]); |
| 4544 |
} |
| 4545 |
|
| 4546 |
@safe unittest |
| 4547 |
{ |
| 4548 |
static struct vec2f { |
| 4549 |
float[2] values; |
| 4550 |
alias values this; |
| 4551 |
} |
| 4552 |
|
| 4553 |
vec2f v; |
| 4554 |
destroy!(true, vec2f)(v); |
| 4555 |
} |
| 4556 |
|
| 4557 |
@system unittest |
| 4558 |
{ |
| 4559 |
// Bugzilla 15009 |
| 4560 |
static string op; |
| 4561 |
static struct S |
| 4562 |
{ |
| 4563 |
int x; |
| 4564 |
this(int x) { op ~= "C" ~ cast(char)('0'+x); this.x = x; } |
| 4565 |
this(this) { op ~= "P" ~ cast(char)('0'+x); } |
| 4566 |
~this() { op ~= "D" ~ cast(char)('0'+x); } |
| 4567 |
} |
| 4568 |
|
| 4569 |
{ |
| 4570 |
S[2] a1 = [S(1), S(2)]; |
| 4571 |
op = ""; |
| 4572 |
} |
| 4573 |
assert(op == "D2D1"); // built-in scope destruction |
| 4574 |
{ |
| 4575 |
S[2] a1 = [S(1), S(2)]; |
| 4576 |
op = ""; |
| 4577 |
destroy(a1); |
| 4578 |
assert(op == "D2D1"); // consistent with built-in behavior |
| 4579 |
} |
| 4580 |
|
| 4581 |
{ |
| 4582 |
S[2][2] a2 = [[S(1), S(2)], [S(3), S(4)]]; |
| 4583 |
op = ""; |
| 4584 |
} |
| 4585 |
assert(op == "D4D3D2D1"); |
| 4586 |
{ |
| 4587 |
S[2][2] a2 = [[S(1), S(2)], [S(3), S(4)]]; |
| 4588 |
op = ""; |
| 4589 |
destroy(a2); |
| 4590 |
assert(op == "D4D3D2D1", op); |
| 4591 |
} |
| 4592 |
} |
| 4593 |
|
| 4594 |
// https://issues.dlang.org/show_bug.cgi?id=19218 |
| 4595 |
@system unittest |
| 4596 |
{ |
| 4597 |
static struct S |
| 4598 |
{ |
| 4599 |
static dtorCount = 0; |
| 4600 |
~this() { ++dtorCount; } |
| 4601 |
} |
| 4602 |
|
| 4603 |
static interface I |
| 4604 |
{ |
| 4605 |
ref S[3] getArray(); |
| 4606 |
alias getArray this; |
| 4607 |
} |
| 4608 |
|
| 4609 |
static class C : I |
| 4610 |
{ |
| 4611 |
static dtorCount = 0; |
| 4612 |
~this() { ++dtorCount; } |
| 4613 |
|
| 4614 |
S[3] a; |
| 4615 |
alias a this; |
| 4616 |
|
| 4617 |
ref S[3] getArray() { return a; } |
| 4618 |
} |
| 4619 |
|
| 4620 |
C c = new C(); |
| 4621 |
destroy(c); |
| 4622 |
assert(S.dtorCount == 3); |
| 4623 |
assert(C.dtorCount == 1); |
| 4624 |
|
| 4625 |
I i = new C(); |
| 4626 |
destroy(i); |
| 4627 |
assert(S.dtorCount == 6); |
| 4628 |
assert(C.dtorCount == 2); |
| 4629 |
} |
| 4630 |
|
| 4631 |
/// ditto |
| 4632 |
void destroy(bool initialize = true, T)(ref T obj) |
| 4633 |
if (!is(T == struct) && !is(T == interface) && !is(T == class) && !__traits(isStaticArray, T)) |
| 4634 |
{ |
| 4635 |
static if (initialize) |
| 4636 |
obj = T.init; |
| 4637 |
} |
| 4638 |
|
| 4639 |
@safe unittest |
| 4640 |
{ |
| 4641 |
{ |
| 4642 |
int a = 42; |
| 4643 |
destroy!false(a); |
| 4644 |
assert(a == 42); |
| 4645 |
destroy(a); |
| 4646 |
assert(a == 0); |
| 4647 |
} |
| 4648 |
{ |
| 4649 |
float a = 42; |
| 4650 |
destroy!false(a); |
| 4651 |
assert(a == 42); |
| 4652 |
destroy(a); |
| 4653 |
assert(a != a); // isnan |
| 4654 |
} |
| 4655 |
} |
| 4656 |
|
| 4657 |
@safe unittest |
| 4658 |
{ |
| 4659 |
// Bugzilla 14746 |
| 4660 |
static struct HasDtor |
| 4661 |
{ |
| 4662 |
~this() { assert(0); } |
| 4663 |
} |
| 4664 |
static struct Owner |
| 4665 |
{ |
| 4666 |
HasDtor* ptr; |
| 4667 |
alias ptr this; |
| 4668 |
} |
| 4669 |
|
| 4670 |
Owner o; |
| 4671 |
assert(o.ptr is null); |
| 4672 |
destroy(o); // must not reach in HasDtor.__dtor() |
| 4673 |
} |
| 4674 |
|
| 4675 |
/* ************************************************************************ |
| 4676 |
COMPILER SUPPORT |
| 4677 |
The compiler lowers certain expressions to instantiations of the following |
| 4678 |
templates. They must be implicitly imported, which is why they are here |
| 4679 |
in this file. They must also be `public` as they must be visible from the |
| 4680 |
scope in which they are instantiated. They are explicitly undocumented as |
| 4681 |
they are only intended to be instantiated by the compiler, not the user. |
| 4682 |
**************************************************************************/ |
| 4683 |
|
| 4684 |
public import core.internal.entrypoint : _d_cmain; |
| 4685 |
|
| 4686 |
public import core.internal.array.appending : _d_arrayappendT; |
| 4687 |
version (D_ProfileGC) |
| 4688 |
{ |
| 4689 |
public import core.internal.array.appending : _d_arrayappendTTrace; |
| 4690 |
public import core.internal.array.appending : _d_arrayappendcTXTrace; |
| 4691 |
public import core.internal.array.concatenation : _d_arraycatnTXTrace; |
| 4692 |
public import core.lifetime : _d_newitemTTrace; |
| 4693 |
public import core.internal.array.construction : _d_newarrayTTrace; |
| 4694 |
public import core.internal.array.construction : _d_newarraymTXTrace; |
| 4695 |
} |
| 4696 |
public import core.internal.array.appending : _d_arrayappendcTX; |
| 4697 |
public import core.internal.array.comparison : __cmp; |
| 4698 |
public import core.internal.array.equality : __equals; |
| 4699 |
public import core.internal.array.casting: __ArrayCast; |
| 4700 |
public import core.internal.array.concatenation : _d_arraycatnTX; |
| 4701 |
public import core.internal.array.construction : _d_arrayctor; |
| 4702 |
public import core.internal.array.construction : _d_arraysetctor; |
| 4703 |
public import core.internal.array.construction : _d_newarrayT; |
| 4704 |
public import core.internal.array.construction : _d_newarraymTX; |
| 4705 |
public import core.internal.array.arrayassign : _d_arrayassign_l; |
| 4706 |
public import core.internal.array.arrayassign : _d_arrayassign_r; |
| 4707 |
public import core.internal.array.arrayassign : _d_arraysetassign; |
| 4708 |
public import core.internal.array.capacity: _d_arraysetlengthTImpl; |
| 4709 |
|
| 4710 |
public import core.internal.dassert: _d_assert_fail; |
| 4711 |
|
| 4712 |
public import core.internal.destruction: __ArrayDtor; |
| 4713 |
|
| 4714 |
public import core.internal.moving: __move_post_blt; |
| 4715 |
|
| 4716 |
public import core.internal.postblit: __ArrayPostblit; |
| 4717 |
|
| 4718 |
public import core.internal.switch_: __switch; |
| 4719 |
public import core.internal.switch_: __switch_error; |
| 4720 |
|
| 4721 |
public import core.lifetime : _d_delstructImpl; |
| 4722 |
public import core.lifetime : _d_newThrowable; |
| 4723 |
public import core.lifetime : _d_newclassT; |
| 4724 |
public import core.lifetime : _d_newclassTTrace; |
| 4725 |
public import core.lifetime : _d_newitemT; |
| 4726 |
|
| 4727 |
public @trusted @nogc nothrow pure extern (C) void _d_delThrowable(scope Throwable); |
| 4728 |
|
| 4729 |
// Compare class and interface objects for ordering. |
| 4730 |
int __cmp(C1, C2)(C1 lhs, C2 rhs) |
| 4731 |
if ((is(C1 : const(Object)) || (is(C1 == interface) && (__traits(getLinkage, C1) == "D"))) && |
| 4732 |
(is(C2 : const(Object)) || (is(C2 == interface) && (__traits(getLinkage, C2) == "D")))) |
| 4733 |
{ |
| 4734 |
static if (is(C1 == typeof(null)) && is(C2 == typeof(null))) |
| 4735 |
{ |
| 4736 |
return 0; |
| 4737 |
} |
| 4738 |
else static if (is(C1 == typeof(null))) |
| 4739 |
{ |
| 4740 |
// Regard null references as always being "less than" |
| 4741 |
return -1; |
| 4742 |
} |
| 4743 |
else static if (is(C2 == typeof(null))) |
| 4744 |
{ |
| 4745 |
return 1; |
| 4746 |
} |
| 4747 |
else |
| 4748 |
{ |
| 4749 |
if (lhs is rhs) |
| 4750 |
return 0; |
| 4751 |
if (lhs is null) |
| 4752 |
return -1; |
| 4753 |
if (rhs is null) |
| 4754 |
return 1; |
| 4755 |
return lhs.opCmp(rhs); |
| 4756 |
} |
| 4757 |
} |
| 4758 |
|
| 4759 |
// objects |
| 4760 |
@safe unittest |
| 4761 |
{ |
| 4762 |
class C |
| 4763 |
{ |
| 4764 |
int i; |
| 4765 |
this(int i) { this.i = i; } |
| 4766 |
|
| 4767 |
override int opCmp(Object c) const @safe |
| 4768 |
{ |
| 4769 |
return i - (cast(C)c).i; |
| 4770 |
} |
| 4771 |
} |
| 4772 |
|
| 4773 |
auto c1 = new C(1); |
| 4774 |
auto c2 = new C(2); |
| 4775 |
assert(__cmp(c1, null) > 0); |
| 4776 |
assert(__cmp(null, c1) < 0); |
| 4777 |
assert(__cmp(c1, c1) == 0); |
| 4778 |
assert(__cmp(c1, c2) < 0); |
| 4779 |
assert(__cmp(c2, c1) > 0); |
| 4780 |
|
| 4781 |
assert(__cmp([c1, c1][], [c2, c2][]) < 0); |
| 4782 |
assert(__cmp([c2, c2], [c1, c1]) > 0); |
| 4783 |
} |
| 4784 |
|
| 4785 |
// structs |
| 4786 |
@safe unittest |
| 4787 |
{ |
| 4788 |
struct C |
| 4789 |
{ |
| 4790 |
ubyte i; |
| 4791 |
this(ubyte i) { this.i = i; } |
| 4792 |
} |
| 4793 |
|
| 4794 |
auto c1 = C(1); |
| 4795 |
auto c2 = C(2); |
| 4796 |
|
| 4797 |
assert(__cmp([c1, c1][], [c2, c2][]) < 0); |
| 4798 |
assert(__cmp([c2, c2], [c1, c1]) > 0); |
| 4799 |
assert(__cmp([c2, c2], [c2, c1]) > 0); |
| 4800 |
} |
| 4801 |
|
| 4802 |
@safe unittest |
| 4803 |
{ |
| 4804 |
auto a = "hello"c; |
| 4805 |
|
| 4806 |
assert(a > "hel"); |
| 4807 |
assert(a >= "hel"); |
| 4808 |
assert(a < "helloo"); |
| 4809 |
assert(a <= "helloo"); |
| 4810 |
assert(a > "betty"); |
| 4811 |
assert(a >= "betty"); |
| 4812 |
assert(a == "hello"); |
| 4813 |
assert(a <= "hello"); |
| 4814 |
assert(a >= "hello"); |
| 4815 |
assert(a < "я"); |
| 4816 |
} |
| 4817 |
|
| 4818 |
// Used in Exception Handling LSDA tables to 'wrap' C++ type info |
| 4819 |
// so it can be distinguished from D TypeInfo |
| 4820 |
class __cpp_type_info_ptr |
| 4821 |
{ |
| 4822 |
void* ptr; // opaque pointer to C++ RTTI type info |
| 4823 |
} |
| 4824 |
|
| 4825 |
// Compiler hook into the runtime implementation of array (vector) operations. |
| 4826 |
template _arrayOp(Args...) |
| 4827 |
{ |
| 4828 |
import core.internal.array.operations; |
| 4829 |
alias _arrayOp = arrayOp!Args; |
| 4830 |
} |
| 4831 |
|
| 4832 |
public import core.builtins : __ctfeWrite; |
| 4833 |
|
| 4834 |
/** |
| 4835 |
|
| 4836 |
Provides an "inline import", i.e. an `import` that is only available for a |
| 4837 |
limited lookup. For example: |
| 4838 |
|
| 4839 |
--- |
| 4840 |
void fun(imported!"std.stdio".File input) |
| 4841 |
{ |
| 4842 |
... use File from std.stdio normally ... |
| 4843 |
} |
| 4844 |
--- |
| 4845 |
|
| 4846 |
There is no need to import `std.stdio` at top level, so `fun` carries its own |
| 4847 |
dependencies. The same approach can be used for template constraints: |
| 4848 |
|
| 4849 |
--- |
| 4850 |
void fun(T)(imported!"std.stdio".File input, T value) |
| 4851 |
if (imported!"std.traits".isIntegral!T) |
| 4852 |
{ |
| 4853 |
... |
| 4854 |
} |
| 4855 |
--- |
| 4856 |
|
| 4857 |
An inline import may be used in conjunction with the `with` statement as well. |
| 4858 |
Inside the scope controlled by `with`, all symbols in the imported module are |
| 4859 |
made available: |
| 4860 |
|
| 4861 |
--- |
| 4862 |
void fun() |
| 4863 |
{ |
| 4864 |
with (imported!"std.datetime") |
| 4865 |
with (imported!"std.stdio") |
| 4866 |
{ |
| 4867 |
Clock.currTime.writeln; |
| 4868 |
} |
| 4869 |
} |
| 4870 |
--- |
| 4871 |
|
| 4872 |
The advantages of inline imports over top-level uses of the `import` declaration |
| 4873 |
are the following: |
| 4874 |
|
| 4875 |
$(UL |
| 4876 |
$(LI The `imported` template specifies dependencies at declaration level, not at |
| 4877 |
module level. This allows reasoning about the dependency cost of declarations in |
| 4878 |
separation instead of aggregated at module level.) |
| 4879 |
$(LI Declarations using `imported` are easier to move around because they don't |
| 4880 |
require top-level context, making for simpler and quicker refactorings.) |
| 4881 |
$(LI Declarations using `imported` scale better with templates. This is because |
| 4882 |
templates that are not instantiated do not have their parameters and constraints |
| 4883 |
instantiated, so additional modules are not imported without necessity. This |
| 4884 |
makes the cost of unused templates negligible. Dependencies are pulled on a need |
| 4885 |
basis depending on the declarations used by client code.) |
| 4886 |
) |
| 4887 |
|
| 4888 |
The use of `imported` also has drawbacks: |
| 4889 |
|
| 4890 |
$(UL |
| 4891 |
$(LI If most declarations in a module need the same imports, then factoring them |
| 4892 |
at top level, outside the declarations, is simpler than repeating them.) |
| 4893 |
$(LI Traditional dependency-tracking tools such as make and other build systems |
| 4894 |
assume file-level dependencies and need special tooling (such as rdmd) in order |
| 4895 |
to work efficiently.) |
| 4896 |
$(LI Dependencies at the top of a module are easier to inspect quickly than |
| 4897 |
dependencies spread throughout the module.) |
| 4898 |
) |
| 4899 |
|
| 4900 |
See_Also: The $(HTTP forum.dlang.org/post/[email protected], |
| 4901 |
forum discussion) that led to the creation of the `imported` facility. Credit is |
| 4902 |
due to Daniel Nielsen and Dominikus Dittes Scherkl. |
| 4903 |
|
| 4904 |
*/ |
| 4905 |
template imported(string moduleName) |
| 4906 |
{ |
| 4907 |
mixin("import imported = " ~ moduleName ~ ";"); |
| 4908 |
} |