xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangFunctionCaller.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 //===-- ClangFunctionCaller.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_CLANGFUNCTIONCALLER_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H
11 
12 #include "ClangExpressionHelper.h"
13 
14 #include "lldb/Core/Address.h"
15 #include "lldb/Core/Value.h"
16 #include "lldb/Core/ValueObjectList.h"
17 #include "lldb/Expression/FunctionCaller.h"
18 #include "lldb/Symbol/CompilerType.h"
19 #include "lldb/Target/Process.h"
20 
21 namespace lldb_private {
22 
23 class ASTStructExtractor;
24 
25 /// \class ClangFunctionCaller ClangFunctionCaller.h
26 /// "lldb/Expression/ClangFunctionCaller.h" Encapsulates a function that can
27 /// be called.
28 ///
29 /// A given ClangFunctionCaller object can handle a single function signature.
30 /// Once constructed, it can set up any number of concurrent calls to
31 /// functions with that signature.
32 ///
33 /// It performs the call by synthesizing a structure that contains the pointer
34 /// to the function and the arguments that should be passed to that function,
35 /// and producing a special-purpose JIT-compiled function that accepts a void*
36 /// pointing to this struct as its only argument and calls the function in the
37 /// struct with the written arguments.  This method lets Clang handle the
38 /// vagaries of function calling conventions.
39 ///
40 /// The simplest use of the ClangFunctionCaller is to construct it with a
41 /// function representative of the signature you want to use, then call
42 /// ExecuteFunction(ExecutionContext &, Stream &, Value &).
43 ///
44 /// If you need to reuse the arguments for several calls, you can call
45 /// InsertFunction() followed by WriteFunctionArguments(), which will return
46 /// the location of the args struct for the wrapper function in args_addr_ref.
47 ///
48 /// If you need to call the function on the thread plan stack, you can also
49 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
50 ///
51 /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed a
52 /// pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
53 /// and its address returned in that variable.
54 ///
55 /// Any of the methods that take arg_addr_ptr can be passed NULL, and the
56 /// argument space will be managed for you.
57 class ClangFunctionCaller : public FunctionCaller {
58   friend class ASTStructExtractor;
59 
60   class ClangFunctionCallerHelper
61       : public llvm::RTTIExtends<ClangFunctionCallerHelper,
62                                  ClangExpressionHelper> {
63   public:
64     // LLVM RTTI support
65     static char ID;
66 
ClangFunctionCallerHelper(ClangFunctionCaller & owner)67     ClangFunctionCallerHelper(ClangFunctionCaller &owner) : m_owner(owner) {}
68 
69     /// Return the object that the parser should use when resolving external
70     /// values.  May be NULL if everything should be self-contained.
DeclMap()71     ClangExpressionDeclMap *DeclMap() override { return nullptr; }
72 
73     /// Return the object that the parser should allow to access ASTs. May be
74     /// NULL if the ASTs do not need to be transformed.
75     ///
76     /// \param[in] passthrough
77     ///     The ASTConsumer that the returned transformer should send
78     ///     the ASTs to after transformation.
79     clang::ASTConsumer *
80     ASTTransformer(clang::ASTConsumer *passthrough) override;
81 
82   private:
83     ClangFunctionCaller &m_owner;
84     std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that
85                                                             ///generates the
86                                                             ///argument struct
87                                                             ///layout.
88   };
89 
90   // LLVM RTTI support
91   static char ID;
92 
93 public:
isA(const void * ClassID)94   bool isA(const void *ClassID) const override {
95     return ClassID == &ID || FunctionCaller::isA(ClassID);
96   }
classof(const Expression * obj)97   static bool classof(const Expression *obj) { return obj->isA(&ID); }
98 
99   /// Constructor
100   ///
101   /// \param[in] exe_scope
102   ///     An execution context scope that gets us at least a target and
103   ///     process.
104   ///
105   /// \param[in] return_type
106   ///     A compiler type for the function result.  Should be
107   ///     defined in ast_context.
108   ///
109   /// \param[in] function_address
110   ///     The address of the function to call.
111   ///
112   /// \param[in] arg_value_list
113   ///     The default values to use when calling this function.  Can
114   ///     be overridden using WriteFunctionArguments().
115   ClangFunctionCaller(ExecutionContextScope &exe_scope,
116                       const CompilerType &return_type,
117                       const Address &function_address,
118                       const ValueList &arg_value_list, const char *name);
119 
120   ~ClangFunctionCaller() override;
121 
122   /// Compile the wrapper function
123   ///
124   /// \param[in] thread_to_use_sp
125   ///     Compilation might end up calling functions.  Pass in the thread you
126   ///     want the compilation to use.  If you pass in an empty ThreadSP it will
127   ///     use the currently selected thread.
128   ///
129   /// \param[in] diagnostic_manager
130   ///     The diagnostic manager to report parser errors to.
131   ///
132   /// \return
133   ///     The number of errors.
134   unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp,
135                            DiagnosticManager &diagnostic_manager) override;
136 
GetTypeSystemHelper()137   ExpressionTypeSystemHelper *GetTypeSystemHelper() override {
138     return &m_type_system_helper;
139   }
140 
141 protected:
GetWrapperStructName()142   const char *GetWrapperStructName() { return m_wrapper_struct_name.c_str(); }
143 
144 private:
145   // For ClangFunctionCaller only
146 
147   // Note: the parser needs to be destructed before the execution unit, so
148   // declare the execution unit first.
149   ClangFunctionCallerHelper m_type_system_helper;
150 };
151 
152 } // namespace lldb_private
153 
154 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H
155