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