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