[cfe-dev] PATCH: Overloaded function declarations in C++
doug.gregor at gmail.com
Sun Sep 7 22:32:06 PDT 2008
This patch implements support for declaring overloaded functions. For
example, one can now write
void f(int, float);
and have these functions overloaded appropriately. Name lookup for "f"
will refer to the overload set, although at this point you can't actually do
anything with the overload set (that patch is coming later).
All of the semantic analysis required to distinguish between
redeclarations and overloads is implemented. Most of the interesting
logic is in the new Sema::IsOverload, which determines whether two
function declarations are overloaded and in Sema::MergeFunctionDecl,
which produces diagnostics when two function declarations with the
same signature (meaning: they aren't overloads) aren't declaring the
The new OverloadedFunctionDecl class stores a set of overloaded
functions. It's generated by Sema when one declares an overload of an
existing function. Name bindings for functions can refer to
OverloadedFunctionDecl nodes, which will store all of the functions in
that scope with that name.
- An OverloadedFunctionDecl never owns the FunctionDecls it
stores. The scope where those FunctionDecls were declared owns
them. However, who owns the OverloadedFunctionDecl? It's required
for semantic analysis, and will need to be retained for use in
qualified name lookup later, but it doesn't belong with all of the
decls written by users.
- The type of an OverloadedFunctionDecl is a special builtin
"Overload" type, used as a placeholder for OverloadedFunctionDecls.
- When we redeclare a function, we now replace the existing name
binding for that function with the new declaration. This way, there
will only be one name binding for a given function in a given scope
at a time, rather than one for each (re)declaration of that
function. (This is important for function overloading, because we
don't want redeclarations to look like overloads). In this patch,
all of the function declarations and redeclarations show up as they
always have, the AST will contain references to whichever function
declaration was active that the time that part of the AST was
generated [*], but we don't end up with multiple bindings to the
same function in the same scope.
- When we overload a function, we replace the existing name binding
(which will point to a FunctionDecl) with an OverloadedFunctionDecl
that contains the original FunctionDecl and the new FunctionDecl.
- In C++ mode, we now create a FunctionTypeProto for "void f()" rather
than a FunctionTypeNoProto.
Comments greatly appreciated.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 30332 bytes
Desc: not available
More information about the cfe-dev