Jump to page: 1 2
Thread overview
symbol aliasing in structs
Nov 07, 2005
Hasan Aljudy
Nov 07, 2005
Regan Heath
Nov 08, 2005
Walter Bright
Nov 08, 2005
Derek Parnell
Nov 08, 2005
Walter Bright
Nov 08, 2005
Derek Parnell
Nov 09, 2005
Walter Bright
Nov 08, 2005
Ivan Senji
Nov 09, 2005
Walter Bright
Nov 09, 2005
Georg Wrede
Nov 09, 2005
Ivan Senji
Nov 09, 2005
Walter Bright
Nov 09, 2005
Ivan Senji
Nov 08, 2005
Hasan Aljudy
November 07, 2005
Not sure if this is a bug,
this works:

#struct Fields
#{
#	bit[32] b;
#	alias b field;
#}

but this doesnt:


#struct Fields
#{
#	bit[32] b;
#	alias b[0] field;
#}

produces error messege:
C:\test\d>dmd bits.d
bits.d(6): b is used as a type

on dmd 0.137

I guess it parsed that as: field is an alias for an array of zero b's. woops, type b is not defined!

According to the docs, the compiler should be smart enough to actually figure out whether b is a type or a symbol.

Quote from http://www.digitalmars.com/d/declaration.html

>Note: Type aliases can sometimes look indistinguishable from alias declarations:

>#alias foo.bar abc;	// is it a type or a symbol?
>The distinction is made in the semantic analysis pass.
November 07, 2005
On Mon, 07 Nov 2005 02:10:35 -0700, Hasan Aljudy <hasan.aljudy@gmail.com> wrote:
> Not sure if this is a bug,
> this works:
>
> #struct Fields
> #{
> #	bit[32] b;
> #	alias b field;
> #}
>
> but this doesnt:
>
>
> #struct Fields
> #{
> #	bit[32] b;
> #	alias b[0] field;
> #}
>
> produces error messege:
> C:\test\d>dmd bits.d
> bits.d(6): b is used as a type
>
> on dmd 0.137
>
> I guess it parsed that as: field is an alias for an array of zero b's. woops, type b is not defined!

Ahh! So that is what it was doing. I never did figure it out. I would _love_ for this to work.

Regan
November 08, 2005
"Hasan Aljudy" <hasan.aljudy@gmail.com> wrote in message news:dkn5mb$2aq9$1@digitaldaemon.com...
> Not sure if this is a bug,
> this works:
>
> #struct Fields
> #{
> # bit[32] b;
> # alias b field;
> #}
>
> but this doesnt:
>
>
> #struct Fields
> #{
> # bit[32] b;
> # alias b[0] field;
> #}

I'm not sure what is intended here, but b isn't a type, and one can only create an alias for a type or a symbol. b[0] is neither, hence an error message. I don't think this is a compiler bug.


November 08, 2005
On Mon, 7 Nov 2005 22:14:45 -0800, Walter Bright wrote:

> "Hasan Aljudy" <hasan.aljudy@gmail.com> wrote in message news:dkn5mb$2aq9$1@digitaldaemon.com...
>> Not sure if this is a bug,
>> this works:
>>
>> #struct Fields
>> #{
>> # bit[32] b;
>> # alias b field;
>> #}
>>
>> but this doesnt:
>>
>>
>> #struct Fields
>> #{
>> # bit[32] b;
>> # alias b[0] field;
>> #}
> 
> I'm not sure what is intended here,

I thought it was obvious what was intended. Namely that "field" is a shorthand way of referring to "b[0]"; an alias, if you like.

>but b isn't a type, and one can only
> create an alias for a type or a symbol.
>b[0] is neither, hence an error
> message. I don't think this is a compiler bug.

Fair enough comment.

What is the preferred way of referencing individual bits in a platform agnostic manner? If I have a piece of RAM that is 8 bits long, is there some way I can name the bit-fields mapped over that block of RAM? My simplistic attempt has failed big time.

struct Fields
{
   bit fldA;
   bit fldB;
   bit[4] fldC;
   bit fldD;
   bit fldE;
}

This coding above doesn't seem to map to the 8 bits of my block of RAM. By that I mean that the bit with the lowest address (ie. the address of the 8-bit long block of RAM) is the first bit and is called fldA, the next adjacent bit to the right (higher *bit* address) if fldB, then next four bits is a single field called fldC (values 0 to 15), the next adjacent bit is fldD, and the last bit in the RAM block is fldE. However, I can't seem to set them or get them correctly and the size of the struct is not 1 (one) as I'd expect.

void main()
{
    Fields q;

    q.fldA = 0;
    q.fldB = 1;
    q.fldC[] = 1;
    q.fldD = 0;
    q.fldE = 1;

    writefln("%x %d", *(cast(byte*)(&(q))), q.sizeof);
}


