PATCH RFA: Support Plan 9 extensions in gcc

Ian Lance Taylor iant@google.com
Thu Apr 9 19:35:00 GMT 2009


gcc currently supports an option -fms-extensions which permits some uses
of unnamed fields in structs and unions.  The Plan 9 C compiler has two
extensions which are not included in -fms-extensions.

The first extension is if a structure has an anonymous field, a pointer
to that structure can be automatically converted to apointer to the
anonymous field in an assignment or a function call.  For example:

struct s1 { int a; };
struct s2 { int b; struct s1; };
extern void f1 (struct s1 *);
void f2 (struct s2 *p) { f1 (p); }

Here struct s2 has an anonymous field of type struct s1.  When calling
f1 from f2, the pointer to s2 is converted into a pointer to s1.  Note
that the anonymous field is not the first field in s2, so this actually
adjusts the pointer in order to make the call.

The second extension is that if a pointer has an anonymous field, and
the type of the anonymous field is a typedef for a struct or a union,
then code can refer to the entire anonymous field using the typedef
name.  For example, this is valid:

typedef struct { int a; } s3;
struct s4 { s3; };
s3 f1 (struct s4 *p) { return p->s3; }

Note the use of the typedef name s3 as the name of a field in s4.

In order to use gcc to compile code written for the Plan 9 compiler, gcc
should support these extensions.  Therefore, I propose the following
patch.  This has been bootstrapped and tested on i686-pc-linux-gnu.

OK for mainline?

If committed, I will prepare an entry for the gcc 4.5 changes.html page.

Ian


gcc/ChangeLog:

2009-04-09  Ian Lance Taylor  <iant@google.com>

	* c.opt (fplan9-extensions): New option.
	* c-typeck.c (lookup_field): If flag_plan9_extensions, permit
	using a typedef name for an anonymous field.
	(find_anonymous_field_with_type): New static function.
	(convert_to_anonymous_field): New static function.
	(convert_for_assignment): If flag_plan9_extensions, permit
	converting a pointer to a struct to a pointer to an anonymous
	field.
	* c-decl.c (grokfield): Handle flag_plan9_extensions like
	flag_ms_extensions.
	* doc/invoke.texi (Option Summary): Mention -fplan9-extensions.
	(C Dialect Options): Document -fplan9-extensions.
	* doc/extend.texi (Unnamed Fields): Document Plan 9 extensions.

gcc/testsuite/ChangeLog:

2009-04-09  Ian Lance Taylor  <iant@google.com>

	* gcc.dg/anon-struct-9.c: New test.
	* gcc.dg/anon-struct-10.c: New test.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: foo.patch
Type: text/x-patch
Size: 13219 bytes
Desc: Add -fplan9-extensions
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20090409/4155a8c5/attachment.bin>


More information about the Gcc-patches mailing list