• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- ScriptedInterface.h -------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
10 #define LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
11 
12 #include "ScriptedInterfaceUsages.h"
13 
14 #include "lldb/Core/StructuredDataImpl.h"
15 #include "lldb/Utility/LLDBLog.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/UnimplementedError.h"
18 #include "lldb/lldb-private.h"
19 
20 #include "llvm/Support/Compiler.h"
21 
22 #include <string>
23 
24 namespace lldb_private {
25 class ScriptedInterface {
26 public:
27   ScriptedInterface() = default;
28   virtual ~ScriptedInterface() = default;
29 
GetScriptObjectInstance()30   StructuredData::GenericSP GetScriptObjectInstance() {
31     return m_object_instance_sp;
32   }
33 
34   virtual llvm::SmallVector<llvm::StringLiteral> GetAbstractMethods() const = 0;
35 
36   template <typename Ret>
37   static Ret ErrorWithMessage(llvm::StringRef caller_name,
38                               llvm::StringRef error_msg, Status &error,
39                               LLDBLog log_category = LLDBLog::Process) {
40     LLDB_LOGF(GetLog(log_category), "%s ERROR = %s", caller_name.data(),
41               error_msg.data());
42     std::string full_error_message =
43         llvm::Twine(caller_name + llvm::Twine(" ERROR = ") +
44                     llvm::Twine(error_msg))
45             .str();
46     if (const char *detailed_error = error.AsCString())
47       full_error_message +=
48           llvm::Twine(llvm::Twine(" (") + llvm::Twine(detailed_error) +
49                       llvm::Twine(")"))
50               .str();
51     error.SetErrorString(full_error_message);
52     return {};
53   }
54 
55   template <typename T = StructuredData::ObjectSP>
CheckStructuredDataObject(llvm::StringRef caller,T obj,Status & error)56   static bool CheckStructuredDataObject(llvm::StringRef caller, T obj,
57                                         Status &error) {
58     if (!obj)
59       return ErrorWithMessage<bool>(caller, "Null Structured Data object",
60                                     error);
61 
62     if (!obj->IsValid()) {
63       return ErrorWithMessage<bool>(caller, "Invalid StructuredData object",
64                                     error);
65     }
66 
67     if (error.Fail())
68       return ErrorWithMessage<bool>(caller, error.AsCString(), error);
69 
70     return true;
71   }
72 
CreateInstance(lldb::ScriptLanguage language,ScriptedInterfaceUsages usages)73   static bool CreateInstance(lldb::ScriptLanguage language,
74                              ScriptedInterfaceUsages usages) {
75     return false;
76   }
77 
78 protected:
79   StructuredData::GenericSP m_object_instance_sp;
80 };
81 } // namespace lldb_private
82 #endif // LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
83