This file is indexed.

/usr/share/doc/llvm-3.6-doc/html/HowToSetUpLLVMStyleRTTI.html is in llvm-3.6-doc 1:3.6-2ubuntu1~trusty2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>How to set up LLVM-style RTTI for your class hierarchy &mdash; LLVM 3.6 documentation</title>
    
    <link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '3.6',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="LLVM 3.6 documentation" href="index.html" />
    <link rel="next" title="LLVM Programmer’s Manual" href="ProgrammersManual.html" />
    <link rel="prev" title="Extending LLVM: Adding instructions, intrinsics, types, etc." href="ExtendingLLVM.html" />
<style type="text/css">
  table.right { float: right; margin-left: 20px; }
  table.right td { border: 1px solid #ccc; }
</style>

  </head>
  <body>
<div class="logo">
  <a href="index.html">
    <img src="_static/logo.png"
         alt="LLVM Logo" width="250" height="88"/></a>
</div>

    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="ProgrammersManual.html" title="LLVM Programmer’s Manual"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="ExtendingLLVM.html" title="Extending LLVM: Adding instructions, intrinsics, types, etc."
             accesskey="P">previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>
 
      </ul>
    </div>


    <div class="document">
      <div class="documentwrapper">
          <div class="body">
            
  <div class="section" id="how-to-set-up-llvm-style-rtti-for-your-class-hierarchy">
<h1><a class="toc-backref" href="#id1">How to set up LLVM-style RTTI for your class hierarchy</a><a class="headerlink" href="#how-to-set-up-llvm-style-rtti-for-your-class-hierarchy" title="Permalink to this headline"></a></h1>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#how-to-set-up-llvm-style-rtti-for-your-class-hierarchy" id="id1">How to set up LLVM-style RTTI for your class hierarchy</a><ul>
<li><a class="reference internal" href="#background" id="id2">Background</a></li>
<li><a class="reference internal" href="#basic-setup" id="id3">Basic Setup</a></li>
<li><a class="reference internal" href="#concrete-bases-and-deeper-hierarchies" id="id4">Concrete Bases and Deeper Hierarchies</a><ul>
<li><a class="reference internal" href="#a-bug-to-be-aware-of" id="id5">A Bug to be Aware Of</a></li>
<li><a class="reference internal" href="#the-contract-of-classof" id="id6">The Contract of <tt class="docutils literal"><span class="pre">classof</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#rules-of-thumb" id="id7">Rules of Thumb</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="background">
<h2><a class="toc-backref" href="#id2">Background</a><a class="headerlink" href="#background" title="Permalink to this headline"></a></h2>
<p>LLVM avoids using C++&#8217;s built in RTTI. Instead, it  pervasively uses its
own hand-rolled form of RTTI which is much more efficient and flexible,
although it requires a bit more work from you as a class author.</p>
<p>A description of how to use LLVM-style RTTI from a client&#8217;s perspective is
given in the <a class="reference external" href="ProgrammersManual.html#isa">Programmer&#8217;s Manual</a>. This
document, in contrast, discusses the steps you need to take as a class
hierarchy author to make LLVM-style RTTI available to your clients.</p>
<p>Before diving in, make sure that you are familiar with the Object Oriented
Programming concept of &#8220;<a class="reference external" href="http://en.wikipedia.org/wiki/Is-a">is-a</a>&#8221;.</p>
</div>
<div class="section" id="basic-setup">
<h2><a class="toc-backref" href="#id3">Basic Setup</a><a class="headerlink" href="#basic-setup" title="Permalink to this headline"></a></h2>
<p>This section describes how to set up the most basic form of LLVM-style RTTI
(which is sufficient for 99.9% of the cases). We will set up LLVM-style
RTTI for this class hierarchy:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Shape</span> <span class="p">{</span>
<span class="nl">public:</span>
  <span class="n">Shape</span><span class="p">()</span> <span class="p">{}</span>
  <span class="k">virtual</span> <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">Square</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="kt">double</span> <span class="n">SideLength</span><span class="p">;</span>
<span class="nl">public:</span>
  <span class="n">Square</span><span class="p">(</span><span class="kt">double</span> <span class="n">S</span><span class="p">)</span> <span class="o">:</span> <span class="n">SideLength</span><span class="p">(</span><span class="n">S</span><span class="p">)</span> <span class="p">{}</span>
  <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">Circle</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="kt">double</span> <span class="n">Radius</span><span class="p">;</span>
<span class="nl">public:</span>
  <span class="n">Circle</span><span class="p">(</span><span class="kt">double</span> <span class="n">R</span><span class="p">)</span> <span class="o">:</span> <span class="n">Radius</span><span class="p">(</span><span class="n">R</span><span class="p">)</span> <span class="p">{}</span>
  <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
</div>
<p>The most basic working setup for LLVM-style RTTI requires the following
steps:</p>
<ol class="arabic">
<li><p class="first">In the header where you declare <tt class="docutils literal"><span class="pre">Shape</span></tt>, you will want to <tt class="docutils literal"><span class="pre">#include</span>
<span class="pre">&quot;llvm/Support/Casting.h&quot;</span></tt>, which declares LLVM&#8217;s RTTI templates. That
way your clients don&#8217;t even have to think about it.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#include &quot;llvm/Support/Casting.h&quot;</span>
</pre></div>
</div>
</li>
<li><p class="first">In the base class, introduce an enum which discriminates all of the
different concrete classes in the hierarchy, and stash the enum value
somewhere in the base class.</p>
<p>Here is the code after introducing this change:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">class</span> <span class="nc">Shape</span> <span class="p">{</span>
 <span class="nl">public:</span>
<span class="o">+</span>  <span class="c1">/// Discriminator for LLVM-style RTTI (dyn_cast&lt;&gt; et al.)</span>
<span class="o">+</span>  <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
<span class="o">+</span>    <span class="n">SK_Square</span><span class="p">,</span>
<span class="o">+</span>    <span class="n">SK_Circle</span>
<span class="o">+</span>  <span class="p">};</span>
<span class="o">+</span><span class="k">private</span><span class="o">:</span>
<span class="o">+</span>  <span class="k">const</span> <span class="n">ShapeKind</span> <span class="n">Kind</span><span class="p">;</span>
<span class="o">+</span><span class="k">public</span><span class="o">:</span>
<span class="o">+</span>  <span class="n">ShapeKind</span> <span class="n">getKind</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">Kind</span><span class="p">;</span> <span class="p">}</span>
<span class="o">+</span>
   <span class="n">Shape</span><span class="p">()</span> <span class="p">{}</span>
   <span class="k">virtual</span> <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
 <span class="p">};</span>
