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