Finding Stmt Kind

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

Finding Stmt Kind

Oleg Smolsky via cfe-dev
Hi all,

Is it possible to get the kind of a Stmt object in clang?
The only solution that I know of is using dyn_cast. However, in this solution, I have to check for every kind of Stmt, e.g.
if (CompoundStmt *CS = dyn_cast<CompoundStmt>(d))
  ....
else if (IfStmt *IS = dyn_cast<IfStmt>(d))
  ....

Is there a better solution or trick?

Thanks,
Pardis




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

Re: Finding Stmt Kind

Oleg Smolsky via cfe-dev
Hi Pardis,

Take a look at Stmt::getStmtClass().

18.11.2018 21:20, Pardis Pashakhanloo via cfe-dev пишет:
Hi all,

Is it possible to get the kind of a Stmt object in clang?
The only solution that I know of is using dyn_cast. However, in this solution, I have to check for every kind of Stmt, e.g.
if (CompoundStmt *CS = dyn_cast<CompoundStmt>(d))
  ....
else if (IfStmt *IS = dyn_cast<IfStmt>(d))
  ....

Is there a better solution or trick?

Thanks,
Pardis




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



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

Re: Finding Stmt Kind

Oleg Smolsky via cfe-dev
In reply to this post by Oleg Smolsky via cfe-dev
On Sun, Nov 18, 2018 at 1:20 PM Pardis Pashakhanloo via cfe-dev
<[hidden email]> wrote:

>
> Hi all,
>
> Is it possible to get the kind of a Stmt object in clang?
> The only solution that I know of is using dyn_cast. However, in this solution, I have to check for every kind of Stmt, e.g.
> if (CompoundStmt *CS = dyn_cast<CompoundStmt>(d))
>   ....
> else if (IfStmt *IS = dyn_cast<IfStmt>(d))
>   ....
>
> Is there a better solution or trick?

The usual approach is to use the casting machinery, but you can also
use the statement kind directly by calling Stmt::getStmtClass(),

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

Re: Finding Stmt Kind

Oleg Smolsky via cfe-dev
In reply to this post by Oleg Smolsky via cfe-dev
If your purpose is to make a large switch over the kind of Stmt, i guess
i should also recommend StmtVisitor, which is essentially the same thing
- a huge switch, but it keeps the additional option to specify the
default behavior for intermediate sub-classes of statements without
listing all members of such sub-classes explicitly.

For example, yes, VisitCallExpr would be called for every
CUDAKernelCallExpr unless VisitCUDAKernelCallExpr is defined explicitly.
But you still don't need to explicitly remember that and spell this out
in your code, and you (or people after you) don't need to update your
switch later every time they add a new kind of CallExpr as long as it
truly doesn't require any special behavior.

So it kinda combines the benefits of both the dyn_cast chain approach
and the switch approach, and additionally ensures correct control flow
and scope separation in your code.

On 11/18/18 10:20 AM, Pardis Pashakhanloo via cfe-dev wrote:

> Hi all,
>
> Is it possible to get the kind of a Stmt object in clang?
> The only solution that I know of is using dyn_cast. However, in this
> solution, I have to check for every kind of Stmt, e.g.
> if (CompoundStmt *CS = dyn_cast<CompoundStmt>(d))
>   ....
> else if (IfStmt *IS = dyn_cast<IfStmt>(d))
>   ....
>
> Is there a better solution or trick?
>
> Thanks,
> Pardis
>
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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