blob: b0f1cf275e0c4f7cd9900fe65d32033db4c55d2f [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 Callanan810f22d2010-07-16 00:09:46 +000031#include "lldb/Target/Process.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032#include "lldb/Target/StackFrame.h"
33#include "lldb/Target/ExecutionContext.h"
34
Chris Lattner24943d22010-06-08 16:52:24 +000035using namespace lldb_private;
36using namespace clang;
37
38ClangExpressionDeclMap::ClangExpressionDeclMap(ExecutionContext *exe_ctx) :
Sean Callanan8bce6652010-07-13 21:41:46 +000039 m_exe_ctx(exe_ctx),
Sean Callanan810f22d2010-07-16 00:09:46 +000040 m_struct_laid_out(false),
41 m_materialized_location(0)
Chris Lattner24943d22010-06-08 16:52:24 +000042{
43 if (exe_ctx && exe_ctx->frame)
44 m_sym_ctx = new SymbolContext(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything));
45 else
46 m_sym_ctx = NULL;
47}
48
49ClangExpressionDeclMap::~ClangExpressionDeclMap()
50{
51 uint32_t num_tuples = m_tuples.size ();
52 uint32_t tuple_index;
53
54 for (tuple_index = 0; tuple_index < num_tuples; ++tuple_index)
55 delete m_tuples[tuple_index].m_value;
56
57 if (m_sym_ctx)
58 delete m_sym_ctx;
59}
60
61bool
62ClangExpressionDeclMap::GetIndexForDecl (uint32_t &index,
63 const clang::Decl *decl)
64{
65 uint32_t num_tuples = m_tuples.size ();
66 uint32_t tuple_index;
67
68 for (tuple_index = 0; tuple_index < num_tuples; ++tuple_index)
69 {
70 if (m_tuples[tuple_index].m_decl == decl)
71 {
72 index = tuple_index;
73 return true;
74 }
75 }
76
77 return false;
78}
79
Sean Callanan8bce6652010-07-13 21:41:46 +000080// Interface for IRForTarget
81
82bool
83ClangExpressionDeclMap::AddValueToStruct (llvm::Value *value,
84 const clang::NamedDecl *decl,
Sean Callanan810f22d2010-07-16 00:09:46 +000085 std::string &name,
86 void *type,
Sean Callanan8bce6652010-07-13 21:41:46 +000087 size_t size,
88 off_t alignment)
89{
90 m_struct_laid_out = false;
91
92 StructMemberIterator iter;
93
94 for (iter = m_members.begin();
95 iter != m_members.end();
96 ++iter)
97 {
98 if (iter->m_decl == decl)
99 return true;
100 }
101
102 StructMember member;
103
104 member.m_value = value;
105 member.m_decl = decl;
Sean Callanan810f22d2010-07-16 00:09:46 +0000106 member.m_name = name;
107 member.m_type = type;
Sean Callanan8bce6652010-07-13 21:41:46 +0000108 member.m_offset = 0;
109 member.m_size = size;
110 member.m_alignment = alignment;
111
112 m_members.push_back(member);
113
114 return true;
115}
116
117bool
118ClangExpressionDeclMap::DoStructLayout ()
119{
120 if (m_struct_laid_out)
121 return true;
122
123 StructMemberIterator iter;
124
125 off_t cursor = 0;
126
127 m_struct_alignment = 0;
128 m_struct_size = 0;
129
130 for (iter = m_members.begin();
131 iter != m_members.end();
132 ++iter)
133 {
134 if (iter == m_members.begin())
135 m_struct_alignment = iter->m_alignment;
136
137 if (cursor % iter->m_alignment)
138 cursor += (iter->m_alignment - (cursor % iter->m_alignment));
139
140 iter->m_offset = cursor;
141 cursor += iter->m_size;
142 }
143
144 m_struct_size = cursor;
145
146 m_struct_laid_out = true;
147 return true;
148}
149
150bool ClangExpressionDeclMap::GetStructInfo (uint32_t &num_elements,
151 size_t &size,
152 off_t &alignment)
153{
154 if (!m_struct_laid_out)
155 return false;
156
157 num_elements = m_members.size();
158 size = m_struct_size;
159 alignment = m_struct_alignment;
160
161 return true;
162}
163
164bool
165ClangExpressionDeclMap::GetStructElement (const clang::NamedDecl *&decl,
166 llvm::Value *&value,
167 off_t &offset,
168 uint32_t index)
169{
170 if (!m_struct_laid_out)
171 return false;
172
173 if (index >= m_members.size())
174 return false;
175
176 decl = m_members[index].m_decl;
177 value = m_members[index].m_value;
178 offset = m_members[index].m_offset;
179
180 return true;
181}
182
Chris Lattner24943d22010-06-08 16:52:24 +0000183// Interface for DwarfExpression
184Value
185*ClangExpressionDeclMap::GetValueForIndex (uint32_t index)
186{
187 if (index >= m_tuples.size ())
188 return NULL;
189
190 return m_tuples[index].m_value;
191}
192
Sean Callanan810f22d2010-07-16 00:09:46 +0000193// Interface for CommandObjectExpression
194lldb::addr_t
195ClangExpressionDeclMap::Materialize (ExecutionContext *exe_ctx, Error &err)
196{
197 if (!m_struct_laid_out)
198 {
199 err.SetErrorString("Structure hasn't been laid out yet");
200 return LLDB_INVALID_ADDRESS;
201 }
202
203 if (m_materialized_location)
204 {
205 exe_ctx->process->DeallocateMemory(m_materialized_location);
206 m_materialized_location = 0;
207 }
208
209 if (!exe_ctx)
210 {
211 err.SetErrorString("Received null execution context");
212 return LLDB_INVALID_ADDRESS;
213 }
214
215 const SymbolContext &sym_ctx(exe_ctx->frame->GetSymbolContext(lldb::eSymbolContextEverything));
216
217 StructMemberIterator iter;
218
219 lldb::addr_t mem = exe_ctx->process->AllocateMemory(m_struct_alignment + m_struct_size,
220 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
221 err);
222
223 if (mem == LLDB_INVALID_ADDRESS)
224 return LLDB_INVALID_ADDRESS;
225
226 m_materialized_location = mem;
227
228 lldb::addr_t aligned_mem = mem;
229
230 if (aligned_mem % m_struct_alignment)
231 {
232 aligned_mem += (m_struct_alignment - (aligned_mem % m_struct_alignment));
233 }
234
235 for (iter = m_members.begin();
236 iter != m_members.end();
237 ++iter)
238 {
239 uint32_t tuple_index;
240
241 if (!GetIndexForDecl(tuple_index, iter->m_decl))
242 continue;
243
244 Tuple &tuple(m_tuples[tuple_index]);
245
246 MaterializeOneVariable(*exe_ctx, sym_ctx, iter->m_name.c_str(), tuple.m_orig_type, tuple.m_ast_context, aligned_mem + iter->m_offset);
247 }
248
249 return aligned_mem;
250}
251
252bool
253ClangExpressionDeclMap::MaterializeOneVariable(ExecutionContext &exe_ctx,
254 const SymbolContext &sym_ctx,
255 const char *name,
256 void *type,
257 clang::ASTContext *ast_context,
258 lldb::addr_t addr)
259{
260 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
261
262 Function *function(m_sym_ctx->function);
263 Block *block(m_sym_ctx->block);
264
265 if (!function || !block)
266 {
267 if (log)
268 log->Printf("function = %p, block = %p", function, block);
269 return false;
270 }
271
272 BlockList& blocks(function->GetBlocks(true));
273
274 lldb::user_id_t current_block_id;
275
276 for (current_block_id = block->GetID();
277 current_block_id != Block::InvalidID;
278 current_block_id = blocks.GetParent(current_block_id))
279 {
280 Block *current_block(blocks.GetBlockByID(current_block_id));
281
282 lldb::VariableListSP var_list = current_block->GetVariableList(false, true);
283
284 if (!var_list)
285 continue;
286
287 lldb::VariableSP var = var_list->FindVariable(ConstString(name));
288
289 if (!var)
290 continue;
291
292 // var->GetType()->GetClangAST() is the program's AST context and holds
293 // var->GetType()->GetOpaqueClangQualType().
294
295 // type is m_type for one of the struct members, which was added by
296 // AddValueToStruct. That type was extracted from the AST context of
297 // the compiler in IRForTarget. The original for the type was copied
298 // out of the program's AST context by AddOneVariable.
299
300 // The key here is: we know when we copied a type, and for what Decl we
301 // did it. So we need for each struct Tuple to keep the type that we
302 // found, and which AST context we found it in. Then we can look up
303 // m_decl in m_tuples.
304
305 if (ast_context == var->GetType()->GetClangAST())
306 {
307 if (!ClangASTContext::AreTypesSame(ast_context, type, var->GetType()->GetOpaqueClangQualType()))
308 continue;
309 }
310 else
311 {
312 if (log)
313 log->PutCString("Skipping a candidate variable because of different AST contexts");
314 continue;
315 }
316
317
318 }
319
320 return true;
321}
322
Chris Lattner24943d22010-06-08 16:52:24 +0000323// Interface for ClangASTSource
324void
325ClangExpressionDeclMap::GetDecls(NameSearchContext &context,
326 const char *name)
327{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000328 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
329
Sean Callanan810f22d2010-07-16 00:09:46 +0000330 if (log)
331 log->Printf("Hunting for a definition for %s", name);
Chris Lattner24943d22010-06-08 16:52:24 +0000332
333 // Back out in all cases where we're not fully initialized
334 if (!m_exe_ctx || !m_exe_ctx->frame || !m_sym_ctx)
335 return;
336
337 Function *function = m_sym_ctx->function;
338 Block *block = m_sym_ctx->block;
339
340 if (!function || !block)
341 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000342 if (log)
343 log->Printf("function = %p, block = %p", function, block);
Chris Lattner24943d22010-06-08 16:52:24 +0000344 return;
345 }
346
347 BlockList& blocks = function->GetBlocks(true);
348
349 lldb::user_id_t current_block_id = block->GetID();
350
351 ConstString name_cs(name);
352
Sean Callanan8f0dc342010-06-22 23:46:24 +0000353 Function *fn = m_sym_ctx->FindFunctionByName(name_cs.GetCString());
354
355 if (fn)
356 {
357 AddOneFunction(context, fn);
358 }
359
Chris Lattner24943d22010-06-08 16:52:24 +0000360 for (current_block_id = block->GetID();
361 current_block_id != Block::InvalidID;
362 current_block_id = blocks.GetParent(current_block_id))
363 {
364 Block *current_block = blocks.GetBlockByID(current_block_id);
365
366 lldb::VariableListSP var_list = current_block->GetVariableList(false, true);
367
368 if (!var_list)
369 continue;
370
371 lldb::VariableSP var = var_list->FindVariable(name_cs);
372
373 if (!var)
374 continue;
375
376 AddOneVariable(context, var.get());
377 return;
378 }
379
380 {
381 CompileUnit *compile_unit = m_sym_ctx->comp_unit;
382
383 if (!compile_unit)
384 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000385 if (log)
386 log->Printf("compile_unit = %p", compile_unit);
Chris Lattner24943d22010-06-08 16:52:24 +0000387 return;
388 }
389
390 lldb::VariableListSP var_list = compile_unit->GetVariableList(true);
391
392 if (!var_list)
393 return;
394
395 lldb::VariableSP var = var_list->FindVariable(name_cs);
396
397 if (!var)
398 return;
399
400 AddOneVariable(context, var.get());
401 return;
402 }
403}
404
405void
406ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
407 Variable* var)
408{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000409 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
410
Chris Lattner24943d22010-06-08 16:52:24 +0000411 Type *var_type = var->GetType();
412
413 if (!var_type)
414 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000415 if (log)
416 log->PutCString("Skipped a definition because it has no type");
Chris Lattner24943d22010-06-08 16:52:24 +0000417 return;
418 }
419
420 void *var_opaque_type = var_type->GetOpaqueClangQualType();
421
422 if (!var_opaque_type)
423 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000424 if (log)
425 log->PutCString("Skipped a definition because it has no Clang type");
Chris Lattner24943d22010-06-08 16:52:24 +0000426 return;
427 }
428
429 DWARFExpression &var_location_expr = var->LocationExpression();
430
431 TypeList *type_list = var_type->GetTypeList();
432
433 if (!type_list)
434 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000435 if (log)
436 log->PutCString("Skipped a definition because the type has no associated type list");
Chris Lattner24943d22010-06-08 16:52:24 +0000437 return;
438 }
439
440 clang::ASTContext *exe_ast_ctx = type_list->GetClangASTContext().getASTContext();
441
442 if (!exe_ast_ctx)
443 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000444 if (log)
445 log->PutCString("There is no AST context for the current execution context");
Chris Lattner24943d22010-06-08 16:52:24 +0000446 return;
447 }
448
449 std::auto_ptr<Value> var_location(new Value);
450
451 Error err;
452
453 if (!var_location_expr.Evaluate(m_exe_ctx, exe_ast_ctx, NULL, *var_location.get(), &err))
454 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000455 if (log)
456 log->Printf("Error evaluating location: %s", err.AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000457 return;
458 }
459
Sean Callanan810f22d2010-07-16 00:09:46 +0000460 clang::ASTContext *var_ast_context = type_list->GetClangASTContext().getASTContext();
461
462 void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), var_ast_context, var_opaque_type);
Chris Lattner24943d22010-06-08 16:52:24 +0000463
464 if (var_location.get()->GetContextType() == Value::eContextTypeInvalid)
465 var_location.get()->SetContext(Value::eContextTypeOpaqueClangQualType, copied_type);
466
467 if (var_location.get()->GetValueType() == Value::eValueTypeFileAddress)
468 {
469 SymbolContext var_sc;
470 var->CalculateSymbolContext(&var_sc);
471
472 if (!var_sc.module_sp)
473 return;
474
475 ObjectFile *object_file = var_sc.module_sp->GetObjectFile();
476
477 if (!object_file)
478 return;
479
480 Address so_addr(var_location->GetScalar().ULongLong(), object_file->GetSectionList());
481
482 lldb::addr_t load_addr = so_addr.GetLoadAddress(m_exe_ctx->process);
483
484 var_location->GetScalar() = load_addr;
485 var_location->SetValueType(Value::eValueTypeLoadAddress);
486 }
487
488 NamedDecl *var_decl = context.AddVarDecl(copied_type);
489
490 Tuple tuple;
491
Sean Callanan810f22d2010-07-16 00:09:46 +0000492 tuple.m_decl = var_decl;
493 tuple.m_value = var_location.release();
494 tuple.m_orig_type = var_opaque_type;
495 tuple.m_ast_context = var_ast_context;
Chris Lattner24943d22010-06-08 16:52:24 +0000496
497 m_tuples.push_back(tuple);
498
Sean Callanan810f22d2010-07-16 00:09:46 +0000499 if (log)
500 log->PutCString("Found variable");
Sean Callanan8f0dc342010-06-22 23:46:24 +0000501}
502
503void
504ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
505 Function* fun)
506{
Sean Callanan6184dfe2010-06-23 00:47:48 +0000507 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
508
Sean Callanan8f0dc342010-06-22 23:46:24 +0000509 Type *fun_type = fun->GetType();
510
511 if (!fun_type)
512 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000513 if (log)
514 log->PutCString("Skipped a function because it has no type");
Sean Callanan8f0dc342010-06-22 23:46:24 +0000515 return;
516 }
517
518 void *fun_opaque_type = fun_type->GetOpaqueClangQualType();
519
520 if (!fun_opaque_type)
521 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000522 if (log)
523 log->PutCString("Skipped a function because it has no Clang type");
Sean Callanan8f0dc342010-06-22 23:46:24 +0000524 return;
525 }
526
527 std::auto_ptr<Value> fun_location(new Value);
528
529 const Address &fun_address = fun->GetAddressRange().GetBaseAddress();
530 lldb::addr_t load_addr = fun_address.GetLoadAddress(m_exe_ctx->process);
531 fun_location->SetValueType(Value::eValueTypeLoadAddress);
532 fun_location->GetScalar() = load_addr;
533
534 TypeList *type_list = fun_type->GetTypeList();
Sean Callanan810f22d2010-07-16 00:09:46 +0000535 clang::ASTContext *fun_ast_context = type_list->GetClangASTContext().getASTContext();
536 void *copied_type = ClangASTContext::CopyType(context.GetASTContext(), fun_ast_context, fun_opaque_type);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000537
538 NamedDecl *fun_decl = context.AddFunDecl(copied_type);
539
540 Tuple tuple;
541
Sean Callanan810f22d2010-07-16 00:09:46 +0000542 tuple.m_decl = fun_decl;
543 tuple.m_value = fun_location.release();
544 tuple.m_orig_type = fun_opaque_type;
545 tuple.m_ast_context = fun_ast_context;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000546
547 m_tuples.push_back(tuple);
548
Sean Callanan810f22d2010-07-16 00:09:46 +0000549 if (log)
550 log->PutCString("Found function");
Chris Lattner24943d22010-06-08 16:52:24 +0000551}