Start constructing a real node tree.
[gnome.gobject-introspection] / giscanner / giscannermodule.c
1 /* GObject introspection: scanner
2  *
3  * Copyright (C) 2008  Johan Dahlin <johan@gnome.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25 #include "sourcescanner.h"
26 #include <Python.h>
27
28 #define NEW_CLASS(ctype, name, cname)         \
29 static PyMethodDef _Py##cname##_methods[];    \
30 PyTypeObject Py##cname##_Type = {             \
31     PyObject_HEAD_INIT(NULL)                  \
32     0,                                        \
33     "scanner." name,                          \
34     sizeof(ctype),                    \
35     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,             \
36     0, 0, 0, 0, 0, 0,                         \
37     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \
38     NULL, 0, 0, 0,                            \
39     0,        \
40     0, 0,                                     \
41     0,                                        \
42     0, 0, NULL, NULL, 0, 0,                   \
43     0             \
44 }
45
46 #define REGISTER_TYPE(d, name, type)          \
47     type.ob_type = &PyType_Type;              \
48     type.tp_alloc = PyType_GenericAlloc;      \
49     type.tp_new = PyType_GenericNew;          \
50     if (PyType_Ready (&type))                 \
51         return;                               \
52     PyDict_SetItemString (d, name, (PyObject *)&type); \
53     Py_INCREF (&type);
54
55 typedef struct {
56   PyObject_HEAD
57   GISourceType *type;
58 } PyGISourceType;
59
60 typedef struct {
61   PyObject_HEAD
62   GISourceSymbol *symbol;
63 } PyGISourceSymbol;
64
65 typedef struct {
66   PyObject_HEAD
67   GISourceScanner *scanner;
68 } PyGISourceScanner;
69
70 NEW_CLASS (PyGISourceSymbol, "SourceSymbol", GISourceSymbol);
71 NEW_CLASS (PyGISourceType, "SourceType", GISourceType);
72 NEW_CLASS (PyGISourceScanner, "SourceScanner", GISourceScanner);
73
74
75 /* Symbol */
76
77 static PyObject *
78 symbol_get_type (PyGISourceSymbol *self,
79                  void             *context)
80 {
81   return PyInt_FromLong (self->symbol->type);
82 }
83
84 static PyObject *
85 symbol_get_ident (PyGISourceSymbol *self,
86                   void            *context)
87 {
88   return PyString_FromString (self->symbol->ident);
89 }
90
91 static PyObject *
92 symbol_get_base_type (PyGISourceSymbol *self,
93                       void             *context)
94 {
95   PyGISourceType *item;
96   item = (PyGISourceType *)PyObject_New (PyGISourceType,
97                                          &PyGISourceType_Type);
98   item->type = self->symbol->base_type;
99   return (PyObject*)item;
100 }
101
102 static PyObject *
103 symbol_get_const_int (PyGISourceSymbol *self,
104                       void             *context)
105 {
106   return PyInt_FromLong (self->symbol->const_int);
107 }
108
109 static PyGetSetDef _PyGISourceSymbol_getsets[] = {
110   /* int ref_count; */
111   { "type", (getter)symbol_get_type, NULL, NULL},
112   /* int id; */
113   { "ident", (getter)symbol_get_ident, NULL, NULL},
114   { "base_type", (getter)symbol_get_base_type, NULL, NULL},
115   /* gboolean const_int_set; */
116   { "const_int", (getter)symbol_get_const_int, NULL, NULL},
117   /* char *const_string; */
118   /* GSList *directives; */
119   { 0 }
120 };
121
122
123
124 /* Type */
125
126 static PyObject *
127 type_get_type (PyGISourceType *self,
128                void           *context)
129 {
130   return PyInt_FromLong (self->type->type);
131 }
132
133 static PyObject *
134 type_get_storage_class_specifier (PyGISourceType *self,
135                                   void           *context)
136 {
137   return PyInt_FromLong (self->type->storage_class_specifier);
138 }
139
140 static PyObject *
141 type_get_type_qualifier (PyGISourceType *self,
142                          void           *context)
143 {
144   return PyInt_FromLong (self->type->type_qualifier);
145 }
146
147 static PyObject *
148 type_get_function_specifier (PyGISourceType *self,
149                              void           *context)
150 {
151   return PyInt_FromLong (self->type->function_specifier);
152 }
153
154 static PyObject *
155 type_get_name (PyGISourceType *self,
156                void           *context)
157 {
158   if (!self->type->name)
159     {
160       Py_INCREF (Py_None);
161       return Py_None;
162     }
163     
164   return PyString_FromString (self->type->name);
165 }
166
167 static PyObject *
168 type_get_base_type (PyGISourceType *self,
169                     void           *context)
170 {
171   PyGISourceType *item;
172   item = (PyGISourceType *)PyObject_New (PyGISourceType,
173                                          &PyGISourceType_Type);
174   item->type = self->type->base_type;
175   return (PyObject*)item;
176 }
177
178 static PyObject *
179 type_get_child_list (PyGISourceType *self,
180                      void           *context)
181 {
182   GList *l, *symbols;
183   PyObject *list;
184   int i = 0;
185
186   if (!self->type)
187     return Py_BuildValue("[]");
188   
189   list = PyList_New (g_list_length (self->type->child_list));
190   
191   for (l = self->type->child_list; l; l = l->next)
192     {
193       PyGISourceSymbol *item;
194       item = (PyGISourceSymbol *)PyObject_New (PyGISourceSymbol,
195                                                &PyGISourceSymbol_Type);
196       item->symbol = l->data;
197       PyList_SetItem (list, i++, (PyObject*)item);
198       Py_INCREF (item);
199     }
200
201   Py_INCREF (list);
202   return list;
203 }
204
205 static PyGetSetDef _PyGISourceType_getsets[] = {
206   { "type", (getter)type_get_type, NULL, NULL},
207   { "storage_class_specifier", (getter)type_get_storage_class_specifier, NULL, NULL},
208   { "type_qualifier", (getter)type_get_type_qualifier, NULL, NULL},
209   { "function_specifier", (getter)type_get_function_specifier, NULL, NULL},
210   { "name", (getter)type_get_name, NULL, NULL},
211   { "base_type", (getter)type_get_base_type, NULL, NULL},
212   { "child_list", (getter)type_get_child_list, NULL, NULL},
213   { 0 }
214 };
215
216
217
218 /* Scanner */
219
220 static int
221 pygi_source_scanner_init (PyGISourceScanner *self,
222                           PyObject          *args,
223                           PyObject          *kwargs)
224 {
225   if (!PyArg_ParseTuple (args, ":SourceScanner.__init__"))
226     return -1;
227
228   self->scanner = gi_source_scanner_new ();
229
230   return 0;
231 }
232
233 static PyObject *
234 pygi_source_scanner_parse_file (PyGISourceScanner *self,
235                                 PyObject          *args)
236 {
237   int fd;
238   char *filename;
239   FILE *fp;
240   
241   if (!PyArg_ParseTuple (args, "is:SourceScanner.__init__", &fd, &filename))
242     return NULL;
243
244   fp = fdopen (fd, "r");
245   if (!fp)
246     {
247       PyErr_SetFromErrnoWithFilename (PyExc_OSError, filename);
248       return NULL;
249     }
250
251   self->scanner->filenames =
252     g_list_append (self->scanner->filenames, g_strdup (filename));
253   self->scanner->current_filename = g_strdup (filename);
254
255   if (!gi_source_scanner_parse_file (self->scanner, fp))
256     {
257       g_print ("Something went wrong..\n");
258       return NULL;
259     }
260
261   Py_INCREF (Py_None);
262   return Py_None;
263 }
264
265 static PyObject *
266 pygi_source_scanner_set_macro_scan (PyGISourceScanner *self,
267                                     PyObject          *args)
268 {
269   int macro_scan;
270   
271   if (!PyArg_ParseTuple (args, "b:SourceScanner.set_macro_scan", &macro_scan))
272     return NULL;
273
274   gi_source_scanner_set_macro_scan (self->scanner, macro_scan);
275
276   Py_INCREF (Py_None);
277   return Py_None;
278 }
279
280 static PyObject *
281 pygi_source_scanner_get_symbols (PyGISourceScanner *self)
282 {
283   GSList *l, *symbols;
284   PyObject *list;
285   int i = 0;
286   
287   symbols = gi_source_scanner_get_symbols (self->scanner);
288   list = PyList_New (g_slist_length (symbols));
289   
290   for (l = symbols; l; l = l->next)
291     {
292       PyGISourceSymbol *item;
293       item = (PyGISourceSymbol *)PyObject_New (PyGISourceSymbol,
294                                                &PyGISourceSymbol_Type);
295       item->symbol = l->data;
296       PyList_SetItem (list, i++, (PyObject*)item);
297       Py_INCREF (item);
298     }
299
300   Py_INCREF (list);
301   return list;
302 }
303
304 static PyMethodDef _PyGISourceScanner_methods[] = {
305   { "get_symbols", (PyCFunction) pygi_source_scanner_get_symbols, METH_NOARGS },
306   { "parse_file", (PyCFunction) pygi_source_scanner_parse_file, METH_VARARGS },
307   { "set_macro_scan", (PyCFunction) pygi_source_scanner_set_macro_scan, METH_VARARGS },
308   { NULL, NULL, 0 }
309 };
310
311
312 /* Module */
313
314 PyMethodDef pyscanner_functions[] = {
315   { NULL, NULL, 0, NULL }
316 };
317
318 DL_EXPORT(void)
319 init_giscanner(void)
320 {
321     PyObject *m, *d;
322
323     m = Py_InitModule ("giscanner._giscanner", pyscanner_functions);
324     d = PyModule_GetDict (m);
325
326     PyGISourceScanner_Type.tp_init = (initproc)pygi_source_scanner_init;
327     PyGISourceScanner_Type.tp_methods = _PyGISourceScanner_methods;
328     REGISTER_TYPE (d, "SourceScanner", PyGISourceScanner_Type);
329
330     PyGISourceSymbol_Type.tp_getset = _PyGISourceSymbol_getsets;
331     REGISTER_TYPE (d, "SourceSymbol", PyGISourceSymbol_Type);
332
333     PyGISourceType_Type.tp_getset = _PyGISourceType_getsets;
334     REGISTER_TYPE (d, "SourceType", PyGISourceType_Type);
335 }