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