[cfe-dev] C++ Constructors & Destructors in the AST

Anders Carlsson andersca at mac.com
Tue Apr 21 12:45:16 PDT 2009


On Apr 20, 2009, at 4:40 PM, Chris Lattner wrote:

>
> If you have indirect initialization and the copy constructor has not
> been elided, then we get a temporary:
>
> { T x = 4;
> ...
> }
>
> (VarDecl 'x' Type=T,
>    Init = CXXConstruct("x", "T::T(const T&)",
>                 CXXConstruct("somecxxtempdecl", "T::T(int)", 4))
> (CXXDestroy "somecxxtempdecl")
> ...
> (CXXDestroy "x")
>

I think that it's good to always have copy ctors represented in the  
AST, and then have an "canBeElided" bit on either the temp decl or the  
CXXConstruct call.

Here's another example that was on the board but that wasn't written  
down:

{ const T& x = 4;
...
}

(VarDecl 'x' Type=const T&,
   Init = CXXConstruct("somecxxtempdecl", "T::T(int)", 4))
...
(CXXDestroy("somecxxtempdecl")

> Still to discuss after the basic pieces are done:
> 1. Global variable initialization, where to CXXTempVarDecls go?  Just
> cram them into the containing declcontext?  Should sema explicitly
> generate the "translation unit constructor" function or not?

Another option would be to not insert the CXXTempVarDecls in a  
DeclContext, since we know that they must belong to a single  
CXXConstructDecl. Doug pointed out that it might be good to be able to  
iterate over temp decls though.

> 2. Conditional liveness of temporaries, how to we represent the
> condition to destroy a temp.

3. How should we represent compound literal expressions and init list  
expressions? For example:

{ T xs[] = { 1, 2 }; }

or even

(T[2]){1, 2 };

Both these have copy ctors that can be elided, but what's the VarDecl  
they go to?

Anders




More information about the cfe-dev mailing list