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