[cfe-dev] PATCH: C++ Function Overloading (v2)

Argiris Kirtzidis akyrtzi at gmail.com
Wed Oct 22 11:53:25 PDT 2008

Doug Gregor wrote:
>> So what is the advantage of using
>> - CallExpr -> DeclRefExpr -> OverloadedFunctionDecl (unique for this
>> expression)
>> instead of
>> - CallExpr -> OverloadedFunctionExpr
>> ?
>> The second avoids the ownership issues of the first (plus it's a bit more
>> compact).
> The advantage of the first is that name lookup has something it can
> return when it finds a set of overloaded functions. Name lookup is
> going to find a Decl of some sort, and when that Decl is really a set
> of overloaded function declarations, we need a Decl to represent it.
> Hence, we have OverloadedFunctionDecl to refer to those declarations.

Ok, I think I see the main usefulness of OverloadedFunctionDecl.
LookupDecl returns a Decl; the reasoning is to keep it that way and deal 
with multiple overloaded FunctionDecls by having LookupDecl return a 
single OverloadedFunctionDecl. The callers of LookupDecl can still 
reason of name lookup as returning a single Decl.
While there's a bit of a cost involved, it certainly makes things cleaner.

About the current implementation:
OverloadedFunctionDecl is created in "anticipation" of a possible name 
lookup and not as the result of "executing" a name lookup (and it 
doesn't seem to have an owner). Isn't it better to have 
OverloadedFunctionDecl be created by LookupDecl and have it represent 
the result of a particular name lookup search ?

e.g: as in the using directive example:

namespace A {
 void f(int); #1

void f(char); #2

void g() {
 using namespace A; #3

With the current implementation, a OverloadedFunctionDecl should be 
created when #3 is encountered, just in case there's a lookup for 'f' 
later on. Then when 'g()' is exited, the OverloadedFunctionDecl should 
be discarded and the FunctionDecl be made to take its place.
If the OverloadedFunctionDecl is created only when needed (when there's 
a name lookup of 'f' involved) no OverloadedFunctionDecl will be created 
in this case (which is the optimal thing to do).

Another advantage is that using this way, the ownership issue of 
OverloadedFunctionDecl can be handled in a clean way, like this:
-OverloadedFunctionDecls returned by LookupDecl are newly created 
objects not owned by anyone. The caller creates a 
OverloadedFunctionDeclRefExpr expression that holds and owns this 
specific OverloadedFunctionDecl object.
-OverloadedFunctionDeclRefExpr is a subclass of DeclRefExpr and the main 
difference is that it owns the referenced Decl.
-When the overloads get resolved (in non-template code), 
OverloadedFunctionDeclRefExpr is removed and destroyed, which in turn 
destroys the OverloadedFunctionDecl that it references.
-For templates, OverloadedFunctionDeclRefExpr remain in the AST.

What do you think ?


More information about the cfe-dev mailing list