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" |
| 22 | #include "lldb/Symbol/ClangASTContext.h" |
| 23 | #include "lldb/Symbol/CompileUnit.h" |
| 24 | #include "lldb/Symbol/Function.h" |
| 25 | #include "lldb/Symbol/ObjectFile.h" |
| 26 | #include "lldb/Symbol/SymbolContext.h" |
| 27 | #include "lldb/Symbol/Type.h" |
| 28 | #include "lldb/Symbol/TypeList.h" |
| 29 | #include "lldb/Symbol/Variable.h" |
| 30 | #include "lldb/Symbol/VariableList.h" |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 31 | #include "lldb/Target/ExecutionContext.h" |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 32 | #include "lldb/Target/Process.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | #include "lldb/Target/StackFrame.h" |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 34 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | using namespace lldb_private; |
| 37 | using namespace clang; |
| 38 | |
| 39 | ClangExpressionDeclMap::ClangExpressionDeclMap(ExecutionContext *exe_ctx) : |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 40 | m_exe_ctx(exe_ctx), |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 41 | m_struct_laid_out(false), |
| 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; |
| 48 | } |
| 49 | |
| 50 | ClangExpressionDeclMap::~ClangExpressionDeclMap() |
| 51 | { |
| 52 | uint32_t num_tuples = m_tuples.size (); |
| 53 | uint32_t tuple_index; |
| 54 | |
| 55 | for (tuple_index = 0; tuple_index < num_tuples; ++tuple_index) |
| 56 | delete m_tuples[tuple_index].m_value; |
| 57 | |
| 58 | if (m_sym_ctx) |
| 59 | delete m_sym_ctx; |
| 60 | } |
| 61 | |
| 62 | bool |
| 63 | ClangExpressionDeclMap::GetIndexForDecl (uint32_t &index, |
| 64 | const clang::Decl *decl) |
| 65 | { |
| 66 | uint32_t num_tuples = m_tuples.size (); |
| 67 | uint32_t tuple_index; |
| 68 | |
| 69 | for (tuple_index = 0; tuple_index < num_tuples; ++tuple_index) |
| 70 | { |
| 71 | if (m_tuples[tuple_index].m_decl == decl) |
| 72 | { |
| 73 | index = tuple_index; |
| 74 | return true; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 81 | // Interface for IRForTarget |
| 82 | |
| 83 | bool |
| 84 | ClangExpressionDeclMap::AddValueToStruct (llvm::Value *value, |
| 85 | const clang::NamedDecl *decl, |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 86 | std::string &name, |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 87 | void *parser_type, |
| 88 | clang::ASTContext *parser_ast_context, |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 89 | size_t size, |
| 90 | off_t alignment) |
| 91 | { |
| 92 | m_struct_laid_out = false; |
| 93 | |
| 94 | StructMemberIterator iter; |
| 95 | |
| 96 | for (iter = m_members.begin(); |
| 97 | iter != m_members.end(); |
| 98 | ++iter) |
| 99 | { |
| 100 | if (iter->m_decl == decl) |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | StructMember member; |
| 105 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 106 | member.m_value = value; |
| 107 | member.m_decl = decl; |
| 108 | member.m_name = name; |
| 109 | member.m_parser_type = TypeFromParser(parser_type, parser_ast_context); |
| 110 | member.m_offset = 0; |
| 111 | member.m_size = size; |
| 112 | member.m_alignment = alignment; |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 113 | |
| 114 | m_members.push_back(member); |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | bool |
| 120 | ClangExpressionDeclMap::DoStructLayout () |
| 121 | { |
| 122 | if (m_struct_laid_out) |
| 123 | return true; |
| 124 | |
| 125 | StructMemberIterator iter; |
| 126 | |
| 127 | off_t cursor = 0; |
| 128 | |
| 129 | m_struct_alignment = 0; |
| 130 | m_struct_size = 0; |
| 131 | |
| 132 | for (iter = m_members.begin(); |
| 133 | iter != m_members.end(); |
| 134 | ++iter) |
| 135 | { |
| 136 | if (iter == m_members.begin()) |
| 137 | m_struct_alignment = iter->m_alignment; |
| 138 | |
| 139 | if (cursor % iter->m_alignment) |
| 140 | cursor += (iter->m_alignment - (cursor % iter->m_alignment)); |
| 141 | |
| 142 | iter->m_offset = cursor; |
| 143 | cursor += iter->m_size; |
| 144 | } |
| 145 | |
| 146 | m_struct_size = cursor; |
| 147 | |
| 148 | m_struct_laid_out = true; |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | bool ClangExpressionDeclMap::GetStructInfo (uint32_t &num_elements, |
| 153 | size_t &size, |
| 154 | off_t &alignment) |
| 155 | { |
| 156 | if (!m_struct_laid_out) |
| 157 | return false; |
| 158 | |
| 159 | num_elements = m_members.size(); |
| 160 | size = m_struct_size; |
| 161 | alignment = m_struct_alignment; |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | bool |
| 167 | ClangExpressionDeclMap::GetStructElement (const clang::NamedDecl *&decl, |
| 168 | llvm::Value *&value, |
| 169 | off_t &offset, |
| 170 | uint32_t index) |
| 171 | { |
| 172 | if (!m_struct_laid_out) |
| 173 | return false; |
| 174 | |
| 175 | if (index >= m_members.size()) |
| 176 | return false; |
| 177 | |
| 178 | decl = m_members[index].m_decl; |
| 179 | value = m_members[index].m_value; |
| 180 | offset = m_members[index].m_offset; |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | // Interface for DwarfExpression |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 186 | lldb_private::Value |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | *ClangExpressionDeclMap::GetValueForIndex (uint32_t index) |
| 188 | { |
| 189 | if (index >= m_tuples.size ()) |
| 190 | return NULL; |
| 191 | |
| 192 | return m_tuples[index].m_value; |
| 193 | } |
| 194 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 195 | // Interface for CommandObjectExpression |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 196 | |
| 197 | bool |
| 198 | ClangExpressionDeclMap::Materialize (ExecutionContext *exe_ctx, |
| 199 | lldb::addr_t &struct_address, |
| 200 | Error &err) |
| 201 | { |
| 202 | bool result = DoMaterialize(false, exe_ctx, NULL, err); |
| 203 | |
| 204 | if (result) |
| 205 | struct_address = m_materialized_location; |
| 206 | |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | bool |
| 211 | ClangExpressionDeclMap::Dematerialize (ExecutionContext *exe_ctx, |
| 212 | lldb_private::Value &result_value, |
| 213 | Error &err) |
| 214 | { |
| 215 | return DoMaterialize(true, exe_ctx, &result_value, err); |
| 216 | } |
| 217 | |
Sean Callanan | 32824aa | 2010-07-23 22:19:18 +0000 | [diff] [blame^] | 218 | bool |
| 219 | ClangExpressionDeclMap::DumpMaterializedStruct(ExecutionContext *exe_ctx, |
| 220 | Stream &s, |
| 221 | Error &err) |
| 222 | { |
| 223 | if (!m_struct_laid_out) |
| 224 | { |
| 225 | err.SetErrorString("Structure hasn't been laid out yet"); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | if (!exe_ctx) |
| 230 | { |
| 231 | err.SetErrorString("Received null execution context"); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | if (!exe_ctx->process) |
| 237 | { |
| 238 | err.SetErrorString("Couldn't find the process"); |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | if (!exe_ctx->target) |
| 243 | { |
| 244 | err.SetErrorString("Couldn't find the target"); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | lldb::DataBufferSP data(new DataBufferHeap(m_struct_size, 0)); |
| 249 | |
| 250 | Error error; |
| 251 | if (exe_ctx->process->ReadMemory (m_materialized_location, data->GetBytes(), data->GetByteSize(), error) != data->GetByteSize()) |
| 252 | { |
| 253 | err.SetErrorStringWithFormat ("Couldn't read struct from the target: %s", error.AsCString()); |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | DataExtractor extractor(data, exe_ctx->process->GetByteOrder(), exe_ctx->target->GetArchitecture().GetAddressByteSize()); |
| 258 | |
| 259 | StructMemberIterator iter; |
| 260 | |
| 261 | for (iter = m_members.begin(); |
| 262 | iter != m_members.end(); |
| 263 | ++iter) |
| 264 | { |
| 265 | s.Printf("[%s]\n", iter->m_name.c_str()); |
| 266 | |
| 267 | extractor.Dump(&s, // stream |
| 268 | iter->m_offset, // offset |
| 269 | lldb::eFormatBytesWithASCII, // format |
| 270 | 1, // byte size of individual entries |
| 271 | iter->m_size, // number of entries |
| 272 | 16, // entries per line |
| 273 | m_materialized_location + iter->m_offset, // address to print |
| 274 | 0, // bit size (bitfields only; 0 means ignore) |
| 275 | 0); // bit alignment (bitfields only; 0 means ignore) |
| 276 | |
| 277 | s.PutChar('\n'); |
| 278 | } |
| 279 | |
| 280 | return true; |
| 281 | } |
| 282 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 283 | bool |
| 284 | ClangExpressionDeclMap::DoMaterialize (bool dematerialize, |
| 285 | ExecutionContext *exe_ctx, |
| 286 | lldb_private::Value *result_value, |
| 287 | Error &err) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 288 | { |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 289 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 290 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 291 | if (!m_struct_laid_out) |
| 292 | { |
| 293 | err.SetErrorString("Structure hasn't been laid out yet"); |
| 294 | return LLDB_INVALID_ADDRESS; |
| 295 | } |
| 296 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 297 | if (!exe_ctx) |
| 298 | { |
| 299 | err.SetErrorString("Received null execution context"); |
| 300 | return LLDB_INVALID_ADDRESS; |
| 301 | } |
| 302 | |
| 303 | const SymbolContext &sym_ctx(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything)); |
| 304 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 305 | if (!dematerialize) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 306 | { |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 307 | if (m_materialized_location) |
| 308 | { |
| 309 | exe_ctx->process->DeallocateMemory(m_materialized_location); |
| 310 | m_materialized_location = 0; |
| 311 | } |
| 312 | |
| 313 | lldb::addr_t mem = exe_ctx->process->AllocateMemory(m_struct_alignment + m_struct_size, |
| 314 | lldb::ePermissionsReadable | lldb::ePermissionsWritable, |
| 315 | err); |
| 316 | |
| 317 | if (mem == LLDB_INVALID_ADDRESS) |
| 318 | return false; |
| 319 | |
| 320 | m_allocated_area = mem; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 323 | m_materialized_location = m_allocated_area; |
| 324 | |
| 325 | if (m_materialized_location % m_struct_alignment) |
| 326 | { |
| 327 | m_materialized_location += (m_struct_alignment - (m_materialized_location % m_struct_alignment)); |
| 328 | } |
| 329 | |
| 330 | StructMemberIterator iter; |
| 331 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 332 | for (iter = m_members.begin(); |
| 333 | iter != m_members.end(); |
| 334 | ++iter) |
| 335 | { |
| 336 | uint32_t tuple_index; |
| 337 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 338 | if (!GetIndexForDecl(tuple_index, iter->m_decl)) |
| 339 | { |
| 340 | if (iter->m_name.find("___clang_expr_result") == std::string::npos) |
| 341 | { |
| 342 | err.SetErrorStringWithFormat("Unexpected variable %s", iter->m_name.c_str()); |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | if (log) |
| 347 | log->Printf("Found special result variable %s", iter->m_name.c_str()); |
| 348 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 349 | if (dematerialize) |
| 350 | { |
| 351 | clang::ASTContext *context(exe_ctx->target->GetScratchClangASTContext()->getASTContext()); |
| 352 | |
| 353 | if (!context) |
| 354 | { |
| 355 | err.SetErrorString("Couldn't find a scratch AST context to put the result type into"); |
| 356 | } |
| 357 | |
| 358 | TypeFromUser copied_type(ClangASTContext::CopyType(context, |
| 359 | iter->m_parser_type.GetASTContext(), |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 360 | iter->m_parser_type.GetOpaqueQualType()), |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 361 | context); |
| 362 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 363 | result_value->SetContext(Value::eContextTypeOpaqueClangQualType, copied_type.GetOpaqueQualType()); |
| 364 | |
| 365 | result_value->SetValueType(Value::eValueTypeLoadAddress); |
| 366 | result_value->GetScalar() = (uintptr_t)m_materialized_location + iter->m_offset; |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 369 | continue; |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 370 | } |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 371 | |
| 372 | Tuple &tuple(m_tuples[tuple_index]); |
| 373 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 374 | 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] | 375 | return false; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 378 | return true; |
| 379 | } |
| 380 | |
| 381 | bool |
| 382 | ClangExpressionDeclMap::DoMaterializeOneVariable(bool dematerialize, |
| 383 | ExecutionContext &exe_ctx, |
| 384 | const SymbolContext &sym_ctx, |
| 385 | const char *name, |
| 386 | TypeFromUser type, |
| 387 | lldb::addr_t addr, |
| 388 | Error &err) |
| 389 | { |
| 390 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 391 | |
| 392 | Variable *var = FindVariableInScope(sym_ctx, name, &type); |
| 393 | |
| 394 | if (!var) |
| 395 | { |
| 396 | err.SetErrorStringWithFormat("Couldn't find %s with appropriate type", name); |
| 397 | return false; |
| 398 | } |
| 399 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 400 | if (log) |
| 401 | 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] | 402 | |
| 403 | std::auto_ptr<lldb_private::Value> location_value(GetVariableValue(exe_ctx, |
| 404 | var, |
| 405 | type.GetASTContext())); |
| 406 | |
| 407 | if (!location_value.get()) |
| 408 | { |
| 409 | err.SetErrorStringWithFormat("Couldn't get value for %s", name); |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | if (location_value->GetValueType() == Value::eValueTypeLoadAddress) |
| 414 | { |
| 415 | lldb::addr_t value_addr = location_value->GetScalar().ULongLong(); |
| 416 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 417 | size_t bit_size = ClangASTContext::GetTypeBitSize(type.GetASTContext(), type.GetOpaqueQualType()); |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 418 | size_t byte_size = bit_size % 8 ? ((bit_size + 8) / 8) : (bit_size / 8); |
| 419 | |
| 420 | DataBufferHeap data; |
| 421 | data.SetByteSize(byte_size); |
| 422 | |
| 423 | lldb::addr_t src_addr; |
| 424 | lldb::addr_t dest_addr; |
| 425 | |
| 426 | if (dematerialize) |
| 427 | { |
| 428 | src_addr = addr; |
| 429 | dest_addr = value_addr; |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | src_addr = value_addr; |
| 434 | dest_addr = addr; |
| 435 | } |
| 436 | |
| 437 | Error error; |
| 438 | if (exe_ctx.process->ReadMemory (src_addr, data.GetBytes(), byte_size, error) != byte_size) |
| 439 | { |
| 440 | err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString()); |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | if (exe_ctx.process->WriteMemory (dest_addr, data.GetBytes(), byte_size, error) != byte_size) |
| 445 | { |
| 446 | err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString()); |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | if (log) |
| 451 | log->Printf("Copied from 0x%llx to 0x%llx", (uint64_t)src_addr, (uint64_t)addr); |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | StreamString ss; |
| 456 | |
| 457 | location_value->Dump(&ss); |
| 458 | |
| 459 | err.SetErrorStringWithFormat("%s has a value of unhandled type: %s", name, ss.GetString().c_str()); |
| 460 | } |
| 461 | |
| 462 | return true; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 465 | Variable* |
| 466 | ClangExpressionDeclMap::FindVariableInScope(const SymbolContext &sym_ctx, |
| 467 | const char *name, |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 468 | TypeFromUser *type) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 469 | { |
| 470 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 471 | |
| 472 | Function *function(m_sym_ctx->function); |
| 473 | Block *block(m_sym_ctx->block); |
| 474 | |
| 475 | if (!function || !block) |
| 476 | { |
| 477 | if (log) |
| 478 | log->Printf("function = %p, block = %p", function, block); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 479 | return NULL; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | BlockList& blocks(function->GetBlocks(true)); |
| 483 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 484 | ConstString name_cs(name); |
| 485 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 486 | lldb::user_id_t current_block_id; |
| 487 | |
| 488 | for (current_block_id = block->GetID(); |
| 489 | current_block_id != Block::InvalidID; |
| 490 | current_block_id = blocks.GetParent(current_block_id)) |
| 491 | { |
| 492 | Block *current_block(blocks.GetBlockByID(current_block_id)); |
| 493 | |
| 494 | lldb::VariableListSP var_list = current_block->GetVariableList(false, true); |
| 495 | |
| 496 | if (!var_list) |
| 497 | continue; |
| 498 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 499 | lldb::VariableSP var = var_list->FindVariable(name_cs); |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 500 | |
| 501 | if (!var) |
| 502 | continue; |
| 503 | |
| 504 | // var->GetType()->GetClangAST() is the program's AST context and holds |
| 505 | // var->GetType()->GetOpaqueClangQualType(). |
| 506 | |
| 507 | // type is m_type for one of the struct members, which was added by |
| 508 | // AddValueToStruct. That type was extracted from the AST context of |
| 509 | // the compiler in IRForTarget. The original for the type was copied |
| 510 | // out of the program's AST context by AddOneVariable. |
| 511 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 512 | // So that we can compare these two without having to copy back |
| 513 | // something we already had in the original AST context, we maintain |
| 514 | // m_orig_type and m_ast_context (which are passed into |
| 515 | // MaterializeOneVariable by Materialize) for each variable. |
| 516 | |
| 517 | if (!type) |
| 518 | return var.get(); |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 519 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 520 | if (type->GetASTContext() == var->GetType()->GetClangAST()) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 521 | { |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 522 | if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type, var->GetType()->GetOpaqueClangQualType())) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 523 | continue; |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | if (log) |
| 528 | log->PutCString("Skipping a candidate variable because of different AST contexts"); |
| 529 | continue; |
| 530 | } |
| 531 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 532 | return var.get(); |
| 533 | } |
| 534 | |
| 535 | { |
| 536 | CompileUnit *compile_unit = m_sym_ctx->comp_unit; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 537 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 538 | if (!compile_unit) |
| 539 | { |
| 540 | if (log) |
| 541 | log->Printf("compile_unit = %p", compile_unit); |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | lldb::VariableListSP var_list = compile_unit->GetVariableList(true); |
| 546 | |
| 547 | if (!var_list) |
| 548 | return NULL; |
| 549 | |
| 550 | lldb::VariableSP var = var_list->FindVariable(name_cs); |
| 551 | |
| 552 | if (!var) |
| 553 | return NULL; |
| 554 | |
| 555 | if (!type) |
| 556 | return var.get(); |
| 557 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 558 | if (type->GetASTContext() == var->GetType()->GetClangAST()) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 559 | { |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 560 | if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType())) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 561 | return NULL; |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | if (log) |
| 566 | log->PutCString("Skipping a candidate variable because of different AST contexts"); |
| 567 | return NULL; |
| 568 | } |
| 569 | |
| 570 | return var.get(); |
| 571 | } |
| 572 | |
| 573 | return NULL; |
| 574 | } |
| 575 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 576 | // Interface for ClangASTSource |
| 577 | void |
| 578 | ClangExpressionDeclMap::GetDecls(NameSearchContext &context, |
| 579 | const char *name) |
| 580 | { |
Sean Callanan | 6184dfe | 2010-06-23 00:47:48 +0000 | [diff] [blame] | 581 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 582 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 583 | if (log) |
| 584 | log->Printf("Hunting for a definition for %s", name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 585 | |
| 586 | // Back out in all cases where we're not fully initialized |
| 587 | if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx) |
| 588 | return; |
| 589 | |
| 590 | Function *function = m_sym_ctx->function; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 591 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 592 | if (!function) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 593 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 594 | if (log) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 595 | log->Printf("Can't evaluate an expression when not in a function"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 599 | ConstString name_cs(name); |
| 600 | |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 601 | Function *fn = m_sym_ctx->FindFunctionByName(name_cs.GetCString()); |
| 602 | |
| 603 | if (fn) |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 604 | AddOneFunction(context, fn); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 605 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 606 | Variable *var = FindVariableInScope(*m_sym_ctx, name); |
| 607 | |
| 608 | if (var) |
| 609 | AddOneVariable(context, var); |
| 610 | } |
| 611 | |
| 612 | Value * |
| 613 | ClangExpressionDeclMap::GetVariableValue(ExecutionContext &exe_ctx, |
| 614 | Variable *var, |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 615 | clang::ASTContext *parser_ast_context, |
| 616 | TypeFromUser *user_type, |
| 617 | TypeFromParser *parser_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 618 | { |
Sean Callanan | 6184dfe | 2010-06-23 00:47:48 +0000 | [diff] [blame] | 619 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 620 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 621 | Type *var_type = var->GetType(); |
| 622 | |
| 623 | if (!var_type) |
| 624 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 625 | if (log) |
| 626 | log->PutCString("Skipped a definition because it has no type"); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 627 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void *var_opaque_type = var_type->GetOpaqueClangQualType(); |
| 631 | |
| 632 | if (!var_opaque_type) |
| 633 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 634 | if (log) |
| 635 | log->PutCString("Skipped a definition because it has no Clang type"); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 636 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 639 | TypeList *type_list = var_type->GetTypeList(); |
| 640 | |
| 641 | if (!type_list) |
| 642 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 643 | if (log) |
| 644 | 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] | 645 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext(); |
| 649 | |
| 650 | if (!exe_ast_ctx) |
| 651 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 652 | if (log) |
| 653 | log->PutCString("There is no AST context for the current execution context"); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 654 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 657 | DWARFExpression &var_location_expr = var->LocationExpression(); |
| 658 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 659 | std::auto_ptr<Value> var_location(new Value); |
| 660 | |
| 661 | Error err; |
| 662 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 663 | 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] | 664 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 665 | if (log) |
| 666 | log->Printf("Error evaluating location: %s", err.AsCString()); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 667 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 670 | clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext(); |
| 671 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 672 | void *type_to_use; |
| 673 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 674 | if (parser_ast_context) |
| 675 | { |
| 676 | type_to_use = ClangASTContext::CopyType(parser_ast_context, var_ast_context, var_opaque_type); |
| 677 | |
| 678 | if (parser_type) |
| 679 | *parser_type = TypeFromParser(type_to_use, parser_ast_context); |
| 680 | } |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 681 | else |
| 682 | type_to_use = var_opaque_type; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 683 | |
| 684 | if (var_location.get()->GetContextType() == Value::eContextTypeInvalid) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 685 | var_location.get()->SetContext(Value::eContextTypeOpaqueClangQualType, type_to_use); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 686 | |
| 687 | if (var_location.get()->GetValueType() == Value::eValueTypeFileAddress) |
| 688 | { |
| 689 | SymbolContext var_sc; |
| 690 | var->CalculateSymbolContext(&var_sc); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 692 | if (!var_sc.module_sp) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 693 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 694 | |
| 695 | ObjectFile *object_file = var_sc.module_sp->GetObjectFile(); |
| 696 | |
| 697 | if (!object_file) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 698 | return NULL; |
| 699 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 700 | Address so_addr(var_location->GetScalar().ULongLong(), object_file->GetSectionList()); |
| 701 | |
| 702 | lldb::addr_t load_addr = so_addr.GetLoadAddress(m_exe_ctx->process); |
| 703 | |
| 704 | var_location->GetScalar() = load_addr; |
| 705 | var_location->SetValueType(Value::eValueTypeLoadAddress); |
| 706 | } |
| 707 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 708 | if (user_type) |
| 709 | *user_type = TypeFromUser(var_opaque_type, var_ast_context); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 710 | |
| 711 | return var_location.release(); |
| 712 | } |
| 713 | |
| 714 | void |
| 715 | ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, |
| 716 | Variable* var) |
| 717 | { |
| 718 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 719 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 720 | TypeFromUser ut; |
| 721 | TypeFromParser pt; |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 722 | |
| 723 | Value *var_location = GetVariableValue(*m_exe_ctx, |
| 724 | var, |
| 725 | context.GetASTContext(), |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 726 | &ut, |
| 727 | &pt); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 728 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 729 | NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 730 | |
| 731 | Tuple tuple; |
| 732 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 733 | tuple.m_decl = var_decl; |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 734 | tuple.m_value = var_location; |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 735 | tuple.m_user_type = ut; |
| 736 | tuple.m_parser_type = pt; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 737 | |
| 738 | m_tuples.push_back(tuple); |
| 739 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 740 | if (log) |
| 741 | log->PutCString("Found variable"); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | void |
| 745 | ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context, |
| 746 | Function* fun) |
| 747 | { |
Sean Callanan | 6184dfe | 2010-06-23 00:47:48 +0000 | [diff] [blame] | 748 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 749 | |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 750 | Type *fun_type = fun->GetType(); |
| 751 | |
| 752 | if (!fun_type) |
| 753 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 754 | if (log) |
| 755 | log->PutCString("Skipped a function because it has no type"); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 756 | return; |
| 757 | } |
| 758 | |
| 759 | void *fun_opaque_type = fun_type->GetOpaqueClangQualType(); |
| 760 | |
| 761 | if (!fun_opaque_type) |
| 762 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 763 | if (log) |
| 764 | log->PutCString("Skipped a function because it has no Clang type"); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 765 | return; |
| 766 | } |
| 767 | |
| 768 | std::auto_ptr<Value> fun_location(new Value); |
| 769 | |
| 770 | const Address &fun_address = fun->GetAddressRange().GetBaseAddress(); |
| 771 | lldb::addr_t load_addr = fun_address.GetLoadAddress(m_exe_ctx->process); |
| 772 | fun_location->SetValueType(Value::eValueTypeLoadAddress); |
| 773 | fun_location->GetScalar() = load_addr; |
| 774 | |
| 775 | TypeList *type_list = fun_type->GetTypeList(); |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 776 | clang::ASTContext *fun_ast_context = type_list->GetClangASTContext().getASTContext(); |
| 777 | void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), fun_ast_context, fun_opaque_type); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 778 | |
| 779 | NamedDecl *fun_decl = context.AddFunDecl(copied_type); |
| 780 | |
| 781 | Tuple tuple; |
| 782 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 783 | tuple.m_decl = fun_decl; |
| 784 | tuple.m_value = fun_location.release(); |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 785 | tuple.m_user_type = TypeFromUser(fun_opaque_type, fun_ast_context); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 786 | |
| 787 | m_tuples.push_back(tuple); |
| 788 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 789 | if (log) |
| 790 | log->PutCString("Found function"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 791 | } |