blob: 54bfad5772ae3a0e3046aa769b71d924c38083b2 [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
Chris Lattner24943d22010-06-08 16:52:24 +0000185// Interface for DwarfExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000186lldb_private::Value
Chris Lattner24943d22010-06-08 16:52:24 +0000187*ClangExpressionDeclMap::GetValueForIndex (uint32_t index)
188{
189 if (index >= m_tuples.size ())
190 return NULL;
191
192 return m_tuples[index].m_value;
193}
194
Sean Callanan810f22d2010-07-16 00:09:46 +0000195// Interface for CommandObjectExpression
Sean Callananf328c9f2010-07-20 23:31:16 +0000196
197bool
198ClangExpressionDeclMap::Materialize (ExecutionContext *exe_ctx,
199 lldb::addr_t &struct_address,
200 Error &err)
201{
202 bool result = DoMaterialize(false, exe_ctx, NULL, err);
203
204 if (result)
205 struct_address = m_materialized_location;
206
207 return result;
208}
209
210bool
211ClangExpressionDeclMap::Dematerialize (ExecutionContext *exe_ctx,
212 lldb_private::Value &result_value,
213 Error &err)
214{
215 return DoMaterialize(true, exe_ctx, &result_value, err);
216}
217
218bool
219ClangExpressionDeclMap::DoMaterialize (bool dematerialize,
220 ExecutionContext *exe_ctx,
221 lldb_private::Value *result_value,
222 Error &err)
Sean Callanan810f22d2010-07-16 00:09:46 +0000223{
Sean Callanan336a0002010-07-17 00:43:37 +0000224 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
225
Sean Callanan810f22d2010-07-16 00:09:46 +0000226 if (!m_struct_laid_out)
227 {
228 err.SetErrorString("Structure hasn't been laid out yet");
229 return LLDB_INVALID_ADDRESS;
230 }
231
Sean Callanan810f22d2010-07-16 00:09:46 +0000232 if (!exe_ctx)
233 {
234 err.SetErrorString("Received null execution context");
235 return LLDB_INVALID_ADDRESS;
236 }
237
238 const SymbolContext &sym_ctx(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything));
239
Sean Callananf328c9f2010-07-20 23:31:16 +0000240 if (!dematerialize)
Sean Callanan810f22d2010-07-16 00:09:46 +0000241 {
Sean Callananf328c9f2010-07-20 23:31:16 +0000242 if (m_materialized_location)
243 {
244 exe_ctx->process->DeallocateMemory(m_materialized_location);
245 m_materialized_location = 0;
246 }
247
248 lldb::addr_t mem = exe_ctx->process->AllocateMemory(m_struct_alignment + m_struct_size,
249 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
250 err);
251
252 if (mem == LLDB_INVALID_ADDRESS)
253 return false;
254
255 m_allocated_area = mem;
Sean Callanan810f22d2010-07-16 00:09:46 +0000256 }
257
Sean Callananf328c9f2010-07-20 23:31:16 +0000258 m_materialized_location = m_allocated_area;
259
260 if (m_materialized_location % m_struct_alignment)
261 {
262 m_materialized_location += (m_struct_alignment - (m_materialized_location % m_struct_alignment));
263 }
264
265 StructMemberIterator iter;
266
Sean Callanan810f22d2010-07-16 00:09:46 +0000267 for (iter = m_members.begin();
268 iter != m_members.end();
269 ++iter)
270 {
271 uint32_t tuple_index;
272
Sean Callanan336a0002010-07-17 00:43:37 +0000273 if (!GetIndexForDecl(tuple_index, iter->m_decl))
274 {
275 if (iter->m_name.find("___clang_expr_result") == std::string::npos)
276 {
277 err.SetErrorStringWithFormat("Unexpected variable %s", iter->m_name.c_str());
278 return false;
279 }
280
281 if (log)
282 log->Printf("Found special result variable %s", iter->m_name.c_str());
283
Sean Callananf328c9f2010-07-20 23:31:16 +0000284 if (dematerialize)
285 {
286 clang::ASTContext *context(exe_ctx->target->GetScratchClangASTContext()->getASTContext());
287
288 if (!context)
289 {
290 err.SetErrorString("Couldn't find a scratch AST context to put the result type into");
291 }
292
293 TypeFromUser copied_type(ClangASTContext::CopyType(context,
294 iter->m_parser_type.GetASTContext(),
Greg Clayton1674b122010-07-21 22:12:05 +0000295 iter->m_parser_type.GetOpaqueQualType()),
Sean Callananf328c9f2010-07-20 23:31:16 +0000296 context);
297
Greg Clayton1674b122010-07-21 22:12:05 +0000298 result_value->SetContext(Value::eContextTypeOpaqueClangQualType,
299 copied_type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000300 }
301
Sean Callanan810f22d2010-07-16 00:09:46 +0000302 continue;
Sean Callanan336a0002010-07-17 00:43:37 +0000303 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000304
305 Tuple &tuple(m_tuples[tuple_index]);
306
Sean Callananf328c9f2010-07-20 23:31:16 +0000307 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 +0000308 return false;
Sean Callanan810f22d2010-07-16 00:09:46 +0000309 }
310
Sean Callananf328c9f2010-07-20 23:31:16 +0000311 return true;
312}
313
314bool
315ClangExpressionDeclMap::DoMaterializeOneVariable(bool dematerialize,
316 ExecutionContext &exe_ctx,
317 const SymbolContext &sym_ctx,
318 const char *name,
319 TypeFromUser type,
320 lldb::addr_t addr,
321 Error &err)
322{
323 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
324
325 Variable *var = FindVariableInScope(sym_ctx, name, &type);
326
327 if (!var)
328 {
329 err.SetErrorStringWithFormat("Couldn't find %s with appropriate type", name);
330 return false;
331 }
332
Greg Clayton1674b122010-07-21 22:12:05 +0000333 log->Printf("%s %s with type %p",
334 (dematerialize ? "Dematerializing" : "Materializing"),
335 name,
336 type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000337
338 std::auto_ptr<lldb_private::Value> location_value(GetVariableValue(exe_ctx,
339 var,
340 type.GetASTContext()));
341
342 if (!location_value.get())
343 {
344 err.SetErrorStringWithFormat("Couldn't get value for %s", name);
345 return false;
346 }
347
348 if (location_value->GetValueType() == Value::eValueTypeLoadAddress)
349 {
350 lldb::addr_t value_addr = location_value->GetScalar().ULongLong();
351
Greg Clayton1674b122010-07-21 22:12:05 +0000352 size_t bit_size = ClangASTContext::GetTypeBitSize (type.GetASTContext(),
353 type.GetOpaqueQualType());
Sean Callananf328c9f2010-07-20 23:31:16 +0000354 size_t byte_size = bit_size % 8 ? ((bit_size + 8) / 8) : (bit_size / 8);
355
356 DataBufferHeap data;
357 data.SetByteSize(byte_size);
358
359 lldb::addr_t src_addr;
360 lldb::addr_t dest_addr;
361
362 if (dematerialize)
363 {
364 src_addr = addr;
365 dest_addr = value_addr;
366 }
367 else
368 {
369 src_addr = value_addr;
370 dest_addr = addr;
371 }
372
373 Error error;
374 if (exe_ctx.process->ReadMemory (src_addr, data.GetBytes(), byte_size, error) != byte_size)
375 {
376 err.SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
377 return false;
378 }
379
380 if (exe_ctx.process->WriteMemory (dest_addr, data.GetBytes(), byte_size, error) != byte_size)
381 {
382 err.SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
383 return false;
384 }
385
386 if (log)
387 log->Printf("Copied from 0x%llx to 0x%llx", (uint64_t)src_addr, (uint64_t)addr);
388 }
389 else
390 {
391 StreamString ss;
392
393 location_value->Dump(&ss);
394
395 err.SetErrorStringWithFormat("%s has a value of unhandled type: %s", name, ss.GetString().c_str());
396 }
397
398 return true;
Sean Callanan810f22d2010-07-16 00:09:46 +0000399}
400
Sean Callanan336a0002010-07-17 00:43:37 +0000401Variable*
402ClangExpressionDeclMap::FindVariableInScope(const SymbolContext &sym_ctx,
403 const char *name,
Sean Callananf328c9f2010-07-20 23:31:16 +0000404 TypeFromUser *type)
Sean Callanan810f22d2010-07-16 00:09:46 +0000405{
406 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
407
408 Function *function(m_sym_ctx->function);
409 Block *block(m_sym_ctx->block);
410
411 if (!function || !block)
412 {
413 if (log)
414 log->Printf("function = %p, block = %p", function, block);
Sean Callanan336a0002010-07-17 00:43:37 +0000415 return NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000416 }
417
418 BlockList& blocks(function->GetBlocks(true));
419
Sean Callanan336a0002010-07-17 00:43:37 +0000420 ConstString name_cs(name);
421
Sean Callanan810f22d2010-07-16 00:09:46 +0000422 lldb::user_id_t current_block_id;
423
424 for (current_block_id = block->GetID();
425 current_block_id != Block::InvalidID;
426 current_block_id = blocks.GetParent(current_block_id))
427 {
428 Block *current_block(blocks.GetBlockByID(current_block_id));
429
430 lldb::VariableListSP var_list = current_block->GetVariableList(false, true);
431
432 if (!var_list)
433 continue;
434
Sean Callanan336a0002010-07-17 00:43:37 +0000435 lldb::VariableSP var = var_list->FindVariable(name_cs);
Sean Callanan810f22d2010-07-16 00:09:46 +0000436
437 if (!var)
438 continue;
439
440 // var->GetType()->GetClangAST() is the program's AST context and holds
441 // var->GetType()->GetOpaqueClangQualType().
442
443 // type is m_type for one of the struct members, which was added by
444 // AddValueToStruct. That type was extracted from the AST context of
445 // the compiler in IRForTarget. The original for the type was copied
446 // out of the program's AST context by AddOneVariable.
447
Sean Callanan336a0002010-07-17 00:43:37 +0000448 // So that we can compare these two without having to copy back
449 // something we already had in the original AST context, we maintain
450 // m_orig_type and m_ast_context (which are passed into
451 // MaterializeOneVariable by Materialize) for each variable.
452
453 if (!type)
454 return var.get();
Sean Callanan810f22d2010-07-16 00:09:46 +0000455
Sean Callananf328c9f2010-07-20 23:31:16 +0000456 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan810f22d2010-07-16 00:09:46 +0000457 {
Sean Callananf328c9f2010-07-20 23:31:16 +0000458 if (!ClangASTContext::AreTypesSame(type->GetASTContext(), type, var->GetType()->GetOpaqueClangQualType()))
Sean Callanan810f22d2010-07-16 00:09:46 +0000459 continue;
460 }
461 else
462 {
463 if (log)
464 log->PutCString("Skipping a candidate variable because of different AST contexts");
465 continue;
466 }
467
Sean Callanan336a0002010-07-17 00:43:37 +0000468 return var.get();
469 }
470
471 {
472 CompileUnit *compile_unit = m_sym_ctx->comp_unit;
Sean Callanan810f22d2010-07-16 00:09:46 +0000473
Sean Callanan336a0002010-07-17 00:43:37 +0000474 if (!compile_unit)
475 {
476 if (log)
477 log->Printf("compile_unit = %p", compile_unit);
478 return NULL;
479 }
480
481 lldb::VariableListSP var_list = compile_unit->GetVariableList(true);
482
483 if (!var_list)
484 return NULL;
485
486 lldb::VariableSP var = var_list->FindVariable(name_cs);
487
488 if (!var)
489 return NULL;
490
491 if (!type)
492 return var.get();
493
Sean Callananf328c9f2010-07-20 23:31:16 +0000494 if (type->GetASTContext() == var->GetType()->GetClangAST())
Sean Callanan336a0002010-07-17 00:43:37 +0000495 {
Greg Clayton1674b122010-07-21 22:12:05 +0000496 if (!ClangASTContext::AreTypesSame(type->GetASTContext(),
497 type->GetOpaqueQualType(),
498 var->GetType()->GetOpaqueClangQualType()))
Sean Callanan336a0002010-07-17 00:43:37 +0000499 return NULL;
500 }
501 else
502 {
503 if (log)
504 log->PutCString("Skipping a candidate variable because of different AST contexts");
505 return NULL;
506 }
507
508 return var.get();
509 }
510
511 return NULL;
512}
513
Chris Lattner24943d22010-06-08 16:52:24 +0000514// Interface for ClangASTSource
515void
516ClangExpressionDeclMap::GetDecls(NameSearchContext &context,
517 const char *name)
518{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000519 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
520
Sean Callanan810f22d2010-07-16 00:09:46 +0000521 if (log)
522 log->Printf("Hunting for a definition for %s", name);
Chris Lattner24943d22010-06-08 16:52:24 +0000523
524 // Back out in all cases where we're not fully initialized
525 if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
526 return;
527
528 Function *function = m_sym_ctx->function;
Chris Lattner24943d22010-06-08 16:52:24 +0000529
Sean Callanan336a0002010-07-17 00:43:37 +0000530 if (!function)
Chris Lattner24943d22010-06-08 16:52:24 +0000531 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000532 if (log)
Sean Callanan336a0002010-07-17 00:43:37 +0000533 log->Printf("Can't evaluate an expression when not in a function");
Chris Lattner24943d22010-06-08 16:52:24 +0000534 return;
535 }
536
Chris Lattner24943d22010-06-08 16:52:24 +0000537 ConstString name_cs(name);
538
Sean Callanan8f0dc342010-06-22 23:46:24 +0000539 Function *fn = m_sym_ctx->FindFunctionByName(name_cs.GetCString());
540
541 if (fn)
Sean Callanan8f0dc342010-06-22 23:46:24 +0000542 AddOneFunction(context, fn);
Chris Lattner24943d22010-06-08 16:52:24 +0000543
Sean Callanan336a0002010-07-17 00:43:37 +0000544 Variable *var = FindVariableInScope(*m_sym_ctx, name);
545
546 if (var)
547 AddOneVariable(context, var);
548}
549
550Value *
551ClangExpressionDeclMap::GetVariableValue(ExecutionContext &exe_ctx,
552 Variable *var,
Sean Callananf328c9f2010-07-20 23:31:16 +0000553 clang::ASTContext *parser_ast_context,
554 TypeFromUser *user_type,
555 TypeFromParser *parser_type)
Chris Lattner24943d22010-06-08 16:52:24 +0000556{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000557 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
558
Chris Lattner24943d22010-06-08 16:52:24 +0000559 Type *var_type = var->GetType();
560
561 if (!var_type)
562 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000563 if (log)
564 log->PutCString("Skipped a definition because it has no type");
Sean Callanan336a0002010-07-17 00:43:37 +0000565 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000566 }
567
568 void *var_opaque_type = var_type->GetOpaqueClangQualType();
569
570 if (!var_opaque_type)
571 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000572 if (log)
573 log->PutCString("Skipped a definition because it has no Clang type");
Sean Callanan336a0002010-07-17 00:43:37 +0000574 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000575 }
576
Chris Lattner24943d22010-06-08 16:52:24 +0000577 TypeList *type_list = var_type->GetTypeList();
578
579 if (!type_list)
580 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000581 if (log)
582 log->PutCString("Skipped a definition because the type has no associated type list");
Sean Callanan336a0002010-07-17 00:43:37 +0000583 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000584 }
585
586 clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext();
587
588 if (!exe_ast_ctx)
589 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000590 if (log)
591 log->PutCString("There is no AST context for the current execution context");
Sean Callanan336a0002010-07-17 00:43:37 +0000592 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000593 }
594
Sean Callanan336a0002010-07-17 00:43:37 +0000595 DWARFExpression &var_location_expr = var->LocationExpression();
596
Chris Lattner24943d22010-06-08 16:52:24 +0000597 std::auto_ptr<Value> var_location(new Value);
598
599 Error err;
600
Sean Callanan336a0002010-07-17 00:43:37 +0000601 if (!var_location_expr.Evaluate(&exe_ctx, exe_ast_ctx, NULL, *var_location.get(), &err))
Chris Lattner24943d22010-06-08 16:52:24 +0000602 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000603 if (log)
604 log->Printf("Error evaluating location: %s", err.AsCString());
Sean Callanan336a0002010-07-17 00:43:37 +0000605 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000606 }
607
Sean Callanan810f22d2010-07-16 00:09:46 +0000608 clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext();
609
Sean Callanan336a0002010-07-17 00:43:37 +0000610 void *type_to_use;
611
Sean Callananf328c9f2010-07-20 23:31:16 +0000612 if (parser_ast_context)
613 {
614 type_to_use = ClangASTContext::CopyType(parser_ast_context, var_ast_context, var_opaque_type);
615
616 if (parser_type)
617 *parser_type = TypeFromParser(type_to_use, parser_ast_context);
618 }
Sean Callanan336a0002010-07-17 00:43:37 +0000619 else
620 type_to_use = var_opaque_type;
Chris Lattner24943d22010-06-08 16:52:24 +0000621
622 if (var_location.get()->GetContextType() == Value::eContextTypeInvalid)
Sean Callanan336a0002010-07-17 00:43:37 +0000623 var_location.get()->SetContext(Value::eContextTypeOpaqueClangQualType, type_to_use);
Chris Lattner24943d22010-06-08 16:52:24 +0000624
625 if (var_location.get()->GetValueType() == Value::eValueTypeFileAddress)
626 {
627 SymbolContext var_sc;
628 var->CalculateSymbolContext(&var_sc);
Sean Callanan336a0002010-07-17 00:43:37 +0000629
Chris Lattner24943d22010-06-08 16:52:24 +0000630 if (!var_sc.module_sp)
Sean Callanan336a0002010-07-17 00:43:37 +0000631 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000632
633 ObjectFile *object_file = var_sc.module_sp->GetObjectFile();
634
635 if (!object_file)
Sean Callanan336a0002010-07-17 00:43:37 +0000636 return NULL;
637
Chris Lattner24943d22010-06-08 16:52:24 +0000638 Address so_addr(var_location->GetScalar().ULongLong(), object_file->GetSectionList());
639
640 lldb::addr_t load_addr = so_addr.GetLoadAddress(m_exe_ctx->process);
641
642 var_location->GetScalar() = load_addr;
643 var_location->SetValueType(Value::eValueTypeLoadAddress);
644 }
645
Sean Callananf328c9f2010-07-20 23:31:16 +0000646 if (user_type)
647 *user_type = TypeFromUser(var_opaque_type, var_ast_context);
Sean Callanan336a0002010-07-17 00:43:37 +0000648
649 return var_location.release();
650}
651
652void
653ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
654 Variable* var)
655{
656 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
657
Sean Callananf328c9f2010-07-20 23:31:16 +0000658 TypeFromUser ut;
659 TypeFromParser pt;
Sean Callanan336a0002010-07-17 00:43:37 +0000660
661 Value *var_location = GetVariableValue(*m_exe_ctx,
662 var,
663 context.GetASTContext(),
Sean Callananf328c9f2010-07-20 23:31:16 +0000664 &ut,
665 &pt);
Sean Callanan336a0002010-07-17 00:43:37 +0000666
Greg Clayton1674b122010-07-21 22:12:05 +0000667 NamedDecl *var_decl = context.AddVarDecl(pt.GetOpaqueQualType());
Chris Lattner24943d22010-06-08 16:52:24 +0000668
669 Tuple tuple;
670
Sean Callanan810f22d2010-07-16 00:09:46 +0000671 tuple.m_decl = var_decl;
Sean Callanan336a0002010-07-17 00:43:37 +0000672 tuple.m_value = var_location;
Sean Callananf328c9f2010-07-20 23:31:16 +0000673 tuple.m_user_type = ut;
674 tuple.m_parser_type = pt;
Chris Lattner24943d22010-06-08 16:52:24 +0000675
676 m_tuples.push_back(tuple);
677
Sean Callanan810f22d2010-07-16 00:09:46 +0000678 if (log)
679 log->PutCString("Found variable");
Sean Callanan8f0dc342010-06-22 23:46:24 +0000680}
681
682void
683ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
684 Function* fun)
685{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000686 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
687
Sean Callanan8f0dc342010-06-22 23:46:24 +0000688 Type *fun_type = fun->GetType();
689
690 if (!fun_type)
691 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000692 if (log)
693 log->PutCString("Skipped a function because it has no type");
Sean Callanan8f0dc342010-06-22 23:46:24 +0000694 return;
695 }
696
697 void *fun_opaque_type = fun_type->GetOpaqueClangQualType();
698
699 if (!fun_opaque_type)
700 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000701 if (log)
702 log->PutCString("Skipped a function because it has no Clang type");
Sean Callanan8f0dc342010-06-22 23:46:24 +0000703 return;
704 }
705
706 std::auto_ptr<Value> fun_location(new Value);
707
708 const Address &fun_address = fun->GetAddressRange().GetBaseAddress();
709 lldb::addr_t load_addr = fun_address.GetLoadAddress(m_exe_ctx->process);
710 fun_location->SetValueType(Value::eValueTypeLoadAddress);
711 fun_location->GetScalar() = load_addr;
712
713 TypeList *type_list = fun_type->GetTypeList();
Sean Callanan810f22d2010-07-16 00:09:46 +0000714 clang::ASTContext *fun_ast_context = type_list->GetClangASTContext().getASTContext();
715 void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), fun_ast_context, fun_opaque_type);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000716
717 NamedDecl *fun_decl = context.AddFunDecl(copied_type);
718
719 Tuple tuple;
720
Sean Callanan810f22d2010-07-16 00:09:46 +0000721 tuple.m_decl = fun_decl;
722 tuple.m_value = fun_location.release();
Sean Callananf328c9f2010-07-20 23:31:16 +0000723 tuple.m_user_type = TypeFromUser(fun_opaque_type, fun_ast_context);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000724
725 m_tuples.push_back(tuple);
726
Sean Callanan810f22d2010-07-16 00:09:46 +0000727 if (log)
728 log->PutCString("Found function");
Chris Lattner24943d22010-06-08 16:52:24 +0000729}