[cfe-dev] PATCH: Cleanup function redeclaration representations

Doug Gregor doug.gregor at gmail.com
Thu Apr 24 16:58:25 PDT 2008


On Thu, Apr 24, 2008 at 4:36 PM, Argiris Kirtzidis <akyrtzi at gmail.com> wrote:
> > How about having a merge more like #1 <-> #2 :
> >
> > (1)  -#2 merges default args from #1
> > (2)  -#2 is checked for error diagnostics about default args
> > (3)  -#1 gets default args from #2        \ (4)
> -(#1)->setNextDeclaration(#2)      ----  (maybe 3,4 are performed in
> addRedeclaration method)
> > (5)  -#2 is returned by the parser but only #1 is visible in scope, thus
> all uses of the function will point to the same FunctionDecl node (#1)
> >
> > In the end, both #1 and #2 contain all of the information that its
> corresponding declarations contain.
> > Merged default arguments become a part of both declarations.
> > Name lookup returns #1, which has all the necessary semantic information
> about default args.
> >
> > Will this work or am I missing something ?

It will work. We'll have lost some of the information regarding what
#1 actually declared (e.g., the default arguments it actually
expressed), but we can probably encode that through other means.

>  Another thing I forgot to mention is that I believe that a name should be
> associated with the declaration that firstly introduces it in the source,
>  not the latest redeclaration. Do you find this reasonable or does it have
> disadvantages ?

Ah, this is the philosophical point that's pushing us toward different
solutions. I think that the name should be associated with the most
recent redeclaration in the source, because that declaration (1) is
closest to the user's call, and (2) has---from the language
perspective-all of the information accumulated from the previous
declarations. For example, here's a test case for default arguments:

  void f(int i);
  void f(int i = 0); // expected-error {{previous definition is here}}
  void f(int i = 17); // expected-error {{redefinition of default argument}}

Should the "previous definition is here" point to the first or the
second declaration of "f"? In general, we typically refer to the
previous declaration or previous definition of something (function,
variable, etc.), and it seems natural to me that that would point to
the previous declaration (or definition) in the source, rather than
the first declaration.

Now, one point to consider is that functions aren't often declared
multiple times, so a lot of these examples are off in the land of
corner cases. Most functions are declared either once or twice,
depending on whether the user has decided to separate the declaration
from the definition. I don't know where that leads us :)

  - Doug



More information about the cfe-dev mailing list