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