CTF metadata structure generation code finished
[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>
27#include <babeltrace/list.h>
28#include "ctf-scanner.h"
29#include "ctf-parser.h"
30#include "ctf-ast.h"
31
34f7b02c 32#define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
67905e42
MD
33
34static
35int 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
80static
81int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node)
82{
3e11b713
MD
83 int ret;
84
67905e42
MD
85 switch (node->u.type_specifier.type) {
86 case TYPESPEC_VOID:
87 case TYPESPEC_CHAR:
88 case TYPESPEC_SHORT:
89 case TYPESPEC_INT:
90 case TYPESPEC_LONG:
91 case TYPESPEC_FLOAT:
92 case TYPESPEC_DOUBLE:
93 case TYPESPEC_SIGNED:
94 case TYPESPEC_UNSIGNED:
95 case TYPESPEC_BOOL:
96 case TYPESPEC_COMPLEX:
3888a159 97 case TYPESPEC_IMAGINARY:
67905e42
MD
98 case TYPESPEC_CONST:
99 case TYPESPEC_ID_TYPE:
100 break;
3e11b713
MD
101 case TYPESPEC_FLOATING_POINT:
102 case TYPESPEC_INTEGER:
103 case TYPESPEC_STRING:
104 case TYPESPEC_STRUCT:
105 case TYPESPEC_VARIANT:
106 case TYPESPEC_ENUM:
107 node->u.type_specifier.node->parent = node;
108 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.type_specifier.node);
109 if (ret)
110 return ret;
111 break;
67905e42
MD
112
113 case TYPESPEC_UNKNOWN:
114 default:
115 fprintf(fd, "[error] %s: unknown type specifier %d\n", __func__,
116 (int) node->u.type_specifier.type);
117 return -EINVAL;
118 }
119 return 0;
120}
121
122static
123int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node)
124{
125 int ret = 0;
126 struct ctf_node *iter;
127
128 depth++;
129
130 if (!cds_list_empty(&node->u.type_declarator.pointers)) {
131 cds_list_for_each_entry(iter, &node->u.type_declarator.pointers,
132 siblings) {
133 iter->parent = node;
134 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
135 if (ret)
136 return ret;
137 }
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 }
3e11b713
MD
151 if (node->u.type_declarator.u.nested.length) {
152 node->u.type_declarator.u.nested.length->parent = node;
153 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.type_declarator.u.nested.length);
67905e42
MD
154 if (ret)
155 return ret;
156 }
157 if (node->u.type_declarator.bitfield_len) {
158 node->u.type_declarator.bitfield_len = node;
159 ret = ctf_visitor_parent_links(fd, depth + 1,
160 node->u.type_declarator.bitfield_len);
161 if (ret)
162 return ret;
163 }
164 break;
165 case TYPEDEC_UNKNOWN:
166 default:
167 fprintf(fd, "[error] %s: unknown type declarator %d\n", __func__,
168 (int) node->u.type_declarator.type);
169 return -EINVAL;
170 }
171 depth--;
172 return 0;
173}
174
175int ctf_visitor_parent_links(FILE *fd, int depth, struct ctf_node *node)
176{
177 int ret = 0;
178 struct ctf_node *iter;
179
180 switch (node->type) {
181 case NODE_ROOT:
3e11b713 182 cds_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) {
67905e42
MD
183 iter->parent = node;
184 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
185 if (ret)
186 return ret;
187 }
188 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
189 iter->parent = node;
190 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
191 if (ret)
192 return ret;
193 }
194 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
195 iter->parent = node;
196 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
197 if (ret)
198 return ret;
199 }
200 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
201 iter->parent = node;
202 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
203 if (ret)
204 return ret;
205 }
206 break;
207
208 case NODE_EVENT:
209 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
210 iter->parent = node;
211 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
212 if (ret)
213 return ret;
214 }
215 break;
216 case NODE_STREAM:
217 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
218 iter->parent = node;
219 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
220 if (ret)
221 return ret;
222 }
223 break;
224 case NODE_TRACE:
225 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
226 iter->parent = node;
227 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
228 if (ret)
229 return ret;
230 }
231 break;
232
233 case NODE_CTF_EXPRESSION:
234 depth++;
235 cds_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) {
236 iter->parent = node;
237 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
238 if (ret)
239 return ret;
240 }
241 cds_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) {
242 iter->parent = node;
243 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
244 if (ret)
245 return ret;
246 }
247 depth--;
248 break;
249 case NODE_UNARY_EXPRESSION:
250 return ctf_visitor_unary_expression(fd, depth, node);
251
252 case NODE_TYPEDEF:
253 depth++;
3e11b713
MD
254 node->u._typedef.type_specifier_list->parent = node;
255 ret = ctf_visitor_parent_links(fd, depth + 1, node->u._typedef.type_specifier_list);
256 if (ret)
257 return ret;
67905e42
MD
258 cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
259 iter->parent = node;
260 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
261 if (ret)
262 return ret;
263 }
264 depth--;
265 break;
266 case NODE_TYPEALIAS_TARGET:
267 depth++;
3e11b713
MD
268 node->u.typealias_target.type_specifier_list->parent = node;
269 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_target.type_specifier_list);
270 if (ret)
271 return ret;
67905e42
MD
272 cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
273 iter->parent = node;
274 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
275 if (ret)
276 return ret;
277 }
278 depth--;
279 break;
280 case NODE_TYPEALIAS_ALIAS:
281 depth++;
3e11b713
MD
282 node->u.typealias_alias.type_specifier_list->parent = node;
283 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_alias.type_specifier_list);
284 if (ret)
285 return ret;
67905e42
MD
286 cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) {
287 iter->parent = node;
288 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
289 if (ret)
290 return ret;
291 }
292 depth--;
293 break;
294 case NODE_TYPEALIAS:
295 node->u.typealias.target->parent = node;
296 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.target);
297 if (ret)
298 return ret;
299 node->u.typealias.alias->parent = node;
300 ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.alias);
301 if (ret)
302 return ret;
303 break;
304
3e11b713
MD
305 case NODE_TYPE_SPECIFIER_LIST:
306 cds_list_for_each_entry(iter, &node->u.type_specifier_list.head, siblings) {
307 iter->parent = node;
308 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
309 if (ret)
310 return ret;
311 }
312 break;
313
67905e42
MD
314 case NODE_TYPE_SPECIFIER:
315 ret = ctf_visitor_type_specifier(fd, depth, node);
316 if (ret)
317 return ret;
318 break;
319 case NODE_POINTER:
320 break;
321 case NODE_TYPE_DECLARATOR:
322 ret = ctf_visitor_type_declarator(fd, depth, node);
323 if (ret)
324 return ret;
325 break;
326
327 case NODE_FLOATING_POINT:
328 cds_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
329 iter->parent = node;
330 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
331 if (ret)
332 return ret;
333 }
334 break;
335 case NODE_INTEGER:
336 cds_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
337 iter->parent = node;
338 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
339 if (ret)
340 return ret;
341 }
342 break;
343 case NODE_STRING:
344 cds_list_for_each_entry(iter, &node->u.string.expressions, siblings) {
345 iter->parent = node;
346 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
347 if (ret)
348 return ret;
349 }
350 break;
351 case NODE_ENUMERATOR:
67905e42
MD
352 cds_list_for_each_entry(iter, &node->u.enumerator.values, siblings) {
353 iter->parent = node;
354 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
355 if (ret)
356 return ret;
357 }
358 break;
359 case NODE_ENUM:
67905e42 360 depth++;
3e11b713
MD
361 ret = ctf_visitor_parent_links(fd, depth + 1, node->u._enum.container_type);
362 if (ret)
363 return ret;
67905e42
MD
364
365 cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
366 iter->parent = node;
367 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
368 if (ret)
369 return ret;
370 }
371 depth--;
372 break;
373 case NODE_STRUCT_OR_VARIANT_DECLARATION:
3e11b713
MD
374 node->u.struct_or_variant_declaration.type_specifier_list->parent = node;
375 ret = ctf_visitor_parent_links(fd, depth + 1,
376 node->u.struct_or_variant_declaration.type_specifier_list);
377 if (ret)
378 return ret;
67905e42
MD
379 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) {
380 iter->parent = node;
381 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
382 if (ret)
383 return ret;
384 }
385 break;
386 case NODE_VARIANT:
67905e42
MD
387 cds_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) {
388 iter->parent = node;
389 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
390 if (ret)
391 return ret;
392 }
393 break;
394 case NODE_STRUCT:
395 cds_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
396 iter->parent = node;
397 ret = ctf_visitor_parent_links(fd, depth + 1, iter);
398 if (ret)
399 return ret;
400 }
401 break;
402
403 case NODE_UNKNOWN:
404 default:
405 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
406 (int) node->type);
407 return -EINVAL;
408 }
409 return ret;
410}
This page took 0.038495 seconds and 4 git commands to generate.