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