xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.h (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1061da546Spatrick //===-- ClangPersistentVariables.h ------------------------------*- C++ -*-===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9dda28197Spatrick #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGPERSISTENTVARIABLES_H
10dda28197Spatrick #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGPERSISTENTVARIABLES_H
11061da546Spatrick 
12061da546Spatrick #include "llvm/ADT/DenseMap.h"
13061da546Spatrick 
14061da546Spatrick #include "ClangExpressionVariable.h"
15061da546Spatrick #include "ClangModulesDeclVendor.h"
16061da546Spatrick 
17061da546Spatrick #include "lldb/Expression/ExpressionVariable.h"
18*f6aab3d8Srobert #include <optional>
19061da546Spatrick 
20061da546Spatrick namespace lldb_private {
21061da546Spatrick 
22dda28197Spatrick class ClangASTImporter;
23be691f3bSpatrick class ClangModulesDeclVendor;
24be691f3bSpatrick class Target;
25dda28197Spatrick class TypeSystemClang;
26dda28197Spatrick 
27061da546Spatrick /// \class ClangPersistentVariables ClangPersistentVariables.h
28061da546Spatrick /// "lldb/Expression/ClangPersistentVariables.h" Manages persistent values
29061da546Spatrick /// that need to be preserved between expression invocations.
30061da546Spatrick ///
31061da546Spatrick /// A list of variables that can be accessed and updated by any expression.  See
32061da546Spatrick /// ClangPersistentVariable for more discussion.  Also provides an increasing,
33061da546Spatrick /// 0-based counter for naming result variables.
34061da546Spatrick class ClangPersistentVariables : public PersistentExpressionState {
35061da546Spatrick public:
36be691f3bSpatrick   ClangPersistentVariables(std::shared_ptr<Target> target_sp);
37061da546Spatrick 
38061da546Spatrick   ~ClangPersistentVariables() override = default;
39061da546Spatrick 
40061da546Spatrick   // llvm casting support
classof(const PersistentExpressionState * pv)41061da546Spatrick   static bool classof(const PersistentExpressionState *pv) {
42061da546Spatrick     return pv->getKind() == PersistentExpressionState::eKindClang;
43061da546Spatrick   }
44061da546Spatrick 
45dda28197Spatrick   std::shared_ptr<ClangASTImporter> GetClangASTImporter();
46be691f3bSpatrick   std::shared_ptr<ClangModulesDeclVendor> GetClangModulesDeclVendor();
47dda28197Spatrick 
48061da546Spatrick   lldb::ExpressionVariableSP
49061da546Spatrick   CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) override;
50061da546Spatrick 
51061da546Spatrick   lldb::ExpressionVariableSP CreatePersistentVariable(
52061da546Spatrick       ExecutionContextScope *exe_scope, ConstString name,
53061da546Spatrick       const CompilerType &compiler_type, lldb::ByteOrder byte_order,
54061da546Spatrick       uint32_t addr_byte_size) override;
55061da546Spatrick 
56061da546Spatrick   void RemovePersistentVariable(lldb::ExpressionVariableSP variable) override;
57061da546Spatrick 
58dda28197Spatrick   ConstString GetNextPersistentVariableName(bool is_error = false) override;
59061da546Spatrick 
60061da546Spatrick   /// Returns the next file name that should be used for user expressions.
GetNextExprFileName()61061da546Spatrick   std::string GetNextExprFileName() {
62061da546Spatrick     std::string name;
63061da546Spatrick     name.append("<user expression ");
64061da546Spatrick     name.append(std::to_string(m_next_user_file_id++));
65061da546Spatrick     name.append(">");
66061da546Spatrick     return name;
67061da546Spatrick   }
68061da546Spatrick 
69*f6aab3d8Srobert   std::optional<CompilerType>
70061da546Spatrick   GetCompilerTypeFromPersistentDecl(ConstString type_name) override;
71061da546Spatrick 
72061da546Spatrick   void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl,
73*f6aab3d8Srobert                               std::shared_ptr<TypeSystemClang> ctx);
74061da546Spatrick 
75061da546Spatrick   clang::NamedDecl *GetPersistentDecl(ConstString name);
76061da546Spatrick 
AddHandLoadedClangModule(ClangModulesDeclVendor::ModuleID module)77061da546Spatrick   void AddHandLoadedClangModule(ClangModulesDeclVendor::ModuleID module) {
78061da546Spatrick     m_hand_loaded_clang_modules.push_back(module);
79061da546Spatrick   }
80061da546Spatrick 
GetHandLoadedClangModules()81061da546Spatrick   const ClangModulesDeclVendor::ModuleVector &GetHandLoadedClangModules() {
82061da546Spatrick     return m_hand_loaded_clang_modules;
83061da546Spatrick   }
84061da546Spatrick 
85dda28197Spatrick protected:
86dda28197Spatrick   llvm::StringRef
87dda28197Spatrick   GetPersistentVariablePrefix(bool is_error = false) const override {
88dda28197Spatrick     return "$";
89dda28197Spatrick   }
90dda28197Spatrick 
91061da546Spatrick private:
92061da546Spatrick   /// The counter used by GetNextExprFileName.
93061da546Spatrick   uint32_t m_next_user_file_id = 0;
94061da546Spatrick   // The counter used by GetNextPersistentVariableName
95061da546Spatrick   uint32_t m_next_persistent_variable_id = 0;
96061da546Spatrick 
97061da546Spatrick   struct PersistentDecl {
98061da546Spatrick     /// The persistent decl.
99061da546Spatrick     clang::NamedDecl *m_decl = nullptr;
100dda28197Spatrick     /// The TypeSystemClang for the ASTContext of m_decl.
101*f6aab3d8Srobert     lldb::TypeSystemWP m_context;
102061da546Spatrick   };
103061da546Spatrick 
104061da546Spatrick   typedef llvm::DenseMap<const char *, PersistentDecl> PersistentDeclMap;
105061da546Spatrick   PersistentDeclMap
106061da546Spatrick       m_persistent_decls; ///< Persistent entities declared by the user.
107061da546Spatrick 
108061da546Spatrick   ClangModulesDeclVendor::ModuleVector
109061da546Spatrick       m_hand_loaded_clang_modules; ///< These are Clang modules we hand-loaded;
110061da546Spatrick                                    ///these are the highest-
111061da546Spatrick                                    ///< priority source for macros.
112dda28197Spatrick   std::shared_ptr<ClangASTImporter> m_ast_importer_sp;
113be691f3bSpatrick   std::shared_ptr<ClangModulesDeclVendor> m_modules_decl_vendor_sp;
114be691f3bSpatrick   std::shared_ptr<Target> m_target_sp;
115061da546Spatrick };
116061da546Spatrick 
117061da546Spatrick } // namespace lldb_private
118061da546Spatrick 
119dda28197Spatrick #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGPERSISTENTVARIABLES_H
120