Add missing permission notice in each source file
[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
437 switch (node->type) {
438 case NODE_ROOT:
3122e6f0 439 bt_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) {
67905e42
MD
440 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
441 if (ret)
442 return ret;
443 }
3122e6f0 444 bt_list_for_each_entry(iter, &node->u.root.trace, siblings) {
67905e42
MD
445 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
446 if (ret)
447 return ret;
448 }
3122e6f0 449 bt_list_for_each_entry(iter, &node->u.root.stream, siblings) {
67905e42
MD
450 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
451 if (ret)
452 return ret;
453 }
3122e6f0 454 bt_list_for_each_entry(iter, &node->u.root.event, siblings) {
67905e42
MD
455 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
456 if (ret)
457 return ret;
458 }
459 break;
460
461 case NODE_EVENT:
462 switch (node->parent->type) {
463 case NODE_ROOT:
464 break; /* OK */
465 default:
466 goto errinval;
467 }
468
3122e6f0 469 bt_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) {
67905e42
MD
470 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
471 if (ret)
472 return ret;
473 }
474 break;
475 case NODE_STREAM:
476 switch (node->parent->type) {
477 case NODE_ROOT:
478 break; /* OK */
479 default:
480 goto errinval;
481 }
482
3122e6f0 483 bt_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) {
67905e42
MD
484 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
485 if (ret)
486 return ret;
487 }
488 break;
e2c76a4d
MD
489 case NODE_ENV:
490 switch (node->parent->type) {
491 case NODE_ROOT:
492 break; /* OK */
493 default:
494 goto errinval;
495 }
496
3122e6f0 497 bt_list_for_each_entry(iter, &node->u.env.declaration_list, siblings) {
e2c76a4d
MD
498 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
499 if (ret)
500 return ret;
501 }
502 break;
67905e42
MD
503 case NODE_TRACE:
504 switch (node->parent->type) {
505 case NODE_ROOT:
506 break; /* OK */
507 default:
508 goto errinval;
509 }
510
3122e6f0 511 bt_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) {
67905e42
MD
512 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
513 if (ret)
514 return ret;
515 }
516 break;
73d15916
MD
517 case NODE_CLOCK:
518 switch (node->parent->type) {
519 case NODE_ROOT:
520 break; /* OK */
521 default:
522 goto errinval;
523 }
524
3122e6f0 525 bt_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) {
73d15916
MD
526 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
527 if (ret)
528 return ret;
529 }
530 break;
f133896d
MD
531 case NODE_CALLSITE:
532 switch (node->parent->type) {
533 case NODE_ROOT:
534 break; /* OK */
535 default:
536 goto errinval;
537 }
73d15916 538
f133896d
MD
539 bt_list_for_each_entry(iter, &node->u.callsite.declaration_list, siblings) {
540 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
541 if (ret)
542 return ret;
543 }
544 break;
67905e42
MD
545
546 case NODE_CTF_EXPRESSION:
547 switch (node->parent->type) {
548 case NODE_ROOT:
549 case NODE_EVENT:
550 case NODE_STREAM:
e2c76a4d 551 case NODE_ENV:
67905e42 552 case NODE_TRACE:
73d15916 553 case NODE_CLOCK:
f133896d 554 case NODE_CALLSITE:
67905e42
MD
555 case NODE_FLOATING_POINT:
556 case NODE_INTEGER:
557 case NODE_STRING:
558 break; /* OK */
559
560 case NODE_CTF_EXPRESSION:
561 case NODE_UNARY_EXPRESSION:
562 case NODE_TYPEDEF:
563 case NODE_TYPEALIAS_TARGET:
564 case NODE_TYPEALIAS_ALIAS:
565 case NODE_STRUCT_OR_VARIANT_DECLARATION:
566 case NODE_TYPEALIAS:
567 case NODE_TYPE_SPECIFIER:
3e11b713 568 case NODE_TYPE_SPECIFIER_LIST:
67905e42
MD
569 case NODE_POINTER:
570 case NODE_TYPE_DECLARATOR:
571 case NODE_ENUMERATOR:
572 case NODE_ENUM:
573 case NODE_VARIANT:
574 case NODE_STRUCT:
575 default:
576 goto errinval;
577 }
578
579 depth++;
3122e6f0 580 bt_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) {
67905e42
MD
581 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
582 if (ret)
583 return ret;
584 }
3122e6f0 585 bt_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) {
67905e42
MD
586 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
587 if (ret)
588 return ret;
589 }
590 depth--;
591 break;
592 case NODE_UNARY_EXPRESSION:
593 return ctf_visitor_unary_expression(fd, depth, node);
594
595 case NODE_TYPEDEF:
596 switch (node->parent->type) {
597 case NODE_ROOT:
598 case NODE_EVENT:
599 case NODE_STREAM:
600 case NODE_TRACE:
601 case NODE_VARIANT:
602 case NODE_STRUCT:
603 break; /* OK */
604
605 case NODE_CTF_EXPRESSION:
606 case NODE_UNARY_EXPRESSION:
607 case NODE_TYPEDEF:
608 case NODE_TYPEALIAS_TARGET:
609 case NODE_TYPEALIAS_ALIAS:
610 case NODE_TYPEALIAS:
611 case NODE_STRUCT_OR_VARIANT_DECLARATION:
612 case NODE_TYPE_SPECIFIER:
3e11b713 613 case NODE_TYPE_SPECIFIER_LIST:
67905e42
MD
614 case NODE_POINTER:
615 case NODE_TYPE_DECLARATOR:
616 case NODE_FLOATING_POINT:
617 case NODE_INTEGER:
618 case NODE_STRING:
619 case NODE_ENUMERATOR:
620 case NODE_ENUM:
73d15916 621 case NODE_CLOCK:
f133896d 622 case NODE_CALLSITE:
e2c76a4d 623 case NODE_ENV:
67905e42
MD
624 default:
625 goto errinval;
626 }
627
628 depth++;
3e11b713
MD
629 ret = _ctf_visitor_semantic_check(fd, depth + 1,
630 node->u._typedef.type_specifier_list);
631 if (ret)
632 return ret;
3122e6f0 633 bt_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) {
67905e42
MD
634 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
635 if (ret)
636 return ret;
637 }
638 depth--;
639 break;
640 case NODE_TYPEALIAS_TARGET:
1ee8e81d
MD
641 {
642 int nr_declarators;
643
67905e42
MD
644 switch (node->parent->type) {
645 case NODE_TYPEALIAS:
646 break; /* OK */
647 default:
648 goto errinval;
649 }
650
651 depth++;
3e11b713
MD
652 ret = _ctf_visitor_semantic_check(fd, depth + 1,
653 node->u.typealias_target.type_specifier_list);
654 if (ret)
655 return ret;
1ee8e81d 656 nr_declarators = 0;
3122e6f0 657 bt_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) {
67905e42
MD
658 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
659 if (ret)
660 return ret;
1ee8e81d
MD
661 nr_declarators++;
662 }
663 if (nr_declarators > 1) {
664 fprintf(fd, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__, nr_declarators);
665
666 return -EINVAL;
67905e42
MD
667 }
668 depth--;
669 break;
1ee8e81d 670 }
67905e42 671 case NODE_TYPEALIAS_ALIAS:
1ee8e81d
MD
672 {
673 int nr_declarators;
674
67905e42
MD
675 switch (node->parent->type) {
676 case NODE_TYPEALIAS:
677 break; /* OK */
678 default:
679 goto errinval;
680 }
681
682 depth++;
3e11b713
MD
683 ret = _ctf_visitor_semantic_check(fd, depth + 1,
684 node->u.typealias_alias.type_specifier_list);
685 if (ret)
686 return ret;
1ee8e81d 687 nr_declarators = 0;
3122e6f0 688 bt_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) {
67905e42
MD
689 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
690 if (ret)
691 return ret;
1ee8e81d
MD
692 nr_declarators++;
693 }
694 if (nr_declarators > 1) {
695 fprintf(fd, "[error] %s: Too many declarators in typealias alias (%d, max is 1)\n", __func__, nr_declarators);
696
697 return -EINVAL;
67905e42
MD
698 }
699 depth--;
700 break;
1ee8e81d 701 }
67905e42
MD
702 case NODE_TYPEALIAS:
703 switch (node->parent->type) {
704 case NODE_ROOT:
705 case NODE_EVENT:
706 case NODE_STREAM:
707 case NODE_TRACE:
708 case NODE_VARIANT:
709 case NODE_STRUCT:
710 break; /* OK */
711
712 case NODE_CTF_EXPRESSION:
713 case NODE_UNARY_EXPRESSION:
714 case NODE_TYPEDEF:
715 case NODE_TYPEALIAS_TARGET:
716 case NODE_TYPEALIAS_ALIAS:
717 case NODE_TYPEALIAS:
718 case NODE_STRUCT_OR_VARIANT_DECLARATION:
719 case NODE_TYPE_SPECIFIER:
3e11b713 720 case NODE_TYPE_SPECIFIER_LIST:
67905e42
MD
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:
73d15916 728 case NODE_CLOCK:
f133896d 729 case NODE_CALLSITE:
e2c76a4d 730 case NODE_ENV:
67905e42
MD
731 default:
732 goto errinval;
733 }
734
735 ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u.typealias.target);
736 if (ret)
737 return ret;
738 ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u.typealias.alias);
739 if (ret)
740 return ret;
741 break;
742
3e11b713
MD
743 case NODE_TYPE_SPECIFIER_LIST:
744 ret = ctf_visitor_type_specifier_list(fd, depth, node);
745 if (ret)
746 return ret;
747 break;
67905e42
MD
748 case NODE_TYPE_SPECIFIER:
749 ret = ctf_visitor_type_specifier(fd, depth, node);
750 if (ret)
751 return ret;
752 break;
753 case NODE_POINTER:
754 switch (node->parent->type) {
755 case NODE_TYPE_DECLARATOR:
756 break; /* OK */
757 default:
758 goto errinval;
759 }
760 break;
761 case NODE_TYPE_DECLARATOR:
762 ret = ctf_visitor_type_declarator(fd, depth, node);
763 if (ret)
764 return ret;
765 break;
766
767 case NODE_FLOATING_POINT:
768 switch (node->parent->type) {
67905e42 769 case NODE_TYPE_SPECIFIER:
3e11b713 770 break; /* OK */
67905e42
MD
771 default:
772 goto errinval;
773
774 case NODE_UNARY_EXPRESSION:
775 goto errperm;
776 }
3122e6f0 777 bt_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) {
67905e42
MD
778 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
779 if (ret)
780 return ret;
781 }
782 break;
783 case NODE_INTEGER:
784 switch (node->parent->type) {
67905e42 785 case NODE_TYPE_SPECIFIER:
3e11b713 786 break; /* OK */
67905e42
MD
787 default:
788 goto errinval;
789
790 }
791
3122e6f0 792 bt_list_for_each_entry(iter, &node->u.integer.expressions, siblings) {
67905e42
MD
793 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
794 if (ret)
795 return ret;
796 }
797 break;
798 case NODE_STRING:
799 switch (node->parent->type) {
67905e42 800 case NODE_TYPE_SPECIFIER:
3e11b713 801 break; /* OK */
67905e42
MD
802 default:
803 goto errinval;
804
805 case NODE_UNARY_EXPRESSION:
806 goto errperm;
807 }
808
3122e6f0 809 bt_list_for_each_entry(iter, &node->u.string.expressions, siblings) {
67905e42
MD
810 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
811 if (ret)
812 return ret;
813 }
814 break;
815 case NODE_ENUMERATOR:
816 switch (node->parent->type) {
817 case NODE_ENUM:
818 break;
819 default:
820 goto errinval;
821 }
822 /*
823 * Enumerators are only allows to contain:
824 * numeric unary expression
825 * or num. unary exp. ... num. unary exp
826 */
827 {
828 int count = 0;
829
3122e6f0 830 bt_list_for_each_entry(iter, &node->u.enumerator.values,
67905e42
MD
831 siblings) {
832 switch (count++) {
833 case 0: if (iter->type != NODE_UNARY_EXPRESSION
834 || (iter->u.unary_expression.type != UNARY_SIGNED_CONSTANT
835 && iter->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
34f7b02c
MD
836 || iter->u.unary_expression.link != UNARY_LINK_UNKNOWN) {
837 fprintf(fd, "[error]: semantic error (first unary expression of enumerator is unexpected)\n");
67905e42 838 goto errperm;
34f7b02c 839 }
67905e42
MD
840 break;
841 case 1: if (iter->type != NODE_UNARY_EXPRESSION
842 || (iter->u.unary_expression.type != UNARY_SIGNED_CONSTANT
843 && iter->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
34f7b02c
MD
844 || iter->u.unary_expression.link != UNARY_DOTDOTDOT) {
845 fprintf(fd, "[error]: semantic error (second unary expression of enumerator is unexpected)\n");
67905e42 846 goto errperm;
34f7b02c 847 }
67905e42
MD
848 break;
849 default:
850 goto errperm;
851 }
852 }
853 }
854
3122e6f0 855 bt_list_for_each_entry(iter, &node->u.enumerator.values, siblings) {
67905e42
MD
856 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
857 if (ret)
858 return ret;
859 }
860 break;
861 case NODE_ENUM:
862 switch (node->parent->type) {
67905e42 863 case NODE_TYPE_SPECIFIER:
3e11b713 864 break; /* OK */
67905e42
MD
865 default:
866 goto errinval;
867
868 case NODE_UNARY_EXPRESSION:
869 goto errperm;
870 }
871
872 depth++;
3e11b713
MD
873 ret = _ctf_visitor_semantic_check(fd, depth + 1, node->u._enum.container_type);
874 if (ret)
875 return ret;
67905e42 876
3122e6f0 877 bt_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) {
67905e42
MD
878 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
879 if (ret)
880 return ret;
881 }
882 depth--;
883 break;
884 case NODE_STRUCT_OR_VARIANT_DECLARATION:
885 switch (node->parent->type) {
886 case NODE_STRUCT:
887 case NODE_VARIANT:
888 break;
889 default:
890 goto errinval;
891 }
3e11b713
MD
892 ret = _ctf_visitor_semantic_check(fd, depth + 1,
893 node->u.struct_or_variant_declaration.type_specifier_list);
894 if (ret)
895 return ret;
3122e6f0 896 bt_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) {
67905e42
MD
897 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
898 if (ret)
899 return ret;
900 }
901 break;
902 case NODE_VARIANT:
903 switch (node->parent->type) {
67905e42 904 case NODE_TYPE_SPECIFIER:
3e11b713 905 break; /* OK */
67905e42
MD
906 default:
907 goto errinval;
908
909 case NODE_UNARY_EXPRESSION:
910 goto errperm;
911 }
3122e6f0 912 bt_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) {
67905e42
MD
913 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
914 if (ret)
915 return ret;
916 }
917 break;
918
919 case NODE_STRUCT:
920 switch (node->parent->type) {
67905e42 921 case NODE_TYPE_SPECIFIER:
3e11b713 922 break; /* OK */
67905e42
MD
923 default:
924 goto errinval;
925
926 case NODE_UNARY_EXPRESSION:
927 goto errperm;
928 }
3122e6f0 929 bt_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) {
67905e42
MD
930 ret = _ctf_visitor_semantic_check(fd, depth + 1, iter);
931 if (ret)
932 return ret;
933 }
934 break;
935
936 case NODE_UNKNOWN:
937 default:
938 fprintf(fd, "[error] %s: unknown node type %d\n", __func__,
939 (int) node->type);
940 return -EINVAL;
941 }
942 return ret;
943
944errinval:
34f7b02c
MD
945 fprintf(fd, "[error] %s: incoherent parent type %s for node type %s\n", __func__,
946 node_type(node->parent), node_type(node));
67905e42
MD
947 return -EINVAL; /* Incoherent structure */
948
949errperm:
34f7b02c
MD
950 fprintf(fd, "[error] %s: semantic error (parent type %s for node type %s)\n", __func__,
951 node_type(node->parent), node_type(node));
67905e42
MD
952 return -EPERM; /* Structure not allowed */
953}
954
955int ctf_visitor_semantic_check(FILE *fd, int depth, struct ctf_node *node)
956{
957 int ret = 0;
958
959 /*
960 * First make sure we create the parent links for all children. Let's
961 * take the safe route and recreate them at each validation, just in
962 * case the structure has changed.
963 */
c8b219a3 964 printf_verbose("CTF visitor: parent links creation... ");
67905e42
MD
965 ret = ctf_visitor_parent_links(fd, depth, node);
966 if (ret)
967 return ret;
c8b219a3
MD
968 printf_verbose("done.\n");
969 printf_verbose("CTF visitor: semantic check... ");
34f7b02c
MD
970 ret = _ctf_visitor_semantic_check(fd, depth, node);
971 if (ret)
972 return ret;
c8b219a3 973 printf_verbose("done.\n");
34f7b02c 974 return ret;
67905e42 975}
This page took 0.070138 seconds and 4 git commands to generate.