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 | |
| 218 | bool |
| 219 | ClangExpressionDeclMap::DoMaterialize (bool dematerialize, |
| 220 | ExecutionContext *exe_ctx, |
| 221 | lldb_private::Value *result_value, |
| 222 | Error &err) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 223 | { |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 224 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 225 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 226 | if (!m_struct_laid_out) |
| 227 | { |
| 228 | err.SetErrorString("Structure hasn't been laid out yet"); |
| 229 | return LLDB_INVALID_ADDRESS; |
| 230 | } |
| 231 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 232 | if (!exe_ctx) |
| 233 | { |
| 234 | err.SetErrorString("Received null execution context"); |
| 235 | return LLDB_INVALID_ADDRESS; |
| 236 | } |
| 237 | |
| 238 | const SymbolContext &sym_ctx(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything)); |
| 239 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 240 | if (!dematerialize) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 241 | { |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 242 | if (m_materialized_location) |
| 243 | { |
| 244 | exe_ctx->process->DeallocateMemory(m_materialized_location); |
| 245 | m_materialized_location = 0; |
| 246 | } |
| 247 | |
| 248 | lldb::addr_t mem = exe_ctx->process->AllocateMemory(m_struct_alignment + m_struct_size, |
| 249 | lldb::ePermissionsReadable | lldb::ePermissionsWritable, |
| 250 | err); |
| 251 | |
| 252 | if (mem == LLDB_INVALID_ADDRESS) |
| 253 | return false; |
| 254 | |
| 255 | m_allocated_area = mem; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 258 | m_materialized_location = m_allocated_area; |
| 259 | |
| 260 | if (m_materialized_location % m_struct_alignment) |
| 261 | { |
| 262 | m_materialized_location += (m_struct_alignment - (m_materialized_location % m_struct_alignment)); |
| 263 | } |
| 264 | |
| 265 | StructMemberIterator iter; |
| 266 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 267 | for (iter = m_members.begin(); |
| 268 | iter != m_members.end(); |
| 269 | ++iter) |
| 270 | { |
| 271 | uint32_t tuple_index; |
| 272 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 273 | if (!GetIndexForDecl(tuple_index, iter->m_decl)) |
| 274 | { |
| 275 | if (iter->m_name.find("___clang_expr_result") == std::string::npos) |
| 276 | { |
| 277 | err.SetErrorStringWithFormat("Unexpected variable %s", iter->m_name.c_str()); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | if (log) |
| 282 | log->Printf("Found special result variable %s", iter->m_name.c_str()); |
| 283 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 284 | if (dematerialize) |
| 285 | { |
| 286 | clang::ASTContext *context(exe_ctx->target->GetScratchClangASTContext()->getASTContext()); |
| 287 | |
| 288 | if (!context) |
| 289 | { |
| 290 | err.SetErrorString("Couldn't find a scratch AST context to put the result type into"); |
| 291 | } |
| 292 | |
| 293 | TypeFromUser copied_type(ClangASTContext::CopyType(context, |
| 294 | iter->m_parser_type.GetASTContext(), |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 295 | iter->m_parser_type.GetOpaqueQualType()), |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 296 | context); |
| 297 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame^] | 298 | result_value->SetContext(Value::eContextTypeOpaqueClangQualType, copied_type.GetOpaqueQualType()); |
| 299 | |
| 300 | result_value->SetValueType(Value::eValueTypeLoadAddress); |
| 301 | result_value->GetScalar() = (uintptr_t)m_materialized_location + iter->m_offset; |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 304 | continue; |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 305 | } |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 306 | |
| 307 | Tuple &tuple(m_tuples[tuple_index]); |
| 308 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 309 | 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] | 310 | return false; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 313 | return true; |
| 314 | } |
| 315 | |
| 316 | bool |
| 317 | ClangExpressionDeclMap::DoMaterializeOneVariable(bool dematerialize, |
| 318 | ExecutionContext &exe_ctx, |
| 319 | const SymbolContext &sym_ctx, |
| 320 | const char *name, |
| 321 | TypeFromUser type, |
| 322 | lldb::addr_t addr, |
| 323 | Error &err) |
| 324 | { |
| 325 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 326 | |
| 327 | Variable *var = FindVariableInScope(sym_ctx, name, &type); |
| 328 | |
| 329 | if (!var) |
| 330 | { |
| 331 | err.SetErrorStringWithFormat("Couldn't find %s with appropriate type", name); |
| 332 | return false; |
| 333 | } |
| 334 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame^] | 335 | if (log) |
| 336 | 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] | 337 | |
| 338 | std::auto_ptr<lldb_private::Value> location_value(GetVariableValue(exe_ctx, |
| 339 | var, |
| 340 | type.GetASTContext())); |
| 341 | |
| 342 | if (!location_value.get()) |
| 343 | { |
| 344 | err.SetErrorStringWithFormat("Couldn't get value for %s", name); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | if (location_value->GetValueType() == Value::eValueTypeLoadAddress) |
| 349 | { |
| 350 | lldb::addr_t value_addr = location_value->GetScalar().ULongLong(); |
| 351 | |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame^] | 352 | size_t bit_size = ClangASTContext::GetTypeBitSize(type.GetASTContext(), type.GetOpaqueQualType()); |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 353 | size_t byte_size = bit_size % 8 ? ((bit_size + 8) / 8) : (bit_size / 8); |
| 354 | |
| 355 | DataBufferHeap data; |
| 356 | data.SetByteSize(byte_size); |
| 357 | |
| 358 | lldb::addr_t src_addr; |
| 359 | lldb::addr_t dest_addr; |
| 360 | |
| 361 | if (dematerialize) |
| 362 | { |
| 363 | src_addr = addr; |
| 364 | dest_addr = value_addr; |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | src_addr = value_addr; |
| 369 | dest_addr = addr; |
| 370 | } |
| 371 | |
| 372 | Error error; |
| 373 | if (exe_ctx.process->ReadMemory (src_addr, data.GetBytes(), byte_size, error) != byte_size) |
| 374 | { |
| 375 | err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString()); |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | if (exe_ctx.process->WriteMemory (dest_addr, data.GetBytes(), byte_size, error) != byte_size) |
| 380 | { |
| 381 | err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString()); |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | if (log) |
| 386 | log->Printf("Copied from 0x%llx to 0x%llx", (uint64_t)src_addr, (uint64_t)addr); |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | StreamString ss; |
| 391 | |
| 392 | location_value->Dump(&ss); |
| 393 | |
| 394 | err.SetErrorStringWithFormat("%s has a value of unhandled type: %s", name, ss.GetString().c_str()); |
| 395 | } |
| 396 | |
| 397 | return true; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 400 | Variable* |
| 401 | ClangExpressionDeclMap::FindVariableInScope(const SymbolContext &sym_ctx, |
| 402 | const char *name, |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 403 | TypeFromUser *type) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 404 | { |
| 405 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 406 | |
| 407 | Function *function(m_sym_ctx->function); |
| 408 | Block *block(m_sym_ctx->block); |
| 409 | |
| 410 | if (!function || !block) |
| 411 | { |
| 412 | if (log) |
| 413 | log->Printf("function = %p, block = %p", function, block); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 414 | return NULL; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | BlockList& blocks(function->GetBlocks(true)); |
| 418 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 419 | ConstString name_cs(name); |
| 420 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 421 | lldb::user_id_t current_block_id; |
| 422 | |
| 423 | for (current_block_id = block->GetID(); |
| 424 | current_block_id != Block::InvalidID; |
| 425 | current_block_id = blocks.GetParent(current_block_id)) |
| 426 | { |
| 427 | Block *current_block(blocks.GetBlockByID(current_block_id)); |
| 428 | |
| 429 | lldb::VariableListSP var_list = current_block->GetVariableList(false, true); |
| 430 | |
| 431 | if (!var_list) |
| 432 | continue; |
| 433 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 434 | lldb::VariableSP var = var_list->FindVariable(name_cs); |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 435 | |
| 436 | if (!var) |
| 437 | continue; |
| 438 | |
| 439 | // var->GetType()->GetClangAST() is the program's AST context and holds |
| 440 | // var->GetType()->GetOpaqueClangQualType(). |
| 441 | |
| 442 | // type is m_type for one of the struct members, which was added by |
| 443 | // AddValueToStruct. That type was extracted from the AST context of |
| 444 | // the compiler in IRForTarget. The original for the type was copied |
| 445 | // out of the program's AST context by AddOneVariable. |
| 446 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 447 | // So that we can compare these two without having to copy back |
| 448 | // something we already had in the original AST context, we maintain |
| 449 | // m_orig_type and m_ast_context (which are passed into |
| 450 | // MaterializeOneVariable by Materialize) for each variable. |
| 451 | |
| 452 | if (!type) |
| 453 | return var.get(); |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 454 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 455 | if (type->GetASTContext() == var->GetType()->GetClangAST()) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 456 | { |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 457 | if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type, var->GetType()->GetOpaqueClangQualType())) |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 458 | continue; |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | if (log) |
| 463 | log->PutCString("Skipping a candidate variable because of different AST contexts"); |
| 464 | continue; |
| 465 | } |
| 466 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 467 | return var.get(); |
| 468 | } |
| 469 | |
| 470 | { |
| 471 | CompileUnit *compile_unit = m_sym_ctx->comp_unit; |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 472 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 473 | if (!compile_unit) |
| 474 | { |
| 475 | if (log) |
| 476 | log->Printf("compile_unit = %p", compile_unit); |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | lldb::VariableListSP var_list = compile_unit->GetVariableList(true); |
| 481 | |
| 482 | if (!var_list) |
| 483 | return NULL; |
| 484 | |
| 485 | lldb::VariableSP var = var_list->FindVariable(name_cs); |
| 486 | |
| 487 | if (!var) |
| 488 | return NULL; |
| 489 | |
| 490 | if (!type) |
| 491 | return var.get(); |
| 492 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 493 | if (type->GetASTContext() == var->GetType()->GetClangAST()) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 494 | { |
Sean Callanan | 841026f | 2010-07-23 00:16:21 +0000 | [diff] [blame^] | 495 | if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType())) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 496 | return NULL; |
| 497 | } |
| 498 | else |
| 499 | { |
| 500 | if (log) |
| 501 | log->PutCString("Skipping a candidate variable because of different AST contexts"); |
| 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | return var.get(); |
| 506 | } |
| 507 | |
| 508 | return NULL; |
| 509 | } |
| 510 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 511 | // Interface for ClangASTSource |
| 512 | void |
| 513 | ClangExpressionDeclMap::GetDecls(NameSearchContext &context, |
| 514 | const char *name) |
| 515 | { |
Sean Callanan | 6184dfe | 2010-06-23 00:47:48 +0000 | [diff] [blame] | 516 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 517 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 518 | if (log) |
| 519 | log->Printf("Hunting for a definition for %s", name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 520 | |
| 521 | // Back out in all cases where we're not fully initialized |
| 522 | if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx) |
| 523 | return; |
| 524 | |
| 525 | Function *function = m_sym_ctx->function; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 526 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 527 | if (!function) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 528 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 529 | if (log) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 530 | log->Printf("Can't evaluate an expression when not in a function"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 531 | return; |
| 532 | } |
| 533 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 534 | ConstString name_cs(name); |
| 535 | |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 536 | Function *fn = m_sym_ctx->FindFunctionByName(name_cs.GetCString()); |
| 537 | |
| 538 | if (fn) |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 539 | AddOneFunction(context, fn); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 540 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 541 | Variable *var = FindVariableInScope(*m_sym_ctx, name); |
| 542 | |
| 543 | if (var) |
| 544 | AddOneVariable(context, var); |
| 545 | } |
| 546 | |
| 547 | Value * |
| 548 | ClangExpressionDeclMap::GetVariableValue(ExecutionContext &exe_ctx, |
| 549 | Variable *var, |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 550 | clang::ASTContext *parser_ast_context, |
| 551 | TypeFromUser *user_type, |
| 552 | TypeFromParser *parser_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 553 | { |
Sean Callanan | 6184dfe | 2010-06-23 00:47:48 +0000 | [diff] [blame] | 554 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 555 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 556 | Type *var_type = var->GetType(); |
| 557 | |
| 558 | if (!var_type) |
| 559 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 560 | if (log) |
| 561 | log->PutCString("Skipped a definition because it has no type"); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 562 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void *var_opaque_type = var_type->GetOpaqueClangQualType(); |
| 566 | |
| 567 | if (!var_opaque_type) |
| 568 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 569 | if (log) |
| 570 | log->PutCString("Skipped a definition because it has no Clang type"); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 571 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 574 | TypeList *type_list = var_type->GetTypeList(); |
| 575 | |
| 576 | if (!type_list) |
| 577 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 578 | if (log) |
| 579 | 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] | 580 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext(); |
| 584 | |
| 585 | if (!exe_ast_ctx) |
| 586 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 587 | if (log) |
| 588 | log->PutCString("There is no AST context for the current execution context"); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 589 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 592 | DWARFExpression &var_location_expr = var->LocationExpression(); |
| 593 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 594 | std::auto_ptr<Value> var_location(new Value); |
| 595 | |
| 596 | Error err; |
| 597 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 598 | 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] | 599 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 600 | if (log) |
| 601 | log->Printf("Error evaluating location: %s", err.AsCString()); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 602 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 605 | clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext(); |
| 606 | |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 607 | void *type_to_use; |
| 608 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 609 | if (parser_ast_context) |
| 610 | { |
| 611 | type_to_use = ClangASTContext::CopyType(parser_ast_context, var_ast_context, var_opaque_type); |
| 612 | |
| 613 | if (parser_type) |
| 614 | *parser_type = TypeFromParser(type_to_use, parser_ast_context); |
| 615 | } |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 616 | else |
| 617 | type_to_use = var_opaque_type; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 618 | |
| 619 | if (var_location.get()->GetContextType() == Value::eContextTypeInvalid) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 620 | var_location.get()->SetContext(Value::eContextTypeOpaqueClangQualType, type_to_use); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 621 | |
| 622 | if (var_location.get()->GetValueType() == Value::eValueTypeFileAddress) |
| 623 | { |
| 624 | SymbolContext var_sc; |
| 625 | var->CalculateSymbolContext(&var_sc); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 627 | if (!var_sc.module_sp) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 628 | return NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 629 | |
| 630 | ObjectFile *object_file = var_sc.module_sp->GetObjectFile(); |
| 631 | |
| 632 | if (!object_file) |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 633 | return NULL; |
| 634 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 635 | Address so_addr(var_location->GetScalar().ULongLong(), object_file->GetSectionList()); |
| 636 | |
| 637 | lldb::addr_t load_addr = so_addr.GetLoadAddress(m_exe_ctx->process); |
| 638 | |
| 639 | var_location->GetScalar() = load_addr; |
| 640 | var_location->SetValueType(Value::eValueTypeLoadAddress); |
| 641 | } |
| 642 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 643 | if (user_type) |
| 644 | *user_type = TypeFromUser(var_opaque_type, var_ast_context); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 645 | |
| 646 | return var_location.release(); |
| 647 | } |
| 648 | |
| 649 | void |
| 650 | ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, |
| 651 | Variable* var) |
| 652 | { |
| 653 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 654 | |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 655 | TypeFromUser ut; |
| 656 | TypeFromParser pt; |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 657 | |
| 658 | Value *var_location = GetVariableValue(*m_exe_ctx, |
| 659 | var, |
| 660 | context.GetASTContext(), |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 661 | &ut, |
| 662 | &pt); |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 663 | |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 664 | NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 665 | |
| 666 | Tuple tuple; |
| 667 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 668 | tuple.m_decl = var_decl; |
Sean Callanan | 336a000 | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 669 | tuple.m_value = var_location; |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 670 | tuple.m_user_type = ut; |
| 671 | tuple.m_parser_type = pt; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 672 | |
| 673 | m_tuples.push_back(tuple); |
| 674 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 675 | if (log) |
| 676 | log->PutCString("Found variable"); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void |
| 680 | ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context, |
| 681 | Function* fun) |
| 682 | { |
Sean Callanan | 6184dfe | 2010-06-23 00:47:48 +0000 | [diff] [blame] | 683 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 684 | |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 685 | Type *fun_type = fun->GetType(); |
| 686 | |
| 687 | if (!fun_type) |
| 688 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 689 | if (log) |
| 690 | log->PutCString("Skipped a function because it has no type"); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 691 | return; |
| 692 | } |
| 693 | |
| 694 | void *fun_opaque_type = fun_type->GetOpaqueClangQualType(); |
| 695 | |
| 696 | if (!fun_opaque_type) |
| 697 | { |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 698 | if (log) |
| 699 | log->PutCString("Skipped a function because it has no Clang type"); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 700 | return; |
| 701 | } |
| 702 | |
| 703 | std::auto_ptr<Value> fun_location(new Value); |
| 704 | |
| 705 | const Address &fun_address = fun->GetAddressRange().GetBaseAddress(); |
| 706 | lldb::addr_t load_addr = fun_address.GetLoadAddress(m_exe_ctx->process); |
| 707 | fun_location->SetValueType(Value::eValueTypeLoadAddress); |
| 708 | fun_location->GetScalar() = load_addr; |
| 709 | |
| 710 | TypeList *type_list = fun_type->GetTypeList(); |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 711 | clang::ASTContext *fun_ast_context = type_list->GetClangASTContext().getASTContext(); |
| 712 | 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] | 713 | |
| 714 | NamedDecl *fun_decl = context.AddFunDecl(copied_type); |
| 715 | |
| 716 | Tuple tuple; |
| 717 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 718 | tuple.m_decl = fun_decl; |
| 719 | tuple.m_value = fun_location.release(); |
Sean Callanan | f328c9f | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 720 | tuple.m_user_type = TypeFromUser(fun_opaque_type, fun_ast_context); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 721 | |
| 722 | m_tuples.push_back(tuple); |
| 723 | |
Sean Callanan | 810f22d | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 724 | if (log) |
| 725 | log->PutCString("Found function"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 726 | } |