Commit 3e96a2fd by DJ Delorie Committed by DJ Delorie

c-decl.c (grokfield): Make sure the only unnamed fields we're allowing are…

c-decl.c (grokfield): Make sure the only unnamed fields we're allowing are either structs or unions.

* c-decl.c (grokfield): Make sure the only unnamed fields
we're allowing are either structs or unions.
* doc/extend.texi: Add documentation for the unnamed field
extension.

From-SVN: r46088
parent 1dde5a4e
2001-10-08 DJ Delorie <dj@redhat.com>
* c-decl.c (grokfield): Make sure the only unnamed fields
we're allowing are either structs or unions.
* doc/extend.texi: Add documentation for the unnamed field
extension.
2001-10-08 Zack Weinberg <zack@codesourcery.com> 2001-10-08 Zack Weinberg <zack@codesourcery.com>
* aclocal.m4 (gcc_AC_PROG_GNAT): New. * aclocal.m4 (gcc_AC_PROG_GNAT): New.
......
...@@ -5392,6 +5392,18 @@ grokfield (filename, line, declarator, declspecs, width) ...@@ -5392,6 +5392,18 @@ grokfield (filename, line, declarator, declspecs, width)
{ {
tree value; 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); value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
finish_decl (value, NULL_TREE, NULL_TREE); finish_decl (value, NULL_TREE, NULL_TREE);
......
...@@ -433,6 +433,7 @@ extensions, accepted by GCC in C89 mode and in C++. ...@@ -433,6 +433,7 @@ extensions, accepted by GCC in C89 mode and in C++.
* Vector Extensions:: Using vector instructions through built-in functions. * Vector Extensions:: Using vector instructions through built-in functions.
* Other Builtins:: Other built-in functions. * Other Builtins:: Other built-in functions.
* Pragmas:: Pragmas accepted by GCC. * Pragmas:: Pragmas accepted by GCC.
* Unnamed Fields:: Unnamed struct/union fields within structs/unions.
@end menu @end menu
@end ifset @end ifset
@ifclear INTERNALS @ifclear INTERNALS
...@@ -4503,6 +4504,47 @@ that of the @code{unused} attribute, except that this pragma may appear ...@@ -4503,6 +4504,47 @@ that of the @code{unused} attribute, except that this pragma may appear
anywhere within the variables' scopes. anywhere within the variables' scopes.
@end table @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}.
You must never create such structures that cause ambiguous field definitions.
For example, this structure:
@example
struct @{
int a;
struct @{
int a;
@};
@} foo;
@end example
It is ambiguous which @code{a} is being referred to with @samp{foo.a}.
Such constructs are not supported and must be avoided. In the future,
such constructs may be detected and treated as compilation errors.
@node C++ Extensions @node C++ Extensions
@chapter Extensions to the C++ Language @chapter Extensions to the C++ Language
@cindex extensions, C++ language @cindex extensions, C++ language
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment