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