blob: 694c59d0936d9e01ca5588e80e5bcb737d674968 [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
Chris Lattner24943d22010-06-08 16:52:24 +0000207// Interface for DwarfExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000208lldb_private::Value
Chris Lattner24943d22010-06-08 16:52:24 +0000209*ClangExpressionDeclMap::GetValueForIndex (uint32_t index)
210{
211 if (index >= m_tuples.size ())
212 return NULL;
213
214 return m_tuples[index].m_value;
215}
216
Sean Callanan810f22d2010-07-16 00:09:46 +0000217// Interface for CommandObjectExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000218
219bool
220ClangExpressionDeclMap::Materialize (ExecutionContext *exe_ctx,
221 lldb::addr_t &struct_address,
222 Error &err)
223{
224 bool result = DoMaterialize(false, exe_ctx, NULL, err);
225
226 if (result)
227 struct_address = m_materialized_location;
228
229 return result;
230}
231
232bool
233ClangExpressionDeclMap::Dematerialize (ExecutionContext *exe_ctx,
234 lldb_private::Value &result_value,
235 Error &err)
236{
237 return DoMaterialize(true, exe_ctx, &result_value, err);
238}
239
Sean Callanan32824aa2010-07-23 22:19:18 +0000240bool
241ClangExpressionDeclMap::DumpMaterializedStruct(ExecutionContext *exe_ctx,
242 Stream &s,
243 Error &err)
244{
245 if (!m_struct_laid_out)
246 {
247 err.SetErrorString("Structure hasn't been laid out yet");
248 return false;
249 }
250
251 if (!exe_ctx)
252 {
253 err.SetErrorString("Received null execution context");
254 return false;
255 }
256
257
258 if (!exe_ctx->process)
259 {
260 err.SetErrorString("Couldn't find the process");
261 return false;
262 }
263
264 if (!exe_ctx->target)
265 {
266 err.SetErrorString("Couldn't find the target");
267 return false;
268 }
269
270 lldb::DataBufferSP data(new DataBufferHeap(m_struct_size, 0));
271
272 Error error;
273 if (exe_ctx->process->ReadMemory (m_materialized_location, data->GetBytes(), data->GetByteSize(), error) != data->GetByteSize())
274 {
275 err.SetErrorStringWithFormat ("Couldn't read struct from the target: %s", error.AsCString());
276 return false;
277 }
278
279 DataExtractor extractor(data, exe_ctx->process->GetByteOrder(), exe_ctx->target->GetArchitecture().GetAddressByteSize());
280
281 StructMemberIterator iter;
282
283 for (iter = m_members.begin();
284 iter != m_members.end();
285 ++iter)
286 {
287 s.Printf("[%s]\n", iter->m_name.c_str());
288
289 extractor.Dump(&s, // stream
290 iter->m_offset, // offset
291 lldb::eFormatBytesWithASCII, // format
292 1, // byte size of individual entries
293 iter->m_size, // number of entries
294 16, // entries per line
295 m_materialized_location + iter->m_offset, // address to print
296 0, // bit size (bitfields only; 0 means ignore)
297 0); // bit alignment (bitfields only; 0 means ignore)
298
299 s.PutChar('\n');
300 }
301
302 return true;
303}
304
Sean Callananf328c9f2010-07-20 23:31:16 +0000305bool
306ClangExpressionDeclMap::DoMaterialize (bool dematerialize,
307 ExecutionContext *exe_ctx,
308 lldb_private::Value *result_value,
309 Error &err)
Sean Callanan810f22d2010-07-16 00:09:46 +0000310{
Sean Callanan336a0002010-07-17 00:43:37 +0000311 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
312
Sean Callanan810f22d2010-07-16 00:09:46 +0000313 if (!m_struct_laid_out)
314 {
315 err.SetErrorString("Structure hasn't been laid out yet");
316 return LLDB_INVALID_ADDRESS;
317 }
318
Sean Callanan810f22d2010-07-16 00:09:46 +0000319 if (!exe_ctx)
320 {
321 err.SetErrorString("Received null execution context");
322 return LLDB_INVALID_ADDRESS;
323 }
324
Sean Callanan45839272010-07-24 01:37:44 +0000325 if (!exe_ctx->frame)
326 {
327 err.SetErrorString("Received null execution frame");
328 return LLDB_INVALID_ADDRESS;
329 }
330
Sean Callanan810f22d2010-07-16 00:09:46 +0000331 const SymbolContext &sym_ctx(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything));
332
Sean Callananf328c9f2010-07-20 23:31:16 +0000333 if (!dematerialize)
Sean Callanan810f22d2010-07-16 00:09:46 +0000334 {
Sean Callananf328c9f2010-07-20 23:31:16 +0000335 if (m_materialized_location)
336 {
337 exe_ctx->process->DeallocateMemory(m_materialized_location);
338 m_materialized_location = 0;
339 }
340
341 lldb::addr_t mem = exe_ctx->process->AllocateMemory(m_struct_alignment + m_struct_size,
342 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
343 err);
344
345 if (mem == LLDB_INVALID_ADDRESS)
346 return false;
347
348 m_allocated_area = mem;
Sean Callanan810f22d2010-07-16 00:09:46 +0000349 }
350
Sean Callananf328c9f2010-07-20 23:31:16 +0000351 m_materialized_location = m_allocated_area;
352
353 if (m_materialized_location % m_struct_alignment)
354 {
355 m_materialized_location += (m_struct_alignment - (m_materialized_location % m_struct_alignment));
356 }
357
358 StructMemberIterator iter;
359
Sean Callanan810f22d2010-07-16 00:09:46 +0000360 for (iter = m_members.begin();
361 iter != m_members.end();
362 ++iter)
363 {
364 uint32_t tuple_index;
365
Sean Callanan336a0002010-07-17 00:43:37 +0000366 if (!GetIndexForDecl(tuple_index, iter->m_decl))
367 {
368 if (iter->m_name.find("___clang_expr_result") == std::string::npos)
369 {
370 err.SetErrorStringWithFormat("Unexpected variable %s", iter->m_name.c_str());
371 return false;
372 }
373
374 if (log)
375 log->Printf("Found special result variable %s", iter->m_name.c_str());
376
Sean Callananf328c9f2010-07-20 23:31:16 +0000377 if (dematerialize)
378 {
379 clang::ASTContext *context(exe_ctx->target->GetScratchClangASTContext()->getASTContext());
380
381 if (!context)
382 {
383 err.SetErrorString("Couldn't find a scratch AST context to put the result type into");
384 }
385
386 TypeFromUser copied_type(ClangASTContext::CopyType(context,
387 iter->m_parser_type.GetASTContext(),
Greg Clayton1674b122010-07-21 22:12:05 +0000388 iter->m_parser_type.GetOpaqueQualType()),
Sean Callananf328c9f2010-07-20 23:31:16 +0000389 context);
390
Sean Callanan841026f2010-07-23 00:16:21 +0000391 result_value->SetContext(Value::eContextTypeOpaqueClangQualType, copied_type.GetOpaqueQualType());
392
393 result_value->SetValueType(Value::eValueTypeLoadAddress);
394 result_value->GetScalar() = (uintptr_t)m_materialized_location + iter->m_offset;
Sean Callananf328c9f2010-07-20 23:31:16 +0000395 }
396
Sean Callanan810f22d2010-07-16 00:09:46 +0000397 continue;
Sean Callanan336a0002010-07-17 00:43:37 +0000398 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000399
400 Tuple &tuple(m_tuples[tuple_index]);
401
Sean Callananf328c9f2010-07-20 23:31:16 +0000402 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 +0000403 return false;
Sean Callanan810f22d2010-07-16 00:09:46 +0000404 }
405
Sean Callananf328c9f2010-07-20 23:31:16 +0000406 return true;
407}
408
409bool
410ClangExpressionDeclMap::DoMaterializeOneVariable(bool dematerialize,
411 ExecutionContext &exe_ctx,
412 const SymbolContext &sym_ctx,
413 const char *name,
414 TypeFromUser type,
415 lldb::addr_t addr,
416 Error &err)
417{
418 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
419
420 Variable *var = FindVariableInScope(sym_ctx, name, &type);
421
422 if (!var)
423 {
424 err.SetErrorStringWithFormat("Couldn't find %s with appropriate type", name);
425 return false;
426 }
427
Sean Callanan841026f2010-07-23 00:16:21 +0000428 if (log)
429 log->Printf("%s %s with type %p", (dematerialize ? "Dematerializing" : "Materializing"), name, type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000430
431 std::auto_ptr<lldb_private::Value> location_value(GetVariableValue(exe_ctx,
432 var,
433 type.GetASTContext()));
434
435 if (!location_value.get())
436 {
437 err.SetErrorStringWithFormat("Couldn't get value for %s", name);
438 return false;
439 }
440
441 if (location_value->GetValueType() == Value::eValueTypeLoadAddress)
442 {
443 lldb::addr_t value_addr = location_value->GetScalar().ULongLong();
444
Sean Callanan841026f2010-07-23 00:16:21 +0000445 size_t bit_size = ClangASTContext::GetTypeBitSize(type.GetASTContext(), type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000446 size_t byte_size = bit_size % 8 ? ((bit_size + 8) / 8) : (bit_size / 8);
447
448 DataBufferHeap data;
449 data.SetByteSize(byte_size);
450
451 lldb::addr_t src_addr;
452 lldb::addr_t dest_addr;
453
454 if (dematerialize)
455 {
456 src_addr = addr;
457 dest_addr = value_addr;
458 }
459 else
460 {
461 src_addr = value_addr;
462 dest_addr = addr;
463 }
464
465 Error error;
466 if (exe_ctx.process->ReadMemory (src_addr, data.GetBytes(), byte_size, error) != byte_size)
467 {
468 err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
469 return false;
470 }
471
472 if (exe_ctx.process->WriteMemory (dest_addr, data.GetBytes(), byte_size, error) != byte_size)
473 {
474 err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
475 return false;
476 }
477
478 if (log)
479 log->Printf("Copied from 0x%llx to 0x%llx", (uint64_t)src_addr, (uint64_t)addr);
480 }
481 else
482 {
483 StreamString ss;
484
485 location_value->Dump(&ss);
486
487 err.SetErrorStringWithFormat("%s has a value of unhandled type: %s", name, ss.GetString().c_str());
488 }
489
490 return true;
Sean Callanan810f22d2010-07-16 00:09:46 +0000491}
492
Sean Callanan336a0002010-07-17 00:43:37 +0000493Variable*
494ClangExpressionDeclMap::FindVariableInScope(const SymbolContext &sym_ctx,
495 const char *name,
Sean Callananf328c9f2010-07-20 23:31:16 +0000496 TypeFromUser *type)
Sean Callanan810f22d2010-07-16 00:09:46 +0000497{
498 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
499
500 Function *function(m_sym_ctx->function);
501 Block *block(m_sym_ctx->block);
502
503 if (!function || !block)
504 {
505 if (log)
506 log->Printf("function = %p, block = %p", function, block);
Sean Callanan336a0002010-07-17 00:43:37 +0000507 return NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000508 }
509
510 BlockList& blocks(function->GetBlocks(true));
511
Sean Callanan336a0002010-07-17 00:43:37 +0000512 ConstString name_cs(name);
513
Sean Callanan810f22d2010-07-16 00:09:46 +0000514 lldb::user_id_t current_block_id;
515
516 for (current_block_id = block->GetID();
517 current_block_id != Block::InvalidID;
518 current_block_id = blocks.GetParent(current_block_id))
519 {
520 Block *current_block(blocks.GetBlockByID(current_block_id));
521
522 lldb::VariableListSP var_list = current_block->GetVariableList(false, true);
523
524 if (!var_list)
525 continue;
526
Sean Callanan336a0002010-07-17 00:43:37 +0000527 lldb::VariableSP var = var_list->FindVariable(name_cs);
Sean Callanan810f22d2010-07-16 00:09:46 +0000528
529 if (!var)
530 continue;
531
532 // var->GetType()->GetClangAST() is the program's AST context and holds
533 // var->GetType()->GetOpaqueClangQualType().
534
535 // type is m_type for one of the struct members, which was added by
536 // AddValueToStruct. That type was extracted from the AST context of
537 // the compiler in IRForTarget. The original for the type was copied
538 // out of the program's AST context by AddOneVariable.
539
Sean Callanan336a0002010-07-17 00:43:37 +0000540 // So that we can compare these two without having to copy back
541 // something we already had in the original AST context, we maintain
542 // m_orig_type and m_ast_context (which are passed into
543 // MaterializeOneVariable by Materialize) for each variable.
544
545 if (!type)
546 return var.get();
Sean Callanan810f22d2010-07-16 00:09:46 +0000547
Sean Callananf328c9f2010-07-20 23:31:16 +0000548 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan810f22d2010-07-16 00:09:46 +0000549 {
Sean Callananf328c9f2010-07-20 23:31:16 +0000550 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type, var->GetType()->GetOpaqueClangQualType()))
Sean Callanan810f22d2010-07-16 00:09:46 +0000551 continue;
552 }
553 else
554 {
555 if (log)
556 log->PutCString("Skipping a candidate variable because of different AST contexts");
557 continue;
558 }
559
Sean Callanan336a0002010-07-17 00:43:37 +0000560 return var.get();
561 }
562
563 {
564 CompileUnit *compile_unit = m_sym_ctx->comp_unit;
Sean Callanan810f22d2010-07-16 00:09:46 +0000565
Sean Callanan336a0002010-07-17 00:43:37 +0000566 if (!compile_unit)
567 {
568 if (log)
569 log->Printf("compile_unit = %p", compile_unit);
570 return NULL;
571 }
572
573 lldb::VariableListSP var_list = compile_unit->GetVariableList(true);
574
575 if (!var_list)
576 return NULL;
577
578 lldb::VariableSP var = var_list->FindVariable(name_cs);
579
580 if (!var)
581 return NULL;
582
583 if (!type)
584 return var.get();
585
Sean Callananf328c9f2010-07-20 23:31:16 +0000586 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan336a0002010-07-17 00:43:37 +0000587 {
Sean Callanan841026f2010-07-23 00:16:21 +0000588 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType()))
Sean Callanan336a0002010-07-17 00:43:37 +0000589 return NULL;
590 }
591 else
592 {
593 if (log)
594 log->PutCString("Skipping a candidate variable because of different AST contexts");
595 return NULL;
596 }
597
598 return var.get();
599 }
600
601 return NULL;
602}
603
Chris Lattner24943d22010-06-08 16:52:24 +0000604// Interface for ClangASTSource
605void
606ClangExpressionDeclMap::GetDecls(NameSearchContext &context,
607 const char *name)
608{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000609 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
610
Sean Callanan810f22d2010-07-16 00:09:46 +0000611 if (log)
612 log->Printf("Hunting for a definition for %s", name);
Chris Lattner24943d22010-06-08 16:52:24 +0000613
614 // Back out in all cases where we're not fully initialized
615 if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
616 return;
617
618 Function *function = m_sym_ctx->function;
Chris Lattner24943d22010-06-08 16:52:24 +0000619
Sean Callanan336a0002010-07-17 00:43:37 +0000620 if (!function)
Chris Lattner24943d22010-06-08 16:52:24 +0000621 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000622 if (log)
Sean Callanan336a0002010-07-17 00:43:37 +0000623 log->Printf("Can't evaluate an expression when not in a function");
Chris Lattner24943d22010-06-08 16:52:24 +0000624 return;
625 }
626
Chris Lattner24943d22010-06-08 16:52:24 +0000627 ConstString name_cs(name);
Sean Callanan0fc73582010-07-27 00:55:47 +0000628 SymbolContextList sym_ctxs;
Chris Lattner24943d22010-06-08 16:52:24 +0000629
Sean Callanan0fc73582010-07-27 00:55:47 +0000630 m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000631
Sean Callanan0fc73582010-07-27 00:55:47 +0000632 for (uint32_t index = 0, num_indices = sym_ctxs.GetSize();
633 index < num_indices;
634 ++index)
635 {
636 SymbolContext sym_ctx;
637 sym_ctxs.GetContextAtIndex(index, sym_ctx);
638
639 if (sym_ctx.function)
640 AddOneFunction(context, sym_ctx.function, NULL);
641 else if(sym_ctx.symbol)
642 AddOneFunction(context, NULL, sym_ctx.symbol);
643 }
644
Sean Callanan336a0002010-07-17 00:43:37 +0000645 Variable *var = FindVariableInScope(*m_sym_ctx, name);
646
647 if (var)
648 AddOneVariable(context, var);
649}
650
651Value *
652ClangExpressionDeclMap::GetVariableValue(ExecutionContext &exe_ctx,
653 Variable *var,
Sean Callananf328c9f2010-07-20 23:31:16 +0000654 clang::ASTContext *parser_ast_context,
655 TypeFromUser *user_type,
656 TypeFromParser *parser_type)
Chris Lattner24943d22010-06-08 16:52:24 +0000657{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000658 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
659
Chris Lattner24943d22010-06-08 16:52:24 +0000660 Type *var_type = var->GetType();
661
662 if (!var_type)
663 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000664 if (log)
665 log->PutCString("Skipped a definition because it has no type");
Sean Callanan336a0002010-07-17 00:43:37 +0000666 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000667 }
668
669 void *var_opaque_type = var_type->GetOpaqueClangQualType();
670
671 if (!var_opaque_type)
672 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000673 if (log)
674 log->PutCString("Skipped a definition because it has no Clang type");
Sean Callanan336a0002010-07-17 00:43:37 +0000675 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000676 }
677
Chris Lattner24943d22010-06-08 16:52:24 +0000678 TypeList *type_list = var_type->GetTypeList();
679
680 if (!type_list)
681 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000682 if (log)
683 log->PutCString("Skipped a definition because the type has no associated type list");
Sean Callanan336a0002010-07-17 00:43:37 +0000684 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000685 }
686
687 clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext();
688
689 if (!exe_ast_ctx)
690 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000691 if (log)
692 log->PutCString("There is no AST context for the current execution context");
Sean Callanan336a0002010-07-17 00:43:37 +0000693 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000694 }
695
Sean Callanan336a0002010-07-17 00:43:37 +0000696 DWARFExpression &var_location_expr = var->LocationExpression();
697
Chris Lattner24943d22010-06-08 16:52:24 +0000698 std::auto_ptr<Value> var_location(new Value);
699
700 Error err;
701
Sean Callanan336a0002010-07-17 00:43:37 +0000702 if (!var_location_expr.Evaluate(&exe_ctx, exe_ast_ctx, NULL, *var_location.get(), &err))
Chris Lattner24943d22010-06-08 16:52:24 +0000703 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000704 if (log)
705 log->Printf("Error evaluating location: %s", err.AsCString());
Sean Callanan336a0002010-07-17 00:43:37 +0000706 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000707 }
708
Sean Callanan810f22d2010-07-16 00:09:46 +0000709 clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext();
710
Sean Callanan336a0002010-07-17 00:43:37 +0000711 void *type_to_use;
712
Sean Callananf328c9f2010-07-20 23:31:16 +0000713 if (parser_ast_context)
714 {
715 type_to_use = ClangASTContext::CopyType(parser_ast_context, var_ast_context, var_opaque_type);
716
717 if (parser_type)
718 *parser_type = TypeFromParser(type_to_use, parser_ast_context);
719 }
Sean Callanan336a0002010-07-17 00:43:37 +0000720 else
721 type_to_use = var_opaque_type;
Chris Lattner24943d22010-06-08 16:52:24 +0000722
723 if (var_location.get()->GetContextType() == Value::eContextTypeInvalid)
Sean Callanan336a0002010-07-17 00:43:37 +0000724 var_location.get()->SetContext(Value::eContextTypeOpaqueClangQualType, type_to_use);
Chris Lattner24943d22010-06-08 16:52:24 +0000725
726 if (var_location.get()->GetValueType() == Value::eValueTypeFileAddress)
727 {
728 SymbolContext var_sc;
729 var->CalculateSymbolContext(&var_sc);
Sean Callanan336a0002010-07-17 00:43:37 +0000730
Chris Lattner24943d22010-06-08 16:52:24 +0000731 if (!var_sc.module_sp)
Sean Callanan336a0002010-07-17 00:43:37 +0000732 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000733
734 ObjectFile *object_file = var_sc.module_sp->GetObjectFile();
735
736 if (!object_file)
Sean Callanan336a0002010-07-17 00:43:37 +0000737 return NULL;
738
Chris Lattner24943d22010-06-08 16:52:24 +0000739 Address so_addr(var_location->GetScalar().ULongLong(), object_file->GetSectionList());
740
741 lldb::addr_t load_addr = so_addr.GetLoadAddress(m_exe_ctx->process);
742
743 var_location->GetScalar() = load_addr;
744 var_location->SetValueType(Value::eValueTypeLoadAddress);
745 }
746
Sean Callananf328c9f2010-07-20 23:31:16 +0000747 if (user_type)
748 *user_type = TypeFromUser(var_opaque_type, var_ast_context);
Sean Callanan336a0002010-07-17 00:43:37 +0000749
750 return var_location.release();
751}
752
753void
754ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
755 Variable* var)
756{
757 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
758
Sean Callananf328c9f2010-07-20 23:31:16 +0000759 TypeFromUser ut;
760 TypeFromParser pt;
Sean Callanan336a0002010-07-17 00:43:37 +0000761
762 Value *var_location = GetVariableValue(*m_exe_ctx,
763 var,
764 context.GetASTContext(),
Sean Callananf328c9f2010-07-20 23:31:16 +0000765 &ut,
766 &pt);
Sean Callanan336a0002010-07-17 00:43:37 +0000767
Greg Clayton1674b122010-07-21 22:12:05 +0000768 NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType());
Chris Lattner24943d22010-06-08 16:52:24 +0000769
770 Tuple tuple;
771
Sean Callanan810f22d2010-07-16 00:09:46 +0000772 tuple.m_decl = var_decl;
Sean Callanan336a0002010-07-17 00:43:37 +0000773 tuple.m_value = var_location;
Sean Callananf328c9f2010-07-20 23:31:16 +0000774 tuple.m_user_type = ut;
775 tuple.m_parser_type = pt;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000776 tuple.m_llvm_value = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000777
778 m_tuples.push_back(tuple);
779
Sean Callanan810f22d2010-07-16 00:09:46 +0000780 if (log)
781 log->PutCString("Found variable");
Sean Callanan8f0dc342010-06-22 23:46:24 +0000782}
783
784void
785ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
Sean Callanan0fc73582010-07-27 00:55:47 +0000786 Function* fun,
787 Symbol* symbol)
Sean Callanan8f0dc342010-06-22 23:46:24 +0000788{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000789 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000790
Sean Callanan0fc73582010-07-27 00:55:47 +0000791 NamedDecl *fun_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000792 std::auto_ptr<Value> fun_location(new Value);
Sean Callanan0fc73582010-07-27 00:55:47 +0000793 const Address *fun_address;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000794
Sean Callanan0fc73582010-07-27 00:55:47 +0000795 // only valid for Functions, not for Symbols
796 void *fun_opaque_type = NULL;
797 clang::ASTContext *fun_ast_context = NULL;
798
799 if (fun)
800 {
801 Type *fun_type = fun->GetType();
802
803 if (!fun_type)
804 {
805 if (log)
806 log->PutCString("Skipped a function because it has no type");
807 return;
808 }
809
810 fun_opaque_type = fun_type->GetOpaqueClangQualType();
811
812 if (!fun_opaque_type)
813 {
814 if (log)
815 log->PutCString("Skipped a function because it has no Clang type");
816 return;
817 }
818
819 fun_address = &fun->GetAddressRange().GetBaseAddress();
820
821 TypeList *type_list = fun_type->GetTypeList();
822 fun_ast_context = type_list->GetClangASTContext().getASTContext();
823 void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), fun_ast_context, fun_opaque_type);
824
825 fun_decl = context.AddFunDecl(copied_type);
826 }
827 else if (symbol)
828 {
829 fun_address = &symbol->GetAddressRangeRef().GetBaseAddress();
830
831 fun_decl = context.AddGenericFunDecl();
832 }
833 else
834 {
835 if (log)
836 log->PutCString("AddOneFunction called with no function and no symbol");
837 return;
838 }
839
840 lldb::addr_t load_addr = fun_address->GetLoadAddress(m_exe_ctx->process);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000841 fun_location->SetValueType(Value::eValueTypeLoadAddress);
842 fun_location->GetScalar() = load_addr;
843
Sean Callanan8f0dc342010-06-22 23:46:24 +0000844 Tuple tuple;
845
Sean Callanan810f22d2010-07-16 00:09:46 +0000846 tuple.m_decl = fun_decl;
847 tuple.m_value = fun_location.release();
Sean Callananf328c9f2010-07-20 23:31:16 +0000848 tuple.m_user_type = TypeFromUser(fun_opaque_type, fun_ast_context);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000849 tuple.m_llvm_value = NULL;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000850
851 m_tuples.push_back(tuple);
852
Sean Callanan810f22d2010-07-16 00:09:46 +0000853 if (log)
854 log->PutCString("Found function");
Chris Lattner24943d22010-06-08 16:52:24 +0000855}