1 //===-- Value.h -------------------------------------------------*- C++ -*-===//
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 
9 #ifndef LLDB_CORE_VALUE_H
10 #define LLDB_CORE_VALUE_H
11 
12 #include "lldb/Symbol/CompilerType.h"
13 #include "lldb/Utility/DataBufferHeap.h"
14 #include "lldb/Utility/Scalar.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/lldb-enumerations.h"
17 #include "lldb/lldb-private-enumerations.h"
18 #include "lldb/lldb-private-types.h"
19 
20 #include "llvm/ADT/APInt.h"
21 
22 #include <vector>
23 
24 #include <cstdint>
25 #include <cstring>
26 
27 namespace lldb_private {
28 class DataExtractor;
29 class ExecutionContext;
30 class Module;
31 class Stream;
32 class Type;
33 class Variable;
34 }
35 
36 namespace lldb_private {
37 
38 class Value {
39 public:
40   /// Type that describes Value::m_value.
41   enum class ValueType {
42     Invalid = -1,
43     // m_value contains:
44     /// A raw scalar value.
45     Scalar = 0,
46     /// A file address value.
47     FileAddress,
48     /// A load address value.
49     LoadAddress,
50     /// A host address value (for memory in the process that < A is
51     /// using liblldb).
52     HostAddress
53   };
54 
55   /// Type that describes Value::m_context.
56   enum class ContextType {
57     // m_context contains:
58     /// Undefined.
59     Invalid = -1,
60     /// RegisterInfo * (can be a scalar or a vector register).
61     RegisterInfo = 0,
62     /// lldb_private::Type *.
63     LLDBType,
64     /// lldb_private::Variable *.
65     Variable
66   };
67 
68   Value();
69   Value(const Scalar &scalar);
70   Value(const void *bytes, int len);
71   Value(const Value &rhs);
72 
73   void SetBytes(const void *bytes, int len);
74 
75   void AppendBytes(const void *bytes, int len);
76 
77   Value &operator=(const Value &rhs);
78 
79   const CompilerType &GetCompilerType();
80 
81   void SetCompilerType(const CompilerType &compiler_type);
82 
83   ValueType GetValueType() const;
84 
85   AddressType GetValueAddressType() const;
86 
87   ContextType GetContextType() const { return m_context_type; }
88 
89   void SetValueType(ValueType value_type) { m_value_type = value_type; }
90 
91   void ClearContext() {
92     m_context = nullptr;
93     m_context_type = ContextType::Invalid;
94   }
95 
96   void SetContext(ContextType context_type, void *p) {
97     m_context_type = context_type;
98     m_context = p;
99     if (m_context_type == ContextType::RegisterInfo) {
100       RegisterInfo *reg_info = GetRegisterInfo();
101       if (reg_info->encoding == lldb::eEncodingVector)
102         SetValueType(ValueType::Scalar);
103     }
104   }
105 
106   RegisterInfo *GetRegisterInfo() const;
107 
108   Type *GetType();
109 
110   Scalar &ResolveValue(ExecutionContext *exe_ctx);
111 
112   const Scalar &GetScalar() const { return m_value; }
113 
114   Scalar &GetScalar() { return m_value; }
115 
116   size_t ResizeData(size_t len);
117 
118   size_t AppendDataToHostBuffer(const Value &rhs);
119 
120   DataBufferHeap &GetBuffer() { return m_data_buffer; }
121 
122   const DataBufferHeap &GetBuffer() const { return m_data_buffer; }
123 
124   bool ValueOf(ExecutionContext *exe_ctx);
125 
126   Variable *GetVariable();
127 
128   void Dump(Stream *strm);
129 
130   lldb::Format GetValueDefaultFormat();
131 
132   uint64_t GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx);
133 
134   Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data,
135                         Module *module); // Can be nullptr
136 
137   static const char *GetValueTypeAsCString(ValueType context_type);
138 
139   static const char *GetContextTypeAsCString(ContextType context_type);
140 
141   /// Convert this value's file address to a load address, if possible.
142   void ConvertToLoadAddress(Module *module, Target *target);
143 
144   bool GetData(DataExtractor &data);
145 
146   void Clear();
147 
148 protected:
149   Scalar m_value;
150   CompilerType m_compiler_type;
151   void *m_context = nullptr;
152   ValueType m_value_type = ValueType::Scalar;
153   ContextType m_context_type = ContextType::Invalid;
154   DataBufferHeap m_data_buffer;
155 };
156 
157 class ValueList {
158 public:
159   ValueList() {}
160 
161   ValueList(const ValueList &rhs);
162 
163   ~ValueList() = default;
164 
165   const ValueList &operator=(const ValueList &rhs);
166 
167   // void InsertValue (Value *value, size_t idx);
168   void PushValue(const Value &value);
169 
170   size_t GetSize();
171   Value *GetValueAtIndex(size_t idx);
172   void Clear();
173 
174 private:
175   typedef std::vector<Value> collection;
176 
177   collection m_values;
178 };
179 
180 } // namespace lldb_private
181 
182 #endif // LLDB_CORE_VALUE_H
183