blob: 7e733e4ce0b4b21b6edd6c8000425dd4ffedeba3 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Callanan810f22d2010-07-16 00:09:46 +000018#include "lldb/Core/Error.h"
Sean Callanan6184dfe2010-06-23 00:47:48 +000019#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#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 Callananf328c9f2010-07-20 23:31:16 +000031#include "lldb/Target/ExecutionContext.h"
Sean Callanan810f22d2010-07-16 00:09:46 +000032#include "lldb/Target/Process.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033#include "lldb/Target/StackFrame.h"
Sean Callananf328c9f2010-07-20 23:31:16 +000034#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000035
Chris Lattner24943d22010-06-08 16:52:24 +000036using namespace lldb_private;
37using namespace clang;
38
39ClangExpressionDeclMap::ClangExpressionDeclMap(ExecutionContext *exe_ctx) :
Sean Callanan8bce6652010-07-13 21:41:46 +000040 m_exe_ctx(exe_ctx),
Sean Callanan810f22d2010-07-16 00:09:46 +000041 m_struct_laid_out(false),
42 m_materialized_location(0)
Chris Lattner24943d22010-06-08 16:52:24 +000043{
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
50ClangExpressionDeclMap::~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
62bool
63ClangExpressionDeclMap::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 Callanan8bce6652010-07-13 21:41:46 +000081// Interface for IRForTarget
82
83bool
84ClangExpressionDeclMap::AddValueToStruct (llvm::Value *value,
85 const clang::NamedDecl *decl,
Sean Callanan810f22d2010-07-16 00:09:46 +000086 std::string &name,
Sean Callananf328c9f2010-07-20 23:31:16 +000087 void *parser_type,
88 clang::ASTContext *parser_ast_context,
Sean Callanan8bce6652010-07-13 21:41:46 +000089 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 Callananf328c9f2010-07-20 23:31:16 +0000106 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 Callanan8bce6652010-07-13 21:41:46 +0000113
114 m_members.push_back(member);
115
116 return true;
117}
118
119bool
120ClangExpressionDeclMap::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
152bool 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
166bool
167ClangExpressionDeclMap::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
Sean Callanan02fbafa2010-07-27 21:39:39 +0000185bool
186ClangExpressionDeclMap::GetFunctionInfo (const clang::NamedDecl *decl,
187 llvm::Value**& value,
188 uint64_t &ptr)
Sean Callananba992c52010-07-27 02:07:53 +0000189{
190 TupleIterator iter;
191
192 for (iter = m_tuples.begin();
193 iter != m_tuples.end();
194 ++iter)
195 {
196 if (decl == iter->m_decl)
197 {
Sean Callanan02fbafa2010-07-27 21:39:39 +0000198 value = &iter->m_llvm_value;
199 ptr = iter->m_value->GetScalar().ULongLong();
200 return true;
Sean Callananba992c52010-07-27 02:07:53 +0000201 }
202 }
203
Sean Callanan02fbafa2010-07-27 21:39:39 +0000204 return false;
Sean Callananba992c52010-07-27 02:07:53 +0000205}
206
Sean Callananf5857a02010-07-31 01:32:05 +0000207bool
208ClangExpressionDeclMap::GetFunctionAddress (const char *name,
209 uint64_t &ptr)
210{
211 // Back out in all cases where we're not fully initialized
212 if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
213 return false;
214
215 ConstString name_cs(name);
216 SymbolContextList sym_ctxs;
217
218 m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs);
219
220 if (!sym_ctxs.GetSize())
221 return false;
222
223 SymbolContext sym_ctx;
224 sym_ctxs.GetContextAtIndex(0, sym_ctx);
225
226 const Address *fun_address;
227
228 if (sym_ctx.function)
229 fun_address = &sym_ctx.function->GetAddressRange().GetBaseAddress();
230 else if (sym_ctx.symbol)
231 fun_address = &sym_ctx.symbol->GetAddressRangeRef().GetBaseAddress();
232 else
233 return false;
234
235 ptr = fun_address->GetLoadAddress(m_exe_ctx->process);
236
237 return true;
238}
239
Chris Lattner24943d22010-06-08 16:52:24 +0000240// Interface for DwarfExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000241lldb_private::Value
Chris Lattner24943d22010-06-08 16:52:24 +0000242*ClangExpressionDeclMap::GetValueForIndex (uint32_t index)
243{
244 if (index >= m_tuples.size ())
245 return NULL;
246
247 return m_tuples[index].m_value;
248}
249
Sean Callanan810f22d2010-07-16 00:09:46 +0000250// Interface for CommandObjectExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000251
252bool
253ClangExpressionDeclMap::Materialize (ExecutionContext *exe_ctx,
254 lldb::addr_t &struct_address,
255 Error &err)
256{
257 bool result = DoMaterialize(false, exe_ctx, NULL, err);
258
259 if (result)
260 struct_address = m_materialized_location;
261
262 return result;
263}
264
265bool
266ClangExpressionDeclMap::Dematerialize (ExecutionContext *exe_ctx,
267 lldb_private::Value &result_value,
268 Error &err)
269{
270 return DoMaterialize(true, exe_ctx, &result_value, err);
271}
272
Sean Callanan32824aa2010-07-23 22:19:18 +0000273bool
274ClangExpressionDeclMap::DumpMaterializedStruct(ExecutionContext *exe_ctx,
275 Stream &s,
276 Error &err)
277{
278 if (!m_struct_laid_out)
279 {
280 err.SetErrorString("Structure hasn't been laid out yet");
281 return false;
282 }
283
284 if (!exe_ctx)
285 {
286 err.SetErrorString("Received null execution context");
287 return false;
288 }
289
290
291 if (!exe_ctx->process)
292 {
293 err.SetErrorString("Couldn't find the process");
294 return false;
295 }
296
297 if (!exe_ctx->target)
298 {
299 err.SetErrorString("Couldn't find the target");
300 return false;
301 }
302
303 lldb::DataBufferSP data(new DataBufferHeap(m_struct_size, 0));
304
305 Error error;
306 if (exe_ctx->process->ReadMemory (m_materialized_location, data->GetBytes(), data->GetByteSize(), error) != data->GetByteSize())
307 {
308 err.SetErrorStringWithFormat ("Couldn't read struct from the target: %s", error.AsCString());
309 return false;
310 }
311
312 DataExtractor extractor(data, exe_ctx->process->GetByteOrder(), exe_ctx->target->GetArchitecture().GetAddressByteSize());
313
314 StructMemberIterator iter;
315
316 for (iter = m_members.begin();
317 iter != m_members.end();
318 ++iter)
319 {
320 s.Printf("[%s]\n", iter->m_name.c_str());
321
322 extractor.Dump(&s, // stream
323 iter->m_offset, // offset
324 lldb::eFormatBytesWithASCII, // format
325 1, // byte size of individual entries
326 iter->m_size, // number of entries
327 16, // entries per line
328 m_materialized_location + iter->m_offset, // address to print
329 0, // bit size (bitfields only; 0 means ignore)
330 0); // bit alignment (bitfields only; 0 means ignore)
331
332 s.PutChar('\n');
333 }
334
335 return true;
336}
337
Sean Callananf328c9f2010-07-20 23:31:16 +0000338bool
339ClangExpressionDeclMap::DoMaterialize (bool dematerialize,
340 ExecutionContext *exe_ctx,
341 lldb_private::Value *result_value,
342 Error &err)
Sean Callanan810f22d2010-07-16 00:09:46 +0000343{
Sean Callanan336a0002010-07-17 00:43:37 +0000344 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
345
Sean Callanan810f22d2010-07-16 00:09:46 +0000346 if (!m_struct_laid_out)
347 {
348 err.SetErrorString("Structure hasn't been laid out yet");
349 return LLDB_INVALID_ADDRESS;
350 }
351
Sean Callanan810f22d2010-07-16 00:09:46 +0000352 if (!exe_ctx)
353 {
354 err.SetErrorString("Received null execution context");
355 return LLDB_INVALID_ADDRESS;
356 }
357
Sean Callanan45839272010-07-24 01:37:44 +0000358 if (!exe_ctx->frame)
359 {
360 err.SetErrorString("Received null execution frame");
361 return LLDB_INVALID_ADDRESS;
362 }
363
Sean Callanan810f22d2010-07-16 00:09:46 +0000364 const SymbolContext &sym_ctx(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything));
365
Sean Callananf328c9f2010-07-20 23:31:16 +0000366 if (!dematerialize)
Sean Callanan810f22d2010-07-16 00:09:46 +0000367 {
Sean Callananf328c9f2010-07-20 23:31:16 +0000368 if (m_materialized_location)
369 {
370 exe_ctx->process->DeallocateMemory(m_materialized_location);
371 m_materialized_location = 0;
372 }
373
374 lldb::addr_t mem = exe_ctx->process->AllocateMemory(m_struct_alignment + m_struct_size,
375 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
376 err);
377
378 if (mem == LLDB_INVALID_ADDRESS)
379 return false;
380
381 m_allocated_area = mem;
Sean Callanan810f22d2010-07-16 00:09:46 +0000382 }
383
Sean Callananf328c9f2010-07-20 23:31:16 +0000384 m_materialized_location = m_allocated_area;
385
386 if (m_materialized_location % m_struct_alignment)
387 {
388 m_materialized_location += (m_struct_alignment - (m_materialized_location % m_struct_alignment));
389 }
390
391 StructMemberIterator iter;
392
Sean Callanan810f22d2010-07-16 00:09:46 +0000393 for (iter = m_members.begin();
394 iter != m_members.end();
395 ++iter)
396 {
397 uint32_t tuple_index;
398
Sean Callanan336a0002010-07-17 00:43:37 +0000399 if (!GetIndexForDecl(tuple_index, iter->m_decl))
400 {
401 if (iter->m_name.find("___clang_expr_result") == std::string::npos)
402 {
403 err.SetErrorStringWithFormat("Unexpected variable %s", iter->m_name.c_str());
404 return false;
405 }
406
407 if (log)
408 log->Printf("Found special result variable %s", iter->m_name.c_str());
409
Sean Callananf328c9f2010-07-20 23:31:16 +0000410 if (dematerialize)
411 {
412 clang::ASTContext *context(exe_ctx->target->GetScratchClangASTContext()->getASTContext());
413
414 if (!context)
415 {
416 err.SetErrorString("Couldn't find a scratch AST context to put the result type into");
417 }
418
419 TypeFromUser copied_type(ClangASTContext::CopyType(context,
420 iter->m_parser_type.GetASTContext(),
Greg Clayton1674b122010-07-21 22:12:05 +0000421 iter->m_parser_type.GetOpaqueQualType()),
Sean Callananf328c9f2010-07-20 23:31:16 +0000422 context);
423
Sean Callanan841026f2010-07-23 00:16:21 +0000424 result_value->SetContext(Value::eContextTypeOpaqueClangQualType, copied_type.GetOpaqueQualType());
425
426 result_value->SetValueType(Value::eValueTypeLoadAddress);
427 result_value->GetScalar() = (uintptr_t)m_materialized_location + iter->m_offset;
Sean Callananf328c9f2010-07-20 23:31:16 +0000428 }
429
Sean Callanan810f22d2010-07-16 00:09:46 +0000430 continue;
Sean Callanan336a0002010-07-17 00:43:37 +0000431 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000432
433 Tuple &tuple(m_tuples[tuple_index]);
434
Sean Callananf328c9f2010-07-20 23:31:16 +0000435 if (!DoMaterializeOneVariable(dematerialize, *exe_ctx, sym_ctx, iter->m_name.c_str(), tuple.m_user_type, m_materialized_location + iter->m_offset, err))
Sean Callanan336a0002010-07-17 00:43:37 +0000436 return false;
Sean Callanan810f22d2010-07-16 00:09:46 +0000437 }
438
Sean Callananf328c9f2010-07-20 23:31:16 +0000439 return true;
440}
441
442bool
443ClangExpressionDeclMap::DoMaterializeOneVariable(bool dematerialize,
444 ExecutionContext &exe_ctx,
445 const SymbolContext &sym_ctx,
446 const char *name,
447 TypeFromUser type,
448 lldb::addr_t addr,
449 Error &err)
450{
451 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
452
453 Variable *var = FindVariableInScope(sym_ctx, name, &type);
454
455 if (!var)
456 {
457 err.SetErrorStringWithFormat("Couldn't find %s with appropriate type", name);
458 return false;
459 }
460
Sean Callanan841026f2010-07-23 00:16:21 +0000461 if (log)
462 log->Printf("%s %s with type %p", (dematerialize ? "Dematerializing" : "Materializing"), name, type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000463
464 std::auto_ptr<lldb_private::Value> location_value(GetVariableValue(exe_ctx,
465 var,
466 type.GetASTContext()));
467
468 if (!location_value.get())
469 {
470 err.SetErrorStringWithFormat("Couldn't get value for %s", name);
471 return false;
472 }
473
474 if (location_value->GetValueType() == Value::eValueTypeLoadAddress)
475 {
476 lldb::addr_t value_addr = location_value->GetScalar().ULongLong();
477
Sean Callanan841026f2010-07-23 00:16:21 +0000478 size_t bit_size = ClangASTContext::GetTypeBitSize(type.GetASTContext(), type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000479 size_t byte_size = bit_size % 8 ? ((bit_size + 8) / 8) : (bit_size / 8);
480
481 DataBufferHeap data;
482 data.SetByteSize(byte_size);
483
484 lldb::addr_t src_addr;
485 lldb::addr_t dest_addr;
486
487 if (dematerialize)
488 {
489 src_addr = addr;
490 dest_addr = value_addr;
491 }
492 else
493 {
494 src_addr = value_addr;
495 dest_addr = addr;
496 }
497
498 Error error;
499 if (exe_ctx.process->ReadMemory (src_addr, data.GetBytes(), byte_size, error) != byte_size)
500 {
501 err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
502 return false;
503 }
504
505 if (exe_ctx.process->WriteMemory (dest_addr, data.GetBytes(), byte_size, error) != byte_size)
506 {
507 err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
508 return false;
509 }
510
511 if (log)
512 log->Printf("Copied from 0x%llx to 0x%llx", (uint64_t)src_addr, (uint64_t)addr);
513 }
514 else
515 {
516 StreamString ss;
517
518 location_value->Dump(&ss);
519
520 err.SetErrorStringWithFormat("%s has a value of unhandled type: %s", name, ss.GetString().c_str());
521 }
522
523 return true;
Sean Callanan810f22d2010-07-16 00:09:46 +0000524}
525
Sean Callanan336a0002010-07-17 00:43:37 +0000526Variable*
527ClangExpressionDeclMap::FindVariableInScope(const SymbolContext &sym_ctx,
528 const char *name,
Sean Callananf328c9f2010-07-20 23:31:16 +0000529 TypeFromUser *type)
Sean Callanan810f22d2010-07-16 00:09:46 +0000530{
531 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
532
533 Function *function(m_sym_ctx->function);
534 Block *block(m_sym_ctx->block);
535
536 if (!function || !block)
537 {
538 if (log)
539 log->Printf("function = %p, block = %p", function, block);
Sean Callanan336a0002010-07-17 00:43:37 +0000540 return NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000541 }
542
543 BlockList& blocks(function->GetBlocks(true));
544
Sean Callanan336a0002010-07-17 00:43:37 +0000545 ConstString name_cs(name);
546
Sean Callanan810f22d2010-07-16 00:09:46 +0000547 lldb::user_id_t current_block_id;
548
549 for (current_block_id = block->GetID();
550 current_block_id != Block::InvalidID;
551 current_block_id = blocks.GetParent(current_block_id))
552 {
553 Block *current_block(blocks.GetBlockByID(current_block_id));
554
555 lldb::VariableListSP var_list = current_block->GetVariableList(false, true);
556
557 if (!var_list)
558 continue;
559
Sean Callanan336a0002010-07-17 00:43:37 +0000560 lldb::VariableSP var = var_list->FindVariable(name_cs);
Sean Callanan810f22d2010-07-16 00:09:46 +0000561
562 if (!var)
563 continue;
564
565 // var->GetType()->GetClangAST() is the program's AST context and holds
566 // var->GetType()->GetOpaqueClangQualType().
567
568 // type is m_type for one of the struct members, which was added by
569 // AddValueToStruct. That type was extracted from the AST context of
570 // the compiler in IRForTarget. The original for the type was copied
571 // out of the program's AST context by AddOneVariable.
572
Sean Callanan336a0002010-07-17 00:43:37 +0000573 // So that we can compare these two without having to copy back
574 // something we already had in the original AST context, we maintain
575 // m_orig_type and m_ast_context (which are passed into
576 // MaterializeOneVariable by Materialize) for each variable.
577
578 if (!type)
579 return var.get();
Sean Callanan810f22d2010-07-16 00:09:46 +0000580
Sean Callananf328c9f2010-07-20 23:31:16 +0000581 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan810f22d2010-07-16 00:09:46 +0000582 {
Sean Callananf5857a02010-07-31 01:32:05 +0000583 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType()))
Sean Callanan810f22d2010-07-16 00:09:46 +0000584 continue;
585 }
586 else
587 {
588 if (log)
589 log->PutCString("Skipping a candidate variable because of different AST contexts");
590 continue;
591 }
592
Sean Callanan336a0002010-07-17 00:43:37 +0000593 return var.get();
594 }
595
596 {
597 CompileUnit *compile_unit = m_sym_ctx->comp_unit;
Sean Callanan810f22d2010-07-16 00:09:46 +0000598
Sean Callanan336a0002010-07-17 00:43:37 +0000599 if (!compile_unit)
600 {
601 if (log)
602 log->Printf("compile_unit = %p", compile_unit);
603 return NULL;
604 }
605
606 lldb::VariableListSP var_list = compile_unit->GetVariableList(true);
607
608 if (!var_list)
609 return NULL;
610
611 lldb::VariableSP var = var_list->FindVariable(name_cs);
612
613 if (!var)
614 return NULL;
615
616 if (!type)
617 return var.get();
618
Sean Callananf328c9f2010-07-20 23:31:16 +0000619 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan336a0002010-07-17 00:43:37 +0000620 {
Sean Callanan841026f2010-07-23 00:16:21 +0000621 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType()))
Sean Callanan336a0002010-07-17 00:43:37 +0000622 return NULL;
623 }
624 else
625 {
626 if (log)
627 log->PutCString("Skipping a candidate variable because of different AST contexts");
628 return NULL;
629 }
630
631 return var.get();
632 }
633
634 return NULL;
635}
636
Chris Lattner24943d22010-06-08 16:52:24 +0000637// Interface for ClangASTSource
638void
639ClangExpressionDeclMap::GetDecls(NameSearchContext &context,
640 const char *name)
641{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000642 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
643
Sean Callanan810f22d2010-07-16 00:09:46 +0000644 if (log)
645 log->Printf("Hunting for a definition for %s", name);
Chris Lattner24943d22010-06-08 16:52:24 +0000646
647 // Back out in all cases where we're not fully initialized
648 if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
649 return;
650
651 Function *function = m_sym_ctx->function;
Chris Lattner24943d22010-06-08 16:52:24 +0000652
Sean Callanan336a0002010-07-17 00:43:37 +0000653 if (!function)
Chris Lattner24943d22010-06-08 16:52:24 +0000654 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000655 if (log)
Sean Callanan336a0002010-07-17 00:43:37 +0000656 log->Printf("Can't evaluate an expression when not in a function");
Chris Lattner24943d22010-06-08 16:52:24 +0000657 return;
658 }
659
Chris Lattner24943d22010-06-08 16:52:24 +0000660 ConstString name_cs(name);
Sean Callanan0fc73582010-07-27 00:55:47 +0000661 SymbolContextList sym_ctxs;
Chris Lattner24943d22010-06-08 16:52:24 +0000662
Sean Callanan0fc73582010-07-27 00:55:47 +0000663 m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000664
Sean Callanan0fc73582010-07-27 00:55:47 +0000665 for (uint32_t index = 0, num_indices = sym_ctxs.GetSize();
666 index < num_indices;
667 ++index)
668 {
669 SymbolContext sym_ctx;
670 sym_ctxs.GetContextAtIndex(index, sym_ctx);
671
672 if (sym_ctx.function)
673 AddOneFunction(context, sym_ctx.function, NULL);
674 else if(sym_ctx.symbol)
675 AddOneFunction(context, NULL, sym_ctx.symbol);
676 }
677
Sean Callanan336a0002010-07-17 00:43:37 +0000678 Variable *var = FindVariableInScope(*m_sym_ctx, name);
679
680 if (var)
681 AddOneVariable(context, var);
682}
683
684Value *
685ClangExpressionDeclMap::GetVariableValue(ExecutionContext &exe_ctx,
686 Variable *var,
Sean Callananf328c9f2010-07-20 23:31:16 +0000687 clang::ASTContext *parser_ast_context,
688 TypeFromUser *user_type,
689 TypeFromParser *parser_type)
Chris Lattner24943d22010-06-08 16:52:24 +0000690{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000691 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
692
Chris Lattner24943d22010-06-08 16:52:24 +0000693 Type *var_type = var->GetType();
694
695 if (!var_type)
696 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000697 if (log)
698 log->PutCString("Skipped a definition because it has no type");
Sean Callanan336a0002010-07-17 00:43:37 +0000699 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000700 }
701
702 void *var_opaque_type = var_type->GetOpaqueClangQualType();
703
704 if (!var_opaque_type)
705 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000706 if (log)
707 log->PutCString("Skipped a definition because it has no Clang type");
Sean Callanan336a0002010-07-17 00:43:37 +0000708 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000709 }
710
Chris Lattner24943d22010-06-08 16:52:24 +0000711 TypeList *type_list = var_type->GetTypeList();
712
713 if (!type_list)
714 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000715 if (log)
716 log->PutCString("Skipped a definition because the type has no associated type list");
Sean Callanan336a0002010-07-17 00:43:37 +0000717 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000718 }
719
720 clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext();
721
722 if (!exe_ast_ctx)
723 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000724 if (log)
725 log->PutCString("There is no AST context for the current execution context");
Sean Callanan336a0002010-07-17 00:43:37 +0000726 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000727 }
728
Sean Callanan336a0002010-07-17 00:43:37 +0000729 DWARFExpression &var_location_expr = var->LocationExpression();
730
Chris Lattner24943d22010-06-08 16:52:24 +0000731 std::auto_ptr<Value> var_location(new Value);
732
733 Error err;
734
Sean Callanan336a0002010-07-17 00:43:37 +0000735 if (!var_location_expr.Evaluate(&exe_ctx, exe_ast_ctx, NULL, *var_location.get(), &err))
Chris Lattner24943d22010-06-08 16:52:24 +0000736 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000737 if (log)
738 log->Printf("Error evaluating location: %s", err.AsCString());
Sean Callanan336a0002010-07-17 00:43:37 +0000739 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000740 }
741
Sean Callanan810f22d2010-07-16 00:09:46 +0000742 clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext();
743
Sean Callanan336a0002010-07-17 00:43:37 +0000744 void *type_to_use;
745
Sean Callananf328c9f2010-07-20 23:31:16 +0000746 if (parser_ast_context)
747 {
748 type_to_use = ClangASTContext::CopyType(parser_ast_context, var_ast_context, var_opaque_type);
749
750 if (parser_type)
751 *parser_type = TypeFromParser(type_to_use, parser_ast_context);
752 }
Sean Callanan336a0002010-07-17 00:43:37 +0000753 else
754 type_to_use = var_opaque_type;
Chris Lattner24943d22010-06-08 16:52:24 +0000755
756 if (var_location.get()->GetContextType() == Value::eContextTypeInvalid)
Sean Callanan336a0002010-07-17 00:43:37 +0000757 var_location.get()->SetContext(Value::eContextTypeOpaqueClangQualType, type_to_use);
Chris Lattner24943d22010-06-08 16:52:24 +0000758
759 if (var_location.get()->GetValueType() == Value::eValueTypeFileAddress)
760 {
761 SymbolContext var_sc;
762 var->CalculateSymbolContext(&var_sc);
Sean Callanan336a0002010-07-17 00:43:37 +0000763
Chris Lattner24943d22010-06-08 16:52:24 +0000764 if (!var_sc.module_sp)
Sean Callanan336a0002010-07-17 00:43:37 +0000765 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000766
767 ObjectFile *object_file = var_sc.module_sp->GetObjectFile();
768
769 if (!object_file)
Sean Callanan336a0002010-07-17 00:43:37 +0000770 return NULL;
771
Chris Lattner24943d22010-06-08 16:52:24 +0000772 Address so_addr(var_location->GetScalar().ULongLong(), object_file->GetSectionList());
773
774 lldb::addr_t load_addr = so_addr.GetLoadAddress(m_exe_ctx->process);
775
776 var_location->GetScalar() = load_addr;
777 var_location->SetValueType(Value::eValueTypeLoadAddress);
778 }
779
Sean Callananf328c9f2010-07-20 23:31:16 +0000780 if (user_type)
781 *user_type = TypeFromUser(var_opaque_type, var_ast_context);
Sean Callanan336a0002010-07-17 00:43:37 +0000782
783 return var_location.release();
784}
785
786void
787ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
788 Variable* var)
789{
790 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
791
Sean Callananf328c9f2010-07-20 23:31:16 +0000792 TypeFromUser ut;
793 TypeFromParser pt;
Sean Callanan336a0002010-07-17 00:43:37 +0000794
795 Value *var_location = GetVariableValue(*m_exe_ctx,
796 var,
797 context.GetASTContext(),
Sean Callananf328c9f2010-07-20 23:31:16 +0000798 &ut,
799 &pt);
Sean Callanan336a0002010-07-17 00:43:37 +0000800
Greg Clayton1674b122010-07-21 22:12:05 +0000801 NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType());
Chris Lattner24943d22010-06-08 16:52:24 +0000802
803 Tuple tuple;
804
Sean Callanan810f22d2010-07-16 00:09:46 +0000805 tuple.m_decl = var_decl;
Sean Callanan336a0002010-07-17 00:43:37 +0000806 tuple.m_value = var_location;
Sean Callananf328c9f2010-07-20 23:31:16 +0000807 tuple.m_user_type = ut;
808 tuple.m_parser_type = pt;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000809 tuple.m_llvm_value = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000810
811 m_tuples.push_back(tuple);
812
Sean Callanan810f22d2010-07-16 00:09:46 +0000813 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +0000814 log->Printf("Found variable %s, returned (NamedDecl)%p", context.Name.getAsString().c_str(), var_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000815}
816
817void
818ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
Sean Callanan0fc73582010-07-27 00:55:47 +0000819 Function* fun,
820 Symbol* symbol)
Sean Callanan8f0dc342010-06-22 23:46:24 +0000821{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000822 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000823
Sean Callanan0fc73582010-07-27 00:55:47 +0000824 NamedDecl *fun_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000825 std::auto_ptr<Value> fun_location(new Value);
Sean Callanan0fc73582010-07-27 00:55:47 +0000826 const Address *fun_address;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000827
Sean Callanan0fc73582010-07-27 00:55:47 +0000828 // only valid for Functions, not for Symbols
829 void *fun_opaque_type = NULL;
830 clang::ASTContext *fun_ast_context = NULL;
831
832 if (fun)
833 {
834 Type *fun_type = fun->GetType();
835
836 if (!fun_type)
837 {
838 if (log)
839 log->PutCString("Skipped a function because it has no type");
840 return;
841 }
842
843 fun_opaque_type = fun_type->GetOpaqueClangQualType();
844
845 if (!fun_opaque_type)
846 {
847 if (log)
848 log->PutCString("Skipped a function because it has no Clang type");
849 return;
850 }
851
852 fun_address = &fun->GetAddressRange().GetBaseAddress();
853
854 TypeList *type_list = fun_type->GetTypeList();
855 fun_ast_context = type_list->GetClangASTContext().getASTContext();
856 void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), fun_ast_context, fun_opaque_type);
857
858 fun_decl = context.AddFunDecl(copied_type);
859 }
860 else if (symbol)
861 {
862 fun_address = &symbol->GetAddressRangeRef().GetBaseAddress();
863
864 fun_decl = context.AddGenericFunDecl();
865 }
866 else
867 {
868 if (log)
869 log->PutCString("AddOneFunction called with no function and no symbol");
870 return;
871 }
872
873 lldb::addr_t load_addr = fun_address->GetLoadAddress(m_exe_ctx->process);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000874 fun_location->SetValueType(Value::eValueTypeLoadAddress);
875 fun_location->GetScalar() = load_addr;
876
Sean Callanan8f0dc342010-06-22 23:46:24 +0000877 Tuple tuple;
878
Sean Callanan810f22d2010-07-16 00:09:46 +0000879 tuple.m_decl = fun_decl;
880 tuple.m_value = fun_location.release();
Sean Callananf328c9f2010-07-20 23:31:16 +0000881 tuple.m_user_type = TypeFromUser(fun_opaque_type, fun_ast_context);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000882 tuple.m_llvm_value = NULL;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000883
884 m_tuples.push_back(tuple);
885
Sean Callanan810f22d2010-07-16 00:09:46 +0000886 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +0000887 log->Printf("Found function %s, returned (NamedDecl)%p", context.Name.getAsString().c_str(), fun_decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000888}