xref: /llvm-project/lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h (revision 21026073e3b0583caf0c81564c4d7ebf002fe67b)
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 
30   StructuredData::GenericSP GetScriptObjectInstance() {
31     return m_object_instance_sp;
32   }
33 
34   struct AbstractMethodRequirement {
35     llvm::StringLiteral name;
36     size_t min_arg_count = 0;
37   };
38 
39   virtual llvm::SmallVector<AbstractMethodRequirement>
40   GetAbstractMethodRequirements() const = 0;
41 
42   llvm::SmallVector<llvm::StringLiteral> const GetAbstractMethods() const {
43     llvm::SmallVector<llvm::StringLiteral> abstract_methods;
44     llvm::transform(GetAbstractMethodRequirements(), abstract_methods.begin(),
45                     [](const AbstractMethodRequirement &requirement) {
46                       return requirement.name;
47                     });
48     return abstract_methods;
49   }
50 
51   template <typename Ret>
52   static Ret ErrorWithMessage(llvm::StringRef caller_name,
53                               llvm::StringRef error_msg, Status &error,
54                               LLDBLog log_category = LLDBLog::Process) {
55     LLDB_LOGF(GetLog(log_category), "%s ERROR = %s", caller_name.data(),
56               error_msg.data());
57     std::string full_error_message =
58         llvm::Twine(caller_name + llvm::Twine(" ERROR = ") +
59                     llvm::Twine(error_msg))
60             .str();
61     if (const char *detailed_error = error.AsCString())
62       full_error_message +=
63           llvm::Twine(llvm::Twine(" (") + llvm::Twine(detailed_error) +
64                       llvm::Twine(")"))
65               .str();
66     error = Status(std::move(full_error_message));
67     return {};
68   }
69 
70   template <typename T = StructuredData::ObjectSP>
71   static bool CheckStructuredDataObject(llvm::StringRef caller, T obj,
72                                         Status &error) {
73     if (!obj)
74       return ErrorWithMessage<bool>(caller, "Null Structured Data object",
75                                     error);
76 
77     if (!obj->IsValid()) {
78       return ErrorWithMessage<bool>(caller, "Invalid StructuredData object",
79                                     error);
80     }
81 
82     if (error.Fail())
83       return ErrorWithMessage<bool>(caller, error.AsCString(), error);
84 
85     return true;
86   }
87 
88   static bool CreateInstance(lldb::ScriptLanguage language,
89                              ScriptedInterfaceUsages usages) {
90     return false;
91   }
92 
93 protected:
94   StructuredData::GenericSP m_object_instance_sp;
95 };
96 } // namespace lldb_private
97 #endif // LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
98