[cfe-dev] AST::Type::getAs<sugar>()?

Reid Kleckner rnk at google.com
Wed May 6 08:53:14 PDT 2015


On Thu, Apr 16, 2015 at 11:51 PM, Stephan Bergmann <sbergman at redhat.com>
wrote:

> I naively assumed AST::Type::getAs<T>() would work regardless whether T
> represents sugar or not, and looking closer it is indeed specialized for
> TypedefType, TemplateSpecializationType, and AttributeType to make those
> work, but not for other sugar like SubstTemplateTypeParmType.
>
> For example, t->getAs<SubstTemplateTypeParmType>() does not work if t is a
> wrapping TypedefType (but happens to work if t itself already is a
> SubsTemplateTypeParmType).  Is that intended?
>

I don't think it was intended. I also thought I understood how type sugar
and getAs work, and this is a surprise to me as well. :)

I would've expected getAs to work like this:

template <typename T> const T *Type::getAs() const {
  // If this is directly a T type, return it.
  if (const T *Ty = dyn_cast<T>(this))
    return Ty;
  // If the canonical type is directly a T type, return it.
  if (const T *Ty = dyn_cast<T>(CanonicalType))
    return Ty;
  // Do repeated single step desugarings until we find the type or return
null.
  ...
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150506/e3dbcf90/attachment.html>


More information about the cfe-dev mailing list