2 * ctf-visitor-generate-ir.c
4 * Common Trace Format metadata visitor (generates CTF IR objects).
6 * Based on older ctf-visitor-generate-io-struct.c.
8 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Copyright 2015-2016 - Philippe Proulx <philippe.proulx@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 #include <babeltrace/ctf/metadata.h>
40 #include <babeltrace/compat/uuid.h>
41 #include <babeltrace/endian.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/ctf/events-internal.h>
44 #include <babeltrace/ctf-ir/trace.h>
45 #include <babeltrace/ctf-ir/stream-class.h>
46 #include <babeltrace/ctf-ir/event.h>
47 #include <babeltrace/ctf-ir/event-class.h>
48 #include <babeltrace/ctf-ir/field-types.h>
49 #include <babeltrace/ctf-ir/field-types-internal.h>
50 #include <babeltrace/ctf-ir/clock.h>
56 /* Bit value (left shift) */
57 #define _BV(_val) (1 << (_val))
59 /* Bit is set in a set of bits */
60 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
62 /* Set bit in a set of bits */
63 #define _SET(_set, _mask) (*(_set) |= (_mask))
65 /* Bits for verifying existing attributes in various declarations */
67 _CLOCK_NAME_SET
= _BV(0),
68 _CLOCK_UUID_SET
= _BV(1),
69 _CLOCK_FREQ_SET
= _BV(2),
70 _CLOCK_PRECISION_SET
= _BV(3),
71 _CLOCK_OFFSET_S_SET
= _BV(4),
72 _CLOCK_OFFSET_SET
= _BV(5),
73 _CLOCK_ABSOLUTE_SET
= _BV(6),
74 _CLOCK_DESCRIPTION_SET
= _BV(7),
78 _INTEGER_ALIGN_SET
= _BV(0),
79 _INTEGER_SIZE_SET
= _BV(1),
80 _INTEGER_BASE_SET
= _BV(2),
81 _INTEGER_ENCODING_SET
= _BV(3),
82 _INTEGER_BYTE_ORDER_SET
= _BV(4),
83 _INTEGER_SIGNED_SET
= _BV(5),
84 _INTEGER_MAP_SET
= _BV(6),
88 _FLOAT_ALIGN_SET
= _BV(0),
89 _FLOAT_MANT_DIG_SET
= _BV(1),
90 _FLOAT_EXP_DIG_SET
= _BV(2),
91 _FLOAT_BYTE_ORDER_SET
= _BV(3),
95 _STRING_ENCODING_SET
= _BV(0),
99 _TRACE_MINOR_SET
= _BV(0),
100 _TRACE_MAJOR_SET
= _BV(1),
101 _TRACE_BYTE_ORDER_SET
= _BV(2),
102 _TRACE_UUID_SET
= _BV(3),
103 _TRACE_PACKET_HEADER_SET
= _BV(4),
107 _STREAM_ID_SET
= _BV(0),
108 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
109 _STREAM_EVENT_HEADER_SET
= _BV(2),
110 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
114 _EVENT_NAME_SET
= _BV(0),
115 _EVENT_ID_SET
= _BV(1),
116 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
117 _EVENT_STREAM_ID_SET
= _BV(3),
118 _EVENT_LOGLEVEL_SET
= _BV(4),
119 _EVENT_CONTEXT_SET
= _BV(5),
120 _EVENT_FIELDS_SET
= _BV(6),
123 /* Prefixes of type aliases */
124 #define _PREFIX_ALIAS 'a'
125 #define _PREFIX_ENUM 'e'
126 #define _PREFIX_STRUCT 's'
127 #define _PREFIX_VARIANT 'v'
129 /* First entry in a BT list */
130 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
131 bt_list_entry((_ptr)->next, _type, _member)
133 #define _BT_CTF_FIELD_TYPE_INIT(_name) struct bt_ctf_field_type *_name = NULL;
135 /* Error printing wrappers */
136 #define _PERROR(_fmt, ...) \
138 fprintf(ctx->efd, "[error] %s: " _fmt "\n", \
139 __func__, __VA_ARGS__); \
142 #define _PWARNING(_fmt, ...) \
144 fprintf(ctx->efd, "[warning] %s: " _fmt "\n", \
145 __func__, __VA_ARGS__); \
148 #define _FPERROR(_stream, _fmt, ...) \
150 fprintf(_stream, "[error] %s: " _fmt "\n", \
151 __func__, __VA_ARGS__); \
154 #define _FPWARNING(_stream, _fmt, ...) \
156 fprintf(_stream, "[warning] %s: " _fmt "\n", \
157 __func__, __VA_ARGS__); \
160 #define _PERROR_DUP_ATTR(_attr, _entity) \
163 "[error] %s: duplicate attribute \"" \
164 _attr "\" in " _entity "\n", __func__); \
168 * Declaration scope of a visitor context. This represents a TSDL
169 * lexical scope, so that aliases and named structures, variants,
170 * and enumerations may be registered and looked up hierarchically.
172 struct ctx_decl_scope
{
174 * Alias name to field type.
176 * GQuark -> struct bt_ctf_field_type *
178 GHashTable
*decl_map
;
180 /* Parent scope; NULL if this is the root declaration scope */
181 struct ctx_decl_scope
*parent_scope
;
188 /* Trace being filled (weak ref.) */
189 struct bt_ctf_trace
*trace
;
191 /* Error stream to use during visit */
194 /* Current declaration scope (top of the stack) */
195 struct ctx_decl_scope
*current_scope
;
197 /* 1 if trace declaration is visited */
198 int is_trace_visited
;
200 /* Trace attributes */
201 uint64_t trace_major
;
202 uint64_t trace_minor
;
203 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
206 * Stream IDs to stream classes.
208 * int64_t -> struct bt_ctf_stream_class *
210 GHashTable
*stream_classes
;
214 * Creates a new declaration scope.
216 * @param par_scope Parent scope (NULL if creating a root scope)
217 * @returns New declaration scope, or NULL on error
220 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
222 struct ctx_decl_scope
*scope
;
224 scope
= g_new(struct ctx_decl_scope
, 1);
229 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
230 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
231 scope
->parent_scope
= par_scope
;
238 * Destroys a declaration scope.
240 * This function does not destroy the parent scope.
242 * @param scope Scope to destroy
245 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
251 g_hash_table_destroy(scope
->decl_map
);
259 * Returns the GQuark of a prefixed alias.
261 * @param prefix Prefix character
263 * @returns Associated GQuark, or 0 on error
266 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
272 /* Prefix character + original string + '\0' */
273 char *prname
= g_new(char, strlen(name
) + 2);
278 sprintf(prname
, "%c%s", prefix
, name
);
279 qname
= g_quark_from_string(prname
);
287 * Looks up a prefixed type alias within a declaration scope.
289 * @param scope Declaration scope
290 * @param prefix Prefix character
291 * @param name Alias name
292 * @param level Number of levels to dig (-1 means infinite)
293 * @returns Declaration, or NULL if not found
296 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
297 struct ctx_decl_scope
*scope
, char prefix
,
298 const char *name
, int levels
)
302 _BT_CTF_FIELD_TYPE_INIT(decl
);
303 struct ctx_decl_scope
*cur_scope
= scope
;
307 qname
= get_prefixed_named_quark(prefix
, name
);
316 while (cur_scope
&& cur_levels
< levels
) {
317 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
318 (gconstpointer
) (unsigned long) qname
);
320 /* Caller's reference */
325 cur_scope
= cur_scope
->parent_scope
;
336 * Looks up a type alias within a declaration scope.
338 * @param scope Declaration scope
339 * @param name Alias name
340 * @param level Number of levels to dig (-1 means infinite)
341 * @returns Declaration, or NULL if not found
344 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
345 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
347 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
352 * Looks up an enumeration within a declaration scope.
354 * @param scope Declaration scope
355 * @param name Enumeration name
356 * @param level Number of levels to dig (-1 means infinite)
357 * @returns Declaration, or NULL if not found
360 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
361 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
363 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
368 * Looks up a structure within a declaration scope.
370 * @param scope Declaration scope
371 * @param name Structure name
372 * @param level Number of levels to dig (-1 means infinite)
373 * @returns Declaration, or NULL if not found
376 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
377 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
379 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
384 * Looks up a variant within a declaration scope.
386 * @param scope Declaration scope
387 * @param name Variant name
388 * @param level Number of levels to dig (-1 means infinite)
389 * @returns Declaration, or NULL if not found
392 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
393 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
395 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
400 * Registers a prefixed type alias within a declaration scope.
402 * @param scope Declaration scope
403 * @param prefix Prefix character
404 * @param name Alias name (non-NULL)
405 * @param decl Declaration to register
406 * @returns 0 if registration went okay, negative value otherwise
409 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
410 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
414 _BT_CTF_FIELD_TYPE_INIT(edecl
);
419 qname
= get_prefixed_named_quark(prefix
, name
);
425 /* Make sure alias does not exist in local scope */
426 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
433 g_hash_table_insert(scope
->decl_map
,
434 (gpointer
) (unsigned long) qname
, decl
);
436 /* Hash table's reference */
446 * Registers a type alias within a declaration scope.
448 * @param scope Declaration scope
449 * @param name Alias name (non-NULL)
450 * @param decl Declaration to register
451 * @returns 0 if registration went okay, negative value otherwise
454 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
455 const char *name
, struct bt_ctf_field_type
*decl
)
457 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
462 * Registers an enumeration declaration within a declaration scope.
464 * @param scope Declaration scope
465 * @param name Enumeration name (non-NULL)
466 * @param decl Enumeration declaration to register
467 * @returns 0 if registration went okay, negative value otherwise
470 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
471 const char *name
, struct bt_ctf_field_type
*decl
)
473 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
478 * Registers a structure declaration within a declaration scope.
480 * @param scope Declaration scope
481 * @param name Structure name (non-NULL)
482 * @param decl Structure declaration to register
483 * @returns 0 if registration went okay, negative value otherwise
486 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
487 const char *name
, struct bt_ctf_field_type
*decl
)
489 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
494 * Registers a variant declaration within a declaration scope.
496 * @param scope Declaration scope
497 * @param name Variant name (non-NULL)
498 * @param decl Variant declaration to register
499 * @returns 0 if registration went okay, negative value otherwise
502 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
503 const char *name
, struct bt_ctf_field_type
*decl
)
505 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
510 * Creates a new visitor context.
512 * @param trace Associated trace
513 * @param efd Error stream
514 * @returns New visitor context, or NULL on error
517 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
, FILE *efd
)
519 struct ctx
*ctx
= NULL
;
520 struct ctx_decl_scope
*scope
= NULL
;
522 ctx
= g_new(struct ctx
, 1);
527 /* Root declaration scope */
528 scope
= ctx_decl_scope_create(NULL
);
533 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
534 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
535 if (!ctx
->stream_classes
) {
541 ctx
->current_scope
= scope
;
542 ctx
->is_trace_visited
= FALSE
;
548 ctx_decl_scope_destroy(scope
);
554 * Destroys a visitor context.
556 * @param ctx Visitor context to destroy
559 void ctx_destroy(struct ctx
*ctx
)
561 struct ctx_decl_scope
*scope
;
563 * Destroy all scopes, from current one to the root scope.
570 scope
= ctx
->current_scope
;
573 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
575 ctx_decl_scope_destroy(scope
);
576 scope
= parent_scope
;
579 g_hash_table_destroy(ctx
->stream_classes
);
587 * Pushes a new declaration scope on top of a visitor context's
588 * declaration scope stack.
590 * @param ctx Visitor context
591 * @returns 0 on success, or a negative value on error
594 int ctx_push_scope(struct ctx
*ctx
)
597 struct ctx_decl_scope
*new_scope
;
600 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
606 ctx
->current_scope
= new_scope
;
613 void ctx_pop_scope(struct ctx
*ctx
)
615 struct ctx_decl_scope
*parent_scope
= NULL
;
619 if (!ctx
->current_scope
) {
623 parent_scope
= ctx
->current_scope
->parent_scope
;
624 ctx_decl_scope_destroy(ctx
->current_scope
);
625 ctx
->current_scope
= parent_scope
;
632 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
633 struct bt_ctf_field_type
**decl
);
636 int is_unary_string(struct bt_list_head
*head
)
639 struct ctf_node
*node
;
641 bt_list_for_each_entry(node
, head
, siblings
) {
642 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
646 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
655 char *concatenate_unary_strings(struct bt_list_head
*head
)
659 struct ctf_node
*node
;
661 str
= g_string_new(NULL
);
663 bt_list_for_each_entry(node
, head
, siblings
) {
667 node
->type
!= NODE_UNARY_EXPRESSION
||
668 node
->u
.unary_expression
.type
!= UNARY_STRING
||
671 node
->u
.unary_expression
.link
!=
679 switch (node
->u
.unary_expression
.link
) {
681 g_string_append(str
, ".");
683 case UNARY_ARROWLINK
:
684 g_string_append(str
, "->");
686 case UNARY_DOTDOTDOT
:
687 g_string_append(str
, "...");
693 src_string
= node
->u
.unary_expression
.u
.string
;
694 g_string_append(str
, src_string
);
698 /* Destroys the container, returns the underlying string */
699 return g_string_free(str
, FALSE
);
702 /* This always returns NULL */
703 return g_string_free(str
, TRUE
);
707 const char *get_map_clock_name_value(struct bt_list_head
*head
)
710 struct ctf_node
*node
;
711 const char *name
= NULL
;
713 bt_list_for_each_entry(node
, head
, siblings
) {
715 int uexpr_type
= node
->u
.unary_expression
.type
;
716 int uexpr_link
= node
->u
.unary_expression
.link
;
717 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
718 uexpr_type
!= UNARY_STRING
||
719 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
724 /* Needs to be chained with . */
725 switch (node
->u
.unary_expression
.link
) {
728 case UNARY_ARROWLINK
:
729 case UNARY_DOTDOTDOT
:
735 src_string
= node
->u
.unary_expression
.u
.string
;
739 if (strcmp("clock", src_string
)) {
747 if (strcmp("value", src_string
)) {
752 /* Extra identifier, unknown */
766 int is_unary_unsigned(struct bt_list_head
*head
)
769 struct ctf_node
*node
;
771 bt_list_for_each_entry(node
, head
, siblings
) {
772 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
776 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
785 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
789 struct ctf_node
*node
;
791 bt_list_for_each_entry(node
, head
, siblings
) {
792 int uexpr_type
= node
->u
.unary_expression
.type
;
793 int uexpr_link
= node
->u
.unary_expression
.link
;
794 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
795 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
796 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
802 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
811 int is_unary_signed(struct bt_list_head
*head
)
814 struct ctf_node
*node
;
816 bt_list_for_each_entry(node
, head
, siblings
) {
817 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
821 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
830 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
834 struct ctf_node
*node
;
836 bt_list_for_each_entry(node
, head
, siblings
) {
837 int uexpr_type
= node
->u
.unary_expression
.type
;
838 int uexpr_link
= node
->u
.unary_expression
.link
;
839 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
840 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
841 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
842 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
843 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
849 switch (node
->u
.unary_expression
.type
) {
850 case UNARY_UNSIGNED_CONSTANT
:
852 node
->u
.unary_expression
.u
.unsigned_constant
;
854 case UNARY_SIGNED_CONSTANT
:
855 *value
= node
->u
.unary_expression
.u
.signed_constant
;
870 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
874 struct ctf_node
*node
;
876 bt_list_for_each_entry(node
, head
, siblings
) {
877 int uexpr_type
= node
->u
.unary_expression
.type
;
878 int uexpr_link
= node
->u
.unary_expression
.link
;
879 const char *src_string
;
881 if (node
->type
!= NODE_UNARY_EXPRESSION
||
882 uexpr_type
!= UNARY_STRING
||
883 uexpr_link
!= UNARY_LINK_UNKNOWN
||
889 src_string
= node
->u
.unary_expression
.u
.string
;
890 ret
= bt_uuid_parse(src_string
, uuid
);
901 int get_boolean(FILE *efd
, struct ctf_node
*unary_expr
)
905 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
906 _FPERROR(efd
, "%s", "expecting unary expression");
911 switch (unary_expr
->u
.unary_expression
.type
) {
912 case UNARY_UNSIGNED_CONSTANT
:
913 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
915 case UNARY_SIGNED_CONSTANT
:
916 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
920 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
922 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
924 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
927 _FPERROR(efd
, "unexpected string \"%s\"", str
);
934 _FPERROR(efd
, "%s", "unexpected unary expression type");
944 enum bt_ctf_byte_order
byte_order_from_unary_expr(FILE *efd
,
945 struct ctf_node
*unary_expr
)
948 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
950 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
952 "\"byte_order\" attribute: expecting string");
956 str
= unary_expr
->u
.unary_expression
.u
.string
;
958 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
959 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
960 } else if (!strcmp(str
, "le")) {
961 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
962 } else if (!strcmp(str
, "native")) {
963 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
965 _FPERROR(efd
, "unexpected \"byte_order\" attribute value \"%s\"; should be \"be\", \"le\", \"network\", or \"native\"",
975 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
976 struct ctf_node
*uexpr
)
978 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(ctx
->efd
, uexpr
);
980 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
981 bo
= bt_ctf_trace_get_byte_order(ctx
->trace
);
988 int is_align_valid(uint64_t align
)
990 return (align
!= 0) && !(align
& (align
- 1));
994 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
999 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1004 switch (type_specifier
->u
.type_specifier
.type
) {
1006 g_string_append(str
, "void");
1009 g_string_append(str
, "char");
1011 case TYPESPEC_SHORT
:
1012 g_string_append(str
, "short");
1015 g_string_append(str
, "int");
1018 g_string_append(str
, "long");
1020 case TYPESPEC_FLOAT
:
1021 g_string_append(str
, "float");
1023 case TYPESPEC_DOUBLE
:
1024 g_string_append(str
, "double");
1026 case TYPESPEC_SIGNED
:
1027 g_string_append(str
, "signed");
1029 case TYPESPEC_UNSIGNED
:
1030 g_string_append(str
, "unsigned");
1033 g_string_append(str
, "bool");
1035 case TYPESPEC_COMPLEX
:
1036 g_string_append(str
, "_Complex");
1038 case TYPESPEC_IMAGINARY
:
1039 g_string_append(str
, "_Imaginary");
1041 case TYPESPEC_CONST
:
1042 g_string_append(str
, "const");
1044 case TYPESPEC_ID_TYPE
:
1045 if (type_specifier
->u
.type_specifier
.id_type
) {
1046 g_string_append(str
,
1047 type_specifier
->u
.type_specifier
.id_type
);
1050 case TYPESPEC_STRUCT
:
1052 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1054 if (!node
->u
._struct
.name
) {
1055 _PERROR("%s", "unexpected empty structure name");
1060 g_string_append(str
, "struct ");
1061 g_string_append(str
, node
->u
._struct
.name
);
1064 case TYPESPEC_VARIANT
:
1066 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1068 if (!node
->u
.variant
.name
) {
1069 _PERROR("%s", "unexpected empty variant name");
1074 g_string_append(str
, "variant ");
1075 g_string_append(str
, node
->u
.variant
.name
);
1080 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1082 if (!node
->u
._enum
.enum_id
) {
1083 _PERROR("%s", "unexpected empty enum name");
1088 g_string_append(str
, "enum ");
1089 g_string_append(str
, node
->u
._enum
.enum_id
);
1092 case TYPESPEC_FLOATING_POINT
:
1093 case TYPESPEC_INTEGER
:
1094 case TYPESPEC_STRING
:
1096 _PERROR("%s", "unknown specifier");
1106 int get_type_specifier_list_name(struct ctx
*ctx
,
1107 struct ctf_node
*type_specifier_list
, GString
*str
)
1110 struct ctf_node
*iter
;
1111 int alias_item_nr
= 0;
1112 struct bt_list_head
*head
=
1113 &type_specifier_list
->u
.type_specifier_list
.head
;
1115 bt_list_for_each_entry(iter
, head
, siblings
) {
1116 if (alias_item_nr
!= 0) {
1117 g_string_append(str
, " ");
1121 ret
= get_type_specifier_name(ctx
, iter
, str
);
1132 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1133 struct ctf_node
*type_specifier_list
,
1134 struct ctf_node
*node_type_declarator
)
1140 struct ctf_node
*iter
;
1141 struct bt_list_head
*pointers
=
1142 &node_type_declarator
->u
.type_declarator
.pointers
;
1144 str
= g_string_new("");
1145 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1147 g_string_free(str
, TRUE
);
1151 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1152 g_string_append(str
, " *");
1154 if (iter
->u
.pointer
.const_qualifier
) {
1155 g_string_append(str
, " const");
1159 str_c
= g_string_free(str
, FALSE
);
1160 qalias
= g_quark_from_string(str_c
);
1168 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1169 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1170 struct bt_ctf_field_type
**field_decl
,
1171 struct bt_ctf_field_type
*nested_decl
)
1174 * During this whole function, nested_decl is always OURS,
1175 * whereas field_decl is an output which we create, but
1176 * belongs to the caller (it is moved).
1182 /* Validate type declarator node */
1183 if (node_type_declarator
) {
1184 if (node_type_declarator
->u
.type_declarator
.type
==
1190 /* TODO: GCC bitfields not supported yet */
1191 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1193 _PERROR("%s", "GCC bitfields are not supported as of this version");
1199 /* Find the right nested declaration if not provided */
1201 struct bt_list_head
*pointers
=
1202 &node_type_declarator
->u
.type_declarator
.pointers
;
1204 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1206 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1209 * If we have a pointer declarator, it HAS to
1210 * be present in the typealiases (else fail).
1212 qalias
= create_typealias_identifier(ctx
,
1213 type_specifier_list
, node_type_declarator
);
1215 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1216 g_quark_to_string(qalias
), -1);
1218 _PERROR("cannot find typealias \"%s\"",
1219 g_quark_to_string(qalias
));
1224 /* Make a copy of it */
1225 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1226 BT_PUT(nested_decl
);
1227 if (!nested_decl_copy
) {
1228 _PERROR("%s", "cannot copy nested declaration");
1233 BT_MOVE(nested_decl
, nested_decl_copy
);
1235 /* Force integer's base to 16 since it's a pointer */
1236 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1237 bt_ctf_field_type_integer_set_base(nested_decl
,
1238 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1241 ret
= visit_type_specifier_list(ctx
,
1242 type_specifier_list
, &nested_decl
);
1244 assert(!nested_decl
);
1250 assert(nested_decl
);
1252 if (!node_type_declarator
) {
1253 BT_MOVE(*field_decl
, nested_decl
);
1257 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1258 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1260 node_type_declarator
->u
.type_declarator
.u
.id
;
1262 *field_name
= g_quark_from_string(id
);
1267 BT_MOVE(*field_decl
, nested_decl
);
1270 struct ctf_node
*first
;
1271 _BT_CTF_FIELD_TYPE_INIT(decl
);
1272 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1273 struct bt_list_head
*length
=
1274 &node_type_declarator
->
1275 u
.type_declarator
.u
.nested
.length
;
1277 /* Create array/sequence, pass nested_decl as child */
1278 if (bt_list_empty(length
)) {
1280 "expecting length field reference or value");
1285 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1286 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1291 switch (first
->u
.unary_expression
.type
) {
1292 case UNARY_UNSIGNED_CONSTANT
:
1295 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1297 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1298 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1300 BT_PUT(nested_decl
);
1303 "cannot create array declaration");
1308 BT_MOVE(decl
, array_decl
);
1313 /* Lookup unsigned integer definition, create seq. */
1314 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1315 char *length_name
= concatenate_unary_strings(length
);
1322 seq_decl
= bt_ctf_field_type_sequence_create(
1323 nested_decl
, length_name
);
1324 g_free(length_name
);
1325 BT_PUT(nested_decl
);
1328 "cannot create sequence declaration");
1333 BT_MOVE(decl
, seq_decl
);
1341 assert(!nested_decl
);
1343 assert(!*field_decl
);
1346 * At this point, we found the next nested declaration.
1347 * We currently own this (and lost the ownership of
1348 * nested_decl in the meantime). Pass this next
1349 * nested declaration as the content of the outer
1350 * container, MOVING its ownership.
1352 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1354 node_type_declarator
->
1355 u
.type_declarator
.u
.nested
.type_declarator
,
1356 &outer_field_decl
, decl
);
1359 assert(!outer_field_decl
);
1364 assert(outer_field_decl
);
1365 BT_MOVE(*field_decl
, outer_field_decl
);
1369 BT_PUT(nested_decl
);
1370 assert(*field_decl
);
1375 BT_PUT(nested_decl
);
1376 BT_PUT(*field_decl
);
1382 int visit_struct_decl_field(struct ctx
*ctx
,
1383 struct bt_ctf_field_type
*struct_decl
,
1384 struct ctf_node
*type_specifier_list
,
1385 struct bt_list_head
*type_declarators
)
1388 struct ctf_node
*iter
;
1389 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1391 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1394 const char *field_name
;
1395 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1397 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1398 &qfield_name
, iter
, &field_decl
, NULL
);
1400 assert(!field_decl
);
1401 _PERROR("%s", "unable to find structure field declaration type");
1406 field_name
= g_quark_to_string(qfield_name
);
1408 /* Check if field with same name already exists */
1410 bt_ctf_field_type_structure_get_field_type_by_name(
1411 struct_decl
, field_name
);
1413 BT_PUT(efield_decl
);
1414 _PERROR("duplicate field \"%s\" in structure",
1420 /* Add field to structure */
1421 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1422 field_decl
, field_name
);
1425 _PERROR("cannot add field \"%s\" to structure",
1426 g_quark_to_string(qfield_name
));
1440 int visit_variant_decl_field(struct ctx
*ctx
,
1441 struct bt_ctf_field_type
*variant_decl
,
1442 struct ctf_node
*type_specifier_list
,
1443 struct bt_list_head
*type_declarators
)
1446 struct ctf_node
*iter
;
1447 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1449 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1452 const char *field_name
;
1453 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1455 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1456 &qfield_name
, iter
, &field_decl
, NULL
);
1458 assert(!field_decl
);
1460 "unable to find variant field declaration type");
1465 field_name
= g_quark_to_string(qfield_name
);
1467 /* Check if field with same name already exists */
1469 bt_ctf_field_type_variant_get_field_type_by_name(
1470 variant_decl
, field_name
);
1472 BT_PUT(efield_decl
);
1473 _PERROR("duplicate field \"%s\" in variant",
1479 /* Add field to structure */
1480 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1481 field_decl
, field_name
);
1484 _PERROR("cannot add field \"%s\" to variant",
1485 g_quark_to_string(qfield_name
));
1499 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1500 struct bt_list_head
*type_declarators
)
1504 struct ctf_node
*iter
;
1505 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1507 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1508 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1509 &qidentifier
, iter
, &type_decl
, NULL
);
1511 _PERROR("%s", "problem creating type declaration");
1516 /* Do not allow typedef and typealias of untagged variants */
1517 if (bt_ctf_field_type_is_variant(type_decl
)) {
1518 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1519 _PERROR("%s", "typedef of untagged variant is not allowed");
1525 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1526 g_quark_to_string(qidentifier
), type_decl
);
1528 _PERROR("cannot register typedef \"%s\"",
1529 g_quark_to_string(qidentifier
));
1541 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1542 struct ctf_node
*alias
)
1546 struct ctf_node
*node
;
1547 GQuark qdummy_field_name
;
1548 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1550 /* Create target type declaration */
1551 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1554 node
= _BT_LIST_FIRST_ENTRY(
1555 &target
->u
.typealias_target
.type_declarators
,
1556 struct ctf_node
, siblings
);
1559 ret
= visit_type_declarator(ctx
,
1560 target
->u
.typealias_target
.type_specifier_list
,
1561 &qdummy_field_name
, node
, &type_decl
, NULL
);
1564 _PERROR("%s", "problem creating type declaration");
1568 /* Do not allow typedef and typealias of untagged variants */
1569 if (bt_ctf_field_type_is_variant(type_decl
)) {
1570 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1572 "typealias of untagged variant is not allowed");
1579 * The semantic validator does not check whether the target is
1580 * abstract or not (if it has an identifier). Check it here.
1582 if (qdummy_field_name
!= 0) {
1583 _PERROR("%s", "expecting empty identifier");
1588 /* Create alias identifier */
1589 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1590 struct ctf_node
, siblings
);
1591 qalias
= create_typealias_identifier(ctx
,
1592 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1593 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1594 g_quark_to_string(qalias
), type_decl
);
1596 _PERROR("cannot register typealias \"%s\"",
1597 g_quark_to_string(qalias
));
1608 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1609 struct bt_ctf_field_type
*struct_decl
)
1613 switch (entry_node
->type
) {
1615 ret
= visit_typedef(ctx
,
1616 entry_node
->u
._typedef
.type_specifier_list
,
1617 &entry_node
->u
._typedef
.type_declarators
);
1620 "cannot add typedef in \"struct\" declaration");
1624 case NODE_TYPEALIAS
:
1625 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1626 entry_node
->u
.typealias
.alias
);
1629 "cannot add typealias in \"struct\" declaration");
1633 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1635 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1636 entry_node
->u
.struct_or_variant_declaration
.
1637 type_specifier_list
,
1638 &entry_node
->u
.struct_or_variant_declaration
.
1645 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1655 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1656 struct bt_ctf_field_type
*variant_decl
)
1660 switch (entry_node
->type
) {
1662 ret
= visit_typedef(ctx
,
1663 entry_node
->u
._typedef
.type_specifier_list
,
1664 &entry_node
->u
._typedef
.type_declarators
);
1667 "cannot add typedef in \"variant\" declaration");
1671 case NODE_TYPEALIAS
:
1672 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1673 entry_node
->u
.typealias
.alias
);
1676 "cannot add typealias in \"variant\" declaration");
1680 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1682 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1683 entry_node
->u
.struct_or_variant_declaration
.
1684 type_specifier_list
,
1685 &entry_node
->u
.struct_or_variant_declaration
.
1692 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1702 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1703 struct bt_list_head
*decl_list
, int has_body
,
1704 struct bt_list_head
*min_align
,
1705 struct bt_ctf_field_type
**struct_decl
)
1709 *struct_decl
= NULL
;
1711 /* For named struct (without body), lookup in declaration scope */
1713 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1720 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1722 if (!*struct_decl
) {
1723 _PERROR("cannot find \"struct %s\"", name
);
1728 /* Make a copy of it */
1729 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1730 if (!struct_decl_copy
) {
1732 "cannot create copy of structure declaration");
1737 BT_MOVE(*struct_decl
, struct_decl_copy
);
1739 struct ctf_node
*entry_node
;
1740 uint64_t min_align_value
= 0;
1743 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1745 estruct_decl
= ctx_decl_scope_lookup_struct(
1746 ctx
->current_scope
, name
, 1);
1748 BT_PUT(estruct_decl
);
1749 _PERROR("\"struct %s\" already declared in local scope",
1756 if (!bt_list_empty(min_align
)) {
1757 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1759 _PERROR("%s", "unexpected unary expression for structure declaration's \"align\" attribute");
1764 *struct_decl
= bt_ctf_field_type_structure_create();
1765 if (!*struct_decl
) {
1766 _PERROR("%s", "cannot create structure declaration");
1771 ret
= ctx_push_scope(ctx
);
1773 _PERROR("%s", "cannot push scope");
1777 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1778 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1789 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1790 name
, *struct_decl
);
1792 _PERROR("cannot register \"struct %s\" in declaration scope",
1802 BT_PUT(*struct_decl
);
1808 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1809 const char *tag
, struct bt_list_head
*decl_list
,
1810 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1813 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1815 *variant_decl
= NULL
;
1817 /* For named variant (without body), lookup in declaration scope */
1819 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1826 untagged_variant_decl
=
1827 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1829 if (!untagged_variant_decl
) {
1830 _PERROR("cannot find \"variant %s\"", name
);
1835 /* Make a copy of it */
1836 variant_decl_copy
= bt_ctf_field_type_copy(
1837 untagged_variant_decl
);
1838 if (!variant_decl_copy
) {
1840 "cannot create copy of structure declaration");
1845 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1847 struct ctf_node
*entry_node
;
1850 struct bt_ctf_field_type
*evariant_decl
=
1851 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1854 if (evariant_decl
) {
1855 BT_PUT(evariant_decl
);
1856 _PERROR("\"variant %s\" already declared in local scope",
1863 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1865 if (!untagged_variant_decl
) {
1866 _PERROR("%s", "cannot create variant declaration");
1871 ret
= ctx_push_scope(ctx
);
1873 _PERROR("%s", "cannot push scope");
1877 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1878 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1879 untagged_variant_decl
);
1889 ret
= ctx_decl_scope_register_variant(
1890 ctx
->current_scope
, name
,
1891 untagged_variant_decl
);
1893 _PERROR("cannot register \"variant %s\" in declaration scope",
1901 * If tagged, create tagged variant and return; otherwise
1902 * return untagged variant.
1905 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1908 * At this point, we have a fresh untagged variant; nobody
1909 * else owns it. Set its tag now.
1911 ret
= bt_ctf_field_type_variant_set_tag_name(
1912 untagged_variant_decl
, tag
);
1917 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1920 assert(!untagged_variant_decl
);
1921 assert(*variant_decl
);
1926 BT_PUT(untagged_variant_decl
);
1927 BT_PUT(*variant_decl
);
1933 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
1934 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
1938 struct ctf_node
*iter
;
1939 int64_t start
= 0, end
= 0;
1940 const char *label
= enumerator
->u
.enumerator
.id
;
1941 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
1943 bt_list_for_each_entry(iter
, values
, siblings
) {
1946 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
1947 _PERROR("wrong unary expression for enumeration label \"%s\"",
1959 switch (iter
->u
.unary_expression
.type
) {
1960 case UNARY_SIGNED_CONSTANT
:
1961 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
1963 case UNARY_UNSIGNED_CONSTANT
:
1965 iter
->u
.unary_expression
.u
.unsigned_constant
;
1968 _PERROR("invalid enumeration entry: \"%s\"",
1975 _PERROR("invalid enumeration entry: \"%s\"",
1995 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
1998 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
1999 label
, (uint64_t) start
, (uint64_t) end
);
2002 _PERROR("cannot add mapping to enumeration for label \"%s\"",
2014 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2015 struct ctf_node
*container_type
,
2016 struct bt_list_head
*enumerator_list
,
2018 struct bt_ctf_field_type
**enum_decl
)
2022 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2026 /* For named enum (without body), lookup in declaration scope */
2028 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2035 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2038 _PERROR("cannot find \"enum %s\"", name
);
2043 /* Make a copy of it */
2044 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2045 if (!enum_decl_copy
) {
2047 "cannot create copy of enumeration declaration");
2053 BT_MOVE(*enum_decl
, enum_decl_copy
);
2055 struct ctf_node
*iter
;
2056 int64_t last_value
= 0;
2059 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2061 eenum_decl
= ctx_decl_scope_lookup_enum(
2062 ctx
->current_scope
, name
, 1);
2065 _PERROR("\"enum %s\" already declared in local scope",
2072 if (!container_type
) {
2073 integer_decl
= ctx_decl_scope_lookup_alias(
2074 ctx
->current_scope
, "int", -1);
2075 if (!integer_decl
) {
2076 _PERROR("%s", "cannot find \"int\" type for enumeration");
2081 ret
= visit_type_declarator(ctx
, container_type
,
2082 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2084 assert(!integer_decl
);
2090 assert(integer_decl
);
2092 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2093 _PERROR("%s", "container type for enumeration is not an integer");
2098 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2100 _PERROR("%s", "cannot create enumeration declaration");
2105 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2106 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2108 bt_ctf_field_type_integer_get_signed(integer_decl
));
2115 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2123 BT_PUT(integer_decl
);
2128 BT_PUT(integer_decl
);
2135 int visit_type_specifier(struct ctx
*ctx
,
2136 struct ctf_node
*type_specifier_list
,
2137 struct bt_ctf_field_type
**decl
)
2140 GString
*str
= NULL
;
2141 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2144 str
= g_string_new("");
2145 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2150 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2152 _PERROR("cannot find type alias \"%s\"", str
->str
);
2157 /* Make a copy of the type declaration */
2158 decl_copy
= bt_ctf_field_type_copy(*decl
);
2160 _PERROR("%s", "cannot create copy of type declaration");
2165 BT_MOVE(*decl
, decl_copy
);
2166 (void) g_string_free(str
, TRUE
);
2173 (void) g_string_free(str
, TRUE
);
2182 int visit_integer_decl(struct ctx
*ctx
,
2183 struct bt_list_head
*expressions
,
2184 struct bt_ctf_field_type
**integer_decl
)
2189 struct ctf_node
*expression
;
2190 uint64_t alignment
= 0, size
= 0;
2191 struct bt_ctf_clock
*mapped_clock
= NULL
;
2192 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2193 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2194 enum bt_ctf_byte_order byte_order
=
2195 bt_ctf_trace_get_byte_order(ctx
->trace
);
2197 *integer_decl
= NULL
;
2199 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2200 struct ctf_node
*left
, *right
;
2202 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2203 struct ctf_node
, siblings
);
2204 right
= _BT_LIST_FIRST_ENTRY(
2205 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2208 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2213 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2214 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2215 _PERROR_DUP_ATTR("signed",
2216 "integer declaration");
2221 signedness
= get_boolean(ctx
->efd
, right
);
2222 if (signedness
< 0) {
2227 _SET(&set
, _INTEGER_SIGNED_SET
);
2228 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2230 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2231 _PERROR_DUP_ATTR("byte_order",
2232 "integer declaration");
2237 byte_order
= get_real_byte_order(ctx
, right
);
2238 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2239 _PERROR("%s", "invalid \"byte_order\" attribute in integer declaration");
2244 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2245 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2246 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2247 _PERROR_DUP_ATTR("size",
2248 "integer declaration");
2253 if (right
->u
.unary_expression
.type
!=
2254 UNARY_UNSIGNED_CONSTANT
) {
2255 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting unsigned constant");
2260 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2262 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting positive constant");
2265 } else if (size
> 64) {
2266 _PERROR("%s", "invalid \"size\" attribute in integer declaration: integers over 64-bit are not supported as of this version");
2271 _SET(&set
, _INTEGER_SIZE_SET
);
2272 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2274 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2275 _PERROR_DUP_ATTR("align",
2276 "integer declaration");
2281 if (right
->u
.unary_expression
.type
!=
2282 UNARY_UNSIGNED_CONSTANT
) {
2283 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting unsigned constant");
2289 right
->u
.unary_expression
.u
.unsigned_constant
;
2290 if (!is_align_valid(alignment
)) {
2291 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting power of two");
2296 _SET(&set
, _INTEGER_ALIGN_SET
);
2297 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2298 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2299 _PERROR_DUP_ATTR("base", "integer declaration");
2304 switch (right
->u
.unary_expression
.type
) {
2305 case UNARY_UNSIGNED_CONSTANT
:
2307 uint64_t constant
= right
->u
.unary_expression
.
2308 u
.unsigned_constant
;
2312 base
= BT_CTF_INTEGER_BASE_BINARY
;
2315 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2318 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2321 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2324 _PERROR("invalid \"base\" attribute in integer declaration: %" PRIu64
,
2325 right
->u
.unary_expression
.u
.unsigned_constant
);
2333 char *s_right
= concatenate_unary_strings(
2334 &expression
->u
.ctf_expression
.right
);
2336 _PERROR("%s", "unexpected unary expression for integer declaration's \"base\" attribute");
2341 if (!strcmp(s_right
, "decimal") ||
2342 !strcmp(s_right
, "dec") ||
2343 !strcmp(s_right
, "d") ||
2344 !strcmp(s_right
, "i") ||
2345 !strcmp(s_right
, "u")) {
2346 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2347 } else if (!strcmp(s_right
, "hexadecimal") ||
2348 !strcmp(s_right
, "hex") ||
2349 !strcmp(s_right
, "x") ||
2350 !strcmp(s_right
, "X") ||
2351 !strcmp(s_right
, "p")) {
2352 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2353 } else if (!strcmp(s_right
, "octal") ||
2354 !strcmp(s_right
, "oct") ||
2355 !strcmp(s_right
, "o")) {
2356 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2357 } else if (!strcmp(s_right
, "binary") ||
2358 !strcmp(s_right
, "b")) {
2359 base
= BT_CTF_INTEGER_BASE_BINARY
;
2361 _PERROR("unexpected unary expression for integer declaration's \"base\" attribute: \"%s\"",
2372 _PERROR("%s", "invalid \"base\" attribute in integer declaration: expecting unsigned constant or unary string");
2377 _SET(&set
, _INTEGER_BASE_SET
);
2378 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2382 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2383 _PERROR_DUP_ATTR("encoding",
2384 "integer declaration");
2389 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2390 _PERROR("%s", "invalid \"encoding\" attribute in integer declaration: expecting unary string");
2395 s_right
= concatenate_unary_strings(
2396 &expression
->u
.ctf_expression
.right
);
2398 _PERROR("%s", "unexpected unary expression for integer declaration's \"encoding\" attribute");
2403 if (!strcmp(s_right
, "UTF8") ||
2404 !strcmp(s_right
, "utf8") ||
2405 !strcmp(s_right
, "utf-8") ||
2406 !strcmp(s_right
, "UTF-8")) {
2407 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2408 } else if (!strcmp(s_right
, "ASCII") ||
2409 !strcmp(s_right
, "ascii")) {
2410 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2411 } else if (!strcmp(s_right
, "none")) {
2412 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2414 _PERROR("invalid \"encoding\" attribute in integer declaration: unknown encoding \"%s\"",
2422 _SET(&set
, _INTEGER_ENCODING_SET
);
2423 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2424 const char *clock_name
;
2426 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2427 _PERROR_DUP_ATTR("map", "integer declaration");
2432 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2433 _PERROR("%s", "invalid \"map\" attribute in integer declaration: expecting unary string");
2439 get_map_clock_name_value(
2440 &expression
->u
.ctf_expression
.right
);
2442 char *s_right
= concatenate_unary_strings(
2443 &expression
->u
.ctf_expression
.right
);
2446 _PERROR("%s", "unexpected unary expression for integer declaration's \"map\" attribute");
2451 _PWARNING("invalid \"map\" attribute in integer declaration: unknown clock: \"%s\"",
2453 _SET(&set
, _INTEGER_MAP_SET
);
2458 mapped_clock
= bt_ctf_trace_get_clock_by_name(
2459 ctx
->trace
, clock_name
);
2460 if (!mapped_clock
) {
2461 _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock \"%s\"",
2467 _SET(&set
, _INTEGER_MAP_SET
);
2469 _PWARNING("unknown attribute \"%s\" in integer declaration",
2470 left
->u
.unary_expression
.u
.string
);
2474 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2476 "missing \"size\" attribute in integer declaration");
2481 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2482 if (size
% CHAR_BIT
) {
2483 /* Bit-packed alignment */
2486 /* Byte-packed alignment */
2487 alignment
= CHAR_BIT
;
2491 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2492 if (!*integer_decl
) {
2493 _PERROR("%s", "cannot create integer declaration");
2498 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2499 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2500 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2501 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2502 (unsigned int) alignment
);
2503 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2507 ret
|= bt_ctf_field_type_integer_set_mapped_clock(
2508 *integer_decl
, mapped_clock
);
2509 bt_put(mapped_clock
);
2510 mapped_clock
= NULL
;
2514 _PERROR("%s", "cannot configure integer declaration");
2523 bt_put(mapped_clock
);
2526 BT_PUT(*integer_decl
);
2532 int visit_floating_point_number_decl(struct ctx
*ctx
,
2533 struct bt_list_head
*expressions
,
2534 struct bt_ctf_field_type
**float_decl
)
2538 struct ctf_node
*expression
;
2539 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2540 enum bt_ctf_byte_order byte_order
=
2541 bt_ctf_trace_get_byte_order(ctx
->trace
);
2545 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2546 struct ctf_node
*left
, *right
;
2548 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2549 struct ctf_node
, siblings
);
2550 right
= _BT_LIST_FIRST_ENTRY(
2551 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2554 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2559 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2560 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2561 _PERROR_DUP_ATTR("byte_order",
2562 "floating point number declaration");
2567 byte_order
= get_real_byte_order(ctx
, right
);
2568 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2569 _PERROR("%s", "invalid \"byte_order\" attribute in floating point number declaration");
2574 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2575 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2577 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2578 _PERROR_DUP_ATTR("exp_dig",
2579 "floating point number declaration");
2584 if (right
->u
.unary_expression
.type
!=
2585 UNARY_UNSIGNED_CONSTANT
) {
2586 _PERROR("%s", "invalid \"exp_dig\" attribute in floating point number declaration: expecting unsigned constant");
2591 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2592 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2593 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2595 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2596 _PERROR_DUP_ATTR("mant_dig",
2597 "floating point number declaration");
2602 if (right
->u
.unary_expression
.type
!=
2603 UNARY_UNSIGNED_CONSTANT
) {
2604 _PERROR("%s", "invalid \"mant_dig\" attribute in floating point number declaration: expecting unsigned constant");
2609 mant_dig
= right
->u
.unary_expression
.u
.
2611 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2612 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2614 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2615 _PERROR_DUP_ATTR("align",
2616 "floating point number declaration");
2621 if (right
->u
.unary_expression
.type
!=
2622 UNARY_UNSIGNED_CONSTANT
) {
2623 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting unsigned constant");
2628 alignment
= right
->u
.unary_expression
.u
.
2631 if (!is_align_valid(alignment
)) {
2632 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting power of two");
2637 _SET(&set
, _FLOAT_ALIGN_SET
);
2639 _PWARNING("unknown attribute \"%s\" in floating point number declaration",
2640 left
->u
.unary_expression
.u
.string
);
2644 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2645 _PERROR("%s", "missing \"mant_dig\" attribute in floating point number declaration");
2650 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2651 _PERROR("%s", "missing \"exp_dig\" attribute in floating point number declaration");
2656 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2657 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2658 /* Bit-packed alignment */
2661 /* Byte-packed alignment */
2662 alignment
= CHAR_BIT
;
2666 *float_decl
= bt_ctf_field_type_floating_point_create();
2669 "cannot create floating point number declaration");
2674 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2675 *float_decl
, exp_dig
);
2676 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2677 *float_decl
, mant_dig
);
2678 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2679 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2682 "cannot configure floating point number declaration");
2690 BT_PUT(*float_decl
);
2696 int visit_string_decl(struct ctx
*ctx
,
2697 struct bt_list_head
*expressions
,
2698 struct bt_ctf_field_type
**string_decl
)
2702 struct ctf_node
*expression
;
2703 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2705 *string_decl
= NULL
;
2707 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2708 struct ctf_node
*left
, *right
;
2710 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2711 struct ctf_node
, siblings
);
2712 right
= _BT_LIST_FIRST_ENTRY(
2713 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2716 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2721 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2724 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2725 _PERROR_DUP_ATTR("encoding",
2726 "string declaration");
2731 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2732 _PERROR("%s", "invalid \"encoding\" attribute in string declaration: expecting unary string");
2737 s_right
= concatenate_unary_strings(
2738 &expression
->u
.ctf_expression
.right
);
2740 _PERROR("%s", "unexpected unary expression for string declaration's \"encoding\" attribute");
2745 if (!strcmp(s_right
, "UTF8") ||
2746 !strcmp(s_right
, "utf8") ||
2747 !strcmp(s_right
, "utf-8") ||
2748 !strcmp(s_right
, "UTF-8")) {
2749 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2750 } else if (!strcmp(s_right
, "ASCII") ||
2751 !strcmp(s_right
, "ascii")) {
2752 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2753 } else if (!strcmp(s_right
, "none")) {
2754 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2756 _PERROR("invalid \"encoding\" attribute in string declaration: unknown encoding \"%s\"",
2764 _SET(&set
, _STRING_ENCODING_SET
);
2766 _PWARNING("unknown attribute \"%s\" in string declaration",
2767 left
->u
.unary_expression
.u
.string
);
2771 *string_decl
= bt_ctf_field_type_string_create();
2772 if (!*string_decl
) {
2773 _PERROR("%s", "cannot create string declaration");
2778 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2780 _PERROR("%s", "cannot configure string declaration");
2788 BT_PUT(*string_decl
);
2794 int visit_type_specifier_list(struct ctx
*ctx
,
2795 struct ctf_node
*ts_list
,
2796 struct bt_ctf_field_type
**decl
)
2799 struct ctf_node
*first
, *node
;
2803 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2808 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
2809 struct ctf_node
, siblings
);
2810 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2815 node
= first
->u
.type_specifier
.node
;
2817 switch (first
->u
.type_specifier
.type
) {
2818 case TYPESPEC_INTEGER
:
2819 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
2826 case TYPESPEC_FLOATING_POINT
:
2827 ret
= visit_floating_point_number_decl(ctx
,
2828 &node
->u
.floating_point
.expressions
, decl
);
2834 case TYPESPEC_STRING
:
2835 ret
= visit_string_decl(ctx
,
2836 &node
->u
.string
.expressions
, decl
);
2842 case TYPESPEC_STRUCT
:
2843 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
2844 &node
->u
._struct
.declaration_list
,
2845 node
->u
._struct
.has_body
,
2846 &node
->u
._struct
.min_align
, decl
);
2852 case TYPESPEC_VARIANT
:
2853 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
2854 node
->u
.variant
.choice
,
2855 &node
->u
.variant
.declaration_list
,
2856 node
->u
.variant
.has_body
, decl
);
2863 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
2864 node
->u
._enum
.container_type
,
2865 &node
->u
._enum
.enumerator_list
,
2866 node
->u
._enum
.has_body
, decl
);
2874 case TYPESPEC_SHORT
:
2877 case TYPESPEC_FLOAT
:
2878 case TYPESPEC_DOUBLE
:
2879 case TYPESPEC_SIGNED
:
2880 case TYPESPEC_UNSIGNED
:
2882 case TYPESPEC_COMPLEX
:
2883 case TYPESPEC_IMAGINARY
:
2884 case TYPESPEC_CONST
:
2885 case TYPESPEC_ID_TYPE
:
2886 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
2893 _PERROR("unexpected node type: %d",
2894 (int) first
->u
.type_specifier
.type
);
2910 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
2911 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
2916 _BT_CTF_FIELD_TYPE_INIT(decl
);
2918 switch (node
->type
) {
2920 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
2921 &node
->u
._typedef
.type_declarators
);
2924 "cannot add typedef in \"event\" declaration");
2928 case NODE_TYPEALIAS
:
2929 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
2930 node
->u
.typealias
.alias
);
2932 _PERROR("%s", "cannot add typealias in \"event\" declaration");
2936 case NODE_CTF_EXPRESSION
:
2938 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2944 if (!strcmp(left
, "name")) {
2945 /* This is already known at this stage */
2946 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
2947 _PERROR_DUP_ATTR("name", "event declaration");
2952 _SET(set
, _EVENT_NAME_SET
);
2953 } else if (!strcmp(left
, "id")) {
2956 if (_IS_SET(set
, _EVENT_ID_SET
)) {
2957 _PERROR_DUP_ATTR("id", "event declaration");
2962 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
2964 if (ret
|| id
< 0) {
2965 _PERROR("%s", "unexpected unary expression for event declaration's \"id\" attribute");
2970 ret
= bt_ctf_event_class_set_id(event_class
, id
);
2973 "cannot set event declaration's ID");
2977 _SET(set
, _EVENT_ID_SET
);
2978 } else if (!strcmp(left
, "stream_id")) {
2979 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
2980 _PERROR_DUP_ATTR("stream_id",
2981 "event declaration");
2986 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
2987 (uint64_t *) stream_id
);
2988 if (ret
|| *stream_id
< 0) {
2989 _PERROR("%s", "unexpected unary expression for event declaration's \"stream_id\" attribute");
2994 _SET(set
, _EVENT_STREAM_ID_SET
);
2995 } else if (!strcmp(left
, "context")) {
2996 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
2997 _PERROR("%s", "duplicate \"context\" entry in event declaration");
3002 ret
= visit_type_specifier_list(ctx
,
3003 _BT_LIST_FIRST_ENTRY(
3004 &node
->u
.ctf_expression
.right
,
3005 struct ctf_node
, siblings
),
3008 _PERROR("%s", "cannot create event context declaration");
3013 ret
= bt_ctf_event_class_set_context_type(
3017 _PERROR("%s", "cannot set event's context declaration");
3021 _SET(set
, _EVENT_CONTEXT_SET
);
3022 } else if (!strcmp(left
, "fields")) {
3023 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3024 _PERROR("%s", "duplicate \"fields\" entry in event declaration");
3029 ret
= visit_type_specifier_list(ctx
,
3030 _BT_LIST_FIRST_ENTRY(
3031 &node
->u
.ctf_expression
.right
,
3032 struct ctf_node
, siblings
),
3035 _PERROR("%s", "cannot create event payload declaration");
3040 ret
= bt_ctf_event_class_set_payload_type(
3044 _PERROR("%s", "cannot set event's payload declaration");
3048 _SET(set
, _EVENT_FIELDS_SET
);
3049 } else if (!strcmp(left
, "loglevel")) {
3052 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3053 _PERROR_DUP_ATTR("loglevel",
3054 "event declaration");
3059 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3062 _PERROR("%s", "unexpected unary expression for event declaration's \"loglevel\" attribute");
3067 // TODO: FIXME: set log level here
3069 _SET(set
, _EVENT_LOGLEVEL_SET
);
3070 } else if (!strcmp(left
, "model.emf.uri")) {
3073 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3074 _PERROR_DUP_ATTR("model.emf.uri",
3075 "event declaration");
3080 right
= concatenate_unary_strings(
3081 &node
->u
.ctf_expression
.right
);
3083 _PERROR("%s", "unexpected unary expression for event declaration's \"model.emf.uri\" attribute");
3088 // TODO: FIXME: set model EMF URI here
3091 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3093 _PWARNING("unknown attribute \"%s\" in event declaration",
3119 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3123 struct ctf_node
*iter
;
3124 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3126 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3127 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3131 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3136 if (!strcmp(left
, "name")) {
3137 name
= concatenate_unary_strings(
3138 &iter
->u
.ctf_expression
.right
);
3140 _PERROR("%s", "unexpected unary expression for event declaration's \"name\" attribute");
3162 int reset_event_decl_types(struct ctx
*ctx
,
3163 struct bt_ctf_event_class
*event_class
)
3168 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3170 _PERROR("%s", "cannot set initial NULL event context");
3174 /* Event payload. */
3175 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3177 _PERROR("%s", "cannot set initial NULL event payload");
3185 int reset_stream_decl_types(struct ctx
*ctx
,
3186 struct bt_ctf_stream_class
*stream_class
)
3190 /* Packet context. */
3191 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3193 _PERROR("%s", "cannot set initial empty packet context");
3198 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3200 _PERROR("%s", "cannot set initial empty event header");
3204 /* Event context. */
3205 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3207 _PERROR("%s", "cannot set initial empty stream event context");
3215 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3218 struct bt_ctf_stream_class
*stream_class
;
3220 stream_class
= bt_ctf_stream_class_create(NULL
);
3221 if (!stream_class
) {
3222 _PERROR("%s", "cannot create stream class");
3227 * Set packet context, event header, and event context to NULL to
3228 * override the default ones.
3230 ret
= reset_stream_decl_types(ctx
, stream_class
);
3235 return stream_class
;
3238 BT_PUT(stream_class
);
3244 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3249 struct ctf_node
*iter
;
3250 int64_t stream_id
= -1;
3251 char *event_name
= NULL
;
3252 struct bt_ctf_event_class
*event_class
= NULL
;
3253 struct bt_ctf_event_class
*eevent_class
;
3254 struct bt_ctf_stream_class
*stream_class
;
3255 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3257 if (node
->visited
) {
3261 node
->visited
= TRUE
;
3262 event_name
= get_event_decl_name(ctx
, node
);
3265 "missing \"name\" attribute in event declaration");
3270 event_class
= bt_ctf_event_class_create(event_name
);
3273 * Unset context and fields to override the default ones.
3275 ret
= reset_event_decl_types(ctx
, event_class
);
3281 ret
= ctx_push_scope(ctx
);
3283 _PERROR("%s", "cannot push scope");
3287 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3288 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3295 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3297 struct bt_ctf_stream_class
*new_stream_class
;
3299 /* Allow missing stream_id if there is only a single stream */
3300 switch (g_hash_table_size(ctx
->stream_classes
)) {
3302 /* Create stream if there's none */
3303 new_stream_class
= create_reset_stream_class(ctx
);
3304 if (!new_stream_class
) {
3309 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3311 _PERROR("%s", "cannot set stream's ID");
3312 BT_PUT(new_stream_class
);
3318 /* Move reference to visitor's context */
3319 g_hash_table_insert(ctx
->stream_classes
,
3320 (gpointer
) stream_id
, new_stream_class
);
3321 new_stream_class
= NULL
;
3325 /* Single stream: get its ID */
3326 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3327 stream_id
= (int64_t) keys
->data
;
3332 _PERROR("%s", "missing \"stream_id\" attribute in event declaration");
3340 assert(stream_id
>= 0);
3342 /* We have the stream ID now; borrow the stream class if found */
3343 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3344 (gpointer
) stream_id
);
3345 if (!stream_class
) {
3346 _PERROR("cannot find stream class with ID %" PRId64
,
3352 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3353 /* Allow only one event without ID per stream */
3354 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3357 "missing \"id\" field in event declaration");
3363 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3365 _PERROR("%s", "cannot set event's ID");
3370 event_id
= bt_ctf_event_class_get_id(event_class
);
3372 _PERROR("%s", "cannot get event's ID");
3377 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3380 BT_PUT(eevent_class
);
3381 _PERROR("%s", "duplicate event with ID %" PRId64
" in same stream");
3386 eevent_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
,
3389 BT_PUT(eevent_class
);
3390 eevent_class
= NULL
;
3392 "duplicate event with name \"%s\" in same stream");
3398 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3399 BT_PUT(event_class
);
3403 _PERROR("%s", "cannot add event class to stream class");
3412 BT_PUT(event_class
);
3414 /* stream_class is borrowed; it still belongs to the hash table */
3420 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3421 struct bt_ctf_stream_class
*stream_class
, int *set
)
3425 _BT_CTF_FIELD_TYPE_INIT(decl
);
3427 switch (node
->type
) {
3429 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3430 &node
->u
._typedef
.type_declarators
);
3433 "cannot add typedef in \"stream\" declaration");
3437 case NODE_TYPEALIAS
:
3438 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3439 node
->u
.typealias
.alias
);
3441 _PERROR("%s", "cannot add typealias in \"stream\" declaration");
3445 case NODE_CTF_EXPRESSION
:
3447 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3453 if (!strcmp(left
, "id")) {
3457 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3458 _PERROR_DUP_ATTR("id", "stream declaration");
3463 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3465 if (ret
|| id
< 0) {
3466 _PERROR("%s", "unexpected unary expression for stream declaration's \"id\" attribute");
3471 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3474 _PERROR("duplicate stream with ID %" PRId64
,
3480 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3483 "cannot set stream declaration's ID");
3487 _SET(set
, _STREAM_ID_SET
);
3488 } else if (!strcmp(left
, "event.header")) {
3489 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3490 _PERROR("%s", "duplicate \"event.header\" entry in stream declaration");
3495 ret
= visit_type_specifier_list(ctx
,
3496 _BT_LIST_FIRST_ENTRY(
3497 &node
->u
.ctf_expression
.right
,
3498 struct ctf_node
, siblings
),
3501 _PERROR("%s", "cannot create event header declaration");
3507 ret
= bt_ctf_stream_class_set_event_header_type(
3508 stream_class
, decl
);
3511 _PERROR("%s", "cannot set stream's event header declaration");
3515 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3516 } else if (!strcmp(left
, "event.context")) {
3517 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3518 _PERROR("%s", "duplicate \"event.context\" entry in stream declaration");
3523 ret
= visit_type_specifier_list(ctx
,
3524 _BT_LIST_FIRST_ENTRY(
3525 &node
->u
.ctf_expression
.right
,
3526 struct ctf_node
, siblings
),
3529 _PERROR("%s", "cannot create stream event context declaration");
3535 ret
= bt_ctf_stream_class_set_event_context_type(
3536 stream_class
, decl
);
3539 _PERROR("%s", "cannot set stream's event context declaration");
3543 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3544 } else if (!strcmp(left
, "packet.context")) {
3545 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3546 _PERROR("%s", "duplicate \"packet.context\" entry in stream declaration");
3551 ret
= visit_type_specifier_list(ctx
,
3552 _BT_LIST_FIRST_ENTRY(
3553 &node
->u
.ctf_expression
.right
,
3554 struct ctf_node
, siblings
),
3557 _PERROR("%s", "cannot create packet context declaration");
3563 ret
= bt_ctf_stream_class_set_packet_context_type(
3564 stream_class
, decl
);
3567 _PERROR("%s", "cannot set stream's packet context declaration");
3571 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3573 _PWARNING("unknown attribute \"%s\" in stream declaration",
3597 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3602 struct ctf_node
*iter
;
3603 struct bt_ctf_stream_class
*stream_class
= NULL
;
3604 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3606 if (node
->visited
) {
3610 node
->visited
= TRUE
;
3611 stream_class
= create_reset_stream_class(ctx
);
3612 if (!stream_class
) {
3617 ret
= ctx_push_scope(ctx
);
3619 _PERROR("%s", "cannot push scope");
3623 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3624 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3633 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3634 /* Check that packet header has stream_id field */
3635 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
3636 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3638 packet_header_decl
=
3639 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
3640 if (!packet_header_decl
) {
3642 "cannot get trace packet header declaration");
3647 bt_ctf_field_type_structure_get_field_type_by_name(
3648 packet_header_decl
, "stream_id");
3649 BT_PUT(packet_header_decl
);
3650 if (!stream_id_decl
) {
3651 _PERROR("%s", "missing \"stream_id\" field in packet header declaration, but \"id\" attribute is declared for stream");
3655 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
3656 BT_PUT(stream_id_decl
);
3657 _PERROR("%s", "\"stream_id\" field in packet header declaration is not an integer");
3661 BT_PUT(stream_id_decl
);
3663 /* Allow only _one_ ID-less stream */
3664 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
3666 "missing \"id\" field in stream declaration");
3671 /* Automatic ID: 0 */
3672 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
3675 id
= bt_ctf_stream_class_get_id(stream_class
);
3677 _PERROR("wrong stream ID: %" PRId64
, id
);
3682 /* Move reference to visitor's context */
3683 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
3685 stream_class
= NULL
;
3691 BT_PUT(stream_class
);
3697 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
3701 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3703 switch (node
->type
) {
3705 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3706 &node
->u
._typedef
.type_declarators
);
3709 "cannot add typedef in \"trace\" declaration");
3713 case NODE_TYPEALIAS
:
3714 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3715 node
->u
.typealias
.alias
);
3718 "cannot add typealias in \"trace\" declaration");
3722 case NODE_CTF_EXPRESSION
:
3724 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3730 if (!strcmp(left
, "major")) {
3731 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3732 _PERROR_DUP_ATTR("major", "trace declaration");
3737 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3740 _PERROR("%s", "unexpected unary expression for trace's \"major\" attribute");
3745 _SET(set
, _TRACE_MAJOR_SET
);
3746 } else if (!strcmp(left
, "minor")) {
3747 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3748 _PERROR_DUP_ATTR("minor", "trace declaration");
3753 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3756 _PERROR("%s", "unexpected unary expression for trace's \"minor\" attribute");
3761 _SET(set
, _TRACE_MINOR_SET
);
3762 } else if (!strcmp(left
, "uuid")) {
3763 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3764 _PERROR_DUP_ATTR("uuid", "trace declaration");
3769 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
3773 "invalid trace declaration's UUID");
3777 _SET(set
, _TRACE_UUID_SET
);
3778 } else if (!strcmp(left
, "byte_order")) {
3779 /* Native byte order is already known at this stage */
3780 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3781 _PERROR_DUP_ATTR("byte_order",
3782 "trace declaration");
3787 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3788 } else if (!strcmp(left
, "packet.header")) {
3789 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3790 _PERROR("%s", "duplicate \"packet.header\" entry in trace declaration");
3795 ret
= visit_type_specifier_list(ctx
,
3796 _BT_LIST_FIRST_ENTRY(
3797 &node
->u
.ctf_expression
.right
,
3798 struct ctf_node
, siblings
),
3799 &packet_header_decl
);
3801 _PERROR("%s", "cannot create packet header declaration");
3805 assert(packet_header_decl
);
3806 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
3807 packet_header_decl
);
3808 BT_PUT(packet_header_decl
);
3810 _PERROR("%s", "cannot set trace declaration's packet header declaration");
3814 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3816 _PWARNING("%s", "unknown attribute \"%s\" in trace declaration");
3824 _PERROR("%s", "unknown expression in trace declaration");
3833 BT_PUT(packet_header_decl
);
3839 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3843 struct ctf_node
*iter
;
3844 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3846 if (node
->visited
) {
3850 node
->visited
= TRUE
;
3852 if (ctx
->is_trace_visited
) {
3853 _PERROR("%s", "duplicate \"trace\" block");
3858 ret
= ctx_push_scope(ctx
);
3860 _PERROR("%s", "cannot push scope");
3864 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3865 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
3874 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
3876 "missing \"major\" attribute in trace declaration");
3881 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
3883 "missing \"minor\" attribute in trace declaration");
3888 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3889 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
3894 ctx
->is_trace_visited
= TRUE
;
3904 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
3908 struct ctf_node
*entry_node
;
3909 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
3911 if (node
->visited
) {
3915 node
->visited
= TRUE
;
3917 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
3918 struct bt_list_head
*right_head
=
3919 &entry_node
->u
.ctf_expression
.right
;
3921 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
3922 _PERROR("%s", "wrong expression in environment entry");
3927 left
= concatenate_unary_strings(
3928 &entry_node
->u
.ctf_expression
.left
);
3930 _PERROR("%s", "cannot get environment entry name");
3935 if (is_unary_string(right_head
)) {
3936 char *right
= concatenate_unary_strings(right_head
);
3939 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
3945 printf_verbose("env.%s = \"%s\"\n", left
, right
);
3946 ret
= bt_ctf_trace_set_environment_field_string(
3947 ctx
->trace
, left
, right
);
3951 _PERROR("environment: cannot add entry \"%s\" to trace",
3955 } else if (is_unary_unsigned(right_head
) ||
3956 is_unary_signed(right_head
)) {
3959 if (is_unary_unsigned(right_head
)) {
3960 ret
= get_unary_unsigned(right_head
,
3963 ret
= get_unary_signed(right_head
, &v
);
3966 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
3972 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
3973 ret
= bt_ctf_trace_set_environment_field_integer(
3974 ctx
->trace
, left
, v
);
3976 _PERROR("environment: cannot add entry \"%s\" to trace",
3981 printf_verbose("%s: environment entry \"%s\" has unknown type\n",
3999 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4004 struct ctf_node
*node
;
4005 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4007 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4008 if (node
->type
== NODE_CTF_EXPRESSION
) {
4009 struct ctf_node
*right_node
;
4011 left
= concatenate_unary_strings(
4012 &node
->u
.ctf_expression
.left
);
4018 if (!strcmp(left
, "byte_order")) {
4019 enum bt_ctf_byte_order bo
;
4021 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4022 _PERROR_DUP_ATTR("byte_order",
4023 "trace declaration");
4028 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4029 right_node
= _BT_LIST_FIRST_ENTRY(
4030 &node
->u
.ctf_expression
.right
,
4031 struct ctf_node
, siblings
);
4032 bo
= byte_order_from_unary_expr(ctx
->efd
,
4034 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4035 _PERROR("%s", "unknown \"byte_order\" attribute in trace declaration");
4038 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4039 _PERROR("%s", "\"byte_order\" attribute cannot be set to \"native\" in trace declaration");
4044 ret
= bt_ctf_trace_set_byte_order(
4047 _PERROR("cannot set trace's byte order (%d)",
4058 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4059 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4073 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4074 struct bt_ctf_clock
*clock
, int *set
)
4079 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4084 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4090 if (!strcmp(left
, "name")) {
4093 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4094 _PERROR_DUP_ATTR("name", "clock declaration");
4099 right
= concatenate_unary_strings(
4100 &entry_node
->u
.ctf_expression
.right
);
4102 _PERROR("%s", "unexpected unary expression for clock declaration's \"name\" attribute");
4107 ret
= bt_ctf_clock_set_name(clock
, right
);
4109 _PERROR("%s", "cannot set clock's name");
4115 _SET(set
, _CLOCK_NAME_SET
);
4116 } else if (!strcmp(left
, "uuid")) {
4117 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4119 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4120 _PERROR_DUP_ATTR("uuid", "clock declaration");
4125 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4127 _PERROR("%s", "invalid clock UUID");
4131 ret
= bt_ctf_clock_set_uuid(clock
, uuid
);
4133 _PERROR("%s", "cannot set clock's UUID");
4137 _SET(set
, _CLOCK_UUID_SET
);
4138 } else if (!strcmp(left
, "description")) {
4141 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4142 _PERROR_DUP_ATTR("description", "clock declaration");
4147 right
= concatenate_unary_strings(
4148 &entry_node
->u
.ctf_expression
.right
);
4150 _PERROR("%s", "unexpected unary expression for clock's \"description\" attribute");
4155 ret
= bt_ctf_clock_set_description(clock
, right
);
4157 _PERROR("%s", "cannot set clock's description");
4163 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4164 } else if (!strcmp(left
, "freq")) {
4167 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4168 _PERROR_DUP_ATTR("freq", "clock declaration");
4173 ret
= get_unary_unsigned(
4174 &entry_node
->u
.ctf_expression
.right
, &freq
);
4176 _PERROR("%s", "unexpected unary expression for clock declaration's \"freq\" attribute");
4181 ret
= bt_ctf_clock_set_frequency(clock
, freq
);
4183 _PERROR("%s", "cannot set clock's frequency");
4187 _SET(set
, _CLOCK_FREQ_SET
);
4188 } else if (!strcmp(left
, "precision")) {
4191 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4192 _PERROR_DUP_ATTR("precision", "clock declaration");
4197 ret
= get_unary_unsigned(
4198 &entry_node
->u
.ctf_expression
.right
, &precision
);
4200 _PERROR("%s", "unexpected unary expression for clock declaration's \"precision\" attribute");
4205 ret
= bt_ctf_clock_set_precision(clock
, precision
);
4207 _PERROR("%s", "cannot set clock's precision");
4211 _SET(set
, _CLOCK_PRECISION_SET
);
4212 } else if (!strcmp(left
, "offset_s")) {
4215 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4216 _PERROR_DUP_ATTR("offset_s", "clock declaration");
4221 ret
= get_unary_unsigned(
4222 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4224 _PERROR("%s", "unexpected unary expression for clock declaration's \"offset_s\" attribute");
4229 ret
= bt_ctf_clock_set_offset_s(clock
, offset_s
);
4231 _PERROR("%s", "cannot set clock's offset in seconds");
4235 _SET(set
, _CLOCK_OFFSET_S_SET
);
4236 } else if (!strcmp(left
, "offset")) {
4239 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4240 _PERROR_DUP_ATTR("offset", "clock declaration");
4245 ret
= get_unary_unsigned(
4246 &entry_node
->u
.ctf_expression
.right
, &offset
);
4248 _PERROR("%s", "unexpected unary expression for clock declaration's \"offset\" attribute");
4253 ret
= bt_ctf_clock_set_offset(clock
, offset
);
4255 _PERROR("%s", "cannot set clock's offset in cycles");
4259 _SET(set
, _CLOCK_OFFSET_SET
);
4260 } else if (!strcmp(left
, "absolute")) {
4261 struct ctf_node
*right
;
4263 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4264 _PERROR_DUP_ATTR("absolute", "clock declaration");
4269 right
= _BT_LIST_FIRST_ENTRY(
4270 &entry_node
->u
.ctf_expression
.right
,
4271 struct ctf_node
, siblings
);
4272 ret
= get_boolean(ctx
->efd
, right
);
4274 _PERROR("%s", "unexpected unary expression for clock declaration's \"absolute\" attribute");
4279 ret
= bt_ctf_clock_set_is_absolute(clock
, ret
);
4281 _PERROR("%s", "cannot set clock's absolute option");
4285 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4287 _PWARNING("unknown attribute \"%s\" in clock declaration",
4303 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4307 struct bt_ctf_clock
*clock
;
4308 struct ctf_node
*entry_node
;
4309 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4311 if (clock_node
->visited
) {
4315 clock_node
->visited
= TRUE
;
4316 clock
= bt_ctf_clock_create(NULL
);
4318 _PERROR("%s", "cannot create clock");
4323 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4324 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4330 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4332 "missing \"name\" attribute in clock declaration");
4337 if (bt_ctf_trace_get_clock_count(ctx
->trace
) != 0) {
4338 _PERROR("%s", "only CTF traces with a single clock declaration are supported as of this version");
4343 ret
= bt_ctf_trace_add_clock(ctx
->trace
, clock
);
4345 _PERROR("%s", "cannot add clock to trace");
4356 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4360 if (root_decl_node
->visited
) {
4364 root_decl_node
->visited
= TRUE
;
4366 switch (root_decl_node
->type
) {
4368 ret
= visit_typedef(ctx
,
4369 root_decl_node
->u
._typedef
.type_specifier_list
,
4370 &root_decl_node
->u
._typedef
.type_declarators
);
4372 _PERROR("%s", "cannot add typedef in root scope");
4376 case NODE_TYPEALIAS
:
4377 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4378 root_decl_node
->u
.typealias
.alias
);
4380 _PERROR("%s", "cannot add typealias in root scope");
4384 case NODE_TYPE_SPECIFIER_LIST
:
4386 _BT_CTF_FIELD_TYPE_INIT(decl
);
4389 * Just add the type specifier to the root
4390 * declaration scope. Put local reference.
4392 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4411 int add_stream_classes_to_trace(struct ctx
*ctx
)
4414 GHashTableIter iter
;
4415 gpointer key
, stream_class
;
4417 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
4419 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
4420 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
4423 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
4424 _PERROR("cannot add stream class %" PRId64
" to trace",
4434 int ctf_visitor_generate_ir(FILE *efd
, struct ctf_node
*node
,
4435 struct bt_ctf_trace
**trace
)
4438 struct ctx
*ctx
= NULL
;
4440 printf_verbose("CTF visitor: AST -> CTF IR...\n");
4442 *trace
= bt_ctf_trace_create();
4444 _FPERROR(efd
, "%s", "cannot create trace");
4449 /* Set packet header to NULL to override the default one */
4450 ret
= bt_ctf_trace_set_packet_header_type(*trace
, NULL
);
4454 "cannot set initial, empty packet header structure");
4458 ctx
= ctx_create(*trace
, efd
);
4460 _FPERROR(efd
, "%s", "cannot create visitor context");
4465 switch (node
->type
) {
4468 struct ctf_node
*iter
;
4469 int got_trace_decl
= FALSE
;
4470 int found_callsite
= FALSE
;
4473 * Find trace declaration's byte order first (for early
4476 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4477 if (got_trace_decl
) {
4478 _PERROR("%s", "duplicate trace declaration");
4482 ret
= set_trace_byte_order(ctx
, iter
);
4484 _PERROR("cannot set trace's byte order (%d)",
4489 got_trace_decl
= TRUE
;
4492 if (!got_trace_decl
) {
4493 _PERROR("no trace declaration found (%d)", ret
);
4499 * Visit clocks first since any early integer can be mapped
4502 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
4503 ret
= visit_clock_decl(ctx
, iter
);
4505 _PERROR("error while visiting clock declaration (%d)",
4512 * Visit root declarations next, as they can be used by any
4515 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
4517 ret
= visit_root_decl(ctx
, iter
);
4519 _PERROR("error while visiting root declaration (%d)",
4525 /* Callsite are not supported */
4526 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
4527 found_callsite
= TRUE
;
4531 if (found_callsite
) {
4532 _PWARNING("%s", "\"callsite\" blocks are not supported as of this version");
4536 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
4537 ret
= visit_env(ctx
, iter
);
4539 _PERROR("error while visiting environment block (%d)",
4546 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4547 ret
= visit_trace_decl(ctx
, iter
);
4549 _PERROR("%s", "error while visiting trace declaration");
4555 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
4556 ret
= visit_stream_decl(ctx
, iter
);
4558 _PERROR("%s", "error while visiting stream declaration");
4564 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
4565 ret
= visit_event_decl(ctx
, iter
);
4567 _PERROR("%s", "error while visiting event declaration");
4575 _PERROR("unknown node type: %d", (int) node
->type
);
4580 /* Add stream classes to trace now */
4581 ret
= add_stream_classes_to_trace(ctx
);
4583 _PERROR("%s", "cannot add stream classes to trace");
4587 printf_verbose("done!\n");