[cfe-dev] The lexical DeclContext of class template instantiations.

Enea Zaffanella zaffanella at cs.unipr.it
Sun Jul 8 10:19:58 PDT 2012


Hello.

I would like to hear some informed opinions on the following, which 
seems to be an AST quality issue.

When parsing the following file:

$ cat lex_dc.cc
template <typename T>
struct Outer {
   template <typename U> static void InnerFun(T, U) {}
   template <typename U> struct InnerRecord { T* t; U u; };
};

struct S {
   static void bar() { Outer<S>::InnerFun<int>(S(), 5); }
   Outer<S>::InnerRecord<int> is;
};


the AST generated by clang contains implicit instantiations for
    Outer<S>::InnerFun<int>
and
    Outer<S>::InnerRecord<int>.

It happens that the function Outer<S>::InnerFun<int> has the same 
semantic and lexical DeclContext, namely struct Outer<S>.

In contrast, the record Outer<S>::InnerRecord<int> has Outer<S> as the 
semantic DeclContext, but it has struct S as the lexical one.

This looks quite odd ... for instance, one of the consequences is that, 
when querying Outer<S>::InnerRecord<int> with method
    bool isOutOfLine()
we would be obtaining a positive answer.

Afaict, the problem is caused by method

QualType
Sema::CheckTemplateIdType(
     TemplateName Name,
     SourceLocation TemplateLoc,
     TemplateArgumentListInfo &TemplateArgs);

around line 2077 of SemaTemplate.cpp, where it does

   ClassTemplate->AddSpecialization(Decl, InsertPos);
   Decl->setLexicalDeclContext(CurContext);

Am I guessing correctly if I say that it should be something like

   ClassTemplate->AddSpecialization(Decl, InsertPos);
   if (ClassTemplate->isOutOfLine())
     Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext());

?

Enea.



More information about the cfe-dev mailing list