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