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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Vassil Vassilev via cfe-dev
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


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

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

Vassil Vassilev via cfe-dev
Hi Ray,

On Thu, Jun 11, 2020 at 11:59:18PM -0700, Ray Zhang via cfe-dev <[hidden email]> wrote:
> 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.

If you already have access to an ExplicitCastExpr, try using
getCastKind(): CK_BaseToDerived would be when you can safely replace
your C cast with a static_cast, I guess.

Regards,

Miklos

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

signature.asc (201 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

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

Vassil Vassilev via cfe-dev
Hi Miklos,

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

- Ray

On Mon, Jun 15, 2020 at 9:04 AM Miklos Vajna <[hidden email]> wrote:
Hi Ray,

On Thu, Jun 11, 2020 at 11:59:18PM -0700, Ray Zhang via cfe-dev <[hidden email]> wrote:
> 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.

If you already have access to an ExplicitCastExpr, try using
getCastKind(): CK_BaseToDerived would be when you can safely replace
your C cast with a static_cast, I guess.

Regards,

Miklos

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev