Detect if a CXXConstructExpr is implicit

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

Detect if a CXXConstructExpr is implicit

Bakhvalov, Denis via cfe-dev
In the following code:

https://godbolt.org/z/czpHOf

classA{
public:
    A();
    A(A const&a);
    A(inti);
};

voidf(A a);

voidg() {
   A a;
   f(a); // 1
   f(2); // 2
}

The generated AST contains nodes about object construction that are
implicit (the copy constructor for 1, the conversion then the copy
constructor for 2).

     |-CallExpr <line:12:5, col:8> 'void'
     | |-ImplicitCastExpr <col:5> 'void (*)(A)' <FunctionToPointerDecay>
     | | `-DeclRefExpr <col:5> 'void (A)' lvalue Function 0x55c42589b208
'f' 'void (A)'
     | `-CXXConstructExpr <col:7> 'A' 'void (const A &)'
     |   `-ImplicitCastExpr <col:7> 'const A' lvalue <NoOp>
     |     `-DeclRefExpr <col:7> 'A' lvalue Var 0x55c42589b3c8 'a' 'A'
     `-ExprWithCleanups <line:13:5, col:8> 'void'
       `-CallExpr <col:5, col:8> 'void'
         |-ImplicitCastExpr <col:5> 'void (*)(A)' <FunctionToPointerDecay>
         | `-DeclRefExpr <col:5> 'void (A)' lvalue Function
0x55c42589b208 'f' 'void (A)'
         `-CXXConstructExpr <col:7> 'A' 'void (const A &)' elidable
           `-MaterializeTemporaryExpr <col:7> 'const A' lvalue
             `-ImplicitCastExpr <col:7> 'const A' <NoOp>
               `-ImplicitCastExpr <col:7> 'A' <ConstructorConversion>
                 `-CXXConstructExpr <col:7> 'A' 'void (int)'
                   `-IntegerLiteral <col:7> 'int' 2

Do you know if there is a way to detect that these constructions are
implicit (as opposed to constructions written as the type name followed
by initialization?)

Thank you!

---
Loïc Joly



_______________________________________________
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: Detect if a CXXConstructExpr is implicit

Bakhvalov, Denis via cfe-dev
If you look at the AST for the record decl, there is a bunch of flags in the DefinitionData of interest:
|-CXXRecordDecl <line:1:1, line:6:1> line:1:7 referenced class A
|definition
| |-DefinitionData empty standard_layout has_user_declared_ctor
| |can_const_default_init
| | |-DefaultConstructor exists non_trivial user_provided
| | |defaulted_is_constexpr -CopyConstructor non_trivial user_declared
| | |has_const_param implicit_has_const_param -MoveConstructor
| | |-CopyAssignment trivial has_const_param needs_implicit
| | |implicit_has_const_param -MoveAssignment

See "user_provided" and "user_declared".  I'm not sure what the difference between those two is however.

-----Original Message-----
From: cfe-dev [mailto:[hidden email]] On Behalf Of Loïc Joly via cfe-dev
Sent: Tuesday, February 12, 2019 8:44 AM
To: [hidden email]
Subject: [cfe-dev] Detect if a CXXConstructExpr is implicit

In the following code:

https://godbolt.org/z/czpHOf

classA{
public:
    A();
    A(A const&a);
    A(inti);
};

voidf(A a);

voidg() {
   A a;
   f(a); // 1
   f(2); // 2
}

The generated AST contains nodes about object construction that are implicit (the copy constructor for 1, the conversion then the copy constructor for 2).

     |-CallExpr <line:12:5, col:8> 'void'
     | |-ImplicitCastExpr <col:5> 'void (*)(A)' <FunctionToPointerDecay>
     | | `-DeclRefExpr <col:5> 'void (A)' lvalue Function 0x55c42589b208 'f' 'void (A)'
     | `-CXXConstructExpr <col:7> 'A' 'void (const A &)'
     |   `-ImplicitCastExpr <col:7> 'const A' lvalue <NoOp>
     |     `-DeclRefExpr <col:7> 'A' lvalue Var 0x55c42589b3c8 'a' 'A'
     `-ExprWithCleanups <line:13:5, col:8> 'void'
       `-CallExpr <col:5, col:8> 'void'
         |-ImplicitCastExpr <col:5> 'void (*)(A)' <FunctionToPointerDecay>
         | `-DeclRefExpr <col:5> 'void (A)' lvalue Function
