blob: da4333c49c7038a8656307af69e116099174cb3b [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"
Sean Callanana48fe162010-08-11 03:57:18 +000022#include "lldb/Expression/ClangPersistentVariables.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Symbol/ClangASTContext.h"
24#include "lldb/Symbol/CompileUnit.h"
25#include "lldb/Symbol/Function.h"
26#include "lldb/Symbol/ObjectFile.h"
27#include "lldb/Symbol/SymbolContext.h"
28#include "lldb/Symbol/Type.h"
29#include "lldb/Symbol/TypeList.h"
30#include "lldb/Symbol/Variable.h"
31#include "lldb/Symbol/VariableList.h"
Sean Callananf328c9f2010-07-20 23:31:16 +000032#include "lldb/Target/ExecutionContext.h"
Sean Callanan810f22d2010-07-16 00:09:46 +000033#include "lldb/Target/Process.h"
Chris Lattner24943d22010-06-08 16:52:24 +000034#include "lldb/Target/StackFrame.h"
Sean Callananf328c9f2010-07-20 23:31:16 +000035#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036
Chris Lattner24943d22010-06-08 16:52:24 +000037using namespace lldb_private;
38using namespace clang;
39
Sean Callanana48fe162010-08-11 03:57:18 +000040ClangExpressionDeclMap::ClangExpressionDeclMap(ExecutionContext *exe_ctx) :
41 m_exe_ctx(exe_ctx), m_struct_laid_out(false),
Sean Callanan810f22d2010-07-16 00:09:46 +000042 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;
Sean Callanana48fe162010-08-11 03:57:18 +000048
49 if (exe_ctx && exe_ctx->process)
50 m_persistent_vars = &exe_ctx->process->GetPersistentVariables();
Chris Lattner24943d22010-06-08 16:52:24 +000051}
52
53ClangExpressionDeclMap::~ClangExpressionDeclMap()
54{
55 uint32_t num_tuples = m_tuples.size ();
56 uint32_t tuple_index;
57
58 for (tuple_index = 0; tuple_index < num_tuples; ++tuple_index)
59 delete m_tuples[tuple_index].m_value;
60
61 if (m_sym_ctx)
62 delete m_sym_ctx;
63}
64
65bool
66ClangExpressionDeclMap::GetIndexForDecl (uint32_t &index,
67 const clang::Decl *decl)
68{
69 uint32_t num_tuples = m_tuples.size ();
70 uint32_t tuple_index;
71
72 for (tuple_index = 0; tuple_index < num_tuples; ++tuple_index)
73 {
74 if (m_tuples[tuple_index].m_decl == decl)
75 {
76 index = tuple_index;
77 return true;
78 }
79 }
80
81 return false;
82}
83
Sean Callanan8bce6652010-07-13 21:41:46 +000084// Interface for IRForTarget
85
86bool
Sean Callanana48fe162010-08-11 03:57:18 +000087ClangExpressionDeclMap::AddPersistentVariable (const clang::NamedDecl *decl)
88{
89 clang::ASTContext *context(m_exe_ctx->target->GetScratchClangASTContext()->getASTContext());
90
91 const clang::VarDecl *var(dyn_cast<clang::VarDecl>(decl));
92
93 if (!var)
94 return false;
95
96 TypeFromUser user_type(ClangASTContext::CopyType(context,
97 &var->getASTContext(),
98 var->getType().getAsOpaquePtr()),
99 context);
100
101 ConstString const_name(decl->getName().str().c_str());
102
103 ClangPersistentVariable *pvar = m_persistent_vars->CreateVariable(const_name, user_type);
104
105 if (!pvar)
106 return false;
107
108 return true;
109}
110
111bool
Sean Callanan8bce6652010-07-13 21:41:46 +0000112ClangExpressionDeclMap::AddValueToStruct (llvm::Value *value,
113 const clang::NamedDecl *decl,
Sean Callanan810f22d2010-07-16 00:09:46 +0000114 std::string &name,
Sean Callananf328c9f2010-07-20 23:31:16 +0000115 void *parser_type,
116 clang::ASTContext *parser_ast_context,
Sean Callanan8bce6652010-07-13 21:41:46 +0000117 size_t size,
118 off_t alignment)
119{
120 m_struct_laid_out = false;
121
122 StructMemberIterator iter;
123
124 for (iter = m_members.begin();
125 iter != m_members.end();
126 ++iter)
127 {
128 if (iter->m_decl == decl)
129 return true;
130 }
131
132 StructMember member;
133
Sean Callananf328c9f2010-07-20 23:31:16 +0000134 member.m_value = value;
135 member.m_decl = decl;
136 member.m_name = name;
137 member.m_parser_type = TypeFromParser(parser_type, parser_ast_context);
138 member.m_offset = 0;
139 member.m_size = size;
140 member.m_alignment = alignment;
Sean Callanan8bce6652010-07-13 21:41:46 +0000141
142 m_members.push_back(member);
143
144 return true;
145}
146
147bool
148ClangExpressionDeclMap::DoStructLayout ()
149{
150 if (m_struct_laid_out)
151 return true;
152
153 StructMemberIterator iter;
154
155 off_t cursor = 0;
156
157 m_struct_alignment = 0;
158 m_struct_size = 0;
159
160 for (iter = m_members.begin();
161 iter != m_members.end();
162 ++iter)
163 {
164 if (iter == m_members.begin())
165 m_struct_alignment = iter->m_alignment;
166
167 if (cursor % iter->m_alignment)
168 cursor += (iter->m_alignment - (cursor % iter->m_alignment));
169
170 iter->m_offset = cursor;
171 cursor += iter->m_size;
172 }
173
174 m_struct_size = cursor;
175
176 m_struct_laid_out = true;
177 return true;
178}
179
180bool ClangExpressionDeclMap::GetStructInfo (uint32_t &num_elements,
181 size_t &size,
182 off_t &alignment)
183{
184 if (!m_struct_laid_out)
185 return false;
186
187 num_elements = m_members.size();
188 size = m_struct_size;
189 alignment = m_struct_alignment;
190
191 return true;
192}
193
194bool
195ClangExpressionDeclMap::GetStructElement (const clang::NamedDecl *&decl,
196 llvm::Value *&value,
197 off_t &offset,
198 uint32_t index)
199{
200 if (!m_struct_laid_out)
201 return false;
202
203 if (index >= m_members.size())
204 return false;
205
206 decl = m_members[index].m_decl;
207 value = m_members[index].m_value;
208 offset = m_members[index].m_offset;
209
210 return true;
211}
212
Sean Callanan02fbafa2010-07-27 21:39:39 +0000213bool
214ClangExpressionDeclMap::GetFunctionInfo (const clang::NamedDecl *decl,
215 llvm::Value**& value,
216 uint64_t &ptr)
Sean Callananba992c52010-07-27 02:07:53 +0000217{
218 TupleIterator iter;
219
220 for (iter = m_tuples.begin();
221 iter != m_tuples.end();
222 ++iter)
223 {
224 if (decl == iter->m_decl)
225 {
Sean Callanan02fbafa2010-07-27 21:39:39 +0000226 value = &iter->m_llvm_value;
227 ptr = iter->m_value->GetScalar().ULongLong();
228 return true;
Sean Callananba992c52010-07-27 02:07:53 +0000229 }
230 }
231
Sean Callanan02fbafa2010-07-27 21:39:39 +0000232 return false;
Sean Callananba992c52010-07-27 02:07:53 +0000233}
234
Sean Callananf5857a02010-07-31 01:32:05 +0000235bool
236ClangExpressionDeclMap::GetFunctionAddress (const char *name,
237 uint64_t &ptr)
238{
239 // Back out in all cases where we're not fully initialized
240 if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
241 return false;
242
243 ConstString name_cs(name);
244 SymbolContextList sym_ctxs;
245
246 m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs);
247
248 if (!sym_ctxs.GetSize())
249 return false;
250
251 SymbolContext sym_ctx;
252 sym_ctxs.GetContextAtIndex(0, sym_ctx);
253
254 const Address *fun_address;
255
256 if (sym_ctx.function)
257 fun_address = &sym_ctx.function->GetAddressRange().GetBaseAddress();
258 else if (sym_ctx.symbol)
259 fun_address = &sym_ctx.symbol->GetAddressRangeRef().GetBaseAddress();
260 else
261 return false;
262
263 ptr = fun_address->GetLoadAddress(m_exe_ctx->process);
264
265 return true;
266}
267
Chris Lattner24943d22010-06-08 16:52:24 +0000268// Interface for DwarfExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000269lldb_private::Value
Chris Lattner24943d22010-06-08 16:52:24 +0000270*ClangExpressionDeclMap::GetValueForIndex (uint32_t index)
271{
272 if (index >= m_tuples.size ())
273 return NULL;
274
275 return m_tuples[index].m_value;
276}
277
Sean Callanan810f22d2010-07-16 00:09:46 +0000278// Interface for CommandObjectExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000279
280bool
281ClangExpressionDeclMap::Materialize (ExecutionContext *exe_ctx,
282 lldb::addr_t &struct_address,
283 Error &err)
284{
285 bool result = DoMaterialize(false, exe_ctx, NULL, err);
286
287 if (result)
288 struct_address = m_materialized_location;
289
290 return result;
291}
292
293bool
294ClangExpressionDeclMap::Dematerialize (ExecutionContext *exe_ctx,
295 lldb_private::Value &result_value,
296 Error &err)
297{
298 return DoMaterialize(true, exe_ctx, &result_value, err);
299}
300
Sean Callanan32824aa2010-07-23 22:19:18 +0000301bool
302ClangExpressionDeclMap::DumpMaterializedStruct(ExecutionContext *exe_ctx,
303 Stream &s,
304 Error &err)
305{
306 if (!m_struct_laid_out)
307 {
308 err.SetErrorString("Structure hasn't been laid out yet");
309 return false;
310 }
311
312 if (!exe_ctx)
313 {
314 err.SetErrorString("Received null execution context");
315 return false;
316 }
317
318
319 if (!exe_ctx->process)
320 {
321 err.SetErrorString("Couldn't find the process");
322 return false;
323 }
324
325 if (!exe_ctx->target)
326 {
327 err.SetErrorString("Couldn't find the target");
328 return false;
329 }
330
331 lldb::DataBufferSP data(new DataBufferHeap(m_struct_size, 0));
332
333 Error error;
334 if (exe_ctx->process->ReadMemory (m_materialized_location, data->GetBytes(), data->GetByteSize(), error) != data->GetByteSize())
335 {
336 err.SetErrorStringWithFormat ("Couldn't read struct from the target: %s", error.AsCString());
337 return false;
338 }
339
340 DataExtractor extractor(data, exe_ctx->process->GetByteOrder(), exe_ctx->target->GetArchitecture().GetAddressByteSize());
341
342 StructMemberIterator iter;
343
344 for (iter = m_members.begin();
345 iter != m_members.end();
346 ++iter)
347 {
348 s.Printf("[%s]\n", iter->m_name.c_str());
349
350 extractor.Dump(&s, // stream
351 iter->m_offset, // offset
352 lldb::eFormatBytesWithASCII, // format
353 1, // byte size of individual entries
354 iter->m_size, // number of entries
355 16, // entries per line
356 m_materialized_location + iter->m_offset, // address to print
357 0, // bit size (bitfields only; 0 means ignore)
358 0); // bit alignment (bitfields only; 0 means ignore)
359
360 s.PutChar('\n');
361 }
362
363 return true;
364}
365
Sean Callananf328c9f2010-07-20 23:31:16 +0000366bool
367ClangExpressionDeclMap::DoMaterialize (bool dematerialize,
368 ExecutionContext *exe_ctx,
369 lldb_private::Value *result_value,
370 Error &err)
Sean Callanan810f22d2010-07-16 00:09:46 +0000371{
Sean Callanan336a0002010-07-17 00:43:37 +0000372 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
373
Sean Callanan810f22d2010-07-16 00:09:46 +0000374 if (!m_struct_laid_out)
375 {
376 err.SetErrorString("Structure hasn't been laid out yet");
377 return LLDB_INVALID_ADDRESS;
378 }
379
Sean Callanan810f22d2010-07-16 00:09:46 +0000380 if (!exe_ctx)
381 {
382 err.SetErrorString("Received null execution context");
383 return LLDB_INVALID_ADDRESS;
384 }
385
Sean Callanan45839272010-07-24 01:37:44 +0000386 if (!exe_ctx->frame)
387 {
388 err.SetErrorString("Received null execution frame");
389 return LLDB_INVALID_ADDRESS;
390 }
391
Sean Callanan810f22d2010-07-16 00:09:46 +0000392 const SymbolContext &sym_ctx(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything));
393
Sean Callananf328c9f2010-07-20 23:31:16 +0000394 if (!dematerialize)
Sean Callanan810f22d2010-07-16 00:09:46 +0000395 {
Sean Callananf328c9f2010-07-20 23:31:16 +0000396 if (m_materialized_location)
397 {
398 exe_ctx->process->DeallocateMemory(m_materialized_location);
399 m_materialized_location = 0;
400 }
401
402 lldb::addr_t mem = exe_ctx->process->AllocateMemory(m_struct_alignment + m_struct_size,
403 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
404 err);
405
406 if (mem == LLDB_INVALID_ADDRESS)
407 return false;
408
409 m_allocated_area = mem;
Sean Callanan810f22d2010-07-16 00:09:46 +0000410 }
411
Sean Callananf328c9f2010-07-20 23:31:16 +0000412 m_materialized_location = m_allocated_area;
413
414 if (m_materialized_location % m_struct_alignment)
415 {
416 m_materialized_location += (m_struct_alignment - (m_materialized_location % m_struct_alignment));
417 }
418
419 StructMemberIterator iter;
420
Sean Callanan810f22d2010-07-16 00:09:46 +0000421 for (iter = m_members.begin();
422 iter != m_members.end();
423 ++iter)
424 {
425 uint32_t tuple_index;
426
Sean Callanan336a0002010-07-17 00:43:37 +0000427 if (!GetIndexForDecl(tuple_index, iter->m_decl))
428 {
Sean Callanana48fe162010-08-11 03:57:18 +0000429 if (iter->m_name[0] == '$')
430 {
431 if (!DoMaterializeOnePersistentVariable(dematerialize, *exe_ctx, iter->m_name.c_str(), m_materialized_location + iter->m_offset, err))
432 return false;
433 }
434 else if (iter->m_name.find("___clang_expr_result") != std::string::npos)
435 {
436 if (log)
437 log->Printf("Found special result variable %s", iter->m_name.c_str());
438
439 if (dematerialize)
440 {
441 clang::ASTContext *context(exe_ctx->target->GetScratchClangASTContext()->getASTContext());
442
443 if (!context)
444 {
445 err.SetErrorString("Couldn't find a scratch AST context to put the result type into");
446 }
447
448 TypeFromUser copied_type(ClangASTContext::CopyType(context,
449 iter->m_parser_type.GetASTContext(),
450 iter->m_parser_type.GetOpaqueQualType()),
451 context);
452
453 result_value->SetContext(Value::eContextTypeOpaqueClangQualType, copied_type.GetOpaqueQualType());
454
455 result_value->SetValueType(Value::eValueTypeLoadAddress);
456 result_value->GetScalar() = (uintptr_t)m_materialized_location + iter->m_offset;
457 }
458 }
459 else
Sean Callanan336a0002010-07-17 00:43:37 +0000460 {
461 err.SetErrorStringWithFormat("Unexpected variable %s", iter->m_name.c_str());
462 return false;
463 }
464
Sean Callanan810f22d2010-07-16 00:09:46 +0000465 continue;
Sean Callanan336a0002010-07-17 00:43:37 +0000466 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000467
468 Tuple &tuple(m_tuples[tuple_index]);
469
Sean Callananf328c9f2010-07-20 23:31:16 +0000470 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 +0000471 return false;
Sean Callanan810f22d2010-07-16 00:09:46 +0000472 }
473
Sean Callananf328c9f2010-07-20 23:31:16 +0000474 return true;
475}
476
Sean Callanana48fe162010-08-11 03:57:18 +0000477bool
478ClangExpressionDeclMap::DoMaterializeOnePersistentVariable(bool dematerialize,
479 ExecutionContext &exe_ctx,
480 const char *name,
481 lldb::addr_t addr,
482 Error &err)
483{
484 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
485
486 if (log)
487 log->Printf("Found persistent variable %s", name);
488
489 ClangPersistentVariable *pvar(m_persistent_vars->GetVariable(ConstString(name)));
490
491 if (!pvar)
492 {
493 err.SetErrorStringWithFormat("Undefined persistent variable %s", name);
494 return LLDB_INVALID_ADDRESS;
495 }
496
497 size_t pvar_size = pvar->Size();
498 uint8_t *pvar_data = pvar->Data();
499 Error error;
500
501 if (dematerialize)
502 {
503 if (exe_ctx.process->ReadMemory (addr, pvar_data, pvar_size, error) != pvar_size)
504 {
505 err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
506 return false;
507 }
508 }
509 else
510 {
511 if (exe_ctx.process->WriteMemory (addr, pvar_data, pvar_size, error) != pvar_size)
512 {
513 err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
514 return false;
515 }
516 }
517
518 return true;
519}
520
Sean Callananf328c9f2010-07-20 23:31:16 +0000521bool
522ClangExpressionDeclMap::DoMaterializeOneVariable(bool dematerialize,
523 ExecutionContext &exe_ctx,
524 const SymbolContext &sym_ctx,
525 const char *name,
526 TypeFromUser type,
527 lldb::addr_t addr,
528 Error &err)
529{
530 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
531
532 Variable *var = FindVariableInScope(sym_ctx, name, &type);
533
534 if (!var)
535 {
536 err.SetErrorStringWithFormat("Couldn't find %s with appropriate type", name);
537 return false;
538 }
539
Sean Callanan841026f2010-07-23 00:16:21 +0000540 if (log)
541 log->Printf("%s %s with type %p", (dematerialize ? "Dematerializing" : "Materializing"), name, type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000542
543 std::auto_ptr<lldb_private::Value> location_value(GetVariableValue(exe_ctx,
544 var,
545 type.GetASTContext()));
546
547 if (!location_value.get())
548 {
549 err.SetErrorStringWithFormat("Couldn't get value for %s", name);
550 return false;
551 }
552
553 if (location_value->GetValueType() == Value::eValueTypeLoadAddress)
554 {
555 lldb::addr_t value_addr = location_value->GetScalar().ULongLong();
556
Greg Clayton960d6a42010-08-03 00:35:52 +0000557 size_t bit_size = ClangASTType::GetClangTypeBitWidth(type.GetASTContext(), type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000558 size_t byte_size = bit_size % 8 ? ((bit_size + 8) / 8) : (bit_size / 8);
559
560 DataBufferHeap data;
561 data.SetByteSize(byte_size);
562
563 lldb::addr_t src_addr;
564 lldb::addr_t dest_addr;
565
566 if (dematerialize)
567 {
568 src_addr = addr;
569 dest_addr = value_addr;
570 }
571 else
572 {
573 src_addr = value_addr;
574 dest_addr = addr;
575 }
576
577 Error error;
578 if (exe_ctx.process->ReadMemory (src_addr, data.GetBytes(), byte_size, error) != byte_size)
579 {
580 err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
581 return false;
582 }
583
584 if (exe_ctx.process->WriteMemory (dest_addr, data.GetBytes(), byte_size, error) != byte_size)
585 {
586 err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
587 return false;
588 }
589
590 if (log)
591 log->Printf("Copied from 0x%llx to 0x%llx", (uint64_t)src_addr, (uint64_t)addr);
592 }
593 else
594 {
595 StreamString ss;
596
597 location_value->Dump(&ss);
598
599 err.SetErrorStringWithFormat("%s has a value of unhandled type: %s", name, ss.GetString().c_str());
600 }
601
602 return true;
Sean Callanan810f22d2010-07-16 00:09:46 +0000603}
604
Sean Callanan336a0002010-07-17 00:43:37 +0000605Variable*
606ClangExpressionDeclMap::FindVariableInScope(const SymbolContext &sym_ctx,
607 const char *name,
Sean Callananf328c9f2010-07-20 23:31:16 +0000608 TypeFromUser *type)
Sean Callanan810f22d2010-07-16 00:09:46 +0000609{
610 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
611
612 Function *function(m_sym_ctx->function);
613 Block *block(m_sym_ctx->block);
614
615 if (!function || !block)
616 {
617 if (log)
618 log->Printf("function = %p, block = %p", function, block);
Sean Callanan336a0002010-07-17 00:43:37 +0000619 return NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000620 }
621
622 BlockList& blocks(function->GetBlocks(true));
623
Sean Callanan336a0002010-07-17 00:43:37 +0000624 ConstString name_cs(name);
625
Sean Callanan810f22d2010-07-16 00:09:46 +0000626 lldb::user_id_t current_block_id;
627
628 for (current_block_id = block->GetID();
629 current_block_id != Block::InvalidID;
630 current_block_id = blocks.GetParent(current_block_id))
631 {
632 Block *current_block(blocks.GetBlockByID(current_block_id));
633
634 lldb::VariableListSP var_list = current_block->GetVariableList(false, true);
635
636 if (!var_list)
637 continue;
638
Sean Callanan336a0002010-07-17 00:43:37 +0000639 lldb::VariableSP var = var_list->FindVariable(name_cs);
Sean Callanan810f22d2010-07-16 00:09:46 +0000640
641 if (!var)
642 continue;
643
644 // var->GetType()->GetClangAST() is the program's AST context and holds
645 // var->GetType()->GetOpaqueClangQualType().
646
647 // type is m_type for one of the struct members, which was added by
648 // AddValueToStruct. That type was extracted from the AST context of
649 // the compiler in IRForTarget. The original for the type was copied
650 // out of the program's AST context by AddOneVariable.
651
Sean Callanan336a0002010-07-17 00:43:37 +0000652 // So that we can compare these two without having to copy back
653 // something we already had in the original AST context, we maintain
654 // m_orig_type and m_ast_context (which are passed into
655 // MaterializeOneVariable by Materialize) for each variable.
656
657 if (!type)
658 return var.get();
Sean Callanan810f22d2010-07-16 00:09:46 +0000659
Sean Callananf328c9f2010-07-20 23:31:16 +0000660 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan810f22d2010-07-16 00:09:46 +0000661 {
Sean Callananf5857a02010-07-31 01:32:05 +0000662 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType()))
Sean Callanan810f22d2010-07-16 00:09:46 +0000663 continue;
664 }
665 else
666 {
667 if (log)
668 log->PutCString("Skipping a candidate variable because of different AST contexts");
669 continue;
670 }
671
Sean Callanan336a0002010-07-17 00:43:37 +0000672 return var.get();
673 }
674
675 {
676 CompileUnit *compile_unit = m_sym_ctx->comp_unit;
Sean Callanan810f22d2010-07-16 00:09:46 +0000677
Sean Callanan336a0002010-07-17 00:43:37 +0000678 if (!compile_unit)
679 {
680 if (log)
681 log->Printf("compile_unit = %p", compile_unit);
682 return NULL;
683 }
684
685 lldb::VariableListSP var_list = compile_unit->GetVariableList(true);
686
687 if (!var_list)
688 return NULL;
689
690 lldb::VariableSP var = var_list->FindVariable(name_cs);
691
692 if (!var)
693 return NULL;
694
695 if (!type)
696 return var.get();
697
Sean Callananf328c9f2010-07-20 23:31:16 +0000698 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan336a0002010-07-17 00:43:37 +0000699 {
Sean Callanan841026f2010-07-23 00:16:21 +0000700 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType()))
Sean Callanan336a0002010-07-17 00:43:37 +0000701 return NULL;
702 }
703 else
704 {
705 if (log)
706 log->PutCString("Skipping a candidate variable because of different AST contexts");
707 return NULL;
708 }
709
710 return var.get();
711 }
712
713 return NULL;
714}
715
Chris Lattner24943d22010-06-08 16:52:24 +0000716// Interface for ClangASTSource
717void
718ClangExpressionDeclMap::GetDecls(NameSearchContext &context,
719 const char *name)
720{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000721 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
722
Sean Callanan810f22d2010-07-16 00:09:46 +0000723 if (log)
724 log->Printf("Hunting for a definition for %s", name);
Chris Lattner24943d22010-06-08 16:52:24 +0000725
726 // Back out in all cases where we're not fully initialized
727 if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
728 return;
729
730 Function *function = m_sym_ctx->function;
Chris Lattner24943d22010-06-08 16:52:24 +0000731
Sean Callanan336a0002010-07-17 00:43:37 +0000732 if (!function)
Chris Lattner24943d22010-06-08 16:52:24 +0000733 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000734 if (log)
Sean Callanan336a0002010-07-17 00:43:37 +0000735 log->Printf("Can't evaluate an expression when not in a function");
Chris Lattner24943d22010-06-08 16:52:24 +0000736 return;
737 }
738
Chris Lattner24943d22010-06-08 16:52:24 +0000739 ConstString name_cs(name);
Sean Callanan0fc73582010-07-27 00:55:47 +0000740 SymbolContextList sym_ctxs;
Chris Lattner24943d22010-06-08 16:52:24 +0000741
Sean Callanan0fc73582010-07-27 00:55:47 +0000742 m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000743
Sean Callanan0fc73582010-07-27 00:55:47 +0000744 for (uint32_t index = 0, num_indices = sym_ctxs.GetSize();
745 index < num_indices;
746 ++index)
747 {
748 SymbolContext sym_ctx;
749 sym_ctxs.GetContextAtIndex(index, sym_ctx);
750
751 if (sym_ctx.function)
752 AddOneFunction(context, sym_ctx.function, NULL);
753 else if(sym_ctx.symbol)
754 AddOneFunction(context, NULL, sym_ctx.symbol);
755 }
756
Sean Callanan336a0002010-07-17 00:43:37 +0000757 Variable *var = FindVariableInScope(*m_sym_ctx, name);
758
759 if (var)
760 AddOneVariable(context, var);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000761
Sean Callanana48fe162010-08-11 03:57:18 +0000762 ClangPersistentVariable *pvar(m_persistent_vars->GetVariable(ConstString(name)));
763
764 if (pvar)
765 AddOneVariable(context, pvar);
766
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000767 /* Commented out pending resolution of a loop when the TagType is imported
768 lldb::TypeSP type = m_sym_ctx->FindTypeByName(name_cs);
769
770 if (type.get())
771 AddOneType(context, type.get());
772 */
Sean Callanan336a0002010-07-17 00:43:37 +0000773}
774
775Value *
776ClangExpressionDeclMap::GetVariableValue(ExecutionContext &exe_ctx,
777 Variable *var,
Sean Callananf328c9f2010-07-20 23:31:16 +0000778 clang::ASTContext *parser_ast_context,
779 TypeFromUser *user_type,
780 TypeFromParser *parser_type)
Chris Lattner24943d22010-06-08 16:52:24 +0000781{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000782 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
783
Chris Lattner24943d22010-06-08 16:52:24 +0000784 Type *var_type = var->GetType();
785
786 if (!var_type)
787 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000788 if (log)
789 log->PutCString("Skipped a definition because it has no type");
Sean Callanan336a0002010-07-17 00:43:37 +0000790 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000791 }
792
793 void *var_opaque_type = var_type->GetOpaqueClangQualType();
794
795 if (!var_opaque_type)
796 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000797 if (log)
798 log->PutCString("Skipped a definition because it has no Clang type");
Sean Callanan336a0002010-07-17 00:43:37 +0000799 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000800 }
801
Chris Lattner24943d22010-06-08 16:52:24 +0000802 TypeList *type_list = var_type->GetTypeList();
803
804 if (!type_list)
805 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000806 if (log)
807 log->PutCString("Skipped a definition because the type has no associated type list");
Sean Callanan336a0002010-07-17 00:43:37 +0000808 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000809 }
810
811 clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext();
812
813 if (!exe_ast_ctx)
814 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000815 if (log)
816 log->PutCString("There is no AST context for the current execution context");
Sean Callanan336a0002010-07-17 00:43:37 +0000817 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000818 }
819
Sean Callanan336a0002010-07-17 00:43:37 +0000820 DWARFExpression &var_location_expr = var->LocationExpression();
821
Chris Lattner24943d22010-06-08 16:52:24 +0000822 std::auto_ptr<Value> var_location(new Value);
823
824 Error err;
825
Sean Callanan336a0002010-07-17 00:43:37 +0000826 if (!var_location_expr.Evaluate(&exe_ctx, exe_ast_ctx, NULL, *var_location.get(), &err))
Chris Lattner24943d22010-06-08 16:52:24 +0000827 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000828 if (log)
829 log->Printf("Error evaluating location: %s", err.AsCString());
Sean Callanan336a0002010-07-17 00:43:37 +0000830 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000831 }
832
Sean Callanan810f22d2010-07-16 00:09:46 +0000833 clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext();
834
Sean Callanan336a0002010-07-17 00:43:37 +0000835 void *type_to_use;
836
Sean Callananf328c9f2010-07-20 23:31:16 +0000837 if (parser_ast_context)
838 {
839 type_to_use = ClangASTContext::CopyType(parser_ast_context, var_ast_context, var_opaque_type);
840
841 if (parser_type)
842 *parser_type = TypeFromParser(type_to_use, parser_ast_context);
843 }
Sean Callanan336a0002010-07-17 00:43:37 +0000844 else
845 type_to_use = var_opaque_type;
Chris Lattner24943d22010-06-08 16:52:24 +0000846
847 if (var_location.get()->GetContextType() == Value::eContextTypeInvalid)
Sean Callanan336a0002010-07-17 00:43:37 +0000848 var_location.get()->SetContext(Value::eContextTypeOpaqueClangQualType, type_to_use);
Chris Lattner24943d22010-06-08 16:52:24 +0000849
850 if (var_location.get()->GetValueType() == Value::eValueTypeFileAddress)
851 {
852 SymbolContext var_sc;
853 var->CalculateSymbolContext(&var_sc);
Sean Callanan336a0002010-07-17 00:43:37 +0000854
Chris Lattner24943d22010-06-08 16:52:24 +0000855 if (!var_sc.module_sp)
Sean Callanan336a0002010-07-17 00:43:37 +0000856 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000857
858 ObjectFile *object_file = var_sc.module_sp->GetObjectFile();
859
860 if (!object_file)
Sean Callanan336a0002010-07-17 00:43:37 +0000861 return NULL;
862
Chris Lattner24943d22010-06-08 16:52:24 +0000863 Address so_addr(var_location->GetScalar().ULongLong(), object_file->GetSectionList());
864
865 lldb::addr_t load_addr = so_addr.GetLoadAddress(m_exe_ctx->process);
866
867 var_location->GetScalar() = load_addr;
868 var_location->SetValueType(Value::eValueTypeLoadAddress);
869 }
870
Sean Callananf328c9f2010-07-20 23:31:16 +0000871 if (user_type)
872 *user_type = TypeFromUser(var_opaque_type, var_ast_context);
Sean Callanan336a0002010-07-17 00:43:37 +0000873
874 return var_location.release();
875}
876
877void
878ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
879 Variable* var)
880{
881 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
882
Sean Callananf328c9f2010-07-20 23:31:16 +0000883 TypeFromUser ut;
884 TypeFromParser pt;
Sean Callanan336a0002010-07-17 00:43:37 +0000885
886 Value *var_location = GetVariableValue(*m_exe_ctx,
887 var,
888 context.GetASTContext(),
Sean Callananf328c9f2010-07-20 23:31:16 +0000889 &ut,
890 &pt);
Sean Callanan336a0002010-07-17 00:43:37 +0000891
Greg Clayton1674b122010-07-21 22:12:05 +0000892 NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType());
Chris Lattner24943d22010-06-08 16:52:24 +0000893
894 Tuple tuple;
895
Sean Callanan810f22d2010-07-16 00:09:46 +0000896 tuple.m_decl = var_decl;
Sean Callanan336a0002010-07-17 00:43:37 +0000897 tuple.m_value = var_location;
Sean Callananf328c9f2010-07-20 23:31:16 +0000898 tuple.m_user_type = ut;
899 tuple.m_parser_type = pt;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000900 tuple.m_llvm_value = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000901
902 m_tuples.push_back(tuple);
903
Sean Callanan810f22d2010-07-16 00:09:46 +0000904 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +0000905 log->Printf("Found variable %s, returned (NamedDecl)%p", context.Name.getAsString().c_str(), var_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000906}
907
908void
Sean Callanana48fe162010-08-11 03:57:18 +0000909ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
910 ClangPersistentVariable *pvar)
911{
912 TypeFromUser user_type = pvar->Type();
913
914 TypeFromParser parser_type(ClangASTContext::CopyType(context.GetASTContext(),
915 user_type.GetASTContext(),
916 user_type.GetOpaqueQualType()),
917 context.GetASTContext());
918
919 (void)context.AddVarDecl(parser_type.GetOpaqueQualType());
920}
921
922void
Sean Callanan8f0dc342010-06-22 23:46:24 +0000923ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
Sean Callanan0fc73582010-07-27 00:55:47 +0000924 Function* fun,
925 Symbol* symbol)
Sean Callanan8f0dc342010-06-22 23:46:24 +0000926{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000927 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000928
Sean Callanan0fc73582010-07-27 00:55:47 +0000929 NamedDecl *fun_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000930 std::auto_ptr<Value> fun_location(new Value);
Sean Callanan0fc73582010-07-27 00:55:47 +0000931 const Address *fun_address;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000932
Sean Callanan0fc73582010-07-27 00:55:47 +0000933 // only valid for Functions, not for Symbols
934 void *fun_opaque_type = NULL;
935 clang::ASTContext *fun_ast_context = NULL;
936
937 if (fun)
938 {
939 Type *fun_type = fun->GetType();
940
941 if (!fun_type)
942 {
943 if (log)
944 log->PutCString("Skipped a function because it has no type");
945 return;
946 }
947
948 fun_opaque_type = fun_type->GetOpaqueClangQualType();
949
950 if (!fun_opaque_type)
951 {
952 if (log)
953 log->PutCString("Skipped a function because it has no Clang type");
954 return;
955 }
956
957 fun_address = &fun->GetAddressRange().GetBaseAddress();
958
959 TypeList *type_list = fun_type->GetTypeList();
960 fun_ast_context = type_list->GetClangASTContext().getASTContext();
961 void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), fun_ast_context, fun_opaque_type);
962
963 fun_decl = context.AddFunDecl(copied_type);
964 }
965 else if (symbol)
966 {
967 fun_address = &symbol->GetAddressRangeRef().GetBaseAddress();
968
969 fun_decl = context.AddGenericFunDecl();
970 }
971 else
972 {
973 if (log)
974 log->PutCString("AddOneFunction called with no function and no symbol");
975 return;
976 }
977
978 lldb::addr_t load_addr = fun_address->GetLoadAddress(m_exe_ctx->process);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000979 fun_location->SetValueType(Value::eValueTypeLoadAddress);
980 fun_location->GetScalar() = load_addr;
981
Sean Callanan8f0dc342010-06-22 23:46:24 +0000982 Tuple tuple;
983
Sean Callanan810f22d2010-07-16 00:09:46 +0000984 tuple.m_decl = fun_decl;
985 tuple.m_value = fun_location.release();
Sean Callananf328c9f2010-07-20 23:31:16 +0000986 tuple.m_user_type = TypeFromUser(fun_opaque_type, fun_ast_context);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000987 tuple.m_llvm_value = NULL;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000988
989 m_tuples.push_back(tuple);
990
Sean Callanan810f22d2010-07-16 00:09:46 +0000991 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +0000992 log->Printf("Found function %s, returned (NamedDecl)%p", context.Name.getAsString().c_str(), fun_decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000993}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000994
995void
996ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
997 Type *type)
998{
999 TypeFromUser ut(type->GetOpaqueClangQualType(),
1000 type->GetClangAST());
1001
1002 void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), ut.GetASTContext(), ut.GetOpaqueQualType());
1003
1004 context.AddTypeDecl(copied_type);
1005}