ClangPersistentVariables.cpp revision 314564
1184054Slulf//===-- ClangPersistentVariables.cpp ----------------------------*- C++ -*-===//
2184054Slulf//
3184054Slulf//                     The LLVM Compiler Infrastructure
4184054Slulf//
5184054Slulf// This file is distributed under the University of Illinois Open Source
6184054Slulf// License. See LICENSE.TXT for details.
7184054Slulf//
8184054Slulf//===----------------------------------------------------------------------===//
9184054Slulf
10184054Slulf#include "ClangPersistentVariables.h"
11184054Slulf
12184054Slulf#include "lldb/Core/DataExtractor.h"
13184054Slulf#include "lldb/Core/Log.h"
14184054Slulf#include "lldb/Core/StreamString.h"
15184054Slulf#include "lldb/Core/Value.h"
16184054Slulf
17184054Slulf#include "clang/AST/Decl.h"
18184054Slulf
19184054Slulf#include "llvm/ADT/StringMap.h"
20184054Slulf
21184054Slulfusing namespace lldb;
22184054Slulfusing namespace lldb_private;
23184054Slulf
24184054SlulfClangPersistentVariables::ClangPersistentVariables()
25184054Slulf    : lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang),
26184054Slulf      m_next_persistent_variable_id(0) {}
27184054Slulf
28184054SlulfExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
29184054Slulf    const lldb::ValueObjectSP &valobj_sp) {
30184054Slulf  return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));
31184054Slulf}
32184054Slulf
33184054SlulfExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
34184054Slulf    ExecutionContextScope *exe_scope, const ConstString &name,
35184054Slulf    const CompilerType &compiler_type, lldb::ByteOrder byte_order,
36184054Slulf    uint32_t addr_byte_size) {
37184054Slulf  return AddNewlyConstructedVariable(new ClangExpressionVariable(
38184054Slulf      exe_scope, name, compiler_type, byte_order, addr_byte_size));
39184054Slulf}
40184054Slulf
41184054Slulfvoid ClangPersistentVariables::RemovePersistentVariable(
42184054Slulf    lldb::ExpressionVariableSP variable) {
43184054Slulf  RemoveVariable(variable);
44184054Slulf
45184054Slulf  const char *name = variable->GetName().AsCString();
46184054Slulf
47184054Slulf  if (*name != '$')
48184054Slulf    return;
49184054Slulf  name++;
50184054Slulf
51184054Slulf  if (strtoul(name, NULL, 0) == m_next_persistent_variable_id - 1)
52184054Slulf    m_next_persistent_variable_id--;
53184054Slulf}
54184054Slulf
55184054SlulfConstString ClangPersistentVariables::GetNextPersistentVariableName() {
56184054Slulf  char name_cstr[256];
57184054Slulf  ::snprintf(name_cstr, sizeof(name_cstr), "$%u",
58184054Slulf             m_next_persistent_variable_id++);
59184054Slulf  ConstString name(name_cstr);
60184054Slulf  return name;
61184054Slulf}
62184054Slulf
63184054Slulfvoid ClangPersistentVariables::RegisterPersistentDecl(const ConstString &name,
64184054Slulf                                                      clang::NamedDecl *decl) {
65184054Slulf  m_persistent_decls.insert(
66184054Slulf      std::pair<const char *, clang::NamedDecl *>(name.GetCString(), decl));
67184054Slulf
68184054Slulf  if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
69184054Slulf    for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
70184054Slulf      m_persistent_decls.insert(std::pair<const char *, clang::NamedDecl *>(
71184054Slulf          ConstString(enumerator_decl->getNameAsString()).GetCString(),
72184054Slulf          enumerator_decl));
73184054Slulf    }
74184054Slulf  }
75184054Slulf}
76184054Slulf
77184054Slulfclang::NamedDecl *
78184054SlulfClangPersistentVariables::GetPersistentDecl(const ConstString &name) {
79184054Slulf  PersistentDeclMap::const_iterator i =
80184054Slulf      m_persistent_decls.find(name.GetCString());
81184054Slulf
82184054Slulf  if (i == m_persistent_decls.end())
83184054Slulf    return NULL;
84184054Slulf  else
85184054Slulf    return i->second;
86184054Slulf}
87184054Slulf