Thread overview
OK, I'm stumped on this one: dstep, struct, mixin, bitfields
Mar 21, 2018
Russel Winder
Mar 21, 2018
Adam D. Ruppe
Mar 21, 2018
Russel Winder
Mar 21, 2018
Jacob Carlborg
Mar 22, 2018
Russel Winder
Mar 22, 2018
Jacob Carlborg
Mar 21, 2018
H. S. Teoh
Mar 22, 2018
Kagamin
Mar 21, 2018
Ali Çehreli
March 21, 2018
The code I am playing with generated by DStep involves lots of lots of structs with mixin bitfields. All of them seem to compile file, except one. How is it that:

    mixin(bitfields!(
        ubyte, "current_next", 1,
        ubyte, "version", 5,
        ubyte, "one2", 2)); /* TS ID */

can result in the following error. The line for "version" is 141 and the one for "one2" is 142.

source/libdvbv5_d/header.d-mixin-139(141): Error: no identifier for declarator `ubyte`
source/libdvbv5_d/header.d-mixin-139(141): Error: identifier or integer expected inside version(...), not `)`
source/libdvbv5_d/header.d-mixin-139(141): Error: found `@` when expecting `)`
source/libdvbv5_d/header.d-mixin-139(141): Error: no identifier for declarator `safe`
source/libdvbv5_d/header.d-mixin-139(141): Error: declaration expected, not `return`
source/libdvbv5_d/header.d-mixin-139(142): Error: no identifier for declarator `void`
source/libdvbv5_d/header.d-mixin-139(142): Error: identifier or integer expected inside version(...), not `ubyte`
source/libdvbv5_d/header.d-mixin-139(142): Error: found `v` when expecting `)`
source/libdvbv5_d/header.d-mixin-139(142): Error: declaration expected, not `)`
source/libdvbv5_d/header.d-mixin-139(142): Error: declaration expected, not `assert`
source/libdvbv5_d/header.d-mixin-139(142): Error: no identifier for declarator `_current_next_version_one2`
source/libdvbv5_d/header.d-mixin-139(142): Error: declaration expected, not `=`
source/libdvbv5_d/header.d(139): Error: incomplete mixin declaration `"private ubyte _current_next_version_one2;@property ubyte current_next() @safe pure nothrow @nogc const { auto result = (_current_next_version_one2 & 1U) >>0U; return cast(ubyte) result;}\x0a@property void current_next(ubyte v) @safe pure nothrow @nogc { assert(v >= current_next_min, \"Value is smaller than the minimum value of bitfield 'current_next'\"); assert(v <= current_next_max, \"Value is greater than the maximum value of bitfield 'current_next'\"); _current_next_version_one2 = cast(typeof(_current_next_version_one2)) ((_current_next_version_one2 & (-1-cast(typeof(_current_next_version_one2))1U)) | ((cast(typeof(_current_next_version_one2)) v << 0U) & 1U));}\x0aenum ubyte current_next_min = cast(ubyte)0U;  enum ubyte current_next_max = cast(ubyte)1U; @property ubyte version() @safe pure nothrow @nogc const { auto result = (_current_next_version_one2 & 62U) >>1U; return cast(ubyte) result;}\x0a@property void version(ubyte v) @safe pure nothrow @nogc { assert(v >= version_min, \"Value is smaller than the minimum value of bitfield 'version'\"); assert(v <= version_max, \"Value is greater than the maximum value of bitfield 'version'\"); _current_next_version_one2 = cast(typeof(_current_next_version_one2)) ((_current_next_version_one2 & (-1-cast(typeof(_current_next_version_one2))62U)) | ((cast(typeof(_current_next_version_one2)) v << 1U) & 62U));}\x0aenum ubyte version_min = cast(ubyte)0U;  enum ubyte version_max = cast(ubyte)31U; @property ubyte one2() @safe pure nothrow @nogc const { auto result = (_current_next_version_one2 & 192U) >>6U; return cast(ubyte) result;}\x0a@property void one2(ubyte v) @safe pure nothrow @nogc { assert(v >= one2_min, \"Value is smaller than the minimum value of bitfield 'one2'\"); assert(v <= one2_max, \"Value is greater than the maximum value of bitfield 'one2'\"); _current_next_version_one2 = cast(typeof(_current_next_version_one2)) ((_current_next_version_one2 & (-1-cast(typeof(_current_next_version_one2))192U)) | ((cast(typeof(_current_next_version_one2)) v << 6U) & 192U));}\x0aenum ubyte one2_min = cast(ubyte)0U;  enum ubyte one2_max = cast(ubyte)3U; "`


-- 
Russel.
===========================================
Dr Russel Winder      t: +44 20 7585 2200
41 Buckmaster Road    m: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk


March 21, 2018
On Wednesday, 21 March 2018 at 18:00:38 UTC, Russel Winder wrote:
>         ubyte, "version", 5,


