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