[cfe-commits] r80446 - in /cfe/trunk: lib/AST/DeclCXX.cpp lib/Sema/SemaTemplateInstantiateDecl.cpp test/SemaTemplate/instantiate-member-initializers.cpp

Douglas Gregor dgregor at apple.com
Tue Sep 1 14:51:53 PDT 2009


On Sep 1, 2009, at 8:00 AM, Fariborz Jahanian wrote:
> On Aug 31, 2009, at 11:41 PM, Eli Friedman wrote:
>
>> On Sun, Aug 30, 2009 at 11:15 AM, Fariborz Jahanian<fjahanian at apple.com
>>> wrote:
>>>
>>> On Aug 29, 2009, at 5:40 PM, Eli Friedman wrote:
>>>
>>>> On Sat, Aug 29, 2009 at 5:35 PM, Fariborz Jahanian<fjahanian at apple.com
>>>>>
>>>> wrote:
>>>>>
>>>> We have to build the node because otherwise, there isn't anywhere  
>>>> in
>>>> the AST for the initializers to live.  In the dependent case, yes,
>>>> we
>>>
>>> Of course we need to build the initializer. Focus of the question
>>> is when
>>> we have dependent type initializers.
>>>>
>>>> could skip all analysis; if we did that, though, we would miss a
>>>> class
>>>> of errors which could be detected earlier, specifically the case
>>>> where
>>>> a member that requires an initializer doesn't have one.
>>>
>>> We don't need to skip the analysis. We just don't build
>>> BaseOrMemberInitializers if
>>> there is even one dependent case.
>>
>> Where exactly are you proposing the initializers should be?  As far  
>> as
>> I can tell, we don't keep them in any other place in the AST.
>
> We keep it where it is. We just don't build it when processing
> templates. We build it
> when instantiating templates.

Let me try to clarify the model a bit, because it looks like there's  
some confusion about how templates get stored and then instantiated.  
Here's a simple example that involves member initializers:

   template<typename T>
   struct X {
     X() : value(0) { }
     T value;
   };

When we parse the constructor for X, we build a CXXConstructorDecl for  
it. Then we see the value(0), for which we construct a  
CXXBaseOrMemberInitializer. We can't actually type-check that  
CXXBaseOrMemberInitializer, since 'value' has a dependent type, but we  
store it in the BaseOrMemberInitializers list for the class template X  
(in the CXXRecordDecl).

When we instantiate class template 'X', template instantiation  
substitutes the template arguments for their corresponding template  
parameters in each of the CXXBaseOrMemberInitializers stored in the  
class template's BaseOrMemberInitializers list. The resulting  
CXXBaseOrMemberInitializers are then type-checked so they will be  
added to the instantiation's BaseOrMemberInitializers list.

By storing everything in the AST, we never have to go back to re-parse  
code at instantiation time. We just substitute into the existing  
(dependent-typed) AST nodes to create new AST nodes.

   - Doug



More information about the cfe-commits mailing list