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