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