Update sequence (type specifier -> field ref), fix definition lookup
[babeltrace.git] / formats / ctf / metadata / ctf-visitor-semantic-validator.c
1 /*
2 * ctf-visitor-semantic-validator.c
3 *
4 * Common Trace Format Metadata Semantic Validator.
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/babeltrace.h>
28 #include <babeltrace/list.h>
29 #include "ctf-scanner.h"
30 #include "ctf-parser.h"
31 #include "ctf-ast.h"
32
33 #define _cds_list_first_entry(ptr, type, member) \
34 cds_list_entry((ptr)->next, type, member)
35
36 #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
37
38 static
39 int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node);
40
41 static
42 int ctf_visitor_unary_expression(FILE *fd, int depth, struct ctf_node *node)
43 {
44 struct ctf_node *iter;
45 int is_ctf_exp = 0, is_ctf_exp_left = 0;
46
47 switch (node->parent->type) {
48 case NODE_CTF_EXPRESSION:
49 is_ctf_exp = 1;
50 cds_list_for_each_entry(iter, &node->parent->u.ctf_expression.left,
51 siblings) {
52 if (iter == node) {
53 is_ctf_exp_left = 1;
54 /*
55 * We are a left child of a ctf expression.
56 * We are only allowed to be a string.
57 */
58 if (node->u.unary_expression.type != UNARY_STRING) {
59 fprintf(fd, "[error]: semantic error (left child of a ctf expression is only allowed to be a string)\n");
60
61 goto errperm;
62 }
63 break;
64 }
65 }
66 /* Right child of a ctf expression can be any type of unary exp. */
67 break; /* OK */
68 case NODE_TYPE_DECLARATOR:
69 /*
70 * We are the length of a type declarator.
71 */
72 switch (node->u.unary_expression.type) {
73 case UNARY_UNSIGNED_CONSTANT:
74 case UNARY_STRING:
75 break;
76 default:
77 fprintf(fd, "[error]: semantic error (children of type declarator and enum can only be unsigned numeric constants or references to fields (a.b.c))\n");
78 goto errperm;
79 }
80 break; /* OK */
81
82 case NODE_STRUCT:
83 /*
84 * We are the size of a struct align attribute.
85 */
86 switch (node->u.unary_expression.type) {
87 case UNARY_UNSIGNED_CONSTANT:
88 break;
89 default:
90 fprintf(fd, "[error]: semantic error (structure alignment attribute can only be unsigned numeric constants)\n");
91 goto errperm;
92 }
93 break;
94
95 case NODE_ENUMERATOR:
96 /* The enumerator's parent has validated its validity already. */
97 break; /* OK */
98
99 case NODE_UNARY_EXPRESSION:
100 /*
101 * We disallow nested unary expressions and "sbrac" unary
102 * expressions.
103 */
104 fprintf(fd, "[error]: semantic error (nested unary expressions not allowed ( () and [] ))\n");
105 goto errperm;
106
107 case NODE_ROOT:
108 case NODE_EVENT:
109 case NODE_STREAM:
110 case NODE_TRACE:
111 case NODE_TYPEDEF:
112 case NODE_TYPEALIAS_TARGET:
113 case NODE_TYPEALIAS_ALIAS:
114 case NODE_TYPEALIAS:
115 case NODE_TYPE_SPECIFIER:
116 case NODE_POINTER:
117 case NODE_FLOATING_POINT:
118 case NODE_INTEGER:
119 case NODE_STRING:
120 case NODE_ENUM:
121 case NODE_STRUCT_OR_VARIANT_DECLARATION:
122 case NODE_VARIANT:
123 default:
124 goto errinval;
125 }
126
127 switch (node->u.unary_expression.link) {
128 case UNARY_LINK_UNKNOWN:
129 /* We don't allow empty link except on the first node of the list */
130 if (is_ctf_exp && _cds_list_first_entry(is_ctf_exp_left ?
131 &node->parent->u.ctf_expression.left :
132 &node->parent->u.ctf_expression.right,
133 struct ctf_node,
134 siblings) != node) {
135 fprintf(fd, "[error]: semantic error (empty link not allowed except on first node of unary expression (need to separate nodes with \".\" or \"->\")\n");
136 goto errperm;
137 }
138 break; /* OK */
139 case UNARY_DOTLINK:
140 case UNARY_ARROWLINK:
141 /* We only allow -> and . links between children of ctf_expression. */
142 if (node->parent->type != NODE_CTF_EXPRESSION) {
143 fprintf(fd, "[error]: semantic error (links \".\" and \"->\" are only allowed as children of ctf expression)\n");
144 goto errperm;
145 }
146 /*
147 * Only strings can be separated linked by . or ->.
148 * This includes "", '' and non-quoted identifiers.
149 */
150 if (node->u.unary_expression.type != UNARY_STRING) {
151 fprintf(fd, "[error]: semantic error (links \".\" and \"->\" are only allowed to separate strings and identifiers)\n");
152 goto errperm;
153 }
154 /* We don't allow link on the first node of the list */
155 if (is_ctf_exp && _cds_list_first_entry(is_ctf_exp_left ?
156 &node->parent->u.ctf_expression.left :
157 &node->parent->u.ctf_expression.right,
158 struct ctf_node,
159 siblings) == node) {
160 fprintf(fd, "[error]: semantic error (links \".\" and \"->\" are not allowed before first node of the unary expression list)\n");
161 goto errperm;
162 }
163 break;
164 case UNARY_DOTDOTDOT:
165 /* We only allow ... link between children of enumerator. */
166 if (node->parent->type != NODE_ENUMERATOR) {
167 fprintf(fd, "[error]: semantic error (link \"...\" is only allowed within enumerator)\n");
168 goto errperm;
169 }
170 /* We don't allow link on the first node of the list */
171 if (_cds_list_first_entry(&node->parent->u.enumerator.values,
172 struct ctf_node,
173 siblings) == node) {
174 fprintf(fd, "[error]: semantic error (link \"...\" is not allowed on the first node of the unary expression list)\n");
175 goto errperm;
176 }
177 break;
178 default:
179 fprintf(fd, "[error] %s: unknown expression link type %d\n", __func__,
180 (int) node->u.unary_expression.link);
181 return -EINVAL;
182 }
183 return 0;
184
185 errinval:
186 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
187 node_type(node->parent), node_type(node));
188 return -EINVAL; /* Incoherent structure */
189
190 errperm:
191 fprintf(fd, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__,
192 node_type(node->parent), node_type(node));
193 return -EPERM; /* Structure not allowed */
194 }
195
196 static
197 int ctf_visitor_type_specifier_list(FILE *fd, int depth, struct ctf_node *node)
198 {
199 switch (node->parent->type) {
200 case NODE_CTF_EXPRESSION:
201 case NODE_TYPE_DECLARATOR:
202 case NODE_TYPEDEF:
203 case NODE_TYPEALIAS_TARGET:
204 case NODE_TYPEALIAS_ALIAS:
205 case NODE_ENUM:
206 case NODE_STRUCT_OR_VARIANT_DECLARATION:
207 case NODE_ROOT:
208 break; /* OK */
209
210 case NODE_EVENT:
211 case NODE_STREAM:
212 case NODE_TRACE:
213 case NODE_UNARY_EXPRESSION:
214 case NODE_TYPEALIAS:
215 case NODE_TYPE_SPECIFIER:
216 case NODE_TYPE_SPECIFIER_LIST:
217 case NODE_POINTER:
218 case NODE_FLOATING_POINT:
219 case NODE_INTEGER:
220 case NODE_STRING:
221 case NODE_ENUMERATOR:
222 case NODE_VARIANT:
223 case NODE_STRUCT:
224 default:
225 goto errinval;
226 }
227 return 0;
228 errinval:
229 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
230 node_type(node->parent), node_type(node));
231 return -EINVAL; /* Incoherent structure */
232 }
233
234 static
235 int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node)
236 {
237 switch (node->parent->type) {
238 case NODE_TYPE_SPECIFIER_LIST:
239 break; /* OK */
240
241 case NODE_CTF_EXPRESSION:
242 case NODE_TYPE_DECLARATOR:
243 case NODE_TYPEDEF:
244 case NODE_TYPEALIAS_TARGET:
245 case NODE_TYPEALIAS_ALIAS:
246 case NODE_ENUM:
247 case NODE_STRUCT_OR_VARIANT_DECLARATION:
248 case NODE_ROOT:
249 case NODE_EVENT:
250 case NODE_STREAM:
251 case NODE_TRACE:
252 case NODE_UNARY_EXPRESSION:
253 case NODE_TYPEALIAS:
254 case NODE_TYPE_SPECIFIER:
255 case NODE_POINTER:
256 case NODE_FLOATING_POINT:
257 case NODE_INTEGER:
258 case NODE_STRING:
259 case NODE_ENUMERATOR:
260 case NODE_VARIANT:
261 case NODE_STRUCT:
262 default:
263 goto errinval;
264 }
265 return 0;
266 errinval:
267 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
268 node_type(node->parent), node_type(node));
269 return -EINVAL; /* Incoherent structure */
270 }
271
272 static
273 int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node)
274 {
275 int ret = 0;
276 struct ctf_node *iter;
277
278 depth++;
279
280 switch (node->parent->type) {
281 case NODE_TYPE_DECLARATOR:
282 /*
283 * A nested type declarator is not allowed to contain pointers.
284 */
285 if (!cds_list_empty(&node->u.type_declarator.pointers))
286 goto errperm;
287 break; /* OK */
288 case NODE_TYPEALIAS_TARGET:
289 break; /* OK */
290 case NODE_TYPEALIAS_ALIAS:
291 /*
292 * Only accept alias name containing:
293 * - identifier
294 * - identifier * (any number of pointers)
295 * NOT accepting alias names containing [] (would otherwise
296 * cause semantic clash for later declarations of
297 * arrays/sequences of elements, where elements could be
298 * arrays/sequences themselves (if allowed in typealias).
299 * NOT accepting alias with identifier. The declarator should
300 * be either empty or contain pointer(s).
301 */
302 if (node->u.type_declarator.type == TYPEDEC_NESTED)
303 goto errperm;
304 switch (node->u.type_declarator.type) {
305 case TYPESPEC_FLOATING_POINT:
306 case TYPESPEC_INTEGER:
307 case TYPESPEC_STRING:
308 case TYPESPEC_STRUCT:
309 case TYPESPEC_VARIANT:
310 case TYPESPEC_ENUM:
311 if (cds_list_empty(&node->u.type_declarator.pointers))
312 goto errperm;
313 break;
314 default:
315 break;
316 }
317 if (node->u.type_declarator.type == TYPEDEC_ID &&
318 node->u.type_declarator.u.id != NULL)
319 goto errperm;
320 break; /* OK */
321 case NODE_TYPEDEF:
322 case NODE_STRUCT_OR_VARIANT_DECLARATION:
323 break; /* OK */
324
325 case NODE_ROOT:
326 case NODE_EVENT:
327 case NODE_STREAM:
328 case NODE_TRACE:
329 case NODE_CTF_EXPRESSION:
330 case NODE_UNARY_EXPRESSION:
331 case NODE_TYPEALIAS:
332 case NODE_TYPE_SPECIFIER:
333 case NODE_POINTER:
334 case NODE_FLOATING_POINT:
335 case NODE_INTEGER:
336 case NODE_STRING:
337 case NODE_ENUMERATOR:
338 case NODE_ENUM:
339 case NODE_VARIANT:
340 case NODE_STRUCT:
341 default:
342 goto errinval;
343 }
344
345 cds_list_for_each_entry(iter, &node->u.type_declarator.pointers,
346 siblings) {
347 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
348 if (ret)
349 return ret;
350 }
351
352 switch (node->u.type_declarator.type) {
353 case TYPEDEC_ID:
354 break;
355 case TYPEDEC_NESTED:
356 {
357 if (node->u.type_declarator.u.nested.type_declarator) {
358 ret = _ctf_visitor_semantic_check(fd, depth + 1,
359 node->u.type_declarator.u.nested.type_declarator);
360 if (ret)
361 return ret;
362 }
363 cds_list_for_each_entry(iter, &node->u.type_declarator.u.nested.length,
364 siblings) {
365 if (iter->type != NODE_UNARY_EXPRESSION) {
366 fprintf(fd, "[error] %s: expecting unary expression as length\n", __func__);
367 return -EINVAL;
368 }
369 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
370 if (ret)
371 return ret;
372 }
373 if (node->u.type_declarator.bitfield_len) {
374 ret = _ctf_visitor_semantic_check(fd, depth + 1,
375 node->u.type_declarator.bitfield_len);
376 if (ret)
377 return ret;
378 }
379 break;
380 }
381 case TYPEDEC_UNKNOWN:
382 default:
383 fprintf(fd, "[error] %s: unknown type declarator %d\n", __func__,
384 (int) node->u.type_declarator.type);
385 return -EINVAL;
386 }
387 depth--;
388 return 0;
389
390 errinval:
391 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
392 node_type(node->parent), node_type(node));
393 return -EINVAL; /* Incoherent structure */
394
395 errperm:
396 fprintf(fd, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__,
397 node_type(node->parent), node_type(node));
398 return -EPERM; /* Structure not allowed */
399 }
400
401 static
402 int _ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node)
403 {
404 int ret = 0;
405 struct ctf_node *iter;
406
407 switch (node->type) {
408 case NODE_ROOT:
409 cds_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) {
410 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
411 if (ret)
412 return ret;
413 }
414 cds_list_for_each_entry(iter, &node->u.root.trace, siblings) {
415 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
416 if (ret)
417 return ret;
418 }
419 cds_list_for_each_entry(iter, &node->u.root.stream, siblings) {
420 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
421 if (ret)
422 return ret;
423 }
424 cds_list_for_each_entry(iter, &node->u.root.event, siblings) {
425 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
426 if (ret)
427 return ret;
428 }
429 break;
430
431 case NODE_EVENT:
432 switch (node->parent->type) {
433 case NODE_ROOT:
434 break; /* OK */
435 default:
436 goto errinval;
437 }
438
439 cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
440 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
441 if (ret)
442 return ret;
443 }
444 break;
445 case NODE_STREAM:
446 switch (node->parent->type) {
447 case NODE_ROOT:
448 break; /* OK */
449 default:
450 goto errinval;
451 }
452
453 cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
454 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
455 if (ret)
456 return ret;
457 }
458 break;
459 case NODE_TRACE:
460 switch (node->parent->type) {
461 case NODE_ROOT:
462 break; /* OK */
463 default:
464 goto errinval;
465 }
466
467 cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
468 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
469 if (ret)
470 return ret;
471 }
472 break;
473
474 case NODE_CTF_EXPRESSION:
475 switch (node->parent->type) {
476 case NODE_ROOT:
477 case NODE_EVENT:
478 case NODE_STREAM:
479 case NODE_TRACE:
480 case NODE_FLOATING_POINT:
481 case NODE_INTEGER:
482 case NODE_STRING:
483 break; /* OK */
484
485 case NODE_CTF_EXPRESSION:
486 case NODE_UNARY_EXPRESSION:
487 case NODE_TYPEDEF:
488 case NODE_TYPEALIAS_TARGET:
489 case NODE_TYPEALIAS_ALIAS:
490 case NODE_STRUCT_OR_VARIANT_DECLARATION:
491 case NODE_TYPEALIAS:
492 case NODE_TYPE_SPECIFIER:
493 case NODE_TYPE_SPECIFIER_LIST:
494 case NODE_POINTER:
495 case NODE_TYPE_DECLARATOR:
496 case NODE_ENUMERATOR:
497 case NODE_ENUM:
498 case NODE_VARIANT:
499 case NODE_STRUCT:
500 default:
501 goto errinval;
502 }
503
504 depth++;
505 cds_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) {
506 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
507 if (ret)
508 return ret;
509 }
510 cds_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) {
511 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
512 if (ret)
513 return ret;
514 }
515 depth--;
516 break;
517 case NODE_UNARY_EXPRESSION:
518 return ctf_visitor_unary_expression(fd, depth, node);
519
520 case NODE_TYPEDEF:
521 switch (node->parent->type) {
522 case NODE_ROOT:
523 case NODE_EVENT:
524 case NODE_STREAM:
525 case NODE_TRACE:
526 case NODE_VARIANT:
527 case NODE_STRUCT:
528 break; /* OK */
529
530 case NODE_CTF_EXPRESSION:
531 case NODE_UNARY_EXPRESSION:
532 case NODE_TYPEDEF:
533 case NODE_TYPEALIAS_TARGET:
534 case NODE_TYPEALIAS_ALIAS:
535 case NODE_TYPEALIAS:
536 case NODE_STRUCT_OR_VARIANT_DECLARATION:
537 case NODE_TYPE_SPECIFIER:
538 case NODE_TYPE_SPECIFIER_LIST:
539 case NODE_POINTER:
540 case NODE_TYPE_DECLARATOR:
541 case NODE_FLOATING_POINT:
542 case NODE_INTEGER:
543 case NODE_STRING:
544 case NODE_ENUMERATOR:
545 case NODE_ENUM:
546 default:
547 goto errinval;
548 }
549
550 depth++;
551 ret = _ctf_visitor_semantic_check(fd, depth + 1,
552 node->u._typedef.type_specifier_list);
553 if (ret)
554 return ret;
555 cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
556 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
557 if (ret)
558 return ret;
559 }
560 depth--;
561 break;
562 case NODE_TYPEALIAS_TARGET:
563 {
564 int nr_declarators;
565
566 switch (node->parent->type) {
567 case NODE_TYPEALIAS:
568 break; /* OK */
569 default:
570 goto errinval;
571 }
572
573 depth++;
574 ret = _ctf_visitor_semantic_check(fd, depth + 1,
575 node->u.typealias_target.type_specifier_list);
576 if (ret)
577 return ret;
578 nr_declarators = 0;
579 cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
580 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
581 if (ret)
582 return ret;
583 nr_declarators++;
584 }
585 if (nr_declarators > 1) {
586 fprintf(fd, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__, nr_declarators);
587
588 return -EINVAL;
589 }
590 depth--;
591 break;
592 }
593 case NODE_TYPEALIAS_ALIAS:
594 {
595 int nr_declarators;
596
597 switch (node->parent->type) {
598 case NODE_TYPEALIAS:
599 break; /* OK */
600 default:
601 goto errinval;
602 }
603
604 depth++;
605 ret = _ctf_visitor_semantic_check(fd, depth + 1,
606 node->u.typealias_alias.type_specifier_list);
607 if (ret)
608 return ret;
609 nr_declarators = 0;
610 cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) {
611 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
612 if (ret)
613 return ret;
614 nr_declarators++;
615 }
616 if (nr_declarators > 1) {
617 fprintf(fd, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__, nr_declarators);
618
619 return -EINVAL;
620 }
621 depth--;
622 break;
623 }
624 case NODE_TYPEALIAS:
625 switch (node->parent->type) {
626 case NODE_ROOT:
627 case NODE_EVENT:
628 case NODE_STREAM:
629 case NODE_TRACE:
630 case NODE_VARIANT:
631 case NODE_STRUCT:
632 break; /* OK */
633
634 case NODE_CTF_EXPRESSION:
635 case NODE_UNARY_EXPRESSION:
636 case NODE_TYPEDEF:
637 case NODE_TYPEALIAS_TARGET:
638 case NODE_TYPEALIAS_ALIAS:
639 case NODE_TYPEALIAS:
640 case NODE_STRUCT_OR_VARIANT_DECLARATION:
641 case NODE_TYPE_SPECIFIER:
642 case NODE_TYPE_SPECIFIER_LIST:
643 case NODE_POINTER:
644 case NODE_TYPE_DECLARATOR:
645 case NODE_FLOATING_POINT:
646 case NODE_INTEGER:
647 case NODE_STRING:
648 case NODE_ENUMERATOR:
649 case NODE_ENUM:
650 default:
651 goto errinval;
652 }
653
654 ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u.typealias.target);
655 if (ret)
656 return ret;
657 ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u.typealias.alias);
658 if (ret)
659 return ret;
660 break;
661
662 case NODE_TYPE_SPECIFIER_LIST:
663 ret = ctf_visitor_type_specifier_list(fd, depth, node);
664 if (ret)
665 return ret;
666 break;
667 case NODE_TYPE_SPECIFIER:
668 ret = ctf_visitor_type_specifier(fd, depth, node);
669 if (ret)
670 return ret;
671 break;
672 case NODE_POINTER:
673 switch (node->parent->type) {
674 case NODE_TYPE_DECLARATOR:
675 break; /* OK */
676 default:
677 goto errinval;
678 }
679 break;
680 case NODE_TYPE_DECLARATOR:
681 ret = ctf_visitor_type_declarator(fd, depth, node);
682 if (ret)
683 return ret;
684 break;
685
686 case NODE_FLOATING_POINT:
687 switch (node->parent->type) {
688 case NODE_TYPE_SPECIFIER:
689 break; /* OK */
690 default:
691 goto errinval;
692
693 case NODE_UNARY_EXPRESSION:
694 goto errperm;
695 }
696 cds_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
697 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
698 if (ret)
699 return ret;
700 }
701 break;
702 case NODE_INTEGER:
703 switch (node->parent->type) {
704 case NODE_TYPE_SPECIFIER:
705 break; /* OK */
706 default:
707 goto errinval;
708
709 }
710
711 cds_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
712 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
713 if (ret)
714 return ret;
715 }
716 break;
717 case NODE_STRING:
718 switch (node->parent->type) {
719 case NODE_TYPE_SPECIFIER:
720 break; /* OK */
721 default:
722 goto errinval;
723
724 case NODE_UNARY_EXPRESSION:
725 goto errperm;
726 }
727
728 cds_list_for_each_entry(iter, &node->u.string.expressions, siblings) {
729 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
730 if (ret)
731 return ret;
732 }
733 break;
734 case NODE_ENUMERATOR:
735 switch (node->parent->type) {
736 case NODE_ENUM:
737 break;
738 default:
739 goto errinval;
740 }
741 /*
742 * Enumerators are only allows to contain:
743 * numeric unary expression
744 * or num. unary exp. ... num. unary exp
745 */
746 {
747 int count = 0;
748
749 cds_list_for_each_entry(iter, &node->u.enumerator.values,
750 siblings) {
751 switch (count++) {
752 case 0: if (iter->type != NODE_UNARY_EXPRESSION
753 || (iter->u.unary_expression.type != UNARY_SIGNED_CONSTANT
754 && iter->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
755 || iter->u.unary_expression.link != UNARY_LINK_UNKNOWN) {
756 fprintf(fd, "[error]: semantic error (first unary expression of enumerator is unexpected)\n");
757 goto errperm;
758 }
759 break;
760 case 1: if (iter->type != NODE_UNARY_EXPRESSION
761 || (iter->u.unary_expression.type != UNARY_SIGNED_CONSTANT
762 && iter->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
763 || iter->u.unary_expression.link != UNARY_DOTDOTDOT) {
764 fprintf(fd, "[error]: semantic error (second unary expression of enumerator is unexpected)\n");
765 goto errperm;
766 }
767 break;
768 default:
769 goto errperm;
770 }
771 }
772 }
773
774 cds_list_for_each_entry(iter, &node->u.enumerator.values, siblings) {
775 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
776 if (ret)
777 return ret;
778 }
779 break;
780 case NODE_ENUM:
781 switch (node->parent->type) {
782 case NODE_TYPE_SPECIFIER:
783 break; /* OK */
784 default:
785 goto errinval;
786
787 case NODE_UNARY_EXPRESSION:
788 goto errperm;
789 }
790
791 depth++;
792 ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u._enum.container_type);
793 if (ret)
794 return ret;
795
796 cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
797 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
798 if (ret)
799 return ret;
800 }
801 depth--;
802 break;
803 case NODE_STRUCT_OR_VARIANT_DECLARATION:
804 switch (node->parent->type) {
805 case NODE_STRUCT:
806 case NODE_VARIANT:
807 break;
808 default:
809 goto errinval;
810 }
811 ret = _ctf_visitor_semantic_check(fd, depth + 1,
812 node->u.struct_or_variant_declaration.type_specifier_list);
813 if (ret)
814 return ret;
815 cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) {
816 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
817 if (ret)
818 return ret;
819 }
820 break;
821 case NODE_VARIANT:
822 switch (node->parent->type) {
823 case NODE_TYPE_SPECIFIER:
824 break; /* OK */
825 default:
826 goto errinval;
827
828 case NODE_UNARY_EXPRESSION:
829 goto errperm;
830 }
831 cds_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) {
832 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
833 if (ret)
834 return ret;
835 }
836 break;
837
838 case NODE_STRUCT:
839 switch (node->parent->type) {
840 case NODE_TYPE_SPECIFIER:
841 break; /* OK */
842 default:
843 goto errinval;
844
845 case NODE_UNARY_EXPRESSION:
846 goto errperm;
847 }
848 cds_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
849 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
850 if (ret)
851 return ret;
852 }
853 break;
854
855 case NODE_UNKNOWN:
856 default:
857 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
858 (int) node->type);
859 return -EINVAL;
860 }
861 return ret;
862
863 errinval:
864 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
865 node_type(node->parent), node_type(node));
866 return -EINVAL; /* Incoherent structure */
867
868 errperm:
869 fprintf(fd, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__,
870 node_type(node->parent), node_type(node));
871 return -EPERM; /* Structure not allowed */
872 }
873
874 int ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node)
875 {
876 int ret = 0;
877
878 /*
879 * First make sure we create the parent links for all children. Let's
880 * take the safe route and recreate them at each validation, just in
881 * case the structure has changed.
882 */
883 printf_verbose("CTF visitor: parent links creation... ");
884 ret = ctf_visitor_parent_links(fd, depth, node);
885 if (ret)
886 return ret;
887 printf_verbose("done.\n");
888 printf_verbose("CTF visitor: semantic check... ");
889 ret = _ctf_visitor_semantic_check(fd, depth, node);
890 if (ret)
891 return ret;
892 printf_verbose("done.\n");
893 return ret;
894 }
This page took 0.047638 seconds and 5 git commands to generate.