[cfe-dev] Ownership of Stmts and Exprs, and destroying them
Chris Lattner
clattner at apple.com
Mon Nov 24 13:33:49 PST 2008
On Nov 23, 2008, at 8:33 AM, Howard Hinnant wrote:
>> friends. That way, the type system enforces our ownership model
>> (transfer into Sema for the parameters of ActOn*, transfer out of
>> Sema
>> on return), and we're safe from early exits.
>
> If it helps, here's a C++03 emulation of the C++0X std::unique_ptr
> (what Doug refers to as move_ptr):
>
> http://home.twcny.rr.com/hinnant/cpp_extensions/unique_ptr_03.html
>
> You can set its "deleter" to call Action::DestroyExpr. It might look
> something like:
>
>
> unique_ptr<ExprTy, CallActionDestroyExpr>
> lhs,
> unique_ptr<ExprTy, CallActionDestroyExpr>
> rhs) {
> BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
> ...
This doesn't work. The problem is that the C++ type system is
insufficiently powerful (!!) to express what we need here. The parser
needs to be independent of any specific actions implementation and AST
form. This is why it needs to call callbacks on Actions to delete
expressions.
Sema, on the other hand, implements these actions but does know the
specific concrete types. It isn't acceptable to encode "Expr" into
the interface used by the parser, that would be a severe layering
violation.
-Chris
More information about the cfe-dev
mailing list