Remove nested expressions
[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
73 case UNARY_UNKNOWN:
74 default:
75 fprintf(fd, "[error] %s: unknown expression type %d\n", __func__,
76 (int) node->u.unary_expression.type);
77 return -EINVAL;
78 }
79 return 0;
80 }
81
82 static
83 int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node)
84 {
85 int ret;
86
87 switch (node->u.type_specifier.type) {
88 case TYPESPEC_VOID:
89 case TYPESPEC_CHAR:
90 case TYPESPEC_SHORT:
91 case TYPESPEC_INT:
92 case TYPESPEC_LONG:
93 case TYPESPEC_FLOAT:
94 case TYPESPEC_DOUBLE:
95 case TYPESPEC_SIGNED:
96 case TYPESPEC_UNSIGNED:
97 case TYPESPEC_BOOL:
98 case TYPESPEC_COMPLEX:
99 case TYPESPEC_IMAGINARY:
100 case TYPESPEC_CONST:
101 case TYPESPEC_ID_TYPE:
102 break;
103 case TYPESPEC_FLOATING_POINT:
104 case TYPESPEC_INTEGER:
105 case TYPESPEC_STRING:
106 case TYPESPEC_STRUCT:
107 case TYPESPEC_VARIANT:
108 case TYPESPEC_ENUM:
109 node->u.type_specifier.node->parent = node;
110 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.type_specifier.node);
111 if (ret)
112 return ret;
113 break;
114
115 case TYPESPEC_UNKNOWN:
116 default:
117 fprintf(fd, "[error] %s: unknown type specifier %d\n", __func__,
118 (int) node->u.type_specifier.type);
119 return -EINVAL;
120 }
121 return 0;
122 }
123
124 static
125 int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node)
126 {
127 int ret = 0;
128 struct ctf_node *iter;
129
130 depth++;
131
132 bt_list_for_each_entry(iter, &node->u.type_declarator.pointers,
133 siblings) {
134 iter->parent = node;
135 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
136 if (ret)
137 return ret;
138 }
139
140 switch (node->u.type_declarator.type) {
141 case TYPEDEC_ID:
142 break;
143 case TYPEDEC_NESTED:
144 if (node->u.type_declarator.u.nested.type_declarator) {
145 node->u.type_declarator.u.nested.type_declarator->parent = node;
146 ret = ctf_visitor_parent_links(fd, depth + 1,
147 node->u.type_declarator.u.nested.type_declarator);
148 if (ret)
149 return ret;
150 }
151 if (!node->u.type_declarator.u.nested.abstract_array) {
152 bt_list_for_each_entry(iter, &node->u.type_declarator.u.nested.length,
153 siblings) {
154 iter->parent = node;
155 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
156 if (ret)
157 return ret;
158 }
159 }
160 if (node->u.type_declarator.bitfield_len) {
161 node->u.type_declarator.bitfield_len = node;
162 ret = ctf_visitor_parent_links(fd, depth + 1,
163 node->u.type_declarator.bitfield_len);
164 if (ret)
165 return ret;
166 }
167 break;
168 case TYPEDEC_UNKNOWN:
169 default:
170 fprintf(fd, "[error] %s: unknown type declarator %d\n", __func__,
171 (int) node->u.type_declarator.type);
172 return -EINVAL;
173 }
174 depth--;
175 return 0;
176 }
177
178 int ctf_visitor_parent_links(FILE *fd, int depth, struct ctf_node *node)
179 {
180 int ret = 0;
181 struct ctf_node *iter;
182
183 switch (node->type) {
184 case NODE_ROOT:
185 bt_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) {
186 iter->parent = node;
187 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
188 if (ret)
189 return ret;
190 }
191 bt_list_for_each_entry(iter, &node->u.root.trace, siblings) {
192 iter->parent = node;
193 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
194 if (ret)
195 return ret;
196 }
197 bt_list_for_each_entry(iter, &node->u.root.stream, siblings) {
198 iter->parent = node;
199 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
200 if (ret)
201 return ret;
202 }
203 bt_list_for_each_entry(iter, &node->u.root.event, siblings) {
204 iter->parent = node;
205 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
206 if (ret)
207 return ret;
208 }
209 bt_list_for_each_entry(iter, &node->u.root.clock, siblings) {
210 iter->parent = node;
211 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
212 if (ret)
213 return ret;
214 }
215 bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) {
216 iter->parent = node;
217 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
218 if (ret)
219 return ret;
220 }
221 break;
222
223 case NODE_EVENT:
224 bt_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
225 iter->parent = node;
226 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
227 if (ret)
228 return ret;
229 }
230 break;
231 case NODE_STREAM:
232 bt_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
233 iter->parent = node;
234 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
235 if (ret)
236 return ret;
237 }
238 break;
239 case NODE_ENV:
240 bt_list_for_each_entry(iter, &node->u.env.declaration_list, siblings) {
241 iter->parent = node;
242 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
243 if (ret)
244 return ret;
245 }
246 break;
247 case NODE_TRACE:
248 bt_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
249 iter->parent = node;
250 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
251 if (ret)
252 return ret;
253 }
254 break;
255 case NODE_CLOCK:
256 bt_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) {
257 iter->parent = node;
258 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
259 if (ret)
260 return ret;
261 }
262 break;
263 case NODE_CALLSITE:
264 bt_list_for_each_entry(iter, &node->u.callsite.declaration_list, siblings) {
265 iter->parent = node;
266 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
267 if (ret)
268 return ret;
269 }
270 break;
271
272 case NODE_CTF_EXPRESSION:
273 depth++;
274 bt_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) {
275 iter->parent = node;
276 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
277 if (ret)
278 return ret;
279 }
280 bt_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) {
281 iter->parent = node;
282 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
283 if (ret)
284 return ret;
285 }
286 depth--;
287 break;
288 case NODE_UNARY_EXPRESSION:
289 return ctf_visitor_unary_expression(fd, depth, node);
290
291 case NODE_TYPEDEF:
292 depth++;
293 node->u._typedef.type_specifier_list->parent = node;
294 ret = ctf_visitor_parent_links(fd, depth + 1, node->u._typedef.type_specifier_list);
295 if (ret)
296 return ret;
297 bt_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
298 iter->parent = node;
299 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
300 if (ret)
301 return ret;
302 }
303 depth--;
304 break;
305 case NODE_TYPEALIAS_TARGET:
306 depth++;
307 node->u.typealias_target.type_specifier_list->parent = node;
308 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_target.type_specifier_list);
309 if (ret)
310 return ret;
311 bt_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
312 iter->parent = node;
313 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
314 if (ret)
315 return ret;
316 }
317 depth--;
318 break;
319 case NODE_TYPEALIAS_ALIAS:
320 depth++;
321 node->u.typealias_alias.type_specifier_list->parent = node;
322 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_alias.type_specifier_list);
323 if (ret)
324 return ret;
325 bt_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) {
326 iter->parent = node;
327 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
328 if (ret)
329 return ret;
330 }
331 depth--;
332 break;
333 case NODE_TYPEALIAS:
334 node->u.typealias.target->parent = node;
335 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.target);
336 if (ret)
337 return ret;
338 node->u.typealias.alias->parent = node;
339 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.alias);
340 if (ret)
341 return ret;
342 break;
343
344 case NODE_TYPE_SPECIFIER_LIST:
345 bt_list_for_each_entry(iter, &node->u.type_specifier_list.head, siblings) {
346 iter->parent = node;
347 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
348 if (ret)
349 return ret;
350 }
351 break;
352
353 case NODE_TYPE_SPECIFIER:
354 ret = ctf_visitor_type_specifier(fd, depth, node);
355 if (ret)
356 return ret;
357 break;
358 case NODE_POINTER:
359 break;
360 case NODE_TYPE_DECLARATOR:
361 ret = ctf_visitor_type_declarator(fd, depth, node);
362 if (ret)
363 return ret;
364 break;
365
366 case NODE_FLOATING_POINT:
367 bt_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
368 iter->parent = node;
369 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
370 if (ret)
371 return ret;
372 }
373 break;
374 case NODE_INTEGER:
375 bt_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
376 iter->parent = node;
377 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
378 if (ret)
379 return ret;
380 }
381 break;
382 case NODE_STRING:
383 bt_list_for_each_entry(iter, &node->u.string.expressions, siblings) {
384 iter->parent = node;
385 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
386 if (ret)
387 return ret;
388 }
389 break;
390 case NODE_ENUMERATOR:
391 bt_list_for_each_entry(iter, &node->u.enumerator.values, siblings) {
392 iter->parent = node;
393 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
394 if (ret)
395 return ret;
396 }
397 break;
398 case NODE_ENUM:
399 depth++;
400 if (node->u._enum.container_type) {
401 ret = ctf_visitor_parent_links(fd, depth + 1, node->u._enum.container_type);
402 if (ret)
403 return ret;
404 }
405
406 bt_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
407 iter->parent = node;
408 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
409 if (ret)
410 return ret;
411 }
412 depth--;
413 break;
414 case NODE_STRUCT_OR_VARIANT_DECLARATION:
415 node->u.struct_or_variant_declaration.type_specifier_list->parent = node;
416 ret = ctf_visitor_parent_links(fd, depth + 1,
417 node->u.struct_or_variant_declaration.type_specifier_list);
418 if (ret)
419 return ret;
420 bt_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) {
421 iter->parent = node;
422 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
423 if (ret)
424 return ret;
425 }
426 break;
427 case NODE_VARIANT:
428 bt_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) {
429 iter->parent = node;
430 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
431 if (ret)
432 return ret;
433 }
434 break;
435 case NODE_STRUCT:
436 bt_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
437 iter->parent = node;
438 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
439 if (ret)
440 return ret;
441 }
442 bt_list_for_each_entry(iter, &node->u._struct.min_align,
443 siblings) {
444 iter->parent = node;
445 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
446 if (ret)
447 return ret;
448 }
449 break;
450
451 case NODE_UNKNOWN:
452 default:
453 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
454 (int) node->type);
455 return -EINVAL;
456 }
457 return ret;
458 }
This page took 0.03823 seconds and 4 git commands to generate.