@@ -404,8 +404,7 @@ Interpreter::Visit(const BitFieldExtractionNode *node) {
404
404
return child_valobj_sp;
405
405
}
406
406
407
- static CompilerType GetBasicTypeFromCU (std::shared_ptr<StackFrame> ctx,
408
- lldb::BasicType basic_type) {
407
+ static lldb::TypeSystemSP GetTypeSystemFromCU (std::shared_ptr<StackFrame> ctx) {
409
408
SymbolContext symbol_context =
410
409
ctx->GetSymbolContext (lldb::eSymbolContextCompUnit);
411
410
auto language = symbol_context.comp_unit ->GetLanguage ();
@@ -414,20 +413,103 @@ static CompilerType GetBasicTypeFromCU(std::shared_ptr<StackFrame> ctx,
414
413
auto type_system =
415
414
symbol_context.module_sp ->GetTypeSystemForLanguage (language);
416
415
416
+ if (type_system)
417
+ return *type_system;
418
+
419
+ return lldb::TypeSystemSP ();
420
+ }
421
+
422
+ static CompilerType GetBasicType (lldb::TypeSystemSP type_system,
423
+ lldb::BasicType basic_type) {
417
424
if (type_system)
418
425
if (auto compiler_type = type_system.get ()->GetBasicTypeFromAST (basic_type))
419
426
return compiler_type;
420
427
421
- return CompilerType ();
428
+ CompilerType empty_type;
429
+ return empty_type;
430
+ }
431
+
432
+ llvm::Expected<CompilerType>
433
+ Interpreter::PickLiteralType (lldb::TypeSystemSP type_system,
434
+ std::shared_ptr<ExecutionContextScope> ctx,
435
+ const ScalarLiteralNode *literal) {
436
+ Scalar scalar = literal->GetValue ();
437
+ if (scalar.GetType () == Scalar::e_float) {
438
+ if (literal->IsFloat ())
439
+ return GetBasicType (type_system, lldb::eBasicTypeFloat);
440
+ return GetBasicType (type_system, lldb::eBasicTypeDouble);
441
+ } else if (scalar.GetType () == Scalar::e_int) {
442
+ // Binary, Octal, Hexadecimal and literals with a U suffix are allowed to be
443
+ // an unsigned integer.
444
+ bool unsigned_is_allowed =
445
+ literal->IsUnsigned () || literal->GetRadix () != 10 ;
446
+
447
+ // Try int/unsigned int.
448
+ uint64_t int_byte_size = 0 ;
449
+ if (auto temp = GetBasicType (type_system, lldb::eBasicTypeInt)
450
+ .GetByteSize (ctx.get ()))
451
+ int_byte_size = *temp;
452
+ unsigned int_size = int_byte_size * CHAR_BIT;
453
+ llvm::APInt apint = scalar.GetAPSInt ();
454
+ if (!literal->IsLong () && !literal->IsLongLong () &&
455
+ apint.isIntN (int_size)) {
456
+ if (!literal->IsUnsigned () && apint.isIntN (int_size - 1 ))
457
+ return GetBasicType (type_system, lldb::eBasicTypeInt);
458
+ if (unsigned_is_allowed)
459
+ return GetBasicType (type_system, lldb::eBasicTypeUnsignedInt);
460
+ }
461
+ // Try long/unsigned long.
462
+ uint64_t long_byte_size = 0 ;
463
+ if (auto temp = GetBasicType (type_system, lldb::eBasicTypeLong)
464
+ .GetByteSize (ctx.get ()))
465
+ long_byte_size = *temp;
466
+ unsigned long_size = long_byte_size * CHAR_BIT;
467
+ if (!literal->IsLongLong () && apint.isIntN (long_size)) {
468
+ if (!literal->IsUnsigned () && apint.isIntN (long_size - 1 ))
469
+ return GetBasicType (type_system, lldb::eBasicTypeLong);
470
+ if (unsigned_is_allowed)
471
+ return GetBasicType (type_system, lldb::eBasicTypeUnsignedLong);
472
+ }
473
+ // Try long long/unsigned long long.
474
+ uint64_t long_long_byte_size = 0 ;
475
+ if (auto temp = GetBasicType (type_system, lldb::eBasicTypeLongLong)
476
+ .GetByteSize (ctx.get ()))
477
+ long_long_byte_size = *temp;
478
+ unsigned long_long_size = long_long_byte_size * CHAR_BIT;
479
+ if (apint.isIntN (long_long_size)) {
480
+ if (!literal->IsUnsigned () && apint.isIntN (long_long_size - 1 ))
481
+ return GetBasicType (type_system, lldb::eBasicTypeLongLong);
482
+ // If we still couldn't decide a type, we probably have something that
483
+ // does not fit in a signed long long, but has no U suffix. Also known as:
484
+ //
485
+ // warning: integer literal is too large to be represented in a signed
486
+ // integer type, interpreting as unsigned [-Wimplicitly-unsigned-literal]
487
+ //
488
+ return GetBasicType (type_system, lldb::eBasicTypeUnsignedLongLong);
489
+ }
490
+ return llvm::make_error<DILDiagnosticError>(
491
+ m_expr,
492
+ " integer literal is too large to be represented in any integer type" ,
493
+ literal->GetLocation ());
494
+ }
495
+ return llvm::make_error<DILDiagnosticError>(
496
+ m_expr, " unable to create a const literal" , literal->GetLocation ());
422
497
}
423
498
424
499
llvm::Expected<lldb::ValueObjectSP>
425
500
Interpreter::Visit (const ScalarLiteralNode *node) {
426
- CompilerType result_type =
427
- GetBasicTypeFromCU (m_exe_ctx_scope, node->GetType ());
428
- Scalar value = node->GetValue ();
429
- return ValueObject::CreateValueObjectFromScalar (m_target, value, result_type,
430
- " result" );
501
+ auto type_system = GetTypeSystemFromCU (m_exe_ctx_scope);
502
+ if (type_system) {
503
+ auto type = PickLiteralType (type_system, m_exe_ctx_scope, node);
504
+ if (type) {
505
+ Scalar scalar = node->GetValue ();
506
+ return ValueObject::CreateValueObjectFromScalar (m_target, scalar, *type,
507
+ " result" );
508
+ } else
509
+ return type.takeError ();
510
+ }
511
+ return llvm::make_error<DILDiagnosticError>(
512
+ m_expr, " unable to create a const literal" , node->GetLocation ());
431
513
}
432
514
433
515
} // namespace lldb_private::dil
0 commit comments