1 //===-- ClangPersistentVariables.cpp ----------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ClangPersistentVariables.h" 10 11 #include "lldb/Core/Value.h" 12 #include "lldb/Symbol/ClangASTContext.h" 13 #include "lldb/Target/Target.h" 14 #include "lldb/Utility/DataExtractor.h" 15 #include "lldb/Utility/Log.h" 16 #include "lldb/Utility/StreamString.h" 17 18 #include "clang/AST/Decl.h" 19 20 #include "llvm/ADT/StringMap.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 ClangPersistentVariables::ClangPersistentVariables() 26 : lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang) {} 27 28 ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable( 29 const lldb::ValueObjectSP &valobj_sp) { 30 return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp)); 31 } 32 33 ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable( 34 ExecutionContextScope *exe_scope, ConstString name, 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)); 39 } 40 41 void ClangPersistentVariables::RemovePersistentVariable( 42 lldb::ExpressionVariableSP variable) { 43 RemoveVariable(variable); 44 45 const char *name = variable->GetName().AsCString(); 46 47 if (*name != '$') 48 return; 49 name++; 50 51 if (strtoul(name, nullptr, 0) == m_next_persistent_variable_id - 1) 52 m_next_persistent_variable_id--; 53 } 54 55 llvm::Optional<CompilerType> 56 ClangPersistentVariables::GetCompilerTypeFromPersistentDecl( 57 ConstString type_name) { 58 CompilerType compiler_type; 59 if (clang::TypeDecl *tdecl = llvm::dyn_cast_or_null<clang::TypeDecl>( 60 GetPersistentDecl(type_name))) { 61 compiler_type.SetCompilerType( 62 ClangASTContext::GetASTContext(&tdecl->getASTContext()), 63 reinterpret_cast<lldb::opaque_compiler_type_t>( 64 const_cast<clang::Type *>(tdecl->getTypeForDecl()))); 65 return compiler_type; 66 } 67 return llvm::None; 68 } 69 70 void ClangPersistentVariables::RegisterPersistentDecl(ConstString name, 71 clang::NamedDecl *decl) { 72 m_persistent_decls.insert( 73 std::pair<const char *, clang::NamedDecl *>(name.GetCString(), decl)); 74 75 if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) { 76 for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) { 77 m_persistent_decls.insert(std::pair<const char *, clang::NamedDecl *>( 78 ConstString(enumerator_decl->getNameAsString()).GetCString(), 79 enumerator_decl)); 80 } 81 } 82 } 83 84 clang::NamedDecl * 85 ClangPersistentVariables::GetPersistentDecl(ConstString name) { 86 PersistentDeclMap::const_iterator i = 87 m_persistent_decls.find(name.GetCString()); 88 89 if (i == m_persistent_decls.end()) 90 return nullptr; 91 else 92 return i->second; 93 } 94