blob: db062d2e20b60e89ee9ec6336bf5fd5a3c5d35bd [file] [log] [blame]
Sean Callanana48fe162010-08-11 03:57:18 +00001//===-- 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
Johnny Chenff7c3e92010-12-20 21:45:22 +000010#include "lldb/Expression/ClangPersistentVariables.h"
Sean Callanan82b74c82010-08-12 01:56:52 +000011#include "lldb/Core/DataExtractor.h"
Sean Callanana48fe162010-08-11 03:57:18 +000012#include "lldb/Core/Log.h"
13#include "lldb/Core/StreamString.h"
Sean Callanan82b74c82010-08-12 01:56:52 +000014#include "lldb/Core/Value.h"
Sean Callanana48fe162010-08-11 03:57:18 +000015
Sean Callanan24312442011-08-23 21:20:51 +000016#include "llvm/ADT/StringMap.h"
17
Greg Clayton427f2902010-12-14 02:59:59 +000018using namespace lldb;
Sean Callanana48fe162010-08-11 03:57:18 +000019using namespace lldb_private;
Sean Callanan82b74c82010-08-12 01:56:52 +000020
Sean Callanana48fe162010-08-11 03:57:18 +000021ClangPersistentVariables::ClangPersistentVariables () :
Greg Clayton427f2902010-12-14 02:59:59 +000022 ClangExpressionVariableList(),
23 m_next_persistent_variable_id (0)
Sean Callanana48fe162010-08-11 03:57:18 +000024{
Sean Callanana48fe162010-08-11 03:57:18 +000025}
Sean Callanan82b74c82010-08-12 01:56:52 +000026
Greg Clayton427f2902010-12-14 02:59:59 +000027ClangExpressionVariableSP
28ClangPersistentVariables::CreatePersistentVariable (const lldb::ValueObjectSP &valobj_sp)
Sean Callanan82b74c82010-08-12 01:56:52 +000029{
Greg Clayton427f2902010-12-14 02:59:59 +000030 ClangExpressionVariableSP var_sp (CreateVariable(valobj_sp));
31 return var_sp;
Sean Callanan82b74c82010-08-12 01:56:52 +000032}
Sean Callanana6223432010-08-20 01:02:30 +000033
Greg Clayton427f2902010-12-14 02:59:59 +000034ClangExpressionVariableSP
Jim Inghamfa3a16a2011-03-31 00:19:25 +000035ClangPersistentVariables::CreatePersistentVariable (ExecutionContextScope *exe_scope,
36 const ConstString &name,
37 const TypeFromUser& user_type,
38 lldb::ByteOrder byte_order,
39 uint32_t addr_byte_size)
Sean Callanana6223432010-08-20 01:02:30 +000040{
Greg Clayton427f2902010-12-14 02:59:59 +000041 ClangExpressionVariableSP var_sp (GetVariable(name));
Sean Callanana6223432010-08-20 01:02:30 +000042
Greg Clayton427f2902010-12-14 02:59:59 +000043 if (!var_sp)
Jim Inghamfa3a16a2011-03-31 00:19:25 +000044 var_sp = CreateVariable(exe_scope, name, user_type, byte_order, addr_byte_size);
Greg Clayton427f2902010-12-14 02:59:59 +000045
46 return var_sp;
47}
48
Sean Callanan8f2e3922012-02-04 08:49:35 +000049void
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}
Greg Clayton427f2902010-12-14 02:59:59 +000063
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}
Sean Callanan24312442011-08-23 21:20:51 +000072
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}