1 //===-- ClangUtilityFunction.h ----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_ClangUtilityFunction_h_
11 #define liblldb_ClangUtilityFunction_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <map>
16 #include <string>
17 #include <vector>
18 
19 // Other libraries and framework includes
20 // Project includes
21 #include "ClangExpressionHelper.h"
22 
23 #include "lldb/Core/ClangForward.h"
24 #include "lldb/Expression/UtilityFunction.h"
25 #include "lldb/lldb-forward.h"
26 #include "lldb/lldb-private.h"
27 
28 namespace lldb_private {
29 
30 //----------------------------------------------------------------------
31 /// @class ClangUtilityFunction ClangUtilityFunction.h
32 /// "lldb/Expression/ClangUtilityFunction.h"
33 /// @brief Encapsulates a single expression for use with Clang
34 ///
35 /// LLDB uses expressions for various purposes, notably to call functions
36 /// and as a backend for the expr command.  ClangUtilityFunction encapsulates
37 /// a self-contained function meant to be used from other code.  Utility
38 /// functions can perform error-checking for ClangUserExpressions, or can
39 /// simply provide a way to push a function into the target for the debugger to
40 /// call later on.
41 //----------------------------------------------------------------------
42 class ClangUtilityFunction : public UtilityFunction {
43 public:
44   class ClangUtilityFunctionHelper : public ClangExpressionHelper {
45   public:
46     ClangUtilityFunctionHelper() {}
47 
48     ~ClangUtilityFunctionHelper() override {}
49 
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     //------------------------------------------------------------------
54     ClangExpressionDeclMap *DeclMap() override {
55       return m_expr_decl_map_up.get();
56     }
57 
58     void ResetDeclMap() { m_expr_decl_map_up.reset(); }
59 
60     void ResetDeclMap(ExecutionContext &exe_ctx, bool keep_result_in_memory);
61 
62     //------------------------------------------------------------------
63     /// Return the object that the parser should allow to access ASTs.
64     /// May be NULL if the ASTs do not need to be transformed.
65     ///
66     /// @param[in] passthrough
67     ///     The ASTConsumer that the returned transformer should send
68     ///     the ASTs to after transformation.
69     //------------------------------------------------------------------
70     clang::ASTConsumer *
71     ASTTransformer(clang::ASTConsumer *passthrough) override {
72       return nullptr;
73     }
74 
75   private:
76     std::unique_ptr<ClangExpressionDeclMap> m_expr_decl_map_up;
77   };
78   //------------------------------------------------------------------
79   /// Constructor
80   ///
81   /// @param[in] text
82   ///     The text of the function.  Must be a full translation unit.
83   ///
84   /// @param[in] name
85   ///     The name of the function, as used in the text.
86   //------------------------------------------------------------------
87   ClangUtilityFunction(ExecutionContextScope &exe_scope, const char *text,
88                        const char *name);
89 
90   ~ClangUtilityFunction() override;
91 
92   ExpressionTypeSystemHelper *GetTypeSystemHelper() override {
93     return &m_type_system_helper;
94   }
95 
96   ClangExpressionDeclMap *DeclMap() { return m_type_system_helper.DeclMap(); }
97 
98   void ResetDeclMap() { m_type_system_helper.ResetDeclMap(); }
99 
100   void ResetDeclMap(ExecutionContext &exe_ctx, bool keep_result_in_memory) {
101     m_type_system_helper.ResetDeclMap(exe_ctx, keep_result_in_memory);
102   }
103 
104   bool Install(DiagnosticManager &diagnostic_manager,
105                ExecutionContext &exe_ctx) override;
106 
107 private:
108   ClangUtilityFunctionHelper m_type_system_helper; ///< The map to use when
109                                                    ///parsing and materializing
110                                                    ///the expression.
111 };
112 
113 } // namespace lldb_private
114 
115 #endif // liblldb_ClangUtilityFunction_h_
116