c++llvmrttillvm-c++-api

Best practice for very large if-else-statement using LLVMs RTTI system


I am currently writing a piece of software that relies on another library, which makes heavy use of LLVMs RTTI system. I cannot change the API of said library and it forces me to implement very large if-else-statements over several types and their sub-types. Usually I would have used at a switch-statement instead, but that is obviously not possible using LLVM's dyn_cast<>().

Below is an example of what I am currently doing, however, this turned out to be a real bottleneck. Are there any better ways of achieving the same, but with less overhead?

Thanks a ton!

 if (const SomeClass *casted = dyn_cast<SomeClass>(something))
 {
     ...
 }

 else if (const anotherClass *casted = dyn_cast<AnotherClass>(something))
 {
     ...
 }

 ...

 else
 {
     ...
 }

Solution

  • You can switch(something->getType()->getTypeId()) sometimes, but some dyn_cast<>() calls will probably be unavoidable, e.g. for struct types you define.