</pre></div>
</div>
<p>You will usually want to keep the <tt class="docutils literal"><span class="pre">Kind</span></tt> member encapsulated and
private, but let the enum <tt class="docutils literal"><span class="pre">ShapeKind</span></tt> be public along with providing a
<tt class="docutils literal"><span class="pre">getKind()</span></tt> method. This is convenient for clients so that they can do
a <tt class="docutils literal"><span class="pre">switch</span></tt> over the enum.</p>
<p>A common naming convention is that these enums are &#8220;kind&#8221;s, to avoid
ambiguity with the words &#8220;type&#8221; or &#8220;class&#8221; which have overloaded meanings
in many contexts within LLVM. Sometimes there will be a natural name for
it, like &#8220;opcode&#8221;. Don&#8217;t bikeshed over this; when in doubt use <tt class="docutils literal"><span class="pre">Kind</span></tt>.</p>
<p>You might wonder why the <tt class="docutils literal"><span class="pre">Kind</span></tt> enum doesn&#8217;t have an entry for
<tt class="docutils literal"><span class="pre">Shape</span></tt>. The reason for this is that since <tt class="docutils literal"><span class="pre">Shape</span></tt> is abstract
(<tt class="docutils literal"><span class="pre">computeArea()</span> <span class="pre">=</span> <span class="pre">0;</span></tt>), you will never actually have non-derived
instances of exactly that class (only subclasses). See <a class="reference internal" href="#concrete-bases-and-deeper-hierarchies">Concrete Bases
and Deeper Hierarchies</a> for information on how to deal with
non-abstract bases. It&#8217;s worth mentioning here that unlike
<tt class="docutils literal"><span class="pre">dynamic_cast&lt;&gt;</span></tt>, LLVM-style RTTI can be used (and is often used) for
classes that don&#8217;t have v-tables.</p>
</li>
<li><p class="first">Next, you need to make sure that the <tt class="docutils literal"><span class="pre">Kind</span></tt> gets initialized to the
value corresponding to the dynamic type of the class. Typically, you will
want to have it be an argument to the constructor of the base class, and
then pass in the respective <tt class="docutils literal"><span class="pre">XXXKind</span></tt> from subclass constructors.</p>
<p>Here is the code after that change:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">class</span> <span class="nc">Shape</span> <span class="p">{</span>
 <span class="nl">public:</span>
   <span class="c1">/// Discriminator for LLVM-style RTTI (dyn_cast&lt;&gt; et al.)</span>
   <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
     <span class="n">SK_Square</span><span class="p">,</span>
     <span class="n">SK_Circle</span>
   <span class="p">};</span>
 <span class="nl">private:</span>
   <span class="k">const</span> <span class="n">ShapeKind</span> <span class="n">Kind</span><span class="p">;</span>
 <span class="nl">public:</span>
   <span class="n">ShapeKind</span> <span class="n">getKind</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">Kind</span><span class="p">;</span> <span class="p">}</span>