0x55c42589b208 'f' 'void (A)'
         `-CXXConstructExpr <col:7> 'A' 'void (const A &)' elidable
           `-MaterializeTemporaryExpr <col:7> 'const A' lvalue
             `-ImplicitCastExpr <col:7> 'const A' <NoOp>
               `-ImplicitCastExpr <col:7> 'A' <ConstructorConversion>
                 `-CXXConstructExpr <col:7> 'A' 'void (int)'
                   `-IntegerLiteral <col:7> 'int' 2

Do you know if there is a way to detect that these constructions are implicit (as opposed to constructions written as the type name followed by initialization?)

Thank you!

---
Loïc Joly



_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
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: Detect if a CXXConstructExpr is implicit

Bakhvalov, Denis via cfe-dev
Le 12/02/2019 à 17:50, Keane, Erich a écrit :

> If you look at the AST for the record decl, there is a bunch of flags in the DefinitionData of interest:
> |-CXXRecordDecl <line:1:1, line:6:1> line:1:7 referenced class A
> |definition
> | |-DefinitionData empty standard_layout has_user_declared_ctor
> | |can_const_default_init
> | | |-DefaultConstructor exists non_trivial user_provided
> | | |defaulted_is_constexpr -CopyConstructor non_trivial user_declared
> | | |has_const_param implicit_has_const_param -MoveConstructor
> | | |-CopyAssignment trivial has_const_param needs_implicit
> | | |implicit_has_const_param -MoveAssignment
>
> See "user_provided" and "user_declared".  I'm not sure what the difference between those two is however.

Unless I'm mistaken, those notions apply to CXXContructorDecl, not to
CXXConstructExpr. A user declared copy constructor may or may not be
called implicitely.


---
Loïc Joly



_______________________________________________
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: Detect if a CXXConstructExpr is implicit

Bakhvalov, Denis via cfe-dev
Ah, I see.  I got lost in the pronoun game :)  

None that I'm aware of, and I don't see anything promising here: https://clang.llvm.org/doxygen/classclang_1_1CXXConstructExpr.html

Perhaps someone with more knowledge of this can elaborate better.

-----Original Message-----
From: Loïc Joly [mailto:[hidden email]]
Sent: Tuesday, February 12, 2019 9:18 AM
To: Keane, Erich <[hidden email]>; Clang Dev <[hidden email]>
Subject: Re: [cfe-dev] Detect if a CXXConstructExpr is implicit

Le 12/02/2019 à 17:50, Keane, Erich a écrit :

> If you look at the AST for the record decl, there is a bunch of flags in the DefinitionData of interest:
> |-CXXRecordDecl <line:1:1, line:6:1> line:1:7 referenced class A
> |definition
> | |-DefinitionData empty standard_layout has_user_declared_ctor
> | |can_const_default_init
> | | |-DefaultConstructor exists non_trivial user_provided
> | | |defaulted_is_constexpr -CopyConstructor non_trivial user_declared
> | | |has_const_param implicit_has_const_param -MoveConstructor
> | | |-CopyAssignment trivial has_const_param needs_implicit
> | | |implicit_has_const_param -MoveAssignment
>
> See "user_provided" and "user_declared".  I'm not sure what the difference between those two is however.

Unless I'm mistaken, those notions apply to CXXContructorDecl, not to CXXConstructExpr. A user declared copy constructor may or may not be called implicitely.


---
Loïc Joly



_______________________________________________
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: Detect if a CXXConstructExpr is implicit

Bakhvalov, Denis via cfe-dev
In reply to this post by Bakhvalov, Denis via cfe-dev
On 12/02/2019 16:44, Loïc Joly via cfe-dev wrote:

> The generated AST contains nodes about object construction that are
> implicit (the copy constructor for 1, the conversion then the copy
> constructor for 2).

If it's useful, I have plans to upstream changes such that you can print
and AST-match on code like this while eliding invisible AST nodes. See:

  http://ec2-18-191-7-3.us-east-2.compute.amazonaws.com:10240/z/PlPx1d

Thanks,

Stephen.

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