Discussion:
Nested structures in headers and C++ compatibility?
Peter Dufault
2014-10-16 10:42:47 UTC
Permalink
In the MPC55XX headers there are typedefs that reference nested structures such as:

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

typedef struct {
uint32_t index : 10;
uint32_t count : 10;
uint32_t output : 1;
union SIU_PCR_tag pcr;
} mpc55xx_siu_pcr_config;

#ifdef __cplusplus
}
#endif

where SIU_PCR_tag is a nested structure:

struct SIU_tag {
...
union SIU_PCR_tag { /* Pad Configuration Registers */
...
}
};

and extern "C" brackets don't prevent C++ errors such as:

error: field 'pcr' has incomplete type
union SIU_PCR_tag pcr;

The only hack I have is to re-define the typedef to scope the type:

typedef struct {
uint32_t index : 10;
uint32_t count : 10;
uint32_t output : 1;
#ifdef __cplusplus
union SIU_tag::SIU_PCR_tag pcr;
#else
union SIU_PCR_tag pcr;
#endif
} mpc55xx_siu_pcr_config;

although I suppose a preferable approach might be:

#ifdef __cplusplus
typedef SIU_tag::SIU_PCR_tag SIU_PCR_tag_type;
#else
typedef union SIU_PCR_tag SIU_PCR_tag_type;
#endif
...
typedef struct {
uint32_t index : 10;
uint32_t count : 10;
uint32_t output : 1;
SIU_PCR_tag_type pcr;
} mpc55xx_siu_pcr_config;

Any thoughts? Any better way?

Peter
-----------------
Peter Dufault
HD Associates, Inc. Software and System Engineering
Gedare Bloom
2014-10-16 14:18:53 UTC
Permalink
Post by Peter Dufault
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
typedef struct {
uint32_t index : 10;
uint32_t count : 10;
uint32_t output : 1;
union SIU_PCR_tag pcr;
} mpc55xx_siu_pcr_config;
#ifdef __cplusplus
}
#endif
struct SIU_tag {
...
union SIU_PCR_tag { /* Pad Configuration Registers */
...
}
};
error: field 'pcr' has incomplete type
union SIU_PCR_tag pcr;
typedef struct {
uint32_t index : 10;
uint32_t count : 10;
uint32_t output : 1;
#ifdef __cplusplus
union SIU_tag::SIU_PCR_tag pcr;
#else
union SIU_PCR_tag pcr;
#endif
} mpc55xx_siu_pcr_config;
Maybe introduce a new conditional typedef would work, something like..

#ifdef __cplusplus
typedef SIU_tag::SIU_PCR_tag SIU_PCR_tag
#endif

Then the structure definition can stay the same.

I haven't seen this before so I don't know if it would work, but it
makes sense to me.

-Gedare
Post by Peter Dufault
#ifdef __cplusplus
typedef SIU_tag::SIU_PCR_tag SIU_PCR_tag_type;
#else
typedef union SIU_PCR_tag SIU_PCR_tag_type;
#endif
...
typedef struct {
uint32_t index : 10;
uint32_t count : 10;
uint32_t output : 1;
SIU_PCR_tag_type pcr;
} mpc55xx_siu_pcr_config;
Any thoughts? Any better way?
Peter
-----------------
Peter Dufault
HD Associates, Inc. Software and System Engineering
_______________________________________________
users mailing list
http://lists.rtems.org/mailman/listinfo/users
Peter Dufault
2014-10-16 15:06:50 UTC
Permalink
Post by Gedare Bloom
Maybe introduce a new conditional typedef would work, something like..
#ifdef __cplusplus
typedef SIU_tag::SIU_PCR_tag SIU_PCR_tag
#endif
Then the structure definition can stay the same.
I haven't seen this before so I don't know if it would work, but it
makes sense to me.
-Gedare
No, it wouldn't work. The structure definition includes:

union SIU_PCR_tag pcr;

so you need to change the structure definition to remove "union" and you need a corresponding not-C++ typedef for SIU_PCR_tag - it's back to my second suggestion.

Peter
-----------------
Peter Dufault
HD Associates, Inc. Software and System Engineering

Loading...