xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.h (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1061da546Spatrick //===-- ClangExpressionSourceCode.h -----------------------------*- C++ -*-===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9dda28197Spatrick #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONSOURCECODE_H
10dda28197Spatrick #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONSOURCECODE_H
11061da546Spatrick 
12061da546Spatrick #include "lldb/Expression/Expression.h"
13061da546Spatrick #include "lldb/Expression/ExpressionSourceCode.h"
14061da546Spatrick #include "lldb/lldb-enumerations.h"
15061da546Spatrick #include "llvm/ADT/ArrayRef.h"
16061da546Spatrick #include "llvm/ADT/StringRef.h"
17061da546Spatrick 
18061da546Spatrick #include <string>
19061da546Spatrick 
20061da546Spatrick namespace lldb_private {
21061da546Spatrick 
22061da546Spatrick class ExecutionContext;
23061da546Spatrick 
24061da546Spatrick class ClangExpressionSourceCode : public ExpressionSourceCode {
25061da546Spatrick public:
26061da546Spatrick   /// The file name we use for the wrapper code that we inject before
27061da546Spatrick   /// the user expression.
28061da546Spatrick   static const llvm::StringRef g_prefix_file_name;
29061da546Spatrick   static const char *g_expression_prefix;
30be691f3bSpatrick   static const char *g_expression_suffix;
31061da546Spatrick 
32dda28197Spatrick   /// The possible ways an expression can be wrapped.
33dda28197Spatrick   enum class WrapKind {
34dda28197Spatrick     /// Wrapped in a non-static member function of a C++ class.
35dda28197Spatrick     CppMemberFunction,
36dda28197Spatrick     /// Wrapped in an instance Objective-C method.
37dda28197Spatrick     ObjCInstanceMethod,
38dda28197Spatrick     /// Wrapped in a static Objective-C method.
39dda28197Spatrick     ObjCStaticMethod,
40dda28197Spatrick     /// Wrapped in a non-member function.
41dda28197Spatrick     /// Note that this is also used for static member functions of a C++ class.
42dda28197Spatrick     Function
43dda28197Spatrick   };
44dda28197Spatrick 
CreateWrapped(llvm::StringRef filename,llvm::StringRef prefix,llvm::StringRef body,WrapKind wrap_kind)45061da546Spatrick   static ClangExpressionSourceCode *CreateWrapped(llvm::StringRef filename,
46061da546Spatrick                                                   llvm::StringRef prefix,
47dda28197Spatrick                                                   llvm::StringRef body,
48dda28197Spatrick                                                   WrapKind wrap_kind) {
49061da546Spatrick     return new ClangExpressionSourceCode(filename, "$__lldb_expr", prefix, body,
50dda28197Spatrick                                          Wrap, wrap_kind);
51061da546Spatrick   }
52061da546Spatrick 
53061da546Spatrick   /// Generates the source code that will evaluate the expression.
54061da546Spatrick   ///
55061da546Spatrick   /// \param text output parameter containing the source code string.
56061da546Spatrick   /// \param exe_ctx The execution context in which the expression will be
57061da546Spatrick   ///        evaluated.
58061da546Spatrick   /// \param add_locals True iff local variables should be injected into the
59061da546Spatrick   ///        expression source code.
60061da546Spatrick   /// \param force_add_all_locals True iff all local variables should be
61061da546Spatrick   ///        injected even if they are not used in the expression.
62061da546Spatrick   /// \param modules A list of (C++) modules that the expression should import.
63061da546Spatrick   ///
64061da546Spatrick   /// \return true iff the source code was successfully generated.
65dda28197Spatrick   bool GetText(std::string &text, ExecutionContext &exe_ctx, bool add_locals,
66061da546Spatrick                bool force_add_all_locals,
67061da546Spatrick                llvm::ArrayRef<std::string> modules) const;
68061da546Spatrick 
69061da546Spatrick   // Given a string returned by GetText, find the beginning and end of the body
70061da546Spatrick   // passed to CreateWrapped. Return true if the bounds could be found.  This
71061da546Spatrick   // will also work on text with FixItHints applied.
72061da546Spatrick   bool GetOriginalBodyBounds(std::string transformed_text,
73061da546Spatrick                              size_t &start_loc, size_t &end_loc);
74061da546Spatrick 
75061da546Spatrick protected:
76061da546Spatrick   ClangExpressionSourceCode(llvm::StringRef filename, llvm::StringRef name,
77061da546Spatrick                             llvm::StringRef prefix, llvm::StringRef body,
78dda28197Spatrick                             Wrapping wrap, WrapKind wrap_kind);
79061da546Spatrick 
80061da546Spatrick private:
81*f6aab3d8Srobert   /// Writes "using" declarations for local variables into the specified stream.
82*f6aab3d8Srobert   ///
83*f6aab3d8Srobert   /// Behaviour is undefined if 'frame == nullptr'.
84*f6aab3d8Srobert   ///
85*f6aab3d8Srobert   /// \param[out] stream Stream that this function generates "using"
86*f6aab3d8Srobert   ///             declarations into.
87*f6aab3d8Srobert   ///
88*f6aab3d8Srobert   /// \param[in]  expr Expression source that we're evaluating.
89*f6aab3d8Srobert   ///
90*f6aab3d8Srobert   /// \param[in]  frame StackFrame which carries information about the local
91*f6aab3d8Srobert   ///             variables that we're generating "using" declarations for.
92*f6aab3d8Srobert   void AddLocalVariableDecls(StreamString &stream, const std::string &expr,
93*f6aab3d8Srobert                              StackFrame *frame) const;
94dda28197Spatrick 
95061da546Spatrick   /// String marking the start of the user expression.
96061da546Spatrick   std::string m_start_marker;
97061da546Spatrick   /// String marking the end of the user expression.
98061da546Spatrick   std::string m_end_marker;
99dda28197Spatrick   /// How the expression has been wrapped.
100dda28197Spatrick   const WrapKind m_wrap_kind;
101061da546Spatrick };
102061da546Spatrick 
103061da546Spatrick } // namespace lldb_private
104061da546Spatrick 
105061da546Spatrick #endif
106