@@ -2735,12 +2735,42 @@ constexpr ExecutionContextScope *g_no_exe_ctx = nullptr;
2735
2735
#define FORWARD_TO_EXPRAST_ONLY (FUNC, ARGS, DEFAULT_RETVAL ) \
2736
2736
do { \
2737
2737
if (auto target_sp = GetTargetWP ().lock ()) \
2738
- if (auto swift_ast_ctx = GetSwiftASTContext ( \
2738
+ if (auto swift_ast_ctx = GetSwiftASTContext ( \
2739
2739
SymbolContext (target_sp, target_sp->GetExecutableModule ()))) \
2740
2740
return swift_ast_ctx->FUNC ARGS; \
2741
2741
return DEFAULT_RETVAL; \
2742
2742
} while (0 )
2743
2743
2744
+ bool TypeSystemSwiftTypeRef::UseSwiftASTContextFallback (
2745
+ const char *func_name, lldb::opaque_compiler_type_t type) {
2746
+ if (!ModuleList::GetGlobalModuleListProperties ().GetSwiftTypeSystemFallback ())
2747
+ return false ;
2748
+
2749
+ LLDB_LOGF (GetLog (LLDBLog::Types),
2750
+ " TypeSystemSwiftTypeRef::%s(): Engaging SwiftASTContext fallback "
2751
+ " for type %s" ,
2752
+ func_name, AsMangledName (type));
2753
+ return true ;
2754
+ }
2755
+
2756
+ bool TypeSystemSwiftTypeRef::DiagnoseSwiftASTContextFallback (
2757
+ const char *func_name, lldb::opaque_compiler_type_t type) {
2758
+ const char *type_name = AsMangledName (type);
2759
+
2760
+ std::optional<lldb::user_id_t > debugger_id;
2761
+ if (auto target_sp = GetTargetWP ().lock ())
2762
+ debugger_id = target_sp->GetDebugger ().GetID ();
2763
+
2764
+ std::string msg;
2765
+ llvm::raw_string_ostream (msg)
2766
+ << " TypeSystemSwiftTypeRef::" << func_name
2767
+ << " : had to engage SwiftASTContext fallback for type " << type_name;
2768
+ Debugger::ReportWarning (msg, debugger_id, &m_fallback_warning);
2769
+
2770
+ LLDB_LOGF (GetLog (LLDBLog::Types), " %s" , msg.c_str ());
2771
+ return true ;
2772
+ }
2773
+
2744
2774
CompilerType
2745
2775
TypeSystemSwiftTypeRef::RemangleAsType (swift::Demangle::Demangler &dem,
2746
2776
swift::Demangle::NodePointer node,
@@ -3453,14 +3483,16 @@ TypeSystemSwiftTypeRef::GetBitSize(opaque_compiler_type_t type,
3453
3483
if (auto result = runtime->GetBitSize ({weak_from_this (), type}, exe_scope))
3454
3484
return result;
3455
3485
// Runtime failed, fallback to SwiftASTContext.
3456
- LLDB_LOGF (GetLog (LLDBLog::Types),
3457
- " Couldn't compute size of type %s using SwiftLanguageRuntime." ,
3458
- AsMangledName (type));
3459
-
3460
- if (auto swift_ast_context =
3461
- GetSwiftASTContext (GetSymbolContext (exe_scope)))
3462
- return swift_ast_context->GetBitSize (ReconstructType (type, exe_scope),
3463
- exe_scope);
3486
+ if (UseSwiftASTContextFallback (__FUNCTION__, type)) {
3487
+ if (auto swift_ast_context =
3488
+ GetSwiftASTContext (GetSymbolContext (exe_scope))) {
3489
+ auto result = swift_ast_context->GetBitSize (
3490
+ ReconstructType (type, exe_scope), exe_scope);
3491
+ if (result)
3492
+ DiagnoseSwiftASTContextFallback (__FUNCTION__, type);
3493
+ return result;
3494
+ }
3495
+ }
3464
3496
}
3465
3497
3466
3498
// FIXME: Move this to the top. Currently this causes VALIDATE
@@ -3501,12 +3533,16 @@ TypeSystemSwiftTypeRef::GetByteStride(opaque_compiler_type_t type,
3501
3533
return stride;
3502
3534
}
3503
3535
// Runtime failed, fallback to SwiftASTContext.
3504
- LLDB_LOGF (GetLog (LLDBLog::Types),
3505
- " Couldn't compute stride of type %s using SwiftLanguageRuntime." ,
3506
- AsMangledName (type));
3507
- if (auto swift_ast_context =
3508
- GetSwiftASTContext (GetSymbolContext (exe_scope)))
3509
- return swift_ast_context->GetByteStride (ReconstructType (type), exe_scope);
3536
+ if (UseSwiftASTContextFallback (__FUNCTION__, type)) {
3537
+ if (auto swift_ast_context =
3538
+ GetSwiftASTContext (GetSymbolContext (exe_scope))) {
3539
+ auto result =
3540
+ swift_ast_context->GetByteStride (ReconstructType (type), exe_scope);
3541
+ if (result)
3542
+ DiagnoseSwiftASTContextFallback (__FUNCTION__, type);
3543
+ return result;
3544
+ }
3545
+ }
3510
3546
return {};
3511
3547
};
3512
3548
VALIDATE_AND_RETURN (impl, GetByteStride, type, exe_scope,
@@ -3624,19 +3660,19 @@ TypeSystemSwiftTypeRef::GetNumChildren(opaque_compiler_type_t type,
3624
3660
impl, GetNumChildren, type, exe_ctx_obj,
3625
3661
(ReconstructType (type, exe_ctx), omit_empty_base_classes, exe_ctx));
3626
3662
}
3627
- LLDB_LOGF ( GetLog (LLDBLog::Types),
3628
- " Using SwiftASTContext::GetNumChildren fallback for type %s " ,
3629
- AsMangledName (type));
3630
-
3631
- // Try SwiftASTContext.
3632
- if ( auto swift_ast_context = GetSwiftASTContext ( GetSymbolContext ( exe_ctx)))
3633
- if ( auto n = llvm::expectedToStdOptional (swift_ast_context-> GetNumChildren (
3634
- ReconstructType (type, exe_ctx ), omit_empty_base_classes ,
3635
- exe_ctx))) {
3636
- LLDB_LOG_ERRORV ( GetLog (LLDBLog::Types), num_children. takeError (),
3637
- " SwiftLanguageRuntime::GetNumChildren() failed: {0} " ) ;
3638
- return *n;
3639
- }
3663
+ // Runtime failed, fallback to SwiftASTContext.
3664
+ if ( UseSwiftASTContextFallback (__FUNCTION__, type)) {
3665
+ if ( auto swift_ast_context = GetSwiftASTContext ( GetSymbolContext (exe_ctx)))
3666
+ if ( auto n =
3667
+ llvm::expectedToStdOptional (swift_ast_context-> GetNumChildren (
3668
+ ReconstructType (type, exe_ctx), omit_empty_base_classes,
3669
+ exe_ctx))) {
3670
+ LLDB_LOG_ERRORV ( GetLog (LLDBLog::Types ), num_children. takeError () ,
3671
+ " SwiftLanguageRuntime::GetNumChildren() failed: {0} " );
3672
+ DiagnoseSwiftASTContextFallback (__FUNCTION__, type);
3673
+ return *n ;
3674
+ }
3675
+ }
3640
3676
3641
3677
// Otherwise return the error from the runtime.
3642
3678
return num_children.takeError ();
@@ -3686,12 +3722,16 @@ uint32_t TypeSystemSwiftTypeRef::GetNumFields(opaque_compiler_type_t type,
3686
3722
.value_or (0 );
3687
3723
}
3688
3724
3689
- LLDB_LOGF (GetLog (LLDBLog::Types),
3690
- " Using SwiftASTContext::GetNumFields fallback for type %s" ,
3691
- AsMangledName (type));
3692
-
3693
- if (auto swift_ast_context = GetSwiftASTContext (GetSymbolContext (exe_ctx)))
3694
- return swift_ast_context->GetNumFields (ReconstructType (type, exe_ctx), exe_ctx);
3725
+ // Runtime failed, fallback to SwiftASTContext.
3726
+ if (UseSwiftASTContextFallback (__FUNCTION__, type))
3727
+ if (auto swift_ast_context =
3728
+ GetSwiftASTContext (GetSymbolContext (exe_ctx))) {
3729
+ auto result = swift_ast_context->GetNumFields (
3730
+ ReconstructType (type, exe_ctx), exe_ctx);
3731
+ if (result)
3732
+ DiagnoseSwiftASTContextFallback (__FUNCTION__, type);
3733
+ return result;
3734
+ }
3695
3735
return {};
3696
3736
}
3697
3737
@@ -3787,6 +3827,7 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
3787
3827
return ast_num_children.value_or (0 );
3788
3828
};
3789
3829
auto impl = [&]() -> llvm::Expected<CompilerType> {
3830
+ std::string error = " unknown error" ;
3790
3831
ExecutionContextScope *exe_scope = nullptr ;
3791
3832
if (exe_ctx)
3792
3833
exe_scope = exe_ctx->GetBestExecutionContextScope ();
@@ -3809,7 +3850,7 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
3809
3850
return result;
3810
3851
}
3811
3852
if (!result)
3812
- llvm::consumeError (result.takeError ());
3853
+ error = llvm::toString (result.takeError ());
3813
3854
}
3814
3855
// Clang types can be resolved even without a process.
3815
3856
bool is_signed;
@@ -3923,10 +3964,17 @@ TypeSystemSwiftTypeRef::GetChildCompilerTypeAtIndex(
3923
3964
" Couldn't compute size of type %s without a process." ,
3924
3965
AsMangledName (type));
3925
3966
3926
- // FIXME: SwiftASTContext can sometimes find more Clang types because it
3927
- // imports Clang modules from source. We should be able to replicate this
3928
- // and remove this fallback.
3929
- return fallback ();
3967
+ // Runtime failed, fallback to SwiftASTContext.
3968
+ if (UseSwiftASTContextFallback (__FUNCTION__, type)) {
3969
+ // FIXME: SwiftASTContext can sometimes find more Clang types because it
3970
+ // imports Clang modules from source. We should be able to replicate this
3971
+ // and remove this fallback.
3972
+ auto result = fallback ();
3973
+ if (result)
3974
+ DiagnoseSwiftASTContextFallback (__FUNCTION__, type);
3975
+ return result;
3976
+ }
3977
+ return llvm::createStringError (llvm::inconvertibleErrorCode (), error);
3930
3978
};
3931
3979
// Skip validation when there is no process, because then we also
3932
3980
// don't have a runtime.
@@ -4080,10 +4128,17 @@ size_t TypeSystemSwiftTypeRef::GetIndexOfChildMemberWithName(
4080
4128
" type %s" ,
4081
4129
AsMangledName (type));
4082
4130
4083
- if (auto swift_ast_context = GetSwiftASTContext (GetSymbolContext (exe_ctx)))
4084
- return swift_ast_context->GetIndexOfChildMemberWithName (
4085
- ReconstructType (type, exe_ctx), name, exe_ctx, omit_empty_base_classes,
4086
- child_indexes);
4131
+ // Runtime failed, fallback to SwiftASTContext.
4132
+ if (UseSwiftASTContextFallback (__FUNCTION__, type))
4133
+ if (auto swift_ast_context =
4134
+ GetSwiftASTContext (GetSymbolContext (exe_ctx))) {
4135
+ auto result = swift_ast_context->GetIndexOfChildMemberWithName (
4136
+ ReconstructType (type, exe_ctx), name, exe_ctx,
4137
+ omit_empty_base_classes, child_indexes);
4138
+ if (result)
4139
+ DiagnoseSwiftASTContextFallback (__FUNCTION__, type);
4140
+ return result;
4141
+ }
4087
4142
return {};
4088
4143
}
4089
4144
@@ -4388,10 +4443,16 @@ TypeSystemSwiftTypeRef::GetInstanceType(opaque_compiler_type_t type,
4388
4443
// type alias isn't possible, or the user might have defined the
4389
4444
// type alias in the REPL. In these cases, fallback to asking the AST
4390
4445
// for the canonical type.
4391
- if (auto swift_ast_context =
4392
- GetSwiftASTContext (GetSymbolContext (exe_scope)))
4393
- return swift_ast_context->GetInstanceType (
4394
- ReconstructType (type, exe_scope), exe_scope);
4446
+ // Runtime failed, fallback to SwiftASTContext.
4447
+ if (UseSwiftASTContextFallback (__FUNCTION__, type))
4448
+ if (auto swift_ast_context =
4449
+ GetSwiftASTContext (GetSymbolContext (exe_scope))) {
4450
+ auto result = swift_ast_context->GetInstanceType (
4451
+ ReconstructType (type, exe_scope), exe_scope);
4452
+ if (result)
4453
+ DiagnoseSwiftASTContextFallback (__FUNCTION__, type);
4454
+ return result;
4455
+ }
4395
4456
return {};
4396
4457
}
4397
4458
0 commit comments