[cfe-dev] Templates (again)

Douglas Gregor dgregor at apple.com
Tue Dec 23 15:01:22 PST 2008


Please CC cfe-dev on these discussions. (Reply All)

On Dec 23, 2008, at 5:11 PM, Andrew Sutton wrote:

>
> I think there are only 4 template-derived subclasses (5, when  
> concept maps come along). The trick is that FunctionTemplate covers  
> all of the various subclasses of FunctionDecl under a single  
> TemplateDecl subclass.
>
> I think my way is simpler :)
> We have the TemplateDecl, which has a ScopedDecl of some sort.  
> Clients that deal with templates abstractly can use TemplateDecl.  
> Then, there are subclasses of TemplateDecl that put more  
> requirements on that underlying declaration and add extra fields  
> (specializations, instantiations, partial specializations, etc.).
>
> To summarize, we're talking about something like this:
>
> class TemplateDecl : ScopedDecl // (?)
> {
>   ScopedDecl* decl;
>   // One of FunctionDecl, ClassDecl, etc.
>   // but not one of *TemplateDecl?
> };
>
> class ClassTemplateDecl : TemplateDecl { };
> class FunctionTemplateDecl : TemplateDecl { };
> // template alias, template template parameter, etc.
>
> And we'd generally be creating derived decl nodes during parsing. Is  
> that about right?

Yes.

>
> This makes me wonder what the AST would look like for something like  
> this:
>
> template <typename T>
> struct S
> {
>   template <typename U> void f(U u);
> };
>
> template <typename T>
> template <typenameU>
> void S<T>::f(U u)
> { }
>
> It looks like we'd have to a) defer the creation of the  
> ClassTemplateDecl until we see S<T> or b) I'm misintepreting what  
> you're saying and we'd create a TemplateDecl for each template<...>,  
> and then create a ClassTemplateDecl when we S<T> and have it  
> reference the outer-most TemplateDecl.


So, walking through this: when we get the ActOnTag callback, we create  
the TemplateDecl and RecordDecl for class template 'S' (basically,  
when we see the '{' after the 'S').

When we get the ActOnDeclarator callback for 'f', we build the  
FunctionDecl and TemplateDecl for that member template.

For the out-of-line member definition, we'll get an ActOnDeclarator  
call with a declarator 'f' in scope S<T>. We'll then create a  
TemplateDecl+FunctionDecl for that out-of-line definition.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20081223/e26318f9/attachment.html>


More information about the cfe-dev mailing list