Jump to content
BeanBrain

Anonymous Reference Not Permitted

Recommended Posts

Bug description:

Anonymous reference with union / structure generates error - should it?

 

Steps to reproduce:

Compile the example code as is, it will generate errors. If you uncomment the #define BOOSTC line it will compile OK because the unions / structure are no longer anonymous nor are the accesses to them.

// BoostC Compiler options -v -W2 -t PIC18F4550
#include <system.h>

typedef unsigned char b_ytes;
typedef unsigned int  word;

// uncomment next line to compile successfully
//#define BOOSTC

typedef union _BYTE
{
   b_ytes _byte;
   struct 
   {
#ifdef BOOSTC
       b_ytes bits;
   } anony0;
#else        
       b_ytes bits;
   };
#endif        
} BYTE;


typedef union _WORD
{
   word _word;
   struct
   {
       b_ytes byte0;
       b_ytes byte1;
#ifdef BOOSTC
   } anony1;
#else
   };
#endif        
   struct
   {
       BYTE Byte0;
       BYTE Byte1;
#ifdef BOOSTC
   } anony2;
#else
   };
#endif        
   struct
   {
       BYTE LowB;
       BYTE HighB;
#ifdef BOOSTC
   } anony3;
#else
   };
#endif        
   struct
   {
       b_ytes v[2];
#ifdef BOOSTC
   } anony4;
#else
   };
#endif        
} WORD;

WORD W1;
WORD W2;
unsigned int  i1=123;
unsigned int  i2=456;
b_ytes b0='a';
b_ytes b1='z';

//------------------------------------------------------
void main()
{
#ifdef BOOSTC  
 W1._word=i1;
 W2.anony1.byte0=b0; 
 W2.anony1.byte1=b1;
 W2=W1;
 W2.anony2.Byte0._byte=b0;
 W2.anony3.HighB.anony0.bits=0xaa;
#else
 W1._word=i1;
 W2.byte0=b0; 
 W2.byte1=b1;
 W2=W1;
 W2.Byte0._byte=b0;
 W2.HighB.bits=0xaa;
#endif  
}  
 
 
// END





Expected behaviour:
To compile the example with #define BOOSTC commented out.


Is the problem 100% reproduceable:
Yes

IDE version:   SourceBoost IDE version 6.35
Compiler:      BoostC Compiler
Compiler version:  6.35
Target device: PIC18F4550
OS:          : XP Pro SP2

Comments:
Anonymous references make for prettier code - if you could implement it would be nice, perhaps with a compiler switch to enable if necessary

Share this post


Link to post
Share on other sites

The code you submitted is a bit difficult to read. The general rule is that the simplier the code is the sooner the problem gets fixed.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
The code you submitted is a bit difficult to read. The general rule is that the simplier the code is the sooner the problem gets fixed.

 

Regards,

Pavel

Hi Pavel,

 

Sorry about the lack of easy reading, but it was necessary in ordet to demonstrate and to test the error.

 

Regards,

G

Share this post


Link to post
Share on other sites
The code you submitted is a bit difficult to read. The general rule is that the simplier the code is the sooner the problem gets fixed.

 

Regards,

Pavel

Hi Pavel,

 

Sorry about the lack of easy reading, but it was necessary in ordet to demonstrate and to test the error.

 

I looked at the code and found out that I didn't even know that such constructions are allowed in C :( That's why compiler doesn't support this feature. Will add this work into my todo list but can't promise it will be implemented soon.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

BeanBrain,

 

I thought anonymous structures within a union was a microsoft specific extention to the language. I ported some code from MSVC++ to GCC G++ and had to name all my anonymous unions.

 

Regards

Dave

Share this post


Link to post
Share on other sites
I thought anonymous structures within a union was a microsoft specific extention to the language. I ported some code from MSVC++ to GCC G++ and had to name all my anonymous unions.

 

IIRC, Microchip's C18 supports them. That's elegant to read and saves some keystrokes, but can easily confuse lint and other static analysis tools.

 

EDIT: I confirm that MPLAB C18 supports unnamed structures within a union. That's explicitly stated on page 17 of their document DS51288A "MPLAB C18 C Compiler User's Guide" (and I do have on my HD some serial protocol handling code that makes use of this feature, as well).

 

As regards C'99, section 6.7.8 (regarding initialization) of the draft generically mentions "unnamed members of objects of type structure and union" as well.

Edited by DL1968

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×