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:
606 symbol_load_addr = sym_address.GetLoadAddress(&target);
607 break;
608 }
609 }
610
611 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process) {
613
614 if (runtime) {
615 symbol_load_addr = runtime->LookupRuntimeSymbol(name);
616 }
617 }
618
619 return symbol_load_addr;
620}
621
623 lldb::SymbolType symbol_type) {
624 assert(m_parser_vars.get());
625
626 if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
628
629 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(),
630 m_parser_vars->m_exe_ctx.GetProcessPtr(), name,
631 symbol_type);
632}
633
635 Target &target, ModuleSP &module, ConstString name,
636 const CompilerDeclContext &namespace_decl) {
637 VariableList vars;
638
639 if (module && namespace_decl)
640 module->FindGlobalVariables(name, namespace_decl, -1, vars);
641 else
642 target.GetImages().FindGlobalVariables(name, -1, vars);
643
644 if (vars.GetSize() == 0)
645 return VariableSP();
646 return vars.GetVariableAtIndex(0);
647}
648
650 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
651 if (frame == nullptr)
652 return nullptr;
653
654 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
655 lldb::eSymbolContextBlock);
656 if (sym_ctx.block == nullptr)
657 return nullptr;
658
659 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
660 if (!frame_decl_context)
661 return nullptr;
662
663 return llvm::dyn_cast_or_null<TypeSystemClang>(
664 frame_decl_context.GetTypeSystem());
665}
666
667// Interface for ClangASTSource
668
670 NameSearchContext &context) {
671 assert(m_ast_context);
672
673 const auto name = context.m_decl_name.getAsString();
674
676
677 if (log) {
678 if (!context.m_decl_context)
679 LLDB_LOG(log,
680 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
681 "'{0}' in a NULL DeclContext",
682 name);
683 else if (const NamedDecl *context_named_decl =
684 dyn_cast<NamedDecl>(context.m_decl_context))
685 LLDB_LOG(log,
686 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
687 "'{0}' in '{1}'",
688 name, context_named_decl->getNameAsString());
689 else
690 LLDB_LOG(log,
691 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
692 "'{0}' in a '{1}'",
693 name, context.m_decl_context->getDeclKindName());
694 }
695
696 if (const NamespaceDecl *namespace_context =
697 dyn_cast<NamespaceDecl>(context.m_decl_context)) {
698 if (namespace_context->getName() == g_lldb_local_vars_namespace_cstr) {
699 CompilerDeclContext compiler_decl_ctx =
700 m_clang_ast_context->CreateDeclContext(
701 const_cast<clang::DeclContext *>(context.m_decl_context));
702 FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx);
703 return;
704 }
705
707 m_ast_importer_sp->GetNamespaceMap(namespace_context);
708
709 if (!namespace_map)
710 return;
711
713 log, " CEDM::FEVD Inspecting (NamespaceMap*){0:x} ({1} entries)",
714 namespace_map.get(), namespace_map->size());
715
716 for (ClangASTImporter::NamespaceMapItem &n : *namespace_map) {
717 LLDB_LOG(log, " CEDM::FEVD Searching namespace {0} in module {1}",
718 n.second.GetName(), n.first->GetFileSpec().GetFilename());
719
720 FindExternalVisibleDecls(context, n.first, n.second);
721 }
722 } else if (isa<TranslationUnitDecl>(context.m_decl_context)) {
723 CompilerDeclContext namespace_decl;
724
725 LLDB_LOG(log, " CEDM::FEVD Searching the root namespace");
726
727 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl);
728 }
729
731}
732
734 FunctionDecl *copied_function_decl) {
735 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) {
736 clang::DeclGroupRef decl_group_ref(copied_function_decl);
737 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
738 }
739}
740
742 if (!m_parser_vars)
743 return nullptr;
744 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
745 if (!target)
746 return nullptr;
747
749
750 if (!m_parser_vars->m_persistent_vars)
751 return nullptr;
752 return m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
753}
754
756 const ConstString name) {
758
759 NamedDecl *persistent_decl = GetPersistentDecl(name);
760
761 if (!persistent_decl)
762 return;
763
764 Decl *parser_persistent_decl = CopyDecl(persistent_decl);
765
766 if (!parser_persistent_decl)
767 return;
768
769 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
770
771 if (!parser_named_decl)
772 return;
773
774 if (clang::FunctionDecl *parser_function_decl =
775 llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
776 MaybeRegisterFunctionBody(parser_function_decl);
777 }
778
779 LLDB_LOG(log, " CEDM::FEVD Found persistent decl {0}", name);
780
781 context.AddNamedDecl(parser_named_decl);
782}
783
786
787 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
788 SymbolContext sym_ctx;
789 if (frame != nullptr)
790 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
791 lldb::eSymbolContextBlock);
792
793 // FIXME: Currently m_ctx_obj is only used through
794 // SBValue::EvaluateExpression. Can we instead *always* use m_ctx_obj
795 // regardless of which EvaluateExpression path we go through? Then we wouldn't
796 // need two separate code-paths here.
797 if (m_ctx_obj) {
798 Status status;
799 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
800 if (!ctx_obj_ptr || status.Fail())
801 return;
802
803 AddContextClassType(context, TypeFromUser(m_ctx_obj->GetCompilerType()));
804 return;
805 }
806
807 // Clang is looking for the type of "this"
808
809 if (frame == nullptr)
810 return;
811
812 // Find the block that defines the function represented by "sym_ctx"
813 Block *function_block = sym_ctx.GetFunctionBlock();
814
815 if (!function_block)
816 return;
817
818 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
819
820 if (!function_decl_ctx)
821 return;
822
823 clang::CXXMethodDecl *method_decl =
825
826 if (method_decl) {
827 if (auto capturedThis = GetCapturedThisValueObject(frame)) {
828 // We're inside a lambda and we captured a 'this'.
829 // Import the outer class's AST instead of the
830 // (unnamed) lambda structure AST so unqualified
831 // member lookups are understood by the Clang parser.
832 //
833 // If we're in a lambda which didn't capture 'this',
834 // $__lldb_class will correspond to the lambda closure
835 // AST and references to captures will resolve like
836 // regular member varaiable accesses do.
837 TypeFromUser pointee_type =
838 capturedThis->GetCompilerType().GetPointeeType();
839
840 LLDB_LOG(log,
841 " CEDM::FEVD Adding captured type ({0} for"
842 " $__lldb_class: {1}",
843 capturedThis->GetTypeName(), capturedThis->GetName());
844
845 AddContextClassType(context, pointee_type);
846 return;
847 }
848
849 clang::CXXRecordDecl *class_decl = method_decl->getParent();
850
851 QualType class_qual_type = m_ast_context->getCanonicalTagType(class_decl);
852
853 // The synthesized __lldb_expr will adopt the qualifiers from this class
854 // type. Make sure we use the qualifiers of the method that we're currently
855 // stopped in.
856 class_qual_type.addFastQualifiers(
857 method_decl->getMethodQualifiers().getFastQualifiers());
858
859 TypeFromUser class_user_type(
860 class_qual_type.getAsOpaquePtr(),
861 function_decl_ctx.GetTypeSystem()->weak_from_this());
862
863 LLDB_LOG(log, " CEDM::FEVD Adding type for $__lldb_class: {0}",
864 class_qual_type.getAsString());
865
866 AddContextClassType(context, class_user_type);
867 return;
868 }
869
870 // FIXME: this code is supposed to handl cases where a function decl
871 // was not attached to a class scope but its DIE had a `DW_AT_object_pointer`
872 // (and thus has a local `this` variable). This isn't a tested flow and
873 // even -flimit-debug-info doesn't seem to generate DWARF like that, so
874 // we should get rid of this code-path. An alternative fix if we ever
875 // encounter such DWARF is for the TypeSystem to attach the function
876 // to some valid class context (we can derive the type of the context
877 // through the `this` pointer anyway.
878 //
879 // The actual reason we can't remove this code is that LLDB currently
880 // creates decls for function templates by attaching them to the TU instead
881 // of a class context. So we can actually have template methods scoped
882 // outside of a class. Once we fix that, we can remove this code-path.
883 // Additionally, we exclude synthetic variables from here. Clang-based
884 // languages are unlikely candidates for synthetic variables anyway, and
885 // especially in this case, we're looking for something specific to C++.
886 VariableList *vars = frame->GetVariableList(
887 /*get_file_globals=*/false, /*include_synthetic_vars=*/false, nullptr);
888
889 lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
890
891 if (this_var && this_var->IsInScope(frame) &&
892 this_var->LocationIsValidForFrame(frame)) {
893 Type *this_type = this_var->GetType();
894
895 if (!this_type)
896 return;
897
898 TypeFromUser pointee_type =
900
901 LLDB_LOG(log, " FEVD Adding type for $__lldb_class: {0}",
902 ClangUtil::GetQualType(pointee_type).getAsString());
903
904 AddContextClassType(context, pointee_type);
905 }
906}
907
910
911 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
912
913 if (m_ctx_obj) {
914 Status status;
915 lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
916 if (!ctx_obj_ptr || status.Fail())
917 return;
918
919 AddOneType(context, TypeFromUser(m_ctx_obj->GetCompilerType()));
920 return;
921 }
922
923 // Clang is looking for the type of "*self"
924
925 if (!frame)
926 return;
927
928 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
929 lldb::eSymbolContextBlock);
930
931 // Find the block that defines the function represented by "sym_ctx"
932 Block *function_block = sym_ctx.GetFunctionBlock();
933
934 if (!function_block)
935 return;
936
937 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
938
939 if (!function_decl_ctx)
940 return;
941
942 clang::ObjCMethodDecl *method_decl =
944
945 if (method_decl) {
946 ObjCInterfaceDecl *self_interface = method_decl->getClassInterface();
947
948 if (!self_interface)
949 return;
950
951 const clang::Type *interface_type = self_interface->getTypeForDecl();
952
953 if (!interface_type)
954 return; // This is unlikely, but we have seen crashes where this
955 // occurred
956
957 TypeFromUser class_user_type(
958 QualType(interface_type, 0).getAsOpaquePtr(),
959 function_decl_ctx.GetTypeSystem()->weak_from_this());
960
961 LLDB_LOG(log, " FEVD Adding type for $__lldb_objc_class: {0}",
962 ClangUtil::ToString(interface_type));
963
964 AddOneType(context, class_user_type);
965 return;
966 }
967 // This branch will get hit if we are executing code in the context of
968 // a function that claims to have an object pointer (through
969 // DW_AT_object_pointer?) but is not formally a method of the class.
970 // In that case, just look up the "self" variable in the current scope
971 // and use its type.
972
973 // We exclude synthetic variables from here. Like above, it's highly unlikely
974 // we care about synthetic variables here, and indeed this code is looking for
975 // an obj-C specific construct.
976 VariableList *vars = frame->GetVariableList(
977 /*get_file_globals=*/false, /*include_synthetic_vars=*/false, nullptr);
978
979 lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
980
981 if (!self_var)
982 return;
983 if (!self_var->IsInScope(frame))
984 return;
985 if (!self_var->LocationIsValidForFrame(frame))
986 return;
987
988 Type *self_type = self_var->GetType();
989
990 if (!self_type)
991 return;
992
993 CompilerType self_clang_type = self_type->GetFullCompilerType();
994
995 if (TypeSystemClang::IsObjCClassType(self_clang_type)) {
996 return;
997 }
998 if (!TypeSystemClang::IsObjCObjectPointerType(self_clang_type))
999 return;
1000 self_clang_type = self_clang_type.GetPointeeType();
1001
1002 if (!self_clang_type)
1003 return;
1004
1005 LLDB_LOG(log, " FEVD Adding type for $__lldb_objc_class: {0}",
1007
1008 TypeFromUser class_user_type(self_clang_type);
1009
1010 AddOneType(context, class_user_type);
1011}
1012
1014 SymbolContext &sym_ctx, NameSearchContext &name_context) {
1015 if (sym_ctx.block == nullptr)
1016 return;
1017
1018 CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
1019 if (!frame_decl_context)
1020 return;
1021
1022 TypeSystemClang *frame_ast = llvm::dyn_cast_or_null<TypeSystemClang>(
1023 frame_decl_context.GetTypeSystem());
1024 if (!frame_ast)
1025 return;
1026
1027 clang::NamespaceDecl *namespace_decl =
1028 m_clang_ast_context->GetUniqueNamespaceDeclaration(
1030 if (!namespace_decl)
1031 return;
1032
1033 name_context.AddNamedDecl(namespace_decl);
1034 clang::DeclContext *ctxt = clang::Decl::castToDeclContext(namespace_decl);
1035 ctxt->setHasExternalVisibleStorage(true);
1036 name_context.m_found_local_vars_nsp = true;
1037}
1038
1040 NameSearchContext &context, ConstString name) {
1042
1043 if (!m_target)
1044 return;
1045
1046 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1048 if (!modules_decl_vendor)
1049 return;
1050
1051 bool append = false;
1052 uint32_t max_matches = 1;
1053 std::vector<CompilerDecl> decls;
1054
1055 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
1056 return;
1057
1058 assert(!decls.empty() && "FindDecls returned true but no decls?");
1059 auto *const decl_from_modules =
1060 llvm::cast<NamedDecl>(ClangUtil::GetDecl(decls[0]));
1061
1062 LLDB_LOG(log,
1063 " CAS::FEVD Matching decl found for "
1064 "\"{0}\" in the modules",
1065 name);
1066
1067 clang::Decl *copied_decl = CopyDecl(decl_from_modules);
1068 if (!copied_decl) {
1069 LLDB_LOG(log, " CAS::FEVD - Couldn't export a "
1070 "declaration from the modules");
1071 return;
1072 }
1073
1074 if (auto copied_function = dyn_cast<clang::FunctionDecl>(copied_decl)) {
1075 MaybeRegisterFunctionBody(copied_function);
1076
1077 context.AddNamedDecl(copied_function);
1078 } else if (auto copied_var = dyn_cast<clang::VarDecl>(copied_decl)) {
1079 context.AddNamedDecl(copied_var);
1080 context.m_found_variable = true;
1081 }
1082}
1083
1085 NameSearchContext &context, ConstString name, SymbolContext &sym_ctx,
1086 const CompilerDeclContext &namespace_decl) {
1087 if (sym_ctx.block == nullptr)
1088 return false;
1089
1090 CompilerDeclContext decl_context = sym_ctx.block->GetDeclContext();
1091 if (!decl_context)
1092 return false;
1093
1094 // Make sure that the variables are parsed so that we have the
1095 // declarations.
1096 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1097 VariableListSP vars = frame->GetInScopeVariableList(true);
1098 for (size_t i = 0; i < vars->GetSize(); i++)
1099 vars->GetVariableAtIndex(i)->GetDecl();
1100
1101 // Search for declarations matching the name. Do not include imported
1102 // decls in the search if we are looking for decls in the artificial
1103 // namespace $__lldb_local_vars.
1104 std::vector<CompilerDecl> found_decls =
1105 decl_context.FindDeclByName(name, namespace_decl.IsValid());
1106
1107 VariableSP var;
1108 bool variable_found = false;
1109 for (CompilerDecl decl : found_decls) {
1110 for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
1111 VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1112 if (candidate_var->GetDecl() == decl) {
1113 var = candidate_var;
1114 break;
1115 }
1116 }
1117
1118 if (var && !variable_found) {
1119 variable_found = true;
1120 ValueObjectSP valobj = ValueObjectVariable::Create(frame, var);
1121 AddOneVariable(context, var, valobj);
1122 context.m_found_variable = true;
1123 }
1124 }
1125
1126 // We're in a local_var_lookup but haven't found any local variables
1127 // so far. When performing a variable lookup from within the context of
1128 // a lambda, we count the lambda captures as local variables. Thus,
1129 // see if we captured any variables with the requested 'name'.
1130 if (!variable_found) {
1131 auto find_capture = [](ConstString varname,
1132 StackFrame *frame) -> ValueObjectSP {
1133 if (auto lambda = ClangExpressionUtil::GetLambdaValueObject(frame)) {
1134 if (auto capture = lambda->GetChildMemberWithName(varname)) {
1135 return capture;
1136 }
1137 }
1138
1139 return nullptr;
1140 };
1141
1142 if (auto capture = find_capture(name, frame)) {
1143 variable_found = true;
1144 context.m_found_variable = true;
1145 AddOneVariable(context, std::move(capture), std::move(find_capture));
1146 }
1147 }
1148
1149 return variable_found;
1150}
1151
1152/// Structure to hold the info needed when comparing function
1153/// declarations.
1154namespace {
1155struct FuncDeclInfo {
1156 ConstString m_name;
1157 CompilerType m_copied_type;
1158 uint32_t m_decl_lvl;
1159 SymbolContext m_sym_ctx;
1160};
1161} // namespace
1162
1164 const SymbolContextList &sc_list,
1165 const CompilerDeclContext &frame_decl_context) {
1166 // First, symplify things by looping through the symbol contexts to
1167 // remove unwanted functions and separate out the functions we want to
1168 // compare and prune into a separate list. Cache the info needed about
1169 // the function declarations in a vector for efficiency.
1170 SymbolContextList sc_sym_list;
1171 std::vector<FuncDeclInfo> decl_infos;
1172 decl_infos.reserve(sc_list.GetSize());
1173 clang::DeclContext *frame_decl_ctx =
1174 (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext();
1175 TypeSystemClang *ast = llvm::dyn_cast_or_null<TypeSystemClang>(
1176 frame_decl_context.GetTypeSystem());
1177
1178 for (const SymbolContext &sym_ctx : sc_list) {
1179 FuncDeclInfo fdi;
1180
1181 // We don't know enough about symbols to compare them, but we should
1182 // keep them in the list.
1183 Function *function = sym_ctx.function;
1184 if (!function) {
1185 sc_sym_list.Append(sym_ctx);
1186 continue;
1187 }
1188 // Filter out functions without declaration contexts, as well as
1189 // class/instance methods, since they'll be skipped in the code that
1190 // follows anyway.
1191 CompilerDeclContext func_decl_context = function->GetDeclContext();
1192 if (!func_decl_context || func_decl_context.IsClassMethod())
1193 continue;
1194 // We can only prune functions for which we can copy the type.
1195 CompilerType func_clang_type = function->GetType()->GetFullCompilerType();
1196 CompilerType copied_func_type = GuardedCopyType(func_clang_type);
1197 if (!copied_func_type) {
1198 sc_sym_list.Append(sym_ctx);
1199 continue;
1200 }
1201
1202 fdi.m_sym_ctx = sym_ctx;
1203 fdi.m_name = function->GetName();
1204 fdi.m_copied_type = copied_func_type;
1205 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL;
1206 if (fdi.m_copied_type && func_decl_context) {
1207 // Call CountDeclLevels to get the number of parent scopes we have
1208 // to look through before we find the function declaration. When
1209 // comparing functions of the same type, the one with a lower count
1210 // will be closer to us in the lookup scope and shadows the other.
1211 clang::DeclContext *func_decl_ctx =
1212 (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext();
1213 fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx, func_decl_ctx,
1214 &fdi.m_name, &fdi.m_copied_type);
1215 }
1216 decl_infos.emplace_back(fdi);
1217 }
1218
1219 // Loop through the functions in our cache looking for matching types,
1220 // then compare their scope levels to see which is closer.
1221 std::multimap<CompilerType, const FuncDeclInfo *> matches;
1222 for (const FuncDeclInfo &fdi : decl_infos) {
1223 const CompilerType t = fdi.m_copied_type;
1224 auto q = matches.find(t);
1225 if (q != matches.end()) {
1226 if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1227 // This function is closer; remove the old set.
1228 matches.erase(t);
1229 else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1230 // The functions in our set are closer - skip this one.
1231 continue;
1232 }
1233 matches.insert(std::make_pair(t, &fdi));
1234 }
1235
1236 // Loop through our matches and add their symbol contexts to our list.
1237 SymbolContextList sc_func_list;
1238 for (const auto &q : matches)
1239 sc_func_list.Append(q.second->m_sym_ctx);
1240
1241 // Rejoin the lists with the functions in front.
1242 sc_func_list.Append(sc_sym_list);
1243 return sc_func_list;
1244}
1245
1247 NameSearchContext &context, lldb::ModuleSP module_sp, ConstString name,
1248 const CompilerDeclContext &namespace_decl) {
1249 if (!m_parser_vars)
1250 return false;
1251
1252 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1253
1254 std::vector<CompilerDecl> decls_from_modules;
1255
1256 if (target) {
1257 if (std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
1259 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
1260 }
1261 }
1262
1263 SymbolContextList sc_list;
1264 if (namespace_decl && module_sp) {
1265 ModuleFunctionSearchOptions function_options;
1266 function_options.include_inlines = false;
1267 function_options.include_symbols = false;
1268
1269 module_sp->FindFunctions(name, namespace_decl, eFunctionNameTypeBase,
1270 function_options, sc_list);
1271 } else if (target && !namespace_decl) {
1272 ModuleFunctionSearchOptions function_options;
1273 function_options.include_inlines = false;
1274 function_options.include_symbols = true;
1275
1276 // TODO Fix FindFunctions so that it doesn't return
1277 // instance methods for eFunctionNameTypeBase.
1278
1279 target->GetImages().FindFunctions(
1280 name, eFunctionNameTypeFull | eFunctionNameTypeBase, function_options,
1281 sc_list);
1282 }
1283
1284 // If we found more than one function, see if we can use the frame's decl
1285 // context to remove functions that are shadowed by other functions which
1286 // match in type but are nearer in scope.
1287 //
1288 // AddOneFunction will not add a function whose type has already been
1289 // added, so if there's another function in the list with a matching type,
1290 // check to see if their decl context is a parent of the current frame's or
1291 // was imported via a and using statement, and pick the best match
1292 // according to lookup rules.
1293 if (sc_list.GetSize() > 1) {
1294 // Collect some info about our frame's context.
1295 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1296 SymbolContext frame_sym_ctx;
1297 if (frame != nullptr)
1298 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1299 lldb::eSymbolContextBlock);
1300 CompilerDeclContext frame_decl_context =
1301 frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext()
1303
1304 // We can't do this without a compiler decl context for our frame.
1305 if (frame_decl_context) {
1306 sc_list = SearchFunctionsInSymbolContexts(sc_list, frame_decl_context);
1307 }
1308 }
1309
1310 bool found_function_with_type_info = false;
1311
1312 if (sc_list.GetSize()) {
1313 const Symbol *extern_symbol = nullptr;
1314 const Symbol *non_extern_symbol = nullptr;
1315
1316 for (const SymbolContext &sym_ctx : sc_list) {
1317 if (sym_ctx.function) {
1318 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext();
1319
1320 if (!decl_ctx)
1321 continue;
1322
1323 // Filter out class/instance methods.
1324 if (decl_ctx.IsClassMethod())
1325 continue;
1326
1327 AddOneFunction(context, sym_ctx.function, nullptr);
1328 found_function_with_type_info = true;
1329 } else if (sym_ctx.symbol) {
1330 const Symbol *symbol = sym_ctx.symbol;
1331 if (target) {
1332 if (Symbol *actual =
1333 symbol->ResolveReExportedSymbol(*target, sym_ctx.module_sp))
1334 symbol = actual;
1335 else if (symbol->GetType() == eSymbolTypeReExported)
1336 continue;
1337 }
1338
1339 if (symbol->IsExternal())
1340 extern_symbol = symbol;
1341 else
1342 non_extern_symbol = symbol;
1343 }
1344 }
1345
1346 if (!found_function_with_type_info) {
1347 for (const CompilerDecl &compiler_decl : decls_from_modules) {
1348 clang::Decl *decl = ClangUtil::GetDecl(compiler_decl);
1349 if (llvm::isa<clang::FunctionDecl>(decl)) {
1350 clang::NamedDecl *copied_decl =
1351 llvm::cast_or_null<FunctionDecl>(CopyDecl(decl));
1352 if (copied_decl) {
1353 context.AddNamedDecl(copied_decl);
1354 found_function_with_type_info = true;
1355 }
1356 }
1357 }
1358 }
1359
1360 if (!found_function_with_type_info) {
1361 if (extern_symbol) {
1362 AddOneFunction(context, nullptr, extern_symbol);
1363 } else if (non_extern_symbol) {
1364 AddOneFunction(context, nullptr, non_extern_symbol);
1365 }
1366 }
1367 }
1368
1369 return found_function_with_type_info;
1370}
1371
1373 NameSearchContext &context, lldb::ModuleSP module_sp,
1374 const CompilerDeclContext &namespace_decl) {
1375 assert(m_ast_context);
1376
1378
1379 const ConstString name(context.m_decl_name.getAsString());
1380 if (IgnoreName(name, false))
1381 return;
1382
1383 // Only look for functions by name out in our symbols if the function doesn't
1384 // start with our phony prefix of '$'
1385
1386 Target *target = nullptr;
1387 StackFrame *frame = nullptr;
1388 SymbolContext sym_ctx;
1389 if (m_parser_vars) {
1390 target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1391 frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1392 }
1393 if (frame != nullptr)
1394 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1395 lldb::eSymbolContextBlock);
1396
1397 // Try the persistent decls, which take precedence over all else.
1398 if (!namespace_decl)
1399 SearchPersistenDecls(context, name);
1400
1401 if (name.GetStringRef().starts_with("$") && !namespace_decl) {
1402 if (name == "$__lldb_class") {
1403 LookUpLldbClass(context);
1404 return;
1405 }
1406
1407 if (name == "$__lldb_objc_class") {
1408 LookUpLldbObjCClass(context);
1409 return;
1410 }
1412 LookupLocalVarNamespace(sym_ctx, context);
1413 return;
1414 }
1415
1416 // any other $__lldb names should be weeded out now
1417 if (name.GetStringRef().starts_with("$__lldb"))
1418 return;
1419
1420 // No ParserVars means we can't do register or variable lookup.
1421 if (!m_parser_vars || !m_parser_vars->m_persistent_vars)
1422 return;
1423
1424 ExpressionVariableSP pvar_sp(
1425 m_parser_vars->m_persistent_vars->GetVariable(name));
1426
1427 if (pvar_sp) {
1428 AddOneVariable(context, pvar_sp);
1429 return;
1430 }
1431
1432 assert(name.GetStringRef().starts_with("$"));
1433 llvm::StringRef reg_name = name.GetStringRef().substr(1);
1434
1435 if (m_parser_vars->m_exe_ctx.GetRegisterContext()) {
1436 const RegisterInfo *reg_info(
1437 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(
1438 reg_name));
1439
1440 if (reg_info) {
1441 LLDB_LOG(log, " CEDM::FEVD Found register {0}", reg_info->name);
1442
1443 AddOneRegister(context, reg_info);
1444 }
1445 }
1446 return;
1447 }
1448
1449 bool local_var_lookup = !namespace_decl || (namespace_decl.GetName() ==
1451 if (frame && local_var_lookup)
1452 if (LookupLocalVariable(context, name, sym_ctx, namespace_decl))
1453 return;
1454
1455 if (target) {
1456 ValueObjectSP valobj;
1457 VariableSP var;
1458 var = FindGlobalVariable(*target, module_sp, name, namespace_decl);
1459
1460 if (var) {
1461 valobj = ValueObjectVariable::Create(target, var);
1462 AddOneVariable(context, var, valobj);
1463 context.m_found_variable = true;
1464 return;
1465 }
1466 }
1467
1468 if (!LookupFunction(context, module_sp, name, namespace_decl))
1469 LookupInModulesDeclVendor(context, name);
1470
1471 if (target && !context.m_found_variable && !namespace_decl) {
1472 // We couldn't find a non-symbol variable for this. Now we'll hunt for a
1473 // generic data symbol, and -- if it is found -- treat it as a variable.
1474 Status error;
1475
1476 const Symbol *data_symbol =
1477 m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error);
1478
1479 if (!error.Success()) {
1480 const unsigned diag_id =
1481 m_ast_context->getDiagnostics().getCustomDiagID(
1482 clang::DiagnosticsEngine::Level::Error, "%0");
1483 m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString();
1484 }
1485
1486 if (data_symbol) {
1487 std::string warning("got name from symbols: ");
1488 warning.append(name.GetStringRef());
1489 const unsigned diag_id =
1490 m_ast_context->getDiagnostics().getCustomDiagID(
1491 clang::DiagnosticsEngine::Level::Warning, "%0");
1492 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
1493 AddOneGenericVariable(context, *data_symbol);
1494 context.m_found_variable = true;
1495 }
1496 }
1497}
1498
1500 lldb_private::Value &var_location,
1501 TypeFromUser *user_type,
1502 TypeFromParser *parser_type) {
1504
1505 Type *var_type = var->GetType();
1506
1507 if (!var_type) {
1508 LLDB_LOG(log, "Skipped a definition because it has no type");
1509 return false;
1510 }
1511
1512 CompilerType var_clang_type = var_type->GetFullCompilerType();
1513
1514 if (!var_clang_type) {
1515 LLDB_LOG(log, "Skipped a definition because it has no Clang type");
1516 return false;
1517 }
1518
1519 auto clang_ast =
1521
1522 if (!clang_ast) {
1523 LLDB_LOG(log, "Skipped a definition because it has no Clang AST");
1524 return false;
1525 }
1526
1527 DWARFExpressionList &var_location_list = var->LocationExpressionList();
1528
1529 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1530 Status err;
1531
1532 if (var->GetLocationIsConstantValueData()) {
1533 DataExtractor const_value_extractor;
1534 if (var_location_list.GetExpressionData(const_value_extractor)) {
1535 var_location = Value(const_value_extractor.GetDataStart(),
1536 const_value_extractor.GetByteSize());
1538 } else {
1539 LLDB_LOG(log, "Error evaluating constant variable: {0}", err.AsCString());
1540 return false;
1541 }
1542 }
1543
1544 CompilerType type_to_use = GuardedCopyType(var_clang_type);
1545
1546 if (!type_to_use) {
1547 LLDB_LOG(log,
1548 "Couldn't copy a variable's type into the parser's AST context");
1549
1550 return false;
1551 }
1552
1553 if (parser_type)
1554 *parser_type = TypeFromParser(type_to_use);
1555
1556 if (var_location.GetContextType() == Value::ContextType::Invalid)
1557 var_location.SetCompilerType(type_to_use);
1558
1559 if (var_location.GetValueType() == Value::ValueType::FileAddress) {
1560 SymbolContext var_sc;
1561 var->CalculateSymbolContext(&var_sc);
1562
1563 if (!var_sc.module_sp)
1564 return false;
1565
1566 Address so_addr(var_location.GetScalar().ULongLong(),
1567 var_sc.module_sp->GetSectionList());
1568
1569 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1570
1571 if (load_addr != LLDB_INVALID_ADDRESS) {
1572 var_location.GetScalar() = load_addr;
1574 }
1575 }
1576
1577 if (user_type)
1578 *user_type = TypeFromUser(var_clang_type);
1579
1580 return true;
1581}
1582
1585 TypeFromParser const &pt,
1586 ValueObjectSP valobj) {
1587 clang::QualType parser_opaque_type =
1588 QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1589
1590 if (parser_opaque_type.isNull())
1591 return nullptr;
1592
1593 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
1594 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1595 CompleteType(tag_type->getDecl()->getDefinitionOrSelf());
1596 if (const ObjCObjectPointerType *objc_object_ptr_type =
1597 dyn_cast<ObjCObjectPointerType>(parser_type))
1598 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1599 }
1600
1601 bool is_reference = pt.IsReferenceType();
1602
1603 NamedDecl *var_decl = nullptr;
1604 if (is_reference)
1605 var_decl = context.AddVarDecl(pt);
1606 else
1607 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1608
1609 std::string decl_name(context.m_decl_name.getAsString());
1610 ConstString entity_name(decl_name);
1612 m_found_entities.AddNewlyConstructedVariable(entity);
1613
1614 assert(entity);
1615 entity->EnableParserVars(GetParserID());
1617 entity->GetParserVars(GetParserID());
1618
1619 parser_vars->m_named_decl = var_decl;
1620
1621 if (is_reference)
1622 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1623
1624 return parser_vars;
1625}
1626
1628 NameSearchContext &context, ValueObjectSP valobj,
1629 ValueObjectProviderTy valobj_provider) {
1630 assert(m_parser_vars.get());
1631 assert(valobj);
1632
1634
1635 Value var_location = valobj->GetValue();
1636
1637 TypeFromUser user_type = valobj->GetCompilerType();
1638
1639 auto clang_ast = user_type.GetTypeSystem<TypeSystemClang>();
1640
1641 if (!clang_ast) {
1642 LLDB_LOG(log, "Skipped a definition because it has no Clang AST");
1643 return;
1644 }
1645
1646 TypeFromParser parser_type = GuardedCopyType(user_type);
1647
1648 if (!parser_type) {
1649 LLDB_LOG(log,
1650 "Couldn't copy a variable's type into the parser's AST context");
1651
1652 return;
1653 }
1654
1655 if (var_location.GetContextType() == Value::ContextType::Invalid)
1656 var_location.SetCompilerType(parser_type);
1657
1659 AddExpressionVariable(context, parser_type, valobj);
1660
1661 if (!parser_vars)
1662 return;
1663
1664 LLDB_LOG(log, " CEDM::FEVD Found variable {0}, returned\n{1} (original {2})",
1665 context.m_decl_name, ClangUtil::DumpDecl(parser_vars->m_named_decl),
1666 ClangUtil::ToString(user_type));
1667
1668 parser_vars->m_llvm_value = nullptr;
1669 parser_vars->m_lldb_value = std::move(var_location);
1670 parser_vars->m_lldb_valobj_provider = std::move(valobj_provider);
1671}
1672
1674 VariableSP var,
1675 ValueObjectSP valobj) {
1676 assert(m_parser_vars.get());
1677
1679
1680 TypeFromUser ut;
1681 TypeFromParser pt;
1682 Value var_location;
1683
1684 if (!GetVariableValue(var, var_location, &ut, &pt))
1685 return;
1686
1688 AddExpressionVariable(context, pt, std::move(valobj));
1689
1690 if (!parser_vars)
1691 return;
1692
1693 LLDB_LOG(log, " CEDM::FEVD Found variable {0}, returned\n{1} (original {2})",
1694 context.m_decl_name, ClangUtil::DumpDecl(parser_vars->m_named_decl),
1696
1697 parser_vars->m_llvm_value = nullptr;
1698 parser_vars->m_lldb_value = var_location;
1699 parser_vars->m_lldb_var = var;
1700}
1701
1703 ExpressionVariableSP &pvar_sp) {
1705
1706 TypeFromUser user_type(
1707 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
1708
1709 TypeFromParser parser_type(GuardedCopyType(user_type));
1710
1711 if (!parser_type.GetOpaqueQualType()) {
1712 LLDB_LOG(log, " CEDM::FEVD Couldn't import type for pvar {0}",
1713 pvar_sp->GetName());
1714 return;
1715 }
1716
1717 NamedDecl *var_decl =
1718 context.AddVarDecl(parser_type.GetLValueReferenceType());
1719
1720 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1721 ->EnableParserVars(GetParserID());
1723 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1724 ->GetParserVars(GetParserID());
1725 parser_vars->m_named_decl = var_decl;
1726 parser_vars->m_llvm_value = nullptr;
1727 parser_vars->m_lldb_value.Clear();
1728
1729 LLDB_LOG(log, " CEDM::FEVD Added pvar {0}, returned\n{1}",
1730 pvar_sp->GetName(), ClangUtil::DumpDecl(var_decl));
1731}
1732
1734 const Symbol &symbol) {
1735 assert(m_parser_vars.get());
1736
1738
1739 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1740
1741 if (target == nullptr)
1742 return;
1743
1744 auto scratch_ast_context = GetScratchContext(*target);
1745 if (!scratch_ast_context)
1746 return;
1747
1748 TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid)
1749 .GetPointerType()
1750 .GetLValueReferenceType());
1751 TypeFromParser parser_type(m_clang_ast_context->GetBasicType(eBasicTypeVoid)
1752 .GetPointerType()
1753 .GetLValueReferenceType());
1754 NamedDecl *var_decl = context.AddVarDecl(parser_type);
1755
1756 std::string decl_name(context.m_decl_name.getAsString());
1757 ConstString entity_name(decl_name);
1759 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name,
1760 user_type, m_parser_vars->m_target_info.byte_order,
1761 m_parser_vars->m_target_info.address_byte_size));
1762 m_found_entities.AddNewlyConstructedVariable(entity);
1763
1764 entity->EnableParserVars(GetParserID());
1766 entity->GetParserVars(GetParserID());
1767
1768 const Address symbol_address = symbol.GetAddress();
1769 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1770
1771 // parser_vars->m_lldb_value.SetContext(Value::ContextType::ClangType,
1772 // user_type.GetOpaqueQualType());
1773 parser_vars->m_lldb_value.SetCompilerType(user_type);
1774 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1776
1777 parser_vars->m_named_decl = var_decl;
1778 parser_vars->m_llvm_value = nullptr;
1779 parser_vars->m_lldb_sym = &symbol;
1780
1781 LLDB_LOG(log, " CEDM::FEVD Found variable {0}, returned\n{1}", decl_name,
1782 ClangUtil::DumpDecl(var_decl));
1783}
1784
1786 const RegisterInfo *reg_info) {
1788
1789 CompilerType clang_type =
1790 m_clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
1791 reg_info->encoding, reg_info->byte_size * 8);
1792
1793 if (!clang_type) {
1794 LLDB_LOG(log, " Tried to add a type for {0}, but couldn't get one",
1795 context.m_decl_name.getAsString());
1796 return;
1797 }
1798
1799 TypeFromParser parser_clang_type(clang_type);
1800
1801 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
1802
1804 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1805 m_parser_vars->m_target_info.byte_order,
1806 m_parser_vars->m_target_info.address_byte_size));
1807 m_found_entities.AddNewlyConstructedVariable(entity);
1808
1809 std::string decl_name(context.m_decl_name.getAsString());
1810 entity->SetName(decl_name);
1811 entity->SetRegisterInfo(reg_info);
1812 entity->EnableParserVars(GetParserID());
1814 entity->GetParserVars(GetParserID());
1815 parser_vars->m_named_decl = var_decl;
1816 parser_vars->m_llvm_value = nullptr;
1817 parser_vars->m_lldb_value.Clear();
1818 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
1819
1820 LLDB_LOG(log, " CEDM::FEVD Added register {0}, returned\n{1}",
1821 context.m_decl_name.getAsString(), ClangUtil::DumpDecl(var_decl));
1822}
1823
1825 Function *function,
1826 const Symbol *symbol) {
1827 assert(m_parser_vars.get());
1828
1830
1831 NamedDecl *function_decl = nullptr;
1832 Address fun_address;
1833 CompilerType function_clang_type;
1834
1835 bool is_indirect_function = false;
1836
1837 if (function) {
1838 Type *function_type = function->GetType();
1839
1840 const auto lang = function->GetCompileUnit()->GetLanguage();
1841 const llvm::StringRef name =
1842 function->GetMangled().GetMangledName().GetStringRef();
1843 const bool extern_c =
1845 (Language::LanguageIsObjC(lang) &&
1847
1848 if (!extern_c) {
1849 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem();
1850 if (llvm::isa<TypeSystemClang>(type_system)) {
1851 clang::DeclContext *src_decl_context =
1852 (clang::DeclContext *)function->GetDeclContext()
1854 clang::FunctionDecl *src_function_decl =
1855 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
1856 if (src_function_decl &&
1857 src_function_decl->getTemplateSpecializationInfo()) {
1858 clang::FunctionTemplateDecl *function_template =
1859 src_function_decl->getTemplateSpecializationInfo()->getTemplate();
1860 clang::FunctionTemplateDecl *copied_function_template =
1861 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
1862 CopyDecl(function_template));
1863 if (copied_function_template) {
1864 if (log) {
1865 StreamString ss;
1866
1867 function->DumpSymbolContext(&ss);
1868
1869 LLDB_LOG(log,
1870 " CEDM::FEVD Imported decl for function template"
1871 " {0} (description {1}), returned\n{2}",
1872 copied_function_template->getNameAsString(),
1873 ss.GetData(),
1874 ClangUtil::DumpDecl(copied_function_template));
1875 }
1876
1877 context.AddNamedDecl(copied_function_template);
1878 }
1879 } else if (src_function_decl) {
1880 if (clang::FunctionDecl *copied_function_decl =
1881 llvm::dyn_cast_or_null<clang::FunctionDecl>(
1882 CopyDecl(src_function_decl))) {
1883 if (log) {
1884 StreamString ss;
1885
1886 function->DumpSymbolContext(&ss);
1887
1888 LLDB_LOG(log,
1889 " CEDM::FEVD Imported decl for function {0} "
1890 "(description {1}), returned\n{2}",
1891 copied_function_decl->getNameAsString(), ss.GetData(),
1892 ClangUtil::DumpDecl(copied_function_decl));
1893 }
1894
1895 context.AddNamedDecl(copied_function_decl);
1896 return;
1897 } else {
1898 LLDB_LOG(log, " Failed to import the function decl for '{0}'",
1899 src_function_decl->getName());
1900 }
1901 }
1902 }
1903 }
1904
1905 if (!function_type) {
1906 LLDB_LOG(log, " Skipped a function because it has no type");
1907 return;
1908 }
1909
1910 function_clang_type = function_type->GetFullCompilerType();
1911
1912 if (!function_clang_type) {
1913 LLDB_LOG(log, " Skipped a function because it has no Clang type");
1914 return;
1915 }
1916
1917 fun_address = function->GetAddress();
1918
1919 CompilerType copied_function_type = GuardedCopyType(function_clang_type);
1920 if (copied_function_type) {
1921 function_decl = context.AddFunDecl(copied_function_type, extern_c);
1922
1923 if (!function_decl) {
1924 LLDB_LOG(log, " Failed to create a function decl for '{0}' ({1:x})",
1925 function_type->GetName(), function_type->GetID());
1926
1927 return;
1928 }
1929 } else {
1930 // We failed to copy the type we found
1931 LLDB_LOG(log,
1932 " Failed to import the function type '{0}' ({1:x})"
1933 " into the expression parser AST context",
1934 function_type->GetName(), function_type->GetID());
1935
1936 return;
1937 }
1938 } else if (symbol) {
1939 fun_address = symbol->GetAddress();
1940 function_decl = context.AddGenericFunDecl();
1941 is_indirect_function = symbol->IsIndirect();
1942 } else {
1943 LLDB_LOG(log, " AddOneFunction called with no function and no symbol");
1944 return;
1945 }
1946
1947 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1948
1949 lldb::addr_t load_addr =
1950 fun_address.GetCallableLoadAddress(target, is_indirect_function);
1951
1953 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1954 m_parser_vars->m_target_info.byte_order,
1955 m_parser_vars->m_target_info.address_byte_size));
1956 m_found_entities.AddNewlyConstructedVariable(entity);
1957
1958 std::string decl_name(context.m_decl_name.getAsString());
1959 entity->SetName(decl_name);
1960 entity->SetCompilerType(function_clang_type);
1961 entity->EnableParserVars(GetParserID());
1962
1964 entity->GetParserVars(GetParserID());
1965
1966 if (load_addr != LLDB_INVALID_ADDRESS) {
1968 parser_vars->m_lldb_value.GetScalar() = load_addr;
1969 } else {
1970 // We have to try finding a file address.
1971
1972 lldb::addr_t file_addr = fun_address.GetFileAddress();
1973
1975 parser_vars->m_lldb_value.GetScalar() = file_addr;
1976 }
1977
1978 parser_vars->m_named_decl = function_decl;
1979 parser_vars->m_llvm_value = nullptr;
1980
1981 if (log) {
1982 StreamString ss;
1983
1984 fun_address.Dump(&ss,
1985 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1987
1988 LLDB_LOG(log,
1989 " CEDM::FEVD Found {0} function {1} (description {2}), "
1990 "returned\n{3}",
1991 (function ? "specific" : "generic"), decl_name, ss.GetData(),
1992 ClangUtil::DumpDecl(function_decl));
1993 }
1994}
1995
1997 const TypeFromUser &ut) {
1998 CompilerType copied_clang_type = GuardedCopyType(ut);
1999
2001
2002 if (!copied_clang_type) {
2003 LLDB_LOG(log,
2004 "ClangExpressionDeclMap::AddThisType - Couldn't import the type");
2005
2006 return;
2007 }
2008
2009 if (copied_clang_type.IsAggregateType() &&
2010 copied_clang_type.GetCompleteType()) {
2011 CompilerType void_clang_type =
2012 m_clang_ast_context->GetBasicType(eBasicTypeVoid);
2013 std::array<CompilerType, 1> args{void_clang_type.GetPointerType()};
2014
2015 CompilerType method_type = m_clang_ast_context->CreateFunctionType(
2016 void_clang_type, args, false,
2018
2019 const bool is_virtual = false;
2020 const bool is_static = false;
2021 const bool is_inline = false;
2022 const bool is_explicit = false;
2023 const bool is_attr_used = true;
2024 const bool is_artificial = false;
2025
2026 CXXMethodDecl *method_decl = m_clang_ast_context->AddMethodToCXXRecordType(
2027 copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", /*asm_label=*/{},
2028 method_type, is_virtual, is_static, is_inline, is_explicit,
2029 is_attr_used, is_artificial);
2030
2031 LLDB_LOG(log,
2032 " CEDM::AddThisType Added function $__lldb_expr "
2033 "(description {0}) for this type\n{1}",
2034 ClangUtil::ToString(copied_clang_type),
2035 ClangUtil::DumpDecl(method_decl));
2036 }
2037
2038 if (!copied_clang_type.IsValid())
2039 return;
2040
2041 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(
2042 QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType()));
2043
2044 if (!type_source_info)
2045 return;
2046
2047 // Construct a typedef type because if "*this" is a templated type we can't
2048 // just return ClassTemplateSpecializationDecls in response to name queries.
2049 // Using a typedef makes this much more robust.
2050
2051 TypedefDecl *typedef_decl = TypedefDecl::Create(
2052 *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(),
2053 SourceLocation(), context.m_decl_name.getAsIdentifierInfo(),
2054 type_source_info);
2055
2056 if (!typedef_decl)
2057 return;
2058
2059 context.AddNamedDecl(typedef_decl);
2060}
2061
2063 const TypeFromUser &ut) {
2064 CompilerType copied_clang_type = GuardedCopyType(ut);
2065
2066 if (!copied_clang_type) {
2068
2069 LLDB_LOG(log,
2070 "ClangExpressionDeclMap::AddOneType - Couldn't import the type");
2071
2072 return;
2073 }
2074
2075 context.AddTypeDecl(copied_clang_type);
2076}
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:891
lldb::ByteOrder GetByteOrder() const
Returns the byte order for the architecture specification.
Definition ArchSpec.cpp:940
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:377
const Address & GetAddress() const
Return the address of the function (its entry point).
Definition Function.h:430
ConstString GetName() const
Definition Function.cpp:726
const Mangled & GetMangled() const
Definition Function.h:532
void DumpSymbolContext(Stream *s) override
Dump the object's symbol context to the stream s.
Definition Function.cpp:523
Type * GetType()
Get accessor for the type that describes the function return value type, and parameter types.
Definition Function.cpp:566
CompilerDeclContext GetDeclContext()
Get the DeclContext for this function, if available.
Definition Function.cpp:552
CompileUnit * GetCompileUnit()
Get accessor for the compile unit that owns this function.
Definition Function.cpp:418
virtual lldb::addr_t LookupRuntimeSymbol(ConstString name)
static bool LanguageIsC(lldb::LanguageType language)
Definition Language.cpp:367
static bool LanguageIsCPlusPlus(lldb::LanguageType language)
Definition Language.cpp:342
static bool LanguageIsObjC(lldb::LanguageType language)
Definition Language.cpp:357
static bool IsMangledName(llvm::StringRef name)
Definition Mangled.cpp:39
ConstString GetMangledName() const
Mangled name get accessor.
Definition Mangled.h:174
Materializer packs the variables an expression reads and writes into a single argument struct,...
void FindFunctions(ConstString name, lldb::FunctionNameType name_type_mask, const ModuleFunctionSearchOptions &options, SymbolContextList &sc_list) const
void FindGlobalVariables(ConstString 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(ConstString name, lldb::SymbolType symbol_type, 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:91
static ObjCLanguageRuntime * Get(Process &process)
A plug-in interface definition class for debugging a process.
Definition Process.h:367
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3973
uint32_t GetAddressByteSize() const
Definition Process.cpp:3977
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:249
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:533
PersistentExpressionState * GetPersistentExpressionStateForLanguage(lldb::LanguageType language)
Definition Target.cpp:2787
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1254
const ArchSpec & GetArchitecture() const
Definition Target.h:1296
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition Thread.h:435
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()
Definition Type.cpp:791
ConstString GetName()
Definition Type.cpp:442
CompilerType GetFullCompilerType()
Definition Type.cpp:781
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:111
void SetCompilerType(const CompilerType &compiler_type)
Definition Value.cpp:276
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
@ 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:67
bool include_inlines
Include inlined functions.
Definition Module.h:71
bool include_symbols
Include the symbol table.
Definition Module.h:69
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