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
40 #include <babeltrace/compat/uuid-internal.h>
41 #include <babeltrace/endian-internal.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/ctf-ir/trace.h>
44 #include <babeltrace/ctf-ir/stream-class.h>
45 #include <babeltrace/ctf-ir/event.h>
46 #include <babeltrace/ctf-ir/event-class.h>
47 #include <babeltrace/ctf-ir/field-types.h>
48 #include <babeltrace/ctf-ir/field-types-internal.h>
49 #include <babeltrace/ctf-ir/clock-class.h>
55 /* Bit value (left shift) */
56 #define _BV(_val) (1 << (_val))
58 /* Bit is set in a set of bits */
59 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
61 /* Set bit in a set of bits */
62 #define _SET(_set, _mask) (*(_set) |= (_mask))
64 /* Bits for verifying existing attributes in various declarations */
66 _CLOCK_NAME_SET
= _BV(0),
67 _CLOCK_UUID_SET
= _BV(1),
68 _CLOCK_FREQ_SET
= _BV(2),
69 _CLOCK_PRECISION_SET
= _BV(3),
70 _CLOCK_OFFSET_S_SET
= _BV(4),
71 _CLOCK_OFFSET_SET
= _BV(5),
72 _CLOCK_ABSOLUTE_SET
= _BV(6),
73 _CLOCK_DESCRIPTION_SET
= _BV(7),
77 _INTEGER_ALIGN_SET
= _BV(0),
78 _INTEGER_SIZE_SET
= _BV(1),
79 _INTEGER_BASE_SET
= _BV(2),
80 _INTEGER_ENCODING_SET
= _BV(3),
81 _INTEGER_BYTE_ORDER_SET
= _BV(4),
82 _INTEGER_SIGNED_SET
= _BV(5),
83 _INTEGER_MAP_SET
= _BV(6),
87 _FLOAT_ALIGN_SET
= _BV(0),
88 _FLOAT_MANT_DIG_SET
= _BV(1),
89 _FLOAT_EXP_DIG_SET
= _BV(2),
90 _FLOAT_BYTE_ORDER_SET
= _BV(3),
94 _STRING_ENCODING_SET
= _BV(0),
98 _TRACE_MINOR_SET
= _BV(0),
99 _TRACE_MAJOR_SET
= _BV(1),
100 _TRACE_BYTE_ORDER_SET
= _BV(2),
101 _TRACE_UUID_SET
= _BV(3),
102 _TRACE_PACKET_HEADER_SET
= _BV(4),
106 _STREAM_ID_SET
= _BV(0),
107 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
108 _STREAM_EVENT_HEADER_SET
= _BV(2),
109 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
113 _EVENT_NAME_SET
= _BV(0),
114 _EVENT_ID_SET
= _BV(1),
115 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
116 _EVENT_STREAM_ID_SET
= _BV(3),
117 _EVENT_LOGLEVEL_SET
= _BV(4),
118 _EVENT_CONTEXT_SET
= _BV(5),
119 _EVENT_FIELDS_SET
= _BV(6),
127 LOGLEVEL_WARNING
= 4,
130 LOGLEVEL_DEBUG_SYSTEM
= 7,
131 LOGLEVEL_DEBUG_PROGRAM
= 8,
132 LOGLEVEL_DEBUG_PROCESS
= 9,
133 LOGLEVEL_DEBUG_MODULE
= 10,
134 LOGLEVEL_DEBUG_UNIT
= 11,
135 LOGLEVEL_DEBUG_FUNCTION
= 12,
136 LOGLEVEL_DEBUG_LINE
= 13,
141 /* Prefixes of type aliases */
142 #define _PREFIX_ALIAS 'a'
143 #define _PREFIX_ENUM 'e'
144 #define _PREFIX_STRUCT 's'
145 #define _PREFIX_VARIANT 'v'
147 /* First entry in a BT list */
148 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
149 bt_list_entry((_ptr)->next, _type, _member)
151 #define _BT_CTF_FIELD_TYPE_INIT(_name) struct bt_ctf_field_type *_name = NULL;
153 /* Error printing wrappers */
154 #define _PERROR(_fmt, ...) \
156 if (!ctx->efd) break; \
157 fprintf(ctx->efd, "[error] %s: " _fmt "\n", \
158 __func__, __VA_ARGS__); \
161 #define _PWARNING(_fmt, ...) \
163 if (!ctx->efd) break; \
164 fprintf(ctx->efd, "[warning] %s: " _fmt "\n", \
165 __func__, __VA_ARGS__); \
168 #define _FPERROR(_stream, _fmt, ...) \
170 if (!_stream) break; \
171 fprintf(_stream, "[error] %s: " _fmt "\n", \
172 __func__, __VA_ARGS__); \
175 #define _FPWARNING(_stream, _fmt, ...) \
177 if (!_stream) break; \
178 fprintf(_stream, "[warning] %s: " _fmt "\n", \
179 __func__, __VA_ARGS__); \
182 #define _PERROR_DUP_ATTR(_attr, _entity) \
184 if (!ctx->efd) break; \
186 "[error] %s: duplicate attribute \"" \
187 _attr "\" in " _entity "\n", __func__); \
191 * Declaration scope of a visitor context. This represents a TSDL
192 * lexical scope, so that aliases and named structures, variants,
193 * and enumerations may be registered and looked up hierarchically.
195 struct ctx_decl_scope
{
197 * Alias name to field type.
199 * GQuark -> struct bt_ctf_field_type *
201 GHashTable
*decl_map
;
203 /* Parent scope; NULL if this is the root declaration scope */
204 struct ctx_decl_scope
*parent_scope
;
208 * Visitor context (private).
211 /* Trace being filled (owned by this) */
212 struct bt_ctf_trace
*trace
;
214 /* Error stream to use during visit */
217 /* Current declaration scope (top of the stack) */
218 struct ctx_decl_scope
*current_scope
;
220 /* 1 if trace declaration is visited */
221 int is_trace_visited
;
223 /* 1 if this is an LTTng trace */
226 /* Offset (ns) to apply to clock classes on creation */
227 uint64_t clock_class_offset_ns
;
229 /* Trace attributes */
230 enum bt_ctf_byte_order trace_bo
;
231 uint64_t trace_major
;
232 uint64_t trace_minor
;
233 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
236 * Stream IDs to stream classes.
238 * int64_t -> struct bt_ctf_stream_class *
240 GHashTable
*stream_classes
;
246 struct ctf_visitor_generate_ir
{ };
249 const char *loglevel_str
[] = {
250 [ LOGLEVEL_EMERG
] = "TRACE_EMERG",
251 [ LOGLEVEL_ALERT
] = "TRACE_ALERT",
252 [ LOGLEVEL_CRIT
] = "TRACE_CRIT",
253 [ LOGLEVEL_ERR
] = "TRACE_ERR",
254 [ LOGLEVEL_WARNING
] = "TRACE_WARNING",
255 [ LOGLEVEL_NOTICE
] = "TRACE_NOTICE",
256 [ LOGLEVEL_INFO
] = "TRACE_INFO",
257 [ LOGLEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
258 [ LOGLEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
259 [ LOGLEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
260 [ LOGLEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
261 [ LOGLEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
262 [ LOGLEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
263 [ LOGLEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
264 [ LOGLEVEL_DEBUG
] = "TRACE_DEBUG",
268 const char *print_loglevel(int64_t value
)
273 if (value
>= _NR_LOGLEVELS
) {
274 return "<<UNKNOWN>>";
276 return loglevel_str
[value
];
280 * Creates a new declaration scope.
282 * @param par_scope Parent scope (NULL if creating a root scope)
283 * @returns New declaration scope, or NULL on error
286 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
288 struct ctx_decl_scope
*scope
;
290 scope
= g_new(struct ctx_decl_scope
, 1);
295 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
296 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
297 scope
->parent_scope
= par_scope
;
304 * Destroys a declaration scope.
306 * This function does not destroy the parent scope.
308 * @param scope Scope to destroy
311 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
317 g_hash_table_destroy(scope
->decl_map
);
325 * Returns the GQuark of a prefixed alias.
327 * @param prefix Prefix character
329 * @returns Associated GQuark, or 0 on error
332 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
338 /* Prefix character + original string + '\0' */
339 char *prname
= g_new(char, strlen(name
) + 2);
344 sprintf(prname
, "%c%s", prefix
, name
);
345 qname
= g_quark_from_string(prname
);
353 * Looks up a prefixed type alias within a declaration scope.
355 * @param scope Declaration scope
356 * @param prefix Prefix character
357 * @param name Alias name
358 * @param level Number of levels to dig (-1 means infinite)
359 * @returns Declaration, or NULL if not found
362 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
363 struct ctx_decl_scope
*scope
, char prefix
,
364 const char *name
, int levels
)
368 _BT_CTF_FIELD_TYPE_INIT(decl
);
369 struct ctx_decl_scope
*cur_scope
= scope
;
373 qname
= get_prefixed_named_quark(prefix
, name
);
382 while (cur_scope
&& cur_levels
< levels
) {
383 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
384 (gconstpointer
) (unsigned long) qname
);
386 /* Caller's reference */
391 cur_scope
= cur_scope
->parent_scope
;
402 * Looks up a type alias within a declaration scope.
404 * @param scope Declaration scope
405 * @param name Alias name
406 * @param level Number of levels to dig (-1 means infinite)
407 * @returns Declaration, or NULL if not found
410 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
411 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
413 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
418 * Looks up an enumeration within a declaration scope.
420 * @param scope Declaration scope
421 * @param name Enumeration name
422 * @param level Number of levels to dig (-1 means infinite)
423 * @returns Declaration, or NULL if not found
426 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
427 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
429 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
434 * Looks up a structure within a declaration scope.
436 * @param scope Declaration scope
437 * @param name Structure name
438 * @param level Number of levels to dig (-1 means infinite)
439 * @returns Declaration, or NULL if not found
442 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
443 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
445 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
450 * Looks up a variant within a declaration scope.
452 * @param scope Declaration scope
453 * @param name Variant name
454 * @param level Number of levels to dig (-1 means infinite)
455 * @returns Declaration, or NULL if not found
458 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
459 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
461 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
466 * Registers a prefixed type alias within a declaration scope.
468 * @param scope Declaration scope
469 * @param prefix Prefix character
470 * @param name Alias name (non-NULL)
471 * @param decl Declaration to register
472 * @returns 0 if registration went okay, negative value otherwise
475 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
476 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
480 _BT_CTF_FIELD_TYPE_INIT(edecl
);
485 qname
= get_prefixed_named_quark(prefix
, name
);
491 /* Make sure alias does not exist in local scope */
492 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
499 g_hash_table_insert(scope
->decl_map
,
500 (gpointer
) (unsigned long) qname
, decl
);
502 /* Hash table's reference */
512 * Registers a type alias within a declaration scope.
514 * @param scope Declaration scope
515 * @param name Alias name (non-NULL)
516 * @param decl Declaration to register
517 * @returns 0 if registration went okay, negative value otherwise
520 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
521 const char *name
, struct bt_ctf_field_type
*decl
)
523 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
528 * Registers an enumeration declaration within a declaration scope.
530 * @param scope Declaration scope
531 * @param name Enumeration name (non-NULL)
532 * @param decl Enumeration declaration to register
533 * @returns 0 if registration went okay, negative value otherwise
536 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
537 const char *name
, struct bt_ctf_field_type
*decl
)
539 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
544 * Registers a structure declaration within a declaration scope.
546 * @param scope Declaration scope
547 * @param name Structure name (non-NULL)
548 * @param decl Structure declaration to register
549 * @returns 0 if registration went okay, negative value otherwise
552 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
553 const char *name
, struct bt_ctf_field_type
*decl
)
555 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
560 * Registers a variant declaration within a declaration scope.
562 * @param scope Declaration scope
563 * @param name Variant name (non-NULL)
564 * @param decl Variant declaration to register
565 * @returns 0 if registration went okay, negative value otherwise
568 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
569 const char *name
, struct bt_ctf_field_type
*decl
)
571 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
576 * Creates a new visitor context.
578 * @param trace Associated trace
579 * @param efd Error stream
580 * @returns New visitor context, or NULL on error
583 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
, FILE *efd
,
584 uint64_t clock_class_offset_ns
)
586 struct ctx
*ctx
= NULL
;
587 struct ctx_decl_scope
*scope
= NULL
;
589 ctx
= g_new0(struct ctx
, 1);
594 /* Root declaration scope */
595 scope
= ctx_decl_scope_create(NULL
);
600 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
601 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
602 if (!ctx
->stream_classes
) {
608 ctx
->current_scope
= scope
;
609 ctx
->trace_bo
= BT_CTF_BYTE_ORDER_NATIVE
;
610 ctx
->clock_class_offset_ns
= clock_class_offset_ns
;
615 ctx_decl_scope_destroy(scope
);
620 * Destroys a visitor context.
622 * @param ctx Visitor context to destroy
625 void ctx_destroy(struct ctx
*ctx
)
627 struct ctx_decl_scope
*scope
;
629 * Destroy all scopes, from current one to the root scope.
636 scope
= ctx
->current_scope
;
639 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
641 ctx_decl_scope_destroy(scope
);
642 scope
= parent_scope
;
646 g_hash_table_destroy(ctx
->stream_classes
);
654 * Pushes a new declaration scope on top of a visitor context's
655 * declaration scope stack.
657 * @param ctx Visitor context
658 * @returns 0 on success, or a negative value on error
661 int ctx_push_scope(struct ctx
*ctx
)
664 struct ctx_decl_scope
*new_scope
;
667 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
673 ctx
->current_scope
= new_scope
;
680 void ctx_pop_scope(struct ctx
*ctx
)
682 struct ctx_decl_scope
*parent_scope
= NULL
;
686 if (!ctx
->current_scope
) {
690 parent_scope
= ctx
->current_scope
->parent_scope
;
691 ctx_decl_scope_destroy(ctx
->current_scope
);
692 ctx
->current_scope
= parent_scope
;
699 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
700 struct bt_ctf_field_type
**decl
);
703 int is_unary_string(struct bt_list_head
*head
)
706 struct ctf_node
*node
;
708 bt_list_for_each_entry(node
, head
, siblings
) {
709 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
713 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
722 char *concatenate_unary_strings(struct bt_list_head
*head
)
726 struct ctf_node
*node
;
728 str
= g_string_new(NULL
);
730 bt_list_for_each_entry(node
, head
, siblings
) {
734 node
->type
!= NODE_UNARY_EXPRESSION
||
735 node
->u
.unary_expression
.type
!= UNARY_STRING
||
738 node
->u
.unary_expression
.link
!=
746 switch (node
->u
.unary_expression
.link
) {
748 g_string_append(str
, ".");
750 case UNARY_ARROWLINK
:
751 g_string_append(str
, "->");
753 case UNARY_DOTDOTDOT
:
754 g_string_append(str
, "...");
760 src_string
= node
->u
.unary_expression
.u
.string
;
761 g_string_append(str
, src_string
);
765 /* Destroys the container, returns the underlying string */
766 return g_string_free(str
, FALSE
);
769 /* This always returns NULL */
770 return g_string_free(str
, TRUE
);
774 const char *get_map_clock_name_value(struct bt_list_head
*head
)
777 struct ctf_node
*node
;
778 const char *name
= NULL
;
780 bt_list_for_each_entry(node
, head
, siblings
) {
782 int uexpr_type
= node
->u
.unary_expression
.type
;
783 int uexpr_link
= node
->u
.unary_expression
.link
;
784 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
785 uexpr_type
!= UNARY_STRING
||
786 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
791 /* Needs to be chained with . */
792 switch (node
->u
.unary_expression
.link
) {
795 case UNARY_ARROWLINK
:
796 case UNARY_DOTDOTDOT
:
802 src_string
= node
->u
.unary_expression
.u
.string
;
806 if (strcmp("clock", src_string
)) {
814 if (strcmp("value", src_string
)) {
819 /* Extra identifier, unknown */
833 int is_unary_unsigned(struct bt_list_head
*head
)
836 struct ctf_node
*node
;
838 bt_list_for_each_entry(node
, head
, siblings
) {
839 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
843 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
852 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
856 struct ctf_node
*node
;
858 if (bt_list_empty(head
)) {
863 bt_list_for_each_entry(node
, head
, siblings
) {
864 int uexpr_type
= node
->u
.unary_expression
.type
;
865 int uexpr_link
= node
->u
.unary_expression
.link
;
866 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
867 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
868 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
874 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
883 int is_unary_signed(struct bt_list_head
*head
)
886 struct ctf_node
*node
;
888 bt_list_for_each_entry(node
, head
, siblings
) {
889 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
893 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
902 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
906 struct ctf_node
*node
;
908 bt_list_for_each_entry(node
, head
, siblings
) {
909 int uexpr_type
= node
->u
.unary_expression
.type
;
910 int uexpr_link
= node
->u
.unary_expression
.link
;
911 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
912 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
913 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
914 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
915 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
921 switch (node
->u
.unary_expression
.type
) {
922 case UNARY_UNSIGNED_CONSTANT
:
924 node
->u
.unary_expression
.u
.unsigned_constant
;
926 case UNARY_SIGNED_CONSTANT
:
927 *value
= node
->u
.unary_expression
.u
.signed_constant
;
942 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
946 struct ctf_node
*node
;
948 bt_list_for_each_entry(node
, head
, siblings
) {
949 int uexpr_type
= node
->u
.unary_expression
.type
;
950 int uexpr_link
= node
->u
.unary_expression
.link
;
951 const char *src_string
;
953 if (node
->type
!= NODE_UNARY_EXPRESSION
||
954 uexpr_type
!= UNARY_STRING
||
955 uexpr_link
!= UNARY_LINK_UNKNOWN
||
961 src_string
= node
->u
.unary_expression
.u
.string
;
962 ret
= bt_uuid_parse(src_string
, uuid
);
973 int get_boolean(FILE *efd
, struct ctf_node
*unary_expr
)
977 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
978 _FPERROR(efd
, "%s", "expecting unary expression");
983 switch (unary_expr
->u
.unary_expression
.type
) {
984 case UNARY_UNSIGNED_CONSTANT
:
985 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
987 case UNARY_SIGNED_CONSTANT
:
988 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
992 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
994 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
996 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
999 _FPERROR(efd
, "unexpected string \"%s\"", str
);
1006 _FPERROR(efd
, "%s", "unexpected unary expression type");
1016 enum bt_ctf_byte_order
byte_order_from_unary_expr(FILE *efd
,
1017 struct ctf_node
*unary_expr
)
1020 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1022 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1024 "\"byte_order\" attribute: expecting string");
1028 str
= unary_expr
->u
.unary_expression
.u
.string
;
1030 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1031 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1032 } else if (!strcmp(str
, "le")) {
1033 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1034 } else if (!strcmp(str
, "native")) {
1035 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1037 _FPERROR(efd
, "unexpected \"byte_order\" attribute value \"%s\"; should be \"be\", \"le\", \"network\", or \"native\"",
1047 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1048 struct ctf_node
*uexpr
)
1050 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(ctx
->efd
, uexpr
);
1052 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1053 bo
= bt_ctf_trace_get_native_byte_order(ctx
->trace
);
1060 int is_align_valid(uint64_t align
)
1062 return (align
!= 0) && !(align
& (align
- 1));
1066 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1071 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1076 switch (type_specifier
->u
.type_specifier
.type
) {
1078 g_string_append(str
, "void");
1081 g_string_append(str
, "char");
1083 case TYPESPEC_SHORT
:
1084 g_string_append(str
, "short");
1087 g_string_append(str
, "int");
1090 g_string_append(str
, "long");
1092 case TYPESPEC_FLOAT
:
1093 g_string_append(str
, "float");
1095 case TYPESPEC_DOUBLE
:
1096 g_string_append(str
, "double");
1098 case TYPESPEC_SIGNED
:
1099 g_string_append(str
, "signed");
1101 case TYPESPEC_UNSIGNED
:
1102 g_string_append(str
, "unsigned");
1105 g_string_append(str
, "bool");
1107 case TYPESPEC_COMPLEX
:
1108 g_string_append(str
, "_Complex");
1110 case TYPESPEC_IMAGINARY
:
1111 g_string_append(str
, "_Imaginary");
1113 case TYPESPEC_CONST
:
1114 g_string_append(str
, "const");
1116 case TYPESPEC_ID_TYPE
:
1117 if (type_specifier
->u
.type_specifier
.id_type
) {
1118 g_string_append(str
,
1119 type_specifier
->u
.type_specifier
.id_type
);
1122 case TYPESPEC_STRUCT
:
1124 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1126 if (!node
->u
._struct
.name
) {
1127 _PERROR("%s", "unexpected empty structure name");
1132 g_string_append(str
, "struct ");
1133 g_string_append(str
, node
->u
._struct
.name
);
1136 case TYPESPEC_VARIANT
:
1138 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1140 if (!node
->u
.variant
.name
) {
1141 _PERROR("%s", "unexpected empty variant name");
1146 g_string_append(str
, "variant ");
1147 g_string_append(str
, node
->u
.variant
.name
);
1152 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1154 if (!node
->u
._enum
.enum_id
) {
1155 _PERROR("%s", "unexpected empty enum name");
1160 g_string_append(str
, "enum ");
1161 g_string_append(str
, node
->u
._enum
.enum_id
);
1164 case TYPESPEC_FLOATING_POINT
:
1165 case TYPESPEC_INTEGER
:
1166 case TYPESPEC_STRING
:
1168 _PERROR("%s", "unknown specifier");
1178 int get_type_specifier_list_name(struct ctx
*ctx
,
1179 struct ctf_node
*type_specifier_list
, GString
*str
)
1182 struct ctf_node
*iter
;
1183 int alias_item_nr
= 0;
1184 struct bt_list_head
*head
=
1185 &type_specifier_list
->u
.type_specifier_list
.head
;
1187 bt_list_for_each_entry(iter
, head
, siblings
) {
1188 if (alias_item_nr
!= 0) {
1189 g_string_append(str
, " ");
1193 ret
= get_type_specifier_name(ctx
, iter
, str
);
1204 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1205 struct ctf_node
*type_specifier_list
,
1206 struct ctf_node
*node_type_declarator
)
1212 struct ctf_node
*iter
;
1213 struct bt_list_head
*pointers
=
1214 &node_type_declarator
->u
.type_declarator
.pointers
;
1216 str
= g_string_new("");
1217 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1219 g_string_free(str
, TRUE
);
1223 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1224 g_string_append(str
, " *");
1226 if (iter
->u
.pointer
.const_qualifier
) {
1227 g_string_append(str
, " const");
1231 str_c
= g_string_free(str
, FALSE
);
1232 qalias
= g_quark_from_string(str_c
);
1240 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1241 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1242 struct bt_ctf_field_type
**field_decl
,
1243 struct bt_ctf_field_type
*nested_decl
)
1246 * During this whole function, nested_decl is always OURS,
1247 * whereas field_decl is an output which we create, but
1248 * belongs to the caller (it is moved).
1254 /* Validate type declarator node */
1255 if (node_type_declarator
) {
1256 if (node_type_declarator
->u
.type_declarator
.type
==
1262 /* TODO: GCC bitfields not supported yet */
1263 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1265 _PERROR("%s", "GCC bitfields are not supported as of this version");
1271 /* Find the right nested declaration if not provided */
1273 struct bt_list_head
*pointers
=
1274 &node_type_declarator
->u
.type_declarator
.pointers
;
1276 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1278 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1281 * If we have a pointer declarator, it HAS to
1282 * be present in the typealiases (else fail).
1284 qalias
= create_typealias_identifier(ctx
,
1285 type_specifier_list
, node_type_declarator
);
1287 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1288 g_quark_to_string(qalias
), -1);
1290 _PERROR("cannot find typealias \"%s\"",
1291 g_quark_to_string(qalias
));
1296 /* Make a copy of it */
1297 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1298 BT_PUT(nested_decl
);
1299 if (!nested_decl_copy
) {
1300 _PERROR("%s", "cannot copy nested field type");
1305 BT_MOVE(nested_decl
, nested_decl_copy
);
1307 /* Force integer's base to 16 since it's a pointer */
1308 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1309 bt_ctf_field_type_integer_set_base(nested_decl
,
1310 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1313 ret
= visit_type_specifier_list(ctx
,
1314 type_specifier_list
, &nested_decl
);
1316 assert(!nested_decl
);
1322 assert(nested_decl
);
1324 if (!node_type_declarator
) {
1325 BT_MOVE(*field_decl
, nested_decl
);
1329 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1330 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1332 node_type_declarator
->u
.type_declarator
.u
.id
;
1334 *field_name
= g_quark_from_string(id
);
1339 BT_MOVE(*field_decl
, nested_decl
);
1342 struct ctf_node
*first
;
1343 _BT_CTF_FIELD_TYPE_INIT(decl
);
1344 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1345 struct bt_list_head
*length
=
1346 &node_type_declarator
->
1347 u
.type_declarator
.u
.nested
.length
;
1349 /* Create array/sequence, pass nested_decl as child */
1350 if (bt_list_empty(length
)) {
1352 "expecting length field reference or value");
1357 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1358 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1363 switch (first
->u
.unary_expression
.type
) {
1364 case UNARY_UNSIGNED_CONSTANT
:
1367 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1369 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1370 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1372 BT_PUT(nested_decl
);
1375 "cannot create array field type");
1380 BT_MOVE(decl
, array_decl
);
1385 /* Lookup unsigned integer definition, create seq. */
1386 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1387 char *length_name
= concatenate_unary_strings(length
);
1394 seq_decl
= bt_ctf_field_type_sequence_create(
1395 nested_decl
, length_name
);
1396 g_free(length_name
);
1397 BT_PUT(nested_decl
);
1400 "cannot create sequence field type");
1405 BT_MOVE(decl
, seq_decl
);
1413 assert(!nested_decl
);
1415 assert(!*field_decl
);
1418 * At this point, we found the next nested declaration.
1419 * We currently own this (and lost the ownership of
1420 * nested_decl in the meantime). Pass this next
1421 * nested declaration as the content of the outer
1422 * container, MOVING its ownership.
1424 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1426 node_type_declarator
->
1427 u
.type_declarator
.u
.nested
.type_declarator
,
1428 &outer_field_decl
, decl
);
1431 assert(!outer_field_decl
);
1436 assert(outer_field_decl
);
1437 BT_MOVE(*field_decl
, outer_field_decl
);
1441 BT_PUT(nested_decl
);
1442 assert(*field_decl
);
1447 BT_PUT(nested_decl
);
1448 BT_PUT(*field_decl
);
1454 int visit_struct_decl_field(struct ctx
*ctx
,
1455 struct bt_ctf_field_type
*struct_decl
,
1456 struct ctf_node
*type_specifier_list
,
1457 struct bt_list_head
*type_declarators
)
1460 struct ctf_node
*iter
;
1461 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1463 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1466 const char *field_name
;
1467 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1469 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1470 &qfield_name
, iter
, &field_decl
, NULL
);
1472 assert(!field_decl
);
1473 _PERROR("%s", "cannot visit type declarator");
1478 field_name
= g_quark_to_string(qfield_name
);
1480 /* Check if field with same name already exists */
1482 bt_ctf_field_type_structure_get_field_type_by_name(
1483 struct_decl
, field_name
);
1485 BT_PUT(efield_decl
);
1486 _PERROR("duplicate field \"%s\" in structure",
1492 /* Add field to structure */
1493 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1494 field_decl
, field_name
);
1497 _PERROR("cannot add field \"%s\" to structure",
1498 g_quark_to_string(qfield_name
));
1512 int visit_variant_decl_field(struct ctx
*ctx
,
1513 struct bt_ctf_field_type
*variant_decl
,
1514 struct ctf_node
*type_specifier_list
,
1515 struct bt_list_head
*type_declarators
)
1518 struct ctf_node
*iter
;
1519 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1521 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1524 const char *field_name
;
1525 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1527 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1528 &qfield_name
, iter
, &field_decl
, NULL
);
1530 assert(!field_decl
);
1532 "cannot visit type declarator");
1537 field_name
= g_quark_to_string(qfield_name
);
1539 /* Check if field with same name already exists */
1541 bt_ctf_field_type_variant_get_field_type_by_name(
1542 variant_decl
, field_name
);
1544 BT_PUT(efield_decl
);
1545 _PERROR("duplicate field \"%s\" in variant",
1551 /* Add field to structure */
1552 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1553 field_decl
, field_name
);
1556 _PERROR("cannot add field \"%s\" to variant",
1557 g_quark_to_string(qfield_name
));
1571 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1572 struct bt_list_head
*type_declarators
)
1576 struct ctf_node
*iter
;
1577 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1579 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1580 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1581 &qidentifier
, iter
, &type_decl
, NULL
);
1583 _PERROR("%s", "cannot visit type declarator");
1588 /* Do not allow typedef and typealias of untagged variants */
1589 if (bt_ctf_field_type_is_variant(type_decl
)) {
1590 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1591 _PERROR("%s", "typedef of untagged variant is not allowed");
1597 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1598 g_quark_to_string(qidentifier
), type_decl
);
1600 _PERROR("cannot register typedef \"%s\"",
1601 g_quark_to_string(qidentifier
));
1613 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1614 struct ctf_node
*alias
)
1618 struct ctf_node
*node
;
1619 GQuark qdummy_field_name
;
1620 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1622 /* Create target type declaration */
1623 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1626 node
= _BT_LIST_FIRST_ENTRY(
1627 &target
->u
.typealias_target
.type_declarators
,
1628 struct ctf_node
, siblings
);
1631 ret
= visit_type_declarator(ctx
,
1632 target
->u
.typealias_target
.type_specifier_list
,
1633 &qdummy_field_name
, node
, &type_decl
, NULL
);
1636 _PERROR("%s", "cannot visit type declarator");
1640 /* Do not allow typedef and typealias of untagged variants */
1641 if (bt_ctf_field_type_is_variant(type_decl
)) {
1642 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1644 "typealias of untagged variant is not allowed");
1651 * The semantic validator does not check whether the target is
1652 * abstract or not (if it has an identifier). Check it here.
1654 if (qdummy_field_name
!= 0) {
1655 _PERROR("%s", "expecting empty identifier");
1660 /* Create alias identifier */
1661 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1662 struct ctf_node
, siblings
);
1663 qalias
= create_typealias_identifier(ctx
,
1664 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1665 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1666 g_quark_to_string(qalias
), type_decl
);
1668 _PERROR("cannot register typealias \"%s\"",
1669 g_quark_to_string(qalias
));
1680 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1681 struct bt_ctf_field_type
*struct_decl
)
1685 switch (entry_node
->type
) {
1687 ret
= visit_typedef(ctx
,
1688 entry_node
->u
._typedef
.type_specifier_list
,
1689 &entry_node
->u
._typedef
.type_declarators
);
1692 "cannot add typedef in \"struct\" declaration");
1696 case NODE_TYPEALIAS
:
1697 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1698 entry_node
->u
.typealias
.alias
);
1701 "cannot add typealias in \"struct\" declaration");
1705 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1707 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1708 entry_node
->u
.struct_or_variant_declaration
.
1709 type_specifier_list
,
1710 &entry_node
->u
.struct_or_variant_declaration
.
1717 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1727 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1728 struct bt_ctf_field_type
*variant_decl
)
1732 switch (entry_node
->type
) {
1734 ret
= visit_typedef(ctx
,
1735 entry_node
->u
._typedef
.type_specifier_list
,
1736 &entry_node
->u
._typedef
.type_declarators
);
1739 "cannot add typedef in \"variant\" declaration");
1743 case NODE_TYPEALIAS
:
1744 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1745 entry_node
->u
.typealias
.alias
);
1748 "cannot add typealias in \"variant\" declaration");
1752 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1754 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1755 entry_node
->u
.struct_or_variant_declaration
.
1756 type_specifier_list
,
1757 &entry_node
->u
.struct_or_variant_declaration
.
1764 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1774 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1775 struct bt_list_head
*decl_list
, int has_body
,
1776 struct bt_list_head
*min_align
,
1777 struct bt_ctf_field_type
**struct_decl
)
1781 *struct_decl
= NULL
;
1783 /* For named struct (without body), lookup in declaration scope */
1785 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1792 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1794 if (!*struct_decl
) {
1795 _PERROR("cannot find \"struct %s\"", name
);
1800 /* Make a copy of it */
1801 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1802 if (!struct_decl_copy
) {
1804 "cannot create copy of structure field type");
1809 BT_MOVE(*struct_decl
, struct_decl_copy
);
1811 struct ctf_node
*entry_node
;
1812 uint64_t min_align_value
= 0;
1815 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1817 estruct_decl
= ctx_decl_scope_lookup_struct(
1818 ctx
->current_scope
, name
, 1);
1820 BT_PUT(estruct_decl
);
1821 _PERROR("\"struct %s\" already declared in local scope",
1828 if (!bt_list_empty(min_align
)) {
1829 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1831 _PERROR("%s", "unexpected unary expression for structure declaration's \"align\" attribute");
1836 *struct_decl
= bt_ctf_field_type_structure_create();
1837 if (!*struct_decl
) {
1838 _PERROR("%s", "cannot create structure field type");
1843 if (min_align_value
!= 0) {
1844 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1847 _PERROR("%s", "failed to set structure's minimal alignment");
1852 ret
= ctx_push_scope(ctx
);
1854 _PERROR("%s", "cannot push scope");
1858 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1859 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1870 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1871 name
, *struct_decl
);
1873 _PERROR("cannot register \"struct %s\" in declaration scope",
1883 BT_PUT(*struct_decl
);
1889 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1890 const char *tag
, struct bt_list_head
*decl_list
,
1891 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1894 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1896 *variant_decl
= NULL
;
1898 /* For named variant (without body), lookup in declaration scope */
1900 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1907 untagged_variant_decl
=
1908 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1910 if (!untagged_variant_decl
) {
1911 _PERROR("cannot find \"variant %s\"", name
);
1916 /* Make a copy of it */
1917 variant_decl_copy
= bt_ctf_field_type_copy(
1918 untagged_variant_decl
);
1919 if (!variant_decl_copy
) {
1921 "cannot create copy of variant field type");
1926 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1928 struct ctf_node
*entry_node
;
1931 struct bt_ctf_field_type
*evariant_decl
=
1932 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1935 if (evariant_decl
) {
1936 BT_PUT(evariant_decl
);
1937 _PERROR("\"variant %s\" already declared in local scope",
1944 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1946 if (!untagged_variant_decl
) {
1947 _PERROR("%s", "cannot create variant field type");
1952 ret
= ctx_push_scope(ctx
);
1954 _PERROR("%s", "cannot push scope");
1958 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1959 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1960 untagged_variant_decl
);
1970 ret
= ctx_decl_scope_register_variant(
1971 ctx
->current_scope
, name
,
1972 untagged_variant_decl
);
1974 _PERROR("cannot register \"variant %s\" in declaration scope",
1982 * If tagged, create tagged variant and return; otherwise
1983 * return untagged variant.
1986 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1989 * At this point, we have a fresh untagged variant; nobody
1990 * else owns it. Set its tag now.
1992 ret
= bt_ctf_field_type_variant_set_tag_name(
1993 untagged_variant_decl
, tag
);
1998 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2001 assert(!untagged_variant_decl
);
2002 assert(*variant_decl
);
2007 BT_PUT(untagged_variant_decl
);
2008 BT_PUT(*variant_decl
);
2014 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2015 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
2019 struct ctf_node
*iter
;
2020 int64_t start
= 0, end
= 0;
2021 const char *label
= enumerator
->u
.enumerator
.id
;
2022 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2024 bt_list_for_each_entry(iter
, values
, siblings
) {
2027 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2028 _PERROR("wrong unary expression for enumeration label \"%s\"",
2040 switch (iter
->u
.unary_expression
.type
) {
2041 case UNARY_SIGNED_CONSTANT
:
2042 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2044 case UNARY_UNSIGNED_CONSTANT
:
2046 iter
->u
.unary_expression
.u
.unsigned_constant
;
2049 _PERROR("invalid enumeration entry: \"%s\"",
2056 _PERROR("invalid enumeration entry: \"%s\"",
2076 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2079 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2080 label
, (uint64_t) start
, (uint64_t) end
);
2083 _PERROR("cannot add mapping to enumeration for label \"%s\"",
2095 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2096 struct ctf_node
*container_type
,
2097 struct bt_list_head
*enumerator_list
,
2099 struct bt_ctf_field_type
**enum_decl
)
2103 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2107 /* For named enum (without body), lookup in declaration scope */
2109 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2116 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2119 _PERROR("cannot find \"enum %s\"", name
);
2124 /* Make a copy of it */
2125 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2126 if (!enum_decl_copy
) {
2128 "cannot create copy of enumeration field type");
2134 BT_MOVE(*enum_decl
, enum_decl_copy
);
2136 struct ctf_node
*iter
;
2137 int64_t last_value
= 0;
2140 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2142 eenum_decl
= ctx_decl_scope_lookup_enum(
2143 ctx
->current_scope
, name
, 1);
2146 _PERROR("\"enum %s\" already declared in local scope",
2153 if (!container_type
) {
2154 integer_decl
= ctx_decl_scope_lookup_alias(
2155 ctx
->current_scope
, "int", -1);
2156 if (!integer_decl
) {
2157 _PERROR("%s", "cannot find \"int\" type for enumeration");
2162 ret
= visit_type_declarator(ctx
, container_type
,
2163 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2165 assert(!integer_decl
);
2171 assert(integer_decl
);
2173 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2174 _PERROR("%s", "container type for enumeration is not an integer");
2179 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2181 _PERROR("%s", "cannot create enumeration field type");
2186 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2187 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2189 bt_ctf_field_type_integer_get_signed(integer_decl
));
2196 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2204 BT_PUT(integer_decl
);
2209 BT_PUT(integer_decl
);
2216 int visit_type_specifier(struct ctx
*ctx
,
2217 struct ctf_node
*type_specifier_list
,
2218 struct bt_ctf_field_type
**decl
)
2221 GString
*str
= NULL
;
2222 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2225 str
= g_string_new("");
2226 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2231 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2233 _PERROR("cannot find type alias \"%s\"", str
->str
);
2238 /* Make a copy of the type declaration */
2239 decl_copy
= bt_ctf_field_type_copy(*decl
);
2241 _PERROR("%s", "cannot create field type copy");
2246 BT_MOVE(*decl
, decl_copy
);
2247 (void) g_string_free(str
, TRUE
);
2254 (void) g_string_free(str
, TRUE
);
2263 int visit_integer_decl(struct ctx
*ctx
,
2264 struct bt_list_head
*expressions
,
2265 struct bt_ctf_field_type
**integer_decl
)
2270 struct ctf_node
*expression
;
2271 uint64_t alignment
= 0, size
= 0;
2272 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2273 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2274 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2275 enum bt_ctf_byte_order byte_order
=
2276 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2278 *integer_decl
= NULL
;
2280 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2281 struct ctf_node
*left
, *right
;
2283 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2284 struct ctf_node
, siblings
);
2285 right
= _BT_LIST_FIRST_ENTRY(
2286 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2289 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2294 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2295 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2296 _PERROR_DUP_ATTR("signed",
2297 "integer declaration");
2302 signedness
= get_boolean(ctx
->efd
, right
);
2303 if (signedness
< 0) {
2308 _SET(&set
, _INTEGER_SIGNED_SET
);
2309 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2311 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2312 _PERROR_DUP_ATTR("byte_order",
2313 "integer declaration");
2318 byte_order
= get_real_byte_order(ctx
, right
);
2319 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2320 _PERROR("%s", "invalid \"byte_order\" attribute in integer declaration");
2325 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2326 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2327 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2328 _PERROR_DUP_ATTR("size",
2329 "integer declaration");
2334 if (right
->u
.unary_expression
.type
!=
2335 UNARY_UNSIGNED_CONSTANT
) {
2336 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting unsigned constant");
2341 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2343 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting positive constant");
2346 } else if (size
> 64) {
2347 _PERROR("%s", "invalid \"size\" attribute in integer declaration: integers over 64-bit are not supported as of this version");
2352 _SET(&set
, _INTEGER_SIZE_SET
);
2353 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2355 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2356 _PERROR_DUP_ATTR("align",
2357 "integer declaration");
2362 if (right
->u
.unary_expression
.type
!=
2363 UNARY_UNSIGNED_CONSTANT
) {
2364 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting unsigned constant");
2370 right
->u
.unary_expression
.u
.unsigned_constant
;
2371 if (!is_align_valid(alignment
)) {
2372 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting power of two");
2377 _SET(&set
, _INTEGER_ALIGN_SET
);
2378 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2379 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2380 _PERROR_DUP_ATTR("base", "integer declaration");
2385 switch (right
->u
.unary_expression
.type
) {
2386 case UNARY_UNSIGNED_CONSTANT
:
2388 uint64_t constant
= right
->u
.unary_expression
.
2389 u
.unsigned_constant
;
2393 base
= BT_CTF_INTEGER_BASE_BINARY
;
2396 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2399 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2402 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2405 _PERROR("invalid \"base\" attribute in integer declaration: %" PRIu64
,
2406 right
->u
.unary_expression
.u
.unsigned_constant
);
2414 char *s_right
= concatenate_unary_strings(
2415 &expression
->u
.ctf_expression
.right
);
2417 _PERROR("%s", "unexpected unary expression for integer declaration's \"base\" attribute");
2422 if (!strcmp(s_right
, "decimal") ||
2423 !strcmp(s_right
, "dec") ||
2424 !strcmp(s_right
, "d") ||
2425 !strcmp(s_right
, "i") ||
2426 !strcmp(s_right
, "u")) {
2427 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2428 } else if (!strcmp(s_right
, "hexadecimal") ||
2429 !strcmp(s_right
, "hex") ||
2430 !strcmp(s_right
, "x") ||
2431 !strcmp(s_right
, "X") ||
2432 !strcmp(s_right
, "p")) {
2433 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2434 } else if (!strcmp(s_right
, "octal") ||
2435 !strcmp(s_right
, "oct") ||
2436 !strcmp(s_right
, "o")) {
2437 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2438 } else if (!strcmp(s_right
, "binary") ||
2439 !strcmp(s_right
, "b")) {
2440 base
= BT_CTF_INTEGER_BASE_BINARY
;
2442 _PERROR("unexpected unary expression for integer declaration's \"base\" attribute: \"%s\"",
2453 _PERROR("%s", "invalid \"base\" attribute in integer declaration: expecting unsigned constant or unary string");
2458 _SET(&set
, _INTEGER_BASE_SET
);
2459 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2463 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2464 _PERROR_DUP_ATTR("encoding",
2465 "integer declaration");
2470 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2471 _PERROR("%s", "invalid \"encoding\" attribute in integer declaration: expecting unary string");
2476 s_right
= concatenate_unary_strings(
2477 &expression
->u
.ctf_expression
.right
);
2479 _PERROR("%s", "unexpected unary expression for integer declaration's \"encoding\" attribute");
2484 if (!strcmp(s_right
, "UTF8") ||
2485 !strcmp(s_right
, "utf8") ||
2486 !strcmp(s_right
, "utf-8") ||
2487 !strcmp(s_right
, "UTF-8")) {
2488 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2489 } else if (!strcmp(s_right
, "ASCII") ||
2490 !strcmp(s_right
, "ascii")) {
2491 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2492 } else if (!strcmp(s_right
, "none")) {
2493 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2495 _PERROR("invalid \"encoding\" attribute in integer declaration: unknown encoding \"%s\"",
2503 _SET(&set
, _INTEGER_ENCODING_SET
);
2504 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2505 const char *clock_name
;
2507 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2508 _PERROR_DUP_ATTR("map", "integer declaration");
2513 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2514 _PERROR("%s", "invalid \"map\" attribute in integer declaration: expecting unary string");
2520 get_map_clock_name_value(
2521 &expression
->u
.ctf_expression
.right
);
2523 char *s_right
= concatenate_unary_strings(
2524 &expression
->u
.ctf_expression
.right
);
2527 _PERROR("%s", "unexpected unary expression for integer declaration's \"map\" attribute");
2532 _PWARNING("invalid \"map\" attribute in integer declaration: unknown clock class: \"%s\"",
2534 _SET(&set
, _INTEGER_MAP_SET
);
2539 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2540 ctx
->trace
, clock_name
);
2541 if (!mapped_clock
) {
2542 _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock class \"%s\"",
2548 _SET(&set
, _INTEGER_MAP_SET
);
2550 _PWARNING("unknown attribute \"%s\" in integer declaration",
2551 left
->u
.unary_expression
.u
.string
);
2555 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2557 "missing \"size\" attribute in integer declaration");
2562 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2563 if (size
% CHAR_BIT
) {
2564 /* Bit-packed alignment */
2567 /* Byte-packed alignment */
2568 alignment
= CHAR_BIT
;
2572 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2573 if (!*integer_decl
) {
2574 _PERROR("%s", "cannot create integer field type");
2579 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2580 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2581 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2582 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2583 (unsigned int) alignment
);
2584 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2588 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2589 *integer_decl
, mapped_clock
);
2590 bt_put(mapped_clock
);
2591 mapped_clock
= NULL
;
2595 _PERROR("%s", "cannot configure integer field type");
2604 bt_put(mapped_clock
);
2607 BT_PUT(*integer_decl
);
2613 int visit_floating_point_number_decl(struct ctx
*ctx
,
2614 struct bt_list_head
*expressions
,
2615 struct bt_ctf_field_type
**float_decl
)
2619 struct ctf_node
*expression
;
2620 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2621 enum bt_ctf_byte_order byte_order
=
2622 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2626 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2627 struct ctf_node
*left
, *right
;
2629 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2630 struct ctf_node
, siblings
);
2631 right
= _BT_LIST_FIRST_ENTRY(
2632 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2635 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2640 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2641 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2642 _PERROR_DUP_ATTR("byte_order",
2643 "floating point number declaration");
2648 byte_order
= get_real_byte_order(ctx
, right
);
2649 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2650 _PERROR("%s", "invalid \"byte_order\" attribute in floating point number declaration");
2655 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2656 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2658 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2659 _PERROR_DUP_ATTR("exp_dig",
2660 "floating point number declaration");
2665 if (right
->u
.unary_expression
.type
!=
2666 UNARY_UNSIGNED_CONSTANT
) {
2667 _PERROR("%s", "invalid \"exp_dig\" attribute in floating point number declaration: expecting unsigned constant");
2672 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2673 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2674 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2676 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2677 _PERROR_DUP_ATTR("mant_dig",
2678 "floating point number declaration");
2683 if (right
->u
.unary_expression
.type
!=
2684 UNARY_UNSIGNED_CONSTANT
) {
2685 _PERROR("%s", "invalid \"mant_dig\" attribute in floating point number declaration: expecting unsigned constant");
2690 mant_dig
= right
->u
.unary_expression
.u
.
2692 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2693 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2695 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2696 _PERROR_DUP_ATTR("align",
2697 "floating point number declaration");
2702 if (right
->u
.unary_expression
.type
!=
2703 UNARY_UNSIGNED_CONSTANT
) {
2704 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting unsigned constant");
2709 alignment
= right
->u
.unary_expression
.u
.
2712 if (!is_align_valid(alignment
)) {
2713 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting power of two");
2718 _SET(&set
, _FLOAT_ALIGN_SET
);
2720 _PWARNING("unknown attribute \"%s\" in floating point number declaration",
2721 left
->u
.unary_expression
.u
.string
);
2725 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2726 _PERROR("%s", "missing \"mant_dig\" attribute in floating point number declaration");
2731 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2732 _PERROR("%s", "missing \"exp_dig\" attribute in floating point number declaration");
2737 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2738 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2739 /* Bit-packed alignment */
2742 /* Byte-packed alignment */
2743 alignment
= CHAR_BIT
;
2747 *float_decl
= bt_ctf_field_type_floating_point_create();
2750 "cannot create floating point number field type");
2755 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2756 *float_decl
, exp_dig
);
2757 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2758 *float_decl
, mant_dig
);
2759 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2760 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2763 "cannot configure floating point number field type");
2771 BT_PUT(*float_decl
);
2777 int visit_string_decl(struct ctx
*ctx
,
2778 struct bt_list_head
*expressions
,
2779 struct bt_ctf_field_type
**string_decl
)
2783 struct ctf_node
*expression
;
2784 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2786 *string_decl
= NULL
;
2788 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2789 struct ctf_node
*left
, *right
;
2791 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2792 struct ctf_node
, siblings
);
2793 right
= _BT_LIST_FIRST_ENTRY(
2794 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2797 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2802 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2805 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2806 _PERROR_DUP_ATTR("encoding",
2807 "string declaration");
2812 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2813 _PERROR("%s", "invalid \"encoding\" attribute in string declaration: expecting unary string");
2818 s_right
= concatenate_unary_strings(
2819 &expression
->u
.ctf_expression
.right
);
2821 _PERROR("%s", "unexpected unary expression for string declaration's \"encoding\" attribute");
2826 if (!strcmp(s_right
, "UTF8") ||
2827 !strcmp(s_right
, "utf8") ||
2828 !strcmp(s_right
, "utf-8") ||
2829 !strcmp(s_right
, "UTF-8")) {
2830 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2831 } else if (!strcmp(s_right
, "ASCII") ||
2832 !strcmp(s_right
, "ascii")) {
2833 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2834 } else if (!strcmp(s_right
, "none")) {
2835 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2837 _PERROR("invalid \"encoding\" attribute in string declaration: unknown encoding \"%s\"",
2845 _SET(&set
, _STRING_ENCODING_SET
);
2847 _PWARNING("unknown attribute \"%s\" in string declaration",
2848 left
->u
.unary_expression
.u
.string
);
2852 *string_decl
= bt_ctf_field_type_string_create();
2853 if (!*string_decl
) {
2854 _PERROR("%s", "cannot create string field type");
2859 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2861 _PERROR("%s", "cannot configure string field type");
2869 BT_PUT(*string_decl
);
2875 int visit_type_specifier_list(struct ctx
*ctx
,
2876 struct ctf_node
*ts_list
,
2877 struct bt_ctf_field_type
**decl
)
2880 struct ctf_node
*first
, *node
;
2884 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2889 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
2890 struct ctf_node
, siblings
);
2891 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2896 node
= first
->u
.type_specifier
.node
;
2898 switch (first
->u
.type_specifier
.type
) {
2899 case TYPESPEC_INTEGER
:
2900 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
2907 case TYPESPEC_FLOATING_POINT
:
2908 ret
= visit_floating_point_number_decl(ctx
,
2909 &node
->u
.floating_point
.expressions
, decl
);
2915 case TYPESPEC_STRING
:
2916 ret
= visit_string_decl(ctx
,
2917 &node
->u
.string
.expressions
, decl
);
2923 case TYPESPEC_STRUCT
:
2924 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
2925 &node
->u
._struct
.declaration_list
,
2926 node
->u
._struct
.has_body
,
2927 &node
->u
._struct
.min_align
, decl
);
2933 case TYPESPEC_VARIANT
:
2934 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
2935 node
->u
.variant
.choice
,
2936 &node
->u
.variant
.declaration_list
,
2937 node
->u
.variant
.has_body
, decl
);
2944 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
2945 node
->u
._enum
.container_type
,
2946 &node
->u
._enum
.enumerator_list
,
2947 node
->u
._enum
.has_body
, decl
);
2955 case TYPESPEC_SHORT
:
2958 case TYPESPEC_FLOAT
:
2959 case TYPESPEC_DOUBLE
:
2960 case TYPESPEC_SIGNED
:
2961 case TYPESPEC_UNSIGNED
:
2963 case TYPESPEC_COMPLEX
:
2964 case TYPESPEC_IMAGINARY
:
2965 case TYPESPEC_CONST
:
2966 case TYPESPEC_ID_TYPE
:
2967 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
2974 _PERROR("unexpected node type: %d",
2975 (int) first
->u
.type_specifier
.type
);
2991 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
2992 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
2997 _BT_CTF_FIELD_TYPE_INIT(decl
);
2999 switch (node
->type
) {
3001 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3002 &node
->u
._typedef
.type_declarators
);
3005 "cannot add typedef in \"event\" declaration");
3009 case NODE_TYPEALIAS
:
3010 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3011 node
->u
.typealias
.alias
);
3013 _PERROR("%s", "cannot add typealias in \"event\" declaration");
3017 case NODE_CTF_EXPRESSION
:
3019 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3025 if (!strcmp(left
, "name")) {
3026 /* This is already known at this stage */
3027 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3028 _PERROR_DUP_ATTR("name", "event declaration");
3033 _SET(set
, _EVENT_NAME_SET
);
3034 } else if (!strcmp(left
, "id")) {
3037 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3038 _PERROR_DUP_ATTR("id", "event declaration");
3043 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3045 /* Only read "id" if get_unary_unsigned() succeeded. */
3046 if (ret
|| (!ret
&& id
< 0)) {
3047 _PERROR("%s", "unexpected unary expression for event declaration's \"id\" attribute");
3052 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3055 "cannot set event declaration's ID");
3059 _SET(set
, _EVENT_ID_SET
);
3060 } else if (!strcmp(left
, "stream_id")) {
3061 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3062 _PERROR_DUP_ATTR("stream_id",
3063 "event declaration");
3068 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3069 (uint64_t *) stream_id
);
3071 * Only read "stream_id" if get_unary_unsigned()
3074 if (ret
|| (!ret
&& *stream_id
< 0)) {
3075 _PERROR("%s", "unexpected unary expression for event declaration's \"stream_id\" attribute");
3080 _SET(set
, _EVENT_STREAM_ID_SET
);
3081 } else if (!strcmp(left
, "context")) {
3082 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3083 _PERROR("%s", "duplicate \"context\" entry in event declaration");
3088 ret
= visit_type_specifier_list(ctx
,
3089 _BT_LIST_FIRST_ENTRY(
3090 &node
->u
.ctf_expression
.right
,
3091 struct ctf_node
, siblings
),
3094 _PERROR("%s", "cannot create event context declaration");
3099 ret
= bt_ctf_event_class_set_context_type(
3103 _PERROR("%s", "cannot set event's context declaration");
3107 _SET(set
, _EVENT_CONTEXT_SET
);
3108 } else if (!strcmp(left
, "fields")) {
3109 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3110 _PERROR("%s", "duplicate \"fields\" entry in event declaration");
3115 ret
= visit_type_specifier_list(ctx
,
3116 _BT_LIST_FIRST_ENTRY(
3117 &node
->u
.ctf_expression
.right
,
3118 struct ctf_node
, siblings
),
3121 _PERROR("%s", "cannot create event payload field type");
3126 ret
= bt_ctf_event_class_set_payload_type(
3130 _PERROR("%s", "cannot set event's payload field type");
3134 _SET(set
, _EVENT_FIELDS_SET
);
3135 } else if (!strcmp(left
, "loglevel")) {
3136 uint64_t loglevel_value
;
3137 const char *loglevel_str
;
3138 struct bt_value
*value_obj
, *str_obj
;
3140 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3141 _PERROR_DUP_ATTR("loglevel",
3142 "event declaration");
3147 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3150 _PERROR("%s", "unexpected unary expression for event declaration's \"loglevel\" attribute");
3154 value_obj
= bt_value_integer_create_init(loglevel_value
);
3156 _PERROR("%s", "cannot allocate memory for loglevel value object");
3160 if (bt_ctf_event_class_set_attribute(event_class
,
3161 "loglevel", value_obj
) != BT_VALUE_STATUS_OK
) {
3162 _PERROR("%s", "cannot set loglevel value");
3167 loglevel_str
= print_loglevel(loglevel_value
);
3169 str_obj
= bt_value_string_create_init(loglevel_str
);
3170 if (bt_ctf_event_class_set_attribute(event_class
,
3171 "loglevel_string", str_obj
) != BT_VALUE_STATUS_OK
) {
3172 _PERROR("%s", "cannot set loglevel string");
3180 _SET(set
, _EVENT_LOGLEVEL_SET
);
3181 } else if (!strcmp(left
, "model.emf.uri")) {
3184 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3185 _PERROR_DUP_ATTR("model.emf.uri",
3186 "event declaration");
3191 right
= concatenate_unary_strings(
3192 &node
->u
.ctf_expression
.right
);
3194 _PERROR("%s", "unexpected unary expression for event declaration's \"model.emf.uri\" attribute");
3199 // TODO: FIXME: set model EMF URI here
3202 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3204 _PWARNING("unknown attribute \"%s\" in event declaration",
3230 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3234 struct ctf_node
*iter
;
3235 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3237 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3238 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3242 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3247 if (!strcmp(left
, "name")) {
3248 name
= concatenate_unary_strings(
3249 &iter
->u
.ctf_expression
.right
);
3251 _PERROR("%s", "unexpected unary expression for event declaration's \"name\" attribute");
3273 int reset_event_decl_types(struct ctx
*ctx
,
3274 struct bt_ctf_event_class
*event_class
)
3279 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3281 _PERROR("%s", "cannot set initial NULL event context");
3285 /* Event payload. */
3286 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3288 _PERROR("%s", "cannot set initial NULL event payload");
3296 int reset_stream_decl_types(struct ctx
*ctx
,
3297 struct bt_ctf_stream_class
*stream_class
)
3301 /* Packet context. */
3302 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3304 _PERROR("%s", "cannot set initial empty packet context");
3309 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3311 _PERROR("%s", "cannot set initial empty event header");
3315 /* Event context. */
3316 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3318 _PERROR("%s", "cannot set initial empty stream event context");
3326 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3329 struct bt_ctf_stream_class
*stream_class
;
3331 stream_class
= bt_ctf_stream_class_create(NULL
);
3332 if (!stream_class
) {
3333 _PERROR("%s", "cannot create stream class");
3338 * Set packet context, event header, and event context to NULL to
3339 * override the default ones.
3341 ret
= reset_stream_decl_types(ctx
, stream_class
);
3346 return stream_class
;
3349 BT_PUT(stream_class
);
3355 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3360 struct ctf_node
*iter
;
3361 int64_t stream_id
= -1;
3362 char *event_name
= NULL
;
3363 struct bt_ctf_event_class
*event_class
= NULL
;
3364 struct bt_ctf_event_class
*eevent_class
;
3365 struct bt_ctf_stream_class
*stream_class
= NULL
;
3366 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3367 bool pop_scope
= false;
3369 if (node
->visited
) {
3373 node
->visited
= TRUE
;
3374 event_name
= get_event_decl_name(ctx
, node
);
3377 "missing \"name\" attribute in event declaration");
3382 event_class
= bt_ctf_event_class_create(event_name
);
3385 * Unset context and fields to override the default ones.
3387 ret
= reset_event_decl_types(ctx
, event_class
);
3392 ret
= ctx_push_scope(ctx
);
3394 _PERROR("%s", "cannot push scope");
3400 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3401 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3408 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3410 struct bt_ctf_stream_class
*new_stream_class
;
3411 size_t stream_class_count
=
3412 g_hash_table_size(ctx
->stream_classes
) +
3413 bt_ctf_trace_get_stream_class_count(ctx
->trace
);
3416 * Allow missing stream_id if there is only a single
3419 switch (stream_class_count
) {
3421 /* Create implicit stream class if there's none */
3422 new_stream_class
= create_reset_stream_class(ctx
);
3423 if (!new_stream_class
) {
3428 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3430 _PERROR("%s", "cannot set stream class's ID");
3431 BT_PUT(new_stream_class
);
3437 /* Move reference to visitor's context */
3438 g_hash_table_insert(ctx
->stream_classes
,
3439 (gpointer
) stream_id
, new_stream_class
);
3440 new_stream_class
= NULL
;
3443 /* Single stream class: get its ID */
3444 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3445 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3446 stream_id
= (int64_t) keys
->data
;
3449 assert(bt_ctf_trace_get_stream_class_count(
3452 bt_ctf_trace_get_stream_class_by_index(
3454 assert(stream_class
);
3455 stream_id
= bt_ctf_stream_class_get_id(
3457 BT_PUT(stream_class
);
3461 _PERROR("%s", "missing \"stream_id\" attribute in event declaration");
3467 assert(stream_id
>= 0);
3469 /* We have the stream ID now; get the stream class if found */
3470 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3471 (gpointer
) stream_id
);
3472 bt_get(stream_class
);
3473 if (!stream_class
) {
3474 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3476 if (!stream_class
) {
3477 _PERROR("cannot find stream class with ID %" PRId64
,
3484 assert(stream_class
);
3486 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3487 /* Allow only one event without ID per stream */
3488 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3491 "missing \"id\" field in event declaration");
3497 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3499 _PERROR("%s", "cannot set event's ID");
3504 event_id
= bt_ctf_event_class_get_id(event_class
);
3506 _PERROR("%s", "cannot get event's ID");
3511 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3514 BT_PUT(eevent_class
);
3515 _PERROR("duplicate event with ID %" PRId64
" in same stream", event_id
);
3520 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3521 BT_PUT(event_class
);
3523 _PERROR("%s", "cannot add event class to stream class");
3530 bt_put(event_class
);
3538 bt_put(stream_class
);
3543 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3544 struct bt_ctf_stream_class
*stream_class
, int *set
)
3548 _BT_CTF_FIELD_TYPE_INIT(decl
);
3550 switch (node
->type
) {
3552 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3553 &node
->u
._typedef
.type_declarators
);
3556 "cannot add typedef in \"stream\" declaration");
3560 case NODE_TYPEALIAS
:
3561 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3562 node
->u
.typealias
.alias
);
3564 _PERROR("%s", "cannot add typealias in \"stream\" declaration");
3568 case NODE_CTF_EXPRESSION
:
3570 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3576 if (!strcmp(left
, "id")) {
3580 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3581 _PERROR_DUP_ATTR("id", "stream declaration");
3586 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3588 /* Only read "id" if get_unary_unsigned() succeeded. */
3589 if (ret
|| (!ret
&& id
< 0)) {
3590 _PERROR("%s", "unexpected unary expression for stream declaration's \"id\" attribute");
3595 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3598 _PERROR("duplicate stream with ID %" PRId64
,
3604 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3607 "cannot set stream class's ID");
3611 _SET(set
, _STREAM_ID_SET
);
3612 } else if (!strcmp(left
, "event.header")) {
3613 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3614 _PERROR("%s", "duplicate \"event.header\" entry in stream declaration");
3619 ret
= visit_type_specifier_list(ctx
,
3620 _BT_LIST_FIRST_ENTRY(
3621 &node
->u
.ctf_expression
.right
,
3622 struct ctf_node
, siblings
),
3625 _PERROR("%s", "cannot create event header field type");
3631 ret
= bt_ctf_stream_class_set_event_header_type(
3632 stream_class
, decl
);
3635 _PERROR("%s", "cannot set stream's event header field type");
3639 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3640 } else if (!strcmp(left
, "event.context")) {
3641 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3642 _PERROR("%s", "duplicate \"event.context\" entry in stream declaration");
3647 ret
= visit_type_specifier_list(ctx
,
3648 _BT_LIST_FIRST_ENTRY(
3649 &node
->u
.ctf_expression
.right
,
3650 struct ctf_node
, siblings
),
3653 _PERROR("%s", "cannot create stream event context field type");
3659 ret
= bt_ctf_stream_class_set_event_context_type(
3660 stream_class
, decl
);
3663 _PERROR("%s", "cannot set stream's event context field type");
3667 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3668 } else if (!strcmp(left
, "packet.context")) {
3669 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3670 _PERROR("%s", "duplicate \"packet.context\" entry in stream declaration");
3675 ret
= visit_type_specifier_list(ctx
,
3676 _BT_LIST_FIRST_ENTRY(
3677 &node
->u
.ctf_expression
.right
,
3678 struct ctf_node
, siblings
),
3681 _PERROR("%s", "cannot create packet context field type");
3687 ret
= bt_ctf_stream_class_set_packet_context_type(
3688 stream_class
, decl
);
3691 _PERROR("%s", "cannot set stream's packet context field type");
3695 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3697 _PWARNING("unknown attribute \"%s\" in stream declaration",
3721 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3726 struct ctf_node
*iter
;
3727 struct bt_ctf_stream_class
*stream_class
= NULL
;
3728 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
3729 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3731 if (node
->visited
) {
3735 node
->visited
= TRUE
;
3736 stream_class
= create_reset_stream_class(ctx
);
3737 if (!stream_class
) {
3742 ret
= ctx_push_scope(ctx
);
3744 _PERROR("%s", "cannot push scope");
3748 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3749 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3758 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3759 /* Check that packet header has stream_id field */
3760 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
3761 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3763 packet_header_decl
=
3764 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
3765 if (!packet_header_decl
) {
3767 "cannot get trace packet header field type");
3772 bt_ctf_field_type_structure_get_field_type_by_name(
3773 packet_header_decl
, "stream_id");
3774 BT_PUT(packet_header_decl
);
3775 if (!stream_id_decl
) {
3776 _PERROR("%s", "missing \"stream_id\" field in packet header declaration, but \"id\" attribute is declared for stream");
3780 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
3781 BT_PUT(stream_id_decl
);
3782 _PERROR("%s", "\"stream_id\" field in packet header declaration is not an integer");
3786 BT_PUT(stream_id_decl
);
3788 /* Allow only _one_ ID-less stream */
3789 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
3791 "missing \"id\" field in stream declaration");
3796 /* Automatic ID: 0 */
3797 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
3800 id
= bt_ctf_stream_class_get_id(stream_class
);
3802 _PERROR("wrong stream ID: %" PRId64
, id
);
3808 * Make sure that this stream class's ID is currently unique in
3811 if (g_hash_table_lookup(ctx
->stream_classes
, (gpointer
) id
)) {
3812 _PERROR("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3817 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3819 if (existing_stream_class
) {
3820 _PERROR("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3825 /* Move reference to visitor's context */
3826 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
3828 stream_class
= NULL
;
3832 bt_put(stream_class
);
3835 bt_put(existing_stream_class
);
3840 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
3844 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3846 switch (node
->type
) {
3848 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3849 &node
->u
._typedef
.type_declarators
);
3852 "cannot add typedef in \"trace\" declaration");
3856 case NODE_TYPEALIAS
:
3857 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3858 node
->u
.typealias
.alias
);
3861 "cannot add typealias in \"trace\" declaration");
3865 case NODE_CTF_EXPRESSION
:
3867 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3873 if (!strcmp(left
, "major")) {
3874 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3875 _PERROR_DUP_ATTR("major", "trace declaration");
3880 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3883 _PERROR("%s", "unexpected unary expression for trace's \"major\" attribute");
3888 _SET(set
, _TRACE_MAJOR_SET
);
3889 } else if (!strcmp(left
, "minor")) {
3890 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3891 _PERROR_DUP_ATTR("minor", "trace declaration");
3896 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3899 _PERROR("%s", "unexpected unary expression for trace's \"minor\" attribute");
3904 _SET(set
, _TRACE_MINOR_SET
);
3905 } else if (!strcmp(left
, "uuid")) {
3906 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3907 _PERROR_DUP_ATTR("uuid", "trace declaration");
3912 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
3916 "invalid trace declaration's UUID");
3920 ret
= bt_ctf_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
3923 "cannot set trace's UUID");
3927 _SET(set
, _TRACE_UUID_SET
);
3928 } else if (!strcmp(left
, "byte_order")) {
3929 /* Native byte order is already known at this stage */
3930 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3931 _PERROR_DUP_ATTR("byte_order",
3932 "trace declaration");
3937 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3938 } else if (!strcmp(left
, "packet.header")) {
3939 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3940 _PERROR("%s", "duplicate \"packet.header\" entry in trace declaration");
3945 ret
= visit_type_specifier_list(ctx
,
3946 _BT_LIST_FIRST_ENTRY(
3947 &node
->u
.ctf_expression
.right
,
3948 struct ctf_node
, siblings
),
3949 &packet_header_decl
);
3951 _PERROR("%s", "cannot create packet header field type");
3955 assert(packet_header_decl
);
3956 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
3957 packet_header_decl
);
3958 BT_PUT(packet_header_decl
);
3960 _PERROR("%s", "cannot set trace's packet header field type");
3964 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3966 _PWARNING("%s", "unknown attribute \"%s\" in trace declaration");
3974 _PERROR("%s", "unknown expression in trace declaration");
3983 BT_PUT(packet_header_decl
);
3989 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3993 struct ctf_node
*iter
;
3994 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3996 if (node
->visited
) {
4000 node
->visited
= TRUE
;
4002 if (ctx
->is_trace_visited
) {
4003 _PERROR("%s", "duplicate \"trace\" block");
4008 ret
= ctx_push_scope(ctx
);
4010 _PERROR("%s", "cannot push scope");
4014 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4015 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4024 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4026 "missing \"major\" attribute in trace declaration");
4031 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4033 "missing \"minor\" attribute in trace declaration");
4038 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4039 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4044 ctx
->is_trace_visited
= TRUE
;
4054 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4058 struct ctf_node
*entry_node
;
4059 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4061 if (node
->visited
) {
4065 node
->visited
= TRUE
;
4067 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4068 struct bt_list_head
*right_head
=
4069 &entry_node
->u
.ctf_expression
.right
;
4071 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4072 _PERROR("%s", "wrong expression in environment entry");
4077 left
= concatenate_unary_strings(
4078 &entry_node
->u
.ctf_expression
.left
);
4080 _PERROR("%s", "cannot get environment entry name");
4085 if (is_unary_string(right_head
)) {
4086 char *right
= concatenate_unary_strings(right_head
);
4089 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4095 if (strcmp(left
, "tracer_name") == 0) {
4096 if (strncmp(right
, "lttng", 5) == 0) {
4101 printf_verbose("env.%s = \"%s\"\n", left
, right
);
4102 ret
= bt_ctf_trace_set_environment_field_string(
4103 ctx
->trace
, left
, right
);
4107 _PERROR("environment: cannot add entry \"%s\" to trace",
4111 } else if (is_unary_unsigned(right_head
) ||
4112 is_unary_signed(right_head
)) {
4115 if (is_unary_unsigned(right_head
)) {
4116 ret
= get_unary_unsigned(right_head
,
4119 ret
= get_unary_signed(right_head
, &v
);
4122 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4128 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
4129 ret
= bt_ctf_trace_set_environment_field_integer(
4130 ctx
->trace
, left
, v
);
4132 _PERROR("environment: cannot add entry \"%s\" to trace",
4137 printf_verbose("%s: environment entry \"%s\" has unknown type\n",
4155 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4160 struct ctf_node
*node
;
4161 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4163 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4164 if (node
->type
== NODE_CTF_EXPRESSION
) {
4165 struct ctf_node
*right_node
;
4167 left
= concatenate_unary_strings(
4168 &node
->u
.ctf_expression
.left
);
4174 if (!strcmp(left
, "byte_order")) {
4175 enum bt_ctf_byte_order bo
;
4177 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4178 _PERROR_DUP_ATTR("byte_order",
4179 "trace declaration");
4184 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4185 right_node
= _BT_LIST_FIRST_ENTRY(
4186 &node
->u
.ctf_expression
.right
,
4187 struct ctf_node
, siblings
);
4188 bo
= byte_order_from_unary_expr(ctx
->efd
,
4190 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4191 _PERROR("%s", "unknown \"byte_order\" attribute in trace declaration");
4194 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4195 _PERROR("%s", "\"byte_order\" attribute cannot be set to \"native\" in trace declaration");
4201 ret
= bt_ctf_trace_set_native_byte_order(
4204 _PERROR("cannot set trace's byte order (%d)",
4215 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4216 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4230 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4231 struct bt_ctf_clock_class
*clock
, int *set
)
4236 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4241 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4247 if (!strcmp(left
, "name")) {
4250 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4251 _PERROR_DUP_ATTR("name", "clock class declaration");
4256 right
= concatenate_unary_strings(
4257 &entry_node
->u
.ctf_expression
.right
);
4259 _PERROR("%s", "unexpected unary expression for clock class declaration's \"name\" attribute");
4264 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4266 _PERROR("%s", "cannot set clock class's name");
4272 _SET(set
, _CLOCK_NAME_SET
);
4273 } else if (!strcmp(left
, "uuid")) {
4274 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4276 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4277 _PERROR_DUP_ATTR("uuid", "clock class declaration");
4282 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4284 _PERROR("%s", "invalid clock class UUID");
4288 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4290 _PERROR("%s", "cannot set clock class's UUID");
4294 _SET(set
, _CLOCK_UUID_SET
);
4295 } else if (!strcmp(left
, "description")) {
4298 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4299 _PERROR_DUP_ATTR("description", "clock class declaration");
4304 right
= concatenate_unary_strings(
4305 &entry_node
->u
.ctf_expression
.right
);
4307 _PERROR("%s", "unexpected unary expression for clock class's \"description\" attribute");
4312 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4314 _PERROR("%s", "cannot set clock class's description");
4320 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4321 } else if (!strcmp(left
, "freq")) {
4324 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4325 _PERROR_DUP_ATTR("freq", "clock class declaration");
4330 ret
= get_unary_unsigned(
4331 &entry_node
->u
.ctf_expression
.right
, &freq
);
4333 _PERROR("%s", "unexpected unary expression for clock class declaration's \"freq\" attribute");
4338 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4340 _PERROR("%s", "cannot set clock class's frequency");
4344 _SET(set
, _CLOCK_FREQ_SET
);
4345 } else if (!strcmp(left
, "precision")) {
4348 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4349 _PERROR_DUP_ATTR("precision", "clock class declaration");
4354 ret
= get_unary_unsigned(
4355 &entry_node
->u
.ctf_expression
.right
, &precision
);
4357 _PERROR("%s", "unexpected unary expression for clock class declaration's \"precision\" attribute");
4362 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4364 _PERROR("%s", "cannot set clock class's precision");
4368 _SET(set
, _CLOCK_PRECISION_SET
);
4369 } else if (!strcmp(left
, "offset_s")) {
4372 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4373 _PERROR_DUP_ATTR("offset_s", "clock class declaration");
4378 ret
= get_unary_unsigned(
4379 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4381 _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset_s\" attribute");
4386 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4388 _PERROR("%s", "cannot set clock class's offset in seconds");
4392 _SET(set
, _CLOCK_OFFSET_S_SET
);
4393 } else if (!strcmp(left
, "offset")) {
4396 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4397 _PERROR_DUP_ATTR("offset", "clock class declaration");
4402 ret
= get_unary_unsigned(
4403 &entry_node
->u
.ctf_expression
.right
, &offset
);
4405 _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset\" attribute");
4410 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4412 _PERROR("%s", "cannot set clock class's offset in cycles");
4416 _SET(set
, _CLOCK_OFFSET_SET
);
4417 } else if (!strcmp(left
, "absolute")) {
4418 struct ctf_node
*right
;
4420 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4421 _PERROR_DUP_ATTR("absolute", "clock class declaration");
4426 right
= _BT_LIST_FIRST_ENTRY(
4427 &entry_node
->u
.ctf_expression
.right
,
4428 struct ctf_node
, siblings
);
4429 ret
= get_boolean(ctx
->efd
, right
);
4431 _PERROR("%s", "unexpected unary expression for clock class declaration's \"absolute\" attribute");
4436 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4438 _PERROR("%s", "cannot set clock class's absolute option");
4442 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4444 _PWARNING("unknown attribute \"%s\" in clock class declaration",
4460 uint64_t cycles_from_ns(uint64_t frequency
, uint64_t ns
)
4465 if (frequency
== 1000000000ULL) {
4468 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4475 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4479 int64_t offset_cycles
;
4481 freq
= bt_ctf_clock_class_get_frequency(clock
);
4482 if (freq
== -1ULL) {
4483 _PERROR("%s", "cannot get clock class frequency");
4488 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4490 _PERROR("%s", "cannot get offset in cycles");
4495 offset_cycles
+= cycles_from_ns(freq
, ctx
->clock_class_offset_ns
);
4496 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4503 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4507 struct bt_ctf_clock_class
*clock
;
4508 struct ctf_node
*entry_node
;
4509 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4510 const char *clock_class_name
;
4512 if (clock_node
->visited
) {
4516 clock_node
->visited
= TRUE
;
4517 clock
= bt_ctf_clock_class_create(NULL
);
4519 _PERROR("%s", "cannot create clock");
4524 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4525 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4531 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4533 "missing \"name\" attribute in clock class declaration");
4538 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
4539 assert(clock_class_name
);
4540 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4542 * Old versions of LTTng forgot to set its clock class
4543 * as absolute, even if it is. This is important because
4544 * it's a condition to be able to sort notifications
4545 * from different sources.
4547 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
4549 _PERROR("%s", "cannot set clock class's absolute option");
4554 ret
= apply_clock_class_offset(ctx
, clock
);
4556 _PERROR("%s", "cannot apply clock class offset ");
4560 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
4562 _PERROR("%s", "cannot add clock class to trace");
4573 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4577 if (root_decl_node
->visited
) {
4581 root_decl_node
->visited
= TRUE
;
4583 switch (root_decl_node
->type
) {
4585 ret
= visit_typedef(ctx
,
4586 root_decl_node
->u
._typedef
.type_specifier_list
,
4587 &root_decl_node
->u
._typedef
.type_declarators
);
4589 _PERROR("%s", "cannot add typedef in root scope");
4593 case NODE_TYPEALIAS
:
4594 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4595 root_decl_node
->u
.typealias
.alias
);
4597 _PERROR("%s", "cannot add typealias in root scope");
4601 case NODE_TYPE_SPECIFIER_LIST
:
4603 _BT_CTF_FIELD_TYPE_INIT(decl
);
4606 * Just add the type specifier to the root
4607 * declaration scope. Put local reference.
4609 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4628 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
4631 GHashTableIter iter
;
4632 gpointer key
, stream_class
;
4634 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
4636 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
4637 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
4640 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
4641 _PERROR("cannot add stream class %" PRId64
" to trace",
4647 g_hash_table_remove_all(ctx
->stream_classes
);
4654 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(FILE *efd
,
4655 uint64_t clock_class_offset_ns
, const char *name
)
4658 struct ctx
*ctx
= NULL
;
4659 struct bt_ctf_trace
*trace
;
4661 trace
= bt_ctf_trace_create();
4663 _FPERROR(efd
, "%s", "cannot create trace");
4667 ret
= bt_ctf_trace_set_name(trace
, name
);
4669 _FPERROR(efd
, "cannot set trace's name to `%s`", name
);
4673 /* Set packet header to NULL to override the default one */
4674 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
4677 "cannot set initial, empty packet header structure");
4681 /* Create visitor's context */
4682 ctx
= ctx_create(trace
, efd
, clock_class_offset_ns
);
4684 _FPERROR(efd
, "%s", "cannot create visitor context");
4697 return (void *) ctx
;
4701 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
4703 ctx_destroy((void *) visitor
);
4707 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
4708 struct ctf_visitor_generate_ir
*visitor
)
4710 struct ctx
*ctx
= (void *) visitor
;
4714 return bt_get(ctx
->trace
);
4718 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
4719 struct ctf_node
*node
)
4722 struct ctx
*ctx
= (void *) visitor
;
4724 printf_verbose("CTF visitor: AST -> CTF IR...\n");
4726 switch (node
->type
) {
4729 struct ctf_node
*iter
;
4730 int got_trace_decl
= FALSE
;
4731 int found_callsite
= FALSE
;
4734 * The first thing we need is the native byte order of
4735 * the trace block, because early type aliases can have
4736 * a `byte_order` attribute set to `native`. If we don't
4737 * have the native byte order yet, and we don't have any
4738 * trace block yet, then fail with EINCOMPLETE.
4740 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4741 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4742 if (got_trace_decl
) {
4743 _PERROR("%s", "duplicate trace declaration");
4748 ret
= set_trace_byte_order(ctx
, iter
);
4750 _PERROR("cannot set trace's native byte order (%d)",
4755 got_trace_decl
= TRUE
;
4758 if (!got_trace_decl
) {
4764 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
4765 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
4766 assert(ctx
->current_scope
&&
4767 ctx
->current_scope
->parent_scope
== NULL
);
4770 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
4771 ret
= visit_env(ctx
, iter
);
4773 _PERROR("error while visiting environment block (%d)",
4779 assert(ctx
->current_scope
&&
4780 ctx
->current_scope
->parent_scope
== NULL
);
4783 * Visit clock blocks.
4785 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
4786 ret
= visit_clock_decl(ctx
, iter
);
4788 _PERROR("error while visiting clock class declaration (%d)",
4794 assert(ctx
->current_scope
&&
4795 ctx
->current_scope
->parent_scope
== NULL
);
4798 * Visit root declarations next, as they can be used by any
4801 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
4803 ret
= visit_root_decl(ctx
, iter
);
4805 _PERROR("error while visiting root declaration (%d)",
4811 assert(ctx
->current_scope
&&
4812 ctx
->current_scope
->parent_scope
== NULL
);
4814 /* Callsite blocks are not supported */
4815 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
4816 found_callsite
= TRUE
;
4820 assert(ctx
->current_scope
&&
4821 ctx
->current_scope
->parent_scope
== NULL
);
4823 if (found_callsite
) {
4824 _PWARNING("%s", "\"callsite\" blocks are not supported as of this version");
4828 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4829 ret
= visit_trace_decl(ctx
, iter
);
4831 _PERROR("%s", "error while visiting trace declaration");
4836 assert(ctx
->current_scope
&&
4837 ctx
->current_scope
->parent_scope
== NULL
);
4840 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
4841 ret
= visit_stream_decl(ctx
, iter
);
4843 _PERROR("%s", "error while visiting stream declaration");
4848 assert(ctx
->current_scope
&&
4849 ctx
->current_scope
->parent_scope
== NULL
);
4852 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
4853 ret
= visit_event_decl(ctx
, iter
);
4855 _PERROR("%s", "error while visiting event declaration");
4860 assert(ctx
->current_scope
&&
4861 ctx
->current_scope
->parent_scope
== NULL
);
4865 _PERROR("cannot decode node type: %d", (int) node
->type
);
4870 /* Move decoded stream classes to trace, if any */
4871 ret
= move_ctx_stream_classes_to_trace(ctx
);
4873 _PERROR("%s", "cannot move stream classes to trace");