[cfe-dev] Adding template default parameter with as typed explicit parameter

Philippe Canal pcanal at fnal.gov
Thu Sep 27 06:47:23 PDT 2012


Hi,

> The CXXRecordDecl has to use the canonical types so we don't try to
> instantiate Point twice if the user writes e.g. "Point<size_t> x;
> Point<unsigned long> y;".

Yes, indeed.  I do not want to change that.

> I suppose it could store non-canonical
> arguments corresponding to the first instantiation as well... but we
> haven't had any use for that in the past.

In our use case there wouldn't be anything magic about the first
instantiation.

> AFAIK no... I'm pretty sure Sema never computes it

Yes.  However, in the case of auto describing data persistency, one
would want to preserve not only the type as written by the user
(Point<size_t> in my example) but also the default value of the template.
This would help in schema evolution (i.e. being able to read the old
data with a newer schema/library where the default value of the template
may have changed).

 From a QualType instanceType (gotten from the variable declaration for example)
which represent Point<size_t>

    const clang::TemplateSpecializationType* TST
       = llvm::dyn_cast<const clang::TemplateSpecializationType>(instanceType.getTypePtr());

I can get the arguments of the template as types (size_t).

I think we need to keep that.   What I would also like to have (and am
offering to implement but would need directions on where best to do it
in the code), is something akin to

   TST->getTypeWithDefaultExpanded();

which would return a QualType/TemplateSpecializationType (but not a
CXXRecordDecl) representing
  
   Point<size_t, Coord<size_t> >'

which would still point to the CXXRecordDecl for Point<long, Coord<long> >.

Thanks,
Philippe.


On 9/26/12 9:57 PM, Eli Friedman wrote:
> On Wed, Sep 26, 2012 at 6:10 AM, Philippe Canal <pcanal at fnal.gov> wrote:
>> Hi,
>>
>> With the class template:
>>
>>     template <typename T> class Coord;
>>     template <typename T, typename U = Coord<T> > class Point;
>>
>> If the user code contains:
>>
>>     typedef long size_t;
>>     Point<size_t> p;
>>
>> when traversing the AST, reaching 'p', I have access to a clang::Type
>> named 'Point<size_t>' which directs the CXXRecordDecl named
>> 'Point<long, Coord<long> >.
> The CXXRecordDecl has to use the canonical types so we don't try to
> instantiate Point twice if the user writes e.g. "Point<size_t> x;
> Point<unsigned long> y;".  I suppose it could store non-canonical
> arguments corresponding to the first instantiation as well... but we
> haven't had any use for that in the past.
>
>> Is there a way to create (from Point<size_t>) a type which would be
>> named 'Point<size_t, Coord<size_t> >'.  I.e. which preserves the
>> typedef when expanding/adding the template default parameters?
> AFAIK no... I'm pretty sure Sema never computes it, and you can't
> compute it yourself without the code to perform template type
> substitution which isn't exposed outside Sema.
>
> -Eli




More information about the cfe-dev mailing list