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 llvm::StringRef GetPersistentVariablePrefix(bool is_error) const override { 55 return "$"; 56 } 57 58 /// Returns the next file name that should be used for user expressions. 59 std::string GetNextExprFileName() { 60 std::string name; 61 name.append("<user expression "); 62 name.append(std::to_string(m_next_user_file_id++)); 63 name.append(">"); 64 return name; 65 } 66 67 llvm::Optional<CompilerType> 68 GetCompilerTypeFromPersistentDecl(ConstString type_name) override; 69 70 void RegisterPersistentDecl(ConstString name, clang::NamedDecl *decl, 71 TypeSystemClang *ctx); 72 73 clang::NamedDecl *GetPersistentDecl(ConstString name); 74 75 void AddHandLoadedClangModule(ClangModulesDeclVendor::ModuleID module) { 76 m_hand_loaded_clang_modules.push_back(module); 77 } 78 79 const ClangModulesDeclVendor::ModuleVector &GetHandLoadedClangModules() { 80 return m_hand_loaded_clang_modules; 81 } 82 83 private: 84 /// The counter used by GetNextExprFileName. 85 uint32_t m_next_user_file_id = 0; 86 // The counter used by GetNextPersistentVariableName 87 uint32_t m_next_persistent_variable_id = 0; 88 89 struct PersistentDecl { 90 /// The persistent decl. 91 clang::NamedDecl *m_decl = nullptr; 92 /// The TypeSystemClang for the ASTContext of m_decl. 93 TypeSystemClang *m_context = nullptr; 94 }; 95 96 typedef llvm::DenseMap<const char *, PersistentDecl> PersistentDeclMap; 97 PersistentDeclMap 98 m_persistent_decls; ///< Persistent entities declared by the user. 99 100 ClangModulesDeclVendor::ModuleVector 101 m_hand_loaded_clang_modules; ///< These are Clang modules we hand-loaded; 102 ///these are the highest- 103 ///< priority source for macros. 104 std::shared_ptr<ClangASTImporter> m_ast_importer_sp; 105 }; 106 107 } // namespace lldb_private 108 109 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGPERSISTENTVARIABLES_H 110