[cfe-dev] Who owns declarations?

Douglas Gregor dgregor at apple.com
Mon Dec 22 11:11:41 PST 2008


On Dec 22, 2008, at 8:05 PM, Sebastian Redl wrote:

> Douglas Gregor wrote:
>>
>> On Dec 22, 2008, at 7:38 PM, Sebastian Redl wrote:
>>
>>>
>>> By the way, why does CXXConditionalDeclExpr derive from DeclRefExpr?
>>
>> I believe it's because IfStmt's condition is an Expr*, so  
>> CXXConditionalDeclExpr is an Expr that also declares a variable.  
>> The derivation from DeclRefExpr allows the rest of semantic  
>> analysis (that knows how to treat a DeclRefExpr as a boolean  
>> expression) to deal with the declaration.
>>
>>> That's counter-intuitive from several viewpoints.
>>
>>
>> How so?
>>
> A CXXConditionalDeclExpr declares a variable, it doesn't reference it.

It does both. We're talking about something like

	if (int x = foo()) {
	}

where everything in the parentheses is the CXXConditionalDeclExpr. It  
declares the variable x, and then acts as a reference to the variable x.

> That's the most important difference between the two, and it doesn't  
> lend itself to using derivation - note that CXXConditionalDeclExpr  
> doesn't have serialization implemented, precisely because it owns  
> the Decl and DeclRefExpr doesn't, which causes confusion.


Perhaps what we really need is not the CXXConditionalDeclExpr class,  
but the ability for DeclRefExpr to own its declaration (based on some  
flag, which could just be the expression class itself). That would  
also be useful, e.g., when we have to build an OverloadedFunctionDecl  
on-the-fly that will be owned by a DeclRefExpr, say, to retain the  
results of name lookup in a template.

	- Doug



More information about the cfe-dev mailing list