<span class="o">-</span>  <span class="n">Shape</span><span class="p">()</span> <span class="p">{}</span>
<span class="o">+</span>  <span class="n">Shape</span><span class="p">(</span><span class="n">ShapeKind</span> <span class="n">K</span><span class="p">)</span> <span class="o">:</span> <span class="n">Kind</span><span class="p">(</span><span class="n">K</span><span class="p">)</span> <span class="p">{}</span>
   <span class="k">virtual</span> <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
 <span class="p">};</span>

 <span class="k">class</span> <span class="nc">Square</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
   <span class="kt">double</span> <span class="n">SideLength</span><span class="p">;</span>
 <span class="nl">public:</span>
<span class="o">-</span>  <span class="n">Square</span><span class="p">(</span><span class="kt">double</span> <span class="n">S</span><span class="p">)</span> <span class="o">:</span> <span class="n">SideLength</span><span class="p">(</span><span class="n">S</span><span class="p">)</span> <span class="p">{}</span>
<span class="o">+</span>  <span class="n">Square</span><span class="p">(</span><span class="kt">double</span> <span class="n">S</span><span class="p">)</span> <span class="o">:</span> <span class="n">Shape</span><span class="p">(</span><span class="n">SK_Square</span><span class="p">),</span> <span class="n">SideLength</span><span class="p">(</span><span class="n">S</span><span class="p">)</span> <span class="p">{}</span>
   <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
 <span class="p">};</span>

 <span class="k">class</span> <span class="nc">Circle</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
   <span class="kt">double</span> <span class="n">Radius</span><span class="p">;</span>
 <span class="nl">public:</span>
<span class="o">-</span>  <span class="n">Circle</span><span class="p">(</span><span class="kt">double</span> <span class="n">R</span><span class="p">)</span> <span class="o">:</span> <span class="n">Radius</span><span class="p">(</span><span class="n">R</span><span class="p">)</span> <span class="p">{}</span>
<span class="o">+</span>  <span class="n">Circle</span><span class="p">(</span><span class="kt">double</span> <span class="n">R</span><span class="p">)</span> <span class="o">:</span> <span class="n">Shape</span><span class="p">(</span><span class="n">SK_Circle</span><span class="p">),</span> <span class="n">Radius</span><span class="p">(</span><span class="n">R</span><span class="p">)</span> <span class="p">{}</span>
   <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
 <span class="p">};</span>
