Add embedded metadata examples
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-parent-links.c
CommitLineData
67905e42
MD
1/*
2 * ctf-visitor-parent-links.c
3 *
4 * Common Trace Format Metadata Parent Link Creator.
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>
24#include <glib.h>
25#include <inttypes.h>
26#include <errno.h>
27#include <babeltrace/list.h>
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)
67905e42
MD
33
34static
35int ctf_visitor_unary_expression(FILE *fd, int depth, struct ctf_node *node)
36{
37 int ret = 0;
38
39 switch (node->u.unary_expression.link) {
40 case UNARY_LINK_UNKNOWN:
41 case UNARY_DOTLINK:
42 case UNARY_ARROWLINK:
43 case UNARY_DOTDOTDOT:
44 break;
45 default:
46 fprintf(fd, "[error] %s: unknown expression link type %d\n", __func__,
47 (int) node->u.unary_expression.link);
48 return -EINVAL;
49 }
50
51 switch (node->u.unary_expression.type) {
52 case UNARY_STRING:
53 case UNARY_SIGNED_CONSTANT:
54 case UNARY_UNSIGNED_CONSTANT:
55 break;
56 case UNARY_SBRAC:
57 node->u.unary_expression.u.sbrac_exp->parent = node;
58 ret = ctf_visitor_unary_expression(fd, depth + 1,
59 node->u.unary_expression.u.sbrac_exp);
60 if (ret)
61 return ret;
62 break;
63 case UNARY_NESTED:
64 node->u.unary_expression.u.nested_exp->parent = node;
65 ret = ctf_visitor_unary_expression(fd, depth + 1,
66 node->u.unary_expression.u.nested_exp);
67 if (ret)
68 return ret;
69 break;
70
71 case UNARY_UNKNOWN:
72 default:
73 fprintf(fd, "[error] %s: unknown expression type %d\n", __func__,
74 (int) node->u.unary_expression.type);
75 return -EINVAL;
76 }
77 return 0;
78}
79
80static
81int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node)
82{
83 switch (node->u.type_specifier.type) {
84 case TYPESPEC_VOID:
85 case TYPESPEC_CHAR:
86 case TYPESPEC_SHORT:
87 case TYPESPEC_INT:
88 case TYPESPEC_LONG:
89 case TYPESPEC_FLOAT:
90 case TYPESPEC_DOUBLE:
91 case TYPESPEC_SIGNED:
92 case TYPESPEC_UNSIGNED:
93 case TYPESPEC_BOOL:
94 case TYPESPEC_COMPLEX:
3888a159 95 case TYPESPEC_IMAGINARY:
67905e42
MD
96 case TYPESPEC_CONST:
97 case TYPESPEC_ID_TYPE:
98 break;
99
100 case TYPESPEC_UNKNOWN:
101 default:
102 fprintf(fd, "[error] %s: unknown type specifier %d\n", __func__,
103 (int) node->u.type_specifier.type);
104 return -EINVAL;
105 }
106 return 0;
107}
108
109static
110int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node)
111{
112 int ret = 0;
113 struct ctf_node *iter;
114
115 depth++;
116
117 if (!cds_list_empty(&node->u.type_declarator.pointers)) {
118 cds_list_for_each_entry(iter, &node->u.type_declarator.pointers,
119 siblings) {
120 iter->parent = node;
121 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
122 if (ret)
123 return ret;
124 }
125 }
126
127 switch (node->u.type_declarator.type) {
128 case TYPEDEC_ID:
129 break;
130 case TYPEDEC_NESTED:
131 if (node->u.type_declarator.u.nested.type_declarator) {
132 node->u.type_declarator.u.nested.type_declarator->parent = node;
133 ret = ctf_visitor_parent_links(fd, depth + 1,
134 node->u.type_declarator.u.nested.type_declarator);
135 if (ret)
136 return ret;
137 }
7d4192cb
MD
138 cds_list_for_each_entry(iter, &node->u.type_declarator.u.nested.length,
139 siblings) {
140 iter->parent = node;
141 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
67905e42
MD
142 if (ret)
143 return ret;
144 }
145 if (node->u.type_declarator.bitfield_len) {
146 node->u.type_declarator.bitfield_len = node;
147 ret = ctf_visitor_parent_links(fd, depth + 1,
148 node->u.type_declarator.bitfield_len);
149 if (ret)
150 return ret;
151 }
152 break;
153 case TYPEDEC_UNKNOWN:
154 default:
155 fprintf(fd, "[error] %s: unknown type declarator %d\n", __func__,
156 (int) node->u.type_declarator.type);
157 return -EINVAL;
158 }
159 depth--;
160 return 0;
161}
162
163int ctf_visitor_parent_links(FILE *fd, int depth, struct ctf_node *node)
164{
165 int ret = 0;
166 struct ctf_node *iter;
167
168 switch (node->type) {
169 case NODE_ROOT:
170 cds_list_for_each_entry(iter, &node->u.root._typedef,
171 siblings) {
172 iter->parent = node;
173 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
174 if (ret)
175 return ret;
176 }
177 cds_list_for_each_entry(iter, &node->u.root.typealias,
178 siblings) {
179 iter->parent = node;
180 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
181 if (ret)
182 return ret;
183 }
184 cds_list_for_each_entry(iter, &node->u.root.declaration_specifier, siblings) {
185 iter->parent = node;
186 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
187 if (ret)
188 return ret;
189 }
190 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
191 iter->parent = node;
192 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
193 if (ret)
194 return ret;
195 }
196 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
197 iter->parent = node;
198 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
199 if (ret)
200 return ret;
201 }
202 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
203 iter->parent = node;
204 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
205 if (ret)
206 return ret;
207 }
208 break;
209
210 case NODE_EVENT:
211 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
212 iter->parent = node;
213 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
214 if (ret)
215 return ret;
216 }
217 break;
218 case NODE_STREAM:
219 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
220 iter->parent = node;
221 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
222 if (ret)
223 return ret;
224 }
225 break;
226 case NODE_TRACE:
227 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
228 iter->parent = node;
229 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
230 if (ret)
231 return ret;
232 }
233 break;
234
235 case NODE_CTF_EXPRESSION:
236 depth++;
237 cds_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) {
238 iter->parent = node;
239 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
240 if (ret)
241 return ret;
242 }
243 cds_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) {
244 iter->parent = node;
245 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
246 if (ret)
247 return ret;
248 }
249 depth--;
250 break;
251 case NODE_UNARY_EXPRESSION:
252 return ctf_visitor_unary_expression(fd, depth, node);
253
254 case NODE_TYPEDEF:
255 depth++;
256 cds_list_for_each_entry(iter, &node->u._typedef.declaration_specifier, siblings) {
257 iter->parent = node;
258 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
259 if (ret)
260 return ret;
261 }
262 cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
263 iter->parent = node;
264 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
265 if (ret)
266 return ret;
267 }
268 depth--;
269 break;
270 case NODE_TYPEALIAS_TARGET:
271 depth++;
272 cds_list_for_each_entry(iter, &node->u.typealias_target.declaration_specifier, siblings) {
273 iter->parent = node;
274 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
275 if (ret)
276 return ret;
277 }
278 cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
279 iter->parent = node;
280 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
281 if (ret)
282 return ret;
283 }
284 depth--;
285 break;
286 case NODE_TYPEALIAS_ALIAS:
287 depth++;
288 cds_list_for_each_entry(iter, &node->u.typealias_alias.declaration_specifier, siblings) {
289 iter->parent = node;
290 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
291 if (ret)
292 return ret;
293 }
294 cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) {
295 iter->parent = node;
296 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
297 if (ret)
298 return ret;
299 }
300 depth--;
301 break;
302 case NODE_TYPEALIAS:
303 node->u.typealias.target->parent = node;
304 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.target);
305 if (ret)
306 return ret;
307 node->u.typealias.alias->parent = node;
308 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.alias);
309 if (ret)
310 return ret;
311 break;
312
313 case NODE_TYPE_SPECIFIER:
314 ret = ctf_visitor_type_specifier(fd, depth, node);
315 if (ret)
316 return ret;
317 break;
318 case NODE_POINTER:
319 break;
320 case NODE_TYPE_DECLARATOR:
321 ret = ctf_visitor_type_declarator(fd, depth, node);
322 if (ret)
323 return ret;
324 break;
325
326 case NODE_FLOATING_POINT:
327 cds_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
328 iter->parent = node;
329 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
330 if (ret)
331 return ret;
332 }
333 break;
334 case NODE_INTEGER:
335 cds_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
336 iter->parent = node;
337 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
338 if (ret)
339 return ret;
340 }
341 break;
342 case NODE_STRING:
343 cds_list_for_each_entry(iter, &node->u.string.expressions, siblings) {
344 iter->parent = node;
345 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
346 if (ret)
347 return ret;
348 }
349 break;
350 case NODE_ENUMERATOR:
67905e42
MD
351 cds_list_for_each_entry(iter, &node->u.enumerator.values, siblings) {
352 iter->parent = node;
353 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
354 if (ret)
355 return ret;
356 }
357 break;
358 case NODE_ENUM:
67905e42
MD
359 depth++;
360
7d4192cb
MD
361 cds_list_for_each_entry(iter, &node->u._enum.container_type,
362 siblings) {
363 iter->parent = node;
364 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
67905e42
MD
365 if (ret)
366 return ret;
367 }
368
369 cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
370 iter->parent = node;
371 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
372 if (ret)
373 return ret;
374 }
375 depth--;
376 break;
377 case NODE_STRUCT_OR_VARIANT_DECLARATION:
378 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.declaration_specifier, siblings) {
379 iter->parent = node;
380 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
381 if (ret)
382 return ret;
383 }
384 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) {
385 iter->parent = node;
386 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
387 if (ret)
388 return ret;
389 }
390 break;
391 case NODE_VARIANT:
67905e42
MD
392 cds_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) {
393 iter->parent = node;
394 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
395 if (ret)
396 return ret;
397 }
398 break;
399 case NODE_STRUCT:
400 cds_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
401 iter->parent = node;
402 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
403 if (ret)
404 return ret;
405 }
406 break;
407
408 case NODE_UNKNOWN:
409 default:
410 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
411 (int) node->type);
412 return -EINVAL;
413 }
414 return ret;
415}
This page took 0.03768 seconds and 4 git commands to generate.