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 //===----------------------------------------------------------------------===//
17 // Other libraries and framework includes
19 #include "lldb/../../source/Commands/CommandObjectExpression.h"
20 #include "lldb/Interpreter/OptionGroupFormat.h"
21 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
23 namespace lldb_private {
25 class REPL : public IOHandlerDelegate {
27 //----------------------------------------------------------------------
28 // See TypeSystem.h for how to add subclasses to this.
29 //----------------------------------------------------------------------
30 enum LLVMCastKind { eKindClang, eKindSwift, eKindGo, kNumKinds };
32 LLVMCastKind getKind() const { return m_kind; }
34 REPL(LLVMCastKind kind, Target &target);
38 //------------------------------------------------------------------
39 /// Get a REPL with an existing target (or, failing that, a debugger to use),
40 /// and (optional) extra arguments for the compiler.
43 /// If this language is supported but the REPL couldn't be created, this
44 /// error is populated with the reason.
46 /// @param[in] language
47 /// The language to create a REPL for.
49 /// @param[in] debugger
50 /// If provided, and target is nullptr, the debugger to use when setting
51 /// up a top-level REPL.
54 /// If provided, the target to put the REPL inside.
56 /// @param[in] repl_options
57 /// If provided, additional options for the compiler when parsing REPL
61 /// The range of the containing object in the target process.
62 //------------------------------------------------------------------
63 static lldb::REPLSP Create(Error &Error, lldb::LanguageType language,
64 Debugger *debugger, Target *target,
65 const char *repl_options);
67 void SetFormatOptions(const OptionGroupFormat &options) {
68 m_format_options = options;
72 SetValueObjectDisplayOptions(const OptionGroupValueObjectDisplay &options) {
73 m_varobj_options = options;
77 SetCommandOptions(const CommandObjectExpression::CommandOptions &options) {
78 m_command_options = options;
81 void SetCompilerOptions(const char *options) {
83 m_compiler_options = options;
86 lldb::IOHandlerSP GetIOHandler();
90 //------------------------------------------------------------------
91 // IOHandler::Delegate functions
92 //------------------------------------------------------------------
93 void IOHandlerActivated(IOHandler &io_handler) override;
95 bool IOHandlerInterrupt(IOHandler &io_handler) override;
97 void IOHandlerInputInterrupted(IOHandler &io_handler,
98 std::string &line) override;
100 const char *IOHandlerGetFixIndentationCharacters() override;
102 ConstString IOHandlerGetControlSequence(char ch) override;
104 const char *IOHandlerGetCommandPrefix() override;
106 const char *IOHandlerGetHelpPrologue() override;
108 bool IOHandlerIsInputComplete(IOHandler &io_handler,
109 StringList &lines) override;
111 int IOHandlerFixIndentation(IOHandler &io_handler, const StringList &lines,
112 int cursor_position) override;
114 void IOHandlerInputComplete(IOHandler &io_handler,
115 std::string &line) override;
117 int IOHandlerComplete(IOHandler &io_handler, const char *current_line,
118 const char *cursor, const char *last_char,
119 int skip_first_n_matches, int max_matches,
120 StringList &matches) override;
123 static int CalculateActualIndentation(const StringList &lines);
125 //----------------------------------------------------------------------
126 // Subclasses should override these functions to implement a functional REPL.
127 //----------------------------------------------------------------------
129 virtual Error DoInitialization() = 0;
131 virtual ConstString GetSourceFileBasename() = 0;
133 virtual const char *GetAutoIndentCharacters() = 0;
135 virtual bool SourceIsComplete(const std::string &source) = 0;
137 virtual lldb::offset_t GetDesiredIndentation(
138 const StringList &lines, int cursor_position,
139 int tab_size) = 0; // LLDB_INVALID_OFFSET means no change
141 virtual lldb::LanguageType GetLanguage() = 0;
143 virtual bool PrintOneVariable(Debugger &debugger,
144 lldb::StreamFileSP &output_sp,
145 lldb::ValueObjectSP &valobj_sp,
146 ExpressionVariable *var = nullptr) = 0;
148 virtual int CompleteCode(const std::string ¤t_code,
149 StringList &matches) = 0;
151 OptionGroupFormat m_format_options = OptionGroupFormat(lldb::eFormatDefault);
152 OptionGroupValueObjectDisplay m_varobj_options;
153 CommandObjectExpression::CommandOptions m_command_options;
154 std::string m_compiler_options;
156 bool m_enable_auto_indent = true;
157 std::string m_indent_str; // Use this string for each level of indentation
158 std::string m_current_indent_str;
159 uint32_t m_current_indent_level = 0;
161 std::string m_repl_source_path;
162 bool m_dedicated_repl_mode = false;
164 StringList m_code; // All accumulated REPL statements are saved here
167 lldb::IOHandlerSP m_io_handler_sp;
171 std::string GetSourcePath();
174 } // namespace lldb_private
176 #endif // lldb_REPL_h