@@ -92,6 +92,19 @@ class ClangNameImporter {
9292 return imported_name.getBaseName ().userFacingName ().str ();
9393 }
9494
95+ template <typename IntTy>
96+ llvm::StringRef ProjectEnumCase (const clang::EnumDecl *decl, IntTy val) {
97+ for (const auto *enumerator : decl->enumerators ()) {
98+ llvm::APSInt case_val = enumerator->getInitVal ();
99+ if ((case_val.isSigned () &&
100+ llvm::APSInt::isSameValue (case_val, llvm::APSInt::get (val))) ||
101+ (case_val.isUnsigned () &&
102+ llvm::APSInt::isSameValue (case_val, llvm::APSInt::getUnsigned (val))))
103+ return m_clang_importer->getEnumConstantName (enumerator).str ();
104+ }
105+ return {};
106+ }
107+
95108private:
96109 swift::CompilerInvocation m_compiler_invocation;
97110 swift::SourceManager m_source_manager;
@@ -2638,7 +2651,8 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
26382651 return result; \
26392652 } while (0 )
26402653
2641- #define VALIDATE_AND_RETURN (IMPL, REFERENCE, TYPE, EXE_CTX, ARGS ) \
2654+ #define VALIDATE_AND_RETURN_CUSTOM (IMPL, REFERENCE, TYPE, COMPARISON, EXE_CTX, \
2655+ ARGS) \
26422656 do { \
26432657 auto result = IMPL (); \
26442658 if (!ModuleList::GetGlobalModuleListProperties () \
@@ -2658,7 +2672,7 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
26582672 return result; \
26592673 bool equivalent = \
26602674 !ReconstructType (TYPE) /* missing .swiftmodule */ || \
2661- (Equivalent ( \
2675+ (COMPARISON ( \
26622676 result, \
26632677 GetSwiftASTContext (GetSymbolContext (&_exe_ctx))->REFERENCE ARGS)); \
26642678 if (!equivalent) \
@@ -2668,6 +2682,9 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
26682682 return result; \
26692683 } while (0 )
26702684
2685+ #define VALIDATE_AND_RETURN (IMPL, REFERENCE, TYPE, EXE_CTX, ARGS ) \
2686+ VALIDATE_AND_RETURN_CUSTOM (IMPL, REFERENCE, TYPE, Equivalent, EXE_CTX, ARGS)
2687+
26712688#define VALIDATE_AND_RETURN_EXPECTED (IMPL, REFERENCE, TYPE, EXE_CTX, ARGS ) \
26722689 do { \
26732690 auto result = IMPL (); \
@@ -2707,7 +2724,10 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
27072724#else
27082725#define VALIDATE_AND_RETURN_STATIC (IMPL, REFERENCE ) \
27092726 return IMPL()
2710- #define VALIDATE_AND_RETURN (IMPL, REFERENCE, TYPE, EXE_CTX, ARGS ) return IMPL();
2727+ #define VALIDATE_AND_RETURN (IMPL, REFERENCE, TYPE, COMPARISON, EXE_CTX, ARGS ) \
2728+ return IMPL();
2729+ #define VALIDATE_AND_RETURN_CUSTOM (IMPL, REFERENCE, TYPE, EXE_CTX, ARGS ) \
2730+ return IMPL();
27112731#define VALIDATE_AND_RETURN_EXPECTED (IMPL, REFERENCE, TYPE, EXE_CTX, ARGS ) \
27122732 return IMPL();
27132733#endif
@@ -4731,18 +4751,41 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
47314751 // In some instances, a swift `structure` wraps an objc enum. The enum
47324752 // case needs to be handled, but structs are no-ops.
47334753 auto resolved = ResolveTypeAlias (dem, node, flavor, true );
4734- auto clang_type = std::get<CompilerType>(resolved);
4735- if (!clang_type )
4754+ auto resolved_type = std::get<CompilerType>(resolved);
4755+ if (!resolved_type )
47364756 return false ;
47374757
47384758 bool is_signed;
4739- if (!clang_type .IsEnumerationType (is_signed))
4759+ if (!resolved_type .IsEnumerationType (is_signed))
47404760 // The type is a clang struct, not an enum.
47414761 return false ;
47424762
4743- // The type is an enum imported from clang. Try Swift type metadata first,
4744- // and failing that fallback to the AST.
4745- LLVM_FALLTHROUGH;
4763+ if (!resolved_type.GetTypeSystem ().isa_and_nonnull <TypeSystemClang>())
4764+ return false ;
4765+
4766+ // The type is an enum imported from clang.
4767+ auto qual_type = ClangUtil::GetQualType (resolved_type);
4768+ auto *enum_type =
4769+ llvm::dyn_cast_or_null<clang::EnumType>(qual_type.getTypePtrOrNull ());
4770+ if (!enum_type)
4771+ return false ;
4772+ auto *importer = GetNameImporter ();
4773+ if (!importer)
4774+ return false ;
4775+ if (!data_byte_size)
4776+ return false ;
4777+ StringRef case_name;
4778+ if (is_signed) {
4779+ int64_t val = data.GetMaxS64 (&data_offset, data_byte_size);
4780+ case_name = importer->ProjectEnumCase (enum_type->getDecl (), val);
4781+ } else {
4782+ uint64_t val = data.GetMaxU64 (&data_offset, data_byte_size);
4783+ case_name = importer->ProjectEnumCase (enum_type->getDecl (), val);
4784+ }
4785+ if (case_name.empty ())
4786+ return false ;
4787+ s << case_name;
4788+ return true ;
47464789 }
47474790 case Node::Kind::Enum:
47484791 case Node::Kind::BoundGenericEnum: {
@@ -4762,18 +4805,6 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
47624805 error = toString (case_name.takeError ());
47634806 }
47644807
4765- // No result available from the runtime, fallback to the AST. This occurs
4766- // for some Clang imported enums.
4767- if (auto swift_ast_context =
4768- GetSwiftASTContext (GetSymbolContext (exe_scope))) {
4769- ExecutionContext exe_ctx;
4770- exe_scope->CalculateExecutionContext (exe_ctx);
4771- if (swift_ast_context->DumpTypeValue (
4772- ReconstructType (type, &exe_ctx), s, format, data, data_offset,
4773- data_byte_size, bitfield_bit_size, bitfield_bit_offset,
4774- exe_scope, is_base_class))
4775- return true ;
4776- }
47774808 s << error;
47784809 return false ;
47794810 }
@@ -4825,17 +4856,21 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
48254856 ConstString (((StreamString *)&s)->GetString ())) &&
48264857 " TypeSystemSwiftTypeRef diverges from SwiftASTContext" );
48274858 });
4828-
4829- // SwiftASTContext fails here, details explained in RemoteASTImport.test
4830- if (StringRef (AsMangledName (type)) == " $s15RemoteASTImport14FromMainModuleCD" )
4831- return impl ();
4832-
48334859#endif
48344860
4835- VALIDATE_AND_RETURN (impl, DumpTypeValue, type, exe_scope,
4836- (ReconstructType (type, exe_scope), ast_s, format, data,
4837- data_offset, data_byte_size, bitfield_bit_size,
4838- bitfield_bit_offset, exe_scope, is_base_class));
4861+ auto better_or_equal = [](bool a, bool b) -> bool {
4862+ if (a || a == b)
4863+ return true ;
4864+
4865+ llvm::dbgs () << " TypeSystemSwiftTypeRef: " << a << " SwiftASTContext: " << b
4866+ << " \n " ;
4867+ return false ;
4868+ };
4869+ VALIDATE_AND_RETURN_CUSTOM (
4870+ impl, DumpTypeValue, type, better_or_equal, exe_scope,
4871+ (ReconstructType (type, exe_scope), ast_s, format, data, data_offset,
4872+ data_byte_size, bitfield_bit_size, bitfield_bit_offset, exe_scope,
4873+ is_base_class));
48394874}
48404875
48414876bool TypeSystemSwiftTypeRef::IsPointerOrReferenceType (
0 commit comments