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