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