-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathctf-visitor-generate-io-struct.c
More file actions
3193 lines (2949 loc) · 92.6 KB
/
ctf-visitor-generate-io-struct.c
File metadata and controls
3193 lines (2949 loc) · 92.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ctf-visitor-generate-io-struct.c
*
* Common Trace Format Metadata Visitor (generate I/O structures).
*
* Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <glib.h>
#include <inttypes.h>
#include <errno.h>
#include <babeltrace/babeltrace-internal.h>
#include <babeltrace/list.h>
#include <babeltrace/types.h>
#include <babeltrace/ctf/metadata.h>
#include <babeltrace/compat/uuid.h>
#include <babeltrace/endian.h>
#include <babeltrace/ctf/events-internal.h>
#include "ctf-scanner.h"
#include "ctf-parser.h"
#include "ctf-ast.h"
#define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args)
#define _bt_list_first_entry(ptr, type, member) \
bt_list_entry((ptr)->next, type, member)
struct last_enum_value {
union {
int64_t s;
uint64_t u;
} u;
};
int opt_clock_force_correlate;
static
struct bt_declaration *ctf_type_specifier_list_visit(FILE *fd,
int depth, struct ctf_node *type_specifier_list,
struct declaration_scope *declaration_scope,
struct ctf_trace *trace);
static
int ctf_stream_visit(FILE *fd, int depth, struct ctf_node *node,
struct declaration_scope *parent_declaration_scope, struct ctf_trace *trace);
static
int is_unary_string(struct bt_list_head *head)
{
struct ctf_node *node;
bt_list_for_each_entry(node, head, siblings) {
if (node->type != NODE_UNARY_EXPRESSION)
return 0;
if (node->u.unary_expression.type != UNARY_STRING)
return 0;
}
return 1;
}
/*
* String returned must be freed by the caller using g_free.
*/
static
char *concatenate_unary_strings(struct bt_list_head *head)
{
struct ctf_node *node;
GString *str;
int i = 0;
str = g_string_new("");
bt_list_for_each_entry(node, head, siblings) {
char *src_string;
if (node->type != NODE_UNARY_EXPRESSION
|| node->u.unary_expression.type != UNARY_STRING
|| !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN)
^ (i == 0)))
return NULL;
switch (node->u.unary_expression.link) {
case UNARY_DOTLINK:
g_string_append(str, ".");
break;
case UNARY_ARROWLINK:
g_string_append(str, "->");
break;
case UNARY_DOTDOTDOT:
g_string_append(str, "...");
break;
default:
break;
}
src_string = node->u.unary_expression.u.string;
g_string_append(str, src_string);
i++;
}
return g_string_free(str, FALSE);
}
static
GQuark get_map_clock_name_value(struct bt_list_head *head)
{
struct ctf_node *node;
const char *name = NULL;
int i = 0;
bt_list_for_each_entry(node, head, siblings) {
char *src_string;
if (node->type != NODE_UNARY_EXPRESSION
|| node->u.unary_expression.type != UNARY_STRING
|| !((node->u.unary_expression.link != UNARY_LINK_UNKNOWN)
^ (i == 0)))
return 0;
/* needs to be chained with . */
switch (node->u.unary_expression.link) {
case UNARY_DOTLINK:
break;
case UNARY_ARROWLINK:
case UNARY_DOTDOTDOT:
return 0;
default:
break;
}
src_string = node->u.unary_expression.u.string;
switch (i) {
case 0: if (strcmp("clock", src_string) != 0) {
return 0;
}
break;
case 1: name = src_string;
break;
case 2: if (strcmp("value", src_string) != 0) {
return 0;
}
break;
default:
return 0; /* extra identifier, unknown */
}
i++;
}
return g_quark_from_string(name);
}
static
int is_unary_unsigned(struct bt_list_head *head)
{
struct ctf_node *node;
bt_list_for_each_entry(node, head, siblings) {
if (node->type != NODE_UNARY_EXPRESSION)
return 0;
if (node->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
return 0;
}
return 1;
}
static
int get_unary_unsigned(struct bt_list_head *head, uint64_t *value)
{
struct ctf_node *node;
int i = 0;
bt_list_for_each_entry(node, head, siblings) {
if (node->type != NODE_UNARY_EXPRESSION
|| node->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT
|| node->u.unary_expression.link != UNARY_LINK_UNKNOWN
|| i != 0)
return -EINVAL;
*value = node->u.unary_expression.u.unsigned_constant;
i++;
}
return 0;
}
static
int is_unary_signed(struct bt_list_head *head)
{
struct ctf_node *node;
bt_list_for_each_entry(node, head, siblings) {
if (node->type != NODE_UNARY_EXPRESSION)
return 0;
if (node->u.unary_expression.type != UNARY_SIGNED_CONSTANT)
return 0;
}
return 1;
}
static
int get_unary_signed(struct bt_list_head *head, int64_t *value)
{
struct ctf_node *node;
int i = 0;
bt_list_for_each_entry(node, head, siblings) {
if (node->type != NODE_UNARY_EXPRESSION
|| node->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT
|| (node->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT && node->u.unary_expression.type != UNARY_SIGNED_CONSTANT)
|| node->u.unary_expression.link != UNARY_LINK_UNKNOWN
|| i != 0)
return -EINVAL;
switch (node->u.unary_expression.type) {
case UNARY_UNSIGNED_CONSTANT:
*value = (int64_t) node->u.unary_expression.u.unsigned_constant;
break;
case UNARY_SIGNED_CONSTANT:
*value = node->u.unary_expression.u.signed_constant;
break;
default:
return -EINVAL;
}
i++;
}
return 0;
}
static
int get_unary_uuid(struct bt_list_head *head, unsigned char *uuid)
{
struct ctf_node *node;
int i = 0;
int ret = -1;
bt_list_for_each_entry(node, head, siblings) {
const char *src_string;
if (node->type != NODE_UNARY_EXPRESSION
|| node->u.unary_expression.type != UNARY_STRING
|| node->u.unary_expression.link != UNARY_LINK_UNKNOWN
|| i != 0)
return -EINVAL;
src_string = node->u.unary_expression.u.string;
ret = bt_uuid_parse(src_string, uuid);
}
return ret;
}
static
struct ctf_stream_declaration *trace_stream_lookup(struct ctf_trace *trace, uint64_t stream_id)
{
if (trace->streams->len <= stream_id)
return NULL;
return g_ptr_array_index(trace->streams, stream_id);
}
static
struct ctf_event_declaration *stream_event_lookup(struct ctf_stream_declaration *stream, uint64_t event_id)
{
if (stream->events_by_id->len <= event_id)
return NULL;
return g_ptr_array_index(stream->events_by_id, event_id);
}
static
struct ctf_clock *trace_clock_lookup(struct ctf_trace *trace, GQuark clock_name)
{
return g_hash_table_lookup(trace->parent.clocks, (gpointer) (unsigned long) clock_name);
}
static
int visit_type_specifier(FILE *fd, struct ctf_node *type_specifier, GString *str)
{
if (type_specifier->type != NODE_TYPE_SPECIFIER)
return -EINVAL;
switch (type_specifier->u.type_specifier.type) {
case TYPESPEC_VOID:
g_string_append(str, "void");
break;
case TYPESPEC_CHAR:
g_string_append(str, "char");
break;
case TYPESPEC_SHORT:
g_string_append(str, "short");
break;
case TYPESPEC_INT:
g_string_append(str, "int");
break;
case TYPESPEC_LONG:
g_string_append(str, "long");
break;
case TYPESPEC_FLOAT:
g_string_append(str, "float");
break;
case TYPESPEC_DOUBLE:
g_string_append(str, "double");
break;
case TYPESPEC_SIGNED:
g_string_append(str, "signed");
break;
case TYPESPEC_UNSIGNED:
g_string_append(str, "unsigned");
break;
case TYPESPEC_BOOL:
g_string_append(str, "bool");
break;
case TYPESPEC_COMPLEX:
g_string_append(str, "_Complex");
break;
case TYPESPEC_IMAGINARY:
g_string_append(str, "_Imaginary");
break;
case TYPESPEC_CONST:
g_string_append(str, "const");
break;
case TYPESPEC_ID_TYPE:
if (type_specifier->u.type_specifier.id_type)
g_string_append(str, type_specifier->u.type_specifier.id_type);
break;
case TYPESPEC_STRUCT:
{
struct ctf_node *node = type_specifier->u.type_specifier.node;
if (!node->u._struct.name) {
fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__);
return -EINVAL;
}
g_string_append(str, "struct ");
g_string_append(str, node->u._struct.name);
break;
}
case TYPESPEC_VARIANT:
{
struct ctf_node *node = type_specifier->u.type_specifier.node;
if (!node->u.variant.name) {
fprintf(fd, "[error] %s: unexpected empty variant name\n", __func__);
return -EINVAL;
}
g_string_append(str, "variant ");
g_string_append(str, node->u.variant.name);
break;
}
case TYPESPEC_ENUM:
{
struct ctf_node *node = type_specifier->u.type_specifier.node;
if (!node->u._enum.enum_id) {
fprintf(fd, "[error] %s: unexpected empty enum ID\n", __func__);
return -EINVAL;
}
g_string_append(str, "enum ");
g_string_append(str, node->u._enum.enum_id);
break;
}
case TYPESPEC_FLOATING_POINT:
case TYPESPEC_INTEGER:
case TYPESPEC_STRING:
default:
fprintf(fd, "[error] %s: unknown specifier\n", __func__);
return -EINVAL;
}
return 0;
}
static
int visit_type_specifier_list(FILE *fd, struct ctf_node *type_specifier_list, GString *str)
{
struct ctf_node *iter;
int alias_item_nr = 0;
int ret;
bt_list_for_each_entry(iter, &type_specifier_list->u.type_specifier_list.head, siblings) {
if (alias_item_nr != 0)
g_string_append(str, " ");
alias_item_nr++;
ret = visit_type_specifier(fd, iter, str);
if (ret)
return ret;
}
return 0;
}
static
GQuark create_typealias_identifier(FILE *fd, int depth,
struct ctf_node *type_specifier_list,
struct ctf_node *node_type_declarator)
{
struct ctf_node *iter;
GString *str;
char *str_c;
GQuark alias_q;
int ret;
str = g_string_new("");
ret = visit_type_specifier_list(fd, type_specifier_list, str);
if (ret) {
g_string_free(str, TRUE);
return 0;
}
bt_list_for_each_entry(iter, &node_type_declarator->u.type_declarator.pointers, siblings) {
g_string_append(str, " *");
if (iter->u.pointer.const_qualifier)
g_string_append(str, " const");
}
str_c = g_string_free(str, FALSE);
alias_q = g_quark_from_string(str_c);
g_free(str_c);
return alias_q;
}
static
struct bt_declaration *ctf_type_declarator_visit(FILE *fd, int depth,
struct ctf_node *type_specifier_list,
GQuark *field_name,
struct ctf_node *node_type_declarator,
struct declaration_scope *declaration_scope,
struct bt_declaration *nested_declaration,
struct ctf_trace *trace)
{
/*
* Visit type declarator by first taking care of sequence/array
* (recursively). Then, when we get to the identifier, take care
* of pointers.
*/
if (node_type_declarator) {
if (node_type_declarator->u.type_declarator.type == TYPEDEC_UNKNOWN) {
return NULL;
}
/* TODO: gcc bitfields not supported yet. */
if (node_type_declarator->u.type_declarator.bitfield_len != NULL) {
fprintf(fd, "[error] %s: gcc bitfields are not supported yet.\n", __func__);
return NULL;
}
}
if (!nested_declaration) {
if (node_type_declarator && !bt_list_empty(&node_type_declarator->u.type_declarator.pointers)) {
GQuark alias_q;
/*
* If we have a pointer declarator, it _has_ to be present in
* the typealiases (else fail).
*/
alias_q = create_typealias_identifier(fd, depth,
type_specifier_list, node_type_declarator);
nested_declaration = bt_lookup_declaration(alias_q, declaration_scope);
if (!nested_declaration) {
fprintf(fd, "[error] %s: cannot find typealias \"%s\".\n", __func__, g_quark_to_string(alias_q));
return NULL;
}
if (nested_declaration->id == CTF_TYPE_INTEGER) {
struct declaration_integer *integer_declaration =
container_of(nested_declaration, struct declaration_integer, p);
/* For base to 16 for pointers (expected pretty-print) */
if (!integer_declaration->base) {
/*
* We need to do a copy of the
* integer declaration to modify it. There could be other references to
* it.
*/
integer_declaration = bt_integer_declaration_new(integer_declaration->len,
integer_declaration->byte_order, integer_declaration->signedness,
integer_declaration->p.alignment, 16, integer_declaration->encoding,
integer_declaration->clock);
nested_declaration = &integer_declaration->p;
}
}
} else {
nested_declaration = ctf_type_specifier_list_visit(fd, depth,
type_specifier_list, declaration_scope, trace);
}
}
if (!node_type_declarator)
return nested_declaration;
if (node_type_declarator->u.type_declarator.type == TYPEDEC_ID) {
if (node_type_declarator->u.type_declarator.u.id)
*field_name = g_quark_from_string(node_type_declarator->u.type_declarator.u.id);
else
*field_name = 0;
return nested_declaration;
} else {
struct bt_declaration *declaration;
struct ctf_node *first;
/* TYPEDEC_NESTED */
if (!nested_declaration) {
fprintf(fd, "[error] %s: nested type is unknown.\n", __func__);
return NULL;
}
/* create array/sequence, pass nested_declaration as child. */
if (bt_list_empty(&node_type_declarator->u.type_declarator.u.nested.length)) {
fprintf(fd, "[error] %s: expecting length field reference or value.\n", __func__);
return NULL;
}
first = _bt_list_first_entry(&node_type_declarator->u.type_declarator.u.nested.length,
struct ctf_node, siblings);
if (first->type != NODE_UNARY_EXPRESSION) {
return NULL;
}
switch (first->u.unary_expression.type) {
case UNARY_UNSIGNED_CONSTANT:
{
struct declaration_array *array_declaration;
size_t len;
len = first->u.unary_expression.u.unsigned_constant;
array_declaration = bt_array_declaration_new(len, nested_declaration,
declaration_scope);
if (!array_declaration) {
fprintf(fd, "[error] %s: cannot create array declaration.\n", __func__);
return NULL;
}
bt_declaration_unref(nested_declaration);
declaration = &array_declaration->p;
break;
}
case UNARY_STRING:
{
/* Lookup unsigned integer definition, create sequence */
char *length_name = concatenate_unary_strings(&node_type_declarator->u.type_declarator.u.nested.length);
struct declaration_sequence *sequence_declaration;
if (!length_name)
return NULL;
sequence_declaration = bt_sequence_declaration_new(length_name, nested_declaration, declaration_scope);
if (!sequence_declaration) {
fprintf(fd, "[error] %s: cannot create sequence declaration.\n", __func__);
g_free(length_name);
return NULL;
}
bt_declaration_unref(nested_declaration);
declaration = &sequence_declaration->p;
g_free(length_name);
break;
}
default:
return NULL;
}
/* Pass it as content of outer container */
declaration = ctf_type_declarator_visit(fd, depth,
type_specifier_list, field_name,
node_type_declarator->u.type_declarator.u.nested.type_declarator,
declaration_scope, declaration, trace);
return declaration;
}
}
static
int ctf_struct_type_declarators_visit(FILE *fd, int depth,
struct declaration_struct *struct_declaration,
struct ctf_node *type_specifier_list,
struct bt_list_head *type_declarators,
struct declaration_scope *declaration_scope,
struct ctf_trace *trace)
{
struct ctf_node *iter;
GQuark field_name;
bt_list_for_each_entry(iter, type_declarators, siblings) {
struct bt_declaration *field_declaration;
field_declaration = ctf_type_declarator_visit(fd, depth,
type_specifier_list,
&field_name, iter,
struct_declaration->scope,
NULL, trace);
if (!field_declaration) {
fprintf(fd, "[error] %s: unable to find struct field declaration type\n", __func__);
return -EINVAL;
}
/* Check if field with same name already exists */
if (bt_struct_declaration_lookup_field_index(struct_declaration, field_name) >= 0) {
fprintf(fd, "[error] %s: duplicate field %s in struct\n", __func__, g_quark_to_string(field_name));
return -EINVAL;
}
bt_struct_declaration_add_field(struct_declaration,
g_quark_to_string(field_name),
field_declaration);
bt_declaration_unref(field_declaration);
}
return 0;
}
static
int ctf_variant_type_declarators_visit(FILE *fd, int depth,
struct declaration_untagged_variant *untagged_variant_declaration,
struct ctf_node *type_specifier_list,
struct bt_list_head *type_declarators,
struct declaration_scope *declaration_scope,
struct ctf_trace *trace)
{
struct ctf_node *iter;
GQuark field_name;
bt_list_for_each_entry(iter, type_declarators, siblings) {
struct bt_declaration *field_declaration;
field_declaration = ctf_type_declarator_visit(fd, depth,
type_specifier_list,
&field_name, iter,
untagged_variant_declaration->scope,
NULL, trace);
if (!field_declaration) {
fprintf(fd, "[error] %s: unable to find variant field declaration type\n", __func__);
return -EINVAL;
}
if (bt_untagged_variant_declaration_get_field_from_tag(untagged_variant_declaration, field_name) != NULL) {
fprintf(fd, "[error] %s: duplicate field %s in variant\n", __func__, g_quark_to_string(field_name));
return -EINVAL;
}
bt_untagged_variant_declaration_add_field(untagged_variant_declaration,
g_quark_to_string(field_name),
field_declaration);
bt_declaration_unref(field_declaration);
}
return 0;
}
static
int ctf_typedef_visit(FILE *fd, int depth, struct declaration_scope *scope,
struct ctf_node *type_specifier_list,
struct bt_list_head *type_declarators,
struct ctf_trace *trace)
{
struct ctf_node *iter;
GQuark identifier;
bt_list_for_each_entry(iter, type_declarators, siblings) {
struct bt_declaration *type_declaration;
int ret;
type_declaration = ctf_type_declarator_visit(fd, depth,
type_specifier_list,
&identifier, iter,
scope, NULL, trace);
if (!type_declaration) {
fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
return -EINVAL;
}
/*
* Don't allow typedef and typealias of untagged
* variants.
*/
if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
bt_declaration_unref(type_declaration);
return -EPERM;
}
ret = bt_register_declaration(identifier, type_declaration, scope);
if (ret) {
type_declaration->declaration_free(type_declaration);
return ret;
}
bt_declaration_unref(type_declaration);
}
return 0;
}
static
int ctf_typealias_visit(FILE *fd, int depth, struct declaration_scope *scope,
struct ctf_node *target, struct ctf_node *alias,
struct ctf_trace *trace)
{
struct bt_declaration *type_declaration;
struct ctf_node *node;
GQuark dummy_id;
GQuark alias_q;
int err;
/* See ctf_visitor_type_declarator() in the semantic validator. */
/*
* Create target type declaration.
*/
if (bt_list_empty(&target->u.typealias_target.type_declarators))
node = NULL;
else
node = _bt_list_first_entry(&target->u.typealias_target.type_declarators,
struct ctf_node, siblings);
type_declaration = ctf_type_declarator_visit(fd, depth,
target->u.typealias_target.type_specifier_list,
&dummy_id, node,
scope, NULL, trace);
if (!type_declaration) {
fprintf(fd, "[error] %s: problem creating type declaration\n", __func__);
err = -EINVAL;
goto error;
}
/*
* Don't allow typedef and typealias of untagged
* variants.
*/
if (type_declaration->id == CTF_TYPE_UNTAGGED_VARIANT) {
fprintf(fd, "[error] %s: typedef of untagged variant is not permitted.\n", __func__);
bt_declaration_unref(type_declaration);
return -EPERM;
}
/*
* The semantic validator does not check whether the target is
* abstract or not (if it has an identifier). Check it here.
*/
if (dummy_id != 0) {
fprintf(fd, "[error] %s: expecting empty identifier\n", __func__);
err = -EINVAL;
goto error;
}
/*
* Create alias identifier.
*/
node = _bt_list_first_entry(&alias->u.typealias_alias.type_declarators,
struct ctf_node, siblings);
alias_q = create_typealias_identifier(fd, depth,
alias->u.typealias_alias.type_specifier_list, node);
err = bt_register_declaration(alias_q, type_declaration, scope);
if (err)
goto error;
bt_declaration_unref(type_declaration);
return 0;
error:
if (type_declaration) {
type_declaration->declaration_free(type_declaration);
}
return err;
}
static
int ctf_struct_declaration_list_visit(FILE *fd, int depth,
struct ctf_node *iter, struct declaration_struct *struct_declaration,
struct ctf_trace *trace)
{
int ret;
switch (iter->type) {
case NODE_TYPEDEF:
/* For each declarator, declare type and add type to struct bt_declaration scope */
ret = ctf_typedef_visit(fd, depth,
struct_declaration->scope,
iter->u._typedef.type_specifier_list,
&iter->u._typedef.type_declarators, trace);
if (ret)
return ret;
break;
case NODE_TYPEALIAS:
/* Declare type with declarator and add type to struct bt_declaration scope */
ret = ctf_typealias_visit(fd, depth,
struct_declaration->scope,
iter->u.typealias.target,
iter->u.typealias.alias, trace);
if (ret)
return ret;
break;
case NODE_STRUCT_OR_VARIANT_DECLARATION:
/* Add field to structure declaration */
ret = ctf_struct_type_declarators_visit(fd, depth,
struct_declaration,
iter->u.struct_or_variant_declaration.type_specifier_list,
&iter->u.struct_or_variant_declaration.type_declarators,
struct_declaration->scope, trace);
if (ret)
return ret;
break;
default:
fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type);
return -EINVAL;
}
return 0;
}
static
int ctf_variant_declaration_list_visit(FILE *fd, int depth,
struct ctf_node *iter,
struct declaration_untagged_variant *untagged_variant_declaration,
struct ctf_trace *trace)
{
int ret;
switch (iter->type) {
case NODE_TYPEDEF:
/* For each declarator, declare type and add type to variant declaration scope */
ret = ctf_typedef_visit(fd, depth,
untagged_variant_declaration->scope,
iter->u._typedef.type_specifier_list,
&iter->u._typedef.type_declarators, trace);
if (ret)
return ret;
break;
case NODE_TYPEALIAS:
/* Declare type with declarator and add type to variant declaration scope */
ret = ctf_typealias_visit(fd, depth,
untagged_variant_declaration->scope,
iter->u.typealias.target,
iter->u.typealias.alias, trace);
if (ret)
return ret;
break;
case NODE_STRUCT_OR_VARIANT_DECLARATION:
/* Add field to structure declaration */
ret = ctf_variant_type_declarators_visit(fd, depth,
untagged_variant_declaration,
iter->u.struct_or_variant_declaration.type_specifier_list,
&iter->u.struct_or_variant_declaration.type_declarators,
untagged_variant_declaration->scope, trace);
if (ret)
return ret;
break;
default:
fprintf(fd, "[error] %s: unexpected node type %d\n", __func__, (int) iter->type);
return -EINVAL;
}
return 0;
}
static
struct bt_declaration *ctf_declaration_struct_visit(FILE *fd,
int depth, const char *name, struct bt_list_head *declaration_list,
int has_body, struct bt_list_head *min_align,
struct declaration_scope *declaration_scope,
struct ctf_trace *trace)
{
struct declaration_struct *struct_declaration;
struct ctf_node *iter;
/*
* For named struct (without body), lookup in
* declaration scope. Don't take reference on struct
* declaration: ref is only taken upon definition.
*/
if (!has_body) {
if (!name)
return NULL;
struct_declaration =
bt_lookup_struct_declaration(g_quark_from_string(name),
declaration_scope);
bt_declaration_ref(&struct_declaration->p);
return &struct_declaration->p;
} else {
uint64_t min_align_value = 0;
/* For unnamed struct, create type */
/* For named struct (with body), create type and add to declaration scope */
if (name) {
if (bt_lookup_struct_declaration(g_quark_from_string(name),
declaration_scope)) {
fprintf(fd, "[error] %s: struct %s already declared in scope\n", __func__, name);
return NULL;
}
}
if (!bt_list_empty(min_align)) {
int ret;
ret = get_unary_unsigned(min_align, &min_align_value);
if (ret) {
fprintf(fd, "[error] %s: unexpected unary expression for structure \"align\" attribute\n", __func__);
goto error;
}
}
struct_declaration = bt_struct_declaration_new(declaration_scope,
min_align_value);
bt_list_for_each_entry(iter, declaration_list, siblings) {
int ret;
ret = ctf_struct_declaration_list_visit(fd, depth + 1, iter,
struct_declaration, trace);
if (ret)
goto error_free_declaration;
}
if (name) {
int ret;
ret = bt_register_struct_declaration(g_quark_from_string(name),
struct_declaration,
declaration_scope);
if (ret)
return NULL;
}
return &struct_declaration->p;
}
error_free_declaration:
struct_declaration->p.declaration_free(&struct_declaration->p);
error:
return NULL;
}
static
struct bt_declaration *ctf_declaration_variant_visit(FILE *fd,
int depth, const char *name, const char *choice,
struct bt_list_head *declaration_list,
int has_body, struct declaration_scope *declaration_scope,
struct ctf_trace *trace)
{
struct declaration_untagged_variant *untagged_variant_declaration;
struct declaration_variant *variant_declaration;
struct ctf_node *iter;
/*
* For named variant (without body), lookup in
* declaration scope. Don't take reference on variant
* declaration: ref is only taken upon definition.
*/
if (!has_body) {
if (!name)
return NULL;
untagged_variant_declaration =
bt_lookup_variant_declaration(g_quark_from_string(name),
declaration_scope);
bt_declaration_ref(&untagged_variant_declaration->p);
} else {
/* For unnamed variant, create type */
/* For named variant (with body), create type and add to declaration scope */
if (name) {
if (bt_lookup_variant_declaration(g_quark_from_string(name),
declaration_scope)) {
fprintf(fd, "[error] %s: variant %s already declared in scope\n", __func__, name);
return NULL;
}
}
untagged_variant_declaration = bt_untagged_bt_variant_declaration_new(declaration_scope);
bt_list_for_each_entry(iter, declaration_list, siblings) {
int ret;
ret = ctf_variant_declaration_list_visit(fd, depth + 1, iter,
untagged_variant_declaration, trace);
if (ret)
goto error;
}
if (name) {
int ret;
ret = bt_register_variant_declaration(g_quark_from_string(name),
untagged_variant_declaration,
declaration_scope);
if (ret)
return NULL;
}
}
/*
* if tagged, create tagged variant and return. else return
* untagged variant.
*/
if (!choice) {
return &untagged_variant_declaration->p;
} else {
variant_declaration = bt_variant_declaration_new(untagged_variant_declaration, choice);
if (!variant_declaration)
goto error;
bt_declaration_unref(&untagged_variant_declaration->p);
return &variant_declaration->p;
}
error:
untagged_variant_declaration->p.declaration_free(&untagged_variant_declaration->p);
return NULL;
}
static
int ctf_enumerator_list_visit(FILE *fd, int depth,
struct ctf_node *enumerator,
struct declaration_enum *enum_declaration,
struct last_enum_value *last)
{
GQuark q;
struct ctf_node *iter;
q = g_quark_from_string(enumerator->u.enumerator.id);
if (enum_declaration->integer_declaration->signedness) {
int64_t start = 0, end = 0;
int nr_vals = 0;
bt_list_for_each_entry(iter, &enumerator->u.enumerator.values, siblings) {