From b30fc55c8f2e87396ddbc443ddf3470e266cb94f Mon Sep 17 00:00:00 2001 From: Augusto Noronha Date: Mon, 19 Sep 2022 15:23:59 -0700 Subject: [PATCH] Ask the external provider for type info first in visitAnyNominalTypeRef C/C++ imported types have Builtin type metadata. Ask the external provider when the type info we have is builtin. --- stdlib/public/Reflection/TypeLowering.cpp | 40 +++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/stdlib/public/Reflection/TypeLowering.cpp b/stdlib/public/Reflection/TypeLowering.cpp index 8dd80ef049ba2..a9d573e18d14c 100644 --- a/stdlib/public/Reflection/TypeLowering.cpp +++ b/stdlib/public/Reflection/TypeLowering.cpp @@ -2226,29 +2226,43 @@ class LowerType } const TypeInfo *visitAnyNominalTypeRef(const TypeRef *TR) { + auto QueryExternalTypeInfoProvider = [&]() -> const TypeInfo * { + if (ExternalTypeInfo) { + std::string MangledName; + if (auto N = dyn_cast(TR)) + MangledName = N->getMangledName(); + else if (auto BG = dyn_cast(TR)) + MangledName = BG->getMangledName(); + if (!MangledName.empty()) + if (auto *imported = ExternalTypeInfo->getTypeInfo(MangledName)) + return imported; + } + return nullptr; + }; + auto FD = TC.getBuilder().getFieldTypeInfo(TR); if (FD == nullptr || FD->isStruct()) { // Maybe this type is opaque -- look for a builtin // descriptor to see if we at least know its size // and alignment. - if (auto ImportedTypeDescriptor = TC.getBuilder().getBuiltinTypeInfo(TR)) + if (auto ImportedTypeDescriptor = + TC.getBuilder().getBuiltinTypeInfo(TR)) { + // This might be an external type we treat as opaque (like C structs), + // the external type info provider might have better type information, + // so ask it first. + if (auto External = QueryExternalTypeInfoProvider()) + return External; + return TC.makeTypeInfo(TC.getBuilder(), ImportedTypeDescriptor); + } - // Otherwise, we're out of luck. if (FD == nullptr) { - if (ExternalTypeInfo) { - // Ask the ExternalTypeInfo. It may be a Clang-imported type. - std::string MangledName; - if (auto N = dyn_cast(TR)) - MangledName = N->getMangledName(); - else if (auto BG = dyn_cast(TR)) - MangledName = BG->getMangledName(); - if (!MangledName.empty()) - if (auto *imported = ExternalTypeInfo->getTypeInfo(MangledName)) - return imported; - } + // If we still have no type info ask the external provider. + if (auto External = QueryExternalTypeInfoProvider()) + return External; + // If the external provider also fails we're out of luck. DEBUG_LOG(fprintf(stderr, "No TypeInfo for nominal type: "); TR->dump()); return nullptr; }