queston for clang developers: Given a ParsedType in a Sema function, how can I get the corresponding NamedDecl?

Hi,

I have a ParsedType returned from a getTypeName call in a Sema function. How can I find the corresponding NamedDecl?

Thanks.

-John

To confirm: you're looking for the NamedDecl that declares the type found
by name lookup in getTypeName?

First convert the ParsedType to a QualType by calling GetTypeFromParser.
Then switch over the type class of the Type* in the QualType, and pick the
declaration out of it. You'd need to handle at least UnresolvedUsingType,
TypedefType, TagType, ElaboratedType, TemplateTypeParmType,
TemplateSpecializationType, InjectedClassNameType, DependentNameType, and
DependentTemplateSpecializationType. Maybe some of the ObjC types too.

But this is a bit of an unusual thing to want to do, and doing the above
will be fragile. Maybe there's a better way; what's the larger context?

Hi Richard,

Thanks for the response.

But this is a bit of an unusual thing to want to do, and doing the above will be fragile. Maybe there’s a better way; what’s the larger context?

In Sema::CorrectTypo, I’ve looked up a type name in the GlobalModuleIndex, and found the corresponding type via getTypeName in the form of a ParsedType that seems to contain an ExtQualsTypeCommonBase. I want to create a TypoCorrection, and I think I need to give it a NamedDecl for the type via TypoCorrection::setCorrectionDecl, but I couldn’t figure out how to find it. I’m I on the right track? This is regarding moving the code for looking for a mission module import into CorrectTypo, per your suggestion.

Thanks.

-John

Hi Richard,

Thanks for the response.

>But this is a bit of an unusual thing to want to do, and doing the above
will be fragile. Maybe there's a better way; what's the larger context?

In Sema::CorrectTypo, I've looked up a type name in the GlobalModuleIndex,
and found the corresponding type via getTypeName in the form of a
ParsedType that seems to contain an ExtQualsTypeCommonBase. I want to
create a TypoCorrection, and I think I need to give it a NamedDecl for the
type via TypoCorrection::setCorrectionDecl, but I couldn't figure out how
to find it. I'm I on the right track? This is regarding moving the code
for looking for a mission module import into CorrectTypo, per your
suggestion.

I don't think you need to call getTypeName here. It sounds like you want to
look up a declaration; getTypeName does that and then does some other stuff
that you don't want. Without knowing exactly what you're trying to do, I'd
think you probably want to invoke name lookup yourself from CorrectTypo.