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/ArchSpec.h" 21 #include "lldb/Core/ClangForward.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 class ASTStructExtractor; 31 class ClangExpressionParser; 32 33 //---------------------------------------------------------------------- 34 /// @class ClangFunctionCaller ClangFunctionCaller.h 35 /// "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 friend class ASTStructExtractor; 69 70 class ClangFunctionCallerHelper : public ClangExpressionHelper { 71 public: 72 ClangFunctionCallerHelper(ClangFunctionCaller &owner) : m_owner(owner) {} 73 74 ~ClangFunctionCallerHelper() override = default; 75 76 //------------------------------------------------------------------ 77 /// Return the object that the parser should use when resolving external 78 /// values. May be NULL if everything should be self-contained. 79 //------------------------------------------------------------------ 80 ClangExpressionDeclMap *DeclMap() override { return NULL; } 81 82 //------------------------------------------------------------------ 83 /// Return the object that the parser should allow to access ASTs. 84 /// May be NULL if the ASTs do not need to be transformed. 85 /// 86 /// @param[in] passthrough 87 /// The ASTConsumer that the returned transformer should send 88 /// the ASTs to after transformation. 89 //------------------------------------------------------------------ 90 clang::ASTConsumer * 91 ASTTransformer(clang::ASTConsumer *passthrough) override; 92 93 private: 94 ClangFunctionCaller &m_owner; 95 std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that 96 ///generates the 97 ///argument struct 98 ///layout. 99 }; 100 101 public: 102 //------------------------------------------------------------------ 103 /// Constructor 104 /// 105 /// @param[in] exe_scope 106 /// An execution context scope that gets us at least a target and 107 /// process. 108 /// 109 /// @param[in] ast_context 110 /// The AST context to evaluate argument types in. 111 /// 112 /// @param[in] return_qualtype 113 /// An opaque Clang QualType for the function result. Should be 114 /// defined in ast_context. 115 /// 116 /// @param[in] function_address 117 /// The address of the function to call. 118 /// 119 /// @param[in] arg_value_list 120 /// The default values to use when calling this function. Can 121 /// be overridden using WriteFunctionArguments(). 122 //------------------------------------------------------------------ 123 ClangFunctionCaller(ExecutionContextScope &exe_scope, 124 const CompilerType &return_type, 125 const Address &function_address, 126 const ValueList &arg_value_list, const char *name); 127 128 ~ClangFunctionCaller() override; 129 130 //------------------------------------------------------------------ 131 /// Compile the wrapper function 132 /// 133 /// @param[in] thread_to_use_sp 134 /// Compilation might end up calling functions. Pass in the thread you 135 /// want the compilation to use. If you pass in an empty ThreadSP it will 136 /// use the currently selected thread. 137 /// 138 /// @param[in] diagnostic_manager 139 /// The diagnostic manager to report parser errors to. 140 /// 141 /// @return 142 /// The number of errors. 143 //------------------------------------------------------------------ 144 unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp, 145 DiagnosticManager &diagnostic_manager) override; 146 147 ExpressionTypeSystemHelper *GetTypeSystemHelper() override { 148 return &m_type_system_helper; 149 } 150 151 protected: 152 const char *GetWrapperStructName() { return m_wrapper_struct_name.c_str(); } 153 154 private: 155 //------------------------------------------------------------------ 156 // For ClangFunctionCaller only 157 //------------------------------------------------------------------ 158 159 // Note: the parser needs to be destructed before the execution unit, so 160 // declare the execution unit first. 161 ClangFunctionCallerHelper m_type_system_helper; 162 }; 163 164 } // namespace lldb_private 165 166 #endif // liblldb_ClangFunctionCaller_h_ 167