| 1 |
/** |
| 2 |
* Contains traits for runtime internal usage. |
| 3 |
* |
| 4 |
* Copyright: Copyright Digital Mars 2014 -. |
| 5 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 6 |
* Authors: Martin Nowak |
| 7 |
* Source: $(DRUNTIMESRC core/internal/_traits.d) |
| 8 |
*/ |
| 9 |
module core.internal.traits; |
| 10 |
|
| 11 |
alias AliasSeq(TList...) = TList; |
| 12 |
|
| 13 |
template Fields(T) |
| 14 |
{ |
| 15 |
static if (is(T == struct) || is(T == union)) |
| 16 |
alias Fields = typeof(T.tupleof[0 .. $ - __traits(isNested, T)]); |
| 17 |
else static if (is(T == class) || is(T == interface)) |
| 18 |
alias Fields = typeof(T.tupleof); |
| 19 |
else |
| 20 |
alias Fields = AliasSeq!T; |
| 21 |
} |
| 22 |
|
| 23 |
T trustedCast(T, U)(auto ref U u) @trusted pure nothrow |
| 24 |
{ |
| 25 |
return cast(T)u; |
| 26 |
} |
| 27 |
|
| 28 |
alias Unconst(T : const U, U) = U; |
| 29 |
|
| 30 |
/// taken from std.traits.Unqual |
| 31 |
template Unqual(T : const U, U) |
| 32 |
{ |
| 33 |
static if (is(U == shared V, V)) |
| 34 |
alias Unqual = V; |
| 35 |
else |
| 36 |
alias Unqual = U; |
| 37 |
} |
| 38 |
|
| 39 |
template BaseElemOf(T) |
| 40 |
{ |
| 41 |
static if (is(OriginalType!T == E[N], E, size_t N)) |
| 42 |
alias BaseElemOf = BaseElemOf!E; |
| 43 |
else |
| 44 |
alias BaseElemOf = T; |
| 45 |
} |
| 46 |
|
| 47 |
unittest |
| 48 |
{ |
| 49 |
static assert(is(BaseElemOf!(int) == int)); |
| 50 |
static assert(is(BaseElemOf!(int[1]) == int)); |
| 51 |
static assert(is(BaseElemOf!(int[1][2]) == int)); |
| 52 |
static assert(is(BaseElemOf!(int[1][]) == int[1][])); |
| 53 |
static assert(is(BaseElemOf!(int[][1]) == int[])); |
| 54 |
enum E : int[2]{ test = [0, 1] } |
| 55 |
static assert(is(BaseElemOf!(E) == int)); |
| 56 |
} |
| 57 |
|
| 58 |
// [For internal use] |
| 59 |
template ModifyTypePreservingTQ(alias Modifier, T) |
| 60 |
{ |
| 61 |
static if (is(T U == immutable U)) alias ModifyTypePreservingTQ = immutable Modifier!U; |
| 62 |
else static if (is(T U == shared inout const U)) alias ModifyTypePreservingTQ = shared inout const Modifier!U; |
| 63 |
else static if (is(T U == shared inout U)) alias ModifyTypePreservingTQ = shared inout Modifier!U; |
| 64 |
else static if (is(T U == shared const U)) alias ModifyTypePreservingTQ = shared const Modifier!U; |
| 65 |
else static if (is(T U == shared U)) alias ModifyTypePreservingTQ = shared Modifier!U; |
| 66 |
else static if (is(T U == inout const U)) alias ModifyTypePreservingTQ = inout const Modifier!U; |
| 67 |
else static if (is(T U == inout U)) alias ModifyTypePreservingTQ = inout Modifier!U; |
| 68 |
else static if (is(T U == const U)) alias ModifyTypePreservingTQ = const Modifier!U; |
| 69 |
else alias ModifyTypePreservingTQ = Modifier!T; |
| 70 |
} |
| 71 |
@safe unittest |
| 72 |
{ |
| 73 |
alias Intify(T) = int; |
| 74 |
static assert(is(ModifyTypePreservingTQ!(Intify, real) == int)); |
| 75 |
static assert(is(ModifyTypePreservingTQ!(Intify, const real) == const int)); |
| 76 |
static assert(is(ModifyTypePreservingTQ!(Intify, inout real) == inout int)); |
| 77 |
static assert(is(ModifyTypePreservingTQ!(Intify, inout const real) == inout const int)); |
| 78 |
static assert(is(ModifyTypePreservingTQ!(Intify, shared real) == shared int)); |
| 79 |
static assert(is(ModifyTypePreservingTQ!(Intify, shared const real) == shared const int)); |
| 80 |
static assert(is(ModifyTypePreservingTQ!(Intify, shared inout real) == shared inout int)); |
| 81 |
static assert(is(ModifyTypePreservingTQ!(Intify, shared inout const real) == shared inout const int)); |
| 82 |
static assert(is(ModifyTypePreservingTQ!(Intify, immutable real) == immutable int)); |
| 83 |
} |
| 84 |
|
| 85 |
// Substitute all `inout` qualifiers that appears in T to `const` |
| 86 |
template substInout(T) |
| 87 |
{ |
| 88 |
static if (is(T == immutable)) |
| 89 |
{ |
| 90 |
alias substInout = T; |
| 91 |
} |
| 92 |
else static if (is(T : shared const U, U) || is(T : const U, U)) |
| 93 |
{ |
| 94 |
// U is top-unqualified |
| 95 |
mixin("alias substInout = " |
| 96 |
~ (is(T == shared) ? "shared " : "") |
| 97 |
~ (is(T == const) || is(T == inout) ? "const " : "") // substitute inout to const |
| 98 |
~ "substInoutForm!U;"); |
| 99 |
} |
| 100 |
else |
| 101 |
static assert(0); |
| 102 |
} |
| 103 |
|
| 104 |
private template substInoutForm(T) |
| 105 |
{ |
| 106 |
static if (is(T == struct) || is(T == class) || is(T == union) || is(T == interface)) |
| 107 |
{ |
| 108 |
alias substInoutForm = T; // prevent matching to the form of alias-this-ed type |
| 109 |
} |
| 110 |
else static if (is(T : V[K], K, V)) alias substInoutForm = substInout!V[substInout!K]; |
| 111 |
else static if (is(T : U[n], U, size_t n)) alias substInoutForm = substInout!U[n]; |
| 112 |
else static if (is(T : U[], U)) alias substInoutForm = substInout!U[]; |
| 113 |
else static if (is(T : U*, U)) alias substInoutForm = substInout!U*; |
| 114 |
else alias substInoutForm = T; |
| 115 |
} |
| 116 |
|
| 117 |
/// used to declare an extern(D) function that is defined in a different module |
| 118 |
template externDFunc(string fqn, T:FT*, FT) if (is(FT == function)) |
| 119 |
{ |
| 120 |
static if (is(FT RT == return) && is(FT Args == function)) |
| 121 |
{ |
| 122 |
import core.demangle : mangleFunc; |
| 123 |
enum decl = { |
| 124 |
string s = "extern(D) RT externDFunc(Args)"; |
| 125 |
foreach (attr; __traits(getFunctionAttributes, FT)) |
| 126 |
s ~= " " ~ attr; |
| 127 |
return s ~ ";"; |
| 128 |
}(); |
| 129 |
pragma(mangle, mangleFunc!T(fqn)) mixin(decl); |
| 130 |
} |
| 131 |
else |
| 132 |
static assert(0); |
| 133 |
} |
| 134 |
|
| 135 |
template staticIota(int beg, int end) |
| 136 |
{ |
| 137 |
static if (beg + 1 >= end) |
| 138 |
{ |
| 139 |
static if (beg >= end) |
| 140 |
{ |
| 141 |
alias staticIota = AliasSeq!(); |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
alias staticIota = AliasSeq!(+beg); |
| 146 |
} |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
enum mid = beg + (end - beg) / 2; |
| 151 |
alias staticIota = AliasSeq!(staticIota!(beg, mid), staticIota!(mid, end)); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
private struct __InoutWorkaroundStruct {} |
| 156 |
@property T rvalueOf(T)(T val) { return val; } |
| 157 |
@property T rvalueOf(T)(inout __InoutWorkaroundStruct = __InoutWorkaroundStruct.init); |
| 158 |
@property ref T lvalueOf(T)(inout __InoutWorkaroundStruct = __InoutWorkaroundStruct.init); |
| 159 |
|
| 160 |
// taken from std.traits.isAssignable |
| 161 |
template isAssignable(Lhs, Rhs = Lhs) |
| 162 |
{ |
| 163 |
enum isAssignable = __traits(compiles, lvalueOf!Lhs = rvalueOf!Rhs) && __traits(compiles, lvalueOf!Lhs = lvalueOf!Rhs); |
| 164 |
} |
| 165 |
|
| 166 |
// taken from std.traits.isInnerClass |
| 167 |
template isInnerClass(T) if (is(T == class)) |
| 168 |
{ |
| 169 |
static if (is(typeof(T.outer))) |
| 170 |
{ |
| 171 |
template hasOuterMember(T...) |
| 172 |
{ |
| 173 |
static if (T.length == 0) |
| 174 |
enum hasOuterMember = false; |
| 175 |
else |
| 176 |
enum hasOuterMember = T[0] == "outer" || hasOuterMember!(T[1 .. $]); |
| 177 |
} |
| 178 |
enum isInnerClass = __traits(isSame, typeof(T.outer), __traits(parent, T)) && !hasOuterMember!(__traits(allMembers, T)); |
| 179 |
} |
| 180 |
else |
| 181 |
enum isInnerClass = false; |
| 182 |
} |
| 183 |
|
| 184 |
template dtorIsNothrow(T) |
| 185 |
{ |
| 186 |
enum dtorIsNothrow = is(typeof(function{T t=void;}) : void function() nothrow); |
| 187 |
} |
| 188 |
|
| 189 |
// taken from std.meta.allSatisfy |
| 190 |
template allSatisfy(alias F, T...) |
| 191 |
{ |
| 192 |
static foreach (Ti; T) |
| 193 |
{ |
| 194 |
static if (!is(typeof(allSatisfy) == bool) && // not yet defined |
| 195 |
!F!(Ti)) |
| 196 |
{ |
| 197 |
enum allSatisfy = false; |
| 198 |
} |
| 199 |
} |
| 200 |
static if (!is(typeof(allSatisfy) == bool)) // if not yet defined |
| 201 |
{ |
| 202 |
enum allSatisfy = true; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// taken from std.meta.anySatisfy |
| 207 |
template anySatisfy(alias F, Ts...) |
| 208 |
{ |
| 209 |
static foreach (T; Ts) |
| 210 |
{ |
| 211 |
static if (!is(typeof(anySatisfy) == bool) && // not yet defined |
| 212 |
F!T) |
| 213 |
{ |
| 214 |
enum anySatisfy = true; |
| 215 |
} |
| 216 |
} |
| 217 |
static if (!is(typeof(anySatisfy) == bool)) // if not yet defined |
| 218 |
{ |
| 219 |
enum anySatisfy = false; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
// simplified from std.traits.maxAlignment |
| 224 |
template maxAlignment(Ts...) |
| 225 |
if (Ts.length > 0) |
| 226 |
{ |
| 227 |
enum maxAlignment = |
| 228 |
{ |
| 229 |
size_t result = 0; |
| 230 |
static foreach (T; Ts) |
| 231 |
if (T.alignof > result) result = T.alignof; |
| 232 |
return result; |
| 233 |
}(); |
| 234 |
} |
| 235 |
|
| 236 |
template classInstanceAlignment(T) |
| 237 |
if (is(T == class)) |
| 238 |
{ |
| 239 |
enum classInstanceAlignment = __traits(classInstanceAlignment, T); |
| 240 |
} |
| 241 |
|
| 242 |
/// See $(REF hasElaborateMove, std,traits) |
| 243 |
template hasElaborateMove(S) |
| 244 |
{ |
| 245 |
static if (__traits(isStaticArray, S)) |
| 246 |
{ |
| 247 |
enum bool hasElaborateMove = S.sizeof && hasElaborateMove!(BaseElemOf!S); |
| 248 |
} |
| 249 |
else static if (is(S == struct)) |
| 250 |
{ |
| 251 |
enum hasElaborateMove = (is(typeof(S.init.opPostMove(lvalueOf!S))) && |
| 252 |
!is(typeof(S.init.opPostMove(rvalueOf!S)))) || |
| 253 |
anySatisfy!(.hasElaborateMove, Fields!S); |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
enum bool hasElaborateMove = false; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
// std.traits.hasElaborateDestructor |
| 262 |
template hasElaborateDestructor(S) |
| 263 |
{ |
| 264 |
static if (__traits(isStaticArray, S)) |
| 265 |
{ |
| 266 |
enum bool hasElaborateDestructor = S.sizeof && hasElaborateDestructor!(BaseElemOf!S); |
| 267 |
} |
| 268 |
else static if (is(S == struct)) |
| 269 |
{ |
| 270 |
// Once https://issues.dlang.org/show_bug.cgi?id=24865 is fixed, then |
| 271 |
// this should be the implementation, but until that's fixed, we need the |
| 272 |
// uncommented code. |
| 273 |
// enum hasElaborateDestructor = __traits(hasMember, S, "__xdtor"); |
| 274 |
|
| 275 |
enum hasElaborateDestructor = hasDtor([__traits(allMembers, S)]); |
| 276 |
} |
| 277 |
else |
| 278 |
{ |
| 279 |
enum bool hasElaborateDestructor = false; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
private bool hasDtor(string[] members) |
| 284 |
{ |
| 285 |
foreach (name; members) |
| 286 |
{ |
| 287 |
if (name == "__xdtor") |
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
@safe unittest |
| 295 |
{ |
| 296 |
static struct NoDestructor {} |
| 297 |
static assert(!hasElaborateDestructor!NoDestructor); |
| 298 |
static assert(!hasElaborateDestructor!(NoDestructor[42])); |
| 299 |
static assert(!hasElaborateDestructor!(NoDestructor[0])); |
| 300 |
static assert(!hasElaborateDestructor!(NoDestructor[])); |
| 301 |
|
| 302 |
static struct HasDestructor { ~this() {} } |
| 303 |
static assert( hasElaborateDestructor!HasDestructor); |
| 304 |
static assert( hasElaborateDestructor!(HasDestructor[42])); |
| 305 |
static assert(!hasElaborateDestructor!(HasDestructor[0])); |
| 306 |
static assert(!hasElaborateDestructor!(HasDestructor[])); |
| 307 |
|
| 308 |
static struct HasDestructor2 { HasDestructor s; } |
| 309 |
static assert( hasElaborateDestructor!HasDestructor2); |
| 310 |
static assert( hasElaborateDestructor!(HasDestructor2[42])); |
| 311 |
static assert(!hasElaborateDestructor!(HasDestructor2[0])); |
| 312 |
static assert(!hasElaborateDestructor!(HasDestructor2[])); |
| 313 |
|
| 314 |
static class HasFinalizer { ~this() {} } |
| 315 |
static assert(!hasElaborateDestructor!HasFinalizer); |
| 316 |
|
| 317 |
static struct HasUnion { union { HasDestructor s; } } |
| 318 |
static assert(!hasElaborateDestructor!HasUnion); |
| 319 |
static assert(!hasElaborateDestructor!(HasUnion[42])); |
| 320 |
static assert(!hasElaborateDestructor!(HasUnion[0])); |
| 321 |
static assert(!hasElaborateDestructor!(HasUnion[])); |
| 322 |
|
| 323 |
static assert(!hasElaborateDestructor!int); |
| 324 |
static assert(!hasElaborateDestructor!(int[0])); |
| 325 |
static assert(!hasElaborateDestructor!(int[42])); |
| 326 |
static assert(!hasElaborateDestructor!(int[])); |
| 327 |
} |
| 328 |
|
| 329 |
// https://issues.dlang.org/show_bug.cgi?id=24865 |
| 330 |
@safe unittest |
| 331 |
{ |
| 332 |
static struct S2 { ~this() {} } |
| 333 |
static struct S3 { S2 field; } |
| 334 |
static struct S6 { S3[0] field; } |
| 335 |
|
| 336 |
static assert( hasElaborateDestructor!S2); |
| 337 |
static assert( hasElaborateDestructor!S3); |
| 338 |
static assert(!hasElaborateDestructor!S6); |
| 339 |
} |
| 340 |
|
| 341 |
// std.traits.hasElaborateCopyDestructor |
| 342 |
template hasElaborateCopyConstructor(S) |
| 343 |
{ |
| 344 |
static if (__traits(isStaticArray, S)) |
| 345 |
{ |
| 346 |
enum bool hasElaborateCopyConstructor = S.sizeof && hasElaborateCopyConstructor!(BaseElemOf!S); |
| 347 |
} |
| 348 |
else static if (is(S == struct)) |
| 349 |
{ |
| 350 |
enum hasElaborateCopyConstructor = __traits(hasCopyConstructor, S) || __traits(hasPostblit, S); |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
enum bool hasElaborateCopyConstructor = false; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
@safe unittest |
| 359 |
{ |
| 360 |
static struct S |
| 361 |
{ |
| 362 |
int x; |
| 363 |
this(return scope ref typeof(this) rhs) { } |
| 364 |
this(int x, int y) {} |
| 365 |
} |
| 366 |
|
| 367 |
static assert( hasElaborateCopyConstructor!S); |
| 368 |
static assert(!hasElaborateCopyConstructor!(S[0][1])); |
| 369 |
|
| 370 |
static struct S2 |
| 371 |
{ |
| 372 |
int x; |
| 373 |
this(int x, int y) {} |
| 374 |
} |
| 375 |
|
| 376 |
static assert(!hasElaborateCopyConstructor!S2); |
| 377 |
|
| 378 |
static struct S3 |
| 379 |
{ |
| 380 |
int x; |
| 381 |
this(return scope ref typeof(this) rhs, int x = 42) { } |
| 382 |
this(int x, int y) {} |
| 383 |
} |
| 384 |
|
| 385 |
static assert( hasElaborateCopyConstructor!S3); |
| 386 |
|
| 387 |
static struct S4 { union { S s; } } |
| 388 |
|
| 389 |
static assert(!hasElaborateCopyConstructor!S4); |
| 390 |
} |
| 391 |
|
| 392 |
template hasElaborateAssign(S) |
| 393 |
{ |
| 394 |
static if (__traits(isStaticArray, S)) |
| 395 |
{ |
| 396 |
enum bool hasElaborateAssign = S.sizeof && hasElaborateAssign!(BaseElemOf!S); |
| 397 |
} |
| 398 |
else static if (is(S == struct)) |
| 399 |
{ |
| 400 |
enum hasElaborateAssign = is(typeof(S.init.opAssign(rvalueOf!S))) || |
| 401 |
is(typeof(S.init.opAssign(lvalueOf!S))); |
| 402 |
} |
| 403 |
else |
| 404 |
{ |
| 405 |
enum bool hasElaborateAssign = false; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
unittest |
| 410 |
{ |
| 411 |
{ |
| 412 |
static struct S {} |
| 413 |
static assert(!hasElaborateAssign!S); |
| 414 |
static assert(!hasElaborateAssign!(S[10])); |
| 415 |
static assert(!hasElaborateAssign!(S[0])); |
| 416 |
static assert(!hasElaborateAssign!(S[])); |
| 417 |
} |
| 418 |
{ |
| 419 |
static struct S { int i; } |
| 420 |
static assert(!hasElaborateAssign!S); |
| 421 |
static assert(!hasElaborateAssign!(S[10])); |
| 422 |
static assert(!hasElaborateAssign!(S[0])); |
| 423 |
static assert(!hasElaborateAssign!(S[])); |
| 424 |
} |
| 425 |
{ |
| 426 |
static struct S { void opAssign(S) {} } |
| 427 |
static assert( hasElaborateAssign!S); |
| 428 |
static assert( hasElaborateAssign!(S[10])); |
| 429 |
static assert(!hasElaborateAssign!(S[0])); |
| 430 |
static assert(!hasElaborateAssign!(S[])); |
| 431 |
} |
| 432 |
{ |
| 433 |
static struct S { void opAssign(ref S) {} } |
| 434 |
static assert( hasElaborateAssign!S); |
| 435 |
static assert( hasElaborateAssign!(S[10])); |
| 436 |
static assert(!hasElaborateAssign!(S[0])); |
| 437 |
static assert(!hasElaborateAssign!(S[])); |
| 438 |
} |
| 439 |
{ |
| 440 |
static struct S { void opAssign(int) {} } |
| 441 |
static assert(!hasElaborateAssign!S); |
| 442 |
static assert(!hasElaborateAssign!(S[10])); |
| 443 |
static assert(!hasElaborateAssign!(S[0])); |
| 444 |
static assert(!hasElaborateAssign!(S[])); |
| 445 |
} |
| 446 |
{ |
| 447 |
static struct S { this(this) {} } |
| 448 |
static assert( hasElaborateAssign!S); |
| 449 |
static assert( hasElaborateAssign!(S[10])); |
| 450 |
static assert(!hasElaborateAssign!(S[0])); |
| 451 |
static assert(!hasElaborateAssign!(S[])); |
| 452 |
} |
| 453 |
// https://issues.dlang.org/show_bug.cgi?id=24834 |
| 454 |
/+ |
| 455 |
{ |
| 456 |
static struct S { this(ref S) {} } |
| 457 |
static assert( hasElaborateAssign!S); |
| 458 |
static assert( hasElaborateAssign!(S[10])); |
| 459 |
static assert(!hasElaborateAssign!(S[0])); |
| 460 |
static assert(!hasElaborateAssign!(S[])); |
| 461 |
} |
| 462 |
+/ |
| 463 |
{ |
| 464 |
static struct S { ~this() {} } |
| 465 |
static assert( hasElaborateAssign!S); |
| 466 |
static assert( hasElaborateAssign!(S[10])); |
| 467 |
static assert(!hasElaborateAssign!(S[0])); |
| 468 |
static assert(!hasElaborateAssign!(S[])); |
| 469 |
} |
| 470 |
{ |
| 471 |
static struct S { @disable void opAssign(S); } |
| 472 |
static assert(!hasElaborateAssign!S); |
| 473 |
static assert(!hasElaborateAssign!(S[10])); |
| 474 |
static assert(!hasElaborateAssign!(S[0])); |
| 475 |
static assert(!hasElaborateAssign!(S[])); |
| 476 |
} |
| 477 |
{ |
| 478 |
static struct Member {} |
| 479 |
static struct S { Member member; } |
| 480 |
static assert(!hasElaborateAssign!S); |
| 481 |
static assert(!hasElaborateAssign!(S[10])); |
| 482 |
static assert(!hasElaborateAssign!(S[0])); |
| 483 |
static assert(!hasElaborateAssign!(S[])); |
| 484 |
} |
| 485 |
{ |
| 486 |
static struct Member { void opAssign(Member) {} } |
| 487 |
static struct S { Member member; } |
| 488 |
static assert( hasElaborateAssign!S); |
| 489 |
static assert( hasElaborateAssign!(S[10])); |
| 490 |
static assert(!hasElaborateAssign!(S[0])); |
| 491 |
static assert(!hasElaborateAssign!(S[])); |
| 492 |
} |
| 493 |
{ |
| 494 |
static struct Member {} |
| 495 |
static struct S { Member member; void opAssign(S) {} } |
| 496 |
static assert( hasElaborateAssign!S); |
| 497 |
static assert( hasElaborateAssign!(S[10])); |
| 498 |
static assert(!hasElaborateAssign!(S[0])); |
| 499 |
static assert(!hasElaborateAssign!(S[])); |
| 500 |
} |
| 501 |
{ |
| 502 |
static struct Member { @disable void opAssign(Member); } |
| 503 |
static struct S { Member member; } |
| 504 |
static assert(!hasElaborateAssign!S); |
| 505 |
static assert(!hasElaborateAssign!(S[10])); |
| 506 |
static assert(!hasElaborateAssign!(S[0])); |
| 507 |
static assert(!hasElaborateAssign!(S[])); |
| 508 |
} |
| 509 |
{ |
| 510 |
static struct Member { @disable void opAssign(Member); } |
| 511 |
static struct S { Member member; void opAssign(S) {} } |
| 512 |
static assert( hasElaborateAssign!S); |
| 513 |
static assert( hasElaborateAssign!(S[10])); |
| 514 |
static assert(!hasElaborateAssign!(S[0])); |
| 515 |
static assert(!hasElaborateAssign!(S[])); |
| 516 |
} |
| 517 |
{ |
| 518 |
static struct Member { void opAssign(Member) {} } |
| 519 |
static struct S { Member member; @disable void opAssign(S); } |
| 520 |
static assert(!hasElaborateAssign!S); |
| 521 |
static assert(!hasElaborateAssign!(S[10])); |
| 522 |
static assert(!hasElaborateAssign!(S[0])); |
| 523 |
static assert(!hasElaborateAssign!(S[])); |
| 524 |
} |
| 525 |
{ |
| 526 |
static struct Member { void opAssign(Member) {} } |
| 527 |
static struct S { union { Member member; } } |
| 528 |
static assert(!hasElaborateAssign!S); |
| 529 |
static assert(!hasElaborateAssign!(S[10])); |
| 530 |
static assert(!hasElaborateAssign!(S[0])); |
| 531 |
static assert(!hasElaborateAssign!(S[])); |
| 532 |
} |
| 533 |
|
| 534 |
static assert(!hasElaborateAssign!int); |
| 535 |
static assert(!hasElaborateAssign!(string[])); |
| 536 |
static assert(!hasElaborateAssign!Object); |
| 537 |
} |
| 538 |
|
| 539 |
template hasIndirections(T) |
| 540 |
{ |
| 541 |
static if (is(T == enum)) |
| 542 |
enum hasIndirections = hasIndirections!(OriginalType!T); |
| 543 |
else static if (is(T == struct) || is(T == union)) |
| 544 |
enum hasIndirections = anySatisfy!(.hasIndirections, typeof(T.tupleof)); |
| 545 |
else static if (__traits(isAssociativeArray, T) || is(T == class) || is(T == interface)) |
| 546 |
enum hasIndirections = true; |
| 547 |
else static if (is(T == E[N], E, size_t N)) |
| 548 |
enum hasIndirections = T.sizeof && (is(immutable E == immutable void) || hasIndirections!(BaseElemOf!E)); |
| 549 |
else static if (isFunctionPointer!T) |
| 550 |
enum hasIndirections = false; |
| 551 |
else |
| 552 |
enum hasIndirections = isPointer!T || isDelegate!T || isDynamicArray!T; |
| 553 |
} |
| 554 |
|
| 555 |
@safe unittest |
| 556 |
{ |
| 557 |
static assert(!hasIndirections!int); |
| 558 |
static assert(!hasIndirections!(const int)); |
| 559 |
|
| 560 |
static assert( hasIndirections!(int*)); |
| 561 |
static assert( hasIndirections!(const int*)); |
| 562 |
|
| 563 |
static assert( hasIndirections!(int[])); |
| 564 |
static assert(!hasIndirections!(int[42])); |
| 565 |
static assert(!hasIndirections!(int[0])); |
| 566 |
|
| 567 |
static assert( hasIndirections!(int*)); |
| 568 |
static assert( hasIndirections!(int*[])); |
| 569 |
static assert( hasIndirections!(int*[42])); |
| 570 |
static assert(!hasIndirections!(int*[0])); |
| 571 |
|
| 572 |
static assert( hasIndirections!string); |
| 573 |
static assert( hasIndirections!(string[])); |
| 574 |
static assert( hasIndirections!(string[42])); |
| 575 |
static assert(!hasIndirections!(string[0])); |
| 576 |
|
| 577 |
static assert( hasIndirections!(void[])); |
| 578 |
static assert( hasIndirections!(void[17])); |
| 579 |
static assert(!hasIndirections!(void[0])); |
| 580 |
|
| 581 |
static assert( hasIndirections!(string[int])); |
| 582 |
static assert( hasIndirections!(string[int]*)); |
| 583 |
static assert( hasIndirections!(string[int][])); |
| 584 |
static assert( hasIndirections!(string[int][12])); |
| 585 |
static assert(!hasIndirections!(string[int][0])); |
| 586 |
|
| 587 |
static assert(!hasIndirections!(int function(string))); |
| 588 |
static assert( hasIndirections!(int delegate(string))); |
| 589 |
static assert(!hasIndirections!(const(int function(string)))); |
| 590 |
static assert( hasIndirections!(const(int delegate(string)))); |
| 591 |
static assert(!hasIndirections!(immutable(int function(string)))); |
| 592 |
static assert( hasIndirections!(immutable(int delegate(string)))); |
| 593 |
|
| 594 |
static class C {} |
| 595 |
static assert( hasIndirections!C); |
| 596 |
|
| 597 |
static interface I {} |
| 598 |
static assert( hasIndirections!I); |
| 599 |
|
| 600 |
{ |
| 601 |
enum E : int { a } |
| 602 |
static assert(!hasIndirections!E); |
| 603 |
} |
| 604 |
{ |
| 605 |
enum E : int* { a } |
| 606 |
static assert( hasIndirections!E); |
| 607 |
} |
| 608 |
{ |
| 609 |
enum E : string { a = "" } |
| 610 |
static assert( hasIndirections!E); |
| 611 |
} |
| 612 |
{ |
| 613 |
enum E : int[] { a = null } |
| 614 |
static assert( hasIndirections!E); |
| 615 |
} |
| 616 |
{ |
| 617 |
enum E : int[3] { a = [1, 2, 3] } |
| 618 |
static assert(!hasIndirections!E); |
| 619 |
} |
| 620 |
{ |
| 621 |
enum E : int*[3] { a = [null, null, null] } |
| 622 |
static assert( hasIndirections!E); |
| 623 |
} |
| 624 |
{ |
| 625 |
enum E : int*[0] { a = int*[0].init } |
| 626 |
static assert(!hasIndirections!E); |
| 627 |
} |
| 628 |
{ |
| 629 |
enum E : C { a = null } |
| 630 |
static assert( hasIndirections!E); |
| 631 |
} |
| 632 |
{ |
| 633 |
enum E : I { a = null } |
| 634 |
static assert( hasIndirections!E); |
| 635 |
} |
| 636 |
|
| 637 |
{ |
| 638 |
static struct S {} |
| 639 |
static assert(!hasIndirections!S); |
| 640 |
|
| 641 |
enum E : S { a = S.init } |
| 642 |
static assert(!hasIndirections!S); |
| 643 |
} |
| 644 |
{ |
| 645 |
static struct S { int i; } |
| 646 |
static assert(!hasIndirections!S); |
| 647 |
|
| 648 |
enum E : S { a = S.init } |
| 649 |
static assert(!hasIndirections!S); |
| 650 |
} |
| 651 |
{ |
| 652 |
static struct S { C c; } |
| 653 |
static assert( hasIndirections!S); |
| 654 |
|
| 655 |
enum E : S { a = S.init } |
| 656 |
static assert( hasIndirections!S); |
| 657 |
} |
| 658 |
{ |
| 659 |
static struct S { int[] arr; } |
| 660 |
static assert( hasIndirections!S); |
| 661 |
|
| 662 |
enum E : S { a = S.init } |
| 663 |
static assert( hasIndirections!S); |
| 664 |
} |
| 665 |
{ |
| 666 |
int local; |
| 667 |
struct S { void foo() { ++local; } } |
| 668 |
static assert( hasIndirections!S); |
| 669 |
|
| 670 |
enum E : S { a = S.init } |
| 671 |
static assert( hasIndirections!S); |
| 672 |
} |
| 673 |
|
| 674 |
{ |
| 675 |
static union U {} |
| 676 |
static assert(!hasIndirections!U); |
| 677 |
} |
| 678 |
{ |
| 679 |
static union U { int i; } |
| 680 |
static assert(!hasIndirections!U); |
| 681 |
} |
| 682 |
{ |
| 683 |
static union U { C c; } |
| 684 |
static assert( hasIndirections!U); |
| 685 |
} |
| 686 |
{ |
| 687 |
static union U { int[] arr; } |
| 688 |
static assert( hasIndirections!U); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
// https://issues.dlang.org/show_bug.cgi?id=12000 |
| 693 |
@safe unittest |
| 694 |
{ |
| 695 |
static struct S(T) |
| 696 |
{ |
| 697 |
static assert(hasIndirections!T); |
| 698 |
} |
| 699 |
|
| 700 |
static class A(T) |
| 701 |
{ |
| 702 |
S!A a; |
| 703 |
} |
| 704 |
|
| 705 |
A!int dummy; |
| 706 |
} |
| 707 |
|
| 708 |
// https://github.com/dlang/dmd/issues/20812 |
| 709 |
@safe unittest |
| 710 |
{ |
| 711 |
static assert(!hasIndirections!void); |
| 712 |
static assert(!hasIndirections!(const void)); |
| 713 |
static assert(!hasIndirections!(inout void)); |
| 714 |
static assert(!hasIndirections!(immutable void)); |
| 715 |
static assert(!hasIndirections!(shared void)); |
| 716 |
|
| 717 |
static assert( hasIndirections!(void*)); |
| 718 |
static assert( hasIndirections!(const void*)); |
| 719 |
static assert( hasIndirections!(inout void*)); |
| 720 |
static assert( hasIndirections!(immutable void*)); |
| 721 |
static assert( hasIndirections!(shared void*)); |
| 722 |
|
| 723 |
static assert( hasIndirections!(void[])); |
| 724 |
static assert( hasIndirections!(const void[])); |
| 725 |
static assert( hasIndirections!(inout void[])); |
| 726 |
static assert( hasIndirections!(immutable void[])); |
| 727 |
static assert( hasIndirections!(shared void[])); |
| 728 |
|
| 729 |
static assert( hasIndirections!(void[42])); |
| 730 |
static assert( hasIndirections!(const void[42])); |
| 731 |
static assert( hasIndirections!(inout void[42])); |
| 732 |
static assert( hasIndirections!(immutable void[42])); |
| 733 |
static assert( hasIndirections!(shared void[42])); |
| 734 |
|
| 735 |
static assert(!hasIndirections!(void[0])); |
| 736 |
static assert(!hasIndirections!(const void[0])); |
| 737 |
static assert(!hasIndirections!(inout void[0])); |
| 738 |
static assert(!hasIndirections!(immutable void[0])); |
| 739 |
static assert(!hasIndirections!(shared void[0])); |
| 740 |
} |
| 741 |
|
| 742 |
template hasUnsharedIndirections(T) |
| 743 |
{ |
| 744 |
static if (is(T == immutable)) |
| 745 |
enum hasUnsharedIndirections = false; |
| 746 |
else static if (is(T == struct) || is(T == union)) |
| 747 |
enum hasUnsharedIndirections = anySatisfy!(.hasUnsharedIndirections, Fields!T); |
| 748 |
else static if (is(T : E[N], E, size_t N)) |
| 749 |
enum hasUnsharedIndirections = is(E == void) ? false : hasUnsharedIndirections!E; |
| 750 |
else static if (isFunctionPointer!T) |
| 751 |
enum hasUnsharedIndirections = false; |
| 752 |
else static if (isPointer!T) |
| 753 |
enum hasUnsharedIndirections = !is(T : shared(U)*, U) && !is(T : immutable(U)*, U); |
| 754 |
else static if (isDynamicArray!T) |
| 755 |
enum hasUnsharedIndirections = !is(T : shared(V)[], V) && !is(T : immutable(V)[], V); |
| 756 |
else static if (is(T == class) || is(T == interface)) |
| 757 |
enum hasUnsharedIndirections = !is(T : shared(W), W); |
| 758 |
else |
| 759 |
enum hasUnsharedIndirections = isDelegate!T || __traits(isAssociativeArray, T); // TODO: how to handle these? |
| 760 |
} |
| 761 |
|
| 762 |
unittest |
| 763 |
{ |
| 764 |
static struct Foo { shared(int)* val; } |
| 765 |
|
| 766 |
static assert(!hasUnsharedIndirections!(immutable(char)*)); |
| 767 |
static assert(!hasUnsharedIndirections!(string)); |
| 768 |
|
| 769 |
static assert(!hasUnsharedIndirections!(Foo)); |
| 770 |
static assert( hasUnsharedIndirections!(Foo*)); |
| 771 |
static assert(!hasUnsharedIndirections!(shared(Foo)*)); |
| 772 |
static assert(!hasUnsharedIndirections!(immutable(Foo)*)); |
| 773 |
|
| 774 |
int local; |
| 775 |
struct HasContextPointer { int opCall() { return ++local; } } |
| 776 |
static assert(hasIndirections!HasContextPointer); |
| 777 |
} |
| 778 |
|
| 779 |
enum bool isAggregateType(T) = is(T == struct) || is(T == union) || |
| 780 |
is(T == class) || is(T == interface); |
| 781 |
|
| 782 |
enum bool isPointer(T) = is(T == U*, U) && !isAggregateType!T; |
| 783 |
|
| 784 |
enum bool isDynamicArray(T) = is(DynamicArrayTypeOf!T) && !isAggregateType!T; |
| 785 |
|
| 786 |
template OriginalType(T) |
| 787 |
{ |
| 788 |
template Impl(T) |
| 789 |
{ |
| 790 |
static if (is(T U == enum)) alias Impl = OriginalType!U; |
| 791 |
else alias Impl = T; |
| 792 |
} |
| 793 |
|
| 794 |
alias OriginalType = ModifyTypePreservingTQ!(Impl, T); |
| 795 |
} |
| 796 |
|
| 797 |
template DynamicArrayTypeOf(T) |
| 798 |
{ |
| 799 |
static if (is(AliasThisTypeOf!T AT) && !is(AT[] == AT)) |
| 800 |
alias X = DynamicArrayTypeOf!AT; |
| 801 |
else |
| 802 |
alias X = OriginalType!T; |
| 803 |
|
| 804 |
static if (is(Unqual!X : E[], E) && !is(typeof({ enum n = X.length; }))) |
| 805 |
alias DynamicArrayTypeOf = X; |
| 806 |
else |
| 807 |
static assert(0, T.stringof ~ " is not a dynamic array"); |
| 808 |
} |
| 809 |
|
| 810 |
private template AliasThisTypeOf(T) |
| 811 |
if (isAggregateType!T) |
| 812 |
{ |
| 813 |
alias members = __traits(getAliasThis, T); |
| 814 |
|
| 815 |
static if (members.length == 1) |
| 816 |
alias AliasThisTypeOf = typeof(__traits(getMember, T.init, members[0])); |
| 817 |
else |
| 818 |
static assert(0, T.stringof~" does not have alias this type"); |
| 819 |
} |
| 820 |
|
| 821 |
template isFunctionPointer(T...) |
| 822 |
if (T.length == 1) |
| 823 |
{ |
| 824 |
static if (is(T[0] U) || is(typeof(T[0]) U)) |
| 825 |
{ |
| 826 |
static if (is(U F : F*) && is(F == function)) |
| 827 |
enum bool isFunctionPointer = true; |
| 828 |
else |
| 829 |
enum bool isFunctionPointer = false; |
| 830 |
} |
| 831 |
else |
| 832 |
enum bool isFunctionPointer = false; |
| 833 |
} |
| 834 |
|
| 835 |
template isDelegate(T...) |
| 836 |
if (T.length == 1) |
| 837 |
{ |
| 838 |
static if (is(typeof(& T[0]) U : U*) && is(typeof(& T[0]) U == delegate)) |
| 839 |
{ |
| 840 |
// T is a (nested) function symbol. |
| 841 |
enum bool isDelegate = true; |
| 842 |
} |
| 843 |
else static if (is(T[0] W) || is(typeof(T[0]) W)) |
| 844 |
{ |
| 845 |
// T is an expression or a type. Take the type of it and examine. |
| 846 |
enum bool isDelegate = is(W == delegate); |
| 847 |
} |
| 848 |
else |
| 849 |
enum bool isDelegate = false; |
| 850 |
} |
| 851 |
|
| 852 |
// std.meta.Filter |
| 853 |
template Filter(alias pred, TList...) |
| 854 |
{ |
| 855 |
static if (TList.length == 0) |
| 856 |
{ |
| 857 |
alias Filter = AliasSeq!(); |
| 858 |
} |
| 859 |
else static if (TList.length == 1) |
| 860 |
{ |
| 861 |
static if (pred!(TList[0])) |
| 862 |
alias Filter = AliasSeq!(TList[0]); |
| 863 |
else |
| 864 |
alias Filter = AliasSeq!(); |
| 865 |
} |
| 866 |
/* The next case speeds up compilation by reducing |
| 867 |
* the number of Filter instantiations |
| 868 |
*/ |
| 869 |
else static if (TList.length == 2) |
| 870 |
{ |
| 871 |
static if (pred!(TList[0])) |
| 872 |
{ |
| 873 |
static if (pred!(TList[1])) |
| 874 |
alias Filter = AliasSeq!(TList[0], TList[1]); |
| 875 |
else |
| 876 |
alias Filter = AliasSeq!(TList[0]); |
| 877 |
} |
| 878 |
else |
| 879 |
{ |
| 880 |
static if (pred!(TList[1])) |
| 881 |
alias Filter = AliasSeq!(TList[1]); |
| 882 |
else |
| 883 |
alias Filter = AliasSeq!(); |
| 884 |
} |
| 885 |
} |
| 886 |
else |
| 887 |
{ |
| 888 |
alias Filter = |
| 889 |
AliasSeq!( |
| 890 |
Filter!(pred, TList[ 0 .. $/2]), |
| 891 |
Filter!(pred, TList[$/2 .. $ ])); |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
// std.meta.staticMap |
| 896 |
template staticMap(alias F, T...) |
| 897 |
{ |
| 898 |
static if (T.length == 0) |
| 899 |
{ |
| 900 |
alias staticMap = AliasSeq!(); |
| 901 |
} |
| 902 |
else static if (T.length == 1) |
| 903 |
{ |
| 904 |
alias staticMap = AliasSeq!(F!(T[0])); |
| 905 |
} |
| 906 |
/* Cases 2 to 8 improve compile performance by reducing |
| 907 |
* the number of recursive instantiations of staticMap |
| 908 |
*/ |
| 909 |
else static if (T.length == 2) |
| 910 |
{ |
| 911 |
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1])); |
| 912 |
} |
| 913 |
else static if (T.length == 3) |
| 914 |
{ |
| 915 |
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2])); |
| 916 |
} |
| 917 |
else static if (T.length == 4) |
| 918 |
{ |
| 919 |
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]), F!(T[3])); |
| 920 |
} |
| 921 |
else static if (T.length == 5) |
| 922 |
{ |
| 923 |
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]), F!(T[3]), F!(T[4])); |
| 924 |
} |
| 925 |
else static if (T.length == 6) |
| 926 |
{ |
| 927 |
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]), F!(T[3]), F!(T[4]), F!(T[5])); |
| 928 |
} |
| 929 |
else static if (T.length == 7) |
| 930 |
{ |
| 931 |
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]), F!(T[3]), F!(T[4]), F!(T[5]), F!(T[6])); |
| 932 |
} |
| 933 |
else static if (T.length == 8) |
| 934 |
{ |
| 935 |
alias staticMap = AliasSeq!(F!(T[0]), F!(T[1]), F!(T[2]), F!(T[3]), F!(T[4]), F!(T[5]), F!(T[6]), F!(T[7])); |
| 936 |
} |
| 937 |
else |
| 938 |
{ |
| 939 |
alias staticMap = |
| 940 |
AliasSeq!( |
| 941 |
staticMap!(F, T[ 0 .. $/2]), |
| 942 |
staticMap!(F, T[$/2 .. $ ])); |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
// std.exception.assertCTFEable |
| 947 |
version (CoreUnittest) package(core) |
| 948 |
void assertCTFEable(alias dg)() |
| 949 |
{ |
| 950 |
static assert({ cast(void) dg(); return true; }()); |
| 951 |
cast(void) dg(); |
| 952 |
} |
| 953 |
|
| 954 |
// std.traits.FunctionTypeOf |
| 955 |
/* |
| 956 |
Get the function type from a callable object `func`. |
| 957 |
|
| 958 |
Using builtin `typeof` on a property function yields the types of the |
| 959 |
property value, not of the property function itself. Still, |
| 960 |
`FunctionTypeOf` is able to obtain function types of properties. |
| 961 |
|
| 962 |
Note: |
| 963 |
Do not confuse function types with function pointer types; function types are |
| 964 |
usually used for compile-time reflection purposes. |
| 965 |
*/ |
| 966 |
template FunctionTypeOf(func...) |
| 967 |
if (func.length == 1 /*&& isCallable!func*/) |
| 968 |
{ |
| 969 |
static if (is(typeof(& func[0]) Fsym : Fsym*) && is(Fsym == function) || is(typeof(& func[0]) Fsym == delegate)) |
| 970 |
{ |
| 971 |
alias FunctionTypeOf = Fsym; // HIT: (nested) function symbol |
| 972 |
} |
| 973 |
else static if (is(typeof(& func[0].opCall) Fobj == delegate)) |
| 974 |
{ |
| 975 |
alias FunctionTypeOf = Fobj; // HIT: callable object |
| 976 |
} |
| 977 |
else static if (is(typeof(& func[0].opCall) Ftyp : Ftyp*) && is(Ftyp == function)) |
| 978 |
{ |
| 979 |
alias FunctionTypeOf = Ftyp; // HIT: callable type |
| 980 |
} |
| 981 |
else static if (is(func[0] T) || is(typeof(func[0]) T)) |
| 982 |
{ |
| 983 |
static if (is(T == function)) |
| 984 |
alias FunctionTypeOf = T; // HIT: function |
| 985 |
else static if (is(T Fptr : Fptr*) && is(Fptr == function)) |
| 986 |
alias FunctionTypeOf = Fptr; // HIT: function pointer |
| 987 |
else static if (is(T Fdlg == delegate)) |
| 988 |
alias FunctionTypeOf = Fdlg; // HIT: delegate |
| 989 |
else |
| 990 |
static assert(0); |
| 991 |
} |
| 992 |
else |
| 993 |
static assert(0); |
| 994 |
} |
| 995 |
|
| 996 |
@safe unittest |
| 997 |
{ |
| 998 |
class C |
| 999 |
{ |
| 1000 |
int value() @property { return 0; } |
| 1001 |
} |
| 1002 |
static assert(is( typeof(C.value) == int )); |
| 1003 |
static assert(is( FunctionTypeOf!(C.value) == function )); |
| 1004 |
} |
| 1005 |
|
| 1006 |
@system unittest |
| 1007 |
{ |
| 1008 |
int test(int a); |
| 1009 |
int propGet() @property; |
| 1010 |
int propSet(int a) @property; |
| 1011 |
int function(int) test_fp; |
| 1012 |
int delegate(int) test_dg; |
| 1013 |
static assert(is( typeof(test) == FunctionTypeOf!(typeof(test)) )); |
| 1014 |
static assert(is( typeof(test) == FunctionTypeOf!test )); |
| 1015 |
static assert(is( typeof(test) == FunctionTypeOf!test_fp )); |
| 1016 |
static assert(is( typeof(test) == FunctionTypeOf!test_dg )); |
| 1017 |
alias int GetterType() @property; |
| 1018 |
alias int SetterType(int) @property; |
| 1019 |
static assert(is( FunctionTypeOf!propGet == GetterType )); |
| 1020 |
static assert(is( FunctionTypeOf!propSet == SetterType )); |
| 1021 |
|
| 1022 |
interface Prop { int prop() @property; } |
| 1023 |
Prop prop; |
| 1024 |
static assert(is( FunctionTypeOf!(Prop.prop) == GetterType )); |
| 1025 |
static assert(is( FunctionTypeOf!(prop.prop) == GetterType )); |
| 1026 |
|
| 1027 |
class Callable { int opCall(int) { return 0; } } |
| 1028 |
auto call = new Callable; |
| 1029 |
static assert(is( FunctionTypeOf!call == typeof(test) )); |
| 1030 |
|
| 1031 |
struct StaticCallable { static int opCall(int) { return 0; } } |
| 1032 |
StaticCallable stcall_val; |
| 1033 |
StaticCallable* stcall_ptr; |
| 1034 |
static assert(is( FunctionTypeOf!stcall_val == typeof(test) )); |
| 1035 |
static assert(is( FunctionTypeOf!stcall_ptr == typeof(test) )); |
| 1036 |
|
| 1037 |
interface Overloads |
| 1038 |
{ |
| 1039 |
void test(string); |
| 1040 |
real test(real); |
| 1041 |
int test(int); |
| 1042 |
int test() @property; |
| 1043 |
} |
| 1044 |
alias ov = __traits(getVirtualMethods, Overloads, "test"); |
| 1045 |
alias F_ov0 = FunctionTypeOf!(ov[0]); |
| 1046 |
alias F_ov1 = FunctionTypeOf!(ov[1]); |
| 1047 |
alias F_ov2 = FunctionTypeOf!(ov[2]); |
| 1048 |
alias F_ov3 = FunctionTypeOf!(ov[3]); |
| 1049 |
static assert(is(F_ov0* == void function(string))); |
| 1050 |
static assert(is(F_ov1* == real function(real))); |
| 1051 |
static assert(is(F_ov2* == int function(int))); |
| 1052 |
static assert(is(F_ov3* == int function() @property)); |
| 1053 |
|
| 1054 |
alias F_dglit = FunctionTypeOf!((int a){ return a; }); |
| 1055 |
static assert(is(F_dglit* : int function(int))); |
| 1056 |
} |
| 1057 |
|
| 1058 |
// std.traits.ReturnType |
| 1059 |
/* |
| 1060 |
Get the type of the return value from a function, |
| 1061 |
a pointer to function, a delegate, a struct |
| 1062 |
with an opCall, a pointer to a struct with an opCall, |
| 1063 |
or a class with an `opCall`. Please note that $(D_KEYWORD ref) |
| 1064 |
is not part of a type, but the attribute of the function |
| 1065 |
(see template $(LREF functionAttributes)). |
| 1066 |
*/ |
| 1067 |
template ReturnType(func...) |
| 1068 |
if (func.length == 1 /*&& isCallable!func*/) |
| 1069 |
{ |
| 1070 |
static if (is(FunctionTypeOf!func R == return)) |
| 1071 |
alias ReturnType = R; |
| 1072 |
else |
| 1073 |
static assert(0, "argument has no return type"); |
| 1074 |
} |
| 1075 |
|
| 1076 |
// |
| 1077 |
@safe unittest |
| 1078 |
{ |
| 1079 |
int foo(); |
| 1080 |
ReturnType!foo x; // x is declared as int |
| 1081 |
} |
| 1082 |
|
| 1083 |
@safe unittest |
| 1084 |
{ |
| 1085 |
struct G |
| 1086 |
{ |
| 1087 |
int opCall (int i) { return 1;} |
| 1088 |
} |
| 1089 |
|
| 1090 |
alias ShouldBeInt = ReturnType!G; |
| 1091 |
static assert(is(ShouldBeInt == int)); |
| 1092 |
|
| 1093 |
G g; |
| 1094 |
static assert(is(ReturnType!g == int)); |
| 1095 |
|
| 1096 |
G* p; |
| 1097 |
alias pg = ReturnType!p; |
| 1098 |
static assert(is(pg == int)); |
| 1099 |
|
| 1100 |
class C |
| 1101 |
{ |
| 1102 |
int opCall (int i) { return 1;} |
| 1103 |
} |
| 1104 |
|
| 1105 |
static assert(is(ReturnType!C == int)); |
| 1106 |
|
| 1107 |
C c; |
| 1108 |
static assert(is(ReturnType!c == int)); |
| 1109 |
|
| 1110 |
class Test |
| 1111 |
{ |
| 1112 |
int prop() @property { return 0; } |
| 1113 |
} |
| 1114 |
alias R_Test_prop = ReturnType!(Test.prop); |
| 1115 |
static assert(is(R_Test_prop == int)); |
| 1116 |
|
| 1117 |
alias R_dglit = ReturnType!((int a) { return a; }); |
| 1118 |
static assert(is(R_dglit == int)); |
| 1119 |
} |
| 1120 |
|
| 1121 |
// std.traits.Parameters |
| 1122 |
/* |
| 1123 |
Get, as a tuple, the types of the parameters to a function, a pointer |
| 1124 |
to function, a delegate, a struct with an `opCall`, a pointer to a |
| 1125 |
struct with an `opCall`, or a class with an `opCall`. |
| 1126 |
*/ |
| 1127 |
template Parameters(func...) |
| 1128 |
if (func.length == 1 /*&& isCallable!func*/) |
| 1129 |
{ |
| 1130 |
static if (is(FunctionTypeOf!func P == function)) |
| 1131 |
alias Parameters = P; |
| 1132 |
else |
| 1133 |
static assert(0, "argument has no parameters"); |
| 1134 |
} |
| 1135 |
|
| 1136 |
// |
| 1137 |
@safe unittest |
| 1138 |
{ |
| 1139 |
int foo(int, long); |
| 1140 |
void bar(Parameters!foo); // declares void bar(int, long); |
| 1141 |
void abc(Parameters!foo[1]); // declares void abc(long); |
| 1142 |
} |
| 1143 |
|
| 1144 |
@safe unittest |
| 1145 |
{ |
| 1146 |
int foo(int i, bool b) { return 0; } |
| 1147 |
static assert(is(Parameters!foo == AliasSeq!(int, bool))); |
| 1148 |
static assert(is(Parameters!(typeof(&foo)) == AliasSeq!(int, bool))); |
| 1149 |
|
| 1150 |
struct S { real opCall(real r, int i) { return 0.0; } } |
| 1151 |
S s; |
| 1152 |
static assert(is(Parameters!S == AliasSeq!(real, int))); |
| 1153 |
static assert(is(Parameters!(S*) == AliasSeq!(real, int))); |
| 1154 |
static assert(is(Parameters!s == AliasSeq!(real, int))); |
| 1155 |
|
| 1156 |
class Test |
| 1157 |
{ |
| 1158 |
int prop() @property { return 0; } |
| 1159 |
} |
| 1160 |
alias P_Test_prop = Parameters!(Test.prop); |
| 1161 |
static assert(P_Test_prop.length == 0); |
| 1162 |
|
| 1163 |
alias P_dglit = Parameters!((int a){}); |
| 1164 |
static assert(P_dglit.length == 1); |
| 1165 |
static assert(is(P_dglit[0] == int)); |
| 1166 |
} |
| 1167 |
|
| 1168 |
// Return `true` if `Type` has `member` that evaluates to `true` in a static if condition |
| 1169 |
enum isTrue(Type, string member) = __traits(compiles, { static if (__traits(getMember, Type, member)) {} else static assert(0); }); |
| 1170 |
|
| 1171 |
unittest |
| 1172 |
{ |
| 1173 |
static struct T |
| 1174 |
{ |
| 1175 |
enum a = true; |
| 1176 |
enum b = false; |
| 1177 |
enum c = 1; |
| 1178 |
enum d = 45; |
| 1179 |
enum e = "true"; |
| 1180 |
enum f = ""; |
| 1181 |
enum g = null; |
| 1182 |
alias h = bool; |
| 1183 |
} |
| 1184 |
|
| 1185 |
static assert( isTrue!(T, "a")); |
| 1186 |
static assert(!isTrue!(T, "b")); |
| 1187 |
static assert( isTrue!(T, "c")); |
| 1188 |
static assert( isTrue!(T, "d")); |
| 1189 |
static assert( isTrue!(T, "e")); |
| 1190 |
static assert( isTrue!(T, "f")); |
| 1191 |
static assert(!isTrue!(T, "g")); |
| 1192 |
static assert(!isTrue!(T, "h")); |
| 1193 |
} |
| 1194 |
|
| 1195 |
template hasUDA(alias symbol, alias attribute) |
| 1196 |
{ |
| 1197 |
enum isAttr(T) = is(T == attribute); |
| 1198 |
|
| 1199 |
enum hasUDA = anySatisfy!(isAttr, __traits(getAttributes, symbol)); |
| 1200 |
} |
| 1201 |
|
| 1202 |
unittest |
| 1203 |
{ |
| 1204 |
enum SomeUDA; |
| 1205 |
|
| 1206 |
struct Test |
| 1207 |
{ |
| 1208 |
int woUDA; |
| 1209 |
@SomeUDA int oneUDA; |
| 1210 |
@SomeUDA @SomeUDA int twoUDAs; |
| 1211 |
} |
| 1212 |
|
| 1213 |
static assert(hasUDA!(Test.oneUDA, SomeUDA)); |
| 1214 |
static assert(hasUDA!(Test.twoUDAs, SomeUDA)); |
| 1215 |
static assert(!hasUDA!(Test.woUDA, SomeUDA)); |
| 1216 |
} |