This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Repository navigation
Translate _d_array{,set}ctor to templates #2655
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,264 @@ | ||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
| This module contains compiler support for constructing dynamic arrays | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| 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 rt/_array/_construction.d) | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
| module rt.array.construction; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
| * Does array initialization (not assignment) from another array of the same element type. | ||||||||||||||||||||||||||||||||||||||||||
| * Params: | ||||||||||||||||||||||||||||||||||||||||||
| * to = what array to initialize | ||||||||||||||||||||||||||||||||||||||||||
| * from = what data the array should be initialized with | ||||||||||||||||||||||||||||||||||||||||||
| * Returns: | ||||||||||||||||||||||||||||||||||||||||||
| * The constructed `to` | ||||||||||||||||||||||||||||||||||||||||||
| * Bugs: | ||||||||||||||||||||||||||||||||||||||||||
| * This function template was ported from a much older runtime hook that bypassed safety, | ||||||||||||||||||||||||||||||||||||||||||
| * purity, and throwabilty checks. To prevent breaking existing code, this function template | ||||||||||||||||||||||||||||||||||||||||||
| * is temporarily declared `@trusted` until the implementation can be brought up to modern D expectations. | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
| Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| pragma(inline, false); | ||||||||||||||||||||||||||||||||||||||||||
| import core.internal.traits : Unqual; | ||||||||||||||||||||||||||||||||||||||||||
| import core.stdc.string : memcpy; | ||||||||||||||||||||||||||||||||||||||||||
| debug(PRINTF) import core.stdc.stdio; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Force `enforceRawArraysConformable` to be `pure` | ||||||||||||||||||||||||||||||||||||||||||
| void enforceRawArraysConformable(const char[] action, in size_t elementSize, const void[] a1, const void[] a2, in bool allowOverlap = false) @trusted | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| import rt.util.array : enforceRawArraysConformable; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| alias Type = void function(const char[] action, in size_t elementSize, const void[] a1, const void[] a2, in bool allowOverlap = false) pure nothrow; | ||||||||||||||||||||||||||||||||||||||||||
| (cast(Type)&enforceRawArraysConformable)(action, elementSize, a1, a2, allowOverlap); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| debug(PRINTF) printf("_d_arrayctor(to = %p,%d, from = %p,%d) size = %d\n", from.ptr, from.length, to.ptr, to.length, T.tsize); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| auto element_size = T.sizeof; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| void[] vFrom = (cast(void*)from.ptr)[0..from.length]; | ||||||||||||||||||||||||||||||||||||||||||
| void[] vTo = (cast(void*)to.ptr)[0..to.length]; | ||||||||||||||||||||||||||||||||||||||||||
| enforceRawArraysConformable("initialization", element_size, vFrom, vTo, false); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| size_t i; | ||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| for (i = 0; i < to.length; i++) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| auto elem = cast(Unqual!T*)&to[i]; | ||||||||||||||||||||||||||||||||||||||||||
| // Copy construction is defined as bit copy followed by postblit. | ||||||||||||||||||||||||||||||||||||||||||
| memcpy(elem, &from[i], element_size); | ||||||||||||||||||||||||||||||||||||||||||
| _postblitRecurse(*elem); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| catch (Exception o) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for reference, this means calling destructors will not be done if the postblit threw a non-exception throwable. IIRC the language spec allows this behaviour @WalterBright @andralex ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See Lines 180 to 199 in ab890e0
|
||||||||||||||||||||||||||||||||||||||||||
| /* Destroy, in reverse order, what we've constructed so far | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
| while (i--) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| auto elem = cast(Unqual!T*)&to[i]; | ||||||||||||||||||||||||||||||||||||||||||
| destroy(*elem); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| throw o; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return to; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @safe unittest | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| int counter; | ||||||||||||||||||||||||||||||||||||||||||
| struct S | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| int val; | ||||||||||||||||||||||||||||||||||||||||||
| this(this) { counter++; } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| S[4] arr1; | ||||||||||||||||||||||||||||||||||||||||||
| S[4] arr2 = [S(0), S(1), S(2), S(3)]; | ||||||||||||||||||||||||||||||||||||||||||
| _d_arrayctor(arr1[], arr2[]); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| assert(counter == 4); | ||||||||||||||||||||||||||||||||||||||||||
| assert(arr1 == arr2); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @safe nothrow unittest | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| // Test that throwing works | ||||||||||||||||||||||||||||||||||||||||||
| int counter; | ||||||||||||||||||||||||||||||||||||||||||
| bool didThrow; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| struct Throw | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| int val; | ||||||||||||||||||||||||||||||||||||||||||
| this(this) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| counter++; | ||||||||||||||||||||||||||||||||||||||||||
| if (counter == 2) | ||||||||||||||||||||||||||||||||||||||||||
| throw new Exception(""); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| Throw[4] a; | ||||||||||||||||||||||||||||||||||||||||||
| Throw[4] b = [Throw(1), Throw(2), Throw(3), Throw(4)]; | ||||||||||||||||||||||||||||||||||||||||||
| _d_arrayctor(a[], b[]); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| catch (Exception) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| didThrow = true; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| assert(didThrow); | ||||||||||||||||||||||||||||||||||||||||||
| assert(counter == 2); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Test that `nothrow` works | ||||||||||||||||||||||||||||||||||||||||||
| didThrow = false; | ||||||||||||||||||||||||||||||||||||||||||
| counter = 0; | ||||||||||||||||||||||||||||||||||||||||||
| struct NoThrow | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| int val; | ||||||||||||||||||||||||||||||||||||||||||
| this(this) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| counter++; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| NoThrow[4] a; | ||||||||||||||||||||||||||||||||||||||||||
| NoThrow[4] b = [NoThrow(1), NoThrow(2), NoThrow(3), NoThrow(4)]; | ||||||||||||||||||||||||||||||||||||||||||
| _d_arrayctor(a[], b[]); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| catch (Exception) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| didThrow = false; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| assert(!didThrow); | ||||||||||||||||||||||||||||||||||||||||||
| assert(counter == 4); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
| * Do construction of an array. | ||||||||||||||||||||||||||||||||||||||||||
| * ti[count] p = value; | ||||||||||||||||||||||||||||||||||||||||||
| * Params: | ||||||||||||||||||||||||||||||||||||||||||
| * p = what array to initialize | ||||||||||||||||||||||||||||||||||||||||||
| * value = what data to construct the array with | ||||||||||||||||||||||||||||||||||||||||||
| * Bugs: | ||||||||||||||||||||||||||||||||||||||||||
| * This function template was ported from a much older runtime hook that bypassed safety, | ||||||||||||||||||||||||||||||||||||||||||
| * purity, and throwabilty checks. To prevent breaking existing code, this function template | ||||||||||||||||||||||||||||||||||||||||||
| * is temporarily declared `@trusted` until the implementation can be brought up to modern D expectations. | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
| void _d_arraysetctor(Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| pragma(inline, false); | ||||||||||||||||||||||||||||||||||||||||||
| import core.stdc.string : memcpy; | ||||||||||||||||||||||||||||||||||||||||||
| import core.internal.traits : Unqual; | ||||||||||||||||||||||||||||||||||||||||||
| size_t walker; | ||||||||||||||||||||||||||||||||||||||||||
| auto element_size = T.sizeof; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| foreach (i; 0 .. p.length) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| auto elem = cast(Unqual!T*)&p[walker]; | ||||||||||||||||||||||||||||||||||||||||||
| // Copy construction is defined as bit copy followed by postblit. | ||||||||||||||||||||||||||||||||||||||||||
| memcpy(elem, &value, element_size); | ||||||||||||||||||||||||||||||||||||||||||
| _postblitRecurse(*elem); | ||||||||||||||||||||||||||||||||||||||||||
| walker++; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| catch (Exception o) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| // Destroy, in reverse order, what we've constructed so far | ||||||||||||||||||||||||||||||||||||||||||
| while (walker > 0) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| walker--; | ||||||||||||||||||||||||||||||||||||||||||
| auto elem = cast(Unqual!T*)&p[walker]; | ||||||||||||||||||||||||||||||||||||||||||
| destroy(*elem); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| throw o; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @safe unittest | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| int counter; | ||||||||||||||||||||||||||||||||||||||||||
| struct S | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| int val; | ||||||||||||||||||||||||||||||||||||||||||
| this(this) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| counter++; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| S[4] arr; | ||||||||||||||||||||||||||||||||||||||||||
| S s = S(1234); | ||||||||||||||||||||||||||||||||||||||||||
| _d_arraysetctor(arr[], s); | ||||||||||||||||||||||||||||||||||||||||||
| assert(counter == arr.length); | ||||||||||||||||||||||||||||||||||||||||||
| assert(arr == [S(1234), S(1234), S(1234), S(1234)]); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @safe nothrow unittest | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| // Test that throwing works | ||||||||||||||||||||||||||||||||||||||||||
| int counter; | ||||||||||||||||||||||||||||||||||||||||||
| bool didThrow; | ||||||||||||||||||||||||||||||||||||||||||
| struct Throw | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| int val; | ||||||||||||||||||||||||||||||||||||||||||
| this(this) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| counter++; | ||||||||||||||||||||||||||||||||||||||||||
| if (counter == 2) | ||||||||||||||||||||||||||||||||||||||||||
| throw new Exception("Oh no."); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| Throw[4] a; | ||||||||||||||||||||||||||||||||||||||||||
| Throw[4] b = [Throw(1), Throw(2), Throw(3), Throw(4)]; | ||||||||||||||||||||||||||||||||||||||||||
| _d_arrayctor(a[], b[]); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| catch (Exception) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| didThrow = true; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| assert(didThrow); | ||||||||||||||||||||||||||||||||||||||||||
| assert(counter == 2); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Test that `nothrow` works | ||||||||||||||||||||||||||||||||||||||||||
| didThrow = false; | ||||||||||||||||||||||||||||||||||||||||||
| counter = 0; | ||||||||||||||||||||||||||||||||||||||||||
| struct NoThrow | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| int val; | ||||||||||||||||||||||||||||||||||||||||||
| this(this) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| counter++; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| NoThrow[4] a; | ||||||||||||||||||||||||||||||||||||||||||
| NoThrow b = NoThrow(1); | ||||||||||||||||||||||||||||||||||||||||||
| _d_arraysetctor(a[], b); | ||||||||||||||||||||||||||||||||||||||||||
| foreach (ref e; a) | ||||||||||||||||||||||||||||||||||||||||||
| assert(e == NoThrow(1)); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| catch (Exception) | ||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||
| didThrow = false; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| assert(!didThrow); | ||||||||||||||||||||||||||||||||||||||||||
| assert(counter == 4); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.