Value.h revision b335e7090f1c6579d8bf2915f6a4a0ba98be15ef
1//===-- Value.h -------------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_Value_h_
11#define liblldb_Value_h_
12
13// C Includes
14// C++ Includes
15#include <string>
16#include <vector>
17// Other libraries and framework includes
18// Project includes
19#include "lldb/lldb-private.h"
20#include "lldb/Core/ClangForward.h"
21#include "lldb/Core/DataBufferHeap.h"
22#include "lldb/Core/Error.h"
23#include "lldb/Core/Scalar.h"
24
25namespace lldb_private {
26
27class Value
28{
29public:
30
31    // Values Less than zero are an error, greater than or equal to zero
32    // returns what the Scalar result is.
33    enum ValueType
34    {
35                                        // m_value contains...
36                                        // ============================
37        eValueTypeScalar,               // raw scalar value
38        eValueTypeVector,               // byte array of m_vector.length with endianness of m_vector.byte_order
39        eValueTypeFileAddress,          // file address value
40        eValueTypeLoadAddress,          // load address value
41        eValueTypeHostAddress           // host address value (for memory in the process that is using liblldb)
42    };
43
44    enum ContextType                    // Type that describes Value::m_context
45    {
46                                        // m_context contains...
47                                        // ====================
48        eContextTypeInvalid,            // undefined
49        eContextTypeClangType,          // void * (an opaque clang::QualType * that can be fed to "static QualType QualType::getFromOpaquePtr(void *)")
50        eContextTypeRegisterInfo,       // RegisterInfo * (can be a scalar or a vector register)
51        eContextTypeLLDBType,           // lldb_private::Type *
52        eContextTypeVariable            // lldb_private::Variable *
53    };
54
55    const static size_t kMaxByteSize = 32u;
56
57    struct Vector
58    {
59        // The byte array must be big enough to hold vector registers for any supported target.
60        uint8_t bytes[kMaxByteSize];
61        size_t length;
62        lldb::ByteOrder byte_order;
63
64        Vector() :
65			length(0),
66			byte_order(lldb::eByteOrderInvalid)
67        {
68		}
69
70        Vector(const Vector& vector)
71		{ *this = vector;
72        }
73        const Vector&
74		operator=(const Vector& vector)
75		{
76            SetBytes(vector.bytes, vector.length, vector.byte_order);
77            return *this;
78        }
79
80        bool
81		SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
82		{
83            this->length = length;
84            this->byte_order = byte_order;
85            if (length)
86                ::memcpy(this->bytes, bytes, length < kMaxByteSize ? length : kMaxByteSize);
87            return IsValid();
88        }
89
90        bool
91		IsValid() const
92		{
93            return (length > 0 && length < kMaxByteSize && byte_order != lldb::eByteOrderInvalid);
94        }
95        // Casts a vector, if valid, to an unsigned int of matching or largest supported size.
96        // Truncates to the beginning of the vector if required.
97        // Returns a default constructed Scalar if the Vector data is internally inconsistent.
98        Scalar
99		GetAsScalar() const
100		{
101            Scalar scalar;
102            if (IsValid())
103            {
104                if (length == 1) scalar = *(const uint8_t *)bytes;
105                else if (length == 2) scalar = *(const uint16_t *)bytes;
106                else if (length == 4) scalar = *(const uint32_t *)bytes;
107                else if (length == 8) scalar = *(const uint64_t *)bytes;
108#if defined (ENABLE_128_BIT_SUPPORT)
109                else if (length >= 16) scalar = *(const __uint128_t *)bytes;
110#else
111                else if (length >= 16) scalar = *(const __uint64_t *)bytes;
112#endif
113            }
114            return scalar;
115        }
116    };
117
118    Value();
119    Value(const Scalar& scalar);
120    Value(const Vector& vector);
121    Value(const uint8_t *bytes, int len);
122    Value(const Value &rhs);
123
124    Value &
125    operator=(const Value &rhs);
126
127    lldb::clang_type_t
128    GetClangType();
129
130    ValueType
131    GetValueType() const;
132
133    AddressType
134    GetValueAddressType () const;
135
136    ContextType
137    GetContextType() const
138    {
139        return m_context_type;
140    }
141
142    void
143    SetValueType (ValueType value_type)
144    {
145        m_value_type = value_type;
146    }
147
148    void
149    ClearContext ()
150    {
151        m_context = NULL;
152        m_context_type = eContextTypeInvalid;
153    }
154
155    void
156    SetContext (ContextType context_type, void *p)
157    {
158        m_context_type = context_type;
159        m_context = p;
160        if (m_context_type == eContextTypeRegisterInfo) {
161            RegisterInfo *reg_info = GetRegisterInfo();
162            if (reg_info->encoding == lldb::eEncodingVector)
163                SetValueType(eValueTypeVector);
164            else
165                SetValueType(eValueTypeScalar);
166        }
167    }
168
169    RegisterInfo *
170    GetRegisterInfo() const;
171
172    Type *
173    GetType();
174
175    Scalar &
176    ResolveValue (ExecutionContext *exe_ctx, clang::ASTContext *ast_context);
177
178    const Scalar &
179    GetScalar() const
180    {
181        return m_value;
182    }
183
184    const Vector &
185    GetVector() const
186    {
187        return m_vector;
188    }
189
190    Scalar &
191    GetScalar()
192    {
193        return m_value;
194    }
195
196    Vector &
197    GetVector()
198    {
199        return m_vector;
200    }
201
202    bool
203    SetVectorBytes(const Vector& vector)
204	{
205        m_vector = vector;
206        return m_vector.IsValid();
207    }
208
209    bool
210    SetVectorBytes(uint8_t *bytes, size_t length, lldb::ByteOrder byte_order)
211	{
212        return m_vector.SetBytes(bytes, length, byte_order);
213    }
214
215    bool
216    SetScalarFromVector()
217	{
218        if (m_vector.IsValid())
219		{
220            m_value = m_vector.GetAsScalar();
221            return true;
222        }
223        return false;
224    }
225
226    void
227    ResizeData(size_t len);
228
229    bool
230    ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context);
231
232    Variable *
233    GetVariable();
234
235    void
236    Dump (Stream* strm);
237
238    lldb::Format
239    GetValueDefaultFormat ();
240
241    uint64_t
242    GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr);
243
244    Error
245    GetValueAsData (ExecutionContext *exe_ctx,
246                    clang::ASTContext *ast_context,
247                    DataExtractor &data,
248                    uint32_t data_offset,
249                    Module *module);     // Can be NULL
250
251    static const char *
252    GetValueTypeAsCString (ValueType context_type);
253
254    static const char *
255    GetContextTypeAsCString (ContextType context_type);
256
257    bool
258    GetData (DataExtractor &data);
259
260protected:
261    Scalar          m_value;
262    Vector          m_vector;
263    ValueType       m_value_type;
264    void *          m_context;
265    ContextType     m_context_type;
266    DataBufferHeap  m_data_buffer;
267};
268
269class ValueList
270{
271public:
272    ValueList () :
273        m_values()
274    {
275    }
276
277    ValueList (const ValueList &rhs);
278
279    ~ValueList ()
280    {
281    }
282
283    const ValueList & operator= (const ValueList &rhs);
284
285    // void InsertValue (Value *value, size_t idx);
286    void PushValue (const Value &value);
287
288    size_t GetSize ();
289    Value *GetValueAtIndex(size_t idx);
290    void Clear();
291
292protected:
293
294private:
295    typedef std::vector<Value> collection;
296
297    collection m_values;
298};
299
300} // namespace lldb_private
301
302#endif  // liblldb_Value_h_
303