Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / Plugins / ExpressionParser / Clang / ClangFunctionCaller.h
blob2a5c863b51a2f1f56eca7e0f0f63db7c18880ebf
1 //===-- ClangFunctionCaller.h -----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H
12 #include "ClangExpressionHelper.h"
14 #include "lldb/Core/Address.h"
15 #include "lldb/Core/Value.h"
16 #include "lldb/Core/ValueObjectList.h"
17 #include "lldb/Expression/FunctionCaller.h"
18 #include "lldb/Symbol/CompilerType.h"
19 #include "lldb/Target/Process.h"
21 namespace lldb_private {
23 class ASTStructExtractor;
25 /// \class ClangFunctionCaller ClangFunctionCaller.h
26 /// "lldb/Expression/ClangFunctionCaller.h" Encapsulates a function that can
27 /// be called.
28 ///
29 /// A given ClangFunctionCaller object can handle a single function signature.
30 /// Once constructed, it can set up any number of concurrent calls to
31 /// functions with that signature.
32 ///
33 /// It performs the call by synthesizing a structure that contains the pointer
34 /// to the function and the arguments that should be passed to that function,
35 /// and producing a special-purpose JIT-compiled function that accepts a void*
36 /// pointing to this struct as its only argument and calls the function in the
37 /// struct with the written arguments. This method lets Clang handle the
38 /// vagaries of function calling conventions.
39 ///
40 /// The simplest use of the ClangFunctionCaller is to construct it with a
41 /// function representative of the signature you want to use, then call
42 /// ExecuteFunction(ExecutionContext &, Stream &, Value &).
43 ///
44 /// If you need to reuse the arguments for several calls, you can call
45 /// InsertFunction() followed by WriteFunctionArguments(), which will return
46 /// the location of the args struct for the wrapper function in args_addr_ref.
47 ///
48 /// If you need to call the function on the thread plan stack, you can also
49 /// call InsertFunction() followed by GetThreadPlanToCallFunction().
50 ///
51 /// Any of the methods that take arg_addr_ptr or arg_addr_ref can be passed a
52 /// pointer set to LLDB_INVALID_ADDRESS and new structure will be allocated
53 /// and its address returned in that variable.
54 ///
55 /// Any of the methods that take arg_addr_ptr can be passed NULL, and the
56 /// argument space will be managed for you.
57 class ClangFunctionCaller : public FunctionCaller {
58 friend class ASTStructExtractor;
60 class ClangFunctionCallerHelper
61 : public llvm::RTTIExtends<ClangFunctionCallerHelper,
62 ClangExpressionHelper> {
63 public:
64 // LLVM RTTI support
65 static char ID;
67 ClangFunctionCallerHelper(ClangFunctionCaller &owner) : m_owner(owner) {}
69 /// Return the object that the parser should use when resolving external
70 /// values. May be NULL if everything should be self-contained.
71 ClangExpressionDeclMap *DeclMap() override { return nullptr; }
73 /// Return the object that the parser should allow to access ASTs. May be
74 /// NULL if the ASTs do not need to be transformed.
75 ///
76 /// \param[in] passthrough
77 /// The ASTConsumer that the returned transformer should send
78 /// the ASTs to after transformation.
79 clang::ASTConsumer *
80 ASTTransformer(clang::ASTConsumer *passthrough) override;
82 private:
83 ClangFunctionCaller &m_owner;
84 std::unique_ptr<ASTStructExtractor> m_struct_extractor; ///< The class that
85 ///generates the
86 ///argument struct
87 ///layout.
90 // LLVM RTTI support
91 static char ID;
93 public:
94 bool isA(const void *ClassID) const override {
95 return ClassID == &ID || FunctionCaller::isA(ClassID);
97 static bool classof(const Expression *obj) { return obj->isA(&ID); }
99 /// Constructor
101 /// \param[in] exe_scope
102 /// An execution context scope that gets us at least a target and
103 /// process.
105 /// \param[in] return_type
106 /// A compiler type for the function result. Should be
107 /// defined in ast_context.
109 /// \param[in] function_address
110 /// The address of the function to call.
112 /// \param[in] arg_value_list
113 /// The default values to use when calling this function. Can
114 /// be overridden using WriteFunctionArguments().
115 ClangFunctionCaller(ExecutionContextScope &exe_scope,
116 const CompilerType &return_type,
117 const Address &function_address,
118 const ValueList &arg_value_list, const char *name);
120 ~ClangFunctionCaller() override;
122 /// Compile the wrapper function
124 /// \param[in] thread_to_use_sp
125 /// Compilation might end up calling functions. Pass in the thread you
126 /// want the compilation to use. If you pass in an empty ThreadSP it will
127 /// use the currently selected thread.
129 /// \param[in] diagnostic_manager
130 /// The diagnostic manager to report parser errors to.
132 /// \return
133 /// The number of errors.
134 unsigned CompileFunction(lldb::ThreadSP thread_to_use_sp,
135 DiagnosticManager &diagnostic_manager) override;
137 ExpressionTypeSystemHelper *GetTypeSystemHelper() override {
138 return &m_type_system_helper;
141 protected:
142 const char *GetWrapperStructName() { return m_wrapper_struct_name.c_str(); }
144 private:
145 // For ClangFunctionCaller only
147 // Note: the parser needs to be destructed before the execution unit, so
148 // declare the execution unit first.
149 ClangFunctionCallerHelper m_type_system_helper;
152 } // namespace lldb_private
154 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGFUNCTIONCALLER_H