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