xref: /llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h (revision 22c8efcd3446c59415e2c65ae230668b0563c714)
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 // C Includes
14 // C++ Includes
15 
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/lldb-defines.h"
19 #include "lldb/Core/ConstString.h"
20 #include "lldb/Core/StructuredData.h"
21 #include "lldb/Core/Flags.h"
22 #include "lldb/Interpreter/OptionValue.h"
23 
24 namespace lldb_private {
25 class PythonString;
26 class PythonList;
27 class PythonDictionary;
28 class PythonObject;
29 class PythonInteger;
30 
31 class StructuredPythonObject : public StructuredData::Generic
32 {
33   public:
34     StructuredPythonObject()
35         : StructuredData::Generic()
36     {
37     }
38 
39     StructuredPythonObject(void *obj)
40         : StructuredData::Generic(obj)
41     {
42         Py_XINCREF(GetValue());
43     }
44 
45     virtual ~StructuredPythonObject()
46     {
47         if (Py_IsInitialized())
48             Py_XDECREF(GetValue());
49         SetValue(nullptr);
50     }
51 
52     bool
53     IsValid() const override
54     {
55         return GetValue() && GetValue() != Py_None;
56     }
57 
58     void Dump(Stream &s) const override;
59 
60   private:
61     DISALLOW_COPY_AND_ASSIGN(StructuredPythonObject);
62 };
63 
64 enum class PyObjectType
65 {
66     Unknown,
67     None,
68     Integer,
69     Dictionary,
70     List,
71     String
72 };
73 
74     class PythonObject
75     {
76     public:
77         PythonObject () :
78             m_py_obj(NULL)
79         {
80         }
81 
82         explicit PythonObject (PyObject* py_obj) :
83             m_py_obj(NULL)
84         {
85             Reset (py_obj);
86         }
87 
88         PythonObject (const PythonObject &rhs) :
89             m_py_obj(NULL)
90         {
91             Reset (rhs.m_py_obj);
92         }
93 
94         virtual
95         ~PythonObject ()
96         {
97             Reset (NULL);
98         }
99 
100         bool
101         Reset (const PythonObject &object)
102         {
103             return Reset(object.get());
104         }
105 
106         virtual bool
107         Reset (PyObject* py_obj = NULL)
108         {
109             if (py_obj != m_py_obj)
110             {
111                 if (Py_IsInitialized())
112                     Py_XDECREF(m_py_obj);
113                 m_py_obj = py_obj;
114                 if (Py_IsInitialized())
115                     Py_XINCREF(m_py_obj);
116             }
117             return true;
118         }
119 
120         void
121         Dump () const
122         {
123             if (m_py_obj)
124                 _PyObject_Dump (m_py_obj);
125             else
126                 puts ("NULL");
127         }
128 
129         void
130         Dump (Stream &strm) const;
131 
132         PyObject*
133         get () const
134         {
135             return m_py_obj;
136         }
137 
138         PyObjectType GetObjectType() const;
139 
140         PythonString
141         Repr ();
142 
143         PythonString
144         Str ();
145 
146         explicit operator bool () const
147         {
148             return m_py_obj != NULL;
149         }
150 
151         bool
152         IsNULLOrNone () const;
153 
154         StructuredData::ObjectSP CreateStructuredObject() const;
155 
156     protected:
157         PyObject* m_py_obj;
158     };
159 
160     class PythonString: public PythonObject
161     {
162     public:
163         PythonString ();
164         PythonString (PyObject *o);
165         PythonString (const PythonObject &object);
166         PythonString (llvm::StringRef string);
167         PythonString (const char *string);
168         virtual ~PythonString ();
169 
170         static bool Check(PyObject *py_obj);
171 
172         virtual bool
173         Reset (PyObject* py_obj = NULL);
174 
175         llvm::StringRef
176         GetString() const;
177 
178         size_t
179         GetSize() const;
180 
181         void SetString(llvm::StringRef string);
182 
183         StructuredData::StringSP CreateStructuredString() const;
184     };
185 
186     class PythonInteger: public PythonObject
187     {
188     public:
189 
190         PythonInteger ();
191         PythonInteger (PyObject* py_obj);
192         PythonInteger (const PythonObject &object);
193         PythonInteger (int64_t value);
194         virtual ~PythonInteger ();
195 
196         static bool Check(PyObject *py_obj);
197 
198         virtual bool
199         Reset (PyObject* py_obj = NULL);
200 
201         int64_t GetInteger() const;
202 
203         void
204         SetInteger (int64_t value);
205 
206         StructuredData::IntegerSP CreateStructuredInteger() const;
207     };
208 
209     class PythonList: public PythonObject
210     {
211     public:
212       PythonList();
213         PythonList (PyObject* py_obj);
214         PythonList (const PythonObject &object);
215         virtual ~PythonList ();
216 
217         static bool Check(PyObject *py_obj);
218 
219         virtual bool
220         Reset (PyObject* py_obj = NULL);
221 
222         uint32_t GetSize() const;
223 
224         PythonObject GetItemAtIndex(uint32_t index) const;
225 
226         void
227         SetItemAtIndex (uint32_t index, const PythonObject &object);
228 
229         void
230         AppendItem (const PythonObject &object);
231 
232         StructuredData::ArraySP CreateStructuredArray() const;
233     };
234 
235     class PythonDictionary: public PythonObject
236     {
237     public:
238       PythonDictionary();
239         PythonDictionary (PyObject* object);
240         PythonDictionary (const PythonObject &object);
241         virtual ~PythonDictionary ();
242 
243         static bool Check(PyObject *py_obj);
244 
245         virtual bool
246         Reset (PyObject* object = NULL);
247 
248         uint32_t GetSize() const;
249 
250         PythonObject
251         GetItemForKey (const PythonString &key) const;
252 
253         const char *
254         GetItemForKeyAsString (const PythonString &key, const char *fail_value = NULL) const;
255 
256         int64_t
257         GetItemForKeyAsInteger (const PythonString &key, int64_t fail_value = 0) const;
258 
259         PythonObject
260         GetItemForKey (const char *key) const;
261 
262         typedef bool (*DictionaryIteratorCallback)(PythonString* key, PythonDictionary* dict);
263 
264         PythonList
265         GetKeys () const;
266 
267         PythonString
268         GetKeyAtPosition (uint32_t pos) const;
269 
270         PythonObject
271         GetValueAtPosition (uint32_t pos) const;
272 
273         void
274         SetItemForKey (const PythonString &key, PyObject *value);
275 
276         void
277         SetItemForKey (const PythonString &key, const PythonObject& value);
278 
279         StructuredData::DictionarySP CreateStructuredDictionary() const;
280     };
281 
282 } // namespace lldb_private
283 
284 #endif  // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H
285