1 //===-- ClangFunctionCaller.h -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ClangFunctionCaller_h_
11 #define liblldb_ClangFunctionCaller_h_
15 // Other libraries and framework includes
17 #include "ClangExpressionHelper.h"
19 #include "lldb/Core/Address.h"
20 #include "lldb/Core/ArchSpec.h"
21 #include "lldb/Core/ClangForward.h"
22 #include "lldb/Core/Value.h"
23 #include "lldb/Core/ValueObjectList.h"
24 #include "lldb/Expression/FunctionCaller.h"
25 #include "lldb/Symbol/CompilerType.h"
26 #include "lldb/Target/Process.h"
28 namespace lldb_private {
30 class ASTStructExtractor;
31 class ClangExpressionParser;
33 //----------------------------------------------------------------------
34 /// @class ClangFunctionCaller ClangFunctionCaller.h
35 /// "lldb/Expression/ClangFunctionCaller.h"
36 /// @brief Encapsulates a function that can be called.
38 /// A given ClangFunctionCaller object can handle a single function signature.
39 /// Once constructed, it can set up any number of concurrent calls to
40 /// functions with that signature.
42 /// It performs the call by synthesizing a structure that contains the pointer
43 /// to the function and the arguments that should be passed to that function,
44 /// and producing a special-purpose JIT-compiled function that accepts a void*
45 /// pointing to this struct as its only argument and calls the function in the
46 /// struct with the written arguments. This method lets Clang handle the
47 /// vagaries of function calling conventions.
49 /// The simplest use of the ClangFunctionCaller is to construct it with a
50 /// function representative of the signature you want to use, then call
51 /// ExecuteFunction(ExecutionContext &, Stream &, Value &).
53 /// If you need to reuse the arguments for several calls, you can call
54 /// InsertFunction() followed by WriteFunctionArguments(), which will return
55 /// the location of the args struct for the wrapper function in args_addr_ref.
57 /// If you need to call the function on the thread plan stack, you can also
58 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
60 /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed
61 /// a pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
62 /// and its address returned in that variable.
64 /// Any of the methods that take arg_addr_ptr can be passed NULL, and the
65 /// argument space will be managed for you.
66 //----------------------------------------------------------------------
67 class ClangFunctionCaller : public FunctionCaller {
68 friend class ASTStructExtractor;
70 class ClangFunctionCallerHelper : public ClangExpressionHelper {
72 ClangFunctionCallerHelper(ClangFunctionCaller &owner) : m_owner(owner) {}
74 ~ClangFunctionCallerHelper() override = default;
76 //------------------------------------------------------------------
77 /// Return the object that the parser should use when resolving external
78 /// values. May be NULL if everything should be self-contained.
79 //------------------------------------------------------------------
80 ClangExpressionDeclMap *DeclMap() override { return NULL; }
82 //------------------------------------------------------------------
83 /// Return the object that the parser should allow to access ASTs.
84 /// May be NULL if the ASTs do not need to be transformed.
86 /// @param[in] passthrough
87 /// The ASTConsumer that the returned transformer should send
88 /// the ASTs to after transformation.
89 //------------------------------------------------------------------
91 ASTTransformer(clang::ASTConsumer *passthrough) override;
94 ClangFunctionCaller &m_owner;
95 std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that
102 //------------------------------------------------------------------
105 /// @param[in] exe_scope
106 /// An execution context scope that gets us at least a target and
109 /// @param[in] ast_context
110 /// The AST context to evaluate argument types in.
112 /// @param[in] return_qualtype
113 /// An opaque Clang QualType for the function result. Should be
114 /// defined in ast_context.
116 /// @param[in] function_address
117 /// The address of the function to call.
119 /// @param[in] arg_value_list
120 /// The default values to use when calling this function. Can
121 /// be overridden using WriteFunctionArguments().
122 //------------------------------------------------------------------
123 ClangFunctionCaller(ExecutionContextScope &exe_scope,
124 const CompilerType &return_type,
125 const Address &function_address,
126 const ValueList &arg_value_list, const char *name);
128 ~ClangFunctionCaller() override;
130 //------------------------------------------------------------------
131 /// Compile the wrapper function
133 /// @param[in] thread_to_use_sp
134 /// Compilation might end up calling functions. Pass in the thread you
135 /// want the compilation to use. If you pass in an empty ThreadSP it will
136 /// use the currently selected thread.
138 /// @param[in] diagnostic_manager
139 /// The diagnostic manager to report parser errors to.
142 /// The number of errors.
143 //------------------------------------------------------------------
144 unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp,
145 DiagnosticManager &diagnostic_manager) override;
147 ExpressionTypeSystemHelper *GetTypeSystemHelper() override {
148 return &m_type_system_helper;
152 const char *GetWrapperStructName() { return m_wrapper_struct_name.c_str(); }
155 //------------------------------------------------------------------
156 // For ClangFunctionCaller only
157 //------------------------------------------------------------------
159 // Note: the parser needs to be destructed before the execution unit, so
160 // declare the execution unit first.
161 ClangFunctionCallerHelper m_type_system_helper;
164 } // namespace lldb_private
166 #endif // liblldb_ClangFunctionCaller_h_