LLDB mainline
ClangExpressionDeclMap.cpp
Go to the documentation of this file.
1//===-- ClangExpressionDeclMap.cpp ----------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "ClangASTSource.h"
12#include "ClangExpressionUtil.h"
16#include "ClangUtil.h"
17
18#include "NameSearchContext.h"
20#include "lldb/Core/Address.h"
21#include "lldb/Core/Mangled.h"
22#include "lldb/Core/Module.h"
34#include "lldb/Symbol/Type.h"
40#include "lldb/Target/Process.h"
43#include "lldb/Target/Target.h"
44#include "lldb/Target/Thread.h"
45#include "lldb/Utility/Endian.h"
47#include "lldb/Utility/Log.h"
49#include "lldb/Utility/Status.h"
53#include "lldb/lldb-private.h"
54#include "clang/AST/ASTConsumer.h"
55#include "clang/AST/ASTContext.h"
56#include "clang/AST/ASTImporter.h"
57#include "clang/AST/Decl.h"
58#include "clang/AST/DeclarationName.h"
59#include "clang/AST/RecursiveASTVisitor.h"
60
62
63using namespace lldb;
64using namespace lldb_private;
65using namespace clang;
66
67static const char *g_lldb_local_vars_namespace_cstr = "$__lldb_local_vars";
68
69namespace {
70/// A lambda is represented by Clang as an artifical class whose
71/// members are the lambda captures. If we capture a 'this' pointer,
72/// the artifical class will contain a member variable named 'this'.
73/// The function returns a ValueObject for the captured 'this' if such
74/// member exists. If no 'this' was captured, return a nullptr.
75lldb::ValueObjectSP GetCapturedThisValueObject(StackFrame *frame) {
76 assert(frame);
77
78 if (auto thisValSP = frame->FindVariable(ConstString("this"))) {
79 if (auto thisThisValSP = thisValSP->GetChildMemberWithName("this"))
80 return thisThisValSP;
81 // With CodeView/PDB, the member is named "__this".
82 if (auto codeview_this_sp = thisValSP->GetChildMemberWithName("__this"))
83 return codeview_this_sp;
84 }
85
86 return nullptr;
87}
88} // namespace
89
91 bool keep_result_in_memory,
93 const lldb::TargetSP &target,
94 const std::shared_ptr<ClangASTImporter> &importer, ValueObject *ctx_obj,
95 bool ignore_context_qualifiers)
96 : ClangASTSource(target, importer), m_found_entities(), m_struct_members(),
97 m_keep_result_in_memory(keep_result_in_memory),
98 m_result_delegate(result_delegate), m_ctx_obj(ctx_obj),
99 m_ignore_context_qualifiers(ignore_context_qualifiers), m_parser_vars(),
100 m_struct_vars() {
102}
103
105 // Note: The model is now that the parser's AST context and all associated
106 // data does not vanish until the expression has been executed. This means
107 // that valuable lookup data (like namespaces) doesn't vanish, but
108
109 DidParse();
111}
112
114 Materializer *materializer) {
116 m_parser_vars->m_exe_ctx = exe_ctx;
117
118 Target *target = exe_ctx.GetTargetPtr();
119 if (exe_ctx.GetFramePtr())
120 m_parser_vars->m_sym_ctx =
121 exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
122 else if (exe_ctx.GetThreadPtr() &&
124 m_parser_vars->m_sym_ctx =
125 exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(
126 lldb::eSymbolContextEverything);
127 else if (exe_ctx.GetProcessPtr()) {
128 m_parser_vars->m_sym_ctx.Clear(true);
129 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
130 } else if (target) {
131 m_parser_vars->m_sym_ctx.Clear(true);
132 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
133 }
134
135 if (target) {
136 m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(
138
140 return false;
141 }
142
143 m_parser_vars->m_target_info = GetTargetInfo();
144 m_parser_vars->m_materializer = materializer;
145
146 return true;
147}
148
150 clang::ASTConsumer *code_gen) {
151 assert(m_parser_vars);
152 m_parser_vars->m_code_gen = code_gen;
153}
154
156 DiagnosticManager &diag_manager) {
157 assert(m_parser_vars);
158 m_parser_vars->m_diagnostics = &diag_manager;
159}
160
162 if (m_parser_vars && m_parser_vars->m_persistent_vars) {
163 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
164 entity_index < num_entities; ++entity_index) {
166 m_found_entities.GetVariableAtIndex(entity_index));
167 if (var_sp)
168 llvm::cast<ClangExpressionVariable>(var_sp.get())
169 ->DisableParserVars(GetParserID());
170 }
171
172 for (size_t pvar_index = 0,
173 num_pvars = m_parser_vars->m_persistent_vars->GetSize();
174 pvar_index < num_pvars; ++pvar_index) {
175 ExpressionVariableSP pvar_sp(
176 m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
177 if (ClangExpressionVariable *clang_var =
178 llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get()))
179 clang_var->DisableParserVars(GetParserID());
180 }
181
183 }
184}
185
186// Interface for IRForTarget
187
189 assert(m_parser_vars.get());
190
191 TargetInfo ret;
192
193 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
194
195 Process *process = exe_ctx.GetProcessPtr();
196 if (process) {
197 ret.byte_order = process->GetByteOrder();
198 ret.address_byte_size = process->GetAddressByteSize();
199 } else {
200 Target *target = exe_ctx.GetTargetPtr();
201 if (target) {
202 ret.byte_order = target->GetArchitecture().GetByteOrder();
204 }
205 }
206
207 return ret;
208}
209
211 TypeSystemClang &source,
212 TypeFromParser parser_type) {
213 assert(&target == GetScratchContext(*m_target).get());
214 assert((TypeSystem *)&source ==
215 parser_type.GetTypeSystem().GetSharedPointer().get());
216 assert(&source.getASTContext() == m_ast_context);
217
218 return TypeFromUser(m_ast_importer_sp->DeportType(target, parser_type));
219}
220
222 ConstString name,
223 TypeFromParser parser_type,
224 bool is_result,
225 bool is_lvalue) {
226 assert(m_parser_vars.get());
227 auto ast = parser_type.GetTypeSystem<TypeSystemClang>();
228 if (ast == nullptr)
229 return false;
230
231 // Check if we already declared a persistent variable with the same name.
232 if (lldb::ExpressionVariableSP conflicting_var =
233 m_parser_vars->m_persistent_vars->GetVariable(name)) {
234 std::string msg = llvm::formatv("redefinition of persistent variable '{0}'",
235 name).str();
236 m_parser_vars->m_diagnostics->AddDiagnostic(
238 return false;
239 }
240
241 if (m_parser_vars->m_materializer && is_result) {
242 Status err;
243
244 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
245 Target *target = exe_ctx.GetTargetPtr();
246 if (target == nullptr)
247 return false;
248
249 auto clang_ast_context = GetScratchContext(*target);
250 if (!clang_ast_context)
251 return false;
252
253 TypeFromUser user_type = DeportType(*clang_ast_context, *ast, parser_type);
254
255 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(
256 user_type, is_lvalue, m_keep_result_in_memory, m_result_delegate, err);
257
259 exe_ctx.GetBestExecutionContextScope(), name, user_type,
260 m_parser_vars->m_target_info.byte_order,
261 m_parser_vars->m_target_info.address_byte_size);
262
263 m_found_entities.AddNewlyConstructedVariable(var);
264
266
269
270 parser_vars->m_named_decl = decl;
271
273
275
276 jit_vars->m_offset = offset;
277
278 return true;
279 }
280
282 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
283 Target *target = exe_ctx.GetTargetPtr();
284 if (target == nullptr)
285 return false;
286
287 auto context = GetScratchContext(*target);
288 if (!context)
289 return false;
290
291 TypeFromUser user_type = DeportType(*context, *ast, parser_type);
292
293 if (!user_type.GetOpaqueQualType()) {
294 LLDB_LOG(log, "Persistent variable's type wasn't copied successfully");
295 return false;
296 }
297
298 if (!m_parser_vars->m_target_info.IsValid())
299 return false;
300
301 if (!m_parser_vars->m_persistent_vars)
302 return false;
303
304 ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>(
305 m_parser_vars->m_persistent_vars
306 ->CreatePersistentVariable(
307 exe_ctx.GetBestExecutionContextScope(), name, user_type,
308 m_parser_vars->m_target_info.byte_order,
309 m_parser_vars->m_target_info.address_byte_size)
310 .get());
311
312 if (!var)
313 return false;
314
315 var->m_frozen_sp->SetHasCompleteType();
316
317 if (is_result)
318 var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
319 else
320 var->m_flags |=
321 ClangExpressionVariable::EVKeepInTarget; // explicitly-declared
322 // persistent variables should
323 // persist
324
325 if (is_lvalue) {
326 var->m_flags |= ClangExpressionVariable::EVIsProgramReference;
327 } else {
328 var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
329 var->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
330 }
331
333 var->m_flags |= ClangExpressionVariable::EVKeepInTarget;
334 }
335
336 LLDB_LOG(log, "Created persistent variable with flags {0:x}", var->m_flags);
337
339
342
343 parser_vars->m_named_decl = decl;
344
345 return true;
346}
347
349 ConstString name,
350 llvm::Value *value, size_t size,
351 lldb::offset_t alignment) {
352 assert(m_struct_vars.get());
353 assert(m_parser_vars.get());
354
355 bool is_persistent_variable = false;
356
358
359 m_struct_vars->m_struct_laid_out = false;
360
362 GetParserID()))
363 return true;
364
366 m_found_entities, decl, GetParserID()));
367
368 if (!var && m_parser_vars->m_persistent_vars) {
370 *m_parser_vars->m_persistent_vars, decl, GetParserID());
371 is_persistent_variable = true;
372 }
373
374 if (!var)
375 return false;
376
377 LLDB_LOG(log, "Adding value for (NamedDecl*){0} [{1} - {2}] to the structure",
378 decl, name, var->GetName());
379
380 // We know entity->m_parser_vars is valid because we used a parser variable
381 // to find it
382
384 llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID());
385
386 parser_vars->m_llvm_value = value;
387
389 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID())) {
390 // We already laid this out; do not touch
391
392 LLDB_LOG(log, "Already placed at {0:x}", jit_vars->m_offset);
393 }
394
395 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID());
396
398 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID());
399
400 jit_vars->m_alignment = alignment;
401 jit_vars->m_size = size;
402
403 m_struct_members.AddVariable(var->shared_from_this());
404
405 if (m_parser_vars->m_materializer) {
406 uint32_t offset = 0;
407
408 Status err;
409
410 if (is_persistent_variable) {
411 ExpressionVariableSP var_sp(var->shared_from_this());
412 offset = m_parser_vars->m_materializer->AddPersistentVariable(
413 var_sp, nullptr, err);
414 } else {
415 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
416 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
417 else if (const RegisterInfo *reg_info = var->GetRegisterInfo())
418 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
419 else if (parser_vars->m_lldb_var)
420 offset = m_parser_vars->m_materializer->AddVariable(
421 parser_vars->m_lldb_var, err);
422 else if (parser_vars->m_lldb_valobj_provider) {
423 offset = m_parser_vars->m_materializer->AddValueObject(
424 name, parser_vars->m_lldb_valobj_provider, err);
425 }
426 }
427
428 if (!err.Success())
429 return false;
430
431 LLDB_LOG(log, "Placed at {0:x}", offset);
432
433 jit_vars->m_offset =
434 offset; // TODO DoStructLayout() should not change this.
435 }
436
437 return true;
438}
439
441 assert(m_struct_vars.get());
442
443 if (m_struct_vars->m_struct_laid_out)
444 return true;
445
446 if (!m_parser_vars->m_materializer)
447 return false;
448
449 m_struct_vars->m_struct_alignment =
450 m_parser_vars->m_materializer->GetStructAlignment();
451 m_struct_vars->m_struct_size =
452 m_parser_vars->m_materializer->GetStructByteSize();
453 m_struct_vars->m_struct_laid_out = true;
454 return true;
455}
456
457bool ClangExpressionDeclMap::GetStructInfo(uint32_t &num_elements, size_t &size,
458 lldb::offset_t &alignment) {
459 assert(m_struct_vars.get());
460
461 if (!m_struct_vars->m_struct_laid_out)
462 return false;
463
464 num_elements = m_struct_members.GetSize();
465 size = m_struct_vars->m_struct_size;
466 alignment = m_struct_vars->m_struct_alignment;
467
468 return true;
469}
470
472 llvm::Value *&value,
473 lldb::offset_t &offset,
474 ConstString &name,
475 uint32_t index) {
476 assert(m_struct_vars.get());
477
478 if (!m_struct_vars->m_struct_laid_out)
479 return false;
480
481 if (index >= m_struct_members.GetSize())
482 return false;
483
484 ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
485
486 if (!member_sp)
487 return false;
488
490 llvm::cast<ClangExpressionVariable>(member_sp.get())
491 ->GetParserVars(GetParserID());
493 llvm::cast<ClangExpressionVariable>(member_sp.get())
494 ->GetJITVars(GetParserID());
495
496 if (!parser_vars || !jit_vars || !member_sp->GetValueObject())
497 return false;
498
499 decl = parser_vars->m_named_decl;
500 value = parser_vars->m_llvm_value;
501 offset = jit_vars->m_offset;
502 name = member_sp->GetName();
503
504 return true;
505}
506
508 uint64_t &ptr) {
510 m_found_entities, decl, GetParserID()));
511
512 if (!entity)
513 return false;
514
515 // We know m_parser_vars is valid since we searched for the variable by its
516 // NamedDecl
517
519 entity->GetParserVars(GetParserID());
520
521 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
522
523 return true;
524}
525
527 Process *process,
528 ConstString name,
529 lldb::SymbolType symbol_type,
530 lldb_private::Module *module) {
531 SymbolContextList sc_list;
532
533 if (module)
534 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
535 else
536 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
537
538 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
539
540 for (const SymbolContext &sym_ctx : sc_list) {
541 if (symbol_load_addr != 0 && symbol_load_addr != LLDB_INVALID_ADDRESS)
542 break;
543
544 const Address sym_address = sym_ctx.symbol->GetAddress();
545
546 if (!sym_address.IsValid())
547 continue;
548
549 switch (sym_ctx.symbol->GetType()) {
550 case eSymbolTypeCode:
552 symbol_load_addr = sym_address.GetCallableLoadAddress(&target);
553 break;
554
556 symbol_load_addr = sym_address.GetCallableLoadAddress(&target, true);
557 break;
558
560 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
561 if (reexport_name) {
562 ModuleSP reexport_module_sp;
563 ModuleSpec reexport_module_spec;
564 reexport_module_spec.GetPlatformFileSpec() =
565 sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
566 if (reexport_module_spec.GetPlatformFileSpec()) {
567 reexport_module_sp =
568 target.GetImages().FindFirstModule(reexport_module_spec);
569 if (!reexport_module_sp) {
570 reexport_module_spec.GetPlatformFileSpec().ClearDirectory();
571 reexport_module_sp =
572 target.GetImages().FindFirstModule(reexport_module_spec);
573 }
574 }
575 symbol_load_addr = GetSymbolAddress(
576 target, process, sym_ctx.symbol->GetReExportedSymbolName(),
577 symbol_type, reexport_module_sp.get());
578 }
579 } break;
580
581 case eSymbolTypeData:
584 case eSymbolTypeLocal:
585 case eSymbolTypeParam:
593 case eSymbolTypeBlock:
607 symbol_load_addr = sym_address.GetLoadAddress(&target);
608 break;
609 }
610 }
611
612 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process) {
614
615 if (runtime) {
616 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
617 }
618 }
619
620 return symbol_load_addr;
621}
622
624 lldb::SymbolType symbol_type) {
625 assert(m_parser_vars.get());
626
627 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
629
630 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(),
631 m_parser_vars->m_exe_ctx.GetProcessPtr(), name,
632 symbol_type);
633}
634
636 Target &target, ModuleSP &module, ConstString name,
637 const CompilerDeclContext &namespace_decl) {
638 VariableList vars;
639
640 if (module && namespace_decl)
641 module->FindGlobalVariables(name, namespace_decl, -1, vars);
642 else
643 target.GetImages().FindGlobalVariables(name, -1, vars);
644
645 if (vars.GetSize() == 0)
646 return VariableSP();
647 return vars.GetVariableAtIndex(0);
648}
649
651 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
652 if (frame == nullptr)
653 return nullptr;
654
655 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
656 lldb::eSymbolContextBlock);
657 if (sym_ctx.block == nullptr)
658 return nullptr;
659
660 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
661 if (!frame_decl_context)
662 return nullptr;
663
664 return llvm::dyn_cast_or_null<TypeSystemClang>(
665 frame_decl_context.GetTypeSystem());
666}
667
668// Interface for ClangASTSource
669
671 NameSearchContext &context) {
672 assert(m_ast_context);
673
674 const auto name = context.m_decl_name.getAsString();
675
677
678 if (log) {
679 if (!context.m_decl_context)
680 LLDB_LOG(log,
681 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
682 "'{0}' in a NULL DeclContext",
683 name);
684 else if (const NamedDecl *context_named_decl =
685 dyn_cast<NamedDecl>(context.m_decl_context))
686 LLDB_LOG(log,
687 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
688 "'{0}' in '{1}'",
689 name, context_named_decl->getNameAsString());
690 else
691 LLDB_LOG(log,
692 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
693 "'{0}' in a '{1}'",
694 name, context.m_decl_context->getDeclKindName());
695 }
696
697 if (const NamespaceDecl *namespace_context =
698 dyn_cast<NamespaceDecl>(context.m_decl_context)) {
699 if (namespace_context->getName() == g_lldb_local_vars_namespace_cstr) {
700 CompilerDeclContext compiler_decl_ctx =
701 m_clang_ast_context->CreateDeclContext(
702 const_cast<clang::DeclContext *>(context.m_decl_context));
703 FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx);
704 return;
705 }
706
708 m_ast_importer_sp->GetNamespaceMap(namespace_context);
709
710 if (!namespace_map)
711 return;
712
714 log, " CEDM::FEVD Inspecting (NamespaceMap*){0:x} ({1} entries)",
715 namespace_map.get(), namespace_map->size());
716
717 for (ClangASTImporter::NamespaceMapItem &n : *namespace_map) {
718 LLDB_LOG(log, " CEDM::FEVD Searching namespace {0} in module {1}",
719 n.second.GetName(), n.first->GetFileSpec().GetFilename());
720
721 FindExternalVisibleDecls(context, n.first, n.second);
722 }
723 } else if (isa<TranslationUnitDecl>(context.m_decl_context)) {
724 CompilerDeclContext namespace_decl;
725
726 LLDB_LOG(log, " CEDM::FEVD Searching the root namespace");
727
728 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl);
729 }
730
732}
733
735 FunctionDecl *copied_function_decl) {
736 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) {
737 clang::DeclGroupRef decl_group_ref(copied_function_decl);
738 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
739 }
740}
741
743 if (!m_parser_vars)
744 return nullptr;
745 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
746 if (!target)
747 return nullptr;
748
750
751 if (!m_parser_vars->m_persistent_vars)
752 return nullptr;
753 return m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
754}
755
757 const ConstString name) {
759
760 NamedDecl *persistent_decl = GetPersistentDecl(name);
761
762 if (!persistent_decl)
763 return;
764
765 Decl *parser_persistent_decl = CopyDecl(persistent_decl);
766
767 if (!parser_persistent_decl)
768 return;
769
770 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
771
772 if (!parser_named_decl)
773 return;
774
775 if (clang::FunctionDecl *parser_function_decl =
776 llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
777 MaybeRegisterFunctionBody(parser_function_decl);
778 }
779
780 LLDB_LOG(log, " CEDM::FEVD Found persistent decl {0}", name);
781
782 context.AddNamedDecl(parser_named_decl);
783}
784
787
788 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
789 SymbolContext sym_ctx;
790 if (frame != nullptr)
791 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
792 lldb::eSymbolContextBlock);
793
794 // FIXME: Currently m_ctx_obj is only used through
795 // SBValue::EvaluateExpression. Can we instead *always* use m_ctx_obj
796 // regardless of which EvaluateExpression path we go through? Then we wouldn't
797 // need two separate code-paths here.
798 if (m_ctx_obj) {
799 Status status;
800 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
801 if (!ctx_obj_ptr || status.Fail())
802 return;
803
804 AddContextClassType(context, TypeFromUser(m_ctx_obj->GetCompilerType()));
805 return;
806 }
807
808 // Clang is looking for the type of "this"
809
810 if (frame == nullptr)
811 return;
812
813 // Find the block that defines the function represented by "sym_ctx"
814 Block *function_block = sym_ctx.GetFunctionBlock();
815
816 if (!function_block)
817 return;
818
819 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
820
821 if (!function_decl_ctx)
822 return;
823
824 clang::CXXMethodDecl *method_decl =
826
827 if (method_decl) {
828 if (auto capturedThis = GetCapturedThisValueObject(frame)) {
829 // We're inside a lambda and we captured a 'this'.
830 // Import the outer class's AST instead of the
831 // (unnamed) lambda structure AST so unqualified
832 // member lookups are understood by the Clang parser.
833 //
834 // If we're in a lambda which didn't capture 'this',
835 // $__lldb_class will correspond to the lambda closure
836 // AST and references to captures will resolve like
837 // regular member varaiable accesses do.
838 TypeFromUser pointee_type =
839 capturedThis->GetCompilerType().GetPointeeType();
840
841 LLDB_LOG(log,
842 " CEDM::FEVD Adding captured type ({0} for"
843 " $__lldb_class: {1}",
844 capturedThis->GetTypeName(), capturedThis->GetName());
845
846 AddContextClassType(context, pointee_type);
847 return;
848 }
849
850 clang::CXXRecordDecl *class_decl = method_decl->getParent();
851
852 QualType class_qual_type = m_ast_context->getCanonicalTagType(class_decl);
853
854 // The synthesized __lldb_expr will adopt the qualifiers from this class
855 // type. Make sure we use the qualifiers of the method that we're currently
856 // stopped in.
857 class_qual_type.addFastQualifiers(
858 method_decl->getMethodQualifiers().getFastQualifiers());
859
860 TypeFromUser class_user_type(
861 class_qual_type.getAsOpaquePtr(),
862 function_decl_ctx.GetTypeSystem()->weak_from_this());
863
864 LLDB_LOG(log, " CEDM::FEVD Adding type for $__lldb_class: {0}",
865 class_qual_type.getAsString());
866
867 AddContextClassType(context, class_user_type);
868 return;
869 }
870
871 // FIXME: this code is supposed to handl cases where a function decl
872 // was not attached to a class scope but its DIE had a `DW_AT_object_pointer`
873 // (and thus has a local `this` variable). This isn't a tested flow and
874 // even -flimit-debug-info doesn't seem to generate DWARF like that, so
875 // we should get rid of this code-path. An alternative fix if we ever
876 // encounter such DWARF is for the TypeSystem to attach the function
877 // to some valid class context (we can derive the type of the context
878 // through the `this` pointer anyway.
879 //
880 // The actual reason we can't remove this code is that LLDB currently
881 // creates decls for function templates by attaching them to the TU instead
882 // of a class context. So we can actually have template methods scoped
883 // outside of a class. Once we fix that, we can remove this code-path.
884 // Additionally, we exclude synthetic variables from here. Clang-based
885 // languages are unlikely candidates for synthetic variables anyway, and
886 // especially in this case, we're looking for something specific to C++.
887 VariableList *vars = frame->GetVariableList(
888 /*get_file_globals=*/false, /*include_synthetic_vars=*/false, nullptr);
889
890 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
891
892 if (this_var && this_var->IsInScope(frame) &&
893 this_var->LocationIsValidForFrame(frame)) {
894 Type *this_type = this_var->GetType();
895
896 if (!this_type)
897 return;
898
899 TypeFromUser pointee_type =
901
902 LLDB_LOG(log, " FEVD Adding type for $__lldb_class: {0}",
903 ClangUtil::GetQualType(pointee_type).getAsString());
904
905 AddContextClassType(context, pointee_type);
906 }
907}
908
911
912 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
913
914 if (m_ctx_obj) {
915 Status status;
916 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
917 if (!ctx_obj_ptr || status.Fail())
918 return;
919
920 AddOneType(context, TypeFromUser(m_ctx_obj->GetCompilerType()));
921 return;
922 }
923
924 // Clang is looking for the type of "*self"
925
926 if (!frame)
927 return;
928
929 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
930 lldb::eSymbolContextBlock);
931
932 // Find the block that defines the function represented by "sym_ctx"
933 Block *function_block = sym_ctx.GetFunctionBlock();
934
935 if (!function_block)
936 return;
937
938 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
939
940 if (!function_decl_ctx)
941 return;
942
943 clang::ObjCMethodDecl *method_decl =
945
946 if (method_decl) {
947 ObjCInterfaceDecl *self_interface = method_decl->getClassInterface();
948
949 if (!self_interface)
950 return;
951
952 const clang::Type *interface_type = self_interface->getTypeForDecl();
953
954 if (!interface_type)
955 return; // This is unlikely, but we have seen crashes where this
956 // occurred
957
958 TypeFromUser class_user_type(
959 QualType(interface_type, 0).getAsOpaquePtr(),
960 function_decl_ctx.GetTypeSystem()->weak_from_this());
961
962 LLDB_LOG(log, " FEVD Adding type for $__lldb_objc_class: {0}",
963 ClangUtil::ToString(interface_type));
964
965 AddOneType(context, class_user_type);
966 return;
967 }
968 // This branch will get hit if we are executing code in the context of
969 // a function that claims to have an object pointer (through
970 // DW_AT_object_pointer?) but is not formally a method of the class.
971 // In that case, just look up the "self" variable in the current scope
972 // and use its type.
973
974 // We exclude synthetic variables from here. Like above, it's highly unlikely
975 // we care about synthetic variables here, and indeed this code is looking for
976 // an obj-C specific construct.
977 VariableList *vars = frame->GetVariableList(
978 /*get_file_globals=*/false, /*include_synthetic_vars=*/false, nullptr);
979
980 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
981
982 if (!self_var)
983 return;
984 if (!self_var->IsInScope(frame))
985 return;
986 if (!self_var->LocationIsValidForFrame(frame))
987 return;
988
989 Type *self_type = self_var->GetType();
990
991 if (!self_type)
992 return;
993
994 CompilerType self_clang_type = self_type->GetFullCompilerType();
995
996 if (TypeSystemClang::IsObjCClassType(self_clang_type)) {
997 return;
998 }
999 if (!TypeSystemClang::IsObjCObjectPointerType(self_clang_type))
1000 return;
1001 self_clang_type = self_clang_type.GetPointeeType();
1002
1003 if (!self_clang_type)
1004 return;
1005
1006 LLDB_LOG(log, " FEVD Adding type for $__lldb_objc_class: {0}",
1008
1009 TypeFromUser class_user_type(self_clang_type);
1010
1011 AddOneType(context, class_user_type);
1012}
1013
1015 SymbolContext &sym_ctx, NameSearchContext &name_context) {
1016 if (sym_ctx.block == nullptr)
1017 return;
1018
1019 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
1020 if (!frame_decl_context)
1021 return;
1022
1023 TypeSystemClang *frame_ast = llvm::dyn_cast_or_null<TypeSystemClang>(
1024 frame_decl_context.GetTypeSystem());
1025 if (!frame_ast)
1026 return;
1027
1028 clang::NamespaceDecl *namespace_decl =
1029 m_clang_ast_context->GetUniqueNamespaceDeclaration(
1031 if (!namespace_decl)
1032 return;
1033
1034 name_context.AddNamedDecl(namespace_decl);
1035 clang::DeclContext *ctxt = clang::Decl::castToDeclContext(namespace_decl);
1036 ctxt->setHasExternalVisibleStorage(true);
1037 name_context.m_found_local_vars_nsp = true;
1038}
1039
1041 NameSearchContext &context, ConstString name) {
1043
1044 if (!m_target)
1045 return;
1046
1047 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1049 if (!modules_decl_vendor)
1050 return;
1051
1052 bool append = false;
1053 uint32_t max_matches = 1;
1054 std::vector<CompilerDecl> decls;
1055
1056 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
1057 return;
1058
1059 assert(!decls.empty() && "FindDecls returned true but no decls?");
1060 auto *const decl_from_modules =
1061 llvm::cast<NamedDecl>(ClangUtil::GetDecl(decls[0]));
1062
1063 LLDB_LOG(log,
1064 " CAS::FEVD Matching decl found for "
1065 "\"{0}\" in the modules",
1066 name);
1067
1068 clang::Decl *copied_decl = CopyDecl(decl_from_modules);
1069 if (!copied_decl) {
1070 LLDB_LOG(log, " CAS::FEVD - Couldn't export a "
1071 "declaration from the modules");
1072 return;
1073 }
1074
1075 if (auto copied_function = dyn_cast<clang::FunctionDecl>(copied_decl)) {
1076 MaybeRegisterFunctionBody(copied_function);
1077
1078 context.AddNamedDecl(copied_function);
1079 } else if (auto copied_var = dyn_cast<clang::VarDecl>(copied_decl)) {
1080 context.AddNamedDecl(copied_var);
1081 context.m_found_variable = true;
1082 }
1083}
1084
1086 NameSearchContext &context, ConstString name, SymbolContext &sym_ctx,
1087 const CompilerDeclContext &namespace_decl) {
1088 if (sym_ctx.block == nullptr)
1089 return false;
1090
1091 CompilerDeclContext decl_context = sym_ctx.block->GetDeclContext();
1092 if (!decl_context)
1093 return false;
1094
1095 // Make sure that the variables are parsed so that we have the
1096 // declarations.
1097 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1098 VariableListSP vars = frame->GetInScopeVariableList(true);
1099 for (size_t i = 0; i < vars->GetSize(); i++)
1100 vars->GetVariableAtIndex(i)->GetDecl();
1101
1102 // Search for declarations matching the name. Do not include imported
1103 // decls in the search if we are looking for decls in the artificial
1104 // namespace $__lldb_local_vars.
1105 std::vector<CompilerDecl> found_decls =
1106 decl_context.FindDeclByName(name, namespace_decl.IsValid());
1107
1108 VariableSP var;
1109 bool variable_found = false;
1110 for (CompilerDecl decl : found_decls) {
1111 for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
1112 VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1113 if (candidate_var->GetDecl() == decl) {
1114 var = candidate_var;
1115 break;
1116 }
1117 }
1118
1119 if (var && !variable_found) {
1120 variable_found = true;
1121 ValueObjectSP valobj = ValueObjectVariable::Create(frame, var);
1122 AddOneVariable(context, var, valobj);
1123 context.m_found_variable = true;
1124 }
1125 }
1126
1127 // We're in a local_var_lookup but haven't found any local variables
1128 // so far. When performing a variable lookup from within the context of
1129 // a lambda, we count the lambda captures as local variables. Thus,
1130 // see if we captured any variables with the requested 'name'.
1131 if (!variable_found) {
1132 auto find_capture = [](ConstString varname,
1133 StackFrame *frame) -> ValueObjectSP {
1134 if (auto lambda = ClangExpressionUtil::GetLambdaValueObject(frame)) {
1135 if (auto capture = lambda->GetChildMemberWithName(varname)) {
1136 return capture;
1137 }
1138 }
1139
1140 return nullptr;
1141 };
1142
1143 if (auto capture = find_capture(name, frame)) {
1144 variable_found = true;
1145 context.m_found_variable = true;
1146 AddOneVariable(context, std::move(capture), std::move(find_capture));
1147 }
1148 }
1149
1150 return variable_found;
1151}
1152
1153/// Structure to hold the info needed when comparing function
1154/// declarations.
1155namespace {
1156struct FuncDeclInfo {
1157 ConstString m_name;
1158 CompilerType m_copied_type;
1159 uint32_t m_decl_lvl;
1160 SymbolContext m_sym_ctx;
1161};
1162} // namespace
1163
1165 const SymbolContextList &sc_list,
1166 const CompilerDeclContext &frame_decl_context) {
1167 // First, symplify things by looping through the symbol contexts to
1168 // remove unwanted functions and separate out the functions we want to
1169 // compare and prune into a separate list. Cache the info needed about
1170 // the function declarations in a vector for efficiency.
1171 SymbolContextList sc_sym_list;
1172 std::vector<FuncDeclInfo> decl_infos;
1173 decl_infos.reserve(sc_list.GetSize());
1174 clang::DeclContext *frame_decl_ctx =
1175 (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext();
1176 TypeSystemClang *ast = llvm::dyn_cast_or_null<TypeSystemClang>(
1177 frame_decl_context.GetTypeSystem());
1178
1179 for (const SymbolContext &sym_ctx : sc_list) {
1180 FuncDeclInfo fdi;
1181
1182 // We don't know enough about symbols to compare them, but we should
1183 // keep them in the list.
1184 Function *function = sym_ctx.function;
1185 if (!function) {
1186 sc_sym_list.Append(sym_ctx);
1187 continue;
1188 }
1189 // Filter out functions without declaration contexts, as well as
1190 // class/instance methods, since they'll be skipped in the code that
1191 // follows anyway.
1192 CompilerDeclContext func_decl_context = function->GetDeclContext();
1193 if (!func_decl_context || func_decl_context.IsClassMethod())
1194 continue;
1195 // We can only prune functions for which we can copy the type.
1196 CompilerType func_clang_type = function->GetType()->GetFullCompilerType();
1197 CompilerType copied_func_type = GuardedCopyType(func_clang_type);
1198 if (!copied_func_type) {
1199 sc_sym_list.Append(sym_ctx);
1200 continue;
1201 }
1202
1203 fdi.m_sym_ctx = sym_ctx;
1204 fdi.m_name = function->GetName();
1205 fdi.m_copied_type = copied_func_type;
1206 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL;
1207 if (fdi.m_copied_type && func_decl_context) {
1208 // Call CountDeclLevels to get the number of parent scopes we have
1209 // to look through before we find the function declaration. When
1210 // comparing functions of the same type, the one with a lower count
1211 // will be closer to us in the lookup scope and shadows the other.
1212 clang::DeclContext *func_decl_ctx =
1213 (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext();
1214 fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx, func_decl_ctx,
1215 &fdi.m_name, &fdi.m_copied_type);
1216 }
1217 decl_infos.emplace_back(fdi);
1218 }
1219
1220 // Loop through the functions in our cache looking for matching types,
1221 // then compare their scope levels to see which is closer.
1222 std::multimap<CompilerType, const FuncDeclInfo *> matches;
1223 for (const FuncDeclInfo &fdi : decl_infos) {
1224 const CompilerType t = fdi.m_copied_type;
1225 auto q = matches.find(t);
1226 if (q != matches.end()) {
1227 if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1228 // This function is closer; remove the old set.
1229 matches.erase(t);
1230 else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1231 // The functions in our set are closer - skip this one.
1232 continue;
1233 }
1234 matches.insert(std::make_pair(t, &fdi));
1235 }
1236
1237 // Loop through our matches and add their symbol contexts to our list.
1238 SymbolContextList sc_func_list;
1239 for (const auto &q : matches)
1240 sc_func_list.Append(q.second->m_sym_ctx);
1241
1242 // Rejoin the lists with the functions in front.
1243 sc_func_list.Append(sc_sym_list);
1244 return sc_func_list;
1245}
1246
1248 NameSearchContext &context, lldb::ModuleSP module_sp, ConstString name,
1249 const CompilerDeclContext &namespace_decl) {
1250 if (!m_parser_vars)
1251 return false;
1252
1253 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1254
1255 std::vector<CompilerDecl> decls_from_modules;
1256
1257 if (target) {
1258 if (std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
1260 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
1261 }
1262 }
1263
1264 SymbolContextList sc_list;
1265 if (namespace_decl && module_sp) {
1266 ModuleFunctionSearchOptions function_options;
1267 function_options.include_inlines = false;
1268 function_options.include_symbols = false;
1269
1270 module_sp->FindFunctions(name, namespace_decl, eFunctionNameTypeBase,
1271 function_options, sc_list);
1272 } else if (target && !namespace_decl) {
1273 ModuleFunctionSearchOptions function_options;
1274 function_options.include_inlines = false;
1275 function_options.include_symbols = true;
1276
1277 // TODO Fix FindFunctions so that it doesn't return
1278 // instance methods for eFunctionNameTypeBase.
1279
1280 target->GetImages().FindFunctions(
1281 name, eFunctionNameTypeFull | eFunctionNameTypeBase, function_options,
1282 sc_list);
1283 }
1284
1285 // If we found more than one function, see if we can use the frame's decl
1286 // context to remove functions that are shadowed by other functions which
1287 // match in type but are nearer in scope.
1288 //
1289 // AddOneFunction will not add a function whose type has already been
1290 // added, so if there's another function in the list with a matching type,
1291 // check to see if their decl context is a parent of the current frame's or
1292 // was imported via a and using statement, and pick the best match
1293 // according to lookup rules.
1294 if (sc_list.GetSize() > 1) {
1295 // Collect some info about our frame's context.
1296 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1297 SymbolContext frame_sym_ctx;
1298 if (frame != nullptr)
1299 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1300 lldb::eSymbolContextBlock);
1301 CompilerDeclContext frame_decl_context =
1302 frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext()
1304
1305 // We can't do this without a compiler decl context for our frame.
1306 if (frame_decl_context) {
1307 sc_list = SearchFunctionsInSymbolContexts(sc_list, frame_decl_context);
1308 }
1309 }
1310
1311 bool found_function_with_type_info = false;
1312
1313 if (sc_list.GetSize()) {
1314 const Symbol *extern_symbol = nullptr;
1315 const Symbol *non_extern_symbol = nullptr;
1316
1317 for (const SymbolContext &sym_ctx : sc_list) {
1318 if (sym_ctx.function) {
1319 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext();
1320
1321 if (!decl_ctx)
1322 continue;
1323
1324 // Filter out class/instance methods.
1325 if (decl_ctx.IsClassMethod())
1326 continue;
1327
1328 AddOneFunction(context, sym_ctx.function, nullptr);
1329 found_function_with_type_info = true;
1330 } else if (sym_ctx.symbol) {
1331 const Symbol *symbol = sym_ctx.symbol;
1332 if (target) {
1333 if (Symbol *actual =
1334 symbol->ResolveReExportedSymbol(*target, sym_ctx.module_sp))
1335 symbol = actual;
1336 else if (symbol->GetType() == eSymbolTypeReExported)
1337 continue;
1338 }
1339
1340 if (symbol->IsExternal())
1341 extern_symbol = symbol;
1342 else
1343 non_extern_symbol = symbol;
1344 }
1345 }
1346
1347 if (!found_function_with_type_info) {
1348 for (const CompilerDecl &compiler_decl : decls_from_modules) {
1349 clang::Decl *decl = ClangUtil::GetDecl(compiler_decl);
1350 if (llvm::isa<clang::FunctionDecl>(decl)) {
1351 clang::NamedDecl *copied_decl =
1352 llvm::cast_or_null<FunctionDecl>(CopyDecl(decl));
1353 if (copied_decl) {
1354 context.AddNamedDecl(copied_decl);
1355 found_function_with_type_info = true;
1356 }
1357 }
1358 }
1359 }
1360
1361 if (!found_function_with_type_info) {
1362 if (extern_symbol) {
1363 AddOneFunction(context, nullptr, extern_symbol);
1364 } else if (non_extern_symbol) {
1365 AddOneFunction(context, nullptr, non_extern_symbol);
1366 }
1367 }
1368 }
1369
1370 return found_function_with_type_info;
1371}
1372
1374 NameSearchContext &context, lldb::ModuleSP module_sp,
1375 const CompilerDeclContext &namespace_decl) {
1376 assert(m_ast_context);
1377
1379
1380 const ConstString name(context.m_decl_name.getAsString());
1381 if (IgnoreName(name, false))
1382 return;
1383
1384 // Only look for functions by name out in our symbols if the function doesn't
1385 // start with our phony prefix of '$'
1386
1387 Target *target = nullptr;
1388 StackFrame *frame = nullptr;
1389 SymbolContext sym_ctx;
1390 if (m_parser_vars) {
1391 target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1392 frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1393 }
1394 if (frame != nullptr)
1395 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1396 lldb::eSymbolContextBlock);
1397
1398 // Try the persistent decls, which take precedence over all else.
1399 if (!namespace_decl)
1400 SearchPersistenDecls(context, name);
1401
1402 if (name.GetStringRef().starts_with("$") && !namespace_decl) {
1403 if (name == "$__lldb_class") {
1404 LookUpLldbClass(context);
1405 return;
1406 }
1407
1408 if (name == "$__lldb_objc_class") {
1409 LookUpLldbObjCClass(context);
1410 return;
1411 }
1413 LookupLocalVarNamespace(sym_ctx, context);
1414 return;
1415 }
1416
1417 // any other $__lldb names should be weeded out now
1418 if (name.GetStringRef().starts_with("$__lldb"))
1419 return;
1420
1421 // No ParserVars means we can't do register or variable lookup.
1422 if (!m_parser_vars || !m_parser_vars->m_persistent_vars)
1423 return;
1424
1425 ExpressionVariableSP pvar_sp(
1426 m_parser_vars->m_persistent_vars->GetVariable(name));
1427
1428 if (pvar_sp) {
1429 AddOneVariable(context, pvar_sp);
1430 return;
1431 }
1432
1433 assert(name.GetStringRef().starts_with("$"));
1434 llvm::StringRef reg_name = name.GetStringRef().substr(1);
1435
1436 if (m_parser_vars->m_exe_ctx.GetRegisterContext()) {
1437 const RegisterInfo *reg_info(
1438 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(
1439 reg_name));
1440
1441 if (reg_info) {
1442 LLDB_LOG(log, " CEDM::FEVD Found register {0}", reg_info->name);
1443
1444 AddOneRegister(context, reg_info);
1445 }
1446 }
1447 return;
1448 }
1449
1450 bool local_var_lookup = !namespace_decl || (namespace_decl.GetName() ==
1452 if (frame && local_var_lookup)
1453 if (LookupLocalVariable(context, name, sym_ctx, namespace_decl))
1454 return;
1455
1456 if (target) {
1457 ValueObjectSP valobj;
1458 VariableSP var;
1459 var = FindGlobalVariable(*target, module_sp, name, namespace_decl);
1460
1461 if (var) {
1462 valobj = ValueObjectVariable::Create(target, var);
1463 AddOneVariable(context, var, valobj);
1464 context.m_found_variable = true;
1465 return;
1466 }
1467 }
1468
1469 if (!LookupFunction(context, module_sp, name, namespace_decl))
1470 LookupInModulesDeclVendor(context, name);
1471
1472 if (target && !context.m_found_variable && !namespace_decl) {
1473 // We couldn't find a non-symbol variable for this. Now we'll hunt for a
1474 // generic data symbol, and -- if it is found -- treat it as a variable.
1475 Status error;
1476
1477 const Symbol *data_symbol =
1478 m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error);
1479
1480 if (!error.Success()) {
1481 const unsigned diag_id =
1482 m_ast_context->getDiagnostics().getCustomDiagID(
1483 clang::DiagnosticsEngine::Level::Error, "%0");
1484 m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString();
1485 }
1486
1487 if (data_symbol) {
1488 std::string warning("got name from symbols: ");
1489 warning.append(name.GetStringRef());
1490 const unsigned diag_id =
1491 m_ast_context->getDiagnostics().getCustomDiagID(
1492 clang::DiagnosticsEngine::Level::Warning, "%0");
1493 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
1494 AddOneGenericVariable(context, *data_symbol);
1495 context.m_found_variable = true;
1496 }
1497 }
1498}
1499
1501 lldb_private::Value &var_location,
1502 TypeFromUser *user_type,
1503 TypeFromParser *parser_type) {
1505
1506 Type *var_type = var->GetType();
1507
1508 if (!var_type) {
1509 LLDB_LOG(log, "Skipped a definition because it has no type");
1510 return false;
1511 }
1512
1513 CompilerType var_clang_type = var_type->GetFullCompilerType();
1514
1515 if (!var_clang_type) {
1516 LLDB_LOG(log, "Skipped a definition because it has no Clang type");
1517 return false;
1518 }
1519
1520 auto clang_ast =
1522
1523 if (!clang_ast) {
1524 LLDB_LOG(log, "Skipped a definition because it has no Clang AST");
1525 return false;
1526 }
1527
1528 DWARFExpressionList &var_location_list = var->LocationExpressionList();
1529
1530 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1531 Status err;
1532
1533 if (var->GetLocationIsConstantValueData()) {
1534 DataExtractor const_value_extractor;
1535 if (var_location_list.GetExpressionData(const_value_extractor)) {
1536 var_location = Value(const_value_extractor.GetDataStart(),
1537 const_value_extractor.GetByteSize());
1539 } else {
1540 LLDB_LOG(log, "Error evaluating constant variable: {0}", err.AsCString());
1541 return false;
1542 }
1543 }
1544
1545 CompilerType type_to_use = GuardedCopyType(var_clang_type);
1546
1547 if (!type_to_use) {
1548 LLDB_LOG(log,
1549 "Couldn't copy a variable's type into the parser's AST context");
1550
1551 return false;
1552 }
1553
1554 if (parser_type)
1555 *parser_type = TypeFromParser(type_to_use);
1556
1557 if (var_location.GetContextType() == Value::ContextType::Invalid)
1558 var_location.SetCompilerType(type_to_use);
1559
1560 if (var_location.GetValueType() == Value::ValueType::FileAddress) {
1561 SymbolContext var_sc;
1562 var->CalculateSymbolContext(&var_sc);
1563
1564 if (!var_sc.module_sp)
1565 return false;
1566
1567 Address so_addr(var_location.GetScalar().ULongLong(),
1568 var_sc.module_sp->GetSectionList());
1569
1570 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1571
1572 if (load_addr != LLDB_INVALID_ADDRESS) {
1573 var_location.GetScalar() = load_addr;
1575 }
1576 }
1577
1578 if (user_type)
1579 *user_type = TypeFromUser(var_clang_type);
1580
1581 return true;
1582}
1583
1586 TypeFromParser const &pt,
1587 ValueObjectSP valobj) {
1588 clang::QualType parser_opaque_type =
1589 QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1590
1591 if (parser_opaque_type.isNull())
1592 return nullptr;
1593
1594 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
1595 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1596 CompleteType(tag_type->getDecl()->getDefinitionOrSelf());
1597 if (const ObjCObjectPointerType *objc_object_ptr_type =
1598 dyn_cast<ObjCObjectPointerType>(parser_type))
1599 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1600 }
1601
1602 bool is_reference = pt.IsReferenceType();
1603
1604 NamedDecl *var_decl = nullptr;
1605 if (is_reference)
1606 var_decl = context.AddVarDecl(pt);
1607 else
1608 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1609
1610 std::string decl_name(context.m_decl_name.getAsString());
1611 ConstString entity_name(decl_name);
1613 m_found_entities.AddNewlyConstructedVariable(entity);
1614
1615 assert(entity);
1616 entity->EnableParserVars(GetParserID());
1618 entity->GetParserVars(GetParserID());
1619
1620 parser_vars->m_named_decl = var_decl;
1621
1622 if (is_reference)
1623 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1624
1625 return parser_vars;
1626}
1627
1629 NameSearchContext &context, ValueObjectSP valobj,
1630 ValueObjectProviderTy valobj_provider) {
1631 assert(m_parser_vars.get());
1632 assert(valobj);
1633
1635
1636 Value var_location = valobj->GetValue();
1637
1638 TypeFromUser user_type = valobj->GetCompilerType();
1639
1640 auto clang_ast = user_type.GetTypeSystem<TypeSystemClang>();
1641
1642 if (!clang_ast) {
1643 LLDB_LOG(log, "Skipped a definition because it has no Clang AST");
1644 return;
1645 }
1646
1647 TypeFromParser parser_type = GuardedCopyType(user_type);
1648
1649 if (!parser_type) {
1650 LLDB_LOG(log,
1651 "Couldn't copy a variable's type into the parser's AST context");
1652
1653 return;
1654 }
1655
1656 if (var_location.GetContextType() == Value::ContextType::Invalid)
1657 var_location.SetCompilerType(parser_type);
1658
1660 AddExpressionVariable(context, parser_type, valobj);
1661
1662 if (!parser_vars)
1663 return;
1664
1665 LLDB_LOG(log, " CEDM::FEVD Found variable {0}, returned\n{1} (original {2})",
1666 context.m_decl_name, ClangUtil::DumpDecl(parser_vars->m_named_decl),
1667 ClangUtil::ToString(user_type));
1668
1669 parser_vars->m_llvm_value = nullptr;
1670 parser_vars->m_lldb_value = std::move(var_location);
1671 parser_vars->m_lldb_valobj_provider = std::move(valobj_provider);
1672}
1673
1675 VariableSP var,
1676 ValueObjectSP valobj) {
1677 assert(m_parser_vars.get());
1678
1680
1681 TypeFromUser ut;
1682 TypeFromParser pt;
1683 Value var_location;
1684
1685 if (!GetVariableValue(var, var_location, &ut, &pt))
1686 return;
1687
1689 AddExpressionVariable(context, pt, std::move(valobj));
1690
1691 if (!parser_vars)
1692 return;
1693
1694 LLDB_LOG(log, " CEDM::FEVD Found variable {0}, returned\n{1} (original {2})",
1695 context.m_decl_name, ClangUtil::DumpDecl(parser_vars->m_named_decl),
1697
1698 parser_vars->m_llvm_value = nullptr;
1699 parser_vars->m_lldb_value = var_location;
1700 parser_vars->m_lldb_var = var;
1701}
1702
1704 ExpressionVariableSP &pvar_sp) {
1706
1707 TypeFromUser user_type(
1708 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
1709
1710 TypeFromParser parser_type(GuardedCopyType(user_type));
1711
1712 if (!parser_type.GetOpaqueQualType()) {
1713 LLDB_LOG(log, " CEDM::FEVD Couldn't import type for pvar {0}",
1714 pvar_sp->GetName());
1715 return;
1716 }
1717
1718 NamedDecl *var_decl =
1719 context.AddVarDecl(parser_type.GetLValueReferenceType());
1720
1721 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1722 ->EnableParserVars(GetParserID());
1724 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1725 ->GetParserVars(GetParserID());
1726 parser_vars->m_named_decl = var_decl;
1727 parser_vars->m_llvm_value = nullptr;
1728 parser_vars->m_lldb_value.Clear();
1729
1730 LLDB_LOG(log, " CEDM::FEVD Added pvar {0}, returned\n{1}",
1731 pvar_sp->GetName(), ClangUtil::DumpDecl(var_decl));
1732}
1733
1735 const Symbol &symbol) {
1736 assert(m_parser_vars.get());
1737
1739
1740 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1741
1742 if (target == nullptr)
1743 return;
1744
1745 auto scratch_ast_context = GetScratchContext(*target);
1746 if (!scratch_ast_context)
1747 return;
1748
1749 TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid)
1750 .GetPointerType()
1751 .GetLValueReferenceType());
1752 TypeFromParser parser_type(m_clang_ast_context->GetBasicType(eBasicTypeVoid)
1753 .GetPointerType()
1754 .GetLValueReferenceType());
1755 NamedDecl *var_decl = context.AddVarDecl(parser_type);
1756
1757 std::string decl_name(context.m_decl_name.getAsString());
1758 ConstString entity_name(decl_name);
1760 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name,
1761 user_type, m_parser_vars->m_target_info.byte_order,
1762 m_parser_vars->m_target_info.address_byte_size));
1763 m_found_entities.AddNewlyConstructedVariable(entity);
1764
1765 entity->EnableParserVars(GetParserID());
1767 entity->GetParserVars(GetParserID());
1768
1769 const Address symbol_address = symbol.GetAddress();
1770 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1771
1772 // parser_vars->m_lldb_value.SetContext(Value::ContextType::ClangType,
1773 // user_type.GetOpaqueQualType());
1774 parser_vars->m_lldb_value.SetCompilerType(user_type);
1775 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1777
1778 parser_vars->m_named_decl = var_decl;
1779 parser_vars->m_llvm_value = nullptr;
1780 parser_vars->m_lldb_sym = &symbol;
1781
1782 LLDB_LOG(log, " CEDM::FEVD Found variable {0}, returned\n{1}", decl_name,
1783 ClangUtil::DumpDecl(var_decl));
1784}
1785
1787 const RegisterInfo *reg_info) {
1789
1790 CompilerType clang_type =
1791 m_clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
1792 reg_info->encoding, reg_info->byte_size * 8);
1793
1794 if (!clang_type) {
1795 LLDB_LOG(log, " Tried to add a type for {0}, but couldn't get one",
1796 context.m_decl_name.getAsString());
1797 return;
1798 }
1799
1800 TypeFromParser parser_clang_type(clang_type);
1801
1802 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
1803
1805 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1806 m_parser_vars->m_target_info.byte_order,
1807 m_parser_vars->m_target_info.address_byte_size));
1808 m_found_entities.AddNewlyConstructedVariable(entity);
1809
1810 std::string decl_name(context.m_decl_name.getAsString());
1811 entity->SetName(decl_name);
1812 entity->SetRegisterInfo(reg_info);
1813 entity->EnableParserVars(GetParserID());
1815 entity->GetParserVars(GetParserID());
1816 parser_vars->m_named_decl = var_decl;
1817 parser_vars->m_llvm_value = nullptr;
1818 parser_vars->m_lldb_value.Clear();
1819 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
1820
1821 LLDB_LOG(log, " CEDM::FEVD Added register {0}, returned\n{1}",
1822 context.m_decl_name.getAsString(), ClangUtil::DumpDecl(var_decl));
1823}
1824
1826 Function *function,
1827 const Symbol *symbol) {
1828 assert(m_parser_vars.get());
1829
1831
1832 NamedDecl *function_decl = nullptr;
1833 Address fun_address;
1834 CompilerType function_clang_type;
1835
1836 bool is_indirect_function = false;
1837
1838 if (function) {
1839 Type *function_type = function->GetType();
1840
1841 const auto lang = function->GetCompileUnit()->GetLanguage();
1842 const llvm::StringRef name =
1843 function->GetMangled().GetMangledName().GetStringRef();
1844 const bool extern_c =
1846 (Language::LanguageIsObjC(lang) &&
1848
1849 if (!extern_c) {
1850 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem();
1851 if (llvm::isa<TypeSystemClang>(type_system)) {
1852 clang::DeclContext *src_decl_context =
1853 (clang::DeclContext *)function->GetDeclContext()
1855 clang::FunctionDecl *src_function_decl =
1856 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
1857 if (src_function_decl &&
1858 src_function_decl->getTemplateSpecializationInfo()) {
1859 clang::FunctionTemplateDecl *function_template =
1860 src_function_decl->getTemplateSpecializationInfo()->getTemplate();
1861 clang::FunctionTemplateDecl *copied_function_template =
1862 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
1863 CopyDecl(function_template));
1864 if (copied_function_template) {
1865 if (log) {
1866 StreamString ss;
1867
1868 function->DumpSymbolContext(&ss);
1869
1870 LLDB_LOG(log,
1871 " CEDM::FEVD Imported decl for function template"
1872 " {0} (description {1}), returned\n{2}",
1873 copied_function_template->getNameAsString(),
1874 ss.GetData(),
1875 ClangUtil::DumpDecl(copied_function_template));
1876 }
1877
1878 context.AddNamedDecl(copied_function_template);
1879 }
1880 } else if (src_function_decl) {
1881 if (clang::FunctionDecl *copied_function_decl =
1882 llvm::dyn_cast_or_null<clang::FunctionDecl>(
1883 CopyDecl(src_function_decl))) {
1884 if (log) {
1885 StreamString ss;
1886
1887 function->DumpSymbolContext(&ss);
1888
1889 LLDB_LOG(log,
1890 " CEDM::FEVD Imported decl for function {0} "
1891 "(description {1}), returned\n{2}",
1892 copied_function_decl->getNameAsString(), ss.GetData(),
1893 ClangUtil::DumpDecl(copied_function_decl));
1894 }
1895
1896 context.AddNamedDecl(copied_function_decl);
1897 return;
1898 } else {
1899 LLDB_LOG(log, " Failed to import the function decl for '{0}'",
1900 src_function_decl->getName());
1901 }
1902 }
1903 }
1904 }
1905
1906 if (!function_type) {
1907 LLDB_LOG(log, " Skipped a function because it has no type");
1908 return;
1909 }
1910
1911 function_clang_type = function_type->GetFullCompilerType();
1912
1913 if (!function_clang_type) {
1914 LLDB_LOG(log, " Skipped a function because it has no Clang type");
1915 return;
1916 }
1917
1918 fun_address = function->GetAddress();
1919
1920 CompilerType copied_function_type = GuardedCopyType(function_clang_type);
1921 if (copied_function_type) {
1922 function_decl = context.AddFunDecl(copied_function_type, extern_c);
1923
1924 if (!function_decl) {
1925 LLDB_LOG(log, " Failed to create a function decl for '{0}' ({1:x})",
1926 function_type->GetName(), function_type->GetID());
1927
1928 return;
1929 }
1930 } else {
1931 // We failed to copy the type we found
1932 LLDB_LOG(log,
1933 " Failed to import the function type '{0}' ({1:x})"
1934 " into the expression parser AST context",
1935 function_type->GetName(), function_type->GetID());
1936
1937 return;
1938 }
1939 } else if (symbol) {
1940 fun_address = symbol->GetAddress();
1941 function_decl = context.AddGenericFunDecl();
1942 is_indirect_function = symbol->IsIndirect();
1943 } else {
1944 LLDB_LOG(log, " AddOneFunction called with no function and no symbol");
1945 return;
1946 }
1947
1948 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1949
1950 lldb::addr_t load_addr =
1951 fun_address.GetCallableLoadAddress(target, is_indirect_function);
1952
1954 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1955 m_parser_vars->m_target_info.byte_order,
1956 m_parser_vars->m_target_info.address_byte_size));
1957 m_found_entities.AddNewlyConstructedVariable(entity);
1958
1959 std::string decl_name(context.m_decl_name.getAsString());
1960 entity->SetName(decl_name);
1961 entity->SetCompilerType(function_clang_type);
1962 entity->EnableParserVars(GetParserID());
1963
1965 entity->GetParserVars(GetParserID());
1966
1967 if (load_addr != LLDB_INVALID_ADDRESS) {
1969 parser_vars->m_lldb_value.GetScalar() = load_addr;
1970 } else {
1971 // We have to try finding a file address.
1972
1973 lldb::addr_t file_addr = fun_address.GetFileAddress();
1974
1976 parser_vars->m_lldb_value.GetScalar() = file_addr;
1977 }
1978
1979 parser_vars->m_named_decl = function_decl;
1980 parser_vars->m_llvm_value = nullptr;
1981
1982 if (log) {
1983 StreamString ss;
1984
1985 fun_address.Dump(&ss,
1986 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1988
1989 LLDB_LOG(log,
1990 " CEDM::FEVD Found {0} function {1} (description {2}), "
1991 "returned\n{3}",
1992 (function ? "specific" : "generic"), decl_name, ss.GetData(),
1993 ClangUtil::DumpDecl(function_decl));
1994 }
1995}
1996
1998 const TypeFromUser &ut) {
1999 CompilerType copied_clang_type = GuardedCopyType(ut);
2000
2002
2003 if (!copied_clang_type) {
2004 LLDB_LOG(log,
2005 "ClangExpressionDeclMap::AddThisType - Couldn't import the type");
2006
2007 return;
2008 }
2009
2010 if (copied_clang_type.IsAggregateType() &&
2011 copied_clang_type.GetCompleteType()) {
2012 CompilerType void_clang_type =
2013 m_clang_ast_context->GetBasicType(eBasicTypeVoid);
2014 std::array<CompilerType, 1> args{void_clang_type.GetPointerType()};
2015
2016 CompilerType method_type = m_clang_ast_context->CreateFunctionType(
2017 void_clang_type, args, false,
2019
2020 const bool is_virtual = false;
2021 const bool is_static = false;
2022 const bool is_inline = false;
2023 const bool is_explicit = false;
2024 const bool is_attr_used = true;
2025 const bool is_artificial = false;
2026
2027 CXXMethodDecl *method_decl = m_clang_ast_context->AddMethodToCXXRecordType(
2028 copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", /*asm_label=*/{},
2029 method_type, is_virtual, is_static, is_inline, is_explicit,
2030 is_attr_used, is_artificial);
2031
2032 LLDB_LOG(log,
2033 " CEDM::AddThisType Added function $__lldb_expr "
2034 "(description {0}) for this type\n{1}",
2035 ClangUtil::ToString(copied_clang_type),
2036 ClangUtil::DumpDecl(method_decl));
2037 }
2038
2039 if (!copied_clang_type.IsValid())
2040 return;
2041
2042 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(
2043 QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType()));
2044
2045 if (!type_source_info)
2046 return;
2047
2048 // Construct a typedef type because if "*this" is a templated type we can't
2049 // just return ClassTemplateSpecializationDecls in response to name queries.
2050 // Using a typedef makes this much more robust.
2051
2052 TypedefDecl *typedef_decl = TypedefDecl::Create(
2053 *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(),
2054 SourceLocation(), context.m_decl_name.getAsIdentifierInfo(),
2055 type_source_info);
2056
2057 if (!typedef_decl)
2058 return;
2059
2060 context.AddNamedDecl(typedef_decl);
2061}
2062
2064 const TypeFromUser &ut) {
2065 CompilerType copied_clang_type = GuardedCopyType(ut);
2066
2067 if (!copied_clang_type) {
2069
2070 LLDB_LOG(log,
2071 "ClangExpressionDeclMap::AddOneType - Couldn't import the type");
2072
2073 return;
2074 }
2075
2076 context.AddTypeDecl(copied_clang_type);
2077}
static const char * g_lldb_local_vars_namespace_cstr
static llvm::raw_ostream & error(Stream &strm)
static llvm::raw_ostream & warning(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
#define LLDB_LOG_VERBOSE(log,...)
Definition Log.h:382
#define LLDB_INVALID_DECL_LEVEL
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:303
lldb::addr_t GetCallableLoadAddress(Target *target, bool is_indirect=false) const
Get the load address as a callable code load address.
Definition Address.cpp:328
@ DumpStyleResolvedDescription
Display the details about what an address resolves to.
Definition Address.h:104
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX, bool all_ranges=false, std::optional< Stream::HighlightSettings > settings=std::nullopt) const
Dump a description of this object to a Stream.
Definition Address.cpp:398
lldb::addr_t GetFileAddress() const
Get the file address.
Definition Address.cpp:283
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition ArchSpec.cpp:710
lldb::ByteOrder GetByteOrder() const
Returns the byte order for the architecture specification.
Definition ArchSpec.cpp:759
A class that describes a single lexical block.
Definition Block.h:41
CompilerDeclContext GetDeclContext()
Definition Block.cpp:463
std::shared_ptr< NamespaceMap > NamespaceMapSP
std::pair< lldb::ModuleSP, CompilerDeclContext > NamespaceMapItem
clang::ASTContext * m_ast_context
The AST context requests are coming in for.
ClangASTSource(const lldb::TargetSP &target, const std::shared_ptr< ClangASTImporter > &importer)
Constructor.
clang::Decl * CopyDecl(clang::Decl *src_decl)
Copies a single Decl into the parser's AST context.
bool IgnoreName(const ConstString name, bool ignore_all_dollar_names)
Returns true if a name should be ignored by name lookup.
virtual void FindExternalVisibleDecls(NameSearchContext &context)
The worker function for FindExternalVisibleDeclsByName.
std::shared_ptr< ClangModulesDeclVendor > GetClangModulesDeclVendor()
void CompleteType(clang::TagDecl *Tag) override
Complete a TagDecl.
CompilerType GuardedCopyType(const CompilerType &src_type)
A wrapper for TypeSystemClang::CopyType that sets a flag that indicates that we should not respond to...
std::shared_ptr< ClangASTImporter > m_ast_importer_sp
The target's AST importer.
TypeSystemClang * m_clang_ast_context
The TypeSystemClang for m_ast_context.
const lldb::TargetSP m_target
The target to use in finding variables and types.
bool GetVariableValue(lldb::VariableSP &var, lldb_private::Value &var_location, TypeFromUser *found_type=nullptr, TypeFromParser *parser_type=nullptr)
Get the value of a variable in a given execution context and return the associated Types if needed.
ClangExpressionVariable::ParserVars * AddExpressionVariable(NameSearchContext &context, TypeFromParser const &pt, lldb::ValueObjectSP valobj)
Use the NameSearchContext to generate a Decl for the given LLDB ValueObject, and put it in the list o...
std::unique_ptr< StructVars > m_struct_vars
void EnableParserVars()
Activate parser-specific variables.
uint64_t GetParserID()
Get this parser's ID for use in extracting parser- and JIT-specific data from persistent variables.
bool AddPersistentVariable(const clang::NamedDecl *decl, ConstString name, TypeFromParser type, bool is_result, bool is_lvalue)
[Used by IRForTarget] Add a variable to the list of persistent variables for the process.
bool LookupLocalVariable(NameSearchContext &context, ConstString name, SymbolContext &sym_ctx, const CompilerDeclContext &namespace_decl)
Looks up a local variable.
void DisableStructVars()
Deallocate struct variables.
void AddOneVariable(NameSearchContext &context, lldb::VariableSP var, lldb::ValueObjectSP valobj)
Use the NameSearchContext to generate a Decl for the given LLDB Variable, and put it in the Tuple lis...
void FindExternalVisibleDecls(NameSearchContext &context) override
[Used by ClangASTSource] Find all entities matching a given name, using a NameSearchContext to make D...
bool LookupFunction(NameSearchContext &context, lldb::ModuleSP module_sp, ConstString name, const CompilerDeclContext &namespace_decl)
Looks up a function.
bool GetStructElement(const clang::NamedDecl *&decl, llvm::Value *&value, lldb::offset_t &offset, ConstString &name, uint32_t index)
[Used by IRForTarget] Get specific information about one field of the laid-out struct after DoStructL...
bool DoStructLayout()
[Used by IRForTarget] Finalize the struct, laying out the position of each object in it.
void MaybeRegisterFunctionBody(clang::FunctionDecl *copied_function_decl)
Should be called on all copied functions.
void EnableStructVars()
Activate struct variables.
void AddOneGenericVariable(NameSearchContext &context, const Symbol &symbol)
Use the NameSearchContext to generate a Decl for the given LLDB symbol (treated as a variable),...
void DidParse()
Disable the state needed for parsing and IR transformation.
void AddOneRegister(NameSearchContext &context, const RegisterInfo *reg_info)
Use the NameSearchContext to generate a Decl for the given register.
bool WillParse(ExecutionContext &exe_ctx, Materializer *materializer)
Enable the state needed for parsing and IR transformation.
void LookUpLldbObjCClass(NameSearchContext &context)
Handles looking up $__lldb_objc_class which requires special treatment.
void DisableParserVars()
Deallocate parser-specific variables.
void LookupLocalVarNamespace(SymbolContext &sym_ctx, NameSearchContext &name_context)
Handles looking up the synthetic namespace that contains our local variables for the current frame.
bool AddValueToStruct(const clang::NamedDecl *decl, ConstString name, llvm::Value *value, size_t size, lldb::offset_t alignment)
[Used by IRForTarget] Add a variable to the struct that needs to be materialized each time the expres...
lldb::addr_t GetSymbolAddress(Target &target, Process *process, ConstString name, lldb::SymbolType symbol_type, Module *module=nullptr)
[Used by IRForTarget] Get the address of a symbol given nothing but its name.
void InstallDiagnosticManager(DiagnosticManager &diag_manager)
void LookUpLldbClass(NameSearchContext &context)
Handles looking up $__lldb_class which requires special treatment.
bool m_keep_result_in_memory
True if result persistent variables generated by this expression should stay in memory.
bool GetFunctionInfo(const clang::NamedDecl *decl, uint64_t &ptr)
[Used by IRForTarget] Get information about a function given its Decl.
lldb::TypeSystemClangSP GetScratchContext(Target &target)
void AddContextClassType(NameSearchContext &context, const TypeFromUser &type)
Adds the class in which the expression is evaluated to the lookup and prepares the class to be used a...
bool GetStructInfo(uint32_t &num_elements, size_t &size, lldb::offset_t &alignment)
[Used by IRForTarget] Get general information about the laid-out struct after DoStructLayout() has be...
void AddOneFunction(NameSearchContext &context, Function *fun, const Symbol *sym)
Use the NameSearchContext to generate a Decl for the given function.
SymbolContextList SearchFunctionsInSymbolContexts(const SymbolContextList &sc_list, const CompilerDeclContext &frame_decl_context)
Searches for functions in the given SymbolContextList.
ExpressionVariableList m_struct_members
All entities that need to be placed in the struct.
ExpressionVariableList m_found_entities
All entities that were looked up for the parser.
void SearchPersistenDecls(NameSearchContext &context, const ConstString name)
Searches the persistent decls of the target for entities with the given name.
TypeFromUser DeportType(TypeSystemClang &target, TypeSystemClang &source, TypeFromParser parser_type)
Move a type out of the current ASTContext into another, but make sure to export all components of the...
Materializer::PersistentVariableDelegate * m_result_delegate
If non-NULL, used to report expression results to ClangUserExpression.
void LookupInModulesDeclVendor(NameSearchContext &context, ConstString name)
Lookup entities in the ClangModulesDeclVendor.
void AddOneType(NameSearchContext &context, const TypeFromUser &type)
Use the NameSearchContext to generate a Decl for the given type.
ValueObject * m_ctx_obj
If not empty, then expression is evaluated in context of this object.
lldb::VariableSP FindGlobalVariable(Target &target, lldb::ModuleSP &module, ConstString name, const CompilerDeclContext &namespace_decl)
Given a target, find a variable that matches the given name and type.
ClangExpressionDeclMap(bool keep_result_in_memory, Materializer::PersistentVariableDelegate *result_delegate, const lldb::TargetSP &target, const std::shared_ptr< ClangASTImporter > &importer, ValueObject *ctx_obj, bool ignore_context_qualifiers)
Constructor.
std::unique_ptr< ParserVars > m_parser_vars
void InstallCodeGenerator(clang::ASTConsumer *code_gen)
bool m_ignore_context_qualifiers
If true, evaluates the expression without taking into account the CV-qualifiers of the scope.
virtual clang::NamedDecl * GetPersistentDecl(ConstString name)
Retrieves the declaration with the given name from the storage of persistent declarations.
The following values should not live beyond parsing.
ValueObjectProviderTy m_lldb_valobj_provider
Callback that provides a ValueObject for the specified frame.
const lldb_private::Symbol * m_lldb_sym
The original symbol for this variable, if it was a symbol.
lldb_private::Value m_lldb_value
The value found in LLDB for this variable.
lldb::VariableSP m_lldb_var
The original variable for this variable.
llvm::Value * m_llvm_value
The IR value corresponding to this variable; usually a GlobalValue.
const clang::NamedDecl * m_named_decl
The Decl corresponding to this variable.
"lldb/Expression/ClangExpressionVariable.h" Encapsulates one variable for the expression parser.
static ClangExpressionVariable * FindVariableInList(ExpressionVariableList &list, const clang::NamedDecl *decl, uint64_t parser_id)
Utility functions for dealing with ExpressionVariableLists in Clang- specific ways.
ParserVars * GetParserVars(uint64_t parser_id)
Access parser-specific variables.
void EnableJITVars(uint64_t parser_id)
Make this variable usable for materializing for the JIT by allocating space for JIT-specific variable...
void EnableParserVars(uint64_t parser_id)
Make this variable usable by the parser by allocating space for parser- specific variables.
lldb::LanguageType GetLanguage()
Represents a generic declaration context in a program.
std::vector< CompilerDecl > FindDeclByName(ConstString name, const bool ignore_using_decls)
bool IsClassMethod()
Checks if this decl context represents a method of a class.
Represents a generic declaration such as a function declaration.
Generic representation of a type in a programming language.
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
lldb::opaque_compiler_type_t GetOpaqueQualType() const
CompilerType GetLValueReferenceType() const
Return a new CompilerType that is a L value reference to this type if this type is valid and the type...
bool IsReferenceType(CompilerType *pointee_type=nullptr, bool *is_rvalue=nullptr) const
unsigned GetTypeQualifiers() const
CompilerType GetPointeeType() const
If this type is a pointer type, return the type that the pointer points to, else return an invalid ty...
bool GetCompleteType() const
Type Completion.
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
"lldb/Expression/DWARFExpressionList.h" Encapsulates a range map from file address range to a single ...
bool GetExpressionData(DataExtractor &data, lldb::addr_t func_load_addr=LLDB_INVALID_ADDRESS, lldb::addr_t file_addr=0) const
Get the expression data at the file address.
An data extractor class.
virtual uint64_t GetByteSize() const
Get the number of bytes contained in this object.
const uint8_t * GetDataStart() const
Get the data start pointer.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
ExecutionContextScope * GetBestExecutionContextScope() const
const lldb::TargetSP & GetTargetSP() const
Get accessor to get the target shared pointer.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
Thread * GetThreadPtr() const
Returns a pointer to the thread object.
void ClearDirectory()
Clear the directory in this object.
Definition FileSpec.cpp:373
A class that describes a function.
Definition Function.h:386
const Address & GetAddress() const
Return the address of the function (its entry point).
Definition Function.h:439
ConstString GetName() const
Definition Function.cpp:723
const Mangled & GetMangled() const
Definition Function.h:541
void DumpSymbolContext(Stream *s) override
Dump the object's symbol context to the stream s.
Definition Function.cpp:520
Type * GetType()
Get accessor for the type that describes the function return value type, and parameter types.
Definition Function.cpp:563
CompilerDeclContext GetDeclContext()
Get the DeclContext for this function, if available.
Definition Function.cpp:549
CompileUnit * GetCompileUnit()
Get accessor for the compile unit that owns this function.
Definition Function.cpp:415
virtual lldb::addr_t LookupRuntimeSymbol(llvm::StringRef name)
static bool LanguageIsC(lldb::LanguageType language)
Definition Language.cpp:354
static bool LanguageIsCPlusPlus(lldb::LanguageType language)
Definition Language.cpp:329
static bool LanguageIsObjC(lldb::LanguageType language)
Definition Language.cpp:344
static bool IsMangledName(llvm::StringRef name)
Definition Mangled.cpp:178
ConstString GetMangledName() const
Mangled name get accessor.
Definition Mangled.h:169
Materializer packs the variables an expression reads and writes into a single argument struct,...
void FindGlobalVariables(llvm::StringRef name, size_t max_matches, VariableList &variable_list) const
Find global and static variables by name.
lldb::ModuleSP FindFirstModule(const ModuleSpec &module_spec) const
Finds the first module whose file specification matches module_spec.
void FindSymbolsWithNameAndType(llvm::StringRef name, lldb::SymbolType symbol_type, SymbolContextList &sc_list) const
void FindFunctions(llvm::StringRef name, lldb::FunctionNameType name_type_mask, const ModuleFunctionSearchOptions &options, SymbolContextList &sc_list) const
FileSpec & GetPlatformFileSpec()
Definition ModuleSpec.h:69
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:92
static ObjCLanguageRuntime * Get(Process &process)
A plug-in interface definition class for debugging a process.
Definition Process.h:370
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:4090
uint32_t GetAddressByteSize() const
Definition Process.cpp:4094
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:366
static lldb::TypeSystemClangSP GetForTarget(Target &target, std::optional< IsolatedASTKind > ast_kind=DefaultAST, bool create_on_demand=true)
Returns the scratch TypeSystemClang for the given target.
This base class provides an interface to stack frames.
Definition StackFrame.h:44
virtual VariableList * GetVariableList(bool get_file_globals, bool include_synthetic_vars, Status *error_ptr)
Retrieve the list of variables whose scope either:
virtual const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
virtual lldb::VariableListSP GetInScopeVariableList(bool get_file_globals, bool include_synthetic_vars=true, bool must_have_valid_location=false)
Retrieve the list of variables that are in scope at this StackFrame's pc.
virtual lldb::ValueObjectSP FindVariable(ConstString name)
Attempt to reconstruct the ValueObject for a variable with a given name from within the current Stack...
An error handling class.
Definition Status.h:118
bool Fail() const
Test for error condition.
Definition Status.cpp:293
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition Status.cpp:194
bool Success() const
Test for success condition.
Definition Status.cpp:303
const char * GetData() const
Defines a list of symbol context objects.
uint32_t GetSize() const
Get accessor for a symbol context list size.
void Append(const SymbolContext &sc)
Append a new symbol context to the list.
Defines a symbol context baton that can be handed other debug core functions.
Block * GetFunctionBlock()
Find a block that defines the function represented by this symbol context.
Block * block
The Block for a given query.
lldb::ModuleSP module_sp
The Module for a given query.
bool IsExternal() const
Definition Symbol.h:225
bool IsIndirect() const
Definition Symbol.cpp:250
lldb::SymbolType GetType() const
Definition Symbol.h:197
Address GetAddress() const
Definition Symbol.h:98
Symbol * ResolveReExportedSymbol(Target &target, const lldb::ModuleSP &containing_module_sp=lldb::ModuleSP()) const
Find the symbol this re-exported symbol resolves to.
Definition Symbol.cpp:536
PersistentExpressionState * GetPersistentExpressionStateForLanguage(lldb::LanguageType language)
Definition Target.cpp:2865
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1295
const ArchSpec & GetArchitecture() const
Definition Target.h:1337
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition Thread.h:451
A TypeSystem implementation based on Clang.
static clang::CXXMethodDecl * DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc)
uint32_t CountDeclLevels(clang::DeclContext *frame_decl_ctx, clang::DeclContext *child_decl_ctx, ConstString *child_name=nullptr, CompilerType *child_type=nullptr)
static clang::ObjCMethodDecl * DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc)
static bool IsObjCClassType(const CompilerType &type)
clang::ASTContext & getASTContext() const
Returns the clang::ASTContext instance managed by this TypeSystemClang.
static bool IsObjCObjectPointerType(const CompilerType &type, CompilerType *target_type=nullptr)
Interface for representing a type system.
Definition TypeSystem.h:72
CompilerType GetForwardCompilerType()
ConstString GetName()
CompilerType GetFullCompilerType()
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
Definition Value.h:114
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
Definition Value.h:53
@ FileAddress
A file address value.
Definition Value.h:48
@ LoadAddress
A load address value.
Definition Value.h:50
ValueType GetValueType() const
Definition Value.cpp:112
void SetCompilerType(const CompilerType &compiler_type)
Definition Value.cpp:277
void SetValueType(ValueType value_type)
Definition Value.h:90
ContextType GetContextType() const
Definition Value.h:88
lldb::VariableSP GetVariableAtIndex(size_t idx) const
lldb::VariableSP FindVariable(ConstString name, bool include_static_members=true) const
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
lldb::ValueObjectSP GetLambdaValueObject(StackFrame *frame)
Returns a ValueObject for the lambda class in the current frame.
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
std::function< lldb::ValueObjectSP(ConstString, StackFrame *)> ValueObjectProviderTy
Functor that returns a ValueObjectSP for a variable given its name and the StackFrame of interest.
TaggedASTType< 0 > TypeFromParser
TaggedASTType< 1 > TypeFromUser
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::ExpressionVariable > ExpressionVariableSP
uint64_t offset_t
Definition lldb-types.h:86
@ eLanguageTypeC
Non-standardized C, such as K&R.
SymbolType
Symbol types.
@ eSymbolTypeUndefined
@ eSymbolTypeVariableType
@ eSymbolTypeObjCMetaClass
@ eSymbolTypeReExported
@ eSymbolTypeObjCClass
@ eSymbolTypeObjectFile
@ eSymbolTypeTrampoline
@ eSymbolTypeResolver
@ eSymbolTypeSourceFile
@ eSymbolTypeException
@ eSymbolTypeVariable
@ eSymbolTypeASTFile
The symbol's name is the path to a compiler AST file.
@ eSymbolTypeAbsolute
@ eSymbolTypeAdditional
When symbols take more than one entry, the extra entries get this type.
@ eSymbolTypeInstrumentation
@ eSymbolTypeHeaderFile
@ eSymbolTypeCommonBlock
@ eSymbolTypeCompiler
@ eSymbolTypeLineHeader
@ eSymbolTypeObjCIVar
@ eSymbolTypeLineEntry
@ eSymbolTypeScopeBegin
@ eSymbolTypeScopeEnd
std::shared_ptr< lldb_private::VariableList > VariableListSP
std::shared_ptr< lldb_private::Variable > VariableSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP
The following values are valid if the variable is used by JIT code.
size_t m_size
The space required for the variable, in bytes.
lldb::offset_t m_alignment
The required alignment of the variable, in bytes.
lldb::offset_t m_offset
The offset of the variable in the struct, in bytes.
static clang::QualType GetQualType(const CompilerType &ct)
Definition ClangUtil.cpp:36
static std::string ToString(const clang::Type *t)
Returns a textual representation of the given type.
Definition ClangUtil.cpp:80
static std::string DumpDecl(const clang::Decl *d)
Returns a textual representation of the given Decl's AST.
Definition ClangUtil.cpp:68
static clang::Decl * GetDecl(const CompilerDecl &decl)
Returns the clang::Decl of the given CompilerDecl.
Definition ClangUtil.cpp:31
Options used by Module::FindFunctions.
Definition Module.h:68
bool include_inlines
Include inlined functions.
Definition Module.h:72
bool include_symbols
Include the symbol table.
Definition Module.h:70
clang::NamedDecl * AddTypeDecl(const CompilerType &compiler_type)
Create a TypeDecl with the name being searched for and the provided type and register it in the right...
const clang::DeclarationName m_decl_name
The name being looked for.
clang::NamedDecl * AddFunDecl(const CompilerType &type, bool extern_c=false)
Create a FunDecl with the name being searched for and the provided type and register it in the right ...
clang::NamedDecl * AddGenericFunDecl()
Create a FunDecl with the name being searched for and generic type (i.e.
const clang::DeclContext * m_decl_context
The DeclContext to put declarations into.
void AddNamedDecl(clang::NamedDecl *decl)
Add a NamedDecl to the list of results.
clang::NamedDecl * AddVarDecl(const CompilerType &type)
Create a VarDecl with the name being searched for and the provided type and register it in the right ...
Every register is described in detail including its name, alternate name (optional),...
lldb::Encoding encoding
Encoding of the register bits.
uint32_t byte_size
Size in bytes of the register.
const char * name
Name of this register, can't be NULL.
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition UserID.h:47