1 //===-- REPL.h --------------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
14 #include "lldb/Core/IOHandler.h"
15 #include "lldb/Interpreter/OptionGroupFormat.h"
16 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
17 #include "lldb/Target/Target.h"
19 namespace lldb_private {
21 class REPL : public IOHandlerDelegate {
23 // See TypeSystem.h for how to add subclasses to this.
24 enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
26 LLVMCastKind getKind() const { return m_kind; }
28 REPL(LLVMCastKind kind, Target &target);
32 /// Get a REPL with an existing target (or, failing that, a debugger to use),
33 /// and (optional) extra arguments for the compiler.
36 /// If this language is supported but the REPL couldn't be created, this
37 /// error is populated with the reason.
39 /// \param[in] language
40 /// The language to create a REPL for.
42 /// \param[in] debugger
43 /// If provided, and target is nullptr, the debugger to use when setting
44 /// up a top-level REPL.
47 /// If provided, the target to put the REPL inside.
49 /// \param[in] repl_options
50 /// If provided, additional options for the compiler when parsing REPL
54 /// The range of the containing object in the target process.
55 static lldb::REPLSP Create(Status &Status, lldb::LanguageType language,
56 Debugger *debugger, Target *target,
57 const char *repl_options);
59 void SetFormatOptions(const OptionGroupFormat &options) {
60 m_format_options = options;
64 SetValueObjectDisplayOptions(const OptionGroupValueObjectDisplay &options) {
65 m_varobj_options = options;
68 void SetEvaluateOptions(const EvaluateExpressionOptions &options) {
69 m_expr_options = options;
72 void SetCompilerOptions(const char *options) {
74 m_compiler_options = options;
77 lldb::IOHandlerSP GetIOHandler();
81 // IOHandler::Delegate functions
82 void IOHandlerActivated(IOHandler &io_handler, bool interactive) override;
84 bool IOHandlerInterrupt(IOHandler &io_handler) override;
86 void IOHandlerInputInterrupted(IOHandler &io_handler,
87 std::string &line) override;
89 const char *IOHandlerGetFixIndentationCharacters() override;
91 ConstString IOHandlerGetControlSequence(char ch) override;
93 const char *IOHandlerGetCommandPrefix() override;
95 const char *IOHandlerGetHelpPrologue() override;
97 bool IOHandlerIsInputComplete(IOHandler &io_handler,
98 StringList &lines) override;
100 int IOHandlerFixIndentation(IOHandler &io_handler, const StringList &lines,
101 int cursor_position) override;
103 void IOHandlerInputComplete(IOHandler &io_handler,
104 std::string &line) override;
106 int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
107 const char *cursor, const char *last_char,
108 int skip_first_n_matches, int max_matches,
109 StringList &matches, StringList &descriptions) override;
112 static int CalculateActualIndentation(const StringList &lines);
114 // Subclasses should override these functions to implement a functional REPL.
116 virtual Status DoInitialization() = 0;
118 virtual ConstString GetSourceFileBasename() = 0;
120 virtual const char *GetAutoIndentCharacters() = 0;
122 virtual bool SourceIsComplete(const std::string &source) = 0;
124 virtual lldb::offset_t GetDesiredIndentation(
125 const StringList &lines, int cursor_position,
126 int tab_size) = 0; // LLDB_INVALID_OFFSET means no change
128 virtual lldb::LanguageType GetLanguage() = 0;
130 virtual bool PrintOneVariable(Debugger &debugger,
131 lldb::StreamFileSP &output_sp,
132 lldb::ValueObjectSP &valobj_sp,
133 ExpressionVariable *var = nullptr) = 0;
135 virtual int CompleteCode(const std::string ¤t_code,
136 StringList &matches) = 0;
138 OptionGroupFormat m_format_options = OptionGroupFormat(lldb::eFormatDefault);
139 OptionGroupValueObjectDisplay m_varobj_options;
140 EvaluateExpressionOptions m_expr_options;
141 std::string m_compiler_options;
143 bool m_enable_auto_indent = true;
144 std::string m_indent_str; // Use this string for each level of indentation
145 std::string m_current_indent_str;
146 uint32_t m_current_indent_level = 0;
148 std::string m_repl_source_path;
149 bool m_dedicated_repl_mode = false;
151 StringList m_code; // All accumulated REPL statements are saved here
154 lldb::IOHandlerSP m_io_handler_sp;
158 std::string GetSourcePath();
161 } // namespace lldb_private
163 #endif // lldb_REPL_h