48 #include "clang/AST/ASTConsumer.h"
49 #include "clang/AST/ASTContext.h"
50 #include "clang/AST/ASTImporter.h"
51 #include "clang/AST/Decl.h"
52 #include "clang/AST/DeclarationName.h"
53 #include "clang/AST/RecursiveASTVisitor.h"
61 using namespace clang;
65 ClangExpressionDeclMap::ClangExpressionDeclMap(
66 bool keep_result_in_memory,
68 const lldb::TargetSP &target,
69 const std::shared_ptr<ClangASTImporter> &importer,
ValueObject *ctx_obj)
70 :
ClangASTSource(target, importer), m_found_entities(), m_struct_members(),
71 m_keep_result_in_memory(keep_result_in_memory),
72 m_result_delegate(result_delegate), m_ctx_obj(ctx_obj), m_parser_vars(),
99 lldb::eSymbolContextEverything);
109 m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(
123 clang::ASTConsumer *code_gen) {
137 entity_index < num_entities; ++entity_index) {
138 ExpressionVariableSP var_sp(
141 llvm::cast<ClangExpressionVariable>(var_sp.get())
145 for (
size_t pvar_index = 0,
147 pvar_index < num_pvars; ++pvar_index) {
148 ExpressionVariableSP pvar_sp(
149 m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
151 llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get()))
187 assert((
TypeSystem *)&source == parser_type.GetTypeSystem());
201 llvm::dyn_cast_or_null<TypeSystemClang>(parser_type.GetTypeSystem());
206 if (lldb::ExpressionVariableSP conflicting_var =
208 std::string msg = llvm::formatv(
"redefinition of persistent variable '{0}'",
221 if (target ==
nullptr)
225 if (!clang_ast_context)
259 if (target ==
nullptr)
269 LLDB_LOG(log,
"Persistent variable's type wasn't copied successfully");
281 ->CreatePersistentVariable(
311 LLDB_LOG(log,
"Created persistent variable with flags {0:x}", var->
m_flags);
325 llvm::Value *value,
size_t size,
330 bool is_persistent_variable =
false;
346 is_persistent_variable =
true;
352 LLDB_LOG(log,
"Adding value for (NamedDecl*){0} [{1} - {2}] to the structure",
359 llvm::cast<ClangExpressionVariable>(var)->GetParserVars(
GetParserID());
364 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(
GetParserID())) {
367 LLDB_LOG(log,
"Already placed at {0:x}", jit_vars->m_offset);
370 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(
GetParserID());
373 llvm::cast<ClangExpressionVariable>(var)->GetJITVars(
GetParserID());
385 if (is_persistent_variable) {
386 ExpressionVariableSP var_sp(var->shared_from_this());
387 offset =
m_parser_vars->m_materializer->AddPersistentVariable(
388 var_sp,
nullptr, err);
391 offset =
m_parser_vars->m_materializer->AddSymbol(*sym, err);
393 offset =
m_parser_vars->m_materializer->AddRegister(*reg_info, err);
402 LLDB_LOG(log,
"Placed at {0:x}", offset);
461 llvm::cast<ClangExpressionVariable>(member_sp.get())
464 llvm::cast<ClangExpressionVariable>(member_sp.get())
467 if (!parser_vars || !jit_vars || !member_sp->GetValueObject())
473 name = member_sp->GetName();
537 ModuleSP reexport_module_sp;
544 if (!reexport_module_sp) {
552 symbol_type, reexport_module_sp.get());
594 return symbol_load_addr;
614 if (module && namespace_decl)
615 module->FindGlobalVariables(name, namespace_decl, -1, vars);
626 if (frame ==
nullptr)
630 lldb::eSymbolContextBlock);
631 if (sym_ctx.
block ==
nullptr)
635 if (!frame_decl_context)
638 return llvm::dyn_cast_or_null<TypeSystemClang>(
655 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
656 "'{0}' in a NULL DeclContext",
658 else if (
const NamedDecl *context_named_decl =
661 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
663 name, context_named_decl->getNameAsString());
666 "ClangExpressionDeclMap::FindExternalVisibleDecls for "
671 if (
const NamespaceDecl *namespace_context =
673 if (namespace_context->getName().str() ==
688 LLDB_LOGV(log,
" CEDM::FEVD Inspecting (NamespaceMap*){0:x} ({1} entries)",
689 namespace_map.get(), namespace_map->size());
692 LLDB_LOG(log,
" CEDM::FEVD Searching namespace {0} in module {1}",
693 n.second.GetName(), n.first->GetFileSpec().GetFilename());
700 LLDB_LOG(log,
" CEDM::FEVD Searching the root namespace");
709 FunctionDecl *copied_function_decl) {
710 if (copied_function_decl->getBody() &&
m_parser_vars->m_code_gen) {
711 clang::DeclGroupRef decl_group_ref(copied_function_decl);
712 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
727 return m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
736 if (!persistent_decl)
739 Decl *parser_persistent_decl =
CopyDecl(persistent_decl);
741 if (!parser_persistent_decl)
744 NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
746 if (!parser_named_decl)
749 if (clang::FunctionDecl *parser_function_decl =
750 llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
754 LLDB_LOG(log,
" CEDM::FEVD Found persistent decl {0}", name);
764 if (frame !=
nullptr)
766 lldb::eSymbolContextBlock);
771 if (!ctx_obj_ptr || status.
Fail())
784 if (frame ==
nullptr)
795 if (!function_decl_ctx)
798 clang::CXXMethodDecl *method_decl =
802 clang::CXXRecordDecl *class_decl = method_decl->getParent();
804 QualType class_qual_type(class_decl->getTypeForDecl(), 0);
806 TypeFromUser class_user_type(class_qual_type.getAsOpaquePtr(),
809 LLDB_LOG(log,
" CEDM::FEVD Adding type for $__lldb_class: {1}",
810 class_qual_type.getAsString());
814 if (method_decl->isInstance()) {
817 QualType class_pointer_type =
818 method_decl->getASTContext().getPointerType(class_qual_type);
820 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
841 if (this_var && this_var->IsInScope(frame) &&
842 this_var->LocationIsValidForFrame(frame)) {
843 Type *this_type = this_var->GetType();
851 LLDB_LOG(log,
" FEVD Adding type for $__lldb_class: {1}",
868 if (!ctx_obj_ptr || status.
Fail())
885 lldb::eSymbolContextBlock);
895 if (!function_decl_ctx)
898 clang::ObjCMethodDecl *method_decl =
902 ObjCInterfaceDecl *self_interface = method_decl->getClassInterface();
907 const clang::Type *interface_type = self_interface->getTypeForDecl();
913 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
916 LLDB_LOG(log,
" FEVD[{0}] Adding type for $__lldb_objc_class: {1}",
921 if (method_decl->isInstanceMethod()) {
924 QualType class_pointer_type =
925 method_decl->getASTContext().getObjCObjectPointerType(
926 QualType(interface_type, 0));
928 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
934 QualType class_type = method_decl->getASTContext().getObjCClassType();
936 TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
956 if (!self_var->IsInScope(frame))
958 if (!self_var->LocationIsValidForFrame(frame))
961 Type *self_type = self_var->GetType();
975 if (!self_clang_type)
978 LLDB_LOG(log,
" FEVD[{0}] Adding type for $__lldb_objc_class: {1}",
992 if (sym_ctx.
block ==
nullptr)
996 if (!frame_decl_context)
1004 clang::NamespaceDecl *namespace_decl =
1007 if (!namespace_decl)
1011 clang::DeclContext *ctxt = clang::Decl::castToDeclContext(namespace_decl);
1012 ctxt->setHasExternalVisibleStorage(
true);
1023 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1025 if (!modules_decl_vendor)
1028 bool append =
false;
1030 std::vector<clang::NamedDecl *> decls;
1032 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
1035 assert(!decls.empty() &&
"FindDecls returned true but no decls?");
1036 clang::NamedDecl *
const decl_from_modules = decls[0];
1039 " CAS::FEVD Matching decl found for "
1040 "\"{1}\" in the modules",
1043 clang::Decl *copied_decl =
CopyDecl(decl_from_modules);
1045 LLDB_LOG(log,
" CAS::FEVD - Couldn't export a "
1046 "declaration from the modules");
1050 if (
auto copied_function = dyn_cast<clang::FunctionDecl>(copied_decl)) {
1057 }
else if (
auto copied_var = dyn_cast<clang::VarDecl>(copied_decl)) {
1066 if (sym_ctx.
block ==
nullptr)
1077 for (
size_t i = 0; i < vars->GetSize(); i++)
1078 vars->GetVariableAtIndex(i)->GetDecl();
1083 std::vector<CompilerDecl> found_decls =
1087 bool variable_found =
false;
1089 for (
size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) {
1090 VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1091 if (candidate_var->GetDecl() == decl) {
1092 var = candidate_var;
1097 if (var && !variable_found) {
1098 variable_found =
true;
1104 return variable_found;
1110 struct FuncDeclInfo {
1127 std::vector<FuncDeclInfo> decl_infos;
1128 decl_infos.reserve(num_indices);
1129 clang::DeclContext *frame_decl_ctx =
1134 for (
uint32_t index = 0; index < num_indices; ++index) {
1143 sc_sym_list.
Append(sym_ctx);
1150 if (!func_decl_context ||
1154 CompilerType func_clang_type =
function->GetType()->GetFullCompilerType();
1156 if (!copied_func_type) {
1157 sc_sym_list.
Append(sym_ctx);
1161 fdi.m_sym_ctx = sym_ctx;
1162 fdi.m_name =
function->GetName();
1163 fdi.m_copied_type = copied_func_type;
1165 if (fdi.m_copied_type && func_decl_context) {
1170 clang::DeclContext *func_decl_ctx =
1173 &fdi.m_name, &fdi.m_copied_type);
1175 decl_infos.emplace_back(fdi);
1180 std::multimap<CompilerType, const FuncDeclInfo *> matches;
1181 for (
const FuncDeclInfo &fdi : decl_infos) {
1183 auto q = matches.find(t);
1184 if (q != matches.end()) {
1185 if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1188 else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1192 matches.insert(std::make_pair(t, &fdi));
1197 for (
const auto &q : matches)
1198 sc_func_list.
Append(q.second->m_sym_ctx);
1201 sc_func_list.
Append(sc_sym_list);
1202 return sc_func_list;
1213 std::vector<clang::NamedDecl *> decls_from_modules;
1216 if (std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
1218 decl_vendor->FindDecls(name,
false,
UINT32_MAX, decls_from_modules);
1223 if (namespace_decl && module_sp) {
1228 module_sp->FindFunctions(name, namespace_decl, eFunctionNameTypeBase,
1229 function_options, sc_list);
1230 }
else if (target && !namespace_decl) {
1239 name, eFunctionNameTypeFull | eFunctionNameTypeBase, function_options,
1256 if (frame !=
nullptr)
1258 lldb::eSymbolContextBlock);
1264 if (frame_decl_context) {
1270 Symbol *extern_symbol =
nullptr;
1271 Symbol *non_extern_symbol =
nullptr;
1274 index < num_indices; ++index) {
1291 }
else if (sym_ctx.
symbol) {
1294 if (sym_ctx.
symbol ==
nullptr)
1299 extern_symbol = sym_ctx.
symbol;
1301 non_extern_symbol = sym_ctx.
symbol;
1306 for (clang::NamedDecl *decl : decls_from_modules) {
1307 if (llvm::isa<clang::FunctionDecl>(decl)) {
1308 clang::NamedDecl *copied_decl =
1309 llvm::cast_or_null<FunctionDecl>(
CopyDecl(decl));
1319 if (extern_symbol) {
1322 }
else if (non_extern_symbol) {
1344 Target *target =
nullptr;
1351 if (frame !=
nullptr)
1353 lldb::eSymbolContextBlock);
1356 if (!namespace_decl)
1359 if (name.
GetStringRef().startswith(
"$") && !namespace_decl) {
1360 if (name ==
"$__lldb_class") {
1365 if (name ==
"$__lldb_objc_class") {
1382 ExpressionVariableSP pvar_sp(
1391 llvm::StringRef reg_name = name.
GetStringRef().substr(1);
1394 const RegisterInfo *reg_info(
1395 m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(
1399 LLDB_LOG(log,
" CEDM::FEVD Found register {0}", reg_info->name);
1407 bool local_var_lookup = !namespace_decl || (namespace_decl.
GetName() ==
1409 if (frame && local_var_lookup)
1414 ValueObjectSP valobj;
1437 const Symbol *data_symbol =
1440 if (!
error.Success()) {
1441 const unsigned diag_id =
1450 const unsigned diag_id =
1452 clang::DiagnosticsEngine::Level::Warning,
"%0");
1466 Type *var_type = var->GetType();
1469 LLDB_LOG(log,
"Skipped a definition because it has no type");
1475 if (!var_clang_type) {
1476 LLDB_LOG(log,
"Skipped a definition because it has no Clang type");
1484 LLDB_LOG(log,
"Skipped a definition because it has no Clang AST");
1493 if (var->GetLocationIsConstantValueData()) {
1510 "Couldn't copy a variable's type into the parser's AST context");
1523 var->CalculateSymbolContext(&var_sc);
1547 ValueObjectSP valobj) {
1559 clang::QualType parser_opaque_type =
1560 QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1562 if (parser_opaque_type.isNull())
1565 if (
const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
1566 if (
const TagType *tag_type = dyn_cast<TagType>(parser_type))
1568 if (
const ObjCObjectPointerType *objc_object_ptr_type =
1569 dyn_cast<ObjCObjectPointerType>(parser_type))
1570 CompleteType(objc_object_ptr_type->getInterfaceDecl());
1573 bool is_reference = pt.IsReferenceType();
1575 NamedDecl *var_decl =
nullptr;
1579 var_decl = context.
AddVarDecl(pt.GetLValueReferenceType());
1598 LLDB_LOG(log,
" CEDM::FEVD Found variable {1}, returned\n{2} (original {3})",
1603 ExpressionVariableSP &pvar_sp) {
1607 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
1611 if (!parser_type.GetOpaqueQualType()) {
1612 LLDB_LOG(log,
" CEDM::FEVD Couldn't import type for pvar {0}",
1613 pvar_sp->GetName());
1617 NamedDecl *var_decl =
1618 context.
AddVarDecl(parser_type.GetLValueReferenceType());
1620 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1623 llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1629 LLDB_LOG(log,
" CEDM::FEVD Added pvar {1}, returned\n{2}",
1641 if (target ==
nullptr)
1645 if (!scratch_ast_context)
1654 NamedDecl *var_decl = context.
AddVarDecl(parser_type);
1659 m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name,
1681 LLDB_LOG(log,
" CEDM::FEVD Found variable {1}, returned\n{2}", decl_name,
1686 const RegisterInfo *reg_info) {
1691 reg_info->encoding, reg_info->byte_size * 8);
1694 LLDB_LOG(log,
" Tried to add a type for {0}, but couldn't get one",
1701 NamedDecl *var_decl = context.
AddVarDecl(parser_clang_type);
1720 LLDB_LOG(log,
" CEDM::FEVD Added register {1}, returned\n{2}",
1731 NamedDecl *function_decl =
nullptr;
1735 bool is_indirect_function =
false;
1738 Type *function_type =
function->GetType();
1740 const auto lang =
function->GetCompileUnit()->GetLanguage();
1741 const auto name =
function->GetMangled().GetMangledName().AsCString();
1748 TypeSystem *type_system =
function->GetDeclContext().GetTypeSystem();
1749 if (llvm::isa<TypeSystemClang>(type_system)) {
1750 clang::DeclContext *src_decl_context =
1751 (clang::DeclContext *)function->GetDeclContext()
1752 .GetOpaqueDeclContext();
1753 clang::FunctionDecl *src_function_decl =
1754 llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
1755 if (src_function_decl &&
1756 src_function_decl->getTemplateSpecializationInfo()) {
1757 clang::FunctionTemplateDecl *function_template =
1758 src_function_decl->getTemplateSpecializationInfo()->getTemplate();
1759 clang::FunctionTemplateDecl *copied_function_template =
1760 llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
1762 if (copied_function_template) {
1766 function->DumpSymbolContext(&ss);
1769 " CEDM::FEVD Imported decl for function template"
1770 " {1} (description {2}), returned\n{3}",
1771 copied_function_template->getNameAsString(),
1778 }
else if (src_function_decl) {
1779 if (clang::FunctionDecl *copied_function_decl =
1780 llvm::dyn_cast_or_null<clang::FunctionDecl>(
1785 function->DumpSymbolContext(&ss);
1788 " CEDM::FEVD Imported decl for function {1} "
1789 "(description {2}), returned\n{3}",
1790 copied_function_decl->getNameAsString(), ss.
GetData(),
1797 LLDB_LOG(log,
" Failed to import the function decl for '{0}'",
1798 src_function_decl->getName());
1804 if (!function_type) {
1805 LLDB_LOG(log,
" Skipped a function because it has no type");
1811 if (!function_clang_type) {
1812 LLDB_LOG(log,
" Skipped a function because it has no Clang type");
1816 fun_address =
function->GetAddressRange().GetBaseAddress();
1819 if (copied_function_type) {
1820 function_decl = context.
AddFunDecl(copied_function_type, extern_c);
1822 if (!function_decl) {
1823 LLDB_LOG(log,
" Failed to create a function decl for '{0}' ({1:x})",
1831 " Failed to import the function type '{0}' ({1:x})"
1832 " into the expression parser AST contenxt",
1837 }
else if (symbol) {
1842 LLDB_LOG(log,
" AddOneFunction called with no function and no symbol");
1883 fun_address.
Dump(&ss,
1888 " CEDM::FEVD Found {1} function {2} (description {3}), "
1890 (
function ?
"specific" :
"generic"), decl_name, ss.
GetData(),
1901 if (!copied_clang_type) {
1903 "ClangExpressionDeclMap::AddThisType - Couldn't import the type");
1915 void_clang_type, &void_ptr_clang_type, 1,
false, 0);
1917 const bool is_virtual =
false;
1918 const bool is_static =
false;
1919 const bool is_inline =
false;
1920 const bool is_explicit =
false;
1921 const bool is_attr_used =
true;
1922 const bool is_artificial =
false;
1927 is_explicit, is_attr_used, is_artificial);
1930 " CEDM::AddThisType Added function $__lldb_expr "
1931 "(description {0}) for this type\n{1}",
1936 if (!copied_clang_type.
IsValid())
1939 TypeSourceInfo *type_source_info =
m_ast_context->getTrivialTypeSourceInfo(
1942 if (!type_source_info)
1949 TypedefDecl *typedef_decl = TypedefDecl::Create(
1951 SourceLocation(), context.
m_decl_name.getAsIdentifierInfo(),
1964 if (!copied_clang_type) {
1968 "ClangExpressionDeclMap::AddOneType - Couldn't import the type");