10b57cec5SDimitry Andric //===-- REPL.h --------------------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
95ffd83dbSDimitry Andric #ifndef LLDB_EXPRESSION_REPL_H
105ffd83dbSDimitry Andric #define LLDB_EXPRESSION_REPL_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include <string>
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "lldb/Core/IOHandler.h"
150b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupFormat.h"
160b57cec5SDimitry Andric #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
170b57cec5SDimitry Andric #include "lldb/Target/Target.h"
1806c3fb27SDimitry Andric #include "llvm/Support/ExtensibleRTTI.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric namespace lldb_private {
210b57cec5SDimitry Andric 
2206c3fb27SDimitry Andric class REPL : public IOHandlerDelegate,
2306c3fb27SDimitry Andric              public llvm::RTTIExtends<REPL, llvm::RTTIRoot> {
240b57cec5SDimitry Andric public:
2506c3fb27SDimitry Andric   /// LLVM RTTI support
2606c3fb27SDimitry Andric   static char ID;
270b57cec5SDimitry Andric 
2806c3fb27SDimitry Andric   REPL(Target &target);
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric   ~REPL() override;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric   /// Get a REPL with an existing target (or, failing that, a debugger to use),
330b57cec5SDimitry Andric   /// and (optional) extra arguments for the compiler.
340b57cec5SDimitry Andric   ///
35480093f4SDimitry Andric   /// \param[out] Status
360b57cec5SDimitry Andric   ///     If this language is supported but the REPL couldn't be created, this
370b57cec5SDimitry Andric   ///     error is populated with the reason.
380b57cec5SDimitry Andric   ///
390b57cec5SDimitry Andric   /// \param[in] language
400b57cec5SDimitry Andric   ///     The language to create a REPL for.
410b57cec5SDimitry Andric   ///
420b57cec5SDimitry Andric   /// \param[in] debugger
430b57cec5SDimitry Andric   ///     If provided, and target is nullptr, the debugger to use when setting
440b57cec5SDimitry Andric   ///     up a top-level REPL.
450b57cec5SDimitry Andric   ///
460b57cec5SDimitry Andric   /// \param[in] target
470b57cec5SDimitry Andric   ///     If provided, the target to put the REPL inside.
480b57cec5SDimitry Andric   ///
490b57cec5SDimitry Andric   /// \param[in] repl_options
500b57cec5SDimitry Andric   ///     If provided, additional options for the compiler when parsing REPL
510b57cec5SDimitry Andric   ///     expressions.
520b57cec5SDimitry Andric   ///
530b57cec5SDimitry Andric   /// \return
540b57cec5SDimitry Andric   ///     The range of the containing object in the target process.
550b57cec5SDimitry Andric   static lldb::REPLSP Create(Status &Status, lldb::LanguageType language,
560b57cec5SDimitry Andric                              Debugger *debugger, Target *target,
570b57cec5SDimitry Andric                              const char *repl_options);
580b57cec5SDimitry Andric 
SetFormatOptions(const OptionGroupFormat & options)590b57cec5SDimitry Andric   void SetFormatOptions(const OptionGroupFormat &options) {
600b57cec5SDimitry Andric     m_format_options = options;
610b57cec5SDimitry Andric   }
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   void
SetValueObjectDisplayOptions(const OptionGroupValueObjectDisplay & options)640b57cec5SDimitry Andric   SetValueObjectDisplayOptions(const OptionGroupValueObjectDisplay &options) {
650b57cec5SDimitry Andric     m_varobj_options = options;
660b57cec5SDimitry Andric   }
670b57cec5SDimitry Andric 
SetEvaluateOptions(const EvaluateExpressionOptions & options)680b57cec5SDimitry Andric   void SetEvaluateOptions(const EvaluateExpressionOptions &options) {
690b57cec5SDimitry Andric     m_expr_options = options;
700b57cec5SDimitry Andric   }
710b57cec5SDimitry Andric 
SetCompilerOptions(const char * options)720b57cec5SDimitry Andric   void SetCompilerOptions(const char *options) {
730b57cec5SDimitry Andric     if (options)
740b57cec5SDimitry Andric       m_compiler_options = options;
750b57cec5SDimitry Andric   }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   lldb::IOHandlerSP GetIOHandler();
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   Status RunLoop();
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   // IOHandler::Delegate functions
820b57cec5SDimitry Andric   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override;
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   bool IOHandlerInterrupt(IOHandler &io_handler) override;
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   void IOHandlerInputInterrupted(IOHandler &io_handler,
870b57cec5SDimitry Andric                                  std::string &line) override;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   const char *IOHandlerGetFixIndentationCharacters() override;
900b57cec5SDimitry Andric 
9106c3fb27SDimitry Andric   llvm::StringRef IOHandlerGetControlSequence(char ch) override;
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   const char *IOHandlerGetCommandPrefix() override;
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   const char *IOHandlerGetHelpPrologue() override;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   bool IOHandlerIsInputComplete(IOHandler &io_handler,
980b57cec5SDimitry Andric                                 StringList &lines) override;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   int IOHandlerFixIndentation(IOHandler &io_handler, const StringList &lines,
1010b57cec5SDimitry Andric                               int cursor_position) override;
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   void IOHandlerInputComplete(IOHandler &io_handler,
1040b57cec5SDimitry Andric                               std::string &line) override;
1050b57cec5SDimitry Andric 
1069dba64beSDimitry Andric   void IOHandlerComplete(IOHandler &io_handler,
1079dba64beSDimitry Andric                          CompletionRequest &request) override;
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric protected:
11006c3fb27SDimitry Andric   /// Method that can be optionally overriden by subclasses to get notified
11106c3fb27SDimitry Andric   /// whenever an expression has been evaluated. The params of this method
11206c3fb27SDimitry Andric   /// include the inputs and outputs of the expression evaluation.
11306c3fb27SDimitry Andric   ///
11406c3fb27SDimitry Andric   /// Note: meta commands that start with : are not covered by this method.
11506c3fb27SDimitry Andric   ///
11606c3fb27SDimitry Andric   /// \return
11706c3fb27SDimitry Andric   ///   An \a Error object that, if it is a failure, aborts the regular
11806c3fb27SDimitry Andric   ///   REPL expression result handling.
11906c3fb27SDimitry Andric   virtual llvm::Error
OnExpressionEvaluated(const ExecutionContext & exe_ctx,llvm::StringRef code,const EvaluateExpressionOptions & expr_options,lldb::ExpressionResults execution_results,const lldb::ValueObjectSP & result_valobj_sp,const Status & error)12006c3fb27SDimitry Andric   OnExpressionEvaluated(const ExecutionContext &exe_ctx, llvm::StringRef code,
12106c3fb27SDimitry Andric                         const EvaluateExpressionOptions &expr_options,
12206c3fb27SDimitry Andric                         lldb::ExpressionResults execution_results,
12306c3fb27SDimitry Andric                         const lldb::ValueObjectSP &result_valobj_sp,
12406c3fb27SDimitry Andric                         const Status &error) {
12506c3fb27SDimitry Andric     return llvm::Error::success();
12606c3fb27SDimitry Andric   }
12706c3fb27SDimitry Andric 
1280b57cec5SDimitry Andric   static int CalculateActualIndentation(const StringList &lines);
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   // Subclasses should override these functions to implement a functional REPL.
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   virtual Status DoInitialization() = 0;
1330b57cec5SDimitry Andric 
13406c3fb27SDimitry Andric   virtual llvm::StringRef GetSourceFileBasename() = 0;
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   virtual const char *GetAutoIndentCharacters() = 0;
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   virtual bool SourceIsComplete(const std::string &source) = 0;
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   virtual lldb::offset_t GetDesiredIndentation(
1410b57cec5SDimitry Andric       const StringList &lines, int cursor_position,
1420b57cec5SDimitry Andric       int tab_size) = 0; // LLDB_INVALID_OFFSET means no change
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   virtual lldb::LanguageType GetLanguage() = 0;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   virtual bool PrintOneVariable(Debugger &debugger,
1470b57cec5SDimitry Andric                                 lldb::StreamFileSP &output_sp,
1480b57cec5SDimitry Andric                                 lldb::ValueObjectSP &valobj_sp,
1490b57cec5SDimitry Andric                                 ExpressionVariable *var = nullptr) = 0;
1500b57cec5SDimitry Andric 
1515ffd83dbSDimitry Andric   virtual void CompleteCode(const std::string &current_code,
1525ffd83dbSDimitry Andric                             CompletionRequest &request) = 0;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   OptionGroupFormat m_format_options = OptionGroupFormat(lldb::eFormatDefault);
1550b57cec5SDimitry Andric   OptionGroupValueObjectDisplay m_varobj_options;
1560b57cec5SDimitry Andric   EvaluateExpressionOptions m_expr_options;
1570b57cec5SDimitry Andric   std::string m_compiler_options;
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   bool m_enable_auto_indent = true;
1600b57cec5SDimitry Andric   std::string m_indent_str; // Use this string for each level of indentation
1610b57cec5SDimitry Andric   std::string m_current_indent_str;
1620b57cec5SDimitry Andric   uint32_t m_current_indent_level = 0;
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   std::string m_repl_source_path;
1650b57cec5SDimitry Andric   bool m_dedicated_repl_mode = false;
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   StringList m_code; // All accumulated REPL statements are saved here
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   Target &m_target;
1700b57cec5SDimitry Andric   lldb::IOHandlerSP m_io_handler_sp;
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric private:
1730b57cec5SDimitry Andric   std::string GetSourcePath();
1740b57cec5SDimitry Andric };
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric } // namespace lldb_private
1770b57cec5SDimitry Andric 
1785ffd83dbSDimitry Andric #endif // LLDB_EXPRESSION_REPL_H
179