Files
Iain Buclaw 9d9663ea15 d: Merge upstream dmd, druntime 24a41073c2, phobos 24a41073c2.
D front-end changes:

	- Import dmd v2.112.0.
	- Bitfields feature is now enabled by default.
	- The compiler now accepts `-std=d2024' and `-std=d202y'.
	- An error is now issued for dangling `else' statements.
	- `finally' statements are no longer rewritten to a sequence if
	  no `Exception' was thrown.
	- Some forms of `printf' calls are now treated as `@safe'.
	- Implicit integer conversions in `int op= float` assignments
	  has been deprecated.

D runtime changes:

	- Import druntime v2.112.0.
	- Added `filterCaughtThrowable' in `core.thread.ThreadBase'.

Phobos changes:

	- Import phobos v2.112.0.

gcc/d/ChangeLog:

	* dmd/VERSION: Bump version to v2.112.0.
	* dmd/MERGE: Merge upstream dmd 24a41073c2.
	* d-attribs.cc (build_attributes): Update for new front-end interface.
	* d-builtins.cc (build_frontend_type): Likewise.
	(matches_builtin_type): Likewise.
	(d_init_versions): Predefine D_Profile when compiling with profile
	enabled.
	* d-codegen.cc (get_array_length): Update for new front-end interface.
	(lower_struct_comparison): Likewise.
	(build_array_from_val): Likewise.
	(get_function_type): Likewise.
	(get_frameinfo): Likewise.
	* d-compiler.cc (Compiler::paintAsType): Likewise.
	* d-convert.cc (convert_expr): Likewise.
	(convert_for_rvalue): Likewise.
	(convert_for_assignment): Likewise.
	(d_array_convert): Likewise.
	* d-diagnostic.cc (verrorReport): Rename to ...
	(vreportDiagnostic): ... this.
	(verrorReportSupplemental): Rename to ...
	(vsupplementalDiagnostic): ... this.
	* d-lang.cc (d_handle_option): Handle -std=d2024 and -std=d202y.
	(d_parse_file): Update for new front-end interface.
	* d-target.cc (Target::fieldalign): Likewise.
	(Target::isVectorTypeSupported): Likewise.
	(Target::isVectorOpSupported): Likewise.
	* decl.cc (get_symbol_decl): Likewise.
	(DeclVisitor::visit): Likewise.
	(DeclVisitor::visit (FuncDeclaration *)): Do NRVO on `__result' decl.
	* expr.cc (needs_postblit): Remove.
	(needs_dtor): Remove.
	(lvalue_p): Remove.
	(ExprVisitor::visit): Update for new front-end interface.
	(ExprVisitor::visit (AssignExp *)): Update for front-end lowering
	expression using templates.
	* imports.cc (ImportVisitor::visit): Update for new front-end
	interface.
	* intrinsics.def (INTRINSIC_VA_ARG): Update signature.
	(INTRINSIC_C_VA_ARG): Update signature.
	(INTRINSIC_VASTART): Update signature.
	* lang.opt: Add -std=d2024 and -std=d202y.
	* toir.cc (IRVisitor::visit): Update for new front-end interface.
	* typeinfo.cc (TypeInfoVisitor::visit): Likewise.
	(TypeInfoVisitor::visit (TypeInfoStructDeclaration *)): Ensure
	semantic is ran on all TypeInfo members.
	(base_vtable_offset): Update for new front-end interface.
	* types.cc (TypeVisitor::visit): Likewise.

libphobos/ChangeLog:

	* libdruntime/MERGE: Merge upstream druntime 24a41073c2.
	* libdruntime/__importc_builtins.di: Reimplement.
	* src/MERGE: Merge upstream phobos 808314eb2.
	* testsuite/libphobos.aa/test_aa.d: Adjust test.
	* testsuite/libphobos.gc/forkgc2.d: Removed.
	* testsuite/libphobos.thread/filterthrownglobal.d: New test.
	* testsuite/libphobos.thread/filterthrownmethod.d: New test.

gcc/testsuite/ChangeLog:

	* gdc.dg/pr90601.d: Adjust test.
	* lib/gdc-utils.exp: Handle new compiler options.
2026-02-03 23:09:53 +01:00

246 lines
4.9 KiB
D

/**
* Array container for internal usage.
*
* Copyright: Copyright Martin Nowak 2013.
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Martin Nowak
*/
module core.internal.container.array;
static import common = core.internal.container.common;
import core.exception : onOutOfMemoryError;
struct Array(T)
{
nothrow:
@disable this(this);
~this()
{
reset();
}
void reset()
{
length = 0;
}
@property size_t length() const
{
return _length;
}
@property void length(size_t nlength)
{
import core.checkedint : mulu;
bool overflow = false;
size_t reqsize = mulu(T.sizeof, nlength, overflow);
if (!overflow)
{
if (nlength < _length)
foreach (ref val; _ptr[nlength .. _length]) common.destroy(val);
_ptr = cast(T*)common.xrealloc(_ptr, reqsize);
if (nlength > _length)
foreach (ref val; _ptr[_length .. nlength]) common.initialize(val);
_length = nlength;
}
else
onOutOfMemoryError();
}
@property bool empty() const
{
return !length;
}
@property ref inout(T) front() inout
in { assert(!empty); }
do
{
return _ptr[0];
}
@property ref inout(T) back() inout
in { assert(!empty); }
do
{
return _ptr[_length - 1];
}
ref inout(T) opIndex(size_t idx) inout
in { assert(idx < length); }
do
{
return _ptr[idx];
}
inout(T)[] opSlice() inout
{
return _ptr[0 .. _length];
}
inout(T)[] opSlice(size_t a, size_t b) inout
in { assert(a < b && b <= length); }
do
{
return _ptr[a .. b];
}
alias length opDollar;
void insertBack()(auto ref T val)
{
import core.checkedint : addu;
bool overflow = false;
size_t newlength = addu(length, 1, overflow);
if (!overflow)
{
length = newlength;
back = val;
}
else
onOutOfMemoryError();
}
void popBack()
{
length = length - 1;
}
void remove(size_t idx)
in { assert(idx < length); }
do
{
foreach (i; idx .. length - 1)
_ptr[i] = _ptr[i+1];
popBack();
}
void swap(ref Array other)
{
auto ptr = _ptr;
_ptr = other._ptr;
other._ptr = ptr;
immutable len = _length;
_length = other._length;
other._length = len;
}
invariant
{
assert(!_ptr == !_length);
}
private:
T* _ptr;
size_t _length;
}
unittest
{
Array!size_t ary;
assert(ary[] == []);
ary.insertBack(5);
assert(ary[] == [5]);
assert(ary[$-1] == 5);
ary.popBack();
assert(ary[] == []);
ary.insertBack(0);
ary.insertBack(1);
assert(ary[] == [0, 1]);
assert(ary[0 .. 1] == [0]);
assert(ary[1 .. 2] == [1]);
assert(ary[$ - 2 .. $] == [0, 1]);
size_t idx;
foreach (val; ary) assert(idx++ == val);
foreach_reverse (val; ary) assert(--idx == val);
foreach (i, val; ary) assert(i == val);
foreach_reverse (i, val; ary) assert(i == val);
ary.insertBack(2);
ary.remove(1);
assert(ary[] == [0, 2]);
assert(!ary.empty);
ary.reset();
assert(ary.empty);
ary.insertBack(0);
assert(!ary.empty);
destroy(ary);
assert(ary.empty);
// not copyable
static assert(!__traits(compiles, { Array!size_t ary2 = ary; }));
Array!size_t ary2;
static assert(!__traits(compiles, ary = ary2));
static void foo(Array!size_t copy) {}
static assert(!__traits(compiles, foo(ary)));
ary2.insertBack(0);
assert(ary.empty);
assert(ary2[] == [0]);
ary.swap(ary2);
assert(ary[] == [0]);
assert(ary2.empty);
}
unittest
{
alias RC = common.RC!();
Array!RC ary;
size_t cnt;
assert(cnt == 0);
ary.insertBack(RC(&cnt));
assert(cnt == 1);
ary.insertBack(RC(&cnt));
assert(cnt == 2);
ary.back = ary.front;
assert(cnt == 2);
ary.popBack();
assert(cnt == 1);
ary.popBack();
assert(cnt == 0);
}
unittest
{
import core.exception;
try
{
// Overflow ary.length.
auto ary = Array!size_t(cast(size_t*)0xdeadbeef, -1);
scope(failure)
{
// prevent destructor from cleaning up invalid memory.
ary._length = 0;
ary._ptr = null;
}
ary.insertBack(0);
}
catch (OutOfMemoryError)
{
}
try
{
// Overflow requested memory size for common.xrealloc().
auto ary = Array!size_t(cast(size_t*)0xdeadbeef, -2);
scope(failure)
{
// prevent destructor from cleaning up invalid memory.
ary._length = 0;
ary._ptr = null;
}
ary.insertBack(0);
}
catch (OutOfMemoryError)
{
}
}