1 //===-- REPL.h --------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef lldb_REPL_h
11 #define lldb_REPL_h
12 
13 // C Includes
14 // C++ Includes
15 #include <string>
16 
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/Interpreter/OptionGroupFormat.h"
20 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
21 #include "lldb/../../source/Commands/CommandObjectExpression.h"
22 
23 namespace lldb_private
24 {
25 
26 class REPL : public IOHandlerDelegate
27 {
28 public:
29     //----------------------------------------------------------------------
30     // See TypeSystem.h for how to add subclasses to this.
31     //----------------------------------------------------------------------
32     enum LLVMCastKind {
33         eKindClang,
34         eKindSwift,
35         eKindGo,
36         kNumKinds
37     };
38 
39     LLVMCastKind getKind() const { return m_kind; }
40 
41     REPL(LLVMCastKind kind, Target &target);
42 
43     ~REPL() override;
44 
45     //------------------------------------------------------------------
46     /// Get a REPL with an existing target (or, failing that, a debugger to use), and (optional) extra arguments for the compiler.
47     ///
48     /// @param[out] error
49     ///     If this language is supported but the REPL couldn't be created, this error is populated with the reason.
50     ///
51     /// @param[in] language
52     ///     The language to create a REPL for.
53     ///
54     /// @param[in] debugger
55     ///     If provided, and target is nullptr, the debugger to use when setting up a top-level REPL.
56     ///
57     /// @param[in] target
58     ///     If provided, the target to put the REPL inside.
59     ///
60     /// @param[in] repl_options
61     ///     If provided, additional options for the compiler when parsing REPL expressions.
62     ///
63     /// @return
64     ///     The range of the containing object in the target process.
65     //------------------------------------------------------------------
66     static lldb::REPLSP
67     Create (Error &Error, lldb::LanguageType language, Debugger *debugger, Target *target, const char *repl_options);
68 
69     void
70     SetFormatOptions (const OptionGroupFormat &options)
71     {
72         m_format_options = options;
73     }
74 
75     void
76     SetValueObjectDisplayOptions (const OptionGroupValueObjectDisplay &options)
77     {
78         m_varobj_options = options;
79     }
80 
81     void
82     SetCommandOptions (const CommandObjectExpression::CommandOptions &options)
83     {
84         m_command_options = options;
85     }
86 
87     void
88     SetCompilerOptions (const char *options)
89     {
90         if (options)
91             m_compiler_options = options;
92     }
93 
94     lldb::IOHandlerSP
95     GetIOHandler ();
96 
97     Error
98     RunLoop ();
99 
100     //------------------------------------------------------------------
101     // IOHandler::Delegate functions
102     //------------------------------------------------------------------
103     void
104     IOHandlerActivated (IOHandler &io_handler) override;
105 
106     bool
107     IOHandlerInterrupt (IOHandler &io_handler) override;
108 
109     void
110     IOHandlerInputInterrupted (IOHandler &io_handler,
111                                std::string &line) override;
112 
113     const char *
114     IOHandlerGetFixIndentationCharacters () override;
115 
116     ConstString
117     IOHandlerGetControlSequence (char ch) override;
118 
119     const char *
120     IOHandlerGetCommandPrefix () override;
121 
122     const char *
123     IOHandlerGetHelpPrologue () override;
124 
125     bool
126     IOHandlerIsInputComplete (IOHandler &io_handler,
127                               StringList &lines) override;
128 
129     int
130     IOHandlerFixIndentation (IOHandler &io_handler,
131                              const StringList &lines,
132                              int cursor_position) override;
133 
134     void
135     IOHandlerInputComplete (IOHandler &io_handler,
136                             std::string &line) override;
137 
138     int
139     IOHandlerComplete (IOHandler &io_handler,
140                        const char *current_line,
141                        const char *cursor,
142                        const char *last_char,
143                        int skip_first_n_matches,
144                        int max_matches,
145                        StringList &matches) override;
146 
147 protected:
148     static int
149     CalculateActualIndentation (const StringList &lines);
150 
151     //----------------------------------------------------------------------
152     // Subclasses should override these functions to implement a functional REPL.
153     //----------------------------------------------------------------------
154 
155     virtual Error
156     DoInitialization () = 0;
157 
158     virtual ConstString
159     GetSourceFileBasename () = 0;
160 
161     virtual const char *
162     GetAutoIndentCharacters () = 0;
163 
164     virtual bool
165     SourceIsComplete (const std::string &source) = 0;
166 
167     virtual lldb::offset_t
168     GetDesiredIndentation (const StringList &lines,
169                            int cursor_position,
170                            int tab_size) = 0; // LLDB_INVALID_OFFSET means no change
171 
172     virtual lldb::LanguageType
173     GetLanguage () = 0;
174 
175     virtual bool
176     PrintOneVariable (Debugger &debugger,
177                       lldb::StreamFileSP &output_sp,
178                       lldb::ValueObjectSP &valobj_sp,
179                       ExpressionVariable *var = nullptr) = 0;
180 
181     virtual int
182     CompleteCode(const std::string &current_code,
183                  StringList &matches) = 0;
184 
185     OptionGroupFormat m_format_options = OptionGroupFormat(lldb::eFormatDefault);
186     OptionGroupValueObjectDisplay m_varobj_options;
187     CommandObjectExpression::CommandOptions m_command_options;
188     std::string m_compiler_options;
189 
190     bool m_enable_auto_indent = true;
191     std::string m_indent_str; // Use this string for each level of indentation
192     std::string m_current_indent_str;
193     uint32_t m_current_indent_level = 0;
194 
195     std::string m_repl_source_path;
196     bool m_dedicated_repl_mode = false;
197 
198     StringList m_code; // All accumulated REPL statements are saved here
199 
200     Target &m_target;
201     lldb::IOHandlerSP m_io_handler_sp;
202     LLVMCastKind m_kind;
203 
204 private:
205     std::string
206     GetSourcePath();
207 };
208 
209 } // namespace lldb_private
210 
211 #endif // lldb_REPL_h
212