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_
13 #include "ClangExpressionHelper.h"
15 #include "lldb/Core/Address.h"
16 #include "lldb/Core/ClangForward.h"
17 #include "lldb/Core/Value.h"
18 #include "lldb/Core/ValueObjectList.h"
19 #include "lldb/Expression/FunctionCaller.h"
20 #include "lldb/Symbol/CompilerType.h"
21 #include "lldb/Target/Process.h"
23 namespace lldb_private {
25 class ASTStructExtractor;
26 class ClangExpressionParser;
28 //----------------------------------------------------------------------
29 /// @class ClangFunctionCaller ClangFunctionCaller.h
30 /// "lldb/Expression/ClangFunctionCaller.h" Encapsulates a function that can
33 /// A given ClangFunctionCaller object can handle a single function signature.
34 /// Once constructed, it can set up any number of concurrent calls to
35 /// functions with that signature.
37 /// It performs the call by synthesizing a structure that contains the pointer
38 /// to the function and the arguments that should be passed to that function,
39 /// and producing a special-purpose JIT-compiled function that accepts a void*
40 /// pointing to this struct as its only argument and calls the function in the
41 /// struct with the written arguments. This method lets Clang handle the
42 /// vagaries of function calling conventions.
44 /// The simplest use of the ClangFunctionCaller is to construct it with a
45 /// function representative of the signature you want to use, then call
46 /// ExecuteFunction(ExecutionContext &, Stream &, Value &).
48 /// If you need to reuse the arguments for several calls, you can call
49 /// InsertFunction() followed by WriteFunctionArguments(), which will return
50 /// the location of the args struct for the wrapper function in args_addr_ref.
52 /// If you need to call the function on the thread plan stack, you can also
53 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
55 /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed a
56 /// pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
57 /// and its address returned in that variable.
59 /// Any of the methods that take arg_addr_ptr can be passed NULL, and the
60 /// argument space will be managed for you.
61 //----------------------------------------------------------------------
62 class ClangFunctionCaller : public FunctionCaller {
63 friend class ASTStructExtractor;
65 class ClangFunctionCallerHelper : public ClangExpressionHelper {
67 ClangFunctionCallerHelper(ClangFunctionCaller &owner) : m_owner(owner) {}
69 ~ClangFunctionCallerHelper() override = default;
71 //------------------------------------------------------------------
72 /// Return the object that the parser should use when resolving external
73 /// values. May be NULL if everything should be self-contained.
74 //------------------------------------------------------------------
75 ClangExpressionDeclMap *DeclMap() override { return NULL; }
77 //------------------------------------------------------------------
78 /// Return the object that the parser should allow to access ASTs. May be
79 /// NULL if the ASTs do not need to be transformed.
81 /// @param[in] passthrough
82 /// The ASTConsumer that the returned transformer should send
83 /// the ASTs to after transformation.
84 //------------------------------------------------------------------
86 ASTTransformer(clang::ASTConsumer *passthrough) override;
89 ClangFunctionCaller &m_owner;
90 std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that
97 //------------------------------------------------------------------
100 /// @param[in] exe_scope
101 /// An execution context scope that gets us at least a target and
104 /// @param[in] ast_context
105 /// The AST context to evaluate argument types in.
107 /// @param[in] return_qualtype
108 /// An opaque Clang QualType for the function result. Should be
109 /// defined in ast_context.
111 /// @param[in] function_address
112 /// The address of the function to call.
114 /// @param[in] arg_value_list
115 /// The default values to use when calling this function. Can
116 /// be overridden using WriteFunctionArguments().
117 //------------------------------------------------------------------
118 ClangFunctionCaller(ExecutionContextScope &exe_scope,
119 const CompilerType &return_type,
120 const Address &function_address,
121 const ValueList &arg_value_list, const char *name);
123 ~ClangFunctionCaller() override;
125 //------------------------------------------------------------------
126 /// Compile the wrapper function
128 /// @param[in] thread_to_use_sp
129 /// Compilation might end up calling functions. Pass in the thread you
130 /// want the compilation to use. If you pass in an empty ThreadSP it will
131 /// use the currently selected thread.
133 /// @param[in] diagnostic_manager
134 /// The diagnostic manager to report parser errors to.
137 /// The number of errors.
138 //------------------------------------------------------------------
139 unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp,
140 DiagnosticManager &diagnostic_manager) override;
142 ExpressionTypeSystemHelper *GetTypeSystemHelper() override {
143 return &m_type_system_helper;
147 const char *GetWrapperStructName() { return m_wrapper_struct_name.c_str(); }
150 //------------------------------------------------------------------
151 // For ClangFunctionCaller only
152 //------------------------------------------------------------------
154 // Note: the parser needs to be destructed before the execution unit, so
155 // declare the execution unit first.
156 ClangFunctionCallerHelper m_type_system_helper;
159 } // namespace lldb_private
161 #endif // liblldb_ClangFunctionCaller_h_