blob: 81f8ccef6dcaa8bae704edef6712fd71de6a4f95 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ValueObjectChild.cpp ------------------------------------*- 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#include "lldb/Core/ValueObjectChild.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/Core/Module.h"
13#include "lldb/Core/ValueObjectList.h"
14
Greg Clayton1674b122010-07-21 22:12:05 +000015#include "lldb/Symbol/ClangASTType.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Symbol/ObjectFile.h"
17#include "lldb/Symbol/SymbolContext.h"
18#include "lldb/Symbol/Type.h"
19#include "lldb/Symbol/Variable.h"
20
21#include "lldb/Target/ExecutionContext.h"
22#include "lldb/Target/Process.h"
23#include "lldb/Target/Target.h"
24
25using namespace lldb_private;
26
27ValueObjectChild::ValueObjectChild
28(
Jim Inghamfa3a16a2011-03-31 00:19:25 +000029 ValueObject &parent,
Chris Lattner24943d22010-06-08 16:52:24 +000030 clang::ASTContext *clang_ast,
31 void *clang_type,
32 const ConstString &name,
33 uint32_t byte_size,
34 int32_t byte_offset,
35 uint32_t bitfield_bit_size,
Greg Claytonbf8e42b2010-10-14 22:52:14 +000036 uint32_t bitfield_bit_offset,
Greg Clayton00c3ae72011-01-21 01:59:00 +000037 bool is_base_class,
Enrico Granata91544802011-09-06 19:20:51 +000038 bool is_deref_of_parent,
39 AddressType child_ptr_or_ref_addr_type
Chris Lattner24943d22010-06-08 16:52:24 +000040) :
Greg Claytonbf8e42b2010-10-14 22:52:14 +000041 ValueObject (parent),
Chris Lattner24943d22010-06-08 16:52:24 +000042 m_clang_ast (clang_ast),
43 m_clang_type (clang_type),
44 m_byte_size (byte_size),
45 m_byte_offset (byte_offset),
46 m_bitfield_bit_size (bitfield_bit_size),
Greg Claytonbf8e42b2010-10-14 22:52:14 +000047 m_bitfield_bit_offset (bitfield_bit_offset),
Greg Clayton00c3ae72011-01-21 01:59:00 +000048 m_is_base_class (is_base_class),
49 m_is_deref_of_parent (is_deref_of_parent)
Chris Lattner24943d22010-06-08 16:52:24 +000050{
51 m_name = name;
Enrico Granata91544802011-09-06 19:20:51 +000052 SetAddressTypeOfChildren(child_ptr_or_ref_addr_type);
Chris Lattner24943d22010-06-08 16:52:24 +000053}
54
55ValueObjectChild::~ValueObjectChild()
56{
57}
58
Chris Lattner24943d22010-06-08 16:52:24 +000059lldb::ValueType
60ValueObjectChild::GetValueType() const
61{
62 return m_parent->GetValueType();
63}
64
65uint32_t
66ValueObjectChild::CalculateNumChildren()
67{
Sean Callanan931acec2012-02-22 23:57:45 +000068 return ClangASTContext::GetNumChildren (GetClangAST (), GetClangType(), true);
Chris Lattner24943d22010-06-08 16:52:24 +000069}
70
Chris Lattner24943d22010-06-08 16:52:24 +000071ConstString
72ValueObjectChild::GetTypeName()
73{
74 if (m_type_name.IsEmpty())
75 {
Greg Claytondc0a38c2012-03-26 23:03:23 +000076 m_type_name = ClangASTType::GetConstTypeName (GetClangAST(), GetClangType());
Chris Lattner24943d22010-06-08 16:52:24 +000077 if (m_type_name)
78 {
79 if (m_bitfield_bit_size > 0)
80 {
81 const char *clang_type_name = m_type_name.AsCString();
82 if (clang_type_name)
83 {
Greg Clayton54e7afa2010-07-09 20:39:50 +000084 std::vector<char> bitfield_type_name (strlen(clang_type_name) + 32, 0);
Greg Clayton53d68e72010-07-20 22:52:08 +000085 ::snprintf (&bitfield_type_name.front(), bitfield_type_name.size(), "%s:%u", clang_type_name, m_bitfield_bit_size);
86 m_type_name.SetCString(&bitfield_type_name.front());
Chris Lattner24943d22010-06-08 16:52:24 +000087 }
88 }
89 }
90 }
91 return m_type_name;
92}
93
Greg Claytondc0a38c2012-03-26 23:03:23 +000094ConstString
95ValueObjectChild::GetQualifiedTypeName()
96{
97 ConstString qualified_name = ClangASTType::GetConstQualifiedTypeName (GetClangAST(), GetClangType());
98 if (qualified_name)
99 {
100 if (m_bitfield_bit_size > 0)
101 {
102 const char *clang_type_name = qualified_name.AsCString();
103 if (clang_type_name)
104 {
105 std::vector<char> bitfield_type_name (strlen(clang_type_name) + 32, 0);
106 ::snprintf (&bitfield_type_name.front(), bitfield_type_name.size(), "%s:%u", clang_type_name, m_bitfield_bit_size);
107 qualified_name.SetCString(&bitfield_type_name.front());
108 }
109 }
110 }
111 return qualified_name;
112}
113
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000114bool
115ValueObjectChild::UpdateValue ()
Chris Lattner24943d22010-06-08 16:52:24 +0000116{
117 m_error.Clear();
118 SetValueIsValid (false);
119 ValueObject* parent = m_parent;
120 if (parent)
121 {
Enrico Granataafb7c852011-08-02 17:27:39 +0000122 if (parent->UpdateValueIfNeeded(false))
Chris Lattner24943d22010-06-08 16:52:24 +0000123 {
Sean Callanan931acec2012-02-22 23:57:45 +0000124 m_value.SetContext(Value::eContextTypeClangType, GetClangType());
Chris Lattner24943d22010-06-08 16:52:24 +0000125
126 // Copy the parent scalar value and the scalar value type
127 m_value.GetScalar() = parent->GetValue().GetScalar();
128 Value::ValueType value_type = parent->GetValue().GetValueType();
129 m_value.SetValueType (value_type);
130
Greg Clayton462d4142010-09-29 01:12:09 +0000131 if (ClangASTContext::IsPointerOrReferenceType (parent->GetClangType()))
Chris Lattner24943d22010-06-08 16:52:24 +0000132 {
Enrico Granata91544802011-09-06 19:20:51 +0000133 lldb::addr_t addr = parent->GetPointerValue ();
Greg Claytonb2fbdad2011-08-16 00:44:29 +0000134 m_value.GetScalar() = addr;
Greg Claytona564ec62010-11-02 01:50:16 +0000135
136 if (addr == LLDB_INVALID_ADDRESS)
137 {
138 m_error.SetErrorString ("parent address is invalid.");
139 }
140 else if (addr == 0)
141 {
142 m_error.SetErrorString ("parent is NULL");
143 }
144 else
145 {
Greg Clayton2e810202010-11-02 21:21:20 +0000146 m_value.GetScalar() += m_byte_offset;
Enrico Granata91544802011-09-06 19:20:51 +0000147 AddressType addr_type = parent->GetAddressTypeOfChildren();
148
149 switch (addr_type)
150 {
151 case eAddressTypeFile:
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000152 {
153 lldb::ProcessSP process_sp (GetProcessSP());
154 if (process_sp && process_sp->IsAlive() == true)
155 m_value.SetValueType (Value::eValueTypeLoadAddress);
156 else
157 m_value.SetValueType(Value::eValueTypeFileAddress);
158 }
Enrico Granata91544802011-09-06 19:20:51 +0000159 break;
160 case eAddressTypeLoad:
161 m_value.SetValueType (Value::eValueTypeLoadAddress);
162 break;
163 case eAddressTypeHost:
164 m_value.SetValueType(Value::eValueTypeHostAddress);
165 break;
166 case eAddressTypeInvalid:
Enrico Granata91544802011-09-06 19:20:51 +0000167 // TODO: does this make sense?
168 m_value.SetValueType(Value::eValueTypeScalar);
169 break;
170 }
Greg Claytona564ec62010-11-02 01:50:16 +0000171 }
Chris Lattner24943d22010-06-08 16:52:24 +0000172 }
173 else
174 {
175 switch (value_type)
176 {
177 case Value::eValueTypeLoadAddress:
178 case Value::eValueTypeFileAddress:
179 case Value::eValueTypeHostAddress:
180 {
181 lldb::addr_t addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Claytona564ec62010-11-02 01:50:16 +0000182 if (addr == LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000183 {
Greg Claytona564ec62010-11-02 01:50:16 +0000184 m_error.SetErrorString ("parent address is invalid.");
Chris Lattner24943d22010-06-08 16:52:24 +0000185 }
Greg Claytona564ec62010-11-02 01:50:16 +0000186 else if (addr == 0)
187 {
188 m_error.SetErrorString ("parent is NULL");
189 }
190 else
191 {
192 // Set this object's scalar value to the address of its
Jim Ingham574c3d62011-08-12 23:34:31 +0000193 // value by adding its byte offset to the parent address
Greg Claytona564ec62010-11-02 01:50:16 +0000194 m_value.GetScalar() += GetByteOffset();
195 }
Chris Lattner24943d22010-06-08 16:52:24 +0000196 }
197 break;
198
199 case Value::eValueTypeScalar:
200 // TODO: What if this is a register value? Do we try and
201 // extract the child value from within the parent data?
202 // Probably...
203 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000204 m_error.SetErrorString ("parent has invalid value.");
Chris Lattner24943d22010-06-08 16:52:24 +0000205 break;
206 }
207 }
208
209 if (m_error.Success())
210 {
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000211 ExecutionContext exe_ctx (GetExecutionContextRef().Lock());
Greg Clayton3508c382012-02-24 01:59:29 +0000212 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST (), m_data, 0, GetModule().get());
Chris Lattner24943d22010-06-08 16:52:24 +0000213 }
214 }
215 else
216 {
Greg Clayton9c236732011-10-26 00:56:27 +0000217 m_error.SetErrorStringWithFormat("parent failed to evaluate: %s", parent->GetError().AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000218 }
219 }
220 else
221 {
222 m_error.SetErrorString("ValueObjectChild has a NULL parent ValueObject.");
223 }
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000224
225 return m_error.Success();
Chris Lattner24943d22010-06-08 16:52:24 +0000226}
227
228
229bool
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000230ValueObjectChild::IsInScope ()
Chris Lattner24943d22010-06-08 16:52:24 +0000231{
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000232 return m_parent->IsInScope ();
Chris Lattner24943d22010-06-08 16:52:24 +0000233}