</pre></div>
</div>
</li>
<li><p class="first">Finally, you need to inform LLVM&#8217;s RTTI templates how to dynamically
determine the type of a class (i.e. whether the <tt class="docutils literal"><span class="pre">isa&lt;&gt;</span></tt>/<tt class="docutils literal"><span class="pre">dyn_cast&lt;&gt;</span></tt>
should succeed). The default &#8220;99.9% of use cases&#8221; way to accomplish this
is through a small static member function <tt class="docutils literal"><span class="pre">classof</span></tt>. In order to have
proper context for an explanation, we will display this code first, and
then below describe each part:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">class</span> <span class="nc">Shape</span> <span class="p">{</span>
 <span class="nl">public:</span>
   <span class="c1">/// Discriminator for LLVM-style RTTI (dyn_cast&lt;&gt; et al.)</span>
   <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
     <span class="n">SK_Square</span><span class="p">,</span>
     <span class="n">SK_Circle</span>
   <span class="p">};</span>
 <span class="nl">private:</span>
   <span class="k">const</span> <span class="n">ShapeKind</span> <span class="n">Kind</span><span class="p">;</span>
 <span class="nl">public:</span>
   <span class="n">ShapeKind</span> <span class="n">getKind</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">Kind</span><span class="p">;</span> <span class="p">}</span>

   <span class="n">Shape</span><span class="p">(</span><span class="n">ShapeKind</span> <span class="n">K</span><span class="p">)</span> <span class="o">:</span> <span class="n">Kind</span><span class="p">(</span><span class="n">K</span><span class="p">)</span> <span class="p">{}</span>
   <span class="k">virtual</span> <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
 <span class="p">};</span>

 <span class="k">class</span> <span class="nc">Square</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
   <span class="kt">double</span> <span class="n">SideLength</span><span class="p">;</span>
 <span class="nl">public:</span>
   <span class="n">Square</span><span class="p">(</span><span class="kt">double</span> <span class="n">S</span><span class="p">)</span> <span class="o">:</span> <span class="n">Shape</span><span class="p">(</span><span class="n">SK_Square</span><span class="p">),</span> <span class="n">SideLength</span><span class="p">(</span><span class="n">S</span><span class="p">)</span> <span class="p">{}</span>
   <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
<span class="o">+</span>
<span class="o">+</span>  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
<span class="o">+</span>    <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">==</span> <span class="n">SK_Square</span><span class="p">;</span>
<span class="o">+</span>  <span class="p">}</span>
 <span class="p">};</span>

 <span class="k">class</span> <span class="nc">Circle</span> <span class="o">:</span> <span class="k">public</span> <span class="n">Shape</span> <span class="p">{</span>
   <span class="kt">double</span> <span class="n">Radius</span><span class="p">;</span>
 <span class="nl">public:</span>
   <span class="n">Circle</span><span class="p">(</span><span class="kt">double</span> <span class="n">R</span><span class="p">)</span> <span class="o">:</span> <span class="n">Shape</span><span class="p">(</span><span class="n">SK_Circle</span><span class="p">),</span> <span class="n">Radius</span><span class="p">(</span><span class="n">R</span><span class="p">)</span> <span class="p">{}</span>
   <span class="kt">double</span> <span class="n">computeArea</span><span class="p">()</span> <span class="cm">/* override */</span><span class="p">;</span>
<span class="o">+</span>
<span class="o">+</span>  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
<span class="o">+</span>    <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">==</span> <span class="n">SK_Circle</span><span class="p">;</span>
<span class="o">+</span>  <span class="p">}</span>
 <span class="p">};</span>
</pre></div>
</div>
<p>The job of <tt class="docutils literal"><span class="pre">classof</span></tt> is to dynamically determine whether an object of
a base class is in fact of a particular derived class.  In order to
downcast a type <tt class="docutils literal"><span class="pre">Base</span></tt> to a type <tt class="docutils literal"><span class="pre">Derived</span></tt>, there needs to be a
<tt class="docutils literal"><span class="pre">classof</span></tt> in <tt class="docutils literal"><span class="pre">Derived</span></tt> which will accept an object of type <tt class="docutils literal"><span class="pre">Base</span></tt>.</p>
<p>To be concrete, consider the following code:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="n">Shape</span> <span class="o">*</span><span class="n">S</span> <span class="o">=</span> <span class="p">...;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">isa</span><span class="o">&lt;</span><span class="n">Circle</span><span class="o">&gt;</span><span class="p">(</span><span class="n">S</span><span class="p">))</span> <span class="p">{</span>
  <span class="cm">/* do something ... */</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The code of the <tt class="docutils literal"><span class="pre">isa&lt;&gt;</span></tt> test in this code will eventually boil
