Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ClangExpressionDeclMap.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/Expression/ClangExpressionDeclMap.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/lldb-private.h" |
| 17 | #include "lldb/Core/Address.h" |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Error.h" |
Sean Callanan | 6184dfe | 2010-06-23 00:47:48 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Log.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Module.h" |
| 21 | #include "lldb/Expression/ClangASTSource.h" |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 22 | #include "lldb/Expression/ClangPersistentVariables.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/ClangASTContext.h" |
| 24 | #include "lldb/Symbol/CompileUnit.h" |
| 25 | #include "lldb/Symbol/Function.h" |
| 26 | #include "lldb/Symbol/ObjectFile.h" |
| 27 | #include "lldb/Symbol/SymbolContext.h" |
| 28 | #include "lldb/Symbol/Type.h" |
| 29 | #include "lldb/Symbol/TypeList.h" |
| 30 | #include "lldb/Symbol/Variable.h" |
| 31 | #include "lldb/Symbol/VariableList.h" |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 32 | #include "lldb/Target/ExecutionContext.h" |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 33 | #include "lldb/Target/Process.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | #include "lldb/Target/StackFrame.h" |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 35 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | using namespace lldb_private; |
| 38 | using namespace clang; |
| 39 | |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 40 | ClangExpressionDeclMap::ClangExpressionDeclMap(ExecutionContext *exe_ctx) : |
| 41 | m_exe_ctx(exe_ctx), m_struct_laid_out(false), |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 42 | m_materialized_location(0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | { |
| 44 | if (exe_ctx && exe_ctx->frame) |
| 45 | m_sym_ctx = new SymbolContext(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything)); |
| 46 | else |
| 47 | m_sym_ctx = NULL; |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 48 | |
| 49 | if (exe_ctx && exe_ctx->process) |
| 50 | m_persistent_vars = &exe_ctx->process->GetPersistentVariables(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | ClangExpressionDeclMap::~ClangExpressionDeclMap() |
| 54 | { |
| 55 | uint32_t num_tuples = m_tuples.size (); |
| 56 | uint32_t tuple_index; |
| 57 | |
| 58 | for (tuple_index = 0; tuple_index < num_tuples; ++tuple_index) |
| 59 | delete m_tuples[tuple_index].m_value; |
| 60 | |
| 61 | if (m_sym_ctx) |
| 62 | delete m_sym_ctx; |
| 63 | } |
| 64 | |
| 65 | bool |
| 66 | ClangExpressionDeclMap::GetIndexForDecl (uint32_t &index, |
| 67 | const clang::Decl *decl) |
| 68 | { |
| 69 | uint32_t num_tuples = m_tuples.size (); |
| 70 | uint32_t tuple_index; |
| 71 | |
| 72 | for (tuple_index = 0; tuple_index < num_tuples; ++tuple_index) |
| 73 | { |
| 74 | if (m_tuples[tuple_index].m_decl == decl) |
| 75 | { |
| 76 | index = tuple_index; |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return false; |
| 82 | } |
| 83 | |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 84 | // Interface for IRForTarget |
| 85 | |
Sean Callanan | 82b74c8 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 86 | void |
| 87 | ClangExpressionDeclMap::GetPersistentResultName (std::string &name) |
| 88 | { |
| 89 | m_persistent_vars->GetNextResultName(m_result_name); |
| 90 | |
| 91 | name = m_result_name; |
| 92 | } |
| 93 | |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 94 | bool |
Sean Callanan | 82b74c8 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 95 | ClangExpressionDeclMap::AddPersistentVariable (const char *name, TypeFromParser parser_type) |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 96 | { |
| 97 | clang::ASTContext *context(m_exe_ctx->target->GetScratchClangASTContext()->getASTContext()); |
| 98 | |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 99 | TypeFromUser user_type(ClangASTContext::CopyType(context, |
Sean Callanan | 82b74c8 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 100 | parser_type.GetASTContext(), |
| 101 | parser_type.GetOpaqueQualType()), |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 102 | context); |
| 103 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 104 | return m_persistent_vars->CreatePersistentVariable (name, user_type); |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 108 | ClangExpressionDeclMap::AddValueToStruct (llvm::Value *value, |
| 109 | const clang::NamedDecl *decl, |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 110 | std::string &name, |
Sean Callanan | a386e05 | 2010-08-13 22:29:54 +0000 | [diff] [blame] | 111 | TypeFromParser type, |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 112 | size_t size, |
| 113 | off_t alignment) |
| 114 | { |
| 115 | m_struct_laid_out = false; |
| 116 | |
| 117 | StructMemberIterator iter; |
| 118 | |
| 119 | for (iter = m_members.begin(); |
| 120 | iter != m_members.end(); |
| 121 | ++iter) |
| 122 | { |
| 123 | if (iter->m_decl == decl) |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | StructMember member; |
| 128 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 129 | member.m_value = value; |
| 130 | member.m_decl = decl; |
| 131 | member.m_name = name; |
Sean Callanan | a386e05 | 2010-08-13 22:29:54 +0000 | [diff] [blame] | 132 | member.m_parser_type = type; |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 133 | member.m_offset = 0; |
| 134 | member.m_size = size; |
| 135 | member.m_alignment = alignment; |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 136 | |
| 137 | m_members.push_back(member); |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | bool |
| 143 | ClangExpressionDeclMap::DoStructLayout () |
| 144 | { |
| 145 | if (m_struct_laid_out) |
| 146 | return true; |
| 147 | |
| 148 | StructMemberIterator iter; |
| 149 | |
| 150 | off_t cursor = 0; |
| 151 | |
| 152 | m_struct_alignment = 0; |
| 153 | m_struct_size = 0; |
| 154 | |
| 155 | for (iter = m_members.begin(); |
| 156 | iter != m_members.end(); |
| 157 | ++iter) |
| 158 | { |
| 159 | if (iter == m_members.begin()) |
| 160 | m_struct_alignment = iter->m_alignment; |
| 161 | |
| 162 | if (cursor % iter->m_alignment) |
| 163 | cursor += (iter->m_alignment - (cursor % iter->m_alignment)); |
| 164 | |
| 165 | iter->m_offset = cursor; |
| 166 | cursor += iter->m_size; |
| 167 | } |
| 168 | |
| 169 | m_struct_size = cursor; |
| 170 | |
| 171 | m_struct_laid_out = true; |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | bool ClangExpressionDeclMap::GetStructInfo (uint32_t &num_elements, |
| 176 | size_t &size, |
| 177 | off_t &alignment) |
| 178 | { |
| 179 | if (!m_struct_laid_out) |
| 180 | return false; |
| 181 | |
| 182 | num_elements = m_members.size(); |
| 183 | size = m_struct_size; |
| 184 | alignment = m_struct_alignment; |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | bool |
| 190 | ClangExpressionDeclMap::GetStructElement (const clang::NamedDecl *&decl, |
| 191 | llvm::Value *&value, |
| 192 | off_t &offset, |
| 193 | uint32_t index) |
| 194 | { |
| 195 | if (!m_struct_laid_out) |
| 196 | return false; |
| 197 | |
| 198 | if (index >= m_members.size()) |
| 199 | return false; |
| 200 | |
| 201 | decl = m_members[index].m_decl; |
| 202 | value = m_members[index].m_value; |
| 203 | offset = m_members[index].m_offset; |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
Sean Callanan | 02fbafa | 2010-07-27 21:39:39 +0000 | [diff] [blame] | 208 | bool |
| 209 | ClangExpressionDeclMap::GetFunctionInfo (const clang::NamedDecl *decl, |
| 210 | llvm::Value**& value, |
| 211 | uint64_t &ptr) |
Sean Callanan | ba992c5 | 2010-07-27 02:07:53 +0000 | [diff] [blame] | 212 | { |
| 213 | TupleIterator iter; |
| 214 | |
| 215 | for (iter = m_tuples.begin(); |
| 216 | iter != m_tuples.end(); |
| 217 | ++iter) |
| 218 | { |
| 219 | if (decl == iter->m_decl) |
| 220 | { |
Sean Callanan | 02fbafa | 2010-07-27 21:39:39 +0000 | [diff] [blame] | 221 | value = &iter->m_llvm_value; |
| 222 | ptr = iter->m_value->GetScalar().ULongLong(); |
| 223 | return true; |
Sean Callanan | ba992c5 | 2010-07-27 02:07:53 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Sean Callanan | 02fbafa | 2010-07-27 21:39:39 +0000 | [diff] [blame] | 227 | return false; |
Sean Callanan | ba992c5 | 2010-07-27 02:07:53 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Sean Callanan | f5857a0 | 2010-07-31 01:32:05 +0000 | [diff] [blame] | 230 | bool |
| 231 | ClangExpressionDeclMap::GetFunctionAddress (const char *name, |
| 232 | uint64_t &ptr) |
| 233 | { |
| 234 | // Back out in all cases where we're not fully initialized |
| 235 | if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx) |
| 236 | return false; |
| 237 | |
| 238 | ConstString name_cs(name); |
| 239 | SymbolContextList sym_ctxs; |
| 240 | |
| 241 | m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs); |
| 242 | |
| 243 | if (!sym_ctxs.GetSize()) |
| 244 | return false; |
| 245 | |
| 246 | SymbolContext sym_ctx; |
| 247 | sym_ctxs.GetContextAtIndex(0, sym_ctx); |
| 248 | |
| 249 | const Address *fun_address; |
| 250 | |
| 251 | if (sym_ctx.function) |
| 252 | fun_address = &sym_ctx.function->GetAddressRange().GetBaseAddress(); |
| 253 | else if (sym_ctx.symbol) |
| 254 | fun_address = &sym_ctx.symbol->GetAddressRangeRef().GetBaseAddress(); |
| 255 | else |
| 256 | return false; |
| 257 | |
| 258 | ptr = fun_address->GetLoadAddress(m_exe_ctx->process); |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | // Interface for DwarfExpression |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 264 | lldb_private::Value |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 265 | *ClangExpressionDeclMap::GetValueForIndex (uint32_t index) |
| 266 | { |
| 267 | if (index >= m_tuples.size ()) |
| 268 | return NULL; |
| 269 | |
| 270 | return m_tuples[index].m_value; |
| 271 | } |
| 272 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 273 | // Interface for CommandObjectExpression |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 274 | |
| 275 | bool |
| 276 | ClangExpressionDeclMap::Materialize (ExecutionContext *exe_ctx, |
| 277 | lldb::addr_t &struct_address, |
| 278 | Error &err) |
| 279 | { |
| 280 | bool result = DoMaterialize(false, exe_ctx, NULL, err); |
| 281 | |
| 282 | if (result) |
| 283 | struct_address = m_materialized_location; |
| 284 | |
| 285 | return result; |
| 286 | } |
| 287 | |
| 288 | bool |
| 289 | ClangExpressionDeclMap::Dematerialize (ExecutionContext *exe_ctx, |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 290 | ClangExpressionVariable *&result, |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 291 | Error &err) |
| 292 | { |
Sean Callanan | 82b74c8 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 293 | return DoMaterialize(true, exe_ctx, &result, err); |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Sean Callanan | 32824aa | 2010-07-23 22:19:18 +0000 | [diff] [blame] | 296 | bool |
| 297 | ClangExpressionDeclMap::DumpMaterializedStruct(ExecutionContext *exe_ctx, |
| 298 | Stream &s, |
| 299 | Error &err) |
| 300 | { |
| 301 | if (!m_struct_laid_out) |
| 302 | { |
| 303 | err.SetErrorString("Structure hasn't been laid out yet"); |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | if (!exe_ctx) |
| 308 | { |
| 309 | err.SetErrorString("Received null execution context"); |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | if (!exe_ctx->process) |
| 315 | { |
| 316 | err.SetErrorString("Couldn't find the process"); |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | if (!exe_ctx->target) |
| 321 | { |
| 322 | err.SetErrorString("Couldn't find the target"); |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | lldb::DataBufferSP data(new DataBufferHeap(m_struct_size, 0)); |
| 327 | |
| 328 | Error error; |
| 329 | if (exe_ctx->process->ReadMemory (m_materialized_location, data->GetBytes(), data->GetByteSize(), error) != data->GetByteSize()) |
| 330 | { |
| 331 | err.SetErrorStringWithFormat ("Couldn't read struct from the target: %s", error.AsCString()); |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | DataExtractor extractor(data, exe_ctx->process->GetByteOrder(), exe_ctx->target->GetArchitecture().GetAddressByteSize()); |
| 336 | |
| 337 | StructMemberIterator iter; |
| 338 | |
| 339 | for (iter = m_members.begin(); |
| 340 | iter != m_members.end(); |
| 341 | ++iter) |
| 342 | { |
| 343 | s.Printf("[%s]\n", iter->m_name.c_str()); |
| 344 | |
| 345 | extractor.Dump(&s, // stream |
| 346 | iter->m_offset, // offset |
| 347 | lldb::eFormatBytesWithASCII, // format |
| 348 | 1, // byte size of individual entries |
| 349 | iter->m_size, // number of entries |
| 350 | 16, // entries per line |
| 351 | m_materialized_location + iter->m_offset, // address to print |
| 352 | 0, // bit size (bitfields only; 0 means ignore) |
| 353 | 0); // bit alignment (bitfields only; 0 means ignore) |
| 354 | |
| 355 | s.PutChar('\n'); |
| 356 | } |
| 357 | |
| 358 | return true; |
| 359 | } |
| 360 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 361 | bool |
| 362 | ClangExpressionDeclMap::DoMaterialize (bool dematerialize, |
| 363 | ExecutionContext *exe_ctx, |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 364 | ClangExpressionVariable **result, |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 365 | Error &err) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 366 | { |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 367 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
Sean Callanan | 82b74c8 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 368 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 369 | if (!m_struct_laid_out) |
| 370 | { |
| 371 | err.SetErrorString("Structure hasn't been laid out yet"); |
| 372 | return LLDB_INVALID_ADDRESS; |
| 373 | } |
| 374 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 375 | if (!exe_ctx) |
| 376 | { |
| 377 | err.SetErrorString("Received null execution context"); |
| 378 | return LLDB_INVALID_ADDRESS; |
| 379 | } |
| 380 | |
Sean Callanan | 4583927 | 2010-07-24 01:37:44 +0000 | [diff] [blame] | 381 | if (!exe_ctx->frame) |
| 382 | { |
| 383 | err.SetErrorString("Received null execution frame"); |
| 384 | return LLDB_INVALID_ADDRESS; |
| 385 | } |
| 386 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 387 | const SymbolContext &sym_ctx(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything)); |
| 388 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 389 | if (!dematerialize) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 390 | { |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 391 | if (m_materialized_location) |
| 392 | { |
| 393 | exe_ctx->process->DeallocateMemory(m_materialized_location); |
| 394 | m_materialized_location = 0; |
| 395 | } |
| 396 | |
| 397 | lldb::addr_t mem = exe_ctx->process->AllocateMemory(m_struct_alignment + m_struct_size, |
| 398 | lldb::ePermissionsReadable | lldb::ePermissionsWritable, |
| 399 | err); |
| 400 | |
| 401 | if (mem == LLDB_INVALID_ADDRESS) |
| 402 | return false; |
| 403 | |
| 404 | m_allocated_area = mem; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 407 | m_materialized_location = m_allocated_area; |
| 408 | |
| 409 | if (m_materialized_location % m_struct_alignment) |
| 410 | { |
| 411 | m_materialized_location += (m_struct_alignment - (m_materialized_location % m_struct_alignment)); |
| 412 | } |
| 413 | |
| 414 | StructMemberIterator iter; |
| 415 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 416 | for (iter = m_members.begin(); |
| 417 | iter != m_members.end(); |
| 418 | ++iter) |
| 419 | { |
| 420 | uint32_t tuple_index; |
| 421 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 422 | if (!GetIndexForDecl(tuple_index, iter->m_decl)) |
| 423 | { |
Sean Callanan | 82b74c8 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 424 | if (iter->m_name.find("___clang_expr_result") != std::string::npos) |
| 425 | { |
| 426 | if (dematerialize) |
| 427 | { |
| 428 | // Here we pick up the odd anomaly produced by |
| 429 | // IRForTarget::createResultVariable (and described in a comment |
| 430 | // there). |
| 431 | // |
| 432 | // We rename the variable to the name of the result PVar and |
| 433 | // incidentally drop the address of the PVar into *result |
| 434 | // (if it's non-NULL, of course). We then let this case fall |
| 435 | // through to the persistent variable handler. |
| 436 | |
| 437 | if (log) |
| 438 | log->PutCString("Found result member in the struct"); |
| 439 | |
| 440 | iter->m_name = m_result_name; |
| 441 | |
| 442 | if (result) |
| 443 | { |
| 444 | if (log) |
| 445 | log->PutCString("Returning result PVar"); |
| 446 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 447 | *result = m_persistent_vars->GetVariable(m_result_name.c_str()); |
Sean Callanan | 82b74c8 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 448 | |
| 449 | if (!*result) |
| 450 | { |
| 451 | err.SetErrorStringWithFormat("Couldn't find persistent variable for result %s", m_result_name.c_str()); |
| 452 | } |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | if (log) |
| 457 | log->PutCString("Didn't return result PVar; pointer was NULL"); |
| 458 | } |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | // The result variable doesn't need to be materialized, ever. |
| 463 | continue; |
| 464 | } |
| 465 | } |
| 466 | |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 467 | if (iter->m_name[0] == '$') |
| 468 | { |
| 469 | if (!DoMaterializeOnePersistentVariable(dematerialize, *exe_ctx, iter->m_name.c_str(), m_materialized_location + iter->m_offset, err)) |
| 470 | return false; |
| 471 | } |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 472 | else |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 473 | { |
| 474 | err.SetErrorStringWithFormat("Unexpected variable %s", iter->m_name.c_str()); |
| 475 | return false; |
| 476 | } |
| 477 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 478 | continue; |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 479 | } |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 480 | |
| 481 | Tuple &tuple(m_tuples[tuple_index]); |
| 482 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 483 | if (!DoMaterializeOneVariable(dematerialize, *exe_ctx, sym_ctx, iter->m_name.c_str(), tuple.m_user_type, m_materialized_location + iter->m_offset, err)) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 484 | return false; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 487 | return true; |
| 488 | } |
| 489 | |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 490 | bool |
| 491 | ClangExpressionDeclMap::DoMaterializeOnePersistentVariable(bool dematerialize, |
| 492 | ExecutionContext &exe_ctx, |
| 493 | const char *name, |
| 494 | lldb::addr_t addr, |
| 495 | Error &err) |
| 496 | { |
| 497 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 498 | |
| 499 | if (log) |
| 500 | log->Printf("Found persistent variable %s", name); |
| 501 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 502 | ClangExpressionVariable *pvar(m_persistent_vars->GetVariable(name)); |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 503 | |
| 504 | if (!pvar) |
| 505 | { |
| 506 | err.SetErrorStringWithFormat("Undefined persistent variable %s", name); |
| 507 | return LLDB_INVALID_ADDRESS; |
| 508 | } |
| 509 | |
| 510 | size_t pvar_size = pvar->Size(); |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 511 | |
| 512 | if (!pvar->m_data_vars.get()) |
| 513 | return false; |
| 514 | |
| 515 | uint8_t *pvar_data = pvar->m_data_vars->m_data->GetBytes(); |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 516 | Error error; |
| 517 | |
| 518 | if (dematerialize) |
| 519 | { |
| 520 | if (exe_ctx.process->ReadMemory (addr, pvar_data, pvar_size, error) != pvar_size) |
| 521 | { |
| 522 | err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString()); |
| 523 | return false; |
| 524 | } |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | if (exe_ctx.process->WriteMemory (addr, pvar_data, pvar_size, error) != pvar_size) |
| 529 | { |
| 530 | err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString()); |
| 531 | return false; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | return true; |
| 536 | } |
| 537 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 538 | bool |
| 539 | ClangExpressionDeclMap::DoMaterializeOneVariable(bool dematerialize, |
| 540 | ExecutionContext &exe_ctx, |
| 541 | const SymbolContext &sym_ctx, |
| 542 | const char *name, |
| 543 | TypeFromUser type, |
| 544 | lldb::addr_t addr, |
| 545 | Error &err) |
| 546 | { |
| 547 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 548 | |
| 549 | Variable *var = FindVariableInScope(sym_ctx, name, &type); |
| 550 | |
| 551 | if (!var) |
| 552 | { |
| 553 | err.SetErrorStringWithFormat("Couldn't find %s with appropriate type", name); |
| 554 | return false; |
| 555 | } |
| 556 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 557 | if (log) |
| 558 | log->Printf("%s %s with type %p", (dematerialize ? "Dematerializing" : "Materializing"), name, type.GetOpaqueQualType()); |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 559 | |
| 560 | std::auto_ptr<lldb_private::Value> location_value(GetVariableValue(exe_ctx, |
| 561 | var, |
| 562 | type.GetASTContext())); |
| 563 | |
| 564 | if (!location_value.get()) |
| 565 | { |
| 566 | err.SetErrorStringWithFormat("Couldn't get value for %s", name); |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | if (location_value->GetValueType() == Value::eValueTypeLoadAddress) |
| 571 | { |
| 572 | lldb::addr_t value_addr = location_value->GetScalar().ULongLong(); |
| 573 | |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 574 | size_t bit_size = ClangASTType::GetClangTypeBitWidth(type.GetASTContext(), type.GetOpaqueQualType()); |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 575 | size_t byte_size = bit_size % 8 ? ((bit_size + 8) / 8) : (bit_size / 8); |
| 576 | |
| 577 | DataBufferHeap data; |
| 578 | data.SetByteSize(byte_size); |
| 579 | |
| 580 | lldb::addr_t src_addr; |
| 581 | lldb::addr_t dest_addr; |
| 582 | |
| 583 | if (dematerialize) |
| 584 | { |
| 585 | src_addr = addr; |
| 586 | dest_addr = value_addr; |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | src_addr = value_addr; |
| 591 | dest_addr = addr; |
| 592 | } |
| 593 | |
| 594 | Error error; |
| 595 | if (exe_ctx.process->ReadMemory (src_addr, data.GetBytes(), byte_size, error) != byte_size) |
| 596 | { |
| 597 | err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString()); |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | if (exe_ctx.process->WriteMemory (dest_addr, data.GetBytes(), byte_size, error) != byte_size) |
| 602 | { |
| 603 | err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString()); |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | if (log) |
| 608 | log->Printf("Copied from 0x%llx to 0x%llx", (uint64_t)src_addr, (uint64_t)addr); |
| 609 | } |
| 610 | else |
| 611 | { |
| 612 | StreamString ss; |
| 613 | |
| 614 | location_value->Dump(&ss); |
| 615 | |
| 616 | err.SetErrorStringWithFormat("%s has a value of unhandled type: %s", name, ss.GetString().c_str()); |
| 617 | } |
| 618 | |
| 619 | return true; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 622 | Variable* |
| 623 | ClangExpressionDeclMap::FindVariableInScope(const SymbolContext &sym_ctx, |
| 624 | const char *name, |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 625 | TypeFromUser *type) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 626 | { |
| 627 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 628 | |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 629 | if (m_sym_ctx->function == NULL || m_sym_ctx->block == NULL) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 630 | { |
| 631 | if (log) |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 632 | log->Printf("function = %p, block = %p", m_sym_ctx->function, m_sym_ctx->block); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 633 | return NULL; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 636 | ConstString name_cs(name); |
| 637 | |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 638 | Block *current_block; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 639 | |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame^] | 640 | for (current_block = m_sym_ctx->block; |
| 641 | current_block != NULL; |
| 642 | current_block = current_block->GetParent()) |
| 643 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 644 | lldb::VariableListSP var_list = current_block->GetVariableList(false, true); |
| 645 | |
| 646 | if (!var_list) |
| 647 | continue; |
| 648 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 649 | lldb::VariableSP var = var_list->FindVariable(name_cs); |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 650 | |
| 651 | if (!var) |
| 652 | continue; |
| 653 | |
| 654 | // var->GetType()->GetClangAST() is the program's AST context and holds |
| 655 | // var->GetType()->GetOpaqueClangQualType(). |
| 656 | |
| 657 | // type is m_type for one of the struct members, which was added by |
| 658 | // AddValueToStruct. That type was extracted from the AST context of |
| 659 | // the compiler in IRForTarget. The original for the type was copied |
| 660 | // out of the program's AST context by AddOneVariable. |
| 661 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 662 | // So that we can compare these two without having to copy back |
| 663 | // something we already had in the original AST context, we maintain |
| 664 | // m_orig_type and m_ast_context (which are passed into |
| 665 | // MaterializeOneVariable by Materialize) for each variable. |
| 666 | |
| 667 | if (!type) |
| 668 | return var.get(); |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 669 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 670 | if (type->GetASTContext() == var->GetType()->GetClangAST()) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 671 | { |
Sean Callanan | f5857a0 | 2010-07-31 01:32:05 +0000 | [diff] [blame] | 672 | if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType())) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 673 | continue; |
| 674 | } |
| 675 | else |
| 676 | { |
| 677 | if (log) |
| 678 | log->PutCString("Skipping a candidate variable because of different AST contexts"); |
| 679 | continue; |
| 680 | } |
| 681 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 682 | return var.get(); |
| 683 | } |
| 684 | |
| 685 | { |
| 686 | CompileUnit *compile_unit = m_sym_ctx->comp_unit; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 687 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 688 | if (!compile_unit) |
| 689 | { |
| 690 | if (log) |
| 691 | log->Printf("compile_unit = %p", compile_unit); |
| 692 | return NULL; |
| 693 | } |
| 694 | |
| 695 | lldb::VariableListSP var_list = compile_unit->GetVariableList(true); |
| 696 | |
| 697 | if (!var_list) |
| 698 | return NULL; |
| 699 | |
| 700 | lldb::VariableSP var = var_list->FindVariable(name_cs); |
| 701 | |
| 702 | if (!var) |
| 703 | return NULL; |
| 704 | |
| 705 | if (!type) |
| 706 | return var.get(); |
| 707 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 708 | if (type->GetASTContext() == var->GetType()->GetClangAST()) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 709 | { |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 710 | if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType())) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 711 | return NULL; |
| 712 | } |
| 713 | else |
| 714 | { |
| 715 | if (log) |
| 716 | log->PutCString("Skipping a candidate variable because of different AST contexts"); |
| 717 | return NULL; |
| 718 | } |
| 719 | |
| 720 | return var.get(); |
| 721 | } |
| 722 | |
| 723 | return NULL; |
| 724 | } |
| 725 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 726 | // Interface for ClangASTSource |
| 727 | void |
| 728 | ClangExpressionDeclMap::GetDecls(NameSearchContext &context, |
| 729 | const char *name) |
| 730 | { |
Sean Callanan | 6184dfe | 2010-06-23 00:47:48 +0000 | [diff] [blame] | 731 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 732 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 733 | if (log) |
| 734 | log->Printf("Hunting for a definition for %s", name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 735 | |
| 736 | // Back out in all cases where we're not fully initialized |
| 737 | if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx) |
| 738 | return; |
| 739 | |
| 740 | Function *function = m_sym_ctx->function; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 741 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 742 | if (!function) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 743 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 744 | if (log) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 745 | log->Printf("Can't evaluate an expression when not in a function"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | ConstString name_cs(name); |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 750 | SymbolContextList sym_ctxs; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 751 | |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 752 | m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 753 | |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 754 | for (uint32_t index = 0, num_indices = sym_ctxs.GetSize(); |
| 755 | index < num_indices; |
| 756 | ++index) |
| 757 | { |
| 758 | SymbolContext sym_ctx; |
| 759 | sym_ctxs.GetContextAtIndex(index, sym_ctx); |
| 760 | |
| 761 | if (sym_ctx.function) |
| 762 | AddOneFunction(context, sym_ctx.function, NULL); |
| 763 | else if(sym_ctx.symbol) |
| 764 | AddOneFunction(context, NULL, sym_ctx.symbol); |
| 765 | } |
| 766 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 767 | Variable *var = FindVariableInScope(*m_sym_ctx, name); |
| 768 | |
| 769 | if (var) |
| 770 | AddOneVariable(context, var); |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 771 | |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 772 | ClangExpressionVariable *pvar(m_persistent_vars->GetVariable(name)); |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 773 | |
| 774 | if (pvar) |
| 775 | AddOneVariable(context, pvar); |
| 776 | |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 777 | /* Commented out pending resolution of a loop when the TagType is imported |
| 778 | lldb::TypeSP type = m_sym_ctx->FindTypeByName(name_cs); |
| 779 | |
| 780 | if (type.get()) |
| 781 | AddOneType(context, type.get()); |
| 782 | */ |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | Value * |
| 786 | ClangExpressionDeclMap::GetVariableValue(ExecutionContext &exe_ctx, |
| 787 | Variable *var, |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 788 | clang::ASTContext *parser_ast_context, |
| 789 | TypeFromUser *user_type, |
| 790 | TypeFromParser *parser_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 791 | { |
Sean Callanan | 6184dfe | 2010-06-23 00:47:48 +0000 | [diff] [blame] | 792 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 793 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 794 | Type *var_type = var->GetType(); |
| 795 | |
| 796 | if (!var_type) |
| 797 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 798 | if (log) |
| 799 | log->PutCString("Skipped a definition because it has no type"); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 800 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | void *var_opaque_type = var_type->GetOpaqueClangQualType(); |
| 804 | |
| 805 | if (!var_opaque_type) |
| 806 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 807 | if (log) |
| 808 | log->PutCString("Skipped a definition because it has no Clang type"); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 809 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 812 | TypeList *type_list = var_type->GetTypeList(); |
| 813 | |
| 814 | if (!type_list) |
| 815 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 816 | if (log) |
| 817 | log->PutCString("Skipped a definition because the type has no associated type list"); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 818 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext(); |
| 822 | |
| 823 | if (!exe_ast_ctx) |
| 824 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 825 | if (log) |
| 826 | log->PutCString("There is no AST context for the current execution context"); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 827 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 830 | DWARFExpression &var_location_expr = var->LocationExpression(); |
| 831 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 832 | std::auto_ptr<Value> var_location(new Value); |
| 833 | |
| 834 | Error err; |
| 835 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 836 | if (!var_location_expr.Evaluate(&exe_ctx, exe_ast_ctx, NULL, *var_location.get(), &err)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 837 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 838 | if (log) |
| 839 | log->Printf("Error evaluating location: %s", err.AsCString()); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 840 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 843 | clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext(); |
| 844 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 845 | void *type_to_use; |
| 846 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 847 | if (parser_ast_context) |
| 848 | { |
| 849 | type_to_use = ClangASTContext::CopyType(parser_ast_context, var_ast_context, var_opaque_type); |
| 850 | |
| 851 | if (parser_type) |
| 852 | *parser_type = TypeFromParser(type_to_use, parser_ast_context); |
| 853 | } |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 854 | else |
| 855 | type_to_use = var_opaque_type; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 856 | |
| 857 | if (var_location.get()->GetContextType() == Value::eContextTypeInvalid) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 858 | var_location.get()->SetContext(Value::eContextTypeOpaqueClangQualType, type_to_use); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 859 | |
| 860 | if (var_location.get()->GetValueType() == Value::eValueTypeFileAddress) |
| 861 | { |
| 862 | SymbolContext var_sc; |
| 863 | var->CalculateSymbolContext(&var_sc); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 865 | if (!var_sc.module_sp) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 866 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 867 | |
| 868 | ObjectFile *object_file = var_sc.module_sp->GetObjectFile(); |
| 869 | |
| 870 | if (!object_file) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 871 | return NULL; |
| 872 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 873 | Address so_addr(var_location->GetScalar().ULongLong(), object_file->GetSectionList()); |
| 874 | |
| 875 | lldb::addr_t load_addr = so_addr.GetLoadAddress(m_exe_ctx->process); |
| 876 | |
| 877 | var_location->GetScalar() = load_addr; |
| 878 | var_location->SetValueType(Value::eValueTypeLoadAddress); |
| 879 | } |
| 880 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 881 | if (user_type) |
| 882 | *user_type = TypeFromUser(var_opaque_type, var_ast_context); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 883 | |
| 884 | return var_location.release(); |
| 885 | } |
| 886 | |
| 887 | void |
| 888 | ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, |
| 889 | Variable* var) |
| 890 | { |
| 891 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 892 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 893 | TypeFromUser ut; |
| 894 | TypeFromParser pt; |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 895 | |
| 896 | Value *var_location = GetVariableValue(*m_exe_ctx, |
| 897 | var, |
| 898 | context.GetASTContext(), |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 899 | &ut, |
| 900 | &pt); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 901 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 902 | NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 903 | |
| 904 | Tuple tuple; |
| 905 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 906 | tuple.m_decl = var_decl; |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 907 | tuple.m_value = var_location; |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 908 | tuple.m_user_type = ut; |
| 909 | tuple.m_parser_type = pt; |
Sean Callanan | 02fbafa | 2010-07-27 21:39:39 +0000 | [diff] [blame] | 910 | tuple.m_llvm_value = NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 911 | |
| 912 | m_tuples.push_back(tuple); |
| 913 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 914 | if (log) |
Sean Callanan | f5857a0 | 2010-07-31 01:32:05 +0000 | [diff] [blame] | 915 | log->Printf("Found variable %s, returned (NamedDecl)%p", context.Name.getAsString().c_str(), var_decl); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | void |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 919 | ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 920 | ClangExpressionVariable *pvar) |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 921 | { |
Sean Callanan | a622343 | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 922 | TypeFromUser user_type = pvar->m_user_type; |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 923 | |
| 924 | TypeFromParser parser_type(ClangASTContext::CopyType(context.GetASTContext(), |
| 925 | user_type.GetASTContext(), |
| 926 | user_type.GetOpaqueQualType()), |
| 927 | context.GetASTContext()); |
| 928 | |
| 929 | (void)context.AddVarDecl(parser_type.GetOpaqueQualType()); |
| 930 | } |
| 931 | |
| 932 | void |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 933 | ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context, |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 934 | Function* fun, |
| 935 | Symbol* symbol) |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 936 | { |
Sean Callanan | 6184dfe | 2010-06-23 00:47:48 +0000 | [diff] [blame] | 937 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 938 | |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 939 | NamedDecl *fun_decl; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 940 | std::auto_ptr<Value> fun_location(new Value); |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 941 | const Address *fun_address; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 942 | |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 943 | // only valid for Functions, not for Symbols |
| 944 | void *fun_opaque_type = NULL; |
| 945 | clang::ASTContext *fun_ast_context = NULL; |
| 946 | |
| 947 | if (fun) |
| 948 | { |
| 949 | Type *fun_type = fun->GetType(); |
| 950 | |
| 951 | if (!fun_type) |
| 952 | { |
| 953 | if (log) |
| 954 | log->PutCString("Skipped a function because it has no type"); |
| 955 | return; |
| 956 | } |
| 957 | |
| 958 | fun_opaque_type = fun_type->GetOpaqueClangQualType(); |
| 959 | |
| 960 | if (!fun_opaque_type) |
| 961 | { |
| 962 | if (log) |
| 963 | log->PutCString("Skipped a function because it has no Clang type"); |
| 964 | return; |
| 965 | } |
| 966 | |
| 967 | fun_address = &fun->GetAddressRange().GetBaseAddress(); |
| 968 | |
| 969 | TypeList *type_list = fun_type->GetTypeList(); |
| 970 | fun_ast_context = type_list->GetClangASTContext().getASTContext(); |
| 971 | void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), fun_ast_context, fun_opaque_type); |
| 972 | |
| 973 | fun_decl = context.AddFunDecl(copied_type); |
| 974 | } |
| 975 | else if (symbol) |
| 976 | { |
| 977 | fun_address = &symbol->GetAddressRangeRef().GetBaseAddress(); |
| 978 | |
| 979 | fun_decl = context.AddGenericFunDecl(); |
| 980 | } |
| 981 | else |
| 982 | { |
| 983 | if (log) |
| 984 | log->PutCString("AddOneFunction called with no function and no symbol"); |
| 985 | return; |
| 986 | } |
| 987 | |
| 988 | lldb::addr_t load_addr = fun_address->GetLoadAddress(m_exe_ctx->process); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 989 | fun_location->SetValueType(Value::eValueTypeLoadAddress); |
| 990 | fun_location->GetScalar() = load_addr; |
| 991 | |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 992 | Tuple tuple; |
| 993 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 994 | tuple.m_decl = fun_decl; |
| 995 | tuple.m_value = fun_location.release(); |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 996 | tuple.m_user_type = TypeFromUser(fun_opaque_type, fun_ast_context); |
Sean Callanan | 02fbafa | 2010-07-27 21:39:39 +0000 | [diff] [blame] | 997 | tuple.m_llvm_value = NULL; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 998 | |
| 999 | m_tuples.push_back(tuple); |
| 1000 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 1001 | if (log) |
Sean Callanan | f5857a0 | 2010-07-31 01:32:05 +0000 | [diff] [blame] | 1002 | log->Printf("Found function %s, returned (NamedDecl)%p", context.Name.getAsString().c_str(), fun_decl); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1003 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1004 | |
| 1005 | void |
| 1006 | ClangExpressionDeclMap::AddOneType(NameSearchContext &context, |
| 1007 | Type *type) |
| 1008 | { |
| 1009 | TypeFromUser ut(type->GetOpaqueClangQualType(), |
| 1010 | type->GetClangAST()); |
| 1011 | |
| 1012 | void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), ut.GetASTContext(), ut.GetOpaqueQualType()); |
| 1013 | |
| 1014 | context.AddTypeDecl(copied_type); |
| 1015 | } |