blob: 41d62a462ab4ee46dc68cac02968002d0ee9e5f1 [file] [log] [blame]
Sean Callanan2235f322010-08-11 03:57:18 +00001//===-- ClangPersistentVariables.cpp ----------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sean Callanan2235f322010-08-11 03:57:18 +00006//
7//===----------------------------------------------------------------------===//
8
Sean Callanan4dbb2712015-09-25 20:35:58 +00009#include "ClangPersistentVariables.h"
10
Sean Callanand1e5b432010-08-12 01:56:52 +000011#include "lldb/Core/Value.h"
Alex Langford5b999282019-06-12 17:47:06 +000012#include "lldb/Symbol/ClangASTContext.h"
Adrian Prantl5435f782018-04-30 23:59:15 +000013#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000014#include "lldb/Utility/DataExtractor.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000015#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000016#include "lldb/Utility/StreamString.h"
Sean Callanan2235f322010-08-11 03:57:18 +000017
Sean Callananb3a36df2016-03-19 00:51:43 +000018#include "clang/AST/Decl.h"
19
Sean Callananbccce812011-08-23 21:20:51 +000020#include "llvm/ADT/StringMap.h"
21
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000022using namespace lldb;
Sean Callanan2235f322010-08-11 03:57:18 +000023using namespace lldb_private;
Sean Callanand1e5b432010-08-12 01:56:52 +000024
Kate Stoneb9c1b512016-09-06 20:57:50 +000025ClangPersistentVariables::ClangPersistentVariables()
Raphael Isemann2fb7cf82019-08-05 13:40:39 +000026 : lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang) {}
Kate Stoneb9c1b512016-09-06 20:57:50 +000027
28ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
29 const lldb::ValueObjectSP &valobj_sp) {
30 return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));
Sean Callanan2235f322010-08-11 03:57:18 +000031}
Sean Callanand1e5b432010-08-12 01:56:52 +000032
Kate Stoneb9c1b512016-09-06 20:57:50 +000033ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
Adrian Prantl0e4c4822019-03-06 21:22:25 +000034 ExecutionContextScope *exe_scope, ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 const CompilerType &compiler_type, lldb::ByteOrder byte_order,
36 uint32_t addr_byte_size) {
37 return AddNewlyConstructedVariable(new ClangExpressionVariable(
38 exe_scope, name, compiler_type, byte_order, addr_byte_size));
Sean Callanand1e5b432010-08-12 01:56:52 +000039}
Sean Callanand0ef0ef2010-08-20 01:02:30 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041void ClangPersistentVariables::RemovePersistentVariable(
42 lldb::ExpressionVariableSP variable) {
43 RemoveVariable(variable);
44
Raphael Isemann9de71692019-08-05 21:43:53 +000045 // Check if the removed variable was the last one that was created. If yes,
46 // reuse the variable id for the next variable.
Kate Stoneb9c1b512016-09-06 20:57:50 +000047
Raphael Isemann9de71692019-08-05 21:43:53 +000048 // Nothing to do if we have not assigned a variable id so far.
49 if (m_next_persistent_variable_id == 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +000051
Raphael Isemann9de71692019-08-05 21:43:53 +000052 llvm::StringRef name = variable->GetName().GetStringRef();
53 // Remove the prefix from the variable that only the indes is left.
54 if (!name.consume_front(GetPersistentVariablePrefix(false)))
55 return;
56
57 // Check if the variable contained a variable id.
58 uint32_t variable_id;
59 if (name.getAsInteger(10, variable_id))
60 return;
61 // If it's the most recent variable id that was assigned, make sure that this
62 // variable id will be used for the next persistent variable.
63 if (variable_id == m_next_persistent_variable_id - 1)
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 m_next_persistent_variable_id--;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000065}
66
Alex Langford5b999282019-06-12 17:47:06 +000067llvm::Optional<CompilerType>
68ClangPersistentVariables::GetCompilerTypeFromPersistentDecl(
69 ConstString type_name) {
Raphael Isemann36fb1992019-12-28 22:00:27 +010070 PersistentDecl p = m_persistent_decls.lookup(type_name.GetCString());
71
72 if (p.m_decl == nullptr)
73 return llvm::None;
74
75 if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {
76 opaque_compiler_type_t t = static_cast<opaque_compiler_type_t>(
77 const_cast<clang::Type *>(tdecl->getTypeForDecl()));
78 return CompilerType(p.m_context, t);
Alex Langford5b999282019-06-12 17:47:06 +000079 }
80 return llvm::None;
81}
82
Adrian Prantl0e4c4822019-03-06 21:22:25 +000083void ClangPersistentVariables::RegisterPersistentDecl(ConstString name,
Raphael Isemann36fb1992019-12-28 22:00:27 +010084 clang::NamedDecl *decl,
85 ClangASTContext *ctx) {
86 PersistentDecl p = {decl, ctx};
87 m_persistent_decls.insert(std::make_pair(name.GetCString(), p));
Sean Callananbccce812011-08-23 21:20:51 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
90 for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
Raphael Isemann36fb1992019-12-28 22:00:27 +010091 p = {enumerator_decl, ctx};
92 m_persistent_decls.insert(std::make_pair(
93 ConstString(enumerator_decl->getNameAsString()).GetCString(), p));
Sean Callananb3a36df2016-03-19 00:51:43 +000094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 }
Sean Callananbccce812011-08-23 21:20:51 +000096}
97
Sean Callananb3a36df2016-03-19 00:51:43 +000098clang::NamedDecl *
Adrian Prantl0e4c4822019-03-06 21:22:25 +000099ClangPersistentVariables::GetPersistentDecl(ConstString name) {
Raphael Isemann36fb1992019-12-28 22:00:27 +0100100 return m_persistent_decls.lookup(name.GetCString()).m_decl;
Sean Callananbccce812011-08-23 21:20:51 +0000101}