[cfe-dev] TemplateTypeParmDecl declaration context

Douglas Gregor dgregor at apple.com
Tue Jun 29 07:50:29 PDT 2010


On Jun 18, 2010, at 10:34 AM, Abramo Bagnara wrote:

> 
> In my simple mind I'd have guessed that declaration context for
> TemplateTypeParmDecl was the containing template, but:
> 
> 1) in current clang this is not true in both (as neither of two template
> container nodes is a decl context):
> 
> template <typename T>
> T f(T x) {
> }
> 
> template <typename T>
> struct s {
>  static int v;
> };

The issue with template parameters is that they are created before we've created the AST node that would normally contain them, e.g., we need to build the TemplateTypeParmDecl for each 'T' in this example before we create the FunctionTemplateDecl or ClassTemplateDecl nodes. The same issue occurs with ParmVarDecls; there, we create the ParmVarDecls with the translation unit as their context. Then, when we add those ParmVarDecls to a FunctionDecl, we change the declaration context. I think it makes sense for us to do the same thing with template parameters (all three kinds).

> 2) here there is no container to use as declaration context
> 
> template <typename T>
> int s<T>::v;

This is more tricky, since an out-of-line static data member is not really a context that holds other declarations: it's just a reflection of something that exists within the class template itself. If anything really owns the template parameter, perhaps it's s<T>?

> I'm missing something about concept of declaration context?


I don't think so. (1) is a (probably benign) bug, while (2) is a strange corner case we haven't worked out.

	- Doug



More information about the cfe-dev mailing list