blob: b50d68ef64a3450a6b923f808c84b738666959f6 [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
Sean Callanane562a072010-08-06 00:35:32 +000039ClangExpressionDeclMap::ClangExpressionDeclMap(ExecutionContext *exe_ctx,
40 ClangPersistentVariables &persistent_vars) :
Sean Callanan8bce6652010-07-13 21:41:46 +000041 m_exe_ctx(exe_ctx),
Sean Callanane562a072010-08-06 00:35:32 +000042 m_persistent_vars(persistent_vars),
Sean Callanan810f22d2010-07-16 00:09:46 +000043 m_struct_laid_out(false),
44 m_materialized_location(0)
Chris Lattner24943d22010-06-08 16:52:24 +000045{
46 if (exe_ctx && exe_ctx->frame)
47 m_sym_ctx = new SymbolContext(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything));
48 else
49 m_sym_ctx = NULL;
50}
51
52ClangExpressionDeclMap::~ClangExpressionDeclMap()
53{
54 uint32_t num_tuples = m_tuples.size ();
55 uint32_t tuple_index;
56
57 for (tuple_index = 0; tuple_index < num_tuples; ++tuple_index)
58 delete m_tuples[tuple_index].m_value;
59
60 if (m_sym_ctx)
61 delete m_sym_ctx;
62}
63
64bool
65ClangExpressionDeclMap::GetIndexForDecl (uint32_t &index,
66 const clang::Decl *decl)
67{
68 uint32_t num_tuples = m_tuples.size ();
69 uint32_t tuple_index;
70
71 for (tuple_index = 0; tuple_index < num_tuples; ++tuple_index)
72 {
73 if (m_tuples[tuple_index].m_decl == decl)
74 {
75 index = tuple_index;
76 return true;
77 }
78 }
79
80 return false;
81}
82
Sean Callanan8bce6652010-07-13 21:41:46 +000083// Interface for IRForTarget
84
85bool
86ClangExpressionDeclMap::AddValueToStruct (llvm::Value *value,
87 const clang::NamedDecl *decl,
Sean Callanan810f22d2010-07-16 00:09:46 +000088 std::string &name,
Sean Callananf328c9f2010-07-20 23:31:16 +000089 void *parser_type,
90 clang::ASTContext *parser_ast_context,
Sean Callanan8bce6652010-07-13 21:41:46 +000091 size_t size,
92 off_t alignment)
93{
94 m_struct_laid_out = false;
95
96 StructMemberIterator iter;
97
98 for (iter = m_members.begin();
99 iter != m_members.end();
100 ++iter)
101 {
102 if (iter->m_decl == decl)
103 return true;
104 }
105
106 StructMember member;
107
Sean Callananf328c9f2010-07-20 23:31:16 +0000108 member.m_value = value;
109 member.m_decl = decl;
110 member.m_name = name;
111 member.m_parser_type = TypeFromParser(parser_type, parser_ast_context);
112 member.m_offset = 0;
113 member.m_size = size;
114 member.m_alignment = alignment;
Sean Callanan8bce6652010-07-13 21:41:46 +0000115
116 m_members.push_back(member);
117
118 return true;
119}
120
121bool
122ClangExpressionDeclMap::DoStructLayout ()
123{
124 if (m_struct_laid_out)
125 return true;
126
127 StructMemberIterator iter;
128
129 off_t cursor = 0;
130
131 m_struct_alignment = 0;
132 m_struct_size = 0;
133
134 for (iter = m_members.begin();
135 iter != m_members.end();
136 ++iter)
137 {
138 if (iter == m_members.begin())
139 m_struct_alignment = iter->m_alignment;
140
141 if (cursor % iter->m_alignment)
142 cursor += (iter->m_alignment - (cursor % iter->m_alignment));
143
144 iter->m_offset = cursor;
145 cursor += iter->m_size;
146 }
147
148 m_struct_size = cursor;
149
150 m_struct_laid_out = true;
151 return true;
152}
153
154bool ClangExpressionDeclMap::GetStructInfo (uint32_t &num_elements,
155 size_t &size,
156 off_t &alignment)
157{
158 if (!m_struct_laid_out)
159 return false;
160
161 num_elements = m_members.size();
162 size = m_struct_size;
163 alignment = m_struct_alignment;
164
165 return true;
166}
167
168bool
169ClangExpressionDeclMap::GetStructElement (const clang::NamedDecl *&decl,
170 llvm::Value *&value,
171 off_t &offset,
172 uint32_t index)
173{
174 if (!m_struct_laid_out)
175 return false;
176
177 if (index >= m_members.size())
178 return false;
179
180 decl = m_members[index].m_decl;
181 value = m_members[index].m_value;
182 offset = m_members[index].m_offset;
183
184 return true;
185}
186
Sean Callanan02fbafa2010-07-27 21:39:39 +0000187bool
188ClangExpressionDeclMap::GetFunctionInfo (const clang::NamedDecl *decl,
189 llvm::Value**& value,
190 uint64_t &ptr)
Sean Callananba992c52010-07-27 02:07:53 +0000191{
192 TupleIterator iter;
193
194 for (iter = m_tuples.begin();
195 iter != m_tuples.end();
196 ++iter)
197 {
198 if (decl == iter->m_decl)
199 {
Sean Callanan02fbafa2010-07-27 21:39:39 +0000200 value = &iter->m_llvm_value;
201 ptr = iter->m_value->GetScalar().ULongLong();
202 return true;
Sean Callananba992c52010-07-27 02:07:53 +0000203 }
204 }
205
Sean Callanan02fbafa2010-07-27 21:39:39 +0000206 return false;
Sean Callananba992c52010-07-27 02:07:53 +0000207}
208
Sean Callananf5857a02010-07-31 01:32:05 +0000209bool
210ClangExpressionDeclMap::GetFunctionAddress (const char *name,
211 uint64_t &ptr)
212{
213 // Back out in all cases where we're not fully initialized
214 if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
215 return false;
216
217 ConstString name_cs(name);
218 SymbolContextList sym_ctxs;
219
220 m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs);
221
222 if (!sym_ctxs.GetSize())
223 return false;
224
225 SymbolContext sym_ctx;
226 sym_ctxs.GetContextAtIndex(0, sym_ctx);
227
228 const Address *fun_address;
229
230 if (sym_ctx.function)
231 fun_address = &sym_ctx.function->GetAddressRange().GetBaseAddress();
232 else if (sym_ctx.symbol)
233 fun_address = &sym_ctx.symbol->GetAddressRangeRef().GetBaseAddress();
234 else
235 return false;
236
237 ptr = fun_address->GetLoadAddress(m_exe_ctx->process);
238
239 return true;
240}
241
Chris Lattner24943d22010-06-08 16:52:24 +0000242// Interface for DwarfExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000243lldb_private::Value
Chris Lattner24943d22010-06-08 16:52:24 +0000244*ClangExpressionDeclMap::GetValueForIndex (uint32_t index)
245{
246 if (index >= m_tuples.size ())
247 return NULL;
248
249 return m_tuples[index].m_value;
250}
251
Sean Callanan810f22d2010-07-16 00:09:46 +0000252// Interface for CommandObjectExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000253
254bool
255ClangExpressionDeclMap::Materialize (ExecutionContext *exe_ctx,
256 lldb::addr_t &struct_address,
257 Error &err)
258{
259 bool result = DoMaterialize(false, exe_ctx, NULL, err);
260
261 if (result)
262 struct_address = m_materialized_location;
263
264 return result;
265}
266
267bool
268ClangExpressionDeclMap::Dematerialize (ExecutionContext *exe_ctx,
269 lldb_private::Value &result_value,
270 Error &err)
271{
272 return DoMaterialize(true, exe_ctx, &result_value, err);
273}
274
Sean Callanan32824aa2010-07-23 22:19:18 +0000275bool
276ClangExpressionDeclMap::DumpMaterializedStruct(ExecutionContext *exe_ctx,
277 Stream &s,
278 Error &err)
279{
280 if (!m_struct_laid_out)
281 {
282 err.SetErrorString("Structure hasn't been laid out yet");
283 return false;
284 }
285
286 if (!exe_ctx)
287 {
288 err.SetErrorString("Received null execution context");
289 return false;
290 }
291
292
293 if (!exe_ctx->process)
294 {
295 err.SetErrorString("Couldn't find the process");
296 return false;
297 }
298
299 if (!exe_ctx->target)
300 {
301 err.SetErrorString("Couldn't find the target");
302 return false;
303 }
304
305 lldb::DataBufferSP data(new DataBufferHeap(m_struct_size, 0));
306
307 Error error;
308 if (exe_ctx->process->ReadMemory (m_materialized_location, data->GetBytes(), data->GetByteSize(), error) != data->GetByteSize())
309 {
310 err.SetErrorStringWithFormat ("Couldn't read struct from the target: %s", error.AsCString());
311 return false;
312 }
313
314 DataExtractor extractor(data, exe_ctx->process->GetByteOrder(), exe_ctx->target->GetArchitecture().GetAddressByteSize());
315
316 StructMemberIterator iter;
317
318 for (iter = m_members.begin();
319 iter != m_members.end();
320 ++iter)
321 {
322 s.Printf("[%s]\n", iter->m_name.c_str());
323
324 extractor.Dump(&s, // stream
325 iter->m_offset, // offset
326 lldb::eFormatBytesWithASCII, // format
327 1, // byte size of individual entries
328 iter->m_size, // number of entries
329 16, // entries per line
330 m_materialized_location + iter->m_offset, // address to print
331 0, // bit size (bitfields only; 0 means ignore)
332 0); // bit alignment (bitfields only; 0 means ignore)
333
334 s.PutChar('\n');
335 }
336
337 return true;
338}
339
Sean Callananf328c9f2010-07-20 23:31:16 +0000340bool
341ClangExpressionDeclMap::DoMaterialize (bool dematerialize,
342 ExecutionContext *exe_ctx,
343 lldb_private::Value *result_value,
344 Error &err)
Sean Callanan810f22d2010-07-16 00:09:46 +0000345{
Sean Callanan336a0002010-07-17 00:43:37 +0000346 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
347
Sean Callanan810f22d2010-07-16 00:09:46 +0000348 if (!m_struct_laid_out)
349 {
350 err.SetErrorString("Structure hasn't been laid out yet");
351 return LLDB_INVALID_ADDRESS;
352 }
353
Sean Callanan810f22d2010-07-16 00:09:46 +0000354 if (!exe_ctx)
355 {
356 err.SetErrorString("Received null execution context");
357 return LLDB_INVALID_ADDRESS;
358 }
359
Sean Callanan45839272010-07-24 01:37:44 +0000360 if (!exe_ctx->frame)
361 {
362 err.SetErrorString("Received null execution frame");
363 return LLDB_INVALID_ADDRESS;
364 }
365
Sean Callanan810f22d2010-07-16 00:09:46 +0000366 const SymbolContext &sym_ctx(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything));
367
Sean Callananf328c9f2010-07-20 23:31:16 +0000368 if (!dematerialize)
Sean Callanan810f22d2010-07-16 00:09:46 +0000369 {
Sean Callananf328c9f2010-07-20 23:31:16 +0000370 if (m_materialized_location)
371 {
372 exe_ctx->process->DeallocateMemory(m_materialized_location);
373 m_materialized_location = 0;
374 }
375
376 lldb::addr_t mem = exe_ctx->process->AllocateMemory(m_struct_alignment + m_struct_size,
377 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
378 err);
379
380 if (mem == LLDB_INVALID_ADDRESS)
381 return false;
382
383 m_allocated_area = mem;
Sean Callanan810f22d2010-07-16 00:09:46 +0000384 }
385
Sean Callananf328c9f2010-07-20 23:31:16 +0000386 m_materialized_location = m_allocated_area;
387
388 if (m_materialized_location % m_struct_alignment)
389 {
390 m_materialized_location += (m_struct_alignment - (m_materialized_location % m_struct_alignment));
391 }
392
393 StructMemberIterator iter;
394
Sean Callanan810f22d2010-07-16 00:09:46 +0000395 for (iter = m_members.begin();
396 iter != m_members.end();
397 ++iter)
398 {
399 uint32_t tuple_index;
400
Sean Callanan336a0002010-07-17 00:43:37 +0000401 if (!GetIndexForDecl(tuple_index, iter->m_decl))
402 {
403 if (iter->m_name.find("___clang_expr_result") == std::string::npos)
404 {
405 err.SetErrorStringWithFormat("Unexpected variable %s", iter->m_name.c_str());
406 return false;
407 }
408
409 if (log)
410 log->Printf("Found special result variable %s", iter->m_name.c_str());
411
Sean Callananf328c9f2010-07-20 23:31:16 +0000412 if (dematerialize)
413 {
414 clang::ASTContext *context(exe_ctx->target->GetScratchClangASTContext()->getASTContext());
415
416 if (!context)
417 {
418 err.SetErrorString("Couldn't find a scratch AST context to put the result type into");
419 }
420
421 TypeFromUser copied_type(ClangASTContext::CopyType(context,
422 iter->m_parser_type.GetASTContext(),
Greg Clayton1674b122010-07-21 22:12:05 +0000423 iter->m_parser_type.GetOpaqueQualType()),
Sean Callananf328c9f2010-07-20 23:31:16 +0000424 context);
425
Sean Callanan841026f2010-07-23 00:16:21 +0000426 result_value->SetContext(Value::eContextTypeOpaqueClangQualType, copied_type.GetOpaqueQualType());
427
428 result_value->SetValueType(Value::eValueTypeLoadAddress);
429 result_value->GetScalar() = (uintptr_t)m_materialized_location + iter->m_offset;
Sean Callananf328c9f2010-07-20 23:31:16 +0000430 }
431
Sean Callanan810f22d2010-07-16 00:09:46 +0000432 continue;
Sean Callanan336a0002010-07-17 00:43:37 +0000433 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000434
435 Tuple &tuple(m_tuples[tuple_index]);
436
Sean Callananf328c9f2010-07-20 23:31:16 +0000437 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 +0000438 return false;
Sean Callanan810f22d2010-07-16 00:09:46 +0000439 }
440
Sean Callananf328c9f2010-07-20 23:31:16 +0000441 return true;
442}
443
444bool
445ClangExpressionDeclMap::DoMaterializeOneVariable(bool dematerialize,
446 ExecutionContext &exe_ctx,
447 const SymbolContext &sym_ctx,
448 const char *name,
449 TypeFromUser type,
450 lldb::addr_t addr,
451 Error &err)
452{
453 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
454
455 Variable *var = FindVariableInScope(sym_ctx, name, &type);
456
457 if (!var)
458 {
459 err.SetErrorStringWithFormat("Couldn't find %s with appropriate type", name);
460 return false;
461 }
462
Sean Callanan841026f2010-07-23 00:16:21 +0000463 if (log)
464 log->Printf("%s %s with type %p", (dematerialize ? "Dematerializing" : "Materializing"), name, type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000465
466 std::auto_ptr<lldb_private::Value> location_value(GetVariableValue(exe_ctx,
467 var,
468 type.GetASTContext()));
469
470 if (!location_value.get())
471 {
472 err.SetErrorStringWithFormat("Couldn't get value for %s", name);
473 return false;
474 }
475
476 if (location_value->GetValueType() == Value::eValueTypeLoadAddress)
477 {
478 lldb::addr_t value_addr = location_value->GetScalar().ULongLong();
479
Greg Clayton960d6a42010-08-03 00:35:52 +0000480 size_t bit_size = ClangASTType::GetClangTypeBitWidth(type.GetASTContext(), type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000481 size_t byte_size = bit_size % 8 ? ((bit_size + 8) / 8) : (bit_size / 8);
482
483 DataBufferHeap data;
484 data.SetByteSize(byte_size);
485
486 lldb::addr_t src_addr;
487 lldb::addr_t dest_addr;
488
489 if (dematerialize)
490 {
491 src_addr = addr;
492 dest_addr = value_addr;
493 }
494 else
495 {
496 src_addr = value_addr;
497 dest_addr = addr;
498 }
499
500 Error error;
501 if (exe_ctx.process->ReadMemory (src_addr, data.GetBytes(), byte_size, error) != byte_size)
502 {
503 err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
504 return false;
505 }
506
507 if (exe_ctx.process->WriteMemory (dest_addr, data.GetBytes(), byte_size, error) != byte_size)
508 {
509 err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
510 return false;
511 }
512
513 if (log)
514 log->Printf("Copied from 0x%llx to 0x%llx", (uint64_t)src_addr, (uint64_t)addr);
515 }
516 else
517 {
518 StreamString ss;
519
520 location_value->Dump(&ss);
521
522 err.SetErrorStringWithFormat("%s has a value of unhandled type: %s", name, ss.GetString().c_str());
523 }
524
525 return true;
Sean Callanan810f22d2010-07-16 00:09:46 +0000526}
527
Sean Callanan336a0002010-07-17 00:43:37 +0000528Variable*
529ClangExpressionDeclMap::FindVariableInScope(const SymbolContext &sym_ctx,
530 const char *name,
Sean Callananf328c9f2010-07-20 23:31:16 +0000531 TypeFromUser *type)
Sean Callanan810f22d2010-07-16 00:09:46 +0000532{
533 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
534
535 Function *function(m_sym_ctx->function);
536 Block *block(m_sym_ctx->block);
537
538 if (!function || !block)
539 {
540 if (log)
541 log->Printf("function = %p, block = %p", function, block);
Sean Callanan336a0002010-07-17 00:43:37 +0000542 return NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000543 }
544
545 BlockList& blocks(function->GetBlocks(true));
546
Sean Callanan336a0002010-07-17 00:43:37 +0000547 ConstString name_cs(name);
548
Sean Callanan810f22d2010-07-16 00:09:46 +0000549 lldb::user_id_t current_block_id;
550
551 for (current_block_id = block->GetID();
552 current_block_id != Block::InvalidID;
553 current_block_id = blocks.GetParent(current_block_id))
554 {
555 Block *current_block(blocks.GetBlockByID(current_block_id));
556
557 lldb::VariableListSP var_list = current_block->GetVariableList(false, true);
558
559 if (!var_list)
560 continue;
561
Sean Callanan336a0002010-07-17 00:43:37 +0000562 lldb::VariableSP var = var_list->FindVariable(name_cs);
Sean Callanan810f22d2010-07-16 00:09:46 +0000563
564 if (!var)
565 continue;
566
567 // var->GetType()->GetClangAST() is the program's AST context and holds
568 // var->GetType()->GetOpaqueClangQualType().
569
570 // type is m_type for one of the struct members, which was added by
571 // AddValueToStruct. That type was extracted from the AST context of
572 // the compiler in IRForTarget. The original for the type was copied
573 // out of the program's AST context by AddOneVariable.
574
Sean Callanan336a0002010-07-17 00:43:37 +0000575 // So that we can compare these two without having to copy back
576 // something we already had in the original AST context, we maintain
577 // m_orig_type and m_ast_context (which are passed into
578 // MaterializeOneVariable by Materialize) for each variable.
579
580 if (!type)
581 return var.get();
Sean Callanan810f22d2010-07-16 00:09:46 +0000582
Sean Callananf328c9f2010-07-20 23:31:16 +0000583 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan810f22d2010-07-16 00:09:46 +0000584 {
Sean Callananf5857a02010-07-31 01:32:05 +0000585 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType()))
Sean Callanan810f22d2010-07-16 00:09:46 +0000586 continue;
587 }
588 else
589 {
590 if (log)
591 log->PutCString("Skipping a candidate variable because of different AST contexts");
592 continue;
593 }
594
Sean Callanan336a0002010-07-17 00:43:37 +0000595 return var.get();
596 }
597
598 {
599 CompileUnit *compile_unit = m_sym_ctx->comp_unit;
Sean Callanan810f22d2010-07-16 00:09:46 +0000600
Sean Callanan336a0002010-07-17 00:43:37 +0000601 if (!compile_unit)
602 {
603 if (log)
604 log->Printf("compile_unit = %p", compile_unit);
605 return NULL;
606 }
607
608 lldb::VariableListSP var_list = compile_unit->GetVariableList(true);
609
610 if (!var_list)
611 return NULL;
612
613 lldb::VariableSP var = var_list->FindVariable(name_cs);
614
615 if (!var)
616 return NULL;
617
618 if (!type)
619 return var.get();
620
Sean Callananf328c9f2010-07-20 23:31:16 +0000621 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan336a0002010-07-17 00:43:37 +0000622 {
Sean Callanan841026f2010-07-23 00:16:21 +0000623 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type->GetOpaqueQualType(), var->GetType()->GetOpaqueClangQualType()))
Sean Callanan336a0002010-07-17 00:43:37 +0000624 return NULL;
625 }
626 else
627 {
628 if (log)
629 log->PutCString("Skipping a candidate variable because of different AST contexts");
630 return NULL;
631 }
632
633 return var.get();
634 }
635
636 return NULL;
637}
638
Chris Lattner24943d22010-06-08 16:52:24 +0000639// Interface for ClangASTSource
640void
641ClangExpressionDeclMap::GetDecls(NameSearchContext &context,
642 const char *name)
643{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000644 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
645
Sean Callanan810f22d2010-07-16 00:09:46 +0000646 if (log)
647 log->Printf("Hunting for a definition for %s", name);
Chris Lattner24943d22010-06-08 16:52:24 +0000648
649 // Back out in all cases where we're not fully initialized
650 if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
651 return;
652
653 Function *function = m_sym_ctx->function;
Chris Lattner24943d22010-06-08 16:52:24 +0000654
Sean Callanan336a0002010-07-17 00:43:37 +0000655 if (!function)
Chris Lattner24943d22010-06-08 16:52:24 +0000656 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000657 if (log)
Sean Callanan336a0002010-07-17 00:43:37 +0000658 log->Printf("Can't evaluate an expression when not in a function");
Chris Lattner24943d22010-06-08 16:52:24 +0000659 return;
660 }
661
Chris Lattner24943d22010-06-08 16:52:24 +0000662 ConstString name_cs(name);
Sean Callanan0fc73582010-07-27 00:55:47 +0000663 SymbolContextList sym_ctxs;
Chris Lattner24943d22010-06-08 16:52:24 +0000664
Sean Callanan0fc73582010-07-27 00:55:47 +0000665 m_sym_ctx->FindFunctionsByName(name_cs, false, sym_ctxs);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000666
Sean Callanan0fc73582010-07-27 00:55:47 +0000667 for (uint32_t index = 0, num_indices = sym_ctxs.GetSize();
668 index < num_indices;
669 ++index)
670 {
671 SymbolContext sym_ctx;
672 sym_ctxs.GetContextAtIndex(index, sym_ctx);
673
674 if (sym_ctx.function)
675 AddOneFunction(context, sym_ctx.function, NULL);
676 else if(sym_ctx.symbol)
677 AddOneFunction(context, NULL, sym_ctx.symbol);
678 }
679
Sean Callanan336a0002010-07-17 00:43:37 +0000680 Variable *var = FindVariableInScope(*m_sym_ctx, name);
681
682 if (var)
683 AddOneVariable(context, var);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000684
685 /* Commented out pending resolution of a loop when the TagType is imported
686 lldb::TypeSP type = m_sym_ctx->FindTypeByName(name_cs);
687
688 if (type.get())
689 AddOneType(context, type.get());
690 */
Sean Callanan336a0002010-07-17 00:43:37 +0000691}
692
693Value *
694ClangExpressionDeclMap::GetVariableValue(ExecutionContext &exe_ctx,
695 Variable *var,
Sean Callananf328c9f2010-07-20 23:31:16 +0000696 clang::ASTContext *parser_ast_context,
697 TypeFromUser *user_type,
698 TypeFromParser *parser_type)
Chris Lattner24943d22010-06-08 16:52:24 +0000699{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000700 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
701
Chris Lattner24943d22010-06-08 16:52:24 +0000702 Type *var_type = var->GetType();
703
704 if (!var_type)
705 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000706 if (log)
707 log->PutCString("Skipped a definition because it has no type");
Sean Callanan336a0002010-07-17 00:43:37 +0000708 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000709 }
710
711 void *var_opaque_type = var_type->GetOpaqueClangQualType();
712
713 if (!var_opaque_type)
714 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000715 if (log)
716 log->PutCString("Skipped a definition because it has no Clang type");
Sean Callanan336a0002010-07-17 00:43:37 +0000717 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000718 }
719
Chris Lattner24943d22010-06-08 16:52:24 +0000720 TypeList *type_list = var_type->GetTypeList();
721
722 if (!type_list)
723 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000724 if (log)
725 log->PutCString("Skipped a definition because the type has no associated type list");
Sean Callanan336a0002010-07-17 00:43:37 +0000726 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000727 }
728
729 clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext();
730
731 if (!exe_ast_ctx)
732 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000733 if (log)
734 log->PutCString("There is no AST context for the current execution context");
Sean Callanan336a0002010-07-17 00:43:37 +0000735 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000736 }
737
Sean Callanan336a0002010-07-17 00:43:37 +0000738 DWARFExpression &var_location_expr = var->LocationExpression();
739
Chris Lattner24943d22010-06-08 16:52:24 +0000740 std::auto_ptr<Value> var_location(new Value);
741
742 Error err;
743
Sean Callanan336a0002010-07-17 00:43:37 +0000744 if (!var_location_expr.Evaluate(&exe_ctx, exe_ast_ctx, NULL, *var_location.get(), &err))
Chris Lattner24943d22010-06-08 16:52:24 +0000745 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000746 if (log)
747 log->Printf("Error evaluating location: %s", err.AsCString());
Sean Callanan336a0002010-07-17 00:43:37 +0000748 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000749 }
750
Sean Callanan810f22d2010-07-16 00:09:46 +0000751 clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext();
752
Sean Callanan336a0002010-07-17 00:43:37 +0000753 void *type_to_use;
754
Sean Callananf328c9f2010-07-20 23:31:16 +0000755 if (parser_ast_context)
756 {
757 type_to_use = ClangASTContext::CopyType(parser_ast_context, var_ast_context, var_opaque_type);
758
759 if (parser_type)
760 *parser_type = TypeFromParser(type_to_use, parser_ast_context);
761 }
Sean Callanan336a0002010-07-17 00:43:37 +0000762 else
763 type_to_use = var_opaque_type;
Chris Lattner24943d22010-06-08 16:52:24 +0000764
765 if (var_location.get()->GetContextType() == Value::eContextTypeInvalid)
Sean Callanan336a0002010-07-17 00:43:37 +0000766 var_location.get()->SetContext(Value::eContextTypeOpaqueClangQualType, type_to_use);
Chris Lattner24943d22010-06-08 16:52:24 +0000767
768 if (var_location.get()->GetValueType() == Value::eValueTypeFileAddress)
769 {
770 SymbolContext var_sc;
771 var->CalculateSymbolContext(&var_sc);
Sean Callanan336a0002010-07-17 00:43:37 +0000772
Chris Lattner24943d22010-06-08 16:52:24 +0000773 if (!var_sc.module_sp)
Sean Callanan336a0002010-07-17 00:43:37 +0000774 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000775
776 ObjectFile *object_file = var_sc.module_sp->GetObjectFile();
777
778 if (!object_file)
Sean Callanan336a0002010-07-17 00:43:37 +0000779 return NULL;
780
Chris Lattner24943d22010-06-08 16:52:24 +0000781 Address so_addr(var_location->GetScalar().ULongLong(), object_file->GetSectionList());
782
783 lldb::addr_t load_addr = so_addr.GetLoadAddress(m_exe_ctx->process);
784
785 var_location->GetScalar() = load_addr;
786 var_location->SetValueType(Value::eValueTypeLoadAddress);
787 }
788
Sean Callananf328c9f2010-07-20 23:31:16 +0000789 if (user_type)
790 *user_type = TypeFromUser(var_opaque_type, var_ast_context);
Sean Callanan336a0002010-07-17 00:43:37 +0000791
792 return var_location.release();
793}
794
795void
796ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
797 Variable* var)
798{
799 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
800
Sean Callananf328c9f2010-07-20 23:31:16 +0000801 TypeFromUser ut;
802 TypeFromParser pt;
Sean Callanan336a0002010-07-17 00:43:37 +0000803
804 Value *var_location = GetVariableValue(*m_exe_ctx,
805 var,
806 context.GetASTContext(),
Sean Callananf328c9f2010-07-20 23:31:16 +0000807 &ut,
808 &pt);
Sean Callanan336a0002010-07-17 00:43:37 +0000809
Greg Clayton1674b122010-07-21 22:12:05 +0000810 NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType());
Chris Lattner24943d22010-06-08 16:52:24 +0000811
812 Tuple tuple;
813
Sean Callanan810f22d2010-07-16 00:09:46 +0000814 tuple.m_decl = var_decl;
Sean Callanan336a0002010-07-17 00:43:37 +0000815 tuple.m_value = var_location;
Sean Callananf328c9f2010-07-20 23:31:16 +0000816 tuple.m_user_type = ut;
817 tuple.m_parser_type = pt;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000818 tuple.m_llvm_value = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000819
820 m_tuples.push_back(tuple);
821
Sean Callanan810f22d2010-07-16 00:09:46 +0000822 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +0000823 log->Printf("Found variable %s, returned (NamedDecl)%p", context.Name.getAsString().c_str(), var_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000824}
825
826void
827ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
Sean Callanan0fc73582010-07-27 00:55:47 +0000828 Function* fun,
829 Symbol* symbol)
Sean Callanan8f0dc342010-06-22 23:46:24 +0000830{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000831 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000832
Sean Callanan0fc73582010-07-27 00:55:47 +0000833 NamedDecl *fun_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000834 std::auto_ptr<Value> fun_location(new Value);
Sean Callanan0fc73582010-07-27 00:55:47 +0000835 const Address *fun_address;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000836
Sean Callanan0fc73582010-07-27 00:55:47 +0000837 // only valid for Functions, not for Symbols
838 void *fun_opaque_type = NULL;
839 clang::ASTContext *fun_ast_context = NULL;
840
841 if (fun)
842 {
843 Type *fun_type = fun->GetType();
844
845 if (!fun_type)
846 {
847 if (log)
848 log->PutCString("Skipped a function because it has no type");
849 return;
850 }
851
852 fun_opaque_type = fun_type->GetOpaqueClangQualType();
853
854 if (!fun_opaque_type)
855 {
856 if (log)
857 log->PutCString("Skipped a function because it has no Clang type");
858 return;
859 }
860
861 fun_address = &fun->GetAddressRange().GetBaseAddress();
862
863 TypeList *type_list = fun_type->GetTypeList();
864 fun_ast_context = type_list->GetClangASTContext().getASTContext();
865 void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), fun_ast_context, fun_opaque_type);
866
867 fun_decl = context.AddFunDecl(copied_type);
868 }
869 else if (symbol)
870 {
871 fun_address = &symbol->GetAddressRangeRef().GetBaseAddress();
872
873 fun_decl = context.AddGenericFunDecl();
874 }
875 else
876 {
877 if (log)
878 log->PutCString("AddOneFunction called with no function and no symbol");
879 return;
880 }
881
882 lldb::addr_t load_addr = fun_address->GetLoadAddress(m_exe_ctx->process);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000883 fun_location->SetValueType(Value::eValueTypeLoadAddress);
884 fun_location->GetScalar() = load_addr;
885
Sean Callanan8f0dc342010-06-22 23:46:24 +0000886 Tuple tuple;
887
Sean Callanan810f22d2010-07-16 00:09:46 +0000888 tuple.m_decl = fun_decl;
889 tuple.m_value = fun_location.release();
Sean Callananf328c9f2010-07-20 23:31:16 +0000890 tuple.m_user_type = TypeFromUser(fun_opaque_type, fun_ast_context);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000891 tuple.m_llvm_value = NULL;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000892
893 m_tuples.push_back(tuple);
894
Sean Callanan810f22d2010-07-16 00:09:46 +0000895 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +0000896 log->Printf("Found function %s, returned (NamedDecl)%p", context.Name.getAsString().c_str(), fun_decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000897}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000898
899void
900ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
901 Type *type)
902{
903 TypeFromUser ut(type->GetOpaqueClangQualType(),
904 type->GetClangAST());
905
906 void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), ut.GetASTContext(), ut.GetOpaqueQualType());
907
908 context.AddTypeDecl(copied_type);
909}