1292932Sdim//===-- ClangFunctionCaller.h -----------------------------------*- C++ -*-===//
2292932Sdim//
3292932Sdim//                     The LLVM Compiler Infrastructure
4292932Sdim//
5292932Sdim// This file is distributed under the University of Illinois Open Source
6292932Sdim// License. See LICENSE.TXT for details.
7292932Sdim//
8292932Sdim//===----------------------------------------------------------------------===//
9292932Sdim
10292932Sdim#ifndef liblldb_ClangFunctionCaller_h_
11292932Sdim#define liblldb_ClangFunctionCaller_h_
12292932Sdim
13292932Sdim// C Includes
14292932Sdim// C++ Includes
15292932Sdim// Other libraries and framework includes
16292932Sdim// Project includes
17292932Sdim#include "ClangExpressionHelper.h"
18292932Sdim
19292932Sdim#include "lldb/Core/ClangForward.h"
20292932Sdim#include "lldb/Core/Address.h"
21292932Sdim#include "lldb/Core/ArchSpec.h"
22292932Sdim#include "lldb/Core/Value.h"
23292932Sdim#include "lldb/Core/ValueObjectList.h"
24292932Sdim#include "lldb/Expression/FunctionCaller.h"
25292932Sdim#include "lldb/Symbol/CompilerType.h"
26292932Sdim#include "lldb/Target/Process.h"
27292932Sdim
28292932Sdimnamespace lldb_private
29292932Sdim{
30292932Sdim
31292932Sdimclass ASTStructExtractor;
32292932Sdimclass ClangExpressionParser;
33292932Sdim
34292932Sdim//----------------------------------------------------------------------
35292932Sdim/// @class ClangFunctionCaller ClangFunctionCaller.h "lldb/Expression/ClangFunctionCaller.h"
36292932Sdim/// @brief Encapsulates a function that can be called.
37292932Sdim///
38292932Sdim/// A given ClangFunctionCaller object can handle a single function signature.
39292932Sdim/// Once constructed, it can set up any number of concurrent calls to
40292932Sdim/// functions with that signature.
41292932Sdim///
42292932Sdim/// It performs the call by synthesizing a structure that contains the pointer
43292932Sdim/// to the function and the arguments that should be passed to that function,
44292932Sdim/// and producing a special-purpose JIT-compiled function that accepts a void*
45292932Sdim/// pointing to this struct as its only argument and calls the function in the
46292932Sdim/// struct with the written arguments.  This method lets Clang handle the
47292932Sdim/// vagaries of function calling conventions.
48292932Sdim///
49292932Sdim/// The simplest use of the ClangFunctionCaller is to construct it with a
50292932Sdim/// function representative of the signature you want to use, then call
51292932Sdim/// ExecuteFunction(ExecutionContext &, Stream &, Value &).
52292932Sdim///
53292932Sdim/// If you need to reuse the arguments for several calls, you can call
54292932Sdim/// InsertFunction() followed by WriteFunctionArguments(), which will return
55292932Sdim/// the location of the args struct for the wrapper function in args_addr_ref.
56292932Sdim///
57292932Sdim/// If you need to call the function on the thread plan stack, you can also
58292932Sdim/// call InsertFunction() followed by GetThreadPlanToCallFunction().
59292932Sdim///
60292932Sdim/// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed
61292932Sdim/// a pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
62292932Sdim/// and its address returned in that variable.
63292932Sdim///
64292932Sdim/// Any of the methods that take arg_addr_ptr can be passed NULL, and the
65292932Sdim/// argument space will be managed for you.
66292932Sdim//----------------------------------------------------------------------
67292932Sdimclass ClangFunctionCaller : public FunctionCaller
68292932Sdim{
69292932Sdim    friend class ASTStructExtractor;
70292932Sdim
71292932Sdim    class ClangFunctionCallerHelper : public ClangExpressionHelper
72292932Sdim    {
73292932Sdim    public:
74292932Sdim        ClangFunctionCallerHelper (ClangFunctionCaller &owner) :
75292932Sdim            m_owner(owner)
76292932Sdim        {
77292932Sdim        }
78292932Sdim
79292932Sdim        ~ClangFunctionCallerHelper() override = default;
80292932Sdim
81292932Sdim        //------------------------------------------------------------------
82292932Sdim        /// Return the object that the parser should use when resolving external
83292932Sdim        /// values.  May be NULL if everything should be self-contained.
84292932Sdim        //------------------------------------------------------------------
85292932Sdim        ClangExpressionDeclMap *
86292932Sdim        DeclMap() override
87292932Sdim        {
88292932Sdim            return NULL;
89292932Sdim        }
90292932Sdim
91292932Sdim        //------------------------------------------------------------------
92292932Sdim        /// Return the object that the parser should allow to access ASTs.
93292932Sdim        /// May be NULL if the ASTs do not need to be transformed.
94292932Sdim        ///
95292932Sdim        /// @param[in] passthrough
96292932Sdim        ///     The ASTConsumer that the returned transformer should send
97292932Sdim        ///     the ASTs to after transformation.
98292932Sdim        //------------------------------------------------------------------
99292932Sdim        clang::ASTConsumer *
100292932Sdim        ASTTransformer(clang::ASTConsumer *passthrough) override;
101292932Sdim
102292932Sdim    private:
103292932Sdim        ClangFunctionCaller                      &m_owner;
104292932Sdim        std::unique_ptr<ASTStructExtractor> m_struct_extractor;         ///< The class that generates the argument struct layout.
105292932Sdim    };
106292932Sdim
107292932Sdimpublic:
108292932Sdim    //------------------------------------------------------------------
109292932Sdim    /// Constructor
110292932Sdim    ///
111292932Sdim    /// @param[in] exe_scope
112292932Sdim    ///     An execution context scope that gets us at least a target and
113292932Sdim    ///     process.
114292932Sdim    ///
115292932Sdim    /// @param[in] ast_context
116292932Sdim    ///     The AST context to evaluate argument types in.
117292932Sdim    ///
118292932Sdim    /// @param[in] return_qualtype
119292932Sdim    ///     An opaque Clang QualType for the function result.  Should be
120292932Sdim    ///     defined in ast_context.
121292932Sdim    ///
122292932Sdim    /// @param[in] function_address
123292932Sdim    ///     The address of the function to call.
124292932Sdim    ///
125292932Sdim    /// @param[in] arg_value_list
126292932Sdim    ///     The default values to use when calling this function.  Can
127292932Sdim    ///     be overridden using WriteFunctionArguments().
128292932Sdim    //------------------------------------------------------------------
129292932Sdim    ClangFunctionCaller (ExecutionContextScope &exe_scope,
130292932Sdim                         const CompilerType &return_type,
131292932Sdim                         const Address& function_address,
132292932Sdim                         const ValueList &arg_value_list,
133292932Sdim                         const char *name);
134292932Sdim
135292932Sdim    ~ClangFunctionCaller() override;
136292932Sdim
137292932Sdim    //------------------------------------------------------------------
138292932Sdim    /// Compile the wrapper function
139292932Sdim    ///
140292932Sdim    /// @param[in] errors
141292932Sdim    ///     The stream to print parser errors to.
142292932Sdim    ///
143292932Sdim    /// @return
144292932Sdim    ///     The number of errors.
145292932Sdim    //------------------------------------------------------------------
146292932Sdim    unsigned
147292932Sdim    CompileFunction (Stream &errors) override;
148292932Sdim
149292932Sdim    ExpressionTypeSystemHelper *
150292932Sdim    GetTypeSystemHelper () override
151292932Sdim    {
152292932Sdim        return &m_type_system_helper;
153292932Sdim    }
154292932Sdim
155292932Sdimprotected:
156292932Sdim    const char *GetWrapperStructName()
157292932Sdim    {
158292932Sdim        return m_wrapper_struct_name.c_str();
159292932Sdim    }
160292932Sdim
161292932Sdimprivate:
162292932Sdim    //------------------------------------------------------------------
163292932Sdim    // For ClangFunctionCaller only
164292932Sdim    //------------------------------------------------------------------
165292932Sdim
166292932Sdim    // Note: the parser needs to be destructed before the execution unit, so
167292932Sdim    // declare the execution unit first.
168292932Sdim    ClangFunctionCallerHelper   m_type_system_helper;
169292932Sdim};
170292932Sdim
171292932Sdim} // namespace lldb_private
172292932Sdim
173292932Sdim#endif // liblldb_ClangFunctionCaller_h_
174