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