xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.h (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 //===-- ClangUtilityFunction.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_CLANGUTILITYFUNCTION_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGUTILITYFUNCTION_H
11 
12 #include <map>
13 #include <string>
14 #include <vector>
15 
16 #include "ClangExpressionHelper.h"
17 
18 #include "lldb/Expression/UtilityFunction.h"
19 #include "lldb/lldb-forward.h"
20 #include "lldb/lldb-private.h"
21 
22 namespace lldb_private {
23 
24 /// \class ClangUtilityFunction ClangUtilityFunction.h
25 /// "lldb/Expression/ClangUtilityFunction.h" Encapsulates a single expression
26 /// for use with Clang
27 ///
28 /// LLDB uses expressions for various purposes, notably to call functions
29 /// and as a backend for the expr command.  ClangUtilityFunction encapsulates
30 /// a self-contained function meant to be used from other code.  Utility
31 /// functions can perform error-checking for ClangUserExpressions, or can
32 /// simply provide a way to push a function into the target for the debugger
33 /// to call later on.
34 class ClangUtilityFunction : public UtilityFunction {
35   // LLVM RTTI support
36   static char ID;
37 
38 public:
39   bool isA(const void *ClassID) const override {
40     return ClassID == &ID || UtilityFunction::isA(ClassID);
41   }
42   static bool classof(const Expression *obj) { return obj->isA(&ID); }
43 
44   class ClangUtilityFunctionHelper : public ClangExpressionHelper {
45   public:
46     ClangUtilityFunctionHelper() {}
47 
48     ~ClangUtilityFunctionHelper() override {}
49 
50     /// Return the object that the parser should use when resolving external
51     /// values.  May be NULL if everything should be self-contained.
52     ClangExpressionDeclMap *DeclMap() override {
53       return m_expr_decl_map_up.get();
54     }
55 
56     void ResetDeclMap() { m_expr_decl_map_up.reset(); }
57 
58     void ResetDeclMap(ExecutionContext &exe_ctx, bool keep_result_in_memory);
59 
60     /// Return the object that the parser should allow to access ASTs. May be
61     /// NULL if the ASTs do not need to be transformed.
62     ///
63     /// \param[in] passthrough
64     ///     The ASTConsumer that the returned transformer should send
65     ///     the ASTs to after transformation.
66     clang::ASTConsumer *
67     ASTTransformer(clang::ASTConsumer *passthrough) override {
68       return nullptr;
69     }
70 
71   private:
72     std::unique_ptr<ClangExpressionDeclMap> m_expr_decl_map_up;
73   };
74   /// Constructor
75   ///
76   /// \param[in] text
77   ///     The text of the function.  Must be a full translation unit.
78   ///
79   /// \param[in] name
80   ///     The name of the function, as used in the text.
81   ClangUtilityFunction(ExecutionContextScope &exe_scope, const char *text,
82                        const char *name);
83 
84   ~ClangUtilityFunction() override;
85 
86   ExpressionTypeSystemHelper *GetTypeSystemHelper() override {
87     return &m_type_system_helper;
88   }
89 
90   ClangExpressionDeclMap *DeclMap() { return m_type_system_helper.DeclMap(); }
91 
92   void ResetDeclMap() { m_type_system_helper.ResetDeclMap(); }
93 
94   void ResetDeclMap(ExecutionContext &exe_ctx, bool keep_result_in_memory) {
95     m_type_system_helper.ResetDeclMap(exe_ctx, keep_result_in_memory);
96   }
97 
98   bool Install(DiagnosticManager &diagnostic_manager,
99                ExecutionContext &exe_ctx) override;
100 
101 private:
102   ClangUtilityFunctionHelper m_type_system_helper; ///< The map to use when
103                                                    ///parsing and materializing
104                                                    ///the expression.
105 };
106 
107 } // namespace lldb_private
108 
109 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGUTILITYFUNCTION_H
110