Add missing permission notice in each source file
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-parent-links.c
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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include <glib.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/list.h>
37 #include "ctf-scanner.h"
38 #include "ctf-parser.h"
39 #include "ctf-ast.h"
40
41 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
42
43 static
44 int ctf_visitor_unary_expression(FILE *fd, int depth, struct ctf_node *node)
45 {
46 int ret = 0;
47
48 switch (node->u.unary_expression.link) {
49 case UNARY_LINK_UNKNOWN:
50 case UNARY_DOTLINK:
51 case UNARY_ARROWLINK:
52 case UNARY_DOTDOTDOT:
53 break;
54 default:
55 fprintf(fd, "[error] %s: unknown expression link type %d\n", __func__,
56 (int) node->u.unary_expression.link);
57 return -EINVAL;
58 }
59
60 switch (node->u.unary_expression.type) {
61 case UNARY_STRING:
62 case UNARY_SIGNED_CONSTANT:
63 case UNARY_UNSIGNED_CONSTANT:
64 break;
65 case UNARY_SBRAC:
66 node->u.unary_expression.u.sbrac_exp->parent = node;
67 ret = ctf_visitor_unary_expression(fd, depth + 1,
68 node->u.unary_expression.u.sbrac_exp);
69 if (ret)
70 return ret;
71 break;
72 case UNARY_NESTED:
73 node->u.unary_expression.u.nested_exp->parent = node;
74 ret = ctf_visitor_unary_expression(fd, depth + 1,
75 node->u.unary_expression.u.nested_exp);
76 if (ret)
77 return ret;
78 break;
79
80 case UNARY_UNKNOWN:
81 default:
82 fprintf(fd, "[error] %s: unknown expression type %d\n", __func__,
83 (int) node->u.unary_expression.type);
84 return -EINVAL;
85 }
86 return 0;
87 }
88
89 static
90 int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node)
91 {
92 int ret;
93
94 switch (node->u.type_specifier.type) {
95 case TYPESPEC_VOID:
96 case TYPESPEC_CHAR:
97 case TYPESPEC_SHORT:
98 case TYPESPEC_INT:
99 case TYPESPEC_LONG:
100 case TYPESPEC_FLOAT:
101 case TYPESPEC_DOUBLE:
102 case TYPESPEC_SIGNED:
103 case TYPESPEC_UNSIGNED:
104 case TYPESPEC_BOOL:
105 case TYPESPEC_COMPLEX:
106 case TYPESPEC_IMAGINARY:
107 case TYPESPEC_CONST:
108 case TYPESPEC_ID_TYPE:
109 break;
110 case TYPESPEC_FLOATING_POINT:
111 case TYPESPEC_INTEGER:
112 case TYPESPEC_STRING:
113 case TYPESPEC_STRUCT:
114 case TYPESPEC_VARIANT:
115 case TYPESPEC_ENUM:
116 node->u.type_specifier.node->parent = node;
117 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.type_specifier.node);
118 if (ret)
119 return ret;
120 break;
121
122 case TYPESPEC_UNKNOWN:
123 default:
124 fprintf(fd, "[error] %s: unknown type specifier %d\n", __func__,
125 (int) node->u.type_specifier.type);
126 return -EINVAL;
127 }
128 return 0;
129 }
130
131 static
132 int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node)
133 {
134 int ret = 0;
135 struct ctf_node *iter;
136
137 depth++;
138
139 bt_list_for_each_entry(iter, &node->u.type_declarator.pointers,
140 siblings) {
141 iter->parent = node;
142 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
143 if (ret)
144 return ret;
145 }
146
147 switch (node->u.type_declarator.type) {
148 case TYPEDEC_ID:
149 break;
150 case TYPEDEC_NESTED:
151 if (node->u.type_declarator.u.nested.type_declarator) {
152 node->u.type_declarator.u.nested.type_declarator->parent = node;
153 ret = ctf_visitor_parent_links(fd, depth + 1,
154 node->u.type_declarator.u.nested.type_declarator);
155 if (ret)
156 return ret;
157 }
158 if (!node->u.type_declarator.u.nested.abstract_array) {
159 bt_list_for_each_entry(iter, &node->u.type_declarator.u.nested.length,
160 siblings) {
161 iter->parent = node;
162 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
163 if (ret)
164 return ret;
165 }
166 }
167 if (node->u.type_declarator.bitfield_len) {
168 node->u.type_declarator.bitfield_len = node;
169 ret = ctf_visitor_parent_links(fd, depth + 1,
170 node->u.type_declarator.bitfield_len);
171 if (ret)
172 return ret;
173 }
174 break;
175 case TYPEDEC_UNKNOWN:
176 default:
177 fprintf(fd, "[error] %s: unknown type declarator %d\n", __func__,
178 (int) node->u.type_declarator.type);
179 return -EINVAL;
180 }
181 depth--;
182 return 0;
183 }
184
185 int ctf_visitor_parent_links(FILE *fd, int depth, struct ctf_node *node)
186 {
187 int ret = 0;
188 struct ctf_node *iter;
189
190 switch (node->type) {
191 case NODE_ROOT:
192 bt_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) {
193 iter->parent = node;
194 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
195 if (ret)
196 return ret;
197 }
198 bt_list_for_each_entry(iter, &node->u.root.trace, siblings) {
199 iter->parent = node;
200 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
201 if (ret)
202 return ret;
203 }
204 bt_list_for_each_entry(iter, &node->u.root.stream, siblings) {
205 iter->parent = node;
206 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
207 if (ret)
208 return ret;
209 }
210 bt_list_for_each_entry(iter, &node->u.root.event, siblings) {
211 iter->parent = node;
212 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
213 if (ret)
214 return ret;
215 }
216 bt_list_for_each_entry(iter, &node->u.root.clock, siblings) {
217 iter->parent = node;
218 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
219 if (ret)
220 return ret;
221 }
222 bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) {
223 iter->parent = node;
224 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
225 if (ret)
226 return ret;
227 }
228 break;
229
230 case NODE_EVENT:
231 bt_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
232 iter->parent = node;
233 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
234 if (ret)
235 return ret;
236 }
237 break;
238 case NODE_STREAM:
239 bt_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
240 iter->parent = node;
241 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
242 if (ret)
243 return ret;
244 }
245 break;
246 case NODE_ENV:
247 bt_list_for_each_entry(iter, &node->u.env.declaration_list, siblings) {
248 iter->parent = node;
249 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
250 if (ret)
251 return ret;
252 }
253 break;
254 case NODE_TRACE:
255 bt_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
256 iter->parent = node;
257 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
258 if (ret)
259 return ret;
260 }
261 break;
262 case NODE_CLOCK:
263 bt_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) {
264 iter->parent = node;
265 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
266 if (ret)
267 return ret;
268 }
269 break;
270 case NODE_CALLSITE:
271 bt_list_for_each_entry(iter, &node->u.callsite.declaration_list, siblings) {
272 iter->parent = node;
273 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
274 if (ret)
275 return ret;
276 }
277 break;
278
279 case NODE_CTF_EXPRESSION:
280 depth++;
281 bt_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) {
282 iter->parent = node;
283 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
284 if (ret)
285 return ret;
286 }
287 bt_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) {
288 iter->parent = node;
289 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
290 if (ret)
291 return ret;
292 }
293 depth--;
294 break;
295 case NODE_UNARY_EXPRESSION:
296 return ctf_visitor_unary_expression(fd, depth, node);
297
298 case NODE_TYPEDEF:
299 depth++;
300 node->u._typedef.type_specifier_list->parent = node;
301 ret = ctf_visitor_parent_links(fd, depth + 1, node->u._typedef.type_specifier_list);
302 if (ret)
303 return ret;
304 bt_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
305 iter->parent = node;
306 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
307 if (ret)
308 return ret;
309 }
310 depth--;
311 break;
312 case NODE_TYPEALIAS_TARGET:
313 depth++;
314 node->u.typealias_target.type_specifier_list->parent = node;
315 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_target.type_specifier_list);
316 if (ret)
317 return ret;
318 bt_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
319 iter->parent = node;
320 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
321 if (ret)
322 return ret;
323 }
324 depth--;
325 break;
326 case NODE_TYPEALIAS_ALIAS:
327 depth++;
328 node->u.typealias_alias.type_specifier_list->parent = node;
329 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_alias.type_specifier_list);
330 if (ret)
331 return ret;
332 bt_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) {
333 iter->parent = node;
334 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
335 if (ret)
336 return ret;
337 }
338 depth--;
339 break;
340 case NODE_TYPEALIAS:
341 node->u.typealias.target->parent = node;
342 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.target);
343 if (ret)
344 return ret;
345 node->u.typealias.alias->parent = node;
346 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.alias);
347 if (ret)
348 return ret;
349 break;
350
351 case NODE_TYPE_SPECIFIER_LIST:
352 bt_list_for_each_entry(iter, &node->u.type_specifier_list.head, siblings) {
353 iter->parent = node;
354 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
355 if (ret)
356 return ret;
357 }
358 break;
359
360 case NODE_TYPE_SPECIFIER:
361 ret = ctf_visitor_type_specifier(fd, depth, node);
362 if (ret)
363 return ret;
364 break;
365 case NODE_POINTER:
366 break;
367 case NODE_TYPE_DECLARATOR:
368 ret = ctf_visitor_type_declarator(fd, depth, node);
369 if (ret)
370 return ret;
371 break;
372
373 case NODE_FLOATING_POINT:
374 bt_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
375 iter->parent = node;
376 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
377 if (ret)
378 return ret;
379 }
380 break;
381 case NODE_INTEGER:
382 bt_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
383 iter->parent = node;
384 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
385 if (ret)
386 return ret;
387 }
388 break;
389 case NODE_STRING:
390 bt_list_for_each_entry(iter, &node->u.string.expressions, siblings) {
391 iter->parent = node;
392 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
393 if (ret)
394 return ret;
395 }
396 break;
397 case NODE_ENUMERATOR:
398 bt_list_for_each_entry(iter, &node->u.enumerator.values, siblings) {
399 iter->parent = node;
400 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
401 if (ret)
402 return ret;
403 }
404 break;
405 case NODE_ENUM:
406 depth++;
407 if (node->u._enum.container_type) {
408 ret = ctf_visitor_parent_links(fd, depth + 1, node->u._enum.container_type);
409 if (ret)
410 return ret;
411 }
412
413 bt_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
414 iter->parent = node;
415 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
416 if (ret)
417 return ret;
418 }
419 depth--;
420 break;
421 case NODE_STRUCT_OR_VARIANT_DECLARATION:
422 node->u.struct_or_variant_declaration.type_specifier_list->parent = node;
423 ret = ctf_visitor_parent_links(fd, depth + 1,
424 node->u.struct_or_variant_declaration.type_specifier_list);
425 if (ret)
426 return ret;
427 bt_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) {
428 iter->parent = node;
429 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
430 if (ret)
431 return ret;
432 }
433 break;
434 case NODE_VARIANT:
435 bt_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) {
436 iter->parent = node;
437 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
438 if (ret)
439 return ret;
440 }
441 break;
442 case NODE_STRUCT:
443 bt_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
444 iter->parent = node;
445 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
446 if (ret)
447 return ret;
448 }
449 bt_list_for_each_entry(iter, &node->u._struct.min_align,
450 siblings) {
451 iter->parent = node;
452 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
453 if (ret)
454 return ret;
455 }
456 break;
457
458 case NODE_UNKNOWN:
459 default:
460 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
461 (int) node->type);
462 return -EINVAL;
463 }
464 return ret;
465 }
This page took 0.038789 seconds and 5 git commands to generate.