blob: 222ea890322efdebbdbf686a5e6977de548b0da1 [file] [log] [blame]
Sean Silva5ce54c22012-10-05 03:32:01 +00001======================================================
2How to set up LLVM-style RTTI for your class hierarchy
3======================================================
4
Sean Silva5ce54c22012-10-05 03:32:01 +00005.. contents::
6
7Background
8==========
9
10LLVM avoids using C++'s built in RTTI. Instead, it pervasively uses its
11own hand-rolled form of RTTI which is much more efficient and flexible,
12although it requires a bit more work from you as a class author.
13
14A description of how to use LLVM-style RTTI from a client's perspective is
15given in the `Programmer's Manual <ProgrammersManual.html#isa>`_. This
16document, in contrast, discusses the steps you need to take as a class
17hierarchy author to make LLVM-style RTTI available to your clients.
18
19Before diving in, make sure that you are familiar with the Object Oriented
20Programming concept of "`is-a`_".
21
22.. _is-a: http://en.wikipedia.org/wiki/Is-a
23
24Basic Setup
25===========
26
27This 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
29RTTI for this class hierarchy:
30
31.. code-block:: c++
32
33 class Shape {
34 public:
Dmitri Gribenko8b761132012-10-05 20:52:13 +000035 Shape() {}
Sean Silva5ce54c22012-10-05 03:32:01 +000036 virtual double computeArea() = 0;
37 };
38
39 class Square : public Shape {
40 double SideLength;
41 public:
42 Square(double S) : SideLength(S) {}
Sean Silvab1548ed2015-01-28 10:26:29 +000043 double computeArea() override;
Sean Silva5ce54c22012-10-05 03:32:01 +000044 };
45
46 class Circle : public Shape {
47 double Radius;
48 public:
49 Circle(double R) : Radius(R) {}
Sean Silvab1548ed2015-01-28 10:26:29 +000050 double computeArea() override;
Sean Silva5ce54c22012-10-05 03:32:01 +000051 };
52
53The most basic working setup for LLVM-style RTTI requires the following
54steps:
55
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.
59
60 .. code-block:: c++
61
62 #include "llvm/Support/Casting.h"
63
Sean Silva5ce54c22012-10-05 03:32:01 +000064#. In the base class, introduce an enum which discriminates all of the
Sean Silvad7802ad2012-10-11 23:30:52 +000065 different concrete classes in the hierarchy, and stash the enum value
66 somewhere in the base class.
Sean Silva5ce54c22012-10-05 03:32:01 +000067
68 Here is the code after introducing this change:
69
70 .. code-block:: c++
71
72 class Shape {
73 public:
74 + /// Discriminator for LLVM-style RTTI (dyn_cast<> et al.)
75 + enum ShapeKind {
Sean Silva3b942ab2012-10-12 01:55:51 +000076 + SK_Square,
77 + SK_Circle
Sean Silva5ce54c22012-10-05 03:32:01 +000078 + };
79 +private:
80 + const ShapeKind Kind;
81 +public:
82 + ShapeKind getKind() const { return Kind; }
83 +
Dmitri Gribenko8b761132012-10-05 20:52:13 +000084 Shape() {}
Sean Silva5ce54c22012-10-05 03:32:01 +000085 virtual double computeArea() = 0;
86 };
87
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.
92
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``.
97
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
Sean Silvad7802ad2012-10-11 23:30:52 +0000101 instances of exactly that class (only subclasses). See `Concrete Bases
Sean Silva5ce54c22012-10-05 03:32:01 +0000102 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.
106
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.
111
112 Here is the code after that change:
113
114 .. code-block:: c++
115
116 class Shape {
117 public:
118 /// Discriminator for LLVM-style RTTI (dyn_cast<> et al.)
119 enum ShapeKind {
Sean Silva3b942ab2012-10-12 01:55:51 +0000120 SK_Square,
121 SK_Circle
Sean Silva5ce54c22012-10-05 03:32:01 +0000122 };
123 private:
124 const ShapeKind Kind;
125 public:
126 ShapeKind getKind() const { return Kind; }
127
Dmitri Gribenko8b761132012-10-05 20:52:13 +0000128 - Shape() {}
129 + Shape(ShapeKind K) : Kind(K) {}
Sean Silva5ce54c22012-10-05 03:32:01 +0000130 virtual double computeArea() = 0;
131 };
132
133 class Square : public Shape {
134 double SideLength;
135 public:
136 - Square(double S) : SideLength(S) {}
Sean Silva3b942ab2012-10-12 01:55:51 +0000137 + Square(double S) : Shape(SK_Square), SideLength(S) {}
Sean Silvab1548ed2015-01-28 10:26:29 +0000138 double computeArea() override;
Sean Silva5ce54c22012-10-05 03:32:01 +0000139 };
140
141 class Circle : public Shape {
142 double Radius;
143 public:
144 - Circle(double R) : Radius(R) {}
Sean Silva3b942ab2012-10-12 01:55:51 +0000145 + Circle(double R) : Shape(SK_Circle), Radius(R) {}
Sean Silvab1548ed2015-01-28 10:26:29 +0000146 double computeArea() override;
Sean Silva5ce54c22012-10-05 03:32:01 +0000147 };
148
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:
155
156 .. code-block:: c++
157
158 class Shape {
159 public:
160 /// Discriminator for LLVM-style RTTI (dyn_cast<> et al.)
161 enum ShapeKind {
Sean Silva3b942ab2012-10-12 01:55:51 +0000162 SK_Square,
163 SK_Circle
Sean Silva5ce54c22012-10-05 03:32:01 +0000164 };
165 private:
166 const ShapeKind Kind;
167 public:
168 ShapeKind getKind() const { return Kind; }
169
Dmitri Gribenko8b761132012-10-05 20:52:13 +0000170 Shape(ShapeKind K) : Kind(K) {}
Sean Silva5ce54c22012-10-05 03:32:01 +0000171 virtual double computeArea() = 0;
Sean Silva5ce54c22012-10-05 03:32:01 +0000172 };
173
174 class Square : public Shape {
175 double SideLength;
176 public:
Sean Silva3b942ab2012-10-12 01:55:51 +0000177 Square(double S) : Shape(SK_Square), SideLength(S) {}
Sean Silvab1548ed2015-01-28 10:26:29 +0000178 double computeArea() override;
Sean Silva5ce54c22012-10-05 03:32:01 +0000179 +
Sean Silva5ce54c22012-10-05 03:32:01 +0000180 + static bool classof(const Shape *S) {
Sean Silva3b942ab2012-10-12 01:55:51 +0000181 + return S->getKind() == SK_Square;
Sean Silva5ce54c22012-10-05 03:32:01 +0000182 + }
183 };
184
185 class Circle : public Shape {
186 double Radius;
187 public:
Sean Silva3b942ab2012-10-12 01:55:51 +0000188 Circle(double R) : Shape(SK_Circle), Radius(R) {}
Sean Silvab1548ed2015-01-28 10:26:29 +0000189 double computeArea() override;
Sean Silva5ce54c22012-10-05 03:32:01 +0000190 +
Sean Silva5ce54c22012-10-05 03:32:01 +0000191 + static bool classof(const Shape *S) {
Sean Silva3b942ab2012-10-12 01:55:51 +0000192 + return S->getKind() == SK_Circle;
Sean Silva5ce54c22012-10-05 03:32:01 +0000193 + }
194 };
195
Sean Silvad73b2812012-10-11 23:30:41 +0000196 The job of ``classof`` is to dynamically determine whether an object of
Sean Silvad7802ad2012-10-11 23:30:52 +0000197 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``.
Sean Silva5ce54c22012-10-05 03:32:01 +0000200
Sean Silvad7802ad2012-10-11 23:30:52 +0000201 To be concrete, consider the following code:
Sean Silvad73b2812012-10-11 23:30:41 +0000202
203 .. code-block:: c++
204
205 Shape *S = ...;
206 if (isa<Circle>(S)) {
207 /* do something ... */
208 }
209
Sean Silvad7802ad2012-10-11 23:30:52 +0000210 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
Sean Silvad73b2812012-10-11 23:30:41 +0000213 :ref:`classof-contract`.
Sean Silva5ce54c22012-10-05 03:32:01 +0000214
Sean Silvad7802ad2012-10-11 23:30:52 +0000215 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:
219
220 .. code-block:: c++
221
222 class Foo {
223 [...]
224 template <class T>
225 static bool classof(const T *,
Benjamin Kramerf04ddd02014-03-07 14:42:25 +0000226 ::std::enable_if<
227 ::std::is_base_of<Foo, T>::value
Sean Silvad7802ad2012-10-11 23:30:52 +0000228 >::type* = 0) { return true; }
229 [...]
230 };
231
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.
238
Sean Silva5ce54c22012-10-05 03:32:01 +0000239Although for this small example setting up LLVM-style RTTI seems like a lot
240of "boilerplate", if your classes are doing anything interesting then this
241will end up being a tiny fraction of the code.
242
243Concrete Bases and Deeper Hierarchies
244=====================================
245
246For concrete bases (i.e. non-abstract interior nodes of the inheritance
247tree), the ``Kind`` check inside ``classof`` needs to be a bit more
Sean Silvad7802ad2012-10-11 23:30:52 +0000248complicated. The situation differs from the example above in that
249
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
252 type.
253
254* Since the class has children, the check inside ``classof`` must take them
255 into account.
256
257Say that ``SpecialSquare`` and ``OtherSpecialSquare`` derive
Sean Silva5ce54c22012-10-05 03:32:01 +0000258from ``Square``, and so ``ShapeKind`` becomes:
259
260.. code-block:: c++
261
262 enum ShapeKind {
Sean Silva3b942ab2012-10-12 01:55:51 +0000263 SK_Square,
264 + SK_SpecialSquare,
265 + SK_OtherSpecialSquare,
266 SK_Circle
Sean Silva5ce54c22012-10-05 03:32:01 +0000267 }
268
269Then in ``Square``, we would need to modify the ``classof`` like so:
270
271.. code-block:: c++
272
Sean Silva5ce54c22012-10-05 03:32:01 +0000273 - static bool classof(const Shape *S) {
Sean Silva3b942ab2012-10-12 01:55:51 +0000274 - return S->getKind() == SK_Square;
Sean Silva5ce54c22012-10-05 03:32:01 +0000275 - }
276 + static bool classof(const Shape *S) {
Sean Silva3b942ab2012-10-12 01:55:51 +0000277 + return S->getKind() >= SK_Square &&
278 + S->getKind() <= SK_OtherSpecialSquare;
Sean Silva5ce54c22012-10-05 03:32:01 +0000279 + }
280
281The reason that we need to test a range like this instead of just equality
282is that both ``SpecialSquare`` and ``OtherSpecialSquare`` "is-a"
283``Square``, and so ``classof`` needs to return ``true`` for them.
284
285This approach can be made to scale to arbitrarily deep hierarchies. The
286trick is that you arrange the enum values so that they correspond to a
287preorder traversal of the class hierarchy tree. With that arrangement, all
288subclass tests can be done with two comparisons as shown above. If you just
289list the class hierarchy like a list of bullet points, you'll get the
290ordering right::
291
292 | Shape
293 | Square
294 | SpecialSquare
295 | OtherSpecialSquare
296 | Circle
297
Sean Silvaca11d2c2013-03-16 16:58:20 +0000298A Bug to be Aware Of
299--------------------
300
301The example just given opens the door to bugs where the ``classof``\s are
302not updated to match the ``Kind`` enum when adding (or removing) classes to
303(from) the hierarchy.
304
305Continuing the example above, suppose we add a ``SomewhatSpecialSquare`` as
306a subclass of ``Square``, and update the ``ShapeKind`` enum like so:
307
308.. code-block:: c++
309
310 enum ShapeKind {
311 SK_Square,
312 SK_SpecialSquare,
313 SK_OtherSpecialSquare,
314 + SK_SomewhatSpecialSquare,
315 SK_Circle
316 }
317
318Now, suppose that we forget to update ``Square::classof()``, so it still
319looks like:
320
321.. code-block:: c++
322
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;
328 }
329
330As the comment indicates, this code contains a bug. A straightforward and
331non-clever way to avoid this is to introduce an explicit ``SK_LastSquare``
332entry in the enum when adding the first subclass(es). For example, we could
333rewrite the example at the beginning of `Concrete Bases and Deeper
334Hierarchies`_ as:
335
336.. code-block:: c++
337
338 enum ShapeKind {
339 SK_Square,
340 + SK_SpecialSquare,
341 + SK_OtherSpecialSquare,
342 + SK_LastSquare,
343 SK_Circle
344 }
345 ...
346 // Square::classof()
347 - static bool classof(const Shape *S) {
348 - return S->getKind() == SK_Square;
349 - }
350 + static bool classof(const Shape *S) {
351 + return S->getKind() >= SK_Square &&
352 + S->getKind() <= SK_LastSquare;
353 + }
354
355Then, adding new subclasses is easy:
356
357.. code-block:: c++
358
359 enum ShapeKind {
360 SK_Square,
361 SK_SpecialSquare,
362 SK_OtherSpecialSquare,
363 + SK_SomewhatSpecialSquare,
364 SK_LastSquare,
365 SK_Circle
366 }
367
368Notice that ``Square::classof`` does not need to be changed.
369
Sean Silvad73b2812012-10-11 23:30:41 +0000370.. _classof-contract:
371
372The Contract of ``classof``
373---------------------------
374
375To be more precise, let ``classof`` be inside a class ``C``. Then the
376contract for ``classof`` is "return ``true`` if the dynamic type of the
377argument is-a ``C``". As long as your implementation fulfills this
378contract, you can tweak and optimize it as much as you want.
379
Sean Silvab8d41c52015-02-07 01:16:26 +0000380For example, LLVM-style RTTI can work fine in the presence of
381multiple-inheritance by defining an appropriate ``classof``.
382An example of this in practice is
Sylvestre Ledru72fd1032020-03-22 22:42:03 +0100383`Decl <https://clang.llvm.org/doxygen/classclang_1_1Decl.html>`_ vs.
384`DeclContext <https://clang.llvm.org/doxygen/classclang_1_1DeclContext.html>`_
Sean Silvab8d41c52015-02-07 01:16:26 +0000385inside Clang.
386The ``Decl`` hierarchy is done very similarly to the example setup
387demonstrated in this tutorial.
388The key part is how to then incorporate ``DeclContext``: all that is needed
389is in ``bool DeclContext::classof(const Decl *)``, which asks the question
390"Given a ``Decl``, how can I determine if it is-a ``DeclContext``?".
391It answers this with a simple switch over the set of ``Decl`` "kinds", and
392returning true for ones that are known to be ``DeclContext``'s.
393
Sean Silva5ce54c22012-10-05 03:32:01 +0000394.. TODO::
395
396 Touch on some of the more advanced features, like ``isa_impl`` and
397 ``simplify_type``. However, those two need reference documentation in
398 the form of doxygen comments as well. We need the doxygen so that we can
Sylvestre Ledru72fd1032020-03-22 22:42:03 +0100399 say "for full details, see https://llvm.org/doxygen/..."
Sean Silvad7802ad2012-10-11 23:30:52 +0000400
401Rules of Thumb
402==============
403
404#. The ``Kind`` enum should have one entry per concrete class, ordered
405 according to a preorder traversal of the inheritance tree.
406#. The argument to ``classof`` should be a ``const Base *``, where ``Base``
407 is some ancestor in the inheritance hierarchy. The argument should
408 *never* be a derived class or the class itself: the template machinery
409 for ``isa<>`` already handles this case and optimizes it.
410#. For each class in the hierarchy that has no children, implement a
411 ``classof`` that checks only against its ``Kind``.
412#. For each class in the hierarchy that has children, implement a
413 ``classof`` that checks a range of the first child's ``Kind`` and the
414 last child's ``Kind``.