down&#8212;after template instantiation and some other machinery&#8212;to a
check roughly like <tt class="docutils literal"><span class="pre">Circle::classof(S)</span></tt>. For more information, see
<a class="reference internal" href="#classof-contract"><em>The Contract of classof</em></a>.</p>
<p>The argument to <tt class="docutils literal"><span class="pre">classof</span></tt> should always be an <em>ancestor</em> class because
the implementation has logic to allow and optimize away
upcasts/up-<tt class="docutils literal"><span class="pre">isa&lt;&gt;</span></tt>&#8216;s automatically. It is as though every class
<tt class="docutils literal"><span class="pre">Foo</span></tt> automatically has a <tt class="docutils literal"><span class="pre">classof</span></tt> like:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Foo</span> <span class="p">{</span>
  <span class="p">[...]</span>
  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="o">&gt;</span>
  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">T</span> <span class="o">*</span><span class="p">,</span>
                      <span class="o">::</span><span class="n">std</span><span class="o">::</span><span class="n">enable_if</span><span class="o">&lt;</span>
                        <span class="o">::</span><span class="n">std</span><span class="o">::</span><span class="n">is_base_of</span><span class="o">&lt;</span><span class="n">Foo</span><span class="p">,</span> <span class="n">T</span><span class="o">&gt;::</span><span class="n">value</span>
                      <span class="o">&gt;::</span><span class="n">type</span><span class="o">*</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nb">true</span><span class="p">;</span> <span class="p">}</span>
  <span class="p">[...]</span>
<span class="p">};</span>
</pre></div>
</div>
<p>Note that this is the reason that we did not need to introduce a
<tt class="docutils literal"><span class="pre">classof</span></tt> into <tt class="docutils literal"><span class="pre">Shape</span></tt>: all relevant classes derive from <tt class="docutils literal"><span class="pre">Shape</span></tt>,
and <tt class="docutils literal"><span class="pre">Shape</span></tt> itself is abstract (has no entry in the <tt class="docutils literal"><span class="pre">Kind</span></tt> enum),
so this notional inferred <tt class="docutils literal"><span class="pre">classof</span></tt> is all we need. See <a class="reference internal" href="#concrete-bases-and-deeper-hierarchies">Concrete
Bases and Deeper Hierarchies</a> for more information about how to extend
this example to more general hierarchies.</p>
</li>
</ol>
<p>Although for this small example setting up LLVM-style RTTI seems like a lot
of &#8220;boilerplate&#8221;, if your classes are doing anything interesting then this
will end up being a tiny fraction of the code.</p>
</div>
<div class="section" id="concrete-bases-and-deeper-hierarchies">
<h2><a class="toc-backref" href="#id4">Concrete Bases and Deeper Hierarchies</a><a class="headerlink" href="#concrete-bases-and-deeper-hierarchies" title="Permalink to this headline"></a></h2>
<p>For concrete bases (i.e. non-abstract interior nodes of the inheritance
tree), the <tt class="docutils literal"><span class="pre">Kind</span></tt> check inside <tt class="docutils literal"><span class="pre">classof</span></tt> needs to be a bit more
complicated. The situation differs from the example above in that</p>
<ul class="simple">
<li>Since the class is concrete, it must itself have an entry in the <tt class="docutils literal"><span class="pre">Kind</span></tt>
enum because it is possible to have objects with this class as a dynamic
type.</li>
<li>Since the class has children, the check inside <tt class="docutils literal"><span class="pre">classof</span></tt> must take them
into account.</li>
</ul>
<p>Say that <tt class="docutils literal"><span class="pre">SpecialSquare</span></tt> and <tt class="docutils literal"><span class="pre">OtherSpecialSquare</span></tt> derive
from <tt class="docutils literal"><span class="pre">Square</span></tt>, and so <tt class="docutils literal"><span class="pre">ShapeKind</span></tt> becomes:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
   <span class="n">SK_Square</span><span class="p">,</span>
<span class="o">+</span>  <span class="n">SK_SpecialSquare</span><span class="p">,</span>
<span class="o">+</span>  <span class="n">SK_OtherSpecialSquare</span><span class="p">,</span>
   <span class="n">SK_Circle</span>
 <span class="p">}</span>
