enum, sequence, array: use declaration list for length/container 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 }
7d4192cb 221 if (!cds_list_empty(&node->u.type_declarator.u.nested.length)) {
7de8808c
MD
222 print_tabs(fd, depth);
223 fprintf(fd, "<length>\n");
7d4192cb
MD
224 }
225 cds_list_for_each_entry(iter, &node->u.type_declarator.u.nested.length,
226 siblings) {
227 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
7de8808c
MD
228 if (ret)
229 return ret;
7d4192cb
MD
230 }
231 if (!cds_list_empty(&node->u.type_declarator.u.nested.length)) {
7de8808c
MD
232 print_tabs(fd, depth);
233 fprintf(fd, "</length>\n");
234 }
235 if (node->u.type_declarator.u.nested.abstract_array) {
236 print_tabs(fd, depth);
237 fprintf(fd, "<length>\n");
238 print_tabs(fd, depth);
239 fprintf(fd, "</length>\n");
240 }
241 if (node->u.type_declarator.bitfield_len) {
242 print_tabs(fd, depth);
243 fprintf(fd, "<bitfield_len>\n");
244 ret = ctf_visitor_print_xml(fd, depth + 1,
245 node->u.type_declarator.bitfield_len);
246 if (ret)
247 return ret;
248 print_tabs(fd, depth);
249 fprintf(fd, "</bitfield_len>\n");
250 }
251 break;
252 case TYPEDEC_UNKNOWN:
253 default:
254 fprintf(stderr, "[error] %s: unknown type declarator %d\n", __func__,
255 (int) node->u.type_declarator.type);
256 return -EINVAL;
257 }
258
259 depth--;
260 print_tabs(fd, depth);
261 fprintf(fd, "</type_declarator>\n");
262 return 0;
263}
264
265int ctf_visitor_print_xml(FILE *fd, int depth, struct ctf_node *node)
266{
267 int ret = 0;
268 struct ctf_node *iter;
269
270 switch (node->type) {
271 case NODE_ROOT:
272 print_tabs(fd, depth);
273 fprintf(fd, "<root>\n");
274 cds_list_for_each_entry(iter, &node->u.root._typedef,
275 siblings) {
276 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
277 if (ret)
278 return ret;
279 }
280 cds_list_for_each_entry(iter, &node->u.root.typealias,
281 siblings) {
282 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
283 if (ret)
284 return ret;
285 }
286 cds_list_for_each_entry(iter, &node->u.root.declaration_specifier, siblings) {
287 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
288 if (ret)
289 return ret;
290 }
291 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
292 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
293 if (ret)
294 return ret;
295 }
296 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
297 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
298 if (ret)
299 return ret;
300 }
301 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
302 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
303 if (ret)
304 return ret;
305 }
306 print_tabs(fd, depth);
307 fprintf(fd, "</root>\n");
308 break;
309
310 case NODE_EVENT:
311 print_tabs(fd, depth);
312 fprintf(fd, "<event>\n");
313 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
314 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
315 if (ret)
316 return ret;
317 }
318 print_tabs(fd, depth);
319 fprintf(fd, "</event>\n");
320 break;
321 case NODE_STREAM:
322 print_tabs(fd, depth);
323 fprintf(fd, "<stream>\n");
324 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
325 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
326 if (ret)
327 return ret;
328 }
329 print_tabs(fd, depth);
330 fprintf(fd, "</stream>\n");
331 break;
332 case NODE_TRACE:
333 print_tabs(fd, depth);
334 fprintf(fd, "<trace>\n");
335 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
336 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
337 if (ret)
338 return ret;
339 }
340 print_tabs(fd, depth);
341 fprintf(fd, "</trace>\n");
342 break;
343
344 case NODE_CTF_EXPRESSION:
345 print_tabs(fd, depth);
346 fprintf(fd, "<ctf_expression>\n");
347 depth++;
348 print_tabs(fd, depth);
349 fprintf(fd, "<left>\n");
48a01768
MD
350 cds_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) {
351 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
352 if (ret)
353 return ret;
354 }
355
7de8808c
MD
356 print_tabs(fd, depth);
357 fprintf(fd, "</left>\n");
358
359 print_tabs(fd, depth);
360 fprintf(fd, "<right>\n");
48a01768
MD
361 cds_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) {
362 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
363 if (ret)
364 return ret;
365 }
7de8808c
MD
366 print_tabs(fd, depth);
367 fprintf(fd, "</right>\n");
368 depth--;
369 print_tabs(fd, depth);
370 fprintf(fd, "</ctf_expression>\n");
371 break;
372 case NODE_UNARY_EXPRESSION:
373 return ctf_visitor_print_unary_expression(fd, depth, node);
374
375 case NODE_TYPEDEF:
376 print_tabs(fd, depth);
377 fprintf(fd, "<typedef>\n");
378 depth++;
379 print_tabs(fd, depth);
380 fprintf(fd, "<declaration_specifier>\n");
381 cds_list_for_each_entry(iter, &node->u._typedef.declaration_specifier, siblings) {
382 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
383 if (ret)
384 return ret;
385 }
386 print_tabs(fd, depth);
387 fprintf(fd, "</declaration_specifier>\n");
388
389 print_tabs(fd, depth);
390 fprintf(fd, "<type_declarators>\n");
391 cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
392 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
393 if (ret)
394 return ret;
395 }
396 print_tabs(fd, depth);
397 fprintf(fd, "</type_declarators>\n");
398 depth--;
399 print_tabs(fd, depth);
400 fprintf(fd, "</typedef>\n");
401 break;
402 case NODE_TYPEALIAS_TARGET:
403 print_tabs(fd, depth);
404 fprintf(fd, "<target>\n");
405 depth++;
406
407 print_tabs(fd, depth);
408 fprintf(fd, "<declaration_specifier>\n");
409 cds_list_for_each_entry(iter, &node->u.typealias_target.declaration_specifier, siblings) {
410 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
411 if (ret)
412 return ret;
413 }
414 print_tabs(fd, depth);
415 fprintf(fd, "</declaration_specifier>\n");
416
417 print_tabs(fd, depth);
418 fprintf(fd, "<type_declarators>\n");
419 cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
420 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
421 if (ret)
422 return ret;
423 }
424 print_tabs(fd, depth);
425 fprintf(fd, "</type_declarators>\n");
426
427 depth--;
428 print_tabs(fd, depth);
429 fprintf(fd, "</target>\n");
430 break;
431 case NODE_TYPEALIAS_ALIAS:
432 print_tabs(fd, depth);
433 fprintf(fd, "<alias>\n");
434 depth++;
435
436 print_tabs(fd, depth);
437 fprintf(fd, "<declaration_specifier>\n");
438 cds_list_for_each_entry(iter, &node->u.typealias_alias.declaration_specifier, siblings) {
439 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
440 if (ret)
441 return ret;
442 }
443 print_tabs(fd, depth);
444 fprintf(fd, "</declaration_specifier>\n");
445
446 print_tabs(fd, depth);
447 fprintf(fd, "<type_declarators>\n");
448 cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) {
449 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
450 if (ret)
451 return ret;
452 }
453 print_tabs(fd, depth);
454 fprintf(fd, "</type_declarators>\n");
455
456 depth--;
457 print_tabs(fd, depth);
458 fprintf(fd, "</alias>\n");
459 break;
460 case NODE_TYPEALIAS:
461 print_tabs(fd, depth);
462 fprintf(fd, "<typealias>\n");
463 ret = ctf_visitor_print_xml(fd, depth + 1, node->u.typealias.target);
464 if (ret)
465 return ret;
466 ret = ctf_visitor_print_xml(fd, depth + 1, node->u.typealias.alias);
467 if (ret)
468 return ret;
469 print_tabs(fd, depth);
470 fprintf(fd, "</typealias>\n");
471 break;
472
473 case NODE_TYPE_SPECIFIER:
474 ret = ctf_visitor_print_type_specifier(fd, depth, node);
475 if (ret)
476 return ret;
477 break;
478 case NODE_POINTER:
479 print_tabs(fd, depth);
480 if (node->u.pointer.const_qualifier)
481 fprintf(fd, "<const_pointer />\n");
482 else
483 fprintf(fd, "<pointer />\n");
484 break;
485 case NODE_TYPE_DECLARATOR:
486 ret = ctf_visitor_print_type_declarator(fd, depth, node);
487 if (ret)
488 return ret;
489 break;
490
491 case NODE_FLOATING_POINT:
492 print_tabs(fd, depth);
493 fprintf(fd, "<floating_point>\n");
494 cds_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
495 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
496 if (ret)
497 return ret;
498 }
499 print_tabs(fd, depth);
500 fprintf(fd, "</floating_point>\n");
501 break;
502 case NODE_INTEGER:
503 print_tabs(fd, depth);
504 fprintf(fd, "<integer>\n");
505 cds_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
506 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
507 if (ret)
508 return ret;
509 }
510 print_tabs(fd, depth);
511 fprintf(fd, "</integer>\n");
512 break;
513 case NODE_STRING:
514 print_tabs(fd, depth);
515 fprintf(fd, "<string>\n");
516 cds_list_for_each_entry(iter, &node->u.string.expressions, siblings) {
517 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
518 if (ret)
519 return ret;
520 }
521 print_tabs(fd, depth);
522 fprintf(fd, "</string>\n");
523 break;
524 case NODE_ENUMERATOR:
525 print_tabs(fd, depth);
526 fprintf(fd, "<enumerator");
527 if (node->u.enumerator.id)
528 fprintf(fd, " id=\"%s\"", node->u.enumerator.id);
529 fprintf(fd, ">\n");
48a01768
MD
530 cds_list_for_each_entry(iter, &node->u.enumerator.values, siblings) {
531 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
7de8808c
MD
532 if (ret)
533 return ret;
534 }
535 print_tabs(fd, depth);
48a01768 536 fprintf(fd, "</enumerator>\n");
7de8808c
MD
537 break;
538 case NODE_ENUM:
539 print_tabs(fd, depth);
540 if (node->u._struct.name)
541 fprintf(fd, "<enum name=\"%s\">\n",
542 node->u._enum.enum_id);
543 else
544 fprintf(fd, "<enum >\n");
545 depth++;
546
7d4192cb 547 if (!cds_list_empty(&node->u._enum.container_type)) {
7de8808c 548 print_tabs(fd, depth);
48a01768 549 fprintf(fd, "<container_type>\n");
7d4192cb
MD
550 }
551
552 cds_list_for_each_entry(iter, &node->u._enum.container_type,
553 siblings) {
554 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
7de8808c
MD
555 if (ret)
556 return ret;
7d4192cb
MD
557 }
558 if (!cds_list_empty(&node->u._enum.container_type)) {
7de8808c 559 print_tabs(fd, depth);
48a01768 560 fprintf(fd, "</container_type>\n");
7de8808c
MD
561 }
562
563 print_tabs(fd, depth);
48a01768 564 fprintf(fd, "<enumerator_list>\n");
7de8808c
MD
565 cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
566 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
567 if (ret)
568 return ret;
569 }
570 print_tabs(fd, depth);
48a01768 571 fprintf(fd, "</enumerator_list>\n");
7de8808c
MD
572
573 depth--;
574 print_tabs(fd, depth);
575 fprintf(fd, "</enum>\n");
576 break;
577 case NODE_STRUCT_OR_VARIANT_DECLARATION:
578 print_tabs(fd, depth);
579 fprintf(fd, "<declaration_specifier>\n");
580 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.declaration_specifier, 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, "</declaration_specifier>\n");
587
588 print_tabs(fd, depth);
589 fprintf(fd, "<type_declarators>\n");
590 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) {
591 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
592 if (ret)
593 return ret;
594 }
595 print_tabs(fd, depth);
596 fprintf(fd, "</type_declarators>\n");
597 break;
598 case NODE_VARIANT:
599 print_tabs(fd, depth);
600 fprintf(fd, "<variant");
601 if (node->u.variant.name)
602 fprintf(fd, " name=\"%s\"", node->u.variant.name);
603 if (node->u.variant.choice)
604 fprintf(fd, " choice=\"%s\"", node->u.variant.choice);
605 fprintf(fd, ">\n");
606 cds_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) {
607 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
608 if (ret)
609 return ret;
610 }
611 print_tabs(fd, depth);
612 fprintf(fd, "</variant>\n");
613 break;
614 case NODE_STRUCT:
615 print_tabs(fd, depth);
616 if (node->u._struct.name)
617 fprintf(fd, "<struct name=\"%s\">\n",
618 node->u._struct.name);
619 else
620 fprintf(fd, "<struct>\n");
621 cds_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
622 ret = ctf_visitor_print_xml(fd, depth + 1, iter);
623 if (ret)
624 return ret;
625 }
626 print_tabs(fd, depth);
627 fprintf(fd, "</struct>\n");
628 break;
629
630 case NODE_UNKNOWN:
631 default:
632 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
633 (int) node->type);
634 return -EINVAL;
635 }
636 return ret;
637}
This page took 0.048053 seconds and 4 git commands to generate.