Create copy of integer declaration before applying base-16 for pointers
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-parent-links.c
CommitLineData
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>
c8b219a3 27#include <babeltrace/babeltrace.h>
67905e42
MD
28#include <babeltrace/list.h>
29#include "ctf-scanner.h"
30#include "ctf-parser.h"
31#include "ctf-ast.h"
32
34f7b02c 33#define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
67905e42
MD
34
35static
36int ctf_visitor_unary_expression(FILE *fd, int depth, struct ctf_node *node)
37{
38 int ret = 0;
39
40 switch (node->u.unary_expression.link) {
41 case UNARY_LINK_UNKNOWN:
42 case UNARY_DOTLINK:
43 case UNARY_ARROWLINK:
44 case UNARY_DOTDOTDOT:
45 break;
46 default:
47 fprintf(fd, "[error] %s: unknown expression link type %d\n", __func__,
48 (int) node->u.unary_expression.link);
49 return -EINVAL;
50 }
51
52 switch (node->u.unary_expression.type) {
53 case UNARY_STRING:
54 case UNARY_SIGNED_CONSTANT:
55 case UNARY_UNSIGNED_CONSTANT:
56 break;
57 case UNARY_SBRAC:
58 node->u.unary_expression.u.sbrac_exp->parent = node;
59 ret = ctf_visitor_unary_expression(fd, depth + 1,
60 node->u.unary_expression.u.sbrac_exp);
61 if (ret)
62 return ret;
63 break;
64 case UNARY_NESTED:
65 node->u.unary_expression.u.nested_exp->parent = node;
66 ret = ctf_visitor_unary_expression(fd, depth + 1,
67 node->u.unary_expression.u.nested_exp);
68 if (ret)
69 return ret;
70 break;
71
72 case UNARY_UNKNOWN:
73 default:
74 fprintf(fd, "[error] %s: unknown expression type %d\n", __func__,
75 (int) node->u.unary_expression.type);
76 return -EINVAL;
77 }
78 return 0;
79}
80
81static
82int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node)
83{
3e11b713
MD
84 int ret;
85
67905e42
MD
86 switch (node->u.type_specifier.type) {
87 case TYPESPEC_VOID:
88 case TYPESPEC_CHAR:
89 case TYPESPEC_SHORT:
90 case TYPESPEC_INT:
91 case TYPESPEC_LONG:
92 case TYPESPEC_FLOAT:
93 case TYPESPEC_DOUBLE:
94 case TYPESPEC_SIGNED:
95 case TYPESPEC_UNSIGNED:
96 case TYPESPEC_BOOL:
97 case TYPESPEC_COMPLEX:
3888a159 98 case TYPESPEC_IMAGINARY:
67905e42
MD
99 case TYPESPEC_CONST:
100 case TYPESPEC_ID_TYPE:
101 break;
3e11b713
MD
102 case TYPESPEC_FLOATING_POINT:
103 case TYPESPEC_INTEGER:
104 case TYPESPEC_STRING:
105 case TYPESPEC_STRUCT:
106 case TYPESPEC_VARIANT:
107 case TYPESPEC_ENUM:
108 node->u.type_specifier.node->parent = node;
109 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.type_specifier.node);
110 if (ret)
111 return ret;
112 break;
67905e42
MD
113
114 case TYPESPEC_UNKNOWN:
115 default:
116 fprintf(fd, "[error] %s: unknown type specifier %d\n", __func__,
117 (int) node->u.type_specifier.type);
118 return -EINVAL;
119 }
120 return 0;
121}
122
123static
124int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node)
125{
126 int ret = 0;
127 struct ctf_node *iter;
128
129 depth++;
130
131 if (!cds_list_empty(&node->u.type_declarator.pointers)) {
132 cds_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
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;
147 ret = ctf_visitor_parent_links(fd, depth + 1,
148 node->u.type_declarator.u.nested.type_declarator);
149 if (ret)
150 return ret;
151 }
3e11b713
MD
152 if (node->u.type_declarator.u.nested.length) {
153 node->u.type_declarator.u.nested.length->parent = node;
154 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.type_declarator.u.nested.length);
67905e42
MD
155 if (ret)
156 return ret;
157 }
158 if (node->u.type_declarator.bitfield_len) {
159 node->u.type_declarator.bitfield_len = node;
160 ret = ctf_visitor_parent_links(fd, depth + 1,
161 node->u.type_declarator.bitfield_len);
162 if (ret)
163 return ret;
164 }
165 break;
166 case TYPEDEC_UNKNOWN:
167 default:
168 fprintf(fd, "[error] %s: unknown type declarator %d\n", __func__,
169 (int) node->u.type_declarator.type);
170 return -EINVAL;
171 }
172 depth--;
173 return 0;
174}
175
176int ctf_visitor_parent_links(FILE *fd, int depth, struct ctf_node *node)
177{
178 int ret = 0;
179 struct ctf_node *iter;
180
181 switch (node->type) {
182 case NODE_ROOT:
3e11b713 183 cds_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) {
67905e42
MD
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++;
3e11b713
MD
255 node->u._typedef.type_specifier_list->parent = node;
256 ret = ctf_visitor_parent_links(fd, depth + 1, node->u._typedef.type_specifier_list);
257 if (ret)
258 return ret;
67905e42
MD
259 cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
260 iter->parent = node;
261 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
262 if (ret)
263 return ret;
264 }
265 depth--;
266 break;
267 case NODE_TYPEALIAS_TARGET:
268 depth++;
3e11b713
MD
269 node->u.typealias_target.type_specifier_list->parent = node;
270 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_target.type_specifier_list);
271 if (ret)
272 return ret;
67905e42
MD
273 cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
274 iter->parent = node;
275 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
276 if (ret)
277 return ret;
278 }
279 depth--;
280 break;
281 case NODE_TYPEALIAS_ALIAS:
282 depth++;
3e11b713
MD
283 node->u.typealias_alias.type_specifier_list->parent = node;
284 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_alias.type_specifier_list);
285 if (ret)
286 return ret;
67905e42
MD
287 cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, 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_TYPEALIAS:
296 node->u.typealias.target->parent = node;
297 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.target);
298 if (ret)
299 return ret;
300 node->u.typealias.alias->parent = node;
301 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.alias);
302 if (ret)
303 return ret;
304 break;
305
3e11b713
MD
306 case NODE_TYPE_SPECIFIER_LIST:
307 cds_list_for_each_entry(iter, &node->u.type_specifier_list.head, siblings) {
308 iter->parent = node;
309 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
310 if (ret)
311 return ret;
312 }
313 break;
314
67905e42
MD
315 case NODE_TYPE_SPECIFIER:
316 ret = ctf_visitor_type_specifier(fd, depth, node);
317 if (ret)
318 return ret;
319 break;
320 case NODE_POINTER:
321 break;
322 case NODE_TYPE_DECLARATOR:
323 ret = ctf_visitor_type_declarator(fd, depth, node);
324 if (ret)
325 return ret;
326 break;
327
328 case NODE_FLOATING_POINT:
329 cds_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
330 iter->parent = node;
331 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
332 if (ret)
333 return ret;
334 }
335 break;
336 case NODE_INTEGER:
337 cds_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
338 iter->parent = node;
339 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
340 if (ret)
341 return ret;
342 }
343 break;
344 case NODE_STRING:
345 cds_list_for_each_entry(iter, &node->u.string.expressions, 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 case NODE_ENUMERATOR:
67905e42
MD
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:
67905e42 361 depth++;
6743829a
MD
362 if (node->u._enum.container_type) {
363 ret = ctf_visitor_parent_links(fd, depth + 1, node->u._enum.container_type);
364 if (ret)
365 return ret;
366 }
67905e42
MD
367
368 cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
369 iter->parent = node;
370 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
371 if (ret)
372 return ret;
373 }
374 depth--;
375 break;
376 case NODE_STRUCT_OR_VARIANT_DECLARATION:
3e11b713
MD
377 node->u.struct_or_variant_declaration.type_specifier_list->parent = node;
378 ret = ctf_visitor_parent_links(fd, depth + 1,
379 node->u.struct_or_variant_declaration.type_specifier_list);
380 if (ret)
381 return ret;
67905e42
MD
382 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, 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_VARIANT:
67905e42
MD
390 cds_list_for_each_entry(iter, &node->u.variant.declaration_list, 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_STRUCT:
398 cds_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
399 iter->parent = node;
400 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
401 if (ret)
402 return ret;
403 }
b7e35bad
MD
404 cds_list_for_each_entry(iter, &node->u._struct.min_align,
405 siblings) {
406 iter->parent = node;
407 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
408 if (ret)
409 return ret;
410 }
67905e42
MD
411 break;
412
413 case NODE_UNKNOWN:
414 default:
415 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
416 (int) node->type);
417 return -EINVAL;
418 }
419 return ret;
420}
This page took 0.039063 seconds and 4 git commands to generate.