LLDB mainline
RustVector.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8#include "RustLanguage.h"
9
13#include "lldb/lldb-forward.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/ErrorExtras.h"
16#include "llvm/Support/MathExtras.h"
17#include <optional>
18
19using namespace lldb;
20using namespace lldb_private;
21using namespace lldb_private::formatters;
22
23namespace lldb_private {
24namespace formatters {
26public:
28
30
31 llvm::Expected<uint32_t> CalculateNumChildren() override;
32
33 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
34
36
37 llvm::Expected<size_t> GetIndexOfChildWithName(llvm::StringRef name) override;
38
39private:
40 // m_start and m_len need to stay around because they are child objects
41 // who will follow their parent's life cycle; don't delete them in the
42 // destructor.
43 ValueObject *m_start = nullptr;
44 ValueObject *m_len = nullptr;
45
47 uint64_t m_element_size = 0;
48};
49
50} // namespace formatters
51} // namespace lldb_private
52
58
61
62llvm::Expected<uint32_t>
64 if (!m_len || !m_len->GetCompilerType().IsInteger())
65 return llvm::createStringError("Invalid vector length");
66 return m_len->GetValueAsUnsigned(0);
67}
68
71 uint32_t idx) {
72 if (!m_start || !m_len)
73 return lldb::ValueObjectSP();
74
76 return lldb::ValueObjectSP();
77
78 uint64_t address = m_start->GetValueAsUnsigned(0) + idx * m_element_size;
79 StreamString name;
80 name << llvm::formatv("[{0}]", idx);
81 return CreateChildValueObjectFromAddress(name.GetString(), address,
82 m_backend.GetExecutionContextRef(),
84}
85
88 m_start = m_len = nullptr;
89
90 m_start =
92 .GetChildAtNamePath({"buf", "inner", "ptr", "pointer", "pointer"})
93 .get();
94
95 // Newer versions of the rustc compiler omit the second "pointer" child.
96 if (!m_start)
97 m_start =
98 m_backend.GetChildAtNamePath({"buf", "inner", "ptr", "pointer"}).get();
99
100 m_len = m_backend.GetChildMemberWithName("len").get();
101
102 // Sanity check: Verify m_len < 2^(ptr_bit_size - 1)
103 uint64_t len = m_len->GetValueAsUnsigned(0);
104 lldb::TargetSP target_sp = m_len->GetTargetSP();
105 if (target_sp) {
106 uint32_t ptr_byte_size = target_sp->GetArchitecture().GetAddressByteSize();
107 uint32_t ptr_bit_size = ptr_byte_size * 8;
108 if (len > llvm::maxUIntN(ptr_bit_size - 1)) {
109 m_start = m_len = nullptr;
111 }
112 }
113
115 m_backend.GetCompilerType().GetNonReferenceType().GetTypeTemplateArgument(
116 0);
117
118 llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
119 if (!size_or_err) {
120 LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
121 "{0}");
122 m_start = m_len = nullptr;
124 }
125
126 m_element_size = *size_or_err;
127 if (m_element_size == 0) {
128 // Don't reset m_len; Vec<T> can validly have a 0-sized T.
129 m_start = nullptr;
131 }
132
134}
135
136llvm::Expected<size_t>
138 llvm::StringRef name) {
139 if (!m_start || !m_len)
140 return llvm::createStringErrorV("type has no child named '{0}'", name);
141 auto optional_idx = formatters::ExtractIndexFromString(name);
142 if (!optional_idx)
143 return llvm::createStringErrorV("type has no child named '{0}'", name);
144
145 return *optional_idx;
146}
147
151 if (!valobj_sp)
152 return nullptr;
153 CompilerType type = valobj_sp->GetCompilerType();
154 if (!type.IsValid() ||
156 return nullptr;
157 return new RustVectorSyntheticFrontEnd(valobj_sp);
158}
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:421
Generic representation of a type in a programming language.
size_t GetNumTemplateArguments(bool expand_pack=false) const
Return the number of template arguments the type has.
CompilerType GetNonReferenceType() const
If this type is a reference to a type (L value or R value reference), return a new type with the refe...
llvm::StringRef GetString() const
uint32_t CalculateNumChildrenIgnoringErrors(uint32_t max=UINT32_MAX)
lldb::ValueObjectSP CreateChildValueObjectFromAddress(llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx, CompilerType type, bool do_deref=true)
SyntheticChildrenFrontEnd(ValueObject &backend)
RustVectorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
llvm::Expected< uint32_t > CalculateNumChildren() override
llvm::Expected< size_t > GetIndexOfChildWithName(llvm::StringRef name) override
Determine the index of a named child.
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
std::optional< size_t > ExtractIndexFromString(llvm::StringRef item_name)
SyntheticChildrenFrontEnd * RustVectorSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
ChildCacheState
Specifies if children need to be re-computed after a call to SyntheticChildrenFrontEnd::Update.
@ eRefetch
Children need to be recomputed dynamically.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::Target > TargetSP