1 //===-- ClangPersistentVariables.cpp ----------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ClangPersistentVariables.h"
11 
12 #include "lldb/Core/Value.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       m_next_persistent_variable_id(0) {}
28 
29 ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
30     const lldb::ValueObjectSP &valobj_sp) {
31   return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));
32 }
33 
34 ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
35     ExecutionContextScope *exe_scope, const ConstString &name,
36     const CompilerType &compiler_type, lldb::ByteOrder byte_order,
37     uint32_t addr_byte_size) {
38   return AddNewlyConstructedVariable(new ClangExpressionVariable(
39       exe_scope, name, compiler_type, byte_order, addr_byte_size));
40 }
41 
42 void ClangPersistentVariables::RemovePersistentVariable(
43     lldb::ExpressionVariableSP variable) {
44   RemoveVariable(variable);
45 
46   const char *name = variable->GetName().AsCString();
47 
48   if (*name != '$')
49     return;
50   name++;
51 
52   if (strtoul(name, NULL, 0) == m_next_persistent_variable_id - 1)
53     m_next_persistent_variable_id--;
54 }
55 
56 void ClangPersistentVariables::RegisterPersistentDecl(const ConstString &name,
57                                                       clang::NamedDecl *decl) {
58   m_persistent_decls.insert(
59       std::pair<const char *, clang::NamedDecl *>(name.GetCString(), decl));
60 
61   if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
62     for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
63       m_persistent_decls.insert(std::pair<const char *, clang::NamedDecl *>(
64           ConstString(enumerator_decl->getNameAsString()).GetCString(),
65           enumerator_decl));
66     }
67   }
68 }
69 
70 clang::NamedDecl *
71 ClangPersistentVariables::GetPersistentDecl(const ConstString &name) {
72   PersistentDeclMap::const_iterator i =
73       m_persistent_decls.find(name.GetCString());
74 
75   if (i == m_persistent_decls.end())
76     return NULL;
77   else
78     return i->second;
79 }
80