52 #include "clang/AST/ASTConsumer.h"
53 #include "clang/AST/ASTContext.h"
54 #include "clang/AST/ASTImporter.h"
55 #include "clang/AST/Decl.h"
56 #include "clang/AST/DeclarationName.h"
57 #include "clang/AST/RecursiveASTVisitor.h"
65 using namespace clang;
75 lldb::ValueObjectSP GetCapturedThisValueObject(
StackFrame *frame) {
79 if (
auto thisThisValSP =
80 thisValSP->GetChildMemberWithName(
ConstString(
"this"),
true))
87 ClangExpressionDeclMap::ClangExpressionDeclMap(
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(),
121 lldb::eSymbolContextEverything);
131 m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(
145 clang::ASTConsumer *code_gen) {
159 entity_index < num_entities; ++entity_index) {
160 ExpressionVariableSP var_sp(
163 llvm::cast<ClangExpressionVariable>(var_sp.get())
167 for (
size_t pvar_index = 0,
169 pvar_index < num_pvars; ++pvar_index) {
170 ExpressionVariableSP pvar_sp(
171 m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
173 llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get()))
210 parser_type.GetTypeSystem().GetSharedPointer().get());
222 auto ast = parser_type.GetTypeSystem().dyn_cast_or_null<
TypeSystemClang>();
227 if (lldb::ExpressionVariableSP conflicting_var =
229 std::string msg = llvm::formatv(
"redefinition of persistent variable '{0}'",
242 if (target ==
nullptr)
246 if (!clang_ast_context)
280 if (target ==
nullptr)
290 LLDB_LOG(log,
"Persistent variable's type wasn't copied successfully");
302 ->CreatePersistentVariable(
332 LLDB_LOG(log,
"Created persistent variable with flags {0:x}", var->
m_flags);
346 llvm::Value *value,
size_t size,
351 bool is_persistent_variable =
false;
367 is_persistent_variable =
true;
373 LLDB_LOG(log,
"Adding value for (NamedDecl*){0} [{1} - {2}] to the structure",
380 llvm::cast<ClangExpressionVariable>(var)->GetParserVars(
GetParserID());
385 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(
GetParserID())) {
388 LLDB_LOG(log,
"Already placed at {0:x}", jit_vars->m_offset);
391 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(
GetParserID());
394 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(
GetParserID());
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);
412 offset =
m_parser_vars->m_materializer->AddSymbol(*sym, err);
414 offset =
m_parser_vars->m_materializer->AddRegister(*reg_info, err);
427 LLDB_LOG(log,
"Placed at {0:x}", offset);
486 llvm::cast<ClangExpressionVariable>(member_sp.get())
489 llvm::cast<ClangExpressionVariable>(member_sp.get())
492 if (!parser_vars || !jit_vars || !member_sp->GetValueObject())
498 name = member_sp->GetName();
562 ModuleSP reexport_module_sp;
569 if (!reexport_module_sp) {
577 symbol_type, reexport_module_sp.get());
619 return symbol_load_addr;
639 if (module && namespace_decl)
640 module->FindGlobalVariables(name, namespace_decl, -1, vars);
651 if (frame ==
nullptr)
655 lldb::eSymbolContextBlock);
656 if (sym_ctx.
block ==
nullptr)
660 if (!frame_decl_context)
663 return llvm::dyn_cast_or_null<TypeSystemClang>(
680 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
681 "'{0}' in a NULL DeclContext",
683 else if (
const NamedDecl *context_named_decl =
686 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
688 name, context_named_decl->getNameAsString());
691 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
696 if (
const NamespaceDecl *namespace_context =
698 if (namespace_context->getName().str() ==
713 LLDB_LOGV(log,
" CEDM::FEVD Inspecting (NamespaceMap*){0:x} ({1} entries)",
714 namespace_map.get(), namespace_map->size());
717 LLDB_LOG(log,
" CEDM::FEVD Searching namespace {0} in module {1}",
718 n.second.GetName(), n.first->GetFileSpec().GetFilename());
725 LLDB_LOG(log,
" CEDM::FEVD Searching the root namespace");
734 FunctionDecl *copied_function_decl) {
735 if (copied_function_decl->getBody() &&
m_parser_vars->m_code_gen) {
736 clang::DeclGroupRef decl_group_ref(copied_function_decl);
737 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
752 return m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
761 if (!persistent_decl)
764 Decl *parser_persistent_decl =
CopyDecl(persistent_decl);
766 if (!parser_persistent_decl)
769 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
771 if (!parser_named_decl)
774 if (clang::FunctionDecl *parser_function_decl =
775 llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
779 LLDB_LOG(log,
" CEDM::FEVD Found persistent decl {0}", name);
789 if (frame !=
nullptr)
791 lldb::eSymbolContextBlock);
796 if (!ctx_obj_ptr || status.
Fail())
805 if (frame ==
nullptr)
816 if (!function_decl_ctx)
819 clang::CXXMethodDecl *method_decl =
823 if (
auto capturedThis = GetCapturedThisValueObject(frame)) {
837 " CEDM::FEVD Adding captured type ({0} for"
838 " $__lldb_class: {1}",
839 capturedThis->GetTypeName(), capturedThis->GetName());
845 clang::CXXRecordDecl *class_decl = method_decl->getParent();
847 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
850 class_qual_type.getAsOpaquePtr(),
853 LLDB_LOG(log,
" CEDM::FEVD Adding type for $__lldb_class: {0}",
854 class_qual_type.getAsString());
873 if (this_var && this_var->IsInScope(frame) &&
874 this_var->LocationIsValidForFrame(frame)) {
875 Type *this_type = this_var->GetType();
883 LLDB_LOG(log,
" FEVD Adding type for $__lldb_class: {0}",
898 if (!ctx_obj_ptr || status.
Fail())
911 lldb::eSymbolContextBlock);
921 if (!function_decl_ctx)
924 clang::ObjCMethodDecl *method_decl =
928 ObjCInterfaceDecl *self_interface = method_decl->getClassInterface();
933 const clang::Type *interface_type = self_interface->getTypeForDecl();
940 QualType(interface_type, 0).getAsOpaquePtr(),
943 LLDB_LOG(log,
" FEVD[{0}] Adding type for $__lldb_objc_class: {1}",
961 if (!self_var->IsInScope(frame))
963 if (!self_var->LocationIsValidForFrame(frame))
966 Type *self_type = self_var->GetType();
980 if (!self_clang_type)
983 LLDB_LOG(log,
" FEVD[{0}] Adding type for $__lldb_objc_class: {1}",
993 if (sym_ctx.
block ==
nullptr)
997 if (!frame_decl_context)
1005 clang::NamespaceDecl *namespace_decl =
1008 if (!namespace_decl)
1012 clang::DeclContext *ctxt = clang::Decl::castToDeclContext(namespace_decl);
1013 ctxt->setHasExternalVisibleStorage(
true);
1024 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1026 if (!modules_decl_vendor)
1029 bool append =
false;
1031 std::vector<clang::NamedDecl *> decls;
1033 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
1036 assert(!decls.empty() &&
"FindDecls returned true but no decls?");
1037 clang::NamedDecl *
const decl_from_modules = decls[0];
1040 " CAS::FEVD Matching decl found for "
1041 "\"{0}\" in the modules",
1044 clang::Decl *copied_decl =
CopyDecl(decl_from_modules);
1046 LLDB_LOG(log,
" CAS::FEVD - Couldn't export a "
1047 "declaration from the modules");
1051 if (
auto copied_function = dyn_cast<clang::FunctionDecl>(copied_decl)) {
1058 }
else if (
auto copied_var = dyn_cast<clang::VarDecl>(copied_decl)) {
1067 if (sym_ctx.
block ==
nullptr)
1078 for (
size_t i = 0; i < vars->GetSize(); i++)
1079 vars->GetVariableAtIndex(i)->GetDecl();
1084 std::vector<CompilerDecl> found_decls =
1088 bool variable_found =
false;
1090 for (
size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
1091 VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1092 if (candidate_var->GetDecl() == decl) {
1093 var = candidate_var;
1098 if (var && !variable_found) {
1099 variable_found =
true;
1110 if (!variable_found) {
1114 if (
auto capture = lambda->GetChildMemberWithName(varname,
true)) {
1122 if (
auto capture = find_capture(name, frame)) {
1123 variable_found =
true;
1125 AddOneVariable(context, std::move(capture), std::move(find_capture));
1129 return variable_found;
1135 struct FuncDeclInfo {
1152 std::vector<FuncDeclInfo> decl_infos;
1153 decl_infos.reserve(num_indices);
1154 clang::DeclContext *frame_decl_ctx =
1159 for (
uint32_t index = 0; index < num_indices; ++index) {
1168 sc_sym_list.
Append(sym_ctx);
1175 if (!func_decl_context ||
1179 CompilerType func_clang_type =
function->GetType()->GetFullCompilerType();
1181 if (!copied_func_type) {
1182 sc_sym_list.
Append(sym_ctx);
1186 fdi.m_sym_ctx = sym_ctx;
1187 fdi.m_name =
function->GetName();
1188 fdi.m_copied_type = copied_func_type;
1190 if (fdi.m_copied_type && func_decl_context) {
1195 clang::DeclContext *func_decl_ctx =
1198 &fdi.m_name, &fdi.m_copied_type);
1200 decl_infos.emplace_back(fdi);
1205 std::multimap<CompilerType, const FuncDeclInfo *> matches;
1206 for (
const FuncDeclInfo &fdi : decl_infos) {
1208 auto q = matches.find(t);
1209 if (q != matches.end()) {
1210 if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1213 else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1217 matches.insert(std::make_pair(t, &fdi));
1222 for (
const auto &q : matches)
1223 sc_func_list.
Append(q.second->m_sym_ctx);
1226 sc_func_list.
Append(sc_sym_list);
1227 return sc_func_list;
1238 std::vector<clang::NamedDecl *> decls_from_modules;
1241 if (std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
1243 decl_vendor->FindDecls(name,
false,
UINT32_MAX, decls_from_modules);
1248 if (namespace_decl && module_sp) {
1253 module_sp->FindFunctions(name, namespace_decl, eFunctionNameTypeBase,
1254 function_options, sc_list);
1255 }
else if (target && !namespace_decl) {
1264 name, eFunctionNameTypeFull | eFunctionNameTypeBase, function_options,
1281 if (frame !=
nullptr)
1283 lldb::eSymbolContextBlock);
1289 if (frame_decl_context) {
1295 Symbol *extern_symbol =
nullptr;
1296 Symbol *non_extern_symbol =
nullptr;
1299 index < num_indices; ++index) {
1316 }
else if (sym_ctx.
symbol) {
1319 if (sym_ctx.
symbol ==
nullptr)
1324 extern_symbol = sym_ctx.
symbol;
1326 non_extern_symbol = sym_ctx.
symbol;
1331 for (clang::NamedDecl *decl : decls_from_modules) {
1332 if (llvm::isa<clang::FunctionDecl>(decl)) {
1333 clang::NamedDecl *copied_decl =
1334 llvm::cast_or_null<FunctionDecl>(
CopyDecl(decl));
1344 if (extern_symbol) {
1347 }
else if (non_extern_symbol) {
1369 Target *target =
nullptr;
1376 if (frame !=
nullptr)
1378 lldb::eSymbolContextBlock);
1381 if (!namespace_decl)
1384 if (name.
GetStringRef().startswith(
"$") && !namespace_decl) {
1385 if (name ==
"$__lldb_class") {
1390 if (name ==
"$__lldb_objc_class") {
1407 ExpressionVariableSP pvar_sp(
1416 llvm::StringRef reg_name = name.
GetStringRef().substr(1);
1419 const RegisterInfo *reg_info(
1420 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(
1424 LLDB_LOG(log,
" CEDM::FEVD Found register {0}", reg_info->name);
1432 bool local_var_lookup = !namespace_decl || (namespace_decl.
GetName() ==
1434 if (frame && local_var_lookup)
1439 ValueObjectSP valobj;
1462 const Symbol *data_symbol =
1465 if (!
error.Success()) {
1466 const unsigned diag_id =
1475 const unsigned diag_id =
1477 clang::DiagnosticsEngine::Level::Warning,
"%0");
1491 Type *var_type = var->GetType();
1494 LLDB_LOG(log,
"Skipped a definition because it has no type");
1500 if (!var_clang_type) {
1501 LLDB_LOG(log,
"Skipped a definition because it has no Clang type");
1509 LLDB_LOG(log,
"Skipped a definition because it has no Clang AST");
1518 if (var->GetLocationIsConstantValueData()) {
1534 "Couldn't copy a variable's type into the parser's AST context");
1547 var->CalculateSymbolContext(&var_sc);
1572 ValueObjectSP valobj) {
1573 clang::QualType parser_opaque_type =
1574 QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1576 if (parser_opaque_type.isNull())
1579 if (
const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
1580 if (
const TagType *tag_type = dyn_cast<TagType>(parser_type))
1582 if (
const ObjCObjectPointerType *objc_object_ptr_type =
1583 dyn_cast<ObjCObjectPointerType>(parser_type))
1584 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1587 bool is_reference = pt.IsReferenceType();
1589 NamedDecl *var_decl =
nullptr;
1593 var_decl = context.
AddVarDecl(pt.GetLValueReferenceType());
1615 ValueObjectProviderTy valobj_provider) {
1621 Value var_location = valobj->GetValue();
1629 LLDB_LOG(log,
"Skipped a definition because it has no Clang AST");
1637 "Couldn't copy a variable's type into the parser's AST context");
1651 LLDB_LOG(log,
" CEDM::FEVD Found variable {0}, returned\n{1} (original {2})",
1662 ValueObjectSP valobj) {
1680 LLDB_LOG(log,
" CEDM::FEVD Found variable {0}, returned\n{1} (original {2})",
1690 ExpressionVariableSP &pvar_sp) {
1694 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
1698 if (!parser_type.GetOpaqueQualType()) {
1699 LLDB_LOG(log,
" CEDM::FEVD Couldn't import type for pvar {0}",
1700 pvar_sp->GetName());
1704 NamedDecl *var_decl =
1705 context.
AddVarDecl(parser_type.GetLValueReferenceType());
1707 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1710 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1716 LLDB_LOG(log,
" CEDM::FEVD Added pvar {0}, returned\n{1}",
1728 if (target ==
nullptr)
1732 if (!scratch_ast_context)
1737 .GetLValueReferenceType());
1741 NamedDecl *var_decl = context.
AddVarDecl(parser_type);
1746 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name,
1768 LLDB_LOG(log,
" CEDM::FEVD Found variable {0}, returned\n{1}", decl_name,
1773 const RegisterInfo *reg_info) {
1778 reg_info->encoding, reg_info->byte_size * 8);
1781 LLDB_LOG(log,
" Tried to add a type for {0}, but couldn't get one",
1788 NamedDecl *var_decl = context.
AddVarDecl(parser_clang_type);
1807 LLDB_LOG(log,
" CEDM::FEVD Added register {0}, returned\n{1}",
1818 NamedDecl *function_decl =
nullptr;
1822 bool is_indirect_function =
false;
1825 Type *function_type =
function->GetType();
1827 const auto lang =
function->GetCompileUnit()->GetLanguage();
1828 const auto name =
function->GetMangled().GetMangledName().AsCString();
1835 TypeSystem *type_system =
function->GetDeclContext().GetTypeSystem();
1836 if (llvm::isa<TypeSystemClang>(type_system)) {
1837 clang::DeclContext *src_decl_context =
1838 (clang::DeclContext *)function->GetDeclContext()
1839 .GetOpaqueDeclContext();
1840 clang::FunctionDecl *src_function_decl =
1841 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
1842 if (src_function_decl &&
1843 src_function_decl->getTemplateSpecializationInfo()) {
1844 clang::FunctionTemplateDecl *function_template =
1845 src_function_decl->getTemplateSpecializationInfo()->getTemplate();
1846 clang::FunctionTemplateDecl *copied_function_template =
1847 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
1849 if (copied_function_template) {
1853 function->DumpSymbolContext(&ss);
1856 " CEDM::FEVD Imported decl for function template"
1857 " {0} (description {1}), returned\n{2}",
1858 copied_function_template->getNameAsString(),
1865 }
else if (src_function_decl) {
1866 if (clang::FunctionDecl *copied_function_decl =
1867 llvm::dyn_cast_or_null<clang::FunctionDecl>(
1872 function->DumpSymbolContext(&ss);
1875 " CEDM::FEVD Imported decl for function {0} "
1876 "(description {1}), returned\n{2}",
1877 copied_function_decl->getNameAsString(), ss.
GetData(),
1884 LLDB_LOG(log,
" Failed to import the function decl for '{0}'",
1885 src_function_decl->getName());
1891 if (!function_type) {
1892 LLDB_LOG(log,
" Skipped a function because it has no type");
1898 if (!function_clang_type) {
1899 LLDB_LOG(log,
" Skipped a function because it has no Clang type");
1903 fun_address =
function->GetAddressRange().GetBaseAddress();
1906 if (copied_function_type) {
1907 function_decl = context.
AddFunDecl(copied_function_type, extern_c);
1909 if (!function_decl) {
1910 LLDB_LOG(log,
" Failed to create a function decl for '{0}' ({1:x})",
1918 " Failed to import the function type '{0}' ({1:x})"
1919 " into the expression parser AST context",
1924 }
else if (symbol) {
1929 LLDB_LOG(log,
" AddOneFunction called with no function and no symbol");
1970 fun_address.
Dump(&ss,
1975 " CEDM::FEVD Found {0} function {1} (description {2}), "
1977 (
function ?
"specific" :
"generic"), decl_name, ss.
GetData(),
1988 if (!copied_clang_type) {
1990 "ClangExpressionDeclMap::AddThisType - Couldn't import the type");
2002 void_clang_type, &void_ptr_clang_type, 1,
false, 0);
2004 const bool is_virtual =
false;
2005 const bool is_static =
false;
2006 const bool is_inline =
false;
2007 const bool is_explicit =
false;
2008 const bool is_attr_used =
true;
2009 const bool is_artificial =
false;
2014 is_explicit, is_attr_used, is_artificial);
2017 " CEDM::AddThisType Added function $__lldb_expr "
2018 "(description {0}) for this type\n{1}",
2023 if (!copied_clang_type.
IsValid())
2026 TypeSourceInfo *type_source_info =
m_ast_context->getTrivialTypeSourceInfo(
2029 if (!type_source_info)
2036 TypedefDecl *typedef_decl = TypedefDecl::Create(
2038 SourceLocation(), context.
m_decl_name.getAsIdentifierInfo(),
2051 if (!copied_clang_type) {
2055 "ClangExpressionDeclMap::AddOneType - Couldn't import the type");