1061da546Spatrick //===-- ClangFunctionCaller.h -----------------------------------*- C++ -*-===// 2061da546Spatrick // 3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information. 5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6061da546Spatrick // 7061da546Spatrick //===----------------------------------------------------------------------===// 8061da546Spatrick 9*dda28197Spatrick #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H 10*dda28197Spatrick #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H 11061da546Spatrick 12061da546Spatrick #include "ClangExpressionHelper.h" 13061da546Spatrick 14061da546Spatrick #include "lldb/Core/Address.h" 15061da546Spatrick #include "lldb/Core/Value.h" 16061da546Spatrick #include "lldb/Core/ValueObjectList.h" 17061da546Spatrick #include "lldb/Expression/FunctionCaller.h" 18061da546Spatrick #include "lldb/Symbol/CompilerType.h" 19061da546Spatrick #include "lldb/Target/Process.h" 20061da546Spatrick 21061da546Spatrick namespace lldb_private { 22061da546Spatrick 23061da546Spatrick class ASTStructExtractor; 24061da546Spatrick 25061da546Spatrick /// \class ClangFunctionCaller ClangFunctionCaller.h 26061da546Spatrick /// "lldb/Expression/ClangFunctionCaller.h" Encapsulates a function that can 27061da546Spatrick /// be called. 28061da546Spatrick /// 29061da546Spatrick /// A given ClangFunctionCaller object can handle a single function signature. 30061da546Spatrick /// Once constructed, it can set up any number of concurrent calls to 31061da546Spatrick /// functions with that signature. 32061da546Spatrick /// 33061da546Spatrick /// It performs the call by synthesizing a structure that contains the pointer 34061da546Spatrick /// to the function and the arguments that should be passed to that function, 35061da546Spatrick /// and producing a special-purpose JIT-compiled function that accepts a void* 36061da546Spatrick /// pointing to this struct as its only argument and calls the function in the 37061da546Spatrick /// struct with the written arguments. This method lets Clang handle the 38061da546Spatrick /// vagaries of function calling conventions. 39061da546Spatrick /// 40061da546Spatrick /// The simplest use of the ClangFunctionCaller is to construct it with a 41061da546Spatrick /// function representative of the signature you want to use, then call 42061da546Spatrick /// ExecuteFunction(ExecutionContext &, Stream &, Value &). 43061da546Spatrick /// 44061da546Spatrick /// If you need to reuse the arguments for several calls, you can call 45061da546Spatrick /// InsertFunction() followed by WriteFunctionArguments(), which will return 46061da546Spatrick /// the location of the args struct for the wrapper function in args_addr_ref. 47061da546Spatrick /// 48061da546Spatrick /// If you need to call the function on the thread plan stack, you can also 49061da546Spatrick /// call InsertFunction() followed by GetThreadPlanToCallFunction(). 50061da546Spatrick /// 51061da546Spatrick /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed a 52061da546Spatrick /// pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated 53061da546Spatrick /// and its address returned in that variable. 54061da546Spatrick /// 55061da546Spatrick /// Any of the methods that take arg_addr_ptr can be passed NULL, and the 56061da546Spatrick /// argument space will be managed for you. 57061da546Spatrick class ClangFunctionCaller : public FunctionCaller { 58061da546Spatrick friend class ASTStructExtractor; 59061da546Spatrick 60061da546Spatrick class ClangFunctionCallerHelper : public ClangExpressionHelper { 61061da546Spatrick public: ClangFunctionCallerHelper(ClangFunctionCaller & owner)62061da546Spatrick ClangFunctionCallerHelper(ClangFunctionCaller &owner) : m_owner(owner) {} 63061da546Spatrick 64061da546Spatrick ~ClangFunctionCallerHelper() override = default; 65061da546Spatrick 66061da546Spatrick /// Return the object that the parser should use when resolving external 67061da546Spatrick /// values. May be NULL if everything should be self-contained. DeclMap()68061da546Spatrick ClangExpressionDeclMap *DeclMap() override { return nullptr; } 69061da546Spatrick 70061da546Spatrick /// Return the object that the parser should allow to access ASTs. May be 71061da546Spatrick /// NULL if the ASTs do not need to be transformed. 72061da546Spatrick /// 73061da546Spatrick /// \param[in] passthrough 74061da546Spatrick /// The ASTConsumer that the returned transformer should send 75061da546Spatrick /// the ASTs to after transformation. 76061da546Spatrick clang::ASTConsumer * 77061da546Spatrick ASTTransformer(clang::ASTConsumer *passthrough) override; 78061da546Spatrick 79061da546Spatrick private: 80061da546Spatrick ClangFunctionCaller &m_owner; 81061da546Spatrick std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that 82061da546Spatrick ///generates the 83061da546Spatrick ///argument struct 84061da546Spatrick ///layout. 85061da546Spatrick }; 86061da546Spatrick 87061da546Spatrick // LLVM RTTI support 88061da546Spatrick static char ID; 89061da546Spatrick 90061da546Spatrick public: isA(const void * ClassID)91061da546Spatrick bool isA(const void *ClassID) const override { 92061da546Spatrick return ClassID == &ID || FunctionCaller::isA(ClassID); 93061da546Spatrick } classof(const Expression * obj)94061da546Spatrick static bool classof(const Expression *obj) { return obj->isA(&ID); } 95061da546Spatrick 96061da546Spatrick /// Constructor 97061da546Spatrick /// 98061da546Spatrick /// \param[in] exe_scope 99061da546Spatrick /// An execution context scope that gets us at least a target and 100061da546Spatrick /// process. 101061da546Spatrick /// 102061da546Spatrick /// \param[in] return_type 103061da546Spatrick /// A compiler type for the function result. Should be 104061da546Spatrick /// defined in ast_context. 105061da546Spatrick /// 106061da546Spatrick /// \param[in] function_address 107061da546Spatrick /// The address of the function to call. 108061da546Spatrick /// 109061da546Spatrick /// \param[in] arg_value_list 110061da546Spatrick /// The default values to use when calling this function. Can 111061da546Spatrick /// be overridden using WriteFunctionArguments(). 112061da546Spatrick ClangFunctionCaller(ExecutionContextScope &exe_scope, 113061da546Spatrick const CompilerType &return_type, 114061da546Spatrick const Address &function_address, 115061da546Spatrick const ValueList &arg_value_list, const char *name); 116061da546Spatrick 117061da546Spatrick ~ClangFunctionCaller() override; 118061da546Spatrick 119061da546Spatrick /// Compile the wrapper function 120061da546Spatrick /// 121061da546Spatrick /// \param[in] thread_to_use_sp 122061da546Spatrick /// Compilation might end up calling functions. Pass in the thread you 123061da546Spatrick /// want the compilation to use. If you pass in an empty ThreadSP it will 124061da546Spatrick /// use the currently selected thread. 125061da546Spatrick /// 126061da546Spatrick /// \param[in] diagnostic_manager 127061da546Spatrick /// The diagnostic manager to report parser errors to. 128061da546Spatrick /// 129061da546Spatrick /// \return 130061da546Spatrick /// The number of errors. 131061da546Spatrick unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp, 132061da546Spatrick DiagnosticManager &diagnostic_manager) override; 133061da546Spatrick GetTypeSystemHelper()134061da546Spatrick ExpressionTypeSystemHelper *GetTypeSystemHelper() override { 135061da546Spatrick return &m_type_system_helper; 136061da546Spatrick } 137061da546Spatrick 138061da546Spatrick protected: GetWrapperStructName()139061da546Spatrick const char *GetWrapperStructName() { return m_wrapper_struct_name.c_str(); } 140061da546Spatrick 141061da546Spatrick private: 142061da546Spatrick // For ClangFunctionCaller only 143061da546Spatrick 144061da546Spatrick // Note: the parser needs to be destructed before the execution unit, so 145061da546Spatrick // declare the execution unit first. 146061da546Spatrick ClangFunctionCallerHelper m_type_system_helper; 147061da546Spatrick }; 148061da546Spatrick 149061da546Spatrick } // namespace lldb_private 150061da546Spatrick 151*dda28197Spatrick #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H 152