10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
13
- #include " swift/AST/ASTContext.h"
14
- #include " swift/Demangling/Demangler.h"
15
13
#include < stdio.h>
16
14
#if HAVE_SYS_TYPES_H
17
15
#include < sys/types.h>
18
16
#endif
19
17
20
- #include " SwiftASTManipulator.h" "
18
+ #include " SwiftASTManipulator.h"
21
19
#include " SwiftExpressionParser.h"
22
20
#include " SwiftExpressionSourceCode.h"
23
21
#include " SwiftREPLMaterializer.h"
22
+ #include " SwiftASTManipulator.h"
24
23
25
24
#include " Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
26
25
#include " lldb/Core/Module.h"
40
39
41
40
#include " swift/AST/Type.h"
42
41
#include " swift/AST/Types.h"
42
+ #include " swift/AST/ASTContext.h"
43
+ #include " swift/Demangling/Demangler.h"
44
+ #include " swift/AST/GenericEnvironment.h"
45
+
43
46
44
47
#include < cstdlib>
45
48
#include < map>
@@ -451,14 +454,99 @@ GetPersistentState(Target *target, ExecutionContext &exe_ctx) {
451
454
return target->GetSwiftPersistentExpressionState (*exe_scope);
452
455
}
453
456
457
+ // / Check if we can evaluate the expression as generic.
458
+ // / Currently, evaluating expression as a generic has several limitations:
459
+ // / - Only self will be evaluated with unbound generics.
460
+ // / - The Self type can only have one generic parameter.
461
+ // / - The Self type has to be the outermost type with unbound generics.
462
+ static bool CanEvaluateExpressionWithoutBindingGenericParams (
463
+ const llvm::SmallVectorImpl<SwiftASTManipulator::VariableInfo>
464
+ &variables,
465
+ Block *block, StackFrame &stack_frame) {
466
+ // First, find the compiler type of self with the generic parameters not
467
+ // bound.
468
+ auto self_var = SwiftExpressionParser::FindSelfVariable (block);
469
+ if (!self_var)
470
+ return false ;
471
+
472
+ lldb::ValueObjectSP self_valobj =
473
+ stack_frame.GetValueObjectForFrameVariable (self_var,
474
+ lldb::eNoDynamicValues);
475
+ if (!self_valobj)
476
+ return false ;
477
+
478
+ auto self_type = self_valobj->GetCompilerType ();
479
+ if (!self_type)
480
+ return false ;
481
+
482
+ auto *ts = self_type.GetTypeSystem ()
483
+ .dyn_cast_or_null <TypeSystemSwift>()
484
+ ->GetSwiftASTContext ();
485
+
486
+ if (!ts)
487
+ return false ;
488
+
489
+ auto swift_type = ts->GetSwiftType (self_type);
490
+ if (!swift_type)
491
+ return false ;
492
+
493
+ auto *decl = swift_type->getAnyGeneric ();
494
+ if (!decl)
495
+ return false ;
496
+
497
+
498
+ auto *env = decl->getGenericEnvironment ();
499
+ if (!env)
500
+ return false ;
501
+ auto params = env->getGenericParams ();
502
+
503
+ // If there aren't any generic parameters we can't evaluate the expression as
504
+ // generic.
505
+ if (params.empty ())
506
+ return false ;
507
+
508
+ auto *first_param = params[0 ];
509
+ // Currently we only support evaluating self as generic if the generic
510
+ // parameter is the first one.
511
+ if (first_param->getDepth () != 0 || first_param->getIndex () != 0 )
512
+ return false ;
513
+
514
+ bool contains_0_0 = false ;
515
+ bool contains_other_0_depth_params = false ;
516
+ for (auto *pair : params) {
517
+ if (pair->getDepth () == 0 ) {
518
+ if (pair->getIndex () == 0 )
519
+ contains_0_0 = true ;
520
+ else
521
+ contains_other_0_depth_params = true ;
522
+ }
523
+ }
524
+
525
+ // We only allow generic evaluation when the Self type contains the outermost
526
+ // generic parameter.
527
+ if (!contains_0_0)
528
+ return false ;
529
+
530
+ // We only allow the Self type to have one generic parameter.
531
+ if (contains_other_0_depth_params)
532
+ return false ;
533
+
534
+ // Now, check that we do have the metadata pointer as a local variable.
535
+ for (auto &variable : variables) {
536
+ if (variable.GetName ().str () == " $τ_0_0" )
537
+ return true ;
538
+ }
539
+ // Couldn't find the metadata pointer, so can't evaluate as generic.
540
+ return false ;
541
+ }
542
+
454
543
SwiftExpressionParser::ParseResult
455
544
SwiftUserExpression::GetTextAndSetExpressionParser (
456
545
DiagnosticManager &diagnostic_manager,
457
546
std::unique_ptr<SwiftExpressionSourceCode> &source_code,
458
547
ExecutionContext &exe_ctx, ExecutionContextScope *exe_scope) {
459
548
using ParseResult = SwiftExpressionParser::ParseResult;
460
549
Log *log = GetLog (LLDBLog::Expressions);
461
- uint32_t first_body_line = 0;
462
550
463
551
lldb::TargetSP target_sp;
464
552
SymbolContext sc;
@@ -468,9 +556,10 @@ SwiftUserExpression::GetTextAndSetExpressionParser(
468
556
stack_frame = exe_scope->CalculateStackFrame ();
469
557
470
558
if (stack_frame) {
471
- sc = stack_frame->GetSymbolContext(lldb::eSymbolContextEverything);
559
+ sc = stack_frame->GetSymbolContext (lldb::eSymbolContextEverything);
472
560
}
473
561
}
562
+
474
563
llvm::SmallVector<SwiftASTManipulator::VariableInfo> local_variables;
475
564
476
565
if (!RegisterAllVariables (sc, stack_frame, *m_swift_ast_ctx, local_variables,
@@ -483,6 +572,16 @@ SwiftUserExpression::GetTextAndSetExpressionParser(
483
572
return ParseResult::retry_no_bind_generic_params;
484
573
}
485
574
575
+ if (m_options.GetBindGenericTypes () == lldb::eDontBind &&
576
+ !CanEvaluateExpressionWithoutBindingGenericParams (
577
+ local_variables, sc.block , *stack_frame.get ())) {
578
+ diagnostic_manager.PutString (
579
+ eDiagnosticSeverityError,
580
+ " Could not evaluate the expression without binding generic types." );
581
+ return ParseResult::unrecoverable_error;
582
+ }
583
+
584
+ uint32_t first_body_line = 0 ;
486
585
if (!source_code->GetText (m_transformed_text, m_options.GetLanguage (),
487
586
m_needs_object_ptr, m_in_static_method, m_is_class,
488
587
m_is_weak_self, m_options, exe_ctx,
0 commit comments