This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Fix PR c/6660



Hi All,

On 28 May 2002, Jim Wilson wrote:

> For extra points, I looked up the Plan9 reference.
> http://plan9.bell-labs.com/sys/doc/comp.html
> This is about 2/3 of the way down, in a section called "extensions".
> There is a little more detail here, but still not as much as I'd like to see.
> This one explicitly allows anonymous typedefs within a structure.  It also
> goes a little farther and allows one to use the super-struct in contexts
> where the base-struct is required, kind of like C++ (though I probably have
> the terminology wrong).

  The Plan9 extension is effectively structure inheritance and it gets 
used extensively in the Inferno code (which I have worked on).  Under GCC 
(3.0.x not 3.1.x+) on my current project we are forced to use a hack that 
goes something like this:

-----------------------------------------------

/* structure inheritance */
#define inherit(x)      union { x x; x; }

typedef struct base base;
struct base {
	int foo;
};

typedef struct ext_base ext_base;
struct ext_base {
	inherit(base);
	int boo;
};

/* static cast to another (inherited) object pointer type
 * with a small amount of type checking */     
#define scast(t, o)     ((t)((vaddr_t)&(o)->t - - 1))

void
boohoo(ext_base *e)
{
	printf("foo %d boo %d\n", e->foo, e->boo);

	base *b = scast(base *, e);

	printf(foo %d\n", b->foo);
}

------------------------------------------------

  This is nowhere as clean as the Plan9 extension but it does work ...

	Thanks,

		CraigN
--
      Craig Newell                email: CraigN@ieee.org
      Free Spirit                 icbm:  N 42°38'47" W 71°18'19"



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]