</pre></div>
</div>
<p>Then in <tt class="docutils literal"><span class="pre">Square</span></tt>, we would need to modify the <tt class="docutils literal"><span class="pre">classof</span></tt> like so:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="o">-</span>  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
<span class="o">-</span>    <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">==</span> <span class="n">SK_Square</span><span class="p">;</span>
<span class="o">-</span>  <span class="p">}</span>
<span class="o">+</span>  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
<span class="o">+</span>    <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="n">SK_Square</span> <span class="o">&amp;&amp;</span>
<span class="o">+</span>           <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">SK_OtherSpecialSquare</span><span class="p">;</span>
<span class="o">+</span>  <span class="p">}</span>
</pre></div>
</div>
<p>The reason that we need to test a range like this instead of just equality
is that both <tt class="docutils literal"><span class="pre">SpecialSquare</span></tt> and <tt class="docutils literal"><span class="pre">OtherSpecialSquare</span></tt> &#8220;is-a&#8221;
<tt class="docutils literal"><span class="pre">Square</span></tt>, and so <tt class="docutils literal"><span class="pre">classof</span></tt> needs to return <tt class="docutils literal"><span class="pre">true</span></tt> for them.</p>
<p>This approach can be made to scale to arbitrarily deep hierarchies. The
trick is that you arrange the enum values so that they correspond to a
preorder traversal of the class hierarchy tree. With that arrangement, all
subclass tests can be done with two comparisons as shown above. If you just
list the class hierarchy like a list of bullet points, you&#8217;ll get the
ordering right:</p>
<div class="highlight-python"><div class="highlight"><pre>| Shape
  | Square
    | SpecialSquare
    | OtherSpecialSquare
  | Circle
</pre></div>
</div>
<div class="section" id="a-bug-to-be-aware-of">
<h3><a class="toc-backref" href="#id5">A Bug to be Aware Of</a><a class="headerlink" href="#a-bug-to-be-aware-of" title="Permalink to this headline"></a></h3>
<p>The example just given opens the door to bugs where the <tt class="docutils literal"><span class="pre">classof</span></tt>s are
not updated to match the <tt class="docutils literal"><span class="pre">Kind</span></tt> enum when adding (or removing) classes to
(from) the hierarchy.</p>
<p>Continuing the example above, suppose we add a <tt class="docutils literal"><span class="pre">SomewhatSpecialSquare</span></tt> as
a subclass of <tt class="docutils literal"><span class="pre">Square</span></tt>, and update the <tt class="docutils literal"><span class="pre">ShapeKind</span></tt> enum like so:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
   <span class="n">SK_Square</span><span class="p">,</span>
   <span class="n">SK_SpecialSquare</span><span class="p">,</span>
   <span class="n">SK_OtherSpecialSquare</span><span class="p">,</span>
<span class="o">+</span>  <span class="n">SK_SomewhatSpecialSquare</span><span class="p">,</span>
   <span class="n">SK_Circle</span>
 <span class="p">}</span>
</pre></div>
</div>
<p>Now, suppose that we forget to update <tt class="docutils literal"><span class="pre">Square::classof()</span></tt>, so it still
looks like:</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="k">static</span> <span class="kt">bool</span> <span class="nf">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// BUG: Returns false when S-&gt;getKind() == SK_SomewhatSpecialSquare,</span>
  <span class="c1">// even though SomewhatSpecialSquare &quot;is a&quot; Square.</span>
  <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="n">SK_Square</span> <span class="o">&amp;&amp;</span>
         <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">SK_OtherSpecialSquare</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>As the comment indicates, this code contains a bug. A straightforward and
non-clever way to avoid this is to introduce an explicit <tt class="docutils literal"><span class="pre">SK_LastSquare</span></tt>
entry in the enum when adding the first subclass(es). For example, we could
rewrite the example at the beginning of <a class="reference internal" href="#concrete-bases-and-deeper-hierarchies">Concrete Bases and Deeper
Hierarchies</a> as:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
   <span class="n">SK_Square</span><span class="p">,</span>
<span class="o">+</span>  <span class="n">SK_SpecialSquare</span><span class="p">,</span>
<span class="o">+</span>  <span class="n">SK_OtherSpecialSquare</span><span class="p">,</span>
<span class="o">+</span>  <span class="n">SK_LastSquare</span><span class="p">,</span>
   <span class="n">SK_Circle</span>
 <span class="p">}</span>