This compiles and prints "0 8" but I expected "7d 1"

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
8/11/2005 5:36:07 PM
November 08, 2005
"Derek Parnell" <derek@psych.ward> wrote in message news:1tud7p1r6ehw8.sfqxfg7qd4rz$.dlg@40tude.net...
> What is the preferred way of referencing individual bits in a platform agnostic manner? If I have a piece of RAM that is 8 bits long, is there some way I can name the bit-fields mapped over that block of RAM? My simplistic attempt has failed big time.

Klunky, but this'll work:

struct Fields
{
    bit foo[8];

    bit fldA() { return foo[0]; }
    bit fldA(bit s) { return foo[0] = s; }

    ... etc ...
}


November 08, 2005
On Tue, 8 Nov 2005 01:02:07 -0800, Walter Bright wrote:

> "Derek Parnell" <derek@psych.ward> wrote in message news:1tud7p1r6ehw8.sfqxfg7qd4rz$.dlg@40tude.net...
>> What is the preferred way of referencing individual bits in a platform agnostic manner? If I have a piece of RAM that is 8 bits long, is there some way I can name the bit-fields mapped over that block of RAM? My simplistic attempt has failed big time.
> 
> Klunky, but this'll work:
> 
> struct Fields
> {
>     bit foo[8];
> 
>     bit fldA() { return foo[0]; }
>     bit fldA(bit s) { return foo[0] = s; }
> 
>     ... etc ...
> }

Yuck! Can't you do better? Anyhow, would I have to worry about endian-ness with this 'technique'? In other words, does foo[0] always reference the bit with the lowest RAM address?

-- 
Derek Parnell
Melbourne, Australia
8/11/2005 11:17:08 PM
November 08, 2005
Walter Bright schrieb:
> I'm not sure what is intended here, but b isn't a type, and one can only
> create an alias for a type or a symbol. (...)
> 

But properties are symbols, right?
So, why does this code not compile:

struct foo
{
    int[] bar;
    alias bar.length size;
}

-----
foobar.d(19): no property 'length' for type 'int[]'
foobar.d(19): bar.length is used as a type
-----

This also happens with .sizeof, .min, .max, .alignof, .dup, .init, ...

(I already posted this in http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/5301 )

Thanks,
Florian
November 08, 2005
Walter Bright wrote:
> "Derek Parnell" <derek@psych.ward> wrote in message
> news:1tud7p1r6ehw8.sfqxfg7qd4rz$.dlg@40tude.net...
> 
>>What is the preferred way of referencing individual bits in a platform
>>agnostic manner? If I have a piece of RAM that is 8 bits long, is there
>>some way I can name the bit-fields mapped over that block of RAM? My
>>simplistic attempt has failed big time.
> 
> 
> Klunky, but this'll work:
> 
> struct Fields
> {
>     bit foo[8];
> 
>     bit fldA() { return foo[0]; }
>     bit fldA(bit s) { return foo[0] = s; }
> 
>     ... etc ...
> }
> 


Wouldn't return by reference simplify these cases where property doesn't need to do any real task except get or set?
Something like:
inout bit fldA() { return foo[0]; }

Is there any chance for this ever to be in D?
November 08, 2005
Walter Bright wrote:
> "Hasan Aljudy" <hasan.aljudy@gmail.com> wrote in message
> news:dkn5mb$2aq9$1@digitaldaemon.com...
> 
>>Not sure if this is a bug,
>>this works:
>>
>>#struct Fields
>>#{
>># bit[32] b;
>># alias b field;
>>#}
>>
>>but this doesnt:
>>
>>
>>#struct Fields
>>#{
>># bit[32] b;
>># alias b[0] field;
>>#}
> 
> 
> I'm not sure what is intended here, but b isn't a type, and one can only
> create an alias for a type or a symbol. b[0] is neither, hence an error
> message. I don't think this is a compiler bug.
> 
> 

Fair enough.
I wasn't clear on what "symbol" means (I think I'm still not clear on it). I thought that "symbol" just means "variable".
November 09, 2005
"Derek Parnell" <derek@psych.ward> wrote in message news:1tz7dpeylfsl0$.1ry62433jeiwu$.dlg@40tude.net...
> On Tue, 8 Nov 2005 01:02:07 -0800, Walter Bright wrote:
>
> > "Derek Parnell" <derek@psych.ward> wrote in message news:1tud7p1r6ehw8.sfqxfg7qd4rz$.dlg@40tude.net...
> >> What is the preferred way of referencing individual bits in a platform agnostic manner? If I have a piece of RAM that is 8 bits long, is there some way I can name the bit-fields mapped over that block of RAM? My simplistic attempt has failed big time.
> >
> > Klunky, but this'll work:
> >
> > struct Fields
> > {
> >     bit foo[8];
> >
> >     bit fldA() { return foo[0]; }
> >     bit fldA(bit s) { return foo[0] = s; }
> >
> >     ... etc ...
> > }
>
> Yuck! Can't you do better? Anyhow, would I have to worry about endian-ness with this 'technique'? In other words, does foo[0] always reference the
bit
> with the lowest RAM address?

It always references the bit with the lowest bit address in the byte, and the lowest byte address in ram.


« First   ‹ Prev
1 2