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