www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - OK, I'm stumped on this one: dstep, struct, mixin, bitfields

reply Russel Winder <russel winder.org.uk> writes:
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 declara=
tor `ubyte`
source/libdvbv5_d/header.d-mixin-139(141): Error: identifier or integer exp=
ected 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 declara=
tor `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 declara=
tor `void`
source/libdvbv5_d/header.d-mixin-139(142): Error: identifier or integer exp=
ected 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 declara=
tor `_current_next_version_one2`
source/libdvbv5_d/header.d-mixin-139(142): Error: declaration expected, not=
 `=3D`
source/libdvbv5_d/header.d(139): Error: incomplete mixin declaration `"priv=
ate ubyte _current_next_version_one2; property ubyte current_next()  safe p=
ure nothrow  nogc const { auto result =3D (_current_next_version_one2 & 1U)=
0U; return cast(ubyte) result;}\x0a property void current_next(ubyte v) =
safe pure nothrow nogc { assert(v >=3D current_next_min, \"Value is small= er than the minimum value of bitfield 'current_next'\"); assert(v <=3D curr= ent_next_max, \"Value is greater than the maximum value of bitfield 'curren= t_next'\"); _current_next_version_one2 =3D cast(typeof(_current_next_versio= n_one2)) ((_current_next_version_one2 & (-1-cast(typeof(_current_next_versi= on_one2))1U)) | ((cast(typeof(_current_next_version_one2)) v << 0U) & 1U));= }\x0aenum ubyte current_next_min =3D cast(ubyte)0U; enum ubyte current_nex= t_max =3D cast(ubyte)1U; property ubyte version() safe pure nothrow nogc= const { auto result =3D (_current_next_version_one2 & 62U) >>1U; return ca= st(ubyte) result;}\x0a property void version(ubyte v) safe pure nothrow n= ogc { assert(v >=3D version_min, \"Value is smaller than the minimum value = of bitfield 'version'\"); assert(v <=3D version_max, \"Value is greater tha= n the maximum value of bitfield 'version'\"); _current_next_version_one2 = =3D 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 =3D cast(= ubyte)0U; enum ubyte version_max =3D cast(ubyte)31U; property ubyte one2(= ) safe pure nothrow nogc const { auto result =3D (_current_next_version_o= ne2 & 192U) >>6U; return cast(ubyte) result;}\x0a property void one2(ubyte = v) safe pure nothrow nogc { assert(v >=3D one2_min, \"Value is smaller th= an the minimum value of bitfield 'one2'\"); assert(v <=3D one2_max, \"Value= is greater than the maximum value of bitfield 'one2'\"); _current_next_ver= sion_one2 =3D cast(typeof(_current_next_version_one2)) ((_current_next_vers= ion_one2 & (-1-cast(typeof(_current_next_version_one2))192U)) | ((cast(type= of(_current_next_version_one2)) v << 6U) & 192U));}\x0aenum ubyte one2_min = =3D cast(ubyte)0U; enum ubyte one2_max =3D cast(ubyte)3U; "` --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Mar 21 2018
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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)
Mar 21 2018
next sibling parent reply Russel Winder <russel winder.org.uk> writes:
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,
=20 =20 version is a D keyword, so I would suggest trying "version_"=20 there instead and see if it works. (I'm guessing btw, the error=20 message was way to long and illegible, but this is an easy first=20 guess anyway)
On Wed, 2018-03-21 at 11:14 -0700, Ali =C3=87ehreli via Digitalmars-d-learn wrote: [=E2=80=A6]
=20
 I think it's because 'version' is a D keyword. :)
=20
 Ali
=20
Thanks to Adam and Ali, it was clear and obvious.=20 But :-( Why does version have to be a keyword? --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Mar 21 2018
parent reply Jacob Carlborg <doob me.com> writes:
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
Mar 21 2018
parent reply Russel Winder <russel winder.org.uk> writes:
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:
=20
 Thanks to Adam and Ali, it was clear and obvious.
=20 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!(=E2=80=A6)) construct. Is that li= kely easy to fix? =20 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D 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
Mar 22 2018
parent Jacob Carlborg <doob me.com> writes:
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
Mar 22 2018
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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 -- Маленькие детки - маленькие бедки.
Mar 21 2018
parent Kagamin <spam here.lot> writes:
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; }
Mar 22 2018
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Mar 21 2018