[cfe-commits] [PATCH (need approval)] Add instantiations of out-of-line member variable declarations to DeclContext

Peter Collingbourne peter at pcc.me.uk
Thu Jul 15 18:34:34 PDT 2010

On Tue, Jul 06, 2010 at 11:25:15AM -0700, Douglas Gregor wrote:
> On Jul 4, 2010, at 7:02 AM, Peter Collingbourne wrote:
> > Currently instantiations of out-of-line member variable declarations
> > are created and added to the redeclaration list but are not added to
> > any DeclContext.  This is a problem for any traversal of the AST which
> > depends on seeing identical sets of AST nodes via the redeclaration
> > lists and via DeclContexts.
> The current behavior is actually intentional. Template instantiations aren't meant to show up in a normal AST traversal (e.g., in decls_begin()/decls_end() when iterating over a DeclContext) because they weren't written in the source. For what kind of application is this behavior causing a problem?

Hi Doug,

I would agree we shouldn't be inserting these nodes where they
shouldn't belong.  I proposed adding the nodes to the DeclContext
as it seemed the easiest thing to do at the time, and didn't seem
to break anything (though I can imagine it could also affect other
external users).

This application converts the Clang AST to a form of AST used by
another compiler framework.  We need to see every redeclaration of a
particular declaration somewhere in a tree-oriented traversal of the
AST (including DeclContexts and implicit instantiations in template
declarations' specialisation lists), so that we know where to place
each redeclaration in the AST.  We could special case instantiations
of out-of-line member variable declarations, but this could become
error prone.

I would imagine the same issue would affect any other client that
needs to visit every (re)declaration in the translation unit (including
implicit instantiations) exactly once, since the application would
need to be very careful to avoid double visiting or not visiting at
all the instantiated variable declarations.

One could consider this a consistency issue -- namely, we do not store
a specialisation list for out-of-line member variable declaration
templates, as we seem to do for every other type of template
(except instantiations of out-of-line member function definition
templates which as I understand it are merged into the class template

One fix could be to add a specialisation list to VarDecl.
An alternative fix could be to merge the variable declarations into
the class template instantation, for example by adding a flag to
the VarDecl indicating whether it has been defined elsewhere in the
translation unit.

Please let me know what you think.


More information about the cfe-commits mailing list