Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- ClangExpressionDeclMap.cpp ----------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 9 | #include "ClangExpressionDeclMap.h" |
| 10 | |
Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 11 | #include "ClangASTSource.h" |
| 12 | #include "ClangModulesDeclVendor.h" |
| 13 | #include "ClangPersistentVariables.h" |
Alex Langford | 8be3021 | 2020-01-29 11:59:28 -0800 | [diff] [blame^] | 14 | #include "ClangUtil.h" |
Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 15 | |
Alex Langford | 8be3021 | 2020-01-29 11:59:28 -0800 | [diff] [blame^] | 16 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Address.h" |
| 18 | #include "lldb/Core/Module.h" |
Greg Clayton | 9191db4 | 2013-10-21 18:40:51 +0000 | [diff] [blame] | 19 | #include "lldb/Core/ModuleSpec.h" |
Sean Callanan | e4ec90e | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ValueObjectConstResult.h" |
Sean Callanan | 9b3569b | 2011-12-10 03:12:34 +0000 | [diff] [blame] | 21 | #include "lldb/Core/ValueObjectVariable.h" |
Sean Callanan | 96d2730 | 2013-04-11 00:09:05 +0000 | [diff] [blame] | 22 | #include "lldb/Expression/Materializer.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/CompileUnit.h" |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/CompilerDecl.h" |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 25 | #include "lldb/Symbol/CompilerDeclContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | #include "lldb/Symbol/Function.h" |
| 27 | #include "lldb/Symbol/ObjectFile.h" |
| 28 | #include "lldb/Symbol/SymbolContext.h" |
Siva Chandra | 9293fc4 | 2016-01-07 23:32:34 +0000 | [diff] [blame] | 29 | #include "lldb/Symbol/SymbolFile.h" |
Sean Callanan | 503aa52 | 2011-10-12 00:12:34 +0000 | [diff] [blame] | 30 | #include "lldb/Symbol/SymbolVendor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "lldb/Symbol/Type.h" |
| 32 | #include "lldb/Symbol/TypeList.h" |
| 33 | #include "lldb/Symbol/Variable.h" |
| 34 | #include "lldb/Symbol/VariableList.h" |
Sean Callanan | 1d18066 | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 35 | #include "lldb/Target/ExecutionContext.h" |
Sean Callanan | ea22d42 | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 36 | #include "lldb/Target/Process.h" |
Sean Callanan | f4b9bd3 | 2010-10-05 20:18:48 +0000 | [diff] [blame] | 37 | #include "lldb/Target/RegisterContext.h" |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 38 | #include "lldb/Target/StackFrame.h" |
Sean Callanan | 1d18066 | 2010-07-20 23:31:16 +0000 | [diff] [blame] | 39 | #include "lldb/Target/Target.h" |
Jim Ingham | 895c982 | 2010-12-07 01:56:02 +0000 | [diff] [blame] | 40 | #include "lldb/Target/Thread.h" |
Zachary Turner | 01c3243 | 2017-02-14 19:06:07 +0000 | [diff] [blame] | 41 | #include "lldb/Utility/Endian.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 42 | #include "lldb/Utility/Log.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 43 | #include "lldb/Utility/RegisterValue.h" |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 44 | #include "lldb/Utility/Status.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | #include "lldb/lldb-private.h" |
| 46 | #include "clang/AST/ASTConsumer.h" |
| 47 | #include "clang/AST/ASTContext.h" |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 48 | #include "clang/AST/ASTImporter.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | #include "clang/AST/Decl.h" |
| 50 | #include "clang/AST/DeclarationName.h" |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 51 | #include "clang/AST/RecursiveASTVisitor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | |
Siva Chandra | 0f4873d | 2015-09-03 23:27:10 +0000 | [diff] [blame] | 53 | #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" |
Alex Langford | e0678ca | 2019-07-12 20:09:32 +0000 | [diff] [blame] | 54 | #include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h" |
Alex Langford | b5701710 | 2019-07-15 22:56:12 +0000 | [diff] [blame] | 55 | #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" |
Siva Chandra | 0f4873d | 2015-09-03 23:27:10 +0000 | [diff] [blame] | 56 | |
Greg Clayton | 83c5cd9 | 2010-11-14 22:13:40 +0000 | [diff] [blame] | 57 | using namespace lldb; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | using namespace lldb_private; |
| 59 | using namespace clang; |
| 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | namespace { |
| 62 | const char *g_lldb_local_vars_namespace_cstr = "$__lldb_local_vars"; |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 63 | } // anonymous namespace |
| 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | ClangExpressionDeclMap::ClangExpressionDeclMap( |
| 66 | bool keep_result_in_memory, |
| 67 | Materializer::PersistentVariableDelegate *result_delegate, |
Raphael Isemann | 4aee81c | 2019-12-17 13:38:07 +0100 | [diff] [blame] | 68 | const lldb::TargetSP &target, const lldb::ClangASTImporterSP &importer, |
| 69 | ValueObject *ctx_obj) |
| 70 | : ClangASTSource(target, importer), m_found_entities(), m_struct_members(), |
| 71 | m_keep_result_in_memory(keep_result_in_memory), |
Krasimir Georgiev | 7ffc468 | 2019-02-05 11:35:45 +0000 | [diff] [blame] | 72 | m_result_delegate(result_delegate), m_ctx_obj(ctx_obj), m_parser_vars(), |
| 73 | m_struct_vars() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | EnableStructVars(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | ClangExpressionDeclMap::~ClangExpressionDeclMap() { |
| 78 | // Note: The model is now that the parser's AST context and all associated |
| 79 | // data does not vanish until the expression has been executed. This means |
| 80 | // that valuable lookup data (like namespaces) doesn't vanish, but |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | DidParse(); |
| 83 | DisableStructVars(); |
Sean Callanan | 979f74d | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 84 | } |
Sean Callanan | be3a1b1 | 2010-10-26 00:31:56 +0000 | [diff] [blame] | 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | bool ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx, |
| 87 | Materializer *materializer) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | EnableParserVars(); |
| 89 | m_parser_vars->m_exe_ctx = exe_ctx; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | Target *target = exe_ctx.GetTargetPtr(); |
| 92 | if (exe_ctx.GetFramePtr()) |
| 93 | m_parser_vars->m_sym_ctx = |
| 94 | exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything); |
| 95 | else if (exe_ctx.GetThreadPtr() && |
| 96 | exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)) |
| 97 | m_parser_vars->m_sym_ctx = |
| 98 | exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext( |
| 99 | lldb::eSymbolContextEverything); |
| 100 | else if (exe_ctx.GetProcessPtr()) { |
| 101 | m_parser_vars->m_sym_ctx.Clear(true); |
| 102 | m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP(); |
| 103 | } else if (target) { |
| 104 | m_parser_vars->m_sym_ctx.Clear(true); |
| 105 | m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP(); |
| 106 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | if (target) { |
| 109 | m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>( |
| 110 | target->GetPersistentExpressionStateForLanguage(eLanguageTypeC)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 111 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 112 | if (!TypeSystemClang::GetScratch(*target)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | return false; |
| 114 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | m_parser_vars->m_target_info = GetTargetInfo(); |
| 117 | m_parser_vars->m_materializer = materializer; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | return true; |
Sean Callanan | 979f74d | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | void ClangExpressionDeclMap::InstallCodeGenerator( |
| 123 | clang::ASTConsumer *code_gen) { |
| 124 | assert(m_parser_vars); |
| 125 | m_parser_vars->m_code_gen = code_gen; |
Sean Callanan | 80c9759 | 2015-05-01 00:47:29 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | void ClangExpressionDeclMap::DidParse() { |
Adrian Prantl | e9331a5 | 2020-01-08 14:18:47 -0800 | [diff] [blame] | 129 | if (m_parser_vars && m_parser_vars->m_persistent_vars) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | for (size_t entity_index = 0, num_entities = m_found_entities.GetSize(); |
| 131 | entity_index < num_entities; ++entity_index) { |
| 132 | ExpressionVariableSP var_sp( |
| 133 | m_found_entities.GetVariableAtIndex(entity_index)); |
| 134 | if (var_sp) |
| 135 | llvm::cast<ClangExpressionVariable>(var_sp.get()) |
| 136 | ->DisableParserVars(GetParserID()); |
Sean Callanan | 6b1b953 | 2010-10-08 01:58:41 +0000 | [diff] [blame] | 137 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | |
| 139 | for (size_t pvar_index = 0, |
| 140 | num_pvars = m_parser_vars->m_persistent_vars->GetSize(); |
| 141 | pvar_index < num_pvars; ++pvar_index) { |
| 142 | ExpressionVariableSP pvar_sp( |
| 143 | m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index)); |
| 144 | if (ClangExpressionVariable *clang_var = |
| 145 | llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get())) |
| 146 | clang_var->DisableParserVars(GetParserID()); |
| 147 | } |
| 148 | |
| 149 | DisableParserVars(); |
| 150 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Sean Callanan | 549c9f7 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 153 | // Interface for IRForTarget |
| 154 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | ClangExpressionDeclMap::TargetInfo ClangExpressionDeclMap::GetTargetInfo() { |
| 156 | assert(m_parser_vars.get()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 157 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | TargetInfo ret; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; |
Sean Callanan | 933693b | 2012-02-10 01:22:05 +0000 | [diff] [blame] | 161 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | Process *process = exe_ctx.GetProcessPtr(); |
| 163 | if (process) { |
| 164 | ret.byte_order = process->GetByteOrder(); |
| 165 | ret.address_byte_size = process->GetAddressByteSize(); |
| 166 | } else { |
| 167 | Target *target = exe_ctx.GetTargetPtr(); |
| 168 | if (target) { |
| 169 | ret.byte_order = target->GetArchitecture().GetByteOrder(); |
| 170 | ret.address_byte_size = target->GetArchitecture().GetAddressByteSize(); |
Sean Callanan | 933693b | 2012-02-10 01:22:05 +0000 | [diff] [blame] | 171 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | } |
Sean Callanan | 933693b | 2012-02-10 01:22:05 +0000 | [diff] [blame] | 173 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | return ret; |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 177 | TypeFromUser ClangExpressionDeclMap::DeportType(TypeSystemClang &target, |
| 178 | TypeSystemClang &source, |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 179 | TypeFromParser parser_type) { |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 180 | assert(&target == TypeSystemClang::GetScratch(*m_target)); |
Jonas Devlieghere | 50a3f0e | 2019-07-23 21:14:23 +0000 | [diff] [blame] | 181 | assert((TypeSystem *)&source == parser_type.GetTypeSystem()); |
Raphael Isemann | f9f49d3 | 2019-12-21 22:40:52 +0100 | [diff] [blame] | 182 | assert(&source.getASTContext() == m_ast_context); |
Jonas Devlieghere | 50a3f0e | 2019-07-23 21:14:23 +0000 | [diff] [blame] | 183 | |
Raphael Isemann | 1ccc702 | 2020-01-31 13:00:32 +0100 | [diff] [blame] | 184 | return TypeFromUser(m_ast_importer_sp->DeportType(target, parser_type)); |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | bool ClangExpressionDeclMap::AddPersistentVariable(const NamedDecl *decl, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 188 | ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | TypeFromParser parser_type, |
| 190 | bool is_result, |
| 191 | bool is_lvalue) { |
| 192 | assert(m_parser_vars.get()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 193 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 194 | TypeSystemClang *ast = |
| 195 | llvm::dyn_cast_or_null<TypeSystemClang>(parser_type.GetTypeSystem()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | if (ast == nullptr) |
| 197 | return false; |
Greg Clayton | f73034f | 2015-09-08 18:15:05 +0000 | [diff] [blame] | 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | if (m_parser_vars->m_materializer && is_result) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 200 | Status err; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 201 | |
Sean Callanan | 933693b | 2012-02-10 01:22:05 +0000 | [diff] [blame] | 202 | ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; |
| 203 | Target *target = exe_ctx.GetTargetPtr(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 204 | if (target == nullptr) |
| 205 | return false; |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 206 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 207 | auto *clang_ast_context = TypeSystemClang::GetScratch(*target); |
Alex Langford | 3031818 | 2019-11-14 13:41:52 -0800 | [diff] [blame] | 208 | if (!clang_ast_context) |
| 209 | return false; |
| 210 | |
| 211 | TypeFromUser user_type = DeportType(*clang_ast_context, *ast, parser_type); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 212 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | uint32_t offset = m_parser_vars->m_materializer->AddResultVariable( |
| 214 | user_type, is_lvalue, m_keep_result_in_memory, m_result_delegate, err); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 215 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | ClangExpressionVariable *var = new ClangExpressionVariable( |
| 217 | exe_ctx.GetBestExecutionContextScope(), name, user_type, |
| 218 | m_parser_vars->m_target_info.byte_order, |
| 219 | m_parser_vars->m_target_info.address_byte_size); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 220 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | m_found_entities.AddNewlyConstructedVariable(var); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 222 | |
Sean Callanan | bc8ac34 | 2015-09-04 20:49:51 +0000 | [diff] [blame] | 223 | var->EnableParserVars(GetParserID()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | ClangExpressionVariable::ParserVars *parser_vars = |
| 226 | var->GetParserVars(GetParserID()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 227 | |
Sean Callanan | 3c495c1 | 2013-01-15 23:29:36 +0000 | [diff] [blame] | 228 | parser_vars->m_named_decl = decl; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 230 | var->EnableJITVars(GetParserID()); |
| 231 | |
| 232 | ClangExpressionVariable::JITVars *jit_vars = var->GetJITVars(GetParserID()); |
| 233 | |
| 234 | jit_vars->m_offset = offset; |
| 235 | |
Sean Callanan | 64dfc9a | 2010-08-23 23:09:38 +0000 | [diff] [blame] | 236 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 240 | ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; |
| 241 | Target *target = exe_ctx.GetTargetPtr(); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 242 | if (target == nullptr) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | return false; |
| 244 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 245 | TypeSystemClang *context = TypeSystemClang::GetScratch(*target); |
Alex Langford | 3031818 | 2019-11-14 13:41:52 -0800 | [diff] [blame] | 246 | if (!context) |
| 247 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 249 | TypeFromUser user_type = DeportType(*context, *ast, parser_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | |
| 251 | if (!user_type.GetOpaqueQualType()) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 252 | LLDB_LOGF(log, "Persistent variable's type wasn't copied successfully"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | |
| 256 | if (!m_parser_vars->m_target_info.IsValid()) |
| 257 | return false; |
| 258 | |
Adrian Prantl | e9331a5 | 2020-01-08 14:18:47 -0800 | [diff] [blame] | 259 | if (!m_parser_vars->m_persistent_vars) |
| 260 | return false; |
| 261 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 262 | ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>( |
| 263 | m_parser_vars->m_persistent_vars |
| 264 | ->CreatePersistentVariable( |
| 265 | exe_ctx.GetBestExecutionContextScope(), name, user_type, |
| 266 | m_parser_vars->m_target_info.byte_order, |
| 267 | m_parser_vars->m_target_info.address_byte_size) |
| 268 | .get()); |
| 269 | |
| 270 | if (!var) |
| 271 | return false; |
| 272 | |
| 273 | var->m_frozen_sp->SetHasCompleteType(); |
| 274 | |
| 275 | if (is_result) |
| 276 | var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry; |
| 277 | else |
| 278 | var->m_flags |= |
| 279 | ClangExpressionVariable::EVKeepInTarget; // explicitly-declared |
| 280 | // persistent variables should |
| 281 | // persist |
| 282 | |
| 283 | if (is_lvalue) { |
| 284 | var->m_flags |= ClangExpressionVariable::EVIsProgramReference; |
| 285 | } else { |
| 286 | var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated; |
| 287 | var->m_flags |= ClangExpressionVariable::EVNeedsAllocation; |
| 288 | } |
| 289 | |
| 290 | if (m_keep_result_in_memory) { |
| 291 | var->m_flags |= ClangExpressionVariable::EVKeepInTarget; |
| 292 | } |
| 293 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 294 | LLDB_LOGF(log, "Created persistent variable with flags 0x%hx", var->m_flags); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 295 | |
| 296 | var->EnableParserVars(GetParserID()); |
| 297 | |
| 298 | ClangExpressionVariable::ParserVars *parser_vars = |
| 299 | var->GetParserVars(GetParserID()); |
| 300 | |
| 301 | parser_vars->m_named_decl = decl; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 302 | |
| 303 | return true; |
Sean Callanan | 2235f32 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 306 | bool ClangExpressionDeclMap::AddValueToStruct(const NamedDecl *decl, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 307 | ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 308 | llvm::Value *value, size_t size, |
| 309 | lldb::offset_t alignment) { |
| 310 | assert(m_struct_vars.get()); |
| 311 | assert(m_parser_vars.get()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 312 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | bool is_persistent_variable = false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 314 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 315 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 316 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 317 | m_struct_vars->m_struct_laid_out = false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 318 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 319 | if (ClangExpressionVariable::FindVariableInList(m_struct_members, decl, |
| 320 | GetParserID())) |
| 321 | return true; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 322 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 323 | ClangExpressionVariable *var(ClangExpressionVariable::FindVariableInList( |
| 324 | m_found_entities, decl, GetParserID())); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 325 | |
Adrian Prantl | e9331a5 | 2020-01-08 14:18:47 -0800 | [diff] [blame] | 326 | if (!var && m_parser_vars->m_persistent_vars) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 327 | var = ClangExpressionVariable::FindVariableInList( |
| 328 | *m_parser_vars->m_persistent_vars, decl, GetParserID()); |
| 329 | is_persistent_variable = true; |
| 330 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 331 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 332 | if (!var) |
| 333 | return false; |
| 334 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 335 | LLDB_LOGF(log, "Adding value for (NamedDecl*)%p [%s - %s] to the structure", |
| 336 | static_cast<const void *>(decl), name.GetCString(), |
| 337 | var->GetName().GetCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 338 | |
| 339 | // We know entity->m_parser_vars is valid because we used a parser variable |
| 340 | // to find it |
| 341 | |
| 342 | ClangExpressionVariable::ParserVars *parser_vars = |
| 343 | llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID()); |
| 344 | |
| 345 | parser_vars->m_llvm_value = value; |
| 346 | |
| 347 | if (ClangExpressionVariable::JITVars *jit_vars = |
| 348 | llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID())) { |
| 349 | // We already laid this out; do not touch |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 350 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 351 | LLDB_LOGF(log, "Already placed at 0x%llx", |
| 352 | (unsigned long long)jit_vars->m_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 354 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 355 | llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 356 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | ClangExpressionVariable::JITVars *jit_vars = |
| 358 | llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID()); |
Sean Callanan | 3c495c1 | 2013-01-15 23:29:36 +0000 | [diff] [blame] | 359 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 360 | jit_vars->m_alignment = alignment; |
| 361 | jit_vars->m_size = size; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 362 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 363 | m_struct_members.AddVariable(var->shared_from_this()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 364 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 365 | if (m_parser_vars->m_materializer) { |
| 366 | uint32_t offset = 0; |
| 367 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 368 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 369 | |
| 370 | if (is_persistent_variable) { |
| 371 | ExpressionVariableSP var_sp(var->shared_from_this()); |
| 372 | offset = m_parser_vars->m_materializer->AddPersistentVariable( |
| 373 | var_sp, nullptr, err); |
| 374 | } else { |
| 375 | if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym) |
| 376 | offset = m_parser_vars->m_materializer->AddSymbol(*sym, err); |
| 377 | else if (const RegisterInfo *reg_info = var->GetRegisterInfo()) |
| 378 | offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err); |
| 379 | else if (parser_vars->m_lldb_var) |
| 380 | offset = m_parser_vars->m_materializer->AddVariable( |
| 381 | parser_vars->m_lldb_var, err); |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 382 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 383 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 384 | if (!err.Success()) |
| 385 | return false; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 386 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 387 | LLDB_LOGF(log, "Placed at 0x%llx", (unsigned long long)offset); |
Sean Callanan | 3c495c1 | 2013-01-15 23:29:36 +0000 | [diff] [blame] | 388 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 389 | jit_vars->m_offset = |
| 390 | offset; // TODO DoStructLayout() should not change this. |
| 391 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 392 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 393 | return true; |
| 394 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 395 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 396 | bool ClangExpressionDeclMap::DoStructLayout() { |
| 397 | assert(m_struct_vars.get()); |
Sean Callanan | 3dd6a42 | 2013-04-11 21:16:36 +0000 | [diff] [blame] | 398 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 399 | if (m_struct_vars->m_struct_laid_out) |
| 400 | return true; |
Sean Callanan | df66765 | 2013-04-11 02:05:11 +0000 | [diff] [blame] | 401 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 402 | if (!m_parser_vars->m_materializer) |
| 403 | return false; |
| 404 | |
| 405 | m_struct_vars->m_struct_alignment = |
| 406 | m_parser_vars->m_materializer->GetStructAlignment(); |
| 407 | m_struct_vars->m_struct_size = |
| 408 | m_parser_vars->m_materializer->GetStructByteSize(); |
| 409 | m_struct_vars->m_struct_laid_out = true; |
| 410 | return true; |
| 411 | } |
| 412 | |
| 413 | bool ClangExpressionDeclMap::GetStructInfo(uint32_t &num_elements, size_t &size, |
| 414 | lldb::offset_t &alignment) { |
| 415 | assert(m_struct_vars.get()); |
| 416 | |
| 417 | if (!m_struct_vars->m_struct_laid_out) |
| 418 | return false; |
| 419 | |
| 420 | num_elements = m_struct_members.GetSize(); |
| 421 | size = m_struct_vars->m_struct_size; |
| 422 | alignment = m_struct_vars->m_struct_alignment; |
| 423 | |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | bool ClangExpressionDeclMap::GetStructElement(const NamedDecl *&decl, |
| 428 | llvm::Value *&value, |
| 429 | lldb::offset_t &offset, |
| 430 | ConstString &name, |
| 431 | uint32_t index) { |
| 432 | assert(m_struct_vars.get()); |
| 433 | |
| 434 | if (!m_struct_vars->m_struct_laid_out) |
| 435 | return false; |
| 436 | |
| 437 | if (index >= m_struct_members.GetSize()) |
| 438 | return false; |
| 439 | |
| 440 | ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index)); |
| 441 | |
| 442 | if (!member_sp) |
| 443 | return false; |
| 444 | |
| 445 | ClangExpressionVariable::ParserVars *parser_vars = |
| 446 | llvm::cast<ClangExpressionVariable>(member_sp.get()) |
| 447 | ->GetParserVars(GetParserID()); |
| 448 | ClangExpressionVariable::JITVars *jit_vars = |
| 449 | llvm::cast<ClangExpressionVariable>(member_sp.get()) |
| 450 | ->GetJITVars(GetParserID()); |
| 451 | |
| 452 | if (!parser_vars || !jit_vars || !member_sp->GetValueObject()) |
| 453 | return false; |
| 454 | |
| 455 | decl = parser_vars->m_named_decl; |
| 456 | value = parser_vars->m_llvm_value; |
| 457 | offset = jit_vars->m_offset; |
| 458 | name = member_sp->GetName(); |
| 459 | |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | bool ClangExpressionDeclMap::GetFunctionInfo(const NamedDecl *decl, |
| 464 | uint64_t &ptr) { |
| 465 | ClangExpressionVariable *entity(ClangExpressionVariable::FindVariableInList( |
| 466 | m_found_entities, decl, GetParserID())); |
| 467 | |
| 468 | if (!entity) |
| 469 | return false; |
| 470 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 471 | // We know m_parser_vars is valid since we searched for the variable by its |
| 472 | // NamedDecl |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 473 | |
| 474 | ClangExpressionVariable::ParserVars *parser_vars = |
| 475 | entity->GetParserVars(GetParserID()); |
| 476 | |
| 477 | ptr = parser_vars->m_lldb_value.GetScalar().ULongLong(); |
| 478 | |
| 479 | return true; |
| 480 | } |
| 481 | |
| 482 | addr_t ClangExpressionDeclMap::GetSymbolAddress(Target &target, |
| 483 | Process *process, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 484 | ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 485 | lldb::SymbolType symbol_type, |
| 486 | lldb_private::Module *module) { |
| 487 | SymbolContextList sc_list; |
| 488 | |
| 489 | if (module) |
| 490 | module->FindSymbolsWithNameAndType(name, symbol_type, sc_list); |
| 491 | else |
| 492 | target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list); |
| 493 | |
| 494 | const uint32_t num_matches = sc_list.GetSize(); |
| 495 | addr_t symbol_load_addr = LLDB_INVALID_ADDRESS; |
| 496 | |
| 497 | for (uint32_t i = 0; |
| 498 | i < num_matches && |
| 499 | (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); |
| 500 | i++) { |
| 501 | SymbolContext sym_ctx; |
| 502 | sc_list.GetContextAtIndex(i, sym_ctx); |
| 503 | |
| 504 | const Address sym_address = sym_ctx.symbol->GetAddress(); |
| 505 | |
| 506 | if (!sym_address.IsValid()) |
| 507 | continue; |
| 508 | |
| 509 | switch (sym_ctx.symbol->GetType()) { |
| 510 | case eSymbolTypeCode: |
| 511 | case eSymbolTypeTrampoline: |
| 512 | symbol_load_addr = sym_address.GetCallableLoadAddress(&target); |
| 513 | break; |
| 514 | |
| 515 | case eSymbolTypeResolver: |
| 516 | symbol_load_addr = sym_address.GetCallableLoadAddress(&target, true); |
| 517 | break; |
| 518 | |
| 519 | case eSymbolTypeReExported: { |
| 520 | ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName(); |
| 521 | if (reexport_name) { |
| 522 | ModuleSP reexport_module_sp; |
| 523 | ModuleSpec reexport_module_spec; |
| 524 | reexport_module_spec.GetPlatformFileSpec() = |
| 525 | sym_ctx.symbol->GetReExportedSymbolSharedLibrary(); |
| 526 | if (reexport_module_spec.GetPlatformFileSpec()) { |
| 527 | reexport_module_sp = |
| 528 | target.GetImages().FindFirstModule(reexport_module_spec); |
| 529 | if (!reexport_module_sp) { |
| 530 | reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear(); |
| 531 | reexport_module_sp = |
| 532 | target.GetImages().FindFirstModule(reexport_module_spec); |
| 533 | } |
Sean Callanan | df66765 | 2013-04-11 02:05:11 +0000 | [diff] [blame] | 534 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 535 | symbol_load_addr = GetSymbolAddress( |
| 536 | target, process, sym_ctx.symbol->GetReExportedSymbolName(), |
| 537 | symbol_type, reexport_module_sp.get()); |
| 538 | } |
| 539 | } break; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 540 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 541 | case eSymbolTypeData: |
| 542 | case eSymbolTypeRuntime: |
| 543 | case eSymbolTypeVariable: |
| 544 | case eSymbolTypeLocal: |
| 545 | case eSymbolTypeParam: |
| 546 | case eSymbolTypeInvalid: |
| 547 | case eSymbolTypeAbsolute: |
| 548 | case eSymbolTypeException: |
| 549 | case eSymbolTypeSourceFile: |
| 550 | case eSymbolTypeHeaderFile: |
| 551 | case eSymbolTypeObjectFile: |
| 552 | case eSymbolTypeCommonBlock: |
| 553 | case eSymbolTypeBlock: |
| 554 | case eSymbolTypeVariableType: |
| 555 | case eSymbolTypeLineEntry: |
| 556 | case eSymbolTypeLineHeader: |
| 557 | case eSymbolTypeScopeBegin: |
| 558 | case eSymbolTypeScopeEnd: |
| 559 | case eSymbolTypeAdditional: |
| 560 | case eSymbolTypeCompiler: |
| 561 | case eSymbolTypeInstrumentation: |
| 562 | case eSymbolTypeUndefined: |
| 563 | case eSymbolTypeObjCClass: |
| 564 | case eSymbolTypeObjCMetaClass: |
| 565 | case eSymbolTypeObjCIVar: |
| 566 | symbol_load_addr = sym_address.GetLoadAddress(&target); |
| 567 | break; |
Sean Callanan | df66765 | 2013-04-11 02:05:11 +0000 | [diff] [blame] | 568 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 569 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 570 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 571 | if (symbol_load_addr == LLDB_INVALID_ADDRESS && process) { |
Alex Langford | e823bbe | 2019-06-10 20:53:23 +0000 | [diff] [blame] | 572 | ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process); |
Sean Callanan | 549c9f7 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 573 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 574 | if (runtime) { |
| 575 | symbol_load_addr = runtime->LookupRuntimeSymbol(name); |
Greg Clayton | 084db10 | 2011-06-23 04:25:29 +0000 | [diff] [blame] | 576 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 577 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 578 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 579 | return symbol_load_addr; |
Sean Callanan | c3a1600 | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 582 | addr_t ClangExpressionDeclMap::GetSymbolAddress(ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 583 | lldb::SymbolType symbol_type) { |
| 584 | assert(m_parser_vars.get()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 585 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 586 | if (!m_parser_vars->m_exe_ctx.GetTargetPtr()) |
| 587 | return false; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 588 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 589 | return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(), |
| 590 | m_parser_vars->m_exe_ctx.GetProcessPtr(), name, |
| 591 | symbol_type); |
Sean Callanan | d9ca42a | 2011-05-08 02:21:26 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 594 | lldb::VariableSP ClangExpressionDeclMap::FindGlobalVariable( |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 595 | Target &target, ModuleSP &module, ConstString name, |
Raphael Isemann | b6c29d9 | 2019-12-23 12:18:49 +0100 | [diff] [blame] | 596 | CompilerDeclContext *namespace_decl) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 597 | VariableList vars; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 598 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 599 | if (module && namespace_decl) |
Pavel Labath | 34cda14 | 2018-05-31 09:46:26 +0000 | [diff] [blame] | 600 | module->FindGlobalVariables(name, namespace_decl, -1, vars); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 601 | else |
Pavel Labath | 34cda14 | 2018-05-31 09:46:26 +0000 | [diff] [blame] | 602 | target.GetImages().FindGlobalVariables(name, -1, vars); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 603 | |
Raphael Isemann | b6c29d9 | 2019-12-23 12:18:49 +0100 | [diff] [blame] | 604 | if (vars.GetSize() == 0) |
| 605 | return VariableSP(); |
| 606 | return vars.GetVariableAtIndex(0); |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 609 | TypeSystemClang *ClangExpressionDeclMap::GetTypeSystemClang() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 610 | StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); |
| 611 | if (frame == nullptr) |
| 612 | return nullptr; |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 613 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 614 | SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | |
| 615 | lldb::eSymbolContextBlock); |
| 616 | if (sym_ctx.block == nullptr) |
| 617 | return nullptr; |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 618 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 619 | CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext(); |
| 620 | if (!frame_decl_context) |
| 621 | return nullptr; |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 622 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 623 | return llvm::dyn_cast_or_null<TypeSystemClang>( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 624 | frame_decl_context.GetTypeSystem()); |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 627 | // Interface for ClangASTSource |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 628 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 629 | void ClangExpressionDeclMap::FindExternalVisibleDecls( |
| 630 | NameSearchContext &context) { |
| 631 | assert(m_ast_context); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 632 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 633 | const ConstString name(context.m_decl_name.getAsString().c_str()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 634 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 635 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 636 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 637 | if (GetImportInProgress()) { |
| 638 | if (log && log->GetVerbose()) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 639 | LLDB_LOGF(log, "Ignoring a query during an import"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 640 | return; |
| 641 | } |
| 642 | |
| 643 | static unsigned int invocation_id = 0; |
| 644 | unsigned int current_id = invocation_id++; |
| 645 | |
| 646 | if (log) { |
| 647 | if (!context.m_decl_context) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 648 | LLDB_LOGF(log, |
| 649 | "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for " |
| 650 | "'%s' in a NULL DeclContext", |
| 651 | current_id, name.GetCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 652 | else if (const NamedDecl *context_named_decl = |
| 653 | dyn_cast<NamedDecl>(context.m_decl_context)) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 654 | LLDB_LOGF(log, |
| 655 | "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for " |
| 656 | "'%s' in '%s'", |
| 657 | current_id, name.GetCString(), |
| 658 | context_named_decl->getNameAsString().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 659 | else |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 660 | LLDB_LOGF(log, |
| 661 | "ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for " |
| 662 | "'%s' in a '%s'", |
| 663 | current_id, name.GetCString(), |
| 664 | context.m_decl_context->getDeclKindName()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | if (const NamespaceDecl *namespace_context = |
| 668 | dyn_cast<NamespaceDecl>(context.m_decl_context)) { |
| 669 | if (namespace_context->getName().str() == |
| 670 | std::string(g_lldb_local_vars_namespace_cstr)) { |
Raphael Isemann | 73951a1 | 2019-12-23 10:55:21 +0100 | [diff] [blame] | 671 | CompilerDeclContext compiler_decl_ctx = |
| 672 | m_clang_ast_context->CreateDeclContext( |
| 673 | const_cast<clang::DeclContext *>(context.m_decl_context)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 674 | FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx, |
| 675 | current_id); |
| 676 | return; |
Sean Callanan | 6abfabf | 2010-11-19 20:20:02 +0000 | [diff] [blame] | 677 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 678 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 679 | ClangASTImporter::NamespaceMapSP namespace_map = |
Raphael Isemann | 1ccc702 | 2020-01-31 13:00:32 +0100 | [diff] [blame] | 680 | m_ast_importer_sp->GetNamespaceMap(namespace_context); |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 681 | |
| 682 | if (!namespace_map) |
| 683 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 684 | |
| 685 | if (log && log->GetVerbose()) |
| 686 | log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)", |
| 687 | current_id, static_cast<void *>(namespace_map.get()), |
| 688 | (int)namespace_map->size()); |
Jonas Devlieghere | 50a3f0e | 2019-07-23 21:14:23 +0000 | [diff] [blame] | 689 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 690 | for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), |
| 691 | e = namespace_map->end(); |
| 692 | i != e; ++i) { |
| 693 | if (log) |
| 694 | log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s", |
| 695 | current_id, i->second.GetName().AsCString(), |
| 696 | i->first->GetFileSpec().GetFilename().GetCString()); |
| 697 | |
| 698 | FindExternalVisibleDecls(context, i->first, i->second, current_id); |
| 699 | } |
| 700 | } else if (isa<TranslationUnitDecl>(context.m_decl_context)) { |
| 701 | CompilerDeclContext namespace_decl; |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 702 | |
Sean Callanan | 503aa52 | 2011-10-12 00:12:34 +0000 | [diff] [blame] | 703 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 704 | log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 705 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 706 | FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl, |
| 707 | current_id); |
| 708 | } |
Jonas Devlieghere | 50a3f0e | 2019-07-23 21:14:23 +0000 | [diff] [blame] | 709 | |
Sean Callanan | c02a1c0 | 2017-04-24 23:14:04 +0000 | [diff] [blame] | 710 | ClangASTSource::FindExternalVisibleDecls(context); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Raphael Isemann | c34478f | 2019-11-20 13:41:44 +0100 | [diff] [blame] | 713 | void ClangExpressionDeclMap::MaybeRegisterFunctionBody( |
| 714 | FunctionDecl *copied_function_decl) { |
| 715 | if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) { |
| 716 | clang::DeclGroupRef decl_group_ref(copied_function_decl); |
| 717 | m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref); |
| 718 | } |
| 719 | } |
| 720 | |
Raphael Isemann | d8a3194 | 2019-12-17 16:12:07 +0100 | [diff] [blame] | 721 | clang::NamedDecl *ClangExpressionDeclMap::GetPersistentDecl(ConstString name) { |
Raphael Isemann | 4aee81c | 2019-12-17 13:38:07 +0100 | [diff] [blame] | 722 | if (!m_parser_vars) |
Raphael Isemann | d8a3194 | 2019-12-17 16:12:07 +0100 | [diff] [blame] | 723 | return nullptr; |
Raphael Isemann | c34478f | 2019-11-20 13:41:44 +0100 | [diff] [blame] | 724 | Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); |
| 725 | if (!target) |
Raphael Isemann | d8a3194 | 2019-12-17 16:12:07 +0100 | [diff] [blame] | 726 | return nullptr; |
Raphael Isemann | c34478f | 2019-11-20 13:41:44 +0100 | [diff] [blame] | 727 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 728 | TypeSystemClang::GetScratch(*target); |
Raphael Isemann | c34478f | 2019-11-20 13:41:44 +0100 | [diff] [blame] | 729 | |
Adrian Prantl | e9331a5 | 2020-01-08 14:18:47 -0800 | [diff] [blame] | 730 | if (!m_parser_vars->m_persistent_vars) |
| 731 | return nullptr; |
Raphael Isemann | d8a3194 | 2019-12-17 16:12:07 +0100 | [diff] [blame] | 732 | return m_parser_vars->m_persistent_vars->GetPersistentDecl(name); |
| 733 | } |
| 734 | |
| 735 | void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context, |
| 736 | const ConstString name, |
| 737 | unsigned int current_id) { |
| 738 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 739 | |
| 740 | NamedDecl *persistent_decl = GetPersistentDecl(name); |
Raphael Isemann | c34478f | 2019-11-20 13:41:44 +0100 | [diff] [blame] | 741 | |
| 742 | if (!persistent_decl) |
| 743 | return; |
| 744 | |
| 745 | Decl *parser_persistent_decl = CopyDecl(persistent_decl); |
| 746 | |
| 747 | if (!parser_persistent_decl) |
| 748 | return; |
| 749 | |
| 750 | NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl); |
| 751 | |
| 752 | if (!parser_named_decl) |
| 753 | return; |
| 754 | |
| 755 | if (clang::FunctionDecl *parser_function_decl = |
| 756 | llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) { |
| 757 | MaybeRegisterFunctionBody(parser_function_decl); |
| 758 | } |
| 759 | |
| 760 | LLDB_LOGF(log, " CEDM::FEVD[%u] Found persistent decl %s", current_id, |
| 761 | name.GetCString()); |
| 762 | |
| 763 | context.AddNamedDecl(parser_named_decl); |
| 764 | } |
Raphael Isemann | e7cc833 | 2019-11-20 14:35:18 +0100 | [diff] [blame] | 765 | |
| 766 | void ClangExpressionDeclMap::LookUpLldbClass(NameSearchContext &context, |
| 767 | unsigned int current_id) { |
| 768 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 769 | |
| 770 | StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); |
| 771 | SymbolContext sym_ctx; |
| 772 | if (frame != nullptr) |
| 773 | sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | |
| 774 | lldb::eSymbolContextBlock); |
| 775 | |
| 776 | if (m_ctx_obj) { |
| 777 | Status status; |
| 778 | lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status); |
| 779 | if (!ctx_obj_ptr || status.Fail()) |
| 780 | return; |
| 781 | |
| 782 | AddThisType(context, TypeFromUser(m_ctx_obj->GetCompilerType()), |
| 783 | current_id); |
| 784 | |
| 785 | m_struct_vars->m_object_pointer_type = |
| 786 | TypeFromUser(ctx_obj_ptr->GetCompilerType()); |
| 787 | |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | // Clang is looking for the type of "this" |
| 792 | |
| 793 | if (frame == nullptr) |
| 794 | return; |
| 795 | |
| 796 | // Find the block that defines the function represented by "sym_ctx" |
| 797 | Block *function_block = sym_ctx.GetFunctionBlock(); |
| 798 | |
| 799 | if (!function_block) |
| 800 | return; |
| 801 | |
| 802 | CompilerDeclContext function_decl_ctx = function_block->GetDeclContext(); |
| 803 | |
| 804 | if (!function_decl_ctx) |
| 805 | return; |
| 806 | |
| 807 | clang::CXXMethodDecl *method_decl = |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 808 | TypeSystemClang::DeclContextGetAsCXXMethodDecl(function_decl_ctx); |
Raphael Isemann | e7cc833 | 2019-11-20 14:35:18 +0100 | [diff] [blame] | 809 | |
| 810 | if (method_decl) { |
| 811 | clang::CXXRecordDecl *class_decl = method_decl->getParent(); |
| 812 | |
| 813 | QualType class_qual_type(class_decl->getTypeForDecl(), 0); |
| 814 | |
Raphael Isemann | b036f55 | 2019-12-25 23:43:52 +0100 | [diff] [blame] | 815 | TypeFromUser class_user_type(class_qual_type.getAsOpaquePtr(), |
| 816 | function_decl_ctx.GetTypeSystem()); |
Raphael Isemann | e7cc833 | 2019-11-20 14:35:18 +0100 | [diff] [blame] | 817 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 818 | LLDB_LOG(log, " CEDM::FEVD[{0}] Adding type for $__lldb_class: {1}", |
| 819 | current_id, class_qual_type.getAsString()); |
Raphael Isemann | e7cc833 | 2019-11-20 14:35:18 +0100 | [diff] [blame] | 820 | |
| 821 | AddThisType(context, class_user_type, current_id); |
| 822 | |
| 823 | if (method_decl->isInstance()) { |
| 824 | // self is a pointer to the object |
| 825 | |
| 826 | QualType class_pointer_type = |
| 827 | method_decl->getASTContext().getPointerType(class_qual_type); |
| 828 | |
Raphael Isemann | b036f55 | 2019-12-25 23:43:52 +0100 | [diff] [blame] | 829 | TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(), |
| 830 | function_decl_ctx.GetTypeSystem()); |
Raphael Isemann | e7cc833 | 2019-11-20 14:35:18 +0100 | [diff] [blame] | 831 | |
| 832 | m_struct_vars->m_object_pointer_type = self_user_type; |
| 833 | } |
| 834 | return; |
| 835 | } |
| 836 | |
| 837 | // This branch will get hit if we are executing code in the context of |
| 838 | // a function that claims to have an object pointer (through |
| 839 | // DW_AT_object_pointer?) but is not formally a method of the class. |
| 840 | // In that case, just look up the "this" variable in the current scope |
| 841 | // and use its type. |
| 842 | // FIXME: This code is formally correct, but clang doesn't currently |
| 843 | // emit DW_AT_object_pointer |
| 844 | // for C++ so it hasn't actually been tested. |
| 845 | |
| 846 | VariableList *vars = frame->GetVariableList(false); |
| 847 | |
| 848 | lldb::VariableSP this_var = vars->FindVariable(ConstString("this")); |
| 849 | |
| 850 | if (this_var && this_var->IsInScope(frame) && |
| 851 | this_var->LocationIsValidForFrame(frame)) { |
| 852 | Type *this_type = this_var->GetType(); |
| 853 | |
| 854 | if (!this_type) |
| 855 | return; |
| 856 | |
| 857 | TypeFromUser pointee_type = |
| 858 | this_type->GetForwardCompilerType().GetPointeeType(); |
| 859 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 860 | LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_class: {1}", current_id, |
| 861 | ClangUtil::GetQualType(pointee_type).getAsString()); |
Raphael Isemann | e7cc833 | 2019-11-20 14:35:18 +0100 | [diff] [blame] | 862 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 863 | AddThisType(context, pointee_type, current_id); |
| 864 | TypeFromUser this_user_type(this_type->GetFullCompilerType()); |
| 865 | m_struct_vars->m_object_pointer_type = this_user_type; |
Raphael Isemann | e7cc833 | 2019-11-20 14:35:18 +0100 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 869 | void ClangExpressionDeclMap::LookUpLldbObjCClass(NameSearchContext &context, |
| 870 | unsigned int current_id) { |
| 871 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 872 | |
| 873 | StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); |
| 874 | |
| 875 | if (m_ctx_obj) { |
| 876 | Status status; |
| 877 | lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status); |
| 878 | if (!ctx_obj_ptr || status.Fail()) |
| 879 | return; |
| 880 | |
| 881 | AddOneType(context, TypeFromUser(m_ctx_obj->GetCompilerType()), current_id); |
| 882 | |
| 883 | m_struct_vars->m_object_pointer_type = |
| 884 | TypeFromUser(ctx_obj_ptr->GetCompilerType()); |
| 885 | |
| 886 | return; |
| 887 | } |
| 888 | |
| 889 | // Clang is looking for the type of "*self" |
| 890 | |
| 891 | if (!frame) |
| 892 | return; |
| 893 | |
| 894 | SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | |
| 895 | lldb::eSymbolContextBlock); |
| 896 | |
| 897 | // Find the block that defines the function represented by "sym_ctx" |
| 898 | Block *function_block = sym_ctx.GetFunctionBlock(); |
| 899 | |
| 900 | if (!function_block) |
| 901 | return; |
| 902 | |
| 903 | CompilerDeclContext function_decl_ctx = function_block->GetDeclContext(); |
| 904 | |
| 905 | if (!function_decl_ctx) |
| 906 | return; |
| 907 | |
| 908 | clang::ObjCMethodDecl *method_decl = |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 909 | TypeSystemClang::DeclContextGetAsObjCMethodDecl(function_decl_ctx); |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 910 | |
| 911 | if (method_decl) { |
| 912 | ObjCInterfaceDecl *self_interface = method_decl->getClassInterface(); |
| 913 | |
| 914 | if (!self_interface) |
| 915 | return; |
| 916 | |
| 917 | const clang::Type *interface_type = self_interface->getTypeForDecl(); |
| 918 | |
| 919 | if (!interface_type) |
| 920 | return; // This is unlikely, but we have seen crashes where this |
| 921 | // occurred |
| 922 | |
Raphael Isemann | b036f55 | 2019-12-25 23:43:52 +0100 | [diff] [blame] | 923 | TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(), |
| 924 | function_decl_ctx.GetTypeSystem()); |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 925 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 926 | LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_objc_class: {1}", |
| 927 | current_id, ClangUtil::ToString(interface_type)); |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 928 | |
| 929 | AddOneType(context, class_user_type, current_id); |
| 930 | |
| 931 | if (method_decl->isInstanceMethod()) { |
| 932 | // self is a pointer to the object |
| 933 | |
| 934 | QualType class_pointer_type = |
| 935 | method_decl->getASTContext().getObjCObjectPointerType( |
| 936 | QualType(interface_type, 0)); |
| 937 | |
Raphael Isemann | b036f55 | 2019-12-25 23:43:52 +0100 | [diff] [blame] | 938 | TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(), |
| 939 | function_decl_ctx.GetTypeSystem()); |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 940 | |
| 941 | m_struct_vars->m_object_pointer_type = self_user_type; |
| 942 | } else { |
| 943 | // self is a Class pointer |
| 944 | QualType class_type = method_decl->getASTContext().getObjCClassType(); |
| 945 | |
Raphael Isemann | b036f55 | 2019-12-25 23:43:52 +0100 | [diff] [blame] | 946 | TypeFromUser self_user_type(class_type.getAsOpaquePtr(), |
| 947 | function_decl_ctx.GetTypeSystem()); |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 948 | |
| 949 | m_struct_vars->m_object_pointer_type = self_user_type; |
| 950 | } |
| 951 | |
| 952 | return; |
| 953 | } |
| 954 | // This branch will get hit if we are executing code in the context of |
| 955 | // a function that claims to have an object pointer (through |
| 956 | // DW_AT_object_pointer?) but is not formally a method of the class. |
| 957 | // In that case, just look up the "self" variable in the current scope |
| 958 | // and use its type. |
| 959 | |
| 960 | VariableList *vars = frame->GetVariableList(false); |
| 961 | |
| 962 | lldb::VariableSP self_var = vars->FindVariable(ConstString("self")); |
| 963 | |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 964 | if (!self_var) |
| 965 | return; |
| 966 | if (!self_var->IsInScope(frame)) |
| 967 | return; |
| 968 | if (!self_var->LocationIsValidForFrame(frame)) |
| 969 | return; |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 970 | |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 971 | Type *self_type = self_var->GetType(); |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 972 | |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 973 | if (!self_type) |
| 974 | return; |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 975 | |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 976 | CompilerType self_clang_type = self_type->GetFullCompilerType(); |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 977 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 978 | if (TypeSystemClang::IsObjCClassType(self_clang_type)) { |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 979 | return; |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 980 | } |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 981 | if (!TypeSystemClang::IsObjCObjectPointerType(self_clang_type)) |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 982 | return; |
| 983 | self_clang_type = self_clang_type.GetPointeeType(); |
| 984 | |
| 985 | if (!self_clang_type) |
| 986 | return; |
| 987 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 988 | LLDB_LOG(log, " FEVD[{0}] Adding type for $__lldb_objc_class: {1}", |
| 989 | current_id, ClangUtil::ToString(self_type->GetFullCompilerType())); |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 990 | |
| 991 | TypeFromUser class_user_type(self_clang_type); |
| 992 | |
| 993 | AddOneType(context, class_user_type, current_id); |
| 994 | |
| 995 | TypeFromUser self_user_type(self_type->GetFullCompilerType()); |
| 996 | |
| 997 | m_struct_vars->m_object_pointer_type = self_user_type; |
Raphael Isemann | 51ad025 | 2019-11-20 15:58:07 +0100 | [diff] [blame] | 998 | } |
| 999 | |
Raphael Isemann | 337151f | 2019-11-21 10:45:04 +0100 | [diff] [blame] | 1000 | void ClangExpressionDeclMap::LookupLocalVarNamespace( |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 1001 | SymbolContext &sym_ctx, NameSearchContext &name_context) { |
| 1002 | if (sym_ctx.block == nullptr) |
| 1003 | return; |
Raphael Isemann | 337151f | 2019-11-21 10:45:04 +0100 | [diff] [blame] | 1004 | |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 1005 | CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext(); |
| 1006 | if (!frame_decl_context) |
| 1007 | return; |
Raphael Isemann | 337151f | 2019-11-21 10:45:04 +0100 | [diff] [blame] | 1008 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 1009 | TypeSystemClang *frame_ast = llvm::dyn_cast_or_null<TypeSystemClang>( |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 1010 | frame_decl_context.GetTypeSystem()); |
| 1011 | if (!frame_ast) |
| 1012 | return; |
| 1013 | |
Raphael Isemann | bc7f1df | 2019-11-29 13:02:41 +0100 | [diff] [blame] | 1014 | clang::NamespaceDecl *namespace_decl = |
| 1015 | m_clang_ast_context->GetUniqueNamespaceDeclaration( |
| 1016 | g_lldb_local_vars_namespace_cstr, nullptr); |
Raphael Isemann | 1e0d395 | 2019-11-23 21:09:41 +0100 | [diff] [blame] | 1017 | if (!namespace_decl) |
| 1018 | return; |
| 1019 | |
| 1020 | name_context.AddNamedDecl(namespace_decl); |
| 1021 | clang::DeclContext *ctxt = clang::Decl::castToDeclContext(namespace_decl); |
| 1022 | ctxt->setHasExternalVisibleStorage(true); |
| 1023 | name_context.m_found.local_vars_nsp = true; |
Raphael Isemann | 337151f | 2019-11-21 10:45:04 +0100 | [diff] [blame] | 1024 | } |
| 1025 | |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1026 | void ClangExpressionDeclMap::LookupInModulesDeclVendor( |
| 1027 | NameSearchContext &context, ConstString name, unsigned current_id) { |
| 1028 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 1029 | |
Raphael Isemann | 4aee81c | 2019-12-17 13:38:07 +0100 | [diff] [blame] | 1030 | if (!m_target) |
| 1031 | return; |
| 1032 | |
Raphael Isemann | 46883f4 | 2019-11-23 20:06:50 +0100 | [diff] [blame] | 1033 | auto *modules_decl_vendor = m_target->GetClangModulesDeclVendor(); |
| 1034 | if (!modules_decl_vendor) |
| 1035 | return; |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1036 | |
Raphael Isemann | 46883f4 | 2019-11-23 20:06:50 +0100 | [diff] [blame] | 1037 | bool append = false; |
| 1038 | uint32_t max_matches = 1; |
| 1039 | std::vector<clang::NamedDecl *> decls; |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1040 | |
Raphael Isemann | 46883f4 | 2019-11-23 20:06:50 +0100 | [diff] [blame] | 1041 | if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls)) |
| 1042 | return; |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1043 | |
Raphael Isemann | 46883f4 | 2019-11-23 20:06:50 +0100 | [diff] [blame] | 1044 | assert(!decls.empty() && "FindDecls returned true but no decls?"); |
| 1045 | clang::NamedDecl *const decl_from_modules = decls[0]; |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1046 | |
Raphael Isemann | 46883f4 | 2019-11-23 20:06:50 +0100 | [diff] [blame] | 1047 | LLDB_LOG(log, |
| 1048 | " CAS::FEVD[{0}] Matching decl found for " |
| 1049 | "\"{1}\" in the modules", |
| 1050 | current_id, name); |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1051 | |
Raphael Isemann | 46883f4 | 2019-11-23 20:06:50 +0100 | [diff] [blame] | 1052 | clang::Decl *copied_decl = CopyDecl(decl_from_modules); |
| 1053 | if (!copied_decl) { |
| 1054 | LLDB_LOG(log, |
| 1055 | " CAS::FEVD[{0}] - Couldn't export a " |
| 1056 | "declaration from the modules", |
| 1057 | current_id); |
| 1058 | return; |
| 1059 | } |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1060 | |
Raphael Isemann | 46883f4 | 2019-11-23 20:06:50 +0100 | [diff] [blame] | 1061 | if (auto copied_function = dyn_cast<clang::FunctionDecl>(copied_decl)) { |
| 1062 | MaybeRegisterFunctionBody(copied_function); |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1063 | |
Raphael Isemann | 46883f4 | 2019-11-23 20:06:50 +0100 | [diff] [blame] | 1064 | context.AddNamedDecl(copied_function); |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1065 | |
Raphael Isemann | 46883f4 | 2019-11-23 20:06:50 +0100 | [diff] [blame] | 1066 | context.m_found.function_with_type_info = true; |
| 1067 | context.m_found.function = true; |
| 1068 | } else if (auto copied_var = dyn_cast<clang::VarDecl>(copied_decl)) { |
| 1069 | context.AddNamedDecl(copied_var); |
| 1070 | context.m_found.variable = true; |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | |
Raphael Isemann | 7fa976d | 2019-11-21 12:16:00 +0100 | [diff] [blame] | 1074 | bool ClangExpressionDeclMap::LookupLocalVariable( |
| 1075 | NameSearchContext &context, ConstString name, unsigned current_id, |
| 1076 | SymbolContext &sym_ctx, CompilerDeclContext &namespace_decl) { |
Raphael Isemann | 7a0c548 | 2019-11-23 17:01:25 +0100 | [diff] [blame] | 1077 | if (sym_ctx.block == nullptr) |
| 1078 | return false; |
Raphael Isemann | 7fa976d | 2019-11-21 12:16:00 +0100 | [diff] [blame] | 1079 | |
Raphael Isemann | 7a0c548 | 2019-11-23 17:01:25 +0100 | [diff] [blame] | 1080 | CompilerDeclContext decl_context = sym_ctx.block->GetDeclContext(); |
| 1081 | if (!decl_context) |
| 1082 | return false; |
| 1083 | |
| 1084 | // Make sure that the variables are parsed so that we have the |
| 1085 | // declarations. |
Raphael Isemann | 7fa976d | 2019-11-21 12:16:00 +0100 | [diff] [blame] | 1086 | StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); |
Raphael Isemann | 7a0c548 | 2019-11-23 17:01:25 +0100 | [diff] [blame] | 1087 | VariableListSP vars = frame->GetInScopeVariableList(true); |
| 1088 | for (size_t i = 0; i < vars->GetSize(); i++) |
| 1089 | vars->GetVariableAtIndex(i)->GetDecl(); |
Raphael Isemann | 7fa976d | 2019-11-21 12:16:00 +0100 | [diff] [blame] | 1090 | |
Raphael Isemann | 7a0c548 | 2019-11-23 17:01:25 +0100 | [diff] [blame] | 1091 | // Search for declarations matching the name. Do not include imported |
| 1092 | // decls in the search if we are looking for decls in the artificial |
| 1093 | // namespace $__lldb_local_vars. |
| 1094 | std::vector<CompilerDecl> found_decls = |
| 1095 | decl_context.FindDeclByName(name, namespace_decl.IsValid()); |
Raphael Isemann | 7fa976d | 2019-11-21 12:16:00 +0100 | [diff] [blame] | 1096 | |
Raphael Isemann | 7a0c548 | 2019-11-23 17:01:25 +0100 | [diff] [blame] | 1097 | VariableSP var; |
| 1098 | bool variable_found = false; |
| 1099 | for (CompilerDecl decl : found_decls) { |
| 1100 | for (size_t vi = 0, ve = vars->GetSize(); vi != ve; ++vi) { |
| 1101 | VariableSP candidate_var = vars->GetVariableAtIndex(vi); |
| 1102 | if (candidate_var->GetDecl() == decl) { |
| 1103 | var = candidate_var; |
| 1104 | break; |
Raphael Isemann | 7fa976d | 2019-11-21 12:16:00 +0100 | [diff] [blame] | 1105 | } |
| 1106 | } |
Raphael Isemann | 7a0c548 | 2019-11-23 17:01:25 +0100 | [diff] [blame] | 1107 | |
| 1108 | if (var && !variable_found) { |
| 1109 | variable_found = true; |
| 1110 | ValueObjectSP valobj = ValueObjectVariable::Create(frame, var); |
| 1111 | AddOneVariable(context, var, valobj, current_id); |
| 1112 | context.m_found.variable = true; |
| 1113 | } |
Raphael Isemann | 7fa976d | 2019-11-21 12:16:00 +0100 | [diff] [blame] | 1114 | } |
Raphael Isemann | 7a0c548 | 2019-11-23 17:01:25 +0100 | [diff] [blame] | 1115 | return variable_found; |
Raphael Isemann | 7fa976d | 2019-11-21 12:16:00 +0100 | [diff] [blame] | 1116 | } |
| 1117 | |
Raphael Isemann | 16c0653 | 2019-11-29 15:05:42 +0100 | [diff] [blame] | 1118 | /// Structure to hold the info needed when comparing function |
| 1119 | /// declarations. |
| 1120 | namespace { |
| 1121 | struct FuncDeclInfo { |
| 1122 | ConstString m_name; |
| 1123 | CompilerType m_copied_type; |
| 1124 | uint32_t m_decl_lvl; |
| 1125 | SymbolContext m_sym_ctx; |
| 1126 | }; |
| 1127 | } // namespace |
| 1128 | |
| 1129 | SymbolContextList ClangExpressionDeclMap::SearchFunctionsInSymbolContexts( |
| 1130 | const SymbolContextList &sc_list, |
| 1131 | const CompilerDeclContext &frame_decl_context) { |
| 1132 | // First, symplify things by looping through the symbol contexts to |
| 1133 | // remove unwanted functions and separate out the functions we want to |
| 1134 | // compare and prune into a separate list. Cache the info needed about |
| 1135 | // the function declarations in a vector for efficiency. |
| 1136 | uint32_t num_indices = sc_list.GetSize(); |
| 1137 | SymbolContextList sc_sym_list; |
| 1138 | std::vector<FuncDeclInfo> decl_infos; |
| 1139 | decl_infos.reserve(num_indices); |
| 1140 | clang::DeclContext *frame_decl_ctx = |
| 1141 | (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext(); |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 1142 | TypeSystemClang *ast = llvm::dyn_cast_or_null<TypeSystemClang>( |
Raphael Isemann | 16c0653 | 2019-11-29 15:05:42 +0100 | [diff] [blame] | 1143 | frame_decl_context.GetTypeSystem()); |
| 1144 | |
| 1145 | for (uint32_t index = 0; index < num_indices; ++index) { |
| 1146 | FuncDeclInfo fdi; |
| 1147 | SymbolContext sym_ctx; |
| 1148 | sc_list.GetContextAtIndex(index, sym_ctx); |
| 1149 | |
| 1150 | // We don't know enough about symbols to compare them, but we should |
| 1151 | // keep them in the list. |
| 1152 | Function *function = sym_ctx.function; |
| 1153 | if (!function) { |
| 1154 | sc_sym_list.Append(sym_ctx); |
| 1155 | continue; |
| 1156 | } |
| 1157 | // Filter out functions without declaration contexts, as well as |
| 1158 | // class/instance methods, since they'll be skipped in the code that |
| 1159 | // follows anyway. |
| 1160 | CompilerDeclContext func_decl_context = function->GetDeclContext(); |
| 1161 | if (!func_decl_context || |
| 1162 | func_decl_context.IsClassMethod(nullptr, nullptr, nullptr)) |
| 1163 | continue; |
| 1164 | // We can only prune functions for which we can copy the type. |
| 1165 | CompilerType func_clang_type = function->GetType()->GetFullCompilerType(); |
| 1166 | CompilerType copied_func_type = GuardedCopyType(func_clang_type); |
| 1167 | if (!copied_func_type) { |
| 1168 | sc_sym_list.Append(sym_ctx); |
| 1169 | continue; |
| 1170 | } |
| 1171 | |
| 1172 | fdi.m_sym_ctx = sym_ctx; |
| 1173 | fdi.m_name = function->GetName(); |
| 1174 | fdi.m_copied_type = copied_func_type; |
| 1175 | fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL; |
| 1176 | if (fdi.m_copied_type && func_decl_context) { |
| 1177 | // Call CountDeclLevels to get the number of parent scopes we have |
| 1178 | // to look through before we find the function declaration. When |
| 1179 | // comparing functions of the same type, the one with a lower count |
| 1180 | // will be closer to us in the lookup scope and shadows the other. |
| 1181 | clang::DeclContext *func_decl_ctx = |
| 1182 | (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext(); |
| 1183 | fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx, func_decl_ctx, |
| 1184 | &fdi.m_name, &fdi.m_copied_type); |
| 1185 | } |
| 1186 | decl_infos.emplace_back(fdi); |
| 1187 | } |
| 1188 | |
| 1189 | // Loop through the functions in our cache looking for matching types, |
| 1190 | // then compare their scope levels to see which is closer. |
| 1191 | std::multimap<CompilerType, const FuncDeclInfo *> matches; |
| 1192 | for (const FuncDeclInfo &fdi : decl_infos) { |
| 1193 | const CompilerType t = fdi.m_copied_type; |
| 1194 | auto q = matches.find(t); |
| 1195 | if (q != matches.end()) { |
| 1196 | if (q->second->m_decl_lvl > fdi.m_decl_lvl) |
| 1197 | // This function is closer; remove the old set. |
| 1198 | matches.erase(t); |
| 1199 | else if (q->second->m_decl_lvl < fdi.m_decl_lvl) |
| 1200 | // The functions in our set are closer - skip this one. |
| 1201 | continue; |
| 1202 | } |
| 1203 | matches.insert(std::make_pair(t, &fdi)); |
| 1204 | } |
| 1205 | |
| 1206 | // Loop through our matches and add their symbol contexts to our list. |
| 1207 | SymbolContextList sc_func_list; |
| 1208 | for (const auto &q : matches) |
| 1209 | sc_func_list.Append(q.second->m_sym_ctx); |
| 1210 | |
| 1211 | // Rejoin the lists with the functions in front. |
| 1212 | sc_func_list.Append(sc_sym_list); |
| 1213 | return sc_func_list; |
| 1214 | } |
| 1215 | |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1216 | void ClangExpressionDeclMap::LookupFunction(NameSearchContext &context, |
| 1217 | lldb::ModuleSP module_sp, |
| 1218 | ConstString name, |
| 1219 | CompilerDeclContext &namespace_decl, |
| 1220 | unsigned current_id) { |
Raphael Isemann | 4aee81c | 2019-12-17 13:38:07 +0100 | [diff] [blame] | 1221 | if (!m_parser_vars) |
| 1222 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1224 | Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); |
Sean Callanan | b3a36df | 2016-03-19 00:51:43 +0000 | [diff] [blame] | 1225 | |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1226 | std::vector<clang::NamedDecl *> decls_from_modules; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1227 | |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1228 | if (target) { |
| 1229 | if (ClangModulesDeclVendor *decl_vendor = |
| 1230 | target->GetClangModulesDeclVendor()) { |
| 1231 | decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules); |
Sean Callanan | c02a1c0 | 2017-04-24 23:14:04 +0000 | [diff] [blame] | 1232 | } |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1233 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1234 | |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1235 | const bool include_inlines = false; |
Raphael Isemann | 24e9886 | 2019-11-21 13:43:48 +0100 | [diff] [blame] | 1236 | SymbolContextList sc_list; |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1237 | if (namespace_decl && module_sp) { |
| 1238 | const bool include_symbols = false; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1239 | |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1240 | module_sp->FindFunctions(name, &namespace_decl, eFunctionNameTypeBase, |
| 1241 | include_symbols, include_inlines, sc_list); |
| 1242 | } else if (target && !namespace_decl) { |
| 1243 | const bool include_symbols = true; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1244 | |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1245 | // TODO Fix FindFunctions so that it doesn't return |
| 1246 | // instance methods for eFunctionNameTypeBase. |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1247 | |
Levon Ter-Grigoryan | a705cf1 | 2020-01-14 14:33:43 +0100 | [diff] [blame] | 1248 | target->GetImages().FindFunctions( |
| 1249 | name, eFunctionNameTypeFull | eFunctionNameTypeBase, include_symbols, |
| 1250 | include_inlines, sc_list); |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1251 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1252 | |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1253 | // If we found more than one function, see if we can use the frame's decl |
| 1254 | // context to remove functions that are shadowed by other functions which |
| 1255 | // match in type but are nearer in scope. |
| 1256 | // |
| 1257 | // AddOneFunction will not add a function whose type has already been |
| 1258 | // added, so if there's another function in the list with a matching type, |
| 1259 | // check to see if their decl context is a parent of the current frame's or |
| 1260 | // was imported via a and using statement, and pick the best match |
| 1261 | // according to lookup rules. |
| 1262 | if (sc_list.GetSize() > 1) { |
| 1263 | // Collect some info about our frame's context. |
| 1264 | StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); |
| 1265 | SymbolContext frame_sym_ctx; |
| 1266 | if (frame != nullptr) |
| 1267 | frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | |
| 1268 | lldb::eSymbolContextBlock); |
| 1269 | CompilerDeclContext frame_decl_context = |
| 1270 | frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext() |
| 1271 | : CompilerDeclContext(); |
Sean Callanan | c02a1c0 | 2017-04-24 23:14:04 +0000 | [diff] [blame] | 1272 | |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1273 | // We can't do this without a compiler decl context for our frame. |
| 1274 | if (frame_decl_context) { |
Raphael Isemann | 16c0653 | 2019-11-29 15:05:42 +0100 | [diff] [blame] | 1275 | sc_list = SearchFunctionsInSymbolContexts(sc_list, frame_decl_context); |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | if (sc_list.GetSize()) { |
| 1280 | Symbol *extern_symbol = nullptr; |
| 1281 | Symbol *non_extern_symbol = nullptr; |
| 1282 | |
| 1283 | for (uint32_t index = 0, num_indices = sc_list.GetSize(); |
| 1284 | index < num_indices; ++index) { |
| 1285 | SymbolContext sym_ctx; |
| 1286 | sc_list.GetContextAtIndex(index, sym_ctx); |
| 1287 | |
| 1288 | if (sym_ctx.function) { |
| 1289 | CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext(); |
| 1290 | |
| 1291 | if (!decl_ctx) |
| 1292 | continue; |
| 1293 | |
| 1294 | // Filter out class/instance methods. |
| 1295 | if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr)) |
| 1296 | continue; |
| 1297 | |
| 1298 | AddOneFunction(context, sym_ctx.function, nullptr, current_id); |
| 1299 | context.m_found.function_with_type_info = true; |
| 1300 | context.m_found.function = true; |
| 1301 | } else if (sym_ctx.symbol) { |
| 1302 | if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target) { |
| 1303 | sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target); |
| 1304 | if (sym_ctx.symbol == nullptr) |
| 1305 | continue; |
| 1306 | } |
| 1307 | |
| 1308 | if (sym_ctx.symbol->IsExternal()) |
| 1309 | extern_symbol = sym_ctx.symbol; |
| 1310 | else |
| 1311 | non_extern_symbol = sym_ctx.symbol; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | if (!context.m_found.function_with_type_info) { |
| 1316 | for (clang::NamedDecl *decl : decls_from_modules) { |
| 1317 | if (llvm::isa<clang::FunctionDecl>(decl)) { |
| 1318 | clang::NamedDecl *copied_decl = |
| 1319 | llvm::cast_or_null<FunctionDecl>(CopyDecl(decl)); |
| 1320 | if (copied_decl) { |
| 1321 | context.AddNamedDecl(copied_decl); |
| 1322 | context.m_found.function_with_type_info = true; |
| 1323 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1324 | } |
| 1325 | } |
Sean Callanan | b3a36df | 2016-03-19 00:51:43 +0000 | [diff] [blame] | 1326 | } |
Sean Callanan | c02a1c0 | 2017-04-24 23:14:04 +0000 | [diff] [blame] | 1327 | |
| 1328 | if (!context.m_found.function_with_type_info) { |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1329 | if (extern_symbol) { |
| 1330 | AddOneFunction(context, nullptr, extern_symbol, current_id); |
| 1331 | context.m_found.function = true; |
| 1332 | } else if (non_extern_symbol) { |
| 1333 | AddOneFunction(context, nullptr, non_extern_symbol, current_id); |
| 1334 | context.m_found.function = true; |
| 1335 | } |
| 1336 | } |
| 1337 | } |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | void ClangExpressionDeclMap::FindExternalVisibleDecls( |
| 1341 | NameSearchContext &context, lldb::ModuleSP module_sp, |
| 1342 | CompilerDeclContext &namespace_decl, unsigned int current_id) { |
| 1343 | assert(m_ast_context); |
| 1344 | |
| 1345 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 1346 | |
| 1347 | const ConstString name(context.m_decl_name.getAsString().c_str()); |
| 1348 | if (IgnoreName(name, false)) |
| 1349 | return; |
| 1350 | |
| 1351 | // Only look for functions by name out in our symbols if the function doesn't |
| 1352 | // start with our phony prefix of '$' |
Raphael Isemann | 4aee81c | 2019-12-17 13:38:07 +0100 | [diff] [blame] | 1353 | |
| 1354 | Target *target = nullptr; |
| 1355 | StackFrame *frame = nullptr; |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1356 | SymbolContext sym_ctx; |
Raphael Isemann | 4aee81c | 2019-12-17 13:38:07 +0100 | [diff] [blame] | 1357 | if (m_parser_vars) { |
| 1358 | target = m_parser_vars->m_exe_ctx.GetTargetPtr(); |
| 1359 | frame = m_parser_vars->m_exe_ctx.GetFramePtr(); |
| 1360 | } |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1361 | if (frame != nullptr) |
| 1362 | sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | |
| 1363 | lldb::eSymbolContextBlock); |
| 1364 | |
| 1365 | // Try the persistent decls, which take precedence over all else. |
| 1366 | if (!namespace_decl) |
| 1367 | SearchPersistenDecls(context, name, current_id); |
| 1368 | |
Raphael Isemann | 8cf8ec40 | 2019-11-21 14:40:45 +0100 | [diff] [blame] | 1369 | if (name.GetStringRef().startswith("$") && !namespace_decl) { |
| 1370 | if (name == "$__lldb_class") { |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1371 | LookUpLldbClass(context, current_id); |
| 1372 | return; |
| 1373 | } |
| 1374 | |
Raphael Isemann | 8cf8ec40 | 2019-11-21 14:40:45 +0100 | [diff] [blame] | 1375 | if (name == "$__lldb_objc_class") { |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1376 | LookUpLldbObjCClass(context, current_id); |
| 1377 | return; |
| 1378 | } |
Raphael Isemann | 8cf8ec40 | 2019-11-21 14:40:45 +0100 | [diff] [blame] | 1379 | if (name == g_lldb_local_vars_namespace_cstr) { |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1380 | LookupLocalVarNamespace(sym_ctx, context); |
| 1381 | return; |
| 1382 | } |
| 1383 | |
| 1384 | // any other $__lldb names should be weeded out now |
| 1385 | if (name.GetStringRef().startswith("$__lldb")) |
| 1386 | return; |
| 1387 | |
Raphael Isemann | d8a3194 | 2019-12-17 16:12:07 +0100 | [diff] [blame] | 1388 | // No ParserVars means we can't do register or variable lookup. |
Adrian Prantl | e9331a5 | 2020-01-08 14:18:47 -0800 | [diff] [blame] | 1389 | if (!m_parser_vars || !m_parser_vars->m_persistent_vars) |
Raphael Isemann | d8a3194 | 2019-12-17 16:12:07 +0100 | [diff] [blame] | 1390 | return; |
| 1391 | |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1392 | ExpressionVariableSP pvar_sp( |
| 1393 | m_parser_vars->m_persistent_vars->GetVariable(name)); |
| 1394 | |
| 1395 | if (pvar_sp) { |
| 1396 | AddOneVariable(context, pvar_sp, current_id); |
| 1397 | return; |
| 1398 | } |
| 1399 | |
Raphael Isemann | 8cf8ec40 | 2019-11-21 14:40:45 +0100 | [diff] [blame] | 1400 | assert(name.GetStringRef().startswith("$")); |
| 1401 | llvm::StringRef reg_name = name.GetStringRef().substr(1); |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1402 | |
| 1403 | if (m_parser_vars->m_exe_ctx.GetRegisterContext()) { |
| 1404 | const RegisterInfo *reg_info( |
| 1405 | m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName( |
| 1406 | reg_name)); |
| 1407 | |
| 1408 | if (reg_info) { |
| 1409 | LLDB_LOGF(log, " CEDM::FEVD[%u] Found register %s", current_id, |
| 1410 | reg_info->name); |
| 1411 | |
| 1412 | AddOneRegister(context, reg_info, current_id); |
| 1413 | } |
| 1414 | } |
| 1415 | return; |
| 1416 | } |
| 1417 | |
Raphael Isemann | 8cf8ec40 | 2019-11-21 14:40:45 +0100 | [diff] [blame] | 1418 | bool local_var_lookup = !namespace_decl || (namespace_decl.GetName() == |
| 1419 | g_lldb_local_vars_namespace_cstr); |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1420 | if (frame && local_var_lookup) |
| 1421 | if (LookupLocalVariable(context, name, current_id, sym_ctx, namespace_decl)) |
| 1422 | return; |
| 1423 | |
| 1424 | if (target) { |
| 1425 | ValueObjectSP valobj; |
| 1426 | VariableSP var; |
Raphael Isemann | b6c29d9 | 2019-12-23 12:18:49 +0100 | [diff] [blame] | 1427 | var = FindGlobalVariable(*target, module_sp, name, &namespace_decl); |
Raphael Isemann | 5fb7dd8 | 2019-11-21 13:58:48 +0100 | [diff] [blame] | 1428 | |
| 1429 | if (var) { |
| 1430 | valobj = ValueObjectVariable::Create(target, var); |
| 1431 | AddOneVariable(context, var, valobj, current_id); |
| 1432 | context.m_found.variable = true; |
| 1433 | return; |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | LookupFunction(context, module_sp, name, namespace_decl, current_id); |
Sean Callanan | c02a1c0 | 2017-04-24 23:14:04 +0000 | [diff] [blame] | 1438 | |
Raphael Isemann | a0408ab | 2019-11-21 11:29:59 +0100 | [diff] [blame] | 1439 | // Try the modules next. |
| 1440 | if (!context.m_found.function_with_type_info) |
| 1441 | LookupInModulesDeclVendor(context, name, current_id); |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1442 | |
| 1443 | if (target && !context.m_found.variable && !namespace_decl) { |
| 1444 | // We couldn't find a non-symbol variable for this. Now we'll hunt for a |
| 1445 | // generic data symbol, and -- if it is found -- treat it as a variable. |
| 1446 | Status error; |
| 1447 | |
| 1448 | const Symbol *data_symbol = |
| 1449 | m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error); |
| 1450 | |
| 1451 | if (!error.Success()) { |
| 1452 | const unsigned diag_id = |
| 1453 | m_ast_context->getDiagnostics().getCustomDiagID( |
| 1454 | clang::DiagnosticsEngine::Level::Error, "%0"); |
| 1455 | m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString(); |
Sean Callanan | c02a1c0 | 2017-04-24 23:14:04 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Raphael Isemann | 2cada1e | 2019-11-20 16:12:50 +0100 | [diff] [blame] | 1458 | if (data_symbol) { |
| 1459 | std::string warning("got name from symbols: "); |
| 1460 | warning.append(name.AsCString()); |
| 1461 | const unsigned diag_id = |
| 1462 | m_ast_context->getDiagnostics().getCustomDiagID( |
| 1463 | clang::DiagnosticsEngine::Level::Warning, "%0"); |
| 1464 | m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str(); |
| 1465 | AddOneGenericVariable(context, *data_symbol, current_id); |
| 1466 | context.m_found.variable = true; |
Sean Callanan | c02a1c0 | 2017-04-24 23:14:04 +0000 | [diff] [blame] | 1467 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1468 | } |
Sean Callanan | 6b4067c | 2010-07-17 00:43:37 +0000 | [diff] [blame] | 1469 | } |
Greg Clayton | a272147 | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 1470 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1471 | bool ClangExpressionDeclMap::GetVariableValue(VariableSP &var, |
| 1472 | lldb_private::Value &var_location, |
| 1473 | TypeFromUser *user_type, |
| 1474 | TypeFromParser *parser_type) { |
| 1475 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1476 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1477 | Type *var_type = var->GetType(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1478 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1479 | if (!var_type) { |
Sean Callanan | ea22d42 | 2010-07-16 00:09:46 +0000 | [diff] [blame] | 1480 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1481 | log->PutCString("Skipped a definition because it has no type"); |
| 1482 | return false; |
| 1483 | } |
| 1484 | |
| 1485 | CompilerType var_clang_type = var_type->GetFullCompilerType(); |
| 1486 | |
| 1487 | if (!var_clang_type) { |
| 1488 | if (log) |
| 1489 | log->PutCString("Skipped a definition because it has no Clang type"); |
| 1490 | return false; |
| 1491 | } |
| 1492 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 1493 | TypeSystemClang *clang_ast = llvm::dyn_cast_or_null<TypeSystemClang>( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1494 | var_type->GetForwardCompilerType().GetTypeSystem()); |
| 1495 | |
| 1496 | if (!clang_ast) { |
| 1497 | if (log) |
| 1498 | log->PutCString("Skipped a definition because it has no Clang AST"); |
| 1499 | return false; |
| 1500 | } |
| 1501 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1502 | DWARFExpression &var_location_expr = var->LocationExpression(); |
| 1503 | |
| 1504 | Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1505 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1506 | |
| 1507 | if (var->GetLocationIsConstantValueData()) { |
| 1508 | DataExtractor const_value_extractor; |
| 1509 | |
| 1510 | if (var_location_expr.GetExpressionData(const_value_extractor)) { |
| 1511 | var_location = Value(const_value_extractor.GetDataStart(), |
| 1512 | const_value_extractor.GetByteSize()); |
| 1513 | var_location.SetValueType(Value::eValueTypeHostAddress); |
| 1514 | } else { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1515 | LLDB_LOGF(log, "Error evaluating constant variable: %s", err.AsCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1516 | return false; |
Greg Clayton | 7b462cc | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1517 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | CompilerType type_to_use = GuardedCopyType(var_clang_type); |
| 1521 | |
| 1522 | if (!type_to_use) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1523 | LLDB_LOGF(log, |
| 1524 | "Couldn't copy a variable's type into the parser's AST context"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1525 | |
| 1526 | return false; |
| 1527 | } |
| 1528 | |
| 1529 | if (parser_type) |
| 1530 | *parser_type = TypeFromParser(type_to_use); |
| 1531 | |
| 1532 | if (var_location.GetContextType() == Value::eContextTypeInvalid) |
| 1533 | var_location.SetCompilerType(type_to_use); |
| 1534 | |
| 1535 | if (var_location.GetValueType() == Value::eValueTypeFileAddress) { |
| 1536 | SymbolContext var_sc; |
| 1537 | var->CalculateSymbolContext(&var_sc); |
| 1538 | |
| 1539 | if (!var_sc.module_sp) |
| 1540 | return false; |
| 1541 | |
| 1542 | Address so_addr(var_location.GetScalar().ULongLong(), |
| 1543 | var_sc.module_sp->GetSectionList()); |
| 1544 | |
| 1545 | lldb::addr_t load_addr = so_addr.GetLoadAddress(target); |
| 1546 | |
| 1547 | if (load_addr != LLDB_INVALID_ADDRESS) { |
| 1548 | var_location.GetScalar() = load_addr; |
| 1549 | var_location.SetValueType(Value::eValueTypeLoadAddress); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | if (user_type) |
| 1554 | *user_type = TypeFromUser(var_clang_type); |
| 1555 | |
| 1556 | return true; |
Sean Callanan | 468574b | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1559 | void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, |
| 1560 | VariableSP var, |
| 1561 | ValueObjectSP valobj, |
| 1562 | unsigned int current_id) { |
| 1563 | assert(m_parser_vars.get()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1564 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1565 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1566 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1567 | TypeFromUser ut; |
| 1568 | TypeFromParser pt; |
| 1569 | Value var_location; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1570 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1571 | if (!GetVariableValue(var, var_location, &ut, &pt)) |
| 1572 | return; |
| 1573 | |
| 1574 | clang::QualType parser_opaque_type = |
| 1575 | QualType::getFromOpaquePtr(pt.GetOpaqueQualType()); |
| 1576 | |
| 1577 | if (parser_opaque_type.isNull()) |
| 1578 | return; |
| 1579 | |
| 1580 | if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) { |
| 1581 | if (const TagType *tag_type = dyn_cast<TagType>(parser_type)) |
| 1582 | CompleteType(tag_type->getDecl()); |
| 1583 | if (const ObjCObjectPointerType *objc_object_ptr_type = |
| 1584 | dyn_cast<ObjCObjectPointerType>(parser_type)) |
| 1585 | CompleteType(objc_object_ptr_type->getInterfaceDecl()); |
| 1586 | } |
| 1587 | |
| 1588 | bool is_reference = pt.IsReferenceType(); |
| 1589 | |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1590 | NamedDecl *var_decl = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1591 | if (is_reference) |
| 1592 | var_decl = context.AddVarDecl(pt); |
| 1593 | else |
| 1594 | var_decl = context.AddVarDecl(pt.GetLValueReferenceType()); |
| 1595 | |
| 1596 | std::string decl_name(context.m_decl_name.getAsString()); |
| 1597 | ConstString entity_name(decl_name.c_str()); |
| 1598 | ClangExpressionVariable *entity(new ClangExpressionVariable(valobj)); |
| 1599 | m_found_entities.AddNewlyConstructedVariable(entity); |
| 1600 | |
| 1601 | assert(entity); |
| 1602 | entity->EnableParserVars(GetParserID()); |
| 1603 | ClangExpressionVariable::ParserVars *parser_vars = |
| 1604 | entity->GetParserVars(GetParserID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1605 | parser_vars->m_named_decl = var_decl; |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1606 | parser_vars->m_llvm_value = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1607 | parser_vars->m_lldb_value = var_location; |
| 1608 | parser_vars->m_lldb_var = var; |
| 1609 | |
| 1610 | if (is_reference) |
| 1611 | entity->m_flags |= ClangExpressionVariable::EVTypeIsReference; |
| 1612 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 1613 | LLDB_LOG(log, |
| 1614 | " CEDM::FEVD[{0}] Found variable {1}, returned\n{2} (original {3})", |
| 1615 | current_id, decl_name, ClangUtil::DumpDecl(var_decl), |
| 1616 | ClangUtil::ToString(ut)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, |
| 1620 | ExpressionVariableSP &pvar_sp, |
| 1621 | unsigned int current_id) { |
| 1622 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 1623 | |
| 1624 | TypeFromUser user_type( |
| 1625 | llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser()); |
| 1626 | |
| 1627 | TypeFromParser parser_type(GuardedCopyType(user_type)); |
| 1628 | |
| 1629 | if (!parser_type.GetOpaqueQualType()) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1630 | LLDB_LOGF(log, " CEDM::FEVD[%u] Couldn't import type for pvar %s", |
| 1631 | current_id, pvar_sp->GetName().GetCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1632 | return; |
| 1633 | } |
| 1634 | |
| 1635 | NamedDecl *var_decl = |
| 1636 | context.AddVarDecl(parser_type.GetLValueReferenceType()); |
| 1637 | |
| 1638 | llvm::cast<ClangExpressionVariable>(pvar_sp.get()) |
| 1639 | ->EnableParserVars(GetParserID()); |
| 1640 | ClangExpressionVariable::ParserVars *parser_vars = |
| 1641 | llvm::cast<ClangExpressionVariable>(pvar_sp.get()) |
| 1642 | ->GetParserVars(GetParserID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1643 | parser_vars->m_named_decl = var_decl; |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1644 | parser_vars->m_llvm_value = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1645 | parser_vars->m_lldb_value.Clear(); |
| 1646 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 1647 | LLDB_LOG(log, " CEDM::FEVD[{0}] Added pvar {1}, returned\n{2}", current_id, |
| 1648 | pvar_sp->GetName(), ClangUtil::DumpDecl(var_decl)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | void ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context, |
| 1652 | const Symbol &symbol, |
| 1653 | unsigned int current_id) { |
| 1654 | assert(m_parser_vars.get()); |
| 1655 | |
| 1656 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 1657 | |
| 1658 | Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); |
| 1659 | |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1660 | if (target == nullptr) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1661 | return; |
| 1662 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 1663 | TypeSystemClang *scratch_ast_context = TypeSystemClang::GetScratch(*target); |
Alex Langford | 3031818 | 2019-11-14 13:41:52 -0800 | [diff] [blame] | 1664 | if (!scratch_ast_context) |
| 1665 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1666 | |
Raphael Isemann | c502bae | 2019-11-20 12:40:08 +0100 | [diff] [blame] | 1667 | TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid) |
| 1668 | .GetPointerType() |
| 1669 | .GetLValueReferenceType()); |
Raphael Isemann | bc7f1df | 2019-11-29 13:02:41 +0100 | [diff] [blame] | 1670 | TypeFromParser parser_type(m_clang_ast_context->GetBasicType(eBasicTypeVoid) |
Raphael Isemann | c502bae | 2019-11-20 12:40:08 +0100 | [diff] [blame] | 1671 | .GetPointerType() |
| 1672 | .GetLValueReferenceType()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1673 | NamedDecl *var_decl = context.AddVarDecl(parser_type); |
| 1674 | |
| 1675 | std::string decl_name(context.m_decl_name.getAsString()); |
| 1676 | ConstString entity_name(decl_name.c_str()); |
| 1677 | ClangExpressionVariable *entity(new ClangExpressionVariable( |
| 1678 | m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name, |
| 1679 | user_type, m_parser_vars->m_target_info.byte_order, |
| 1680 | m_parser_vars->m_target_info.address_byte_size)); |
| 1681 | m_found_entities.AddNewlyConstructedVariable(entity); |
| 1682 | |
| 1683 | entity->EnableParserVars(GetParserID()); |
| 1684 | ClangExpressionVariable::ParserVars *parser_vars = |
| 1685 | entity->GetParserVars(GetParserID()); |
| 1686 | |
| 1687 | const Address symbol_address = symbol.GetAddress(); |
| 1688 | lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target); |
| 1689 | |
| 1690 | // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, |
| 1691 | // user_type.GetOpaqueQualType()); |
| 1692 | parser_vars->m_lldb_value.SetCompilerType(user_type); |
| 1693 | parser_vars->m_lldb_value.GetScalar() = symbol_load_addr; |
| 1694 | parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress); |
| 1695 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1696 | parser_vars->m_named_decl = var_decl; |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1697 | parser_vars->m_llvm_value = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1698 | parser_vars->m_lldb_sym = &symbol; |
| 1699 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 1700 | LLDB_LOG(log, " CEDM::FEVD[{0}] Found variable {1}, returned\n{2}", |
| 1701 | current_id, decl_name, ClangUtil::DumpDecl(var_decl)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1704 | void ClangExpressionDeclMap::AddOneRegister(NameSearchContext &context, |
| 1705 | const RegisterInfo *reg_info, |
| 1706 | unsigned int current_id) { |
| 1707 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1708 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1709 | CompilerType clang_type = |
Raphael Isemann | c214c92 | 2019-11-29 13:43:23 +0100 | [diff] [blame] | 1710 | m_clang_ast_context->GetBuiltinTypeForEncodingAndBitSize( |
| 1711 | reg_info->encoding, reg_info->byte_size * 8); |
Sean Callanan | 7736a20 | 2016-04-29 18:09:03 +0000 | [diff] [blame] | 1712 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1713 | if (!clang_type) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1714 | LLDB_LOGF(log, " Tried to add a type for %s, but couldn't get one", |
| 1715 | context.m_decl_name.getAsString().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1716 | return; |
| 1717 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1718 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1719 | TypeFromParser parser_clang_type(clang_type); |
Sean Callanan | fa4fab7 | 2013-02-01 06:55:48 +0000 | [diff] [blame] | 1720 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1721 | NamedDecl *var_decl = context.AddVarDecl(parser_clang_type); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1722 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1723 | ClangExpressionVariable *entity(new ClangExpressionVariable( |
| 1724 | m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), |
| 1725 | m_parser_vars->m_target_info.byte_order, |
| 1726 | m_parser_vars->m_target_info.address_byte_size)); |
| 1727 | m_found_entities.AddNewlyConstructedVariable(entity); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1728 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1729 | std::string decl_name(context.m_decl_name.getAsString()); |
| 1730 | entity->SetName(ConstString(decl_name.c_str())); |
| 1731 | entity->SetRegisterInfo(reg_info); |
| 1732 | entity->EnableParserVars(GetParserID()); |
| 1733 | ClangExpressionVariable::ParserVars *parser_vars = |
| 1734 | entity->GetParserVars(GetParserID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1735 | parser_vars->m_named_decl = var_decl; |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1736 | parser_vars->m_llvm_value = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1737 | parser_vars->m_lldb_value.Clear(); |
| 1738 | entity->m_flags |= ClangExpressionVariable::EVBareRegister; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1739 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 1740 | LLDB_LOG(log, " CEDM::FEVD[{0}] Added register {1}, returned\n{2}", |
| 1741 | current_id, context.m_decl_name.getAsString(), |
| 1742 | ClangUtil::DumpDecl(var_decl)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | void ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context, |
| 1746 | Function *function, Symbol *symbol, |
| 1747 | unsigned int current_id) { |
| 1748 | assert(m_parser_vars.get()); |
| 1749 | |
| 1750 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 1751 | |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1752 | NamedDecl *function_decl = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1753 | Address fun_address; |
| 1754 | CompilerType function_clang_type; |
| 1755 | |
| 1756 | bool is_indirect_function = false; |
| 1757 | |
| 1758 | if (function) { |
| 1759 | Type *function_type = function->GetType(); |
| 1760 | |
Luke Drummond | f5bb1d6 | 2016-12-19 17:22:44 +0000 | [diff] [blame] | 1761 | const auto lang = function->GetCompileUnit()->GetLanguage(); |
| 1762 | const auto name = function->GetMangled().GetMangledName().AsCString(); |
| 1763 | const bool extern_c = (Language::LanguageIsC(lang) && |
| 1764 | !CPlusPlusLanguage::IsCPPMangledName(name)) || |
| 1765 | (Language::LanguageIsObjC(lang) && |
| 1766 | !Language::LanguageIsCPlusPlus(lang)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1767 | |
| 1768 | if (!extern_c) { |
| 1769 | TypeSystem *type_system = function->GetDeclContext().GetTypeSystem(); |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 1770 | if (llvm::isa<TypeSystemClang>(type_system)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1771 | clang::DeclContext *src_decl_context = |
| 1772 | (clang::DeclContext *)function->GetDeclContext() |
| 1773 | .GetOpaqueDeclContext(); |
| 1774 | clang::FunctionDecl *src_function_decl = |
| 1775 | llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context); |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1776 | if (src_function_decl && |
| 1777 | src_function_decl->getTemplateSpecializationInfo()) { |
| 1778 | clang::FunctionTemplateDecl *function_template = |
| 1779 | src_function_decl->getTemplateSpecializationInfo()->getTemplate(); |
| 1780 | clang::FunctionTemplateDecl *copied_function_template = |
| 1781 | llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>( |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 1782 | CopyDecl(function_template)); |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1783 | if (copied_function_template) { |
| 1784 | if (log) { |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1785 | StreamString ss; |
Jonas Devlieghere | 50a3f0e | 2019-07-23 21:14:23 +0000 | [diff] [blame] | 1786 | |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1787 | function->DumpSymbolContext(&ss); |
Jonas Devlieghere | 50a3f0e | 2019-07-23 21:14:23 +0000 | [diff] [blame] | 1788 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 1789 | LLDB_LOG(log, |
| 1790 | " CEDM::FEVD[{0}] Imported decl for function template" |
| 1791 | " {1} (description {2}), returned\n{3}", |
| 1792 | current_id, copied_function_template->getNameAsString(), |
| 1793 | ss.GetData(), |
| 1794 | ClangUtil::DumpDecl(copied_function_template)); |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1795 | } |
Jonas Devlieghere | 50a3f0e | 2019-07-23 21:14:23 +0000 | [diff] [blame] | 1796 | |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1797 | context.AddNamedDecl(copied_function_template); |
| 1798 | } |
| 1799 | } else if (src_function_decl) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1800 | if (clang::FunctionDecl *copied_function_decl = |
| 1801 | llvm::dyn_cast_or_null<clang::FunctionDecl>( |
Jonas Devlieghere | 50a3f0e | 2019-07-23 21:14:23 +0000 | [diff] [blame] | 1802 | CopyDecl(src_function_decl))) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1803 | if (log) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1804 | StreamString ss; |
| 1805 | |
| 1806 | function->DumpSymbolContext(&ss); |
| 1807 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 1808 | LLDB_LOG(log, |
| 1809 | " CEDM::FEVD[{0}]] Imported decl for function {1} " |
| 1810 | "(description {2}), returned\n{3}", |
| 1811 | current_id, copied_function_decl->getNameAsString(), |
| 1812 | ss.GetData(), ClangUtil::DumpDecl(copied_function_decl)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | context.AddNamedDecl(copied_function_decl); |
| 1816 | return; |
| 1817 | } else { |
| 1818 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1819 | LLDB_LOGF(log, " Failed to import the function decl for '%s'", |
| 1820 | src_function_decl->getName().str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1821 | } |
| 1822 | } |
Sean Callanan | 7736a20 | 2016-04-29 18:09:03 +0000 | [diff] [blame] | 1823 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1824 | } |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 1825 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1826 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1827 | if (!function_type) { |
| 1828 | if (log) |
| 1829 | log->PutCString(" Skipped a function because it has no type"); |
| 1830 | return; |
| 1831 | } |
| 1832 | |
| 1833 | function_clang_type = function_type->GetFullCompilerType(); |
| 1834 | |
| 1835 | if (!function_clang_type) { |
| 1836 | if (log) |
| 1837 | log->PutCString(" Skipped a function because it has no Clang type"); |
| 1838 | return; |
| 1839 | } |
| 1840 | |
| 1841 | fun_address = function->GetAddressRange().GetBaseAddress(); |
| 1842 | |
| 1843 | CompilerType copied_function_type = GuardedCopyType(function_clang_type); |
| 1844 | if (copied_function_type) { |
| 1845 | function_decl = context.AddFunDecl(copied_function_type, extern_c); |
| 1846 | |
| 1847 | if (!function_decl) { |
| 1848 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1849 | LLDB_LOGF( |
| 1850 | log, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1851 | " Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}", |
| 1852 | function_type->GetName().GetCString(), function_type->GetID()); |
| 1853 | } |
| 1854 | |
Sean Callanan | 3b436d2 | 2015-10-23 21:45:02 +0000 | [diff] [blame] | 1855 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1856 | } |
| 1857 | } else { |
| 1858 | // We failed to copy the type we found |
| 1859 | if (log) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1860 | LLDB_LOGF(log, |
| 1861 | " Failed to import the function type '%s' {0x%8.8" PRIx64 |
| 1862 | "} into the expression parser AST contenxt", |
| 1863 | function_type->GetName().GetCString(), |
| 1864 | function_type->GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | return; |
| 1868 | } |
| 1869 | } else if (symbol) { |
| 1870 | fun_address = symbol->GetAddress(); |
| 1871 | function_decl = context.AddGenericFunDecl(); |
| 1872 | is_indirect_function = symbol->IsIndirect(); |
| 1873 | } else { |
| 1874 | if (log) |
| 1875 | log->PutCString(" AddOneFunction called with no function and no symbol"); |
| 1876 | return; |
| 1877 | } |
| 1878 | |
| 1879 | Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); |
| 1880 | |
| 1881 | lldb::addr_t load_addr = |
| 1882 | fun_address.GetCallableLoadAddress(target, is_indirect_function); |
| 1883 | |
| 1884 | ClangExpressionVariable *entity(new ClangExpressionVariable( |
| 1885 | m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), |
| 1886 | m_parser_vars->m_target_info.byte_order, |
| 1887 | m_parser_vars->m_target_info.address_byte_size)); |
| 1888 | m_found_entities.AddNewlyConstructedVariable(entity); |
| 1889 | |
| 1890 | std::string decl_name(context.m_decl_name.getAsString()); |
| 1891 | entity->SetName(ConstString(decl_name.c_str())); |
| 1892 | entity->SetCompilerType(function_clang_type); |
| 1893 | entity->EnableParserVars(GetParserID()); |
| 1894 | |
| 1895 | ClangExpressionVariable::ParserVars *parser_vars = |
| 1896 | entity->GetParserVars(GetParserID()); |
| 1897 | |
| 1898 | if (load_addr != LLDB_INVALID_ADDRESS) { |
| 1899 | parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress); |
| 1900 | parser_vars->m_lldb_value.GetScalar() = load_addr; |
| 1901 | } else { |
| 1902 | // We have to try finding a file address. |
| 1903 | |
| 1904 | lldb::addr_t file_addr = fun_address.GetFileAddress(); |
| 1905 | |
| 1906 | parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress); |
| 1907 | parser_vars->m_lldb_value.GetScalar() = file_addr; |
| 1908 | } |
| 1909 | |
| 1910 | parser_vars->m_named_decl = function_decl; |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 1911 | parser_vars->m_llvm_value = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1912 | |
| 1913 | if (log) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1914 | StreamString ss; |
| 1915 | |
| 1916 | fun_address.Dump(&ss, |
| 1917 | m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), |
| 1918 | Address::DumpStyleResolvedDescription); |
| 1919 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 1920 | LLDB_LOG(log, |
| 1921 | " CEDM::FEVD[{0}] Found {1} function {2} (description {3}), " |
| 1922 | "returned\n{4}", |
| 1923 | current_id, (function ? "specific" : "generic"), decl_name, |
| 1924 | ss.GetData(), ClangUtil::DumpDecl(function_decl)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | void ClangExpressionDeclMap::AddThisType(NameSearchContext &context, |
Aleksandr Urakov | 40624a0 | 2019-02-05 09:14:36 +0000 | [diff] [blame] | 1929 | const TypeFromUser &ut, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1930 | unsigned int current_id) { |
| 1931 | CompilerType copied_clang_type = GuardedCopyType(ut); |
| 1932 | |
| 1933 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 1934 | |
| 1935 | if (!copied_clang_type) { |
| 1936 | if (log) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 1937 | LLDB_LOGF( |
| 1938 | log, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1939 | "ClangExpressionDeclMap::AddThisType - Couldn't import the type"); |
Sean Callanan | 3b436d2 | 2015-10-23 21:45:02 +0000 | [diff] [blame] | 1940 | |
| 1941 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | if (copied_clang_type.IsAggregateType() && |
| 1945 | copied_clang_type.GetCompleteType()) { |
Raphael Isemann | bc7f1df | 2019-11-29 13:02:41 +0100 | [diff] [blame] | 1946 | CompilerType void_clang_type = |
| 1947 | m_clang_ast_context->GetBasicType(eBasicTypeVoid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1948 | CompilerType void_ptr_clang_type = void_clang_type.GetPointerType(); |
| 1949 | |
Raphael Isemann | 0007f9d | 2019-12-29 20:10:29 +0100 | [diff] [blame] | 1950 | CompilerType method_type = m_clang_ast_context->CreateFunctionType( |
| 1951 | void_clang_type, &void_ptr_clang_type, 1, false, 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1952 | |
| 1953 | const bool is_virtual = false; |
| 1954 | const bool is_static = false; |
| 1955 | const bool is_inline = false; |
| 1956 | const bool is_explicit = false; |
| 1957 | const bool is_attr_used = true; |
| 1958 | const bool is_artificial = false; |
| 1959 | |
Raphael Isemann | bc7f1df | 2019-11-29 13:02:41 +0100 | [diff] [blame] | 1960 | CXXMethodDecl *method_decl = m_clang_ast_context->AddMethodToCXXRecordType( |
| 1961 | copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", nullptr, |
| 1962 | method_type, lldb::eAccessPublic, is_virtual, is_static, is_inline, |
| 1963 | is_explicit, is_attr_used, is_artificial); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1964 | |
Raphael Isemann | 7a6588a | 2019-11-25 13:27:51 +0100 | [diff] [blame] | 1965 | LLDB_LOG(log, |
| 1966 | " CEDM::AddThisType Added function $__lldb_expr " |
| 1967 | "(description {0}) for this type\n{1}", |
| 1968 | ClangUtil::ToString(copied_clang_type), |
| 1969 | ClangUtil::DumpDecl(method_decl)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | if (!copied_clang_type.IsValid()) |
| 1973 | return; |
| 1974 | |
| 1975 | TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo( |
| 1976 | QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType())); |
| 1977 | |
| 1978 | if (!type_source_info) |
| 1979 | return; |
| 1980 | |
| 1981 | // Construct a typedef type because if "*this" is a templated type we can't |
| 1982 | // just return ClassTemplateSpecializationDecls in response to name queries. |
| 1983 | // Using a typedef makes this much more robust. |
| 1984 | |
| 1985 | TypedefDecl *typedef_decl = TypedefDecl::Create( |
| 1986 | *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(), |
| 1987 | SourceLocation(), context.m_decl_name.getAsIdentifierInfo(), |
| 1988 | type_source_info); |
| 1989 | |
| 1990 | if (!typedef_decl) |
| 1991 | return; |
| 1992 | |
| 1993 | context.AddNamedDecl(typedef_decl); |
| 1994 | |
| 1995 | return; |
Sean Callanan | fa4fab7 | 2013-02-01 06:55:48 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1998 | void ClangExpressionDeclMap::AddOneType(NameSearchContext &context, |
Aleksandr Urakov | 40624a0 | 2019-02-05 09:14:36 +0000 | [diff] [blame] | 1999 | const TypeFromUser &ut, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2000 | unsigned int current_id) { |
| 2001 | CompilerType copied_clang_type = GuardedCopyType(ut); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 2002 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2003 | if (!copied_clang_type) { |
| 2004 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | fa4fab7 | 2013-02-01 06:55:48 +0000 | [diff] [blame] | 2005 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2006 | if (log) |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 2007 | LLDB_LOGF( |
| 2008 | log, "ClangExpressionDeclMap::AddOneType - Couldn't import the type"); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 2009 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2010 | return; |
| 2011 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 2012 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2013 | context.AddTypeDecl(copied_clang_type); |
Sean Callanan | 5666b67 | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 2014 | } |