ClangPersistentVariables.cpp revision 292932
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
19using namespace lldb;
20using namespace lldb_private;
21
22ClangPersistentVariables::ClangPersistentVariables () :
23    lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang),
24    m_next_persistent_variable_id (0)
25{
26}
27
28ExpressionVariableSP
29ClangPersistentVariables::CreatePersistentVariable (const lldb::ValueObjectSP &valobj_sp)
30{
31    return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));
32}
33
34ExpressionVariableSP
35ClangPersistentVariables::CreatePersistentVariable (ExecutionContextScope *exe_scope,
36                                                    const ConstString &name,
37                                                    const CompilerType& compiler_type,
38                                                    lldb::ByteOrder byte_order,
39                                                    uint32_t addr_byte_size)
40{
41    return AddNewlyConstructedVariable(new ClangExpressionVariable(exe_scope, name, compiler_type, byte_order, addr_byte_size));
42}
43
44void
45ClangPersistentVariables::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
59ConstString
60ClangPersistentVariables::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
68void
69ClangPersistentVariables::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
75clang::TypeDecl *
76ClangPersistentVariables::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