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/DataExtractor.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Core/StreamString.h"
15 #include "lldb/Core/Value.h"
16 
17 #include "llvm/ADT/StringMap.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 ClangPersistentVariables::ClangPersistentVariables () :
23     lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang),
24     m_next_persistent_variable_id (0)
25 {
26 }
27 
28 ExpressionVariableSP
29 ClangPersistentVariables::CreatePersistentVariable (const lldb::ValueObjectSP &valobj_sp)
30 {
31     return ClangExpressionVariable::CreateVariableInList(*this, valobj_sp)->shared_from_this();
32 }
33 
34 ClangExpressionVariable *
35 ClangPersistentVariables::CreatePersistentVariable (ExecutionContextScope *exe_scope,
36                                                     const ConstString &name,
37                                                     const TypeFromUser& user_type,
38                                                     lldb::ByteOrder byte_order,
39                                                     uint32_t addr_byte_size)
40 {
41     return ClangExpressionVariable::CreateVariableInList(*this, exe_scope, name, user_type, byte_order, addr_byte_size);
42 }
43 
44 void
45 ClangPersistentVariables::RemovePersistentVariable (lldb::ExpressionVariableSP variable)
46 {
47     RemoveVariable(variable);
48 
49     const char *name = variable->GetName().AsCString();
50 
51     if (*name != '$')
52         return;
53     name++;
54 
55     if (strtoul(name, NULL, 0) == m_next_persistent_variable_id - 1)
56         m_next_persistent_variable_id--;
57 }
58 
59 ConstString
60 ClangPersistentVariables::GetNextPersistentVariableName ()
61 {
62     char name_cstr[256];
63     ::snprintf (name_cstr, sizeof(name_cstr), "$%u", m_next_persistent_variable_id++);
64     ConstString name(name_cstr);
65     return name;
66 }
67 
68 void
69 ClangPersistentVariables::RegisterPersistentType (const ConstString &name,
70                                                   clang::TypeDecl *type_decl)
71 {
72     m_persistent_types.insert(std::pair<const char*, clang::TypeDecl*>(name.GetCString(), type_decl));
73 }
74 
75 clang::TypeDecl *
76 ClangPersistentVariables::GetPersistentType (const ConstString &name)
77 {
78     PersistentTypeMap::const_iterator i = m_persistent_types.find(name.GetCString());
79 
80     if (i == m_persistent_types.end())
81         return NULL;
82     else
83         return i->second;
84 }
85