xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===-- PythonDataObjects.h--------------------------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
10*0b57cec5SDimitry Andric #define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
11*0b57cec5SDimitry Andric 
12*0b57cec5SDimitry Andric #ifndef LLDB_DISABLE_PYTHON
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric // LLDB Python header must be included first
15*0b57cec5SDimitry Andric #include "lldb-python.h"
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric #include "lldb/Host/File.h"
18*0b57cec5SDimitry Andric #include "lldb/Utility/StructuredData.h"
19*0b57cec5SDimitry Andric 
20*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric namespace lldb_private {
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric class PythonBytes;
25*0b57cec5SDimitry Andric class PythonString;
26*0b57cec5SDimitry Andric class PythonList;
27*0b57cec5SDimitry Andric class PythonDictionary;
28*0b57cec5SDimitry Andric class PythonInteger;
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric class StructuredPythonObject : public StructuredData::Generic {
31*0b57cec5SDimitry Andric public:
32*0b57cec5SDimitry Andric   StructuredPythonObject() : StructuredData::Generic() {}
33*0b57cec5SDimitry Andric 
34*0b57cec5SDimitry Andric   StructuredPythonObject(void *obj) : StructuredData::Generic(obj) {
35*0b57cec5SDimitry Andric     Py_XINCREF(GetValue());
36*0b57cec5SDimitry Andric   }
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric   ~StructuredPythonObject() override {
39*0b57cec5SDimitry Andric     if (Py_IsInitialized())
40*0b57cec5SDimitry Andric       Py_XDECREF(GetValue());
41*0b57cec5SDimitry Andric     SetValue(nullptr);
42*0b57cec5SDimitry Andric   }
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric   bool IsValid() const override { return GetValue() && GetValue() != Py_None; }
45*0b57cec5SDimitry Andric 
46*0b57cec5SDimitry Andric   void Dump(Stream &s, bool pretty_print = true) const override;
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric private:
49*0b57cec5SDimitry Andric   DISALLOW_COPY_AND_ASSIGN(StructuredPythonObject);
50*0b57cec5SDimitry Andric };
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric enum class PyObjectType {
53*0b57cec5SDimitry Andric   Unknown,
54*0b57cec5SDimitry Andric   None,
55*0b57cec5SDimitry Andric   Boolean,
56*0b57cec5SDimitry Andric   Integer,
57*0b57cec5SDimitry Andric   Dictionary,
58*0b57cec5SDimitry Andric   List,
59*0b57cec5SDimitry Andric   String,
60*0b57cec5SDimitry Andric   Bytes,
61*0b57cec5SDimitry Andric   ByteArray,
62*0b57cec5SDimitry Andric   Module,
63*0b57cec5SDimitry Andric   Callable,
64*0b57cec5SDimitry Andric   Tuple,
65*0b57cec5SDimitry Andric   File
66*0b57cec5SDimitry Andric };
67*0b57cec5SDimitry Andric 
68*0b57cec5SDimitry Andric enum class PyRefType {
69*0b57cec5SDimitry Andric   Borrowed, // We are not given ownership of the incoming PyObject.
70*0b57cec5SDimitry Andric             // We cannot safely hold it without calling Py_INCREF.
71*0b57cec5SDimitry Andric   Owned     // We have ownership of the incoming PyObject.  We should
72*0b57cec5SDimitry Andric             // not call Py_INCREF.
73*0b57cec5SDimitry Andric };
74*0b57cec5SDimitry Andric 
75*0b57cec5SDimitry Andric enum class PyInitialValue { Invalid, Empty };
76*0b57cec5SDimitry Andric 
77*0b57cec5SDimitry Andric class PythonObject {
78*0b57cec5SDimitry Andric public:
79*0b57cec5SDimitry Andric   PythonObject() : m_py_obj(nullptr) {}
80*0b57cec5SDimitry Andric 
81*0b57cec5SDimitry Andric   PythonObject(PyRefType type, PyObject *py_obj) : m_py_obj(nullptr) {
82*0b57cec5SDimitry Andric     Reset(type, py_obj);
83*0b57cec5SDimitry Andric   }
84*0b57cec5SDimitry Andric 
85*0b57cec5SDimitry Andric   PythonObject(const PythonObject &rhs) : m_py_obj(nullptr) { Reset(rhs); }
86*0b57cec5SDimitry Andric 
87*0b57cec5SDimitry Andric   virtual ~PythonObject() { Reset(); }
88*0b57cec5SDimitry Andric 
89*0b57cec5SDimitry Andric   void Reset() {
90*0b57cec5SDimitry Andric     // Avoid calling the virtual method since it's not necessary
91*0b57cec5SDimitry Andric     // to actually validate the type of the PyObject if we're
92*0b57cec5SDimitry Andric     // just setting to null.
93*0b57cec5SDimitry Andric     if (Py_IsInitialized())
94*0b57cec5SDimitry Andric       Py_XDECREF(m_py_obj);
95*0b57cec5SDimitry Andric     m_py_obj = nullptr;
96*0b57cec5SDimitry Andric   }
97*0b57cec5SDimitry Andric 
98*0b57cec5SDimitry Andric   void Reset(const PythonObject &rhs) {
99*0b57cec5SDimitry Andric     // Avoid calling the virtual method if it's not necessary
100*0b57cec5SDimitry Andric     // to actually validate the type of the PyObject.
101*0b57cec5SDimitry Andric     if (!rhs.IsValid())
102*0b57cec5SDimitry Andric       Reset();
103*0b57cec5SDimitry Andric     else
104*0b57cec5SDimitry Andric       Reset(PyRefType::Borrowed, rhs.m_py_obj);
105*0b57cec5SDimitry Andric   }
106*0b57cec5SDimitry Andric 
107*0b57cec5SDimitry Andric   // PythonObject is implicitly convertible to PyObject *, which will call the
108*0b57cec5SDimitry Andric   // wrong overload.  We want to explicitly disallow this, since a PyObject
109*0b57cec5SDimitry Andric   // *always* owns its reference.  Therefore the overload which takes a
110*0b57cec5SDimitry Andric   // PyRefType doesn't make sense, and the copy constructor should be used.
111*0b57cec5SDimitry Andric   void Reset(PyRefType type, const PythonObject &ref) = delete;
112*0b57cec5SDimitry Andric 
113*0b57cec5SDimitry Andric   virtual void Reset(PyRefType type, PyObject *py_obj) {
114*0b57cec5SDimitry Andric     if (py_obj == m_py_obj)
115*0b57cec5SDimitry Andric       return;
116*0b57cec5SDimitry Andric 
117*0b57cec5SDimitry Andric     if (Py_IsInitialized())
118*0b57cec5SDimitry Andric       Py_XDECREF(m_py_obj);
119*0b57cec5SDimitry Andric 
120*0b57cec5SDimitry Andric     m_py_obj = py_obj;
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric     // If this is a borrowed reference, we need to convert it to
123*0b57cec5SDimitry Andric     // an owned reference by incrementing it.  If it is an owned
124*0b57cec5SDimitry Andric     // reference (for example the caller allocated it with PyDict_New()
125*0b57cec5SDimitry Andric     // then we must *not* increment it.
126*0b57cec5SDimitry Andric     if (Py_IsInitialized() && type == PyRefType::Borrowed)
127*0b57cec5SDimitry Andric       Py_XINCREF(m_py_obj);
128*0b57cec5SDimitry Andric   }
129*0b57cec5SDimitry Andric 
130*0b57cec5SDimitry Andric   void Dump() const {
131*0b57cec5SDimitry Andric     if (m_py_obj)
132*0b57cec5SDimitry Andric       _PyObject_Dump(m_py_obj);
133*0b57cec5SDimitry Andric     else
134*0b57cec5SDimitry Andric       puts("NULL");
135*0b57cec5SDimitry Andric   }
136*0b57cec5SDimitry Andric 
137*0b57cec5SDimitry Andric   void Dump(Stream &strm) const;
138*0b57cec5SDimitry Andric 
139*0b57cec5SDimitry Andric   PyObject *get() const { return m_py_obj; }
140*0b57cec5SDimitry Andric 
141*0b57cec5SDimitry Andric   PyObject *release() {
142*0b57cec5SDimitry Andric     PyObject *result = m_py_obj;
143*0b57cec5SDimitry Andric     m_py_obj = nullptr;
144*0b57cec5SDimitry Andric     return result;
145*0b57cec5SDimitry Andric   }
146*0b57cec5SDimitry Andric 
147*0b57cec5SDimitry Andric   PythonObject &operator=(const PythonObject &other) {
148*0b57cec5SDimitry Andric     Reset(PyRefType::Borrowed, other.get());
149*0b57cec5SDimitry Andric     return *this;
150*0b57cec5SDimitry Andric   }
151*0b57cec5SDimitry Andric 
152*0b57cec5SDimitry Andric   PyObjectType GetObjectType() const;
153*0b57cec5SDimitry Andric 
154*0b57cec5SDimitry Andric   PythonString Repr() const;
155*0b57cec5SDimitry Andric 
156*0b57cec5SDimitry Andric   PythonString Str() const;
157*0b57cec5SDimitry Andric 
158*0b57cec5SDimitry Andric   static PythonObject ResolveNameWithDictionary(llvm::StringRef name,
159*0b57cec5SDimitry Andric                                                 const PythonDictionary &dict);
160*0b57cec5SDimitry Andric 
161*0b57cec5SDimitry Andric   template <typename T>
162*0b57cec5SDimitry Andric   static T ResolveNameWithDictionary(llvm::StringRef name,
163*0b57cec5SDimitry Andric                                      const PythonDictionary &dict) {
164*0b57cec5SDimitry Andric     return ResolveNameWithDictionary(name, dict).AsType<T>();
165*0b57cec5SDimitry Andric   }
166*0b57cec5SDimitry Andric 
167*0b57cec5SDimitry Andric   PythonObject ResolveName(llvm::StringRef name) const;
168*0b57cec5SDimitry Andric 
169*0b57cec5SDimitry Andric   template <typename T> T ResolveName(llvm::StringRef name) const {
170*0b57cec5SDimitry Andric     return ResolveName(name).AsType<T>();
171*0b57cec5SDimitry Andric   }
172*0b57cec5SDimitry Andric 
173*0b57cec5SDimitry Andric   bool HasAttribute(llvm::StringRef attribute) const;
174*0b57cec5SDimitry Andric 
175*0b57cec5SDimitry Andric   PythonObject GetAttributeValue(llvm::StringRef attribute) const;
176*0b57cec5SDimitry Andric 
177*0b57cec5SDimitry Andric   bool IsValid() const;
178*0b57cec5SDimitry Andric 
179*0b57cec5SDimitry Andric   bool IsAllocated() const;
180*0b57cec5SDimitry Andric 
181*0b57cec5SDimitry Andric   bool IsNone() const;
182*0b57cec5SDimitry Andric 
183*0b57cec5SDimitry Andric   template <typename T> T AsType() const {
184*0b57cec5SDimitry Andric     if (!T::Check(m_py_obj))
185*0b57cec5SDimitry Andric       return T();
186*0b57cec5SDimitry Andric     return T(PyRefType::Borrowed, m_py_obj);
187*0b57cec5SDimitry Andric   }
188*0b57cec5SDimitry Andric 
189*0b57cec5SDimitry Andric   StructuredData::ObjectSP CreateStructuredObject() const;
190*0b57cec5SDimitry Andric 
191*0b57cec5SDimitry Andric protected:
192*0b57cec5SDimitry Andric   PyObject *m_py_obj;
193*0b57cec5SDimitry Andric };
194*0b57cec5SDimitry Andric 
195*0b57cec5SDimitry Andric class PythonBytes : public PythonObject {
196*0b57cec5SDimitry Andric public:
197*0b57cec5SDimitry Andric   PythonBytes();
198*0b57cec5SDimitry Andric   explicit PythonBytes(llvm::ArrayRef<uint8_t> bytes);
199*0b57cec5SDimitry Andric   PythonBytes(const uint8_t *bytes, size_t length);
200*0b57cec5SDimitry Andric   PythonBytes(PyRefType type, PyObject *o);
201*0b57cec5SDimitry Andric 
202*0b57cec5SDimitry Andric   ~PythonBytes() override;
203*0b57cec5SDimitry Andric 
204*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
205*0b57cec5SDimitry Andric 
206*0b57cec5SDimitry Andric   // Bring in the no-argument base class version
207*0b57cec5SDimitry Andric   using PythonObject::Reset;
208*0b57cec5SDimitry Andric 
209*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
210*0b57cec5SDimitry Andric 
211*0b57cec5SDimitry Andric   llvm::ArrayRef<uint8_t> GetBytes() const;
212*0b57cec5SDimitry Andric 
213*0b57cec5SDimitry Andric   size_t GetSize() const;
214*0b57cec5SDimitry Andric 
215*0b57cec5SDimitry Andric   void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
216*0b57cec5SDimitry Andric 
217*0b57cec5SDimitry Andric   StructuredData::StringSP CreateStructuredString() const;
218*0b57cec5SDimitry Andric };
219*0b57cec5SDimitry Andric 
220*0b57cec5SDimitry Andric class PythonByteArray : public PythonObject {
221*0b57cec5SDimitry Andric public:
222*0b57cec5SDimitry Andric   PythonByteArray();
223*0b57cec5SDimitry Andric   explicit PythonByteArray(llvm::ArrayRef<uint8_t> bytes);
224*0b57cec5SDimitry Andric   PythonByteArray(const uint8_t *bytes, size_t length);
225*0b57cec5SDimitry Andric   PythonByteArray(PyRefType type, PyObject *o);
226*0b57cec5SDimitry Andric   PythonByteArray(const PythonBytes &object);
227*0b57cec5SDimitry Andric 
228*0b57cec5SDimitry Andric   ~PythonByteArray() override;
229*0b57cec5SDimitry Andric 
230*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
231*0b57cec5SDimitry Andric 
232*0b57cec5SDimitry Andric   // Bring in the no-argument base class version
233*0b57cec5SDimitry Andric   using PythonObject::Reset;
234*0b57cec5SDimitry Andric 
235*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
236*0b57cec5SDimitry Andric 
237*0b57cec5SDimitry Andric   llvm::ArrayRef<uint8_t> GetBytes() const;
238*0b57cec5SDimitry Andric 
239*0b57cec5SDimitry Andric   size_t GetSize() const;
240*0b57cec5SDimitry Andric 
241*0b57cec5SDimitry Andric   void SetBytes(llvm::ArrayRef<uint8_t> stringbytes);
242*0b57cec5SDimitry Andric 
243*0b57cec5SDimitry Andric   StructuredData::StringSP CreateStructuredString() const;
244*0b57cec5SDimitry Andric };
245*0b57cec5SDimitry Andric 
246*0b57cec5SDimitry Andric class PythonString : public PythonObject {
247*0b57cec5SDimitry Andric public:
248*0b57cec5SDimitry Andric   PythonString();
249*0b57cec5SDimitry Andric   explicit PythonString(llvm::StringRef string);
250*0b57cec5SDimitry Andric   explicit PythonString(const char *string);
251*0b57cec5SDimitry Andric   PythonString(PyRefType type, PyObject *o);
252*0b57cec5SDimitry Andric 
253*0b57cec5SDimitry Andric   ~PythonString() override;
254*0b57cec5SDimitry Andric 
255*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
256*0b57cec5SDimitry Andric 
257*0b57cec5SDimitry Andric   // Bring in the no-argument base class version
258*0b57cec5SDimitry Andric   using PythonObject::Reset;
259*0b57cec5SDimitry Andric 
260*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
261*0b57cec5SDimitry Andric 
262*0b57cec5SDimitry Andric   llvm::StringRef GetString() const;
263*0b57cec5SDimitry Andric 
264*0b57cec5SDimitry Andric   size_t GetSize() const;
265*0b57cec5SDimitry Andric 
266*0b57cec5SDimitry Andric   void SetString(llvm::StringRef string);
267*0b57cec5SDimitry Andric 
268*0b57cec5SDimitry Andric   StructuredData::StringSP CreateStructuredString() const;
269*0b57cec5SDimitry Andric };
270*0b57cec5SDimitry Andric 
271*0b57cec5SDimitry Andric class PythonInteger : public PythonObject {
272*0b57cec5SDimitry Andric public:
273*0b57cec5SDimitry Andric   PythonInteger();
274*0b57cec5SDimitry Andric   explicit PythonInteger(int64_t value);
275*0b57cec5SDimitry Andric   PythonInteger(PyRefType type, PyObject *o);
276*0b57cec5SDimitry Andric 
277*0b57cec5SDimitry Andric   ~PythonInteger() override;
278*0b57cec5SDimitry Andric 
279*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
280*0b57cec5SDimitry Andric 
281*0b57cec5SDimitry Andric   // Bring in the no-argument base class version
282*0b57cec5SDimitry Andric   using PythonObject::Reset;
283*0b57cec5SDimitry Andric 
284*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
285*0b57cec5SDimitry Andric 
286*0b57cec5SDimitry Andric   int64_t GetInteger() const;
287*0b57cec5SDimitry Andric 
288*0b57cec5SDimitry Andric   void SetInteger(int64_t value);
289*0b57cec5SDimitry Andric 
290*0b57cec5SDimitry Andric   StructuredData::IntegerSP CreateStructuredInteger() const;
291*0b57cec5SDimitry Andric };
292*0b57cec5SDimitry Andric 
293*0b57cec5SDimitry Andric class PythonBoolean : public PythonObject {
294*0b57cec5SDimitry Andric public:
295*0b57cec5SDimitry Andric   PythonBoolean() = default;
296*0b57cec5SDimitry Andric   explicit PythonBoolean(bool value);
297*0b57cec5SDimitry Andric   PythonBoolean(PyRefType type, PyObject *o);
298*0b57cec5SDimitry Andric 
299*0b57cec5SDimitry Andric   ~PythonBoolean() override = default;
300*0b57cec5SDimitry Andric 
301*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
302*0b57cec5SDimitry Andric 
303*0b57cec5SDimitry Andric   // Bring in the no-argument base class version
304*0b57cec5SDimitry Andric   using PythonObject::Reset;
305*0b57cec5SDimitry Andric 
306*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
307*0b57cec5SDimitry Andric 
308*0b57cec5SDimitry Andric   bool GetValue() const;
309*0b57cec5SDimitry Andric 
310*0b57cec5SDimitry Andric   void SetValue(bool value);
311*0b57cec5SDimitry Andric 
312*0b57cec5SDimitry Andric   StructuredData::BooleanSP CreateStructuredBoolean() const;
313*0b57cec5SDimitry Andric };
314*0b57cec5SDimitry Andric 
315*0b57cec5SDimitry Andric class PythonList : public PythonObject {
316*0b57cec5SDimitry Andric public:
317*0b57cec5SDimitry Andric   PythonList() {}
318*0b57cec5SDimitry Andric   explicit PythonList(PyInitialValue value);
319*0b57cec5SDimitry Andric   explicit PythonList(int list_size);
320*0b57cec5SDimitry Andric   PythonList(PyRefType type, PyObject *o);
321*0b57cec5SDimitry Andric 
322*0b57cec5SDimitry Andric   ~PythonList() override;
323*0b57cec5SDimitry Andric 
324*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
325*0b57cec5SDimitry Andric 
326*0b57cec5SDimitry Andric   // Bring in the no-argument base class version
327*0b57cec5SDimitry Andric   using PythonObject::Reset;
328*0b57cec5SDimitry Andric 
329*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
330*0b57cec5SDimitry Andric 
331*0b57cec5SDimitry Andric   uint32_t GetSize() const;
332*0b57cec5SDimitry Andric 
333*0b57cec5SDimitry Andric   PythonObject GetItemAtIndex(uint32_t index) const;
334*0b57cec5SDimitry Andric 
335*0b57cec5SDimitry Andric   void SetItemAtIndex(uint32_t index, const PythonObject &object);
336*0b57cec5SDimitry Andric 
337*0b57cec5SDimitry Andric   void AppendItem(const PythonObject &object);
338*0b57cec5SDimitry Andric 
339*0b57cec5SDimitry Andric   StructuredData::ArraySP CreateStructuredArray() const;
340*0b57cec5SDimitry Andric };
341*0b57cec5SDimitry Andric 
342*0b57cec5SDimitry Andric class PythonTuple : public PythonObject {
343*0b57cec5SDimitry Andric public:
344*0b57cec5SDimitry Andric   PythonTuple() {}
345*0b57cec5SDimitry Andric   explicit PythonTuple(PyInitialValue value);
346*0b57cec5SDimitry Andric   explicit PythonTuple(int tuple_size);
347*0b57cec5SDimitry Andric   PythonTuple(PyRefType type, PyObject *o);
348*0b57cec5SDimitry Andric   PythonTuple(std::initializer_list<PythonObject> objects);
349*0b57cec5SDimitry Andric   PythonTuple(std::initializer_list<PyObject *> objects);
350*0b57cec5SDimitry Andric 
351*0b57cec5SDimitry Andric   ~PythonTuple() override;
352*0b57cec5SDimitry Andric 
353*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
354*0b57cec5SDimitry Andric 
355*0b57cec5SDimitry Andric   // Bring in the no-argument base class version
356*0b57cec5SDimitry Andric   using PythonObject::Reset;
357*0b57cec5SDimitry Andric 
358*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
359*0b57cec5SDimitry Andric 
360*0b57cec5SDimitry Andric   uint32_t GetSize() const;
361*0b57cec5SDimitry Andric 
362*0b57cec5SDimitry Andric   PythonObject GetItemAtIndex(uint32_t index) const;
363*0b57cec5SDimitry Andric 
364*0b57cec5SDimitry Andric   void SetItemAtIndex(uint32_t index, const PythonObject &object);
365*0b57cec5SDimitry Andric 
366*0b57cec5SDimitry Andric   StructuredData::ArraySP CreateStructuredArray() const;
367*0b57cec5SDimitry Andric };
368*0b57cec5SDimitry Andric 
369*0b57cec5SDimitry Andric class PythonDictionary : public PythonObject {
370*0b57cec5SDimitry Andric public:
371*0b57cec5SDimitry Andric   PythonDictionary() {}
372*0b57cec5SDimitry Andric   explicit PythonDictionary(PyInitialValue value);
373*0b57cec5SDimitry Andric   PythonDictionary(PyRefType type, PyObject *o);
374*0b57cec5SDimitry Andric 
375*0b57cec5SDimitry Andric   ~PythonDictionary() override;
376*0b57cec5SDimitry Andric 
377*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
378*0b57cec5SDimitry Andric 
379*0b57cec5SDimitry Andric   // Bring in the no-argument base class version
380*0b57cec5SDimitry Andric   using PythonObject::Reset;
381*0b57cec5SDimitry Andric 
382*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
383*0b57cec5SDimitry Andric 
384*0b57cec5SDimitry Andric   uint32_t GetSize() const;
385*0b57cec5SDimitry Andric 
386*0b57cec5SDimitry Andric   PythonList GetKeys() const;
387*0b57cec5SDimitry Andric 
388*0b57cec5SDimitry Andric   PythonObject GetItemForKey(const PythonObject &key) const;
389*0b57cec5SDimitry Andric   void SetItemForKey(const PythonObject &key, const PythonObject &value);
390*0b57cec5SDimitry Andric 
391*0b57cec5SDimitry Andric   StructuredData::DictionarySP CreateStructuredDictionary() const;
392*0b57cec5SDimitry Andric };
393*0b57cec5SDimitry Andric 
394*0b57cec5SDimitry Andric class PythonModule : public PythonObject {
395*0b57cec5SDimitry Andric public:
396*0b57cec5SDimitry Andric   PythonModule();
397*0b57cec5SDimitry Andric   PythonModule(PyRefType type, PyObject *o);
398*0b57cec5SDimitry Andric 
399*0b57cec5SDimitry Andric   ~PythonModule() override;
400*0b57cec5SDimitry Andric 
401*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
402*0b57cec5SDimitry Andric 
403*0b57cec5SDimitry Andric   static PythonModule BuiltinsModule();
404*0b57cec5SDimitry Andric 
405*0b57cec5SDimitry Andric   static PythonModule MainModule();
406*0b57cec5SDimitry Andric 
407*0b57cec5SDimitry Andric   static PythonModule AddModule(llvm::StringRef module);
408*0b57cec5SDimitry Andric 
409*0b57cec5SDimitry Andric   static PythonModule ImportModule(llvm::StringRef module);
410*0b57cec5SDimitry Andric 
411*0b57cec5SDimitry Andric   // Bring in the no-argument base class version
412*0b57cec5SDimitry Andric   using PythonObject::Reset;
413*0b57cec5SDimitry Andric 
414*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
415*0b57cec5SDimitry Andric 
416*0b57cec5SDimitry Andric   PythonDictionary GetDictionary() const;
417*0b57cec5SDimitry Andric };
418*0b57cec5SDimitry Andric 
419*0b57cec5SDimitry Andric class PythonCallable : public PythonObject {
420*0b57cec5SDimitry Andric public:
421*0b57cec5SDimitry Andric   struct ArgInfo {
422*0b57cec5SDimitry Andric     size_t count;
423*0b57cec5SDimitry Andric     bool is_bound_method : 1;
424*0b57cec5SDimitry Andric     bool has_varargs : 1;
425*0b57cec5SDimitry Andric     bool has_kwargs : 1;
426*0b57cec5SDimitry Andric   };
427*0b57cec5SDimitry Andric 
428*0b57cec5SDimitry Andric   PythonCallable();
429*0b57cec5SDimitry Andric   PythonCallable(PyRefType type, PyObject *o);
430*0b57cec5SDimitry Andric 
431*0b57cec5SDimitry Andric   ~PythonCallable() override;
432*0b57cec5SDimitry Andric 
433*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
434*0b57cec5SDimitry Andric 
435*0b57cec5SDimitry Andric   // Bring in the no-argument base class version
436*0b57cec5SDimitry Andric   using PythonObject::Reset;
437*0b57cec5SDimitry Andric 
438*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
439*0b57cec5SDimitry Andric 
440*0b57cec5SDimitry Andric   ArgInfo GetNumArguments() const;
441*0b57cec5SDimitry Andric 
442*0b57cec5SDimitry Andric   PythonObject operator()();
443*0b57cec5SDimitry Andric 
444*0b57cec5SDimitry Andric   PythonObject operator()(std::initializer_list<PyObject *> args);
445*0b57cec5SDimitry Andric 
446*0b57cec5SDimitry Andric   PythonObject operator()(std::initializer_list<PythonObject> args);
447*0b57cec5SDimitry Andric 
448*0b57cec5SDimitry Andric   template <typename Arg, typename... Args>
449*0b57cec5SDimitry Andric   PythonObject operator()(const Arg &arg, Args... args) {
450*0b57cec5SDimitry Andric     return operator()({arg, args...});
451*0b57cec5SDimitry Andric   }
452*0b57cec5SDimitry Andric };
453*0b57cec5SDimitry Andric 
454*0b57cec5SDimitry Andric class PythonFile : public PythonObject {
455*0b57cec5SDimitry Andric public:
456*0b57cec5SDimitry Andric   PythonFile();
457*0b57cec5SDimitry Andric   PythonFile(File &file, const char *mode);
458*0b57cec5SDimitry Andric   PythonFile(const char *path, const char *mode);
459*0b57cec5SDimitry Andric   PythonFile(PyRefType type, PyObject *o);
460*0b57cec5SDimitry Andric 
461*0b57cec5SDimitry Andric   ~PythonFile() override;
462*0b57cec5SDimitry Andric 
463*0b57cec5SDimitry Andric   static bool Check(PyObject *py_obj);
464*0b57cec5SDimitry Andric 
465*0b57cec5SDimitry Andric   using PythonObject::Reset;
466*0b57cec5SDimitry Andric 
467*0b57cec5SDimitry Andric   void Reset(PyRefType type, PyObject *py_obj) override;
468*0b57cec5SDimitry Andric   void Reset(File &file, const char *mode);
469*0b57cec5SDimitry Andric 
470*0b57cec5SDimitry Andric   static uint32_t GetOptionsFromMode(llvm::StringRef mode);
471*0b57cec5SDimitry Andric 
472*0b57cec5SDimitry Andric   bool GetUnderlyingFile(File &file) const;
473*0b57cec5SDimitry Andric };
474*0b57cec5SDimitry Andric 
475*0b57cec5SDimitry Andric } // namespace lldb_private
476*0b57cec5SDimitry Andric 
477*0b57cec5SDimitry Andric #endif
478*0b57cec5SDimitry Andric 
479*0b57cec5SDimitry Andric #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
480