<span class="p">...</span>
<span class="c1">// Square::classof()</span>
<span class="o">-</span>  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
<span class="o">-</span>    <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">==</span> <span class="n">SK_Square</span><span class="p">;</span>
<span class="o">-</span>  <span class="p">}</span>
<span class="o">+</span>  <span class="k">static</span> <span class="kt">bool</span> <span class="n">classof</span><span class="p">(</span><span class="k">const</span> <span class="n">Shape</span> <span class="o">*</span><span class="n">S</span><span class="p">)</span> <span class="p">{</span>
<span class="o">+</span>    <span class="k">return</span> <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="n">SK_Square</span> <span class="o">&amp;&amp;</span>
<span class="o">+</span>           <span class="n">S</span><span class="o">-&gt;</span><span class="n">getKind</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">SK_LastSquare</span><span class="p">;</span>
<span class="o">+</span>  <span class="p">}</span>
</pre></div>
</div>
<p>Then, adding new subclasses is easy:</p>
<div class="highlight-c++"><div class="highlight"><pre> <span class="k">enum</span> <span class="n">ShapeKind</span> <span class="p">{</span>
   <span class="n">SK_Square</span><span class="p">,</span>
   <span class="n">SK_SpecialSquare</span><span class="p">,</span>
   <span class="n">SK_OtherSpecialSquare</span><span class="p">,</span>
<span class="o">+</span>  <span class="n">SK_SomewhatSpecialSquare</span><span class="p">,</span>
   <span class="n">SK_LastSquare</span><span class="p">,</span>
   <span class="n">SK_Circle</span>
 <span class="p">}</span>
</pre></div>
</div>
<p>Notice that <tt class="docutils literal"><span class="pre">Square::classof</span></tt> does not need to be changed.</p>
</div>
<div class="section" id="the-contract-of-classof">
<span id="classof-contract"></span><h3><a class="toc-backref" href="#id6">The Contract of <tt class="docutils literal"><span class="pre">classof</span></tt></a><a class="headerlink" href="#the-contract-of-classof" title="Permalink to this headline"></a></h3>
<p>To be more precise, let <tt class="docutils literal"><span class="pre">classof</span></tt> be inside a class <tt class="docutils literal"><span class="pre">C</span></tt>.  Then the
contract for <tt class="docutils literal"><span class="pre">classof</span></tt> is &#8220;return <tt class="docutils literal"><span class="pre">true</span></tt> if the dynamic type of the
argument is-a <tt class="docutils literal"><span class="pre">C</span></tt>&#8221;.  As long as your implementation fulfills this
contract, you can tweak and optimize it as much as you want.</p>
</div>
</div>
<div class="section" id="rules-of-thumb">
<h2><a class="toc-backref" href="#id7">Rules of Thumb</a><a class="headerlink" href="#rules-of-thumb" title="Permalink to this headline"></a></h2>
<ol class="arabic simple">
<li>The <tt class="docutils literal"><span class="pre">Kind</span></tt> enum should have one entry per concrete class, ordered
according to a preorder traversal of the inheritance tree.</li>
<li>The argument to <tt class="docutils literal"><span class="pre">classof</span></tt> should be a <tt class="docutils literal"><span class="pre">const</span> <span class="pre">Base</span> <span class="pre">*</span></tt>, where <tt class="docutils literal"><span class="pre">Base</span></tt>
is some ancestor in the inheritance hierarchy. The argument should
<em>never</em> be a derived class or the class itself: the template machinery
for <tt class="docutils literal"><span class="pre">isa&lt;&gt;</span></tt> already handles this case and optimizes it.</li>
<li>For each class in the hierarchy that has no children, implement a
<tt class="docutils literal"><span class="pre">classof</span></tt> that checks only against its <tt class="docutils literal"><span class="pre">Kind</span></tt>.</li>
<li>For each class in the hierarchy that has children, implement a
<tt class="docutils literal"><span class="pre">classof</span></tt> that checks a range of the first child&#8217;s <tt class="docutils literal"><span class="pre">Kind</span></tt> and the
last child&#8217;s <tt class="docutils literal"><span class="pre">Kind</span></tt>.</li>
</ol>
</div>
</div>


          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="ProgrammersManual.html" title="LLVM Programmer’s Manual"
             >next</a> |</li>
        <li class="right" >
          <a href="ExtendingLLVM.html" title="Extending LLVM: Adding instructions, intrinsics, types, etc."
             >previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>
 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2003-2014, LLVM Project.
      Last updated on 2018-07-24.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
    </div>
  </body>
</html>