X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FHowToSetUpLLVMStyleRTTI.rst;h=38929948590916ecc1198980e012abe21631295e;hb=0d71b6afcb5d9e6e1ef44424635d3462362b4055;hp=e0f865a141c7edef20e9a0ca4ab9279a4400f412;hpb=229aa6d23b7dc70e3e7cc81d385cf815ac478a80;p=oota-llvm.git diff --git a/docs/HowToSetUpLLVMStyleRTTI.rst b/docs/HowToSetUpLLVMStyleRTTI.rst index e0f865a141c..38929948590 100644 --- a/docs/HowToSetUpLLVMStyleRTTI.rst +++ b/docs/HowToSetUpLLVMStyleRTTI.rst @@ -40,14 +40,14 @@ RTTI for this class hierarchy: double SideLength; public: Square(double S) : SideLength(S) {} - double computeArea() /* override */; + double computeArea() override; }; class Circle : public Shape { double Radius; public: Circle(double R) : Radius(R) {} - double computeArea() /* override */; + double computeArea() override; }; The most basic working setup for LLVM-style RTTI requires the following @@ -135,7 +135,7 @@ steps: public: - Square(double S) : SideLength(S) {} + Square(double S) : Shape(SK_Square), SideLength(S) {} - double computeArea() /* override */; + double computeArea() override; }; class Circle : public Shape { @@ -143,7 +143,7 @@ steps: public: - Circle(double R) : Radius(R) {} + Circle(double R) : Shape(SK_Circle), Radius(R) {} - double computeArea() /* override */; + double computeArea() override; }; #. Finally, you need to inform LLVM's RTTI templates how to dynamically @@ -175,7 +175,7 @@ steps: double SideLength; public: Square(double S) : Shape(SK_Square), SideLength(S) {} - double computeArea() /* override */; + double computeArea() override; + + static bool classof(const Shape *S) { + return S->getKind() == SK_Square; @@ -186,7 +186,7 @@ steps: double Radius; public: Circle(double R) : Shape(SK_Circle), Radius(R) {} - double computeArea() /* override */; + double computeArea() override; + + static bool classof(const Shape *S) { + return S->getKind() == SK_Circle; @@ -223,8 +223,8 @@ steps: [...] template static bool classof(const T *, - ::llvm::enable_if_c< - ::llvm::is_base_of::value + ::std::enable_if< + ::std::is_base_of::value >::type* = 0) { return true; } [...] }; @@ -377,6 +377,20 @@ contract for ``classof`` is "return ``true`` if the dynamic type of the argument is-a ``C``". As long as your implementation fulfills this contract, you can tweak and optimize it as much as you want. +For example, LLVM-style RTTI can work fine in the presence of +multiple-inheritance by defining an appropriate ``classof``. +An example of this in practice is +`Decl `_ vs. +`DeclContext `_ +inside Clang. +The ``Decl`` hierarchy is done very similarly to the example setup +demonstrated in this tutorial. +The key part is how to then incorporate ``DeclContext``: all that is needed +is in ``bool DeclContext::classof(const Decl *)``, which asks the question +"Given a ``Decl``, how can I determine if it is-a ``DeclContext``?". +It answers this with a simple switch over the set of ``Decl`` "kinds", and +returning true for ones that are known to be ``DeclContext``'s. + .. TODO:: Touch on some of the more advanced features, like ``isa_impl`` and