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