/usr/share/doc/llvm-3.5-doc/html/HowToSetUpLLVMStyleRTTI.html is in llvm-3.5-doc 1:3.5.2-3ubuntu1.
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 — LLVM 3.5 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.5',
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.5 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 role="document">
<div class="logo">
<a href="index.html">
<img src="_static/logo.png"
alt="LLVM Logo" width="250" height="88"/></a>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<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> | </li>
<li><a href="index.html">Documentation</a>»</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body" role="main">
<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 <code class="docutils literal"><span class="pre">classof</span></code></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++’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’s perspective is
given in the <a class="reference external" href="ProgrammersManual.html#isa">Programmer’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 “<a class="reference external" href="http://en.wikipedia.org/wiki/Is-a">is-a</a>”.</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="k">public</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>
<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="k">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="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="k">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="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 <code class="docutils literal"><span class="pre">Shape</span></code>, you will want to <code class="docutils literal"><span class="pre">#include</span>
<span class="pre">"llvm/Support/Casting.h"</span></code>, which declares LLVM’s RTTI templates. That
way your clients don’t even have to think about it.</p>
<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#include</span> <span class="cpf">"llvm/Support/Casting.h"</span><span class="cp"></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="k">public</span><span class="o">:</span>
<span class="o">+</span> <span class="c1">/// Discriminator for LLVM-style RTTI (dyn_cast<> 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 <code class="docutils literal"><span class="pre">Kind</span></code> member encapsulated and
private, but let the enum <code class="docutils literal"><span class="pre">ShapeKind</span></code> be public along with providing a
<code class="docutils literal"><span class="pre">getKind()</span></code> method. This is convenient for clients so that they can do
a <code class="docutils literal"><span class="pre">switch</span></code> over the enum.</p>
<p>A common naming convention is that these enums are “kind”s, to avoid
ambiguity with the words “type” or “class” which have overloaded meanings
in many contexts within LLVM. Sometimes there will be a natural name for
it, like “opcode”. Don’t bikeshed over this; when in doubt use <code class="docutils literal"><span class="pre">Kind</span></code>.</p>
<p>You might wonder why the <code class="docutils literal"><span class="pre">Kind</span></code> enum doesn’t have an entry for
<code class="docutils literal"><span class="pre">Shape</span></code>. The reason for this is that since <code class="docutils literal"><span class="pre">Shape</span></code> is abstract
(<code class="docutils literal"><span class="pre">computeArea()</span> <span class="pre">=</span> <span class="pre">0;</span></code>), 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’s worth mentioning here that unlike
<code class="docutils literal"><span class="pre">dynamic_cast<></span></code>, LLVM-style RTTI can be used (and is often used) for
classes that don’t have v-tables.</p>
</li>
<li><p class="first">Next, you need to make sure that the <code class="docutils literal"><span class="pre">Kind</span></code> 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 <code class="docutils literal"><span class="pre">XXXKind</span></code> 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="k">public</span><span class="o">:</span>
<span class="c1">/// Discriminator for LLVM-style RTTI (dyn_cast<> 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="k">private</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="k">public</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="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="k">public</span><span class="o">:</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="k">public</span><span class="o">:</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’s RTTI templates how to dynamically
determine the type of a class (i.e. whether the <code class="docutils literal"><span class="pre">isa<></span></code>/<code class="docutils literal"><span class="pre">dyn_cast<></span></code>
should succeed). The default “99.9% of use cases” way to accomplish this
is through a small static member function <code class="docutils literal"><span class="pre">classof</span></code>. 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="k">public</span><span class="o">:</span>
<span class="c1">/// Discriminator for LLVM-style RTTI (dyn_cast<> 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="k">private</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="k">public</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="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="k">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">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">-></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="k">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">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">-></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 <code class="docutils literal"><span class="pre">classof</span></code> 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 <code class="docutils literal"><span class="pre">Base</span></code> to a type <code class="docutils literal"><span class="pre">Derived</span></code>, there needs to be a
<code class="docutils literal"><span class="pre">classof</span></code> in <code class="docutils literal"><span class="pre">Derived</span></code> which will accept an object of type <code class="docutils literal"><span class="pre">Base</span></code>.</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"><</span><span class="n">Circle</span><span class="o">></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 <code class="docutils literal"><span class="pre">isa<></span></code> test in this code will eventually boil
down—after template instantiation and some other machinery—to a
check roughly like <code class="docutils literal"><span class="pre">Circle::classof(S)</span></code>. For more information, see
<a class="reference internal" href="#classof-contract"><span>The Contract of classof</span></a>.</p>
<p>The argument to <code class="docutils literal"><span class="pre">classof</span></code> should always be an <em>ancestor</em> class because
the implementation has logic to allow and optimize away
upcasts/up-<code class="docutils literal"><span class="pre">isa<></span></code>‘s automatically. It is as though every class
<code class="docutils literal"><span class="pre">Foo</span></code> automatically has a <code class="docutils literal"><span class="pre">classof</span></code> 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"><</span><span class="k">class</span> <span class="nc">T</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">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"><</span>
<span class="o">::</span><span class="n">std</span><span class="o">::</span><span class="n">is_base_of</span><span class="o"><</span><span class="n">Foo</span><span class="p">,</span> <span class="n">T</span><span class="o">>::</span><span class="n">value</span>
<span class="o">>::</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
<code class="docutils literal"><span class="pre">classof</span></code> into <code class="docutils literal"><span class="pre">Shape</span></code>: all relevant classes derive from <code class="docutils literal"><span class="pre">Shape</span></code>,
and <code class="docutils literal"><span class="pre">Shape</span></code> itself is abstract (has no entry in the <code class="docutils literal"><span class="pre">Kind</span></code> enum),
so this notional inferred <code class="docutils literal"><span class="pre">classof</span></code> 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 “boilerplate”, 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 <code class="docutils literal"><span class="pre">Kind</span></code> check inside <code class="docutils literal"><span class="pre">classof</span></code> 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 <code class="docutils literal"><span class="pre">Kind</span></code>
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 <code class="docutils literal"><span class="pre">classof</span></code> must take them
into account.</li>
</ul>
<p>Say that <code class="docutils literal"><span class="pre">SpecialSquare</span></code> and <code class="docutils literal"><span class="pre">OtherSpecialSquare</span></code> derive
from <code class="docutils literal"><span class="pre">Square</span></code>, and so <code class="docutils literal"><span class="pre">ShapeKind</span></code> 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 <code class="docutils literal"><span class="pre">Square</span></code>, we would need to modify the <code class="docutils literal"><span class="pre">classof</span></code> 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">-></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">-></span><span class="n">getKind</span><span class="p">()</span> <span class="o">>=</span> <span class="n">SK_Square</span> <span class="o">&&</span>
<span class="o">+</span> <span class="n">S</span><span class="o">-></span><span class="n">getKind</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="p">}</span>
</pre></div>
</div>
<p>The reason that we need to test a range like this instead of just equality
is that both <code class="docutils literal"><span class="pre">SpecialSquare</span></code> and <code class="docutils literal"><span class="pre">OtherSpecialSquare</span></code> “is-a”
<code class="docutils literal"><span class="pre">Square</span></code>, and so <code class="docutils literal"><span class="pre">classof</span></code> needs to return <code class="docutils literal"><span class="pre">true</span></code> 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’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 <code class="docutils literal"><span class="pre">classof</span></code>s are
not updated to match the <code class="docutils literal"><span class="pre">Kind</span></code> enum when adding (or removing) classes to
(from) the hierarchy.</p>
<p>Continuing the example above, suppose we add a <code class="docutils literal"><span class="pre">SomewhatSpecialSquare</span></code> as
a subclass of <code class="docutils literal"><span class="pre">Square</span></code>, and update the <code class="docutils literal"><span class="pre">ShapeKind</span></code> 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 <code class="docutils literal"><span class="pre">Square::classof()</span></code>, 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->getKind() == SK_SomewhatSpecialSquare,</span>
<span class="c1">// even though SomewhatSpecialSquare "is a" Square.</span>
<span class="k">return</span> <span class="n">S</span><span class="o">-></span><span class="n">getKind</span><span class="p">()</span> <span class="o">>=</span> <span class="n">SK_Square</span> <span class="o">&&</span>
<span class="n">S</span><span class="o">-></span><span class="n">getKind</span><span class="p">()</span> <span class="o"><=</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 <code class="docutils literal"><span class="pre">SK_LastSquare</span></code>
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">-></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">-></span><span class="n">getKind</span><span class="p">()</span> <span class="o">>=</span> <span class="n">SK_Square</span> <span class="o">&&</span>
<span class="o">+</span> <span class="n">S</span><span class="o">-></span><span class="n">getKind</span><span class="p">()</span> <span class="o"><=</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 <code class="docutils literal"><span class="pre">Square::classof</span></code> 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 <code class="docutils literal"><span class="pre">classof</span></code></a><a class="headerlink" href="#the-contract-of-classof" title="Permalink to this headline">¶</a></h3>
<p>To be more precise, let <code class="docutils literal"><span class="pre">classof</span></code> be inside a class <code class="docutils literal"><span class="pre">C</span></code>. Then the
contract for <code class="docutils literal"><span class="pre">classof</span></code> is “return <code class="docutils literal"><span class="pre">true</span></code> if the dynamic type of the
argument is-a <code class="docutils literal"><span class="pre">C</span></code>”. 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 <code class="docutils literal"><span class="pre">Kind</span></code> enum should have one entry per concrete class, ordered
according to a preorder traversal of the inheritance tree.</li>
<li>The argument to <code class="docutils literal"><span class="pre">classof</span></code> should be a <code class="docutils literal"><span class="pre">const</span> <span class="pre">Base</span> <span class="pre">*</span></code>, where <code class="docutils literal"><span class="pre">Base</span></code>
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 <code class="docutils literal"><span class="pre">isa<></span></code> already handles this case and optimizes it.</li>
<li>For each class in the hierarchy that has no children, implement a
<code class="docutils literal"><span class="pre">classof</span></code> that checks only against its <code class="docutils literal"><span class="pre">Kind</span></code>.</li>
<li>For each class in the hierarchy that has children, implement a
<code class="docutils literal"><span class="pre">classof</span></code> that checks a range of the first child’s <code class="docutils literal"><span class="pre">Kind</span></code> and the
last child’s <code class="docutils literal"><span class="pre">Kind</span></code>.</li>
</ol>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<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> | </li>
<li><a href="index.html">Documentation</a>»</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2003-2014, LLVM Project.
Last updated on 2016-04-18.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.6.
</div>
</body>
</html>
|