1 //===-- REPL.h --------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
15 #include "lldb/../../source/Commands/CommandObjectExpression.h"
16 #include "lldb/Interpreter/OptionGroupFormat.h"
17 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
19 namespace lldb_private {
21 class REPL : public IOHandlerDelegate {
23 //----------------------------------------------------------------------
24 // See TypeSystem.h for how to add subclasses to this.
25 //----------------------------------------------------------------------
26 enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
28 LLVMCastKind getKind() const { return m_kind; }
30 REPL(LLVMCastKind kind, Target &target);
34 //------------------------------------------------------------------
35 /// Get a REPL with an existing target (or, failing that, a debugger to use),
36 /// and (optional) extra arguments for the compiler.
39 /// If this language is supported but the REPL couldn't be created, this
40 /// error is populated with the reason.
42 /// @param[in] language
43 /// The language to create a REPL for.
45 /// @param[in] debugger
46 /// If provided, and target is nullptr, the debugger to use when setting
47 /// up a top-level REPL.
50 /// If provided, the target to put the REPL inside.
52 /// @param[in] repl_options
53 /// If provided, additional options for the compiler when parsing REPL
57 /// The range of the containing object in the target process.
58 //------------------------------------------------------------------
59 static lldb::REPLSP Create(Status &Status, lldb::LanguageType language,
60 Debugger *debugger, Target *target,
61 const char *repl_options);
63 void SetFormatOptions(const OptionGroupFormat &options) {
64 m_format_options = options;
68 SetValueObjectDisplayOptions(const OptionGroupValueObjectDisplay &options) {
69 m_varobj_options = options;
73 SetCommandOptions(const CommandObjectExpression::CommandOptions &options) {
74 m_command_options = options;
77 void SetCompilerOptions(const char *options) {
79 m_compiler_options = options;
82 lldb::IOHandlerSP GetIOHandler();
86 //------------------------------------------------------------------
87 // IOHandler::Delegate functions
88 //------------------------------------------------------------------
89 void IOHandlerActivated(IOHandler &io_handler) override;
91 bool IOHandlerInterrupt(IOHandler &io_handler) override;
93 void IOHandlerInputInterrupted(IOHandler &io_handler,
94 std::string &line) override;
96 const char *IOHandlerGetFixIndentationCharacters() override;
98 ConstString IOHandlerGetControlSequence(char ch) override;
100 const char *IOHandlerGetCommandPrefix() override;
102 const char *IOHandlerGetHelpPrologue() override;
104 bool IOHandlerIsInputComplete(IOHandler &io_handler,
105 StringList &lines) override;
107 int IOHandlerFixIndentation(IOHandler &io_handler, const StringList &lines,
108 int cursor_position) override;
110 void IOHandlerInputComplete(IOHandler &io_handler,
111 std::string &line) override;
113 int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
114 const char *cursor, const char *last_char,
115 int skip_first_n_matches, int max_matches,
116 StringList &matches, StringList &descriptions) override;
119 static int CalculateActualIndentation(const StringList &lines);
121 //----------------------------------------------------------------------
122 // Subclasses should override these functions to implement a functional REPL.
123 //----------------------------------------------------------------------
125 virtual Status DoInitialization() = 0;
127 virtual ConstString GetSourceFileBasename() = 0;
129 virtual const char *GetAutoIndentCharacters() = 0;
131 virtual bool SourceIsComplete(const std::string &source) = 0;
133 virtual lldb::offset_t GetDesiredIndentation(
134 const StringList &lines, int cursor_position,
135 int tab_size) = 0; // LLDB_INVALID_OFFSET means no change
137 virtual lldb::LanguageType GetLanguage() = 0;
139 virtual bool PrintOneVariable(Debugger &debugger,
140 lldb::StreamFileSP &output_sp,
141 lldb::ValueObjectSP &valobj_sp,
142 ExpressionVariable *var = nullptr) = 0;
144 virtual int CompleteCode(const std::string ¤t_code,
145 StringList &matches) = 0;
147 OptionGroupFormat m_format_options = OptionGroupFormat(lldb::eFormatDefault);
148 OptionGroupValueObjectDisplay m_varobj_options;
149 CommandObjectExpression::CommandOptions m_command_options;
150 std::string m_compiler_options;
152 bool m_enable_auto_indent = true;
153 std::string m_indent_str; // Use this string for each level of indentation
154 std::string m_current_indent_str;
155 uint32_t m_current_indent_level = 0;
157 std::string m_repl_source_path;
158 bool m_dedicated_repl_mode = false;
160 StringList m_code; // All accumulated REPL statements are saved here
163 lldb::IOHandlerSP m_io_handler_sp;
167 std::string GetSourcePath();
170 } // namespace lldb_private
172 #endif // lldb_REPL_h