Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:47

0001 //===-- ExpressionVariable.h ------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_EXPRESSION_EXPRESSIONVARIABLE_H
0010 #define LLDB_EXPRESSION_EXPRESSIONVARIABLE_H
0011 
0012 #include <memory>
0013 #include <optional>
0014 #include <vector>
0015 
0016 #include "llvm/ADT/DenseMap.h"
0017 
0018 #include "lldb/Utility/ConstString.h"
0019 #include "lldb/ValueObject/ValueObject.h"
0020 #include "lldb/lldb-public.h"
0021 #include "llvm/Support/ExtensibleRTTI.h"
0022 
0023 namespace lldb_private {
0024 
0025 class ExpressionVariable
0026     : public std::enable_shared_from_this<ExpressionVariable>,
0027       public llvm::RTTIExtends<ExpressionVariable, llvm::RTTIRoot> {
0028 public:
0029   /// LLVM RTTI support
0030   static char ID;
0031 
0032   ExpressionVariable();
0033 
0034   virtual ~ExpressionVariable() = default;
0035 
0036   std::optional<uint64_t> GetByteSize() { return m_frozen_sp->GetByteSize(); }
0037 
0038   ConstString GetName() { return m_frozen_sp->GetName(); }
0039 
0040   lldb::ValueObjectSP GetValueObject() { return m_frozen_sp; }
0041 
0042   uint8_t *GetValueBytes();
0043 
0044   void ValueUpdated() { m_frozen_sp->ValueUpdated(); }
0045 
0046   RegisterInfo *GetRegisterInfo() {
0047     return m_frozen_sp->GetValue().GetRegisterInfo();
0048   }
0049 
0050   void SetRegisterInfo(const RegisterInfo *reg_info) {
0051     return m_frozen_sp->GetValue().SetContext(
0052         Value::ContextType::RegisterInfo, const_cast<RegisterInfo *>(reg_info));
0053   }
0054 
0055   CompilerType GetCompilerType() { return m_frozen_sp->GetCompilerType(); }
0056 
0057   void SetCompilerType(const CompilerType &compiler_type) {
0058     m_frozen_sp->GetValue().SetCompilerType(compiler_type);
0059   }
0060 
0061   void SetName(ConstString name) { m_frozen_sp->SetName(name); }
0062 
0063   // this function is used to copy the address-of m_live_sp into m_frozen_sp
0064   // this is necessary because the results of certain cast and pointer-
0065   // arithmetic operations (such as those described in bugzilla issues 11588
0066   // and 11618) generate frozen objects that do not have a valid address-of,
0067   // which can be troublesome when using synthetic children providers.
0068   // Transferring the address-of the live object solves these issues and
0069   // provides the expected user-level behavior
0070   void TransferAddress(bool force = false) {
0071     if (m_live_sp.get() == nullptr)
0072       return;
0073 
0074     if (m_frozen_sp.get() == nullptr)
0075       return;
0076 
0077     if (force || (m_frozen_sp->GetLiveAddress() == LLDB_INVALID_ADDRESS))
0078       m_frozen_sp->SetLiveAddress(m_live_sp->GetLiveAddress());
0079   }
0080 
0081   enum Flags {
0082     EVNone = 0,
0083     EVIsLLDBAllocated = 1 << 0, ///< This variable is resident in a location
0084                                 ///specifically allocated for it by LLDB in the
0085                                 ///target process
0086     EVIsProgramReference = 1 << 1, ///< This variable is a reference to a
0087                                    ///(possibly invalid) area managed by the
0088                                    ///target program
0089     EVNeedsAllocation = 1 << 2,    ///< Space for this variable has yet to be
0090                                    ///allocated in the target process
0091     EVIsFreezeDried = 1 << 3, ///< This variable's authoritative version is in
0092                               ///m_frozen_sp (for example, for
0093                               ///statically-computed results)
0094     EVNeedsFreezeDry =
0095         1 << 4, ///< Copy from m_live_sp to m_frozen_sp during dematerialization
0096     EVKeepInTarget = 1 << 5, ///< Keep the allocation after the expression is
0097                              ///complete rather than freeze drying its contents
0098                              ///and freeing it
0099     EVTypeIsReference = 1 << 6, ///< The original type of this variable is a
0100                                 ///reference, so materialize the value rather
0101                                 ///than the location
0102     EVBareRegister = 1 << 7 ///< This variable is a direct reference to $pc or
0103                             ///some other entity.
0104   };
0105 
0106   typedef uint16_t FlagType;
0107 
0108   FlagType m_flags; // takes elements of Flags
0109 
0110   // these should be private
0111   lldb::ValueObjectSP m_frozen_sp;
0112   lldb::ValueObjectSP m_live_sp;
0113 };
0114 
0115 /// \class ExpressionVariableList ExpressionVariable.h
0116 /// "lldb/Expression/ExpressionVariable.h"
0117 /// A list of variable references.
0118 ///
0119 /// This class stores variables internally, acting as the permanent store.
0120 class ExpressionVariableList {
0121 public:
0122   /// Implementation of methods in ExpressionVariableListBase
0123   size_t GetSize() { return m_variables.size(); }
0124 
0125   lldb::ExpressionVariableSP GetVariableAtIndex(size_t index) {
0126     lldb::ExpressionVariableSP var_sp;
0127     if (index < m_variables.size())
0128       var_sp = m_variables[index];
0129     return var_sp;
0130   }
0131 
0132   size_t AddVariable(const lldb::ExpressionVariableSP &var_sp) {
0133     m_variables.push_back(var_sp);
0134     return m_variables.size() - 1;
0135   }
0136 
0137   lldb::ExpressionVariableSP
0138   AddNewlyConstructedVariable(ExpressionVariable *var) {
0139     lldb::ExpressionVariableSP var_sp(var);
0140     m_variables.push_back(var_sp);
0141     return m_variables.back();
0142   }
0143 
0144   bool ContainsVariable(const lldb::ExpressionVariableSP &var_sp) {
0145     const size_t size = m_variables.size();
0146     for (size_t index = 0; index < size; ++index) {
0147       if (m_variables[index].get() == var_sp.get())
0148         return true;
0149     }
0150     return false;
0151   }
0152 
0153   /// Finds a variable by name in the list.
0154   ///
0155   /// \param[in] name
0156   ///     The name of the requested variable.
0157   ///
0158   /// \return
0159   ///     The variable requested, or nullptr if that variable is not in the
0160   ///     list.
0161   lldb::ExpressionVariableSP GetVariable(ConstString name) {
0162     lldb::ExpressionVariableSP var_sp;
0163     for (size_t index = 0, size = GetSize(); index < size; ++index) {
0164       var_sp = GetVariableAtIndex(index);
0165       if (var_sp->GetName() == name)
0166         return var_sp;
0167     }
0168     var_sp.reset();
0169     return var_sp;
0170   }
0171 
0172   lldb::ExpressionVariableSP GetVariable(llvm::StringRef name) {
0173     if (name.empty())
0174       return nullptr;
0175 
0176     for (size_t index = 0, size = GetSize(); index < size; ++index) {
0177       auto var_sp = GetVariableAtIndex(index);
0178       llvm::StringRef var_name_str = var_sp->GetName().GetStringRef();
0179       if (var_name_str == name)
0180         return var_sp;
0181     }
0182     return nullptr;
0183   }
0184 
0185   void RemoveVariable(lldb::ExpressionVariableSP var_sp) {
0186     for (std::vector<lldb::ExpressionVariableSP>::iterator
0187              vi = m_variables.begin(),
0188              ve = m_variables.end();
0189          vi != ve; ++vi) {
0190       if (vi->get() == var_sp.get()) {
0191         m_variables.erase(vi);
0192         return;
0193       }
0194     }
0195   }
0196 
0197   void Clear() { m_variables.clear(); }
0198 
0199 private:
0200   std::vector<lldb::ExpressionVariableSP> m_variables;
0201 };
0202 
0203 class PersistentExpressionState
0204     : public ExpressionVariableList,
0205       public llvm::RTTIExtends<PersistentExpressionState, llvm::RTTIRoot> {
0206 public:
0207   /// LLVM RTTI support
0208   static char ID;
0209 
0210   PersistentExpressionState();
0211 
0212   virtual ~PersistentExpressionState();
0213 
0214   virtual lldb::ExpressionVariableSP
0215   CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) = 0;
0216 
0217   virtual lldb::ExpressionVariableSP
0218   CreatePersistentVariable(ExecutionContextScope *exe_scope,
0219                            ConstString name, const CompilerType &type,
0220                            lldb::ByteOrder byte_order,
0221                            uint32_t addr_byte_size) = 0;
0222 
0223   /// Return a new persistent variable name with the specified prefix.
0224   virtual ConstString GetNextPersistentVariableName(bool is_error = false) = 0;
0225 
0226   virtual void
0227   RemovePersistentVariable(lldb::ExpressionVariableSP variable) = 0;
0228 
0229   virtual std::optional<CompilerType>
0230   GetCompilerTypeFromPersistentDecl(ConstString type_name) = 0;
0231 
0232   virtual lldb::addr_t LookupSymbol(ConstString name);
0233 
0234   void RegisterExecutionUnit(lldb::IRExecutionUnitSP &execution_unit_sp);
0235 
0236 protected:
0237   virtual llvm::StringRef
0238   GetPersistentVariablePrefix(bool is_error = false) const = 0;
0239 
0240 private:
0241   typedef std::set<lldb::IRExecutionUnitSP> ExecutionUnitSet;
0242   ExecutionUnitSet
0243       m_execution_units; ///< The execution units that contain valuable symbols.
0244 
0245   typedef llvm::DenseMap<const char *, lldb::addr_t> SymbolMap;
0246   SymbolMap
0247       m_symbol_map; ///< The addresses of the symbols in m_execution_units.
0248 };
0249 
0250 } // namespace lldb_private
0251 
0252 #endif // LLDB_EXPRESSION_EXPRESSIONVARIABLE_H