1namespace lldb_private {
2namespace python {
3
4PythonObject ToSWIGHelper(void *obj, swig_type_info *info) {
5  return {PyRefType::Owned, SWIG_NewPointerObj(obj, info, SWIG_POINTER_OWN)};
6}
7
8/// A class that automatically clears an SB object when it goes out of scope.
9/// Use for cases where the SB object points to a temporary/unowned entity.
10template <typename T> class ScopedPythonObject : PythonObject {
11public:
12  ScopedPythonObject(T *sb, swig_type_info *info)
13      : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {}
14  ~ScopedPythonObject() {
15    if (m_sb)
16      *m_sb = T();
17  }
18  ScopedPythonObject(ScopedPythonObject &&rhs)
19      : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {}
20  ScopedPythonObject(const ScopedPythonObject &) = delete;
21  ScopedPythonObject &operator=(const ScopedPythonObject &) = delete;
22  ScopedPythonObject &operator=(ScopedPythonObject &&) = delete;
23
24  const PythonObject &obj() const { return *this; }
25
26private:
27  T *m_sb;
28};
29
30PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb) {
31  return ToSWIGHelper(value_sb.release(), SWIGTYPE_p_lldb__SBValue);
32}
33
34PythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp) {
35  return ToSWIGWrapper(std::make_unique<lldb::SBValue>(std::move(value_sp)));
36}
37
38PythonObject ToSWIGWrapper(lldb::TargetSP target_sp) {
39  return ToSWIGHelper(new lldb::SBTarget(std::move(target_sp)),
40                      SWIGTYPE_p_lldb__SBTarget);
41}
42
43PythonObject ToSWIGWrapper(lldb::ProcessSP process_sp) {
44  return ToSWIGHelper(new lldb::SBProcess(std::move(process_sp)),
45                      SWIGTYPE_p_lldb__SBProcess);
46}
47
48PythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp) {
49  return ToSWIGHelper(new lldb::SBThreadPlan(std::move(thread_plan_sp)),
50                      SWIGTYPE_p_lldb__SBThreadPlan);
51}
52
53PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp) {
54  return ToSWIGHelper(new lldb::SBBreakpoint(std::move(breakpoint_sp)),
55                      SWIGTYPE_p_lldb__SBBreakpoint);
56}
57
58PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStream> stream_sb) {
59  return ToSWIGHelper(stream_sb.release(), SWIGTYPE_p_lldb__SBStream);
60}
61
62PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb) {
63  return ToSWIGHelper(data_sb.release(), SWIGTYPE_p_lldb__SBStructuredData);
64}
65
66PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl) {
67  return ToSWIGWrapper(std::make_unique<lldb::SBStructuredData>(data_impl));
68}
69
70PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp) {
71  return ToSWIGHelper(new lldb::SBThread(std::move(thread_sp)),
72                      SWIGTYPE_p_lldb__SBThread);
73}
74
75PythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp) {
76  return ToSWIGHelper(new lldb::SBFrame(std::move(frame_sp)),
77                      SWIGTYPE_p_lldb__SBFrame);
78}
79
80PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp) {
81  return ToSWIGHelper(new lldb::SBDebugger(std::move(debugger_sp)),
82                      SWIGTYPE_p_lldb__SBDebugger);
83}
84
85PythonObject ToSWIGWrapper(lldb::WatchpointSP watchpoint_sp) {
86  return ToSWIGHelper(new lldb::SBWatchpoint(std::move(watchpoint_sp)),
87                      SWIGTYPE_p_lldb__SBWatchpoint);
88}
89
90PythonObject ToSWIGWrapper(lldb::BreakpointLocationSP bp_loc_sp) {
91  return ToSWIGHelper(new lldb::SBBreakpointLocation(std::move(bp_loc_sp)),
92                      SWIGTYPE_p_lldb__SBBreakpointLocation);
93}
94
95PythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp) {
96  return ToSWIGHelper(new lldb::SBExecutionContext(std::move(ctx_sp)),
97                      SWIGTYPE_p_lldb__SBExecutionContext);
98}
99
100PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options) {
101  return ToSWIGHelper(new lldb::SBTypeSummaryOptions(summary_options),
102                      SWIGTYPE_p_lldb__SBTypeSummaryOptions);
103}
104
105PythonObject ToSWIGWrapper(const SymbolContext &sym_ctx) {
106  return ToSWIGHelper(new lldb::SBSymbolContext(sym_ctx),
107                      SWIGTYPE_p_lldb__SBSymbolContext);
108}
109
110ScopedPythonObject<lldb::SBCommandReturnObject>
111ToSWIGWrapper(CommandReturnObject &cmd_retobj) {
112  return ScopedPythonObject<lldb::SBCommandReturnObject>(
113      new lldb::SBCommandReturnObject(cmd_retobj),
114      SWIGTYPE_p_lldb__SBCommandReturnObject);
115}
116
117ScopedPythonObject<lldb::SBEvent> ToSWIGWrapper(Event *event) {
118  return ScopedPythonObject<lldb::SBEvent>(new lldb::SBEvent(event),
119                                           SWIGTYPE_p_lldb__SBEvent);
120}
121
122} // namespace python
123} // namespace lldb_private
124