How to tell whether two TypeDecls are implicit convertible to each other?

Hi,

I’m trying to contribute the C++ cast conversion tool for clang-tools-extra and I was wondering whether there’s any way to tell whether two types are implicitly convertible to each other. The reason is because we want to convert the C-style cast to static_cast if it’s implicitly convertible, and otherwise we fall-through to use reinterpret_cast.

In order to safely change C-style casts to static_cast, we want to be able to tell whether two types are convertible to each other. Even if we hardcode implicit convertibility with built-in types, In the case of CXXRecordDecls it may be especially difficult because of user-defined cast operators within the class/struct.

It might be that I overlooked some part of the documentation on this, but it seems like a difficult problem. It might be that clang as a compiler has the logic somewhere for deciding implicit convertibility, and if so could anyone point me to the source? If not, I was thinking of transforming all C-style casts into static_casts on the first pass, and upon failure on building the AST, transforming broken static_casts into reinterpret_casts on the second pass. I’m not sure if this idea will work.

Any suggestions would be greatly appreciated.

Best,
Ray

Hi Ray,

Hi Miklos,

Thank you so much! I was not aware of the cast kind enums that were available - this helps me a ton.

  • Ray