blob: c26b77d75e6d9b98547986bbfa107d27056c0aa7 [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 Callananba992c52010-07-27 02:07:53 +0000185uint64_t
186ClangExpressionDeclMap::GetFunctionAddress (const clang::NamedDecl *decl)
187{
188 TupleIterator iter;
189
190 for (iter = m_tuples.begin();
191 iter != m_tuples.end();
192 ++iter)
193 {
194 if (decl == iter->m_decl)
195 {
196 return iter->m_value->GetScalar().ULongLong();
197 }
198 }
199
200 return 0;
201}
202
Chris Lattner24943d22010-06-08 16:52:24 +0000203// Interface for DwarfExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000204lldb_private::Value
Chris Lattner24943d22010-06-08 16:52:24 +0000205*ClangExpressionDeclMap::GetValueForIndex (uint32_t index)
206{
207 if (index >= m_tuples.size ())
208 return NULL;
209
210 return m_tuples[index].m_value;
211}
212
Sean Callanan810f22d2010-07-16 00:09:46 +0000213// Interface for CommandObjectExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000214
215bool
216ClangExpressionDeclMap::Materialize (ExecutionContext *exe_ctx,
217 lldb::addr_t &struct_address,
218 Error &err)
219{
220 bool result = DoMaterialize(false, exe_ctx, NULL, err);
221
222 if (result)
223 struct_address = m_materialized_location;
224
225 return result;
226}
227
228bool
229ClangExpressionDeclMap::Dematerialize (ExecutionContext *exe_ctx,
230 lldb_private::Value &result_value,
231 Error &err)
232{
233 return DoMaterialize(true, exe_ctx, &result_value, err);
234}
235
Sean Callanan32824aa2010-07-23 22:19:18 +0000236bool
237ClangExpressionDeclMap::DumpMaterializedStruct(ExecutionContext *exe_ctx,
238 Stream &s,
239 Error &err)
240{
241 if (!m_struct_laid_out)
242 {
243 err.SetErrorString("Structure hasn't been laid out yet");
244 return false;
245 }
246
247 if (!exe_ctx)
248 {
249 err.SetErrorString("Received null execution context");
250 return false;
251 }
252
253
254 if (!exe_ctx->process)
255 {
256 err.SetErrorString("Couldn't find the process");
257 return false;
258 }
259
260 if (!exe_ctx->target)
261 {
262 err.SetErrorString("Couldn't find the target");
263 return false;
264 }
265
266 lldb::DataBufferSP data(new DataBufferHeap(m_struct_size, 0));
267
268 Error error;
269 if (exe_ctx->process->ReadMemory (m_materialized_location, data->GetBytes(), data->GetByteSize(), error) != data->GetByteSize())
270 {
271 err.SetErrorStringWithFormat ("Couldn't read struct from the target: %s", error.AsCString());
272 return false;
273 }
274
275 DataExtractor extractor(data, exe_ctx->process->GetByteOrder(), exe_ctx->target->GetArchitecture().GetAddressByteSize());
276
277 StructMemberIterator iter;
278
279 for (iter = m_members.begin();
280 iter != m_members.end();
281 ++iter)
282 {
283 s.Printf("[%s]\n", iter->m_name.c_str());
284
285 extractor.Dump(&s, // stream
286 iter->m_offset, // offset
287 lldb::eFormatBytesWithASCII, // format
288 1, // byte size of individual entries
289 iter->m_size, // number of entries
290 16, // entries per line
291 m_materialized_location + iter->m_offset, // address to print
292 0, // bit size (bitfields only; 0 means ignore)
293 0); // bit alignment (bitfields only; 0 means ignore)
294
295 s.PutChar('\n');
296 }
297
298 return true;
299}
300
Sean Callananf328c9f2010-07-20 23:31:16 +0000301bool
302ClangExpressionDeclMap::DoMaterialize (bool dematerialize,
303 ExecutionContext *exe_ctx,
304 lldb_private::Value *result_value,
305 Error &err)
Sean Callanan810f22d2010-07-16 00:09:46 +0000306{
Sean Callanan336a0002010-07-17 00:43:37 +0000307 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
308
Sean Callanan810f22d2010-07-16 00:09:46 +0000309 if (!m_struct_laid_out)
310 {
311 err.SetErrorString("Structure hasn't been laid out yet");
312 return LLDB_INVALID_ADDRESS;
313 }
314
Sean Callanan810f22d2010-07-16 00:09:46 +0000315 if (!exe_ctx)
316 {
317 err.SetErrorString("Received null execution context");
318 return LLDB_INVALID_ADDRESS;
319 }
320
Sean Callanan45839272010-07-24 01:37:44 +0000321 if (!exe_ctx->frame)
322 {
323 err.SetErrorString("Received null execution frame");
324 return LLDB_INVALID_ADDRESS;
325 }
326
Sean Callanan810f22d2010-07-16 00:09:46 +0000327 const SymbolContext &sym_ctx(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything));
328
Sean Callananf328c9f2010-07-20 23:31:16 +0000329 if (!dematerialize)
Sean Callanan810f22d2010-07-16 00:09:46 +0000330 {
Sean Callananf328c9f2010-07-20 23:31:16 +0000331 if (m_materialized_location)
332 {
333 exe_ctx->process->DeallocateMemory(m_materialized_location);
334 m_materialized_location = 0;
335 }
336
337 lldb::addr_t mem = exe_ctx->process->AllocateMemory(m_struct_alignment + m_struct_size,
338 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
339 err);
340
341 if (mem == LLDB_INVALID_ADDRESS)
342 return false;
343
344 m_allocated_area = mem;
Sean Callanan810f22d2010-07-16 00:09:46 +0000345 }
346
Sean Callananf328c9f2010-07-20 23:31:16 +0000347 m_materialized_location = m_allocated_area;
348
349 if (m_materialized_location % m_struct_alignment)
350 {
351 m_materialized_location += (m_struct_alignment - (m_materialized_location % m_struct_alignment));
352 }
353
354 StructMemberIterator iter;
355
Sean Callanan810f22d2010-07-16 00:09:46 +0000356 for (iter = m_members.begin();
357 iter != m_members.end();
358 ++iter)
359 {
360 uint32_t tuple_index;
361
Sean Callanan336a0002010-07-17 00:43:37 +0000362 if (!GetIndexForDecl(tuple_index, iter->m_decl))
363 {
364 if (iter->m_name.find("___clang_expr_result") == std::string::npos)
365 {
366 err.SetErrorStringWithFormat("Unexpected variable %s", iter->m_name.c_str());
367 return false;
368 }
369
370 if (log)
371 log->Printf("Found special result variable %s", iter->m_name.c_str());
372
Sean Callananf328c9f2010-07-20 23:31:16 +0000373 if (dematerialize)
374 {
375 clang::ASTContext *context(exe_ctx->target->GetScratchClangASTContext()->getASTContext());
376
377 if (!context)
378 {
379 err.SetErrorString("Couldn't find a scratch AST context to put the result type into");
380 }
381
382 TypeFromUser copied_type(ClangASTContext::CopyType(context,
383 iter->m_parser_type.GetASTContext(),
Greg Clayton1674b122010-07-21 22:12:05 +0000384 iter->m_parser_type.GetOpaqueQualType()),
Sean Callananf328c9f2010-07-20 23:31:16 +0000385 context);
386
Sean Callanan841026f2010-07-23 00:16:21 +0000387 result_value->SetContext(Value::eContextTypeOpaqueClangQualType, copied_type.GetOpaqueQualType());
388
389 result_value->SetValueType(Value::eValueTypeLoadAddress);
390 result_value->GetScalar() = (uintptr_t)m_materialized_location + iter->m_offset;
Sean Callananf328c9f2010-07-20 23:31:16 +0000391 }
392
Sean Callanan810f22d2010-07-16 00:09:46 +0000393 continue;
Sean Callanan336a0002010-07-17 00:43:37 +0000394 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000395
396 Tuple &tuple(m_tuples[tuple_index]);
397
Sean Callananf328c9f2010-07-20 23:31:16 +0000398 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 +0000399 return false;
Sean Callanan810f22d2010-07-16 00:09:46 +0000400 }
401
Sean Callananf328c9f2010-07-20 23:31:16 +0000402 return true;
403}
404
405bool
406ClangExpressionDeclMap::DoMaterializeOneVariable(bool dematerialize,
407 ExecutionContext &exe_ctx,
408 const SymbolContext &sym_ctx,
409 const char *name,
410 TypeFromUser type,
411 lldb::addr_t addr,
412 Error &err)
413{
414 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
415
416 Variable *var = FindVariableInScope(sym_ctx, name, &type);
417
418 if (!var)
419 {
420 err.SetErrorStringWithFormat("Couldn't find %s with appropriate type", name);
421 return false;
422 }
423
Sean Callanan841026f2010-07-23 00:16:21 +0000424 if (log)
425 log->Printf("%s %s with type %p", (dematerialize ? "Dematerializing" : "Materializing"), name, type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000426
427 std::auto_ptr<lldb_private::Value> location_value(GetVariableValue(exe_ctx,
428 var,
429 type.GetASTContext()));
430
431 if (!location_value.get())
432 {
433 err.SetErrorStringWithFormat("Couldn't get value for %s", name);
434 return false;
435 }
436
437 if (location_value->GetValueType() == Value::eValueTypeLoadAddress)
438 {
439 lldb::addr_t value_addr = location_value->GetScalar().ULongLong();
440
Sean Callanan841026f2010-07-23 00:16:21 +0000441 size_t bit_size = ClangASTContext::GetTypeBitSize(type.GetASTContext(), type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000442 size_t byte_size = bit_size % 8 ? ((bit_size + 8) / 8) : (bit_size / 8);
443
444 DataBufferHeap data;
445 data.SetByteSize(byte_size);
446
447 lldb::addr_t src_addr;
448 lldb::addr_t dest_addr;
449
450 if (dematerialize)
451 {
452 src_addr = addr;
453 dest_addr = value_addr;
454 }
455 else
456 {
457 src_addr = value_addr;
458 dest_addr = addr;
459 }
460
461 Error error;
462 if (exe_ctx.process->ReadMemory (src_addr, data.GetBytes(), byte_size, error) != byte_size)
463 {
464 err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
465 return false;
466 }
467
468 if (exe_ctx.process->WriteMemory (dest_addr, data.GetBytes(), byte_size, error) != byte_size)
469 {
470 err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
471 return false;
472 }
473
474 if (log)
475 log->Printf("Copied from 0x%llx to 0x%llx", (uint64_t)src_addr, (uint64_t)addr);
476 }
477 else
478 {
479 StreamString ss;
480
481 location_value->Dump(&ss);
482
483 err.SetErrorStringWithFormat("%s has a value of unhandled type: %s", name, ss.GetString().c_str());
484 }
485
486 return true;
Sean Callanan810f22d2010-07-16 00:09:46 +0000487}
488
Sean Callanan336a0002010-07-17 00:43:37 +0000489Variable*
490ClangExpressionDeclMap::FindVariableInScope(const SymbolContext &sym_ctx,
491 const char *name,
Sean Callananf328c9f2010-07-20 23:31:16 +0000492 TypeFromUser *type)
Sean Callanan810f22d2010-07-16 00:09:46 +0000493{
494 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
495
496 Function *function(m_sym_ctx->function);
497 Block *block(m_sym_ctx->block);
498
499 if (!function || !block)
500 {
501 if (log)
502 log->Printf("function = %p, block = %p", function, block);
Sean Callanan336a0002010-07-17 00:43:37 +0000503 return NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000504 }
505
506 BlockList& blocks(function->GetBlocks(true));
507
Sean Callanan336a0002010-07-17 00:43:37 +0000508 ConstString name_cs(name);
509
Sean Callanan810f22d2010-07-16 00:09:46 +0000510 lldb::user_id_t current_block_id;
511
512 for (current_block_id = block->GetID();
513 current_block_id != Block::InvalidID;
514 current_block_id = blocks.GetParent(current_block_id))
515 {
516 Block *current_block(blocks.GetBlockByID(current_block_id));
517
518 lldb::VariableListSP var_list = current_block->GetVariableList(false, true);
519
520 if (!var_list)
521 continue;
522
Sean Callanan336a0002010-07-17 00:43:37 +0000523 lldb::VariableSP var = var_list->FindVariable(name_cs);
Sean Callanan810f22d2010-07-16 00:09:46 +0000524
525 if (!var)
526 continue;
527
528 // var->GetType()->GetClangAST() is the program's AST context and holds
529 // var->GetType()->GetOpaqueClangQualType().
530
531 // type is m_type for one of the struct members, which was added by
532 // AddValueToStruct. That type was extracted from the AST context of
533 // the compiler in IRForTarget. The original for the type was copied
534 // out of the program's AST context by AddOneVariable.
535
Sean Callanan336a0002010-07-17 00:43:37 +0000536 // So that we can compare these two without having to copy back
537 // something we already had in the original AST context, we maintain
538 // m_orig_type and m_ast_context (which are passed into
539 // MaterializeOneVariable by Materialize) for each variable.
540
541 if (!type)
542 return var.get();
Sean Callanan810f22d2010-07-16 00:09:46 +0000543
Sean Callananf328c9f2010-07-20 23:31:16 +0000544 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan810f22d2010-07-16 00:09:46 +0000545 {
Sean Callananf328c9f2010-07-20 23:31:16 +0000546 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type, var->GetType()->GetOpaqueClangQualType()))
Sean Callanan810f22d2010-07-16 00:09:46 +0000547 continue;
548 }
549 else
550 {
551 if (log)
552 log->PutCString("Skipping a candidate variable because of different AST contexts");
553 continue;
554 }
555
Sean Callanan336a0002010-07-17 00:43:37 +0000556 return var.get();
557 }
558
559 {
560 CompileUnit *compile_unit = m_sym_ctx->comp_unit;
Sean Callanan810f22d2010-07-16 00:09:46 +0000561
Sean Callanan336a0002010-07-17 00:43:37 +0000562 if (!compile_unit)
563 {
564 if (log)
565 log->Printf("compile_unit = %p", compile_unit);
566 return NULL;
567 }
568
569 lldb::VariableListSP var_list = compile_unit->GetVariableList(true);
570
571 if (!var_list)
572 return NULL;
573
574 lldb::VariableSP var = var_list->FindVariable(name_cs);
575
576 if (!var)
577 return NULL;
578
579 if (!type)
580 return var.get();
581
Sean Callananf328c9f2010-07-20 23:31:16 +0000582 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan336a0002010-07-17 00:43:37 +0000583 {
Sean Callanan841026f2010-07-23 00:16:21 +0000584 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType()))
Sean Callanan336a0002010-07-17 00:43:37 +0000585 return NULL;
586 }
587 else
588 {
589 if (log)
590 log->PutCString("Skipping a candidate variable because of different AST contexts");
591 return NULL;
592 }
593
594 return var.get();
595 }
596
597 return NULL;
598}
599
Chris Lattner24943d22010-06-08 16:52:24 +0000600// Interface for ClangASTSource
601void
602ClangExpressionDeclMap::GetDecls(NameSearchContext &context,
603 const char *name)
604{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000605 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
606
Sean Callanan810f22d2010-07-16 00:09:46 +0000607 if (log)
608 log->Printf("Hunting for a definition for %s", name);
Chris Lattner24943d22010-06-08 16:52:24 +0000609
610 // Back out in all cases where we're not fully initialized
611 if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
612 return;
613
614 Function *function = m_sym_ctx->function;
Chris Lattner24943d22010-06-08 16:52:24 +0000615
Sean Callanan336a0002010-07-17 00:43:37 +0000616 if (!function)
Chris Lattner24943d22010-06-08 16:52:24 +0000617 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000618 if (log)
Sean Callanan336a0002010-07-17 00:43:37 +0000619 log->Printf("Can't evaluate an expression when not in a function");
Chris Lattner24943d22010-06-08 16:52:24 +0000620 return;
621 }
622
Chris Lattner24943d22010-06-08 16:52:24 +0000623 ConstString name_cs(name);
Sean Callanan0fc73582010-07-27 00:55:47 +0000624 SymbolContextList sym_ctxs;
Chris Lattner24943d22010-06-08 16:52:24 +0000625
Sean Callanan0fc73582010-07-27 00:55:47 +0000626 m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000627
Sean Callanan0fc73582010-07-27 00:55:47 +0000628 for (uint32_t index = 0, num_indices = sym_ctxs.GetSize();
629 index < num_indices;
630 ++index)
631 {
632 SymbolContext sym_ctx;
633 sym_ctxs.GetContextAtIndex(index, sym_ctx);
634
635 if (sym_ctx.function)
636 AddOneFunction(context, sym_ctx.function, NULL);
637 else if(sym_ctx.symbol)
638 AddOneFunction(context, NULL, sym_ctx.symbol);
639 }
640
Sean Callanan336a0002010-07-17 00:43:37 +0000641 Variable *var = FindVariableInScope(*m_sym_ctx, name);
642
643 if (var)
644 AddOneVariable(context, var);
645}
646
647Value *
648ClangExpressionDeclMap::GetVariableValue(ExecutionContext &exe_ctx,
649 Variable *var,
Sean Callananf328c9f2010-07-20 23:31:16 +0000650 clang::ASTContext *parser_ast_context,
651 TypeFromUser *user_type,
652 TypeFromParser *parser_type)
Chris Lattner24943d22010-06-08 16:52:24 +0000653{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000654 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
655
Chris Lattner24943d22010-06-08 16:52:24 +0000656 Type *var_type = var->GetType();
657
658 if (!var_type)
659 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000660 if (log)
661 log->PutCString("Skipped a definition because it has no type");
Sean Callanan336a0002010-07-17 00:43:37 +0000662 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000663 }
664
665 void *var_opaque_type = var_type->GetOpaqueClangQualType();
666
667 if (!var_opaque_type)
668 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000669 if (log)
670 log->PutCString("Skipped a definition because it has no Clang type");
Sean Callanan336a0002010-07-17 00:43:37 +0000671 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000672 }
673
Chris Lattner24943d22010-06-08 16:52:24 +0000674 TypeList *type_list = var_type->GetTypeList();
675
676 if (!type_list)
677 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000678 if (log)
679 log->PutCString("Skipped a definition because the type has no associated type list");
Sean Callanan336a0002010-07-17 00:43:37 +0000680 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000681 }
682
683 clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext();
684
685 if (!exe_ast_ctx)
686 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000687 if (log)
688 log->PutCString("There is no AST context for the current execution context");
Sean Callanan336a0002010-07-17 00:43:37 +0000689 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000690 }
691
Sean Callanan336a0002010-07-17 00:43:37 +0000692 DWARFExpression &var_location_expr = var->LocationExpression();
693
Chris Lattner24943d22010-06-08 16:52:24 +0000694 std::auto_ptr<Value> var_location(new Value);
695
696 Error err;
697
Sean Callanan336a0002010-07-17 00:43:37 +0000698 if (!var_location_expr.Evaluate(&exe_ctx, exe_ast_ctx, NULL, *var_location.get(), &err))
Chris Lattner24943d22010-06-08 16:52:24 +0000699 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000700 if (log)
701 log->Printf("Error evaluating location: %s", err.AsCString());
Sean Callanan336a0002010-07-17 00:43:37 +0000702 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000703 }
704
Sean Callanan810f22d2010-07-16 00:09:46 +0000705 clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext();
706
Sean Callanan336a0002010-07-17 00:43:37 +0000707 void *type_to_use;
708
Sean Callananf328c9f2010-07-20 23:31:16 +0000709 if (parser_ast_context)
710 {
711 type_to_use = ClangASTContext::CopyType(parser_ast_context, var_ast_context, var_opaque_type);
712
713 if (parser_type)
714 *parser_type = TypeFromParser(type_to_use, parser_ast_context);
715 }
Sean Callanan336a0002010-07-17 00:43:37 +0000716 else
717 type_to_use = var_opaque_type;
Chris Lattner24943d22010-06-08 16:52:24 +0000718
719 if (var_location.get()->GetContextType() == Value::eContextTypeInvalid)
Sean Callanan336a0002010-07-17 00:43:37 +0000720 var_location.get()->SetContext(Value::eContextTypeOpaqueClangQualType, type_to_use);
Chris Lattner24943d22010-06-08 16:52:24 +0000721
722 if (var_location.get()->GetValueType() == Value::eValueTypeFileAddress)
723 {
724 SymbolContext var_sc;
725 var->CalculateSymbolContext(&var_sc);
Sean Callanan336a0002010-07-17 00:43:37 +0000726
Chris Lattner24943d22010-06-08 16:52:24 +0000727 if (!var_sc.module_sp)
Sean Callanan336a0002010-07-17 00:43:37 +0000728 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000729
730 ObjectFile *object_file = var_sc.module_sp->GetObjectFile();
731
732 if (!object_file)
Sean Callanan336a0002010-07-17 00:43:37 +0000733 return NULL;
734
Chris Lattner24943d22010-06-08 16:52:24 +0000735 Address so_addr(var_location->GetScalar().ULongLong(), object_file->GetSectionList());
736
737 lldb::addr_t load_addr = so_addr.GetLoadAddress(m_exe_ctx->process);
738
739 var_location->GetScalar() = load_addr;
740 var_location->SetValueType(Value::eValueTypeLoadAddress);
741 }
742
Sean Callananf328c9f2010-07-20 23:31:16 +0000743 if (user_type)
744 *user_type = TypeFromUser(var_opaque_type, var_ast_context);
Sean Callanan336a0002010-07-17 00:43:37 +0000745
746 return var_location.release();
747}
748
749void
750ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
751 Variable* var)
752{
753 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
754
Sean Callananf328c9f2010-07-20 23:31:16 +0000755 TypeFromUser ut;
756 TypeFromParser pt;
Sean Callanan336a0002010-07-17 00:43:37 +0000757
758 Value *var_location = GetVariableValue(*m_exe_ctx,
759 var,
760 context.GetASTContext(),
Sean Callananf328c9f2010-07-20 23:31:16 +0000761 &ut,
762 &pt);
Sean Callanan336a0002010-07-17 00:43:37 +0000763
Greg Clayton1674b122010-07-21 22:12:05 +0000764 NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType());
Chris Lattner24943d22010-06-08 16:52:24 +0000765
766 Tuple tuple;
767
Sean Callanan810f22d2010-07-16 00:09:46 +0000768 tuple.m_decl = var_decl;
Sean Callanan336a0002010-07-17 00:43:37 +0000769 tuple.m_value = var_location;
Sean Callananf328c9f2010-07-20 23:31:16 +0000770 tuple.m_user_type = ut;
771 tuple.m_parser_type = pt;
Chris Lattner24943d22010-06-08 16:52:24 +0000772
773 m_tuples.push_back(tuple);
774
Sean Callanan810f22d2010-07-16 00:09:46 +0000775 if (log)
776 log->PutCString("Found variable");
Sean Callanan8f0dc342010-06-22 23:46:24 +0000777}
778
779void
780ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
Sean Callanan0fc73582010-07-27 00:55:47 +0000781 Function* fun,
782 Symbol* symbol)
Sean Callanan8f0dc342010-06-22 23:46:24 +0000783{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000784 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000785
Sean Callanan0fc73582010-07-27 00:55:47 +0000786 NamedDecl *fun_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000787 std::auto_ptr<Value> fun_location(new Value);
Sean Callanan0fc73582010-07-27 00:55:47 +0000788 const Address *fun_address;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000789
Sean Callanan0fc73582010-07-27 00:55:47 +0000790 // only valid for Functions, not for Symbols
791 void *fun_opaque_type = NULL;
792 clang::ASTContext *fun_ast_context = NULL;
793
794 if (fun)
795 {
796 Type *fun_type = fun->GetType();
797
798 if (!fun_type)
799 {
800 if (log)
801 log->PutCString("Skipped a function because it has no type");
802 return;
803 }
804
805 fun_opaque_type = fun_type->GetOpaqueClangQualType();
806
807 if (!fun_opaque_type)
808 {
809 if (log)
810 log->PutCString("Skipped a function because it has no Clang type");
811 return;
812 }
813
814 fun_address = &fun->GetAddressRange().GetBaseAddress();
815
816 TypeList *type_list = fun_type->GetTypeList();
817 fun_ast_context = type_list->GetClangASTContext().getASTContext();
818 void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), fun_ast_context, fun_opaque_type);
819
820 fun_decl = context.AddFunDecl(copied_type);
821 }
822 else if (symbol)
823 {
824 fun_address = &symbol->GetAddressRangeRef().GetBaseAddress();
825
826 fun_decl = context.AddGenericFunDecl();
827 }
828 else
829 {
830 if (log)
831 log->PutCString("AddOneFunction called with no function and no symbol");
832 return;
833 }
834
835 lldb::addr_t load_addr = fun_address->GetLoadAddress(m_exe_ctx->process);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000836 fun_location->SetValueType(Value::eValueTypeLoadAddress);
837 fun_location->GetScalar() = load_addr;
838
Sean Callanan8f0dc342010-06-22 23:46:24 +0000839 Tuple tuple;
840
Sean Callanan810f22d2010-07-16 00:09:46 +0000841 tuple.m_decl = fun_decl;
842 tuple.m_value = fun_location.release();
Sean Callananf328c9f2010-07-20 23:31:16 +0000843 tuple.m_user_type = TypeFromUser(fun_opaque_type, fun_ast_context);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000844
845 m_tuples.push_back(tuple);
846
Sean Callanan810f22d2010-07-16 00:09:46 +0000847 if (log)
848 log->PutCString("Found function");
Chris Lattner24943d22010-06-08 16:52:24 +0000849}