1 //===-- ClangPersistentVariables.cpp ----------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ClangPersistentVariables.h"
10 
11 #include "lldb/Core/Value.h"
12 #include "lldb/Symbol/ClangASTContext.h"
13 #include "lldb/Target/Target.h"
14 #include "lldb/Utility/DataExtractor.h"
15 #include "lldb/Utility/Log.h"
16 #include "lldb/Utility/StreamString.h"
17 
18 #include "clang/AST/Decl.h"
19 
20 #include "llvm/ADT/StringMap.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 ClangPersistentVariables::ClangPersistentVariables()
26     : lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang),
27       m_next_persistent_variable_id(0) {}
28 
29 ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
30     const lldb::ValueObjectSP &valobj_sp) {
31   return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));
32 }
33 
34 ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
35     ExecutionContextScope *exe_scope, ConstString name,
36     const CompilerType &compiler_type, lldb::ByteOrder byte_order,
37     uint32_t addr_byte_size) {
38   return AddNewlyConstructedVariable(new ClangExpressionVariable(
39       exe_scope, name, compiler_type, byte_order, addr_byte_size));
40 }
41 
42 void ClangPersistentVariables::RemovePersistentVariable(
43     lldb::ExpressionVariableSP variable) {
44   RemoveVariable(variable);
45 
46   const char *name = variable->GetName().AsCString();
47 
48   if (*name != '$')
49     return;
50   name++;
51 
52   if (strtoul(name, nullptr, 0) == m_next_persistent_variable_id - 1)
53     m_next_persistent_variable_id--;
54 }
55 
56 llvm::Optional<CompilerType>
57 ClangPersistentVariables::GetCompilerTypeFromPersistentDecl(
58     ConstString type_name) {
59   CompilerType compiler_type;
60   if (clang::TypeDecl *tdecl = llvm::dyn_cast_or_null<clang::TypeDecl>(
61           GetPersistentDecl(type_name))) {
62     compiler_type.SetCompilerType(
63         ClangASTContext::GetASTContext(&tdecl->getASTContext()),
64         reinterpret_cast<lldb::opaque_compiler_type_t>(
65             const_cast<clang::Type *>(tdecl->getTypeForDecl())));
66     return compiler_type;
67   }
68   return llvm::None;
69 }
70 
71 void ClangPersistentVariables::RegisterPersistentDecl(ConstString name,
72                                                       clang::NamedDecl *decl) {
73   m_persistent_decls.insert(
74       std::pair<const char *, clang::NamedDecl *>(name.GetCString(), decl));
75 
76   if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
77     for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
78       m_persistent_decls.insert(std::pair<const char *, clang::NamedDecl *>(
79           ConstString(enumerator_decl->getNameAsString()).GetCString(),
80           enumerator_decl));
81     }
82   }
83 }
84 
85 clang::NamedDecl *
86 ClangPersistentVariables::GetPersistentDecl(ConstString name) {
87   PersistentDeclMap::const_iterator i =
88       m_persistent_decls.find(name.GetCString());
89 
90   if (i == m_persistent_decls.end())
91     return nullptr;
92   else
93     return i->second;
94 }
95