Inheritance and GTY (GNU Compiler Collection (GCC) Internals) (original) (raw)


22.2 Support for inheritance

gengtype has some support for simple class hierarchies. You can use this to have gengtype autogenerate marking routines, provided:

If your class hierarchy does not fit in this pattern, you must useSupport for user-provided GC marking routines instead.

The base class and its discriminator must be identified using the “desc” option. Each concrete subclass must use the “tag” option to identify which value of the discriminator it corresponds to.

Every class in the hierarchy must have a GTY(()) marker, as gengtype will only attempt to parse classes that have such a marker8.

class GTY((desc("%h.kind"), tag("0"))) example_base { public: int kind; tree a; };

class GTY((tag("1"))) some_subclass : public example_base { public: tree b; };

class GTY((tag("2"))) some_other_subclass : public example_base { public: tree c; };

The generated marking routines for the above will contain a “switch” on “kind”, visiting all appropriate fields. For example, if kind is 2, it will cast to “some_other_subclass” and visit fields a, b, and c.