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