Add missing _Imaginary type
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-xml.c
CommitLineData
7de8808c
MD
1/*
2 * ctf-visitor-xml.c
3 *
4 * Common Trace Format Metadata Visitor (XML dump).
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19#include <stdio.h>
20#include <unistd.h>
21#include <string.h>
22#include <stdlib.h>
23#include <assert.h>
7de8808c 24#include <glib.h>
380d60b1 25#include <inttypes.h>
7de8808c 26#include <errno.h>
fe41395a 27#include <babeltrace/list.h>
7de8808c
MD
28#include "ctf-scanner.h"
29#include "ctf-parser.h"
30#include "ctf-ast.h"
31
34f7b02c 32#define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
7de8808c 33
67905e42
MD
34static
35void print_tabs(FILE *fd, int depth)
7de8808c
MD
36{
37 int i;
38
39 for (i = 0; i < depth; i++)
40 fprintf(fd, "\t");
41}
42
67905e42 43static
7de8808c
MD
44int ctf_visitor_print_unary_expression(FILE *fd, int depth, struct ctf_node *node)
45{
46 int ret = 0;
47
48a01768
MD
48 switch (node->u.unary_expression.link) {
49 case UNARY_LINK_UNKNOWN:
50 break;
51 case UNARY_DOTLINK:
52 print_tabs(fd, depth);
53 fprintf(fd, "<dotlink/>\n");
54 break;
55 case UNARY_ARROWLINK:
56 print_tabs(fd, depth);
57 fprintf(fd, "<arrowlink/>\n");
58 break;
59 case UNARY_DOTDOTDOT:
60 print_tabs(fd, depth);
61 fprintf(fd, "<dotdotdot/>\n");
62 break;
63 default:
64 fprintf(stderr, "[error] %s: unknown expression link type %d\n", __func__,
65 (int) node->u.unary_expression.link);
66 return -EINVAL;
67 }
68
7de8808c
MD
69 switch (node->u.unary_expression.type) {
70 case UNARY_STRING:
71 print_tabs(fd, depth);
72 fprintf(fd, "<unary_expression value=");
73 fprintf(fd, "\"%s\"", node->u.unary_expression.u.string);
48a01768 74 fprintf(fd, " />\n");
7de8808c
MD
75 break;
76 case UNARY_SIGNED_CONSTANT:
77 print_tabs(fd, depth);
78 fprintf(fd, "<unary_expression value=");
380d60b1 79 fprintf(fd, "%" PRId64, node->u.unary_expression.u.signed_constant);
48a01768 80 fprintf(fd, " />\n");
7de8808c
MD
81 break;
82 case UNARY_UNSIGNED_CONSTANT:
83 print_tabs(fd, depth);
84 fprintf(fd, "<unary_expression value=");
380d60b1 85 fprintf(fd, "%" PRIu64, node->u.unary_expression.u.signed_constant);
48a01768 86 fprintf(fd, " />\n");
7de8808c
MD
87 break;
88 case UNARY_SBRAC:
89 print_tabs(fd, depth);
4759bcf5 90 fprintf(fd, "<unary_expression_sbrac>\n");
7de8808c
MD
91 ret = ctf_visitor_print_unary_expression(fd, depth + 1,
92 node->u.unary_expression.u.sbrac_exp);
93 if (ret)
94 return ret;
95 print_tabs(fd, depth);
4759bcf5 96 fprintf(fd, "</unary_expression_sbrac>\n");
7de8808c 97 break;
48a01768
MD
98 case UNARY_NESTED:
99 print_tabs(fd, depth);
4759bcf5 100 fprintf(fd, "<unary_expression_nested>\n");
48a01768
MD
101 ret = ctf_visitor_print_unary_expression(fd, depth + 1,
102 node->u.unary_expression.u.nested_exp);
103 if (ret)
104 return ret;
105 print_tabs(fd, depth);
4759bcf5 106 fprintf(fd, "</unary_expression_nested>\n");
48a01768 107 break;
7de8808c
MD
108
109 case UNARY_UNKNOWN:
110 default:
111 fprintf(stderr, "[error] %s: unknown expression type %d\n", __func__,
112 (int) node->u.unary_expression.type);
113 return -EINVAL;
114 }
7de8808c
MD
115 return 0;
116}
117
67905e42 118static
7de8808c
MD
119int ctf_visitor_print_type_specifier(FILE *fd, int depth, struct ctf_node *node)
120{
121 print_tabs(fd, depth);
48a01768 122 fprintf(fd, "<type_specifier \"");
7de8808c
MD
123
124 switch (node->u.type_specifier.type) {
125 case TYPESPEC_VOID:
126 fprintf(fd, "void");
127 break;
128 case TYPESPEC_CHAR:
129 fprintf(fd, "char");
130 break;
131 case TYPESPEC_SHORT:
132 fprintf(fd, "short");
133 break;
134 case TYPESPEC_INT:
135 fprintf(fd, "int");
136 break;
137 case TYPESPEC_LONG:
138 fprintf(fd, "long");
139 break;
140 case TYPESPEC_FLOAT:
141 fprintf(fd, "float");
142 break;
143 case TYPESPEC_DOUBLE:
144 fprintf(fd, "double");
145 break;
146 case TYPESPEC_SIGNED:
147 fprintf(fd, "signed");
148 break;
149 case TYPESPEC_UNSIGNED:
150 fprintf(fd, "unsigned");
151 break;
152 case TYPESPEC_BOOL:
153 fprintf(fd, "bool");
154 break;
155 case TYPESPEC_COMPLEX:
3888a159
MD
156 fprintf(fd, "_Complex");
157 break;
158 case TYPESPEC_IMAGINARY:
159 fprintf(fd, "_Imaginary");
7de8808c
MD
160 break;
161 case TYPESPEC_CONST:
162 fprintf(fd, "const");
163 break;
164 case TYPESPEC_ID_TYPE:
165 fprintf(fd, "%s", node->u.type_specifier.id_type);
166 break;
167
168 case TYPESPEC_UNKNOWN:
169 default:
170 fprintf(stderr, "[error] %s: unknown type specifier %d\n", __func__,
171 (int) node->u.type_specifier.type);
172 return -EINVAL;
173 }
174 fprintf(fd, "\"/>\n");
175 return 0;
176}
177
67905e42 178static
7de8808c
MD
179int ctf_visitor_print_type_declarator(FILE *fd, int depth, struct ctf_node *node)
180{
181 int ret = 0;
182 struct ctf_node *iter;
183
184 print_tabs(fd, depth);
185 fprintf(fd, "<type_declarator>\n");
186 depth++;
187
48a01768
MD
188 if (!cds_list_empty(&node->u.type_declarator.pointers)) {
189 print_tabs(fd, depth);
190 fprintf(fd, "<pointers>\n");
191 cds_list_for_each_entry(iter, &node->u.type_declarator.pointers,
192 siblings) {
193 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
194 if (ret)
195 return ret;
196 }
197 print_tabs(fd, depth);
198 fprintf(fd, "</pointers>\n");
7de8808c 199 }
7de8808c
MD
200
201 switch (node->u.type_declarator.type) {
202 case TYPEDEC_ID:
0009a725
MD
203 if (node->u.type_declarator.u.id) {
204 print_tabs(fd, depth);
205 fprintf(fd, "<id \"");
206 fprintf(fd, "%s", node->u.type_declarator.u.id);
207 fprintf(fd, "\" />\n");
208 }
7de8808c
MD
209 break;
210 case TYPEDEC_NESTED:
211 if (node->u.type_declarator.u.nested.type_declarator) {
212 print_tabs(fd, depth);
213 fprintf(fd, "<type_declarator>\n");
214 ret = ctf_visitor_print_xml(fd, depth + 1,
215 node->u.type_declarator.u.nested.type_declarator);
216 if (ret)
217 return ret;
218 print_tabs(fd, depth);
219 fprintf(fd, "</type_declarator>\n");
220 }
221 if (node->u.type_declarator.u.nested.length) {
222 print_tabs(fd, depth);
223 fprintf(fd, "<length>\n");
224 ret = ctf_visitor_print_xml(fd, depth + 1,
225 node->u.type_declarator.u.nested.length);
226 if (ret)
227 return ret;
228 print_tabs(fd, depth);
229 fprintf(fd, "</length>\n");
230 }
231 if (node->u.type_declarator.u.nested.abstract_array) {
232 print_tabs(fd, depth);
233 fprintf(fd, "<length>\n");
234 print_tabs(fd, depth);
235 fprintf(fd, "</length>\n");
236 }
237 if (node->u.type_declarator.bitfield_len) {
238 print_tabs(fd, depth);
239 fprintf(fd, "<bitfield_len>\n");
240 ret = ctf_visitor_print_xml(fd, depth + 1,
241 node->u.type_declarator.bitfield_len);
242 if (ret)
243 return ret;
244 print_tabs(fd, depth);
245 fprintf(fd, "</bitfield_len>\n");
246 }
247 break;
248 case TYPEDEC_UNKNOWN:
249 default:
250 fprintf(stderr, "[error] %s: unknown type declarator %d\n", __func__,
251 (int) node->u.type_declarator.type);
252 return -EINVAL;
253 }
254
255 depth--;
256 print_tabs(fd, depth);
257 fprintf(fd, "</type_declarator>\n");
258 return 0;
259}
260
261int ctf_visitor_print_xml(FILE *fd, int depth, struct ctf_node *node)
262{
263 int ret = 0;
264 struct ctf_node *iter;
265
266 switch (node->type) {
267 case NODE_ROOT:
268 print_tabs(fd, depth);
269 fprintf(fd, "<root>\n");
270 cds_list_for_each_entry(iter, &node->u.root._typedef,
271 siblings) {
272 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
273 if (ret)
274 return ret;
275 }
276 cds_list_for_each_entry(iter, &node->u.root.typealias,
277 siblings) {
278 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
279 if (ret)
280 return ret;
281 }
282 cds_list_for_each_entry(iter, &node->u.root.declaration_specifier, siblings) {
283 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
284 if (ret)
285 return ret;
286 }
287 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
288 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
289 if (ret)
290 return ret;
291 }
292 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
293 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
294 if (ret)
295 return ret;
296 }
297 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
298 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
299 if (ret)
300 return ret;
301 }
302 print_tabs(fd, depth);
303 fprintf(fd, "</root>\n");
304 break;
305
306 case NODE_EVENT:
307 print_tabs(fd, depth);
308 fprintf(fd, "<event>\n");
309 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
310 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
311 if (ret)
312 return ret;
313 }
314 print_tabs(fd, depth);
315 fprintf(fd, "</event>\n");
316 break;
317 case NODE_STREAM:
318 print_tabs(fd, depth);
319 fprintf(fd, "<stream>\n");
320 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
321 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
322 if (ret)
323 return ret;
324 }
325 print_tabs(fd, depth);
326 fprintf(fd, "</stream>\n");
327 break;
328 case NODE_TRACE:
329 print_tabs(fd, depth);
330 fprintf(fd, "<trace>\n");
331 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
332 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
333 if (ret)
334 return ret;
335 }
336 print_tabs(fd, depth);
337 fprintf(fd, "</trace>\n");
338 break;
339
340 case NODE_CTF_EXPRESSION:
341 print_tabs(fd, depth);
342 fprintf(fd, "<ctf_expression>\n");
343 depth++;
344 print_tabs(fd, depth);
345 fprintf(fd, "<left>\n");
48a01768
MD
346 cds_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) {
347 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
348 if (ret)
349 return ret;
350 }
351
7de8808c
MD
352 print_tabs(fd, depth);
353 fprintf(fd, "</left>\n");
354
355 print_tabs(fd, depth);
356 fprintf(fd, "<right>\n");
48a01768
MD
357 cds_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) {
358 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
359 if (ret)
360 return ret;
361 }
7de8808c
MD
362 print_tabs(fd, depth);
363 fprintf(fd, "</right>\n");
364 depth--;
365 print_tabs(fd, depth);
366 fprintf(fd, "</ctf_expression>\n");
367 break;
368 case NODE_UNARY_EXPRESSION:
369 return ctf_visitor_print_unary_expression(fd, depth, node);
370
371 case NODE_TYPEDEF:
372 print_tabs(fd, depth);
373 fprintf(fd, "<typedef>\n");
374 depth++;
375 print_tabs(fd, depth);
376 fprintf(fd, "<declaration_specifier>\n");
377 cds_list_for_each_entry(iter, &node->u._typedef.declaration_specifier, siblings) {
378 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
379 if (ret)
380 return ret;
381 }
382 print_tabs(fd, depth);
383 fprintf(fd, "</declaration_specifier>\n");
384
385 print_tabs(fd, depth);
386 fprintf(fd, "<type_declarators>\n");
387 cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
388 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
389 if (ret)
390 return ret;
391 }
392 print_tabs(fd, depth);
393 fprintf(fd, "</type_declarators>\n");
394 depth--;
395 print_tabs(fd, depth);
396 fprintf(fd, "</typedef>\n");
397 break;
398 case NODE_TYPEALIAS_TARGET:
399 print_tabs(fd, depth);
400 fprintf(fd, "<target>\n");
401 depth++;
402
403 print_tabs(fd, depth);
404 fprintf(fd, "<declaration_specifier>\n");
405 cds_list_for_each_entry(iter, &node->u.typealias_target.declaration_specifier, siblings) {
406 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
407 if (ret)
408 return ret;
409 }
410 print_tabs(fd, depth);
411 fprintf(fd, "</declaration_specifier>\n");
412
413 print_tabs(fd, depth);
414 fprintf(fd, "<type_declarators>\n");
415 cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
416 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
417 if (ret)
418 return ret;
419 }
420 print_tabs(fd, depth);
421 fprintf(fd, "</type_declarators>\n");
422
423 depth--;
424 print_tabs(fd, depth);
425 fprintf(fd, "</target>\n");
426 break;
427 case NODE_TYPEALIAS_ALIAS:
428 print_tabs(fd, depth);
429 fprintf(fd, "<alias>\n");
430 depth++;
431
432 print_tabs(fd, depth);
433 fprintf(fd, "<declaration_specifier>\n");
434 cds_list_for_each_entry(iter, &node->u.typealias_alias.declaration_specifier, siblings) {
435 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
436 if (ret)
437 return ret;
438 }
439 print_tabs(fd, depth);
440 fprintf(fd, "</declaration_specifier>\n");
441
442 print_tabs(fd, depth);
443 fprintf(fd, "<type_declarators>\n");
444 cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) {
445 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
446 if (ret)
447 return ret;
448 }
449 print_tabs(fd, depth);
450 fprintf(fd, "</type_declarators>\n");
451
452 depth--;
453 print_tabs(fd, depth);
454 fprintf(fd, "</alias>\n");
455 break;
456 case NODE_TYPEALIAS:
457 print_tabs(fd, depth);
458 fprintf(fd, "<typealias>\n");
459 ret = ctf_visitor_print_xml(fd, depth + 1, node->u.typealias.target);
460 if (ret)
461 return ret;
462 ret = ctf_visitor_print_xml(fd, depth + 1, node->u.typealias.alias);
463 if (ret)
464 return ret;
465 print_tabs(fd, depth);
466 fprintf(fd, "</typealias>\n");
467 break;
468
469 case NODE_TYPE_SPECIFIER:
470 ret = ctf_visitor_print_type_specifier(fd, depth, node);
471 if (ret)
472 return ret;
473 break;
474 case NODE_POINTER:
475 print_tabs(fd, depth);
476 if (node->u.pointer.const_qualifier)
477 fprintf(fd, "<const_pointer />\n");
478 else
479 fprintf(fd, "<pointer />\n");
480 break;
481 case NODE_TYPE_DECLARATOR:
482 ret = ctf_visitor_print_type_declarator(fd, depth, node);
483 if (ret)
484 return ret;
485 break;
486
487 case NODE_FLOATING_POINT:
488 print_tabs(fd, depth);
489 fprintf(fd, "<floating_point>\n");
490 cds_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
491 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
492 if (ret)
493 return ret;
494 }
495 print_tabs(fd, depth);
496 fprintf(fd, "</floating_point>\n");
497 break;
498 case NODE_INTEGER:
499 print_tabs(fd, depth);
500 fprintf(fd, "<integer>\n");
501 cds_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
502 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
503 if (ret)
504 return ret;
505 }
506 print_tabs(fd, depth);
507 fprintf(fd, "</integer>\n");
508 break;
509 case NODE_STRING:
510 print_tabs(fd, depth);
511 fprintf(fd, "<string>\n");
512 cds_list_for_each_entry(iter, &node->u.string.expressions, siblings) {
513 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
514 if (ret)
515 return ret;
516 }
517 print_tabs(fd, depth);
518 fprintf(fd, "</string>\n");
519 break;
520 case NODE_ENUMERATOR:
521 print_tabs(fd, depth);
522 fprintf(fd, "<enumerator");
523 if (node->u.enumerator.id)
524 fprintf(fd, " id=\"%s\"", node->u.enumerator.id);
525 fprintf(fd, ">\n");
48a01768
MD
526 cds_list_for_each_entry(iter, &node->u.enumerator.values, siblings) {
527 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
7de8808c
MD
528 if (ret)
529 return ret;
530 }
531 print_tabs(fd, depth);
48a01768 532 fprintf(fd, "</enumerator>\n");
7de8808c
MD
533 break;
534 case NODE_ENUM:
535 print_tabs(fd, depth);
536 if (node->u._struct.name)
537 fprintf(fd, "<enum name=\"%s\">\n",
538 node->u._enum.enum_id);
539 else
540 fprintf(fd, "<enum >\n");
541 depth++;
542
543 if (node->u._enum.container_type) {
544 print_tabs(fd, depth);
48a01768 545 fprintf(fd, "<container_type>\n");
7de8808c
MD
546 ret = ctf_visitor_print_xml(fd, depth + 1, node->u._enum.container_type);
547 if (ret)
548 return ret;
549 print_tabs(fd, depth);
48a01768 550 fprintf(fd, "</container_type>\n");
7de8808c
MD
551 }
552
553 print_tabs(fd, depth);
48a01768 554 fprintf(fd, "<enumerator_list>\n");
7de8808c
MD
555 cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
556 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
557 if (ret)
558 return ret;
559 }
560 print_tabs(fd, depth);
48a01768 561 fprintf(fd, "</enumerator_list>\n");
7de8808c
MD
562
563 depth--;
564 print_tabs(fd, depth);
565 fprintf(fd, "</enum>\n");
566 break;
567 case NODE_STRUCT_OR_VARIANT_DECLARATION:
568 print_tabs(fd, depth);
569 fprintf(fd, "<declaration_specifier>\n");
570 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.declaration_specifier, siblings) {
571 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
572 if (ret)
573 return ret;
574 }
575 print_tabs(fd, depth);
576 fprintf(fd, "</declaration_specifier>\n");
577
578 print_tabs(fd, depth);
579 fprintf(fd, "<type_declarators>\n");
580 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) {
581 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
582 if (ret)
583 return ret;
584 }
585 print_tabs(fd, depth);
586 fprintf(fd, "</type_declarators>\n");
587 break;
588 case NODE_VARIANT:
589 print_tabs(fd, depth);
590 fprintf(fd, "<variant");
591 if (node->u.variant.name)
592 fprintf(fd, " name=\"%s\"", node->u.variant.name);
593 if (node->u.variant.choice)
594 fprintf(fd, " choice=\"%s\"", node->u.variant.choice);
595 fprintf(fd, ">\n");
596 cds_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) {
597 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
598 if (ret)
599 return ret;
600 }
601 print_tabs(fd, depth);
602 fprintf(fd, "</variant>\n");
603 break;
604 case NODE_STRUCT:
605 print_tabs(fd, depth);
606 if (node->u._struct.name)
607 fprintf(fd, "<struct name=\"%s\">\n",
608 node->u._struct.name);
609 else
610 fprintf(fd, "<struct>\n");
611 cds_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
612 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
613 if (ret)
614 return ret;
615 }
616 print_tabs(fd, depth);
617 fprintf(fd, "</struct>\n");
618 break;
619
620 case NODE_UNKNOWN:
621 default:
622 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
623 (int) node->type);
624 return -EINVAL;
625 }
626 return ret;
627}
This page took 0.046336 seconds and 4 git commands to generate.