1 ======================================================
2 How to set up LLVM-style RTTI for your class hierarchy
3 ======================================================
10 LLVM avoids using C++'s built in RTTI. Instead, it pervasively uses its
11 own hand-rolled form of RTTI which is much more efficient and flexible,
12 although it requires a bit more work from you as a class author.
14 A description of how to use LLVM-style RTTI from a client's perspective is
15 given in the `Programmer's Manual <ProgrammersManual.html#isa>`_. This
16 document, in contrast, discusses the steps you need to take as a class
17 hierarchy author to make LLVM-style RTTI available to your clients.
19 Before diving in, make sure that you are familiar with the Object Oriented
20 Programming concept of "`is-a`_".
22 .. _is-a: http://en.wikipedia.org/wiki/Is-a
27 This section describes how to set up the most basic form of LLVM-style RTTI
28 (which is sufficient for 99.9% of the cases). We will set up LLVM-style
29 RTTI for this class hierarchy:
36 virtual double computeArea() = 0;
39 class Square : public Shape {
42 Square(double S) : SideLength(S) {}
43 double computeArea() /* override */;
46 class Circle : public Shape {
49 Circle(double R) : Radius(R) {}
50 double computeArea() /* override */;
53 The most basic working setup for LLVM-style RTTI requires the following
56 #. In the header where you declare ``Shape``, you will want to ``#include
57 "llvm/Support/Casting.h"``, which declares LLVM's RTTI templates. That
58 way your clients don't even have to think about it.
62 #include "llvm/Support/Casting.h"
64 #. In the base class, introduce an enum which discriminates all of the
65 different concrete classes in the hierarchy, and stash the enum value
66 somewhere in the base class.
68 Here is the code after introducing this change:
74 + /// Discriminator for LLVM-style RTTI (dyn_cast<> et al.)
80 + const ShapeKind Kind;
82 + ShapeKind getKind() const { return Kind; }
85 virtual double computeArea() = 0;
88 You will usually want to keep the ``Kind`` member encapsulated and
89 private, but let the enum ``ShapeKind`` be public along with providing a
90 ``getKind()`` method. This is convenient for clients so that they can do
91 a ``switch`` over the enum.
93 A common naming convention is that these enums are "kind"s, to avoid
94 ambiguity with the words "type" or "class" which have overloaded meanings
95 in many contexts within LLVM. Sometimes there will be a natural name for
96 it, like "opcode". Don't bikeshed over this; when in doubt use ``Kind``.
98 You might wonder why the ``Kind`` enum doesn't have an entry for
99 ``Shape``. The reason for this is that since ``Shape`` is abstract
100 (``computeArea() = 0;``), you will never actually have non-derived
101 instances of exactly that class (only subclasses). See `Concrete Bases
102 and Deeper Hierarchies`_ for information on how to deal with
103 non-abstract bases. It's worth mentioning here that unlike
104 ``dynamic_cast<>``, LLVM-style RTTI can be used (and is often used) for
105 classes that don't have v-tables.
107 #. Next, you need to make sure that the ``Kind`` gets initialized to the
108 value corresponding to the dynamic type of the class. Typically, you will
109 want to have it be an argument to the constructor of the base class, and
110 then pass in the respective ``XXXKind`` from subclass constructors.
112 Here is the code after that change:
118 /// Discriminator for LLVM-style RTTI (dyn_cast<> et al.)
124 const ShapeKind Kind;
126 ShapeKind getKind() const { return Kind; }
129 + Shape(ShapeKind K) : Kind(K) {}
130 virtual double computeArea() = 0;
133 class Square : public Shape {
136 - Square(double S) : SideLength(S) {}
137 + Square(double S) : Shape(SK_Square), SideLength(S) {}
138 double computeArea() /* override */;
141 class Circle : public Shape {
144 - Circle(double R) : Radius(R) {}
145 + Circle(double R) : Shape(SK_Circle), Radius(R) {}
146 double computeArea() /* override */;
149 #. Finally, you need to inform LLVM's RTTI templates how to dynamically
150 determine the type of a class (i.e. whether the ``isa<>``/``dyn_cast<>``
151 should succeed). The default "99.9% of use cases" way to accomplish this
152 is through a small static member function ``classof``. In order to have
153 proper context for an explanation, we will display this code first, and
154 then below describe each part:
160 /// Discriminator for LLVM-style RTTI (dyn_cast<> et al.)
166 const ShapeKind Kind;
168 ShapeKind getKind() const { return Kind; }
170 Shape(ShapeKind K) : Kind(K) {}
171 virtual double computeArea() = 0;
174 class Square : public Shape {
177 Square(double S) : Shape(SK_Square), SideLength(S) {}
178 double computeArea() /* override */;
180 + static bool classof(const Shape *S) {
181 + return S->getKind() == SK_Square;
185 class Circle : public Shape {
188 Circle(double R) : Shape(SK_Circle), Radius(R) {}
189 double computeArea() /* override */;
191 + static bool classof(const Shape *S) {
192 + return S->getKind() == SK_Circle;
196 The job of ``classof`` is to dynamically determine whether an object of
197 a base class is in fact of a particular derived class. In order to
198 downcast a type ``Base`` to a type ``Derived``, there needs to be a
199 ``classof`` in ``Derived`` which will accept an object of type ``Base``.
201 To be concrete, consider the following code:
206 if (isa<Circle>(S)) {
207 /* do something ... */
210 The code of the ``isa<>`` test in this code will eventually boil
211 down---after template instantiation and some other machinery---to a
212 check roughly like ``Circle::classof(S)``. For more information, see
213 :ref:`classof-contract`.
215 The argument to ``classof`` should always be an *ancestor* class because
216 the implementation has logic to allow and optimize away
217 upcasts/up-``isa<>``'s automatically. It is as though every class
218 ``Foo`` automatically has a ``classof`` like:
225 static bool classof(const T *,
227 ::llvm::is_base_of<Foo, T>::value
228 >::type* = 0) { return true; }
232 Note that this is the reason that we did not need to introduce a
233 ``classof`` into ``Shape``: all relevant classes derive from ``Shape``,
234 and ``Shape`` itself is abstract (has no entry in the ``Kind`` enum),
235 so this notional inferred ``classof`` is all we need. See `Concrete
236 Bases and Deeper Hierarchies`_ for more information about how to extend
237 this example to more general hierarchies.
239 Although for this small example setting up LLVM-style RTTI seems like a lot
240 of "boilerplate", if your classes are doing anything interesting then this
241 will end up being a tiny fraction of the code.
243 Concrete Bases and Deeper Hierarchies
244 =====================================
246 For concrete bases (i.e. non-abstract interior nodes of the inheritance
247 tree), the ``Kind`` check inside ``classof`` needs to be a bit more
248 complicated. The situation differs from the example above in that
250 * Since the class is concrete, it must itself have an entry in the ``Kind``
251 enum because it is possible to have objects with this class as a dynamic
254 * Since the class has children, the check inside ``classof`` must take them
257 Say that ``SpecialSquare`` and ``OtherSpecialSquare`` derive
258 from ``Square``, and so ``ShapeKind`` becomes:
265 + SK_OtherSpecialSquare,
269 Then in ``Square``, we would need to modify the ``classof`` like so:
273 - static bool classof(const Shape *S) {
274 - return S->getKind() == SK_Square;
276 + static bool classof(const Shape *S) {
277 + return S->getKind() >= SK_Square &&
278 + S->getKind() <= SK_OtherSpecialSquare;
281 The reason that we need to test a range like this instead of just equality
282 is that both ``SpecialSquare`` and ``OtherSpecialSquare`` "is-a"
283 ``Square``, and so ``classof`` needs to return ``true`` for them.
285 This approach can be made to scale to arbitrarily deep hierarchies. The
286 trick is that you arrange the enum values so that they correspond to a
287 preorder traversal of the class hierarchy tree. With that arrangement, all
288 subclass tests can be done with two comparisons as shown above. If you just
289 list the class hierarchy like a list of bullet points, you'll get the
301 The example just given opens the door to bugs where the ``classof``\s are
302 not updated to match the ``Kind`` enum when adding (or removing) classes to
303 (from) the hierarchy.
305 Continuing the example above, suppose we add a ``SomewhatSpecialSquare`` as
306 a subclass of ``Square``, and update the ``ShapeKind`` enum like so:
313 SK_OtherSpecialSquare,
314 + SK_SomewhatSpecialSquare,
318 Now, suppose that we forget to update ``Square::classof()``, so it still
323 static bool classof(const Shape *S) {
324 // BUG: Returns false when S->getKind() == SK_SomewhatSpecialSquare,
325 // even though SomewhatSpecialSquare "is a" Square.
326 return S->getKind() >= SK_Square &&
327 S->getKind() <= SK_OtherSpecialSquare;
330 As the comment indicates, this code contains a bug. A straightforward and
331 non-clever way to avoid this is to introduce an explicit ``SK_LastSquare``
332 entry in the enum when adding the first subclass(es). For example, we could
333 rewrite the example at the beginning of `Concrete Bases and Deeper
341 + SK_OtherSpecialSquare,
347 - static bool classof(const Shape *S) {
348 - return S->getKind() == SK_Square;
350 + static bool classof(const Shape *S) {
351 + return S->getKind() >= SK_Square &&
352 + S->getKind() <= SK_LastSquare;
355 Then, adding new subclasses is easy:
362 SK_OtherSpecialSquare,
363 + SK_SomewhatSpecialSquare,
368 Notice that ``Square::classof`` does not need to be changed.
370 .. _classof-contract:
372 The Contract of ``classof``
373 ---------------------------
375 To be more precise, let ``classof`` be inside a class ``C``. Then the
376 contract for ``classof`` is "return ``true`` if the dynamic type of the
377 argument is-a ``C``". As long as your implementation fulfills this
378 contract, you can tweak and optimize it as much as you want.
382 Touch on some of the more advanced features, like ``isa_impl`` and
383 ``simplify_type``. However, those two need reference documentation in
384 the form of doxygen comments as well. We need the doxygen so that we can
385 say "for full details, see http://llvm.org/doxygen/..."
390 #. The ``Kind`` enum should have one entry per concrete class, ordered
391 according to a preorder traversal of the inheritance tree.
392 #. The argument to ``classof`` should be a ``const Base *``, where ``Base``
393 is some ancestor in the inheritance hierarchy. The argument should
394 *never* be a derived class or the class itself: the template machinery
395 for ``isa<>`` already handles this case and optimizes it.
396 #. For each class in the hierarchy that has no children, implement a
397 ``classof`` that checks only against its ``Kind``.
398 #. For each class in the hierarchy that has children, implement a
399 ``classof`` that checks a range of the first child's ``Kind`` and the
400 last child's ``Kind``.