Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mak/COPY
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ COPY=\
$(IMPDIR)\core\internal\dassert.d \
$(IMPDIR)\core\internal\entrypoint.d \
$(IMPDIR)\core\internal\hash.d \
$(IMPDIR)\core\internal\moving.d \
$(IMPDIR)\core\internal\parseoptions.d \
$(IMPDIR)\core\internal\spinlock.d \
$(IMPDIR)\core\internal\string.d \
Expand Down
1 change: 1 addition & 0 deletions mak/DOCS
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ DOCS=\
$(DOCDIR)\core_sys_darwin_netinet_in_.html \
\
$(DOCDIR)\core_internal_dassert.html \
$(DOCDIR)\core_internal_moving.html \
$(DOCDIR)\core_internal_switch_.html \
\
$(DOCDIR)\core_internal_array_appending.html \
Expand Down
1 change: 1 addition & 0 deletions mak/SRCS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SRCS=\
src\core\internal\dassert.d \
src\core\internal\entrypoint.d \
src\core\internal\hash.d \
src\core\internal\moving.d \
src\core\internal\parseoptions.d \
src\core\internal\spinlock.d \
src\core\internal\string.d \
Expand Down
3 changes: 3 additions & 0 deletions mak/WINDOWS
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ $(IMPDIR)\core\internal\entrypoint.d : src\core\internal\entrypoint.d
$(IMPDIR)\core\internal\hash.d : src\core\internal\hash.d
copy $** $@

$(IMPDIR)\core\internal\moving.d : src\core\internal\moving.d
copy $** $@

$(IMPDIR)\core\internal\parseoptions.d : src\core\internal\parseoptions.d
copy $** $@

Expand Down
3 changes: 3 additions & 0 deletions posix.mak
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ $(DOCDIR)/core_sys_darwin_netinet_%.html : src/core/sys/darwin/netinet/%.d $(DMD
$(DOCDIR)/core_internal_dassert.html : src/core/internal/dassert.d $(DMD)
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<

$(DOCDIR)/core_internal_moving.html : src/core/internal/moving.d $(DMD)
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<

$(DOCDIR)/core_internal_switch_.html : src/core/internal/switch_.d $(DMD)
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<

Expand Down
89 changes: 89 additions & 0 deletions src/core/internal/moving.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
This module contains the implementation of move semantics of DIP 1014

Copyright: Copyright Digital Mars 2000 - 2019.
License: Distributed under the
$(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
(See accompanying file LICENSE)
Source: $(DRUNTIMESRC core/_internal/_moving.d)
*/
module core.internal.moving;

/**
Recursively calls the `opPostMove` callbacks of a struct and its members if
they're defined.

When moving a struct instance, the compiler emits a call to this function
after blitting the instance and before releasing the original instance's
memory.

Params:
newLocation = reference to struct instance being moved into
oldLocation = reference to the original instance

Note:
This function is tentatively defined as `nothrow` to prevent
`opPostMove` from being defined without `nothrow`, which would allow
for possibly confusing changes in program flow.
*/
void __move_post_blt(S)(ref S newLocation, ref S oldLocation) nothrow
if (is(S == struct))
{
static foreach (memberName; __traits(allMembers, S))
{
static if (is(typeof(__traits(getMember, S, memberName)) == struct))
{
__move_post_blt(__traits(getMember, newLocation, memberName), __traits(getMember, oldLocation, memberName));
}
}

static if (__traits(hasMember, S, "opPostMove"))
{
import core.internal.traits : lvalueOf, rvalueOf;
static assert( is(typeof(S.init.opPostMove(lvalueOf!S))) &&
!is(typeof(S.init.opPostMove(rvalueOf!S))),
"`" ~ S.stringof ~ ".opPostMove` must take exactly one argument of type `" ~ S.stringof ~ "` by reference");

newLocation.opPostMove(oldLocation);
}
}

@safe nothrow unittest
{
struct A
{
bool movedInto;
void opPostMove(const ref A oldLocation)
{
movedInto = true;
}
}
A src, dest;
__move_post_blt(dest, src);
assert(dest.movedInto);
}

@safe nothrow unittest
{
struct A
{
bool movedInto;
void opPostMove(const ref A oldLocation)
{
movedInto = true;
}
}
struct B
{
A a;

bool movedInto;
void opPostMove(const ref B oldLocation)
{
movedInto = true;
}
}
B src, dest;
__move_post_blt(dest, src);
assert(dest.movedInto && dest.a.movedInto);
}
82 changes: 3 additions & 79 deletions src/object.d
Original file line number Diff line number Diff line change
Expand Up @@ -45,85 +45,6 @@ public import core.internal.array.capacity: reserve;
/// See $(REF assumeSafeAppend, core,internal,array,capacity)
public import core.internal.array.capacity: assumeSafeAppend;

/**
* Recursively calls the `opPostMove` callbacks of a struct and its members if
* they're defined.
*
* When moving a struct instance, the compiler emits a call to this function
* after blitting the instance and before releasing the original instance's
* memory.
*
* Params:
* newLocation = reference to struct instance being moved into
* oldLocation = reference to the original instance
*
* Note:
* This function is tentatively defined as `nothrow` to prevent
* `opPostMove` from being defined without `nothrow`, which would allow
* for possibly confusing changes in program flow.
*/
void __move_post_blt(S)(ref S newLocation, ref S oldLocation) nothrow
if (is(S == struct))
{
static foreach (memberName; __traits(allMembers, S))
{
static if (is(typeof(__traits(getMember, S, memberName)) == struct))
{
__move_post_blt(__traits(getMember, newLocation, memberName), __traits(getMember, oldLocation, memberName));
}
}

static if (__traits(hasMember, S, "opPostMove"))
{
import core.internal.traits : lvalueOf, rvalueOf;
static assert( is(typeof(S.init.opPostMove(lvalueOf!S))) &&
!is(typeof(S.init.opPostMove(rvalueOf!S))),
"`" ~ S.stringof ~ ".opPostMove` must take exactly one argument of type `" ~ S.stringof ~ "` by reference");

newLocation.opPostMove(oldLocation);
}
}

@safe nothrow unittest
{
struct A
{
bool movedInto;
void opPostMove(const ref A oldLocation)
{
movedInto = true;
}
}
A src, dest;
__move_post_blt(dest, src);
assert(dest.movedInto);
}

@safe nothrow unittest
{
struct A
{
bool movedInto;
void opPostMove(const ref A oldLocation)
{
movedInto = true;
}
}
struct B
{
A a;

bool movedInto;
void opPostMove(const ref B oldLocation)
{
movedInto = true;
}
}
B src, dest;
__move_post_blt(dest, src);
assert(dest.movedInto && dest.a.movedInto);
}

/**
Destroys the given object and optionally resets to initial state. It's used to
_destroy an object, calling its destructor or finalizer so it no longer
Expand Down Expand Up @@ -4101,6 +4022,9 @@ public import core.internal.array.capacity: _d_arraysetlengthTImpl;
/// See $(REF _d_assert_fail, core,internal,dassert)
public import core.internal.dassert: _d_assert_fail;

/// See $(REF __move_post_blt, core,internal,moving)
public import core.internal.moving: __move_post_blt;

/// See $(REF __switch, core,internal,switch_)
public import core.internal.switch_: __switch;
/// See $(REF __switch_error, core,internal,switch_)
Expand Down