[cfe-dev] Using isa & co with custom class hierarchy

Jan Bierbaum s3306700 at inf.tu-dresden.de
Wed Aug 25 11:22:06 PDT 2010


Hi Victor!


Thanks for your answer.

Victor Zverovich meinte am 25.08.2010 18:33:
> The problem is that B::classof(const A *) always return true in your
> implementation, so llvm::isa<B>(a)returns true as well.

Yeah, but that one is intended. Every instance of B "is a" instance of A
due to inheritance. I just modeled this in the 'classof' method.

But even leaving this aside: There is no 'A::classof(const B *)' yet
'isa<B>(a)' returns 'true'. I added this one specifically to make sure I
did not mix up something with which class must define which as the
parameter to 'classof'.


> You can have a look at how classof is implemented in clang's AST.

I actually did. First the short introduction LLVM gives [1] states

| To add support for these templates, you simply need to add classof
| static methods to the class you are interested casting to.

That's how I came up with the scheme at all. Then for example
'CXXRecordDecl' [2] define the following (besides others):

| static bool classof(const CXXRecordDecl *D) { return true; }
| static bool classof(const ClassTemplateSpecializationDecl *D) {
|   return true;
| }

I take this to mean that every 'CXXRecordDecl' is a 'CXXRecordDecl' -
well obviously - and every 'ClassTemplateSpecializationDecl' is a
'CXXRecordDecl'.

Hmm, looks I did mix up the directions, but my example should
nevertheless also work only one way - every 'A' being a 'B' but not vice
versa. So 'isa<A>(b)' should report false/0.


[1] http://llvm.org/docs/ProgrammersManual.html#isa
[2] http://clang.llvm.org/doxygen/DeclCXX_8h_source.html#l01051



Regards, Jan.



More information about the cfe-dev mailing list