version is a D keyword, so I would suggest trying "version_" there instead and see if it works. (I'm guessing btw, the error message was way to long and illegible, but this is an easy first guess anyway)
March 21, 2018
On 03/21/2018 11:00 AM, Russel Winder wrote:
> The code I am playing with generated by DStep involves lots of lots of
> structs with mixin bitfields. All of them seem to compile file, except
> one. How is it that:
>
>      mixin(bitfields!(
>          ubyte, "current_next", 1,
>          ubyte, "version", 5,
>          ubyte, "one2", 2)); /* TS ID */
>
> can result in the following error. The line for "version" is 141 and
> the one for "one2" is 142.

I think it's because 'version' is a D keyword. :)

Ali

March 21, 2018
On Wed, 2018-03-21 at 18:11 +0000, Adam D. Ruppe via Digitalmars-d- learn wrote:
> On Wednesday, 21 March 2018 at 18:00:38 UTC, Russel Winder wrote:
> >         ubyte, "version", 5,
> 
> 
> version is a D keyword, so I would suggest trying "version_" there instead and see if it works. (I'm guessing btw, the error message was way to long and illegible, but this is an easy first guess anyway)

On Wed, 2018-03-21 at 11:14 -0700, Ali Çehreli via Digitalmars-d-learn
wrote:
[…]
> 
> I think it's because 'version' is a D keyword. :)
> 
> Ali
> 

Thanks to Adam and Ali, it was clear and obvious.

But :-(

Why does version have to be a keyword?

-- 
Russel.
===========================================
Dr Russel Winder      t: +44 20 7585 2200
41 Buckmaster Road    m: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk


March 21, 2018
On Wed, Mar 21, 2018 at 07:30:28PM +0000, Russel Winder via Digitalmars-d-learn wrote: [...]
> But :-(
> 
> Why does version have to be a keyword?
[...]

	version(all) { ... }
	version(none) { ... }
	version(Posix) { ... }
	version(Windows) { ... }

But yeah, using "version" for this purpose makes the very common identifier "version" unavailable for use.  I've been bitten by this multiple times.

	auto version = getVersion();	// NG
	auto ver = getVersion();	// OK

	struct PacketHeader {
		ushort version;		// NG
		ushort ver;		// OK
	}

:-(


T

-- 
Маленькие детки - маленькие бедки.
March 21, 2018
On 2018-03-21 20:30, Russel Winder wrote:

> Thanks to Adam and Ali, it was clear and obvious.

Please report and issue so it's not forgotten.

-- 
/Jacob Carlborg
March 22, 2018
On Wed, 2018-03-21 at 22:18 +0100, Jacob Carlborg via Digitalmars-d-learn wrote:
> On 2018-03-21 20:30, Russel Winder wrote:
> 
> > Thanks to Adam and Ali, it was clear and obvious.
> 
> Please report and issue so it's not forgotten.

I am guessing you mean against DStep rather than D :-)

Though clearly T and I would prefer version not to be a D keyword.

I suspect I have seen one place where DStep has turned version into version_ where version was a variable name. In this case "version" was a string used to create a bitfield using a mixin(bitfield!(…)) construct. Is that likely easy to fix?

-- 
Russel.
==========================================
Dr Russel Winder      t: +44 20 7585 2200
41 Buckmaster Road    m: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk


March 22, 2018
On Wednesday, 21 March 2018 at 19:41:39 UTC, H. S. Teoh wrote:
> 	version(all) { ... }
> 	version(none) { ... }
> 	version(Posix) { ... }
> 	version(Windows) { ... }
>
> But yeah, using "version" for this purpose makes the very common identifier "version" unavailable for use.  I've been bitten by this multiple times.
>
> 	auto version = getVersion();	// NG
> 	auto ver = getVersion();	// OK
>
> 	struct PacketHeader {
> 		ushort version;		// NG
> 		ushort ver;		// OK
> 	}

C code also uses `in`, `out` and `with` as identifiers.

I think, with some funny escape sequences we can do without any keyword at all:
	\1(all) { ... }
	\1(none) { ... }
	\1(Posix) { ... }
	\1(Windows) { ... }
	\5 version = getVersion();
	\2 PacketHeader {
		\6 version;
	}
March 22, 2018
On Thursday, 22 March 2018 at 09:55:44 UTC, Russel Winder wrote:

> I am guessing you mean against DStep rather than D :-)

Yes :)

> Though clearly T and I would prefer version not to be a D keyword.
>
> I suspect I have seen one place where DStep has turned version into version_ where version was a variable name. In this case "version" was a string used to create a bitfield using a mixin(bitfield!(…)) construct. Is that likely easy to fix?

Yes. It's not a string literal in the C code. The identifier should be renamed before it's added to the call to "bitfield".

--
/Jacob Carlborg