17f09ab08SPavel Labathnamespace lldb_private {
27f09ab08SPavel Labathnamespace python {
37f09ab08SPavel Labath
47f09ab08SPavel LabathPythonObject ToSWIGHelper(void *obj, swig_type_info *info) {
57f09ab08SPavel Labath  return {PyRefType::Owned, SWIG_NewPointerObj(obj, info, SWIG_POINTER_OWN)};
67f09ab08SPavel Labath}
77f09ab08SPavel Labath
8*0a07c966SPavel Labath/// A class that automatically clears an SB object when it goes out of scope.
9*0a07c966SPavel Labath/// Use for cases where the SB object points to a temporary/unowned entity.
10*0a07c966SPavel Labathtemplate <typename T> class ScopedPythonObject : PythonObject {
11*0a07c966SPavel Labathpublic:
12*0a07c966SPavel Labath  ScopedPythonObject(T *sb, swig_type_info *info)
13*0a07c966SPavel Labath      : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {}
14*0a07c966SPavel Labath  ~ScopedPythonObject() {
15*0a07c966SPavel Labath    if (m_sb)
16*0a07c966SPavel Labath      *m_sb = T();
17*0a07c966SPavel Labath  }
18*0a07c966SPavel Labath  ScopedPythonObject(ScopedPythonObject &&rhs)
19*0a07c966SPavel Labath      : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {}
20*0a07c966SPavel Labath  ScopedPythonObject(const ScopedPythonObject &) = delete;
21*0a07c966SPavel Labath  ScopedPythonObject &operator=(const ScopedPythonObject &) = delete;
22*0a07c966SPavel Labath  ScopedPythonObject &operator=(ScopedPythonObject &&) = delete;
23*0a07c966SPavel Labath
24*0a07c966SPavel Labath  const PythonObject &obj() const { return *this; }
25*0a07c966SPavel Labath
26*0a07c966SPavel Labathprivate:
27*0a07c966SPavel Labath  T *m_sb;
28*0a07c966SPavel Labath};
29*0a07c966SPavel Labath
307f09ab08SPavel LabathPythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb) {
317f09ab08SPavel Labath  return ToSWIGHelper(value_sb.release(), SWIGTYPE_p_lldb__SBValue);
327f09ab08SPavel Labath}
337f09ab08SPavel Labath
347f09ab08SPavel LabathPythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp) {
357f09ab08SPavel Labath  return ToSWIGWrapper(std::make_unique<lldb::SBValue>(std::move(value_sp)));
367f09ab08SPavel Labath}
377f09ab08SPavel Labath
387f09ab08SPavel LabathPythonObject ToSWIGWrapper(lldb::TargetSP target_sp) {
397f09ab08SPavel Labath  return ToSWIGHelper(new lldb::SBTarget(std::move(target_sp)),
407f09ab08SPavel Labath                      SWIGTYPE_p_lldb__SBTarget);
417f09ab08SPavel Labath}
427f09ab08SPavel Labath
437f09ab08SPavel LabathPythonObject ToSWIGWrapper(lldb::ProcessSP process_sp) {
447f09ab08SPavel Labath  return ToSWIGHelper(new lldb::SBProcess(std::move(process_sp)),
457f09ab08SPavel Labath                      SWIGTYPE_p_lldb__SBProcess);
467f09ab08SPavel Labath}
477f09ab08SPavel Labath
487f09ab08SPavel LabathPythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp) {
497f09ab08SPavel Labath  return ToSWIGHelper(new lldb::SBThreadPlan(std::move(thread_plan_sp)),
507f09ab08SPavel Labath                      SWIGTYPE_p_lldb__SBThreadPlan);
517f09ab08SPavel Labath}
527f09ab08SPavel Labath
537f09ab08SPavel LabathPythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp) {
547f09ab08SPavel Labath  return ToSWIGHelper(new lldb::SBBreakpoint(std::move(breakpoint_sp)),
557f09ab08SPavel Labath                      SWIGTYPE_p_lldb__SBBreakpoint);
567f09ab08SPavel Labath}
577f09ab08SPavel Labath
582efc6892SPavel LabathPythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStream> stream_sb) {
592efc6892SPavel Labath  return ToSWIGHelper(stream_sb.release(), SWIGTYPE_p_lldb__SBStream);
602efc6892SPavel Labath}
612efc6892SPavel Labath
62ebb6bb72SPavel LabathPythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb) {
63ebb6bb72SPavel Labath  return ToSWIGHelper(data_sb.release(), SWIGTYPE_p_lldb__SBStructuredData);
64ebb6bb72SPavel Labath}
65ebb6bb72SPavel Labath
66ebb6bb72SPavel LabathPythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl) {
67ebb6bb72SPavel Labath  return ToSWIGWrapper(std::make_unique<lldb::SBStructuredData>(data_impl));
68ebb6bb72SPavel Labath}
69ebb6bb72SPavel Labath
707406d236SPavel LabathPythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp) {
717406d236SPavel Labath  return ToSWIGHelper(new lldb::SBThread(std::move(thread_sp)),
727406d236SPavel Labath                      SWIGTYPE_p_lldb__SBThread);
737406d236SPavel Labath}
747406d236SPavel Labath
757406d236SPavel LabathPythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp) {
767406d236SPavel Labath  return ToSWIGHelper(new lldb::SBFrame(std::move(frame_sp)),
777406d236SPavel Labath                      SWIGTYPE_p_lldb__SBFrame);
787406d236SPavel Labath}
797406d236SPavel Labath
807406d236SPavel LabathPythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp) {
817406d236SPavel Labath  return ToSWIGHelper(new lldb::SBDebugger(std::move(debugger_sp)),
827406d236SPavel Labath                      SWIGTYPE_p_lldb__SBDebugger);
837406d236SPavel Labath}
847406d236SPavel Labath
852efc6892SPavel LabathPythonObject ToSWIGWrapper(lldb::WatchpointSP watchpoint_sp) {
862efc6892SPavel Labath  return ToSWIGHelper(new lldb::SBWatchpoint(std::move(watchpoint_sp)),
872efc6892SPavel Labath                      SWIGTYPE_p_lldb__SBWatchpoint);
882efc6892SPavel Labath}
892efc6892SPavel Labath
902efc6892SPavel LabathPythonObject ToSWIGWrapper(lldb::BreakpointLocationSP bp_loc_sp) {
912efc6892SPavel Labath  return ToSWIGHelper(new lldb::SBBreakpointLocation(std::move(bp_loc_sp)),
922efc6892SPavel Labath                      SWIGTYPE_p_lldb__SBBreakpointLocation);
932efc6892SPavel Labath}
942efc6892SPavel Labath
952efc6892SPavel LabathPythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp) {
962efc6892SPavel Labath  return ToSWIGHelper(new lldb::SBExecutionContext(std::move(ctx_sp)),
972efc6892SPavel Labath                      SWIGTYPE_p_lldb__SBExecutionContext);
982efc6892SPavel Labath}
992efc6892SPavel Labath
1002efc6892SPavel LabathPythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options) {
1012efc6892SPavel Labath  return ToSWIGHelper(new lldb::SBTypeSummaryOptions(summary_options),
1022efc6892SPavel Labath                      SWIGTYPE_p_lldb__SBTypeSummaryOptions);
1032efc6892SPavel Labath}
1042efc6892SPavel Labath
1052efc6892SPavel LabathPythonObject ToSWIGWrapper(const SymbolContext &sym_ctx) {
1062efc6892SPavel Labath  return ToSWIGHelper(new lldb::SBSymbolContext(sym_ctx),
1072efc6892SPavel Labath                      SWIGTYPE_p_lldb__SBSymbolContext);
1082efc6892SPavel Labath}
1092efc6892SPavel Labath
110*0a07c966SPavel LabathScopedPythonObject<lldb::SBCommandReturnObject>
111*0a07c966SPavel LabathToSWIGWrapper(CommandReturnObject &cmd_retobj) {
112*0a07c966SPavel Labath  return ScopedPythonObject<lldb::SBCommandReturnObject>(
113*0a07c966SPavel Labath      new lldb::SBCommandReturnObject(cmd_retobj),
114*0a07c966SPavel Labath      SWIGTYPE_p_lldb__SBCommandReturnObject);
115*0a07c966SPavel Labath}
116*0a07c966SPavel Labath
117*0a07c966SPavel LabathScopedPythonObject<lldb::SBEvent> ToSWIGWrapper(Event *event) {
118*0a07c966SPavel Labath  return ScopedPythonObject<lldb::SBEvent>(new lldb::SBEvent(event),
119*0a07c966SPavel Labath                                           SWIGTYPE_p_lldb__SBEvent);
120*0a07c966SPavel Labath}
121*0a07c966SPavel Labath
1227f09ab08SPavel Labath} // namespace python
1237f09ab08SPavel Labath} // namespace lldb_private
124