1 //===-- UtilityFunction.h ----------------------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_UtilityFunction_h_
11 #define liblldb_UtilityFunction_h_
16 #include "lldb/Expression/Expression.h"
17 #include "lldb/lldb-forward.h"
18 #include "lldb/lldb-private.h"
20 namespace lldb_private {
22 /// \class UtilityFunction UtilityFunction.h
23 /// "lldb/Expression/UtilityFunction.h" Encapsulates a bit of source code that
24 /// provides a function that is callable
26 /// LLDB uses expressions for various purposes, notably to call functions
27 /// and as a backend for the expr command. UtilityFunction encapsulates a
28 /// self-contained function meant to be used from other code. Utility
29 /// functions can perform error-checking for ClangUserExpressions,
30 class UtilityFunction : public Expression {
32 /// LLVM-style RTTI support.
33 static bool classof(const Expression *E) {
34 return E->getKind() == eKindUtilityFunction;
40 /// The text of the function. Must be a full translation unit.
43 /// The name of the function, as used in the text.
44 UtilityFunction(ExecutionContextScope &exe_scope, const char *text,
45 const char *name, ExpressionKind kind);
47 ~UtilityFunction() override;
49 /// Install the utility function into a process
51 /// \param[in] diagnostic_manager
52 /// A diagnostic manager to print parse errors and warnings to.
54 /// \param[in] exe_ctx
55 /// The execution context to install the utility function to.
58 /// True on success (no errors); false otherwise.
59 virtual bool Install(DiagnosticManager &diagnostic_manager,
60 ExecutionContext &exe_ctx) = 0;
62 /// Check whether the given PC is inside the function
64 /// Especially useful if the function dereferences nullptr to indicate a
68 /// The program counter to check.
71 /// True if the program counter falls within the function's bounds;
72 /// false if not (or the function is not JIT compiled)
73 bool ContainsAddress(lldb::addr_t address) {
74 // nothing is both >= LLDB_INVALID_ADDRESS and < LLDB_INVALID_ADDRESS, so
75 // this always returns false if the function is not JIT compiled yet
76 return (address >= m_jit_start_addr && address < m_jit_end_addr);
79 /// Return the string that the parser should parse. Must be a full
81 const char *Text() override { return m_function_text.c_str(); }
83 /// Return the function name that should be used for executing the
84 /// expression. Text() should contain the definition of this function.
85 const char *FunctionName() override { return m_function_name.c_str(); }
87 /// Return the object that the parser should use when registering local
88 /// variables. May be nullptr if the Expression doesn't care.
89 ExpressionVariableList *LocalVariables() { return nullptr; }
91 /// Return true if validation code should be inserted into the expression.
92 bool NeedsValidation() override { return false; }
94 /// Return true if external variables in the expression should be resolved.
95 bool NeedsVariableResolution() override { return false; }
97 // This makes the function caller function. Pass in the ThreadSP if you have
98 // one available, compilation can end up calling code (e.g. to look up
99 // indirect functions) and we don't want this to wander onto another thread.
100 FunctionCaller *MakeFunctionCaller(const CompilerType &return_type,
101 const ValueList &arg_value_list,
102 lldb::ThreadSP compilation_thread,
105 // This one retrieves the function caller that is already made. If you
106 // haven't made it yet, this returns nullptr
107 FunctionCaller *GetFunctionCaller() { return m_caller_up.get(); }
110 std::shared_ptr<IRExecutionUnit> m_execution_unit_sp;
111 lldb::ModuleWP m_jit_module_wp;
112 std::string m_function_text; ///< The text of the function. Must be a
113 ///well-formed translation unit.
114 std::string m_function_name; ///< The name of the function.
115 std::unique_ptr<FunctionCaller> m_caller_up;
118 } // namespace lldb_private
120 #endif // liblldb_UtilityFunction_h_