This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch] limit and document unnamed fields.
- To: gcc-patches at gcc dot gnu dot org
- Subject: [patch] limit and document unnamed fields.
- From: DJ Delorie <dj at redhat dot com>
- Date: Wed, 3 Oct 2001 17:19:03 -0400
- CC: dj at greed dot delorie dot com
When unnamed struct/union fields in struct/unions was introduced, it
was (1) overzealous about what it allowed, and (2) not documented.
This patch attempts to fix those, and adds two test cases.
2001-10-03 DJ Delorie <dj@redhat.com>
* c-decl.c (grokdeclarator): Make sure the only unnamed fields
we're allowing are either structs or unions.
* doc/extend.texi: Add documentation for the unnamed field
extension.
* gcc.dg/20011003-1.c: New.
* gcc.dg/20011003-2.c: New.
Index: c-decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-decl.c,v
retrieving revision 1.248
diff -p -3 -r1.248 c-decl.c
*** c-decl.c 2001/10/02 07:19:43 1.248
--- c-decl.c 2001/10/03 21:16:20
*************** grokfield (filename, line, declarator, d
*** 5365,5370 ****
--- 5365,5382 ----
{
tree value;
+ if (declarator == NULL_TREE && width == NULL_TREE)
+ {
+ /* This is an unnamed decl. We only support unnamed
+ structs/unions, so check for other things and refuse them. */
+ if (TREE_CODE (TREE_VALUE (declspecs)) != RECORD_TYPE
+ && TREE_CODE (TREE_VALUE (declspecs)) != UNION_TYPE)
+ {
+ error ("Unnamed fields of type other than struct or union are not allowed.");
+ return NULL_TREE;
+ }
+ }
+
value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
finish_decl (value, NULL_TREE, NULL_TREE);
Index: doc/extend.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/extend.texi,v
retrieving revision 1.27
diff -p -3 -r1.27 extend.texi
*** extend.texi 2001/10/02 23:15:55 1.27
--- extend.texi 2001/10/03 21:16:36
*************** extensions, accepted by GCC in C89 mode
*** 433,438 ****
--- 433,439 ----
* Vector Extensions:: Using vector instructions through built-in functions.
* Other Builtins:: Other built-in functions.
* Pragmas:: Pragmas accepted by GCC.
+ * Unnamed Fields:: Unnamed struct/union fields within structs/unions.
@end menu
@end ifset
@ifclear INTERNALS
*************** produce warnings for the listed variable
*** 4502,4507 ****
--- 4503,4533 ----
that of the @code{unused} attribute, except that this pragma may appear
anywhere within the variables' scopes.
@end table
+
+ @node Unnamed Fields
+ @section Unnamed struct/union fields within structs/unions.
+ @cindex struct
+ @cindex union
+
+ For compatibility with other compilers, GCC allows you to define
+ a structure or union that contains, as fields, structures and unions
+ without names. For example:
+
+ @example
+ struct @{
+ int a;
+ union @{
+ int b;
+ float c;
+ @};
+ int d;
+ @} foo;
+ @end example
+
+ In this example, the user would be able to access members of the unnamed
+ union with code like @samp{foo.b}. Note that only unnamed structs and
+ unions are allowed, you may not have, for example, an unnamed
+ @code{int}.
@node C++ Extensions
@chapter Extensions to the C++ Language
----------------------------------------
/* { dg-do compile } */
/* { dg-options "-O0" } */
struct { int; int q; } a; /* { dg-error "Unnamed" } */
struct { union {int x;}; int q; } b;
struct { struct {int x;}; int q; } c;
union { union {int x;}; int q; } d;
union { struct {int x;}; int q; } e;
----------------------------------------
/* { dg-do run } */
/* { dg-options "-O0" } */
struct { union {int x; int y;}; int q; } b;
union { struct {int x;}; int q; } e;
main()
{
b.y = 10;
b.x = 15;
if (b.y != 15)
abort();
e.x = 10;
e.q = 15;
if (e.x != 15)
abort();
exit(0);
}