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/Target/Target.h"
13 #include "lldb/Utility/DataExtractor.h"
14 #include "lldb/Utility/Log.h"
15 #include "lldb/Utility/StreamString.h"
16 
17 #include "clang/AST/Decl.h"
18 
19 #include "llvm/ADT/StringMap.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 ClangPersistentVariables::ClangPersistentVariables()
25     : lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang),
26       m_next_persistent_variable_id(0) {}
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, NULL, 0) == m_next_persistent_variable_id - 1)
52     m_next_persistent_variable_id--;
53 }
54 
55 void ClangPersistentVariables::RegisterPersistentDecl(ConstString name,
56                                                       clang::NamedDecl *decl) {
57   m_persistent_decls.insert(
58       std::pair<const char *, clang::NamedDecl *>(name.GetCString(), decl));
59 
60   if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
61     for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
62       m_persistent_decls.insert(std::pair<const char *, clang::NamedDecl *>(
63           ConstString(enumerator_decl->getNameAsString()).GetCString(),
64           enumerator_decl));
65     }
66   }
67 }
68 
69 clang::NamedDecl *
70 ClangPersistentVariables::GetPersistentDecl(ConstString name) {
71   PersistentDeclMap::const_iterator i =
72       m_persistent_decls.find(name.GetCString());
73 
74   if (i == m_persistent_decls.end())
75     return NULL;
76   else
77     return i->second;
78 }
79