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 "clang/AST/Decl.h"
18 
19 #include "llvm/ADT/StringMap.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 ClangPersistentVariables::ClangPersistentVariables () :
25     lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang),
26     m_next_persistent_variable_id (0)
27 {
28 }
29 
30 ExpressionVariableSP
31 ClangPersistentVariables::CreatePersistentVariable (const lldb::ValueObjectSP &valobj_sp)
32 {
33     return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));
34 }
35 
36 ExpressionVariableSP
37 ClangPersistentVariables::CreatePersistentVariable (ExecutionContextScope *exe_scope,
38                                                     const ConstString &name,
39                                                     const CompilerType& compiler_type,
40                                                     lldb::ByteOrder byte_order,
41                                                     uint32_t addr_byte_size)
42 {
43     return AddNewlyConstructedVariable(new ClangExpressionVariable(exe_scope, name, compiler_type, byte_order, addr_byte_size));
44 }
45 
46 void
47 ClangPersistentVariables::RemovePersistentVariable (lldb::ExpressionVariableSP variable)
48 {
49     RemoveVariable(variable);
50 
51     const char *name = variable->GetName().AsCString();
52 
53     if (*name != '$')
54         return;
55     name++;
56 
57     if (strtoul(name, NULL, 0) == m_next_persistent_variable_id - 1)
58         m_next_persistent_variable_id--;
59 }
60 
61 ConstString
62 ClangPersistentVariables::GetNextPersistentVariableName ()
63 {
64     char name_cstr[256];
65     ::snprintf (name_cstr, sizeof(name_cstr), "$%u", m_next_persistent_variable_id++);
66     ConstString name(name_cstr);
67     return name;
68 }
69 
70 void
71 ClangPersistentVariables::RegisterPersistentDecl (const ConstString &name,
72                                                   clang::NamedDecl *decl)
73 {
74     m_persistent_decls.insert(std::pair<const char*, clang::NamedDecl*>(name.GetCString(), decl));
75 
76     if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl))
77     {
78         for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators())
79         {
80             m_persistent_decls.insert(std::pair<const char*, clang::NamedDecl*>(ConstString(enumerator_decl->getNameAsString()).GetCString(), enumerator_decl));
81         }
82     }
83 }
84 
85 clang::NamedDecl *
86 ClangPersistentVariables::GetPersistentDecl (const ConstString &name)
87 {
88     PersistentDeclMap::const_iterator i = m_persistent_decls.find(name.GetCString());
89 
90     if (i == m_persistent_decls.end())
91         return NULL;
92     else
93         return i->second;
94 }
95