blob: 5e782773757759ccfd56b9d3eba95613e110cc6f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Variable.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/Symbol/Variable.h"
11
Greg Clayton1f746072012-08-29 21:13:06 +000012#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Core/Stream.h"
Greg Clayton83c5cd92010-11-14 22:13:40 +000014#include "lldb/Core/RegularExpression.h"
Greg Clayton884fb692011-07-08 21:46:14 +000015#include "lldb/Core/ValueObject.h"
16#include "lldb/Core/ValueObjectVariable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Symbol/Block.h"
Greg Claytonddaf6a72015-07-08 22:32:23 +000018#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Symbol/Function.h"
20#include "lldb/Symbol/SymbolContext.h"
21#include "lldb/Symbol/Type.h"
Greg Clayton884fb692011-07-08 21:46:14 +000022#include "lldb/Symbol/VariableList.h"
Greg Claytonafacd142011-09-02 01:15:17 +000023#include "lldb/Target/ABI.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000024#include "lldb/Target/Process.h"
Greg Clayton9da7bd02010-08-24 21:05:24 +000025#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000026#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Target/Thread.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000028#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
33//----------------------------------------------------------------------
34// Variable constructor
35//----------------------------------------------------------------------
Greg Clayton83c5cd92010-11-14 22:13:40 +000036Variable::Variable
37(
38 lldb::user_id_t uid,
39 const char *name,
Siva Chandra0783ab92015-03-24 18:32:27 +000040 const char *mangled, // The mangled or fully qualified name of the variable.
Greg Claytond1767f02011-12-08 02:13:16 +000041 const lldb::SymbolFileTypeSP &symfile_type_sp,
Greg Clayton83c5cd92010-11-14 22:13:40 +000042 ValueType scope,
43 SymbolContextScope *context,
44 Declaration* decl_ptr,
45 const DWARFExpression& location,
46 bool external,
47 bool artificial
48) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049 UserID(uid),
50 m_name(name),
Siva Chandra0783ab92015-03-24 18:32:27 +000051 m_mangled (ConstString(mangled)),
Greg Claytond1767f02011-12-08 02:13:16 +000052 m_symfile_type_sp(symfile_type_sp),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053 m_scope(scope),
Greg Clayton59e8fc1c2010-08-30 18:11:35 +000054 m_owner_scope(context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055 m_declaration(decl_ptr),
56 m_location(location),
57 m_external(external),
58 m_artificial(artificial)
59{
60}
61
62//----------------------------------------------------------------------
63// Destructor
64//----------------------------------------------------------------------
65Variable::~Variable()
66{
67}
68
Greg Claytonddaf6a72015-07-08 22:32:23 +000069lldb::LanguageType
70Variable::GetLanguage () const
71{
72 SymbolContext variable_sc;
73 m_owner_scope->CalculateSymbolContext(&variable_sc);
74 if (variable_sc.comp_unit)
75 return variable_sc.comp_unit->GetLanguage();
76 return lldb::eLanguageTypeUnknown;
77}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Greg Claytonddaf6a72015-07-08 22:32:23 +000079
80
81ConstString
Greg Clayton83c5cd92010-11-14 22:13:40 +000082Variable::GetName() const
83{
Greg Claytonddaf6a72015-07-08 22:32:23 +000084 if (m_mangled)
85 {
86 ConstString name = m_mangled.GetName(GetLanguage());
87 if (name)
88 return name;
89 }
Greg Clayton83c5cd92010-11-14 22:13:40 +000090 return m_name;
91}
92
93bool
Greg Claytonddaf6a72015-07-08 22:32:23 +000094Variable::NameMatches (const ConstString &name) const
95{
96 if (m_name == name)
97 return true;
98 SymbolContext variable_sc;
99 m_owner_scope->CalculateSymbolContext(&variable_sc);
100
101 LanguageType language = eLanguageTypeUnknown;
102 if (variable_sc.comp_unit)
103 language = variable_sc.comp_unit->GetLanguage();
104 return m_mangled.NameMatches (name, language);
105}
106bool
Greg Clayton83c5cd92010-11-14 22:13:40 +0000107Variable::NameMatches (const RegularExpression& regex) const
108{
109 if (regex.Execute (m_name.AsCString()))
110 return true;
Greg Claytonddaf6a72015-07-08 22:32:23 +0000111 if (m_mangled)
112 return m_mangled.NameMatches (regex, GetLanguage());
113 return false;
Greg Clayton83c5cd92010-11-14 22:13:40 +0000114}
115
Greg Claytond1767f02011-12-08 02:13:16 +0000116Type *
117Variable::GetType()
118{
119 if (m_symfile_type_sp)
120 return m_symfile_type_sp->GetType();
Ed Masted4612ad2014-04-20 13:17:36 +0000121 return nullptr;
Greg Claytond1767f02011-12-08 02:13:16 +0000122}
123
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124void
125Variable::Dump(Stream *s, bool show_context) const
126{
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000127 s->Printf("%p: ", static_cast<const void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128 s->Indent();
129 *s << "Variable" << (const UserID&)*this;
130
131 if (m_name)
132 *s << ", name = \"" << m_name << "\"";
133
Greg Claytond1767f02011-12-08 02:13:16 +0000134 if (m_symfile_type_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 {
Greg Claytond1767f02011-12-08 02:13:16 +0000136 Type *type = m_symfile_type_sp->GetType();
137 if (type)
138 {
139 *s << ", type = {" << type->GetID() << "} " << (void*)type << " (";
140 type->DumpTypeName(s);
141 s->PutChar(')');
142 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 }
144
145 if (m_scope != eValueTypeInvalid)
146 {
147 s->PutCString(", scope = ");
148 switch (m_scope)
149 {
150 case eValueTypeVariableGlobal: s->PutCString(m_external ? "global" : "static"); break;
151 case eValueTypeVariableArgument: s->PutCString("parameter"); break;
152 case eValueTypeVariableLocal: s->PutCString("local"); break;
153 default: *s << "??? (" << m_scope << ')';
154 }
155 }
156
Ed Masted4612ad2014-04-20 13:17:36 +0000157 if (show_context && m_owner_scope != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158 {
159 s->PutCString(", context = ( ");
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000160 m_owner_scope->DumpSymbolContext(s);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 s->PutCString(" )");
162 }
163
Greg Clayton6dbd3982010-09-15 05:51:24 +0000164 bool show_fullpaths = false;
165 m_declaration.Dump(s, show_fullpaths);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166
167 if (m_location.IsValid())
168 {
169 s->PutCString(", location = ");
Greg Clayton016a95e2010-09-14 02:20:48 +0000170 lldb::addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
171 if (m_location.IsLocationList())
172 {
173 SymbolContext variable_sc;
174 m_owner_scope->CalculateSymbolContext(&variable_sc);
175 if (variable_sc.function)
176 loclist_base_addr = variable_sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
177 }
Ed Masted4612ad2014-04-20 13:17:36 +0000178 ABI *abi = nullptr;
Greg Claytonafacd142011-09-02 01:15:17 +0000179 if (m_owner_scope)
180 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000181 ModuleSP module_sp (m_owner_scope->CalculateSymbolContextModule());
182 if (module_sp)
183 abi = ABI::FindPlugin (module_sp->GetArchitecture()).get();
Greg Claytonafacd142011-09-02 01:15:17 +0000184 }
185 m_location.GetDescription(s, lldb::eDescriptionLevelBrief, loclist_base_addr, abi);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 }
187
188 if (m_external)
189 s->PutCString(", external");
190
191 if (m_artificial)
192 s->PutCString(", artificial");
193
194 s->EOL();
195}
196
Greg Clayton45ba8542011-07-10 19:21:23 +0000197bool
198Variable::DumpDeclaration (Stream *s, bool show_fullpaths, bool show_module)
199{
200 bool dumped_declaration_info = false;
201 if (m_owner_scope)
202 {
203 SymbolContext sc;
204 m_owner_scope->CalculateSymbolContext(&sc);
Ed Masted4612ad2014-04-20 13:17:36 +0000205 sc.block = nullptr;
Greg Clayton45ba8542011-07-10 19:21:23 +0000206 sc.line_entry.Clear();
207 bool show_inlined_frames = false;
Jason Molendaaff1b352014-10-10 23:07:36 +0000208 const bool show_function_arguments = true;
Jason Molendac980fa92015-02-13 23:24:21 +0000209 const bool show_function_name = true;
Greg Clayton45ba8542011-07-10 19:21:23 +0000210
211 dumped_declaration_info = sc.DumpStopContext (s,
Ed Masted4612ad2014-04-20 13:17:36 +0000212 nullptr,
Greg Clayton45ba8542011-07-10 19:21:23 +0000213 Address(),
214 show_fullpaths,
215 show_module,
Jason Molendaaff1b352014-10-10 23:07:36 +0000216 show_inlined_frames,
Jason Molendac980fa92015-02-13 23:24:21 +0000217 show_function_arguments,
218 show_function_name);
Greg Clayton45ba8542011-07-10 19:21:23 +0000219
220 if (sc.function)
221 s->PutChar(':');
222 }
223 if (m_declaration.DumpStopContext (s, false))
224 dumped_declaration_info = true;
225 return dumped_declaration_info;
226}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227
228size_t
229Variable::MemorySize() const
230{
231 return sizeof(Variable);
232}
233
234
235void
236Variable::CalculateSymbolContext (SymbolContext *sc)
237{
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000238 if (m_owner_scope)
Greg Clayton2501e5e2015-01-15 02:59:20 +0000239 {
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000240 m_owner_scope->CalculateSymbolContext(sc);
Greg Clayton2501e5e2015-01-15 02:59:20 +0000241 sc->variable = this;
242 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 else
Greg Clayton72310352013-02-23 04:12:47 +0000244 sc->Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245}
246
Greg Clayton007d5be2011-05-30 00:49:24 +0000247bool
Jason Molendab57e4a12013-11-04 09:33:30 +0000248Variable::LocationIsValidForFrame (StackFrame *frame)
Greg Clayton007d5be2011-05-30 00:49:24 +0000249{
250 // Is the variable is described by a single location?
251 if (!m_location.IsLocationList())
252 {
253 // Yes it is, the location is valid.
254 return true;
255 }
256
257 if (frame)
258 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000259 Function *function = frame->GetSymbolContext(eSymbolContextFunction).function;
260 if (function)
261 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000262 TargetSP target_sp (frame->CalculateTarget());
263
264 addr_t loclist_base_load_addr = function->GetAddressRange().GetBaseAddress().GetLoadAddress (target_sp.get());
Greg Clayton007d5be2011-05-30 00:49:24 +0000265 if (loclist_base_load_addr == LLDB_INVALID_ADDRESS)
266 return false;
267 // It is a location list. We just need to tell if the location
268 // list contains the current address when converted to a load
269 // address
270 return m_location.LocationListContainsAddress (loclist_base_load_addr,
Greg Claytond9e416c2012-02-18 05:35:26 +0000271 frame->GetFrameCodeAddress().GetLoadAddress (target_sp.get()));
Greg Clayton007d5be2011-05-30 00:49:24 +0000272 }
273 }
274 return false;
275}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276
277bool
Greg Claytonc749eb82011-07-11 05:12:02 +0000278Variable::LocationIsValidForAddress (const Address &address)
279{
280 // Be sure to resolve the address to section offset prior to
281 // calling this function.
282 if (address.IsSectionOffset())
283 {
284 SymbolContext sc;
285 CalculateSymbolContext(&sc);
Greg Claytone72dfb32012-02-24 01:59:29 +0000286 if (sc.module_sp == address.GetModule())
Greg Claytonc749eb82011-07-11 05:12:02 +0000287 {
288 // Is the variable is described by a single location?
289 if (!m_location.IsLocationList())
290 {
291 // Yes it is, the location is valid.
292 return true;
293 }
294
295 if (sc.function)
296 {
297 addr_t loclist_base_file_addr = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
298 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
299 return false;
300 // It is a location list. We just need to tell if the location
301 // list contains the current address when converted to a load
302 // address
303 return m_location.LocationListContainsAddress (loclist_base_file_addr,
304 address.GetFileAddress());
305 }
306 }
307 }
308 return false;
309}
310
311bool
Jason Molendab57e4a12013-11-04 09:33:30 +0000312Variable::IsInScope (StackFrame *frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313{
314 switch (m_scope)
315 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000316 case eValueTypeRegister:
317 case eValueTypeRegisterSet:
Ed Masted4612ad2014-04-20 13:17:36 +0000318 return frame != nullptr;
Greg Clayton007d5be2011-05-30 00:49:24 +0000319
320 case eValueTypeConstResult:
Greg Clayton007d5be2011-05-30 00:49:24 +0000321 case eValueTypeVariableGlobal:
322 case eValueTypeVariableStatic:
Greg Claytondaf515f2011-07-09 20:12:33 +0000323 return true;
324
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 case eValueTypeVariableArgument:
326 case eValueTypeVariableLocal:
Greg Clayton007d5be2011-05-30 00:49:24 +0000327 if (frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 {
329 // We don't have a location list, we just need to see if the block
330 // that this variable was defined in is currently
Greg Clayton6f00abd2010-09-14 03:16:58 +0000331 Block *deepest_frame_block = frame->GetSymbolContext(eSymbolContextBlock).block;
332 if (deepest_frame_block)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 {
334 SymbolContext variable_sc;
335 CalculateSymbolContext (&variable_sc);
Greg Clayton007d5be2011-05-30 00:49:24 +0000336 // Check for static or global variable defined at the compile unit
337 // level that wasn't defined in a block
Ed Masted4612ad2014-04-20 13:17:36 +0000338 if (variable_sc.block == nullptr)
Greg Clayton007d5be2011-05-30 00:49:24 +0000339 return true;
340
Greg Clayton6f00abd2010-09-14 03:16:58 +0000341 if (variable_sc.block == deepest_frame_block)
Greg Clayton016a95e2010-09-14 02:20:48 +0000342 return true;
Greg Clayton6f00abd2010-09-14 03:16:58 +0000343 return variable_sc.block->Contains (deepest_frame_block);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 }
345 }
346 break;
347
348 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 break;
350 }
351 return false;
352}
353
Greg Clayton884fb692011-07-08 21:46:14 +0000354Error
355Variable::GetValuesForVariableExpressionPath (const char *variable_expr_path,
356 ExecutionContextScope *scope,
357 GetVariableCallback callback,
358 void *baton,
359 VariableList &variable_list,
360 ValueObjectList &valobj_list)
361{
362 Error error;
363 if (variable_expr_path && callback)
364 {
365 switch (variable_expr_path[0])
366 {
367 case '*':
368 {
369 error = Variable::GetValuesForVariableExpressionPath (variable_expr_path + 1,
370 scope,
371 callback,
372 baton,
373 variable_list,
374 valobj_list);
375 if (error.Success())
376 {
377 for (uint32_t i=0; i<valobj_list.GetSize(); )
378 {
379 Error tmp_error;
380 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error));
381 if (tmp_error.Fail())
382 {
383 variable_list.RemoveVariableAtIndex (i);
384 valobj_list.RemoveValueObjectAtIndex (i);
385 }
386 else
387 {
388 valobj_list.SetValueObjectAtIndex (i, valobj_sp);
389 ++i;
390 }
391 }
392 }
393 else
394 {
395 error.SetErrorString ("unknown error");
396 }
397 return error;
398 }
399 break;
400
401 case '&':
402 {
403 error = Variable::GetValuesForVariableExpressionPath (variable_expr_path + 1,
404 scope,
405 callback,
406 baton,
407 variable_list,
408 valobj_list);
409 if (error.Success())
410 {
411 for (uint32_t i=0; i<valobj_list.GetSize(); )
412 {
413 Error tmp_error;
414 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error));
415 if (tmp_error.Fail())
416 {
417 variable_list.RemoveVariableAtIndex (i);
418 valobj_list.RemoveValueObjectAtIndex (i);
419 }
420 else
421 {
422 valobj_list.SetValueObjectAtIndex (i, valobj_sp);
423 ++i;
424 }
425 }
426 }
427 else
428 {
429 error.SetErrorString ("unknown error");
430 }
431 return error;
432 }
433 break;
434
435 default:
436 {
Greg Claytonbc43cab2013-04-03 21:37:16 +0000437 static RegularExpression g_regex ("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)");
438 RegularExpression::Match regex_match(1);
439 if (g_regex.Execute(variable_expr_path, &regex_match))
Greg Clayton884fb692011-07-08 21:46:14 +0000440 {
441 std::string variable_name;
Greg Claytonbc43cab2013-04-03 21:37:16 +0000442 if (regex_match.GetMatchAtIndex(variable_expr_path, 1, variable_name))
Greg Clayton884fb692011-07-08 21:46:14 +0000443 {
444 variable_list.Clear();
445 if (callback (baton, variable_name.c_str(), variable_list))
446 {
447 uint32_t i=0;
448 while (i < variable_list.GetSize())
449 {
450 VariableSP var_sp (variable_list.GetVariableAtIndex (i));
451 ValueObjectSP valobj_sp;
452 if (var_sp)
453 {
454 ValueObjectSP variable_valobj_sp(ValueObjectVariable::Create (scope, var_sp));
455 if (variable_valobj_sp)
456 {
Greg Clayton958d4eb2013-05-20 16:50:51 +0000457 const char *variable_sub_expr_path = variable_expr_path + variable_name.size();
458 if (*variable_sub_expr_path)
Greg Clayton884fb692011-07-08 21:46:14 +0000459 {
Ed Masted4612ad2014-04-20 13:17:36 +0000460 const char* first_unparsed = nullptr;
Greg Clayton884fb692011-07-08 21:46:14 +0000461 ValueObject::ExpressionPathScanEndReason reason_to_stop;
462 ValueObject::ExpressionPathEndResultType final_value_type;
463 ValueObject::GetValueForExpressionPathOptions options;
464 ValueObject::ExpressionPathAftermath final_task_on_target;
465
Greg Clayton958d4eb2013-05-20 16:50:51 +0000466 valobj_sp = variable_valobj_sp->GetValueForExpressionPath (variable_sub_expr_path,
Greg Clayton884fb692011-07-08 21:46:14 +0000467 &first_unparsed,
468 &reason_to_stop,
469 &final_value_type,
470 options,
471 &final_task_on_target);
472 if (!valobj_sp)
473 {
474 error.SetErrorStringWithFormat ("invalid expression path '%s' for variable '%s'",
Greg Clayton958d4eb2013-05-20 16:50:51 +0000475 variable_sub_expr_path,
Greg Clayton884fb692011-07-08 21:46:14 +0000476 var_sp->GetName().GetCString());
477 }
478 }
479 else
480 {
481 // Just the name of a variable with no extras
482 valobj_sp = variable_valobj_sp;
483 }
484 }
485 }
486
487 if (!var_sp || !valobj_sp)
488 {
489 variable_list.RemoveVariableAtIndex (i);
490 }
491 else
492 {
493 valobj_list.Append(valobj_sp);
494 ++i;
495 }
496 }
497
498 if (variable_list.GetSize() > 0)
499 {
500 error.Clear();
501 return error;
502 }
503 }
504 }
505 }
Greg Clayton885b4b72013-05-20 16:52:10 +0000506 error.SetErrorStringWithFormat ("unable to extract a variable name from '%s'", variable_expr_path);
Greg Clayton884fb692011-07-08 21:46:14 +0000507 }
508 break;
509 }
510 }
511 error.SetErrorString ("unknown error");
512 return error;
513}
514
Greg Claytonc749eb82011-07-11 05:12:02 +0000515bool
516Variable::DumpLocationForAddress (Stream *s, const Address &address)
517{
518 // Be sure to resolve the address to section offset prior to
519 // calling this function.
520 if (address.IsSectionOffset())
521 {
522 SymbolContext sc;
523 CalculateSymbolContext(&sc);
Greg Claytone72dfb32012-02-24 01:59:29 +0000524 if (sc.module_sp == address.GetModule())
Greg Claytonc749eb82011-07-11 05:12:02 +0000525 {
Ed Masted4612ad2014-04-20 13:17:36 +0000526 ABI *abi = nullptr;
Greg Claytonafacd142011-09-02 01:15:17 +0000527 if (m_owner_scope)
528 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000529 ModuleSP module_sp (m_owner_scope->CalculateSymbolContextModule());
530 if (module_sp)
531 abi = ABI::FindPlugin (module_sp->GetArchitecture()).get();
Greg Claytonafacd142011-09-02 01:15:17 +0000532 }
533
Greg Claytonc749eb82011-07-11 05:12:02 +0000534 const addr_t file_addr = address.GetFileAddress();
535 if (sc.function)
536 {
537 if (sc.function->GetAddressRange().ContainsFileAddress(address))
538 {
539 addr_t loclist_base_file_addr = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
540 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
541 return false;
542 return m_location.DumpLocationForAddress (s,
543 eDescriptionLevelBrief,
544 loclist_base_file_addr,
Greg Claytonafacd142011-09-02 01:15:17 +0000545 file_addr,
546 abi);
Greg Claytonc749eb82011-07-11 05:12:02 +0000547 }
548 }
549 return m_location.DumpLocationForAddress (s,
550 eDescriptionLevelBrief,
551 LLDB_INVALID_ADDRESS,
Greg Claytonafacd142011-09-02 01:15:17 +0000552 file_addr,
553 abi);
Greg Claytonc749eb82011-07-11 05:12:02 +0000554 }
555 }
556 return false;
Greg Claytonf21fead2013-05-14 23:43:18 +0000557}
Greg Claytonc749eb82011-07-11 05:12:02 +0000558
Greg Claytonf21fead2013-05-14 23:43:18 +0000559
560static void
Jason Molendab57e4a12013-11-04 09:33:30 +0000561PrivateAutoComplete (StackFrame *frame,
Greg Claytonf21fead2013-05-14 23:43:18 +0000562 const std::string &partial_path,
563 const std::string &prefix_path, // Anything that has been resolved already will be in here
564 const ClangASTType& clang_type,
565 StringList &matches,
566 bool &word_complete);
567
568static void
Jason Molendab57e4a12013-11-04 09:33:30 +0000569PrivateAutoCompleteMembers (StackFrame *frame,
Greg Claytonf21fead2013-05-14 23:43:18 +0000570 const std::string &partial_member_name,
571 const std::string &partial_path,
572 const std::string &prefix_path, // Anything that has been resolved already will be in here
573 const ClangASTType& clang_type,
574 StringList &matches,
575 bool &word_complete);
576
577static void
Jason Molendab57e4a12013-11-04 09:33:30 +0000578PrivateAutoCompleteMembers (StackFrame *frame,
Greg Claytonf21fead2013-05-14 23:43:18 +0000579 const std::string &partial_member_name,
580 const std::string &partial_path,
581 const std::string &prefix_path, // Anything that has been resolved already will be in here
582 const ClangASTType& clang_type,
583 StringList &matches,
584 bool &word_complete)
585{
586
587 // We are in a type parsing child members
Pavel Labathc7c30eb2015-06-08 23:38:06 +0000588 const uint32_t num_bases = clang_type.GetNumDirectBaseClasses();
Greg Claytonf21fead2013-05-14 23:43:18 +0000589
590 if (num_bases > 0)
591 {
592 for (uint32_t i = 0; i < num_bases; ++i)
593 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +0000594 ClangASTType base_class_type (clang_type.GetDirectBaseClassAtIndex (i, nullptr));
Greg Claytonf21fead2013-05-14 23:43:18 +0000595
596 PrivateAutoCompleteMembers (frame,
597 partial_member_name,
598 partial_path,
599 prefix_path,
600 base_class_type.GetCanonicalType(),
601 matches,
602 word_complete);
603 }
604 }
605
Pavel Labathc7c30eb2015-06-08 23:38:06 +0000606 const uint32_t num_vbases = clang_type.GetNumVirtualBaseClasses();
Greg Claytonf21fead2013-05-14 23:43:18 +0000607
608 if (num_vbases > 0)
609 {
610 for (uint32_t i = 0; i < num_vbases; ++i)
611 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +0000612 ClangASTType vbase_class_type (clang_type.GetVirtualBaseClassAtIndex(i,nullptr));
Greg Claytonf21fead2013-05-14 23:43:18 +0000613
614 PrivateAutoCompleteMembers (frame,
615 partial_member_name,
616 partial_path,
617 prefix_path,
618 vbase_class_type.GetCanonicalType(),
619 matches,
620 word_complete);
621 }
622 }
623
624 // We are in a type parsing child members
Greg Clayton57ee3062013-07-11 22:46:58 +0000625 const uint32_t num_fields = clang_type.GetNumFields();
Greg Claytonf21fead2013-05-14 23:43:18 +0000626
627 if (num_fields > 0)
628 {
629 for (uint32_t i = 0; i < num_fields; ++i)
630 {
631 std::string member_name;
632
Ed Masted4612ad2014-04-20 13:17:36 +0000633 ClangASTType member_clang_type = clang_type.GetFieldAtIndex (i, member_name, nullptr, nullptr, nullptr);
Greg Claytonf21fead2013-05-14 23:43:18 +0000634
635 if (partial_member_name.empty() ||
636 member_name.find(partial_member_name) == 0)
637 {
638 if (member_name == partial_member_name)
639 {
Greg Claytonf21fead2013-05-14 23:43:18 +0000640 PrivateAutoComplete (frame,
641 partial_path,
642 prefix_path + member_name, // Anything that has been resolved already will be in here
643 member_clang_type.GetCanonicalType(),
644 matches,
645 word_complete);
646 }
647 else
648 {
649 matches.AppendString (prefix_path + member_name);
650 }
651 }
652 }
653 }
654}
655
656static void
Jason Molendab57e4a12013-11-04 09:33:30 +0000657PrivateAutoComplete (StackFrame *frame,
Greg Claytonf21fead2013-05-14 23:43:18 +0000658 const std::string &partial_path,
659 const std::string &prefix_path, // Anything that has been resolved already will be in here
660 const ClangASTType& clang_type,
661 StringList &matches,
662 bool &word_complete)
663{
664// printf ("\nPrivateAutoComplete()\n\tprefix_path = '%s'\n\tpartial_path = '%s'\n", prefix_path.c_str(), partial_path.c_str());
665 std::string remaining_partial_path;
666
667 const lldb::TypeClass type_class = clang_type.GetTypeClass();
668 if (partial_path.empty())
669 {
670 if (clang_type.IsValid())
671 {
672 switch (type_class)
673 {
674 default:
675 case eTypeClassArray:
676 case eTypeClassBlockPointer:
677 case eTypeClassBuiltin:
678 case eTypeClassComplexFloat:
679 case eTypeClassComplexInteger:
680 case eTypeClassEnumeration:
681 case eTypeClassFunction:
682 case eTypeClassMemberPointer:
683 case eTypeClassReference:
684 case eTypeClassTypedef:
685 case eTypeClassVector:
686 {
687 matches.AppendString (prefix_path);
688 word_complete = matches.GetSize() == 1;
689 }
690 break;
691
692 case eTypeClassClass:
693 case eTypeClassStruct:
694 case eTypeClassUnion:
695 if (prefix_path.back() != '.')
696 matches.AppendString (prefix_path + '.');
697 break;
698
699 case eTypeClassObjCObject:
700 case eTypeClassObjCInterface:
701 break;
702 case eTypeClassObjCObjectPointer:
703 case eTypeClassPointer:
704 {
705 bool omit_empty_base_classes = true;
Greg Clayton57ee3062013-07-11 22:46:58 +0000706 if (clang_type.GetNumChildren (omit_empty_base_classes) > 0)
Greg Claytonf21fead2013-05-14 23:43:18 +0000707 matches.AppendString (prefix_path + "->");
708 else
709 {
710 matches.AppendString (prefix_path);
711 word_complete = true;
712 }
713 }
714 break;
715 }
716 }
717 else
718 {
719 if (frame)
720 {
721 const bool get_file_globals = true;
722
723 VariableList *variable_list = frame->GetVariableList(get_file_globals);
724
Jason Molendaa3a542f2013-10-09 02:39:26 +0000725 if (variable_list)
Greg Claytonf21fead2013-05-14 23:43:18 +0000726 {
Jason Molendaa3a542f2013-10-09 02:39:26 +0000727 const size_t num_variables = variable_list->GetSize();
728 for (size_t i=0; i<num_variables; ++i)
729 {
730 Variable *variable = variable_list->GetVariableAtIndex(i).get();
731 matches.AppendString (variable->GetName().AsCString());
732 }
Greg Claytonf21fead2013-05-14 23:43:18 +0000733 }
734 }
735 }
736 }
737 else
738 {
739 const char ch = partial_path[0];
740 switch (ch)
741 {
742 case '*':
743 if (prefix_path.empty())
744 {
745 PrivateAutoComplete (frame,
746 partial_path.substr(1),
747 std::string("*"),
748 clang_type,
749 matches,
750 word_complete);
751 }
752 break;
753
754 case '&':
755 if (prefix_path.empty())
756 {
757 PrivateAutoComplete (frame,
758 partial_path.substr(1),
759 std::string("&"),
760 clang_type,
761 matches,
762 word_complete);
763 }
764 break;
765
766 case '-':
767 if (partial_path[1] == '>' && !prefix_path.empty())
768 {
769 switch (type_class)
770 {
771 case lldb::eTypeClassPointer:
772 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000773 ClangASTType pointee_type(clang_type.GetPointeeType());
Greg Claytonf21fead2013-05-14 23:43:18 +0000774 if (partial_path[2])
775 {
776 // If there is more after the "->", then search deeper
777 PrivateAutoComplete (frame,
778 partial_path.substr(2),
779 prefix_path + "->",
780 pointee_type.GetCanonicalType(),
781 matches,
782 word_complete);
783 }
784 else
785 {
786 // Nothing after the "->", so list all members
787 PrivateAutoCompleteMembers (frame,
788 std::string(),
789 std::string(),
790 prefix_path + "->",
791 pointee_type.GetCanonicalType(),
792 matches,
793 word_complete);
794 }
795 }
796 default:
797 break;
798 }
799 }
800 break;
801
802 case '.':
803 if (clang_type.IsValid())
804 {
805 switch (type_class)
806 {
807 case lldb::eTypeClassUnion:
808 case lldb::eTypeClassStruct:
809 case lldb::eTypeClassClass:
810 if (partial_path[1])
811 {
812 // If there is more after the ".", then search deeper
813 PrivateAutoComplete (frame,
814 partial_path.substr(1),
815 prefix_path + ".",
816 clang_type,
817 matches,
818 word_complete);
819
820 }
821 else
822 {
823 // Nothing after the ".", so list all members
824 PrivateAutoCompleteMembers (frame,
825 std::string(),
826 partial_path,
827 prefix_path + ".",
828 clang_type,
829 matches,
830 word_complete);
831 }
832 default:
833 break;
834 }
835 }
836 break;
837 default:
838 if (isalpha(ch) || ch == '_' || ch == '$')
839 {
840 const size_t partial_path_len = partial_path.size();
841 size_t pos = 1;
842 while (pos < partial_path_len)
843 {
844 const char curr_ch = partial_path[pos];
845 if (isalnum(curr_ch) || curr_ch == '_' || curr_ch == '$')
846 {
847 ++pos;
848 continue;
849 }
850 break;
851 }
852
853 std::string token(partial_path, 0, pos);
854 remaining_partial_path = partial_path.substr(pos);
855
856 if (clang_type.IsValid())
857 {
858 PrivateAutoCompleteMembers (frame,
859 token,
860 remaining_partial_path,
861 prefix_path,
862 clang_type,
863 matches,
864 word_complete);
865 }
866 else if (frame)
867 {
868 // We haven't found our variable yet
869 const bool get_file_globals = true;
870
871 VariableList *variable_list = frame->GetVariableList(get_file_globals);
872
Enrico Granata16f35ea2013-12-21 08:44:28 +0000873 if (!variable_list)
874 break;
875
Greg Claytonf21fead2013-05-14 23:43:18 +0000876 const size_t num_variables = variable_list->GetSize();
877 for (size_t i=0; i<num_variables; ++i)
878 {
879 Variable *variable = variable_list->GetVariableAtIndex(i).get();
Enrico Granata16f35ea2013-12-21 08:44:28 +0000880
881 if (!variable)
882 continue;
883
Greg Claytonf21fead2013-05-14 23:43:18 +0000884 const char *variable_name = variable->GetName().AsCString();
885 if (strstr(variable_name, token.c_str()) == variable_name)
886 {
887 if (strcmp (variable_name, token.c_str()) == 0)
888 {
889 Type *variable_type = variable->GetType();
890 if (variable_type)
891 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000892 ClangASTType variable_clang_type (variable_type->GetClangForwardType());
Greg Claytonf21fead2013-05-14 23:43:18 +0000893 PrivateAutoComplete (frame,
894 remaining_partial_path,
895 prefix_path + token, // Anything that has been resolved already will be in here
896 variable_clang_type.GetCanonicalType(),
897 matches,
898 word_complete);
899 }
900 else
901 {
902 matches.AppendString (prefix_path + variable_name);
903 }
904 }
905 else if (remaining_partial_path.empty())
906 {
907 matches.AppendString (prefix_path + variable_name);
908 }
909 }
910 }
911 }
912 }
913 break;
914 }
915 }
916}
917
918
919
920size_t
921Variable::AutoComplete (const ExecutionContext &exe_ctx,
922 const char *partial_path_cstr,
923 StringList &matches,
924 bool &word_complete)
925{
926 word_complete = false;
927 std::string partial_path;
928 std::string prefix_path;
929 ClangASTType clang_type;
930 if (partial_path_cstr && partial_path_cstr[0])
931 partial_path = partial_path_cstr;
932
933 PrivateAutoComplete (exe_ctx.GetFramePtr(),
934 partial_path,
935 prefix_path,
936 clang_type,
937 matches,
938 word_complete);
939
940 return matches.GetSize();
Greg Claytonc749eb82011-07-11 05:12:02 +0000941}
942