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   // LLVM RTTI support
37   static char ID;
38 
39 public:
40   bool isA(const void *ClassID) const override {
41     return ClassID == &ID || UtilityFunction::isA(ClassID);
42   }
43   static bool classof(const Expression *obj) { return obj->isA(&ID); }
44 
45   class ClangUtilityFunctionHelper : public ClangExpressionHelper {
46   public:
47     ClangUtilityFunctionHelper() {}
48 
49     ~ClangUtilityFunctionHelper() override {}
50 
51     /// Return the object that the parser should use when resolving external
52     /// values.  May be NULL if everything should be self-contained.
53     ClangExpressionDeclMap *DeclMap() override {
54       return m_expr_decl_map_up.get();
55     }
56 
57     void ResetDeclMap() { m_expr_decl_map_up.reset(); }
58 
59     void ResetDeclMap(ExecutionContext &exe_ctx, bool keep_result_in_memory);
60 
61     /// Return the object that the parser should allow to access ASTs. May be
62     /// NULL if the ASTs do not need to be transformed.
63     ///
64     /// \param[in] passthrough
65     ///     The ASTConsumer that the returned transformer should send
66     ///     the ASTs to after transformation.
67     clang::ASTConsumer *
68     ASTTransformer(clang::ASTConsumer *passthrough) override {
69       return nullptr;
70     }
71 
72   private:
73     std::unique_ptr<ClangExpressionDeclMap> m_expr_decl_map_up;
74   };
75   /// Constructor
76   ///
77   /// \param[in] text
78   ///     The text of the function.  Must be a full translation unit.
79   ///
80   /// \param[in] name
81   ///     The name of the function, as used in the text.
82   ClangUtilityFunction(ExecutionContextScope &exe_scope, const char *text,
83                        const char *name);
84 
85   ~ClangUtilityFunction() override;
86 
87   ExpressionTypeSystemHelper *GetTypeSystemHelper() override {
88     return &m_type_system_helper;
89   }
90 
91   ClangExpressionDeclMap *DeclMap() { return m_type_system_helper.DeclMap(); }
92 
93   void ResetDeclMap() { m_type_system_helper.ResetDeclMap(); }
94 
95   void ResetDeclMap(ExecutionContext &exe_ctx, bool keep_result_in_memory) {
96     m_type_system_helper.ResetDeclMap(exe_ctx, keep_result_in_memory);
97   }
98 
99   bool Install(DiagnosticManager &diagnostic_manager,
100                ExecutionContext &exe_ctx) override;
101 
102 private:
103   ClangUtilityFunctionHelper m_type_system_helper; ///< The map to use when
104                                                    ///parsing and materializing
105                                                    ///the expression.
106 };
107 
108 } // namespace lldb_private
109 
110 #endif // liblldb_ClangUtilityFunction_h_
111