This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 421
Convert _d_arraycast to template #2264
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 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 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 |
---|---|---|
|
@@ -4881,3 +4881,101 @@ void __ArrayDtor(T)(T[] a) | |
foreach_reverse (ref T e; a) | ||
e.__xdtor(); | ||
} | ||
|
||
/** | ||
Used by `__ArrayCast` to emit a descriptive error message. | ||
|
||
It is a template so it can be used by `__ArrayCast` in -betterC | ||
builds. It is separate from `__ArrayCast` to minimize code | ||
bloat. | ||
|
||
Params: | ||
fromType = name of the type being cast from | ||
fromSize = total size in bytes of the array being cast from | ||
toType = name of the type being cast o | ||
toSize = total size in bytes of the array being cast to | ||
*/ | ||
private void onArrayCastError()(string fromType, size_t fromSize, string toType, size_t toSize) @trusted | ||
{ | ||
import core.internal.string : unsignedToTempString; | ||
import core.stdc.stdlib : alloca; | ||
|
||
const(char)[][8] msgComponents = | ||
[ | ||
"Cannot cast `" | ||
, fromType | ||
, "` to `" | ||
, toType | ||
, "`; an array of size " | ||
, unsignedToTempString(fromSize) | ||
, " does not align on an array of size " | ||
, unsignedToTempString(toSize) | ||
]; | ||
|
||
// convert discontiguous `msgComponents` to contiguous string on the stack | ||
size_t length = 0; | ||
foreach (m ; msgComponents) | ||
length += m.length; | ||
|
||
auto msg = (cast(char*)alloca(length))[0 .. length]; | ||
|
||
size_t index = 0; | ||
foreach (m ; msgComponents) | ||
foreach (c; m) | ||
msg[index++] = c; | ||
|
||
// first argument must evaluate to `false` at compile-time to maintain memory safety in release builds | ||
assert(false, msg); | ||
} | ||
|
||
/** | ||
The compiler lowers expressions of `cast(TTo[])TFrom[]` to | ||
this implementation. | ||
|
||
Params: | ||
from = the array to reinterpret-cast | ||
|
||
Returns: | ||
`from` reinterpreted as `TTo[]` | ||
*/ | ||
TTo[] __ArrayCast(TFrom, TTo)(TFrom[] from) @nogc pure @trusted | ||
{ | ||
const fromSize = from.length * TFrom.sizeof; | ||
const toLength = fromSize / TTo.sizeof; | ||
|
||
if ((fromSize % TTo.sizeof) != 0) | ||
{ | ||
onArrayCastError(TFrom.stringof, fromSize, TTo.stringof, toLength * TTo.sizeof); | ||
} | ||
|
||
struct Array | ||
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. Does this need 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. Since there is no functions, it does not |
||
{ | ||
size_t length; | ||
void* ptr; | ||
} | ||
auto a = cast(Array*)&from; | ||
a.length = toLength; // jam new length | ||
return *cast(TTo[]*)a; | ||
} | ||
|
||
@safe @nogc pure nothrow unittest | ||
{ | ||
byte[int.sizeof * 3] b = cast(byte) 0xab; | ||
int[] i; | ||
short[] s; | ||
|
||
i = __ArrayCast!(byte, int)(b); | ||
assert(i.length == 3); | ||
foreach (v; i) | ||
assert(v == cast(int) 0xabab_abab); | ||
|
||
s = __ArrayCast!(byte, short)(b); | ||
assert(s.length == 6); | ||
foreach (v; s) | ||
assert(v == cast(short) 0xabab); | ||
|
||
s = __ArrayCast!(int, short)(i); | ||
assert(s.length == 6); | ||
foreach (v; s) | ||
assert(v == cast(short) 0xabab); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making sure that @Geod24's suggestion in #2264 (comment) isn't lost:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm waiting to find out what happens with dlang/dmd#8532. I don't think it should hold up this PR, and would even be better if it were done in a followup PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair.