[cfe-dev] RFC: Extra source location information for dependent types

Douglas Gregor dgregor at apple.com
Tue Mar 31 12:16:14 PDT 2009


Template instantiation for class templates and types is starting to  
shape up. One problem I'm seeing now is that we don't retain enough  
source location information in the type system to do a good job with  
instantiation. For example, here's a C++ template metafunction:

   template<typename MetaFun, typename T>
   struct apply1 {
     typedef typename MetaFun::template apply<T>::type type;
   };

We have no source location information for that nested type "type"  
except the location of the declarator. This leads to poor placement of  
the '^' in error messages, e.g.,

test/SemaTemplate/metafun-apply.cpp:25:53: error: 'apply' following  
the 'template' keyword does not refer to a template
   typedef typename MetaFun::template apply<T>::type type;
                                                     ^
The caret is under the declarator ("type"), which is far away from the  
actual error. If this were in non-template code, the error message  
would point to "apply", but we lost that source information when we  
built the uniqued type.

I believe that we have to fix this problem, and to do so we're going  
to need to introduce more source locations into our representation of  
types. Unfortunately, adding source locations to types has some  
serious downsides:

   - The representation of each type gets larger
   - Since source locations will need to be part of each type's  
profile, we'll be creating many more unique types
   - With more unique types, we'll spend more time in the type- 
uniquing process.

For these reasons, I think we need to introduce source locations into  
the type system in a few places as possible. In particular, I suggest  
that source locations are only available in the dependent form of a  
type. Thus, the type used to represent "typename MetaFun::template  
apply<T>::type" will have source locations for the keyword "template",  
the template name "apply", and the trailing type "type" only if the  
types depend somehow on a template parameter. Hence, when we're  
outside of a template we won't run into any of the downsides described  
above.

Comments? Concerns?

	- Doug



More information about the cfe-dev mailing list