Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionVariable.h
39654 views
//===-- ClangExpressionVariable.h -------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONVARIABLE_H9#define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONVARIABLE_H1011#include <csignal>12#include <cstdint>13#include <cstring>1415#include <map>16#include <string>17#include <vector>1819#include "llvm/Support/Casting.h"2021#include "lldb/Core/Value.h"22#include "lldb/Expression/ExpressionVariable.h"23#include "lldb/Symbol/TaggedASTType.h"24#include "lldb/Utility/ConstString.h"25#include "lldb/lldb-public.h"2627namespace llvm {28class Value;29}3031namespace clang {32class NamedDecl;33}3435namespace lldb_private {3637class ValueObjectConstResult;3839/// \class ClangExpressionVariable ClangExpressionVariable.h40/// "lldb/Expression/ClangExpressionVariable.h" Encapsulates one variable for41/// the expression parser.42///43/// The expression parser uses variables in three different contexts:44///45/// First, it stores persistent variables along with the process for use in46/// expressions. These persistent variables contain their own data and are47/// typed.48///49/// Second, in an interpreted expression, it stores the local variables for50/// the expression along with the expression. These variables contain their51/// own data and are typed.52///53/// Third, in a JIT-compiled expression, it stores the variables that the54/// expression needs to have materialized and dematerialized at each55/// execution. These do not contain their own data but are named and typed.56///57/// This class supports all of these use cases using simple type polymorphism,58/// and provides necessary support methods. Its interface is RTTI-neutral.59class ClangExpressionVariable60: public llvm::RTTIExtends<ClangExpressionVariable, ExpressionVariable> {61public:62// LLVM RTTI support63static char ID;6465ClangExpressionVariable(ExecutionContextScope *exe_scope,66lldb::ByteOrder byte_order, uint32_t addr_byte_size);6768ClangExpressionVariable(ExecutionContextScope *exe_scope, Value &value,69ConstString name, uint16_t flags = EVNone);7071ClangExpressionVariable(const lldb::ValueObjectSP &valobj_sp);7273ClangExpressionVariable(ExecutionContextScope *exe_scope,74ConstString name,75const TypeFromUser &user_type,76lldb::ByteOrder byte_order, uint32_t addr_byte_size);7778/// Utility functions for dealing with ExpressionVariableLists in Clang-79/// specific ways8081/// Finds a variable by NamedDecl in the list.82///83/// \return84/// The variable requested, or NULL if that variable is not in the list.85static ClangExpressionVariable *86FindVariableInList(ExpressionVariableList &list, const clang::NamedDecl *decl,87uint64_t parser_id) {88lldb::ExpressionVariableSP var_sp;89for (size_t index = 0, size = list.GetSize(); index < size; ++index) {90var_sp = list.GetVariableAtIndex(index);9192if (ClangExpressionVariable *clang_var =93llvm::dyn_cast<ClangExpressionVariable>(var_sp.get())) {94ClangExpressionVariable::ParserVars *parser_vars =95clang_var->GetParserVars(parser_id);9697if (parser_vars && parser_vars->m_named_decl == decl)98return clang_var;99}100}101return nullptr;102}103104/// If the variable contains its own data, make a Value point at it. If \a105/// exe_ctx in not NULL, the value will be resolved in with that execution106/// context.107///108/// \param[in] value109/// The value to point at the data.110///111/// \param[in] exe_ctx112/// The execution context to use to resolve \a value.113///114/// \return115/// True on success; false otherwise (in particular, if this variable116/// does not contain its own data).117bool PointValueAtData(Value &value, ExecutionContext *exe_ctx);118119/// The following values should not live beyond parsing120class ParserVars {121public:122ParserVars() = default;123124const clang::NamedDecl *m_named_decl =125nullptr; ///< The Decl corresponding to this variable126llvm::Value *m_llvm_value =127nullptr; ///< The IR value corresponding to this variable;128/// usually a GlobalValue129lldb_private::Value130m_lldb_value; ///< The value found in LLDB for this variable131lldb::VariableSP m_lldb_var; ///< The original variable for this variable132const lldb_private::Symbol *m_lldb_sym =133nullptr; ///< The original symbol for this134/// variable, if it was a symbol135136/// Callback that provides a ValueObject for the137/// specified frame. Used by the materializer for138/// re-fetching ValueObjects when materializing139/// ivars.140ValueObjectProviderTy m_lldb_valobj_provider;141};142143private:144typedef std::map<uint64_t, ParserVars> ParserVarMap;145ParserVarMap m_parser_vars;146147public:148/// Make this variable usable by the parser by allocating space for parser-149/// specific variables150void EnableParserVars(uint64_t parser_id) {151m_parser_vars.insert(std::make_pair(parser_id, ParserVars()));152}153154/// Deallocate parser-specific variables155void DisableParserVars(uint64_t parser_id) { m_parser_vars.erase(parser_id); }156157/// Access parser-specific variables158ParserVars *GetParserVars(uint64_t parser_id) {159ParserVarMap::iterator i = m_parser_vars.find(parser_id);160161if (i == m_parser_vars.end())162return nullptr;163else164return &i->second;165}166167/// The following values are valid if the variable is used by JIT code168struct JITVars {169JITVars() = default;170171lldb::offset_t m_alignment =1720; ///< The required alignment of the variable, in bytes173size_t m_size = 0; ///< The space required for the variable, in bytes174lldb::offset_t m_offset =1750; ///< The offset of the variable in the struct, in bytes176};177178private:179typedef std::map<uint64_t, JITVars> JITVarMap;180JITVarMap m_jit_vars;181182public:183/// Make this variable usable for materializing for the JIT by allocating184/// space for JIT-specific variables185void EnableJITVars(uint64_t parser_id) {186m_jit_vars.insert(std::make_pair(parser_id, JITVars()));187}188189/// Deallocate JIT-specific variables190void DisableJITVars(uint64_t parser_id) { m_jit_vars.erase(parser_id); }191192JITVars *GetJITVars(uint64_t parser_id) {193JITVarMap::iterator i = m_jit_vars.find(parser_id);194195if (i == m_jit_vars.end())196return nullptr;197else198return &i->second;199}200201TypeFromUser GetTypeFromUser();202203/// Members204ClangExpressionVariable(const ClangExpressionVariable &) = delete;205const ClangExpressionVariable &206operator=(const ClangExpressionVariable &) = delete;207};208209} // namespace lldb_private210211#endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONVARIABLE_H212213214