1 /* GObject introspection: gen-introspect
3 * Copyright (C) 2007 Jürg Billeter
4 * Copyright (C) 2007 Johan Dahlin
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 * Jürg Billeter <j@bitron.ch>
35 static void node_generate (GIdlWriter
* writer
, GIdlNode
* node
);
38 g_writer_write_inline (GIdlWriter
* writer
, const char *s
)
40 fprintf (writer
->output
, "%s", s
);
44 g_writer_write (GIdlWriter
* writer
, const char *s
)
47 for (i
= 0; i
< writer
->indent
; i
++)
49 fprintf (writer
->output
, "\t");
52 g_writer_write_inline (writer
, s
);
56 g_writer_write_indent (GIdlWriter
* writer
, const char *s
)
58 g_writer_write (writer
, s
);
63 g_writer_write_unindent (GIdlWriter
* writer
, const char *s
)
66 g_writer_write (writer
, s
);
70 field_generate (GIdlWriter
* writer
, GIdlNodeField
* node
)
73 g_markup_printf_escaped ("<field name=\"%s\" type=\"%s\"/>\n",
74 node
->node
.name
, node
->type
->unparsed
);
75 g_writer_write (writer
, markup
);
80 value_generate (GIdlWriter
* writer
, GIdlNodeValue
* node
)
83 g_markup_printf_escaped ("<member name=\"%s\" value=\"%d\"/>\n",
84 node
->node
.name
, node
->value
);
85 g_writer_write (writer
, markup
);
90 constant_generate (GIdlWriter
* writer
, GIdlNodeConstant
* node
)
93 g_markup_printf_escaped
94 ("<constant name=\"%s\" type=\"%s\" value=\"%s\"/>\n", node
->node
.name
,
95 node
->type
->unparsed
, node
->value
);
96 g_writer_write (writer
, markup
);
101 property_generate (GIdlWriter
* writer
, GIdlNodeProperty
* node
)
104 g_markup_printf_escaped ("<property name=\"%s\" "
109 "construct-only=\"%s\"/>\n",
111 node
->type
->unparsed
,
112 node
->readable
? "1" : "0",
113 node
->writable
? "1" : "0",
114 node
->construct
? "1" : "0",
115 node
->construct_only
? "1" : "0");
116 g_writer_write (writer
, markup
);
121 function_generate (GIdlWriter
* writer
, GIdlNodeFunction
* node
)
123 const char *tag_name
;
127 if (node
->node
.type
== G_IDL_NODE_CALLBACK
)
128 tag_name
= "callback";
129 else if (node
->is_constructor
)
130 tag_name
= "constructor";
131 else if (node
->is_method
)
134 tag_name
= "function";
136 markup_s
= g_string_new ("<");
137 g_string_append_printf (markup_s
,
139 tag_name
, node
->node
.name
);
141 if (node
->node
.type
!= G_IDL_NODE_CALLBACK
) {
142 gchar
*tmp
= g_markup_printf_escaped (" symbol=\"%s\"", node
->symbol
);
143 markup_s
= g_string_append (markup_s
, tmp
);
147 if (node
->deprecated
)
148 markup_s
= g_string_append (markup_s
, " deprecated=\"1\"");
150 g_string_append (markup_s
, ">\n");
152 g_writer_write_indent (writer
, markup_s
->str
);
153 g_string_free (markup_s
, TRUE
);
156 g_markup_printf_escaped ("<return-type type=\"%s\"/>\n",
157 node
->result
->type
->unparsed
);
158 g_writer_write (writer
, markup
);
161 if (node
->parameters
!= NULL
)
164 g_writer_write_indent (writer
, "<parameters>\n");
165 for (l
= node
->parameters
; l
!= NULL
; l
= l
->next
)
167 GIdlNodeParam
*param
= l
->data
;
169 g_markup_printf_escaped ("<parameter name=\"%s\" type=\"%s\"/>\n",
170 param
->node
.name
, param
->type
->unparsed
);
171 g_writer_write (writer
, markup
);
174 g_writer_write_unindent (writer
, "</parameters>\n");
176 markup
= g_strdup_printf ("</%s>\n", tag_name
);
177 g_writer_write_unindent (writer
, markup
);
182 vfunc_generate (GIdlWriter
* writer
, GIdlNodeVFunc
* node
)
185 g_markup_printf_escaped ("<vfunc name=\"%s\">\n", node
->node
.name
);
186 g_writer_write_indent (writer
, markup
);
189 g_markup_printf_escaped ("<return-type type=\"%s\"/>\n",
190 node
->result
->type
->unparsed
);
191 g_writer_write (writer
, markup
);
193 if (node
->parameters
!= NULL
)
196 g_writer_write_indent (writer
, "<parameters>\n");
197 for (l
= node
->parameters
; l
!= NULL
; l
= l
->next
)
199 GIdlNodeParam
*param
= l
->data
;
201 g_markup_printf_escaped ("<parameter name=\"%s\" type=\"%s\"/>\n",
202 param
->node
.name
, param
->type
->unparsed
);
203 g_writer_write (writer
, markup
);
206 g_writer_write_unindent (writer
, "</parameters>\n");
208 g_writer_write_unindent (writer
, "</vfunc>\n");
212 signal_generate (GIdlWriter
* writer
, GIdlNodeSignal
* node
)
215 const char *when
= "LAST";
220 else if (node
->run_cleanup
)
225 g_markup_printf_escaped ("<signal name=\"%s\" when=\"%s\">\n",
226 node
->node
.name
, when
);
227 g_writer_write_indent (writer
, markup
);
230 g_markup_printf_escaped ("<return-type type=\"%s\"/>\n",
231 node
->result
->type
->unparsed
);
232 g_writer_write (writer
, markup
);
234 if (node
->parameters
!= NULL
)
237 g_writer_write_indent (writer
, "<parameters>\n");
238 for (l
= node
->parameters
; l
!= NULL
; l
= l
->next
)
240 GIdlNodeParam
*param
= l
->data
;
242 g_markup_printf_escaped ("<parameter name=\"%s\" type=\"%s\"/>\n",
243 param
->node
.name
, param
->type
->unparsed
);
244 g_writer_write (writer
, markup
);
247 g_writer_write_unindent (writer
, "</parameters>\n");
249 g_writer_write_unindent (writer
, "</signal>\n");
253 interface_generate (GIdlWriter
* writer
, GIdlNodeInterface
* node
)
257 if (node
->node
.type
== G_IDL_NODE_OBJECT
)
260 g_markup_printf_escaped ("<object name=\"%s\" "
263 "get-type=\"%s\">\n",
269 else if (node
->node
.type
== G_IDL_NODE_INTERFACE
)
272 g_markup_printf_escaped
273 ("<interface name=\"%s\" type-name=\"%s\" get-type=\"%s\">\n",
274 node
->node
.name
, node
->gtype_name
, node
->gtype_init
);
277 g_writer_write_indent (writer
, markup
);
279 if (node
->node
.type
== G_IDL_NODE_OBJECT
&& node
->interfaces
!= NULL
)
282 g_writer_write_indent (writer
, "<implements>\n");
283 for (l
= node
->interfaces
; l
!= NULL
; l
= l
->next
)
286 g_markup_printf_escaped ("<interface name=\"%s\"/>\n",
288 g_writer_write (writer
, markup
);
291 g_writer_write_unindent (writer
, "</implements>\n");
293 else if (node
->node
.type
== G_IDL_NODE_INTERFACE
294 && node
->prerequisites
!= NULL
)
297 g_writer_write_indent (writer
, "<requires>\n");
298 for (l
= node
->prerequisites
; l
!= NULL
; l
= l
->next
)
301 g_markup_printf_escaped ("<interface name=\"%s\"/>\n",
303 g_writer_write (writer
, markup
);
306 g_writer_write_unindent (writer
, "</requires>\n");
309 for (l
= node
->members
; l
!= NULL
; l
= l
->next
)
311 node_generate (writer
, l
->data
);
314 if (node
->node
.type
== G_IDL_NODE_OBJECT
)
316 g_writer_write_unindent (writer
, "</object>\n");
318 else if (node
->node
.type
== G_IDL_NODE_INTERFACE
)
320 g_writer_write_unindent (writer
, "</interface>\n");
325 struct_generate (GIdlWriter
* writer
, GIdlNodeStruct
* node
)
329 g_markup_printf_escaped ("<struct name=\"%s\">\n", node
->node
.name
);
330 g_writer_write_indent (writer
, markup
);
332 for (l
= node
->members
; l
!= NULL
; l
= l
->next
)
334 node_generate (writer
, l
->data
);
336 g_writer_write_unindent (writer
, "</struct>\n");
340 union_generate (GIdlWriter
* writer
, GIdlNodeUnion
* node
)
344 g_markup_printf_escaped ("<union name=\"%s\">\n", node
->node
.name
);
345 g_writer_write_indent (writer
, markup
);
347 for (l
= node
->members
; l
!= NULL
; l
= l
->next
)
349 node_generate (writer
, l
->data
);
351 g_writer_write_unindent (writer
, "</union>\n");
355 boxed_generate (GIdlWriter
* writer
, GIdlNodeBoxed
* node
)
359 g_markup_printf_escaped
360 ("<boxed name=\"%s\" type-name=\"%s\" get-type=\"%s\">\n",
361 node
->node
.name
, node
->gtype_name
, node
->gtype_init
);
362 g_writer_write_indent (writer
, markup
);
364 for (l
= node
->members
; l
!= NULL
; l
= l
->next
)
366 node_generate (writer
, l
->data
);
368 g_writer_write_unindent (writer
, "</boxed>\n");
372 enum_generate (GIdlWriter
* writer
, GIdlNodeEnum
* node
)
377 const char *tag_name
= NULL
;
379 if (node
->node
.type
== G_IDL_NODE_ENUM
)
383 else if (node
->node
.type
== G_IDL_NODE_FLAGS
)
388 markup_s
= g_string_new ("<");
389 g_string_append_printf (markup_s
,
391 tag_name
, node
->node
.name
);
393 if (node
->gtype_name
!= NULL
) {
394 gchar
*tmp
= g_markup_printf_escaped (" type-name=\"%s\"", node
->gtype_name
);
395 markup_s
= g_string_append (markup_s
, tmp
);
398 if (node
->gtype_init
!= NULL
) {
399 gchar
*tmp
= g_markup_printf_escaped (" get-type=\"%s\"", node
->gtype_init
);
400 markup_s
= g_string_append (markup_s
, tmp
);
404 if (node
->deprecated
)
405 markup_s
= g_string_append (markup_s
, " deprecated=\"1\"");
407 markup_s
= g_string_append (markup_s
, ">\n");
409 g_writer_write_indent (writer
, markup_s
->str
);
410 g_string_free (markup_s
, TRUE
);
412 for (l
= node
->values
; l
!= NULL
; l
= l
->next
)
414 node_generate (writer
, l
->data
);
417 markup
= g_strdup_printf ("</%s>\n", tag_name
);
418 g_writer_write_unindent (writer
, markup
);
423 node_generate (GIdlWriter
* writer
, GIdlNode
* node
)
427 case G_IDL_NODE_FUNCTION
:
428 case G_IDL_NODE_CALLBACK
:
429 function_generate (writer
, (GIdlNodeFunction
*) node
);
431 case G_IDL_NODE_VFUNC
:
432 vfunc_generate (writer
, (GIdlNodeVFunc
*) node
);
434 case G_IDL_NODE_OBJECT
:
435 case G_IDL_NODE_INTERFACE
:
436 interface_generate (writer
, (GIdlNodeInterface
*) node
);
438 case G_IDL_NODE_STRUCT
:
439 struct_generate (writer
, (GIdlNodeStruct
*) node
);
441 case G_IDL_NODE_UNION
:
442 union_generate (writer
, (GIdlNodeUnion
*) node
);
444 case G_IDL_NODE_BOXED
:
445 boxed_generate (writer
, (GIdlNodeBoxed
*) node
);
447 case G_IDL_NODE_ENUM
:
448 case G_IDL_NODE_FLAGS
:
449 enum_generate (writer
, (GIdlNodeEnum
*) node
);
451 case G_IDL_NODE_PROPERTY
:
452 property_generate (writer
, (GIdlNodeProperty
*) node
);
454 case G_IDL_NODE_FIELD
:
455 field_generate (writer
, (GIdlNodeField
*) node
);
457 case G_IDL_NODE_SIGNAL
:
458 signal_generate (writer
, (GIdlNodeSignal
*) node
);
460 case G_IDL_NODE_VALUE
:
461 value_generate (writer
, (GIdlNodeValue
*) node
);
463 case G_IDL_NODE_CONSTANT
:
464 constant_generate (writer
, (GIdlNodeConstant
*) node
);
467 g_assert_not_reached ();
472 g_writer_write_module (GIdlWriter
* writer
, GIdlModule
* module
)
476 g_markup_printf_escaped ("<namespace name=\"%s\">\n", module
->name
);
477 g_writer_write_indent (writer
, markup
);
479 for (l
= module
->entries
; l
!= NULL
; l
= l
->next
)
481 node_generate (writer
, l
->data
);
483 g_writer_write_unindent (writer
, "</namespace>\n");
487 g_idl_writer_save_file (GIdlModule
*module
,
488 const gchar
*filename
)
492 writer
= g_new0 (GIdlWriter
, 1);
495 writer
->output
= stdout
;
497 writer
->output
= fopen (filename
, "w");
499 g_writer_write (writer
, "<?xml version=\"1.0\"?>\n");
500 g_writer_write_indent (writer
, "<api version=\"1.0\">\n");
501 g_writer_write_module (writer
, module
);
502 g_writer_write_unindent (writer
, "</api>\n");
505 fclose (writer
->output
);