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