| Sean Callanan | 2235f32 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 1 | //===-- ClangPersistentVariables.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 |
| Sean Callanan | 2235f32 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 9 | #include "ClangPersistentVariables.h" |
| 10 | |
| Sean Callanan | d1e5b43 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Value.h" |
| Alex Langford | 5b99928 | 2019-06-12 17:47:06 +0000 | [diff] [blame] | 12 | #include "lldb/Symbol/ClangASTContext.h" |
| Adrian Prantl | 5435f78 | 2018-04-30 23:59:15 +0000 | [diff] [blame] | 13 | #include "lldb/Target/Target.h" |
| Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 14 | #include "lldb/Utility/DataExtractor.h" |
| Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/Log.h" |
| Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/StreamString.h" |
| Sean Callanan | 2235f32 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 17 | |
| Sean Callanan | b3a36df | 2016-03-19 00:51:43 +0000 | [diff] [blame] | 18 | #include "clang/AST/Decl.h" |
| 19 | |
| Sean Callanan | bccce81 | 2011-08-23 21:20:51 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringMap.h" |
| 21 | |
| Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 22 | using namespace lldb; |
| Sean Callanan | 2235f32 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 23 | using namespace lldb_private; |
| Sean Callanan | d1e5b43 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 24 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | ClangPersistentVariables::ClangPersistentVariables() |
| Raphael Isemann | 2fb7cf8 | 2019-08-05 13:40:39 +0000 | [diff] [blame] | 26 | : lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang) {} |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | |
| 28 | ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable( |
| 29 | const lldb::ValueObjectSP &valobj_sp) { |
| 30 | return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp)); |
| Sean Callanan | 2235f32 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 31 | } |
| Sean Callanan | d1e5b43 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 32 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable( |
| Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 34 | ExecutionContextScope *exe_scope, ConstString name, |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | const CompilerType &compiler_type, lldb::ByteOrder byte_order, |
| 36 | uint32_t addr_byte_size) { |
| 37 | return AddNewlyConstructedVariable(new ClangExpressionVariable( |
| 38 | exe_scope, name, compiler_type, byte_order, addr_byte_size)); |
| Sean Callanan | d1e5b43 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 39 | } |
| Sean Callanan | d0ef0ef | 2010-08-20 01:02:30 +0000 | [diff] [blame] | 40 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | void ClangPersistentVariables::RemovePersistentVariable( |
| 42 | lldb::ExpressionVariableSP variable) { |
| 43 | RemoveVariable(variable); |
| 44 | |
| Raphael Isemann | 9de7169 | 2019-08-05 21:43:53 +0000 | [diff] [blame] | 45 | // Check if the removed variable was the last one that was created. If yes, |
| 46 | // reuse the variable id for the next variable. |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | |
| Raphael Isemann | 9de7169 | 2019-08-05 21:43:53 +0000 | [diff] [blame] | 48 | // Nothing to do if we have not assigned a variable id so far. |
| 49 | if (m_next_persistent_variable_id == 0) |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | return; |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | |
| Raphael Isemann | 9de7169 | 2019-08-05 21:43:53 +0000 | [diff] [blame] | 52 | llvm::StringRef name = variable->GetName().GetStringRef(); |
| 53 | // Remove the prefix from the variable that only the indes is left. |
| 54 | if (!name.consume_front(GetPersistentVariablePrefix(false))) |
| 55 | return; |
| 56 | |
| 57 | // Check if the variable contained a variable id. |
| 58 | uint32_t variable_id; |
| 59 | if (name.getAsInteger(10, variable_id)) |
| 60 | return; |
| 61 | // If it's the most recent variable id that was assigned, make sure that this |
| 62 | // variable id will be used for the next persistent variable. |
| 63 | if (variable_id == m_next_persistent_variable_id - 1) |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | m_next_persistent_variable_id--; |
| Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| Alex Langford | 5b99928 | 2019-06-12 17:47:06 +0000 | [diff] [blame] | 67 | llvm::Optional<CompilerType> |
| 68 | ClangPersistentVariables::GetCompilerTypeFromPersistentDecl( |
| 69 | ConstString type_name) { |
| Raphael Isemann | 36fb199 | 2019-12-28 22:00:27 +0100 | [diff] [blame] | 70 | PersistentDecl p = m_persistent_decls.lookup(type_name.GetCString()); |
| 71 | |
| 72 | if (p.m_decl == nullptr) |
| 73 | return llvm::None; |
| 74 | |
| 75 | if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) { |
| 76 | opaque_compiler_type_t t = static_cast<opaque_compiler_type_t>( |
| 77 | const_cast<clang::Type *>(tdecl->getTypeForDecl())); |
| 78 | return CompilerType(p.m_context, t); |
| Alex Langford | 5b99928 | 2019-06-12 17:47:06 +0000 | [diff] [blame] | 79 | } |
| 80 | return llvm::None; |
| 81 | } |
| 82 | |
| Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 83 | void ClangPersistentVariables::RegisterPersistentDecl(ConstString name, |
| Raphael Isemann | 36fb199 | 2019-12-28 22:00:27 +0100 | [diff] [blame] | 84 | clang::NamedDecl *decl, |
| 85 | ClangASTContext *ctx) { |
| 86 | PersistentDecl p = {decl, ctx}; |
| 87 | m_persistent_decls.insert(std::make_pair(name.GetCString(), p)); |
| Sean Callanan | bccce81 | 2011-08-23 21:20:51 +0000 | [diff] [blame] | 88 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) { |
| 90 | for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) { |
| Raphael Isemann | 36fb199 | 2019-12-28 22:00:27 +0100 | [diff] [blame] | 91 | p = {enumerator_decl, ctx}; |
| 92 | m_persistent_decls.insert(std::make_pair( |
| 93 | ConstString(enumerator_decl->getNameAsString()).GetCString(), p)); |
| Sean Callanan | b3a36df | 2016-03-19 00:51:43 +0000 | [diff] [blame] | 94 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | } |
| Sean Callanan | bccce81 | 2011-08-23 21:20:51 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| Sean Callanan | b3a36df | 2016-03-19 00:51:43 +0000 | [diff] [blame] | 98 | clang::NamedDecl * |
| Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 99 | ClangPersistentVariables::GetPersistentDecl(ConstString name) { |
| Raphael Isemann | 36fb199 | 2019-12-28 22:00:27 +0100 | [diff] [blame] | 100 | return m_persistent_decls.lookup(name.GetCString()).m_decl; |
| Sean Callanan | bccce81 | 2011-08-23 21:20:51 +0000 | [diff] [blame] | 101 | } |