[cfe-dev] Reference-counted statements
Douglas Gregor
dgregor at apple.com
Fri Aug 7 10:48:35 PDT 2009
On Aug 7, 2009, at 10:43 AM, Daniel Dunbar wrote:
> Hi Doug,
>
> I'd like to clarify one point:
>
> On Fri, Aug 7, 2009 at 9:26 AM, Douglas Gregor<dgregor at apple.com>
> wrote:
>> Most AST clients will still use simple Stmt or Expr pointers and
>> will not
>> need to increase or decrease the reference counts. In those few
>> places where
>> we are explicitly sharing statements or expressions (e.g., as part
>> of a tree
>> transform), we will call the statement's Retain operation to
>> increase the
>> reference count. In the attached patch, I've introduced Retain
>> calls in a
>> few places to illustrate the idea:
>
> I think this amounts to an extension of our "strong" ownership model,
> right?
Yes, that is the intent.
> The precise rule for when clients need to handle the reference
> count themselves is when passing an owned expression into another
> context (e.g., a constructor) expecting to resolve a strong reference
> itself. I haven't thought it through, but this may even be something
> we could eventually enforce via the static type system.
Perhaps we could describe it via the static type system. The pattern
within Sema is that we allocate a new statement and place it into a
Sema::OwnedStmtResult, using smart pointers to hold ownership of the
statement and eventually transferring ownership from the smart pointer
into an AST (via take()/release()). The pattern for using Retain() is
that you've gotten a statement from the AST and you also want to own
it (to eventually put into a new AST, for example), so you call Retain
() and build a new AST or put it in a Sema::OwnedStmtResult.
- Doug
More information about the cfe-dev
mailing list