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 && symbol->GetType() == eSymbolTypeReExported) {
1332 symbol = symbol->ResolveReExportedSymbol(*target);
1333 if (symbol == nullptr)
1334 continue;
1335 }
1336
1337 if (symbol->IsExternal())
1338 extern_symbol = symbol;
1339 else
1340 non_extern_symbol = symbol;
1341 }
1342 }
1343
1344 if (!found_function_with_type_info) {
1345 for (const CompilerDecl &compiler_decl : decls_from_modules) {
1346 clang::Decl *decl = ClangUtil::GetDecl(compiler_decl);
1347 if (llvm::isa<clang::FunctionDecl>(decl)) {
1348 clang::NamedDecl *copied_decl =
1349 llvm::cast_or_null<FunctionDecl>(CopyDecl(decl));
1350 if (copied_decl) {
1351 context.AddNamedDecl(copied_decl);
1352 found_function_with_type_info = true;
1353 }
1354 }
1355 }
1356 }
1357
1358 if (!found_function_with_type_info) {
1359 if (extern_symbol) {
1360 AddOneFunction(context, nullptr, extern_symbol);
1361 } else if (non_extern_symbol) {
1362 AddOneFunction(context, nullptr, non_extern_symbol);
1363 }
1364 }
1365 }
1366
1367 return found_function_with_type_info;
1368}
1369
1371 NameSearchContext &context, lldb::ModuleSP module_sp,
1372 const CompilerDeclContext &namespace_decl) {
1373 assert(m_ast_context);
1374
1376
1377 const ConstString name(context.m_decl_name.getAsString());
1378 if (IgnoreName(name, false))
1379 return;
1380
1381 // Only look for functions by name out in our symbols if the function doesn't
1382 // start with our phony prefix of '$'
1383
1384 Target *target = nullptr;
1385 StackFrame *frame = nullptr;
1386 SymbolContext sym_ctx;
1387 if (m_parser_vars) {
1388 target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1389 frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1390 }
1391 if (frame != nullptr)
1392 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1393 lldb::eSymbolContextBlock);
1394
1395 // Try the persistent decls, which take precedence over all else.
1396 if (!namespace_decl)
1397 SearchPersistenDecls(context, name);
1398
1399 if (name.GetStringRef().starts_with("$") && !namespace_decl) {
1400 if (name == "$__lldb_class") {
1401 LookUpLldbClass(context);
1402 return;
1403 }
1404
1405 if (name == "$__lldb_objc_class") {
1406 LookUpLldbObjCClass(context);
1407 return;
1408 }
1410 LookupLocalVarNamespace(sym_ctx, context);
1411 return;
1412 }
1413
1414 // any other $__lldb names should be weeded out now
1415 if (name.GetStringRef().starts_with("$__lldb"))
1416 return;
1417
1418 // No ParserVars means we can't do register or variable lookup.
1419 if (!m_parser_vars || !m_parser_vars->m_persistent_vars)
1420 return;
1421
1422 ExpressionVariableSP pvar_sp(
1423 m_parser_vars->m_persistent_vars->GetVariable(name));
1424
1425 if (pvar_sp) {
1426 AddOneVariable(context, pvar_sp);
1427 return;
1428 }
1429
1430 assert(name.GetStringRef().starts_with("$"));
1431 llvm::StringRef reg_name = name.GetStringRef().substr(1);
1432
1433 if (m_parser_vars->m_exe_ctx.GetRegisterContext()) {
1434 const RegisterInfo *reg_info(
1435 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(
1436 reg_name));
1437
1438 if (reg_info) {
1439 LLDB_LOG(log, " CEDM::FEVD Found register {0}", reg_info->name);
1440
1441 AddOneRegister(context, reg_info);
1442 }
1443 }
1444 return;
1445 }
1446
1447 bool local_var_lookup = !namespace_decl || (namespace_decl.GetName() ==
1449 if (frame && local_var_lookup)
1450 if (LookupLocalVariable(context, name, sym_ctx, namespace_decl))
1451 return;
1452
1453 if (target) {
1454 ValueObjectSP valobj;
1455 VariableSP var;
1456 var = FindGlobalVariable(*target, module_sp, name, namespace_decl);
1457
1458 if (var) {
1459 valobj = ValueObjectVariable::Create(target, var);
1460 AddOneVariable(context, var, valobj);
1461 context.m_found_variable = true;
1462 return;
1463 }
1464 }
1465
1466 if (!LookupFunction(context, module_sp, name, namespace_decl))
1467 LookupInModulesDeclVendor(context, name);
1468
1469 if (target && !context.m_found_variable && !namespace_decl) {
1470 // We couldn't find a non-symbol variable for this. Now we'll hunt for a
1471 // generic data symbol, and -- if it is found -- treat it as a variable.
1472 Status error;
1473
1474 const Symbol *data_symbol =
1475 m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error);
1476
1477 if (!error.Success()) {
1478 const unsigned diag_id =
1479 m_ast_context->getDiagnostics().getCustomDiagID(
1480 clang::DiagnosticsEngine::Level::Error, "%0");
1481 m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString();
1482 }
1483
1484 if (data_symbol) {
1485 std::string warning("got name from symbols: ");
1486 warning.append(name.GetStringRef());
1487 const unsigned diag_id =
1488 m_ast_context->getDiagnostics().getCustomDiagID(
1489 clang::DiagnosticsEngine::Level::Warning, "%0");
1490 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
1491 AddOneGenericVariable(context, *data_symbol);
1492 context.m_found_variable = true;
1493 }
1494 }
1495}
1496
1498 lldb_private::Value &var_location,
1499 TypeFromUser *user_type,
1500 TypeFromParser *parser_type) {
1502
1503 Type *var_type = var->GetType();
1504
1505 if (!var_type) {
1506 LLDB_LOG(log, "Skipped a definition because it has no type");
1507 return false;
1508 }
1509
1510 CompilerType var_clang_type = var_type->GetFullCompilerType();
1511
1512 if (!var_clang_type) {
1513 LLDB_LOG(log, "Skipped a definition because it has no Clang type");
1514 return false;
1515 }
1516
1517 auto clang_ast =
1519
1520 if (!clang_ast) {
1521 LLDB_LOG(log, "Skipped a definition because it has no Clang AST");
1522 return false;
1523 }
1524
1525 DWARFExpressionList &var_location_list = var->LocationExpressionList();
1526
1527 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1528 Status err;
1529
1530 if (var->GetLocationIsConstantValueData()) {
1531 DataExtractor const_value_extractor;
1532 if (var_location_list.GetExpressionData(const_value_extractor)) {
1533 var_location = Value(const_value_extractor.GetDataStart(),
1534 const_value_extractor.GetByteSize());
1536 } else {
1537 LLDB_LOG(log, "Error evaluating constant variable: {0}", err.AsCString());
1538 return false;
1539 }
1540 }
1541
1542 CompilerType type_to_use = GuardedCopyType(var_clang_type);
1543
1544 if (!type_to_use) {
1545 LLDB_LOG(log,
1546 "Couldn't copy a variable's type into the parser's AST context");
1547
1548 return false;
1549 }
1550
1551 if (parser_type)
1552 *parser_type = TypeFromParser(type_to_use);
1553
1554 if (var_location.GetContextType() == Value::ContextType::Invalid)
1555 var_location.SetCompilerType(type_to_use);
1556
1557 if (var_location.GetValueType() == Value::ValueType::FileAddress) {
1558 SymbolContext var_sc;
1559 var->CalculateSymbolContext(&var_sc);
1560
1561 if (!var_sc.module_sp)
1562 return false;
1563
1564 Address so_addr(var_location.GetScalar().ULongLong(),
1565 var_sc.module_sp->GetSectionList());
1566
1567 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1568
1569 if (load_addr != LLDB_INVALID_ADDRESS) {
1570 var_location.GetScalar() = load_addr;
1572 }
1573 }
1574
1575 if (user_type)
1576 *user_type = TypeFromUser(var_clang_type);
1577
1578 return true;
1579}
1580
1583 TypeFromParser const &pt,
1584 ValueObjectSP valobj) {
1585 clang::QualType parser_opaque_type =
1586 QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1587
1588 if (parser_opaque_type.isNull())
1589 return nullptr;
1590
1591 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
1592 if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1593 CompleteType(tag_type->getDecl()->getDefinitionOrSelf());
1594 if (const ObjCObjectPointerType *objc_object_ptr_type =
1595 dyn_cast<ObjCObjectPointerType>(parser_type))
1596 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1597 }
1598
1599 bool is_reference = pt.IsReferenceType();
1600
1601 NamedDecl *var_decl = nullptr;
1602 if (is_reference)
1603 var_decl = context.AddVarDecl(pt);
1604 else
1605 var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1606
1607 std::string decl_name(context.m_decl_name.getAsString());
1608 ConstString entity_name(decl_name);
1610 m_found_entities.AddNewlyConstructedVariable(entity);
1611
1612 assert(entity);
1613 entity->EnableParserVars(GetParserID());
1615 entity->GetParserVars(GetParserID());
1616
1617 parser_vars->m_named_decl = var_decl;
1618
1619 if (is_reference)
1620 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1621
1622 return parser_vars;
1623}
1624
1626 NameSearchContext &context, ValueObjectSP valobj,
1627 ValueObjectProviderTy valobj_provider) {
1628 assert(m_parser_vars.get());
1629 assert(valobj);
1630
1632
1633 Value var_location = valobj->GetValue();
1634
1635 TypeFromUser user_type = valobj->GetCompilerType();
1636
1637 auto clang_ast = user_type.GetTypeSystem<TypeSystemClang>();
1638
1639 if (!clang_ast) {
1640 LLDB_LOG(log, "Skipped a definition because it has no Clang AST");
1641 return;
1642 }
1643
1644 TypeFromParser parser_type = GuardedCopyType(user_type);
1645
1646 if (!parser_type) {
1647 LLDB_LOG(log,
1648 "Couldn't copy a variable's type into the parser's AST context");
1649
1650 return;
1651 }
1652
1653 if (var_location.GetContextType() == Value::ContextType::Invalid)
1654 var_location.SetCompilerType(parser_type);
1655
1657 AddExpressionVariable(context, parser_type, valobj);
1658
1659 if (!parser_vars)
1660 return;
1661
1662 LLDB_LOG(log, " CEDM::FEVD Found variable {0}, returned\n{1} (original {2})",
1663 context.m_decl_name, ClangUtil::DumpDecl(parser_vars->m_named_decl),
1664 ClangUtil::ToString(user_type));
1665
1666 parser_vars->m_llvm_value = nullptr;
1667 parser_vars->m_lldb_value = std::move(var_location);
1668 parser_vars->m_lldb_valobj_provider = std::move(valobj_provider);
1669}
1670
1672 VariableSP var,
1673 ValueObjectSP valobj) {
1674 assert(m_parser_vars.get());
1675
1677
1678 TypeFromUser ut;
1679 TypeFromParser pt;
1680 Value var_location;
1681
1682 if (!GetVariableValue(var, var_location, &ut, &pt))
1683 return;
1684
1686 AddExpressionVariable(context, pt, std::move(valobj));
1687
1688 if (!parser_vars)
1689 return;
1690
1691 LLDB_LOG(log, " CEDM::FEVD Found variable {0}, returned\n{1} (original {2})",
1692 context.m_decl_name, ClangUtil::DumpDecl(parser_vars->m_named_decl),
1694
1695 parser_vars->m_llvm_value = nullptr;
1696 parser_vars->m_lldb_value = var_location;
1697 parser_vars->m_lldb_var = var;
1698}
1699
1701 ExpressionVariableSP &pvar_sp) {
1703
1704 TypeFromUser user_type(
1705 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
1706
1707 TypeFromParser parser_type(GuardedCopyType(user_type));
1708
1709 if (!parser_type.GetOpaqueQualType()) {
1710 LLDB_LOG(log, " CEDM::FEVD Couldn't import type for pvar {0}",
1711 pvar_sp->GetName());
1712 return;
1713 }
1714
1715 NamedDecl *var_decl =
1716 context.AddVarDecl(parser_type.GetLValueReferenceType());
1717
1718 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1719 ->EnableParserVars(GetParserID());
1721 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1722 ->GetParserVars(GetParserID());
1723 parser_vars->m_named_decl = var_decl;
1724 parser_vars->m_llvm_value = nullptr;
1725 parser_vars->m_lldb_value.Clear();
1726
1727 LLDB_LOG(log, " CEDM::FEVD Added pvar {0}, returned\n{1}",
1728 pvar_sp->GetName(), ClangUtil::DumpDecl(var_decl));
1729}
1730
1732 const Symbol &symbol) {
1733 assert(m_parser_vars.get());
1734
1736
1737 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1738
1739 if (target == nullptr)
1740 return;
1741
1742 auto scratch_ast_context = GetScratchContext(*target);
1743 if (!scratch_ast_context)
1744 return;
1745
1746 TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid)
1747 .GetPointerType()
1748 .GetLValueReferenceType());
1749 TypeFromParser parser_type(m_clang_ast_context->GetBasicType(eBasicTypeVoid)
1750 .GetPointerType()
1751 .GetLValueReferenceType());
1752 NamedDecl *var_decl = context.AddVarDecl(parser_type);
1753
1754 std::string decl_name(context.m_decl_name.getAsString());
1755 ConstString entity_name(decl_name);
1757 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name,
1758 user_type, m_parser_vars->m_target_info.byte_order,
1759 m_parser_vars->m_target_info.address_byte_size));
1760 m_found_entities.AddNewlyConstructedVariable(entity);
1761
1762 entity->EnableParserVars(GetParserID());
1764 entity->GetParserVars(GetParserID());
1765
1766 const Address symbol_address = symbol.GetAddress();
1767 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1768
1769 // parser_vars->m_lldb_value.SetContext(Value::ContextType::ClangType,
1770 // user_type.GetOpaqueQualType());
1771 parser_vars->m_lldb_value.SetCompilerType(user_type);
1772 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1774
1775 parser_vars->m_named_decl = var_decl;
1776 parser_vars->m_llvm_value = nullptr;
1777 parser_vars->m_lldb_sym = &symbol;
1778
1779 LLDB_LOG(log, " CEDM::FEVD Found variable {0}, returned\n{1}", decl_name,
1780 ClangUtil::DumpDecl(var_decl));
1781}
1782
1784 const RegisterInfo *reg_info) {
1786
1787 CompilerType clang_type =
1788 m_clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
1789 reg_info->encoding, reg_info->byte_size * 8);
1790
1791 if (!clang_type) {
1792 LLDB_LOG(log, " Tried to add a type for {0}, but couldn't get one",
1793 context.m_decl_name.getAsString());
1794 return;
1795 }
1796
1797 TypeFromParser parser_clang_type(clang_type);
1798
1799 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
1800
1802 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1803 m_parser_vars->m_target_info.byte_order,
1804 m_parser_vars->m_target_info.address_byte_size));
1805 m_found_entities.AddNewlyConstructedVariable(entity);
1806
1807 std::string decl_name(context.m_decl_name.getAsString());
1808 entity->SetName(decl_name);
1809 entity->SetRegisterInfo(reg_info);
1810 entity->EnableParserVars(GetParserID());
1812 entity->GetParserVars(GetParserID());
1813 parser_vars->m_named_decl = var_decl;
1814 parser_vars->m_llvm_value = nullptr;
1815 parser_vars->m_lldb_value.Clear();
1816 entity->m_flags |= ClangExpressionVariable::EVBareRegister;
1817
1818 LLDB_LOG(log, " CEDM::FEVD Added register {0}, returned\n{1}",
1819 context.m_decl_name.getAsString(), ClangUtil::DumpDecl(var_decl));
1820}
1821
1823 Function *function,
1824 const Symbol *symbol) {
1825 assert(m_parser_vars.get());
1826
1828
1829 NamedDecl *function_decl = nullptr;
1830 Address fun_address;
1831 CompilerType function_clang_type;
1832
1833 bool is_indirect_function = false;
1834
1835 if (function) {
1836 Type *function_type = function->GetType();
1837
1838 const auto lang = function->GetCompileUnit()->GetLanguage();
1839 const llvm::StringRef name =
1840 function->GetMangled().GetMangledName().GetStringRef();
1841 const bool extern_c =
1843 (Language::LanguageIsObjC(lang) &&
1845
1846 if (!extern_c) {
1847 TypeSystem *type_system = function->GetDeclContext().GetTypeSystem();
1848 if (llvm::isa<TypeSystemClang>(type_system)) {
1849 clang::DeclContext *src_decl_context =
1850 (clang::DeclContext *)function->GetDeclContext()
1852 clang::FunctionDecl *src_function_decl =
1853 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
1854 if (src_function_decl &&
1855 src_function_decl->getTemplateSpecializationInfo()) {
1856 clang::FunctionTemplateDecl *function_template =
1857 src_function_decl->getTemplateSpecializationInfo()->getTemplate();
1858 clang::FunctionTemplateDecl *copied_function_template =
1859 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
1860 CopyDecl(function_template));
1861 if (copied_function_template) {
1862 if (log) {
1863 StreamString ss;
1864
1865 function->DumpSymbolContext(&ss);
1866
1867 LLDB_LOG(log,
1868 " CEDM::FEVD Imported decl for function template"
1869 " {0} (description {1}), returned\n{2}",
1870 copied_function_template->getNameAsString(),
1871 ss.GetData(),
1872 ClangUtil::DumpDecl(copied_function_template));
1873 }
1874
1875 context.AddNamedDecl(copied_function_template);
1876 }
1877 } else if (src_function_decl) {
1878 if (clang::FunctionDecl *copied_function_decl =
1879 llvm::dyn_cast_or_null<clang::FunctionDecl>(
1880 CopyDecl(src_function_decl))) {
1881 if (log) {
1882 StreamString ss;
1883
1884 function->DumpSymbolContext(&ss);
1885
1886 LLDB_LOG(log,
1887 " CEDM::FEVD Imported decl for function {0} "
1888 "(description {1}), returned\n{2}",
1889 copied_function_decl->getNameAsString(), ss.GetData(),
1890 ClangUtil::DumpDecl(copied_function_decl));
1891 }
1892
1893 context.AddNamedDecl(copied_function_decl);
1894 return;
1895 } else {
1896 LLDB_LOG(log, " Failed to import the function decl for '{0}'",
1897 src_function_decl->getName());
1898 }
1899 }
1900 }
1901 }
1902
1903 if (!function_type) {
1904 LLDB_LOG(log, " Skipped a function because it has no type");
1905 return;
1906 }
1907
1908 function_clang_type = function_type->GetFullCompilerType();
1909
1910 if (!function_clang_type) {
1911 LLDB_LOG(log, " Skipped a function because it has no Clang type");
1912 return;
1913 }
1914
1915 fun_address = function->GetAddress();
1916
1917 CompilerType copied_function_type = GuardedCopyType(function_clang_type);
1918 if (copied_function_type) {
1919 function_decl = context.AddFunDecl(copied_function_type, extern_c);
1920
1921 if (!function_decl) {
1922 LLDB_LOG(log, " Failed to create a function decl for '{0}' ({1:x})",
1923 function_type->GetName(), function_type->GetID());
1924
1925 return;
1926 }
1927 } else {
1928 // We failed to copy the type we found
1929 LLDB_LOG(log,
1930 " Failed to import the function type '{0}' ({1:x})"
1931 " into the expression parser AST context",
1932 function_type->GetName(), function_type->GetID());
1933
1934 return;
1935 }
1936 } else if (symbol) {
1937 fun_address = symbol->GetAddress();
1938 function_decl = context.AddGenericFunDecl();
1939 is_indirect_function = symbol->IsIndirect();
1940 } else {
1941 LLDB_LOG(log, " AddOneFunction called with no function and no symbol");
1942 return;
1943 }
1944
1945 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1946
1947 lldb::addr_t load_addr =
1948 fun_address.GetCallableLoadAddress(target, is_indirect_function);
1949
1951 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1952 m_parser_vars->m_target_info.byte_order,
1953 m_parser_vars->m_target_info.address_byte_size));
1954 m_found_entities.AddNewlyConstructedVariable(entity);
1955
1956 std::string decl_name(context.m_decl_name.getAsString());
1957 entity->SetName(decl_name);
1958 entity->SetCompilerType(function_clang_type);
1959 entity->EnableParserVars(GetParserID());
1960
1962 entity->GetParserVars(GetParserID());
1963
1964 if (load_addr != LLDB_INVALID_ADDRESS) {
1966 parser_vars->m_lldb_value.GetScalar() = load_addr;
1967 } else {
1968 // We have to try finding a file address.
1969
1970 lldb::addr_t file_addr = fun_address.GetFileAddress();
1971
1973 parser_vars->m_lldb_value.GetScalar() = file_addr;
1974 }
1975
1976 parser_vars->m_named_decl = function_decl;
1977 parser_vars->m_llvm_value = nullptr;
1978
1979 if (log) {
1980 StreamString ss;
1981
1982 fun_address.Dump(&ss,
1983 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1985
1986 LLDB_LOG(log,
1987 " CEDM::FEVD Found {0} function {1} (description {2}), "
1988 "returned\n{3}",
1989 (function ? "specific" : "generic"), decl_name, ss.GetData(),
1990 ClangUtil::DumpDecl(function_decl));
1991 }
1992}
1993
1995 const TypeFromUser &ut) {
1996 CompilerType copied_clang_type = GuardedCopyType(ut);
1997
1999
2000 if (!copied_clang_type) {
2001 LLDB_LOG(log,
2002 "ClangExpressionDeclMap::AddThisType - Couldn't import the type");
2003
2004 return;
2005 }
2006
2007 if (copied_clang_type.IsAggregateType() &&
2008 copied_clang_type.GetCompleteType()) {
2009 CompilerType void_clang_type =
2010 m_clang_ast_context->GetBasicType(eBasicTypeVoid);
2011 std::array<CompilerType, 1> args{void_clang_type.GetPointerType()};
2012
2013 CompilerType method_type = m_clang_ast_context->CreateFunctionType(
2014 void_clang_type, args, false,
2016
2017 const bool is_virtual = false;
2018 const bool is_static = false;
2019 const bool is_inline = false;
2020 const bool is_explicit = false;
2021 const bool is_attr_used = true;
2022 const bool is_artificial = false;
2023
2024 CXXMethodDecl *method_decl = m_clang_ast_context->AddMethodToCXXRecordType(
2025 copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", /*asm_label=*/{},
2026 method_type, is_virtual, is_static, is_inline, is_explicit,
2027 is_attr_used, is_artificial);
2028
2029 LLDB_LOG(log,
2030 " CEDM::AddThisType Added function $__lldb_expr "
2031 "(description {0}) for this type\n{1}",
2032 ClangUtil::ToString(copied_clang_type),
2033 ClangUtil::DumpDecl(method_decl));
2034 }
2035
2036 if (!copied_clang_type.IsValid())
2037 return;
2038
2039 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(
2040 QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType()));
2041
2042 if (!type_source_info)
2043 return;
2044
2045 // Construct a typedef type because if "*this" is a templated type we can't
2046 // just return ClassTemplateSpecializationDecls in response to name queries.
2047 // Using a typedef makes this much more robust.
2048
2049 TypedefDecl *typedef_decl = TypedefDecl::Create(
2050 *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(),
2051 SourceLocation(), context.m_decl_name.getAsIdentifierInfo(),
2052 type_source_info);
2053
2054 if (!typedef_decl)
2055 return;
2056
2057 context.AddNamedDecl(typedef_decl);
2058}
2059
2061 const TypeFromUser &ut) {
2062 CompilerType copied_clang_type = GuardedCopyType(ut);
2063
2064 if (!copied_clang_type) {
2066
2067 LLDB_LOG(log,
2068 "ClangExpressionDeclMap::AddOneType - Couldn't import the type");
2069
2070 return;
2071 }
2072
2073 context.AddTypeDecl(copied_clang_type);
2074}
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:364
#define LLDB_LOG_VERBOSE(log,...)
Definition Log.h:371
#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:301
lldb::addr_t GetCallableLoadAddress(Target *target, bool is_indirect=false) const
Get the load address as a callable code load address.
Definition Address.cpp:326
@ 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:396
lldb::addr_t GetFileAddress() const
Get the file address.
Definition Address.cpp:281
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:690
lldb::ByteOrder GetByteOrder() const
Returns the byte order for the architecture specification.
Definition ArchSpec.cpp:739
A class that describes a single lexical block.
Definition Block.h:41
CompilerDeclContext GetDeclContext()
Definition Block.cpp:473
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:367
A class that describes a function.
Definition Function.h:392
const Address & GetAddress() const
Return the address of the function (its entry point).
Definition Function.h:445
ConstString GetName() const
Definition Function.cpp:713
const Mangled & GetMangled() const
Definition Function.h:526
void DumpSymbolContext(Stream *s) override
Dump the object's symbol context to the stream s.
Definition Function.cpp:507
Type * GetType()
Get accessor for the type that describes the function return value type, and parameter types.
Definition Function.cpp:555
CompilerDeclContext GetDeclContext()
Get the DeclContext for this function, if available.
Definition Function.cpp:541
CompileUnit * GetCompileUnit()
Get accessor for the compile unit that owns this function.
Definition Function.cpp:402
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
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:357
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3909
uint32_t GetAddressByteSize() const
Definition Process.cpp:3913
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:365
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:197
bool IsIndirect() const
Definition Symbol.cpp:223
lldb::SymbolType GetType() const
Definition Symbol.h:169
Address GetAddress() const
Definition Symbol.h:89
Symbol * ResolveReExportedSymbol(Target &target) const
Definition Symbol.cpp:483
PersistentExpressionState * GetPersistentExpressionStateForLanguage(lldb::LanguageType language)
Definition Target.cpp:2743
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1241
const ArchSpec & GetArchitecture() const
Definition Target.h:1283
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:113
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
Definition Value.h:52
@ FileAddress
A file address value.
Definition Value.h:47
@ LoadAddress
A load address value.
Definition Value.h:49
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:89
ContextType GetContextType() const
Definition Value.h:87
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:327
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:85
@ 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