@@ -1951,19 +1951,30 @@ static QualType findBuiltinType(llvm::StringRef typeName, ASTContext& Context) {
19511951 */
19521952 return QualType ();
19531953}
1954+ static std::optional<QualType> GetTypeInternal (Decl* D) {
1955+ if (!D)
1956+ return {};
1957+ // Even though typedefs derive from TypeDecl, their getTypeForDecl()
1958+ // returns a nullptr.
1959+ if (const auto * TND = llvm::dyn_cast_or_null<TypedefNameDecl>(D))
1960+ return TND->getUnderlyingType ();
1961+
1962+ if (auto * VD = dyn_cast<ValueDecl>(D))
1963+ return VD->getType ();
1964+
1965+ if (const auto * TD = llvm::dyn_cast_or_null<TypeDecl>(D))
1966+ return QualType (TD->getTypeForDecl (), 0 );
1967+
1968+ return {};
1969+ }
19541970} // namespace
19551971
19561972TCppType_t GetType (const std::string& name) {
19571973 QualType builtin = findBuiltinType (name, getASTContext ());
19581974 if (!builtin.isNull ())
19591975 return builtin.getAsOpaquePtr ();
19601976
1961- auto * D = (Decl*)GetNamed (name, /* Within= */ 0 );
1962- if (auto * TD = llvm::dyn_cast_or_null<TypeDecl>(D)) {
1963- return QualType (TD->getTypeForDecl (), 0 ).getAsOpaquePtr ();
1964- }
1965-
1966- return (TCppType_t)0 ;
1977+ return GetTypeFromScope ((Decl*)GetNamed (name, /* Within=*/ nullptr ));
19671978}
19681979
19691980TCppType_t GetComplexType (TCppType_t type) {
@@ -1974,17 +1985,12 @@ TCppType_t GetComplexType(TCppType_t type) {
19741985
19751986TCppType_t GetTypeFromScope (TCppScope_t klass) {
19761987 if (!klass)
1977- return 0 ;
1978-
1979- auto * D = (Decl*)klass;
1980-
1981- if (auto * VD = dyn_cast<ValueDecl>(D))
1982- return VD->getType ().getAsOpaquePtr ();
1988+ return nullptr ;
19831989
1984- if (auto * TD = dyn_cast<TypeDecl>(D ))
1985- return getASTContext (). getTypeDeclType (TD). getAsOpaquePtr ();
1990+ if (auto QT = GetTypeInternal ((Decl*)klass ))
1991+ return QT-> getAsOpaquePtr ();
19861992
1987- return (TCppType_t) nullptr ;
1993+ return nullptr ;
19881994}
19891995
19901996// Internal functions that are not needed outside the library are
0 commit comments