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