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