| 1 |
/** |
| 2 |
* D header file for interaction with Microsoft C++ <xutility> |
| 3 |
* |
| 4 |
* Copyright: Copyright (c) 2018 D Language Foundation |
| 5 |
* License: Distributed under the |
| 6 |
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). |
| 7 |
* (See accompanying file LICENSE) |
| 8 |
* Authors: Manu Evans |
| 9 |
* Source: $(DRUNTIMESRC core/stdcpp/xutility.d) |
| 10 |
*/ |
| 11 |
|
| 12 |
module core.stdcpp.xutility; |
| 13 |
|
| 14 |
@nogc: |
| 15 |
|
| 16 |
version (CppRuntime_LLVM) |
| 17 |
{ |
| 18 |
import core.internal.traits : AliasSeq; |
| 19 |
enum StdNamespace = AliasSeq!("std", "__1"); |
| 20 |
} |
| 21 |
else |
| 22 |
{ |
| 23 |
enum StdNamespace = "std"; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Possible values of the `__cplusplus` macro provided by C++ compilers |
| 28 |
* |
| 29 |
* For foolproofness, use ordering comparison, e.g. `__cplusplus >= CppStdRevision.cpp17`. |
| 30 |
*/ |
| 31 |
enum CppStdRevision : uint |
| 32 |
{ |
| 33 |
cpp98 = 199711, |
| 34 |
cpp11 = 201103, |
| 35 |
cpp14 = 201402, |
| 36 |
cpp17 = 201703, |
| 37 |
cpp20 = 202002, |
| 38 |
cpp23 = 202302, |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns the target C++ version, encoded as C++ compilers do |
| 43 |
* |
| 44 |
* C++ compilers provide a `__cplusplus` macro which returns an integer |
| 45 |
* representing the targetted standard. This manifest provides the same |
| 46 |
* interface, retrieved from the compiler via a `__traits`. |
| 47 |
*/ |
| 48 |
enum __cplusplus = __traits(getTargetInfo, "cppStd"); |
| 49 |
|
| 50 |
// wrangle C++ features |
| 51 |
enum __cpp_sized_deallocation = __cplusplus >= CppStdRevision.cpp14 || is(typeof(_MSC_VER)) ? 201309 : 0; |
| 52 |
enum __cpp_aligned_new = __cplusplus >= CppStdRevision.cpp17 ? 201606 : 0; |
| 53 |
|
| 54 |
|
| 55 |
version (CppRuntime_Microsoft) |
| 56 |
{ |
| 57 |
import core.stdcpp.type_traits : is_empty; |
| 58 |
|
| 59 |
version (_MSC_VER_1200) |
| 60 |
enum _MSC_VER = 1200; |
| 61 |
else version (_MSC_VER_1300) |
| 62 |
enum _MSC_VER = 1300; |
| 63 |
else version (_MSC_VER_1310) |
| 64 |
enum _MSC_VER = 1310; |
| 65 |
else version (_MSC_VER_1400) |
| 66 |
enum _MSC_VER = 1400; |
| 67 |
else version (_MSC_VER_1500) |
| 68 |
enum _MSC_VER = 1500; |
| 69 |
else version (_MSC_VER_1600) |
| 70 |
enum _MSC_VER = 1600; |
| 71 |
else version (_MSC_VER_1700) |
| 72 |
enum _MSC_VER = 1700; |
| 73 |
else version (_MSC_VER_1800) |
| 74 |
enum _MSC_VER = 1800; |
| 75 |
else version (_MSC_VER_1900) |
| 76 |
enum _MSC_VER = 1900; |
| 77 |
else version (_MSC_VER_1910) |
| 78 |
enum _MSC_VER = 1910; |
| 79 |
else version (_MSC_VER_1911) |
| 80 |
enum _MSC_VER = 1911; |
| 81 |
else version (_MSC_VER_1912) |
| 82 |
enum _MSC_VER = 1912; |
| 83 |
else version (_MSC_VER_1913) |
| 84 |
enum _MSC_VER = 1913; |
| 85 |
else version (_MSC_VER_1914) |
| 86 |
enum _MSC_VER = 1914; |
| 87 |
else version (_MSC_VER_1915) |
| 88 |
enum _MSC_VER = 1915; |
| 89 |
else version (_MSC_VER_1916) |
| 90 |
enum _MSC_VER = 1916; |
| 91 |
else version (_MSC_VER_1920) |
| 92 |
enum _MSC_VER = 1920; |
| 93 |
else version (_MSC_VER_1921) |
| 94 |
enum _MSC_VER = 1921; |
| 95 |
else version (_MSC_VER_1922) |
| 96 |
enum _MSC_VER = 1922; |
| 97 |
else version (_MSC_VER_1923) |
| 98 |
enum _MSC_VER = 1923; |
| 99 |
else |
| 100 |
enum _MSC_VER = 1923; // assume most recent compiler version |
| 101 |
|
| 102 |
// Client code can mixin the set of MSVC linker directives |
| 103 |
mixin template MSVCLinkDirectives(bool failMismatch = false) |
| 104 |
{ |
| 105 |
import core.stdcpp.xutility : __CXXLIB__, _ITERATOR_DEBUG_LEVEL; |
| 106 |
|
| 107 |
static if (__CXXLIB__ == "libcmtd") |
| 108 |
{ |
| 109 |
pragma(lib, "libcpmtd"); |
| 110 |
static if (failMismatch) |
| 111 |
pragma(linkerDirective, "/FAILIFMISMATCH:RuntimeLibrary=MTd_StaticDebug"); |
| 112 |
} |
| 113 |
else static if (__CXXLIB__ == "msvcrtd") |
| 114 |
{ |
| 115 |
pragma(lib, "msvcprtd"); |
| 116 |
static if (failMismatch) |
| 117 |
pragma(linkerDirective, "/FAILIFMISMATCH:RuntimeLibrary=MDd_DynamicDebug"); |
| 118 |
} |
| 119 |
else static if (__CXXLIB__ == "libcmt") |
| 120 |
{ |
| 121 |
pragma(lib, "libcpmt"); |
| 122 |
static if (failMismatch) |
| 123 |
pragma(linkerDirective, "/FAILIFMISMATCH:RuntimeLibrary=MT_StaticRelease"); |
| 124 |
} |
| 125 |
else static if (__CXXLIB__ == "msvcrt") |
| 126 |
{ |
| 127 |
pragma(lib, "msvcprt"); |
| 128 |
static if (failMismatch) |
| 129 |
pragma(linkerDirective, "/FAILIFMISMATCH:RuntimeLibrary=MD_DynamicRelease"); |
| 130 |
} |
| 131 |
static if (failMismatch) |
| 132 |
pragma(linkerDirective, "/FAILIFMISMATCH:_ITERATOR_DEBUG_LEVEL=" ~ ('0' + _ITERATOR_DEBUG_LEVEL)); |
| 133 |
} |
| 134 |
|
| 135 |
// HACK: should we guess _DEBUG for `debug` builds? |
| 136 |
version (NDEBUG) {} |
| 137 |
else debug version = _DEBUG; |
| 138 |
|
| 139 |
// By specific user request |
| 140 |
version (_ITERATOR_DEBUG_LEVEL_0) |
| 141 |
enum _ITERATOR_DEBUG_LEVEL = 0; |
| 142 |
else version (_ITERATOR_DEBUG_LEVEL_1) |
| 143 |
enum _ITERATOR_DEBUG_LEVEL = 1; |
| 144 |
else version (_ITERATOR_DEBUG_LEVEL_2) |
| 145 |
enum _ITERATOR_DEBUG_LEVEL = 2; |
| 146 |
else |
| 147 |
{ |
| 148 |
// Match the C Runtime |
| 149 |
static if (__CXXLIB__ == "libcmtd" || __CXXLIB__ == "msvcrtd") |
| 150 |
enum _ITERATOR_DEBUG_LEVEL = 2; |
| 151 |
else static if (__CXXLIB__ == "libcmt" || __CXXLIB__ == "msvcrt" || |
| 152 |
__CXXLIB__ == "msvcrt100" || __CXXLIB__ == "msvcrt110" || __CXXLIB__ == "msvcrt120") |
| 153 |
enum _ITERATOR_DEBUG_LEVEL = 0; |
| 154 |
else |
| 155 |
{ |
| 156 |
static if (__CXXLIB__.length > 0) |
| 157 |
pragma(msg, "Unrecognised C++ runtime library '" ~ __CXXLIB__ ~ "'"); |
| 158 |
|
| 159 |
// No runtime specified; as a best-guess, -release will produce code that matches the MSVC release CRT |
| 160 |
version (_DEBUG) |
| 161 |
enum _ITERATOR_DEBUG_LEVEL = 2; |
| 162 |
else |
| 163 |
enum _ITERATOR_DEBUG_LEVEL = 0; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// convenient alias for the C++ std library name |
| 168 |
enum __CXXLIB__ = __traits(getTargetInfo, "cppRuntimeLibrary"); |
| 169 |
|
| 170 |
extern(C++, "std"): |
| 171 |
package: |
| 172 |
enum _LOCK_DEBUG = 3; |
| 173 |
|
| 174 |
extern(C++, class) struct _Lockit |
| 175 |
{ |
| 176 |
this(int) nothrow @nogc @safe; |
| 177 |
~this() nothrow @nogc @safe; |
| 178 |
|
| 179 |
private: |
| 180 |
int _Locktype; |
| 181 |
} |
| 182 |
void dummyDtor() { assert(false); } |
| 183 |
pragma(linkerDirective, "/ALTERNATENAME:" ~ _Lockit.__dtor.mangleof ~ "=" ~ dummyDtor.mangleof); |
| 184 |
|
| 185 |
struct _Container_base0 |
| 186 |
{ |
| 187 |
extern(D): |
| 188 |
void _Orphan_all()() nothrow @nogc @safe {} |
| 189 |
void _Swap_all()(ref _Container_base0) nothrow @nogc @safe {} |
| 190 |
void _Swap_proxy_and_iterators()(ref _Container_base0) nothrow {} |
| 191 |
} |
| 192 |
struct _Iterator_base0 |
| 193 |
{ |
| 194 |
extern(D): |
| 195 |
void _Adopt()(const(void)*) nothrow @nogc @safe {} |
| 196 |
const(_Container_base0)* _Getcont()() const nothrow @nogc @safe { return null; } |
| 197 |
|
| 198 |
enum bool _Unwrap_when_unverified = true; |
| 199 |
} |
| 200 |
|
| 201 |
struct _Container_proxy |
| 202 |
{ |
| 203 |
const(_Container_base12)* _Mycont; |
| 204 |
_Iterator_base12* _Myfirstiter; |
| 205 |
} |
| 206 |
|
| 207 |
struct _Container_base12 |
| 208 |
{ |
| 209 |
extern(D): |
| 210 |
inout(_Iterator_base12*)*_Getpfirst()() inout nothrow @nogc @safe |
| 211 |
{ |
| 212 |
return _Myproxy == null ? null : &_Myproxy._Myfirstiter; |
| 213 |
} |
| 214 |
void _Orphan_all()() nothrow @nogc @safe |
| 215 |
{ |
| 216 |
static if (_ITERATOR_DEBUG_LEVEL == 2) |
| 217 |
{ |
| 218 |
if (_Myproxy != null) |
| 219 |
{ |
| 220 |
auto _Lock = _Lockit(_LOCK_DEBUG); |
| 221 |
for (_Iterator_base12 **_Pnext = &_Myproxy._Myfirstiter; *_Pnext != null; *_Pnext = (*_Pnext)._Mynextiter) |
| 222 |
(*_Pnext)._Myproxy = null; |
| 223 |
_Myproxy._Myfirstiter = null; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
// void _Swap_all()(ref _Container_base12) nothrow @nogc; |
| 228 |
|
| 229 |
void _Swap_proxy_and_iterators()(ref _Container_base12 _Right) nothrow |
| 230 |
{ |
| 231 |
static if (_ITERATOR_DEBUG_LEVEL == 2) |
| 232 |
auto _Lock = _Lockit(_LOCK_DEBUG); |
| 233 |
|
| 234 |
_Container_proxy* _Temp = _Myproxy; |
| 235 |
_Myproxy = _Right._Myproxy; |
| 236 |
_Right._Myproxy = _Temp; |
| 237 |
|
| 238 |
if (_Myproxy) |
| 239 |
_Myproxy._Mycont = &this; |
| 240 |
|
| 241 |
if (_Right._Myproxy) |
| 242 |
_Right._Myproxy._Mycont = &_Right; |
| 243 |
} |
| 244 |
|
| 245 |
_Container_proxy* _Myproxy; |
| 246 |
} |
| 247 |
|
| 248 |
struct _Iterator_base12 |
| 249 |
{ |
| 250 |
extern(D): |
| 251 |
void _Adopt()(_Container_base12 *_Parent) nothrow @nogc @safe |
| 252 |
{ |
| 253 |
if (_Parent == null) |
| 254 |
{ |
| 255 |
static if (_ITERATOR_DEBUG_LEVEL == 2) |
| 256 |
{ |
| 257 |
auto _Lock = _Lockit(_LOCK_DEBUG); |
| 258 |
_Orphan_me(); |
| 259 |
} |
| 260 |
} |
| 261 |
else |
| 262 |
{ |
| 263 |
_Container_proxy *_Parent_proxy = _Parent._Myproxy; |
| 264 |
|
| 265 |
static if (_ITERATOR_DEBUG_LEVEL == 2) |
| 266 |
{ |
| 267 |
if (_Myproxy != _Parent_proxy) |
| 268 |
{ |
| 269 |
auto _Lock = _Lockit(_LOCK_DEBUG); |
| 270 |
_Orphan_me(); |
| 271 |
_Mynextiter = _Parent_proxy._Myfirstiter; |
| 272 |
_Parent_proxy._Myfirstiter = &this; |
| 273 |
_Myproxy = _Parent_proxy; |
| 274 |
} |
| 275 |
} |
| 276 |
else |
| 277 |
_Myproxy = _Parent_proxy; |
| 278 |
} |
| 279 |
} |
| 280 |
void _Clrcont()() nothrow @nogc @safe |
| 281 |
{ |
| 282 |
_Myproxy = null; |
| 283 |
} |
| 284 |
const(_Container_base12)* _Getcont()() const nothrow @nogc @safe |
| 285 |
{ |
| 286 |
return _Myproxy == null ? null : _Myproxy._Mycont; |
| 287 |
} |
| 288 |
inout(_Iterator_base12*)*_Getpnext()() inout nothrow @nogc @safe |
| 289 |
{ |
| 290 |
return &_Mynextiter; |
| 291 |
} |
| 292 |
void _Orphan_me()() nothrow @nogc @safe |
| 293 |
{ |
| 294 |
static if (_ITERATOR_DEBUG_LEVEL == 2) |
| 295 |
{ |
| 296 |
if (_Myproxy != null) |
| 297 |
{ |
| 298 |
_Iterator_base12 **_Pnext = &_Myproxy._Myfirstiter; |
| 299 |
while (*_Pnext != null && *_Pnext != &this) |
| 300 |
_Pnext = &(*_Pnext)._Mynextiter; |
| 301 |
assert(*_Pnext, "ITERATOR LIST CORRUPTED!"); |
| 302 |
*_Pnext = _Mynextiter; |
| 303 |
_Myproxy = null; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
enum bool _Unwrap_when_unverified = _ITERATOR_DEBUG_LEVEL == 0; |
| 309 |
|
| 310 |
_Container_proxy *_Myproxy; |
| 311 |
_Iterator_base12 *_Mynextiter; |
| 312 |
} |
| 313 |
|
| 314 |
static if (_ITERATOR_DEBUG_LEVEL == 0) |
| 315 |
{ |
| 316 |
alias _Container_base = _Container_base0; |
| 317 |
alias _Iterator_base = _Iterator_base0; |
| 318 |
} |
| 319 |
else |
| 320 |
{ |
| 321 |
alias _Container_base = _Container_base12; |
| 322 |
alias _Iterator_base = _Iterator_base12; |
| 323 |
} |
| 324 |
|
| 325 |
extern (C++, class) struct _Compressed_pair(_Ty1, _Ty2, bool Ty1Empty = is_empty!_Ty1.value) |
| 326 |
{ |
| 327 |
pragma (inline, true): |
| 328 |
extern(D): |
| 329 |
pure nothrow @nogc: |
| 330 |
enum _HasFirst = !Ty1Empty; |
| 331 |
|
| 332 |
ref inout(_Ty1) first() inout @safe { return _Myval1; } |
| 333 |
ref inout(_Ty2) second() inout @safe { return _Myval2; } |
| 334 |
|
| 335 |
static if (!Ty1Empty) |
| 336 |
_Ty1 _Myval1; |
| 337 |
else |
| 338 |
{ |
| 339 |
@property ref inout(_Ty1) _Myval1() inout @trusted { return *_GetBase(); } |
| 340 |
private inout(_Ty1)* _GetBase() inout @trusted { return cast(inout(_Ty1)*)&this; } |
| 341 |
} |
| 342 |
_Ty2 _Myval2; |
| 343 |
} |
| 344 |
|
| 345 |
// these are all [[noreturn]] |
| 346 |
void _Xbad_alloc() nothrow; |
| 347 |
void _Xinvalid_argument(const(char)* message) nothrow; |
| 348 |
void _Xlength_error(const(char)* message) nothrow; |
| 349 |
void _Xout_of_range(const(char)* message) nothrow; |
| 350 |
void _Xoverflow_error(const(char)* message) nothrow; |
| 351 |
void _Xruntime_error(const(char)* message) nothrow; |
| 352 |
} |
| 353 |
else version (CppRuntime_LLVM) |
| 354 |
{ |
| 355 |
import core.stdcpp.type_traits : is_empty; |
| 356 |
|
| 357 |
extern(C++, "std"): |
| 358 |
|
| 359 |
extern (C++, class) struct __compressed_pair(_T1, _T2) |
| 360 |
{ |
| 361 |
pragma (inline, true): |
| 362 |
extern(D): |
| 363 |
enum Ty1Empty = is_empty!_T1.value; |
| 364 |
enum Ty2Empty = is_empty!_T2.value; |
| 365 |
|
| 366 |
ref inout(_T1) first() inout nothrow @safe @nogc { return __value1_; } |
| 367 |
ref inout(_T2) second() inout nothrow @safe @nogc { return __value2_; } |
| 368 |
|
| 369 |
private: |
| 370 |
private inout(_T1)* __get_base1() inout { return cast(inout(_T1)*)&this; } |
| 371 |
private inout(_T2)* __get_base2() inout { return cast(inout(_T2)*)&__get_base1()[Ty1Empty ? 0 : 1]; } |
| 372 |
|
| 373 |
static if (!Ty1Empty) |
| 374 |
_T1 __value1_; |
| 375 |
else |
| 376 |
@property ref inout(_T1) __value1_() inout nothrow @trusted @nogc { return *__get_base1(); } |
| 377 |
static if (!Ty2Empty) |
| 378 |
_T2 __value2_; |
| 379 |
else |
| 380 |
@property ref inout(_T2) __value2_() inout nothrow @trusted @nogc { return *__get_base2(); } |
| 381 |
} |
| 382 |
} |
| 383 |
version (CppRuntime_GNU) |
| 384 |
{ |
| 385 |
import core.atomic; |
| 386 |
|
| 387 |
alias _Atomic_word = int; |
| 388 |
|
| 389 |
void __atomic_add_dispatch()(_Atomic_word* __mem, int __val) nothrow @nogc @safe |
| 390 |
{ |
| 391 |
version (__GTHREADS) |
| 392 |
{ |
| 393 |
// TODO: check __gthread_active_p() |
| 394 |
// if (__gthread_active_p()) |
| 395 |
__atomic_add(__mem, __val); |
| 396 |
// } |
| 397 |
// else |
| 398 |
// __atomic_add_single(__mem, __val); |
| 399 |
} |
| 400 |
else |
| 401 |
__atomic_add_single(__mem, __val); |
| 402 |
} |
| 403 |
|
| 404 |
void __atomic_add()(_Atomic_word* __mem, int __val) nothrow @nogc @safe |
| 405 |
{ |
| 406 |
atomicFetchAdd!(MemoryOrder.acq_rel)(*__mem, __val); |
| 407 |
} |
| 408 |
|
| 409 |
void __atomic_add_single()(_Atomic_word* __mem, int __val) nothrow @nogc @safe |
| 410 |
{ |
| 411 |
*__mem += __val; |
| 412 |
} |
| 413 |
|
| 414 |
_Atomic_word __exchange_and_add_dispatch()(_Atomic_word* __mem, int __val) nothrow @nogc @safe |
| 415 |
{ |
| 416 |
version (__GTHREADS) |
| 417 |
{ |
| 418 |
// TODO: check __gthread_active_p() |
| 419 |
return __exchange_and_add(__mem, __val); |
| 420 |
|
| 421 |
// if (__gthread_active_p()) |
| 422 |
// return __exchange_and_add(__mem, __val); |
| 423 |
// else |
| 424 |
// return __exchange_and_add_single(__mem, __val); |
| 425 |
} |
| 426 |
else |
| 427 |
return __exchange_and_add_single(__mem, __val); |
| 428 |
} |
| 429 |
|
| 430 |
_Atomic_word __exchange_and_add()(_Atomic_word* __mem, int __val) nothrow @nogc @safe |
| 431 |
{ |
| 432 |
return atomicFetchAdd!(MemoryOrder.acq_rel)(*__mem, __val); |
| 433 |
} |
| 434 |
|
| 435 |
_Atomic_word __exchange_and_add_single()(_Atomic_word* __mem, int __val) nothrow @nogc @safe |
| 436 |
{ |
| 437 |
_Atomic_word __result = *__mem; |
| 438 |
*__mem += __val; |
| 439 |
return __result; |
| 440 |
} |
| 441 |
} |