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