1254721Semaste//===-- ClangPersistentVariables.cpp ----------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/Expression/ClangPersistentVariables.h"
11254721Semaste#include "lldb/Core/DataExtractor.h"
12254721Semaste#include "lldb/Core/Log.h"
13254721Semaste#include "lldb/Core/StreamString.h"
14254721Semaste#include "lldb/Core/Value.h"
15254721Semaste
16254721Semaste#include "llvm/ADT/StringMap.h"
17254721Semaste
18254721Semasteusing namespace lldb;
19254721Semasteusing namespace lldb_private;
20254721Semaste
21254721SemasteClangPersistentVariables::ClangPersistentVariables () :
22254721Semaste    ClangExpressionVariableList(),
23254721Semaste    m_next_persistent_variable_id (0)
24254721Semaste{
25254721Semaste}
26254721Semaste
27254721SemasteClangExpressionVariableSP
28254721SemasteClangPersistentVariables::CreatePersistentVariable (const lldb::ValueObjectSP &valobj_sp)
29254721Semaste{
30254721Semaste    ClangExpressionVariableSP var_sp (CreateVariable(valobj_sp));
31254721Semaste    return var_sp;
32254721Semaste}
33254721Semaste
34254721SemasteClangExpressionVariableSP
35254721SemasteClangPersistentVariables::CreatePersistentVariable (ExecutionContextScope *exe_scope,
36254721Semaste                                                    const ConstString &name,
37254721Semaste                                                    const TypeFromUser& user_type,
38254721Semaste                                                    lldb::ByteOrder byte_order,
39254721Semaste                                                    uint32_t addr_byte_size)
40254721Semaste{
41254721Semaste    ClangExpressionVariableSP var_sp (GetVariable(name));
42254721Semaste
43254721Semaste    if (!var_sp)
44254721Semaste        var_sp = CreateVariable(exe_scope, name, user_type, byte_order, addr_byte_size);
45254721Semaste
46254721Semaste    return var_sp;
47254721Semaste}
48254721Semaste
49254721Semastevoid
50254721SemasteClangPersistentVariables::RemovePersistentVariable (lldb::ClangExpressionVariableSP variable)
51254721Semaste{
52254721Semaste    RemoveVariable(variable);
53254721Semaste
54254721Semaste    const char *name = variable->GetName().AsCString();
55254721Semaste
56254721Semaste    if (*name != '$')
57254721Semaste        return;
58254721Semaste    name++;
59254721Semaste
60254721Semaste    if (strtoul(name, NULL, 0) == m_next_persistent_variable_id - 1)
61254721Semaste        m_next_persistent_variable_id--;
62254721Semaste}
63254721Semaste
64254721SemasteConstString
65254721SemasteClangPersistentVariables::GetNextPersistentVariableName ()
66254721Semaste{
67254721Semaste    char name_cstr[256];
68254721Semaste    ::snprintf (name_cstr, sizeof(name_cstr), "$%u", m_next_persistent_variable_id++);
69254721Semaste    ConstString name(name_cstr);
70254721Semaste    return name;
71254721Semaste}
72254721Semaste
73254721Semastevoid
74254721SemasteClangPersistentVariables::RegisterPersistentType (const ConstString &name,
75254721Semaste                                                  clang::TypeDecl *type_decl)
76254721Semaste{
77254721Semaste    m_persistent_types.insert(std::pair<const char*, clang::TypeDecl*>(name.GetCString(), type_decl));
78254721Semaste}
79254721Semaste
80254721Semasteclang::TypeDecl *
81254721SemasteClangPersistentVariables::GetPersistentType (const ConstString &name)
82254721Semaste{
83254721Semaste    PersistentTypeMap::const_iterator i = m_persistent_types.find(name.GetCString());
84254721Semaste
85254721Semaste    if (i == m_persistent_types.end())
86254721Semaste        return NULL;
87254721Semaste    else
88254721Semaste        return i->second;
89254721Semaste}
90