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 ConstString
57 ClangPersistentVariables::GetNextPersistentVariableName(Target &target) {
58   char name_cstr[256];
59   ::snprintf(name_cstr, sizeof(name_cstr), "$%u",
60              target.GetNextPersistentVariableIndex());
61   ConstString name(name_cstr);
62   return name;
63 }
64 
65 void ClangPersistentVariables::RegisterPersistentDecl(const ConstString &name,
66                                                       clang::NamedDecl *decl) {
67   m_persistent_decls.insert(
68       std::pair<const char *, clang::NamedDecl *>(name.GetCString(), decl));
69 
70   if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
71     for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
72       m_persistent_decls.insert(std::pair<const char *, clang::NamedDecl *>(
73           ConstString(enumerator_decl->getNameAsString()).GetCString(),
74           enumerator_decl));
75     }
76   }
77 }
78 
79 clang::NamedDecl *
80 ClangPersistentVariables::GetPersistentDecl(const ConstString &name) {
81   PersistentDeclMap::const_iterator i =
82       m_persistent_decls.find(name.GetCString());
83 
84   if (i == m_persistent_decls.end())
85     return NULL;
86   else
87     return i->second;
88 }
89