@@ -92,6 +92,19 @@ class ClangNameImporter {
92
92
return imported_name.getBaseName ().userFacingName ().str ();
93
93
}
94
94
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
+
95
108
private:
96
109
swift::CompilerInvocation m_compiler_invocation;
97
110
swift::SourceManager m_source_manager;
@@ -2638,7 +2651,8 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
2638
2651
return result; \
2639
2652
} while (0 )
2640
2653
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) \
2642
2656
do { \
2643
2657
auto result = IMPL (); \
2644
2658
if (!ModuleList::GetGlobalModuleListProperties () \
@@ -2658,7 +2672,7 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
2658
2672
return result; \
2659
2673
bool equivalent = \
2660
2674
!ReconstructType (TYPE) /* missing .swiftmodule */ || \
2661
- (Equivalent ( \
2675
+ (COMPARISON ( \
2662
2676
result, \
2663
2677
GetSwiftASTContext (GetSymbolContext (&_exe_ctx))->REFERENCE ARGS)); \
2664
2678
if (!equivalent) \
@@ -2668,6 +2682,9 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
2668
2682
return result; \
2669
2683
} while (0 )
2670
2684
2685
+ #define VALIDATE_AND_RETURN (IMPL, REFERENCE, TYPE, EXE_CTX, ARGS ) \
2686
+ VALIDATE_AND_RETURN_CUSTOM (IMPL, REFERENCE, TYPE, Equivalent, EXE_CTX, ARGS)
2687
+
2671
2688
#define VALIDATE_AND_RETURN_EXPECTED (IMPL, REFERENCE, TYPE, EXE_CTX, ARGS ) \
2672
2689
do { \
2673
2690
auto result = IMPL (); \
@@ -2707,7 +2724,10 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
2707
2724
#else
2708
2725
#define VALIDATE_AND_RETURN_STATIC (IMPL, REFERENCE ) \
2709
2726
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();
2711
2731
#define VALIDATE_AND_RETURN_EXPECTED (IMPL, REFERENCE, TYPE, EXE_CTX, ARGS ) \
2712
2732
return IMPL();
2713
2733
#endif
@@ -4731,18 +4751,41 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
4731
4751
// In some instances, a swift `structure` wraps an objc enum. The enum
4732
4752
// case needs to be handled, but structs are no-ops.
4733
4753
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 )
4736
4756
return false ;
4737
4757
4738
4758
bool is_signed;
4739
- if (!clang_type .IsEnumerationType (is_signed))
4759
+ if (!resolved_type .IsEnumerationType (is_signed))
4740
4760
// The type is a clang struct, not an enum.
4741
4761
return false ;
4742
4762
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 ;
4746
4789
}
4747
4790
case Node::Kind::Enum:
4748
4791
case Node::Kind::BoundGenericEnum: {
@@ -4762,18 +4805,6 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
4762
4805
error = toString (case_name.takeError ());
4763
4806
}
4764
4807
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
- }
4777
4808
s << error;
4778
4809
return false ;
4779
4810
}
@@ -4825,17 +4856,21 @@ bool TypeSystemSwiftTypeRef::DumpTypeValue(
4825
4856
ConstString (((StreamString *)&s)->GetString ())) &&
4826
4857
" TypeSystemSwiftTypeRef diverges from SwiftASTContext" );
4827
4858
});
4828
-
4829
- // SwiftASTContext fails here, details explained in RemoteASTImport.test
4830
- if (StringRef (AsMangledName (type)) == " $s15RemoteASTImport14FromMainModuleCD" )
4831
- return impl ();
4832
-
4833
4859
#endif
4834
4860
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));
4839
4874
}
4840
4875
4841
4876
bool TypeSystemSwiftTypeRef::IsPointerOrReferenceType (
0 commit comments