Commit | Line | Data |
---|---|---|
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 | ||
32 | #define printf_dbg(fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args) | |
33 | ||
34 | static | |
35 | int 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 | ||
80 | static | |
81 | int 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: | |
95 | case TYPESPEC_CONST: | |
96 | case TYPESPEC_ID_TYPE: | |
97 | break; | |
98 | ||
99 | case TYPESPEC_UNKNOWN: | |
100 | default: | |
101 | fprintf(fd, "[error] %s: unknown type specifier %d\n", __func__, | |
102 | (int) node->u.type_specifier.type); | |
103 | return -EINVAL; | |
104 | } | |
105 | return 0; | |
106 | } | |
107 | ||
108 | static | |
109 | int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node) | |
110 | { | |
111 | int ret = 0; | |
112 | struct ctf_node *iter; | |
113 | ||
114 | depth++; | |
115 | ||
116 | if (!cds_list_empty(&node->u.type_declarator.pointers)) { | |
117 | cds_list_for_each_entry(iter, &node->u.type_declarator.pointers, | |
118 | siblings) { | |
119 | iter->parent = node; | |
120 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
121 | if (ret) | |
122 | return ret; | |
123 | } | |
124 | } | |
125 | ||
126 | switch (node->u.type_declarator.type) { | |
127 | case TYPEDEC_ID: | |
128 | break; | |
129 | case TYPEDEC_NESTED: | |
130 | if (node->u.type_declarator.u.nested.type_declarator) { | |
131 | node->u.type_declarator.u.nested.type_declarator->parent = node; | |
132 | ret = ctf_visitor_parent_links(fd, depth + 1, | |
133 | node->u.type_declarator.u.nested.type_declarator); | |
134 | if (ret) | |
135 | return ret; | |
136 | } | |
137 | if (node->u.type_declarator.u.nested.length) { | |
138 | node->u.type_declarator.u.nested.length->parent = node; | |
139 | ret = ctf_visitor_parent_links(fd, depth + 1, | |
140 | node->u.type_declarator.u.nested.length); | |
141 | if (ret) | |
142 | return ret; | |
143 | } | |
144 | if (node->u.type_declarator.bitfield_len) { | |
145 | node->u.type_declarator.bitfield_len = node; | |
146 | ret = ctf_visitor_parent_links(fd, depth + 1, | |
147 | node->u.type_declarator.bitfield_len); | |
148 | if (ret) | |
149 | return ret; | |
150 | } | |
151 | break; | |
152 | case TYPEDEC_UNKNOWN: | |
153 | default: | |
154 | fprintf(fd, "[error] %s: unknown type declarator %d\n", __func__, | |
155 | (int) node->u.type_declarator.type); | |
156 | return -EINVAL; | |
157 | } | |
158 | depth--; | |
159 | return 0; | |
160 | } | |
161 | ||
162 | int ctf_visitor_parent_links(FILE *fd, int depth, struct ctf_node *node) | |
163 | { | |
164 | int ret = 0; | |
165 | struct ctf_node *iter; | |
166 | ||
167 | switch (node->type) { | |
168 | case NODE_ROOT: | |
169 | cds_list_for_each_entry(iter, &node->u.root._typedef, | |
170 | siblings) { | |
171 | iter->parent = node; | |
172 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
173 | if (ret) | |
174 | return ret; | |
175 | } | |
176 | cds_list_for_each_entry(iter, &node->u.root.typealias, | |
177 | siblings) { | |
178 | iter->parent = node; | |
179 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
180 | if (ret) | |
181 | return ret; | |
182 | } | |
183 | cds_list_for_each_entry(iter, &node->u.root.declaration_specifier, siblings) { | |
184 | iter->parent = node; | |
185 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
186 | if (ret) | |
187 | return ret; | |
188 | } | |
189 | cds_list_for_each_entry(iter, &node->u.root.trace, siblings) { | |
190 | iter->parent = node; | |
191 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
192 | if (ret) | |
193 | return ret; | |
194 | } | |
195 | cds_list_for_each_entry(iter, &node->u.root.stream, siblings) { | |
196 | iter->parent = node; | |
197 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
198 | if (ret) | |
199 | return ret; | |
200 | } | |
201 | cds_list_for_each_entry(iter, &node->u.root.event, siblings) { | |
202 | iter->parent = node; | |
203 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
204 | if (ret) | |
205 | return ret; | |
206 | } | |
207 | break; | |
208 | ||
209 | case NODE_EVENT: | |
210 | cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) { | |
211 | iter->parent = node; | |
212 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
213 | if (ret) | |
214 | return ret; | |
215 | } | |
216 | break; | |
217 | case NODE_STREAM: | |
218 | cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) { | |
219 | iter->parent = node; | |
220 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
221 | if (ret) | |
222 | return ret; | |
223 | } | |
224 | break; | |
225 | case NODE_TRACE: | |
226 | cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) { | |
227 | iter->parent = node; | |
228 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
229 | if (ret) | |
230 | return ret; | |
231 | } | |
232 | break; | |
233 | ||
234 | case NODE_CTF_EXPRESSION: | |
235 | depth++; | |
236 | cds_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) { | |
237 | iter->parent = node; | |
238 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
239 | if (ret) | |
240 | return ret; | |
241 | } | |
242 | cds_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) { | |
243 | iter->parent = node; | |
244 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
245 | if (ret) | |
246 | return ret; | |
247 | } | |
248 | depth--; | |
249 | break; | |
250 | case NODE_UNARY_EXPRESSION: | |
251 | return ctf_visitor_unary_expression(fd, depth, node); | |
252 | ||
253 | case NODE_TYPEDEF: | |
254 | depth++; | |
255 | cds_list_for_each_entry(iter, &node->u._typedef.declaration_specifier, siblings) { | |
256 | iter->parent = node; | |
257 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
258 | if (ret) | |
259 | return ret; | |
260 | } | |
261 | cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) { | |
262 | iter->parent = node; | |
263 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
264 | if (ret) | |
265 | return ret; | |
266 | } | |
267 | depth--; | |
268 | break; | |
269 | case NODE_TYPEALIAS_TARGET: | |
270 | depth++; | |
271 | cds_list_for_each_entry(iter, &node->u.typealias_target.declaration_specifier, siblings) { | |
272 | iter->parent = node; | |
273 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
274 | if (ret) | |
275 | return ret; | |
276 | } | |
277 | cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) { | |
278 | iter->parent = node; | |
279 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
280 | if (ret) | |
281 | return ret; | |
282 | } | |
283 | depth--; | |
284 | break; | |
285 | case NODE_TYPEALIAS_ALIAS: | |
286 | depth++; | |
287 | cds_list_for_each_entry(iter, &node->u.typealias_alias.declaration_specifier, siblings) { | |
288 | iter->parent = node; | |
289 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
290 | if (ret) | |
291 | return ret; | |
292 | } | |
293 | cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) { | |
294 | iter->parent = node; | |
295 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
296 | if (ret) | |
297 | return ret; | |
298 | } | |
299 | depth--; | |
300 | break; | |
301 | case NODE_TYPEALIAS: | |
302 | node->u.typealias.target->parent = node; | |
303 | ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.target); | |
304 | if (ret) | |
305 | return ret; | |
306 | node->u.typealias.alias->parent = node; | |
307 | ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.alias); | |
308 | if (ret) | |
309 | return ret; | |
310 | break; | |
311 | ||
312 | case NODE_TYPE_SPECIFIER: | |
313 | ret = ctf_visitor_type_specifier(fd, depth, node); | |
314 | if (ret) | |
315 | return ret; | |
316 | break; | |
317 | case NODE_POINTER: | |
318 | break; | |
319 | case NODE_TYPE_DECLARATOR: | |
320 | ret = ctf_visitor_type_declarator(fd, depth, node); | |
321 | if (ret) | |
322 | return ret; | |
323 | break; | |
324 | ||
325 | case NODE_FLOATING_POINT: | |
326 | cds_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) { | |
327 | iter->parent = node; | |
328 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
329 | if (ret) | |
330 | return ret; | |
331 | } | |
332 | break; | |
333 | case NODE_INTEGER: | |
334 | cds_list_for_each_entry(iter, &node->u.integer.expressions, siblings) { | |
335 | iter->parent = node; | |
336 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
337 | if (ret) | |
338 | return ret; | |
339 | } | |
340 | break; | |
341 | case NODE_STRING: | |
342 | cds_list_for_each_entry(iter, &node->u.string.expressions, siblings) { | |
343 | iter->parent = node; | |
344 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
345 | if (ret) | |
346 | return ret; | |
347 | } | |
348 | break; | |
349 | case NODE_ENUMERATOR: | |
350 | if (node->u.enumerator.id) | |
351 | fprintf(fd, " id=\"%s\"", node->u.enumerator.id); | |
352 | fprintf(fd, ">\n"); | |
353 | cds_list_for_each_entry(iter, &node->u.enumerator.values, siblings) { | |
354 | iter->parent = node; | |
355 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
356 | if (ret) | |
357 | return ret; | |
358 | } | |
359 | break; | |
360 | case NODE_ENUM: | |
361 | if (node->u._struct.name) | |
362 | fprintf(fd, "<enum name=\"%s\">\n", | |
363 | node->u._enum.enum_id); | |
364 | else | |
365 | fprintf(fd, "<enum >\n"); | |
366 | depth++; | |
367 | ||
368 | if (node->u._enum.container_type) { | |
369 | node->u._enum.container_type->parent = node; | |
370 | ret = ctf_visitor_parent_links(fd, depth + 1, node->u._enum.container_type); | |
371 | if (ret) | |
372 | return ret; | |
373 | } | |
374 | ||
375 | cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) { | |
376 | iter->parent = node; | |
377 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
378 | if (ret) | |
379 | return ret; | |
380 | } | |
381 | depth--; | |
382 | break; | |
383 | case NODE_STRUCT_OR_VARIANT_DECLARATION: | |
384 | cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.declaration_specifier, siblings) { | |
385 | iter->parent = node; | |
386 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
387 | if (ret) | |
388 | return ret; | |
389 | } | |
390 | cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, 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_VARIANT: | |
398 | if (node->u.variant.choice) | |
399 | fprintf(fd, " choice=\"%s\"", node->u.variant.choice); | |
400 | cds_list_for_each_entry(iter, &node->u.variant.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 | case NODE_STRUCT: | |
408 | cds_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) { | |
409 | iter->parent = node; | |
410 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); | |
411 | if (ret) | |
412 | return ret; | |
413 | } | |
414 | break; | |
415 | ||
416 | case NODE_UNKNOWN: | |
417 | default: | |
418 | fprintf(fd, "[error] %s: unknown node type %d\n", __func__, | |
419 | (int) node->type); | |
420 | return -EINVAL; | |
421 | } | |
422 | return ret; | |
423 | } |