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