1 //===-- ClangPersistentVariables.h ------------------------------*- 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 #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGPERSISTENTVARIABLES_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGPERSISTENTVARIABLES_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 
14 #include "ClangExpressionVariable.h"
15 #include "ClangModulesDeclVendor.h"
16 
17 #include "lldb/Expression/ExpressionVariable.h"
18 #include <optional>
19 
20 namespace lldb_private {
21 
22 class ClangASTImporter;
23 class ClangModulesDeclVendor;
24 class Target;
25 class TypeSystemClang;
26 
27 /// \class ClangPersistentVariables ClangPersistentVariables.h
28 /// "lldb/Expression/ClangPersistentVariables.h" Manages persistent values
29 /// that need to be preserved between expression invocations.
30 ///
31 /// A list of variables that can be accessed and updated by any expression.  See
32 /// ClangPersistentVariable for more discussion.  Also provides an increasing,
33 /// 0-based counter for naming result variables.
34 class ClangPersistentVariables : public PersistentExpressionState {
35 public:
36   ClangPersistentVariables(std::shared_ptr<Target> target_sp);
37 
38   ~ClangPersistentVariables() override = default;
39 
40   // llvm casting support
41   static bool classof(const PersistentExpressionState *pv) {
42     return pv->getKind() == PersistentExpressionState::eKindClang;
43   }
44 
45   std::shared_ptr<ClangASTImporter> GetClangASTImporter();
46   std::shared_ptr<ClangModulesDeclVendor> GetClangModulesDeclVendor();
47 
48   lldb::ExpressionVariableSP
49   CreatePersistentVariable(const lldb::ValueObjectSP &valobj_sp) override;
50 
51   lldb::ExpressionVariableSP CreatePersistentVariable(
52       ExecutionContextScope *exe_scope, ConstString name,
53       const CompilerType &compiler_type, lldb::ByteOrder byte_order,
54       uint32_t addr_byte_size) override;
55 
56   void RemovePersistentVariable(lldb::ExpressionVariableSP variable) override;
57 
58   ConstString GetNextPersistentVariableName(bool is_error = false) override;
59 
60   /// Returns the next file name that should be used for user expressions.
61   std::string GetNextExprFileName() {
62     std::string name;
63     name.append("<user expression ");
64     name.append(std::to_string(m_next_user_file_id++));
65     name.append(">");
66     return name;
67   }
68 
69   std::optional<CompilerType>
70   GetCompilerTypeFromPersistentDecl(ConstString type_name) override;
71 
72   void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl,
73                               std::shared_ptr<TypeSystemClang> ctx);
74 
75   clang::NamedDecl *GetPersistentDecl(ConstString name);
76 
77   void AddHandLoadedClangModule(ClangModulesDeclVendor::ModuleID module) {
78     m_hand_loaded_clang_modules.push_back(module);
79   }
80 
81   const ClangModulesDeclVendor::ModuleVector &GetHandLoadedClangModules() {
82     return m_hand_loaded_clang_modules;
83   }
84 
85 protected:
86   llvm::StringRef
87   GetPersistentVariablePrefix(bool is_error = false) const override {
88     return "$";
89   }
90 
91 private:
92   /// The counter used by GetNextExprFileName.
93   uint32_t m_next_user_file_id = 0;
94   // The counter used by GetNextPersistentVariableName
95   uint32_t m_next_persistent_variable_id = 0;
96 
97   struct PersistentDecl {
98     /// The persistent decl.
99     clang::NamedDecl *m_decl = nullptr;
100     /// The TypeSystemClang for the ASTContext of m_decl.
101     lldb::TypeSystemWP m_context;
102   };
103 
104   typedef llvm::DenseMap<const char *, PersistentDecl> PersistentDeclMap;
105   PersistentDeclMap
106       m_persistent_decls; ///< Persistent entities declared by the user.
107 
108   ClangModulesDeclVendor::ModuleVector
109       m_hand_loaded_clang_modules; ///< These are Clang modules we hand-loaded;
110                                    ///these are the highest-
111                                    ///< priority source for macros.
112   std::shared_ptr<ClangASTImporter> m_ast_importer_sp;
113   std::shared_ptr<ClangModulesDeclVendor> m_modules_decl_vendor_sp;
114   std::shared_ptr<Target> m_target_sp;
115 };
116 
117 } // namespace lldb_private
118 
119 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGPERSISTENTVARIABLES_H
120