155 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H10#define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H11 12#include "ClangExpressionHelper.h"13 14#include "lldb/Core/Address.h"15#include "lldb/Core/Value.h"16#include "lldb/Expression/FunctionCaller.h"17#include "lldb/Symbol/CompilerType.h"18#include "lldb/Target/Process.h"19#include "lldb/ValueObject/ValueObjectList.h"20 21namespace lldb_private {22 23class ASTStructExtractor;24 25/// \class ClangFunctionCaller ClangFunctionCaller.h26/// "lldb/Expression/ClangFunctionCaller.h" Encapsulates a function that can27/// 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 to31/// functions with that signature.32///33/// It performs the call by synthesizing a structure that contains the pointer34/// 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 the37/// struct with the written arguments. This method lets Clang handle the38/// vagaries of function calling conventions.39///40/// The simplest use of the ClangFunctionCaller is to construct it with a41/// function representative of the signature you want to use, then call42/// ExecuteFunction(ExecutionContext &, Stream &, Value &).43///44/// If you need to reuse the arguments for several calls, you can call45/// InsertFunction() followed by WriteFunctionArguments(), which will return46/// 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 also49/// call InsertFunction() followed by GetThreadPlanToCallFunction().50///51/// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed a52/// pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated53/// and its address returned in that variable.54///55/// Any of the methods that take arg_addr_ptr can be passed NULL, and the56/// argument space will be managed for you.57class ClangFunctionCaller : public FunctionCaller {58 friend class ASTStructExtractor;59 60 class ClangFunctionCallerHelper61 : public llvm::RTTIExtends<ClangFunctionCallerHelper,62 ClangExpressionHelper> {63 public:64 // LLVM RTTI support65 static char ID;66 67 ClangFunctionCallerHelper(ClangFunctionCaller &owner) : m_owner(owner) {}68 69 /// Return the object that the parser should use when resolving external70 /// values. May be NULL if everything should be self-contained.71 ClangExpressionDeclMap *DeclMap() override { return nullptr; }72 73 /// Return the object that the parser should allow to access ASTs. May be74 /// NULL if the ASTs do not need to be transformed.75 ///76 /// \param[in] passthrough77 /// The ASTConsumer that the returned transformer should send78 /// 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 that85 ///generates the86 ///argument struct87 ///layout.88 };89 90 // LLVM RTTI support91 static char ID;92 93public:94 bool isA(const void *ClassID) const override {95 return ClassID == &ID || FunctionCaller::isA(ClassID);96 }97 static bool classof(const Expression *obj) { return obj->isA(&ID); }98 99 /// Constructor100 ///101 /// \param[in] exe_scope102 /// An execution context scope that gets us at least a target and103 /// process.104 ///105 /// \param[in] return_type106 /// A compiler type for the function result. Should be107 /// defined in ast_context.108 ///109 /// \param[in] function_address110 /// The address of the function to call.111 ///112 /// \param[in] arg_value_list113 /// The default values to use when calling this function. Can114 /// 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 function123 ///124 /// \param[in] thread_to_use_sp125 /// Compilation might end up calling functions. Pass in the thread you126 /// want the compilation to use. If you pass in an empty ThreadSP it will127 /// use the currently selected thread.128 ///129 /// \param[in] diagnostic_manager130 /// The diagnostic manager to report parser errors to.131 ///132 /// \return133 /// The number of errors.134 unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp,135 DiagnosticManager &diagnostic_manager) override;136 137 ExpressionTypeSystemHelper *GetTypeSystemHelper() override {138 return &m_type_system_helper;139 }140 141protected:142 const char *GetWrapperStructName() { return m_wrapper_struct_name.c_str(); }143 144private:145 // For ClangFunctionCaller only146 147 // Note: the parser needs to be destructed before the execution unit, so148 // declare the execution unit first.149 ClangFunctionCallerHelper m_type_system_helper;150};151 152} // namespace lldb_private153 154#endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H155