[cfe-dev] Ownership of Stmts and Exprs, and destroying them

Chris Lattner clattner at apple.com
Mon Nov 24 14:00:19 PST 2008


On Nov 24, 2008, at 1:57 PM, Sebastian Redl wrote:
>>  The name "Guard" is somewhat strange for this.  How about  
>> "ASTOwner" or "ASTHolder" or something like that?
> I use Guard for most things that guard against early exit from a  
> frame. Or sentry, which is a synonym. I didn't use a name like  
> Holder because the ExprResult/ExprTy/Stmt equivalents aren't  
> actually part of the class - the class is an external addon to guard  
> against early exit, and it needs to be told to watch a variable that  
> you already have.

Ok!

>> +  public:
>> +    enum AstType {
>> +      Stmt,
>> +      Expr
>> +    };
>>
>> Would it make sense to make an ExprAstGuard and StmtAstGuard class
> Yes.
>> (or a template parameterized on ExprResult/StmtResult)?
> Feels wrong.

Ok.

>
>> In other words, make this determination statically instead of  
>> dynamically?
> Yes, it is. I went for the dynamic way because I didn't have any  
> immediately obvious idea of what to template on (and specializing  
> individual members is a hassle, if at all possible), and I didn't  
> want to duplicate the code.
> But I think I should just make it
>
> template <void (Action::*Destroyer)(void*)> class ASTGuard;
> typedef ASTGuard<&Action::DestroyExpr> ExprGuard;
> typedef ASTGuard<&Action::DestroyStmt> StmtGuard;
>
> Then the destructor is
>
> ~ASTGuard() { (Actions.*Destroyer)(Node); }
>
> with Node being a new name for AstElement. If the compiler is smart,  
> this should be equivalent to a direct function call.

Yeah, I'd be very happy with that.

>> +  // RAII guard for freeing SmallVectors of Stmts and Exprs on  
>> early exit
>> +  // in the parser.
>> +  class AstArrayGuard {
>>
>> instead of having a pointer to a smallvector, how about just making  
>> this contain the temporary smallvector?
> I suppose that's possible in the actual cases where I use it. I  
> didn't before, because I wasn't sure where I would put it. And of  
> course, this means making it a template on the smallvector size.

In practice, the size of a small vector is pretty arbitrary.  Would it  
be fine to just fix it to 16 or something like that?

-Chris




More information about the cfe-dev mailing list