Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:48

0001 //===-- REPL.h --------------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_EXPRESSION_REPL_H
0010 #define LLDB_EXPRESSION_REPL_H
0011 
0012 #include <string>
0013 
0014 #include "lldb/Core/IOHandler.h"
0015 #include "lldb/Interpreter/OptionGroupFormat.h"
0016 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
0017 #include "lldb/Target/Target.h"
0018 #include "llvm/Support/ExtensibleRTTI.h"
0019 
0020 namespace lldb_private {
0021 
0022 class REPL : public IOHandlerDelegate,
0023              public llvm::RTTIExtends<REPL, llvm::RTTIRoot> {
0024 public:
0025   /// LLVM RTTI support
0026   static char ID;
0027 
0028   REPL(Target &target);
0029 
0030   ~REPL() override;
0031 
0032   /// Get a REPL with an existing target (or, failing that, a debugger to use),
0033   /// and (optional) extra arguments for the compiler.
0034   ///
0035   /// \param[out] Status
0036   ///     If this language is supported but the REPL couldn't be created, this
0037   ///     error is populated with the reason.
0038   ///
0039   /// \param[in] language
0040   ///     The language to create a REPL for.
0041   ///
0042   /// \param[in] debugger
0043   ///     If provided, and target is nullptr, the debugger to use when setting
0044   ///     up a top-level REPL.
0045   ///
0046   /// \param[in] target
0047   ///     If provided, the target to put the REPL inside.
0048   ///
0049   /// \param[in] repl_options
0050   ///     If provided, additional options for the compiler when parsing REPL
0051   ///     expressions.
0052   ///
0053   /// \return
0054   ///     The range of the containing object in the target process.
0055   static lldb::REPLSP Create(Status &Status, lldb::LanguageType language,
0056                              Debugger *debugger, Target *target,
0057                              const char *repl_options);
0058 
0059   void SetFormatOptions(const OptionGroupFormat &options) {
0060     m_format_options = options;
0061   }
0062 
0063   void
0064   SetValueObjectDisplayOptions(const OptionGroupValueObjectDisplay &options) {
0065     m_varobj_options = options;
0066   }
0067 
0068   void SetEvaluateOptions(const EvaluateExpressionOptions &options) {
0069     m_expr_options = options;
0070   }
0071 
0072   void SetCompilerOptions(const char *options) {
0073     if (options)
0074       m_compiler_options = options;
0075   }
0076 
0077   lldb::IOHandlerSP GetIOHandler();
0078 
0079   Status RunLoop();
0080 
0081   // IOHandler::Delegate functions
0082   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override;
0083 
0084   bool IOHandlerInterrupt(IOHandler &io_handler) override;
0085 
0086   void IOHandlerInputInterrupted(IOHandler &io_handler,
0087                                  std::string &line) override;
0088 
0089   const char *IOHandlerGetFixIndentationCharacters() override;
0090 
0091   llvm::StringRef IOHandlerGetControlSequence(char ch) override;
0092 
0093   const char *IOHandlerGetCommandPrefix() override;
0094 
0095   const char *IOHandlerGetHelpPrologue() override;
0096 
0097   bool IOHandlerIsInputComplete(IOHandler &io_handler,
0098                                 StringList &lines) override;
0099 
0100   int IOHandlerFixIndentation(IOHandler &io_handler, const StringList &lines,
0101                               int cursor_position) override;
0102 
0103   void IOHandlerInputComplete(IOHandler &io_handler,
0104                               std::string &line) override;
0105 
0106   void IOHandlerComplete(IOHandler &io_handler,
0107                          CompletionRequest &request) override;
0108 
0109 protected:
0110   /// Method that can be optionally overriden by subclasses to get notified
0111   /// whenever an expression has been evaluated. The params of this method
0112   /// include the inputs and outputs of the expression evaluation.
0113   ///
0114   /// Note: meta commands that start with : are not covered by this method.
0115   ///
0116   /// \return
0117   ///   An \a Error object that, if it is a failure, aborts the regular
0118   ///   REPL expression result handling.
0119   virtual llvm::Error
0120   OnExpressionEvaluated(const ExecutionContext &exe_ctx, llvm::StringRef code,
0121                         const EvaluateExpressionOptions &expr_options,
0122                         lldb::ExpressionResults execution_results,
0123                         const lldb::ValueObjectSP &result_valobj_sp,
0124                         const Status &error) {
0125     return llvm::Error::success();
0126   }
0127 
0128   static int CalculateActualIndentation(const StringList &lines);
0129 
0130   // Subclasses should override these functions to implement a functional REPL.
0131 
0132   virtual Status DoInitialization() = 0;
0133 
0134   virtual llvm::StringRef GetSourceFileBasename() = 0;
0135 
0136   virtual const char *GetAutoIndentCharacters() = 0;
0137 
0138   virtual bool SourceIsComplete(const std::string &source) = 0;
0139 
0140   virtual lldb::offset_t GetDesiredIndentation(
0141       const StringList &lines, int cursor_position,
0142       int tab_size) = 0; // LLDB_INVALID_OFFSET means no change
0143 
0144   virtual lldb::LanguageType GetLanguage() = 0;
0145 
0146   virtual bool PrintOneVariable(Debugger &debugger,
0147                                 lldb::StreamFileSP &output_sp,
0148                                 lldb::ValueObjectSP &valobj_sp,
0149                                 ExpressionVariable *var = nullptr) = 0;
0150 
0151   virtual void CompleteCode(const std::string &current_code,
0152                             CompletionRequest &request) = 0;
0153 
0154   OptionGroupFormat m_format_options = OptionGroupFormat(lldb::eFormatDefault);
0155   OptionGroupValueObjectDisplay m_varobj_options;
0156   EvaluateExpressionOptions m_expr_options;
0157   std::string m_compiler_options;
0158 
0159   bool m_enable_auto_indent = true;
0160   std::string m_indent_str; // Use this string for each level of indentation
0161   std::string m_current_indent_str;
0162   uint32_t m_current_indent_level = 0;
0163 
0164   std::string m_repl_source_path;
0165   bool m_dedicated_repl_mode = false;
0166 
0167   StringList m_code; // All accumulated REPL statements are saved here
0168 
0169   Target &m_target;
0170   lldb::IOHandlerSP m_io_handler_sp;
0171 
0172 private:
0173   std::string GetSourcePath();
0174 };
0175 
0176 } // namespace lldb_private
0177 
0178 #endif // LLDB_EXPRESSION_REPL_H