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